@lifeaitools/rdc-skills 0.24.4 → 0.24.6
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/git-sha.json +1 -1
- package/hooks/gate-watchdog-selfcheck.js +245 -0
- package/hooks/post-tool-batch-gate.js +203 -0
- package/hooks/task-completed-gate.js +252 -0
- package/hooks/work-item-exit-gate.js +293 -0
- package/package.json +2 -2
- package/scripts/install-rdc-skills.js +11 -0
- package/tests/harness-gates.test.mjs +265 -0
- package/tests/work-item-exit-gate-l3.test.mjs +197 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Truth Gate 3.0 — Layers 5+6 harness-gate tests (WP-5).
|
|
4
|
+
*
|
|
5
|
+
* Covers the three new hooks, fully offline (no DB, no brain, no clauth):
|
|
6
|
+
* task-completed-gate.js — flag OFF no-op; flag ON + no/unsound closure BLOCK;
|
|
7
|
+
* flag ON + sound closure PASS.
|
|
8
|
+
* post-tool-batch-gate.js — flag OFF no-op; flag ON + stale worktree base FLAG;
|
|
9
|
+
* flag ON + current base PASS.
|
|
10
|
+
* gate-watchdog-selfcheck — STOP-banner when a gate file/registration absent;
|
|
11
|
+
* silent when all present; checks the LIVE hookify
|
|
12
|
+
* hooks.json (plugin manifest), not the dead wrappers.
|
|
13
|
+
*
|
|
14
|
+
* Run: node tests/harness-gates.test.mjs
|
|
15
|
+
*/
|
|
16
|
+
import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
17
|
+
import { tmpdir } from 'node:os';
|
|
18
|
+
import { join, resolve, dirname } from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { spawnSync } from 'node:child_process';
|
|
21
|
+
import { createRequire } from 'node:module';
|
|
22
|
+
|
|
23
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
24
|
+
const REPO_ROOT = resolve(__dirname, '..');
|
|
25
|
+
const HOOKS = join(REPO_ROOT, 'hooks');
|
|
26
|
+
const require = createRequire(import.meta.url);
|
|
27
|
+
|
|
28
|
+
const failures = [];
|
|
29
|
+
function assert(name, condition, detail = '') {
|
|
30
|
+
if (!condition) failures.push(`${name}${detail ? `: ${detail}` : ''}`);
|
|
31
|
+
else process.stdout.write(` ok ${name}\n`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function runHook(file, payload, extraEnv = {}) {
|
|
35
|
+
return spawnSync(process.execPath, [join(HOOKS, file)], {
|
|
36
|
+
input: JSON.stringify(payload),
|
|
37
|
+
encoding: 'utf8',
|
|
38
|
+
env: { ...process.env, ...extraEnv },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const WI = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee';
|
|
43
|
+
|
|
44
|
+
// ===========================================================================
|
|
45
|
+
// 1. task-completed-gate.js
|
|
46
|
+
// ===========================================================================
|
|
47
|
+
{
|
|
48
|
+
const tcg = require(join(HOOKS, 'task-completed-gate.js'));
|
|
49
|
+
|
|
50
|
+
// pure: closureIsSound branches
|
|
51
|
+
assert('TCG closureIsSound: null row → not ok', tcg.closureIsSound(null).ok === false);
|
|
52
|
+
assert('TCG closureIsSound: not-done task → not ok',
|
|
53
|
+
tcg.closureIsSound({ id: WI, item_type: 'task', status: 'review' }).ok === false);
|
|
54
|
+
assert('TCG closureIsSound: done but witness:agent → not ok',
|
|
55
|
+
tcg.closureIsSound({ id: WI, item_type: 'task', status: 'done', implementation_report: { codeflow_post: { witness: 'agent', commit: 'x', files_changed: ['f'], verification: [{ exit_code: 0 }] } } }).ok === false);
|
|
56
|
+
const soundRow = {
|
|
57
|
+
id: WI, item_type: 'task', status: 'done',
|
|
58
|
+
implementation_report: { codeflow_post: { witness: 'validator-rerun', commit: 'c3189c9d58a37d648e9de4a6bcd7d46772053eea', files_changed: ['hooks/x.js'], verification: [{ exit_code: 0 }] } },
|
|
59
|
+
};
|
|
60
|
+
assert('TCG closureIsSound: full sound closure → ok', tcg.closureIsSound(soundRow).ok === true,
|
|
61
|
+
tcg.closureIsSound(soundRow).reason || '');
|
|
62
|
+
assert('TCG closureIsSound: epic done → ok',
|
|
63
|
+
tcg.closureIsSound({ id: WI, item_type: 'epic', status: 'done' }).ok === true);
|
|
64
|
+
|
|
65
|
+
// pure: extractWorkItemId
|
|
66
|
+
assert('TCG extractWorkItemId: from field', tcg.extractWorkItemId({ work_item_id: WI }) === WI);
|
|
67
|
+
assert('TCG extractWorkItemId: from blob', tcg.extractWorkItemId({ task: { note: `closes ${WI}` } }) === WI);
|
|
68
|
+
assert('TCG extractWorkItemId: none → null', tcg.extractWorkItemId({ task: { note: 'no uuid' } }) === null);
|
|
69
|
+
|
|
70
|
+
// pure: flagEnabledEnv default OFF
|
|
71
|
+
{
|
|
72
|
+
const prev = process.env.RDC_TRUTHGATE_TASKCOMPLETED;
|
|
73
|
+
delete process.env.RDC_TRUTHGATE_TASKCOMPLETED;
|
|
74
|
+
assert('TCG flag default OFF', tcg.flagEnabledEnv() === false);
|
|
75
|
+
process.env.RDC_TRUTHGATE_TASKCOMPLETED = 'true';
|
|
76
|
+
assert('TCG flag ON via env', tcg.flagEnabledEnv() === true);
|
|
77
|
+
if (prev === undefined) delete process.env.RDC_TRUTHGATE_TASKCOMPLETED;
|
|
78
|
+
else process.env.RDC_TRUTHGATE_TASKCOMPLETED = prev;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// process: flag OFF → no-op (exit 0, no block)
|
|
82
|
+
{
|
|
83
|
+
const r = runHook('task-completed-gate.js', { work_item_id: WI }, { RDC_TRUTHGATE_TASKCOMPLETED: '' });
|
|
84
|
+
assert('TCG flag OFF → exit 0 no-op', r.status === 0, `status=${r.status} ${r.stderr}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// process: flag ON + no closure ref → BLOCK (exit 2)
|
|
88
|
+
{
|
|
89
|
+
const r = runHook('task-completed-gate.js', { task: { note: 'no uuid here' } },
|
|
90
|
+
{ RDC_TRUTHGATE_TASKCOMPLETED: '1' });
|
|
91
|
+
assert('TCG flag ON + no work-item ref → exit 2 BLOCK', r.status === 2, `status=${r.status}`);
|
|
92
|
+
assert('TCG block mentions no work-item reference', /no work-item reference/.test(r.stdout + r.stderr), r.stdout);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// process: flag ON + unsound closure (status review via sink) → BLOCK
|
|
96
|
+
{
|
|
97
|
+
const dir = mkdtempSync(join(tmpdir(), 'tcg-'));
|
|
98
|
+
const sink = join(dir, 'closure.json');
|
|
99
|
+
writeFileSync(sink, JSON.stringify({ id: WI, item_type: 'task', status: 'review', implementation_report: null }));
|
|
100
|
+
const r = runHook('task-completed-gate.js', { work_item_id: WI },
|
|
101
|
+
{ RDC_TRUTHGATE_TASKCOMPLETED: '1', RDC_TASKCOMPLETED_CLOSURE_SINK: sink });
|
|
102
|
+
assert('TCG flag ON + unsound closure → exit 2 BLOCK', r.status === 2, `status=${r.status} ${r.stdout}`);
|
|
103
|
+
rmSync(dir, { recursive: true, force: true });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// process: flag ON + sound closure (via sink) → PASS (exit 0)
|
|
107
|
+
{
|
|
108
|
+
const dir = mkdtempSync(join(tmpdir(), 'tcg-'));
|
|
109
|
+
const sink = join(dir, 'closure.json');
|
|
110
|
+
writeFileSync(sink, JSON.stringify(soundRow));
|
|
111
|
+
const r = runHook('task-completed-gate.js', { work_item_id: WI },
|
|
112
|
+
{ RDC_TRUTHGATE_TASKCOMPLETED: '1', RDC_TASKCOMPLETED_CLOSURE_SINK: sink });
|
|
113
|
+
assert('TCG flag ON + sound closure → exit 0 PASS', r.status === 0, `status=${r.status} ${r.stdout}${r.stderr}`);
|
|
114
|
+
rmSync(dir, { recursive: true, force: true });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ===========================================================================
|
|
119
|
+
// 2. post-tool-batch-gate.js
|
|
120
|
+
// ===========================================================================
|
|
121
|
+
{
|
|
122
|
+
const ptb = require(join(HOOKS, 'post-tool-batch-gate.js'));
|
|
123
|
+
const DEV = 'c3189c9d58a37d648e9de4a6bcd7d46772053eea';
|
|
124
|
+
const STALE = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef';
|
|
125
|
+
const FRESH = '1234567812345678123456781234567812345678';
|
|
126
|
+
|
|
127
|
+
// pure: evaluateWave — cannotEvaluate when no develop HEAD
|
|
128
|
+
{
|
|
129
|
+
const v = ptb.evaluateWave({ developHead: null, worktrees: [], isAncestor: () => true });
|
|
130
|
+
assert('PTB evaluateWave: no develop HEAD → cannotEvaluate', v.cannotEvaluate === true && v.ok === false);
|
|
131
|
+
}
|
|
132
|
+
// pure: current base (develop is ancestor of worktree HEAD) → ok
|
|
133
|
+
{
|
|
134
|
+
const v = ptb.evaluateWave({
|
|
135
|
+
developHead: DEV,
|
|
136
|
+
worktrees: [{ path: '/wt/a', head: FRESH }],
|
|
137
|
+
isAncestor: (a, b) => a === DEV && b === FRESH, // DEV is ancestor of FRESH
|
|
138
|
+
});
|
|
139
|
+
assert('PTB evaluateWave: current base → ok, no stale', v.ok === true && v.stale.length === 0);
|
|
140
|
+
}
|
|
141
|
+
// pure: worktree HEAD == develop HEAD → ok
|
|
142
|
+
{
|
|
143
|
+
const v = ptb.evaluateWave({
|
|
144
|
+
developHead: DEV,
|
|
145
|
+
worktrees: [{ path: '/wt/a', head: DEV }],
|
|
146
|
+
isAncestor: () => false,
|
|
147
|
+
});
|
|
148
|
+
assert('PTB evaluateWave: head==develop → ok', v.ok === true && v.stale.length === 0);
|
|
149
|
+
}
|
|
150
|
+
// pure: stale base (develop NOT an ancestor) → flagged
|
|
151
|
+
{
|
|
152
|
+
const v = ptb.evaluateWave({
|
|
153
|
+
developHead: DEV,
|
|
154
|
+
worktrees: [{ path: '/wt/stale', head: STALE }],
|
|
155
|
+
isAncestor: () => false, // DEV is NOT an ancestor of STALE
|
|
156
|
+
});
|
|
157
|
+
assert('PTB evaluateWave: stale base → not ok', v.ok === false, JSON.stringify(v));
|
|
158
|
+
assert('PTB evaluateWave: stale worktree listed', v.stale.length === 1 && v.stale[0].head === STALE);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// process: flag OFF → no-op (exit 0)
|
|
162
|
+
{
|
|
163
|
+
const r = runHook('post-tool-batch-gate.js', { hook_event_name: 'PostToolBatch' },
|
|
164
|
+
{ RDC_TRUTHGATE_POSTTOOLBATCH: '' });
|
|
165
|
+
assert('PTB flag OFF → exit 0 no-op', r.status === 0, `status=${r.status} ${r.stderr}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ===========================================================================
|
|
170
|
+
// 3. gate-watchdog-selfcheck.js
|
|
171
|
+
// ===========================================================================
|
|
172
|
+
{
|
|
173
|
+
const wd = require(join(HOOKS, 'gate-watchdog-selfcheck.js'));
|
|
174
|
+
|
|
175
|
+
const allPresent = {
|
|
176
|
+
exitGateFileExists: true, exitGateRegistered: true,
|
|
177
|
+
truthGateFileExists: true, truthGateOnStop: true, truthGateOnSubagentStop: true,
|
|
178
|
+
liveHookifyManifestExists: true, deadWrapperPointsAtStaleCache: false,
|
|
179
|
+
};
|
|
180
|
+
// all present → no findings, empty banner (silent)
|
|
181
|
+
{
|
|
182
|
+
const findings = wd.evaluateWatchdog(allPresent);
|
|
183
|
+
assert('WD all present → no findings', findings.length === 0, JSON.stringify(findings));
|
|
184
|
+
assert('WD all present → empty banner (silent)', wd.renderBanner(findings) === '');
|
|
185
|
+
}
|
|
186
|
+
// truth-gate missing → finding + STOP banner
|
|
187
|
+
{
|
|
188
|
+
const findings = wd.evaluateWatchdog({ ...allPresent, truthGateFileExists: false });
|
|
189
|
+
assert('WD truth-gate missing → finding', findings.some((f) => /truth-gate\.mjs is MISSING/.test(f)), JSON.stringify(findings));
|
|
190
|
+
const banner = wd.renderBanner(findings);
|
|
191
|
+
assert('WD truth-gate missing → STOP banner emitted', /TRUTH GATE WATCHDOG/.test(banner) && banner.length > 0);
|
|
192
|
+
}
|
|
193
|
+
// truth-gate unregistered on SubagentStop → finding
|
|
194
|
+
{
|
|
195
|
+
const findings = wd.evaluateWatchdog({ ...allPresent, truthGateOnSubagentStop: false });
|
|
196
|
+
assert('WD truth-gate not on SubagentStop → finding', findings.some((f) => /SubagentStop/.test(f)));
|
|
197
|
+
}
|
|
198
|
+
// exit gate missing → finding
|
|
199
|
+
{
|
|
200
|
+
const findings = wd.evaluateWatchdog({ ...allPresent, exitGateFileExists: false });
|
|
201
|
+
assert('WD exit-gate missing → finding', findings.some((f) => /work-item-exit-gate\.js is MISSING/.test(f)));
|
|
202
|
+
}
|
|
203
|
+
// LIVE hookify manifest absent → finding mentions plugin manifest, NOT dead wrappers as trusted
|
|
204
|
+
{
|
|
205
|
+
const findings = wd.evaluateWatchdog({ ...allPresent, liveHookifyManifestExists: false });
|
|
206
|
+
const f = findings.find((x) => /hookify/i.test(x));
|
|
207
|
+
assert('WD live hookify manifest absent → finding', Boolean(f), JSON.stringify(findings));
|
|
208
|
+
assert('WD finding names the LIVE plugin hooks.json (not dead wrappers as trusted)',
|
|
209
|
+
f && /plugins\/cache.*hookify.*hooks\.json/i.test(f) && /NOT the live path/i.test(f), f || '');
|
|
210
|
+
}
|
|
211
|
+
// dead wrapper points at stale cache → drift finding
|
|
212
|
+
{
|
|
213
|
+
const findings = wd.evaluateWatchdog({ ...allPresent, deadWrapperPointsAtStaleCache: true,
|
|
214
|
+
deadWrapperRoot: '/cache/hookify/OLDHASH', liveHookifyRoot: '/cache/hookify/NEWHASH' });
|
|
215
|
+
assert('WD stale-cache drift → finding', findings.some((f) => /ORPHANED cache hash/.test(f)));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// structural: gatherFacts inspects the LIVE plugins/cache hooks.json path, not the wrappers.
|
|
219
|
+
{
|
|
220
|
+
const home = mkdtempSync(join(tmpdir(), 'wd-home-'));
|
|
221
|
+
const repo = mkdtempSync(join(tmpdir(), 'wd-repo-'));
|
|
222
|
+
// Build a fake LIVE hookify plugin manifest under plugins/cache/.../hookify/<hash>/hooks/hooks.json
|
|
223
|
+
const live = join(home, '.claude', 'plugins', 'cache', 'official', 'hookify', 'NEWHASH', 'hooks');
|
|
224
|
+
mkdirSync(live, { recursive: true });
|
|
225
|
+
writeFileSync(join(live, 'hooks.json'), JSON.stringify({ hooks: {} }));
|
|
226
|
+
// Build a dead wrapper that points at a DIFFERENT (orphaned) hash.
|
|
227
|
+
const deadDir = join(home, '.claude', 'hooks');
|
|
228
|
+
mkdirSync(deadDir, { recursive: true });
|
|
229
|
+
writeFileSync(join(deadDir, 'hookify-stop.js'),
|
|
230
|
+
"const PLUGIN_ROOT = 'C:/cache/official/hookify/OLDHASH';\n");
|
|
231
|
+
// Minimal settings.json registering both gates so those facts are true.
|
|
232
|
+
mkdirSync(join(repo, '.claude', 'hooks'), { recursive: true });
|
|
233
|
+
writeFileSync(join(repo, '.claude', 'hooks', 'truth-gate.mjs'), '// stub');
|
|
234
|
+
writeFileSync(join(repo, '.claude', 'settings.json'), JSON.stringify({
|
|
235
|
+
hooks: {
|
|
236
|
+
PreToolUse: [{ hooks: [{ type: 'command', command: 'node hooks/work-item-exit-gate.js' }] }],
|
|
237
|
+
Stop: [{ hooks: [{ type: 'command', command: 'node .claude/hooks/truth-gate.mjs' }] }],
|
|
238
|
+
SubagentStop: [{ hooks: [{ type: 'command', command: 'node .claude/hooks/truth-gate.mjs' }] }],
|
|
239
|
+
},
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
const manifest = wd.findLiveHookifyManifest(home);
|
|
243
|
+
assert('WD findLiveHookifyManifest resolves the plugin hooks.json',
|
|
244
|
+
typeof manifest === 'string' && /plugins[\\/]cache.*hookify.*hooks\.json$/i.test(manifest.replace(/\\/g, '/')), manifest || 'null');
|
|
245
|
+
|
|
246
|
+
const facts = wd.gatherFacts({ home, repoRoot: repo, hooksDir: HOOKS });
|
|
247
|
+
assert('WD gatherFacts: live hookify manifest detected', facts.liveHookifyManifestExists === true);
|
|
248
|
+
assert('WD gatherFacts: dead wrapper stale-cache drift detected', facts.deadWrapperPointsAtStaleCache === true,
|
|
249
|
+
`dead=${facts.deadWrapperRoot} live=${facts.liveHookifyRoot}`);
|
|
250
|
+
assert('WD gatherFacts: truth-gate registered on Stop+SubagentStop',
|
|
251
|
+
facts.truthGateOnStop === true && facts.truthGateOnSubagentStop === true);
|
|
252
|
+
assert('WD gatherFacts: exit gate registered on PreToolUse', facts.exitGateRegistered === true);
|
|
253
|
+
|
|
254
|
+
rmSync(home, { recursive: true, force: true });
|
|
255
|
+
rmSync(repo, { recursive: true, force: true });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ===========================================================================
|
|
260
|
+
if (failures.length > 0) {
|
|
261
|
+
console.error('\nharness-gates tests — FAIL\n');
|
|
262
|
+
for (const f of failures) console.error(` - ${f}`);
|
|
263
|
+
process.exit(1);
|
|
264
|
+
}
|
|
265
|
+
console.log('\nharness-gates tests — PASS');
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Truth Gate 3.0 — Layer 3 (validator re-run receipt) tests for the closure gate.
|
|
4
|
+
*
|
|
5
|
+
* Exercises the FLAG-GATED Layer-3 check wired into work-item-exit-gate.js. The
|
|
6
|
+
* receipt store, running-sha probe, and secret are injected via verifyLayer3's
|
|
7
|
+
* `deps`, so the test runs fully offline (no DB, no brain, no clauth). The HMAC
|
|
8
|
+
* is computed with a fixed secret using the SAME canonical field order the gate
|
|
9
|
+
* mirrors from receipt.mjs.
|
|
10
|
+
*
|
|
11
|
+
* Branches proven:
|
|
12
|
+
* A. flag OFF → ALLOW as today (no-op, no receipt needed)
|
|
13
|
+
* B. flag ON + valid receipt (fresh nonce, pinned sha, unreplayed) → ALLOW
|
|
14
|
+
* C. flag ON + NO receipt → DENY
|
|
15
|
+
* D. flag ON + forged/tampered HMAC → DENY
|
|
16
|
+
* E. flag ON + stale-by-age receipt → DENY
|
|
17
|
+
* F. flag ON + replayed nonce (in durable seen-set) → DENY
|
|
18
|
+
* G. flag ON + wrong git_sha (not the running brain) → DENY
|
|
19
|
+
* H. flag ON + nonce_in_output:false (cached artifact) → DENY
|
|
20
|
+
* I. flag ON + witness:"agent" (self-witness) → DENY
|
|
21
|
+
* J. flag ON + INFRA: no secret → DENY ; no running sha → DENY
|
|
22
|
+
* K. truthGateFlagEnabled reads the env switch
|
|
23
|
+
*
|
|
24
|
+
* Run: node tests/work-item-exit-gate-l3.test.mjs
|
|
25
|
+
*/
|
|
26
|
+
import { resolve, dirname, join } from 'node:path';
|
|
27
|
+
import { fileURLToPath } from 'node:url';
|
|
28
|
+
import { createRequire } from 'node:module';
|
|
29
|
+
import crypto from 'node:crypto';
|
|
30
|
+
|
|
31
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
32
|
+
const REPO_ROOT = resolve(__dirname, '..');
|
|
33
|
+
const HOOK = join(REPO_ROOT, 'hooks', 'work-item-exit-gate.js');
|
|
34
|
+
|
|
35
|
+
const require = createRequire(import.meta.url);
|
|
36
|
+
const gate = require(HOOK);
|
|
37
|
+
|
|
38
|
+
const failures = [];
|
|
39
|
+
function assert(name, condition, detail = '') {
|
|
40
|
+
if (!condition) failures.push(`${name}${detail ? `: ${detail}` : ''}`);
|
|
41
|
+
else process.stdout.write(` ok ${name}\n`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const SECRET = 'test-secret-not-real';
|
|
45
|
+
const SHA = 'c3189c9d58a37d648e9de4a6bcd7d46772053eea';
|
|
46
|
+
const NOW = Date.parse('2026-06-25T12:00:00Z');
|
|
47
|
+
const SIGNED_FIELDS = gate.VALIDATOR_SIGNED_FIELDS;
|
|
48
|
+
|
|
49
|
+
function signValidator(receipt, secret = SECRET) {
|
|
50
|
+
const picked = {};
|
|
51
|
+
for (const k of SIGNED_FIELDS) picked[k] = receipt[k] ?? null;
|
|
52
|
+
return crypto.createHmac('sha256', secret).update(JSON.stringify(picked)).digest('hex');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A signed, otherwise-valid validator receipt; override fields to break it. */
|
|
56
|
+
function freshReceipt(over = {}) {
|
|
57
|
+
const r = {
|
|
58
|
+
claim: 'WP-4 validator re-run passes',
|
|
59
|
+
witness: 'validator-rerun',
|
|
60
|
+
git_sha: SHA,
|
|
61
|
+
nonce: 'vrr-fresh-abc123',
|
|
62
|
+
command: 'node scripts/needle-verify.mjs',
|
|
63
|
+
result: { exit_code: 0 },
|
|
64
|
+
nonce_in_output: true,
|
|
65
|
+
ts: new Date(NOW - 60_000).toISOString(), // 1 min old
|
|
66
|
+
...over,
|
|
67
|
+
};
|
|
68
|
+
r.hmac = signValidator(r);
|
|
69
|
+
return r;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const statusCall = { id: '11111111-2222-3333-4444-555555555555', actorSessionId: 'validator-x', actorRole: 'validator' };
|
|
73
|
+
const item = { id: statusCall.id, item_type: 'task', status: 'review', session_id: 'sess-origin' };
|
|
74
|
+
|
|
75
|
+
/** Build a deps object that drives verifyLayer3 fully offline. */
|
|
76
|
+
function deps({ flag = true, secret = SECRET, runningSha = SHA, receipts = [], seenNonces = [] } = {}) {
|
|
77
|
+
return {
|
|
78
|
+
flagEnabled: async () => flag,
|
|
79
|
+
getSecret: async () => secret,
|
|
80
|
+
getRunningSha: async () => runningSha,
|
|
81
|
+
loadReceiptRows: async () => receipts.map((p) => ({ payload: p })),
|
|
82
|
+
loadSeenNonces: async () => seenNonces,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Run verifyLayer3 and return the GateDenied reason, or null if it allowed. */
|
|
87
|
+
async function runL3(d) {
|
|
88
|
+
try {
|
|
89
|
+
await gate.verifyLayer3(statusCall, item, d, NOW);
|
|
90
|
+
return null; // ALLOW
|
|
91
|
+
} catch (e) {
|
|
92
|
+
if (e instanceof gate.GateDenied) return e.reason;
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
await (async () => {
|
|
98
|
+
// A. flag OFF → ALLOW as today (no receipt needed at all)
|
|
99
|
+
{
|
|
100
|
+
const r = await runL3(deps({ flag: false, receipts: [] }));
|
|
101
|
+
assert('A. flag OFF → ALLOW (no-op, no receipt)', r === null, r || '');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// B. flag ON + valid receipt → ALLOW
|
|
105
|
+
{
|
|
106
|
+
const r = await runL3(deps({ receipts: [freshReceipt()] }));
|
|
107
|
+
assert('B. flag ON + valid receipt → ALLOW', r === null, r || '');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// C. flag ON + NO receipt → DENY
|
|
111
|
+
{
|
|
112
|
+
const r = await runL3(deps({ receipts: [] }));
|
|
113
|
+
assert('C. flag ON + no receipt → DENY', r && /no validator re-run receipt found/.test(r), r || 'no denial');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// D. flag ON + forged/tampered HMAC → DENY
|
|
117
|
+
{
|
|
118
|
+
const bad = freshReceipt();
|
|
119
|
+
bad.claim = 'TAMPERED after signing'; // signature no longer matches
|
|
120
|
+
const r = await runL3(deps({ receipts: [bad] }));
|
|
121
|
+
assert('D. flag ON + forged HMAC → DENY', r && /HMAC invalid\/absent/.test(r), r || 'no denial');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// E. flag ON + stale-by-age receipt → DENY
|
|
125
|
+
{
|
|
126
|
+
const stale = freshReceipt({ ts: new Date(NOW - 60 * 60_000).toISOString() });
|
|
127
|
+
const r = await runL3(deps({ receipts: [stale] }));
|
|
128
|
+
assert('E. flag ON + stale receipt → DENY', r && /stale/.test(r), r || 'no denial');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// F. flag ON + replayed nonce (in durable seen-set) → DENY
|
|
132
|
+
{
|
|
133
|
+
const rec = freshReceipt();
|
|
134
|
+
const r = await runL3(deps({ receipts: [rec], seenNonces: [rec.nonce] }));
|
|
135
|
+
assert('F. flag ON + replayed nonce → DENY', r && /replayed|already consumed/.test(r), r || 'no denial');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// G. flag ON + wrong git_sha (not the running brain) → DENY
|
|
139
|
+
{
|
|
140
|
+
const r = await runL3(deps({ receipts: [freshReceipt()], runningSha: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' }));
|
|
141
|
+
assert('G. flag ON + wrong git_sha → DENY', r && /git_sha/.test(r), r || 'no denial');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// H. flag ON + nonce_in_output:false (cached artifact) → DENY
|
|
145
|
+
{
|
|
146
|
+
const r = await runL3(deps({ receipts: [freshReceipt({ nonce_in_output: false })] }));
|
|
147
|
+
assert('H. flag ON + nonce_in_output:false → DENY', r && /nonce_in_output|cached|replay/.test(r), r || 'no denial');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// I. flag ON + witness:"agent" (self-witness) → DENY
|
|
151
|
+
{
|
|
152
|
+
const r = await runL3(deps({ receipts: [freshReceipt({ witness: 'agent' })] }));
|
|
153
|
+
assert('I. flag ON + witness:agent → DENY', r && /witness/.test(r), r || 'no denial');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// I2. flag ON + result not a pass (exit_code:1) → DENY
|
|
157
|
+
{
|
|
158
|
+
const r = await runL3(deps({ receipts: [freshReceipt({ result: { exit_code: 1 } })] }));
|
|
159
|
+
assert('I2. flag ON + failing result → DENY', r && /pass/.test(r), r || 'no denial');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// J. INFRA fail-closed: no secret → DENY ; no running sha → DENY
|
|
163
|
+
{
|
|
164
|
+
const r1 = await runL3(deps({ secret: null, receipts: [freshReceipt()] }));
|
|
165
|
+
assert('J. flag ON + no secret → DENY (INFRA is a BLOCK)', r1 && /INFRA: no truth-gate HMAC secret/.test(r1), r1 || 'no denial');
|
|
166
|
+
const r2 = await runL3(deps({ runningSha: null, receipts: [freshReceipt()] }));
|
|
167
|
+
assert('J. flag ON + no running sha → DENY (INFRA is a BLOCK)', r2 && /INFRA: could not read the running brain git_sha/.test(r2), r2 || 'no denial');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// K. truthGateFlagEnabled reads the env switch (default OFF)
|
|
171
|
+
{
|
|
172
|
+
const prev = process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT;
|
|
173
|
+
delete process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT;
|
|
174
|
+
assert('K. flag default OFF', gate.truthGateFlagEnabled('require_validator_receipt') === false);
|
|
175
|
+
process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT = 'true';
|
|
176
|
+
assert('K. flag ON via env', gate.truthGateFlagEnabled('require_validator_receipt') === true);
|
|
177
|
+
process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT = '0';
|
|
178
|
+
assert('K. flag "0" is OFF', gate.truthGateFlagEnabled('require_validator_receipt') === false);
|
|
179
|
+
if (prev === undefined) delete process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT;
|
|
180
|
+
else process.env.RDC_TRUTHGATE_REQUIRE_VALIDATOR_RECEIPT = prev;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// L. pickNewestValidatorReceipt picks the latest by ts
|
|
184
|
+
{
|
|
185
|
+
const older = freshReceipt({ nonce: 'older', ts: new Date(NOW - 10 * 60_000).toISOString() });
|
|
186
|
+
const newer = freshReceipt({ nonce: 'newer', ts: new Date(NOW - 60_000).toISOString() });
|
|
187
|
+
const picked = gate.pickNewestValidatorReceipt([{ payload: older }, { payload: newer }]);
|
|
188
|
+
assert('L. picks newest receipt by ts', picked && picked.nonce === 'newer', picked ? picked.nonce : 'null');
|
|
189
|
+
}
|
|
190
|
+
})();
|
|
191
|
+
|
|
192
|
+
if (failures.length > 0) {
|
|
193
|
+
console.error('\nwork-item-exit-gate L3 tests — FAIL\n');
|
|
194
|
+
for (const f of failures) console.error(` - ${f}`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
console.log('\nwork-item-exit-gate L3 tests — PASS');
|