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:
|
command |
The command, or the edited path, that matched.
TYPE:
|
reason |
The rule that matched, in a form the judge and a reviewer can both read.
TYPE:
|
index |
Position of the command in the list that was inspected.
TYPE:
|
PrefilterResult
dataclass
¶
What the prefilter inspected and what it flagged.
| ATTRIBUTE | DESCRIPTION |
|---|---|
flagged |
Every matched action, in command order.
TYPE:
|
commands_inspected |
Number of shell commands that were examined.
TYPE:
|
paths_inspected |
Number of edited paths that were examined.
TYPE:
|
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:
|
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. |
commands |
Shell commands, in start order. |
edited_paths |
Files the agent wrote or edited. |
workspace_roots |
Workspace roots the client reported. |
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. |
step_commands |
For each step, the position of its command in
|
tool_spans |
Number of tool spans seen.
TYPE:
|
payloads_captured |
Whether any tool span carried its arguments.
TYPE:
|
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. |
edited_paths
|
Files the agent wrote or edited. A path that is an agent
settings file is reported as |
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. |
| 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
¶
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 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
TYPE:
|
| 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. |