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/CHANGELOG.md +177 -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 +4 -0
- 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/command.ts
CHANGED
|
@@ -1,36 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `/permission-model` command: choose
|
|
3
|
-
*
|
|
2
|
+
* The `/permission-model` command: choose who judges permission asks (the
|
|
3
|
+
* mode) and each judge's model.
|
|
4
4
|
*
|
|
5
5
|
* Forms:
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
6
|
+
* - `mode <llm|quick|quick-then-llm>`: persist the mode. A mode that runs
|
|
7
|
+
* the quick judge is refused while the global config file sets no quick
|
|
8
|
+
* judge model; one from the launch flag or a project config does not
|
|
9
|
+
* count.
|
|
10
|
+
* - `llm <provider>/<id>`: validate through the chat model registry,
|
|
11
|
+
* persist the LLM judge pair, reload, and apply live for the next ask.
|
|
12
|
+
* A classifier model is refused.
|
|
13
|
+
* - `llm chat-model`: remove the LLM judge pair so the LLM judge follows
|
|
14
|
+
* the session's chat model.
|
|
15
|
+
* - `quick <provider>/<id>`: validate through the classifier lookup only
|
|
16
|
+
* and persist the quick judge pair. A chat model is refused.
|
|
17
|
+
* - `llm timeout <seconds>` and `quick timeout <seconds>`: persist that
|
|
18
|
+
* judge's model-call budget, entered in whole seconds and written as
|
|
19
|
+
* milliseconds. `0` clears the key, returning the judge to its built-in
|
|
20
|
+
* budget. A non-integer, a negative value or a value above the cap is
|
|
21
|
+
* refused with no write.
|
|
22
|
+
* - `verdicts <allow-deny-defer|allow-defer|allow-deny>` and
|
|
23
|
+
* `on-error <defer|deny>`: persist the policy of the last judge in line
|
|
24
|
+
* for the live mode (launch flag included): the LLM judge's top-level
|
|
25
|
+
* fields in modes llm and quick-then-llm, the quick judge's `quickJudge`
|
|
26
|
+
* fields in mode quick. The quick judge in quick-then-llm stays forced,
|
|
27
|
+
* so its policy is never offered.
|
|
28
|
+
* - Setting a model never changes the mode.
|
|
29
|
+
* - no argument in the TUI: a menu, each list mounted through
|
|
30
|
+
* `ctx.ui.custom`, with entries showing their current values: Who judges
|
|
31
|
+
* (the modes, each with a one-line description), Quick judge model (the
|
|
32
|
+
* available classifier models), LLM judge model ("Same as the chat model
|
|
33
|
+
* (follows /model)" and the available chat models), a Model-call timeout
|
|
34
|
+
* list for each judge the mode runs (preset budgets, the saved budget
|
|
35
|
+
* when it is not a preset, and the clear row), and the last judge's
|
|
36
|
+
* verdicts and on error. A model entry whose judge the mode does not run
|
|
37
|
+
* is marked unused. Each entry opens one list with the
|
|
38
|
+
* current value marked and preselected; a pick writes only that entry's
|
|
39
|
+
* fields, the same as the typed form, and the menu opens again. A quick
|
|
40
|
+
* mode picked while the global config has no quick model opens the
|
|
41
|
+
* quick model list next and writes both together. Esc in a list
|
|
42
|
+
* returns to the menu without a write; Esc on the menu closes it.
|
|
43
|
+
* - no argument outside the TUI: print the mode, the model of each judge
|
|
44
|
+
* the mode runs, the last judge's policy, and the usage line (pi's
|
|
45
|
+
* `ctx.ui.custom` returns nothing in rpc/json/print modes).
|
|
46
|
+
* - anything else, including the removed forms `<provider>/<id>`,
|
|
47
|
+
* `session` and `system-one`, gets the usage line and writes nothing.
|
|
21
48
|
*
|
|
22
|
-
* Argument completion offers `
|
|
23
|
-
*
|
|
24
|
-
*
|
|
49
|
+
* Argument completion offers the forms `mode`, `llm`, `quick`, `verdicts` and
|
|
50
|
+
* `on-error`, then the values each takes; `llm ` and `quick ` also offer
|
|
51
|
+
* `timeout`, and `llm timeout `/`quick timeout ` offer the preset budgets
|
|
52
|
+
* (see `getArgumentCompletions`).
|
|
25
53
|
*
|
|
26
54
|
* Every write form refuses when the global config file is absent or there is
|
|
27
55
|
* no valid merged config - no config file found, or the files found failed
|
|
28
56
|
* validation: nothing is written and the operator gets the setup hint naming
|
|
29
57
|
* the global path. The precondition is the config, not registration: with a
|
|
30
58
|
* valid config and a link that never registers a write still succeeds, and the
|
|
31
|
-
* new judge applies once the link does.
|
|
32
|
-
* when the project layer sets its own
|
|
33
|
-
* shadows the choice.
|
|
59
|
+
* new judge applies once the link does. Every write is one atomic write of
|
|
60
|
+
* the global file; when the project layer sets its own value for a field the
|
|
61
|
+
* write changed, the operator is warned that it shadows the choice.
|
|
34
62
|
*
|
|
35
63
|
* Choosing a judge never touches pi's session model or the operator's default
|
|
36
64
|
* model. This module performs no filesystem or network access of its own:
|
|
@@ -39,47 +67,170 @@
|
|
|
39
67
|
* in-memory config as it was, so a bad pick can never widen a verdict.
|
|
40
68
|
*/
|
|
41
69
|
|
|
42
|
-
import type { Model } from "@earendil-works/pi-ai";
|
|
70
|
+
import type { ClassifierApi, ClassifierModel, Model } from "@earendil-works/pi-ai";
|
|
71
|
+
import type { ExtensionAPI, ExtensionUIContext } from "@earendil-works/pi-coding-agent";
|
|
72
|
+
import { Container, type SelectItem, SelectList, Text } from "@earendil-works/pi-tui";
|
|
73
|
+
|
|
74
|
+
import {
|
|
75
|
+
formatConfigIssue,
|
|
76
|
+
type JudgePolicy,
|
|
77
|
+
type JudgeSelection,
|
|
78
|
+
type LoadConfigResult,
|
|
79
|
+
} from "./config-loader";
|
|
43
80
|
import {
|
|
44
|
-
|
|
45
|
-
type
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
type
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
DEFAULT_TIMEOUT_MS,
|
|
82
|
+
type ClassifierConfig,
|
|
83
|
+
type Mode,
|
|
84
|
+
modeSchema,
|
|
85
|
+
type OnError,
|
|
86
|
+
onErrorSchema,
|
|
87
|
+
quickJudgeConfigSchema,
|
|
88
|
+
type Verdicts,
|
|
89
|
+
verdictsSchema,
|
|
90
|
+
} from "./config-schema";
|
|
53
91
|
import {
|
|
54
92
|
formatJudgeStatus,
|
|
93
|
+
formatQuickStatus,
|
|
94
|
+
formatTimeoutSeconds,
|
|
55
95
|
type JudgePair,
|
|
56
96
|
parseJudgePair,
|
|
57
97
|
resolveJudge,
|
|
98
|
+
resolveQuickJudge,
|
|
58
99
|
} from "./judge";
|
|
59
100
|
|
|
60
101
|
/** The slash command name (`/permission-model`). */
|
|
61
102
|
export const COMMAND_NAME = "permission-model";
|
|
62
103
|
|
|
63
|
-
const
|
|
104
|
+
const MODE_FORM = "mode";
|
|
105
|
+
|
|
106
|
+
const LLM_FORM = "llm";
|
|
107
|
+
|
|
108
|
+
const QUICK_FORM = "quick";
|
|
109
|
+
|
|
110
|
+
const VERDICTS_FORM = "verdicts";
|
|
111
|
+
|
|
112
|
+
const ON_ERROR_FORM = "on-error";
|
|
113
|
+
|
|
114
|
+
/** The `llm` and `quick` argument that sets that judge's model-call budget. */
|
|
115
|
+
const TIMEOUT_FORM = "timeout";
|
|
116
|
+
|
|
117
|
+
/** The `llm timeout` form, and the menu entry that opens its list. */
|
|
118
|
+
const LLM_TIMEOUT_FORM = `${LLM_FORM} ${TIMEOUT_FORM}`;
|
|
119
|
+
|
|
120
|
+
/** The `quick timeout` form, and the menu entry that opens its list. */
|
|
121
|
+
const QUICK_TIMEOUT_FORM = `${QUICK_FORM} ${TIMEOUT_FORM}`;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Largest budget the timeout forms accept, in whole seconds. The config key is
|
|
125
|
+
* unbounded, so the form needs a cap of its own: a mistyped value reaches a
|
|
126
|
+
* judge as a budget, and one hour is far past any judge call that answers.
|
|
127
|
+
*/
|
|
128
|
+
const MAX_TIMEOUT_SECONDS = 3600;
|
|
129
|
+
|
|
130
|
+
/** Budgets the timeout list and its completion offer, in whole seconds. */
|
|
131
|
+
const TIMEOUT_PRESETS: readonly number[] = [5, 10, 15, 25, 30, 60];
|
|
132
|
+
|
|
133
|
+
/** The `llm` argument that makes the LLM judge follow the chat model. */
|
|
134
|
+
const CHAT_MODEL = "chat-model";
|
|
135
|
+
|
|
136
|
+
const MODES = modeSchema.options;
|
|
64
137
|
|
|
65
|
-
const
|
|
138
|
+
const VERDICTS = verdictsSchema.options;
|
|
139
|
+
|
|
140
|
+
const ON_ERRORS = onErrorSchema.options;
|
|
141
|
+
|
|
142
|
+
const USAGE =
|
|
143
|
+
`/${COMMAND_NAME} ${MODE_FORM} <${MODES.join("|")}> | ${LLM_FORM} <provider>/<id> | ` +
|
|
144
|
+
`${LLM_FORM} ${CHAT_MODEL} | ${QUICK_FORM} <provider>/<id> | ` +
|
|
145
|
+
`${LLM_FORM} ${TIMEOUT_FORM} <seconds> | ${QUICK_FORM} ${TIMEOUT_FORM} <seconds> | ` +
|
|
146
|
+
`${VERDICTS_FORM} <${VERDICTS.join("|")}> | ${ON_ERROR_FORM} <${ON_ERRORS.join("|")}>`;
|
|
147
|
+
|
|
148
|
+
/** What a write did: see `persist`. */
|
|
149
|
+
type PersistOutcome = "applied" | "not-written" | "reload-invalid";
|
|
150
|
+
|
|
151
|
+
/** The menu's title. */
|
|
152
|
+
const MENU_TITLE = "Permission judge";
|
|
153
|
+
|
|
154
|
+
/** How the menu and its lists name each judge at the start of a label. */
|
|
155
|
+
const JUDGE_TITLES: Record<JudgePolicy["judge"], string> = {
|
|
156
|
+
llm: "LLM judge",
|
|
157
|
+
quick: "Quick judge",
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** How a confirmation or the print form names each judge. */
|
|
161
|
+
const JUDGE_NAMES: Record<JudgePolicy["judge"], string> = {
|
|
162
|
+
llm: "LLM judge",
|
|
163
|
+
quick: "quick judge",
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The judge whose answer ends an ask in this mode, so the one whose verdicts
|
|
168
|
+
* and onError apply. The quick judge in quick-then-llm is forced and never
|
|
169
|
+
* last.
|
|
170
|
+
*/
|
|
171
|
+
function lastJudge(mode: Mode): JudgePolicy["judge"] {
|
|
172
|
+
return mode === "quick" ? "quick" : "llm";
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Whole seconds for a timeout form: a bare non-negative integer no larger than
|
|
177
|
+
* `MAX_TIMEOUT_SECONDS`. A sign, a decimal point, an exponent, an empty string
|
|
178
|
+
* or a value over the cap all read as `undefined`, so the caller refuses the
|
|
179
|
+
* form and writes nothing. A negative budget is meaningless and a budget past
|
|
180
|
+
* the cap is a mistyped value, never a judge that answers.
|
|
181
|
+
*/
|
|
182
|
+
function parseTimeoutSeconds(text: string | undefined): number | undefined {
|
|
183
|
+
if (text === undefined || !/^\d+$/.test(text)) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
const seconds = Number(text);
|
|
187
|
+
return seconds <= MAX_TIMEOUT_SECONDS ? seconds : undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The model-call budget a config holds for one judge, in milliseconds. */
|
|
191
|
+
function timeoutOf(
|
|
192
|
+
config: ClassifierConfig,
|
|
193
|
+
judge: JudgePolicy["judge"],
|
|
194
|
+
): number | undefined {
|
|
195
|
+
return judge === "llm" ? config.timeoutMs : config.quickJudge?.timeoutMs;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* A judge's verdicts and onError in a config, with the schema defaults for a
|
|
200
|
+
* quick judge the config does not set up.
|
|
201
|
+
*/
|
|
202
|
+
function policyOf(
|
|
203
|
+
config: ClassifierConfig,
|
|
204
|
+
judge: JudgePolicy["judge"],
|
|
205
|
+
): { verdicts: Verdicts; onError: OnError } {
|
|
206
|
+
const source =
|
|
207
|
+
judge === "llm" ? config : (config.quickJudge ?? quickJudgeConfigSchema.parse({}));
|
|
208
|
+
return { verdicts: source.verdicts, onError: source.onError };
|
|
209
|
+
}
|
|
66
210
|
|
|
67
211
|
type CommandOptions = Parameters<ExtensionAPI["registerCommand"]>[1];
|
|
68
|
-
/** pi's completion result,
|
|
69
|
-
type ArgumentCompletions =
|
|
70
|
-
ReturnType<NonNullable<CommandOptions["getArgumentCompletions"]
|
|
71
|
-
Promise<unknown>
|
|
212
|
+
/** pi's completion result, in the asynchronous form this command returns. */
|
|
213
|
+
type ArgumentCompletions = Promise<
|
|
214
|
+
Awaited<ReturnType<NonNullable<CommandOptions["getArgumentCompletions"]>>>
|
|
72
215
|
>;
|
|
73
216
|
|
|
74
217
|
type PickerFactory = Parameters<ExtensionUIContext["custom"]>[0];
|
|
75
218
|
/** pi's TUI handle, as `ctx.ui.custom` hands it to the factory. */
|
|
76
219
|
export type PickerTui = Parameters<PickerFactory>[0];
|
|
220
|
+
/** pi's theme, as `ctx.ui.custom` hands it to the factory. */
|
|
221
|
+
export type PickerTheme = Parameters<PickerFactory>[1];
|
|
77
222
|
/** What the factory returns: a component, optionally disposable. */
|
|
78
223
|
export type PickerComponent = ReturnType<PickerFactory>;
|
|
79
224
|
|
|
80
225
|
/** The narrow registry projection the command needs. */
|
|
81
226
|
export interface CommandRegistryLike {
|
|
82
227
|
find(provider: string, modelId: string): Model<any> | undefined;
|
|
228
|
+
findOfType(
|
|
229
|
+
type: "classifier",
|
|
230
|
+
provider: string,
|
|
231
|
+
modelId: string,
|
|
232
|
+
): ClassifierModel<ClassifierApi> | undefined;
|
|
233
|
+
getAvailableOfType(type: "classifier"): Promise<readonly ClassifierModel<ClassifierApi>[]>;
|
|
83
234
|
hasConfiguredAuth(model: Model<any>): boolean;
|
|
84
235
|
getAvailable(): Model<any>[];
|
|
85
236
|
}
|
|
@@ -95,65 +246,102 @@ export interface CommandContextLike {
|
|
|
95
246
|
mode: "tui" | "rpc" | "json" | "print";
|
|
96
247
|
model: Model<any> | undefined;
|
|
97
248
|
modelRegistry: CommandRegistryLike;
|
|
98
|
-
scopedModels: readonly ScopedModel[];
|
|
99
249
|
ui: Pick<ExtensionUIContext, "notify" | "select" | "custom">;
|
|
100
250
|
}
|
|
101
251
|
|
|
102
|
-
/**
|
|
252
|
+
/** One step of the picker: a titled list of values. */
|
|
103
253
|
export interface PickerRequest {
|
|
104
254
|
tui: PickerTui;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
255
|
+
theme: PickerTheme;
|
|
256
|
+
title: string;
|
|
257
|
+
items: SelectItem[];
|
|
258
|
+
/** The value preselected in the list, if it is there. */
|
|
259
|
+
selectedValue: string | undefined;
|
|
260
|
+
onSelect(value: string): void;
|
|
110
261
|
onCancel(): void;
|
|
111
262
|
}
|
|
112
263
|
|
|
113
|
-
/** Builds
|
|
264
|
+
/** Builds one picker step; injectable so tests never mount a real TUI. */
|
|
114
265
|
export type PickerSeam = (request: PickerRequest) => PickerComponent;
|
|
115
266
|
|
|
116
|
-
/**
|
|
117
|
-
|
|
118
|
-
* (tui, currentModel, modelRuntime, scopedModels, onSelect, onCancel; the
|
|
119
|
-
* trailing optional arguments are omitted). The optional set-as-default
|
|
120
|
-
* callback is deliberately not passed, so the selector has no way to rewrite
|
|
121
|
-
* the operator's default model.
|
|
122
|
-
*/
|
|
123
|
-
export const buildModelSelector: PickerSeam = (request) =>
|
|
124
|
-
new ModelSelectorComponent(
|
|
125
|
-
request.tui,
|
|
126
|
-
request.currentModel,
|
|
127
|
-
request.runtime,
|
|
128
|
-
request.scopedModels,
|
|
129
|
-
request.onSelect,
|
|
130
|
-
request.onCancel,
|
|
131
|
-
);
|
|
267
|
+
/** Rows a list shows before it scrolls. */
|
|
268
|
+
const PICKER_ROWS = 12;
|
|
132
269
|
|
|
133
270
|
/**
|
|
134
|
-
* The
|
|
135
|
-
*
|
|
136
|
-
* yields `undefined` here (and a degraded picker) instead of a crash.
|
|
271
|
+
* The production picker step: pi-tui's `SelectList` under a title, with the
|
|
272
|
+
* value, label and one-line description of each item, in the pi theme.
|
|
137
273
|
*/
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
274
|
+
export const buildSelectList: PickerSeam = (request) => {
|
|
275
|
+
const { theme } = request;
|
|
276
|
+
const list = new SelectList(request.items, Math.min(request.items.length, PICKER_ROWS), {
|
|
277
|
+
selectedPrefix: (text) => theme.fg("accent", text),
|
|
278
|
+
selectedText: (text) => theme.fg("accent", text),
|
|
279
|
+
description: (text) => theme.fg("muted", text),
|
|
280
|
+
scrollInfo: (text) => theme.fg("dim", text),
|
|
281
|
+
noMatch: (text) => theme.fg("warning", text),
|
|
282
|
+
});
|
|
283
|
+
const selected = request.items.findIndex((item) => item.value === request.selectedValue);
|
|
284
|
+
if (selected >= 0) {
|
|
285
|
+
list.setSelectedIndex(selected);
|
|
147
286
|
}
|
|
148
|
-
|
|
149
|
-
|
|
287
|
+
list.onSelect = (item) => request.onSelect(item.value);
|
|
288
|
+
list.onCancel = () => request.onCancel();
|
|
289
|
+
const container = new Container();
|
|
290
|
+
container.addChild(new Text(theme.fg("accent", theme.bold(request.title))));
|
|
291
|
+
container.addChild(list);
|
|
292
|
+
container.addChild(new Text(theme.fg("dim", "up/down move, enter select, esc cancel")));
|
|
293
|
+
return {
|
|
294
|
+
render: (width: number) => container.render(width),
|
|
295
|
+
invalidate: () => container.invalidate(),
|
|
296
|
+
handleInput: (data: string) => {
|
|
297
|
+
list.handleInput(data);
|
|
298
|
+
request.tui.requestRender();
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/** The Who judges list, in its fixed order. */
|
|
304
|
+
const MODE_ITEMS: readonly { value: Mode; description: string }[] = [
|
|
305
|
+
{
|
|
306
|
+
value: "quick-then-llm",
|
|
307
|
+
description: "The quick judge settles clear cases; the LLM judge reviews the rest.",
|
|
308
|
+
},
|
|
309
|
+
{ value: "llm", description: "A chat model reads each ask and reasons about it." },
|
|
310
|
+
{
|
|
311
|
+
value: "quick",
|
|
312
|
+
description: "A System One decision model answers three fixed questions, fast.",
|
|
313
|
+
},
|
|
314
|
+
];
|
|
315
|
+
|
|
316
|
+
/** The verdicts list, in schema order. */
|
|
317
|
+
const VERDICTS_ITEMS: readonly { value: Verdicts; description: string }[] = [
|
|
318
|
+
{ value: "allow-deny-defer", description: "Allow, deny, or send the ask to you when unsure." },
|
|
319
|
+
{ value: "allow-defer", description: "A model deny or an unsure answer goes to you." },
|
|
320
|
+
{ value: "allow-deny", description: "An unsure answer denies instead of asking you." },
|
|
321
|
+
];
|
|
322
|
+
|
|
323
|
+
/** The on error list, in schema order. */
|
|
324
|
+
const ON_ERROR_ITEMS: readonly { value: OnError; description: string }[] = [
|
|
325
|
+
{ value: "defer", description: "A failure (timeout, no reply) sends the ask to you." },
|
|
326
|
+
{ value: "deny", description: "A failure denies with a retry hint. It never allows." },
|
|
327
|
+
];
|
|
328
|
+
|
|
329
|
+
/** The LLM list entry that makes the LLM judge follow the chat model. */
|
|
330
|
+
export const CHAT_MODEL_LABEL = "Same as the chat model (follows /model)";
|
|
150
331
|
|
|
151
332
|
/** Seams the extension supplies; none of them are called on a rejected pick. */
|
|
152
333
|
export interface CommandDependencies {
|
|
153
|
-
/**
|
|
334
|
+
/**
|
|
335
|
+
* The config the judges read, with any session-only flag values over it;
|
|
336
|
+
* `undefined` when invalid or absent.
|
|
337
|
+
*/
|
|
154
338
|
getConfig(): ClassifierConfig | undefined;
|
|
155
|
-
/**
|
|
156
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Whether the global config file itself sets the quick judge pair. The mode
|
|
341
|
+
* is written there, so a quick mode is saved only beside a quick model in
|
|
342
|
+
* the same file.
|
|
343
|
+
*/
|
|
344
|
+
globalQuickJudgeSet(): boolean;
|
|
157
345
|
/** The session's model registry (captured at `session_start`), for completion. */
|
|
158
346
|
getRegistry(): CommandRegistryLike | undefined;
|
|
159
347
|
/** Whether the global config file exists (the precondition for a write). */
|
|
@@ -162,13 +350,16 @@ export interface CommandDependencies {
|
|
|
162
350
|
globalConfigPath(): string;
|
|
163
351
|
/** The project config path for `cwd`, for the shadow warning. */
|
|
164
352
|
projectConfigPath(cwd: string): string;
|
|
165
|
-
/** Persist
|
|
166
|
-
|
|
353
|
+
/** Persist a selection to the global config in one atomic write. */
|
|
354
|
+
write(selection: JudgeSelection): void;
|
|
167
355
|
/** Reload the merged config from disk after a write. */
|
|
168
356
|
reload(cwd: string): LoadConfigResult;
|
|
169
|
-
/**
|
|
170
|
-
|
|
171
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Replace the live config and refresh the status. `selection` is what the
|
|
359
|
+
* write set, so a session-only flag value for those fields is dropped.
|
|
360
|
+
*/
|
|
361
|
+
apply(config: ClassifierConfig, selection: JudgeSelection): void;
|
|
362
|
+
/** Builds one picker step; defaults to {@link buildSelectList}. */
|
|
172
363
|
buildPicker?: PickerSeam;
|
|
173
364
|
}
|
|
174
365
|
|
|
@@ -207,22 +398,21 @@ export function createPermissionModelCommand(
|
|
|
207
398
|
}
|
|
208
399
|
|
|
209
400
|
/**
|
|
210
|
-
* Write the
|
|
211
|
-
*
|
|
401
|
+
* Write the selection, reload, apply, and report. Returns `applied` when
|
|
402
|
+
* the new config went live, `not-written` when nothing was written, and
|
|
403
|
+
* `reload-invalid` when the write landed but the merged config no longer
|
|
404
|
+
* validates. Every failure leaves the live config as it was.
|
|
212
405
|
*/
|
|
213
|
-
function persist(
|
|
214
|
-
pair: JudgePair | undefined,
|
|
215
|
-
ctx: CommandContextLike,
|
|
216
|
-
): boolean {
|
|
406
|
+
function persist(selection: JudgeSelection, ctx: CommandContextLike): PersistOutcome {
|
|
217
407
|
try {
|
|
218
|
-
deps.
|
|
408
|
+
deps.write(selection);
|
|
219
409
|
} catch (error) {
|
|
220
410
|
const message = error instanceof Error ? error.message : String(error);
|
|
221
411
|
ctx.ui.notify(
|
|
222
412
|
`Could not write ${deps.globalConfigPath()}: ${message}. Nothing changed.`,
|
|
223
413
|
"error",
|
|
224
414
|
);
|
|
225
|
-
return
|
|
415
|
+
return "not-written";
|
|
226
416
|
}
|
|
227
417
|
const result = deps.reload(ctx.cwd);
|
|
228
418
|
if (result.config === undefined) {
|
|
@@ -234,36 +424,32 @@ export function createPermissionModelCommand(
|
|
|
234
424
|
`Saved to ${deps.globalConfigPath()}, but the merged config is now invalid (${describeIssues(result.issues)}). The previous judge stays active for this session; fix the config and restart.`,
|
|
235
425
|
"error",
|
|
236
426
|
);
|
|
237
|
-
return
|
|
427
|
+
return "reload-invalid";
|
|
238
428
|
}
|
|
239
|
-
deps.apply(result.config);
|
|
240
|
-
ctx.ui.notify(
|
|
241
|
-
pair
|
|
242
|
-
? `Permission judge set to ${pair.provider}/${pair.model} (saved to the global config).`
|
|
243
|
-
: "Permission judge follows the session model (provider/model removed from the global config).",
|
|
244
|
-
"info",
|
|
245
|
-
);
|
|
429
|
+
deps.apply(result.config, selection);
|
|
430
|
+
ctx.ui.notify(`${describeSelection(selection)} (saved to the global config).`, "info");
|
|
246
431
|
if (result.issues.length > 0) {
|
|
247
432
|
ctx.ui.notify(
|
|
248
|
-
`Config issues after reload: ${describeIssues(result.issues)}
|
|
433
|
+
`Config issues after reload: ${withPeriod(describeIssues(result.issues))}`,
|
|
249
434
|
"warning",
|
|
250
435
|
);
|
|
251
436
|
}
|
|
252
|
-
|
|
437
|
+
const shadowed = shadowedFields(selection, result.config);
|
|
438
|
+
if (shadowed.length > 0) {
|
|
253
439
|
ctx.ui.notify(
|
|
254
|
-
`${deps.projectConfigPath(ctx.cwd)} sets its own
|
|
440
|
+
`${deps.projectConfigPath(ctx.cwd)} sets its own ${shadowed.join(" and ")} and shadows this choice in this project.`,
|
|
255
441
|
"warning",
|
|
256
442
|
);
|
|
257
443
|
}
|
|
258
|
-
return
|
|
444
|
+
return "applied";
|
|
259
445
|
}
|
|
260
446
|
|
|
261
|
-
const buildPicker = deps.buildPicker ??
|
|
447
|
+
const buildPicker = deps.buildPicker ?? buildSelectList;
|
|
262
448
|
|
|
263
449
|
/** The judge the next ask would use. */
|
|
264
450
|
function currentJudge(ctx: CommandContextLike) {
|
|
265
451
|
return resolveJudge(
|
|
266
|
-
|
|
452
|
+
undefined,
|
|
267
453
|
deps.getConfig(),
|
|
268
454
|
ctx.modelRegistry,
|
|
269
455
|
ctx.model,
|
|
@@ -275,30 +461,55 @@ export function createPermissionModelCommand(
|
|
|
275
461
|
ctx.ui.notify(setupHint(), "warning");
|
|
276
462
|
return;
|
|
277
463
|
}
|
|
464
|
+
const config = deps.getConfig()!;
|
|
465
|
+
const parts: string[] = [];
|
|
466
|
+
if (config.mode !== "llm") {
|
|
467
|
+
parts.push(formatQuickStatus(resolveQuickJudge(config.quickJudge ?? {}, ctx.modelRegistry)));
|
|
468
|
+
}
|
|
469
|
+
if (config.mode !== "quick") {
|
|
470
|
+
parts.push(formatJudgeStatus(currentJudge(ctx)));
|
|
471
|
+
}
|
|
472
|
+
const judge = lastJudge(config.mode);
|
|
473
|
+
const policy = policyOf(config, judge);
|
|
474
|
+
// The budgets stay out of the print form: the usage line names the forms
|
|
475
|
+
// that set them, and a print is a status read, not a place to pick a
|
|
476
|
+
// number out of a list.
|
|
278
477
|
ctx.ui.notify(
|
|
279
|
-
`Permission
|
|
478
|
+
`Permission judge mode ${config.mode}: ${parts.join(", ")}; ` +
|
|
479
|
+
`${JUDGE_NAMES[judge]} verdicts ${policy.verdicts}, onError ${policy.onError}. ` +
|
|
480
|
+
`Usage: ${USAGE}`,
|
|
280
481
|
"info",
|
|
281
482
|
);
|
|
282
483
|
}
|
|
283
484
|
|
|
284
|
-
/**
|
|
285
|
-
function
|
|
286
|
-
|
|
287
|
-
const hasAuth = ctx.modelRegistry.hasConfiguredAuth(model);
|
|
288
|
-
if (persist(pair, ctx) && !hasAuth) {
|
|
485
|
+
/** Warn after a successful LLM write when the chosen model has no auth. */
|
|
486
|
+
function warnWithoutAuth(model: Model<any>, ctx: CommandContextLike): void {
|
|
487
|
+
if (!ctx.modelRegistry.hasConfiguredAuth(model)) {
|
|
289
488
|
ctx.ui.notify(
|
|
290
|
-
`No auth is configured for ${
|
|
489
|
+
`No auth is configured for ${model.provider}/${model.id}; permission asks will defer to the prompt until it is.`,
|
|
291
490
|
"warning",
|
|
292
491
|
);
|
|
293
492
|
}
|
|
294
493
|
}
|
|
295
494
|
|
|
296
|
-
/**
|
|
495
|
+
/** Persist a chat model as the LLM judge; warn when it has no auth. */
|
|
496
|
+
function applyModel(model: Model<any>, ctx: CommandContextLike): PersistOutcome {
|
|
497
|
+
const outcome = persist({ llm: { provider: model.provider, model: model.id } }, ctx);
|
|
498
|
+
if (outcome === "applied") {
|
|
499
|
+
warnWithoutAuth(model, ctx);
|
|
500
|
+
}
|
|
501
|
+
return outcome;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** Resolve a pair through the chat model registry and set it as the LLM judge. */
|
|
297
505
|
function applyPair(pair: JudgePair, ctx: CommandContextLike): void {
|
|
506
|
+
const label = `${pair.provider}/${pair.model}`;
|
|
298
507
|
const model = ctx.modelRegistry.find(pair.provider, pair.model);
|
|
299
508
|
if (!model) {
|
|
300
509
|
ctx.ui.notify(
|
|
301
|
-
|
|
510
|
+
ctx.modelRegistry.findOfType("classifier", pair.provider, pair.model)
|
|
511
|
+
? `${label} is a classifier model, not a chat model; set it with /${COMMAND_NAME} ${QUICK_FORM} ${label}. Nothing changed.`
|
|
512
|
+
: `Unknown model ${label}: not in the model registry. Nothing changed.`,
|
|
302
513
|
"error",
|
|
303
514
|
);
|
|
304
515
|
return;
|
|
@@ -306,66 +517,420 @@ export function createPermissionModelCommand(
|
|
|
306
517
|
applyModel(model, ctx);
|
|
307
518
|
}
|
|
308
519
|
|
|
309
|
-
/**
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
520
|
+
/**
|
|
521
|
+
* Resolve a pair through the classifier lookup only and set it as the
|
|
522
|
+
* quick judge. The session model and the LLM judge are never touched.
|
|
523
|
+
*/
|
|
524
|
+
function applyQuickPair(pair: JudgePair, ctx: CommandContextLike): void {
|
|
525
|
+
const label = `${pair.provider}/${pair.model}`;
|
|
526
|
+
const model = ctx.modelRegistry.findOfType("classifier", pair.provider, pair.model);
|
|
527
|
+
if (!model) {
|
|
315
528
|
ctx.ui.notify(
|
|
316
|
-
|
|
317
|
-
|
|
529
|
+
ctx.modelRegistry.find(pair.provider, pair.model)
|
|
530
|
+
? `${label} is a chat model; the quick judge needs a classifier model. Set it with /${COMMAND_NAME} ${LLM_FORM} ${label}. Nothing changed.`
|
|
531
|
+
: `Unknown classifier model ${label}: the model registry has no classifier by that name. Nothing changed.`,
|
|
532
|
+
"error",
|
|
318
533
|
);
|
|
319
534
|
return;
|
|
320
535
|
}
|
|
321
|
-
|
|
322
|
-
"The model picker degraded to a plain list: this pi version does not expose the registry runtime the searchable selector needs.",
|
|
323
|
-
"warning",
|
|
324
|
-
);
|
|
325
|
-
const chosen = await ctx.ui.select("Permission judge model", labels);
|
|
326
|
-
// Labels are built as provider/id above, so the parse cannot fail.
|
|
327
|
-
const pair = chosen === undefined ? undefined : parseJudgePair(chosen);
|
|
328
|
-
if (pair) {
|
|
329
|
-
applyPair(pair, ctx);
|
|
330
|
-
}
|
|
536
|
+
persist({ quick: { provider: model.provider, model: model.id } }, ctx);
|
|
331
537
|
}
|
|
332
538
|
|
|
333
|
-
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
539
|
+
/**
|
|
540
|
+
* Set the mode; a mode that runs the quick judge needs a quick model in the
|
|
541
|
+
* global config file first. A quick model from the launch flag or a project
|
|
542
|
+
* config does not count: the mode is saved globally and would outlive the
|
|
543
|
+
* flag or apply in other projects.
|
|
544
|
+
*/
|
|
545
|
+
function applyMode(mode: Mode, ctx: CommandContextLike): PersistOutcome {
|
|
546
|
+
if (mode !== "llm" && !deps.globalQuickJudgeSet()) {
|
|
547
|
+
ctx.ui.notify(
|
|
548
|
+
`Mode ${mode} needs a quick judge model in the global config: set one first with /${COMMAND_NAME} ${QUICK_FORM} <provider>/<id>. A quick model from the launch flag or a project config does not count. Nothing changed.`,
|
|
549
|
+
"warning",
|
|
550
|
+
);
|
|
551
|
+
return "not-written";
|
|
339
552
|
}
|
|
340
|
-
|
|
341
|
-
|
|
553
|
+
return persist({ mode }, ctx);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Persist one judge's model-call budget, entered in whole seconds. `0`
|
|
558
|
+
* clears the key and the judge returns to its built-in budget.
|
|
559
|
+
*/
|
|
560
|
+
function applyTimeout(
|
|
561
|
+
judge: JudgePolicy["judge"],
|
|
562
|
+
seconds: number,
|
|
563
|
+
ctx: CommandContextLike,
|
|
564
|
+
): PersistOutcome {
|
|
565
|
+
return persist({ timeout: { judge, seconds } }, ctx);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Persist verdicts or onError for the last judge of the live mode. Called
|
|
570
|
+
* only after `canWrite`, so the config is there.
|
|
571
|
+
*/
|
|
572
|
+
function applyPolicy(
|
|
573
|
+
fields: Omit<JudgePolicy, "judge">,
|
|
574
|
+
ctx: CommandContextLike,
|
|
575
|
+
): PersistOutcome {
|
|
576
|
+
return persist({ policy: { judge: lastJudge(deps.getConfig()!.mode), ...fields } }, ctx);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Mount one picker step; resolves the chosen value, or `undefined` on cancel. */
|
|
580
|
+
async function step(
|
|
581
|
+
ctx: CommandContextLike,
|
|
582
|
+
title: string,
|
|
583
|
+
items: SelectItem[],
|
|
584
|
+
selectedValue: string | undefined,
|
|
585
|
+
): Promise<string | undefined> {
|
|
586
|
+
return ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) =>
|
|
342
587
|
buildPicker({
|
|
343
588
|
tui,
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
},
|
|
351
|
-
onCancel: () => done(),
|
|
589
|
+
theme,
|
|
590
|
+
title,
|
|
591
|
+
items,
|
|
592
|
+
selectedValue,
|
|
593
|
+
onSelect: (value) => done(value),
|
|
594
|
+
onCancel: () => done(undefined),
|
|
352
595
|
}),
|
|
353
596
|
);
|
|
354
597
|
}
|
|
355
598
|
|
|
356
|
-
|
|
599
|
+
/**
|
|
600
|
+
* The no-argument TUI form: a menu of entries, each showing its current
|
|
601
|
+
* value. An entry opens one list; a pick there writes only that entry's
|
|
602
|
+
* fields and the menu opens again with the new values. Esc in a list
|
|
603
|
+
* returns to the menu without a write; Esc on the menu closes it. A write
|
|
604
|
+
* that lands but leaves the merged config invalid closes the menu.
|
|
605
|
+
*/
|
|
606
|
+
async function pick(ctx: CommandContextLike): Promise<void> {
|
|
607
|
+
let selected: string = MODE_FORM;
|
|
608
|
+
for (;;) {
|
|
609
|
+
const config = deps.getConfig();
|
|
610
|
+
if (config === undefined) {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
const entry = await step(ctx, MENU_TITLE, menuItems(config), selected);
|
|
614
|
+
if (entry === undefined) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
selected = entry;
|
|
618
|
+
if ((await pickEntry(entry, config, ctx)) === "reload-invalid") {
|
|
619
|
+
// The file now holds a config that does not validate; more picks
|
|
620
|
+
// would only write to it. The error already says what to fix.
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** The menu entries, each with the live config's current value. */
|
|
627
|
+
function menuItems(config: ClassifierConfig): SelectItem[] {
|
|
628
|
+
const judge = lastJudge(config.mode);
|
|
629
|
+
const policy = policyOf(config, judge);
|
|
630
|
+
const unused = ` (not used in ${config.mode})`;
|
|
631
|
+
const quick = config.quickJudge;
|
|
632
|
+
const quickValue =
|
|
633
|
+
quick?.provider !== undefined && quick.model !== undefined
|
|
634
|
+
? `${quick.provider}/${quick.model}`
|
|
635
|
+
: "not set";
|
|
636
|
+
const llmValue =
|
|
637
|
+
config.provider !== undefined && config.model !== undefined
|
|
638
|
+
? `${config.provider}/${config.model}`
|
|
639
|
+
: CHAT_MODEL_LABEL;
|
|
640
|
+
return [
|
|
641
|
+
{ value: MODE_FORM, label: "Who judges", description: config.mode },
|
|
642
|
+
{
|
|
643
|
+
value: QUICK_FORM,
|
|
644
|
+
label: "Quick judge model",
|
|
645
|
+
description: config.mode === "llm" ? quickValue + unused : quickValue,
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
value: LLM_FORM,
|
|
649
|
+
label: "LLM judge model",
|
|
650
|
+
description: config.mode === "quick" ? llmValue + unused : llmValue,
|
|
651
|
+
},
|
|
652
|
+
// A judge the mode never runs has no budget to set, so its timeout
|
|
653
|
+
// entry is absent rather than marked unused.
|
|
654
|
+
...(config.mode === "llm"
|
|
655
|
+
? []
|
|
656
|
+
: [
|
|
657
|
+
{
|
|
658
|
+
value: QUICK_TIMEOUT_FORM,
|
|
659
|
+
label: `${JUDGE_TITLES.quick} model-call timeout`,
|
|
660
|
+
description: `${formatTimeoutSeconds(timeoutOf(config, "quick"))} seconds`,
|
|
661
|
+
},
|
|
662
|
+
]),
|
|
663
|
+
...(config.mode === "quick"
|
|
664
|
+
? []
|
|
665
|
+
: [
|
|
666
|
+
{
|
|
667
|
+
value: LLM_TIMEOUT_FORM,
|
|
668
|
+
label: `${JUDGE_TITLES.llm} model-call timeout`,
|
|
669
|
+
description: `${formatTimeoutSeconds(timeoutOf(config, "llm"))} seconds`,
|
|
670
|
+
},
|
|
671
|
+
]),
|
|
672
|
+
{
|
|
673
|
+
value: VERDICTS_FORM,
|
|
674
|
+
label: `${JUDGE_TITLES[judge]} verdicts`,
|
|
675
|
+
description: policy.verdicts,
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
value: ON_ERROR_FORM,
|
|
679
|
+
label: `${JUDGE_TITLES[judge]} on error`,
|
|
680
|
+
description: policy.onError,
|
|
681
|
+
},
|
|
682
|
+
];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/** Open the list for one menu entry and write the pick, if any. */
|
|
686
|
+
async function pickEntry(
|
|
687
|
+
entry: string,
|
|
688
|
+
config: ClassifierConfig,
|
|
689
|
+
ctx: CommandContextLike,
|
|
690
|
+
): Promise<PersistOutcome> {
|
|
691
|
+
if (entry === MODE_FORM) {
|
|
692
|
+
const mode = modeSchema.safeParse(
|
|
693
|
+
await step(ctx, "Who judges", markCurrent(MODE_ITEMS, config.mode), config.mode),
|
|
694
|
+
);
|
|
695
|
+
if (!mode.success) {
|
|
696
|
+
return "not-written";
|
|
697
|
+
}
|
|
698
|
+
if (mode.data !== "llm" && !deps.globalQuickJudgeSet()) {
|
|
699
|
+
// The mode is saved globally, so it needs a quick model beside it:
|
|
700
|
+
// ask for one now and write both together.
|
|
701
|
+
const pair = await pickQuickModel(config, ctx);
|
|
702
|
+
return pair ? persist({ mode: mode.data, quick: pair }, ctx) : "not-written";
|
|
703
|
+
}
|
|
704
|
+
return applyMode(mode.data, ctx);
|
|
705
|
+
}
|
|
706
|
+
if (entry === QUICK_FORM) {
|
|
707
|
+
const pair = await pickQuickModel(config, ctx);
|
|
708
|
+
return pair ? persist({ quick: pair }, ctx) : "not-written";
|
|
709
|
+
}
|
|
710
|
+
if (entry === LLM_FORM) {
|
|
711
|
+
return pickLlmModel(config, ctx);
|
|
712
|
+
}
|
|
713
|
+
if (entry === QUICK_TIMEOUT_FORM || entry === LLM_TIMEOUT_FORM) {
|
|
714
|
+
return pickTimeout(entry === QUICK_TIMEOUT_FORM ? "quick" : "llm", config, ctx);
|
|
715
|
+
}
|
|
716
|
+
const judge = lastJudge(config.mode);
|
|
717
|
+
const policy = policyOf(config, judge);
|
|
718
|
+
if (entry === VERDICTS_FORM) {
|
|
719
|
+
const verdicts = verdictsSchema.safeParse(
|
|
720
|
+
await step(
|
|
721
|
+
ctx,
|
|
722
|
+
`${JUDGE_TITLES[judge]} verdicts`,
|
|
723
|
+
markCurrent(VERDICTS_ITEMS, policy.verdicts),
|
|
724
|
+
policy.verdicts,
|
|
725
|
+
),
|
|
726
|
+
);
|
|
727
|
+
return verdicts.success ? applyPolicy({ verdicts: verdicts.data }, ctx) : "not-written";
|
|
728
|
+
}
|
|
729
|
+
if (entry === ON_ERROR_FORM) {
|
|
730
|
+
const onError = onErrorSchema.safeParse(
|
|
731
|
+
await step(
|
|
732
|
+
ctx,
|
|
733
|
+
`${JUDGE_TITLES[judge]} on error`,
|
|
734
|
+
markCurrent(ON_ERROR_ITEMS, policy.onError),
|
|
735
|
+
policy.onError,
|
|
736
|
+
),
|
|
737
|
+
);
|
|
738
|
+
return onError.success ? applyPolicy({ onError: onError.data }, ctx) : "not-written";
|
|
739
|
+
}
|
|
740
|
+
return "not-written";
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* The quick model list: the available classifier models, the current one
|
|
745
|
+
* marked and preselected (first, as not available, when it is not in the
|
|
746
|
+
* list). Resolves the pick, or `undefined` on a cancel, on a pick of the
|
|
747
|
+
* unavailable current row (with a note), or when none is available (with
|
|
748
|
+
* a warning).
|
|
749
|
+
*/
|
|
750
|
+
async function pickQuickModel(
|
|
751
|
+
config: ClassifierConfig,
|
|
752
|
+
ctx: CommandContextLike,
|
|
753
|
+
): Promise<JudgePair | undefined> {
|
|
754
|
+
const labels = (await ctx.modelRegistry.getAvailableOfType("classifier")).map(
|
|
755
|
+
(model) => `${model.provider}/${model.id}`,
|
|
756
|
+
);
|
|
757
|
+
if (labels.length === 0) {
|
|
758
|
+
ctx.ui.notify(
|
|
759
|
+
"No classifier models are available for the quick judge. Nothing changed.",
|
|
760
|
+
"warning",
|
|
761
|
+
);
|
|
762
|
+
return undefined;
|
|
763
|
+
}
|
|
764
|
+
const current = config.quickJudge;
|
|
765
|
+
const currentLabel =
|
|
766
|
+
current?.provider !== undefined && current.model !== undefined
|
|
767
|
+
? `${current.provider}/${current.model}`
|
|
768
|
+
: undefined;
|
|
769
|
+
const chosen = await step(
|
|
770
|
+
ctx,
|
|
771
|
+
"Quick judge model",
|
|
772
|
+
withCurrent(
|
|
773
|
+
labels.map((label) => ({ value: label, label })),
|
|
774
|
+
currentLabel,
|
|
775
|
+
),
|
|
776
|
+
currentLabel,
|
|
777
|
+
);
|
|
778
|
+
if (chosen === undefined) {
|
|
779
|
+
return undefined;
|
|
780
|
+
}
|
|
781
|
+
if (!labels.includes(chosen)) {
|
|
782
|
+
notifyNotAvailable(chosen, ctx);
|
|
783
|
+
return undefined;
|
|
784
|
+
}
|
|
785
|
+
// Labels are built as provider/id above, so the parse cannot fail.
|
|
786
|
+
return parseJudgePair(chosen);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* The timeout list for one judge: the preset budgets, the saved budget when
|
|
791
|
+
* it is not a preset, and the clear row that returns the judge to its
|
|
792
|
+
* built-in budget. The saved budget is marked `(current)` and preselected,
|
|
793
|
+
* and a pick of it writes nothing: the config already holds it, and a
|
|
794
|
+
* budget that is not whole seconds cannot go through the seconds form
|
|
795
|
+
* without change.
|
|
796
|
+
*/
|
|
797
|
+
async function pickTimeout(
|
|
798
|
+
judge: JudgePolicy["judge"],
|
|
799
|
+
config: ClassifierConfig,
|
|
800
|
+
ctx: CommandContextLike,
|
|
801
|
+
): Promise<PersistOutcome> {
|
|
802
|
+
const current = formatTimeoutSeconds(timeoutOf(config, judge));
|
|
803
|
+
const presets: { value: string; description: string }[] = TIMEOUT_PRESETS.map(
|
|
804
|
+
(preset) => ({
|
|
805
|
+
value: String(preset),
|
|
806
|
+
description: `Give the ${JUDGE_NAMES[judge]} ${preset} seconds per model call.`,
|
|
807
|
+
}),
|
|
808
|
+
);
|
|
809
|
+
if (!presets.some((item) => item.value === current)) {
|
|
810
|
+
presets.push({
|
|
811
|
+
value: current,
|
|
812
|
+
description: `The budget the config holds for the ${JUDGE_NAMES[judge]} now.`,
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
presets.push({
|
|
816
|
+
value: "0",
|
|
817
|
+
description: `Clear the saved budget: the ${JUDGE_NAMES[judge]} uses its built-in ${formatTimeoutSeconds(undefined)} seconds.`,
|
|
818
|
+
});
|
|
819
|
+
const chosen = await step(
|
|
820
|
+
ctx,
|
|
821
|
+
`${JUDGE_TITLES[judge]} model-call timeout`,
|
|
822
|
+
markCurrent(presets, current),
|
|
823
|
+
current,
|
|
824
|
+
);
|
|
825
|
+
if (chosen === current) {
|
|
826
|
+
return "not-written";
|
|
827
|
+
}
|
|
828
|
+
const seconds = parseTimeoutSeconds(chosen);
|
|
829
|
+
if (seconds === undefined) {
|
|
830
|
+
return "not-written";
|
|
831
|
+
}
|
|
832
|
+
return applyTimeout(judge, seconds, ctx);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* The LLM model list: the chat model option, then the available chat
|
|
837
|
+
* models, the current one marked and preselected. A pick is written at
|
|
838
|
+
* once, with the auth warning for a model that has none.
|
|
839
|
+
*/
|
|
840
|
+
async function pickLlmModel(
|
|
841
|
+
config: ClassifierConfig,
|
|
842
|
+
ctx: CommandContextLike,
|
|
843
|
+
): Promise<PersistOutcome> {
|
|
844
|
+
const models = ctx.modelRegistry.getAvailable();
|
|
845
|
+
const current =
|
|
846
|
+
config.provider !== undefined && config.model !== undefined
|
|
847
|
+
? `${config.provider}/${config.model}`
|
|
848
|
+
: CHAT_MODEL;
|
|
849
|
+
const items = withCurrent(
|
|
850
|
+
[
|
|
851
|
+
{ value: CHAT_MODEL, label: CHAT_MODEL_LABEL },
|
|
852
|
+
...models.map((model) => {
|
|
853
|
+
const label = `${model.provider}/${model.id}`;
|
|
854
|
+
return { value: label, label };
|
|
855
|
+
}),
|
|
856
|
+
],
|
|
857
|
+
current,
|
|
858
|
+
);
|
|
859
|
+
const chosen = await step(ctx, "LLM judge model", items, current);
|
|
860
|
+
if (chosen === undefined) {
|
|
861
|
+
return "not-written";
|
|
862
|
+
}
|
|
863
|
+
if (chosen === CHAT_MODEL) {
|
|
864
|
+
return persist({ llm: null }, ctx);
|
|
865
|
+
}
|
|
866
|
+
const model = models.find((candidate) => `${candidate.provider}/${candidate.id}` === chosen);
|
|
867
|
+
if (!model) {
|
|
868
|
+
// The unavailable current row matches no model and changes nothing.
|
|
869
|
+
notifyNotAvailable(chosen, ctx);
|
|
870
|
+
return "not-written";
|
|
871
|
+
}
|
|
872
|
+
return applyModel(model, ctx);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Complete the form, then its value: the modes after `mode `, the
|
|
877
|
+
* chat-model option, `timeout` and the available chat model labels after
|
|
878
|
+
* `llm `, and `timeout` and the available classifier model labels after
|
|
879
|
+
* `quick `, each filtered by the prefix. After `llm timeout ` or
|
|
880
|
+
* `quick timeout ` the preset budgets and the clear value are offered.
|
|
881
|
+
*/
|
|
882
|
+
async function getArgumentCompletions(prefix: string): ArgumentCompletions {
|
|
357
883
|
const registry = deps.getRegistry();
|
|
358
884
|
if (!registry) {
|
|
359
885
|
return null;
|
|
360
886
|
}
|
|
361
|
-
const
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
.
|
|
368
|
-
|
|
887
|
+
const text = prefix.trimStart().toLowerCase();
|
|
888
|
+
const space = text.indexOf(" ");
|
|
889
|
+
if (space < 0) {
|
|
890
|
+
const items = [MODE_FORM, LLM_FORM, QUICK_FORM, VERDICTS_FORM, ON_ERROR_FORM]
|
|
891
|
+
.filter((form) => form.startsWith(text))
|
|
892
|
+
.map((form) => ({ value: form, label: form }));
|
|
893
|
+
return items.length > 0 ? items : null;
|
|
894
|
+
}
|
|
895
|
+
const form = text.slice(0, space);
|
|
896
|
+
const wanted = text.slice(space + 1).trim();
|
|
897
|
+
const words = text.split(/\s+/);
|
|
898
|
+
if (
|
|
899
|
+
(form === LLM_FORM || form === QUICK_FORM) &&
|
|
900
|
+
words[1] === TIMEOUT_FORM &&
|
|
901
|
+
words.length > 2
|
|
902
|
+
) {
|
|
903
|
+
const typed = words[2]!;
|
|
904
|
+
const budgets = [...TIMEOUT_PRESETS, 0]
|
|
905
|
+
.map((seconds) => String(seconds))
|
|
906
|
+
.filter((seconds) => seconds.startsWith(typed))
|
|
907
|
+
.map((seconds) => ({ value: `${form} ${TIMEOUT_FORM} ${seconds}`, label: `${seconds} s` }));
|
|
908
|
+
return budgets.length > 0 ? budgets : null;
|
|
909
|
+
}
|
|
910
|
+
const options =
|
|
911
|
+
form === MODE_FORM
|
|
912
|
+
? [...MODES]
|
|
913
|
+
: form === LLM_FORM
|
|
914
|
+
? [
|
|
915
|
+
CHAT_MODEL,
|
|
916
|
+
TIMEOUT_FORM,
|
|
917
|
+
...registry.getAvailable().map((model) => `${model.provider}/${model.id}`),
|
|
918
|
+
]
|
|
919
|
+
: form === QUICK_FORM
|
|
920
|
+
? [
|
|
921
|
+
TIMEOUT_FORM,
|
|
922
|
+
...(await registry.getAvailableOfType("classifier")).map(
|
|
923
|
+
(model) => `${model.provider}/${model.id}`,
|
|
924
|
+
),
|
|
925
|
+
]
|
|
926
|
+
: form === VERDICTS_FORM
|
|
927
|
+
? [...VERDICTS]
|
|
928
|
+
: form === ON_ERROR_FORM
|
|
929
|
+
? [...ON_ERRORS]
|
|
930
|
+
: [];
|
|
931
|
+
const items = options
|
|
932
|
+
.filter((option) => option.toLowerCase().startsWith(wanted))
|
|
933
|
+
.map((option) => ({ value: `${form} ${option}`, label: option }));
|
|
369
934
|
return items.length > 0 ? items : null;
|
|
370
935
|
}
|
|
371
936
|
|
|
@@ -379,25 +944,209 @@ export function createPermissionModelCommand(
|
|
|
379
944
|
}
|
|
380
945
|
return;
|
|
381
946
|
}
|
|
382
|
-
|
|
947
|
+
const [head, ...tail] = text.split(/\s+/);
|
|
948
|
+
const value = tail.length === 1 ? tail[0]! : undefined;
|
|
949
|
+
if (head === MODE_FORM && value !== undefined) {
|
|
950
|
+
const mode = modeSchema.safeParse(value);
|
|
951
|
+
if (!mode.success) {
|
|
952
|
+
ctx.ui.notify(`Unknown mode ${value}. Usage: ${USAGE}`, "error");
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
if (canWrite(ctx)) {
|
|
956
|
+
applyMode(mode.data, ctx);
|
|
957
|
+
}
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
if (head === LLM_FORM && value === CHAT_MODEL) {
|
|
961
|
+
if (canWrite(ctx)) {
|
|
962
|
+
persist({ llm: null }, ctx);
|
|
963
|
+
}
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
// `llm timeout <seconds>` and `quick timeout <seconds>`: the budget form
|
|
967
|
+
// for one named judge, whatever the mode runs. A judge the mode does not
|
|
968
|
+
// run keeps its budget: the entry is not offered in the menu.
|
|
969
|
+
if (
|
|
970
|
+
(head === LLM_FORM || head === QUICK_FORM) &&
|
|
971
|
+
tail.length === 2 &&
|
|
972
|
+
tail[0] === TIMEOUT_FORM
|
|
973
|
+
) {
|
|
974
|
+
const seconds = parseTimeoutSeconds(tail[1]);
|
|
975
|
+
if (seconds === undefined) {
|
|
976
|
+
ctx.ui.notify(
|
|
977
|
+
`Timeout must be a whole number of seconds from 0 to ${MAX_TIMEOUT_SECONDS}, where 0 clears the saved budget. Usage: ${USAGE}`,
|
|
978
|
+
"error",
|
|
979
|
+
);
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
if (canWrite(ctx)) {
|
|
983
|
+
applyTimeout(head === LLM_FORM ? "llm" : "quick", seconds, ctx);
|
|
984
|
+
}
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
if (head === VERDICTS_FORM && value !== undefined) {
|
|
988
|
+
const verdicts = verdictsSchema.safeParse(value);
|
|
989
|
+
if (!verdicts.success) {
|
|
990
|
+
ctx.ui.notify(`Unknown verdicts ${value}. Usage: ${USAGE}`, "error");
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
if (canWrite(ctx)) {
|
|
994
|
+
applyPolicy({ verdicts: verdicts.data }, ctx);
|
|
995
|
+
}
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
if (head === ON_ERROR_FORM && value !== undefined) {
|
|
999
|
+
const onError = onErrorSchema.safeParse(value);
|
|
1000
|
+
if (!onError.success) {
|
|
1001
|
+
ctx.ui.notify(`Unknown onError ${value}. Usage: ${USAGE}`, "error");
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
383
1004
|
if (canWrite(ctx)) {
|
|
384
|
-
|
|
1005
|
+
applyPolicy({ onError: onError.data }, ctx);
|
|
385
1006
|
}
|
|
386
1007
|
return;
|
|
387
1008
|
}
|
|
388
|
-
const pair = parseJudgePair(
|
|
389
|
-
if (
|
|
390
|
-
ctx
|
|
1009
|
+
const pair = value === undefined ? undefined : parseJudgePair(value);
|
|
1010
|
+
if (head === LLM_FORM && pair) {
|
|
1011
|
+
if (canWrite(ctx)) {
|
|
1012
|
+
applyPair(pair, ctx);
|
|
1013
|
+
}
|
|
391
1014
|
return;
|
|
392
1015
|
}
|
|
393
|
-
if (
|
|
394
|
-
|
|
1016
|
+
if (head === QUICK_FORM && pair) {
|
|
1017
|
+
if (canWrite(ctx)) {
|
|
1018
|
+
applyQuickPair(pair, ctx);
|
|
1019
|
+
}
|
|
1020
|
+
return;
|
|
395
1021
|
}
|
|
1022
|
+
ctx.ui.notify(`Unknown form ${text}. Usage: ${USAGE}`, "error");
|
|
396
1023
|
}
|
|
397
1024
|
|
|
398
1025
|
return {
|
|
399
|
-
description: "Choose
|
|
1026
|
+
description: "Choose who judges permission asks and each judge's model",
|
|
400
1027
|
getArgumentCompletions,
|
|
401
1028
|
handler,
|
|
402
1029
|
};
|
|
403
1030
|
}
|
|
1031
|
+
|
|
1032
|
+
/** Picker items for fixed values, the current one labelled `(current)`. */
|
|
1033
|
+
function markCurrent(
|
|
1034
|
+
items: readonly { value: string; description: string }[],
|
|
1035
|
+
current: string | undefined,
|
|
1036
|
+
): SelectItem[] {
|
|
1037
|
+
return items.map(({ value, description }) => ({
|
|
1038
|
+
value,
|
|
1039
|
+
label: value === current ? `${value} (current)` : value,
|
|
1040
|
+
description,
|
|
1041
|
+
}));
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/** Say that a pick of the unavailable current row changed nothing. */
|
|
1045
|
+
function notifyNotAvailable(label: string, ctx: CommandContextLike): void {
|
|
1046
|
+
ctx.ui.notify(`${label} is not available. Nothing changed.`, "info");
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Model list items with the current value labelled `(current)`. A current
|
|
1051
|
+
* value the list does not hold (the configured model is not available) goes
|
|
1052
|
+
* first as `(current, not available)`, so the preselected row is never one
|
|
1053
|
+
* that changes the judge.
|
|
1054
|
+
*/
|
|
1055
|
+
function withCurrent(items: SelectItem[], current: string | undefined): SelectItem[] {
|
|
1056
|
+
if (current === undefined) {
|
|
1057
|
+
return items;
|
|
1058
|
+
}
|
|
1059
|
+
if (!items.some((item) => item.value === current)) {
|
|
1060
|
+
return [{ value: current, label: `${current} (current, not available)` }, ...items];
|
|
1061
|
+
}
|
|
1062
|
+
return items.map((item) =>
|
|
1063
|
+
item.value === current ? { ...item, label: `${item.label} (current)` } : item,
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/** `text` ending in exactly one period, whether or not it had one. */
|
|
1068
|
+
function withPeriod(text: string): string {
|
|
1069
|
+
return text.endsWith(".") ? text : `${text}.`;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/** One sentence naming what a selection set. */
|
|
1073
|
+
function describeSelection(selection: JudgeSelection): string {
|
|
1074
|
+
const parts: string[] = [];
|
|
1075
|
+
if (selection.mode !== undefined) {
|
|
1076
|
+
parts.push(`mode ${selection.mode}`);
|
|
1077
|
+
}
|
|
1078
|
+
if (selection.quick !== undefined) {
|
|
1079
|
+
parts.push(`quick judge ${selection.quick.provider}/${selection.quick.model}`);
|
|
1080
|
+
}
|
|
1081
|
+
if (selection.llm === null) {
|
|
1082
|
+
parts.push(`LLM judge follows the ${CHAT_MODEL.replace("-", " ")}`);
|
|
1083
|
+
} else if (selection.llm !== undefined) {
|
|
1084
|
+
parts.push(`LLM judge ${selection.llm.provider}/${selection.llm.model}`);
|
|
1085
|
+
}
|
|
1086
|
+
const policy = selection.policy;
|
|
1087
|
+
if (policy?.verdicts !== undefined) {
|
|
1088
|
+
parts.push(`${JUDGE_NAMES[policy.judge]} verdicts ${policy.verdicts}`);
|
|
1089
|
+
}
|
|
1090
|
+
if (policy?.onError !== undefined) {
|
|
1091
|
+
parts.push(`${JUDGE_NAMES[policy.judge]} onError ${policy.onError}`);
|
|
1092
|
+
}
|
|
1093
|
+
const timeout = selection.timeout;
|
|
1094
|
+
if (timeout !== undefined) {
|
|
1095
|
+
parts.push(
|
|
1096
|
+
timeout.seconds === 0
|
|
1097
|
+
? `${JUDGE_NAMES[timeout.judge]} model-call timeout cleared (built-in ${formatTimeoutSeconds(undefined)} s)`
|
|
1098
|
+
: `${JUDGE_NAMES[timeout.judge]} model-call timeout ${timeout.seconds} s`,
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
return `Permission judge: ${parts.join(", ")}`;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* The fields a write set that the merged config does not show: the project
|
|
1106
|
+
* layer sets its own value and shadows the global one.
|
|
1107
|
+
*/
|
|
1108
|
+
function shadowedFields(selection: JudgeSelection, config: ClassifierConfig): string[] {
|
|
1109
|
+
const shadowed: string[] = [];
|
|
1110
|
+
if (selection.mode !== undefined && config.mode !== selection.mode) {
|
|
1111
|
+
shadowed.push("mode");
|
|
1112
|
+
}
|
|
1113
|
+
const llmShadowed =
|
|
1114
|
+
selection.llm === null
|
|
1115
|
+
? config.provider !== undefined || config.model !== undefined
|
|
1116
|
+
: selection.llm !== undefined &&
|
|
1117
|
+
(config.provider !== selection.llm.provider || config.model !== selection.llm.model);
|
|
1118
|
+
if (llmShadowed) {
|
|
1119
|
+
shadowed.push("provider/model");
|
|
1120
|
+
}
|
|
1121
|
+
if (
|
|
1122
|
+
selection.quick !== undefined &&
|
|
1123
|
+
(config.quickJudge?.provider !== selection.quick.provider ||
|
|
1124
|
+
config.quickJudge?.model !== selection.quick.model)
|
|
1125
|
+
) {
|
|
1126
|
+
shadowed.push("quickJudge");
|
|
1127
|
+
}
|
|
1128
|
+
const timeout = selection.timeout;
|
|
1129
|
+
if (timeout !== undefined) {
|
|
1130
|
+
const merged = timeoutOf(config, timeout.judge);
|
|
1131
|
+
const writes = timeout.seconds === 0 ? DEFAULT_TIMEOUT_MS : timeout.seconds * 1000;
|
|
1132
|
+
// A cleared key reads back as the built-in default, so a merged value of
|
|
1133
|
+
// exactly that default is not the project layer shadowing the clear.
|
|
1134
|
+
if (merged !== undefined && merged !== writes) {
|
|
1135
|
+
shadowed.push(
|
|
1136
|
+
timeout.judge === "quick" ? "quickJudge.timeoutMs" : "timeoutMs",
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
const policy = selection.policy;
|
|
1141
|
+
if (policy !== undefined) {
|
|
1142
|
+
const merged = policyOf(config, policy.judge);
|
|
1143
|
+
const prefix = policy.judge === "quick" ? "quickJudge." : "";
|
|
1144
|
+
if (policy.verdicts !== undefined && merged.verdicts !== policy.verdicts) {
|
|
1145
|
+
shadowed.push(`${prefix}verdicts`);
|
|
1146
|
+
}
|
|
1147
|
+
if (policy.onError !== undefined && merged.onError !== policy.onError) {
|
|
1148
|
+
shadowed.push(`${prefix}onError`);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return shadowed;
|
|
1152
|
+
}
|