review

package
v1.26.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyGraphFlags

func ApplyGraphFlags(
	ctx context.Context,
	analytics graphx.Analytics,
	repoKey string,
	symbols []ChangedSymbol,
	beforeCommunity map[string]string,
)

ApplyGraphFlags enriches each changed symbol with Flag/Note based on persistent-graph signals (community membership and structural surprise). Noop when analytics is nil or repoKey is empty.

beforeCommunity maps "symbolName:file" to the community label recorded before the change (e.g. from a pre-merge-base snapshot). When the map is nil or empty the community_move branch can never fire, and only high_surprise flags are emitted.

A pre-existing non-empty Flag on a symbol is never overwritten.

func GitExec

func GitExec(ctx context.Context, dir string, args ...string) (string, error)

GitExec runs a git command in the given directory. Exported for use by tool_review_pr.go.

When dir is a git worktree (its .git entry is a file rather than a directory), GitExec automatically switches to --git-dir / --work-tree invocation style so that git can locate the worktree admin directory. The optional pathRewrite func is applied to the gitdir path extracted from the .git file; pass nil when no path-mapping is needed (e.g. non-containerised callers).

func GitExecRewrite

func GitExecRewrite(ctx context.Context, dir string, pathRewrite func(string) string, args ...string) (string, error)

GitExecRewrite is like GitExec but applies pathRewrite to the gitdir path resolved from a worktree .git file. Use this when git runs inside a container where filesystem paths differ from the host paths embedded in .git files.

Types

type ChangeType

type ChangeType string

ChangeType describes how a symbol was modified.

const (
	ChangeAdded    ChangeType = "added"
	ChangeModified ChangeType = "modified"
	ChangeRemoved  ChangeType = "removed"
)

type ChangedSymbol

type ChangedSymbol struct {
	Symbol     *parser.Symbol
	ChangeType ChangeType
	FileDiff   FileDiff

	// Flag is a non-fatal classification attached by the review pipeline when
	// graph signals indicate something noteworthy (e.g. community_move, high_surprise).
	// Empty when no flag applied.
	Flag string `json:"flag,omitempty"`
	// Note is a human-readable explanation of Flag. Empty when Flag is empty.
	Note string `json:"note,omitempty"`

	// DeadCodeScore is the CE reranker probability [0..1] that this symbol is
	// genuine dead code. Set only for ChangeRemoved symbols when a score is
	// available in code_dead_code_scores (score > 0.25).
	DeadCodeScore float32 `json:"dead_code_score,omitempty"`
	// DeadCodeNote is a human-readable explanation of the dead-code probability.
	DeadCodeNote string `json:"dead_code_note,omitempty"`
}

ChangedSymbol pairs a symbol with its change type.

func ChangedSymbols

func ChangedSymbols(symbols []*parser.Symbol, diffs []FileDiff, repoRoot string) []ChangedSymbol

ChangedSymbols intersects parsed symbols with git diff line ranges. repoRoot is the absolute path to the repo root (symbols have absolute File paths).

type DeltaInput

type DeltaInput struct {
	Root            string // repo root (absolute path)
	Base            string // base ref (default "HEAD~1")
	Head            string // head ref (default "HEAD"; PR reviews pass "FETCH_HEAD")
	Depth           int    // impact traversal depth (default 2)
	Language        string // optional language filter
	IncludeSnippets bool   // include source code snippets around changed symbols
	OxCodes         *oxcodes.Client

	// PathRewrite, when non-nil, is applied to gitdir paths extracted from
	// worktree .git pointer files. Use this when git commands run inside a
	// container where filesystem paths differ from host paths embedded in
	// .git files (e.g. PATH_MAPPINGS=/home/user:/host remaps /home/user→/host).
	PathRewrite func(string) string
}

DeltaInput configures a delta review.

type DeltaResult

type DeltaResult struct {
	ChangedFiles    []FileDiff       `json:"changed_files"`
	ChangedSymbols  []ChangedSymbol  `json:"changed_symbols"`
	ImpactedSymbols []ImpactedSymbol `json:"impacted_symbols"`
	UntestedSymbols []string         `json:"untested_symbols,omitempty"`
	Snippets        []Snippet        `json:"snippets,omitempty"`
	Risk            RiskGuidance     `json:"risk"`
	Tier            string           `json:"tier"`
}

