Skip to content
Open
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 scripts/check-frontend-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const FORBIDDEN: Boundary[] = [
{ module: "context/plans", reason: "plan content arrives with the plan_approval request" },
{ module: "utils/thinking", reason: "thinking settings are part of SessionState" },
{ module: "utils/teammateMailbox", reason: "background wake-ups are handled by the session" },
{ module: "permissions/permissions", reason: "confirmations are interaction requests" },
{ module: "permissions/", reason: "confirmations are interaction requests" },
{ module: "tools/bashTool", reason: "use AgentSession.runShell" },
];

Expand Down
125 changes: 125 additions & 0 deletions src/permissions/autoMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { harnessSetting } from "../harness/active.js";
import type { BashReadOnlyAnalysis } from "../tools/bashReadOnlyAnalysis.js";
import { classifyAutoModeAction } from "./autoClassifier.js";
import {
recordClassifierDenial,
recordClassifierFailure,
recordClassifierSuccess,
shouldFallbackToPrompting,
} from "./autoModeState.js";
import { stripDangerousAllowRules } from "./dangerousPatterns.js";
import { extractBashCommand, isCoordinationTool, isHardDeniedBashCommand, matchesAnyRule } from "./rules.js";
import type {
PermissionCheckParams,
PermissionRequest,
PermissionResponse,
PermissionRuleSet,
PermissionSettings,
} from "./types.js";

/**
* Auto Mode decision pipeline. Reached only when `mode === "auto"`.
*
* Order is deliberate and security-first:
* 1. Coordination-only tools → allow (never classified)
* 2. EnterPlanMode → ask (a mode switch is a conscious choice)
* 3. Explicit deny rules → deny (always win, before any allow)
* 4. Bash hard-deny blacklist → deny (never-safe; saves a classifier call)
* 5. Read-only fast-path → allow (saves a classifier call)
* 6. Explicit allow rules → allow
* 7. AI classifier → allow / (block → deny) / (unavailable → ask);
* ask without calling it when the Agent turns `features.autoModeClassifier` off
*
* The classifier never auto-allows on uncertainty: an unavailable verdict
* falls back to the normal confirmation UI and feeds the failure circuit
* breaker. Consecutive blocks are tracked as well; once the threshold is
* reached, further blocks degrade to `ask` so the human decides instead.
*/
export async function resolveAutoModeDecision(
params: PermissionCheckParams,
request: PermissionRequest,
settings: PermissionSettings,
sessionRules: PermissionRuleSet,
bashAnalysis?: BashReadOnlyAnalysis,
): Promise<PermissionResponse> {
if (isCoordinationTool(params.tool.name)) {
return { behavior: "allow", reason: `${params.tool.name} writes coordination-only state`, request };
}

if (params.tool.name === "EnterPlanMode") {
return { behavior: "ask", reason: "entering plan mode requires confirmation", request };
}

if (
matchesAnyRule(sessionRules.deny, params.tool.name, params.input) ||
matchesAnyRule(settings.deny, params.tool.name, params.input)
) {
return { behavior: "deny", reason: "matched deny rule", request };
}

if (params.tool.name === "Bash") {
const command = extractBashCommand(params.input);
if (isHardDeniedBashCommand(command)) {
return { behavior: "deny", reason: "high-risk shell command blocked in auto mode", request };
}
if (bashAnalysis?.isReadOnly) {
return { behavior: "allow", reason: "read-only shell command", request };
}
} else if (params.tool.isReadOnly()) {
return { behavior: "allow", reason: "read-only tool", request };
}

// Honor explicit allow rules — but NOT dangerous ones (interpreters, bare
// Bash, Agent), which would let the agent bypass the classifier. Those are
// stripped here so the action falls through to the classifier instead.
const safeSessionAllow = stripDangerousAllowRules(sessionRules.allow);
const safeSettingsAllow = stripDangerousAllowRules(settings.allow);
if (
matchesAnyRule(safeSessionAllow, params.tool.name, params.input) ||
matchesAnyRule(safeSettingsAllow, params.tool.name, params.input)
) {
return { behavior: "allow", reason: "matched allow rule", request };
}

if (!harnessSetting<boolean>("features.autoModeClassifier")) {
return { behavior: "ask", reason: "Auto Mode classifier is off for this Agent — manual review", request };
}

const verdict = await classifyAutoModeAction({
messages: params.messages ?? [],
toolName: params.tool.name,
toolInput: params.input,
allowRules: [...safeSessionAllow, ...safeSettingsAllow],
denyRules: [...sessionRules.deny, ...settings.deny],
model: params.model,
});

// Classifier unavailable (API/parse failure): never auto-allow on
// uncertainty. Degrade to manual confirmation and feed the circuit breaker
// — enough consecutive failures disables the classifier for the session.
if (verdict.unavailable) {
recordClassifierFailure();
return {
behavior: "ask",
reason: `classifier unavailable — manual review (${verdict.reason})`,
request,
};
}

if (verdict.shouldBlock) {
recordClassifierDenial();
// After enough consecutive blocks, stop silently denying and let the
// human decide instead (the classifier reason is surfaced in the prompt).
if (shouldFallbackToPrompting()) {
return {
behavior: "ask",
reason: `classifier blocked repeatedly — please review: ${verdict.reason}`,
request,
};
}
return { behavior: "deny", reason: verdict.reason, request };
}

recordClassifierSuccess();
return { behavior: "allow", reason: `auto-approved: ${verdict.reason}`, request };
}
Loading
Loading