From 4f6203fefbf01f519424336da0823263bdd07396 Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Thu, 5 Nov 2026 21:24:58 +0000 Subject: [PATCH] fix(core): name the tools that changed within a Code Mode namespace --- packages/core/src/codemode/instructions.ts | 43 +++++++++++++++++++-- packages/core/test/codemode/catalog.test.ts | 29 ++++++++++++-- packages/core/test/session-runner.test.ts | 4 +- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/packages/core/src/codemode/instructions.ts b/packages/core/src/codemode/instructions.ts index d8b866c6242..47af8982b62 100644 --- a/packages/core/src/codemode/instructions.ts +++ b/packages/core/src/codemode/instructions.ts @@ -41,10 +41,14 @@ ${render(current)}` ) const parts = [ ...block("New tool namespaces are available:", sections.added), - ...block( - "These namespaces changed; their listings supersede the previous ones:", - sections.changed.map((change) => change.current), - ), + ...(sections.changed.length === 0 + ? [] + : [ + [ + "These namespaces changed; a namespace shown with its whole listing replaces the previous one:", + ...sections.changed.map((change) => changes(change.previous, change.current)), + ].join("\n"), + ]), ...(sections.removed.length === 0 ? [] : [ @@ -58,6 +62,37 @@ ${render(current)}` return delta.length < replacement.length ? delta : replacement } +// One namespace's change: the tools added, changed or gone under its new header, or its whole listing when that is +// shorter. A namespace too large to list in full can drop a name without losing the tool, so it says "listed". +function changes(previous: CodeModeCatalog.Section, current: CodeModeCatalog.Section) { + const names = Instructions.diffByKey(previous.names, current.names, toolName, (before, after) => before !== after) + const pinned = Instructions.diffByKey( + previous.pinned, + current.pinned, + (entry) => entry.path, + (before, after) => before.line !== after.line, + ) + const gone = [ + ...pinned.removed.map((entry) => entry.path.slice(current.name.length + 1)), + ...names.removed.map(toolName), + ] + const capped = current.pinned.length + current.names.length < current.count + const listed = [...names.added, ...names.changed.map((change) => change.current)] + const delta = [ + CodeModeCatalog.namespaceLine(current), + ...[...pinned.added, ...pinned.changed.map((change) => change.current)].map((entry) => entry.line), + ...(listed.length === 0 ? [] : [` - new or changed: ${listed.join(", ")}`]), + ...(gone.length === 0 ? [] : [` - no longer ${capped ? "listed" : "available"}: ${gone.join(", ")}`]), + ].join("\n") + const whole = CodeModeCatalog.renderSection(current) + return delta.length < whole.length ? delta : whole +} + +function toolName(listing: string) { + const open = listing.indexOf("(") + return open === -1 ? listing : listing.slice(0, open) +} + function block(heading: string, sections: ReadonlyArray) { return sections.length === 0 ? [] : [[heading, ...sections.map(CodeModeCatalog.renderSection)].join("\n")] } diff --git a/packages/core/test/codemode/catalog.test.ts b/packages/core/test/codemode/catalog.test.ts index c32ba93e918..6b3ec986f58 100644 --- a/packages/core/test/codemode/catalog.test.ts +++ b/packages/core/test/codemode/catalog.test.ts @@ -148,11 +148,32 @@ describe("CodeModeInstructions.update", () => { ) }) - test("restates only the sections that changed", () => { - const text = update([...tools("alpha", 3), ...tools("beta", 2)], [...tools("alpha", 4), ...tools("beta", 2)]) - expect(text).toBe( - "The Code Mode tool catalog has changed.\n\nThese namespaces changed; their listings supersede the previous ones:\n- alpha (4 tools)\n - tool0(id), tool1(id), tool2(id), tool3(id)", + test("names the tools that changed in a namespace", () => { + expect(update([...tools("alpha", 3), ...tools("beta", 2)], [...tools("alpha", 4), ...tools("beta", 2)])).toBe( + "The Code Mode tool catalog has changed.\n\n" + + "These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" + + "- alpha (4 tools)\n - new or changed: tool3(id)", ) + const changed = [entry("alpha.tool0", "x", { parameters: ["id", "…"] }), ...tools("alpha", 3).slice(1)] + expect(update(tools("alpha", 3), changed)).toContain("- alpha (3 tools)\n - new or changed: tool0(id, …)") + expect(update(tools("alpha", 10), tools("alpha", 10).slice(1))).toContain( + "- alpha (9 tools)\n - no longer available: tool0", + ) + }) + + test("says a name is no longer listed when its namespace is too large to list in full", () => { + const before = summarize(tools("bulk", 400), 40) + const after = summarize([entry("bulk.a_first", "First", { parameters: ["id"] }), ...tools("bulk", 400)], 40) + const text = CodeModeInstructions.update(before, after) + expect(text).toContain(" - new or changed: a_first(id)") + expect(text).toMatch(/ {2}- no longer listed: tool\d+/) + expect(text).not.toContain("no longer available") + }) + + test("restates a namespace's whole listing when that is shorter than its changes", () => { + const text = update(tools("alpha", 2), [entry("alpha.renamed", "x", { parameters: ["id"] })]) + expect(text).toContain("- alpha (1 tool)\n - renamed(id)") + expect(text).not.toContain("new or changed") }) test("restates a section whose description changed", () => { diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index b053400a95c..a420bf323f7 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -2429,7 +2429,9 @@ describe("SessionRunnerLLM", () => { yield* s.runPrompt("Fourth") const grown = yield* section("alpha") expect(updates(s.requests.at(-1)).at(-1)).toBe( - `The Code Mode tool catalog has changed.\n\nThese namespaces changed; their listings supersede the previous ones:\n${grown}`, + "The Code Mode tool catalog has changed.\n\n" + + "These namespaces changed; a namespace shown with its whole listing replaces the previous one:\n" + + "- alpha (13 tools)\n - new or changed: alpha_12(id, …)", ) // Compaction restates the stored catalog as the new baseline and has no update to send.