mirror of
https://github.com/Heretek-AI/agency-control-plane.git
synced 2026-11-07 10:46:08 -10:00
17 KiB
17 KiB
MASTER COORDINATOR BUILD PROMPT: REPO 4 — agency-control-plane
Target Repository: /home/john/Projects/Heretek-AI-Rebuild/agency-control-plane
GitHub Remote: https://github.com/Heretek-AI/agency-control-plane
Package Name: @heretek-ai/agency-control-plane
Primary Session Agent: agency-coordinator (opencode-go/muse-spark-1.3-contributor)
Required Triad Subagents: agency-programmer, agency-qa, agency-drift-guard
0. CRITICAL MUSE 1.3 HARNESS SUPPLEMENT (READ FIRST — NON-NEGOTIABLE)
You are running on opencode-go/muse-spark-1.3-contributor. Obey these six rules on every turn:
- ZERO PROMPT SUMMARIZATION WHEN CALLING SUBAGENTS:
- In Wave 0, write this entire specification into
docs/prompts/SPEC.mdandAGENTS.md. Every subagent call MUST instruct the subagent toreadAGENTS.mdanddocs/prompts/SPEC.mddirectly from disk.
- In Wave 0, write this entire specification into
- EXPLICIT SUBAGENT ROLES:
- Always pass
"agent": "agency-programmer","agent": "agency-qa", or"agent": "agency-drift-guard"when callingsubagent.
- Always pass
- VANILLA
read/edit/writeHYGIENE:- Use native
read,write,edit,glob, andgrep—nevercatorsed. Never includereadline numbers (12:) ineditoldString/newString. Fix<diagnostics>immediately.
- Use native
- STRICT SCOPE & REFUSAL BOUNDARY FOR
agency-control-plane:- This repository is ONLY the Fleet Dispatcher Daemon (
dispatcher/) and the unified High-Fidelity WebUI (web/) featuring your 5 locked-in UX pillars:- Visual Kanban Board mapped to isolated Jujutsu (
jj) / Git worktrees, - Persistent Top-Level Director Chat Sidebar to plan and steer missions,
- Interactive DAG & Worker Drill-Down Inspector (live SSE tool stream, stage gates, token/cost telemetry),
- Side-by-Side / Unified Git Diff Viewer with file tree navigation, and
- 1-Click / Passphrase Cryptographic Ed25519 Human Approval Drawer (enforcing Invariant I1: passphrase kept strictly in local closure, never in reactive state stores).
- Visual Kanban Board mapped to isolated Jujutsu (
- FORBIDDEN IN THIS REPO: Do NOT copy
SwarmOS(src/omni,swarmos-plugin.ts) into this repo! Do NOT implement AST parsers or domain MCP tools here.
- This repository is ONLY the Fleet Dispatcher Daemon (
- CRITICAL BUG FIXES ENFORCED IN THIS REPO (FROM REPORTS
01b,04a,04b):- Fix 1 (
limits.ts429 Global Freeze Bug): Never pause all missions for 60s in an infinite loop on HTTP 429 (gousagelimiterror). Parseretry-after-ms/Retry-After(clamped1000ms..300000ms) and immediately return{ action: "route-fallback", targetTier: "tier-2-commercial" | "tier-3-local" }via Maxim Bifrost! - Fix 2 (OpenCode v2 SQLite Single-Writer & SSE Overflow): Set
PRAGMA busy_timeout = 15000; PRAGMA journal_mode = WAL;on the dispatcher SQLite store, isolate each worker's.opencodestate per worktree, and multiplex worker events over a ring-buffered SSE / NATS JetStream bus.
- Fix 1 (
- BINDING TRIAD GATE:
- Every wave requires
agency-qaRed-Proof Sabotage (docs/evidence/wave-<N>/red-proofs.log) andagency-drift-guardVERDICT: PASS(docs/reports/wave-<N>-verification.md) before merging tomain.
- Every wave requires
1. TARGET REPOSITORY ARCHITECTURE & FILE TREE
/home/john/Projects/Heretek-AI-Rebuild/agency-control-plane/
├── AGENTS.md
├── Makefile
├── package.json # Bun workspace root: ["dispatcher", "web"]
├── tsconfig.json
├── docs/
│ ├── prompts/
│ │ ├── SPEC.md
│ │ └── verify-stage.md
│ ├── reports/
│ └── evidence/
├── scripts/
│ └── accept/
│ ├── wave-1.sh
│ ├── wave-2.sh
│ └── wave-3.sh
├── dispatcher/ # Wave 1 & 2: Fleet Dispatcher Daemon & HTTP/SSE Control API
│ ├── package.json # @heretek-ai/agency-dispatcher
│ ├── src/
│ │ ├── model.ts # TaskStatuses, Task, Mission, ApprovalTicket schemas
│ │ ├── wildcard.ts # Glob path intersection (`mayOverlap(pathA, pathB)`)
│ │ ├── scheduler.ts # Pure 186-line DAG scheduler (`all_success`, `one_success`, `pathsOverlap` lock, cascade cancel)
│ │ ├── limits.ts # Upgraded 3-tier failover & Retry-After classifier (Report 01b §9.1)
│ │ ├── audit.ts # O_APPEND + fsync SHA-256 hash-chained audit ledger with torn-tail check
│ │ ├── worktrees.ts # 2-phase crash-safe worktree coordinator supporting Jujutsu (`jj workspace add`) + git worktree fallback + `refs/fleet-salvage/<id>`
│ │ ├── landing.ts # Transactional landing in throwaway worktree (pre-verify -> merge -> post-verify -> rollback on conflict/failure)
│ │ ├── event-bus.ts # Ring-buffered SSE + NATS JetStream / KV Blackboard bridge
│ │ ├── approval-tickets.ts # Single-use preview tickets bound to subjectHash + loopback Ed25519 verification
│ │ ├── server.ts # Bun HTTP + SSE API serving Kanban state, Director chat, Worker drill-down, Diffs, and Approvals
│ │ └── index.ts
│ └── test/
│ ├── scheduler.test.ts # Tests all_success, one_success, cascade cancel, and pathsOverlap serialization
│ ├── limits.test.ts # Proves 429 gousagelimiterror triggers route-fallback instead of global freeze!
│ ├── worktrees-landing.test.ts # Tests 2-phase allocation, salvage ref anchoring, and transactional landing rollback
│ └── server-api.test.ts # Tests REST + SSE endpoints, approval tickets, and CSRF/loopback guards
└── web/ # Wave 3: Unified High-Fidelity WebUI (5 UX Pillars)
├── package.json # @heretek-ai/agency-web
├── index.html
├── src/
│ ├── state/
│ │ └── fleet-store.ts # Typed state store for Kanban columns, DAG nodes, active drill-down worker, and diffs
│ ├── security/
│ │ └── approval-client.ts # Ported from iumbtems approvals.ts (Invariant I1: passphrase held only in closure, never stored in state)
│ ├── components/
│ │ ├── KanbanBoard.tsx # Pillar 1: 5-column Worktree Kanban (Backlog, Ready, In Progress, Waiting Approval/QA, Done)
│ │ ├── DirectorChat.tsx # Pillar 2: Persistent Top-Level Director Chat sidebar with mission decomposition cards
│ │ ├── WorkerDrillDown.tsx # Pillar 3: Live worker subagent inspector (SSE tool stream, stage gates, token/tier badge, DAG view)
│ │ ├── SplitDiffViewer.tsx # Pillar 4: Side-by-side & unified Git diff viewer with file tree & line stats
│ │ └── ApprovalDrawer.tsx # Pillar 5: 1-click / passphrase Ed25519 approval & rejection gate
│ └── app.tsx # High-craft dark-mode layout satisfying WCAG 2.2 AA contrast & 24x24px target sizes
└── test/
├── approval-invariant.test.ts # Asserts passphrase never enters fleet-store or DOM attributes (Invariant I1)
└── ui-components.test.ts # Tests rendering and state transitions across all 5 UX pillars
2. EXACT MODULE SPECIFICATIONS (WRITE TO docs/prompts/SPEC.md IN WAVE 0)
2.1 Wave 1 Specification: Pure Scheduler, Path-Overlap Arbiter, 3-Tier Failover Limits & Audit Chain
dispatcher/src/wildcard.ts&dispatcher/src/scheduler.ts:- Implement the exact salvaged
mayOverlap(patternA: string, patternB: string): booleanandschedule(tasks: Task[], runningIds: Iterable<string>, caps: ScheduleCaps): ScheduleResultfrom/home/john/Projects/The-Agency/dispatcher/src/scheduler.ts:- Under
all_success, a task is ready when alldepsaredone; if any dep isfailedorcancelled, it cascades tocancelledvia fixpoint closure. - Under
one_success, a task is ready when at least one dep isdone; it cancels only when all deps are terminal with zerodone. - Two
expert-runtasks whoseinput.pathsoverlap (mayOverlap(pa, pb) === true) NEVER start simultaneously (blockedByOverlap/collidesSelected). waitingHumantasks never start and never force downstream cancellation.
- Under
- Implement the exact salvaged
dispatcher/src/limits.ts(Exact Patch from Report01b§9.1):extractRetryAfterMs(headers?: Record<string, string>, defaultMs?: number): number— Parsesretry-after-ms(ms) andretry-after(seconds or HTTP-Date), clamped strictly to[1000, 300000].classifyProviderFailure(failure: ProviderFailure, consecutiveFailures: number, currentTier: "tier-1-opencode" | "tier-2-commercial" | "tier-3-local"): ProviderDisposition:- When
failure.status === 402or code matchesgousagelimiterror/freeusagelimiterror/insufficient_quotaor 429 body matches/usage limit|quota exceeded|insufficient quota|credit balance/i:- From
tier-1-opencode\rightarrowreturns{ action: "route-fallback", targetTier: "tier-2-commercial" }(ZERO mission pause!). - From
tier-2-commercial\rightarrowreturns{ action: "route-fallback", targetTier: "tier-3-local" }. - From
tier-3-local\rightarrowreturns{ action: "pause-missions", reason: "...", resumeAfterMs }.
- From
- When
failure.status === 429(transient rate spike without quota exhaustion)\rightarrowreturns{ action: "retry-delay", delayMs: extractRetryAfterMs(failure.headers) }.
- When
dispatcher/src/audit.ts:- Hash-chained JSONL audit log (
seq,timestamp,event,data,prevHash,hash). Redacts secrets (sk-*,Bearer *,OPENCODE_API_KEY) before hashing. Detects any byte modification or equal-length rewrite viacheckAnchor.
- Hash-chained JSONL audit log (
2.2 Wave 2 Specification: Crash-Safe Worktrees (jj / git), Transactional Landing & HTTP/SSE Control Server
dispatcher/src/worktrees.ts&dispatcher/src/landing.ts:- Two-phase crash-safe worktree allocation (
allocatingrecorded in SQLite before worktree creation, flipped toallocatedafter;cleanupUnconfirmedcleans orphans). - Supports Jujutsu colocated workspaces (
jj workspace add) when.jjis present, with seamless fallback togit worktree add -b fleet/<taskId>. salvageWorktreeanchorsrefs/fleet-salvage/<taskId>before deleting abandoned task branches so agent work is never lost.landTaskruns pre-checks in the task worktree, performs--no-ffmerge ontofleet/integration/<missionId>inside a throwaway landing worktree, runs post-merge verification, and atomically rolls back to pre-merge SHA if verification or merge fails.
- Two-phase crash-safe worktree allocation (
dispatcher/src/server.ts&dispatcher/src/approval-tickets.ts:- Serves HTTP API +
/api/eventsSSE stream on loopback (127.0.0.1):GET /api/board— Returns Kanban columns, tasks, worktree statuses, active model tier (tier-1-opencode/tier-2-commercial/tier-3-local), and DAG edges.POST /api/director/message— Appends steering message to Top-Level Director session and decomposes new task cards onto the board.GET /api/tasks/:id/drilldown— Returns worker session metadata, stage gates, live tool log buffer, and git diff hunks ([{ file, status, additions, deletions, patch }]).POST /api/approvals/:taskId/ticket— Mints a single-use, 60-second TTL preview ticket bound to{ taskId, subjectHash }.POST /api/approvals/:taskId/sign— Verifies the single-use ticket, verifies the Ed25519 signature / passphrase in memory, transitions task fromwaitingHumantoready/done, and appends toaudit.ts.
- Serves HTTP API +
2.3 Wave 3 Specification: Unified High-Fidelity WebUI (web/src/*)
web/src/security/approval-client.ts(Salvaged fromiumbtems/packages/web/src/approvals.ts):- Strict Security Invariant I1: The human passphrase entered in the approval dialog is held ONLY in a lexical function closure variable during the
fetch("/api/approvals/:id/sign")call, is zeroed out immediately after, and is never written tofleet-store,localStorage,sessionStorage, or DOM data attributes.
- Strict Security Invariant I1: The human passphrase entered in the approval dialog is held ONLY in a lexical function closure variable during the
- Five High-Fidelity UX Components (
web/src/components/*):KanbanBoard.tsx: Renders the 5 Kanban columns (Backlog,Ready,In Progress,Waiting Approval / QA,Done) with worktree branch badges (fleet/task-101), owned-path chips, and model-tier indicators.DirectorChat.tsx: Persistent right/left dockable chat with the Top-Level Director Agent (director), supporting/ship <spec>, mission charter preview, and 1-click task DAG injection onto the Kanban board.WorkerDrillDown.tsx: Clicking any Kanban card opens the drill-down inspector showing the worker's live SSE tool-call timeline, active Docker Compose profile badge (core/appsec/android/game/research), and DAG dependency graph.SplitDiffViewer.tsx: Renders side-by-side and unified Git diffs for the selected worktree with file-tree navigation, addition/deletion counters, and syntax-highlighted hunk lines.ApprovalDrawer.tsx: Displays the exactsubjectHash, diff summary, QA verdicts (qa-functional+qa-adversarial), and the 1-click / passphrase Ed25519 approval gate.
3. WAVE-BY-WAVE TRIAD EXECUTION PLAN FOR agency-coordinator
Step 0 (Coordinator Bootstrap on main)
- Write
AGENTS.md,docs/prompts/SPEC.md,docs/prompts/verify-stage.md,Makefile, rootpackage.json, andtsconfig.json. - Commit baseline scaffolding to
main.
Wave 1: Pure Scheduler, Path-Overlap Arbiter, 3-Tier Failover Limits & Audit Chain (dispatcher/src/{model,wildcard,scheduler,limits,audit}.ts)
- Create worktree:
git worktree add -b wave-1-scheduler-limits worktrees/wave-1 main - Dispatch Programmer (
agency-programmer):
{
"agent": "agency-programmer",
"description": "Wave 1 Programmer: Implement scheduler, wildcard overlap, 3-tier limits, and audit log",
"prompt": "You are the Programmer Agent for Wave 1 in `/home/john/Projects/Heretek-AI-Rebuild/agency-control-plane/worktrees/wave-1` on branch `wave-1-scheduler-limits`.\n\nFIRST: Read `AGENTS.md` and `docs/prompts/SPEC.md` in full.\nSECOND: Implement in `dispatcher/src/`:\n1. `model.ts` and `wildcard.ts` (`mayOverlap`).\n2. `scheduler.ts` (`schedule`, `nextStatus` with `all_success`, `one_success`, fixpoint cancellation closure, and `pathsOverlap` serialization).\n3. `limits.ts` (`extractRetryAfterMs` clamped 1s..300s and `classifyProviderFailure` with 3-tier failover: `tier-1-opencode` -> `tier-2-commercial` -> `tier-3-local` on 429 quota errors).\n4. `audit.ts` (hash-chained JSONL ledger with secret redaction and equal-length rewrite detection).\n5. Write unit tests in `dispatcher/test/scheduler.test.ts` and `dispatcher/test/limits.test.ts`, acceptance script `scripts/accept/wave-1.sh`, and report `docs/reports/wave-1.md`.\n6. Run `bun test` and `bun run check` until green, then commit."
}
- Dispatch QA (
agency-qa) to runmake check,bash scripts/accept/wave-1.sh, and perform Red-Proof Sabotage (revertlimits.tsso 429 returnspause-missionsontier-1-opencodeand confirmlimits.test.tsfails; breakpathsOverlapinscheduler.tsand confirmscheduler.test.tsfails; restore cleanly withgit checkout --). Record indocs/evidence/wave-1/red-proofs.log. - Dispatch Drift Guard (
agency-drift-guard) to audit Wave 1 and issueVERDICT: PASSindocs/reports/wave-1-verification.md. - Merge
wave-1-scheduler-limitsintomainand remove worktree.
Wave 2: Crash-Safe Worktrees, Transactional Landing & HTTP/SSE Control Server (dispatcher/src/{worktrees,landing,event-bus,approval-tickets,server}.ts)
- Create worktree:
git worktree add -b wave-2-worktrees-server worktrees/wave-2 main - Dispatch Programmer (
agency-programmer) to implementworktrees.ts,landing.ts,event-bus.ts,approval-tickets.ts,server.ts,dispatcher/test/worktrees-landing.test.ts,dispatcher/test/server-api.test.ts,scripts/accept/wave-2.sh, anddocs/reports/wave-2.md. - Dispatch QA (
agency-qa) and Drift Guard (agency-drift-guard) to verify and red-proof Wave 2 (VERDICT: PASS). - Merge
wave-2-worktrees-serverintomainand remove worktree.
Wave 3: Unified High-Fidelity WebUI (web/src/* — Kanban, Director Chat, Worker Drill-Down, Split Diff & Ed25519 Approval Drawer)
- Create worktree:
git worktree add -b wave-3-webui worktrees/wave-3 main - Dispatch Programmer (
agency-programmer) to implementweb/src/state/fleet-store.ts,web/src/security/approval-client.ts,KanbanBoard.tsx,DirectorChat.tsx,WorkerDrillDown.tsx,SplitDiffViewer.tsx,ApprovalDrawer.tsx,app.tsx,web/test/approval-invariant.test.ts,web/test/ui-components.test.ts,scripts/accept/wave-3.sh,README.md, anddocs/reports/wave-3.md. - Dispatch QA (
agency-qa) and Drift Guard (agency-drift-guard) to verify and red-proof Wave 3 (VERDICT: PASS). - Merge
wave-3-webuiintomain, remove worktree, and verifymake check && make acceptexits 0 onmain.