instructions

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 8 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 instructions renders, detects, upgrades, and removes the Orch-managed instruction block inside a repository's root AGENTS.md/CLAUDE.md file (PRD §19):

<!-- orchestrator:managed:start version=1 -->
...engine-owned block content...
<!-- orchestrator:managed:end -->

Every byte outside the region is human-owned and preserved verbatim by Plan/PlanFile — the same exact-line-marker, byte-splice discipline internal/manifest applies to issue and PR bodies. The engine owns the canonical content for each block version (block.go); Inspect classifies an existing region against it without ever erroring, and Plan/PlanRemove turn that classification into a proposed byte-exact change plus a unified diff, leaving approval and the actual write to the caller (apply.go). Conflicts found outside the two managed files (scan.go) are reported, never edited — nested files are someone else's to fix.

The package is stdlib-only and policy-free: it trusts whatever path it is handed. It does not canonicalize paths, does not check containment under a repository root, and does not decide which files count as "the" root AGENTS.md/CLAUDE.md. Building safe paths with paths.FindRoot and filepath.Join, and keeping this package's writes confined to files a human or `orch init`/`configure` (tasks 15/16) explicitly named, is entirely the caller's responsibility.

Index

Constants

View Source
const CurrentVersion = 1

CurrentVersion is the managed-block schema this build renders and recognizes as current. Only version 1 exists today; a future version is added by extending canonicalBody's registry, never by mutating an existing version's body — Inspect's drift and stale classification for old content depends on old bodies staying byte-stable forever.

View Source
const EndMarker = "<!-- orchestrator:managed:end -->"

EndMarker delimits the end of the managed region. A line is the end marker only if, after stripping at most one trailing "\r", it equals this string exactly (the internal/manifest marker-matching idiom): a mid-line mention in prose is ordinary content, never a match.

Variables

View Source
var (
	// ErrMalformed reports a managed region that is present but
	// structurally unusable: a duplicate, nested, misordered, or
	// unpaired begin/end marker, or a begin marker whose version
	// overflows strconv.Atoi. Plan and PlanRemove both fail closed on
	// it.
	ErrMalformed = errors.New("managed region is malformed")
	// ErrDrifted reports a region at CurrentVersion whose body does
	// not byte-match the canonical render for that version — the
	// block was hand-edited. Plan fails closed on it; PlanRemove does
	// not (removal needs only the marker lines, not a trusted body).
	ErrDrifted = errors.New("managed region body has drifted from the canonical render")
	// ErrNewerVersion reports a region whose marker declares a
	// version newer than CurrentVersion. This build has no canonical
	// content for that version to compare or upgrade to, so Plan
	// fails closed rather than guess; PlanRemove does not (the marker
	// lines alone are enough to remove the region).
	ErrNewerVersion = errors.New("managed region declares a version newer than this build knows")
)

Sentinel errors callers test with errors.Is. Each marks a distinct fail-closed structural condition; specifics wrap these with %w and a human-readable detail (manifest/state style) rather than replace them.

Functions

func Apply

func Apply(path string, change Change) error

Apply writes change.New to path, or does nothing when change.Old equals change.New (ActionNone). There is deliberately no combined plan-and-apply entry point for install/upgrade: the PRD requires a diff preview before approval, and merging the two steps would make it too easy to skip that gate.

func IsOtherwiseEmpty

func IsOtherwiseEmpty(content string) bool

IsOtherwiseEmpty reports whether content is empty once surrounding whitespace is discounted (strings.TrimSpace(content) == ""). PlanRemove uses it to decide DeleteWholeFile: a file that held nothing but the managed region and blank lines around it can be deleted outright instead of left behind as whitespace.

func Remove

func Remove(path string, confirm Confirmation) (Change, RemoveResult, error)

Remove is PlanRemoveFile followed by ApplyRemove: the convenience entry point for deinit callers that don't need the plan and apply steps separately.

func Render

func Render(version int) (string, error)

Render returns the full managed block for version — begin marker, canonical body, end marker — as LF-only text with no trailing newline, or an error if version is unknown. It is pure: the same version always yields the same bytes, which is what makes Inspect's drift check sound (classify byte-compares a found region against this output).

func UnifiedDiff

func UnifiedDiff(old, newText string) string

