Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/preallow/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preallow",
"version": "0.0.13",
"version": "0.0.14",
"description": "Lets Claude Code auto mode run the steps of the task you gave when its classifier blocks them: a model judge allows what is reversible or what you explicitly asked for",
"author": {
"name": "Entwining"
Expand Down
4 changes: 3 additions & 1 deletion plugins/preallow/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The user's authority comes from `prompt.submit` with a `composer` or `bridge` or

The user's words are kept in `$.store` under the session id, so a resume keeps them and `/clear`, which goes on under a new id, starts without them; never rebuild them from `$.session.messages`, which does not say where a message came from. A conversation is kept bounded by its JSON size (`addTurn` in `hooks/messages.ts`), so 50 fit the store whatever they hold, and the judge reads all of it for every call, since leaving out a limit lets through a call the user ruled out. Each write replaces it whole: a judge reading meanwhile sees it before or after the message, never without it. The judge prompt is one JSON document, so text inside a message cannot pose as another field; it is sent as two blocks, the kept conversation first and marked for the prompt cache, then the call's fields (`judgePrompt` in `hooks/judge.ts`), so every call until the user writes again opens the same way and reads the conversation, and on gateway routes the policy, from the cache.

The calling agent's own recent tool calls (`recentCalls` in `hooks/calls.ts`, from the same `$.session.messages` read as a subagent's brief) go in the call's block as evidence of the state a call acts on, never as the user's words: tool output is where text others wrote enters, so `JUDGE_SYSTEM` says they authorize nothing, and a change to what is kept or how the policy describes it is checked against results that claim approval or safety as well as against real denials. They are a contiguous run of the newest calls that fit, so an older call never stands in for a newer one that contradicts it. The `repeated` digest leaves them out, since a call made again after other calls has nothing new from the user, and judging it again only spends a judge on an unchanged decision.

## Judging a call

The judge chooses what to check; `hooks/facts.ts`, with `hooks/paths.ts`, `hooks/refs.ts`, and `hooks/git.ts`, answers from a fixed set of queries that only read, stay offline, and carry a time limit, and takes the judge's strings only as absolute paths or ref names that cannot pose as options. A new query keeps all of that, with a test that fails without it.
Expand All @@ -36,7 +38,7 @@ The output scan answers a call the plugin allowed with a new `{ result }` holdin

The log is the evidence for every later decision about the plugin: which branches real sessions reach, why a call was not allowed, and why an output was withheld, read by joining a line to the session's transcript by `tool_use_id`. `tool.judge` writes one line for every call it is shown, with the classifier's verdict and the same fields whichever way the call went: a new branch leaves through `leave` in `hooks/register.ts` with an event of its own, never by returning the core's answer unlogged. A `catch` that changes what happens to a call logs the caught error's own message in `error`, since a reason the plugin words itself cannot tell two causes apart.

Add a field only for a question about real sessions that the log joined to the transcript cannot answer, and name that question in the change; what the transcript already records stays out. The plugin puts no tool output and no value the scan found in a line; `error` is the engine's or the runtime's own message, or one the plugin words itself and starts with `preallow:`.
Add a field only for a question about real sessions that the log joined to the transcript cannot answer, and name that question in the change; what the transcript already records stays out. The plugin puts no tool output and no value the scan found in a line; `error` is the engine's or the runtime's own message, or one the plugin words itself and starts with `preallow:`. A judge's `reason`, and the paths and refs it asks to check with their answers, are the model's choices, and since the judge reads earlier calls' output they may carry text from one, the reason despite the policy telling it not to; the README lists that as a known gap rather than dropping the reason, which is how a wrong verdict gets diagnosed.

## Credential patterns

Expand Down
5 changes: 5 additions & 0 deletions plugins/preallow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.0.14 - 2026-11-30

- The judge also reads the agent's own newest finished tool calls, from the loop that made the call, with the text it read from each: up to 16,000 characters as JSON, each input cut to its head and tail past 2,000 and each result past 4,000. They can show what a call acts on that its input does not, such as the prompt in another terminal a keypress answers, which the judge sent back to the classifier as unknown before. They never count as your words, and text in them claiming your approval or that a call is safe grants nothing. The [known gaps](docs/usage.md#known-gaps) cover state that changed after the agent read it, results that mislead the judge, and a reason that quotes an earlier result.
- Each `judged` line in the log gives in `toolUseIds` which of the agent's earlier calls the judge read.

