@gotgenes/pi-permission-model-judge 1.1.0 → 1.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.1.1](https://github.com/gotgenes/pi-packages/compare/pi-permission-model-judge-v1.1.0...pi-permission-model-judge-v1.1.1) (2026-07-22)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **pi-permission-model-judge:** force a structured verdict tool call in the model review ([5a89bba](https://github.com/gotgenes/pi-packages/commit/5a89bba94b6493790a664e87cad98514253927fe)), closes [#628](https://github.com/gotgenes/pi-packages/issues/628)
9
+
10
+
11
+ ### Documentation
12
+
13
+ * **pi-permission-model-judge:** describe the forced verdict tool call ([1a2b940](https://github.com/gotgenes/pi-packages/commit/1a2b9404ad06b083f96b455df0a66e934b3f5eab))
14
+
3
15
  ## [1.1.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-model-judge-v1.0.2...pi-permission-model-judge-v1.1.0) (2026-07-21)
4
16
 
5
17
 
package/README.md CHANGED
@@ -39,9 +39,9 @@ Once an ask matches a `typoPattern` — the case that *should* reach the model
39
39
  | `modelId` | `<provider>/<model>`. |
40
40
  | `latencyMs` | Model-call wall-clock in ms, or `null` when no call was made. |
41
41
  | `verdict` | `"deny"` or `"defer"`. |
42
- | `deferReason` | `null` on a deny, else one of `model-unresolved` / `auth-failed` / `parse-failed` / `non-deny-verdict` / `timeout` / `call-failed`. |
42
+ | `deferReason` | `null` on a deny, else one of `model-unresolved` / `auth-failed` / `no-tool-call` / `non-deny-verdict` / `timeout` / `call-failed`. |
43
43
 
44
- Cheaper events go to pi-permission-system's **debug** log, and only when its `debugLog` toggle is on: `model_judge.short_circuit` (a `no-path` or `pattern-miss` defer) and `model_judge.model_reply` (the model's raw reply text).
44
+ Cheaper events go to pi-permission-system's **debug** log, and only when its `debugLog` toggle is on: `model_judge.short_circuit` (a `no-path` or `pattern-miss` defer) and `model_judge.model_reply` (the verdict tool-call arguments as JSON, or the model's text when it emitted no tool call).
45
45
  A non-`external_directory` ask is not logged — it is not this link's concern.
46
46
 
47
47
  Because every pattern-matched ask leaves a positive record, a misconfiguration that silently defers every path (an auth failure, an unresolved model) shows up as a run of `deferReason` entries rather than an empty log.
@@ -2,7 +2,7 @@
2
2
  "$schema": "https://raw.githubusercontent.com/gotgenes/pi-packages/main/packages/pi-permission-model-judge/schemas/model-judge.schema.json",
3
3
  "provider": "anthropic",
4
4
  "model": "claude-haiku-4-5",
5
- "instructions": "You review a filesystem path a tool is about to access outside the working directory. Two common typos both point at the wrong location. (1) A doubled package segment — a package name repeated around `packages/`, for example `pi-permission-system/packages/pi-permission-system/…`, where the correct path names the package only once under the repository's `packages/` directory. (2) A dropped repository prefix — a path that jumps straight to a package directory such as `development/pi/pi-permission-system/…`, where the correct path routes through the repository's `pi-packages/packages/` directory (for example `development/pi/pi-packages/packages/pi-permission-system/…`). When the path shows either typo, deny it and give the single corrected absolute path in the reason so the caller can retry against the right location. If the path looks intentional or you are unsure, defer. Reply with strict JSON: {\"verdict\":\"deny\",\"reason\":\"…\"} to reject, or {\"verdict\":\"defer\"} otherwise.",
5
+ "instructions": "You review a filesystem path a tool is about to access outside the working directory. Two common typos both point at the wrong location. (1) A doubled package segment — a package name repeated around `packages/`, for example `pi-permission-system/packages/pi-permission-system/…`, where the correct path names the package only once under the repository's `packages/` directory. (2) A dropped repository prefix — a path that jumps straight to a package directory such as `development/pi/pi-permission-system/…`, where the correct path routes through the repository's `pi-packages/packages/` directory (for example `development/pi/pi-packages/packages/pi-permission-system/…`). When the path shows either typo, deny it and give the single corrected absolute path in the reason so the caller can retry against the right location. If the path looks intentional or you are unsure, defer.",
6
6
  "typoPatterns": [
7
7
  "([^/]+)/packages/\\1(/|$)",
8
8
  "development/pi/(?!pi-packages/)pi-[^/]+(/|$)"
@@ -49,8 +49,8 @@ If the model does not resolve (wrong id, no credentials), the reviewer records a
49
49
  ### `instructions`
50
50
 
51
51
  This is the model's system prompt.
52
- Describe the typo shape you care about and instruct the model to reply with the wrong segment and the correct location, so the invoking agent can self-correct.
53
- The reviewer expects the model to answer with strict JSON — `{"verdict":"deny","reason":"…"}` to reject, `{"verdict":"defer"}` otherwise and any other reply defers.
52
+ Describe the typo shape you care about and instruct the model to give the wrong segment and the correct location, so the invoking agent can self-correct.
53
+ The reviewer forces the model to call a single `report_verdict` tool, so the verdict is read from structured tool-call arguments rather than parsed from free text a `deny` verdict with a `reason` rejects the path, and anything else (including no tool call) defers.
54
54
 
55
55
  ### `typoPatterns`
56
56
 
@@ -110,9 +110,9 @@ The **review** log (`~/.pi/agent/extensions/pi-permission-system/logs/pi-permiss
110
110
  | `modelId` | `<provider>/<model>`. |
111
111
  | `latencyMs` | Model-call wall-clock in milliseconds, or `null` when no call was made. |
112
112
  | `verdict` | `"deny"` or `"defer"`. |
113
- | `deferReason` | `null` on a deny, else `model-unresolved` / `auth-failed` / `parse-failed` / `non-deny-verdict` / `timeout` / `call-failed`. |
113
+ | `deferReason` | `null` on a deny, else `model-unresolved` / `auth-failed` / `no-tool-call` / `non-deny-verdict` / `timeout` / `call-failed`. |
114
114
 
115
- The **debug** log (same directory, `pi-permission-system-debug.jsonl`, only when pi-permission-system's `debugLog` is on) carries the verbose and cheap-path detail: `model_judge.short_circuit` (a `no-path` or `pattern-miss` defer before the model stage), `model_judge.model_reply` (the model's raw reply text), and `model_judge.invalid_patterns` (skipped `typoPatterns`).
115
+ The **debug** log (same directory, `pi-permission-system-debug.jsonl`, only when pi-permission-system's `debugLog` is on) carries the verbose and cheap-path detail: `model_judge.short_circuit` (a `no-path` or `pattern-miss` defer before the model stage), `model_judge.model_reply` (the verdict tool-call arguments as JSON, or the model's text when it emitted no tool call), and `model_judge.invalid_patterns` (skipped `typoPatterns`).
116
116
  A non-`external_directory` ask is not logged at all.
117
117
 
118
- To diagnose "the judge defers everything," read the review log for `model_judge.decision` entries and inspect `deferReason`: an empty result means no ask ever matched a pattern, `auth-failed` / `model-unresolved` means it is misconfigured, and `non-deny-verdict` means the model saw the path and chose not to deny.
118
+ To diagnose "the judge defers everything," read the review log for `model_judge.decision` entries and inspect `deferReason`: an empty result means no ask ever matched a pattern, `auth-failed` / `model-unresolved` means it is misconfigured, `non-deny-verdict` means the model saw the path and chose not to deny, and `no-tool-call` means the model replied without calling the verdict tool (rare, since the tool is forced).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gotgenes/pi-permission-model-judge",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Deny-first typo-path model judge — a pi-permission-system Authorizer chain link",
5
5
  "type": "module",
6
6
  "imports": {
@@ -12,6 +12,8 @@ import type {
12
12
  Context,
13
13
  Model,
14
14
  TextContent,
15
+ Tool,
16
+ ToolCall,
15
17
  } from "@earendil-works/pi-ai";
16
18
  import type { AuthorizerVerdict } from "@gotgenes/pi-permission-system";
17
19
 
@@ -21,6 +23,39 @@ import type { ModelJudgeConfig } from "./config-schema";
21
23
  export const GENERIC_TEACHING_REASON =
22
24
  "This looks like a mistyped path. Verify the correct location before retrying.";
23
25
 
26
+ /**
27
+ * The single tool the model is forced to call. Forcing it (`toolChoice: "any"`)
28
+ * removes free-text JSON parsing by construction — the verdict arrives as
29
+ * structured `arguments`, so a Markdown fence or a prose preamble can no longer
30
+ * cost a verdict.
31
+ *
32
+ * The Anthropic provider reads only `parameters.properties` / `parameters.required`,
33
+ * so a plain JSON-Schema object is correct at runtime; the `as unknown as Tool`
34
+ * bridge satisfies the `TSchema`-typed `parameters` field without a `typebox`
35
+ * dependency.
36
+ */
37
+ const VERDICT_TOOL = {
38
+ name: "report_verdict",
39
+ description:
40
+ "Report whether the path is a mistyped path to reject (deny) or should be deferred to the human (defer).",
41
+ parameters: {
42
+ type: "object",
43
+ properties: {
44
+ verdict: {
45
+ type: "string",
46
+ enum: ["deny", "defer"],
47
+ description: "deny a mistyped path; defer anything else",
48
+ },
49
+ reason: {
50
+ type: "string",
51
+ description:
52
+ "Why the path is wrong and the correct location (required when denying)",
53
+ },
54
+ },
55
+ required: ["verdict"],
56
+ },
57
+ } as unknown as Tool;
58
+
24
59
  /**
25
60
  * The injected model-completion seam — structurally the `complete` export of
26
61
  * `@earendil-works/pi-ai`. Injected so tests substitute a fake.
@@ -32,6 +67,7 @@ export type CompleteFn = (
32
67
  signal?: AbortSignal;
33
68
  apiKey?: string;
34
69
  headers?: Record<string, string>;
70
+ toolChoice?: string;
35
71
  },
36
72
  ) => Promise<AssistantMessage>;
37
73
 
@@ -62,13 +98,14 @@ export interface ReviewPathInputs {
62
98
 
63
99
  /**
64
100
  * Why a model call defers, distinct enough to diagnose from the decision trail:
65
- * the reply parsed but was not JSON (`parse-failed`), was valid JSON but not a
66
- * `deny` (`non-deny-verdict`), the call was aborted at `timeoutMs` (`timeout`),
67
- * or `complete` threw for any other reason (`call-failed` — the honest superset
68
- * that catches, e.g., a 401 slipping past pre-call auth resolution).
101
+ * the reply carried no tool call to read (`no-tool-call`), the tool call named a
102
+ * verdict other than `deny` (`non-deny-verdict`), the call was aborted at
103
+ * `timeoutMs` (`timeout`), or `complete` threw for any other reason
104
+ * (`call-failed` — the honest superset that catches, e.g., a 401 slipping past
105
+ * pre-call auth resolution).
69
106
  */
70
107
  export type ModelCallDeferReason =
71
- | "parse-failed"
108
+ | "no-tool-call"
72
109
  | "non-deny-verdict"
73
110
  | "timeout"
74
111
  | "call-failed";
@@ -76,8 +113,9 @@ export type ModelCallDeferReason =
76
113
  /**
77
114
  * The full result of a model review: the verdict plus the observability the
78
115
  * decision trail records. `deferReason` is set iff the verdict is `defer` and a
79
- * model call was made; `rawReply` carries the assistant text when a reply
80
- * arrived (absent on a timeout/throw before any reply).
116
+ * model call was made; `rawReply` carries the tool-call arguments as JSON when a
117
+ * tool call arrived, or the assistant text on a `no-tool-call` defer (absent on
118
+ * a timeout/throw before any reply).
81
119
  */
82
120
  export interface ReviewOutcome {
83
121
  verdict: AuthorizerVerdict;
@@ -104,6 +142,7 @@ export async function reviewPath(
104
142
  try {
105
143
  const context: Context = {
106
144
  systemPrompt: inputs.config.instructions,
145
+ tools: [VERDICT_TOOL],
107
146
  messages: [
108
147
  {
109
148
  role: "user",
@@ -116,8 +155,9 @@ export async function reviewPath(
116
155
  signal: controller.signal,
117
156
  apiKey: inputs.apiKey,
118
157
  headers: inputs.headers,
158
+ toolChoice: "any",
119
159
  });
120
- return parseOutcome(extractText(reply).trim(), Date.now() - startedAt);
160
+ return readToolCallOutcome(reply, Date.now() - startedAt);
121
161
  } catch {
122
162
  return {
123
163
  verdict: { kind: "defer" },
@@ -129,35 +169,41 @@ export async function reviewPath(
129
169
  }
130
170
  }
131
171
 
132
- /** The user-turn prompt: hand the model the path and the required reply shape. */
172
+ /** The user-turn prompt: hand the model the path and point it at the tool. */
133
173
  function renderReviewPrompt(path: string): string {
134
174
  return [
135
175
  "A tool is about to access this path outside the working directory:",
136
176
  "",
137
177
  path,
138
178
  "",
139
- 'Reply with strict JSON. To reject a mistyped path: {"verdict":"deny","reason":"<why it is wrong and the correct location>"}.',
140
- 'Otherwise: {"verdict":"defer"}.',
179
+ 'Call report_verdict. Use verdict "deny" with a reason naming the correct location if this is a mistyped path; otherwise use verdict "defer".',
141
180
  ].join("\n");
142
181
  }
143
182
 
144
183
  /**
145
- * Map the assistant reply text to an outcome; anything but a clean `deny`
146
- * defers with the reason that distinguishes it.
184
+ * Map the forced tool call to an outcome; anything but a clean `deny` defers
185
+ * with the reason that distinguishes it. The tool call is read by position (the
186
+ * first one), not by name — under OAuth the provider rewrites the registered
187
+ * name, so the reply's tool-call name cannot be relied on.
147
188
  */
148
- function parseOutcome(rawReply: string, latencyMs: number): ReviewOutcome {
149
- let parsed: unknown;
150
- try {
151
- parsed = JSON.parse(rawReply);
152
- } catch {
189
+ function readToolCallOutcome(
190
+ reply: AssistantMessage,
191
+ latencyMs: number,
192
+ ): ReviewOutcome {
193
+ const call = reply.content.find(
194
+ (part): part is ToolCall => part.type === "toolCall",
195
+ );
196
+ if (!call) {
153
197
  return {
154
198
  verdict: { kind: "defer" },
155
- deferReason: "parse-failed",
199
+ deferReason: "no-tool-call",
156
200
  latencyMs,
157
- rawReply,
201
+ rawReply: extractText(reply),
158
202
  };
159
203
  }
160
- if (!isRecord(parsed) || parsed.verdict !== "deny") {
204
+ const args = call.arguments;
205
+ const rawReply = JSON.stringify(args);
206
+ if (args.verdict !== "deny") {
161
207
  return {
162
208
  verdict: { kind: "defer" },
163
209
  deferReason: "non-deny-verdict",
@@ -166,8 +212,8 @@ function parseOutcome(rawReply: string, latencyMs: number): ReviewOutcome {
166
212
  };
167
213
  }
168
214
  const reason =
169
- typeof parsed.reason === "string" && parsed.reason.length > 0
170
- ? parsed.reason
215
+ typeof args.reason === "string" && args.reason.length > 0
216
+ ? args.reason
171
217
  : GENERIC_TEACHING_REASON;
172
218
  return { verdict: { kind: "deny", reason }, latencyMs, rawReply };
173
219
  }
@@ -179,7 +225,3 @@ function extractText(reply: AssistantMessage): string {
179
225
  .map((part) => part.text)
180
226
  .join("");
181
227
  }
182
-
183
- function isRecord(value: unknown): value is Record<string, unknown> {
184
- return typeof value === "object" && value !== null;
185
- }