policy

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 16 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

Decides whether a tool call may run, asking a person when the rules do not settle it

Index

Constants

View Source
const (
	KindPlain   = "plain"
	KindCommand = "command"
	KindPath    = "path"
	KindDomain  = "domain"
)

Kinds of subject a pattern is matched against

Variables

View Source
var DefaultEdits = []string{"Write", "Edit"}

Tools AcceptEdits lets through without asking

View Source
var DefaultSubjects = map[string]Extractor{
	"Shell":       Shell("command"),
	"ShellOutput": Strings("job_id"),
	"KillShell":   Strings("job_id"),
	"Read":        Paths("file_path"),
	"Write":       Paths("file_path"),
	"Edit":        Paths("file_path"),
	"Glob":        Paths("path"),
	"Grep":        Paths("path"),
	"WebFetch":    URLs("url"),
}

Extractors for the tools icebath ships, every other tool matched over its string arguments

Functions

func AgentFrom

func AgentFrom(ctx context.Context) string

Agent name carried by the context, empty when none was set

func ReadOnlyOf

func ReadOnlyOf(r *tool.Registry) func(string) bool

Reports read only tools out of a registry by their annotations

func WithAgent

func WithAgent(ctx context.Context, name string) context.Context

Carries the name of the agent whose calls are being gated

Types

type Answer

type Answer struct {
	Allowed bool
	// Who decided, person or memory
	By string
	// What the person chose when a person decided
	Verdict Verdict
	Reason  string
}

Outcome of an approval with who settled it

type Approver

type Approver interface {
	Approve(ctx context.Context, req Request) (Answer, error)
}

Answers the Ask decision, usually by prompting in the UI

type Asker

type Asker interface {
	Ask(ctx context.Context, req Request) (Verdict, error)
}

Prompts a person and reports what they chose, including whether to remember it

type Bypasser

type Bypasser interface {
	Bypassing() bool
}

Policy that can be switched off, every call then running with nothing consulted

type Decision

type Decision string
const (
	Allow Decision = "allow"
	Deny  Decision = "deny"
	Ask   Decision = "ask"
)

func Explain

func Explain(p Policy, call protocol.ToolCall) (Decision, string)

Decision of p with its reason, which is the word policy for policies that cannot explain

func Stricter

func Stricter(a, b Decision) Decision

Picks the stricter of two decisions, an empty one losing to anything

type Denied

type Denied struct {
	Call   protocol.ToolCall
	Reason string
}

Call that was not allowed to run

func (*Denied) Error

func (d *Denied) Error() string

type Explainer

type Explainer interface {
	Explain(call protocol.ToolCall) (Decision, string)
}

Policy that can name the rule or layer behind its decision

type Extractor

type Extractor func(args json.RawMessage) []Subject

Draws subjects from the arguments of one tool

func Paths

func Paths(names ...string) Extractor

Subjects for path arguments, a missing path meaning the working directory

func Shell

func Shell(name string) Extractor

Subjects for a shell command argument, one per simple command plus the whole line

func Strings

func Strings(names ...string) Extractor

Plain subjects for the named string arguments

func URLs

func URLs(name string) Extractor

Subject for a URL argument matched by domain or by the whole URL

type Gate

type Gate struct {
	Policy   Policy
	Approver Approver
	// Describes tools on approval requests when set
	Tools Lookup
	// Called with every request handed to the approver
	OnAsk func(Request)
	// Called with the answer or error the approver gave
	OnAnswer func(Request, Answer, error)
}

Policy and approver together, asking for everything without a policy and denying every ask without an approver

func (Gate) Resolve

func (g Gate) Resolve(ctx context.Context, call protocol.ToolCall, settled Decision) error

Checks the call with a decision settled ahead of the policy, replacing its allow or ask but never its deny, nothing checked while the policy is bypassed

type Guard

type Guard struct {
	// Consulted first, asking for everything when nil
	Base Policy
	// Registry whose annotations are read, every tool counting as destructive and open world when nil
	Tools *tool.Registry
}

Policy raising an allow the base reached by default to ask for tools annotated destructive or open world

func (Guard) Decide

func (g Guard) Decide(call protocol.ToolCall) Decision

func (Guard) Explain

func (g Guard) Explain(call protocol.ToolCall) (Decision, string)

Decision with the guard reason when it raised a default allow to ask

type Keyer

type Keyer interface {
	Key(call protocol.ToolCall) string
}

Policy that can name the rule an always answer should remember for a call

type Lookup

type Lookup interface {
	Lookup(name string) (tool.Tool, bool)
}

Registry view a gate uses to describe the tool behind a call

type Memory

type Memory struct {
	Ask Asker
	// Groups calls that share one remembered answer, the request key or tool name when nil
	Key func(call protocol.ToolCall) string
	// contains filtered or unexported fields
}

Approver that remembers always and never answers under a key derived from the call

func (*Memory) Approve

func (m *Memory) Approve(ctx context.Context, req Request) (Answer, error)

Answers from memory when it can and asks otherwise, learning always and never

func (*Memory) Remember

func (m *Memory) Remember(key string, allow bool)

Stores an answer for a key ahead of any prompt

type Mode

type Mode string
const (
	// Base policy decides everything
	ModeDefault Mode = "default"
	// Read only tools follow the base policy and every other tool is denied
	ModePlan Mode = "plan"
	// File editing tools run without asking unless the base policy denies them
	ModeAcceptEdits Mode = "acceptEdits"
	// Every call runs with no rule, guard, memory or hook permission consulted
	ModeBypass Mode = "bypassPermissions"
)

func Modes

func Modes() []Mode

