Introducing a new way to review your PRs.

Read the announcement

kenryu42 / cc-safety-net

A pre-execution guard for AI coding agents. It blocks destructive Git and file system commands, plus common attempts to access sensitive files, before a tool call runs. Supports Amp Code, Antigravity CLI, Claude Code, Codex, Cursor, DeepSeek Harness, Devin CLI, GitHub Copilot CLI, Grok Build, Hermes Agent, Kimi Code, OpenClaw, OpenCode, and Pi.

View on GitHub

20 documents · Updated Nov 23, 2026

docs / programmatic-safety-api

Programmatic safety API

The programmatic safety API gives library callers a direct way to ask whether one shell command may run under the current CC Safety Net policy. It uses the same policy, path protection, and command analysis pipeline as guarded tool calls, but it does not need an agent hook and does not execute the command or record an audit event.

Public interface

The public interface is the cc-safety-net/api package subpath. It exposes checkCommand and its input and result types without requiring the optional OpenCode plugin dependency. The package root remains the plugin entry point, so library callers should import the API from the dedicated subpath.

import { checkCommand, type CheckCommandResult } from 'cc-safety-net/api';

const result: CheckCommandResult = checkCommand({
  command: 'git status',
  cwd: '/path/to/project',
});

References: src/api.ts• L8-23 package.json• L8-17 tests/…/public-api.test.ts• L73-95

Input contract

CheckCommandInput requires a non-empty command string and an absolute cwd string. The working directory is both the project context used to load local policy and the execution context used to interpret relative paths and command behavior.

The API normalizes the path without first resolving it through a real path lookup. It then requires a readable and searchable directory. A missing path, a file, or an inaccessible directory returns a fail-closed denial rather than checking a different project context.

FieldContractSystem meaning
commandNon-empty stringShell text to check.
cwdAbsolute path to a usable directoryPolicy location and command working directory.

References: src/api.ts• L8-42 src/…/cwd-containment.ts• L5-15 tests/…/check-command.test.ts• L105-135

Decision contract

CheckCommandResult is a small projection of the shared safety decision. An allow contains only kind: 'allow'. A deny contains kind: 'deny', a user-facing reason, and an optional ruleId that identifies the matched built-in or custom rule.

The API does not expose the internal block intent, evidence, evaluation stage, policy level, or diagnostic trace. Callers that need a detailed explanation should use the separate explanation surfaces rather than infer those fields from the API result. See Safety decisions, explanations, and audit trail.

if (result.kind === 'deny') {
  console.error(result.reason, result.ruleId);
}

References: src/api.ts• L13-15 src/api.ts• L68-75 src/…/decision.ts• L14-22 tests/…/public-api.test.ts• L80-88

Evaluation behavior

Each call creates a command invocation for the shared guard. The guard reads local policy and current filesystem facts for that call, checks protected paths, and then analyzes the command with automatic shell selection. This keeps a direct library check aligned with the agent-hook safety path while keeping the caller in control of what happens after the result.

The API follows this high-level flow:

flowchart LR
    A[Library caller] --> B[checkCommand]
    B --> C[Validate command and cwd]
    C --> D[Load local policy and facts]
    D --> E[Check protected paths]
    E --> F[Analyze command]
    F --> G[Allow or deny result]
    G --> H[Caller decides whether to execute]

References: src/api.ts• L23-53 src/…/guard.ts• L102-243 src/…/snapshot.ts• L13-20

Policy and context

The API reads the effective policy on every call. Project policy is selected from the supplied cwd, and the current process environment supplies the ambient values needed by command and path analysis. A policy or rule source that cannot be used produces a protective fallback and a deny only when the command itself matches a protection; the compact API result does not expose the fallback detail.

Because the API does not cache the policy snapshot, a policy change can affect the next call in the same process. Callers must treat each result as the outcome for the policy and filesystem state observed during that call.

References: src/api.ts• L35-52 src/…/guard.ts• L178-243 src/…/snapshot.ts• L13-20 src/…/environment.ts• L37-49 tests/…/check-command.test.ts• L156-163

Protection coverage

The direct check includes the shared early protections before ordinary command rules. This means it can deny a command that would change the safety policy, alter Git control data, or access a sensitive path even when the command is not a named destructive-command rule.

The command uses automatic shell selection. The same call can therefore identify supported POSIX or PowerShell forms without asking the library caller to choose a shell dialect.

