fix(assistant): send the earlier answer back to the provider in the shape it saw it (#3094)

Follow-up messages failed with HTTP 400: a whitespace-only text block, two model messages in a row where the turn had pushed for an answer, or a tool call renamed after its provider state was recorded. The rebuilt history now matches what the provider saw during the first answer.
This commit is contained in:
Maurice
2026-11-14 17:48:56 +01:00
parent d6905435ae
commit f71acb08bd
5 changed files with 185 additions and 5 deletions
@@ -60,10 +60,10 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
if (message.role === 'user') {
push('user', [...(message.images ?? []).map(imageBlock), { type: 'text', text: message.text }]);
} else if (message.role === 'assistant' && message.providerState?.adapter === 'anthropic-messages') {
push('assistant', message.providerState.blocks.map(replayed));
push('assistant', message.providerState.blocks.filter((block) => !isBlankText(block)).map(replayed));
} else if (message.role === 'assistant') {
push('assistant', [
...(message.text ? [{ type: 'text', text: message.text }] : []),
...(message.text.trim() ? [{ type: 'text', text: message.text }] : []),
...message.toolCalls.map((call) => ({ type: 'tool_use', id: call.id, name: call.name, input: call.args })),
]);
} else {
@@ -82,6 +82,15 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
return out;
}
/**
* A text block of nothing but whitespace, which the Messages API refuses in a
* request. A model that came back blank streams one now and then; it is left
* out of the replay, which changes nothing the model said.
*/
function isBlankText(block: ContentBlock): boolean {
return block.type === 'text' && !asText(block.text).trim();
}
/**
* A block of an answer as it goes back to the provider: as it came, but a text
* block without the citations it carried, which the provider checks against
+47 -1
View File
@@ -261,6 +261,51 @@ function uniqueCallId(call: AiToolCall, used: Set<string>, newId: () => string):
return id;
}
/**
* The provider's own record of a step under the ids its calls go on with. A
* call whose id the thread had used already is renamed (uniqueCallId), and
* the record has to follow: Gemini finds a thought signature by the call's
* id, Anthropic and the Responses API pair each answer with the call of the
* same id, and a record left on the old id was refused on the next request
* that replayed it, which for the last step of an answer is every later
* message of the thread (#3094).
*/
function withRenamedCalls(state: AiProviderState | null, renamed: ReadonlyMap<string, string>): AiProviderState | null {
if (!state || renamed.size === 0) return state;
const idOf = (id: unknown) => (typeof id === 'string' ? (renamed.get(id) ?? id) : id);
if (state.adapter === 'anthropic-messages') {
return {
...state,
blocks: state.blocks.map((block) => (block.type === 'tool_use' ? { ...block, id: idOf(block.id) } : block)),
};
}
if (state.adapter === 'openai-responses') {
return {
...state,
items: state.items.map((item) =>
item.type === 'function_call' ? { ...item, call_id: idOf(item.call_id) } : item,
),
};
}
return {
...state,
signatures: Object.fromEntries(
Object.entries(state.signatures).map(([id, signature]) => [renamed.get(id) ?? id, signature]),
),
};
}
/** The ids the step's calls were renamed from, to the ones they go on with; an id one of its calls kept stays. */
function renamedCalls(received: readonly AiToolCall[], kept: readonly AiToolCall[]): Map<string, string> {
const keptIds = new Set(kept.map((call) => call.id));
const renamed = new Map<string, string>();
received.forEach((call, index) => {
const id = kept[index]?.id;
if (id && call.id !== id && !keptIds.has(call.id) && !renamed.has(call.id)) renamed.set(call.id, id);
});
return renamed;
}
/** What every step of one turn shares. */
interface Turn {
deps: AgentDeps;
@@ -576,8 +621,9 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
w.startStep();
const answer = await streamAnswer(turn, rowId);
if ('end' in answer) return answer;
const { text, providerState, cutOff } = answer;
const { text, cutOff } = answer;
const all = answer.received.map((call) => ({ ...call, id: uniqueCallId(call, input.usedCallIds, deps.newId) }));
const providerState = withRenamedCalls(answer.providerState, renamedCalls(answer.received, all));
// The follow-ups the model offers are the server's to answer: they never run, and alone they end the turn.
const asks = all.filter((call) => call.name === ASSISTANT_FOLLOW_UPS_TOOL);
const received = all.filter((call) => call.name !== ASSISTANT_FOLLOW_UPS_TOOL);
+15 -1
View File
@@ -57,6 +57,15 @@ const NOT_APPLIED = JSON.stringify({ applied: false, reason: 'The user did not a
/** What the model is told of a call it made that never ran, because the answer was stopped before it. */
const NOT_RUN = 'This call did not run: the answer was stopped before it.';
/**
* What stands between two steps of one answer where the first called nothing:
* the turn pushed the model on there (an answer that only announced its
* lookups, one cut off), and the push is never stored. Without it the replay
* has one model message follow another, which Gemini refuses for a step that
* makes calls, and with it every later message of the thread (#3094).
*/
const WENT_ON = 'Go on.';
/**
* The calls a provider's own record of a step holds, which it replays as they
* came and wants answered: Anthropic's tool uses, the Responses API's function
@@ -330,8 +339,13 @@ export function toChatHistory(
name: part.toolName,
args: part.args,
}));
const text = textOf(parts);
// A step that wrote only blank lines wrote nothing: the turn sent it on as empty, and Anthropic refuses
// a text block of whitespace, so replaying it as text broke every later message of the thread (#3094).
const written = textOf(parts);
const text = written.trim() ? written : '';
if (!text && toolCalls.length === 0) return;
const before = history.at(-1);
if (before?.role === 'assistant' && before.toolCalls.length === 0) history.push({ role: 'user', text: WENT_ON });
const providerState = decodeJson(ASSISTANT_PROVIDER_STATE, row.provider_state, `message ${row.id}`);
// The recorded calls that never ran go into the step's calls as well: an adapter that does not
// replay this record rebuilds the step from them, and would otherwise find answers to calls it never made.
@@ -347,6 +347,55 @@ describe('runAgentLoop', () => {
expect(assistant.toolCalls[0].id).toBe('call_id-1');
});
it('ASTAGT-006b: a renamed call keeps its thought signature and its record, for the next step and the stored row (#3094)', async () => {
const t = setup([
[
{ type: 'tool-call', call: { id: 'call_0', name: 'list_places', args: {} } },
{
type: 'provider-state',
state: { adapter: 'openai-chat', model: 'gemini-3-flash', signatures: { call_0: 'sig-A' } },
},
{ type: 'finish', reason: 'tool-calls' },
],
[{ type: 'text-delta', delta: 'Found it' }],
]);
t.input.usedCallIds.add('call_0');
await runAgentLoop(t.deps, t.input, t.writer, new AbortController().signal);
const renamed = { adapter: 'openai-chat', model: 'gemini-3-flash', signatures: { 'call_id-1': 'sig-A' } };
expect(t.requests[1].messages[1]).toMatchObject({
role: 'assistant',
toolCalls: [{ id: 'call_id-1' }],
providerState: renamed,
});
expect(t.memory.saved[0].step.providerState).toEqual(renamed);
const anthropic = setup([
[
{ type: 'tool-call', call: { id: 'toolu_1', name: 'list_places', args: {} } },
{
type: 'provider-state',
state: {
adapter: 'anthropic-messages',
blocks: [
{ type: 'thinking', thinking: 'look', signature: 's' },
{ type: 'tool_use', id: 'toolu_1', name: 'list_places', input: {} },
],
},
},
],
[{ type: 'text-delta', delta: 'Found it' }],
]);
anthropic.input.usedCallIds.add('toolu_1');
await runAgentLoop(anthropic.deps, anthropic.input, anthropic.writer, new AbortController().signal);
expect(anthropic.memory.saved[0].step.providerState).toEqual({
adapter: 'anthropic-messages',
blocks: [
{ type: 'thinking', thinking: 'look', signature: 's' },
{ type: 'tool_use', id: 'call_id-1', name: 'list_places', input: {} },
],
});
});
it('ASTAGT-007: stops after MAX_STEPS steps with max_steps', async () => {
const reads = Array.from({ length: MAX_STEPS }, (_, i): Step => [
{ type: 'tool-call', call: { id: `r${i}`, name: 'list_places', args: {} } },
@@ -17,7 +17,7 @@ import {
toChatHistory,
toThreadMessages,
} from '../../../../src/nest/assistant/assistant-history';
import type { AssistantMessagePart, AssistantToolApproval } from '@trek/shared';
import { ASSISTANT_FOLLOW_UPS_TOOL, type AssistantMessagePart, type AssistantToolApproval } from '@trek/shared';
import { describe, expect, it } from 'vitest';
@@ -740,6 +740,68 @@ describe('toChatHistory: calls the model made of the server tools (#2845)', () =
});
});
describe('toChatHistory: the next message after an answer (#3094)', () => {
/**
* The first answer of #3094 as its rows hold it: a step that only announced
* its lookups (the turn pushed it on, and the push is not stored), the
* lookups, a step that came back blank with only its follow-ups (asked
* again for the answer), and the answer. Then the user writes again.
*/
function secondMessage(): AiChatMessage[] {
return toChatHistory(
[
row('user', [{ type: 'text', text: 'Hello' }]),
row('assistant', [{ type: 'text', text: 'Let me look at your trip:' }]),
row('assistant', [
{ type: 'tool-call', toolCallId: 'call_a', toolName: 'get_trip_summary', args: {}, result: { days: [] } },
{ type: 'tool-call', toolCallId: 'call_b', toolName: 'list_places', args: {}, result: { places: [] } },
]),
row('assistant', [
{ type: 'text', text: '\n\n' },
{
type: 'tool-call',
toolCallId: 'call_c',
toolName: ASSISTANT_FOLLOW_UPS_TOOL,
args: { questions: ['What is on day 2?'] },
result: 'Write your answer now.',
},
]),
row('assistant', [{ type: 'text', text: 'Your trip has no days yet.' }]),
row('user', [{ type: 'text', text: 'Add a day' }]),
],
new Map(),
);
}
it('ASTHIS-022: the second message gives Anthropic every tool_use answered and no blank text block', () => {
const messages = toAnthropicMessages(secondMessage());
messages.forEach((message, index) => {
expect(message.role).toBe(index % 2 === 0 ? 'user' : 'assistant');
});
expect(messages.at(-1)?.content.at(-1)).toEqual({ type: 'text', text: 'Add a day' });
messages.forEach((message, index) => {
const ids = message.content.filter((block) => block.type === 'tool_use').map((block) => block.id);
if (ids.length === 0) return;
const results = messages[index + 1].content
.filter((block) => block.type === 'tool_result')
.map((block) => block.tool_use_id);
expect(results).toEqual(ids);
});
const texts = messages.flatMap((message) => message.content).filter((block) => block.type === 'text');
expect(texts.every((block) => typeof block.text === 'string' && block.text.trim() !== '')).toBe(true);
});
it('ASTHIS-023: the second message never has one model message follow another on Chat Completions', () => {
const chat = toChatMessages({ system: 'sys', messages: secondMessage(), tools: [] });
chat.forEach((message, index) => {
if (message.role !== 'assistant') return;
expect(['user', 'tool']).toContain(chat[index - 1]?.role);
if (typeof message.content === 'string') expect(message.content.trim()).not.toBe('');
});
expect(chat.at(-1)).toEqual({ role: 'user', content: 'Add a day' });
});
});
describe('toThreadMessages', () => {
it('ASTHIS-010: the steps of one answer are one message under the first row id', () => {
nextId = 10;