From a2ccb66ce4668aa11a1badfd202fb34f1355e38a Mon Sep 17 00:00:00 2001
From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
Date: Sun, 1 Nov 2026 21:13:38 -0600
Subject: [PATCH] refactor(core): share the instruction list update renderer
(#61167)
---
packages/core/src/instructions/index.ts | 30 ++++++++++++++
packages/core/src/mcp/instructions.ts | 43 +++++++--------------
packages/core/src/reference/instructions.ts | 43 +++++++--------------
packages/core/src/skill/instructions.ts | 42 ++++++--------------
4 files changed, 68 insertions(+), 90 deletions(-)
diff --git a/packages/core/src/instructions/index.ts b/packages/core/src/instructions/index.ts
index 49ef600aa36..918dd0e1fef 100644
--- a/packages/core/src/instructions/index.ts
+++ b/packages/core/src/instructions/index.ts
@@ -236,6 +236,36 @@ export function diffByKey(
}
}
+/**
+ * Renders a keyed list update: added, changed, and removed entries as deltas,
+ * or the full list restated when no entry differs.
+ */
+export function listUpdate(input: {
+ readonly key: (value: A) => string
+ readonly changed: (previous: A, current: A) => boolean
+ readonly entries: (values: ReadonlyArray) => ReadonlyArray
+ readonly render: (values: ReadonlyArray) => string
+ readonly text: {
+ readonly replaced: string
+ readonly added: string
+ readonly changed: string
+ readonly removed: (keys: string) => string
+ }
+}) {
+ return (previous: ReadonlyArray, current: ReadonlyArray) => {
+ const diff = diffByKey(previous, current, input.key, input.changed)
+ if (diff.added.length === 0 && diff.changed.length === 0 && diff.removed.length === 0)
+ return [input.text.replaced, input.render(current)].join("\n")
+ return [
+ ...(diff.added.length === 0 ? [] : [input.text.added, ...input.entries(diff.added)]),
+ ...(diff.changed.length === 0
+ ? []
+ : [input.text.changed, ...input.entries(diff.changed.map((change) => change.current))]),
+ ...(diff.removed.length === 0 ? [] : [input.text.removed(diff.removed.map(input.key).join(", "))]),
+ ].join("\n")
+ }
+}
+
function render(parts: ReadonlyArray) {
return parts.join("\n\n")
}
diff --git a/packages/core/src/mcp/instructions.ts b/packages/core/src/mcp/instructions.ts
index 7866e4af919..d8d0871f4b4 100644
--- a/packages/core/src/mcp/instructions.ts
+++ b/packages/core/src/mcp/instructions.ts
@@ -28,36 +28,19 @@ const entries = (servers: ReadonlyArray) =>
const render = (servers: ReadonlyArray) =>
["", ...entries(servers), ""].join("\n")
-const update = (previous: ReadonlyArray, current: ReadonlyArray) => {
- const diff = Instructions.diffByKey(
- previous,
- current,
- (server) => server.server,
- (before, after) => before.instructions !== after.instructions || before.codemode !== after.codemode,
- )
- // 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),
- ].join("\n")
- return [
- ...(diff.added.length === 0
- ? []
- : ["New MCP server instructions are available in addition to those previously listed:", ...entries(diff.added)]),
- ...(diff.changed.length === 0
- ? []
- : [
- "The following MCP server instructions have changed. Each entry replaces the previously listed server with the same name:",
- ...entries(diff.changed.map((change) => change.current)),
- ]),
- ...(diff.removed.length === 0
- ? []
- : [
- `Instructions for the following MCP servers are no longer available: ${diff.removed.map((server) => server.server).join(", ")}.`,
- ]),
- ].join("\n")
-}
+const update = Instructions.listUpdate({
+ key: (server) => server.server,
+ changed: (before, after) => before.instructions !== after.instructions || before.codemode !== after.codemode,
+ entries,
+ render,
+ text: {
+ replaced: "The available MCP server instructions have changed. This list supersedes the previous one.",
+ added: "New MCP server instructions are available in addition to those previously listed:",
+ changed:
+ "The following MCP server instructions have changed. Each entry replaces the previously listed server with the same name:",
+ removed: (names) => `Instructions for the following MCP servers are no longer available: ${names}.`,
+ },
+})
export interface Interface {
/** Lists server instructions reachable under the given ruleset; callers pass the merged agent and Session permissions. */
diff --git a/packages/core/src/reference/instructions.ts b/packages/core/src/reference/instructions.ts
index 5fdbaa161f5..44d2cf8a79b 100644
--- a/packages/core/src/reference/instructions.ts
+++ b/packages/core/src/reference/instructions.ts
@@ -28,36 +28,19 @@ const render = (references: ReadonlyArray) =>
"",
].join("\n")
-const update = (previous: ReadonlyArray, current: ReadonlyArray) => {
- const diff = Instructions.diffByKey(
- previous,
- current,
- (reference) => reference.name,
- (before, after) => before.path !== after.path || before.description !== after.description,
- )
- // 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),
- ].join("\n")
- return [
- ...(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
- ? []
- : [
- `The following project references are no longer available and must not be used: ${diff.removed.map((reference) => reference.name).join(", ")}.`,
- ]),
- ].join("\n")
-}
+const update = Instructions.listUpdate({
+ key: (reference) => reference.name,
+ changed: (before, after) => before.path !== after.path || before.description !== after.description,
+ entries,
+ render,
+ text: {
+ replaced: "The available project references have changed. This list supersedes the previous reference list.",
+ added: "New project references are available in addition to those previously listed:",
+ changed:
+ "The following project references have changed. Each entry replaces the previously listed reference with the same name:",
+ removed: (names) => `The following project references are no longer available and must not be used: ${names}.`,
+ },
+})
export interface Interface {
readonly load: () => Effect.Effect
diff --git a/packages/core/src/skill/instructions.ts b/packages/core/src/skill/instructions.ts
index e26c3f19e9d..ab838bffe0e 100644
--- a/packages/core/src/skill/instructions.ts
+++ b/packages/core/src/skill/instructions.ts
@@ -32,36 +32,18 @@ const render = (skills: ReadonlyArray) =>
: ["", ...entries(skills), ""]),
].join("\n")
-const update = (previous: ReadonlyArray, current: ReadonlyArray) => {
- const diff = Instructions.diffByKey(
- previous,
- current,
- (skill) => skill.id,
- (before, after) => before.name !== after.name || before.description !== after.description,
- )
- // 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),
- ].join("\n")
- return [
- ...(diff.added.length === 0
- ? []
- : ["New skills are available in addition to those previously listed:", ...entries(diff.added)]),
- ...(diff.changed.length === 0
- ? []
- : [
- "The following skills have changed. Each entry replaces the previously listed skill with the same ID:",
- ...entries(diff.changed.map((change) => change.current)),
- ]),
- ...(diff.removed.length === 0
- ? []
- : [
- `The following skill IDs are no longer available and must not be used: ${diff.removed.map((skill) => skill.id).join(", ")}.`,
- ]),
- ].join("\n")
-}
+const update = Instructions.listUpdate({
+ key: (skill) => skill.id,
+ changed: (before, after) => before.name !== after.name || before.description !== after.description,
+ entries,
+ render,
+ text: {
+ replaced: "The available skills have changed. This list supersedes the previous available skills list.",
+ added: "New skills are available in addition to those previously listed:",
+ changed: "The following skills have changed. Each entry replaces the previously listed skill with the same ID:",
+ removed: (ids) => `The following skill IDs are no longer available and must not be used: ${ids}.`,
+ },
+})
export interface Interface {
/** Lists skills the given ruleset does not deny; callers pass the merged agent and Session permissions. */