Every mode in the order a user cycles through them

func ParseMode

func ParseMode(s string) (Mode, error)

Parses a mode name

func (Mode) Label

func (m Mode) Label() string

Short label for display

func (Mode) Next

func (m Mode) Next() Mode

Mode after m in the cycle order

type ModeSwitch

type ModeSwitch struct {

	// Consulted when the mode defers, asking for everything when nil
	Base Policy
	// Reports whether a tool only reads
	ReadOnly func(name string) bool
	// contains filtered or unexported fields
}

Policy whose mode may be changed while runs are in flight

func Compose

func Compose(start Mode, rules Policy, tools *tool.Registry) *ModeSwitch

Standard stack, a switchable mode over a guard over the rules, with the guard respecting explicit rules

func (*ModeSwitch) Bypassing

func (s *ModeSwitch) Bypassing() bool

Reports whether every call runs with nothing consulted

func (*ModeSwitch) Decide

func (s *ModeSwitch) Decide(call protocol.ToolCall) Decision

func (*ModeSwitch) Explain

func (s *ModeSwitch) Explain(call protocol.ToolCall) (Decision, string)

func (*ModeSwitch) Mode

func (s *ModeSwitch) Mode() Mode

Current mode, Default when none was set

func (*ModeSwitch) Set

func (s *ModeSwitch) Set(m Mode)

Changes the mode for every later decision

type Policy

type Policy interface {
	Decide(call protocol.ToolCall) Decision
}

Maps a tool call to a decision without side effects

var (
	AllowAll Policy = Static(Allow)
	DenyAll  Policy = Static(Deny)
	AskAll   Policy = Static(Ask)
)

type Request

type Request struct {
	Call protocol.ToolCall
	// Agent that wants to run the call
	Agent string
	// Why the gate is asking rather than deciding
	Reason string
	// Rule text an always or never answer will remember
	Key      string
	Subjects []Subject
	// Definition of the tool when the gate knows its registry
	Tool  *protocol.Tool
	Hints tool.Annotations
}

Everything a person needs to decide one call

type Rule

type Rule struct {
	Tool       string
	Pattern    string
	HasPattern bool
}

Rule text split into its tool glob and optional pattern

func ParseRule

func ParseRule(s string) (Rule, error)

Parses Tool or Tool(pattern)

func (Rule) String

func (r Rule) String() string

type RuleSet

type RuleSet struct {
	Allow []string `json:"allow,omitempty"`
	Deny  []string `json:"deny,omitempty"`
	Ask   []string `json:"ask,omitempty"`
}

Lists of rules by decision, the shape of a permissions block

func LoadRules

func LoadRules(r io.Reader) (RuleSet, error)

Reads a permissions block from JSON, nested under permissions or at the top level

func LoadRulesFile

func LoadRulesFile(path string) (RuleSet, error)

func (RuleSet) Save

func (s RuleSet) Save(path string) error

Writes a permissions block nested under permissions as settings files expect

type Rules

type Rules struct {
	// Answer when no rule settles a call, Ask when empty
	Default Decision
	// Base of relative path patterns and paths, the process working directory when empty
	Dir string
	// Current directory consulted ahead of Dir on every match so a moving shell is followed
	DirFunc func() string
	// contains filtered or unexported fields
}

Policy answering deny for any denied subject, ask for any asked one and allow when every subject is allowed

func NewRules

func NewRules(sets ...RuleSet) (*Rules, error)

Compiles rule sets in order, failing on the first malformed rule

func (*Rules) Decide

func (p *Rules) Decide(call protocol.ToolCall) Decision

func (*Rules) Explain

func (p *Rules) Explain(call protocol.ToolCall) (Decision, string)

Decision with the rule text that settled it or the word default

func (*Rules) Key

func (p *Rules) Key(call protocol.ToolCall) string

Rule text that would allow exactly this call again, for remembering answers

func (*Rules) Reset

func (p *Rules) Reset(sets ...RuleSet) error

Swaps every rule for the sets compiled in order, keeping the old rules when one is malformed

func (*Rules) Rules

func (p *Rules) Rules(decision Decision) []string

Rules recorded under a decision in the order added

func (*Rules) Set

func (p *Rules) Set() RuleSet

Every rule as a set ready to save

func (*Rules) SubjectsOf

func (p *Rules) SubjectsOf(call protocol.ToolCall) []Subject

Subjects the policy would match for a call

type Static

type Static Decision

Policy that answers the same decision for every call

func (Static) Decide

func (s Static) Decide(protocol.ToolCall) Decision

type Subject

type Subject struct {
	// What the text is, such as command, path, domain or plain
	Kind string
	Text string
	// Runs commands the line does not spell out, through substitution, eval or source, so no allow pattern can vouch for it
	Unsafe bool
	// Whole command line that only a wildcard free pattern may match
	Exact bool
}

One thing a call operates on, drawn from its arguments

func AllStrings

func AllStrings(args json.RawMessage) []Subject

Plain subjects for every top level string argument in key order

func SplitCommand

func SplitCommand(line string) []Subject

Subjects for the simple commands of a line as the shell parses it, one unsafe subject for a line that does not parse

type Subjecter

type Subjecter interface {
	SubjectsOf(call protocol.ToolCall) []Subject
}

Policy that can list what a call operates on

type Verdict

type Verdict int

Answer a person gives to one approval prompt

const (
	No Verdict = iota
	Yes
	Never
	Always
)

func (Verdict) Allowed

func (v Verdict) Allowed() bool

func (Verdict) String

func (v Verdict) String() string

Jump to

Keyboard shortcuts

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