fix(broker): workspace-scope auto node id so `up` injection works
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_idnow foldsworkspace_idinto the hash → each(machine, cwd, workspace)gets a distinct node, no cross-workspace collision. The pinnedRELAY_NODE_TOKEN(operator-enrolled / fleet) path is unchanged.
Hardening (so this can never be silent again)
create_nodemint failures log the real HTTP status + body instead ofMax retries exceeded; non-retryable4xxare no longer retried.agent-relay uprefuses to auto-spawn when node delivery is down (exit 1 + guidance) instead of producing a dead session.- broker
/health+/api/statusexposenodeConnected/nodeDelivery;statusanddoctorreport 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:cligreen. New coverage for theupfail-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;
/healthreportednodeDelivery.connected:true. - ✅ 401-churn is NOT this change: a node-control
/v1/node/ws401 → 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_nodeshould return409for a node-id conflict (and ideally be idempotent for the same owner) instead of a raw500; and the/v1/node/ws401-churn (above) needs root-causing engine-side. agent-relay upmints a fresh workspace on every run (source=fresh) — worth revisiting.- The Fix-2 broker-local
create_nodehelper reintroduces a hardcodedRELAYCAST_DEFAULT_BASE_URLfallback; consider routing through the SDK base-URL resolution.
🤖 Generated with Claude Code