Skip to content

fix(auth): let agent exec reuse shared OAuth - #143744

Closed
sergeseyfetdinov wants to merge 2 commits into
openclaw:mainfrom
SenamTech:fix/agent-exec-oauth
Closed

sergeseyfetdinov wants to merge 2 commits into
openclaw:mainfrom
SenamTech:fix/agent-exec-oauth

Conversation

@sergeseyfetdinov

Copy link
Copy Markdown

Related: #124768

What Problem This Solves

Fixes an issue where users running openclaw agent exec with a stored shared OAuth login would see the selected profile as unavailable after the command switched into temporary run state.

Why This Change Was Made

The bounded exec scope now exposes shared OAuth as an owner-tagged in-memory read-through alongside portable static credentials. Inherited credentials are never written into the temporary agent store, while OAuth refresh locking and rotated-token persistence remain on the canonical shared SQLite owner. --auth-env-only remains a complete stored-auth opt-out.

User Impact

Users can run isolated agent exec turns with their existing shared OAuth login without copying credentials or reauthenticating, and without creating a second refresh-token owner.

Evidence

  • Focused Vitest: 50 tests passed across agent-exec.auth, auth publication ownership, and external OAuth ownership suites.
  • node scripts/check-changed.mjs --base upstream/main: passed all selected core, core-test, docs, formatting, lint, typecheck, dead-code, boundary, and policy gates on Node 24.20.0.
  • Live source-built agent exec against an existing stored OpenAI OAuth profile returned exactly SOURCE_AGENT_EXEC_OAUTH_OK using openai/gpt-5.6-sol with no fallback.
  • Regression coverage proves shared OAuth visibility, no temporary persisted credential copy, canonical-owner refresh persistence, local override behavior, unreadable-store fail-closed behavior, and unchanged --auth-env-only isolation.

@sergeseyfetdinov
sergeseyfetdinov requested a review from a team as a code owner September 10, 2026 05:51
@clawsweeper

clawsweeper Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation commands Command implementations agents Agent runtime and tooling size: S labels Sep 10, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 10, 2026
@clawsweeper

clawsweeper Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed September 10, 2026, 1:57 AM ET / 05:57 UTC.

ClawSweeper review

What this changes

Allows temporary agent exec runs to read shared OAuth credentials in memory and directs shared OAuth refresh writes to the original credential store.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⛔ Blocked before merge - 12 items remain

This addresses a current, independently reported OAuth gap, but refresh ownership is incomplete: local profiles can target the shared store, captured credentials can survive deletion, and refresh coordination still follows temporary state.

Priority: P2
Reviewed head: 5e886e11343f460dd81bd0903604b329ae5cb4ca

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The focused patch and reported live success are useful, but concrete refresh-ownership defects and missing authority proof block readiness.
Proof confidence 🦐 gold shrimp (3/6) Needs stronger real behavior proof before merge: Authority-chain proof required: the captured body reports a successful source-built OAuth agent exec response, which supports the allowed login path. It does not show canonical refresh under temporary state or rejection of a removed/reassigned profile before provider I/O; the new refresh coverage uses mocks and identical state roots. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🦪 silver shellfish (2/6) Security review found an item that needs attention.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: Authority-chain proof required: the captured body reports a successful source-built OAuth agent exec response, which supports the allowed login path. It does not show canonical refresh under temporary state or rejection of a removed/reassigned profile before provider I/O; the new refresh coverage uses mocks and identical state roots. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 12 items Current main still excludes shared OAuth: The fetched main implementation filters the bounded shared view to portable static credentials, so the central requested behavior remains missing.
Latest release has the same exclusion: The v2026.9.3 source also filters shared credentials to portable-static-credential; this is not already fixed in the supplied latest release.
Local ownership is collapsed inside the bounded scope: Both sides of isSharedMainAuthProfileAgentDir resolve to the scoped agent directory, causing resolvePersistedAuthProfileOwnerAgentDir to return undefined even for independent local OAuth. The newly added sharedStoreWrite flag interprets that result as the canonical shared destination.
Findings 3 actionable findings [P1] Preserve the real owner of agent-local OAuth refreshes
[P1] Exclude captured credentials from authoritative refresh updates
[P1] Carry the original state root through refresh coordination
Security Needs attention Deleted shared OAuth can regain refresh authority: A scope retains shared OAuth material, and the new shared-write path merges that material into authoritative reads when the durable profile is absent. This can recreate a deleted profile and perform provider refresh I/O after local removal.

