Skip to content

fix(auth): automatic reauthorization loses command context dependencies #1277

Closed

Description

@TheAngryPit

When a stored OAuth refresh token returns invalid_grant during an interactive API command, accepting automatic reauthorization can fail before the browser opens:

reauth: keychain access: resolve keyring backend: command runtime is required

Verified in v0.47.1 and the callback on current main 82e7fd6. The reauth closure in internal/cmd/root.go passes the HTTP request context into command callbacks. That context need not contain the command runtime or the authclient credential/store/resolver bindings installed on the CLI context. Restoring only the runtime advances the failure to read OAuth client credentials: credentials reader is required.

Reproduction: with an expired/revoked stored OAuth account and an interactive terminal, run an allowed API read such as gog --account you@example.invalid gmail labels get INBOX --json, then accept “Re-authorize now?”. Expected: the existing client/grant reaches browser authorization and retries the read after successful consent. Actual: the callback can fail with missing command dependencies before authorization. A synthetic regression exercises the same callback with a bare request context, fake credentials, and an offline pre-browser guard; it needs no real Google account.

Proposed repair: bind the original command runtime and authclient operations to the incoming request context, preserving its cancellation and values. Keep the stored grant, confirmation, identity validation and token persistence behavior.

Related: #974 introduced native recovery; #1196 concerns doctor recovery hints, and #1260 concerns Ads/default-service passkey prompts. This is a separate callback-dependency failure and does not change service defaults or authentication policy.

Activity

  1. added
    clawsweeper:intake:bugPreliminary submission classification; full source review pending.
    P2Normal priority bug or improvement with limited blast radius.
    clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.
    clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.
    clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.
    impact:auth-providerThis issue is about auth, provider routing, model choice, or SecretRef resolution.
    issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.
    and removed
    clawsweeper:intake:bugPreliminary submission classification; full source review pending.
    on Oct 30, 2026
  2. clawsweeper commented on Oct 30, 2026

    @clawsweeper
    Contributor

    Codex review: this still needs some work.

    Summary
    Current main still contains the callback dependency defect, and the same-author open fix PR explicitly closes this issue.

    Reproducibility: yes. the Gmail label prefetch omits the command context, the transport forwards the request context, and the recovery callback requires missing runtime and credential bindings; no live reproduction was run.

    Next step
    Review #1278 for dependency restoration and a failing-before regression, keeping this issue open until that fix is resolved.

    Security
    None.

    Review details

    Best possible solution:

    Restore command dependencies on the incoming request context while retaining cancellation, recorded grants, confirmation, identity validation, and token persistence ownership.

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

    Yes: the Gmail label prefetch omits the command context, the transport forwards the request context, and the recovery callback requires missing runtime and credential bindings; no live reproduction was run.

    Is this the best way to solve the issue?

    Binding the original dependencies onto the incoming context is a focused repair; replacing that context wholesale would lose cancellation and request values.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The reported live recovery and synthetic test results were not independently executed during this read-only review.

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

    Label changes

    Label changes:

    • add P2: Automatic recovery fails for expired or revoked stored OAuth grants, with manual authorization available as a workaround.
    • add impact:auth-provider: Missing command dependencies prevent Google OAuth reauthorization before browser consent.
    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.
    • add clawsweeper:linked-pr-open: Current issue advisory state selects this label.
    • add clawsweeper:no-new-fix-pr: Current issue advisory state selects this label.

    Label justifications:

    • P2: Automatic recovery fails for expired or revoked stored OAuth grants, with manual authorization available as a workaround.
    • impact:auth-provider: Missing command dependencies prevent Google OAuth reauthorization before browser consent.
    Evidence reviewed

    What I checked:

    • Current callback omits dependency binding: At verified default-branch HEAD, reauthFn forwards its incoming context directly to googleauth.Reauth; runtime and authclient bindings are installed separately on the command context. (internal/cmd/root.go:298, 82e7fd6005d4)
    • Concrete request path: RetryTransport invokes reauthorization with req.Context(); Gmail label lookup makes an initial Labels.List call without attaching the command context, providing a concrete route to the missing bindings. (internal/cmd/gmail_labels.go:390, 82e7fd6005d4)
    • Source-established failure: After confirmation, Reauth calls ensureKeychainAccessIfNeeded; resolveKeyringBackendInfo returns command runtime is required when the request context lacks app.Runtime. OAuth credential reading independently requires its context binding. (internal/cmd/auth.go:93, 82e7fd6005d4)
    • Open fix owns the work: fix(auth): retain command dependencies during automatic reauthorization #1278 is open, unmerged, authored by the reporter, and explicitly closes this issue; its patch restores runtime and authclient bindings and adds a synthetic regression. (8f746b8728c5)
    • Feature history and safeguards: The merged recovery feature feat(auth): recover revoked OAuth tokens safely #974 added this callback shape and records inamiy's contribution; its stated boundaries include explicit confirmation, grant preservation, identity validation, persistence before token replacement, and noninteractive suppression. (internal/cmd/root.go:298, 229da12809c1)
    • Existing coverage misses command dependency boundary: The existing persistence/reset test supplies a replacement Reauth callback, so it does not exercise root.go's command callbacks with a bare request context. (internal/googleapi/reauth_glue_test.go:36, 82e7fd6005d4)
    How this review workflow works
    • 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; those execution lanes are retired.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

    Reviewed October 30, 2026, 3:06 PM ET / 19:06 UTC.

  3. added a commit that references this issue on Nov 5, 2026
    a35d628
  4. steipete commented on Nov 5, 2026

    @steipete
    Collaborator

    Landed in #1278 as ebe74f2. The automatic reauthorization callback restores the command runtime and OAuth client dependencies on the incoming context, preserving cancellation, the selected client, and stored services/scopes.

    The synthetic regression reproduces the old missing-runtime failure and verifies the restored dependencies and grant. Existing transport tests prove retry with the refreshed token. Full make ci, independent P0–P2 review, and all exact-head CI checks passed: https://github.com/openclaw/gogcli/actions/runs/42082522973. No live consent run happened. Thanks @TheAngryPit; contributor credit is retained in the squash commit.

  5. added a commit that references this issue on Nov 5, 2026
    ebe74f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal priority bug or improvement with limited blast radius.clawsweeper:linked-pr-openClawSweeper found an open linked pull request for this issue.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:auth-providerThis issue is about auth, provider routing, model choice, or SecretRef resolution.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions