refactor(core): share the instruction list update renderer (#61167)

This commit is contained in:
Aiden Cline
2026-11-01 21:13:38 -06:00
committed by GitHub
Unverified
parent dc683405c7
commit a2ccb66ce4
4 changed files with 68 additions and 90 deletions
+30
View File
@@ -236,6 +236,36 @@ export function diffByKey<A>(
}
}
/**
* Renders a keyed list update: added, changed, and removed entries as deltas,
* or the full list restated when no entry differs.
*/
export function listUpdate<A>(input: {
readonly key: (value: A) => string
readonly changed: (previous: A, current: A) => boolean
readonly entries: (values: ReadonlyArray<A>) => ReadonlyArray<string>
readonly render: (values: ReadonlyArray<A>) => string
readonly text: {
readonly replaced: string
readonly added: string
readonly changed: string
readonly removed: (keys: string) => string
}
}) {
return (previous: ReadonlyArray<A>, current: ReadonlyArray<A>) => {
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<string>) {
return parts.join("\n\n")
}
+13 -30
View File
@@ -28,36 +28,19 @@ const entries = (servers: ReadonlyArray<Summary>) =>
const render = (servers: ReadonlyArray<Summary>) =>
["<mcp_instructions>", ...entries(servers), "</mcp_instructions>"].join("\n")
const update = (previous: ReadonlyArray<Summary>, current: ReadonlyArray<Summary>) => {
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<Summary>({
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. */
+13 -30
View File
@@ -28,36 +28,19 @@ const render = (references: ReadonlyArray<typeof Summary.Type>) =>
"</available_references>",
].join("\n")
const update = (previous: ReadonlyArray<typeof Summary.Type>, current: ReadonlyArray<typeof Summary.Type>) => {
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<typeof Summary.Type>({
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<Instructions.List>
+12 -30
View File
@@ -32,36 +32,18 @@ const render = (skills: ReadonlyArray<Summary>) =>
: ["<available_skills>", ...entries(skills), "</available_skills>"]),
].join("\n")
const update = (previous: ReadonlyArray<Summary>, current: ReadonlyArray<Summary>) => {
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<Summary>({
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. */