run

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 22 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 run owns the on-disk layout of review runs: paths, references, atomic writes and locking.

Index

Constants

View Source
const CommentsFile = "comments.json"

CommentsFile holds the feedback capture read from other reviewers. See publish.Comments.

View Source
const KindLocal = "local"
View Source
const ModelPattern = `^[a-z0-9][a-z0-9._/:-]*$`

ModelPattern admits ids like anthropic/claude-sonnet-5 and, like SourcePattern, keeps > out because loupe-meta carries the value unescaped inside an HTML comment. ValidateModel refuses -- for the same reason.

View Source
const OutlineFile = "outline.json"

OutlineFile holds the headings of each Markdown file the diff changes, read at the head, so add resolves a heading anchor from the run alone.

View Source
const OutlineSchema = 1

Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).

View Source
const PreviousFile = "previous.json"

PreviousFile holds the round capture read back from GitHub. See publish.Previous.

View Source
const SourcePattern = `^[a-z0-9][a-z0-9._-]*(@[0-9][0-9A-Za-z.+-]*)?$`

SourcePattern keeps a source to one token without > because loupe-meta carries it unescaped inside an HTML comment. ValidateSource also refuses --, which RE2 cannot express, for the same reason. LoadTarget applies it to stored runs, so tightening it would refuse runs captured under the looser rule.

View Source
const TargetSchema = 3

Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).

Variables

This section is empty.

Functions

func CreateRun

func CreateRun(dir string, target Target, diff, draftJSON []byte, optional map[string][]byte) (err error)

CreateRun takes the draft and the optional files, such as PreviousFile and CommentsFile, as bytes because run must not import draft or publish. The run appears complete or not at all: files are written to a sibling temp directory that is renamed into place.

func DataRoot

func DataRoot(getenv func(string) string) (string, error)

func DiffSHA256

func DiffSHA256(diff []byte) string

DiffSHA256 is the fingerprint target.json records for pr.diff.

func EncodeOutline

func EncodeOutline(files []OutlineEntry) ([]byte, error)

EncodeOutline returns the file capture stores; run takes bytes in CreateRun.

func HasReceipt

func HasReceipt(dir string) (bool, error)

func HasReturn

func HasReturn(dir string) (bool, error)

HasReturn reports whether the run directory contains a return.json, which seals the run.

func LoadDiff

func LoadDiff(dir string, target Target) (*diff.Diff, error)

LoadDiff parses the diff ReadDiff verified.

func LoadOutline

func LoadOutline(dir string) (diff.Outline, error)

LoadOutline returns nil for a run captured before heading anchors, and refuses a damaged or newer file as ReadJSON does.

func LocalDir

func LocalDir(root string, n int) string

func LocalRoot

func LocalRoot(root string) string

func Newest

func Newest(root, owner, repo string, number int) (int, error)

Newest returns 0 when the pull request has no rounds.

func ReadDiff

func ReadDiff(dir string, target Target) ([]byte, error)

ReadDiff returns pr.diff only when it still matches the fingerprint capture recorded, so a truncated or edited diff cannot silently change which locations are valid, what the review shows, or what a pipeline hands its reviewer.

func ReadJSON

func ReadJSON(path string, v any, schema int) error

ReadJSON reads a run file whose schema is from 1 up to schema. It refuses a damaged record rather than repairing it, so the human can inspect what went wrong, and refuses a newer one as a loupe too old to read it. It decodes every accepted schema into the current struct, so the first bump that removes or redefines a field MUST add a migration step here (docs/versioning.md).

func RecordRefusal

func RecordRefusal(path string, cause error) error

RecordRefusal refuses a damaged run file, naming it and a command to inspect it; loupe never repairs one.

func Rounds

func Rounds(root, owner, repo string, number int) ([]int, error)

Rounds ignores non-numeric entries such as the temp directories CreateRun leaves while it works.

func RunDir

func RunDir(root, owner, repo string, number, round int) string

