gate

package module
v0.0.0-...-790ab80 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 8 Imported by: ? 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

Package gate adds human-approved, remembered permission to agentkit tools. It is a thin layer on the policy-blind core: a tool action is checked against a Policy (allow / ask / deny); an "ask" first consults remembered Grants and, if none covers it, prompts a human out-of-band via an Approver; the answer is remembered — for this session, or always.

Two things are separate on purpose:

  • WHICH tools an agent has at all is agentkit's ToolSet (Select) — a static bound set once.
  • WHAT a tool may DO (this package) — checked per action, asked when risky, remembered.

The machinery is installed into ctx with With and flows to nested tool calls and sub-agents automatically, so one install covers the whole tree. There is no separate host concept: a host allowlist is just Grants whose Target is the host (use a shared Tool name like "net" so one grant covers every network tool).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDeniedPolicy — the policy returned Deny for this action.
	ErrDeniedPolicy = fmt.Errorf("policy deny: %w", ErrDenied)
	// ErrDeniedUnattended — an Ask with no covering grant and no approver (a fired/background agent
	// has no human to approve), so it fails closed.
	ErrDeniedUnattended = fmt.Errorf("unattended, no approver: %w", ErrDenied)
	// ErrDeniedDeclined — a human was asked and declined.
	ErrDeniedDeclined = fmt.Errorf("declined by human: %w", ErrDenied)
	// ErrDeniedUnconfigured — Check ran on a ctx with no permission machinery (or machinery without
	// a Policy). That is a wiring mistake, so it fails closed: a forgotten install must never mean
	// "everything is allowed". Run ungated on purpose with Open.
	ErrDeniedUnconfigured = fmt.Errorf("no permission machinery installed: %w", ErrDenied)
)

Reason-specific denials, each wrapping ErrDenied:

View Source
var ErrDenied = errors.New("gate: denied")

ErrDenied is the umbrella "not permitted" error: every deny below wraps it, so errors.Is(err, ErrDenied) still matches any denial. The reason-specific sentinels let a caller branch on WHY — e.g. an unattended deny might be retried attended, a policy deny never should. The error is surfaced to the model as the tool result, so the model adjusts instead of the turn crashing.

Functions

func Check

func Check(ctx context.Context, a Action) error

Check gates one action against the installed machinery:

Policy Allow -> nil
Policy Deny  -> ErrDenied
Policy Ask   -> a covering Grant passes; else the Approver is asked out-of-band (the turn's
                wall-clock is paused via agentkit.Pause while the human decides) and the answer is
                remembered at the more restrictive of the policy's Recall and the human's choice.

No machinery in ctx, or machinery without a Policy, = ErrDeniedUnconfigured: gating fails closed, and running ungated is the explicit choice Open makes. A tool calls Check before a targeted action (Action{Kind: "net", Target: host}); the name check every ToolSet.Call meets is Check of the tool's name. What a target pattern means, and which wider grants a human may pick instead, are the policy's: it knows the targets, and offers the widenings with its Ask (AskWith). An empty a.Tool is the calling tool's name from the ctx.

func IsReadOnly

func IsReadOnly(t agentkit.Tool) bool

IsReadOnly reports whether t declares that it only reads.

func Open

func Open(ctx context.Context) context.Context

Open installs machinery that allows every action: the explicit way to run ungated, for tests, examples and consumers that do their own gating. Without it (or With), Check denies.

func With

func With(ctx context.Context, p Policy, g Grants, a Approver) context.Context

With installs the permission machinery into ctx: a Policy, a Grants store and an Approver (nil = unattended). The Policy is required; a nil one denies every action (ErrDeniedUnconfigured). It flows to nested tool calls and sub-agents through ctx, so one install covers the whole tree; a nested run inherits it and cannot widen it (only a human, via the Approver, widens).

It also installs the name check as agentkit's CallGuard: every ToolSet.Call under ctx is gated on the tool's name (Action{Kind: name}) before the tool runs, whichever set the call came from. A tool that reaches a runtime target (a host, a path) calls Check itself with that Target as well.

func WithLogger

func WithLogger(ctx context.Context, log agentkit.Logger) context.Context

WithLogger installs the logger that Check traces every decision through — kept separate from With so the permission signature stays stable. A nil logger (the default when unset) traces nothing.

func WithReadOnly

func WithReadOnly(t agentkit.Tool) agentkit.Tool

WithReadOnly is t declaring that it only reads (ReadOnly).

func WithTargets

func WithTargets(t agentkit.Tool, args ...string) agentkit.Tool

WithTargets is t declaring that the Target it checks is at args in its arguments.

func Wrap

func Wrap(t agentkit.Tool) agentkit.Tool

Wrap decorates a tool so every call is gated on its name (Action{Kind: name}) before it runs.