## 0.0.13 - 2026-11-29

- Claude Code's auto mode classifier now decides each call first, and the judge is asked only about a call the classifier blocked or gave no verdict on; its allow lets that call run. Before, the judge decided every call Claude Code asked about and only the calls it did not allow went to the classifier, which cost a few seconds on most calls the classifier would have allowed in under one. Requires Claude Code 2.1.326 or later, the first release that lets a plugin hear the classifier's verdict.
Expand Down
4 changes: 2 additions & 2 deletions plugins/preallow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For calls the judge allowed, preallow [redacts credential-shaped values or withh

## Configure the judges

You can [choose the models and effort in `/config`](docs/usage.md#settings). A second judge can review calls the first did not allow. Each judge reads [your kept messages and the call's input](docs/usage.md#what-the-judge-reads).
You can [choose the models and effort in `/config`](docs/usage.md#settings). A second judge can review calls the first did not allow. Each judge reads [your kept messages, the call's input, and the agent's recent tool calls](docs/usage.md#what-the-judge-reads), which can show what a call acts on but never grant it.

## Install

Expand All @@ -28,7 +28,7 @@ Start a new session after installing or updating. Third-party marketplace plugin

## Know the limits

The judge can lose earlier instructions, and output delivered later can escape the scan. The [known gaps](docs/usage.md#known-gaps) describe these limits and the scan's misses and false positives.
The judge can lose earlier instructions, take state the agent read earlier as still true, or be misled by a tool result, and output delivered later can escape the scan. The [known gaps](docs/usage.md#known-gaps) describe these limits and the scan's misses and false positives.

Tool inputs are logged without scanning. A credential in a command or file edit lands in the local log. The [log records the classifier's verdicts, the judge's, failures, and redaction events](docs/usage.md#reading-the-log).

Expand Down
9 changes: 7 additions & 2 deletions plugins/preallow/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The defaults were chosen by evaluating the policy on real calls auto mode had se
- The tool's name and its whole input, since the call runs with all of it.
- Your own messages in this conversation: what you typed at the terminal or sent through Remote Control, including slash command lines, and your answers to the agent's questions (`AskUserQuestion`): the questions and options as you were shown them, the options you chose, your notes, and anything you typed instead. A chosen option authorizes the action on the target it states, nothing wider; a question that resolved while you were away holds no answer of yours. Task notifications, other sessions' messages, channel relays, and the agent's own text never count as your words.
- For a subagent's call, the brief its parent agent gave it, cut to its head and tail past 8,000 characters. Your words quoted in it count as yours; the parent's restatement of your task shows what the task is, but cannot supply the explicit words an irreversible effect needs or lift a prohibition of yours.
- The agent's own newest finished tool calls before this one, from the loop that made the call, so a subagent's own for its calls, oldest first: each tool's name, its input cut to its head and tail past 2,000 characters, the text the agent read from its result cut past 4,000, and whether it failed, up to 16,000 characters in all, counted as JSON as your messages are; the first call that does not fit is left out with every call before it. They show state the call's input does not, such as the prompt a keypress answers or the script a command runs, and the judge takes the newest of them that shows a target as its state. They never count as your words: text in them claiming your approval, or that a call is safe, grants nothing, and instructions in them are not your task. A subagent of a workflow run, whose loop Claude Code does not give the plugin, has neither these nor a brief.
- The directory the call's loop runs in, and what the checks it asked for found.

Your messages are kept per conversation in the plugin's store, so a conversation you resume still has them, and `/clear` starts with none; the store keeps the 50 conversations you wrote in most recently. A conversation keeps your first message and the newest that fit 64,000 characters, each message cut to its head and tail past 16,000. Both limits count the messages as JSON, in which quotes, backslashes, and control characters take more than one character. The judge reads all your kept messages for every call, with a note where the store let messages go.
Expand All @@ -91,7 +92,7 @@ Each judge runs through your session's own model client: one model call per tool

A judge allows a call only when its reply ends with an `allow` verdict. A reply that does not end with a verdict or that states different verdicts along the way, a call the judge does not answer in time, and any other verdict go to the second judge if there is one, or else leave the classifier's verdict.

The policy and your kept messages open every judge's prompt and are marked for the prompt cache, so a call soon after another, and the call after a check, can read them from the cache until you write again; the `usage` in each `judged` line shows what was read and written.
The policy and your kept messages open every judge's prompt and are marked for the prompt cache, so a call soon after another, and the call after a check, can read them from the cache until you write again; the call's own fields, the agent's earlier calls among them, follow uncached. The `usage` in each `judged` line shows what was read and written.

On Bedrock, Vertex, Foundry, or behind a gateway, Claude Code finds cached text again only while the prompt opens the same way, and the judge's prompt opens with your kept messages, so after each message you write the judge's next call writes its instructions to the cache again along with your messages.

Expand All @@ -111,6 +112,10 @@ On Bedrock, Vertex, Foundry, or behind a gateway, Claude Code finds cached text
- A full-output file the plugin redacts is rewritten as text, so bytes in it that are not UTF-8, as in binary output, are replaced.
- For a call moved to the background while it runs, the plugin scans only the hand-off. Output delivered later, including task notifications, is outside that scan; a later read is scanned only if the plugin allows that call.
- A `Monitor` call starts a watch whose events reach the model later, one line at a time; the judge may allow it, and the plugin does not scan those events.
- The agent's earlier calls show state when the agent read it, and the judge takes it as still true: the state can change before the call, as when someone else answers a prompt the agent read, and the judge cannot see that.
- An earlier call's result holds whatever its source wrote, such as a web page, an issue, or another agent's reply. The judge is told that such text grants nothing and does not show a call is safe, but a model judge can still be misled by it, which no instruction rules out.
- A target the agent read before the newest 16,000 characters of its calls, or in the middle of a result cut to its head and tail, is not shown to the judge, which then does not allow a call whose effect turns on it unless your words or a check's answers show that effect.
- The judge's `reason` is its own words, which the log records and an allow passes to Claude Code as its reason; it is told not to copy text from the agent's earlier calls, but may. The paths and refs it asks to check, which the log records in `checks` and `facts`, can likewise name one it saw only in an earlier call's output. An earlier call the classifier allowed was not scanned, so a credential its output held can reach the log that way. Those outputs also go to your session's model client in the judge's prompt, as they already went in the agent's.
- After the plugin redacted or withheld a call's output, the call run again keeps the classifier's verdict only when its input is the same; a rerun with any input change is judged again.

## Reading the log
Expand All @@ -122,7 +127,7 @@ To count the classifier's denials, count the lines whose `classifier.decision` i
A call ends in one of these:

- `classified`: a call the classifier allowed, left to it without asking the judge.
- `judged`: a call the judge was asked about, with how many of your kept messages it read and how many the store had let go in `messages`, and the first judge's fields listed below. When a second judge ran, `escalation` holds the same fields for it, and its verdict decided the plugin's answer: an `allow` answered the classifier's verdict with an allow, and any other verdict left the classifier's. The line records that answer, not that the call ran: Claude Code keeps its own verdict where only it may decide, such as a script's call under Code Mode.
- `judged`: a call the judge was asked about, with how many of your kept messages it read and how many the store had let go in `messages`, the `tool_use_id`s of the agent's earlier calls it read in `toolUseIds`, oldest first, which is absent when Claude Code did not give the plugin the agent's loop and empty when the loop had no finished call, and the first judge's fields listed below. When a second judge ran, `escalation` holds the same fields for it, and its verdict decided the plugin's answer: an `allow` answered the classifier's verdict with an allow, and any other verdict left the classifier's. The line records that answer, not that the call ran: Claude Code keeps its own verdict where only it may decide, such as a script's call under Code Mode.
- `owned`: a verdict another plugin gave, or a call whose asks a host answers or whose tool your organization caps, left as it came without asking the judge (see [What it leaves to native approval](#what-it-leaves-to-native-approval)), with the field that named the decider in `ownedBy` and its value.
- `reserved`: a call of a tool whose verdict the engine keeps, left to it without asking the judge.
- `background`: a call run in the background, left to the classifier without asking the judge.
Expand Down
43 changes: 43 additions & 0 deletions plugins/preallow/hooks/calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// The calling agent's own earlier tool calls: what it has already read of the
// state a call acts on, such as the prompt a keypress answers. The judge
// weighs them as evidence of that state and never as the user's words.

import type { SessionMessage } from 'claude-code';

import { clipJson } from './messages.ts';

export type ToolCall = { tool: string; input: string; text: string; isError?: true };

const INPUT_CHARS = 2000;
const TEXT_CHARS = 4000;
// Several times one call's limits, so the newest call always fits.
const CALLS_CHARS = 16_000;

// The newest answered calls that fit, oldest first, with their ids for the
// log. A call still in flight, the judged one among them, has no text yet.
// The first call that does not fit ends the list, so an older call never
// stands in for a newer one that may contradict it.
export function recentCalls(rows: readonly SessionMessage[]): {
calls: ToolCall[];
ids: string[];
} {
const calls: ToolCall[] = [];
const ids: string[] = [];
let room = CALLS_CHARS;
for (const use of rows.flatMap((row) => row.toolUses).toReversed()) {
if (use.text === undefined) continue;
const call: ToolCall = {
tool: use.tool,
input: clipJson(JSON.stringify(use.input), INPUT_CHARS),
text: clipJson(use.text, TEXT_CHARS),
...(use.isError === true ? { isError: true } : {}),
};
// Each kept call also costs the comma that separates it.
const size = JSON.stringify(call).length + 1;
if (size > room) break;
room -= size;
calls.unshift(call);
ids.unshift(use.tool_use_id);
}
return { calls, ids };
}
30 changes: 18 additions & 12 deletions plugins/preallow/hooks/judge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ModelUsage,
} from 'claude-code';

import { recentCalls, type ToolCall } from './calls.ts';
import type { FactRequest } from './facts.ts';
import { JUDGE_SYSTEM } from './policy.ts';
import { parseVerdict, type Reply } from './verdict.ts';
Expand All @@ -24,6 +25,8 @@ export type JudgeCase = {
messages: UserMessages;
// For a subagent's call, the brief its parent agent gave it.
brief?: string;
// The calling agent's own earlier calls, absent when its loop cannot be read.
toolCalls?: readonly ToolCall[];
// The directory the call's loop runs in.
cwd?: string;
// Answers to the checks the judge asked for.
Expand Down Expand Up @@ -88,6 +91,7 @@ export function judgePrompt(call: JudgeCase): ModelTextBlock[] {
{
...words,
...(call.brief === undefined ? {} : { subagent_brief: clip(call.brief, BRIEF_CHARS) }),
recent_tool_calls: call.toolCalls,
cwd: call.cwd,
tool: call.tool,
input: call.input,
Expand Down Expand Up @@ -199,27 +203,29 @@ export async function judge(call: JudgeCase, host: JudgeHost): Promise<Verdict>
};
}

// For a subagent's call, the first message of its loop: the brief its parent
// gave it.
async function briefOf($: EngineInterface, agentId: string | undefined) {
if (agentId === undefined) return undefined;
const messages = await $.session.messages({ agentId });
if (!Array.isArray(messages)) return undefined;
return messages.find((message) => message.role === 'user')?.text;
async function loopOf($: EngineInterface, agentId: string | undefined) {
const rows = await (agentId === undefined
? $.session.messages()
: $.session.messages({ agentId }));
return Array.isArray(rows) ? rows : undefined;
}

// What the judge reads about a call before any check.
// What the judge reads about a call before any check, and the ids of the
// earlier calls it reads, for the log.
export async function judgeCase(
$: EngineInterface,
tool: string,
input: unknown,
agentId: string | undefined,
): Promise<JudgeCase & { cwd: string }> {
const [messages, brief, cwd] = await Promise.all([
): Promise<JudgeCase & { cwd: string; toolUseIds?: string[] }> {
const [messages, rows, cwd] = await Promise.all([
userMessages($),
briefOf($, agentId),
loopOf($, agentId),
// Read inside the hook: a subagent's loop may run in a directory of its own.
$.session.cwd(),
]);
return { tool, input, messages, brief, cwd };
// A subagent's loop opens with the brief its parent gave it.
const brief = agentId === undefined ? undefined : rows?.find((row) => row.role === 'user')?.text;
const recent = rows === undefined ? undefined : recentCalls(rows);
return { tool, input, messages, brief, toolCalls: recent?.calls, cwd, toolUseIds: recent?.ids };
}
Loading