@floydous/pi-agent-kernel 0.1.0 → 0.3.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/CHANGELOG.md +57 -0
- package/README.md +78 -74
- package/docs/testing.md +10 -9
- package/grammars/tree-sitter-bash.wasm +0 -0
- package/grammars/tree-sitter-c.wasm +0 -0
- package/grammars/tree-sitter-c_sharp.wasm +0 -0
- package/grammars/tree-sitter-cpp.wasm +0 -0
- package/grammars/tree-sitter-go.wasm +0 -0
- package/grammars/tree-sitter-java.wasm +0 -0
- package/grammars/tree-sitter-javascript.wasm +0 -0
- package/grammars/tree-sitter-php.wasm +0 -0
- package/grammars/tree-sitter-python.wasm +0 -0
- package/grammars/tree-sitter-ruby.wasm +0 -0
- package/grammars/tree-sitter-rust.wasm +0 -0
- package/grammars/tree-sitter-tsx.wasm +0 -0
- package/grammars/tree-sitter-typescript.wasm +0 -0
- package/grammars/web-tree-sitter.wasm +0 -0
- package/package.json +11 -9
- package/src/dedup/content_store.ts +365 -365
- package/src/index.ts +918 -852
- package/src/lsp/lsp_formatter.ts +3 -1
- package/src/lsp/lsp_registry.ts +25 -1
- package/src/retrieval/ast_search.ts +930 -926
- package/src/retrieval/repomap.ts +220 -8
- package/src/retrieval/search_bm25.ts +190 -190
- package/src/retrieval/search_chunker.ts +254 -251
- package/src/retrieval/search_index.ts +22 -3
- package/src/retrieval/symbol_reader.ts +198 -200
- package/src/retrieval/tree_sitter_engine.ts +799 -0
- package/src/safety/epistemic_guard.ts +8 -1
- package/src/tools/ast_search_formatter.ts +161 -0
- package/src/tools/ast_search_tool.ts +17 -10
- package/src/tools/context.ts +20 -20
- package/src/tools/lsp_tool.ts +18 -5
- package/src/ui/tui_utils.ts +278 -278
|
@@ -1,251 +1,254 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import * as crypto from "node:crypto";
|
|
4
|
-
import { extractFileTags, SymbolDef } from "./repomap";
|
|
5
|
-
|
|
6
|
-
export interface CodeChunk {
|
|
7
|
-
id: string; // e.g. "src/auth.ts:45-90#verifyToken"
|
|
8
|
-
filePath: string; // Relative path
|
|
9
|
-
absolutePath: string;
|
|
10
|
-
startLine: number;
|
|
11
|
-
endLine: number;
|
|
12
|
-
symbolName: string;
|
|
13
|
-
kind: string; // "function" | "class" | "method" | "interface" | "type" | "block"
|
|
14
|
-
signature: string;
|
|
15
|
-
breadcrumb: string; // e.g. "// [File: src/auth.ts] > [Class: AuthManager] > [Function: verifyToken]"
|
|
16
|
-
content: string; // The full code block text
|
|
17
|
-
textForEmbedding: string;// Search-optimized representation
|
|
18
|
-
hash: string; // SHA-256 hash of content
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const SUPPORTED_EXTENSIONS = new Set([
|
|
22
|
-
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql", ".md"
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
const IGNORED_DIRS = new Set([
|
|
26
|
-
".git", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", "coverage", ".pi", ".hermes", ".next", ".turbo", ".cache", "target", "vendor"
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Compute SHA-256 hash of a string.
|
|
31
|
-
*/
|
|
32
|
-
export function computeHash(content: string): string {
|
|
33
|
-
return crypto.createHash("sha256").update(content, "utf8").digest("hex").slice(0, 16);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Chunk a single source file into logical AST-bounded chunks.
|
|
38
|
-
*/
|
|
39
|
-
export function chunkFile(rootDir: string, filePath: string, content?: string): CodeChunk[] {
|
|
40
|
-
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDir, filePath);
|
|
41
|
-
const relPath = path.relative(rootDir, absPath).replace(/\\/g, "/");
|
|
42
|
-
|
|
43
|
-
let fileContent = content;
|
|
44
|
-
if (fileContent === undefined) {
|
|
45
|
-
try {
|
|
46
|
-
fileContent = fs.readFileSync(absPath, "utf-8");
|
|
47
|
-
} catch {
|
|
48
|
-
return [];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const lines = fileContent.split("\n");
|
|
53
|
-
if (lines.length === 0) return [];
|
|
54
|
-
|
|
55
|
-
const ext = path.extname(absPath).toLowerCase();
|
|
56
|
-
const chunks: CodeChunk[] = [];
|
|
57
|
-
|
|
58
|
-
// 1. Extract AST symbols using extractFileTags
|
|
59
|
-
const tags = extractFileTags(absPath, fileContent);
|
|
60
|
-
const defs = tags.definitions.sort((a, b) => a.line - b.line);
|
|
61
|
-
|
|
62
|
-
// If file has no AST definitions or is small (< 40 lines), chunk as single whole file
|
|
63
|
-
if (defs.length === 0 || lines.length <= 40) {
|
|
64
|
-
const wholeContent = lines.join("\n");
|
|
65
|
-
const breadcrumb = `// [File: ${relPath}]`;
|
|
66
|
-
const hash = computeHash(wholeContent);
|
|
67
|
-
chunks.push({
|
|
68
|
-
id: `${relPath}:1-${lines.length}`,
|
|
69
|
-
filePath: relPath,
|
|
70
|
-
absolutePath: absPath,
|
|
71
|
-
startLine: 1,
|
|
72
|
-
endLine: lines.length,
|
|
73
|
-
symbolName: path.basename(relPath),
|
|
74
|
-
kind: "file",
|
|
75
|
-
signature: `File: ${relPath}`,
|
|
76
|
-
breadcrumb,
|
|
77
|
-
content: wholeContent,
|
|
78
|
-
textForEmbedding: `${breadcrumb}\n${wholeContent.slice(0, 1500)}`,
|
|
79
|
-
hash,
|
|
80
|
-
});
|
|
81
|
-
return chunks;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 2. Map symbol spans using delimiter & indentation boundaries
|
|
85
|
-
interface SymbolSpan {
|
|
86
|
-
def: SymbolDef;
|
|
87
|
-
startLine: number;
|
|
88
|
-
endLine: number;
|
|
89
|
-
parentClass?: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const spans: SymbolSpan[] = [];
|
|
93
|
-
let currentClass: string | undefined = undefined;
|
|
94
|
-
let currentClassEndLine = -1;
|
|
95
|
-
|
|
96
|
-
for (let i = 0; i < defs.length; i++) {
|
|
97
|
-
const def = defs[i];
|
|
98
|
-
const startIdx = Math.max(0, def.line - 1);
|
|
99
|
-
let endIdx = lines.length - 1;
|
|
100
|
-
|
|
101
|
-
if (def.line > currentClassEndLine) {
|
|
102
|
-
currentClass = undefined;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (def.kind === "class") {
|
|
106
|
-
currentClass = def.name;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
braceCount
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (entry.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as crypto from "node:crypto";
|
|
4
|
+
import { extractFileTags, SymbolDef } from "./repomap";
|
|
5
|
+
|
|
6
|
+
export interface CodeChunk {
|
|
7
|
+
id: string; // e.g. "src/auth.ts:45-90#verifyToken"
|
|
8
|
+
filePath: string; // Relative path
|
|
9
|
+
absolutePath: string;
|
|
10
|
+
startLine: number;
|
|
11
|
+
endLine: number;
|
|
12
|
+
symbolName: string;
|
|
13
|
+
kind: string; // "function" | "class" | "method" | "interface" | "type" | "block"
|
|
14
|
+
signature: string;
|
|
15
|
+
breadcrumb: string; // e.g. "// [File: src/auth.ts] > [Class: AuthManager] > [Function: verifyToken]"
|
|
16
|
+
content: string; // The full code block text
|
|
17
|
+
textForEmbedding: string;// Search-optimized representation
|
|
18
|
+
hash: string; // SHA-256 hash of content
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SUPPORTED_EXTENSIONS = new Set([
|
|
22
|
+
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql", ".md"
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const IGNORED_DIRS = new Set([
|
|
26
|
+
".git", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", "coverage", ".pi", ".hermes", ".next", ".turbo", ".cache", "target", "vendor"
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Compute SHA-256 hash of a string.
|
|
31
|
+
*/
|
|
32
|
+
export function computeHash(content: string): string {
|
|
33
|
+
return crypto.createHash("sha256").update(content, "utf8").digest("hex").slice(0, 16);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Chunk a single source file into logical AST-bounded chunks.
|
|
38
|
+
*/
|
|
39
|
+
export function chunkFile(rootDir: string, filePath: string, content?: string): CodeChunk[] {
|
|
40
|
+
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDir, filePath);
|
|
41
|
+
const relPath = path.relative(rootDir, absPath).replace(/\\/g, "/");
|
|
42
|
+
|
|
43
|
+
let fileContent = content;
|
|
44
|
+
if (fileContent === undefined) {
|
|
45
|
+
try {
|
|
46
|
+
fileContent = fs.readFileSync(absPath, "utf-8");
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const lines = fileContent.split("\n");
|
|
53
|
+
if (lines.length === 0) return [];
|
|
54
|
+
|
|
55
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
56
|
+
const chunks: CodeChunk[] = [];
|
|
57
|
+
|
|
58
|
+
// 1. Extract AST symbols using extractFileTags
|
|
59
|
+
const tags = extractFileTags(absPath, fileContent);
|
|
60
|
+
const defs = tags.definitions.sort((a, b) => a.line - b.line);
|
|
61
|
+
|
|
62
|
+
// If file has no AST definitions or is small (< 40 lines), chunk as single whole file
|
|
63
|
+
if (defs.length === 0 || lines.length <= 40) {
|
|
64
|
+
const wholeContent = lines.join("\n");
|
|
65
|
+
const breadcrumb = `// [File: ${relPath}]`;
|
|
66
|
+
const hash = computeHash(wholeContent);
|
|
67
|
+
chunks.push({
|
|
68
|
+
id: `${relPath}:1-${lines.length}`,
|
|
69
|
+
filePath: relPath,
|
|
70
|
+
absolutePath: absPath,
|
|
71
|
+
startLine: 1,
|
|
72
|
+
endLine: lines.length,
|
|
73
|
+
symbolName: path.basename(relPath),
|
|
74
|
+
kind: "file",
|
|
75
|
+
signature: `File: ${relPath}`,
|
|
76
|
+
breadcrumb,
|
|
77
|
+
content: wholeContent,
|
|
78
|
+
textForEmbedding: `${breadcrumb}\n${wholeContent.slice(0, 1500)}`,
|
|
79
|
+
hash,
|
|
80
|
+
});
|
|
81
|
+
return chunks;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 2. Map symbol spans using delimiter & indentation boundaries
|
|
85
|
+
interface SymbolSpan {
|
|
86
|
+
def: SymbolDef;
|
|
87
|
+
startLine: number;
|
|
88
|
+
endLine: number;
|
|
89
|
+
parentClass?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const spans: SymbolSpan[] = [];
|
|
93
|
+
let currentClass: string | undefined = undefined;
|
|
94
|
+
let currentClassEndLine = -1;
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < defs.length; i++) {
|
|
97
|
+
const def = defs[i];
|
|
98
|
+
const startIdx = Math.max(0, def.line - 1);
|
|
99
|
+
let endIdx = lines.length - 1;
|
|
100
|
+
|
|
101
|
+
if (def.line > currentClassEndLine) {
|
|
102
|
+
currentClass = undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (def.kind === "class") {
|
|
106
|
+
currentClass = def.name;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (def.endLine && def.endLine >= def.line) {
|
|
110
|
+
// Exact AST node boundaries from Tree-sitter
|
|
111
|
+
endIdx = Math.min(lines.length - 1, def.endLine - 1);
|
|
112
|
+
} else if (ext === ".py") {
|
|
113
|
+
// Python scope
|
|
114
|
+
const defLine = lines[startIdx];
|
|
115
|
+
const matchIndent = defLine.match(/^(\s*)/);
|
|
116
|
+
const baseIndent = matchIndent ? matchIndent[1].length : 0;
|
|
117
|
+
|
|
118
|
+
for (let j = startIdx + 1; j < lines.length; j++) {
|
|
119
|
+
const line = lines[j];
|
|
120
|
+
if (!line.trim() || line.trim().startsWith("#")) continue;
|
|
121
|
+
const indent = line.match(/^(\s*)/)?.[1].length || 0;
|
|
122
|
+
if (indent <= baseIndent) {
|
|
123
|
+
endIdx = j - 1;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
endIdx = j;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
// Brace based scope (fallback)
|
|
130
|
+
let braceCount = 0;
|
|
131
|
+
let foundOpenBrace = false;
|
|
132
|
+
|
|
133
|
+
for (let j = startIdx; j < lines.length; j++) {
|
|
134
|
+
const line = lines[j];
|
|
135
|
+
for (const ch of line) {
|
|
136
|
+
if (ch === "{") {
|
|
137
|
+
braceCount++;
|
|
138
|
+
foundOpenBrace = true;
|
|
139
|
+
} else if (ch === "}") {
|
|
140
|
+
braceCount--;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (foundOpenBrace && braceCount <= 0) {
|
|
144
|
+
endIdx = j;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Bound endLine to before next top-level def if necessary
|
|
151
|
+
if (i + 1 < defs.length && endIdx >= defs[i + 1].line - 1 && def.kind !== "class") {
|
|
152
|
+
endIdx = Math.min(endIdx, defs[i + 1].line - 2);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
endIdx = Math.max(startIdx, Math.min(lines.length - 1, endIdx));
|
|
156
|
+
|
|
157
|
+
if (def.kind === "class") {
|
|
158
|
+
currentClassEndLine = endIdx + 1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
spans.push({
|
|
162
|
+
def,
|
|
163
|
+
startLine: startIdx + 1,
|
|
164
|
+
endLine: endIdx + 1,
|
|
165
|
+
parentClass: def.kind !== "class" && def.line <= currentClassEndLine ? currentClass : undefined,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 3. Construct CodeChunk objects from spans
|
|
170
|
+
for (const span of spans) {
|
|
171
|
+
const startIdx = span.startLine - 1;
|
|
172
|
+
const endIdx = span.endLine - 1;
|
|
173
|
+
const chunkLines = lines.slice(startIdx, endIdx + 1);
|
|
174
|
+
const chunkContent = chunkLines.join("\n");
|
|
175
|
+
|
|
176
|
+
if (!chunkContent.trim()) continue;
|
|
177
|
+
|
|
178
|
+
let breadcrumb = `// [File: ${relPath}]`;
|
|
179
|
+
if (span.parentClass) {
|
|
180
|
+
breadcrumb += ` > [Class: ${span.parentClass}]`;
|
|
181
|
+
}
|
|
182
|
+
breadcrumb += ` > [${span.def.kind.toUpperCase()}: ${span.def.name}]`;
|
|
183
|
+
|
|
184
|
+
const textForEmbedding = `${breadcrumb}\nSignature: ${span.def.signature}\n\n${chunkContent.slice(0, 1500)}`;
|
|
185
|
+
const hash = computeHash(chunkContent);
|
|
186
|
+
|
|
187
|
+
chunks.push({
|
|
188
|
+
id: `${relPath}:${span.startLine}-${span.endLine}#${span.def.name}`,
|
|
189
|
+
filePath: relPath,
|
|
190
|
+
absolutePath: absPath,
|
|
191
|
+
startLine: span.startLine,
|
|
192
|
+
endLine: span.endLine,
|
|
193
|
+
symbolName: span.def.name,
|
|
194
|
+
kind: span.def.kind,
|
|
195
|
+
signature: span.def.signature,
|
|
196
|
+
breadcrumb,
|
|
197
|
+
content: chunkContent,
|
|
198
|
+
textForEmbedding,
|
|
199
|
+
hash,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return chunks;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Find supported source files in a workspace using the same traversal policy as
|
|
208
|
+
* chunkWorkspace. The search index uses this to check live file freshness
|
|
209
|
+
* without reparsing unchanged files.
|
|
210
|
+
*/
|
|
211
|
+
export function findChunkableFiles(rootDir: string, maxFiles = 500): string[] {
|
|
212
|
+
const files: string[] = [];
|
|
213
|
+
|
|
214
|
+
function scan(dir: string) {
|
|
215
|
+
if (files.length >= maxFiles) return;
|
|
216
|
+
let entries: fs.Dirent[];
|
|
217
|
+
try {
|
|
218
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
219
|
+
} catch {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
for (const entry of entries) {
|
|
224
|
+
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
225
|
+
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
226
|
+
|
|
227
|
+
const fullPath = path.join(dir, entry.name);
|
|
228
|
+
if (entry.isDirectory()) {
|
|
229
|
+
scan(fullPath);
|
|
230
|
+
} else if (entry.isFile()) {
|
|
231
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
232
|
+
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
233
|
+
files.push(fullPath);
|
|
234
|
+
if (files.length >= maxFiles) return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
scan(rootDir);
|
|
241
|
+
return files;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Scan workspace and chunk all supported source files.
|
|
246
|
+
*/
|
|
247
|
+
export function chunkWorkspace(rootDir: string, maxFiles = 500): { chunks: CodeChunk[]; fileCount: number } {
|
|
248
|
+
const files = findChunkableFiles(rootDir, maxFiles);
|
|
249
|
+
const chunks: CodeChunk[] = [];
|
|
250
|
+
for (const filePath of files) {
|
|
251
|
+
chunks.push(...chunkFile(rootDir, filePath));
|
|
252
|
+
}
|
|
253
|
+
return { chunks, fileCount: files.length };
|
|
254
|
+
}
|
|
@@ -18,6 +18,10 @@ import {
|
|
|
18
18
|
} from "./search_config";
|
|
19
19
|
import { kernelDebug } from "../safety/kernel_debug";
|
|
20
20
|
import { writeFileSyncAtomic } from "../safety/atomic_write";
|
|
21
|
+
import { TreeSitterEngine } from "./tree_sitter_engine";
|
|
22
|
+
|
|
23
|
+
const INDEX_VERSION = 3;
|
|
24
|
+
const EXTRACTOR_GENERATION = "tree-sitter-wasm-v2";
|
|
21
25
|
|
|
22
26
|
export interface SearchHit {
|
|
23
27
|
chunk: CodeChunk;
|
|
@@ -120,7 +124,12 @@ export class HybridSearchIndex {
|
|
|
120
124
|
const raw = fs.readFileSync(indexPath, "utf-8");
|
|
121
125
|
const data = JSON.parse(raw);
|
|
122
126
|
|
|
123
|
-
if (
|
|
127
|
+
if (
|
|
128
|
+
data.version !== INDEX_VERSION ||
|
|
129
|
+
data.extractorGeneration !== EXTRACTOR_GENERATION
|
|
130
|
+
) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
124
133
|
|
|
125
134
|
this.chunks.clear();
|
|
126
135
|
this.bm25.clear();
|
|
@@ -242,7 +251,8 @@ export class HybridSearchIndex {
|
|
|
242
251
|
.digest("hex");
|
|
243
252
|
|
|
244
253
|
const indexData = {
|
|
245
|
-
version:
|
|
254
|
+
version: INDEX_VERSION,
|
|
255
|
+
extractorGeneration: EXTRACTOR_GENERATION,
|
|
246
256
|
updatedAt: new Date().toISOString(),
|
|
247
257
|
profile: this.config.profile,
|
|
248
258
|
vectorDim,
|
|
@@ -389,6 +399,15 @@ export class HybridSearchIndex {
|
|
|
389
399
|
|
|
390
400
|
onProgress?.("Scanning workspace files...");
|
|
391
401
|
|
|
402
|
+
// Ensure Tree-sitter parsers are warm for languages present in this workspace
|
|
403
|
+
const chunkableFiles = findChunkableFiles(this.cwd);
|
|
404
|
+
const workspaceExts = new Set<string>();
|
|
405
|
+
for (const f of chunkableFiles) {
|
|
406
|
+
const ext = path.extname(f).toLowerCase();
|
|
407
|
+
if (ext) workspaceExts.add(ext);
|
|
408
|
+
}
|
|
409
|
+
await TreeSitterEngine.getInstance().loadLanguages(Array.from(workspaceExts));
|
|
410
|
+
|
|
392
411
|
// Read each file once and derive both its chunks and hash from that same
|
|
393
412
|
// snapshot. A second read could pair old chunks with a new hash and make
|
|
394
413
|
// the final freshness check falsely accept stale content.
|
|
@@ -396,7 +415,7 @@ export class HybridSearchIndex {
|
|
|
396
415
|
string,
|
|
397
416
|
{ hash: string; chunks: CodeChunk[] }
|
|
398
417
|
>();
|
|
399
|
-
for (const absPath of
|
|
418
|
+
for (const absPath of chunkableFiles) {
|
|
400
419
|
const relPath = path.relative(this.cwd, absPath).replace(/\\/g, "/");
|
|
401
420
|
try {
|
|
402
421
|
const content = fs.readFileSync(absPath, "utf-8");
|