Recover poisoned shared-state locks

This commit is contained in:
Georg Bauer
2026-07-25 13:12:49 +02:00
parent 292ee82af5
commit c77ef47cfc
2 changed files with 64 additions and 22 deletions

View File

@@ -410,16 +410,17 @@ pub(super) fn validate_tool_results(
if !matches!(protocol, Protocol::Anthropic | Protocol::Responses) {
return Ok(());
}
let memory = state.tool_memory.lock().ok();
let memory = state
.tool_memory
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for (index, message) in messages.iter().enumerate() {
if !matches!(message.role.as_str(), "tool" | "function") || message.tool_call_id.is_empty()
{
continue;
}
let id = &message.tool_call_id;
let live = memory
.as_ref()
.is_some_and(|memory| memory.contains_key(id));
let live = memory.contains_key(id);
let replayed = messages[..index].iter().any(|message| {
message.role == "assistant" && message.tool_calls.iter().any(|call| call.id == *id)
});
@@ -441,13 +442,15 @@ pub(super) fn validate_tool_results(
}
fn replayed_or_canonical_tools(state: &State, calls: &[ApiToolCall]) -> String {
if let Ok(memory) = state.tool_memory.lock()
&& let Some(raw) = calls.iter().find_map(|call| {
(!call.id.is_empty())
.then(|| memory.get(&call.id))
.flatten()
})
{
let memory = state
.tool_memory
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if let Some(raw) = calls.iter().find_map(|call| {
(!call.id.is_empty())
.then(|| memory.get(&call.id))
.flatten()
}) {
return raw.clone();
}
canonical_tools(calls)
@@ -568,14 +571,16 @@ pub(super) fn parse_generated_tools_with_ids(
.cloned()
.unwrap_or_else(|| random_tool_id(prefix));
}
if let Ok(mut memory) = state.tool_memory.lock() {
// ponytail: one process-local replay table; add LRU eviction if 100k live tool ids is measured insufficient.
if memory.len() >= 100_000 {
memory.clear();
}
for call in &calls {
memory.insert(call.id.clone(), raw.to_owned());
}
let mut memory = state
.tool_memory
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
// ponytail: one process-local replay table; add LRU eviction if 100k live tool ids is measured insufficient.
if memory.len() >= 100_000 {
memory.clear();
}
for call in &calls {
memory.insert(call.id.clone(), raw.to_owned());
}
(content.to_owned(), calls)
}