diff

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package diff is the Stage-1 namespace for diff sandbox, staging, preview, summariser, test selector, and 3-way merge. See ../REFACTOR_PLAN.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplySearchReplace

func ApplySearchReplace(content string, blocks []SearchReplaceBlock) (string, error)

ApplySearchReplace applies a sequence of SearchReplaceBlocks to content in order.

Rules:

  • Each block's Search string must appear exactly once in the current content (after all prior substitutions have been applied). If it is not found at all, ApplySearchReplace returns an error. If it appears more than once, the replacement is ambiguous and an error is returned.
  • An empty Replace string is valid and causes the matched text to be deleted.
  • Blocks are applied left-to-right; each successive block operates on the result of the previous one.

func BuildDependencyGraph

func BuildDependencyGraph(projectDir string) map[string][]string

BuildDependencyGraph scans the project directory and builds a reverse import graph mapping packages to their dependents.

func EstimateTimeSaved

func EstimateTimeSaved(totalTests, selectedTests int) string

EstimateTimeSaved returns a human-readable string estimating time savings.

func FormatConflictMarkers

func FormatConflictMarkers(conflict Diff3Conflict) string

FormatConflictMarkers formats a conflict with standard diff3 markers.

func FormatDiff3Result

func FormatDiff3Result(result *Diff3Result) string

FormatDiff3Result produces a human-readable summary of a Diff3Result.

func FormatSelection

func FormatSelection(selected *SelectedTests, changedFiles []string, language string, totalTests int) string

FormatSelection produces a human-readable summary of the test selection.

func GenerateTestCommand

func GenerateTestCommand(selected *SelectedTests, language string) string

GenerateTestCommand produces a runnable test command for the selected tests.

func LCS

func LCS(a, b []string) []string

LCS computes the Longest Common Subsequence of two string slices.

func MergeClean

func MergeClean(base, ours, theirs string) (string, bool)

MergeClean performs a three-way merge and returns the merged string and whether the merge was clean (no conflicts).

func RenderUnified

func RenderUnified(change *FileChange) string

RenderUnified renders a single FileChange in standard unified diff format.

Types

type ChangeStats

type ChangeStats struct {
	Additions int
	Deletions int
	NetChange int
}

ChangeStats summarizes additions and deletions for a file change.

type Diff3Conflict

type Diff3Conflict struct {
	BaseLines   []string
	OursLines   []string
	TheirsLines []string
	StartLine   int
}

Diff3Conflict represents a single conflict region in the merge.

type Diff3Region

type Diff3Region struct {
	Type        string // "unchanged", "ours", "theirs", "conflict"
	BaseStart   int
	BaseEnd     int
	OursLines   []string
	TheirsLines []string
}

Diff3Region represents a classified region in the three-way diff.

type Diff3Result

type Diff3Result struct {
	Merged       string
	Conflicts    []Diff3Conflict
	HasConflicts bool
	Stats        Diff3Stats
}

Diff3Result holds the outcome of a three-way merge.

func Merge3

func Merge3(base, ours, theirs string) *Diff3Result

Merge3 performs a three-way merge using the diff3 algorithm. It splits base, ours, and theirs into lines, computes diff regions, and merges non-conflicting changes automatically while marking conflicts.

type Diff3Stats

type Diff3Stats struct {
	TotalLines    int
	ConflictCount int
	AutoMerged    int
	OursOnly      int
	TheirsOnly    int
}

Diff3Stats provides statistics about the merge operation.

type DiffHunk

type DiffHunk struct {
	OldStart int
	OldCount int
	NewStart int
	NewCount int
	Lines    []DiffLine
}

DiffHunk represents a contiguous block of changes with surrounding context.

func ComputeDiff

func ComputeDiff(oldContent, newContent string) []DiffHunk

ComputeDiff computes hunks between old and new content using Myers diff with 3 lines of context.

type DiffLine

type DiffLine struct {
	Type      string // "add", "remove", "context"
	Content   string
	OldLineNo int
	NewLineNo int
}

