Patch many

Patch many#

The patch_many tool applies several patch-style changes in one call and commits them atomically: if any hunk fails validation, no file is written. It is meant for cross-cutting edits (“rename this symbol”, “add a parameter and update its callers”) that would otherwise take one patch call per file.

Patches are validated in-memory before anything lands, so a failed hunk validation leaves files unchanged. If a file write fails, rollback is best-effort and a partial refactor may remain on disk.

Gives the LLM agent the ability to patch multiple files atomically in one tool call.

Intended for cross-cutting changes (“rename this class”, “add a param and update callers”) where the model would otherwise issue N separate patch calls, one per file.

Instructions

Apply patches to multiple files atomically.
Patches are validated in-memory: if ANY fails, NO files are written.

Simple (one hunk per file), paths in the fence header:
  ```patch_many path1.py
  <<<<<<< ORIGINAL
  old content
  =======
  new content
  >>>>>>> UPDATED
  ```

Multi-hunk (any number of hunks per file), using `=== PATH: ... ===` headers:
  ```patch_many
  === PATH: path1.py ===
  <<<<<<< ORIGINAL
  first hunk original
  =======
  first hunk updated
  >>>>>>> UPDATED
  <<<<<<< ORIGINAL
  second hunk original
  =======
  second hunk updated
  >>>>>>> UPDATED
  ```

Tool-call: `patches` is a JSON array of {"path": "...", "patch": "..."} entries.
Each patch string may hold multiple ORIGINAL/UPDATED blocks, so one entry lands
several hunks.

Repeated paths apply in order, each hunk seeing the previous result. Prefer one
entry per path with multiple blocks; repeat a path only when a later hunk depends
on an earlier one landing.
gptme.tools.patch_many.execute_patch_many(code: str | None, args: list[str] | None, kwargs: dict[str, str] | None) → Generator[Message, None, None]

Execute patch_many from a markdown block or tool/function-call kwargs.

gptme.tools.patch_many.execute_patch_many_impl(patches: Sequence[tuple[Path, str | Patch]]) → Generator[Message, None, None]

Resolve all patches in-memory before writing any files.

gptme.tools.patch_many.get_patch_many_targets(tool_use: ToolUse, *, resolve_symlinks: bool = True) → list[tuple[Path, str]] | None

Public structured input of a patch_many call, for policy hooks.

Returns the resolved (path, patch) pairs the call would write, in order, or None when tool_use is not a patch_many call. An unusable call (malformed, or a relative path escaping the working directory) returns []: the tool rejects it before writing anything.

Set resolve_symlinks=False to inspect requested names as well as resolved destinations (e.g. a protected .env alias to an ordinary filename). Traversal validation still applies in both modes.

Intended for TOOL_EXECUTE_PRE hooks, e.g.:

from gptme.tools.patch_many import get_patch_many_targets

def deny_ssh(data):
    for path, _ in get_patch_many_targets(data.tool_use) or []:
        if ".ssh" in path.parts:
            data.deny("patch_many may not touch ~/.ssh")