@floydous/pi-agent-kernel 0.4.0 → 0.4.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/AGENT_KERNEL_SYS_PROMPT.md +8 -4
- package/CHANGELOG.md +40 -87
- package/README.md +39 -74
- package/config.example.toml +1 -1
- package/docs/retrieval.md +2 -0
- package/package.json +7 -6
- package/src/config/agent_kernel_command.ts +29 -14
- package/src/config/kernel_config.ts +175 -30
- package/src/context/session_repair.ts +166 -85
- package/src/editing/patch.ts +28 -16
- package/src/editing/smart_anchor.ts +13 -29
- package/src/editing/syntax-verify.ts +35 -11
- package/src/index.ts +161 -141
- package/src/lsp/lsp_client.ts +101 -20
- package/src/lsp/lsp_formatter.ts +57 -22
- package/src/lsp/lsp_installer.ts +272 -487
- package/src/lsp/lsp_manager.ts +1 -1
- package/src/lsp/lsp_registry.ts +75 -46
- package/src/retrieval/search_bm25.ts +5 -2
- package/src/retrieval/search_cache.ts +69 -0
- package/src/retrieval/search_chunker.ts +12 -9
- package/src/retrieval/search_config.ts +20 -8
- package/src/retrieval/search_embedder.ts +8 -16
- package/src/retrieval/search_index.ts +552 -121
- package/src/safety/atomic_write.ts +59 -20
- package/src/safety/epistemic_guard.ts +440 -67
- package/src/safety/output_clamper.ts +316 -76
- package/src/tools/code_search_output.ts +59 -23
- package/src/tools/code_search_tool.ts +42 -46
- package/src/tools/context.ts +15 -1
- package/src/tools/edit_tool.ts +114 -36
- package/src/tools/lsp_tool.ts +5 -2
- package/src/tools/read_tool.ts +44 -3
package/src/lsp/lsp_client.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import crossSpawn from "cross-spawn";
|
|
2
|
+
import { type ChildProcess } from "node:child_process";
|
|
2
3
|
import * as fs from "node:fs";
|
|
3
4
|
import * as path from "node:path";
|
|
4
5
|
import {
|
|
@@ -16,6 +17,60 @@ import { getLspAdapter, type LspAdapter } from "./adapters";
|
|
|
16
17
|
import { kernelDebug } from "../safety/kernel_debug";
|
|
17
18
|
import { loadKernelConfig } from "../config";
|
|
18
19
|
|
|
20
|
+
const LSP_ENV_ALLOWLIST = new Set([
|
|
21
|
+
"path",
|
|
22
|
+
"pathext",
|
|
23
|
+
"home",
|
|
24
|
+
"userprofile",
|
|
25
|
+
"homedrive",
|
|
26
|
+
"homepath",
|
|
27
|
+
"temp",
|
|
28
|
+
"tmp",
|
|
29
|
+
"tmpdir",
|
|
30
|
+
"systemroot",
|
|
31
|
+
"windir",
|
|
32
|
+
"comspec",
|
|
33
|
+
"appdata",
|
|
34
|
+
"localappdata",
|
|
35
|
+
"programdata",
|
|
36
|
+
"programfiles",
|
|
37
|
+
"programfiles(x86)",
|
|
38
|
+
"lang",
|
|
39
|
+
"lc_all",
|
|
40
|
+
"java_home",
|
|
41
|
+
"jdk_home",
|
|
42
|
+
"goroot",
|
|
43
|
+
"gopath",
|
|
44
|
+
"cargo_home",
|
|
45
|
+
"rustup_home",
|
|
46
|
+
"pythonhome",
|
|
47
|
+
"pythonpath",
|
|
48
|
+
"virtual_env",
|
|
49
|
+
"node_path",
|
|
50
|
+
"ld_library_path",
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
export function buildLspEnvironment(
|
|
54
|
+
overrides: Record<string, string> = {},
|
|
55
|
+
): Record<string, string> {
|
|
56
|
+
const source = new Map<string, { key: string; value: string }>();
|
|
57
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
58
|
+
if (typeof value === "string") source.set(key.toLowerCase(), { key, value });
|
|
59
|
+
}
|
|
60
|
+
const result: Record<string, string> = {};
|
|
61
|
+
for (const [lowerKey, entry] of source) {
|
|
62
|
+
if (LSP_ENV_ALLOWLIST.has(lowerKey)) result[entry.key] = entry.value;
|
|
63
|
+
}
|
|
64
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
65
|
+
if (LSP_ENV_ALLOWLIST.has(key.toLowerCase())) result[key] = value;
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const MAX_LSP_STDERR_BYTES = 64 * 1024;
|
|
71
|
+
export const MAX_LSP_DOCUMENT_BYTES = 5 * 1024 * 1024;
|
|
72
|
+
export const MAX_LSP_DIAGNOSTICS = 200;
|
|
73
|
+
|
|
19
74
|
export interface StdioLspClientOptions {
|
|
20
75
|
command: string;
|
|
21
76
|
args: string[];
|
|
@@ -84,11 +139,12 @@ export class StdioLspClient {
|
|
|
84
139
|
this.touch();
|
|
85
140
|
|
|
86
141
|
try {
|
|
87
|
-
this.process =
|
|
142
|
+
this.process = crossSpawn(this.options.command, this.options.args, {
|
|
88
143
|
cwd: this.rootDir,
|
|
89
|
-
env:
|
|
144
|
+
env: buildLspEnvironment(this.options.env),
|
|
90
145
|
stdio: ["pipe", "pipe", "pipe"],
|
|
91
|
-
shell:
|
|
146
|
+
shell: false,
|
|
147
|
+
windowsHide: true,
|
|
92
148
|
});
|
|
93
149
|
|
|
94
150
|
this.process.stdout?.on("data", (chunk: Buffer) => {
|
|
@@ -97,7 +153,9 @@ export class StdioLspClient {
|
|
|
97
153
|
|
|
98
154
|
let serverStderr = "";
|
|
99
155
|
this.process.stderr?.on("data", (chunk: Buffer) => {
|
|
100
|
-
serverStderr
|
|
156
|
+
if (serverStderr.length < MAX_LSP_STDERR_BYTES) {
|
|
157
|
+
serverStderr = `${serverStderr}${chunk.toString("utf8")}`.slice(0, MAX_LSP_STDERR_BYTES);
|
|
158
|
+
}
|
|
101
159
|
});
|
|
102
160
|
|
|
103
161
|
this.process.on("error", (err) => {
|
|
@@ -321,6 +379,9 @@ export class StdioLspClient {
|
|
|
321
379
|
if (notif.method === "textDocument/publishDiagnostics") {
|
|
322
380
|
const rawUri = notif.params?.uri;
|
|
323
381
|
let diagnostics = notif.params?.diagnostics || [];
|
|
382
|
+
if (diagnostics.length > MAX_LSP_DIAGNOSTICS) {
|
|
383
|
+
diagnostics = diagnostics.slice(0, MAX_LSP_DIAGNOSTICS);
|
|
384
|
+
}
|
|
324
385
|
if (rawUri) {
|
|
325
386
|
const uri = normalizeUri(rawUri);
|
|
326
387
|
if (this.adapter.transformDiagnostics) {
|
|
@@ -406,12 +467,14 @@ export class StdioLspClient {
|
|
|
406
467
|
*/
|
|
407
468
|
public async openDocument(filePath: string, content?: string): Promise<void> {
|
|
408
469
|
const uri = pathToUri(filePath);
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
470
|
+
const fileStat = fs.statSync(filePath);
|
|
471
|
+
if (!fileStat.isFile() || fileStat.size > MAX_LSP_DOCUMENT_BYTES) {
|
|
472
|
+
throw new Error(`LSP document exceeds the ${MAX_LSP_DOCUMENT_BYTES}-byte limit`);
|
|
473
|
+
}
|
|
474
|
+
const text = content === undefined ? fs.readFileSync(filePath, "utf8") : content;
|
|
475
|
+
if (Buffer.byteLength(text, "utf8") > MAX_LSP_DOCUMENT_BYTES) {
|
|
476
|
+
throw new Error(`LSP document exceeds the ${MAX_LSP_DOCUMENT_BYTES}-byte limit`);
|
|
477
|
+
}
|
|
415
478
|
|
|
416
479
|
if (this.openDocuments.has(uri)) {
|
|
417
480
|
const existing = this.openDocuments.get(uri);
|
|
@@ -652,18 +715,36 @@ export class StdioLspClient {
|
|
|
652
715
|
* Graceful shutdown and termination
|
|
653
716
|
*/
|
|
654
717
|
public async stop(): Promise<void> {
|
|
655
|
-
if (this.state === "stopped") return;
|
|
718
|
+
if (this.state === "stopped" && !this.process) return;
|
|
656
719
|
this.state = "shutting_down";
|
|
720
|
+
const child = this.process;
|
|
657
721
|
|
|
658
722
|
try {
|
|
659
|
-
if (
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
723
|
+
if (child) {
|
|
724
|
+
const exited = new Promise<void>((resolve) => {
|
|
725
|
+
child.once("exit", () => resolve());
|
|
726
|
+
child.once("error", () => resolve());
|
|
727
|
+
});
|
|
728
|
+
try {
|
|
729
|
+
await Promise.race([
|
|
730
|
+
this.sendRequest("shutdown", undefined, 1000).then(() => {
|
|
731
|
+
this.sendNotification("exit");
|
|
732
|
+
}),
|
|
733
|
+
new Promise((resolve) => setTimeout(resolve, 1000)),
|
|
734
|
+
]);
|
|
735
|
+
} catch (error) {
|
|
736
|
+
kernelDebug(error);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
await Promise.race([exited, new Promise((resolve) => setTimeout(resolve, 250))]);
|
|
740
|
+
if (child.exitCode === null && child.signalCode === null && !child.killed) {
|
|
741
|
+
child.kill();
|
|
742
|
+
}
|
|
743
|
+
await Promise.race([exited, new Promise((resolve) => setTimeout(resolve, 2000))]);
|
|
744
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
745
|
+
child.kill("SIGKILL");
|
|
746
|
+
await Promise.race([exited, new Promise((resolve) => setTimeout(resolve, 1000))]);
|
|
747
|
+
}
|
|
667
748
|
}
|
|
668
749
|
} catch (e) {
|
|
669
750
|
kernelDebug(e);
|
package/src/lsp/lsp_formatter.ts
CHANGED
|
@@ -66,6 +66,19 @@ function severityLabel(severity?: LspDiagnosticSeverity): string {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
const MAX_DEFINITIONS = 200;
|
|
70
|
+
const MAX_REFERENCES = 200;
|
|
71
|
+
const MAX_REFERENCE_FILE_BYTES = 1024 * 1024;
|
|
72
|
+
const MAX_SYMBOLS = 100;
|
|
73
|
+
const MAX_SYMBOL_DEPTH = 10;
|
|
74
|
+
const MAX_HOVER_CHARS = 32 * 1024;
|
|
75
|
+
const MAX_DIAGNOSTIC_MESSAGE_CHARS = 2 * 1024;
|
|
76
|
+
|
|
77
|
+
function clipText(value: unknown, limit: number): string {
|
|
78
|
+
const text = String(value ?? "");
|
|
79
|
+
return text.length > limit ? `${text.slice(0, limit)}…` : text;
|
|
80
|
+
}
|
|
81
|
+
|
|
69
82
|
/**
|
|
70
83
|
* Symbol kind name lookup
|
|
71
84
|
*/
|
|
@@ -85,15 +98,17 @@ export function formatDiagnostics(
|
|
|
85
98
|
return "";
|
|
86
99
|
}
|
|
87
100
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
for (const d of diagnostics) {
|
|
101
|
+
const visible = diagnostics.slice(0, 200);
|
|
102
|
+
const lines = visible.map((d) => {
|
|
91
103
|
const sev = severityLabel(d.severity);
|
|
92
104
|
const line = d.range.start.line + 1; // Convert 0-indexed to 1-indexed
|
|
93
105
|
const col = d.range.start.character + 1;
|
|
94
106
|
const codeStr = d.code !== undefined ? ` [${d.code}]` : "";
|
|
95
107
|
const srcStr = d.source ? ` (${d.source})` : "";
|
|
96
|
-
|
|
108
|
+
return `- [${line}:${col}] [${sev}]${codeStr} ${clipText(d.message, MAX_DIAGNOSTIC_MESSAGE_CHARS)}${srcStr}`;
|
|
109
|
+
});
|
|
110
|
+
if (diagnostics.length > visible.length) {
|
|
111
|
+
lines.push(`[Truncated: ${visible.length}/${diagnostics.length} diagnostics]`);
|
|
97
112
|
}
|
|
98
113
|
|
|
99
114
|
return lines.join("\n");
|
|
@@ -131,7 +146,8 @@ export function formatDefinitions(
|
|
|
131
146
|
return "Definition not found.";
|
|
132
147
|
}
|
|
133
148
|
|
|
134
|
-
const
|
|
149
|
+
const visible = locations.slice(0, MAX_DEFINITIONS);
|
|
150
|
+
const lines = visible.map((loc) => {
|
|
135
151
|
const filePath = uriToPath(loc.uri);
|
|
136
152
|
const rawDisplay = cwd ? path.relative(cwd, filePath) || filePath : filePath;
|
|
137
153
|
const displayPath = rawDisplay.replace(/\\/g, "/");
|
|
@@ -139,6 +155,9 @@ export function formatDefinitions(
|
|
|
139
155
|
const col = loc.range.start.character + 1;
|
|
140
156
|
return `${displayPath}:${line}:${col}`;
|
|
141
157
|
});
|
|
158
|
+
if (locations.length > visible.length) {
|
|
159
|
+
lines.push(`[Truncated: ${visible.length}/${locations.length} definitions]`);
|
|
160
|
+
}
|
|
142
161
|
|
|
143
162
|
return lines.join("\n");
|
|
144
163
|
}
|
|
@@ -156,11 +175,11 @@ export function windowAround(
|
|
|
156
175
|
const s = Math.max(0, Math.min(startCol0, normLine.length));
|
|
157
176
|
const e = Math.max(s, Math.min(endCol0, normLine.length));
|
|
158
177
|
const symLen = Math.max(1, e - s);
|
|
159
|
-
const effectiveBudget = Math.max(budget, symLen);
|
|
178
|
+
const effectiveBudget = Math.min(4096, Math.max(budget, symLen));
|
|
160
179
|
const pad = Math.max(0, effectiveBudget - symLen);
|
|
161
180
|
const left = Math.max(0, s - Math.floor(pad / 2));
|
|
162
181
|
const right = Math.min(normLine.length, e + Math.ceil(pad / 2));
|
|
163
|
-
const snippet = normLine.slice(left, right).trim();
|
|
182
|
+
const snippet = normLine.slice(left, right).trim().slice(0, 4096);
|
|
164
183
|
const prefix = left > 0 ? "... " : "";
|
|
165
184
|
const suffix = right < normLine.length ? " ..." : "";
|
|
166
185
|
return `${prefix}${snippet}${suffix}`.trim();
|
|
@@ -187,9 +206,12 @@ export function formatReferences(
|
|
|
187
206
|
}
|
|
188
207
|
try {
|
|
189
208
|
if (fs.existsSync(filePath)) {
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
209
|
+
const stat = fs.statSync(filePath);
|
|
210
|
+
if (stat.isFile() && stat.size <= MAX_REFERENCE_FILE_BYTES) {
|
|
211
|
+
const lines = fs.readFileSync(filePath, "utf8").split("\n");
|
|
212
|
+
fileLinesCache.set(filePath, lines);
|
|
213
|
+
return lines;
|
|
214
|
+
}
|
|
193
215
|
}
|
|
194
216
|
} catch {
|
|
195
217
|
// Fall through to null for inaccessible/virtual files
|
|
@@ -198,7 +220,8 @@ export function formatReferences(
|
|
|
198
220
|
return null;
|
|
199
221
|
};
|
|
200
222
|
|
|
201
|
-
const
|
|
223
|
+
const visible = references.slice(0, MAX_REFERENCES);
|
|
224
|
+
const lines = visible.map((loc) => {
|
|
202
225
|
const filePath = uriToPath(loc.uri);
|
|
203
226
|
const rawDisplay = cwd ? path.relative(cwd, filePath) || filePath : filePath;
|
|
204
227
|
const displayPath = rawDisplay.replace(/\\/g, "/");
|
|
@@ -221,7 +244,9 @@ export function formatReferences(
|
|
|
221
244
|
const snippet = windowAround(rawLine, startCol0, endCol0, 50);
|
|
222
245
|
return snippet ? `${baseLoc}: ${snippet}` : baseLoc;
|
|
223
246
|
});
|
|
224
|
-
|
|
247
|
+
if (references.length > visible.length) {
|
|
248
|
+
lines.push(`[Truncated: ${visible.length}/${references.length} references]`);
|
|
249
|
+
}
|
|
225
250
|
return lines.join("\n");
|
|
226
251
|
}
|
|
227
252
|
|
|
@@ -245,7 +270,10 @@ export function formatHover(hover: LspHover | null | undefined): string {
|
|
|
245
270
|
const rawText = extractText(hover.contents).trim();
|
|
246
271
|
// Strip transient LSP loading state markers (e.g. tsserver / vtsls prefixing "(loading...) ")
|
|
247
272
|
const cleanText = rawText.replace(/\(loading\.\.\.\)\s*/g, "").trim();
|
|
248
|
-
|
|
273
|
+
if (!cleanText) return "No hover information available at this position.";
|
|
274
|
+
return cleanText.length > MAX_HOVER_CHARS
|
|
275
|
+
? `${cleanText.slice(0, MAX_HOVER_CHARS)}\n[Hover truncated]`
|
|
276
|
+
: cleanText;
|
|
249
277
|
}
|
|
250
278
|
|
|
251
279
|
/**
|
|
@@ -259,30 +287,37 @@ export function formatDocumentSymbols(
|
|
|
259
287
|
}
|
|
260
288
|
|
|
261
289
|
const lines: string[] = [];
|
|
290
|
+
let emitted = 0;
|
|
291
|
+
let truncated = false;
|
|
262
292
|
|
|
263
293
|
const renderSymbol = (s: any, depth = 0) => {
|
|
294
|
+
if (emitted >= MAX_SYMBOLS || depth > MAX_SYMBOL_DEPTH) {
|
|
295
|
+
truncated = true;
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
264
298
|
const indent = " ".repeat(depth);
|
|
265
299
|
const kind = symbolKindLabel(s.kind);
|
|
266
300
|
const range = s.range || s.location?.range;
|
|
267
301
|
const line = range ? range.start.line + 1 : 1;
|
|
268
|
-
const detail = s.detail ? ` (${s.detail})` : "";
|
|
269
|
-
lines.push(`${indent}${line}: [${kind}] ${s.name}${detail}`);
|
|
302
|
+
const detail = s.detail ? ` (${clipText(s.detail, 500)})` : "";
|
|
303
|
+
lines.push(`${indent}${line}: [${kind}] ${clipText(s.name, 200)}${detail}`);
|
|
304
|
+
emitted++;
|
|
270
305
|
|
|
271
306
|
if (s.children && Array.isArray(s.children)) {
|
|
272
|
-
for (const child of s.children)
|
|
273
|
-
renderSymbol(child, depth + 1);
|
|
274
|
-
}
|
|
307
|
+
for (const child of s.children) renderSymbol(child, depth + 1);
|
|
275
308
|
}
|
|
276
309
|
};
|
|
277
310
|
|
|
278
311
|
for (const s of symbols) {
|
|
312
|
+
if (emitted >= MAX_SYMBOLS) {
|
|
313
|
+
truncated = true;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
279
316
|
renderSymbol(s, 0);
|
|
280
317
|
}
|
|
281
318
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return lines.slice(0, 100).join("\n") + `\n\n[Truncated: 100/${lines.length} symbols shown]`;
|
|
319
|
+
if (truncated) {
|
|
320
|
+
lines.push(`[Truncated: ${emitted}/${symbols.length} symbols shown]`);
|
|
285
321
|
}
|
|
286
|
-
|
|
287
322
|
return lines.join("\n");
|
|
288
323
|
}
|