@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
|
@@ -651,7 +651,14 @@ export class EpistemicGuard {
|
|
|
651
651
|
};
|
|
652
652
|
}
|
|
653
653
|
|
|
654
|
-
|
|
654
|
+
if (!exists && operation === "edit") {
|
|
655
|
+
return {
|
|
656
|
+
allowed: false,
|
|
657
|
+
reason: `[EPISTEMIC GUARD]: Target '${filePath}' does not exist.`,
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// A new file has no prior contents to inspect. Containment still applies for write.
|
|
655
662
|
if (!enforceInspection || !exists) return { allowed: true };
|
|
656
663
|
|
|
657
664
|
const normalized = this.normalize(resolvedPath, workspace);
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { AstQueryResult } from "../retrieval/ast_search";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Clean signature string by removing redundant declaration keywords and access modifiers,
|
|
5
|
+
* while preserving semantic modifiers (async, static, readonly, unsafe, const, mut)
|
|
6
|
+
* and full parameter/return types.
|
|
7
|
+
*/
|
|
8
|
+
export function cleanSignature(rawSignature: string | undefined, symbolName: string | undefined): string {
|
|
9
|
+
const trimmedName = symbolName?.trim();
|
|
10
|
+
if (!rawSignature || !rawSignature.trim()) {
|
|
11
|
+
return trimmedName || "unknown";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Normalize multiline signatures to a single line
|
|
15
|
+
let sig = rawSignature.replace(/\r?\n/g, " ").replace(/\s+/g, " ").trim();
|
|
16
|
+
|
|
17
|
+
// Iteratively peel redundant leading prefixes from the start of the signature
|
|
18
|
+
let changed = true;
|
|
19
|
+
while (changed) {
|
|
20
|
+
const before = sig;
|
|
21
|
+
|
|
22
|
+
// 1. Module / declaration wrappers
|
|
23
|
+
sig = sig.replace(/^(export\s+|default\s+|declare\s+)/, "");
|
|
24
|
+
|
|
25
|
+
// 2. Visibility modifiers (TypeScript, Java, PHP, C++, Rust)
|
|
26
|
+
sig = sig.replace(/^(public\s+|private\s+|protected\s+)/, "");
|
|
27
|
+
sig = sig.replace(/^pub(\s*\([^)]*\))?\s+/, "");
|
|
28
|
+
|
|
29
|
+
// 3. Redundant kind keywords (when not preceded by async/static/unsafe)
|
|
30
|
+
// e.g. "function foo()", "def foo()", "fn foo()", "func foo()", "class Foo"
|
|
31
|
+
sig = sig.replace(/^(function\s*\*?|def\s+|fn\s+|func\s+|class\s+|interface\s+|type\s+|enum\s+|struct\s+|trait\s+)/, "");
|
|
32
|
+
|
|
33
|
+
// 4. Strip redundant kind keywords that follow preserved semantic modifiers (async, static, unsafe)
|
|
34
|
+
sig = sig.replace(/^((?:async|static|unsafe)\s+)(?:function\s*\*?|fn|def|func)\s+/, "$1");
|
|
35
|
+
|
|
36
|
+
// 5. TypeScript/ES accessors (get/set)
|
|
37
|
+
sig = sig.replace(/^(get\s+|set\s+)/, "");
|
|
38
|
+
|
|
39
|
+
// 6. Abstract modifier
|
|
40
|
+
sig = sig.replace(/^abstract\s+/, "");
|
|
41
|
+
|
|
42
|
+
sig = sig.trim();
|
|
43
|
+
changed = sig !== before;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return sig.trim() || trimmedName || "unknown";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Format raw AST search results into a compact hierarchical layout:
|
|
51
|
+
* - Each normalized file path appears once.
|
|
52
|
+
* - Symbols are grouped by kind preserving incoming rank order.
|
|
53
|
+
* - Singletons of a kind render inline: `[kind] range signature`
|
|
54
|
+
* - Multiple items of a kind render with a header and `- range signature`
|
|
55
|
+
* - Optional body preview indented with 4 spaces and truncation notice
|
|
56
|
+
*/
|
|
57
|
+
export function formatAstSearchResults(
|
|
58
|
+
results: AstQueryResult[],
|
|
59
|
+
includeBody = false,
|
|
60
|
+
): string {
|
|
61
|
+
if (!results || results.length === 0) {
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 1. Group by normalized relative filePath preserving first-seen file order
|
|
66
|
+
const fileOrder: string[] = [];
|
|
67
|
+
const byFile = new Map<string, Map<string, AstQueryResult[]>>();
|
|
68
|
+
|
|
69
|
+
for (const r of results) {
|
|
70
|
+
const normPath = r.filePath.replace(/\\/g, "/");
|
|
71
|
+
let byKind = byFile.get(normPath);
|
|
72
|
+
if (!byKind) {
|
|
73
|
+
byKind = new Map<string, AstQueryResult[]>();
|
|
74
|
+
byFile.set(normPath, byKind);
|
|
75
|
+
fileOrder.push(normPath);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const kind = r.kind || "symbol";
|
|
79
|
+
let items = byKind.get(kind);
|
|
80
|
+
if (!items) {
|
|
81
|
+
items = [];
|
|
82
|
+
byKind.set(kind, items);
|
|
83
|
+
}
|
|
84
|
+
items.push(r);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 2. Build sections
|
|
88
|
+
const sections: string[] = [];
|
|
89
|
+
|
|
90
|
+
for (const filePath of fileOrder) {
|
|
91
|
+
const byKind = byFile.get(filePath)!;
|
|
92
|
+
const fileLines: string[] = [filePath];
|
|
93
|
+
|
|
94
|
+
for (const [kind, items] of byKind) {
|
|
95
|
+
if (items.length === 1) {
|
|
96
|
+
const r = items[0];
|
|
97
|
+
const span = formatRange(r.line, r.endLine);
|
|
98
|
+
const sig = formatSymbolSignature(r);
|
|
99
|
+
fileLines.push(`[${kind}] ${span} ${sig}`);
|
|
100
|
+
if (includeBody && (r.codeBlock !== undefined || r.bodyTruncated)) {
|
|
101
|
+
fileLines.push(indentBody(r.codeBlock, r.bodyTruncated, r.visibleEndLine));
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
fileLines.push(`[${kind}]`);
|
|
105
|
+
for (const r of items) {
|
|
106
|
+
const span = formatRange(r.line, r.endLine);
|
|
107
|
+
const sig = formatSymbolSignature(r);
|
|
108
|
+
fileLines.push(`- ${span} ${sig}`);
|
|
109
|
+
if (includeBody && (r.codeBlock !== undefined || r.bodyTruncated)) {
|
|
110
|
+
fileLines.push(indentBody(r.codeBlock, r.bodyTruncated, r.visibleEndLine));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
sections.push(fileLines.join("\n"));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return sections.join("\n\n");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function formatRange(startLine: number, endLine?: number): string {
|
|
123
|
+
if (endLine !== undefined && endLine !== null && endLine > startLine) {
|
|
124
|
+
return `${startLine}-${endLine}`;
|
|
125
|
+
}
|
|
126
|
+
return `${startLine}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function formatSymbolSignature(r: AstQueryResult): string {
|
|
130
|
+
const name = r.name?.trim() || "unknown";
|
|
131
|
+
let sig = cleanSignature(r.signature, r.name);
|
|
132
|
+
|
|
133
|
+
if (r.aliasedFrom && r.aliasedFrom.originalName && r.aliasedFrom.originalName !== name) {
|
|
134
|
+
const orig = r.aliasedFrom.originalName;
|
|
135
|
+
if (sig === orig) {
|
|
136
|
+
sig = name;
|
|
137
|
+
} else if (sig.startsWith(orig + "(") || sig.startsWith(orig + "<") || sig.startsWith(orig + " ")) {
|
|
138
|
+
sig = name + sig.slice(orig.length);
|
|
139
|
+
}
|
|
140
|
+
sig = `${sig} [alias of ${orig}]`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return sig;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function indentBody(codeBlock?: string, bodyTruncated?: boolean, visibleEndLine?: number): string {
|
|
147
|
+
const indentedLines: string[] = [];
|
|
148
|
+
if (codeBlock && codeBlock.trim()) {
|
|
149
|
+
for (const line of codeBlock.split("\n")) {
|
|
150
|
+
indentedLines.push(` ${line}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (bodyTruncated) {
|
|
154
|
+
if (visibleEndLine !== undefined && visibleEndLine !== null) {
|
|
155
|
+
indentedLines.push(` [... body preview truncated at line ${visibleEndLine}]`);
|
|
156
|
+
} else {
|
|
157
|
+
indentedLines.push(` [... body preview truncated]`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return indentedLines.join("\n");
|
|
161
|
+
}
|
|
@@ -3,7 +3,9 @@ import { Type } from "typebox";
|
|
|
3
3
|
import { Text, makeOutputText } from "../ui/tui_utils";
|
|
4
4
|
import { searchAstSymbols } from "../retrieval/ast_search";
|
|
5
5
|
import { globalEpistemicGuard } from "../safety/epistemic_guard";
|
|
6
|
+
import { TreeSitterEngine } from "../retrieval/tree_sitter_engine";
|
|
6
7
|
import type { SessionDeps } from "./context";
|
|
8
|
+
import { formatAstSearchResults } from "./ast_search_formatter";
|
|
7
9
|
|
|
8
10
|
/** Extracted from index.ts — registers the `ast_search` tool. */
|
|
9
11
|
export function registerAstSearchTool(
|
|
@@ -54,6 +56,15 @@ export function registerAstSearchTool(
|
|
|
54
56
|
{ type: "text", text: `Searching AST for "${params.name || "*"}"...` },
|
|
55
57
|
],
|
|
56
58
|
});
|
|
59
|
+
|
|
60
|
+
// If query specifies a filePattern with an extension, ensure its grammar is loaded
|
|
61
|
+
if (params.filePattern) {
|
|
62
|
+
const ext = (params.filePattern.match(/\.[a-zA-Z0-9]+$/) || [])[0]?.toLowerCase();
|
|
63
|
+
if (ext && TreeSitterEngine.getInstance().isSupported(ext)) {
|
|
64
|
+
await TreeSitterEngine.getInstance().loadLanguages([ext]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
57
68
|
const results = searchAstSymbols(ctx.cwd, {
|
|
58
69
|
name: params.name,
|
|
59
70
|
kind: params.kind,
|
|
@@ -88,21 +99,17 @@ export function registerAstSearchTool(
|
|
|
88
99
|
};
|
|
89
100
|
}
|
|
90
101
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
str += `\n${r.codeBlock}`;
|
|
97
|
-
}
|
|
98
|
-
return str;
|
|
99
|
-
});
|
|
102
|
+
const displayedResults = results.slice(0, 30);
|
|
103
|
+
const formatted = formatAstSearchResults(
|
|
104
|
+
displayedResults,
|
|
105
|
+
params.includeBody ?? false,
|
|
106
|
+
);
|
|
100
107
|
|
|
101
108
|
return {
|
|
102
109
|
content: [
|
|
103
110
|
{
|
|
104
111
|
type: "text",
|
|
105
|
-
text: formatted
|
|
112
|
+
text: formatted,
|
|
106
113
|
},
|
|
107
114
|
],
|
|
108
115
|
details: { count: results.length },
|
package/src/tools/context.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared dependencies injected from the kernel entry point into tool modules.
|
|
3
|
-
* Keeps tool registration pure: no hidden singletons, everything flows through
|
|
4
|
-
* these explicit contracts.
|
|
5
|
-
*/
|
|
6
|
-
export interface SessionDeps {
|
|
7
|
-
/** Resolve stable session id for epistemic-guard bookkeeping. */
|
|
8
|
-
getSessionId: (ctx: any) => string;
|
|
9
|
-
/** Load runtime settings for the active workspace. */
|
|
10
|
-
getConfig?: (cwd: string) => import("../config").KernelConfig;
|
|
11
|
-
/** Mark a successfully mutated file stale in the workspace index. */
|
|
12
|
-
invalidateSearchFile?: (cwd: string, filePath: string) => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface SearchDeps extends SessionDeps {
|
|
16
|
-
/** Lazily create/access the workspace hybrid search index singleton. */
|
|
17
|
-
getSearchIndex: (
|
|
18
|
-
cwd: string,
|
|
19
|
-
) => import("../retrieval/search_index").HybridSearchIndex;
|
|
20
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Shared dependencies injected from the kernel entry point into tool modules.
|
|
3
|
+
* Keeps tool registration pure: no hidden singletons, everything flows through
|
|
4
|
+
* these explicit contracts.
|
|
5
|
+
*/
|
|
6
|
+
export interface SessionDeps {
|
|
7
|
+
/** Resolve stable session id for epistemic-guard bookkeeping. */
|
|
8
|
+
getSessionId: (ctx: any) => string;
|
|
9
|
+
/** Load runtime settings for the active workspace. */
|
|
10
|
+
getConfig?: (cwd: string) => import("../config").KernelConfig;
|
|
11
|
+
/** Mark a successfully mutated file stale in the workspace index. */
|
|
12
|
+
invalidateSearchFile?: (cwd: string, filePath: string) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SearchDeps extends SessionDeps {
|
|
16
|
+
/** Lazily create/access the workspace hybrid search index singleton. */
|
|
17
|
+
getSearchIndex: (
|
|
18
|
+
cwd: string,
|
|
19
|
+
) => import("../retrieval/search_index").HybridSearchIndex;
|
|
20
|
+
}
|
package/src/tools/lsp_tool.ts
CHANGED
|
@@ -261,7 +261,12 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
|
|
|
261
261
|
const refs = findSymbolReferences(ctx.cwd, targetSym);
|
|
262
262
|
if (refs.length > 0) {
|
|
263
263
|
const formatted = refs
|
|
264
|
-
.map((r) =>
|
|
264
|
+
.map((r) => {
|
|
265
|
+
const col0 = Math.max(0, r.column - 1);
|
|
266
|
+
const endCol0 = col0 + targetSym.length;
|
|
267
|
+
const snippet = windowAround(r.lineText, col0, endCol0, 50);
|
|
268
|
+
return `${r.filePath}:${r.line}:${r.column}: ${snippet}`;
|
|
269
|
+
})
|
|
265
270
|
.join("\n");
|
|
266
271
|
return {
|
|
267
272
|
content: [
|
|
@@ -412,8 +417,14 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
|
|
|
412
417
|
],
|
|
413
418
|
};
|
|
414
419
|
}
|
|
420
|
+
const relPath = path.relative(ctx.cwd, absPath).replace(/\\/g, "/") || absPath;
|
|
415
421
|
return {
|
|
416
|
-
content: [
|
|
422
|
+
content: [
|
|
423
|
+
{
|
|
424
|
+
type: "text",
|
|
425
|
+
text: `${relPath} clean`,
|
|
426
|
+
},
|
|
427
|
+
],
|
|
417
428
|
};
|
|
418
429
|
}
|
|
419
430
|
|
|
@@ -533,9 +544,11 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
|
|
|
533
544
|
let refs = await client.findReferences(absPath, line0, col0);
|
|
534
545
|
if (refs.length > 0) {
|
|
535
546
|
refs = filterRefs(refs);
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
547
|
+
if (refs.length > 0) {
|
|
548
|
+
return {
|
|
549
|
+
content: [{ type: "text", text: formatReferences(refs, ctx.cwd) }],
|
|
550
|
+
};
|
|
551
|
+
}
|
|
539
552
|
}
|
|
540
553
|
const ext = path.extname(absPath).toLowerCase();
|
|
541
554
|
const nonCodeExts = [".md", ".markdown", ".json", ".jsonc", ".yaml", ".yml", ".toml", ".proto", ".txt"];
|