Files
oh-my-pi/packages/coding-agent/src/prompts/tools/patch.md
T
can1357 84e1636ad3 docs(coding-agent/prompts): updated patch tool documentation to clarify formatter usage and whitespace handling
- Updated patch tool documentation to clarify formatter usage and whitespace handling guidance.
2026-02-02 15:17:48 +01:00

2.8 KiB
Raw Blame History

Edit

Patch operations on file given diff. Primary tool for existing-file edits.

**Hunk Headers:** - `@@` — bare header when context lines unique - `@@ $ANCHOR` — anchor copied verbatim from file (full line or unique substring) **Anchor Selection:** 1. Otherwise choose highly specific anchor copied from file: - full function signature - class declaration - unique string literal/error message - config key with uncommon name 2. On "Found multiple matches": add context lines, use multiple hunks with separate anchors, or use longer anchor substring **Context Lines:** Use enough ` `-prefixed lines to make match unique (usually 2–8) When editing structured blocks (nested braces, tags, indented regions), include opening and closing lines so edit stays inside block ```ts type T = // Diff is one or more hunks in the same file. // - Each hunk begins with "@@" (anchor optional). // - Each hunk body only has lines starting with ' ' | '+' | '-'. // - Each hunk includes at least one change (+ or -). | { path: string, op: "update", diff: string } // Diff is full file content, no prefixes. | { path: string, op: "create", diff: string } // No diff for delete. | { path: string, op: "delete" } // New path for update+move. | { path: string, op: "update", rename: string, diff: string } ``` Returns success/failure; on failure, error message indicates: - "Found multiple matches" — anchor/context not unique enough - "No match found" — context lines don't exist in file (wrong content or stale read) - Syntax errors in diff format - Always read target file before editing - Copy anchors and context lines verbatim (including whitespace) - Never use anchors as comments (no line numbers, location labels, placeholders like `@@ @@`) - Do not place new lines outside intended block - If edit fails or breaks structure, re-read file and produce new patch from current content—do not retry same diff - If indentation/alignment wrong after editing, run formatter (`go fmt`, `cargo fmt`, `ruff format`, `biome`, etc.)—never make repeated edit attempts to fix whitespace edit {"path":"hello.txt","op":"create","diff":"Hello\n"} edit {"path":"src/app.py","op":"update","diff":"@@ def greet():\n def greet():\n-print('Hi')\n+print('Hello')\n"} edit {"path":"src/app.py","op":"update","rename":"src/main.py","diff":"@@\n ...\n"} edit {"path":"obsolete.txt","op":"delete"} - Generic anchors: `import`, `export`, `describe`, `function`, `const` - Repeating same addition in multiple hunks (duplicate blocks) - Full-file overwrites for minor changes (acceptable for major restructures or short files)