Files
cleveragents-core/tools/controller/worker
drew 23a014e402 feat(controller): Phase 1c-3 — production agent_runner (real MCP subprocess)
The production seam that connects the controller's runner to actual
LLM execution. Per-attempt MCP subprocess spawn + OpenCode session
drive + canonical JSON read + strict-parse → output dict.

tools/controller/worker/agent_runner.py:

- production_agent_runner(): the function tested production wires
  into run_one_attempt's agent_runner param. Per-attempt lifecycle:
  1. Map role → matching MCP module + V1 output contract.
  2. mkstemp a canonical-output path; spawn `python -m {mcp_module}`
     subprocess with CONTROLLER_CANONICAL_OUTPUT_PATH set in env.
  3. Call injected run_opencode_session (tests use JSON-RPC stdin
     driver; production wires _opencode_worker.run_session_blocking).
  4. Wait for the MCP's finalize_and_emit to write the canonical
     JSON to the tempfile.
  5. Strict-parse against the role's V1 output model.
  6. SIGTERM/grace/SIGKILL the MCP + unlink the tempfile in finally.

- ROLE_TO_MCP_MODULE / ROLE_TO_OUTPUT_MODEL: explicit per-role
  dispatch tables. Covers all 5 worker roles.

- Error classification per the WorkerError outcome enum:
  - unknown role → ValueError (master bug; not a worker outcome)
  - session raises generic Exception → WorkerError(worker-internal-error)
  - session raises WorkerLostLock → propagated unchanged
  - lost_lock_check returns True after session → WorkerLostLock
  - MCP didn't emit canonical output within timeout → WorkerError(
    worker-internal-error)
  - MCP emitted JSON that fails strict-parse → WorkerError(
    contract-violation; per v9 status='failed' policy maps to
    workflow STUCK)

tools/controller/mcp/_builder_base.py:

- finalize_and_emit() now writes to CONTROLLER_CANONICAL_OUTPUT_PATH
  if set (production subprocess path) or stdout if not (direct-call
  test path; capsys captures). Decouples canonical output from the
  FastMCP JSON-RPC stdout transport — they would otherwise collide
  in the subprocess (both writing to the same stream).
- Atomic file write: open + write + fsync + close.
- Existing test_mcp_builders.py (capsys-based) still passes with the
  fallback path; new test_worker_agent_runner.py exercises the
  file-based subprocess path with real MCPs.

10 new tests in test_worker_agent_runner.py:
- End-to-end with real MCP subprocesses (implementer-resolved,
  reviewer-approve, estimator-tier-recommendation)
- Error paths: unknown role (ValueError), session raises (wrapped
  as WorkerError), session raises WorkerLostLock (propagated),
  lost_lock_check returns True post-session (raises WorkerLostLock),
  no finalize → finalize_timeout → WorkerError
- Role-map sanity: every role has both an MCP module and output model

Total: 376 controller tests; full auto_agents suite 2738 pass.

This commit completes the v1 boundary — the controller now has the
complete stack from V1 contracts through MCP response builders, DB
schema, worker dequeue/heartbeat/runner/workspace, master state
machine + tick + reaper + pickup guard + scheduler + discovery +
Forgejo writes + MERGING + HTTP adapter, AND a production
agent_runner that wires it all to real OpenCode + MCP subprocess
spawning. Phase 1d-3+ enhancements (real OpenCode session wiring
in run_opencode_session) and Phase 2+ migration steps remain.
2026-05-18 14:05:10 -04:00
..