apply_patch

package
v0.0.0-...-187d9d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package apply_patch implements the unified-diff mutation tool.

Single primitive: takes a unified diff, applies it to the working tree. Creates new files, modifies existing, deletes (when patch wipes content or marks file deleted). Fails loud on context mismatch — no fuzzy retry.

Package apply_patch provides patch application primitives plus the LINE#ID anchor system used to reference specific source lines.

LINE#ID format: "<lineNumber>#<3-char tag>", e.g. "42#VKM". The tag is a deterministic content hash of the (normalized) line. Edits that cite a tag which no longer matches the live content are rejected, which catches stale or hallucinated line numbers before they corrupt files.

Algorithm:

  1. normalize: strip \r, trim trailing space/tab.
  2. seed: 0 if the line contains any Unicode letter or digit, else the 1-based line number (so blanks and pure-punctuation lines still vary by position).
  3. h = xxhash32(normalized, seed); idx = h % 4096.
  4. tag = alphabet[idx/256] + alphabet[(idx/16)%16] + alphabet[idx%16] using the 16-char set "ZPMQVRWSNKTXJBYH". 4096 buckets cuts the birthday-collision probability ~16x vs the prior 256-bucket space.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() tools.Tool

New returns a fresh apply_patch tool.

func NewWithFilter

func NewWithFilter(pathRe *regexp.Regexp) tools.Tool

NewWithFilter constructs the apply_patch tool with an optional path regex. When pathRe is nil, all paths are allowed (existing behavior). When pathRe is non-nil, the whole batch is rejected if ANY file path in the patch fails the match — no file is touched on rejection. Paths are matched relative to the current working directory when possible.

func Ref

func Ref(line string, lineNumber int) string

Ref renders the standard "<lineNumber>#<tag>" anchor format.

func Tag

func Tag(line string, lineNumber int) string

Tag returns the 3-character content-hash tag for one source line. lineNumber is 1-based and used as the xxh seed when the line has no alphanumerics (so blank lines and pure-punctuation lines still vary).

func TagAll

func TagAll(src string) []string

TagAll returns Tag for every line in src (split on \n). Index i+1 is the 1-based line number passed to Tag.

Types

type Tool

type Tool struct {
	// contains filtered or unexported fields
}

Tool is the apply_patch tool.

func (*Tool) Run

func (t *Tool) Run(ctx context.Context, input map[string]any) (tools.Result, error)

Run parses the patch and applies each file.

func (*Tool) Spec

func (t *Tool) Spec() llm.ToolSpec

Spec advertises the tool to the model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL