@criogaid/pi-hashline-edit 0.9.0 → 0.11.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/README.md +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
package/src/pi/file-commit.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
rm,
|
|
17
17
|
stat,
|
|
18
18
|
link,
|
|
19
|
+
type FileHandle,
|
|
19
20
|
} from "node:fs/promises";
|
|
20
21
|
import type { BigIntStats, Stats } from "node:fs";
|
|
21
22
|
import { dirname, join } from "node:path";
|
|
@@ -24,20 +25,20 @@ import { decodeEditableText, unwritableTextError } from "../core/text.ts";
|
|
|
24
25
|
import {
|
|
25
26
|
errnoCode,
|
|
26
27
|
filesystemErrorCode,
|
|
28
|
+
PATH_ERROR_MESSAGES,
|
|
29
|
+
pathError,
|
|
27
30
|
HashlineError,
|
|
28
|
-
type ErrorCode,
|
|
29
|
-
type HashlineErrorArguments,
|
|
30
31
|
} from "../core/errors.ts";
|
|
31
|
-
import {
|
|
32
|
+
import { FileChangedDuringReadError, throwIfCancelled } from "./error-text.ts";
|
|
32
33
|
import { withFileRead } from "./file-read.ts";
|
|
33
|
-
import type { Freshness,
|
|
34
|
+
import type { Freshness, PublicationStatus } from "./report-schema.ts";
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* How a commit treats the target. `create` requires that it not exist. `overwrite` requires
|
|
37
38
|
* that it exist; with `baseRevision` (read-modify-write), the target must still hold the bytes
|
|
38
39
|
* that revision names, and that revision stands in for hashing the target again.
|
|
39
40
|
*/
|
|
40
|
-
|
|
41
|
+
type CommitOptions = { readonly signal?: AbortSignal } & (
|
|
41
42
|
| { readonly mode: "create" }
|
|
42
43
|
| { readonly mode: "overwrite"; readonly baseRevision?: string }
|
|
43
44
|
);
|
|
@@ -51,49 +52,75 @@ interface CommitRevisions {
|
|
|
51
52
|
|
|
52
53
|
export type CommitResult = CommitRevisions &
|
|
53
54
|
(
|
|
54
|
-
| { readonly
|
|
55
|
-
| {
|
|
56
|
-
readonly created: false;
|
|
57
|
-
readonly publication: "NOT_PUBLISHED";
|
|
58
|
-
readonly observedRevision: string;
|
|
59
|
-
}
|
|
55
|
+
| { readonly publication: "PUBLISHED" }
|
|
56
|
+
| { readonly publication: "NOT_PUBLISHED"; readonly observedRevision: string }
|
|
60
57
|
);
|
|
61
58
|
|
|
62
59
|
/**
|
|
63
|
-
* A mutation failure with
|
|
64
|
-
*
|
|
60
|
+
* A mutation failure with what it published. Every other failure of a mutation
|
|
61
|
+
* tool published nothing, so it stays a plain HashlineError.
|
|
65
62
|
*/
|
|
66
|
-
export class FileMutationError
|
|
67
|
-
readonly stage: MutationStage;
|
|
63
|
+
export class FileMutationError extends HashlineError<"PUBLISH_FAILED" | "POST_PROCESS_FAILED"> {
|
|
68
64
|
readonly publication: PublicationStatus;
|
|
69
65
|
|
|
70
66
|
constructor(
|
|
71
|
-
code:
|
|
72
|
-
stage: MutationStage,
|
|
67
|
+
code: "PUBLISH_FAILED" | "POST_PROCESS_FAILED",
|
|
73
68
|
publication: PublicationStatus,
|
|
74
69
|
message: string,
|
|
75
|
-
|
|
70
|
+
options?: { readonly cause?: unknown },
|
|
76
71
|
) {
|
|
77
|
-
super(code, message,
|
|
72
|
+
super(code, message, options);
|
|
78
73
|
this.name = "FileMutationError";
|
|
79
|
-
this.stage = stage;
|
|
80
74
|
this.publication = publication;
|
|
81
75
|
}
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
function requireRegularFile(target: Stats | BigIntStats): void {
|
|
85
|
-
if (!target.isFile())
|
|
79
|
+
if (!target.isFile())
|
|
80
|
+
throw new HashlineError("NOT_REGULAR_FILE", PATH_ERROR_MESSAGES.NOT_REGULAR_FILE);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const REGULAR_FILE_READ_CHUNK_BYTES = 64 * 1024;
|
|
84
|
+
|
|
85
|
+
/** Read a regular file in full, even when the filesystem returns short reads. */
|
|
86
|
+
async function readRegularFileBytes(
|
|
87
|
+
handle: FileHandle,
|
|
88
|
+
size: bigint,
|
|
89
|
+
signal: AbortSignal | undefined,
|
|
90
|
+
): Promise<Buffer> {
|
|
91
|
+
const chunks: Buffer[] = [];
|
|
92
|
+
let position = 0n;
|
|
93
|
+
while (position < size) {
|
|
94
|
+
throwIfCancelled(signal);
|
|
95
|
+
const remaining = size - position;
|
|
96
|
+
const length = Number(
|
|
97
|
+
remaining < BigInt(REGULAR_FILE_READ_CHUNK_BYTES)
|
|
98
|
+
? remaining
|
|
99
|
+
: BigInt(REGULAR_FILE_READ_CHUNK_BYTES),
|
|
100
|
+
);
|
|
101
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
102
|
+
const result = await handle.read({
|
|
103
|
+
buffer,
|
|
104
|
+
offset: 0,
|
|
105
|
+
length,
|
|
106
|
+
position: Number(position),
|
|
107
|
+
});
|
|
108
|
+
if (result.bytesRead <= 0 || result.bytesRead > length) throw new FileChangedDuringReadError();
|
|
109
|
+
chunks.push(buffer.subarray(0, result.bytesRead));
|
|
110
|
+
position += BigInt(result.bytesRead);
|
|
111
|
+
}
|
|
112
|
+
return Buffer.concat(chunks);
|
|
86
113
|
}
|
|
87
114
|
|
|
88
115
|
/** Check the opened object before reading; a FIFO must not block even if the path changed. */
|
|
89
116
|
async function readRegularFile(path: string, signal?: AbortSignal): Promise<Buffer> {
|
|
90
117
|
return withFileRead(path, signal, async (handle, before) => {
|
|
91
118
|
requireRegularFile(before);
|
|
92
|
-
return handle.
|
|
119
|
+
return readRegularFileBytes(handle, before.size, signal);
|
|
93
120
|
});
|
|
94
121
|
}
|
|
95
122
|
|
|
96
|
-
|
|
123
|
+
async function fileRevision(path: string): Promise<string> {
|
|
97
124
|
return byteRevision(await readRegularFile(path));
|
|
98
125
|
}
|
|
99
126
|
|
|
@@ -103,59 +130,40 @@ export async function readEditableSnapshot(path: string, signal?: AbortSignal) {
|
|
|
103
130
|
const bytes = await readRegularFile(path, signal);
|
|
104
131
|
return { text: decodeEditableText(bytes), baseRevision: byteRevision(bytes) };
|
|
105
132
|
} catch (error) {
|
|
106
|
-
|
|
133
|
+
throwIfCancelled(signal);
|
|
107
134
|
throw error;
|
|
108
135
|
}
|
|
109
136
|
}
|
|
110
137
|
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
code: C,
|
|
123
|
-
message: string,
|
|
124
|
-
...options: HashlineErrorArguments<NoInfer<C>>
|
|
125
|
-
): FileMutationError<C> {
|
|
126
|
-
return new FileMutationError(code, "prepare", "NOT_PUBLISHED", message, ...options);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** A classified failure before any publication step keeps its code, facts, cause, and recovery. */
|
|
130
|
-
function asPrepareError<C extends ErrorCode>(error: HashlineError<C>): FileMutationError<C> {
|
|
131
|
-
// The original constructor already enforced its code's facts; preserve that correlated tuple.
|
|
132
|
-
return prepareError(
|
|
133
|
-
error.errorCode,
|
|
134
|
-
error.message,
|
|
135
|
-
...([
|
|
136
|
-
{
|
|
137
|
-
cause: error.cause,
|
|
138
|
-
next: error.next,
|
|
139
|
-
facts: error.facts,
|
|
140
|
-
},
|
|
141
|
-
] as HashlineErrorArguments<C>),
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function throwIfCancelledBeforePublication(signal: AbortSignal | undefined): void {
|
|
146
|
-
if (signal?.aborted) throw asPrepareError(cancellationError());
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** A failed filesystem step keeps the classification of its cause. */
|
|
150
|
-
function causeCode(error: unknown, fallback: ErrorCode): ErrorCode {
|
|
151
|
-
return error instanceof HashlineError
|
|
152
|
-
? error.errorCode
|
|
153
|
-
: (filesystemErrorCode(error) ?? fallback);
|
|
138
|
+
/**
|
|
139
|
+
* A failure of a filesystem step before publication: the step is the message, the failure its
|
|
140
|
+
* cause, whose classification it keeps. A missing path or directory target keeps its own report,
|
|
141
|
+
* whichever step found it.
|
|
142
|
+
*/
|
|
143
|
+
function stepError(step: string, error: unknown): HashlineError {
|
|
144
|
+
const code =
|
|
145
|
+
error instanceof HashlineError
|
|
146
|
+
? error.errorCode
|
|
147
|
+
: (filesystemErrorCode(error) ?? "FILESYSTEM_ERROR");
|
|
148
|
+
return pathError(error) ?? new HashlineError(code, step, { cause: error });
|
|
154
149
|
}
|
|
155
150
|
|
|
156
|
-
/**
|
|
157
|
-
function
|
|
158
|
-
|
|
151
|
+
/** The target must still hold the bytes a mutation read; cancellation wins over what this read finds. */
|
|
152
|
+
async function requireBaseRevision(
|
|
153
|
+
path: string,
|
|
154
|
+
baseRevision: string,
|
|
155
|
+
signal: AbortSignal | undefined,
|
|
156
|
+
): Promise<void> {
|
|
157
|
+
let currentRevision: string;
|
|
158
|
+
try {
|
|
159
|
+
currentRevision = await fileRevision(path);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
throwIfCancelled(signal);
|
|
162
|
+
throw stepError("Unable to recheck target revision.", error);
|
|
163
|
+
}
|
|
164
|
+
throwIfCancelled(signal);
|
|
165
|
+
if (currentRevision !== baseRevision)
|
|
166
|
+
throw new HashlineError("FILE_CHANGED", "File changed after it was read.");
|
|
159
167
|
}
|
|
160
168
|
|
|
161
169
|
type TargetInfo = { readonly publishPath: string } & (
|
|
@@ -178,7 +186,7 @@ async function inspectTarget(path: string, knownRevision?: string): Promise<Targ
|
|
|
178
186
|
try {
|
|
179
187
|
publishPath = await realpath(path);
|
|
180
188
|
} catch (error) {
|
|
181
|
-
throw
|
|
189
|
+
throw new HashlineError("SYMLINK_UNRESOLVED", "Target symlink cannot be resolved.", {
|
|
182
190
|
cause: error,
|
|
183
191
|
});
|
|
184
192
|
}
|
|
@@ -192,7 +200,7 @@ async function inspectTarget(path: string, knownRevision?: string): Promise<Targ
|
|
|
192
200
|
}
|
|
193
201
|
requireRegularFile(target);
|
|
194
202
|
if (target.nlink > 1)
|
|
195
|
-
throw
|
|
203
|
+
throw new HashlineError(
|
|
196
204
|
"MULTIPLE_HARD_LINKS",
|
|
197
205
|
"Target has multiple hard links; publishing would split the link set.",
|
|
198
206
|
);
|
|
@@ -222,7 +230,8 @@ async function writeAndSyncTemp(
|
|
|
222
230
|
signal: AbortSignal | undefined,
|
|
223
231
|
): Promise<void> {
|
|
224
232
|
throwIfCancelled(signal);
|
|
225
|
-
|
|
233
|
+
// A new file takes the umask's default mode; its private temp directory hides it until publication.
|
|
234
|
+
const handle = await open(tempPath, "w");
|
|
226
235
|
try {
|
|
227
236
|
await handle.writeFile(content);
|
|
228
237
|
await handle.sync();
|
|
@@ -243,21 +252,11 @@ async function publishCreate(
|
|
|
243
252
|
await link(tempPath, targetPath);
|
|
244
253
|
} catch (error) {
|
|
245
254
|
if (errnoCode(error) === "EEXIST" || errnoCode(error) === "ENOTEMPTY") {
|
|
246
|
-
throw new
|
|
247
|
-
"TARGET_EXISTS",
|
|
248
|
-
"commit",
|
|
249
|
-
"NOT_PUBLISHED",
|
|
250
|
-
"Target appeared during create.",
|
|
251
|
-
{ cause: error },
|
|
252
|
-
);
|
|
255
|
+
throw new HashlineError("TARGET_EXISTS", "Target appeared during create.", { cause: error });
|
|
253
256
|
}
|
|
254
|
-
throw new FileMutationError(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
"UNKNOWN",
|
|
258
|
-
"Unable to publish new target.",
|
|
259
|
-
{ cause: error },
|
|
260
|
-
);
|
|
257
|
+
throw new FileMutationError("PUBLISH_FAILED", "UNKNOWN", "Unable to publish new target.", {
|
|
258
|
+
cause: error,
|
|
259
|
+
});
|
|
261
260
|
}
|
|
262
261
|
}
|
|
263
262
|
|
|
@@ -271,13 +270,9 @@ async function publishReplace(
|
|
|
271
270
|
try {
|
|
272
271
|
await rename(tempPath, targetPath);
|
|
273
272
|
} catch (error) {
|
|
274
|
-
throw new FileMutationError(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
"UNKNOWN",
|
|
278
|
-
"Unable to publish replacement.",
|
|
279
|
-
{ cause: error },
|
|
280
|
-
);
|
|
273
|
+
throw new FileMutationError("PUBLISH_FAILED", "UNKNOWN", "Unable to publish replacement.", {
|
|
274
|
+
cause: error,
|
|
275
|
+
});
|
|
281
276
|
}
|
|
282
277
|
}
|
|
283
278
|
|
|
@@ -294,6 +289,21 @@ async function syncDirectory(path: string): Promise<void> {
|
|
|
294
289
|
}
|
|
295
290
|
}
|
|
296
291
|
|
|
292
|
+
/**
|
|
293
|
+
* A temporary directory beside the target. Recursive mkdir fails with EPERM on a Windows
|
|
294
|
+
* drive root even though it exists, so it runs only when the directory is missing.
|
|
295
|
+
*/
|
|
296
|
+
async function makeTempDirectory(directory: string): Promise<string> {
|
|
297
|
+
const prefix = join(directory, ".hashline-commit-");
|
|
298
|
+
try {
|
|
299
|
+
return await mkdtemp(prefix);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (errnoCode(error) !== "ENOENT") throw error;
|
|
302
|
+
}
|
|
303
|
+
await mkdir(directory, { recursive: true });
|
|
304
|
+
return mkdtemp(prefix);
|
|
305
|
+
}
|
|
306
|
+
|
|
297
307
|
/** Validate the target, skip identical content, otherwise sync and publish. Callers own the file queue. */
|
|
298
308
|
export async function commitFile(
|
|
299
309
|
path: string,
|
|
@@ -303,35 +313,27 @@ export async function commitFile(
|
|
|
303
313
|
// Tools reject such input in prepareArguments, but a transformation can still produce it
|
|
304
314
|
// (a regex without `u` can split a surrogate pair), so publication keeps the final check.
|
|
305
315
|
const unwritable = unwritableTextError(content);
|
|
306
|
-
if (unwritable) throw
|
|
316
|
+
if (unwritable) throw unwritable;
|
|
307
317
|
const bytes = Buffer.from(content, "utf8");
|
|
308
318
|
const { mode } = options;
|
|
309
319
|
const baseRevision = mode === "overwrite" ? options.baseRevision : undefined;
|
|
310
320
|
const target = await inspectTarget(path, baseRevision);
|
|
311
321
|
if (mode === "create" && target.existed)
|
|
312
|
-
throw
|
|
322
|
+
throw new HashlineError("TARGET_EXISTS", "Target already exists.");
|
|
313
323
|
if (mode === "overwrite" && !target.existed)
|
|
314
|
-
throw
|
|
315
|
-
|
|
324
|
+
throw new HashlineError("PATH_NOT_FOUND", PATH_ERROR_MESSAGES.PATH_NOT_FOUND, {
|
|
325
|
+
facts: { missing: [path] },
|
|
326
|
+
// A read-modify-write target vanished after its read; those tools have no mode to change.
|
|
327
|
+
next: baseRevision === undefined ? "useCreate" : undefined,
|
|
328
|
+
});
|
|
329
|
+
throwIfCancelled(options.signal);
|
|
316
330
|
const publishedRevision = byteRevision(bytes);
|
|
317
331
|
// Mode, revision, target safety, and cancellation checks still apply to no-ops.
|
|
318
332
|
if (target.beforeRevision === publishedRevision) {
|
|
319
|
-
// A base revision was not hashed from the target here
|
|
320
|
-
if (baseRevision !== undefined)
|
|
321
|
-
|
|
322
|
-
try {
|
|
323
|
-
throwIfCancelledBeforePublication(options.signal);
|
|
324
|
-
currentRevision = await fileRevision(target.publishPath);
|
|
325
|
-
throwIfCancelledBeforePublication(options.signal);
|
|
326
|
-
} catch (error) {
|
|
327
|
-
if (options.signal?.aborted) throw asPrepareError(cancellationError());
|
|
328
|
-
throw stepError("Unable to read target revision.", error);
|
|
329
|
-
}
|
|
330
|
-
if (currentRevision !== baseRevision)
|
|
331
|
-
throw prepareError("FILE_CHANGED", "File changed after it was read.");
|
|
332
|
-
}
|
|
333
|
+
// A base revision was not hashed from the target here.
|
|
334
|
+
if (baseRevision !== undefined)
|
|
335
|
+
await requireBaseRevision(target.publishPath, baseRevision, options.signal);
|
|
333
336
|
return {
|
|
334
|
-
created: false,
|
|
335
337
|
publishedRevision,
|
|
336
338
|
observedRevision: publishedRevision,
|
|
337
339
|
publication: "NOT_PUBLISHED",
|
|
@@ -342,37 +344,23 @@ export async function commitFile(
|
|
|
342
344
|
const publishDirectory = dirname(publishPath);
|
|
343
345
|
let tempDir: string;
|
|
344
346
|
try {
|
|
345
|
-
await
|
|
346
|
-
tempDir = await mkdtemp(join(publishDirectory, ".hashline-commit-"));
|
|
347
|
+
tempDir = await makeTempDirectory(publishDirectory);
|
|
347
348
|
} catch (error) {
|
|
348
|
-
throw new
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
"NOT_PUBLISHED",
|
|
352
|
-
"Unable to prepare temporary publication area.",
|
|
353
|
-
{ cause: error },
|
|
354
|
-
);
|
|
349
|
+
throw new HashlineError("PUBLISH_FAILED", "Unable to prepare temporary publication area.", {
|
|
350
|
+
cause: error,
|
|
351
|
+
});
|
|
355
352
|
}
|
|
356
353
|
const tempPath = join(tempDir, "content");
|
|
357
354
|
let published = false;
|
|
358
355
|
try {
|
|
359
356
|
await writeAndSyncTemp(tempPath, bytes, target.modeBits, options.signal);
|
|
360
|
-
if (baseRevision !== undefined)
|
|
361
|
-
|
|
362
|
-
try {
|
|
363
|
-
currentRevision = await fileRevision(publishPath);
|
|
364
|
-
} catch (error) {
|
|
365
|
-
throw stepError("Unable to recheck target revision.", error);
|
|
366
|
-
}
|
|
367
|
-
if (currentRevision !== baseRevision)
|
|
368
|
-
throw prepareError("FILE_CHANGED", "File changed before publication.");
|
|
369
|
-
}
|
|
357
|
+
if (baseRevision !== undefined)
|
|
358
|
+
await requireBaseRevision(publishPath, baseRevision, options.signal);
|
|
370
359
|
if (mode === "create") await publishCreate(tempPath, publishPath, options.signal);
|
|
371
360
|
else await publishReplace(tempPath, publishPath, options.signal);
|
|
372
361
|
published = true;
|
|
373
362
|
await syncDirectory(publishDirectory);
|
|
374
363
|
const committed: CommitResult = {
|
|
375
|
-
created: !target.existed,
|
|
376
364
|
publishedRevision,
|
|
377
365
|
publication: "PUBLISHED",
|
|
378
366
|
};
|
|
@@ -384,32 +372,24 @@ export async function commitFile(
|
|
|
384
372
|
if (error instanceof FileChangedDuringReadError) return committed;
|
|
385
373
|
throw new FileMutationError(
|
|
386
374
|
"POST_PROCESS_FAILED",
|
|
387
|
-
"post_process",
|
|
388
375
|
"PUBLISHED",
|
|
389
376
|
"Final revision could not be read.",
|
|
390
377
|
{ cause: error },
|
|
391
378
|
);
|
|
392
379
|
}
|
|
393
380
|
} catch (error) {
|
|
394
|
-
|
|
381
|
+
// Already classified, including cancellation and every publication failure.
|
|
382
|
+
if (error instanceof HashlineError) throw error;
|
|
395
383
|
if (published)
|
|
396
384
|
throw new FileMutationError(
|
|
397
385
|
"POST_PROCESS_FAILED",
|
|
398
|
-
"post_process",
|
|
399
386
|
"PUBLISHED",
|
|
400
387
|
"Post-publication processing failed.",
|
|
401
388
|
{ cause: error },
|
|
402
389
|
);
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
throw new FileMutationError(
|
|
407
|
-
"PUBLISH_FAILED",
|
|
408
|
-
"commit",
|
|
409
|
-
"NOT_PUBLISHED",
|
|
410
|
-
"Unable to prepare or publish target.",
|
|
411
|
-
{ cause: error },
|
|
412
|
-
);
|
|
390
|
+
throw new HashlineError("PUBLISH_FAILED", "Unable to prepare or publish target.", {
|
|
391
|
+
cause: error,
|
|
392
|
+
});
|
|
413
393
|
} finally {
|
|
414
394
|
// Best effort: a leftover temporary directory changes neither the publication nor the recovery.
|
|
415
395
|
await rm(tempDir, { recursive: true, force: true }).catch(() => undefined);
|
|
@@ -3,7 +3,8 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { test } from "node:test";
|
|
6
|
-
import {
|
|
6
|
+
import { HashlineError } from "../core/errors.ts";
|
|
7
|
+
import { readEditableSnapshot } from "./file-commit.ts";
|
|
7
8
|
|
|
8
9
|
test("snapshot cancelled while opening → rejects as an unpublished cancellation", async (t) => {
|
|
9
10
|
const directory = await mkdtemp(join(tmpdir(), "hashline-snapshot-"));
|
|
@@ -15,10 +16,7 @@ test("snapshot cancelled while opening → rejects as an unpublished cancellatio
|
|
|
15
16
|
controller.abort();
|
|
16
17
|
await assert.rejects(
|
|
17
18
|
reading,
|
|
18
|
-
(error: unknown) =>
|
|
19
|
-
error instanceof FileMutationError &&
|
|
20
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
21
|
-
error.message.startsWith("Operation aborted"),
|
|
19
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
22
20
|
);
|
|
23
21
|
assert.equal(await readFile(path, "utf8"), "before\n");
|
|
24
22
|
});
|
|
@@ -5,12 +5,14 @@ import { tmpdir } from "node:os";
|
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { registerFusionCards } from "./fusion-card.ts";
|
|
8
|
-
import { createActionFusionExecutor
|
|
8
|
+
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
9
|
+
import type { ActionFusionProgress } from "./report-schema.ts";
|
|
9
10
|
import type { CommandResult } from "./report-schema.ts";
|
|
10
11
|
import { commandText } from "./mutation-result.ts";
|
|
11
|
-
import {
|
|
12
|
-
import { makeWriteOverride } from "./
|
|
13
|
-
import {
|
|
12
|
+
import { reportOf } from "./report.ts";
|
|
13
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
14
|
+
import { commandCompletion, createFusionContext } from "./action-fusion.testing.ts";
|
|
15
|
+
import { callTool } from "./tool-call.testing.ts";
|
|
14
16
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
15
17
|
import { ProcessTerminal, TuiMainScreen } from "@earendil-works/pi-tui";
|
|
16
18
|
import { theme as activeTheme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
|
|
@@ -46,6 +48,7 @@ function harness(entries: any[] = []) {
|
|
|
46
48
|
handlers.get(event)(
|
|
47
49
|
{},
|
|
48
50
|
{
|
|
51
|
+
cwd: process.cwd(),
|
|
49
52
|
mode: "tui",
|
|
50
53
|
ui: {
|
|
51
54
|
setWidget(_key: string, content: Parameters<ExtensionContext["ui"]["setWidget"]>[1]) {
|
|
@@ -102,12 +105,13 @@ test("one independent card updates live and only persists its endpoints", () =>
|
|
|
102
105
|
|
|
103
106
|
test("Fusion command outcomes → progress, cards, and reports preserve command results", async (t) => {
|
|
104
107
|
const success: CommandResult = {
|
|
108
|
+
details: {},
|
|
105
109
|
content: [
|
|
106
110
|
{ type: "text", text: "first" },
|
|
107
111
|
{ type: "text", text: "second" },
|
|
108
112
|
],
|
|
109
113
|
};
|
|
110
|
-
const failure: CommandResult = { content: [{ type: "text", text: "boom" }] };
|
|
114
|
+
const failure: CommandResult = { content: [{ type: "text", text: "boom" }], details: {} };
|
|
111
115
|
const cases: readonly {
|
|
112
116
|
scenario:
|
|
113
117
|
| "succeeded"
|
|
@@ -126,7 +130,7 @@ test("Fusion command outcomes → progress, cards, and reports preserve command
|
|
|
126
130
|
{
|
|
127
131
|
scenario: "cancelled-empty",
|
|
128
132
|
command: "cancelled",
|
|
129
|
-
result: { content: [] },
|
|
133
|
+
result: { content: [], details: {} },
|
|
130
134
|
text: /Not run: cancelled\./,
|
|
131
135
|
},
|
|
132
136
|
{ scenario: "skipped", command: "skipped", text: /Not run: the mutation did not complete/ },
|
|
@@ -137,24 +141,24 @@ test("Fusion command outcomes → progress, cards, and reports preserve command
|
|
|
137
141
|
const dir = await mkdtemp(join(tmpdir(), "hashline-fusion-card-"));
|
|
138
142
|
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
139
143
|
const h = harness();
|
|
144
|
+
h.restore("session_start", h.entries, { cwd: dir });
|
|
140
145
|
const events: ActionFusionProgress[] = [];
|
|
141
146
|
const controller = new AbortController();
|
|
142
147
|
if (scenario === "cancelled") controller.abort();
|
|
143
148
|
let commands = 0;
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
);
|
|
149
|
+
const context = await createFusionContext(dir, t, async () => {
|
|
150
|
+
commands++;
|
|
151
|
+
if (scenario === "cancelled-error" || scenario === "cancelled-empty") controller.abort();
|
|
152
|
+
if (scenario === "failed" || scenario === "cancelled-error")
|
|
153
|
+
return commandCompletion(failure, true);
|
|
154
|
+
return scenario === "cancelled-empty"
|
|
155
|
+
? commandCompletion({ content: [] }, true)
|
|
156
|
+
: commandCompletion(success);
|
|
157
|
+
});
|
|
158
|
+
const fusion = createActionFusionExecutor((progress) => {
|
|
159
|
+
events.push(progress);
|
|
160
|
+
h.report(progress);
|
|
161
|
+
});
|
|
158
162
|
const result = await callTool(
|
|
159
163
|
makeWriteOverride(dir, fusion),
|
|
160
164
|
{
|
|
@@ -163,7 +167,7 @@ test("Fusion command outcomes → progress, cards, and reports preserve command
|
|
|
163
167
|
content: "saved\n",
|
|
164
168
|
then_run: { command: "check" },
|
|
165
169
|
},
|
|
166
|
-
{ ctx:
|
|
170
|
+
{ ctx: context, signal: controller.signal },
|
|
167
171
|
);
|
|
168
172
|
assert.equal(commands, expected ? 1 : 0);
|
|
169
173
|
const final = events.at(-1);
|
|
@@ -181,7 +185,7 @@ test("Fusion command outcomes → progress, cards, and reports preserve command
|
|
|
181
185
|
assert.equal(Object.hasOwn(h.entries[0].data, "result"), false);
|
|
182
186
|
const block = result.content.at(-1);
|
|
183
187
|
assert.ok(block?.type === "text");
|
|
184
|
-
const report =
|
|
188
|
+
const report = reportOf({ content: [block] }, "write");
|
|
185
189
|
assert.ok(report);
|
|
186
190
|
const output = expected ? commandText(expected.content) : "";
|
|
187
191
|
assert.deepEqual(report.then_run, { status: command, ...(output ? { output } : {}) });
|
package/src/pi/fusion-card.ts
CHANGED
|
@@ -25,8 +25,7 @@ import {
|
|
|
25
25
|
} from "@earendil-works/pi-tui";
|
|
26
26
|
import type { TSchema } from "typebox";
|
|
27
27
|
import { Value } from "typebox/value";
|
|
28
|
-
import { ACTION_FUSION_PROGRESS_SCHEMA } from "./report-schema.ts";
|
|
29
|
-
import type { ActionFusionProgress } from "./action-fusion.ts";
|
|
28
|
+
import { ACTION_FUSION_PROGRESS_SCHEMA, type ActionFusionProgress } from "./report-schema.ts";
|
|
30
29
|
import { commandText } from "./mutation-result.ts";
|
|
31
30
|
import type { MutationRenderState } from "./render.ts";
|
|
32
31
|
|
|
@@ -37,6 +36,11 @@ const RESULT_TYPE = "hashline-then-run-result";
|
|
|
37
36
|
const COMMAND_CARD_SCHEMA = ACTION_FUSION_PROGRESS_SCHEMA;
|
|
38
37
|
type CommandCardData = ActionFusionProgress;
|
|
39
38
|
|
|
39
|
+
const commandPending = ({ command }: CommandCardData) =>
|
|
40
|
+
command === "waiting" || command === "running";
|
|
41
|
+
const commandFailed = ({ command }: CommandCardData) =>
|
|
42
|
+
command === "failed" || command === "timeout";
|
|
43
|
+
|
|
40
44
|
const TARGET_STATE = {
|
|
41
45
|
changed: "the target changed after the mutation",
|
|
42
46
|
missing: "the target is missing after the mutation",
|
|
@@ -95,7 +99,7 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
95
99
|
let tui: TUI | undefined;
|
|
96
100
|
|
|
97
101
|
const restore = (ctx: ExtensionContext) => {
|
|
98
|
-
|
|
102
|
+
currentCwd = ctx.cwd;
|
|
99
103
|
states.clear();
|
|
100
104
|
active.clear();
|
|
101
105
|
cards.clear();
|
|
@@ -160,14 +164,14 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
160
164
|
box = new Box(outputPadding, 1);
|
|
161
165
|
}
|
|
162
166
|
const value = current();
|
|
163
|
-
const pending = value
|
|
167
|
+
const pending = commandPending(value);
|
|
164
168
|
const interrupted = pending && !active.has(value.toolCallId);
|
|
165
169
|
const status = interrupted ? "interrupted (final status unknown)" : value.command;
|
|
166
170
|
const countdown =
|
|
167
171
|
value.command === "running" && !interrupted && value.timing
|
|
168
172
|
? ` · ${value.timing.remainingSeconds}s remaining`
|
|
169
173
|
: "";
|
|
170
|
-
const failed = value
|
|
174
|
+
const failed = commandFailed(value);
|
|
171
175
|
const color =
|
|
172
176
|
pending || value.command === "cancelled"
|
|
173
177
|
? "warning"
|
|
@@ -221,10 +225,9 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
221
225
|
native.updateResult(
|
|
222
226
|
{
|
|
223
227
|
...(value.result ?? { content: [] }),
|
|
224
|
-
isError: value
|
|
228
|
+
isError: commandFailed(value),
|
|
225
229
|
},
|
|
226
|
-
(value
|
|
227
|
-
active.has(value.toolCallId),
|
|
230
|
+
commandPending(value) && active.has(value.toolCallId),
|
|
228
231
|
);
|
|
229
232
|
},
|
|
230
233
|
"bash",
|
|
@@ -247,11 +250,10 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
247
250
|
return card;
|
|
248
251
|
});
|
|
249
252
|
|
|
250
|
-
return (progress: ActionFusionProgress
|
|
251
|
-
if (ctx?.cwd) currentCwd = ctx.cwd;
|
|
253
|
+
return (progress: ActionFusionProgress) => {
|
|
252
254
|
const first = !states.has(progress.toolCallId);
|
|
253
255
|
states.set(progress.toolCallId, progress);
|
|
254
|
-
const pending = progress
|
|
256
|
+
const pending = commandPending(progress);
|
|
255
257
|
if (pending) active.add(progress.toolCallId);
|
|
256
258
|
else active.delete(progress.toolCallId);
|
|
257
259
|
// Only endpoints are persisted; streaming snapshots reuse Bash's bounded output.
|
|
@@ -276,20 +278,10 @@ interface FusedMutationRenderState extends MutationRenderState {
|
|
|
276
278
|
};
|
|
277
279
|
}
|
|
278
280
|
|
|
279
|
-
type MutationCardTool<TParams extends TSchema, TDetails, TRenderDetails> = Omit<
|
|
280
|
-
ToolDefinition<TParams, TDetails, FusedMutationRenderState>,
|
|
281
|
-
"renderResult"
|
|
282
|
-
> &
|
|
283
|
-
Pick<ToolDefinition<TParams, TRenderDetails, FusedMutationRenderState>, "renderResult">;
|
|
284
|
-
|
|
285
281
|
/** Finish the mutation card independently while the fused command remains active. */
|
|
286
|
-
export function withMutationStatus<
|
|
287
|
-
TParams
|
|
288
|
-
|
|
289
|
-
TRenderDetails,
|
|
290
|
-
>(
|
|
291
|
-
tool: MutationCardTool<TParams, TDetails, TRenderDetails>,
|
|
292
|
-
): MutationCardTool<TParams, TDetails, TRenderDetails> {
|
|
282
|
+
export function withMutationStatus<TParams extends TSchema, TDetails>(
|
|
283
|
+
tool: ToolDefinition<TParams, TDetails, FusedMutationRenderState>,
|
|
284
|
+
): ToolDefinition<TParams, TDetails, FusedMutationRenderState> {
|
|
293
285
|
return {
|
|
294
286
|
...tool,
|
|
295
287
|
renderShell: "self",
|