fix(assistant): send a blind write back with its schema, pair unrun calls with their answers, and give the wordless-answer recovery its own nudge

A write from a group the turn had not loaded was held as a change card with
schema-less arguments and failed on apply (#2967); it now goes back to the
model with the group loaded. Calls answered as not run are now listed on
the assistant turn too, so a thread replayed on another provider has no
orphan tool results. The blind-call hint only goes with the blind call.
This commit is contained in:
Maurice
2026-11-05 13:02:25 +01:00
parent 91f6430762
commit c3357ebea4
6 changed files with 222 additions and 20 deletions
@@ -859,6 +859,49 @@ describe('runAgentLoop', () => {
expect(outcome).toMatchObject({ end: 'failed', code: 'empty_answer' });
expect(t.requests).toHaveLength(2);
});
it('ASTAGT-045: follow-ups with no answer after an announcing first answer are still asked for the answer', async () => {
const t = setup([
[{ type: 'text-delta', delta: 'Let me check your places:' }],
[{ type: 'tool-call', call: { id: 'l1', name: 'list_places', args: {} } }],
[ask('toolu_8', ['Add Ginkaku-ji']), { type: 'finish', reason: 'tool-calls' }],
[{ type: 'text-delta', delta: 'Kinkaku-ji is the only place so far.' }],
]);
const outcome = await runAgentLoop(t.deps, t.input, t.writer, new AbortController().signal);
expect(outcome.end).toBe('stop');
expect(t.requests).toHaveLength(4);
expect(t.requests[1].messages.at(-1)).toMatchObject({ role: 'user', text: expect.stringContaining('now') });
expect(t.requests[3].messages.at(-1)).toMatchObject({
role: 'tool',
toolCallId: 'toolu_8',
output: expect.stringContaining('Not shown yet'),
});
expect(t.chunks).toContainEqual({ type: 'data-trek-follow-ups', data: { questions: ['Add Ginkaku-ji'] } });
expect(t.memory.saved.map((saved) => saved.status)).not.toContain('error');
});
});
it('ASTAGT-046: a change its session sends back (called before its tools were loaded) is a tool error, never held (#2967)', async () => {
const t = setup([
[{ type: 'tool-call', call: { id: 'w1', name: 'create_place', args: { name: 'Blind' } } }],
[{ type: 'tool-call', call: { id: 'w2', name: 'create_place', args: { name: 'With schema' } } }],
]);
t.tools.pinArgs.mockImplementationOnce(() => ({ error: 'create_place did not run: send it again' }));
const outcome = await runAgentLoop(t.deps, t.input, t.writer, new AbortController().signal);
expect(outcome.end).toBe('awaiting');
expect(t.memory.saved[0].step.parts).toContainEqual({
type: 'tool-call',
toolCallId: 'w1',
toolName: 'create_place',
args: { name: 'Blind' },
result: 'create_place did not run: send it again',
isError: true,
});
const sentBack = t.requests[1].messages.find((m) => m.role === 'tool' && m.toolCallId === 'w1');
expect(sentBack).toMatchObject({ isError: true, output: expect.stringContaining('did not run') });
expect(t.deps.describe).toHaveBeenCalledTimes(1);
expect(t.memory.held).toHaveLength(1);
expect(t.memory.held[0].held.map((call) => call.args)).toEqual([{ name: 'With schema' }]);
});
it('ASTAGT-038: followUpsOf trims, drops duplicates and blanks, cuts each to a chip and keeps three', () => {
@@ -651,6 +651,61 @@ describe('toChatHistory: calls the model made of the server tools (#2845)', () =
expect(outputs).toEqual(['call_1', 'call_2']);
});
it('ASTHIS-021: a stopped step replayed on another provider names every call it answers', () => {
const anthropic = stoppedMidStep({
adapter: 'anthropic-messages',
blocks: [
{ type: 'text', text: 'Let me look.' },
{ type: 'tool_use', id: 'call_1', name: 'list_places', input: {} },
{ type: 'tool_use', id: 'call_2', name: 'get_weather', input: { day: 3 } },
],
});
expect(anthropic.find((message) => message.role === 'assistant')).toMatchObject({
toolCalls: [
{ id: 'call_1', name: 'list_places', args: {} },
{ id: 'call_2', name: 'get_weather', args: { day: 3 } },
],
});
// Chat Completions: every tool message follows an assistant message whose tool_calls name it.
const chat = toChatMessages({ system: '', messages: anthropic, tools: [] });
chat.forEach((message, index) => {
if (message.role !== 'tool') return;
const before = chat.slice(0, index).reverse().find((earlier) => earlier.role === 'assistant');
const named = ((before?.tool_calls ?? []) as Array<{ id: string }>).map((call) => call.id);
expect(named).toContain(message.tool_call_id);
});
// Responses: every function_call_output has its function_call before it.
const items = toResponsesInput({ system: '', messages: anthropic, tools: [] });
items.forEach((item, index) => {
if (item.type !== 'function_call_output') return;
const made = items
.slice(0, index)
.some((earlier) => earlier.type === 'function_call' && earlier.call_id === item.call_id);
expect(made).toBe(true);
});
// Anthropic: a step the Responses API recorded is rebuilt with every tool_use its results answer.
const responses = stoppedMidStep({
adapter: 'openai-responses',
items: [
{ type: 'function_call', call_id: 'call_1', name: 'list_places', arguments: '{}' },
{ type: 'function_call', call_id: 'call_2', name: 'get_weather', arguments: 'not json' },
],
});
const messages = toAnthropicMessages(responses);
const uses = messages[1].content.filter((block) => block.type === 'tool_use');
expect(uses.map((block) => [block.id, block.input])).toEqual([
['call_1', {}],
['call_2', {}],
]);
const results = messages[2].content
.filter((block) => block.type === 'tool_result')
.map((block) => block.tool_use_id);
expect(results).toEqual(['call_1', 'call_2']);
});
it('ASTHIS-017: a step that only kept a note is the model call it was, answered with what was kept', () => {
const history = toChatHistory(
[
@@ -425,6 +425,63 @@ describe('an assistant tool session', () => {
}
});
it('ASTTOOL-025: a change called before its group was loaded is sent back until a step was offered its schema (#2967)', async () => {
const { userId, tripId } = await ownerWithTrips();
const session = await tools.open(userId, tripId);
try {
expect(session.specs().map((spec) => spec.name)).not.toContain('insert_tour_stop');
// Held as a card, the blind arguments would only fail once the user applies it.
expect(session.pinArgs('insert_tour_stop', { placeId: '530' })).toEqual({
error: expect.stringContaining('"tours" tools, which were not loaded in this turn, so it did not run'),
});
// A second change of the same step was written without the schema too.
expect(session.pinArgs('create_city_walk', { stations: '[1, 2]' })).toEqual({
error: expect.stringContaining('did not run'),
});
// The next step is offered the schema, and the call goes through.
expect(session.specs().map((spec) => spec.name)).toContain('insert_tour_stop');
expect(session.pinArgs('insert_tour_stop', { placeId: 530 })).toHaveProperty('args');
} finally {
await session.close();
}
});
it('ASTTOOL-026: only the blind call itself is told about the schema; a later failure of the same tool is not', async () => {
const { userId, tripId } = await ownerWithTrips();
const session = await tools.open(userId, tripId);
try {
session.specs();
expect(session.pinArgs('get_tour_plan', { placeId: '530' })).toEqual({ args: { placeId: '530' } });
const blind = await session.call('get_tour_plan', { placeId: '530' });
expect(String(blind.value)).toContain('"tours" tools were loaded in this turn');
// Sent again after the next step's specs: it fails on its own, without the hint.
session.specs();
expect(session.pinArgs('get_tour_plan', { placeId: 999_999 })).toEqual({ args: { placeId: 999_999 } });
const again = await session.call('get_tour_plan', { placeId: 999_999 });
expect(again.ok).toBe(false);
expect(String(again.value)).not.toContain('were loaded in this turn');
} finally {
await session.close();
}
});
it('ASTTOOL-027: a blind read refused before it ran leaves no hint for a later call of the same tool', async () => {
const { userId, tripId } = await ownerWithTrips();
const session = await tools.open(userId, tripId);
try {
session.specs();
// Called blind, then refused by the loop before it ran: call() never sees it.
expect(session.pinArgs('get_tour_plan', { placeId: '530' })).toHaveProperty('args');
session.specs();
expect(session.pinArgs('get_tour_plan', { placeId: 999_999 })).toEqual({ args: { placeId: 999_999 } });
const run = await session.call('get_tour_plan', { placeId: 999_999 });
expect(run.ok).toBe(false);
expect(String(run.value)).not.toContain('were loaded in this turn');
} finally {
await session.close();
}
});
it('ASTTOOL-014: an undo session holds its target and nothing else', async () => {
const { userId, tripId } = await ownerWithTrips();
const session = await tools.open(userId, tripId, new Set(['delete_place']));