A git-diff/patch-inspired edit tool for the pi coding agent. Shadows pi's built-in edit with a unified-diff interface.
Replaces pi's edit tool (exact oldText/newText replacement) with one that accepts a unified diff — the format git diff produces, and the format LLMs are most fluent in.
@@ -10,7 +10,7 @@ class Foo {
def bar(self):
- return 1
+ return 2- Multiple hunks for one file in a single call
- Fuzzy matching: tolerates line-number drift and trailing-whitespace differences via context lines (powered by the
diffnpm package) - Section-header guidance: the text after
@@(e.g.class Foo:) validates the target scope and corrects obviously-wrong line numbers - Line numbers respected:
oldStartis treated as a reliable hint (fromgit diff/grep, or approximately-right LLM guesses) - Edits only: existing files only. Use
writeto create files,bash(mv/rm) for renames/deletions
pi install git:github.com/JerryAZR/pi-patch-editOr try without installing:
pi -e git:github.com/JerryAZR/pi-patch-editThe tool shadows the built-in edit automatically (pi prints a shadow warning on load).
- Parse the unified diff with
diff'sparsePatch; reject multi-file diffs (the tool takes an explicitpath). - Validate captions: if a hunk's section header (text after
@@) doesn't appear as a line in the file, reject the whole patch — likely the wrong file. - Correct
oldStart: if a captioned hunk'soldStartpoints before the caption's first occurrence (nonsensical — the hunk can't be inside a scope that starts later), re-derive it to the first context line at/after the anchor. Otherwise keepoldStartas-is. - Apply all hunks in one batched
applyPatchcall on the whole source.
npm install # installs diff + pi peer deps for typecheck
npm test # 26 tests, no external deps
npm run typecheckMIT