@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.
Files changed (35) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +78 -74
  3. package/docs/testing.md +10 -9
  4. package/grammars/tree-sitter-bash.wasm +0 -0
  5. package/grammars/tree-sitter-c.wasm +0 -0
  6. package/grammars/tree-sitter-c_sharp.wasm +0 -0
  7. package/grammars/tree-sitter-cpp.wasm +0 -0
  8. package/grammars/tree-sitter-go.wasm +0 -0
  9. package/grammars/tree-sitter-java.wasm +0 -0
  10. package/grammars/tree-sitter-javascript.wasm +0 -0
  11. package/grammars/tree-sitter-php.wasm +0 -0
  12. package/grammars/tree-sitter-python.wasm +0 -0
  13. package/grammars/tree-sitter-ruby.wasm +0 -0
  14. package/grammars/tree-sitter-rust.wasm +0 -0
  15. package/grammars/tree-sitter-tsx.wasm +0 -0
  16. package/grammars/tree-sitter-typescript.wasm +0 -0
  17. package/grammars/web-tree-sitter.wasm +0 -0
  18. package/package.json +11 -9
  19. package/src/dedup/content_store.ts +365 -365
  20. package/src/index.ts +918 -852
  21. package/src/lsp/lsp_formatter.ts +3 -1
  22. package/src/lsp/lsp_registry.ts +25 -1
  23. package/src/retrieval/ast_search.ts +930 -926
  24. package/src/retrieval/repomap.ts +220 -8
  25. package/src/retrieval/search_bm25.ts +190 -190
  26. package/src/retrieval/search_chunker.ts +254 -251
  27. package/src/retrieval/search_index.ts +22 -3
  28. package/src/retrieval/symbol_reader.ts +198 -200
  29. package/src/retrieval/tree_sitter_engine.ts +799 -0
  30. package/src/safety/epistemic_guard.ts +8 -1
  31. package/src/tools/ast_search_formatter.ts +161 -0
  32. package/src/tools/ast_search_tool.ts +17 -10
  33. package/src/tools/context.ts +20 -20
  34. package/src/tools/lsp_tool.ts +18 -5
  35. 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 (ext === ".py") {
84
- // Python indentation based boundary detection
85
- const defLine = lines[startIdx];
86
- const matchIndent = defLine.match(/^(\s*)/);
87
- const baseIndent = matchIndent ? matchIndent[1].length : 0;
88
-
89
- // Handle multi-line Python signatures (e.g. def foo(\n arg: int\n) -> bool:)
90
- let headerEndIdx = startIdx;
91
- let parenCount = 0;
92
- for (let i = startIdx; i < lines.length; i++) {
93
- const line = lines[i];
94
- for (const ch of line) {
95
- if (ch === "(") parenCount++;
96
- else if (ch === ")") parenCount--;
97
- }
98
- if (parenCount <= 0 && line.trim().endsWith(":")) {
99
- headerEndIdx = i;
100
- break;
101
- }
102
- }
103
-
104
- let inDocstring = false;
105
- let docstringQuote = "";
106
-
107
- for (let i = headerEndIdx + 1; i < lines.length; i++) {
108
- const currentLine = lines[i];
109
- const trimmed = currentLine.trim();
110
-
111
- if (!trimmed) {
112
- endIdx = i;
113
- continue;
114
- }
115
-
116
- // Check docstrings
117
- if (!inDocstring) {
118
- if (trimmed.startsWith('"""') || trimmed.startsWith("'''")) {
119
- docstringQuote = trimmed.slice(0, 3);
120
- if (trimmed.length === 3 || !trimmed.slice(3).includes(docstringQuote)) {
121
- inDocstring = true;
122
- }
123
- }
124
- } else {
125
- if (trimmed.includes(docstringQuote)) {
126
- inDocstring = false;
127
- }
128
- endIdx = i;
129
- continue;
130
- }
131
-
132
- if (inDocstring) {
133
- endIdx = i;
134
- continue;
135
- }
136
-
137
- if (trimmed.startsWith("#")) {
138
- endIdx = i;
139
- continue;
140
- }
141
-
142
- const currIndent = currentLine.match(/^(\s*)/)?.[1].length || 0;
143
- if (currIndent <= baseIndent) {
144
- break;
145
- }
146
- endIdx = i;
147
- }
148
- } else {
149
- // Brace-based languages (TS, JS, Go, Rust, C++, Java, C#)
150
- let braceCount = 0;
151
- let foundOpenBrace = false;
152
-
153
- for (let i = startIdx; i < lines.length; i++) {
154
- const currentLine = lines[i];
155
-
156
- for (let charIdx = 0; charIdx < currentLine.length; charIdx++) {
157
- const char = currentLine[charIdx];
158
- if (char === "{") {
159
- braceCount++;
160
- foundOpenBrace = true;
161
- } else if (char === "}") {
162
- braceCount--;
163
- }
164
- }
165
-
166
- endIdx = i;
167
- if (foundOpenBrace && braceCount <= 0) {
168
- break;
169
- }
170
- }
171
- }
172
-
173
- // Apply surrounding context if requested
174
- const extra = options.surroundingLines || 0;
175
- const finalStart = Math.max(0, actualStartIdx - extra);
176
- const finalEnd = Math.min(lines.length - 1, endIdx + extra);
177
-
178
- // Format with line numbers (e.g. " 1420 | def handle(...):")
179
- const formattedLines = [];
180
- for (let i = finalStart; i <= finalEnd; i++) {
181
- const lineNumStr = String(i + 1).padStart(5, " ");
182
- formattedLines.push(`${lineNumStr} | ${lines[i]}`);
183
- }
184
-
185
- matches.push({
186
- name: def.name,
187
- kind: def.kind,
188
- signature: def.signature,
189
- startLine: finalStart + 1,
190
- endLine: finalEnd + 1,
191
- content: formattedLines.join("\n"),
192
- filePath: resolvedPath,
193
- });
194
- }
195
-
196
- return {
197
- found: matches.length > 0,
198
- symbols: matches,
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
+ }