Documentation
¶
Overview ¶
Package gates is the rules that gate an agent's turn (spec_gates_041): checks written in plain English in .agents/rules/<name>.md, with front matter when: before_agent or after_agent, judged in order by a model before the agent runs (on the prompt) or after (on its answer and the files it changed), each blocking, warning or asking for a revision. A rule without when: is an instruction rule (spec_memory_012 MEM-07), listed here so Studio can edit it, and loaded by the memory package.
Index ¶
- Constants
- Variables
- func Check(spec Spec) []string
- func CheckGuardrail(spec GuardrailSpec) []string
- func CommandDenied(command string, denied []string) string
- func Delete(dir, name string) error
- func DeleteGuardrail(dir, name string) error
- func DomainAllowed(raw string, domains []string) bool
- func Injection(text string) string
- func Reorder(dir string, names []string) error
- func Save(dir string, spec Spec, previous string) (string, []string, error)
- func SaveGuardrail(dir string, spec GuardrailSpec, previous string) (string, []string, error)
- func ValidName(name string) bool
- type Asker
- type Builtin
- type Guardrail
- type GuardrailSpec
- type Limits
- type Rule
- type Spec
- type Verdict
Constants ¶
const ( // BeforeAgent gates judge the prompt, before the agent runs. BeforeAgent = "before_agent" // AfterAgent gates judge the answer and the changed files, before the // turn ends. AfterAgent = "after_agent" )
When a gate runs.
const ( // Block stops the turn (before) or marks the answer as failing the // rule (after). Block = "block" // Warn says so in the conversation. Warn = "warn" // Revise gives the agent the reason: with the prompt (before), or as // a request for one more pass (after). Revise = "revise" )
What a failed gate does.
const ( // StageInput checks the prompt, before the turn. StageInput = "input" // StageToolResult checks what a tool brought back, before the model // reads it. StageToolResult = "tool_result" // StageOutput checks the model's answers. StageOutput = "output" // StageAction checks a tool call before it runs. StageAction = "action" )
The stages a guardrail checks.
const ( // BuiltinInjection watches tool results for instructions aimed at the // agent (prompt injection): patterns, and the judge when asked. BuiltinInjection = "prompt_injection" // BuiltinSecrets masks credentials (and, when asked, personal data) // in answers. BuiltinSecrets = "secrets" // BuiltinLimits holds a turn to limits: spending, model calls, files // changed, the domains it may reach and the commands it may not run. BuiltinLimits = "limits" )
The built-in guardrails.
const Redact = "redact"
Redact is a guardrail action: what it finds is masked, the rest kept.
Variables ¶
var Builtins = []Builtin{ {ID: BuiltinInjection, Stage: StageToolResult, Action: Warn, Actions: []string{Warn, Block}}, {ID: BuiltinSecrets, Stage: StageOutput, Action: Redact, Actions: []string{Redact, Warn, Block}}, {ID: BuiltinLimits, Stage: StageAction, Action: Block, Actions: []string{Block}}, }
Builtins are the built-in guardrails, in the order Studio lists them.
var DefaultCommands = []string{"git push*--force*", "git push* -f*", "rm -rf /", "rm -rf ~", "rm -rf ~/"}
DefaultCommands are the commands the limits guardrail refuses without a file: force pushes, and deleting the whole disk or home folder.
var ErrUnknown = errors.New("no such rule")
ErrUnknown means there's no rule of that name.
Functions ¶
func CheckGuardrail ¶
func CheckGuardrail(spec GuardrailSpec) []string
CheckGuardrail is what keeps spec from being saved.
func CommandDenied ¶
CommandDenied is the glob in denied that the shell command matches ("" for none), matched against the whole command with its spaces collapsed.
func DeleteGuardrail ¶
DeleteGuardrail removes guardrail name from dir (a built-in's file: it runs with its defaults again).
func DomainAllowed ¶
DomainAllowed reports whether the URL raw is on one of domains (a domain allows its subdomains); with none listed, every domain is.
func Injection ¶
Injection reports the first phrase in text that looks like instructions aimed at the agent ("" when none does).
func Reorder ¶
Reorder gives the gates named, in dir, orders 10, 20, 30… in the order given: Studio's dragged lists.
func Save ¶
Save writes dir/<name>.md from spec and returns its path, or the problems that keep it from being saved (nothing written). previous is the rule edited ("" for a new one); another name renames its file. The front matter's other fields are kept.
func SaveGuardrail ¶
SaveGuardrail writes dir/<name>.md from spec (a built-in's file is named by its id, and edited in place when it's there) and returns its path, or the problems that keep it from being saved. previous is the custom guardrail edited ("" for a new one).
Types ¶
type Builtin ¶
Builtin is one of the built-in guardrails: where it runs, its default action, and the actions it takes.
type Guardrail ¶
type Guardrail struct {
// Name is its file's name without .md (a built-in's is its id).
Name string
Path string
Builtin string
Stage string
// Action is Block, Warn or Redact (a built-in's allowed ones).
Action string
// Agents are the agents it guards (none: every one).
Agents []string
// Tools are the tools a tool-result or action guardrail checks (none:
// those that bring in outside content, for tool results; every tool,
// for actions).
Tools []string
Description string
// PII makes the secrets guardrail mask personal data too; Judge makes
// the injection guardrail confirm with the judge before acting.
PII bool
Judge bool
Limits Limits
// Text is a custom guardrail's check, in plain English.
Text string
// Off is a guardrail switched off (enabled: false): it checks nothing.
Off bool
Problem string
}
Guardrail is one guardrail file.
func Active ¶
Active are the guardrails that hold (no problem, not switched off) at stage for agent.
func BuiltinDefault ¶
BuiltinDefault is a built-in guardrail as it runs without a file: on, with its default action.
func LoadGuardrails ¶
LoadGuardrails reads every guardrail file (*.md) in dirs, a name in an earlier folder winning, and adds each built-in one without a file, with its defaults: the built-in ones first, in catalog order, then custom ones by stage and name.
func ParseGuardrail ¶
ParseGuardrail reads a guardrail file; what doesn't hold is its Problem (it's skipped).
func (Guardrail) Checks ¶
Checks reports whether g checks the named tool: one of its tools, or with none listed, every tool for an action guardrail and those that bring in outside content (external) for a tool-result one.
func (Guardrail) GuardsAgent ¶
GuardsAgent reports whether g guards the named agent (none listed: every agent; one that can't be told: guarded).
type GuardrailSpec ¶
type GuardrailSpec struct {
Name string
Builtin string
Stage string
Action string
Agents []string
Tools []string
Description string
PII bool
Judge bool
Limits Limits
Text string
// Off switches the guardrail off (enabled: false), keeping its
// settings.
Off bool
}
GuardrailSpec is what Studio's guardrail form edits.
type Limits ¶
type Limits struct {
MaxCostUSD float64 `yaml:"max_cost_usd,omitempty"`
MaxTurns int `yaml:"max_turns,omitempty"`
MaxFilesChanged int `yaml:"max_files_changed,omitempty"`
AllowedDomains []string `yaml:"allowed_domains,omitempty"`
// Commands are shell commands the agent may not run (globs, like
// "git push --force*", matched against the whole command).
Commands []string `yaml:"commands,omitempty"`
}
Limits are what the limits guardrail holds a turn to; zero or empty means no limit of that kind.
type Rule ¶
type Rule struct {
// Name is its file's path in the rule folder, without .md.
Name string
Path string
// When is BeforeAgent or AfterAgent for a gate, "" for an instruction
// rule.
When string
// Order sorts gates (then their names): lower first.
Order int
// Agents are the agents it gates (none: every one).
Agents []string
// Action is Block, Warn or Revise (a gate; Warn when unset).
Action string
Description string
// Paths scope an instruction rule to files (MEM-07).
Paths []string
// Text is the rule, in plain English.
Text string
// Problem says why its front matter doesn't hold (it's skipped).
Problem string
}
Rule is one rule file: a gate (When set) or an instruction rule.
func Gates ¶
Gates are the rules that run when for the agent, in order, without those whose front matter doesn't hold.
func Load ¶
Load reads every rule file (*.md, recursively) in dirs; a name in an earlier folder wins. Gates come first, by when (before, then after), order and name; then instruction rules by name.
type Spec ¶
type Spec struct {
Name string
When string
Action string
Order int
Agents []string
Description string
Paths []string
Text string
}
Spec is what Studio's rule form edits.
type Verdict ¶
Verdict is the judge's answer: whether the subject passes the rule, and why (for a failure, what to fix).
func ConfirmInjection ¶
ConfirmInjection has ask judge whether content is prompt injection.