view source

Octomind AI Agent Guardrails

skill

Write and debug LLM guardrails for an Octomind AI agent in .agents/guardrails.toml: pipe, guard, hook and validator tables, match syntax, scripts.

Apache-2.0 Requires: octomind binary to try rules in a session; scripts need an executable bit on macOS/Linux and their interpreter installed.

agents that use this skill

instructions

Overview

A project can ship its own rules for an Octomind session in <workdir>/.agents/guardrails.toml. A rule can rewrite or reject the user's message, refuse a tool call, or run a script that sends feedback to the model after a tool result or after the final reply. This skill gives the exact tables, fields, match syntax, script contract and limits.

Mental model

The file has four kinds of table, one per moment in a turn:

TableMomentCan it stop work?
[[pipe]]before the model sees the user messageyes: a non-zero exit rejects the message
[[guard]]before a tool call runsyes: the model gets an error instead of a result
[[hook]]after a tool resultno: it can only send feedback
[[validator]]after the final assistant message of a turnno: it can only send feedback

Hooks and validators only nudge: their stdout reaches the model on a later request and undoes nothing. The file and every script it names are read when the session starts; edits apply only in a new session.

Rules

File loading

  • The file is <workdir>/.agents/guardrails.toml. A missing file means no rules, with no message.
  • A parse error prints guardrails: failed to parse ... to stderr and the session runs with no authored rules. It never crashes. Duplicate name values among pipes, or among validators, and a bad regex or a bad when sign are parse errors, so one mistake disables the whole file.
  • Misspelled optional keys are ignored, not reported: role instead of roles, or roles on a guard or hook, does nothing.
  • Scripts run from a private copy in <data dir>/guardrails/, not from the workdir. $0 is the copy, so a script that reads files beside itself uses $OCTOMIND_SCRIPT_DIR. The working directory stays the session workdir.
  • A script missing at session start never runs: a matching pipe rejects the message; a hook or validator prints to stderr and is skipped.
  • command and script are one path, relative to the workdir or absolute. They are not shell strings and take no arguments.
  • A plugin can supply rules in the same format in its register reply, each with an id and a mode of apply or observe; they are appended after the authored ones, and a registered pipe is dropped when the file has its own pipe.

Match syntax

Used in match of [[guard]] and [[hook]], and in when entries.

