Metadata-Version: 2.4
Name: apply-patch-py
Version: 0.4.1
Summary: Apply Codex-style patch blocks (*** Begin Patch ... *** End Patch) to a file
Project-URL: Homepage, https://github.com/marcius-llmus/apply-patch-py
Project-URL: Repository, https://github.com/marcius-llmus/apply-patch-py
Project-URL: Issues, https://github.com/marcius-llmus/apply-patch-py/issues
Project-URL: Changelog, https://github.com/marcius-llmus/apply-patch-py/releases
Author: marcin
License: MIT License
        
        Copyright (c) 2026 marcin
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: apply,codex,diff,llm,patch,tooling
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Version Control
Classifier: Typing :: Typed
Requires-Python: >=3.13
Requires-Dist: aiofiles>=24.1.0
Description-Content-Type: text/markdown

# apply-patch-py

Apply **Codex-style patch blocks** to a working directory.

This project is heavily inspired by (and effectively a Python port of) the patching mechanism used by **Codex**. Some tools (including **Codex** and **opencode**) already emit this patch format; this implementation aims to be **more forgiving** and will try harder to apply *slightly malformed* patches by using whitespace/normalization fallbacks and an additional “anchor line” fallback.

The CLI exists and is useful for quick manual runs, but the primary intent is to use this package as the **patch-application backend for agent tools and MCP**

---


`apply-patch-py` consumes patch text shaped like:

```
*** Begin Patch
*** Update File: path/to/file.txt
@@
-old line
+new line
*** End Patch
```

Supported operations:

- `*** Add File: <path>`
- `*** Delete File: <path>`
- `*** Update File: <path>` (optionally with `*** Move to: <new_path>`)

---

LLMs sometimes emit patches that are “almost correct” but fail strict application due to:

- whitespace drift
- minor punctuation differences (Unicode quotes/dashes)
- slightly malformed hunks

This library tries strict matching first (OpenAI models will match almost everytime, strictly), then progressively relaxes how it matches context lines:

1. **Exact match**
2. **Right-stripped match** (`rstrip`)
3. **Trimmed match** (`strip`)
4. **Normalized match** (Unicode punctuation normalization + whitespace normalization)

---

### Install

From PyPI:

```bash
uv add apply-patch-py
```

Or run without installing:

```bash
uvx apply-patch-py "*** Begin Patch
*** End Patch"
```

---

### PydanticAI tool Example

You can try it from examples folder:

```bash
# 1) Clone and run the example
git clone https://github.com/marcius-llmus/apply-patch-py
cd apply-patch-py

# Provide your LLM key (example: OpenAI)
export OPENAI_API_KEY="sk-proj-..."

# Run the example with uv
uv run examples/pydantic_example/pydantic_example.py
```

Then you can start asking edits to files inside `example_repo` folder.

For example: 

```
Request> Create a new file named "notes/hello.txt" with the content:
hello from the patch tool
```
or
```
Request> Remove content "xyz" from existing_file.txt 123
```
or
```
Request> Edit file xyz, remove the middle block.
```


---

### Direct usage

You can also call the library directly:

```python
import asyncio

from apply_patch_py import apply_patch

patch = """\
*** Begin Patch
*** Add File: hello.txt
+hello
*** End Patch
"""

async def main() -> None:
    affected = await apply_patch(patch)
    assert affected.success

asyncio.run(main())
```

---

### CLI usage

Apply a patch provided as a command-line argument:

```bash
apply-patch-py "*** Begin Patch
*** Add File: hello.txt
+hello\n
*** End Patch"
```

Apply a patch from stdin:

```bash
cat patch.txt | apply-patch-py
```

The CLI prints a summary of affected files:

```text
Success. Updated the following files:
A hello.txt
M existing.txt
D obsolete.txt
```


### Add a file

```diff
*** Begin Patch
*** Add File: nested/new.txt
+created
*** End Patch
```

### Delete a file

```diff
*** Begin Patch
*** Delete File: obsolete.txt
*** End Patch
```

### Update a file (single hunk)

```diff
*** Begin Patch
*** Update File: modify.txt
@@
-line2
+changed
*** End Patch
```

### Update a file (multiple hunks)

```diff
*** Begin Patch
*** Update File: multi.txt
@@
-line2
+changed2
@@
-line4
+changed4
*** End Patch
```

### Rename/move a file while updating

```diff
*** Begin Patch
*** Update File: old/name.txt
*** Move to: renamed/dir/name.txt
@@
-old content
+new content
*** End Patch
```

---

### Run tests

```bash
uv run pytest
```

### Integration tests (LLM providers)

This repo also contains integration tests that validate patch generation via real LLM providers. They are **skipped by default** unless explicitly selected:

```bash
uv run pytest -m integration
```

See `tests/integration/` for provider configuration.

---

## Notes

- The patch format and workflow are **directly inspired by OpenAI Codex** diff patching.
- Some other tools (e.g. opencode) emit the same format.
- This project is essentially a **port** from their rust patcher with a few changes to improve success rates on imperfect LLM output.

---

## License

MIT. See [LICENSE](LICENSE).
