feat(fleet): give the fleet connect a status instead of a log nobody reads

Merged
KH
3mo ago
+664-10

The gap

The dial that makes Factory's own agent online had no status on any surface.

#ensureEventSubscription(): void {
  if (this.#eventsStarted) return
  this.#eventsStarted = true
  void this.#subscribeEvents().catch((error) => {
    this.#eventsStarted = false
    this.#log(`relay fleet event subscription failed: ${errorMessage(error)}`)   // <- the only trace
  })
}

Fire-and-forget, and the rejection went to #log alone. A client that registered an agent and then failed to connect was indistinguishable from a healthy one on /healthz, on /evidence, and in factory status.

Readers substituted eventListener — but that is the orchestrator's issue subscription (factory.ts returns subscribed whenever #subscription is truthy), a different subsystem entirely. That conflation is why days of instruments read healthy while the fleet socket was never up.

Context: AgentWorkforce/factory-cloud#55, where seven consecutive Factory fleet identities show created == last_seen to the second across two published versions.

What this adds

FleetConnectStatus on the fleet port — never-attempted | connecting | connected | failed, with attempts, timestamps and a reduced lastError. The void-catch rejection now lands in that field instead of only in a log line.

Three deliberate choices:

  1. connected is stamped at the dial, not when #subscribeEvents resolves. That function also returns early when the client is disposed, and reporting that as connected would recreate the exact false-healthy signal this removes.
  2. The method is optional on the port. A backend with no socket (the internal fleet) omits it, and an absent value stays absent rather than being invented as healthy. guardFleetControlPlane's Proxy passes it through untouched.
  3. Redaction reuses describeControlPlaneError, now exported so there is one reducer rather than two that can drift. Any cause becomes Name (CODE), code matched against /^[A-Z0-9_]{1,80}$/ — no transport message, URL or credential can reach it.

Deliberately NOT dispatch-gating

fleetConnect is published on /healthz but is not added to DISPATCH_GATING_SUBSYSTEMS.

A failed socket does not itself stop dispatch — roster() runs over HTTP — and listing it would flip ok on a live deployment and hand the container-replacement logic a new reason to cycle. Publishing the fact is the goal; changing what ok means is a separate decision belonging to whoever owns dispatch behaviour. lastError stays behind authenticated /evidence, exactly as it does for the circuit.

Verification

Build typecheck (tsconfig.build.json)exit 0, zero errors
src/fleet + public-health + cli/fleet363 passed
health-projection-guard, atomic-json-file, reaper, diagnose49 passed
orchestrator/factory574 passed
Total986 green

Mutation control: reverting the catch to log-only (the pre-fix behaviour) fails exactly the three tests that assert recording — exit 1, 3 failed | 68 passed — while never-attempted and connected keep passing, because they do not depend on the catch. Restoring returns 71/71.

There is also an explicit control test proving the same harness yields connected when the gate does not throw, so the failure arm is not quietly passing against a client that reports failed unconditionally.

Note: the full tsconfig.json typecheck has pre-existing errors in unrelated test files (e.g. dist-entrypoints.test.ts needs a build). None of the touched files appear, and the build config this package ships from is clean.

Not in scope

No version bump and no release — publishing is not mine to do. This needs a publish and a factory-version.json bump in factory-cloud before it reaches production; the companion factory-cloud PR projects fleetConnect into /evidence and is ready to follow.


Summary by cubic

Publishes a first-class fleet socket status and exposes it on /healthz, CLI, and the heartbeat so failed connects are visible. Previously, failed connects only logged while health read healthy; now fleetConnect reports lifecycle and counters, with redacted causes shown only via authenticated /evidence.

  • Adds FleetConnectStatus to the FleetClient port: never-attempted | connecting | dialed | connected | failed. dialed means the SDK accepted connect() but no stream event has confirmed the socket; a silent workspace can remain dialed.
  • Relay client records lifecycle: listens before dialing, marks dialed after connect(), upgrades to connected on first event, and records drops/errors as failed (RelayEventStreamDisconnected/RelayEventStreamError). Tracks attempts and timestamps.
  • Orchestrator writes fleetConnect into the heartbeat and projects it to public health. Unauthenticated /healthz publishes state and counters only; lastError remains behind /evidence. Not dispatch-gating; ok semantics do not change.
  • CLI: factory status and diagnose render fleetConnect. diagnose now reads fleetConnect.lastError from /evidence when a token is provided and prints it; it also explains dialed as unconfirmed.
  • Exports describeControlPlaneError and introduces FactoryAgentRegistrationErrorCode; registration failures render as Name (CODE) across surfaces.
  • Optional on the port: backends without a socket omit the field and absence is preserved. No migrations required.