func RunsDir

func RunsDir(root string) string

func SessionOpen

func SessionOpen(dir string) (bool, error)

SessionOpen reports whether any review of the run is running: an exclusive flock fails only while one holds it.

func ValidateModel

func ValidateModel(model string) error

ValidateModel accepts the empty model, which means none.

func ValidateSource

func ValidateSource(source string) error

ValidateSource accepts the empty source, which means none.

func WriteFileAtomic

func WriteFileAtomic(path string, data []byte) (err error)

WriteFileAtomic leaves either the old or the new content at path, never a partial file, even across a crash.

func WriteJSONAtomic

func WriteJSONAtomic(path string, v any) error

Types

type Entry

type Entry struct {
	Ref    Ref
	Dir    string
	Target Target
}

func Walk

func Walk(root string) ([]Entry, error)

Walk returns every run ordered by owner, repo, number and round. A damaged target.json fails the walk rather than hiding the run.

type Held

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

func Lock

func Lock(dir, command string, getenv func(string) string) (*Held, error)

Lock takes the run's exclusive flock. The kernel drops a flock when its holder dies, so a timeout means a live contender or a wedged process, never a leftover from a crash.

func LockNotifying

func LockNotifying(dir, command string, getenv func(string) string, busy func()) (*Held, error)

LockNotifying is Lock that calls busy, when set, each time it finds the lock held, so a test can order a race on the lock instead of sleeping and hoping the other process got there first.

func (*Held) Unlock

func (h *Held) Unlock() error

type LocalEntry

type LocalEntry struct {
	N   int
	Dir string
}

LocalEntry is returned by WalkLocal for each numeric local run directory.

func WalkLocal

func WalkLocal(root string) ([]LocalEntry, error)

WalkLocal returns every local run in numeric order, skipping non-numeric and non-directory entries.

type LocalFile

type LocalFile struct {
	Path string `json:"path"`
	Abs  string `json:"abs"`
	New  bool   `json:"new,omitempty"`
}

type LocalSkipped

type LocalSkipped struct {
	Path   string `json:"path"`
	Reason string `json:"reason"`
}

type LocalTarget

type LocalTarget struct {
	N          int            `json:"n"`
	Prefix     string         `json:"prefix"`
	Repository string         `json:"repository,omitempty"`
	Capture    string         `json:"capture"`
	Root       string         `json:"root"`
	Branch     string         `json:"branch,omitempty"`
	BaseRef    string         `json:"baseRef,omitempty"`
	MergeBase  string         `json:"mergeBase,omitempty"`
	Files      []LocalFile    `json:"files"`
	Skipped    []LocalSkipped `json:"skipped"`
}

type LocalTargetFile

type LocalTargetFile struct {
	Schema     int         `json:"schema"`
	Kind       string      `json:"kind"`
	Title      string      `json:"title,omitempty"`
	Applier    string      `json:"applier,omitempty"`
	Source     string      `json:"source,omitempty"`
	Model      string      `json:"model,omitempty"`
	CapturedAt time.Time   `json:"capturedAt"`
	DiffSHA256 string      `json:"diffSha256"`
	Local      LocalTarget `json:"local"`
}

LocalTargetFile is the on-disk format for local-run target.json. Local targets marshal through this struct so that PR-only fields (owner, repo, round, etc.) are absent from the file.

type Outline

type Outline struct {
	Schema int `json:"schema"`
	// Files lists every changed Markdown file that is neither deleted nor binary, in diff order, including one with no
	// headings, so a missing path is damage rather than a file without headings.
	Files []OutlineEntry `json:"files"`
}

Outline is written once by capture and never changed.

type OutlineEntry

type OutlineEntry struct {
	Path     string             `json:"path"`
	Headings []markdown.Heading `json:"headings"`
}

type Ref

