Skip to content

fix(usage): avoid rescanning unchanged histories during refresh - #190295

Merged
steipete merged 4 commits into
mainfrom
steipete/perf-micro-roboclaw-db-300mbs-background-reads-20260919
Nov 1, 2026
Merged

steipete merged 4 commits into
mainfrom
steipete/perf-micro-roboclaw-db-300mbs-background-reads-20260919

Conversation

@steipete

@steipete steipete commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes unnecessary transcript reads when background usage refresh revisits histories whose durable rollups are already current.

User Impact

Unchanged histories can refresh without reading every transcript event. Changed or missing histories still use canonical statistics and normal rollup rebuilding. No schema, index, configuration, or cache migration is required.

There is a small dirty-history tradeoff: one additional indexed metadata query. On the synthetic fixture it adds 24 KiB of logical reads per selected-session operation; measured CPU/allocation differences are comparable to A/A variation. Lead accepted: the unchanged-refresh reduction from 21.50 GiB to 0.82 MiB of logical reads outweighs the dirty-path increase of 24 KiB reads, approximately 0.27 ms CPU, and 34 KiB sampled allocation per operation. The lead explicitly approved this tradeoff and landing once exact-head CI is green.

Why This Change Was Made

Refresh computed transcript statistics before checking freshness. The existing session index selects the right rows, but the noncovering byte-total aggregate visits payload-bearing rows across the selected history. A full refresh selects every session.

Read existing rollup metadata first, decode each envelope once, and reuse its exact count/sequence/byte statistics only when the transcript owner's mutation timestamp and rewrite generation match. Imports that append events now strictly advance that timestamp even when the source mtime is equal or older; idempotent imports preserve it. Single-session freshness checks use the existing keyed lookup without first counting all session windows.

Production code grows by 47 lines to connect the existing checkpoint and freshness owners and preserve import invalidation. No new cache is introduced; byte budgets remain exact. Dirty histories retain their canonical scan because existing hot-session metadata does not own exact raw-byte totals.

Evidence

  • Real-worker unchanged-refresh regression fails on parent c3fca6737b29956a61faeedd234d7296af48baca, then passes for targeted and full refresh. Equal/older-mtime import regressions also fail before the producer fix.
  • Final focused CI repair run: 77 tests across five files pass, including usage refresh/worker I/O, the backfill consumer and suppression contract. Import suite: 14 passed. Usage browser: 1 passed. Usage overview/query-recovery UI E2E: 4 passed.
  • Codex autoreview of the complete change against that parent at P0/P1: scoped-clean. Subsequent focused corrections were reviewed again with no actionable findings.
  • pnpm check:changed completed. Its PR-owned prefer-const finding is corrected, with targeted lint and core typecheck passing. Its remaining unused assertSafeSqliteRuntime export reproduces on the exact clean parent and was introduced by fix(infra): release completed SQLite readers and prevent cursor restarts #190242; fix(ci): keep SQLite runtime assertion private #190303 (1c437d1bbde7) repairs that main regression.
  • CI exposed an age-retention race in an existing lifecycle fixture: epoch timestamps allowed legitimate scheduled archival to change the row before the final deletion compare-and-swap. The unmodified exact-main-parent suite passed, but a controlled interleave through real maintenance reproduced the missing fifth notification. Current timestamps prevent archival; the corrected focused test passes on the pinned Bun runtime. All operations and the exact five-event assertion remain unchanged. The fixture is corrected with zero line growth; exact-head CI run 41308973195 passed, including all PostgreSQL shards and the aggregate gate.
  • Prior-head CI exposed the old import-clock expectation and the benchmark's intentional native-method suppression contract entry. Both are fixed. A separate lint failure reproduces on CI's exact main parent 5342923d897ad8a8021bdf586753f8cd38cd3094: two redundant <void> arguments introduced by refactor(agents): run background session work as a fork of the stored session #190158; peer fix(update-repair): preserve repair budgets across wall-clock changes #190298 owns that main regression. No inherited failure is reported as green.

The committed scripts/perf/usage-refresh-stats-measure.ts creates 5,000 sessions and 1.4 million interleaved events, half text/half compressed payloads, in a 39.27 GiB DB. No production data is used. The canonical schema is identical at the investigated deployment revision and measured baseline.

