permission

package
v0.34.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package permission mediates tool execution approvals between the agent loop and the frontend. Three modes: yolo (auto-approve all), ask (auto-approve reads, confirm writes), auto (AI decides, with user-provided rules).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAllowPattern

func GenerateAllowPattern(toolName string, args map[string]any) string

GenerateAllowPattern creates an allow pattern for the "always allow" shortcut. For bash, generates "Bash(firstWord:*)" from the command. For other tools, generates just the tool name.

func IsDangerousCommand

func IsDangerousCommand(cmd string) bool

IsDangerousCommand reports whether cmd downloads and executes remote code. See dangerousPatterns for the exact shapes and the heuristic-not-sandbox caveat.

func PopApprovedFeedback

func PopApprovedFeedback(args map[string]any) string

PopApprovedFeedback removes and returns a permission feedback note (if any) injected by askUser on approved decisions.

Types

type Config

type Config struct {
	Allow     []string   // glob patterns: "Bash(npm:*)", "edit"
	Deny      []string   // glob patterns always denied
	Rules     []string   // natural language rules for auto mode
	Evaluator *Evaluator // AI evaluator (nil in ask mode)
	Headless  bool       // Deny instead of blocking when user approval needed (headless/CLI mode).
}

Config holds the gate's initial settings from merged config files.

type Decision

type Decision int

Decision is the AI evaluator's verdict.

const (
	DecisionApprove Decision = iota
	DecisionDeny
	DecisionAsk // escalate to user
)

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator uses a lightweight LLM to decide whether a tool call is safe.

func NewEvaluator

func NewEvaluator(provider core.Provider, model core.Model) *Evaluator

NewEvaluator creates an evaluator with the given provider and model.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(ctx context.Context, toolName string, args map[string]any, rules []string) Decision

Evaluate asks the LLM whether the tool call should be approved, denied, or escalated to the user. Rules are natural language instructions that guide the decision.

type Gate

type Gate struct {
	// contains filtered or unexported fields
}

Gate mediates tool permissions. Created once, shared between agent and frontend.

func New

func New(mode Mode, cfg Config) *Gate

New creates a Gate with the given mode and config.

func (*Gate) AddAllow

func (g *Gate) AddAllow(pattern string)

AddAllow appends a glob allow pattern (for ask mode "always allow").

func (*Gate) AddRule

func (g *Gate) AddRule(rule string)

AddRule appends a natural language rule (for auto mode).

func (*Gate) AllowPatterns

func (g *Gate) AllowPatterns() []string

Allow returns the current allow patterns.

func (*Gate) Check

func (g *Gate) Check(ctx context.Context, name string, args map[string]any) *core.ToolCallDecision

Check decides whether a tool call may proceed. May block waiting for user approval. Returns nil to approve, or a blocking ToolCallDecision to reject. Called from the agent loop goroutine.

ask mode: deny globs → readOnly → allow globs → ask user auto mode: deny globs → readOnly → AI evaluator (rules) → ask user (fallback)

func (*Gate) Mode

func (g *Gate) Mode() Mode

Mode returns the active permission mode.

func (*Gate) Requests

func (g *Gate) Requests() <-chan Request

Requests returns the channel the UI listens on for approval requests.

func (*Gate) Rules

func (g *Gate) Rules() []string

Rules returns the current rule set (for AI evaluator).

func (*Gate) SetMode

func (g *Gate) SetMode(mode Mode)

SetMode changes the permission mode at runtime.

func (*Gate) SnapshotConfig

func (g *Gate) SnapshotConfig() Config

SnapshotConfig returns a Config snapshot of the current gate state. Useful for preserving config when reconstructing a gate after yolo mode.

type Mode

type Mode string

Mode controls how tool permissions are evaluated.

const (
	ModeYolo Mode = "yolo" // Everything auto-approved
	ModeAsk  Mode = "ask"  // Read tools auto-approved, writes require confirmation
	ModeAuto Mode = "auto" // AI evaluator decides; falls back to ask if no evaluator
)

type Request

type Request struct {
	ToolName string
	Args     map[string]any
	Response chan<- Response
}

Request is sent to the UI when user approval is needed. The receiver must send exactly one value on Response.

type Response

type Response struct {
	Approved bool
	Feedback string // optional: denial reason or approval note
	Allow    string // non-empty: add this glob pattern to the allow list
}

Response carries the user's decision back to the agent loop.

Jump to

Keyboard shortcuts

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