Skip to content

trulens.feedback.command_safety

trulens.feedback.command_safety

Rule-based prefilter for risky shell commands run by coding agents.

The prefilter is the first stage of destructive_actions_with_cot_reasons. It reads the shell commands and edited paths of one turn and reports every action that matches a risk category. Only those actions reach the judge, so a turn with nothing flagged costs no LLM call.

A clean result is only as good as the rules. [PrefilterResult][trulens.feedback. command_safety.PrefilterResult] therefore records how many commands and paths were inspected, so "20 commands, 0 flagged" can be told apart from "0 commands, 0 flagged".

Classes

FlaggedAction dataclass

One action matched by the prefilter.

ATTRIBUTE DESCRIPTION
category

One of [CATEGORIES][trulens.feedback.command_safety. CATEGORIES].

TYPE: str

command

The command, or the edited path, that matched.

TYPE: str

reason

The rule that matched, in a form the judge and a reviewer can both read.

TYPE: str

index

Position of the command in the list that was inspected.

TYPE: int

Functions
to_dict
to_dict() -> Dict[str, object]

Return a JSON-serializable form for the metric's meta.

PrefilterResult dataclass

What the prefilter inspected and what it flagged.

ATTRIBUTE DESCRIPTION
flagged

Every matched action, in command order.

TYPE: List[FlaggedAction]

commands_inspected

Number of shell commands that were examined.

TYPE: int

paths_inspected

Number of edited paths that were examined.

TYPE: int

workspace_known

Whether workspace roots were available. Without them, a delete is only flagged when it targets the filesystem root, the home directory or a system directory.

TYPE: bool

Attributes
counts property
counts: Dict[str, int]

Matches per category, with zero for categories that never matched.

Functions
to_dict
to_dict() -> Dict[str, object]

Return a JSON-serializable form for the metric's meta.

TurnActions dataclass

What one coding-agent turn did, as read from its trace.

ATTRIBUTE DESCRIPTION
user_request

The user's request for the turn, from the record root, or None when content capture was off. This is the only text that may justify an action.

TYPE: Optional[str]

commands

Shell commands, in start order.

TYPE: List[str]

edited_paths

Files the agent wrote or edited.

TYPE: List[str]

workspace_roots

Workspace roots the client reported.

TYPE: List[str]

steps

Every tool call as context for the judge: name, a clipped argument and a clipped result. Tool results can carry text written by a third party, so the judge treats them as untrusted.

TYPE: List[Dict[str, str]]

step_commands

For each step, the position of its command in commands, or None when the step was not a shell command.

TYPE: List[Optional[int]]

tool_spans

Number of tool spans seen.

TYPE: int

payloads_captured

Whether any tool span carried its arguments. TRULENS_CAPTURE_TOOL_PAYLOADS controls this, and without it the commands cannot be read.

TYPE: bool

Functions

prefilter_actions

prefilter_actions(
    commands: Sequence[str],
    edited_paths: Sequence[str] = (),
    workspace_roots: Sequence[str] = (),
) -> PrefilterResult

Flag risky shell commands and edited paths in one turn.

PARAMETER DESCRIPTION
commands

Shell commands the agent ran, in order.

TYPE: Sequence[str]

edited_paths

Files the agent wrote or edited. A path that is an agent settings file is reported as agent_permission_change.

TYPE: Sequence[str] DEFAULT: ()

workspace_roots

Absolute workspace roots. When given, a delete is judged against them. When empty, a delete is only flagged if it targets the filesystem root, the home directory or a system directory.

TYPE: Sequence[str] DEFAULT: ()

RETURNS DESCRIPTION
PrefilterResult

What was inspected and what matched. The same action can match more than

PrefilterResult

one category, and is reported once per category.

redact_secrets

redact_secrets(text: str) -> str

Replace common secret forms in text with [redacted].

This is best effort: bearer tokens and Authorization headers, key=value pairs (also with spaces around the = or as JSON), --password x flags, mysql -p and docker login -p passwords, URL credentials and a few well-known key shapes. A secret in any other form is not caught.

redact_and_clip

redact_and_clip(text: str, limit: int) -> str

Redact secrets in the first part of text, then keep at most limit characters.

Only twice limit characters are scanned, so a very long tool result costs no more than a short one.

collect_turn_actions

collect_turn_actions(trace: Any) -> TurnActions

Read the shell commands, edited paths and user request out of a trace.

PARAMETER DESCRIPTION
trace

A trulens.core.feedback.selector.Trace from a coding-agent turn. Its events must carry record_attributes.

TYPE: Any

RETURNS DESCRIPTION
TurnActions

The turn's actions. Spans are read in start order.

RAISES DESCRIPTION
ValueError

If the trace's events do not carry record attributes.