loop

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 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 loop is the implement loop's engine (itd-2609201916151817, spc-2609202134338445): the state file a run lives in, the checks that decide whether a run may start, and the step interface a driving host calls. `abcd build <itd-N>` starts a run; `abcd implement step` performs the next step and exits; `abcd implement receipt <path>` hands back what an agent step waited on; `abcd implement status` renders the state. Nothing here holds a run in memory between invocations: every call reads the state file first, does at most one step, writes the state file last and returns (decisions 1 and 2), so a killed process loses only the step it was in, which the next call repeats.

The state lives in the checkout's local tier:

.abcd/.work.local/run/<run-id>/state.json   the run: its key, lanes, clock and record
.abcd/.work.local/run/.lock                 the advisory lock every mutation takes

The tier is never created here: only a repository abcd manages has it, which is what keeps a run managed-only without a check of its own to drift from (internal/core/mode draws the same line). The run directories beneath it are created one level at a time and proved real (fsutil.EnsureRealDirAll), and the state is written through the package's one atomic writer inside an os.Root, so a symlinked component is refused rather than followed.

The lane's steps are a sequence (Sequence), and each step's body is a Handler the piece of the spec that delivers it registers in DefaultSteps: the worktree (piece 6, lane.go), the brief (piece 5, brief.go), the implement step and its receipt's verifier (piece 7, receipt.go), the validators (piece 8) and the landing (piece 9). A step whose body this build does not carry is refused by name, with the piece that delivers it, and the run is left unchanged. A lane's files live in its own directory of the run:

.abcd/.work.local/run/<run-id>/<lane-id>/brief.md      the brief the loop renders
.abcd/.work.local/run/<run-id>/<lane-id>/receipt.json  the implementer's receipt

and its worktree in the machine-scoped store, ~/.abcd/worktrees/<root-sha>/<run-id>-<lane-id>. The process driver (piece 3, waiting on the runner intent itd-2609201916056194) is the same loop called by a process instead of a host: it starts the agent an Await names through the runner and then calls Receipt, so it needs no seam beyond the two this package exports.

Core never writes to stdout; the CLI front door formats what these functions return.

Index

Constants

View Source
const (
	BriefFileName   = "brief.md"
	ReceiptFileName = "receipt.json"
	ReportFileName  = "report.md"
	DoDFileName     = "dod.log"
)

The files of a lane's directory, .abcd/.work.local/run/<run-id>/<lane-id>/. The loop writes the brief; the implementer writes the other three.

View Source
const (
	CheckKey           = "key"
	CheckReady         = "ready"
	CheckOpenQuestions = "open_questions"
	CheckClaimSections = "claim_sections"
	CheckHold          = "hold"
	CheckSteps         = "steps"
	CheckPeers         = "peers"
)

Check names, in the fixed order Check reports them.

View Source
const BranchPrefix = "build/"

BranchPrefix is the namespace every lane branch is cut under.

View Source
const ConventionsFile = "AGENTS.md"

ConventionsFile is the file a brief's conventions are read from.

View Source
const DecisionsLogRel = ".abcd/work/DECISIONS.md"

DecisionsLogRel is the append-only decision log the brief quotes entries of.

View Source
const ReceiptSchemaVersion = 1

ReceiptSchemaVersion is the receipt's shape.

View Source
const RoleImplementer = "implementer"

RoleImplementer is the agent the implement step hands a lane to.

View Source
const RunIDFamily = "run"

RunIDFamily is the run id's prefix; the id is minted through the record-id seam (adr-45), so two checkouts starting runs in one second draw distinct ids.

View Source
const RunRelDir = TierRelDir + "/run"

RunRelDir is the directory every run of this checkout lives under.

View Source
const SchemaVersion = 1

SchemaVersion is the state file's shape. A file carrying any other version is refused rather than read as this one: a field a later build added and this one would drop on its next write is a run silently losing state.

View Source
const StateFileName = "state.json"

StateFileName is the name of a run's state file inside its directory.

View Source
const TierRelDir = ".abcd/.work.local"

TierRelDir is the local-ephemeral tier the run state lives in. It is never created here.

View Source
const WorktreeStoreRel = ".abcd/worktrees"

WorktreeStoreRel is the machine-scoped worktree store, relative to the caller's home.

Variables

Sequence is the lane's steps in the order the loop performs them: make the lane's worktree, render its brief, hand it to an implementer and take the receipt, run the validators, land it. A lane past its last step is StepDone.

Functions

func Resolve

func Resolve(repoRoot, runID string) (string, error)

Resolve names the run a call addresses. An explicit id is checked for shape and presence; no id addresses the one run in this checkout that is not complete, and is refused naming them when there are several, or none.

func StateRelPath

func StateRelPath(runID string) string

StateRelPath is a run's state file, relative to the checkout root.

