Octomind AI Agent Guardrails
skillWrite and debug LLM guardrails for an Octomind AI agent in .agents/guardrails.toml: pipe, guard, hook and validator tables, match syntax, scripts.
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:
| Table | Moment | Can it stop work? |
|---|---|---|
[[pipe]] | before the model sees the user message | yes: a non-zero exit rejects the message |
[[guard]] | before a tool call runs | yes: the model gets an error instead of a result |
[[hook]] | after a tool result | no: it can only send feedback |
[[validator]] | after the final assistant message of a turn | no: 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. Duplicatenamevalues among pipes, or among validators, and a bad regex or a badwhensign are parse errors, so one mistake disables the whole file. - Misspelled optional keys are ignored, not reported:
roleinstead ofroles, orroleson a guard or hook, does nothing. - Scripts run from a private copy in
<data dir>/guardrails/, not from the workdir.$0is 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.
commandandscriptare 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
registerreply, each with anidand amodeofapplyorobserve; 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.
capability any call to that capability
capability(regex) regex tried against the whole params object as JSON
capability(arg=regex) regex tried against one argumentcapabilityis the name of the tap capability that owns the tool, for exampleshell,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_toolsof installed capabilities: an exactserver:toolentry wins overserver:*. 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 asa=bis therefore read as argumenta. - 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]]
| Field | Type | Required | Notes |
|---|---|---|---|
name | string | yes | unique; sent to the script as PIPE_NAME |
command | path | yes | script to run |
match | regex | no | on the user message; empty or absent matches all |
when | "first" or "any" | no | default "any"; "first" only for the first message |
roles | list of strings | no | empty 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]]
| Field | Type | Required | Notes |
|---|---|---|---|
match | target | yes | the call to refuse |
has | string or list | no | names of configured MCP servers that must all exist |
when | list of signed targets | no | history condition |
message | string | yes | what 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. hastakes MCP server names from the config (filesystem,core), not capability names.
[[hook]]
| Field | Type | Required | Notes |
|---|---|---|---|
match | target | no | blank means every call |
result | regex | no | on the result text; "" matches everything |
on | "success", "error" or "any" | no | default "any" |
script | path | yes | script 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};capabilityisnullwhen the tool has no owner. The result is the full, untruncated text. Environment:OCTOMIND_CAPABILITY,OCTOMIND_TOOL,OCTOMIND_SUCCESS(1or0),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]]
| Field | Type | Required | Notes |
|---|---|---|---|
name | string | yes | unique; keys the history cursor |
match | regex | no | on the final assistant message; empty or absent matches all |
when | list of signed targets | no | checked against calls since the last run |
roles | list of strings | no | empty means every agent |
script | path | yes | script 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_bylists{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 debugthenoctomind run <role>logsLoaded 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
injectedevents withsource_kindguardrail_hookorguardrail_validator, fromoctomind run developer --format jsonl. - To see which capability a tool belongs to, add a temporary
[[hook]]with nomatchwhose script printstoolandcapabilityfrom 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 inhas. - 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 tocommand.
Examples
A pipe, a guard that refuses a destructive shell command, and a guard that depends on history and a configured server:
[[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/:
[[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:
#!/usr/bin/env bash
printf '%s\n' "Edits were made without a cargo test call. Run the tests or say why not."
exit 1Checklist
capabilitynames are owning capability names;hasnames are server names.nameis unique among pipes and among validators; everywhenentry 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.mdin the Octomind docs, anddoc/integration/05-plugins.mdfor theregisterreply. The docs are served at https://octomind.run/docs/.