From 47d912107f29a6c40f8908a1ae8836fb70e6b352 Mon Sep 17 00:00:00 2001 From: Hai Huang Date: Sat, 21 Nov 2026 17:12:51 -0500 Subject: [PATCH] fix: Do not mark a tool lost when a call with no tools follows it Bob sends a call to its security classifier right after each tool call, and router calls when it starts a subtask. These calls are turns of the main agent, but they have no tool list. The pairing code read each of them as "the agent asked again without the result", so the tool showed "lost". When the next real request brought the result, the row changed to "ok". Now only a request that offers tools can make a tool "abandoned". Imported transcript calls still count, because a transcript holds only real turns and never a tool list. Backtest on 2,004 archived sessions: no change for Claude Code or OpenCode. For Bob, 94 tools no longer show "lost" before their result arrives. One Bob tool, in a session that stopped while its command ran, now stays "running", as an open tool does for every agent. Assisted-By: Claude (Anthropic AI) Signed-off-by: Hai Huang --- core/session/toolpair/toolpair.go | 6 +++++- core/session/toolpair/toolpair_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/session/toolpair/toolpair.go b/core/session/toolpair/toolpair.go index 91f200a75..31929aebf 100644 --- a/core/session/toolpair/toolpair.go +++ b/core/session/toolpair/toolpair.go @@ -404,7 +404,11 @@ func Calls(events []model.SessionEvent, opts Options) Result { } } prev[c.agent] = msgs - if lastAsk[c.agent].Before(c.req.At) { + // Only a call that offers tools can carry a tool's result. Bob's security classifier and + // router calls offer none, and they follow every tool call. An imported call has no + // manifest, but a transcript holds only real turns. + canAnswer := len(inf.Tools) > 0 || inf.ToolCount > 0 || c.req.Imported() + if canAnswer && lastAsk[c.agent].Before(c.req.At) { lastAsk[c.agent] = c.req.At } diff --git a/core/session/toolpair/toolpair_test.go b/core/session/toolpair/toolpair_test.go index 3bb4df8e8..7b363b3bb 100644 --- a/core/session/toolpair/toolpair_test.go +++ b/core/session/toolpair/toolpair_test.go @@ -217,6 +217,31 @@ func TestAbandonedCallIsNamedAsSuch(t *testing.T) { } } +// TestToolLessCallDoesNotAbandon: Bob asks its security classifier, with no tool manifest, while +// its command runs. That call cannot carry the result, so the command stays open until the +// request that does. +func TestToolLessCallDoesNotAbandon(t *testing.T) { + var r rec + bob := &model.EventClient{Name: "bob"} + ask := func(start float64, tools []model.InferenceTool, results []model.InferenceToolResult, calls ...model.InferenceToolCall) { + id := r.id() + r.add(model.SessionEvent{At: at(start), Phase: model.SessionRequest, RequestID: id, Client: bob, + Inference: &model.InferenceExtension{Tools: tools, Messages: []model.InferenceMessage{user("go")}, ToolResults: results}}) + r.add(model.SessionEvent{At: at(start + 1), Phase: model.SessionResponse, RequestID: id, Client: bob, StatusCode: 200, + Inference: &model.InferenceExtension{FinishReason: "tool_calls", ToolCalls: calls}}) + } + ask(0, manifest, nil, model.InferenceToolCall{ID: "t1", Name: "execute_command"}) + ask(2, nil, nil) // the security classifier + + if c := callOf(t, Calls(r.events, Options{}).Calls, "t1"); c.EndReason != "" { + t.Fatalf("after the classifier: EndReason = %q, want empty (still open)", c.EndReason) + } + ask(10, manifest, []model.InferenceToolResult{result("t1", "done")}) + if c := callOf(t, Calls(r.events, Options{}).Calls, "t1"); c.EndReason != ReasonResult { + t.Errorf("after the result: EndReason = %q, want %q", c.EndReason, ReasonResult) + } +} + // TestOpenCallHasNoReason is the distinction the zero value exists for: a call still being // waited on is not a lost one, and must not read as a 0 ms call. func TestOpenCallHasNoReason(t *testing.T) {