DiffLine represents a single line in a diff hunk.

func ComputeMyersDiff

func ComputeMyersDiff(a, b []string) []DiffLine

ComputeMyersDiff implements Myers' O(ND) diff algorithm returning an edit script. It finds a minimal edit distance between sequences a and b, then returns the result as a series of DiffLine entries (context, add, remove).

type DiffPreview

type DiffPreview struct {
	Changes   []FileChange
	CreatedAt time.Time
	SessionID string
	// contains filtered or unexported fields
}

DiffPreview holds all pending changes for review before they are committed/applied.

func NewDiffPreview

func NewDiffPreview() *DiffPreview

NewDiffPreview creates a new DiffPreview with a generated session ID.

func (*DiffPreview) Approve

func (dp *DiffPreview) Approve(path string)

Approve marks a specific file change as approved.

func (*DiffPreview) ApproveAll

func (dp *DiffPreview) ApproveAll()

ApproveAll marks all changes as approved.

func (*DiffPreview) Clear

func (dp *DiffPreview) Clear()

Clear removes all recorded changes.

func (*DiffPreview) GetApproved

func (dp *DiffPreview) GetApproved() []FileChange

GetApproved returns changes that have been approved.

func (*DiffPreview) GetPending

func (dp *DiffPreview) GetPending() []FileChange

GetPending returns changes that are neither approved nor rejected.

func (*DiffPreview) RecordChange

func (dp *DiffPreview) RecordChange(path, oldContent, newContent string)

RecordChange computes a unified diff between old and new content and records it.

func (*DiffPreview) Reject

func (dp *DiffPreview) Reject(path string, comment string)

Reject marks a specific file change as rejected with an optional comment.

func (*DiffPreview) RejectAll

func (dp *DiffPreview) RejectAll(comment string)

RejectAll marks all changes as rejected with an optional comment.

func (*DiffPreview) RenderAll

func (dp *DiffPreview) RenderAll() string

RenderAll renders all changes as a combined unified diff.

func (*DiffPreview) RenderSummary

func (dp *DiffPreview) RenderSummary() string

RenderSummary produces a compact summary of all pending changes.

type DiffSandbox

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

DiffSandbox holds pending file changes so the user can review diffs before applying.

func NewDiffSandbox

func NewDiffSandbox() *DiffSandbox

NewDiffSandbox creates a new, enabled DiffSandbox.

func (*DiffSandbox) Apply

func (ds *DiffSandbox) Apply(path string) error

Apply writes one pending change to disk and removes it from the sandbox.

func (*DiffSandbox) ApplyAll

func (ds *DiffSandbox) ApplyAll() (int, error)

ApplyAll writes all pending changes to disk and clears the sandbox.

func (*DiffSandbox) DiffAll

func (ds *DiffSandbox) DiffAll() string

DiffAll returns all diffs combined.

func (*DiffSandbox) DiffFor

func (ds *DiffSandbox) DiffFor(path string) string

DiffFor returns the unified diff for a single pending file.

func (*DiffSandbox) Disable

func (ds *DiffSandbox) Disable()

Disable deactivates the sandbox.

func (*DiffSandbox) Enable

func (ds *DiffSandbox) Enable()

Enable activates the sandbox.

func (*DiffSandbox) Format

func (ds *DiffSandbox) Format() string

Format returns a human-readable summary of all pending changes.

func (*DiffSandbox) Get

func (ds *DiffSandbox) Get(path string) *PendingChange

Get returns the pending change for a specific path, or nil.

func (*DiffSandbox) IsEnabled

func (ds *DiffSandbox) IsEnabled() bool

IsEnabled returns whether the sandbox is active.

func (*DiffSandbox) List

func (ds *DiffSandbox) List() []*PendingChange

List returns all pending changes sorted by path.

func (*DiffSandbox) Reject

func (ds *DiffSandbox) Reject(path string)

Reject discards the pending change for one file.

func (*DiffSandbox) RejectAll

