pi-permission-classifier 0.5.1 → 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/CHANGELOG.md +187 -0
- package/README.md +547 -122
- package/config/config.example.json +8 -1
- package/package.json +7 -4
- package/src/breaker.ts +13 -1
- package/src/command.ts +916 -167
- package/src/config-loader.ts +159 -30
- package/src/config-schema.ts +61 -0
- package/src/extension.ts +169 -81
- package/src/index.ts +2 -2
- package/src/judge-link.ts +144 -0
- package/src/judge.ts +201 -8
- package/src/model-review.ts +17 -2
- package/src/name-alias.ts +83 -0
- package/src/prompt.ts +57 -5
- package/src/quick-judge.ts +452 -0
- package/src/reviewer.ts +157 -43
- package/src/system-one-review.ts +341 -0
- package/src/user-intent.ts +129 -0
- package/src/verdict-policy.ts +91 -0
package/src/extension.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Extension wiring: load the config at `session_start`, register the
|
|
3
|
-
* `"
|
|
3
|
+
* `"permission-judge"` link and its deprecated alias `"classifier"` from
|
|
4
|
+
* `permissions:ready`, and dispose both on shutdown. The link reads the
|
|
5
|
+
* config's `mode` on every ask and runs the LLM judge, the quick judge, or
|
|
6
|
+
* both (see `judge-link.ts`); the names share one judge (see
|
|
7
|
+
* `name-alias.ts`).
|
|
4
8
|
*
|
|
5
9
|
* The ready handler is the sole registration site. pi-permission-system
|
|
6
10
|
* >=27.0.0 emits `permissions:ready` at least once after every extension's
|
|
@@ -25,17 +29,22 @@
|
|
|
25
29
|
* (never cached at ready), and the selection applies the trust gate and the
|
|
26
30
|
* byte caps. An older pi without a trust query counts as untrusted.
|
|
27
31
|
*
|
|
32
|
+
* The user's own messages are read for each ask from the session manager of
|
|
33
|
+
* the ctx the latest `session_start` delivered (see `user-intent.ts`); no
|
|
34
|
+
* file is read, and `session_shutdown` drops the ctx.
|
|
35
|
+
*
|
|
28
36
|
* Once the link registers, the footer status `zz-permission-classifier` names
|
|
29
|
-
* the
|
|
37
|
+
* the active judges (see `judge.ts`); it follows `model_select` and is
|
|
30
38
|
* cleared at `session_shutdown`. No status is set when the link does not
|
|
31
39
|
* register.
|
|
32
40
|
*
|
|
33
|
-
* The `--permission-model
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
41
|
+
* The `--permission-model` launch flag is read at `session_start`: a comma
|
|
42
|
+
* list of `quick:<provider>/<id>`, `llm:<provider>/<id>`, `llm:chat-model`
|
|
43
|
+
* and `mode:<mode>` (see `parseFlagOverride`). The values become a
|
|
44
|
+
* session-only override merged over the config per ask; nothing is written.
|
|
45
|
+
* An item that does not parse or resolve is ignored with a warning and the
|
|
46
|
+
* rest still applies. A /permission-model write drops the values it touches,
|
|
47
|
+
* and the override is dropped at `session_shutdown`.
|
|
39
48
|
*/
|
|
40
49
|
|
|
41
50
|
import type { Model } from "@earendil-works/pi-ai";
|
|
@@ -62,9 +71,11 @@ import {
|
|
|
62
71
|
getGlobalConfigPath,
|
|
63
72
|
getProjectConfigPath,
|
|
64
73
|
globalConfigExists,
|
|
74
|
+
globalQuickJudgeSet,
|
|
75
|
+
type JudgeSelection,
|
|
65
76
|
type LoadConfigResult,
|
|
66
77
|
loadClassifierConfig,
|
|
67
|
-
|
|
78
|
+
writeGlobalSelection,
|
|
68
79
|
} from "./config-loader";
|
|
69
80
|
import { CircuitBreaker } from "./breaker";
|
|
70
81
|
import {
|
|
@@ -80,24 +91,29 @@ import {
|
|
|
80
91
|
} from "./guidance";
|
|
81
92
|
import { formatHealthSuffix, SessionHealth } from "./health";
|
|
82
93
|
import {
|
|
94
|
+
applySessionOverride,
|
|
83
95
|
formatJudgeStatus,
|
|
84
|
-
|
|
85
|
-
|
|
96
|
+
formatQuickStatus,
|
|
97
|
+
parseFlagOverride,
|
|
86
98
|
resolveJudge,
|
|
99
|
+
resolveQuickJudge,
|
|
100
|
+
type SessionOverride,
|
|
87
101
|
} from "./judge";
|
|
102
|
+
import { createJudgeLink } from "./judge-link";
|
|
88
103
|
import type { CompleteFn } from "./model-review";
|
|
89
|
-
import {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
} from "./
|
|
104
|
+
import { createNamedAuthorizers } from "./name-alias";
|
|
105
|
+
import type { QuickJudgeRegistryLike } from "./quick-judge";
|
|
106
|
+
import type { ModelRegistryLike } from "./reviewer";
|
|
107
|
+
import { readUserIntent, type UserIntent } from "./user-intent";
|
|
93
108
|
|
|
94
|
-
/** The operator-facing chain-link name referenced from `authorizerChain`. */
|
|
95
|
-
const LINK_NAME = "classifier";
|
|
96
109
|
|
|
97
110
|
/** The footer status key naming the effective judge. pi sorts extension
|
|
98
111
|
* statuses by key on one footer line; the `zz-` prefix keeps this entry last. */
|
|
99
112
|
const STATUS_KEY = "zz-permission-classifier";
|
|
100
113
|
|
|
114
|
+
/** The label that opens the footer entry, so it reads as the permission judge. */
|
|
115
|
+
const STATUS_LABEL = "perm-judge: ";
|
|
116
|
+
|
|
101
117
|
/** The launch flag carrying a session-only judge override. */
|
|
102
118
|
const FLAG_NAME = "permission-model";
|
|
103
119
|
|
|
@@ -111,8 +127,8 @@ export interface ClassifierDependencies {
|
|
|
111
127
|
/** Load the layered config; the project layer applies only when trusted. */
|
|
112
128
|
loadConfig?: (cwd: string, projectTrusted: boolean) => LoadConfigResult;
|
|
113
129
|
complete?: CompleteFn;
|
|
114
|
-
/** Persist a
|
|
115
|
-
|
|
130
|
+
/** Persist a /permission-model selection to the global config. */
|
|
131
|
+
writeSelection?: (selection: JudgeSelection) => void;
|
|
116
132
|
/** Builds the /permission-model picker; the default mounts pi's selector. */
|
|
117
133
|
buildPicker?: PickerSeam;
|
|
118
134
|
/** Reads the context files from disk; defaults to pi's context-file walk. */
|
|
@@ -139,45 +155,42 @@ export function createClassifierExtension(
|
|
|
139
155
|
const complete: CompleteFn =
|
|
140
156
|
dependencies.complete ??
|
|
141
157
|
((model, context, options) => realComplete(model, context, options));
|
|
142
|
-
const
|
|
143
|
-
dependencies.
|
|
144
|
-
((
|
|
145
|
-
writeGlobalJudge(agentDir(), provider, model));
|
|
158
|
+
const writeSelection =
|
|
159
|
+
dependencies.writeSelection ??
|
|
160
|
+
((selection: JudgeSelection) => writeGlobalSelection(agentDir(), selection));
|
|
146
161
|
const loadGuidance = dependencies.loadGuidance ?? loadGuidanceFromDisk;
|
|
147
162
|
|
|
148
163
|
pi.registerFlag(FLAG_NAME, {
|
|
149
164
|
description:
|
|
150
|
-
"
|
|
151
|
-
"
|
|
165
|
+
"Permission judge for this session only, a comma list of " +
|
|
166
|
+
"quick:<provider>/<id>, llm:<provider>/<id>, llm:chat-model and " +
|
|
167
|
+
"mode:<llm|quick|quick-then-llm>; writes nothing.",
|
|
152
168
|
type: "string",
|
|
153
169
|
});
|
|
154
170
|
|
|
155
171
|
let config: ClassifierConfig | undefined;
|
|
156
172
|
let sessionModel: Model<any> | undefined;
|
|
157
|
-
let registry:
|
|
173
|
+
let registry:
|
|
174
|
+
| (ModelRegistryLike & CommandRegistryLike & QuickJudgeRegistryLike)
|
|
175
|
+
| undefined;
|
|
158
176
|
let ui: ExtensionContext["ui"] | undefined;
|
|
159
177
|
/** The session context, for the per-ask cwd and trust reads. */
|
|
160
178
|
let session: ExtensionContext | undefined;
|
|
161
|
-
let override:
|
|
179
|
+
let override: SessionOverride = {};
|
|
162
180
|
let dispose: (() => void) | undefined;
|
|
163
181
|
let warnedUnreachable = false;
|
|
164
|
-
/** Per-session judge health and breaker; both start fresh at shutdown. */
|
|
182
|
+
/** Per-session LLM judge health and breaker; both start fresh at shutdown. */
|
|
165
183
|
const health = new SessionHealth();
|
|
166
|
-
let breaker = new CircuitBreaker();
|
|
184
|
+
let breaker = new CircuitBreaker("llm");
|
|
185
|
+
/** The quick judge's own health and breaker, apart from the LLM judge's. */
|
|
186
|
+
const quickHealth = new SessionHealth();
|
|
187
|
+
let quickBreaker = new CircuitBreaker("quick");
|
|
167
188
|
/** The once-per-second footer refresh while the breaker cools down. */
|
|
168
189
|
let countdown: ReturnType<typeof setInterval> | undefined;
|
|
169
190
|
|
|
170
|
-
/**
|
|
171
|
-
* The config the reviewer judges with: the merged config, with the
|
|
172
|
-
* provider/model pair replaced by the session-only override when one is
|
|
173
|
-
* active. The reviewer's own resolution (registry lookup, auth check,
|
|
174
|
-
* defer on failure) is unchanged, so an override can never widen a verdict.
|
|
175
|
-
*/
|
|
191
|
+
/** The config the judges read: the merged config with the flag values over it. */
|
|
176
192
|
function effectiveConfig(): ClassifierConfig | undefined {
|
|
177
|
-
|
|
178
|
-
return config;
|
|
179
|
-
}
|
|
180
|
-
return { ...config, provider: override.provider, model: override.model };
|
|
193
|
+
return applySessionOverride(config, override);
|
|
181
194
|
}
|
|
182
195
|
|
|
183
196
|
/**
|
|
@@ -209,38 +222,68 @@ export function createClassifierExtension(
|
|
|
209
222
|
);
|
|
210
223
|
}
|
|
211
224
|
|
|
212
|
-
/**
|
|
213
|
-
function
|
|
225
|
+
/** The session cwd for the quick judge state; no session is an internal error. */
|
|
226
|
+
function sessionCwd(): string {
|
|
227
|
+
if (!session) {
|
|
228
|
+
throw new Error("no session context to read the cwd from");
|
|
229
|
+
}
|
|
230
|
+
return session.cwd;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The user intent for one ask, read from the latest session_start ctx at
|
|
235
|
+
* the moment of the ask (never cached); none once the session shut down.
|
|
236
|
+
*/
|
|
237
|
+
function getUserIntent(forwarded: boolean): UserIntent | undefined {
|
|
238
|
+
return readUserIntent(session, forwarded);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Read the launch flag; each item that does not apply is ignored with a warning. */
|
|
242
|
+
function readFlagOverride(): SessionOverride {
|
|
214
243
|
const raw = pi.getFlag(FLAG_NAME);
|
|
215
244
|
if (typeof raw !== "string" || raw.length === 0) {
|
|
216
|
-
return
|
|
245
|
+
return {};
|
|
217
246
|
}
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
247
|
+
const parsed = parseFlagOverride(raw, registry);
|
|
248
|
+
for (const warning of parsed.warnings) {
|
|
249
|
+
const message = `--${FLAG_NAME}: ${warning} Ignored.`;
|
|
250
|
+
warn(message);
|
|
251
|
+
ui?.notify(message, "warning");
|
|
221
252
|
}
|
|
222
|
-
|
|
223
|
-
`--${FLAG_NAME} ${raw} is not a known <provider>/<id>; ` +
|
|
224
|
-
"the configured judge applies.";
|
|
225
|
-
warn(message);
|
|
226
|
-
ui?.notify(message, "warning");
|
|
227
|
-
return undefined;
|
|
253
|
+
return parsed.override;
|
|
228
254
|
}
|
|
229
255
|
|
|
230
256
|
/**
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
257
|
+
* Repaint the footer: the `perm-judge: ` label, then a part for each judge
|
|
258
|
+
* the mode runs, the quick judge first and joined with ` -> ` in the order
|
|
259
|
+
* they run, each with its own health suffix. A no-op until the link
|
|
260
|
+
* registers. Refreshed on judge changes and after every judge outcome.
|
|
234
261
|
*/
|
|
235
262
|
function refreshStatus(): void {
|
|
236
263
|
if (!dispose) {
|
|
237
264
|
return;
|
|
238
265
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
266
|
+
const effective = effectiveConfig();
|
|
267
|
+
const mode = effective?.mode ?? "llm";
|
|
268
|
+
const parts: string[] = [];
|
|
269
|
+
if (mode !== "llm") {
|
|
270
|
+
parts.push(
|
|
271
|
+
formatQuickStatus(resolveQuickJudge(effective?.quickJudge ?? {}, registry)) +
|
|
272
|
+
formatHealthSuffix(quickHealth, quickBreaker.remainingMs()),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
if (mode !== "quick") {
|
|
276
|
+
parts.push(
|
|
277
|
+
formatJudgeStatus(resolveJudge(undefined, effective, registry, sessionModel)) +
|
|
278
|
+
formatHealthSuffix(health, breaker.remainingMs()),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
ui?.setStatus(STATUS_KEY, STATUS_LABEL + parts.join(" -> "));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The longer of the two breakers' remaining cooldowns. */
|
|
285
|
+
function breakerRemainingMs(): number {
|
|
286
|
+
return Math.max(breaker.remainingMs(), quickBreaker.remainingMs());
|
|
244
287
|
}
|
|
245
288
|
|
|
246
289
|
function stopCountdown(): void {
|
|
@@ -257,7 +300,7 @@ export function createClassifierExtension(
|
|
|
257
300
|
* closed. The interval never keeps the process alive.
|
|
258
301
|
*/
|
|
259
302
|
function syncCountdown(): void {
|
|
260
|
-
if (
|
|
303
|
+
if (breakerRemainingMs() === 0) {
|
|
261
304
|
stopCountdown();
|
|
262
305
|
return;
|
|
263
306
|
}
|
|
@@ -266,7 +309,7 @@ export function createClassifierExtension(
|
|
|
266
309
|
}
|
|
267
310
|
countdown = setInterval(() => {
|
|
268
311
|
refreshStatus();
|
|
269
|
-
if (
|
|
312
|
+
if (breakerRemainingMs() === 0) {
|
|
270
313
|
stopCountdown();
|
|
271
314
|
}
|
|
272
315
|
}, COUNTDOWN_TICK_MS);
|
|
@@ -276,8 +319,8 @@ export function createClassifierExtension(
|
|
|
276
319
|
pi.registerCommand(
|
|
277
320
|
COMMAND_NAME,
|
|
278
321
|
createPermissionModelCommand({
|
|
279
|
-
getConfig:
|
|
280
|
-
|
|
322
|
+
getConfig: effectiveConfig,
|
|
323
|
+
globalQuickJudgeSet: () => globalQuickJudgeSet(agentDir()),
|
|
281
324
|
getRegistry: () => registry,
|
|
282
325
|
...(dependencies.buildPicker
|
|
283
326
|
? { buildPicker: dependencies.buildPicker }
|
|
@@ -285,12 +328,23 @@ export function createClassifierExtension(
|
|
|
285
328
|
globalConfigExists: () => globalConfigExists(agentDir()),
|
|
286
329
|
globalConfigPath: () => getGlobalConfigPath(agentDir()),
|
|
287
330
|
projectConfigPath: getProjectConfigPath,
|
|
288
|
-
|
|
331
|
+
write: writeSelection,
|
|
289
332
|
reload: (cwd) => loadConfig(cwd, projectTrusted()),
|
|
290
|
-
apply: (next) => {
|
|
291
|
-
// An explicit choice replaces
|
|
333
|
+
apply: (next, selection) => {
|
|
334
|
+
// An explicit choice replaces the flag value it touches for the rest
|
|
335
|
+
// of the session.
|
|
292
336
|
config = next;
|
|
293
|
-
override =
|
|
337
|
+
override = {
|
|
338
|
+
...(selection.mode === undefined && override.mode !== undefined
|
|
339
|
+
? { mode: override.mode }
|
|
340
|
+
: {}),
|
|
341
|
+
...(selection.llm === undefined && override.llm !== undefined
|
|
342
|
+
? { llm: override.llm }
|
|
343
|
+
: {}),
|
|
344
|
+
...(selection.quick === undefined && override.quick !== undefined
|
|
345
|
+
? { quick: override.quick }
|
|
346
|
+
: {}),
|
|
347
|
+
};
|
|
294
348
|
refreshStatus();
|
|
295
349
|
},
|
|
296
350
|
}),
|
|
@@ -304,7 +358,7 @@ export function createClassifierExtension(
|
|
|
304
358
|
warn(
|
|
305
359
|
"could not resolve this session's permission service — " +
|
|
306
360
|
"pi-permission-system 27.0.0 or later must be loaded in the same " +
|
|
307
|
-
"session;
|
|
361
|
+
"session; no permission link is registered.",
|
|
308
362
|
);
|
|
309
363
|
}
|
|
310
364
|
|
|
@@ -341,20 +395,52 @@ export function createClassifierExtension(
|
|
|
341
395
|
warnUnreachableOnce();
|
|
342
396
|
return;
|
|
343
397
|
}
|
|
344
|
-
|
|
398
|
+
// One link per session: its once-per-session warnings live in the closures.
|
|
399
|
+
const judge = createJudgeLink({
|
|
345
400
|
getConfig: effectiveConfig,
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
401
|
+
getUserIntent,
|
|
402
|
+
warn,
|
|
403
|
+
llm: {
|
|
404
|
+
getSessionModel: () => sessionModel,
|
|
405
|
+
getRegistry: () => registry,
|
|
406
|
+
complete,
|
|
407
|
+
breaker,
|
|
408
|
+
getGuidance,
|
|
409
|
+
onOutcome: (outcome) => {
|
|
410
|
+
health.record(outcome);
|
|
411
|
+
refreshStatus();
|
|
412
|
+
syncCountdown();
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
quick: {
|
|
416
|
+
getRegistry: () => registry,
|
|
417
|
+
getCwd: sessionCwd,
|
|
418
|
+
getGuidance,
|
|
419
|
+
breaker: quickBreaker,
|
|
420
|
+
onOutcome: (outcome) => {
|
|
421
|
+
quickHealth.record(outcome);
|
|
422
|
+
refreshStatus();
|
|
423
|
+
syncCountdown();
|
|
424
|
+
},
|
|
355
425
|
},
|
|
356
426
|
});
|
|
357
|
-
|
|
427
|
+
// Register each name on its own: a name another extension already holds
|
|
428
|
+
// is skipped with a warning, the others stay registered, and dispose is
|
|
429
|
+
// set either way so a repeated ready never retries a taken name.
|
|
430
|
+
const disposers: (() => void)[] = [];
|
|
431
|
+
for (const { name, authorize } of createNamedAuthorizers(judge, { warn })) {
|
|
432
|
+
try {
|
|
433
|
+
disposers.push(service.registerAuthorizer(name, authorize));
|
|
434
|
+
} catch (error) {
|
|
435
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
436
|
+
warn(`could not register the chain link ${name}: ${reason}`);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
dispose = () => {
|
|
440
|
+
for (const disposeName of disposers) {
|
|
441
|
+
disposeName();
|
|
442
|
+
}
|
|
443
|
+
};
|
|
358
444
|
refreshStatus();
|
|
359
445
|
});
|
|
360
446
|
|
|
@@ -369,10 +455,12 @@ export function createClassifierExtension(
|
|
|
369
455
|
registry = undefined;
|
|
370
456
|
ui = undefined;
|
|
371
457
|
session = undefined;
|
|
372
|
-
override =
|
|
458
|
+
override = {};
|
|
373
459
|
warnedUnreachable = false;
|
|
374
460
|
stopCountdown();
|
|
375
461
|
health.reset();
|
|
376
|
-
breaker = new CircuitBreaker();
|
|
462
|
+
breaker = new CircuitBreaker("llm");
|
|
463
|
+
quickHealth.reset();
|
|
464
|
+
quickBreaker = new CircuitBreaker("quick");
|
|
377
465
|
});
|
|
378
466
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,8 +3,8 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
3
3
|
import { createClassifierExtension } from "./extension";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Entry point: register the
|
|
7
|
-
* Authorizer chain link named `"
|
|
6
|
+
* Entry point: register the permission judge as a pi-permission-system
|
|
7
|
+
* Authorizer chain link named `"permission-judge"`.
|
|
8
8
|
*/
|
|
9
9
|
export default function piPermissionClassifier(pi: ExtensionAPI): void {
|
|
10
10
|
createClassifierExtension(pi);
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The permission-judge link body: composes the LLM judge and the quick judge
|
|
3
|
+
* by the config's `mode`, read live on every ask.
|
|
4
|
+
*
|
|
5
|
+
* - `llm` runs the LLM judge alone, under the top-level `verdicts` and
|
|
6
|
+
* `onError`. It is also the mode when no config is loaded, so the LLM
|
|
7
|
+
* judge records the no-config defer.
|
|
8
|
+
* - `quick` runs the quick judge alone, under `quickJudge.verdicts` and
|
|
9
|
+
* `quickJudge.onError`.
|
|
10
|
+
* - `quick-then-llm` runs the quick judge with verdicts allow-defer and
|
|
11
|
+
* onError defer, whatever `quickJudge` says. A quick allow ends the ask;
|
|
12
|
+
* anything else goes to the LLM judge, which decides.
|
|
13
|
+
*
|
|
14
|
+
* Each judge keeps its own breaker and health seam, and writes its own
|
|
15
|
+
* decision entry. A failure never allows: each judge applies its own
|
|
16
|
+
* policy, and a throw here defers.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Authorizer, AuthorizerVerdict } from "@gotgenes/pi-permission-system";
|
|
20
|
+
|
|
21
|
+
import { CircuitBreaker } from "./breaker";
|
|
22
|
+
import {
|
|
23
|
+
type ClassifierConfig,
|
|
24
|
+
type QuickJudgeConfig,
|
|
25
|
+
quickJudgeConfigSchema,
|
|
26
|
+
} from "./config-schema";
|
|
27
|
+
import { createQuickJudge, type QuickJudgeDeps, type QuickJudgeSettings } from "./quick-judge";
|
|
28
|
+
import { type ClassifierReviewerDeps, createClassifierReviewer } from "./reviewer";
|
|
29
|
+
import { QUICK_JUDGE_INTENT } from "./system-one-review";
|
|
30
|
+
import type { UserIntent } from "./user-intent";
|
|
31
|
+
|
|
32
|
+
/** The quick judge policy in front of the LLM judge: never deny, never fail closed. */
|
|
33
|
+
const CASCADE_QUICK_POLICY = { verdicts: "allow-defer", onError: "defer" } as const;
|
|
34
|
+
|
|
35
|
+
/** Collaborators for the link, injected so the extension and tests wire them. */
|
|
36
|
+
export interface JudgeLinkDeps {
|
|
37
|
+
/** The loaded config, read live per ask (absent until session config loads). */
|
|
38
|
+
getConfig: () => ClassifierConfig | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The LLM judge's collaborators; its config comes from `getConfig` and its
|
|
41
|
+
* intent from `getUserIntent`.
|
|
42
|
+
*/
|
|
43
|
+
llm: Omit<ClassifierReviewerDeps, "getConfig" | "getUserIntent">;
|
|
44
|
+
/**
|
|
45
|
+
* The quick judge's collaborators; its settings come from `getConfig` and
|
|
46
|
+
* its intent from `getUserIntent`.
|
|
47
|
+
*/
|
|
48
|
+
quick: Omit<QuickJudgeDeps, "getSettings" | "getUserIntent">;
|
|
49
|
+
/**
|
|
50
|
+
* The user intent for an ask, read live from the session; undefined when
|
|
51
|
+
* intent is not available. `forwarded` is true for an ask a subagent
|
|
52
|
+
* forwarded.
|
|
53
|
+
*/
|
|
54
|
+
getUserIntent?: (forwarded: boolean) => UserIntent | undefined;
|
|
55
|
+
/** Operator warning sink, for the cascade's policy override. */
|
|
56
|
+
warn: (message: string) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Build the link's `authorize` callback. One callback serves a whole
|
|
61
|
+
* session: the once-per-session override warning lives in this closure.
|
|
62
|
+
*/
|
|
63
|
+
export function createJudgeLink(deps: JudgeLinkDeps): Authorizer["authorize"] {
|
|
64
|
+
let warnedOverride = false;
|
|
65
|
+
const quickBreaker = deps.quick.breaker ?? new CircuitBreaker("quick");
|
|
66
|
+
|
|
67
|
+
const llmJudge = createClassifierReviewer({
|
|
68
|
+
...deps.llm,
|
|
69
|
+
...(deps.getUserIntent ? { getUserIntent: deps.getUserIntent } : {}),
|
|
70
|
+
getConfig: deps.getConfig,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const quickSettings = (forced: boolean): QuickJudgeSettings | undefined => {
|
|
74
|
+
const config = deps.getConfig();
|
|
75
|
+
if (!config) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const quickJudge = quickJudgeOf(config);
|
|
79
|
+
if (!forced) {
|
|
80
|
+
return { quickJudge, contextBudgetBytes: config.contextBudgetBytes };
|
|
81
|
+
}
|
|
82
|
+
if (
|
|
83
|
+
!warnedOverride &&
|
|
84
|
+
(quickJudge.verdicts !== CASCADE_QUICK_POLICY.verdicts ||
|
|
85
|
+
quickJudge.onError !== CASCADE_QUICK_POLICY.onError)
|
|
86
|
+
) {
|
|
87
|
+
warnedOverride = true;
|
|
88
|
+
deps.warn(
|
|
89
|
+
`quickJudge asks for verdicts ${quickJudge.verdicts} and onError ` +
|
|
90
|
+
`${quickJudge.onError}, but in mode quick-then-llm the quick judge ` +
|
|
91
|
+
"runs with verdicts allow-defer and onError defer. Set mode quick " +
|
|
92
|
+
"to apply them.",
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
quickJudge: { ...quickJudge, ...CASCADE_QUICK_POLICY },
|
|
97
|
+
contextBudgetBytes: config.contextBudgetBytes,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// The quick judge gets the intent reader only while the eval ships intent
|
|
102
|
+
// for it; otherwise it sends its request without intent.
|
|
103
|
+
const quickIntent =
|
|
104
|
+
QUICK_JUDGE_INTENT && deps.getUserIntent ? { getUserIntent: deps.getUserIntent } : {};
|
|
105
|
+
|
|
106
|
+
// Two callbacks over one breaker and one health seam: the quick judge's
|
|
107
|
+
// state is the same whichever mode runs it.
|
|
108
|
+
const quickAlone = createQuickJudge({
|
|
109
|
+
...deps.quick,
|
|
110
|
+
...quickIntent,
|
|
111
|
+
breaker: quickBreaker,
|
|
112
|
+
getSettings: () => quickSettings(false),
|
|
113
|
+
});
|
|
114
|
+
const quickInFront = createQuickJudge({
|
|
115
|
+
...deps.quick,
|
|
116
|
+
...quickIntent,
|
|
117
|
+
breaker: quickBreaker,
|
|
118
|
+
getSettings: () => quickSettings(true),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return async (details, query, log): Promise<AuthorizerVerdict> => {
|
|
122
|
+
let mode: ClassifierConfig["mode"];
|
|
123
|
+
try {
|
|
124
|
+
mode = deps.getConfig()?.mode ?? "llm";
|
|
125
|
+
} catch {
|
|
126
|
+
return { kind: "defer" };
|
|
127
|
+
}
|
|
128
|
+
switch (mode) {
|
|
129
|
+
case "llm":
|
|
130
|
+
return llmJudge(details, query, log);
|
|
131
|
+
case "quick":
|
|
132
|
+
return quickAlone(details, query, log);
|
|
133
|
+
case "quick-then-llm": {
|
|
134
|
+
const quick = await quickInFront(details, query, log);
|
|
135
|
+
return quick.kind === "allow" ? quick : llmJudge(details, query, log);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The quick judge settings, or the defaults (no pair) when none are set. */
|
|
142
|
+
function quickJudgeOf(config: ClassifierConfig): QuickJudgeConfig {
|
|
143
|
+
return config.quickJudge ?? quickJudgeConfigSchema.parse({});
|
|
144
|
+
}
|