Skip to content

perf(sqlite): avoid redundant path checks in database admission - #169740

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-realpath-hot-callers-20260919
Oct 12, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-realpath-hot-callers-20260919

Conversation

@steipete

@steipete steipete commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

SQLite writer admission repeatedly resolves the same existing-file path, and worker dispatch verifies the same database access twice before posting its request.

User Impact

Reduces synchronous filesystem work on the Gateway thread while retaining replacement-file detection, FIFO write admission, current authority at dispatch/native grants, and the original writer diagnostic. No schema, configuration, migration, or update behavior changes.

Why This Change Was Made

Writer reservations already capture physical identity and a canonical queue path. Existing-file rechecks now use the existing device/inode/birthtime assertion, translating refusal back to the caller's diagnostic with the original cause. First-open reservations retain missing-path resolution so creation and alias convergence keep their contract.

Worker operation-admission preparation remains the owner of the dispatch access check; actor-context preparation now only carries context. Live transaction/commit checks remain. Production LOC: 8 additions, 13 deletions, net −5. Related: #169701.

Evidence

Fresh isolated Linux rig, Node 26.8.1, seven samples of 10,000 operations after warmup, through the actual queued writer entry point. Measurements compare source 9a1b712a9b80a86d124a417301b2328405a31a98 with this patch's exact two production files, including the diagnostic correction:

Measurement Before After Speedup
Repeated writer assertion, direct path 204.823 ms 35.316 ms 5.80×
Repeated writer assertion, symlink 321.850 ms 39.780 ms 8.09×
Complete admission with two assertions, direct path 761.584 ms 290.169 ms 2.62×
Complete admission with two assertions, symlink 1,166.888 ms 410.576 ms 2.84×

The matched 20-session tool-heavy profile pair passed strictly with 100/100 independently verified turns on each side: 20 agents, five turns each, 24 plugins, real read/write/exec tools, workspace fanout, two history clients, four subscribers, and control-plane probes. SELF values are exclusive native leaf sample time weighted by timeDelta, with idle/program/profiler frames excluded; they are sampled elapsed time, not exact syscall CPU counters.

Native realpath owner SELF share before → after ms per 1,000 verified turns before → after
boundary-path identity resolution 2.1903% → 2.0542% 40,807 → 38,254
database path-key resolution 0.7865% → 0.6912% 14,653 → 12,871
fs-safe resolution 20.4028% → 20.6368% 380,117 → 384,305

Within those buckets, native realpath SELF under writer rechecks fell 2,068 → 0 ms/1,000 turns (0.1110% → 0%), and actor-context preparation fell 3,019 → 0 (0.1620% → 0%). These selected stacks overlap the aggregate buckets and must not be added to them. Whole-Gateway turn time was 195.20 → 195.56 s (+0.18%); no throughput improvement is established. Directory-observation fs-safe calls dominate and remain outside this bounded patch.

The successful profile pair preceded only the catch restoring the caller's error message. Exact-final-source micros above passed, but its separate Gateway run failed with SQLite lock contention in a native shared-state state.write BEGIN (20 launched, zero verified, no CPU export). This is an unresolved synthetic-runtime failure, not a successful exact-final-source profile or proven attribution to this patch. Earlier 240-session attempts are excluded: baseline verified all turns but failed diagnostics teardown; candidate hit worker capacity during setup. The rig runs measurements only.

Changed-file oxfmt and repo lint passed. Focused local proof:

  • pnpm test src/state/openclaw-agent-write-admission.test.ts src/state/openclaw-agent-db-write.test.ts --maxWorkers=1: 33 passed, 109.22 s including cold compiler preparation.
  • pnpm test src/state/openclaw-agent-write-admission.test.ts src/infra/sqlite-worker-broker.test.ts --maxWorkers=1: 27 passed, 91.99 s including cold compiler preparation. The changed writer file took 4.95 s, about 1.2 s of tests.
  • Independent Codex review was clean at P0–P2, including a fresh review of the diagnostic correction.

The first PR CI run caught four alias-retarget diagnostic expectations in openclaw-agent-db-write.test.ts; those failures were caused by this patch and are now fixed without weakening the tests. Final-head PR CI owns typechecks and broad gates. No Testbox/Crabbox leases were allocated. Shared-host CPU contention limits whole-Gateway timing claims.

Keep the worker dispatch access check at operation admission. Reuse the captured physical identity for existing-file writer checks; preserve first-open path resolution and replacement detection.
@clawsweeper

clawsweeper Bot commented Oct 12, 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 October 12, 2026 08:02
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 12, 2026
@clawsweeper

clawsweeper Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR replaces repeated existing-file path resolution with physical-identity checks and removes a duplicate worker-dispatch access check.

Example: An agent writes repeatedly through a directory symlink to agent.sqlite.

  • Before: Each existing-file admission recheck resolves the canonical path again.
  • After: Rechecks compare the captured file identity while retaining replacement detection and the original refusal message.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A useful, focused patch with relevant runtime evidence and the prior finding resolved; whole-Gateway throughput improvement is not established.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied isolated Linux Gateway profile exercises both changed owners across 100 verified turns; its successful path is unchanged by the final diagnostic catch. Final-source admission measurements and focused tests also pass. The separate final Gateway contention failure remains unresolved and is not counted as passing proof or attributed to this patch.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Repeated database admission performs avoidable synchronous filesystem work on the Gateway thread.
