@memnexus-ai/mx-agent-cli 0.1.265 → 0.1.266
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__/runtime-codex-adapter.test.js +18 -5
- package/dist/__tests__/runtime-codex-adapter.test.js.map +1 -1
- package/dist/__tests__/runtime-codex-config.test.js +133 -12
- package/dist/__tests__/runtime-codex-config.test.js.map +1 -1
- package/dist/__tests__/runtime-codex-guard-proof.test.d.ts +2 -0
- package/dist/__tests__/runtime-codex-guard-proof.test.d.ts.map +1 -0
- package/dist/__tests__/runtime-codex-guard-proof.test.js +747 -0
- package/dist/__tests__/runtime-codex-guard-proof.test.js.map +1 -0
- package/dist/__tests__/start-runtime-wiring.test.js +91 -2
- package/dist/__tests__/start-runtime-wiring.test.js.map +1 -1
- package/dist/commands/codex-prove-guards.d.ts +62 -0
- package/dist/commands/codex-prove-guards.d.ts.map +1 -0
- package/dist/commands/codex-prove-guards.js +353 -0
- package/dist/commands/codex-prove-guards.js.map +1 -0
- package/dist/commands/start.d.ts.map +1 -1
- package/dist/commands/start.js +27 -1
- package/dist/commands/start.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/runtime/codex-adapter.d.ts +42 -2
- package/dist/lib/runtime/codex-adapter.d.ts.map +1 -1
- package/dist/lib/runtime/codex-adapter.js +81 -9
- package/dist/lib/runtime/codex-adapter.js.map +1 -1
- package/dist/lib/runtime/codex-config.d.ts +53 -4
- package/dist/lib/runtime/codex-config.d.ts.map +1 -1
- package/dist/lib/runtime/codex-config.js +164 -15
- package/dist/lib/runtime/codex-config.js.map +1 -1
- package/dist/lib/runtime/codex-guard-proof.d.ts +127 -0
- package/dist/lib/runtime/codex-guard-proof.d.ts.map +1 -0
- package/dist/lib/runtime/codex-guard-proof.js +372 -0
- package/dist/lib/runtime/codex-guard-proof.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The G-7 proof artifact and its producer (#4864, #4851).
|
|
3
|
+
*
|
|
4
|
+
* WHAT THESE TESTS ARE FOR. `provenGuards` is the difference between "codex launches
|
|
5
|
+
* with no guard enforced" and "codex launches". Before #4864 it was permanently empty
|
|
6
|
+
* because nothing wrote it; the risk of writing it is the mirror image — a proof that
|
|
7
|
+
* is accepted when it should not be. So the bulk of this file is refusals: every way an
|
|
8
|
+
* artifact can be absent, malformed, foreign, out of date or unbacked by trust reads as
|
|
9
|
+
* NOTHING PROVEN, which leaves the launch refusal exactly as it was.
|
|
10
|
+
*
|
|
11
|
+
* MUTATION CHECKS (each one verified to fail the named test when applied):
|
|
12
|
+
* - drop the `currentSha !== artifact.hooksSha256` comparison in readGuardProof
|
|
13
|
+
* → 'a proof of a DIFFERENT hooks.json is stale, not proof' fails.
|
|
14
|
+
* - accept a `permittedExitCode` other than 0 in validateGuardProofShape
|
|
15
|
+
* → 'an artifact whose permitted half was refused is not a proof' fails.
|
|
16
|
+
* - drop the attribution check in proveCodexGuards
|
|
17
|
+
* → 'exit 2 from the WRAPPER, not the guard, is not a proof' fails.
|
|
18
|
+
* - drop the trust check in readGuardProof
|
|
19
|
+
* → 'trust revoked after the proof reads as not proven' fails.
|
|
20
|
+
*/
|
|
21
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
22
|
+
import { createHash } from 'crypto';
|
|
23
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'fs';
|
|
24
|
+
import { join } from 'path';
|
|
25
|
+
import { tmpdir } from 'os';
|
|
26
|
+
import { CODEX_HOOKS_FILENAME, PHASE1_PORTED_GUARDS, VERIFIED_CODEX_VERSION, buildCodexHooksJson, codexHookTrustKeysForDoc, readDeployedHooksDoc, } from '../lib/runtime/codex-config.js';
|
|
27
|
+
import { GUARD_PROOF_FILENAME, GUARD_PROOF_VERSION, readGuardProof, runningCliVersion, serializeGuardProof, validateGuardProofShape, writeGuardProof, } from '../lib/runtime/codex-guard-proof.js';
|
|
28
|
+
import { CodexAdapter } from '../lib/runtime/codex-adapter.js';
|
|
29
|
+
import { proveCodexGuards } from '../commands/codex-prove-guards.js';
|
|
30
|
+
const NOW = '2026-08-02T00:00:00Z';
|
|
31
|
+
const CLI_ROOT = join(import.meta.dirname, '..', '..');
|
|
32
|
+
/**
|
|
33
|
+
* Where the real guard scripts come from.
|
|
34
|
+
*
|
|
35
|
+
* TRACKED first, generated second — and the order is the whole point. These tests read
|
|
36
|
+
* `<repo>/.claude/hooks/` only, which exists in every agent worktree because the config
|
|
37
|
+
* generator deploys it there. It is also GITIGNORED (`.gitignore:20` — `/.claude/`), so
|
|
38
|
+
* a clean CI checkout does not have it: all 58 tests in this file threw
|
|
39
|
+
* `ENOENT … copyfile '…/.claude/hooks/git-mutation-guard.sh'` on the #4871 merge commit
|
|
40
|
+
* and turned main red, after passing locally, in QA and in security review — every one
|
|
41
|
+
* of those ran inside a worktree where the generated directory was present.
|
|
42
|
+
*
|
|
43
|
+
* `mx-agent-system/agent-config/hooks/` is the tracked SOURCE the generator copies from,
|
|
44
|
+
* so it is present in any checkout. `.claude/hooks/` stays as a fallback for the case
|
|
45
|
+
* where the source directory moves without this constant moving with it; it is not the
|
|
46
|
+
* preferred path and CI must never depend on it.
|
|
47
|
+
*/
|
|
48
|
+
const TRACKED_HOOKS = join(CLI_ROOT, '..', 'agent-config', 'hooks');
|
|
49
|
+
const GENERATED_HOOKS = join(CLI_ROOT, '..', '..', '.claude', 'hooks');
|
|
50
|
+
/** First readable source for `script`, or a thrown error naming both places tried. */
|
|
51
|
+
function guardSourcePath(script) {
|
|
52
|
+
for (const dir of [TRACKED_HOOKS, GENERATED_HOOKS]) {
|
|
53
|
+
const candidate = join(dir, script);
|
|
54
|
+
if (existsSync(candidate))
|
|
55
|
+
return candidate;
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`guard script ${script} not found in the tracked source ${TRACKED_HOOKS} nor in the ` +
|
|
58
|
+
`generated ${GENERATED_HOOKS}. The tracked directory is the one CI has; if it moved, ` +
|
|
59
|
+
`update TRACKED_HOOKS rather than relying on the generated copy.`);
|
|
60
|
+
}
|
|
61
|
+
let worktree;
|
|
62
|
+
let codexHome;
|
|
63
|
+
/** Copy the real guard scripts in — the reader now hashes them (review MF1). */
|
|
64
|
+
function deployGuardScripts() {
|
|
65
|
+
mkdirSync(join(worktree, '.claude', 'hooks'), { recursive: true });
|
|
66
|
+
for (const script of PHASE1_PORTED_GUARDS) {
|
|
67
|
+
copyFileSync(guardSourcePath(script), join(worktree, '.claude', 'hooks', script));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function scriptSha(guard) {
|
|
71
|
+
return createHash('sha256')
|
|
72
|
+
.update(readFileSync(join(worktree, '.claude', 'hooks', `${guard}.sh`)))
|
|
73
|
+
.digest('hex');
|
|
74
|
+
}
|
|
75
|
+
/** Deploy the hooks file exactly as `mx-agent start --runtime codex` would. */
|
|
76
|
+
function deployHooks() {
|
|
77
|
+
mkdirSync(join(worktree, '.codex'), { recursive: true });
|
|
78
|
+
writeFileSync(join(worktree, CODEX_HOOKS_FILENAME), buildCodexHooksJson({ worktreePath: worktree }));
|
|
79
|
+
}
|
|
80
|
+
function hooksSha() {
|
|
81
|
+
return createHash('sha256').update(readFileSync(join(worktree, CODEX_HOOKS_FILENAME))).digest('hex');
|
|
82
|
+
}
|
|
83
|
+
/** Grant hook trust for every entry of the deployed file, plus project trust. */
|
|
84
|
+
function grantTrust() {
|
|
85
|
+
const hooksPath = join(worktree, CODEX_HOOKS_FILENAME);
|
|
86
|
+
const keys = codexHookTrustKeysForDoc(hooksPath, readDeployedHooksDoc(worktree));
|
|
87
|
+
mkdirSync(codexHome, { recursive: true });
|
|
88
|
+
writeFileSync(join(codexHome, 'config.toml'), `[projects."${worktree}"]\ntrust_level = "trusted"\n\n` +
|
|
89
|
+
keys.map((k) => `[hooks.state."${k}"]\nenabled = true\ntrusted_hash = "sha256:aa"\n`).join('\n'));
|
|
90
|
+
}
|
|
91
|
+
function validArtifact(over = {}) {
|
|
92
|
+
return {
|
|
93
|
+
version: GUARD_PROOF_VERSION,
|
|
94
|
+
hooksPath: join(worktree, CODEX_HOOKS_FILENAME),
|
|
95
|
+
hooksSha256: hooksSha(),
|
|
96
|
+
provenAt: NOW,
|
|
97
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
98
|
+
cliVersion: runningCliVersion(),
|
|
99
|
+
guards: [
|
|
100
|
+
{
|
|
101
|
+
guard: 'git-mutation-guard',
|
|
102
|
+
scriptPath: '.claude/hooks/git-mutation-guard.sh',
|
|
103
|
+
scriptSha256: scriptSha('git-mutation-guard'),
|
|
104
|
+
refusedStimulus: 'git reset --hard HEAD',
|
|
105
|
+
refusedExitCode: 2,
|
|
106
|
+
refusalAttribution: 'BLOCKED: destructive git operation',
|
|
107
|
+
permittedStimulus: 'git status --short',
|
|
108
|
+
permittedExitCode: 0,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
guard: 'named-memory-write-guard',
|
|
112
|
+
scriptPath: '.claude/hooks/named-memory-write-guard.sh',
|
|
113
|
+
scriptSha256: scriptSha('named-memory-write-guard'),
|
|
114
|
+
refusedStimulus: 'mx memories update --name x --content "y"',
|
|
115
|
+
refusedExitCode: 2,
|
|
116
|
+
refusalAttribution: 'BLOCKED: truncating write to named memory',
|
|
117
|
+
permittedStimulus: 'mx memories update --name x --content "…"',
|
|
118
|
+
permittedExitCode: 0,
|
|
119
|
+
stubbedDependency: 'mx memories get',
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
spawnGate: {
|
|
123
|
+
refusedExitCode: 2,
|
|
124
|
+
refusalAttribution: 'subagent spawning is unavailable under codex',
|
|
125
|
+
unrelatedToolExitCode: 0,
|
|
126
|
+
},
|
|
127
|
+
...over,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Write raw bytes to the artifact path, bypassing validation. */
|
|
131
|
+
function writeRaw(text) {
|
|
132
|
+
mkdirSync(join(worktree, '.codex'), { recursive: true });
|
|
133
|
+
writeFileSync(join(worktree, GUARD_PROOF_FILENAME), text);
|
|
134
|
+
}
|
|
135
|
+
const read = () => readGuardProof({ worktreePath: worktree, codexHome });
|
|
136
|
+
beforeEach(() => {
|
|
137
|
+
worktree = mkdtempSync(join(tmpdir(), 'mx-proof-wt-'));
|
|
138
|
+
codexHome = join(worktree, 'fake-codex-home');
|
|
139
|
+
deployHooks();
|
|
140
|
+
deployGuardScripts();
|
|
141
|
+
grantTrust();
|
|
142
|
+
});
|
|
143
|
+
afterEach(() => {
|
|
144
|
+
rmSync(worktree, { recursive: true, force: true });
|
|
145
|
+
});
|
|
146
|
+
describe('readGuardProof — every unreadable state is NOTHING PROVEN', () => {
|
|
147
|
+
it('a valid, current artifact proves exactly the guards it names', () => {
|
|
148
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
149
|
+
const status = read();
|
|
150
|
+
expect(status.valid).toBe(true);
|
|
151
|
+
expect(status.provenGuards.sort()).toEqual(['git-mutation-guard', 'named-memory-write-guard']);
|
|
152
|
+
// ATTRIBUTED, not asserted (review SF1): the line names the file it read and says
|
|
153
|
+
// what that file is, instead of claiming this process watched the refusal.
|
|
154
|
+
expect(status.detail).toContain('Per .codex/guard-proof.json');
|
|
155
|
+
expect(status.detail).toContain('not a security boundary');
|
|
156
|
+
expect(status.detail).not.toContain('observed refusing');
|
|
157
|
+
// And it surfaces the weaker class of proof (review SF3).
|
|
158
|
+
expect(status.detail).toContain('WEAKER PROOF for named-memory-write-guard');
|
|
159
|
+
expect(status.detail).toContain('mx memories get');
|
|
160
|
+
});
|
|
161
|
+
it('NO artifact proves nothing, and names the command that would produce one', () => {
|
|
162
|
+
const status = read();
|
|
163
|
+
expect(status.provenGuards).toEqual([]);
|
|
164
|
+
expect(status.valid).toBe(false);
|
|
165
|
+
expect(status.detail).toContain('mx-agent codex prove-guards');
|
|
166
|
+
});
|
|
167
|
+
it('an artifact that is not JSON proves nothing', () => {
|
|
168
|
+
writeRaw('{ not json');
|
|
169
|
+
expect(read().provenGuards).toEqual([]);
|
|
170
|
+
});
|
|
171
|
+
it('an artifact that is not an object proves nothing', () => {
|
|
172
|
+
writeRaw('[]');
|
|
173
|
+
expect(read().provenGuards).toEqual([]);
|
|
174
|
+
});
|
|
175
|
+
/**
|
|
176
|
+
* THE STALENESS PROPERTY. This is the one that stops a proof of the old
|
|
177
|
+
* configuration authorising the new one: prove the guards, then change the hooks
|
|
178
|
+
* file, and the proof is void until it is re-run.
|
|
179
|
+
*/
|
|
180
|
+
it('a proof of a DIFFERENT hooks.json is stale, not proof', () => {
|
|
181
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
182
|
+
expect(read().valid).toBe(true);
|
|
183
|
+
writeFileSync(join(worktree, CODEX_HOOKS_FILENAME), `${readFileSync(join(worktree, CODEX_HOOKS_FILENAME), 'utf-8')} `);
|
|
184
|
+
// Re-grant so the ONLY thing that changed for the reader is the hash.
|
|
185
|
+
grantTrust();
|
|
186
|
+
const status = read();
|
|
187
|
+
expect(status.provenGuards).toEqual([]);
|
|
188
|
+
expect(status.detail).toContain('STALE PROOF');
|
|
189
|
+
});
|
|
190
|
+
it('a hooks.json that cannot be read at all proves nothing', () => {
|
|
191
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
192
|
+
rmSync(join(worktree, CODEX_HOOKS_FILENAME));
|
|
193
|
+
expect(read().provenGuards).toEqual([]);
|
|
194
|
+
});
|
|
195
|
+
it('an artifact naming ANOTHER worktree\'s hooks file proves nothing here', () => {
|
|
196
|
+
writeRaw(serializeGuardProof(validArtifact({ hooksPath: '/somewhere/else/.codex/hooks.json' })));
|
|
197
|
+
const status = read();
|
|
198
|
+
expect(status.provenGuards).toEqual([]);
|
|
199
|
+
expect(status.detail).toContain('/somewhere/else/.codex/hooks.json');
|
|
200
|
+
});
|
|
201
|
+
it('an artifact from another codex version proves nothing', () => {
|
|
202
|
+
writeRaw(serializeGuardProof(validArtifact({ codexVersion: '0.144.5' })));
|
|
203
|
+
expect(read().provenGuards).toEqual([]);
|
|
204
|
+
expect(read().detail).toContain('0.144.5');
|
|
205
|
+
});
|
|
206
|
+
it('trust revoked after the proof reads as not proven', () => {
|
|
207
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
208
|
+
expect(read().valid).toBe(true);
|
|
209
|
+
// Same artifact, same hooks file — only the human grant is gone. The guard would
|
|
210
|
+
// not be invoked at all, so a current proof of its behaviour is not enough.
|
|
211
|
+
writeFileSync(join(codexHome, 'config.toml'), `[projects."${worktree}"]\ntrust_level = "trusted"\n`);
|
|
212
|
+
const status = read();
|
|
213
|
+
expect(status.provenGuards).toEqual([]);
|
|
214
|
+
expect(status.detail).toContain('hook-trust');
|
|
215
|
+
});
|
|
216
|
+
it('PARTIAL proof: one guard proven is one guard proven, and the gate stays shut', () => {
|
|
217
|
+
const artifact = validArtifact();
|
|
218
|
+
artifact.guards = [artifact.guards[0]];
|
|
219
|
+
writeRaw(serializeGuardProof(artifact));
|
|
220
|
+
const status = read();
|
|
221
|
+
expect(status.provenGuards).toEqual(['git-mutation-guard']);
|
|
222
|
+
// The adapter needs BOTH containment guards, so a partial proof still refuses.
|
|
223
|
+
expect(new CodexAdapter({ projectRoot: '/repo', provenGuards: status.provenGuards }).guardsProven).toBe(false);
|
|
224
|
+
});
|
|
225
|
+
it('a full proof satisfies the adapter gate — the transition out of the refusal exists', () => {
|
|
226
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
227
|
+
expect(new CodexAdapter({ projectRoot: '/repo', provenGuards: read().provenGuards }).guardsProven).toBe(true);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
describe('#4864 review — the gaps the security review found', () => {
|
|
231
|
+
/**
|
|
232
|
+
* MF1: the artifact bound sha256(hooks.json) and nothing else. hooks.json only NAMES
|
|
233
|
+
* the script; the behaviour is in the script, and `.claude/hooks/` is writable from
|
|
234
|
+
* inside a session. See also the executed pair at the end of this file.
|
|
235
|
+
*
|
|
236
|
+
* MUTATION CHECK: removing the scriptSha256 loop in readGuardProof fails these.
|
|
237
|
+
*/
|
|
238
|
+
describe('MF1 — the proof is bound to the guard SCRIPTS, not only to hooks.json', () => {
|
|
239
|
+
it('a changed guard script is a STALE PROOF, named by path', () => {
|
|
240
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
241
|
+
expect(read().valid).toBe(true);
|
|
242
|
+
writeFileSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'), '#!/bin/bash\nexit 0\n');
|
|
243
|
+
const status = read();
|
|
244
|
+
expect(status.provenGuards).toEqual([]);
|
|
245
|
+
expect(status.detail).toContain('STALE PROOF');
|
|
246
|
+
expect(status.detail).toContain('.claude/hooks/git-mutation-guard.sh');
|
|
247
|
+
});
|
|
248
|
+
it('a MISSING guard script proves nothing', () => {
|
|
249
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
250
|
+
rmSync(join(worktree, '.claude', 'hooks', 'named-memory-write-guard.sh'));
|
|
251
|
+
expect(read().provenGuards).toEqual([]);
|
|
252
|
+
expect(read().detail).toContain('named-memory-write-guard');
|
|
253
|
+
});
|
|
254
|
+
it('the recorded scriptPath cannot be pointed somewhere harmless', () => {
|
|
255
|
+
// An artifact that named a file nobody runs would carry a hash that always
|
|
256
|
+
// matches. The path is pinned to the guard's own script, so this is rejected at
|
|
257
|
+
// validation rather than checked against the wrong file.
|
|
258
|
+
const artifact = validArtifact();
|
|
259
|
+
artifact.guards[0].scriptPath = '.claude/hooks/harmless.sh';
|
|
260
|
+
expect(validateGuardProofShape(artifact)).not.toBeNull();
|
|
261
|
+
writeRaw(serializeGuardProof(artifact));
|
|
262
|
+
expect(read().provenGuards).toEqual([]);
|
|
263
|
+
});
|
|
264
|
+
it.each([
|
|
265
|
+
['an absolute path', '/etc/hostname'],
|
|
266
|
+
['a traversal', '../../.claude/hooks/git-mutation-guard.sh'],
|
|
267
|
+
])('rejects %s as a scriptPath', (_label, path) => {
|
|
268
|
+
const artifact = validArtifact();
|
|
269
|
+
artifact.guards[0].scriptPath = path;
|
|
270
|
+
expect(validateGuardProofShape(artifact)).not.toBeNull();
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
/**
|
|
274
|
+
* MF3: the pin must void a proof taken against a different LIVE binary, not only one
|
|
275
|
+
* that disagrees with the compiled constant.
|
|
276
|
+
*
|
|
277
|
+
* MUTATION CHECK: dropping the liveCodexVersion comparison fails the first of these.
|
|
278
|
+
*/
|
|
279
|
+
describe('MF3 — the LIVE codex version, not only the compiled pin', () => {
|
|
280
|
+
it('a live version different from the proven one voids the proof', () => {
|
|
281
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
282
|
+
const status = readGuardProof({
|
|
283
|
+
worktreePath: worktree,
|
|
284
|
+
codexHome,
|
|
285
|
+
liveCodexVersion: '0.146.0',
|
|
286
|
+
});
|
|
287
|
+
expect(status.provenGuards).toEqual([]);
|
|
288
|
+
expect(status.detail).toContain('0.146.0');
|
|
289
|
+
expect(status.detail).toContain('binary on PATH');
|
|
290
|
+
});
|
|
291
|
+
it('a live version equal to the proven one is accepted', () => {
|
|
292
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
293
|
+
expect(readGuardProof({ worktreePath: worktree, codexHome, liveCodexVersion: VERIFIED_CODEX_VERSION }).valid).toBe(true);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
/**
|
|
297
|
+
* SF4: the spawn gate's decision lives in the PATH-resolved mx-agent binary, not in
|
|
298
|
+
* hooks.json, so a CLI change can alter proven behaviour with every hash matching.
|
|
299
|
+
*/
|
|
300
|
+
it('SF4 — an artifact from a different CLI build voids the proof', () => {
|
|
301
|
+
writeRaw(serializeGuardProof(validArtifact({ cliVersion: '0.1.262' })));
|
|
302
|
+
const status = read();
|
|
303
|
+
expect(status.provenGuards).toEqual([]);
|
|
304
|
+
expect(status.detail).toContain('0.1.262');
|
|
305
|
+
});
|
|
306
|
+
/**
|
|
307
|
+
* SF2: `lstat`, not `stat`. A symlinked artifact or hooks file would otherwise be
|
|
308
|
+
* read through to its target and reported on as though the path held those bytes.
|
|
309
|
+
*/
|
|
310
|
+
describe('SF2 — symlinks and non-regular files are refused, never followed', () => {
|
|
311
|
+
it('a symlinked artifact is refused', () => {
|
|
312
|
+
const real = join(worktree, 'elsewhere.json');
|
|
313
|
+
writeFileSync(real, serializeGuardProof(validArtifact()));
|
|
314
|
+
rmSync(join(worktree, GUARD_PROOF_FILENAME), { force: true });
|
|
315
|
+
symlinkSync(real, join(worktree, GUARD_PROOF_FILENAME));
|
|
316
|
+
const status = read();
|
|
317
|
+
expect(status.provenGuards).toEqual([]);
|
|
318
|
+
expect(status.detail).toContain('not a regular file');
|
|
319
|
+
});
|
|
320
|
+
it('a symlinked hooks.json proves nothing', () => {
|
|
321
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
322
|
+
const hooksPath = join(worktree, CODEX_HOOKS_FILENAME);
|
|
323
|
+
const real = join(worktree, 'elsewhere-hooks.json');
|
|
324
|
+
writeFileSync(real, readFileSync(hooksPath));
|
|
325
|
+
rmSync(hooksPath);
|
|
326
|
+
symlinkSync(real, hooksPath);
|
|
327
|
+
expect(read().provenGuards).toEqual([]);
|
|
328
|
+
});
|
|
329
|
+
it('a symlinked guard script proves nothing', () => {
|
|
330
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
331
|
+
const scriptPath = join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh');
|
|
332
|
+
const real = join(worktree, 'elsewhere-guard.sh');
|
|
333
|
+
writeFileSync(real, readFileSync(scriptPath));
|
|
334
|
+
rmSync(scriptPath);
|
|
335
|
+
symlinkSync(real, scriptPath);
|
|
336
|
+
expect(read().provenGuards).toEqual([]);
|
|
337
|
+
});
|
|
338
|
+
it('writeGuardProof refuses to write through a symlink', () => {
|
|
339
|
+
const real = join(worktree, 'target.json');
|
|
340
|
+
writeFileSync(real, '{}');
|
|
341
|
+
rmSync(join(worktree, GUARD_PROOF_FILENAME), { force: true });
|
|
342
|
+
symlinkSync(real, join(worktree, GUARD_PROOF_FILENAME));
|
|
343
|
+
expect(() => writeGuardProof({ worktreePath: worktree, artifact: validArtifact() })).toThrow(/not a regular file/);
|
|
344
|
+
expect(readFileSync(real, 'utf-8')).toBe('{}');
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
/**
|
|
349
|
+
* MF2 — the evaluation that CONTROLS the launch must postdate the last write.
|
|
350
|
+
*
|
|
351
|
+
* `runStart` read the proof to construct the adapter and rewrote `.codex/hooks.json`
|
|
352
|
+
* later in the same run, so the gate decided on a snapshot taken before the file it
|
|
353
|
+
* was about was last written — and `launch()`'s second `assertLaunchAllowed` call
|
|
354
|
+
* replayed that same snapshot rather than checking again.
|
|
355
|
+
*
|
|
356
|
+
* These use the adapter directly with a real reader, so what is pinned is the property
|
|
357
|
+
* (the gate re-reads) rather than a call count.
|
|
358
|
+
*
|
|
359
|
+
* MUTATION CHECK: deleting `this.refreshGuardProof(ctx.worktreePath)` from
|
|
360
|
+
* assertLaunchAllowed makes 'a proof invalidated AFTER construction refuses at the
|
|
361
|
+
* gate' fail.
|
|
362
|
+
*/
|
|
363
|
+
describe('MF2 — the launch gate re-reads the proof, it does not remember it', () => {
|
|
364
|
+
const reader = (wt) => {
|
|
365
|
+
const status = readGuardProof({ worktreePath: wt, codexHome });
|
|
366
|
+
return { provenGuards: status.provenGuards, detail: status.detail };
|
|
367
|
+
};
|
|
368
|
+
/**
|
|
369
|
+
* `assertLaunchAllowed` checks project trust through the adapter's default codexHome
|
|
370
|
+
* ($HOME/.codex), so the same grant has to be visible there.
|
|
371
|
+
*/
|
|
372
|
+
let oldHome;
|
|
373
|
+
beforeEach(() => {
|
|
374
|
+
oldHome = process.env.HOME;
|
|
375
|
+
const fakeHome = join(worktree, 'home');
|
|
376
|
+
mkdirSync(join(fakeHome, '.codex'), { recursive: true });
|
|
377
|
+
writeFileSync(join(fakeHome, '.codex', 'config.toml'), readFileSync(join(codexHome, 'config.toml')));
|
|
378
|
+
process.env.HOME = fakeHome;
|
|
379
|
+
});
|
|
380
|
+
afterEach(() => {
|
|
381
|
+
if (oldHome === undefined)
|
|
382
|
+
delete process.env.HOME;
|
|
383
|
+
else
|
|
384
|
+
process.env.HOME = oldHome;
|
|
385
|
+
});
|
|
386
|
+
function adapterWithReader() {
|
|
387
|
+
return new CodexAdapter({
|
|
388
|
+
projectRoot: '/repo',
|
|
389
|
+
// The stale snapshot a caller might construct with. The gate must not trust it.
|
|
390
|
+
provenGuards: ['git-mutation-guard', 'named-memory-write-guard'],
|
|
391
|
+
readGuardProof: reader,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
it('a proof invalidated AFTER construction refuses at the gate', () => {
|
|
395
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
396
|
+
const adapter = adapterWithReader();
|
|
397
|
+
expect(adapter.guardsProven).toBe(true); // constructor snapshot
|
|
398
|
+
// Exactly what deployConfig does later in runStart: rewrite the hooks file.
|
|
399
|
+
writeFileSync(join(worktree, CODEX_HOOKS_FILENAME), `${readFileSync(join(worktree, CODEX_HOOKS_FILENAME), 'utf-8')} `);
|
|
400
|
+
expect(() => adapter.assertLaunchAllowed({ worktreePath: worktree })).toThrow(/gate G-7 is unpassed/);
|
|
401
|
+
expect(adapter.guardsProven).toBe(false);
|
|
402
|
+
});
|
|
403
|
+
/**
|
|
404
|
+
* SF5 — the refusal has to name the binding that broke. `refreshGuardProof` has just
|
|
405
|
+
* computed the precise reason; throwing a generic message discards it, and the
|
|
406
|
+
* operator is left with a banner from the earlier read (possibly green) followed by
|
|
407
|
+
* a refusal that says only "G-7 is unpassed".
|
|
408
|
+
*
|
|
409
|
+
* MUTATION CHECK: dropping the `this.guardProofDetail` interpolation from the throw
|
|
410
|
+
* makes this fail.
|
|
411
|
+
*/
|
|
412
|
+
it('the refusal NAMES the binding that broke, not just that the gate is shut', () => {
|
|
413
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
414
|
+
const adapter = adapterWithReader();
|
|
415
|
+
writeFileSync(join(worktree, CODEX_HOOKS_FILENAME), `${readFileSync(join(worktree, CODEX_HOOKS_FILENAME), 'utf-8')} `);
|
|
416
|
+
let message = '';
|
|
417
|
+
try {
|
|
418
|
+
adapter.assertLaunchAllowed({ worktreePath: worktree });
|
|
419
|
+
}
|
|
420
|
+
catch (err) {
|
|
421
|
+
message = err instanceof Error ? err.message : String(err);
|
|
422
|
+
}
|
|
423
|
+
expect(message).toContain('gate G-7 is unpassed');
|
|
424
|
+
// The specific cause, and enough of it to act on.
|
|
425
|
+
expect(message).toContain('STALE PROOF');
|
|
426
|
+
expect(message).toContain(CODEX_HOOKS_FILENAME);
|
|
427
|
+
expect(message).toContain('prove-guards');
|
|
428
|
+
});
|
|
429
|
+
it('names a CHANGED GUARD SCRIPT in the refusal too, not only a stale hooks hash', () => {
|
|
430
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
431
|
+
const adapter = adapterWithReader();
|
|
432
|
+
writeFileSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'), '#!/bin/bash\nexit 0\n');
|
|
433
|
+
let message = '';
|
|
434
|
+
try {
|
|
435
|
+
adapter.assertLaunchAllowed({ worktreePath: worktree });
|
|
436
|
+
}
|
|
437
|
+
catch (err) {
|
|
438
|
+
message = err instanceof Error ? err.message : String(err);
|
|
439
|
+
}
|
|
440
|
+
expect(message).toContain('.claude/hooks/git-mutation-guard.sh');
|
|
441
|
+
});
|
|
442
|
+
it('a guard neutered after construction refuses at the gate', () => {
|
|
443
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
444
|
+
const adapter = adapterWithReader();
|
|
445
|
+
writeFileSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'), '#!/bin/bash\nexit 0\n');
|
|
446
|
+
expect(() => adapter.assertLaunchAllowed({ worktreePath: worktree })).toThrow(/gate G-7 is unpassed/);
|
|
447
|
+
});
|
|
448
|
+
it('EVERY gate call re-reads — a proof restored between calls is seen', () => {
|
|
449
|
+
const adapter = adapterWithReader();
|
|
450
|
+
// No artifact yet: the gate refuses despite the constructor's optimistic list.
|
|
451
|
+
expect(() => adapter.assertLaunchAllowed({ worktreePath: worktree })).toThrow(/gate G-7 is unpassed/);
|
|
452
|
+
writeGuardProof({ worktreePath: worktree, artifact: validArtifact() });
|
|
453
|
+
expect(() => adapter.assertLaunchAllowed({ worktreePath: worktree })).not.toThrow();
|
|
454
|
+
expect(adapter.guardsProven).toBe(true);
|
|
455
|
+
});
|
|
456
|
+
it('the banner detail follows the latest read, not the constructor', () => {
|
|
457
|
+
const adapter = adapterWithReader();
|
|
458
|
+
try {
|
|
459
|
+
adapter.assertLaunchAllowed({ worktreePath: worktree });
|
|
460
|
+
}
|
|
461
|
+
catch {
|
|
462
|
+
// Expected: no artifact exists yet.
|
|
463
|
+
}
|
|
464
|
+
const notices = adapter.bannerNotices().join('\n');
|
|
465
|
+
expect(notices).toContain('NO guard hook is proven enforced');
|
|
466
|
+
expect(notices).toContain('prove-guards');
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
describe('validateGuardProofShape — strict, and any malformed part voids the whole', () => {
|
|
470
|
+
const cases = [
|
|
471
|
+
['a version this reader does not implement', { version: 2 }],
|
|
472
|
+
['a sha that is not 64 hex chars', { hooksSha256: 'deadbeef' }],
|
|
473
|
+
['an uppercase sha (not the digest form written)', { hooksSha256: 'A'.repeat(64) }],
|
|
474
|
+
['a provenAt that is not an ISO-8601 UTC stamp', { provenAt: 'yesterday' }],
|
|
475
|
+
['an empty guards list', { guards: [] }],
|
|
476
|
+
[
|
|
477
|
+
'a guard name outside the Phase 1 set',
|
|
478
|
+
(a) => {
|
|
479
|
+
a.guards[0].guard = 'invented-guard';
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
[
|
|
483
|
+
'the same guard listed twice',
|
|
484
|
+
(a) => {
|
|
485
|
+
a.guards = [a.guards[0], a.guards[0]];
|
|
486
|
+
},
|
|
487
|
+
],
|
|
488
|
+
[
|
|
489
|
+
'a refused half that did not exit 2',
|
|
490
|
+
(a) => {
|
|
491
|
+
a.guards[0].refusedExitCode = 0;
|
|
492
|
+
},
|
|
493
|
+
],
|
|
494
|
+
[
|
|
495
|
+
'a permitted half that was refused',
|
|
496
|
+
(a) => {
|
|
497
|
+
a.guards[0].permittedExitCode = 2;
|
|
498
|
+
},
|
|
499
|
+
],
|
|
500
|
+
[
|
|
501
|
+
'an unattributed refusal',
|
|
502
|
+
(a) => {
|
|
503
|
+
a.guards[0].refusalAttribution = '';
|
|
504
|
+
},
|
|
505
|
+
],
|
|
506
|
+
[
|
|
507
|
+
'a junk entry alongside a real one',
|
|
508
|
+
(a) => {
|
|
509
|
+
a.guards.push({ guard: 'git-mutation-guard' });
|
|
510
|
+
},
|
|
511
|
+
],
|
|
512
|
+
['no spawn-gate observation', { spawnGate: undefined }],
|
|
513
|
+
[
|
|
514
|
+
'a spawn gate that also refused unrelated tools',
|
|
515
|
+
(a) => {
|
|
516
|
+
a.spawnGate.unrelatedToolExitCode = 2;
|
|
517
|
+
},
|
|
518
|
+
],
|
|
519
|
+
];
|
|
520
|
+
it.each(cases)('rejects %s', (_label, mutation) => {
|
|
521
|
+
const artifact = validArtifact();
|
|
522
|
+
if (typeof mutation === 'function')
|
|
523
|
+
mutation(artifact);
|
|
524
|
+
else
|
|
525
|
+
Object.assign(artifact, mutation);
|
|
526
|
+
expect(validateGuardProofShape(artifact)).not.toBeNull();
|
|
527
|
+
writeRaw(serializeGuardProof(artifact));
|
|
528
|
+
expect(read().provenGuards).toEqual([]);
|
|
529
|
+
});
|
|
530
|
+
it('an artifact whose permitted half was refused is not a proof', () => {
|
|
531
|
+
const artifact = validArtifact();
|
|
532
|
+
artifact.guards[0].permittedExitCode = 2;
|
|
533
|
+
writeRaw(serializeGuardProof(artifact));
|
|
534
|
+
expect(read().provenGuards).toEqual([]);
|
|
535
|
+
});
|
|
536
|
+
it('writeGuardProof refuses to write what its own reader would reject', () => {
|
|
537
|
+
expect(() => writeGuardProof({ worktreePath: worktree, artifact: validArtifact({ hooksSha256: 'nope' }) })).toThrow(/refusing to write an invalid/);
|
|
538
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
describe('proveCodexGuards — the artifact is only written on an OBSERVED refusal', () => {
|
|
542
|
+
const GUARD_MARKERS = {
|
|
543
|
+
'git-mutation-guard.sh': 'BLOCKED: destructive git operation',
|
|
544
|
+
'named-memory-write-guard.sh': 'BLOCKED: truncating write to named memory',
|
|
545
|
+
};
|
|
546
|
+
const GATE_MARKER = 'subagent spawning is unavailable under codex';
|
|
547
|
+
/**
|
|
548
|
+
* A fake runtime that behaves the way a healthy configuration does. Each test then
|
|
549
|
+
* breaks exactly one thing, so the assertion names the cause it is about.
|
|
550
|
+
*/
|
|
551
|
+
function runner(over = {}) {
|
|
552
|
+
return (command, payload) => {
|
|
553
|
+
const parsed = JSON.parse(payload);
|
|
554
|
+
const isGate = command.includes('internal-codex-agent-gate');
|
|
555
|
+
if (isGate) {
|
|
556
|
+
if (parsed.tool_name === 'spawn_agent') {
|
|
557
|
+
return { status: over.gateStatus ?? 2, stderr: over.gateStderr ?? GATE_MARKER };
|
|
558
|
+
}
|
|
559
|
+
return { status: over.unrelatedStatus ?? 0, stderr: '' };
|
|
560
|
+
}
|
|
561
|
+
const script = Object.keys(GUARD_MARKERS).find((s) => command.includes(s));
|
|
562
|
+
const forbidden = (parsed.tool_input.command ?? '').includes('reset --hard') ||
|
|
563
|
+
(parsed.tool_input.command ?? '').includes('--content "x"');
|
|
564
|
+
if (!forbidden)
|
|
565
|
+
return { status: over.permittedStatus ?? 0, stderr: '' };
|
|
566
|
+
return {
|
|
567
|
+
status: over.guardStatus ?? 2,
|
|
568
|
+
stderr: over.guardStderr ?? GUARD_MARKERS[script],
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
const prove = (r) => proveCodexGuards({
|
|
573
|
+
worktreePath: worktree,
|
|
574
|
+
codexHome,
|
|
575
|
+
now: NOW,
|
|
576
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
577
|
+
runner: r,
|
|
578
|
+
env: { PATH: process.env.PATH ?? '', HOME: worktree },
|
|
579
|
+
});
|
|
580
|
+
it('writes an artifact the reader accepts, bound to the file it proved', () => {
|
|
581
|
+
const result = prove(runner());
|
|
582
|
+
expect(result.ok).toBe(true);
|
|
583
|
+
expect(result.proven.sort()).toEqual(['git-mutation-guard', 'named-memory-write-guard']);
|
|
584
|
+
const status = read();
|
|
585
|
+
expect(status.valid).toBe(true);
|
|
586
|
+
const artifact = JSON.parse(readFileSync(join(worktree, GUARD_PROOF_FILENAME), 'utf-8'));
|
|
587
|
+
expect(artifact.hooksSha256).toBe(hooksSha());
|
|
588
|
+
expect(artifact.provenAt).toBe(NOW);
|
|
589
|
+
});
|
|
590
|
+
it('a guard that ALLOWS the forbidden command writes nothing', () => {
|
|
591
|
+
const result = prove(runner({ guardStatus: 0 }));
|
|
592
|
+
expect(result.ok).toBe(false);
|
|
593
|
+
expect(result.lines.join('\n')).toContain('DID NOT REFUSE');
|
|
594
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
595
|
+
});
|
|
596
|
+
/**
|
|
597
|
+
* The wrapper refuses too — with `BLOCKED: mx-agent containment guard … could not be
|
|
598
|
+
* resolved` — and that refusal means the guard was NEVER REACHED. Exit 2 alone
|
|
599
|
+
* cannot tell the two apart, which is why attribution is required (#4851).
|
|
600
|
+
*/
|
|
601
|
+
it('exit 2 from the WRAPPER, not the guard, is not a proof', () => {
|
|
602
|
+
const result = prove(runner({ guardStderr: 'BLOCKED: mx-agent containment guard could not be resolved.' }));
|
|
603
|
+
expect(result.ok).toBe(false);
|
|
604
|
+
expect(result.lines.join('\n')).toContain('cannot be attributed to the guard');
|
|
605
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
606
|
+
});
|
|
607
|
+
it('a guard that refuses the PERMITTED command writes nothing — both halves are required', () => {
|
|
608
|
+
const result = prove(runner({ permittedStatus: 2 }));
|
|
609
|
+
expect(result.ok).toBe(false);
|
|
610
|
+
expect(result.lines.join('\n')).toContain('blocks everything is not a control');
|
|
611
|
+
});
|
|
612
|
+
it('a spawn gate that allows a spawn writes nothing', () => {
|
|
613
|
+
const result = prove(runner({ gateStatus: 0 }));
|
|
614
|
+
expect(result.ok).toBe(false);
|
|
615
|
+
expect(result.lines.join('\n')).toContain('spawn gate did not refuse');
|
|
616
|
+
});
|
|
617
|
+
it('a spawn gate that refuses unrelated tools writes nothing', () => {
|
|
618
|
+
const result = prove(runner({ unrelatedStatus: 2 }));
|
|
619
|
+
expect(result.ok).toBe(false);
|
|
620
|
+
expect(result.lines.join('\n')).toContain('refused an unrelated Bash call');
|
|
621
|
+
});
|
|
622
|
+
it('refuses when Codex would not RUN the hooks (trust ungranted)', () => {
|
|
623
|
+
writeFileSync(join(codexHome, 'config.toml'), `[projects."${worktree}"]\ntrust_level = "trusted"\n`);
|
|
624
|
+
const result = prove(runner());
|
|
625
|
+
expect(result.ok).toBe(false);
|
|
626
|
+
expect(result.lines.join('\n')).toContain('would not RUN these hooks');
|
|
627
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
628
|
+
});
|
|
629
|
+
it('refuses when there is no deployed hooks.json to prove', () => {
|
|
630
|
+
rmSync(join(worktree, CODEX_HOOKS_FILENAME));
|
|
631
|
+
expect(prove(runner()).ok).toBe(false);
|
|
632
|
+
});
|
|
633
|
+
it('refuses a hooks.json this CLI did not generate — no blessing hand edits', () => {
|
|
634
|
+
const text = readFileSync(join(worktree, CODEX_HOOKS_FILENAME), 'utf-8');
|
|
635
|
+
writeFileSync(join(worktree, CODEX_HOOKS_FILENAME), text.replace('"description"', '"Description"'));
|
|
636
|
+
grantTrust();
|
|
637
|
+
const result = prove(runner());
|
|
638
|
+
expect(result.ok).toBe(false);
|
|
639
|
+
expect(result.lines.join('\n')).toContain('does not match what this CLI generates');
|
|
640
|
+
});
|
|
641
|
+
it('leaves an existing artifact untouched when a later probe fails', () => {
|
|
642
|
+
prove(runner());
|
|
643
|
+
const before = readFileSync(join(worktree, GUARD_PROOF_FILENAME), 'utf-8');
|
|
644
|
+
const result = prove(runner({ gateStatus: 0 }));
|
|
645
|
+
expect(result.ok).toBe(false);
|
|
646
|
+
expect(readFileSync(join(worktree, GUARD_PROOF_FILENAME), 'utf-8')).toBe(before);
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
/**
|
|
650
|
+
* ─── EXECUTED, not mocked ──────────────────────────────────────────────────────
|
|
651
|
+
*
|
|
652
|
+
* The tests above prove the decision logic. This one proves the thing the decision
|
|
653
|
+
* logic is about: the REAL guard scripts, invoked through the REAL generated wrapper,
|
|
654
|
+
* by the REAL default runner. A mocked positive control is a positive control that has
|
|
655
|
+
* never been observed to fire — which is the defect this whole change exists to remove.
|
|
656
|
+
*
|
|
657
|
+
* The spawn gate is exercised through a `mx-agent` shim pointing at the built CLI, the
|
|
658
|
+
* same lift codex-agent-gate.test.ts uses.
|
|
659
|
+
*/
|
|
660
|
+
describe('proveCodexGuards — executed against the shipped guards and gate', () => {
|
|
661
|
+
let shimDir;
|
|
662
|
+
beforeEach(() => {
|
|
663
|
+
// Guard scripts are already on disk from the outer beforeEach (the wrapper looks
|
|
664
|
+
// for them there, and the reader hashes them).
|
|
665
|
+
shimDir = mkdtempSync(join(tmpdir(), 'mx-proof-bin-'));
|
|
666
|
+
const binary = join(CLI_ROOT, 'dist', 'index.js');
|
|
667
|
+
expect(existsSync(binary)).toBe(true); // CI builds before testing (binary-smoke.test.ts)
|
|
668
|
+
writeFileSync(join(shimDir, 'mx-agent'), `#!/bin/bash\nexec node ${binary} "$@"\n`, { mode: 0o755 });
|
|
669
|
+
chmodSync(join(shimDir, 'mx-agent'), 0o755);
|
|
670
|
+
});
|
|
671
|
+
afterEach(() => {
|
|
672
|
+
rmSync(shimDir, { recursive: true, force: true });
|
|
673
|
+
});
|
|
674
|
+
it('observes both guards and the gate refusing, and records it', () => {
|
|
675
|
+
const result = proveCodexGuards({
|
|
676
|
+
worktreePath: worktree,
|
|
677
|
+
codexHome,
|
|
678
|
+
now: NOW,
|
|
679
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
680
|
+
env: { PATH: `${shimDir}:${process.env.PATH ?? ''}`, HOME: worktree },
|
|
681
|
+
});
|
|
682
|
+
expect(result.lines.join('\n')).toContain('✓ git-mutation-guard');
|
|
683
|
+
expect(result.ok).toBe(true);
|
|
684
|
+
expect(result.proven.sort()).toEqual(['git-mutation-guard', 'named-memory-write-guard']);
|
|
685
|
+
expect(read().valid).toBe(true);
|
|
686
|
+
});
|
|
687
|
+
it('a guard script that is absent is refused before anything is probed', () => {
|
|
688
|
+
rmSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'));
|
|
689
|
+
const result = proveCodexGuards({
|
|
690
|
+
worktreePath: worktree,
|
|
691
|
+
codexHome,
|
|
692
|
+
now: NOW,
|
|
693
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
694
|
+
env: { PATH: `${shimDir}:${process.env.PATH ?? ''}`, HOME: worktree },
|
|
695
|
+
});
|
|
696
|
+
// There is no script to hash, so there is nothing to prove. (Before the script
|
|
697
|
+
// hash existed this reached the probe, where the WRAPPER's fail-closed exit 2
|
|
698
|
+
// would have been mistaken for the guard refusing had attribution not caught it.)
|
|
699
|
+
expect(result.ok).toBe(false);
|
|
700
|
+
expect(result.lines.join('\n')).toContain('there is no guard');
|
|
701
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
702
|
+
});
|
|
703
|
+
/**
|
|
704
|
+
* MF1, EXECUTED. `.claude/hooks/` is writable from inside a Codex session — the
|
|
705
|
+
* runtime read-only-binds `.git`, `.agents` and `.codex`, and nothing else. So the
|
|
706
|
+
* script that carries the behaviour can be replaced while every byte the earlier
|
|
707
|
+
* revision hashed stays untouched.
|
|
708
|
+
*/
|
|
709
|
+
it('a NEUTERED guard script is not recorded as proven', () => {
|
|
710
|
+
writeFileSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'), '#!/bin/bash\nexit 0\n');
|
|
711
|
+
const result = proveCodexGuards({
|
|
712
|
+
worktreePath: worktree,
|
|
713
|
+
codexHome,
|
|
714
|
+
now: NOW,
|
|
715
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
716
|
+
env: { PATH: `${shimDir}:${process.env.PATH ?? ''}`, HOME: worktree },
|
|
717
|
+
});
|
|
718
|
+
expect(result.ok).toBe(false);
|
|
719
|
+
expect(result.lines.join('\n')).toContain('DID NOT REFUSE');
|
|
720
|
+
expect(existsSync(join(worktree, GUARD_PROOF_FILENAME))).toBe(false);
|
|
721
|
+
});
|
|
722
|
+
/**
|
|
723
|
+
* MF1's headline case, EXECUTED end to end: prove honestly, THEN neuter the script.
|
|
724
|
+
* Before the script hash, this artifact stayed valid and the banner still reported a
|
|
725
|
+
* guard that refused nothing.
|
|
726
|
+
*
|
|
727
|
+
* MUTATION CHECK: dropping the scriptSha256 comparison in readGuardProof makes this
|
|
728
|
+
* fail.
|
|
729
|
+
*/
|
|
730
|
+
it('neutering a guard AFTER an honest proof voids that proof', () => {
|
|
731
|
+
const proved = proveCodexGuards({
|
|
732
|
+
worktreePath: worktree,
|
|
733
|
+
codexHome,
|
|
734
|
+
now: NOW,
|
|
735
|
+
codexVersion: VERIFIED_CODEX_VERSION,
|
|
736
|
+
env: { PATH: `${shimDir}:${process.env.PATH ?? ''}`, HOME: worktree },
|
|
737
|
+
});
|
|
738
|
+
expect(proved.ok).toBe(true);
|
|
739
|
+
expect(read().valid).toBe(true);
|
|
740
|
+
writeFileSync(join(worktree, '.claude', 'hooks', 'git-mutation-guard.sh'), '#!/bin/bash\nexit 0\n');
|
|
741
|
+
const status = read();
|
|
742
|
+
expect(status.provenGuards).toEqual([]);
|
|
743
|
+
expect(status.detail).toContain('STALE PROOF');
|
|
744
|
+
expect(status.detail).toContain('.claude/hooks/git-mutation-guard.sh');
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
//# sourceMappingURL=runtime-codex-guard-proof.test.js.map
|