feat(cli): distinguish cross-node agents from missing agents on attach 404

Merged
KH
4mo ago
+349-6

Summary

Closes requirement #3 of relay#1449 (clear, distinct failure when agent is on another node vs. not found at all). Phase 1 of 2.

  • Before: agent-relay node agent attach remote-worker on a machine where the agent is running on finn-mini produces Error: no agent named 'remote-worker' — byte-identical to a typo. The prior proof lane (relay-1449-proof-0810) confirmed 9/9 cross-node attempts hash to the same stderr as a nonexistent-name control.
  • After: The same command produces Error: agent 'remote-worker' is registered on node 'finn-mini'; cross-node attach is not yet supported — run \agent-relay node agent attach remote-worker` on that machine`

What changed

  • fleet-hint.ts (new): best-effort workspace-registry lookup. On 404, calls relay.agents.get(name) → reads metadata.fleet.nodeId → looks up the human-readable node name via relay.nodes.list(). All errors swallowed; returns null on any failure so the caller falls back to the original message. Injectable factory param keeps it testable without network.
  • attach.ts: added optional fleetHint?(name) => Promise<string|null> to AttachSnapshotDeps. On 404, awaits the hint and builds an enhanced message; falls through to no agent named 'X' when hint is null or absent.
  • attach-view.ts, attach-drive.ts, attach-passthrough.ts: wired resolveFleetHint as the default fleetHint dep; threaded it through captureAndRenderSnapshot calls.
  • captureInitialSnapshot in attach.ts: threads fleetHint from its own deps to the render call so drive/passthrough also get the better message via this code path.

Test plan

  • fleet-hint.test.ts — 9 new unit tests covering: no fleet metadata, agent not in registry, node found by nodeId, node found by id, node roster throws (raw-ID fallback), no matching node (raw-ID fallback), createRelay throws (no credentials), null metadata
  • attach.test.ts — 2 new tests: fleet hint upgrades the message; null hint preserves original message
  • Mutation test: replacing deps.fleetHint ? await deps.fleetHint(agentName) : null with null causes the cross-node test to fail — confirms tests bite
  • 279 total tests pass across all 5 affected test files

Phase 2 (Cloud-owned, pending)

The existing cloud terminal-streaming mechanism (cloud/ARCHITECTURE.md:133-176) — broker on 0.0.0.0:9800, HMAC-derived API key, signed 24h preview URL, existing WS protocol — is the right transport for cross-node attach on Daytona fleet nodes. Cloud needs a new endpoint:

POST /workspaces/{wid}/fleet-nodes/{nodeId}/agents/{name}/attach
→ { execUrl: string, apiKey: string, ttl: number }

Relay CLI adds --node <nodeId> to the attach command, calls this endpoint, and substitutes the returned execUrl for the local broker URL. Mode preservation (view/drive/passthrough) is automatic — same WS protocol. The endpoint returns 503 when the node is unreachable (distinct from 404 = agent not present on that node). Physical fleet nodes (barry, finn-mini, sf-mini) are a Phase 3 problem — no signed-URL mechanism exists for boxes without a Daytona-style preview-URL API.


🤖 Generated with Claude Code