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/config-loader.ts
CHANGED
|
@@ -32,6 +32,9 @@ import {
|
|
|
32
32
|
CLASSIFIER_EXTENSION_ID,
|
|
33
33
|
type ClassifierConfig,
|
|
34
34
|
classifierConfigSchema,
|
|
35
|
+
type Mode,
|
|
36
|
+
type OnError,
|
|
37
|
+
type Verdicts,
|
|
35
38
|
} from "./config-schema";
|
|
36
39
|
|
|
37
40
|
const CONFIG_FILE_NAME = "config.json";
|
|
@@ -56,15 +59,10 @@ export function formatConfigIssue(issue: ConfigIssue): string {
|
|
|
56
59
|
return `${issue.sourcePath ?? "(merged)"} ${issue.path}: ${issue.message}`;
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
/**
|
|
60
|
-
* Outcome of a config load: a validated config (or `undefined`) plus issues.
|
|
61
|
-
* `projectSetsJudge` reports whether the project layer sets `provider` or
|
|
62
|
-
* `model`, so a global judge write can warn that the project file shadows it.
|
|
63
|
-
*/
|
|
62
|
+
/** Outcome of a config load: a validated config (or `undefined`) plus issues. */
|
|
64
63
|
export interface LoadConfigResult {
|
|
65
64
|
config: ClassifierConfig | undefined;
|
|
66
65
|
issues: ConfigIssue[];
|
|
67
|
-
projectSetsJudge: boolean;
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
/** Global scope: `<agentDir>/extensions/<id>/config.json`. */
|
|
@@ -93,22 +91,43 @@ export function globalConfigExists(agentDir: string): boolean {
|
|
|
93
91
|
return existsSync(getGlobalConfigPath(agentDir));
|
|
94
92
|
}
|
|
95
93
|
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Whether the global config file itself sets `quickJudge.provider` and
|
|
96
|
+
* `quickJudge.model` as non-empty strings. A missing or unreadable file, or a
|
|
97
|
+
* file that is not a JSON object, is false.
|
|
98
|
+
*/
|
|
99
|
+
export function globalQuickJudgeSet(agentDir: string): boolean {
|
|
100
|
+
try {
|
|
101
|
+
const parsed: unknown = JSON.parse(readFileSync(getGlobalConfigPath(agentDir), "utf-8"));
|
|
102
|
+
const quickJudge = isRecord(parsed) ? parsed.quickJudge : undefined;
|
|
103
|
+
return (
|
|
104
|
+
isRecord(quickJudge) &&
|
|
105
|
+
typeof quickJudge.provider === "string" &&
|
|
106
|
+
quickJudge.provider.length > 0 &&
|
|
107
|
+
typeof quickJudge.model === "string" &&
|
|
108
|
+
quickJudge.model.length > 0
|
|
109
|
+
);
|
|
110
|
+
} catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
98
113
|
}
|
|
99
114
|
|
|
100
|
-
function
|
|
101
|
-
return
|
|
115
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
116
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
102
117
|
}
|
|
103
118
|
|
|
104
119
|
/**
|
|
105
|
-
* Fields the schema still accepts
|
|
106
|
-
*
|
|
107
|
-
*
|
|
120
|
+
* Fields nothing reads: `surfaces`, which the schema still accepts, and the
|
|
121
|
+
* renamed `systemOne` and `classifierEnabled`, which it drops. A layer that
|
|
122
|
+
* sets one gets an issue naming the file, so the operator learns the field
|
|
123
|
+
* does nothing; the config itself still loads.
|
|
108
124
|
*/
|
|
109
125
|
const IGNORED_FIELDS: Record<string, string> = {
|
|
110
126
|
surfaces:
|
|
111
127
|
"This field is ignored: every surface outside the path and external_directory families is judged.",
|
|
128
|
+
systemOne: "This field is ignored: it was renamed to quickJudge.",
|
|
129
|
+
classifierEnabled:
|
|
130
|
+
"This field is ignored: set mode (llm, quick or quick-then-llm) to choose who judges.",
|
|
112
131
|
};
|
|
113
132
|
|
|
114
133
|
function reportIgnoredFields(
|
|
@@ -167,8 +186,7 @@ function readLayer(
|
|
|
167
186
|
* from the SDK's `getAgentDir()` (which honors `PI_CODING_AGENT_DIR`) and `cwd`
|
|
168
187
|
* from the session context, so this module reads no process globals of its own.
|
|
169
188
|
* `projectTrusted` is the session's trust state at this load: when false the
|
|
170
|
-
* project file is skipped unread and the global layer alone applies
|
|
171
|
-
* `projectSetsJudge` false.
|
|
189
|
+
* project file is skipped unread and the global layer alone applies.
|
|
172
190
|
*
|
|
173
191
|
* When neither file exists, returns `{ config: undefined, issues: [] }` — the
|
|
174
192
|
* normal not-configured state, reported without noise. When a present config is
|
|
@@ -189,10 +207,9 @@ export function loadClassifierConfig(options: {
|
|
|
189
207
|
const projectPath = getProjectConfigPath(cwd);
|
|
190
208
|
const global = readLayer(globalPath, issues);
|
|
191
209
|
const project = projectTrusted ? readLayer(projectPath, issues) : undefined;
|
|
192
|
-
const projectSetsJudge = setsJudge(project);
|
|
193
210
|
|
|
194
211
|
if (global === undefined && project === undefined) {
|
|
195
|
-
return { config: undefined, issues
|
|
212
|
+
return { config: undefined, issues };
|
|
196
213
|
}
|
|
197
214
|
|
|
198
215
|
reportIgnoredFields(global, globalPath, issues);
|
|
@@ -214,16 +231,15 @@ export function loadClassifierConfig(options: {
|
|
|
214
231
|
sourcePath,
|
|
215
232
|
});
|
|
216
233
|
}
|
|
217
|
-
return { config: undefined, issues
|
|
234
|
+
return { config: undefined, issues };
|
|
218
235
|
}
|
|
219
236
|
|
|
220
|
-
return { config: parsed.data, issues
|
|
237
|
+
return { config: parsed.data, issues };
|
|
221
238
|
}
|
|
222
239
|
|
|
223
240
|
/**
|
|
224
|
-
* Rewrite
|
|
225
|
-
*
|
|
226
|
-
* removed (the session model judges); the command passes both or neither.
|
|
241
|
+
* Rewrite the global config file through `transform`, which receives the
|
|
242
|
+
* parsed JSON object and returns the next one.
|
|
227
243
|
*
|
|
228
244
|
* The write is atomic and durable: the new content goes to a sibling temporary
|
|
229
245
|
* file that is fsynced before it is renamed over `config.json`, so neither a
|
|
@@ -238,10 +254,9 @@ export function loadClassifierConfig(options: {
|
|
|
238
254
|
* with the original file's mode, set exactly with `fchmodSync` so the process
|
|
239
255
|
* umask cannot silently tighten it.
|
|
240
256
|
*/
|
|
241
|
-
export function
|
|
257
|
+
export function rewriteGlobalConfig(
|
|
242
258
|
agentDir: string,
|
|
243
|
-
|
|
244
|
-
model: string | undefined,
|
|
259
|
+
transform: (current: Record<string, unknown>) => Record<string, unknown>,
|
|
245
260
|
): void {
|
|
246
261
|
const path = getGlobalConfigPath(agentDir);
|
|
247
262
|
if (!existsSync(path)) {
|
|
@@ -256,11 +271,7 @@ export function writeGlobalJudge(
|
|
|
256
271
|
throw new Error(`Global config is not a JSON object: ${path}`);
|
|
257
272
|
}
|
|
258
273
|
|
|
259
|
-
const
|
|
260
|
-
const next: Record<string, unknown> =
|
|
261
|
-
provider === undefined || model === undefined
|
|
262
|
-
? rest
|
|
263
|
-
: { ...rest, provider, model };
|
|
274
|
+
const next = transform(parsed);
|
|
264
275
|
|
|
265
276
|
const tempPath = `${target}.${process.pid}.tmp`;
|
|
266
277
|
try {
|
|
@@ -283,3 +294,121 @@ export function writeGlobalJudge(
|
|
|
283
294
|
throw error;
|
|
284
295
|
}
|
|
285
296
|
}
|
|
297
|
+
|
|
298
|
+
/** A provider/model pair naming one judge's model. */
|
|
299
|
+
export interface ModelPair {
|
|
300
|
+
provider: string;
|
|
301
|
+
model: string;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* The verdicts and onError of one judge: the LLM judge's top-level fields or
|
|
306
|
+
* the quick judge's `quickJudge` fields. An absent field is left as it is.
|
|
307
|
+
*/
|
|
308
|
+
export interface JudgePolicy {
|
|
309
|
+
judge: "llm" | "quick";
|
|
310
|
+
verdicts?: Verdicts;
|
|
311
|
+
onError?: OnError;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* One judge's model-call budget in whole seconds: the LLM judge's top-level
|
|
316
|
+
* `timeoutMs` or the quick judge's `quickJudge.timeoutMs`. `seconds: 0` clears
|
|
317
|
+
* the key, returning that judge to its built-in default; the config file only
|
|
318
|
+
* ever holds milliseconds, so the unit conversion happens at this one write
|
|
319
|
+
* path.
|
|
320
|
+
*/
|
|
321
|
+
export interface JudgeTimeout {
|
|
322
|
+
judge: "llm" | "quick";
|
|
323
|
+
seconds: number;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* What one /permission-model choice sets. An absent field is left as it is;
|
|
328
|
+
* `llm: null` removes the top-level `provider` and `model`, so the LLM judge
|
|
329
|
+
* follows the chat model.
|
|
330
|
+
*/
|
|
331
|
+
export interface JudgeSelection {
|
|
332
|
+
mode?: Mode;
|
|
333
|
+
llm?: ModelPair | null;
|
|
334
|
+
quick?: ModelPair;
|
|
335
|
+
policy?: JudgePolicy;
|
|
336
|
+
timeout?: JudgeTimeout;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Write a selection to the global config file in one atomic write: `mode`,
|
|
341
|
+
* the top-level LLM pair, `quickJudge.provider`/`quickJudge.model`, the
|
|
342
|
+
* policy and the model-call timeout, top-level for the LLM judge and inside
|
|
343
|
+
* `quickJudge` for the quick judge (creating the `quickJudge` object when
|
|
344
|
+
* absent). Every field the selection does not set is preserved, inside
|
|
345
|
+
* `quickJudge` too. A timeout of 0 removes that judge's `timeoutMs` key, and
|
|
346
|
+
* an emptied `quickJudge` object is removed with it. See
|
|
347
|
+
* {@link rewriteGlobalConfig}.
|
|
348
|
+
*/
|
|
349
|
+
export function writeGlobalSelection(agentDir: string, selection: JudgeSelection): void {
|
|
350
|
+
rewriteGlobalConfig(agentDir, (current) => {
|
|
351
|
+
let next = { ...current };
|
|
352
|
+
if (selection.mode !== undefined) {
|
|
353
|
+
next.mode = selection.mode;
|
|
354
|
+
}
|
|
355
|
+
if (selection.llm === null) {
|
|
356
|
+
const { provider: _provider, model: _model, ...rest } = next;
|
|
357
|
+
next = rest;
|
|
358
|
+
} else if (selection.llm !== undefined) {
|
|
359
|
+
next.provider = selection.llm.provider;
|
|
360
|
+
next.model = selection.llm.model;
|
|
361
|
+
}
|
|
362
|
+
if (selection.quick !== undefined) {
|
|
363
|
+
const existing = isRecord(current.quickJudge) ? current.quickJudge : {};
|
|
364
|
+
next.quickJudge = {
|
|
365
|
+
...existing,
|
|
366
|
+
provider: selection.quick.provider,
|
|
367
|
+
model: selection.quick.model,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
const policy = selection.policy;
|
|
371
|
+
if (policy !== undefined) {
|
|
372
|
+
const fields: Record<string, unknown> = {};
|
|
373
|
+
if (policy.verdicts !== undefined) {
|
|
374
|
+
fields.verdicts = policy.verdicts;
|
|
375
|
+
}
|
|
376
|
+
if (policy.onError !== undefined) {
|
|
377
|
+
fields.onError = policy.onError;
|
|
378
|
+
}
|
|
379
|
+
if (policy.judge === "llm") {
|
|
380
|
+
next = { ...next, ...fields };
|
|
381
|
+
} else {
|
|
382
|
+
const existing = isRecord(next.quickJudge) ? next.quickJudge : {};
|
|
383
|
+
next.quickJudge = { ...existing, ...fields };
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const timeout = selection.timeout;
|
|
387
|
+
if (timeout !== undefined) {
|
|
388
|
+
const timeoutMs = timeout.seconds * 1000;
|
|
389
|
+
if (timeout.judge === "llm") {
|
|
390
|
+
if (timeoutMs === 0) {
|
|
391
|
+
const { timeoutMs: _timeoutMs, ...rest } = next;
|
|
392
|
+
next = rest;
|
|
393
|
+
} else {
|
|
394
|
+
next.timeoutMs = timeoutMs;
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
const existing = isRecord(next.quickJudge) ? next.quickJudge : {};
|
|
398
|
+
if (timeoutMs > 0) {
|
|
399
|
+
next.quickJudge = { ...existing, timeoutMs };
|
|
400
|
+
} else {
|
|
401
|
+
const { timeoutMs: _timeoutMs, ...kept } = existing;
|
|
402
|
+
// Clearing the only key of an absent block must not create one.
|
|
403
|
+
if (Object.keys(kept).length === 0) {
|
|
404
|
+
const { quickJudge: _quickJudge, ...withoutQuickJudge } = next;
|
|
405
|
+
next = withoutQuickJudge;
|
|
406
|
+
} else {
|
|
407
|
+
next.quickJudge = kept;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return next;
|
|
413
|
+
});
|
|
414
|
+
}
|
package/src/config-schema.ts
CHANGED
|
@@ -22,6 +22,60 @@ export const DEFAULT_TIMEOUT_MS = 5000;
|
|
|
22
22
|
*/
|
|
23
23
|
export const DEFAULT_CONTEXT_BUDGET_BYTES = 8192;
|
|
24
24
|
|
|
25
|
+
/** Default quick judge allow and deny probability threshold. */
|
|
26
|
+
export const DEFAULT_SYSTEM_ONE_THRESHOLD = 0.9;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Which verdicts a link may return. `allow-deny-defer` passes all three;
|
|
30
|
+
* `allow-defer` never denies; `allow-deny` never defers.
|
|
31
|
+
*/
|
|
32
|
+
export const verdictsSchema = z.enum(["allow-deny-defer", "allow-defer", "allow-deny"]);
|
|
33
|
+
|
|
34
|
+
export type Verdicts = z.infer<typeof verdictsSchema>;
|
|
35
|
+
|
|
36
|
+
/** What a link returns on any failure. Never allow. */
|
|
37
|
+
export const onErrorSchema = z.enum(["defer", "deny"]);
|
|
38
|
+
|
|
39
|
+
export type OnError = z.infer<typeof onErrorSchema>;
|
|
40
|
+
|
|
41
|
+
const thresholdSchema = z
|
|
42
|
+
.number()
|
|
43
|
+
.min(0)
|
|
44
|
+
.max(1)
|
|
45
|
+
.default(DEFAULT_SYSTEM_ONE_THRESHOLD);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Who judges an ask: `llm` (the LLM judge alone, the default), `quick` (the
|
|
49
|
+
* quick judge alone), or `quick-then-llm` (the quick judge first, the LLM
|
|
50
|
+
* judge for anything it does not allow).
|
|
51
|
+
*/
|
|
52
|
+
export const modeSchema = z.enum(["llm", "quick", "quick-then-llm"]);
|
|
53
|
+
|
|
54
|
+
export type Mode = z.infer<typeof modeSchema>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Settings for the quick judge, a System One decision model.
|
|
58
|
+
* `provider`/`model` name a classifier model and come together or not at all.
|
|
59
|
+
*/
|
|
60
|
+
export const quickJudgeConfigSchema = z
|
|
61
|
+
.object({
|
|
62
|
+
provider: z.string().min(1).optional(),
|
|
63
|
+
model: z.string().min(1).optional(),
|
|
64
|
+
verdicts: verdictsSchema.default("allow-defer"),
|
|
65
|
+
onError: onErrorSchema.default("defer"),
|
|
66
|
+
// Absent by default: the quick judge then uses its built-in threshold per
|
|
67
|
+
// question. A value set here applies to all three questions.
|
|
68
|
+
allowThreshold: z.number().min(0).max(1).optional(),
|
|
69
|
+
denyThreshold: thresholdSchema,
|
|
70
|
+
guidance: z.boolean().default(true),
|
|
71
|
+
timeoutMs: z.number().int().positive().default(DEFAULT_TIMEOUT_MS),
|
|
72
|
+
})
|
|
73
|
+
.refine((config) => (config.provider === undefined) === (config.model === undefined), {
|
|
74
|
+
message: "provider and model must be set together or not at all",
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
export type QuickJudgeConfig = z.infer<typeof quickJudgeConfigSchema>;
|
|
78
|
+
|
|
25
79
|
/**
|
|
26
80
|
* Operator-owned config for the auto-classifier reviewer. An absent config
|
|
27
81
|
* file means no registration (fail-safe); an empty object means the defaults
|
|
@@ -44,6 +98,13 @@ export const classifierConfigSchema = z
|
|
|
44
98
|
.int()
|
|
45
99
|
.positive()
|
|
46
100
|
.default(DEFAULT_CONTEXT_BUDGET_BYTES),
|
|
101
|
+
// The top-level fields above and below are the LLM judge's settings.
|
|
102
|
+
// Unknown keys, such as the renamed systemOne and classifierEnabled, are
|
|
103
|
+
// dropped; the loader reports those two.
|
|
104
|
+
mode: modeSchema.default("llm"),
|
|
105
|
+
quickJudge: quickJudgeConfigSchema.optional(),
|
|
106
|
+
verdicts: verdictsSchema.default("allow-deny-defer"),
|
|
107
|
+
onError: onErrorSchema.default("defer"),
|
|
47
108
|
})
|
|
48
109
|
.refine((config) => (config.provider === undefined) === (config.model === undefined), {
|
|
49
110
|
message: "provider and model must be set together or not at all",
|