Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/permission"

Package permission defines the permission modes and the effective policy a session runs under.

A Policy is plain data: the current mode, the modes a host has authorized a session to switch to, and an optional ceiling on the tools it may call. Policies only ever narrow. Narrow combines a parent’s policy with a narrower request, as a skill or a subagent definition makes one, and refuses anything that would widen authority.

Rules and protected paths configure the deterministic decision engine inside the reducer. Hosts do not build action requests or invoke rule evaluation. Shell matching is advisory; the launcher supplies process enforcement.

const (
ModePlan = engine.ModePlan
ModeDefault = engine.ModeDefault
ModeAcceptEdits = engine.ModeAcceptEdits
ModeAuto = engine.ModeAuto
ModeDontAsk = engine.ModeDontAsk
ModeBypass = engine.ModeBypass
)

Supported permission modes.

var (
// ErrUnknownMode reports an unknown permission mode.
ErrUnknownMode = engine.ErrUnknownMode
// ErrWiden reports a policy change that would widen authority.
ErrWiden = engine.ErrWiden
// ErrBadRule reports a malformed permission rule.
ErrBadRule = engine.ErrBadRule
)
func CheckProtected(pattern string) error

CheckProtected reports why pattern can’t be used as a protected path pattern (WithProtectedPaths), or nil. A pattern that doesn’t compile is never matched, so a host checks one it can’t afford to lose.

func ReviewedEdit(p string) bool

ReviewedEdit reports whether auto mode sends an edit of the workspace file p to its reviewer instead of allowing it: one that RunsLater, or one that configures the application.

func RunsLater(p string) bool

RunsLater reports whether a workspace file’s contents run or instruct whoever next builds, tests or opens the project, such as a Makefile, a CI workflow or a script. p is workspace-relative, with forward slashes.

type Mode = engine.Mode

Mode is a permission ceiling on unattended session actions.

type Policy struct {
// Mode is the current mode.
Mode Mode
// Modes are the modes the host authorizes switches to, including
// Mode. Nil authorizes only Mode.
Modes []Mode
// Tools is the ceiling on the tools the session may call, by name.
// Nil allows every tool; an empty, non-nil slice allows none.
Tools []string
// Rules are the deny, human-only, ask and allow rules.
Rules Rules
// Protected extends the built-in protected paths: globs relative to
// the workspace, or base-name globs (with no slash) matched anywhere.
Protected []string
// NoSkillGrants says no skill's allowed-tools pre-approve calls.
NoSkillGrants bool
}

Policy is the host’s permission configuration. Its zero value is invalid. Rule evaluation belongs to the reducer; hosts configure and narrow policy.

func (p Policy) AllowsRead(path string) bool

AllowsRead reports whether path, relative to the workspace, may be inlined by a host without approval. Protected paths are always withheld, even though the Read tool itself may read them; hooks still run on actual tool calls.

Example

policy := permission.Policy{Mode: permission.ModePlan, Rules: permission.Rules{Deny: []permission.Rule{permission.MustRule("Read(private/**)")}}}
fmt.Println(policy.AllowsRead("main.go"))
fmt.Println(policy.AllowsRead("private/key.txt"))
fmt.Println(policy.AllowsRead(".zelk/config.json"))
// Output:
// true
// false
// false

Output:

true
false
false
func (p Policy) AllowsTool(name string) bool

AllowsTool reports whether the tool is within the policy’s ceiling.

func (p Policy) Amend(rules ...Rule) Policy

Amend returns p with additional session allow rules.

func (p Policy) Authorized() []Mode

Authorized returns the modes the session may select, ordered by authority.

func (p Policy) Narrow(child Policy) (Policy, error)

Narrow combines p with a child policy, refusing any widening of authority.

Example

session := permission.Policy{
Mode: permission.ModeBypass,
Modes: []permission.Mode{permission.ModePlan, permission.ModeBypass},
}
// An exploring subagent asks for plan mode and two tools.
child, err := session.Narrow(permission.Policy{Mode: permission.ModePlan, Tools: []string{"Read", "Grep"}})
if err != nil {
panic(err)
}
fmt.Println(child.Mode, child.Modes, child.Tools)
// It can never get back what its parent didn't give it.
_, err = child.Narrow(permission.Policy{Mode: permission.ModeBypass})
fmt.Println(err)
// Output:
// plan [plan] [Grep Read]
// policy can only narrow: mode "bypass" is wider than "plan"

Output:

plan [plan] [Grep Read]
policy can only narrow: mode "bypass" is wider than "plan"
func (p Policy) Validate() error

Validate reports whether the policy can be enforced.

type Rule = engine.Rule

Rule is a parsed tool, command, path or URL permission rule.

func MustRule(text string) Rule

MustRule parses text and panics if it is invalid, for static host configuration.

func ParseRule(text string) (Rule, error)

ParseRule parses a tool name or a tool pattern with an argument constraint.

type Rules = engine.Rules

Rules are the deny, human-only, ask and allow rule sets.