fix(auth): send a stored CLI session only to the cloud that issued it

TH
8h ago
+138-11

Summary

The CLI stores the cloud origin with its CLI session, but AGENT_DEVICE_CLOUD_BASE_URL overrode it, so the stored refresh credential was sent to whatever origin that variable named.

  • A refresh refuses an override with a different origin before any request: UNAUTHORIZED, reason cloud_login_origin_mismatch (declared in a ReasonTable), with a hint to log in again or unset the variable. The same origin with a different path or trailing slash still works.
  • Cloud auth requests (device-auth start and poll, session refresh) send redirect: 'error', so a redirect cannot resend a credential or device code to another host.

Stacked on #4833; retarget to main after it merges.

Before / after

Stored CLI session issued by https://cloud.example, refresh with a different origin named
(the new test covers both rows; a same-origin override with another path still refreshes):

base: POSTs the stored refresh credential to https://elsewhere.example
head: refuses before any request
      UNAUTHORIZED: The stored CLI session belongs to another cloud API origin.
      reason: cloud_login_origin_mismatch

Validation

  • Tested at af2a887668. New test a stored CLI session is sent only to the cloud that issued it, without redirects fails on the base commit and passes at head.
  • Locally at head: pnpm format, pnpm typecheck, pnpm lint, pnpm check:layering, and npx vitest run src/cli pass (one unrelated timing test failed once under host load and passes alone).
  • CI obligations: GitHub lanes cover the rest. Unresolved risks: none known.

Run Proof on this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.