@gate-forge/cli 0.7.1 → 0.8.0

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 (254) hide show
  1. package/README.md +499 -47
  2. package/dist/adapter-audit.d.ts +154 -0
  3. package/dist/adapter-audit.d.ts.map +1 -0
  4. package/dist/adapter-audit.js +531 -0
  5. package/dist/adapter-audit.js.map +1 -0
  6. package/dist/adapter-scaffold.d.ts +66 -0
  7. package/dist/adapter-scaffold.d.ts.map +1 -0
  8. package/dist/adapter-scaffold.js +353 -0
  9. package/dist/adapter-scaffold.js.map +1 -0
  10. package/dist/adopted-baseline.d.ts +10 -0
  11. package/dist/adopted-baseline.d.ts.map +1 -1
  12. package/dist/adopted-baseline.js +18 -1
  13. package/dist/adopted-baseline.js.map +1 -1
  14. package/dist/args.d.ts +15 -0
  15. package/dist/args.d.ts.map +1 -1
  16. package/dist/args.js +59 -2
  17. package/dist/args.js.map +1 -1
  18. package/dist/behavior-setup.d.ts +239 -0
  19. package/dist/behavior-setup.d.ts.map +1 -0
  20. package/dist/behavior-setup.js +738 -0
  21. package/dist/behavior-setup.js.map +1 -0
  22. package/dist/broker.d.ts.map +1 -1
  23. package/dist/broker.js +28 -2
  24. package/dist/broker.js.map +1 -1
  25. package/dist/cache-exclusions.d.ts +45 -0
  26. package/dist/cache-exclusions.d.ts.map +1 -0
  27. package/dist/cache-exclusions.js +205 -0
  28. package/dist/cache-exclusions.js.map +1 -0
  29. package/dist/candidate-tree.d.ts +86 -14
  30. package/dist/candidate-tree.d.ts.map +1 -1
  31. package/dist/candidate-tree.js +236 -62
  32. package/dist/candidate-tree.js.map +1 -1
  33. package/dist/ci-witnessed-template.d.ts +43 -0
  34. package/dist/ci-witnessed-template.d.ts.map +1 -0
  35. package/dist/ci-witnessed-template.js +283 -0
  36. package/dist/ci-witnessed-template.js.map +1 -0
  37. package/dist/cli.d.ts +1 -1
  38. package/dist/cli.d.ts.map +1 -1
  39. package/dist/cli.js +31 -4
  40. package/dist/cli.js.map +1 -1
  41. package/dist/commands/adapters.d.ts +15 -0
  42. package/dist/commands/adapters.d.ts.map +1 -0
  43. package/dist/commands/adapters.js +284 -0
  44. package/dist/commands/adapters.js.map +1 -0
  45. package/dist/commands/adopt.d.ts.map +1 -1
  46. package/dist/commands/adopt.js +9 -1
  47. package/dist/commands/adopt.js.map +1 -1
  48. package/dist/commands/baseline.d.ts.map +1 -1
  49. package/dist/commands/baseline.js +42 -1
  50. package/dist/commands/baseline.js.map +1 -1
  51. package/dist/commands/blocking.d.ts +81 -2
  52. package/dist/commands/blocking.d.ts.map +1 -1
  53. package/dist/commands/blocking.js +336 -53
  54. package/dist/commands/blocking.js.map +1 -1
  55. package/dist/commands/check.d.ts +36 -0
  56. package/dist/commands/check.d.ts.map +1 -1
  57. package/dist/commands/check.js +1008 -62
  58. package/dist/commands/check.js.map +1 -1
  59. package/dist/commands/classify.d.ts +2 -1
  60. package/dist/commands/classify.d.ts.map +1 -1
  61. package/dist/commands/classify.js +143 -5
  62. package/dist/commands/classify.js.map +1 -1
  63. package/dist/commands/enforce.d.ts +4 -4
  64. package/dist/commands/enforce.d.ts.map +1 -1
  65. package/dist/commands/enforce.js +41 -9
  66. package/dist/commands/enforce.js.map +1 -1
  67. package/dist/commands/enforcement.d.ts +13 -1
  68. package/dist/commands/enforcement.d.ts.map +1 -1
  69. package/dist/commands/enforcement.js +800 -30
  70. package/dist/commands/enforcement.js.map +1 -1
  71. package/dist/commands/history.d.ts +12 -0
  72. package/dist/commands/history.d.ts.map +1 -0
  73. package/dist/commands/history.js +45 -0
  74. package/dist/commands/history.js.map +1 -0
  75. package/dist/commands/init-presets.d.ts +148 -0
  76. package/dist/commands/init-presets.d.ts.map +1 -0
  77. package/dist/commands/init-presets.js +194 -0
  78. package/dist/commands/init-presets.js.map +1 -0
  79. package/dist/commands/init.d.ts +7 -12
  80. package/dist/commands/init.d.ts.map +1 -1
  81. package/dist/commands/init.js +610 -69
  82. package/dist/commands/init.js.map +1 -1
  83. package/dist/commands/keys.d.ts.map +1 -1
  84. package/dist/commands/keys.js +29 -13
  85. package/dist/commands/keys.js.map +1 -1
  86. package/dist/commands/next.d.ts.map +1 -1
  87. package/dist/commands/next.js +744 -15
  88. package/dist/commands/next.js.map +1 -1
  89. package/dist/commands/pre-commit.d.ts +25 -0
  90. package/dist/commands/pre-commit.d.ts.map +1 -1
  91. package/dist/commands/pre-commit.js +91 -6
  92. package/dist/commands/pre-commit.js.map +1 -1
  93. package/dist/commands/quarantine.d.ts +16 -0
  94. package/dist/commands/quarantine.d.ts.map +1 -0
  95. package/dist/commands/quarantine.js +213 -0
  96. package/dist/commands/quarantine.js.map +1 -0
  97. package/dist/commands/run.d.ts +39 -0
  98. package/dist/commands/run.d.ts.map +1 -0
  99. package/dist/commands/run.js +196 -0
  100. package/dist/commands/run.js.map +1 -0
  101. package/dist/commands/test-gates.d.ts +284 -2
  102. package/dist/commands/test-gates.d.ts.map +1 -1
  103. package/dist/commands/test-gates.js +3619 -186
  104. package/dist/commands/test-gates.js.map +1 -1
  105. package/dist/commands/tests.d.ts +1 -1
  106. package/dist/commands/tests.d.ts.map +1 -1
  107. package/dist/commands/tests.js +225 -22
  108. package/dist/commands/tests.js.map +1 -1
  109. package/dist/endpoint-compiler.d.ts.map +1 -1
  110. package/dist/endpoint-compiler.js +29 -8
  111. package/dist/endpoint-compiler.js.map +1 -1
  112. package/dist/engine-browser.d.ts +183 -0
  113. package/dist/engine-browser.d.ts.map +1 -0
  114. package/dist/engine-browser.js +381 -0
  115. package/dist/engine-browser.js.map +1 -0
  116. package/dist/engine-identity.d.ts +100 -0
  117. package/dist/engine-identity.d.ts.map +1 -0
  118. package/dist/engine-identity.js +182 -0
  119. package/dist/engine-identity.js.map +1 -0
  120. package/dist/evaluate.d.ts +89 -0
  121. package/dist/evaluate.d.ts.map +1 -1
  122. package/dist/evaluate.js +137 -16
  123. package/dist/evaluate.js.map +1 -1
  124. package/dist/execution.d.ts +253 -9
  125. package/dist/execution.d.ts.map +1 -1
  126. package/dist/execution.js +309 -12
  127. package/dist/execution.js.map +1 -1
  128. package/dist/git-hooks.d.ts +17 -0
  129. package/dist/git-hooks.d.ts.map +1 -1
  130. package/dist/git-hooks.js +119 -0
  131. package/dist/git-hooks.js.map +1 -1
  132. package/dist/git-objects.d.ts +60 -0
  133. package/dist/git-objects.d.ts.map +1 -0
  134. package/dist/git-objects.js +152 -0
  135. package/dist/git-objects.js.map +1 -0
  136. package/dist/history.d.ts +52 -0
  137. package/dist/history.d.ts.map +1 -0
  138. package/dist/history.js +116 -0
  139. package/dist/history.js.map +1 -0
  140. package/dist/host-load.d.ts +49 -0
  141. package/dist/host-load.d.ts.map +1 -0
  142. package/dist/host-load.js +78 -0
  143. package/dist/host-load.js.map +1 -0
  144. package/dist/input-snapshot.d.ts +63 -9
  145. package/dist/input-snapshot.d.ts.map +1 -1
  146. package/dist/input-snapshot.js +195 -17
  147. package/dist/input-snapshot.js.map +1 -1
  148. package/dist/io.d.ts +36 -0
  149. package/dist/io.d.ts.map +1 -1
  150. package/dist/io.js +21 -0
  151. package/dist/io.js.map +1 -1
  152. package/dist/mapping.d.ts +84 -1
  153. package/dist/mapping.d.ts.map +1 -1
  154. package/dist/mapping.js +276 -5
  155. package/dist/mapping.js.map +1 -1
  156. package/dist/native-freeze.d.ts +217 -0
  157. package/dist/native-freeze.d.ts.map +1 -0
  158. package/dist/native-freeze.js +332 -0
  159. package/dist/native-freeze.js.map +1 -0
  160. package/dist/pipeline.d.ts +52 -1
  161. package/dist/pipeline.d.ts.map +1 -1
  162. package/dist/pipeline.js +141 -3
  163. package/dist/pipeline.js.map +1 -1
  164. package/dist/playwright-browsers.d.ts +210 -0
  165. package/dist/playwright-browsers.d.ts.map +1 -0
  166. package/dist/playwright-browsers.js +364 -0
  167. package/dist/playwright-browsers.js.map +1 -0
  168. package/dist/plugins.d.ts +15 -5
  169. package/dist/plugins.d.ts.map +1 -1
  170. package/dist/plugins.js +59 -9
  171. package/dist/plugins.js.map +1 -1
  172. package/dist/progress.d.ts +148 -0
  173. package/dist/progress.d.ts.map +1 -0
  174. package/dist/progress.js +263 -0
  175. package/dist/progress.js.map +1 -0
  176. package/dist/providers.d.ts +26 -1
  177. package/dist/providers.d.ts.map +1 -1
  178. package/dist/providers.js +39 -0
  179. package/dist/providers.js.map +1 -1
  180. package/dist/receipts.d.ts +1 -0
  181. package/dist/receipts.d.ts.map +1 -1
  182. package/dist/receipts.js +35 -7
  183. package/dist/receipts.js.map +1 -1
  184. package/dist/repo-scan.d.ts +8 -1
  185. package/dist/repo-scan.d.ts.map +1 -1
  186. package/dist/repo-scan.js +221 -27
  187. package/dist/repo-scan.js.map +1 -1
  188. package/dist/reseal-chain.d.ts +184 -0
  189. package/dist/reseal-chain.d.ts.map +1 -0
  190. package/dist/reseal-chain.js +735 -0
  191. package/dist/reseal-chain.js.map +1 -0
  192. package/dist/reseal-evidence.d.ts +241 -0
  193. package/dist/reseal-evidence.d.ts.map +1 -0
  194. package/dist/reseal-evidence.js +319 -0
  195. package/dist/reseal-evidence.js.map +1 -0
  196. package/dist/reseal.d.ts +181 -0
  197. package/dist/reseal.d.ts.map +1 -0
  198. package/dist/reseal.js +1620 -0
  199. package/dist/reseal.js.map +1 -0
  200. package/dist/response-field-guidance.d.ts +82 -0
  201. package/dist/response-field-guidance.d.ts.map +1 -0
  202. package/dist/response-field-guidance.js +184 -0
  203. package/dist/response-field-guidance.js.map +1 -0
  204. package/dist/run-cache.d.ts +260 -0
  205. package/dist/run-cache.d.ts.map +1 -0
  206. package/dist/run-cache.js +771 -0
  207. package/dist/run-cache.js.map +1 -0
  208. package/dist/run-preflight.d.ts +77 -0
  209. package/dist/run-preflight.d.ts.map +1 -0
  210. package/dist/run-preflight.js +774 -0
  211. package/dist/run-preflight.js.map +1 -0
  212. package/dist/run-recipe.d.ts +107 -0
  213. package/dist/run-recipe.d.ts.map +1 -0
  214. package/dist/run-recipe.js +295 -0
  215. package/dist/run-recipe.js.map +1 -0
  216. package/dist/run-reliability.d.ts +126 -0
  217. package/dist/run-reliability.d.ts.map +1 -0
  218. package/dist/run-reliability.js +221 -0
  219. package/dist/run-reliability.js.map +1 -0
  220. package/dist/runtime.d.ts +15 -0
  221. package/dist/runtime.d.ts.map +1 -1
  222. package/dist/runtime.js +11 -1
  223. package/dist/runtime.js.map +1 -1
  224. package/dist/secret-guard.d.ts +36 -0
  225. package/dist/secret-guard.d.ts.map +1 -0
  226. package/dist/secret-guard.js +63 -0
  227. package/dist/secret-guard.js.map +1 -0
  228. package/dist/singleton-guidance.d.ts +73 -0
  229. package/dist/singleton-guidance.d.ts.map +1 -0
  230. package/dist/singleton-guidance.js +118 -0
  231. package/dist/singleton-guidance.js.map +1 -0
  232. package/dist/staged-candidate.d.ts +17 -0
  233. package/dist/staged-candidate.d.ts.map +1 -1
  234. package/dist/staged-candidate.js +50 -0
  235. package/dist/staged-candidate.js.map +1 -1
  236. package/dist/state-artifacts.d.ts +70 -0
  237. package/dist/state-artifacts.d.ts.map +1 -0
  238. package/dist/state-artifacts.js +215 -0
  239. package/dist/state-artifacts.js.map +1 -0
  240. package/dist/state.d.ts +170 -2
  241. package/dist/state.d.ts.map +1 -1
  242. package/dist/state.js +248 -8
  243. package/dist/state.js.map +1 -1
  244. package/dist/verifier-keys.d.ts +33 -2
  245. package/dist/verifier-keys.d.ts.map +1 -1
  246. package/dist/verifier-keys.js +95 -9
  247. package/dist/verifier-keys.js.map +1 -1
  248. package/guides/CONNECT-YOUR-PROJECT.md +367 -0
  249. package/guides/QUICKSTART.md +259 -24
  250. package/guides/RUNNER-NEUTRAL-EVIDENCE.md +199 -0
  251. package/guides/TEST-ENVIRONMENT.md +1041 -6
  252. package/guides/UPGRADE-0.6-to-0.7.md +3 -3
  253. package/guides/UPGRADE-0.7-to-0.8.md +114 -0
  254. package/package.json +10 -9