Protection or analysisOutcome exposed by the API
Policy-file mutationDeny with a protection reason.
Git metadata mutationDeny with a protection reason.
Sensitive-path accessDeny with the relevant protection rule ID when available.
Built-in destructive commandDeny with its rule ID.
Project custom ruleDeny with the custom rule ID.
No matchAllow.

References: src/…/guard.ts• L140-243 src/api.ts• L44-52 tests/…/check-command.test.ts• L74-103

Caller boundary

The API only checks command text. It does not run the command, approve a later execution, or create the audit record used by agent integrations. A caller that receives allow still owns command execution and must use the same command and working directory it checked if it wants the result to remain relevant.

The API is synchronous. Type errors identify invalid input at the public boundary. A valid request with an unusable working directory or an engine limit returns a fail-closed deny. Unexpected non-safety code errors remain visible to the caller; a known guard dependency failure is converted into a fail-closed denial so a failed safety check cannot be mistaken for permission.

References: src/api.ts• L17-65 src/…/guard.ts• L80-89 src/…/guard.ts• L272-309 tests/…/check-command.test.ts• L120-143

Side-effect boundary

The check is read-only with respect to command execution, network access, and audit storage. It may read local policy files and filesystem facts because those facts are part of the safety decision. An allow or deny is returned without creating the audit directories used by runtime integrations.

This makes the API suitable for a caller that wants to preflight a command before a separate execution step. It does not make the checked command safe to run under a changed directory, changed policy, or changed environment.

References: src/api.ts• L17-22 src/…/guard.ts• L178-240 src/…/snapshot.ts• L13-20 tests/…/check-command.test.ts• L145-153

The API result is intended for a simple allow-or-deny gate. The command safety pipeline page explains how the same guard combines parsing, protected-path checks, policy, and command analysis. The parsing page explains how shell text becomes bounded facts. The semantic analysis page explains how command behavior and path targets are assessed. The decisions page covers richer explanations, traces, and audit records.

References: src/api.ts• L43-75 src/…/guard.ts• L102-243 src/…/explain.ts• L40-127

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the cc-safety-net/api export limited to the intended function and public types. The package root and the API subpath have different consumer contracts.
  • Keep CheckCommandInput.cwd aligned with policy loading, path resolution, and execution context. A change in one meaning can make a result apply to the wrong project.
  • Keep the compact result projection aligned with the shared decision fields. Denial reasons and rule IDs must survive without exposing internal evidence or trace types.
  • Keep fail-closed handling aligned between unusable directories, guard dependency failures, and analysis limits. None of these cases may become an allow through the library boundary.
  • Keep the no-audit and no-execution boundary intact when adding work to the API path. Local reads needed for a decision must not become command, network, or audit side effects.
  • Keep per-call policy loading behavior intact so a changed project policy affects the next API call.

Important failure modes

TriggerConsequenceGuard
Caller passes a missing, inaccessible, relative, or non-directory cwdThe command is not checked against a usable project contextValidate the absolute path and return a fail-closed deny for an unusable directory
Caller passes a missing or empty input valueNo trustworthy command request existsThrow TypeError at the public boundary
Command crosses parser, path, or analysis limitsThe full command cannot be trusted as safeReturn the engine's fail-closed deny
Local policy or rule sources are invalidThe configured policy is not fully activeUse protective fallback behavior and continue with the safe remainder
A guard dependency fails during a valid callThe caller could otherwise mistake an unavailable check for permissionConvert GuardEvaluationError into a fail-closed deny
Caller changes command, cwd, policy, or environment after checkingThe earlier result may no longer describe the command that runsTreat the result as a preflight for the exact state and command used next
Caller expects the API to create audit historyNo runtime audit record exists for the checkUse an agent integration or a separate diagnostic or audit surface when a record is required

Key files

FileWhy to read it
src/api.tsDefines the public input, result, validation, guard call, and side-effect boundary.
src/…/guard.tsDefines the shared safety pipeline that the API invokes.
src/…/decision.tsDefines the internal decision fields projected into the public result.
src/…/snapshot.tsShows how local policy becomes the per-call evaluation context.
src/…/cwd-containment.tsDefines the usable-directory check shared by the API and an integration surface.
src/…/environment.tsDefines the process facts captured for command analysis.
tests/…/check-command.test.tsDemonstrates allow, built-in and custom denials, input validation, fail-closed behavior, and side-effect guarantees.
tests/…/public-api.test.tsVerifies the package subpath, type contract, and isolation from the optional plugin dependency.
package.jsonDefines the published root and api package exports.