Repository navigation
fix(tasks): keep board snapshots whose anchors survive compaction - #1458
Conversation
updateBoardHistoryState dropped the whole retained board state whenever compaction was detected, starting a new epoch and sequence and busting the prompt cache for snapshots whose anchor messages were still present. Keep the snapshots whose anchors survive, and delete the state only when none do. Fixes alvinunreal#1457
|
| previous.snapshots = previous.snapshots.filter((snapshot) => | ||
| currentAnchorKeys.includes(snapshot.anchorKey), | ||
| ); |
There was a problem hiding this comment.
Anonymous anchors can shift If compaction removes the first of two identical messages without an ID, the surviving message takes the removed message’s anchor key. This filter then keeps the removed message’s board snapshot and replays it after the wrong message, while dropping the survivor’s snapshot. The compacted prompt can show stale board content and lose the cache reuse this change aims to preserve.
Knowledge Base Used: Task sessions and background jobs
| const boardIDs = (r: TurnResult) => | ||
| summarizeInjected(r.injected as never).filter((s) => | ||
| s.startsWith('BOARD('), | ||
| ); | ||
| expect(boardIDs(before).length).toBe(3); | ||
|
|
||
| // Compaction: the oldest messages disappear, later anchors remain. | ||
| const compacted = history.slice(2); | ||
| const after = await runTurn(hook, compacted); | ||
|
|
||
| // Snapshots anchored on surviving messages must be replayed unchanged | ||
| // (same id, so same epoch and sequence), not purged into a new epoch. | ||
| const surviving = boardIDs(before).slice(1); | ||
| expect(boardIDs(after)).toEqual(surviving); | ||
| }, 20_000); |
There was a problem hiding this comment.
Test misses prompt changes This assertion checks board snapshot IDs but not their text or position among real messages. A snapshot could keep its ID while changing the provider-visible prompt or moving to the wrong anchor, and the test would still pass. Comparing the provider-visible payload and snapshot positions would make this regression test cover those cache failures.
Knowledge Base Used: Task sessions and background jobs
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Anonymous anchor keys use an occurrence index, so pruning a duplicate anonymous message made the survivor inherit the removed message's key. Retain only id-backed anchors, and extend the test to cover the provider-visible prefix plus the anonymous-duplicate case. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: drakeo338 <[email protected]>
Fixes #1457
With
backgroundJobs.strategy: "checkpoint-compatible", a shorter message list after a Magic Context compaction madeupdateBoardHistoryStatedelete all retained board snapshots, including ones whose anchor messages were still present, which busted the prompt cache a second time. It now drops only snapshots whose anchors are gone and deletes the state only when none survive. Added a regression test that fails on the base commit and passes with the change;biome checkand typecheck pass.Snapshots anchored to anonymous messages (no id) are not retained, because their occurrence-index key can point at a different message after pruning.