Policy and rule management
Policy and rules form the control layer for command safety. A user policy sets the safety level, protected paths, workflow behavior, and rule switches. Rulebooks add command-specific rules from user or project scope. The safety engine combines these inputs into one effective context before it returns an outcome.
Policy model
The policy model separates broad safety settings from named rules. This lets an operator raise the general safety level or adjust one known rule without changing the safety engine.
Policy controls
The user policy defines settings that apply across workspaces. Its main controls are:
- Safety level:
standard,strict, orparanoid. - Safety capabilities: fail-closed analysis, paranoid removal checks, and paranoid interpreter checks.
- Destructive command protection: the master switch, per-rule
onoroffchanges, and paths allowed for destructive operations. - Secret protection: the master switch, per-rule changes, paths that must stay protected, and exact paths that may be read or changed.
- Workflow mode: worktree-aware behavior.
- Audit retention: the number of days for stored activity.
The policy uses named rule IDs for per-rule changes. A rule override can disable a rule or replace its block message and intent. Catastrophic built-in protections remain active even when the destructive-command master switch is off. See Policy storage and snapshots for the policy file, validation, repair, and snapshot contract.
References: src/…/policy.ts• L19-40 src/…/policy.ts• L82-105 src/…/destructive-command-rules.ts• L538-625
Rulebook controls
A rulebook is a named and versioned group of custom command rules. Each rule identifies a command and the command shape or arguments that cause a block. It also gives the operator the reason and intent for that block. A rulebook lists the commands its rules may target, which prevents a rule from silently introducing an undeclared command.
The rules configuration selects rulebook sources and can add two kinds of local behavior: per-rule overrides and transparent wrappers. A transparent wrapper tells analysis that a command can run a visible protected child command without hiding that child from safety checks. Custom rule IDs use the rulebook name as their namespace, such as custom.project-rules/block-docker-system-prune. See Rulebook resolution and customization for source resolution, synchronization, and custom rule behavior.
New project setup can create an empty rules configuration and a starter rulebook. The starter content demonstrates a project-specific command block without changing the built-in rule set.
Rulebooks support two rule formats. Version 1 keeps the command, optional subcommand, and blocking argument model. Version 2 describes an exact command path and can also require or exclude argument values. Version 2 rulebooks can include fixture commands that prove their rules block or allow the intended cases when the rulebook is added or updated.
References: src/…/rulebook.ts• L22-54 src/…/rules-config.ts• L14-28 src/…/scope-policy.ts• L228-247 src/…/custom.ts• L42-190 src/…/rulebook-fixtures.ts• L11-52 src/…/config-file.ts• L6-44
Effective policy
The effective policy is the single set of controls used for one safety evaluation. It joins the user policy with the active rulebooks and records enough source information to explain why a setting or rule is active.
Safety levels
The selected safety level expands into three capabilities. standard keeps fail-closed and paranoid checks off. strict enables fail-closed analysis. paranoid also enables the stronger removal and interpreter checks.
The effective level is the result of policy settings, environment settings, and explicit overrides. A safety level from the environment can raise the policy level. Boolean environment flags can enable individual capabilities. Policy capability overrides then set their named values, so a mixed result is reported as custom rather than being forced into a preset name. Analysis options can apply a final per-request change.
References: src/…/env.ts• L27-43 src/…/env.ts• L67-165 src/…/policy-context.ts• L16-43
Policy snapshots
A policy snapshot is an immutable view of the policy and rules used by an evaluation. It contains built-in and custom rules, active wrappers, safety settings, protection paths, and rule metadata. The engine loads the snapshot once for the request and passes the same context to secret checks and command analysis.
The snapshot has a ready state when all sources load cleanly. It has a degraded state when a policy field or rule source needs repair. Degraded state does not discard all protection: invalid policy data falls back to protective values, and an unusable rule source is left inactive while built-in protections and other valid rules remain available.
The following diagram shows how policy inputs become the context for a safety outcome.
flowchart LR
A[User policy] --> C[Effective policy snapshot]
B[User and project rulebooks] --> C
E[Environment and request options] --> D[Effective safety capabilities]
D --> C
C --> F[Secret and path checks]
C --> G[Command rule checks]
F --> H[Safety outcome]
G --> H
References: src/…/snapshot.ts• L18-64 src/…/snapshot.ts• L67-119 src/…/guard.ts• L175-240 src/…/policy.ts• L128-150
Protected paths
Path settings shape where exceptions can apply. Destructive-command allow paths permit selected removal targets to pass the relevant path checks. Secret deny paths add protected locations. Secret allow paths permit exact user-managed files or supported recursive file patterns to bypass secret protection.
Secret path entries can use ~ or a $HOME form for the home directory. A recursive secret allow path must name one exact file after **/, such as ~/…/.env.local; broad globs are rejected. Destructive allow paths must be absolute or home-anchored and cannot contain the home directory itself. Secret allow paths also cannot cover the home directory or the guard's own configuration. These checks keep an exception narrow enough to describe a real workspace path.
Policy and rule data also use bounded user or project scopes. The system resolves a scope-specific configuration location together with the filesystem boundary for that scope. It rejects links and paths that leave the selected scope. It uses safe file replacement and guarded removal so a path race cannot turn policy maintenance into an unrestricted file operation.
References: src/…/allow-paths.ts• L7-91 src/…/paths.ts• L18-40 src/…/paths.ts• L100-164 src/…/filesystem.ts• L101-169 src/…/filesystem.ts• L263-347 src/…/paths.ts• L17-56
Rulebook scope
Rulebooks can apply to the user scope, the project scope, or both. Scope controls let shared user protections and project-specific rules work together without giving a project rulebook control over user-owned rules.
Scope precedence
The loader reads the user and project rule configurations as separate sources. It combines their active rules and wrappers after loading each scope. If both scopes claim the same rulebook name, the user scope keeps the name and the later project rulebook contributes no rules. A project override cannot target a user-scoped rule.
Within a scope, a rule override of off removes that rule from the active set. An object override changes only the rule's reason and optional intent. An unknown override is reported as a warning and does not change other rules.
References: src/…/scope-policy.ts• L34-117 src/…/scope-policy.ts• L169-225 src/…/scope-policy.ts• L332-383
Rulebook sources
A rulebook source identifies where a selected rulebook comes from. A local source is a bare rulebook name. A remote source identifies a GitHub repository, ref, and rulebook name in the form owner/repo#ref/<rulebook-name>. The source format fixes the rulebook identity before its content is loaded.
Each source maps to a rulebook file in its scope. Local rulebooks are authored in place. Remote rulebooks are vendored into the same scope by the add or update flow. Runtime loading reads these files and does not need a network fetch.
Every loaded rulebook must pass schema checks. Its name must match the source identity. Rules must have unique names within the rulebook, and each rule command must appear in allowed_commands. These checks protect the namespace used by overrides and explanations.
References: src/…/source-syntax.ts• L1-15 src/…/source-syntax.ts• L30-88 src/…/rulebook.ts• L49-87 src/…/scope-policy.ts• L251-289
Rulebook validation
Rulebook validation limits the amount of data that the system accepts before schema and matcher checks run. The limits cover source commands, rules, fixtures, match values, string data, and the number of reported errors. This keeps authored or remote rulebooks from making policy loading consume unbounded resources.
Version 2 fixtures are analyzer inputs rather than executed commands. A blocked fixture passes only when its named rule is the first match. An allowed fixture passes only when no rule matches. Freshly authored or fetched content must pass these checks before it becomes a vendored rulebook. A rulebook already stored in the scope is loaded by schema validation alone.
References: src/…/rulebook-limits.ts• L1-14 src/…/rulebook-limits.ts• L16-109 src/…/rulebook-fixtures.ts• L11-52 src/…/resolver.ts• L228-235
Synchronization
Synchronization turns configured sources into active files for a selected scope. It reads every configured source so the scope can be checked as a whole. A refresh fetches only the selected remote sources. Other remote sources use their existing vendored files.
The add flow can discover all rulebooks in a GitHub repository or select named rulebooks from that repository. It resolves the ref, validates the selected content, and writes each remote rulebook to the path claimed by its name. An existing file with unknown provenance is not overwritten. A source-name collision is rejected because two sources would claim the same active file.
The update flow reports source-level fetch or validation failures while keeping successful changes. It treats resource-budget exhaustion as an operation-wide failure. Writes use atomic replacement, and a failed batch restores the files that the operation changed. The runtime scope is loaded again before success is reported, so a successful sync matches what the safety engine will use.
Remote work is bounded by limits on sources, concurrent work, requests, time, and response bytes. GitHub response bodies are also read under per-response byte caps and a shared operation budget. Use cc-safety-net rule update when a remote rulebook file is missing or when its configured source must be refreshed.
References: src/…/sync.ts• L69-121 src/…/sync.ts• L123-199 src/…/sync.ts• L304-424 src/…/resolver.ts• L71-107 src/…/resolver.ts• L109-155 src/…/resolver.ts• L270-364 src/…/resource-limits.ts• L1-57
Safety exceptions
Exceptions are narrow changes to inherited behavior. The system keeps the inherited state and the source of each effective rule so operators can tell whether a result came from a preset, an override, an environment flag, or a built-in safeguard.
Built-in rule classes
Built-in destructive-command rules cover high-risk Git, filesystem, PowerShell, interpreter, and dynamic execution patterns. Some rules always block catastrophic targets such as root, home, or Git metadata. Other rules depend on strict, paranoid removal, or paranoid interpreter capabilities.
Secret protection uses built-in matches for credential files, environment files, private keys, home service credentials, and coding CLI credential paths. Coding CLI configuration files that can carry inline credentials are off by default because agents edit them during normal work. An explicit rule override opts those rules in.
The destructive-command master switch disables ordinary built-in rules. It does not disable catastrophic rules. Each rule also carries an intent such as a hard stop, an alternative, or a narrower scope. That intent guides the safety outcome shown to the operator.
References: src/…/destructive-command-rules.ts• L22-42 src/…/destructive-command-rules.ts• L232-290 src/…/destructive-command-rules.ts• L419-511 src/…/destructive-command-rules.ts• L544-625 src/…/secret-protection-rules.ts• L21-179 src/…/secret-protection-rules.ts• L183-500
Custom rule behavior
Custom rules add local or shared command blocks without changing the built-in rule set. A version 1 match checks the normalized command, its optional subcommand, and its listed block arguments. A version 2 match checks an exact command path and can require or exclude listed arguments.
The matcher skips known global options when it finds the command path or subcommand. It does not guess the value of an unknown option. This keeps a custom rule from blocking a command when the input does not provide enough information to prove the configured match.
When a custom rule matches, the result carries its namespaced rule ID and its reason and intent. Custom rules do not replace path analysis or built-in protections. Both rule types contribute to the same effective policy that command analysis consumes.
References: src/…/custom.ts• L7-47 src/…/custom.ts• L50-96 src/…/custom.ts• L102-190 src/…/custom-match-options.ts• L1-45 src/…/custom-subcommand.ts• L1-22
Configuration failures
Policy loading favors a safe and useful runtime state over silently accepting invalid data. Diagnostics identify the source that needs repair while the snapshot records whether the runtime state is ready or degraded.
Fallback behavior
An invalid policy file keeps recognized valid sections when possible and replaces invalid sections with protective defaults. An empty or unreadable file uses built-in protective defaults. A bad rule configuration or rulebook source is not enforced. Other active sources continue to provide rules.
The rule manager treats a rules configuration with read errors as a failed scope operation. When no errors exist but no configuration is present, it uses the default empty rule set. A new scope can also receive a known starter rulebook that demonstrates the expected project rule shape.
A missing or invalid remote rulebook file leaves that source inactive until it is repaired or updated. A malformed rulebook does not become active through a loose read. A source with an invalid name, mismatched rulebook identity, duplicate rule names, undeclared commands, or failed version 2 fixtures is rejected.
This split matters for safety outcomes. A configuration error can make the system degraded without denying ordinary work. It cannot turn malformed policy data into an unbounded allow or activate unverified rules.
References: src/…/snapshot.ts• L77-119 src/…/config-file.ts• L6-44 src/…/scope-policy.ts• L256-289 src/…/rulebook.ts• L49-87 src/…/resolver.ts• L228-235
Change checklist
When these files or sections are changed, remember to consider these:
- Keep policy path roots, filesystem scope labels, config paths, and local rulebook paths aligned across user and project operations.
- Keep filesystem boundary checks aligned with every policy read, write, sync, vendoring, source removal, and cache-free maintenance path.
- Keep rulebook source identity, rulebook names, source directories, and the configured
ruleslist aligned so runtime loading reads the intended file. - Keep source discovery, selected repository rulebooks, remote ref handling, and vendored file writes aligned so
rule addreports the content it activates. - Keep atomic config and rulebook writes, runtime reload validation, and rollback behavior aligned so a reported sync result matches the state the guard will use.
- Keep user/project precedence rules aligned with override validation and namespaced rule metadata used in explanations.
- Keep version 1 and version 2 rule shapes aligned with their matcher semantics and fixture validation boundaries.
- Keep built-in rule metadata, capability gates, catastrophic safeguards, default-off rule IDs, and override validation aligned when protection coverage changes.
- Keep path exception validation aligned with the path scanners so a saved exception has the same scope at evaluation time. In particular, keep recursive secret allow syntax narrow and preserve the home and guard-configuration exclusions.
- Keep GitHub response caps, shared request and byte budgets, and the sync concurrency limit aligned so discovery and fetch paths enforce one resource policy.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Configured remote rulebook file is missing | The affected source contributes no custom rules and reports an update repair | Runtime reads the vendored file and directs the operator to rule update |
| Vendored file name does not match the configured source or rulebook name | The affected source is rejected | Source parsing and rulebook validation require one consistent name |
| Two configured sources claim the same rulebook name | The conflicting remote source is not written, or the later loaded scope contributes no rules | Sync rejects file-path collisions and scope loading keeps the first name claim |
| Project override targets a user rule | Only that project override is ignored | Scope loading rejects cross-scope override targets |
| Malformed policy field or policy JSON | Valid policy parts may remain active with protective defaults and degraded status | Policy validation and field-level normalization prevent invalid values from becoming active |
| Policy data path uses a link or resolves outside its scope | The policy operation fails instead of reading or changing an uncontrolled path | Scoped filesystem access checks links and canonical containment |
| Broad secret allow path, unsupported glob, or path that covers home or guard configuration | The exception is rejected instead of disabling secret protection | Allow-path validation permits an exact path or one exact file after **/ and blocks dangerous roots |
| Rulebook exceeds validation limits or a version 2 fixture fails | The rulebook is rejected before it becomes active | Acceptance limits and fixture evaluation bound and verify rulebook content |
| Rule sync exceeds source, request, concurrency, time, or byte limits | Synchronization fails without accepting an unbounded remote input | Shared rule-sync resource budgets and response caps abort the operation |
Key files
| File | Why to read it |
|---|---|
| src/…/snapshot.ts | Shows how policy and rule sources become the immutable evaluation context. |
| src/…/env.ts | Explains safety-level expansion and environment precedence. |
| src/…/allow-paths.ts | Defines the accepted path forms and the exclusions for destructive and secret exceptions. |
| src/…/paths.ts | Shows how user and project policy locations map to config and vendored rulebook data. |
| src/…/filesystem.ts | Shows the safety boundary used for policy data access and maintenance. |
| src/…/scope-policy.ts | Shows user/project loading, precedence, overrides, and diagnostics. |
| src/…/source-syntax.ts | Defines local and GitHub rulebook source identity. |
| src/…/config-file.ts | Shows the default rules configuration and starter rulebook shape used for setup. |
| src/…/paths.ts | Shows how each scope binds its configuration and lock paths to a safe filesystem boundary. |
| src/…/resolver.ts | Shows how local and GitHub sources become validated rulebooks and how remote input is bounded. |
| src/…/sync.ts | Shows source add, update, vendoring, rollback, and runtime verification. |
| src/…/resource-limits.ts | Defines the shared request and response bounds for rule synchronization. |
| src/…/rulebook.ts | Shows rulebook schema validation and acceptance checks. |
| src/…/rulebook-fixtures.ts | Shows how version 2 rulebook fixtures verify matching behavior. |
| src/…/destructive-command-rules.ts | Describes built-in destructive protections and their capability gates. |
| src/…/custom.ts | Shows how a custom rule becomes a command match. |