Repository navigation
improve: reduce CPU overhead during session imports - #133689
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 30, 2026, 9:36 PM ET / August 31, 2026, 01:36 UTC. ClawSweeper reviewWhat this changesThe PR reuses compilation of the SQLite transcript-identity query within each synchronous session-import append batch while retaining fresh per-event reads. Merge readinessKeep open for ordinary maintainer handling. The prepared reader is batch-local, rebinds each event ID, and still executes a fresh lookup after each append; no introduced correctness or security defect was found. Priority: P3 Review scores
Verification
How this fits togetherSession imports append historical events to an agent’s SQLite transcript store. Identity lookups decide whether an event is already present before the event, its parent relationship, and sequence metadata are written. flowchart LR
A[Session history import] --> B[Transcript append batch]
B --> C[Prepared identity lookup]
C --> D{Event already stored?}
D -->|Yes| E[Skip duplicate]
D -->|No| F[Write event and identity]
F --> G[SQLite transcript store]
Decision needed
Why: The protected maintainer label requires explicit maintainer handling even though the source review found no patch defect. Before merge
Agent review detailsSecurityNone. PR surfaceSource +18. Total +18 across 5 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the batch-local prepared reader and land it after exact-head checks confirm the supplied import parity and performance evidence. Do we have a high-confidence way to reproduce the issue? Not applicable: this is a performance optimization, not a report of broken behavior. The supplied before/after import evidence and source-level parity coverage are the relevant verification path. Is this the best way to solve the issue? Yes. Reusing compilation at the synchronous batch owner mirrors the existing prepared write/update cursor pattern while preserving fresh database reads for every identity check. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 67433ff43813. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Related: #133641
What Problem This Solves
Large session-history imports repeatedly rebuild and compile the same event-identity query, even though only the event ID changes within a session batch. This consumes CPU on top of the SQLite lookup itself.
Why This Change Was Made
Compile the existing identity lookup once per synchronous append batch through
prepareSqliteQuerySync, binding fresh event IDs and reading current database rows on every call. Move the canonical identity reader into the existing transcript-read module and update its internal callers; there is no second query path or result cache.User Impact
Lower CPU overhead for importing histories with event IDs. Duplicate detection, replay ownership, parent links, sequence numbers, opaque data and migration semantics stay unchanged. No schema, index, concurrency, configuration, dependency or public SDK change.
Evidence
node scripts/run-vitest.mjs src/config/sessions/session-accessor.sqlite-import.test.ts src/config/sessions/session-accessor.sqlite-transcript-store.test.ts src/config/sessions/session-transcript-context-eligibility.test.ts src/config/sessions/session-accessor.test.ts src/infra/kysely-sync.test.ts src/config/sessions/session-accessor.conformance.test.ts src/config/sessions/session-accessor.parent-fork.test.ts src/config/sessions/session-accessor.sqlite-message-cut.test.ts.src/logging/logger.ts; sharedtsloglackstypes/BaseLogger.d.ts. Shared dependencies were not modified; exact-head CI passed.The private JSONL line-wrapper cleanup was also inspected, but not included: it removes a small allocation while this query compilation has a measured CPU cost. Doctor pre-count removal remains a separate error/report-ownership refactor.
Linux before/after imports
Candidate
bd2175131d4c9e1f6c8cd450eab6c4d11ce9ba39; baseline83b61257d6e328d904e7ed0c99ee50b661bbfe6d. Linux Node 24.19.0 / SQLite 3.53.3. Five alternating before/after pairs per shape, each in a fresh process. Fixtures and parity verification are outside the timer. These are complete file-to-SQLite imports.The clearest gains are 100,000-event deep histories (8.1% less wall time, 8.3% less CPU) and legacy hook histories (12.7% less wall time, 12.0% less CPU). Smaller multi-session/v1 cases were noisy; the table reports medians without claiming a uniform speedup. Already-imported replay is effectively neutral.
All 45 pairs preserve exact transcript JSON, identities, session windows, active positions, index state and FTS digests. SQL trace is identical: 100,034 calls across 34 shapes. Eight CPU profiles cover the timed import region; sampled Kysely self-time falls from 1387.5 to 1040.4 ms in the deep profile, consistent with removing repeated AST compilation. Profile sampling is supporting evidence, not the timing benchmark.
Deep-case median peak RSS is 492.7 → 492.9 MiB. The branch case is 371.8 → 395.6 MiB; no general memory reduction is claimed. All raw samples are retained.
The harness overrides every changed module reachable from the import entry point with its baseline source, leaving all other source/dependencies identical. Four changed modules are reachable; the checkpoint import-only change is not in that bundle and is covered by the passing checkpoint/branch tests. An initial harness assertion incorrectly required all five changed files to be reachable and stopped before timing; it now verifies the actual bundle inputs and records the unused file explicitly.
Raw results/profile archive SHA-256:
cc83f499372d77b827a4ab5bd04a575ac960d1aff9f7ab749ee4f36061b33afb. Dedicated Testbox workflow; exact-head CI. The benchmark command exited 0 in 211 seconds.Native review compared current main
5de1376b7f62: none of the five changed files differ from the benchmark baseline. Its newer retained-history optimization touches separate modules.Packaged stable upgrade and landing gates
The existing
published-upgrade-survivorDocker lane passed from stable2026.7.1-2to the candidate tarball; build info confirms exact commitbd2175131d4c9e1f6c8cd450eab6c4d11ce9ba39. It preserved 4,800 sessions, 1,227,946 events and 10,000 cron jobs. Automatic migration passed immediately after the update, before the standalone Doctor check. All survival assertions passed; eight Gateway history samples (600 messages across two agents) remained identical after restart. Repeat Doctor completed in 38 seconds within its 60-second budget. The lane took 505 seconds; build/package/test command took 779 seconds.The test uses an explicit candidate tarball, manual Gateway restart and expected synthetic plugin-capability consent. It verifies automatic data migration, not unattended update-channel switching. The volume fixture uses v3 transcript headers; legacy-format performance is proved separately by the paired imports.
Docker proof archive SHA-256:
a43f5670e25aac4d70511df27dd1d77b948cc0881c4a44b045b3185911b23942. The dedicated worker has been stopped after artifact collection.Exact-head CI is green, and native preparation accepted the same tested commit without a rebase or additional push. The latest ClawSweeper review (2026-08-31 01:36 UTC) has no source correctness/security finding or rank-up moves; its incomplete-CI concern is resolved by the completed exact-head run and native gates. Maintainer handling proceeds under the requested performance-pass-and-land workflow.