fix(broker): workspace-scope auto node id so `up` injection works

Merged
KH
5mo ago
+968-96

Problem

agent-relay up spawns agents that silently never receive injected messages. Broker 9.1.x (#1201, node-only delivery) injects solely over the node-control /v1/node/ws channel, which needs a node token from create_node. The auto-derived node id was sha256(machine_seed, cwd) — workspace-independent — but the engine scopes nodes globally.

So when a project directory is re-pointed at a new workspace (and up mints a fresh workspace on each run), the broker reuses a node id already owned by the old workspace. create_node's INSERT hits a unique-constraint violation, the engine returns it as a misclassified HTTP 500, the retry wrapper collapses it to Max retries exceeded, no token is minted, and every spawned agent is undeliverable — while /health reports relaycastConnected:true.

Regressed in #1201, which removed the firehose delivery path and introduced the cwd-only node id in the same commit.

Fix

Root cause

  • derive_node_id now folds workspace_id into the hash → each (machine, cwd, workspace) gets a distinct node, no cross-workspace collision. The pinned RELAY_NODE_TOKEN (operator-enrolled / fleet) path is unchanged.

Hardening (so this can never be silent again)

  • create_node mint failures log the real HTTP status + body instead of Max retries exceeded; non-retryable 4xx are no longer retried.
  • agent-relay up refuses to auto-spawn when node delivery is down (exit 1 + guidance) instead of producing a dead session.
  • broker /health + /api/status expose nodeConnected/nodeDelivery; status and doctor report it.

Tests

  • cargo test -p agent-relay-broker --lib → 794 passed / 0 failed. New regression test: same (seed, cwd) yields distinct node ids per workspace, stable within one.
  • CLI: vitest (60) + tsc --noEmit + build:cli green. New coverage for the up fail-loud path.

Verification status

  • ✅ Root fix proven: clean-cwd run against the freshly-built release broker minted a workspace-scoped node token with no 500 collision; /health reported nodeDelivery.connected:true.
  • ✅ 401-churn is NOT this change: a node-control /v1/node/ws 401 → re-mint churn was observed after connect. An A/B (our build vs published 9.1.7, identical clean cwd + durable workspace key) reproduced it identically on both, so it is pre-existing / engine-side, not a regression here. Tracked as a follow-up below.
  • ⏳ In progress (not a merge blocker): a full DM-injection e2e between two up-spawned agents; local providers are broken (Codex v0.121 model rejection; Claude MCP-auth), being retried with a minimal echo agent. Evidence will be added as a comment.

Out-of-scope follow-ups (not fixed here)

  • Engine/cloud repo: create_node should return 409 for a node-id conflict (and ideally be idempotent for the same owner) instead of a raw 500; and the /v1/node/ws 401-churn (above) needs root-causing engine-side.
  • agent-relay up mints a fresh workspace on every run (source=fresh) — worth revisiting.
  • The Fix-2 broker-local create_node helper reintroduces a hardcoded RELAYCAST_DEFAULT_BASE_URL fallback; consider routing through the SDK base-URL resolution.

🤖 Generated with Claude Code