sweep

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 45 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 sweep is the core of `abcd sweep` (spc-2610230730269295): it judges what a repository has left lying around and clears only what is provably finished.

The liveness probe errs toward keeping: every doubt holds the worktree, and every hold names its cause, so a false keep is visible and cheap to clear by hand. Its known limits:

  • It judges only worktrees this account owns: one whose top directory or .git another uid owns is held outright. Another account's process can enter this account's worktree only where this account's permissions let it, and the worktree store makes its directories 0700. So the process proof's remaining blind spot is a process run as root, or one in a tree shared through its group: such a process, like one under a hidepid /proc or in another pid namespace, is invisible to the process table. That is a documented gap, not a case the probe fails closed on. A /proc that does not list this process at all is empty or another's, and the check fails closed on it.
  • The process table is read once, when the probe is made. A process that starts after that is not seen, so a caller removing a worktree makes a fresh probe immediately before each removal, never reusing one.

Index

Constants

View Source
const (
	// ExitActed is a run that acted on something, or a dry run that would.
	ExitActed = 0
	// ExitNothing is a run that completed and acted, or would act, on nothing.
	ExitNothing = 1
	// ExitRefused is a refusal or a fault.
	ExitRefused = 2
)

The exit codes a sweep maps to (spc-2610230730269295, Rows).

View Source
const DefaultRecentWindow = 24 * time.Hour

DefaultRecentWindow is how recent a write to a worktree must be to keep it as a hint when the caller names no window (spc-2610230730269295, Liveness).

View Source
const KeyAutomatic = "sweep.automatic"

KeyAutomatic is the one setting that turns the automatic sweep off, in .abcd/config.json or ~/.abcd.noindex/config.json: a bool, on by default in a repository abcd manages. It only restricts (adr-2611011332427073): false from any layer holds, and no layer turns the sweep on outside a managed repository.

Variables

This section is empty.

Functions

func NewDeleteProver

func NewDeleteProver(o DeleteProofOptions) guard.DeleteProver

NewDeleteProver returns the prover the shell guard's front doors attach to the registry (guard.Registry.ProvingDeletes).

func RowLine added in v0.17.0

func RowLine(r Row, applied bool) string

RowLine is one row in the sweep's wording: what apply would do (applied, what it did), the kind, the name, the path where there is one, the state read, and the reason. Every field is display-ready already (Row).

Types

type Action

type Action string

Action is what `--apply` does with a row's item: remove, save-patch and keep for worktrees, delete for branches, dispatch for spent reservations and promote for waiting inbox reports.

const (
	// ActionRemove is a merged, clean worktree nothing holds, reclaimed
	// through the store's prune.
	ActionRemove Action = "remove"
	// ActionSavePatch is a dirty worktree nothing holds: its changes are saved
	// as a patch and the worktree is kept.
	ActionSavePatch Action = "save-patch"
	// ActionDelete is a merged branch nothing holds: a local one deleted with
	// `git branch -d`, a branch on origin deleted under a lease on the tip
	// that was judged.
	ActionDelete Action = "delete"
	// ActionDispatch is a spent reservation: apply dispatches the reservation
	// sweep's workflow, which deletes it. The sweep never deletes one itself.
	ActionDispatch Action = "dispatch"
	// ActionPromote is a waiting inbox report: apply files it as an issue
	// through the inbox's promote, with the automatic filer's remedy.
	ActionPromote Action = "promote"
	// ActionKeep leaves the item as it is, its reason saying why.
	ActionKeep Action = "keep"
)

type Auto added in v0.17.0

type Auto struct {
	Setting Setting
	// Ran says the sweep ran: the setting was on.
	Ran    bool
	Result Result
	// Fault is the run's refusal or fault, "" when it completed.
	Fault string
	// contains filtered or unexported fields
}

Auto is what an automatic sweep came to.

func Automatic added in v0.17.0

func Automatic(ctx context.Context, repoRoot string, a AutoOptions) (out Auto)

Automatic runs the automatic sweep of the repository at repoRoot: its setting read first, then, when it is on, Run's fetch and an applied sweep of the worktrees, the local branches and the branches on origin, the branches it deletes held to the ones abcd made (abcdMade) and the worktrees it removes to ones holding no ignored file outside the local tier (keptForIgnored). It never returns an error and never panics: a refusal, a fault, or a panic in any half, is the answer's Fault. ctx bounds the whole of it, the fetch and each delete's push included; once ctx ends, it stops between removals.

