@emilia-protocol/gate 0.11.0 → 0.14.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 (425) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +310 -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 +1003 -0
  179. package/dist/index.d.ts.map +1 -0
  180. package/dist/index.js +1737 -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/proposal-to-effect.d.ts +146 -0
  203. package/dist/proposal-to-effect.d.ts.map +1 -0
  204. package/dist/proposal-to-effect.js +427 -0
  205. package/dist/proposal-to-effect.js.map +1 -0
  206. package/dist/protocol-state.d.ts +141 -0
  207. package/dist/protocol-state.d.ts.map +1 -0
  208. package/dist/protocol-state.js +106 -0
  209. package/dist/protocol-state.js.map +1 -0
  210. package/dist/receipt-program.d.ts +59 -0
  211. package/dist/receipt-program.d.ts.map +1 -0
  212. package/dist/receipt-program.js +1053 -0
  213. package/dist/receipt-program.js.map +1 -0
  214. package/dist/reliance-kernel.d.ts +66 -0
  215. package/dist/reliance-kernel.d.ts.map +1 -0
  216. package/dist/reliance-kernel.js +103 -0
  217. package/dist/reliance-kernel.js.map +1 -0
  218. package/dist/reliance-packet.d.ts +67 -0
  219. package/dist/reliance-packet.d.ts.map +1 -0
  220. package/dist/reliance-packet.js +176 -0
  221. package/dist/reliance-packet.js.map +1 -0
  222. package/dist/remedy-program-postgres.d.ts +37 -0
  223. package/dist/remedy-program-postgres.d.ts.map +1 -0
  224. package/dist/remedy-program-postgres.js +291 -0
  225. package/dist/remedy-program-postgres.js.map +1 -0
  226. package/dist/remedy-program-receipt.d.ts +97 -0
  227. package/dist/remedy-program-receipt.d.ts.map +1 -0
  228. package/dist/remedy-program-receipt.js +817 -0
  229. package/dist/remedy-program-receipt.js.map +1 -0
  230. package/dist/remedy-program.d.ts +76 -0
  231. package/dist/remedy-program.d.ts.map +1 -0
  232. package/dist/remedy-program.js +1346 -0
  233. package/dist/remedy-program.js.map +1 -0
  234. package/dist/reports/art14.d.ts +101 -0
  235. package/dist/reports/art14.d.ts.map +1 -0
  236. package/dist/reports/art14.js +340 -0
  237. package/dist/reports/art14.js.map +1 -0
  238. package/dist/reports/assurance-package.d.ts +197 -0
  239. package/dist/reports/assurance-package.d.ts.map +1 -0
  240. package/dist/reports/assurance-package.js +313 -0
  241. package/dist/reports/assurance-package.js.map +1 -0
  242. package/dist/reports/auditor-workpaper.d.ts +194 -0
  243. package/dist/reports/auditor-workpaper.d.ts.map +1 -0
  244. package/dist/reports/auditor-workpaper.js +545 -0
  245. package/dist/reports/auditor-workpaper.js.map +1 -0
  246. package/dist/reports/external-verification.d.ts +76 -0
  247. package/dist/reports/external-verification.d.ts.map +1 -0
  248. package/dist/reports/external-verification.js +209 -0
  249. package/dist/reports/external-verification.js.map +1 -0
  250. package/dist/reports/reperform.d.ts +122 -0
  251. package/dist/reports/reperform.d.ts.map +1 -0
  252. package/dist/reports/reperform.js +432 -0
  253. package/dist/reports/reperform.js.map +1 -0
  254. package/dist/reports/underwriter.d.ts +127 -0
  255. package/dist/reports/underwriter.d.ts.map +1 -0
  256. package/dist/reports/underwriter.js +348 -0
  257. package/dist/reports/underwriter.js.map +1 -0
  258. package/dist/retention.d.ts +88 -0
  259. package/dist/retention.d.ts.map +1 -0
  260. package/dist/retention.js +90 -0
  261. package/dist/retention.js.map +1 -0
  262. package/dist/roster.d.ts +113 -0
  263. package/dist/roster.d.ts.map +1 -0
  264. package/dist/roster.js +261 -0
  265. package/dist/roster.js.map +1 -0
  266. package/dist/runtime-monitor.d.ts +132 -0
  267. package/dist/runtime-monitor.d.ts.map +1 -0
  268. package/dist/runtime-monitor.js +197 -0
  269. package/dist/runtime-monitor.js.map +1 -0
  270. package/dist/settlement.d.ts +68 -0
  271. package/dist/settlement.d.ts.map +1 -0
  272. package/dist/settlement.js +306 -0
  273. package/dist/settlement.js.map +1 -0
  274. package/dist/siem.d.ts +261 -0
  275. package/dist/siem.d.ts.map +1 -0
  276. package/dist/siem.js +247 -0
  277. package/dist/siem.js.map +1 -0
  278. package/dist/store-postgres.d.ts +107 -0
  279. package/dist/store-postgres.d.ts.map +1 -0
  280. package/dist/store-postgres.js +152 -0
  281. package/dist/store-postgres.js.map +1 -0
  282. package/dist/store.d.ts +95 -0
  283. package/dist/store.d.ts.map +1 -0
  284. package/dist/store.js +202 -0
  285. package/dist/store.js.map +1 -0
  286. package/dist/strict-json.d.ts +14 -0
  287. package/dist/strict-json.d.ts.map +1 -0
  288. package/dist/strict-json.js +132 -0
  289. package/dist/strict-json.js.map +1 -0
  290. package/dist/trust-program-adapters.d.ts +122 -0
  291. package/dist/trust-program-adapters.d.ts.map +1 -0
  292. package/dist/trust-program-adapters.js +802 -0
  293. package/dist/trust-program-adapters.js.map +1 -0
  294. package/dist/trust-program-postgres.d.ts +41 -0
  295. package/dist/trust-program-postgres.d.ts.map +1 -0
  296. package/dist/trust-program-postgres.js +346 -0
  297. package/dist/trust-program-postgres.js.map +1 -0
  298. package/dist/trust-program-revocation.d.ts +206 -0
  299. package/dist/trust-program-revocation.d.ts.map +1 -0
  300. package/dist/trust-program-revocation.js +600 -0
  301. package/dist/trust-program-revocation.js.map +1 -0
  302. package/dist/trust-program.d.ts +197 -0
  303. package/dist/trust-program.d.ts.map +1 -0
  304. package/dist/trust-program.js +1351 -0
  305. package/dist/trust-program.js.map +1 -0
  306. package/dist/witness-postgres.d.ts +43 -0
  307. package/dist/witness-postgres.d.ts.map +1 -0
  308. package/dist/witness-postgres.js +92 -0
  309. package/dist/witness-postgres.js.map +1 -0
  310. package/dist/zk-range-proof.d.ts +78 -0
  311. package/dist/zk-range-proof.d.ts.map +1 -0
  312. package/dist/zk-range-proof.js +228 -0
  313. package/dist/zk-range-proof.js.map +1 -0
  314. package/eg1-conformance.js +3 -404
  315. package/enterprise.js +3 -180
  316. package/ep-assure.mjs +1 -0
  317. package/evidence-postgres.js +3 -356
  318. package/evidence.js +4 -360
  319. package/execution-binding.js +3 -212
  320. package/formal-runtime-map.js +4 -0
  321. package/index.js +4 -1249
  322. package/key-registry.js +3 -134
  323. package/mcp.js +3 -109
  324. package/metering.js +3 -226
  325. package/metrics.js +3 -231
  326. package/network-witness.js +3 -499
  327. package/package.json +35 -4
  328. package/proposal-to-effect.js +4 -0
  329. package/receipt-program.js +4 -0
  330. package/reliance-kernel.js +3 -112
  331. package/reliance-packet.js +3 -175
  332. package/remedy-program-postgres.js +4 -0
  333. package/remedy-program-receipt.js +4 -0
  334. package/remedy-program.js +4 -0
  335. package/reports/art14.js +0 -0
  336. package/reports/assurance-package.js +2 -312
  337. package/reports/auditor-workpaper.js +3 -537
  338. package/reports/external-verification.js +3 -222
  339. package/reports/reperform.js +3 -424
  340. package/reports/underwriter.js +3 -339
  341. package/retention.js +3 -84
  342. package/roster.js +3 -264
  343. package/runtime-monitor.js +4 -0
  344. package/settlement.js +3 -299
  345. package/siem.js +3 -235
  346. package/src/action-control-manifest.ts +424 -0
  347. package/src/action-escrow-custodian.ts +427 -0
  348. package/src/action-escrow-evidence.ts +1178 -0
  349. package/src/action-escrow-package.ts +1821 -0
  350. package/src/action-escrow-postgres.ts +366 -0
  351. package/src/action-escrow-state.ts +498 -0
  352. package/src/action-escrow-verifiers.ts +484 -0
  353. package/src/action-escrow.ts +3039 -0
  354. package/src/action-packs.ts +170 -0
  355. package/src/adapters/_kit.ts +117 -0
  356. package/src/adapters/aws.ts +142 -0
  357. package/src/adapters/cloudflare.ts +78 -0
  358. package/src/adapters/gcp.ts +71 -0
  359. package/src/adapters/github.ts +110 -0
  360. package/src/adapters/jira.ts +56 -0
  361. package/src/adapters/k8s.ts +71 -0
  362. package/src/adapters/linear.ts +56 -0
  363. package/src/adapters/salesforce.ts +56 -0
  364. package/src/adapters/stripe.ts +82 -0
  365. package/src/adapters/supabase.ts +122 -0
  366. package/src/adapters/terraform.ts +60 -0
  367. package/src/adapters/vercel.ts +85 -0
  368. package/src/aec-execution.ts +354 -0
  369. package/src/breakglass.ts +647 -0
  370. package/src/capability-receipt.ts +1258 -0
  371. package/src/cf1-conformance.ts +120 -0
  372. package/src/challenge-store.ts +70 -0
  373. package/src/control-plane.ts +374 -0
  374. package/src/coverage.ts +832 -0
  375. package/src/deployment-attestation.ts +259 -0
  376. package/src/eg1-conformance.ts +471 -0
  377. package/src/enterprise.ts +195 -0
  378. package/src/evidence-postgres.ts +374 -0
  379. package/src/evidence.ts +367 -0
  380. package/src/execution-binding.ts +236 -0
  381. package/src/formal-runtime-map.ts +24 -0
  382. package/src/index.ts +2127 -0
  383. package/src/key-registry.ts +150 -0
  384. package/src/mcp.ts +120 -0
  385. package/src/metering.ts +237 -0
  386. package/src/metrics.ts +232 -0
  387. package/src/network-witness.ts +585 -0
  388. package/src/proposal-to-effect.ts +571 -0
  389. package/src/protocol-state.ts +143 -0
  390. package/src/protocol-state.typecheck.ts +94 -0
  391. package/src/receipt-program.ts +1105 -0
  392. package/src/reliance-kernel.ts +119 -0
  393. package/src/reliance-packet.ts +206 -0
  394. package/src/remedy-program-postgres.ts +367 -0
  395. package/src/remedy-program-receipt.ts +891 -0
  396. package/src/remedy-program.ts +1388 -0
  397. package/src/reports/art14.ts +361 -0
  398. package/src/reports/assurance-package.ts +330 -0
  399. package/src/reports/auditor-workpaper.ts +558 -0
  400. package/src/reports/external-verification.ts +232 -0
  401. package/src/reports/reperform.ts +441 -0
  402. package/src/reports/underwriter.ts +356 -0
  403. package/src/retention.ts +100 -0
  404. package/src/roster.ts +273 -0
  405. package/src/runtime-monitor.ts +217 -0
  406. package/src/settlement.ts +313 -0
  407. package/src/siem.ts +247 -0
  408. package/src/store-postgres.ts +167 -0
  409. package/src/store.ts +214 -0
  410. package/src/strict-json.ts +115 -0
  411. package/src/trust-program-adapters.ts +847 -0
  412. package/src/trust-program-postgres.ts +429 -0
  413. package/src/trust-program-revocation.ts +751 -0
  414. package/src/trust-program.ts +1460 -0
  415. package/src/witness-postgres.ts +106 -0
  416. package/src/zk-range-proof.ts +250 -0
  417. package/store-postgres.js +3 -161
  418. package/store.js +4 -198
  419. package/strict-json.js +3 -102
  420. package/trust-program-adapters.js +4 -0
  421. package/trust-program-postgres.js +4 -0
  422. package/trust-program-revocation.js +4 -0
  423. package/trust-program.js +4 -0
  424. package/witness-postgres.js +3 -96
  425. package/zk-range-proof.js +4 -0
