Skip to content

refactor(gateway): carry authoritative read facts - #125145

Merged
steipete merged 3 commits into
mainfrom
steipete/w3-gateway-fact-carrying-cleanups
Aug 17, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/w3-gateway-fact-carrying-cleanups

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Removes three families of read-path rediscovery that had drifted away from their authoritative producers:

  • devices re-read node.list and joined node/device records by device ID or IP even though device.pair.list already carries approved and pending node surfaces.
  • one-shot Gateway calls loaded and validated stored device auth, then discarded it and made the client read the same SQLite row again during connect.
  • eleven shared-state readers checked filesystem existence themselves before entering the canonical read-only database lifecycle, duplicating missing/error handling and creating check/open races.

Why This Change Was Made

Facts now cross the boundary that produced them. Device approval notices use nodeSurface/pendingNodeSurface from the pairing record; Gateway calls pass their prepared auth entry into the client host deps; writable and read-only device-auth loaders share one Kysely query owner; and all eleven existing-state readers use withExistingOpenClawStateDatabaseReadOnly for reuse, existence, forward-schema, corruption, and close handling.

The migrated readers are MCP OAuth, sandbox registry, Doctor incognito repair journal, fleet registry, startup migration lease, both installed-plugin index readers, config machine state, onboarding recommendations, registered agent databases, and TUI last-session state.

No protocol, config, SQLite schema/version, migration, dependency, or compatibility surface changes.

User Impact

Device/node approval guidance no longer spends extra Gateway round trips or depends on inferred ID/IP joins. Read-only Gateway calls reuse the exact stored credential they already validated. CLI and metadata reads consistently avoid creating writable state while preserving missing-database fallbacks and non-missing filesystem failures.

Production LOC: +322/-404 (net -82) | Tests: +87/-118 (net -31).

Evidence

  • Pre-fix devices regression: devices-cli.test.ts observed 5/2 Gateway calls where the recorded-fact contract requires 3/1; 2 tests failed for the intended reason.
  • Pre-fix prepared-auth regression: the call options dropped preparedDeviceAuth, and the client connect frame had no prepared token; 2 tests failed for the intended reason.
  • Post-rebase focused proof: 15 test files across 8 routed Vitest shards, 399 tests passed (devices, Gateway call/client, device-auth store, and every reader owner).
  • Post-rebase changed gate: OPENCLAW_TESTBOX=1 node scripts/check-changed.mjs succeeded on Blacksmith Testbox run https://github.com/openclaw/openclaw/actions/runs/32004752402. Core/core-test typechecks, three changed-file lint stripes, dead exports, import cycles, database-first, schema, sidecar, pairing, formatting, and selected guards passed.
  • P1-depth structured autoreview on the exact branch diff: clean; no accepted/actionable findings.

Best-fix verdict: best. Each cleanup removes consumer inference or duplicate lifecycle/query ownership instead of adding a cache, fallback, or compatibility path. Missing-database behavior remains at the canonical helper, and stored credentials remain owned by the one-shot call that selected and validated them.

@clawsweeper

clawsweeper Bot commented Aug 17, 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 gateway Gateway runtime cli CLI command changes commands Command implementations agents Agent runtime and tooling fleet size: L maintainer Maintainer-authored PR labels Aug 17, 2026
@steipete
steipete marked this pull request as ready for review August 17, 2026 07:27
@steipete
steipete requested a review from a team as a code owner August 17, 2026 07:27
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Aug 17, 2026
@clawsweeper

clawsweeper Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 17, 2026, 7:55 AM ET / 11:55 UTC.

ClawSweeper review

What this changes

The PR removes repeated Gateway and CLI state reads by carrying validated pairing and device-auth facts forward and consolidating read-only SQLite access.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open: this active maintainer PR is not on current main, and the prior credential-rotation blocker is addressed by conditional writes and deletes at the durable auth owner.

Priority: P1
Reviewed head: fc88f5f4639ce2515e274905c9a510b45bc08a2a

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The owner-boundary repair and focused regressions address the prior blocker; exact-head CI remains the normal landing gate.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored PR is exempt from the external contributor proof gate; its comment supplies focused regression and changed-gate evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored PR is exempt from the external contributor proof gate; its comment supplies focused regression and changed-gate evidence.
Evidence reviewed 7 items Prepared-auth owner boundary: The Gateway wrapper uses the already validated credential for the connection and applies its token as a fence to all later durable store and clear callbacks.
Atomic durable fence: Both device-scoped and origin-scoped writes update only a row whose token matches the prepared snapshot; deletes use the same condition.
Regression coverage: The client test covers stale clear and overwrite for local and origin credentials, and the SQLite-owner test verifies a rotated credential remains current.
Findings None None.
Security None None.

How this fits together

