fix(cli): stop read-only commands registering a relay agent (factory-cloud#55)

Merged
KH
3mo ago
+443-5

Fix 1 of the ruling on factory-cloud#55: make the read-only CLI paths side-effect-free. Not a re-diagnosis — the cause is accepted as stated.

Enumeration: every non-live CLI path that constructs Factory

All Factory construction in the CLI goes through one site, src/cli/fleet.ts:551 ((deps.createFactory ?? createFactory)(...)). So the enumeration is exactly "which commands reach that line", and it is closed by construction rather than by grep.

Reaches the construction site — classified read-only and covered:

CommandWhy read-onlyPre-daemon in the container?
factory statusrenders in-memory counters; Factory.status() is synchronous and touches no fleetyes — entrypoint.mjs:1028, every boot
factory run-once --dry-runtriages and reports, dispatches nothingyes — entrypoint.mjs:1049, when FACTORY_START_ENABLED !== '1'
factory loop --dry-runsame sweep, repeatedno
factory loop-statusreads the heartbeat file; never touches the Factory objectno
factory kill-loopreads the heartbeat, signals a pid; never touches the Factory objectno
factory canary <issue>hardcoded runOnce({ dryRun: true })no
factory triage <issue>triageIssue() + JSON out; no dispatchno
factory dispatch <issue> --dry-rundispatch(decision, { dryRun: true })no

loop-status and kill-loop do not use the Factory at all, but they reach the same construction site, so they are classified with the rest rather than left to register by accident.

Reaches the construction site — deliberately NOT read-only: factory start, live run-once, live loop, and dispatch on any backend without --dry-run. These spawn agents and need both an identity and the exit wiring.

One trap worth naming: dispatch --backend internal skips the relay start({ mode: 'dispatch-owner' }) branch at fleet.ts:989 but still falls through to factory.dispatch(decision, { dryRun: false }), which spawns a real team. My first classifier had it as read-only; it is not, and there is a test case pinning that.

Never constructs Factory (return before the site) — out of scope, listed for completeness: featuremap check, factory diagnose, factory init, factory intake …, factory close-probe, factory reap-orphans, factory babysit, and the fleet … subcommands. Two of these — reap-orphans and babysit — do receive the live fleet client and can register, but neither is read-only (reap-orphans releases agents) and neither is a pre-daemon gate, so both keep a registering client on purpose.

Also out of scope, stated rather than silently skipped: --backend internal bootstraps a broker via ensureRelayBroker before any of this. That is a separate local-dev side effect, not the cloud outage, and constraining it would break factory status --backend internal for anyone without a running broker. readOnly is relay-only.

The fix, and why one layer was not enough

Layer 1 — FactoryPorts.readOnly. The constructor's unconditional #wireFleetEvents() is now gated, and start() refuses on a read-only instance. This is the mechanism named in the ruling.

Layer 2 — RelayFleetClient.readOnly. #bootstrapMessaging throws ReadOnlyFleetIdentityError instead of calling #registerFactoryAgent. An explicitly supplied agentToken/messaging still works, so a read-only command that already owns an identity keeps full fleet reads; what it cannot do is create one.

I ablated each layer against the end-to-end tests, and the result changed the shape of the fix:

  • Layer 1 alone: status is clean, run-once --dry-run still registers. A GitHub-backed sweep calls #githubOrphanRecoveryContext() (factory.ts:2850), which reads fleet.roster() regardless of dryRun, and that mints the identity on demand. The cloud config template is issueSource: "github", so this is the real path, not a hypothetical.
  • Layer 2 alone: all three pass. It is the load-bearing half.

Layer 1 stays because without it every read-only command still attempts the subscription and logs a failure it cannot act on. The two together mean no path through a read-only command can plant a row, present or future.

The degraded outcome on that roster read is intentional: #githubOrphanRecoveryContext already catches control-plane failures, increments githubOrphanRecoveryContextFailures, and continues. A dry-run probe that cannot enumerate the fleet without creating an agent should report less, not write more.

The must-not-fire test

src/cli/fleet.test.ts → read-only CLI commands leave no workspace identity. Every other test in that file injects fleet or createFleet and so never exercises the client that registers. These run the real RelayFleetClient through the real buildFleet, stubbing only the relay engine (new deps.createRelay seam, alongside the existing cloudReporterFetch / diagnoseFetch hermetic transports), and assert on the one thing the outage turned on: whether agents.register was called.

  • creates no agent row for the status preflight the cloud container runs on every boot
  • creates no agent row for the run-once dry run that runs ahead of a start-disabled boot — GitHub-backed, so it goes through the orphan-recovery roster read
  • control arm: still registers for a command that is not read-only (fleet roster) — proves the harness can reach registration, so the two above cannot pass vacuously

Verified fail-first by reverting the guards. Both fail on the register call itself, not on a downstream symptom:

AssertionError: expected [ { name: 'factory' } ] to deeply equal []

Supporting tests: read-only classification of every Factory-constructing command (a 12-case table over the enumeration above, driven through the real CLI); read-only construction in factory.test.ts (zero fleet subscriptions, with a live control arm, plus start() refusing); and two at the client level in relay-fleet-client.test.ts mirroring the existing registers the factory agent identity from a workspace key on first use control.

Explicitly not in this PR

  • #subscribeEvents and the awaited readiness gate — factory#341, factory-dispatch-lead-0823. Nothing here touches that function.
  • The #assertAgentNotLive reclaim inversion — separate PR, next. This stops new orphans being planted; that makes an orphan from any crashed daemon recoverable. Both are needed.

🤖 Generated with Claude Code


Summary by cubic

Stops read-only CLI commands from registering a relay agent, preventing orphaned identities that blocked the live daemon (factory-cloud#55). Previously, constructing Factory always subscribed to fleet events and the relay client minted an identity on first roster read; now read-only paths neither subscribe nor register.

  • CLI decides read-only once via isReadOnlyFactoryCommand and passes the flag to both buildFleet and createFactory.
  • FactoryPorts.readOnly: constructor skips #wireFleetEvents(); start() rejects on a read-only instance.
  • RelayFleetClient.readOnly: refuses registration by throwing ReadOnlyFleetIdentityError; if an agentToken/messaging is supplied, reads still work without creating an agent.
  • Read-only commands: status, run-once --dry-run, loop --dry-run, loop-status, kill-loop, canary, triage, dispatch --dry-run. Live paths remain registering: start, live run-once/loop, live dispatch (including --backend internal).
  • Tests: end-to-end asserts no registration for status and run-once --dry-run with a control fleet roster that does register; unit tests cover factory wiring and client refusal.
  • Maintenance: new commands default to live; explicitly opt in to read-only in isReadOnlyFactoryCommand when behavior must be side-effect-free.