UnifiedDiff renders old and newText's line-level differences as unified-diff text; it is unifiedDiff's exported form, for internal/interview's configure-local summary, which needs a diff display surface over an entire proposed file rather than one Plan/PlanRemove-scoped Change. See unifiedDiff's doc comment for the exact format and its documented limitation.

Types

type Action

type Action int

Action is the change Plan or PlanRemove proposes for a managed region. It is a closed enumeration.

const (
	// ActionNone proposes no change: nothing to install, and the
	// existing region already matches the canonical render.
	ActionNone Action = iota
	// ActionInstall proposes appending a fresh CurrentVersion block
	// to content that carries no managed region.
	ActionInstall
	// ActionUpgrade proposes replacing an older-versioned block with
	// the CurrentVersion render, in place.
	ActionUpgrade
	// ActionRemove proposes deleting the managed region's marker
	// lines (PlanRemove only).
	ActionRemove
)

type Change

type Change struct {
	Action          Action
	FileExisted     bool
	Old, New, Diff  string
	DeleteWholeFile bool
}

Change is a proposed byte-exact edit: Old is the content before the change and New is the content after. Diff is unifiedDiff(Old, New) and is "" exactly when Old == New (ActionNone). FileExisted reports whether the file existed before the change — Plan infers this from content == "" standing in for "missing file"; PlanFile overrides it with the truth from its own read. DeleteWholeFile is set only by PlanRemove/PlanRemoveFile: it reports that New, once stripped of the managed region, is otherwise empty (IsOtherwiseEmpty), so the caller may choose to delete the file outright instead of writing a whitespace remainder.

func Plan

func Plan(content string) (Change, error)

Plan proposes a change for content's managed region: install when absent, upgrade when a valid older version is present, no-op when the region already matches the canonical render, and a fail-closed error for every other structural taxonomy row (StatusDrifted wraps ErrDrifted, StatusNewerVersion wraps ErrNewerVersion, StatusMalformed wraps ErrMalformed) — install/upgrade never proceed over content Inspect cannot trust. content == "" stands in for a missing file.

Plan never writes anything; Apply does. There is deliberately no combined plan-and-apply entry point here (see Apply's doc comment).

func PlanFile

func PlanFile(path string) (Change, error)

PlanFile reads path and calls Plan on its content, then overrides the result's FileExisted with the truth from the read: a missing file is FileExisted == false regardless of what Plan inferred from an empty content string.

func PlanRemove

func PlanRemove(content string) (Change, error)

PlanRemove proposes removing content's managed region: no-op when no markers are present, ActionRemove when an unambiguous begin/end pair is found regardless of its version or whether its body has drifted (removal only needs the marker lines, not a trusted body — StatusDrifted and StatusNewerVersion are deliberately treated the same as StatusCurrent/StatusStale here), and a fail-closed error wrapping ErrMalformed when the markers themselves are structurally broken. content == "" stands in for a missing file.

func PlanRemoveFile

func PlanRemoveFile(path string) (Change, error)

PlanRemoveFile reads path and calls PlanRemove on its content, then overrides the result's FileExisted with the truth from the read.

type Confirmation

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

Confirmation proves the caller obtained explicit human approval for a whole-file deletion (PRD §15 shape; gitops.Confirmation precedent). It is a distinct type from gitops.Confirmation so a token approved for one destructive domain (branch/worktree removal) can never be reused to authorize this package's file deletion. The zero value fails closed; only ExplicitConfirmation constructs a valid token, and this package never prompts — collecting the approval is the caller's job.

func ExplicitConfirmation

func ExplicitConfirmation() Confirmation

ExplicitConfirmation returns the token that authorizes one whole-file deletion in ApplyRemove/Remove. Call it only after a human approved that specific deletion.

type Conflict

type Conflict struct {
	Path   string
	Report Report
}

Conflict is a nested AGENTS.md/CLAUDE.md file — anywhere under root other than root's own direct pair — whose managed region is not StatusAbsent. Scan reports conflicts; it never edits them.

func Scan

func Scan(root string) ([]Conflict, error)

Scan walks root looking for AGENTS.md/CLAUDE.md files whose managed region is not absent, outside of root's own direct pair (those are the files the caller manages directly — they are not "nested" conflicts). It skips every directory whose base name starts with "." (covers .git, .orchestrator, .memhub, .github uniformly) except root itself, which is never skipped regardless of its own name. filepath.WalkDir never follows symlinks on its own; Scan reinforces that by never opening a symlinked entry, directory or file, through its target. Any directory or file Scan cannot read is a hard error naming the path — a nested conflict that silently went unseen would be worse than failing loudly.

type RemoveResult

type RemoveResult int

RemoveResult reports what ApplyRemove actually did.

const (
	// RemovedNothing reports that change.Action was ActionNone: there
	// was no managed region to remove.
	RemovedNothing RemoveResult = iota
	// RemovedBlockOnly reports that the managed region's marker lines
	// were stripped and the file rewritten with change.New.
	RemovedBlockOnly
	// RemovedWholeFile reports that the file itself was deleted,
	// because stripping the region would have left it otherwise
	// empty and the caller supplied ExplicitConfirmation.
	RemovedWholeFile
)

func ApplyRemove

func ApplyRemove(path string, change Change, confirm Confirmation) (RemoveResult, error)

ApplyRemove applies change (from PlanRemove/PlanRemoveFile) to path. ActionNone does nothing (RemovedNothing). When change.DeleteWholeFile is true and confirm is a valid ExplicitConfirmation, it deletes the file outright (RemovedWholeFile) — it never writes the whitespace remainder first, so a crash between steps can never leave a half-finished intermediate file. Otherwise — no confirmation, or the file has real content left once the region is stripped — it writes change.New (RemovedBlockOnly).

A missing Confirmation soft-degrades to a block-only removal rather than failing outright: leaving the (now block-free) file in place is always a safe, reversible partial completion, unlike gitops' RemoveWorktree, which has no such degrade available for a real destructive git operation with no safe partial form. This is a judgment call — flagged for reviewer attention.

type Report

type Report struct {
	Status  Status
	Version int
	Detail  string
}

Report is Inspect's pure classification of a managed region: never an error, always a Status plus enough Detail for a human message. Version is meaningful for every Status except StatusAbsent.

func Inspect

func Inspect(content string) Report

Inspect classifies content's managed region. It is pure and never errors: a malformed region is StatusMalformed with the problem described in Detail, not an error return, so callers can always display "here's what's going on" without a preceding error check.

func InspectFile

func InspectFile(path string) (Report, error)

InspectFile reads path and reports Inspect of its content. A missing file is StatusAbsent with a nil error, mirroring Inspect's own absent case for content that was never written. Any other read failure is a non-nil error naming the path.

func (Report) Blocking

func (r Report) Blocking() bool

Blocking reports whether r represents a structural conflict Plan refuses to proceed past: Malformed, Drifted, or NewerVersion. PlanRemove does not consult Blocking — removal needs only unambiguous marker lines, not a trusted body, so it treats Drifted and NewerVersion the same as Current and Stale (see PlanRemove).

type Status

type Status int

Status classifies a managed region relative to the engine's canonical content. It is a closed enumeration; inspect/classify are its only producers.

const (
	// StatusAbsent reports that content carries no managed region at
	// all: neither marker is present.
	StatusAbsent Status = iota
	// StatusCurrent reports a region at CurrentVersion whose body
	// matches the canonical render for that version (compared after
	// normalizing CRLF to LF within the region only, manifest.Parse's
	// idiom, so a checkout-time line-ending conversion is not a drift).
	StatusCurrent
	// StatusStale reports a valid region whose marker version is
	// older than CurrentVersion. Unreachable today (see block.go's
	// classify): CurrentVersion is 1 and the begin-marker grammar
	// accepts only version >= 1, so no real content can produce it
	// until a version 2 exists.
	StatusStale
	// StatusDrifted reports a region at CurrentVersion whose body
	// does not match the canonical render even after CRLF
	// normalization — a hand edit.
	StatusDrifted
	// StatusNewerVersion reports a region whose marker version is
	// newer than CurrentVersion: this build does not know that
	// version's canonical content and fails closed rather than guess.
	StatusNewerVersion
	// StatusMalformed reports a structural marker problem: a
	// duplicate, nested, misordered, or unpaired begin/end marker, or
	// a begin marker whose version overflows strconv.Atoi.
	StatusMalformed
)

Jump to

Keyboard shortcuts

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