fix(memory): flush orphaned tool_use DB rows after deferred summarization#2256
Merged
fix(memory): flush orphaned tool_use DB rows after deferred summarization#2256
Conversation
…tion When apply_deferred_summaries() fires, it replaces tool_use/tool_result pairs in the in-memory context with plain-text summaries but never updated the database. On the next session restore, load_history_filtered(agent_visible) loaded the still-visible original pairs, triggering an unbounded stream of WARN "stripping orphaned mid-history tool_use parts" on every subsequent restore. Fix: accumulate (db_ids_to_hide, summary_texts) in two new Vec fields on Agent (deferred_db_hide_ids, deferred_db_summaries) when apply_deferred_summaries() runs, then flush them atomically to SQLite via apply_tool_pair_summaries() which sets agent_visible=0 on the hidden pairs and inserts summary messages in one transaction. flush_deferred_summaries() is called at all async call sites after apply_deferred_summaries() or maybe_soft_compact_mid_iteration() fires. Track db_id per message via a new MessageMetadata.db_id: Option<i64> field (#[serde(skip)], never sent to LLM). Populated from DB on load_history_filtered() and from persist_message() via last_persisted_message_id on Agent. Closes #2243
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2243 — deferred tool pair summarization left orphaned
tool_usemessages in the DB.apply_deferred_summaries()was synchronous and performed no DB writes; it only hid pairs in-memory and inserted summary messages in-memoryload_history_filtered(agent_visible=true)loaded the still-visible original pairs, triggering unboundedWARN stripping orphaned mid-history tool_use partsgrowthFix
db_id: Option<i64>toMessageMetadata(#[serde(skip)], never sent to LLM); populated fromload_history_filtered()and frompersist_message()vialast_persisted_message_idonAgentapply_deferred_summaries()now accumulates(db_ids_to_hide, summary_texts)in two newVecfields onAgentflush_deferred_summaries()async method flushes these atomically to SQLite viaapply_tool_pair_summaries()(single transaction: setsagent_visible=0+ inserts summary messages)maybe_apply_deferred_summaries()andmaybe_soft_compact_mid_iteration()in bothnative.rsandlegacy.rs, and insidemaybe_compact()hard-tier early-exitTest
New unit test
apply_tool_pair_summaries_hides_pairs_and_inserts_summaryinzeph-memory: saves a tool_use + tool_result pair, applies summaries, verifies the pairs are hidden (agent_visible=0) and a summary message is visible in the agent-visible filtered view.Validation
cargo build --workspace --features full— cleancargo +nightly fmt --check— cleancargo clippy --workspace -- -D warnings— cleanzeph-memory829/829,zeph-core1156/1156#[serde(skip)]on db_id, hidden rows not re-exposed)