WIP: I HATE PYTHON
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
import ai_client
|
||||
from src import ai_client
|
||||
import json
|
||||
import asyncio
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
from typing import List, Optional, Tuple
|
||||
from dataclasses import asdict
|
||||
import events
|
||||
from models import Ticket, Track, WorkerContext
|
||||
from file_cache import ASTParser
|
||||
from src import events
|
||||
from src.models import Ticket, Track, WorkerContext
|
||||
from src.file_cache import ASTParser
|
||||
from pathlib import Path
|
||||
|
||||
from dag_engine import TrackDAG, ExecutionEngine
|
||||
from src.dag_engine import TrackDAG, ExecutionEngine
|
||||
|
||||
class ConductorEngine:
|
||||
"""
|
||||
Orchestrates the execution of tickets within a track.
|
||||
"""
|
||||
|
||||
def __init__(self, track: Track, event_queue: Optional[events.AsyncEventQueue] = None, auto_queue: bool = False) -> None:
|
||||
def __init__(self, track: Track, event_queue: Optional[events.SyncEventQueue] = None, auto_queue: bool = False) -> None:
|
||||
self.track = track
|
||||
self.event_queue = event_queue
|
||||
self.tier_usage = {
|
||||
@@ -29,7 +29,7 @@ class ConductorEngine:
|
||||
self.dag = TrackDAG(self.track.tickets)
|
||||
self.engine = ExecutionEngine(self.dag, auto_queue=auto_queue)
|
||||
|
||||
async def _push_state(self, status: str = "running", active_tier: str = None) -> None:
|
||||
def _push_state(self, status: str = "running", active_tier: str = None) -> None:
|
||||
if not self.event_queue:
|
||||
return
|
||||
payload = {
|
||||
@@ -42,7 +42,7 @@ class ConductorEngine:
|
||||
},
|
||||
"tickets": [asdict(t) for t in self.track.tickets]
|
||||
}
|
||||
await self.event_queue.put("mma_state_update", payload)
|
||||
self.event_queue.put("mma_state_update", payload)
|
||||
|
||||
def parse_json_tickets(self, json_str: str) -> None:
|
||||
"""
|
||||
@@ -73,14 +73,14 @@ class ConductorEngine:
|
||||
except KeyError as e:
|
||||
print(f"Missing required field in ticket definition: {e}")
|
||||
|
||||
async def run(self, md_content: str = "") -> None:
|
||||
def run(self, md_content: str = "") -> None:
|
||||
"""
|
||||
Main execution loop using the DAG engine.
|
||||
Args:
|
||||
md_content: The full markdown context (history + files) for AI workers.
|
||||
"""
|
||||
await self._push_state(status="running", active_tier="Tier 2 (Tech Lead)")
|
||||
loop = asyncio.get_event_loop()
|
||||
self._push_state(status="running", active_tier="Tier 2 (Tech Lead)")
|
||||
|
||||
while True:
|
||||
# 1. Identify ready tasks
|
||||
ready_tasks = self.engine.tick()
|
||||
@@ -89,15 +89,15 @@ class ConductorEngine:
|
||||
all_done = all(t.status == "completed" for t in self.track.tickets)
|
||||
if all_done:
|
||||
print("Track completed successfully.")
|
||||
await self._push_state(status="done", active_tier=None)
|
||||
self._push_state(status="done", active_tier=None)
|
||||
else:
|
||||
# Check if any tasks are in-progress or could be ready
|
||||
if any(t.status == "in_progress" for t in self.track.tickets):
|
||||
# Wait for async tasks to complete
|
||||
await asyncio.sleep(1)
|
||||
# Wait for tasks to complete
|
||||
time.sleep(1)
|
||||
continue
|
||||
print("No more executable tickets. Track is blocked or finished.")
|
||||
await self._push_state(status="blocked", active_tier=None)
|
||||
self._push_state(status="blocked", active_tier=None)
|
||||
break
|
||||
# 3. Process ready tasks
|
||||
to_run = [t for t in ready_tasks if t.status == "in_progress" or (not t.step_mode and self.engine.auto_queue)]
|
||||
@@ -106,15 +106,15 @@ class ConductorEngine:
|
||||
for ticket in ready_tasks:
|
||||
if ticket not in to_run and ticket.status == "todo":
|
||||
print(f"Ticket {ticket.id} is ready and awaiting approval.")
|
||||
await self._push_state(active_tier=f"Awaiting Approval: {ticket.id}")
|
||||
await asyncio.sleep(1)
|
||||
self._push_state(active_tier=f"Awaiting Approval: {ticket.id}")
|
||||
time.sleep(1)
|
||||
|
||||
if to_run:
|
||||
tasks = []
|
||||
threads = []
|
||||
for ticket in to_run:
|
||||
ticket.status = "in_progress"
|
||||
print(f"Executing ticket {ticket.id}: {ticket.description}")
|
||||
await self._push_state(active_tier=f"Tier 3 (Worker): {ticket.id}")
|
||||
self._push_state(active_tier=f"Tier 3 (Worker): {ticket.id}")
|
||||
|
||||
# Escalation logic based on retry_count
|
||||
models = ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-3.1-pro-preview"]
|
||||
@@ -127,19 +127,17 @@ class ConductorEngine:
|
||||
messages=[]
|
||||
)
|
||||
context_files = ticket.context_requirements if ticket.context_requirements else None
|
||||
tasks.append(loop.run_in_executor(
|
||||
None,
|
||||
run_worker_lifecycle,
|
||||
ticket,
|
||||
context,
|
||||
context_files,
|
||||
self.event_queue,
|
||||
self,
|
||||
md_content,
|
||||
loop
|
||||
))
|
||||
|
||||
t = threading.Thread(
|
||||
target=run_worker_lifecycle,
|
||||
args=(ticket, context, context_files, self.event_queue, self, md_content),
|
||||
daemon=True
|
||||
)
|
||||
threads.append(t)
|
||||
t.start()
|
||||
|
||||
await asyncio.gather(*tasks)
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
# 4. Retry and escalation logic
|
||||
for ticket in to_run:
|
||||
@@ -149,13 +147,13 @@ class ConductorEngine:
|
||||
ticket.status = 'todo'
|
||||
print(f"Ticket {ticket.id} BLOCKED. Escalating to {models[min(ticket.retry_count, len(models)-1)]} and retrying...")
|
||||
|
||||
await self._push_state(active_tier="Tier 2 (Tech Lead)")
|
||||
self._push_state(active_tier="Tier 2 (Tech Lead)")
|
||||
|
||||
def _queue_put(event_queue: events.AsyncEventQueue, loop: asyncio.AbstractEventLoop, event_name: str, payload) -> None:
|
||||
"""Thread-safe helper to push an event to the AsyncEventQueue from a worker thread."""
|
||||
asyncio.run_coroutine_threadsafe(event_queue.put(event_name, payload), loop)
|
||||
def _queue_put(event_queue: events.SyncEventQueue, event_name: str, payload) -> None:
|
||||
"""Thread-safe helper to push an event to the SyncEventQueue from a worker thread."""
|
||||
event_queue.put(event_name, payload)
|
||||
|
||||
def confirm_execution(payload: str, event_queue: events.AsyncEventQueue, ticket_id: str, loop: asyncio.AbstractEventLoop = None) -> bool:
|
||||
def confirm_execution(payload: str, event_queue: events.SyncEventQueue, ticket_id: str) -> bool:
|
||||
"""
|
||||
Pushes an approval request to the GUI and waits for response.
|
||||
"""
|
||||
@@ -166,11 +164,9 @@ def confirm_execution(payload: str, event_queue: events.AsyncEventQueue, ticket_
|
||||
"payload": payload,
|
||||
"dialog_container": dialog_container
|
||||
}
|
||||
if loop:
|
||||
_queue_put(event_queue, loop, "mma_step_approval", task)
|
||||
else:
|
||||
raise RuntimeError("loop is required for thread-safe event queue access")
|
||||
# Wait for the GUI to create the dialog and for the user to respond
|
||||
_queue_put(event_queue, "mma_step_approval", task)
|
||||
|
||||
# Wait for the GUI to create the dialog and for the user to respond
|
||||
start = time.time()
|
||||
while dialog_container[0] is None and time.time() - start < 60:
|
||||
time.sleep(0.1)
|
||||
@@ -179,7 +175,7 @@ def confirm_execution(payload: str, event_queue: events.AsyncEventQueue, ticket_
|
||||
return approved
|
||||
return False
|
||||
|
||||
def confirm_spawn(role: str, prompt: str, context_md: str, event_queue: events.AsyncEventQueue, ticket_id: str, loop: asyncio.AbstractEventLoop = None) -> Tuple[bool, str, str]:
|
||||
def confirm_spawn(role: str, prompt: str, context_md: str, event_queue: events.SyncEventQueue, ticket_id: str) -> Tuple[bool, str, str]:
|
||||
"""
|
||||
Pushes a spawn approval request to the GUI and waits for response.
|
||||
Returns (approved, modified_prompt, modified_context)
|
||||
@@ -193,11 +189,9 @@ def confirm_spawn(role: str, prompt: str, context_md: str, event_queue: events.A
|
||||
"context_md": context_md,
|
||||
"dialog_container": dialog_container
|
||||
}
|
||||
if loop:
|
||||
_queue_put(event_queue, loop, "mma_spawn_approval", task)
|
||||
else:
|
||||
raise RuntimeError("loop is required for thread-safe event queue access")
|
||||
# Wait for the GUI to create the dialog and for the user to respond
|
||||
_queue_put(event_queue, "mma_spawn_approval", task)
|
||||
|
||||
# Wait for the GUI to create the dialog and for the user to respond
|
||||
start = time.time()
|
||||
while dialog_container[0] is None and time.time() - start < 60:
|
||||
time.sleep(0.1)
|
||||
@@ -220,7 +214,7 @@ def confirm_spawn(role: str, prompt: str, context_md: str, event_queue: events.A
|
||||
return approved, modified_prompt, modified_context
|
||||
return False, prompt, context_md
|
||||
|
||||
def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files: List[str] | None = None, event_queue: events.AsyncEventQueue | None = None, engine: Optional['ConductorEngine'] = None, md_content: str = "", loop: asyncio.AbstractEventLoop = None) -> None:
|
||||
def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files: List[str] | None = None, event_queue: events.SyncEventQueue | None = None, engine: Optional['ConductorEngine'] = None, md_content: str = "") -> None:
|
||||
"""
|
||||
Simulates the lifecycle of a single agent working on a ticket.
|
||||
Calls the AI client and updates the ticket status based on the response.
|
||||
@@ -231,7 +225,6 @@ def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files:
|
||||
event_queue: Queue for pushing state updates and receiving approvals.
|
||||
engine: The conductor engine.
|
||||
md_content: The markdown context (history + files) for AI workers.
|
||||
loop: The main asyncio event loop (required for thread-safe queue access).
|
||||
"""
|
||||
# Enforce Context Amnesia: each ticket starts with a clean slate.
|
||||
ai_client.reset_session()
|
||||
@@ -270,8 +263,7 @@ def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files:
|
||||
prompt=user_message,
|
||||
context_md=md_content,
|
||||
event_queue=event_queue,
|
||||
ticket_id=ticket.id,
|
||||
loop=loop
|
||||
ticket_id=ticket.id
|
||||
)
|
||||
if not approved:
|
||||
ticket.mark_blocked("Spawn rejected by user.")
|
||||
@@ -283,15 +275,15 @@ def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files:
|
||||
def clutch_callback(payload: str) -> bool:
|
||||
if not event_queue:
|
||||
return True
|
||||
return confirm_execution(payload, event_queue, ticket.id, loop=loop)
|
||||
return confirm_execution(payload, event_queue, ticket.id)
|
||||
|
||||
def stream_callback(chunk: str) -> None:
|
||||
if event_queue and loop:
|
||||
_queue_put(event_queue, loop, 'mma_stream', {'stream_id': f'Tier 3 (Worker): {ticket.id}', 'text': chunk})
|
||||
if event_queue:
|
||||
_queue_put(event_queue, 'mma_stream', {'stream_id': f'Tier 3 (Worker): {ticket.id}', 'text': chunk})
|
||||
|
||||
old_comms_cb = ai_client.comms_log_callback
|
||||
def worker_comms_callback(entry: dict) -> None:
|
||||
if event_queue and loop:
|
||||
if event_queue:
|
||||
kind = entry.get("kind")
|
||||
payload = entry.get("payload", {})
|
||||
chunk = ""
|
||||
@@ -303,7 +295,7 @@ def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files:
|
||||
chunk = f"\n[TOOL RESULT]\n{res}\n"
|
||||
|
||||
if chunk:
|
||||
_queue_put(event_queue, loop, "response", {"text": chunk, "stream_id": f"Tier 3 (Worker): {ticket.id}", "status": "streaming..."})
|
||||
_queue_put(event_queue, "response", {"text": chunk, "stream_id": f"Tier 3 (Worker): {ticket.id}", "status": "streaming..."})
|
||||
if old_comms_cb:
|
||||
old_comms_cb(entry)
|
||||
|
||||
@@ -331,11 +323,8 @@ def run_worker_lifecycle(ticket: Ticket, context: WorkerContext, context_files:
|
||||
"stream_id": f"Tier 3 (Worker): {ticket.id}",
|
||||
"status": "done"
|
||||
}
|
||||
print(f"[MMA] Pushing Tier 3 response for {ticket.id}, loop={'present' if loop else 'NONE'}, stream_id={response_payload['stream_id']}")
|
||||
if loop:
|
||||
_queue_put(event_queue, loop, "response", response_payload)
|
||||
else:
|
||||
raise RuntimeError("loop is required for thread-safe event queue access")
|
||||
print(f"[MMA] Pushing Tier 3 response for {ticket.id}, stream_id={response_payload['stream_id']}")
|
||||
_queue_put(event_queue, "response", response_payload)
|
||||
except Exception as e:
|
||||
print(f"[MMA] ERROR pushing response to UI: {e}\n{traceback.format_exc()}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user