func (Auto) Text added in v0.17.0

func (a Auto) Text() string

Text is the automatic sweep's report for the act it followed, one line: what it removed and what it left, counted, then the first rows in the sweep's own row wording, acted-on rows first, then how many more and where the full list is, then any refusal or fault, which leaves the act's outcome as it was. A sweep that did not run says why only when the setting is to be named (Setting.Said); otherwise Text is "".

type AutoOptions added in v0.17.0

type AutoOptions struct {
	Options
	// Roots is where sweep.automatic is read; nil is layered.RootsFor at the
	// repository.
	Roots *layered.Roots
	// Managed says whether the repository is one abcd manages; nil asks
	// ahoy.ManagedContext. An error reads as not managed.
	Managed func(ctx context.Context, repoRoot string) (bool, error)
}

AutoOptions is one automatic sweep: the options a sweep runs with, and where its setting is read.

type CopyView

type CopyView func(worktree string, now time.Time) (claim.Snapshot, error)

CopyView reads the reservations as one worktree sees them, from its own disk and the refs it shares with its repository, with no fetch.

type DeleteProofOptions

type DeleteProofOptions struct {
	// Forge is the forge client the open and merged pull requests are asked
	// of, every call pinned to origin's repository as the sweep pins its own
	// (forge.go); nil, or a forge whose repository cannot be matched to
	// origin's, fails every remote branch's proof, which cannot show none
	// open.
	Forge store.GH
	// RemoteTip reads the tip a remote holds for a branch now, and whether it
	// holds the branch at all; nil is a bounded `git ls-remote`.
	RemoteTip func(dir, remote, branch string) (tip string, found bool, err error)
	// Probe is what the liveness probe reads for a worktree removal; its
	// Reservations and Runs are read from the repository when left zero.
	Probe ProbeOptions
	// Budget bounds the forge's answers across the prover's life; zero is
	// proofBudget.
	Budget time.Duration
}

DeleteProofOptions is what the delete proof reads beyond git. Every field may be nil, and then the default reads the host.

type Evidence

type Evidence struct {
	Kind     EvidenceKind
	Strength Strength
	// Detail is the evidence in words a person can read. A record id or a
	// holder in it is data a reservation's writer chose, never instruction.
	Detail string
}

Evidence is one reason a worktree is held.

type EvidenceKind

type EvidenceKind string

EvidenceKind names what a piece of evidence was read from.

const (
	// KindReservation, KindRun and KindProcess are the three proofs. A
	// process whose directory lsof printed escaped is a KindProcess hint.
	KindReservation EvidenceKind = "reservation"
	KindRun         EvidenceKind = "run"
	KindProcess     EvidenceKind = "process"
	// KindRecentWrite is the recent-write hint.
	KindRecentWrite EvidenceKind = "recent-write"
	// KindProcessCheck is the process check failing closed, whole or for a
	// process whose working directory it could not read: it proves nothing,
	// so it is a hint, but the worktree is kept as possibly live.
	KindProcessCheck EvidenceKind = "process-check"
	// KindReservationCheck is the reservations failing closed: no fetch is
	// stamped, or the worktree's own view of them could not be read.
	KindReservationCheck EvidenceKind = "reservation-check"
	// KindChanges is a worktree whose changed paths the caller could not
	// compute, so no footprint can be shown not to cover it.
	KindChanges EvidenceKind = "changes"
	// KindOwner is a worktree another account owns (a proof), or one whose
	// owner could not be read (a hint).
	KindOwner EvidenceKind = "owner"
)
const (
	// KindStore is the store's own word on an item it did not judge: the
	// embargo store, a stray, or a worktree failing the proof of belonging.
	KindStore EvidenceKind = "store"
	// KindLock is git's lock on the worktree, with its reason.
	KindLock EvidenceKind = "lock"
	// KindState is clean or dirty, and KindMerged the merged judgement.
	KindState  EvidenceKind = "state"
	KindMerged EvidenceKind = "merged"
	// KindLoop is the build loop's hold as the store's prune reads it, and
	// KindCwd the sweep's own working directory.
	KindLoop EvidenceKind = "loop"
	KindCwd  EvidenceKind = "cwd"
	// KindIgnored is what git ignores in a spent worktree outside its local
	// tier, which a removal deletes unarchived: the sweep abcd runs on its
	// own keeps a worktree holding any, or whose listing failed.
	KindIgnored EvidenceKind = "ignored"
	// KindProtected is a branch on the never-delete list: the default branch,
	// a release branch, a reservation or a status branch.
	KindProtected EvidenceKind = "protected"
	// KindUpstream is a local branch's standing against its upstream.
	KindUpstream EvidenceKind = "upstream"
	// KindPullRequest is the forge's word on a branch's pull requests: an
	// open one proves it held, and a forge that could not be asked is a hint.
	KindPullRequest EvidenceKind = "pull-request"
	// KindDeleteProof is a check of the shell guard's delete proof that a
	// branch fails: one of abcd's coordination branches, or one a worktree
	// has checked out or a stopped rebase or bisect holds.
	KindDeleteProof EvidenceKind = "delete-proof"
	// KindRemote is origin's own answer at the last check before a delete:
	// its default branch and the branch's tip there.
	KindRemote EvidenceKind = "remote"
	// KindReservationSweep is the reservation sweep's judgement of a
	// reservation branch, and KindWorkflow the dispatch of its workflow.
	KindReservationSweep EvidenceKind = "reservation-sweep"
	KindWorkflow         EvidenceKind = "workflow"
	// KindReport is what the inbox read of a report, and KindPromote the
	// inbox's promote of it.
	KindReport  EvidenceKind = "report"
	KindPromote EvidenceKind = "promote"
)

