Skip to content

fix(secrets): publish the durable auth order on a running gateway refresh - #135847

Merged
obviyus merged 2 commits into
openclaw:mainfrom
yetval:fix/auth-refresh-durable-order
Sep 2, 2026
Merged

obviyus merged 2 commits into
openclaw:mainfrom
yetval:fix/auth-refresh-durable-order

Conversation

@yetval

@yetval yetval commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

openclaw models auth order set persists the requested order and asks a running Gateway to refresh through models.authStatus { refresh: true }. Before this fix, the refresh could report success while the Gateway kept routing with its old in-memory order until restart.

Fixes #135734.

Why This Change Was Made

Root cause

mergeLiveAuthStoreBookkeeping always preferred same-owner live bookkeeping during activation. A separate CLI process cannot update the Gateway's process-local snapshot, so this rule replaced the freshly loaded SQLite order with the stale live order.

The faulty merge came from #105289. #119946 later connected auth-order writes to the refresh path and made the stale merge user-visible on this command.

Fix

Prepared secrets snapshots now record the process-local auth snapshot generation before reading auth stores. Activation publishes the prepared order, last-good pointer, usage data, and inheritance state when that exact store did not change after preparation. A later in-process mutation advances the store generation and still wins.

The generation receipt is required for every internal snapshot. There is no fallback to the stale merge behavior. Rollback uses the same generation rule, so it retains durable bookkeeping loaded by the candidate and any newer live mutation.

User Impact

Operators can change provider profile priority while the Gateway is running. A successful refresh now makes routing use the durable order without a restart, while a real concurrent in-process update still wins.

Evidence

  • Current-main red at 07cac7a3b6644e3dadef56cf3f4dc08372e330d9: a real separate CLI changed durable order from a,b to b,a; both the CLI-triggered refresh and an explicit real Gateway refresh succeeded; live order stayed a,b.
  • Exact-head green at 4ff38d7dc8da7d8e5e753ca59fcfac2bbfe194f3: the same real CLI and running-Gateway path changed durable and live order to b,a; a second explicit refresh kept them aligned.
  • Regression test: fails on current main for the stale live-order mismatch and passes on the repaired head.
  • node scripts/run-vitest.mjs src/secrets/runtime-state.test.ts: 63 passed.
  • All 13 changed test files: 579 passed.
  • Owner and sibling refresh tests: 141 passed.
  • Targeted Oxlint, Oxfmt, conflict-marker, max-lines, assertion-safety, test-temp, coercion, deprecated-API, and import-cycle checks passed.
  • Local typecheck and build were not run because this host forbids them; exact-head CI owns those checks.

Test audit

  • The regression protects prepared bookkeeping when live state did not change.
  • Existing concurrency coverage now checks order and last-good state as well as usage data.
  • Rollback coverage proves that candidate-loaded order survives a rejected config activation.
  • Tests use the production activation boundary and add no test-only production seam.

Risk

Production grows by 37 net lines. The added state is one lifecycle generation receipt required to distinguish stale live state from a real concurrent live mutation. It does not add configuration, schema, protocol, or fallback behavior.

…resh

A successful models.authStatus refresh loaded the freshly persisted auth
order from SQLite, but activation replaced the candidate's order, lastGood,
usageStats and inheritance flag with the gateway's own live snapshot. When
the write came from a separate CLI process, that live snapshot was exactly
the stale pre-write order, so routing kept the old order until restart.

Prepared snapshots now record the runtime auth snapshot generation observed
before their auth stores were loaded. Activation keeps live bookkeeping only
for stores that changed after that point, so genuinely concurrent in-process
mutations still win while a fresh durable candidate replaces the stale live
order.

Fixes openclaw#135734
@yetval
yetval requested a review from a team as a code owner September 2, 2026 03:11
@clawsweeper

clawsweeper Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Sep 2, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 2, 2026
@clawsweeper

clawsweeper Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 1, 2026, 11:17 PM ET / September 2, 2026, 03:17 UTC.

ClawSweeper review

