Policy as code
A policy file holds Guard rules as text you can review, test, and keep in version control. Presets set a default for each risk class. A policy file adds rules for specific commands, packages, publishers, paths, or tools, and tests that prove those rules do what you meant.
The format is GuardPolicy, version v1alpha1. The version is suitable for interoperability testing and is not a stability promise, so expect the format to change between releases and re-run hol-guard policy validate after you upgrade.
Policy files are optional. Without one, Guard uses the security preset you chose and your saved approvals.
Start from a starterDirect link to Start from a starter
hol-guard policy init --list
hol-guard policy init --starter package-firewall
init writes guard-policy.yaml in the current directory, or the file named by --output. It will not overwrite an existing file unless you pass --force. The new file has the editor schema line, a set of rules, and at least two example tests.
| Starter | What it does |
|---|---|
safe-defaults | Blocks agents from reading environment variables, and blocks unknown publishers. This is the default starter. |
package-firewall | Blocks known compromised packages and sets new publishers to review in the defaults. |
secrets-guard | Blocks agents from reading environment variables or files into a prompt. |
mcp-guard | Blocks agents from starting MCP servers until you narrow or remove the rule. |
team-baseline | A shared floor for every device: compromised packages and secret reads are blocked outright. |
blank | No rules. Guard asks at the device, and you add rules as you learn what your agents do. |
The same starters appear in the Policy Studio gallery in Guard Cloud, so a policy begun in either place reads the same.
The shape of a policyDirect link to The shape of a policy
# yaml-language-server: $schema=https://hol.org/schema/policy/v1alpha1
apiVersion: guard.hashgraphonline.com/v1alpha1
kind: GuardPolicy
metadata:
id: policy.package-firewall
name: "Package firewall"
revision: 1
spec:
defaults:
mode: observe
defaultAction: warn
unknownPublisherAction: review
rules:
- id: rule-package-firewall-compromised-packages
description: "Known compromised npm packages."
enabled: true
effect: block
match:
artifacts:
- pkg:npm/event-stream
- pkg:npm/flatmap-stream
lifetime:
mode: permanent
provenance:
source: builder
createdAt: 2026-01-01T00:00:00Z
x-hol.tests:
- id: test-block-event-stream
name: "Installing event-stream"
event:
harness: codex
artifact: pkg:npm/event-stream
expect:
effect: block
ruleId: rule-package-firewall-compromised-packages
- id: test-fallthrough-package
name: "An unlisted package falls through to the defaults"
event:
harness: codex
artifact: pkg:npm/left-pad
expect:
effect: default
The editor line at the top lets an editor that supports yaml-language-server check the file as you type.
| Part | What it holds |
|---|---|
metadata | An id, a name, a numeric revision, and optional labels. |
spec.defaults | The mode (required) and fallback actions. See Modes. |
spec.rules | The rules, up to 1000. |
spec.x-hol.tests | Examples that run against the rules. See Test it. |
Defaults other than mode are defaultAction (allow, warn, or block), unknownPublisherAction (allow, review, or block), changedHashAction, newNetworkDomainAction, subprocessAction, telemetryEnabled, and syncEnabled. Guard Cloud records these as bundle defaults. Importing a file on a device does not turn a default into a rule.
RulesDirect link to Rules
Each rule has an id, enabled, an effect, a match, a lifetime, and provenance.
effectisallow,block,review, orignore.matchselects what the rule applies to. An emptymatchis an explicit global rule, so add a selector unless you mean that. Selectors includeharnesses,tools,commands,paths,packages,artifacts,ecosystems,mcps,skills,publishers,domains,workspaces,repositories, andsecretTypes.commandstakes a list of exact commands, or an expression that combines conditions withallorany, using the operatorsexact,startsWith,contains,endsWith,glob, andregex.lifetime.modeisonce,session,project,machine,workspace,team,permanent, oruntil.untilneeds anexpiresAtUTC timestamp, and no other mode accepts one.provenancerecords where the rule came from, with asourceand acreatedAttimestamp.
A policy must not store credentials. Guard rejects a field whose name looks like it holds a secret, with the diagnostic forbidden_sensitive_field. Put a package name, a command, or a path in a rule, never a token.
ModesDirect link to Modes
spec.defaults.mode controls what the policy does at the device.
| Mode | Behavior |
|---|---|
observe | Guard records what the policy would decide and does not stop the agent. |
prompt | Guard asks you at the device. This is the mode of the safe-defaults, secrets-guard, mcp-guard, and blank starters. |
enforce | Guard applies the rules. |
Start in observe, and move to enforce once the receipts look right.
Validate and formatDirect link to Validate and format
hol-guard policy validate guard-policy.yaml
hol-guard policy validate guard-policy.yaml --format json
hol-guard policy fmt guard-policy.yaml
hol-guard policy fmt guard-policy.yaml --check
validate reports one of three statuses:
valid: the file is a valid policy Guard can enforce.schema_invalid: the file is not a valid policy.not_enforceable: the file is valid, but this Guard version cannot compile part of it into device rules, for example a selector that only Guard Cloud evaluates.
Text output shows the first 20 problems, and --format json lists all of them. Each problem has a code, a path, a line and column, and a message. A policy file that other users can write to also gets a suggestion to run chmod 600 on it.
fmt rewrites the file in canonical form. With --check, it changes nothing and exits with status 1 if the file differs.
These commands read only the file, so they work in CI and on a machine where Guard has never run.
Test itDirect link to Test it
Tests live in the file, under spec.x-hol.tests, and travel with the policy. Each test has an id, a name, an event (what happened), and an expect. expect.effect is block, allow, or default, where default asserts that no rule matches. expect.ruleId is optional and pins which rule must decide.
hol-guard policy test guard-policy.yaml
hol-guard policy test guard-policy.yaml --watch
hol-guard policy test guard-policy.yaml --format json
Each case prints PASS, FAIL, or AMBIGUOUS, and the command exits with status 1 if any case is not a pass, so it can gate a CI job. --watch re-runs whenever the file changes. A case is AMBIGUOUS when another rule ties with the one you named on both effect and specificity. Tighten a match until one rule clearly wins.
Check one decisionDirect link to Check one decision
hol-guard policy check guard-policy.yaml --event '{"harness":"codex","artifact":"pkg:npm/event-stream"}'
hol-guard policy check guard-policy.yaml --command "npm install event-stream"
check decides one event or one shell command against the file. It prints the effect, the winning rule, and every candidate rule with its state. --event - reads the event from standard input, up to 64 KiB. Command rules need the Guard 3.1 runtime. A file with business rules cannot be checked or tested locally.
A candidate is selected, outranked, unmatched, expired, or not_enforced. An outranked rule carries a reason: block_wins, more_specific_rule, or tie. A tie is flagged as ambiguous and lists the tied rule IDs. If more than 256 rules match one event, Guard blocks and reports matchLimitExceeded.
Which rule winsDirect link to Which rule wins
When several rules match, Guard picks one in this order:
- A local one-shot approval is claimed first.
- Active rules are ordered by scope: artifact, then workspace, then publisher, then harness, then global.
- Within the workspace, harness, and global scopes, a rule that names a specific artifact beats one that does not.
- Remaining ties go to the most recently updated rule. Where the rule came from does not break a tie.
- Expired rules and rules that fail their integrity check are skipped.
Two consequences matter when you write rules. A signed Cloud block or review does not automatically outrank a more specific or newer local allow, so narrow your allows. A managed-restrictive floor set by organization controls is separate and stays in force. See Exceptions and expiring windows.
Install a policy on the deviceDirect link to Install a policy on the device
policy import previews by default and applies only when you ask.
hol-guard policy import guard-policy.yaml --merge
hol-guard policy import guard-policy.yaml --replace --apply
hol-guard policy diff guard-policy.yaml
hol-guard policy status
--mergeadds the file's rules and keeps the ones already on the device.--replacemakes the file the active policy. You must choose one. An import fails if the same rule ID would appear twice, even for identical rules.--dry-runis the default and prints the difference without changing anything.--applywrites it.- An applied import asks you to confirm.
--yesskips the question. With--format jsonthere is no prompt. - When the approval gate is on, applying an import asks for your password or app code first, because it changes saved rules.
policy diff <file>compares a file with the active local policy and exits with status 1 if they differ.
policy status shows the installed policy and, when the device is connected, the Cloud bundle version, its mode, the last sync, and whether the device acknowledged it. --offline skips the Cloud read, and the command still succeeds when you are offline or signed out.
Read the active policyDirect link to Read the active policy
hol-guard policy show
hol-guard policy export --output previous.yaml
hol-guard policy explain
hol-guard policy explain <decision-id>
show prints the active local policy as canonical YAML, and --include-provenance adds where each rule came from. export writes the same document to a file. Export before a change, and you have a copy to import if you need to go back.
policy explain <decision-id> replays a recorded decision against the live policy and lists every candidate rule. The decision ID is a receipt ID. The replay notes that publisher and workspace scopes are not replayed, so a result can differ from the original for rules that depend on them. Without an ID, explain describes the active policy.
Roll out in stepsDirect link to Roll out in steps
- Run
hol-guard policy testuntil every case passes. - Set
mode: observeand import with--replace --apply. - Watch
hol-guard policy status,hol-guard history explain <receipt-id>, andhol-guard policy explain <decision-id>. If a rule would have blocked something you wanted, fix the match, add a test for that event, and import again. - Change the mode to
enforce, runhol-guard policy diff guard-policy.yaml, and import again. - Check
hol-guard policy statusfor the new version and mode.
Exit codesDirect link to Exit codes
| Code | Meaning |
|---|---|
| 0 | Success. For test, every case passed. |
| 1 | test had a failing or ambiguous case, or diff or fmt --check found a difference. |
| 4 | The input was invalid, or the command could not run. JSON output carries an error code. |
All policy commands take --format text|json, and most accept --json. Commands that read the Guard home accept --home or --guard-home.
Policy Studio and Guard CloudDirect link to Policy Studio and Guard Cloud
In Guard Cloud, Policy Studio stores and edits these same documents. The signed bundle that reaches a device and the rules saved on it are compiled from the document, so the document is the thing to review. A device verifies the bundle signature against keys it has pinned. A candidate that fails validation never replaces the current state. Public recipes are inert until you review and publish them in Policy Studio.
See Team policy and delegated approvals for the team workflow.