Repository navigation
fix(auth): only the running Gateway refreshes shared OAuth sign-ins - #198214
Conversation
A local CLI command beside a running Gateway could refresh a shared OAuth token and store it. The Gateway kept reading its stale copy, and every later turn failed with "No credentials found for profile". Each command had to opt in to the state-owner check, and several did not (`infer image/audio/ video/web/embedding`, local `tts convert`, `infer model auth login/logout`). Enforce ownership once, at the OAuth refresh: a process that does not own the state uses a still-valid token as is and refuses an expired one before any write, naming the Gateway. With no Gateway, refresh runs locally as before. `infer model auth login/logout` now use the `models auth` state owner and credential removal. `agent --local`, `agent exec --state-dir`, `tui --local` and `models status --probe` take state ownership through `runWithLocalStateOwner`; the duplicate embedded state lock and its four refusal messages are removed. Co-authored-by: obviyus <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
|
Codex review: needs maintainer review before merge. What this changesThis PR prevents local OAuth refreshes beside a running Gateway and consolidates credential edits and embedded commands under existing state owners. Example: Run openclaw infer audio transcribe while the Gateway owns an OAuth sign-in with about 4.5 minutes remaining.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR addresses a confirmed shared-credential ownership bug; the earlier findings are resolved, and no new actionable defect was found. Priority: P1 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherCLI inference and agent commands resolve credentials through the OAuth manager, which returns usable access tokens or refreshes persisted credentials under the selected state owner. flowchart TD
A[Local CLI command] --> B[OAuth manager]
B --> C[Credential scope and state owner]
C --> D[Use valid token without refresh]
C --> E[Refuse foreign owned refresh]
C --> F[Owner refresh and persistence]
D --> G[Provider request]
F --> G
Technical reviewBest possible solution: Keep refresh ownership in the OAuth manager and reuse the existing state and credential-removal owners for affected commands. Do we have a high-confidence way to reproduce the issue? Current-main source exposes the unguarded refresh path, and the supplied CLI/Gateway comparisons report matching failures; this read-only review did not execute the reproduction. Is this the best way to solve the issue? The change repairs the credential writer boundary, preserves intentional Gateway cache behavior, and removes competing command-specific ownership implementations. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 777c036d5928. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource -73, Tests -365, Docs +5. Total -433 across 30 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (2 earlier review cycles)Reviewed November 13, 2026, 7:33 AM ET / 12:33 UTC (Revision 3). |
…r exports Co-authored-by: obviyus <[email protected]>
An exec run redirects its own state directory while it keeps the original credential scope. Discover the state owner from that scope, so a refresh of credentials a running Gateway owns is still refused. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
🦞👀 Re-review progress:
|
|
Maintainer override: the only red jobs are |
Peer settlement moved into oauth-store in #198214, leaving the helper without an external consumer. Remove only its unused export; preserve the internal call and all credential settlement behavior. Co-authored-by: obviyus <[email protected]> Co-authored-by: steipete <[email protected]>
…penclaw#198214) ## Problem The rule "outside writers route through the Gateway or hold exclusive offline custody" ([worker access](https://docs.openclaw.ai/reference/database-schemas/worker-access)) was enforced per command. Every command that forgot it could refresh a shared OAuth sign-in beside a running Gateway and store the new token. The Gateway keeps reading its own copy, its next refresh claim loses, and every later turn on that profile fails with `No credentials found for profile "<provider>:<account>"` until the Gateway restarts. Unguarded today: `infer image/audio/video/web/embedding`, local `infer tts convert`, `infer model auth login/logout`. `agent --local`, `agent exec --state-dir`, `tui --local` and `models status --probe` had a second, duplicate owner (`acquireEmbeddedStateLock`) with four separate refusal messages. ## Fix: one owner **The OAuth refresh is the choke point.** A read-only command can only write credentials by refreshing an OAuth sign-in. `resolveOAuthAccessBesideStateOwner` (called once from the OAuth manager, before the refresh queue, claim or any write) decides: - This process owns the state (the Gateway itself, or a `runWithLocalStateOwner` holder), or no Gateway runs: unchanged. - A Gateway (or another state holder) owns it, and the token is still valid: use it as is, no refresh. The Gateway refreshes on its next turn. - A Gateway owns it, and the token has expired (or the provider rejected it): refuse before any write: ``` The <provider> sign-in "<profile>" needs a refresh, but the Gateway (pid N, port P) owns the shared credentials and only it may refresh them. No credential was changed. Run this through the Gateway, retry after its next turn refreshes the sign-in, or stop the Gateway through its service owner and rerun this command. ``` No Gateway method, protocol, config or schema change. Nothing changes on the Gateway side (no freshness probes, no cache retirement). **Explicit credential edits reuse the `models auth` owner.** - `infer model auth login` runs the `models auth login` path: with a Gateway running, the sign-in runs through `models.authLogin`. - `infer model auth logout` uses the `models auth` state owner (refuses while a Gateway runs) and the shared `removeModelAuthCredentials` owner. This also forgets the provider's saved catalog rows, which the private copy skipped. **Embedded runs use the one state owner.** `agent --local`, `agent exec --state-dir`, `tui --local` and `models status --probe` take custody through `runWithLocalStateOwner` and share its refusal text (`... This command is offline-only. <online alternative> To run offline, stop the Gateway ...`). `agent exec --state-dir` keeps its retained custody after uncertain runtime cleanup through `retainAfterUncertainCleanup`. `infer model run` keeps its openclaw#198141 routing. ## Caller map | CLI path | Writes while a Gateway runs | Covered by | |---|---|---| | `infer model run` (default / `--local`) | refresh | openclaw#198141 routing / refusal (unchanged) | | `infer image/audio/video/web/embedding`, local `tts convert` | refresh | refresh owner | | `infer model auth login` | sign-in | `models auth login` owner (routes) | | `infer model auth logout` | profile removal | `models auth` owner (refuses) | | `agent --local`, `agent exec --state-dir`, `tui --local`, `models status --probe` | refresh + all state | `runWithLocalStateOwner` (refuses) | | `models auth *`, `mcp` credential commands, `memory` | as before | `runWithLocalStateOwner` (unchanged) | | any other local refresh (plugins, tools) | refresh | refresh owner | ## Deleted - `src/infra/embedded-state-lock.ts`: `acquireEmbeddedStateLock`. The signal bridge moved to `process-signal-bridge.ts`. - The four per-command refusal formatters (`formatActiveGatewayLocalRefusal`, `formatActiveGatewayExecRefusal`, `formatActiveGatewayTuiRefusal`, `formatActiveGatewayModelsProbeRefusal`). - The private `infer model auth logout` implementation (`runModelAuthLogout`) and the direct login call. Production diff (excluding tests and docs): **+251 / −324, net −73** (includes moving `settleOAuthRefreshPeerClaimsUnderLock` unchanged from `oauth-manager.ts` to `oauth-store.ts` to respect the line cap). ## Accepted edge cases - A failed `models status --probe` batch now reports after its in-flight probes settle, because ownership must outlive them. - Usage bookkeeping (cooldown and last-used stats) from a local command is not a credential refresh and is not gated here. ## Tests (red on `main`, green here) - `src/agents/auth-profiles/oauth-manager.test.ts` › `credentials owned by a running Gateway` › `uses a still-valid sign-in without refreshing it`. On main the CLI refreshes the in-window token. - … › `refuses to refresh an expired sign-in before any write`. On main the refresh runs and stores. - … › `refuses for the retained credential scope of a redirected run`. Without the scope fix, owner discovery reads the process env and the refresh runs. - `src/cli/capability-cli/model.state-owner.test.ts` › `refuses infer model auth logout while the Gateway owns the shared credentials`. On main logout enters local agent admission. Updated: `agent-via-gateway`, `agent-exec.state-lock`, `agent-exec.auth`, `tui`, `list.probe`, `list.probe.cleanup`, `database-port-admission` (ownership assertions now use the shared owner; tests that duplicated `runWithLocalStateOwner`'s own suite were removed). ## E2E Isolated scratch Gateway per case (one at a time, own state dir and port), real agent turns, two OAuth sign-ins: a stand-in OAuth provider (local mock token endpoint) and a real hosted OAuth provider. `main` = 92217cc build, `fix` = PR build (a1045f3; later commits change only the redirected-scope lookup and tests). "Writer" = the process that stored a refreshed credential (from the credential row's writer record). "upvalid" = token valid for ~4.5 min; "upexpired" = token expired, Gateway up; "down" = no Gateway. Each up case runs 8–10 Gateway turns around the command. | Case | main: command | main: writer | main: later turns | fix: command | fix: writer | fix: later turns | |---|---|---|---|---|---|---| | `infer model run` up valid / expired | via Gateway, OK | Gateway | 10/10, 8/8 | via Gateway, OK | Gateway | 10/10, 8/8 | | `infer image describe` up valid | local, OK | Gateway | 10/10 | local, OK, no refresh | Gateway | 10/10 | | `infer image describe` up expired | `No API key found` | Gateway | 8/8 | ownership refusal, nothing written | Gateway | 8/8 | | `infer audio transcribe` up valid | local, OK, **refreshed a valid token** | **CLI** | **9/10** (`No credentials found for profile`) | local, OK, no refresh | Gateway | 10/10 | | `infer audio transcribe` up expired | local, OK, refreshed | **CLI** | 8/8 | refused, nothing written (see note) | Gateway | 8/8 | | `infer tts convert` up valid | local, OK, **refreshed a valid token** | **CLI** | **9/10** (`No credentials found for profile`) | local, OK, no refresh | Gateway | 10/10 | | `infer tts convert` up expired | local, OK, refreshed | **CLI** | 8/8 | ownership refusal, nothing written | Gateway | 8/8 | | `infer model auth login` up | local sign-in, stored | **CLI** | 10/10 | routed to `models auth login` (refuses a non-Gateway method) | Gateway | 10/10 | | `infer model auth logout` up | removed beside the Gateway | – | 10/10 (Gateway re-stored the profile) | `models auth` refusal, nothing removed | Gateway | 10/10 | | `agent --local` + `models status --probe` up | 2 separate refusals | Gateway | 10/10 | shared `runWithLocalStateOwner` refusal | Gateway | 10/10 | | every command with no Gateway (model run, image, audio, tts, login, logout, `agent --local`, probe) | OK, refreshes locally | CLI | – | OK, refreshes locally | CLI | – | Summary: on `main`, the CLI is a second credential writer beside a running Gateway in audio, tts and login, and the next Gateway turn fails with `No credentials found for profile`. On this branch the Gateway is the only writer whenever it runs, and no Gateway turn fails. Offline behavior is unchanged. Notes: - `infer audio transcribe` with an expired, Gateway-owned sign-in reports `No audio transcription provider is configured or ready` instead of the ownership message, because the shared `hasAvailableAuthForProvider` probe maps every auth error to "not available". Nothing is written. Follow-up, not in this PR (the probe has six other callers). - `auth-triggered model runtime refresh failed: Plugin invocation scope is closed` shows in Gateway logs on both `main` and this branch. It is not caused by this change and did not fail any turn. Data compatibility: no schema, stored credential shape, config or protocol change. The same stored sign-ins (copied from a real install) load and work before and after. Co-authored-by: Ayaan Zaidi <[email protected]>
Peer settlement moved into oauth-store in openclaw#198214, leaving the helper without an external consumer. Remove only its unused export; preserve the internal call and all credential settlement behavior. Co-authored-by: obviyus <[email protected]> Co-authored-by: steipete <[email protected]>
…94251) fix(infer): stop video runs on Ctrl+C `openclaw infer video` dropped its execution context, and with it the cancel signal, so Ctrl+C left the provider request open until the CLI was force-killed about 2 seconds later. The existing execution context is now passed through, as audio already does. Uninterrupted runs are unchanged. Evidence: built CLI with a real MP4 against a slow local video-provider fixture, twice per ref. Main needed SIGKILL after about 2,090 ms; with this change the CLI exits with 130 in under 100 ms and the HTTP request closes within 1.1 ms. Uninterrupted output and request bodies are identical. Details: openclaw#194251 (comment). Thanks @Alix-007. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 3af1447) Release adaptation: Forward video execution signal/assertCurrent and move existing guarded fetch/process scope to shared file owner. Retain baseline audio CLI and filesystem resolution: excluded HOLD audio/path migration not imported. Only13-line cancel regression, no fixture refactor. Follow-up dispositions: 577dd00 fix: send infer audio/video --model files only to the requested model (openclaw#190287); bcb47e8 feat(google): add Nano Banana 2.1 image model as the default (openclaw#200468); 6f5d3b3 fix: local path commands pick a neighboring file when a symlinked folder is followed by `..` (openclaw#200031); 3577724 fix(auth): only the running Gateway refreshes shared OAuth sign-ins (openclaw#198214); 81daea9 fix(cli): run infer model run through the Gateway that owns state (openclaw#198141); 22b1aac fix(media): cancel interrupted attachment downloads (openclaw#194399); e43ec1e fix(infer): cancel interrupted image descriptions (openclaw#194391) Validation: Baseline canceled-video assertion fails with16controls passing; candidate17/17passes,194251-green.log. Existing audio cancellation and workspace/cleanup behavior retained. Real MP4/CLI interrupt proof remains upstream-attributed, not locally rerun. Co-authored-by: RomneyDa <[email protected]> (cherry picked from commit 853a96335df6193815789ebcc02d38af11123fcf) (cherry picked from commit 2ea2570de7f13823a8174590bfddfbfabdbcd915)
Problem
The rule "outside writers route through the Gateway or hold exclusive offline custody" (worker access) was enforced per command. Every command that forgot it could refresh a shared OAuth sign-in beside a running Gateway and store the new token. The Gateway keeps reading its own copy, its next refresh claim loses, and every later turn on that profile fails with
No credentials found for profile "<provider>:<account>"until the Gateway restarts.Unguarded today:
infer image/audio/video/web/embedding, localinfer tts convert,infer model auth login/logout.agent --local,agent exec --state-dir,tui --localandmodels status --probehad a second, duplicate owner (acquireEmbeddedStateLock) with four separate refusal messages.Fix: one owner
The OAuth refresh is the choke point. A read-only command can only write credentials by refreshing an OAuth sign-in.
resolveOAuthAccessBesideStateOwner(called once from the OAuth manager, before the refresh queue, claim or any write) decides:This process owns the state (the Gateway itself, or a
runWithLocalStateOwnerholder), or no Gateway runs: unchanged.A Gateway (or another state holder) owns it, and the token is still valid: use it as is, no refresh. The Gateway refreshes on its next turn.
A Gateway owns it, and the token has expired (or the provider rejected it): refuse before any write:
No Gateway method, protocol, config or schema change. Nothing changes on the Gateway side (no freshness probes, no cache retirement).
Explicit credential edits reuse the
models authowner.infer model auth loginruns themodels auth loginpath: with a Gateway running, the sign-in runs throughmodels.authLogin.infer model auth logoutuses themodels authstate owner (refuses while a Gateway runs) and the sharedremoveModelAuthCredentialsowner. This also forgets the provider's saved catalog rows, which the private copy skipped.Embedded runs use the one state owner.
agent --local,agent exec --state-dir,tui --localandmodels status --probetake custody throughrunWithLocalStateOwnerand share its refusal text (... This command is offline-only. <online alternative> To run offline, stop the Gateway ...).agent exec --state-dirkeeps its retained custody after uncertain runtime cleanup throughretainAfterUncertainCleanup.infer model runkeeps its #198141 routing.Caller map
infer model run(default /--local)infer image/audio/video/web/embedding, localtts convertinfer model auth loginmodels auth loginowner (routes)infer model auth logoutmodels authowner (refuses)agent --local,agent exec --state-dir,tui --local,models status --proberunWithLocalStateOwner(refuses)models auth *,mcpcredential commands,memoryrunWithLocalStateOwner(unchanged)Deleted
src/infra/embedded-state-lock.ts:acquireEmbeddedStateLock. The signal bridge moved toprocess-signal-bridge.ts.formatActiveGatewayLocalRefusal,formatActiveGatewayExecRefusal,formatActiveGatewayTuiRefusal,formatActiveGatewayModelsProbeRefusal).infer model auth logoutimplementation (runModelAuthLogout) and the direct login call.Production diff (excluding tests and docs): +251 / −324, net −73 (includes moving
settleOAuthRefreshPeerClaimsUnderLockunchanged fromoauth-manager.tstooauth-store.tsto respect the line cap).Accepted edge cases
models status --probebatch now reports after its in-flight probes settle, because ownership must outlive them.Tests (red on
main, green here)src/agents/auth-profiles/oauth-manager.test.ts›credentials owned by a running Gateway›uses a still-valid sign-in without refreshing it. On main the CLI refreshes the in-window token.refuses to refresh an expired sign-in before any write. On main the refresh runs and stores.refuses for the retained credential scope of a redirected run. Without the scope fix, owner discovery reads the process env and the refresh runs.src/cli/capability-cli/model.state-owner.test.ts›refuses infer model auth logout while the Gateway owns the shared credentials. On main logout enters local agent admission.Updated:
agent-via-gateway,agent-exec.state-lock,agent-exec.auth,tui,list.probe,list.probe.cleanup,database-port-admission(ownership assertions now use the shared owner; tests that duplicatedrunWithLocalStateOwner's own suite were removed).E2E
Isolated scratch Gateway per case (one at a time, own state dir and port), real agent turns, two OAuth sign-ins: a stand-in OAuth provider (local mock token endpoint) and a real hosted OAuth provider.
main= 8ebe740 build,fix= PR build (a1045f3; later commits change only the redirected-scope lookup and tests). "Writer" = the process that stored a refreshed credential (from the credential row's writer record). "upvalid" = token valid for ~4.5 min; "upexpired" = token expired, Gateway up; "down" = no Gateway. Each up case runs 8–10 Gateway turns around the command.infer model runup valid / expiredinfer image describeup validinfer image describeup expiredNo API key foundinfer audio transcribeup validNo credentials found for profile)infer audio transcribeup expiredinfer tts convertup validNo credentials found for profile)infer tts convertup expiredinfer model auth loginupmodels auth login(refuses a non-Gateway method)infer model auth logoutupmodels authrefusal, nothing removedagent --local+models status --probeuprunWithLocalStateOwnerrefusalagent --local, probe)Summary: on
main, the CLI is a second credential writer beside a running Gateway in audio, tts and login, and the next Gateway turn fails withNo credentials found for profile. On this branch the Gateway is the only writer whenever it runs, and no Gateway turn fails. Offline behavior is unchanged.Notes:
infer audio transcribewith an expired, Gateway-owned sign-in reportsNo audio transcription provider is configured or readyinstead of the ownership message, because the sharedhasAvailableAuthForProviderprobe maps every auth error to "not available". Nothing is written. Follow-up, not in this PR (the probe has six other callers).auth-triggered model runtime refresh failed: Plugin invocation scope is closedshows in Gateway logs on bothmainand this branch. It is not caused by this change and did not fail any turn.Data compatibility: no schema, stored credential shape, config or protocol change. The same stored sign-ins (copied from a real install) load and work before and after.