Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ <h3>This computer <small id="threads"></small></h3>
// 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;
Expand Down Expand Up @@ -881,10 +881,14 @@ <h3>This computer <small id="threads"></small></h3>
// 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 => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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.

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 } }))] };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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 (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 };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

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 };
});
Expand Down Expand Up @@ -1296,6 +1300,13 @@ <h3>This computer <small id="threads"></small></h3>
{ 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', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

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);
Expand Down