Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PatchCode

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).

🔴 Live Sandbox Demo (no install needed)

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.

The problem

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.

How it works

  1. patchcode read file.js 40 60 → returns [file.js#a1b2] + numbered lines 40-60
  2. Agent proposes a patch referencing that exact hash tag
  3. 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

Install

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>

Patch format

[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

Benchmark

Approach Tokens for a 2-line fix in a 2,000-line file
Full file rewrite ~15,000
PatchCode patch ~150
Savings ~99%

Known limitation

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.

Built with Codex

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

Origin

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.

License

MIT

About

Surgical, hash-verified file editing for AI coding agents — cuts token usage ~99% vs full-file rewrites

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages