Skip to content

perf(state): await existing-state source probes - #188386

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-state-lease-sync-open-20260919
Oct 30, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-state-lease-sync-open-20260919

Conversation

@steipete

@steipete steipete commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Existing-state reads synchronously inspect the source database twice on the Gateway thread, so filesystem latency blocks unrelated requests even though SQLite reads already run in a worker.

User Impact

Source existence and queued file-identity checks now yield while waiting for the filesystem. Missing sources still avoid worker allocation, replacements still fail identity validation, and closing the owner during the probe rejects the read. There are no schema or configuration changes.

Why This Change Was Made

Use the existing asynchronous filesystem operation and identity predicate, hold the existing admission turn through dispatch preparation, and check live authority after waiting. This removes the nested dispatch callback and changes production code by −1 line (+19/−20). No cache or alternate fast path is introduced.

This is a bounded reader fix. Native state-lease verification and quarantine admission still perform synchronous work; this PR does not claim to remove their cost. The measured allocation increase below is the cost of making these source checks asynchronous.

Evidence

Real SQLite owner benchmark: 10,000 retained lease keys across 40 scopes, initialized quarantine store, 100 warmups, 2,000 reads per pass. CPU and sampled allocation use separate uninstrumented passes. The filesystem-count pass wraps exported synchronous node:fs functions and realpathSync.native. These are JavaScript filesystem counts, not native SQLite syscall tracing.

Measurement Base Candidate
Synchronous source probes / 2,000 reads 4,000 0
All measured host synchronous filesystem calls / 2,000 reads 14,000 10,000
Main-thread sampled allocation/read, pair 1 106.8 KB 114.7 KB
Main-thread sampled allocation/read, reverse pair 106.3 KB 113.8 KB
Main-thread CPU / 2,000 reads, pair 1 2.233 s 2.542 s
Main-thread CPU / 2,000 reads, reverse pair 2.010 s 2.405 s
Heartbeat maximum gap with 20 ms injected per source probe, pair 1 43.2 ms 9.3 ms
Same injected probe, reverse pair 46.2 ms 1.8 ms

Source-probe regression assertion fails on the base (4,000 calls) and passes on the candidate (zero). The reverse pair used the complete exported-sync-function instrumentation. Five synchronous quarantine-admission calls/read remain and are counted separately from the source probes.

Allocation rose approximately 7.3%. CPU initially rose 13.8% and 19.6%, but identical-source controls produced 1.892→1.499 s and 1.516→0.953 s (−20.8% and −37.2%), so CPU is inconclusive on this shared host, not a demonstrated improvement. One-minute host load was 84.8/86.5 in pair 1, 76.3/99.7 in the reverse pair, and 50.2–56.9 during controls. No whole-Gateway throughput claim is made.

Validation:

  • Focused reader, source-identity, and error-mapping tests: 3 files, 30 tests passed; final owner-close regression run: 20 reader tests passed.
  • Changed-file core and benchmark lint, formatting, and git diff --check passed.
  • Independent subagent review (autoreview route unavailable): no actionable findings. The configured review route refused its provider/configuration mismatch; no operator configuration was changed.
  • Owner-level proof follows the requested fast-path protocol. No Team load, Testbox lease, or broad local suite was run. PR CI supplies broad validation.

Reproduce using scripts/perf/sqlite-native-lease-measure.mjs --existing-read --iterations 2000 --delay-source-only --assert-no-source-io with the Node invocation in the script header. --owner-dir selects a sparse baseline source overlay; omit --delay-source-only to inject latency into the remaining measured host filesystem calls too.

CI attribution

All completed checks outside PostgreSQL conformance shard 2 passed. That shard fails src/infra/database-engine/archive-restore.postgres.test.ts, case “publishes recovered PostgreSQL and SQLite selection from a captured config alias last,” with ConfigMutationConflictError: included config target changed since last load at captureConfigFileWritePathProof (io.write-safety.ts:124).

The identical test/error/stack also fails on unchanged main f80ab2ad1c1a0d01ce5a73c39959c27daf425248: main job 124089262163, compared with PR job 124098389842. The failing test and config/backup stack files are identical between that main revision and this PR. No CI rerun was requested. This is an attributed inherited failure, not a passing check.

Move warm reader existence and queued identity probes off the main thread while retaining admission and checking live authority after each wait. Reject missing-source results when the owner closes during the probe.

Keep native lease verification and quarantine admission outside this bounded change; their synchronous ownership checks need a separate complete cutover.
@clawsweeper

clawsweeper Bot commented Oct 30, 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T13:48:33.349588Z 797ad42 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation scripts Repository scripts size: M labels Oct 30, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 30, 2026
@clawsweeper

clawsweeper Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR makes existing-state source existence and dispatch-time identity checks asynchronous while preserving admission and owner-lifetime checks.

Example: A backup-history read encounters 20 ms filesystem latency on each source probe.

  • Before: The reported owner benchmark records heartbeat gaps of 43.2–46.2 ms.
  • After: The reported candidate records gaps of 1.8–9.3 ms while retaining replacement rejection.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, source-supported repair with useful owner-level measurements; proof remains below the shipped entry point, and some added assertions depend on helper names.
