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 ¶
- Variables
- func Check(ctx context.Context, a Action) error
- func IsReadOnly(t agentkit.Tool) bool
- func Open(ctx context.Context) context.Context
- func With(ctx context.Context, p Policy, g Grants, a Approver) context.Context
- func WithLogger(ctx context.Context, log agentkit.Logger) context.Context
- func WithReadOnly(t agentkit.Tool) agentkit.Tool
- func WithTargets(t agentkit.Tool, args ...string) agentkit.Tool
- func Wrap(t agentkit.Tool) agentkit.Tool
- type Action
- type Answer
- type Approver
- type Grant
- type Grants
- type MemGrants
- type Policy
- type PolicyFunc
- type ReadOnly
- type Recall
- type Ruling
- type Targeted
Constants ¶
This section is empty.
Variables ¶
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:
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 ¶
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 ¶
IsReadOnly reports whether t declares that it only reads.
func Open ¶
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 ¶
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 ¶
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 ¶
WithReadOnly is t declaring that it only reads (ReadOnly).
func WithTargets ¶
WithTargets is t declaring that the Target it checks is at args in its arguments.
func Wrap ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
NewMemGrants builds an in-memory grant set, optionally seeded with standing grants.
func (*MemGrants) All ¶
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.
type Policy ¶
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 ¶
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.
type ReadOnly ¶
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.
func Offerable ¶
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.
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 AskWith ¶
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.
type Targeted ¶
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.
Source Files
¶
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. |