""" The Conductor — KanBot's scheduler. It does exactly one thing: LISTEN on the 'kb_events' Postgres channel, and for every column transition, dispatch the right agent to the card. The board is the scheduler; the Conductor is just the hand that turns board events into agent runs. Dispatch policy (which role reacts to which arrival): -> backlog : PLANNER decomposes (only if role==planner; leaf cards wait) -> ready : an EXECUTOR claims it (fast head picks up) -> review : a CRITIC evaluates it -> blocked : relay to Telegram if requires_human -> done : if it has a parent, nudge the planner to check completion Concurrency is bounded by a worker semaphore so the M4 isn't stampeded. Run: python -m core.conductor """ from __future__ import annotations import asyncio import json import os import asyncpg from . import db, workers from .contract import Column, Role from .telegram import relay_human_request MAX_WORKERS = int(os.environ.get("KANBOT_MAX_WORKERS", "3")) _sem = asyncio.Semaphore(MAX_WORKERS) async def _handle_event(payload: dict) -> None: card_id = payload["card_id"] to = Column(payload["to"]) role = Role(payload["role"]) card = await db.get_card(card_id) if not card: return async with _sem: # bound concurrent agent runs if to == Column.BACKLOG and role == Role.PLANNER: await workers.run_planner(card) elif to == Column.BACKLOG: # a leaf executor card was created; auto-promote to ready await workers.promote_to_ready(card) elif to == Column.READY: await workers.claim_and_execute(card) elif to == Column.REVIEW: await workers.run_critic(card) elif to == Column.BLOCKED: if card.requires_human: await relay_human_request(card) elif to == Column.DONE and card.parent: parent = await db.get_card(card.parent) if parent: await workers.check_parent_completion(parent) async def _drain_startup_backlog() -> None: """On boot, sweep BACKLOG cards left from a prior run so nothing is stranded (NOTIFY only fires on live changes, not existing rows). Only promotes backlog → ready. READY/REVIEW cards are skipped: the reaper reclaims stale leases and live NOTIFY events handle the rest. Processing them here would race the semaphore and starve live events. """ for card in await db.list_column(Column.BACKLOG): try: await workers.promote_to_ready(card) except Exception as exc: print(f"[conductor] drain skipped {card.id}: {exc}", flush=True) async def main() -> None: dsn = os.environ.get("DATABASE_URL", "postgresql://kanbot:kanbot@localhost:5432/kanbot") conn = await asyncpg.connect(dsn) queue: asyncio.Queue = asyncio.Queue() def _on_notify(_c, _pid, _chan, payload): queue.put_nowait(json.loads(payload)) await conn.add_listener("kb_events", _on_notify) print(f"[conductor] listening on kb_events | max_workers={MAX_WORKERS} " f"| dry_run={os.environ.get('KANBOT_DRY_RUN','1')}") # Run startup drain as a background task so the NOTIFY listener loop # starts immediately. Without this, a long drain blocks every live # transition (approve, reaper, critic) until it finishes. asyncio.create_task(_drain_startup_backlog()) try: while True: payload = await queue.get() asyncio.create_task(_guarded(payload)) finally: await conn.remove_listener("kb_events", _on_notify) await conn.close() async def _guarded(payload: dict) -> None: try: await _handle_event(payload) except Exception as e: # a single bad card must never kill the Conductor print(f"[conductor] error handling {payload.get('card_id')}: {e!r}") if __name__ == "__main__": asyncio.run(main())