Command safety evaluation pipeline
The command safety evaluation pipeline turns an intercepted tool call into one shared allow or deny decision. It builds a common view of commands and paths, checks protections before command analysis, evaluates behavior under one policy view, and returns the result with evidence for the host and audit trail. See Command and tool-input parsing, Semantic command analysis and guards, Safety decisions, explanations, and audit trail, and Programmatic safety API for focused views.
Evaluation boundary
An evaluation starts when an integration can describe a tool call. A tool invocation is the shared request contract. It carries the tool name, input, route, shell details for command routes, and separate configuration and execution directories. This lets different integrations use the same safety meaning even when their event formats differ.
Command routes carry command text. Other routes carry tool values or path candidates. Unknown routes can still provide command and path candidates so early protections can act before the route is fully known. The gate treats this input as untrusted until it creates shared semantic facts.
Tool invocation
Each integration sends a tool invocation to the shared gate. The gate can promote an unknown tool to a declared command tool when policy identifies the tool and its input contains a usable command. The requested working directory becomes the execution context only after the gate validates it.
This promotion keeps the policy directory separate from the requested execution directory. A bad, conflicting, or unusable directory produces a validation denial instead of an unclear analysis context. Hook adapters also bound their input and deny malformed JSON or unusable session directories before evaluation continues.
References: src/…/pipeline.ts• L113-175 src/…/pipeline.ts• L375-425 src/…/intake.ts• L88-161
Evaluation stages
The shared gate is the authoritative path for intercepted calls. It creates one semantic view before it checks protected resources and command behavior. This order protects the policy and repository control paths even when the command is unfamiliar or its behavior is not fully provable.
The normal evaluation sequence is:
- Read command data from the tool input and fail closed if the input exceeds its safety bounds.
- Promote a declared command tool when its policy declaration permits that route.
- Create shared command, shell, and path facts.
- Stop on structural limits or invalid safety input.
- Check policy configuration, policy-apply actions, and Git metadata.
- Load one policy snapshot and check sensitive paths.
- Allow a non-command route when no protection matches.
- Analyze a command route for behavior and derived commands.
- Return the decision, stage, evidence, policy level, and fallback state.
This diagram shows how an intercepted request moves through the shared safety boundary.
flowchart TD
A[Intercepted tool call] --> B[Create tool invocation]
B --> C[Validate or promote execution context]
C --> D[Create shared semantic facts]
D --> E{Structural limit or failed input?}
E -->|yes| Z[Deny and explain]
E -->|no| F[Policy and repository guards]
F -->|match| Z
F -->|clear| G[Load policy snapshot]
G --> H[Sensitive-path guard]
H -->|match| Z
H -->|clear| I{Command route?}
I -->|no| Y[Allow non-command route]
I -->|yes| J[Analyze command behavior]
J -->|match or no proof| Z
J -->|no match| Y
Y --> K[Project decision and audit data]
Z --> K
K --> L[Return host outcome]
Semantic facts
The semantic fact stage creates one reusable view of command syntax and path values. It keeps both the command declared by a command route and the command found in tool input when both exist. A shared fact store caches each command by shell dialect and reuses its parsed program and shell structure across guards and analysis.
The fact view records structural limits and invalid syntax instead of treating partial input as complete. It also includes direct path values from tool input and patch targets. This gives early guards a common source for nested shell content, redirections, assignments, and other path-bearing values.
Guard walking turns that parsed view into ordered words, boundaries, redirections, and scoped nested content. It carries the current directory and tracked shell variables across command boundaries. It separates data-only heredocs from executable content while preserving interpreter heredocs and pipeline context. It expands functions and heredoc content within bounded limits, while marking invalid or incomplete syntax so guards can fail closed instead of using a partial view.
References: src/…/semantic-facts.ts• L47-113 src/…/semantic-facts.ts• L129-173 src/…/guard-walk.ts• L142-187 src/…/guard-walk.ts• L190-281 src/…/guard-walk.ts• L395-590
Early guards
Early guards stop changes that could disable the safety system or repository controls. They run before ordinary command rules so command and non-command routes share the same protected boundary.
The early boundary covers:
- Policy configuration and policy-apply actions.
- Git metadata and hook locations.
- Sensitive paths, credentials, and other configured protected data.
Sensitive-path inspection follows values through command segments, redirections, nested scripts, interpreter input, pipeline-carried data, and tool paths. It also separates executable content from data-only heredocs and follows code sent to interpreters. In standard mode it can distinguish path, data, write, and metadata-only roles when the command proves that distinction. This avoids treating a known data value, a new output path, or a metadata-only lookup as a sensitive path access while keeping explicit denied paths protected. Unclear syntax or unresolved path use remains protected. Interpreter and shell source handling can widen inspection when code or a derived command may read or execute a path.
References: src/…/pipeline.ts• L179-281 src/…/guard-walk.ts• L939-1044 src/…/secret-protection.ts• L425-491 src/…/secret-protection.ts• L582-758 src/…/secret-protection.ts• L1179-1203
Policy snapshot
The policy stage loads one snapshot for the full evaluation. The snapshot supplies the selected safety level, active capabilities, rule state, path settings, and custom policy rules that later stages must use together.
The snapshot can be ready or degraded. Protective fallback keeps the result fail-safe when policy sources are incomplete. The returned evaluation reports the fallback reason so callers can tell a normal policy result from one made with reduced configuration.
References: src/…/pipeline.ts• L240-247 src/…/pipeline.ts• L288-310 src/…/analysis.ts• L27-57
Command analysis
Command analysis checks what a command can execute rather than only its outer text. It uses the parsed program, policy context, environment, protected Git metadata, working-directory state, and a bounded analysis budget.
The analyzer follows control flow and nested command sources while carrying the context that can change behavior. This includes directory changes, environment assignments, functions, heredoc files, Git state, interpreter code and module or script sources, and possible branch outcomes. Nested analysis can carry its own directory, environment, shell, worktree, and function context so derived commands are evaluated in the state they would inherit. Known transparent wrappers are reduced to their child commands. Shell startup sources, child-command input, and interpreter sources are inspected when they are readable. Unresolved or dynamic sources remain protected instead of becoming an allow.
Tracked heredoc and script paths are resolved against the effective directory before the analyzer reads their known content. Persistent file sources can be inspected when the environment can read them. Missing content, downloaded content, or a source whose path cannot be proved remains an unresolved source rather than an implicit allow. A path written earlier in the same command can also change whether a later script source is treated as an existing file.
Destructive delete checks use one target trust model across POSIX rm, cmd recursive deletion, and PowerShell Remove-Item. PowerShell wrapper handling reads literal -Command scripts after supported switches. Encoded commands and unread recursive-delete scripts are blocked because the analyzer cannot inspect their behavior. A protective -WhatIf request is not treated as an actual deletion. The shared model distinguishes root or home targets, Git metadata, dynamic targets, current-directory scope, trusted temporary paths, and configured allow paths. Unsafe expansion or pipeline input remains unverified rather than becoming an allow. Wrapper preludes also carry effective environment assignments and directory changes into this decision.
The analyzer also recognizes isolated container runs. Docker, Podman, and nerdctl payloads can avoid a second scan for ordinary findings only when the run has no host-reaching option and its option values are known. Host mounts, privileged or host-process access, devices, root filesystems, dynamic options, and rules that can affect the host keep the normal safety checks active.
The analyzer tracks file state that can affect later behavior in the same command. A literal path written by a remote fetch is treated as unverified if a later shell or interpreter runs it. A later overwrite removes that state. Literal heredoc files and directories created earlier in the command are also carried into later checks when their effects are known. An interpreter syntax check does not run the file, so it is not treated as execution of the downloaded program.
Analysis limits are converted into explicit stop-and-explain denials for derived tokens, tracked heredoc files, control-flow states, wrapper peeling, and derived command shape. The analyzer records the limit in the trace when one is active, so a caller can distinguish an ordinary rule match from a bounded analysis result. It stops at the first applicable block. An unresolved source, unsupported construct, strict parse failure, recursion limit, or budget limit produces a denial when the analyzer cannot prove safety. Interpreter code is checked for dangerous command text and native recursive deletion of root or home targets. Inline code, main scripts, and module sources are kept distinct so each executable source receives the correct check. When no rule matches, the command receives an allow result. For an automatic shell route, a standard-mode warning can trigger a second POSIX interpretation; the gate adopts that result only when it provides a stronger confirmed denial.
References: src/…/index.ts• L16-95 src/…/heredoc-files.ts• L6-27 src/…/analyze-command.ts• L122-213 src/…/analyze-command.ts• L247-470 src/…/analyze-command.ts• L620-677 src/…/analyze-command.ts• L770-961 src/…/analyze-command.ts• L1056-1157 src/…/segment.ts• L101-249 src/…/segment.ts• L297-431 src/…/segment.ts• L545-647 src/…/segment.ts• L780-846 src/…/segment.ts• L1053-1175 src/…/container-run.ts• L23-68 src/…/powershell-wrapper.ts• L22-77 src/…/rm.ts• L54-115 src/…/cmd.ts• L69-152 src/…/remove-item.ts• L102-217 src/…/recursive-delete-targets.ts• L96-135
Decision projection
The pipeline projects every result into a shared decision contract. An allow means that no active guard or command rule blocked the request. A deny carries the reason, rule identity, intent, and available command or segment evidence.
The evaluation also reports the stage, effective safety level, policy fallback state, and analysis error code when relevant. Standard-mode results can mark dangerous text or an unresolved dynamic source as unverified. This tells the caller that the system stopped without a direct rule proof.
Rulebook fixtures use the same bounded analyzer path as normal command checks. They turn a rulebook's expected blocked or allowed examples into validation errors when another rule matches first or when no expected rule matches. This keeps rule ordering and custom rule behavior visible without changing the live gate result.
References: src/…/pipeline.ts• L49-62 src/…/pipeline.ts• L340-371 src/…/pipeline.ts• L484-517 src/…/rulebook-fixtures.ts• L10-62
Safety decision
The returned decision is the contract between the gate and its callers. Integrations map an allow or deny to their host-specific behavior without changing the safety meaning.
Host outcome
An integration maps a deny to its host-specific block response and does not execute the tool call. The response can include redacted evidence and a next action such as stopping, asking for approval, narrowing scope, or simplifying the command.
Unexpected failures stay inside the same boundary. A failed safety dependency becomes a fail-closed evaluation, so a missing result cannot become an allow.
References: src/…/pipeline.ts• L464-511 src/…/intake.ts• L164-196
Audit projection
The audit path records the decision after evaluation. It uses bounded and redacted fields so later review can connect a result to its stage, reason, intent, rule identity, policy level, fallback state, and failure information without storing unrestricted command data.
Audit storage does not change the safety result. A write failure can make history incomplete, but it cannot turn a deny into an allow. See Safety decisions, explanations, and audit trail for the record and review model.
References: src/…/pipeline.ts• L49-62 src/…/trace.ts• L126-166
Non-executing explanation
The explanation path answers the safety question without running the command. It uses the same invocation and gate as an intercepted request, then returns the predicted result with policy details and an ordered trace. A caller can select the shell dialect used for parsing, which keeps an explanation aligned with the host that will interpret the command.
Explanation trace
The trace shows how the gate reached its result. It can describe parsing, wrapper handling, context changes, rule checks, skipped segments, nested analysis, and errors. Global steps describe the full command, while segment steps describe the command part that was analyzed.
Trace data is diagnostic and bounded. The recorder limits event size and count, redacts environment assignments and likely secrets, and prevents trace output from replacing the authoritative decision. When the gate tests an alternate shell interpretation, it records that work on a branch and adopts the branch only if it matches the returned evaluation. A denial from an early protection is projected as a pre-analysis rule check so the explanation shows the stage that stopped the request. Empty input returns an allowed result with an error trace, while structural and parse limits remain fail-closed.
References: src/…/explain.ts• L47-137 src/…/explain.ts• L140-202 src/…/explain.ts• L271-314 src/…/trace.ts• L8-72 src/…/trace.ts• L107-166
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to tool routes or input extraction must keep declared commands, input candidates, route shells, configuration directories, and execution directories aligned.
- Changes to hook input limits or malformed-call handling must keep host denials fail-closed and preserve safe command text for diagnostics.
- Changes to command promotion must preserve the distinction between the policy configuration directory and the requested execution directory.
- Changes to semantic facts or guard walking must keep early path protection and command analysis on the same parsed source and nested-source view.
- Changes to guard walking must preserve ordered command boundaries, redirection roles, nested scopes, heredoc consumers, tracked directory state, variable state, and bounded function expansion.
- Changes to guard heredoc classification must preserve the difference between data sinks, interpreter input, pipeline input, and executable substitutions.
- Changes to gate order must keep policy, policy-apply, Git metadata, and sensitive-path protection before ordinary command analysis.
- Changes to policy loading must keep the snapshot, effective capabilities, rule activation, safety level, and fallback reason consistent for every stage.
- Changes to sensitive-path role classification must preserve the distinction between path access, data, writes that create new files, metadata-only inspection, and explicit denied paths.
- Changes to recursive-delete rules must keep target classes and rule IDs aligned across
rm,cmd, and PowerShell paths. Preserve the difference between trusted temporary or allowed paths and unverified targets. - Changes to PowerShell wrapper parsing must keep literal
-Command,-File, encoded-command, and unread recursive-delete cases distinct. - Changes to wrapper-prelude handling must keep effective environment assignments and directory changes consistent between command analysis and sensitive-path extraction.
- Changes to remote-fetch and file-state tracking must keep downloaded output paths scoped to one command, remove a path after a later overwrite, and block execution through shell or interpreter sources.
- Changes to heredoc or script path resolution must keep relative sources tied to the inherited effective directory and keep virtual system paths from being treated as persistent files.
- Changes to interpreter argument parsing must keep inline code, main scripts, module files, and syntax-check options distinct. Only options before the script operand can mark a command as a syntax check.
- Changes to interpreter or derived-command inspection must preserve the difference between literal, derived, and unresolved executable sources.
- Changes to transparent-wrapper handling must keep wrapped child commands on the same policy, environment, directory, and analysis-budget path.
- Changes to container-run isolation must keep Docker, Podman, and nerdctl host-access options conservative. Dynamic options and host-reaching rules must not be hidden by the container boundary.
- Changes to analysis state must preserve directory, environment, Git, heredoc, function, branch, and child-command context across isolated and forked paths.
- Changes to child-command rules must keep dynamic input inside the same command, path, policy, and analysis-budget checks.
- Changes to analyzer cap handling must keep derived-command limits mapped to stop-and-explain denials with the matching error code and trace event.
- Changes to rulebook fixture evaluation must keep custom rule IDs, first-match ordering, and blocked-versus-allowed expectations aligned with the live analyzer.
- Changes to denial projection must preserve stage, reason, intent, rule identity, evidence, error code, and the standard-mode unverified marker.
- Changes to explanation traces must keep diagnostic events separate from the authoritative decision, preserve branch adoption, shell selection, and redaction limits.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Hook input exceeds its byte bound or tool input exceeds its safety bound | The request has no trusted input view | The intake or gate stops processing and returns a fail-closed denial |
| Hook JSON is malformed or a tool call has conflicting input fields | The host cannot provide a reliable invocation | Intake returns a malformed-call denial |
| A declared command tool supplies conflicting or unusable working-directory values | Analysis could run under an unclear directory context | Command promotion returns a command-validation denial |
| A policy file, policy-apply action, or Git metadata target is found | The request could disable safety or repository controls | Early protection stops the request before command analysis |
| A sensitive path appears in a nested command, interpreter body, redirection, pipeline carrier, heredoc, or tool path | Sensitive access could bypass a narrow command rule | Shared semantic path extraction checks direct and derived candidates, while standard-mode role checks exempt only proven data, new writes, or metadata-only reads |
| A recursive delete targets root, home, Git metadata, the current directory, or an unapproved path | A destructive command could remove protected or broad data | Shared target classification returns the shell-specific destructive rule denial |
| A recursive delete uses dynamic expansion, unsafe brace expansion, or unverifiable PowerShell pipeline input | The analyzer cannot prove the deletion targets | Dynamic-target or pipeline rules stop the request |
| A PowerShell wrapper uses an encoded command or hands recursive deletion to an unread script | The analyzer cannot inspect the nested delete | The wrapper returns a dynamic-source or unread-recursive-delete denial |
A cmd recursive delete contains escaped quoting, a namespace path, or unresolved expansion | The target could split or expand to a dangerous location | The cmd analyzer returns an escaped-quote or dynamic-source denial |
| A container run uses a host-reaching option, a dynamic option, or an unverified mount | The payload can affect the host or its safety boundary | Container isolation is not trusted and the normal embedded-command checks remain active |
| A policy snapshot is degraded | Configured protection may not be fully active | Protective fallback continues with a reported fallback reason |
| A tracked heredoc or script source cannot be resolved, read, or bounded | The analyzer cannot prove which source will run | Source analysis returns a dynamic-source or analysis-limit denial |
| A remote fetch writes a literal path and a later shell or interpreter runs that path in the same command | Untrusted remote content could execute without a safety view | The analyzer tracks the path and returns an unresolved-source denial until the path is overwritten |
| A wrapper, interpreter, shell input, startup source, or child-command source remains unresolved | The analyzer cannot prove which command or target will run | Dynamic-source or command-stream rules return stop-and-explain |
| Control-flow states, derived commands, script reads, or recursion exceed the analysis budget | Analysis could stop before it covers all possible behavior | The budget returns an analysis-limit denial |
| A gate dependency throws | The host could otherwise execute without a safety result | GuardEvaluationError carries a fail-closed evaluation |
| Trace or audit output contains sensitive command data | Diagnostic history could expose secrets | Bounded redaction removes sensitive values without changing the decision |
Key files
| File | Why to read it |
|---|---|
| src/…/pipeline.ts | Defines the authoritative order from input extraction to the returned guard evaluation. |
| src/…/intake.ts | Defines bounded hook input, route selection, working-directory checks, and early host denials. |
| src/…/semantic-facts.ts | Shows how command programs, shell facts, and direct path values are shared across stages. |
| src/…/guard-walk.ts | Shows how parsed command content becomes ordered guard events with boundaries, redirections, nested scopes, heredocs, and shell state. |
| src/…/secret-protection.ts | Shows how command and tool data becomes sensitive-path candidates across nested sources and how proven data, writes, and metadata-only reads are treated in standard mode. |
| src/…/index.ts | Shows how analyzer budget breaches become explicit decisions with error codes and trace events. |
| src/…/heredoc-files.ts | Defines how tracked script and heredoc paths follow the effective directory and which paths can be treated as persistent files. |
| src/…/analyze-command.ts | Shows how command programs, control flow, nested sources, file state, downloaded paths, and analysis state produce a result. |
| src/…/segment.ts | Shows how wrappers, shell sources, interpreters, child input, container boundaries, and command rule families are dispatched. |
| src/…/container-run.ts | Defines when a Docker-, Podman-, or nerdctl-style run can keep ordinary payload findings inside the container boundary. |
| src/…/powershell-wrapper.ts | Shows how readable PowerShell scripts and unread recursive-delete wrappers reach safety checks. |
| src/…/rm.ts | Shows how recursive POSIX deletion is judged against path scope, temporary paths, and policy. |
| src/…/cmd.ts | Shows how Windows recursive deletion inside cmd is checked for target scope and unsafe quoting. |
| src/…/remove-item.ts | Shows how PowerShell deletion, pipeline input, aliases, protective switches, and target parsing reach the shared target checks. |
| src/…/recursive-delete-targets.ts | Defines the shared meaning of trusted, dynamic, protected, and out-of-scope deletion targets. |
| src/…/wrapper-prelude.ts | Shows how launch wrappers, environment assignments, and directory changes become analysis context. |
| src/…/interpreters.ts | Shows how interpreter arguments and executable sources reach safety checks. |
| src/…/shell-execution.ts | Shows how shell input, scripts, pipelines, and remote fetch sources are classified as literal or dynamic. |
| src/…/transparent-wrappers.ts | Shows how launcher commands are reduced to protectable child commands. |
| src/…/rule.ts | Maps command heads to the main destructive and child-command rule families. |
| src/…/analysis.ts | Defines the policy, environment, Git, mode, nested context, and budget data passed into command analysis. |
| src/…/rulebook-fixtures.ts | Shows how custom rulebook examples validate first-match behavior through the bounded analyzer. |
| src/…/explain.ts | Shows how the same gate produces a non-executing result with policy data and trace. |
| src/…/trace.ts | Defines the diagnostic trace model, alternate-branch handling, and its bounds and redaction behavior. |
| src/…/decision.ts | Defines the shared allow and deny contract used by callers. |