From 7e5b23808d3ccaf63446a87af9c0a8c6385607b3 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 24 Oct 2026 01:21:28 +0000 Subject: [PATCH] fix(assistant): the home chat leaves a journey's own writes to the journey The home chat kept every write whose arguments named no trip and no journey, so the journey group brought update_journey_entry, delete_journey_entry and reorder_journey_entry_photos (they name only the entry) and the Dawarich journal entry (it names the journey as journalId). The writes of the journey's controllers now stay out by their controller, and journalId counts as a journey, as the home chat's own contract and prompt say. --- .../nest/assistant/assistant-tool-offer.ts | 27 +++++++++++++++-- .../nest/assistant/assistant-tools.service.ts | 7 ++++- .../assistant/assistant-home-session.test.ts | 29 ++++++++++++++++++- .../nest/assistant/assistant-home.test.ts | 22 ++++++++++---- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/server/src/nest/assistant/assistant-tool-offer.ts b/server/src/nest/assistant/assistant-tool-offer.ts index 495d6c48a..52318b9e4 100644 --- a/server/src/nest/assistant/assistant-tool-offer.ts +++ b/server/src/nest/assistant/assistant-tool-offer.ts @@ -209,7 +209,8 @@ export function keptInJourney( /** * The arguments that name a trip or a journey, as the tools spell them: the - * collections' copy takes `trip_id`, a new journey its `trip_ids`. + * collections' copy takes `trip_id`, a new journey its `trip_ids`, a Dawarich + * stay taken into a journal its `journalId`. */ const TRIP_OR_JOURNEY_ARGS: ReadonlySet = new Set([ PINNED_ARG, @@ -217,6 +218,20 @@ const TRIP_OR_JOURNEY_ARGS: ReadonlySet = new Set([ 'trip_id', 'trip_ids', 'journey_id', + 'journalId', +]); + +/** + * The controllers of a journey's own content, whose every write works on one + * journey, also when it names only a part of it (an entry by its `entryId`). + */ +const JOURNEY_WRITE_CONTROLLERS: ReadonlySet = new Set([ + 'JourneyMcp', + 'JourneyMapMcp', + 'JourneyTracksMcp', + 'JourneyRecordingsMcp', + 'JourneyDayImportMcp', + 'TripJournalLinkMcp', ]); /** @@ -226,10 +241,16 @@ const TRIP_OR_JOURNEY_ARGS: ReadonlySet = new Set([ * user may read it, as for any MCP client); and the writes that take neither * a trip nor a journey (the atlas, the vacation planner, collections, the * tour library, the settings). A trip's own changes belong in that trip's - * chat, which the home chat offers instead (open_trip_chat). + * chat, which the home chat offers instead (open_trip_chat); a journey's stay + * with the journey page, as they do for the Studio's chat. */ -export function keptAtHome(kind: 'read' | 'write', schemaArgs: Iterable): boolean { +export function keptAtHome( + kind: 'read' | 'write', + schemaArgs: Iterable, + className: string | undefined, +): boolean { if (kind === 'read') return true; + if (className !== undefined && JOURNEY_WRITE_CONTROLLERS.has(className)) return false; for (const arg of schemaArgs) if (TRIP_OR_JOURNEY_ARGS.has(arg)) return false; return true; } diff --git a/server/src/nest/assistant/assistant-tools.service.ts b/server/src/nest/assistant/assistant-tools.service.ts index caafadb37..21d7b1277 100644 --- a/server/src/nest/assistant/assistant-tools.service.ts +++ b/server/src/nest/assistant/assistant-tools.service.ts @@ -430,7 +430,12 @@ export class AssistantToolsService implements OnApplicationBootstrap { const include = (name: string) => undoTargets ? undoTargets.has(name) : isOfferedTool(name) && !isStudioController(catalog.get(name)?.className); const keep = (tool: ListedTool) => - !!undoTargets || keptAtHome(tool.kind, Object.keys((tool.inputSchema.properties as object | undefined) ?? {})); + !!undoTargets || + keptAtHome( + tool.kind, + Object.keys((tool.inputSchema.properties as object | undefined) ?? {}), + catalog.get(tool.name)?.className, + ); const mode: SessionMode = undoTargets ? 'undo' : 'home'; return this.session(userId, NO_PIN, mode, { catalog, include, keep }); } diff --git a/server/tests/unit/nest/assistant/assistant-home-session.test.ts b/server/tests/unit/nest/assistant/assistant-home-session.test.ts index 574e324a7..7f8508507 100644 --- a/server/tests/unit/nest/assistant/assistant-home-session.test.ts +++ b/server/tests/unit/nest/assistant/assistant-home-session.test.ts @@ -1,7 +1,8 @@ /** * The assistant's tool session for the home chat, over the container's real * MCP registry: it reads any trip the user may read, on the id the model - * passes, and holds no write that takes a trip or a journey; the writes that + * passes, and holds no write that takes a trip or a journey, nor one of a + * journey's own that names only its entry; the writes that * belong to no trip (the atlas, collections) come with their groups; the way * into a trip's chat answers from the user's own trips only; an undo's * session holds its target. @@ -29,6 +30,8 @@ beforeAll(async () => { tools = app.get(AssistantToolsService); await setAddonEnabled(orm, 'atlas', true); await setAddonEnabled(orm, 'collections', true); + await setAddonEnabled(orm, 'journey', true); + await setAddonEnabled(orm, 'dawarich', true); }); async function ownerWithTrip() { @@ -78,6 +81,30 @@ describe('a home tool session', () => { } }); + it("ASTHSES-006: the journey group brings a journey's reads and none of its writes", async () => { + const { userId } = await ownerWithTrip(); + const session = await tools.openHome(userId); + try { + await session.call(USE_TOOLS_TOOL, { groups: ['journey'] }); + const names = session.specs().map((spec) => spec.name); + expect(names).toEqual(expect.arrayContaining(['list_journeys', 'list_journey_entries'])); + const journeyWrites = [ + 'update_journey_entry', + 'delete_journey_entry', + 'reorder_journey_entry_photos', + 'accept_dawarich_suggestion_as_journal_entry', + ]; + for (const name of journeyWrites) { + expect(names, name).not.toContain(name); + expect(session.kindOf(name), name).toBeNull(); + } + // A Dawarich suggestion waved away belongs to no journey and stays. + expect(session.kindOf('dismiss_dawarich_suggestion')).toBe('write'); + } finally { + await session.close(); + } + }); + it('ASTHSES-003: reads a trip of the user by its id, and only with the user rights', async () => { const { userId, tripId } = await ownerWithTrip(); const { user: stranger } = await makeUser(orm); diff --git a/server/tests/unit/nest/assistant/assistant-home.test.ts b/server/tests/unit/nest/assistant/assistant-home.test.ts index f618252eb..a9fe30183 100644 --- a/server/tests/unit/nest/assistant/assistant-home.test.ts +++ b/server/tests/unit/nest/assistant/assistant-home.test.ts @@ -185,12 +185,22 @@ describe("the way into a trip's chat", () => { describe('the tools the home chat keeps', () => { it('ASTHOME-030: every read, and only the writes that name no trip and no journey', () => { - expect(keptAtHome('read', ['tripId'])).toBe(true); - expect(keptAtHome('read', ['journeyId'])).toBe(true); - expect(keptAtHome('write', ['country_code'])).toBe(true); - expect(keptAtHome('write', [])).toBe(true); - for (const arg of ['tripId', 'journeyId', 'trip_id', 'trip_ids', 'journey_id']) { - expect(keptAtHome('write', ['name', arg]), arg).toBe(false); + expect(keptAtHome('read', ['tripId'], 'TripsMcp')).toBe(true); + expect(keptAtHome('read', ['journeyId'], 'JourneyMcp')).toBe(true); + expect(keptAtHome('write', ['country_code'], 'AtlasMcp')).toBe(true); + expect(keptAtHome('write', [], undefined)).toBe(true); + for (const arg of ['tripId', 'journeyId', 'trip_id', 'trip_ids', 'journey_id', 'journalId']) { + expect(keptAtHome('write', ['name', arg], undefined), arg).toBe(false); } }); + + it("ASTHOME-031: a journey's writes stay with the journey, also those that name only an entry", () => { + expect(keptAtHome('read', ['entryId'], 'JourneyMcp')).toBe(true); + for (const className of ['JourneyMcp', 'JourneyRecordingsMcp', 'JourneyDayImportMcp']) { + expect(keptAtHome('write', ['entryId'], className), className).toBe(false); + } + // A Dawarich stay taken into a journal names the journey as `journalId`; one marked on the bucket list stays. + expect(keptAtHome('write', ['suggestionId', 'journalId'], 'DawarichMcp')).toBe(false); + expect(keptAtHome('write', ['suggestionId', 'bucketListItemId'], 'DawarichMcp')).toBe(true); + }); });