A call through agentkit.ToolSet.Call needs no Wrap: With installs the same name check as the ctx's CallGuard. Wrap is for a consumer that calls a Tool directly, outside a ToolSet. A tool that reaches a runtime target — a host, a path — calls Check itself with that Target as well.

Types

type Action

type Action struct {
	Tool   string
	Kind   string
	Target string
}

Action is the thing being gated: the Tool whose call it is, a Kind — the tool's own name, or a shared axis such as "net" for a host allowlist — and, when the action reaches one, the runtime Target (a host, a path). Target is "" for a Kind with no target.

Tool is filled in by Check from the ctx (agentkit.CallingTool) when the call site leaves it empty, so a tool checking its own target never has to name itself, and a policy can tell which tool asks even where two tools share one Kind. Outside any tool call it stays "".

type Answer

type Answer struct {
	Name   string
	Grant  Grant
	Recall Recall
	Widens bool
}

Answer is one answer a human may give to an ask: the grant it remembers, for how long, and whether it reaches further than the action asked. Name identifies it — "once", "session", "always" on the action's exact target, "widen0", "widen1" … for the suggestions in order — so a surface can label it and a remote client can echo it back.

func Answers

func Answers(a Action, ceiling Recall, suggest []Grant) []Answer

Answers is every answer an Approver may offer for a, in presentation order: allow once (remembering nothing), for this session, always — each on the action's exact target — then the suggested widenings, each wanting "always". Every answer has passed Offerable, so none is shown that the gate would narrow afterwards. A ceiling of RecallNever leaves exactly "once", the truth about a Kind that is asked every time. Every surface (the phone's sheet, the terminal's modal, a command line) builds its choices from this one list, so the same action reads the same way wherever it is answered.

type Approver

type Approver interface {
	Ask(ctx context.Context, a Action, ceiling Recall, suggest []Grant) (approved bool, remember Grant, recall Recall, err error)
}

Approver presents an action — plus a set of SUGGESTED grants the policy proposes as sensible widenings (e.g. "*.github.com" for a host, or a parent directory for a path) — to a human out-of-band. It returns whether the human approved, the Grant to remember if so (the exact action, a suggestion, or whatever the human chose), and how long it holds. When approved is false the Grant and Scope are ignored. A nil Approver means unattended: an action that would be asked is denied (fail-closed). The suggestions are target-semantic and come from the policy (AskWith), not from this library or the approver. Implementations block until the human answers or ctx is cancelled; the caller pauses the turn's wall-clock around the wait.

ceiling is the policy's cap on how long an approval may be remembered, and an implementation MUST NOT offer an answer above it. Check clamps the returned recall anyway, so ignoring the ceiling is not a security hole — it is a HONESTY hole, which is worse in the way that matters: a sheet offering "always" for an action capped at RecallNever takes a person's standing yes, silently downgrades it to this once, and asks them again tomorrow. They answered a question that was never on offer. Offerable is the shared rule for filtering.

type Grant

type Grant struct {
	Kind   string `json:"kind"`
	Target string `json:"target"`
}

Grant is a remembered approval — a (Kind, Target) pattern a human has allowed. What a Target pattern MEANS is the store's and the policy's to decide, not this library's.

type Grants

type Grants interface {
	Allowed(ctx context.Context, a Action) bool
	Remember(ctx context.Context, a Action, g Grant, recall Recall) error
}

Grants stores remembered approvals. Allowed reports whether an action is covered by any grant, by the store's own target semantics; Remember records the answer a human gave to action a — the grant g, the exact action or a wider one, at the given Recall (RecallSession = for a while, RecallAlways = keep durably; Check never calls Remember with RecallNever). Both get the ctx the action was checked under, so a store may keep answers apart by whom the work was for. A Remember that fails is logged by Check; the call it answered still runs.

A durable implementation is the consumer's; MemGrants is the in-memory default.

type MemGrants

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

MemGrants is an in-memory Grants: a grant's Kind and Target each cover their own value, and "*" covers any. It has no durable backing, so Always is remembered only for the life of the process. The grants are a set, so repeated approvals don't accumulate. Safe for concurrent use.

func NewMemGrants

func NewMemGrants(seed ...Grant) *MemGrants

NewMemGrants builds an in-memory grant set, optionally seeded with standing grants.

func (*MemGrants) All

func (m *MemGrants) All() []Grant

All returns the standing grants, sorted, so a consumer can SHOW them.

A remembered approval is authority that outlives the moment it was given, and one nobody can see is one nobody revokes.

func (*MemGrants) Allowed

func (m *MemGrants) Allowed(_ context.Context, a Action) bool

Allowed reports whether a grant covers a.

func (*MemGrants) Forget

func (m *MemGrants) Forget(g Grant) bool

Forget drops a grant, reporting whether it was there. A remembered approval outlives the thing it was granted for, so a consumer that removes what prompted the question drops the answer with it.

