Repository navigation
fix(agents): keep the replayed user turn in history after compaction - #204313
Conversation
A retry after mid-turn compaction continued through a persisted hidden "continue the current task" turn, and the user's question was gone from history because compaction summarized it with the split turn. - continueFromCurrentTranscript decides the mode: a persisted keyed turn is replayed in place unless the attempt left an unsettled tail. - After a compaction, the replay writes a copy of the turn after the kept tail (derived per-compaction key, display: false), so the next turn's history holds the question and extends the retry request byte for byte. - A replayed turn keeps its persisted timestamp. Co-authored-by: obviyus <[email protected]>
|
🦞👀 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. |
A retry after the same compaction found the replay copy under its derived key while submission looked up the original turn key, so it appended the question again. Resolve the key under which the current turn sits in live context in one place and use it for submission and the compaction budget. The copy now stays in live history and is replayed in place, which also keeps its persisted timestamp. Co-authored-by: obviyus <[email protected]>
|
Codex review: needs changes before merge. What this changesThis PR persists compacted user-turn replays and reuses their keys during later retries and prompt budgeting. Example: “First question after the long history.” crosses a mid-turn compaction and a rate-limit retry.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 1 item remains Keep open: this maintainer-authored PR addresses a concrete history defect, but a second compaction can cause it to persist another copy of an already-retained question. Priority: P2 Before merge
Findings
Tests
Agent review detailsHow this fits togetherThe embedded runner restores persisted session history, chooses replay or continuation, and submits the resulting messages through the provider transport. flowchart TD
A[Accepted user turn] --> B[Session transcript]
B --> C[Compaction]
C --> D[Replay preparation]
D --> B
D --> E[Prompt submission]
E --> F[Provider transport]
B --> G[Next turn history]
Technical reviewBest possible solution: Reuse the existing context-key resolver before appending so any retained replay satisfies the current turn, while a summarized-away replay still receives one replacement. Do we have a high-confidence way to reproduce the issue? Source establishes the hidden-continuation path on main and the repeated-compaction duplication in the patch; this read-only review executed no reproduction. Is this the best way to solve the issue? Persisting the model-visible replay through the existing transcript owner is appropriate, but replay detection must recognize copies retained across compaction boundaries. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 7cbf1585ed73. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceAcceptance criteria:
What I checked:
Likely related people:
PR surfaceSource +91, Tests +112. Total +203 across 14 files. Added test files: 1. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. 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 November 23, 2026, 9:19 PM ET / November 24, 2026, 02:19 UTC. |
|
Maintainer override: all 30 red test files on head 36a2a52 (PR CI run 45309489715, both attempts) also fail on the merge base. Each file is named below with its base evidence. None is caused by this PR. Merge base: main
|
|
Landed. After mid-turn compaction, the retry now replays the user's keyed question in place, and the question stays in history for the next turn, instead of continuing through a persisted hidden prompt. The next turn caches the whole cacheable retry prefix (table in the description). Every red check is named in the override comment above with merge-base evidence. |
…204313) ## What Problem This Solves After mid-turn compaction, the retry that crosses the compaction boundary continued through a hidden user turn ("Continue the current task from the existing transcript…") plus a runtime carrier. That prompt was persisted. Every such retry added another hidden user turn, and the user's own question was gone from history: compaction had summarized it as part of the split turn. ## User Impact - The question the user asked stays in the conversation history after compaction. The next turn sees it verbatim, instead of a hidden "continue" instruction. - No hidden continuation turn is added after compaction when the user's turn can be replayed. - The next turn's request extends the retry request byte for byte, so the provider prompt cache keeps working across the turn. ## Why This Change Was Made - **One owner decides the retry mode.** `continueFromCurrentTranscript` (`session-prompt-state.ts`) now replays a persisted keyed user turn in place unless the attempt left an unsettled tail (partial output, or an operator abort mid-flight; callers pass `unsettledTail`). The caller-chosen `replayCurrentUserTurn` option is removed. The hidden prompt stays only for non-replayable turns. - **The replayed turn is written where the model sees it.** `persistCompactedCurrentUserTurnReplay` (`pre-persisted-user-turn.ts`), called from the replay branch of `prepareEmbeddedAttemptSessionBoundary`, appends the turn after the kept tail when a compaction summarized it. The original row keeps the turn key, because the transcript identity index is unique per session. The copy uses a key derived from the turn key and the compaction entry id, so repeated retries after the same compaction write it once, and a second compaction writes one new copy. It is `display: false`, so the chat still shows the question once. - **Submission replays that row in place.** `resolveCurrentUserTurnContextKey` returns the key under which the current turn sits in live context (its own row or its replay copy). Prompt submission and the compaction budget use it, so a later retry (for example after a rate limit) reuses the copy and its carrier instead of appending the question again. - Accepted edge, no code: if the process crashes between the compaction commit and the copy's write, the restart writes no copy for that run. The question stays in the original row and in the summary. ## Evidence Real-API replay (gpt-4.1-mini, captured wire pair: the retry after compaction, then the first request of the next turn; fresh seed and cache key per trial). The metric that does not depend on request length is whether the next turn reuses the whole cacheable retry prefix. | Arm, trial | Retry input | Next-turn input | Next-turn cached | Next-turn uncached | New tokens in next turn | Retry prefix fully cached | Question in next-turn history | |---|---|---|---|---|---|---|---| | main, 1 | 9,071 | 9,153 | 8,960 | 193 | 82 | yes | no (hidden prompt instead) | | main, 2 | 9,073 | 9,155 | 8,960 | 195 | 82 | yes | no | | this PR, 1 | 8,995 | 9,077 | 8,832 | 245 | 82 | yes | yes | | this PR, 2 | 8,998 | 9,080 | 8,832 | 248 | 82 | yes | yes | | this PR, 3 | 8,999 | 9,081 | 8,960 | 121 | 82 | yes | yes | | this PR, 4 | 8,993 | 9,075 | 8,832 | 243 | 82 | yes | yes | | replay without persistence (rejected design) | 9,007 | 9,059 | 7,936 | 1,123 | 52 | no (breaks at the user turn) | no | - The provider caches in 128-token blocks. Next-turn uncached = the 82 new tokens (the same in both arms) + the retry's tail inside its last partial block. That tail depends only on where the retry length falls, not on changed bytes. - "Fully cached" means the next turn caches as much as resending the identical retry request. The control resend caches 8,832 for this PR's retry (8,996 tokens) and 8,960 for main's (9,072 tokens, 76 tokens longer because of the hidden prompt). The next request's input starts with the retry's full input byte for byte. Tests: - `agent-runner-compaction-fallback.test.ts` (mid-turn case) checks: - the warm summary request equals the previous request plus one instruction; - a rate-limited first retry and the second retry after the same compaction send the identical request, with the keyed turn once and no hidden prompt; - the next turn's input starts with the retry's input and holds the question once. It is red on `main` (hidden prompt) and on the previous head of this PR (the rate-limited retry), and green here. - The fixture's stale `largestPrompt < 800_000` check moves to the end of the test, unchanged, so the new assertions run first. It fails on `main` too (851,263 at the 85% trigger) and has its own owner. - `embedded-agent-runner.prompt-cache.test.ts`: the fixture window is raised so its journey stays below the compaction trigger. This test covers prefix stability; the compaction-fallback test owns the warm summary shape. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 39bdddd) Co-authored-by: RomneyDa <[email protected]>
…penclaw#204313) ## What Problem This Solves After mid-turn compaction, the retry that crosses the compaction boundary continued through a hidden user turn ("Continue the current task from the existing transcript…") plus a runtime carrier. That prompt was persisted. Every such retry added another hidden user turn, and the user's own question was gone from history: compaction had summarized it as part of the split turn. ## User Impact - The question the user asked stays in the conversation history after compaction. The next turn sees it verbatim, instead of a hidden "continue" instruction. - No hidden continuation turn is added after compaction when the user's turn can be replayed. - The next turn's request extends the retry request byte for byte, so the provider prompt cache keeps working across the turn. ## Why This Change Was Made - **One owner decides the retry mode.** `continueFromCurrentTranscript` (`session-prompt-state.ts`) now replays a persisted keyed user turn in place unless the attempt left an unsettled tail (partial output, or an operator abort mid-flight; callers pass `unsettledTail`). The caller-chosen `replayCurrentUserTurn` option is removed. The hidden prompt stays only for non-replayable turns. - **The replayed turn is written where the model sees it.** `persistCompactedCurrentUserTurnReplay` (`pre-persisted-user-turn.ts`), called from the replay branch of `prepareEmbeddedAttemptSessionBoundary`, appends the turn after the kept tail when a compaction summarized it. The original row keeps the turn key, because the transcript identity index is unique per session. The copy uses a key derived from the turn key and the compaction entry id, so repeated retries after the same compaction write it once, and a second compaction writes one new copy. It is `display: false`, so the chat still shows the question once. - **Submission replays that row in place.** `resolveCurrentUserTurnContextKey` returns the key under which the current turn sits in live context (its own row or its replay copy). Prompt submission and the compaction budget use it, so a later retry (for example after a rate limit) reuses the copy and its carrier instead of appending the question again. - Accepted edge, no code: if the process crashes between the compaction commit and the copy's write, the restart writes no copy for that run. The question stays in the original row and in the summary. ## Evidence Real-API replay (gpt-4.1-mini, captured wire pair: the retry after compaction, then the first request of the next turn; fresh seed and cache key per trial). The metric that does not depend on request length is whether the next turn reuses the whole cacheable retry prefix. | Arm, trial | Retry input | Next-turn input | Next-turn cached | Next-turn uncached | New tokens in next turn | Retry prefix fully cached | Question in next-turn history | |---|---|---|---|---|---|---|---| | main, 1 | 9,071 | 9,153 | 8,960 | 193 | 82 | yes | no (hidden prompt instead) | | main, 2 | 9,073 | 9,155 | 8,960 | 195 | 82 | yes | no | | this PR, 1 | 8,995 | 9,077 | 8,832 | 245 | 82 | yes | yes | | this PR, 2 | 8,998 | 9,080 | 8,832 | 248 | 82 | yes | yes | | this PR, 3 | 8,999 | 9,081 | 8,960 | 121 | 82 | yes | yes | | this PR, 4 | 8,993 | 9,075 | 8,832 | 243 | 82 | yes | yes | | replay without persistence (rejected design) | 9,007 | 9,059 | 7,936 | 1,123 | 52 | no (breaks at the user turn) | no | - The provider caches in 128-token blocks. Next-turn uncached = the 82 new tokens (the same in both arms) + the retry's tail inside its last partial block. That tail depends only on where the retry length falls, not on changed bytes. - "Fully cached" means the next turn caches as much as resending the identical retry request. The control resend caches 8,832 for this PR's retry (8,996 tokens) and 8,960 for main's (9,072 tokens, 76 tokens longer because of the hidden prompt). The next request's input starts with the retry's full input byte for byte. Tests: - `agent-runner-compaction-fallback.test.ts` (mid-turn case) checks: - the warm summary request equals the previous request plus one instruction; - a rate-limited first retry and the second retry after the same compaction send the identical request, with the keyed turn once and no hidden prompt; - the next turn's input starts with the retry's input and holds the question once. It is red on `main` (hidden prompt) and on the previous head of this PR (the rate-limited retry), and green here. - The fixture's stale `largestPrompt < 800_000` check moves to the end of the test, unchanged, so the new assertions run first. It fails on `main` too (851,263 at the 85% trigger) and has its own owner. - `embedded-agent-runner.prompt-cache.test.ts`: the fixture window is raised so its journey stays below the compaction trigger. This test covers prefix stability; the compaction-fallback test owns the warm summary shape. Co-authored-by: Ayaan Zaidi <[email protected]>
…204313) After mid-turn compaction, the retry that crosses the compaction boundary continued through a hidden user turn ("Continue the current task from the existing transcript…") plus a runtime carrier. That prompt was persisted. Every such retry added another hidden user turn, and the user's own question was gone from history: compaction had summarized it as part of the split turn. - The question the user asked stays in the conversation history after compaction. The next turn sees it verbatim, instead of a hidden "continue" instruction. - No hidden continuation turn is added after compaction when the user's turn can be replayed. - The next turn's request extends the retry request byte for byte, so the provider prompt cache keeps working across the turn. - **One owner decides the retry mode.** `continueFromCurrentTranscript` (`session-prompt-state.ts`) now replays a persisted keyed user turn in place unless the attempt left an unsettled tail (partial output, or an operator abort mid-flight; callers pass `unsettledTail`). The caller-chosen `replayCurrentUserTurn` option is removed. The hidden prompt stays only for non-replayable turns. - **The replayed turn is written where the model sees it.** `persistCompactedCurrentUserTurnReplay` (`pre-persisted-user-turn.ts`), called from the replay branch of `prepareEmbeddedAttemptSessionBoundary`, appends the turn after the kept tail when a compaction summarized it. The original row keeps the turn key, because the transcript identity index is unique per session. The copy uses a key derived from the turn key and the compaction entry id, so repeated retries after the same compaction write it once, and a second compaction writes one new copy. It is `display: false`, so the chat still shows the question once. - **Submission replays that row in place.** `resolveCurrentUserTurnContextKey` returns the key under which the current turn sits in live context (its own row or its replay copy). Prompt submission and the compaction budget use it, so a later retry (for example after a rate limit) reuses the copy and its carrier instead of appending the question again. - Accepted edge, no code: if the process crashes between the compaction commit and the copy's write, the restart writes no copy for that run. The question stays in the original row and in the summary. Real-API replay (gpt-4.1-mini, captured wire pair: the retry after compaction, then the first request of the next turn; fresh seed and cache key per trial). The metric that does not depend on request length is whether the next turn reuses the whole cacheable retry prefix. | Arm, trial | Retry input | Next-turn input | Next-turn cached | Next-turn uncached | New tokens in next turn | Retry prefix fully cached | Question in next-turn history | |---|---|---|---|---|---|---|---| | main, 1 | 9,071 | 9,153 | 8,960 | 193 | 82 | yes | no (hidden prompt instead) | | main, 2 | 9,073 | 9,155 | 8,960 | 195 | 82 | yes | no | | this PR, 1 | 8,995 | 9,077 | 8,832 | 245 | 82 | yes | yes | | this PR, 2 | 8,998 | 9,080 | 8,832 | 248 | 82 | yes | yes | | this PR, 3 | 8,999 | 9,081 | 8,960 | 121 | 82 | yes | yes | | this PR, 4 | 8,993 | 9,075 | 8,832 | 243 | 82 | yes | yes | | replay without persistence (rejected design) | 9,007 | 9,059 | 7,936 | 1,123 | 52 | no (breaks at the user turn) | no | - The provider caches in 128-token blocks. Next-turn uncached = the 82 new tokens (the same in both arms) + the retry's tail inside its last partial block. That tail depends only on where the retry length falls, not on changed bytes. - "Fully cached" means the next turn caches as much as resending the identical retry request. The control resend caches 8,832 for this PR's retry (8,996 tokens) and 8,960 for main's (9,072 tokens, 76 tokens longer because of the hidden prompt). The next request's input starts with the retry's full input byte for byte. Tests: - `agent-runner-compaction-fallback.test.ts` (mid-turn case) checks: - the warm summary request equals the previous request plus one instruction; - a rate-limited first retry and the second retry after the same compaction send the identical request, with the keyed turn once and no hidden prompt; - the next turn's input starts with the retry's input and holds the question once. It is red on `main` (hidden prompt) and on the previous head of this PR (the rate-limited retry), and green here. - The fixture's stale `largestPrompt < 800_000` check moves to the end of the test, unchanged, so the new assertions run first. It fails on `main` too (851,263 at the 85% trigger) and has its own owner. - `embedded-agent-runner.prompt-cache.test.ts`: the fixture window is raised so its journey stays below the compaction trigger. This test covers prefix stability; the compaction-fallback test owns the warm summary shape. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 39bdddd) Release adaptation: Preserve the stable ChatGPT V2 compaction boundary and test the cold-summary retry, without importing the warm-summary feature or its unrelated assertion. Companion audit: #204443 changes only fixture sizing; stable test retains its pre-existing budget assertion. #207519 consolidates separate output-limit tests; #208522 is separately ordered in this lane; #204111 warm summaries are not needed for the stable cold path. Co-authored-by: RomneyDa <[email protected]>
What Problem This Solves
After mid-turn compaction, the retry that crosses the compaction boundary continued through a hidden user turn ("Continue the current task from the existing transcript…") plus a runtime carrier. That prompt was persisted. Every such retry added another hidden user turn, and the user's own question was gone from history: compaction had summarized it as part of the split turn.
User Impact
Why This Change Was Made
continueFromCurrentTranscript(session-prompt-state.ts) now replays a persisted keyed user turn in place unless the attempt left an unsettled tail (partial output, or an operator abort mid-flight; callers passunsettledTail). The caller-chosenreplayCurrentUserTurnoption is removed. The hidden prompt stays only for non-replayable turns.persistCompactedCurrentUserTurnReplay(pre-persisted-user-turn.ts), called from the replay branch ofprepareEmbeddedAttemptSessionBoundary, appends the turn after the kept tail when a compaction summarized it. The original row keeps the turn key, because the transcript identity index is unique per session. The copy uses a key derived from the turn key and the compaction entry id, so repeated retries after the same compaction write it once, and a second compaction writes one new copy. It isdisplay: false, so the chat still shows the question once.resolveCurrentUserTurnContextKeyreturns the key under which the current turn sits in live context (its own row or its replay copy). Prompt submission and the compaction budget use it, so a later retry (for example after a rate limit) reuses the copy and its carrier instead of appending the question again.Evidence
Real-API replay (gpt-4.1-mini, captured wire pair: the retry after compaction, then the first request of the next turn; fresh seed and cache key per trial). The metric that does not depend on request length is whether the next turn reuses the whole cacheable retry prefix.
Tests:
agent-runner-compaction-fallback.test.ts(mid-turn case) checks:It is red on
main(hidden prompt) and on the previous head of this PR (the rate-limited retry), and green here.largestPrompt < 800_000check moves to the end of the test, unchanged, so the new assertions run first. It fails onmaintoo (851,263 at the 85% trigger) and has its own owner.embedded-agent-runner.prompt-cache.test.ts: the fixture window is raised so its journey stays below the compaction trigger. This test covers prefix stability; the compaction-fallback test owns the warm summary shape.