From 0dae908e050d1276edb3a61b70312c9c5b7301a0 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 3 Nov 2026 21:48:37 +0100 Subject: [PATCH] fix(assistant): send back a call to a tool whose group the turn has not loaded The model could call a tool it knew from an earlier turn without loading its group in this one. TREK ran it, but the model had never seen its schema in the turn, so numbers, arrays and objects came as strings and required arguments were missing, and the MCP check refused it with -32602 (mostly the Tours tools). Such a call no longer runs: its group is loaded for the rest of the turn and the call is answered with a tool error asking for it again with the schema the next step now offers. Closes #2967 --- .../nest/assistant/assistant-tools.service.ts | 18 ++++++++++++++++++ .../assistant/assistant-tools.service.test.ts | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/server/src/nest/assistant/assistant-tools.service.ts b/server/src/nest/assistant/assistant-tools.service.ts index 0029d50aa..eff03a137 100644 --- a/server/src/nest/assistant/assistant-tools.service.ts +++ b/server/src/nest/assistant/assistant-tools.service.ts @@ -307,6 +307,13 @@ export class AssistantToolSession { return tool?.offered ? tool.kind : null; } + /** The group of a tool this grouped session holds but has not loaded in this turn; null otherwise. */ + private unloadedGroupOf(name: string): string | null { + const group = this.tools.get(name)?.hint.group; + if (!this.grouped || !group || this.loaded.has(group)) return null; + return group; + } + /** The home chat offers a journal's page while the user's journals can be listed (the journal addon is on). */ private get offersJournalChat(): boolean { return this.mode === 'home' && this.tools.has(OPEN_JOURNEY_CHAT_READ); @@ -323,6 +330,17 @@ export class AssistantToolSession { * conversation's is refused. */ pinArgs(name: string, args: ToolArgs): { args: ToolArgs } | { error: string } { + const unloaded = this.unloadedGroupOf(name); + if (unloaded) { + // The model called a tool it was not offered in this turn (it knows the + // name from an earlier turn), so it wrote the arguments without the + // schema and they would fail the MCP check (#2967). Its group is loaded + // here, so the next step offers the schema, and the call is sent back. + this.loaded.add(unloaded); + return { + error: `${name} belongs to the "${unloaded}" tools, which were not loaded in this turn, so it did not run. They are loaded now: send the call again with the arguments exactly as its schema describes them.`, + }; + } const given = pinNamesOf(this.pinArg).filter((key) => args[key] !== undefined); if (given.some((key) => Number(args[key]) !== this.pinnedId)) { const about = this.pinArg === PINNED_ARG ? 'trip' : 'journey'; diff --git a/server/tests/unit/nest/assistant/assistant-tools.service.test.ts b/server/tests/unit/nest/assistant/assistant-tools.service.test.ts index 2232b3062..8cd96b6e7 100644 --- a/server/tests/unit/nest/assistant/assistant-tools.service.test.ts +++ b/server/tests/unit/nest/assistant/assistant-tools.service.test.ts @@ -403,6 +403,23 @@ describe('an assistant tool session', () => { } }); + it('ASTTOOL-024: a tool of a group not loaded in the turn is sent back once, with its group loaded for the next step (#2967)', async () => { + const { userId, tripId } = await ownerWithTrips(); + const session = await tools.open(userId, tripId); + try { + expect(session.specs().map((spec) => spec.name)).not.toContain('list_tours'); + const refused = session.pinArgs('list_tours', { placeId: '530' }); + expect(refused).toEqual({ error: expect.stringContaining('"tours" tools, which were not loaded') }); + expect(session.specs().map((spec) => spec.name)).toContain('list_tours'); + // Sent again with the schema in hand, the call goes through. + expect(session.pinArgs('list_tours', {})).toEqual({ args: {} }); + // A core tool was never held back. + expect(session.pinArgs('get_trip_summary', {})).toEqual({ args: {} }); + } 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']));