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 ¶
- Variables
- func DenyGlobs(l Level, project []string) []string
- func IsChildCommand(cmd, sub string) bool
- func Match(g, rel string) bool
- type Decision
- type Level
- type Policy
- func (p Policy) CheckCommand(cmd string) Decision
- func (p Policy) CheckPatch(patch string) Decision
- func (p Policy) CheckRead(target string) Decision
- func (p Policy) CheckToolArgs(tool string, input []byte) Decision
- func (p Policy) CheckWrite(target string) Decision
- func (p Policy) Inside(dir string) bool
Constants ¶
This section is empty.
Variables ¶
Levels lists the valid level names; DefaultLevel is a project's level when it sets none.
var DefaultDeny = []string{".env*"}
DefaultDeny are the deny globs a light run adds to the project's: secrets.
Functions ¶
func DenyGlobs ¶
DenyGlobs returns the deny globs of a run at level l: the project's, plus DefaultDeny for a light run.
func IsChildCommand ¶
IsChildCommand reports whether `otag <cmd> [<sub>]` is open to a child (the CLI enforces the same list itself, for commands no shell line shows).
Types ¶
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.
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 ¶
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 ¶
CheckPatch inspects all apply_patch destinations, including indented headers embedded in shell heredocs. Shell callers check it before tokenization.
func (Policy) CheckRead ¶
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 ¶
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 ¶
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.