Surgical file edits for AI coding agents. Read and patch only the lines that change — with a content-hash tag that rejects stale edits — instead of rewriting whole files for a 2-line fix.
Core patch protocol concept (line-anchored edits + content-hash verification) is adapted from Oh My Pi (MIT License, © 2025 Mario Zechner, 2025-2026 Can Bölük). This project reimplements it as a standalone CLI/library for AI coding agent workflows, built and refined with Codex + GPT-5.6 during OpenAI Build Week, adding stale-tag rejection, in-process snapshot history, and multi-operation batch patching (SWAP / DEL / INS.POST / INS.PRE / INS.HEAD / INS.TAIL).
Test the protocol directly in your browser — no clone, no install, no login:
https://aria-os.online/u/036af0799412/sites/patchcode-demo/
Edit the sample file, click "Read + Hash", apply a patch, then click "Simulate Stale Edit" to see the safety rejection in action.
LLM coding agents — including Codex — routinely re-read and rewrite entire files to make a one-line change. On a 2,000-line file, that's thousands of wasted tokens per edit, every edit, all session long. This was built to solve a real, measured problem in a production AI agent system (ARIA / Aria-OS), where it's used in a live MCP tool serving real file-edit requests.
patchcode read file.js 40 60→ returns[file.js#a1b2]+ numbered lines 40-60- Agent proposes a patch referencing that exact hash tag
patchcode patch file.js patch.txt→ validates the hash still matches (rejects if the file changed since read), applies the diff, returns the new hash
git clone https://github.com/leofroeder-stack/patchcode-cli.git
cd patchcode-cli
node cli.js read <file> <startLine> <endLine>
node cli.js patch <file> <patchfile>[server.js#a1b2]
SWAP 105.=107:
+ const newLine = 'replacement';
INS.POST 120:
+ console.log('inserted after 120');
DEL 130.=132
Supported operations:
| Op | Meaning |
|---|---|
SWAP N.=M: |
Replace lines N through M with the following + body lines |
DEL N.=M or DEL N |
Delete a line range |
INS.POST N: |
Insert body lines after line N |
INS.PRE N: |
Insert body lines before line N |
INS.HEAD: |
Insert body lines at the start of the file |
INS.TAIL: |
Insert body lines at the end of the file |
| Approach | Tokens for a 2-line fix in a 2,000-line file |
|---|---|
| Full file rewrite | ~15,000 |
| PatchCode patch | ~150 |
| Savings | ~99% |
Snapshot/version history is kept in-memory per process. Since each CLI invocation spawns a new Node process, that history does not persist between separate calls. This does not affect the core safety guarantee — the content hash is always recomputed fresh from the file on disk, so stale-tag rejection works correctly regardless of process lifetime. A future version could persist snapshots to disk for cross-invocation history.
This CLI was built and refined inside OpenAI Codex during Build Week using
GPT-5.6. Codex wrote the CLI wrapper, fixed a real module-resolution bug
(relative require path failing when run from outside the project directory,
fixed with path.join(__dirname, 'patchcode.js')), and flagged the in-memory
snapshot limitation documented above.
Codex Session / Feedback ID: 019f8112-1733-75d1-8380-4ebb73b3431d
The core engine already runs in production inside Aria-OS, an AI agent platform, as an MCP tool used for real file edits by AI agents working on the platform's own codebase. This repo generalizes it into a standalone, dependency-free CLI usable by any coding agent or workflow.
MIT