DeltaResult is the output of a delta review.

func DeltaReview

func DeltaReview(ctx context.Context, input DeltaInput) (*DeltaResult, error)

DeltaReview runs the full delta review pipeline.

type FileDiff

type FileDiff struct {
	Path       string      // relative file path
	Added      int         // lines added
	Removed    int         // lines removed
	LineRanges []LineRange // changed line ranges in the new version
}

FileDiff describes changes to a single file.

func ChangedFiles

func ChangedFiles(ctx context.Context, repoRoot, base, head string) ([]FileDiff, error)

ChangedFiles returns files changed between base ref and head. If base is empty, falls back to staged changes (git diff --cached). If head is empty, "HEAD" is used (back-compat for delta reviews of the current checkout). For PR reviews, callers pass "FETCH_HEAD" after a `git fetch origin pull/N/head` so the diff reflects PR contents instead of the warm-clone HEAD (which usually points at main).

func ChangedFilesRewrite

func ChangedFilesRewrite(ctx context.Context, repoRoot string, pathRewrite func(string) string, base, head string) ([]FileDiff, error)

ChangedFilesRewrite is like ChangedFiles but applies pathRewrite when resolving gitdir paths inside worktree .git pointer files. Pass nil when no path-mapping is required.

type ImpactedSymbol

type ImpactedSymbol struct {
	Name       string  `json:"name"`
	File       string  `json:"file"`
	Distance   int     `json:"distance"`
	Confidence float64 `json:"confidence"`
	ChangedBy  string  `json:"changed_by"`
}

ImpactedSymbol is a downstream symbol affected by a change.

type LineRange

type LineRange struct {
	Start int
	End   int
}

LineRange is an inclusive range of line numbers.

type PRWorktree

type PRWorktree struct {
	Path    string // absolute path to the worktree (tree-walking root)
	Cleanup func()
}

PRWorktree creates an isolated git worktree at FETCH_HEAD so the call graph and any tree-walking analysis sees the PR code, not the warm clone's checked-out ref.

Why isolated: BuildFromRepo and friends parse the working tree on disk. `git fetch origin pull/N/head` only updates FETCH_HEAD ref — the working tree stays on whatever the original clone checked out (usually main). Without a worktree, impact analysis on PR-only symbols (functions added by the PR) silently misses callers because the parser never sees them.

Cleanup: returned cleanup func runs `git worktree remove --force` and rms the temp dir. Always defer-call it, even on error paths from the caller — the temp dir leaks otherwise.

func CreatePRWorktree

func CreatePRWorktree(ctx context.Context, repoRoot, ref string) (*PRWorktree, error)

CreatePRWorktree adds a worktree at the given ref under a temp directory. repoRoot is the original clone (where FETCH_HEAD is set). ref is the reference to check out into the new worktree (typically "FETCH_HEAD").

On error, returns a fully-formed cleanup that's safe to call.

type RiskGuidance

type RiskGuidance struct {
	RiskLevel   string   `json:"risk_level"`
	RiskScore   float64  `json:"risk_score"`
	Flags       []string `json:"flags"`
	Suggestions []string `json:"suggestions"`
}

func GenerateRiskGuidance

func GenerateRiskGuidance(input RiskInput) RiskGuidance

type RiskInput

type RiskInput struct {
	ChangedSymbols  []ChangedSymbol
	ImpactResults   map[string]*impact.Result
	UntestedSymbols []string
	HasInheritance  bool
}

type Snippet

type Snippet struct {
	File      string `json:"file"`
	Symbol    string `json:"symbol"`
	StartLine int    `json:"start_line"`
	EndLine   int    `json:"end_line"`
	Code      string `json:"code"`
}

Snippet is a source code excerpt around a changed symbol.

func ExtractSnippets

func ExtractSnippets(changed []ChangedSymbol, repoRoot string) []Snippet

ExtractSnippets reads source files and extracts code context around changed symbols. repoRoot is the absolute path to the repo root.

Jump to

Keyboard shortcuts

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