Skip to content

Commit bb8871a

Browse files
committed
fix: preserve tool-call history across thread hydration
Fixes #568 Thread hydration discarded tool-call history, causing the LLM to re-attempt completed tool calls on page reload. Emit full tool-call sequences in messages(), enrich persisted JSON for DB rebuild, sanitize before recording in all paths, fix multi-stage alignment. No schema migration; backward compatible. 13 regression tests added.
1 parent 3079043 commit bb8871a

File tree

3 files changed

+617
-80
lines changed

3 files changed

+617
-80
lines changed

src/agent/dispatcher.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -687,23 +687,6 @@ impl Agent {
687687
.await;
688688
}
689689

690-
// Record result in thread
691-
{
692-
let mut sess = session.lock().await;
693-
if let Some(thread) = sess.threads.get_mut(&thread_id)
694-
&& let Some(turn) = thread.last_turn_mut()
695-
{
696-
match &tool_result {
697-
Ok(output) => {
698-
turn.record_tool_result(serde_json::json!(output));
699-
}
700-
Err(e) => {
701-
turn.record_tool_error(e.to_string());
702-
}
703-
}
704-
}
705-
}
706-
707690
// Check for auth awaiting — defer the return
708691
// until all results are recorded.
709692
if deferred_auth.is_none()
@@ -743,6 +726,7 @@ impl Agent {
743726
}
744727

745728
// Sanitize and add tool result to context
729+
let is_tool_error = tool_result.is_err();
746730
let result_content = match tool_result {
747731
Ok(output) => {
748732
let sanitized =
@@ -756,6 +740,20 @@ impl Agent {
756740
Err(e) => format!("Tool '{}' failed: {}", tc.name, e),
757741
};
758742

743+
// Record sanitized result in thread so messages()
744+
// and persist_tool_calls() use cleaned content.
745+
{
746+
let mut sess = session.lock().await;
747+
if let Some(thread) = sess.threads.get_mut(&thread_id)
748+
&& let Some(turn) = thread.last_turn_mut()
749+
{
750+
if is_tool_error {
751+
turn.record_tool_error(result_content.clone());
752+
}
753+
turn.record_tool_result(serde_json::json!(result_content));
754+
}
755+
}
756+
759757
context_messages.push(ChatMessage::tool_result(
760758
&tc.id,
761759
&tc.name,

0 commit comments

Comments
 (0)