pi-verdict 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -99,7 +99,9 @@ pi-verdict runs on both [pi](https://github.com/badlogic/pi-mono) and [oh-my-pi]
99
99
  ],
100
100
  "builtinDenyFloor": true,
101
101
  "classifierModel": null,
102
- "toggleShortcut": "ctrl+shift+a"
102
+ "toggleShortcut": "ctrl+shift+a",
103
+ "audit": false,
104
+ "notifyAllows": false
103
105
  }
104
106
  ```
105
107
 
@@ -108,6 +110,8 @@ pi-verdict runs on both [pi](https://github.com/badlogic/pi-mono) and [oh-my-pi]
108
110
  - `builtinDenyFloor: false` turns off the built-in danger/path floor (your risk; the self-protection layer below always stays on)
109
111
  - `classifierModel` pins the classifier model, e.g. `"zai/glm-5.3-flash:low"` (thinking suffix supported; default: session model with thinking off)
110
112
  - `classifierModel: "typesafe/jev-latest"` opts into the bundled **jev decisions adapter** — gray-zone verdicts via TypeSafe's jev on OpenRouter (`/api/alpha/decisions`), reusing your pi OpenRouter login; experimental, see [ADR-0003](docs/adr/0003-jev-decisions-adapter.md)
113
+ - `audit: true` records every **gray-zone adjudication** (the full transcript sent to the classifier, its raw response, the parsed verdict) as JSONL under `~/.pi/agent/verdicts/<sessionId>.jsonl` — one file per session, the 20 most recent kept. Local-only and full-fidelity (protected-path plaintext may appear — it never leaves your machine; [ADR-0002](docs/adr/0002-deny-paths-deterministic-ask.md) boundary note); the agent can neither read nor write the directory. `/automode` shows the audit state and path while on
114
+ - `notifyAllows: true` notifies on every **classifier allow** (reason + action line — e.g. jev's probability breakdown); default `false` keeps passes silent. Mechanical passes (your own allow rules, protected-path confirms) never notify; shadow-cache annotations stay debug-only; with both switches on the notification appears once
111
115
 
112
116
  No built-in allowlist — every "always allow" claim is yours ([why](docs/configuration.md#why-no-built-in-allowlist)). Full reference: [docs/configuration.md](docs/configuration.md).
113
117
 
package/README.zh-CN.md CHANGED
@@ -101,7 +101,9 @@ pi-verdict 同时支持 [pi](https://github.com/badlogic/pi-mono) 与 [oh-my-pi]
101
101
  ],
102
102
  "builtinDenyFloor": true,
103
103
  "classifierModel": null,
104
- "toggleShortcut": "ctrl+shift+a"
104
+ "toggleShortcut": "ctrl+shift+a",
105
+ "audit": false,
106
+ "notifyAllows": false
105
107
  }
106
108
  ```
107
109
 
