Policy storage and snapshots
Policy storage gives the guard one trusted description of safety settings, path exceptions, and custom rules. The system reads this data from user and project scopes, checks it before use, and turns it into an immutable snapshot that command analysis can use with one fixed context.
This page complements Policy and rules and Rulebook resolution and customization.
Policy model
User policy schema
The user policy is a versioned JSON document for machine-wide safety choices. It groups safety mode, worktree behavior, protection switches, path exceptions, and audit history settings so that the GUI and the runtime share one contract. The current schema accepts version 1 and reports unknown or invalid fields while preserving other valid sections.
| Section | Purpose | Main values |
|---|---|---|
safety | Selects the protection level and optional capability changes | standard, strict, paranoid; fail_closed, paranoid_rm, paranoid_interpreters |
workflow | Changes worktree-aware command handling | worktree_mode |
destructive_command_protection | Controls destructive-command checks | enabled, rule overrides, allow_paths |
secret_protection | Controls secret-file checks | enabled, rule overrides, deny_paths, allow_paths |
audit | Sets the retained audit history window | retention_days |
The normal policy shape fills omitted values with protective defaults. Protection is enabled by default, the safety level is standard, worktree mode is off, and audit history is retained for 30 days. Invalid fields do not become active. A readable document can keep valid sections while invalid parts use their defaults.
References: src/…/store.ts• L52-75 src/…/store.ts• L253-277 src/…/store.ts• L283-377 src/…/types.ts• L41-68
Rule configuration
Custom rule selection is stored separately from the user policy. Each user or project rules configuration names rulebook sources, optional rule overrides, and transparent command wrappers. The runtime loads the named rulebooks from the scope's local policy area and records their source, name, version, and rule names in the loaded policy.
The project scope takes effect beside the user scope. User rulebooks are loaded first, so a duplicate active rulebook name leaves the later rulebook inactive. A project override cannot target a user-scoped rule. An invalid or missing rulebook is omitted and reported without removing other valid rulebooks.
References: src/…/rules-config.ts• L14-56 src/…/rules-config.ts• L59-95 src/…/scope-policy.ts• L37-121 src/…/scope-policy.ts• L173-222 src/…/scope-policy.ts• L243-275
Environment and path context
Safety environment
Environment values provide per-process safety context on top of the stored policy. The configured safety level and CC_SAFETY_NET_LEVEL combine by choosing the stronger level. Boolean environment flags can then force stricter capabilities on. Policy capability overrides shape the selected level before those force-on flags apply.
The main environment controls are:
CC_SAFETY_NET_LEVELselects a safety level.CC_SAFETY_NET_STRICTandCC_SAFETY_NET_PARANOIDare legacy-compatible force-on forms.CC_SAFETY_NET_PARANOID_RMandCC_SAFETY_NET_PARANOID_INTERPRETERSforce their named checks on.CC_SAFETY_NET_WORKTREEenables worktree mode for the process.CC_SAFETY_NET_AUDIT_SCOPEselectsallorblockedaudit recording. Denials remain recorded in either mode.
The resolved result keeps the source of each capability. Status, explanation, and guard results can therefore show whether a setting came from the policy preset, a policy override, or the environment.
References: src/…/env.ts• L14-25 src/…/env.ts• L49-56 src/…/env.ts• L68-163
Allow and deny paths
Path lists limit where a protection rule may make an exception or add extra coverage. The system validates these paths before saving and removes unusable entries when it repairs a policy. Secret allow paths may also use one constrained recursive form: a literal folder followed by **/ and an exact file name, such as ~/…/.env.local.
| Setting | Meaning and boundary |
|---|---|
Destructive allow_paths | Allows configured destructive actions for literal absolute paths or paths rooted with ~/. It cannot cover the home directory or a path above it. |
Secret deny_paths | Adds secret protection coverage. Relative entries resolve from the session configuration directory. Absolute entries cannot cover the home directory or a path above it. |
Secret allow_paths | Vouches for exact user-managed files or directories. It accepts relative or absolute entries, rewrites $HOME forms to ~, and rejects unsupported wildcards except for the constrained recursive form. It cannot cover the home directory, a parent of home, or the guard's own configuration. |
These limits prevent one exception from silently disabling protection across every workspace. A path that fails validation is not active, even if other entries in the same list remain usable.
References: src/…/allow-paths.ts• L7-91 src/…/store.ts• L464-483
Policy storage
User and project locations
The editable user policy is policy.json beside the user rules directory. By default, the user rules directory is ~/.cc-safety-net/rules, so the policy is ~/…/policy.json. CC_SAFETY_NET_HOME, an explicit user configuration directory, or an explicit user configuration path can change this location.
Project rule configuration lives under the current project at .cc-safety-net/…/rule.json, and project policy lives at .cc-safety-net/policy.json. Cached or vendored rulebooks are resolved from the matching scope. The path layer also creates a protected filesystem scope for each configured location so that reads and writes use the same boundary.
The storage layer treats these locations as protected policy files. Each read, write, directory scan, or cleanup operation stays inside its bound scope and rejects links, parent traversal, and paths that resolve outside that scope. Policy JSON is written through an atomic replacement with private file permissions. Access checks also detect changes during an operation, so a policy source is not trusted when its filesystem state cannot be checked safely.
References: src/…/paths.ts• L12-36 src/…/paths.ts• L38-115 src/…/safe-read.ts• L1
Policy reads and repair
Policy reads separate diagnosis from the usable policy. Missing user policy means default settings. Invalid JSON uses protective defaults. Readable JSON with field errors keeps valid recognized sections and replaces the rest with protective defaults, then reports a degraded configuration state.
The GUI writes only a schema-valid normalized policy. The runtime can still load a partially damaged file so that one bad setting does not remove protections supplied by other valid settings. A real local policy file also takes precedence over an embedded policy snapshot.
The GUI and runtime use one salvage pass for a damaged policy. That pass returns both the normalized policy and the ordered diagnostic messages, which keeps status, repair, and draft views aligned.
References: src/…/store.ts• L268-377 src/…/store.ts• L534-575 src/…/store-gui.ts• L30-96 src/…/user-policy-diagnostics.ts• L1-22
Policy drafts and diffs
The GUI compares a current normalized policy with a proposed policy as a set of field-level rows. Unchanged fields are omitted, lists use a stable display form, and audit retention is excluded from project-scope comparisons because audit is user scope only. Project policy files preserve only the non-audit policy sections that the project scope can own.
The runtime baseline comes from the local user policy when it exists. If it does not, the comparison uses the embedded policy or protective defaults. The same salvage pass supplies the normalized baseline and its diagnostics, so a draft review can distinguish a real change from a repaired source.
References: src/…/diff.ts• L8-48 src/…/diff.ts• L50-85 src/…/diff.ts• L87-100 src/…/user-policy-diagnostics.ts• L8-17
User and project policy composition
The effective policy combines a machine-wide user baseline with an optional project policy. Project scalar values replace user values when present. Rule overrides are overlaid by key, and path lists are combined as a de-duplicated union. Audit settings remain from the user policy.
Project policy can weaken a user baseline. The loader records a lower safety level, disabled capability or protection, disabled rule, or added allow path in the policy scope metadata. A project audit section produces a diagnostic rather than changing the effective retention. Status and explanation surfaces can therefore show how the project changes the user settings.
References: src/…/merge.ts• L30-127 src/…/store.ts• L77-109 src/…/store.ts• L111-213 src/…/types.ts• L147-154
Evaluation snapshots
Snapshot assembly
An evaluation snapshot is the complete policy context for one analysis pass. The snapshot loader combines the active user and project rules with the effective safety policy, path lists, protection switches, transparent wrappers, and metadata for each active custom rule. This gives analysis one policy object instead of separate sources to resolve during the pass.
The loader also carries policy-scope information and rule override details into the snapshot. Rule metadata links an active rule to its rulebook and public source when that information is safe to expose. This supports status and explanation views without changing the rules used for evaluation.
The snapshot loader is read-only. It does not fetch sources, write files, or reuse an earlier in-memory result. A locked or local rulebook is read from verified policy content, so a change on disk is detected as a load or validation problem instead of silently changing the rules used for evaluation.
The evaluation context moves through the system as follows:
flowchart LR
A[User policy] --> C[Snapshot loader]
B[Project rules and verified rulebook content] --> C
C --> D[Policy snapshot]
E[Process environment] --> F[Effective capabilities]
D --> G[Command analysis]
F --> G
G --> H[Guard decision and audit record]
References: src/…/snapshot.ts• L16-70 src/…/scope-policy.ts• L37-121 src/…/scope-policy.ts• L315-340 src/…/policy.ts• L82-110 src/…/analysis.ts• L48-69
Snapshot immutability
The snapshot is deeply frozen after the loader clones the policy and scope data. This prevents a later command, GUI draft, or caller from changing the context after analysis starts. The analyzer receives the snapshot explicitly instead of reloading configuration during the same evaluation.
The snapshot has two states. ready means the sources loaded without diagnostics. degraded means some rule or policy configuration failed, but the snapshot still contains the policy that can be enforced and explains what was dropped or replaced. A dropped rule source does not remove other active rules or built-in protections. Invalid policy values are not active; the snapshot uses the salvaged policy or built-in protective defaults instead.
References: src/…/snapshot.ts• L78-105 src/…/snapshot.ts• L113-144 src/…/policy-context.ts• L16-43 tests/…/policy-snapshot.test.ts• L139-150
Embedded policy snapshot
An installed Amp plugin can carry a normalized policy snapshot for a machine that has no local policy file. The runtime reads this embedded value only when the local user policy is absent, then resolves home-relative paths against the current machine. A local file wins, and a malformed embedded value falls back to protective defaults without creating an editable-file diagnostic.
References: src/…/store.ts• L603-640 src/…/install.ts• L27-30 src/…/embedded-policy.test.ts• L48-89
Audit retention
Retention setting
audit.retention_days controls how much audit history remains available to the CLI and GUI. It must be a whole number from 1 through 365. The default is 30 days. An unusable value uses the default during normalization or direct retention lookup, while an out-of-range integer is bounded to the supported window when the policy is repaired.
The audit scope is independent of retention. CC_SAFETY_NET_AUDIT_SCOPE=blocked records denials only, while all also records allowed commands. The logs command does not let a query ask for more days than the configured retention window.
References: src/…/audit-retention-days.ts• L1-10 src/…/store.ts• L424-434 src/…/env.ts• L49-56 src/…/audit-log.ts• L37-80
Audit storage and cleanup
Audit records are JSON Lines files below ~/.cc-safety-net/logs. The directory groups records by an encoded working directory and UTC month, with a dated file for each session. Records are sanitized and bounded before they are written.
Retention cleanup runs opportunistically after writes and before reads. It removes fully expired dated files and old legacy files, runs at most once per UTC day per audit root, and never changes or blocks the guard decision if cleanup fails. The CLI and GUI therefore expose retained history rather than an unbounded archive.
References: src/…/audit.ts• L55-75 src/…/audit.ts• L86-146 src/…/audit.ts• L174-175 src/…/audit-retention.ts• L31-95 src/…/audit-log.ts• L149-177
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the user policy schema, GUI policy shape, normalizer, project projection, and camelCase runtime projection aligned. A new field that reaches only one of these layers will be ignored or absent from snapshots.
- When changing policy or rule locations, update snapshot loading, audit retention lookup, cache or vendored rulebook lookup, migration cleanup, and the CLI and GUI history readers together.
- When changing a snapshot field, update its clone and deep-freeze path and every analysis entry point that consumes the explicit snapshot.
- When changing safety precedence, keep the preview, explain flow, guard flow, and capability source reporting consistent with the environment resolver.
- When changing path rules, preserve the distinction between session-relative deny paths and literal allow paths. Recheck that broad exceptions cannot cover home or the guard configuration. Also keep the constrained recursive secret allow form aligned across validation and use.
- When changing rulebook source or local rulebook layout, keep rules-config validation, scope loading, rulebook metadata, and snapshot rule IDs in agreement.
- When changing project composition, keep merge output, weakening metadata, policy scope labels, and draft diff behavior in agreement.
- When changing a policy location or its configured scope, keep target derivation and filesystem containment checks aligned so reads and writes use the same protected root.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Policy JSON is malformed | The full user policy uses protective defaults and the snapshot is degraded | Repair or replace the policy file manually |
| A policy field is invalid | Valid sections remain active, but the invalid field uses its protective default | Follow the field diagnostic and save a schema-valid policy |
| A project override targets a user-scoped rule | Only that override is ignored and the rule keeps its user-configured state | Remove the project override or change the rule in its owning scope |
| A project policy lowers safety or adds an allow path | The project-specific effective policy is weaker and the weakening is reported | Review the project diff and policy scope information |
| A rule source is invalid, missing, duplicated, or its rulebook name does not match | That rulebook is dropped; built-in protections and other valid sources remain active | Fix the local rulebook or remove the source from the rules config |
| A policy or rule path is a link or leaves its protected filesystem area | The source is rejected before its bytes are trusted | Restore a normal file or directory inside the policy area |
| A policy filesystem entry changes during a read or write | The operation fails closed instead of trusting a possibly replaced file | Restore stable files inside the configured policy scope |
| An allow path covers home, a parent of home, or the guard configuration | The path entry is rejected and cannot disable protection broadly | Use an exact user-managed file or directory |
| A secret allow path uses an unsupported wildcard pattern | The path entry is rejected and does not create a broad exception | Use a literal path or a folder followed by **/ and an exact file name |
| Retention is missing, malformed, or outside the supported window | Cleanup uses a bounded retention value, normally 30 days | Set an integer from 1 through 365 |
| No local user policy exists on an embedded-policy install | The stamped policy supplies the evaluation settings | Install or repair the local policy when machine-specific settings are needed |
Key files
| File | Why to read it |
|---|---|
| src/…/store.ts | Defines the versioned policy contract, defaults, normalization, repair, project projection, and fallback behavior. |
| src/…/store-gui.ts | Shows GUI policy reads, validation, atomic writes, previews, and repair. |
| src/…/user-policy-diagnostics.ts | Keeps policy salvage and diagnostic messages aligned across runtime and GUI surfaces. |
| src/…/types.ts | Defines the policy, effective policy, scope, and snapshot contracts. |
| src/…/env.ts | Defines environment inputs and safety precedence. |
| src/…/allow-paths.ts | Defines the safety boundaries for allow and deny path entries, including the constrained recursive secret exception. |
| src/…/diff.ts | Defines policy baselines and field-level draft diffs. |
| src/…/merge.ts | Defines user and project policy composition and weakening reports. |
| src/…/paths.ts | Maps user and project policy locations and protected scopes. |
| src/…/rules-config.ts | Defines the rules configuration shape and validation diagnostics. |
| src/…/scope-policy.ts | Explains how user and project rule sources become active rules. |
| src/…/snapshot.ts | Assembles and freezes the evaluation context. |
| src/…/audit-retention-days.ts | Defines the audit retention range and fallback behavior. |
| src/…/audit-retention.ts | Defines retention lookup and opportunistic cleanup. |
| src/…/audit.ts | Defines audit record storage and write-time retention behavior. |