func ValidLaneID

func ValidLaneID(id string) bool

ValidLaneID reports whether id is a lane id the loop opens.

func ValidRunID

func ValidRunID(id string) bool

ValidRunID reports whether id is a run id this package mints.

Types

type Await

type Await struct {
	// Role is the agent the host starts: an implementer, or a validator that
	// did not implement.
	Role string `json:"role"`
	// Brief is the file the agent is handed.
	Brief string `json:"brief"`
	// Receipt is where the agent writes its receipt; `implement receipt` is
	// called with this path.
	Receipt string    `json:"receipt"`
	Since   time.Time `json:"since"`
}

Await is what a lane waits on: the agent a host must start, the brief it is handed and the receipt it writes.

type CheckResult

type CheckResult struct {
	Key    string     `json:"key"`
	Intent string     `json:"intent,omitempty"`
	Spec   string     `json:"spec,omitempty"`
	OK     bool       `json:"ok"`
	Checks []CheckRow `json:"checks"`
	// contains filtered or unexported fields
}

CheckResult is every pre-start check for one key.

func Check

func Check(repoRoot, key string) (CheckResult, error)

Check runs the pre-start checks for key against the checkout at repoRoot. It returns an error only for a fault in reading this checkout (the store will not load, git will not answer); a record that may not start is a result with OK false.

The rows, in order:

  • key: an intent id. An issue id is decision 10's key, which a later piece of the spec admits with drain's eligibility rule.
  • ready: the implement-readiness gate (intent.Ready) — planned, criteria, the spec linked and written. Its advisory rows stay advisory here.
  • open_questions: no open question under `## Open Questions` (intent.OpenQuestions: every list item, less the settled markers).
  • claim_sections: no unanswered claim section — the mechanism prompt answered or the section absent, the scope conditions recorded.
  • hold: no `held:` on the record (iss-2609200830076665).
  • steps: the spec's `## Steps` reads, and leaves a step to build.
  • peers: no peer holds the record — no sibling worktree or local branch holds it in another bucket, and no session holds a live claim on it.

type CheckRow

type CheckRow struct {
	Name   string `json:"name"`
	OK     bool   `json:"ok"`
	Detail string `json:"detail"`
	Remedy string `json:"remedy,omitempty"`
	// contains filtered or unexported fields
}

CheckRow is one pre-start check's verdict.

type Context

type Context struct {
	RepoRoot string
	// RunDir is the run's directory, relative to RepoRoot.
	RunDir string
	State  State
	Now    time.Time
}

Context is what a step's body is handed: where the checkout and the run live, the run as it stood before the step, and the clock.

type DoDRun

type DoDRun struct {
	Command  string `json:"command"`
	ExitCode *int   `json:"exit_code"`
	// Output is the run's whole output, a path inside the lane's directory.
	Output string `json:"output"`
}

DoDRun is one run of the definition of done.

type Driver

type Driver string

Driver names what drives the loop. The host session is decision 5's default; the process driver is piece 3's, opt-in by configuration.

const DriverHost Driver = "host"

DriverHost is a host session calling step and receipt itself.

type Entry

type Entry struct {
	At   time.Time `json:"at"`
	Lane string    `json:"lane,omitempty"`
	// Step is the step the entry records: a lane step, "start" or "receipt".
	Step string `json:"step"`
	Note string `json:"note,omitempty"`
}

Entry is one line of the run record.

type Handler

type Handler func(c Context, lane *Lane) (Outcome, error)

Handler performs one step for a lane, writing what it made into lane. It must be idempotent: a process killed after the body's effect and before the state write runs the body again on the next call, so a body finds what it made last time rather than making it twice. An error leaves the state as it was; a *Refusal error is passed to the caller as the refusal.

type Lane

type Lane struct {
	// ID is the lane's name inside the run: lane-1, lane-2, ….
	ID string `json:"id"`
	// Key is the record the lane delivers (itd-N; iss-N once decision 10 lands).
	Key string `json:"key"`
	// SpecStep is the number of the spec step the lane lands, and StepTitle its
	// title. An unstepped spec is one implicit step, number 1.
	SpecStep  int    `json:"spec_step"`
	StepTitle string `json:"step_title"`
	// Step is the next step the loop performs for this lane; StepDone when the
	// lane has nothing left.
	Step StepName `json:"step"`
	// Awaiting is set while the lane waits on an agent: the step handed its
	// work out and advances only on the receipt it names (criterion 8).
	Awaiting *Await `json:"awaiting,omitempty"`
	// The lane's footprint, filled by the steps that make it.
	Branch   string `json:"branch,omitempty"`
	BaseSHA  string `json:"base_sha,omitempty"`
	HeadSHA  string `json:"head_sha,omitempty"`
	Worktree string `json:"worktree,omitempty"`
	Brief    string `json:"brief,omitempty"`
	Receipt  string `json:"receipt,omitempty"`
	PR       int    `json:"pr,omitempty"`
}

