Rulebook resolution and customization
Rulebooks extend CC Safety Net with named command rules that can be shared by a user or a project. The runtime combines these rules with built-in destructive-command and secret-protection rules, then uses only verified rulebook content for each safety evaluation. This page complements Policy and rule management and Policy storage and snapshots.
Rule families
Built-in rules protect common high-risk actions without requiring a rulebook. Custom rulebooks add local or shared restrictions for commands that are important to a team or workspace.
Built-in protections
Built-in destructive-command rules cover unsafe Git state changes, file removal, device writes, secure erase, network access to cloud metadata, remote uploads, namespace creation, interpreter commands, and dynamic command forms. Catastrophic rules remain active for targets such as root, home, and Git metadata. Other rules follow the selected safety capabilities, such as strict fail-closed checks or paranoid removal and interpreter checks.
The rule catalog also groups common display and request-client commands for command analysis. These groups help the safety system distinguish commands that inspect output from commands that can contact or change external systems.
Secret protection covers common credential and secret locations. Its rule families include exact names such as .env, private-key names, and credential files; home paths such as SSH, AWS, GCP, Kubernetes, Docker, and GitHub CLI data; credential-file extensions; credential variants; and coding-CLI credential stores. Mixed coding-CLI configuration files are off by default because an agent may need to edit them during normal work. An explicit rule override can enable that tier.
References: src/…/constants.ts• L14-19 src/…/constants.ts• L21-135 src/…/destructive.ts• L15-282 src/…/destructive.ts• L285-457 src/…/destructive.ts• L468-638 src/…/secret.ts• L21-92 src/…/secret.ts• L120-179 src/…/secret.ts• L183-372 src/…/secret.ts• L374-504
Custom rulebooks
A custom rulebook is a named and versioned group of command rules. Version 1 uses the original custom-rule shape, while version 2 describes matching through a structured command pattern. Both versions must declare their commands in allowed_commands, which keeps each rulebook within an explicit command set.
Custom matching normalizes the executable name and applies the rule's command pattern. Rules are considered in rulebook order, so the first match supplies the namespaced ID, reason, and optional intent. Custom rules add restrictions; they do not replace built-in analysis or path protection.
Rulebook fixtures document expected blocked or allowed commands. For version 2 rulebooks, synchronization parses each fixture command and checks that a blocked fixture matches its named rule first or that an allowed fixture matches no rule. The command is never executed. Version 1 fixtures receive shape validation only.
References: src/…/rulebook.ts• L22-54 src/…/rulebook-fixtures.ts• L11-46 src/…/rulebook.ts• L134-177 src/…/custom.ts• L6-46 src/…/custom.ts• L49-174
Configuration sources
Rule configuration selects rulebook sources. Rulebook content lives in separate rulebook.json files, so a configuration file can select a local rulebook or a remote rulebook without embedding its rules.
Source formats
Local sources are bare names such as project-rules. The matching file is <rules directory>/project-rules/rulebook.json. GitHub sources use owner/repo#ref/<rulebook-name>, where ref is one or more valid path segments such as a branch, tag, or commit name. A GitHub repository source such as owner/repo can also be inspected to discover all rulebooks below .cc-safety-net/rules/.
The source name fixes the rulebook identity. The local directory or GitHub path and the name field inside the rulebook must agree. Source names must be unique within one configuration, and rulebook names must be valid and unique within the active policy namespace.
References: src/…/source-syntax.ts• L1-15 src/…/source-syntax.ts• L26-68 src/…/resolver.ts• L109-155 src/…/rules-config.ts• L216-230 src/…/rulebook.ts• L36-47
Configuration files
The user and project scopes each have a rule.json that lists sources. By default, user rules are selected from ~/…/rule.json, while project rules are selected from .cc-safety-net/…/rule.json. Each scope keeps its own rule.lock beside its rule.json. Cache data is kept in the matching safety-net cache area. CC_SAFETY_NET_HOME can select the user safety-net home.
| Scope | Source list | Local rulebook location | Lock location |
|---|---|---|---|
| User | ~/…/rule.json | ~/…/rulebook.json | Beside the user rule.json |
| Project | .cc-safety-net/…/rule.json | .cc-safety-net/…/rulebook.json | Beside the project rule.json |
| GitHub | A source in either scope's rule.json | .cc-safety-net/…/rulebook.json in the source repository | The consumer scope's rule.lock |
Policy files and cached rulebooks are read and written inside their bound scope. The policy file layer rejects path escapes and symbolic-link paths, checks files before and after access, and publishes writes atomically. These safeguards keep a source or concurrent filesystem change from redirecting policy operations outside the intended area.
The overrides object can disable a custom rule with "off" or replace its reason and intent. The transparent_wrappers list identifies commands that expose a protected child command for analysis; it does not create a custom block. The rule verify command can validate local rulebooks and add missing schema metadata to a valid source list.
Legacy inline files such as .safety-net.json and ~/…/config.json are not active at runtime. rule migrate converts their rules into a rulebook and source list. Cleanup deletes the old file only after the migrated rulebook has been checked.
References: src/…/paths.ts• L12-18 src/…/paths.ts• L38-75 src/…/paths.ts• L78-117 src/…/rules-config.ts• L14-27 src/…/rules-config.ts• L139-170 src/…/verify.ts• L99-188 src/…/migrate.ts• L46-147
Scope and locking
Scope determines which users or projects can provide a rule and how the runtime proves that the selected content has not changed. User rules load before project rules, while each scope keeps its own source list and lock state.
Scope precedence
The loader reads user and project configurations separately, then joins the active rules. Sources apply in configuration order within a scope. If two active rulebooks use the same name, the first claim wins. Because the user scope loads first, a user rulebook name shadows a project rulebook with that name; the later rulebook contributes no rules and produces a warning.
Overrides are local to the scope that owns the rule. An override of off removes only that rule. An object override changes its block reason and optional intent. An unknown key or a project attempt to target a user rule is ignored for that key and reported as a warning. Other valid rules remain active.
The final rule set contains built-in protections plus active custom rules. Built-in catastrophic protections are not removed by the destructive-command master switch or by custom rule configuration.
References: src/…/scope-policy.ts• L39-123 src/…/scope-policy.ts• L175-230 src/…/scope-policy.ts• L335-375 src/…/destructive.ts• L225-240 src/…/destructive.ts• L285-310
Lock and cache contract
Synchronization creates a lock entry for each configured source. The entry records the source identity, rulebook name, version, and content digest. A GitHub entry also records the repository, requested ref, resolved commit, and rulebook path. The cache stores the exact content represented by that entry.
At runtime, a configured source is active only when its lock entry is valid, its cache content exists, the cache digest matches, and the cached rulebook passes validation. A local edit therefore does not change active behavior until a successful sync publishes a new digest. A missing lock entry, missing cache, digest mismatch, or invalid cached rulebook drops only that source. Built-in protections and other valid sources continue to apply.
The lock is a pin for runtime loading, not a request to fetch on every command. Normal snapshot loading is read-only and uses the verified vendored file. Synchronization reuses that file when it is present, fetches a missing GitHub file during a normal sync, and fetches selected sources again during a refresh or update. It verifies the resulting content before publication, so normal evaluation does not need network access.
References: src/…/types.ts• L6-37 src/…/lockfile.ts• L11-85 src/…/paths.ts• L78-117 src/…/resolver.ts• L71-107 src/…/resolver.ts• L203-235 src/…/scope-policy.ts• L251-320 src/…/snapshot.ts• L16-69
Resolution and synchronization
Resolution turns a source string into verified rulebook content. Synchronization performs that work before runtime use and publishes the cache and lock as one policy update.
Source resolution
When a source is resolved for synchronization, the system follows a bounded sequence:
- A local source is read from its named rulebook directory. Its name must match the source name.
- A GitHub source resolves the requested ref to a commit, fetches the rulebook at the expected repository path, and checks the returned rulebook name.
- The content is parsed and checked against the rulebook contract. Version 2 fixtures are also checked against the rulebook's own matching behavior.
- Synchronization records the source identity and SHA-256 content digest in the lock entry.
For a normal sync, an existing GitHub vendored file is used without a network request. A missing file is fetched so the scope can converge. refresh resolves and fetches the selected source again. The requested GitHub ref remains useful for display, but the commit and digest identify the content that runtime loading accepts.
References: src/…/resolver.ts• L56-87 src/…/resolver.ts• L90-107 src/…/resolver.ts• L185-235 src/…/resolver.ts• L245-267 src/…/resolver.ts• L270-364
Synchronization actions
The rule commands provide separate actions for selecting sources and making them active:
| Action | System effect |
|---|---|
rule init | Creates an inert scope configuration and cache area. An example can add a local starter rulebook without selecting it. |
rule add <source> | Adds a source, then resolves and synchronizes it. A GitHub repository source can add its discovered rulebooks. |
rule sync | Resolves all configured sources and publishes their cache and lock state. |
rule update [source] | Synchronizes one selected source or rulebook. |
rule sync --check | Checks the selected scope without changing cache or lock state. |
rule remove <source> | Removes a source and rebuilds the lock; --delete-source can remove a clean local source directory. |
rule verify | Checks configuration and local source content without fetching remote content. |
Sync writes new vendored content before publishing the lock. It then reloads the synchronized scope through the runtime checks, so a published policy cannot report success while its overrides or source state still produce runtime errors. A normal sync stops on a source failure and keeps the prior published state. A selective refresh reports a fetch or validation failure for that source while keeping its previous vendored copy, so other selected sources can still update. Resource-limit failures remain fatal for the whole operation. If publication or later cleanup fails, it restores the prior policy state when possible. Cache directories that no longer belong to the lock may be pruned, but a failed prune does not make the newly synchronized rule set inactive.
Adding a GitHub repository source can discover all rulebooks below .cc-safety-net/rules/ or select a named subset. Synchronization refuses two resolved sources that would write the same rulebook file, and it refuses to overwrite an existing unclaimed file when a new source is added. These checks protect content whose origin is not known to the source list.
Removing a local source deletes only the approved rulebook.json and removes its directory only if it is still empty. A concurrent extra file therefore remains in place and causes the cleanup to fail rather than causing an unrelated file to be swept away.
References: src/…/sync.ts• L69-121 src/…/sync.ts• L123-255 src/…/sync.ts• L304-424 src/…/sync.ts• L446-504 src/…/sync.ts• L534-641 src/…/sync.ts• L643-739 src/…/sources.ts• L16-119
Resource limits
Limits keep rulebooks and remote sources bounded because rule content and remote responses are not trusted inputs. A limit failure rejects the operation before it can publish an over-sized or over-fanned-out rule set.
Source and fetch limits
One sync operation accepts at most 64 sources. It resolves at most four sources at the same time, permits at most 131 remote requests, and reads at most 64 MiB across remote response bodies. Each remote response also has a class-specific cap and a 15-second request timeout.
| Remote response | Maximum size |
|---|---|
| Repository metadata | 512 KiB |
| Commit response | 256 KiB |
| Repository tree | 16 MiB |
| Rulebook content | 4 MiB |
The request and byte budgets are shared across repository discovery, commit resolution, tree inspection, and rulebook fetches in one operation. Response bodies are consumed under their caps instead of trusting a declared size. A resource-limit failure aborts the remaining work and leaves the prior published state in place. Other source failures follow the synchronization action: a full sync fails as a whole, while a selective refresh can keep the old copy for the failed source and report the failure.
References: src/…/resolver.ts• L44-53 src/…/resolver.ts• L270-364 src/…/sync.ts• L156-183 src/…/sync.ts• L471-503 src/…/resource-limits.ts• L1-2
Rulebook validation limits
Each rulebook must fit safe validation bounds before it can enter the cache or lock. The bounds cover collection size, total text size, and the token lists used by version 2 match patterns.
| Rulebook item | Limit |
|---|---|
| Allowed commands | 1,024 |
| Rules | 1,024 |
| Test fixtures | 2,048 |
| Block arguments per rule | 1,024 |
| Block arguments across the rulebook | 16,384 |
| One string | 1 MiB |
| All strings together | 4 MiB |
| One fixture command | 128 KiB |
| Reported validation errors | 64 |
Fixtures help authors describe intended behavior, but they do not expand the accepted resource budget. Version 2 fixtures are parsed for matching checks, but their commands are not executed.
References: src/…/rulebook-limits.ts• L1-14 src/…/rulebook-limits.ts• L16-110 src/…/rulebook-fixtures.ts• L11-46 src/…/rulebook.ts• L68-87
Change checklist
When these files or sections are changed, remember to consider these:
- Keep source syntax, rulebook names, lock entry identity, and cache addressing aligned. A source that resolves during sync must also be accepted by runtime loading.
- Keep rulebook validation,
allowed_commands, namespaced rule IDs, and override-key validation aligned. A valid rule that cannot be named by an override is an incomplete contract. - Keep user-first scope loading aligned with duplicate-name handling and cross-scope override diagnostics. A change in order can change which rulebook is active.
- Keep sync publication, post-sync runtime verification, and runtime cache checks aligned. The lock must never describe content that the cache does not contain and digest-verify.
- Keep policy filesystem boundaries aligned with every path used for configs, locks, local sources, caches, cleanup, and rollback. A new path must not bypass the same containment and atomic-write safeguards.
- Keep built-in rule metadata, destructive rule IDs, secret rule IDs, default-off secret rules, and policy schema checks aligned.
- Keep source, request, response-byte, concurrency, and rulebook validation limits shared across add, update, sync, and repository discovery paths.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Configured source has no lock entry or its lockfile is malformed | That scope cannot activate the affected custom source | Run cc-safety-net rule sync after repairing the source or lock |
| Cache is missing, changed, or invalid | The affected rulebook is dropped during runtime loading; sync can restore a locked GitHub cache only when its pinned content and digest still match | Runtime checks the cache digest and rulebook schema; sync verifies restored content |
| Local rulebook is edited without sync | The last verified cached content remains active | Run cc-safety-net rule sync to publish the edit |
| User and project rulebooks claim the same name | The later rulebook contributes no rules | The first scope claim wins and the loader reports a warning |
| Project config overrides a user rule | That override is ignored; the user rule keeps its state | Overrides are validated against the owning scope |
| Policy path uses a symbolic link or escapes its bound scope | The policy operation fails instead of reading or writing the redirected path | Scoped filesystem access checks containment and rejects links |
| Legacy inline rules are left unmigrated | Their custom blocks are inert | Run cc-safety-net rule migrate |
| Sync exceeds source, request, response, or rulebook limits | Sync fails without publishing the oversized result | Shared budgets and rulebook acceptance limits reject the operation |
| GitHub ref or returned rulebook identity does not match the source | The source cannot be locked or activated | Resolution checks repository identity, path, name, and digest |
A local source gains extra files during --delete-source | Source removal stops and the extra file remains | Cleanup rechecks the directory and removes only the approved rulebook file |
Key files
| File | Why to read it |
|---|---|
| src/…/constants.ts | Defines command groups used to classify shell wrappers, interpreters, display commands, and request clients. |
| src/…/destructive.ts | Defines the built-in destructive rule families and their capability and catastrophic behavior. |
| src/…/secret.ts | Defines the built-in secret, credential, and coding-CLI protection families. |
| src/…/rulebook.ts | States the high-level rulebook shape and acceptance checks. |
| src/…/rulebook-fixtures.ts | Shows how version 2 fixture commands check rule order and matching without execution. |
| src/…/rules-config.ts | Defines source lists, overrides, wrapper settings, and their validation contract. |
| src/…/scope-policy.ts | Explains user/project loading, precedence, overrides, and runtime diagnostics. |
| src/…/paths.ts | Maps user and project scopes to config and local rulebook locations. |
| src/…/resolver.ts | Explains local and GitHub source resolution, bounded fetches, and content validation. |
| src/…/sources.ts | Explains how update and remove commands map user input to configured sources. |
| src/…/sync.ts | Explains add, sync, update, remove, vendoring, runtime verification, and rollback behavior. |
| src/…/resource-limits.ts | Defines the configured source limit used during synchronization. |
| src/…/rulebook-limits.ts | Defines safe rulebook validation bounds. |
| src/…/snapshot.ts | Shows how verified custom rules join built-in policy for one evaluation. |