The evidence kinds a row adds to the liveness probe's: what the store read of the worktree, each a proof, since git said it.

type Liveness

type Liveness struct {
	Held bool
	// Evidence lists the proofs first, then the hints. The recent-write hint
	// is read only when nothing else holds the worktree: it would add nothing
	// to a hold, and it is the one check that walks the tree.
	Evidence []Evidence
	// Reason is the evidence joined, or why nothing holds the worktree.
	Reason string
}

Liveness is the probe's judgement of one worktree.

type Options

type Options struct {
	// Probe is what the liveness probe reads: the reservations already
	// fetched, the build loop's runs, the process lister and the clock.
	Probe ProbeOptions
	// Forge is the forge client every half asks: the store's merged
	// judgement, the pull requests on origin's branches and the reservation
	// workflow's dispatch. nil asks none, and merged is judged from git
	// alone. Every call is pinned to origin's repository (forge.go); when it
	// cannot be, none is made.
	Forge store.GH
	// Held reads what the build loop holds, as the store's prune reads it. It
	// is required: like prune, a sweep that cannot tell what the loop holds
	// acts on nothing.
	Held store.HeldReader
	// Cwd is the sweep's own working directory; the worktree holding it is
	// kept, as prune keeps its own. It is required, as Held is: a sweep that
	// does not know where it runs could remove the worktree it runs in.
	Cwd string
	// Match is the filing-time match each promoted inbox report's record
	// runs, configured as the inbox's promote verb configures it; nil files
	// each unmatched.
	Match *match.Config
	// contains filtered or unexported fields
}

Options is one dry run. The caller fetches first (criterion 11) and hands the reservations it read in Probe.Reservations: the dry run itself reaches no remote.

type Outcome

type Outcome string

Outcome is what `--apply` did with a row's item.

const (
	// OutcomeRemoved is a worktree the store's prune reclaimed.
	OutcomeRemoved Outcome = "removed"
	// OutcomeSaved is a dirty worktree whose changes were saved as a patch;
	// the worktree itself is kept.
	OutcomeSaved Outcome = "saved"
	// OutcomeDeleted is a branch the delete took.
	OutcomeDeleted Outcome = "deleted"
	// OutcomeDispatched is a spent reservation the reservation sweep's
	// workflow was dispatched to delete.
	OutcomeDispatched Outcome = "dispatched"
	// OutcomePromoted is an inbox report filed as an issue.
	OutcomePromoted Outcome = "promoted"
	// OutcomeKept is an item left as it was, its reason saying why: one the
	// dry run kept, or one a check at the moment of acting kept.
	OutcomeKept Outcome = "kept"
)

type OwnerCheck

type OwnerCheck func(worktree string) (uid int, foreign bool, err error)

OwnerCheck reports whether a worktree is owned by an account other than this process's, and that account's uid when it is. An error means the owner could not be read, and the worktree is then kept.

type Probe

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

Probe judges worktrees against one reading of the process table, taken when it is made: a sweep runs the process check once, not once per worktree.

func (*Probe) Judge

func (p *Probe) Judge(w Worktree) Liveness

