@emilia-protocol/gate 0.11.0 → 0.13.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 (419) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/README.md +252 -0
  3. package/action-control-manifest.js +3 -268
  4. package/action-escrow-custodian.js +3 -394
  5. package/action-escrow-evidence.js +3 -858
  6. package/action-escrow-package.js +3 -1668
  7. package/action-escrow-postgres.js +3 -337
  8. package/action-escrow-state.js +3 -396
  9. package/action-escrow-verifiers.js +3 -331
  10. package/action-escrow.js +3 -2447
  11. package/action-packs.js +3 -140
  12. package/adapters/_kit.js +3 -104
  13. package/adapters/aws.js +3 -108
  14. package/adapters/cloudflare.js +3 -54
  15. package/adapters/gcp.js +3 -70
  16. package/adapters/github.js +3 -109
  17. package/adapters/jira.js +3 -55
  18. package/adapters/k8s.js +3 -70
  19. package/adapters/linear.js +3 -55
  20. package/adapters/salesforce.js +3 -55
  21. package/adapters/stripe.js +3 -81
  22. package/adapters/supabase.js +3 -121
  23. package/adapters/terraform.js +3 -59
  24. package/adapters/vercel.js +3 -84
  25. package/aec-execution.js +3 -312
  26. package/breakglass.js +3 -582
  27. package/capability-receipt.js +4 -0
  28. package/cf1-conformance.js +3 -106
  29. package/challenge-store.js +3 -69
  30. package/control-plane.js +3 -340
  31. package/coverage.js +3 -721
  32. package/demo.mjs +1 -1
  33. package/deployment-attestation.js +3 -247
  34. package/dist/action-control-manifest.d.ts +82 -0
  35. package/dist/action-control-manifest.d.ts.map +1 -0
  36. package/dist/action-control-manifest.js +422 -0
  37. package/dist/action-control-manifest.js.map +1 -0
  38. package/dist/action-escrow-custodian.d.ts +74 -0
  39. package/dist/action-escrow-custodian.d.ts.map +1 -0
  40. package/dist/action-escrow-custodian.js +363 -0
  41. package/dist/action-escrow-custodian.js.map +1 -0
  42. package/dist/action-escrow-evidence.d.ts +130 -0
  43. package/dist/action-escrow-evidence.d.ts.map +1 -0
  44. package/dist/action-escrow-evidence.js +1012 -0
  45. package/dist/action-escrow-evidence.js.map +1 -0
  46. package/dist/action-escrow-package.d.ts +10 -0
  47. package/dist/action-escrow-package.d.ts.map +1 -0
  48. package/dist/action-escrow-package.js +1583 -0
  49. package/dist/action-escrow-package.js.map +1 -0
  50. package/dist/action-escrow-postgres.d.ts +62 -0
  51. package/dist/action-escrow-postgres.d.ts.map +1 -0
  52. package/dist/action-escrow-postgres.js +339 -0
  53. package/dist/action-escrow-postgres.js.map +1 -0
  54. package/dist/action-escrow-state.d.ts +141 -0
  55. package/dist/action-escrow-state.d.ts.map +1 -0
  56. package/dist/action-escrow-state.js +413 -0
  57. package/dist/action-escrow-state.js.map +1 -0
  58. package/dist/action-escrow-verifiers.d.ts +65 -0
  59. package/dist/action-escrow-verifiers.d.ts.map +1 -0
  60. package/dist/action-escrow-verifiers.js +421 -0
  61. package/dist/action-escrow-verifiers.js.map +1 -0
  62. package/dist/action-escrow.d.ts +112 -0
  63. package/dist/action-escrow.d.ts.map +1 -0
  64. package/dist/action-escrow.js +2661 -0
  65. package/dist/action-escrow.js.map +1 -0
  66. package/dist/action-packs.d.ts +54 -0
  67. package/dist/action-packs.d.ts.map +1 -0
  68. package/dist/action-packs.js +143 -0
  69. package/dist/action-packs.js.map +1 -0
  70. package/dist/adapters/_kit.d.ts +50 -0
  71. package/dist/adapters/_kit.d.ts.map +1 -0
  72. package/dist/adapters/_kit.js +98 -0
  73. package/dist/adapters/_kit.js.map +1 -0
  74. package/dist/adapters/aws.d.ts +158 -0
  75. package/dist/adapters/aws.d.ts.map +1 -0
  76. package/dist/adapters/aws.js +136 -0
  77. package/dist/adapters/aws.js.map +1 -0
  78. package/dist/adapters/cloudflare.d.ts +116 -0
  79. package/dist/adapters/cloudflare.d.ts.map +1 -0
  80. package/dist/adapters/cloudflare.js +75 -0
  81. package/dist/adapters/cloudflare.js.map +1 -0
  82. package/dist/adapters/gcp.d.ts +139 -0
  83. package/dist/adapters/gcp.d.ts.map +1 -0
  84. package/dist/adapters/gcp.js +69 -0
  85. package/dist/adapters/gcp.js.map +1 -0
  86. package/dist/adapters/github.d.ts +123 -0
  87. package/dist/adapters/github.d.ts.map +1 -0
  88. package/dist/adapters/github.js +105 -0
  89. package/dist/adapters/github.js.map +1 -0
  90. package/dist/adapters/jira.d.ts +109 -0
  91. package/dist/adapters/jira.d.ts.map +1 -0
  92. package/dist/adapters/jira.js +54 -0
  93. package/dist/adapters/jira.js.map +1 -0
  94. package/dist/adapters/k8s.d.ts +139 -0
  95. package/dist/adapters/k8s.d.ts.map +1 -0
  96. package/dist/adapters/k8s.js +69 -0
  97. package/dist/adapters/k8s.js.map +1 -0
  98. package/dist/adapters/linear.d.ts +109 -0
  99. package/dist/adapters/linear.d.ts.map +1 -0
  100. package/dist/adapters/linear.js +54 -0
  101. package/dist/adapters/linear.js.map +1 -0
  102. package/dist/adapters/salesforce.d.ts +109 -0
  103. package/dist/adapters/salesforce.d.ts.map +1 -0
  104. package/dist/adapters/salesforce.js +54 -0
  105. package/dist/adapters/salesforce.js.map +1 -0
  106. package/dist/adapters/stripe.d.ts +116 -0
  107. package/dist/adapters/stripe.d.ts.map +1 -0
  108. package/dist/adapters/stripe.js +77 -0
  109. package/dist/adapters/stripe.js.map +1 -0
  110. package/dist/adapters/supabase.d.ts +127 -0
  111. package/dist/adapters/supabase.d.ts.map +1 -0
  112. package/dist/adapters/supabase.js +112 -0
  113. package/dist/adapters/supabase.js.map +1 -0
  114. package/dist/adapters/terraform.d.ts +109 -0
  115. package/dist/adapters/terraform.d.ts.map +1 -0
  116. package/dist/adapters/terraform.js +58 -0
  117. package/dist/adapters/terraform.js.map +1 -0
  118. package/dist/adapters/vercel.d.ts +114 -0
  119. package/dist/adapters/vercel.d.ts.map +1 -0
  120. package/dist/adapters/vercel.js +81 -0
  121. package/dist/adapters/vercel.js.map +1 -0
  122. package/dist/aec-execution.d.ts +107 -0
  123. package/dist/aec-execution.d.ts.map +1 -0
  124. package/dist/aec-execution.js +334 -0
  125. package/dist/aec-execution.js.map +1 -0
  126. package/dist/breakglass.d.ts +205 -0
  127. package/dist/breakglass.d.ts.map +1 -0
  128. package/dist/breakglass.js +627 -0
  129. package/dist/breakglass.js.map +1 -0
  130. package/dist/capability-receipt.d.ts +601 -0
  131. package/dist/capability-receipt.d.ts.map +1 -0
  132. package/dist/capability-receipt.js +1177 -0
  133. package/dist/capability-receipt.js.map +1 -0
  134. package/dist/cf1-conformance.d.ts +83 -0
  135. package/dist/cf1-conformance.d.ts.map +1 -0
  136. package/dist/cf1-conformance.js +94 -0
  137. package/dist/cf1-conformance.js.map +1 -0
  138. package/dist/challenge-store.d.ts +20 -0
  139. package/dist/challenge-store.d.ts.map +1 -0
  140. package/dist/challenge-store.js +62 -0
  141. package/dist/challenge-store.js.map +1 -0
  142. package/dist/control-plane.d.ts +136 -0
  143. package/dist/control-plane.d.ts.map +1 -0
  144. package/dist/control-plane.js +349 -0
  145. package/dist/control-plane.js.map +1 -0
  146. package/dist/coverage.d.ts +156 -0
  147. package/dist/coverage.d.ts.map +1 -0
  148. package/dist/coverage.js +768 -0
  149. package/dist/coverage.js.map +1 -0
  150. package/dist/deployment-attestation.d.ts +93 -0
  151. package/dist/deployment-attestation.d.ts.map +1 -0
  152. package/dist/deployment-attestation.js +262 -0
  153. package/dist/deployment-attestation.js.map +1 -0
  154. package/dist/eg1-conformance.d.ts +286 -0
  155. package/dist/eg1-conformance.d.ts.map +1 -0
  156. package/dist/eg1-conformance.js +379 -0
  157. package/dist/eg1-conformance.js.map +1 -0
  158. package/dist/enterprise.d.ts +88 -0
  159. package/dist/enterprise.d.ts.map +1 -0
  160. package/dist/enterprise.js +194 -0
  161. package/dist/enterprise.js.map +1 -0
  162. package/dist/evidence-postgres.d.ts +123 -0
  163. package/dist/evidence-postgres.d.ts.map +1 -0
  164. package/dist/evidence-postgres.js +343 -0
  165. package/dist/evidence-postgres.js.map +1 -0
  166. package/dist/evidence.d.ts +113 -0
  167. package/dist/evidence.d.ts.map +1 -0
  168. package/dist/evidence.js +369 -0
  169. package/dist/evidence.js.map +1 -0
  170. package/dist/execution-binding.d.ts +49 -0
  171. package/dist/execution-binding.d.ts.map +1 -0
  172. package/dist/execution-binding.js +243 -0
  173. package/dist/execution-binding.js.map +1 -0
  174. package/dist/formal-runtime-map.d.ts +42 -0
  175. package/dist/formal-runtime-map.d.ts.map +1 -0
  176. package/dist/formal-runtime-map.js +23 -0
  177. package/dist/formal-runtime-map.js.map +1 -0
  178. package/dist/index.d.ts +1002 -0
  179. package/dist/index.d.ts.map +1 -0
  180. package/dist/index.js +1736 -0
  181. package/dist/index.js.map +1 -0
  182. package/dist/key-registry.d.ts +60 -0
  183. package/dist/key-registry.d.ts.map +1 -0
  184. package/dist/key-registry.js +144 -0
  185. package/dist/key-registry.js.map +1 -0
  186. package/dist/mcp.d.ts +53 -0
  187. package/dist/mcp.d.ts.map +1 -0
  188. package/dist/mcp.js +112 -0
  189. package/dist/mcp.js.map +1 -0
  190. package/dist/metering.d.ts +80 -0
  191. package/dist/metering.d.ts.map +1 -0
  192. package/dist/metering.js +227 -0
  193. package/dist/metering.js.map +1 -0
  194. package/dist/metrics.d.ts +68 -0
  195. package/dist/metrics.d.ts.map +1 -0
  196. package/dist/metrics.js +242 -0
  197. package/dist/metrics.js.map +1 -0
  198. package/dist/network-witness.d.ts +167 -0
  199. package/dist/network-witness.d.ts.map +1 -0
  200. package/dist/network-witness.js +520 -0
  201. package/dist/network-witness.js.map +1 -0
  202. package/dist/protocol-state.d.ts +141 -0
  203. package/dist/protocol-state.d.ts.map +1 -0
  204. package/dist/protocol-state.js +106 -0
  205. package/dist/protocol-state.js.map +1 -0
  206. package/dist/receipt-program.d.ts +59 -0
  207. package/dist/receipt-program.d.ts.map +1 -0
  208. package/dist/receipt-program.js +1053 -0
  209. package/dist/receipt-program.js.map +1 -0
  210. package/dist/reliance-kernel.d.ts +66 -0
  211. package/dist/reliance-kernel.d.ts.map +1 -0
  212. package/dist/reliance-kernel.js +103 -0
  213. package/dist/reliance-kernel.js.map +1 -0
  214. package/dist/reliance-packet.d.ts +67 -0
  215. package/dist/reliance-packet.d.ts.map +1 -0
  216. package/dist/reliance-packet.js +176 -0
  217. package/dist/reliance-packet.js.map +1 -0
  218. package/dist/remedy-program-postgres.d.ts +37 -0
  219. package/dist/remedy-program-postgres.d.ts.map +1 -0
  220. package/dist/remedy-program-postgres.js +291 -0
  221. package/dist/remedy-program-postgres.js.map +1 -0
  222. package/dist/remedy-program-receipt.d.ts +97 -0
  223. package/dist/remedy-program-receipt.d.ts.map +1 -0
  224. package/dist/remedy-program-receipt.js +817 -0
  225. package/dist/remedy-program-receipt.js.map +1 -0
  226. package/dist/remedy-program.d.ts +76 -0
  227. package/dist/remedy-program.d.ts.map +1 -0
  228. package/dist/remedy-program.js +1346 -0
  229. package/dist/remedy-program.js.map +1 -0
  230. package/dist/reports/art14.d.ts +101 -0
  231. package/dist/reports/art14.d.ts.map +1 -0
  232. package/dist/reports/art14.js +340 -0
  233. package/dist/reports/art14.js.map +1 -0
  234. package/dist/reports/assurance-package.d.ts +197 -0
  235. package/dist/reports/assurance-package.d.ts.map +1 -0
  236. package/dist/reports/assurance-package.js +313 -0
  237. package/dist/reports/assurance-package.js.map +1 -0
  238. package/dist/reports/auditor-workpaper.d.ts +194 -0
  239. package/dist/reports/auditor-workpaper.d.ts.map +1 -0
  240. package/dist/reports/auditor-workpaper.js +545 -0
  241. package/dist/reports/auditor-workpaper.js.map +1 -0
  242. package/dist/reports/external-verification.d.ts +76 -0
  243. package/dist/reports/external-verification.d.ts.map +1 -0
  244. package/dist/reports/external-verification.js +209 -0
  245. package/dist/reports/external-verification.js.map +1 -0
  246. package/dist/reports/reperform.d.ts +122 -0
  247. package/dist/reports/reperform.d.ts.map +1 -0
  248. package/dist/reports/reperform.js +432 -0
  249. package/dist/reports/reperform.js.map +1 -0
  250. package/dist/reports/underwriter.d.ts +127 -0
  251. package/dist/reports/underwriter.d.ts.map +1 -0
  252. package/dist/reports/underwriter.js +348 -0
  253. package/dist/reports/underwriter.js.map +1 -0
  254. package/dist/retention.d.ts +88 -0
  255. package/dist/retention.d.ts.map +1 -0
  256. package/dist/retention.js +90 -0
  257. package/dist/retention.js.map +1 -0
  258. package/dist/roster.d.ts +113 -0
  259. package/dist/roster.d.ts.map +1 -0
  260. package/dist/roster.js +261 -0
  261. package/dist/roster.js.map +1 -0
  262. package/dist/runtime-monitor.d.ts +132 -0
  263. package/dist/runtime-monitor.d.ts.map +1 -0
  264. package/dist/runtime-monitor.js +197 -0
  265. package/dist/runtime-monitor.js.map +1 -0
  266. package/dist/settlement.d.ts +68 -0
  267. package/dist/settlement.d.ts.map +1 -0
  268. package/dist/settlement.js +306 -0
  269. package/dist/settlement.js.map +1 -0
  270. package/dist/siem.d.ts +261 -0
  271. package/dist/siem.d.ts.map +1 -0
  272. package/dist/siem.js +247 -0
  273. package/dist/siem.js.map +1 -0
  274. package/dist/store-postgres.d.ts +107 -0
  275. package/dist/store-postgres.d.ts.map +1 -0
  276. package/dist/store-postgres.js +152 -0
  277. package/dist/store-postgres.js.map +1 -0
  278. package/dist/store.d.ts +95 -0
  279. package/dist/store.d.ts.map +1 -0
  280. package/dist/store.js +202 -0
  281. package/dist/store.js.map +1 -0
  282. package/dist/strict-json.d.ts +14 -0
  283. package/dist/strict-json.d.ts.map +1 -0
  284. package/dist/strict-json.js +132 -0
  285. package/dist/strict-json.js.map +1 -0
  286. package/dist/trust-program-adapters.d.ts +122 -0
  287. package/dist/trust-program-adapters.d.ts.map +1 -0
  288. package/dist/trust-program-adapters.js +802 -0
  289. package/dist/trust-program-adapters.js.map +1 -0
  290. package/dist/trust-program-postgres.d.ts +41 -0
  291. package/dist/trust-program-postgres.d.ts.map +1 -0
  292. package/dist/trust-program-postgres.js +346 -0
  293. package/dist/trust-program-postgres.js.map +1 -0
  294. package/dist/trust-program-revocation.d.ts +206 -0
  295. package/dist/trust-program-revocation.d.ts.map +1 -0
  296. package/dist/trust-program-revocation.js +600 -0
  297. package/dist/trust-program-revocation.js.map +1 -0
  298. package/dist/trust-program.d.ts +197 -0
  299. package/dist/trust-program.d.ts.map +1 -0
  300. package/dist/trust-program.js +1351 -0
  301. package/dist/trust-program.js.map +1 -0
  302. package/dist/witness-postgres.d.ts +43 -0
  303. package/dist/witness-postgres.d.ts.map +1 -0
  304. package/dist/witness-postgres.js +92 -0
  305. package/dist/witness-postgres.js.map +1 -0
  306. package/dist/zk-range-proof.d.ts +78 -0
  307. package/dist/zk-range-proof.d.ts.map +1 -0
  308. package/dist/zk-range-proof.js +228 -0
  309. package/dist/zk-range-proof.js.map +1 -0
  310. package/eg1-conformance.js +3 -404
  311. package/enterprise.js +3 -180
  312. package/ep-assure.mjs +1 -0
  313. package/evidence-postgres.js +3 -356
  314. package/evidence.js +4 -360
  315. package/execution-binding.js +3 -212
  316. package/formal-runtime-map.js +4 -0
  317. package/index.js +4 -1249
  318. package/key-registry.js +3 -134
  319. package/mcp.js +3 -109
  320. package/metering.js +3 -226
  321. package/metrics.js +3 -231
  322. package/network-witness.js +3 -499
  323. package/package.json +31 -2
  324. package/receipt-program.js +4 -0
  325. package/reliance-kernel.js +3 -112
  326. package/reliance-packet.js +3 -175
  327. package/remedy-program-postgres.js +4 -0
  328. package/remedy-program-receipt.js +4 -0
  329. package/remedy-program.js +4 -0
  330. package/reports/art14.js +0 -0
  331. package/reports/assurance-package.js +2 -312
  332. package/reports/auditor-workpaper.js +3 -537
  333. package/reports/external-verification.js +3 -222
  334. package/reports/reperform.js +3 -424
  335. package/reports/underwriter.js +3 -339
  336. package/retention.js +3 -84
  337. package/roster.js +3 -264
  338. package/runtime-monitor.js +4 -0
  339. package/settlement.js +3 -299
  340. package/siem.js +3 -235
  341. package/src/action-control-manifest.ts +424 -0
  342. package/src/action-escrow-custodian.ts +427 -0
  343. package/src/action-escrow-evidence.ts +1178 -0
  344. package/src/action-escrow-package.ts +1821 -0
  345. package/src/action-escrow-postgres.ts +366 -0
  346. package/src/action-escrow-state.ts +498 -0
  347. package/src/action-escrow-verifiers.ts +484 -0
  348. package/src/action-escrow.ts +3039 -0
  349. package/src/action-packs.ts +170 -0
  350. package/src/adapters/_kit.ts +117 -0
  351. package/src/adapters/aws.ts +142 -0
  352. package/src/adapters/cloudflare.ts +78 -0
  353. package/src/adapters/gcp.ts +71 -0
  354. package/src/adapters/github.ts +110 -0
  355. package/src/adapters/jira.ts +56 -0
  356. package/src/adapters/k8s.ts +71 -0
  357. package/src/adapters/linear.ts +56 -0
  358. package/src/adapters/salesforce.ts +56 -0
  359. package/src/adapters/stripe.ts +82 -0
  360. package/src/adapters/supabase.ts +122 -0
  361. package/src/adapters/terraform.ts +60 -0
  362. package/src/adapters/vercel.ts +85 -0
  363. package/src/aec-execution.ts +354 -0
  364. package/src/breakglass.ts +647 -0
  365. package/src/capability-receipt.ts +1258 -0
  366. package/src/cf1-conformance.ts +120 -0
  367. package/src/challenge-store.ts +70 -0
  368. package/src/control-plane.ts +374 -0
  369. package/src/coverage.ts +832 -0
  370. package/src/deployment-attestation.ts +259 -0
  371. package/src/eg1-conformance.ts +471 -0
  372. package/src/enterprise.ts +195 -0
  373. package/src/evidence-postgres.ts +374 -0
  374. package/src/evidence.ts +367 -0
  375. package/src/execution-binding.ts +236 -0
  376. package/src/formal-runtime-map.ts +24 -0
  377. package/src/index.ts +2122 -0
  378. package/src/key-registry.ts +150 -0
  379. package/src/mcp.ts +120 -0
  380. package/src/metering.ts +237 -0
  381. package/src/metrics.ts +232 -0
  382. package/src/network-witness.ts +585 -0
  383. package/src/protocol-state.ts +143 -0
  384. package/src/protocol-state.typecheck.ts +94 -0
  385. package/src/receipt-program.ts +1105 -0
  386. package/src/reliance-kernel.ts +119 -0
  387. package/src/reliance-packet.ts +206 -0
  388. package/src/remedy-program-postgres.ts +367 -0
  389. package/src/remedy-program-receipt.ts +891 -0
  390. package/src/remedy-program.ts +1388 -0
  391. package/src/reports/art14.ts +361 -0
  392. package/src/reports/assurance-package.ts +330 -0
  393. package/src/reports/auditor-workpaper.ts +558 -0
  394. package/src/reports/external-verification.ts +232 -0
  395. package/src/reports/reperform.ts +441 -0
  396. package/src/reports/underwriter.ts +356 -0
  397. package/src/retention.ts +100 -0
  398. package/src/roster.ts +273 -0
  399. package/src/runtime-monitor.ts +217 -0
  400. package/src/settlement.ts +313 -0
  401. package/src/siem.ts +247 -0
  402. package/src/store-postgres.ts +167 -0
  403. package/src/store.ts +214 -0
  404. package/src/strict-json.ts +115 -0
  405. package/src/trust-program-adapters.ts +847 -0
  406. package/src/trust-program-postgres.ts +429 -0
  407. package/src/trust-program-revocation.ts +751 -0
  408. package/src/trust-program.ts +1460 -0
  409. package/src/witness-postgres.ts +106 -0
  410. package/src/zk-range-proof.ts +250 -0
  411. package/store-postgres.js +3 -161
  412. package/store.js +4 -198
  413. package/strict-json.js +3 -102
  414. package/trust-program-adapters.js +4 -0
  415. package/trust-program-postgres.js +4 -0
  416. package/trust-program-revocation.js +4 -0
  417. package/trust-program.js +4 -0
  418. package/witness-postgres.js +3 -96
  419. package/zk-range-proof.js +4 -0
