@floydous/pi-agent-kernel 0.3.31 → 0.4.1
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 +6 -4
- package/CHANGELOG.md +35 -4
- package/README.md +39 -74
- package/config.example.toml +36 -0
- package/docs/configuration.md +11 -3
- package/docs/retrieval.md +17 -1
- package/docs/testing.md +5 -3
- package/package.json +9 -7
- package/src/config/agent_kernel_command.ts +91 -91
- package/src/config/kernel_config.ts +179 -43
- package/src/config/toml.ts +15 -4
- package/src/context/session_repair.ts +179 -80
- package/src/editing/patch.ts +28 -16
- package/src/editing/smart_anchor.ts +29 -36
- package/src/editing/syntax-verify.ts +52 -18
- package/src/index.ts +257 -219
- package/src/lsp/lsp_client.ts +138 -23
- package/src/lsp/lsp_formatter.ts +61 -24
- package/src/lsp/lsp_installer.ts +277 -561
- package/src/lsp/lsp_manager.ts +1 -1
- package/src/lsp/lsp_registry.ts +110 -73
- package/src/retrieval/ast_search.ts +136 -172
- package/src/retrieval/repomap.ts +8 -47
- package/src/retrieval/search_bm25.ts +5 -2
- package/src/retrieval/search_cache.ts +69 -0
- package/src/retrieval/search_chunker.ts +21 -43
- package/src/retrieval/search_config.ts +75 -18
- package/src/retrieval/search_embedder.ts +8 -16
- package/src/retrieval/search_index.ts +923 -123
- package/src/retrieval/workspace_walker.ts +262 -0
- package/src/safety/atomic_write.ts +59 -20
- package/src/safety/epistemic_guard.ts +163 -43
- package/src/safety/output_clamper.ts +316 -74
- 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 +124 -43
- package/src/tools/lsp_tool.ts +17 -4
- package/src/tools/read_tool.ts +48 -7
- package/config.toml +0 -31
|
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
|
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import { extractFileTags } from "./repomap";
|
|
4
4
|
import { kernelDebug } from "../safety/kernel_debug";
|
|
5
|
+
import { walkWorkspaceFiles, DEFAULT_IGNORED_DIRS } from "./workspace_walker";
|
|
5
6
|
|
|
6
7
|
export interface AstQueryResult {
|
|
7
8
|
filePath: string;
|
|
@@ -104,18 +105,7 @@ const SUPPORTED_EXTENSIONS = new Set([
|
|
|
104
105
|
".lua",
|
|
105
106
|
]);
|
|
106
107
|
|
|
107
|
-
const IGNORED_DIRS =
|
|
108
|
-
"node_modules",
|
|
109
|
-
".git",
|
|
110
|
-
"target",
|
|
111
|
-
"dist",
|
|
112
|
-
"build",
|
|
113
|
-
".next",
|
|
114
|
-
"__pycache__",
|
|
115
|
-
".pytest_cache",
|
|
116
|
-
".venv",
|
|
117
|
-
"venv",
|
|
118
|
-
]);
|
|
108
|
+
const IGNORED_DIRS = DEFAULT_IGNORED_DIRS;
|
|
119
109
|
|
|
120
110
|
/**
|
|
121
111
|
* Extract top-level symbols from a single file using fast AST tagging
|
|
@@ -204,88 +194,73 @@ export function findSymbolReferences(
|
|
|
204
194
|
const escaped = cleanSym.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
205
195
|
const regex = new RegExp(`\\b${escaped}\\b`, "g");
|
|
206
196
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
try {
|
|
228
|
-
const content = fs.readFileSync(fullPath, "utf-8");
|
|
229
|
-
const lines = content.split("\n");
|
|
230
|
-
let inBlockComment = false;
|
|
231
|
-
|
|
232
|
-
for (let i = 0; i < lines.length; i++) {
|
|
233
|
-
if (results.length >= maxResults) break;
|
|
234
|
-
const lineText = lines[i];
|
|
235
|
-
const trimmed = lineText.trim();
|
|
236
|
-
|
|
237
|
-
// Block comment handling (/* ... */)
|
|
238
|
-
if (inBlockComment) {
|
|
239
|
-
if (trimmed.includes("*/")) {
|
|
240
|
-
inBlockComment = false;
|
|
241
|
-
}
|
|
242
|
-
continue;
|
|
197
|
+
walkWorkspaceFiles({
|
|
198
|
+
rootDir,
|
|
199
|
+
extensions: SUPPORTED_EXTENSIONS,
|
|
200
|
+
includeGithub: false,
|
|
201
|
+
onFile: (fullPath) => {
|
|
202
|
+
if (results.length >= maxResults) return false;
|
|
203
|
+
try {
|
|
204
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
205
|
+
const lines = content.split("\n");
|
|
206
|
+
let inBlockComment = false;
|
|
207
|
+
|
|
208
|
+
for (let i = 0; i < lines.length; i++) {
|
|
209
|
+
if (results.length >= maxResults) return false;
|
|
210
|
+
const lineText = lines[i];
|
|
211
|
+
const trimmed = lineText.trim();
|
|
212
|
+
|
|
213
|
+
// Block comment handling (/* ... */)
|
|
214
|
+
if (inBlockComment) {
|
|
215
|
+
if (trimmed.includes("*/")) {
|
|
216
|
+
inBlockComment = false;
|
|
243
217
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (trimmed.startsWith("/*")) {
|
|
221
|
+
if (!trimmed.includes("*/")) {
|
|
222
|
+
inBlockComment = true;
|
|
249
223
|
}
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Skip lines starting with comment markers
|
|
228
|
+
if (
|
|
229
|
+
trimmed.startsWith("//") ||
|
|
230
|
+
trimmed.startsWith("///") ||
|
|
231
|
+
trimmed.startsWith("//!") ||
|
|
232
|
+
trimmed.startsWith("#") ||
|
|
233
|
+
trimmed.startsWith("*") ||
|
|
234
|
+
trimmed.startsWith("--")
|
|
235
|
+
) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
250
238
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
trimmed.startsWith("#") ||
|
|
257
|
-
trimmed.startsWith("*") ||
|
|
258
|
-
trimmed.startsWith("--")
|
|
259
|
-
) {
|
|
239
|
+
let match;
|
|
240
|
+
regex.lastIndex = 0;
|
|
241
|
+
while ((match = regex.exec(lineText)) !== null) {
|
|
242
|
+
// Filter out matches inside inline comments or string literals
|
|
243
|
+
if (isInsideCommentOrString(lineText, match.index)) {
|
|
260
244
|
continue;
|
|
261
245
|
}
|
|
262
246
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
results.push({
|
|
272
|
-
filePath:
|
|
273
|
-
path.relative(rootDir, fullPath).replace(/\\/g, "/") || fullPath,
|
|
274
|
-
line: i + 1,
|
|
275
|
-
column: match.index + 1,
|
|
276
|
-
lineText: lineText.trim(),
|
|
277
|
-
});
|
|
278
|
-
if (results.length >= maxResults) break;
|
|
279
|
-
}
|
|
247
|
+
results.push({
|
|
248
|
+
filePath:
|
|
249
|
+
path.relative(rootDir, fullPath).replace(/\\/g, "/") || fullPath,
|
|
250
|
+
line: i + 1,
|
|
251
|
+
column: match.index + 1,
|
|
252
|
+
lineText: lineText.trim(),
|
|
253
|
+
});
|
|
254
|
+
if (results.length >= maxResults) return false;
|
|
280
255
|
}
|
|
281
|
-
} catch (e) {
|
|
282
|
-
kernelDebug(e);
|
|
283
256
|
}
|
|
257
|
+
} catch (e) {
|
|
258
|
+
kernelDebug(e);
|
|
284
259
|
}
|
|
285
|
-
|
|
286
|
-
|
|
260
|
+
return results.length < maxResults;
|
|
261
|
+
},
|
|
262
|
+
});
|
|
287
263
|
|
|
288
|
-
walk(rootDir);
|
|
289
264
|
return results;
|
|
290
265
|
}
|
|
291
266
|
|
|
@@ -747,100 +722,86 @@ export function searchAstSymbols(
|
|
|
747
722
|
: cleanQueryName;
|
|
748
723
|
const includeBody = query.includeBody ?? false;
|
|
749
724
|
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
725
|
+
walkWorkspaceFiles({
|
|
726
|
+
rootDir,
|
|
727
|
+
extensions: SUPPORTED_EXTENSIONS,
|
|
728
|
+
includeGithub: false,
|
|
729
|
+
onFile: (fullPath) => {
|
|
730
|
+
const relPath = path.relative(rootDir, fullPath).replace(/\\/g, "/");
|
|
731
|
+
if (
|
|
732
|
+
query.filePattern &&
|
|
733
|
+
!relPath
|
|
734
|
+
.toLowerCase()
|
|
735
|
+
.includes(query.filePattern.replace(/\\/g, "/").toLowerCase())
|
|
736
|
+
) {
|
|
737
|
+
return;
|
|
761
738
|
}
|
|
762
|
-
const
|
|
763
|
-
if (
|
|
764
|
-
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
) {
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
const fileDefs: AstQueryResult[] = [];
|
|
784
|
-
|
|
785
|
-
for (const def of tags.definitions) {
|
|
786
|
-
let codeBlock: string | undefined;
|
|
787
|
-
let bodyTruncated = false;
|
|
788
|
-
let visibleEndLine: number | undefined;
|
|
789
|
-
if (includeBody) {
|
|
790
|
-
const start = Math.max(0, def.line - 1);
|
|
791
|
-
const end = Math.min(lines.length, start + 25);
|
|
792
|
-
codeBlock = lines.slice(start, end).join("\n");
|
|
793
|
-
visibleEndLine = end;
|
|
794
|
-
bodyTruncated = end < lines.length;
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
const item: AstQueryResult = {
|
|
798
|
-
filePath: relPath,
|
|
799
|
-
name: def.name,
|
|
800
|
-
kind: def.kind,
|
|
801
|
-
signature: def.signature,
|
|
802
|
-
line: def.line,
|
|
803
|
-
endLine: def.endLine && def.endLine >= def.line ? def.endLine : findSymbolEndLine(lines, def.line - 1, ext),
|
|
804
|
-
codeBlock,
|
|
805
|
-
visibleEndLine,
|
|
806
|
-
bodyTruncated: includeBody ? bodyTruncated : undefined,
|
|
807
|
-
aliasedFrom: def.aliasedFrom,
|
|
808
|
-
};
|
|
809
|
-
|
|
810
|
-
fileDefs.push(item);
|
|
811
|
-
|
|
812
|
-
if (query.kind) {
|
|
813
|
-
const qKind = query.kind.toLowerCase();
|
|
814
|
-
const dKind = def.kind.toLowerCase();
|
|
815
|
-
const matchKind =
|
|
816
|
-
qKind === dKind ||
|
|
817
|
-
(qKind === "struct" && dKind === "class") ||
|
|
818
|
-
(qKind === "trait" && dKind === "class") ||
|
|
819
|
-
(qKind === "constant" && (dKind === "constant" || dKind === "variable")) ||
|
|
820
|
-
(qKind === "variable" && (dKind === "variable" || dKind === "constant"));
|
|
821
|
-
if (!matchKind) continue;
|
|
822
|
-
}
|
|
739
|
+
const ext = path.extname(fullPath).toLowerCase();
|
|
740
|
+
if (!SUPPORTED_EXTENSIONS.has(ext)) {
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
try {
|
|
744
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
745
|
+
const tags = extractFileTags(fullPath, content);
|
|
746
|
+
const lines = content.split("\n");
|
|
747
|
+
const fileDefs: AstQueryResult[] = [];
|
|
748
|
+
|
|
749
|
+
for (const def of tags.definitions) {
|
|
750
|
+
let codeBlock: string | undefined;
|
|
751
|
+
let bodyTruncated = false;
|
|
752
|
+
let visibleEndLine: number | undefined;
|
|
753
|
+
if (includeBody) {
|
|
754
|
+
const start = Math.max(0, def.line - 1);
|
|
755
|
+
const end = Math.min(lines.length, start + 25);
|
|
756
|
+
codeBlock = lines.slice(start, end).join("\n");
|
|
757
|
+
visibleEndLine = end;
|
|
758
|
+
bodyTruncated = end < lines.length;
|
|
759
|
+
}
|
|
823
760
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
761
|
+
const item: AstQueryResult = {
|
|
762
|
+
filePath: relPath,
|
|
763
|
+
name: def.name,
|
|
764
|
+
kind: def.kind,
|
|
765
|
+
signature: def.signature,
|
|
766
|
+
line: def.line,
|
|
767
|
+
endLine: def.endLine && def.endLine >= def.line ? def.endLine : findSymbolEndLine(lines, def.line - 1, ext),
|
|
768
|
+
codeBlock,
|
|
769
|
+
visibleEndLine,
|
|
770
|
+
bodyTruncated: includeBody ? bodyTruncated : undefined,
|
|
771
|
+
aliasedFrom: def.aliasedFrom,
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
fileDefs.push(item);
|
|
775
|
+
|
|
776
|
+
if (query.kind) {
|
|
777
|
+
const qKind = query.kind.toLowerCase();
|
|
778
|
+
const dKind = def.kind.toLowerCase();
|
|
779
|
+
const matchKind =
|
|
780
|
+
qKind === dKind ||
|
|
781
|
+
(qKind === "struct" && dKind === "class") ||
|
|
782
|
+
(qKind === "trait" && dKind === "class") ||
|
|
783
|
+
(qKind === "constant" && (dKind === "constant" || dKind === "variable")) ||
|
|
784
|
+
(qKind === "variable" && (dKind === "variable" || dKind === "constant"));
|
|
785
|
+
if (!matchKind) continue;
|
|
786
|
+
}
|
|
831
787
|
|
|
832
|
-
|
|
788
|
+
if (queryLeaf) {
|
|
789
|
+
const qName = queryLeaf.toLowerCase();
|
|
790
|
+
const dName = def.name.toLowerCase();
|
|
791
|
+
if (query.exactMatch) {
|
|
792
|
+
if (dName !== qName) continue;
|
|
793
|
+
} else if (!dName.includes(qName)) continue;
|
|
833
794
|
}
|
|
834
795
|
|
|
835
|
-
|
|
836
|
-
} catch {
|
|
837
|
-
// skip unreadable
|
|
796
|
+
rawResults.push(item);
|
|
838
797
|
}
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
798
|
|
|
843
|
-
|
|
799
|
+
allDefsByFile.set(relPath, fileDefs);
|
|
800
|
+
} catch {
|
|
801
|
+
// skip unreadable
|
|
802
|
+
}
|
|
803
|
+
},
|
|
804
|
+
});
|
|
844
805
|
|
|
845
806
|
// Resolve aliased re-exports (e.g. `WebSocketClient` -> `client` in `.websocket`)
|
|
846
807
|
const resolvedResults: AstQueryResult[] = [...rawResults];
|
|
@@ -994,9 +955,12 @@ export function findSymbolSuggestions(
|
|
|
994
955
|
const nameLower = h.name.toLowerCase();
|
|
995
956
|
const dist = levenshteinDistance(qLower, nameLower);
|
|
996
957
|
const normHitPath = h.filePath.replace(/\\/g, "/");
|
|
958
|
+
const isWindows = process.platform === "win32";
|
|
959
|
+
const r1 = path.resolve(cwd, normHitPath);
|
|
960
|
+
const r2 = path.resolve(cwd, targetFilePath);
|
|
997
961
|
const isSameFile =
|
|
998
962
|
normHitPath === relTarget ||
|
|
999
|
-
|
|
963
|
+
(isWindows ? r1.toLowerCase() === r2.toLowerCase() : r1 === r2);
|
|
1000
964
|
const exactCaseSub = h.name.includes(qTrim);
|
|
1001
965
|
return {
|
|
1002
966
|
hit: h,
|
package/src/retrieval/repomap.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import { TreeSitterEngine } from "./tree_sitter_engine";
|
|
4
|
+
import { walkWorkspaceFiles, DEFAULT_IGNORED_DIRS } from "./workspace_walker";
|
|
4
5
|
|
|
5
6
|
export interface SymbolDef {
|
|
6
7
|
name: string;
|
|
@@ -31,23 +32,7 @@ export interface FileTags {
|
|
|
31
32
|
hasSyntaxError?: boolean;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
const IGNORED_DIRS =
|
|
35
|
-
".git",
|
|
36
|
-
"node_modules",
|
|
37
|
-
".venv",
|
|
38
|
-
"venv",
|
|
39
|
-
"__pycache__",
|
|
40
|
-
"dist",
|
|
41
|
-
"build",
|
|
42
|
-
"coverage",
|
|
43
|
-
".pi",
|
|
44
|
-
".hermes",
|
|
45
|
-
".next",
|
|
46
|
-
".turbo",
|
|
47
|
-
".cache",
|
|
48
|
-
"target",
|
|
49
|
-
"vendor",
|
|
50
|
-
]);
|
|
35
|
+
const IGNORED_DIRS = DEFAULT_IGNORED_DIRS;
|
|
51
36
|
|
|
52
37
|
const SUPPORTED_EXTENSIONS = new Set([
|
|
53
38
|
".ts",
|
|
@@ -786,36 +771,12 @@ export function extractFileTags(filePath: string, content: string): FileTags {
|
|
|
786
771
|
|
|
787
772
|
// Recursively find all supported code files in a directory
|
|
788
773
|
export function findSourceFiles(rootDir: string, maxFiles = 300): string[] {
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
796
|
-
} catch {
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
for (const entry of entries) {
|
|
801
|
-
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
802
|
-
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
803
|
-
|
|
804
|
-
const fullPath = path.join(dir, entry.name);
|
|
805
|
-
if (entry.isDirectory()) {
|
|
806
|
-
scan(fullPath);
|
|
807
|
-
} else if (entry.isFile()) {
|
|
808
|
-
const ext = path.extname(entry.name).toLowerCase();
|
|
809
|
-
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
810
|
-
results.push(fullPath);
|
|
811
|
-
if (results.length >= maxFiles) return;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
scan(rootDir);
|
|
818
|
-
return results;
|
|
774
|
+
return walkWorkspaceFiles({
|
|
775
|
+
rootDir,
|
|
776
|
+
maxFiles,
|
|
777
|
+
extensions: SUPPORTED_EXTENSIONS,
|
|
778
|
+
includeGithub: false,
|
|
779
|
+
});
|
|
819
780
|
}
|
|
820
781
|
|
|
821
782
|
// Check if a path corresponds to a test file or test directory
|
|
@@ -122,7 +122,7 @@ export class BM25Engine {
|
|
|
122
122
|
public removeFile(filePath: string): void {
|
|
123
123
|
const prefix = `${filePath}:`;
|
|
124
124
|
for (const chunkId of Array.from(this.docTermFreqs.keys())) {
|
|
125
|
-
if (chunkId.startsWith(prefix)
|
|
125
|
+
if (chunkId.startsWith(prefix)) {
|
|
126
126
|
const tfMap = this.docTermFreqs.get(chunkId);
|
|
127
127
|
if (tfMap) {
|
|
128
128
|
for (const term of tfMap.keys()) {
|
|
@@ -202,7 +202,10 @@ export class BM25Engine {
|
|
|
202
202
|
|
|
203
203
|
return Array.from(scores.entries())
|
|
204
204
|
.map(([chunkId, data]) => ({ chunkId, score: data.score, matches: data.matches }))
|
|
205
|
-
.sort((a, b) =>
|
|
205
|
+
.sort((a, b) => {
|
|
206
|
+
if (b.score !== a.score) return b.score - a.score;
|
|
207
|
+
return a.chunkId < b.chunkId ? -1 : a.chunkId > b.chunkId ? 1 : 0;
|
|
208
|
+
})
|
|
206
209
|
.slice(0, limit);
|
|
207
210
|
}
|
|
208
211
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as crypto from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { getPiHomeDir } from "../config/kernel_config";
|
|
5
|
+
|
|
6
|
+
function isWithin(root: string, target: string): boolean {
|
|
7
|
+
const relative = path.relative(root, target);
|
|
8
|
+
return relative === "" || (
|
|
9
|
+
relative !== ".." &&
|
|
10
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
11
|
+
!path.isAbsolute(relative)
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function ensureDirectoryNoLinks(trustedRoot: string, target: string): string {
|
|
16
|
+
const canonicalRoot = fs.realpathSync(trustedRoot);
|
|
17
|
+
const relative = path.relative(canonicalRoot, target);
|
|
18
|
+
if (!isWithin(canonicalRoot, target)) {
|
|
19
|
+
throw new Error(`Search cache path escapes trusted root: ${target}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let current = canonicalRoot;
|
|
23
|
+
for (const segment of relative.split(path.sep).filter(Boolean)) {
|
|
24
|
+
current = path.join(current, segment);
|
|
25
|
+
try {
|
|
26
|
+
const stat = fs.lstatSync(current);
|
|
27
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
28
|
+
throw new Error(`Search cache path contains a link or non-directory: ${current}`);
|
|
29
|
+
}
|
|
30
|
+
} catch (error: any) {
|
|
31
|
+
if (error?.code !== "ENOENT") throw error;
|
|
32
|
+
try {
|
|
33
|
+
fs.mkdirSync(current);
|
|
34
|
+
} catch (mkdirError: any) {
|
|
35
|
+
if (mkdirError?.code !== "EEXIST") throw mkdirError;
|
|
36
|
+
}
|
|
37
|
+
const stat = fs.lstatSync(current);
|
|
38
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
39
|
+
throw new Error(`Search cache path changed to a link or non-directory: ${current}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const canonicalTarget = fs.realpathSync(current);
|
|
45
|
+
if (!isWithin(canonicalRoot, canonicalTarget)) {
|
|
46
|
+
throw new Error(`Resolved search cache path escapes trusted root: ${canonicalTarget}`);
|
|
47
|
+
}
|
|
48
|
+
return canonicalTarget;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getSearchCacheDir(cwd: string): string {
|
|
52
|
+
const workspace = fs.realpathSync(path.resolve(cwd));
|
|
53
|
+
const workspaceId = crypto
|
|
54
|
+
.createHash("sha256")
|
|
55
|
+
.update(process.platform === "win32" ? workspace.toLowerCase() : workspace)
|
|
56
|
+
.digest("hex")
|
|
57
|
+
.slice(0, 24);
|
|
58
|
+
const configuredPiRoot = path.resolve(getPiHomeDir());
|
|
59
|
+
fs.mkdirSync(configuredPiRoot, { recursive: true });
|
|
60
|
+
const trustedPiRoot = fs.realpathSync(configuredPiRoot);
|
|
61
|
+
const trustedRoot = ensureDirectoryNoLinks(
|
|
62
|
+
trustedPiRoot,
|
|
63
|
+
path.join(trustedPiRoot, "cache", "agent-kernel", "search"),
|
|
64
|
+
);
|
|
65
|
+
return ensureDirectoryNoLinks(
|
|
66
|
+
trustedRoot,
|
|
67
|
+
path.join(trustedRoot, workspaceId),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import * as crypto from "node:crypto";
|
|
4
|
+
import { walkWorkspaceFiles, DEFAULT_IGNORED_DIRS } from "./workspace_walker";
|
|
4
5
|
import { extractFileTags, isTestPath, SymbolDef } from "./repomap";
|
|
5
6
|
import { TreeSitterEngine } from "./tree_sitter_engine";
|
|
6
7
|
|
|
@@ -19,20 +20,14 @@ export interface CodeChunk {
|
|
|
19
20
|
hash: string; // SHA-256 hash of content
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
const SUPPORTED_EXTENSIONS = new Set([
|
|
23
|
-
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql"
|
|
23
|
+
export const SUPPORTED_EXTENSIONS = new Set([
|
|
24
|
+
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql"
|
|
24
25
|
]);
|
|
25
26
|
|
|
26
|
-
const IGNORED_DIRS =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/** Prose extensions are excluded from the default code-search scope. */
|
|
31
|
-
export const PROSE_EXTENSIONS = new Set([".md", ".mdx", ".txt", ".rst"]);
|
|
32
|
-
|
|
33
|
-
export function isProseFilePath(filePath: string): boolean {
|
|
34
|
-
return PROSE_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
|
35
|
-
}
|
|
27
|
+
export const IGNORED_DIRS = DEFAULT_IGNORED_DIRS;
|
|
28
|
+
export const MAX_INDEX_FILE_BYTES = 2 * 1024 * 1024;
|
|
29
|
+
export const MAX_INDEX_WORKSPACE_BYTES = 20 * 1024 * 1024;
|
|
30
|
+
export const MAX_INDEX_CHUNKS = 10_000;
|
|
36
31
|
|
|
37
32
|
/**
|
|
38
33
|
* Compute SHA-256 hash of a string.
|
|
@@ -46,6 +41,13 @@ export function computeHash(content: string): string {
|
|
|
46
41
|
*/
|
|
47
42
|
export function chunkFile(rootDir: string, filePath: string, content?: string): CodeChunk[] {
|
|
48
43
|
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDir, filePath);
|
|
44
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
45
|
+
if (!SUPPORTED_EXTENSIONS.has(ext)) return [];
|
|
46
|
+
try {
|
|
47
|
+
if (fs.statSync(absPath).size > MAX_INDEX_FILE_BYTES) return [];
|
|
48
|
+
} catch {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
49
51
|
const relPath = path.relative(rootDir, absPath).replace(/\\/g, "/");
|
|
50
52
|
|
|
51
53
|
let fileContent = content;
|
|
@@ -56,11 +58,11 @@ export function chunkFile(rootDir: string, filePath: string, content?: string):
|
|
|
56
58
|
return [];
|
|
57
59
|
}
|
|
58
60
|
}
|
|
61
|
+
if (Buffer.byteLength(fileContent, "utf8") > MAX_INDEX_FILE_BYTES) return [];
|
|
59
62
|
|
|
60
63
|
const lines = fileContent.split("\n");
|
|
61
64
|
if (lines.length === 0) return [];
|
|
62
65
|
|
|
63
|
-
const ext = path.extname(absPath).toLowerCase();
|
|
64
66
|
const chunks: CodeChunk[] = [];
|
|
65
67
|
|
|
66
68
|
// 1. Extract AST symbols using extractFileTags
|
|
@@ -282,36 +284,12 @@ export function chunkFile(rootDir: string, filePath: string, content?: string):
|
|
|
282
284
|
* without reparsing unchanged files.
|
|
283
285
|
*/
|
|
284
286
|
export function findChunkableFiles(rootDir: string, maxFiles = 500): string[] {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
292
|
-
} catch {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
for (const entry of entries) {
|
|
297
|
-
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
298
|
-
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
299
|
-
|
|
300
|
-
const fullPath = path.join(dir, entry.name);
|
|
301
|
-
if (entry.isDirectory()) {
|
|
302
|
-
scan(fullPath);
|
|
303
|
-
} else if (entry.isFile()) {
|
|
304
|
-
const ext = path.extname(entry.name).toLowerCase();
|
|
305
|
-
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
306
|
-
files.push(fullPath);
|
|
307
|
-
if (files.length >= maxFiles) return;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
scan(rootDir);
|
|
314
|
-
return files;
|
|
287
|
+
return walkWorkspaceFiles({
|
|
288
|
+
rootDir,
|
|
289
|
+
maxFiles,
|
|
290
|
+
extensions: SUPPORTED_EXTENSIONS,
|
|
291
|
+
includeGithub: true,
|
|
292
|
+
});
|
|
315
293
|
}
|
|
316
294
|
|
|
317
295
|
/**
|