Judge says whether the worktree is held, and by what.

func (*Probe) ProcessCheckUnavailable

func (p *Probe) ProcessCheckUnavailable() string

ProcessCheckUnavailable is why the process check could not be made, "" when it was. Every worktree is then held as possibly live, and a dry run says so once at the top.

func (*Probe) ReservationsUnread

func (p *Probe) ReservationsUnread() string

ReservationsUnread is why the reservations cannot show that nothing is reserved, "" when they can. Every worktree is then held, and a dry run says so once at the top.

type ProbeOptions

type ProbeOptions struct {
	// Reservations is the claim view after the sweep's fetch (claim.View).
	// A FetchedAt of zero means no fetch is stamped, so the view cannot show
	// that nothing is reserved: every worktree is then held.
	Reservations claim.Snapshot
	// CopyView reads each worktree's own view of the reservations, to find
	// one made from that worktree (its tip naming the worktree's copy key);
	// nil is claim.View on the worktree. It reads only local refs, so the
	// sweep's one fetch, made before Reservations was read, serves it too.
	CopyView CopyView
	// Owner says whether another account owns the worktree; nil reads the
	// owner of its top directory and its .git.
	Owner OwnerCheck
	// Runs is the build loop's run state (loop.Runs).
	Runs []loop.State
	// RunsUnread is why the run state could not be read, "" when it was. An
	// empty Runs cannot then show that no unfinished run names a worktree, so
	// every worktree is held, as unread reservations hold them.
	RunsUnread string
	// Processes lists the processes; nil is hostProcesses.
	Processes ProcessLister
	// Now is the clock, read once; nil is time.Now.
	Now func() time.Time
	// RecentWindow is the recent-write hint's window; zero or less is
	// DefaultRecentWindow.
	RecentWindow time.Duration
}

ProbeOptions is what the probe reads. The caller fetches and reads the stores; the probe reads only the worktree and the process table.

type Process

type Process struct {
	PID int
	Cwd string
	// Escaped marks a working directory lsof printed with an escape in it.
	// Cwd is then only the text before the first backslash or caret: the real
	// directory's leading bytes, cut wherever the escape fell, perhaps in the
	// middle of a name.
	Escaped bool
	// Unreadable is why a process this account may own would not show its
	// working directory (on Linux, a link the kernel refuses for permission:
	// a process that is not dumpable, or one in another user namespace).
	// Cwd is then empty, and the process may work in any worktree.
	Unreadable string
}

Process is one running process and its working directory.

type ProcessLister

type ProcessLister func(ctx context.Context) ([]Process, error)

ProcessLister lists the running processes' working directories. An error means the check could not be made, and the probe then fails closed.

type Result

type Result struct {
	// Lane is the repository's lane in the worktree store, home-redacted.
	Lane string `json:"lane,omitempty"`
	Rows []Row  `json:"rows"`
	// ProcessCheckUnavailable is why the process check could not be made.
	ProcessCheckUnavailable string `json:"process_check_unavailable,omitempty"`
	// ReservationsUnread is why the reservations cannot show that nothing is
	// reserved.
	ReservationsUnread string `json:"reservations_unread,omitempty"`
	// RunsUnread is why the build runs could not be read.
	RunsUnread string `json:"runs_unread,omitempty"`
	// ForgeNote is the store's note that no forge answered.
	ForgeNote string `json:"forge_note,omitempty"`
	// Workflow is what became of the reservation sweep's workflow: the run a
	// dispatch started, or why none was dispatched and when the workflow's
	// schedule runs it next.
	Workflow string `json:"workflow,omitempty"`
	// Refusal is why the run refused or faulted; "" when it completed.
	Refusal string `json:"refusal,omitempty"`
	// Applied marks a run that acted, each row carrying its Outcome; a dry
	// run leaves it false.
	Applied bool `json:"applied,omitempty"`
}

Result is one sweep's answer. The process check, the reservation read and the build-run read each fail closed, holding every worktree; when one could not be made, the result says so once, here, as well as on every row it holds.

func Run added in v0.17.0

func Run(ctx context.Context, repoRoot string, o Options, apply bool) (Result, error)

Run is one whole sweep of the repository at repoRoot, as `abcd sweep` runs it: a dry run, or with apply an applied one. It fetches first (criterion 11): origin's branches, pruned, so the half that judges them reads origin as it is, then the reservations (claim.Fetch). It then reads into the probe the reservations as fetched and the build loop's runs, unless the options already carry them. A checkout with no origin fetches nothing and runs on: no fetch of the reservations is then stamped, so every worktree is held, and origin's half has no branch to judge. A fetch that fails refuses the run before anything is judged, since every judgement after it would read origin as it was at some earlier fetch.