Reason: The area owner's narrow optimization removes redundant work while preserving the existing creation, queue, diagnostic, and authority contracts.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it makes a focused, useful performance improvement, resolves the earlier diagnostic finding, and introduces no concrete bug found in this review.

Priority: P2
Reviewed head: 12f5e7c21a6b4fd6d2b1fc64f241c2ccfd053b12

Before merge

None.

Findings

None.

Agent review details

How this fits together

Agent writes enter the shared FIFO admission queue; worker requests then obtain live database authority before native SQLite operations.

flowchart LR
  A[Agent write] --> B[Captured database identity]
  B --> C[FIFO admission]
  C --> D[Worker dispatch]
  D --> E[Live native grants]
  E --> F[SQLite operation]
Loading

Technical review

Best possible solution:

Retain this bounded optimization with physical replacement detection, first-open alias convergence, and live authority owned by operation admission.

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

Current-main source shows repeated canonical-path resolution and duplicate dispatch access validation; the PR supplies comparative runtime measurements, and this review ran no target code.

Is this the best way to solve the issue?

The change reuses existing identity and authority owners without adding a cache, parallel admission path, or persisted format.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit ef6747c adds src/state/openclaw-agent-write-admission.ts:94 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ef6747c362b7; files: src/state/openclaw-agent-write-admission.ts)
  • Josh Avant: Raw commit 96e81e6 adds src/state/openclaw-agent-write-admission.ts:34 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 96e81e68add4; files: src/state/openclaw-agent-write-admission.ts)
  • Vincent Koc: Raw commit 1c8be7f adds src/state/openclaw-agent-write-admission.ts:105 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 1c8be7fb6e9a; files: src/state/openclaw-agent-write-admission.ts)

PR surface

Source -5, Tests +34. Total +29 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 8 13 -5
Tests 1 34 0 +34
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 42 13 +29

Review metrics

Metric Value Why it matters
Production versus test LOC Production +8/−13; tests +34 Production shrinks by reusing the identity owner; tests cover post-await identity changes and first creation.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove rating: 🦐 gold shrimp: 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:

  • P2: This is a measured reduction in synchronous admission overhead with limited scope and no established urgent user regression.
  • 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.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-12T08:10:08.989Z sha e58b0ae :: needs changes before merge. :: [P2] Update boundary tests for the reused identity diagnostic

Reviewed October 12, 2026, 4:30 AM ET / 08:30 UTC (Revision 2).

@clawsweeper clawsweeper Bot added 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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 12, 2026
@steipete

steipete commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

The final-head CI failure is inherited from the tested main parent, not from this PR's SQLite change. checks-node-compact-small-1 failed test/scripts/type-suppression-inventory.test.ts → keeps unchecked any casts at zero and negative type assertions explicit: the expected list has 18 entries, while the existing Bun connect({ fd }) suppression in scripts/lib/managed-cleanup-handoff.mts makes the actual list 19.

CI run https://github.com/openclaw/openclaw/actions/runs/38222451435 tested merge 7821785e6aac1c2c07ee58b3fc82acc48f941144, whose ordered parents are main 76f4fdd41acf06621fe5b4605f67816f63a7f698 and PR head 12f5e7c21a6b4fd6d2b1fc64f241c2ccfd053b12. The inventory test, scanner/parser inputs, and Bun source are byte-identical to that main parent. The PR's three changed files produce zero suppression findings.

I reproduced the identical 19-versus-18 failure on a clean checkout of that main parent with pnpm test test/scripts/type-suppression-inventory.test.ts --maxWorkers=1 -t 'keeps unchecked any casts' (13.49 s, exit 1). The downstream CI gate and fail-fast cancellations follow this root failure; canceled coverage is unrun, not passing evidence. No CI rerun or update-branch was requested.

The patch's 33 writer tests and 11 broker tests pass locally; changed-file formatting/lint and independent P0–P2 review are clean. The first-head diagnostic regression was fixed and its four alias-retarget cases now pass. Landing the exact final head under the inherited-failure exception. Benchmark gains and the unresolved exact-final-source SQLite-contention gap are recorded in the PR body.

Merged as 3490c47e4070d0f31ad9fc3eccd3d3fb1cd77bc1 after the native inherited-failure verifier passed for exact head 12f5e7c21a6b4fd6d2b1fc64f241c2ccfd053b12, including current security/review requirements. Used the authorized SHA-pinned REST fallback because the merge wrapper routes worktree setup through the shared canonical checkout, outside this lane’s allowed workspace. No CI was rerun.

@steipete
steipete merged commit 3490c47 into main Oct 12, 2026
114 of 150 checks passed
@steipete
steipete deleted the steipete/perf-micro-realpath-hot-callers-20260919 branch October 12, 2026 08:45
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
…claw#169740)

* perf(sqlite): avoid redundant path checks in database admission

Keep the worker dispatch access check at operation admission. Reuse the captured physical identity for existing-file writer checks; preserve first-open path resolution and replacement detection.

* fix(sqlite): preserve write admission diagnostics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. 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.

1 participant