fix(cli): stop workspace create from orphaning its key, self-heal workspace active
Closed
KH
18d ago+581+581+012345678901234567890123456789-22-22-01234567890123456789
Summary
Fixes #1260 — agent-relay workspace create (used by agentworkforce login --workspace <key>) minted a workspace directly against Relaycast and never created the corresponding Cloud Postgres row, so workspace active / agentworkforce login / deploy immediately 404 with "Workspace not found" on that same key, forever. Root-caused with a live reproduction in #1260.
What changed
workspace createnow goes through Cloud's unified/api/v1/workspacesendpoint (@agent-relay/cloud.createWorkspace, the same pathrelayfile setupalready uses) instead of the direct-RelaycastAgentRelay.createWorkspace. That endpoint persists the missing Postgres row, so the returned key actually resolves. Renamed the command's--base-urlflag to--api-urlto matchworkspace active's existing flag (it now points at Cloud, not the Relaycast gateway). Fails loudly instead of silently storing an unusable key if the response is missingrelaycastApiKey.resolveActiveWorkspaceself-heals. It opportunistically caches the resolvedcloudWorkspaceIdalongside each local workspace alias. If a key later stops resolving (rotated server-side, or a pre-fix orphan created before this PR), it re-mints a working key for that same workspace via the session-authenticatedPOST /api/v1/workspaces/{id}/joinendpoint and retries once — no new workspace is ever created. Best-effort: any failure (no session, no longer a member, no cached id to heal from) falls through to the original resolve error, so the failure mode is unchanged for cases self-heal can't help with.
Why this shape
- The
/joinendpoint already exists and is exactly the right primitive — session-authenticated, returns the canonicalrelaycastApiKeyfor a workspace the caller already belongs to. No new server-side (cloud repo) code was needed. - Self-heal only fires when there's a cached
cloudWorkspaceIdfrom a prior successful resolve, so it can't paper over a workspace the CLI has never proven the caller has access to.
Test plan
-
packages/cloud/src/workspace-store.test.ts— caching/preservingcloudWorkspaceIdacross key updates -
packages/cloud/src/workspaces.test.ts— cachescloudWorkspaceIdon success; self-heals viajoinWorkspaceon a stale key; falls through to the original error with no cached id; falls through when self-heal itself fails;joinWorkspaceunit tests -
packages/cli/src/cli/commands/workspace.test.ts—createstores the Cloud-issued key and prints it; fails loudly when the key is missing -
packages/cli/src/cli/lib/sdk-command.test.ts(new) — assertswithSdkDefaults().createWorkspacecalls@agent-relay/cloud, never the direct-Relaycast SDK -
npx vitest run packages/cloud packages/cli— 776 passed, 9 pre-existing unrelated failures (confirmed present onmainbefore this branch too), 0 regressions -
npx tsc --noEmitclean on bothpackages/cloudandpackages/clifor every file this PR touches
🤖 Generated with Claude Code