guard

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 11 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 decides whether an agent's write or shell command is allowed: writes must stay inside the workspace and off the project's deny globs, and commands must not push or write outside it. Adapters call it from ParseEvent (PreToolUse and the like) and turn a denial into the harness's own decision output.

It is a best-effort filter over what the harness reports, not a sandbox: shell constructs it cannot see into ($(...), eval, scripts) are inspected only for an obvious git push or ref change. The write boundary is the harness's native sandbox where it has one (docs/ADAPTERS.md, "Threat model"); adapters also put hard denies in the harness config.

Index

Constants

This section is empty.

Variables

View Source
var (
	Levels       = []string{string(Light), string(Strict)}
	DefaultLevel = Light
)

Levels lists the valid level names; DefaultLevel is a project's level when it sets none.

View Source
var DefaultDeny = []string{".env*"}

DefaultDeny are the deny globs a light run adds to the project's: secrets.

Functions

func DenyGlobs

func DenyGlobs(l Level, project []string) []string

DenyGlobs returns the deny globs of a run at level l: the project's, plus DefaultDeny for a light run.

func IsChildCommand

func IsChildCommand(cmd, sub string) bool

IsChildCommand reports whether `otag <cmd> [<sub>]` is open to a child (the CLI enforces the same list itself, for commands no shell line shows).

func Match

func Match(g, rel string) bool

Match reports whether the slash-separated relative path rel matches the gitignore-style glob g (see Policy.Deny).

Types

type Decision

type Decision struct {
	Allow  bool
	Reason string // why it was denied, for the agent
}

Decision is the outcome of a check.

type Level

type Level string

Level is how much the guard enforces, the project setting `guard` (docs/ADAPTERS.md, "Threat model"). The run records the level it started with.

  • Light (the project default) keeps a short list of rules and allows everything else: no git push, no git change to other refs, other worktrees or the main checkout, no file-tool write and no `rm -r` outside the workspace (temporary directories excepted), no read or write of deny-glob files. A command the guard cannot inspect is allowed. Harnesses run without their native sandbox.
  • Strict is the full guard behind the harness's native sandbox.

The zero Level is strict: runs from before levels, and policies built by hand.

const (
	Light  Level = "light"
	Strict Level = "strict"
)

Levels.

func (Level) IsLight

func (l Level) IsLight() bool

IsLight reports whether l is the light level ("" is strict).

func (Level) String

func (l Level) String() string

String returns the level name, "strict" for the zero Level.

type Policy

type Policy struct {
	Workdir string // writes must stay inside (symlinks resolved)
	// Deny are gitignore-style globs relative to Workdir that must not be written:
	// without "/" they match a name at any depth (".env*", "*.pem"); with "/" they are
	// anchored ("infra/prod/**"); "**" spans directories; a match on a directory covers
	// everything below it.
	Deny []string
	// AllowOutside are absolute paths (files or directories) writable outside Workdir.
	// /dev/null, /dev/stdout and /dev/stderr are always allowed.
	AllowOutside []string
	// DenyCommands are extra patterns matched against each simple command, its words
	// joined by single spaces; "*" matches any run of characters, "?" one character.
	DenyCommands []string
	// Cwd is where relative paths resolve: the shell's current directory as the harness
	// reports it (Claude's payload cwd; OpenCode's shell workdir, which may be relative to
	// Workdir). "" means Workdir. It never moves the boundary:
	// writes must still stay inside Workdir. CheckCommand follows `cd` within a line.
	Cwd string
	// Branch is the run's own branch; "" in same mode or a detached review worktree (then
	// no branch may be moved). Only it may be
	// force-moved or deleted (`git branch -f/-D/-M`, `checkout -B`).
	Branch string
	// Detached: Workdir is a detached review checkout (Branch ""): at both levels HEAD
	// must stay detached and no ref may be created, moved or deleted (detached.go).
	Detached bool
	// Level selects the rule set (level.go); "" is strict.
	Level Level

	// Child protections, set for child runs: otag's own state is not the agent's to
	// touch. DB is the database (reads, writes and sqlite3 on it and its -wal/-shm are
	// refused), TmuxSocket otag's tmux socket name (tmux on it, or plain tmux inside a
	// otag pane, is refused), OtagBin the otag binary (only child commands may run).
	DB         string
	TmuxSocket string
	OtagBin    string
	// contains filtered or unexported fields
}

Policy is the guard of one run.

func (Policy) CheckCommand

func (p Policy) CheckCommand(cmd string) Decision

CheckCommand decides whether a shell command line may run: no git push, no write (redirection, tee, rm, mv, cp, touch, ...) outside the workspace or on a deny glob, no DenyCommands pattern. `sh -c`/`bash -c` scripts are checked recursively; a shell fed its commands through a pipe or input redirect is refused. A light run refuses only git pushes and ref/worktree changes, `rm -r` outside the workspace and writes of deny-glob files; what it cannot inspect it allows.

func (Policy) CheckPatch

func (p Policy) CheckPatch(patch string) Decision

CheckPatch inspects all apply_patch destinations, including indented headers embedded in shell heredocs. Shell callers check it before tokenization.

func (Policy) CheckRead

func (p Policy) CheckRead(target string) Decision

CheckRead decides whether the agent may read target: files on the project deny list (secrets such as .env*) and otag's database are refused; anything else is allowed.

func (Policy) CheckToolArgs

func (p Policy) CheckToolArgs(tool string, input []byte) Decision

CheckToolArgs checks a tool otag does not know (an MCP server's, say; MCP servers run outside the harness's sandbox) by its name and input, which must be a JSON object:

  • every path-like argument (path, file_path, destination, ...) must pass CheckWrite, since otag cannot tell whether the tool writes;
  • a command-like argument (command, cmd, script, shell) must be a string (or a list of strings, an argv) and pass CheckCommand;
  • a tool whose name says it runs commands but whose input has no command-like string to check is refused.

A light run has no executor heuristics: input it cannot read is allowed, a string command is checked like a shell line, and path arguments are checked as writes only for a tool named like a writer (writeTool), otherwise as reads (deny-glob files).

func (Policy) CheckWrite

func (p Policy) CheckWrite(target string) Decision

CheckWrite decides whether the agent may create, modify or delete p (absolute, or relative to Workdir). Symlinks are followed, dangling ones included; a path whose resolution is ambiguous (a loop, an unreadable link) is refused. A file with other hard links is refused too: one of its other names may be outside or protected.

func (Policy) Inside

func (p Policy) Inside(dir string) bool

Inside reports whether dir (absolute, or relative to Workdir), symlinks resolved, is Workdir or below it.

Jump to

Keyboard shortcuts

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