From 15adc3d91ecc6955ee0918607a5593e23bd4d538 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 20 Oct 2026 23:09:10 -0500 Subject: [PATCH] fix(core): send only changed entries in instruction updates (#57626) --- packages/core/src/mcp/instructions.ts | 10 ++++-- packages/core/src/reference/instructions.ts | 10 ++++-- packages/core/src/skill/instructions.ts | 10 ++++-- packages/core/test/mcp-instructions.test.ts | 14 ++------ .../core/test/reference-instructions.test.ts | 36 +++++++++++++++++++ packages/core/test/skill/instructions.test.ts | 24 +++++++------ 6 files changed, 77 insertions(+), 27 deletions(-) diff --git a/packages/core/src/mcp/instructions.ts b/packages/core/src/mcp/instructions.ts index 3517b74ce9d..b6cfd31bef9 100644 --- a/packages/core/src/mcp/instructions.ts +++ b/packages/core/src/mcp/instructions.ts @@ -35,8 +35,8 @@ const update = (previous: ReadonlyArray, current: ReadonlyArray server.server, (before, after) => before.instructions !== after.instructions || before.codemode !== after.codemode, ) - // Additions and removals render as small deltas; anything else restates the full list. - if (diff.changed.length > 0 || (diff.added.length === 0 && diff.removed.length === 0)) + // Added, changed, and removed servers render as deltas; the full list is restated only when no entry differs. + if (diff.added.length === 0 && diff.changed.length === 0 && diff.removed.length === 0) return [ "The available MCP server instructions have changed. This list supersedes the previous one.", render(current), @@ -45,6 +45,12 @@ const update = (previous: ReadonlyArray, current: ReadonlyArray change.current)), + ]), ...(diff.removed.length === 0 ? [] : [ diff --git a/packages/core/src/reference/instructions.ts b/packages/core/src/reference/instructions.ts index 36d40887339..5fdbaa161f5 100644 --- a/packages/core/src/reference/instructions.ts +++ b/packages/core/src/reference/instructions.ts @@ -35,8 +35,8 @@ const update = (previous: ReadonlyArray, current: ReadonlyA (reference) => reference.name, (before, after) => before.path !== after.path || before.description !== after.description, ) - // Additions and removals render as small deltas; anything else restates the full list. - if (diff.changed.length > 0 || (diff.added.length === 0 && diff.removed.length === 0)) + // Added, changed, and removed references render as deltas; the full list is restated only when no entry differs. + if (diff.added.length === 0 && diff.changed.length === 0 && diff.removed.length === 0) return [ "The available project references have changed. This list supersedes the previous reference list.", render(current), @@ -45,6 +45,12 @@ const update = (previous: ReadonlyArray, current: ReadonlyA ...(diff.added.length === 0 ? [] : ["New project references are available in addition to those previously listed:", ...entries(diff.added)]), + ...(diff.changed.length === 0 + ? [] + : [ + "The following project references have changed. Each entry replaces the previously listed reference with the same name:", + ...entries(diff.changed.map((change) => change.current)), + ]), ...(diff.removed.length === 0 ? [] : [ diff --git a/packages/core/src/skill/instructions.ts b/packages/core/src/skill/instructions.ts index 14811dace4f..e26c3f19e9d 100644 --- a/packages/core/src/skill/instructions.ts +++ b/packages/core/src/skill/instructions.ts @@ -39,8 +39,8 @@ const update = (previous: ReadonlyArray, current: ReadonlyArray skill.id, (before, after) => before.name !== after.name || before.description !== after.description, ) - // Additions and removals render as small deltas; anything else restates the full list. - if (diff.changed.length > 0 || (diff.added.length === 0 && diff.removed.length === 0)) + // Added, changed, and removed skills render as deltas; the full list is restated only when no entry differs. + if (diff.added.length === 0 && diff.changed.length === 0 && diff.removed.length === 0) return [ "The available skills have changed. This list supersedes the previous available skills list.", render(current), @@ -49,6 +49,12 @@ const update = (previous: ReadonlyArray, current: ReadonlyArray change.current)), + ]), ...(diff.removed.length === 0 ? [] : [ diff --git a/packages/core/test/mcp-instructions.test.ts b/packages/core/test/mcp-instructions.test.ts index 906a6d78ca8..8635a80d0e4 100644 --- a/packages/core/test/mcp-instructions.test.ts +++ b/packages/core/test/mcp-instructions.test.ts @@ -116,7 +116,7 @@ describe("McpInstructions", () => { ), ) - it.effect("restates guidance when Code Mode is disabled for a server", () => { + it.effect("replaces a server entry when Code Mode is disabled for it", () => { let tools: Mcp.Tool[] = [tool("alpha")] return Effect.gen(function* () { const service = yield* McpInstructions.Service @@ -126,12 +126,10 @@ describe("McpInstructions", () => { const changed = yield* readUpdate(yield* service.load([]), initialized) expect(changed.text).toBe( [ - "The available MCP server instructions have changed. This list supersedes the previous one.", - "", + "The following MCP server instructions have changed. Each entry replaces the previously listed server with the same name:", ' ', " Alpha instructions", " ", - "", ].join("\n"), ) }).pipe( @@ -167,17 +165,11 @@ describe("McpInstructions", () => { const changed = yield* readUpdate(yield* service.load([]), added) expect(changed.text).toBe( [ - "The available MCP server instructions have changed. This list supersedes the previous one.", - "", + "The following MCP server instructions have changed. Each entry replaces the previously listed server with the same name:", ' ', ' Use tools from this server through `execute` under `tools["alpha"]`.', " Updated alpha", " ", - ' ', - ' Use tools from this server through `execute` under `tools["beta"]`.', - " Beta instructions", - " ", - "", ].join("\n"), ) diff --git a/packages/core/test/reference-instructions.test.ts b/packages/core/test/reference-instructions.test.ts index b1e8fce2777..ee1338437ee 100644 --- a/packages/core/test/reference-instructions.test.ts +++ b/packages/core/test/reference-instructions.test.ts @@ -108,4 +108,40 @@ describe("ReferenceInstructions", () => { Effect.provide(instructionsLayer(Layer.mock(Reference.Service, { list: () => Effect.succeed(references) }))), ) }) + + it.effect("announces added, changed, and removed references in one delta", () => { + const reference = (name: string, description: string) => + Reference.Info.make({ + name, + path: AbsolutePath.make(`/${name}`), + description, + source: Reference.LocalSource.make({ type: "local", path: AbsolutePath.make(`/${name}`), description }), + }) + let references = [reference("docs", "Use for product documentation"), reference("legacy", "Use for old code")] + return Effect.gen(function* () { + const instructions = yield* ReferenceInstructions.Service + const initialized = yield* readInitial(yield* instructions.load()) + + references = [reference("docs", "Use for API documentation"), reference("examples", "Use for examples")] + expect((yield* readUpdate(yield* instructions.load(), initialized)).text).toBe( + [ + "New project references are available in addition to those previously listed:", + " ", + " examples", + " /examples", + " Use for examples", + " ", + "The following project references have changed. Each entry replaces the previously listed reference with the same name:", + " ", + " docs", + " /docs", + " Use for API documentation", + " ", + "The following project references are no longer available and must not be used: legacy.", + ].join("\n"), + ) + }).pipe( + Effect.provide(instructionsLayer(Layer.mock(Reference.Service, { list: () => Effect.succeed(references) }))), + ) + }) }) diff --git a/packages/core/test/skill/instructions.test.ts b/packages/core/test/skill/instructions.test.ts index 6af55cf3d0a..5529955f273 100644 --- a/packages/core/test/skill/instructions.test.ts +++ b/packages/core/test/skill/instructions.test.ts @@ -117,7 +117,7 @@ describe("SkillInstructions", () => { }).pipe(Effect.provide(layer(() => skills))) }) - it.effect("restates the full skill list when a description changes", () => { + it.effect("renders only the changed skill when a description changes", () => { const agent = Agent.Info.make(Agent.Info.default(build)) let skills = [effect] return Effect.gen(function* () { @@ -125,15 +125,19 @@ describe("SkillInstructions", () => { const initialized = yield* instructions.load(agent.permissions).pipe(Effect.flatMap(readInitial)) skills = [Skill.Info.make({ ...effect, description: "Build applications with Effect v4" })] - expect( - yield* instructions - .load(agent.permissions) - .pipe(Effect.flatMap((context) => readUpdate(context, initialized))), - ).toMatchObject({ - text: expect.stringContaining( - "The available skills have changed. This list supersedes the previous available skills list.", - ), - }) + const changed = yield* instructions + .load(agent.permissions) + .pipe(Effect.flatMap((context) => readUpdate(context, initialized))) + expect(changed.text).toBe( + [ + "The following skills have changed. Each entry replaces the previously listed skill with the same ID:", + " ", + " effect", + " Effect", + " Build applications with Effect v4", + " ", + ].join("\n"), + ) }).pipe(Effect.provide(layer(() => skills))) })