From 448606c9f489e1e99f4df68c5180bb4633a1b7c4 Mon Sep 17 00:00:00 2001 From: Roomote Date: Sun, 4 Oct 2026 22:11:31 +0000 Subject: [PATCH] Revert "fix(prompts): prevent models from hallucinating line numbers in apply_diff (#641)" This reverts commit a077066538cbf4306e7a3f1d0761f07c9cdcac0b. --- .../native-tools/__tests__/apply_diff.spec.ts | 79 ------------------- .../prompts/tools/native-tools/apply_diff.ts | 8 +- 2 files changed, 3 insertions(+), 84 deletions(-) delete mode 100644 src/core/prompts/tools/native-tools/__tests__/apply_diff.spec.ts diff --git a/src/core/prompts/tools/native-tools/__tests__/apply_diff.spec.ts b/src/core/prompts/tools/native-tools/__tests__/apply_diff.spec.ts deleted file mode 100644 index 8b23fa82fe..0000000000 --- a/src/core/prompts/tools/native-tools/__tests__/apply_diff.spec.ts +++ /dev/null @@ -1,79 +0,0 @@ -import type OpenAI from "openai" -import { apply_diff } from "../apply_diff" - -// Helper type to access function tools -type FunctionTool = OpenAI.Chat.ChatCompletionTool & { type: "function" } - -// Helper to get function definition from tool -const getFunctionDef = (tool: OpenAI.Chat.ChatCompletionTool) => (tool as FunctionTool).function - -// OpenAI types the parameter schema as FunctionParameters (Record). -// Narrow it with runtime checks instead of assertions so the spec tracks schema changes. -function asRecord(value: unknown): Record { - if (typeof value !== "object" || value === null) { - throw new Error("expected an object value in the apply_diff parameter schema") - } - return value as Record -} - -const getDiffDescription = (): string => { - const parameters = asRecord(getFunctionDef(apply_diff).parameters) - const properties = asRecord(parameters["properties"]) - const diff = asRecord(properties["diff"]) - const description = diff["description"] - if (typeof description !== "string") { - throw new Error("diff parameter must declare a string description") - } - return description -} - -const getRequiredParameters = (): unknown[] => { - const parameters = asRecord(getFunctionDef(apply_diff).parameters) - const required = parameters["required"] - if (!Array.isArray(required)) { - throw new Error("apply_diff parameters must declare a required array") - } - return required -} - -describe("apply_diff tool description", () => { - const description = getFunctionDef(apply_diff).description - - describe("read_file line-number format", () => { - it("should document line numbers using the actual read_file output format (number, spaces, pipe)", () => { - // The real format comes from addLineNumbers() in extract-text.ts: "N | content" - expect(description).toContain("'1 | '") - expect(description).toContain("'2 | '") - }) - - it("should not describe read_file line numbers with a colon suffix", () => { - // Regression guard: read_file never outputs "N: " prefixes (extract-text.ts addLineNumbers), - // and the server-side auto-strip in multi-search-replace.ts only keys on the "N | " pattern. - expect(description).not.toContain("'1: '") - expect(description).not.toContain("'2: '") - }) - - it("should instruct the model to strip line numbers from the SEARCH block", () => { - expect(description).toContain("MUST STRIP all line numbers from the 'SEARCH' block") - }) - }) - - describe("diff parameter description", () => { - const diffDescription = getDiffDescription() - - it("should use the actual read_file output format in the line-number prohibition bullet", () => { - expect(diffDescription).toContain("'137 | '") - expect(diffDescription).not.toContain("'137: '") - }) - - it("should keep the template placeholder free of line numbers", () => { - expect(diffDescription).toContain("[exact content to find WITHOUT line numbers]") - }) - }) - - describe("schema", () => { - it("should require path and diff parameters", () => { - expect(getRequiredParameters()).toEqual(["path", "diff"]) - }) - }) -}) diff --git a/src/core/prompts/tools/native-tools/apply_diff.ts b/src/core/prompts/tools/native-tools/apply_diff.ts index f799ae7534..cdbd99df33 100644 --- a/src/core/prompts/tools/native-tools/apply_diff.ts +++ b/src/core/prompts/tools/native-tools/apply_diff.ts @@ -1,13 +1,12 @@ import type OpenAI from "openai" -const APPLY_DIFF_DESCRIPTION = `Apply precise, targeted modifications to an existing file using one or more search/replace blocks. This tool is for surgical edits only; the 'SEARCH' block must exactly match the existing content, including whitespace and indentation. To make multiple targeted changes, provide multiple SEARCH/REPLACE blocks in the 'diff' parameter. Use the 'read_file' tool first if you are not confident in the exact content to search for. -CRITICAL WARNING: The 'read_file' tool outputs line numbers (e.g. '1 | ', '2 | ') for your convenience, but these line numbers DO NOT exist in the actual file! You MUST STRIP all line numbers from the 'SEARCH' block. Your 'SEARCH' block must match the RAW file content exactly.` +const APPLY_DIFF_DESCRIPTION = `Apply precise, targeted modifications to an existing file using one or more search/replace blocks. This tool is for surgical edits only; the 'SEARCH' block must exactly match the existing content, including whitespace and indentation. To make multiple targeted changes, provide multiple SEARCH/REPLACE blocks in the 'diff' parameter. Use the 'read_file' tool first if you are not confident in the exact content to search for.` const DIFF_PARAMETER_DESCRIPTION = `A string containing one or more search/replace blocks defining the changes. The ':start_line:' is strongly recommended and indicates the starting line number of the original content. You must not add a start line for the replacement content. Each block must follow this format: <<<<<<< SEARCH :start_line:[line_number] ------- -[exact content to find WITHOUT line numbers] +[exact content to find] ======= [new content to replace with] >>>>>>> REPLACE @@ -15,8 +14,7 @@ const DIFF_PARAMETER_DESCRIPTION = `A string containing one or more search/repla CRITICAL: - The ':start_line:[line_number]' header is strongly recommended for accurate matching. When provided, it must follow the exact syntax ':start_line:[integer]' (for example: ':start_line:220'). Do not write headers with shorthand forms like ':220' or variations like ':start_line=220'. - Copy the exact lines from the source file for a 100% string match including all whitespace, indentation, and newlines. -- Ensure the separator '-------' is on its own line immediately following ':start_line:[line_number]' with a newline. -- DO NOT include line numbers (like '137 | ') in the [exact content to find] section! It must match the actual file content, which does not have line numbers.` +- Ensure the separator '-------' is on its own line immediately following ':start_line:[line_number]' with a newline.` export const apply_diff = { type: "function",