@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,200 +1,198 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
import { extractFileTags, SymbolDef } from "./repomap";
|
|
4
|
-
|
|
5
|
-
export interface SymbolLocation {
|
|
6
|
-
name: string;
|
|
7
|
-
kind: string;
|
|
8
|
-
signature: string;
|
|
9
|
-
startLine: number;
|
|
10
|
-
endLine: number;
|
|
11
|
-
content: string;
|
|
12
|
-
filePath: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Extract exact start and end line range for a symbol in a file.
|
|
17
|
-
*/
|
|
18
|
-
export function extractSymbolContent(
|
|
19
|
-
filePath: string,
|
|
20
|
-
targetSymbol: string,
|
|
21
|
-
options: { surroundingLines?: number } = {},
|
|
22
|
-
fileContentOverride?: string,
|
|
23
|
-
): { found: boolean; symbols: SymbolLocation[]; error?: string } {
|
|
24
|
-
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
|
|
25
|
-
|
|
26
|
-
if (!fs.existsSync(resolvedPath)) {
|
|
27
|
-
return { found: false, symbols: [], error: `File not found: ${resolvedPath}` };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let fileContent: string;
|
|
31
|
-
if (fileContentOverride !== undefined) {
|
|
32
|
-
fileContent = fileContentOverride;
|
|
33
|
-
} else {
|
|
34
|
-
try {
|
|
35
|
-
fileContent = fs.readFileSync(resolvedPath, "utf-8");
|
|
36
|
-
} catch (e: any) {
|
|
37
|
-
return { found: false, symbols: [], error: `Unable to read file: ${e.message}` };
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const lines = fileContent.split("\n");
|
|
42
|
-
const tags = extractFileTags(resolvedPath, fileContent);
|
|
43
|
-
const ext = path.extname(resolvedPath).toLowerCase();
|
|
44
|
-
|
|
45
|
-
const matches: SymbolLocation[] = [];
|
|
46
|
-
|
|
47
|
-
// Search matching definitions in tags
|
|
48
|
-
const matchingDefs = tags.definitions.filter(
|
|
49
|
-
(d) => d.name === targetSymbol || d.name.toLowerCase() === targetSymbol.toLowerCase()
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
if (matchingDefs.length === 0) {
|
|
53
|
-
// Fallback: search for symbol with regex across all occurrences if tags missed it
|
|
54
|
-
const escaped = targetSymbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
55
|
-
const regex = new RegExp(`(?:def|class|function|async function|const|let|var|type|interface)\\s+${escaped}\\b`, "i");
|
|
56
|
-
for (let i = 0; i < lines.length; i++) {
|
|
57
|
-
if (regex.test(lines[i])) {
|
|
58
|
-
matchingDefs.push({
|
|
59
|
-
name: targetSymbol,
|
|
60
|
-
kind: lines[i].includes("class") ? "class" : "function",
|
|
61
|
-
signature: lines[i].trim(),
|
|
62
|
-
line: i + 1,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
for (const def of matchingDefs) {
|
|
69
|
-
const startIdx = def.line - 1;
|
|
70
|
-
let endIdx = startIdx;
|
|
71
|
-
|
|
72
|
-
// Include preceding docstrings/comments/decorators if present
|
|
73
|
-
let actualStartIdx = startIdx;
|
|
74
|
-
while (actualStartIdx > 0) {
|
|
75
|
-
const prevLine = lines[actualStartIdx - 1].trim();
|
|
76
|
-
if (prevLine.startsWith("#") || prevLine.startsWith("//") || prevLine.startsWith("*") || prevLine.startsWith("/*") || prevLine.startsWith("@")) {
|
|
77
|
-
actualStartIdx--;
|
|
78
|
-
} else {
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
//
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
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
|
-
braceCount
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
};
|
|
200
|
-
}
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { extractFileTags, SymbolDef } from "./repomap";
|
|
4
|
+
|
|
5
|
+
export interface SymbolLocation {
|
|
6
|
+
name: string;
|
|
7
|
+
kind: string;
|
|
8
|
+
signature: string;
|
|
9
|
+
startLine: number;
|
|
10
|
+
endLine: number;
|
|
11
|
+
content: string;
|
|
12
|
+
filePath: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Extract exact start and end line range for a symbol in a file.
|
|
17
|
+
*/
|
|
18
|
+
export function extractSymbolContent(
|
|
19
|
+
filePath: string,
|
|
20
|
+
targetSymbol: string,
|
|
21
|
+
options: { surroundingLines?: number } = {},
|
|
22
|
+
fileContentOverride?: string,
|
|
23
|
+
): { found: boolean; symbols: SymbolLocation[]; error?: string } {
|
|
24
|
+
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
|
|
25
|
+
|
|
26
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
27
|
+
return { found: false, symbols: [], error: `File not found: ${resolvedPath}` };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let fileContent: string;
|
|
31
|
+
if (fileContentOverride !== undefined) {
|
|
32
|
+
fileContent = fileContentOverride;
|
|
33
|
+
} else {
|
|
34
|
+
try {
|
|
35
|
+
fileContent = fs.readFileSync(resolvedPath, "utf-8");
|
|
36
|
+
} catch (e: any) {
|
|
37
|
+
return { found: false, symbols: [], error: `Unable to read file: ${e.message}` };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const lines = fileContent.split("\n");
|
|
42
|
+
const tags = extractFileTags(resolvedPath, fileContent);
|
|
43
|
+
const ext = path.extname(resolvedPath).toLowerCase();
|
|
44
|
+
|
|
45
|
+
const matches: SymbolLocation[] = [];
|
|
46
|
+
|
|
47
|
+
// Search matching definitions in tags
|
|
48
|
+
const matchingDefs = tags.definitions.filter(
|
|
49
|
+
(d) => d.name === targetSymbol || d.name.toLowerCase() === targetSymbol.toLowerCase()
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
if (matchingDefs.length === 0) {
|
|
53
|
+
// Fallback: search for symbol with regex across all occurrences if tags missed it
|
|
54
|
+
const escaped = targetSymbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
55
|
+
const regex = new RegExp(`(?:def|class|function|async function|const|let|var|type|interface)\\s+${escaped}\\b`, "i");
|
|
56
|
+
for (let i = 0; i < lines.length; i++) {
|
|
57
|
+
if (regex.test(lines[i])) {
|
|
58
|
+
matchingDefs.push({
|
|
59
|
+
name: targetSymbol,
|
|
60
|
+
kind: lines[i].includes("class") ? "class" : "function",
|
|
61
|
+
signature: lines[i].trim(),
|
|
62
|
+
line: i + 1,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const def of matchingDefs) {
|
|
69
|
+
const startIdx = def.line - 1;
|
|
70
|
+
let endIdx = startIdx;
|
|
71
|
+
|
|
72
|
+
// Include preceding docstrings/comments/decorators if present
|
|
73
|
+
let actualStartIdx = startIdx;
|
|
74
|
+
while (actualStartIdx > 0) {
|
|
75
|
+
const prevLine = lines[actualStartIdx - 1].trim();
|
|
76
|
+
if (prevLine.startsWith("#") || prevLine.startsWith("//") || prevLine.startsWith("*") || prevLine.startsWith("/*") || prevLine.startsWith("@")) {
|
|
77
|
+
actualStartIdx--;
|
|
78
|
+
} else {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (def.endLine && def.endLine >= def.line) {
|
|
84
|
+
// Ground-truth AST range provided directly by Tree-sitter
|
|
85
|
+
endIdx = Math.min(lines.length - 1, def.endLine - 1);
|
|
86
|
+
} else if (ext === ".py") {
|
|
87
|
+
// Python indentation based boundary detection
|
|
88
|
+
const defLine = lines[startIdx];
|
|
89
|
+
const matchIndent = defLine.match(/^(\s*)/);
|
|
90
|
+
const baseIndent = matchIndent ? matchIndent[1].length : 0;
|
|
91
|
+
|
|
92
|
+
// Handle multi-line Python signatures (e.g. def foo(\n arg: int\n) -> bool:)
|
|
93
|
+
let headerEndIdx = startIdx;
|
|
94
|
+
let parenCount = 0;
|
|
95
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
96
|
+
const line = lines[i];
|
|
97
|
+
for (const ch of line) {
|
|
98
|
+
if (ch === "(") parenCount++;
|
|
99
|
+
else if (ch === ")") parenCount--;
|
|
100
|
+
}
|
|
101
|
+
if (parenCount <= 0 && line.trim().endsWith(":")) {
|
|
102
|
+
headerEndIdx = i;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let inDocstring = false;
|
|
108
|
+
let docstringQuote = "";
|
|
109
|
+
|
|
110
|
+
for (let i = headerEndIdx + 1; i < lines.length; i++) {
|
|
111
|
+
const currentLine = lines[i];
|
|
112
|
+
const trimmed = currentLine.trim();
|
|
113
|
+
|
|
114
|
+
if (!trimmed) {
|
|
115
|
+
endIdx = i;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Check docstrings
|
|
120
|
+
if (!inDocstring) {
|
|
121
|
+
if (trimmed.startsWith('"""') || trimmed.startsWith("'''")) {
|
|
122
|
+
docstringQuote = trimmed.slice(0, 3);
|
|
123
|
+
if (trimmed.length === 3 || !trimmed.slice(3).includes(docstringQuote)) {
|
|
124
|
+
inDocstring = true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
if (trimmed.includes(docstringQuote)) {
|
|
129
|
+
inDocstring = false;
|
|
130
|
+
}
|
|
131
|
+
endIdx = i;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (inDocstring) {
|
|
136
|
+
endIdx = i;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (trimmed.startsWith("#")) {
|
|
141
|
+
endIdx = i;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const currIndent = currentLine.match(/^(\s*)/)?.[1].length || 0;
|
|
146
|
+
if (currIndent <= baseIndent) {
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
endIdx = i;
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
// Brace-based languages (fallback if AST endLine is unavailable)
|
|
153
|
+
let braceCount = 0;
|
|
154
|
+
let foundOpenBrace = false;
|
|
155
|
+
|
|
156
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
157
|
+
const currentLine = lines[i];
|
|
158
|
+
|
|
159
|
+
for (let charIdx = 0; charIdx < currentLine.length; charIdx++) {
|
|
160
|
+
const char = currentLine[charIdx];
|
|
161
|
+
if (char === "{") {
|
|
162
|
+
braceCount++;
|
|
163
|
+
foundOpenBrace = true;
|
|
164
|
+
} else if (char === "}") {
|
|
165
|
+
braceCount--;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
endIdx = i;
|
|
170
|
+
if (foundOpenBrace && braceCount <= 0) {
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Apply surrounding context if requested
|
|
177
|
+
const extra = options.surroundingLines || 0;
|
|
178
|
+
const finalStart = Math.max(0, actualStartIdx - extra);
|
|
179
|
+
const finalEnd = Math.min(lines.length - 1, endIdx + extra);
|
|
180
|
+
|
|
181
|
+
const rawContent = lines.slice(finalStart, finalEnd + 1).join("\n");
|
|
182
|
+
|
|
183
|
+
matches.push({
|
|
184
|
+
name: def.name,
|
|
185
|
+
kind: def.kind,
|
|
186
|
+
signature: def.signature,
|
|
187
|
+
startLine: finalStart + 1,
|
|
188
|
+
endLine: finalEnd + 1,
|
|
189
|
+
content: rawContent,
|
|
190
|
+
filePath: resolvedPath,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
found: matches.length > 0,
|
|
196
|
+
symbols: matches,
|
|
197
|
+
};
|
|
198
|
+
}
|