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']));