Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions layers/CognitiveLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import logging
import queue
import pathlib
import threading
import time

from reasoning_engines.GPTModels import GPTModel
from capability_manager import CapabilityManager
Expand Down Expand Up @@ -68,6 +70,9 @@ def __init__(self, name):
self.total_tokens: int = 0
self.total_cost: float = 0

self._stop_event = threading.Event()
self.running = False

# Set up logging
self.logger = logging.getLogger(name) # Create a logger for this layer
self.logger.setLevel(logging.DEBUG) # Set the logging level
Expand Down Expand Up @@ -251,3 +256,18 @@ def create_product(self):

raise NotImplementedError("Subclasses must implement create_product method.")

def stop(self):
"""Signal the main loop to stop."""
self._stop_event.set()

def main_loop(self):
"""Run the layer until stop is requested."""
self.running = True
while not self._stop_event.is_set():
try:
self.execute()
except Exception as e:
self.logger.error(f"Error in main loop: {e}")
time.sleep(0.1)
self.running = False

49 changes: 24 additions & 25 deletions orchestration/CognitiveArchitecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,43 +96,42 @@ def _check_thread_status(self):
print(f'{key}: running={thread.running()}, done={thread.done()}')

def start_execution(self):
"""
Start the execution of all layers in separate threads.
"""
# Create a thread for each layer
aspirational_thread = threading.Thread(target=self.aspirational_layer.main_loop)
"""Start all layers in separate threads."""
aspirational_thread = threading.Thread(target=self.aspirational_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.ASPIRATIONAL] = aspirational_thread

global_strategy_thread = threading.Thread(target=self.global_strategy_layer.main_loop)
global_strategy_thread = threading.Thread(target=self.global_strategy_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.GLOBAL_STRATEGY] = global_strategy_thread

agent_model_thread = threading.Thread(target=self.agent_model_layer.main_loop)
agent_model_thread = threading.Thread(target=self.agent_model_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.AGENT_MODEL] = agent_model_thread

executive_function_thread = threading.Thread(target=self.executive_function_layer.main_loop)
executive_function_thread = threading.Thread(target=self.executive_function_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.EXECUTIVE_FUNCTION] = executive_function_thread

cognitive_control_thread = threading.Thread(target=self.cognitive_control_layer.main_loop)
cognitive_control_thread = threading.Thread(target=self.cognitive_control_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.COGNITIVE_CONTROL] = cognitive_control_thread

task_prosecution_thread = threading.Thread(target=self.task_prosecution_layer.main_loop)
task_prosecution_thread = threading.Thread(target=self.task_prosecution_layer.main_loop, daemon=True)
self.threads[LayerHierarchy.TASK_PROSECUTION] = task_prosecution_thread

# Start all threads
aspirational_thread.start()
global_strategy_thread.start()
agent_model_thread.start()
executive_function_thread.start()
cognitive_control_thread.start()
task_prosecution_thread.start()

# Wait for all threads to finish
aspirational_thread.join()
global_strategy_thread.join()
agent_model_thread.join()
executive_function_thread.join()
cognitive_control_thread.join()
task_prosecution_thread.join()
for t in self.threads.values():
t.start()

def stop_execution(self):
"""Signal all layers to stop and wait for threads to finish."""
for layer in [
self.aspirational_layer,
self.global_strategy_layer,
self.agent_model_layer,
self.executive_function_layer,
self.cognitive_control_layer,
self.task_prosecution_layer,
]:
layer.stop()

for t in self.threads.values():
t.join()

def process_input(self, input_data):
"""
Expand Down
39 changes: 36 additions & 3 deletions ui/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from flask import Flask, request, redirect, url_for, render_template_string
import threading
import time
import signal
import atexit

from orchestration import CognitiveArchitecture

Expand All @@ -9,6 +11,11 @@
# Instantiate the cognitive architecture
architecture = CognitiveArchitecture()

# Control events for background threads
stop_event = threading.Event()
arch_thread = None
resource_thread = None

# Simple in-memory list of tasks
tasks = []

Expand All @@ -20,12 +27,14 @@
def run_architecture():
"""Start the cognitive architecture execution."""
architecture.start_execution()
stop_event.wait()
architecture.stop_execution()


def update_resources():
"""Periodically update resource status."""
global remaining_currency
while True:
while not stop_event.is_set():
if currency_resource:
try:
remaining_currency = currency_resource.get_remaining()
Expand All @@ -34,9 +43,33 @@ def update_resources():
time.sleep(5)


def start_threads():
global arch_thread, resource_thread
arch_thread = threading.Thread(target=run_architecture, daemon=True)
resource_thread = threading.Thread(target=update_resources, daemon=True)
arch_thread.start()
resource_thread.start()


def stop_threads(*args):
stop_event.set()
if arch_thread:
arch_thread.join()
if resource_thread:
resource_thread.join()


# Start background threads
threading.Thread(target=run_architecture, daemon=True).start()
threading.Thread(target=update_resources, daemon=True).start()
start_threads()

@app.teardown_appcontext
def shutdown_session(exception=None):
stop_threads()

# Ensure background threads shut down with the Flask app
atexit.register(stop_threads)
signal.signal(signal.SIGINT, stop_threads)
signal.signal(signal.SIGTERM, stop_threads)


@app.route('/tasks')
Expand Down