@floydous/pi-agent-kernel 0.2.0 → 0.3.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/CHANGELOG.md +10 -23
- package/README.md +127 -82
- package/docs/testing.md +10 -9
- package/package.json +9 -5
- package/src/config/kernel_config.ts +15 -0
- package/src/dedup/content_store.ts +365 -365
- package/src/dedup/recall_tool.ts +3 -5
- package/src/index.ts +975 -918
- package/src/retrieval/ast_search.ts +1035 -930
- package/src/retrieval/repomap.ts +353 -37
- package/src/retrieval/search_bm25.ts +195 -190
- package/src/retrieval/search_chunker.ts +261 -254
- package/src/retrieval/search_index.ts +21 -10
- package/src/retrieval/symbol_reader.ts +198 -203
- package/src/retrieval/tree_sitter_engine.ts +1 -1
- package/src/safety/epistemic_guard.ts +8 -1
- package/src/tools/ast_search_formatter.ts +161 -0
- package/src/tools/ast_search_tool.ts +22 -18
- package/src/tools/code_search_output.ts +237 -0
- package/src/tools/code_search_tool.ts +52 -18
- package/src/tools/context.ts +20 -20
- package/src/tools/edit_tool.ts +6 -7
- package/src/tools/lsp_tool.ts +207 -241
- package/src/tools/read_tool.ts +38 -19
- package/src/tools/repo_map_tool.ts +1 -2
- package/src/ui/tui_utils.ts +278 -278
- package/static/with-agent-kernel.png +0 -0
- package/static/without-agent-kernel-1.png +0 -0
- package/static/without-agent-kernel-2.png +0 -0
- package/static/without-agent-kernel-3.png +0 -0
|
@@ -1,190 +1,195 @@
|
|
|
1
|
-
import { CodeChunk } from "./search_chunker";
|
|
2
|
-
|
|
3
|
-
export interface BM25SearchResult {
|
|
4
|
-
chunkId: string;
|
|
5
|
-
score: number;
|
|
6
|
-
matches: string[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Specialized code tokenizer handling CamelCase, snake_case, identifiers and symbols.
|
|
11
|
-
*/
|
|
12
|
-
export function tokenizeCode(text: string): string[] {
|
|
13
|
-
if (!text) return [];
|
|
14
|
-
|
|
15
|
-
const rawTokens = text.match(/[a-zA-Z0-9_]+/g) || [];
|
|
16
|
-
const tokens: string[] = [];
|
|
17
|
-
|
|
18
|
-
for (const raw of rawTokens) {
|
|
19
|
-
const lower = raw.toLowerCase();
|
|
20
|
-
if (lower.length >= 2) {
|
|
21
|
-
tokens.push(lower);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Split snake_case
|
|
25
|
-
if (raw.includes("_")) {
|
|
26
|
-
const parts = raw.split("_");
|
|
27
|
-
for (const p of parts) {
|
|
28
|
-
const pl = p.toLowerCase();
|
|
29
|
-
if (pl.length >= 2) tokens.push(pl);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Split camelCase and PascalCase (e.g. "verifySignature" -> "verify", "signature")
|
|
34
|
-
const camelParts = raw.replace(/([a-z])([A-Z])/g, "$1 $2").split(" ");
|
|
35
|
-
if (camelParts.length > 1) {
|
|
36
|
-
for (const cp of camelParts) {
|
|
37
|
-
const cpl = cp.toLowerCase();
|
|
38
|
-
if (cpl.length >= 2) tokens.push(cpl);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return tokens;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export class BM25Engine {
|
|
47
|
-
private k1 = 1.2;
|
|
48
|
-
private b = 0.75;
|
|
49
|
-
|
|
50
|
-
// chunkId -> term frequency map: term -> count
|
|
51
|
-
private docTermFreqs: Map<string, Map<string, number>> = new Map();
|
|
52
|
-
// chunkId -> total token count in document
|
|
53
|
-
private docLengths: Map<string, number> = new Map();
|
|
54
|
-
// term -> document frequency (number of docs containing term)
|
|
55
|
-
private termDocFreqs: Map<string, number> = new Map();
|
|
56
|
-
// term -> Set of chunkIds (Inverted index for O(Σ matches) query scanning)
|
|
57
|
-
private postings: Map<string, Set<string>> = new Map();
|
|
58
|
-
|
|
59
|
-
private totalDocs = 0;
|
|
60
|
-
private avgDocLength = 0;
|
|
61
|
-
|
|
62
|
-
public clear(): void {
|
|
63
|
-
this.docTermFreqs.clear();
|
|
64
|
-
this.docLengths.clear();
|
|
65
|
-
this.termDocFreqs.clear();
|
|
66
|
-
this.postings.clear();
|
|
67
|
-
this.totalDocs = 0;
|
|
68
|
-
this.avgDocLength = 0;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Index a list of CodeChunks into the BM25 inverted index.
|
|
73
|
-
*/
|
|
74
|
-
public indexChunks(chunks: CodeChunk[]): void {
|
|
75
|
-
for (const chunk of chunks) {
|
|
76
|
-
this.addChunk(chunk);
|
|
77
|
-
}
|
|
78
|
-
this.recalculateStats();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public addChunk(chunk: CodeChunk): void {
|
|
82
|
-
// Combine breadcrumb, signature and content with extra weight for signature/name
|
|
83
|
-
const weightedText = `${chunk.symbolName} ${chunk.symbolName} ${chunk.signature} ${chunk.breadcrumb}\n${chunk.content}`;
|
|
84
|
-
const tokens = tokenizeCode(weightedText);
|
|
85
|
-
|
|
86
|
-
const tfMap = new Map<string, number>();
|
|
87
|
-
for (const t of tokens) {
|
|
88
|
-
tfMap.set(t, (tfMap.get(t) || 0) + 1);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.docTermFreqs.set(chunk.id, tfMap);
|
|
92
|
-
this.docLengths.set(chunk.id, tokens.length);
|
|
93
|
-
|
|
94
|
-
for (const term of tfMap.keys()) {
|
|
95
|
-
this.termDocFreqs.set(term, (this.termDocFreqs.get(term) || 0) + 1);
|
|
96
|
-
let chunkSet = this.postings.get(term);
|
|
97
|
-
if (!chunkSet) {
|
|
98
|
-
chunkSet = new Set();
|
|
99
|
-
this.postings.set(term, chunkSet);
|
|
100
|
-
}
|
|
101
|
-
chunkSet.add(chunk.id);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this.totalDocs++;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Remove all chunks associated with a specific relative file path.
|
|
109
|
-
*/
|
|
110
|
-
public removeFile(filePath: string): void {
|
|
111
|
-
const prefix = `${filePath}:`;
|
|
112
|
-
for (const chunkId of Array.from(this.docTermFreqs.keys())) {
|
|
113
|
-
if (chunkId.startsWith(prefix) || chunkId.includes(filePath)) {
|
|
114
|
-
const tfMap = this.docTermFreqs.get(chunkId);
|
|
115
|
-
if (tfMap) {
|
|
116
|
-
for (const term of tfMap.keys()) {
|
|
117
|
-
const count = (this.termDocFreqs.get(term) || 1) - 1;
|
|
118
|
-
if (count <= 0) {
|
|
119
|
-
this.termDocFreqs.delete(term);
|
|
120
|
-
} else {
|
|
121
|
-
this.termDocFreqs.set(term, count);
|
|
122
|
-
}
|
|
123
|
-
const chunkSet = this.postings.get(term);
|
|
124
|
-
if (chunkSet) {
|
|
125
|
-
chunkSet.delete(chunkId);
|
|
126
|
-
if (chunkSet.size === 0) {
|
|
127
|
-
this.postings.delete(term);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
this.docTermFreqs.delete(chunkId);
|
|
133
|
-
this.docLengths.delete(chunkId);
|
|
134
|
-
this.totalDocs--;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
this.recalculateStats();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public recalculateStats(): void {
|
|
141
|
-
let totalLength = 0;
|
|
142
|
-
for (const len of this.docLengths.values()) {
|
|
143
|
-
totalLength += len;
|
|
144
|
-
}
|
|
145
|
-
this.totalDocs = this.docLengths.size;
|
|
146
|
-
this.avgDocLength = this.totalDocs > 0 ? totalLength / this.totalDocs : 0;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Search the BM25 index with a query string.
|
|
151
|
-
*/
|
|
152
|
-
public search(
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
1
|
+
import { CodeChunk } from "./search_chunker";
|
|
2
|
+
|
|
3
|
+
export interface BM25SearchResult {
|
|
4
|
+
chunkId: string;
|
|
5
|
+
score: number;
|
|
6
|
+
matches: string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Specialized code tokenizer handling CamelCase, snake_case, identifiers and symbols.
|
|
11
|
+
*/
|
|
12
|
+
export function tokenizeCode(text: string): string[] {
|
|
13
|
+
if (!text) return [];
|
|
14
|
+
|
|
15
|
+
const rawTokens = text.match(/[a-zA-Z0-9_]+/g) || [];
|
|
16
|
+
const tokens: string[] = [];
|
|
17
|
+
|
|
18
|
+
for (const raw of rawTokens) {
|
|
19
|
+
const lower = raw.toLowerCase();
|
|
20
|
+
if (lower.length >= 2) {
|
|
21
|
+
tokens.push(lower);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Split snake_case
|
|
25
|
+
if (raw.includes("_")) {
|
|
26
|
+
const parts = raw.split("_");
|
|
27
|
+
for (const p of parts) {
|
|
28
|
+
const pl = p.toLowerCase();
|
|
29
|
+
if (pl.length >= 2) tokens.push(pl);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Split camelCase and PascalCase (e.g. "verifySignature" -> "verify", "signature")
|
|
34
|
+
const camelParts = raw.replace(/([a-z])([A-Z])/g, "$1 $2").split(" ");
|
|
35
|
+
if (camelParts.length > 1) {
|
|
36
|
+
for (const cp of camelParts) {
|
|
37
|
+
const cpl = cp.toLowerCase();
|
|
38
|
+
if (cpl.length >= 2) tokens.push(cpl);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return tokens;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class BM25Engine {
|
|
47
|
+
private k1 = 1.2;
|
|
48
|
+
private b = 0.75;
|
|
49
|
+
|
|
50
|
+
// chunkId -> term frequency map: term -> count
|
|
51
|
+
private docTermFreqs: Map<string, Map<string, number>> = new Map();
|
|
52
|
+
// chunkId -> total token count in document
|
|
53
|
+
private docLengths: Map<string, number> = new Map();
|
|
54
|
+
// term -> document frequency (number of docs containing term)
|
|
55
|
+
private termDocFreqs: Map<string, number> = new Map();
|
|
56
|
+
// term -> Set of chunkIds (Inverted index for O(Σ matches) query scanning)
|
|
57
|
+
private postings: Map<string, Set<string>> = new Map();
|
|
58
|
+
|
|
59
|
+
private totalDocs = 0;
|
|
60
|
+
private avgDocLength = 0;
|
|
61
|
+
|
|
62
|
+
public clear(): void {
|
|
63
|
+
this.docTermFreqs.clear();
|
|
64
|
+
this.docLengths.clear();
|
|
65
|
+
this.termDocFreqs.clear();
|
|
66
|
+
this.postings.clear();
|
|
67
|
+
this.totalDocs = 0;
|
|
68
|
+
this.avgDocLength = 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Index a list of CodeChunks into the BM25 inverted index.
|
|
73
|
+
*/
|
|
74
|
+
public indexChunks(chunks: CodeChunk[]): void {
|
|
75
|
+
for (const chunk of chunks) {
|
|
76
|
+
this.addChunk(chunk);
|
|
77
|
+
}
|
|
78
|
+
this.recalculateStats();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public addChunk(chunk: CodeChunk): void {
|
|
82
|
+
// Combine breadcrumb, signature and content with extra weight for signature/name
|
|
83
|
+
const weightedText = `${chunk.symbolName} ${chunk.symbolName} ${chunk.signature} ${chunk.breadcrumb}\n${chunk.content}`;
|
|
84
|
+
const tokens = tokenizeCode(weightedText);
|
|
85
|
+
|
|
86
|
+
const tfMap = new Map<string, number>();
|
|
87
|
+
for (const t of tokens) {
|
|
88
|
+
tfMap.set(t, (tfMap.get(t) || 0) + 1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.docTermFreqs.set(chunk.id, tfMap);
|
|
92
|
+
this.docLengths.set(chunk.id, tokens.length);
|
|
93
|
+
|
|
94
|
+
for (const term of tfMap.keys()) {
|
|
95
|
+
this.termDocFreqs.set(term, (this.termDocFreqs.get(term) || 0) + 1);
|
|
96
|
+
let chunkSet = this.postings.get(term);
|
|
97
|
+
if (!chunkSet) {
|
|
98
|
+
chunkSet = new Set();
|
|
99
|
+
this.postings.set(term, chunkSet);
|
|
100
|
+
}
|
|
101
|
+
chunkSet.add(chunk.id);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.totalDocs++;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Remove all chunks associated with a specific relative file path.
|
|
109
|
+
*/
|
|
110
|
+
public removeFile(filePath: string): void {
|
|
111
|
+
const prefix = `${filePath}:`;
|
|
112
|
+
for (const chunkId of Array.from(this.docTermFreqs.keys())) {
|
|
113
|
+
if (chunkId.startsWith(prefix) || chunkId.includes(filePath)) {
|
|
114
|
+
const tfMap = this.docTermFreqs.get(chunkId);
|
|
115
|
+
if (tfMap) {
|
|
116
|
+
for (const term of tfMap.keys()) {
|
|
117
|
+
const count = (this.termDocFreqs.get(term) || 1) - 1;
|
|
118
|
+
if (count <= 0) {
|
|
119
|
+
this.termDocFreqs.delete(term);
|
|
120
|
+
} else {
|
|
121
|
+
this.termDocFreqs.set(term, count);
|
|
122
|
+
}
|
|
123
|
+
const chunkSet = this.postings.get(term);
|
|
124
|
+
if (chunkSet) {
|
|
125
|
+
chunkSet.delete(chunkId);
|
|
126
|
+
if (chunkSet.size === 0) {
|
|
127
|
+
this.postings.delete(term);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
this.docTermFreqs.delete(chunkId);
|
|
133
|
+
this.docLengths.delete(chunkId);
|
|
134
|
+
this.totalDocs--;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
this.recalculateStats();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public recalculateStats(): void {
|
|
141
|
+
let totalLength = 0;
|
|
142
|
+
for (const len of this.docLengths.values()) {
|
|
143
|
+
totalLength += len;
|
|
144
|
+
}
|
|
145
|
+
this.totalDocs = this.docLengths.size;
|
|
146
|
+
this.avgDocLength = this.totalDocs > 0 ? totalLength / this.totalDocs : 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Search the BM25 index with a query string.
|
|
151
|
+
*/
|
|
152
|
+
public search(
|
|
153
|
+
query: string,
|
|
154
|
+
limit = 50,
|
|
155
|
+
filter?: (chunkId: string) => boolean,
|
|
156
|
+
): BM25SearchResult[] {
|
|
157
|
+
if (this.totalDocs === 0) return [];
|
|
158
|
+
const queryTokens = Array.from(new Set(tokenizeCode(query)));
|
|
159
|
+
if (queryTokens.length === 0) return [];
|
|
160
|
+
|
|
161
|
+
const scores: Map<string, { score: number; matches: string[] }> = new Map();
|
|
162
|
+
|
|
163
|
+
for (const term of queryTokens) {
|
|
164
|
+
const n = this.termDocFreqs.get(term) || 0;
|
|
165
|
+
if (n === 0) continue;
|
|
166
|
+
|
|
167
|
+
// Okapi BM25 Inverse Document Frequency
|
|
168
|
+
const idf = Math.log(1 + (this.totalDocs - n + 0.5) / (n + 0.5));
|
|
169
|
+
const matchingChunkIds = this.postings.get(term);
|
|
170
|
+
if (!matchingChunkIds) continue;
|
|
171
|
+
|
|
172
|
+
for (const chunkId of matchingChunkIds) {
|
|
173
|
+
if (filter && !filter(chunkId)) continue;
|
|
174
|
+
const tfMap = this.docTermFreqs.get(chunkId);
|
|
175
|
+
if (!tfMap) continue;
|
|
176
|
+
const tf = tfMap.get(term) || 0;
|
|
177
|
+
if (tf === 0) continue;
|
|
178
|
+
|
|
179
|
+
const docLen = this.docLengths.get(chunkId) || this.avgDocLength;
|
|
180
|
+
const tfNorm = (tf * (this.k1 + 1)) / (tf + this.k1 * (1 - this.b + this.b * (docLen / (this.avgDocLength || 1))));
|
|
181
|
+
const termScore = idf * tfNorm;
|
|
182
|
+
|
|
183
|
+
const current = scores.get(chunkId) || { score: 0, matches: [] };
|
|
184
|
+
current.score += termScore;
|
|
185
|
+
current.matches.push(term);
|
|
186
|
+
scores.set(chunkId, current);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return Array.from(scores.entries())
|
|
191
|
+
.map(([chunkId, data]) => ({ chunkId, score: data.score, matches: data.matches }))
|
|
192
|
+
.sort((a, b) => b.score - a.score)
|
|
193
|
+
.slice(0, limit);
|
|
194
|
+
}
|
|
195
|
+
}
|