@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.
- package/CHANGELOG.md +80 -0
- package/README.md +310 -0
- package/action-control-manifest.js +3 -268
- package/action-escrow-custodian.js +3 -394
- package/action-escrow-evidence.js +3 -858
- package/action-escrow-package.js +3 -1668
- package/action-escrow-postgres.js +3 -337
- package/action-escrow-state.js +3 -396
- package/action-escrow-verifiers.js +3 -331
- package/action-escrow.js +3 -2447
- package/action-packs.js +3 -140
- package/adapters/_kit.js +3 -104
- package/adapters/aws.js +3 -108
- package/adapters/cloudflare.js +3 -54
- package/adapters/gcp.js +3 -70
- package/adapters/github.js +3 -109
- package/adapters/jira.js +3 -55
- package/adapters/k8s.js +3 -70
- package/adapters/linear.js +3 -55
- package/adapters/salesforce.js +3 -55
- package/adapters/stripe.js +3 -81
- package/adapters/supabase.js +3 -121
- package/adapters/terraform.js +3 -59
- package/adapters/vercel.js +3 -84
- package/aec-execution.js +3 -312
- package/breakglass.js +3 -582
- package/capability-receipt.js +4 -0
- package/cf1-conformance.js +3 -106
- package/challenge-store.js +3 -69
- package/control-plane.js +3 -340
- package/coverage.js +3 -721
- package/demo.mjs +1 -1
- package/deployment-attestation.js +3 -247
- package/dist/action-control-manifest.d.ts +82 -0
- package/dist/action-control-manifest.d.ts.map +1 -0
- package/dist/action-control-manifest.js +422 -0
- package/dist/action-control-manifest.js.map +1 -0
- package/dist/action-escrow-custodian.d.ts +74 -0
- package/dist/action-escrow-custodian.d.ts.map +1 -0
- package/dist/action-escrow-custodian.js +363 -0
- package/dist/action-escrow-custodian.js.map +1 -0
- package/dist/action-escrow-evidence.d.ts +130 -0
- package/dist/action-escrow-evidence.d.ts.map +1 -0
- package/dist/action-escrow-evidence.js +1012 -0
- package/dist/action-escrow-evidence.js.map +1 -0
- package/dist/action-escrow-package.d.ts +10 -0
- package/dist/action-escrow-package.d.ts.map +1 -0
- package/dist/action-escrow-package.js +1583 -0
- package/dist/action-escrow-package.js.map +1 -0
- package/dist/action-escrow-postgres.d.ts +62 -0
- package/dist/action-escrow-postgres.d.ts.map +1 -0
- package/dist/action-escrow-postgres.js +339 -0
- package/dist/action-escrow-postgres.js.map +1 -0
- package/dist/action-escrow-state.d.ts +141 -0
- package/dist/action-escrow-state.d.ts.map +1 -0
- package/dist/action-escrow-state.js +413 -0
- package/dist/action-escrow-state.js.map +1 -0
- package/dist/action-escrow-verifiers.d.ts +65 -0
- package/dist/action-escrow-verifiers.d.ts.map +1 -0
- package/dist/action-escrow-verifiers.js +421 -0
- package/dist/action-escrow-verifiers.js.map +1 -0
- package/dist/action-escrow.d.ts +112 -0
- package/dist/action-escrow.d.ts.map +1 -0
- package/dist/action-escrow.js +2661 -0
- package/dist/action-escrow.js.map +1 -0
- package/dist/action-packs.d.ts +54 -0
- package/dist/action-packs.d.ts.map +1 -0
- package/dist/action-packs.js +143 -0
- package/dist/action-packs.js.map +1 -0
- package/dist/adapters/_kit.d.ts +50 -0
- package/dist/adapters/_kit.d.ts.map +1 -0
- package/dist/adapters/_kit.js +98 -0
- package/dist/adapters/_kit.js.map +1 -0
- package/dist/adapters/aws.d.ts +158 -0
- package/dist/adapters/aws.d.ts.map +1 -0
- package/dist/adapters/aws.js +136 -0
- package/dist/adapters/aws.js.map +1 -0
- package/dist/adapters/cloudflare.d.ts +116 -0
- package/dist/adapters/cloudflare.d.ts.map +1 -0
- package/dist/adapters/cloudflare.js +75 -0
- package/dist/adapters/cloudflare.js.map +1 -0
- package/dist/adapters/gcp.d.ts +139 -0
- package/dist/adapters/gcp.d.ts.map +1 -0
- package/dist/adapters/gcp.js +69 -0
- package/dist/adapters/gcp.js.map +1 -0
- package/dist/adapters/github.d.ts +123 -0
- package/dist/adapters/github.d.ts.map +1 -0
- package/dist/adapters/github.js +105 -0
- package/dist/adapters/github.js.map +1 -0
- package/dist/adapters/jira.d.ts +109 -0
- package/dist/adapters/jira.d.ts.map +1 -0
- package/dist/adapters/jira.js +54 -0
- package/dist/adapters/jira.js.map +1 -0
- package/dist/adapters/k8s.d.ts +139 -0
- package/dist/adapters/k8s.d.ts.map +1 -0
- package/dist/adapters/k8s.js +69 -0
- package/dist/adapters/k8s.js.map +1 -0
- package/dist/adapters/linear.d.ts +109 -0
- package/dist/adapters/linear.d.ts.map +1 -0
- package/dist/adapters/linear.js +54 -0
- package/dist/adapters/linear.js.map +1 -0
- package/dist/adapters/salesforce.d.ts +109 -0
- package/dist/adapters/salesforce.d.ts.map +1 -0
- package/dist/adapters/salesforce.js +54 -0
- package/dist/adapters/salesforce.js.map +1 -0
- package/dist/adapters/stripe.d.ts +116 -0
- package/dist/adapters/stripe.d.ts.map +1 -0
- package/dist/adapters/stripe.js +77 -0
- package/dist/adapters/stripe.js.map +1 -0
- package/dist/adapters/supabase.d.ts +127 -0
- package/dist/adapters/supabase.d.ts.map +1 -0
- package/dist/adapters/supabase.js +112 -0
- package/dist/adapters/supabase.js.map +1 -0
- package/dist/adapters/terraform.d.ts +109 -0
- package/dist/adapters/terraform.d.ts.map +1 -0
- package/dist/adapters/terraform.js +58 -0
- package/dist/adapters/terraform.js.map +1 -0
- package/dist/adapters/vercel.d.ts +114 -0
- package/dist/adapters/vercel.d.ts.map +1 -0
- package/dist/adapters/vercel.js +81 -0
- package/dist/adapters/vercel.js.map +1 -0
- package/dist/aec-execution.d.ts +107 -0
- package/dist/aec-execution.d.ts.map +1 -0
- package/dist/aec-execution.js +334 -0
- package/dist/aec-execution.js.map +1 -0
- package/dist/breakglass.d.ts +205 -0
- package/dist/breakglass.d.ts.map +1 -0
- package/dist/breakglass.js +627 -0
- package/dist/breakglass.js.map +1 -0
- package/dist/capability-receipt.d.ts +601 -0
- package/dist/capability-receipt.d.ts.map +1 -0
- package/dist/capability-receipt.js +1177 -0
- package/dist/capability-receipt.js.map +1 -0
- package/dist/cf1-conformance.d.ts +83 -0
- package/dist/cf1-conformance.d.ts.map +1 -0
- package/dist/cf1-conformance.js +94 -0
- package/dist/cf1-conformance.js.map +1 -0
- package/dist/challenge-store.d.ts +20 -0
- package/dist/challenge-store.d.ts.map +1 -0
- package/dist/challenge-store.js +62 -0
- package/dist/challenge-store.js.map +1 -0
- package/dist/control-plane.d.ts +136 -0
- package/dist/control-plane.d.ts.map +1 -0
- package/dist/control-plane.js +349 -0
- package/dist/control-plane.js.map +1 -0
- package/dist/coverage.d.ts +156 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +768 -0
- package/dist/coverage.js.map +1 -0
- package/dist/deployment-attestation.d.ts +93 -0
- package/dist/deployment-attestation.d.ts.map +1 -0
- package/dist/deployment-attestation.js +262 -0
- package/dist/deployment-attestation.js.map +1 -0
- package/dist/eg1-conformance.d.ts +286 -0
- package/dist/eg1-conformance.d.ts.map +1 -0
- package/dist/eg1-conformance.js +379 -0
- package/dist/eg1-conformance.js.map +1 -0
- package/dist/enterprise.d.ts +88 -0
- package/dist/enterprise.d.ts.map +1 -0
- package/dist/enterprise.js +194 -0
- package/dist/enterprise.js.map +1 -0
- package/dist/evidence-postgres.d.ts +123 -0
- package/dist/evidence-postgres.d.ts.map +1 -0
- package/dist/evidence-postgres.js +343 -0
- package/dist/evidence-postgres.js.map +1 -0
- package/dist/evidence.d.ts +113 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +369 -0
- package/dist/evidence.js.map +1 -0
- package/dist/execution-binding.d.ts +49 -0
- package/dist/execution-binding.d.ts.map +1 -0
- package/dist/execution-binding.js +243 -0
- package/dist/execution-binding.js.map +1 -0
- package/dist/formal-runtime-map.d.ts +42 -0
- package/dist/formal-runtime-map.d.ts.map +1 -0
- package/dist/formal-runtime-map.js +23 -0
- package/dist/formal-runtime-map.js.map +1 -0
- package/dist/index.d.ts +1003 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1737 -0
- package/dist/index.js.map +1 -0
- package/dist/key-registry.d.ts +60 -0
- package/dist/key-registry.d.ts.map +1 -0
- package/dist/key-registry.js +144 -0
- package/dist/key-registry.js.map +1 -0
- package/dist/mcp.d.ts +53 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +112 -0
- package/dist/mcp.js.map +1 -0
- package/dist/metering.d.ts +80 -0
- package/dist/metering.d.ts.map +1 -0
- package/dist/metering.js +227 -0
- package/dist/metering.js.map +1 -0
- package/dist/metrics.d.ts +68 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +242 -0
- package/dist/metrics.js.map +1 -0
- package/dist/network-witness.d.ts +167 -0
- package/dist/network-witness.d.ts.map +1 -0
- package/dist/network-witness.js +520 -0
- package/dist/network-witness.js.map +1 -0
- package/dist/proposal-to-effect.d.ts +146 -0
- package/dist/proposal-to-effect.d.ts.map +1 -0
- package/dist/proposal-to-effect.js +427 -0
- package/dist/proposal-to-effect.js.map +1 -0
- package/dist/protocol-state.d.ts +141 -0
- package/dist/protocol-state.d.ts.map +1 -0
- package/dist/protocol-state.js +106 -0
- package/dist/protocol-state.js.map +1 -0
- package/dist/receipt-program.d.ts +59 -0
- package/dist/receipt-program.d.ts.map +1 -0
- package/dist/receipt-program.js +1053 -0
- package/dist/receipt-program.js.map +1 -0
- package/dist/reliance-kernel.d.ts +66 -0
- package/dist/reliance-kernel.d.ts.map +1 -0
- package/dist/reliance-kernel.js +103 -0
- package/dist/reliance-kernel.js.map +1 -0
- package/dist/reliance-packet.d.ts +67 -0
- package/dist/reliance-packet.d.ts.map +1 -0
- package/dist/reliance-packet.js +176 -0
- package/dist/reliance-packet.js.map +1 -0
- package/dist/remedy-program-postgres.d.ts +37 -0
- package/dist/remedy-program-postgres.d.ts.map +1 -0
- package/dist/remedy-program-postgres.js +291 -0
- package/dist/remedy-program-postgres.js.map +1 -0
- package/dist/remedy-program-receipt.d.ts +97 -0
- package/dist/remedy-program-receipt.d.ts.map +1 -0
- package/dist/remedy-program-receipt.js +817 -0
- package/dist/remedy-program-receipt.js.map +1 -0
- package/dist/remedy-program.d.ts +76 -0
- package/dist/remedy-program.d.ts.map +1 -0
- package/dist/remedy-program.js +1346 -0
- package/dist/remedy-program.js.map +1 -0
- package/dist/reports/art14.d.ts +101 -0
- package/dist/reports/art14.d.ts.map +1 -0
- package/dist/reports/art14.js +340 -0
- package/dist/reports/art14.js.map +1 -0
- package/dist/reports/assurance-package.d.ts +197 -0
- package/dist/reports/assurance-package.d.ts.map +1 -0
- package/dist/reports/assurance-package.js +313 -0
- package/dist/reports/assurance-package.js.map +1 -0
- package/dist/reports/auditor-workpaper.d.ts +194 -0
- package/dist/reports/auditor-workpaper.d.ts.map +1 -0
- package/dist/reports/auditor-workpaper.js +545 -0
- package/dist/reports/auditor-workpaper.js.map +1 -0
- package/dist/reports/external-verification.d.ts +76 -0
- package/dist/reports/external-verification.d.ts.map +1 -0
- package/dist/reports/external-verification.js +209 -0
- package/dist/reports/external-verification.js.map +1 -0
- package/dist/reports/reperform.d.ts +122 -0
- package/dist/reports/reperform.d.ts.map +1 -0
- package/dist/reports/reperform.js +432 -0
- package/dist/reports/reperform.js.map +1 -0
- package/dist/reports/underwriter.d.ts +127 -0
- package/dist/reports/underwriter.d.ts.map +1 -0
- package/dist/reports/underwriter.js +348 -0
- package/dist/reports/underwriter.js.map +1 -0
- package/dist/retention.d.ts +88 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +90 -0
- package/dist/retention.js.map +1 -0
- package/dist/roster.d.ts +113 -0
- package/dist/roster.d.ts.map +1 -0
- package/dist/roster.js +261 -0
- package/dist/roster.js.map +1 -0
- package/dist/runtime-monitor.d.ts +132 -0
- package/dist/runtime-monitor.d.ts.map +1 -0
- package/dist/runtime-monitor.js +197 -0
- package/dist/runtime-monitor.js.map +1 -0
- package/dist/settlement.d.ts +68 -0
- package/dist/settlement.d.ts.map +1 -0
- package/dist/settlement.js +306 -0
- package/dist/settlement.js.map +1 -0
- package/dist/siem.d.ts +261 -0
- package/dist/siem.d.ts.map +1 -0
- package/dist/siem.js +247 -0
- package/dist/siem.js.map +1 -0
- package/dist/store-postgres.d.ts +107 -0
- package/dist/store-postgres.d.ts.map +1 -0
- package/dist/store-postgres.js +152 -0
- package/dist/store-postgres.js.map +1 -0
- package/dist/store.d.ts +95 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +202 -0
- package/dist/store.js.map +1 -0
- package/dist/strict-json.d.ts +14 -0
- package/dist/strict-json.d.ts.map +1 -0
- package/dist/strict-json.js +132 -0
- package/dist/strict-json.js.map +1 -0
- package/dist/trust-program-adapters.d.ts +122 -0
- package/dist/trust-program-adapters.d.ts.map +1 -0
- package/dist/trust-program-adapters.js +802 -0
- package/dist/trust-program-adapters.js.map +1 -0
- package/dist/trust-program-postgres.d.ts +41 -0
- package/dist/trust-program-postgres.d.ts.map +1 -0
- package/dist/trust-program-postgres.js +346 -0
- package/dist/trust-program-postgres.js.map +1 -0
- package/dist/trust-program-revocation.d.ts +206 -0
- package/dist/trust-program-revocation.d.ts.map +1 -0
- package/dist/trust-program-revocation.js +600 -0
- package/dist/trust-program-revocation.js.map +1 -0
- package/dist/trust-program.d.ts +197 -0
- package/dist/trust-program.d.ts.map +1 -0
- package/dist/trust-program.js +1351 -0
- package/dist/trust-program.js.map +1 -0
- package/dist/witness-postgres.d.ts +43 -0
- package/dist/witness-postgres.d.ts.map +1 -0
- package/dist/witness-postgres.js +92 -0
- package/dist/witness-postgres.js.map +1 -0
- package/dist/zk-range-proof.d.ts +78 -0
- package/dist/zk-range-proof.d.ts.map +1 -0
- package/dist/zk-range-proof.js +228 -0
- package/dist/zk-range-proof.js.map +1 -0
- package/eg1-conformance.js +3 -404
- package/enterprise.js +3 -180
- package/ep-assure.mjs +1 -0
- package/evidence-postgres.js +3 -356
- package/evidence.js +4 -360
- package/execution-binding.js +3 -212
- package/formal-runtime-map.js +4 -0
- package/index.js +4 -1249
- package/key-registry.js +3 -134
- package/mcp.js +3 -109
- package/metering.js +3 -226
- package/metrics.js +3 -231
- package/network-witness.js +3 -499
- package/package.json +35 -4
- package/proposal-to-effect.js +4 -0
- package/receipt-program.js +4 -0
- package/reliance-kernel.js +3 -112
- package/reliance-packet.js +3 -175
- package/remedy-program-postgres.js +4 -0
- package/remedy-program-receipt.js +4 -0
- package/remedy-program.js +4 -0
- package/reports/art14.js +0 -0
- package/reports/assurance-package.js +2 -312
- package/reports/auditor-workpaper.js +3 -537
- package/reports/external-verification.js +3 -222
- package/reports/reperform.js +3 -424
- package/reports/underwriter.js +3 -339
- package/retention.js +3 -84
- package/roster.js +3 -264
- package/runtime-monitor.js +4 -0
- package/settlement.js +3 -299
- package/siem.js +3 -235
- package/src/action-control-manifest.ts +424 -0
- package/src/action-escrow-custodian.ts +427 -0
- package/src/action-escrow-evidence.ts +1178 -0
- package/src/action-escrow-package.ts +1821 -0
- package/src/action-escrow-postgres.ts +366 -0
- package/src/action-escrow-state.ts +498 -0
- package/src/action-escrow-verifiers.ts +484 -0
- package/src/action-escrow.ts +3039 -0
- package/src/action-packs.ts +170 -0
- package/src/adapters/_kit.ts +117 -0
- package/src/adapters/aws.ts +142 -0
- package/src/adapters/cloudflare.ts +78 -0
- package/src/adapters/gcp.ts +71 -0
- package/src/adapters/github.ts +110 -0
- package/src/adapters/jira.ts +56 -0
- package/src/adapters/k8s.ts +71 -0
- package/src/adapters/linear.ts +56 -0
- package/src/adapters/salesforce.ts +56 -0
- package/src/adapters/stripe.ts +82 -0
- package/src/adapters/supabase.ts +122 -0
- package/src/adapters/terraform.ts +60 -0
- package/src/adapters/vercel.ts +85 -0
- package/src/aec-execution.ts +354 -0
- package/src/breakglass.ts +647 -0
- package/src/capability-receipt.ts +1258 -0
- package/src/cf1-conformance.ts +120 -0
- package/src/challenge-store.ts +70 -0
- package/src/control-plane.ts +374 -0
- package/src/coverage.ts +832 -0
- package/src/deployment-attestation.ts +259 -0
- package/src/eg1-conformance.ts +471 -0
- package/src/enterprise.ts +195 -0
- package/src/evidence-postgres.ts +374 -0
- package/src/evidence.ts +367 -0
- package/src/execution-binding.ts +236 -0
- package/src/formal-runtime-map.ts +24 -0
- package/src/index.ts +2127 -0
- package/src/key-registry.ts +150 -0
- package/src/mcp.ts +120 -0
- package/src/metering.ts +237 -0
- package/src/metrics.ts +232 -0
- package/src/network-witness.ts +585 -0
- package/src/proposal-to-effect.ts +571 -0
- package/src/protocol-state.ts +143 -0
- package/src/protocol-state.typecheck.ts +94 -0
- package/src/receipt-program.ts +1105 -0
- package/src/reliance-kernel.ts +119 -0
- package/src/reliance-packet.ts +206 -0
- package/src/remedy-program-postgres.ts +367 -0
- package/src/remedy-program-receipt.ts +891 -0
- package/src/remedy-program.ts +1388 -0
- package/src/reports/art14.ts +361 -0
- package/src/reports/assurance-package.ts +330 -0
- package/src/reports/auditor-workpaper.ts +558 -0
- package/src/reports/external-verification.ts +232 -0
- package/src/reports/reperform.ts +441 -0
- package/src/reports/underwriter.ts +356 -0
- package/src/retention.ts +100 -0
- package/src/roster.ts +273 -0
- package/src/runtime-monitor.ts +217 -0
- package/src/settlement.ts +313 -0
- package/src/siem.ts +247 -0
- package/src/store-postgres.ts +167 -0
- package/src/store.ts +214 -0
- package/src/strict-json.ts +115 -0
- package/src/trust-program-adapters.ts +847 -0
- package/src/trust-program-postgres.ts +429 -0
- package/src/trust-program-revocation.ts +751 -0
- package/src/trust-program.ts +1460 -0
- package/src/witness-postgres.ts +106 -0
- package/src/zk-range-proof.ts +250 -0
- package/store-postgres.js +3 -161
- package/store.js +4 -198
- package/strict-json.js +3 -102
- package/trust-program-adapters.js +4 -0
- package/trust-program-postgres.js +4 -0
- package/trust-program-revocation.js +4 -0
- package/trust-program.js +4 -0
- package/witness-postgres.js +3 -96
- 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 };
|