How this fits together

Agent exec runs one agent turn with temporary session state while borrowing the operator’s configured credentials. The auth subsystem selects credentials and coordinates refreshes before supplying access tokens to the model runtime.

flowchart TD
  A[Agent exec request] --> B[Temporary run scope]
  C[Configured agent credentials] --> D[Credential selection]
  E[Shared OAuth store] --> D
  B --> D
  D --> F[Refresh ownership and locking]
  F --> E
  F --> G[Model runtime]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: Authority-chain proof required: the captured body reports a successful source-built OAuth agent exec response, which supports the allowed login path. It does not show canonical refresh under temporary state or rejection of a removed/reassigned profile before provider I/O; the new refresh coverage uses mocks and identical state roots. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Preserve the real owner of agent-local OAuth refreshes (P1) - Inside withAuthProfileStoreAgentDir, isSharedMainAuthProfileAgentDir compares the scoped agent directory with itself, so owner resolution returns undefined even for an independent local OAuth profile. This new flag now sends its claim to the shared database instead of the scoped local database. If the shared profile is absent or belongs to another account, the generation comparison fails and an expired local login cannot refresh. Resolve local versus shared ownership explicitly before applying this flag; the existing local-override test covers only an API key.
  • Exclude captured credentials from authoritative refresh updates (P1) - The newly enabled shared write still calls loadAuthProfileStoreForAgent, which merges the scope’s captured shared credentials into the database result. If another process removes an expired shared profile after exec enters its scope, that merge restores the old credential before the refresh claim compares generations. The claim can then persist a new fence, call the provider, and restore rotated credentials for the removed profile. Read the selected durable owner without read-through overlays for claim, settlement, and recovery checks; deletion must remain authoritative.
  • Carry the original state root through refresh coordination (P1) - Enabling shared OAuth here reaches refresh code after agent exec has changed OPENCLAW_STATE_DIR, but only database writes receive the captured original environment. claimOAuthRefresh still derives its owner path and profile lock from the temporary root, and fenceOAuthRefreshPeers discovers that root’s agent stores and registry. Historical peers in the original installation are consequently omitted from generation fencing, leaving copied refresh material behind after rotation. Carry the original owner environment through locks and peer discovery as well as writes. The added refresh test keeps both roots identical and cannot expose this.
  • Resolve security concern: Deleted shared OAuth can regain refresh authority - A scope retains shared OAuth material, and the new shared-write path merges that material into authoritative reads when the durable profile is absent. This can recreate a deleted profile and perform provider refresh I/O after local removal.
  • Resolve merge risk (P1) - Existing agent-local OAuth setups can lose refresh functionality after this change; the supplied local-override coverage exercises only static credentials.
  • Resolve merge risk (P1) - Safe rotation for upgraded installations with historical OAuth peers remains unproven under an actual temporary state root.
  • Resolve merge risk (P1) - The supplied live success output does not establish rejection of removed or reassigned credentials before refresh I/O.
  • Complete next step (P2) - Resolve the three refresh findings with secops owner involvement, then provide redacted after-fix refresh and revocation evidence before merge. Terminal output or logs are suitable; remove credentials, private endpoints, IP addresses, and other personal data.
  • Improve patch quality - Repair local/shared owner selection and remove captured overlays from authoritative refresh reads.
  • Improve patch quality - Use the original state root for locks and historical-peer fencing, with coverage that actually switches to temporary state.
  • Improve patch quality - Add redacted production-path evidence for shared and local refresh plus removed/reassigned credential rejection before I/O; update the PR body to trigger re-review, or ask a maintainer for @clawsweeper re-review.

