From 574303b4f2c30cd37e3b2988ed64a01c3713ccb0 Mon Sep 17 00:00:00 2001 From: Reid Miller <855629+reidm@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:23:09 -0500 Subject: [PATCH] serve: a failed MCP tool result goes back to the model; before, wire 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 **. 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. --- src/web/index.html | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/web/index.html b/src/web/index.html index 372de80c..f6ce2293 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -612,7 +612,7 @@

This computer

// Export and import. const download = (name, text, type) => { const a = el('a'); a.href = URL.createObjectURL(new Blob([text], { type })); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 1000); }; const pack = list => JSON.stringify({ version: 1, chats: list }, null, 1); -const chatMd = c => `# ${c.title}\n\n` + c.msgs.filter(m => !m.error && (m.content || m.role !== 'assistant')).map(m => m.role === 'tool' ? `**tool ${m.name}:**\n\n~~~\n${m.content}\n~~~` : `**${m.role === 'user' ? 'you' : 'model'}:**\n\n${m.content}`).join('\n\n---\n\n') + '\n'; +const chatMd = c => `# ${c.title}\n\n` + c.msgs.filter(m => (m.role === 'tool' || !m.error) && (m.content || m.role !== 'assistant')).map(m => m.role === 'tool' ? `**${m.error ? 'tool failed' : 'tool'} ${m.name}:**\n\n~~~\n${m.content}\n~~~` : `**${m.role === 'user' ? 'you' : 'model'}:**\n\n${m.content}`).join('\n\n---\n\n') + '\n'; // Takes chats from an export file; an existing chat is replaced only by a newer one. Throws on any other file. function merge(text) { const j = JSON.parse(text), inc = Array.isArray(j) ? j : j?.chats; @@ -881,10 +881,14 @@

This computer

// end of the request as the start of its answer and returns only the new text. // The chat as the API takes it. `open` is the reply being continued (its thinking goes along). function wire(msgs, open) { - return msgs.filter(x => !x.error).map(x => { + // `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 => { 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 } }))] }; - 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 }; if (x.calls?.length) return { role: 'assistant', content: x.content, tool_calls: x.calls.map(c => ({ id: c.id, type: 'function', function: { name: c.name, arguments: c.args || '{}' } })) }; return x === open && x.think ? { role: x.role, content: x.content, reasoning_content: x.think } : { role: x.role, content: x.content }; }); @@ -1296,6 +1300,13 @@

This computer

{ role: 'tool', id: 'call_0_0', name: 'a__x', content: 'r' }, { role: 'assistant', content: 'done' }]); 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', () => { + const w = wire([{ role: 'user', content: 'q' }, { role: 'assistant', content: '', calls: [{ id: 'c1', name: 'a__x', args: '{}' }, { id: 'c2', name: 'a__y', args: '{}' }] }, + { role: 'tool', id: 'c1', name: 'a__x', content: 'boom', error: true }, + { role: 'tool', id: 'c2', name: 'a__y', content: 'The tool failed: Bad Gateway', error: true }, { role: 'assistant', content: 'gone', error: true }]); + return w.length === 4 && w[1].tool_calls.length === 2 && w[2].role === 'tool' && w[2].tool_call_id === 'c1' && w[3].tool_call_id === 'c2' && w[2].content === 'Error: boom' && w[3].content === 'The tool failed: Bad Gateway'; + }], ['tool messages export and import', () => { const c = { id: 't1', title: 't', at: 1, msgs: [{ role: 'user', content: 'q' }, { role: 'assistant', content: '', calls: [{ id: 'c1', name: 'a__x', args: '{}' }] }, { role: 'tool', id: 'c1', name: 'a__x', content: 'r' }, { role: 'assistant', content: 'done' }] }; const bad = { ...c, msgs: [{ role: 'assistant', content: '', calls: 'x' }] }, md = chatMd(c);