Lane is one lane: one spec step, one branch, one pull request.

type LaneReceipt

type LaneReceipt struct {
	SchemaVersion int    `json:"schema_version"`
	RunID         string `json:"run_id"`
	Lane          string `json:"lane"`
	Branch        string `json:"branch"`
	// Commits are the full object names of the commits the implementer made on
	// the lane's branch.
	Commits []string `json:"commits"`
	// DefinitionOfDone is the repository's definition of done as the
	// implementer ran it.
	DefinitionOfDone *DoDRun `json:"definition_of_done"`
	// Report is the implementer's report, a path inside the lane's directory.
	Report string `json:"report"`
	// Model is the model the implementer's harness reported, as reported: the
	// binary cannot verify it.
	Model string `json:"model,omitempty"`
}

LaneReceipt is the file an implementer writes when its lane is built.

type LaneWorktree

type LaneWorktree struct {
	// Home is the caller's home, the base the store is proved real from.
	Home string
	// RootSHA keys the store on the repository.
	RootSHA string
	// StoreRel is the lane's store directory, relative to Home.
	StoreRel string
	// Path is the worktree, Home/StoreRel/<name>.
	Path string
	// Branch is the lane's branch, build/<name>.
	Branch string
}

LaneWorktree is where a lane's worktree lives and what it is called.

type Options

type Options struct {
	// Now is the clock; nil means time.Now.
	Now func() time.Time
	// Minter mints the run id; the zero value is production.
	Minter recordid.Minter
}

Options are the seams tests set; the zero value is production.

type Outcome

type Outcome struct {
	Await *Await
	// Note is the run record's line for the step.
	Note string
}

Outcome is what a step's body returns. A body that hands its work to an agent returns Await: the lane then waits on the receipt it names and the step completes only when Receipt verifies it.

type PendingStep

type PendingStep struct {
	Number int    `json:"number"`
	Title  string `json:"title"`
}

PendingStep is a spec step the run will open a lane for.

type Refusal

type Refusal struct {
	// Step is where the loop refused: "check" before a run starts, "state" for
	// a run that cannot be read, "pause" for the window clock, or the lane step
	// ("worktree", "implement", …) and "receipt" inside a run.
	Step string `json:"step"`
	// Check names the pre-start check that failed, when Step is "check".
	Check string `json:"check,omitempty"`
	// Lane names the lane, inside a run.
	Lane   string `json:"lane,omitempty"`
	Reason string `json:"reason"`
	Remedy string `json:"remedy"`
	// Contention marks a refusal that is somebody else's hold rather than the
	// caller's fault — a peer holding the record, a run locked by another
	// invocation, a pause: back off and take other work. The CLI maps it to
	// exit 3, as `abcd implement` does.
	Contention bool `json:"contention,omitempty"`
	// Checks is every pre-start check's row, when Step is "check", so a caller
	// sees the whole picture rather than the first failure.
	Checks []CheckRow `json:"checks,omitempty"`
}

Refusal is the one shape every refusal of the loop takes (criterion 13): the step it happened at, the reason and the remedy, in text and in --json. A refusal writes nothing: the state file is as it was before the call.

func AsRefusal

func AsRefusal(err error) (*Refusal, bool)

AsRefusal returns err's Refusal, if it carries one.

func (*Refusal) Error

func (r *Refusal) Error() string

Error renders the refusal as one line: step, reason, remedy.

type StartResult

type StartResult struct {
	RunID string `json:"run_id"`
	// State is the state file, relative to the checkout root.
	State string `json:"state"`
	// Resumed is true when a live run for the key already existed: the call
	// created nothing and names that run.
	Resumed bool          `json:"resumed"`
	Lane    Lane          `json:"lane"`
	Pending []PendingStep `json:"pending"`
	// Checks are the pre-start checks' rows when the call created the run; a
	// resumed start runs none, and carries none.
	Checks []CheckRow `json:"checks"`
	Next   string     `json:"next"`
}

StartResult is what Start returns.

func Start

func Start(repoRoot, key string, o Options) (StartResult, error)

Start resumes the live run for key, or runs the checks and, when every one passes, creates the run: the state file with one lane at the sequence's first step, the spec's other unlanded steps pending, and the record's first line. A refused check writes nothing.

A live run for the same key in this checkout is looked up first, under the lock, and resumed — named, not duplicated, and not re-judged. The checks judged the record when the run was created; since then the run's own lanes change the tree they read (a lane's worktree moves the intent to shipped/, a lane claims it), so judging again would refuse the run as its own peer. So starting again after a kill loses nothing and repeats nothing, and the checks run only when a run is created. Only the key's shape is checked before the lookup, so a path is never built from a key that is not an intent id.