Owner operation Baseline Candidate
Unchanged full refresh, transcript SELECTs 13 indexed aggregate batches 0
Unchanged full refresh, pager misses 5,636,053 210
Unchanged full refresh, logical bytes 23,085,273,088 (21.50 GiB) 860,160 (0.82 MiB)
Unchanged full refresh, warm median wall / CPU (5 pairs) 4,759.0 / 4,756.1 ms 9.965 / 9.989 ms
Unchanged full refresh, warm p95 wall / CPU 5,094.9 / 5,089.7 ms 10.746 / 10.777 ms
Unchanged full refresh, sampled allocation (baseline n1 / candidate n5 median) 15,241,552 B 11,915,328 B
Dirty selected session, logical bytes 4,714,496 4,739,072
Dirty selected session, median wall / CPU 4.389 / 4.427 ms 4.669 / 4.695 ms
Dirty selected session, median sampled allocation 492,248 B 527,096 B

The dirty comparison has five interleaved pairs. Candidate A/A median CPU drift is +5.08% versus +6.05% A/B; sampled allocation varies in both A/A sets. This does not prove equality, so the nonzero cost remains explicit. Full-refresh warm timing has five interleaved pairs; candidate A/A wall drift is +0.11% and CPU drift +0.02%. Two earlier I/O-bound baseline scans took 193.8–194.2 seconds and are kept separate, not pooled with warm results. Full allocation has baseline n1/candidate n5; an attempted warm allocation expansion exceeded its 45-second ceiling and was stopped without a completed sample. One accidentally overlapped preliminary series was excluded; final accepted series ran serially. The complete refresh-consumed statistics digest matches on every accepted sample.

Acceptance: identical consumed statistics; eliminate unchanged-history event queries; five interleaved timing pairs with A/A controls; lower full-refresh CPU/time beyond A/A spread; disclose dirty-path and allocation tradeoffs. SQLite page caches are fresh/private, mmap is disabled, and SQL replay records EXPLAIN QUERY PLAN plus pager misses. These are logical reads, not measured physical disk bytes. The measured collection owner excludes envelope decoding and full worker/Gateway scheduling. Host is macOS/APFS with uncontrolled OS cache. Immediate Linux test capacity queued and was canceled under the proof policy. Linux/btrfs physical-I/O reduction and sustained production-reader attribution remain unproven.

@clawsweeper

clawsweeper Bot commented Nov 1, 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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation scripts Repository scripts size: M labels Nov 1, 2026
@steipete
steipete marked this pull request as ready for review November 1, 2026 11:54
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 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-11-01T12:26:34.353279Z dc18dd3 New commits
ℹ️ 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.

@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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 1, 2026
@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Nov 1, 2026
@steipete
steipete merged commit 6463a0a into main Nov 1, 2026
134 of 138 checks passed
@steipete
steipete deleted the steipete/perf-micro-roboclaw-db-300mbs-background-reads-20260919 branch November 1, 2026 13:24
@steipete

steipete commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

roboclaw-bot added a commit that referenced this pull request Nov 1, 2026
* fix(ci): allow the host sqlite3 CLI in Knip binary checks

#190295 added usage-refresh-stats-measure.ts, which inspects benchmark
fixtures with the host sqlite3 CLI. Knip reported it as an unlisted
binary and deadcode:dependencies failed on main.

Co-authored-by: vyctorbrzezowski <[email protected]>

* fix(ci): allow the host sqlite3 CLI in Knip binary checks

Worked on by:
- @vyctorbrzezowski

Co-authored-by: vyctorbrzezowski <[email protected]>
OpenClaw-Publication: 0c259938-137e-4428-b495-a3b45716bdeb

---------

Co-authored-by: vyctorbrzezowski <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 2, 2026
…claw#190295)

* fix(usage): avoid rescanning unchanged histories during refresh

* refactor(usage): share prepared rollup envelopes during refresh

* fix(usage): keep single-session freshness reads keyed

* test(sessions): keep lifecycle fixtures outside age retention
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 2, 2026
…90396)

* fix(ci): allow the host sqlite3 CLI in Knip binary checks

openclaw#190295 added usage-refresh-stats-measure.ts, which inspects benchmark
fixtures with the host sqlite3 CLI. Knip reported it as an unlisted
binary and deadcode:dependencies failed on main.

Co-authored-by: vyctorbrzezowski <[email protected]>

* fix(ci): allow the host sqlite3 CLI in Knip binary checks

Worked on by:
- @vyctorbrzezowski

Co-authored-by: vyctorbrzezowski <[email protected]>
OpenClaw-Publication: 0c259938-137e-4428-b495-a3b45716bdeb

---------

Co-authored-by: vyctorbrzezowski <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 22, 2026
Partial, path-only backport of 6463a0a (#190295). Excludes unrelated usage changes. Fresh timestamps prevent real age-retention maintenance from archiving lifecycle test fixtures before expected-entry deletion. All lifecycle assertions remain unchanged.

Co-authored-by: RomneyDa <[email protected]>

Co-authored-by: vincentkoc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. docs Improvements or additions to documentation gateway Gateway runtime scripts Repository scripts size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant