Skip to content

perf(auth): keep active database readers warm with idle expiry - #150524

Merged
steipete merged 4 commits into
mainfrom
codex/perf-auth-missing-reader-5152
Sep 17, 2026
Merged

steipete merged 4 commits into
mainfrom
codex/perf-auth-missing-reader-5152

Conversation

@steipete

@steipete steipete commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Related: #145579.

What Problem This Solves

Auth reads across many active agents repeatedly reopen SQLite databases after the eight-reader pool fills. A failed open for a missing database also evicts a useful reader before there is a replacement.

User Impact

Keep up to 64 auth readers available and close each connection after 30 minutes without use. Missing or unreadable candidates preserve existing readers. The measured 32-agent workload retains 24 additional file descriptors while warm; explicit cleanup releases them all. Reads still observe external committed changes and runtime snapshot invalidation. No configuration, schema, persisted data or credential policy changes.

Why This Change Was Made

Keep admission and native-handle custody in one pool owner. Validate a new reader before eviction, retain failed-close handles for cleanup, and block further uncached admission while an unadmitted handle cannot close. The bound is 64 admitted readers plus one transient or failed candidate.

Refresh each reader's existing idle timer on reuse. Timers are unreferenced and use the existing SQLite maintenance context so they do not retain a request context. A stale callback cannot close a replacement reader; failed idle closes retain custody, retry at the same interval, and preserve exit cleanup. Explicit closure still takes effect immediately when native close succeeds.

Evidence

  • Missing-store eviction reproduced on the original implementation. Four timer/cleanup regressions failed before the lifecycle fix. The focused auth/store/WAL suites passed 91 tests; the final seven-case reader suite passed after the interface and full-64-reader fixture corrections. Coverage includes expiry, refresh, missing stores at capacity, external writes, snapshot invalidation, stale callbacks, request-context isolation and failed-close recovery.
  • Fresh independent P0–P2 reviews covered admission, the CI repair, and the final lifecycle delta, with no actionable findings. Lead verified the final copied source hashes. Targeted lint, format, line-cap, assertion and SQL guards passed. The existing busy-timeout primitive replaces raw SQL, with the obsolete guard exemption removed.
  • Admission-only census at the original eight-reader limit: 8,000 read pairs across eight valid agents plus a missing store fell from 8,000 opens/7,993 closes to eight opens/zero closes. All values remained equal. This separately measured stage precedes the final larger pool and timer changes.
  • Final 32-agent census against that repaired eight-reader baseline: 8,000 read pairs require 32 successful opens instead of 8,000. Values match and all retained handles/descriptors return to baseline on close.
  • One fixed baseline/candidate/candidate/baseline comparison retained all 24 trials. For 32 agents, paired median wall time improved 61.3%/67.2% and process CPU improved 60.2%/65.4%. Wall medians were 1,375/532 ms and 1,362/447 ms per 8,000 read pairs. The eight-agent-plus-missing control was mixed: wall +31.1%/−3.2%, CPU +30.7%/−1.2%. These are component results, not Gateway throughput; trials share child processes, host exclusivity is not established, and RSS/heap measurements do not isolate pool memory.
  • Children report Node 24.19.0, but the measurement controller selected Node through PATH without recording the actual executable path against its separately hashed binary. All children joined and fixtures were removed; no repeat runs were selected for favorable results.
  • Exact-head hosted CI passed for 62f1666ccc6f6912ae3ba779470d7aa5de67fa78, including types and architecture checks. Local type preparation stopped before compilation at the maintained nested-checkout dependency-isolation guard. All diff-caused CI findings were repaired and verified. Final combined-main Gateway load follows landing.
  • The latest automated review found no introduced defect and explicitly confirmed there is no persistent-contract change. Its remaining generated migration-proof checkbox does not apply: the inspected production diff only moves the existing read pool and changes connection admission/lifetime; schemas, serialized rows, write transactions, credential policy and migrations are unchanged. Existing store compatibility and lifecycle tests passed.

