harness-everything 1.0.1 → 1.1.1

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.
Files changed (39) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +22 -0
  4. package/benchmarks/results/live-host/agy-2026-11-25/README.md +32 -0
  5. package/ci/mechanism-394-ledger-match.test.js +219 -0
  6. package/ci/mechanism-496-claude-capture.test.js +342 -0
  7. package/ci/mechanism-496-codex-capture.test.js +508 -0
  8. package/ci/mechanism-496-local-error-cli-edges.test.js +234 -0
  9. package/ci/mechanism-496-local-error-cli.test.js +517 -0
  10. package/ci/mechanism-496-local-error-store.test.js +731 -0
  11. package/ci/mechanism-496-opencode-capture.test.js +483 -0
  12. package/ci/mechanism-496-review-fixes.test.js +278 -0
  13. package/ci/mechanism-516-windows-verify-and-shell.test.js +328 -0
  14. package/docs/platform-capabilities.md +2 -2
  15. package/docs/platform-compatibility.json +2 -2
  16. package/hooks/scripts/codex-ledger-lesson.js +103 -27
  17. package/hooks/scripts/ledger-lesson.js +94 -23
  18. package/hooks/scripts/lib/ledger-match.js +39 -3
  19. package/hooks/scripts/lib/local-error-source.js +127 -0
  20. package/hooks/scripts/lib/local-errors.js +597 -0
  21. package/hooks/scripts/lib/observations.js +1 -1
  22. package/hooks/scripts/lib/workflow-isolation.js +1 -1
  23. package/opencode-plugin/index.mjs +86 -1
  24. package/opencode-plugin/plugin.json +1 -1
  25. package/package.json +2 -1
  26. package/plugins/harness-everything/.antigravity-plugin/plugin.json +1 -1
  27. package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
  28. package/plugins/harness-everything/hooks/scripts/codex-ledger-lesson.js +103 -27
  29. package/plugins/harness-everything/hooks/scripts/ledger-lesson.js +94 -23
  30. package/plugins/harness-everything/hooks/scripts/lib/ledger-match.js +39 -3
  31. package/plugins/harness-everything/hooks/scripts/lib/local-error-source.js +127 -0
  32. package/plugins/harness-everything/hooks/scripts/lib/local-errors.js +597 -0
  33. package/plugins/harness-everything/hooks/scripts/lib/observations.js +1 -1
  34. package/plugins/harness-everything/hooks/scripts/lib/workflow-isolation.js +1 -1
  35. package/plugins/harness-everything/plugin.json +1 -1
  36. package/plugins/harness-everything/scripts/lib/verification-contract.js +68 -12
  37. package/sbom.cdx.json +7 -7
  38. package/scripts/errors-local.js +357 -0
  39. package/scripts/lib/verification-contract.js +68 -12