Gateway CLI calls select pairing and stored device credentials before constructing the shared Gateway client; authentication outcomes can then update the same durable credential record. Separate status and metadata readers use the shared read-only SQLite lifecycle without creating state.

flowchart LR
  A[CLI and status commands] --> B[Gateway call and pairing list]
  B --> C[Validated device and node facts]
  C --> D[Gateway client]
  D --> E[Gateway authentication result]
  E --> F[Conditional credential store]
  A --> G[Read-only state consumers]
  G --> H[Existing SQLite read helper]
Loading

Before merge

  • Resolve merge risk (P1) - This changes durable device-auth mutation for both local and origin scopes; exact-head CI should retain the conditional-store and conditional-delete regressions before merge.
  • Resolve merge risk (P1) - A full historical diff was limited by this partial checkout's failed DNS fetch, although direct current-main and PR-head source inspection completed.
  • Complete next step (P2) - No repair job is appropriate: the prior P1 is addressed on this active maintainer branch, leaving ordinary exact-head validation and landing judgment.
Agent review details

Security

None.

PR surface

Source -12, Tests +103. Total +91 across 19 files.

View PR surface stats
Area Files Added Removed Net
Source 15 455 467 -12
Tests 4 221 118 +103
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 19 676 585 +91

Review metrics

Metric Value Why it matters
Production versus test LOC production +455/-467 (net -12); tests +221/-118 (net +103) The refactor is production-net-negative while adding focused regression coverage for credential rotation.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/commands/doctor-session-incognito-key-repair-state.ts, serialized state: src/commands/doctor-session-incognito-key-repair-state.ts, serialized state: src/state/config-machine-state.ts, serialized state: src/state/onboarding-recommendations.ts, serialized state: src/state/openclaw-agent-db-registry-listing.ts, unknown-data-model-change: src/state/onboarding-recommendations.ts, and 1 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Land with the rotation fence (recommended)
    Merge after exact-head checks retain the local- and origin-scoped conditional credential regression coverage.

Technical review

Best possible solution:

Land the canonical fact-carrying flow after exact-head validation, retaining conditional token mutations for both durable credential scopes.

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

Yes: the focused A→B credential-rotation interleaving is directly represented in the client and SQLite-owner regressions, though this read-only review did not execute them.

Is this the best way to solve the issue?

Yes: fencing mutations at the SQLite owner is preferable to a client-side retry or cleanup guard because it preserves the credential selected for the request without allowing it to alter a newer row.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This maintainer-authored PR is exempt from the external contributor proof gate; its comment supplies focused regression and changed-gate evidence.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P1: A stale Gateway request could otherwise clear or overwrite a newly rotated device credential.
  • merge-risk: 🚨 auth-provider: The PR changes persistence behavior for local and origin-bound Gateway device-auth credentials.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This maintainer-authored PR is exempt from the external contributor proof gate; its comment supplies focused regression and changed-gate evidence.

Evidence

What I checked:

  • Prepared-auth owner boundary: The Gateway wrapper uses the already validated credential for the connection and applies its token as a fence to all later durable store and clear callbacks. (src/gateway/client.ts:64, fc88f5f4639c)
  • Atomic durable fence: Both device-scoped and origin-scoped writes update only a row whose token matches the prepared snapshot; deletes use the same condition. (src/infra/device-auth-store.ts:196, fc88f5f4639c)
  • Regression coverage: The client test covers stale clear and overwrite for local and origin credentials, and the SQLite-owner test verifies a rotated credential remains current. (src/gateway/client.test.ts:1991, fc88f5f4639c)
  • Call-path propagation: The one-shot Gateway call loads and scope-validates stored auth before passing that exact entry to the client. (src/gateway/call.ts:1168, fc88f5f4639c)
  • Current-main status: The PR head is not an ancestor of current main, so the central cleanup and rotation fence remain unmerged. (e6616fdc4c4c)
  • Feature history: The follow-up fence commit is attributable to the recent Gateway/device-auth contributor and directly follows this PR's refactor commit. (src/gateway/client.ts:64, fc88f5f4639c)

Likely related people:

  • steipete: Recent current-main history and both commits on this PR are authored by Peter Steinberger, mapped by the PR metadata to this login. (role: recent Gateway and device-auth contributor; confidence: high; commits: fc88f5f4639c, 016237e779dc, 2d3aaaa0d593; files: src/gateway/client.ts, src/infra/device-auth-store.ts, src/gateway/call.ts)

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.

History

