Repository navigation
serve: a failed MCP tool result goes back to the model; before, wire … - #69
Conversation
…dropped every message with error set, so the model's own call stayed unanswered and it repeated the same call `error` on a chat message means two different things, and wire() treated them as one. On an assistant message it marks a turn that never happened (the server was unreachable, no model loaded): there is nothing to send, dropping it is right. On a tool message it marks the tool's answer, which is the reply the model asked for. Dropping that left the assistant message with tool_calls and no role: "tool" result for it, so the model saw an unanswered call and re-issued the same one instead of noticing the failure. The page showed the failure in red (the flag also drives .tcall.bad) while the request sent back to the server did not carry it. The filter now keeps tool messages whatever `error` says, and their content is prefixed "Error: " so a failed result cannot read as a payload that happens to be prose; the guard leaves text already starting with "Error: " or "The tool failed: " alone, which is what the protocol-error path produces. This matches what serve.rs does for the Anthropic bridge. chatMd had the same conflation, so a .md export left a failed tool result out and hid what went wrong in a chat someone else would read; it now prints it as **tool failed <name>**. The copy button and the continue button keep skipping errored messages: no copy is wanted for a failure. selftest gained 'tool errors go to the model, transport errors do not', which asserts both halves: the two failed results survive with their tool_call_id attached and one gains the prefix, while the errored assistant message is still dropped (w.length 4 of 5). The page is compiled in by include_str!, so a rebuild is needed to see this.
🔍 Aletheore evidence diffWhat changed
Aggregate deltas
|
| // `error` means two different things. On an assistant message the turn never happened, so there is nothing to send. | ||
| // On a tool message it means the tool failed, and that failure is the answer the model is waiting for: dropping it | ||
| // leaves the call unanswered, and a model with an unanswered call repeats the same call instead of noticing. | ||
| return msgs.filter(x => !x.error || x.role === 'tool').map(x => { |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 4
Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.
Prompt for your AI agent
File: src/web/index.html
Line: 887
Issue: Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| // leaves the call unanswered, and a model with an unanswered call repeats the same call instead of noticing. | ||
| return msgs.filter(x => !x.error || x.role === 'tool').map(x => { | ||
| const imgs = x.images || []; | ||
| if (imgs.length) return { role: x.role, content: [{ type: 'text', text: x.content }, ...imgs.map(url => ({ type: 'image_url', image_url: { url } }))] }; |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 4
Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier if (imgs.length) return. It is sent as a user-style content array with no tool_call_id and no 'Error:' marker, so the model never sees that the call failed, and the API may reject the unmatched tool message. The new error handling on line 891 never runs for this case.
Prompt for your AI agent
File: src/web/index.html
Line: 889
Issue: Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier `if (imgs.length)` return. It is sent as a user-style content array with no tool_call_id and no 'Error:' marker, so the model never sees that the call failed, and the API may reject the unmatched tool message. The new error handling on line 891 never runs for this case.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| if (imgs.length) return { role: x.role, content: [{ type: 'text', text: x.content }, ...imgs.map(url => ({ type: 'image_url', image_url: { url } }))] }; | ||
| if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.content }; | ||
| // Say it is an error in the text too: a failed result otherwise looks like a payload that happens to be prose. | ||
| if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.error && !/^(Error: |The tool failed: )/.test(x.content) ? `Error: ${x.content}` : x.content }; |
There was a problem hiding this comment.
🔵 Low · Maintainability · 3 of 4
Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex.
Prompt for your AI agent
File: src/web/index.html
Line: 891
Issue: Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| return w[1].tool_calls[0].function.arguments === '{"k":1}' && w[1].tool_calls[0].id === 'call_0_0' && w[2].role === 'tool' && w[2].tool_call_id === 'call_0_0' && w[2].content === 'r' && !w[3].tool_calls; | ||
| }], | ||
| // `error` on a tool message is the tool's answer, so it goes; on an assistant message the turn never happened. | ||
| ['tool errors go to the model, transport errors do not', () => { |
There was a problem hiding this comment.
🔵 Low · Maintainability · 4 of 4
New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed.
Prompt for your AI agent
File: src/web/index.html
Line: 1304
Issue: New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
Aletheore Flash Lite reviewWhat this PR does. This change makes failed MCP tool results go back to the model as tool responses, where previously they were silently dropped and left the call unanswered. Failed assistant turns are still excluded, and the failure text gets an "Error:" prefix so the model reads it as an error. The change also updates the markdown chat export to label failed tool results, and adds a test for the new behavior. It affects the model's view of tool-call history in the web chat client and users who export chats.
4 finding(s) posted as inline review comment(s).
Review detailsReviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 4 of 4 proposed finding(s). Fix all with your agent |
|
@reidm Merged as it is, ty. I read the change and ran the page's |
…dropped every message with error set, so the model's own call stayed unanswered and it repeated the same call
erroron a chat message means two different things, and wire() treated them as one. On an assistant message itmarks a turn that never happened (the server was unreachable, no model loaded): there is nothing to send, dropping
it is right. On a tool message it marks the tool's answer, which is the reply the model asked for. Dropping that
left the assistant message with tool_calls and no role: "tool" result for it, so the model saw an unanswered call
and re-issued the same one instead of noticing the failure. The page showed the failure in red (the flag also
drives .tcall.bad) while the request sent back to the server did not carry it.
The filter now keeps tool messages whatever
errorsays, and their content is prefixed "Error: " so a failedresult cannot read as a payload that happens to be prose; the guard leaves text already starting with "Error: "
or "The tool failed: " alone, which is what the protocol-error path produces. This matches what serve.rs does for
the Anthropic bridge.
chatMd had the same conflation, so a .md export left a failed tool result out and hid what went wrong in a chat
someone else would read; it now prints it as tool failed . The copy button and the continue button keep
skipping errored messages: no copy is wanted for a failure.
selftest gained 'tool errors go to the model, transport errors do not', which asserts both halves: the two failed
results survive with their tool_call_id attached and one gains the prefix, while the errored assistant message is
still dropped (w.length 4 of 5). The page is compiled in by include_str!, so a rebuild is needed to see this.
What changed and why
In the web-ui, agents will be made aware of failing tool calls. Tool call results no longer filtered out of the messages array sent back to the model.
This handles cases where, for example, a required parameter was missing for an MCP call. Now agents will know a call failed, and can act to correct it. Experience while using https://github.com/jchip/tacks
Here's an example of the problem:

Tool call fails (in this case, change was rejected), but the agent doesn't get notified and considers the operation a success.
Verification
Interrogated my model, who reported errors in tool calls were correctly reported.
It's difficult to set up a scenario where this error occurred 100% of the time because the content of the error existed in the content return. In practice, it works for me without any functional regression.
Checklist