Repository navigation
perf(agents): reuse prepared context across unrelated session writes - #178602
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 SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex review: needs changes before merge. What this changesThis PR scopes prepared-context invalidation to the physical session and extends the existing reopen benchmark with seeded histories and preparation timings. Example: After hydrating session hydrated-model-context, unrelated-session appends a message to the same database.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 1 item remains Keep this maintainer-authored PR open: it addresses source-verifiable redundant context reads with a focused runtime change, and inspection found no definite introduced correctness defect. Priority: P2 Before merge
FindingsNone. Tests
Agent review detailsHow this fits togetherSessionManager receives bounded transcript snapshots from the history owner and supplies detached conversation context to agent creation; existing database receipts determine whether prepared bytes remain reusable. flowchart LR
A[History owner] --> B[Bounded transcript hydration]
B --> C[SessionManager prepared context]
D[Session scoped write receipt] --> C
C --> E[Current writer and reader checks]
E --> F[Agent model context]
Technical reviewBest possible solution: Use the existing physical-session receipt at both prepared-context boundaries while retaining live authority checks and conservative invalidation. Do we have a high-confidence way to reproduce the issue? The base compares a database-wide write token, so an unrelated committed session write forces the fallback context reader; the added interleaved regression exercises that path, but no target code was executed during this read-only review. Is this the best way to solve the issue? The runtime fix uses the existing freshness owner and changes both token boundaries together; benchmark additions support measuring the affected concurrent reopen flow without introducing another runtime cache. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against fc374815c3e8. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +6, Tests +33, Docs +29, Other +202. Total +270 across 7 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 20, 2026, 1:44 PM ET / 17:44 UTC. |
|
The remaining compaction-replay failure is inherited. CI run 39466279051 tested merge Both concurrent, swapped-core Gateway pairs are complete, with 100 first-text samples and 100 append-only transcript checks per side. The body now records all latency, CPU, promotion, GC, and heap results. There is no consistent median latency gain, and both baseline and candidate miss the long-history 10% heap-growth goal. This first step removes the independently reproduced false invalidation; it does not claim to solve the remaining cold-worker or retained-fingerprint costs. @clawsweeper re-review |
|
🦞👀 Re-review progress:
|
What Problem This Solves
Reopening a session can repeat transcript preparation when another session writes to the same database between hydration and model-context construction.
Related: #178536.
User Impact
Concurrent sessions can reuse their already-prepared context without rereading unchanged transcript data. Transcript contents, authority checks, and idle-worker retirement remain unchanged. No migration or configuration change is needed.
Why This Change Was Made
The prepared context used a database-wide write receipt even though its readers select one physical session ID. It now uses the existing session-scoped receipt, which still invalidates on same-session writes, unknown writes, schema changes, and database replacement. No new cache or retention policy is introduced.
The benchmark now separates provider arrival, synthetic cohort-barrier wait, and first assistant text. It can seed 655-entry measured histories through the transcript owner and paginate integrity verification after reopening, avoiding pre-reopen history reads that warmed the workers. The original workload created fresh measured sessions regardless of the background roster's history size.
Evidence
Paired measurements
The isolated real Gateway uses a deterministic loopback SSE provider and the real read tool: 100 sessions in cohorts of 10, 128 KiB prompts, 655 seeded transcript entries, then six minutes idle. Two concurrent baseline/candidate pairs ran on separate 16-core halves under the shared exclusive measurement lock, swapping halves for pair 2. Fixtures and instrumentation match; states and ports are independent. These isolate the receipt change on the same source base.
There is no consistent median speedup: pair 1 improves by 0.8%, while pair 2 is 5.7% slower. Both tails improve slightly. This is a small work-elimination step, not a fix for the dominant latency or a claim of a material end-to-end gain.
Each side captured 100 first-text samples and passed all 100 transcript-prefix/answer checks. The long-history absolute idle-heap target of 110% of baseline is not met by either version in either pair. The patch adds no retained cache; its demonstrated benefit is avoiding the redundant read in the regression. Pair 2 also uses 13% more main-thread CPU and promotes 193 MB more; the small sample does not establish a CPU/GC improvement.
A separate source-Gateway attribution probe with the same 100 long histories, but a smaller background roster, found zero live SessionManager, AgentSession, and Agent instances after idle. Invoking existing cleanup owners only in that synthetic probe released 118.50 MiB of JavaScript heap from prompt-cache observations. Fork snapshots released another 1.10 MiB of heap and 65.03 MiB of array-buffer storage. This attributes retained memory; it is not a proposed production cleanup policy or a passing memory acceptance run.
The original short-history diagnostic reproduces 2839 ms p50 / 14362 ms p95. The first cold cohort owns the tail. Checkpoint p95 deltas are 4001 ms for the region ending at session-resource-loader, 2603 ms for AgentSession construction, 198 ms for history-window selection, 478 ms for model dispatch, 7 ms for tool catalog, and 2 ms for system prompt. The resource-loader checkpoint includes lifecycle and transcript hydration; these are not isolated function timings. Transcript-worker aggregate queue time rises from 2.44 s during touch to 97.02 s during reopen. Source workers restart through the TypeScript loader after idle; a bundled-runtime comparison remains necessary before attributing all of this tail to production JavaScript workers.
These synthetic SSE runs do not measure real provider WebSocket reconnects, prompt-cache hits, or network inference latency. First text is timestamped inside the Gateway and excludes later client delivery. Sessions were already rebuilt/disposed per attempt before #178536; that fix removed references pinning disposed objects.
CI attribution
The only failing test job is
checks-node-changed, intest/gateway-openai-compaction-replay.e2e.test.ts. The aggregate CI gate reflects that failure; the other completed test/type/lint/security jobs passed.The failing CI checkout is merge
6d2eaa81543d9d3d6d405da6d5c70559a41da25d, with main parent051bf6fa4d05e0870d0f360aee7ac1643cbb787dand PR head75c2531b9e825da1b494bb0ccb213d4b936416f8. In an isolated sandbox, both exact source trees passedpnpm build qaRuntime; both then reproduced the same assertion under the pinned Bun runtime with:The continuation instruction appears twice in the third provider request. The retry-persistence path and failing assertion are unchanged by this PR. The reproduction changes only the fixture's loopback ports to avoid other processes; the initial baseline attempt hit a port collision and was discarded as setup failure, then repeated on a free port above the ephemeral range. No CI reruns were requested. This is an attributed inherited failure, not a passing test.