Repository navigation
perf(sessions): avoid rescanning branches after message appends - #159309
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 26, 2026, 11:56 PM ET / September 27, 2026, 03:56 UTC (Revision 2). ClawSweeper reviewWhat this changesThe PR extends cached session branch counts and headlines from newly appended messages, with fallback tests and database-worker documentation. Merge readiness✅ Ready for maintainer review Keep open: current main still rescans the transcript after an append, and the focused patch has credible performance evidence with no confirmed introduced defect. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway's branch-list API reads an agent's SQLite transcript through a read worker. It returns compact branch summaries for the chat branch menu. flowchart LR
A[Saved session messages] --> B[Branch list request]
B --> C[Transcript read worker]
C --> D{Valid cache and linear append?}
D -->|Yes| E[Read new rows]
D -->|No| F[Scan transcript]
E --> G[Branch menu summaries]
F --> G
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +80, Tests +90, Docs +9. Total +179 across 5 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one worker-owned branch-summary cache that updates from verified append suffixes and uses the complete scanner for rewrites or irregular history. Do we have a high-confidence way to reproduce the issue? Yes. Current main visibly scans all navigation rows on an append cache miss, and the PR records a repeatable 50,000-message branch-list benchmark; I did not rerun it in this read-only checkout. Is this the best way to solve the issue? Yes. Extending the existing worker cache with a verified append suffix keeps the optimization at its current owner and preserves full scans for structural changes. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against b5f3444b442c. LabelsLabel changes:
Label 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)
|
What Problem This Solves
Opening or refreshing the session branch menu after a new message rescans the entire transcript, even when the new message only extends the active branch.
User Impact
Branch refreshes after ordinary message appends are much faster for long conversations. Branch ordering, counts, headlines, timestamps, and navigation behavior remain unchanged. First uncached reads and structural changes still require the complete scan; this does not meet a sub-20 ms cold-read target.
Why This Change Was Made
Extend the existing compact summary cache in the transcript read worker. The initial scan certifies identity coverage, unique IDs, and backward ancestry. A later read in the same physical database and rewrite generation reads only the indexed sequence suffix and extends the active count/headline when every new row is an identity-owned message continuing that branch. Rewrites, leaf/reset controls, side branches, and irregular imported graphs use the existing scanner. No new cache owner, configuration, schema, or retained transcript graph; net production growth is 80 lines.
Evidence
A synthetic stored session with 50,000 messages and five branches, exercised through
listSessionBranchesonblacksmith-testboxleasetbx_01m3g3zwptw7p6243mm4jcxs3t(run):The append refresh is approximately 58× faster (98.3% lower mean latency). Persisted cache misses already ran in the transcript worker before this change. The synchronous column measures entry-to-first-yield, not all main-thread work after worker completion. The synthetic fixture is not a live deployment measurement.
Focused validation used
node scripts/run-vitest.mjs <files> --maxWorkers=1:session-accessor.sqlite-branches.test.ts,session-accessor.sqlite-message-cut.test.ts,sessions-rewind.test.ts, andsessions-rewind.storage.test.ts: 97 passed, leasetbx_01m3g54pjfqx4p29g0qbvr8nzx(run). This includes the registered Gateway methods and the deterministic bounded-row assertion.session-accessor.sqlite-branch-summaries.test.ts: 11 passed, leasetbx_01m3g5y0r96bh58gbs0prp3yf7(run). Covers selected ancestors with inactive descendants, headline inheritance, forward references, duplicate IDs, cycles, and snapshot consistency.git diff --checkpassed.Changed-file gate: passed on
tbx_01m3g5y0r96bh58gbs0prp3yf7withnode scripts/check-changed.mjs --base 5fd7c3d95e03c1acbbfe180991309fe99d6a9fa3 -- <five changed paths>(22m17s command time). Production typechecking, all 25 test graphs, lint, formatting, dead-export checks, and repository guards passed. All task-owned leases are stopped.Validation recovery: native Testbox sync hit its two-minute checksum deadline on reused leases; those leases were retired and replaced, never used concurrently. Fresh snapshots needed
pnpm install --frozen-lockfile. Two new fixture cases initially attempted an empty bulk insert and then used the raw event writer for messages; both fixture errors were corrected before the passing run. The initial default changed-file check compared against a moving Testboxorigin/mainand encountered unrelated Slack/UI line limits; the final check pins the lane base5fd7c3d95e03c1acbbfe180991309fe99d6a9fa3and the five changed paths.No live Gateway was modified or restarted. Cold/structural reads and process-local incognito appends retain full-scan behavior.
CI failure investigation
CI run 36286345135 on head
3df6a1dde9ef55f320a6fff435989aea25e02121finished cancelled afterchecks-node-changed-compact-small-19failed; 131 other jobs succeeded. The failure starts inserver.private-completion.test.ts, case “settles private execution without confusing cancellation and failure: provider failed”: a 120-second test timeout followed by a 180-second queue-drain hook timeout. Later cases report the same session's competing work, and the five-minute no-output watchdog eventually terminates the shard. The failing job ran for 2,749 seconds. This is an unresolved failure, not a claimed infrastructure transient.The failed-only path persists a lifecycle error and appends its transcript report through foreground/worker write admission. Source review found no forgotten fixture release and did not establish a safe product repair. The later cleanup errors are consistent with that first producer remaining unsettled. No change to this path is included in this PR.
Bounded reproduction on
blacksmith-testboxleasetbx_01m3gcmv85t7k2tt5n6b27c80m:scripts/ci-run-node-test-shard.mtson the PR head: 92 passed, 125.43 seconds wrapper wall time.f6a0680aab74a0ca2acaa52404895c97f083756b, isolatedserver.private-completion.test.ts: 21 passed, 56.98 seconds wrapper wall time.These probes used bail-after-first-failure without changing test/hook budgets or assertions. They do not prove the hang fixed. Exact-merge scratch worktrees and the lease were cleaned up. CI was not rerun merely to obtain green, and the branch was not refreshed to hide the failure. CI remains red; the next useful evidence is a reproducing private-completion write/cleanup wait trace. Per-file CI timings for this PR's changed tests were not captured; their measured Testbox costs are above.