Findings

  • [P1] Preserve the real owner of agent-local OAuth refreshes — src/agents/auth-profiles/oauth-manager.ts:859-862
  • [P1] Exclude captured credentials from authoritative refresh updates — src/agents/auth-profiles/store.ts:1495-1500
  • [P1] Carry the original state root through refresh coordination — src/agents/auth-profiles/store.ts:207-212
  • [high] Deleted shared OAuth can regain refresh authority — src/agents/auth-profiles/store.ts:1498
Agent review details

Security

Needs attention: The authority review found that captured shared OAuth can override durable deletion during refresh; no dependency or supply-chain changes were introduced.

PR surface

Source +12, Tests +107, Docs 0. Total +119 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 2 15 3 +12
Tests 3 114 7 +107
Docs 1 1 1 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 130 11 +119

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #124768
Summary: This PR targets the shared-OAuth portion of the canonical report; explicit agent selection has a separate open implementation.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Complete canonical-owner handling (recommended)
    Repair owner selection and authoritative reads, carry the original root through refresh coordination, and demonstrate allowed and revoked credential behavior before merge.
  2. Pause shared OAuth enablement
    Keep the current exclusion until refresh ownership and revocation can be demonstrated safely.

Technical review

Best possible solution:

Carry one explicit credential owner through selection, authoritative reads, refresh locks, peer discovery, and persistence while preserving local overrides and the environment-only opt-out.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: current main removes shared OAuth before temporary exec resolves credentials, matching the v2026.9.3 report. The introduced refresh defects are source-traceable; no contributor code or tests were executed.

Is this the best way to solve the issue?

No, not yet: borrowing credentials is appropriate, but changing only visibility and write routing leaves owner resolution, authoritative reads, and refresh coordination inconsistent.

Full review comments:

  • [P1] Preserve the real owner of agent-local OAuth refreshes — src/agents/auth-profiles/oauth-manager.ts:859-862
    Inside withAuthProfileStoreAgentDir, isSharedMainAuthProfileAgentDir compares the scoped agent directory with itself, so owner resolution returns undefined even for an independent local OAuth profile. This new flag now sends its claim to the shared database instead of the scoped local database. If the shared profile is absent or belongs to another account, the generation comparison fails and an expired local login cannot refresh. Resolve local versus shared ownership explicitly before applying this flag; the existing local-override test covers only an API key.
    Confidence: 0.98
  • [P1] Exclude captured credentials from authoritative refresh updates — src/agents/auth-profiles/store.ts:1495-1500
    The newly enabled shared write still calls loadAuthProfileStoreForAgent, which merges the scope’s captured shared credentials into the database result. If another process removes an expired shared profile after exec enters its scope, that merge restores the old credential before the refresh claim compares generations. The claim can then persist a new fence, call the provider, and restore rotated credentials for the removed profile. Read the selected durable owner without read-through overlays for claim, settlement, and recovery checks; deletion must remain authoritative.
    Confidence: 0.97
  • [P1] Carry the original state root through refresh coordination — src/agents/auth-profiles/store.ts:207-212
    Enabling shared OAuth here reaches refresh code after agent exec has changed OPENCLAW_STATE_DIR, but only database writes receive the captured original environment. claimOAuthRefresh still derives its owner path and profile lock from the temporary root, and fenceOAuthRefreshPeers discovers that root’s agent stores and registry. Historical peers in the original installation are consequently omitted from generation fencing, leaving copied refresh material behind after rotation. Carry the original owner environment through locks and peer discovery as well as writes. The added refresh test keeps both roots identical and cannot expose this.
    Confidence: 0.95

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against f2539206906a.

Labels

