Repository navigation
improve(sessions): avoid full transcript scans during session checks - #148725
Conversation
Read physical transcript presence from hot events and current cold descriptors in one statement. Reuse the paired session-window mutation facts for terminal freshness checks while preserving observed timestamps, error handling, and caller order. Formatting and independent source reviews through P2 passed. Focused runtime and performance proof follows in the native PR checkout; no measured Gateway speedup is claimed.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
|
Codex review: blocked before merge. Reviewed September 14, 2026, 11:43 PM ET / September 15, 2026, 03:43 UTC (Revision 2). ClawSweeper reviewWhat this changesReplace full transcript statistics queries with narrow presence and timestamp reads for cron persistence, failed-session recovery, and terminal-session freshness checks. Merge readiness⛔ Blocked before merge - 3 items remain Keep open: current main still performs the aggregate reads, and this focused optimization remains useful. No actionable correctness defect was found; the declared performance and parity validation remains unfinished. Priority: P2 Review scores
Verification
How this fits togetherOpenClaw stores session transcripts and lifecycle metadata in agent-scoped SQLite databases. Gateway and cron session checks consult this storage to decide whether to reuse a session and preserve its resume information. flowchart TD
A[Gateway session checks] --> C[Session storage accessor]
B[Cron persistence] --> C
C --> D[Hot events and cold archive descriptors]
C --> E[Session mutation timestamps]
D --> F[Transcript present or absent]
E --> G[Transcript freshness]
F --> H[Preserve existing session decisions]
G --> H
Before merge
Agent review detailsSecurityNone. PR surfaceSource +44, Tests +50. Total +94 across 13 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep session reuse and resume semantics unchanged while making metadata-only checks scale independently of transcript length, supported by workload-specific measurements. Do we have a high-confidence way to reproduce the issue? Yes for the unnecessary aggregate work: current-main callers reach transcript-wide statistics queries for presence or timestamps. An end-to-end performance penalty was not measured in this review. Is this the best way to solve the issue? Yes at the source level: the patch preserves the storage owner and existing decisions, and the message-presence API has different semantics. Performance value still needs the declared comparisons. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against cae155c7e75d. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Keep scoped physical presence and mutation readers in one metadata module, preserving their queries and transaction boundaries. Use a lazy reader map in the cold-storage race test.
|
Merged via squash.
|
…penclaw#148725) * improve(sessions): avoid full transcript scans during session checks Read physical transcript presence from hot events and current cold descriptors in one statement. Reuse the paired session-window mutation facts for terminal freshness checks while preserving observed timestamps, error handling, and caller order. Formatting and independent source reviews through P2 passed. Focused runtime and performance proof follows in the native PR checkout; no measured Gateway speedup is claimed. * refactor(sessions): isolate transcript metadata reads Keep scoped physical presence and mutation readers in one metadata module, preserving their queries and transaction boundaries. Use a lazy reader map in the cold-storage race test.
What Problem This Solves
Cron persistence, failed-session recovery, and terminal-session freshness checks scan an entire transcript even when they only need its presence or mutation timestamps.
User Impact
These checks use narrow database reads while preserving recovery decisions, physical transcript presence, and terminal-session freshness. No schema, configuration, retention, or permission change is included.
The synthetic warm-database comparison below shows a substantial improvement for 4,096-event transcripts, with a small-transcript cost: presence checks at 0–10 events took roughly 4–10 µs more wall time per call. This is a bounded aggregate-work optimization, not a measured production Gateway speedup.
Why This Change Was Made
The canonical transcript metadata reader checks hot events and current cold descriptors in one statement for presence, and exposes the existing paired session-window mutation facts for freshness. Callers retain their error handling and operation order. The full statistics reader remains available where statistics are needed. The new queries compile per call; the baseline statistics query already caches compilation and avoids payload decoding.
Evidence
CPU comparison and tradeoff
2026-09-15, Node 26.7.0 / ABI 147, macOS arm64, one shared host. The baseline uses the unchanged full statistics implementation; the candidate uses the new presence/paired-freshness readers. Four physical-event counts (header first, then custom rows with 256-character payloads), 3 warmup pairs, 10 alternating baseline/candidate rounds, 100 calls per sample: 160 raw samples total.
Counters and lifecycle proof run in separate processes. Timed code uses native JSON.parse; no spies, assertions, RSS capture, setup, or restoration are inside the CPU/wall intervals. Results and authoritative state are checked outside each sample. Process CPU includes background runtime work; overlapping ranges and host noise limit small-difference conclusions.
Values are median [min, max] µs per call:
At 4,096 events, median wall time fell from 481.06 to 32.30 µs for presence and 434.07 to 25.29 µs for freshness. At small counts, per-call compilation can cost more than the avoided scan; no speculative cache was added. These measurements establish bounded reader work and the observed tradeoff, not end-to-end latency or fleet CPU savings.
Proof completed with all 13 source hashes and 194 install inputs unchanged, a clean exact-head tree, and temporary source fixture removed. Completed raw receipt SHA256: f6701f4c9bc03cf5c81744419a4351acbaa579ffffbaa6272707621200059fab. The retained initial parser-failure receipt is linked from it; its failed/restoration-pending status was preserved.
Review resolution and follow-up
The three pending-evidence items in the exact-head ClawSweeper review, and its rank-up suggestion, are addressed by the corrected-head parity and measured small-to-large comparison above. The measured small-transcript overhead is accepted in exchange for removing transcript-length work at these session boundaries. No source changed after that review.
Named follow-up: reuse prepared metadata query compilation through the existing prepareSqliteQueryTakeFirstSync / database-owned WeakMap pattern, then compare the same empty, 1-row, and 10-row cases. That follow-up should remove repeated compilation without adding a separate metadata cache or changing freshness semantics; it is outside this PR.