-
Notifications
You must be signed in to change notification settings - Fork 2
memory: deferred tool pair summarization leaves orphaned tool_use messages in DB, stripped on every restore #2243
Description
Summary
When deferred tool pair summarization fires (batch-applied deferred tool summaries applied=N), it replaces tool_result messages with plain-text summaries but leaves the corresponding tool_use messages in the database. On the next session restore, these tool_use messages have no matching tool_result and are stripped as orphaned mid-history pairs.
Reproduction
- Run a long session with
tool_call_cutoff = 6and many memory_save calls - Deferred summarization fires:
tier-0: batch-applied deferred tool summaries applied=6 count_pressure=true - Start a new session — observe many WARN:
stripping orphaned mid-history tool_use parts
Evidence
After the Alice session (CI-211, 19 turns, 9 memory saves):
applied deferred tool pair summaries count=6
tier-0: batch-applied deferred tool summaries applied=6 token_pressure=false count_pressure=true
Next session (CI-212):
WARN stripping orphaned mid-history tool_use parts tool_ids={"call_n8k3ZB450zOj9rhj6pqW1b9y"} index=2
WARN stripping orphaned mid-history tool_result parts tool_use_ids={"call_n8k3ZB450zOj9rhj6pqW1b9y"} index=4
... (12 pairs total stripped)
DB inspection confirms: tool_use message (id=83) contains both tool_use IDs; ids 84-85 are plain-text summaries (tool_result replaced); id=86 is a raw tool_result for only one of the two parallel calls.
Impact
- MEDIUM: Grows unboundedly — each session with deferred summarization adds more orphaned tool_use messages to the DB
- Every subsequent restore pays the cost of stripping N orphaned pairs
- Context history becomes increasingly mismatched between DB records and actual conversation
Fix Direction
When batch_apply_deferred_summaries() replaces tool_result messages with plain-text summaries, it must also:
a) Remove the corresponding tool_use messages from the DB (or mark them as summarized), OR
b) Update the tool_use messages to reference the summary instead of expecting a tool_result
Discovered CI-212 (2026-03-27)