type Ref struct {
	Owner  string
	Repo   string
	Number int
	Round  int
	// Local is the local run number (1-based); non-zero for local refs.
	Local int
	// Prefix is the display-only repository prefix recorded in the target; never checked on input.
	Prefix string
}

Ref names a run. Round 0 means the newest round of the pull request. Local > 0 means a local run.

func NextLocal

func NextLocal(root, cmd string, getenv func(string) string, target Target, diff, draftJSON []byte, optional map[string][]byte) (string, Ref, error)

NextLocal allocates the next local run number under a lock, writes n+1 to local/next, and creates the run directory through CreateRun. target.Local must not be nil; NextLocal sets its N field before writing.

func OpenSession

func OpenSession(root string, ref Ref) (Ref, bool, error)

OpenSession reports a review running on any round of ref's pull request: the resolved run first, then the others newest first. A new round is a new run, so a per-run probe would let a second pane open on the same pull request. For local refs, only that run's directory is probed.

func ParseRef

func ParseRef(s string) (Ref, error)

func ResolveBranch

func ResolveBranch(ctx context.Context, root, clone string, gh func() (github.Client, error)) (Ref, error)

ResolveBranch takes a client constructor so a clone with no usable branch refuses before credentials are read.

func ResolveRef

func ResolveRef(root string, ref Ref) (string, Ref, error)

func (Ref) String

func (r Ref) String() string

type Session

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

Session is a running review's shared flock on the run. The kernel drops it when the process ends by any route, so an open session is always a live one; several reviews of one run can hold it at once.

func HoldSession

func HoldSession(dir string) (*Session, error)

func (*Session) Release

func (s *Session) Release() error

type Target

type Target struct {
	Schema int    `json:"schema"`
	Owner  string `json:"owner"`
	Repo   string `json:"repo"`
	Number int    `json:"number"`
	URL    string `json:"url"`
	Title  string `json:"title"`
	Author string `json:"author"`
	Viewer string `json:"viewer"`
	// BaseSHA and HeadSHA come from the API and were verified against the fetched refs.
	BaseSHA string `json:"baseSha"`
	HeadSHA string `json:"headSha"`
	// Round is 1-based, one higher than any existing round for the pull request.
	Round int `json:"round"`
	// PreviousRound is Round-1 when any earlier round exists, published or not; lineage only.
	PreviousRound int       `json:"previousRound,omitempty"`
	CapturedAt    time.Time `json:"capturedAt"`
	ClonePath     string    `json:"clonePath"`
	// BaseRef and HeadRef are refs/loupe/<owner>/<repo>/<number>/<round>/{base,head}.
	BaseRef string `json:"baseRef"`
	HeadRef string `json:"headRef"`
	// MergeBaseSHA is the merge base of BaseRef and HeadRef, the commit pr.diff compares the head against.
	MergeBaseSHA string `json:"mergeBaseSha"`
	DiffSHA256   string `json:"diffSha256"`
	// Source names what filed the findings, as name[@version]; empty when capture was given none.
	Source string `json:"source,omitempty"`
	// Model is the reviewer's model id as the caller names it; empty when capture was given none. Provenance only.
	Model string `json:"model,omitempty"`
	// PRState is the pull request's state as GitHub reported it at capture, open or closed; empty before schema 2.
	PRState string `json:"prState,omitempty"`
	// Kind is KindLocal for local runs; empty for PR runs.
	Kind string `json:"kind,omitempty"`
	// Applier names the agent that will apply changes; local runs only.
	Applier string `json:"applier,omitempty"`
	// Local holds the local-run-specific fields; nil for PR runs.
	Local *LocalTarget `json:"local,omitempty"`
}

Target is written once by capture and never changed.

func LoadTarget

func LoadTarget(dir string) (Target, error)

func (Target) MarshalJSON

func (t Target) MarshalJSON() ([]byte, error)

MarshalJSON writes local targets as LocalTargetFile and PR targets as the standard struct.

Jump to

Keyboard shortcuts

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