func (ds *DiffSandbox) RejectAll()

RejectAll discards all pending changes.

func (*DiffSandbox) Stage

func (ds *DiffSandbox) Stage(path, action, oldContent, newContent string)

Stage records a pending file change and computes a unified diff.

type DiffSummarizer

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

DiffSummarizer parses unified diffs and produces human-readable summaries.

func NewDiffSummarizer

func NewDiffSummarizer() *DiffSummarizer

NewDiffSummarizer creates a new DiffSummarizer instance.

func (*DiffSummarizer) AssessImpact

func (ds *DiffSummarizer) AssessImpact(summary *DiffSummary) string

AssessImpact determines the impact level of the changes.

func (*DiffSummarizer) DetectChangeType

func (ds *DiffSummarizer) DetectChangeType(summary *DiffSummary) string

DetectChangeType classifies the overall change type based on file patterns and content.

func (*DiffSummarizer) FormatSummary

func (ds *DiffSummarizer) FormatSummary(summary *DiffSummary) string

FormatSummary produces a compact terminal-friendly summary display.

func (*DiffSummarizer) GenerateCommitMessage

func (ds *DiffSummarizer) GenerateCommitMessage(summary *DiffSummary) string

GenerateCommitMessage produces a conventional commit message from the summary.

func (*DiffSummarizer) GeneratePRSummary

func (ds *DiffSummarizer) GeneratePRSummary(summary *DiffSummary) string

GeneratePRSummary produces a multi-paragraph PR description from the summary.

func (*DiffSummarizer) Summarize

func (ds *DiffSummarizer) Summarize(diff string) *DiffSummary

Summarize parses a unified diff and returns a complete DiffSummary.

func (*DiffSummarizer) SummarizeFile

func (ds *DiffSummarizer) SummarizeFile(path string, hunks []string) *FileSummary

SummarizeFile analyzes hunks for a single file and produces a FileSummary.

type DiffSummary

type DiffSummary struct {
	Files          []FileSummary
	OverallSummary string
	ChangeType     string // "feature", "bugfix", "refactor", "test", "docs", "config"
	Impact         string // "low", "medium", "high"
	AffectedAreas  []string
}

DiffSummary holds the full summarized output of a diff analysis.

type Edit

type Edit struct {
	Type  string // "keep", "insert", "delete"
	Line  string
	Index int
}

Edit represents a single edit operation in an edit script.

func EditScript

func EditScript(from, to []string) []Edit

EditScript computes a minimal edit script to transform 'from' into 'to'.

type FileChange

type FileChange struct {
	Path       string
	Type       string // "modified", "created", "deleted", "renamed"
	OldContent string
	NewContent string
	Hunks      []DiffHunk
	Stats      ChangeStats
	Approved   bool
	Rejected   bool
	Comment    string
}

FileChange represents a single file modification in the workspace.

type FileSummary

type FileSummary struct {
	Path       string
	Action     string // "added", "modified", "deleted"
	Summary    string
	Additions  int
	Deletions  int
	KeyChanges []string
}

FileSummary describes summarized changes for a single file in the diff.

type PendingChange

type PendingChange struct {
	Path       string
	Action     string // "create", "edit", "overwrite"
	OldContent string
	NewContent string
	Diff       string
	CreatedAt  time.Time
}

PendingChange represents a staged file modification that has not yet been applied to disk.

type Preview

type Preview = DiffPreview

type SearchReplaceBlock

type SearchReplaceBlock struct {
	Search  string
	Replace string
}

SearchReplaceBlock is a single SEARCH/REPLACE pair extracted from LLM output.

The on-wire format used by LLMs (e.g. Kimi-Dev, aider) is:

<<<<<<< SEARCH
old content to find
=======
new content to replace with
>>>>>>> REPLACE

func ParseSearchReplace

func ParseSearchReplace(text string) []SearchReplaceBlock

ParseSearchReplace extracts all SEARCH/REPLACE blocks from LLM output text. Text outside blocks (prose, code fences, explanations) is silently ignored. Incomplete or malformed blocks are skipped.

