Release persisted MCP tool results from active-turn memory (#62641)

## Why

Active turns in paginated threads retain completed MCP tool results in
memory even when history can supply the same payloads. History can also
store a truncated preview, so dropping a result requires checking that
the full payload is recoverable.

## What changed

For persisted paginated threads, check successful MCP calls in batches
and release an active-turn result only when the stored result matches
it exactly. When resuming with full history, fetch released results by
item ID so a stale history page cannot supply an older result.

## Testing

Add a regression test that restores the latest result despite a stale
page and checks that truncated, metadata-mismatched, missing, and failed
results remain in memory.

GitOrigin-RevId: 49f6f1414201e4327433043050d65a1f4adc9720
This commit is contained in:
Brent Traut
2026-11-20 01:49:15 +00:00
committed by copyberry
parent e720e6c80b
commit 7642e80723
9 changed files with 281 additions and 27 deletions
@@ -18,6 +18,7 @@ use crate::protocol::v2::McpToolCallError;
use crate::protocol::v2::McpToolCallResult;
use crate::protocol::v2::McpToolCallStatus;
use crate::protocol::v2::ThreadItem;
use crate::protocol::v2::ThreadItemEntry;
use crate::protocol::v2::ThreadItemPayload;
use crate::protocol::v2::Turn;
use crate::protocol::v2::TurnError as V2TurnError;
@@ -430,6 +431,32 @@ impl ThreadHistoryBuilder {
}
}
/// Drops only completed MCP results that the active turn can recover in full by ID.
pub fn release_saved_mcp_results(&mut self, entries: &[ThreadItemEntry]) {
let Some(turn) = self.current_turn.as_mut() else {
return;
};
for entry in entries {
if entry.turn_id == turn.id
&& let ThreadItemPayload::McpToolCall {
id,
status: McpToolCallStatus::Completed,
result: Some(saved),
..
} = &entry.item.payload
&& let Some(index) = turn.item_index.position(&turn.items, id)
&& let ThreadItemPayload::McpToolCall {
status: McpToolCallStatus::Completed,
result,
..
} = &mut turn.items[index]
&& result.as_ref() == Some(saved)
{
*result = None;
}
}
}
/// Shared reducer for persisted rollout replay and in-memory current-turn
/// tracking used by running thread resume/rejoin.
///
@@ -1893,11 +1920,7 @@ impl TurnItemIndex {
items.push(item);
}
fn upsert<'a>(
&mut self,
items: &'a mut Vec<ThreadItemPayload>,
item: ThreadItemPayload,
) -> &'a ThreadItemPayload {
fn position(&mut self, items: &[ThreadItemPayload], id: &str) -> Option<usize> {
if self.positions.is_none() && items.len() >= TURN_ITEM_INDEX_THRESHOLD {
let mut positions = HashMap::with_capacity(items.len());
for (index, existing) in items.iter().enumerate() {
@@ -1906,11 +1929,18 @@ impl TurnItemIndex {
self.positions = Some(positions);
}
let existing_index = match &self.positions {
Some(positions) => positions.get(item.id()).copied(),
None => items.iter().position(|existing| existing.id() == item.id()),
};
if let Some(index) = existing_index {
match &self.positions {
Some(positions) => positions.get(id).copied(),
None => items.iter().position(|existing| existing.id() == id),
}
}
fn upsert<'a>(
&mut self,
items: &'a mut Vec<ThreadItemPayload>,
item: ThreadItemPayload,
) -> &'a ThreadItemPayload {
if let Some(index) = self.position(items, item.id()) {
items[index] = item;
&items[index]
} else {