What this changes

The PR changes auth-runtime snapshot activation so a running Gateway adopts an externally persisted provider-order change unless newer in-process bookkeeping exists.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep this focused fix open: current main still overwrites the freshly loaded durable auth order with stale live bookkeeping, and the introduced generation guard repairs that owner-boundary failure while retaining post-preparation live mutations.

Priority: P1
Reviewed head: 38e1f7b26157c9b24cd7d2fee271ccf00f8da670

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary fix with credible real before/after evidence and no identified correctness finding.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.
Evidence reviewed 7 items Current-main failure boundary: Current main unconditionally selects the same-owner live store during activation, then copies its order, last-good pointer, usage stats, and inheritance state over the disk-loaded candidate. This permits a stale process-local order to replace the durable order.
Introduced repair: The PR records the snapshot generation observed before loading auth stores and preserves the candidate only when the corresponding live store has not advanced since then; otherwise the existing live-bookkeeping merge remains in effect.
Gateway entry path: The Gateway handler awaits the provider-auth runtime refresh when refresh is requested, and the runtime prepares then compare-activates the candidate snapshot through the changed merge owner.
Findings None None.
Security None None.

How this fits together

A CLI auth-order write persists provider selection order in the SQLite auth store, then asks the running Gateway to refresh its in-memory secrets runtime. The snapshot activation layer decides whether durable state or newer live bookkeeping becomes the next runtime state.

flowchart LR
  A[CLI auth-order write] --> B[SQLite auth store]
  B --> C[Gateway refresh request]
  C --> D[Prepare auth snapshot]
  D --> E[Activation merge]
  E --> F[Live provider routing]
Loading

Before merge

  • Resolve merge risk (P1) - Production code grows by 35 lines to carry a preparation watermark through snapshot construction, cloning, activation, restore, and the fast path; that growth appears necessary to preserve the lifecycle invariant rather than adding a parallel refresh path.
Agent review details

Security

None.

PR surface

Source +34, Tests +49. Total +83 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 4 35 1 +34
Tests 1 49 0 +49
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 84 1 +83

Review metrics

Metric Value Why it matters
Production versus test LOC production +35/-1; tests +49/-0 The positive production footprint threads one lifecycle watermark through all existing snapshot construction and restoration boundaries; the test additions cover both durable and concurrent-state outcomes.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #135734
Summary: This PR is the candidate fix for the reported running-Gateway durable-order publication defect.

Members:

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

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Retain the activation-boundary generation check and merge after exact-head validation; the watermark is the smallest shared fact that distinguishes a stale live snapshot from a genuinely concurrent in-process update without changing configuration or SQLite schemas.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Retain the activation-boundary generation check and merge after exact-head validation; the watermark is the smallest shared fact that distinguishes a stale live snapshot from a genuinely concurrent in-process update without changing configuration or SQLite schemas.

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

Yes, source provides a high-confidence reproduction path: persist an order from another process, request the Gateway refresh, and observe that current-main activation copies the old live order over the newly loaded durable candidate.

Is this the best way to solve the issue?

Yes. The fault is at the single snapshot activation merge shared by Gateway-driven refreshes, and the generation guard preserves the established concurrent-mutation behavior instead of adding a CLI- or protocol-specific workaround.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 77a24659740f.

Labels

Label changes:

  • add P1: A successful auth-order update can leave a running Gateway routing with the prior provider profile until restart, despite durable state showing the requested order.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.

Label justifications:

  • P1: A successful auth-order update can leave a running Gateway routing with the prior provider profile until restart, despite durable state showing the requested order.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.
  • proof: sufficient: Contributor real behavior proof is sufficient. The supplied terminal trace uses a real SQLite auth store, a separate child CLI write, and the production snapshot refresh path; it shows current main retaining the old live order and this head publishing the durable replacement. WebSocket dispatch and an outbound provider request were not independently exercised, but the inspected Gateway handler calls the same refresh owner.

Evidence

