Skip to content

perf(sessions): batch eager model-context payload reads - #138168

Merged
steipete merged 1 commit into
mainfrom
codex/db-prep-session-context
Sep 4, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/db-prep-session-context

Conversation

@steipete

@steipete steipete commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Opening a long model context executes one SQLite payload query per selected transcript entry. A synthetic 4,000-message context requires 4,002 statement executions even though the caller needs a complete, detached result.

Why This Change Was Made

Fetch selected eager payloads in bounded batches of 400 within the existing read snapshot. Restore the existing navigation order and keep replay/private-field projection inside SQL. Full-fidelity callback readers remain lazy and close their iterators before the snapshot ends.

User Impact

Long eager context reads perform fewer database operations. Model content, ancestry, compaction handling, admission fences, stored transcript bytes, and the public API stay unchanged. SQLite remains the only backend; there is no schema or configuration change.

Evidence

  • All 23 model-context tests passed. The added real-SQLite query-budget regression fails on the original implementation and passes with batching.
  • Full changed gate and independent Codex P0–P2 review passed.
  • Uninstrumented, interleaved reads of the same synthetic database measured 40 messages at 1.88→1.89 ms, 1,200 at 35.40→32.19 ms, and 4,000 at 115.60→105.21 ms. These are local synthetic medians, not production latency claims.
  • Separately measured statement counts: 42→3, 1,202→5, and 4,002→12 respectively. Complete projected-result hashes and stored-event hashes matched.
  • Production: +71/-48 (net +23); tests: +46/-1 (net +45). Added code owns bounded eager batches while preserving the separate lazy contract.
  • Independent exact-candidate native SQLite validation passed on commit d058173f883a1c396c195fcacdded217924a69b4, with the same statement-count reductions, matching projected bytes, and unchanged durable bytes. Uninstrumented second-run large-case medians were 36.59→32.57 ms and 122.61→111.43 ms. Source tree 6ca87bf8058550e4bf127b9b5fa7205d0773e5d0 matched before/after.

@clawsweeper

clawsweeper Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete
steipete marked this pull request as ready for review September 4, 2026 10:05
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Sep 4, 2026
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 4, 2026
@clawsweeper

clawsweeper Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 4, 2026, 6:28 AM ET / 10:28 UTC.

ClawSweeper review

What this changes

The PR batches SQLite payload reads for detached model-context loading so long session histories require bounded query executions while preserving selected-message order and projections.

Merge readiness

⛔ Blocked before merge - 3 items remain

Keep this PR open for an explicit maintainer landing decision. The current main branch still uses one eager payload query per selected entry, while the introduced batching preserves the separate lazy reader and existing model-context projections; no actionable correctness defect was found in the reviewed change.

Priority: P3
Reviewed head: d058173f883a1c396c195fcacdded217924a69b4
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-bound performance repair with behavior coverage and no source-level finding; exact-head CI remains a normal landing gate.
Proof confidence 🌊 off-meta tidepool Not applicable: Repository state identifies this as a maintainer-authored PR, so the external-contributor proof gate does not apply; its supplied native-SQLite validation claims are supplemental.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Repository state identifies this as a maintainer-authored PR, so the external-contributor proof gate does not apply; its supplied native-SQLite validation claims are supplemental.
Evidence reviewed 5 items Introduced eager batching: The introduced path collects only entries selected by the canonical context-window iterator, reads them in batches of 400 inside the existing read snapshot, applies the existing SQL projection, and reconstructs output in navigation order.
Current main remains unbatched: Current main at the fetched SHA retains the prior model-context branch that invokes the prepared single-row payload query for every selected entry, so this PR is neither implemented nor obsolete on main.
Behavioral regression coverage: The added real-SQLite test checks selected-message order and bounds statement iteration for a 1,200-message sparse context; pre-existing cases cover compaction, reset retention, private-field exclusion, detached reads, and lazy-reader lifetime.
Findings None None.
Security None None.

How this fits together

Session context loading reads persisted transcript events from each agent’s SQLite database and turns selected events into model-visible conversation history. The eager model-context path feeds agent execution, while full-fidelity callback readers retain a separate lazy path.

flowchart LR
  A[Stored transcript events] --> B[Context navigation snapshot]
  B --> C[Selected model entries]
  C --> D[Bounded payload batches]
  D --> E[Detached model context]
  B --> F[Lazy full-fidelity reader]
  F --> G[Callback consumer]
Loading

Decision needed

Question Recommendation
Should this maintainer-authored P3 session-context performance change land once the exact-head ci-gate is green? Land after green validation: Accept the bounded eager-read implementation after the failed exact-head ci-gate is resolved and rerun successfully.

Why: The PR is marked maintainer and the repository state identifies its author as a MEMBER, so automated cleanup must leave the landing decision to a maintainer despite the absence of source-level findings.

Before merge

  • Resolve merge risk (P1) - The supplied exact-head snapshot reports a failed ci-gate, but provides no failure log; source review cannot establish that the required green validation gate holds.
  • Complete next step (P2) - A maintainer must decide whether to land this protected performance change after the exact-head ci-gate is green.
  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.
Agent review details

Security

None.

PR surface

Source +23, Tests +45. Total +68 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 71 48 +23
Tests 1 46 1 +45
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 117 49 +68

Review metrics

Metric Value Why it matters
Production versus test LOC production +23; tests +45 The production growth is confined to the owner-bound batching and projection logic, while test growth exercises the query-budget and retained context behavior.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep one eager, projected batch-read path inside the existing SQLite snapshot and retain the distinct lazy full-fidelity reader; accept it only after the maintainer confirms the performance tradeoff and exact-head validation is green.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep one eager, projected batch-read path inside the existing SQLite snapshot and retain the distinct lazy full-fidelity reader; accept it only after the maintainer confirms the performance tradeoff and exact-head validation is green.

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

Yes, from source: current main performs one prepared payload query for each selected entry, and the added real-SQLite test exercises a 1,200-message context with an execution-count bound. The test was not run because this is a read-only review.

Is this the best way to solve the issue?

Yes. The implementation keeps projection at the SQLite owner boundary, batches only the eager detached-reader path, and leaves the full-fidelity callback reader lazy; no existing alternative path duplicates this behavior.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 605e68260516.

Labels

Label justifications:

  • P3: This is a scoped performance optimization with no demonstrated user-facing regression or safety impact.
  • 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: Repository state identifies this as a maintainer-authored PR, so the external-contributor proof gate does not apply; its supplied native-SQLite validation claims are supplemental.

Evidence

What I checked:

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-04T10:10:44.434Z sha d058173 :: needs maintainer review before merge. :: none

@steipete
steipete merged commit c861706 into main Sep 4, 2026
363 of 374 checks passed
@steipete
steipete deleted the codex/db-prep-session-context branch September 4, 2026 10:48
@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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