Label justifications:

  • P2: The current defect affects temporary OAuth-backed agent runs while the reported Gateway workflow remains usable.
  • merge-risk: 🚨 auth-provider: The introduced shared-write routing can break agent-local OAuth refresh and bypass original-root refresh coordination.
  • merge-risk: 🚨 security-boundary: A captured shared credential can be restored into an authoritative refresh update after its durable profile is deleted.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: Authority-chain proof required: the captured body reports a successful source-built OAuth agent exec response, which supports the allowed login path. It does not show canonical refresh under temporary state or rejection of a removed/reassigned profile before provider I/O; the new refresh coverage uses mocks and identical state roots. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

Security concerns:

  • [high] Deleted shared OAuth can regain refresh authority — src/agents/auth-profiles/store.ts:1498
    A scope retains shared OAuth material, and the new shared-write path merges that material into authoritative reads when the durable profile is absent. This can recreate a deleted profile and perform provider refresh I/O after local removal.
    Confidence: 0.97

What I checked:

  • Current main still excludes shared OAuth: The fetched main implementation filters the bounded shared view to portable static credentials, so the central requested behavior remains missing. (src/agents/auth-profiles/store.ts:196, f2539206906a)
  • Latest release has the same exclusion: The v2026.9.3 source also filters shared credentials to portable-static-credential; this is not already fixed in the supplied latest release. (src/agents/auth-profiles/store.ts:165, 1391f7cd2d40)
  • Local ownership is collapsed inside the bounded scope: Both sides of isSharedMainAuthProfileAgentDir resolve to the scoped agent directory, causing resolvePersistedAuthProfileOwnerAgentDir to return undefined even for independent local OAuth. The newly added sharedStoreWrite flag interprets that result as the canonical shared destination. (src/agents/auth-profiles/store.ts:649, 5e886e11343f)
  • Authoritative refresh updates merge captured credentials: The new shared write destination still loads through loadAuthProfileStoreForAgent, whose result includes applyScopedAuthReadThrough. A shared profile deleted after scope creation can therefore be reintroduced from the captured view before the refresh generation comparison and persisted again. (src/agents/auth-profiles/store.ts:1519, 5e886e11343f)
  • Refresh coordination retains the ambient temporary root: OAuth claims resolve authPath and the profile lock without the captured original environment. Peer discovery likewise defaults to process.env and scans that root’s agent directories and registry; agent exec has already switched this environment to temporary state. (src/agents/auth-profiles/oauth-manager.ts:674, 5e886e11343f)
  • Focused tests omit the critical refresh composition: The new refresh test uses the same root for ambient state and bounded scope. The agent-exec test switches roots but uses an unexpired token; the local override case uses an API key. These checks do not exercise expired local OAuth, deletion during a scope, or historical-peer fencing across temporary state. (src/agents/auth-profiles/oauth.external-owner.test.ts:74, 5e886e11343f)

Likely related people:

  • vincentkoc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

itsuzef added a commit to itsuzef/openclaw that referenced this pull request Sep 18, 2026
…orized

`openclaw agent exec` runs against a disposable state root, and the MCP OAuth
store, its pending-authorization rows, and the refresh lease all resolved
through that root. Every OAuth MCP server the operator had signed in to with
`openclaw mcp login` therefore failed at run start with
`requires OAuth authorization. Run openclaw mcp login <server>`, and following
that advice could not help because the login lands in the operator store the
run never reads.

MCP OAuth sessions are operator credentials, so they now follow the original
shared state root exactly like stored auth profiles do: `agent exec` pins the
MCP OAuth store to that root for the run, and reads, rotated-token writes, and
the cross-process refresh lease resolve there. The temporary state never holds
a credential copy and the run never becomes a second refresh owner.
`--auth-env-only` keeps disabling stored credential access, MCP sessions
included.

Related: openclaw#124768, openclaw#143744
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #clawtributors on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling commands Command implementations docs Improvements or additions to documentation merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S stale Marked as stale due to inactivity status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant