Documentation
¶
Overview ¶
Package guard is the mechanical write-enforcement core behind the host-adapter PreToolUse hooks (PRD §23: "Assist reliably blocks tracked-file changes" and "Delivery cannot write outside registered worktrees"). Adapters shell out to `orch guard` before every agent file write; guard answers allow/deny and fails closed on any ambiguity — an unreadable state or an unverifiable path is a deny, never a permissive default.
All decision logic lives here in the Go core, not in adapter shell (PRD §6: adapters stay thin). The package is policy-mechanical: it enforces the Assist read-only rule (PRD §7), the Delivery containment/branch/phase rules and role read-only-ness (PRD §15), and treats orchestrator internals and git internals as never writable.
The verdict is split into a pure decision (evaluate, over a resolved facts value, no I/O — the normative decision table as data) and the I/O that resolves those facts (resolve.go). Rows the table classifies as "cannot verify" or "propagate message" surface as a Go error from Check, so the CLI can map them to the hook protocol's blocking exit; every genuine allow/deny decision is a Verdict.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPatchEnvelope = errors.New("malformed apply_patch envelope")
ErrPatchEnvelope reports an apply_patch tool_input whose command envelope guard cannot parse into at least one write target: missing "*** Begin Patch"/"*** End Patch" framing, a directive line guard does not recognize, or a directive with an empty path. Like ErrUnknownTool, it is a deny (fail closed), not an internal failure: an upstream envelope format guard's parser does not yet understand must degrade to a denial the agent can report, never a silent allow.
var ErrUnknownTool = errors.New("unrecognized tool_name")
ErrUnknownTool reports a PreToolUse event whose tool_name guard does not extract a write target from. It is a deny (fail closed; adapters scope their hook matchers), not an internal failure — callers test for it with errors.Is and answer the hook with a denial rather than the blocking error exit.
Functions ¶
func ClaudeTools ¶
func ClaudeTools() []string
ClaudeTools returns, sorted, the Claude Code tool_name values guard extracts a write target from. Any other tool_name is ErrUnknownTool (deny by default). It exists so the Claude adapter's plugin_test.go can pin the hooks.json PreToolUse matcher against the exact dispatch set by import instead of duplicating the tool list.
func CodexTools ¶
func CodexTools() []string
CodexTools returns, sorted, the Codex CLI tool_name values guard extracts a write target from. It exists so the Codex adapter's hooks.json PreToolUse matcher can be pinned against the exact dispatch set by import instead of duplicating the tool list — the same purpose ClaudeTools serves for the Claude adapter.
func PathsFromClaudeEvent ¶
PathsFromClaudeEvent decodes a raw PreToolUse event and returns the absolute write targets it declares. Targets are taken by tool_name: Write/Edit/MultiEdit from tool_input.file_path, NotebookEdit from tool_input.notebook_path. A relative target is resolved against the payload's cwd; a relative target with no cwd is an internal failure. An unrecognized tool_name returns an error wrapping ErrUnknownTool so the caller can deny by default.
func PathsFromCodexEvent ¶
PathsFromCodexEvent decodes a raw PreToolUse event and returns the absolute write targets it declares. Only tool_name "apply_patch" carries a write target: its tool_input.command is the raw patch envelope string, parsed by parseApplyPatchPaths. A relative target is resolved against the payload's cwd; a relative target with no cwd is an internal failure. An unrecognized tool_name — including "Bash" and any "mcp__*" tool — returns an error wrapping ErrUnknownTool so the caller can deny by default; inspecting Bash commands for write targets is deliberately out of scope here (task 27).
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker resolves the facts behind a guard Verdict. Its function fields are injected so tests drive the decision without a real filesystem or git; NewChecker wires the production implementations.
func NewChecker ¶
NewChecker returns a Checker wired to the production path, state, lock, and git implementations. The ignore probe runs through runner, the only place guard shells out (and only on the rare Assist in-repo write); every other row is resolved from state, the lock, and a small number of file reads and lstats.
func (*Checker) Check ¶
Check evaluates req and returns the verdict for the first path that is not allowed, or an allow when every path passes. Multiple paths are all-or-nothing: the first denial denies the whole invocation. A non-nil error reports an operational failure the caller must treat as "cannot decide" (decision-table rows 1, 2, and 5): an unverifiable path, an unwalkable root, or unreadable/inconsistent state. Those are distinct from a policy denial, which is a Verdict with Allow false.