Skip to content

improve: reduce repeated work loading session history - #153161

Merged
steipete merged 2 commits into
mainfrom
codex/session-history-query-cleanup-20260919
Sep 19, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/session-history-query-cleanup-20260919

Conversation

@steipete

@steipete steipete commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #153160. Continues #153010.

What Problem This Solves

Session-history reads still rebuild repeated queries, parse saved prompts for presence checks, and decompress an archive they may never examine.

User Impact

History behavior stays the same, with less latest-message query construction and smaller presence-read payloads. No schema changes, migration, or operator action is required. Component latency is mixed; this is not a claim of uniformly faster history or measured live Gateway latency.

Why This Change Was Made

Reuse the existing message-query owner and compiled readers, remap imported controls with indexed set lookups, and keep archive fallback policy in the transcript kernel. The archive reader now serves only retained reset archives, matching every production caller. Anchored reads materialize each candidate when examined.

Net 108 production lines and 79 test lines removed. Existing 500-position retained-message chunks remain because packing the entire range made a byte-capped early exit 4.5× slower. Cache lifetime, database ownership, reset visibility, byte caps, and retention remain unchanged.

Evidence

Local synthetic before/after measurements on Node 24.21.0 / SQLite 3.53.4:

Workload Result
Latest message, median round p50 0.0506 → 0.0393 ms, about 22% lower
Presence with 512 KiB saved prompts Entry-owner JSON parsed: 524,845 → 120 bytes; latency mixed (roughly ±5%). Durable SQL calls 7 → 9; incognito 12 → 13
4,096 imported controls 10 → 2 statements; p50 6.9346 → 7.2378 ms (+4.4%)
Two archive candidates, anchor in first 2 → 1 decompressions; compressed bytes 105,565 → 52,783
Ordinary one-candidate archive p50 0.3416 → 0.3426 ms; identical I/O
10k retained messages 1 KiB budget p50 0.5975 → 0.6035 ms; 4 MiB budget 50.0187 → 53.1006 ms (+6.2%)

The range matrix uses medians of four round percentiles; presence uses ABBA processes; archive stability uses 1,000 interleaved AB/BA pairs after 100 warmup pairs. Shared-host timings vary. Statement, parsed-byte, and decompression counts describe the concrete work changes more reliably.

Validation: 390 focused tests across 22 files passed, followed by 137 affected tests across 10 files after final refinements. Baseline archive suites passed 90 tests. Final changed-code gate passes, including typechecks, lint, dead-code scans, and storage guards. Additional experiments checked 33 range comparisons, 402 sparse-control comparisons with index-plan assertions, 18 presence behavior cells, and 2,201 archive-result comparisons. Independent P0–P2 review passed with no actionable findings.

Hosted CI caught an additional Kysely guard violation in the numeric-set SQL. The follow-up uses the existing structured table-valued-function pattern. All 33 comparisons pass again, with identical statement and returned-row counts across 26 workloads; the repository-wide Kysely guard, core typecheck, and targeted lint now pass. The timing table predates this equivalent query-construction rewrite; latest-message, presence, and archive execution paths are unchanged.

Reuse the message-query owner, keep archive fallback policy in the transcript kernel, and avoid parsing saved prompts for boolean presence checks. Preserve bounded reset-tail chunks and cache lifetime guards. Local synthetic latest-message p50 improves about 22%; other latency results are mixed. Refs #153160.
@clawsweeper

clawsweeper Bot commented Sep 19, 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 19, 2026 20:41
@steipete steipete self-assigned this Sep 19, 2026
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Sep 19, 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 19, 2026
@clawsweeper

clawsweeper Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 19, 2026, 4:58 PM ET / 20:58 UTC (Revision 2).

ClawSweeper review

What this changes

The PR reuses session-history queries, reads smaller session metadata for presence checks, and decompresses retained archives only when examined.

Merge readiness

✅ Ready for maintainer review

The PR remains useful: current main retains repeated query construction and the unused archive-reader mode. Source review found no actionable introduced defect, and the previous review has no outstanding findings.

Priority: P2
Reviewed head: 7c5267463f8b8fc76d4c07c5471895828030598f

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-preserving cleanup with candid component measurements and no actionable source-review findings.
Proof confidence 🌊 off-meta tidepool Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate and does not materially change authority. Its body reports production-reader component comparisons for SQLite queries, presence payloads, and archive decompression, explicitly excluding live Gateway latency claims.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate and does not materially change authority. Its body reports production-reader component comparisons for SQLite queries, presence payloads, and archive decompression, explicitly excluding live Gateway latency claims.
Evidence reviewed 8 items Introduced scope and review continuity: GitHub comparison confirms the pinned merge-base-to-head file set. The follow-up from the previously reviewed head changes only numeric-set construction to a structured Kysely subquery; the supplied previous review contains no findings or published rank-up moves.
Query and cache contracts: The latest-message reader uses the existing connection-keyed compiled-reader owner with fresh bindings. Shared queries retain session predicates, ordering, and range bounds; retained reset ranges still use 500-position chunks. Reset-cache connection and transaction guards remain unchanged.
Archive fallback ownership: Repository references identify the transcript kernel as the archive reader's only production consumer. Its existing fallback conditions remain intact; formerly explicit archive-only calls now use the narrowed reader, and anchored reads materialize candidates during iteration.
Findings None None.
Security None None.