text
capability                  any call to that capability
capability(regex)           regex tried against the whole params object as JSON
capability(arg=regex)       regex tried against one argument
  • capability is the name of the tap capability that owns the tool, for example shell, filesystem-read, filesystem-write. It is an exact comparison. A server name or tool name matches only if it is also a capability name.
  • The owner comes from the allowed_tools of installed capabilities: an exact server:tool entry wins over server:*. Tools activated at runtime are checked next. A tool with no owner never matches.
  • The text before the first = inside the parentheses names an argument only if it is made of letters, digits and underscores. Otherwise the whole text is the regex. A regex such as a=b is therefore read as argument a.
  • A string argument is matched without quotes. Arrays, objects and numbers are matched in their JSON form. A missing argument is matched as an empty string.
  • Regexes are unanchored: use ^ and $ when position matters. Write backslashes doubled in a TOML basic string or use a single-quoted literal string.
  • An unclosed ( or an empty capability is a parse error.

when conditions

A list of strings, each +target (a matching call exists in the history window) or -target (none exists). All entries must hold. A string with no leading sign is a parse error. History holds calls that were allowed, recorded before they run, so it tracks attempts, not success. Blocked calls are not recorded.

  • In a guard the window is the whole session.
  • In a validator the window is the calls since that validator last ran.

[[pipe]]

FieldTypeRequiredNotes
namestringyesunique; sent to the script as PIPE_NAME
commandpathyesscript to run
matchregexnoon the user message; empty or absent matches all
when"first" or "any"nodefault "any"; "first" only for the first message
roleslist of stringsnoempty means every agent
  • Checked in the order roles, when, match. At most one pipe may match a message; two matches reject the message with an error.
  • stdin is the raw message. Environment: OCTOMIND_ROLE, OCTOMIND_WORKDIR, OCTOMIND_SCRIPT_DIR, PIPE_NAME, PIPE_RUN_COUNT (starts at 1, per pipe), SESSION_MESSAGE_COUNT (counts every message, matching or not).
  • Exit 0: stdout replaces the message exactly, without trimming. Exit non-zero, a spawn failure or a timeout: the message is not sent and an error is shown, built from stderr, else stdout, else the exit code.
  • Timeout: 300 seconds.

[[guard]]

FieldTypeRequiredNotes
matchtargetyesthe call to refuse
hasstring or listnonames of configured MCP servers that must all exist
whenlist of signed targetsnohistory condition
messagestringyeswhat the model reads
  • Guards are tried in file order and the first one whose conditions all hold blocks the call. The model receives [guardrail] <message> as the tool error and the tool never starts.
  • has takes MCP server names from the config (filesystem, core), not capability names.

[[hook]]

FieldTypeRequiredNotes
matchtargetnoblank means every call
resultregexnoon the result text; "" matches everything
on"success", "error" or "any"nodefault "any"
scriptpathyesscript to run
  • Every matching hook runs, in parallel, after the tool batch finishes and before the result is truncated. Calls blocked by a guard are skipped.
  • stdin is JSON {capability, tool, tool_id, params, result, success}; capability is null when the tool has no owner. The result is the full, untruncated text. Environment: OCTOMIND_CAPABILITY, OCTOMIND_TOOL, OCTOMIND_SUCCESS (1 or 0), OCTOMIND_WORKDIR, OCTOMIND_SCRIPT_DIR.
  • Exit 0 does nothing. Exit non-zero sends trimmed stdout to the model as feedback; empty stdout sends nothing. stderr goes to the debug log only on a non-zero exit. Timeout: 300 seconds, with no feedback.

[[validator]]

FieldTypeRequiredNotes
namestringyesunique; keys the history cursor
matchregexnoon the final assistant message; empty or absent matches all
whenlist of signed targetsnochecked against calls since the last run
roleslist of stringsnoempty means every agent
scriptpathyesscript to run
  • Runs once at the end of each assistant turn, for every validator whose filters pass, checked in the order roles, when, match. With no filters it runs every turn. It is independent of [skills] auto_validation.
  • The history cursor moves when the script starts, so a failed or timed-out run still uses up that window.
  • stdin is JSON {validator, role, assistant_text, triggered_by}. triggered_by lists {capability, params} of the calls in the window that matched a + target, or every call in the window when there is no + target. Environment: OCTOMIND_VALIDATOR, OCTOMIND_ROLE, OCTOMIND_WORKDIR, OCTOMIND_SCRIPT_DIR.
  • Exit non-zero with non-empty trimmed stdout sends <validation validator="NAME"> plus the stdout to the model. Anything else sends nothing. Timeout: 300 seconds.
  • A script that always exits 1 with text nags after every turn. Exit 0 once the problem is fixed.

Roles

A roles entry matches the running agent's tag exactly, or names its namespace: developer covers developer and developer:acme; developer:acme covers only that agent; developer:default means developer. Only pipes and validators take roles.

Order within a turn

Pipe on the message; guards on the tool calls in arrival order, each allowed call entering the history for the next; allowed tools run; hooks on the results; validators after the final message.

Testing a rule

  • Pipe sample stdin into the script by hand and check its exit code.
  • Start a new session after every edit. octomind config --log-level debug then octomind run <role> logs Loaded guardrails: N pipe(s), N guard(s), N hook(s), N validator(s); a parse error appears on stderr instead.
  • A guard fired when the model reports a tool error starting with [guardrail].
  • Hook and validator feedback appears as JSONL injected events with source_kind guardrail_hook or guardrail_validator, from octomind run developer --format jsonl.
  • To see which capability a tool belongs to, add a temporary [[hook]] with no match whose script prints tool and capability from stdin to stderr and exits 1, then run with /loglevel debug.

Common mistakes

  • Using a tool name or server name as capability, or a capability name in has.
  • Expecting a validator or hook to block. They cannot.
  • Expecting when = ["+shell(command=cargo test)"] to prove tests passed. It proves a call was allowed.
  • Two pipes whose filters overlap, or a catch-all pipe next to another pipe.
  • Editing a script mid-session and expecting the change to apply.
  • Forgetting chmod +x, or adding arguments to command.

Examples

A pipe, a guard that refuses a destructive shell command, and a guard that depends on history and a configured server:

toml
[[pipe]]
name = "prepare"
command = "./scripts/prepare-input.sh"
when = "first"
roles = ["developer"]

[[guard]]
match = 'shell(command=^rm\s+-rf?\s+/)'
message = "Refusing rm -rf on root paths."

[[guard]]
match = 'shell(command=^ls\b)'
has = "filesystem"
when = ["-filesystem-read"]
message = "Use the file-listing tool instead of ls."

A hook and a validator with scripts under .agents/:

toml
[[hook]]
match = 'shell(command=cargo (build|test|check))'
result = 'error\['
on = "any"
script = ".agents/hooks/cargo-summary.sh"

[[validator]]
name = "test-after-edit"
when = ["+filesystem-write", "-shell(command=cargo test)"]
roles = ["developer"]
script = ".agents/validators/remind-tests.sh"

The validator script prints its feedback and exits 1:

bash
#!/usr/bin/env bash
printf '%s\n' "Edits were made without a cargo test call. Run the tests or say why not."
exit 1

Checklist

  • capability names are owning capability names; has names are server names.
  • name is unique among pipes and among validators; every when entry starts with + or -.
  • At most one pipe can match any message.
  • Scripts exist at session start, are executable, and take no arguments.
  • Hook and validator scripts exit non-zero and print to stdout only when feedback is wanted.
  • The rule was tried in a new session and its effect seen in the log, the tool error or the JSONL events.

References

  • doc/usage/18-guardrails.md in the Octomind docs, and doc/integration/05-plugins.md for the register reply. The docs are served at https://octomind.run/docs/.