Proof confidence 🦐 gold shrimp (3/6) The ordinary contributor proof gate does not apply to this maintainer-authored PR; its reported real SQLite owner benchmark exercises the changed read pipeline with injected filesystem latency, supported by a committed reproduction script.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Slow source-database filesystem checks block unrelated Gateway requests even though SQL execution already runs in workers.
Reason: The bounded change preserves existing ownership contracts, removes measured synchronous source work, and records the allocation tradeoff under the maintainer-author's accepted direction; new flags control only the benchmark.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR addresses a current main-thread blocking path with a focused change, and no introduced production defect was found.

Priority: P2
Reviewed head: 797ad4233f554d67948ebba98f2c4cd07ffcad2b

Before merge

None.

Findings

None.

Tests

  • Low-value test src/state/openclaw-state-read-worker.test.ts: Remove the added warm-read helper-name spies: asserting that existingPathOrUndefined and assertExistingDatabaseIdentity were not called can pass while equivalent synchronous filesystem work remains; the benchmark's filesystem-count assertion protects the actual contract.
  • Missing end-to-end proof: No shipped Gateway request scenario is supplied; the real SQLite owner benchmark is the strongest behavioral evidence.
Agent review details

How this fits together

Shared-state callers submit typed reads to an operation owner, which checks source availability and admission before dispatching SQL work to the existing reader pool and returning results after cleanup.

flowchart TD
  A[Shared-state caller] --> B[Read operation owner]
  B --> C[Await source existence]
  C --> D[Admission and identity check]
  D --> E[Existing reader worker]
  E --> F[Result and cleanup]
Loading

Technical review

Best possible solution:

Keep both source probes asynchronous within the existing read owner, preserving missing-source behavior, dispatch-time identity validation, admission custody, and checks after waiting.

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

Current main synchronously stats the source in both owning paths, corroborating the reported blocking mechanism; the supplied benchmark reports a base failure and candidate pass, but no target code was executed during this read-only review.

Is this the best way to solve the issue?

The invariant is nonblocking source inspection without weakening source identity or live admission, and replacing these probes through their existing owners is the smallest complete production fix.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Dispatch-time source identity validation keeps the original intent (9cea70f: Restore replacement refusal after queued admission without changing prepared-snapshot semantics.)
  • Read-operation lifecycle and source absence keeps the original intent (997e2bf: Consolidate query and cleanup under one asynchronous owner while preserving noncreating reads and lifecycle settlement.)
  • SQLite ownership benchmark keeps the original intent (e0b550b: Measure runtime ownership filesystem costs using real synthetic SQLite and separate CPU, allocation, and filesystem-count passes.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 9cea70f adds src/state/openclaw-state-read-worker.ts:363 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9cea70faa44f; files: src/state/openclaw-state-read-worker.ts)

PR surface

Source -1, Tests +29, Docs +6, Other +76. Total +110 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 19 20 -1
Tests 1 30 1 +29
Docs 1 6 0 +6
Config 0 0 0 0
Generated 0 0 0 0
Other 1 108 32 +76
Total 5 163 53 +110

Review metrics

Metric Value Why it matters
Production versus regression-test delta Production +19/-20; tests +30/-1 Production remains smaller; the owner-close regression protects a distinct lifecycle boundary, while the added helper-name spies provide weaker coverage.
Reported synchronous source probes 4,000 → 0 per 2,000 reads The committed owner benchmark directly measures the targeted work; remaining synchronous quarantine checks are explicitly outside scope.

Labels

Label changes:

  • add P2: Slow filesystem probes can delay unrelated Gateway work, but this is a bounded performance repair without evidence of an urgent outage.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Slow filesystem probes can delay unrelated Gateway work, but this is a bounded performance repair without evidence of an urgent outage.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Remove the added helper-name spies and retain the filesystem-count benchmark and distinct owner-close regression.

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.

Reviewed October 30, 2026, 10:01 AM ET / 14:01 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

The remaining PostgreSQL conformance (2/3) failure is inherited: archive-restore.postgres.test.ts → “publishes recovered PostgreSQL and SQLite selection from a captured config alias last” raises ConfigMutationConflictError: included config target changed since last load at captureConfigFileWritePathProof (io.write-safety.ts:124).

The same test, error, and stack fail on main f80ab2ad1c1a0d01ce5a73c39959c27daf425248 (baseline job); the PR job reproduces it. The failing test and config/backup stack files are unchanged. No rerun was used to chase green. Independent review is clean; this failure is eligible for the maintainer-authorized inherited-failure landing exception.

@steipete
steipete merged commit 7c9bb1b into main Oct 30, 2026
156 of 161 checks passed
@steipete
steipete deleted the steipete/perf-micro-state-lease-sync-open-20260919 branch October 30, 2026 14:13
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 31, 2026
* perf(state): await existing-state source probes

Move warm reader existence and queued identity probes off the main thread while retaining admission and checking live authority after each wait. Reject missing-source results when the owner closes during the probe.

Keep native lease verification and quarantine admission outside this bounded change; their synchronous ownership checks need a separate complete cutover.

* chore(perf): avoid shadowing the filesystem probe key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: M 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