func (*MemGrants) Remember

func (m *MemGrants) Remember(_ context.Context, _ Action, g Grant, _ Recall) error

Remember records g. In-memory, everything lasts the process; the Recall (for a while vs durable) matters only to a durable store.

type Policy

type Policy interface {
	Decide(ctx context.Context, a Action) Ruling
}

Policy decides whether an action is freely allowed, must be asked, or denied — and for an Ask, the Recall ceiling on remembering it. The ctx is the one the action is checked under, so a policy may read who the work is for from it; the grant + approval layer sits on top of an Ask.

type PolicyFunc

type PolicyFunc func(context.Context, Action) Ruling

PolicyFunc adapts a plain function to Policy. Use it for per-Kind control, e.g. return AskWith(RecallNever) for an irreversible Kind so it is asked every time.

func (PolicyFunc) Decide

func (f PolicyFunc) Decide(ctx context.Context, a Action) Ruling

Decide calls f.

type ReadOnly

type ReadOnly interface {
	agentkit.Tool
	ReadOnly() bool
}

ReadOnly is a tool that declares it only reads: a call changes nothing anywhere (no write, no message sent, no state kept), so repeating it with nobody watching (a refresh) has no effect but its answer. The declaration is the tool author's; a policy may require it, never infer it.

type Recall

type Recall int

Recall caps, per Kind, how long an approval MAY be remembered — the policy's ceiling on the human's choice. It bounds irreversible actions: a pay/delete Kind can require RecallNever so it is asked every single time, no matter what the human picks. (Prefixed because a bare "Always" would collide with Scope.Always.)

The zero value is RecallNever — the SAFEST option — so a forgotten Recall fails closed: it asks every time rather than silently remembering. Loosening to RecallSession/RecallAlways is explicit.

The order is LOAD-BEARING: values ascend from most restrictive (Never) to least (Always), so Check combines the policy ceiling and the human's choice with min() — the more restrictive wins. Do not reorder.

const (
	RecallNever   Recall = iota // 0 = never remembered; the grant cache is skipped and it asks every time
	RecallSession               // remembered only this session
	RecallAlways                // remembered durably (the human's Always is honored)
)

func Offerable

func Offerable(want, ceiling Recall, widens bool) (Recall, bool)

Offerable reports the recall an answer may carry under ceiling, and whether to offer it at all. It is the WHOLE rule, in one place, because every approver renders its own answer sheet and a rule split across two of them drifts the day it changes.

The two kinds of answer are treated differently, and that asymmetry is the substance:

An answer on the action's EXACT target is offered unchanged or not at all. Clamping "always" down to a session under a session ceiling would put two buttons on the sheet that do the same thing, with the upper one lying about it. So above the ceiling it is dropped, and what remains is what it says. "Allow once" is RecallNever and therefore survives every ceiling — which is the point: there is always at least one answer.

A WIDENING is clamped instead, because its purpose is the broader TARGET and the recall is secondary: "everyone at this domain, for this session" is a real answer somebody may want. The exception is a RecallNever ceiling, where nothing is remembered at all — a broader grant that outlives nothing is the "once" button wearing a bigger label, and offering it would be the same lie in a wider frame.

func (Recall) String

func (r Recall) String() string

String names a recall: "never", "session" or "always". An unknown value names the narrowest one rather than inventing a wider promise for whoever renders it.

type Ruling

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

Ruling is a policy's verdict on an action. Its fields are unexported: build it with Allowed, Denied or AskWith so an Ask ALWAYS states its Recall — there is no way to construct an ask that silently defaults its remembering.

func Allowed

func Allowed() Ruling

Allowed runs the action freely — no ask, nothing remembered.

func AskWith

func AskWith(recall Recall, suggest ...Grant) Ruling

AskWith requires human approval, with recall capping how long an approval may be remembered (RecallNever = ask every time, RecallSession = for a while, RecallAlways = durable). suggest are the wider grants the human may choose instead of the exact action: what a target means is the policy's to know, so the widenings it offers are too.

func Denied

func Denied() Ruling

Denied refuses the action.

type Targeted

type Targeted interface {
	agentkit.Tool
	TargetArgs() []string
}

Targeted is a tool that says where its call carries the Target it checks: JSON pointers into its arguments ("/url", "/to"), each naming a string or an array of strings. A policy that decides a call before it is made (a standing call whose request is reviewed ahead) reads the targets from there, the way the tool itself reads them, without running it.

Directories

Path Synopsis
Command example exercises the gate package without an LLM: a guarded tool asks once, the answer is remembered so the second call passes silently, and a denied tool is refused.
Command example exercises the gate package without an LLM: a guarded tool asks once, the answer is remembered so the second call passes silently, and a denied tool is refused.

Jump to

Keyboard shortcuts

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