[AR-448] Make Relay workspace identity durable across node restarts

Closed
KH
4mo ago
+959-29

Closes AR-448.

The bug

A node started with no project-pinned workspace fell straight through to the broker, which mints a brand-new messaging-only workspace (startup_single_session_set_from_sources, crates/broker/src/relaycast/auth.rs).

Nothing errored. The node came up, the resident agent registered, everything looked healthy — but the agent was a stranger in a different workspace with a new address, so every message sent to its previous address went nowhere. khaliq-chief became a new process-lifetime agent on each such start.

The fix

node up (and the local up alias) now resolves the workspace in this order:

  1. explicit --workspace-key / RELAY_WORKSPACE_KEY / AGENT_RELAY_WORKSPACE_KEY / RELAY_API_KEY, or a RELAY_NODE_TOKEN that already implies a workspace;
  2. the project pin from a previous start;
  3. the machine-global canonical workspace (agent-relay workspace join|switch) — new;
  4. mint a new workspace — last resort.

Step 3 is the fix. The resolved key is pinned to the project after startup, so later starts take step 2 and no key is ever copied by hand. A machine with no canonical workspace set behaves exactly as before.

Also made the invariant observable rather than assumed:

  • workspace active --json gains a dataPlane object (unified, the shared workspaceId, per-plane IDs, and the names of any that diverge). The human output prints the Relaycast ID it previously omitted. --require-unified turns a divergence into a non-zero exit for setup doctors and supervisors.
  • node status prints the durable Workspace: ID next to the masked workspace key.

Acceptance

workspace active --json proves one data-plane workspace ID — run against the live default workspace:

$ agent-relay workspace active --json --require-unified
{
  "name": "default",
  "key": "rk_live_…9812",
  "cloudWorkspaceId": "50587328-441d-4acb-b8f3-dbe1b3c5de99",
  "relaycastWorkspaceId": "rw_7ccfea89",
  "relaycastApiKey": "rk_live_…9812",
  "relayfileWorkspaceId": "rw_7ccfea89",
  "relayauthWorkspaceId": "rw_7ccfea89",
  ...
  "dataPlane": {
    "unified": true,
    "workspaceId": "rw_7ccfea89",
    "planes": { "relaycast": "rw_7ccfea89", "relayfile": "rw_7ccfea89", "relayauth": "rw_7ccfea89" },
    "divergent": []
  }
}
$ echo $?
0
$ agent-relay workspace active --require-unified
Workspace: default
Cloud workspace ID: 50587328-441d-4acb-b8f3-dbe1b3c5de99
Relaycast workspace ID: rw_7ccfea89
Relayfile workspace ID: rw_7ccfea89
Relayauth workspace ID: rw_7ccfea89
Data-plane workspace ID: rw_7ccfea89 (unified)

node up uses the canonical workspace with no manual key copying — core.test.ts › up falls back to the machine-global canonical workspace when nothing else selects one, and up prefers the project pin over the machine-global canonical workspace.

Stop/start regression test — packages/cli/src/cli/lib/workspace-identity-restart.test.ts. Each start() is a fresh CLI process (new env, new deps) over one persistent checkout and AGENT_RELAY_HOME, which is what a stop/start looks like from the CLI. It models the two behaviors that decide the outcome: the broker joins RELAY_WORKSPACE_KEY when set and otherwise mints, and Relaycast returns the existing agent when a name is re-registered in a workspace it already belongs to.

  • keeps one workspace and one resident address across a restart
  • joins the canonical workspace on a first start with no project pin
  • pins the canonical workspace to the project so later starts resume it
  • explicit --workspace-key still wins
  • a second checkout shares the canonical workspace and the resident address
  • negative control: with no canonical workspace, a second checkout is a different node entirely

Status and logs never print credentials — core.test.ts › status reports the durable workspace ID without leaking any credential asserts the raw workspace key, node token, ot_live_ tokens, and ?token=-style URLs are all absent. workspace.test.ts asserts the same for workspace active human output. The canonical-workspace fallback logs only its source, never the key.

Documented — specs/workspace-identity.md covers the invariant, resolution order, how to prove it, secret handling, and migration for existing local nodes (including the one behavior change: a node with no project pin now joins the canonical workspace instead of minting, which moves its resident agents there).

Test evidence

$ npx vitest run packages/cloud/src/workspace-convergence.test.ts \
    packages/cli/src/cli/lib/workspace-identity-restart.test.ts \
    packages/cli/src/cli/commands/workspace.test.ts \
    packages/cli/src/cli/commands/core.test.ts \
    packages/cli/src/cli/commands/node.test.ts \
    packages/cli/src/cli/lib/broker-lifecycle.test.ts

 Test Files  6 passed (6)
      Tests  146 passed (146)

Full packages/cli + packages/cloud: 1095 passed, 20 skipped, 0 failed. npm run typecheck clean; npm run lint 0 errors (no new warnings).

One pre-existing environmental failure, integration-relayfile-contract.test.ts › replaces a stale v1 daemon only when the installed binary can serve v3, reproduces identically on a stashed clean tree — the sandbox can't invoke lsof. Unrelated to this change.

Note

node up's test harness now defaults AGENT_RELAY_HOME to an isolated temp dir. Without that, the new store fallback made tests read (and nearly print) whatever workspace the developer's own machine had active.

Do not merge — the principal owns the merge gate.

🤖 Generated with Claude Code