---
engine: devin
cwd: /Users/sd9235/code/mygh/learning_ai_devops_tools
yolo: true
lock: devops-tools-demo
timeout: 4h
---

ROLE: Senior engineer. Build the PHASE 2 TWO-FACTORY PARALLEL DEMO — the final
Phase-2 EXIT-CRITERIA box (§14): >=2 factories executing jobs in parallel via the
coordinator, proving conflict-free atomic claims, lease fencing, and reaper-reclaim
end-to-end. This is a DEMO HARNESS + DOCS, not new runtime behavior — agent-queue.sh
and lib/fleet-client.sh already implement everything; you orchestrate + observe them.

PARALLEL-SAFETY: One other Devin is running in a DIFFERENT repo (learning_ai_common_plat,
the tracker-wiring slice) — no overlap. In THIS repo you OWN a NEW demo directory and the
additive selftest/docs only:
- You OWN (create/edit): agent-queue/demo/two-factory-demo.sh (NEW),
  agent-queue/demo/README.md (NEW), additive checks in agent-queue/selftest.sh,
  and the §14 Phase-2 demo/exit-criteria ticks in agent-queue/docs/GIGAFACTORY/GIGAFACTORY_ROADMAP.md.
- You MUST NOT change the behavior of agent-queue.sh or lib/fleet-client.sh. You may READ
  them and CALL them; if a tiny additive hook is unavoidable, keep it flag-gated and prove
  all 68 existing selftest checks still pass byte-for-byte.
- Leave the runtime agent-queue/queue/* working-tree artifacts ALONE (live-daemon state,
  not yours) — never stage or commit them.

READ FIRST:
- agent-queue/agent-queue.sh — the run loop, AQ_FLEET / AQ_FLEET_ROUTE flags, claim path,
  fencing/quarantine, offline-degrade.
- agent-queue/lib/fleet-client.sh — fleet_register/heartbeat, claim, lease renew, fenced
  PATCH, the coordinator HTTP wrappers and their env (AQ_FLEET_API, AQ_FLEET_TOKEN, factory id).
- agent-queue/selftest.sh — how the EXISTING fleet tests STUB the coordinator (the canned
  responder pattern). Reuse that exact stub style so the demo's selftest needs NO live service.
- ../learning_ai_common_plat/services/platform-service/src/modules/fleet/coordinator.ts —
  the claim/lease/fence/reaper contract you are demonstrating (read-only; do not edit).
- agent-queue/docs/GIGAFACTORY/GIGAFACTORY_ROADMAP.md §14 Phase-2 "Two-factory demo" + "Exit criteria".

DELIVERABLES

1. agent-queue/demo/two-factory-demo.sh — an orchestration script that:
   - Starts >=2 factories (distinct factoryIds, e.g. mac-1 + ubuntu-1) against ONE
     coordinator with AQ_FLEET=1 AQ_FLEET_ROUTE=1, each in its own working dir/queue so
     they do not share local inbox state — they compete ONLY through the coordinator.
   - Submits 3 jobs and lets the two factories drain them in parallel.
   - DEMONSTRATES + ASSERTS the Phase-2 exit guarantees:
       (a) no double-assign: each job is claimed/executed by exactly ONE factory.
       (b) fencing: kill a factory MID-JOB -> the reaper returns the job -> the OTHER
           factory reclaims and completes it AND the dead worker's late/zombie report is
           FENCED (rejected, never shipped).
       (c) parallelism: both factories make progress concurrently (not serialized).
   - Prints a clear PASS/FAIL summary (per-job winner, reclaim event, fence event).
   - DUAL MODE: works against a real coordinator when AQ_FLEET_API/AQ_FLEET_TOKEN are set;
     otherwise drives the SAME selftest coordinator STUB so the demo is runnable + CI-safe
     with zero external deps. Document both invocations.
   - bash, mac+linux safe, curl-only, no new runtime deps; style consistent with the repo.

2. agent-queue/demo/README.md — how to run the demo (stub mode + real-coordinator mode),
   the env vars, what each asserted guarantee proves, and a short "what to watch" guide
   (the kanban/log lines that show the reclaim + fence).

3. selftest.sh — ADD a small number of checks (do NOT modify the existing 68) that run the
   demo in STUB mode headlessly and assert: 3 jobs all reach a terminal state across the 2
   factories with no double-assignment; the kill -> reclaim -> fenced-zombie path fires;
   exit 0. Keep them fast + deterministic (seeded, no real sleeps where avoidable).

TESTS / VERIFY GATE:
- bash agent-queue/selftest.sh  -> all prior 68 + the new demo checks green, exit 0.
- bash -n agent-queue/demo/two-factory-demo.sh && bash -n agent-queue/agent-queue.sh
  && bash -n agent-queue/lib/fleet-client.sh  -> OK.
- shellcheck --severity=error on the new script + the two core scripts -> clean.
- node --check agent-queue/dashboard.mjs -> OK (must remain unchanged).

CONSTRAINTS: do NOT alter agent-queue.sh / fleet-client.sh runtime behavior; reuse the
existing coordinator stub pattern; never commit queue/* runtime artifacts; mac+linux safe;
no emojis; conventional commits (feat(agent-queue): ...); tests sacred (the 68 stay green).

DOCS: tick the §14 Phase-2 "Two-factory demo" box and, once the demo asserts all three
guarantees, the Phase-2 "Exit criteria" line in GIGAFACTORY_ROADMAP.md — set §0 Phase 2 ->
complete (or note the exact remaining %). This is the box that closes Phase 2.

FINAL OUTPUT — report in EXACTLY this format:
## Implementation Report — Phase 2 Two-Factory Parallel Demo (Exit Criteria)
### Branch & commits / PR
- branch / based-on: feat/gigafactory-p2-two-factory-demo off current main
### Files changed
### What was implemented (orchestration, the 3 asserted guarantees, stub vs real mode)
### Tests added (+ selftest PASS/FAIL summary: prior 68 + new)
### Verify gate results (selftest / bash -n / shellcheck / node --check)
### Deviations / assumptions (how factories are isolated, how kill/reclaim is simulated in stub)
### Phase 2 status (which §14 boxes now complete; exit criteria met Y/N; what (if anything) remains)
### Suggested next slice (Phase 3 — tracker-web fleet control plane + DAG + budgets)
