diff --git a/server/src/nest/assistant/assistant-tools.service.ts b/server/src/nest/assistant/assistant-tools.service.ts index eff03a137..f2b9a3ea8 100644 --- a/server/src/nest/assistant/assistant-tools.service.ts +++ b/server/src/nest/assistant/assistant-tools.service.ts @@ -266,6 +266,9 @@ export class AssistantToolSession { /** The groups this turn loaded; the core is always there. */ private readonly loaded = new Set(); + /** Tools the model called before their group was loaded in this turn, by name, with that group. */ + private readonly calledBlind = new Map(); + /** Whether the session offers its tools by group: a trip's turn and a home turn do. */ private get grouped(): boolean { return this.mode === 'turn' || this.mode === 'home'; @@ -334,12 +337,10 @@ export class AssistantToolSession { 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. + // schema (#2967). Its group is loaded here, so the next step offers the + // schema, and a refusal of these arguments says so (see call()). 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.`, - }; + this.calledBlind.set(name, unloaded); } const given = pinNamesOf(this.pinArg).filter((key) => args[key] !== undefined); if (given.some((key) => Number(args[key]) !== this.pinnedId)) { @@ -360,6 +361,12 @@ export class AssistantToolSession { const final = name === TRANSIT_CREATE_TOOL ? await this.withFoundItinerary(args) : args; const run = await this.invoke(name, final); this.audit(name); + const blind = this.calledBlind.get(name); + if (!run.ok && blind) { + const hint = `${name} was called before the "${blind}" tools were loaded in this turn, so its arguments were written without its schema. They are loaded now and the schema is in your tools: send the call again with the arguments exactly as it describes them.`; + return { ok: false, value: `${typeof run.value === 'string' ? run.value : JSON.stringify(run.value)} +${hint}` }; + } if (name === TRANSIT_SEARCH_TOOL && run.ok) this.itineraries.keep(run.value); return run; } 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 8cd96b6e7..8722cf0a8 100644 --- a/server/tests/unit/nest/assistant/assistant-tools.service.test.ts +++ b/server/tests/unit/nest/assistant/assistant-tools.service.test.ts @@ -403,18 +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 () => { + it('ASTTOOL-024: a tool called before its group was loaded loads the group, and a refusal of its arguments says why (#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') }); + // Called blind, as from an earlier turn: it still runs, and its group comes with it. + const pinned = session.pinArgs('get_tour_plan', { placeId: '530' }); + expect(pinned).toEqual({ args: { placeId: '530' } }); 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: {} }); + // The MCP check refuses the string id; the answer tells the model to send it again. + const run = await session.call('get_tour_plan', { placeId: '530' }); + expect(run.ok).toBe(false); + expect(String(run.value)).toContain('"tours" tools were loaded in this turn'); + // A core tool's refusal carries no such hint. + const core = await session.call('create_place', {}); + expect(core.ok).toBe(false); + expect(String(core.value)).not.toContain('were loaded in this turn'); } finally { await session.close(); }