Skip to content

fix(memory): flush orphaned tool_use DB rows after deferred summarization#2256

Merged
bug-ops merged 1 commit intomainfrom
2243-memory-orphaned-tool-use
Mar 27, 2026
Merged

fix(memory): flush orphaned tool_use DB rows after deferred summarization#2256
bug-ops merged 1 commit intomainfrom
2243-memory-orphaned-tool-use

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Mar 27, 2026

Summary

Fixes #2243 — deferred tool pair summarization left orphaned tool_use messages in the DB.

  • apply_deferred_summaries() was synchronous and performed no DB writes; it only hid pairs in-memory and inserted summary messages in-memory
  • On the next session restore, load_history_filtered(agent_visible=true) loaded the still-visible original pairs, triggering unbounded WARN stripping orphaned mid-history tool_use parts growth

Fix

  • Added db_id: Option<i64> to MessageMetadata (#[serde(skip)], never sent to LLM); populated from load_history_filtered() and from persist_message() via last_persisted_message_id on Agent
  • apply_deferred_summaries() now accumulates (db_ids_to_hide, summary_texts) in two new Vec fields on Agent
  • New flush_deferred_summaries() async method flushes these atomically to SQLite via apply_tool_pair_summaries() (single transaction: sets agent_visible=0 + inserts summary messages)
  • Flush is called at all async call sites after deferred summaries may have been applied: after maybe_apply_deferred_summaries() and maybe_soft_compact_mid_iteration() in both native.rs and legacy.rs, and inside maybe_compact() hard-tier early-exit

Test

New unit test apply_tool_pair_summaries_hides_pairs_and_inserts_summary in zeph-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

  • Build: cargo build --workspace --features full — clean
  • Fmt: cargo +nightly fmt --check — clean
  • Clippy: cargo clippy --workspace -- -D warnings — clean
  • Tests: zeph-memory 829/829, zeph-core 1156/1156
  • Security audit: approved (parameterized SQL, #[serde(skip)] on db_id, hidden rows not re-exposed)
  • Performance: approved (sub-ms flush, single transaction, no hot-path regression)
  • Adversarial critique: minor verdict (no blocking issues)

…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
@github-actions github-actions bot added documentation Improvements or additions to documentation bug Something isn't working llm zeph-llm crate (Ollama, Claude) memory zeph-memory crate (SQLite) rust Rust code changes core zeph-core crate size/L Large PR (201-500 lines) labels Mar 27, 2026
@bug-ops bug-ops enabled auto-merge (squash) March 27, 2026 11:45
@bug-ops bug-ops merged commit bcd7788 into main Mar 27, 2026
25 checks passed
@bug-ops bug-ops deleted the 2243-memory-orphaned-tool-use branch March 27, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core zeph-core crate documentation Improvements or additions to documentation llm zeph-llm crate (Ollama, Claude) memory zeph-memory crate (SQLite) rust Rust code changes size/L Large PR (201-500 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

memory: deferred tool pair summarization leaves orphaned tool_use messages in DB, stripped on every restore

1 participant