@@ -0,0 +1,508 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const assert = require('node:assert/strict');
5
+ const { spawnSync } = require('node:child_process');
6
+ const fs = require('node:fs');
7
+ const os = require('node:os');
8
+ const path = require('node:path');
9
+ const test = require('node:test');
10
+
11
+ const ROOT = path.resolve(__dirname, '..');
12
+ const CANONICAL_ADAPTER = path.join(ROOT, 'hooks/scripts/codex-ledger-lesson.js');
13
+ const PACKAGED_ADAPTER = path.join(ROOT, 'plugins/harness-everything/hooks/scripts/codex-ledger-lesson.js');
14
+ const ERRORS_SYNC = path.join(ROOT, 'scripts/errors-sync.js');
15
+ const ERRORS_LOCAL = path.join(ROOT, 'scripts/errors-local.js');
16
+ const LEDGER = path.join(ROOT, '.agents/harness-everything/errors');
17
+ const E3_FILE = fs.readdirSync(LEDGER).find(file => /^E-0003-[a-z0-9-]+\.md$/.test(file));
18
+ const COMMITLINT_OUTPUT = fs.readFileSync(
19
+ path.join(__dirname, 'fixtures/394-ledger-match/commitlint-subject-case.txt'),
20
+ 'utf8'
21
+ );
22
+
23
+ // E-0011: canonicalize temp root with realpathSync.native to expand Windows 8.3 short paths.
24
+ const TMP = fs.realpathSync.native(fs.mkdtempSync(path.join(os.tmpdir(), '496-codex-capture-')));
25
+ let serial = 0;
26
+
27
+ test.after(() => fs.rmSync(TMP, { recursive: true, force: true }));
28
+
29
+ function isolatedEnv(caseDir, overrides = {}) {
30
+ const homeDir = path.join(caseDir, 'home');
31
+ const stateHome = path.join(caseDir, 'state-home');
32
+ const obsDir = path.join(caseDir, 'observations');
33
+ fs.mkdirSync(homeDir, { recursive: true });
34
+ fs.mkdirSync(stateHome, { recursive: true });
35
+ const env = {
36
+ ...process.env,
37
+ HOME: homeDir,
38
+ USERPROFILE: homeDir,
39
+ HARNESS_STATE_HOME: stateHome,
40
+ HARNESS_OBSERVATIONS_DIR: obsDir,
41
+ ...overrides
42
+ };
43
+ delete env.HARNESS_SYSTEM_ONE_MODE;
44
+ delete env.CLAUDE_PROJECT_DIR;
45
+ delete env.CODEX_PROJECT_DIR;
46
+ delete env.HARNESS_WORKSPACE_ROOT;
47
+ delete env.FABLE_WORKSPACE_ROOT;
48
+ return { env, homeDir, stateHome, obsDir };
49
+ }
50
+
51
+ function newCaseWorkspace(name, { withLedger = true, envOverrides = {} } = {}) {
52
+ const caseDir = path.join(TMP, `${name}-${++serial}`);
53
+ const workspaceRoot = path.join(caseDir, 'workspace');
54
+ fs.mkdirSync(path.join(workspaceRoot, '.git'), { recursive: true });
55
+ if (withLedger) {
56
+ const ledgerDir = path.join(workspaceRoot, '.agents/harness-everything/errors');
57
+ fs.mkdirSync(ledgerDir, { recursive: true });
58
+ fs.copyFileSync(path.join(LEDGER, E3_FILE), path.join(ledgerDir, E3_FILE));
59
+ const synced = spawnSync(process.execPath, [ERRORS_SYNC, '--root', workspaceRoot], {
60
+ cwd: ROOT,
61
+ encoding: 'utf8'
62
+ });
63
+ assert.equal(synced.status, 0, synced.stderr || 'errors-sync should succeed');
64
+ }
65
+ const envState = isolatedEnv(caseDir, envOverrides);
66
+ return { caseDir, workspaceRoot, ...envState };
67
+ }
68
+
69
+ function runHook(adapterPath, payload, env) {
70
+ return spawnSync(process.execPath, [adapterPath], {
71
+ cwd: ROOT,
72
+ env,
73
+ input: typeof payload === 'string' ? payload : JSON.stringify(payload),
74
+ encoding: 'utf8'
75
+ });
76
+ }
77
+
78
+ function runCli(args, cwd, env) {
79
+ return spawnSync(process.execPath, [ERRORS_LOCAL, ...args], {
80
+ cwd,
81
+ env,
82
+ encoding: 'utf8'
83
+ });
84
+ }
85
+
86
+ function listRecords(workspaceRoot, env, extraArgs = []) {
87
+ const res = runCli(['list', '--json', ...extraArgs], workspaceRoot, env);
88
+ assert.equal(res.status, 0, res.stderr || 'errors-local list should exit 0');
89
+ return JSON.parse(res.stdout);
90
+ }
91
+
92
+ function preToolPayload(workspaceRoot, overrides = {}) {
93
+ return {
94
+ hook_event_name: 'PreToolUse',
95
+ tool_name: 'Bash',
96
+ session_id: `sess-${serial}`,
97
+ turn_id: `turn-${serial}`,
98
+ tool_use_id: `toolu_${serial}_pre`,
99
+ cwd: workspaceRoot,
100
+ permission_mode: 'default',
101
+ tool_input: { command: 'npm test' },
102
+ ...overrides
103
+ };
104
+ }
105
+
106
+ function postToolPayload(workspaceRoot, overrides = {}) {
107
+ return {
108
+ hook_event_name: 'PostToolUse',
109
+ tool_name: 'Bash',
110
+ session_id: `sess-${serial}`,
111
+ turn_id: `turn-${serial}`,
112
+ tool_use_id: `toolu_${serial}_post`,
113
+ cwd: workspaceRoot,
114
+ permission_mode: 'default',
115
+ tool_input: { command: 'git commit -m "Bad Subject"' },
116
+ tool_response: {
117
+ exit_code: 1,
118
+ stdout: '',
119
+ stderr: COMMITLINT_OUTPUT
120
+ },
121
+ ...overrides
122
+ };
123
+ }
124
+
125
+ function runEndToEndA(adapterPath, label) {
126
+ const ctx = newCaseWorkspace(`e2e-a-${label}`);
127
+ const secretPath = path.join(ctx.homeDir, 'private-project', 'secret.txt');
128
+ const stderrWithHome = `${COMMITLINT_OUTPUT}\nsee ${secretPath}\n`;
129
+
130
+ const hookRes = runHook(
131
+ adapterPath,
132
+ postToolPayload(ctx.workspaceRoot, {
133
+ session_id: `e2e-a-session-${label}`,
134
+ tool_use_id: `toolu_e2e_a_${label}`,
135
+ tool_response: {
136
+ exit_code: 1,
137
+ stdout: '',
138
+ stderr: stderrWithHome
139
+ }
140
+ }),
141
+ ctx.env
142
+ );
143
+
144
+ assert.equal(hookRes.status, 0, hookRes.stderr || 'failure hook exits 0');
145
+ assert.equal(hookRes.stderr, '', 'failure hook stays silent on stderr');
146
+ const out = JSON.parse(hookRes.stdout);
147
+ assert.equal(out.hookSpecificOutput.hookEventName, 'PostToolUse');
148
+ assert.match(out.hookSpecificOutput.additionalContext, /Check the subject line against both rules/);
149
+
150
+ const records = listRecords(ctx.workspaceRoot, ctx.env);
151
+ assert.equal(records.length, 1, 'exactly one occurrence row captured');
152
+ const row = records[0];
153
+ assert.equal(row.kind, 'occurrence');
154
+ assert.equal(row.host, 'codex');
155
+ assert.equal(row.destination, 'unknown');
156
+ assert.equal(row.commandClass, 'git');
157
+ assert.equal(row.exitCode, 1);
158
+ assert.ok(row.sessionKey && !row.sessionKey.includes(`e2e-a-session-${label}`), 'sessionKey is opaque');
159
+ assert.ok(row.toolUseId && !row.toolUseId.includes(`toolu_e2e_a_${label}`), 'toolUseId is opaque');
160
+ assert.ok(row.outputExcerpt.includes('<home>'), 'home path is redacted in outputExcerpt');
161
+ assert.ok(!row.outputExcerpt.includes(ctx.homeDir), 'raw home path is not stored in outputExcerpt');
162
+ assert.ok(row.matchedLedgerIds.includes('E-0003'), `matchedLedgerIds should include E-0003, got ${JSON.stringify(row.matchedLedgerIds)}`);
163
+ }
164
+
165
+ function runEndToEndB(adapterPath, label) {
166
+ const ctx = newCaseWorkspace(`e2e-b-${label}`, { withLedger: false });
167
+ const actionLesson = 'Run npm run build before npm test in this project';
168
+ const failureLesson = 'Check failing test output before retrying npm test';
169
+
170
+ const addAction = runCli(
171
+ [
172
+ 'add',
173
+ '--executable', 'npm',
174
+ '--subcommand', 'test',
175
+ '--lesson', actionLesson,
176
+ '--destination', 'project'
177
+ ],
178
+ ctx.workspaceRoot,
179
+ ctx.env
180
+ );
181
+ assert.equal(addAction.status, 0, addAction.stderr || 'errors-local add action exits 0');
182
+
183
+ const preRes = runHook(
184
+ adapterPath,
185
+ preToolPayload(ctx.workspaceRoot, {
186
+ session_id: `e2e-b-session-${label}`,
187
+ tool_input: { command: 'npm test' }
188
+ }),
189
+ ctx.env
190
+ );
191
+ assert.equal(preRes.status, 0, preRes.stderr || 'PreToolUse hook exits 0');
192
+ assert.equal(preRes.stderr, '', 'PreToolUse hook stays silent on stderr');
193
+ assert.ok(preRes.stdout.trim(), 'PreToolUse hook emits provisional lesson context');
194
+ const preOut = JSON.parse(preRes.stdout);
195
+ assert.equal(preOut.hookSpecificOutput.hookEventName, 'PreToolUse');
196
+ assert.ok(preOut.hookSpecificOutput.additionalContext.includes(actionLesson), 'PreToolUse additionalContext contains provisional lesson text');
197
+ assert.match(preOut.hookSpecificOutput.additionalContext, /provisional/i, 'PreToolUse additionalContext labels lesson as provisional');
198
+ assert.doesNotMatch(preOut.hookSpecificOutput.additionalContext, /\b\d+\s*(?:times?|occurrences?|count|failures?)\b/i, 'no counts injected in PreToolUse lesson text');
199
+
200
+ // Also verify PostToolUse Bash failure phase delivers matching provisional failure lessons.
201
+ const addFailure = runCli(
202
+ [
203
+ 'add',
204
+ '--executable', 'git',
205
+ '--subcommand', 'commit',
206
+ '--diagnostic', 'subject-case',
207
+ '--lesson', failureLesson,
208
+ '--destination', 'project'
209
+ ],
210
+ ctx.workspaceRoot,
211
+ ctx.env
212
+ );
213
+ assert.equal(addFailure.status, 0, addFailure.stderr || 'errors-local add failure exits 0');
214
+
215
+ const postRes = runHook(
216
+ adapterPath,
217
+ postToolPayload(ctx.workspaceRoot, {
218
+ session_id: `e2e-b-session-${label}`,
219
+ tool_use_id: `toolu_e2e_b_post_${label}`
220
+ }),
221
+ ctx.env
222
+ );
223
+ assert.equal(postRes.status, 0);
224
+ assert.equal(postRes.stderr, '');
225
+ assert.ok(postRes.stdout.trim(), 'PostToolUse failure phase emits provisional failure lesson');
226
+ const postOut = JSON.parse(postRes.stdout);
227
+ assert.equal(postOut.hookSpecificOutput.hookEventName, 'PostToolUse');
228
+ assert.ok(postOut.hookSpecificOutput.additionalContext.includes(failureLesson), 'PostToolUse additionalContext contains provisional failure lesson text');
229
+ assert.match(postOut.hookSpecificOutput.additionalContext, /provisional/i);
230
+ assert.doesNotMatch(postOut.hookSpecificOutput.additionalContext, /\b\d+\s*(?:times?|occurrences?|count|failures?)\b/i);
231
+ }
232
+
233
+ test('Matrix 1: PostToolUse Bash confirmed failure captures one redacted occurrence with matchedLedgerIds', () => {
234
+ runEndToEndA(CANONICAL_ADAPTER, 'canonical');
235
+ });
236
+
237
+ test('Matrix 1b: confirmed failure without a matched lesson still captures occurrence with empty matchedLedgerIds and nullable exitCode', () => {
238
+ const ctx = newCaseWorkspace('unmatched-failure', { withLedger: false });
239
+ const hookRes = runHook(
240
+ CANONICAL_ADAPTER,
241
+ postToolPayload(ctx.workspaceRoot, {
242
+ session_id: 'unmatched-sess',
243
+ tool_use_id: 'toolu_unmatched',
244
+ tool_input: { command: 'npm test' },
245
+ tool_response: {
246
+ isError: true,
247
+ stderr: 'FAIL some test assertion failed'
248
+ }
249
+ }),
250
+ ctx.env
251
+ );
252
+ assert.equal(hookRes.status, 0);
253
+ assert.equal(hookRes.stdout, '', 'no matched lesson means stdout stays silent');
254
+ assert.equal(hookRes.stderr, '');
255
+
256
+ const records = listRecords(ctx.workspaceRoot, ctx.env);
257
+ assert.equal(records.length, 1, 'unmatched confirmed failure is still captured');
258
+ assert.equal(records[0].host, 'codex');
259
+ assert.equal(records[0].exitCode, null, 'exitCode is null when structured outcome has isError without integer exit code');
260
+ assert.deepEqual(records[0].matchedLedgerIds, []);
261
+ });
262
+
263
+ test('Matrix 2: CLI-added provisional lesson is delivered on PreToolUse and PostToolUse Bash without counts', () => {
264
+ runEndToEndB(CANONICAL_ADAPTER, 'canonical');
265
+ });
266
+
267
+ test('Matrix 3: unknown, success, masked-failure reminder, PreToolUse, and non-Bash outcomes do not capture rows', () => {
268
+ const ctx = newCaseWorkspace('no-capture-controls');
269
+
270
+ const unknownTextOnly = runHook(
271
+ CANONICAL_ADAPTER,
272
+ postToolPayload(ctx.workspaceRoot, {
273
+ session_id: 'ctrl-sess-1',
274
+ tool_use_id: 'toolu_unknown_str',
275
+ tool_response: `Process exited with code 1\n${COMMITLINT_OUTPUT}`
276
+ }),
277
+ ctx.env
278
+ );
279
+ assert.equal(unknownTextOnly.status, 0);
280
+ assert.equal(unknownTextOnly.stdout, '');
281
+
282
+ const unknownObj = runHook(
283
+ CANONICAL_ADAPTER,
284
+ postToolPayload(ctx.workspaceRoot, {
285
+ session_id: 'ctrl-sess-2',
286
+ tool_use_id: 'toolu_unknown_obj',
287
+ tool_response: { stderr: COMMITLINT_OUTPUT }
288
+ }),
289
+ ctx.env
290
+ );
291
+ assert.equal(unknownObj.status, 0);
292
+ assert.equal(unknownObj.stdout, '');
293
+
294
+ const successRes = runHook(
295
+ CANONICAL_ADAPTER,
296
+ postToolPayload(ctx.workspaceRoot, {
297
+ session_id: 'ctrl-sess-3',
298
+ tool_use_id: 'toolu_success',
299
+ tool_response: { exit_code: 0, stdout: '', stderr: COMMITLINT_OUTPUT }
300
+ }),
301
+ ctx.env
302
+ );
303
+ assert.equal(successRes.status, 0);
304
+ assert.equal(successRes.stdout, '');
305
+
306
+ // Masked-failure reminder exception delivers context on compound exit 0, but is NOT a confirmed failure for capture.
307
+ const maskedRes = runHook(
308
+ CANONICAL_ADAPTER,
309
+ postToolPayload(ctx.workspaceRoot, {
310
+ session_id: 'ctrl-sess-4',
311
+ tool_use_id: 'toolu_masked',
312
+ tool_input: { command: 'git commit -m "Bad Subject"; echo "$?"' },
313
+ tool_response: { exit_code: 0, stdout: COMMITLINT_OUTPUT, stderr: '' }
314
+ }),
315
+ ctx.env
316
+ );
317
+ assert.equal(maskedRes.status, 0);
318
+ assert.ok(maskedRes.stdout.trim(), 'masked-failure reminder is still delivered');
319
+
320
+ const preRes = runHook(
321
+ CANONICAL_ADAPTER,
322
+ preToolPayload(ctx.workspaceRoot, {
323
+ session_id: 'ctrl-sess-5',
324
+ tool_input: { command: 'git commit -m "Bad Subject"' }
325
+ }),
326
+ ctx.env
327
+ );
328
+ assert.equal(preRes.status, 0);
329
+ assert.ok(preRes.stdout.trim(), 'PreToolUse delivers repo lesson');
330
+
331
+ const nonBashFail = runHook(
332
+ CANONICAL_ADAPTER,
333
+ postToolPayload(ctx.workspaceRoot, {
334
+ session_id: 'ctrl-sess-6',
335
+ tool_name: 'Write',
336
+ tool_use_id: 'toolu_write_fail'
337
+ }),
338
+ ctx.env
339
+ );
340
+ assert.equal(nonBashFail.status, 0);
341
+ assert.equal(nonBashFail.stdout, '');
342
+
343
+ assert.equal(listRecords(ctx.workspaceRoot, ctx.env).length, 0, 'none of the negative controls created an occurrence row');
344
+
345
+ // Positive control in the same workspace confirms the oracle reaches the store.
346
+ const posRes = runHook(
347
+ CANONICAL_ADAPTER,
348
+ postToolPayload(ctx.workspaceRoot, {
349
+ session_id: 'ctrl-sess-pos',
350
+ tool_use_id: 'toolu_pos_ctrl'
351
+ }),
352
+ ctx.env
353
+ );
354
+ assert.equal(posRes.status, 0);
355
+ assert.equal(listRecords(ctx.workspaceRoot, ctx.env).length, 1, 'positive control captures exactly one row');
356
+ });
357
+
358
+ test('Matrix 4a: unwritable store leaves injected context identical to today and does not throw', () => {
359
+ const normalCtx = newCaseWorkspace('silent-normal');
360
+ const blockedCtx = newCaseWorkspace('silent-unwritable');
361
+ // Regular file at HARNESS_OBSERVATIONS_DIR blocks directory/store writes on all OSes.
362
+ fs.writeFileSync(blockedCtx.obsDir, 'not-a-directory', 'utf8');
363
+
364
+ const normalRes = runHook(
365
+ CANONICAL_ADAPTER,
366
+ postToolPayload(normalCtx.workspaceRoot, {
367
+ session_id: 'silent-session',
368
+ tool_use_id: 'toolu_silent_1'
369
+ }),
370
+ normalCtx.env
371
+ );
372
+ const blockedRes = runHook(
373
+ CANONICAL_ADAPTER,
374
+ postToolPayload(blockedCtx.workspaceRoot, {
375
+ session_id: 'silent-session',
376
+ tool_use_id: 'toolu_silent_1'
377
+ }),
378
+ blockedCtx.env
379
+ );
380
+
381
+ assert.equal(normalRes.status, 0);
382
+ assert.equal(blockedRes.status, 0);
383
+ assert.equal(normalRes.stderr, '');
384
+ assert.equal(blockedRes.stderr, '');
385
+ assert.ok(normalRes.stdout.trim(), 'lesson output is emitted in normal case');
386
+ assert.equal(normalRes.stdout, blockedRes.stdout, 'stdout with capture equals stdout when store is unwritable');
387
+
388
+ assert.equal(listRecords(normalCtx.workspaceRoot, normalCtx.env).length, 1);
389
+ assert.equal(listRecords(blockedCtx.workspaceRoot, blockedCtx.env).length, 0);
390
+ });
391
+
392
+ test('Matrix 4b: missing local-error-source and local-errors modules leave injected context identical to today and do not throw', () => {
393
+ const ctx = newCaseWorkspace('missing-modules');
394
+ const minimalDir = path.join(ctx.caseDir, 'minimal-plugin');
395
+ for (const [src, dest] of [
396
+ ['hooks/scripts/codex-ledger-lesson.js', 'hooks/scripts/codex-ledger-lesson.js'],
397
+ ['hooks/scripts/lib/ledger-match.js', 'hooks/scripts/lib/ledger-match.js'],
398
+ ['hooks/scripts/lib/codex-tool-outcome.js', 'hooks/scripts/lib/codex-tool-outcome.js'],
399
+ ['hooks/scripts/lib/harness-state.js', 'hooks/scripts/lib/harness-state.js'],
400
+ ['scripts/lib/workspace.js', 'hooks/scripts/lib/workspace.js']
401
+ ]) {
402
+ const target = path.join(minimalDir, dest);
403
+ fs.mkdirSync(path.dirname(target), { recursive: true });
404
+ fs.copyFileSync(path.join(ROOT, src), target);
405
+ }
406
+ const statePath = path.join(minimalDir, 'hooks/scripts/lib/harness-state.js');
407
+ fs.writeFileSync(
408
+ statePath,
409
+ fs.readFileSync(statePath, 'utf8').replace("require('../../../scripts/lib/workspace')", "require('./workspace')"),
410
+ 'utf8'
411
+ );
412
+ const minimalAdapter = path.join(minimalDir, 'hooks/scripts/codex-ledger-lesson.js');
413
+
414
+ const preNormal = runHook(CANONICAL_ADAPTER, preToolPayload(ctx.workspaceRoot, {
415
+ session_id: 'mod-pre-normal',
416
+ tool_input: { command: 'git commit -m "Bad Subject"' }
417
+ }), ctx.env);
418
+ const preMinimal = runHook(minimalAdapter, preToolPayload(ctx.workspaceRoot, {
419
+ session_id: 'mod-pre-minimal',
420
+ tool_input: { command: 'git commit -m "Bad Subject"' }
421
+ }), ctx.env);
422
+ assert.equal(preMinimal.status, 0);
423
+ assert.equal(preMinimal.stderr, '');
424
+ assert.equal(preMinimal.stdout, preNormal.stdout, 'PreToolUse stdout identical when provisional/error modules are absent');
425
+
426
+ const postNormal = runHook(CANONICAL_ADAPTER, postToolPayload(ctx.workspaceRoot, {
427
+ session_id: 'mod-post-normal',
428
+ tool_use_id: 'toolu_mod_1'
429
+ }), ctx.env);
430
+ const postMinimal = runHook(minimalAdapter, postToolPayload(ctx.workspaceRoot, {
431
+ session_id: 'mod-post-minimal',
432
+ tool_use_id: 'toolu_mod_2'
433
+ }), ctx.env);
434
+ assert.equal(postMinimal.status, 0);
435
+ assert.equal(postMinimal.stderr, '');
436
+ assert.equal(postMinimal.stdout, postNormal.stdout, 'PostToolUse stdout identical when provisional/error modules are absent');
437
+ });
438
+
439
+ test('Matrix 5: same tool_use_id or call_id is idempotent; distinct ids record two rows with matchedLedgerIds', () => {
440
+ const ctx = newCaseWorkspace('idempotency');
441
+ const sessionId = 'idem-session';
442
+
443
+ const first = runHook(
444
+ CANONICAL_ADAPTER,
445
+ postToolPayload(ctx.workspaceRoot, {
446
+ session_id: sessionId,
447
+ tool_use_id: 'toolu_same'
448
+ }),
449
+ ctx.env
450
+ );
451
+ assert.equal(first.status, 0);
452
+ assert.ok(first.stdout.trim(), 'first failure delivers lesson');
453
+
454
+ const dup = runHook(
455
+ CANONICAL_ADAPTER,
456
+ postToolPayload(ctx.workspaceRoot, {
457
+ session_id: sessionId,
458
+ tool_use_id: 'toolu_same'
459
+ }),
460
+ ctx.env
461
+ );
462
+ assert.equal(dup.status, 0);
463
+ assert.equal(dup.stdout, '', 'second delivery in same session is silent');
464
+ assert.equal(dup.stderr, '');
465
+ assert.equal(listRecords(ctx.workspaceRoot, ctx.env).length, 1, 'same tool_use_id is idempotent (1 row)');
466
+
467
+ const second = runHook(
468
+ CANONICAL_ADAPTER,
469
+ postToolPayload(ctx.workspaceRoot, {
470
+ session_id: sessionId,
471
+ tool_use_id: 'toolu_different'
472
+ }),
473
+ ctx.env
474
+ );
475
+ assert.equal(second.status, 0);
476
+ assert.equal(second.stdout, '', 'second distinct failure still has silent delivery');
477
+ assert.equal(second.stderr, '');
478
+
479
+ const afterSecond = listRecords(ctx.workspaceRoot, ctx.env);
480
+ assert.equal(afterSecond.length, 2, 'different tool_use_id creates a second row');
481
+ assert.ok(afterSecond[0].matchedLedgerIds.includes('E-0003'));
482
+ assert.ok(afterSecond[1].matchedLedgerIds.includes('E-0003'), 'second silent failure still records matchedLedgerIds');
483
+
484
+ // Also test host payloads supplying call_id instead of tool_use_id.
485
+ const ctxCall = newCaseWorkspace('idempotency-call-id');
486
+ const callFirst = postToolPayload(ctxCall.workspaceRoot, {
487
+ session_id: 'call-sess',
488
+ call_id: 'call_same'
489
+ });
490
+ delete callFirst.tool_use_id;
491
+ assert.equal(runHook(CANONICAL_ADAPTER, callFirst, ctxCall.env).status, 0);
492
+ assert.equal(runHook(CANONICAL_ADAPTER, callFirst, ctxCall.env).status, 0);
493
+ assert.equal(listRecords(ctxCall.workspaceRoot, ctxCall.env).length, 1, 'same call_id is idempotent');
494
+
495
+ const callSecond = postToolPayload(ctxCall.workspaceRoot, {
496
+ session_id: 'call-sess',
497
+ call_id: 'call_distinct'
498
+ });
499
+ delete callSecond.tool_use_id;
500
+ assert.equal(runHook(CANONICAL_ADAPTER, callSecond, ctxCall.env).status, 0);
501
+ assert.equal(listRecords(ctxCall.workspaceRoot, ctxCall.env).length, 2, 'distinct call_id creates a second row');
502
+ });
503
+
504
+ test('Matrix 6: packaged copy passes End-to-end A and End-to-end B', () => {
505
+ assert.ok(fs.existsSync(PACKAGED_ADAPTER), 'packaged codex-ledger-lesson.js exists');
506
+ runEndToEndA(PACKAGED_ADAPTER, 'packaged');
507
+ runEndToEndB(PACKAGED_ADAPTER, 'packaged');
508
+ });