How this fits together

Gateway history requests read session messages from SQLite, with the transcript kernel deciding when retained reset archives may supply fallback history. These readers return bounded message pages and metadata to history consumers.

flowchart TD
  A[History request] --> B[Transcript read kernel]
  B --> C[SQLite snapshot]
  C --> D[Shared message queries]
  D --> E[Bounded history results]
  B --> F[Archive fallback decision]
  F --> G[Read selected reset archive]
  G --> E
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -108, Tests -79. Total -187 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 8 194 302 -108
Tests 2 73 152 -79
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 267 454 -187

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #153160
Summary: The paired open issue describes this exact cleanup; the earlier merged work concerns distinct cache-lifetime correctness.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Keep query compilation and archive fallback with their existing owners while preserving history ordering, byte caps, snapshot boundaries, and stored formats.

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

Not applicable to a functional bug: this is an efficiency cleanup. Repeated work is visible in main's source, and the body supplies synthetic comparisons; this review did not execute benchmarks or tests.

Is this the best way to solve the issue?

Yes. Reusing existing query and metadata owners and removing a production-unused archive mode is a focused simplification without a new API, schema, or configuration surface.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This is a bounded session-history efficiency improvement with concrete component measurements and no demonstrated urgent user-facing regression.
  • 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 maintainer-authored PR is exempt from the external-contributor proof gate and does not materially change authority. Its body reports production-reader component comparisons for SQLite queries, presence payloads, and archive decompression, explicitly excluding live Gateway latency claims.

Evidence

What I checked:

  • Introduced scope and review continuity: GitHub comparison confirms the pinned merge-base-to-head file set. The follow-up from the previously reviewed head changes only numeric-set construction to a structured Kysely subquery; the supplied previous review contains no findings or published rank-up moves. (src/config/sessions/session-accessor.sqlite-projection-read.ts:109, 7c5267463f8b)
  • Query and cache contracts: The latest-message reader uses the existing connection-keyed compiled-reader owner with fresh bindings. Shared queries retain session predicates, ordering, and range bounds; retained reset ranges still use 500-position chunks. Reset-cache connection and transaction guards remain unchanged. (src/config/sessions/session-accessor.sqlite-projection-read.ts:156, 7c5267463f8b)
  • Archive fallback ownership: Repository references identify the transcript kernel as the archive reader's only production consumer. Its existing fallback conditions remain intact; formerly explicit archive-only calls now use the narrowed reader, and anchored reads materialize candidates during iteration. (src/gateway/session-transcript-read-kernel.ts:178, 7c5267463f8b)
  • Presence validation preserved: Both durable-worker and incognito presence paths select the existing list projection. The exact-read owner still performs canonical admission, row parsing, participant projection, and physical database ownership checks; only saved prompt payloads are omitted. (src/config/sessions/session-accessor.sqlite-entry-read.ts:237, 7c5267463f8b)
  • Still distinct from main: Fetched main lacks the compiled latest-message reader and retains the archive reader's live-artifact mode. The related merged cache-lifetime fix addresses rollback and database replacement, not this remaining cleanup. No local tag contains the reviewed head. (src/config/sessions/session-accessor.sqlite-projection-read.ts:143, a8046858b9ea)
  • Supplied component evidence: The captured PR body reports Node 24.21.0 / SQLite 3.53.4 synthetic comparisons: latest-message p50 0.0506 to 0.0393 ms, presence payload parsing 524,845 to 120 bytes, and first-candidate archive decompressions 2 to 1. It explicitly reports mixed latency elsewhere and no measured live Gateway improvement. These are contributor-reported results, not reviewer executions. (7c5267463f8b)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • vincentkoc: 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-19T20:48:30.415Z sha 20b3cda :: needs maintainer review before merge. :: none

@steipete
steipete merged commit 5ca92f8 into main Sep 19, 2026
213 of 214 checks passed
@steipete
steipete deleted the codex/session-history-query-cleanup-20260919 branch September 19, 2026 21:17
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 20, 2026
* perf(sessions): reduce repeated history reader work

Reuse the message-query owner, keep archive fallback policy in the transcript kernel, and avoid parsing saved prompts for boolean presence checks. Preserve bounded reset-tail chunks and cache lifetime guards. Local synthetic latest-message p50 improves about 22%; other latency results are mixed. Refs openclaw#153160.

* refactor(sessions): build numeric set reads through Kysely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime 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: 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.

Reduce avoidable work in session-history readers

1 participant