@@ -0,0 +1,150 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EMILIA Gate — issuer key registry (production key custody for the VERIFIER).
4
+ *
5
+ * A flat `trustedKeys: string[]` cannot express the two things a production
6
+ * deployment needs:
7
+ * 1. ROTATION — an issuer key is valid only for a window; a new key overlaps
8
+ * the old one, then the old one retires, without rejecting receipts that
9
+ * were legitimately issued while it was current.
10
+ * 2. REVOCATION — a COMPROMISED issuer key must be rejected immediately, for
11
+ * every receipt, regardless of the issuance time the (now-untrusted) key
12
+ * claims. Fail closed: once revoked, the key signs nothing the gate accepts.
13
+ *
14
+ * The registry resolves, for a given receipt issuance time, the set of public
15
+ * keys the gate should verify against — excluding revoked keys entirely and
16
+ * windowed keys whose [not_before, not_after] does not contain that time. The
17
+ * gate passes that resolved set to the receipt verifier, so an excluded key's
18
+ * signature simply does not verify and the action is refused.
19
+ *
20
+ * A key with no window and no revocation behaves exactly like a flat trustedKeys
21
+ * entry (back-compatible).
22
+ */
23
+
24
+ const RFC3339_INSTANT = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(?:Z|([+-])(\d{2}):(\d{2}))$/;
25
+
26
+ type RawKeyEntry = {
27
+ kid?: string;
28
+ key: string;
29
+ not_before?: string;
30
+ not_after?: string;
31
+ revoked_at?: string;
32
+ };
33
+ type NormalizedKeyEntry = {
34
+ kid: string;
35
+ key: string;
36
+ not_before: number | null;
37
+ not_after: number | null;
38
+ revoked_at: number | null;
39
+ };
40
+
41
+ function strictInstantMs(value) {
42
+ if (typeof value !== 'string') return NaN;
43
+ const match = value.match(RFC3339_INSTANT);
44
+ if (!match) return NaN;
45
+ const [, y, mo, d, h, mi, s, , oh, om] = match;
46
+ const calendar = new Date(0);
47
+ calendar.setUTCFullYear(Number(y), Number(mo) - 1, Number(d));
48
+ calendar.setUTCHours(Number(h), Number(mi), Number(s), 0);
49
+ if (calendar.toISOString().slice(0, 19) !== `${y}-${mo}-${d}T${h}:${mi}:${s}`) return NaN;
50
+ if (oh !== undefined && (Number(oh) > 23 || Number(om) > 59)) return NaN;
51
+ const ms = Date.parse(value);
52
+ return Number.isFinite(ms) ? ms : NaN;
53
+ }
54
+
55
+ function optionalInstant(entry, field) {
56
+ if (!Object.hasOwn(entry, field)) return null;
57
+ const ms = strictInstantMs(entry[field]);
58
+ if (!Number.isFinite(ms)) {
59
+ throw new Error(`key registry: ${field} must be a valid RFC3339 instant when supplied`);
60
+ }
61
+ return ms;
62
+ }
63
+
64
+ /**
65
+ * @param {Array<object>} entries each: {
66
+ * kid?: string, key: string (base64url SPKI-DER public key),
67
+ * not_before?: string, not_after?: string, revoked_at?: string (strict RFC3339)
68
+ * }
69
+ */
70
+ export function createKeyRegistry(entries: RawKeyEntry[] = []) {
71
+ const list: NormalizedKeyEntry[] = [];
72
+ function normalize(e) {
73
+ if (!e || !e.key || typeof e.key !== 'string') throw new Error('key registry entry requires a base64url SPKI key');
74
+ const normalized = {
75
+ kid: e.kid || e.key.slice(0, 16),
76
+ key: e.key,
77
+ not_before: optionalInstant(e, 'not_before'),
78
+ not_after: optionalInstant(e, 'not_after'),
79
+ revoked_at: optionalInstant(e, 'revoked_at'),
80
+ };
81
+ if (normalized.not_before != null && normalized.not_after != null
82
+ && normalized.not_after < normalized.not_before) {
83
+ throw new Error('key registry: not_after must not precede not_before');
84
+ }
85
+ return normalized;
86
+ }
87
+ for (const e of entries) list.push(normalize(e));
88
+
89
+ /** Is this entry usable to verify a receipt issued at `atMs`? */
90
+ function entryActiveAt(entry, atMs) {
91
+ if (entry.revoked_at != null) return false; // HARD revocation: never trust a revoked key
92
+ if (!Number.isFinite(atMs)) return false;
93
+ if (entry.not_before != null && atMs < entry.not_before) return false;
94
+ if (entry.not_after != null && atMs > entry.not_after) return false;
95
+ return true;
96
+ }
97
+
98
+ return {
99
+ /** The base64url public keys to verify a receipt issued at strict RFC3339 `at`. */
100
+ keysValidAt(at) {
101
+ const atMs = strictInstantMs(at);
102
+ return list.filter((e) => entryActiveAt(e, atMs)).map((e) => e.key);
103
+ },
104
+ /** Mark a kid revoked as of `at` (default: now-as-supplied). Fail-closed thereafter. */
105
+ revoke(kid: string, at?: string) {
106
+ let revokedAt = 0;
107
+ if (arguments.length > 1) {
108
+ revokedAt = strictInstantMs(at);
109
+ if (!Number.isFinite(revokedAt)) {
110
+ throw new Error('key registry: revoked_at must be a valid RFC3339 instant when supplied');
111
+ }
112
+ }
113
+ let n = 0;
114
+ for (const e of list) {
115
+ if (e.kid === kid && e.revoked_at == null) { e.revoked_at = revokedAt; n += 1; }
116
+ }
117
+ if (n === 0) throw new Error(`key registry: no active key with kid "${kid}" to revoke`);
118
+ return n;
119
+ },
120
+ /** Add/rotate in a new key. */
121
+ add(entry) { list.push(normalize(entry)); return this; },
122
+ /** Operational snapshot (no private material; keys are public). */
123
+ status(at) {
124
+ const supplied = arguments.length > 0;
125
+ const atMs = supplied ? strictInstantMs(at) : NaN;
126
+ return list.map((e) => ({
127
+ kid: e.kid,
128
+ revoked: e.revoked_at != null,
129
+ active: supplied
130
+ ? entryActiveAt(e, atMs)
131
+ : e.revoked_at == null && e.not_before == null && e.not_after == null,
132
+ not_before: e.not_before,
133
+ not_after: e.not_after,
134
+ revoked_at: e.revoked_at,
135
+ }));
136
+ },
137
+ get size() { return list.length; },
138
+ };
139
+ }
140
+
141
+ /** Coerce a flat trustedKeys[] OR a registry into a registry (back-compat). */
142
+ export function asKeyRegistry(trustedKeysOrRegistry) {
143
+ if (trustedKeysOrRegistry && typeof trustedKeysOrRegistry.keysValidAt === 'function') {
144
+ return trustedKeysOrRegistry;
145
+ }
146
+ const keys = Array.isArray(trustedKeysOrRegistry) ? trustedKeysOrRegistry : [];
147
+ return createKeyRegistry(keys.map((key) => ({ key })));
148
+ }
149
+
150
+ export default { createKeyRegistry, asKeyRegistry };
package/src/mcp.ts ADDED
@@ -0,0 +1,120 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EMILIA Gate — MCP drop-in. The lowest-friction place to put the firewall:
4
+ * agents already live at the MCP tool-call boundary, and a single wrapper turns
5
+ * a dangerous MCP tool into a receipt-required one.
6
+ *
7
+ * import { createTrustedActionFirewall } from '@emilia-protocol/gate';
8
+ * import { gateMcpTool } from '@emilia-protocol/gate/mcp';
9
+ * const gate = createTrustedActionFirewall({ trustedKeys: [ISSUER], store: sharedConsumptionStore });
10
+ *
11
+ * server.tool('release_payment',
12
+ * gateMcpTool(gate, { tool: 'release_payment' }, async (args) => actuallyPay(args)));
13
+ *
14
+ * A guarded call with no valid, sufficiently-assured, non-replayed receipt
15
+ * returns a structured MCP error (isError) carrying the Receipt-Required
16
+ * challenge, so the agent knows to go get a human/quorum to authorize THIS exact
17
+ * action. On success the tool runs and an execution proof + reliance packet are
18
+ * attached under `_emilia`.
19
+ *
20
+ * Receipt resolution order (override with opts.receipt): args._emilia_receipt,
21
+ * args.emilia_receipt, then a base64 string in args._emilia_receipt_b64.
22
+ */
23
+
24
+ import { parseReceiptCarrier } from '@emilia-protocol/require-receipt';
25
+
26
+ type Obj = Record<string, any>;
27
+
28
+ type GateMcpToolOptions = {
29
+ tool?: string;
30
+ protocol?: string;
31
+ action?: string;
32
+ observedAction?: Obj | ((args: Obj, extra: any) => any);
33
+ receipt?: Obj | ((args: Obj) => any);
34
+ };
35
+
36
+ function resolveReceipt(args, opts) {
37
+ if (typeof opts.receipt === 'function') return opts.receipt(args);
38
+ if (opts.receipt) return opts.receipt;
39
+ if (args && typeof args === 'object') {
40
+ if (args._emilia_receipt) return args._emilia_receipt;
41
+ if (args.emilia_receipt) return args.emilia_receipt;
42
+ if (typeof args._emilia_receipt_b64 === 'string') {
43
+ return parseReceiptCarrier(args._emilia_receipt_b64);
44
+ }
45
+ }
46
+ return null;
47
+ }
48
+
49
+ /**
50
+ * Wrap a single MCP tool handler so it runs only behind a passing gate check.
51
+ * @param {object} gate an EMILIA Gate (createGate/createTrustedActionFirewall)
52
+ * @param {object} o
53
+ * @param {string} [o.tool] the MCP tool name (matched against the manifest)
54
+ * @param {string} [o.protocol='mcp']
55
+ * @param {string} [o.action] explicit action_type (else resolved by the manifest from {protocol,tool})
56
+ * @param {object|function} [o.observedAction] the system-of-record facts to bind (default: the tool args)
57
+ * @param {object|function} [o.receipt] override receipt resolution
58
+ * @param {function} handler the real tool implementation (args, extra) => result
59
+ * @returns {function} a gated MCP tool handler
60
+ */
61
+ export function gateMcpTool(gate, o: GateMcpToolOptions = {}, handler) {
62
+ if (!gate || typeof gate.run !== 'function') throw new Error('gateMcpTool requires an EMILIA Gate (with .run)');
63
+ if (typeof handler !== 'function') throw new Error('gateMcpTool requires a tool handler function');
64
+ const { tool, protocol = 'mcp', action } = o;
65
+ if (!tool) throw new Error('gateMcpTool requires { tool }');
66
+
67
+ const refused = (reason, body = null) => ({
68
+ isError: true,
69
+ content: [{
70
+ type: 'text',
71
+ text: `EMILIA Gate refused "${tool}": ${reason}. `
72
+ + 'This is a high-risk action; present a valid, sufficiently-assured, unused human/quorum receipt.',
73
+ }],
74
+ _emilia: {
75
+ gate: 'refused',
76
+ status: 428,
77
+ reason,
78
+ challenge: body,
79
+ },
80
+ });
81
+
82
+ return async function gatedTool(args = {}, extra) {
83
+ const selector = { protocol, tool, ...(action ? { action_type: action } : {}) };
84
+ let receipt;
85
+ let observedAction;
86
+ try {
87
+ receipt = resolveReceipt(args, o);
88
+ observedAction = typeof o.observedAction === 'function'
89
+ ? o.observedAction(args, extra)
90
+ : (o.observedAction ?? args);
91
+ } catch {
92
+ return refused('receipt_boundary_failed');
93
+ }
94
+
95
+ const out = await gate.run({ selector, receipt, observedAction }, () => handler(args, extra));
96
+ if (!out.ok) {
97
+ return refused(out.authorization.reason, out.body);
98
+ }
99
+ const result = out.result;
100
+ // Attach the proof without clobbering a structured tool result.
101
+ if (result && typeof result === 'object' && !Array.isArray(result)) {
102
+ return { ...result, _emilia: { gate: 'allowed', execution: out.execution, reliance: out.packet } };
103
+ }
104
+ return { result, _emilia: { gate: 'allowed', execution: out.execution, reliance: out.packet } };
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Convenience: wrap a map of { toolName: handler } in one call. Tools not named
110
+ * in the manifest still pass through the gate (which lets non-guarded tools run).
111
+ */
112
+ export function gateMcpTools(gate, handlers = {}, opts = {}) {
113
+ const wrapped = {};
114
+ for (const [tool, handler] of Object.entries(handlers)) {
115
+ wrapped[tool] = gateMcpTool(gate, { ...opts, tool }, handler);
116
+ }
117
+ return wrapped;
118
+ }
119
+
120
+ export default { gateMcpTool, gateMcpTools };
@@ -0,0 +1,237 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EMILIA Gate — usage metering (the value metric, EP-GATE-USAGE-v1).
4
+ *
5
+ * Pricing scales with what the gate actually protects: PROTECTED IRREVERSIBLE
6
+ * ACTIONS (guarded decisions — allows AND denies both consume enforcement) plus
7
+ * RECEIPT-YEARS under retention (one receipt kept one year = one receipt-year;
8
+ * horizons follow retention.js, default 6y = 2190 days). NEVER seats.
9
+ *
10
+ * Pure functions over evidence-log entries (`evidence.all()`); no clock, no
11
+ * network. The billing period is an explicit [periodStart, periodEnd) window —
12
+ * INCLUSIVE start, EXCLUSIVE end — so adjacent periods never double-count an
13
+ * entry sitting exactly on a boundary.
14
+ *
15
+ * Fail-closed posture for billing: an unbounded or reversed period is refused
16
+ * (thrown); malformed entries are surfaced in `integrity_warnings` rather than
17
+ * silently dropped; an entry whose allow flag is not literally `true` is
18
+ * counted as a deny; a statement over foreign or malformed usage is refused.
19
+ * `buildUsageStatement` output is deterministic (sorted keys + content hash)
20
+ * and UNSIGNED — the deployer signs it; the content hash binds that signature
21
+ * to exactly these numbers for billing reconciliation.
22
+ */
23
+ import crypto from 'node:crypto';
24
+
25
+ export const USAGE_VERSION = 'EP-GATE-USAGE-v1';
26
+
27
+ const DAYS_PER_YEAR = 365;
28
+ // Mirrors retention.js's cold-horizon default (coldDays = 2190 = 6y): a receipt
29
+ // is presumed retained for the full audit horizon unless the entry states its own.
30
+ const DEFAULT_RETENTION_YEARS = 2190 / DAYS_PER_YEAR;
31
+
32
+ function sha256hex(s) {
33
+ return crypto.createHash('sha256').update(s).digest('hex');
34
+ }
35
+
36
+ /** Canonical JSON (recursive sorted keys) — matches evidence.js / @emilia-protocol/verify. */
37
+ function canonical(v) {
38
+ if (v === null || v === undefined) return JSON.stringify(v);
39
+ if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`;
40
+ if (typeof v === 'object') {
41
+ return `{${Object.keys(v).sort().map((k) => JSON.stringify(k) + ':' + canonical(v[k])).join(',')}}`;
42
+ }
43
+ return JSON.stringify(v);
44
+ }
45
+
46
+ function toMs(t) {
47
+ if (t == null) return null;
48
+ const ms = typeof t === 'number' ? t : Date.parse(t);
49
+ return Number.isFinite(ms) ? ms : null;
50
+ }
51
+
52
+ /** Key-sorted copy so output is byte-stable regardless of entry order. */
53
+ function sortedCounts(map) {
54
+ const out = {};
55
+ for (const k of Object.keys(map).sort()) out[k] = map[k];
56
+ return out;
57
+ }
58
+
59
+ /** Entry-level day resolution (1/365y ≈ 0.00274) without float noise. */
60
+ function round6(x) {
61
+ return Math.round(x * 1e6) / 1e6;
62
+ }
63
+
64
+ /**
65
+ * The retention this entry STATES for itself, in years.
66
+ * @returns {number|null|NaN} years, `null` when unstated (default applies),
67
+ * `NaN` when stated but invalid (default applies + integrity warning — a
68
+ * malformed stated value must never silently shrink the metered total).
69
+ */
70
+ function statedRetentionYears(e) {
71
+ if (e.retention_years != null) {
72
+ const y = Number(e.retention_years);
73
+ return Number.isFinite(y) && y >= 0 ? y : NaN;
74
+ }
75
+ if (e.retention_days != null) {
76
+ const d = Number(e.retention_days);
77
+ return Number.isFinite(d) && d >= 0 ? d / DAYS_PER_YEAR : NaN;
78
+ }
79
+ return null;
80
+ }
81
+
82
+ /**
83
+ * Meter a billing period over evidence entries.
84
+ *
85
+ * Billable = `kind: 'decision'` entries on GUARDED actions (`not_guarded`
86
+ * pass-throughs are free; execution records are provenance, not enforcement).
87
+ * Window is [periodStart, periodEnd): inclusive start, exclusive end.
88
+ *
89
+ * @param {Array<object>} entries evidence.all()
90
+ * @param {object} [o]
91
+ * @param {string|number} [o.periodStart] ISO or ms — required
92
+ * @param {string|number} [o.periodEnd] ISO or ms — required, >= periodStart
93
+ * @param {number} [o.retentionYearsDefault=6] applied when an entry states no retention
94
+ * @returns {{'@version':string, protected_actions:number, allows:number, denies:number,
95
+ * replays_blocked:number, by_action_type:object, by_tier:object,
96
+ * receipt_years:number, retention_years_default:number, period:object, integrity_warnings:object[]}}
97
+ */
98
+ export function meterUsage(entries: any[] = [], {
99
+ periodStart,
100
+ periodEnd,
101
+ retentionYearsDefault = DEFAULT_RETENTION_YEARS,
102
+ }: {
103
+ periodStart?: string | number;
104
+ periodEnd?: string | number;
105
+ retentionYearsDefault?: number;
106
+ } = {}) {
107
+ const startMs = toMs(periodStart);
108
+ const endMs = toMs(periodEnd);
109
+ // An unbounded or reversed period is not meterable — refuse rather than emit
110
+ // a statement whose window cannot be reconciled.
111
+ if (startMs == null || endMs == null) {
112
+ throw new Error('meterUsage: periodStart and periodEnd are required (ISO string or ms)');
113
+ }
114
+ if (endMs < startMs) throw new Error('meterUsage: periodEnd must be >= periodStart');
115
+ if (!Number.isFinite(retentionYearsDefault) || retentionYearsDefault < 0) {
116
+ throw new Error('meterUsage: retentionYearsDefault must be a finite number >= 0');
117
+ }
118
+
119
+ const warnings: { index: number; reason: string }[] = [];
120
+ const byAction = Object.create(null);
121
+ const byTier = Object.create(null);
122
+ let protectedActions = 0;
123
+ let allows = 0;
124
+ let denies = 0;
125
+ let replaysBlocked = 0;
126
+ let receiptYears = 0;
127
+
128
+ entries.forEach((e, index) => {
129
+ if (!e || typeof e !== 'object' || Array.isArray(e)) {
130
+ warnings.push({ index, reason: 'not_an_object' });
131
+ return;
132
+ }
133
+ const t = toMs(e.at);
134
+ if (t == null) {
135
+ // Unplaceable in ANY period — surfaced, never silently dropped.
136
+ warnings.push({ index, reason: 'unparseable_at' });
137
+ return;
138
+ }
139
+ if (t < startMs || t >= endMs) return; // outside [start, end)
140
+ if (typeof e.kind !== 'string' || e.kind.length === 0) {
141
+ warnings.push({ index, reason: 'missing_kind' });
142
+ return;
143
+ }
144
+ if (e.kind !== 'decision') return; // execution/other records are not billable
145
+ if (e.reason === 'not_guarded') return; // pass-throughs are free by design
146
+
147
+ protectedActions += 1;
148
+ // Fail closed: only a literal `true` counts as an allow.
149
+ if (e.allow === true) allows += 1; else denies += 1;
150
+ if (e.reason === 'replay_refused') replaysBlocked += 1;
151
+
152
+ const action = typeof e.action === 'string' && e.action ? e.action : 'unknown';
153
+ byAction[action] = (byAction[action] || 0) + 1;
154
+ const tier = typeof e.required_tier === 'string' && e.required_tier ? e.required_tier : 'unknown';
155
+ byTier[tier] = (byTier[tier] || 0) + 1;
156
+
157
+ const stated = statedRetentionYears(e);
158
+ if (Number.isNaN(stated)) {
159
+ warnings.push({ index, reason: 'invalid_stated_retention' });
160
+ receiptYears += retentionYearsDefault;
161
+ } else {
162
+ receiptYears += stated ?? retentionYearsDefault;
163
+ }
164
+ });
165
+
166
+ return {
167
+ '@version': USAGE_VERSION,
168
+ period: {
169
+ start: new Date(startMs).toISOString(),
170
+ end: new Date(endMs).toISOString(),
171
+ bounds: 'inclusive_start_exclusive_end',
172
+ },
173
+ protected_actions: protectedActions,
174
+ allows,
175
+ denies,
176
+ replays_blocked: replaysBlocked,
177
+ by_action_type: sortedCounts(byAction),
178
+ by_tier: sortedCounts(byTier),
179
+ receipt_years: round6(receiptYears),
180
+ retention_years_default: retentionYearsDefault,
181
+ integrity_warnings: warnings,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Build the signed-ready usage statement handed to billing reconciliation.
187
+ * UNSIGNED — the deployer signs it; `content_hash` (sha256 over the canonical
188
+ * JSON of everything else) binds that signature to exactly these numbers.
189
+ * Deterministic: same usage + org → byte-identical statement, regardless of
190
+ * the entry order the usage was metered from.
191
+ * @param {object} usage a USAGE_VERSION object from meterUsage
192
+ * @param {{ org?: string }} [o]
193
+ */
194
+ export function buildUsageStatement(usage, { org }: { org?: string } = {}) {
195
+ // Never emit a statement over an artifact of a different or unknown format,
196
+ // and never one that fails to name who it bills.
197
+ if (!usage || usage['@version'] !== USAGE_VERSION) {
198
+ throw new Error(`buildUsageStatement: usage must be a ${USAGE_VERSION} object from meterUsage`);
199
+ }
200
+ if (!org || typeof org !== 'string') {
201
+ throw new Error('buildUsageStatement: org is required — a statement must name the billed party');
202
+ }
203
+ if (!usage.period || !usage.period.start || !usage.period.end) {
204
+ throw new Error('buildUsageStatement: usage.period is missing');
205
+ }
206
+ for (const k of ['protected_actions', 'allows', 'denies', 'replays_blocked', 'receipt_years']) {
207
+ if (!Number.isFinite(usage[k]) || usage[k] < 0) {
208
+ throw new Error(`buildUsageStatement: usage.${k} must be a finite number >= 0`);
209
+ }
210
+ }
211
+ const warningCount = Array.isArray(usage.integrity_warnings) ? usage.integrity_warnings.length : 0;
212
+ const body = {
213
+ '@version': USAGE_VERSION,
214
+ kind: 'usage_statement',
215
+ org,
216
+ period: {
217
+ start: usage.period.start,
218
+ end: usage.period.end,
219
+ bounds: 'inclusive_start_exclusive_end',
220
+ },
221
+ protected_actions: usage.protected_actions,
222
+ allows: usage.allows,
223
+ denies: usage.denies,
224
+ replays_blocked: usage.replays_blocked,
225
+ by_action_type: sortedCounts(usage.by_action_type || {}),
226
+ by_tier: sortedCounts(usage.by_tier || {}),
227
+ receipt_years: usage.receipt_years,
228
+ retention_years_default: usage.retention_years_default ?? DEFAULT_RETENTION_YEARS,
229
+ // Data-quality signal for the reconciler: a statement metered over a log
230
+ // with integrity warnings is flagged incomplete, never silently clean.
231
+ integrity_warning_count: warningCount,
232
+ complete: warningCount === 0,
233
+ };
234
+ return { ...body, content_hash: sha256hex(canonical(body)) };
235
+ }
236
+
237
+ export default { meterUsage, buildUsageStatement, USAGE_VERSION };