fix: keep post-compaction replies ordered - #10831
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge The fix is well-structured and surgical. The core changes are:
The The tests are real-implementation tests (no mocks) and cover the exact persisted shape described in the PR. The Files Reviewed (11 files)
Reviewed by claude-4.6-sonnet-20260217 · 1,094,062 tokens Review guidance: REVIEW.md from base branch |
fix: keep post-compaction replies ordered
Compaction can project retained historical messages after a newer compact summary so the model receives a useful summary-first context. The prompt loop was also using that projected array position to identify the active turn, which allowed a retained pre-compaction user message to be mistaken for the newest user message. Follow-up tool calls could then be persisted with that historical user as their parent.
The VS Code transcript queue made the storage issue visible. #9195 intentionally grouped assistant output by
parentIDso tool output for a running turn remains above newer queued prompts. That grouping was broader than the queued-prompt case, so assistant messages with stale cross-compaction parents were moved back above the compact summary. The stale parent pattern became reachable after #10507 imported retained-tail compaction ordering from anomalyco/opencode#25851.#10800 made this easier to reproduce by forcing oversized sessions through manual compaction, but it was reverted by #10826. This PR is based on current
mainafter that revert and deliberately does not restore the post-prune payload hard stop. The ordering bug still matters for manual compaction, automatic compaction, and sessions that already persisted stale cross-boundary parents.Reproduction
parentIDfrom the pre-compaction user turn.The persisted shape is equivalent to:
Fix
The CLI now records chronological order before retained-tail projection rearranges model-facing messages. Prompt-loop state selection uses that chronology for the active user, latest assistant, and latest completed assistant while leaving queue projection and FIFO task processing unchanged. Repeated compactions also select the newest completed summary chronologically instead of treating an older retained summary as the newest trim boundary.
The VS Code transcript treats compact requests as barriers. Assistant output encountered after a compact request cannot be reassociated with a user turn before that barrier. The same mapping is used for rendering, active-turn detection, queued-message partitioning, virtualization, and lazy history hydration. This repairs existing persisted sessions in addition to preventing new stale parent links from being created.
The shared upstream-owned edits are intentionally narrow: one pre-projection chronology annotation and the prompt-loop hook. Kilo-specific ordering logic lives under
src/kilocode/to keep the fork diff small. The CLI chronology direction aligns with the upstream work in anomalyco/opencode#29035.