# permission

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

<!-- Generated from the Go source of github.com/barevoidco/zelk/core/permission (cmd/zelk/internal/reference TestReferenceGoAPI). Don't edit: change the code, then run make reference. -->

```go
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.


## Constants

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

Supported permission modes.


## Variables

```go
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
)
```


## CheckProtected

```go
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.


## ReviewedEdit

```go
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.


## RunsLater

```go
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.


## Mode

```go
type Mode = engine.Mode
```

Mode is a permission ceiling on unattended session actions.


## Policy

```go
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.


### Policy.AllowsRead

```go
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**

```go
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:

```text
true
false
false
```


### Policy.AllowsTool

```go
func (p Policy) AllowsTool(name string) bool
```

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


### Policy.Amend

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

Amend returns p with additional session allow rules.


### Policy.Authorized

```go
func (p Policy) Authorized() []Mode
```

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


### Policy.Narrow

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

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

**Example**

```go
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:

```text
plan [plan] [Grep Read]
policy can only narrow: mode "bypass" is wider than "plan"
```


### Policy.Validate

```go
func (p Policy) Validate() error
```

Validate reports whether the policy can be enforced.


## Rule

```go
type Rule = engine.Rule
```

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


### MustRule

```go
func MustRule(text string) Rule
```

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


### ParseRule

```go
func ParseRule(text string) (Rule, error)
```

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


## Rules

```go
type Rules = engine.Rules
```

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