@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -1,628 +0,0 @@
|
|
|
1
|
-
import fsPromises from "node:fs/promises";
|
|
2
|
-
import { syncBuiltinESMExports } from "node:module";
|
|
3
|
-
import {
|
|
4
|
-
chmod,
|
|
5
|
-
lstat,
|
|
6
|
-
mkdir,
|
|
7
|
-
mkdtemp,
|
|
8
|
-
open,
|
|
9
|
-
readFile,
|
|
10
|
-
readlink,
|
|
11
|
-
stat,
|
|
12
|
-
symlink,
|
|
13
|
-
link as createHardLink,
|
|
14
|
-
rm,
|
|
15
|
-
writeFile,
|
|
16
|
-
} from "node:fs/promises";
|
|
17
|
-
import { tmpdir } from "node:os";
|
|
18
|
-
import { join } from "node:path";
|
|
19
|
-
import assert from "node:assert/strict";
|
|
20
|
-
import { test } from "node:test";
|
|
21
|
-
import { spawn } from "node:child_process";
|
|
22
|
-
import {
|
|
23
|
-
commitFile,
|
|
24
|
-
readEditableSnapshot,
|
|
25
|
-
type CommitResult,
|
|
26
|
-
type FreshnessObservation,
|
|
27
|
-
} from "./file-commit.ts";
|
|
28
|
-
import { byteRevision } from "./file-revision.ts";
|
|
29
|
-
import { HashlineError, PATH_ERROR_MESSAGES } from "../core/errors.ts";
|
|
30
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
31
|
-
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
32
|
-
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
33
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
34
|
-
import { reportOf } from "./report.ts";
|
|
35
|
-
|
|
36
|
-
// Checked by tsc; this state cannot be returned by the commit layer.
|
|
37
|
-
function invalidCommitStates() {
|
|
38
|
-
// @ts-expect-error A no-op has a confirmed observed revision.
|
|
39
|
-
const unobservedNoop: CommitResult = {
|
|
40
|
-
publication: "NOT_PUBLISHED",
|
|
41
|
-
publishedRevision: "r",
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// @ts-expect-error A known freshness state cannot carry a read error.
|
|
45
|
-
const knownError: FreshnessObservation = {
|
|
46
|
-
freshness: "changed",
|
|
47
|
-
error: new Error("read failed"),
|
|
48
|
-
};
|
|
49
|
-
// @ts-expect-error Unknown freshness records the error that prevented observation.
|
|
50
|
-
const unexplainedUnknown: FreshnessObservation = { freshness: "unknown" };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
|
|
54
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-commit-"));
|
|
55
|
-
try {
|
|
56
|
-
return await run(dir);
|
|
57
|
-
} finally {
|
|
58
|
-
await rm(dir, { recursive: true, force: true });
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function isPermissionError(error: unknown): boolean {
|
|
63
|
-
return (
|
|
64
|
-
error instanceof Error && "code" in error && (error as NodeJS.ErrnoException).code === "EPERM"
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function holdWindowsExclusive(
|
|
69
|
-
path: string,
|
|
70
|
-
): Promise<{ release: () => void; done: Promise<void> }> {
|
|
71
|
-
const escaped = path.replaceAll("'", "''");
|
|
72
|
-
const child = spawn(
|
|
73
|
-
"powershell.exe",
|
|
74
|
-
[
|
|
75
|
-
"-NoProfile",
|
|
76
|
-
"-Command",
|
|
77
|
-
`$stream = [IO.File]::Open('${escaped}', [IO.FileMode]::Open, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None); Write-Output READY; [Console]::ReadLine(); $stream.Dispose()`,
|
|
78
|
-
],
|
|
79
|
-
{ stdio: ["pipe", "pipe", "pipe"] },
|
|
80
|
-
);
|
|
81
|
-
let output = "";
|
|
82
|
-
const ready = new Promise<void>((resolve, reject) => {
|
|
83
|
-
child.stdout.on("data", (chunk) => {
|
|
84
|
-
output += String(chunk);
|
|
85
|
-
if (output.includes("READY")) resolve();
|
|
86
|
-
});
|
|
87
|
-
child.once("error", reject);
|
|
88
|
-
});
|
|
89
|
-
const done = new Promise<void>((resolve, reject) => {
|
|
90
|
-
child.once("error", reject);
|
|
91
|
-
child.once("exit", (code) =>
|
|
92
|
-
code === 0 ? resolve() : reject(new Error(`exclusive holder exited with ${code}`)),
|
|
93
|
-
);
|
|
94
|
-
});
|
|
95
|
-
await ready;
|
|
96
|
-
return {
|
|
97
|
-
release: () => {
|
|
98
|
-
child.stdin.write("release\n");
|
|
99
|
-
child.stdin.end();
|
|
100
|
-
},
|
|
101
|
-
done,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
test("another create commits immediately before publication → preserves the winner's bytes", async (t) =>
|
|
106
|
-
withTemp(async (dir) => {
|
|
107
|
-
const target = join(dir, "race.txt");
|
|
108
|
-
const nativeLink = fsPromises.link;
|
|
109
|
-
t.mock.method(fsPromises, "link", async (...args: Parameters<typeof nativeLink>) => {
|
|
110
|
-
t.mock.restoreAll();
|
|
111
|
-
syncBuiltinESMExports();
|
|
112
|
-
const winner = await commitFile(target, "winner\n", { mode: "create" });
|
|
113
|
-
assert.equal(winner.publication, "PUBLISHED");
|
|
114
|
-
return nativeLink(...args);
|
|
115
|
-
});
|
|
116
|
-
syncBuiltinESMExports();
|
|
117
|
-
try {
|
|
118
|
-
await assert.rejects(
|
|
119
|
-
commitFile(target, "loser\n", { mode: "create" }),
|
|
120
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "TARGET_EXISTS",
|
|
121
|
-
);
|
|
122
|
-
assert.deepEqual(await readFile(target), Buffer.from("winner\n"));
|
|
123
|
-
} finally {
|
|
124
|
-
t.mock.restoreAll();
|
|
125
|
-
syncBuiltinESMExports();
|
|
126
|
-
}
|
|
127
|
-
}));
|
|
128
|
-
|
|
129
|
-
test("create never overwrites an existing target", async () =>
|
|
130
|
-
withTemp(async (dir) => {
|
|
131
|
-
const target = join(dir, "existing.txt");
|
|
132
|
-
await writeFile(target, "original\n");
|
|
133
|
-
const revision = byteRevision(await readFile(target));
|
|
134
|
-
await assert.rejects(
|
|
135
|
-
commitFile(target, "replacement\n", { mode: "create" }),
|
|
136
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "TARGET_EXISTS",
|
|
137
|
-
);
|
|
138
|
-
assert.equal(byteRevision(await readFile(target)), revision);
|
|
139
|
-
assert.equal(await readFile(target, "utf8"), "original\n");
|
|
140
|
-
}));
|
|
141
|
-
|
|
142
|
-
test("short low-level reads → complete mutation snapshots before revision binding", async (t) =>
|
|
143
|
-
withTemp(async (dir) => {
|
|
144
|
-
const target = join(dir, "short-read.txt");
|
|
145
|
-
const expected = Buffer.from("abcDEF");
|
|
146
|
-
await writeFile(target, expected);
|
|
147
|
-
const nativeOpen = fsPromises.open;
|
|
148
|
-
t.mock.method(fsPromises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
149
|
-
const handle = await nativeOpen(...args);
|
|
150
|
-
if (args[0] !== target) return handle;
|
|
151
|
-
const nativeRead = handle.read.bind(handle);
|
|
152
|
-
t.mock.method(
|
|
153
|
-
handle,
|
|
154
|
-
"read",
|
|
155
|
-
(options: { buffer: Buffer; length: number; position: number }) =>
|
|
156
|
-
nativeRead({ ...options, length: Math.min(options.length, 3) }),
|
|
157
|
-
);
|
|
158
|
-
return handle;
|
|
159
|
-
});
|
|
160
|
-
syncBuiltinESMExports();
|
|
161
|
-
try {
|
|
162
|
-
const snapshot = await readEditableSnapshot(target, undefined);
|
|
163
|
-
assert.equal(snapshot.text, expected.toString("utf8"));
|
|
164
|
-
assert.equal(snapshot.baseRevision, byteRevision(expected));
|
|
165
|
-
} finally {
|
|
166
|
-
t.mock.restoreAll();
|
|
167
|
-
syncBuiltinESMExports();
|
|
168
|
-
}
|
|
169
|
-
}));
|
|
170
|
-
|
|
171
|
-
test("replacement exposes complete old or new content during concurrent reads", async (t) =>
|
|
172
|
-
withTemp(async (dir) => {
|
|
173
|
-
if (process.platform === "win32")
|
|
174
|
-
return t.skip("Windows rename can reject a concurrently open reader; see platform matrix");
|
|
175
|
-
const target = join(dir, "atomic.txt");
|
|
176
|
-
const oldContent = "old-".repeat(200_000);
|
|
177
|
-
const newContent = "new-".repeat(200_000);
|
|
178
|
-
await writeFile(target, oldContent);
|
|
179
|
-
let done = false;
|
|
180
|
-
const commit = commitFile(target, newContent, { mode: "overwrite" }).finally(() => {
|
|
181
|
-
done = true;
|
|
182
|
-
});
|
|
183
|
-
while (!done) {
|
|
184
|
-
const current = await readFile(target, "utf8");
|
|
185
|
-
assert.ok(
|
|
186
|
-
current === oldContent || current === newContent,
|
|
187
|
-
"reader observed partial replacement content",
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
await commit;
|
|
191
|
-
assert.equal(await readFile(target, "utf8"), newContent);
|
|
192
|
-
}));
|
|
193
|
-
|
|
194
|
-
test("overwriting a symlink updates its resolved regular-file target and preserves the link", async (t) =>
|
|
195
|
-
withTemp(async (dir) => {
|
|
196
|
-
const target = join(dir, "real.txt");
|
|
197
|
-
const alias = join(dir, "alias.txt");
|
|
198
|
-
await writeFile(target, "old\n");
|
|
199
|
-
try {
|
|
200
|
-
await symlink("real.txt", alias);
|
|
201
|
-
} catch (error) {
|
|
202
|
-
if (isPermissionError(error)) return t.skip("symlink creation unavailable");
|
|
203
|
-
throw error;
|
|
204
|
-
}
|
|
205
|
-
await commitFile(alias, "new\n", { mode: "overwrite" });
|
|
206
|
-
assert.equal(await readFile(target, "utf8"), "new\n");
|
|
207
|
-
assert.equal((await lstat(alias)).isSymbolicLink(), true);
|
|
208
|
-
assert.equal(await readlink(alias), "real.txt");
|
|
209
|
-
}));
|
|
210
|
-
|
|
211
|
-
test("create rejects both valid and dangling symlinks without replacing the link", async (t) =>
|
|
212
|
-
withTemp(async (dir) => {
|
|
213
|
-
const real = join(dir, "real.txt");
|
|
214
|
-
const valid = join(dir, "valid.txt");
|
|
215
|
-
const dangling = join(dir, "dangling.txt");
|
|
216
|
-
await writeFile(real, "original\n");
|
|
217
|
-
try {
|
|
218
|
-
await symlink("real.txt", valid);
|
|
219
|
-
await symlink("missing.txt", dangling);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
if (isPermissionError(error)) return t.skip("symlink creation unavailable");
|
|
222
|
-
throw error;
|
|
223
|
-
}
|
|
224
|
-
await assert.rejects(commitFile(valid, "bad\n", { mode: "create" }), /already exists|symlink/);
|
|
225
|
-
await assert.rejects(commitFile(dangling, "bad\n", { mode: "create" }), /symlink/);
|
|
226
|
-
assert.equal((await lstat(valid)).isSymbolicLink(), true);
|
|
227
|
-
assert.equal((await lstat(dangling)).isSymbolicLink(), true);
|
|
228
|
-
assert.equal(await readFile(real, "utf8"), "original\n");
|
|
229
|
-
}));
|
|
230
|
-
|
|
231
|
-
test("replacement rejects a multi-hardlink target without changing either link", async (t) =>
|
|
232
|
-
withTemp(async (dir) => {
|
|
233
|
-
const target = join(dir, "target.txt");
|
|
234
|
-
const sibling = join(dir, "sibling.txt");
|
|
235
|
-
await writeFile(target, "original\n");
|
|
236
|
-
try {
|
|
237
|
-
await createHardLink(target, sibling);
|
|
238
|
-
} catch (error) {
|
|
239
|
-
if (isPermissionError(error)) return t.skip("hardlink creation unavailable");
|
|
240
|
-
throw error;
|
|
241
|
-
}
|
|
242
|
-
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }), /multiple hard links/);
|
|
243
|
-
assert.equal(await readFile(target, "utf8"), "original\n");
|
|
244
|
-
assert.equal(await readFile(sibling, "utf8"), "original\n");
|
|
245
|
-
assert.equal((await stat(target)).nlink, 2);
|
|
246
|
-
}));
|
|
247
|
-
|
|
248
|
-
test("files are replaced or created → preserve existing modes and apply umask to new files", async (t) =>
|
|
249
|
-
withTemp(async (dir) => {
|
|
250
|
-
if (process.platform === "win32")
|
|
251
|
-
return t.skip("Windows mode bits are not an ACL preservation test");
|
|
252
|
-
const existing = join(dir, "existing.sh");
|
|
253
|
-
await writeFile(existing, "old\n");
|
|
254
|
-
await chmod(existing, 0o750);
|
|
255
|
-
await commitFile(existing, "new\n", { mode: "overwrite" });
|
|
256
|
-
assert.equal((await stat(existing)).mode & 0o777, 0o750);
|
|
257
|
-
const created = join(dir, "created.txt");
|
|
258
|
-
await commitFile(created, "new\n", { mode: "create" });
|
|
259
|
-
assert.equal((await stat(created)).mode & 0o777, 0o666 & ~process.umask());
|
|
260
|
-
}));
|
|
261
|
-
|
|
262
|
-
test("directories are rejected instead of entering regular-file publication", async () =>
|
|
263
|
-
withTemp(async (dir) => {
|
|
264
|
-
const target = join(dir, "directory");
|
|
265
|
-
await mkdir(target);
|
|
266
|
-
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }), (error: unknown) => {
|
|
267
|
-
assert.ok(error instanceof HashlineError);
|
|
268
|
-
assert.equal(error.errorCode, "NOT_REGULAR_FILE");
|
|
269
|
-
assert.equal(error.message, PATH_ERROR_MESSAGES.NOT_REGULAR_FILE);
|
|
270
|
-
return true;
|
|
271
|
-
});
|
|
272
|
-
}));
|
|
273
|
-
|
|
274
|
-
test("Windows shared access failure preserves the target, skips then_run, and releases the queue", async (t) =>
|
|
275
|
-
withTemp(async (dir) => {
|
|
276
|
-
if (process.platform !== "win32") return t.skip("Windows shared-access behavior");
|
|
277
|
-
const target = join(dir, "shared.txt");
|
|
278
|
-
await writeFile(target, "original\n");
|
|
279
|
-
const holder = await holdWindowsExclusive(target);
|
|
280
|
-
let commandRuns = 0;
|
|
281
|
-
const context = {
|
|
282
|
-
cwd: dir,
|
|
283
|
-
executeTool: async () => {
|
|
284
|
-
commandRuns++;
|
|
285
|
-
return commandCompletion({ content: [{ type: "text", text: "ok" }] });
|
|
286
|
-
},
|
|
287
|
-
};
|
|
288
|
-
const tool = makeWriteOverride(
|
|
289
|
-
dir,
|
|
290
|
-
createActionFusionExecutor(() => {}),
|
|
291
|
-
);
|
|
292
|
-
try {
|
|
293
|
-
const failed = await callTool(
|
|
294
|
-
tool,
|
|
295
|
-
{
|
|
296
|
-
path: target,
|
|
297
|
-
content: "replacement\n",
|
|
298
|
-
mode: "overwrite",
|
|
299
|
-
then_run: { command: "deterministic-command" },
|
|
300
|
-
},
|
|
301
|
-
{ toolCallId: "shared-failure", ctx: context },
|
|
302
|
-
);
|
|
303
|
-
assert.equal(failed.isError, true);
|
|
304
|
-
const report = reportOf(failed, "write");
|
|
305
|
-
assert.ok(report);
|
|
306
|
-
assert.ok(report.publication === "NOT_PUBLISHED" || report.publication === "UNKNOWN");
|
|
307
|
-
assert.equal(report.then_run?.status, "skipped");
|
|
308
|
-
assert.equal(commandRuns, 0);
|
|
309
|
-
} finally {
|
|
310
|
-
holder.release();
|
|
311
|
-
await holder.done;
|
|
312
|
-
}
|
|
313
|
-
let unlocked = false;
|
|
314
|
-
for (let attempt = 0; attempt < 100; attempt++) {
|
|
315
|
-
try {
|
|
316
|
-
const probe = await open(target, "r");
|
|
317
|
-
await probe.close();
|
|
318
|
-
unlocked = true;
|
|
319
|
-
break;
|
|
320
|
-
} catch (error) {
|
|
321
|
-
if (
|
|
322
|
-
!(error instanceof Error) ||
|
|
323
|
-
!("code" in error) ||
|
|
324
|
-
(error as NodeJS.ErrnoException).code !== "EBUSY"
|
|
325
|
-
)
|
|
326
|
-
throw error;
|
|
327
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
assert.equal(unlocked, true, "Windows did not release the shared-access holder");
|
|
331
|
-
assert.equal(await readFile(target, "utf8"), "original\n");
|
|
332
|
-
const result = await callTool(
|
|
333
|
-
tool,
|
|
334
|
-
{
|
|
335
|
-
path: target,
|
|
336
|
-
content: "replacement\n",
|
|
337
|
-
mode: "overwrite",
|
|
338
|
-
then_run: { command: "deterministic-command" },
|
|
339
|
-
},
|
|
340
|
-
{ toolCallId: "shared-retry", ctx: context },
|
|
341
|
-
);
|
|
342
|
-
assert.equal(result.isError, undefined);
|
|
343
|
-
assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
|
|
344
|
-
assert.equal(commandRuns, 1);
|
|
345
|
-
assert.equal(await readFile(target, "utf8"), "replacement\n");
|
|
346
|
-
}));
|
|
347
|
-
|
|
348
|
-
test("cancellation during rename → successful publication reports the new bytes as published", async (t) =>
|
|
349
|
-
withTemp(async (dir) => {
|
|
350
|
-
const target = join(dir, "cancel.txt");
|
|
351
|
-
await writeFile(target, "old\n");
|
|
352
|
-
const controller = new AbortController();
|
|
353
|
-
const rename = fsPromises.rename;
|
|
354
|
-
t.mock.method(fsPromises, "rename", (...args: Parameters<typeof rename>) => {
|
|
355
|
-
const publishing = rename(...args);
|
|
356
|
-
controller.abort();
|
|
357
|
-
return publishing;
|
|
358
|
-
});
|
|
359
|
-
syncBuiltinESMExports();
|
|
360
|
-
try {
|
|
361
|
-
const result = await commitFile(target, "new\n", {
|
|
362
|
-
mode: "overwrite",
|
|
363
|
-
signal: controller.signal,
|
|
364
|
-
});
|
|
365
|
-
assert.equal(controller.signal.aborted, true);
|
|
366
|
-
assert.equal(result.publication, "PUBLISHED");
|
|
367
|
-
assert.equal(await readFile(target, "utf8"), "new\n");
|
|
368
|
-
} finally {
|
|
369
|
-
t.mock.restoreAll();
|
|
370
|
-
syncBuiltinESMExports();
|
|
371
|
-
}
|
|
372
|
-
}));
|
|
373
|
-
|
|
374
|
-
test("overwrite target changes during temporary staging → rejects before publishing", async (t) => {
|
|
375
|
-
const cases = [
|
|
376
|
-
{ change: "remove", code: "PATH_NOT_FOUND" },
|
|
377
|
-
{ change: "hard link", code: "MULTIPLE_HARD_LINKS" },
|
|
378
|
-
{ change: "symlink retarget", code: "FILE_CHANGED" },
|
|
379
|
-
] as const;
|
|
380
|
-
for (const { change, code } of cases) {
|
|
381
|
-
await t.test(change, () =>
|
|
382
|
-
withTemp(async (dir) => {
|
|
383
|
-
const firstDir = join(dir, "first");
|
|
384
|
-
const secondDir = join(dir, "second");
|
|
385
|
-
const alias = join(dir, "alias");
|
|
386
|
-
const original = join(firstDir, "file.txt");
|
|
387
|
-
const other = join(secondDir, "file.txt");
|
|
388
|
-
const retargets = change === "symlink retarget";
|
|
389
|
-
const target = retargets ? join(alias, "file.txt") : original;
|
|
390
|
-
const linkType = process.platform === "win32" ? "junction" : "dir";
|
|
391
|
-
await mkdir(firstDir);
|
|
392
|
-
await mkdir(secondDir);
|
|
393
|
-
await writeFile(original, "old\n");
|
|
394
|
-
if (retargets) {
|
|
395
|
-
await writeFile(other, "old\n");
|
|
396
|
-
await symlink(firstDir, alias, linkType);
|
|
397
|
-
}
|
|
398
|
-
const baseRevision = change === "remove" ? undefined : byteRevision(await readFile(target));
|
|
399
|
-
let staged = false;
|
|
400
|
-
const makeTemp = fsPromises.mkdtemp;
|
|
401
|
-
t.mock.method(fsPromises, "mkdtemp", async (...args: Parameters<typeof makeTemp>) => {
|
|
402
|
-
const temp = await makeTemp(...args);
|
|
403
|
-
staged = true;
|
|
404
|
-
if (change === "remove") await rm(original);
|
|
405
|
-
else if (change === "hard link") await createHardLink(original, other);
|
|
406
|
-
else {
|
|
407
|
-
await rm(alias);
|
|
408
|
-
await symlink(secondDir, alias, linkType);
|
|
409
|
-
}
|
|
410
|
-
return temp;
|
|
411
|
-
});
|
|
412
|
-
syncBuiltinESMExports();
|
|
413
|
-
try {
|
|
414
|
-
await assert.rejects(
|
|
415
|
-
commitFile(target, "new\n", { mode: "overwrite", baseRevision }),
|
|
416
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === code,
|
|
417
|
-
);
|
|
418
|
-
assert.equal(staged, true);
|
|
419
|
-
if (change === "remove") await assert.rejects(readFile(original), { code: "ENOENT" });
|
|
420
|
-
else {
|
|
421
|
-
assert.equal(await readFile(original, "utf8"), "old\n");
|
|
422
|
-
assert.equal(await readFile(other, "utf8"), "old\n");
|
|
423
|
-
}
|
|
424
|
-
} finally {
|
|
425
|
-
t.mock.restoreAll();
|
|
426
|
-
syncBuiltinESMExports();
|
|
427
|
-
}
|
|
428
|
-
}),
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
test("bound overwrite → reports published and observed revisions for the published bytes", async () =>
|
|
434
|
-
withTemp(async (dir) => {
|
|
435
|
-
const target = join(dir, "versions.txt");
|
|
436
|
-
await writeFile(target, "before\n");
|
|
437
|
-
const baseRevision = byteRevision(await readFile(target));
|
|
438
|
-
const result = await commitFile(target, "after\n", {
|
|
439
|
-
mode: "overwrite",
|
|
440
|
-
baseRevision,
|
|
441
|
-
});
|
|
442
|
-
assert.equal(result.publication, "PUBLISHED");
|
|
443
|
-
assert.equal(await readFile(target, "utf8"), "after\n");
|
|
444
|
-
assert.equal(result.publishedRevision, byteRevision(Buffer.from("after\n")));
|
|
445
|
-
assert.equal(result.observedRevision, result.publishedRevision);
|
|
446
|
-
}));
|
|
447
|
-
|
|
448
|
-
test("commit rejects lossy UTF-8 output before modifying or creating files", async () =>
|
|
449
|
-
withTemp(async (dir) => {
|
|
450
|
-
const existing = join(dir, "existing.txt");
|
|
451
|
-
const missing = join(dir, "missing.txt");
|
|
452
|
-
await writeFile(existing, "original\n");
|
|
453
|
-
for (const content of ["\ud800", "\udfff", "before\ud800after"]) {
|
|
454
|
-
for (const path of [existing, missing]) {
|
|
455
|
-
await assert.rejects(
|
|
456
|
-
commitFile(path, content, { mode: path === existing ? "overwrite" : "create" }),
|
|
457
|
-
(error: unknown) =>
|
|
458
|
-
error instanceof HashlineError && error.errorCode === "INVALID_UNICODE",
|
|
459
|
-
);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
assert.equal(await readFile(existing, "utf8"), "original\n");
|
|
463
|
-
await assert.rejects(readFile(missing), { code: "ENOENT" });
|
|
464
|
-
const content = "\uFEFFvalid 😀\r\n";
|
|
465
|
-
const result = await commitFile(existing, content, { mode: "overwrite" });
|
|
466
|
-
assert.deepEqual(await readFile(existing), Buffer.from(content));
|
|
467
|
-
assert.equal(result.publishedRevision, byteRevision(Buffer.from(content)));
|
|
468
|
-
}));
|
|
469
|
-
|
|
470
|
-
test("identical commits preserve the file and still enforce mode, revision, and cancellation", async () =>
|
|
471
|
-
withTemp(async (dir) => {
|
|
472
|
-
const target = join(dir, "same.txt");
|
|
473
|
-
const content = "\uFEFFsame\r\nbytes\n";
|
|
474
|
-
await writeFile(target, content);
|
|
475
|
-
const before = await stat(target);
|
|
476
|
-
const revision = byteRevision(await readFile(target));
|
|
477
|
-
const result = await commitFile(target, content, {
|
|
478
|
-
mode: "overwrite",
|
|
479
|
-
baseRevision: revision,
|
|
480
|
-
});
|
|
481
|
-
assert.deepEqual(result, {
|
|
482
|
-
publishedRevision: revision,
|
|
483
|
-
observedRevision: revision,
|
|
484
|
-
publication: "NOT_PUBLISHED",
|
|
485
|
-
});
|
|
486
|
-
const after = await stat(target);
|
|
487
|
-
assert.deepEqual(
|
|
488
|
-
[after.ino, after.mtimeMs, after.ctimeMs],
|
|
489
|
-
[before.ino, before.mtimeMs, before.ctimeMs],
|
|
490
|
-
);
|
|
491
|
-
await assert.rejects(commitFile(target, content, { mode: "create" }), /already exists/);
|
|
492
|
-
await assert.rejects(
|
|
493
|
-
commitFile(target, content, { mode: "overwrite", baseRevision: "stale" }),
|
|
494
|
-
(error: unknown) =>
|
|
495
|
-
error instanceof HashlineError &&
|
|
496
|
-
error.errorCode === "FILE_CHANGED" &&
|
|
497
|
-
error.message === "File changed after it was read.",
|
|
498
|
-
);
|
|
499
|
-
await assert.rejects(
|
|
500
|
-
commitFile(target, content, { mode: "overwrite", signal: AbortSignal.abort() }),
|
|
501
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
502
|
-
);
|
|
503
|
-
const created = await commitFile(join(dir, "empty"), "", { mode: "create" });
|
|
504
|
-
assert.equal(created.publication, "PUBLISHED");
|
|
505
|
-
}));
|
|
506
|
-
|
|
507
|
-
test("missing overwrite target → suggests create only without a base revision", async () =>
|
|
508
|
-
withTemp(async (dir) => {
|
|
509
|
-
const missing = join(dir, "missing");
|
|
510
|
-
for (const baseRevision of [undefined, "previous-revision"]) {
|
|
511
|
-
await assert.rejects(
|
|
512
|
-
commitFile(missing, "", { mode: "overwrite", baseRevision }),
|
|
513
|
-
(error: unknown) => {
|
|
514
|
-
assert.ok(error instanceof HashlineError);
|
|
515
|
-
assert.equal(error.errorCode, "PATH_NOT_FOUND");
|
|
516
|
-
assert.equal(error.message, PATH_ERROR_MESSAGES.PATH_NOT_FOUND);
|
|
517
|
-
assert.deepEqual(error.facts, { missing: [missing] });
|
|
518
|
-
assert.equal(error.next, baseRevision === undefined ? "useCreate" : undefined);
|
|
519
|
-
return true;
|
|
520
|
-
},
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
}));
|
|
524
|
-
|
|
525
|
-
test("file changes after the read → bound commits reject changed and unchanged output", async () =>
|
|
526
|
-
withTemp(async (dir) => {
|
|
527
|
-
const target = join(dir, "target.txt");
|
|
528
|
-
const initialContent = "line 1\nline 2\n";
|
|
529
|
-
await writeFile(target, initialContent);
|
|
530
|
-
const snapshot = await readEditableSnapshot(target, undefined);
|
|
531
|
-
assert.equal(snapshot.text, initialContent);
|
|
532
|
-
|
|
533
|
-
// External process modifies target after snapshot is read.
|
|
534
|
-
await writeFile(target, "line 1\nmodified line 2\n");
|
|
535
|
-
const externalRevision = byteRevision(await readFile(target));
|
|
536
|
-
|
|
537
|
-
for (const content of [snapshot.text, "replacement\n"]) {
|
|
538
|
-
await assert.rejects(
|
|
539
|
-
() =>
|
|
540
|
-
commitFile(target, content, { mode: "overwrite", baseRevision: snapshot.baseRevision }),
|
|
541
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "FILE_CHANGED",
|
|
542
|
-
);
|
|
543
|
-
assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
|
|
544
|
-
assert.equal(byteRevision(await readFile(target)), externalRevision);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// Without baseRevision, overwrite hashes the current target instead of using the snapshot.
|
|
548
|
-
const overwriteResult = await commitFile(target, snapshot.text, {
|
|
549
|
-
mode: "overwrite",
|
|
550
|
-
});
|
|
551
|
-
assert.equal(overwriteResult.publication, "PUBLISHED");
|
|
552
|
-
assert.equal(overwriteResult.publishedRevision, snapshot.baseRevision);
|
|
553
|
-
assert.equal(overwriteResult.observedRevision, overwriteResult.publishedRevision);
|
|
554
|
-
assert.equal(await readFile(target, "utf8"), initialContent);
|
|
555
|
-
|
|
556
|
-
// When target does not change externally, no-op commit succeeds and confirms observed revision.
|
|
557
|
-
const freshSnapshot = await readEditableSnapshot(target, undefined);
|
|
558
|
-
const noopResult = await commitFile(target, freshSnapshot.text, {
|
|
559
|
-
mode: "overwrite",
|
|
560
|
-
baseRevision: freshSnapshot.baseRevision,
|
|
561
|
-
});
|
|
562
|
-
assert.equal(noopResult.publication, "NOT_PUBLISHED");
|
|
563
|
-
assert.equal(noopResult.publishedRevision, freshSnapshot.baseRevision);
|
|
564
|
-
assert.equal(noopResult.observedRevision, freshSnapshot.baseRevision);
|
|
565
|
-
}));
|
|
566
|
-
|
|
567
|
-
test("no-op commit with baseRevision rejects if target was deleted externally", async () =>
|
|
568
|
-
withTemp(async (dir) => {
|
|
569
|
-
const target = join(dir, "deleted.txt");
|
|
570
|
-
await writeFile(target, "content\n");
|
|
571
|
-
const snapshot = await readEditableSnapshot(target, undefined);
|
|
572
|
-
|
|
573
|
-
await rm(target);
|
|
574
|
-
|
|
575
|
-
await assert.rejects(
|
|
576
|
-
commitFile(target, snapshot.text, {
|
|
577
|
-
mode: "overwrite",
|
|
578
|
-
baseRevision: snapshot.baseRevision,
|
|
579
|
-
}),
|
|
580
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "PATH_NOT_FOUND",
|
|
581
|
-
);
|
|
582
|
-
}));
|
|
583
|
-
|
|
584
|
-
test("commitFile temporary publication → handles existing and missing parents and preserves other failures", async (t) =>
|
|
585
|
-
withTemp(async (dir) => {
|
|
586
|
-
const originalMkdir = fsPromises.mkdir;
|
|
587
|
-
const originalMkdtemp = fsPromises.mkdtemp;
|
|
588
|
-
for (const scenario of ["existing", "nested", "denied"] as const) {
|
|
589
|
-
const parent = scenario === "nested" ? join(dir, "new", "nested") : dir;
|
|
590
|
-
const target = join(parent, `${scenario}.txt`);
|
|
591
|
-
let mkdirCalls = 0;
|
|
592
|
-
const denied = Object.assign(new Error("denied"), { code: "EPERM" });
|
|
593
|
-
try {
|
|
594
|
-
t.mock.method(fsPromises, "mkdir", async (...args: Parameters<typeof originalMkdir>) => {
|
|
595
|
-
mkdirCalls++;
|
|
596
|
-
if (scenario !== "nested") throw denied;
|
|
597
|
-
return originalMkdir(...args);
|
|
598
|
-
});
|
|
599
|
-
t.mock.method(
|
|
600
|
-
fsPromises,
|
|
601
|
-
"mkdtemp",
|
|
602
|
-
async (...args: Parameters<typeof originalMkdtemp>) => {
|
|
603
|
-
if (scenario === "denied") throw denied;
|
|
604
|
-
return originalMkdtemp(...args);
|
|
605
|
-
},
|
|
606
|
-
);
|
|
607
|
-
syncBuiltinESMExports();
|
|
608
|
-
if (scenario === "denied") {
|
|
609
|
-
await assert.rejects(
|
|
610
|
-
commitFile(target, "data", { mode: "create" }),
|
|
611
|
-
(error: unknown) =>
|
|
612
|
-
error instanceof HashlineError &&
|
|
613
|
-
error.errorCode === "PUBLISH_FAILED" &&
|
|
614
|
-
error.cause === denied,
|
|
615
|
-
);
|
|
616
|
-
await assert.rejects(readFile(target), { code: "ENOENT" });
|
|
617
|
-
} else {
|
|
618
|
-
const result = await commitFile(target, "data", { mode: "create" });
|
|
619
|
-
assert.equal(result.publication, "PUBLISHED");
|
|
620
|
-
assert.equal(await readFile(target, "utf8"), "data");
|
|
621
|
-
}
|
|
622
|
-
assert.equal(mkdirCalls, scenario === "nested" ? 1 : 0);
|
|
623
|
-
} finally {
|
|
624
|
-
t.mock.restoreAll();
|
|
625
|
-
syncBuiltinESMExports();
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}));
|
package/src/pi/file-read.test.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { test } from "node:test";
|
|
6
|
-
import { withFileRead } from "./file-read.ts";
|
|
7
|
-
|
|
8
|
-
for (const replacement of ["", "汉字\n"]) {
|
|
9
|
-
test(`${replacement ? "same-size rewrite" : "truncation"} during a whole-file read → rejects the unstable snapshot`, async (t) => {
|
|
10
|
-
const directory = await mkdtemp(join(tmpdir(), "hashline-file-read-"));
|
|
11
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
12
|
-
const path = join(directory, "source.txt");
|
|
13
|
-
await writeFile(path, "中文\n");
|
|
14
|
-
await utimes(path, new Date(0), new Date(0));
|
|
15
|
-
await assert.rejects(
|
|
16
|
-
withFileRead(path, undefined, async (handle) => {
|
|
17
|
-
const bytes = await handle.readFile();
|
|
18
|
-
await writeFile(path, replacement);
|
|
19
|
-
return bytes;
|
|
20
|
-
}),
|
|
21
|
-
/File changed/,
|
|
22
|
-
);
|
|
23
|
-
assert.equal(await readFile(path, "utf8"), replacement);
|
|
24
|
-
assert.equal(
|
|
25
|
-
(await withFileRead(path, undefined, (handle) => handle.readFile())).toString("utf8"),
|
|
26
|
-
replacement,
|
|
27
|
-
);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { test } from "node:test";
|
|
6
|
-
import { HashlineError } from "../core/errors.ts";
|
|
7
|
-
import { readEditableSnapshot } from "./file-commit.ts";
|
|
8
|
-
|
|
9
|
-
test("snapshot cancelled while opening → rejects as an unpublished cancellation", async (t) => {
|
|
10
|
-
const directory = await mkdtemp(join(tmpdir(), "hashline-snapshot-"));
|
|
11
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
12
|
-
const path = join(directory, "file.txt");
|
|
13
|
-
await writeFile(path, "before\n");
|
|
14
|
-
const controller = new AbortController();
|
|
15
|
-
const reading = readEditableSnapshot(path, controller.signal);
|
|
16
|
-
controller.abort();
|
|
17
|
-
await assert.rejects(
|
|
18
|
-
reading,
|
|
19
|
-
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
20
|
-
);
|
|
21
|
-
assert.equal(await readFile(path, "utf8"), "before\n");
|
|
22
|
-
});
|