@floydous/pi-agent-kernel 0.1.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/LICENSE +15 -0
- package/README.md +108 -0
- package/config.toml +24 -0
- package/docs/README.md +22 -0
- package/docs/architecture.md +40 -0
- package/docs/configuration.md +43 -0
- package/docs/editing-and-verification.md +41 -0
- package/docs/lsp.md +31 -0
- package/docs/retrieval.md +42 -0
- package/docs/testing.md +32 -0
- package/package.json +48 -0
- package/src/config/index.ts +2 -0
- package/src/config/kernel_config.ts +218 -0
- package/src/config/toml.ts +237 -0
- package/src/context/session_repair.ts +99 -0
- package/src/dedup/content_store.ts +365 -0
- package/src/dedup/recall_tool.ts +157 -0
- package/src/editing/patch.ts +482 -0
- package/src/editing/post_edit_verification.ts +200 -0
- package/src/editing/syntax-verify.ts +353 -0
- package/src/index.ts +852 -0
- package/src/lsp/adapters/base.ts +42 -0
- package/src/lsp/adapters/index.ts +21 -0
- package/src/lsp/adapters/python.ts +37 -0
- package/src/lsp/adapters/rust.ts +35 -0
- package/src/lsp/adapters/typescript.ts +40 -0
- package/src/lsp/index.ts +13 -0
- package/src/lsp/lsp_client.ts +650 -0
- package/src/lsp/lsp_detector.ts +52 -0
- package/src/lsp/lsp_formatter.ts +284 -0
- package/src/lsp/lsp_installer.ts +604 -0
- package/src/lsp/lsp_manager.ts +202 -0
- package/src/lsp/lsp_modal.ts +675 -0
- package/src/lsp/lsp_registry.ts +539 -0
- package/src/lsp/lsp_types.ts +143 -0
- package/src/retrieval/ast_search.ts +926 -0
- package/src/retrieval/repomap.ts +779 -0
- package/src/retrieval/search_bm25.ts +190 -0
- package/src/retrieval/search_chunker.ts +251 -0
- package/src/retrieval/search_config.ts +162 -0
- package/src/retrieval/search_embedder.ts +362 -0
- package/src/retrieval/search_index.ts +753 -0
- package/src/retrieval/search_modal.ts +358 -0
- package/src/retrieval/symbol_reader.ts +200 -0
- package/src/safety/atomic_write.ts +26 -0
- package/src/safety/epistemic_guard.ts +733 -0
- package/src/safety/kernel_debug.ts +15 -0
- package/src/safety/output_clamper.ts +213 -0
- package/src/tools/ast_search_tool.ts +131 -0
- package/src/tools/code_search_tool.ts +147 -0
- package/src/tools/context.ts +20 -0
- package/src/tools/edit_tool.ts +288 -0
- package/src/tools/lsp_tool.ts +813 -0
- package/src/tools/read_tool.ts +348 -0
- package/src/tools/repo_map_tool.ts +57 -0
- package/src/ui/footer.ts +287 -0
- package/src/ui/tui_utils.ts +278 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { Text, makeOutputText } from "../ui/tui_utils";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import { kernelDebug } from "../safety/kernel_debug";
|
|
7
|
+
import {
|
|
8
|
+
applySurgicalPatch,
|
|
9
|
+
applyMultiBlockPatch,
|
|
10
|
+
findSurgicalPatchTargetRange,
|
|
11
|
+
} from "../editing/patch";
|
|
12
|
+
import type { EvidenceRange } from "../safety/epistemic_guard";
|
|
13
|
+
import {
|
|
14
|
+
renderEditFailure,
|
|
15
|
+
renderPostEditVerification,
|
|
16
|
+
verifyEditedFile,
|
|
17
|
+
} from "../editing/post_edit_verification";
|
|
18
|
+
import { LspManager } from "../lsp";
|
|
19
|
+
import {
|
|
20
|
+
globalEpistemicGuard,
|
|
21
|
+
resolveUserPath,
|
|
22
|
+
} from "../safety/epistemic_guard";
|
|
23
|
+
import { loadKernelConfig } from "../config";
|
|
24
|
+
import type { SessionDeps } from "./context";
|
|
25
|
+
|
|
26
|
+
/** Extracted from index.ts — registers the `edit` tool. */
|
|
27
|
+
export function registerEditTool(pi: ExtensionAPI, deps: SessionDeps): void {
|
|
28
|
+
// 7. Tool: `edit` (Unified Surgical Diff & Multi-Block Editor - Replaces stock edit tool)
|
|
29
|
+
const editToolDefinition: any = {
|
|
30
|
+
name: "edit",
|
|
31
|
+
label: "Surgical Code Editor",
|
|
32
|
+
description:
|
|
33
|
+
"Surgically edit code using search/replace blocks with multi-strategy fuzzy matching and automatic syntax verification. Supports single search/replace and multi-block edits.",
|
|
34
|
+
promptSnippet:
|
|
35
|
+
"Surgically edit code using search/replace blocks with automatic syntax verification",
|
|
36
|
+
renderShell: "default",
|
|
37
|
+
parameters: Type.Object({
|
|
38
|
+
path: Type.String({
|
|
39
|
+
description: "File path (absolute or relative) to the file to edit",
|
|
40
|
+
}),
|
|
41
|
+
search: Type.Optional(
|
|
42
|
+
Type.String({
|
|
43
|
+
description: "Exact or near-exact lines of code to replace (single block)",
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
replace: Type.Optional(
|
|
47
|
+
Type.String({ description: "New replacement code lines (single block)" }),
|
|
48
|
+
),
|
|
49
|
+
edits: Type.Optional(
|
|
50
|
+
Type.Array(
|
|
51
|
+
Type.Object({
|
|
52
|
+
search: Type.String({ description: "Search block" }),
|
|
53
|
+
replace: Type.String({ description: "Replacement block" }),
|
|
54
|
+
}),
|
|
55
|
+
{
|
|
56
|
+
description:
|
|
57
|
+
"Optional list of multiple disjoint search/replace blocks to apply atomically",
|
|
58
|
+
},
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
}),
|
|
62
|
+
async execute(
|
|
63
|
+
_toolCallId: string,
|
|
64
|
+
params: any,
|
|
65
|
+
_signal: any,
|
|
66
|
+
onUpdate: any,
|
|
67
|
+
ctx: any,
|
|
68
|
+
): Promise<any> {
|
|
69
|
+
if (!params || typeof params !== "object" || !params.path) {
|
|
70
|
+
return {
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
type: "text",
|
|
74
|
+
text: `[EDIT ERROR] Missing required 'path' parameter or malformed argument payload.`,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
isError: true,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const resolvedPath = resolveUserPath(params.path, ctx.cwd);
|
|
82
|
+
|
|
83
|
+
// 1. Read-Before-Write Epistemic Guard Check
|
|
84
|
+
const config = deps.getConfig?.(ctx.cwd) ?? loadKernelConfig(ctx.cwd);
|
|
85
|
+
const targetRanges: EvidenceRange[] = [];
|
|
86
|
+
const hasSingleBlock =
|
|
87
|
+
typeof params.search === "string" && typeof params.replace === "string";
|
|
88
|
+
const hasMultiBlock = Array.isArray(params.edits) && params.edits.length > 0;
|
|
89
|
+
if (!hasSingleBlock && !hasMultiBlock) {
|
|
90
|
+
return {
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
type: "text",
|
|
94
|
+
text: "[EDIT ERROR] Must provide either 'search' and 'replace' strings, or an 'edits' array of search/replace blocks.",
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
isError: true,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (hasSingleBlock) {
|
|
101
|
+
const targetRange = findSurgicalPatchTargetRange(resolvedPath, params.search);
|
|
102
|
+
if (targetRange) targetRanges.push(targetRange);
|
|
103
|
+
} else {
|
|
104
|
+
// Multi-block patching reports ranges after applying blocks; preflight
|
|
105
|
+
// uses the original file's coordinates for each search block.
|
|
106
|
+
for (const block of params.edits) {
|
|
107
|
+
if (!block || typeof block.search !== "string") continue;
|
|
108
|
+
const targetRange = findSurgicalPatchTargetRange(resolvedPath, block.search);
|
|
109
|
+
if (targetRange) targetRanges.push(targetRange);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const epistemicCheck = globalEpistemicGuard.checkReadPrecondition(
|
|
113
|
+
resolvedPath,
|
|
114
|
+
"edit",
|
|
115
|
+
deps.getSessionId(ctx),
|
|
116
|
+
ctx.cwd,
|
|
117
|
+
config.safety.enable_epistemic_guard,
|
|
118
|
+
targetRanges,
|
|
119
|
+
);
|
|
120
|
+
if (!epistemicCheck.allowed) {
|
|
121
|
+
return {
|
|
122
|
+
content: [
|
|
123
|
+
{
|
|
124
|
+
type: "text",
|
|
125
|
+
text:
|
|
126
|
+
epistemicCheck.reason || "[ERROR] Epistemic read pre-condition failed.",
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
isError: true,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
onUpdate?.({
|
|
134
|
+
content: [{ type: "text", text: `Editing ${params.path}...` }],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let patchRes: ReturnType<typeof applySurgicalPatch>;
|
|
138
|
+
if (hasMultiBlock) {
|
|
139
|
+
patchRes = applyMultiBlockPatch(resolvedPath, params.edits);
|
|
140
|
+
} else if (hasSingleBlock) {
|
|
141
|
+
patchRes = applySurgicalPatch(resolvedPath, params.search, params.replace);
|
|
142
|
+
} else {
|
|
143
|
+
return {
|
|
144
|
+
content: [
|
|
145
|
+
{
|
|
146
|
+
type: "text",
|
|
147
|
+
text: `[EDIT ERROR] Must provide either 'search' and 'replace' strings, or an 'edits' array of search/replace blocks.`,
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
isError: true,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!patchRes.success) {
|
|
155
|
+
return {
|
|
156
|
+
content: [
|
|
157
|
+
{
|
|
158
|
+
type: "text",
|
|
159
|
+
text: renderEditFailure(patchRes.error || "patch failed"),
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
details: { error: patchRes.error, success: false },
|
|
163
|
+
isError: true,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Read back the exact content as written to disk
|
|
168
|
+
let diskContent: string;
|
|
169
|
+
try {
|
|
170
|
+
diskContent = fs.readFileSync(resolvedPath, "utf8");
|
|
171
|
+
} catch {
|
|
172
|
+
diskContent = "";
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Calculate net line count difference from the edit blocks
|
|
176
|
+
let deltaLines = 0;
|
|
177
|
+
if (hasSingleBlock) {
|
|
178
|
+
const searchLines = params.search.replace(/\r\n/g, "\n").split("\n").length;
|
|
179
|
+
const replaceLines = params.replace.replace(/\r\n/g, "\n").split("\n").length;
|
|
180
|
+
deltaLines = replaceLines - searchLines;
|
|
181
|
+
} else if (hasMultiBlock) {
|
|
182
|
+
for (const block of params.edits) {
|
|
183
|
+
if (block && typeof block.search === "string" && typeof block.replace === "string") {
|
|
184
|
+
const sLines = block.search.replace(/\r\n/g, "\n").split("\n").length;
|
|
185
|
+
const rLines = block.replace.replace(/\r\n/g, "\n").split("\n").length;
|
|
186
|
+
deltaLines += (rLines - sLines);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Update the Epistemic Guard ledger with the freshly written file snapshot.
|
|
192
|
+
// This maintains authorization for subsequent edits in the same session without
|
|
193
|
+
// forcing redundant reads, while preserving strict protection against external file drift.
|
|
194
|
+
globalEpistemicGuard.recordFileMutation(
|
|
195
|
+
resolvedPath,
|
|
196
|
+
deps.getSessionId(ctx),
|
|
197
|
+
ctx.cwd,
|
|
198
|
+
diskContent,
|
|
199
|
+
{
|
|
200
|
+
targetRanges: patchRes.targetRanges || targetRanges,
|
|
201
|
+
deltaLines,
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// The file was mutated even when post-edit diagnostics later report a
|
|
206
|
+
// problem; never leave the search index serving its pre-edit content.
|
|
207
|
+
deps.invalidateSearchFile?.(ctx.cwd, resolvedPath);
|
|
208
|
+
|
|
209
|
+
// Verify locally first. Reuse an already-ready LSP client only; edit
|
|
210
|
+
// verification must not spawn a server or trigger broad analysis.
|
|
211
|
+
const readyLsp = LspManager.getInstance().getReadyClientForFile(
|
|
212
|
+
resolvedPath,
|
|
213
|
+
ctx.cwd,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
// Synchronize LSP client in-memory buffer with the newly patched file
|
|
217
|
+
if (readyLsp && readyLsp.getState() === "ready") {
|
|
218
|
+
try {
|
|
219
|
+
await readyLsp.changeDocument(resolvedPath, diskContent);
|
|
220
|
+
await readyLsp.saveDocument(resolvedPath, diskContent);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
kernelDebug(err);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const verification = await verifyEditedFile(
|
|
226
|
+
resolvedPath,
|
|
227
|
+
readyLsp
|
|
228
|
+
? async () => {
|
|
229
|
+
const result = await readyLsp.getDiagnosticsResult(resolvedPath);
|
|
230
|
+
return {
|
|
231
|
+
state: result.status,
|
|
232
|
+
findings: result.diagnostics.map((finding) => ({
|
|
233
|
+
line: finding.range.start.line + 1,
|
|
234
|
+
column: finding.range.start.character + 1,
|
|
235
|
+
message: finding.message,
|
|
236
|
+
severity:
|
|
237
|
+
finding.severity === 1
|
|
238
|
+
? ("error" as const)
|
|
239
|
+
: finding.severity === 2
|
|
240
|
+
? ("warning" as const)
|
|
241
|
+
: ("info" as const),
|
|
242
|
+
})),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
: undefined,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const statusText = renderPostEditVerification(verification);
|
|
249
|
+
return {
|
|
250
|
+
content: statusText ? [{ type: "text", text: statusText }] : [],
|
|
251
|
+
details: {
|
|
252
|
+
strategy: patchRes.strategy,
|
|
253
|
+
verification,
|
|
254
|
+
success: verification.syntax.state === "clean",
|
|
255
|
+
},
|
|
256
|
+
isError:
|
|
257
|
+
verification.syntax.state === "failed" ||
|
|
258
|
+
verification.diagnostic.findings.some(
|
|
259
|
+
(finding) => finding.severity === "error" || !finding.severity,
|
|
260
|
+
),
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
renderCall(args: any, theme: any, context: any) {
|
|
264
|
+
const rawPath = args?.path || "";
|
|
265
|
+
const relPath = rawPath
|
|
266
|
+
? path.relative(context.cwd, rawPath) || rawPath
|
|
267
|
+
: "";
|
|
268
|
+
return makeOutputText(
|
|
269
|
+
`${theme.fg("toolTitle", theme.bold("edit"))} ${theme.fg("accent", relPath)}`,
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
renderResult(result: any, options: any, theme: any, context: any) {
|
|
273
|
+
if (context.isError) {
|
|
274
|
+
const errMsg =
|
|
275
|
+
result.content?.find((c: any) => c.type === "text")?.text || "Edit failed";
|
|
276
|
+
return makeOutputText(`\n${theme.fg("error", errMsg)}`);
|
|
277
|
+
}
|
|
278
|
+
if (!options.expanded) {
|
|
279
|
+
return new Text("", 0, 0);
|
|
280
|
+
}
|
|
281
|
+
const output =
|
|
282
|
+
result.content?.find((c: any) => c.type === "text")?.text || "";
|
|
283
|
+
return makeOutputText(`\n${theme.fg("toolOutput", output)}`);
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
pi.registerTool(editToolDefinition);
|
|
288
|
+
}
|