Documentation
¶
Overview ¶
Package guardrails runs Blitz's guardrails on a model's request and reply, for a program that has those rather than an engine turn: a gateway between Blitz and the models (spec_053_enterprise EN-23). The rules and their checks are the engine's own (pkg/engine/gates), so a guardrail means the same there as on the desktop:
- a tool's result going back to the model: the injection guardrail takes out instructions aimed at the agent (confirmed by the judge when it asks), and custom tool-result guardrails are judged;
- what the client sends: the secrets guardrail masks or refuses credentials;
- a tool call the model makes: the limits guardrail's domains and commands, and custom action guardrails, judged;
- the model's text as it streams: the secrets guardrail, over a window held back so a secret split between pieces is still caught.
Custom output guardrails judge a whole answer, which a gateway has only once it's been sent: they aren't run here.
Index ¶
- Constants
- type Asker
- type Finding
- type Guardrail
- type Hold
- type Limits
- type Output
- type Set
- func (s Set) Answer(ctx context.Context, text string, streamed bool) (refusal string, fs []Finding)
- func (s Set) Input(text string) (string, bool, []Finding)
- func (s Set) Output() *Output
- func (s Set) Prompt(ctx context.Context, text string) (refusal string, fs []Finding)
- func (s Set) ToolCall(ctx context.Context, tool string, args map[string]any, external bool) (refusal string, fs []Finding)
- func (s Set) ToolResult(ctx context.Context, tool string, external bool, text string) (out string, withheld bool, fs []Finding)
Constants ¶
const ( Block = gates.Block Warn = gates.Warn Redact = gates.Redact StageToolResult = gates.StageToolResult StageOutput = gates.StageOutput StageAction = gates.StageAction Injection = gates.BuiltinInjection Secrets = gates.BuiltinSecrets LimitsID = gates.BuiltinLimits )
Actions and stages, as the engine names them.
const Window = 160
Window is how much of the model's text Output holds back: a secret shorter than this is caught even when it comes in pieces.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Finding ¶
type Finding struct {
Guardrail string `json:"guardrail"`
Action string `json:"action"` // block, warn, redact or pass
Reason string `json:"reason,omitempty"`
// Score is how surely it was broken (0 to 1) and Check which check
// decided (spec_041 GT-44): a custom guardrail's.
Score float64 `json:"score,omitempty"`
Check string `json:"check,omitempty"`
}
Finding is what a guardrail did: for the log of what acted.
type Guardrail ¶
Guardrail is one guardrail: a built-in one or a custom check.
func Defaults ¶
func Defaults() []Guardrail
Defaults are the built-in guardrails as they run with no files: on, each with its default action.
type Hold ¶ added in v0.9.1
type Hold struct {
// Check is text as it may be sent (masked, or replaced); stop ends
// the stream: what it returned is the last of it.
Check func(text string) (out string, stop bool)
// Window is how much is held before the word still arriving (Window
// when less).
Window int
// contains filtered or unexported fields
}
Hold holds back the end of a stream of text that a check can't judge yet, so a secret is checked whole however the stream splits it: the engine's guardrails use it for the model's answer as the gateway's do.
func (*Hold) Write ¶ added in v0.9.1
Write takes the next piece of text and returns what can be sent now. The word still arriving at the end (a run of the characters a key or a password's value is made of) isn't checked yet: a key masked when only its start had come would send the rest of it unmasked. It's held, with the window before it, until something ends it or the text does.
type Output ¶
type Output struct {
// contains filtered or unexported fields
}
Output checks the model's text as it streams, with the secrets guardrail: each piece goes in, and what's safe to send comes out.
type Set ¶
type Set struct {
All []Guardrail
Agent string // the agent the request is for ("" for none)
// Ask is the judge, for custom guardrails and the injection
// guardrail's confirmation. Nil: patterns only; a custom guardrail
// that blocks then refuses, as it can't be judged.
Ask Asker
// Redactor masks known values as well as the built-in patterns (nil:
// the patterns and remembered values).
Redactor *redact.Redactor
}
Set is the guardrails one request runs under.
func (Set) Answer ¶ added in v1.1.0
Answer checks the model's whole answer against the custom after_model (output) guardrails (GT-45). streamed leaves out those an Output already ran on the answer piece by piece (Streamable).
func (Set) Input ¶
Input checks text the client sends to the model with the secrets guardrail: masked when it redacts, refused when it blocks.
func (Set) Prompt ¶ added in v1.1.0
Prompt checks what the client sends the model against the custom before_model (input) guardrails (GT-45): a refusal when one that blocks catches it.
func (Set) ToolCall ¶
func (s Set) ToolCall(ctx context.Context, tool string, args map[string]any, external bool) (refusal string, fs []Finding)
ToolCall checks a tool call the model makes before the client sees it: the limits guardrail's domains (web_fetch, a browser's navigate) and commands (run_shell_command), and custom action guardrails. A refusal is what to tell the agent instead of the call.
func (Set) ToolResult ¶
func (s Set) ToolResult(ctx context.Context, tool string, external bool, text string) (out string, withheld bool, fs []Finding)
ToolResult checks a tool's result going back to the model. external is whether the tool brings in outside content (the web, a browser, an MCP server), which the injection guardrail checks. It returns the result to send (instructions aimed at the agent taken out), or withheld with the reason to send instead.