feat(cloud-worker): mount subscription logins for the run and write refreshed tokens back
Merged
KJ
9d ago+921+921+012345678901234567890123456789-3-3-0123456789
Summary
agent-relay cloud worker now mounts a workspace's subscription logins (Codex ChatGPT and Grok auth files) for the run, and writes refreshed tokens back. This is the worker half of self-hosted Coding agents (AgentWorkforce/relaycontrol, selfhost/coding-agents-subscription-logins) and mirrors how hosted Cloud mounts ~/.codex/auth.json into its sandboxes. Khaliq asked for login credentials to be held server-side the way hosted Cloud does.
Flow
- Payload. A control plane that holds logins adds
cliCredentialsto the assignment payload:{ ref, expiresAt, path: "/api/v1/workers/{workerId}/assignments/<runId>/cli-credentials", credentials: [...] }. The ref is a short-lived, run-scoped reference, not the files. The new field is optional inWorkerWorkflowPayload, and payloads without it behave exactly as before. - Fetch. Before spawning relayflows, the worker calls
GET <baseUrl><path>with its worker token plusX-Cli-Credential-Ref.- Only relative
/api/paths are accepted, so the reference never leaves the control plane origin.
- Only relative
- Write. Each file goes to
<runDir>/.cli-home/<dir>/<file>, with mode 0600 inside 0700 directories.- Only
CODEX_HOMEandGROK_HOMEmay be set, and directory and file names are validated, so a hostile response can't escape the run dir.
- Only
- Env.
CODEX_HOME/GROK_HOMEpoint at those directories and win over inherited values.AGENT_RELAY_SUBSCRIPTION_LOGINS=codex,groklets relayflows give the subscription precedence overOPENAI_API_KEY/XAI_API_KEY(AgentWorkforce/relayflows#65). - Write-back. After the run, any file the CLI rewrote (its own token refresh) is sent back with
PUT .../cli-credentials/<id>. - Cleanup. The run dir, files included, is deleted as before.
- Nothing logs file contents or the reference.
Tests
cloud-worker-cli-credentials.test.ts:
- File modes (0600/0700), the env, and the request headers.
- Rejects a hostile
homeEnvand a path traversal. - Rejects a reference path pointing at another origin.
- Writes back only changed files.
- An assignment-runner test: the CODEX_HOME file is present during the run, the refreshed token is written back, and the run dir is deleted.
On sf-frame: npm ci, npm run typecheck, the new tests and cloud.test.ts (78 tests) pass, and prettier is clean.
🤖 Generated with Claude Code
![Autofix with [code]smith](https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png)
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.
Agent Relay sessions
claudesessiona19e66d9· opened viagh pr create· last active 2026-11-04