Files
cleveragents-core/tools/controller/worker
drew 7b1b68b784 feat(controller): Phase 1e — master + worker __main__ entry points
Runnable as `python -m tools.controller.{master,worker}` for systemd
deployment. Wires the production callbacks (Forgejo HTTP +
OpenCode session adapter + agent_runner) into the previously-shipped
main loops.

tools/controller/master/__main__.py:

- Args: --owner, --repo (required); --opencode-url, --log-level,
  --tick-interval, --discovery-only-once (smoke flag).
- Reads CLEVERAGENTS_DB_URL (returns 2 if unset — operator-actionable
  error visible in systemd logs).
- build_cfg_stub reuses _mcp_common.ForgejoCfg → PAT rotation +
  Forgejo URL env vars work without a new config module.
- SIGTERM/SIGINT → stop_event → master_main_loop drains + exits.
- --discovery-only-once: runs one discovery sweep + exits. Useful for
  initial backfill or smoke testing the Forgejo callback wiring
  without committing to the long-running loop.

tools/controller/worker/__main__.py:

- Args: --opencode-url, --roles (default all 5; comma-separated),
  --max-concurrent, --poll-interval, --log-level, --no-startup-janitor.
- Empty --roles after parsing → exit 2.
- Startup sequence:
  1. Run orphan-workspace sweep (unless --no-startup-janitor)
  2. Build OpenCode session adapter via wire_opencode_session
  3. Wrap production_agent_runner with the session injected
  4. Wire SIGTERM/SIGINT → stop_event
  5. Enter worker_main_loop

9 new tests in test_entry_points.py:
- Arg parsing: master --owner/--repo required; both DB-URL-missing
  returns 2; worker empty-roles returns 2.
- --help works via subprocess for both (no SystemExit issue).
- Master --discovery-only-once smoke: stubs build_callbacks + cfg
  builder; verifies exit 0 + the discovery call chain runs.
- Worker --no-startup-janitor: stubs sweep + loop; verifies sweep
  is skipped when flag set, runs when not.

Total: 407 controller tests; full auto_agents suite 2769 pass.

Controller v1 is now end-to-end deployable as two systemd units
(one per machine for each instance). The remaining work for a real
production rollout is:
- Real prefetch callbacks for the scheduler (assembling V1 input
  payloads from Forgejo data)
- Backfill at master startup (existing-PRs → DISCOVERED rows)
- Reconciliation tick (DB ↔ Forgejo sync)
- Per-role prompt templates (the V1 prompt stub works; specialised
  per-role prompts will land as agents are migrated)
2026-05-18 14:12:56 -04:00
..