@memnexus-ai/mx-agent-cli 0.1.272 → 0.1.274
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/__tests__/codex-write-race.test.js +422 -1
- package/dist/__tests__/codex-write-race.test.js.map +1 -1
- package/dist/__tests__/coordination-401-production-wiring.test.d.ts +19 -0
- package/dist/__tests__/coordination-401-production-wiring.test.d.ts.map +1 -0
- package/dist/__tests__/coordination-401-production-wiring.test.js +97 -0
- package/dist/__tests__/coordination-401-production-wiring.test.js.map +1 -0
- package/dist/__tests__/coordination-401-recovery.test.d.ts +22 -0
- package/dist/__tests__/coordination-401-recovery.test.d.ts.map +1 -0
- package/dist/__tests__/coordination-401-recovery.test.js +577 -0
- package/dist/__tests__/coordination-401-recovery.test.js.map +1 -0
- package/dist/__tests__/coordination-brief-pin.test.js +10 -2
- package/dist/__tests__/coordination-brief-pin.test.js.map +1 -1
- package/dist/__tests__/coordination-brief.test.js +9 -1
- package/dist/__tests__/coordination-brief.test.js.map +1 -1
- package/dist/__tests__/coordination-provision.test.js +17 -2
- package/dist/__tests__/coordination-provision.test.js.map +1 -1
- package/dist/__tests__/coordination-token.test.js +24 -0
- package/dist/__tests__/coordination-token.test.js.map +1 -1
- package/dist/agent-config/hooks/coordination-brief.sh +61 -9
- package/dist/commands/coordination.d.ts +40 -6
- package/dist/commands/coordination.d.ts.map +1 -1
- package/dist/commands/coordination.js +222 -10
- package/dist/commands/coordination.js.map +1 -1
- package/dist/lib/coordination-token.d.ts +104 -2
- package/dist/lib/coordination-token.d.ts.map +1 -1
- package/dist/lib/coordination-token.js +223 -42
- package/dist/lib/coordination-token.js.map +1 -1
- package/dist/lib/runtime/codex-config.d.ts.map +1 -1
- package/dist/lib/runtime/codex-config.js +113 -27
- package/dist/lib/runtime/codex-config.js.map +1 -1
- package/package.json +1 -1
|
@@ -55,10 +55,26 @@ const race = vi.hoisted(() => ({
|
|
|
55
55
|
onFdRead: null,
|
|
56
56
|
/** fd → the path it was opened on, so an fd-based read can be attributed. */
|
|
57
57
|
fdPaths: new Map(),
|
|
58
|
+
/**
|
|
59
|
+
* Runs immediately BEFORE a real `closeSync`, with the path behind the descriptor.
|
|
60
|
+
* Throwing makes the `close` fail — the only way to reach the write-back errors `close`
|
|
61
|
+
* reports (ENOSPC, EDQUOT, EIO, ESTALE) without an exhausted or failing filesystem. The
|
|
62
|
+
* descriptor is still closed for real afterwards, so a throwing hook leaks nothing.
|
|
63
|
+
*/
|
|
64
|
+
onClose: null,
|
|
58
65
|
/** Every path the deploy passed to `unlinkSync`, in order. */
|
|
59
66
|
unlinked: [],
|
|
60
67
|
/** Every path the deploy passed to `openSync`, in order. */
|
|
61
68
|
opened: [],
|
|
69
|
+
/**
|
|
70
|
+
* ── THE CALLS THAT BOUND A DESCRIPTOR'S LIFETIME, IN ORDER (#4908) ───────────
|
|
71
|
+
* `open`, `rename`, `lstat` and `close`, each recorded with the PATH behind it. #4908's
|
|
72
|
+
* remediation is not another check — it is a LIFETIME: the temp file's descriptor is
|
|
73
|
+
* held open across the rename and the install check so the kernel cannot recycle the
|
|
74
|
+
* inode NUMBER the check compares. A lifetime has no verdict to assert, and the only
|
|
75
|
+
* filesystem-independent way to observe one is the ORDER of the calls that bound it.
|
|
76
|
+
*/
|
|
77
|
+
timeline: [],
|
|
62
78
|
/** The last temp path opened — the write is fd-based, so it carries no path. */
|
|
63
79
|
lastOpened: '',
|
|
64
80
|
}));
|
|
@@ -73,12 +89,38 @@ vi.mock('fs', async (importOriginal) => {
|
|
|
73
89
|
race.beforeOpen?.(path);
|
|
74
90
|
}
|
|
75
91
|
const fd = actual.openSync(path, flags, mode);
|
|
76
|
-
if (typeof path === 'string')
|
|
92
|
+
if (typeof path === 'string') {
|
|
77
93
|
race.fdPaths.set(fd, path);
|
|
94
|
+
race.timeline.push(`open ${path}`);
|
|
95
|
+
}
|
|
78
96
|
return fd;
|
|
79
97
|
}),
|
|
98
|
+
closeSync: ((fd) => {
|
|
99
|
+
const path = race.fdPaths.get(fd);
|
|
100
|
+
if (path !== undefined)
|
|
101
|
+
race.timeline.push(`close ${path}`);
|
|
102
|
+
if (path !== undefined && race.onClose) {
|
|
103
|
+
// The real `close` still runs, whatever the hook decides — a kernel that reports a
|
|
104
|
+
// write-back error from `close` has released the descriptor all the same, and a
|
|
105
|
+
// test that skipped it would leak one per deploy.
|
|
106
|
+
let failure;
|
|
107
|
+
try {
|
|
108
|
+
race.onClose(path);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
failure = err;
|
|
112
|
+
}
|
|
113
|
+
const result = actual.closeSync(fd);
|
|
114
|
+
if (failure !== undefined)
|
|
115
|
+
throw failure;
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
return actual.closeSync(fd);
|
|
119
|
+
}),
|
|
80
120
|
lstatSync: ((path, options) => {
|
|
81
121
|
const real = actual.lstatSync(path, options);
|
|
122
|
+
if (typeof path === 'string')
|
|
123
|
+
race.timeline.push(`lstat ${path}`);
|
|
82
124
|
if (typeof path === 'string' && race.onLstat) {
|
|
83
125
|
const bigint = Boolean(options?.bigint);
|
|
84
126
|
const replaced = race.onLstat(path, real, bigint);
|
|
@@ -89,6 +131,8 @@ vi.mock('fs', async (importOriginal) => {
|
|
|
89
131
|
}),
|
|
90
132
|
renameSync: ((from, to) => {
|
|
91
133
|
const result = actual.renameSync(from, to);
|
|
134
|
+
if (typeof to === 'string')
|
|
135
|
+
race.timeline.push(`rename ${to}`);
|
|
92
136
|
if (typeof to === 'string')
|
|
93
137
|
race.afterRename?.(to);
|
|
94
138
|
return result;
|
|
@@ -148,8 +192,10 @@ beforeEach(() => {
|
|
|
148
192
|
race.afterRename = null;
|
|
149
193
|
race.onLstat = null;
|
|
150
194
|
race.onFdRead = null;
|
|
195
|
+
race.onClose = null;
|
|
151
196
|
race.unlinked = [];
|
|
152
197
|
race.opened = [];
|
|
198
|
+
race.timeline = [];
|
|
153
199
|
race.fdReads = [];
|
|
154
200
|
race.fdPaths = new Map();
|
|
155
201
|
race.lastOpened = '';
|
|
@@ -176,6 +222,7 @@ afterEach(() => {
|
|
|
176
222
|
race.afterRename = null;
|
|
177
223
|
race.onLstat = null;
|
|
178
224
|
race.onFdRead = null;
|
|
225
|
+
race.onClose = null;
|
|
179
226
|
rmSync(root, { recursive: true, force: true });
|
|
180
227
|
});
|
|
181
228
|
const deploy = () => {
|
|
@@ -1590,6 +1637,380 @@ describe('write path — the acceptance re-open decides on ONE object (round 8)'
|
|
|
1590
1637
|
expect(outcome.writeFailures.join('\n')).toContain('NOT the file this deploy wrote');
|
|
1591
1638
|
});
|
|
1592
1639
|
});
|
|
1640
|
+
/**
|
|
1641
|
+
* ───────── #4908: AN INODE NUMBER IS NOT A DURABLE IDENTITY, AT CHECK 3 ───────
|
|
1642
|
+
* RP14 (above) proved the point against the ACCEPTANCE. RP12 proves it against the PRIMARY
|
|
1643
|
+
* install check, the one every honest deploy is decided by: `landed.dev !== opened.dev ||
|
|
1644
|
+
* landed.ino !== opened.ino`.
|
|
1645
|
+
*
|
|
1646
|
+
* The attack is three syscalls. Between `renameSync` and the verifying `lstatSync`, unlink
|
|
1647
|
+
* the just-installed artifact — nothing holds it once the descriptor is closed, so the
|
|
1648
|
+
* kernel frees its inode number — and create a regular file of attacker-controlled content.
|
|
1649
|
+
* It takes the freed number, and every field the check compares agrees. Measured before the
|
|
1650
|
+
* fix: reuse on attempt 0, `ok=true`, `AGENTS.override.md` listed as WRITTEN, and
|
|
1651
|
+
* `# ATTACKER CONTENT` on disk — a file Codex reads as a REPLACEMENT for `AGENTS.md`. The
|
|
1652
|
+
* reviewer measured 322 of 400 trials in a tighter variant. It reproduced identically
|
|
1653
|
+
* against `origin/main` and shipped in the published CLI: pre-existing, not introduced by
|
|
1654
|
+
* #4897. CVSS 7.1 (AV:L/AC:H/PR:L/UI:N/S:C/C:N/I:H/A:N), CWE-367.
|
|
1655
|
+
*
|
|
1656
|
+
* The fix is a LIFETIME, not a fourth field: the temp file's descriptor is held open across
|
|
1657
|
+
* the rename and the check, which PINS the inode so its number cannot be recycled while the
|
|
1658
|
+
* check runs.
|
|
1659
|
+
*
|
|
1660
|
+
* inode reuse WITHOUT holding an fd open: 200/200
|
|
1661
|
+
* inode reuse WHILE holding an fd open: 0/200
|
|
1662
|
+
*
|
|
1663
|
+
* (`birthtimeNs`/`ctimeNs` were tested as an alternative and rejected: both are
|
|
1664
|
+
* byte-identical across the two creations, because the timestamps are coarser than the race
|
|
1665
|
+
* window.)
|
|
1666
|
+
*
|
|
1667
|
+
* ── WHY THERE ARE TWO TESTS, AND WHY ONE OF THEM IS STRUCTURAL ───────────────
|
|
1668
|
+
* The first test below is the attack, with real syscalls only. It is filesystem-conditional
|
|
1669
|
+
* in the same way RP14 is (round 11 MUST FIX B measured reuse on overlayfs and ext4 and
|
|
1670
|
+
* never on tmpfs) — but the conditionality runs the OTHER way here, and that is worse: on a
|
|
1671
|
+
* filesystem that never recycles, the attack silently fails to land and the test passes
|
|
1672
|
+
* against UNFIXED code. That is exactly the fail-OPEN direction a mutation pin exists to
|
|
1673
|
+
* catch (KI-METHOD-MUTATE-FAIL-OPEN).
|
|
1674
|
+
*
|
|
1675
|
+
* So the second test pins the mechanism directly and lands on any filesystem. It is
|
|
1676
|
+
* structural — it asserts the ORDER of `open`, `rename`, the verifying `lstat` and `close`
|
|
1677
|
+
* — because the remediation adds no check and therefore produces no verdict to assert. A
|
|
1678
|
+
* descriptor's lifetime is observable only as the interval between the two calls that bound
|
|
1679
|
+
* it. Reverting the fix (closing at the write, as the code did before) moves `close` above
|
|
1680
|
+
* `rename` and this test fails, on tmpfs as readily as on overlayfs.
|
|
1681
|
+
*/
|
|
1682
|
+
describe('write path — the installed inode is PINNED across the install check (#4908)', () => {
|
|
1683
|
+
const override = () => join(worktree, AGENTS_OVERRIDE_FILENAME);
|
|
1684
|
+
const ATTACKER = '# ATTACKER CONTENT — not what this deploy generated\n';
|
|
1685
|
+
/**
|
|
1686
|
+
* The temp path this deploy opened for `AGENTS.override.md`, taken from the timeline
|
|
1687
|
+
* rather than guessed: the name carries a pid and six random bytes.
|
|
1688
|
+
*/
|
|
1689
|
+
const tempPathFor = (artifact) => {
|
|
1690
|
+
const opened = race.timeline
|
|
1691
|
+
.filter((entry) => entry.startsWith('open '))
|
|
1692
|
+
.map((entry) => entry.slice('open '.length))
|
|
1693
|
+
.filter((path) => path.includes(`${artifact}.mx-tmp-`));
|
|
1694
|
+
expect(opened).toHaveLength(1);
|
|
1695
|
+
return opened[0];
|
|
1696
|
+
};
|
|
1697
|
+
/** `<artifact>.mx-tmp-<pid>-<hex>` back to `<artifact>` — the destination it renames to. */
|
|
1698
|
+
const absPathOfTemp = (tmpPath) => tmpPath.replace(/\.mx-tmp-\d+-[0-9a-f]+$/, '');
|
|
1699
|
+
/**
|
|
1700
|
+
* RP12, executed. The attacker unlinks the artifact this deploy just installed and
|
|
1701
|
+
* creates regular files until one takes its inode NUMBER back, then renames the winner
|
|
1702
|
+
* over the artifact path. Losing probes are KEPT until the end for the reason RP14 gives:
|
|
1703
|
+
* unlinking each one returns its number to the head of the free list and hands it
|
|
1704
|
+
* straight back, so the target number never surfaces.
|
|
1705
|
+
*
|
|
1706
|
+
* ── WHERE THE ATTACK ACTUALLY RUNS (security review INFO-1) ──────────────────
|
|
1707
|
+
* This docstring used to say the attacker moves "between `renameSync` and the verifying
|
|
1708
|
+
* `lstatSync`". It does not. The `afterRename` hook fires INSIDE the mocked `renameSync`,
|
|
1709
|
+
* before control returns to the write path and before `installed = true` — so what this
|
|
1710
|
+
* test covers is an attacker who wins the race from the instant the directory entry is
|
|
1711
|
+
* replaced, which is the earliest point the installed artifact is reachable by name and
|
|
1712
|
+
* strictly harder for the deploy to survive than the real window. The real rename → `lstat`
|
|
1713
|
+
* window is not reachable from any hook this harness exposes; it is pinned instead by the
|
|
1714
|
+
* deterministic ordering test below (`close` after the verifying `lstat`, mutant M2).
|
|
1715
|
+
*
|
|
1716
|
+
* With the descriptor held open the allocator CANNOT return that number — which is the
|
|
1717
|
+
* assertion — and the attacker's file therefore lands with a different one, fails the
|
|
1718
|
+
* `{dev, ino}` comparison, fails the byte comparison in the benign-race acceptance behind
|
|
1719
|
+
* it, and is reported as a failed write. Both halves are asserted: the number was not
|
|
1720
|
+
* recycled, AND attacker content was never certified as this deploy's output.
|
|
1721
|
+
*/
|
|
1722
|
+
it('MUST FIX: an attacker file that REUSES the installed inode number is NOT reported as written (RP12)', () => {
|
|
1723
|
+
const PROBES = 64;
|
|
1724
|
+
let installedIno = 0n;
|
|
1725
|
+
let finalIno = 0n;
|
|
1726
|
+
let reused = false;
|
|
1727
|
+
let probesTried = 0;
|
|
1728
|
+
race.afterRename = (destination) => {
|
|
1729
|
+
if (!destination.endsWith(AGENTS_OVERRIDE_FILENAME))
|
|
1730
|
+
return;
|
|
1731
|
+
race.afterRename = null;
|
|
1732
|
+
installedIno = lstatSync(destination, { bigint: true }).ino;
|
|
1733
|
+
unlinkSync(destination);
|
|
1734
|
+
const probes = [];
|
|
1735
|
+
let winner = null;
|
|
1736
|
+
for (let i = 0; i < PROBES && winner === null; i++) {
|
|
1737
|
+
const probe = `${destination}.ino-probe-${i}`;
|
|
1738
|
+
writeFileSync(probe, ATTACKER);
|
|
1739
|
+
chmodSync(probe, 0o644);
|
|
1740
|
+
probes.push(probe);
|
|
1741
|
+
probesTried = i + 1;
|
|
1742
|
+
if (lstatSync(probe, { bigint: true }).ino === installedIno)
|
|
1743
|
+
winner = probe;
|
|
1744
|
+
}
|
|
1745
|
+
if (winner !== null) {
|
|
1746
|
+
renameSync(winner, destination);
|
|
1747
|
+
reused = true;
|
|
1748
|
+
}
|
|
1749
|
+
else {
|
|
1750
|
+
// The allocator did not hand the number back. The attacker still installs the
|
|
1751
|
+
// content — the deploy must refuse it either way, and this keeps the on-disk
|
|
1752
|
+
// state identical in both arms so only the INODE differs between them.
|
|
1753
|
+
writeFileSync(destination, ATTACKER);
|
|
1754
|
+
chmodSync(destination, 0o644);
|
|
1755
|
+
}
|
|
1756
|
+
for (const probe of probes)
|
|
1757
|
+
if (probe !== winner)
|
|
1758
|
+
unlinkSync(probe);
|
|
1759
|
+
finalIno = lstatSync(destination, { bigint: true }).ino;
|
|
1760
|
+
};
|
|
1761
|
+
const outcome = deploy();
|
|
1762
|
+
race.afterRename = null;
|
|
1763
|
+
// The attack was pressed all the way home: attacker content really is at the artifact
|
|
1764
|
+
// path, so the only question left is what the deploy SAID about it.
|
|
1765
|
+
expect(readFileSync(override(), 'utf-8')).toBe(ATTACKER);
|
|
1766
|
+
// The property #4908 buys, stated directly: an open descriptor pins the inode, so the
|
|
1767
|
+
// number this deploy installed was not available to any of the attacker's probes.
|
|
1768
|
+
expect(reused).toBe(false);
|
|
1769
|
+
expect(finalIno).not.toBe(installedIno);
|
|
1770
|
+
expect(probesTried).toBe(PROBES);
|
|
1771
|
+
// And the consequence: no false green. Before the fix this deploy reported ok=true with
|
|
1772
|
+
// the artifact listed as written.
|
|
1773
|
+
expect(outcome.ok).toBe(false);
|
|
1774
|
+
expect(outcome.written).not.toContain(AGENTS_OVERRIDE_FILENAME);
|
|
1775
|
+
expect(outcome.writeFailures.join('\n')).toContain('NOT the file this deploy wrote');
|
|
1776
|
+
});
|
|
1777
|
+
/**
|
|
1778
|
+
* The deterministic half. No attacker, no allocator, no filesystem dependency: an
|
|
1779
|
+
* undisturbed deploy, asserting that the descriptor it opened for the temp file is still
|
|
1780
|
+
* open when the install check reads the path — `open` → `rename` → the verifying `lstat`
|
|
1781
|
+
* → `close`, in that order.
|
|
1782
|
+
*
|
|
1783
|
+
* The verifying `lstat` is identified as the first `lstat` of the DESTINATION after the
|
|
1784
|
+
* rename; the walks before the rename lstat the same name, which is why the index is
|
|
1785
|
+
* taken relative to the rename rather than absolutely.
|
|
1786
|
+
*
|
|
1787
|
+
* ── THIS IS THE SOLE GUARD AGAINST M2, AND IT COVERS ALL THREE ARTIFACTS ─────
|
|
1788
|
+
* An independent QA mutation pass found that moving the `close` to AFTER the rename but
|
|
1789
|
+
* BEFORE the verifying `lstat` (mutant M2) survives RP12 above. It has to: RP12's attacker
|
|
1790
|
+
* runs from the `afterRename` hook, which fires INSIDE the mocked `renameSync` — earlier
|
|
1791
|
+
* than the point M2 moves the `close` to — so the descriptor is still open when the probes
|
|
1792
|
+
* run in both the mutated and unmutated builds. Nothing else in this file observes the
|
|
1793
|
+
* rename → `lstat` window. Deleting or weakening the `iClose > iVerify` assertion below
|
|
1794
|
+
* therefore drops #4908's regression guard silently, with the suite still green.
|
|
1795
|
+
*
|
|
1796
|
+
* So the ordering is asserted for EVERY artifact this deploy writes, not just
|
|
1797
|
+
* `AGENTS.override.md`: M2 applied to any one of the three is caught.
|
|
1798
|
+
*/
|
|
1799
|
+
it('MUST FIX: the temp descriptor is still open when the install check reads the path (#4908)', () => {
|
|
1800
|
+
expect(deploy().ok).toBe(true);
|
|
1801
|
+
for (const artifact of [
|
|
1802
|
+
AGENTS_OVERRIDE_FILENAME,
|
|
1803
|
+
CODEX_CONFIG_FILENAME,
|
|
1804
|
+
CODEX_HOOKS_FILENAME,
|
|
1805
|
+
]) {
|
|
1806
|
+
const tmpPath = tempPathFor(artifact);
|
|
1807
|
+
const destination = absPathOfTemp(tmpPath);
|
|
1808
|
+
const iOpen = race.timeline.indexOf(`open ${tmpPath}`);
|
|
1809
|
+
const iRename = race.timeline.indexOf(`rename ${destination}`);
|
|
1810
|
+
const iClose = race.timeline.indexOf(`close ${tmpPath}`);
|
|
1811
|
+
const iVerify = race.timeline.findIndex((entry, index) => index > iRename && entry === `lstat ${destination}`);
|
|
1812
|
+
expect(iOpen).toBeGreaterThanOrEqual(0);
|
|
1813
|
+
expect(iRename).toBeGreaterThan(iOpen);
|
|
1814
|
+
expect(iVerify).toBeGreaterThan(iRename);
|
|
1815
|
+
// M2's pin, and the reason for the message: the verifying `lstat` — the one whose
|
|
1816
|
+
// `{dev, ino}` the install check compares — must run BEFORE the descriptor is closed.
|
|
1817
|
+
// Closing at the write, where this code closed before #4908, puts `close` above the
|
|
1818
|
+
// `rename`; closing between the rename and the check (M2) leaves the inode number
|
|
1819
|
+
// free for exactly the window the check occupies. Both are refused here.
|
|
1820
|
+
expect(iClose, `${artifact}: close ran before the verifying lstat, so the inode was unpinned for the check`).toBeGreaterThan(iVerify);
|
|
1821
|
+
}
|
|
1822
|
+
});
|
|
1823
|
+
/**
|
|
1824
|
+
* The cost of moving a `close` into an outer `finally` is a descriptor leak if any path
|
|
1825
|
+
* out of the function misses it. Both paths are checked: a clean deploy, and a deploy
|
|
1826
|
+
* whose install check FAILS (the SF-I symlink substitution, which throws from the middle
|
|
1827
|
+
* of the region the descriptor is now held across).
|
|
1828
|
+
*/
|
|
1829
|
+
it('every temp descriptor this deploy opens is closed exactly once — honest path', () => {
|
|
1830
|
+
expect(deploy().ok).toBe(true);
|
|
1831
|
+
const temps = race.timeline
|
|
1832
|
+
.filter((entry) => entry.startsWith('open ') && entry.includes('.mx-tmp-'))
|
|
1833
|
+
.map((entry) => entry.slice('open '.length));
|
|
1834
|
+
expect(temps.length).toBe(3);
|
|
1835
|
+
for (const tmp of temps) {
|
|
1836
|
+
expect(race.timeline.filter((entry) => entry === `close ${tmp}`)).toHaveLength(1);
|
|
1837
|
+
}
|
|
1838
|
+
});
|
|
1839
|
+
it('every temp descriptor this deploy opens is closed exactly once — failed install', () => {
|
|
1840
|
+
const decoy = join(root, 'decoy.md');
|
|
1841
|
+
writeFileSync(decoy, ATTACKER);
|
|
1842
|
+
race.afterWrite = (tmpPath) => {
|
|
1843
|
+
if (!tmpPath.includes(`${AGENTS_OVERRIDE_FILENAME}.mx-tmp-`))
|
|
1844
|
+
return;
|
|
1845
|
+
race.afterWrite = null;
|
|
1846
|
+
// The SF-I substitution: the temp path is replaced with a symlink between the write
|
|
1847
|
+
// and the rename, so `renameSync` installs the attacker's entry and the install
|
|
1848
|
+
// check throws — from inside the region the descriptor is now held across.
|
|
1849
|
+
unlinkSync(tmpPath);
|
|
1850
|
+
symlinkSync(decoy, tmpPath);
|
|
1851
|
+
};
|
|
1852
|
+
const outcome = deploy();
|
|
1853
|
+
race.afterWrite = null;
|
|
1854
|
+
expect(outcome.ok).toBe(false);
|
|
1855
|
+
expect(outcome.writeFailures.join('\n')).toContain('a SYMLINK');
|
|
1856
|
+
const temps = race.timeline
|
|
1857
|
+
.filter((entry) => entry.startsWith('open ') && entry.includes('.mx-tmp-'))
|
|
1858
|
+
.map((entry) => entry.slice('open '.length));
|
|
1859
|
+
expect(temps.length).toBeGreaterThan(0);
|
|
1860
|
+
for (const tmp of temps) {
|
|
1861
|
+
expect(race.timeline.filter((entry) => entry === `close ${tmp}`)).toHaveLength(1);
|
|
1862
|
+
}
|
|
1863
|
+
});
|
|
1864
|
+
/**
|
|
1865
|
+
* ── THE `close` ITSELF MUST STILL BE ABLE TO FAIL THE WRITE (#4908 round 2) ───
|
|
1866
|
+
* Moving the `close` into an outer `finally` came with a `catch {}` that suppressed EVERY
|
|
1867
|
+
* `close` error. That was a fail-closed → fail-open REGRESSION: before #4908 the `close`
|
|
1868
|
+
* sat in an inner `finally` with no `catch`, so a failure PROPAGATED and the artifact was
|
|
1869
|
+
* reported as a FAILED write. Measured on the first version of the fix, with `closeSync`
|
|
1870
|
+
* forced to EIO on `.mx-tmp-` descriptors — FIXED: `ok=true`, all three artifacts listed
|
|
1871
|
+
* as WRITTEN, no failures; PARENT commit: `ok=false`, nothing written, and a failure
|
|
1872
|
+
* naming the EIO. And mutating production `closeSync` to throw unconditionally passed all
|
|
1873
|
+
* 55 tests in this file — nothing observed it at all.
|
|
1874
|
+
*
|
|
1875
|
+
* `close` is where DEFERRED WRITE-BACK errors surface (ENOSPC, EDQUOT, EIO, ESTALE, and
|
|
1876
|
+
* on Linux any error recorded against the file since the open), so suppressing it on the
|
|
1877
|
+
* SUCCESS path means the deploy's own bytes never reached the disk and the deploy said
|
|
1878
|
+
* the artifact was written. `AGENTS.override.md` REPLACES `AGENTS.md`, so the end state is
|
|
1879
|
+
* a worktree launching on a truncated or empty instruction file, certified green.
|
|
1880
|
+
*
|
|
1881
|
+
* This is the test that kills the always-throw mutation.
|
|
1882
|
+
*/
|
|
1883
|
+
it('MUST FIX: a `close` that FAILS on the honest path is a FAILED write, not a written one', () => {
|
|
1884
|
+
let closesFailed = 0;
|
|
1885
|
+
race.onClose = (path) => {
|
|
1886
|
+
if (!path.includes(`${AGENTS_OVERRIDE_FILENAME}.mx-tmp-`))
|
|
1887
|
+
return;
|
|
1888
|
+
closesFailed += 1;
|
|
1889
|
+
const err = new Error('EIO: i/o error, close');
|
|
1890
|
+
err.code = 'EIO';
|
|
1891
|
+
throw err;
|
|
1892
|
+
};
|
|
1893
|
+
const outcome = deploy();
|
|
1894
|
+
race.onClose = null;
|
|
1895
|
+
// Nothing else went wrong: the rename landed, the install check passed, and the ONLY
|
|
1896
|
+
// thing that failed is the `close`. So this asserts the `close` alone can fail a write.
|
|
1897
|
+
expect(closesFailed).toBe(1);
|
|
1898
|
+
expect(existsSync(override())).toBe(true);
|
|
1899
|
+
expect(outcome.ok).toBe(false);
|
|
1900
|
+
expect(outcome.written).not.toContain(AGENTS_OVERRIDE_FILENAME);
|
|
1901
|
+
const failure = outcome.writeFailures.join('\n');
|
|
1902
|
+
expect(failure).toContain('could not be closed');
|
|
1903
|
+
expect(failure).toContain('EIO');
|
|
1904
|
+
expect(failure).toContain('Treating the write as FAILED');
|
|
1905
|
+
// The report is MIXED-STATE, not all-or-nothing: the two artifacts whose descriptors
|
|
1906
|
+
// closed cleanly are still reported as written (#4896/#4897).
|
|
1907
|
+
expect(outcome.written).toContain(CODEX_CONFIG_FILENAME);
|
|
1908
|
+
expect(outcome.written).toContain(CODEX_HOOKS_FILENAME);
|
|
1909
|
+
});
|
|
1910
|
+
/**
|
|
1911
|
+
* The other half of the same rule, and the reason the suppression exists at all. On a
|
|
1912
|
+
* FAILURE path an error is already propagating, and an exception thrown from a `finally`
|
|
1913
|
+
* REPLACES it — so rethrowing a `close` failure there would discard the refusal text
|
|
1914
|
+
* naming what is really on disk, which is the mixed-state lie #4896/#4897 exist to remove.
|
|
1915
|
+
*
|
|
1916
|
+
* Both things fail at once here: the SF-I symlink substitution makes the install check
|
|
1917
|
+
* throw, AND the `close` of that same descriptor fails. The report must be the SYMLINK.
|
|
1918
|
+
*/
|
|
1919
|
+
it('a `close` that fails on a FAILURE path does not replace the refusal that is propagating', () => {
|
|
1920
|
+
const decoy = join(root, 'decoy.md');
|
|
1921
|
+
writeFileSync(decoy, ATTACKER);
|
|
1922
|
+
let closesFailed = 0;
|
|
1923
|
+
race.afterWrite = (tmpPath) => {
|
|
1924
|
+
if (!tmpPath.includes(`${AGENTS_OVERRIDE_FILENAME}.mx-tmp-`))
|
|
1925
|
+
return;
|
|
1926
|
+
race.afterWrite = null;
|
|
1927
|
+
unlinkSync(tmpPath);
|
|
1928
|
+
symlinkSync(decoy, tmpPath);
|
|
1929
|
+
};
|
|
1930
|
+
race.onClose = (path) => {
|
|
1931
|
+
if (!path.includes(`${AGENTS_OVERRIDE_FILENAME}.mx-tmp-`))
|
|
1932
|
+
return;
|
|
1933
|
+
closesFailed += 1;
|
|
1934
|
+
const err = new Error('EIO: i/o error, close');
|
|
1935
|
+
err.code = 'EIO';
|
|
1936
|
+
throw err;
|
|
1937
|
+
};
|
|
1938
|
+
const outcome = deploy();
|
|
1939
|
+
race.afterWrite = null;
|
|
1940
|
+
race.onClose = null;
|
|
1941
|
+
expect(closesFailed).toBe(1);
|
|
1942
|
+
expect(outcome.ok).toBe(false);
|
|
1943
|
+
expect(outcome.written).not.toContain(AGENTS_OVERRIDE_FILENAME);
|
|
1944
|
+
const failure = outcome.writeFailures.join('\n');
|
|
1945
|
+
// The ORIGINAL refusal survives, naming what is really at the artifact path…
|
|
1946
|
+
expect(failure).toContain('a SYMLINK');
|
|
1947
|
+
expect(failure).toContain('NOT the file this deploy wrote');
|
|
1948
|
+
// …and is not overwritten by the `close` failure that happened on the way out.
|
|
1949
|
+
expect(failure).not.toContain('could not be closed');
|
|
1950
|
+
});
|
|
1951
|
+
/**
|
|
1952
|
+
* ── M3: HALF OF A TWO-FIELD IDENTITY IS NOT AN IDENTITY (coverage gap) ───────
|
|
1953
|
+
* An independent QA mutation pass found that weakening the primary install check to
|
|
1954
|
+
* `landed.ino !== opened.ino` — dropping the `landed.dev !== opened.dev` half — SURVIVED
|
|
1955
|
+
* every one of the five tests #4908 added. It was caught only incidentally, by the
|
|
1956
|
+
* pre-existing round-8 test `SF-C: an open descriptor on another DEVICE with the same
|
|
1957
|
+
* inode number is refused`, which lives in a different describe block and could be deleted
|
|
1958
|
+
* or rewritten without anyone noticing that #4908's regression guard went with it.
|
|
1959
|
+
*
|
|
1960
|
+
* It matters HERE because `dev` is what the pinned descriptor is compared on. The whole
|
|
1961
|
+
* point of holding the fd open is that `{dev, ino}` names the object this deploy installed;
|
|
1962
|
+
* an inode number on its own is exactly the non-identity #4908 exists to stop trusting,
|
|
1963
|
+
* and every other substitution in this file also changes the number, so `ino` covers for
|
|
1964
|
+
* `dev` in all of them. This is the one state it cannot: same number, different device.
|
|
1965
|
+
*/
|
|
1966
|
+
it('M3: an entry with the installed inode NUMBER on a different DEVICE is not reported as written', () => {
|
|
1967
|
+
let fired = false;
|
|
1968
|
+
let realDev = 0n;
|
|
1969
|
+
let realIno = 0n;
|
|
1970
|
+
race.afterRename = (destination) => {
|
|
1971
|
+
if (!destination.endsWith(AGENTS_OVERRIDE_FILENAME))
|
|
1972
|
+
return;
|
|
1973
|
+
race.afterRename = null;
|
|
1974
|
+
race.onLstat = (p, real, bigint) => {
|
|
1975
|
+
if (fired || p !== destination || !bigint)
|
|
1976
|
+
return undefined;
|
|
1977
|
+
fired = true;
|
|
1978
|
+
realDev = real.dev;
|
|
1979
|
+
realIno = real.ino;
|
|
1980
|
+
return statReporting(real, { dev: realDev + 1n });
|
|
1981
|
+
};
|
|
1982
|
+
};
|
|
1983
|
+
const outcome = deploy();
|
|
1984
|
+
race.onLstat = null;
|
|
1985
|
+
expect(fired).toBe(true);
|
|
1986
|
+
// The `ino` half AGREES — nothing moved the file — so only the `dev` half can refuse.
|
|
1987
|
+
const now = lstatSync(override(), { bigint: true });
|
|
1988
|
+
expect(now.ino).toBe(realIno);
|
|
1989
|
+
expect(now.dev).toBe(realDev);
|
|
1990
|
+
expect(outcome.ok).toBe(false);
|
|
1991
|
+
expect(outcome.written).not.toContain(AGENTS_OVERRIDE_FILENAME);
|
|
1992
|
+
expect(outcome.writeFailures.join('\n')).toContain('NOT the file this deploy wrote');
|
|
1993
|
+
});
|
|
1994
|
+
/**
|
|
1995
|
+
* The honest path is unchanged. Holding a descriptor open changes nothing an operator or
|
|
1996
|
+
* a second deploy can observe: the artifacts are written, with the right bytes and the
|
|
1997
|
+
* right mode, and the benign concurrent-redeploy acceptance (SF-L) still accepts.
|
|
1998
|
+
*/
|
|
1999
|
+
it('an undisturbed deploy still writes all three artifacts, unchanged', () => {
|
|
2000
|
+
const outcome = deploy();
|
|
2001
|
+
expect(outcome.ok).toBe(true);
|
|
2002
|
+
expect(outcome.written).toContain(AGENTS_OVERRIDE_FILENAME);
|
|
2003
|
+
expect(outcome.written).toContain(CODEX_CONFIG_FILENAME);
|
|
2004
|
+
expect(outcome.written).toContain(CODEX_HOOKS_FILENAME);
|
|
2005
|
+
expect(outcome.writeFailures).toEqual([]);
|
|
2006
|
+
expect(outcome.writeRefusals).toEqual([]);
|
|
2007
|
+
expect(lstatSync(override()).isFile()).toBe(true);
|
|
2008
|
+
expect(lstatSync(override()).mode & 0o777).toBe(0o644);
|
|
2009
|
+
expect(readdirSync(join(worktree, '.codex')).filter((e) => e.includes('.mx-tmp-'))).toEqual([]);
|
|
2010
|
+
// The deploy is repeatable: a redeploy over its own output succeeds too.
|
|
2011
|
+
expect(deploy().ok).toBe(true);
|
|
2012
|
+
});
|
|
2013
|
+
});
|
|
1593
2014
|
/**
|
|
1594
2015
|
* ─────── SF-K (round 7): THE RESIDUAL IS DECLARED, SO IT MUST BE FINDABLE ─────
|
|
1595
2016
|
* The residual this write path cannot close: `<worktree>/.codex` is swapped for a symlink
|