@floydous/pi-agent-kernel 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENT_KERNEL_SYS_PROMPT.md +1 -7
- package/CHANGELOG.md +48 -0
- package/README.md +6 -53
- package/config.example.toml +0 -8
- package/docs/README.md +1 -2
- package/docs/architecture.md +2 -3
- package/docs/configuration.md +10 -9
- package/docs/editing-and-verification.md +1 -3
- package/docs/retrieval.md +6 -11
- package/docs/testing.md +3 -3
- package/package.json +4 -2
- package/src/config/agent_kernel_command.ts +8 -39
- package/src/config/kernel_config.ts +30 -68
- package/src/config/toml.ts +6 -1
- package/src/editing/auto_heal.ts +1 -1
- package/src/editing/patch.ts +27 -16
- package/src/editing/post_edit_verification.ts +12 -10
- package/src/editing/smart_anchor.ts +7 -5
- package/src/editing/syntax-verify.ts +117 -15
- package/src/index.ts +15 -257
- package/src/retrieval/search_bm25.ts +4 -2
- package/src/retrieval/search_chunker.ts +1 -1
- package/src/retrieval/search_config.ts +4 -6
- package/src/retrieval/search_embedder.ts +3 -2
- package/src/retrieval/search_index.ts +82 -35
- package/src/retrieval/search_modal.ts +7 -8
- package/src/retrieval/{repomap.ts → tag_extractor.ts} +5 -292
- package/src/retrieval/tree_sitter_engine.ts +70 -34
- package/src/retrieval/workspace_walker.ts +6 -2
- package/src/safety/atomic_write.ts +1 -1
- package/src/safety/epistemic_guard.ts +122 -32
- package/src/safety/output_clamper.ts +57 -35
- package/src/tools/code_search_output.ts +2 -1
- package/src/tools/code_search_tool.ts +2 -5
- package/src/tools/context.ts +0 -14
- package/src/tools/edit_tool.ts +82 -115
- package/src/tools/read_tool.ts +42 -178
- package/src/ui/footer.ts +7 -7
- package/docs/lsp.md +0 -31
- package/src/lsp/adapters/base.ts +0 -42
- package/src/lsp/adapters/index.ts +0 -21
- package/src/lsp/adapters/python.ts +0 -37
- package/src/lsp/adapters/rust.ts +0 -35
- package/src/lsp/adapters/typescript.ts +0 -40
- package/src/lsp/index.ts +0 -13
- package/src/lsp/lsp_client.ts +0 -765
- package/src/lsp/lsp_detector.ts +0 -52
- package/src/lsp/lsp_formatter.ts +0 -323
- package/src/lsp/lsp_installer.ts +0 -320
- package/src/lsp/lsp_manager.ts +0 -202
- package/src/lsp/lsp_modal.ts +0 -675
- package/src/lsp/lsp_registry.ts +0 -600
- package/src/lsp/lsp_types.ts +0 -143
- package/src/retrieval/ast_search.ts +0 -999
- package/src/retrieval/symbol_reader.ts +0 -228
- package/src/tools/ast_search_formatter.ts +0 -161
- package/src/tools/ast_search_tool.ts +0 -146
- package/src/tools/lsp_tool.ts +0 -794
- package/src/tools/repo_map_tool.ts +0 -56
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
|
-
import { type SymbolDef, type FileTags } from "./
|
|
3
|
+
import { type SymbolDef, type FileTags } from "./tag_extractor";
|
|
4
4
|
import { kernelDebug } from "../safety/kernel_debug";
|
|
5
5
|
|
|
6
6
|
type WtsParser = any;
|
|
@@ -92,9 +92,19 @@ export class TreeSitterEngine {
|
|
|
92
92
|
|
|
93
93
|
this.initPromise = (async () => {
|
|
94
94
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
interface WtsModule {
|
|
96
|
+
Parser?: any;
|
|
97
|
+
Language?: any;
|
|
98
|
+
default?: {
|
|
99
|
+
Parser?: any;
|
|
100
|
+
Language?: any;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// SAFETY: web-tree-sitter exports its Parser and Language classes either directly
|
|
104
|
+
// or on the default export depending on whether it is imported via CJS or ESM loader.
|
|
105
|
+
const wts = (await import("web-tree-sitter")) as unknown as WtsModule;
|
|
106
|
+
this.ParserClass = wts.Parser || wts.default?.Parser || wts;
|
|
107
|
+
this.LanguageClass = wts.Language || wts.default?.Language;
|
|
98
108
|
|
|
99
109
|
let wtsDir = getGrammarsDir();
|
|
100
110
|
const localWtsWasm = path.join(wtsDir, "web-tree-sitter.wasm");
|
|
@@ -265,7 +275,8 @@ export class TreeSitterEngine {
|
|
|
265
275
|
}
|
|
266
276
|
// Stop traversal into function body
|
|
267
277
|
return;
|
|
268
|
-
}
|
|
278
|
+
}
|
|
279
|
+
if (type === "class_declaration" || type === "abstract_class_declaration") {
|
|
269
280
|
const nameNode = node.childForFieldName("name");
|
|
270
281
|
if (nameNode) {
|
|
271
282
|
definitions.push({
|
|
@@ -284,13 +295,15 @@ export class TreeSitterEngine {
|
|
|
284
295
|
}
|
|
285
296
|
}
|
|
286
297
|
return;
|
|
287
|
-
}
|
|
298
|
+
}
|
|
299
|
+
if (type === "ambient_declaration") {
|
|
288
300
|
// Unwrap ambient_declaration (e.g. declare function foo(), declare const X)
|
|
289
301
|
for (const child of node.namedChildren) {
|
|
290
302
|
visit(child, inFunctionScope);
|
|
291
303
|
}
|
|
292
304
|
return;
|
|
293
|
-
}
|
|
305
|
+
}
|
|
306
|
+
if (type === "function_signature") {
|
|
294
307
|
const nameNode = node.childForFieldName("name");
|
|
295
308
|
if (nameNode) {
|
|
296
309
|
definitions.push({
|
|
@@ -302,7 +315,8 @@ export class TreeSitterEngine {
|
|
|
302
315
|
});
|
|
303
316
|
}
|
|
304
317
|
return;
|
|
305
|
-
}
|
|
318
|
+
}
|
|
319
|
+
if (type === "interface_declaration") {
|
|
306
320
|
const nameNode = node.childForFieldName("name");
|
|
307
321
|
if (nameNode) {
|
|
308
322
|
definitions.push({
|
|
@@ -320,7 +334,8 @@ export class TreeSitterEngine {
|
|
|
320
334
|
}
|
|
321
335
|
}
|
|
322
336
|
return;
|
|
323
|
-
}
|
|
337
|
+
}
|
|
338
|
+
if (type === "method_signature" || type === "abstract_method_signature") {
|
|
324
339
|
const nameNode = node.childForFieldName("name");
|
|
325
340
|
if (nameNode) {
|
|
326
341
|
definitions.push({
|
|
@@ -332,7 +347,8 @@ export class TreeSitterEngine {
|
|
|
332
347
|
});
|
|
333
348
|
}
|
|
334
349
|
return;
|
|
335
|
-
}
|
|
350
|
+
}
|
|
351
|
+
if (type === "type_alias_declaration") {
|
|
336
352
|
const nameNode = node.childForFieldName("name");
|
|
337
353
|
if (nameNode) {
|
|
338
354
|
definitions.push({
|
|
@@ -344,7 +360,8 @@ export class TreeSitterEngine {
|
|
|
344
360
|
});
|
|
345
361
|
}
|
|
346
362
|
return;
|
|
347
|
-
}
|
|
363
|
+
}
|
|
364
|
+
if (type === "enum_declaration") {
|
|
348
365
|
const nameNode = node.childForFieldName("name");
|
|
349
366
|
if (nameNode) {
|
|
350
367
|
definitions.push({
|
|
@@ -356,7 +373,8 @@ export class TreeSitterEngine {
|
|
|
356
373
|
});
|
|
357
374
|
}
|
|
358
375
|
return;
|
|
359
|
-
}
|
|
376
|
+
}
|
|
377
|
+
if (type === "method_definition") {
|
|
360
378
|
const nameNode = node.childForFieldName("name");
|
|
361
379
|
if (nameNode) {
|
|
362
380
|
definitions.push({
|
|
@@ -369,7 +387,8 @@ export class TreeSitterEngine {
|
|
|
369
387
|
}
|
|
370
388
|
// Method definition has been recorded; do not traverse inside method body
|
|
371
389
|
return;
|
|
372
|
-
}
|
|
390
|
+
}
|
|
391
|
+
if ((type === "lexical_declaration" || type === "variable_declaration") && (ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" || ext === ".mjs" || ext === ".cjs")) {
|
|
373
392
|
// Only top-level variable declarations (in program or export statement)
|
|
374
393
|
const isTopLevel = node.parent?.type === "program" || node.parent?.type === "export_statement";
|
|
375
394
|
if (isTopLevel) {
|
|
@@ -380,9 +399,15 @@ export class TreeSitterEngine {
|
|
|
380
399
|
if (nameNode) {
|
|
381
400
|
const isFn = valueNode && (valueNode.type === "arrow_function" || valueNode.type === "function");
|
|
382
401
|
const isAllUpper = /^[A-Z0-9_]{2,}$/.test(nameNode.text);
|
|
402
|
+
let kind: "function" | "constant" | "variable" = "variable";
|
|
403
|
+
if (isFn) {
|
|
404
|
+
kind = "function";
|
|
405
|
+
} else if (isAllUpper) {
|
|
406
|
+
kind = "constant";
|
|
407
|
+
}
|
|
383
408
|
definitions.push({
|
|
384
409
|
name: nameNode.text,
|
|
385
|
-
kind
|
|
410
|
+
kind,
|
|
386
411
|
signature: getCleanSignature(node).replace(/^export\s+/, ""),
|
|
387
412
|
line: node.startPosition.row + 1,
|
|
388
413
|
endLine: node.endPosition.row + 1,
|
|
@@ -395,7 +420,7 @@ export class TreeSitterEngine {
|
|
|
395
420
|
}
|
|
396
421
|
|
|
397
422
|
// --- Python ---
|
|
398
|
-
|
|
423
|
+
if (type === "function_definition" && (ext === ".py")) {
|
|
399
424
|
const nameNode = node.childForFieldName("name");
|
|
400
425
|
if (nameNode) {
|
|
401
426
|
let isMethod = false;
|
|
@@ -416,14 +441,16 @@ export class TreeSitterEngine {
|
|
|
416
441
|
});
|
|
417
442
|
}
|
|
418
443
|
return;
|
|
419
|
-
}
|
|
444
|
+
}
|
|
445
|
+
if (type === "decorated_definition" && (ext === ".py")) {
|
|
420
446
|
// Unwrap python decorator to find inner function or class definition
|
|
421
447
|
const defNode = node.childForFieldName("definition") || node.namedChildren.find((c: any) => c.type === "function_definition" || c.type === "class_definition");
|
|
422
448
|
if (defNode) {
|
|
423
449
|
visit(defNode, inFunctionScope);
|
|
424
450
|
}
|
|
425
451
|
return;
|
|
426
|
-
}
|
|
452
|
+
}
|
|
453
|
+
if (type === "class_definition" && (ext === ".py")) {
|
|
427
454
|
const nameNode = node.childForFieldName("name");
|
|
428
455
|
if (nameNode) {
|
|
429
456
|
definitions.push({
|
|
@@ -441,13 +468,14 @@ export class TreeSitterEngine {
|
|
|
441
468
|
}
|
|
442
469
|
}
|
|
443
470
|
return;
|
|
444
|
-
}
|
|
471
|
+
}
|
|
472
|
+
if (type === "expression_statement" && (ext === ".py")) {
|
|
445
473
|
// Expression statements in Python do not define repository symbols
|
|
446
474
|
return;
|
|
447
475
|
}
|
|
448
476
|
|
|
449
477
|
// --- Rust ---
|
|
450
|
-
|
|
478
|
+
if (type === "function_item") {
|
|
451
479
|
const nameNode = node.childForFieldName("name");
|
|
452
480
|
if (nameNode) {
|
|
453
481
|
const isMethod = node.parent && node.parent.type === "declaration_list" && node.parent.parent && node.parent.parent.type === "impl_item";
|
|
@@ -460,7 +488,8 @@ export class TreeSitterEngine {
|
|
|
460
488
|
});
|
|
461
489
|
}
|
|
462
490
|
return;
|
|
463
|
-
}
|
|
491
|
+
}
|
|
492
|
+
if (type === "struct_item" || type === "enum_item" || type === "trait_item") {
|
|
464
493
|
const nameNode = node.childForFieldName("name");
|
|
465
494
|
if (nameNode) {
|
|
466
495
|
definitions.push({
|
|
@@ -489,7 +518,8 @@ export class TreeSitterEngine {
|
|
|
489
518
|
}
|
|
490
519
|
}
|
|
491
520
|
return;
|
|
492
|
-
}
|
|
521
|
+
}
|
|
522
|
+
if (type === "type_item") {
|
|
493
523
|
const nameNode = node.childForFieldName("name");
|
|
494
524
|
if (nameNode) {
|
|
495
525
|
definitions.push({
|
|
@@ -501,7 +531,8 @@ export class TreeSitterEngine {
|
|
|
501
531
|
});
|
|
502
532
|
}
|
|
503
533
|
return;
|
|
504
|
-
}
|
|
534
|
+
}
|
|
535
|
+
if (type === "const_item" || type === "static_item") {
|
|
505
536
|
const isTopLevel = node.parent?.type === "source_file" || node.parent?.type === "declaration_list";
|
|
506
537
|
if (isTopLevel) {
|
|
507
538
|
const nameNode = node.childForFieldName("name");
|
|
@@ -519,7 +550,7 @@ export class TreeSitterEngine {
|
|
|
519
550
|
}
|
|
520
551
|
|
|
521
552
|
// --- Go ---
|
|
522
|
-
|
|
553
|
+
if (type === "function_declaration") {
|
|
523
554
|
const nameNode = node.childForFieldName("name");
|
|
524
555
|
if (nameNode) {
|
|
525
556
|
definitions.push({
|
|
@@ -531,7 +562,8 @@ export class TreeSitterEngine {
|
|
|
531
562
|
});
|
|
532
563
|
}
|
|
533
564
|
return;
|
|
534
|
-
}
|
|
565
|
+
}
|
|
566
|
+
if (type === "method_declaration") {
|
|
535
567
|
const nameNode = node.childForFieldName("name");
|
|
536
568
|
if (nameNode) {
|
|
537
569
|
definitions.push({
|
|
@@ -543,7 +575,8 @@ export class TreeSitterEngine {
|
|
|
543
575
|
});
|
|
544
576
|
}
|
|
545
577
|
return;
|
|
546
|
-
}
|
|
578
|
+
}
|
|
579
|
+
if (type === "type_declaration") {
|
|
547
580
|
for (const child of node.namedChildren) {
|
|
548
581
|
if (child.type === "type_spec") {
|
|
549
582
|
const nameNode = child.childForFieldName("name");
|
|
@@ -580,7 +613,7 @@ export class TreeSitterEngine {
|
|
|
580
613
|
}
|
|
581
614
|
|
|
582
615
|
// --- C / C++ ---
|
|
583
|
-
|
|
616
|
+
if (type === "function_definition" && (ext === ".c" || ext === ".cpp" || ext === ".h" || ext === ".hpp" || ext === ".cc")) {
|
|
584
617
|
let isMethod = false;
|
|
585
618
|
let p = node.parent;
|
|
586
619
|
while (p) {
|
|
@@ -614,7 +647,8 @@ export class TreeSitterEngine {
|
|
|
614
647
|
});
|
|
615
648
|
}
|
|
616
649
|
return;
|
|
617
|
-
}
|
|
650
|
+
}
|
|
651
|
+
if (type === "class_specifier" || type === "struct_specifier") {
|
|
618
652
|
const nameNode = node.childForFieldName("name");
|
|
619
653
|
if (nameNode) {
|
|
620
654
|
definitions.push({
|
|
@@ -635,7 +669,7 @@ export class TreeSitterEngine {
|
|
|
635
669
|
}
|
|
636
670
|
|
|
637
671
|
// --- Java / C# ---
|
|
638
|
-
|
|
672
|
+
if (type === "constructor_declaration" || type === "method_declaration") {
|
|
639
673
|
const nameNode = node.childForFieldName("name");
|
|
640
674
|
if (nameNode) {
|
|
641
675
|
definitions.push({
|
|
@@ -647,7 +681,8 @@ export class TreeSitterEngine {
|
|
|
647
681
|
});
|
|
648
682
|
}
|
|
649
683
|
return;
|
|
650
|
-
}
|
|
684
|
+
}
|
|
685
|
+
if (type === "enum_declaration" && (ext === ".cs" || ext === ".java")) {
|
|
651
686
|
const nameNode = node.childForFieldName("name");
|
|
652
687
|
if (nameNode) {
|
|
653
688
|
definitions.push({
|
|
@@ -662,7 +697,7 @@ export class TreeSitterEngine {
|
|
|
662
697
|
}
|
|
663
698
|
|
|
664
699
|
// --- Ruby ---
|
|
665
|
-
|
|
700
|
+
if (type === "method" || type === "singleton_method") {
|
|
666
701
|
const nameNode = node.childForFieldName("name");
|
|
667
702
|
if (nameNode) {
|
|
668
703
|
let isMethod = false;
|
|
@@ -683,7 +718,8 @@ export class TreeSitterEngine {
|
|
|
683
718
|
});
|
|
684
719
|
}
|
|
685
720
|
return;
|
|
686
|
-
}
|
|
721
|
+
}
|
|
722
|
+
if ((type === "class" || type === "module") && ext === ".rb") {
|
|
687
723
|
const nameNode = node.childForFieldName("name");
|
|
688
724
|
if (nameNode) {
|
|
689
725
|
definitions.push({
|
|
@@ -704,7 +740,7 @@ export class TreeSitterEngine {
|
|
|
704
740
|
}
|
|
705
741
|
|
|
706
742
|
// --- PHP ---
|
|
707
|
-
|
|
743
|
+
if (type === "method_declaration") {
|
|
708
744
|
const nameNode = node.childForFieldName("name");
|
|
709
745
|
if (nameNode) {
|
|
710
746
|
definitions.push({
|
|
@@ -719,7 +755,7 @@ export class TreeSitterEngine {
|
|
|
719
755
|
}
|
|
720
756
|
|
|
721
757
|
// --- Bash ---
|
|
722
|
-
|
|
758
|
+
if (type === "function_definition" && (ext === ".sh" || ext === ".bash")) {
|
|
723
759
|
const nameNode = node.childForFieldName("name") || node.namedChildren.find((c: any) => c.type === "word");
|
|
724
760
|
if (nameNode) {
|
|
725
761
|
definitions.push({
|
|
@@ -734,7 +770,7 @@ export class TreeSitterEngine {
|
|
|
734
770
|
}
|
|
735
771
|
|
|
736
772
|
// --- Imports / Exports Aliases (TS/JS) ---
|
|
737
|
-
|
|
773
|
+
if (type === "export_statement" || type === "import_statement") {
|
|
738
774
|
const text = node.text;
|
|
739
775
|
if (text.includes(" as ")) {
|
|
740
776
|
const asMatch = text.match(/\b([a-zA-Z0-9_]+)\s+as\s+([a-zA-Z0-9_]+)\b/);
|
|
@@ -50,7 +50,9 @@ export function loadIgnoreFile(filePath: string): Ignore | null {
|
|
|
50
50
|
if (!fs.existsSync(filePath)) return null;
|
|
51
51
|
try {
|
|
52
52
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
53
|
-
const
|
|
53
|
+
const ignoreFn = typeof ignore === "function" ? ignore : (ignore as { default?: () => Ignore }).default;
|
|
54
|
+
if (typeof ignoreFn !== "function") return null;
|
|
55
|
+
const ig = ignoreFn();
|
|
54
56
|
return ig.add(content);
|
|
55
57
|
} catch {
|
|
56
58
|
return null;
|
|
@@ -69,7 +71,9 @@ export function isPathIgnored(stack: IgnoreFrame[], relPath: string, isDirectory
|
|
|
69
71
|
if (relToFrame.startsWith("/")) relToFrame = relToFrame.slice(1);
|
|
70
72
|
if (!relToFrame) continue;
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
// SAFETY: the 'ignore' library instance provides .test() returning { ignored, unignored }
|
|
75
|
+
// which is omitted from older @types/ignore declarations.
|
|
76
|
+
const res = (frame.ig as unknown as { test: (p: string) => { ignored: boolean; unignored: boolean } }).test(relToFrame);
|
|
73
77
|
if (res.unignored) return false;
|
|
74
78
|
if (res.ignored) return true;
|
|
75
79
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import { randomBytes } from "node:crypto";
|
|
4
|
-
import { kernelDebug } from "./kernel_debug";
|
|
4
|
+
import { kernelDebug } from "./kernel_debug.js";
|
|
5
5
|
|
|
6
6
|
function syncDirectory(directory: string): void {
|
|
7
7
|
if (process.platform === "win32") return;
|
|
@@ -10,6 +10,7 @@ import * as fs from "node:fs";
|
|
|
10
10
|
import * as os from "node:os";
|
|
11
11
|
import * as path from "node:path";
|
|
12
12
|
import { kernelDebug } from "./kernel_debug";
|
|
13
|
+
import { writeFileSyncAtomic } from "./atomic_write";
|
|
13
14
|
import { getPiHomeDir } from "../config/kernel_config";
|
|
14
15
|
|
|
15
16
|
/**
|
|
@@ -158,9 +159,7 @@ export type EvidenceProvenance =
|
|
|
158
159
|
| "read"
|
|
159
160
|
| "symbol"
|
|
160
161
|
| "search"
|
|
161
|
-
| "lsp"
|
|
162
162
|
| "bash"
|
|
163
|
-
| "ast_search"
|
|
164
163
|
| "code_search"
|
|
165
164
|
| "edit"
|
|
166
165
|
| "write";
|
|
@@ -575,6 +574,78 @@ export function getInspectionStateFilePath(sessionId: string, cwd = process.cwd(
|
|
|
575
574
|
return path.join(baseDir, "inspection-state.json");
|
|
576
575
|
}
|
|
577
576
|
|
|
577
|
+
interface PersistedSearchHit {
|
|
578
|
+
line: number;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
interface PersistedInspectionRecord {
|
|
582
|
+
kind: "read" | "search";
|
|
583
|
+
fingerprint: string;
|
|
584
|
+
coverage?: unknown;
|
|
585
|
+
provenance?: string;
|
|
586
|
+
query?: string;
|
|
587
|
+
searchHits?: PersistedSearchHit[];
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function isSafeWorkspaceRelPath(rel: string): boolean {
|
|
591
|
+
const normalized = rel.replace(/\\/g, "/");
|
|
592
|
+
return normalized !== ".." && !normalized.startsWith("../") && !path.isAbsolute(normalized);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function isSafePersistedRecord(rec: unknown): rec is PersistedInspectionRecord {
|
|
596
|
+
if (!rec || typeof rec !== "object" || Array.isArray(rec)) return false;
|
|
597
|
+
const r = rec as Record<string, unknown>;
|
|
598
|
+
if (r.kind !== "read" && r.kind !== "search") return false;
|
|
599
|
+
if (typeof r.fingerprint !== "string" || !/^[0-9a-f]{64}$/i.test(r.fingerprint)) return false;
|
|
600
|
+
if (r.provenance !== undefined && (typeof r.provenance !== "string" || r.provenance.length > 128)) return false;
|
|
601
|
+
if (r.query !== undefined && (typeof r.query !== "string" || r.query.length > 1024)) return false;
|
|
602
|
+
if (r.searchHits !== undefined) {
|
|
603
|
+
if (!Array.isArray(r.searchHits) || r.searchHits.length > 1000) return false;
|
|
604
|
+
for (const h of r.searchHits) {
|
|
605
|
+
if (!h || typeof h !== "object" || typeof (h as any).line !== "number" || !Number.isSafeInteger((h as any).line) || (h as any).line < 1) {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function parsePersistedCoverage(coverage: unknown, kind: "read" | "search"): EvidenceCoverage {
|
|
614
|
+
if (!coverage || typeof coverage !== "object") {
|
|
615
|
+
return { complete: false, ranges: [] };
|
|
616
|
+
}
|
|
617
|
+
const c = coverage as Record<string, unknown>;
|
|
618
|
+
const complete = kind === "read" && c.complete === true;
|
|
619
|
+
const totalLines =
|
|
620
|
+
typeof c.totalLines === "number" && Number.isSafeInteger(c.totalLines) && c.totalLines >= 0
|
|
621
|
+
? c.totalLines
|
|
622
|
+
: undefined;
|
|
623
|
+
const rawRanges = Array.isArray(c.ranges) ? c.ranges : [];
|
|
624
|
+
const validRanges: EvidenceRange[] = [];
|
|
625
|
+
for (const r of rawRanges.slice(0, 1000)) {
|
|
626
|
+
if (
|
|
627
|
+
r &&
|
|
628
|
+
typeof r === "object" &&
|
|
629
|
+
typeof (r as any).startLine === "number" &&
|
|
630
|
+
typeof (r as any).endLine === "number" &&
|
|
631
|
+
Number.isSafeInteger((r as any).startLine) &&
|
|
632
|
+
Number.isSafeInteger((r as any).endLine) &&
|
|
633
|
+
(r as any).startLine >= 1 &&
|
|
634
|
+
(r as any).endLine >= (r as any).startLine
|
|
635
|
+
) {
|
|
636
|
+
validRanges.push({
|
|
637
|
+
startLine: (r as any).startLine,
|
|
638
|
+
endLine: (r as any).endLine,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return {
|
|
643
|
+
complete,
|
|
644
|
+
ranges: normalizeRanges(validRanges, totalLines),
|
|
645
|
+
...(totalLines !== undefined ? { totalLines } : {}),
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
|
|
578
649
|
export function saveSessionInspectionState(
|
|
579
650
|
sessionId: string,
|
|
580
651
|
evidenceMap: Map<string, InspectionEvidence>,
|
|
@@ -587,10 +658,10 @@ export function saveSessionInspectionState(
|
|
|
587
658
|
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
588
659
|
|
|
589
660
|
const canonicalWorkspace = fs.existsSync(cwd) ? fs.realpathSync(cwd) : path.resolve(cwd);
|
|
590
|
-
const records: Record<string,
|
|
661
|
+
const records: Record<string, PersistedInspectionRecord> = {};
|
|
591
662
|
for (const [absPath, ev] of evidenceMap.entries()) {
|
|
592
663
|
const rel = path.relative(canonicalWorkspace, absPath).replace(/\\/g, "/");
|
|
593
|
-
if (
|
|
664
|
+
if (!isSafeWorkspaceRelPath(rel)) continue;
|
|
594
665
|
records[rel] = {
|
|
595
666
|
kind: ev.kind,
|
|
596
667
|
fingerprint: ev.fingerprint,
|
|
@@ -601,13 +672,10 @@ export function saveSessionInspectionState(
|
|
|
601
672
|
};
|
|
602
673
|
}
|
|
603
674
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
tmpPath,
|
|
675
|
+
writeFileSyncAtomic(
|
|
676
|
+
filePath,
|
|
607
677
|
JSON.stringify({ version: 1, workspace: canonicalWorkspace, records }, null, 2),
|
|
608
|
-
{ encoding: "utf8", mode: 0o600 },
|
|
609
678
|
);
|
|
610
|
-
fs.renameSync(tmpPath, filePath);
|
|
611
679
|
} catch (err) {
|
|
612
680
|
kernelDebug(err);
|
|
613
681
|
}
|
|
@@ -626,44 +694,51 @@ export function loadSessionInspectionState(
|
|
|
626
694
|
if (!stat.isFile() || stat.size > 2 * 1024 * 1024) return result;
|
|
627
695
|
|
|
628
696
|
const raw = fs.readFileSync(filePath, "utf8");
|
|
629
|
-
let data:
|
|
697
|
+
let data: { version?: unknown; records?: unknown };
|
|
630
698
|
try {
|
|
631
699
|
data = JSON.parse(raw);
|
|
632
700
|
} catch {
|
|
633
701
|
return result;
|
|
634
702
|
}
|
|
635
|
-
if (!data || data.version !== 1 || !data.records || typeof data.records !== "object")
|
|
703
|
+
if (!data || data.version !== 1 || !data.records || typeof data.records !== "object" || Array.isArray(data.records)) {
|
|
704
|
+
return result;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const entries = Object.entries(data.records as Record<string, unknown>);
|
|
708
|
+
if (entries.length > 1000) return result;
|
|
636
709
|
|
|
637
710
|
const canonicalWorkspace = fs.existsSync(cwd) ? fs.realpathSync(cwd) : path.resolve(cwd);
|
|
638
|
-
for (const [relPath,
|
|
639
|
-
if (!relPath || typeof relPath !== "string" || relPath.includes("\0")) continue;
|
|
711
|
+
for (const [relPath, rawRec] of entries) {
|
|
712
|
+
if (!relPath || typeof relPath !== "string" || relPath.length > 512 || relPath.includes("\0")) continue;
|
|
713
|
+
if (!isSafeWorkspaceRelPath(relPath)) continue;
|
|
714
|
+
if (!isSafePersistedRecord(rawRec)) continue;
|
|
715
|
+
|
|
640
716
|
const absPath = path.resolve(canonicalWorkspace, relPath);
|
|
641
|
-
const relCheck = path.relative(canonicalWorkspace, absPath)
|
|
642
|
-
if (
|
|
717
|
+
const relCheck = path.relative(canonicalWorkspace, absPath);
|
|
718
|
+
if (!isSafeWorkspaceRelPath(relCheck)) continue;
|
|
643
719
|
|
|
644
720
|
if (!fs.existsSync(absPath) || !fs.statSync(absPath).isFile()) continue;
|
|
645
721
|
// Ensure symlinks pointing outside workspace root are rejected
|
|
646
722
|
const canonicalFile = fs.realpathSync(absPath);
|
|
647
|
-
const realRelCheck = path.relative(canonicalWorkspace, canonicalFile)
|
|
648
|
-
if (
|
|
723
|
+
const realRelCheck = path.relative(canonicalWorkspace, canonicalFile);
|
|
724
|
+
if (!isSafeWorkspaceRelPath(realRelCheck)) continue;
|
|
649
725
|
|
|
650
726
|
const content = fs.readFileSync(absPath);
|
|
651
727
|
const currentFingerprint = crypto.createHash("sha256").update(content).digest("hex");
|
|
652
|
-
if (currentFingerprint !==
|
|
728
|
+
if (currentFingerprint !== rawRec.fingerprint) continue;
|
|
653
729
|
|
|
654
730
|
const normalized = process.platform === "win32" ? absPath.toLowerCase() : absPath;
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
.map((h: any) => ({ line: Math.floor(h.line) }))
|
|
731
|
+
if (result.has(normalized)) continue; // Prevent later duplicate entries from weakening prior verified evidence
|
|
732
|
+
const hits = Array.isArray(rawRec.searchHits)
|
|
733
|
+
? rawRec.searchHits.map((h) => ({ line: Math.floor(h.line) }))
|
|
659
734
|
: undefined;
|
|
660
735
|
|
|
661
736
|
result.set(normalized, {
|
|
662
|
-
kind:
|
|
663
|
-
fingerprint:
|
|
664
|
-
coverage:
|
|
665
|
-
provenance:
|
|
666
|
-
query:
|
|
737
|
+
kind: rawRec.kind,
|
|
738
|
+
fingerprint: rawRec.fingerprint,
|
|
739
|
+
coverage: parsePersistedCoverage(rawRec.coverage, rawRec.kind),
|
|
740
|
+
provenance: (rawRec.provenance || "read") as any,
|
|
741
|
+
query: rawRec.query,
|
|
667
742
|
searchHits: hits,
|
|
668
743
|
});
|
|
669
744
|
}
|
|
@@ -937,9 +1012,6 @@ export class EpistemicGuard {
|
|
|
937
1012
|
outputComplete: boolean,
|
|
938
1013
|
outputText = "",
|
|
939
1014
|
): void {
|
|
940
|
-
const isGrepOrRg =
|
|
941
|
-
/^\s*(?:grep|rg)(?:\.exe)?(?:\s|$)/i.test(command) ||
|
|
942
|
-
/\b(grep|rg)\b/i.test(command);
|
|
943
1015
|
const isPaging = /\b(head|tail|sed)\b/i.test(command);
|
|
944
1016
|
const isCat = /\bcat\b/i.test(command);
|
|
945
1017
|
|
|
@@ -1113,9 +1185,25 @@ export class EpistemicGuard {
|
|
|
1113
1185
|
// 2. Stale file drift check
|
|
1114
1186
|
const currentFingerprint = this.fingerprint(resolvedPath, workspace);
|
|
1115
1187
|
if (!currentFingerprint || currentFingerprint !== evidence.fingerprint) {
|
|
1188
|
+
let remedy = `read({ path: "${relPath}" })`;
|
|
1189
|
+
if (
|
|
1190
|
+
targetRanges &&
|
|
1191
|
+
targetRanges.length > 0 &&
|
|
1192
|
+
targetRanges[0] &&
|
|
1193
|
+
Number.isFinite(targetRanges[0].startLine) &&
|
|
1194
|
+
Number.isFinite(targetRanges[0].endLine) &&
|
|
1195
|
+
targetRanges[0].startLine >= 1 &&
|
|
1196
|
+
targetRanges[0].endLine >= targetRanges[0].startLine
|
|
1197
|
+
) {
|
|
1198
|
+
const start = Math.max(1, targetRanges[0].startLine - 2);
|
|
1199
|
+
const span = targetRanges[0].endLine - start + 3;
|
|
1200
|
+
const limit = Math.min(100, Math.max(15, span));
|
|
1201
|
+
const blockSuffix = targetRanges.length > 1 ? ` (block 1/${targetRanges.length})` : "";
|
|
1202
|
+
remedy = `read({ path: "${relPath}", offset: ${start}, limit: ${limit} })${blockSuffix}`;
|
|
1203
|
+
}
|
|
1116
1204
|
return {
|
|
1117
1205
|
allowed: false,
|
|
1118
|
-
reason: `[BLOCKED: Stale File Drift] File '${relPath}' changed on disk since last observed
|
|
1206
|
+
reason: `[BLOCKED: Stale File Drift] File '${relPath}' changed on disk since last observed. Re-synchronize target lines -> ${remedy}`,
|
|
1119
1207
|
};
|
|
1120
1208
|
}
|
|
1121
1209
|
|
|
@@ -1197,7 +1285,9 @@ export class EpistemicGuard {
|
|
|
1197
1285
|
ranges: [range],
|
|
1198
1286
|
});
|
|
1199
1287
|
isAuthorized = true;
|
|
1200
|
-
} catch {
|
|
1288
|
+
} catch (err) {
|
|
1289
|
+
kernelDebug(err);
|
|
1290
|
+
}
|
|
1201
1291
|
}
|
|
1202
1292
|
}
|
|
1203
1293
|
|