harness-everything 1.2.2 → 1.3.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 (147) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +29 -0
  4. package/ci/archive/325-prep/README.md +9 -2
  5. package/ci/archive/325-prep/doc-reconciliation-red.js +34 -8
  6. package/ci/fixtures/468-dispatch/agy-quota-PROVENANCE.md +19 -0
  7. package/ci/fixtures/468-dispatch/agy-quota-direct-individual.stderr +2 -0
  8. package/ci/fixtures/468-dispatch/agy-quota-direct-third-party.stderr +2 -0
  9. package/ci/fixtures/468-dispatch/agy-quota-exec-individual.log +3 -0
  10. package/ci/fixtures/553-t2a/body-edit/base/.agents/harness-everything/project-docs.json +11 -0
  11. package/ci/fixtures/553-t2a/body-edit/base/docs/a.md +6 -0
  12. package/ci/fixtures/553-t2a/body-edit/base/docs/b.md +1 -0
  13. package/ci/fixtures/553-t2a/body-edit/candidate/docs/a.md +6 -0
  14. package/ci/fixtures/553-t2c/derived-regenerate-must/base/docs/adr/EVID-001-verify-all-evidence.md +11 -0
  15. package/ci/fixtures/553-t2c/derived-regenerate-must/base/docs/governed-index.md +19 -0
  16. package/ci/fixtures/553-t2c/derived-regenerate-must/candidate/docs/adr/EVID-001-verify-all-evidence.md +11 -0
  17. package/ci/fixtures/553-t2c/derived-regenerate-must/candidate/docs/governed-index.md +19 -0
  18. package/ci/fixtures/553-t2c/derived-regenerate-must/config.json +17 -0
  19. package/ci/fixtures/doc-obligations/derive/regression-pins.json +153 -0
  20. package/ci/fixtures/state-persist-post-tool-use/harness.js +206 -0
  21. package/ci/fixtures/state-persist-post-tool-use/off-handoff-golden.json +16 -0
  22. package/ci/fixtures/state-persist-post-tool-use/payload-post-tool-use-success.json +13 -0
  23. package/ci/fixtures/stop/fail-session-writes.js +109 -0
  24. package/ci/fixtures/workflow-contract/cases.json +3355 -0
  25. package/ci/fixtures/workflow-contract/wiring-cases.json +560 -0
  26. package/ci/lib/doc-reconciliation-prep-fixtures.js +4 -9
  27. package/ci/mechanism-2an-workflow-replay.test.js +324 -0
  28. package/ci/mechanism-2ax-doc-lifecycle.test.js +132 -0
  29. package/ci/mechanism-325-3a-doc-evidence.test.js +73 -0
  30. package/ci/mechanism-325-3a-doc-obligations.test.js +61 -0
  31. package/ci/mechanism-325-3b-preparation.test.js +208 -49
  32. package/ci/mechanism-325-3b-s1-review.test.js +20 -828
  33. package/ci/mechanism-325-3b-s3-providers.test.js +59 -0
  34. package/ci/mechanism-325-f3-derived-views.test.js +140 -0
  35. package/ci/mechanism-341-opencode-lane.test.js +164 -284
  36. package/ci/mechanism-35-verification-contract.test.js +246 -0
  37. package/ci/mechanism-365-step-edit-accounting.test.js +5 -6
  38. package/ci/mechanism-383-doc-glob-line-terminators.test.js +0 -30
  39. package/ci/mechanism-397-opencode-observations.test.js +151 -18
  40. package/ci/mechanism-468-dispatch-cli.test.js +1496 -4
  41. package/ci/mechanism-548-bigint-identity.test.js +531 -0
  42. package/ci/mechanism-553-e1-unhook-credit.test.js +328 -0
  43. package/ci/mechanism-553-p2a-claude-hooks.test.js +98 -0
  44. package/ci/mechanism-553-p3-plan-start.test.js +212 -0
  45. package/ci/mechanism-553-state-persist-post-tool-use.test.js +307 -0
  46. package/ci/mechanism-553-stop.test.js +427 -0
  47. package/ci/mechanism-553-t2a-derive.test.js +110 -0
  48. package/ci/mechanism-553-t2c-derive.test.js +94 -0
  49. package/ci/mechanism-e1-derived-regenerate.test.js +45 -206
  50. package/ci/mechanism-error-ledger-oracle.test.js +786 -3
  51. package/ci/mechanism-error-ledger-scan.test.js +981 -0
  52. package/ci/mechanism-outline-sanitizer.test.js +0 -8
  53. package/ci/mechanism-pr-a-conflict-check.test.js +38 -305
  54. package/ci/mechanism-prb-outline-review.test.js +101 -3283
  55. package/ci/mechanism-report-writer.test.js +45 -0
  56. package/docs/adr/DOCS-006-reconciliation-pilot.md +4 -2
  57. package/docs/adr/DOCS-008-obligation-abstraction-bounded-consumer.md +3 -3
  58. package/docs/doc-workflow-reconciliation.md +17 -2
  59. package/docs/document-lifecycle.md +6 -2
  60. package/docs/error-handling/details/c8-dispatch-cli.md +109 -13
  61. package/docs/platform-capabilities.md +35 -5
  62. package/error-ledger-bootstrap/SKILL.md +1 -1
  63. package/error-ledger-bootstrap/references/workflow.md +42 -7
  64. package/error-ledger-bootstrap/scripts/promote.js +8 -6
  65. package/error-ledger-bootstrap/scripts/scan.js +525 -12
  66. package/hooks/hooks.json +0 -46
  67. package/hooks/scripts/lib/workflow-obligations.js +0 -55
  68. package/hooks/scripts/state-persist.js +7 -95
  69. package/hooks/scripts/workflow-disposition.js +0 -2
  70. package/hooks/scripts/workflow-stop-gate.js +17 -30
  71. package/opencode-plugin/index.mjs +6 -70
  72. package/opencode-plugin/plugin.json +1 -1
  73. package/package.json +1 -1
  74. package/plugins/harness-everything/.antigravity-plugin/plugin.json +1 -1
  75. package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
  76. package/plugins/harness-everything/hooks/scripts/lib/workflow-obligations.js +0 -55
  77. package/plugins/harness-everything/hooks/scripts/state-persist.js +7 -95
  78. package/plugins/harness-everything/hooks/scripts/workflow-disposition.js +0 -2
  79. package/plugins/harness-everything/hooks/scripts/workflow-stop-gate.js +17 -30
  80. package/plugins/harness-everything/plugin.json +1 -1
  81. package/plugins/harness-everything/skills/error-ledger-bootstrap/SKILL.md +1 -1
  82. package/plugins/harness-everything/skills/error-ledger-bootstrap/references/workflow.md +42 -7
  83. package/plugins/harness-everything/skills/error-ledger-bootstrap/scripts/promote.js +8 -6
  84. package/plugins/harness-everything/skills/error-ledger-bootstrap/scripts/scan.js +525 -12
  85. package/sbom.cdx.json +7 -7
  86. package/scripts/dispatch.js +499 -87
  87. package/scripts/error-ledger-oracle.js +521 -27
  88. package/scripts/errors-sync.js +4 -3
  89. package/scripts/report-writer.js +4 -3
  90. package/ci/fixtures/doc-obligations/reconciliation-s1/cases.json +0 -6854
  91. package/ci/fixtures/doc-obligations/reconciliation-s1/fail-session-writes.js +0 -116
  92. package/ci/fixtures/doc-obligations/reconciliation-s2a/harness.js +0 -507
  93. package/ci/fixtures/doc-obligations/reconciliation-s2a/run13-workflow-requirements.json +0 -42
  94. package/ci/fixtures/doc-obligations/reconciliation-s2a/vectors.json +0 -617
  95. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/harness.js +0 -415
  96. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-post-tool-use-success-minimal.json +0 -9
  97. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-subagent-start.json +0 -7
  98. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-subagent-stop-freetext-reject.json +0 -11
  99. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-subagent-stop-reject.json +0 -11
  100. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-subagent-stop.json +0 -11
  101. package/ci/fixtures/doc-obligations/reconciliation-s2b/capture/payload-task-result.json +0 -19
  102. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/off-capture-only.json +0 -99
  103. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/off-legacy.json +0 -67
  104. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-concurrent-writers.json +0 -62
  105. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-lookalikes-runtime-mutations.json +0 -67
  106. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-malformed.json +0 -85
  107. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-normal-session.json +0 -74
  108. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-runtime-drift-later-npm-test.json +0 -58
  109. package/ci/fixtures/doc-obligations/reconciliation-s2b/corpus/on-valid-direct-quoted-review.json +0 -85
  110. package/ci/fixtures/doc-obligations/reconciliation-s2b/f4/cases.json +0 -2267
  111. package/ci/fixtures/doc-obligations/reconciliation-s2b/wiring/cases.json +0 -1110
  112. package/ci/lib/doc-reconciliation-file-spy.js +0 -23
  113. package/ci/lib/doc-reconciliation-qualification-preload.js +0 -37
  114. package/ci/lib/doc-reconciliation-replay.js +0 -104
  115. package/ci/lib/doc-reconciliation-staged-tree.js +0 -102
  116. package/ci/mechanism-325-3b-h1-qualification.test.js +0 -206
  117. package/ci/mechanism-325-3b-s1.test.js +0 -188
  118. package/ci/mechanism-325-3b-s2a-core.test.js +0 -840
  119. package/ci/mechanism-325-3b-s2a-producer.test.js +0 -564
  120. package/ci/mechanism-325-3b-s2b-capture.test.js +0 -566
  121. package/ci/mechanism-325-3b-s2b-wiring.test.js +0 -679
  122. package/ci/mechanism-325-f4-invariants.test.js +0 -606
  123. package/ci/mechanism-325-f4-projections.test.js +0 -392
  124. package/ci/mechanism-325-f4-qualification.test.js +0 -228
  125. package/ci/mechanism-325-f4-trust-boundary.test.js +0 -880
  126. package/ci/mechanism-e1-dependency-artifact-scope.test.js +0 -657
  127. package/hooks/scripts/lib/doc-reconciliation-capture.sanitized.jsonl +0 -26
  128. package/hooks/scripts/lib/doc-reconciliation-core.js +0 -336
  129. package/hooks/scripts/lib/doc-reconciliation-events.js +0 -292
  130. package/hooks/scripts/lib/doc-reconciliation-outline-capture.sanitized.jsonl +0 -24
  131. package/hooks/scripts/lib/doc-reconciliation-pilot.js +0 -482
  132. package/hooks/scripts/lib/doc-reconciliation-producer.js +0 -468
  133. package/hooks/scripts/lib/doc-reconciliation-provider.js +0 -3304
  134. package/hooks/scripts/lib/doc-reconciliation-qualification.json +0 -54
  135. package/hooks/scripts/w6-state-operations.js +0 -239
  136. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-capture.sanitized.jsonl +0 -26
  137. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-core.js +0 -336
  138. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-events.js +0 -292
  139. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-outline-capture.sanitized.jsonl +0 -24
  140. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-pilot.js +0 -482
  141. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-producer.js +0 -468
  142. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-provider.js +0 -3304
  143. package/plugins/harness-everything/hooks/scripts/lib/doc-reconciliation-qualification.json +0 -54
  144. package/plugins/harness-everything/hooks/scripts/w6-state-operations.js +0 -239
  145. package/scripts/system-one-eval-laya.js +0 -112
  146. /package/ci/fixtures/{doc-obligations/reconciliation-s2b/capture → state-persist-post-tool-use}/payload-post-tool-use-failure.json +0 -0
  147. /package/ci/fixtures/{doc-obligations/reconciliation-s2b/wiring → workflow-contract}/legacy-off-golden.json +0 -0