func (Result) ExitCode

func (r Result) ExitCode() int

ExitCode is the code the front door exits with: ExitRefused on a refusal or fault, whatever rows came before it; otherwise, for an applied run, ExitActed when any row's outcome is other than kept, and for a dry run when any row carries an action apply would take; ExitNothing when none does. A save-patch row is one: the dry run has already collected its salvage, and one whose changes are saved unchanged by an earlier run, or cannot be saved, is a keep, so a dry run never promises what apply skips.

func (Result) Notes

func (r Result) Notes() []string

Notes are the lines a rendering puts once at the top: each failed-closed check, then the forge note, then the reservation workflow's.

type Row

type Row struct {
	Kind RowKind `json:"kind"`
	Name string  `json:"name"`
	Path string  `json:"path"`
	// State is what the sweep read of the item, in words.
	State  string `json:"state"`
	Action Action `json:"action"`
	// Reason says why the item is kept, or why the action is safe.
	Reason string `json:"reason"`
	// Evidence is each thing read that bears on the action, every piece
	// marked proven or hint.
	Evidence []Evidence `json:"evidence,omitempty"`
	// Outcome is what an applied run did with the item; "" in a dry run.
	Outcome Outcome `json:"outcome,omitempty"`
	// Ignored is, on a worktree judged spent, what its removal deletes
	// unarchived: its ignored paths outside the local tier, or that they
	// could not be listed and why (store.IgnoredForRemoval). The sweep a
	// person runs names them on the remove row and removes it; the sweep abcd
	// runs on its own keeps the worktree. nil when there are none.
	Ignored *store.IgnoredState `json:"ignored,omitempty"`
}

Row is one item a sweep judged. Every string in it is display-ready: a path home-redacted the way the store's own rows are, and any text another checkout or a reservation's writer chose passed through termsafe.Sanitize.

type RowKind

type RowKind string

RowKind is what a row is about: worktrees, strays and the embargo store, local branches, branches on origin, reservations and inbox reports.

const (
	// RowWorktree is a worktree git lists, in the store or outside it, or a
	// directory in the store's lane that fails its proof of belonging.
	RowWorktree RowKind = "worktree"
	// RowStray is an entry in the store's lane that is not a directory: a
	// loose file, say. It is listed and never touched.
	RowStray RowKind = "stray"
	// RowEmbargoStore is abcd's embargo store, never read, saved or removed.
	RowEmbargoStore RowKind = "embargo-store"
	// RowBranch is a local branch (branches.go).
	RowBranch RowKind = "branch"
	// RowRemoteBranch is a branch on origin, as this checkout last fetched it
	// (remote.go).
	RowRemoteBranch RowKind = "remote-branch"
	// RowReservation is a reservation branch on origin, as the reservation
	// sweep judged it (reservations.go).
	RowReservation RowKind = "reservation"
	// RowInbox is a report waiting in abcd's inbox (inbox.go).
	RowInbox RowKind = "inbox"
)

type Setting added in v0.17.0

type Setting struct {
	On bool
	// Why is why the sweep is off, "" when it is on.
	Why string
	// Said is whether the act's output names the off: a layer that turned it
	// off, or a setting that could not be read. Outside a managed repository
	// the sweep is off and nothing is said.
	Said bool
}

Setting is sweep.automatic as resolved for one repository.

type Strength

type Strength string

Strength says whether a piece of evidence proves a worktree is held or only hints that it may be. Both keep the worktree; a row shows which it was.

const (
	Proven Strength = "proven"
	Hint   Strength = "hint"
)

type Worktree

type Worktree struct {
	// Path is the worktree's absolute path.
	Path string
	// Changed is the repository-relative, slash-separated paths the worktree
	// changes: its branch's diff from the default branch and its uncommitted
	// and untracked files. A footprint is a set of repository paths, never a
	// worktree's location, so a reservation names a worktree by covering a
	// path it changes.
	//
	// nil means the caller could not compute the changes, and the worktree is
	// held as "changes unknown"; an empty, non-nil slice means it changes
	// nothing.
	Changed []string
}

Worktree is one worktree as the probe judges it.

Jump to

Keyboard shortcuts

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