Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
fix(agent-langgraph): pair a tool result with the call it answers (#739)
ANIRUDDHA ADAK3c74c8d10fb9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex df58c187..f6e928bc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,12 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.## Unreleased+- A tool result that arrives before the call it answers is now paired with that call by the LangGraph+ Bot, as the sibling Bots already did. Read back from the durable thread store the result arrives+ first, which is a payload no provider accepts: a tool message with no preceding call, then a call+ with nothing following it. The model answers that with silence rather than an error, so the Bot+ stopped responding for the rest of the conversation.+- Pressing Stop now stops the computer even when the desktop worker never collects the instruction.A stop was queued with no timeout and the worker was the only thing that could ever remove it, so aworker that stopped polling left the entry in memory for the life of the process and the Hostdiff --git a/agent-langgraph/src/history.ts b/agent-langgraph/src/history.tsindex ec575db5..6fe53b1a 100644--- a/agent-langgraph/src/history.ts+++ b/agent-langgraph/src/history.ts@@ -70,7 +70,25 @@ export function toLangChainMessages(.filter((id): id is string => Boolean(id)),);+ /*+ * Tool results, by the call they answer.+ *+ * The history is not guaranteed to arrive with a result after the call it belongs to. Read back+ * from the durable thread store it arrives the other way round, result first, which is a payload+ * no provider accepts: a tool message with no preceding call, and then a call with nothing+ * following it. The model answers that with silence rather than an error, which is the worst of+ * both, so the pairing is rebuilt here instead of trusted.+ */+ const resultsByCall = new Map<string, string>();+ for (const message of input.messages) {+ if (message.role !== "tool") continue;+ const id = (message as { toolCallId?: string }).toolCallId;+ if (id) resultsByCall.set(id, String(message.content ?? ""));+ }+for (const message of input.messages) {+ // Placed with the call they answer, below, rather than wherever they arrived.+ if (message.role === "tool") continue;if (message.role === "user") {messages.push(new HumanMessage({ content: userContent(message.content) }),@@ -81,16 +99,6 @@ export function toLangChainMessages(messages.push(new SystemMessage(String(message.content ?? "")));continue;}- if (message.role === "tool") {- // Tool results are appended so the model can continue from the completed call.- messages.push(- new ToolMessage({- tool_call_id: message.toolCallId,- content: String(message.content ?? ""),- }),- );- continue;- }if (message.role === "assistant") {messages.push(new AIMessage({@@ -108,22 +116,23 @@ export function toLangChainMessages();/*- * Close any of its calls that nothing ever answered, immediately after it.+ * Every call this message made, answered, immediately after it.*- * Position is not cosmetic: a tool result has to follow the assistant message that made the- * call, so these go here rather than being appended at the end. A call answered later in the- * history is left alone and its real answer arrives in its own turn.+ * The real result where there is one, wherever it arrived in the input, and `NO_ANSWER_CAME`+ * where there is not. Both cases are the same requirement: a call must be followed by its+ * result, and the provider rejects the message outright otherwise.*/for (const call of message.toolCalls ?? []) {- if (call.id && !answered.has(call.id)) {- messages.push(- new ToolMessage({- tool_call_id: call.id,- content: NO_ANSWER_CAME,- name: callDetails(call).name,- }),- );- }+ if (!call.id) continue;+ messages.push(+ new ToolMessage({+ tool_call_id: call.id,+ content: answered.has(call.id)+ ? (resultsByCall.get(call.id) ?? "")+ : NO_ANSWER_CAME,+ name: callDetails(call).name,+ }),+ );}}}diff --git a/agent-langgraph/tests/history.test.ts b/agent-langgraph/tests/history.test.tsindex 1d81668c..50c468c4 100644--- a/agent-langgraph/tests/history.test.ts+++ b/agent-langgraph/tests/history.test.ts@@ -143,6 +143,37 @@ describe("history with a tool call nobody answered", () => {});});+/**+ * A tool result is not guaranteed to arrive after the call it belongs to.+ *+ * Read back from the durable thread store it arrives the other way round, result first, which is a+ * payload no provider accepts: a tool message with no preceding call, and then a call with nothing+ * following it. The model answers that with silence rather than an error. The shape below is the one+ * the sibling adapter's own test was written from; this adapter had the same gap.+ */+describe("a history that arrives out of order", () => {+ test("pairs each call with its result, whatever order they arrived in", () => {+ const messages = toLangChainMessages(+ input([+ { role: "user", content: "What is in the PRD?" },+ { role: "tool", toolCallId: "call_1", content: "the document text" },+ assistantAsking,+ ]),+ );++ const asked = messages.findIndex((message) => message instanceof AIMessage);+ expect(messages[asked + 1]).toBeInstanceOf(ToolMessage);+ const answer = messages[asked + 1] as ToolMessage;+ expect(answer.tool_call_id).toBe("call_1");+ expect(String(answer.content)).toBe("the document text");++ // The result is not also left where it arrived: no tool message precedes the call it belongs to.+ expect(+ messages.findIndex((message) => message instanceof ToolMessage),+ ).toBe(asked + 1);+ });+});+/*** A message somebody attached a file to.*