From 2c369a21c976c190cfc3a9550d4c41c18a66ce52 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Wed, 23 Sep 2026 21:29:20 -0500 Subject: [PATCH] feat(core): note renamed legacy tools when migrating V1 sessions (#50188) Co-authored-by: rekram1-node --- .../core/src/database/v1-migration.bun.ts | 74 ++++++++++++++- packages/core/test/v1-migration.test.ts | 95 +++++++++++++++++++ 2 files changed, 167 insertions(+), 2 deletions(-) diff --git a/packages/core/src/database/v1-migration.bun.ts b/packages/core/src/database/v1-migration.bun.ts index 11f6a331148..2a78031ad3a 100644 --- a/packages/core/src/database/v1-migration.bun.ts +++ b/packages/core/src/database/v1-migration.bun.ts @@ -436,6 +436,24 @@ export function transformSession(input: TransformInput): TransformResult { ] }) .map((item, seq) => ({ ...item, seq })) + const last = projected.at(-1) + const notice = last === undefined ? undefined : legacyToolNotice(projected) + const migrated = + last === undefined || notice === undefined + ? projected + : [ + ...projected, + { + ...last, + id: syntheticID(last.id, used), + type: "system" as const, + seq: projected.length, + data: { + text: notice, + time: { created: last.time_created }, + }, + }, + ] const assistants = messages .map((item) => item.value) .filter((item): item is SessionV1.Assistant => item.role === "assistant") @@ -446,7 +464,7 @@ export function transformSession(input: TransformInput): TransformResult { return !owned.some((part) => part.value.type === "subtask") || !owned.every((part) => part.value.type === "subtask") }) return { - messages: projected, + messages: migrated, session: { agent: input.session.agent ?? (latestUser?.value.role === "user" ? latestUser.value.agent : null), model: @@ -467,7 +485,7 @@ export function transformSession(input: TransformInput): TransformResult { revert: null, time_compacting: null, }, - watermark: projected.length - 1, + watermark: migrated.length - 1, warnings, } } @@ -878,6 +896,58 @@ function row( } } +const RENAMED_TOOLS: Readonly> = { bash: "shell", task: "subagent", apply_patch: "patch" } +const PATH_TOOLS = ["read", "edit", "write"] +const REMOVED_TOOLS = ["todowrite"] + +/** + * Tells the model about V1 tools it called in the still-visible history whose + * V2 names or arguments differ, so it does not repeat those calls. Only tools + * that actually appear after the last compaction are mentioned. + */ +function legacyToolNotice(messages: ReadonlyArray) { + const start = messages.findLastIndex((message) => message.type === "compaction") + const called = new Set( + messages.slice(start + 1).flatMap((message) => { + if (message.type !== "assistant" || !Array.isArray(message.data.content)) return [] + return message.data.content.flatMap((item: Record) => + item.type === "tool" && typeof item.name === "string" ? [item.name] : [], + ) + }), + ) + const list = (names: ReadonlyArray) => names.map((name) => `\`${name}\``).join(", ") + const renamed = Object.keys(RENAMED_TOOLS).filter((name) => called.has(name)) + const paths = PATH_TOOLS.filter((name) => called.has(name)) + const removed = REMOVED_TOOLS.filter((name) => called.has(name)) + const parts = [ + ...(renamed.length === 1 + ? [`The \`${renamed[0]}\` tool is now \`${RENAMED_TOOLS[renamed[0]]}\` and must be called by that name.`] + : renamed.length > 1 + ? [ + `The following tools were renamed and must be called by their new names: ${renamed + .map((name) => `\`${name}\` is now \`${RENAMED_TOOLS[name]}\``) + .join("; ")}.`, + ] + : []), + ...(called.has("task") + ? ["The `subagent` tool takes `agent` instead of `subagent_type` and `sessionID` instead of `task_id`."] + : []), + ...(paths.length === 1 + ? [`The \`${paths[0]}\` tool now takes \`path\` instead of \`filePath\`.`] + : paths.length > 1 + ? [`The following tools now take \`path\` instead of \`filePath\`: ${list(paths)}.`] + : []), + ...(called.has("skill") ? ["The `skill` tool now takes `id` instead of `name`."] : []), + ...(removed.length === 1 + ? [`The \`${removed[0]}\` tool is no longer available and must not be called.`] + : removed.length > 1 + ? [`The following tools are no longer available and must not be called: ${list(removed)}.`] + : []), + ] + if (parts.length === 0) return undefined + return ["The available tools have changed.", ...parts].join("\n\n") +} + function migrateTool(part: typeof SessionV1.ToolPart.Type, fallback: number) { const base = { type: "tool" as const, diff --git a/packages/core/test/v1-migration.test.ts b/packages/core/test/v1-migration.test.ts index fdddbdc421b..f0201e6cb68 100644 --- a/packages/core/test/v1-migration.test.ts +++ b/packages/core/test/v1-migration.test.ts @@ -469,6 +469,101 @@ describe("V1Migration.transformSession", () => { }) }) + test("appends a legacy tool notice naming only the renamed or removed tools that were called", () => { + const parent = user("msg_000000000060aaaaaaaaaaaaaa") + const message = assistant("msg_000000000061aaaaaaaaaaaaaa", parent.id) + const call = (id: string, tool: string) => + part(id, message.id, { + type: "tool", + callID: id, + tool, + state: { status: "completed", input: {}, output: "ok", title: tool, metadata: {}, time: { start: 1, end: 2 } }, + }) + const result = transform( + [parent, message], + [ + call("prt_1", "bash"), + call("prt_2", "read"), + call("prt_3", "glob"), + call("prt_4", "todowrite"), + call("prt_5", "task"), + call("prt_6", "apply_patch"), + ], + ) + expect(result.messages.map((row) => [row.type, row.seq])).toEqual([ + ["user", 0], + ["assistant", 1], + ["system", 2], + ]) + expect(result.messages[2]).toMatchObject({ + session_id: "ses_test", + time_created: 20, + time_updated: 21, + data: { + text: [ + "The available tools have changed.", + "The following tools were renamed and must be called by their new names: `bash` is now `shell`; `task` is now `subagent`; `apply_patch` is now `patch`.", + "The `subagent` tool takes `agent` instead of `subagent_type` and `sessionID` instead of `task_id`.", + "The `read` tool now takes `path` instead of `filePath`.", + "The `todowrite` tool is no longer available and must not be called.", + ].join("\n\n"), + time: { created: 20 }, + }, + }) + expect(result.messages[2]?.id).toMatch(/^msg_/) + expect(result.watermark).toBe(2) + }) + + test("omits the legacy tool notice when no legacy tools were called or they precede a compaction", () => { + const parent = user("msg_000000000062aaaaaaaaaaaaaa") + const message = assistant("msg_000000000063aaaaaaaaaaaaaa", parent.id) + const call = (id: string, messageID: string, tool: string) => + part(id, messageID, { + type: "tool", + callID: id, + tool, + state: { status: "completed", input: {}, output: "ok", title: tool, metadata: {}, time: { start: 1, end: 2 } }, + }) + expect(transform([parent, message], [call("prt_1", message.id, "glob")]).messages.map((row) => row.type)).toEqual([ + "user", + "assistant", + ]) + + const compact = user("msg_000000000064aaaaaaaaaaaaaa", {}, 30) + const summary = assistant("msg_000000000065aaaaaaaaaaaaaa", compact.id, { summary: true }, 31) + const later = user("msg_000000000066aaaaaaaaaaaaaa", {}, 40) + const answer = assistant("msg_000000000067aaaaaaaaaaaaaa", later.id, {}, 41) + const result = transform( + [parent, message, compact, summary, later, answer], + [ + call("prt_1", message.id, "bash"), + part("prt_2", compact.id, { type: "compaction", auto: true }), + part("prt_3", summary.id, { type: "text", text: "summary" }), + part("prt_4", later.id, { type: "text", text: "again" }), + call("prt_5", answer.id, "skill"), + call("prt_6", answer.id, "bash"), + call("prt_7", answer.id, "edit"), + call("prt_8", answer.id, "write"), + ], + ) + expect(result.messages.map((row) => row.type)).toEqual([ + "user", + "assistant", + "compaction", + "user", + "assistant", + "system", + ]) + expect(result.messages[5]?.data).toMatchObject({ + text: [ + "The available tools have changed.", + "The `bash` tool is now `shell` and must be called by that name.", + "The following tools now take `path` instead of `filePath`: `edit`, `write`.", + "The `skill` tool now takes `id` instead of `name`.", + ].join("\n\n"), + }) + }) + test("normalizes assistant errors and finish reasons", () => { const parent = user("msg_000000000010aaaaaaaaaaaaaa") const cases = [