@steipete steipete self-assigned this Sep 17, 2026
@clawsweeper

clawsweeper Bot commented Sep 17, 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.

@steipete
steipete marked this pull request as ready for review September 17, 2026 02:45
@steipete
steipete requested a review from a team as a code owner September 17, 2026 02:45
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L maintainer Maintainer-authored PR labels Sep 17, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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 17, 2026
@clawsweeper

clawsweeper Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 16, 2026, 11:42 PM ET / September 17, 2026, 03:42 UTC (Revision 3).

ClawSweeper review

What this changes

The PR retains up to 64 auth database readers, expires idle connections after 30 minutes, preserves useful readers when replacement opens fail, and adds focused lifecycle coverage.

Warning

SQLite table change

This PR modifies persisted SQLite tables in src/agents/auth-profiles/sqlite.ts. Prefer a design that avoids changing persisted SQLite tables. If the change is necessary, verify migration and upgrade compatibility against an existing database before merge.

Merge readiness

⛔ Blocked before merge - 1 item remains

The PR remains useful: current main and v2026.9.4 retain the premature-eviction behavior. No actionable introduced defect was found. The earlier migration-proof blocker does not apply because this patch changes connection lifecycle without changing persistent storage contracts.

Priority: P2
Reviewed head: 62f1666ccc6f6912ae3ba779470d7aa5de67fa78

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, coherent repair with useful component measurements and lifecycle coverage, without a supported correctness blocker.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The captured body reports real SQLite auth-read component runs with equal values, sharply fewer opens across 32 agents, and descriptor recovery after cleanup; focused lifecycle regressions supplement that evidence. The results do not claim full Gateway throughput.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured body reports real SQLite auth-read component runs with equal values, sharply fewer opens across 32 agents, and descriptor recovery after cleanup; focused lifecycle regressions supplement that evidence. The results do not claim full Gateway throughput.
Evidence reviewed 9 items Current main still needs the repair: The fetched main implementation evicts its oldest reader before attempting the replacement open; a missing database therefore removes a useful cached connection.
Latest release retains the old admission order: GitHub's v2026.9.4 source likewise evicts before opening the candidate. The requested repair is not already present in that release.
Admission and cleanup have one owner: Candidates are registered for cleanup before validation, eviction follows successful validation, and failed candidate cleanup blocks further uncached admission. Idle callbacks check handle identity, refresh on close failure, and do not keep the process alive.
Findings None None.
Security None None.

How this fits together

Auth profile storage reads saved credentials and scheduling state from agent databases. Its reader pool reuses SQLite connections while leaving row interpretation, credential ownership checks, and writes with their existing owners.

flowchart TD
  A[Auth profile read] --> B[Path-keyed reader pool]
  B --> C{Reusable reader?}
  C -->|Yes| D[Refresh idle timer]
  C -->|No| E[Open and validate candidate]
  E -->|Valid| F[Admit within capacity]
  D --> G[Read current committed rows]
  F --> G
  B --> H[Idle or explicit cleanup]
Loading

Before merge

  • Add data-model compatibility proof - Confirm migration or upgrade compatibility proof before merge.
Agent review details

Security

None.

PR surface

Source +69, Tests +292, Other -1. Total +360 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 3 180 111 +69
Tests 3 449 157 +292
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 1 2 -1
Total 8 630 270 +360

Review metrics

Metric Value Why it matters
Reader resource bound 8 → 64 admitted readers; 30-minute idle expiry; at most 1 additional candidate The larger warm pool trades bounded connection retention for fewer database opens, with the tradeoff documented in the PR.
Production versus test growth Production +69 net lines; tests and support +292 net lines Production growth implements candidate custody and idle cleanup; test growth covers their observable lifecycle failures.

Stored data model

Persistent data-model change detected: unknown-data-model-change: src/agents/auth-profiles/sqlite.ts. Confirm migration or upgrade compatibility proof before merge.

Technical review