What I checked:

  • Current-main failure boundary: Current main unconditionally selects the same-owner live store during activation, then copies its order, last-good pointer, usage stats, and inheritance state over the disk-loaded candidate. This permits a stale process-local order to replace the durable order. (src/secrets/runtime-state.ts:270, 77a24659740f)
  • Introduced repair: The PR records the snapshot generation observed before loading auth stores and preserves the candidate only when the corresponding live store has not advanced since then; otherwise the existing live-bookkeeping merge remains in effect. (src/secrets/runtime-state.ts:275, 38e1f7b26157)
  • Gateway entry path: The Gateway handler awaits the provider-auth runtime refresh when refresh is requested, and the runtime prepares then compare-activates the candidate snapshot through the changed merge owner. (src/gateway/server-methods/models-auth-status.ts:548, 38e1f7b26157)
  • Revision contract: The existing runtime snapshot owner advances per-store revisions whenever a changed entry is published, so a matching store changed after candidate preparation compares greater than the captured global watermark. (src/agents/auth-profiles/runtime-snapshots.ts:107, 38e1f7b26157)
  • Regression coverage: The new tests cover both the stale durable-order publication and a live bookkeeping mutation after candidate preparation; the earlier adjacent test already covers a post-preparation usage-state update winning. (src/secrets/runtime-state.test.ts:494, 38e1f7b26157)
  • PR provenance: Raw commit metadata confirms this one-parent PR commit is based on the verified merge base, and the inspected parent lacks the generation guard added by this PR. (src/secrets/runtime-state.ts:275, 38e1f7b26157)

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Ayaan Zaidi: 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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime commands Command implementations triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Sep 2, 2026
@obviyus

obviyus commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Land-ready at 4ff38d7dc8da7d8e5e753ca59fcfac2bbfe194f3.

Proof

  • Current main 07cac7a3b6644e3dadef56cf3f4dc08372e330d9: a separate real CLI changed durable order from a,b to b,a; the CLI-triggered refresh and a second real models.authStatus request succeeded; Gateway memory stayed a,b.
  • Exact head: the same path changed both durable and live order to b,a; the second refresh kept them aligned.
  • Concurrent live bookkeeping still wins when it changes after candidate preparation.
  • Rollback keeps the order captured by the durable candidate.

Validation

  • node scripts/run-vitest.mjs src/secrets/runtime-state.test.ts — 63 passed.
  • Changed test files — 579 passed.
  • Provider-refresh owner and sibling files — 141 passed.
  • Targeted Oxlint, Oxfmt, conflict-marker, max-lines, assertion-safety, test-temp, coercion, deprecated-API, and import-cycle checks passed.
  • CI run 33599950154 is green on the exact head.
  • Exact-head Autoreview P0 passed with no findings.

Production LOC: +38/-1 (net +37) | Tests: +78/-8 (net +70).

ClawSweeper rank-up move: retained the activation generation receipt and made it required across every internal producer. The production growth is the owner receipt that distinguishes stale live state from a real concurrent mutation; no configuration, schema, protocol, or fallback path was added.

Proof gap: synthetic credentials were used, so no external provider request was sent. The real CLI, WebSocket Gateway method, SQLite store, activation owner, and shutdown path were exercised.

@obviyus
obviyus merged commit c505a0b into openclaw:main Sep 2, 2026
301 of 302 checks passed
@obviyus

obviyus commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks @yetval!

Proof: A separate real CLI changed the SQLite profile order while a real Gateway was running. Current main kept the old live order after two successful refresh requests. The landed head published the new order on the first refresh and retained it on the second.

  • Bug: A successful auth refresh could overwrite a newly loaded durable order with stale Gateway bookkeeping until restart.
  • Fix: Activation now uses a preparation generation receipt. The durable candidate wins when live state did not change, while a later in-process mutation still wins.
Before After
Durable b,a; live a,b after refresh Durable and live b,a without restart

Landed: c505a0b

Source: 4ff38d7

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 gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

models auth order set refresh preserves stale live order until Gateway restart

2 participants