@@ -0,0 +1,307 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ // General state-persist PostToolUse compatibility controls, moved here from the
5
+ // S2b capture/provider suite when the E1 pilot was retired (issue #553, T2-H).
6
+ // These are ordinary state-persist outcomes: retired manifest callbacks stay
7
+ // quiet, the legacy handoff golden survives unset/0/unknown flag values, and
8
+ // ordinary success/failure posts do not depend on the retired pilot switch.
9
+ //
10
+ // Every assertion runs on both legs (canonical hooks/scripts and the packaged
11
+ // plugins/harness-everything copy) against the real state-persist.js entry.
12
+ // This suite does not require any E1 runtime module at load time.
13
+
14
+ const { spawnSync } = require('child_process');
15
+ const fs = require('fs');
16
+ const os = require('os');
17
+ const path = require('path');
18
+ const H = require('./fixtures/state-persist-post-tool-use/harness');
19
+
20
+ const ROOT = path.resolve(__dirname, '..');
21
+ const PILOT_SWITCH = 'HARNESS_DOC_RECONCILIATION_PILOT';
22
+
23
+ const runner = H.createRunner('state-persist PostToolUse');
24
+ const GOLDEN = H.golden();
25
+
26
+ function legacyGoldenChecks(id, drive) {
27
+ if (drive.code !== 0) throw new Error(`state-persist exited ${drive.code}: ${(drive.stderr || drive.stdout).slice(0, 200)}`);
28
+ const names = Object.keys(drive.files);
29
+ for (const rel of names) {
30
+ if (!rel.endsWith('/handoff-state.json')) throw new Error(`OFF created an unexpected session file: ${rel}`);
31
+ }
32
+ const handoffRels = names.filter(rel => rel.endsWith('/handoff-state.json'));
33
+ if (handoffRels.length !== 1) throw new Error(`OFF expected exactly one handoff-state.json, got ${handoffRels.length}`);
34
+ const state = JSON.parse(drive.files[handoffRels[0]]);
35
+ const keys = Object.keys(state).sort();
36
+ if (JSON.stringify(keys) !== JSON.stringify([...GOLDEN.keys].sort())) {
37
+ throw new Error(`legacy handoff keyset changed: got ${JSON.stringify(keys)} expected ${JSON.stringify(GOLDEN.keys)}`);
38
+ }
39
+ for (const [key, expected] of Object.entries(GOLDEN.values)) {
40
+ if (state[key] !== expected) throw new Error(`legacy handoff ${key} changed: got ${state[key]} expected ${expected}`);
41
+ }
42
+ const captures = H.findCaptureRecords(drive.files);
43
+ if (captures.length) throw new Error(`legacy post wrote an E1 capture record: ${JSON.stringify(captures[0]).slice(0, 200)}`);
44
+ }
45
+
46
+ // Old manifest callbacks are ignored regardless of the retired flag value.
47
+ // Drive the real source/package hook with an isolated state home and assert
48
+ // no E1 load, state write, output or nonzero exit.
49
+ const CAPTURE_ONLY_ROUTES = [
50
+ { id: 'pre-bash', event: 'PreToolUse', tool: 'Bash', input: { command: 'echo f8-probe' } },
51
+ { id: 'pre-powershell', event: 'PreToolUse', tool: 'PowerShell', input: { command: 'Write-Output f8-probe' } },
52
+ { id: 'pre-task', event: 'PreToolUse', tool: 'Task', input: { description: 'F8 capture probe' } },
53
+ { id: 'pre-agent', event: 'PreToolUse', tool: 'Agent', input: { description: 'F8 capture probe' } },
54
+ { id: 'post-task', event: 'PostToolUse', tool: 'Task', input: { description: 'F8 capture probe' } },
55
+ { id: 'post-agent', event: 'PostToolUse', tool: 'Agent', input: { description: 'F8 capture probe' } },
56
+ { id: 'subagent-start', event: 'SubagentStart' },
57
+ { id: 'subagent-stop', event: 'SubagentStop' },
58
+ ];
59
+ const RETIRED_SWITCH_VALUES = [
60
+ { id: 'unset' },
61
+ { id: 'zero', value: '0' },
62
+ { id: 'one', value: '1' },
63
+ { id: 'other', value: 'true' },
64
+ ];
65
+ const ORDINARY_SWITCH_VALUES = RETIRED_SWITCH_VALUES.slice(0, 3);
66
+
67
+ function snapshotTree(root) {
68
+ const files = {};
69
+ function visit(dir) {
70
+ if (!fs.existsSync(dir)) return;
71
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
72
+ const abs = path.join(dir, entry.name);
73
+ if (entry.isDirectory()) visit(abs);
74
+ else if (entry.isFile()) files[path.relative(root, abs).split(path.sep).join('/')] = fs.readFileSync(abs, 'utf8');
75
+ }
76
+ }
77
+ visit(root);
78
+ return files;
79
+ }
80
+
81
+ function snapshotSessionTree(root, sessionId) {
82
+ const safeId = String(sessionId || 'default').replace(/[^A-Za-z0-9._-]+/g, '_').replace(/^\.+/, '');
83
+ const allFiles = snapshotTree(root);
84
+ return Object.fromEntries(Object.entries(allFiles)
85
+ .filter(([name]) => name.split('/').includes(safeId)));
86
+ }
87
+
88
+ function captureEventRecords(files) {
89
+ return Object.entries(files)
90
+ .filter(([name]) => /(?:^|\/)doc-reconciliation-capture\/\d{8}\.json$/.test(name))
91
+ .map(([, content]) => JSON.parse(content));
92
+ }
93
+
94
+ function driveStatePersistPayload(leg, inputPayload, switchSetting) {
95
+ const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'f10-f8-capture-'));
96
+ const stateHome = path.join(temp, 'state-home');
97
+ const wsRoot = path.join(temp, 'workspace');
98
+ fs.mkdirSync(stateHome, { recursive: true });
99
+ fs.mkdirSync(wsRoot, { recursive: true });
100
+
101
+ const payload = JSON.parse(JSON.stringify(inputPayload));
102
+ payload.cwd = wsRoot;
103
+ const statePersist = path.join(leg.hooksScripts, 'state-persist.js');
104
+ const sentinel = path.join(temp, 'e1-module-loads.txt');
105
+ const preload = path.join(temp, 'e1-load-sentinel.js');
106
+ fs.writeFileSync(preload, [
107
+ "'use strict';",
108
+ "const fs = require('fs');",
109
+ "const path = require('path');",
110
+ "const Module = require('module');",
111
+ "const statePersist = path.resolve(process.env.HARNESS_553_STATE_PERSIST);",
112
+ "const sentinel = process.env.HARNESS_553_E1_SENTINEL;",
113
+ "const original = Module._load;",
114
+ "Module._load = function (request, parent, isMain) {",
115
+ " if (parent?.filename && path.resolve(parent.filename) === statePersist &&",
116
+ " (request === './lib/doc-reconciliation-pilot' || request === './lib/doc-reconciliation-provider')) {",
117
+ " fs.appendFileSync(sentinel, 'state-persist.js -> ' + request + '\\n');",
118
+ " }",
119
+ " return Reflect.apply(original, this, [request, parent, isMain]);",
120
+ "};",
121
+ ].join('\n') + '\n', 'utf8');
122
+
123
+ const env = {
124
+ ...process.env,
125
+ CLAUDE: '1',
126
+ HOME: temp,
127
+ USERPROFILE: temp,
128
+ HARNESS_STATE_HOME: stateHome,
129
+ HARNESS_WORKSPACE_ROOT: wsRoot,
130
+ };
131
+ delete env.NODE_OPTIONS;
132
+ if (switchSetting.value === undefined) delete env[PILOT_SWITCH];
133
+ else env[PILOT_SWITCH] = switchSetting.value;
134
+ env.HARNESS_553_STATE_PERSIST = statePersist;
135
+ env.HARNESS_553_E1_SENTINEL = sentinel;
136
+
137
+ try {
138
+ const result = spawnSync(process.execPath, ['--require', preload, statePersist], {
139
+ input: JSON.stringify(payload),
140
+ encoding: 'utf8',
141
+ cwd: wsRoot,
142
+ env,
143
+ timeout: 30000,
144
+ });
145
+ if (result.error) throw new H.InfraError('capture-only hook subprocess failed: ' + result.error.message);
146
+ return {
147
+ code: result.status,
148
+ stdout: result.stdout || '',
149
+ stderr: result.stderr || '',
150
+ files: snapshotSessionTree(stateHome, payload.session_id),
151
+ allFiles: snapshotTree(stateHome),
152
+ payload,
153
+ e1Loads: fs.existsSync(sentinel) ? fs.readFileSync(sentinel, 'utf8').trim().split(/\r?\n/) : [],
154
+ };
155
+ } finally {
156
+ fs.rmSync(temp, { recursive: true, force: true });
157
+ }
158
+ }
159
+
160
+ function driveCaptureOnlyRoute(leg, route, switchSetting) {
161
+ const payload = {
162
+ hook_event_name: route.event,
163
+ session_id: 'f8-' + route.id,
164
+ };
165
+ if (route.tool) {
166
+ payload.tool_name = route.tool;
167
+ payload.tool_use_id = 'toolu-' + route.id;
168
+ payload.tool_input = route.input;
169
+ if (route.event === 'PostToolUse') {
170
+ payload.tool_response = { agentId: 'agent-f8', interrupted: false, result: 'completed' };
171
+ }
172
+ }
173
+ if (route.event === 'SubagentStart' || route.event === 'SubagentStop') {
174
+ payload.agent_id = 'agent-f8';
175
+ payload.agent_type = 'doc-reviewer';
176
+ }
177
+ if (route.event === 'SubagentStop') {
178
+ payload.last_assistant_message = 'Review verdict: approve. F8 capture probe.';
179
+ }
180
+ return driveStatePersistPayload(leg, payload, switchSetting);
181
+ }
182
+
183
+ // locks: historical — the old F8 preload-spy predicate is retired; current
184
+ // path checks assert callback side effects and provider non-loading directly.
185
+ runner.run('P2B-553-OLD-MANIFEST-CAPTURE-ONLY-QUIET', [], (_mods, leg) => {
186
+ const routeSwitchCases = CAPTURE_ONLY_ROUTES.length * RETIRED_SWITCH_VALUES.length;
187
+ let stateChangeCases = 0;
188
+ let outputCases = 0;
189
+ let captureCases = 0;
190
+ let badExitCases = 0;
191
+ let providerLoadCases = 0;
192
+ for (const route of CAPTURE_ONLY_ROUTES) {
193
+ for (const switchSetting of RETIRED_SWITCH_VALUES) {
194
+ const result = driveCaptureOnlyRoute(leg, route, switchSetting);
195
+ if (result.code !== 0) badExitCases += 1;
196
+ if (result.stdout || result.stderr) outputCases += 1;
197
+ if (Object.keys(result.allFiles).length) stateChangeCases += 1;
198
+ if (captureEventRecords(result.files).length) captureCases += 1;
199
+ if (result.e1Loads.length) providerLoadCases += 1;
200
+ }
201
+ }
202
+ console.log(' SENTINEL ' + path.relative(ROOT, path.join(leg.hooksScripts, 'state-persist.js'))
203
+ + ': E1 provider load in ' + providerLoadCases + '/' + routeSwitchCases + ' callback cases');
204
+ const failures = [];
205
+ if (badExitCases) failures.push('nonzero exit in ' + badExitCases + '/' + routeSwitchCases + ' retired callback cases');
206
+ if (outputCases) failures.push('output emitted in ' + outputCases + '/' + routeSwitchCases + ' retired callback cases');
207
+ if (stateChangeCases) failures.push('state changed in ' + stateChangeCases + '/' + routeSwitchCases + ' retired callback cases');
208
+ if (captureCases) failures.push('capture written in ' + captureCases + '/' + routeSwitchCases + ' retired callback cases');
209
+ if (providerLoadCases) failures.push('E1 provider loaded from state-persist in ' + providerLoadCases + '/' + routeSwitchCases + ' retired callback cases');
210
+ if (failures.length) failures.push(`observed side effects: state=${stateChangeCases}, capture=${captureCases}, output=${outputCases}, nonzero-exit=${badExitCases}`);
211
+ if (failures.length) throw new Error(failures.join('; '));
212
+ });
213
+
214
+ // ---- legacy post retains the handoff golden under retired flag values ----
215
+
216
+ runner.run('P2B-553-LEGACY-GOLDEN-UNSET', [], (_mods, leg) => {
217
+ legacyGoldenChecks('P2B-553-LEGACY-GOLDEN-UNSET', H.driveStatePersist(leg, 'payload-post-tool-use-success.json', {}));
218
+ });
219
+
220
+ runner.run('P2B-553-LEGACY-GOLDEN-ZERO', [], (_mods, leg) => {
221
+ legacyGoldenChecks('P2B-553-LEGACY-GOLDEN-ZERO', H.driveStatePersist(leg, 'payload-post-tool-use-success.json', {}, { env: { [PILOT_SWITCH]: '0' } }));
222
+ });
223
+
224
+ runner.run('P2B-553-LEGACY-GOLDEN-OTHER', [], (_mods, leg) => {
225
+ // Unknown legacy flag values are ignored too.
226
+ legacyGoldenChecks('P2B-553-LEGACY-GOLDEN-OTHER', H.driveStatePersist(leg, 'payload-post-tool-use-success.json', {}, { env: { [PILOT_SWITCH]: 'true' } }));
227
+ });
228
+
229
+ // ---- ordinary post outcomes ignore the retired pilot switch -----------
230
+
231
+ function assertLegacyPost(drive, label, expectedStatus) {
232
+ if (drive.code !== 0) throw new Error(label + ': state-persist exited ' + drive.code);
233
+ if (drive.stdout || drive.stderr) throw new Error(label + ': hook emitted output');
234
+ if (drive.e1Loads.length) throw new Error(label + ': E1 provider loaded: ' + drive.e1Loads.join('; '));
235
+ const names = Object.keys(drive.files);
236
+ if (names.length !== 1 || !names[0].endsWith('/handoff-state.json')) {
237
+ throw new Error(label + ': unexpected state files: ' + JSON.stringify(names));
238
+ }
239
+ if (H.findCaptureRecords(drive.allFiles).length) throw new Error(label + ': E1 capture record was written');
240
+ if (Object.keys(drive.allFiles).some(name => /doc-reconciliation-capture/.test(name))) {
241
+ throw new Error(label + ': E1 journal file was written');
242
+ }
243
+ const state = JSON.parse(drive.files[names[0]]);
244
+ const creditKeys = Object.keys(state).filter(key => /^last(?:Doc|Generic)Verify/.test(key));
245
+ if (creditKeys.length) throw new Error(label + ': E1 verification fields were written: ' + creditKeys.join(','));
246
+ if (state.status !== expectedStatus) throw new Error(label + ': expected status ' + expectedStatus + ', got ' + state.status);
247
+ if (expectedStatus === 'idle' && !Number.isFinite(state.lastVerifyAt)) {
248
+ throw new Error(label + ': successful verification did not update generic lastVerifyAt');
249
+ }
250
+ if (expectedStatus === 'failed' && !String(state.errorSummary || '').includes('r2c1 simulated failure')) {
251
+ throw new Error(label + ': ordinary failure detail was lost: ' + JSON.stringify(state));
252
+ }
253
+ if (expectedStatus === 'failed' && (state.lastVerifyAt !== undefined || state.lastVerifyExitCode !== undefined)) {
254
+ throw new Error(label + ': failed post was recorded as successful verification');
255
+ }
256
+ return state;
257
+ }
258
+
259
+ function normalizeTimeFields(value) {
260
+ if (Array.isArray(value)) return value.map(normalizeTimeFields);
261
+ if (!value || typeof value !== 'object') return value;
262
+ const out = {};
263
+ for (const [key, child] of Object.entries(value)) {
264
+ out[key] = key === 'timestamp' || /At$/.test(key) ? '<time>' : normalizeTimeFields(child);
265
+ }
266
+ return out;
267
+ }
268
+
269
+ function legacyPostSignature(drive, state) {
270
+ const files = Object.keys(drive.files).map(name => path.basename(name)).sort();
271
+ return JSON.stringify({ files, state: normalizeTimeFields(state) });
272
+ }
273
+
274
+ runner.run('P2B-553-ORDINARY-POST-FLAG-INDEPENDENCE', [], (_mods, leg) => {
275
+ const cases = [
276
+ { id: 'success', fixture: 'payload-post-tool-use-success.json', status: 'idle' },
277
+ { id: 'failure', fixture: 'payload-post-tool-use-failure.json', status: 'failed' },
278
+ ];
279
+ const failures = [];
280
+ for (const scenario of cases) {
281
+ const signatures = [];
282
+ for (const switchSetting of ORDINARY_SWITCH_VALUES) {
283
+ const label = scenario.id + '/' + switchSetting.id;
284
+ const drive = driveStatePersistPayload(leg, H.payload(scenario.fixture), switchSetting);
285
+ const state = assertLegacyPost(drive, label, scenario.status);
286
+ if (scenario.status === 'idle') legacyGoldenChecks(label, drive);
287
+ signatures.push(legacyPostSignature(drive, state));
288
+ }
289
+ if (signatures.some(signature => signature !== signatures[0])) {
290
+ failures.push(scenario.id + ': ordinary post result changed across unset/0/1');
291
+ }
292
+ }
293
+ console.log(' SENTINEL ' + path.relative(ROOT, path.join(leg.hooksScripts, 'state-persist.js'))
294
+ + ': no E1 provider load across success/failure at unset/0/1');
295
+ if (failures.length) throw new Error(failures.join('; '));
296
+ });
297
+
298
+ function finish(counts) {
299
+ if (counts.FAIL === 0 && counts.INFRA === 0) {
300
+ console.log('state-persist PostToolUse suite: all assertions PASS.');
301
+ process.exit(0);
302
+ }
303
+ console.log(`state-persist PostToolUse suite: FAIL/INFRA present (failed assertions: ${counts.FAIL}, infra faults: ${counts.INFRA}).`);
304
+ process.exit(1);
305
+ }
306
+
307
+ finish(runner.summary());