@punks/cli 3.1.2 → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CHANGELOG.md +6 -0
- package/dist/baseline/scaffold-baseline/baseline.json +4 -4
- package/dist/baseline/scaffold-baseline/data/hooks/format-edited-file.mjs +13 -0
- package/dist/baseline/scaffold-baseline/data/scripts/sync-subagents.mjs +862 -49
- package/dist/baseline/scaffold-baseline/data/scripts/sync-subagents.test.ts +1295 -38
- package/dist/baseline/scaffold-baseline.json +4 -4
- package/dist/baseline/scaffold-baseline.tgz +0 -0
- package/dist/data/hooks/format-edited-file.mjs +13 -0
- package/dist/data/scripts/sync-subagents.mjs +862 -49
- package/dist/index.js +544 -34
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import {
|
|
3
3
|
cp,
|
|
4
|
+
link,
|
|
4
5
|
lstat,
|
|
5
6
|
mkdir,
|
|
6
7
|
mkdtemp,
|
|
7
8
|
readFile,
|
|
9
|
+
readdir,
|
|
10
|
+
realpath,
|
|
8
11
|
readlink,
|
|
9
12
|
rm,
|
|
10
13
|
symlink,
|
|
@@ -34,6 +37,46 @@ const requireDefined = <Value>(value: Value | undefined): Value => {
|
|
|
34
37
|
return value;
|
|
35
38
|
};
|
|
36
39
|
|
|
40
|
+
const repositoryInventory = (root: string): Promise<readonly string[]> => {
|
|
41
|
+
const visit = async (directory: string): Promise<readonly string[]> => {
|
|
42
|
+
const directoryEntries = await readdir(directory);
|
|
43
|
+
const names = directoryEntries.toSorted();
|
|
44
|
+
const nestedEntries = await Promise.all(
|
|
45
|
+
names.map(async (name) => {
|
|
46
|
+
const absolutePath = path.join(directory, name);
|
|
47
|
+
const relativePath = path.relative(root, absolutePath).split(path.sep).join("/");
|
|
48
|
+
const stat = await lstat(absolutePath);
|
|
49
|
+
if (stat.isSymbolicLink()) {
|
|
50
|
+
const linkTarget = await readlink(absolutePath);
|
|
51
|
+
return [`${relativePath}:symlink:${linkTarget}`];
|
|
52
|
+
}
|
|
53
|
+
if (stat.isDirectory()) {
|
|
54
|
+
return [`${relativePath}:directory`, ...(await visit(absolutePath))];
|
|
55
|
+
}
|
|
56
|
+
if (stat.isFile()) {
|
|
57
|
+
const content = await readFile(absolutePath);
|
|
58
|
+
return [`${relativePath}:file:${content.toString("base64")}`];
|
|
59
|
+
}
|
|
60
|
+
return [`${relativePath}:special:${stat.mode}`];
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
return nestedEntries.flat();
|
|
64
|
+
};
|
|
65
|
+
return visit(root);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const pathExists = async (targetPath: string): Promise<boolean> => {
|
|
69
|
+
try {
|
|
70
|
+
await lstat(targetPath);
|
|
71
|
+
return true;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
37
80
|
const scriptSourcePath = path.resolve(import.meta.dirname, "sync-subagents.mjs");
|
|
38
81
|
const projectionSourcePath = path.resolve(import.meta.dirname, "harness-projection");
|
|
39
82
|
|
|
@@ -220,6 +263,71 @@ const writeFixture = async ({
|
|
|
220
263
|
};
|
|
221
264
|
|
|
222
265
|
suite("sync-subagents script", () => {
|
|
266
|
+
test("atomically writes managed agents with a long valid basename", async () => {
|
|
267
|
+
const root = await writeFixture({
|
|
268
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
269
|
+
writeContextPlan: false,
|
|
270
|
+
});
|
|
271
|
+
const longAgentName = "a".repeat(230);
|
|
272
|
+
const manifestPath = path.join(root, ".agents/subagents/manifest.mjs");
|
|
273
|
+
const manifest = await readFile(manifestPath, "utf-8");
|
|
274
|
+
await writeFile(manifestPath, manifest.replaceAll("frontend-worker", longAgentName), "utf-8");
|
|
275
|
+
|
|
276
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
277
|
+
cwd: root,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
assertThat(
|
|
281
|
+
await readFile(path.join(root, ".codex/agents", `${longAgentName}.toml`), "utf-8"),
|
|
282
|
+
).toContain(`name = ${JSON.stringify(longAgentName)}`);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("atomically replaces a hard-linked Codex config without changing the shared inode", async () => {
|
|
286
|
+
const root = await writeFixture({
|
|
287
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
288
|
+
writeContextPlan: false,
|
|
289
|
+
});
|
|
290
|
+
const outsideDirectory = await mkdtemp(path.join(os.tmpdir(), "sync-subagents-shared-"));
|
|
291
|
+
const outsidePath = path.join(outsideDirectory, "config.toml");
|
|
292
|
+
const configPath = path.join(root, ".codex/config.toml");
|
|
293
|
+
await mkdir(path.dirname(configPath), { recursive: true });
|
|
294
|
+
await writeFile(outsidePath, "shared config\n", "utf-8");
|
|
295
|
+
await link(outsidePath, configPath);
|
|
296
|
+
|
|
297
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
298
|
+
cwd: root,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
assertThat(await readFile(outsidePath, "utf-8")).toBe("shared config\n");
|
|
302
|
+
assertThat(await readFile(configPath, "utf-8")).toContain("[agents]");
|
|
303
|
+
const configStat = await lstat(configPath);
|
|
304
|
+
const outsideStat = await lstat(outsidePath);
|
|
305
|
+
assertThat(configStat.ino).not.toBe(outsideStat.ino);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("atomically replaces a hard-linked managed agent without changing the shared inode", async () => {
|
|
309
|
+
const root = await writeFixture({
|
|
310
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
311
|
+
writeContextPlan: false,
|
|
312
|
+
});
|
|
313
|
+
const outsideDirectory = await mkdtemp(path.join(os.tmpdir(), "sync-subagents-agent-"));
|
|
314
|
+
const outsidePath = path.join(outsideDirectory, "frontend-worker.toml");
|
|
315
|
+
const agentPath = path.join(root, ".codex/agents/frontend-worker.toml");
|
|
316
|
+
await mkdir(path.dirname(agentPath), { recursive: true });
|
|
317
|
+
await writeFile(outsidePath, "shared agent\n", "utf-8");
|
|
318
|
+
await link(outsidePath, agentPath);
|
|
319
|
+
|
|
320
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
321
|
+
cwd: root,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
assertThat(await readFile(outsidePath, "utf-8")).toBe("shared agent\n");
|
|
325
|
+
assertThat(await readFile(agentPath, "utf-8")).toContain('name = "frontend-worker"');
|
|
326
|
+
const agentStat = await lstat(agentPath);
|
|
327
|
+
const outsideStat = await lstat(outsidePath);
|
|
328
|
+
assertThat(agentStat.ino).not.toBe(outsideStat.ino);
|
|
329
|
+
});
|
|
330
|
+
|
|
223
331
|
test("uses legacy sync only when the canonical context plan is absent", async () => {
|
|
224
332
|
const root = await writeFixture({
|
|
225
333
|
guidanceFile: "app/frontend/AGENTS.md",
|
|
@@ -238,6 +346,23 @@ suite("sync-subagents script", () => {
|
|
|
238
346
|
).toContain("Only this consumer-authored guidance");
|
|
239
347
|
});
|
|
240
348
|
|
|
349
|
+
test("materializes legacy output without creating a missing .devpunks parent", async () => {
|
|
350
|
+
const root = await writeFixture({
|
|
351
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
352
|
+
writeContextPlan: false,
|
|
353
|
+
});
|
|
354
|
+
await rm(path.join(root, ".devpunks"), { recursive: true });
|
|
355
|
+
|
|
356
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
357
|
+
cwd: root,
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
await assertThat(lstat(path.join(root, ".devpunks"))).rejects.toMatchObject({ code: "ENOENT" });
|
|
361
|
+
assertThat(
|
|
362
|
+
await readFile(path.join(root, ".codex/agents/frontend-worker.toml"), "utf-8"),
|
|
363
|
+
).toContain("Only this consumer-authored guidance");
|
|
364
|
+
});
|
|
365
|
+
|
|
241
366
|
test("removes stale legacy hook mirrors when the canonical hook source disappears", async () => {
|
|
242
367
|
const root = await writeFixture({
|
|
243
368
|
guidanceFile: "app/frontend/AGENTS.md",
|
|
@@ -1527,6 +1652,1068 @@ suite("sync-subagents script", () => {
|
|
|
1527
1652
|
}
|
|
1528
1653
|
});
|
|
1529
1654
|
|
|
1655
|
+
test("rejects a symlinked projection receipt without changing its external target", async () => {
|
|
1656
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-receipt-outside-"));
|
|
1657
|
+
const root = await writeFixture({ guidanceFile: "app/frontend/AGENTS.md" });
|
|
1658
|
+
const outsideReceipt = path.join(outside, "receipt.json");
|
|
1659
|
+
const originalContent = "{}\n";
|
|
1660
|
+
|
|
1661
|
+
try {
|
|
1662
|
+
await writeFile(outsideReceipt, originalContent, "utf-8");
|
|
1663
|
+
await rm(path.join(root, ".devpunks/harness-projection-receipt.json"), { force: true });
|
|
1664
|
+
await symlink(outsideReceipt, path.join(root, ".devpunks/harness-projection-receipt.json"));
|
|
1665
|
+
|
|
1666
|
+
await assertThat(
|
|
1667
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1668
|
+
cwd: root,
|
|
1669
|
+
}),
|
|
1670
|
+
).rejects.toThrow();
|
|
1671
|
+
const outsideContent = await readFile(outsideReceipt, "utf-8");
|
|
1672
|
+
assertThat(outsideContent).toBe(originalContent);
|
|
1673
|
+
} finally {
|
|
1674
|
+
await rm(root, { force: true, recursive: true });
|
|
1675
|
+
await rm(outside, { force: true, recursive: true });
|
|
1676
|
+
}
|
|
1677
|
+
});
|
|
1678
|
+
|
|
1679
|
+
test.runIf(process.platform !== "win32")(
|
|
1680
|
+
"rejects a repo-root swap before parent capture without changing outside inventory",
|
|
1681
|
+
async () => {
|
|
1682
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1683
|
+
const captureBoundary =
|
|
1684
|
+
"async function captureConfinedDirectoryIdentity(directoryPath, operation, repoRootIdentity) {";
|
|
1685
|
+
const scriptSource = sourceFile
|
|
1686
|
+
.replace("{ lstat, readFile,", "{ lstat, readFile, rename, symlink,")
|
|
1687
|
+
.replace(
|
|
1688
|
+
captureBoundary,
|
|
1689
|
+
[
|
|
1690
|
+
captureBoundary,
|
|
1691
|
+
" if (!globalThis.__HI_TEST_ROOT_SWAPPED) {",
|
|
1692
|
+
" globalThis.__HI_TEST_ROOT_SWAPPED = true;",
|
|
1693
|
+
" await rename(repoRoot, process.env.HI_TEST_DISPLACED_ROOT);",
|
|
1694
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_ROOT, repoRoot);",
|
|
1695
|
+
" }",
|
|
1696
|
+
].join("\n"),
|
|
1697
|
+
);
|
|
1698
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
1699
|
+
const root = await writeFixture({
|
|
1700
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1701
|
+
scriptSource,
|
|
1702
|
+
});
|
|
1703
|
+
const displacedRoot = `${root}-displaced`;
|
|
1704
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-root-swap-outside-"));
|
|
1705
|
+
await writeFile(path.join(outside, "sentinel.txt"), "outside sentinel\n", "utf-8");
|
|
1706
|
+
|
|
1707
|
+
try {
|
|
1708
|
+
await assertThat(
|
|
1709
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1710
|
+
cwd: root,
|
|
1711
|
+
env: {
|
|
1712
|
+
...process.env,
|
|
1713
|
+
HI_TEST_DISPLACED_ROOT: displacedRoot,
|
|
1714
|
+
HI_TEST_OUTSIDE_ROOT: outside,
|
|
1715
|
+
},
|
|
1716
|
+
}),
|
|
1717
|
+
).rejects.toThrow();
|
|
1718
|
+
|
|
1719
|
+
assertThat(await readdir(outside)).toStrictEqual(["sentinel.txt"]);
|
|
1720
|
+
assertThat(await readFile(path.join(outside, "sentinel.txt"), "utf-8")).toBe(
|
|
1721
|
+
"outside sentinel\n",
|
|
1722
|
+
);
|
|
1723
|
+
} finally {
|
|
1724
|
+
await rm(root, { force: true, recursive: true });
|
|
1725
|
+
await rm(displacedRoot, { force: true, recursive: true });
|
|
1726
|
+
await rm(outside, { force: true, recursive: true });
|
|
1727
|
+
}
|
|
1728
|
+
},
|
|
1729
|
+
);
|
|
1730
|
+
|
|
1731
|
+
test.runIf(process.platform !== "win32")(
|
|
1732
|
+
"keeps one repo-root identity across sequential sync mutations",
|
|
1733
|
+
async () => {
|
|
1734
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1735
|
+
const completedWriteBoundary = [
|
|
1736
|
+
" await assertSameDirectoryIdentity(parentDirectory, parentIdentity, operation, repoRootIdentity);",
|
|
1737
|
+
"}",
|
|
1738
|
+
"",
|
|
1739
|
+
"async function readConfinedRegularFile",
|
|
1740
|
+
].join("\n");
|
|
1741
|
+
const scriptSource = sourceFile
|
|
1742
|
+
.replace("{ lstat, readFile,", "{ lstat, mkdir, readFile, rename, writeFile,")
|
|
1743
|
+
.replace(
|
|
1744
|
+
completedWriteBoundary,
|
|
1745
|
+
[
|
|
1746
|
+
" await assertSameDirectoryIdentity(parentDirectory, parentIdentity, operation, repoRootIdentity);",
|
|
1747
|
+
" if (!globalThis.__HI_TEST_ROOT_REPLACED) {",
|
|
1748
|
+
" globalThis.__HI_TEST_ROOT_REPLACED = true;",
|
|
1749
|
+
" await rename(repoRoot, process.env.HI_TEST_DISPLACED_ROOT);",
|
|
1750
|
+
" await mkdir(repoRoot);",
|
|
1751
|
+
' await writeFile(path.join(repoRoot, "replacement.txt"), "replacement\\n");',
|
|
1752
|
+
" }",
|
|
1753
|
+
"}",
|
|
1754
|
+
"",
|
|
1755
|
+
"async function readConfinedRegularFile",
|
|
1756
|
+
].join("\n"),
|
|
1757
|
+
);
|
|
1758
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
1759
|
+
assertThat(scriptSource).toContain("__HI_TEST_ROOT_REPLACED");
|
|
1760
|
+
const root = await writeFixture({
|
|
1761
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1762
|
+
scriptSource,
|
|
1763
|
+
writeContextPlan: false,
|
|
1764
|
+
});
|
|
1765
|
+
const displacedRoot = `${root}-displaced`;
|
|
1766
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-root-session-outside-"));
|
|
1767
|
+
await writeFile(path.join(outside, "sentinel.txt"), "outside\n");
|
|
1768
|
+
|
|
1769
|
+
try {
|
|
1770
|
+
await assertThat(
|
|
1771
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1772
|
+
cwd: root,
|
|
1773
|
+
env: { ...process.env, HI_TEST_DISPLACED_ROOT: displacedRoot },
|
|
1774
|
+
}),
|
|
1775
|
+
).rejects.toThrow();
|
|
1776
|
+
assertThat(await readdir(root)).toStrictEqual(["replacement.txt"]);
|
|
1777
|
+
assertThat(await readdir(outside)).toStrictEqual(["sentinel.txt"]);
|
|
1778
|
+
assertThat(await pathExists(path.join(displacedRoot, ".codex"))).toBe(true);
|
|
1779
|
+
} finally {
|
|
1780
|
+
await rm(root, { force: true, recursive: true });
|
|
1781
|
+
await rm(displacedRoot, { force: true, recursive: true });
|
|
1782
|
+
await rm(outside, { force: true, recursive: true });
|
|
1783
|
+
}
|
|
1784
|
+
},
|
|
1785
|
+
);
|
|
1786
|
+
|
|
1787
|
+
test.runIf(process.platform !== "win32")(
|
|
1788
|
+
"binds sync directory creation to its validated parent",
|
|
1789
|
+
async () => {
|
|
1790
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1791
|
+
const mkdirBoundary = [
|
|
1792
|
+
" await runStableMutation(parentDirectory, {",
|
|
1793
|
+
' operation: "mkdir",',
|
|
1794
|
+
].join("\n");
|
|
1795
|
+
const scriptSource = sourceFile
|
|
1796
|
+
.replace("{ lstat,", "{ lstat, rename, symlink,")
|
|
1797
|
+
.replace(
|
|
1798
|
+
mkdirBoundary,
|
|
1799
|
+
[
|
|
1800
|
+
" await rename(parentDirectory, process.env.HI_TEST_DISPLACED_PARENT);",
|
|
1801
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_PARENT, parentDirectory);",
|
|
1802
|
+
mkdirBoundary,
|
|
1803
|
+
].join("\n"),
|
|
1804
|
+
);
|
|
1805
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
1806
|
+
const root = await writeFixture({
|
|
1807
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1808
|
+
scriptSource,
|
|
1809
|
+
writeContextPlan: false,
|
|
1810
|
+
});
|
|
1811
|
+
const displacedRoot = `${root}-displaced`;
|
|
1812
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-mkdir-session-outside-"));
|
|
1813
|
+
await writeFile(path.join(outside, "sentinel.txt"), "outside\n");
|
|
1814
|
+
|
|
1815
|
+
try {
|
|
1816
|
+
await assertThat(
|
|
1817
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1818
|
+
cwd: root,
|
|
1819
|
+
env: {
|
|
1820
|
+
...process.env,
|
|
1821
|
+
HI_TEST_DISPLACED_PARENT: displacedRoot,
|
|
1822
|
+
HI_TEST_OUTSIDE_PARENT: outside,
|
|
1823
|
+
},
|
|
1824
|
+
}),
|
|
1825
|
+
).rejects.toThrow();
|
|
1826
|
+
assertThat(await readdir(outside)).toStrictEqual(["sentinel.txt"]);
|
|
1827
|
+
assertThat(await pathExists(path.join(displacedRoot, ".codex"))).toBe(false);
|
|
1828
|
+
} finally {
|
|
1829
|
+
await rm(root, { force: true, recursive: true });
|
|
1830
|
+
await rm(displacedRoot, { force: true, recursive: true });
|
|
1831
|
+
await rm(outside, { force: true, recursive: true });
|
|
1832
|
+
}
|
|
1833
|
+
},
|
|
1834
|
+
);
|
|
1835
|
+
|
|
1836
|
+
test("settles worker shutdown promptly when spawn cwd is missing", async () => {
|
|
1837
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1838
|
+
const workerBoundary = "function createMutationWorker(parentDirectory, parentIdentity, key) {";
|
|
1839
|
+
const scriptSource = sourceFile.replace(
|
|
1840
|
+
workerBoundary,
|
|
1841
|
+
[workerBoundary, ' parentDirectory = path.join(repoRoot, "removed-worker-cwd");'].join("\n"),
|
|
1842
|
+
);
|
|
1843
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
1844
|
+
const root = await writeFixture({
|
|
1845
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1846
|
+
scriptSource,
|
|
1847
|
+
writeContextPlan: false,
|
|
1848
|
+
});
|
|
1849
|
+
const startedAt = Date.now();
|
|
1850
|
+
let failure: unknown;
|
|
1851
|
+
try {
|
|
1852
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1853
|
+
cwd: root,
|
|
1854
|
+
timeout: 2000,
|
|
1855
|
+
});
|
|
1856
|
+
} catch (error) {
|
|
1857
|
+
failure = error;
|
|
1858
|
+
} finally {
|
|
1859
|
+
await rm(root, { force: true, recursive: true });
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
assertThat(failure).toBeDefined();
|
|
1863
|
+
assertThat(Date.now() - startedAt).toBeLessThan(2000);
|
|
1864
|
+
assertThat(String(failure)).toContain("ENOENT");
|
|
1865
|
+
});
|
|
1866
|
+
|
|
1867
|
+
test("bounds mutation workers across many unique parents", async () => {
|
|
1868
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1869
|
+
const spawnBoundary = [" const child = spawn(", " process.execPath,"].join("\n");
|
|
1870
|
+
const sessionBoundary =
|
|
1871
|
+
' operationRepoRootIdentity = await captureRepoRootIdentity("synchronize scaffold output");';
|
|
1872
|
+
const closeBoundary = ["} finally {", " await closeMutationWorkers();", "}"].join("\n");
|
|
1873
|
+
const scriptSource = sourceFile
|
|
1874
|
+
.replace("{ lstat, readFile,", "{ lstat, readFile, writeFile,")
|
|
1875
|
+
.replace(
|
|
1876
|
+
"const mutationWorkers = new Map();",
|
|
1877
|
+
[
|
|
1878
|
+
"let activeWorkerCount = 0;",
|
|
1879
|
+
"let peakWorkerCount = 0;",
|
|
1880
|
+
"const instrumentedSpawn = (...args) => {",
|
|
1881
|
+
" const child = spawn(...args);",
|
|
1882
|
+
" activeWorkerCount += 1;",
|
|
1883
|
+
" peakWorkerCount = Math.max(peakWorkerCount, activeWorkerCount);",
|
|
1884
|
+
' child.once("exit", () => { activeWorkerCount -= 1; });',
|
|
1885
|
+
" return child;",
|
|
1886
|
+
"};",
|
|
1887
|
+
"const mutationWorkers = new Map();",
|
|
1888
|
+
].join("\n"),
|
|
1889
|
+
)
|
|
1890
|
+
.replace(spawnBoundary, spawnBoundary.replace("spawn", "instrumentedSpawn"))
|
|
1891
|
+
.replace(
|
|
1892
|
+
sessionBoundary,
|
|
1893
|
+
[
|
|
1894
|
+
sessionBoundary,
|
|
1895
|
+
" for (let index = 0; index < 24; index += 1) {",
|
|
1896
|
+
' const parentDirectory = path.join(repoRoot, "worker-parents", String(index));',
|
|
1897
|
+
" await ensureConfinedDirectory(parentDirectory);",
|
|
1898
|
+
' await writeConfinedTextFile(path.join(parentDirectory, "value.txt"), "parent-" + index + "\\n");',
|
|
1899
|
+
" }",
|
|
1900
|
+
].join("\n"),
|
|
1901
|
+
)
|
|
1902
|
+
.replace(
|
|
1903
|
+
closeBoundary,
|
|
1904
|
+
[
|
|
1905
|
+
"} finally {",
|
|
1906
|
+
" await closeMutationWorkers();",
|
|
1907
|
+
" await writeFile(",
|
|
1908
|
+
" process.env.HI_TEST_WORKER_STATS,",
|
|
1909
|
+
" JSON.stringify({ active: activeWorkerCount, peak: peakWorkerCount }),",
|
|
1910
|
+
" );",
|
|
1911
|
+
"}",
|
|
1912
|
+
].join("\n"),
|
|
1913
|
+
);
|
|
1914
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
1915
|
+
assertThat(scriptSource).not.toContain(spawnBoundary);
|
|
1916
|
+
assertThat(scriptSource).not.toContain(closeBoundary);
|
|
1917
|
+
const root = await writeFixture({
|
|
1918
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1919
|
+
scriptSource,
|
|
1920
|
+
writeContextPlan: false,
|
|
1921
|
+
});
|
|
1922
|
+
const statsPath = path.join(root, "worker-stats.json");
|
|
1923
|
+
|
|
1924
|
+
try {
|
|
1925
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1926
|
+
cwd: root,
|
|
1927
|
+
env: { ...process.env, HI_TEST_WORKER_STATS: statsPath },
|
|
1928
|
+
});
|
|
1929
|
+
const stats = JSON.parse(await readFile(statsPath, "utf-8")) as {
|
|
1930
|
+
readonly active: number;
|
|
1931
|
+
readonly peak: number;
|
|
1932
|
+
};
|
|
1933
|
+
assertThat(stats).toMatchObject({ active: 0 });
|
|
1934
|
+
assertThat(stats.peak).toBeLessThanOrEqual(8);
|
|
1935
|
+
const parentContents = await Promise.all(
|
|
1936
|
+
Array.from({ length: 24 }, (_, index) =>
|
|
1937
|
+
readFile(path.join(root, "worker-parents", String(index), "value.txt"), "utf-8"),
|
|
1938
|
+
),
|
|
1939
|
+
);
|
|
1940
|
+
assertThat(parentContents).toStrictEqual(
|
|
1941
|
+
Array.from({ length: 24 }, (_, index) => `parent-${index}\n`),
|
|
1942
|
+
);
|
|
1943
|
+
} finally {
|
|
1944
|
+
await rm(root, { force: true, recursive: true });
|
|
1945
|
+
}
|
|
1946
|
+
});
|
|
1947
|
+
|
|
1948
|
+
test.runIf(process.platform !== "win32")(
|
|
1949
|
+
"rejects FIFO, device, and symlink receipt leaves before their first read",
|
|
1950
|
+
async () => {
|
|
1951
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
1952
|
+
const scriptSource = sourceFile
|
|
1953
|
+
.replace("{ lstat, readFile,", "{ lstat, readFile as nodeReadFile, writeFile,")
|
|
1954
|
+
.replace(
|
|
1955
|
+
'const projectionReceiptPath = path.join(repoRoot, ".devpunks/harness-projection-receipt.json");',
|
|
1956
|
+
[
|
|
1957
|
+
'const projectionReceiptPath = path.join(repoRoot, ".devpunks/harness-projection-receipt.json");',
|
|
1958
|
+
"const readFile = async (filePath, ...args) => {",
|
|
1959
|
+
" if (path.resolve(filePath) === projectionReceiptPath) {",
|
|
1960
|
+
' await writeFile(process.env.HI_TEST_RECEIPT_READ_MARKER, "read reached\\n");',
|
|
1961
|
+
' throw new Error("Unsafe receipt read reached");',
|
|
1962
|
+
" }",
|
|
1963
|
+
" return nodeReadFile(filePath, ...args);",
|
|
1964
|
+
"};",
|
|
1965
|
+
].join("\n"),
|
|
1966
|
+
);
|
|
1967
|
+
|
|
1968
|
+
await Promise.all(
|
|
1969
|
+
(["fifo", "device", "symlink"] as const).map(async (leafType) => {
|
|
1970
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), `ip317-receipt-${leafType}-`));
|
|
1971
|
+
const root = await writeFixture({
|
|
1972
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
1973
|
+
scriptSource,
|
|
1974
|
+
});
|
|
1975
|
+
const receiptPath = path.join(root, ".devpunks/harness-projection-receipt.json");
|
|
1976
|
+
const markerPath = path.join(outside, "read-marker.txt");
|
|
1977
|
+
const outsideReceipt = path.join(outside, "receipt.json");
|
|
1978
|
+
|
|
1979
|
+
try {
|
|
1980
|
+
await rm(receiptPath, { force: true });
|
|
1981
|
+
if (leafType === "fifo") {
|
|
1982
|
+
await execFileAsync("mkfifo", [receiptPath]);
|
|
1983
|
+
} else if (leafType === "device") {
|
|
1984
|
+
await symlink("/dev/null", receiptPath);
|
|
1985
|
+
} else {
|
|
1986
|
+
await writeFile(outsideReceipt, "outside sentinel\n", "utf-8");
|
|
1987
|
+
await symlink(outsideReceipt, receiptPath);
|
|
1988
|
+
}
|
|
1989
|
+
const before = await repositoryInventory(root);
|
|
1990
|
+
|
|
1991
|
+
await assertThat(
|
|
1992
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
1993
|
+
cwd: root,
|
|
1994
|
+
env: { ...process.env, HI_TEST_RECEIPT_READ_MARKER: markerPath },
|
|
1995
|
+
timeout: 3000,
|
|
1996
|
+
}),
|
|
1997
|
+
).rejects.toThrow();
|
|
1998
|
+
|
|
1999
|
+
assertThat(await pathExists(markerPath), leafType).toBe(false);
|
|
2000
|
+
assertThat(await repositoryInventory(root), leafType).toStrictEqual(before);
|
|
2001
|
+
if (leafType === "symlink") {
|
|
2002
|
+
const outsideContent = await readFile(outsideReceipt, "utf-8");
|
|
2003
|
+
assertThat(outsideContent).toBe("outside sentinel\n");
|
|
2004
|
+
}
|
|
2005
|
+
} finally {
|
|
2006
|
+
await rm(root, { force: true, recursive: true });
|
|
2007
|
+
await rm(outside, { force: true, recursive: true });
|
|
2008
|
+
}
|
|
2009
|
+
}),
|
|
2010
|
+
);
|
|
2011
|
+
},
|
|
2012
|
+
);
|
|
2013
|
+
|
|
2014
|
+
test.runIf(process.platform !== "win32")(
|
|
2015
|
+
"rejects receipt leaf and parent swaps between preflight and descriptor read",
|
|
2016
|
+
async () => {
|
|
2017
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
2018
|
+
const boundary = " await assertConfinedRegularFile(projectionReceiptPath);";
|
|
2019
|
+
const returnBoundary = ' return Buffer.from(result.content, "base64");';
|
|
2020
|
+
const scriptSource = sourceFile
|
|
2021
|
+
.replace("{ lstat, readFile,", "{ lstat, readFile, rename, symlink, writeFile,")
|
|
2022
|
+
.replace(
|
|
2023
|
+
boundary,
|
|
2024
|
+
[
|
|
2025
|
+
boundary,
|
|
2026
|
+
' if (process.env.HI_TEST_RECEIPT_SWAP === "leaf") {',
|
|
2027
|
+
' await rename(projectionReceiptPath, projectionReceiptPath + "-displaced");',
|
|
2028
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_RECEIPT, projectionReceiptPath);",
|
|
2029
|
+
" } else {",
|
|
2030
|
+
" const receiptParent = path.dirname(projectionReceiptPath);",
|
|
2031
|
+
' await rename(receiptParent, receiptParent + "-displaced");',
|
|
2032
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_PARENT, receiptParent);",
|
|
2033
|
+
" }",
|
|
2034
|
+
].join("\n"),
|
|
2035
|
+
)
|
|
2036
|
+
.replace(
|
|
2037
|
+
returnBoundary,
|
|
2038
|
+
[
|
|
2039
|
+
' await writeFile(process.env.HI_TEST_RECEIPT_READ_MARKER, "read reached\\n");',
|
|
2040
|
+
returnBoundary,
|
|
2041
|
+
].join("\n"),
|
|
2042
|
+
);
|
|
2043
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
2044
|
+
assertThat(scriptSource).toContain("HI_TEST_RECEIPT_READ_MARKER");
|
|
2045
|
+
|
|
2046
|
+
await Promise.all(
|
|
2047
|
+
(["leaf", "parent"] as const).map(async (swap) => {
|
|
2048
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), `ip317-receipt-${swap}-outside-`));
|
|
2049
|
+
const root = await writeFixture({
|
|
2050
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2051
|
+
scriptSource,
|
|
2052
|
+
});
|
|
2053
|
+
const receiptPath = path.join(root, ".devpunks/harness-projection-receipt.json");
|
|
2054
|
+
const displacedPath =
|
|
2055
|
+
swap === "leaf" ? `${receiptPath}-displaced` : path.join(root, ".devpunks-displaced");
|
|
2056
|
+
const outsideReceipt = path.join(outside, "harness-projection-receipt.json");
|
|
2057
|
+
const markerPath = path.join(outside, "read-marker.txt");
|
|
2058
|
+
const originalReceipt = "{}\n";
|
|
2059
|
+
await writeFile(receiptPath, originalReceipt, "utf-8");
|
|
2060
|
+
await writeFile(outsideReceipt, "{}\n", "utf-8");
|
|
2061
|
+
|
|
2062
|
+
try {
|
|
2063
|
+
await assertThat(
|
|
2064
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2065
|
+
cwd: root,
|
|
2066
|
+
env: {
|
|
2067
|
+
...process.env,
|
|
2068
|
+
HI_TEST_OUTSIDE_PARENT: outside,
|
|
2069
|
+
HI_TEST_OUTSIDE_RECEIPT: outsideReceipt,
|
|
2070
|
+
HI_TEST_RECEIPT_READ_MARKER: markerPath,
|
|
2071
|
+
HI_TEST_RECEIPT_SWAP: swap,
|
|
2072
|
+
},
|
|
2073
|
+
timeout: 3000,
|
|
2074
|
+
}),
|
|
2075
|
+
).rejects.toThrow();
|
|
2076
|
+
|
|
2077
|
+
assertThat(await pathExists(markerPath), swap).toBe(false);
|
|
2078
|
+
assertThat(await readFile(outsideReceipt, "utf-8"), swap).toBe("{}\n");
|
|
2079
|
+
const displacedReceipt =
|
|
2080
|
+
swap === "leaf"
|
|
2081
|
+
? displacedPath
|
|
2082
|
+
: path.join(displacedPath, "harness-projection-receipt.json");
|
|
2083
|
+
assertThat(await readFile(displacedReceipt, "utf-8"), swap).toBe(originalReceipt);
|
|
2084
|
+
const outsideEntries = await readdir(outside);
|
|
2085
|
+
const outsideInventory = outsideEntries.toSorted();
|
|
2086
|
+
assertThat(outsideInventory, swap).toStrictEqual(["harness-projection-receipt.json"]);
|
|
2087
|
+
} finally {
|
|
2088
|
+
await rm(root, { force: true, recursive: true });
|
|
2089
|
+
await rm(displacedPath, { force: true, recursive: true });
|
|
2090
|
+
await rm(outside, { force: true, recursive: true });
|
|
2091
|
+
}
|
|
2092
|
+
}),
|
|
2093
|
+
);
|
|
2094
|
+
},
|
|
2095
|
+
);
|
|
2096
|
+
|
|
2097
|
+
test.runIf(process.platform !== "win32")(
|
|
2098
|
+
"checks receipt descriptor identity before read when no-follow is unavailable",
|
|
2099
|
+
async () => {
|
|
2100
|
+
await Promise.all(
|
|
2101
|
+
(["symlink", "descriptor"] as const).map(async (substitution) => {
|
|
2102
|
+
const outside = await mkdtemp(
|
|
2103
|
+
path.join(os.tmpdir(), `ip317-receipt-descriptor-${substitution}-`),
|
|
2104
|
+
);
|
|
2105
|
+
const outsideReceipt = path.join(outside, "outside-receipt.json");
|
|
2106
|
+
const markerPath = path.join(outside, "read-marker.txt");
|
|
2107
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
2108
|
+
const boundary =
|
|
2109
|
+
" const target = await assertConfinedRegularFile(targetPath, repoRootIdentity);";
|
|
2110
|
+
const scriptSource = sourceFile
|
|
2111
|
+
.replace("{ lstat, readFile,", "{ link, lstat, readFile, rename, symlink,")
|
|
2112
|
+
.replace("(constants.O_NOFOLLOW ?? 0)", "0")
|
|
2113
|
+
.replace(
|
|
2114
|
+
" const bytesRead = readSync(descriptor, chunk, 0, chunk.length, null);",
|
|
2115
|
+
` writeFileSync(${JSON.stringify(markerPath)}, "read reached\\n");\n const bytesRead = readSync(descriptor, chunk, 0, chunk.length, null);`,
|
|
2116
|
+
)
|
|
2117
|
+
.replace(
|
|
2118
|
+
boundary,
|
|
2119
|
+
[
|
|
2120
|
+
boundary,
|
|
2121
|
+
' if (path.basename(targetPath) === "harness-projection-receipt.json") {',
|
|
2122
|
+
' await rename(targetPath, targetPath + "-displaced");',
|
|
2123
|
+
substitution === "symlink"
|
|
2124
|
+
? ` await symlink(${JSON.stringify(outsideReceipt)}, targetPath);`
|
|
2125
|
+
: ` await link(${JSON.stringify(outsideReceipt)}, targetPath);`,
|
|
2126
|
+
" }",
|
|
2127
|
+
].join("\n"),
|
|
2128
|
+
);
|
|
2129
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
2130
|
+
const root = await writeFixture({
|
|
2131
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2132
|
+
scriptSource,
|
|
2133
|
+
});
|
|
2134
|
+
const receiptPath = path.join(root, ".devpunks/harness-projection-receipt.json");
|
|
2135
|
+
const displacedReceipt = `${receiptPath}-displaced`;
|
|
2136
|
+
await writeFile(receiptPath, "inside receipt\n", "utf-8");
|
|
2137
|
+
await writeFile(outsideReceipt, "outside sentinel\n", "utf-8");
|
|
2138
|
+
|
|
2139
|
+
try {
|
|
2140
|
+
await assertThat(
|
|
2141
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2142
|
+
cwd: root,
|
|
2143
|
+
}),
|
|
2144
|
+
).rejects.toThrow();
|
|
2145
|
+
assertThat({
|
|
2146
|
+
displaced: await readFile(displacedReceipt, "utf-8"),
|
|
2147
|
+
markerExists: await pathExists(markerPath),
|
|
2148
|
+
outside: await readFile(outsideReceipt, "utf-8"),
|
|
2149
|
+
}).toStrictEqual({
|
|
2150
|
+
displaced: "inside receipt\n",
|
|
2151
|
+
markerExists: false,
|
|
2152
|
+
outside: "outside sentinel\n",
|
|
2153
|
+
});
|
|
2154
|
+
} finally {
|
|
2155
|
+
await rm(root, { force: true, recursive: true });
|
|
2156
|
+
await rm(outside, { force: true, recursive: true });
|
|
2157
|
+
}
|
|
2158
|
+
}),
|
|
2159
|
+
);
|
|
2160
|
+
},
|
|
2161
|
+
);
|
|
2162
|
+
|
|
2163
|
+
test("rejects a parent swap immediately before rename without changing an outside sentinel", async () => {
|
|
2164
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-parent-swap-outside-"));
|
|
2165
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2166
|
+
const source = scriptSource.replace("{ lstat,", "{ lstat, rename, symlink,");
|
|
2167
|
+
const mutationBoundary = [
|
|
2168
|
+
" await runStableMutation(",
|
|
2169
|
+
" parentDirectory,",
|
|
2170
|
+
" {",
|
|
2171
|
+
' operation: "write",',
|
|
2172
|
+
].join("\n");
|
|
2173
|
+
const injectedBoundary = [
|
|
2174
|
+
' if (targetPath === projectionReceiptPath && process.env.HI_TEST_SWAP_PARENT === "1") {',
|
|
2175
|
+
" delete process.env.HI_TEST_SWAP_PARENT;",
|
|
2176
|
+
' await rename(parentDirectory, parentDirectory + "-displaced");',
|
|
2177
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_PARENT, parentDirectory);",
|
|
2178
|
+
" }",
|
|
2179
|
+
mutationBoundary,
|
|
2180
|
+
].join("\n");
|
|
2181
|
+
const transformed = source.replace(mutationBoundary, injectedBoundary);
|
|
2182
|
+
assertThat(transformed).not.toBe(source);
|
|
2183
|
+
const root = await writeFixture({
|
|
2184
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2185
|
+
scriptSource: transformed,
|
|
2186
|
+
});
|
|
2187
|
+
const outsideReceipt = path.join(outside, "harness-projection-receipt.json");
|
|
2188
|
+
|
|
2189
|
+
try {
|
|
2190
|
+
await writeFile(outsideReceipt, "outside sentinel\n", "utf-8");
|
|
2191
|
+
await assertThat(
|
|
2192
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2193
|
+
cwd: root,
|
|
2194
|
+
env: {
|
|
2195
|
+
...process.env,
|
|
2196
|
+
HI_TEST_OUTSIDE_PARENT: outside,
|
|
2197
|
+
HI_TEST_SWAP_PARENT: "1",
|
|
2198
|
+
},
|
|
2199
|
+
}),
|
|
2200
|
+
).rejects.toThrow();
|
|
2201
|
+
assertThat(await readFile(outsideReceipt, "utf-8")).toBe("outside sentinel\n");
|
|
2202
|
+
assertThat(await readdir(outside)).toEqual(["harness-projection-receipt.json"]);
|
|
2203
|
+
} finally {
|
|
2204
|
+
await rm(root, { force: true, recursive: true });
|
|
2205
|
+
await rm(outside, { force: true, recursive: true });
|
|
2206
|
+
}
|
|
2207
|
+
});
|
|
2208
|
+
|
|
2209
|
+
test("preserves a concurrent target substitution before confined write publication", async () => {
|
|
2210
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
2211
|
+
const publicationBoundary = [
|
|
2212
|
+
" closeSync(descriptor);",
|
|
2213
|
+
" descriptor = undefined;",
|
|
2214
|
+
" assertParentIdentity();",
|
|
2215
|
+
].join("\n");
|
|
2216
|
+
const scriptSource = sourceFile.replace(
|
|
2217
|
+
publicationBoundary,
|
|
2218
|
+
[
|
|
2219
|
+
" closeSync(descriptor);",
|
|
2220
|
+
" descriptor = undefined;",
|
|
2221
|
+
' if (request.target === "frontend-worker.toml") {',
|
|
2222
|
+
' renameSync(request.target, request.target + ".displaced");',
|
|
2223
|
+
' writeFileSync(request.target, "concurrent replacement\\n");',
|
|
2224
|
+
" }",
|
|
2225
|
+
" assertParentIdentity();",
|
|
2226
|
+
].join("\n"),
|
|
2227
|
+
);
|
|
2228
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
2229
|
+
const root = await writeFixture({
|
|
2230
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2231
|
+
scriptSource,
|
|
2232
|
+
writeContextPlan: false,
|
|
2233
|
+
});
|
|
2234
|
+
const target = path.join(root, ".codex/agents/frontend-worker.toml");
|
|
2235
|
+
const displaced = `${target}.displaced`;
|
|
2236
|
+
const original = "consumer-owned original\n";
|
|
2237
|
+
|
|
2238
|
+
try {
|
|
2239
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
2240
|
+
await writeFile(target, original, "utf-8");
|
|
2241
|
+
|
|
2242
|
+
await assertThat(
|
|
2243
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2244
|
+
cwd: root,
|
|
2245
|
+
env: process.env,
|
|
2246
|
+
}),
|
|
2247
|
+
).rejects.toThrow();
|
|
2248
|
+
|
|
2249
|
+
assertThat(await readFile(target, "utf-8")).toBe("concurrent replacement\n");
|
|
2250
|
+
assertThat(await readFile(displaced, "utf-8")).toBe(original);
|
|
2251
|
+
const targetEntries = await readdir(path.dirname(target));
|
|
2252
|
+
assertThat(targetEntries.some((name) => name.startsWith(".hi-"))).toBe(false);
|
|
2253
|
+
} finally {
|
|
2254
|
+
await rm(root, { force: true, recursive: true });
|
|
2255
|
+
}
|
|
2256
|
+
});
|
|
2257
|
+
|
|
2258
|
+
test.runIf(process.platform !== "win32")(
|
|
2259
|
+
"binds adapter config and managed-agent reads to validated descriptors",
|
|
2260
|
+
async () => {
|
|
2261
|
+
const sourceFile = await readFile(scriptSourcePath, "utf-8");
|
|
2262
|
+
const readImport =
|
|
2263
|
+
'import { lstat, readFile, realpath, readdir, readlink } from "node:fs/promises";';
|
|
2264
|
+
const scriptSource = sourceFile
|
|
2265
|
+
.replace(
|
|
2266
|
+
readImport,
|
|
2267
|
+
'import { lstat, readFile as nodeReadFile, realpath, readdir, readlink, rename, symlink, writeFile } from "node:fs/promises";',
|
|
2268
|
+
)
|
|
2269
|
+
.replace(
|
|
2270
|
+
'const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");',
|
|
2271
|
+
[
|
|
2272
|
+
"const readFile = async (filePath, ...args) => {",
|
|
2273
|
+
" if (path.relative(repoRoot, path.resolve(filePath)) === process.env.HI_TEST_READ_SWAP_TARGET) {",
|
|
2274
|
+
' await writeFile(process.env.HI_TEST_PATH_READ_MARKER, "pathname read reached\\n");',
|
|
2275
|
+
" }",
|
|
2276
|
+
" return nodeReadFile(filePath, ...args);",
|
|
2277
|
+
"};",
|
|
2278
|
+
"",
|
|
2279
|
+
'const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");',
|
|
2280
|
+
].join("\n"),
|
|
2281
|
+
)
|
|
2282
|
+
.replace(
|
|
2283
|
+
" await assertConfinedRegularFile(claudeSettingsPath);",
|
|
2284
|
+
[
|
|
2285
|
+
" await assertConfinedRegularFile(claudeSettingsPath);",
|
|
2286
|
+
" if (path.relative(repoRoot, claudeSettingsPath) === process.env.HI_TEST_READ_SWAP_TARGET) {",
|
|
2287
|
+
' await rename(claudeSettingsPath, claudeSettingsPath + ".displaced");',
|
|
2288
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_READ_TARGET, claudeSettingsPath);",
|
|
2289
|
+
" }",
|
|
2290
|
+
].join("\n"),
|
|
2291
|
+
)
|
|
2292
|
+
.replace(
|
|
2293
|
+
" await assertConfinedRegularFile(opencodeConfigPath);",
|
|
2294
|
+
[
|
|
2295
|
+
" await assertConfinedRegularFile(opencodeConfigPath);",
|
|
2296
|
+
" if (path.relative(repoRoot, opencodeConfigPath) === process.env.HI_TEST_READ_SWAP_TARGET) {",
|
|
2297
|
+
' await rename(opencodeConfigPath, opencodeConfigPath + ".displaced");',
|
|
2298
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_READ_TARGET, opencodeConfigPath);",
|
|
2299
|
+
" }",
|
|
2300
|
+
].join("\n"),
|
|
2301
|
+
)
|
|
2302
|
+
.replace(
|
|
2303
|
+
" await assertConfinedRegularFile(filePath);",
|
|
2304
|
+
[
|
|
2305
|
+
" await assertConfinedRegularFile(filePath);",
|
|
2306
|
+
" if (path.relative(repoRoot, filePath) === process.env.HI_TEST_READ_SWAP_TARGET) {",
|
|
2307
|
+
' await rename(filePath, filePath + ".displaced");',
|
|
2308
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_READ_TARGET, filePath);",
|
|
2309
|
+
" }",
|
|
2310
|
+
].join("\n"),
|
|
2311
|
+
);
|
|
2312
|
+
assertThat(scriptSource).not.toBe(sourceFile);
|
|
2313
|
+
assertThat(scriptSource).toContain("HI_TEST_PATH_READ_MARKER");
|
|
2314
|
+
|
|
2315
|
+
await Promise.all(
|
|
2316
|
+
[
|
|
2317
|
+
{ content: "{}\n", relativePath: ".claude/settings.json" },
|
|
2318
|
+
{ content: "{}\n", relativePath: "opencode.json" },
|
|
2319
|
+
{
|
|
2320
|
+
content:
|
|
2321
|
+
"<!-- Generated by scripts/sync-subagents.mjs. Edit .agents/subagents/manifest.mjs instead. -->\n",
|
|
2322
|
+
relativePath: ".claude/agents/stale.md",
|
|
2323
|
+
},
|
|
2324
|
+
].map(async (testCase) => {
|
|
2325
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-confined-read-outside-"));
|
|
2326
|
+
const root = await writeFixture({
|
|
2327
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2328
|
+
scriptSource,
|
|
2329
|
+
writeContextPlan: false,
|
|
2330
|
+
});
|
|
2331
|
+
const target = path.join(root, testCase.relativePath);
|
|
2332
|
+
const displaced = `${target}.displaced`;
|
|
2333
|
+
const outsideTarget = path.join(outside, path.basename(target));
|
|
2334
|
+
const marker = path.join(outside, "pathname-read.txt");
|
|
2335
|
+
|
|
2336
|
+
try {
|
|
2337
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
2338
|
+
await writeFile(target, testCase.content, "utf-8");
|
|
2339
|
+
await writeFile(outsideTarget, testCase.content, "utf-8");
|
|
2340
|
+
|
|
2341
|
+
let failure: unknown;
|
|
2342
|
+
try {
|
|
2343
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2344
|
+
cwd: root,
|
|
2345
|
+
env: {
|
|
2346
|
+
...process.env,
|
|
2347
|
+
HI_TEST_OUTSIDE_READ_TARGET: outsideTarget,
|
|
2348
|
+
HI_TEST_PATH_READ_MARKER: marker,
|
|
2349
|
+
HI_TEST_READ_SWAP_TARGET: testCase.relativePath,
|
|
2350
|
+
},
|
|
2351
|
+
timeout: 3000,
|
|
2352
|
+
});
|
|
2353
|
+
} catch (error) {
|
|
2354
|
+
failure = error;
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
assertThat(await pathExists(displaced), testCase.relativePath).toBe(true);
|
|
2358
|
+
assertThat(failure, testCase.relativePath).toBeDefined();
|
|
2359
|
+
assertThat(await pathExists(marker), testCase.relativePath).toBe(false);
|
|
2360
|
+
assertThat(await readFile(displaced, "utf-8"), testCase.relativePath).toBe(
|
|
2361
|
+
testCase.content,
|
|
2362
|
+
);
|
|
2363
|
+
assertThat(await readFile(outsideTarget, "utf-8"), testCase.relativePath).toBe(
|
|
2364
|
+
testCase.content,
|
|
2365
|
+
);
|
|
2366
|
+
} finally {
|
|
2367
|
+
await rm(root, { force: true, recursive: true });
|
|
2368
|
+
await rm(outside, { force: true, recursive: true });
|
|
2369
|
+
}
|
|
2370
|
+
}),
|
|
2371
|
+
);
|
|
2372
|
+
},
|
|
2373
|
+
);
|
|
2374
|
+
|
|
2375
|
+
test("rejects oversized adapter configs and managed agents before consuming their content", async () => {
|
|
2376
|
+
const oversizedJson = JSON.stringify({ padding: "x".repeat(16 * 1024 * 1024) });
|
|
2377
|
+
const oversizedManaged = `${[
|
|
2378
|
+
"# Generated by scripts/sync-subagents.mjs.",
|
|
2379
|
+
"# Do not edit by hand; update .agents/subagents/manifest.mjs and re-run sync.",
|
|
2380
|
+
"",
|
|
2381
|
+
].join("\n")}${"x".repeat(16 * 1024 * 1024)}`;
|
|
2382
|
+
|
|
2383
|
+
await Promise.all(
|
|
2384
|
+
[
|
|
2385
|
+
{ content: oversizedJson, relativePath: ".claude/settings.json" },
|
|
2386
|
+
{ content: oversizedJson, relativePath: "opencode.json" },
|
|
2387
|
+
{ content: oversizedManaged, relativePath: ".claude/agents/stale.md" },
|
|
2388
|
+
].map(async (testCase) => {
|
|
2389
|
+
const root = await writeFixture({
|
|
2390
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2391
|
+
writeContextPlan: false,
|
|
2392
|
+
});
|
|
2393
|
+
const target = path.join(root, testCase.relativePath);
|
|
2394
|
+
|
|
2395
|
+
try {
|
|
2396
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
2397
|
+
await writeFile(target, testCase.content, "utf-8");
|
|
2398
|
+
|
|
2399
|
+
await assertThat(
|
|
2400
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2401
|
+
cwd: root,
|
|
2402
|
+
}),
|
|
2403
|
+
).rejects.toThrow();
|
|
2404
|
+
assertThat(await readFile(target, "utf-8"), testCase.relativePath).toBe(testCase.content);
|
|
2405
|
+
} finally {
|
|
2406
|
+
await rm(root, { force: true, recursive: true });
|
|
2407
|
+
}
|
|
2408
|
+
}),
|
|
2409
|
+
);
|
|
2410
|
+
});
|
|
2411
|
+
|
|
2412
|
+
test.runIf(process.platform !== "win32")(
|
|
2413
|
+
"rejects FIFO adapter configs and managed agents without opening them",
|
|
2414
|
+
async () => {
|
|
2415
|
+
await Promise.all(
|
|
2416
|
+
[".claude/settings.json", "opencode.json", ".claude/agents/stale.md"].map(
|
|
2417
|
+
async (relativePath) => {
|
|
2418
|
+
const root = await writeFixture({
|
|
2419
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2420
|
+
writeContextPlan: false,
|
|
2421
|
+
});
|
|
2422
|
+
const target = path.join(root, relativePath);
|
|
2423
|
+
|
|
2424
|
+
try {
|
|
2425
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
2426
|
+
await execFileAsync("mkfifo", [target]);
|
|
2427
|
+
|
|
2428
|
+
await assertThat(
|
|
2429
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2430
|
+
cwd: root,
|
|
2431
|
+
timeout: 3000,
|
|
2432
|
+
}),
|
|
2433
|
+
).rejects.toThrow();
|
|
2434
|
+
const targetStat = await lstat(target);
|
|
2435
|
+
assertThat(targetStat.isFIFO(), relativePath).toBe(true);
|
|
2436
|
+
} finally {
|
|
2437
|
+
await rm(root, { force: true, recursive: true });
|
|
2438
|
+
}
|
|
2439
|
+
},
|
|
2440
|
+
),
|
|
2441
|
+
);
|
|
2442
|
+
},
|
|
2443
|
+
);
|
|
2444
|
+
|
|
2445
|
+
test("does not remove outside content when a parent swaps after removal validation", async () => {
|
|
2446
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-remove-swap-outside-"));
|
|
2447
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2448
|
+
const removalRequestBoundary =
|
|
2449
|
+
// oxlint-disable-next-line eslint/no-template-curly-in-string -- Match literal source.
|
|
2450
|
+
' await runStableMutation(parentDirectory, {\n claim: `.hi-remove-${randomUUID()}`,\n expectedTarget,\n operation: "remove",';
|
|
2451
|
+
const source = scriptSource
|
|
2452
|
+
.replace("{ lstat,", "{ lstat, rename, symlink,")
|
|
2453
|
+
.replace(
|
|
2454
|
+
removalRequestBoundary,
|
|
2455
|
+
[
|
|
2456
|
+
' if (path.basename(targetPath) === "stale.md") {',
|
|
2457
|
+
' await rename(parentDirectory, parentDirectory + "-displaced");',
|
|
2458
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_PARENT, parentDirectory);",
|
|
2459
|
+
" }",
|
|
2460
|
+
removalRequestBoundary,
|
|
2461
|
+
].join("\n"),
|
|
2462
|
+
);
|
|
2463
|
+
assertThat(source).not.toBe(scriptSource);
|
|
2464
|
+
const root = await writeFixture({
|
|
2465
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2466
|
+
scriptSource: source,
|
|
2467
|
+
});
|
|
2468
|
+
const staleAgent = path.join(root, ".claude/agents/stale.md");
|
|
2469
|
+
const outsideSentinel = path.join(outside, "stale.md");
|
|
2470
|
+
|
|
2471
|
+
try {
|
|
2472
|
+
await mkdir(path.dirname(staleAgent), { recursive: true });
|
|
2473
|
+
await writeFile(
|
|
2474
|
+
staleAgent,
|
|
2475
|
+
"<!-- Generated by scripts/sync-subagents.mjs. Edit .agents/subagents/manifest.mjs instead. -->\n",
|
|
2476
|
+
"utf-8",
|
|
2477
|
+
);
|
|
2478
|
+
await writeFile(outsideSentinel, "outside sentinel\n", "utf-8");
|
|
2479
|
+
await writeFile(
|
|
2480
|
+
path.join(root, ".devpunks/context-plan.json"),
|
|
2481
|
+
'{"version":"1","scopes":[]}\n',
|
|
2482
|
+
);
|
|
2483
|
+
|
|
2484
|
+
await assertThat(
|
|
2485
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2486
|
+
cwd: root,
|
|
2487
|
+
env: { ...process.env, HI_TEST_OUTSIDE_PARENT: outside },
|
|
2488
|
+
}),
|
|
2489
|
+
).rejects.toThrow();
|
|
2490
|
+
assertThat(await readFile(outsideSentinel, "utf-8")).toBe("outside sentinel\n");
|
|
2491
|
+
assertThat(await readdir(outside)).toEqual(["stale.md"]);
|
|
2492
|
+
} finally {
|
|
2493
|
+
await rm(root, { force: true, recursive: true });
|
|
2494
|
+
await rm(outside, { force: true, recursive: true });
|
|
2495
|
+
}
|
|
2496
|
+
});
|
|
2497
|
+
|
|
2498
|
+
test("preserves a directory substituted before recursive stale cleanup", async () => {
|
|
2499
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2500
|
+
const boundary =
|
|
2501
|
+
// oxlint-disable-next-line eslint/no-template-curly-in-string -- Match literal source.
|
|
2502
|
+
' await runStableMutation(parentDirectory, {\n claim: `.hi-remove-${randomUUID()}`,\n expectedTarget,\n operation: "remove",';
|
|
2503
|
+
const source = scriptSource
|
|
2504
|
+
.replace("{ lstat, readFile,", "{ lstat, mkdir, readFile, rename, writeFile,")
|
|
2505
|
+
.replace(
|
|
2506
|
+
boundary,
|
|
2507
|
+
[
|
|
2508
|
+
' if (path.basename(targetPath) === "stale.md") {',
|
|
2509
|
+
' await rename(targetPath, targetPath + "-displaced");',
|
|
2510
|
+
" await mkdir(targetPath);",
|
|
2511
|
+
' await writeFile(path.join(targetPath, "sentinel.txt"), "concurrent directory\\n");',
|
|
2512
|
+
" }",
|
|
2513
|
+
boundary,
|
|
2514
|
+
].join("\n"),
|
|
2515
|
+
);
|
|
2516
|
+
assertThat(source).not.toBe(scriptSource);
|
|
2517
|
+
const root = await writeFixture({
|
|
2518
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2519
|
+
scriptSource: source,
|
|
2520
|
+
});
|
|
2521
|
+
const staleAgent = path.join(root, ".claude/agents/stale.md");
|
|
2522
|
+
const displacedAgent = `${staleAgent}-displaced`;
|
|
2523
|
+
|
|
2524
|
+
try {
|
|
2525
|
+
await mkdir(path.dirname(staleAgent), { recursive: true });
|
|
2526
|
+
await writeFile(
|
|
2527
|
+
staleAgent,
|
|
2528
|
+
"<!-- Generated by scripts/sync-subagents.mjs. Edit .agents/subagents/manifest.mjs instead. -->\n",
|
|
2529
|
+
"utf-8",
|
|
2530
|
+
);
|
|
2531
|
+
await writeFile(
|
|
2532
|
+
path.join(root, ".devpunks/context-plan.json"),
|
|
2533
|
+
'{"version":"1","scopes":[]}\n',
|
|
2534
|
+
);
|
|
2535
|
+
|
|
2536
|
+
await assertThat(
|
|
2537
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2538
|
+
cwd: root,
|
|
2539
|
+
}),
|
|
2540
|
+
).rejects.toThrow();
|
|
2541
|
+
assertThat({
|
|
2542
|
+
displaced: await readFile(displacedAgent, "utf-8"),
|
|
2543
|
+
replacement: await readFile(path.join(staleAgent, "sentinel.txt"), "utf-8"),
|
|
2544
|
+
}).toStrictEqual({
|
|
2545
|
+
displaced:
|
|
2546
|
+
"<!-- Generated by scripts/sync-subagents.mjs. Edit .agents/subagents/manifest.mjs instead. -->\n",
|
|
2547
|
+
replacement: "concurrent directory\n",
|
|
2548
|
+
});
|
|
2549
|
+
} finally {
|
|
2550
|
+
await rm(root, { force: true, recursive: true });
|
|
2551
|
+
}
|
|
2552
|
+
});
|
|
2553
|
+
|
|
2554
|
+
test.each([
|
|
2555
|
+
{
|
|
2556
|
+
kind: "managed agent",
|
|
2557
|
+
recursive: false,
|
|
2558
|
+
target: ".claude/agents/stale.md",
|
|
2559
|
+
writeContextPlan: true,
|
|
2560
|
+
},
|
|
2561
|
+
{
|
|
2562
|
+
kind: "hook mirror",
|
|
2563
|
+
recursive: true,
|
|
2564
|
+
target: ".codex/hooks/format-edited-file.mjs",
|
|
2565
|
+
writeContextPlan: false,
|
|
2566
|
+
},
|
|
2567
|
+
] as const)(
|
|
2568
|
+
"preserves a $kind substituted inside the validated removal window",
|
|
2569
|
+
async ({ recursive, target, writeContextPlan }) => {
|
|
2570
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2571
|
+
const validationBoundary = [
|
|
2572
|
+
' } else if (request.operation === "remove") {',
|
|
2573
|
+
" assertExpectedLeaf(request.target, request.expectedTarget);",
|
|
2574
|
+
].join("\n");
|
|
2575
|
+
const source = scriptSource.replace(
|
|
2576
|
+
validationBoundary,
|
|
2577
|
+
[
|
|
2578
|
+
validationBoundary,
|
|
2579
|
+
` if (request.target === ${JSON.stringify(path.basename(target))} && request.recursive === ${recursive}) {`,
|
|
2580
|
+
' renameSync(request.target, request.target + ".displaced");',
|
|
2581
|
+
" if (request.recursive) {",
|
|
2582
|
+
" mkdirSync(request.target);",
|
|
2583
|
+
' writeFileSync(request.target + "/sentinel.txt", "concurrent directory\\n");',
|
|
2584
|
+
" } else {",
|
|
2585
|
+
' writeFileSync(request.target, "concurrent file\\n");',
|
|
2586
|
+
" }",
|
|
2587
|
+
" }",
|
|
2588
|
+
].join("\n"),
|
|
2589
|
+
);
|
|
2590
|
+
assertThat(source).not.toBe(scriptSource);
|
|
2591
|
+
const root = await writeFixture({
|
|
2592
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2593
|
+
scriptSource: source,
|
|
2594
|
+
writeContextPlan,
|
|
2595
|
+
});
|
|
2596
|
+
const targetPath = path.join(root, target);
|
|
2597
|
+
const displacedPath = `${targetPath}.displaced`;
|
|
2598
|
+
|
|
2599
|
+
try {
|
|
2600
|
+
await mkdir(path.dirname(targetPath), { recursive: true });
|
|
2601
|
+
await writeFile(
|
|
2602
|
+
targetPath,
|
|
2603
|
+
recursive
|
|
2604
|
+
? "obsolete hook mirror\n"
|
|
2605
|
+
: "<!-- Generated by scripts/sync-subagents.mjs. Edit .agents/subagents/manifest.mjs instead. -->\n",
|
|
2606
|
+
"utf-8",
|
|
2607
|
+
);
|
|
2608
|
+
|
|
2609
|
+
await assertThat(
|
|
2610
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2611
|
+
cwd: root,
|
|
2612
|
+
}),
|
|
2613
|
+
).rejects.toThrow();
|
|
2614
|
+
assertThat(
|
|
2615
|
+
recursive
|
|
2616
|
+
? await readFile(path.join(targetPath, "sentinel.txt"), "utf-8")
|
|
2617
|
+
: await readFile(targetPath, "utf-8"),
|
|
2618
|
+
).toBe(recursive ? "concurrent directory\n" : "concurrent file\n");
|
|
2619
|
+
assertThat(await readFile(displacedPath, "utf-8")).toContain(
|
|
2620
|
+
recursive ? "obsolete hook mirror" : "Generated by scripts/sync-subagents.mjs",
|
|
2621
|
+
);
|
|
2622
|
+
const parentEntries = await readdir(path.dirname(targetPath));
|
|
2623
|
+
assertThat(parentEntries.filter((entry) => entry.startsWith(".hi-remove-"))).toEqual([]);
|
|
2624
|
+
} finally {
|
|
2625
|
+
await rm(root, { force: true, recursive: true });
|
|
2626
|
+
}
|
|
2627
|
+
},
|
|
2628
|
+
);
|
|
2629
|
+
|
|
2630
|
+
test("reports the retained claim when failed cleanup collides with target recreation", async () => {
|
|
2631
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2632
|
+
const removalBoundary =
|
|
2633
|
+
" rmSync(request.claim, { force: true, recursive: request.recursive });";
|
|
2634
|
+
const source = scriptSource.replace(
|
|
2635
|
+
removalBoundary,
|
|
2636
|
+
[
|
|
2637
|
+
' if (request.target === "format-edited-file.mjs" && request.recursive === true) {',
|
|
2638
|
+
' writeFileSync(request.target, "concurrent replacement\\n");',
|
|
2639
|
+
" rmSync(request.claim, { force: true, recursive: false });",
|
|
2640
|
+
" }",
|
|
2641
|
+
removalBoundary,
|
|
2642
|
+
].join("\n"),
|
|
2643
|
+
);
|
|
2644
|
+
assertThat(source).not.toBe(scriptSource);
|
|
2645
|
+
const root = await writeFixture({
|
|
2646
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2647
|
+
scriptSource: source,
|
|
2648
|
+
writeContextPlan: false,
|
|
2649
|
+
});
|
|
2650
|
+
const targetPath = path.join(root, ".codex/hooks/format-edited-file.mjs");
|
|
2651
|
+
const originalBytes = Buffer.from("obsolete hook mirror\n");
|
|
2652
|
+
|
|
2653
|
+
try {
|
|
2654
|
+
await mkdir(targetPath, { recursive: true });
|
|
2655
|
+
await writeFile(path.join(targetPath, "original.txt"), originalBytes);
|
|
2656
|
+
|
|
2657
|
+
let failureMessage = "";
|
|
2658
|
+
try {
|
|
2659
|
+
await execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2660
|
+
cwd: root,
|
|
2661
|
+
});
|
|
2662
|
+
} catch (error) {
|
|
2663
|
+
failureMessage = String(error);
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
const parentEntries = await readdir(path.dirname(targetPath));
|
|
2667
|
+
const claimName = requireDefined(
|
|
2668
|
+
parentEntries.find((entry) => entry.startsWith(".hi-remove-")),
|
|
2669
|
+
);
|
|
2670
|
+
const claimPath = path.join(path.dirname(targetPath), claimName);
|
|
2671
|
+
const surfacedClaimPath = await realpath(claimPath);
|
|
2672
|
+
assertThat(await readFile(path.join(claimPath, "original.txt"))).toStrictEqual(originalBytes);
|
|
2673
|
+
assertThat(await readFile(targetPath, "utf-8")).toBe("concurrent replacement\n");
|
|
2674
|
+
assertThat(failureMessage).toContain(
|
|
2675
|
+
`Confined removal could not restore its claimed target. Retained backup: ${surfacedClaimPath}.`,
|
|
2676
|
+
);
|
|
2677
|
+
} finally {
|
|
2678
|
+
await rm(root, { force: true, recursive: true });
|
|
2679
|
+
}
|
|
2680
|
+
});
|
|
2681
|
+
|
|
2682
|
+
test("does not create an outside symlink when a parent swaps after symlink validation", async () => {
|
|
2683
|
+
const outside = await mkdtemp(path.join(os.tmpdir(), "ip317-symlink-swap-outside-"));
|
|
2684
|
+
const scriptSource = await readFile(scriptSourcePath, "utf-8");
|
|
2685
|
+
const source = scriptSource
|
|
2686
|
+
.replace("{ lstat,", "{ lstat, rename, symlink,")
|
|
2687
|
+
.replace(
|
|
2688
|
+
" await runStableMutation(parentDirectory, {\n linkTarget:",
|
|
2689
|
+
[
|
|
2690
|
+
' if (path.basename(parentDirectory) === ".codex" && path.basename(linkPath) === "AGENTS.md") {',
|
|
2691
|
+
' await rename(parentDirectory, parentDirectory + "-displaced");',
|
|
2692
|
+
" await symlink(process.env.HI_TEST_OUTSIDE_PARENT, parentDirectory);",
|
|
2693
|
+
" }",
|
|
2694
|
+
" await runStableMutation(parentDirectory, {\n linkTarget:",
|
|
2695
|
+
].join("\n"),
|
|
2696
|
+
);
|
|
2697
|
+
assertThat(source).not.toBe(scriptSource);
|
|
2698
|
+
const root = await writeFixture({
|
|
2699
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
2700
|
+
scriptSource: source,
|
|
2701
|
+
});
|
|
2702
|
+
|
|
2703
|
+
try {
|
|
2704
|
+
await assertThat(
|
|
2705
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
2706
|
+
cwd: root,
|
|
2707
|
+
env: { ...process.env, HI_TEST_OUTSIDE_PARENT: outside },
|
|
2708
|
+
}),
|
|
2709
|
+
).rejects.toThrow();
|
|
2710
|
+
assertThat(await readdir(outside)).toEqual([]);
|
|
2711
|
+
} finally {
|
|
2712
|
+
await rm(root, { force: true, recursive: true });
|
|
2713
|
+
await rm(outside, { force: true, recursive: true });
|
|
2714
|
+
}
|
|
2715
|
+
});
|
|
2716
|
+
|
|
1530
2717
|
test("preserves consumer prompt files as explicit omissions instead of applied actions", async () => {
|
|
1531
2718
|
const root = await writeFixture({ guidanceFile: "app/frontend/AGENTS.md" });
|
|
1532
2719
|
await mkdir(path.join(root, ".agents/hooks"), { recursive: true });
|
|
@@ -1804,28 +2991,17 @@ suite("sync-subagents script", () => {
|
|
|
1804
2991
|
|
|
1805
2992
|
test("copies prompt mirrors when symlink creation is not permitted", async () => {
|
|
1806
2993
|
const baseSource = await readFile(scriptSourcePath, "utf-8");
|
|
1807
|
-
const scriptSource = baseSource
|
|
1808
|
-
.
|
|
1809
|
-
|
|
1810
|
-
'
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
[" `", "$", "{path.sep}.opencode", "$", "{path.sep}AGENTS.md`,"].join(""),
|
|
1819
|
-
" ];",
|
|
1820
|
-
" if (blocked.some((segment) => linkPath.includes(segment))) {",
|
|
1821
|
-
' const error = new Error("operation not permitted");',
|
|
1822
|
-
' error.code = "EPERM";',
|
|
1823
|
-
" throw error;",
|
|
1824
|
-
" }",
|
|
1825
|
-
" return nativeSymlink(target, linkPath, type);",
|
|
1826
|
-
"};",
|
|
1827
|
-
].join("\n"),
|
|
1828
|
-
);
|
|
2994
|
+
const scriptSource = baseSource.replace(
|
|
2995
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
2996
|
+
[
|
|
2997
|
+
' if (["AGENTS.md", "CLAUDE.md", "skills"].includes(request.target)) {',
|
|
2998
|
+
' const error = new Error("operation not permitted");',
|
|
2999
|
+
' error.code = "EPERM";',
|
|
3000
|
+
" throw error;",
|
|
3001
|
+
" }",
|
|
3002
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3003
|
+
].join("\n"),
|
|
3004
|
+
);
|
|
1829
3005
|
const root = await writeFixture({
|
|
1830
3006
|
guidanceFile: "app/frontend/AGENTS.md",
|
|
1831
3007
|
scriptSource,
|
|
@@ -1849,24 +3025,105 @@ suite("sync-subagents script", () => {
|
|
|
1849
3025
|
);
|
|
1850
3026
|
});
|
|
1851
3027
|
|
|
3028
|
+
test("preserves a target recreated before EPERM copy fallback publication", async () => {
|
|
3029
|
+
const baseSource = await readFile(scriptSourcePath, "utf-8");
|
|
3030
|
+
const withEperm = baseSource.replace(
|
|
3031
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3032
|
+
[
|
|
3033
|
+
' if (request.target === "AGENTS.md") {',
|
|
3034
|
+
' const error = new Error("operation not permitted");',
|
|
3035
|
+
' error.code = "EPERM";',
|
|
3036
|
+
" throw error;",
|
|
3037
|
+
" }",
|
|
3038
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3039
|
+
].join("\n"),
|
|
3040
|
+
);
|
|
3041
|
+
const copyBoundary = [
|
|
3042
|
+
" assertExpectedLeaf(request.target, request.expectedTarget);",
|
|
3043
|
+
" if (request.recursive) {",
|
|
3044
|
+
].join("\n");
|
|
3045
|
+
const scriptSource = withEperm.replace(
|
|
3046
|
+
copyBoundary,
|
|
3047
|
+
[
|
|
3048
|
+
" assertExpectedLeaf(request.target, request.expectedTarget);",
|
|
3049
|
+
' if (request.target === "AGENTS.md") {',
|
|
3050
|
+
' writeFileSync(request.target, "concurrent replacement\\n");',
|
|
3051
|
+
" }",
|
|
3052
|
+
" if (request.recursive) {",
|
|
3053
|
+
].join("\n"),
|
|
3054
|
+
);
|
|
3055
|
+
assertThat(scriptSource).not.toBe(withEperm);
|
|
3056
|
+
const root = await writeFixture({
|
|
3057
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
3058
|
+
scriptSource,
|
|
3059
|
+
writeContextPlan: false,
|
|
3060
|
+
});
|
|
3061
|
+
const targetPath = path.join(root, ".codex/AGENTS.md");
|
|
3062
|
+
|
|
3063
|
+
await assertThat(
|
|
3064
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
3065
|
+
cwd: root,
|
|
3066
|
+
}),
|
|
3067
|
+
).rejects.toThrow();
|
|
3068
|
+
assertThat(await readFile(targetPath, "utf-8")).toBe("concurrent replacement\n");
|
|
3069
|
+
});
|
|
3070
|
+
|
|
3071
|
+
test("preserves a directory recreated before EPERM copy fallback publication", async () => {
|
|
3072
|
+
const baseSource = await readFile(scriptSourcePath, "utf-8");
|
|
3073
|
+
const withEperm = baseSource.replace(
|
|
3074
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3075
|
+
[
|
|
3076
|
+
' if (request.target === "skills") {',
|
|
3077
|
+
' const error = new Error("operation not permitted");',
|
|
3078
|
+
' error.code = "EPERM";',
|
|
3079
|
+
" throw error;",
|
|
3080
|
+
" }",
|
|
3081
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3082
|
+
].join("\n"),
|
|
3083
|
+
);
|
|
3084
|
+
const claimBoundary = [" if (request.recursive) {", " mkdirSync(request.target);"].join(
|
|
3085
|
+
"\n",
|
|
3086
|
+
);
|
|
3087
|
+
const scriptSource = withEperm.replace(
|
|
3088
|
+
claimBoundary,
|
|
3089
|
+
[
|
|
3090
|
+
" if (request.recursive) {",
|
|
3091
|
+
' if (request.target === "skills") {',
|
|
3092
|
+
" mkdirSync(request.target);",
|
|
3093
|
+
' writeFileSync(request.target + "/sentinel.txt", "concurrent replacement\\n");',
|
|
3094
|
+
" }",
|
|
3095
|
+
" mkdirSync(request.target);",
|
|
3096
|
+
].join("\n"),
|
|
3097
|
+
);
|
|
3098
|
+
assertThat(scriptSource).not.toBe(withEperm);
|
|
3099
|
+
const root = await writeFixture({
|
|
3100
|
+
guidanceFile: "app/frontend/AGENTS.md",
|
|
3101
|
+
scriptSource,
|
|
3102
|
+
writeContextPlan: false,
|
|
3103
|
+
});
|
|
3104
|
+
const sentinelPath = path.join(root, ".claude/skills/sentinel.txt");
|
|
3105
|
+
|
|
3106
|
+
await assertThat(
|
|
3107
|
+
execFileAsync("node", [path.join(root, ".agents/scripts/sync-subagents.mjs")], {
|
|
3108
|
+
cwd: root,
|
|
3109
|
+
}),
|
|
3110
|
+
).rejects.toThrow();
|
|
3111
|
+
assertThat(await readFile(sentinelPath, "utf-8")).toBe("concurrent replacement\n");
|
|
3112
|
+
});
|
|
3113
|
+
|
|
1852
3114
|
test("refreshes unchanged EPERM prompt copies but preserves consumer edits", async () => {
|
|
1853
3115
|
const baseSource = await readFile(scriptSourcePath, "utf-8");
|
|
1854
|
-
const scriptSource = baseSource
|
|
1855
|
-
.
|
|
1856
|
-
|
|
1857
|
-
'
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
" }",
|
|
1866
|
-
" return nativeSymlink(target, linkPath, type);",
|
|
1867
|
-
"};",
|
|
1868
|
-
].join("\n"),
|
|
1869
|
-
);
|
|
3116
|
+
const scriptSource = baseSource.replace(
|
|
3117
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3118
|
+
[
|
|
3119
|
+
' if (request.target === "AGENTS.md") {',
|
|
3120
|
+
' const error = new Error("operation not permitted");',
|
|
3121
|
+
' error.code = "EPERM";',
|
|
3122
|
+
" throw error;",
|
|
3123
|
+
" }",
|
|
3124
|
+
" symlinkSync(request.linkTarget, request.target, request.type);",
|
|
3125
|
+
].join("\n"),
|
|
3126
|
+
);
|
|
1870
3127
|
const root = await writeFixture({
|
|
1871
3128
|
guidanceFile: "app/frontend/AGENTS.md",
|
|
1872
3129
|
scriptSource,
|