Enhancements for Claude Code auto mode

Enhancements for Claude Code auto mode.
Records each tool call's permission verdict. It depends on the mod-events plugin, which writes its events.
tool.check: keeps the engine's verdict (allow, ask, deny), the deciding rule or hook, and the loop's agentId.tool.call: times the call and emits a tool.verdict event with the verdict. Tools that wait on the person (AskUserQuestion, the plan-mode tools) are marked interactive.session.start: emits a session.start event.turn.step: emits a server.tool event for each tool the API ran inside a request.The observability:session index ingests the events as mod_events and reads them through the classifier_verdicts view. The engine does not expose the permission mode to tool.check, so a tool.verdict cannot tell an auto-mode classifier ask from a dialog ask. tool_errors carries each classifier refusal as denial_kind = 'automode-blocked'.
bun scripts/mod-test.ts auto-modemod/register.ts 125 lines1import type { EngineInterface, On, ToolCallResult } from "claude-code";
2
3type ModEventsInput = Parameters<EngineInterface["modEvents"]["emit"]>[0];
4
5const MOD = "auto-mode";
6
7/**
8 * Tools whose run waits on the person by design, so their wall time measures the person.
9 */
10const INTERACTIVE = new Set(["AskUserQuestion", "EnterPlanMode", "ExitPlanMode"]);
11
12interface Verdict {
13 decision: string;
14 reason: string | null;
15 rule: string | null;
16 hook: string | null;
17 setting: string | null;
18 decider: string | null;
19 ceiling: string | null;
20 checkedAt: number;
21}
22
23function keyOf(agentId: string | undefined, toolUseId: string): string {
24 return `${agentId ?? ""}/${toolUseId}`;
25}
26
27function outcomeOf(result: ToolCallResult): "ok" | "error" | "deny" {
28 if (result.deny !== undefined) return "deny";
29 if (result.isError) return "error";
30 return "ok";
31}
32
33/**
34 * Records each tool call's permission verdict and each server tool the API ran.
35 * An `ask` goes to the mode's decider, which the mod API does not expose, so
36 * a verdict's `ms` spans the decider plus the tool. `check_ms` is the time to the verdict.
37 */
38export function register(on: On): void {
39 const verdicts = new Map<string, Verdict>();
40
41 on("session.start", ($, e, next) => {
42 void $.modEvents.emit({ mod: MOD, event: "session.start" });
43 return next(e);
44 });
45
46 // Both hooks only observe, so a failure lets the call go on as the engine settled it.
47 on("tool.check", async ($, e, next) => {
48 const verdict = await next(e);
49 if (e.tool_use_id !== undefined) {
50 verdicts.set(keyOf(e.agentId, e.tool_use_id), {
51 decision: verdict.decision,
52 reason: verdict.reason ?? null,
53 rule: verdict.rule ?? null,
54 hook: verdict.hook ?? null,
55 setting: verdict.setting ?? null,
56 decider: verdict.decider ?? null,
57 ceiling: verdict.ceiling ?? null,
58 checkedAt: await $.clock.now(),
59 });
60 }
61 return verdict;
62 }).catch(($, e, next) => next(e));
63
64 on("tool.call", async ($, e, next) => {
65 const key = keyOf(e.agentId, e.tool_use_id);
66 const startedAt = await $.clock.now();
67 const settled: { result: ToolCallResult } | { thrown: unknown } = await next(e).then(
68 (result: ToolCallResult) => ({ result }),
69 (thrown: unknown) => ({ thrown }),
70 );
71 const verdict = verdicts.get(key);
72 verdicts.delete(key);
73 const finishedAt = await $.clock.now();
74 const outcome = "result" in settled ? outcomeOf(settled.result) : "throw";
75 void $.modEvents.emit({
76 mod: MOD,
77 event: "tool.verdict",
78 ok: outcome === "ok",
79 ms: finishedAt - startedAt,
80 detail: {
81 tool_use_id: e.tool_use_id,
82 agent_id: e.agentId ?? null,
83 tool: e.tool,
84 interactive: INTERACTIVE.has(e.tool),
85 decision: verdict?.decision ?? null,
86 rule: verdict?.rule ?? null,
87 hook: verdict?.hook ?? null,
88 setting: verdict?.setting ?? null,
89 decider: verdict?.decider ?? null,
90 ceiling: verdict?.ceiling ?? null,
91 reason: verdict?.reason ?? null,
92 read_only: "result" in settled ? settled.result.isReadOnly === true : null,
93 started_at: startedAt,
94 check_ms: verdict === undefined ? null : verdict.checkedAt - startedAt,
95 outcome,
96 },
97 });
98 if ("thrown" in settled) throw settled.thrown;
99 return settled.result;
100 }).catch(($, e, next) => next(e));
101
102 on("turn.step", async function* ($, e, next) {
103 const response = yield* next(e);
104 const events: ModEventsInput[] = [];
105 for (const use of response.serverToolUses ?? []) {
106 const event: ModEventsInput = {
107 mod: MOD,
108 event: "server.tool",
109 detail: {
110 tool_use_id: use.id,
111 agent_id: e.agentId ?? null,
112 tool: use.name,
113 turn_id: response.turnId,
114 step: response.index,
115 started_at: use.startedAt,
116 },
117 };
118 if (use.endedAt !== undefined) event.ms = use.endedAt - use.startedAt;
119 events.push(event);
120 }
121 await Promise.all(events.map((event) => $.modEvents.emit(event)));
122 return response;
123 });
124}
125