@selesai/code 0.13.33 → 0.13.35
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 +21 -0
- package/dist/extensions/capability-gateway/catalog.ts +4 -1
- package/dist/extensions/capability-gateway/index.test.ts +93 -4
- package/dist/extensions/capability-gateway/index.ts +295 -59
- package/dist/extensions/capability-gateway/integration.test.ts +35 -0
- package/dist/extensions/capability-gateway/routing.test.ts +154 -1
- package/dist/extensions/capability-gateway/routing.ts +227 -45
- package/dist/extensions/jev/decisions.test.ts +37 -0
- package/dist/extensions/jev/decisions.ts +161 -43
- package/dist/extensions/jev-ask-tool.test.ts +501 -0
- package/dist/extensions/jev-ask-tool.ts +952 -0
- package/dist/extensions/package.json +1 -0
- package/dist/extensions/pi-hermes-memory/README.md +11 -36
- package/dist/extensions/pi-hermes-memory/src/config.ts +36 -8
- package/dist/extensions/pi-hermes-memory/src/constants.ts +5 -5
- package/dist/extensions/pi-hermes-memory/src/handlers/auto-consolidate.ts +60 -46
- package/dist/extensions/pi-hermes-memory/tests/config.test.ts +26 -2
- package/dist/extensions/pi-hermes-memory/tests/handlers/auto-consolidate.test.ts +7 -1
- package/dist/extensions/pi-intercom/index.ts +5 -1
- package/dist/extensions/pi-subagents/src/extension/public-execution.ts +6 -4
- package/dist/extensions/pi-subagents/src/extension/schemas.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +26 -1
- package/dist/extensions/pi-subagents/src/runs/shared/jev-subagent-routing.ts +255 -0
- package/dist/extensions/pi-subagents/test/unit/jev-subagent-routing.test.ts +117 -0
- package/dist/extensions/pi-subagents/test/unit/public-execution.test.ts +3 -1
- package/dist/extensions/rtk.test.ts +21 -13
- package/dist/extensions/tps.test.ts +32 -1
- package/dist/extensions/tps.ts +3 -1
- package/docs/settings.md +64 -7
- package/package.json +3 -3
|
@@ -0,0 +1,952 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* jev-ask-tool — the agent-callable `ask_jev` tool.
|
|
3
|
+
*
|
|
4
|
+
* The host does not decide when Jev runs here. The agent assembles one state
|
|
5
|
+
* from its own prose (`state`), code read from `paths`, and the output of one
|
|
6
|
+
* `command`, hands over a block of typed questions, and gets answers back. The
|
|
7
|
+
* file contents and command output it supplied are never returned, and only the
|
|
8
|
+
* answers reach the transcript.
|
|
9
|
+
*
|
|
10
|
+
* On by default; `jevAdvisory.routes.ask.enabled: false` turns it off. Files must resolve
|
|
11
|
+
* inside the working directory and obvious secret files are refused by name; the
|
|
12
|
+
* command runs through the same local shell operations as the bash tool, on the
|
|
13
|
+
* tool's own abort signal. Every part is clipped to the route's payload budget
|
|
14
|
+
* before the call, so an oversized request is trimmed rather than abstained.
|
|
15
|
+
*/
|
|
16
|
+
import { execFile } from "node:child_process";
|
|
17
|
+
import { closeSync, openSync, readSync, realpathSync, statSync } from "node:fs";
|
|
18
|
+
import { basename, isAbsolute, normalize, relative, resolve } from "node:path";
|
|
19
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
20
|
+
import {
|
|
21
|
+
createLocalBashOperations,
|
|
22
|
+
ensureTool,
|
|
23
|
+
type ExtensionAPI,
|
|
24
|
+
type ExtensionContext,
|
|
25
|
+
getSettingsPath,
|
|
26
|
+
} from "@selesai/code";
|
|
27
|
+
import { Type } from "typebox";
|
|
28
|
+
import {
|
|
29
|
+
askJevAnswers,
|
|
30
|
+
confidenceBucket,
|
|
31
|
+
emitJevTelemetry,
|
|
32
|
+
jevConnection,
|
|
33
|
+
jevUnavailable,
|
|
34
|
+
readJevAdvisoryConfig,
|
|
35
|
+
serializeJevRequest,
|
|
36
|
+
UNTRUSTED_MATERIAL_FOCUS,
|
|
37
|
+
warnJevUnavailableOnce,
|
|
38
|
+
type JevAbstainReason,
|
|
39
|
+
} from "./jev/decisions.ts";
|
|
40
|
+
|
|
41
|
+
/** The tool's registered name. */
|
|
42
|
+
export const ASK_JEV_TOOL = "ask_jev";
|
|
43
|
+
|
|
44
|
+
/** Question types the tool accepts, one per Jev primitive. */
|
|
45
|
+
export const ASK_JEV_QUESTION_TYPES = ["choice", "score", "noul"] as const;
|
|
46
|
+
export type AskJevQuestionType = (typeof ASK_JEV_QUESTION_TYPES)[number];
|
|
47
|
+
|
|
48
|
+
/** Ceilings applied before anything leaves the process. */
|
|
49
|
+
export const MAX_ASK_QUESTIONS = 8;
|
|
50
|
+
export const MAX_ASK_PATHS = 8;
|
|
51
|
+
export const MAX_ASK_STATE_CHARS = 4_000;
|
|
52
|
+
export const MAX_ASK_FILE_BYTES = 16 * 1024;
|
|
53
|
+
export const MAX_ASK_COMMAND_BYTES = 16 * 1024;
|
|
54
|
+
export const ASK_COMMAND_TIMEOUT_SECONDS = 60;
|
|
55
|
+
/** Fixed JSON scaffolding (`{"state":{"files":{},"command":{}}}` and friends). */
|
|
56
|
+
const ASK_ENVELOPE_BYTES = 512;
|
|
57
|
+
|
|
58
|
+
/** Marks code Jev is shown as incomplete, so it judges a clipped file as clipped. */
|
|
59
|
+
const TRUNCATION_MARKER = "\n… [truncated]";
|
|
60
|
+
|
|
61
|
+
/** File names never sent to Jev, whatever the agent asks for. */
|
|
62
|
+
const SECRET_BASENAME =
|
|
63
|
+
/^(?:\.env(?:\..+)?|\.npmrc|\.netrc|auth\.json|credentials(?:\..+)?|id_(?:rsa|dsa|ecdsa|ed25519)(?:\.pub)?|.+\.(?:pem|key|p12|pfx|keystore))$/i;
|
|
64
|
+
|
|
65
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
66
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// Questions
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
|
|
73
|
+
export interface AskJevQuestion {
|
|
74
|
+
name: string;
|
|
75
|
+
type: AskJevQuestionType;
|
|
76
|
+
instructions: string;
|
|
77
|
+
criteria?: unknown;
|
|
78
|
+
focus?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ParsedAskQuestions {
|
|
82
|
+
questions: AskJevQuestion[];
|
|
83
|
+
/** Set when the block cannot be asked; `questions` is empty then. */
|
|
84
|
+
error?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Validate the agent's question block, or explain why it cannot be asked. */
|
|
88
|
+
export function parseAskQuestions(raw: Record<string, unknown> | undefined): ParsedAskQuestions {
|
|
89
|
+
const entries = Object.entries(raw ?? {});
|
|
90
|
+
if (entries.length === 0) return { questions: [], error: "At least one question is required." };
|
|
91
|
+
if (entries.length > MAX_ASK_QUESTIONS) {
|
|
92
|
+
return { questions: [], error: `At most ${MAX_ASK_QUESTIONS} questions per call; got ${entries.length}.` };
|
|
93
|
+
}
|
|
94
|
+
const questions: AskJevQuestion[] = [];
|
|
95
|
+
for (const [name, value] of entries) {
|
|
96
|
+
const spec = isRecord(value) ? value : {};
|
|
97
|
+
const rawType = spec.type;
|
|
98
|
+
if (typeof rawType !== "string" || !ASK_JEV_QUESTION_TYPES.includes(rawType as AskJevQuestionType)) {
|
|
99
|
+
return {
|
|
100
|
+
questions: [],
|
|
101
|
+
error: `Question "${name}" has type ${JSON.stringify(rawType)}; use one of ${ASK_JEV_QUESTION_TYPES.join(", ")}.`,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const instructions = spec.instructions;
|
|
105
|
+
if (typeof instructions !== "string" || instructions.trim() === "") {
|
|
106
|
+
return { questions: [], error: `Question "${name}" needs non-empty instructions.` };
|
|
107
|
+
}
|
|
108
|
+
const criteria = spec.criteria;
|
|
109
|
+
if (rawType === "choice" && (!isRecord(criteria) || Object.keys(criteria).length < 2)) {
|
|
110
|
+
return {
|
|
111
|
+
questions: [],
|
|
112
|
+
error: `Choice question "${name}" needs criteria as an object of at least two options, e.g. {"option": "what the option means"}.`,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (rawType === "score" && (!Array.isArray(criteria) || criteria.length < 2)) {
|
|
116
|
+
return {
|
|
117
|
+
questions: [],
|
|
118
|
+
error: `Score question "${name}" needs criteria as an ordered list of at least two level labels.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (rawType === "noul" && criteria !== undefined && !isRecord(criteria)) {
|
|
122
|
+
return {
|
|
123
|
+
questions: [],
|
|
124
|
+
error: `Noul question "${name}" needs criteria as {"true": "...", "false": "..."}.`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
questions.push({
|
|
128
|
+
name,
|
|
129
|
+
type: rawType as AskJevQuestionType,
|
|
130
|
+
instructions: instructions.trim(),
|
|
131
|
+
...(criteria === undefined ? {} : { criteria }),
|
|
132
|
+
...(typeof spec.focus === "string" && spec.focus.trim() !== "" ? { focus: spec.focus.trim() } : {}),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return { questions };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The decisions request: one assembled state and one question per name. Every
|
|
140
|
+
* criterion and every state part stays untrusted material to judge, never an
|
|
141
|
+
* instruction, exactly as [`buildJevPayload`] frames it.
|
|
142
|
+
*/
|
|
143
|
+
export function buildAskPayload(
|
|
144
|
+
questions: readonly AskJevQuestion[],
|
|
145
|
+
state: Record<string, unknown>,
|
|
146
|
+
): Record<string, unknown> {
|
|
147
|
+
return {
|
|
148
|
+
state,
|
|
149
|
+
questions: Object.fromEntries(
|
|
150
|
+
questions.map((question) => [
|
|
151
|
+
question.name,
|
|
152
|
+
{
|
|
153
|
+
type: question.type,
|
|
154
|
+
instructions: {
|
|
155
|
+
question: question.instructions,
|
|
156
|
+
focus: question.focus ? `${question.focus} ${UNTRUSTED_MATERIAL_FOCUS}` : UNTRUSTED_MATERIAL_FOCUS,
|
|
157
|
+
},
|
|
158
|
+
...(question.criteria === undefined ? {} : { criteria: question.criteria }),
|
|
159
|
+
},
|
|
160
|
+
]),
|
|
161
|
+
),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// Bounded state
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
/** Per-part byte caps that together stay under the route's request budget. */
|
|
170
|
+
export function askPartBudget(
|
|
171
|
+
payloadBytes: number,
|
|
172
|
+
fileCount: number,
|
|
173
|
+
questionBytes: number,
|
|
174
|
+
): { stateChars: number; commandBytes: number; fileBytes: number } {
|
|
175
|
+
const available = Math.max(0, payloadBytes - questionBytes - ASK_ENVELOPE_BYTES);
|
|
176
|
+
const stateChars = Math.min(MAX_ASK_STATE_CHARS, Math.floor(available * 0.2));
|
|
177
|
+
const commandBytes = Math.min(MAX_ASK_COMMAND_BYTES, Math.floor(available * 0.4));
|
|
178
|
+
const forFiles = Math.max(0, available - stateChars - commandBytes);
|
|
179
|
+
return {
|
|
180
|
+
stateChars,
|
|
181
|
+
commandBytes,
|
|
182
|
+
fileBytes: fileCount === 0 ? 0 : Math.min(MAX_ASK_FILE_BYTES, Math.floor(forFiles / fileCount)),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Clip text to a UTF-8 byte budget without splitting a character. */
|
|
187
|
+
function clip(text: string, maxBytes: number): { text: string; clipped: boolean } {
|
|
188
|
+
if (maxBytes <= 0) return { text: "", clipped: text !== "" };
|
|
189
|
+
if (Buffer.byteLength(text, "utf-8") <= maxBytes) return { text, clipped: false };
|
|
190
|
+
let end = Math.min(text.length, maxBytes);
|
|
191
|
+
while (end > 0 && Buffer.byteLength(text.slice(0, end), "utf-8") > maxBytes) end -= 1;
|
|
192
|
+
return { text: text.slice(0, end), clipped: true };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A path the tool may read: inside the working directory, and not a known secret file. */
|
|
196
|
+
export function askPath(raw: string, cwd: string): { full: string } | { refused: string } {
|
|
197
|
+
const full = isAbsolute(raw) ? resolve(raw) : resolve(cwd, raw);
|
|
198
|
+
// Judge the symlink-resolved target too: a link inside cwd may point anywhere.
|
|
199
|
+
// A missing path has no target to leak; the read reports "not found".
|
|
200
|
+
const checks: Array<[string, string]> = [[full, resolve(cwd)]];
|
|
201
|
+
try {
|
|
202
|
+
checks.push([realpathSync(full), realpathSync(cwd)]);
|
|
203
|
+
} catch {}
|
|
204
|
+
for (const [path, root] of checks) {
|
|
205
|
+
const inside = relative(root, path);
|
|
206
|
+
if (inside === "" || inside.startsWith("..") || isAbsolute(inside)) {
|
|
207
|
+
return { refused: "outside the working directory" };
|
|
208
|
+
}
|
|
209
|
+
if (SECRET_BASENAME.test(basename(path))) return { refused: "looks like a secret file" };
|
|
210
|
+
}
|
|
211
|
+
return { full };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Read at most `maxBytes` of a file without loading the rest of it, or say why it could not be read. */
|
|
215
|
+
function readBounded(path: string, maxBytes: number): { text: string; clipped: boolean } | { error: string } {
|
|
216
|
+
let size: number;
|
|
217
|
+
try {
|
|
218
|
+
const stats = statSync(path);
|
|
219
|
+
if (!stats.isFile()) return { error: "not a file" };
|
|
220
|
+
size = stats.size;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
return { error: isRecord(error) && error.code === "ENOENT" ? "not found" : "unreadable" };
|
|
223
|
+
}
|
|
224
|
+
const length = Math.max(0, Math.min(size, maxBytes));
|
|
225
|
+
const buffer = Buffer.alloc(length);
|
|
226
|
+
try {
|
|
227
|
+
const fd = openSync(path, "r");
|
|
228
|
+
try {
|
|
229
|
+
const read = readSync(fd, buffer, 0, length, 0);
|
|
230
|
+
return { text: buffer.subarray(0, read).toString("utf-8"), clipped: size > read };
|
|
231
|
+
} finally {
|
|
232
|
+
closeSync(fd);
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
return { error: "unreadable" };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** One shell command through the same local operations the bash tool uses. */
|
|
240
|
+
export async function runAskCommand(
|
|
241
|
+
command: string,
|
|
242
|
+
cwd: string,
|
|
243
|
+
signal: AbortSignal | undefined,
|
|
244
|
+
): Promise<{ output: string; exitCode: number | null; clipped: boolean; failed: boolean }> {
|
|
245
|
+
const chunks: string[] = [];
|
|
246
|
+
let bytes = 0;
|
|
247
|
+
let clipped = false;
|
|
248
|
+
let failed = false;
|
|
249
|
+
let exitCode: number | null = null;
|
|
250
|
+
try {
|
|
251
|
+
const code = await createLocalBashOperations().exec(command, cwd, {
|
|
252
|
+
onData: (data) => {
|
|
253
|
+
if (clipped) return;
|
|
254
|
+
const text = data.toString("utf-8");
|
|
255
|
+
chunks.push(text);
|
|
256
|
+
bytes += Buffer.byteLength(text, "utf-8");
|
|
257
|
+
if (bytes >= MAX_ASK_COMMAND_BYTES) clipped = true;
|
|
258
|
+
},
|
|
259
|
+
signal,
|
|
260
|
+
timeout: ASK_COMMAND_TIMEOUT_SECONDS,
|
|
261
|
+
});
|
|
262
|
+
exitCode = code.exitCode;
|
|
263
|
+
} catch {
|
|
264
|
+
// A failed, timed-out, or cancelled command still reports what it printed.
|
|
265
|
+
failed = true;
|
|
266
|
+
}
|
|
267
|
+
return { output: chunks.join(""), exitCode, clipped, failed };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ---------------------------------------------------------------------------
|
|
271
|
+
// Answers
|
|
272
|
+
// ---------------------------------------------------------------------------
|
|
273
|
+
|
|
274
|
+
function formatValue(value: unknown): string {
|
|
275
|
+
if (typeof value === "number") return Number.isInteger(value) ? String(value) : value.toFixed(2);
|
|
276
|
+
if (typeof value === "string") return value;
|
|
277
|
+
return JSON.stringify(value);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** A score answer as the agent reads it: the nearest level label, then the raw position. */
|
|
281
|
+
export function formatScore(answer: Record<string, unknown>, levels: unknown): string | undefined {
|
|
282
|
+
const score = answer.score;
|
|
283
|
+
if (typeof score !== "number") return undefined;
|
|
284
|
+
const legend = isRecord(answer.legend) ? answer.legend : undefined;
|
|
285
|
+
const nearest = Math.round(score);
|
|
286
|
+
const label = legend?.[String(nearest)] ?? (Array.isArray(levels) ? levels[nearest] : undefined);
|
|
287
|
+
return typeof label === "string" ? `${label} (${formatValue(score)})` : formatValue(score);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Render answers for the agent: each value and its confidence, never the raw
|
|
292
|
+
* probability distribution, the file contents, or the command output.
|
|
293
|
+
*/
|
|
294
|
+
export function renderAskAnswers(input: {
|
|
295
|
+
questions: readonly AskJevQuestion[];
|
|
296
|
+
answers: Record<string, unknown>;
|
|
297
|
+
rejected: Record<string, JevAbstainReason>;
|
|
298
|
+
failure?: JevAbstainReason;
|
|
299
|
+
model: string;
|
|
300
|
+
elapsedMs: number;
|
|
301
|
+
refused: readonly string[];
|
|
302
|
+
notes: readonly string[];
|
|
303
|
+
/** How many paths the agent passed, and the directory they resolve against. */
|
|
304
|
+
pathsRequested?: number;
|
|
305
|
+
cwd?: string;
|
|
306
|
+
}): string {
|
|
307
|
+
const lines: string[] = [];
|
|
308
|
+
let answered = 0;
|
|
309
|
+
for (const question of input.questions) {
|
|
310
|
+
const answer = input.answers[question.name];
|
|
311
|
+
if (!isRecord(answer)) {
|
|
312
|
+
lines.push(`- ${question.name}: no answer (${input.rejected[question.name] ?? input.failure ?? "missing"})`);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
answered += 1;
|
|
316
|
+
// The type is already in the label, the legend is folded into the score, and raw
|
|
317
|
+
// distributions stay out of the agent's context.
|
|
318
|
+
const fields = Object.entries(answer)
|
|
319
|
+
.filter(([key]) => !["probabilities", "type", "legend"].includes(key))
|
|
320
|
+
.map(([key, value]) =>
|
|
321
|
+
key === "score" ? `score=${formatScore(answer, question.criteria)}` : `${key}=${formatValue(value)}`,
|
|
322
|
+
);
|
|
323
|
+
lines.push(`- ${question.name} (${question.type}): ${fields.length > 0 ? fields.join(", ") : "answered"}`);
|
|
324
|
+
}
|
|
325
|
+
const header = [`ask_jev: ${answered}/${input.questions.length} answered via ${input.model} in ${input.elapsedMs}ms`];
|
|
326
|
+
if (answered === 0 && input.failure) header.push(`Jev was unavailable or abstained: ${input.failure}.`);
|
|
327
|
+
// Missing material changes what the answers mean, so it leads rather than trails them.
|
|
328
|
+
const requested = input.pathsRequested ?? 0;
|
|
329
|
+
if (requested > 0 && input.refused.length >= requested) {
|
|
330
|
+
header.push(
|
|
331
|
+
`WARNING: Jev answered without any of the ${requested} files you passed (${input.refused.join("; ")}). ` +
|
|
332
|
+
`Paths resolve inside ${input.cwd ?? "the working directory"}; treat these answers as judged from your prose alone.`,
|
|
333
|
+
);
|
|
334
|
+
} else if (input.refused.length > 0) {
|
|
335
|
+
header.push(`Not sent to Jev: ${input.refused.join("; ")}.`);
|
|
336
|
+
}
|
|
337
|
+
const footer = [
|
|
338
|
+
"Only these answers are returned: the file contents and command output you supplied were not sent back to you.",
|
|
339
|
+
...(input.notes.length > 0 ? [`Truncated to fit the request budget: ${input.notes.join("; ")}.`] : []),
|
|
340
|
+
];
|
|
341
|
+
return [...header, ...lines, ...footer].join("\n");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ---------------------------------------------------------------------------
|
|
345
|
+
// jev_find: ripgrep gathers candidates, Jev judges each one
|
|
346
|
+
// ---------------------------------------------------------------------------
|
|
347
|
+
|
|
348
|
+
/** The file finder's registered name. */
|
|
349
|
+
export const JEV_FIND_TOOL = "jev_find";
|
|
350
|
+
/** ponytail: 16 nouls per request (the JevPDF batch size); raise once Jev is measured on larger blocks. */
|
|
351
|
+
export const FIND_BATCH = 16;
|
|
352
|
+
/** Three batches, sent in parallel, so a find costs one Jev round trip. */
|
|
353
|
+
export const MAX_FIND_CANDIDATES = 48;
|
|
354
|
+
const FIND_MATCH_LINES = 6;
|
|
355
|
+
const FIND_DEFAULT_LIMIT = 8;
|
|
356
|
+
const FIND_MIN_RELEVANCE = 0.5;
|
|
357
|
+
/** Room each batch keeps for the questions and JSON scaffolding. */
|
|
358
|
+
const FIND_QUESTION_RESERVE = 6 * 1024;
|
|
359
|
+
|
|
360
|
+
export interface FindCandidate {
|
|
361
|
+
path: string;
|
|
362
|
+
/** Matched line numbers (pattern mode only). */
|
|
363
|
+
lines: number[];
|
|
364
|
+
/** What Jev reads: the matched lines, or the head of the file. */
|
|
365
|
+
snippet: string;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Run ripgrep with an argument list (no shell). Exit 1 is "no matches"; an overfull buffer keeps what arrived. */
|
|
369
|
+
function runRg(bin: string, args: string[], cwd: string, signal: AbortSignal | undefined): Promise<string> {
|
|
370
|
+
return new Promise((done, fail) => {
|
|
371
|
+
execFile(
|
|
372
|
+
bin,
|
|
373
|
+
args,
|
|
374
|
+
{ cwd, signal, maxBuffer: 8 * 1024 * 1024, timeout: ASK_COMMAND_TIMEOUT_SECONDS * 1000 },
|
|
375
|
+
(error, stdout) => {
|
|
376
|
+
const code = error ? (error as { code?: unknown }).code : 0;
|
|
377
|
+
if (!error || code === 1 || code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") done(String(stdout));
|
|
378
|
+
else fail(error);
|
|
379
|
+
},
|
|
380
|
+
);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Filler words that would match nearly every file. */
|
|
385
|
+
const FIND_STOP_WORDS = new Set(
|
|
386
|
+
"the and for with where what which when who how does did this that from into its are was can should would there file files code find implement implemented implements".split(" "),
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
/** Question words worth searching for: 3+ letters, not filler, at most eight. */
|
|
390
|
+
function questionWords(question: string): string[] {
|
|
391
|
+
const words = question.toLowerCase().match(/[a-z0-9]{3,}/g) ?? [];
|
|
392
|
+
return [...new Set(words)].filter((word) => !FIND_STOP_WORDS.has(word)).slice(0, 8);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** How much of each listed file is scanned for question words. */
|
|
396
|
+
const FIND_SCAN_BYTES = 256 * 1024;
|
|
397
|
+
const FIND_EXCERPT_LINES = 12;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* What Jev reads of a listed file: the lines holding the most distinct question words, in file
|
|
401
|
+
* order and numbered, or the head of the file when none match. A file's head is usually its
|
|
402
|
+
* doc comment, which rarely shows where the asked-about behavior lives.
|
|
403
|
+
*/
|
|
404
|
+
export function excerpt(text: string, words: readonly string[], maxBytes: number): { lines: number[]; snippet: string } {
|
|
405
|
+
const rows = text.split("\n");
|
|
406
|
+
const best = rows
|
|
407
|
+
.map((row, index) => {
|
|
408
|
+
const lower = row.toLowerCase();
|
|
409
|
+
return { index, count: words.filter((word) => lower.includes(word)).length };
|
|
410
|
+
})
|
|
411
|
+
.filter((hit) => hit.count > 0)
|
|
412
|
+
.sort((a, b) => b.count - a.count || a.index - b.index)
|
|
413
|
+
.slice(0, FIND_EXCERPT_LINES);
|
|
414
|
+
if (best.length === 0) return { lines: [], snippet: clip(text, maxBytes).text };
|
|
415
|
+
const inOrder = [...best].sort((a, b) => a.index - b.index);
|
|
416
|
+
const snippet = inOrder.map((hit) => `L${hit.index + 1}: ${rows[hit.index].trim().slice(0, 240)}`).join("\n");
|
|
417
|
+
// Pointers lead with the best-matching lines.
|
|
418
|
+
return { lines: best.map((hit) => hit.index + 1), snippet: clip(snippet, maxBytes).text };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Path parts (3+ letters) sharing a stem with a question word; orders a file listing before it is capped. */
|
|
422
|
+
function pathOverlap(path: string, words: readonly string[]): number {
|
|
423
|
+
const parts = path.toLowerCase().match(/[a-z0-9]{3,}/g) ?? [];
|
|
424
|
+
return parts.filter((part) => words.some((word) => word.includes(part) || part.includes(word))).length;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Candidate files under `root`, most promising first. With `pattern`, the files ripgrep matches
|
|
429
|
+
* (most matches first) with their matched lines as the snippet; without it, every file ripgrep
|
|
430
|
+
* lists (question words in the path first) with the head of the file as the snippet. Respects
|
|
431
|
+
* .gitignore, and never offers a file `askPath` would refuse.
|
|
432
|
+
*/
|
|
433
|
+
export async function findCandidates(
|
|
434
|
+
options: { rg: string; cwd: string; root: string; question: string; pattern?: string; glob?: string; ignoreCase?: boolean },
|
|
435
|
+
snippetBytes: number,
|
|
436
|
+
signal: AbortSignal | undefined,
|
|
437
|
+
): Promise<{ candidates: FindCandidate[]; total: number }> {
|
|
438
|
+
// Always name the root: with no path and a piped stdin, rg searches stdin and hangs.
|
|
439
|
+
// Paths come back as `./src/x.ts`, so they are normalized below.
|
|
440
|
+
const root = ["--", options.root];
|
|
441
|
+
const filters = [...(options.glob ? ["--glob", options.glob] : []), ...(options.ignoreCase ? ["-i"] : [])];
|
|
442
|
+
const allowed = (path: string) => "full" in askPath(path, options.cwd);
|
|
443
|
+
if (options.pattern) {
|
|
444
|
+
const out = await runRg(
|
|
445
|
+
options.rg,
|
|
446
|
+
["--null", "--line-number", "--no-heading", "--color=never", "--max-count", String(FIND_MATCH_LINES), "--max-columns", "240", ...filters, "-e", options.pattern, ...root],
|
|
447
|
+
options.cwd,
|
|
448
|
+
signal,
|
|
449
|
+
);
|
|
450
|
+
const byFile = new Map<string, { lines: number[]; text: string[] }>();
|
|
451
|
+
for (const row of out.split("\n")) {
|
|
452
|
+
const nul = row.indexOf("\0");
|
|
453
|
+
if (nul < 0) continue;
|
|
454
|
+
const path = normalize(row.slice(0, nul));
|
|
455
|
+
const match = /^(\d+):(.*)$/.exec(row.slice(nul + 1));
|
|
456
|
+
if (!match) continue;
|
|
457
|
+
const entry = byFile.get(path) ?? { lines: [], text: [] };
|
|
458
|
+
entry.lines.push(Number(match[1]));
|
|
459
|
+
entry.text.push(`L${match[1]}: ${match[2].trim()}`);
|
|
460
|
+
byFile.set(path, entry);
|
|
461
|
+
}
|
|
462
|
+
const files = [...byFile].filter(([path]) => allowed(path)).sort((a, b) => b[1].lines.length - a[1].lines.length);
|
|
463
|
+
return {
|
|
464
|
+
total: files.length,
|
|
465
|
+
candidates: files.slice(0, MAX_FIND_CANDIDATES).map(([path, entry]) => ({
|
|
466
|
+
path,
|
|
467
|
+
lines: entry.lines,
|
|
468
|
+
snippet: clip(entry.text.join("\n"), snippetBytes).text,
|
|
469
|
+
})),
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
const words = questionWords(options.question);
|
|
473
|
+
const out = await runRg(options.rg, ["--files", "--color=never", ...filters, ...root], options.cwd, signal);
|
|
474
|
+
const listed = out
|
|
475
|
+
.split("\n")
|
|
476
|
+
.filter((path) => path !== "")
|
|
477
|
+
.map((path) => normalize(path))
|
|
478
|
+
.filter(allowed);
|
|
479
|
+
const score = new Map(listed.map((path) => [path, pathOverlap(path, words)]));
|
|
480
|
+
if (listed.length > MAX_FIND_CANDIDATES && words.length > 0) {
|
|
481
|
+
// More files than Jev judges: a file scores once per distinct question word its content holds,
|
|
482
|
+
// one fixed-string ripgrep per word, in parallel.
|
|
483
|
+
const hits = await Promise.all(
|
|
484
|
+
words.map((word) =>
|
|
485
|
+
runRg(options.rg, ["-l", "-i", "-F", "--color=never", ...filters, "-e", word, ...root], options.cwd, signal),
|
|
486
|
+
),
|
|
487
|
+
);
|
|
488
|
+
for (const listing of hits) {
|
|
489
|
+
for (const raw of listing.split("\n")) {
|
|
490
|
+
const path = raw === "" ? "" : normalize(raw);
|
|
491
|
+
const current = score.get(path);
|
|
492
|
+
if (current !== undefined) score.set(path, current + 1);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
const files = listed.map((path) => ({ path })).sort((a, b) => (score.get(b.path) ?? 0) - (score.get(a.path) ?? 0));
|
|
497
|
+
const candidates: FindCandidate[] = [];
|
|
498
|
+
for (const { path } of files.slice(0, MAX_FIND_CANDIDATES)) {
|
|
499
|
+
const file = readBounded(resolve(options.cwd, path), FIND_SCAN_BYTES);
|
|
500
|
+
if ("error" in file) continue;
|
|
501
|
+
candidates.push({ path, ...excerpt(file.text, words, snippetBytes) });
|
|
502
|
+
}
|
|
503
|
+
return { candidates, total: files.length };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** One batch as a decisions request: the shared snippets as state, one noul per file. */
|
|
507
|
+
export function buildFindPayload(question: string, batch: readonly FindCandidate[]): Record<string, unknown> {
|
|
508
|
+
return buildAskPayload(
|
|
509
|
+
batch.map((candidate, index) => ({
|
|
510
|
+
name: `f${index}`,
|
|
511
|
+
type: "noul" as const,
|
|
512
|
+
instructions: `Is the file "${candidate.path}" relevant to the request, judged by its excerpt in state.files?`,
|
|
513
|
+
criteria: {
|
|
514
|
+
true: "The excerpt shows this file implements, defines, configures, or directly answers the request.",
|
|
515
|
+
false: "The file only mentions related words, or is about something else.",
|
|
516
|
+
},
|
|
517
|
+
})),
|
|
518
|
+
{ request: question, files: Object.fromEntries(batch.map((candidate) => [candidate.path, candidate.snippet])) },
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* The batch's request, its snippets shrunk until the serialized request fits `maxBytes`: code
|
|
524
|
+
* grows under JSON escaping, so a byte budget per snippet alone does not guarantee a fit.
|
|
525
|
+
*/
|
|
526
|
+
export function fitFindPayload(question: string, batch: readonly FindCandidate[], maxBytes: number): Record<string, unknown> {
|
|
527
|
+
let limit = Math.max(0, ...batch.map((candidate) => Buffer.byteLength(candidate.snippet, "utf-8")));
|
|
528
|
+
for (;;) {
|
|
529
|
+
const payload = buildFindPayload(
|
|
530
|
+
question,
|
|
531
|
+
batch.map((candidate) => ({ ...candidate, snippet: clip(candidate.snippet, limit).text })),
|
|
532
|
+
);
|
|
533
|
+
if (limit < 64 || serializeJevRequest(payload, maxBytes) !== undefined) return payload;
|
|
534
|
+
limit = Math.floor(limit * 0.75);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Pointers only: path, matched lines, relevance. File contents never reach the agent. */
|
|
539
|
+
export function renderFindResults(input: {
|
|
540
|
+
ranked: ReadonlyArray<FindCandidate & { relevance?: number }>;
|
|
541
|
+
total: number;
|
|
542
|
+
judged: number;
|
|
543
|
+
limit: number;
|
|
544
|
+
model: string;
|
|
545
|
+
elapsedMs: number;
|
|
546
|
+
failure?: string;
|
|
547
|
+
}): string {
|
|
548
|
+
const pointer = (candidate: FindCandidate) =>
|
|
549
|
+
candidate.lines.length > 0 ? `${candidate.path}:${candidate.lines.slice(0, 4).join(",")}` : candidate.path;
|
|
550
|
+
if (input.total === 0) return "jev_find: no candidate files. Loosen `pattern`, `glob`, or `path`.";
|
|
551
|
+
const lines: string[] = [];
|
|
552
|
+
if (input.judged === 0) {
|
|
553
|
+
lines.push(
|
|
554
|
+
`jev_find: Jev did not judge (${input.failure ?? "missing"}); ${input.total} candidate file(s), unranked by relevance:`,
|
|
555
|
+
...input.ranked.slice(0, input.limit).map((candidate) => `- ${pointer(candidate)}`),
|
|
556
|
+
);
|
|
557
|
+
} else {
|
|
558
|
+
const strong = input.ranked.filter((candidate) => (candidate.relevance ?? 0) >= FIND_MIN_RELEVANCE);
|
|
559
|
+
lines.push(`jev_find: judged ${input.judged} of ${input.total} candidate file(s) via ${input.model} in ${input.elapsedMs}ms`);
|
|
560
|
+
const shown = strong.length > 0 ? strong.slice(0, input.limit) : input.ranked.slice(0, 3);
|
|
561
|
+
if (strong.length === 0) lines.push("No file is a confident match; the closest were:");
|
|
562
|
+
for (const candidate of shown) {
|
|
563
|
+
lines.push(`- ${pointer(candidate)} (${candidate.relevance === undefined ? "not judged" : candidate.relevance.toFixed(2)})`);
|
|
564
|
+
}
|
|
565
|
+
if (strong.length > shown.length) lines.push(`${strong.length - shown.length} more relevant file(s) past the limit.`);
|
|
566
|
+
}
|
|
567
|
+
if (input.total > MAX_FIND_CANDIDATES) {
|
|
568
|
+
lines.push(`${input.total - MAX_FIND_CANDIDATES} candidate(s) were not judged; narrow with \`pattern\`, \`glob\`, or \`path\`.`);
|
|
569
|
+
}
|
|
570
|
+
lines.push("Only pointers are returned; read the files you need.");
|
|
571
|
+
return lines.join("\n");
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const JevFindParams = Type.Object({
|
|
575
|
+
question: Type.String({ description: "What you are looking for, in plain words (e.g. 'where is the retry backoff configured')." }),
|
|
576
|
+
pattern: Type.Optional(
|
|
577
|
+
Type.String({ description: "ripgrep regex to narrow candidates to files that match; omit to judge every listed file." }),
|
|
578
|
+
),
|
|
579
|
+
glob: Type.Optional(Type.String({ description: "File glob filter, e.g. '*.ts' or 'src/**/*.md'." })),
|
|
580
|
+
path: Type.Optional(Type.String({ description: "Directory to search, inside the working directory (default: '.')." })),
|
|
581
|
+
ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive pattern." })),
|
|
582
|
+
limit: Type.Optional(Type.Number({ description: `Most results to return (default ${FIND_DEFAULT_LIMIT}).` })),
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
// ---------------------------------------------------------------------------
|
|
586
|
+
// Tool
|
|
587
|
+
// ---------------------------------------------------------------------------
|
|
588
|
+
|
|
589
|
+
const QuestionSpec = Type.Object({
|
|
590
|
+
type: StringEnum(ASK_JEV_QUESTION_TYPES, {
|
|
591
|
+
description: "choice: pick one option. score: place the state on a rubric. noul: probability a statement holds.",
|
|
592
|
+
}),
|
|
593
|
+
instructions: Type.String({ description: "The question, stated in one sentence." }),
|
|
594
|
+
criteria: Type.Optional(
|
|
595
|
+
Type.Unknown({
|
|
596
|
+
description:
|
|
597
|
+
'choice: {"option": "what the option means"} (at least two). score: an ordered array of level labels. noul: {"true": "...", "false": "..."}.',
|
|
598
|
+
}),
|
|
599
|
+
),
|
|
600
|
+
focus: Type.Optional(Type.String({ description: "Extra judging guidance for this question." })),
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
const AskJevParams = Type.Object({
|
|
604
|
+
state: Type.Optional(Type.String({ description: "Your own prose: the situation and what you are deciding." })),
|
|
605
|
+
paths: Type.Optional(
|
|
606
|
+
Type.Array(Type.String(), {
|
|
607
|
+
description: `Code to include as state, read-only (max ${MAX_ASK_PATHS}). Relative paths resolve against the working directory; paths outside it are refused.`,
|
|
608
|
+
}),
|
|
609
|
+
),
|
|
610
|
+
command: Type.Optional(Type.String({ description: "One shell command whose output becomes state." })),
|
|
611
|
+
questions: Type.Record(Type.String(), QuestionSpec, { description: "Question name -> spec." }),
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
export default function jevAskToolExtension(pi: ExtensionAPI): void {
|
|
615
|
+
pi.registerTool({
|
|
616
|
+
name: ASK_JEV_TOOL,
|
|
617
|
+
label: "Ask Jev",
|
|
618
|
+
description: [
|
|
619
|
+
"Ask Jev (a typed decisions model, not a chat model) a block of questions about a state you assemble here:",
|
|
620
|
+
"your own prose in `state`, code read from `paths`, and the output of one shell `command`. Code makes one",
|
|
621
|
+
"bounded call and returns answers only — the file contents and command output are never sent back to you.",
|
|
622
|
+
"Anything you put in the state is sent to Jev, so pass only code you are willing to share.",
|
|
623
|
+
'`questions` is an object keyed by question name; each value is {"type": "choice" | "score" | "noul",',
|
|
624
|
+
'"instructions": <the question>, "criteria": <see below>, "focus"?: <extra guidance>}.',
|
|
625
|
+
'choice criteria: {"option": "what the option means"}; score criteria: an ordered array of level labels;',
|
|
626
|
+
'noul criteria: {"true": "...", "false": "..."} (optional for noul).',
|
|
627
|
+
"Each answer returns the chosen value, score, or probability plus a confidence; raw probability",
|
|
628
|
+
"distributions are omitted.",
|
|
629
|
+
"Use it to triage a failure before touching anything (run the failing command, ask what kind of failure it is),",
|
|
630
|
+
"judge a diff before shipping (a risk score and a needs-review flag), classify a request before planning, or",
|
|
631
|
+
"find which of several files matters without reading them into your own context.",
|
|
632
|
+
`Bounds: ${MAX_ASK_QUESTIONS} questions, ${MAX_ASK_PATHS} paths per call; each file is clipped to about ${MAX_ASK_FILE_BYTES / 1024} KB and marked when clipped.`,
|
|
633
|
+
].join(" "),
|
|
634
|
+
promptSnippet: "Ask Jev a typed question about state you assemble from prose, files, or one command",
|
|
635
|
+
promptGuidelines: [
|
|
636
|
+
"Use ask_jev whenever a judgment would otherwise be a guess — what kind of failure this is, how risky a diff is, what a request is really asking for. It returns typed answers with confidences for a fraction of a cent, so it validates assumptions before they cost a wrong edit.",
|
|
637
|
+
"Prefer ask_jev over reading files when you only need a verdict about them (which of these files handles X, does this code already do Y, is this test output a code bug or a flaky test): pass them as `paths` or the command as `command`, and only the answer enters your context. Read a file yourself when you need its exact text, such as before editing it.",
|
|
638
|
+
],
|
|
639
|
+
discovery: {
|
|
640
|
+
summary: "Ask Jev one block of typed choice/score/noul questions over prose, code, and one command's output",
|
|
641
|
+
aliases: ["jev", "triage", "classify", "judge", "score"],
|
|
642
|
+
category: "Decisions",
|
|
643
|
+
},
|
|
644
|
+
parameters: AskJevParams,
|
|
645
|
+
|
|
646
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx: ExtensionContext) {
|
|
647
|
+
const text = (value: string) => ({ content: [{ type: "text" as const, text: value }], details: {} });
|
|
648
|
+
|
|
649
|
+
const config = readJevAdvisoryConfig(getSettingsPath());
|
|
650
|
+
const route = config.routes.ask;
|
|
651
|
+
if (!route.enabled) {
|
|
652
|
+
return text(
|
|
653
|
+
`ask_jev is disabled (jevAdvisory.routes.ask.enabled is false in ${getSettingsPath()}). Remove that setting or set it to true.`,
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const parsed = parseAskQuestions(params.questions as Record<string, unknown> | undefined);
|
|
658
|
+
if (parsed.error !== undefined) {
|
|
659
|
+
return text(`ask_jev needs a usable question block: ${parsed.error}`);
|
|
660
|
+
}
|
|
661
|
+
const questions = parsed.questions;
|
|
662
|
+
|
|
663
|
+
// Reading files and running the command exist only to feed Jev: without a reachable
|
|
664
|
+
// Jev, do neither (an `npm test` would otherwise run in full and be thrown away).
|
|
665
|
+
const connection = jevConnection(config, route);
|
|
666
|
+
const unreachable = await jevUnavailable(ctx, connection);
|
|
667
|
+
if (unreachable) {
|
|
668
|
+
if (ctx.hasUI) warnJevUnavailableOnce(ctx.ui, config.provider);
|
|
669
|
+
emitJevTelemetry(pi.events, "decision", {
|
|
670
|
+
route: "ask",
|
|
671
|
+
outcome: "fallback",
|
|
672
|
+
candidates: questions.length,
|
|
673
|
+
confidence: confidenceBucket(undefined),
|
|
674
|
+
elapsedMs: 0,
|
|
675
|
+
reason: unreachable,
|
|
676
|
+
});
|
|
677
|
+
return {
|
|
678
|
+
content: [
|
|
679
|
+
{
|
|
680
|
+
type: "text" as const,
|
|
681
|
+
text: `${renderAskAnswers({
|
|
682
|
+
questions,
|
|
683
|
+
answers: {},
|
|
684
|
+
rejected: {},
|
|
685
|
+
failure: unreachable,
|
|
686
|
+
model: config.model,
|
|
687
|
+
elapsedMs: 0,
|
|
688
|
+
refused: [],
|
|
689
|
+
notes: [],
|
|
690
|
+
})}\nNothing was read, run, or sent. Decide without Jev: read the files or run the command yourself.`,
|
|
691
|
+
},
|
|
692
|
+
],
|
|
693
|
+
details: { answered: 0, questions: questions.length, model: config.model, elapsedMs: 0, failure: unreachable },
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
const questionBytes = Buffer.byteLength(
|
|
698
|
+
JSON.stringify(buildAskPayload(questions, {}).questions),
|
|
699
|
+
"utf-8",
|
|
700
|
+
);
|
|
701
|
+
const requested = (params.paths ?? []).slice(0, MAX_ASK_PATHS);
|
|
702
|
+
const budget = askPartBudget(route.payloadBytes, requested.length, questionBytes);
|
|
703
|
+
|
|
704
|
+
const refused: string[] = [];
|
|
705
|
+
const notes: string[] = [];
|
|
706
|
+
|
|
707
|
+
const prose = clip((params.state ?? "").trim(), budget.stateChars);
|
|
708
|
+
if (prose.clipped) notes.push("state");
|
|
709
|
+
|
|
710
|
+
const files: Record<string, string> = {};
|
|
711
|
+
for (const raw of requested) {
|
|
712
|
+
const path = askPath(raw, ctx.cwd);
|
|
713
|
+
if ("refused" in path) {
|
|
714
|
+
refused.push(`${raw} (${path.refused})`);
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
const file = readBounded(path.full, Math.max(0, budget.fileBytes - Buffer.byteLength(TRUNCATION_MARKER, "utf-8")));
|
|
718
|
+
if ("error" in file) {
|
|
719
|
+
refused.push(`${raw} (${file.error})`);
|
|
720
|
+
continue;
|
|
721
|
+
}
|
|
722
|
+
if (file.clipped) notes.push(raw);
|
|
723
|
+
files[relative(ctx.cwd, path.full)] = file.clipped ? `${file.text}${TRUNCATION_MARKER}` : file.text;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
let command: Record<string, unknown> | undefined;
|
|
727
|
+
if (params.command?.trim()) {
|
|
728
|
+
const result = await runAskCommand(params.command.trim(), ctx.cwd, signal);
|
|
729
|
+
const output = clip(result.output, budget.commandBytes);
|
|
730
|
+
if (output.clipped || result.clipped) notes.push("command output");
|
|
731
|
+
command = {
|
|
732
|
+
command: params.command.trim(),
|
|
733
|
+
output: output.text,
|
|
734
|
+
exitCode: result.exitCode,
|
|
735
|
+
...(result.failed ? { failed: true } : {}),
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const payload = buildAskPayload(questions, {
|
|
740
|
+
...(prose.text ? { request: prose.text } : {}),
|
|
741
|
+
...(Object.keys(files).length > 0 ? { files } : {}),
|
|
742
|
+
...(command ? { command } : {}),
|
|
743
|
+
});
|
|
744
|
+
if (serializeJevRequest(payload, route.payloadBytes) === undefined) {
|
|
745
|
+
return text("ask_jev could not fit this request in its budget; pass fewer paths or a shorter command.");
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const result = await askJevAnswers(ctx, connection, {
|
|
749
|
+
payload,
|
|
750
|
+
maxBytes: route.payloadBytes,
|
|
751
|
+
});
|
|
752
|
+
const answers = result.answers ?? {};
|
|
753
|
+
const rejected: Record<string, JevAbstainReason> = {};
|
|
754
|
+
let answered = 0;
|
|
755
|
+
let topConfidence: number | undefined;
|
|
756
|
+
for (const question of questions) {
|
|
757
|
+
const answer = answers[question.name];
|
|
758
|
+
if (!isRecord(answer)) {
|
|
759
|
+
rejected[question.name] = result.failure ?? "missing";
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
answered += 1;
|
|
763
|
+
if (typeof answer.confidence === "number") {
|
|
764
|
+
topConfidence = topConfidence === undefined ? answer.confidence : Math.max(topConfidence, answer.confidence);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// Content-free: no state, answers, or criteria travel, only shape and outcome.
|
|
769
|
+
emitJevTelemetry(pi.events, "decision", {
|
|
770
|
+
route: "ask",
|
|
771
|
+
outcome: answered > 0 ? "jev" : "fallback",
|
|
772
|
+
candidates: questions.length,
|
|
773
|
+
confidence: confidenceBucket(topConfidence),
|
|
774
|
+
elapsedMs: result.elapsedMs,
|
|
775
|
+
...(answered > 0 ? {} : { reason: result.failure ?? "missing" }),
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
return {
|
|
779
|
+
content: [
|
|
780
|
+
{
|
|
781
|
+
type: "text" as const,
|
|
782
|
+
text: renderAskAnswers({
|
|
783
|
+
questions,
|
|
784
|
+
answers,
|
|
785
|
+
rejected,
|
|
786
|
+
failure: result.failure,
|
|
787
|
+
model: config.model,
|
|
788
|
+
elapsedMs: result.elapsedMs,
|
|
789
|
+
refused,
|
|
790
|
+
notes,
|
|
791
|
+
pathsRequested: requested.length,
|
|
792
|
+
cwd: ctx.cwd,
|
|
793
|
+
}),
|
|
794
|
+
},
|
|
795
|
+
],
|
|
796
|
+
// Deliberately shape only: tool details are persisted in the session.
|
|
797
|
+
details: {
|
|
798
|
+
answered,
|
|
799
|
+
questions: questions.length,
|
|
800
|
+
model: config.model,
|
|
801
|
+
elapsedMs: result.elapsedMs,
|
|
802
|
+
...(result.failure ? { failure: result.failure } : {}),
|
|
803
|
+
},
|
|
804
|
+
};
|
|
805
|
+
},
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
pi.registerTool({
|
|
809
|
+
name: JEV_FIND_TOOL,
|
|
810
|
+
label: "Jev Find",
|
|
811
|
+
description: [
|
|
812
|
+
"Find which files answer a question without reading them: ripgrep gathers candidate files (those matching",
|
|
813
|
+
"`pattern`, or every file under `path` filtered by `glob`), and Jev judges each file's excerpt against",
|
|
814
|
+
"`question` in one parallel round trip. Returns ranked `path:lines (relevance)` pointers, never contents.",
|
|
815
|
+
`Judges up to ${MAX_FIND_CANDIDATES} files per call; respects .gitignore; secret-named files are never sent.`,
|
|
816
|
+
].join(" "),
|
|
817
|
+
promptSnippet: "Find the files that answer a question: ripgrep candidates ranked by Jev, pointers only",
|
|
818
|
+
promptGuidelines: [
|
|
819
|
+
"When you look for code by what it does rather than by an exact identifier you already know, call jev_find first — before grep, find, ls, or reading candidate files. One call ranks up to 48 files in about a second and returns file:line pointers; then read only the top ones. Pass `question` in plain words; add `pattern` when you know a likely identifier, `glob`/`path` to scope it.",
|
|
820
|
+
"Use grep for an exact string or identifier you already know; use jev_find when you would otherwise grep several guesses or read files to see which one is relevant.",
|
|
821
|
+
],
|
|
822
|
+
discovery: {
|
|
823
|
+
summary: "Semantic file finder: ripgrep candidates ranked by Jev relevance",
|
|
824
|
+
aliases: ["jevgrep", "find", "search", "locate"],
|
|
825
|
+
category: "Decisions",
|
|
826
|
+
},
|
|
827
|
+
parameters: JevFindParams,
|
|
828
|
+
|
|
829
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx: ExtensionContext) {
|
|
830
|
+
const text = (value: string, details: Record<string, unknown> = {}) => ({
|
|
831
|
+
content: [{ type: "text" as const, text: value }],
|
|
832
|
+
details,
|
|
833
|
+
});
|
|
834
|
+
const config = readJevAdvisoryConfig(getSettingsPath());
|
|
835
|
+
const route = config.routes.ask;
|
|
836
|
+
if (!route.enabled) {
|
|
837
|
+
return text(`jev_find is disabled (jevAdvisory.routes.ask.enabled is false in ${getSettingsPath()}).`);
|
|
838
|
+
}
|
|
839
|
+
const question = params.question?.trim() ?? "";
|
|
840
|
+
if (question === "") return text("jev_find needs a non-empty `question`.");
|
|
841
|
+
|
|
842
|
+
const rootArg = params.path?.trim() || ".";
|
|
843
|
+
const rootFull = resolve(ctx.cwd, rootArg);
|
|
844
|
+
const inside = relative(resolve(ctx.cwd), rootFull);
|
|
845
|
+
if (inside.startsWith("..") || isAbsolute(inside)) {
|
|
846
|
+
return text(`jev_find: ${rootArg} is outside the working directory.`);
|
|
847
|
+
}
|
|
848
|
+
const rg = await ensureTool("rg");
|
|
849
|
+
if (!rg) return text("jev_find needs ripgrep (rg), which is not available; use grep instead.");
|
|
850
|
+
|
|
851
|
+
const snippetBytes = Math.max(256, Math.floor((route.payloadBytes - FIND_QUESTION_RESERVE) / FIND_BATCH));
|
|
852
|
+
let found: { candidates: FindCandidate[]; total: number };
|
|
853
|
+
try {
|
|
854
|
+
found = await findCandidates(
|
|
855
|
+
{
|
|
856
|
+
rg,
|
|
857
|
+
cwd: ctx.cwd,
|
|
858
|
+
root: inside === "" ? "." : inside,
|
|
859
|
+
question,
|
|
860
|
+
pattern: params.pattern || undefined,
|
|
861
|
+
glob: params.glob || undefined,
|
|
862
|
+
ignoreCase: params.ignoreCase,
|
|
863
|
+
},
|
|
864
|
+
snippetBytes,
|
|
865
|
+
signal,
|
|
866
|
+
);
|
|
867
|
+
} catch (error) {
|
|
868
|
+
return text(`jev_find: ripgrep failed (${error instanceof Error ? error.message.split("\n")[0] : String(error)}).`);
|
|
869
|
+
}
|
|
870
|
+
const limit = Math.max(1, Math.floor(params.limit ?? FIND_DEFAULT_LIMIT));
|
|
871
|
+
const { candidates, total } = found;
|
|
872
|
+
if (candidates.length === 0) {
|
|
873
|
+
return text(renderFindResults({ ranked: [], total: 0, judged: 0, limit, model: config.model, elapsedMs: 0 }));
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// Without Jev the candidates are still worth returning: the call is never wasted.
|
|
877
|
+
const connection = jevConnection(config, route);
|
|
878
|
+
const unreachable = await jevUnavailable(ctx, connection);
|
|
879
|
+
const batches: FindCandidate[][] = [];
|
|
880
|
+
for (let start = 0; start < candidates.length; start += FIND_BATCH) {
|
|
881
|
+
batches.push(candidates.slice(start, start + FIND_BATCH));
|
|
882
|
+
}
|
|
883
|
+
const started = Date.now();
|
|
884
|
+
const results = unreachable
|
|
885
|
+
? []
|
|
886
|
+
: await Promise.all(
|
|
887
|
+
batches.map((batch) =>
|
|
888
|
+
askJevAnswers(ctx, connection, {
|
|
889
|
+
payload: fitFindPayload(question, batch, route.payloadBytes),
|
|
890
|
+
maxBytes: route.payloadBytes,
|
|
891
|
+
}),
|
|
892
|
+
),
|
|
893
|
+
);
|
|
894
|
+
const elapsedMs = Date.now() - started;
|
|
895
|
+
|
|
896
|
+
const scored: Array<FindCandidate & { relevance?: number }> = [];
|
|
897
|
+
let judged = 0;
|
|
898
|
+
batches.forEach((batch, b) => {
|
|
899
|
+
const answers = results[b]?.answers ?? {};
|
|
900
|
+
batch.forEach((candidate, i) => {
|
|
901
|
+
const answer = answers[`f${i}`];
|
|
902
|
+
const relevance = isRecord(answer) && typeof answer.noul === "number" ? answer.noul : undefined;
|
|
903
|
+
if (relevance !== undefined) judged += 1;
|
|
904
|
+
scored.push({ ...candidate, ...(relevance === undefined ? {} : { relevance }) });
|
|
905
|
+
});
|
|
906
|
+
});
|
|
907
|
+
// Judged files by relevance; unjudged ones keep ripgrep's order behind them.
|
|
908
|
+
const ranked = judged > 0 ? [...scored].sort((a, b) => (b.relevance ?? -1) - (a.relevance ?? -1)) : scored;
|
|
909
|
+
const failure = unreachable ?? results.find((result) => result.failure)?.failure;
|
|
910
|
+
|
|
911
|
+
emitJevTelemetry(pi.events, "decision", {
|
|
912
|
+
route: "find",
|
|
913
|
+
outcome: judged > 0 ? "jev" : "fallback",
|
|
914
|
+
candidates: candidates.length,
|
|
915
|
+
confidence: confidenceBucket(ranked[0]?.relevance),
|
|
916
|
+
elapsedMs,
|
|
917
|
+
...(judged > 0 ? {} : { reason: failure ?? "missing" }),
|
|
918
|
+
});
|
|
919
|
+
return text(
|
|
920
|
+
renderFindResults({ ranked, total, judged, limit, model: config.model, elapsedMs, failure }),
|
|
921
|
+
// Shape only, like ask_jev: details persist in the session.
|
|
922
|
+
{ candidates: candidates.length, total, judged, elapsedMs, ...(failure ? { failure } : {}) },
|
|
923
|
+
);
|
|
924
|
+
},
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
// Offer the tools only while Jev can answer: a tool that always fails costs the agent a turn
|
|
928
|
+
// every time it reaches for it. Checked before every run, so `/tokenin add` brings them back
|
|
929
|
+
// without a reload. Silent on purpose: the one warning comes from whichever Jev path first
|
|
930
|
+
// needs the missing credential, not from every session of a user without a subscription.
|
|
931
|
+
// Only a removal made here is ever undone, so a loadout the user trimmed stays trimmed.
|
|
932
|
+
const jevTools = [ASK_JEV_TOOL, JEV_FIND_TOOL];
|
|
933
|
+
const hidden = new Set<string>();
|
|
934
|
+
pi.on("before_agent_start", async (_event, ctx) => {
|
|
935
|
+
const config = readJevAdvisoryConfig(getSettingsPath());
|
|
936
|
+
const route = config.routes.ask;
|
|
937
|
+
const offline = !route.enabled || (await jevUnavailable(ctx, jevConnection(config, route))) !== undefined;
|
|
938
|
+
const active = pi.getActiveTools();
|
|
939
|
+
if (offline) {
|
|
940
|
+
const dropping = jevTools.filter((name) => active.includes(name));
|
|
941
|
+
if (dropping.length > 0) {
|
|
942
|
+
pi.setActiveTools(active.filter((name) => !dropping.includes(name)));
|
|
943
|
+
for (const name of dropping) hidden.add(name);
|
|
944
|
+
}
|
|
945
|
+
} else if (hidden.size > 0) {
|
|
946
|
+
const restoring = [...hidden].filter((name) => !active.includes(name));
|
|
947
|
+
if (restoring.length > 0) pi.setActiveTools([...active, ...restoring]);
|
|
948
|
+
hidden.clear();
|
|
949
|
+
}
|
|
950
|
+
return undefined;
|
|
951
|
+
});
|
|
952
|
+
}
|