@@ -110,6 +112,8 @@ pi-verdict 同时支持 [pi](https://github.com/badlogic/pi-mono) 与 [oh-my-pi]
110
112
  - `builtinDenyFloor: false` 整体关闭内置危险/路径拦截(风险自担;下方自保护层永远开启)
111
113
  - `classifierModel` 指定分类器模型,如 `"zai/glm-5.3-flash:low"`(支持思考后缀;缺省 = 会话模型且显式关思考)
112
114
  - `classifierModel: "typesafe/jev-latest"` 启用随包的 **jev 决策适配器**——灰区裁决经 OpenRouter 的 TypeSafe jev(`/api/alpha/decisions`)完成,复用 pi 的 OpenRouter 登录态;实验性质,详见 [ADR-0003](docs/adr/0003-jev-decisions-adapter.md)
115
+ - `audit: true` 把每次**灰区裁决**(发给分类器的完整转录、其原始响应、解析出的裁决)以 JSONL 记录到 `~/.pi/agent/verdicts/<sessionId>.jsonl`——按会话一分文件,保留最近 20 个。仅存本机且全保真(受保护路径明文可能出现——永不出本机;[ADR-0002](docs/adr/0002-deny-paths-deterministic-ask.md) 边界注);agent 对该目录读写双拒。开启时 `/automode` 会显示审计状态与路径
116
+ - `notifyAllows: true` 对每次 **classifier 放行**发通知(reason + action 行——如 jev 的概率分解);默认 `false` 保持放行静默。机械放行(你自己的 allow 规则、protected-path 确认)永不通知;shadow 标注仍属 debug;两开关同开时通知只出现一次
113
117
 
114
118
  没有内置白名单——每一条「永远放行」声明都归你([为什么](docs/configuration.md#why-no-built-in-allowlist))。完整参考:[docs/configuration.md](docs/configuration.md)。
115
119
 
@@ -259,9 +259,13 @@ interface UserRules {
259
259
  classifierModel: string | null;
260
260
  /** 主开关 toggle 快捷键键位(#15);null = 禁用;缺省 DEFAULT_TOGGLE_SHORTCUT */
261
261
  toggleShortcut: string | null;
262
+ /** Opt-in gray-zone adjudication audit (#54): per-session JSONL under <agentDir>/verdicts/ */
263
+ audit: boolean;
264
+ /** Allow visibility (#60): info notification on classifier allows; mechanical passes stay silent. Default off. */
265
+ notifyAllows: boolean;
262
266
  }
263
267
 
264
- const EMPTY_RULES: UserRules = { allow: [], deny: [], denyPaths: [], builtinDenyFloor: true, classifierModel: null, toggleShortcut: DEFAULT_TOGGLE_SHORTCUT };
268
+ const EMPTY_RULES: UserRules = { allow: [], deny: [], denyPaths: [], builtinDenyFloor: true, classifierModel: null, toggleShortcut: DEFAULT_TOGGLE_SHORTCUT, audit: false, notifyAllows: false };
265
269
 
266
270
  /** This module's own file location (import.meta.url resolved; null = unresolvable). */
267
271
  const OWN_FILE_PATH: string | null = (() => {
@@ -324,6 +328,8 @@ const USER_CONFIG_TEMPLATE = `${JSON.stringify({
324
328
  builtinDenyFloor: true,
325
329
  classifierModel: null,
326
330
  toggleShortcut: DEFAULT_TOGGLE_SHORTCUT,
331
+ audit: false,
332
+ notifyAllows: false,
327
333
  }, null, 2)}\n`;
328
334
 
329
335
  /**
@@ -341,7 +347,7 @@ function loadUserRules(): { rules: UserRules; skipped: string[]; shortcutWarning
341
347
  } catch { /* 只读环境静默跳过 */ }
342
348
  return { rules: EMPTY_RULES, skipped: [], shortcutWarning: null };
343
349
  }
344
- let raw: { allow?: unknown; deny?: unknown; denyPaths?: unknown; builtinDenyFloor?: unknown; classifierModel?: unknown; toggleShortcut?: unknown };
350
+ let raw: { allow?: unknown; deny?: unknown; denyPaths?: unknown; builtinDenyFloor?: unknown; classifierModel?: unknown; toggleShortcut?: unknown; audit?: unknown; notifyAllows?: unknown };
345
351
  try {
346
352
  raw = JSON.parse(fs.readFileSync(p, "utf8")) as typeof raw;
347
353
  } catch (err) {
@@ -378,6 +384,8 @@ function loadUserRules(): { rules: UserRules; skipped: string[]; shortcutWarning
378
384
  builtinDenyFloor: raw.builtinDenyFloor !== false,
379
385
  classifierModel: typeof raw.classifierModel === "string" && raw.classifierModel.trim() ? raw.classifierModel.trim() : null,
380
386
  toggleShortcut: shortcut.key,
387
+ audit: raw.audit === true,
388
+ notifyAllows: raw.notifyAllows === true,
381
389
  },
382
390
  skipped,
383
391
  shortcutWarning: shortcut.warning,
@@ -577,6 +585,8 @@ interface ProtectedSet {
577
585
  exact: string[];
578
586
  /** 受保护目录前缀(npm 包安装形态:整个包目录) */
579
587
  prefixes: string[];
588
+ /** 读拒绝前缀(#54):verdicts 审计目录——记录含不可信原始输出,禁回流 agent context */
589
+ readPrefixes: string[];
580
590
  /** bash/powershell 命令串危险特征(子串匹配,可绕——变更检测兜底) */
581
591
  bashPatterns: RegExp[];
582
592
  /** 变更检测基线(词法路径 + 类别;session_start 时快照全文) */
@@ -713,7 +723,28 @@ export function buildProtectedSet(agentDir: string, ownFile: string | null): Pro
713
723
  bashPatterns.push(new RegExp(`(?:${[...alts].join("|")})`));
714
724
  }
715
725
 
716
- return { exact: [...exact], prefixes: [...prefixes], bashPatterns, watchBases };
726
+ // #54 verdicts dir: gate-owned audit storage. Writes ride the normal prefixes;
727
+ // reads are denied separately — records carry raw model output (including
728
+ // fail-closed failures) that must not flow back into agent context. Deliberately
729
+ // NOT added to watchBases: the log legitimately grows every adjudication, so a
730
+ // snapshot diff would false-positive as tampering.
731
+ const verdictsForms = baseForms(path.join(agentDir, "verdicts"));
732
+ for (const f of verdictsForms) prefixes.add(f);
733
+ const home = os.homedir();
734
+ const vAlts = new Set<string>(verdictsForms.map(escapeRegExp));
735
+ for (const f of verdictsForms) {
736
+ if (f.startsWith(home + path.sep)) {
737
+ const rel = f.slice(home.length + 1);
738
+ vAlts.add(escapeRegExp("~/" + rel));
739
+ vAlts.add("\\$HOME/" + escapeRegExp(rel));
740
+ }
741
+ for (const base of baseForms(agentDir)) {
742
+ if (f.startsWith(base + path.sep)) vAlts.add("\\$PI_CODING_AGENT_DIR/" + escapeRegExp(f.slice(base.length + 1)));
743
+ }
744
+ }
745
+ bashPatterns.push(new RegExp(`(?:${[...vAlts].join("|")})`));
746
+
747
+ return { exact: [...exact], prefixes: [...prefixes], readPrefixes: verdictsForms, bashPatterns, watchBases };
717
748
  }
718
749
 
719
750
  /** Does the resolved write path hit the protected set (realpath guards against
@@ -730,6 +761,20 @@ export function isProtectedWritePath(rawPath: string, cwd: string, prot: Protect
730
761
  return false;
731
762
  }
732
763
 
764
+ /** Read-deny for the verdicts dir (#54): audit records contain raw fail-closed
765
+ * model output — untrusted text that must not flow back into agent context.
766
+ * Unlike write protection (prefixes) this is read semantics, hence a separate set. */
767
+ export function isProtectedReadPath(rawPath: string | undefined, cwd: string, prot: ProtectedSet): boolean {
768
+ if (prot.readPrefixes.length === 0) return false;
769
+ const target = rawPath ?? cwd; // #48: absent path → cwd is the effective target
770
+ for (const c of rebuiltForms(path.resolve(cwd, expandHome(target)))) {
771
+ for (const p of prot.readPrefixes) {
772
+ if (c === p || c.startsWith(p + path.sep)) return true;
773
+ }
774
+ }
775
+ return false;
776
+ }
777
+
733
778
  /** 自保护层裁决(第 0 层,先于一切):触碰门禁自身文件 → 不可豁免的 deny;其余 null 交后续层 */
734
779
  function selfProtectCheck(toolName: string, input: Record<string, unknown>, cwd: string, prot: ProtectedSet): RuleResult | null {
735
780
  switch (toolName) {
@@ -739,6 +784,14 @@ function selfProtectCheck(toolName: string, input: Record<string, unknown>, cwd:
739
784
  return { verdict: "deny", reason: `self-protection layer (ADR-0001): ${input.path} is part of the permission gate itself; agent-side modification is denied — edit it manually outside pi if intended` };
740
785
  }
741
786
  return null;
787
+ case "read":
788
+ case "grep":
789
+ case "find":
790
+ case "ls":
791
+ if (isProtectedReadPath(typeof input.path === "string" ? input.path : undefined, cwd, prot)) {
792
+ return { verdict: "deny", reason: `self-protection layer (#54): ${typeof input.path === "string" ? input.path : cwd} holds the gate's verdict audit records — agent reads are denied (untrusted raw model output inside); view them outside pi` };
793
+ }
794
+ return null;
742
795
  case "bash":
743
796
  case "powershell": {
744
797
  const cmd = String(input.command ?? "");
@@ -994,6 +1047,8 @@ interface ClassifierOutcome {
994
1047
  verdict: "allow" | "ask" | "deny";
995
1048
  reason: string;
996
1049
  source: "model" | "fail-closed";
1050
+ /** #54 audit material: the transcript actually sent and the last attempt's raw output (attached on both model and fail-closed outcomes) */
1051
+ auditRaw?: { transcript: string; rawResponse: string; modelId: string; thinking: ThinkingLevel };
997
1052
  }
998
1053
 
999
1054
  const CLASSIFIER_TIMEOUT_MS = 25_000; // 本网关 CC 分类器分布 p90=19.8s(15s 会误杀 ~15%),research/cache-sim 数据
@@ -1003,6 +1058,22 @@ const APIS_WITHOUT_TEMPERATURE = new Set<string>([
1003
1058
  "openai-codex-responses",
1004
1059
  ]);
1005
1060
 
1061
+ // Models whose provider rejected a temperature-bearing request ("`temperature`
1062
+ // is deprecated for this model" — current-gen Anthropic models, #47). Filled
1063
+ // adaptively and cached for the extension's lifetime: pi's model registry has
1064
+ // no sampling-capability metadata and the reject/accept split follows neither
1065
+ // `api` nor `reasoning`, so the provider's own error is the only reliable
1066
+ // signal. Later calls for a cached model omit the parameter upfront.
1067
+ const TEMPERATURE_REJECTED_MODELS = new Set<string>();
1068
+
1069
+ /** The provider rejected the request over the `temperature` parameter itself (#47). */
1070
+ function temperatureRejection(
1071
+ r: { ok: true; stopReason: string; errorMessage?: string } | { ok: false; error: string },
1072
+ ): boolean {
1073
+ if (r.ok) return (r.stopReason === "error" || r.stopReason === "aborted") && /temperature/i.test(r.errorMessage ?? "");
1074
+ return /temperature/i.test(r.error);
1075
+ }
1076
+
1006
1077
  /**
1007
1078
  * Minimal structural shape of a completion call (#35). pi exposes it as
1008
1079
  * ModelRegistry.complete; omp 18 does not, but the pi-ai compat module exports
@@ -1056,7 +1127,13 @@ function completionFor(registry: { complete?: unknown }, compatLoader?: CompatLo
1056
1127
  /** 分类器思考级别(pi 原生词表;后缀语法对齐 pi --model provider/id:thinking) */
1057
1128
  type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
1058
1129
 
1059
- /** 单次分类器调用:显式 reasoning:"off"(见下方注释),失败返回错误串而非抛出 */
1130
+ /**
1131
+ * Single classifier attempt: reasoning "off" by default (see options below);
1132
+ * failures return an error string instead of throwing. A provider rejection
1133
+ * over `temperature` strips the parameter and retries once at the same tier
1134
+ * (#47) — models that accept it keep the temperature 0 determinism pin,
1135
+ * models that deprecate it self-heal instead of fail-closing every call.
1136
+ */
1060
1137
  async function callClassifierOnce(
1061
1138
  host: PipelineHost,
1062
1139
  signal: AbortSignal | undefined,
@@ -1067,50 +1144,62 @@ async function callClassifierOnce(
1067
1144
  thinking: ThinkingLevel = "off",
1068
1145
  systemPrompt: string = CLASSIFIER_SYSTEM,
1069
1146
  ): Promise<{ ok: true; text: string; stopReason: string; errorMessage?: string } | { ok: false; error: string }> {
1070
- const signals = [AbortSignal.timeout(CLASSIFIER_TIMEOUT_MS)];
1071
- if (signal) signals.push(signal);
1072
- try {
1073
- const response = await complete(
1074
- model,
1075
- {
1076
- systemPrompt,
1077
- messages: [{ role: "user", content: userMessage, timestamp: Date.now() }],
1078
- },
1079
- {
1080
- signal: AbortSignal.any(signals),
1081
- maxTokens,
1082
- ...(APIS_WITHOUT_TEMPERATURE.has(model.api) ? {} : { temperature: 0 }),
1083
- // Thinking params go out in both hosts' native dialects (#35):
1084
- // pi's registry.complete consumes thinkingEnabled/effort (the
1085
- // API-native fields, per the blackhole findings in
1086
- // research/thinking-param-blackhole.md); omp's compat complete
1087
- // consumes reasoning/disableReasoning. Both sides ignore unknown
1088
- // option fields, so dual-send lets each host pick its own.
1089
- // pi off = explicitly disabled (verified to send
1090
- // thinking:{"type":"disabled"}; GLM downgrades to effort-low light
1091
- // thinking); suffix levels arrive via adaptive effort (minimal→low).
1092
- // omp off = disableReasoning (without it, an absent `reasoning`
1093
- // leaves the model default undefined); level vocabularies share the
1094
- // ThinkingLevel word list, reasoning passes through as-is.
1095
- ...(thinking === "off"
1096
- ? { thinkingEnabled: false, disableReasoning: true }
1097
- : {
1098
- thinkingEnabled: true,
1099
- effort: thinking === "minimal" ? ("low" as const) : thinking,
1100
- reasoning: thinking === "minimal" ? ("low" as const) : thinking,
1101
- }),
1102
- cacheRetention: "short",
1103
- sessionId: host.getSessionId(),
1104
- },
1105
- );
1106
- const text = response.content
1107
- .filter((b) => b.type === "text")
1108
- .map((b) => b.text)
1109
- .join("");
1110
- return { ok: true, text, stopReason: response.stopReason ?? "unknown", errorMessage: response.errorMessage };
1111
- } catch (err) {
1112
- return { ok: false, error: err instanceof Error ? err.message : String(err) };
1147
+ const fire = async (
1148
+ withTemperature: boolean,
1149
+ ): Promise<{ ok: true; text: string; stopReason: string; errorMessage?: string } | { ok: false; error: string }> => {
1150
+ const signals = [AbortSignal.timeout(CLASSIFIER_TIMEOUT_MS)];
1151
+ if (signal) signals.push(signal);
1152
+ try {
1153
+ const response = await complete(
1154
+ model,
1155
+ {
1156
+ systemPrompt,
1157
+ messages: [{ role: "user", content: userMessage, timestamp: Date.now() }],
1158
+ },
1159
+ {
1160
+ signal: AbortSignal.any(signals),
1161
+ maxTokens,
1162
+ ...(withTemperature ? { temperature: 0 } : {}),
1163
+ // Thinking params go out in both hosts' native dialects (#35):
1164
+ // pi's registry.complete consumes thinkingEnabled/effort (the
1165
+ // API-native fields, per the blackhole findings in
1166
+ // research/thinking-param-blackhole.md); omp's compat complete
1167
+ // consumes reasoning/disableReasoning. Both sides ignore unknown
1168
+ // option fields, so dual-send lets each host pick its own.
1169
+ // pi off = explicitly disabled (verified to send
1170
+ // thinking:{"type":"disabled"}; GLM downgrades to effort-low light
1171
+ // thinking); suffix levels arrive via adaptive effort (minimal→low).
1172
+ // omp off = disableReasoning (without it, an absent `reasoning`
1173
+ // leaves the model default undefined); level vocabularies share the
1174
+ // ThinkingLevel word list, reasoning passes through as-is.
1175
+ ...(thinking === "off"
1176
+ ? { thinkingEnabled: false, disableReasoning: true }
1177
+ : {
1178
+ thinkingEnabled: true,
1179
+ effort: thinking === "minimal" ? ("low" as const) : thinking,
1180
+ reasoning: thinking === "minimal" ? ("low" as const) : thinking,
1181
+ }),
1182
+ cacheRetention: "short",
1183
+ sessionId: host.getSessionId(),
1184
+ },
1185
+ );
1186
+ const text = response.content
1187
+ .filter((b) => b.type === "text")
1188
+ .map((b) => b.text)
1189
+ .join("");
1190
+ return { ok: true, text, stopReason: response.stopReason ?? "unknown", errorMessage: response.errorMessage };
1191
+ } catch (err) {
1192
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
1193
+ }
1194
+ };
1195
+ const modelKey = `${model.api}|${model.id}`;
1196
+ const withTemperature = !APIS_WITHOUT_TEMPERATURE.has(model.api) && !TEMPERATURE_REJECTED_MODELS.has(modelKey);
1197
+ const first = await fire(withTemperature);
1198
+ if (withTemperature && temperatureRejection(first)) {
1199
+ TEMPERATURE_REJECTED_MODELS.add(modelKey);
1200
+ return fire(false);
1113
1201
  }
1202
+ return first;
1114
1203
  }
1115
1204
 
1116
1205
  /**
@@ -1133,14 +1222,16 @@ async function classifyWithModel(
1133
1222
  const systemPrompt = denyPathsActive ? CLASSIFIER_SYSTEM + DENY_PATHS_HINT : CLASSIFIER_SYSTEM;
1134
1223
  const attempts: Array<[number, number]> = [[1, CLASSIFIER_MAX_TOKENS], [2, CLASSIFIER_RETRY_MAX_TOKENS]];
1135
1224
  const failures: string[] = [];
1225
+ let rawResponse = ""; // #54: raw output of the last attempt ("" for exception attempts — diagnostics already live in failures)
1136
1226
  for (const [n, maxTokens] of attempts) {
1137
1227
  if (signal?.aborted) break; // 用户已取消,不再重试
1138
1228
  const r = await callClassifierOnce(host, signal, complete, model, userMessage, maxTokens, thinking, systemPrompt);
1139
1229
  if (r.ok) {
1230
+ rawResponse = r.text;
1140
1231
  const diag = `stopReason=${r.stopReason}, model=${model.id}, errorMessage=${JSON.stringify(r.errorMessage ?? null)}, raw output=${JSON.stringify(r.text.slice(0, 200))}`;
1141
1232
  if (r.stopReason !== "error" && r.stopReason !== "aborted") {
1142
1233
  const parsed = parseVerdict(r.text);
1143
- if (parsed) return { ...parsed, source: "model" };
1234
+ if (parsed) return { ...parsed, source: "model", auditRaw: { transcript, rawResponse, modelId: model.id, thinking } };
1144
1235
  failures.push(`attempt ${n} (${maxTokens}t) contract violation: ${diag}`);
1145
1236
  } else {
1146
1237
  failures.push(`attempt ${n} (${maxTokens}t) aborted/errored: ${diag}`);
@@ -1149,7 +1240,7 @@ async function classifyWithModel(
1149
1240
  failures.push(`attempt ${n} (${maxTokens}t) exception: ${r.error}`);
1150
1241
  }
1151
1242
  }
1152
- return { verdict: "deny", reason: `classifier failure (fail-closed): ${failures.join("; ")}`, source: "fail-closed" };
1243
+ return { verdict: "deny", reason: `classifier failure (fail-closed): ${failures.join("; ")}`, source: "fail-closed", auditRaw: { transcript, rawResponse, modelId: model.id, thinking } };
1153
1244
  }
1154
1245
 
1155
1246
  // ============================================================================
@@ -1271,6 +1362,90 @@ function shadowTag(probe: ShadowProbe): string {
1271
1362
  return `(shadow cache: miss:no-entry)`;
1272
1363
  }
1273
1364
 
1365
+ // ============================================================================
1366
+ // Gray-zone verdict audit (#54): opt-in JSONL decision records, observe-only
1367
+ // (never an adjudication input)
1368
+ // ============================================================================
1369
+
1370
+ const AUDIT_KEEP_SESSIONS = 20;
1371
+
1372
+ /** One gray-zone adjudication record (#54). Full fidelity on purpose: the file is
1373
+ * local-trust-domain (same as pi-verdict.json, per the ADR-0002 boundary note),
1374
+ * so protected-path plaintext is allowed here — it never leaves the machine nor
1375
+ * flows into agent context. */
1376
+ export interface AuditRecord {
1377
+ ts: string;
1378
+ sessionId: string;
1379
+ cwd: string;
1380
+ model: string | null;
1381
+ tool: string;
1382
+ input: unknown;
1383
+ actionLine: string;
1384
+ thinking: string | null;
1385
+ transcript: string | null;
1386
+ rawResponse: string | null;
1387
+ verdict: "allow" | "ask" | "deny";
1388
+ reason: string;
1389
+ source: "model" | "fail-closed";
1390
+ shadow: string;
1391
+ degraded: boolean;
1392
+ }
1393
+
1394
+ /** Audit sink (#54): append-only and fail-soft (the first write failure surfaces
1395
+ * once via drainWarning; verdicts are never affected). The dir is created
1396
+ * lazily — audit on with no gray-zone call all session leaves zero filesystem trace. */
1397
+ export class AuditLog {
1398
+ private warning: string | null = null;
1399
+ private warned = false;
1400
+ constructor(readonly dir: string) {}
1401
+
1402
+ append(record: AuditRecord): void {
1403
+ // sessionId comes from the host with no shape guarantee: narrow to a safe filename charset
1404
+ const file = path.join(this.dir, `${record.sessionId.replace(/[^a-zA-Z0-9_-]/g, "_")}.jsonl`);
1405
+ try {
1406
+ fs.mkdirSync(this.dir, { recursive: true });
1407
+ fs.appendFileSync(file, JSON.stringify(record) + "\n");
1408
+ } catch (err) {
1409
+ if (!this.warned) {
1410
+ this.warned = true;
1411
+ this.warning = `audit log write failed (${err instanceof Error ? err.message : String(err)}) — verdict records are NOT being persisted to ${this.dir}; adjudication is unaffected`;
1412
+ }
1413
+ }
1414
+ }
1415
+
1416
+ /** One-shot drain: the extension handler polls after every tool_call; first failure warns, the rest stay silent */
1417
+ drainWarning(): string | null {
1418
+ const w = this.warning;
1419
+ this.warning = null;
1420
+ return w;
1421
+ }
1422
+
1423
+ /** Keep the most recent AUDIT_KEEP_SESSIONS session files (called at session_start, best-effort) */
1424
+ prune(): void {
1425
+ let files: string[];
1426
+ try {
1427
+ files = fs.readdirSync(this.dir).filter((f) => f.endsWith(".jsonl"));
1428
+ } catch {
1429
+ return;
1430
+ }
1431
+ if (files.length <= AUDIT_KEEP_SESSIONS) return;
1432
+ const byMtime = files
1433
+ .map((f) => {
1434
+ let m = 0;
1435
+ try {
1436
+ m = fs.statSync(path.join(this.dir, f)).mtimeMs;
1437
+ } catch {}
1438
+ return { f, m };
1439
+ })
1440
+ .sort((a, b) => b.m - a.m);
1441
+ for (const { f } of byMtime.slice(AUDIT_KEEP_SESSIONS)) {
1442
+ try {
1443
+ fs.unlinkSync(path.join(this.dir, f));
1444
+ } catch {}
1445
+ }
1446
+ }
1447
+ }
1448
+
1274
1449
  // ============================================================================
1275
1450
  // 会话态:判定管线的会话期状态(复位清单集中一处)
1276
1451
  // ============================================================================
@@ -1284,11 +1459,20 @@ export class SessionState {
1284
1459
  readonly prot: ProtectedSet;
1285
1460
  readonly shadow = new ShadowCache();
1286
1461
  userRules: UserRules;
1462
+ audit: AuditLog | null;
1287
1463
  private denyPathBases: string[] | null = null;
1464
+ private readonly agentDir: string | null;
1288
1465
 
1289
- constructor(prot: ProtectedSet, userRules: UserRules = loadUserRules().rules) {
1466
+ constructor(prot: ProtectedSet, userRules: UserRules = loadUserRules().rules, agentDir: string | null = null) {
1290
1467
  this.prot = prot;
1291
1468
  this.userRules = userRules;
1469
+ this.agentDir = agentDir;
1470
+ this.audit = this.makeAudit(userRules);
1471
+ }
1472
+
1473
+ /** #54: the audit flag follows the rules (applies to new sessions); the dir is anchored to the install path */
1474
+ private makeAudit(rules: UserRules): AuditLog | null {
1475
+ return rules.audit && this.agentDir ? new AuditLog(path.join(this.agentDir, "verdicts")) : null;
1292
1476
  }
1293
1477
 
1294
1478
  /** 会话重置:重载用户规则(配置改动新会话生效)+ 按会话 cwd 重锚 denyPaths
@@ -1298,6 +1482,7 @@ export class SessionState {
1298
1482
  this.userRules = loaded.rules;
1299
1483
  this.denyPathBases = anchorDenyPaths(loaded.rules.denyPaths, cwd); // anchored to the session cwd, once (ADR-0002)
1300
1484
  this.shadow.reset();
1485
+ this.audit = this.makeAudit(loaded.rules);
1301
1486
  return { skipped: loaded.skipped, shortcutWarning: loaded.shortcutWarning };
1302
1487
  }
1303
1488
 
@@ -1362,15 +1547,41 @@ export async function adjudicate(
1362
1547
  }
1363
1548
 
1364
1549
  // 灰区 → 分类器;无可用模型 → fail-closed
1550
+ // #54: gray-zone only (rule-layer verdicts carry no transcript corpus —
1551
+ // brief decision); observe-only — recording never changes a verdict, and
1552
+ // write failures are swallowed fail-soft by the sink and surfaced once via drainWarning
1553
+ const actionLine = toolCallLine(call.toolName, call.input);
1554
+ const audit = (v: Pick<AuditRecord, "verdict" | "reason" | "source" | "degraded">, raw: ClassifierOutcome["auditRaw"] | null, shadow: string): void => {
1555
+ if (!state.audit) return;
1556
+ state.audit.append({
1557
+ ts: new Date().toISOString(),
1558
+ sessionId: env.host.getSessionId(),
1559
+ cwd: env.cwd,
1560
+ model: raw?.modelId ?? null,
1561
+ tool: call.toolName,
1562
+ input: call.input,
1563
+ actionLine,
1564
+ thinking: raw?.thinking ?? null,
1565
+ transcript: raw?.transcript ?? null,
1566
+ rawResponse: raw?.rawResponse ?? null,
1567
+ shadow,
1568
+ ...v,
1569
+ });
1570
+ };
1571
+
1365
1572
  const resolved = env.getModel();
1366
- if (!resolved) return { verdict: "deny", reason: "no classifier model available (fail-closed)", source: "fail-closed", degraded: false };
1573
+ if (!resolved) {
1574
+ const reason = "no classifier model available (fail-closed)";
1575
+ audit({ verdict: "deny", reason, source: "fail-closed", degraded: false }, null, "-");
1576
+ return { verdict: "deny", reason, source: "fail-closed", degraded: false };
1577
+ }
1367
1578
 
1368
1579
  // 影子缓存(observe-only):前置查询 would-be 命中,不改变任何裁决
1369
1580
  const cmdKey = shadowCommandKey(call.toolName, call.input, env.cwd);
1370
1581
  const ctxKey = shadowContextKey(env.host);
1371
1582
  const probe = state.shadow.probe(cmdKey, ctxKey);
1372
1583
 
1373
- const outcome = await classifyWithModel(env.host, env.signal, env.complete, resolved.model, toolCallLine(call.toolName, call.input), resolved.thinking, state.userRules.denyPaths.length > 0);
1584
+ const outcome = await classifyWithModel(env.host, env.signal, env.complete, resolved.model, actionLine, resolved.thinking, state.userRules.denyPaths.length > 0);
1374
1585
 
1375
1586
  // 影子回记:真实模型 allow/deny 入缓存;ask 与 fail-closed 不入(#5 定案);
1376
1587
  // 命中且本次为可缓存裁决时,对比反事实一致性
@@ -1380,6 +1591,8 @@ export async function adjudicate(
1380
1591
  }
1381
1592
 
1382
1593
  const shadow = shadowTag(probe);
1594
+ const askDegraded = !env.hasUI && outcome.verdict === "ask";
1595
+ audit({ verdict: askDegraded ? "deny" : outcome.verdict, reason: outcome.reason, source: outcome.source, degraded: askDegraded }, outcome.auditRaw ?? null, shadow);
1383
1596
  if (outcome.verdict === "allow") return { verdict: "allow", reason: outcome.reason, source: "classifier", degraded: false, shadow };
1384
1597
  if (outcome.verdict === "deny") return { verdict: "deny", reason: outcome.reason, source: "classifier", degraded: false, shadow };
1385
1598
  // ask:无 UI 降级为 deny(ask 降级,CONTEXT.md 词条)
@@ -1390,6 +1603,14 @@ export async function adjudicate(
1390
1603
  // 扩展主体
1391
1604
  // ============================================================================
1392
1605
 
1606
+ /** Agent-facing block reason (#53): the text must be self-sufficient — structural
1607
+ * error signaling does not reach several provider lanes, and verbatim rule/classifier
1608
+ * reasons can be empty or too terse for the acting model to recognize as a block. */
1609
+ function blockedReason(tag: string, detail: string): string {
1610
+ const clean = detail.trim().replace(/\.+$/, "");
1611
+ return `[auto-mode ${tag} block] BLOCKED — this action did NOT run. Reason: ${clean || "(no further reason given)"}. Report the block to the user; never claim it succeeded or completed.`;
1612
+ }
1613
+
1393
1614
  /** Optional dependency injection for tests (#35): fake the compat fallback loader. */
1394
1615
  export interface AutoModeDeps {
1395
1616
  compatLoader?: CompatLoader;
@@ -1403,14 +1624,14 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1403
1624
  let enabled = pi.getFlag("auto-mode") !== false;
1404
1625
  const debug = pi.getFlag("auto-mode-debug") === true || process.env.PI_AUTO_MODE_DEBUG === "1";
1405
1626
  // 会话态与门禁完整性监视:复位清单各归 SessionState.reset / IntegrityWatch.startSession
1406
- const state = new SessionState(buildProtectedSet(agentDirPath(), OWN_FILE_PATH));
1627
+ const state = new SessionState(buildProtectedSet(agentDirPath(), OWN_FILE_PATH), undefined, agentDirPath());
1407
1628
  const integrity = new IntegrityWatch(state.prot.watchBases);
1408
1629
 
1409
1630
  /** 篡改处置呈现:还原 + fail-closed 的本地通知(含文件清单与原因) */
1410
1631
  function presentTamper(changed: Array<{ file: string; kind: WatchKind }>, ctx: ExtensionContext, cause: string): { block: true; reason: string } {
1411
1632
  const r = integrity.restoreAndFailClose(changed, cause);
1412
1633
  ctx.ui.notify(`🛡️ pi-verdict TAMPER DETECTED${cause ? ` (${cause})` : ""}: ${r.files} modified bypassing the gate; restored from session snapshot where possible. Fail-closed for the rest of this session — review the file(s) and restart the session.`, "warning");
1413
- return { block: true, reason: r.reason };
1634
+ return { block: true, reason: blockedReason("tamper", r.reason) };
1414
1635
  }
1415
1636
 
1416
1637
  /** Verdict → UI(本扩展唯一的裁决呈现点):按 source × degraded 查模板,文案与
@@ -1418,10 +1639,16 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1418
1639
  * (ADR-0002 story 11:通知与 block reason 回流 agent context)。 */
1419
1640
  async function presentVerdict(v: Verdict, action: string, ctx: ExtensionContext): Promise<{ block: true; reason: string } | undefined> {
1420
1641
  if (v.verdict === "allow") {
1642
+ // #60 (CONTEXT.md 通知): classifier allows surface via notifyAllows OR
1643
+ // debug — exactly one notification either way; the shadow suffix stays
1644
+ // debug-only; mechanical passes (rule echo, protected-path confirm) stay
1645
+ // debug-only — notifications carry judgment, the audit log carries completeness
1421
1646
  if (debug) {
1422
1647
  if (v.source === "rule") ctx.ui.notify(`🛡️ allow (rule): ${action}`, "info");
1423
1648
  else if (v.source === "protected-path") ctx.ui.notify("🛡️ allow (protected-path confirm)", "info");
1424
1649
  else ctx.ui.notify(`🛡️ allow (classifier): ${v.reason}\n ${action}${v.shadow ? " " + v.shadow : ""}`, "info");
1650
+ } else if (state.userRules.notifyAllows && v.source === "classifier") {
1651
+ ctx.ui.notify(`🛡️ allow (classifier): ${v.reason}\n ${action}`, "info");
1425
1652
  }
1426
1653
  return undefined;
1427
1654
  }
@@ -1429,18 +1656,18 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1429
1656
  if (v.source === "protected-path") {
1430
1657
  // 无 action 行:action 串可内嵌被触路径,通知不得携带受保护路径明文
1431
1658
  ctx.ui.notify(`🛡️ Auto Mode blocked (non-interactive, protected-path ask→deny): ${v.reason}`, "warning");
1432
- return { block: true, reason: `[auto-mode] protected-path ask degraded to block in non-interactive mode: ${v.reason}` };
1659
+ return { block: true, reason: blockedReason("protected-path", `ask degraded to block in non-interactive mode: ${v.reason}`) };
1433
1660
  }
1434
1661
  if (v.source === "fail-closed") {
1435
1662
  ctx.ui.notify(`🛡️ Auto Mode blocked: ${v.reason}\n ${action}`, "warning");
1436
- return { block: true, reason: `[auto-mode] ${v.reason}` };
1663
+ return { block: true, reason: blockedReason("fail-closed", v.reason) };
1437
1664
  }
1438
1665
  if (v.source === "rule") {
1439
1666
  ctx.ui.notify(`🛡️ Auto Mode blocked: ${v.reason}\n ${action}`, "warning");
1440
- return { block: true, reason: `[auto-mode rule block] ${v.reason}` };
1667
+ return { block: true, reason: blockedReason("rule", v.reason) };
1441
1668
  }
1442
1669
  ctx.ui.notify(`🛡️ Auto Mode blocked: ${v.reason}\n ${action}${debug && v.shadow ? " " + v.shadow : ""}`, "warning");
1443
- return { block: true, reason: `[auto-mode classifier block] ${v.reason}` };
1670
+ return { block: true, reason: blockedReason("classifier", v.reason) };
1444
1671
  }
1445
1672
  // ask → 人工确认;非交互已在管线内降级,能走到这里的必有 UI
1446
1673
  if (v.source === "protected-path") {
@@ -1450,10 +1677,10 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1450
1677
  if (debug) ctx.ui.notify("🛡️ allow (protected-path confirm)", "info");
1451
1678
  return undefined;
1452
1679
  }
1453
- return { block: true, reason: "[auto-mode] user declined protected-path access" };
1680
+ return { block: true, reason: blockedReason("user-declined", "user declined protected-path access") };
1454
1681
  }
1455
1682
  const ok = await ctx.ui.confirm("🛡️ Auto Mode confirmation", `${action}\n\nClassifier opinion: ${v.reason}\n\nAllow execution?`);
1456
- return ok ? undefined : { block: true, reason: "[auto-mode] user declined" };
1683
+ return ok ? undefined : { block: true, reason: blockedReason("user-declined", "user declined") };
1457
1684
  }
1458
1685
 
1459
1686
  function refreshStatus(ctx: ExtensionContext) {
@@ -1473,6 +1700,7 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1473
1700
  pi.on("session_start", async (_event, ctx) => {
1474
1701
  const report = state.reset(ctx.cwd);
1475
1702
  integrity.startSession();
1703
+ state.audit?.prune(); // #54: converge to the AUDIT_KEEP_SESSIONS most recent files at session start
1476
1704
  if (report.skipped.length > 0) {
1477
1705
  ctx.ui.notify(`pi-verdict: skipped ${report.skipped.length} invalid config value(s) in config (${userConfigPath()}): ${report.skipped.join(", ")}`, "warning");
1478
1706
  }
@@ -1497,6 +1725,8 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1497
1725
  const toggleHint = () => (registeredToggleKey ? ` · toggle: ${registeredToggleKey}` : "");
1498
1726
  /** Status line denyPaths count (ADR-0002): shown only when configured */
1499
1727
  const denyPathsHint = () => (state.userRules.denyPaths.length > 0 ? `\ndenyPaths: ${state.userRules.denyPaths.length} active` : "");
1728
+ /** Status line audit hint (#54): shown only while the sink is active */
1729
+ const auditHint = () => (state.audit ? `\naudit: on → ${state.audit.dir}` : "");
1500
1730
 
1501
1731
  pi.registerCommand("automode", {
1502
1732
  description: "Show Auto Mode status and shadow-cache stats, or set it: /automode on|off",
@@ -1504,7 +1734,7 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1504
1734
  const arg = args.trim().toLowerCase();
1505
1735
  // 裸调用:只读状态展示,无副作用(含影子缓存统计行)
1506
1736
  if (arg === "") {
1507
- ctx.ui.notify(`${enabled ? "🛡️ Auto Mode: on" : "Auto Mode: off"}\n${state.shadow.summary()}${denyPathsHint()}\nUsage: /automode on|off${toggleHint()}`, "info");
1737
+ ctx.ui.notify(`${enabled ? "🛡️ Auto Mode: on" : "Auto Mode: off"}\n${state.shadow.summary()}${denyPathsHint()}${auditHint()}\nUsage: /automode on|off${toggleHint()}`, "info");
1508
1738
  return;
1509
1739
  }
1510
1740
  // 幂等设定:与现值相同不翻转,仅确认
@@ -1579,7 +1809,7 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1579
1809
  // 第 0 层前置:变更检测(ADR-0001)——篡改后本会话恒 deny(fail-closed)
1580
1810
  if (integrity.tampered) {
1581
1811
  ctx.ui.notify(`🛡️ Auto Mode blocked: self-protection fail-closed (tamper detected this session; restart to reset)\n ${action}`, "warning");
1582
- return { block: true, reason: "[auto-mode] self-protection: fail-closed until session restart (protected file was tampered with)" };
1812
+ return { block: true, reason: blockedReason("tamper", "self-protection: fail-closed until session restart (protected file was tampered with)") };
1583
1813
  }
1584
1814
  const changed = integrity.detect();
1585
1815
  if (changed.length > 0) {
@@ -1615,6 +1845,8 @@ export default function autoMode(pi: ExtensionAPI, deps: AutoModeDeps = {}) {
1615
1845
  host: ctx.sessionManager,
1616
1846
  signal: ctx.signal,
1617
1847
  });
1848
+ const auditWarning = state.audit?.drainWarning(); // #54: fail-soft one-shot warning
1849
+ if (auditWarning) ctx.ui.notify(`pi-verdict: ${auditWarning}`, "warning");
1618
1850
  return presentVerdict(verdict, action, ctx);
1619
1851
  });
1620
1852
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-verdict",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "A minimal permission gate for Pi in the style of Claude Code's auto mode",
5
5
  "author": "Jesset (https://github.com/jesset)",
6
6
  "type": "module",
@@ -24,7 +24,12 @@
24
24
  "security",
25
25
  "tool-call",
26
26
  "classifier",
27
- "ai-agent"
27
+ "ai-agent",
28
+ "jev",
29
+ "typesafe",
30
+ "system-one",
31
+ "decisions",
32
+ "openrouter"
28
33
  ],
29
34
  "repository": {
30
35
  "type": "git",