pi-permission-classifier 0.5.2 → 0.6.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
@@ -1,33 +1,43 @@
1
1
  # pi-permission-classifier
2
2
 
3
- An auto-classifier for the [pi coding agent](https://pi.dev/)'s
4
- permission system. It registers an Authorizer chain link named `classifier`
5
- with [`@gotgenes/pi-permission-system`](https://github.com/gotgenes/pi-packages/tree/main/packages/pi-permission-system):
6
- a light model reviews each permission
7
- `ask` and returns allow, deny, or defer, so clearly benign requests are
8
- approved automatically, clearly bad ones are rejected with a short teaching
9
- reason, and only genuinely uncertain ones reach you.
3
+ A permission judge for the [pi coding agent](https://pi.dev/)'s
4
+ permission system. It registers an Authorizer chain link named
5
+ `permission-judge` with [`@gotgenes/pi-permission-system`](https://github.com/gotgenes/pi-packages/tree/main/packages/pi-permission-system):
6
+ a model reviews each permission `ask` and returns allow, deny, or defer, so
7
+ clearly benign requests are approved automatically, clearly bad ones are
8
+ rejected with a short teaching reason, and only genuinely uncertain ones
9
+ reach you.
10
10
 
11
11
  Think of it as a Claude-Code-style auto-approve mode, without giving up the
12
- permission gate: your deterministic policy still runs first, and the
13
- classifier only sees the asks that policy would have sent to you anyway.
12
+ permission gate: your deterministic policy still runs first, and the judge
13
+ only sees the asks that policy would have sent to you anyway.
14
+
15
+ Two judges can review an ask, and the `mode` setting picks who does:
16
+
17
+ - `llm` (the default): the LLM judge. A chat model reads the ask and
18
+ reasons about it.
19
+ - `quick`: the quick judge. A System One decision model, such as TypeSafe's
20
+ Jev, answers three fixed questions about the ask in about 200 ms, and
21
+ the answer probabilities map to a verdict.
22
+ - `quick-then-llm`: the quick judge settles the clear cases, and the LLM
23
+ judge reviews everything the quick judge does not allow.
24
+
25
+ You choose the mode, both models and the last judge's `verdicts` and
26
+ `onError` with `/permission-model`, inside pi.
14
27
 
15
28
  Fail-safe by construction: a missing config, an unresolved model, an auth
16
- failure, a timeout, an unparseable reply, or any internal error resolves to
17
- defer. More prompting, never less.
29
+ failure, a timeout, an unparseable reply, or any internal error never
30
+ allows. A failure defers unless the judge's `onError` is `deny`, which turns
31
+ it into a deny that tells the agent to retry later or take another route.
18
32
 
19
33
  ## Requirements
20
34
 
21
35
  - pi with `@gotgenes/pi-permission-system` 27.0.0 or newer installed and
22
- active in the same session (the classifier resolves the permission service
36
+ active in the same session (the judge resolves the permission service
23
37
  through the session-keyed locator introduced in 27.0.0; with an older
24
38
  version it warns once and registers nothing)
25
- - pi 0.84.3 or newer for the searchable `/permission-model` picker, declared
26
- as the `@earendil-works/pi-coding-agent >=0.84.3` peer dependency: the
27
- selector component that picker mounts changed constructor shape in 0.84.3,
28
- so on an older pi the command errors before the picker mounts - nothing is
29
- written, the judge keeps reviewing, and the typed and `session` forms still
30
- work. Everything else needs only the permission-system floor above.
39
+ - pi 1.0.0 or newer, declared as the
40
+ `@earendil-works/pi-coding-agent >=1.0.0` peer dependency
31
41
  - Node 22 or newer
32
42
  - No build step: pi loads `src/index.ts` directly
33
43
 
@@ -56,9 +66,9 @@ and installing it grants it no authority until you name it in the chain.
56
66
 
57
67
  Order matters: pi runs `session_start` handlers in package order, and
58
68
  `pi-permission-system` emits its `permissions:ready` event from its own
59
- `session_start`. With the classifier listed first, the link and the
69
+ `session_start`. With the judge listed first, the link and the
60
70
  `zz-permission-classifier` footer entry are in place at startup. Listed
61
- after it, the classifier misses that first ready event and registers
71
+ after it, the judge misses that first ready event and registers
62
72
  at the next one, so the link and the footer entry appear only after the
63
73
  first agent turn (asks are still reviewed, since they happen inside
64
74
  agent turns).
@@ -70,35 +80,42 @@ and installing it grants it no authority until you name it in the chain.
70
80
  3. Activate the chain link. In
71
81
  `~/.pi/agent/extensions/pi-permission-system/config.json`, add:
72
82
 
73
- "authorizerChain": ["classifier"]
83
+ "authorizerChain": ["permission-judge"]
74
84
 
75
85
  Only links named here are consulted; config order fixes the chain order.
86
+ This is the only edit to another package's config you ever make: the
87
+ mode and the models are chosen inside pi (step 5).
76
88
 
77
- 4. Create the classifier config. Without it the link registers nothing
89
+ 4. Create the judge config. Without it the link registers nothing
78
90
  (a safe no-op). The defaults are a good starting point:
79
91
 
80
92
  mkdir -p ~/.pi/agent/extensions/pi-permission-classifier
81
93
  echo '{}' > ~/.pi/agent/extensions/pi-permission-classifier/config.json
82
94
 
83
- `{}` means: judge with the session's active model, judge every surface
95
+ `{}` means: mode `llm`, judged by the session's chat model, every surface
84
96
  except `path` and `external_directory`, 5000 ms timeout, built-in
85
- rubric. See `config/config.example.json`
86
- for a version with a dedicated judge model, or pick one later from
87
- inside pi with `/permission-model` (see "Choosing the judge model").
97
+ rubric. See `config/config.example.json` for a version that names a
98
+ dedicated model and a quick judge, each with its own call budget.
99
+
100
+ 5. Choose the judges. Start a new pi session and run `/permission-model`:
101
+ in the menu, set who judges, the model for each judge that mode runs,
102
+ and the last judge's verdicts and on error (see "Choosing the judges").
103
+ Each pick is saved to the config file from step 4 and applies to the
104
+ next ask.
88
105
 
89
- 5. Try it. Start a new pi session and trigger something your policy sends
90
- to `ask` (for example a bash command not on your allowlist). A benign
91
- command should now be approved automatically; a command matching the
92
- never-allow list should be rejected with a reason; anything uncertain
93
- still prompts you.
106
+ 6. Try it. Trigger something your policy sends to `ask` (for example a bash
107
+ command not on your allowlist). A benign command should now be approved
108
+ automatically; a command matching the never-allow list should be
109
+ rejected with a reason; anything uncertain still prompts you.
94
110
 
95
- 6. Watch the decisions. Every reviewed ask writes one `classifier.decision`
96
- entry to the permission review log:
111
+ 7. Watch the decisions. Every judge that reviews an ask writes one
112
+ `classifier.decision` entry to the permission review log:
97
113
 
98
114
  ~/.pi/agent/extensions/pi-permission-system/logs/pi-permission-system-permission-review.jsonl
99
115
 
100
- Each entry records request id, surface, value, model id, latency,
101
- verdict, defer reason, and three context fields: `contextIncluded`
116
+ Each entry records the judge (`quick` or `llm`), request id,
117
+ surface, value, model id, latency, verdict, defer reason, and three
118
+ context fields: `contextIncluded`
102
119
  (true only when a full command was rendered to the judge),
103
120
  `contextBytes` (its UTF-8 byte length, null when absent), and
104
121
  `contextHash` (first 12 hex chars of its sha256, null when absent).
@@ -107,8 +124,26 @@ and installing it grants it no authority until you name it in the chain.
107
124
  Enable `debugLog` in the permission system config to also capture raw
108
125
  model replies and short-circuit traces.
109
126
 
110
- To disable, remove `classifier` from `authorizerChain` (or remove the
111
- package entry). The previous prompting behavior returns immediately.
127
+ To disable, remove `permission-judge` from `authorizerChain`, and the old
128
+ name `classifier` too if it is listed (or remove the package entry). The
129
+ previous prompting behavior returns immediately.
130
+
131
+ ### The old chain name `classifier`
132
+
133
+ Earlier releases registered the link as `classifier`. For this release the
134
+ same judge is also registered under that name, so an existing
135
+ `"authorizerChain": ["classifier"]` keeps working. The first ask that
136
+ arrives under `classifier` in a session logs one warning that the name is
137
+ deprecated and that `authorizerChain` should name `permission-judge`. A
138
+ later release removes the old name; a chain that still names it then gets
139
+ only pi-permission-system's generic warning for an unregistered link.
140
+
141
+ With both names in `authorizerChain`, an ask is still judged once: the
142
+ second name consulted for a request the judge already reviewed under the
143
+ other name defers at once, with no model call and no decision entry. The
144
+ judge remembers the last 8 request ids per session for this. The first such
145
+ duplicate in a session logs one warning that both names are listed and
146
+ `classifier` should be removed.
112
147
 
113
148
  ## Configuration
114
149
 
@@ -117,18 +152,69 @@ Config files (project overrides global, shallow merge):
117
152
  - Global: `~/.pi/agent/extensions/pi-permission-classifier/config.json`
118
153
  - Project: `<cwd>/.pi/extensions/pi-permission-classifier/config.json`
119
154
 
155
+ The merge is shallow: a field the project file sets replaces the global
156
+ field whole. A project `quickJudge` object therefore replaces the global
157
+ `quickJudge` object completely. A project file that sets only
158
+ `"quickJudge": { "allowThreshold": 0.8 }` drops the global quick judge model,
159
+ and every quick judge ask is then `model-unresolved`; repeat `provider` and
160
+ `model` in the project object.
161
+
120
162
  | Field | Default | Meaning |
121
163
  | --- | --- | --- |
122
- | `provider` | unset | Judge model provider. Set together with `model`; with neither set, the session's active model judges. |
123
- | `model` | unset | Judge model id, resolved from the session model registry. Set together with `provider`. |
124
- | `instructions` | built-in rubric | System prompt for the judge. Replaces the default rubric verbatim when set. |
164
+ | `mode` | `"llm"` | Who judges: `"llm"`, `"quick"` or `"quick-then-llm"`. See "Modes". |
165
+ | `provider` | unset | LLM judge model provider. Set together with `model`; with neither set, the session's chat model judges. |
166
+ | `model` | unset | LLM judge model id, resolved from the session model registry. Set together with `provider`. |
167
+ | `instructions` | built-in rubric | System prompt for the LLM judge. Replaces the default rubric verbatim when set. |
125
168
  | `surfaces` | ignored | Accepted so older config files still parse, but ignored: every surface outside the `path` and `external_directory` families is judged. A file that sets it logs one warning at load: `This field is ignored: every surface outside the path and external_directory families is judged.` |
126
- | `timeoutMs` | `5000` | Per-review model call budget in milliseconds (positive integer). |
127
- | `contextBudgetBytes` | `8192` | Cap on the extracted full-command context in UTF-8 bytes (positive integer). An ask whose context exceeds the budget defers before any model call; context is never truncated to fit. |
169
+ | `timeoutMs` | `5000` | The LLM judge's own model call budget in milliseconds (positive integer). The quick judge has a separate `quickJudge.timeoutMs`. |
170
+ | `contextBudgetBytes` | `8192` | Cap on the extracted full-command context in UTF-8 bytes (positive integer), for both judges. An over-budget ask is never sent to a model and counts as uncertain: it defers, or denies under `verdicts: "allow-deny"`. Context is never truncated to fit. |
171
+ | `verdicts` | `"allow-deny-defer"` | Which verdicts the LLM judge may return. See "Verdicts and onError". |
172
+ | `onError` | `"defer"` | What the LLM judge returns on a failure: `"defer"` or `"deny"`. Never allow. |
173
+ | `quickJudge` | unset | The quick judge settings. See "quickJudge settings". |
174
+
175
+ The top-level fields are the LLM judge's settings, so a config file written
176
+ before the quick judge existed still parses and behaves as before: mode
177
+ `llm`, no quick judge.
178
+
179
+ An unreleased build used the fields `systemOne` and `classifierEnabled`. A
180
+ file that still sets one loads, ignores it, and logs one config issue per
181
+ field: `systemOne` was renamed to `quickJudge`, and `classifierEnabled` is
182
+ replaced by `mode`.
128
183
 
129
184
  A malformed or invalid config file means the link registers nothing and pi
130
185
  logs a warning - the gate falls back to normal prompting.
131
186
 
187
+ ### Verdicts and onError
188
+
189
+ Each judge enforces its own `verdicts` and `onError` in code, after the
190
+ model answered:
191
+
192
+ | `verdicts` | Model allow | Model deny | Uncertain (model defer, answer below thresholds, over budget) |
193
+ | --- | --- | --- | --- |
194
+ | `allow-deny-defer` | allow | deny | defer |
195
+ | `allow-defer` | allow | defer | defer |
196
+ | `allow-deny` | allow | deny | deny: "The permission reviewer denied this action because it could not be confirmed safe." |
197
+
198
+ `onError` applies to every failure: `timeout`, `call-failed` (an error
199
+ reply or a thrown call), `breaker-open`, `model-unresolved`, `auth-failed`,
200
+ `guidance-load-failed` and `internal-error`. `"defer"` passes the ask on.
201
+ `"deny"` denies with a fixed reason that names the failure and ends with
202
+ "Retry later or take another route." It never says the action is unsafe,
203
+ because nothing judged it. No failure ever allows.
204
+
205
+ A judge that runs alone in an unattended session can use `allow-deny` with
206
+ `onError: "deny"`, so it never waits for a human. In mode `quick-then-llm`
207
+ the quick judge always runs with `allow-defer` and `onError: "defer"` (see
208
+ "Modes").
209
+
210
+ `/permission-model` sets both fields for the last judge in line, the judge
211
+ whose answer ends the ask: the LLM judge (top-level fields) in modes `llm`
212
+ and `quick-then-llm`, the quick judge (`quickJudge` fields) in mode
213
+ `quick`. See "The /permission-model command".
214
+
215
+ When `allow-defer` withholds a model deny, the decision entry records
216
+ verdict `defer` with defer reason `deny-capped`.
217
+
132
218
  ### Example: a local judge model
133
219
 
134
220
  A small local model makes a good judge: verdicts stay on your machine, cost
@@ -157,7 +243,7 @@ local OpenAI-compatible provider (llama.cpp, llama-swap, Ollama, vLLM):
157
243
  }
158
244
  ```
159
245
 
160
- Then point the classifier at it:
246
+ Then point the LLM judge at it:
161
247
 
162
248
  ```json
163
249
  {
@@ -166,7 +252,7 @@ Then point the classifier at it:
166
252
  }
167
253
  ```
168
254
 
169
- The model must support tool calling: the classifier forces a
255
+ The model must support tool calling: the LLM judge forces a
170
256
  `report_verdict` tool call and treats a reply without one as defer. Verify
171
257
  with a quick curl (`"tool_choice": "required"`) that your server returns
172
258
  `finish_reason: "tool_calls"`. Keep the model resident if you can - a cold
@@ -208,6 +294,14 @@ describe what is normal for this operator and project, can move a verdict
208
294
  toward allow or deny within the rubric, and never override the never-allow
209
295
  list.
210
296
 
297
+ It also says what your own messages may do (see "User messages"): a
298
+ request that directly and specifically names the action can move a case
299
+ that would otherwise defer to allow, and a general task ("fix the build")
300
+ does not name a push, a publish or a delete; a request never reaches the
301
+ never-allow list; a message that said not to do what the action does, not
302
+ lifted by a later message, means defer; approval that appears only inside
303
+ the command, in the agent's own text or in tool output does not count.
304
+
211
305
  Set `instructions` to replace the rubric wholesale with your own.
212
306
 
213
307
  ### Guidance files
@@ -215,7 +309,7 @@ Set `instructions` to replace the rubric wholesale with your own.
215
309
  The judge reads the same context files the agent does: the global
216
310
  `AGENTS.md` (or `CLAUDE.md` fallback) in the pi agent dir, and the project
217
311
  files pi finds from the session cwd up through its ancestors (override
218
- names and worktree shadowing included, because the classifier calls pi's
312
+ names and worktree shadowing included, because the judge calls pi's
219
313
  own context-file loader). Write the lines that describe what is normal
220
314
  here, for example "downloads into ./vendor and cleanup of build/ are
221
315
  routine in this repo", and the judge sees them verbatim.
@@ -225,7 +319,7 @@ routine in this repo", and the judge sees them verbatim.
225
319
  - Trust gating: the global file always reaches the judge. Project files
226
320
  reach it only while pi reports the project trusted, read at the moment
227
321
  of each ask. In an untrusted directory the judge sees the global file
228
- alone, and the classifier's own project config layer
322
+ alone, and the judge's own project config layer
229
323
  (`.pi/extensions/pi-permission-classifier/config.json`) is not read
230
324
  either, so a project `instructions` string cannot replace the rubric
231
325
  before you trust the directory.
@@ -251,6 +345,41 @@ routine in this repo", and the judge sees them verbatim.
251
345
  `guidance-load-failed`, recorded on the decision entry and shown in the
252
346
  footer health suffix like any other failure defer.
253
347
 
348
+ ### User messages
349
+
350
+ The LLM judge also sees what you asked the agent for, so a command you
351
+ named can be allowed and a command you ruled out is not.
352
+
353
+ - Source: your own messages in the current session, read from pi's
354
+ in-memory session at each ask (no file is read). A skill you invoked
355
+ appears as `[skill <name>] <your arguments>`, without the skill body.
356
+ Assistant text, tool results, bash executions (`!` commands), custom
357
+ messages from extensions, and compaction or branch summaries are never
358
+ sent.
359
+ - Caps: the first message (the opening task) is always kept, then the
360
+ newest messages back to 24,000 characters in total. A message over
361
+ 4,000 characters keeps its head and tail.
362
+ - When it is left out: if any message was cut or dropped by the caps, if a
363
+ message of yours is still queued, or if the ask was forwarded from a
364
+ subagent, the prompt has no user block and the judge decides on the
365
+ rubric alone. None of these defers on its own.
366
+ - Rendering: one data block after the guidance and before the value,
367
+ labelled `Messages the user wrote to the agent in this session, oldest
368
+ first`, with each message in its own `<user-message>` block. A delimiter
369
+ tag inside your text is escaped, so it cannot close the block. The block
370
+ carries no rules; the default rubric does, so a custom `instructions`
371
+ rubric gets the block without them.
372
+ - Logging: the user text is never logged.
373
+ - Hosted judge models receive this text with every ask.
374
+
375
+ The quick judge does not send your messages yet. With them, it would also
376
+ ask whether you requested the action and whether you forbade it, and a
377
+ request could clear only push or publish, force push, discarding
378
+ uncommitted work and other destructive actions; credential access, secret
379
+ exfiltration, remote code execution and disarming guards would never be
380
+ cleared. The first eval run did not meet the bar for shipping it (see
381
+ `eval/RESULTS.md`).
382
+
254
383
  ### Config suggestions
255
384
 
256
385
  The judge decides only what the pi-permission-system policy sends to
@@ -280,7 +409,7 @@ useful and cheap:
280
409
 
281
410
  ### Which surfaces are judged
282
411
 
283
- The classifier judges every surface except the `path` and
412
+ The judges review every surface except the `path` and
284
413
  `external_directory` families, whatever the surface name, including
285
414
  surfaces added by other extensions. A family is the bare name plus its
286
415
  `<name>_*` members, today `path`, `path_read`, `path_write`,
@@ -288,86 +417,309 @@ surfaces added by other extensions. A family is the bare name plus its
288
417
  `external_directory_write`. There is no surface list to maintain. Your
289
418
  cross-cutting `path` and `external_directory` rules still apply, and the
290
419
  engine downgrades any link allow on those families to defer, so the
291
- classifier can never approve access outside the working directory or to a
420
+ judge can never approve access outside the working directory or to a
292
421
  path your policy denies. To keep a tool out of the judge's hands, route it
293
422
  to `allow` or `deny` in the permission system policy instead of `ask`.
294
423
 
295
- ## Choosing the judge model
424
+ ## Modes
425
+
426
+ The `mode` field picks who judges each ask. It is read on every ask, so a
427
+ change from `/permission-model` or a config reload applies to the next ask,
428
+ with no new session.
429
+
430
+ - `llm` (default): only the LLM judge runs, under the top-level `verdicts`
431
+ and `onError`. The quick judge is never called.
432
+ - `quick`: only the quick judge runs, under `quickJudge.verdicts` and
433
+ `quickJudge.onError`. The LLM judge is never called.
434
+ - `quick-then-llm`: the quick judge runs first, always with
435
+ `verdicts: "allow-defer"` and `onError: "defer"`, whatever `quickJudge`
436
+ says. A quick allow ends the ask. Anything else - a deny, an uncertain
437
+ answer, a failure, an unresolved quick model - goes to the LLM judge,
438
+ which decides under the top-level `verdicts` and `onError`. When
439
+ `quickJudge` asks for other verdicts or onError, pi logs one warning per
440
+ session that names the override.
441
+
442
+ Each judge has its own circuit breaker (3 failures open it for 60 seconds)
443
+ and its own health record, so a quick judge outage never opens the LLM
444
+ judge's breaker or changes its footer part, and the reverse.
445
+
446
+ ## The quick judge
447
+
448
+ The quick judge sends each ask to a classifier model through pi's model
449
+ registry (`findOfType("classifier", ...)` and `classify`) and asks three
450
+ fixed questions:
451
+
452
+ - `reversible` (yes/no): can every effect be undone with ordinary tools?
453
+ Output that can be made again (caches, build and test output, logs,
454
+ files the action creates) counts as undoable. Deleting files git does not
455
+ hold, discarding uncommitted work, rewriting history, pushing or
456
+ publishing, and sending a message do not.
457
+ - `contained` (yes/no): do the effects stay inside the project: the
458
+ project tree (the cwd), /tmp, and the usual caches, build output, temp
459
+ files and display of the project's own build, run and test tools?
460
+ Reading a secret (keys, tokens, credentials, .env files, the
461
+ environment), sending data off this machine, and changing files
462
+ elsewhere are not contained.
463
+ - `hazard` (one of nine): none, credential access, secret exfiltration
464
+ (sending secrets or credentials off this machine), push or publish
465
+ (pushing, publishing or uploading project work), remote code execution,
466
+ force push, discard uncommitted work, disarm guards, other destructive.
467
+
468
+ The answer allows when P(reversible), P(contained) and P(hazard = none)
469
+ each reach their allow threshold: 0.8 each, built in, or `allowThreshold`
470
+ for all three when you set it. It denies when one hazard other than none reaches
471
+ `denyThreshold`, with a reason that names the hazard ("it looks like a force
472
+ push"). A deny wins when both match. Anything else is uncertain and maps
473
+ through `verdicts`. The question text is fixed in code.
474
+
475
+ The model sees the gated value, the surface, the tool name, the cwd, the full
476
+ command when it fits `contextBudgetBytes`, and the selected guidance files
477
+ when `guidance` is on. It never sees your messages (see "User messages"),
478
+ tool results, file contents, evidence, annotations or the tool-input
479
+ preview. The `path` and `external_directory`
480
+ families and an undeterminable surface defer with no model call, whatever
481
+ `verdicts` and `onError` say.
482
+
483
+ ### quickJudge settings
484
+
485
+ | Field | Default | Meaning |
486
+ | --- | --- | --- |
487
+ | `provider` | unset | Classifier model provider, for example `typesafe`. Set together with `model`. |
488
+ | `model` | unset | Classifier model id, for example `jev-latest`. An unset or unknown pair never falls back to the session model: every ask takes `onError` with reason `model-unresolved`. |
489
+ | `verdicts` | `"allow-defer"` | See "Verdicts and onError". In mode `quick-then-llm` it is always `allow-defer`. |
490
+ | `onError` | `"defer"` | See "Verdicts and onError". In mode `quick-then-llm` it is always `defer`. |
491
+ | `allowThreshold` | unset | Probability (0 to 1) that reversible, contained and hazard none must each reach to allow. Unset uses the built-in threshold per question (0.8 each), chosen by the eval (see "Development"). |
492
+ | `denyThreshold` | `0.9` | Probability (0 to 1) one hazard must reach to deny. |
493
+ | `guidance` | `true` | Send the selected guidance files, chosen by the same rules as for the LLM judge. |
494
+ | `timeoutMs` | `5000` | The quick judge's own model call budget in milliseconds, enforced with an abort signal. The top-level `timeoutMs` belongs to the LLM judge alone. |
495
+
496
+ Each judge gets its own call budget: the top-level `timeoutMs` is the LLM
497
+ judge's, `quickJudge.timeoutMs` is the quick judge's, and setting one does
498
+ not move the other.
499
+
500
+ A config for the quick judge in front of the LLM judge:
501
+
502
+ {
503
+ "mode": "quick-then-llm",
504
+ "timeoutMs": 15000,
505
+ "quickJudge": {
506
+ "provider": "typesafe",
507
+ "model": "jev-latest",
508
+ "timeoutMs": 5000
509
+ }
510
+ }
296
511
 
297
- The judge is the model that reviews each ask. With no `provider` and
298
- `model` in the config it is the session's active model. Three ways to
299
- change it:
512
+ The quick judge alone, never waiting for a human:
513
+
514
+ {
515
+ "mode": "quick",
516
+ "quickJudge": {
517
+ "provider": "typesafe",
518
+ "model": "jev-latest",
519
+ "verdicts": "allow-deny",
520
+ "onError": "deny"
521
+ }
522
+ }
523
+
524
+ ### The TypeSafe API key
525
+
526
+ Jev needs `TYPESAFE_API_KEY` in the pi process. Any of these works:
527
+
528
+ - [pi-dotenv](https://github.com/TacoTakumi/pi-dotenv): put
529
+ `TYPESAFE_API_KEY=...` in `~/.pi/agent/.env` and list pi-dotenv in
530
+ `packages`.
531
+ - Export it in the shell that starts pi: `export TYPESAFE_API_KEY=...`.
532
+ - Store it in pi's `~/.pi/agent/auth.json` under the provider id:
533
+ `"typesafe": { "type": "api_key", "key": "..." }`. A stored key wins over
534
+ the environment variable.
535
+
536
+ Without a key, Jev resolves but every call fails, and asks take `onError`
537
+ with reason `call-failed`.
538
+
539
+ ### Data sent to a hosted provider
540
+
541
+ With a hosted classifier such as `typesafe/jev-latest`, every ask the quick
542
+ judge reviews leaves your machine: the gated value, the full command (within
543
+ `contextBudgetBytes`), the surface, the tool name, the cwd path, and, with
544
+ `guidance` on, the full text of the selected guidance files. Set
545
+ `"guidance": false` to keep your guidance files local. Use a local model if
546
+ none of this may leave the machine.
547
+
548
+ ### Local classifier models
549
+
550
+ pi 1.0.3 cannot declare a classifier model in `models.json`: it reads every
551
+ `models.json` entry as a chat model and ignores `"type": "classifier"`, so
552
+ `findOfType` never finds it and the quick judge defers with
553
+ `model-unresolved`. A local System One model, such as Kev 4B behind
554
+ llama-swap's `/upstream/<model>/v1/systemone`, works only when an extension
555
+ registers it with `pi.registerProvider`, a `classifiers` entry for the
556
+ `typesafe-system-one` api, and `"type": "classifier"` on the model. This
557
+ package does not do that yet.
558
+
559
+ ### Decision entries
560
+
561
+ Each judge that reviews an ask writes one `classifier.decision` entry with
562
+ `judge` set to `quick` or `llm`. In mode `quick-then-llm` an ask the quick
563
+ judge does not allow writes two entries, `quick` then `llm`. Quick entries
564
+ carry, when the model answered, `answers`: the reversible and contained
565
+ probabilities, and `hazard` as a list of `[option, probability]` pairs for all
566
+ nine options, highest first. The pairs keep the option names out of the JSON
567
+ keys, because the permission log masks any value whose key looks like a
568
+ credential name, which would hide `credential_access`. A below-threshold answer
569
+ records defer reason `model-verdict`. A request skipped as a duplicate (see
570
+ "The old chain name `classifier`") writes no entry.
571
+
572
+ ## Choosing the judges
573
+
574
+ Everything is chosen with `/permission-model` and saved to this package's
575
+ global config file. You never edit another package's config for it, and
576
+ choosing a judge never changes pi's session model or your default model:
577
+ `/model` and `/permission-model` are independent.
300
578
 
301
579
  ### The /permission-model command
302
580
 
303
- - `/permission-model` with no argument opens pi's own searchable model
304
- picker (the same list as `/model`, scoped models included) with the
305
- current judge preselected. Pick a model to make it the judge; cancel to
306
- change nothing. Outside the TUI (rpc, json, print modes) the command
307
- prints the current judge and the usage line instead. The searchable picker
308
- needs pi 0.84.3 or newer (`@earendil-works/pi-coding-agent >=0.84.3`): its
309
- constructor changed in 0.84.3, so on an older pi the command errors before
310
- the picker mounts, writes nothing, and leaves the judge on its previous
311
- rule - loud and safe, not a silent fallback. If your pi version does not
312
- expose the registry runtime the picker needs, the command warns that the
313
- picker degraded and offers a plain list of `provider/id` labels.
314
- - `/permission-model <provider>/<id>` sets the judge by reference. The pair
315
- must be in pi's model registry: an unknown pair is rejected and nothing
316
- changes. A known model without configured auth is accepted with a
317
- warning, and asks defer until the auth exists. Tab completion offers the
318
- `provider/id` labels of the available models plus `session`.
319
- - `/permission-model session` removes `provider` and `model` so the
320
- session's active model judges again.
581
+ - `/permission-model` with no argument opens the "Permission judge" menu.
582
+ Each entry shows its current value:
583
+
584
+ ```
585
+ Who judges quick-then-llm
586
+ Quick judge model typesafe/jev-latest
587
+ LLM judge model Same as the chat model (follows /model)
588
+ Quick judge model-call timeout 5 seconds
589
+ LLM judge model-call timeout 5 seconds
590
+ LLM judge verdicts allow-deny-defer
591
+ LLM judge on error defer
592
+ ```
593
+
594
+ - Who judges lists the modes `quick-then-llm`, `llm`, `quick`, each with
595
+ a one-line description.
596
+ - Quick judge model lists the available classifier models.
597
+ - LLM judge model lists `Same as the chat model (follows /model)` and the
598
+ available chat models.
599
+ - Each model-call timeout entry lists 5, 10, 15, 25, 30 and 60 seconds,
600
+ the saved budget when it is not one of those, and `0`, which clears the
601
+ saved budget so the judge uses its built-in 5 seconds. The menu shows
602
+ a timeout entry only for a judge the mode runs: the quick judge's in
603
+ `quick` and `quick-then-llm`, the LLM judge's in `llm` and
604
+ `quick-then-llm`. A budget that is not whole seconds shows its
605
+ decimals, for example `2.5`.
606
+ - The verdicts entry lists `allow-deny-defer`, `allow-defer` and
607
+ `allow-deny`; the on error entry lists `defer` and `deny`. Both belong
608
+ to the last judge in line, so they read "LLM judge" in `llm` and
609
+ `quick-then-llm` and "Quick judge" in `quick`.
610
+
611
+ A model entry whose judge the mode does not run is marked, for example
612
+ `(not used in llm)`, and can still be set. Each list marks the current
613
+ value `(current)` and preselects it. Picking the current timeout row
614
+ changes nothing. A configured model that is not available shows first
615
+ as `(current, not available)`; picking it changes nothing and says so. A pick is saved at once, exactly as the matching
616
+ typed form saves it, and the menu opens again with the new value. If the
617
+ save lands but the merged config no longer validates, the menu closes
618
+ after the error instead. Picking `quick` or `quick-then-llm` while the
619
+ global config file has no quick model opens the quick model list next
620
+ and saves the mode and the model together. Press escape in a list to go
621
+ back to the menu without a change, and escape on the menu to close it.
622
+ - Outside the TUI (rpc, json, print modes), `/permission-model` with no
623
+ argument prints the mode, the model of each judge the mode runs, the last
624
+ judge's verdicts and onError, and the usage line, and writes nothing.
625
+ - `/permission-model mode <llm|quick|quick-then-llm>` sets the mode. Mode
626
+ `quick` or `quick-then-llm` needs a quick judge model in the global config
627
+ file, where the mode is saved: without one it writes nothing and says to
628
+ set one first. A quick model that only the launch flag or a project config
629
+ sets does not count, because the saved mode would outlive the flag or apply
630
+ in other projects. The menu asks for the quick model in that case.
631
+ - `/permission-model llm <provider>/<id>` sets the LLM judge. The pair must
632
+ be a chat model in pi's model registry: an unknown pair or a classifier
633
+ model is rejected and nothing changes. A known model without configured
634
+ auth is accepted with a warning, and asks defer until the auth exists.
635
+ - `/permission-model llm chat-model` removes `provider` and `model`, so the
636
+ LLM judge follows the session's chat model (it changes with `/model`).
637
+ - `/permission-model quick <provider>/<id>` sets the quick judge. The pair
638
+ must be a classifier model: an unknown pair or a chat model is rejected
639
+ and nothing changes.
640
+ - `/permission-model verdicts <allow-deny-defer|allow-defer|allow-deny>`
641
+ and `/permission-model on-error <defer|deny>` set that field for the last
642
+ judge in line of the mode in effect (a `mode:` launch flag value or a
643
+ project config mode counts). The confirmation names the judge. An
644
+ unknown value is rejected and nothing changes.
645
+ - `/permission-model llm timeout <seconds>` and
646
+ `/permission-model quick timeout <seconds>` set one judge's model-call
647
+ budget in whole seconds, saved as milliseconds: the LLM judge's in
648
+ `timeoutMs`, the quick judge's in `quickJudge.timeoutMs`. `0` clears that
649
+ judge's key, so it uses its built-in 5 seconds. A sign, a decimal point,
650
+ an exponent, a unit, an empty value or anything above 3600 is rejected
651
+ and nothing changes.
652
+ - Setting a model or a timeout never changes the mode.
653
+ - Tab completion offers `mode`, `llm`, `quick`, `verdicts` and `on-error`,
654
+ then the mode values, `chat-model`, `timeout` and the chat model labels,
655
+ `timeout` and the classifier model labels, or the verdicts and onError
656
+ values. After `llm timeout ` or `quick timeout ` it offers the preset
657
+ budgets and `0`.
321
658
 
322
659
  A choice applies to the next reviewed ask immediately and is saved to the
323
660
  global config file
324
- (`~/.pi/agent/extensions/pi-permission-classifier/config.json`): only
325
- `provider` and `model` are rewritten, every other field is preserved, and
326
- the write goes through a temporary file that is fsynced and then renamed, so
327
- neither a process crash nor an OS crash leaves a truncated config. The command
328
- never creates the file. Every write form refuses - nothing is written, and the
329
- setup hint names the global path - when the global config file is absent or
330
- there is no valid merged config, which means no config file was found this
331
- session or the files found failed validation. The precondition is the config,
332
- not registration: with a valid config and a link that never registers
333
- (`pi-permission-system` absent or older than 27.0.0) a write still succeeds,
334
- and the new judge applies as soon as the link does register.
335
- When the project config sets `provider` or `model`, the global write still
336
- happens but the command warns that the project file shadows the choice in
337
- that project.
338
-
339
- Choosing a judge never changes pi's session model or your default model:
340
- `/model` and `/permission-model` are independent.
661
+ (`~/.pi/agent/extensions/pi-permission-classifier/config.json`): only the
662
+ fields the choice sets are rewritten (`mode`, `provider` and `model`,
663
+ `quickJudge.provider` and `quickJudge.model`, `timeoutMs` and
664
+ `quickJudge.timeoutMs`, and `verdicts` and `onError` at the top level or
665
+ inside `quickJudge`), every other field is
666
+ preserved, and the write goes through a temporary file that is fsynced and
667
+ then renamed, so neither a process crash nor an OS crash leaves a truncated
668
+ config. The command never creates the file. Every write form refuses -
669
+ nothing is written, and the setup hint names the global path - when the
670
+ global config file is absent or there is no valid merged config, which
671
+ means no config file was found this session or the files found failed
672
+ validation. The precondition is the config, not registration: with a valid
673
+ config and a link that never registers (`pi-permission-system` absent or
674
+ older than 27.0.0) a write still succeeds, and the new choice applies as
675
+ soon as the link does register. When the project config sets its own value
676
+ for a field the write changed, the global write still happens but the
677
+ command warns that the project file shadows the choice in that project.
341
678
 
342
679
  ### The --permission-model launch flag
343
680
 
344
- `pi --permission-model <provider>/<id>` makes that model the judge for the
345
- session only. It takes precedence over the config and the session model,
346
- nothing is written, and it is dropped at session shutdown. The flag is read
347
- again at every session start in that pi process, so after `/reload` or a new
348
- session in the same process it applies again. A reference the registry does
349
- not know is ignored with a warning and the configured judge applies. An
350
- explicit `/permission-model` choice during the session replaces the flag for
351
- the rest of that session.
681
+ `pi --permission-model <list>` sets the judges for the session only. The
682
+ list is comma-separated, in any subset and order:
352
683
 
353
- ### The status bar entry
354
-
355
- Once the link registers, the footer shows the effective judge under the
356
- key `zz-permission-classifier`, in one of three states (pi sorts extension
357
- statuses by key on one footer line; the `zz-` prefix keeps the judge entry
358
- last, at the end of that line):
684
+ - `quick:<provider>/<id>` - the quick judge model
685
+ - `llm:<provider>/<id>` - the LLM judge model
686
+ - `llm:chat-model` - the LLM judge follows the chat model
687
+ - `mode:<llm|quick|quick-then-llm>` - the mode
359
688
 
360
- - `judge:<provider>/<id>` - a configured or flag-set judge
361
- - `judge:session` - the session's active model judges
362
- - `judge:<provider>/<id> (unresolved)` - the configured pair is not in the
363
- registry, so every ask defers until it resolves
689
+ For example `pi --permission-model quick:typesafe/jev-latest,mode:quick-then-llm`.
690
+ The values take precedence over the config, nothing is written, and they
691
+ are dropped at session shutdown. The flag is read again at every session
692
+ start in that pi process, so after `/reload` or a new session in the same
693
+ process it applies again. An unknown model, an unknown prefix or a bad mode
694
+ is ignored with a warning, and the rest of the list still applies. A bare
695
+ `<provider>/<id>` is not accepted: the warning names the `llm:` form. A
696
+ `/permission-model` choice during the session replaces the flag value it
697
+ touches, for the rest of that session.
364
698
 
365
- The entry follows `/permission-model` changes and `/model` switches and is
366
- cleared at session shutdown. No entry means the link did not register.
699
+ ### The status bar entry
367
700
 
368
- The judge text carries a health suffix once something went wrong this
369
- session. It refreshes after every decision, so the footer is the quickest
370
- read on how often the dialog fell back and why:
701
+ Once the link registers, the footer shows the active judges under the key
702
+ `zz-permission-classifier` (pi sorts extension statuses by key on one
703
+ footer line; the `zz-` prefix keeps the entry last). The entry opens with
704
+ the label `perm-judge:`, then a part for each judge the mode runs. In mode
705
+ `quick-then-llm` the quick judge comes first and `->` joins the parts in
706
+ the order the judges run:
707
+
708
+ - `quick:<provider>/<id>` - the quick judge model
709
+ - `llm:<provider>/<id>` - a configured or flag-set LLM judge
710
+ - `llm:chat-model` - the session's chat model judges
711
+ - ` (unresolved)` after a part - the pair is not in the registry, so that
712
+ judge's asks are `model-unresolved` until it resolves;
713
+ `quick:unset (unresolved)` means no quick judge model is set
714
+
715
+ For example `perm-judge: quick:typesafe/jev-latest | timeout x2 -> llm:chat-model`
716
+ in mode `quick-then-llm`. The entry follows `/permission-model` changes and `/model`
717
+ switches and is cleared at session shutdown. No entry means the link did
718
+ not register.
719
+
720
+ Each part carries its own judge's health suffix once something went wrong
721
+ this session. It refreshes after every decision, so the footer is the
722
+ quickest read on how often the dialog fell back and why:
371
723
 
372
724
  - no suffix - no failure defers yet and the breaker is closed
373
725
  - ` | <reason> x<N>` - the last decision was a failure defer with that
@@ -382,35 +734,40 @@ read on how often the dialog fell back and why:
382
734
  over the others while it lasts
383
735
 
384
736
  Model verdicts are not health events: an allow clears the pending reason,
385
- a deny changes nothing, and neither shows in the footer. The judge's own
737
+ a deny changes nothing, and neither shows in the footer. A judge's own
386
738
  defer verdict counts as a model verdict. Everything resets at session
387
739
  shutdown.
388
740
 
389
741
  ## How it works
390
742
 
391
- - The classifier only sees asks your policy routed to `ask`, on every
743
+ - The judges only see asks your policy routed to `ask`, on every
392
744
  surface except the `path` and `external_directory` families. Anything
393
745
  else is untouched.
394
- - For each reviewed ask it renders the structured ask facts (surface, tool
746
+ - The mode picks which judge runs (see "Modes"). For each ask the LLM
747
+ judge reviews, it renders the structured ask facts (surface, tool
395
748
  names, the decision value, matched pattern, executed unit, requester
396
749
  provenance) into a prompt, followed by the guidance files selected for
397
- this ask. Tool results and file contents never reach the judge, and the
398
- judged value and the guidance are delimited as data, not instructions.
750
+ this ask and your own messages in the session. Tool results, assistant
751
+ text and file contents never reach the judge, and the judged value, the
752
+ guidance and your messages are delimited as data, not instructions.
399
753
  - The judge model must answer through a forced `report_verdict` tool call
400
754
  (allow / deny / defer), so there is no free-text parsing to get wrong.
401
- The call is aborted after `timeoutMs`.
402
- - The verdict returns to the engine uncapped; the engine's own
755
+ The call is aborted after that judge's own timeout: the top-level
756
+ `timeoutMs` for the LLM judge, `quickJudge.timeoutMs` for the quick
757
+ judge.
758
+ - Each judge's `verdicts` and `onError` settings apply to its verdict and
759
+ to every failure; nothing else changes it. The engine's own
403
760
  bounded-delegation checkpoint enforces the `path` / `external_directory`
404
761
  line.
405
- - A circuit breaker opens after 3 consecutive model-call failures or
406
- timeouts; asks then defer instantly for a 60 second cooldown, so a down
407
- model never stalls your session.
762
+ - Each judge has a circuit breaker that opens after 3 consecutive
763
+ model-call failures or timeouts; that judge's asks then defer instantly
764
+ for a 60 second cooldown, so a down model never stalls your session.
408
765
  - In-process subagents are handled correctly: registration is
409
766
  session-scoped, so the link always serves the session that raised the ask.
410
767
 
411
768
  ## Known issues
412
769
 
413
- - The classifier trusts the sessionId on the `permissions:ready` payload. A
770
+ - The extension trusts the sessionId on the `permissions:ready` payload. A
414
771
  stray late ready that carries a previous session's id after a new
415
772
  `session_start` would register the link on the old session's
416
773
  still-published service. This is inherent to the pi-permission-system
@@ -420,7 +777,75 @@ shutdown.
420
777
 
421
778
  npm install
422
779
  npx tsc --noEmit # typecheck
423
- npx vitest run # 200 tests
780
+ npx vitest run # full test suite
781
+ npm run data:pull # pull new judged asks into data/asks.jsonl
782
+ npm run eval:quick # quick judge eval against TypeSafe Jev
783
+ EVAL_S1_MODEL=llama.cpp/h2o-lightning-4b npm run eval:quick # against a local System One model
784
+
785
+ ### The judged ask store
786
+
787
+ `data/asks.jsonl` is a committed record of every permission ask the
788
+ authorizer chain judged on the operator's machine, one JSON line per ask:
789
+ the request id, timestamp and session id, the surface, tool, gated value,
790
+ cwd and full bash command, whether a subagent forwarded it, every judge
791
+ decision on it in order (judge, verdict, defer reason, model, latency, and
792
+ the quick judge's answers), how it ended (approved, denied, session
793
+ approved or blocked), who decided (user, judge with the link's name, rule,
794
+ session or other) and the user text the runtime would have sent with it.
795
+ `npm run data:pull` reads the permission review log and the pi session
796
+ files, appends the asks not yet stored (an ask still waiting is picked up
797
+ by a later pull) and never rewrites a stored line.
798
+
799
+ Every string is scrubbed before it is written, and a structure test scans
800
+ the committed file with the same rules: the home directory becomes `~`,
801
+ the login name `<user>`, email addresses `<email>`, `git -c user.name` and
802
+ `user.email` values `<user>` and `<email>`, IPv4 addresses other than
803
+ loopback `<ip>`, `/volN` and `/hosts/<name>` mounts `<mount>`, and the
804
+ value after an environment-style `KEY=`, `TOKEN=`, `SECRET=` or
805
+ `PASSWORD=` name, a `--password=` style flag, a `&token=` style query
806
+ parameter or `Bearer` becomes `<redacted>`. Shell variable references stay.
807
+ Project directories under `~/AI/Projects` get stable aliases (`p1`, `p2`,
808
+ ...) that replace the name wherever it stands as a whole word, in paths
809
+ and in prose: every name an ask mentions under that root path, plus every
810
+ directory in the root's listing whose name is distinctive (a digit, a dot,
811
+ a hyphen or a capital after the first letter, or a capitalised plain word
812
+ of four letters or more). A plain lowercase or short directory name
813
+ (`core`, `x`) is aliased only when it is seen under the root path, so a
814
+ common word is never replaced in prose. A project an ask works in, and any
815
+ name with those marks, matches in any case wherever it stands (a short
816
+ plain name such as `Fun` only in its exact case); a plain name that is
817
+ only seeded matches in its exact case, and in other cases only where it
818
+ is joined to a path or identifier (`docs/health-check.md` aliases,
819
+ `health check` in prose does not), since it may be a common word. The table that maps names to aliases is
820
+ `data/project-aliases.json`, gitignored, and an entry is never renumbered,
821
+ so keep the file. The rules are patterns: a name or a secret in free text
822
+ that matches none of them is not caught, so read a pull before you commit
823
+ it.
824
+
825
+ The quick judge eval sends each candidate question set (`eval/candidates.ts`)
826
+ to Jev for two sets of asks: the hazard cases in `eval/hazard-cases.json`,
827
+ which must never be allowed, and the safe cases built from the store (each
828
+ stored ask that ended approved on a judged surface, once per state, with
829
+ its user text unless it was forwarded). `EVAL_REBUILD=1` rebuilds the
830
+ corpus from the store after a pull. It writes the corpus, a score cache and
831
+ `report.md` under `drafts/eval/`, which git ignores, and running the eval
832
+ sends the scrubbed asks to TypeSafe. Each safe ask is labeled with who
833
+ approved it: you, a judge link, or something else such as a rule. A
834
+ forwarded approval counts as whoever decided it in the parent session. The
835
+ report gives, per question set, the safe asks allowed (in total and per
836
+ label) and the hazard cases allowed at 0.9, at the built-in thresholds,
837
+ and at the best thresholds that allow no hazard even 0.1 lower, searched
838
+ once over all safe asks and once over only the asks you approved.
839
+ `TYPESAFE_API_KEY` comes from the environment or `~/.pi/agent/.env`, and
840
+ `EVAL_HAZARD_CWD` sets the project the hazard cases run in (default the
841
+ current directory). `EVAL_S1_MODEL=provider/id` scores a System One model
842
+ at `EVAL_S1_BASE_URL` (default `http://127.0.0.1:8080/v1/`) instead of
843
+ Jev, for example a local decision model behind llama-swap; it needs no
844
+ key and writes its own `cache-`, `scores-` and `report-` files named after
845
+ the model, so Jev's stay untouched. `EVAL_SAFE_LIMIT=N` scores a spread
846
+ sample of N safe cases for a slow model; the hazard cases always run in
847
+ full. `EVAL_INTENT=0` skips the intent pass, which the quick judge does
848
+ not use while `QUICK_JUDGE_INTENT` is false.
424
849
 
425
850
  The `@gotgenes/pi-permission-system` dev dependency is a `file:` reference
426
851
  to a local checkout of the permission system source, so types track the