pi-agent-ide 0.7.2 → 0.7.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/CHANGELOG.md +29 -0
- package/README.md +3 -1
- package/dist/{chunk-KOVPDKSJ.js → chunk-2ZMMRINH.js} +1 -1
- package/dist/{chunk-EURHXLN3.js → chunk-BYZSBXUK.js} +1 -1
- package/dist/{chunk-XDG7EQRN.js → chunk-CEX73RI5.js} +1 -1
- package/dist/{chunk-YWQS33DF.js → chunk-DZWTKF2K.js} +1 -1
- package/dist/{chunk-F6IWZKOG.js → chunk-EOKEQ3YH.js} +1 -1
- package/dist/{chunk-SJ4UZ4GU.js → chunk-HZ5BNF3A.js} +1 -1
- package/dist/{chunk-TQGE7RH6.js → chunk-NHKPP2AI.js} +2 -2
- package/dist/{chunk-SNF7PDDX.js → chunk-TXOTNK6B.js} +1 -1
- package/dist/chunk-WLZIUMJL.js +1 -0
- package/dist/entry-11.js +1 -1
- package/dist/entry-17.js +1 -1
- package/dist/entry-25.js +1 -1
- package/dist/entry-31.js +12 -12
- package/dist/entry-33.js +3 -2
- package/dist/entry-37.js +6 -6
- package/dist/entry-51.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-58.js +1 -1
- package/dist/entry-60.js +1 -1
- package/dist/entry-62.js +1 -1
- package/dist/entry-65.js +1 -1
- package/dist/entry-68.js +1 -1
- package/dist/entry-70.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/entry-77.js +56 -51
- package/dist/entry-78.js +8 -7
- package/dist/entry-79.js +1 -1
- package/dist/pi-agent-ide.js +20 -20
- package/docs/agent-guides/editing.md +9 -2
- package/docs/configuration.md +21 -0
- package/docs/development.md +4 -0
- package/docs/directory-operations.md +45 -0
- package/docs/releases.md +8 -0
- package/docs/research/codemode-write-context.md +56 -0
- package/docs/user-hooks.md +26 -0
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-documentation/package.json +1 -1
- package/node_modules/pi-agent-filesystem/package.json +7 -7
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +13 -13
- package/node_modules/pi-agent-ide-ast/src/extension.ts +3 -1
- package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +7 -2
- package/node_modules/pi-agent-ide-changes/package.json +9 -8
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +16 -0
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/index.ts +1 -1
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +7 -7
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-search/package.json +5 -5
- package/node_modules/pi-agent-search-text/package.json +7 -7
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/package.json +9 -9
- package/node_modules/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/node_modules/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +1 -6
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +7 -1
- package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +55 -12
- package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +39 -9
- package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +7 -1
- package/node_modules/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +3 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +51 -1
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +53 -8
- package/node_modules/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +2 -2
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +6 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +2 -2
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/node_modules/pi-agent-text-editor-renderer/package.json +5 -5
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +33 -18
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-web/package.json +7 -7
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +36 -35
- package/src/api/hooks.ts +24 -0
- package/src/composite/nested-tool-rendering.ts +120 -71
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/package.json +7 -7
- package/src/extensions/pi-agent-search/package.json +5 -5
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -7
- package/src/extensions/pi-agent-text-editor/package.json +9 -9
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +33 -18
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/src/extensions/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +1 -6
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +7 -1
- package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +55 -12
- package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +39 -9
- package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +7 -1
- package/src/extensions/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +3 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +51 -1
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +53 -8
- package/src/extensions/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +2 -2
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +6 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +2 -2
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/src/plugins/pi-agent-ide-ast/package.json +13 -13
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +3 -1
- package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +7 -2
- package/src/plugins/pi-agent-ide-changes/package.json +9 -8
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +16 -0
- package/src/plugins/pi-agent-ide-debugger/index.ts +3 -1
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/index.ts +1 -1
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/dist/chunk-PKSQT6TC.js +0 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rm, unlink } from "node:fs/promises";
|
|
2
|
+
import { prepareFileTransfer, type TransferKind } from "./file-transfers.js";
|
|
3
|
+
import { prepareDeletion, type DeletePolicyContext } from "./delete-policy.js";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import fs from "fs-extra";
|
|
4
6
|
import { Type } from "typebox";
|
|
@@ -9,7 +11,7 @@ export const fileOperations = ["delete", "move", "copy"] as const;
|
|
|
9
11
|
export type FileOperation = (typeof fileOperations)[number];
|
|
10
12
|
const filePath = Type.String({
|
|
11
13
|
minLength: 1,
|
|
12
|
-
description: "Local
|
|
14
|
+
description: "Local file, directory, or symlink path, relative to cwd or absolute.",
|
|
13
15
|
});
|
|
14
16
|
export const deleteFileParameters = Type.Object(
|
|
15
17
|
{ path: filePath },
|
|
@@ -23,6 +25,10 @@ export const transferFileParameters = Type.Object(
|
|
|
23
25
|
{ additionalProperties: false },
|
|
24
26
|
);
|
|
25
27
|
|
|
28
|
+
/** Host dependencies for deletion; no tool argument can replace the filesystem primitive. */
|
|
29
|
+
export interface FileOperationContext extends DeletePolicyContext {
|
|
30
|
+
readonly removeDirectory?: (source: string) => Promise<void>;
|
|
31
|
+
}
|
|
26
32
|
/** Plain receipt; unknown means the filesystem call failed after execution began. */
|
|
27
33
|
export interface FileOperationResult {
|
|
28
34
|
readonly kind: "file-operation";
|
|
@@ -31,6 +37,7 @@ export interface FileOperationResult {
|
|
|
31
37
|
readonly effect: "applied" | "not-applied" | "unknown";
|
|
32
38
|
readonly path?: string;
|
|
33
39
|
readonly target?: string;
|
|
40
|
+
readonly sourceKind?: TransferKind;
|
|
34
41
|
readonly error?: { readonly code: string; readonly message: string };
|
|
35
42
|
}
|
|
36
43
|
|
|
@@ -55,16 +62,18 @@ export function formatFileOperation(value: FileOperationResult): string {
|
|
|
55
62
|
.filter((line) => line !== undefined)
|
|
56
63
|
.join("\n");
|
|
57
64
|
}
|
|
58
|
-
/** Uses
|
|
65
|
+
/** Uses filesystem primitives without decoding contents; Delete applies hooks and safety policy first. */
|
|
59
66
|
export async function executeFileOperation(
|
|
60
67
|
operation: FileOperation,
|
|
61
68
|
input: unknown,
|
|
62
69
|
cwd: string,
|
|
63
70
|
signal?: AbortSignal,
|
|
71
|
+
deletion: FileOperationContext = {},
|
|
64
72
|
): Promise<FileOperationResult> {
|
|
65
73
|
let started = false;
|
|
66
74
|
let source: string | undefined;
|
|
67
75
|
let target: string | undefined;
|
|
76
|
+
let sourceKind: TransferKind | undefined;
|
|
68
77
|
try {
|
|
69
78
|
const schema = operation === "delete" ? deleteFileParameters : transferFileParameters;
|
|
70
79
|
if (!Value.Check(schema, input))
|
|
@@ -75,29 +84,25 @@ export async function executeFileOperation(
|
|
|
75
84
|
source = path.resolve(cwd, args.path);
|
|
76
85
|
target = args.target === undefined ? undefined : path.resolve(cwd, args.target);
|
|
77
86
|
signal?.throwIfAborted();
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
if (
|
|
87
|
-
source === target ||
|
|
88
|
-
(sourceStat.ino === targetStat.ino && sourceStat.dev === targetStat.dev)
|
|
89
|
-
)
|
|
90
|
-
throw Object.assign(new Error("Source and target are the same file"), {
|
|
91
|
-
code: "SAME_FILE",
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
87
|
+
const deleteEvent =
|
|
88
|
+
operation === "delete" ? await prepareDeletion(source, cwd, deletion, signal) : undefined;
|
|
89
|
+
const transfer =
|
|
90
|
+
operation !== "delete" && target !== undefined
|
|
91
|
+
? await prepareFileTransfer(operation, source, target, cwd, deletion, signal)
|
|
92
|
+
: undefined;
|
|
93
|
+
sourceKind = transfer?.sourceKind;
|
|
95
94
|
signal?.throwIfAborted();
|
|
96
95
|
started = true;
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
96
|
+
if (deleteEvent !== undefined) {
|
|
97
|
+
if (deleteEvent.recursive) {
|
|
98
|
+
if (deletion.removeDirectory !== undefined)
|
|
99
|
+
await deletion.removeDirectory(deleteEvent.resolvedPath);
|
|
100
|
+
else await rm(deleteEvent.resolvedPath, { recursive: true, force: false });
|
|
101
|
+
} else await unlink(deleteEvent.resolvedPath);
|
|
102
|
+
} else if (transfer !== undefined) {
|
|
103
|
+
if (operation === "copy")
|
|
104
|
+
await fs.copy(transfer.source, transfer.target, { overwrite: true, dereference: false });
|
|
105
|
+
else await fs.move(transfer.source, transfer.target, { overwrite: true });
|
|
101
106
|
}
|
|
102
107
|
return {
|
|
103
108
|
kind: "file-operation",
|
|
@@ -106,6 +111,7 @@ export async function executeFileOperation(
|
|
|
106
111
|
effect: "applied",
|
|
107
112
|
path: source,
|
|
108
113
|
...(target === undefined ? {} : { target }),
|
|
114
|
+
...(sourceKind === undefined ? {} : { sourceKind }),
|
|
109
115
|
};
|
|
110
116
|
} catch (error) {
|
|
111
117
|
return {
|
|
@@ -125,19 +131,3 @@ export async function executeFileOperation(
|
|
|
125
131
|
};
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
|
-
|
|
129
|
-
async function regularFile(file: string) {
|
|
130
|
-
const stat = await lstat(file);
|
|
131
|
-
if (!stat.isFile() || stat.isSymbolicLink())
|
|
132
|
-
throw Object.assign(new Error("Source must be a regular file"), { code: "INVALID_FILE_TYPE" });
|
|
133
|
-
return stat;
|
|
134
|
-
}
|
|
135
|
-
async function optionalStat(file: string) {
|
|
136
|
-
try {
|
|
137
|
-
return await lstat(file);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if (error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT")
|
|
140
|
-
return undefined;
|
|
141
|
-
throw error;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -30,7 +30,7 @@ export async function attachFileMutationTargets(
|
|
|
30
30
|
const stat = await lstat(file);
|
|
31
31
|
states.push({ source: file, state: "present" });
|
|
32
32
|
if (!stat.isFile() || stat.isSymbolicLink())
|
|
33
|
-
throw new Error("
|
|
33
|
+
throw new Error("Target is not a regular text file; no text selection is available.");
|
|
34
34
|
const bytes = await readFile(file, { signal });
|
|
35
35
|
const content = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
36
36
|
if (content.includes("\0")) throw new Error("Binary file has no supported text target.");
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { Stats } from "node:fs";
|
|
2
|
+
import { lstat, readdir, readlink, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
assertUnprotectedTransferPath,
|
|
6
|
+
prepareDeletionGuard,
|
|
7
|
+
type DeletePolicyContext,
|
|
8
|
+
type DeletionGuard,
|
|
9
|
+
} from "./delete-policy.js";
|
|
10
|
+
|
|
11
|
+
/** Object kinds supported by byte-preserving whole-object transfers. */
|
|
12
|
+
export type TransferKind = "file" | "directory" | "symlink";
|
|
13
|
+
|
|
14
|
+
/** Preflight transfers and retain canonical paths; never dereference the final link. */
|
|
15
|
+
export async function prepareFileTransfer(
|
|
16
|
+
operation: "copy" | "move",
|
|
17
|
+
source: string,
|
|
18
|
+
target: string,
|
|
19
|
+
cwd: string,
|
|
20
|
+
context: DeletePolicyContext,
|
|
21
|
+
signal?: AbortSignal,
|
|
22
|
+
): Promise<{ source: string; target: string; sourceKind: TransferKind }> {
|
|
23
|
+
const before = await inspectTransfer(operation, source, target, cwd, signal);
|
|
24
|
+
const guards: DeletionGuard[] = [];
|
|
25
|
+
if (operation === "move" && before.sourceKind !== "file") {
|
|
26
|
+
guards.push(await prepareDeletionGuard(source, cwd, context, signal));
|
|
27
|
+
if (before.targetExists) guards.push(await prepareDeletionGuard(target, cwd, context, signal));
|
|
28
|
+
}
|
|
29
|
+
// A later dialog or hook can change an earlier approved object. Recheck all approvals together.
|
|
30
|
+
for (const guard of guards) await guard.verify();
|
|
31
|
+
const after = await inspectTransfer(operation, source, target, cwd, signal);
|
|
32
|
+
if (before.snapshot !== after.snapshot)
|
|
33
|
+
fail(
|
|
34
|
+
"TRANSFER_TARGET_CHANGED",
|
|
35
|
+
"Transfer paths or contents changed during preflight. Nothing was transferred; make a new request.",
|
|
36
|
+
);
|
|
37
|
+
signal?.throwIfAborted();
|
|
38
|
+
return { source: before.source, target: before.target, sourceKind: before.sourceKind };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function inspectTransfer(
|
|
42
|
+
operation: "copy" | "move",
|
|
43
|
+
source: string,
|
|
44
|
+
target: string,
|
|
45
|
+
cwd: string,
|
|
46
|
+
signal?: AbortSignal,
|
|
47
|
+
) {
|
|
48
|
+
signal?.throwIfAborted();
|
|
49
|
+
const resolvedSource = await resolveLeaf(source);
|
|
50
|
+
const resolvedTarget = await resolveLeaf(target);
|
|
51
|
+
const sourceStat = await lstat(resolvedSource);
|
|
52
|
+
const targetStat = await optionalStat(resolvedTarget);
|
|
53
|
+
const sourceKind = kind(sourceStat);
|
|
54
|
+
validatePair(resolvedSource, sourceStat, resolvedTarget, targetStat);
|
|
55
|
+
if (
|
|
56
|
+
sourceKind === "directory" &&
|
|
57
|
+
(contains(resolvedSource, resolvedTarget) || contains(resolvedTarget, resolvedSource))
|
|
58
|
+
)
|
|
59
|
+
fail("OVERLAPPING_PATHS", "Source and target directory trees overlap.");
|
|
60
|
+
if (operation === "move") await assertUnprotectedTransferPath(resolvedSource, cwd, signal);
|
|
61
|
+
if (operation === "move" || sourceKind !== "file")
|
|
62
|
+
await assertUnprotectedTransferPath(resolvedTarget, cwd, signal);
|
|
63
|
+
const entries: unknown[] = [resolvedSource, resolvedTarget];
|
|
64
|
+
await inspectTree(resolvedSource, resolvedTarget, operation === "copy", entries, signal);
|
|
65
|
+
// Move removes the destination as one object; it does not merge child paths.
|
|
66
|
+
if (operation === "move") entries.push(snapshot(targetStat));
|
|
67
|
+
return {
|
|
68
|
+
source: resolvedSource,
|
|
69
|
+
target: resolvedTarget,
|
|
70
|
+
sourceKind,
|
|
71
|
+
targetExists: targetStat !== undefined,
|
|
72
|
+
snapshot: JSON.stringify(entries),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function inspectTree(
|
|
77
|
+
source: string,
|
|
78
|
+
target: string,
|
|
79
|
+
merge: boolean,
|
|
80
|
+
entries: unknown[],
|
|
81
|
+
signal?: AbortSignal,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
signal?.throwIfAborted();
|
|
84
|
+
const sourceStat = await lstat(source);
|
|
85
|
+
const sourceKind = kind(sourceStat);
|
|
86
|
+
const targetStat = merge ? await optionalStat(target) : undefined;
|
|
87
|
+
if (merge) validatePair(source, sourceStat, target, targetStat);
|
|
88
|
+
entries.push(source, snapshot(sourceStat), snapshot(targetStat));
|
|
89
|
+
if (sourceKind === "symlink") entries.push(await readlink(source));
|
|
90
|
+
if (sourceKind !== "directory") return;
|
|
91
|
+
for (const name of (await readdir(source)).sort())
|
|
92
|
+
await inspectTree(path.join(source, name), path.join(target, name), merge, entries, signal);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function validatePair(
|
|
96
|
+
source: string,
|
|
97
|
+
sourceStat: Stats,
|
|
98
|
+
target: string,
|
|
99
|
+
targetStat: Stats | undefined,
|
|
100
|
+
) {
|
|
101
|
+
if (
|
|
102
|
+
source === target ||
|
|
103
|
+
(targetStat !== undefined &&
|
|
104
|
+
sourceStat.dev === targetStat.dev &&
|
|
105
|
+
sourceStat.ino === targetStat.ino)
|
|
106
|
+
)
|
|
107
|
+
fail("SAME_FILE", "Source and target are the same filesystem object.");
|
|
108
|
+
if (targetStat === undefined) return;
|
|
109
|
+
if (targetStat.isSymbolicLink())
|
|
110
|
+
fail("INVALID_FILE_TYPE", "Transfer destination must not be a symlink.");
|
|
111
|
+
if (kind(sourceStat) !== kind(targetStat))
|
|
112
|
+
fail("INVALID_FILE_TYPE", "Source and target must have the same object type.");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function kind(stat: Stats): TransferKind {
|
|
116
|
+
if (stat.isSymbolicLink()) return "symlink";
|
|
117
|
+
if (stat.isDirectory()) return "directory";
|
|
118
|
+
if (stat.isFile()) return "file";
|
|
119
|
+
fail("INVALID_FILE_TYPE", "Transfers support regular files, directories, and symlinks only.");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function snapshot(stat: Stats | undefined) {
|
|
123
|
+
return stat === undefined
|
|
124
|
+
? null
|
|
125
|
+
: [stat.dev, stat.ino, stat.mode, stat.size, stat.birthtimeMs, stat.ctimeMs, stat.mtimeMs];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function optionalStat(file: string) {
|
|
129
|
+
try {
|
|
130
|
+
return await lstat(file);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function resolveLeaf(file: string): Promise<string> {
|
|
138
|
+
return path.join(await resolveParent(path.dirname(file)), path.basename(file));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function resolveParent(directory: string): Promise<string> {
|
|
142
|
+
try {
|
|
143
|
+
return await realpath(directory);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
146
|
+
// A dangling parent link is not a missing directory that we may create.
|
|
147
|
+
if (await optionalStat(directory)) throw error;
|
|
148
|
+
return path.join(await resolveParent(path.dirname(directory)), path.basename(directory));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function contains(parent: string, child: string) {
|
|
153
|
+
const relative = path.relative(parent, child);
|
|
154
|
+
return (
|
|
155
|
+
relative === "" ||
|
|
156
|
+
// This compares containment, not a parent-relative filesystem argument.
|
|
157
|
+
// eslint-disable-next-line repo/no-parent-paths
|
|
158
|
+
(!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`))
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function fail(code: string, message: string): never {
|
|
163
|
+
throw Object.assign(new Error(message), { code });
|
|
164
|
+
}
|
package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts
CHANGED
|
@@ -220,14 +220,9 @@ export class FileMutationAgentResult {
|
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
// A
|
|
224
|
-
// already applied by the batch.
|
|
225
|
-
const isMutationRejected = fmr.errors.some((error) => error.code === "MUTATION_REJECTED");
|
|
226
|
-
|
|
223
|
+
// A failed call alone does not prove that files stayed unchanged.
|
|
227
224
|
if (fmr.fileChangedStatement !== undefined) {
|
|
228
225
|
lines.push("", fmr.fileChangedStatement);
|
|
229
|
-
} else if (!isMutationRejected) {
|
|
230
|
-
lines.push("", "No file was changed.");
|
|
231
226
|
}
|
|
232
227
|
|
|
233
228
|
// File-level errors (WriteResultDetails blocked/failed, etc.)
|
|
@@ -239,7 +239,13 @@ export interface FileMutationData {
|
|
|
239
239
|
staleDiagnostic?: StaleAnchorDiagnostic | undefined;
|
|
240
240
|
fileChangedStatement?: string | undefined;
|
|
241
241
|
/** Failed rollback resources; an empty list means all attempted writes were restored. */
|
|
242
|
-
rollback?:
|
|
242
|
+
rollback?:
|
|
243
|
+
| {
|
|
244
|
+
readonly failedSources: readonly string[];
|
|
245
|
+
/** Existing resources whose restoration write succeeded. */
|
|
246
|
+
readonly restoredSources: readonly string[];
|
|
247
|
+
}
|
|
248
|
+
| undefined;
|
|
243
249
|
|
|
244
250
|
// ── Transient engine fields ──
|
|
245
251
|
snapshot?: MutationSnapshot | undefined;
|
|
@@ -24,6 +24,7 @@ import { buildFailedTextMutationResult, mutationSources } from "./text-mutation.
|
|
|
24
24
|
import { isWholeFileInvocation } from "./file-operation-tools.js";
|
|
25
25
|
import { isResultInput } from "./result-transfer.js";
|
|
26
26
|
import { FileMutationAgentResult } from "./mutation-result/file-mutation-agent-result.js";
|
|
27
|
+
import { writeProblemNotices } from "./write-agent-result.js";
|
|
27
28
|
import {
|
|
28
29
|
ResourceScheduler,
|
|
29
30
|
resourceAccesses,
|
|
@@ -62,6 +63,7 @@ interface ScriptBatch {
|
|
|
62
63
|
readonly context: ExtensionContext;
|
|
63
64
|
readonly summaries: BatchSummary[];
|
|
64
65
|
readonly results: FileMutationResult[];
|
|
66
|
+
readonly completedWrites: Set<FileMutationResult>;
|
|
65
67
|
readonly presentations: NativeEditBatchEvent[];
|
|
66
68
|
readonly errors: string[];
|
|
67
69
|
readonly cancellation: AbortController;
|
|
@@ -75,6 +77,9 @@ interface ScriptBatch {
|
|
|
75
77
|
closed: boolean;
|
|
76
78
|
}
|
|
77
79
|
|
|
80
|
+
function completedWriteNotices(result: FileMutationResult): string[] {
|
|
81
|
+
return writeProblemNotices([result]).map((notice) => `${result.path ?? "Write"}: ${notice}`);
|
|
82
|
+
}
|
|
78
83
|
function replaceScriptResult(
|
|
79
84
|
script: ScriptBatch,
|
|
80
85
|
previous: FileMutationResult,
|
|
@@ -82,6 +87,7 @@ function replaceScriptResult(
|
|
|
82
87
|
): void {
|
|
83
88
|
const index = script.results.indexOf(previous);
|
|
84
89
|
if (index >= 0) script.results[index] = updated;
|
|
90
|
+
if (script.completedWrites.delete(previous)) script.completedWrites.add(updated);
|
|
85
91
|
for (const [slot, presentation] of script.presentations.entries()) {
|
|
86
92
|
const results = presentation.result.details.results;
|
|
87
93
|
if (!results?.includes(previous)) continue;
|
|
@@ -198,6 +204,7 @@ class NativeTextEditBatchCoordinator {
|
|
|
198
204
|
reports: [],
|
|
199
205
|
presentations: [],
|
|
200
206
|
results: [],
|
|
207
|
+
completedWrites: new Set(),
|
|
201
208
|
errors: [],
|
|
202
209
|
cancellation: new AbortController(),
|
|
203
210
|
targets: new Map(),
|
|
@@ -452,6 +459,11 @@ class NativeTextEditBatchCoordinator {
|
|
|
452
459
|
} satisfies NativeEditBatchEvent);
|
|
453
460
|
}
|
|
454
461
|
this.scripts.delete(event.toolCallId);
|
|
462
|
+
// Write already returned its compact receipt to the script. Keep full user panels,
|
|
463
|
+
// but do not append their file text outside explicit script output.
|
|
464
|
+
const parentResults = script.results.filter(
|
|
465
|
+
(result) => !result.ok || !script.completedWrites.has(result),
|
|
466
|
+
);
|
|
455
467
|
if (
|
|
456
468
|
script.results.length === 0 &&
|
|
457
469
|
script.summaries.length === 0 &&
|
|
@@ -464,12 +476,17 @@ class NativeTextEditBatchCoordinator {
|
|
|
464
476
|
? { ...block, text: block.text.replace(/^Script completed\n/u, "Script failed\n") }
|
|
465
477
|
: block,
|
|
466
478
|
);
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
479
|
+
if (script.summaries.length > 0 || failed)
|
|
480
|
+
content.push({
|
|
481
|
+
type: "text",
|
|
482
|
+
text: `Editor batches: ${script.summaries.filter((batch) => batch.applied).length} committed.${failed ? "\n" + script.errors.join("\n") : ""}`,
|
|
483
|
+
});
|
|
484
|
+
const writeNotices = script.results
|
|
485
|
+
.filter((result) => result.ok && script.completedWrites.has(result))
|
|
486
|
+
.flatMap(completedWriteNotices);
|
|
487
|
+
if (writeNotices.length > 0) content.push({ type: "text", text: writeNotices.join("\n") });
|
|
488
|
+
if (parentResults.length > 0)
|
|
489
|
+
content.push(new FileMutationAgentResult(parentResults).toTextContent());
|
|
473
490
|
const details =
|
|
474
491
|
typeof event.details === "object" && event.details !== null ? event.details : {};
|
|
475
492
|
return {
|
|
@@ -505,7 +522,11 @@ class NativeTextEditBatchCoordinator {
|
|
|
505
522
|
}
|
|
506
523
|
|
|
507
524
|
/** Include applied, non-batched mutations in the script's final post-edit presentation. */
|
|
508
|
-
recordMutation(
|
|
525
|
+
recordMutation(
|
|
526
|
+
id: string,
|
|
527
|
+
toolName: string,
|
|
528
|
+
result: AgentToolResult<FileMutationBatchResult>,
|
|
529
|
+
): void {
|
|
509
530
|
const script = this.scopedInvocations.get(id);
|
|
510
531
|
if (script && !script.closed) script.unfinishedCalls.delete(id);
|
|
511
532
|
if (
|
|
@@ -516,6 +537,8 @@ class NativeTextEditBatchCoordinator {
|
|
|
516
537
|
)
|
|
517
538
|
return;
|
|
518
539
|
script.results.push(...result.details.results);
|
|
540
|
+
if (toolName === "write")
|
|
541
|
+
for (const item of result.details.results) script.completedWrites.add(item);
|
|
519
542
|
script.presentations.push({ parentToolCallId: script.id, calls: [id], result });
|
|
520
543
|
}
|
|
521
544
|
execute(
|
|
@@ -740,7 +763,21 @@ class NativeTextEditBatchCoordinator {
|
|
|
740
763
|
}),
|
|
741
764
|
);
|
|
742
765
|
let observedSources: string[] = [];
|
|
743
|
-
const record = (
|
|
766
|
+
const record = (
|
|
767
|
+
outcome: StructuredResult<MutationData>,
|
|
768
|
+
results: readonly FileMutationResult[] = [],
|
|
769
|
+
): boolean => {
|
|
770
|
+
// Only explicit rollback evidence overrides the journal's call-wide effect.
|
|
771
|
+
const reportedFiles = new Map<string, MutationData["files"][number]>(
|
|
772
|
+
results.flatMap(({ data }) => [
|
|
773
|
+
...(data.rollback?.restoredSources ?? []).map(
|
|
774
|
+
(source) => [source, { source, effect: "not-applied" as const }] as const,
|
|
775
|
+
),
|
|
776
|
+
...(data.rollback?.failedSources ?? []).map(
|
|
777
|
+
(source) => [source, { source, effect: "unknown" as const }] as const,
|
|
778
|
+
),
|
|
779
|
+
]),
|
|
780
|
+
);
|
|
744
781
|
const calls = journal.snapshot();
|
|
745
782
|
const operations = calls.map((call) => ({
|
|
746
783
|
id: call.callId,
|
|
@@ -773,7 +810,9 @@ class NativeTextEditBatchCoordinator {
|
|
|
773
810
|
}),
|
|
774
811
|
...observedSources.map((source) => ({ source, effect: "applied" as const })),
|
|
775
812
|
]).map((file) =>
|
|
776
|
-
observedSources.includes(file.source)
|
|
813
|
+
observedSources.includes(file.source)
|
|
814
|
+
? { ...file, effect: "applied" as const }
|
|
815
|
+
: (reportedFiles.get(file.source) ?? file),
|
|
777
816
|
);
|
|
778
817
|
const errors = [
|
|
779
818
|
...new Map(
|
|
@@ -860,7 +899,10 @@ class NativeTextEditBatchCoordinator {
|
|
|
860
899
|
} satisfies NativeEditBatchEvent;
|
|
861
900
|
script.presentations.push(presentation);
|
|
862
901
|
this.pi.events.emit(NATIVE_EDIT_BATCH_EVENT, presentation);
|
|
863
|
-
return record(
|
|
902
|
+
return record(
|
|
903
|
+
mutationOutcome(value, "batch", captured.completions),
|
|
904
|
+
value.details.results ?? [],
|
|
905
|
+
);
|
|
864
906
|
} catch (error) {
|
|
865
907
|
journal.markRunningUnknown(error);
|
|
866
908
|
await this.settleTargets(script, batch, journal, [], signal);
|
|
@@ -882,13 +924,14 @@ export function runNativePostEditScope<T>(
|
|
|
882
924
|
return coordinator ? coordinator.runPostEdits(id, work, immediate) : work();
|
|
883
925
|
}
|
|
884
926
|
|
|
885
|
-
/** Retain immediate child results
|
|
927
|
+
/** Retain immediate child results for final user panels and deferred processing failures. */
|
|
886
928
|
export function recordNativeTextMutation(
|
|
887
929
|
core: TextEditorCore,
|
|
888
930
|
id: string,
|
|
931
|
+
toolName: string,
|
|
889
932
|
result: AgentToolResult<FileMutationBatchResult>,
|
|
890
933
|
): void {
|
|
891
|
-
coordinators.get(core)?.recordMutation(id, result);
|
|
934
|
+
coordinators.get(core)?.recordMutation(id, toolName, result);
|
|
892
935
|
}
|
|
893
936
|
/** Attach accumulating editor batches to native Codemode's public parent/child lifecycle. */
|
|
894
937
|
export function registerNativeTextEditBatching(pi: ExtensionAPI, core: TextEditorCore): void {
|
|
@@ -4,7 +4,7 @@ import type {
|
|
|
4
4
|
TextMutationContext,
|
|
5
5
|
TextMutationEdit,
|
|
6
6
|
} from "#src/api/mutation-tool.js";
|
|
7
|
-
import type
|
|
7
|
+
import { TextChangeDocument, type TextChange } from "./text-change-engine.js";
|
|
8
8
|
import {
|
|
9
9
|
anchorSpanRange,
|
|
10
10
|
insertionAfterAnchor,
|
|
@@ -26,6 +26,8 @@ interface TransferSpan {
|
|
|
26
26
|
interface PreparedTransfer {
|
|
27
27
|
readonly input: Readonly<Record<string, unknown>>;
|
|
28
28
|
readonly empty: boolean;
|
|
29
|
+
/** Verified unchanged destination points; no editor operation is needed. */
|
|
30
|
+
readonly noOpTarget?: string;
|
|
29
31
|
readonly verifyFileSource?: () => Promise<void>;
|
|
30
32
|
readonly mutate?: (context: TextMutationContext) => Promise<TextMutation>;
|
|
31
33
|
}
|
|
@@ -74,6 +76,28 @@ export async function prepareResultTransfer(
|
|
|
74
76
|
"Transfer source and destination selection counts must match; no broadcast or concatenation.",
|
|
75
77
|
);
|
|
76
78
|
const empty = source?.targets.length === 0 || destination?.targets.length === 0;
|
|
79
|
+
if (operation === "move" && !empty && source !== undefined && destination !== undefined) {
|
|
80
|
+
const spans = (selected: ResolvedResultTargets): TransferSpan[] =>
|
|
81
|
+
selected.targets.map((target) => {
|
|
82
|
+
const range = target.ranges[0];
|
|
83
|
+
if (range === undefined) throw new Error("Transfer selection has no range.");
|
|
84
|
+
return {
|
|
85
|
+
source: target.source,
|
|
86
|
+
...new TextChangeDocument(target.expectedContent).range(
|
|
87
|
+
range.start.lineNumber,
|
|
88
|
+
range.start.column,
|
|
89
|
+
range.end.lineNumber,
|
|
90
|
+
range.end.column,
|
|
91
|
+
),
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
const sources = spans(source);
|
|
95
|
+
const destinations = spans(destination);
|
|
96
|
+
if ([...sources, ...destinations].every((span) => span.from === span.to)) {
|
|
97
|
+
assertMoveTargets(sources, destinations);
|
|
98
|
+
return { input, empty: false, noOpTarget: store.register(destination.targets, cwd) };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
77
101
|
return {
|
|
78
102
|
input,
|
|
79
103
|
empty,
|
|
@@ -157,14 +181,10 @@ export async function prepareResultTransfer(
|
|
|
157
181
|
}
|
|
158
182
|
}
|
|
159
183
|
if (operation === "move") {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
sourceSpan.from <= transfer.target.to &&
|
|
165
|
-
transfer.target.from <= sourceSpan.to
|
|
166
|
-
)
|
|
167
|
-
throw new Error("Move target must not overlap or touch any source range.");
|
|
184
|
+
assertMoveTargets(
|
|
185
|
+
sources,
|
|
186
|
+
planned.map((transfer) => transfer.target),
|
|
187
|
+
);
|
|
168
188
|
for (const span of sources)
|
|
169
189
|
append(span, { from: span.from, to: span.to, insert: "" }, false);
|
|
170
190
|
}
|
|
@@ -178,3 +198,13 @@ export async function prepareResultTransfer(
|
|
|
178
198
|
},
|
|
179
199
|
};
|
|
180
200
|
}
|
|
201
|
+
|
|
202
|
+
function assertMoveTargets(
|
|
203
|
+
sources: readonly TransferSpan[],
|
|
204
|
+
destinations: readonly TransferSpan[],
|
|
205
|
+
): void {
|
|
206
|
+
for (const source of sources)
|
|
207
|
+
for (const target of destinations)
|
|
208
|
+
if (source.source === target.source && source.from <= target.to && target.from <= source.to)
|
|
209
|
+
throw new Error("Move target must not overlap or touch any source range.");
|
|
210
|
+
}
|
|
@@ -110,6 +110,8 @@ export function mutationOutcome(
|
|
|
110
110
|
...new Set([
|
|
111
111
|
...(data.files ?? []).map((file) => file.path),
|
|
112
112
|
...(data.path ? [data.path] : []),
|
|
113
|
+
...(data.rollback?.failedSources ?? []),
|
|
114
|
+
...(data.rollback?.restoredSources ?? []),
|
|
113
115
|
]),
|
|
114
116
|
];
|
|
115
117
|
for (const source of sources)
|
|
@@ -120,7 +122,11 @@ export function mutationOutcome(
|
|
|
120
122
|
observed.has(source) ||
|
|
121
123
|
(data.ok === true && data.files?.some((file) => file.path === source))
|
|
122
124
|
? "applied"
|
|
123
|
-
: (
|
|
125
|
+
: data.rollback?.failedSources.includes(source)
|
|
126
|
+
? "unknown"
|
|
127
|
+
: data.rollback?.restoredSources.includes(source)
|
|
128
|
+
? "not-applied"
|
|
129
|
+
: (details.effect ?? "unknown"),
|
|
124
130
|
});
|
|
125
131
|
for (const error of data.errors ?? [])
|
|
126
132
|
errors.push({
|