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
@@ -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;