Best possible solution:

Keep bounded reader reuse and failure-safe admission in the pool owner while preserving current-row reads and immediate explicit invalidation.

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

Yes, from source: fill main's eight-reader pool, then inspect a missing database; eviction occurs before the failed open. This review did not execute that scenario.

Is this the best way to solve the issue?

Yes. The patch repairs admission in the existing pool, preserves its callers and storage semantics, and bounds the larger cache with idle expiry.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This is a bounded auth-read performance and resource-lifecycle improvement without evidence of an urgent user outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured body reports real SQLite auth-read component runs with equal values, sharply fewer opens across 32 agents, and descriptor recovery after cleanup; focused lifecycle regressions supplement that evidence. The results do not claim full Gateway throughput.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured body reports real SQLite auth-read component runs with equal values, sharply fewer opens across 32 agents, and descriptor recovery after cleanup; focused lifecycle regressions supplement that evidence. The results do not claim full Gateway throughput.

Evidence

What I checked:

  • Current main still needs the repair: The fetched main implementation evicts its oldest reader before attempting the replacement open; a missing database therefore removes a useful cached connection. (src/agents/auth-profiles/sqlite.ts:383, 9a4468148bc0)
  • Latest release retains the old admission order: GitHub's v2026.9.4 source likewise evicts before opening the candidate. The requested repair is not already present in that release. (src/agents/auth-profiles/sqlite.ts:388, 3a9d69db306c)
  • Admission and cleanup have one owner: Candidates are registered for cleanup before validation, eviction follows successful validation, and failed candidate cleanup blocks further uncached admission. Idle callbacks check handle identity, refresh on close failure, and do not keep the process alive. (src/agents/auth-profiles/sqlite-read-pool.ts:73, 62f1666ccc6f)
  • Prior migration concern resolved by scope inspection: The introduced delta extracts the reader pool and reuses the existing busy-timeout primitive; it does not change schemas, serialized rows, write transactions, credential policy, or migration operations. The storage checkpoint distinguishes internal lifecycle mechanics from persistent-contract changes. (src/agents/auth-profiles/sqlite.ts:46, 62f1666ccc6f)
  • Existing invalidation remains connected: Snapshot revision publication still closes pooled readers first, and shared-auth migration still closes the selected database explicitly. Moving the implementation preserves the existing sqlite.ts export consumed by these callers. (src/agents/auth-profiles/runtime-snapshots.ts:72, 62f1666ccc6f)
  • Focused regression coverage: Seven reader lifecycle cases cover capacity, missing candidates, external committed updates, snapshot invalidation, idle refresh, stale callbacks, request-context isolation, and failed-close custody. Existing store compatibility tests remain. Tests were inspected, not executed during this read-only review. (src/agents/auth-profiles.sqlite-read-pool.test.ts:36, 62f1666ccc6f)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; 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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-17T02:53:58.912Z sha 93eaed58c04e17d2d841f125a0bf8ce9ff398448 :: blocked before merge. :: none
  • reviewed 2026-09-17T03:11:00.950Z sha d12f3e0c9c5a24525a5158030830abc2c08f6e4a :: blocked before merge. :: none

@steipete
steipete force-pushed the codex/perf-auth-missing-reader-5152 branch from 46cb0ee to a44bb28 Compare September 17, 2026 03:19
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Sep 17, 2026
@steipete steipete changed the title fix(auth): preserve pooled readers when database opens fail perf(auth): keep active database readers warm with idle expiry Sep 17, 2026
@steipete
steipete merged commit a61b077 into main Sep 17, 2026
212 checks passed
@steipete
steipete deleted the codex/perf-auth-missing-reader-5152 branch September 17, 2026 03:57
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 17, 2026
…law#150524)

* fix(auth): preserve pooled readers when database opens fail

* fix(auth): use owned busy timeout setup for pooled readers

* perf(auth): retain readers with thirty-minute idle expiry

* test(auth): describe required reader lifecycle values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: L 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