patch

package
v1.5.153 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package patch implements the apply_patch DSL: a stripped-down, file-oriented diff format carried by one tool call, so a single invocation can add, update, move, and delete files. The grammar is codex's apply_patch:

*** Begin Patch
*** Add File: path            + one "+" line per content line
*** Delete File: path
*** Update File: path         optionally followed by "*** Move to: path"
@@ [context]                  then " ", "-", "+" lines; "*** End of File"
*** End Patch                 anchors a chunk to the end of the file

This package parses and resolves a patch into the changes it would make; it never writes to disk. The caller decides how to persist the result, which keeps workspace-path policy, snapshots, and prompts in the tool layer where the rest of the agent already handles them. It imports only the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Paths

func Paths(input string) []string

Paths returns every path the patch touches, in operation order, including a move's destination. It is the cheap extraction the security layers need (static denial, prompt summaries, compaction bookkeeping) before anything is resolved or written; an unparseable patch yields nil.

Types

type ApplyError

type ApplyError struct {
	Msg string
}

ApplyError reports a patch that parsed but cannot be applied; the message is written for the model (it names the operation and path), mirroring codex's error vocabulary so existing model habits hold.

func (*ApplyError) Error

func (e *ApplyError) Error() string

type Change

type Change struct {
	Kind ChangeKind
	// Path is the resolved destination path (for Delete and Update, the file
	// itself; for Move, the new location).
	Path string
	// OldPath is the resolved source path for a Move ("" otherwise).
	OldPath string
	// OldContent is the file content before the change ("" for Add).
	OldContent string
	// NewContent is the content after the change ("" for Delete).
	NewContent string
	// Note, when non-empty, says how a hunk was located when it was not a
	// literal match (today: how many context lines were dropped). It is shown
	// with the summary so a reduced match is never silent.
	Note string
}

Change is one resolved, in-memory file change. Nothing has been written when Compute returns; the caller persists each change (and can snapshot it first).

func Compute

func Compute(ops []Op, resolve Resolver) ([]Change, error)

Compute resolves a parsed patch against the working tree and returns the changes it would make, in operation order, without touching the filesystem. It has no byte budget; callers that accept model-generated patches should use ComputeLimited so one oversized call cannot retain an unbounded working set.

func ComputeLimited added in v1.5.9

func ComputeLimited(ops []Op, resolve Resolver, maxBytes int64) ([]Change, error)

ComputeLimited is Compute with a cap on the file content it retains while resolving the patch. The cap covers content read from disk plus content synthesized by updates/moves; a patch that would exceed it fails before any write, naming the budget instead of exhausting memory first. maxBytes <= 0 keeps Compute's unbounded behavior (used by direct library callers/tests).

type ChangeKind

type ChangeKind int

ChangeKind is what an operation does to one file.

const (
	// ChangeAdd creates a file.
	ChangeAdd ChangeKind = iota + 1
	// ChangeUpdate rewrites a file in place.
	ChangeUpdate
	// ChangeDelete removes a file.
	ChangeDelete
	// ChangeMove rewrites a file and renames it.
	ChangeMove
)

type Chunk

type Chunk struct {
	// Anchors are the literal lines named after one or more "@@" markers, in
	// order. They are hints with codex's meaning — the function or section the
	// hunk belongs to — and the matcher seeks each one forward before looking
	// for the hunk's own lines. A second "@@" is the further enclosing scope,
	// exactly as codex's instructions describe it.
	Anchors []string
	// Header is the first non-empty anchor, kept for error messages.
	Header string
	// Lines are the chunk's context, add, and delete lines in file order.
	Lines []Line
	// EOF anchors the search at the end of the file first (the
	// "*** End of File" marker).
	EOF bool
}

Chunk is one hunk of an update: the context/removal lines locate the region (fuzzy-matched; see seek.go), and the additions replace it.

type Failure added in v1.3.8

type Failure struct {
	Path string
	// Hunk is the 1-based hunk number inside an Update; 0 for a file operation.
	Hunk int
	Err  error
}

Failure is one part of a patch that could not be applied. Compute collects every failure in one pass, so a patch with two broken hunks costs one round trip instead of two.

type Line

type Line struct {
	Kind LineKind
	Text string
}

Line is one chunk line.

type LineKind

type LineKind int

LineKind is one line's role inside a chunk.

const (
	// LineContext is unchanged text used to locate the hunk (" " prefix).
	LineContext LineKind = iota
	// LineAdd is inserted text ("+" prefix).
	LineAdd
	// LineDelete is removed text ("-" prefix).
	LineDelete
	// LineUnknown is a line that arrived without a prefix. It only survives
	// until repairUnknownLines decides what it must have been; a patch never
	// applies one as-is.
	LineUnknown
)

type Op

type Op struct {
	Kind OpKind
	// Path is the target path exactly as written in the patch (the caller
	// resolves it against its workspace roots).
	Path string
	// MoveTo, for an update, is the rename target ("" = no rename). It is
	// resolved like Path.
	MoveTo string
	// Body holds the added lines for an Add (without the "+" prefix; each
	// entry is one line, and the created file ends with a newline).
	Body []string
	// Chunks holds the hunks for an Update.
	Chunks []Chunk
	// Notes records repairs the parser made (a hunk line that arrived without a
	// prefix and was unambiguous), so the tool result can say what was assumed.
	Notes []string
}

Op is one file operation in a patch.

func Parse

func Parse(input string) ([]Op, error)

Parse reads a full patch envelope into its operations. It requires the "*** Begin Patch" / "*** End Patch" markers and at least one file operation, so a truncated or empty tool call fails loudly instead of silently doing nothing.

type OpKind

type OpKind int

OpKind is one file operation's kind.

const (
	// OpAdd creates a new file (an existing path is an error).
	OpAdd OpKind = iota + 1
	// OpDelete removes an existing file.
	OpDelete
	// OpUpdate patches an existing file, optionally moving it (MoveTo).
	OpUpdate
)

type ParseError

type ParseError struct {
	Line int
	Msg  string
}

ParseError is a malformed patch; it carries the 1-based input line so the model can see exactly which line broke the grammar.

func (*ParseError) Error

func (e *ParseError) Error() string

type Resolver

type Resolver func(path string) (string, error)

Resolver maps a patch path (exactly as written in the patch) to the absolute path it targets. It is the caller's workspace-boundary policy: the engine never interprets paths itself, so root confinement stays in one place.

Jump to

Keyboard shortcuts

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