Review history (4 earlier review cycles)
  • reviewed 2026-08-17T07:42:53.444Z sha 90238db :: needs maintainer review before merge. :: none
  • reviewed 2026-08-17T10:51:49.742Z sha 541c8c11d48462c5771e8c74f585cbfc30e065b3 :: found issues before merge. :: [P1] Refresh prepared auth before clearing a rotated token
  • reviewed 2026-08-17T11:18:52.773Z sha cc00f763450622ac7be3d14dd176a9edded55700 :: found issues before merge. :: [P1] Fence prepared auth before clearing a rotated token
  • reviewed 2026-08-17T11:38:01.839Z sha 016237e :: needs changes before merge. :: [P1] Fence cleanup to the prepared credential

@steipete steipete self-assigned this Aug 17, 2026
@steipete
steipete force-pushed the steipete/w3-gateway-fact-carrying-cleanups branch from 90238db to 541c8c1 Compare August 17, 2026 10:44
@clawsweeper clawsweeper Bot added merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Aug 17, 2026
@steipete
steipete force-pushed the steipete/w3-gateway-fact-carrying-cleanups branch from 541c8c1 to cc00f76 Compare August 17, 2026 11:12
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Aug 17, 2026
@steipete
steipete force-pushed the steipete/w3-gateway-fact-carrying-cleanups branch from cc00f76 to 016237e Compare August 17, 2026 11:25
@steipete

Copy link
Copy Markdown
Contributor Author

P1 repaired at fc88f5f.

The durable device-auth owner now treats the prepared token as a compare-and-swap fence. Prepared credentials remain immutable request input; later hello stores use conditional UPDATE and mismatch cleanup uses conditional DELETE against the original token. If local or origin-scoped durable auth has rotated or disappeared, the stale request changes zero rows and cannot upsert, clear, or overwrite the current credential. Ordinary unprepared clients retain their existing upsert/clear behavior.

Regression proof reproduces the A→B interleaving at both boundaries: four Gateway client cases cover local/origin × stale clear/overwrite, and the SQLite owner test rotates A to B before stale conditional store and clear attempts, then verifies B remains byte-for-byte current. Both regressions failed on the prior head for the intended reason.

Validation on the pushed head:

  • node scripts/run-vitest.mjs src/gateway/client.test.ts src/gateway/call.test.ts src/infra/device-auth-store.test.ts src/infra/state-migrations.device-auth.test.ts src/gateway/probe.auth.integration.test.ts src/gateway/probe.device-auth-scope.test.ts src/gateway/startup-local-cli-pairing.test.ts src/gateway/device-auth.test.ts src/commands/doctor-device-pairing.test.ts packages/gateway-client/src/browser-device-auth.test.ts — 10 files, 284 tests passed across 4 routed suites.
  • node scripts/check-changed.mjs — passed all selected core/core-test typecheck, formatting, lint, architecture, SQLite, auth, and guard lanes.
  • P1-depth structured autoreview — clean, no accepted/actionable findings.

Current PR surface: production +455/-467 (net -12); tests +221/-118 (net +103). No protocol, config, schema-version, migration, dependency, or compatibility surface changed. Behind-main drift remains advisory while the PR is mergeable; the repo-native prepare chain will enforce the final guarded base and exact-head proof before merge.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added 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. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 17, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready verification for 5731416cdeabfb84a0ae95f869f0f4b3d240e0fe:

  • Durable-store CAS fencing covers prepared-auth store and clear for both local and origin-scoped credentials; stale requests cannot overwrite, delete, or recreate a rotated row.
  • The A→B regression failed on the old implementation for local/origin clear and overwrite, then passed after the owner repair. Focused auth/device proof: 10 files, 284 tests across 4 routed suites.
  • Branch-wide node scripts/check-changed.mjs passed. The exact merge-ref CI failure in check-lint-core-5 was repaired by extracting the self-contained session prompt-report type; the exact failed stripe and its focused changed gate passed locally.
  • Two P1-depth structured autoreviews completed cleanly with no accepted/actionable findings.
  • ClawSweeper: no actionable findings; durable review recommends landing the SQLite-owner fence after exact-head validation.
  • Exact-head CI: https://github.com/openclaw/openclaw/actions/runs/32027837383 is green, including openclaw/ci-gate.
  • Repo-native review-init, review artifact initialization/validation, and OPENCLAW_TESTBOX=1 scripts/pr prepare-run 125145 completed successfully with the same PR head.

No proof gaps remain. No protocol, config, SQLite schema/version, migration, dependency, docs, or compatibility surface changed.

@steipete
steipete merged commit 4eb6aa3 into main Aug 17, 2026
215 checks passed
@steipete
steipete deleted the steipete/w3-gateway-fact-carrying-cleanups branch August 17, 2026 12:13
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 18, 2026
* refactor(gateway): carry authoritative read facts

* fix(gateway): fence prepared device auth rotations

* fix(ci): split oversized session types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling cli CLI command changes commands Command implementations fleet gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XL 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.

1 participant