@emilia-protocol/gate 0.10.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 +91 -0
  2. package/README.md +272 -0
  3. package/action-control-manifest.js +3 -268
  4. package/action-escrow-custodian.js +4 -0
  5. package/action-escrow-evidence.js +4 -0
  6. package/action-escrow-package.js +4 -0
  7. package/action-escrow-postgres.js +4 -0
  8. package/action-escrow-state.js +4 -0
  9. package/action-escrow-verifiers.js +4 -0
  10. package/action-escrow.js +4 -0
  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 +47 -4
  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 -85
  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
package/src/roster.ts ADDED
@@ -0,0 +1,273 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * EMILIA Gate — signer-roster sync from an enterprise IdP (EP-GATE-ROSTER-v1).
4
+ *
5
+ * The boring key story: WHO may approve is an HR fact, not a crypto fact. This
6
+ * module turns a SCIM-like IdP export into a versioned signer roster and
7
+ * reconciles a key registry (key-registry.js) against it, so a deprovisioned
8
+ * employee STOPS being an acceptable approver on the next sync. Fail closed:
9
+ * - only `active === true` users' keys are ever pinned; anything else
10
+ * (false, missing, truthy-but-not-boolean) never pins;
11
+ * - a user absent from the import — offboarded, or silently dropped by a
12
+ * broken IdP export — has every previously pinned key revoked;
13
+ * - a kid claimed by two principals (or carrying two different key
14
+ * materials) is CONTESTED: it pins nothing and is revoked if present;
15
+ * - an import that would leave ZERO active signers requires an explicit
16
+ * `allowEmpty` acknowledgment, so an empty/broken IdP response cannot
17
+ * silently mass-revoke every approver.
18
+ *
19
+ * importRoster/diffRoster are pure (inputs in, artifact out; `importedAt` is
20
+ * the caller-supplied clock). applyRosterToRegistry mutates the given registry
21
+ * through its real API: key-registry CAN express revocation (`revoke(kid, at)`,
22
+ * hard and fail-closed), so reconciliation performs it directly and the
23
+ * returned `revoked` list is the exact set of revocations performed.
24
+ */
25
+
26
+ export const ROSTER_VERSION = 'EP-GATE-ROSTER-v1';
27
+
28
+ /**
29
+ * Import a SCIM-like IdP user export into a signer roster.
30
+ * @param {Array<{id:string, userName:string, active:boolean, emails?:any, keys?:Array<{kid:string, publicKey:string}>}>} idpUsers
31
+ * @param {object} [o]
32
+ * @param {string} [o.source] IdP provenance, e.g. 'scim:okta:acme' (required)
33
+ * @param {string|number} [o.importedAt] import time (ISO or ms); the caller's clock
34
+ * @param {boolean} [o.allowEmpty=false] acknowledge an import with zero active signers
35
+ * @returns {{version:string, source:string, imported_at:string, signers:Array<{principal:string, kid:string, publicKey:string, active:boolean}>, integrity_warnings:object[]}}
36
+ */
37
+ export function importRoster(idpUsers, {
38
+ source,
39
+ importedAt,
40
+ allowEmpty = false,
41
+ }: {
42
+ source?: string;
43
+ importedAt?: string | number;
44
+ allowEmpty?: boolean;
45
+ } = {}) {
46
+ if (!Array.isArray(idpUsers)) throw new Error('roster import: idpUsers must be an array');
47
+ if (!source || typeof source !== 'string') {
48
+ throw new Error('roster import: source (IdP provenance, e.g. "scim:okta:acme") is required');
49
+ }
50
+ const atMs = importedAt == null
51
+ ? Date.now()
52
+ : (typeof importedAt === 'number' ? importedAt : Date.parse(importedAt));
53
+ if (!Number.isFinite(atMs)) throw new Error('roster import: importedAt is not a valid time');
54
+
55
+ const warnings: Record<string, any>[] = [];
56
+ const candidates: { principal: string; kid: string; publicKey: string; active: boolean }[] = [];
57
+ const usersByPrincipal = new Map();
58
+
59
+ for (const u of idpUsers) {
60
+ // A user without a stable id AND a userName cannot be diffed or revoked
61
+ // reliably later — excluded, never an approver.
62
+ if (!u || typeof u !== 'object'
63
+ || typeof u.id !== 'string' || !u.id
64
+ || typeof u.userName !== 'string' || !u.userName) {
65
+ warnings.push({ code: 'malformed_user', id: u?.id ?? null, userName: u?.userName ?? null });
66
+ continue;
67
+ }
68
+ const principal = u.userName;
69
+ usersByPrincipal.set(principal, (usersByPrincipal.get(principal) || 0) + 1);
70
+ const active = u.active === true; // strictly boolean true; anything else never pins
71
+ for (const k of Array.isArray(u.keys) ? u.keys : []) {
72
+ if (!k || typeof k.kid !== 'string' || !k.kid
73
+ || typeof k.publicKey !== 'string' || !k.publicKey) {
74
+ warnings.push({ code: 'malformed_key', principal, kid: k?.kid ?? null });
75
+ continue;
76
+ }
77
+ candidates.push({ principal, kid: k.kid, publicKey: k.publicKey, active });
78
+ }
79
+ }
80
+
81
+ // Two IdP users claiming one principal: identity is ambiguous — neither pins.
82
+ const contestedPrincipals = new Set();
83
+ for (const [principal, count] of usersByPrincipal) {
84
+ if (count > 1) {
85
+ contestedPrincipals.add(principal);
86
+ warnings.push({ code: 'duplicate_principal', principal, users: count });
87
+ }
88
+ }
89
+ let entries = candidates.filter((c) => !contestedPrincipals.has(c.principal));
90
+
91
+ // Exact-duplicate rows collapse silently (same principal + kid + key).
92
+ const seen = new Set();
93
+ entries = entries.filter((c) => {
94
+ const id = `${c.principal}\n${c.kid}\n${c.publicKey}`;
95
+ if (seen.has(id)) return false;
96
+ seen.add(id);
97
+ return true;
98
+ });
99
+
100
+ // One kid claimed by two principals, or one kid carrying two key materials,
101
+ // is an integrity failure: the contested kid pins NOTHING for anyone.
102
+ const byKid = new Map();
103
+ for (const c of entries) {
104
+ const g = byKid.get(c.kid) || { principals: new Set(), keys: new Set() };
105
+ g.principals.add(c.principal);
106
+ g.keys.add(c.publicKey);
107
+ byKid.set(c.kid, g);
108
+ }
109
+ const contestedKids = new Set();
110
+ for (const [kid, g] of byKid) {
111
+ if (g.principals.size > 1 || g.keys.size > 1) {
112
+ contestedKids.add(kid);
113
+ warnings.push({ code: 'duplicate_kid', kid, principals: [...g.principals].sort() });
114
+ }
115
+ }
116
+ entries = entries.filter((c) => !contestedKids.has(c.kid));
117
+
118
+ // Mass-deprovision guard: a roster with zero active signers would, on apply,
119
+ // revoke EVERY approver. That is indistinguishable from a broken IdP export,
120
+ // so it requires the operator's explicit acknowledgment.
121
+ if (!allowEmpty && !entries.some((e) => e.active)) {
122
+ throw new Error('roster import: zero active signers (mass-deprovision guard) — pass allowEmpty:true to acknowledge revoking every approver');
123
+ }
124
+
125
+ entries.sort((a, b) => (a.principal < b.principal ? -1 : a.principal > b.principal ? 1
126
+ : a.kid < b.kid ? -1 : a.kid > b.kid ? 1 : 0));
127
+
128
+ return {
129
+ version: ROSTER_VERSION,
130
+ source,
131
+ imported_at: new Date(atMs).toISOString(),
132
+ signers: entries.map(({ principal, kid, publicKey, active }) => ({ principal, kid, publicKey, active })),
133
+ integrity_warnings: warnings,
134
+ };
135
+ }
136
+
137
+ /** A roster must be well-formed before it can drive a diff or a registry mutation. */
138
+ function assertRoster(r, name) {
139
+ if (!r || r.version !== ROSTER_VERSION || !Array.isArray(r.signers)) {
140
+ throw new Error(`roster: ${name} is not an ${ROSTER_VERSION} roster`);
141
+ }
142
+ for (const s of r.signers) {
143
+ if (!s || typeof s.principal !== 'string' || !s.principal
144
+ || typeof s.kid !== 'string' || !s.kid
145
+ || typeof s.publicKey !== 'string' || !s.publicKey
146
+ || typeof s.active !== 'boolean') {
147
+ throw new Error(`roster: ${name} contains a malformed signer entry`);
148
+ }
149
+ }
150
+ }
151
+
152
+ function groupByPrincipal(roster) {
153
+ const m = new Map();
154
+ for (const s of roster.signers) {
155
+ const g = m.get(s.principal) || { kids: new Set(), active: false };
156
+ g.kids.add(s.kid);
157
+ if (s.active === true) g.active = true;
158
+ m.set(s.principal, g);
159
+ }
160
+ return m;
161
+ }
162
+
163
+ /**
164
+ * Diff two rosters at the principal level.
165
+ * `removed`/`deactivated` carry the PREVIOUS roster's kids — the revocation
166
+ * candidates; `added` carries the next roster's kids.
167
+ * @returns {{added:Array<{principal:string,kids:string[]}>, removed:Array<{principal:string,kids:string[]}>, deactivated:Array<{principal:string,kids:string[]}>}}
168
+ */
169
+ export function diffRoster(previous, next) {
170
+ assertRoster(previous, 'previous');
171
+ assertRoster(next, 'next');
172
+ const prev = groupByPrincipal(previous);
173
+ const nxt = groupByPrincipal(next);
174
+ const added: { principal: string; kids: string[] }[] = [];
175
+ const removed: { principal: string; kids: string[] }[] = [];
176
+ const deactivated: { principal: string; kids: string[] }[] = [];
177
+ for (const [principal, g] of nxt) {
178
+ if (!prev.has(principal)) added.push({ principal, kids: [...g.kids].sort() });
179
+ }
180
+ for (const [principal, g] of prev) {
181
+ if (!nxt.has(principal)) { removed.push({ principal, kids: [...g.kids].sort() }); continue; }
182
+ if (g.active && !nxt.get(principal).active) deactivated.push({ principal, kids: [...g.kids].sort() });
183
+ }
184
+ const byPrincipal = (a, b) => (a.principal < b.principal ? -1 : a.principal > b.principal ? 1 : 0);
185
+ added.sort(byPrincipal); removed.sort(byPrincipal); deactivated.sort(byPrincipal);
186
+ return { added, removed, deactivated };
187
+ }
188
+
189
+ /**
190
+ * Reconcile a key registry (createKeyRegistry) against a roster:
191
+ * - PIN each ACTIVE, uncontested signer's key not already present;
192
+ * - REVOKE every registry kid not owned by an active roster signer — absent
193
+ * or inactive means deprovisioned. The registry passed here must therefore
194
+ * be DEDICATED to roster-managed approver keys.
195
+ * key-registry's API DOES express revocation (revoke(kid, at) — hard,
196
+ * fail-closed), so revocations are performed directly; `revoked` is the exact
197
+ * set performed, returned for the caller's evidence trail.
198
+ * A kid the registry has EVER revoked is never re-pinned (a rehire gets a new
199
+ * key; revoked key material stays dead).
200
+ * @param {object} roster an EP-GATE-ROSTER-v1 roster
201
+ * @param {object} registry createKeyRegistry() instance (add/revoke/status)
202
+ * @param {object} [o]
203
+ * @param {string|number} [o.revokedAt=roster.imported_at] revocation timestamp
204
+ * @returns {{pinned:Array<{principal:string,kid:string}>, already_pinned:string[], revoked:Array<{kid:string,revoked_at:string|number,reason:string}>, refused:Array<{principal:string,kid:string,reason:string}>}}
205
+ */
206
+ export function applyRosterToRegistry(roster, registry, { revokedAt }: { revokedAt?: string | number } = {}) {
207
+ assertRoster(roster, 'roster');
208
+ // Fail closed on the registry too: never coerce a flat key array here —
209
+ // asKeyRegistry would build a DETACHED registry and every revocation this
210
+ // function performs would be silently lost.
211
+ if (!registry || typeof registry.add !== 'function'
212
+ || typeof registry.revoke !== 'function' || typeof registry.status !== 'function') {
213
+ throw new Error('roster apply: registry must expose add/revoke/status (createKeyRegistry)');
214
+ }
215
+ const at = revokedAt ?? roster.imported_at;
216
+
217
+ // Recompute kid contests here as well — a hand-built roster must not bypass
218
+ // the import-time duplicate-kid integrity check.
219
+ const byKid = new Map();
220
+ for (const s of roster.signers) {
221
+ const g = byKid.get(s.kid) || { principals: new Set(), keys: new Set() };
222
+ g.principals.add(s.principal);
223
+ g.keys.add(s.publicKey);
224
+ byKid.set(s.kid, g);
225
+ }
226
+ const contested = new Set();
227
+ for (const [kid, g] of byKid) {
228
+ if (g.principals.size > 1 || g.keys.size > 1) contested.add(kid);
229
+ }
230
+
231
+ const activeByKid = new Map();
232
+ for (const s of roster.signers) {
233
+ if (s.active === true && !contested.has(s.kid)) activeByKid.set(s.kid, s);
234
+ }
235
+
236
+ const status = registry.status();
237
+ const everRevoked = new Set(status.filter((e) => e.revoked).map((e) => e.kid));
238
+ const present = new Set(status.map((e) => e.kid));
239
+
240
+ const pinned: { principal: string; kid: string }[] = [];
241
+ const alreadyPinned: string[] = [];
242
+ const revoked: { kid: string; revoked_at: string | number; reason: string }[] = [];
243
+ const refused: { principal: string; kid: string; reason: string }[] = [];
244
+
245
+ for (const s of roster.signers) {
246
+ if (s.active === true && contested.has(s.kid)) {
247
+ refused.push({ principal: s.principal, kid: s.kid, reason: 'contested_kid' });
248
+ }
249
+ }
250
+
251
+ for (const [kid, s] of activeByKid) {
252
+ if (everRevoked.has(kid)) {
253
+ refused.push({ principal: s.principal, kid, reason: 'kid_previously_revoked' });
254
+ continue;
255
+ }
256
+ if (present.has(kid)) { alreadyPinned.push(kid); continue; }
257
+ registry.add({ kid, key: s.publicKey });
258
+ pinned.push({ principal: s.principal, kid });
259
+ }
260
+
261
+ // Absent-or-inactive (and contested) kids stop being acceptable NOW.
262
+ const done = new Set();
263
+ for (const e of status) {
264
+ if (e.revoked || done.has(e.kid) || activeByKid.has(e.kid)) continue;
265
+ registry.revoke(e.kid, at);
266
+ done.add(e.kid);
267
+ revoked.push({ kid: e.kid, revoked_at: at, reason: contested.has(e.kid) ? 'contested_kid' : 'absent_or_inactive' });
268
+ }
269
+
270
+ return { pinned, already_pinned: alreadyPinned, revoked, refused };
271
+ }
272
+
273
+ export default { ROSTER_VERSION, importRoster, diffRoster, applyRosterToRegistry };
@@ -0,0 +1,217 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ /**
4
+ * Runtime bridge for the Gate lifecycle.
5
+ *
6
+ * This is deliberately an explicit, reviewable monitor rather than a claim
7
+ * that TLA+ can be mechanically compiled into JavaScript. Its transition
8
+ * table mirrors the load-bearing lifecycle invariants in
9
+ * formal/ep_handshake.tla: an effect follows authorization, consumption is
10
+ * one-way, and execution evidence follows the effect attempt. A divergence
11
+ * enters fail-closed safe mode; recovery requires an operator-supplied
12
+ * authorizer and never re-authorizes an old receipt.
13
+ */
14
+
15
+ export const RUNTIME_MONITOR_VERSION = 'EP-GATE-RUNTIME-MONITOR-v1';
16
+ export const RUNTIME_MONITOR_MODES = Object.freeze({
17
+ NORMAL: 'normal',
18
+ DEGRADED: 'degraded',
19
+ LOCKDOWN: 'lockdown',
20
+ });
21
+
22
+ export const RUNTIME_INVARIANTS = Object.freeze({
23
+ CONSUME_ONCE: 'ConsumeOnceSafety',
24
+ WRITE_BYPASS: 'WriteBypassSafety',
25
+ SIGNOFF_BINDING: 'SignoffBindingMatch',
26
+ });
27
+
28
+ const TIER_RANK = Object.freeze({ software: 0, class_a: 1, quorum: 2 });
29
+ const SAFE_MODE_TIER = 'class_a';
30
+ const MAX_EVENTS = 256;
31
+ const MAX_CYCLES = 1024;
32
+
33
+ function isPromiseLike(value) {
34
+ return value !== null
35
+ && (typeof value === 'object' || typeof value === 'function')
36
+ && typeof value.then === 'function';
37
+ }
38
+
39
+ function publicContext(context: { cycle_id?: any; action?: any; receipt_id?: any; phase?: any } = {}) {
40
+ return {
41
+ cycle_id: context.cycle_id ?? null,
42
+ action: typeof context.action === 'string' ? context.action : null,
43
+ receipt_id: typeof context.receipt_id === 'string' ? context.receipt_id : null,
44
+ phase: context.phase ?? null,
45
+ };
46
+ }
47
+
48
+ function copyEvent(event) {
49
+ return Object.freeze({ ...event, context: Object.freeze({ ...event.context }) });
50
+ }
51
+
52
+ /**
53
+ * Create a process-local monitor. Operators should export divergence events to
54
+ * durable SIEM/evidence storage through onDivergence; the monitor itself keeps
55
+ * only a bounded diagnostic buffer.
56
+ *
57
+ * @param {{ now?: (() => number) | number, onDivergence?: ((event: object) => any) | null, authorizeRecovery?: ((input: object) => boolean) | null }} [options]
58
+ */
59
+ export function createRuntimeMonitor({
60
+ now = Date.now,
61
+ onDivergence = null,
62
+ authorizeRecovery = null,
63
+ }: {
64
+ now?: (() => number) | number;
65
+ onDivergence?: ((event: object) => any) | null;
66
+ authorizeRecovery?: ((input: object) => boolean) | null;
67
+ } = {}) {
68
+ let mode: typeof RUNTIME_MONITOR_MODES[keyof typeof RUNTIME_MONITOR_MODES] = RUNTIME_MONITOR_MODES.NORMAL;
69
+ let sequence = 0;
70
+ const cycles = new Map();
71
+ const events: any[] = [];
72
+
73
+ function timestamp() {
74
+ const value = typeof now === 'function' ? now() : now;
75
+ return new Date(Number(value)).toISOString();
76
+ }
77
+
78
+ function clockValue() {
79
+ return typeof now === 'function' ? now() : now;
80
+ }
81
+
82
+ function emitDivergence(cycle, theorem, expected, actual, severity = 'critical') {
83
+ const event = copyEvent({
84
+ version: RUNTIME_MONITOR_VERSION,
85
+ type: 'SPEC_DIVERGENCE',
86
+ at: timestamp(),
87
+ theorem,
88
+ expected,
89
+ actual,
90
+ severity,
91
+ context: publicContext({ ...cycle, phase: cycle?.phase }),
92
+ });
93
+ events.push(event);
94
+ if (events.length > MAX_EVENTS) events.shift();
95
+ mode = severity === 'critical' ? RUNTIME_MONITOR_MODES.LOCKDOWN : RUNTIME_MONITOR_MODES.DEGRADED;
96
+ if (typeof onDivergence === 'function') {
97
+ try {
98
+ const result = onDivergence(event);
99
+ if (isPromiseLike(result)) result.catch(() => {});
100
+ } catch { /* monitoring must not weaken the fail-closed transition */ }
101
+ }
102
+ return { ok: false, reason: `runtime_divergence:${theorem}`, event };
103
+ }
104
+
105
+ function fail(cycle, theorem, expected, actual, severity = 'critical') {
106
+ return emitDivergence(cycle, theorem, expected, actual, severity);
107
+ }
108
+
109
+ function transition(cycleId, event, details: {
110
+ allow?: boolean;
111
+ status?: number;
112
+ guarded?: boolean;
113
+ receipt_id?: string;
114
+ reason?: string;
115
+ } = {}) {
116
+ const cycle = cycles.get(cycleId);
117
+ if (!cycle) {
118
+ return fail({ cycle_id: cycleId, ...details }, RUNTIME_INVARIANTS.WRITE_BYPASS,
119
+ 'known runtime cycle', 'unknown runtime cycle');
120
+ }
121
+ if (event === 'decision') {
122
+ if (cycle.phase !== 'checking') {
123
+ return fail(cycle, RUNTIME_INVARIANTS.WRITE_BYPASS, 'checking → decision', `${cycle.phase} → decision`);
124
+ }
125
+ if (details.allow === true && details.status !== 200) {
126
+ return fail(cycle, RUNTIME_INVARIANTS.WRITE_BYPASS, 'allow requires HTTP 200', `allow with ${details.status}`);
127
+ }
128
+ if (details.allow === true && details.guarded === true && !details.receipt_id) {
129
+ return fail(cycle, RUNTIME_INVARIANTS.SIGNOFF_BINDING, 'guarded allow has receipt_id', 'guarded allow without receipt_id');
130
+ }
131
+ cycle.phase = details.allow === true ? 'authorized' : 'refused';
132
+ cycle.allow = details.allow === true;
133
+ cycle.reason = details.reason ?? null;
134
+ return { ok: true };
135
+ }
136
+ const expected = {
137
+ effect_attempted: ['authorized'],
138
+ effect_returned: ['effect_attempted'],
139
+ effect_failed: ['effect_attempted', 'effect_returned'],
140
+ capability_refused: ['authorized'],
141
+ consumed: ['effect_returned', 'effect_failed'],
142
+ execution_recorded: ['consumed'],
143
+ }[event];
144
+ if (!expected || !expected.includes(cycle.phase)) {
145
+ const theorem = event === 'consumed'
146
+ ? RUNTIME_INVARIANTS.CONSUME_ONCE
147
+ : RUNTIME_INVARIANTS.WRITE_BYPASS;
148
+ return fail(cycle, theorem, `${expected?.join(' or ') || 'known event'} → ${event}`, `${cycle.phase} → ${event}`);
149
+ }
150
+ if (event === 'consumed' && cycle.consumed === true) {
151
+ return fail(cycle, RUNTIME_INVARIANTS.CONSUME_ONCE, 'consumed at most once', 'second consumption transition');
152
+ }
153
+ cycle.phase = event;
154
+ if (event === 'consumed') cycle.consumed = true;
155
+ if (event === 'execution_recorded') cycle.complete = true;
156
+ return { ok: true };
157
+ }
158
+
159
+ return {
160
+ version: RUNTIME_MONITOR_VERSION,
161
+ beginCheck({ action = null, receipt_id = null } = {}) {
162
+ const cycle_id = `rt_${Number(clockValue()).toString(36)}_${(++sequence).toString(36)}`;
163
+ cycles.set(cycle_id, { cycle_id, action, receipt_id, phase: 'checking', consumed: false });
164
+ while (cycles.size > MAX_CYCLES) cycles.delete(cycles.keys().next().value);
165
+ return cycle_id;
166
+ },
167
+ preflight({ hasReceipt }: { hasReceipt?: boolean } = {}) {
168
+ if (mode === RUNTIME_MONITOR_MODES.NORMAL || hasReceipt === true) return { ok: true };
169
+ return { ok: false, reason: 'runtime_safe_mode_signoff_required', mode };
170
+ },
171
+ minimumAssuranceTier(declaredTier) {
172
+ if (mode === RUNTIME_MONITOR_MODES.NORMAL || declaredTier === undefined || declaredTier === null) return declaredTier;
173
+ return (TIER_RANK[declaredTier] ?? 0) < TIER_RANK[SAFE_MODE_TIER] ? SAFE_MODE_TIER : declaredTier;
174
+ },
175
+ recordDecision(cycleId, details) {
176
+ return transition(cycleId, 'decision', details);
177
+ },
178
+ beginExecution(cycleId, authorization) {
179
+ if (!authorization || authorization.allow !== true || isPromiseLike(authorization)) {
180
+ const cycle = cycles.get(cycleId) || { cycle_id: cycleId };
181
+ return fail(cycle, RUNTIME_INVARIANTS.WRITE_BYPASS, 'resolved allow authorization', 'missing or promise authorization');
182
+ }
183
+ return transition(cycleId, 'effect_attempted');
184
+ },
185
+ effectReturned(cycleId) { return transition(cycleId, 'effect_returned'); },
186
+ effectFailed(cycleId) { return transition(cycleId, 'effect_failed'); },
187
+ capabilityRefused(cycleId) { return transition(cycleId, 'capability_refused'); },
188
+ consumptionCommitted(cycleId) { return transition(cycleId, 'consumed'); },
189
+ executionRecorded(cycleId) { return transition(cycleId, 'execution_recorded'); },
190
+ executionSkipped(cycleId) {
191
+ const cycle = cycles.get(cycleId);
192
+ if (!cycle || cycle.phase !== 'consumed') {
193
+ return fail(cycle || { cycle_id: cycleId }, RUNTIME_INVARIANTS.WRITE_BYPASS,
194
+ 'consumed → execution skipped', `${cycle?.phase || 'unknown'} → execution skipped`);
195
+ }
196
+ cycle.phase = 'complete';
197
+ cycle.complete = true;
198
+ return { ok: true };
199
+ },
200
+ getMode() { return mode; },
201
+ getEvents() { return events.map(copyEvent); },
202
+ getState(cycleId) {
203
+ const cycle = cycles.get(cycleId);
204
+ return cycle ? Object.freeze({ ...cycle }) : null;
205
+ },
206
+ recover(input = {}) {
207
+ if (mode === RUNTIME_MONITOR_MODES.NORMAL) return { ok: true, mode };
208
+ if (typeof authorizeRecovery !== 'function' || authorizeRecovery(input) !== true) {
209
+ return { ok: false, reason: 'runtime_recovery_authorization_required', mode };
210
+ }
211
+ mode = RUNTIME_MONITOR_MODES.NORMAL;
212
+ return { ok: true, mode };
213
+ },
214
+ };
215
+ }
216
+
217
+ export default { createRuntimeMonitor, RUNTIME_MONITOR_VERSION, RUNTIME_MONITOR_MODES, RUNTIME_INVARIANTS };