type SelectedTests

type SelectedTests struct {
	Tests    []string
	Packages []string
	Reason   map[string]string
	Coverage float64
}

SelectedTests holds the result of a diff-aware test selection.

type StagedChange

type StagedChange struct {
	File        string
	Original    string
	Modified    string
	Hunks       []StagedHunk
	Status      string // "staged", "applied", "rejected"
	StagedAt    time.Time
	Description string
}

StagedChange represents a file modification held in the staging area.

type StagedHunk

type StagedHunk struct {
	StartLine int
	EndLine   int
	OldLines  []string
	NewLines  []string
	Approved  bool
}

StagedHunk represents a contiguous block of changes within a staged file.

type StagingArea

type StagingArea struct {
	Staged map[string]*StagedChange
	// contains filtered or unexported fields
}

StagingArea provides a local staging area for agent edits, allowing review before changes are applied to disk — like a git staging area for the agent's modifications.

func NewStagingArea

func NewStagingArea() *StagingArea

NewStagingArea creates a new empty StagingArea.

func (*StagingArea) ApplyAll

func (sa *StagingArea) ApplyAll() ([]string, error)

ApplyAll writes all staged changes to disk and returns the list of files modified.

func (*StagingArea) ApplyFile

func (sa *StagingArea) ApplyFile(file string) error

ApplyFile applies a single file's staged changes to disk.

func (*StagingArea) ApproveHunk

func (sa *StagingArea) ApproveHunk(file string, hunkIndex int)

ApproveHunk marks a specific hunk as approved within a staged file.

func (*StagingArea) Clear

func (sa *StagingArea) Clear()

Clear discards all staged changes.

func (*StagingArea) FormatStaging

func (sa *StagingArea) FormatStaging() string

FormatStaging returns a human-readable summary of the staging area.

func (*StagingArea) GetDiff

func (sa *StagingArea) GetDiff(file string) string

GetDiff returns a unified diff for a staged file.

func (*StagingArea) GetStaged

func (sa *StagingArea) GetStaged() map[string]*StagedChange

GetStaged returns a copy of all staged changes.

func (*StagingArea) HasPending

func (sa *StagingArea) HasPending() bool

HasPending returns true if there are any changes in "staged" status.

func (*StagingArea) Reject

func (sa *StagingArea) Reject(file string)

Reject removes a file from staging without applying its changes.

func (*StagingArea) RejectHunk

func (sa *StagingArea) RejectHunk(file string, hunkIndex int)

RejectHunk rejects a specific hunk within a staged file (partial staging).

func (*StagingArea) Stage

func (sa *StagingArea) Stage(file, original, modified, description string)

Stage computes a diff between original and modified content and adds it to the staging area.

type Summarizer

type Summarizer = DiffSummarizer

type Summary

type Summary = DiffSummary

type TestSelector

type TestSelector struct {
	ProjectDir string
	Language   string
	DepGraph   map[string][]string
	// contains filtered or unexported fields
}

TestSelector provides diff-aware test selection, identifying which tests need to run based on changed files and dependency relationships.

func NewTestSelector

func NewTestSelector(projectDir string) *TestSelector

NewTestSelector creates a TestSelector for the given project directory. It auto-detects the project language and builds a dependency graph.

func (*TestSelector) FindRelatedTests

func (ts *TestSelector) FindRelatedTests(file string) []string

FindRelatedTests finds test files related to the given source file.

func (*TestSelector) SelectFromDiff

func (ts *TestSelector) SelectFromDiff(diff string) (*SelectedTests, error)

SelectFromDiff parses a unified diff string, extracts changed file paths, and selects the related tests.

func (*TestSelector) SelectFromFiles

func (ts *TestSelector) SelectFromFiles(changedFiles []string) (*SelectedTests, error)

SelectFromFiles determines which tests to run given a list of changed files. Strategy depends on the detected language.

Jump to

Keyboard shortcuts

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