fix(cli): stop workspace create from orphaning its key, self-heal workspace active

Closed
KH
18d ago
+581-22

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 create now goes through Cloud's unified /api/v1/workspaces endpoint (@agent-relay/cloud.createWorkspace, the same path relayfile setup already uses) instead of the direct-Relaycast AgentRelay.createWorkspace. That endpoint persists the missing Postgres row, so the returned key actually resolves. Renamed the command's --base-url flag to --api-url to match workspace 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 missing relaycastApiKey.
  • resolveActiveWorkspace self-heals. It opportunistically caches the resolved cloudWorkspaceId alongside 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-authenticated POST /api/v1/workspaces/{id}/join endpoint 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 /join endpoint already exists and is exactly the right primitive — session-authenticated, returns the canonical relaycastApiKey for 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 cloudWorkspaceId from 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/preserving cloudWorkspaceId across key updates
  • packages/cloud/src/workspaces.test.ts — caches cloudWorkspaceId on success; self-heals via joinWorkspace on a stale key; falls through to the original error with no cached id; falls through when self-heal itself fails; joinWorkspace unit tests
  • packages/cli/src/cli/commands/workspace.test.ts — create stores the Cloud-issued key and prints it; fails loudly when the key is missing
  • packages/cli/src/cli/lib/sdk-command.test.ts (new) — asserts withSdkDefaults().createWorkspace calls @agent-relay/cloud, never the direct-Relaycast SDK
  • npx vitest run packages/cloud packages/cli — 776 passed, 9 pre-existing unrelated failures (confirmed present on main before this branch too), 0 regressions
  • npx tsc --noEmit clean on both packages/cloud and packages/cli for every file this PR touches

🤖 Generated with Claude Code