guardrails

package
v0.9.1 Latest Latest
Warning

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

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

View Source
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.

View Source
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 Asker

type Asker = gates.Asker

Asker asks the judge's model: an instruction and a prompt, its reply.

type Finding

type Finding struct {
	Guardrail string `json:"guardrail"`
	Action    string `json:"action"` // block, warn, redact or pass
	Reason    string `json:"reason,omitempty"`
}

Finding is what a guardrail did: for the log of what acted.

type Guardrail

type Guardrail = gates.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.

func Load

func Load(dirs ...string) []Guardrail

Load reads the guardrail files (*.md) in dirs, an earlier folder's winning, with each built-in one that has no file at its defaults.

func Parse

func Parse(name string, data []byte) Guardrail

Parse reads one guardrail file's contents; name is its file's name without .md.

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) Flush added in v0.9.1

func (h *Hold) Flush() string

Flush is the rest, checked, at the end of the text.

func (*Hold) Write added in v0.9.1

func (h *Hold) Write(piece string) string

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 Limits

type Limits = gates.Limits

Limits are what the limits guardrail holds a turn to.

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.

func (*Output) Findings

func (o *Output) Findings() []Finding

Findings are what the guardrails did, once each.

func (*Output) Flush

func (o *Output) Flush() string

Flush is the rest, at the end of the text.

func (*Output) Write

func (o *Output) Write(piece string) string

Write takes the next piece of text and returns what can be sent now (Hold.Write).

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) Input

func (s Set) Input(text string) (string, bool, []Finding)

Input checks text the client sends to the model with the secrets guardrail: masked when it redacts, refused when it blocks.

func (Set) Output

func (s Set) Output() *Output

Output is a check of one stream of the model's text.

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.

Jump to

Keyboard shortcuts

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