diff --git a/server/src/nest/assistant/assistant-agent.ts b/server/src/nest/assistant/assistant-agent.ts index 3aa0a7f6e..7e88d4669 100644 --- a/server/src/nest/assistant/assistant-agent.ts +++ b/server/src/nest/assistant/assistant-agent.ts @@ -34,6 +34,7 @@ import { type AssistantWeatherDay, type TrekCard, } from '@trek/shared'; +import { isDeepStrictEqual } from 'node:util'; type ToolArgs = Record; @@ -139,6 +140,7 @@ interface AgentOutcome { } const TOO_MANY_CHANGES = `Too many changes in one step: propose at most ${MAX_WRITES_PER_STEP} at once.`; +const CHANGE_ALREADY_PROPOSED = 'This exact change is already waiting for approval.'; const TOO_MANY_LOOKUPS = `Too many lookups in one step: make at most ${MAX_READS_PER_STEP} at once.`; /** What a step collects while its calls run: its parts, and the weather its lookups found. */ @@ -495,7 +497,9 @@ async function runCalls(turn: Turn, calls: AiToolCall[], collected: StepParts): const unknownPlace = 'error' in prepared ? null : placeIdRefusal(call.name, prepared.args, turn.knownPlaceIds); if (unknownPlace) prepared = { error: unknownPlace }; if (kind === 'write' && !('error' in prepared)) { - const refused = await deps.refusalOf?.(call.name, prepared.args); + const { args } = prepared; + const duplicate = held.some((change) => change.toolName === call.name && isDeepStrictEqual(change.args, args)); + const refused = duplicate ? CHANGE_ALREADY_PROPOSED : await deps.refusalOf?.(call.name, args); if (refused) prepared = { error: refused }; } if (kind === 'read' && !('error' in prepared)) reads += 1; diff --git a/server/tests/unit/nest/assistant/assistant-agent.test.ts b/server/tests/unit/nest/assistant/assistant-agent.test.ts index 49ac2f8cd..b95ac1542 100644 --- a/server/tests/unit/nest/assistant/assistant-agent.test.ts +++ b/server/tests/unit/nest/assistant/assistant-agent.test.ts @@ -65,6 +65,7 @@ const KINDS: Record = { get_weather: 'read', get_place_details: 'read', create_place: 'write', + create_packing_item: 'write', open_trip_chat: 'read', }; @@ -284,6 +285,31 @@ describe('runAgentLoop', () => { expect(t.chunks.at(-1)).toEqual({ type: 'finish', finishReason: 'tool-calls' }); }); + it('holds one proposal when the model repeats the same packing item change in a step', async () => { + const t = setup([ + [ + { type: 'tool-call', call: { id: 'first', name: 'create_packing_item', args: { name: 'Adapter', quantity: 2 } } }, + { type: 'tool-call', call: { id: 'repeat', name: 'create_packing_item', args: { quantity: 2, name: 'Adapter' } } }, + { type: 'tool-call', call: { id: 'other', name: 'create_packing_item', args: { name: 'Adapter', quantity: 3 } } }, + ], + ]); + + const outcome = await runAgentLoop(t.deps, t.input, t.writer, new AbortController().signal); + + expect(outcome.end).toBe('awaiting'); + expect(t.memory.held[0].held.map((call) => call.providerCallId)).toEqual(['first', 'other']); + expect(t.chunks.filter((chunk) => chunk.type === 'tool-approval-request')).toHaveLength(2); + expect(t.memory.held[0].step.parts).toContainEqual({ + type: 'tool-call', + toolCallId: 'repeat', + toolName: 'create_packing_item', + args: { quantity: 2, name: 'Adapter' }, + result: 'This exact change is already waiting for approval.', + isError: true, + }); + expect(t.tools.call).not.toHaveBeenCalled(); + }); + it('ASTAGT-004: more changes than a step may hold go back to the model as an error', async () => { const calls = Array.from({ length: MAX_WRITES_PER_STEP + 1 }, (_, i): AiChatEvent => ({ type: 'tool-call',