@@ -0,0 +1,735 @@
1
+ /**
2
+ * Re-sealed receipt verification (plan phase 3, design rule 8): a
3
+ * receipt that claims `changeClass: 'test-only'` is a CLAIM, and every
4
+ * consumer recomputes it from the sealed bytes with its OWN engine and
5
+ * key. Nothing here trusts a field: the retained parent chain is
6
+ * authenticated hop by hop, the tree difference is re-diffed, the
7
+ * change set is re-classified from the catalog the run itself planned
8
+ * from, and the affected set is recomputed and matched against the
9
+ * fresh outcomes. Any mismatch is `EVIDENCE_STALE` with the exact
10
+ * reason — a re-seal is never half-believed.
11
+ *
12
+ * The chain is retained as additive run-state files (never inside the
13
+ * sealed candidate tree): one hop directory per consecutive re-seal,
14
+ * holding the parent receipt, the parent execution result needed to
15
+ * verify it, and the catalog of the child tree the classification is
16
+ * recomputed with. Every one of those documents is authenticated by a
17
+ * digest the receipts already bind (`resealedFrom`,
18
+ * `executionResultDigest`, `catalogDigest`).
19
+ */
20
+ import { mkdirSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
21
+ import { join } from 'node:path';
22
+ import { CAUSE_NEXT_ACTIONS, ExecutionResultSchema, AttestationSchema, GateReceiptSchema, TestCatalogSchema, executionResultDigestOf, sha256Canonical, testOutcomesDigestOf, } from '@gate-forge/core';
23
+ import { loadOptionalTestMap } from './mapping.js';
24
+ import { classifyResealChange, diffSealedTrees, resealRefusalVerdict } from './reseal.js';
25
+ import { authenticateContributingEvidence, carriedEvidenceDigestOf, carriedEvidenceDocuments, carriedTestIdentities, mappedTestIdentities, stringField, } from './reseal-evidence.js';
26
+ import { readJsonArray, readStateDocument } from './state.js';
27
+ import { verifyGateReceiptWithKeyring, verifyRunRecordWithKeyring } from './verifier-keys.js';
28
+ /** Run-state subdirectory holding the retained re-seal chain. */
29
+ export const RESEAL_CHAIN_DIRECTORY = 'reseal-chain';
30
+ /**
31
+ * How many consecutive re-seals a receipt may carry. Each hop re-runs
32
+ * only the tests its change set can affect, so the drift a chain can
33
+ * accumulate is bounded; past the bound the only honest answer is a
34
+ * full run.
35
+ */
36
+ export const RESEAL_CHAIN_MAX_HOPS = 5;
37
+ function hopFileNames(hop) {
38
+ return [
39
+ `hop-${String(hop)}-receipt.json`,
40
+ `hop-${String(hop)}-run-record.json`,
41
+ `hop-${String(hop)}-execution-result.json`,
42
+ `hop-${String(hop)}-catalog.json`,
43
+ `hop-${String(hop)}-records.json`,
44
+ `hop-${String(hop)}-claims.json`,
45
+ `hop-${String(hop)}-attestations.json`,
46
+ ];
47
+ }
48
+ /**
49
+ * Moves every retained hop one place back so hop 1 is free for the
50
+ * hop about to be written.
51
+ *
52
+ * Args:
53
+ * directory: the chain directory.
54
+ * retained: how many hops are currently retained.
55
+ *
56
+ * Returns:
57
+ * void.
58
+ */
59
+ function shiftResealChain(directory, retained) {
60
+ for (let index = retained; index >= 1; index -= 1) {
61
+ const from = hopFileNames(index);
62
+ const to = hopFileNames(index + 1);
63
+ for (let file = 0; file < from.length; file += 1) {
64
+ const source = join(directory, from[file]);
65
+ try {
66
+ renameSync(source, join(directory, to[file]));
67
+ }
68
+ catch {
69
+ // A missing member makes the chain unreadable, which the
70
+ // verifier rejects; nothing to repair here.
71
+ }
72
+ }
73
+ }
74
+ }
75
+ /**
76
+ * Retains the parent run's evidence BEFORE the re-seal's own run
77
+ * overwrites `records.json`, `claims.json` and the run manifest, and
78
+ * shifts the existing chain one place back at the same moment: the hop
79
+ * being built IS the chain's new hop 1, so an earlier hop must never
80
+ * keep the evidence written for it. The hop's parent document,
81
+ * execution result and catalog are written when the receipt seals, and
82
+ * a run that seals nothing leaves no hop-1 parent behind, so a stale
83
+ * evidence file is never read as a chain.
84
+ *
85
+ * Args:
86
+ * stateDir: absolute run-state directory.
87
+ * evidence: the authenticated parent records, claims and attestation.
88
+ *
89
+ * Returns:
90
+ * void.
91
+ */
92
+ export function writeResealChainParentEvidence(stateDir, evidence) {
93
+ const directory = join(stateDir, RESEAL_CHAIN_DIRECTORY);
94
+ mkdirSync(directory, { recursive: true });
95
+ shiftResealChain(directory, resealChainHopCount(stateDir));
96
+ const [, , , , recordsName, claimsName, attestationsName] = hopFileNames(1);
97
+ writeFileSync(join(directory, recordsName), `${JSON.stringify(evidence.records, null, 2)}\n`, 'utf8');
98
+ writeFileSync(join(directory, claimsName), `${JSON.stringify(evidence.claims, null, 2)}\n`, 'utf8');
99
+ writeFileSync(join(directory, attestationsName), `${JSON.stringify(evidence.attestations, null, 2)}\n`, 'utf8');
100
+ }
101
+ /**
102
+ * True when a hop retains a run record as its parent. Only the
103
+ * IMMEDIATE parent of a re-seal can be a run record: the parent it
104
+ * produces is a gate receipt, and a receipt is the only document a
105
+ * later hop may carry from.
106
+ */
107
+ function hopCarriesRunRecord(hop) {
108
+ return hop.runRecord !== null && hop.runRecord !== undefined;
109
+ }
110
+ /**
111
+ * Counts the consecutive re-seals already retained in the run state.
112
+ *
113
+ * Args:
114
+ * stateDir: absolute run-state directory.
115
+ *
116
+ * Returns:
117
+ * number: the highest contiguous hop index (0 when no hop is retained).
118
+ */
119
+ export function resealChainHopCount(stateDir) {
120
+ let names;
121
+ try {
122
+ names = readdirSync(join(stateDir, RESEAL_CHAIN_DIRECTORY));
123
+ }
124
+ catch {
125
+ return 0;
126
+ }
127
+ let hop = 0;
128
+ for (;;) {
129
+ const [receiptName, runRecordName] = hopFileNames(hop + 1);
130
+ if (!names.includes(receiptName) && !names.includes(runRecordName))
131
+ return hop;
132
+ hop += 1;
133
+ }
134
+ }
135
+ /**
136
+ * Retains one re-seal hop as hop 1 of the chain. The chain was already
137
+ * shifted when the parent evidence was retained (that is the same
138
+ * hop), so this only writes the hop's own members.
139
+ *
140
+ * Args:
141
+ * stateDir: absolute run-state directory.
142
+ *
143
+ * hop: the parent receipt OR run record, its execution result, and
144
+ * the catalog of the tree this re-seal sealed.
145
+ *
146
+ * Returns:
147
+ * void.
148
+ */
149
+ export function writeResealChainHop(stateDir, hop) {
150
+ const directory = join(stateDir, RESEAL_CHAIN_DIRECTORY);
151
+ mkdirSync(directory, { recursive: true });
152
+ const [receiptName, runRecordName, executionName, catalogName, recordsName, claimsName, attestationsName] = hopFileNames(1);
153
+ writeFileSync(join(directory, receiptName), `${JSON.stringify(hop.receipt, null, 2)}\n`, 'utf8');
154
+ writeFileSync(join(directory, executionName), `${JSON.stringify(hop.execution, null, 2)}\n`, 'utf8');
155
+ writeFileSync(join(directory, catalogName), `${JSON.stringify(hop.catalog, null, 2)}\n`, 'utf8');
156
+ writeFileSync(join(directory, recordsName), `${JSON.stringify(hop.records, null, 2)}\n`, 'utf8');
157
+ writeFileSync(join(directory, claimsName), `${JSON.stringify(hop.claims, null, 2)}\n`, 'utf8');
158
+ writeFileSync(join(directory, attestationsName), `${JSON.stringify(hop.attestations, null, 2)}\n`, 'utf8');
159
+ // A leftover run record from an earlier chain would be read as this
160
+ // hop's parent, so the absent member is removed rather than left.
161
+ if (hopCarriesRunRecord(hop)) {
162
+ writeFileSync(join(directory, runRecordName), `${JSON.stringify(hop.runRecord, null, 2)}\n`, 'utf8');
163
+ }
164
+ else {
165
+ rmSync(join(directory, runRecordName), { force: true });
166
+ }
167
+ }
168
+ /**
169
+ * Drops every retained hop (a run that sealed an ordinary receipt has no
170
+ * re-seal chain, and a stale chain would be read as one).
171
+ *
172
+ * Args:
173
+ * stateDir: absolute run-state directory.
174
+ *
175
+ * Returns:
176
+ * void.
177
+ */
178
+ export function clearResealChain(stateDir) {
179
+ rmSync(join(stateDir, RESEAL_CHAIN_DIRECTORY), { recursive: true, force: true });
180
+ }
181
+ /** Reads the retained hops, stopping at the first missing member (fail closed). */
182
+ function readResealChain(stateDir) {
183
+ const hops = [];
184
+ for (let index = 1;; index += 1) {
185
+ const [receiptName, runRecordName, executionName, catalogName, recordsName, claimsName, attestationsName] = hopFileNames(index);
186
+ const directory = join(stateDir, RESEAL_CHAIN_DIRECTORY);
187
+ const receipt = readStateDocument(directory, receiptName);
188
+ const runRecord = readStateDocument(directory, runRecordName);
189
+ if (receipt === null && runRecord === null)
190
+ return hops;
191
+ const execution = readStateDocument(directory, executionName);
192
+ const catalog = readStateDocument(directory, catalogName);
193
+ const records = readStateDocument(directory, recordsName);
194
+ const claims = readStateDocument(directory, claimsName);
195
+ const attestations = readStateDocument(directory, attestationsName);
196
+ const envelopes = Array.isArray(attestations) ? attestations : [];
197
+ if (execution === null || catalog === null) {
198
+ return [...hops, { receipt, runRecord, execution: null, catalog: null, records, claims, attestations: envelopes }];
199
+ }
200
+ hops.push({ receipt, runRecord, execution, catalog, records, claims, attestations: envelopes });
201
+ }
202
+ }
203
+ /**
204
+ * The carried-evidence channels a GRADER needs for a re-sealed run:
205
+ * one per run that contributed to the retained evidence union, each
206
+ * naming the identity, input digest and record ids the contributing
207
+ * document binds. Nothing here is trusted — every envelope's MAC is
208
+ * verified inside the evaluator and the whole chain is recomputed by
209
+ * `resealChainBlocking` — this only tells the grader which envelope
210
+ * belongs to which run of the chain it is grading.
211
+ *
212
+ * Args:
213
+ * stateDir: absolute run-state directory.
214
+ *
215
+ * Returns:
216
+ * the channels, or null when the state holds no re-sealed receipt,
217
+ * no retained hop, or no envelope to carry.
218
+ */
219
+ export function retainedCarriedEvidence(stateDir) {
220
+ const receipt = readStateDocument(stateDir, 'receipt.json');
221
+ if (stringField(receipt, 'resealedFrom') === null)
222
+ return null;
223
+ const chain = readResealChain(stateDir);
224
+ const contributions = [];
225
+ for (const hop of chain) {
226
+ const parent = hopCarriesRunRecord(hop) ? hop.runRecord : hop.receipt;
227
+ const runId = stringField(parent, 'runId');
228
+ const inputDigest = stringField(parent, 'inputDigest');
229
+ if (runId === null || inputDigest === null)
230
+ continue;
231
+ for (const attestation of hop.attestations) {
232
+ const parsed = AttestationSchema.safeParse(attestation);
233
+ if (!parsed.success)
234
+ continue;
235
+ contributions.push({
236
+ attestation,
237
+ runId,
238
+ inputDigest,
239
+ recordIds: [...parsed.data.recordIds],
240
+ });
241
+ }
242
+ }
243
+ return contributions.length === 0 ? null : contributions;
244
+ }
245
+ /**
246
+ * Every run that contributed evidence to the state a re-seal is about
247
+ * to carry from, the parent document first: the parent plus its own
248
+ * run manifest envelope, then one per retained hop, each with the
249
+ * document that binds it and the envelope that run issued. The
250
+ * documents are authenticated with the consumer's own keyring, so a
251
+ * retained file nobody signed contributes nothing.
252
+ *
253
+ * Args:
254
+ * stateDir: absolute run-state directory.
255
+ * parent: the verified parent document of the re-seal.
256
+ * parentAttestation: the envelope the parent's own run fetched.
257
+ * verifierKeyring: the consumer's own trusted key ring.
258
+ *
259
+ * Returns:
260
+ * the contributors, or the first binding that failed in plain words.
261
+ */
262
+ export function retainedEvidenceContributors(input) {
263
+ if (input.verifierKeyring === null) {
264
+ return { contributors: [], reason: 'no verifier key is available to authenticate it' };
265
+ }
266
+ const parentDocument = input.parent.record ?? input.parent.receipt;
267
+ const contributors = [
268
+ {
269
+ runId: stringField(parentDocument, 'runId') ?? '',
270
+ inputDigest: stringField(parentDocument, 'inputDigest') ?? '',
271
+ evidenceAttestationDigest: stringField(parentDocument, 'evidenceAttestationDigest'),
272
+ attestation: input.parentAttestation,
273
+ },
274
+ ];
275
+ const chain = readResealChain(input.stateDir);
276
+ // EVERY retained hop contributed evidence the state still holds: the
277
+ // parent itself is the receipt it sealed, and the hops behind it are
278
+ // the runs whose records that receipt had already carried. Each is
279
+ // authenticated with its own document, and only that run's envelope
280
+ // authorizes its own records.
281
+ for (const hop of chain) {
282
+ const document = hopCarriesRunRecord(hop) ? hop.runRecord : hop.receipt;
283
+ const authenticated = hopCarriesRunRecord(hop)
284
+ ? verifyRunRecordWithKeyring(input.verifierKeyring, hop.runRecord)
285
+ : verifyGateReceiptWithKeyring(input.verifierKeyring, hop.receipt);
286
+ if (!authenticated.ok) {
287
+ return {
288
+ contributors: [],
289
+ reason: `the run it carried from does not authenticate with this keyring: ${authenticated.detail}`,
290
+ };
291
+ }
292
+ contributors.push({
293
+ runId: stringField(document, 'runId') ?? '',
294
+ inputDigest: stringField(document, 'inputDigest') ?? '',
295
+ evidenceAttestationDigest: stringField(document, 'evidenceAttestationDigest'),
296
+ attestation: hop.attestations[0] ?? null,
297
+ });
298
+ }
299
+ return { contributors, reason: null };
300
+ }
301
+ /**
302
+ * The tests a re-seal carries, read from the retained chain: a test
303
+ * carries when NO hop of the chain re-ran it, so the evidence of the
304
+ * original run survives for exactly those. A parent that is itself a
305
+ * re-seal planned only the tests IT re-ran, so its own plan cannot
306
+ * count the rest; the chain can.
307
+ *
308
+ * Args:
309
+ * stateDir: absolute run-state directory holding the retained chain.
310
+ * childFiles: the test files this run re-runs (the immediate
311
+ * child's affected set).
312
+ *
313
+ * Returns:
314
+ * the carried files, identities and count, or null when the state
315
+ * retains no chain at all (a whole-suite parent counts from its own
316
+ * plan, exactly as before). A hop whose execution result is
317
+ * unreadable carries nothing (fail closed).
318
+ */
319
+ export function retainedChainCarriedTests(input) {
320
+ const chain = readResealChain(input.stateDir);
321
+ if (chain.length === 0)
322
+ return null;
323
+ const rerun = new Set(input.childFiles);
324
+ const files = new Set();
325
+ const identities = new Set();
326
+ let count = 0;
327
+ for (const hop of chain) {
328
+ const parsed = ExecutionResultSchema.safeParse(hop.execution);
329
+ if (!parsed.success)
330
+ return { files: new Set(), identities: new Set(), count: 0 };
331
+ const execution = parsed.data;
332
+ const carried = execution.outcomes.filter((outcome) => !rerun.has(outcome.file));
333
+ count += carried.length;
334
+ for (const outcome of carried)
335
+ files.add(outcome.file);
336
+ for (const identity of carriedTestIdentities(execution, [...rerun]))
337
+ identities.add(identity);
338
+ for (const outcome of execution.outcomes)
339
+ rerun.add(outcome.file);
340
+ }
341
+ return { files, identities, count };
342
+ }
343
+ /**
344
+ * One typed `EVIDENCE_STALE` entry naming the exact mismatch; the
345
+ * caller prefixes it with its own surface (`require-e2e: `, `broker: `).
346
+ */
347
+ function stale(detail) {
348
+ return [
349
+ {
350
+ kind: 'finding',
351
+ resourceId: null,
352
+ name: null,
353
+ detail,
354
+ location: null,
355
+ cause: 'EVIDENCE_STALE',
356
+ nextAction: CAUSE_NEXT_ACTIONS['EVIDENCE_STALE'],
357
+ },
358
+ ];
359
+ }
360
+ /**
361
+ * The repository's own test-map entries, read from the candidate tree
362
+ * this consumer is verifying (a sealed file inside the input digest).
363
+ * An unreadable or absent sidecar contributes no entries — the union
364
+ * comparison then fails closed rather than inventing an identity.
365
+ *
366
+ * Args:
367
+ * cwd: the repository root.
368
+ *
369
+ * Returns:
370
+ * the entries (empty when the repository declares none).
371
+ */
372
+ function testMapEntries(cwd) {
373
+ try {
374
+ return loadOptionalTestMap(cwd)?.tests ?? [];
375
+ }
376
+ catch {
377
+ return [];
378
+ }
379
+ }
380
+ function names(paths) {
381
+ return paths.length === 0 ? '<none>' : paths.join(', ');
382
+ }
383
+ /**
384
+ * Recomputes — with the consumer's own engine, keyring and object store
385
+ * — everything a re-sealed receipt claims, hop by hop. A receipt that
386
+ * is not a re-seal returns no blockers; every mismatch, missing
387
+ * document or unreadable parent returns one `EVIDENCE_STALE` entry
388
+ * naming the exact difference.
389
+ *
390
+ * Args:
391
+ * stateDir: absolute run-state directory holding receipt.json,
392
+ * execution-result.json and the retained chain.
393
+ * receipt: the verified receipt under inspection.
394
+ * verifierKeyring: the consumer's own keyring (the parent is
395
+ * authenticated with it, never with the candidate's key).
396
+ * gitDir: the consumer's object store, holding both sealed trees.
397
+ * cwd: the repository root (import-graph alias resolution).
398
+ * env: the process environment (Git redirectors are stripped).
399
+ *
400
+ * Returns:
401
+ * BlockingEntry[]: empty when the re-seal recomputes exactly, else one
402
+ * typed `EVIDENCE_STALE` blocker.
403
+ */
404
+ /**
405
+ * The evidence contributors of one hop and every hop below it: the
406
+ * document each hop retained, paired with the witness envelope that
407
+ * run issued. The evidence those documents hold is a union, so every
408
+ * one of them must authenticate for any of it to carry.
409
+ *
410
+ * Args:
411
+ * chain: the retained hops, innermost first.
412
+ * index: the hop whose evidence is being authenticated.
413
+ *
414
+ * Returns:
415
+ * the contributors, or null when a contributing hop retained no
416
+ * envelope at all (fail closed).
417
+ */
418
+ function chainContributors(chain, index) {
419
+ const contributors = [];
420
+ for (let position = index; position < chain.length; position += 1) {
421
+ const hop = chain[position];
422
+ const document = hopCarriesRunRecord(hop) ? hop.runRecord : hop.receipt;
423
+ const attestation = hop.attestations[0] ?? null;
424
+ if (attestation === null)
425
+ return null;
426
+ contributors.push({
427
+ runId: stringField(document, 'runId') ?? '',
428
+ inputDigest: stringField(document, 'inputDigest') ?? '',
429
+ evidenceAttestationDigest: stringField(document, 'evidenceAttestationDigest'),
430
+ attestation,
431
+ });
432
+ }
433
+ return contributors;
434
+ }
435
+ export function resealChainBlocking(input) {
436
+ const child = input.receipt;
437
+ if (child.resealedFrom === undefined)
438
+ return [];
439
+ if (input.verifierKeyring === null || input.gitDir === null) {
440
+ return stale('this consumer has no verifier keyring or object store, so the re-seal cannot be recomputed (fail closed)');
441
+ }
442
+ const chain = readResealChain(input.stateDir);
443
+ if (chain.length === 0) {
444
+ return stale(`the re-sealed receipt names parent ${child.resealedFrom} but the run state retains no parent receipt (fail closed)`);
445
+ }
446
+ if (chain.length > RESEAL_CHAIN_MAX_HOPS) {
447
+ return stale(`the re-sealed receipt carries ${String(chain.length)} consecutive re-seals, past the bound of ` +
448
+ `${String(RESEAL_CHAIN_MAX_HOPS)} — run the full suite`);
449
+ }
450
+ const childExecutionParsed = ExecutionResultSchema.safeParse(readStateDocument(input.stateDir, 'execution-result.json'));
451
+ if (!childExecutionParsed.success) {
452
+ return stale('the re-sealed receipt binds no readable execution result (fail closed)');
453
+ }
454
+ const childExecution = childExecutionParsed.data;
455
+ if (executionResultDigestOf(childExecution) !== child.executionResultDigest) {
456
+ return stale('the re-sealed receipt\'s execution result no longer matches its bound digest (fail closed)');
457
+ }
458
+ // Walk outward: hop 1 is the immediate parent of the receipt under
459
+ // inspection, hop 2 the parent of that receipt, and so on.
460
+ let currentReceipt = child;
461
+ let currentExecution = childExecution;
462
+ // What the whole chain carries: the count and the identities of the
463
+ // tests NO hop re-ran. `reranFiles` grows with every fresh outcome the
464
+ // walk sees, so an outer hop's carry is reduced by what an inner hop
465
+ // already re-ran — a test is carried once, by the whole chain.
466
+ const reranFiles = new Set();
467
+ let carriedTotal = 0;
468
+ const carriedFiles = new Set();
469
+ const carriedIdentities = new Set();
470
+ // Hop 1's authenticated evidence, kept for the state-evidence union
471
+ // check the walk can only make once every hop has contributed.
472
+ let hopOneEvidence = null;
473
+ for (let index = 0; index < chain.length; index += 1) {
474
+ const childFiles = new Set(currentExecution.outcomes.map((outcome) => outcome.file));
475
+ const hop = chain[index];
476
+ if (hop.execution === null || hop.catalog === null) {
477
+ return stale(`re-seal hop ${String(index + 1)} retains no parent execution result or catalog (fail closed)`);
478
+ }
479
+ const parentExecutionParsed = ExecutionResultSchema.safeParse(hop.execution);
480
+ if (!parentExecutionParsed.success) {
481
+ return stale(`re-seal hop ${String(index + 1)} retains a malformed parent execution result (fail closed)`);
482
+ }
483
+ const parentExecution = parentExecutionParsed.data;
484
+ const catalogParsed = TestCatalogSchema.safeParse(hop.catalog);
485
+ if (!catalogParsed.success) {
486
+ return stale(`re-seal hop ${String(index + 1)} retains a malformed test catalog (fail closed)`);
487
+ }
488
+ const catalog = catalogParsed.data;
489
+ // The parent is whatever the child CLAIMS it is, and the claim must
490
+ // match what the chain retains: a hop holding a run record never
491
+ // stands in for a receipt parent, or the reverse.
492
+ const hopNumber = String(index + 1);
493
+ const claimedKind = currentReceipt.resealedFromKind ?? 'receipt';
494
+ const retainedKind = hopCarriesRunRecord(hop) ? 'run-record' : 'receipt';
495
+ if (claimedKind !== retainedKind) {
496
+ return stale(`re-seal hop ${hopNumber} claims a ${claimedKind} parent but the run state retains a ${retainedKind} ` +
497
+ '(fail closed)');
498
+ }
499
+ let parentDigest;
500
+ let parentTreeId;
501
+ let parentSha;
502
+ let parentReceipt = null;
503
+ if (retainedKind === 'run-record') {
504
+ // A run-record parent is recomputed exactly like a parent
505
+ // receipt: it must authenticate with the consumer's own keyring,
506
+ // it must name this very execution result and its outcomes, and
507
+ // its engine bundle, execution boundary and trusted policy must
508
+ // be the ones the re-sealed receipt itself binds.
509
+ const authenticatedRecord = verifyRunRecordWithKeyring(input.verifierKeyring, hop.runRecord);
510
+ if (!authenticatedRecord.ok) {
511
+ return stale(`re-seal hop ${hopNumber}'s parent run record does not authenticate with this keyring: ` +
512
+ `${authenticatedRecord.detail} (fail closed)`);
513
+ }
514
+ const record = authenticatedRecord.record;
515
+ if (executionResultDigestOf(parentExecution) !== record.executionResultDigest ||
516
+ testOutcomesDigestOf(parentExecution.outcomes) !== record.testOutcomesDigest ||
517
+ record.plannedTests !== parentExecution.planned.length) {
518
+ return stale(`re-seal hop ${hopNumber}'s parent execution result does not match the run record's bound digests ` +
519
+ '(fail closed)');
520
+ }
521
+ if (record.engineBundleDigest !== currentReceipt.engineBundleDigest ||
522
+ record.executionBoundaryDigest !== currentReceipt.executionBoundaryDigest ||
523
+ record.trustedPolicyDigest !== currentReceipt.trustedPolicyDigest) {
524
+ return stale(`re-seal hop ${hopNumber}'s parent run record was sealed under a different engine bundle, execution ` +
525
+ 'boundary or trusted policy than the receipt it parented (fail closed)');
526
+ }
527
+ parentDigest = sha256Canonical(record);
528
+ parentTreeId = record.candidateTreeId;
529
+ parentSha = record.gitSha;
530
+ }
531
+ else {
532
+ const parentParsed = GateReceiptSchema.safeParse(hop.receipt);
533
+ if (!parentParsed.success) {
534
+ return stale(`re-seal hop ${hopNumber} retains a malformed parent receipt (fail closed)`);
535
+ }
536
+ const parent = parentParsed.data;
537
+ const authenticated = verifyGateReceiptWithKeyring(input.verifierKeyring, hop.receipt);
538
+ if (!authenticated.ok) {
539
+ return stale(`re-seal hop ${hopNumber}'s parent receipt does not authenticate with this keyring: ` +
540
+ `${authenticated.detail} (fail closed)`);
541
+ }
542
+ if (executionResultDigestOf(parentExecution) !== parent.executionResultDigest) {
543
+ return stale(`re-seal hop ${hopNumber}'s parent execution result does not match its receipt digest (fail closed)`);
544
+ }
545
+ parentReceipt = parent;
546
+ parentDigest = sha256Canonical(parent);
547
+ parentTreeId = parent.candidateTreeId;
548
+ parentSha = parent.gitSha;
549
+ }
550
+ if (parentDigest !== currentReceipt.resealedFrom) {
551
+ return stale(`re-seal hop ${hopNumber} names parent ${String(currentReceipt.resealedFrom)} but the retained ` +
552
+ `parent ${retainedKind} hashes to ${parentDigest} (fail closed)`);
553
+ }
554
+ if (sha256Canonical(catalog) !== currentExecution.catalogDigest) {
555
+ return stale(`re-seal hop ${String(index + 1)}'s retained catalog does not match the catalog digest the re-seal ` +
556
+ 'planned from (fail closed)');
557
+ }
558
+ if (parentTreeId === null || currentReceipt.candidateTreeId === null) {
559
+ return stale(`re-seal hop ${hopNumber} has no sealed candidate tree on one side (fail closed)`);
560
+ }
561
+ const changed = diffSealedTrees(input.gitDir, input.env, parentTreeId, currentReceipt.candidateTreeId);
562
+ if (changed === null) {
563
+ return stale(`re-seal hop ${hopNumber}: the sealed trees ${parentTreeId} → ` +
564
+ `${currentReceipt.candidateTreeId} could not be diffed (fail closed)`);
565
+ }
566
+ const diffPaths = changed.map((entry) => entry.path).sort();
567
+ const claimed = [...(currentReceipt.changedPaths ?? [])].sort();
568
+ if (diffPaths.length !== claimed.length ||
569
+ diffPaths.some((path, position) => path !== claimed[position])) {
570
+ return stale(`re-sealed receipt names changed paths ${names(claimed)} but the trees differ in ${names(diffPaths)}`);
571
+ }
572
+ // The two commit trees say which paths are TRACKED, and that single
573
+ // reading serves both decisions this recomputation makes: the owner
574
+ // declaration may hide only an untracked path, and a declared browser
575
+ // state counts as generated output only when neither commit tracks
576
+ // it. Both are applied here exactly as the sealing run applied them,
577
+ // so CI reaches the identical decision with its own engine. A
578
+ // document that names no commit has no committed file list, so
579
+ // nothing can be proven untracked: the classifier then disregards
580
+ // nothing, treats no state as generated, and a receipt that claims
581
+ // either is stale below.
582
+ const commitTrees = parentSha !== null && currentReceipt.gitSha !== null
583
+ ? { parentCommitTreeId: `${parentSha}^{tree}`, currentCommitTreeId: `${currentReceipt.gitSha}^{tree}` }
584
+ : {};
585
+ const classification = classifyResealChange({
586
+ gitDir: input.gitDir,
587
+ env: input.env,
588
+ cwd: input.cwd,
589
+ parentTreeId: parentTreeId,
590
+ currentTreeId: currentReceipt.candidateTreeId,
591
+ testFiles: [...new Set(catalog.entries.map((entry) => entry.file))],
592
+ runtimeFileGlobs: input.runtimeFileGlobs ?? [],
593
+ ...commitTrees,
594
+ });
595
+ if (!classification.eligible) {
596
+ return stale(`re-sealed receipt does not recompute: ${resealRefusalVerdict(classification.reason)}`);
597
+ }
598
+ // The receipt says which paths the declaration hid. Reproducing the
599
+ // decision is not enough: it must hide exactly the same ones.
600
+ const recomputed = classification.disregardedPaths ?? [];
601
+ const claimedDisregarded = currentReceipt.resealDisregarded ?? [];
602
+ if (recomputed.join(' ') !== claimedDisregarded.join(' ')) {
603
+ return stale(`re-sealed receipt names ${String(claimedDisregarded.length)} disregarded declared runtime file(s) ` +
604
+ `(${names(claimedDisregarded)}) but the recomputation disregards ${String(recomputed.length)} ` +
605
+ `(${names(recomputed)}) (fail closed)`);
606
+ }
607
+ const affected = new Set(classification.affectedTestFiles);
608
+ const freshFiles = new Set(currentExecution.outcomes.map((outcome) => outcome.file));
609
+ const outside = [...freshFiles].filter((file) => !affected.has(file)).sort();
610
+ if (outside.length > 0) {
611
+ return stale(`re-sealed receipt re-ran tests outside the recomputed affected set (${names(outside)}); ` +
612
+ `the affected set is ${names(classification.affectedTestFiles)}`);
613
+ }
614
+ const missing = classification.affectedTestFiles.filter((file) => !freshFiles.has(file)).sort();
615
+ if (missing.length > 0) {
616
+ return stale(`re-sealed receipt carries no fresh outcome for the recomputed affected set (${names(missing)})`);
617
+ }
618
+ const failed = currentExecution.outcomes.filter((outcome) => outcome.status !== 'passed');
619
+ if (failed.length > 0) {
620
+ return stale(`re-sealed receipt re-ran ${String(failed.length)} test(s) that did not pass ` +
621
+ `(${names(failed.map((outcome) => outcome.logicalKey))})`);
622
+ }
623
+ if (currentReceipt.rerunTests !== currentExecution.outcomes.length) {
624
+ return stale(`re-sealed receipt claims ${String(currentReceipt.rerunTests ?? -1)} re-run test(s) but its execution ` +
625
+ `result holds ${String(currentExecution.outcomes.length)} outcome(s)`);
626
+ }
627
+ // A test an INNER hop already re-ran is not carried by this hop:
628
+ // its fresh outcome is already part of the chain, and counting it
629
+ // twice is exactly the double-count a chain must not have.
630
+ for (const file of freshFiles)
631
+ reranFiles.add(file);
632
+ const carriedOutcomes = parentExecution.outcomes.filter((outcome) => !affected.has(outcome.file) && !reranFiles.has(outcome.file));
633
+ const parentFailures = carriedOutcomes.filter((outcome) => outcome.status !== 'passed');
634
+ if (parentFailures.length > 0) {
635
+ return stale(`re-sealed receipt carries ${String(parentFailures.length)} parent outcome(s) that did not pass ` +
636
+ `(${names(parentFailures.map((outcome) => outcome.logicalKey))})`);
637
+ }
638
+ // A re-seal's parent may itself be a re-seal, so what this hop
639
+ // carries is only PART of what the run carries: every deeper hop
640
+ // carried its own untouched tests too. The count and the carried
641
+ // identities therefore accumulate across the whole chain, and the
642
+ // receipt under inspection (the immediate child) states the total —
643
+ // so the claim is checked ONCE, after the walk has seen every hop.
644
+ carriedTotal += carriedOutcomes.length;
645
+ for (const identity of carriedTestIdentities(parentExecution, [...reranFiles])) {
646
+ carriedIdentities.add(identity);
647
+ }
648
+ for (const outcome of carriedOutcomes)
649
+ carriedFiles.add(outcome.file);
650
+ // The carried EVIDENCE (the second half of what a re-seal carries):
651
+ // the retained parent records and claims are authenticated against
652
+ // the envelope of EVERY run that contributed them — this hop's own
653
+ // parent first, then each deeper hop's — with THIS keyring, and
654
+ // reduced to the tests no hop of the chain re-ran.
655
+ if (hop.records === null || hop.claims === null) {
656
+ return stale(`re-seal hop ${hopNumber} retains no parent evidence documents (fail closed)`);
657
+ }
658
+ const contributors = chainContributors(chain, index);
659
+ if (contributors === null) {
660
+ return stale(`re-seal hop ${hopNumber} retains no witness envelope for a contributing run (fail closed)`);
661
+ }
662
+ const authenticated = authenticateContributingEvidence({
663
+ records: Array.isArray(hop.records) ? hop.records : [],
664
+ claims: Array.isArray(hop.claims) ? hop.claims : [],
665
+ }, contributors, input.verifierKeyring.keys.map((entry) => entry.key));
666
+ if (index === 0 && authenticated.ok)
667
+ hopOneEvidence = authenticated.evidence;
668
+ if (!authenticated.ok) {
669
+ return stale(`re-seal hop ${hopNumber}'s retained evidence does not recompute: ${authenticated.reason} (fail closed)`);
670
+ }
671
+ // A run record is the tail of a chain by construction: it is the
672
+ // only parent a re-seal can have before any receipt exists, so a
673
+ // retained hop beyond it is a chain that cannot be recomputed.
674
+ if (retainedKind === 'run-record' && index + 1 < chain.length) {
675
+ return stale(`re-seal hop ${hopNumber} retains a run record as a parent, but the chain continues past it ` +
676
+ '(fail closed)');
677
+ }
678
+ if (retainedKind === 'run-record')
679
+ break;
680
+ if (parentReceipt !== null)
681
+ currentReceipt = parentReceipt;
682
+ currentExecution = parentExecution;
683
+ }
684
+ // What the IMMEDIATE re-seal claims it carried is only knowable once
685
+ // the whole chain has been walked: each hop carried its own untouched
686
+ // tests, and they accumulate. Both the count and the carried evidence
687
+ // are therefore checked here, against the receipt under inspection.
688
+ if (hopOneEvidence === null) {
689
+ return stale('the run state retains no authenticated evidence for the re-sealed receipt (fail closed)');
690
+ }
691
+ if (child.carriedTests !== carriedTotal) {
692
+ return stale(`re-sealed receipt claims ${String(child.carriedTests ?? -1)} carried test(s) but its chain ` +
693
+ `holds ${String(carriedTotal)} carried outcome(s)`);
694
+ }
695
+ // The state evidence is the IMMEDIATE re-seal's union, so it is
696
+ // compared with hop 1 alone: a deeper hop's own union was the run
697
+ // that sealed its successor, whose evidence this chain no longer
698
+ // retains.
699
+ if (child.carriedEvidenceDigest === undefined) {
700
+ return stale('re-seal hop 1 seals no carriedEvidenceDigest (fail closed)');
701
+ }
702
+ for (const key of mappedTestIdentities(carriedFiles, testMapEntries(input.cwd))) {
703
+ carriedIdentities.add(key);
704
+ }
705
+ const carried = carriedEvidenceDocuments(hopOneEvidence, carriedIdentities);
706
+ const stateRecords = readJsonArray(input.stateDir, 'records.json');
707
+ const stateClaims = readJsonArray(input.stateDir, 'claims.json');
708
+ if (carriedEvidenceDigestOf({ records: stateRecords, claims: stateClaims }) !== child.carriedEvidenceDigest) {
709
+ return stale('re-seal hop 1: the state evidence no longer hashes to the carriedEvidenceDigest the receipt ' +
710
+ 'sealed (fail closed)');
711
+ }
712
+ // The state evidence must BE the union: the retained carried
713
+ // evidence plus exactly this run's own (never a contributing
714
+ // run's records twice).
715
+ const contributingRuns = new Set(hopOneEvidence.contributions.map((entry) => entry.runId));
716
+ const recomputedUnion = {
717
+ records: [
718
+ ...carried.records,
719
+ ...stateRecords.filter((record) => !contributingRuns.has(stringField(record, 'runId') ?? '')),
720
+ ],
721
+ claims: [
722
+ ...carried.claims,
723
+ ...stateClaims.filter((claim) => {
724
+ const testId = stringField(claim, 'testId');
725
+ return testId !== null && !carriedIdentities.has(testId);
726
+ }),
727
+ ],
728
+ };
729
+ if (carriedEvidenceDigestOf(recomputedUnion) !== child.carriedEvidenceDigest) {
730
+ return stale('re-seal hop 1: the state evidence is not the union of the retained parent evidence ' +
731
+ `(${String(carried.records.length)} carried record(s)) and this run's own (fail closed)`);
732
+ }
733
+ return [];
734
+ }
735
+ //# sourceMappingURL=reseal-chain.js.map