@@ -0,0 +1,367 @@
1
+ /**
2
+ * @emilia-protocol/gate — evidence log (tamper-detecting audit of decisions).
3
+ * @license Apache-2.0
4
+ *
5
+ * Every gate decision — allow or deny — appends a hash-chained record. Each
6
+ * record commits to the previous one, so removing or altering any decision
7
+ * breaks the chain and `verify()` catches it. This is the compliance and
8
+ * insurance artifact: a provable account of exactly which consequential actions
9
+ * were authorized, refused, and why. Default sink is in-memory; pass `sink` to
10
+ * stream records to storage. This local chain detects alteration only when the
11
+ * verifier receives its complete history from genesis; a sink alone cannot
12
+ * prevent truncation, restart-from-genesis, or cross-replica forks. Use
13
+ * createAtomicEvidenceLog for a shared production head.
14
+ *
15
+ * Hashing is over CANONICAL JSON (sorted keys) so the same logical record hashes
16
+ * identically across systems and languages — a plain JSON.stringify is
17
+ * insertion-order dependent and would make cross-system verification fragile.
18
+ *
19
+ * `strict` mode makes the log fail CLOSED: if a durable `sink` write fails, the
20
+ * record is NOT appended and `record()` throws. The gate uses this so it never
21
+ * authorizes an action it cannot durably account for. In non-strict (observe)
22
+ * mode a sink failure is best-effort and swallowed.
23
+ */
24
+ import crypto from 'node:crypto';
25
+
26
+ function sha256hex(s) {
27
+ return crypto.createHash('sha256').update(s).digest('hex');
28
+ }
29
+
30
+ /** Canonical JSON (recursive sorted keys) — matches @emilia-protocol/verify. */
31
+ export function canonicalEvidenceJson(v) {
32
+ if (v === null || v === undefined) return JSON.stringify(v);
33
+ if (Array.isArray(v)) return `[${v.map(canonicalEvidenceJson).join(',')}]`;
34
+ if (typeof v === 'object') {
35
+ return `{${Object.keys(v).sort().map((k) => JSON.stringify(k) + ':' + canonicalEvidenceJson(v[k])).join(',')}}`;
36
+ }
37
+ return JSON.stringify(v);
38
+ }
39
+
40
+ const canonical = canonicalEvidenceJson;
41
+
42
+ export function createEvidenceLog(
43
+ { sink, strict = false }: { sink?: (record: any) => any; strict?: boolean } = {},
44
+ ) {
45
+ const records: Record<string, any>[] = [];
46
+ let prev = 'genesis';
47
+
48
+ return {
49
+ // A sink can persist this process-local chain, but it cannot make the head
50
+ // atomic across replicas or continue it after restart. Never advertise the
51
+ // local logger as a fleet-safe production ledger.
52
+ durable: false,
53
+ persisted: typeof sink === 'function' && strict === true,
54
+ strict: strict === true,
55
+ forkAware: false,
56
+ atomicAppend: false,
57
+ async record(entry) {
58
+ // Deep-copy the caller's entry: a shallow spread embeds live references to
59
+ // nested objects, so a caller mutating them after record() would silently
60
+ // corrupt the hash-chained evidence record (and anything a sink persisted).
61
+ const snapshot = entry && typeof entry === 'object' ? structuredClone(entry) : entry;
62
+ const body = { seq: records.length, prev_hash: prev, ...snapshot };
63
+ const hash = sha256hex(canonical(body));
64
+ const rec = { ...body, hash };
65
+ if (sink) {
66
+ try {
67
+ await sink(rec);
68
+ } catch (e) {
69
+ // Fail closed in strict mode: do NOT advance the chain, surface the
70
+ // failure so the caller (the gate) can refuse the action. The seq is
71
+ // left unconsumed so a retry produces a consistent chain.
72
+ if (strict) {
73
+ const err = new Error('evidence_sink_failed');
74
+ err.cause = e;
75
+ throw err;
76
+ }
77
+ /* non-strict (observe): best-effort, swallow — never break the gate */
78
+ }
79
+ }
80
+ records.push(rec);
81
+ prev = hash;
82
+ return rec;
83
+ },
84
+
85
+ all() {
86
+ return records.slice();
87
+ },
88
+
89
+ /** Recompute the chain; detects any altered or removed record. */
90
+ verify() {
91
+ let p = 'genesis';
92
+ for (const r of records) {
93
+ const { hash, ...body } = r;
94
+ if (body.prev_hash !== p) return { ok: false, at: r.seq, reason: 'prev_hash_mismatch' };
95
+ if (sha256hex(canonical(body)) !== hash) return { ok: false, at: r.seq, reason: 'hash_mismatch' };
96
+ p = hash;
97
+ }
98
+ return { ok: true, length: records.length, head: p === 'genesis' ? null : p };
99
+ },
100
+ };
101
+ }
102
+
103
+ const LOG_HEX_256 = /^[0-9a-f]{64}$/;
104
+ const LOG_RESERVED_FIELDS = new Set(['seq', 'prev_hash', 'record_id', 'hash']);
105
+
106
+ /**
107
+ * Verify one logger acknowledgement independently of the logger that emitted it.
108
+ */
109
+ export function verifyEvidenceRecord(
110
+ record: any,
111
+ { atomicRequired = false, expectedEntry }: { atomicRequired?: boolean; expectedEntry?: any } = {},
112
+ ) {
113
+ try {
114
+ if (!record || typeof record !== 'object' || Array.isArray(record)
115
+ || !Number.isSafeInteger(record.seq) || record.seq < 0
116
+ || (record.prev_hash !== 'genesis' && !LOG_HEX_256.test(record.prev_hash))
117
+ || !LOG_HEX_256.test(record.hash)) return false;
118
+ const hasRecordId = Object.prototype.hasOwnProperty.call(record, 'record_id');
119
+ if ((atomicRequired && !hasRecordId)
120
+ || (hasRecordId && (typeof record.record_id !== 'string'
121
+ || record.record_id.length < 16 || record.record_id.length > 256))) return false;
122
+ const { hash, ...body } = record;
123
+ if (sha256hex(canonical(body)) !== hash) return false;
124
+ if (expectedEntry !== undefined) {
125
+ if (!expectedEntry || typeof expectedEntry !== 'object' || Array.isArray(expectedEntry)) return false;
126
+ const entry = Object.fromEntries(
127
+ Object.entries(record).filter(([key]) => !LOG_RESERVED_FIELDS.has(key)),
128
+ );
129
+ if (canonical(entry) !== canonical(expectedEntry)) return false;
130
+ }
131
+ return true;
132
+ } catch {
133
+ return false;
134
+ }
135
+ }
136
+
137
+ function assertLogEntry(entry) {
138
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
139
+ throw new Error('atomic evidence entry must be an object');
140
+ }
141
+ for (const field of LOG_RESERVED_FIELDS) {
142
+ if (Object.prototype.hasOwnProperty.call(entry, field)) {
143
+ throw new Error(`atomic evidence entry must not supply reserved field ${field}`);
144
+ }
145
+ }
146
+ const stack = [{ value: entry, depth: 0 }];
147
+ const seen = new WeakSet();
148
+ let nodes = 0;
149
+ let stringBytes = 0;
150
+ while (stack.length) {
151
+ const { value, depth } = stack.pop() as { value: any; depth: number };
152
+ if (++nodes > 50000 || depth > 64) throw new Error('atomic evidence entry exceeds resource limits');
153
+ if (value === null || typeof value === 'boolean') continue;
154
+ if (typeof value === 'number') {
155
+ if (!Number.isSafeInteger(value)) throw new Error('atomic evidence entry contains a non-safe integer');
156
+ continue;
157
+ }
158
+ if (typeof value === 'string') {
159
+ stringBytes += Buffer.byteLength(value, 'utf8');
160
+ if (stringBytes > 1024 * 1024) throw new Error('atomic evidence entry exceeds string limit');
161
+ continue;
162
+ }
163
+ if (typeof value !== 'object') throw new Error('atomic evidence entry is not canonical JSON');
164
+ if (seen.has(value)) throw new Error('atomic evidence entry contains a cycle or alias');
165
+ seen.add(value);
166
+ if (Array.isArray(value)) {
167
+ for (const child of value) stack.push({ value: child, depth: depth + 1 });
168
+ } else {
169
+ for (const [key, child] of Object.entries(value)) {
170
+ stringBytes += Buffer.byteLength(key, 'utf8');
171
+ if (stringBytes > 1024 * 1024) throw new Error('atomic evidence entry exceeds string limit');
172
+ stack.push({ value: child, depth: depth + 1 });
173
+ }
174
+ }
175
+ }
176
+ }
177
+
178
+ function validateAtomicRecord(record, expectedId, expectedEntry, expectedRecord = null) {
179
+ return record?.record_id === expectedId
180
+ && verifyEvidenceRecord(record, { atomicRequired: true, expectedEntry })
181
+ && (expectedRecord === null || canonical(record) === canonical(expectedRecord));
182
+ }
183
+
184
+ function validHead(head) {
185
+ return head === null || Boolean(head && Number.isSafeInteger(head.seq) && head.seq >= 0 && LOG_HEX_256.test(head.hash));
186
+ }
187
+
188
+ /**
189
+ * Fleet-safe, fail-closed evidence log over an atomic shared-head backend.
190
+ *
191
+ * Backend contract (all operations are scoped by streamId):
192
+ * readHead(streamId) -> null | { seq, hash }
193
+ * getById(streamId, recordId) -> null | record
194
+ * appendIfHead(streamId, expectedHeadHash|null, record) -> boolean
195
+ * readAll(streamId) -> record[] // optional, for verify/all
196
+ *
197
+ * appendIfHead MUST atomically compare the current head, append the immutable
198
+ * record, reject duplicate record_id, and advance the head in one durable
199
+ * transaction. A true return MUST provide immediate read-after-write visibility
200
+ * through getById. `backend.durable === true` is a deployment capability
201
+ * assertion; this module tests the protocol but cannot prove storage hardware
202
+ * semantics.
203
+ */
204
+ export function createAtomicEvidenceLog(backend, {
205
+ streamId = 'emilia-gate',
206
+ maxRetries = 32,
207
+ recordIdFactory = () => crypto.randomUUID(),
208
+ } = {}) {
209
+ for (const method of ['readHead', 'getById', 'appendIfHead']) {
210
+ if (typeof backend?.[method] !== 'function') {
211
+ throw new Error(`createAtomicEvidenceLog: backend must implement async ${method}()`);
212
+ }
213
+ }
214
+ if (typeof streamId !== 'string' || !streamId || streamId.length > 256) {
215
+ throw new Error('createAtomicEvidenceLog: streamId must be a non-empty string of at most 256 characters');
216
+ }
217
+ if (!Number.isSafeInteger(maxRetries) || maxRetries < 1 || maxRetries > 1024) {
218
+ throw new Error('createAtomicEvidenceLog: maxRetries must be an integer from 1 to 1024');
219
+ }
220
+ if (typeof recordIdFactory !== 'function') {
221
+ throw new Error('createAtomicEvidenceLog: recordIdFactory must be a function');
222
+ }
223
+
224
+ async function recover(recordId, snapshot, expectedRecord = null) {
225
+ const existing = await backend.getById(streamId, recordId);
226
+ if (existing === null || existing === undefined) return null;
227
+ if (!validateAtomicRecord(existing, recordId, snapshot, expectedRecord)) {
228
+ throw new Error('atomic evidence backend returned a conflicting record_id');
229
+ }
230
+ return structuredClone(existing);
231
+ }
232
+
233
+ return {
234
+ durable: backend.durable === true,
235
+ persisted: backend.durable === true,
236
+ strict: true,
237
+ forkAware: true,
238
+ atomicAppend: true,
239
+ streamId,
240
+
241
+ async health() {
242
+ if (typeof backend.health !== 'function') return { ok: false, reason: 'backend_health_unavailable' };
243
+ return backend.health();
244
+ },
245
+
246
+ async record(entry) {
247
+ const snapshot = structuredClone(entry);
248
+ assertLogEntry(snapshot);
249
+ const recordId = recordIdFactory();
250
+ if (typeof recordId !== 'string' || recordId.length < 16 || recordId.length > 256) {
251
+ throw new Error('atomic evidence record id must be a string of 16 to 256 characters');
252
+ }
253
+
254
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
255
+ const recovered = await recover(recordId, snapshot);
256
+ if (recovered) return recovered;
257
+
258
+ const head = await backend.readHead(streamId);
259
+ if (!validHead(head)) throw new Error('atomic evidence backend returned a malformed head');
260
+ const seq = head === null ? 0 : head.seq + 1;
261
+ const prevHash = head === null ? 'genesis' : head.hash;
262
+ const body = { seq, prev_hash: prevHash, record_id: recordId, ...snapshot };
263
+ const record = { ...body, hash: sha256hex(canonical(body)) };
264
+ try {
265
+ if ((await backend.appendIfHead(streamId, head?.hash ?? null, record)) === true) {
266
+ const persisted = await recover(recordId, snapshot, record);
267
+ if (persisted) return persisted;
268
+ throw new Error('atomic_evidence_append_not_observable');
269
+ }
270
+ } catch (error) {
271
+ // The append may have linearized before its response was lost. Recover
272
+ // by stable id; if no record is visible, fail closed and let the caller
273
+ // retain/freeze its execution reservation.
274
+ const afterError = await recover(recordId, snapshot, record);
275
+ if (afterError) return afterError;
276
+ const wrapped = new Error('atomic_evidence_append_indeterminate');
277
+ wrapped.cause = error;
278
+ throw wrapped;
279
+ }
280
+ }
281
+ throw new Error('atomic_evidence_contention_limit');
282
+ },
283
+
284
+ async all() {
285
+ if (typeof backend.readAll !== 'function') throw new Error('atomic evidence backend does not expose readAll()');
286
+ const records = await backend.readAll(streamId);
287
+ if (!Array.isArray(records)) throw new Error('atomic evidence backend returned malformed history');
288
+ return structuredClone(records);
289
+ },
290
+
291
+ async verify() {
292
+ try {
293
+ if (typeof backend.readAll !== 'function') return { ok: false, reason: 'read_all_unavailable' };
294
+ const records = await backend.readAll(streamId);
295
+ if (!Array.isArray(records)) return { ok: false, reason: 'malformed_history' };
296
+ let prev = 'genesis';
297
+ const ids = new Set();
298
+ for (let index = 0; index < records.length; index++) {
299
+ const record = records[index];
300
+ if (!record || typeof record !== 'object' || Array.isArray(record)
301
+ || record.seq !== index || record.prev_hash !== prev
302
+ || typeof record.record_id !== 'string' || ids.has(record.record_id)) {
303
+ return { ok: false, at: index, reason: 'sequence_or_predecessor_mismatch' };
304
+ }
305
+ const { hash, ...body } = record;
306
+ if (!LOG_HEX_256.test(hash) || sha256hex(canonical(body)) !== hash) {
307
+ return { ok: false, at: index, reason: 'hash_mismatch' };
308
+ }
309
+ ids.add(record.record_id);
310
+ prev = hash;
311
+ }
312
+ const head = await backend.readHead(streamId);
313
+ const expectedHead = records.length === 0 ? null : { seq: records.length - 1, hash: prev };
314
+ if (canonical(head) !== canonical(expectedHead)) return { ok: false, reason: 'head_mismatch' };
315
+ return { ok: true, length: records.length, head: expectedHead?.hash ?? null };
316
+ } catch {
317
+ return { ok: false, reason: 'backend_read_failed_or_malformed' };
318
+ }
319
+ },
320
+ };
321
+ }
322
+
323
+ /** In-memory contract model for tests. It is intentionally not durable. */
324
+ export function createMemoryAtomicEvidenceBackend() {
325
+ const streams = new Map();
326
+ const state = (streamId) => {
327
+ if (!streams.has(streamId)) streams.set(streamId, { records: [], byId: new Map() });
328
+ return streams.get(streamId);
329
+ };
330
+ return {
331
+ durable: false,
332
+ async readHead(streamId) {
333
+ const records = state(streamId).records;
334
+ const record = records[records.length - 1];
335
+ return record ? { seq: record.seq, hash: record.hash } : null;
336
+ },
337
+ async getById(streamId, recordId) {
338
+ return structuredClone(state(streamId).byId.get(recordId) ?? null);
339
+ },
340
+ async appendIfHead(streamId, expectedHeadHash, record) {
341
+ const s = state(streamId);
342
+ const head = s.records[s.records.length - 1] ?? null;
343
+ const actual = head?.hash ?? null;
344
+ if (actual !== expectedHeadHash || s.byId.has(record.record_id)
345
+ || record.seq !== s.records.length || record.prev_hash !== (head?.hash ?? 'genesis')) return false;
346
+ const snapshot = structuredClone(record);
347
+ s.records.push(snapshot);
348
+ s.byId.set(snapshot.record_id, snapshot);
349
+ return true;
350
+ },
351
+ async readAll(streamId) { return structuredClone(state(streamId).records); },
352
+ };
353
+ }
354
+
355
+ export const __atomicEvidenceSecurityInternals = Object.freeze({
356
+ canonical,
357
+ assertLogEntry,
358
+ validateAtomicRecord,
359
+ validHead,
360
+ });
361
+
362
+ export default {
363
+ createEvidenceLog,
364
+ verifyEvidenceRecord,
365
+ createAtomicEvidenceLog,
366
+ createMemoryAtomicEvidenceBackend,
367
+ };
@@ -0,0 +1,236 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Execution-field binding for EMILIA Gate. This is the executor-side control
3
+ // that prevents "the signed claim said X, the system mutated Y".
4
+
5
+ import crypto from 'node:crypto';
6
+
7
+ export const EXECUTION_BINDING_VERSION = 'EP-GATE-EXECUTION-BINDING-v1';
8
+
9
+ const MAX_JSON_DEPTH = 64;
10
+ const MAX_JSON_NODES = 50000;
11
+ const MAX_JSON_STRING_BYTES = 1024 * 1024;
12
+
13
+ function validUnicodeString(value) {
14
+ for (let i = 0; i < value.length; i++) {
15
+ const unit = value.charCodeAt(i);
16
+ if (unit >= 0xd800 && unit <= 0xdbff) {
17
+ const next = value.charCodeAt(++i);
18
+ if (!(next >= 0xdc00 && next <= 0xdfff)) return false;
19
+ } else if (unit >= 0xdc00 && unit <= 0xdfff) return false;
20
+ }
21
+ return true;
22
+ }
23
+
24
+ function isPlainObject(value) {
25
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
26
+ const prototype = Object.getPrototypeOf(value);
27
+ return prototype === Object.prototype || prototype === null;
28
+ }
29
+
30
+ /**
31
+ * Match the hardened EP canonical JSON safety profile: valid Unicode strings,
32
+ * booleans, null, safe integers, dense arrays, and plain data objects. Object
33
+ * identity is unique across the graph, so cycles and aliases both refuse.
34
+ */
35
+ function assertCanonicalJson(value) {
36
+ const stack = [{ value, depth: 0 }];
37
+ const seen = new WeakSet();
38
+ let nodes = 0;
39
+ let stringBytes = 0;
40
+
41
+ while (stack.length) {
42
+ // stack.length > 0 here guarantees pop() returns an element, but
43
+ // Array#pop()'s type is always T | undefined.
44
+ const current = stack.pop()!;
45
+ if (++nodes > MAX_JSON_NODES || current.depth > MAX_JSON_DEPTH) {
46
+ throw new TypeError('value exceeds the EP canonical JSON resource profile');
47
+ }
48
+ const v = current.value;
49
+ if (v === null || typeof v === 'boolean') continue;
50
+ if (typeof v === 'string') {
51
+ if (!validUnicodeString(v)) throw new TypeError('value contains invalid Unicode');
52
+ stringBytes += Buffer.byteLength(v, 'utf8');
53
+ if (stringBytes > MAX_JSON_STRING_BYTES) throw new TypeError('value exceeds the EP canonical JSON string limit');
54
+ continue;
55
+ }
56
+ if (typeof v === 'number') {
57
+ if (!Number.isSafeInteger(v)) throw new TypeError('value contains a non-safe integer');
58
+ continue;
59
+ }
60
+ if (!Array.isArray(v) && !isPlainObject(v)) {
61
+ throw new TypeError('value contains a non-plain JSON object');
62
+ }
63
+ if (seen.has(v)) throw new TypeError('value contains a cycle or alias');
64
+ seen.add(v);
65
+
66
+ if (Array.isArray(v)) {
67
+ const ownKeys = Reflect.ownKeys(v);
68
+ if (ownKeys.length !== v.length + 1 || !ownKeys.includes('length')) {
69
+ throw new TypeError('value contains a sparse or extended array');
70
+ }
71
+ for (let i = 0; i < v.length; i++) {
72
+ const descriptor = Object.getOwnPropertyDescriptor(v, String(i));
73
+ if (!descriptor || descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) {
74
+ throw new TypeError('value contains a sparse or accessor array');
75
+ }
76
+ stack.push({ value: descriptor.value, depth: current.depth + 1 });
77
+ }
78
+ continue;
79
+ }
80
+
81
+ for (const key of Reflect.ownKeys(v)) {
82
+ if (typeof key !== 'string' || !validUnicodeString(key)) {
83
+ throw new TypeError('value contains a non-JSON object key');
84
+ }
85
+ const descriptor = Object.getOwnPropertyDescriptor(v, key);
86
+ if (!descriptor || descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) {
87
+ throw new TypeError('value contains a non-data JSON property');
88
+ }
89
+ stringBytes += Buffer.byteLength(key, 'utf8');
90
+ if (stringBytes > MAX_JSON_STRING_BYTES) throw new TypeError('value exceeds the EP canonical JSON string limit');
91
+ stack.push({ value: descriptor.value, depth: current.depth + 1 });
92
+ }
93
+ }
94
+ }
95
+
96
+ function canonicalizeValidated(v) {
97
+ if (v === null) return 'null';
98
+ if (Array.isArray(v)) return `[${v.map(canonicalizeValidated).join(',')}]`;
99
+ if (typeof v === 'object') {
100
+ return `{${Object.keys(v).sort().map((k) => JSON.stringify(k) + ':' + canonicalizeValidated(v[k])).join(',')}}`;
101
+ }
102
+ return JSON.stringify(v);
103
+ }
104
+
105
+ export function canonicalize(v) {
106
+ assertCanonicalJson(v);
107
+ return canonicalizeValidated(v);
108
+ }
109
+
110
+ export function hashCanonical(v) {
111
+ return crypto.createHash('sha256').update(canonicalize(v)).digest('hex');
112
+ }
113
+
114
+ function fieldValue(container, field) {
115
+ if (!isPlainObject(container)) return { state: 'invalid' };
116
+ const descriptor = Object.getOwnPropertyDescriptor(container, field);
117
+ if (!descriptor) return { state: 'missing' };
118
+ if (descriptor.enumerable !== true || !Object.hasOwn(descriptor, 'value')) return { state: 'invalid' };
119
+ if (descriptor.value === null) return { state: 'missing' };
120
+ try {
121
+ assertCanonicalJson(descriptor.value);
122
+ return { state: 'valid', value: descriptor.value };
123
+ } catch {
124
+ return { state: 'invalid' };
125
+ }
126
+ }
127
+
128
+ function equalValue(a, b) {
129
+ return canonicalize(a) === canonicalize(b);
130
+ }
131
+
132
+ export function materialFieldsFor(requirement) {
133
+ const fields = requirement?.execution_binding?.required_fields;
134
+ return Array.isArray(fields) ? [...new Set(fields.filter(Boolean))] : [];
135
+ }
136
+
137
+ /**
138
+ * Verifies that the signed claim and the executor-observed mutation fields
139
+ * match for the action pack. The executor must pass `observedAction` from the
140
+ * system of record, not from the agent request body.
141
+ */
142
+ export function verifyExecutionBinding({ requirement, receipt, observedAction }: {
143
+ requirement?: any;
144
+ receipt?: any;
145
+ observedAction?: any;
146
+ } = {}) {
147
+ const requiredFields = materialFieldsFor(requirement);
148
+ if (requiredFields.length === 0) {
149
+ return { ok: true, required: false, required_fields: [], signed_hash: null, observed_hash: null };
150
+ }
151
+
152
+ const signed = receipt?.payload?.claim || {};
153
+ const observed = observedAction || {};
154
+ const missingSigned: PropertyKey[] = [];
155
+ const missingObserved: PropertyKey[] = [];
156
+ const invalidSigned: PropertyKey[] = [];
157
+ const invalidObserved: PropertyKey[] = [];
158
+ const mismatched: PropertyKey[] = [];
159
+ // Null-prototype accumulators. A required field literally named `__proto__`
160
+ // written onto a plain `{}` hits Object.prototype's setter instead of
161
+ // creating an own key: the value silently escapes the binding digest, and
162
+ // when it is an object it repoints the accumulator's prototype so the
163
+ // aggregate re-check below throws with nothing to attribute the refusal to.
164
+ const signedValues = Object.create(null);
165
+ const observedValues = Object.create(null);
166
+ // Field names accepted into each accumulator, recorded at the point of
167
+ // acceptance. The aggregate re-check refuses by this list rather than reading
168
+ // the accumulator back, so a refusal stays attributable for every property
169
+ // key -- including names no `Object.keys` walk can return.
170
+ const signedAccepted: PropertyKey[] = [];
171
+ const observedAccepted: PropertyKey[] = [];
172
+
173
+ for (const field of requiredFields) {
174
+ // Resolve the property key exactly once per iteration. A non-string entry
175
+ // is coerced on every use, so reading through the raw entry and writing
176
+ // through it again can address two different properties.
177
+ const key: PropertyKey = typeof field === 'symbol' ? field : String(field);
178
+ const expected = fieldValue(signed, key);
179
+ const actual = fieldValue(observed, key);
180
+ if (expected.state === 'missing') {
181
+ missingSigned.push(key);
182
+ } else if (expected.state === 'invalid') {
183
+ invalidSigned.push(key);
184
+ } else {
185
+ signedValues[key] = expected.value;
186
+ signedAccepted.push(key);
187
+ }
188
+ if (actual.state === 'missing') {
189
+ missingObserved.push(key);
190
+ } else if (actual.state === 'invalid') {
191
+ invalidObserved.push(key);
192
+ } else {
193
+ observedValues[key] = actual.value;
194
+ observedAccepted.push(key);
195
+ }
196
+ if (expected.state === 'valid' && actual.state === 'valid'
197
+ && !equalValue(expected.value, actual.value)) mismatched.push(key);
198
+ }
199
+
200
+ // Per-field checks do not see one object reused by two required fields.
201
+ // Validate the aggregate graphs before computing either digest.
202
+ try { assertCanonicalJson(signedValues); } catch {
203
+ for (const field of signedAccepted) {
204
+ if (!invalidSigned.includes(field)) invalidSigned.push(field);
205
+ }
206
+ }
207
+ try { assertCanonicalJson(observedValues); } catch {
208
+ for (const field of observedAccepted) {
209
+ if (!invalidObserved.includes(field)) invalidObserved.push(field);
210
+ }
211
+ }
212
+
213
+ return {
214
+ '@version': EXECUTION_BINDING_VERSION,
215
+ ok: missingSigned.length === 0 && missingObserved.length === 0
216
+ && invalidSigned.length === 0 && invalidObserved.length === 0 && mismatched.length === 0,
217
+ required: true,
218
+ required_fields: requiredFields,
219
+ missing_signed_fields: missingSigned,
220
+ missing_observed_fields: missingObserved,
221
+ invalid_signed_fields: invalidSigned,
222
+ invalid_observed_fields: invalidObserved,
223
+ mismatched_fields: mismatched,
224
+ signed_hash: invalidSigned.length === 0 ? hashCanonical(signedValues) : null,
225
+ observed_hash: invalidObserved.length === 0 ? hashCanonical(observedValues) : null,
226
+ note: 'Executor MUST provide observedAction from the system of record; request-body fields are not a trust source.',
227
+ };
228
+ }
229
+
230
+ export default {
231
+ EXECUTION_BINDING_VERSION,
232
+ canonicalize,
233
+ hashCanonical,
234
+ materialFieldsFor,
235
+ verifyExecutionBinding,
236
+ };
@@ -0,0 +1,24 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ /**
4
+ * Machine-checked source map for the explicit formal-to-runtime bridge.
5
+ *
6
+ * This is a coverage binding, not a compiler: the monitor remains reviewed
7
+ * JavaScript and the TLA+ model remains the source of the formal theorem.
8
+ */
9
+ export const FORMAL_RUNTIME_BRIDGE_VERSION = 'EP-FORMAL-RUNTIME-BRIDGE-v1';
10
+ export const FORMAL_RUNTIME_SPEC = 'formal/ep_handshake.tla';
11
+ export const FORMAL_RUNTIME_CONFIG = 'formal/ep_handshake.cfg';
12
+
13
+ export const FORMAL_RUNTIME_INVARIANT_MAP = Object.freeze([
14
+ Object.freeze({ runtime: 'ConsumeOnceSafety', formal: 'ConsumeOnceSafety', transition: 'consumptionCommitted' }),
15
+ Object.freeze({ runtime: 'WriteBypassSafety', formal: 'WriteBypassSafety', transition: 'beginExecution' }),
16
+ Object.freeze({ runtime: 'SignoffBindingMatch', formal: 'SignoffBindingMatch', transition: 'recordDecision' }),
17
+ ]);
18
+
19
+ export default {
20
+ FORMAL_RUNTIME_BRIDGE_VERSION,
21
+ FORMAL_RUNTIME_SPEC,
22
+ FORMAL_RUNTIME_CONFIG,
23
+ FORMAL_RUNTIME_INVARIANT_MAP,
24
+ };