edit

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package edit implements the safe edit transaction used by the agent: read → verify the anchor is unique → patch → re-read → show the diff. Anchored search/replace, no second model. Bash-driven edits are caught separately by the runtime via `git diff`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(ctx context.Context, root, path string) string

Format runs the canonical formatter for path's language, by extension, IN PLACE — if that tool is on PATH. Best-effort: a missing tool or a formatter error (e.g. the edit left a syntax error) is silently ignored; formatting is a courtesy, never a gate. Returns the tool name when it ran, "" otherwise (for display).

func Hash

func Hash(root, path string) (string, bool)

Hash returns the current content hash of a file (for staleness checks).

func HashBytes

func HashBytes(data []byte) string

HashBytes hashes already-read content the SAME way as Hash, so a hash recorded at read time compares directly against Hash() at write time (the stale-read guard).

Types

type Result

type Result struct {
	Path        string `json:"path"`
	Applied     bool   `json:"applied"`
	Occurrences int    `json:"occurrences"`
	Created     bool   `json:"created"`
	Diff        string `json:"diff"`
}

Result reports the outcome of an edit.

func Apply

func Apply(ctx context.Context, root, path, oldString, newString string, replaceAll bool) (Result, error)

Apply performs an anchored search/replace on path (relative to root).

  • oldString == "" creates a new file with newString (errors if it exists).
  • otherwise oldString must occur exactly once, unless replaceAll is set.

The file is re-read after writing to confirm the change landed, and a git diff is captured for the agent to inspect.

Jump to

Keyboard shortcuts

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