type State

type State struct {
	SchemaVersion int `json:"schema_version"`
	// RunID names the run and its directory.
	RunID string `json:"run_id"`
	// Key is the record the run was started for: an intent id (an issue id is
	// decision 10's, which a later piece admits).
	Key string `json:"key"`
	// Intent and Spec are the intent the run delivers and the open spec it
	// builds against, as the readiness gate judged them.
	Intent string `json:"intent"`
	Spec   string `json:"spec"`
	// Driver is what drives the loop (DriverHost unless configured).
	Driver    Driver    `json:"driver"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	// WindowStartedAt and NextEligibleAt are the window clock the pacing intent
	// (itd-2609201925079472) writes and reads. The loop honours NextEligibleAt:
	// before it, a step is refused as a pause and nothing moves (decision 2).
	WindowStartedAt *time.Time `json:"window_started_at,omitempty"`
	NextEligibleAt  *time.Time `json:"next_eligible_at,omitempty"`
	// Lanes are the lanes opened so far, one at a time, in order. A lane lands
	// one step of the spec's `## Steps` (the whole spec when it lists none).
	Lanes []Lane `json:"lanes"`
	// Pending are the spec's unlanded steps no lane has been opened for yet.
	Pending []PendingStep `json:"pending"`
	// Record is the run record, accumulated as steps complete.
	Record []Entry `json:"record"`
}

State is one run: everything the loop needs between two invocations.

func ReadState

func ReadState(repoRoot, runID string) (State, error)

ReadState is the state file's one reader. It refuses a run id of the wrong shape before building a path from it, reads through the checkout's os.Root (so a symlinked component cannot walk the read out of the checkout), decodes strictly — an unknown field is a file some other writer produced — and holds the file to the version and the id it is stored under.

func Runs

func Runs(repoRoot string) ([]State, error)

Runs lists this checkout's runs, oldest first. An absent tier or run directory holds none. A state file that cannot be read fails the listing, naming it: a run the loop cannot read is not one it may skip past.

func (State) Complete

func (s State) Complete() bool

Complete reports whether the run has nothing left: every lane is done and no spec step is waiting for one.

type StepDef

type StepDef struct {
	Name   StepName
	Piece  int
	Run    Handler
	Verify Verifier
}

StepDef is one step of the lane sequence: its name, the spec piece that delivers its body, and the body — nil when this build does not carry it.

type StepName

type StepName string

StepName is one step of a lane.

const (
	StepWorktree  StepName = "worktree"
	StepBrief     StepName = "brief"
	StepImplement StepName = "implement"
	StepValidate  StepName = "validate"
	StepLand      StepName = "land"
	StepDone      StepName = "done"
)

The lane's steps, in the order Sequence performs them. StepDone is the state of a lane with nothing left to do, never a step with a body.

type StepResult

type StepResult struct {
	RunID string `json:"run_id"`
	Lane  string `json:"lane,omitempty"`
	// Performed is the step this call completed; empty when it completed none
	// (the lane awaits a receipt, or the run is complete).
	Performed StepName `json:"performed,omitempty"`
	// Step is the lane's next step after the call.
	Step StepName `json:"step,omitempty"`
	// Awaiting is what the lane waits on, when it waits on an agent.
	Awaiting *Await `json:"awaiting,omitempty"`
	Complete bool   `json:"complete"`
	Next     string `json:"next"`
}

StepResult is what Advance and Receipt return.

func Advance

func Advance(repoRoot, runID string, steps Steps, o Options) (StepResult, error)

Advance performs the next step of the run's current lane and returns. A lane that awaits a receipt performs nothing and re-tells what it awaits; a run that is complete says so; a run paused by its window clock is refused until next_eligible_at. A step whose body this build does not carry is refused naming the piece that delivers it. The state is written only after a body succeeds, and then once.

func Receipt

func Receipt(repoRoot, runID, receipt string, steps Steps, o Options) (StepResult, error)

Receipt hands back the receipt an agent step waited on. It is refused when no lane awaits one, when the path is not the one the step named, when this build carries no verifier for the step, and when the verifier refuses it; in every refusal the lane stays where it was. A verified receipt completes the step.

type Steps

type Steps []StepDef

Steps is the lane sequence with its bodies.

func DefaultSteps

func DefaultSteps() Steps

DefaultSteps is the lane sequence this build carries, each step with the spec piece that delivers its body: the worktree (lane.go), the brief (brief.go) and the implement step with its receipt's verifier (receipt.go). The validators and the landing are later pieces of spc-2609202134338445, and each registers its body here.

type Verifier

type Verifier func(c Context, lane *Lane, receipt string) error

Verifier checks the receipt an agent step waited on. An error refuses the receipt and the lane stays where it was.

Jump to

Keyboard shortcuts

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