Skip to content

fix: reduce CPU overhead for small history and usage reads - #147205

Merged
steipete merged 2 commits into
mainfrom
codex/team-transcript-byte-tail
Sep 13, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/team-transcript-byte-tail

Conversation

@steipete

@steipete steipete commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes extra Gateway CPU work when chat-history or live-usage reads request a small transcript byte budget.

User Impact

Small reads size only the tail needed to enforce their budget. Returned history, token usage, cost, and oversized-newest behavior stay the same; no configuration or migration is required.

Why This Change Was Made

Recent reads previously materialized the entire candidate metadata window before walking backward to the byte boundary. The existing reset-window reader now supports descending iteration, so usage and history stop after the first excluded predecessor. History still merges reset/compaction boundary bytes, and hard-cap siblings retain their complete metadata reads and integrity checks.

Evidence

The regression failed before with 3,000 metadata rows across three reads and now passes with at most six, including repeated reads and a subsequent write on the same SQLite connection. All 66 focused tests pass, covering sparse reset chunks above 500 positions, boundary-only tails, oversized events, hard-cap siblings, concurrent writers, and history cursor fences.

Synthetic calls through the real rich-row reader reduced metadata rows from 1,000 to 74–96 per read while returning the same payload bytes, token totals, and cost. This measures reader work, not its share of live Gateway CPU. Local core and core-test typechecks, scoped lint, and the selected repository guards pass. Fresh independent review found no actionable issues. Exact-head CI run 34771572345 passed before native prepare and merge.

The initial CI run hit a baseline Workboard cleanup lint failure. The branch carried its one-line awaited-cleanup repair, with the exact concurrency test and scoped extension lint passing. #147203 landed that repair independently on main before this merge, so the final merge contains only the four transcript reader/regression files.

@clawsweeper

clawsweeper Bot commented Sep 13, 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 13, 2026 17:10
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Sep 13, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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 13, 2026
@clawsweeper

clawsweeper Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 13, 2026, 1:33 PM ET / 17:33 UTC (Revision 2).

ClawSweeper review

What this changes

The PR makes small history and usage reads stop sizing transcript rows at their byte budget, extends regression coverage, and awaits asynchronous Workboard test cleanup.

Merge readiness

✅ Ready for maintainer review

The optimization remains useful: current main and the latest release still materialize candidate metadata before enforcing small byte budgets. No actionable correctness or security defect was found.

Priority: P2
Reviewed head: 649c5dbc4cf75bfe0de3a3e61a73923a5ab4956d

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused optimization with meaningful regression coverage and no actionable defect found in source review.
Proof confidence 🌊 off-meta tidepool Not applicable: The supplied repository context identifies maintainer-authored work, exempt from the external-contributor proof gate; reported synthetic production-reader measurements support reduced metadata work but do not measure live Gateway CPU share.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The supplied repository context identifies maintainer-authored work, exempt from the external-contributor proof gate; reported synthetic production-reader measurements support reduced metadata work but do not measure live Gateway CPU share.
Evidence reviewed 8 items Verified merge scope: The test merge records the pinned main parent followed by the exact PR head. Its session changes match the introduced patch; unrelated main-branch work is not removed.
Current main still performs eager sizing: The main implementation builds a complete metadata Map before walking backward to the byte boundary.
Latest release also retains eager sizing: The v2026.9.4 source likewise materializes readVisibleMessageMetadata into a Map before selecting the tail.
Findings None None.
Security None None.

How this fits together

The Gateway reads stored session transcripts to serve chat history and calculate recent usage. These SQLite readers select the appropriate transcript tail before loading message payloads.

flowchart TD
  A[History or usage request] --> B[Transcript snapshot]
  B --> C[Visible messages and reset boundaries]
  C --> D[Newest-first byte sizing]
  D --> E{Budget reached}
  E -->|No| D
  E -->|Yes| F[Load selected tail]
  F --> G[History or usage result]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +31, Tests +29. Total +60 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 3 85 54 +31
Tests 2 61 32 +29
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 146 86 +60

Review metrics

None.

Technical review

Best possible solution:

Keep bounded tail selection in the existing transcript reader, preserving snapshot consistency, logical ordering, byte accounting, and iterator cleanup.

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

Yes, from source: a small byte budget with a large candidate window still materializes all metadata on main. The PR reports a failing-before regression; this review did not execute tests.

Is this the best way to solve the issue?

Yes. Descending iteration extends the existing range reader and preserves the separate complete-metadata path required by hard-cap integrity checks.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This is a bounded performance improvement to routine history and usage reads, with no demonstrated urgent outage.
  • 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: The supplied repository context identifies maintainer-authored work, exempt from the external-contributor proof gate; reported synthetic production-reader measurements support reduced metadata work but do not measure live Gateway CPU share.

Evidence

