dsh-better-edit 0.1.1 → 0.1.3
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/README.md +11 -4
- package/lib/edit-pipeline.js +1 -1
- package/lib/fs-bridge.js +10 -3
- package/lib/hash-store.js +52 -27
- package/lib/index.js +34 -34
- package/lib/paths.js +18 -12
- package/lib/sandbox.js +97 -0
- package/lib/tool-batch-edit.js +86 -82
- package/lib/tool-edit.js +101 -96
- package/lib/tool-read.js +30 -27
- package/lib/tool-undo.js +65 -54
- package/lib/types/fs-bridge.d.ts +7 -2
- package/lib/types/hash-store.d.ts +13 -1
- package/lib/types/index.d.ts +1 -1
- package/lib/types/paths.d.ts +12 -8
- package/lib/types/sandbox.d.ts +70 -0
- package/lib/types/tool-batch-edit.d.ts +3 -2
- package/lib/types/tool-edit.d.ts +3 -2
- package/lib/types/tool-undo.d.ts +3 -2
- package/lib/types/workspace.d.ts +24 -0
- package/lib/workspace.js +30 -0
- package/lib/write-hook.js +49 -44
- package/package.json +3 -2
package/lib/tool-batch-edit.js
CHANGED
|
@@ -22,6 +22,7 @@ import { clearNoopLoop, noopPayloadKey, trackNoopPayload, } from "./noop-guard.j
|
|
|
22
22
|
import { BATCH_EDIT_DESCRIPTION } from "./prompts.js";
|
|
23
23
|
import { pathSchema, removeFromSchema, removeToSchema, replacementTextSchema, } from "./schema.js";
|
|
24
24
|
import { execCwd, execSessionKey } from "./dsh-context.js";
|
|
25
|
+
import { withWorkspace } from "./workspace.js";
|
|
25
26
|
const BATCH_ITEM_KS = new Set([
|
|
26
27
|
"path",
|
|
27
28
|
"remove_from",
|
|
@@ -32,7 +33,7 @@ function assertBatchReq(request) {
|
|
|
32
33
|
if (!isRec(request)) {
|
|
33
34
|
throw new Error('[E_BAD_SHAPE] batch_edit request must be an object with an "edits" array.');
|
|
34
35
|
}
|
|
35
|
-
rejectUnknownFields(request, new Set(["edits"]), "batch_edit request");
|
|
36
|
+
rejectUnknownFields(request, new Set(["edits", "sandbox_permissions", "justification"]), "batch_edit request");
|
|
36
37
|
if (!Array.isArray(request.edits) || request.edits.length === 0) {
|
|
37
38
|
throw new Error('[E_BAD_SHAPE] batch_edit request requires a non-empty "edits" array.');
|
|
38
39
|
}
|
|
@@ -312,7 +313,7 @@ function toSection(file) {
|
|
|
312
313
|
* @param io - the filesystem bridge.
|
|
313
314
|
* @returns the exact disposer that unregisters the tool.
|
|
314
315
|
*/
|
|
315
|
-
export function buildBatchEditTool(io) {
|
|
316
|
+
export function buildBatchEditTool(io, sandbox) {
|
|
316
317
|
return defineTool({
|
|
317
318
|
name: "batch_edit",
|
|
318
319
|
description: BATCH_EDIT_DESCRIPTION,
|
|
@@ -341,99 +342,102 @@ export function buildBatchEditTool(io) {
|
|
|
341
342
|
render: (_args, value) => [{ type: "text", text: value }],
|
|
342
343
|
},
|
|
343
344
|
async execute(args, exec) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
canonical
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
assertBatchReq(canonical);
|
|
358
|
-
const items = await prepareItems(io, canonical, cwd, signal);
|
|
359
|
-
const groups = groupByPath(items);
|
|
360
|
-
const processed = [];
|
|
361
|
-
for (const groupItems of groups.values()) {
|
|
362
|
-
abortIf(signal);
|
|
363
|
-
processed.push(await processFile(io, groupItems, cwd, {
|
|
364
|
-
signal,
|
|
365
|
-
sessionKey,
|
|
366
|
-
}));
|
|
367
|
-
}
|
|
368
|
-
const undos = [];
|
|
369
|
-
for (const file of processed) {
|
|
370
|
-
if (file.appliedCount === 0)
|
|
371
|
-
continue;
|
|
372
|
-
const undo = await saveUndo(file.absolutePath, {
|
|
373
|
-
content: file.originalNormalized,
|
|
374
|
-
bom: file.bom,
|
|
375
|
-
originalEnding: file.originalEnding,
|
|
376
|
-
hashes: file.originalHashes,
|
|
377
|
-
resultContent: file.result,
|
|
378
|
-
});
|
|
379
|
-
if (!undo.persisted) {
|
|
380
|
-
for (const u of undos) {
|
|
381
|
-
try {
|
|
382
|
-
await u.restore();
|
|
383
|
-
}
|
|
384
|
-
catch (error) {
|
|
385
|
-
console.error("Failed to restore undo entry after batch abort:", error);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
throw new Error("[E_UNDO_UNAVAILABLE] Cannot persist undo history to the hash store; the batch was NOT applied and no file was written. Retry the batch, or use write if the store cannot be recovered.");
|
|
345
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
346
|
+
const cwd = execCwd(exec);
|
|
347
|
+
const sessionKey = execSessionKey(exec);
|
|
348
|
+
const signal = exec.signal;
|
|
349
|
+
const canonical = normReq(args);
|
|
350
|
+
if (isRec(canonical) && Array.isArray(canonical.edits)) {
|
|
351
|
+
canonical.edits = canonical.edits.map((item) => {
|
|
352
|
+
if (!isRec(item))
|
|
353
|
+
return item;
|
|
354
|
+
const cloned = { ...item };
|
|
355
|
+
normalizeFilePath(cloned);
|
|
356
|
+
return cloned;
|
|
357
|
+
});
|
|
389
358
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
359
|
+
assertBatchReq(canonical);
|
|
360
|
+
const sandboxPolicy = await sandbox.resolvePolicy("batch_edit", canonical, exec);
|
|
361
|
+
const items = await prepareItems(io, canonical, cwd, signal);
|
|
362
|
+
const groups = groupByPath(items);
|
|
363
|
+
const processed = [];
|
|
364
|
+
for (const groupItems of groups.values()) {
|
|
395
365
|
abortIf(signal);
|
|
396
|
-
|
|
397
|
-
|
|
366
|
+
processed.push(await processFile(io, groupItems, cwd, {
|
|
367
|
+
signal,
|
|
368
|
+
sessionKey,
|
|
369
|
+
}));
|
|
398
370
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
371
|
+
const undos = [];
|
|
372
|
+
for (const file of processed) {
|
|
373
|
+
if (file.appliedCount === 0)
|
|
374
|
+
continue;
|
|
375
|
+
const undo = await saveUndo(file.absolutePath, {
|
|
376
|
+
content: file.originalNormalized,
|
|
377
|
+
bom: file.bom,
|
|
378
|
+
originalEnding: file.originalEnding,
|
|
379
|
+
hashes: file.originalHashes,
|
|
380
|
+
resultContent: file.result,
|
|
381
|
+
});
|
|
382
|
+
if (!undo.persisted) {
|
|
383
|
+
for (const u of undos) {
|
|
384
|
+
try {
|
|
385
|
+
await u.restore();
|
|
386
|
+
}
|
|
387
|
+
catch (error) {
|
|
388
|
+
console.error("Failed to restore undo entry after batch abort:", error);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
throw new Error("[E_UNDO_UNAVAILABLE] Cannot persist undo history to the hash store; the batch was NOT applied and no file was written. Retry the batch, or use write if the store cannot be recovered.");
|
|
408
392
|
}
|
|
409
|
-
|
|
410
|
-
|
|
393
|
+
undos.push({ file, restore: undo.restore });
|
|
394
|
+
}
|
|
395
|
+
const written = [];
|
|
396
|
+
try {
|
|
397
|
+
for (const u of undos) {
|
|
398
|
+
abortIf(signal);
|
|
399
|
+
await io.writeText(u.file.absolutePath, u.file.bom + restoreEndings(u.file.result, u.file.originalEnding), signal, exec, sandboxPolicy);
|
|
400
|
+
written.push(u);
|
|
411
401
|
}
|
|
412
|
-
|
|
413
|
-
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
for (const w of written) {
|
|
405
|
+
try {
|
|
406
|
+
await io.writeText(w.file.absolutePath, w.file.bom +
|
|
407
|
+
restoreEndings(w.file.originalNormalized, w.file.originalEnding), undefined, exec, sandboxPolicy);
|
|
408
|
+
}
|
|
409
|
+
catch (restoreError) {
|
|
410
|
+
console.error("Failed to restore file after batch write failure:", restoreError);
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
await w.restore();
|
|
414
|
+
}
|
|
415
|
+
catch (restoreError) {
|
|
416
|
+
console.error("Failed to restore undo entry after batch write failure:", restoreError);
|
|
417
|
+
}
|
|
414
418
|
}
|
|
419
|
+
throw sandbox.mapError(error, sandboxPolicy);
|
|
415
420
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
await recordServedTruncated(sessionKey, file.absolutePath, entry.servedRows, splitLines(file.result).length, file.range.startLine - 1);
|
|
421
|
+
const result = buildBatchResult(processed.map(toSection));
|
|
422
|
+
if (result.details.servedRows && result.details.servedRows.length > 0) {
|
|
423
|
+
const byPath = result.details.servedByPath ?? [];
|
|
424
|
+
for (const entry of byPath) {
|
|
425
|
+
if (entry.servedRows.length === 0)
|
|
426
|
+
continue;
|
|
427
|
+
const file = processed.find((f) => f.displayPath === entry.path);
|
|
428
|
+
if (file) {
|
|
429
|
+
await recordServedTruncated(sessionKey, file.absolutePath, entry.servedRows, splitLines(file.result).length, file.range.startLine - 1);
|
|
430
|
+
}
|
|
427
431
|
}
|
|
428
432
|
}
|
|
429
|
-
|
|
430
|
-
|
|
433
|
+
return result.content[0].text;
|
|
434
|
+
});
|
|
431
435
|
},
|
|
432
436
|
});
|
|
433
437
|
}
|
|
434
438
|
/**
|
|
435
439
|
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
436
440
|
*/
|
|
437
|
-
export function registerBatchEditTool(_rootCtx, agentCtx, io) {
|
|
438
|
-
return agentCtx.tools.register(buildBatchEditTool(io));
|
|
441
|
+
export function registerBatchEditTool(_rootCtx, agentCtx, io, sandbox) {
|
|
442
|
+
return agentCtx.tools.register(buildBatchEditTool(io, sandbox));
|
|
439
443
|
}
|
package/lib/tool-edit.js
CHANGED
|
@@ -19,6 +19,7 @@ import { NOOP_LOOP_THRESHOLD } from "./constants.js";
|
|
|
19
19
|
import { EDIT_DESCRIPTION } from "./prompts.js";
|
|
20
20
|
import { pathSchema, removeFromSchema, removeToSchema, replacementTextSchema, } from "./schema.js";
|
|
21
21
|
import { execCwd, execSessionKey } from "./dsh-context.js";
|
|
22
|
+
import { withWorkspace } from "./workspace.js";
|
|
22
23
|
/**
|
|
23
24
|
* Register the hash-anchored `edit` tool on the calling agent's scope.
|
|
24
25
|
* @param _rootCtx - host context (logger, lifecycle).
|
|
@@ -27,7 +28,7 @@ import { execCwd, execSessionKey } from "./dsh-context.js";
|
|
|
27
28
|
* @param io - the filesystem bridge (ctx.fs backed in deployment).
|
|
28
29
|
* @returns the exact disposer that unregisters the tool.
|
|
29
30
|
*/
|
|
30
|
-
export function buildEditTool(io) {
|
|
31
|
+
export function buildEditTool(io, sandbox) {
|
|
31
32
|
return defineTool({
|
|
32
33
|
name: "edit",
|
|
33
34
|
description: EDIT_DESCRIPTION,
|
|
@@ -36,117 +37,121 @@ export function buildEditTool(io) {
|
|
|
36
37
|
remove_from: removeFromSchema,
|
|
37
38
|
remove_to: removeToSchema,
|
|
38
39
|
replacement_text: replacementTextSchema,
|
|
40
|
+
...(sandbox.escalationModes.length > 0 ? sandbox.schemaFields() : {}),
|
|
39
41
|
},
|
|
40
42
|
output: {
|
|
41
43
|
schema: { type: "string" },
|
|
42
44
|
render: (_args, value) => [{ type: "text", text: value }],
|
|
43
45
|
},
|
|
44
46
|
async execute(args, exec) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
canonical
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
signal
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
warnings
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (originalNormalized === result) {
|
|
69
|
-
const payload = noopPayloadKey(absolutePath, canonical.remove_from, canonical.remove_to, canonical.replacement_text);
|
|
70
|
-
const count = trackNoopPayload(absolutePath, payload);
|
|
71
|
-
if (count >= NOOP_LOOP_THRESHOLD) {
|
|
72
|
-
const echoRows = buildRangeEcho(range.startLine, range.endLine, originalHashes);
|
|
73
|
-
const echo = fmtServedRows(echoRows, splitLines(originalNormalized));
|
|
74
|
-
await recordEchoServes(sessionKey, absolutePath, echoRows, "live");
|
|
75
|
-
throw new Error(`[E_NOOP_LOOP] This exact edit (anchors ${canonical.remove_from} to ${canonical.remove_to} in ${path}) has been submitted ${count} times and produced no changes each time — the range already contains the replacement text. Do not resend this edit; it will never change the file. Current range:\n${echo}`);
|
|
47
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
48
|
+
const cwd = execCwd(exec);
|
|
49
|
+
const sessionKey = execSessionKey(exec);
|
|
50
|
+
const signal = exec.signal;
|
|
51
|
+
const canonical = normReq(args);
|
|
52
|
+
const resolution = isRec(canonical)
|
|
53
|
+
? await resolveMissingPath(canonical)
|
|
54
|
+
: undefined;
|
|
55
|
+
if (resolution && isRec(canonical)) {
|
|
56
|
+
canonical.path = resolution.path;
|
|
57
|
+
}
|
|
58
|
+
assertReq(canonical);
|
|
59
|
+
const sandboxPolicy = await sandbox.resolvePolicy("edit", canonical, exec);
|
|
60
|
+
const normalizedParams = canonical;
|
|
61
|
+
const path = normalizedParams.path;
|
|
62
|
+
abortIf(signal);
|
|
63
|
+
const pipeline = await execPipeline(io, normalizedParams, cwd, {
|
|
64
|
+
signal,
|
|
65
|
+
sessionKey,
|
|
66
|
+
});
|
|
67
|
+
const { originalNormalized, originalHashes, result, bom, originalEnding, hadUtf8DecodeErrors, warnings, noopEdit, firstChangedLine, lastChangedLine, resultHashes, totalAddedLines, totalRemovedLines, driftNotice, range, absolutePath, } = pipeline;
|
|
68
|
+
if (resolution) {
|
|
69
|
+
warnings.unshift(resolution.warning);
|
|
76
70
|
}
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
const editsAttempted = 1;
|
|
72
|
+
if (originalNormalized === result) {
|
|
73
|
+
const payload = noopPayloadKey(absolutePath, canonical.remove_from, canonical.remove_to, canonical.replacement_text);
|
|
74
|
+
const count = trackNoopPayload(absolutePath, payload);
|
|
75
|
+
if (count >= NOOP_LOOP_THRESHOLD) {
|
|
76
|
+
const echoRows = buildRangeEcho(range.startLine, range.endLine, originalHashes);
|
|
77
|
+
const echo = fmtServedRows(echoRows, splitLines(originalNormalized));
|
|
78
|
+
await recordEchoServes(sessionKey, absolutePath, echoRows, "live");
|
|
79
|
+
throw new Error(`[E_NOOP_LOOP] This exact edit (anchors ${canonical.remove_from} to ${canonical.remove_to} in ${path}) has been submitted ${count} times and produced no changes each time — the range already contains the replacement text. Do not resend this edit; it will never change the file. Current range:\n${echo}`);
|
|
80
|
+
}
|
|
81
|
+
if (count === 2) {
|
|
82
|
+
warnings.push(`[E_NOOP_LOOP] Notice: this exact edit (anchors ${canonical.remove_from} to ${canonical.remove_to} in ${path}) has produced no changes twice in a row. The range already contains the replacement text; resending it again will be rejected.`);
|
|
83
|
+
}
|
|
84
|
+
const noopSnapshotId = await snapshotIdFor(io, absolutePath, signal);
|
|
85
|
+
const noopResult = buildNoop({
|
|
86
|
+
path,
|
|
87
|
+
noopEdit,
|
|
88
|
+
snapshotId: noopSnapshotId,
|
|
89
|
+
editMeta: {
|
|
90
|
+
editsAttempted,
|
|
91
|
+
noopEditsCount: noopEdit ? 1 : 0,
|
|
92
|
+
addedLines: 0,
|
|
93
|
+
removedLines: 0,
|
|
94
|
+
},
|
|
95
|
+
warnings,
|
|
96
|
+
driftNotice,
|
|
97
|
+
});
|
|
98
|
+
return noopResult.content[0].text;
|
|
79
99
|
}
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
clearNoopLoop(absolutePath);
|
|
101
|
+
if (hadUtf8DecodeErrors) {
|
|
102
|
+
warnings.push("Non-UTF-8 bytes were shown as U+FFFD; this edit rewrote the file as UTF-8.");
|
|
103
|
+
}
|
|
104
|
+
abortIf(signal);
|
|
105
|
+
const undo = await saveUndo(absolutePath, {
|
|
106
|
+
content: originalNormalized,
|
|
107
|
+
bom,
|
|
108
|
+
originalEnding,
|
|
109
|
+
hashes: originalHashes,
|
|
110
|
+
resultContent: result,
|
|
111
|
+
});
|
|
112
|
+
if (!undo.persisted) {
|
|
113
|
+
throw new Error(`[E_UNDO_UNAVAILABLE] Cannot persist undo history to the hash store; the edit was NOT applied and ${path} is unchanged. Retry the edit, or use write if the store cannot be recovered.`);
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
abortIf(signal);
|
|
117
|
+
await io.writeText(absolutePath, bom + restoreEndings(result, originalEnding), signal, exec, sandboxPolicy);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
await undo.restore();
|
|
121
|
+
throw sandbox.mapError(error, sandboxPolicy);
|
|
122
|
+
}
|
|
123
|
+
const updatedSnapshotId = await snapshotIdFor(io, absolutePath, signal);
|
|
124
|
+
const editMeta = {
|
|
125
|
+
editsAttempted,
|
|
126
|
+
noopEditsCount: noopEdit ? 1 : 0,
|
|
127
|
+
firstChangedLine,
|
|
128
|
+
lastChangedLine,
|
|
129
|
+
addedLines: totalAddedLines,
|
|
130
|
+
removedLines: totalRemovedLines,
|
|
131
|
+
};
|
|
132
|
+
const successInput = {
|
|
82
133
|
path,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
noopEditsCount: noopEdit ? 1 : 0,
|
|
88
|
-
addedLines: 0,
|
|
89
|
-
removedLines: 0,
|
|
90
|
-
},
|
|
134
|
+
originalNormalized,
|
|
135
|
+
originalHashes,
|
|
136
|
+
result,
|
|
137
|
+
resultHashes,
|
|
91
138
|
warnings,
|
|
139
|
+
snapshotId: updatedSnapshotId,
|
|
140
|
+
editMeta,
|
|
92
141
|
driftNotice,
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
abortIf(signal);
|
|
101
|
-
const undo = await saveUndo(absolutePath, {
|
|
102
|
-
content: originalNormalized,
|
|
103
|
-
bom,
|
|
104
|
-
originalEnding,
|
|
105
|
-
hashes: originalHashes,
|
|
106
|
-
resultContent: result,
|
|
142
|
+
};
|
|
143
|
+
const changed = buildChanged(successInput);
|
|
144
|
+
if (changed.details.servedRows && changed.details.servedRows.length > 0) {
|
|
145
|
+
await recordServedTruncated(sessionKey, absolutePath, changed.details.servedRows, splitLines(result).length, range.startLine - 1);
|
|
146
|
+
}
|
|
147
|
+
return changed.content[0].text;
|
|
107
148
|
});
|
|
108
|
-
if (!undo.persisted) {
|
|
109
|
-
throw new Error(`[E_UNDO_UNAVAILABLE] Cannot persist undo history to the hash store; the edit was NOT applied and ${path} is unchanged. Retry the edit, or use write if the store cannot be recovered.`);
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
abortIf(signal);
|
|
113
|
-
await io.writeText(absolutePath, bom + restoreEndings(result, originalEnding), signal, exec);
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
await undo.restore();
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
const updatedSnapshotId = await snapshotIdFor(io, absolutePath, signal);
|
|
120
|
-
const editMeta = {
|
|
121
|
-
editsAttempted,
|
|
122
|
-
noopEditsCount: noopEdit ? 1 : 0,
|
|
123
|
-
firstChangedLine,
|
|
124
|
-
lastChangedLine,
|
|
125
|
-
addedLines: totalAddedLines,
|
|
126
|
-
removedLines: totalRemovedLines,
|
|
127
|
-
};
|
|
128
|
-
const successInput = {
|
|
129
|
-
path,
|
|
130
|
-
originalNormalized,
|
|
131
|
-
originalHashes,
|
|
132
|
-
result,
|
|
133
|
-
resultHashes,
|
|
134
|
-
warnings,
|
|
135
|
-
snapshotId: updatedSnapshotId,
|
|
136
|
-
editMeta,
|
|
137
|
-
driftNotice,
|
|
138
|
-
};
|
|
139
|
-
const changed = buildChanged(successInput);
|
|
140
|
-
if (changed.details.servedRows && changed.details.servedRows.length > 0) {
|
|
141
|
-
await recordServedTruncated(sessionKey, absolutePath, changed.details.servedRows, splitLines(result).length, range.startLine - 1);
|
|
142
|
-
}
|
|
143
|
-
return changed.content[0].text;
|
|
144
149
|
},
|
|
145
150
|
});
|
|
146
151
|
}
|
|
147
152
|
/**
|
|
148
153
|
* Register the hashline tool on the calling agent’s scope (own layer).
|
|
149
154
|
*/
|
|
150
|
-
export function registerEditTool(_rootCtx, agentCtx, io) {
|
|
151
|
-
return agentCtx.tools.register(buildEditTool(io));
|
|
155
|
+
export function registerEditTool(_rootCtx, agentCtx, io, sandbox) {
|
|
156
|
+
return agentCtx.tools.register(buildEditTool(io, sandbox));
|
|
152
157
|
}
|
package/lib/tool-read.js
CHANGED
|
@@ -14,6 +14,7 @@ import { recordServed, clearDriftReported } from "./served-store.js";
|
|
|
14
14
|
import { READ_DESCRIPTION } from "./prompts.js";
|
|
15
15
|
import { pathSchema } from "./schema.js";
|
|
16
16
|
import { execCwd, execSessionKey } from "./dsh-context.js";
|
|
17
|
+
import { withWorkspace } from "./workspace.js";
|
|
17
18
|
const ROOT_KS = new Set(["path", "offset", "limit"]);
|
|
18
19
|
function assertReadReq(request) {
|
|
19
20
|
if (!isRec(request)) {
|
|
@@ -51,34 +52,36 @@ export function buildReadTool(io) {
|
|
|
51
52
|
render: (_args, value) => [{ type: "text", text: value }],
|
|
52
53
|
},
|
|
53
54
|
async execute(args, exec) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
return withWorkspace(execCwd(exec), async () => {
|
|
56
|
+
const cwd = execCwd(exec);
|
|
57
|
+
const sessionKey = execSessionKey(exec);
|
|
58
|
+
const signal = exec.signal;
|
|
59
|
+
const canonical = normReq(args);
|
|
60
|
+
assertReadReq(canonical);
|
|
61
|
+
const rawPath = canonical.path;
|
|
62
|
+
abortIf(signal);
|
|
63
|
+
const absolutePath = await io.resolve(rawPath, cwd, signal);
|
|
64
|
+
const rawText = await io.readText(absolutePath, signal);
|
|
65
|
+
const { normalized, fileHashes, hadUtf8DecodeErrors } = await normFromText({
|
|
66
|
+
absolutePath,
|
|
67
|
+
rawText,
|
|
68
|
+
displayPath: rawPath,
|
|
69
|
+
signal,
|
|
70
|
+
maxLines: MAX_HASH_LINES,
|
|
71
|
+
});
|
|
72
|
+
const preview = await fmtReadPreview(normalized, { offset: canonical.offset, limit: canonical.limit }, fileHashes, absolutePath);
|
|
73
|
+
if (preview.served.length > 0) {
|
|
74
|
+
await recordServed(sessionKey, absolutePath, preview.served);
|
|
75
|
+
}
|
|
76
|
+
await clearDriftReported(sessionKey, absolutePath);
|
|
77
|
+
// Record the present observation with the fs policy gate so later
|
|
78
|
+
// built-in write/edit calls see this file as observed at the
|
|
79
|
+
// version the model just read (a no-op when no policy listens).
|
|
80
|
+
await io.emitObserved(absolutePath, exec, signal);
|
|
81
|
+
return hadUtf8DecodeErrors
|
|
82
|
+
? `${preview.text}\n\n[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]`
|
|
83
|
+
: preview.text;
|
|
69
84
|
});
|
|
70
|
-
const preview = await fmtReadPreview(normalized, { offset: canonical.offset, limit: canonical.limit }, fileHashes, absolutePath);
|
|
71
|
-
if (preview.served.length > 0) {
|
|
72
|
-
await recordServed(sessionKey, absolutePath, preview.served);
|
|
73
|
-
}
|
|
74
|
-
await clearDriftReported(sessionKey, absolutePath);
|
|
75
|
-
// Record the present observation with the fs policy gate so later
|
|
76
|
-
// built-in write/edit calls see this file as observed at the
|
|
77
|
-
// version the model just read (a no-op when no policy listens).
|
|
78
|
-
await io.emitObserved(absolutePath, exec, signal);
|
|
79
|
-
return hadUtf8DecodeErrors
|
|
80
|
-
? `${preview.text}\n\n[Non-UTF-8 bytes shown as U+FFFD; editing rewrites the file as UTF-8.]`
|
|
81
|
-
: preview.text;
|
|
82
85
|
},
|
|
83
86
|
});
|
|
84
87
|
}
|