pi-advisor-flow 0.13.1 → 0.13.2
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 +6 -0
- package/dist/index.js +14 -7
- package/extensions/index.ts +6 -0
- package/package.json +9 -9
- package/scripts/check-bun-version.mjs +9 -0
- package/src/attachments.ts +36 -0
- package/src/child-session.ts +1 -0
- package/src/commands/activation-preparation.ts +12 -0
- package/src/commands/activation.ts +43 -0
- package/src/commands/advisor-tools.ts +3 -0
- package/src/commands/lifecycle.ts +15 -0
- package/src/commands/manual-command.ts +19 -0
- package/src/commands/manual-consultation.ts +20 -0
- package/src/commands/manual-progress.ts +13 -0
- package/src/commands/model-commands.ts +4 -0
- package/src/commands/model-options.ts +18 -0
- package/src/commands/model-picker.ts +16 -0
- package/src/commands/outcome-stats.ts +9 -0
- package/src/commands/registration.ts +1 -0
- package/src/commands/renderers.ts +14 -0
- package/src/commands/runtime.ts +5 -0
- package/src/commands/settings-commands.ts +18 -0
- package/src/commands/settings-persistence.ts +2 -0
- package/src/commands.ts +1 -0
- package/src/config/args.ts +7 -0
- package/src/config/defaults.ts +1 -0
- package/src/config/schema.ts +1 -0
- package/src/config/state.ts +64 -1
- package/src/config/storage.ts +37 -0
- package/src/config/types.ts +9 -0
- package/src/config/validation.ts +7 -0
- package/src/config.ts +6 -0
- package/src/content-utils.ts +5 -0
- package/src/conversation.ts +39 -0
- package/src/follow-up.ts +8 -0
- package/src/git.ts +8 -0
- package/src/herdr-block.ts +7 -0
- package/src/herdr-shared.ts +23 -0
- package/src/herdr.ts +18 -0
- package/src/image-attachments.ts +24 -0
- package/src/image-validation.ts +58 -0
- package/src/images.ts +24 -0
- package/src/jev/base-url.ts +14 -0
- package/src/jev/client.ts +49 -0
- package/src/jev/decisions-client.ts +14 -0
- package/src/jev/decisions-response.ts +38 -0
- package/src/jev/endpoint-key-store.ts +18 -0
- package/src/jev/key-store.ts +28 -0
- package/src/jev/ledger.ts +30 -12
- package/src/jev/openai-key-store.ts +17 -0
- package/src/jev/questions.ts +15 -0
- package/src/jev/state.ts +5 -0
- package/src/jev/transport.ts +35 -0
- package/src/model-stream.ts +32 -0
- package/src/outcome-stats.ts +16 -0
- package/src/outcomes.ts +31 -0
- package/src/pi-settings.ts +9 -0
- package/src/preferences.ts +26 -0
- package/src/redaction.ts +8 -0
- package/src/scout-context.ts +17 -0
- package/src/scout-curation.ts +12 -0
- package/src/scout-groups.ts +47 -0
- package/src/scout-protocol.ts +17 -0
- package/src/scout-reconstruct.ts +12 -0
- package/src/scout.ts +47 -0
- package/src/session-context.ts +11 -0
- package/src/session-state.ts +73 -13
- package/src/tool-result-cap.ts +11 -0
- package/src/tools/consult-context.ts +24 -0
- package/src/tools/consultation.ts +43 -0
- package/src/tools/gate-policy.ts +18 -0
- package/src/tools/gate-protocol.ts +22 -0
- package/src/tools/jev-filter.ts +26 -0
- package/src/tools/jev-turn-gate.ts +17 -0
- package/src/tools/loop-gate.ts +25 -0
- package/src/tools/model-access.ts +12 -0
- package/src/tools/outage-notifier.ts +4 -0
- package/src/tools/prompts.ts +17 -0
- package/src/tools/register-ask-advisor.ts +44 -0
- package/src/tools/register-lifecycle.ts +32 -0
- package/src/tools/register-outcome.ts +7 -0
- package/src/tools/register-renderers.ts +14 -0
- package/src/tools/registration.ts +3 -0
- package/src/tools/render-advisor-result.ts +44 -0
- package/src/tools/render-common.ts +10 -0
- package/src/tools/scout-status.ts +34 -0
- package/src/tools/session.ts +3 -0
- package/src/tools.ts +8 -0
- package/src/ui/jev-filter-submenu.ts +21 -0
- package/src/ui/jev-provider-submenu-view.ts +22 -0
- package/src/ui/jev-provider-submenu.ts +81 -0
- package/src/ui/jev-setup-support.ts +26 -0
- package/src/ui/manual-dialog-render.ts +20 -0
- package/src/ui/manual-dialog.ts +38 -0
- package/src/ui/masked-input.ts +2 -0
- package/src/ui/searchable-model-list.ts +28 -0
- package/src/ui/settings-formatting.ts +26 -0
- package/src/ui/settings-items.ts +11 -0
- package/src/ui/settings-list-adapter.ts +13 -0
- package/src/ui/settings-mutations.ts +60 -0
- package/src/ui/settings-selector.ts +30 -0
- package/src/ui/text-setting-submenu.ts +9 -0
- package/src/ui-guard.ts +1 -0
- package/src/ui.ts +3 -0
- package/src/usage.ts +31 -0
package/src/scout.ts
CHANGED
|
@@ -104,6 +104,7 @@ const defaultDependencies: ScoutDependencies = {
|
|
|
104
104
|
collect: collectTextStream,
|
|
105
105
|
resolve: resolveConfiguredModel,
|
|
106
106
|
};
|
|
107
|
+
|
|
107
108
|
const byteLength = (value: string) => Buffer.byteLength(value, "utf-8");
|
|
108
109
|
const AUTH_ERROR_PATTERN = /api key|auth|login|credential/iu;
|
|
109
110
|
|
|
@@ -134,16 +135,21 @@ export const parseScoutSelection = (
|
|
|
134
135
|
if (!text.trim()) {
|
|
135
136
|
throw new Error("Scout returned an empty response.");
|
|
136
137
|
}
|
|
138
|
+
|
|
137
139
|
let value: unknown;
|
|
140
|
+
|
|
138
141
|
try {
|
|
139
142
|
value = JSON.parse(text);
|
|
140
143
|
} catch (error) {
|
|
141
144
|
throw new Error("Scout response is not a JSON object.", { cause: error });
|
|
142
145
|
}
|
|
146
|
+
|
|
143
147
|
if (!isRecord(value)) {
|
|
144
148
|
throw new Error("Scout response must be a JSON object.");
|
|
145
149
|
}
|
|
150
|
+
|
|
146
151
|
const keys = Object.keys(value).toSorted();
|
|
152
|
+
|
|
147
153
|
if (
|
|
148
154
|
keys.length !== 2 ||
|
|
149
155
|
keys[0] !== "selectedIds" ||
|
|
@@ -153,39 +159,52 @@ export const parseScoutSelection = (
|
|
|
153
159
|
"Scout response must contain only selectedIds and synthesis."
|
|
154
160
|
);
|
|
155
161
|
}
|
|
162
|
+
|
|
156
163
|
if (!isStringArray(value.selectedIds)) {
|
|
157
164
|
throw new Error("Scout selectedIds must be an array of strings.");
|
|
158
165
|
}
|
|
166
|
+
|
|
159
167
|
const { selectedIds } = value;
|
|
168
|
+
|
|
160
169
|
if (new Set(selectedIds).size !== selectedIds.length) {
|
|
161
170
|
throw new Error("Scout selected duplicate group IDs.");
|
|
162
171
|
}
|
|
172
|
+
|
|
163
173
|
const known = new Set(manifest.groups.map((group) => group.id));
|
|
164
174
|
const knownSelectedIds = selectedIds.filter((id) => known.has(id));
|
|
175
|
+
|
|
165
176
|
const requiredIds = manifest.groups
|
|
166
177
|
.filter((group) => group.required)
|
|
167
178
|
.map((group) => group.id);
|
|
179
|
+
|
|
168
180
|
if (requiredIds.length > SCOUT_SELECTION_MAX_IDS) {
|
|
169
181
|
throw new Error(
|
|
170
182
|
`Manifest contains more than ${SCOUT_SELECTION_MAX_IDS} required groups.`
|
|
171
183
|
);
|
|
172
184
|
}
|
|
185
|
+
|
|
173
186
|
const required = new Set(requiredIds);
|
|
187
|
+
|
|
174
188
|
const optionalIds = knownSelectedIds
|
|
175
189
|
.filter((id) => !required.has(id))
|
|
176
190
|
.slice(0, SCOUT_SELECTION_MAX_IDS - requiredIds.length);
|
|
191
|
+
|
|
177
192
|
const retained = new Set([...requiredIds, ...optionalIds]);
|
|
193
|
+
|
|
178
194
|
const normalizedIds = manifest.groups
|
|
179
195
|
.filter((group) => retained.has(group.id))
|
|
180
196
|
.map((group) => group.id);
|
|
197
|
+
|
|
181
198
|
if (!isString(value.synthesis)) {
|
|
182
199
|
throw new TypeError("Scout synthesis must be a string.");
|
|
183
200
|
}
|
|
201
|
+
|
|
184
202
|
if (byteLength(value.synthesis) > SCOUT_SYNTHESIS_MAX_BYTES) {
|
|
185
203
|
throw new Error(
|
|
186
204
|
`Scout synthesis exceeds ${SCOUT_SYNTHESIS_MAX_BYTES} UTF-8 bytes.`
|
|
187
205
|
);
|
|
188
206
|
}
|
|
207
|
+
|
|
189
208
|
return {
|
|
190
209
|
selectedIds: normalizedIds,
|
|
191
210
|
synthesis: knownSelectedIds.length > 0 ? value.synthesis : "",
|
|
@@ -207,9 +226,11 @@ const classifyResolutionError = (message: string): ScoutFallbackCategory => {
|
|
|
207
226
|
if (message.startsWith("Scout model not found:")) {
|
|
208
227
|
return "missing-model";
|
|
209
228
|
}
|
|
229
|
+
|
|
210
230
|
if (AUTH_ERROR_PATTERN.test(message)) {
|
|
211
231
|
return "auth-error";
|
|
212
232
|
}
|
|
233
|
+
|
|
213
234
|
return "provider-error";
|
|
214
235
|
};
|
|
215
236
|
|
|
@@ -221,18 +242,24 @@ const setupAbortWatch = (
|
|
|
221
242
|
let timedOut = false;
|
|
222
243
|
const abortFromParent = () => controller.abort(parentSignal?.reason);
|
|
223
244
|
parentSignal?.addEventListener("abort", abortFromParent, { once: true });
|
|
245
|
+
|
|
224
246
|
const timer = setTimeout(() => {
|
|
225
247
|
timedOut = true;
|
|
226
248
|
controller.abort(new Error("Scout timed out."));
|
|
227
249
|
}, timeoutMs);
|
|
250
|
+
|
|
228
251
|
timer.unref?.();
|
|
252
|
+
|
|
229
253
|
const { promise: abortPromise, reject: rejectOnAbort } =
|
|
230
254
|
Promise.withResolvers<never>();
|
|
255
|
+
|
|
231
256
|
const onControllerAbort = () =>
|
|
232
257
|
rejectOnAbort(controller.signal.reason ?? new Error("Scout aborted."));
|
|
258
|
+
|
|
233
259
|
controller.signal.addEventListener("abort", onControllerAbort, {
|
|
234
260
|
once: true,
|
|
235
261
|
});
|
|
262
|
+
|
|
236
263
|
return {
|
|
237
264
|
abortPromise,
|
|
238
265
|
controller,
|
|
@@ -263,6 +290,7 @@ const streamScoutResponse = async (
|
|
|
263
290
|
parentSignal,
|
|
264
291
|
timeoutMs
|
|
265
292
|
);
|
|
293
|
+
|
|
266
294
|
try {
|
|
267
295
|
const collection = dependencies.collect(resolved, {
|
|
268
296
|
messages: [manifestMessage(manifest)],
|
|
@@ -275,12 +303,14 @@ const streamScoutResponse = async (
|
|
|
275
303
|
signal: controller.signal,
|
|
276
304
|
systemPrompt: SCOUT_SYSTEM,
|
|
277
305
|
});
|
|
306
|
+
|
|
278
307
|
return {
|
|
279
308
|
ok: true,
|
|
280
309
|
streamed: await Promise.race([collection, abortPromise]),
|
|
281
310
|
};
|
|
282
311
|
} catch (error) {
|
|
283
312
|
const message = error instanceof Error ? error.message : String(error);
|
|
313
|
+
|
|
284
314
|
return wasTimedOut()
|
|
285
315
|
? {
|
|
286
316
|
category: "timeout",
|
|
@@ -302,13 +332,17 @@ export const runAdvisorScout = async (
|
|
|
302
332
|
dependencies: ScoutDependencies = defaultDependencies
|
|
303
333
|
): Promise<ScoutOutcome> => {
|
|
304
334
|
const startedAt = Date.now();
|
|
335
|
+
|
|
305
336
|
const publish = (event: ScoutLifecycleEvent) => {
|
|
306
337
|
onEvent?.(event);
|
|
307
338
|
};
|
|
339
|
+
|
|
308
340
|
const cancelled = (): ScoutOutcome => {
|
|
309
341
|
publish({ type: "cancelled" });
|
|
342
|
+
|
|
310
343
|
return { cancelled: true, ok: false };
|
|
311
344
|
};
|
|
345
|
+
|
|
312
346
|
const fallback = (
|
|
313
347
|
category: ScoutFallbackCategory,
|
|
314
348
|
message: string,
|
|
@@ -324,7 +358,9 @@ export const runAdvisorScout = async (
|
|
|
324
358
|
model: effectiveExecutorRef(ctx),
|
|
325
359
|
ok: false as const,
|
|
326
360
|
};
|
|
361
|
+
|
|
327
362
|
publish({ outcome, type: "fallback" });
|
|
363
|
+
|
|
328
364
|
return outcome;
|
|
329
365
|
};
|
|
330
366
|
|
|
@@ -335,19 +371,23 @@ export const runAdvisorScout = async (
|
|
|
335
371
|
const executorModel = effectiveExecutorRef(ctx);
|
|
336
372
|
const executorEffort = effectiveExecutorEffort(ctx);
|
|
337
373
|
let resolved: ResolvedConfiguredModel;
|
|
374
|
+
|
|
338
375
|
try {
|
|
339
376
|
resolved = await dependencies.resolve(ctx, executorModel, "Scout");
|
|
340
377
|
} catch (error) {
|
|
341
378
|
if (parentSignal?.aborted) {
|
|
342
379
|
return cancelled();
|
|
343
380
|
}
|
|
381
|
+
|
|
344
382
|
const message = error instanceof Error ? error.message : String(error);
|
|
383
|
+
|
|
345
384
|
return fallback(classifyResolutionError(message), message);
|
|
346
385
|
}
|
|
347
386
|
|
|
348
387
|
if (parentSignal?.aborted) {
|
|
349
388
|
return cancelled();
|
|
350
389
|
}
|
|
390
|
+
|
|
351
391
|
publish({ model: executorModel, type: "call" });
|
|
352
392
|
|
|
353
393
|
const streamed = await streamScoutResponse(
|
|
@@ -360,21 +400,26 @@ export const runAdvisorScout = async (
|
|
|
360
400
|
timeoutMs,
|
|
361
401
|
publish
|
|
362
402
|
);
|
|
403
|
+
|
|
363
404
|
if (!streamed.ok) {
|
|
364
405
|
if (parentSignal?.aborted) {
|
|
365
406
|
return cancelled();
|
|
366
407
|
}
|
|
408
|
+
|
|
367
409
|
return fallback(streamed.category, streamed.message);
|
|
368
410
|
}
|
|
411
|
+
|
|
369
412
|
if (parentSignal?.aborted) {
|
|
370
413
|
return cancelled();
|
|
371
414
|
}
|
|
372
415
|
|
|
373
416
|
let selection: ScoutSelection;
|
|
417
|
+
|
|
374
418
|
try {
|
|
375
419
|
selection = parseScoutSelection(streamed.streamed.text, manifest);
|
|
376
420
|
} catch (error) {
|
|
377
421
|
const message = error instanceof Error ? error.message : String(error);
|
|
422
|
+
|
|
378
423
|
return fallback(
|
|
379
424
|
streamed.streamed.text.trim() ? "invalid-selection" : "empty-response",
|
|
380
425
|
message,
|
|
@@ -407,6 +452,8 @@ export const runAdvisorScout = async (
|
|
|
407
452
|
.map((group) => group.label),
|
|
408
453
|
selection,
|
|
409
454
|
};
|
|
455
|
+
|
|
410
456
|
publish({ outcome, type: "success" });
|
|
457
|
+
|
|
411
458
|
return outcome;
|
|
412
459
|
};
|
package/src/session-context.ts
CHANGED
|
@@ -11,25 +11,34 @@ const projectBranch = (source: SessionEntry[]): SessionEntry[] => {
|
|
|
11
11
|
const resetBoundaryIndex = source.findLastIndex((entry) =>
|
|
12
12
|
Object.is(entry.type, "reset_boundary")
|
|
13
13
|
);
|
|
14
|
+
|
|
14
15
|
const branch = source.slice(resetBoundaryIndex + 1);
|
|
16
|
+
|
|
15
17
|
const compactionIndex = branch.findLastIndex(
|
|
16
18
|
(entry) => entry.type === "compaction"
|
|
17
19
|
);
|
|
20
|
+
|
|
18
21
|
if (compactionIndex === -1) {
|
|
19
22
|
return branch;
|
|
20
23
|
}
|
|
24
|
+
|
|
21
25
|
const compaction = branch[compactionIndex];
|
|
26
|
+
|
|
22
27
|
if (compaction.type !== "compaction") {
|
|
23
28
|
return branch;
|
|
24
29
|
}
|
|
30
|
+
|
|
25
31
|
const firstKeptIndex = branch.findIndex(
|
|
26
32
|
(entry, index) =>
|
|
27
33
|
index < compactionIndex && entry.id === compaction.firstKeptEntryId
|
|
28
34
|
);
|
|
35
|
+
|
|
29
36
|
const projected: SessionEntry[] = [compaction];
|
|
37
|
+
|
|
30
38
|
if (firstKeptIndex !== -1) {
|
|
31
39
|
for (let index = firstKeptIndex; index < compactionIndex; index += 1) {
|
|
32
40
|
const entry = branch[index];
|
|
41
|
+
|
|
33
42
|
if (
|
|
34
43
|
entry.type !== "compaction" &&
|
|
35
44
|
!(entry.type === "message" && entry.message.role === "system")
|
|
@@ -38,6 +47,7 @@ const projectBranch = (source: SessionEntry[]): SessionEntry[] => {
|
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
}
|
|
50
|
+
|
|
41
51
|
return [...projected, ...branch.slice(compactionIndex + 1)];
|
|
42
52
|
};
|
|
43
53
|
|
|
@@ -46,6 +56,7 @@ export const sessionContextEntries = (
|
|
|
46
56
|
): SessionEntry[] => {
|
|
47
57
|
// SAFETY: older hosts lack projection; retain reset and compaction boundaries before disclosure.
|
|
48
58
|
const sessionManager = ctx.sessionManager as SessionManagerWithContextEntries;
|
|
59
|
+
|
|
49
60
|
return sessionManager.buildContextEntries
|
|
50
61
|
? sessionManager.buildContextEntries()
|
|
51
62
|
: projectBranch(sessionManager.getBranch());
|
package/src/session-state.ts
CHANGED
|
@@ -16,12 +16,16 @@ import {
|
|
|
16
16
|
import type { AdvisorUsageTotals } from "./usage.ts";
|
|
17
17
|
|
|
18
18
|
export type GateDecision = "proceed" | "revise" | "blocked";
|
|
19
|
+
|
|
19
20
|
export type ConsultationTrigger = "manual" | "executor-requested" | "turn-gate";
|
|
21
|
+
|
|
20
22
|
export type GateTrigger =
|
|
21
23
|
| "repeated-tool-call"
|
|
22
24
|
| "completion-review"
|
|
23
25
|
| "custom-rule";
|
|
26
|
+
|
|
24
27
|
type AdvisorTrigger = ConsultationTrigger | GateTrigger;
|
|
28
|
+
|
|
25
29
|
type ExecutionEffect = "continued" | "tool-blocked" | "session-blocked";
|
|
26
30
|
|
|
27
31
|
export interface AdvisorInvocationRecord {
|
|
@@ -37,6 +41,7 @@ export interface AdvisorInvocationRecord {
|
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
const WHITESPACE = /\s/u;
|
|
44
|
+
|
|
40
45
|
const TIMESTAMP_KEYS = new Set([
|
|
41
46
|
"createdat",
|
|
42
47
|
"date",
|
|
@@ -45,9 +50,12 @@ const TIMESTAMP_KEYS = new Set([
|
|
|
45
50
|
"timestamp",
|
|
46
51
|
"updatedat",
|
|
47
52
|
]);
|
|
53
|
+
|
|
48
54
|
const REQUEST_ID_KEYS = new Set(["correlationid", "requestid", "traceid"]);
|
|
55
|
+
|
|
49
56
|
const normalizedKey = (key: string) =>
|
|
50
57
|
key.replaceAll(/[-_]/gu, "").toLowerCase();
|
|
58
|
+
|
|
51
59
|
const isVolatileKey = (key: string, keys: Set<string>) =>
|
|
52
60
|
keys.has(normalizedKey(key));
|
|
53
61
|
|
|
@@ -55,18 +63,23 @@ const normalizeShellWhitespace = (command: string) => {
|
|
|
55
63
|
let result = "";
|
|
56
64
|
let quote: "'" | '"' | "`" | undefined;
|
|
57
65
|
let pendingSpace = false;
|
|
66
|
+
|
|
58
67
|
for (const char of command.trim()) {
|
|
59
68
|
if (quote) {
|
|
60
69
|
result += char;
|
|
70
|
+
|
|
61
71
|
if (char === quote) {
|
|
62
72
|
quote = undefined;
|
|
63
73
|
}
|
|
74
|
+
|
|
64
75
|
continue;
|
|
65
76
|
}
|
|
77
|
+
|
|
66
78
|
if (char === "'" || char === '"' || char === "`") {
|
|
67
79
|
if (pendingSpace && result) {
|
|
68
80
|
result += " ";
|
|
69
81
|
}
|
|
82
|
+
|
|
70
83
|
pendingSpace = false;
|
|
71
84
|
quote = char;
|
|
72
85
|
result += char;
|
|
@@ -76,10 +89,12 @@ const normalizeShellWhitespace = (command: string) => {
|
|
|
76
89
|
if (pendingSpace && result) {
|
|
77
90
|
result += " ";
|
|
78
91
|
}
|
|
92
|
+
|
|
79
93
|
pendingSpace = false;
|
|
80
94
|
result += char;
|
|
81
95
|
}
|
|
82
96
|
}
|
|
97
|
+
|
|
83
98
|
return result;
|
|
84
99
|
};
|
|
85
100
|
|
|
@@ -106,17 +121,22 @@ export const normalizeToolInput = <Input>(
|
|
|
106
121
|
if (key && isVolatileKey(key, TIMESTAMP_KEYS)) {
|
|
107
122
|
return "<timestamp>";
|
|
108
123
|
}
|
|
124
|
+
|
|
109
125
|
if (key && isVolatileKey(key, REQUEST_ID_KEYS)) {
|
|
110
126
|
return "<request-id>";
|
|
111
127
|
}
|
|
128
|
+
|
|
112
129
|
const normalized = normalizeString(value);
|
|
130
|
+
|
|
113
131
|
return toolName === "bash" && key === "command"
|
|
114
132
|
? normalizeShellWhitespace(normalized)
|
|
115
133
|
: normalized;
|
|
116
134
|
}
|
|
135
|
+
|
|
117
136
|
if (Array.isArray(value)) {
|
|
118
137
|
return value.map((item) => visit(item));
|
|
119
138
|
}
|
|
139
|
+
|
|
120
140
|
if (isRecordOf(value)) {
|
|
121
141
|
return Object.fromEntries(
|
|
122
142
|
Object.keys(value)
|
|
@@ -124,8 +144,10 @@ export const normalizeToolInput = <Input>(
|
|
|
124
144
|
.map((childKey) => [childKey, visit(value[childKey], childKey)])
|
|
125
145
|
);
|
|
126
146
|
}
|
|
147
|
+
|
|
127
148
|
return value;
|
|
128
149
|
};
|
|
150
|
+
|
|
129
151
|
// SAFETY: Pi tool inputs are JSON-decoded tool arguments, so the root value is already NormalizedToolInput-shaped.
|
|
130
152
|
return visit(input as Input & NormalizedToolInput);
|
|
131
153
|
};
|
|
@@ -165,6 +187,7 @@ const freshRepetition = (): RepetitionState => ({
|
|
|
165
187
|
count: 0,
|
|
166
188
|
interventions: 0,
|
|
167
189
|
});
|
|
190
|
+
|
|
168
191
|
const freshAdviceLedger = (): AdviceLedger => ({
|
|
169
192
|
draftConsultations: 0,
|
|
170
193
|
issued: new Map(),
|
|
@@ -172,6 +195,7 @@ const freshAdviceLedger = (): AdviceLedger => ({
|
|
|
172
195
|
pending: new Set(),
|
|
173
196
|
reported: new Set(),
|
|
174
197
|
});
|
|
198
|
+
|
|
175
199
|
const freshUsage = (): UsageState => ({
|
|
176
200
|
invocations: [],
|
|
177
201
|
totals: emptyAdvisorUsageTotals(),
|
|
@@ -228,16 +252,20 @@ export class AdvisorSessionState {
|
|
|
228
252
|
if (toolName === "ask_advisor") {
|
|
229
253
|
return false;
|
|
230
254
|
}
|
|
255
|
+
|
|
231
256
|
const signature = normalizedToolSignature(toolName, input);
|
|
232
257
|
this.#repetition.count =
|
|
233
258
|
signature === this.#repetition.previousSignature
|
|
234
259
|
? this.#repetition.count + 1
|
|
235
260
|
: 1;
|
|
236
261
|
this.#repetition.previousSignature = signature;
|
|
262
|
+
|
|
237
263
|
if (this.#repetition.count < threshold) {
|
|
238
264
|
return false;
|
|
239
265
|
}
|
|
266
|
+
|
|
240
267
|
this.#repetition.interventions += 1;
|
|
268
|
+
|
|
241
269
|
return true;
|
|
242
270
|
}
|
|
243
271
|
|
|
@@ -247,16 +275,20 @@ export class AdvisorSessionState {
|
|
|
247
275
|
Number(
|
|
248
276
|
reservationId !== undefined && this.#callReservations.has(reservationId)
|
|
249
277
|
);
|
|
278
|
+
|
|
250
279
|
return limit === undefined || this.#consumedCalls + reservations < limit;
|
|
251
280
|
}
|
|
252
281
|
reserveCall(id: string, limit: number | undefined) {
|
|
253
282
|
if (this.#callReservations.has(id)) {
|
|
254
283
|
return true;
|
|
255
284
|
}
|
|
285
|
+
|
|
256
286
|
if (!this.canConsult(limit)) {
|
|
257
287
|
return false;
|
|
258
288
|
}
|
|
289
|
+
|
|
259
290
|
this.#callReservations.add(id);
|
|
291
|
+
|
|
260
292
|
return true;
|
|
261
293
|
}
|
|
262
294
|
releaseCall(id: string) {
|
|
@@ -269,6 +301,7 @@ export class AdvisorSessionState {
|
|
|
269
301
|
if (reservationId !== undefined) {
|
|
270
302
|
this.#callReservations.delete(reservationId);
|
|
271
303
|
}
|
|
304
|
+
|
|
272
305
|
this.#consumedCalls += 1;
|
|
273
306
|
}
|
|
274
307
|
remainingCalls(limit: number | undefined) {
|
|
@@ -342,11 +375,14 @@ export class AdvisorSessionState {
|
|
|
342
375
|
normalizedQuestion?: string
|
|
343
376
|
) {
|
|
344
377
|
const issued: IssuedAdvice = { advice, trigger };
|
|
378
|
+
|
|
345
379
|
if (normalizedQuestion) {
|
|
346
380
|
issued.normalizedQuestion = normalizedQuestion;
|
|
347
381
|
}
|
|
382
|
+
|
|
348
383
|
this.#ledger.issued.set(id, issued);
|
|
349
384
|
this.#ledger.lastAdvice = advice;
|
|
385
|
+
|
|
350
386
|
if (draft) {
|
|
351
387
|
this.#ledger.draftConsultations += 1;
|
|
352
388
|
}
|
|
@@ -359,29 +395,38 @@ export class AdvisorSessionState {
|
|
|
359
395
|
if (!normalizedQuestion) {
|
|
360
396
|
return undefined;
|
|
361
397
|
}
|
|
398
|
+
|
|
362
399
|
for (const entry of this.#ledger.issued.values()) {
|
|
363
400
|
if (entry.normalizedQuestion === normalizedQuestion) {
|
|
364
401
|
return entry.advice;
|
|
365
402
|
}
|
|
366
403
|
}
|
|
404
|
+
|
|
367
405
|
return undefined;
|
|
368
406
|
}
|
|
369
407
|
claimTrackedFiles(paths: string[]) {
|
|
370
408
|
const advice = this.#ledger.lastAdvice;
|
|
409
|
+
|
|
371
410
|
if (!advice || paths.length === 0) {
|
|
372
411
|
return false;
|
|
373
412
|
}
|
|
413
|
+
|
|
374
414
|
const mentioned = paths.every((path) => {
|
|
375
415
|
const escaped = path.replaceAll(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
416
|
+
|
|
376
417
|
const boundary = `(^|[\\s"'\`()\\[])${
|
|
377
418
|
escaped
|
|
378
419
|
}(?=$|[\\s"'\`),;:!?\\]]|\\.(?=\\s|$))`;
|
|
420
|
+
|
|
379
421
|
return new RegExp(boundary, "u").test(advice);
|
|
380
422
|
});
|
|
423
|
+
|
|
381
424
|
if (!mentioned) {
|
|
382
425
|
return false;
|
|
383
426
|
}
|
|
427
|
+
|
|
384
428
|
this.#ledger.lastAdvice = undefined;
|
|
429
|
+
|
|
385
430
|
return true;
|
|
386
431
|
}
|
|
387
432
|
|
|
@@ -389,11 +434,15 @@ export class AdvisorSessionState {
|
|
|
389
434
|
if (this.#ledger.reported.has(id) || this.#ledger.pending.has(id)) {
|
|
390
435
|
return;
|
|
391
436
|
}
|
|
437
|
+
|
|
392
438
|
const advice = this.#ledger.issued.get(id);
|
|
439
|
+
|
|
393
440
|
if (!advice) {
|
|
394
441
|
return;
|
|
395
442
|
}
|
|
443
|
+
|
|
396
444
|
this.#ledger.pending.add(id);
|
|
445
|
+
|
|
397
446
|
return advice;
|
|
398
447
|
}
|
|
399
448
|
|
|
@@ -401,8 +450,10 @@ export class AdvisorSessionState {
|
|
|
401
450
|
if (!this.#ledger.pending.delete(id)) {
|
|
402
451
|
return false;
|
|
403
452
|
}
|
|
453
|
+
|
|
404
454
|
this.#ledger.reported.add(id);
|
|
405
455
|
this.#ledger.outcomes += 1;
|
|
456
|
+
|
|
406
457
|
return true;
|
|
407
458
|
}
|
|
408
459
|
|
|
@@ -413,10 +464,13 @@ export class AdvisorSessionState {
|
|
|
413
464
|
/** Compatibility helper for synchronous callers that can commit immediately. */
|
|
414
465
|
claimAdvice(id: string) {
|
|
415
466
|
const advice = this.reserveAdvice(id);
|
|
467
|
+
|
|
416
468
|
if (!advice) {
|
|
417
469
|
return;
|
|
418
470
|
}
|
|
471
|
+
|
|
419
472
|
this.commitAdvice(id);
|
|
473
|
+
|
|
420
474
|
return advice;
|
|
421
475
|
}
|
|
422
476
|
|
|
@@ -424,19 +478,18 @@ export class AdvisorSessionState {
|
|
|
424
478
|
const gates = this.#usage.invocations.filter(
|
|
425
479
|
(item) => item.kind === "gate"
|
|
426
480
|
);
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
);
|
|
481
|
+
|
|
482
|
+
const parts: string[] = [];
|
|
483
|
+
|
|
484
|
+
for (const decision of ["proceed", "revise", "blocked"] as const) {
|
|
485
|
+
const count = gates.filter((item) => item.decision === decision).length;
|
|
486
|
+
|
|
487
|
+
if (count > 0) {
|
|
488
|
+
parts.push(`${count} ${decision}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return parts.join(", ") || "none";
|
|
440
493
|
}
|
|
441
494
|
|
|
442
495
|
#countTrigger(trigger: AdvisorTrigger) {
|
|
@@ -497,6 +550,7 @@ export class AdvisorSessionState {
|
|
|
497
550
|
summary(limit: number | undefined) {
|
|
498
551
|
const { invocations, totals } = this.#usage;
|
|
499
552
|
const jevLines = this.#jev.summaryLines(invocations);
|
|
553
|
+
|
|
500
554
|
if (
|
|
501
555
|
invocations.length === 0 &&
|
|
502
556
|
this.#repetition.interventions === 0 &&
|
|
@@ -504,21 +558,27 @@ export class AdvisorSessionState {
|
|
|
504
558
|
) {
|
|
505
559
|
return;
|
|
506
560
|
}
|
|
561
|
+
|
|
507
562
|
const markdown = invocations.filter((item) => item.kind === "markdown");
|
|
508
563
|
const gates = invocations.filter((item) => item.kind === "gate");
|
|
564
|
+
|
|
509
565
|
const effects = (effect: ExecutionEffect) =>
|
|
510
566
|
invocations.filter((item) => item.executionEffect === effect).length;
|
|
567
|
+
|
|
511
568
|
const failures = invocations
|
|
512
569
|
.filter((item) => item.failure)
|
|
513
570
|
.map((item) => item.failure);
|
|
571
|
+
|
|
514
572
|
const models =
|
|
515
573
|
[...new Set(invocations.map((item) => item.model).filter(Boolean))].join(
|
|
516
574
|
", "
|
|
517
575
|
) || "unknown";
|
|
576
|
+
|
|
518
577
|
const budget =
|
|
519
578
|
limit === undefined
|
|
520
579
|
? `${this.#consumedCalls} used; unlimited remaining`
|
|
521
580
|
: `${this.#consumedCalls} / ${limit} used; ${Math.max(0, limit - this.#consumedCalls)} remaining`;
|
|
581
|
+
|
|
522
582
|
return [
|
|
523
583
|
"[Session Advisor Summary]",
|
|
524
584
|
`Consultations: ${markdown.length} Markdown (${this.#countTrigger("manual")} manual, ${this.#countTrigger("executor-requested")} executor-requested), automatic gates: ${gates.length}`,
|
package/src/tool-result-cap.ts
CHANGED
|
@@ -21,14 +21,18 @@ const collect = (
|
|
|
21
21
|
) => {
|
|
22
22
|
const selected: string[] = [];
|
|
23
23
|
let used = 0;
|
|
24
|
+
|
|
24
25
|
for (const line of candidates.slice(0, maxEntries)) {
|
|
25
26
|
const next = used + byteLength(line) + (selected.length ? 1 : 0);
|
|
27
|
+
|
|
26
28
|
if (next > maxContentBytes) {
|
|
27
29
|
break;
|
|
28
30
|
}
|
|
31
|
+
|
|
29
32
|
selected.push(line);
|
|
30
33
|
used = next;
|
|
31
34
|
}
|
|
35
|
+
|
|
32
36
|
return selected;
|
|
33
37
|
};
|
|
34
38
|
|
|
@@ -40,6 +44,7 @@ export const capToolResult = (
|
|
|
40
44
|
const lines = value.split("\n");
|
|
41
45
|
const totalLines = lines.length;
|
|
42
46
|
const totalBytes = byteLength(value);
|
|
47
|
+
|
|
43
48
|
if ((maxLines === 0 || maxBytes === 0) && value.length > 0) {
|
|
44
49
|
return {
|
|
45
50
|
content: "[Tool result omitted: configured limit is zero]",
|
|
@@ -49,6 +54,7 @@ export const capToolResult = (
|
|
|
49
54
|
truncated: true,
|
|
50
55
|
};
|
|
51
56
|
}
|
|
57
|
+
|
|
52
58
|
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
53
59
|
return {
|
|
54
60
|
content: value,
|
|
@@ -60,6 +66,7 @@ export const capToolResult = (
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
const markerBytes = byteLength(OMITTED_MARKER);
|
|
69
|
+
|
|
63
70
|
if (maxBytes < markerBytes || maxLines === 1) {
|
|
64
71
|
return {
|
|
65
72
|
content: capUtf8Bytes(OMITTED_MARKER, maxBytes),
|
|
@@ -69,16 +76,20 @@ export const capToolResult = (
|
|
|
69
76
|
truncated: true,
|
|
70
77
|
};
|
|
71
78
|
}
|
|
79
|
+
|
|
72
80
|
const headCount = Math.floor((maxLines - 1) / 2);
|
|
73
81
|
const tailCount = maxLines - 1 - headCount;
|
|
74
82
|
const availableBytes = maxBytes - markerBytes - 2;
|
|
75
83
|
const head = collect(lines, headCount, Math.floor(availableBytes / 2));
|
|
84
|
+
|
|
76
85
|
const tail = collect(
|
|
77
86
|
lines.slice(Math.max(head.length, lines.length - tailCount)),
|
|
78
87
|
tailCount,
|
|
79
88
|
availableBytes - byteLength(head.join("\n"))
|
|
80
89
|
);
|
|
90
|
+
|
|
81
91
|
const content = [...head, OMITTED_MARKER, ...tail].join("\n");
|
|
92
|
+
|
|
82
93
|
return {
|
|
83
94
|
content,
|
|
84
95
|
omittedLines: Math.max(0, totalLines - head.length - tail.length),
|