What I checked:

  • Verified merge scope: The test merge records the pinned main parent followed by the exact PR head. Its session changes match the introduced patch; unrelated main-branch work is not removed. (428f1a0ced88)
  • Current main still performs eager sizing: The main implementation builds a complete metadata Map before walking backward to the byte boundary. (src/config/sessions/session-accessor.sqlite-history-events.ts:165, adb6e518a442)
  • Latest release also retains eager sizing: The v2026.9.4 source likewise materializes readVisibleMessageMetadata into a Map before selecting the tail. (src/config/sessions/session-accessor.sqlite-history-events.ts:273, 3a9d69db306c)
  • Ordering and cleanup remain owned: Descending iteration reverses both range order and SQL row order while preserving logical positions. History explicitly closes its iterator in finally; the usage for-of loop closes it on early exit. The shared SQLite generator delegates cleanup to the native iterator. (src/config/sessions/session-accessor.sqlite-reset-window.ts:560, 649c5dbc4cf7)
  • Regression coverage targets the changed behavior: Both history and usage readers are exercised with ordinary and oversized newest events, three repeated reads, a metadata-row bound, and a subsequent transcript write. Existing sparse-reset coverage crosses 500-position chunks. Tests were inspected, not executed during this read-only review. (src/config/sessions/session-accessor.sqlite-byte-size.test.ts:211, 649c5dbc4cf7)
  • Reported validation and review continuity: The supplied complete PR body reports 66 focused passing tests and synthetic rich-row reads reducing metadata rows from 1,000 to 74–96 with identical payload bytes, token totals, and cost; it expressly limits the claim to reader work. The previous completed review records no findings or published rank-up moves. Its earlier SHA was unavailable for comparison, so no source-equivalence claim is made. (649c5dbc4cf7)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; 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 (1 earlier review cycle)
  • reviewed 2026-09-13T17:18:57.604Z sha 427939e :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the codex/team-transcript-byte-tail branch from 427939e to 649c5db Compare September 13, 2026 17:25
@steipete
steipete merged commit daf0044 into main Sep 13, 2026
186 of 187 checks passed
@steipete
steipete deleted the codex/team-transcript-byte-tail branch September 13, 2026 17:49
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

vincentkoc added a commit that referenced this pull request Sep 13, 2026
* commit '201f11afefdb19a9c75e9d3542bf7425c80df7a7': (141 commits)
  fix: keep catalog discovery scoped and execution lazy (#147159)
  fix: serialize reclamation worker shutdown with foreground writes (#147178)
  test(sessions): cover abrupt committed transcript recovery (#143984)
  fix: stop transcript storms during context overflow recovery (#147202)
  fix: reduce CPU overhead for small history and usage reads (#147205)
  fix: resume tasks after tools finish and the model times out (#147203)
  refactor(scripts): share SQLite reliability message waits (#146900)
  feat(exec): configure approval reviewer thinking and fastMode (#147192)
  test: remove redundant Bonjour module headers (#146981)
  refactor: share streaming media tail boundary update (#146989)
  refactor(tui): classify resume candidates once (#146983)
  improve: skip unused toast dismissal work (#147196)
  fix(linux): recover saved remote connections without replacing active sessions (#146047)
  fix: doctor stays silent when Windows state dir is under OneDrive (#123720)
  feat: announce successful Daybreak retries (#147146)
  feat(code-mode): reuse results across code cells (#147154)
  refactor(test): reuse CLI plugin fixture writer (#147186)
  test: skip unused compiler setup in routing fixtures (#147194)
  test: reuse LINE webhook runtime fixture (#146710)
  fix(code-mode): check automation and process result types (#147143)
  ...
vincentkoc added a commit that referenced this pull request Sep 13, 2026
* commit '2d87daf04b78992382299d552f15ac84a86a23ed': (1815 commits)
  fix(ui): distinguish tool activity with semantic icons (#147207)
  fix(linux): recover completed Quick Chat replies without resending (#146048)
  feat(desktop): unify Tauri title bar and window controls (#147097)
  refactor(test): share plugin fixture server shims (#147225)
  perf(gateway): defer worker workspace execution imports (#143496)
  fix(gateway): post-ack webhook work fails with "Async work scope is closed" (#144801)
  refactor(scripts): centralize upgrade survivor version floors (#146975)
  fix(ci): check publication registries before FRV fanout (#147184)
  fix(qa): align system-agent delegation with channel contracts (#145905)
  improve: reduce repeated chat message identity work (#147210)
  fix(channels): remove stale pre-tool previews from Slack and Telegram (#147106)
  fix: keep catalog discovery scoped and execution lazy (#147159)
  fix: serialize reclamation worker shutdown with foreground writes (#147178)
  test(sessions): cover abrupt committed transcript recovery (#143984)
  fix: stop transcript storms during context overflow recovery (#147202)
  fix: reduce CPU overhead for small history and usage reads (#147205)
  fix: resume tasks after tools finish and the model times out (#147203)
  refactor(scripts): share SQLite reliability message waits (#146900)
  feat(exec): configure approval reviewer thinking and fastMode (#147192)
  test: remove redundant Bonjour module headers (#146981)
  ...
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 14, 2026
…147205)

* perf(sessions): stop recent transcript sizing at the byte budget

* test(workboard): await concurrent store cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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