@floydous/pi-agent-kernel 0.4.2 → 0.5.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/AGENT_KERNEL_SYS_PROMPT.md +1 -7
- package/CHANGELOG.md +48 -0
- package/README.md +6 -53
- package/config.example.toml +0 -8
- package/docs/README.md +1 -2
- package/docs/architecture.md +2 -3
- package/docs/configuration.md +10 -9
- package/docs/editing-and-verification.md +1 -3
- package/docs/retrieval.md +6 -11
- package/docs/testing.md +3 -3
- package/package.json +4 -2
- package/src/config/agent_kernel_command.ts +8 -39
- package/src/config/kernel_config.ts +30 -68
- package/src/config/toml.ts +6 -1
- package/src/editing/auto_heal.ts +1 -1
- package/src/editing/patch.ts +27 -16
- package/src/editing/post_edit_verification.ts +12 -10
- package/src/editing/smart_anchor.ts +7 -5
- package/src/editing/syntax-verify.ts +117 -15
- package/src/index.ts +15 -257
- package/src/retrieval/search_bm25.ts +4 -2
- package/src/retrieval/search_chunker.ts +1 -1
- package/src/retrieval/search_config.ts +4 -6
- package/src/retrieval/search_embedder.ts +3 -2
- package/src/retrieval/search_index.ts +82 -35
- package/src/retrieval/search_modal.ts +7 -8
- package/src/retrieval/{repomap.ts → tag_extractor.ts} +5 -292
- package/src/retrieval/tree_sitter_engine.ts +70 -34
- package/src/retrieval/workspace_walker.ts +6 -2
- package/src/safety/atomic_write.ts +1 -1
- package/src/safety/epistemic_guard.ts +122 -32
- package/src/safety/output_clamper.ts +57 -35
- package/src/tools/code_search_output.ts +2 -1
- package/src/tools/code_search_tool.ts +2 -5
- package/src/tools/context.ts +0 -14
- package/src/tools/edit_tool.ts +82 -115
- package/src/tools/read_tool.ts +42 -178
- package/src/ui/footer.ts +7 -7
- package/docs/lsp.md +0 -31
- package/src/lsp/adapters/base.ts +0 -42
- package/src/lsp/adapters/index.ts +0 -21
- package/src/lsp/adapters/python.ts +0 -37
- package/src/lsp/adapters/rust.ts +0 -35
- package/src/lsp/adapters/typescript.ts +0 -40
- package/src/lsp/index.ts +0 -13
- package/src/lsp/lsp_client.ts +0 -765
- package/src/lsp/lsp_detector.ts +0 -52
- package/src/lsp/lsp_formatter.ts +0 -323
- package/src/lsp/lsp_installer.ts +0 -320
- package/src/lsp/lsp_manager.ts +0 -202
- package/src/lsp/lsp_modal.ts +0 -675
- package/src/lsp/lsp_registry.ts +0 -600
- package/src/lsp/lsp_types.ts +0 -143
- package/src/retrieval/ast_search.ts +0 -999
- package/src/retrieval/symbol_reader.ts +0 -228
- package/src/tools/ast_search_formatter.ts +0 -161
- package/src/tools/ast_search_tool.ts +0 -146
- package/src/tools/lsp_tool.ts +0 -794
- package/src/tools/repo_map_tool.ts +0 -56
|
@@ -62,17 +62,17 @@ function isOwnedRegularFile(filePath: string): boolean {
|
|
|
62
62
|
function pruneSpillovers(directory: string, maxFiles: number, maxBytes: number): void {
|
|
63
63
|
const files = fs
|
|
64
64
|
.readdirSync(directory)
|
|
65
|
-
.
|
|
66
|
-
|
|
65
|
+
.flatMap((name) => {
|
|
66
|
+
if (!name.startsWith(SPILL_PREFIX)) return [];
|
|
67
67
|
const full = path.join(directory, name);
|
|
68
68
|
try {
|
|
69
69
|
const stat = fs.lstatSync(full);
|
|
70
|
-
|
|
70
|
+
if (!isOwnedRegularFile(full)) return [];
|
|
71
|
+
return [{ full, size: stat.size, mtime: stat.mtimeMs }];
|
|
71
72
|
} catch {
|
|
72
|
-
return
|
|
73
|
+
return [];
|
|
73
74
|
}
|
|
74
75
|
})
|
|
75
|
-
.filter((item) => item.owned)
|
|
76
76
|
.sort((a, b) => a.mtime - b.mtime);
|
|
77
77
|
let totalBytes = files.reduce((sum, file) => sum + file.size, 0);
|
|
78
78
|
let remainingFiles = files.length;
|
|
@@ -146,10 +146,10 @@ export function isExecutionOrTestCommand(command: string): boolean {
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const HOST_TRUNCATION_REGEX =
|
|
149
|
-
/(?:\r?\n){2}\[Showing (?:lines \d+-\d+ of \d+(?: \([^)]+\))?|last [^)]+\))
|
|
149
|
+
/(?:\r?\n){1,2}\[(?:Showing (?:lines \d+-\d+ of \d+(?: \([^)]+\))?|last [^)]+\))\.[ \t]*Full output:|Truncated by host harness\.[ \t]*Full:)[ \t]*([^\]\r\n]+?)(?:[ \t]+\(use offset\/limit with read if needed\))?\][ \t\r\n]*$/;
|
|
150
150
|
|
|
151
151
|
const KERNEL_TRUNCATION_REGEX =
|
|
152
|
-
/(?:\r?\n){2}\[Truncated[^\r\n\]]*?(
|
|
152
|
+
/(?:\r?\n){2}\[Truncated[^\r\n\]]*?(?:\.[ \t]*(?:Full|Spillover):[ \t]*([^\]\r\n]+?)(?:[ \t]+\(use offset\/limit[^)]*\)|[ \t]+\(received fragment only\))?)?\][ \t\r\n]*$/;
|
|
153
153
|
|
|
154
154
|
function validateHostSpilloverPath(candidatePath: string): string | undefined {
|
|
155
155
|
try {
|
|
@@ -219,42 +219,48 @@ export function clampCommandOutput(
|
|
|
219
219
|
let hostSpilloverPath: string | undefined;
|
|
220
220
|
let priorSpilloverPath: string | undefined;
|
|
221
221
|
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
224
|
-
|
|
225
|
-
textToProcess = rawText.slice(0,
|
|
226
|
-
const candidate =
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
222
|
+
const hostMatch = rawText.match(HOST_TRUNCATION_REGEX);
|
|
223
|
+
if (hostMatch && hostMatch.index !== undefined) {
|
|
224
|
+
hostTruncated = true;
|
|
225
|
+
textToProcess = rawText.slice(0, hostMatch.index);
|
|
226
|
+
const candidate = hostMatch[1].trim();
|
|
227
|
+
hostSpilloverPath = validateHostSpilloverPath(candidate);
|
|
228
|
+
} else {
|
|
229
|
+
const kernelMatch = rawText.match(KERNEL_TRUNCATION_REGEX);
|
|
230
|
+
if (kernelMatch && kernelMatch.index !== undefined) {
|
|
231
|
+
priorKernelTruncated = true;
|
|
232
|
+
textToProcess = rawText.slice(0, kernelMatch.index);
|
|
233
|
+
const candidate = kernelMatch[1]?.trim();
|
|
234
|
+
if (candidate) {
|
|
235
|
+
const cleanCandidate = candidate.replace(/\s*\(use offset\/limit.*\)$/i, "").trim();
|
|
236
|
+
hostSpilloverPath = validateHostSpilloverPath(cleanCandidate);
|
|
237
|
+
if (!hostSpilloverPath && fs.existsSync(cleanCandidate)) {
|
|
238
|
+
try {
|
|
239
|
+
const lstat = fs.lstatSync(cleanCandidate);
|
|
240
|
+
if (!lstat.isSymbolicLink() && lstat.isFile()) {
|
|
241
|
+
priorSpilloverPath = path.resolve(cleanCandidate);
|
|
242
|
+
}
|
|
243
|
+
} catch (e) {
|
|
244
|
+
kernelDebug(e);
|
|
235
245
|
}
|
|
236
|
-
}
|
|
246
|
+
}
|
|
237
247
|
}
|
|
238
248
|
}
|
|
239
|
-
} else {
|
|
240
|
-
const hostMatch = rawText.match(HOST_TRUNCATION_REGEX);
|
|
241
|
-
if (hostMatch && hostMatch.index !== undefined) {
|
|
242
|
-
hostTruncated = true;
|
|
243
|
-
textToProcess = rawText.slice(0, hostMatch.index);
|
|
244
|
-
const candidate = hostMatch[1].trim();
|
|
245
|
-
hostSpilloverPath = validateHostSpilloverPath(candidate);
|
|
246
|
-
}
|
|
247
249
|
}
|
|
248
250
|
|
|
249
251
|
let originalBytes = Buffer.byteLength(rawText, "utf8");
|
|
250
252
|
if (hostSpilloverPath) {
|
|
251
253
|
try {
|
|
252
254
|
originalBytes = fs.statSync(hostSpilloverPath).size;
|
|
253
|
-
} catch {
|
|
255
|
+
} catch (e) {
|
|
256
|
+
kernelDebug(e);
|
|
257
|
+
}
|
|
254
258
|
} else if (priorSpilloverPath) {
|
|
255
259
|
try {
|
|
256
260
|
originalBytes = fs.statSync(priorSpilloverPath).size;
|
|
257
|
-
} catch {
|
|
261
|
+
} catch (e) {
|
|
262
|
+
kernelDebug(e);
|
|
263
|
+
}
|
|
258
264
|
}
|
|
259
265
|
|
|
260
266
|
// If the entire output is small, within bounds, and host did not truncate, return as-is
|
|
@@ -344,6 +350,8 @@ export function clampCommandOutput(
|
|
|
344
350
|
/-->\s+\S+:\d+:\d+/,
|
|
345
351
|
];
|
|
346
352
|
|
|
353
|
+
let primaryOmittedRange: { start: number; end: number; limit: number } | undefined;
|
|
354
|
+
|
|
347
355
|
if (totalLines > maxLines) {
|
|
348
356
|
const effectiveMaxLines = Math.max(2, maxLines);
|
|
349
357
|
const isDiscovery = isDiscoveryCommand(command);
|
|
@@ -392,14 +400,22 @@ export function clampCommandOutput(
|
|
|
392
400
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
393
401
|
}
|
|
394
402
|
if (windowStart > headCount) {
|
|
395
|
-
|
|
403
|
+
const count = windowStart - headCount;
|
|
404
|
+
clampedLines.push(`\n[... ${count.toLocaleString()} lines omitted (lines ${headCount + 1}–${windowStart}) ...]\n`);
|
|
405
|
+
if (!primaryOmittedRange || count > primaryOmittedRange.limit) {
|
|
406
|
+
primaryOmittedRange = { start: headCount + 1, end: windowStart, limit: count };
|
|
407
|
+
}
|
|
396
408
|
}
|
|
397
409
|
// Diagnostic window
|
|
398
410
|
for (let i = windowStart; i < windowEnd; i++) {
|
|
399
411
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
400
412
|
}
|
|
401
413
|
if (totalLines - tailCount > windowEnd) {
|
|
402
|
-
|
|
414
|
+
const count = totalLines - tailCount - windowEnd;
|
|
415
|
+
clampedLines.push(`\n[... ${count.toLocaleString()} lines omitted (lines ${windowEnd + 1}–${totalLines - tailCount}) ...]\n`);
|
|
416
|
+
if (!primaryOmittedRange || count > primaryOmittedRange.limit) {
|
|
417
|
+
primaryOmittedRange = { start: windowEnd + 1, end: totalLines - tailCount, limit: count };
|
|
418
|
+
}
|
|
403
419
|
}
|
|
404
420
|
// Tail lines
|
|
405
421
|
for (let i = totalLines - tailCount; i < totalLines; i++) {
|
|
@@ -418,13 +434,15 @@ export function clampCommandOutput(
|
|
|
418
434
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
419
435
|
}
|
|
420
436
|
|
|
437
|
+
const tailStart = totalLines - tailCount;
|
|
438
|
+
|
|
421
439
|
// Middle omission marker
|
|
422
440
|
clampedLines.push(
|
|
423
|
-
`\n[... ${omittedLines.toLocaleString()} lines omitted ...]\n`,
|
|
441
|
+
`\n[... ${omittedLines.toLocaleString()} lines omitted (lines ${headCount + 1}–${tailStart}) ...]\n`,
|
|
424
442
|
);
|
|
443
|
+
primaryOmittedRange = { start: headCount + 1, end: tailStart, limit: omittedLines };
|
|
425
444
|
|
|
426
445
|
// Tail lines
|
|
427
|
-
const tailStart = totalLines - tailCount;
|
|
428
446
|
for (let i = tailStart; i < totalLines; i++) {
|
|
429
447
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
430
448
|
}
|
|
@@ -453,6 +471,10 @@ export function clampCommandOutput(
|
|
|
453
471
|
let f = `\n\n[Truncated: ${linesShown}/${totalLines} lines.`;
|
|
454
472
|
if (spilloverPath) {
|
|
455
473
|
f += ` Full: ${spilloverPath} (use offset/limit with read if needed)]`;
|
|
474
|
+
if (primaryOmittedRange && maxTotalBytes >= 1024) {
|
|
475
|
+
const boundedLimit = Math.min(250, primaryOmittedRange.limit);
|
|
476
|
+
f += `\nTo inspect omitted lines ${primaryOmittedRange.start}–${primaryOmittedRange.end}: read({ path: "${spilloverPath}", offset: ${primaryOmittedRange.start}, limit: ${boundedLimit} })`;
|
|
477
|
+
}
|
|
456
478
|
} else {
|
|
457
479
|
f += `]`;
|
|
458
480
|
}
|
|
@@ -139,7 +139,8 @@ function applyBudget(
|
|
|
139
139
|
let truncated = false;
|
|
140
140
|
|
|
141
141
|
for (let index = 0; index < entries.length; index++) {
|
|
142
|
-
const entry = entries[index]
|
|
142
|
+
const entry = entries[index];
|
|
143
|
+
if (!entry) continue;
|
|
143
144
|
const separator = selected.length > 0 ? "\n\n" : "";
|
|
144
145
|
const candidateLength = separator.length + entry.text.length;
|
|
145
146
|
if (used + candidateLength <= budget) {
|
|
@@ -3,7 +3,7 @@ import { Type } from "typebox";
|
|
|
3
3
|
import { Text, makeOutputText } from "../ui/tui_utils";
|
|
4
4
|
import { globalEpistemicGuard } from "../safety/epistemic_guard";
|
|
5
5
|
import { formatCodeSearchResults, CODE_SEARCH_MODES, type CodeSearchMode } from "./code_search_output";
|
|
6
|
-
import {
|
|
6
|
+
import type { SearchDeps } from "./context";
|
|
7
7
|
|
|
8
8
|
/** Extracted from index.ts — registers the `code_search` tool. */
|
|
9
9
|
export function registerCodeSearchTool(
|
|
@@ -100,10 +100,7 @@ export function registerCodeSearchTool(
|
|
|
100
100
|
content: [
|
|
101
101
|
{
|
|
102
102
|
type: "text",
|
|
103
|
-
text: `${vectorNotice}No code chunks found matching "${query}". (
|
|
104
|
-
(speculativeToolsEnabled(deps.getConfig, ctx.cwd)
|
|
105
|
-
? `Tips: Try 'ast_search' for exact symbol names or 'rg' for exact text literals.`
|
|
106
|
-
: `Tips: Try broader or different terms, or 'rg' for exact text literals.`),
|
|
103
|
+
text: `${vectorNotice}No code chunks found matching "${query}". (Tips: Try broader or different terms, or 'rg' for exact text literals.)`,
|
|
107
104
|
},
|
|
108
105
|
],
|
|
109
106
|
details: { count: 0 },
|
package/src/tools/context.ts
CHANGED
|
@@ -12,20 +12,6 @@ export interface SessionDeps {
|
|
|
12
12
|
invalidateSearchFile?: (cwd: string, filePath: string) => void | Promise<void>;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
* Whether the opt-in tools (`get_repo_map`, `ast_search`, `lsp`) are registered for this
|
|
17
|
-
* workspace. Single source of truth: `src/index.ts` gates registration on this, and user-facing
|
|
18
|
-
* tool hints consult it so they never recommend a tool the model cannot actually call.
|
|
19
|
-
*/
|
|
20
|
-
export function speculativeToolsEnabled(
|
|
21
|
-
getConfig: ((cwd: string) => import("../config").KernelConfig) | undefined,
|
|
22
|
-
cwd: string,
|
|
23
|
-
): boolean {
|
|
24
|
-
const flag = process.env.PI_ENABLE_ALL_RETRIEVAL_TOOLS;
|
|
25
|
-
if (flag === "1" || flag === "true") return true;
|
|
26
|
-
return getConfig?.(cwd)?.retrieval?.enable_tools === true;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
15
|
export interface SearchDeps extends SessionDeps {
|
|
30
16
|
/** Lazily create/access the workspace hybrid search index singleton. */
|
|
31
17
|
getSearchIndex: (
|
package/src/tools/edit_tool.ts
CHANGED
|
@@ -3,12 +3,10 @@ import { Type } from "typebox";
|
|
|
3
3
|
import { Text, makeOutputText } from "../ui/tui_utils";
|
|
4
4
|
import * as path from "node:path";
|
|
5
5
|
import * as fs from "node:fs";
|
|
6
|
-
import { kernelDebug } from "../safety/kernel_debug";
|
|
7
6
|
import { writeFileSyncAtomic } from "../safety/atomic_write";
|
|
8
7
|
import {
|
|
9
8
|
applySurgicalPatch,
|
|
10
9
|
applyMultiBlockPatch,
|
|
11
|
-
findSurgicalPatchTargetRange,
|
|
12
10
|
preflightSurgicalPatchBlock,
|
|
13
11
|
} from "../editing/patch";
|
|
14
12
|
import {
|
|
@@ -22,7 +20,6 @@ import {
|
|
|
22
20
|
renderPostEditVerification,
|
|
23
21
|
verifyEditedFile,
|
|
24
22
|
} from "../editing/post_edit_verification";
|
|
25
|
-
import { LspManager } from "../lsp";
|
|
26
23
|
import {
|
|
27
24
|
globalEpistemicGuard,
|
|
28
25
|
resolveUserPath,
|
|
@@ -40,7 +37,7 @@ export async function withFileMutationLocks<T>(
|
|
|
40
37
|
operation: () => Promise<T>,
|
|
41
38
|
): Promise<T> {
|
|
42
39
|
const { withFileMutationQueue } = await import("@earendil-works/pi-coding-agent");
|
|
43
|
-
const keys = [...new Set(filePaths.map(mutationLockKey))].sort();
|
|
40
|
+
const keys = [...new Set(filePaths.map(mutationLockKey))].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
44
41
|
const run = (index: number): Promise<T> =>
|
|
45
42
|
index >= keys.length
|
|
46
43
|
? operation()
|
|
@@ -297,38 +294,7 @@ async function applyPreparedEdit(
|
|
|
297
294
|
|
|
298
295
|
await deps.invalidateSearchFile?.(ctx.cwd, resolvedPath);
|
|
299
296
|
|
|
300
|
-
const
|
|
301
|
-
if (readyLsp && readyLsp.getState() === "ready") {
|
|
302
|
-
try {
|
|
303
|
-
await readyLsp.changeDocument(resolvedPath, diskContent);
|
|
304
|
-
await readyLsp.saveDocument(resolvedPath, diskContent);
|
|
305
|
-
} catch (err) {
|
|
306
|
-
kernelDebug(err);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const verification = await verifyEditedFile(
|
|
311
|
-
resolvedPath,
|
|
312
|
-
readyLsp
|
|
313
|
-
? async () => {
|
|
314
|
-
const result = await readyLsp.getDiagnosticsResult(resolvedPath);
|
|
315
|
-
return {
|
|
316
|
-
state: result.status,
|
|
317
|
-
findings: result.diagnostics.map((finding) => ({
|
|
318
|
-
line: finding.range.start.line + 1,
|
|
319
|
-
column: finding.range.start.character + 1,
|
|
320
|
-
message: finding.message,
|
|
321
|
-
severity:
|
|
322
|
-
finding.severity === 1
|
|
323
|
-
? ("error" as const)
|
|
324
|
-
: finding.severity === 2
|
|
325
|
-
? ("warning" as const)
|
|
326
|
-
: ("info" as const),
|
|
327
|
-
})),
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
: undefined,
|
|
331
|
-
);
|
|
297
|
+
const verification = await verifyEditedFile(resolvedPath);
|
|
332
298
|
|
|
333
299
|
const statusText = renderPostEditVerification(verification, undefined, relPath);
|
|
334
300
|
return {
|
|
@@ -344,84 +310,94 @@ async function applyPreparedEdit(
|
|
|
344
310
|
export function registerEditTool(pi: ExtensionAPI, deps: SessionDeps): void {
|
|
345
311
|
// 7. Tool: `edit` (Unified Surgical Diff, Smart Anchor, & Multi-Block Editor - Replaces stock edit tool)
|
|
346
312
|
const benchmarkMethod = process.env.PI_EDIT_METHOD?.toLowerCase();
|
|
347
|
-
|
|
348
|
-
|
|
313
|
+
let editToolParameters: any;
|
|
314
|
+
let editToolDescription =
|
|
315
|
+
"Edit code with search/replace, multi-block 'edits', or multi-file 'files'. Valid after read or exact rg search.";
|
|
316
|
+
|
|
317
|
+
switch (benchmarkMethod) {
|
|
318
|
+
case "line-range":
|
|
319
|
+
editToolParameters = Type.Object({
|
|
349
320
|
path: Type.String({ description: "File path to edit" }),
|
|
350
321
|
start_line: Type.Number({ description: "First 1-based line to replace" }),
|
|
351
322
|
end_line: Type.Optional(Type.Number({ description: "Last 1-based line to replace, inclusive" })),
|
|
352
323
|
lines: Type.Array(Type.String(), { description: "Replacement lines (empty to delete)" }),
|
|
353
|
-
})
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
})
|
|
361
|
-
:
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
:
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
324
|
+
});
|
|
325
|
+
editToolDescription = "Edit code with numeric line ranges only: start_line, optional end_line, and replacement lines.";
|
|
326
|
+
break;
|
|
327
|
+
case "hash-anchor":
|
|
328
|
+
editToolParameters = Type.Object({
|
|
329
|
+
path: Type.String({ description: "File path to edit" }),
|
|
330
|
+
pos: Type.String({ description: "Start anchor copied from read output, e.g. '288#ZH'" }),
|
|
331
|
+
end: Type.Optional(Type.String({ description: "End anchor copied from read output, e.g. '294#VN'" })),
|
|
332
|
+
lines: Type.Array(Type.String(), { description: "Replacement lines (empty to delete)" }),
|
|
333
|
+
});
|
|
334
|
+
editToolDescription = "Edit code with LINE#HASH anchors only, copied from read output.";
|
|
335
|
+
break;
|
|
336
|
+
case "search-replace":
|
|
337
|
+
editToolParameters = Type.Object({
|
|
338
|
+
path: Type.String({ description: "File path to edit" }),
|
|
339
|
+
search: Type.String({ description: "Unique exact source block to replace" }),
|
|
340
|
+
replace: Type.String({ description: "Replacement source block" }),
|
|
341
|
+
});
|
|
342
|
+
editToolDescription = "Edit code with a unique exact search block and replacement block only.";
|
|
343
|
+
break;
|
|
344
|
+
case "adaptive":
|
|
345
|
+
editToolParameters = Type.Object({
|
|
346
|
+
path: Type.String({ description: "File path to edit" }),
|
|
347
|
+
search: Type.Optional(Type.String({ description: "Unique exact source block to replace" })),
|
|
348
|
+
replace: Type.Optional(Type.String({ description: "Replacement source block" })),
|
|
349
|
+
start_line: Type.Optional(Type.Number({ description: "First 1-based line to replace" })),
|
|
350
|
+
end_line: Type.Optional(Type.Number({ description: "Last 1-based line to replace, inclusive" })),
|
|
351
|
+
pos: Type.Optional(Type.String({ description: "LINE#HASH anchor copied from read output" })),
|
|
352
|
+
end: Type.Optional(Type.String({ description: "Ending LINE#HASH anchor" })),
|
|
353
|
+
lines: Type.Optional(Type.Array(Type.String(), { description: "Replacement lines (empty to delete)" })),
|
|
354
|
+
});
|
|
355
|
+
editToolDescription = "Edit code with search/replace or line range; use search first.";
|
|
356
|
+
break;
|
|
357
|
+
default:
|
|
358
|
+
editToolParameters = Type.Object(
|
|
359
|
+
{
|
|
360
|
+
path: Type.Optional(Type.String({ description: "Path to file to edit" })),
|
|
361
|
+
search: Type.Optional(Type.String({ description: "Exact block to replace" })),
|
|
362
|
+
replace: Type.Optional(Type.String({ description: "Replacement text" })),
|
|
363
|
+
line_hint: Type.Optional(Type.Number({ description: "Line hint for disambiguation" })),
|
|
364
|
+
edits: Type.Optional(
|
|
365
|
+
Type.Array(
|
|
366
|
+
Type.Object(
|
|
367
|
+
{
|
|
368
|
+
search: Type.Optional(Type.String()),
|
|
369
|
+
replace: Type.Optional(Type.String()),
|
|
370
|
+
line_hint: Type.Optional(Type.Number()),
|
|
371
|
+
},
|
|
372
|
+
{ additionalProperties: true },
|
|
395
373
|
),
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
),
|
|
408
|
-
|
|
409
|
-
|
|
374
|
+
{ description: "Targeted replacements" },
|
|
375
|
+
),
|
|
376
|
+
),
|
|
377
|
+
files: Type.Optional(
|
|
378
|
+
Type.Array(
|
|
379
|
+
Type.Object(
|
|
380
|
+
{
|
|
381
|
+
path: Type.String(),
|
|
382
|
+
search: Type.Optional(Type.String()),
|
|
383
|
+
replace: Type.Optional(Type.String()),
|
|
384
|
+
line_hint: Type.Optional(Type.Number()),
|
|
385
|
+
edits: Type.Optional(Type.Array(Type.Any())),
|
|
386
|
+
},
|
|
387
|
+
{ additionalProperties: true },
|
|
410
388
|
),
|
|
411
|
-
|
|
389
|
+
{ description: "Multi-file edits" },
|
|
390
|
+
),
|
|
391
|
+
),
|
|
392
|
+
},
|
|
393
|
+
{ additionalProperties: true },
|
|
394
|
+
);
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
412
397
|
const editToolDefinition: any = {
|
|
413
398
|
name: "edit",
|
|
414
399
|
label: "Code Editor",
|
|
415
|
-
description:
|
|
416
|
-
benchmarkMethod === "line-range"
|
|
417
|
-
? "Edit code with numeric line ranges only: start_line, optional end_line, and replacement lines."
|
|
418
|
-
: benchmarkMethod === "hash-anchor"
|
|
419
|
-
? "Edit code with LINE#HASH anchors only, copied from read output."
|
|
420
|
-
: benchmarkMethod === "search-replace"
|
|
421
|
-
? "Edit code with a unique exact search block and replacement block only."
|
|
422
|
-
: benchmarkMethod === "adaptive"
|
|
423
|
-
? "Edit code with search/replace or line range; use search first."
|
|
424
|
-
: "Edit code with search/replace, multi-block 'edits', or multi-file 'files'. Valid after read or exact rg search.",
|
|
400
|
+
description: editToolDescription,
|
|
425
401
|
promptSnippet: "Edit code using search/replace (optional line_hint), multi-block 'edits', or multi-file 'files'",
|
|
426
402
|
renderShell: "default",
|
|
427
403
|
parameters: editToolParameters,
|
|
@@ -564,15 +540,6 @@ export function registerEditTool(pi: ExtensionAPI, deps: SessionDeps): void {
|
|
|
564
540
|
} catch (indexErr) {
|
|
565
541
|
rollbackFailures.push(`${path.basename(mutated)} index: ${indexErr instanceof Error ? indexErr.message : String(indexErr)}`);
|
|
566
542
|
}
|
|
567
|
-
try {
|
|
568
|
-
const client = LspManager.getInstance().getReadyClientForFile(mutated, ctx.cwd);
|
|
569
|
-
if (client?.getState() === "ready") {
|
|
570
|
-
await client.changeDocument(mutated, backup);
|
|
571
|
-
await client.saveDocument(mutated, backup);
|
|
572
|
-
}
|
|
573
|
-
} catch (lspErr) {
|
|
574
|
-
rollbackFailures.push(`${path.basename(mutated)} LSP: ${lspErr instanceof Error ? lspErr.message : String(lspErr)}`);
|
|
575
|
-
}
|
|
576
543
|
}
|
|
577
544
|
if (rollbackFailures.length > 0) {
|
|
578
545
|
return {
|
|
@@ -582,7 +549,7 @@ export function registerEditTool(pi: ExtensionAPI, deps: SessionDeps): void {
|
|
|
582
549
|
};
|
|
583
550
|
}
|
|
584
551
|
return {
|
|
585
|
-
content: [{ type: "text", text: `[EDIT FAILED - ROLLED BACK] Multi-file edit aborted and all file/index/
|
|
552
|
+
content: [{ type: "text", text: `[EDIT FAILED - ROLLED BACK] Multi-file edit aborted and all file/index/guard state restored: ${failureReason}` }],
|
|
586
553
|
details: { rolledBack: true },
|
|
587
554
|
isError: true,
|
|
588
555
|
};
|