guard

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 13 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 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) and the Delivery containment/branch/phase rules (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

View Source
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.

View Source
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

func PathsFromClaudeEvent(payload []byte) ([]string, error)

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

func PathsFromCodexEvent(payload []byte) ([]string, error)

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

func NewChecker(runner execx.Runner) *Checker

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

func (c *Checker) Check(ctx context.Context, req Request) (Verdict, error)

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.

type Request

type Request struct {
	Paths []string
	Role  manifest.Role
	Issue int
}

Request is one guard invocation: the write targets, plus the optional narrowing assertions an adapter may pass. Role and Issue are narrowing-only — they can only ever deny more, never grant. The empty Role asserts nothing and falls through to the pure containment rule.

type Verdict

type Verdict struct {
	Allow  bool
	Path   string
	Reason string
}

Verdict is guard's answer for a Request. Path names the target the verdict is about (the first denial for a multi-path Request); Reason is the one-line human explanation for a denial and is empty on allow.

Jump to

Keyboard shortcuts

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