@proofoftech/flowsafe 0.11.0 → 0.12.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 +36 -0
- package/README.md +65 -43
- package/deployment-identity-protocol.d.mts +52 -0
- package/deployment-identity-protocol.mjs +231 -0
- package/dist/agent-host/approval-resumer.d.ts +3 -3
- package/dist/agent-host/approval-resumer.d.ts.map +1 -1
- package/dist/agent-host/approval-resumer.js +6 -7
- package/dist/agent-host/approval-resumer.js.map +1 -1
- package/dist/agent-host/catalog.js +2 -2
- package/dist/agent-host/catalog.js.map +1 -1
- package/dist/agent-host/index.d.ts +2 -2
- package/dist/agent-host/index.d.ts.map +1 -1
- package/dist/agent-host/index.js.map +1 -1
- package/dist/agent-host/router.d.ts +2 -2
- package/dist/agent-host/router.d.ts.map +1 -1
- package/dist/agent-host/router.js +22 -54
- package/dist/agent-host/router.js.map +1 -1
- package/dist/agent-host/thread-host.d.ts +42 -8
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +533 -106
- package/dist/agent-host/thread-host.js.map +1 -1
- package/dist/agent-host/thread-topology.d.ts +28 -7
- package/dist/agent-host/thread-topology.d.ts.map +1 -1
- package/dist/agent-host/thread-topology.js +97 -39
- package/dist/agent-host/thread-topology.js.map +1 -1
- package/dist/agent-host/trusted-context.d.ts.map +1 -1
- package/dist/agent-host/trusted-context.js +3 -1
- package/dist/agent-host/trusted-context.js.map +1 -1
- package/dist/agent-host/types.d.ts +2 -0
- package/dist/agent-host/types.d.ts.map +1 -1
- package/dist/agent-runner/agent-run-state.d.ts +9 -8
- package/dist/agent-runner/agent-run-state.d.ts.map +1 -1
- package/dist/agent-runner/agent-run-state.js +78 -40
- package/dist/agent-runner/agent-run-state.js.map +1 -1
- package/dist/agent-runner/durable-agent-runner.d.ts +18 -16
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +120 -43
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/agent-runner/index.d.ts +1 -1
- package/dist/agent-runner/index.d.ts.map +1 -1
- package/dist/agent-runner/index.js +1 -1
- package/dist/agent-runner/index.js.map +1 -1
- package/dist/approval-api/actor-context.d.ts +67 -0
- package/dist/approval-api/actor-context.d.ts.map +1 -0
- package/dist/approval-api/actor-context.js +120 -0
- package/dist/approval-api/actor-context.js.map +1 -0
- package/dist/approval-api/contract.d.ts +12 -23
- package/dist/approval-api/contract.d.ts.map +1 -1
- package/dist/approval-api/d1-store.d.ts +7 -15
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +83 -159
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/grants.d.ts +3 -17
- package/dist/approval-api/grants.d.ts.map +1 -1
- package/dist/approval-api/grants.js +19 -39
- package/dist/approval-api/grants.js.map +1 -1
- package/dist/approval-api/index.d.ts +8 -8
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js +5 -5
- package/dist/approval-api/index.js.map +1 -1
- package/dist/approval-api/principal.d.ts +29 -30
- package/dist/approval-api/principal.d.ts.map +1 -1
- package/dist/approval-api/principal.js +73 -54
- package/dist/approval-api/principal.js.map +1 -1
- package/dist/approval-api/resource-ownership.d.ts +64 -0
- package/dist/approval-api/resource-ownership.d.ts.map +1 -0
- package/dist/approval-api/resource-ownership.js +329 -0
- package/dist/approval-api/resource-ownership.js.map +1 -0
- package/dist/approval-api/retention.d.ts +6 -11
- package/dist/approval-api/retention.d.ts.map +1 -1
- package/dist/approval-api/retention.js +7 -12
- package/dist/approval-api/retention.js.map +1 -1
- package/dist/approval-api/router.d.ts +9 -8
- package/dist/approval-api/router.d.ts.map +1 -1
- package/dist/approval-api/router.js +50 -18
- package/dist/approval-api/router.js.map +1 -1
- package/dist/approval-api/service.d.ts +26 -36
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +104 -166
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/store-factory.d.ts +19 -0
- package/dist/approval-api/store-factory.d.ts.map +1 -0
- package/dist/approval-api/store-factory.js +49 -0
- package/dist/approval-api/store-factory.js.map +1 -0
- package/dist/approval-api/store.d.ts +22 -36
- package/dist/approval-api/store.d.ts.map +1 -1
- package/dist/approval-api/store.js +48 -66
- package/dist/approval-api/store.js.map +1 -1
- package/dist/approval-api/types.d.ts +28 -22
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js +51 -3
- package/dist/approval-api/types.js.map +1 -1
- package/dist/approval-ui/client.js +3 -3
- package/dist/approval-ui/client.js.map +1 -1
- package/dist/approval-ui/stream.d.ts +4 -4
- package/dist/approval-ui/stream.d.ts.map +1 -1
- package/dist/approval-ui/stream.js +2 -2
- package/dist/approval-ui/tips.d.ts +1 -1
- package/dist/approval-ui/tips.js +1 -1
- package/dist/approval-ui/tips.js.map +1 -1
- package/dist/approval-ui/use-approval-dashboard.d.ts +3 -3
- package/dist/approval-ui/use-approval-dashboard.js +1 -1
- package/dist/artifacts/index.d.ts +2 -3
- package/dist/artifacts/index.d.ts.map +1 -1
- package/dist/artifacts/index.js +9 -8
- package/dist/artifacts/index.js.map +1 -1
- package/dist/audit-export/audit-proxy.d.ts +71 -0
- package/dist/audit-export/audit-proxy.d.ts.map +1 -0
- package/dist/audit-export/audit-proxy.js +142 -0
- package/dist/audit-export/audit-proxy.js.map +1 -0
- package/dist/audit-export/index.d.ts +4 -7
- package/dist/audit-export/index.d.ts.map +1 -1
- package/dist/audit-export/index.js +1 -4
- package/dist/audit-export/index.js.map +1 -1
- package/dist/audit-export/queue-types.d.ts +5 -0
- package/dist/audit-export/queue-types.d.ts.map +1 -0
- package/dist/audit-export/queue-types.js +3 -0
- package/dist/audit-export/queue-types.js.map +1 -0
- package/dist/background-tasks/d1-storage.d.ts +9 -19
- package/dist/background-tasks/d1-storage.d.ts.map +1 -1
- package/dist/background-tasks/d1-storage.js +22 -52
- package/dist/background-tasks/d1-storage.js.map +1 -1
- package/dist/background-tasks/host.d.ts +4 -6
- package/dist/background-tasks/host.d.ts.map +1 -1
- package/dist/background-tasks/host.js +14 -17
- package/dist/background-tasks/host.js.map +1 -1
- package/dist/background-tasks/index.d.ts +1 -1
- package/dist/background-tasks/index.d.ts.map +1 -1
- package/dist/background-tasks/index.js +3 -3
- package/dist/background-tasks/index.js.map +1 -1
- package/dist/background-tasks/routes.d.ts +8 -4
- package/dist/background-tasks/routes.d.ts.map +1 -1
- package/dist/background-tasks/routes.js +33 -31
- package/dist/background-tasks/routes.js.map +1 -1
- package/dist/deployment-identity-protocol.d.ts +3 -0
- package/dist/deployment-identity-protocol.d.ts.map +1 -0
- package/dist/deployment-identity-protocol.js +3 -0
- package/dist/deployment-identity-protocol.js.map +1 -0
- package/dist/do-runner/breakwater-keys.d.ts +7 -5
- package/dist/do-runner/breakwater-keys.d.ts.map +1 -1
- package/dist/do-runner/breakwater-keys.js +7 -5
- package/dist/do-runner/breakwater-keys.js.map +1 -1
- package/dist/do-runner/cf-types.d.ts +18 -12
- package/dist/do-runner/cf-types.d.ts.map +1 -1
- package/dist/do-runner/cf-types.js.map +1 -1
- package/dist/do-runner/d1-storage.d.ts +44 -182
- package/dist/do-runner/d1-storage.d.ts.map +1 -1
- package/dist/do-runner/d1-storage.js +102 -353
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/deployment-identity.d.ts +121 -0
- package/dist/do-runner/deployment-identity.d.ts.map +1 -0
- package/dist/do-runner/deployment-identity.js +272 -0
- package/dist/do-runner/deployment-identity.js.map +1 -0
- package/dist/do-runner/do-error-response.d.ts +1 -1
- package/dist/do-runner/do-error-response.d.ts.map +1 -1
- package/dist/do-runner/do-error-response.js +7 -1
- package/dist/do-runner/do-error-response.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +23 -16
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +303 -70
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/execution-context.d.ts +1 -0
- package/dist/do-runner/execution-context.d.ts.map +1 -1
- package/dist/do-runner/execution-context.js +4 -7
- package/dist/do-runner/execution-context.js.map +1 -1
- package/dist/do-runner/execution-principal-header.d.ts +16 -0
- package/dist/do-runner/execution-principal-header.d.ts.map +1 -0
- package/dist/do-runner/execution-principal-header.js +20 -0
- package/dist/do-runner/execution-principal-header.js.map +1 -0
- package/dist/do-runner/hub-do.d.ts +17 -25
- package/dist/do-runner/hub-do.d.ts.map +1 -1
- package/dist/do-runner/hub-do.js +63 -59
- package/dist/do-runner/hub-do.js.map +1 -1
- package/dist/do-runner/index.d.ts +12 -10
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +10 -9
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/init.d.ts +0 -7
- package/dist/do-runner/init.d.ts.map +1 -1
- package/dist/do-runner/init.js +1 -2
- package/dist/do-runner/init.js.map +1 -1
- package/dist/do-runner/memory-id.d.ts +8 -25
- package/dist/do-runner/memory-id.d.ts.map +1 -1
- package/dist/do-runner/memory-id.js +25 -52
- package/dist/do-runner/memory-id.js.map +1 -1
- package/dist/do-runner/path-safe-id.d.ts +2 -48
- package/dist/do-runner/path-safe-id.d.ts.map +1 -1
- package/dist/do-runner/path-safe-id.js +3 -93
- package/dist/do-runner/path-safe-id.js.map +1 -1
- package/dist/do-runner/pubsub.d.ts +1 -1
- package/dist/do-runner/pubsub.js +1 -1
- package/dist/do-runner/runtime.d.ts +47 -34
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +281 -143
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/schedule-source.d.ts +81 -0
- package/dist/do-runner/schedule-source.d.ts.map +1 -0
- package/dist/do-runner/schedule-source.js +64 -0
- package/dist/do-runner/schedule-source.js.map +1 -0
- package/dist/do-runner/thread-do.d.ts +18 -32
- package/dist/do-runner/thread-do.d.ts.map +1 -1
- package/dist/do-runner/thread-do.js +52 -74
- package/dist/do-runner/thread-do.js.map +1 -1
- package/dist/goals/objective-routes.d.ts +7 -4
- package/dist/goals/objective-routes.d.ts.map +1 -1
- package/dist/goals/objective-routes.js +58 -43
- package/dist/goals/objective-routes.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +18 -21
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +37 -37
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/bearer-auth.d.ts +3 -4
- package/dist/host-kit/bearer-auth.d.ts.map +1 -1
- package/dist/host-kit/bearer-auth.js +7 -11
- package/dist/host-kit/bearer-auth.js.map +1 -1
- package/dist/host-kit/do-run-topology.d.ts +14 -5
- package/dist/host-kit/do-run-topology.d.ts.map +1 -1
- package/dist/host-kit/do-run-topology.js +47 -15
- package/dist/host-kit/do-run-topology.js.map +1 -1
- package/dist/host-kit/env-vars.d.ts +1 -1
- package/dist/host-kit/env-vars.js +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +149 -73
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +619 -141
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/host-approval-service.d.ts +62 -54
- package/dist/host-kit/host-approval-service.d.ts.map +1 -1
- package/dist/host-kit/host-approval-service.js +90 -61
- package/dist/host-kit/host-approval-service.js.map +1 -1
- package/dist/host-kit/hub-topology.d.ts +6 -7
- package/dist/host-kit/hub-topology.d.ts.map +1 -1
- package/dist/host-kit/hub-topology.js +16 -12
- package/dist/host-kit/hub-topology.js.map +1 -1
- package/dist/host-kit/index.d.ts +13 -11
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +9 -12
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/maintenance-capability.d.ts +58 -0
- package/dist/host-kit/maintenance-capability.d.ts.map +1 -0
- package/dist/host-kit/maintenance-capability.js +268 -0
- package/dist/host-kit/maintenance-capability.js.map +1 -0
- package/dist/host-kit/memory-boundary.d.ts +4 -19
- package/dist/host-kit/memory-boundary.d.ts.map +1 -1
- package/dist/host-kit/memory-boundary.js +12 -27
- package/dist/host-kit/memory-boundary.js.map +1 -1
- package/dist/host-kit/resource-access.d.ts +4 -0
- package/dist/host-kit/resource-access.d.ts.map +1 -0
- package/dist/host-kit/resource-access.js +12 -0
- package/dist/host-kit/resource-access.js.map +1 -0
- package/dist/host-kit/run-router.d.ts +25 -15
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +61 -46
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/host-kit/state-egress.d.ts +51 -0
- package/dist/host-kit/state-egress.d.ts.map +1 -0
- package/dist/host-kit/state-egress.js +120 -0
- package/dist/host-kit/state-egress.js.map +1 -0
- package/dist/host-kit/stream-router.d.ts +9 -4
- package/dist/host-kit/stream-router.d.ts.map +1 -1
- package/dist/host-kit/stream-router.js +45 -38
- package/dist/host-kit/stream-router.js.map +1 -1
- package/dist/host-kit/stream-ticket.d.ts +10 -10
- package/dist/host-kit/stream-ticket.d.ts.map +1 -1
- package/dist/host-kit/stream-ticket.js +60 -90
- package/dist/host-kit/stream-ticket.js.map +1 -1
- package/dist/host-kit/thread-topology.d.ts +17 -11
- package/dist/host-kit/thread-topology.d.ts.map +1 -1
- package/dist/host-kit/thread-topology.js +33 -59
- package/dist/host-kit/thread-topology.js.map +1 -1
- package/dist/host-kit/verifier.d.ts +6 -6
- package/dist/host-kit/verifier.d.ts.map +1 -1
- package/dist/host-kit/verifier.js +51 -119
- package/dist/host-kit/verifier.js.map +1 -1
- package/dist/http-body.d.ts +20 -0
- package/dist/http-body.d.ts.map +1 -0
- package/dist/http-body.js +67 -0
- package/dist/http-body.js.map +1 -0
- package/dist/schedules/index.d.ts +3 -2
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +5 -4
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/router.d.ts +16 -10
- package/dist/schedules/router.d.ts.map +1 -1
- package/dist/schedules/router.js +153 -91
- package/dist/schedules/router.js.map +1 -1
- package/dist/schedules/schedules-d1.d.ts +88 -0
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +411 -50
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/target-policy.d.ts +29 -0
- package/dist/schedules/target-policy.d.ts.map +1 -0
- package/dist/schedules/target-policy.js +70 -0
- package/dist/schedules/target-policy.js.map +1 -0
- package/dist/schedules/tick.d.ts +125 -21
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +637 -160
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signal-providers/delivery.d.ts +0 -19
- package/dist/signal-providers/delivery.d.ts.map +1 -1
- package/dist/signal-providers/delivery.js +5 -54
- package/dist/signal-providers/delivery.js.map +1 -1
- package/dist/signal-providers/github-provider.d.ts.map +1 -1
- package/dist/signal-providers/github-provider.js +9 -16
- package/dist/signal-providers/github-provider.js.map +1 -1
- package/dist/signal-providers/host-do.d.ts +3 -45
- package/dist/signal-providers/host-do.d.ts.map +1 -1
- package/dist/signal-providers/host-do.js +86 -106
- package/dist/signal-providers/host-do.js.map +1 -1
- package/dist/signal-providers/host-topology.d.ts +5 -14
- package/dist/signal-providers/host-topology.d.ts.map +1 -1
- package/dist/signal-providers/host-topology.js +11 -25
- package/dist/signal-providers/host-topology.js.map +1 -1
- package/dist/signal-providers/index.d.ts +3 -3
- package/dist/signal-providers/index.d.ts.map +1 -1
- package/dist/signal-providers/index.js +3 -3
- package/dist/signal-providers/index.js.map +1 -1
- package/dist/signal-providers/provider.d.ts +4 -5
- package/dist/signal-providers/provider.d.ts.map +1 -1
- package/dist/signal-providers/provider.js +5 -6
- package/dist/signal-providers/provider.js.map +1 -1
- package/dist/signal-providers/subscription-d1.d.ts +10 -57
- package/dist/signal-providers/subscription-d1.d.ts.map +1 -1
- package/dist/signal-providers/subscription-d1.js +208 -155
- package/dist/signal-providers/subscription-d1.js.map +1 -1
- package/dist/signal-providers/webhook-route.d.ts +28 -14
- package/dist/signal-providers/webhook-route.d.ts.map +1 -1
- package/dist/signal-providers/webhook-route.js +117 -88
- package/dist/signal-providers/webhook-route.js.map +1 -1
- package/dist/signals/client.js +8 -8
- package/dist/signals/client.js.map +1 -1
- package/dist/signals/d1-shared.js +1 -1
- package/dist/signals/d1-shared.js.map +1 -1
- package/dist/signals/index.d.ts +1 -1
- package/dist/signals/index.d.ts.map +1 -1
- package/dist/signals/index.js.map +1 -1
- package/dist/signals/notification-dispatch.d.ts +12 -6
- package/dist/signals/notification-dispatch.d.ts.map +1 -1
- package/dist/signals/notification-dispatch.js +37 -21
- package/dist/signals/notification-dispatch.js.map +1 -1
- package/dist/signals/notifications-d1.d.ts +5 -8
- package/dist/signals/notifications-d1.d.ts.map +1 -1
- package/dist/signals/notifications-d1.js +8 -13
- package/dist/signals/notifications-d1.js.map +1 -1
- package/dist/signals/router.d.ts +10 -10
- package/dist/signals/router.d.ts.map +1 -1
- package/dist/signals/router.js +49 -51
- package/dist/signals/router.js.map +1 -1
- package/dist/signals/thread-do-routes.d.ts +74 -15
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +484 -41
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/dist/signals/thread-state-d1.d.ts.map +1 -1
- package/dist/signals/thread-state-d1.js +2 -3
- package/dist/signals/thread-state-d1.js.map +1 -1
- package/package.json +29 -6
- package/scripts/seed-deployment-identity.mjs +252 -0
- package/dist/approval-api/tenant-brand.d.ts +0 -35
- package/dist/approval-api/tenant-brand.d.ts.map +0 -1
- package/dist/approval-api/tenant-brand.js +0 -19
- package/dist/approval-api/tenant-brand.js.map +0 -1
- package/dist/approval-api/tenant-context.d.ts +0 -84
- package/dist/approval-api/tenant-context.d.ts.map +0 -1
- package/dist/approval-api/tenant-context.js +0 -112
- package/dist/approval-api/tenant-context.js.map +0 -1
- package/dist/approval-api/tenant-store.d.ts +0 -36
- package/dist/approval-api/tenant-store.d.ts.map +0 -1
- package/dist/approval-api/tenant-store.js +0 -129
- package/dist/approval-api/tenant-store.js.map +0 -1
- package/dist/do-runner/resume-ledger.d.ts +0 -48
- package/dist/do-runner/resume-ledger.d.ts.map +0 -1
- package/dist/do-runner/resume-ledger.js +0 -60
- package/dist/do-runner/resume-ledger.js.map +0 -1
- package/dist/do-runner/thread-header.d.ts +0 -31
- package/dist/do-runner/thread-header.d.ts.map +0 -1
- package/dist/do-runner/thread-header.js +0 -39
- package/dist/do-runner/thread-header.js.map +0 -1
- package/dist/host-kit/subdomain-check.d.ts +0 -23
- package/dist/host-kit/subdomain-check.d.ts.map +0 -1
- package/dist/host-kit/subdomain-check.js +0 -61
- package/dist/host-kit/subdomain-check.js.map +0 -1
- package/dist/host-kit/tenant-registry.d.ts +0 -51
- package/dist/host-kit/tenant-registry.d.ts.map +0 -1
- package/dist/host-kit/tenant-registry.js +0 -88
- package/dist/host-kit/tenant-registry.js.map +0 -1
|
@@ -1,54 +1,200 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// createFlowsafeWorker — the whole production-Worker skeleton the deploy
|
|
3
3
|
// template and the showcase host previously carried as near-byte copies:
|
|
4
|
-
// the /healthz → routers → 404 fetch pipeline over
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// would let a slow sweep permanently starve the purge), and the audit-export
|
|
8
|
-
// queue() consumer. Hosts stay thin shells: they supply their workflows,
|
|
4
|
+
// the /healthz → routers → 404 fetch pipeline over one actor resolver, the
|
|
5
|
+
// alarm-driven maintenance (sweep, purge, and an optional schedule tick never
|
|
6
|
+
// share an invocation). Hosts stay thin shells: they supply their workflows,
|
|
9
7
|
// their identity seam (buildVerifier), and their deployment-specific hooks —
|
|
10
|
-
// preRoutes (extra unauthenticated/authenticated mounts),
|
|
11
|
-
// (e.g.
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { purgeExpiredBackgroundTasks, purgeExpiredNotifications, purgeExpiredScheduleTriggers, purgeExpiredThreadState, purgeExpiredThreads, purgeExpiredWorkflowRuns, } from '../do-runner/index.js';
|
|
8
|
+
// preRoutes (extra unauthenticated/authenticated mounts), beforeStart/
|
|
9
|
+
// beforeResume (e.g. a budget charge), notify (reviewer-facing transport), and
|
|
10
|
+
// extraPurgeDuties (e.g. a host-specific purge). Everything here is structural:
|
|
11
|
+
// host-kit never imports @cloudflare/workers-types.
|
|
12
|
+
import { APPROVAL_ROLES, createActorResolver, createApprovalRouter, createResourceOwnershipSchema, RESOURCE_OWNERSHIP_TABLE, } from '../approval-api/index.js';
|
|
13
|
+
import { createAuditProxyDurableObjectBinding, createAuditProxyQueue, } from '../audit-export/index.js';
|
|
14
|
+
import { credentialsMatch } from '../do-runner/deployment-identity.js';
|
|
15
|
+
import { DEPLOYMENT_IDENTITY_HEADER, DeploymentIdentityError, deploymentIdentityHeaders, ensureDeploymentIdentityBindings, purgeExpiredBackgroundTasks, purgeExpiredNotifications, purgeExpiredScheduleTriggers, purgeExpiredThreadState, purgeExpiredThreads, purgeExpiredWorkflowRuns, verifyDurableObjectDeploymentIdentity, verifyDurableObjectDeploymentRequest, } from '../do-runner/index.js';
|
|
18
16
|
import { bearerActorAuthenticator } from './bearer-auth.js';
|
|
19
17
|
import { createDoRunTopology, } from './do-run-topology.js';
|
|
20
18
|
import { numberVar, optionalNumberVar, selfDecisionPolicyVar, } from './env-vars.js';
|
|
21
19
|
import { approvalStoreFactoryFor, buildHostApprovalService, maintenancePrincipal, reconcileApprovalsOnStatusDetached, runApprovalRetentionPurge, runSlaSweepMaintenance, } from './host-approval-service.js';
|
|
22
20
|
import { createHubTopology } from './hub-topology.js';
|
|
21
|
+
import { MAINTENANCE_RECEIPT_HEADER, mintMaintenanceReceipt, verifyAsymmetricMaintenanceCapability, verifyMaintenanceCapability, } from './maintenance-capability.js';
|
|
23
22
|
import { createRunRouter } from './run-router.js';
|
|
23
|
+
import { validateStateEgressEnv, } from './state-egress.js';
|
|
24
24
|
import { createStreamRouter } from './stream-router.js';
|
|
25
|
+
/** Fixed instance name used by every deployment maintenance namespace. */
|
|
26
|
+
export const MAINTENANCE_INSTANCE_NAME = 'deployment-maintenance';
|
|
27
|
+
function auditQueueFor(env) {
|
|
28
|
+
if (env.FLEET_AUDIT_PROXY === 'required') {
|
|
29
|
+
if (!env.AUDIT_PROXY) {
|
|
30
|
+
throw new Error('external fleet release has no trusted audit proxy');
|
|
31
|
+
}
|
|
32
|
+
if (env.AUDIT_QUEUE) {
|
|
33
|
+
throw new Error('external fleet release has a direct audit queue binding');
|
|
34
|
+
}
|
|
35
|
+
return createAuditProxyQueue(createAuditProxyDurableObjectBinding(env.AUDIT_PROXY), env.DEPLOYMENT_IDENTITY_SECRET);
|
|
36
|
+
}
|
|
37
|
+
return env.AUDIT_QUEUE;
|
|
38
|
+
}
|
|
39
|
+
const STATE_EGRESS_ENV_KEYS = [
|
|
40
|
+
'OUTBOUND_PROXY',
|
|
41
|
+
'OUTBOUND_PROXY_CREDENTIAL',
|
|
42
|
+
'OUTBOUND_TENANT_ID',
|
|
43
|
+
'OUTBOUND_ENVIRONMENT',
|
|
44
|
+
'OUTBOUND_RESOURCE_GROUP_ID',
|
|
45
|
+
'OUTBOUND_STATE_SCRIPT_NAME',
|
|
46
|
+
'OUTBOUND_ROUTE_HOSTNAME',
|
|
47
|
+
'OUTBOUND_POLICY_ID',
|
|
48
|
+
];
|
|
49
|
+
async function validateFleetChannelTopology(env) {
|
|
50
|
+
const hasStateEgressBinding = STATE_EGRESS_ENV_KEYS.some((key) => env[key] !== undefined);
|
|
51
|
+
if (env.FLEET_RESOURCE_ROLE === 'platform-state') {
|
|
52
|
+
if (env.FLEET_AUDIT_PROXY === 'required' ||
|
|
53
|
+
env.AUDIT_PROXY ||
|
|
54
|
+
!env.FLEET_ENVIRONMENT ||
|
|
55
|
+
!env.FLEET_RESOURCE_GROUP ||
|
|
56
|
+
!hasStateEgressBinding) {
|
|
57
|
+
throw new Error('trusted state channel topology is incomplete or ambiguous');
|
|
58
|
+
}
|
|
59
|
+
if (env.OUTBOUND_TENANT_ID !== env.DEPLOYMENT_TENANT ||
|
|
60
|
+
env.OUTBOUND_ENVIRONMENT !== env.FLEET_ENVIRONMENT ||
|
|
61
|
+
env.OUTBOUND_RESOURCE_GROUP_ID !== env.FLEET_RESOURCE_GROUP) {
|
|
62
|
+
throw new Error('trusted state outbound attribution is inconsistent');
|
|
63
|
+
}
|
|
64
|
+
if (env.FLEET_AUDIT_PROXY_INGRESS === 'required'
|
|
65
|
+
? !env.AUDIT_QUEUE || !env.FLEET_DEPLOYMENT_SCRIPT
|
|
66
|
+
: env.AUDIT_QUEUE !== undefined) {
|
|
67
|
+
throw new Error('trusted state audit topology is incomplete or ambiguous');
|
|
68
|
+
}
|
|
69
|
+
await validateStateEgressEnv(env);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (env.FLEET_AUDIT_PROXY_INGRESS === 'required' || hasStateEgressBinding) {
|
|
73
|
+
throw new Error('non-state Worker has trusted state channel bindings');
|
|
74
|
+
}
|
|
75
|
+
if (env.FLEET_AUDIT_PROXY === 'required') {
|
|
76
|
+
if (!env.AUDIT_PROXY || env.AUDIT_QUEUE) {
|
|
77
|
+
throw new Error('external fleet audit topology is incomplete or ambiguous');
|
|
78
|
+
}
|
|
79
|
+
createAuditProxyDurableObjectBinding(env.AUDIT_PROXY);
|
|
80
|
+
}
|
|
81
|
+
else if (env.AUDIT_PROXY) {
|
|
82
|
+
throw new Error('unmarked Worker has an audit proxy namespace');
|
|
83
|
+
}
|
|
84
|
+
if (env.FLEET_MAINTENANCE_CAPABILITIES === 'required' && env.AUDIT_QUEUE) {
|
|
85
|
+
throw new Error('external fleet release has a direct audit queue binding');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
25
88
|
function json(payload, status = 200) {
|
|
26
89
|
return new Response(JSON.stringify(payload), {
|
|
27
90
|
status,
|
|
28
91
|
headers: { 'content-type': 'application/json' },
|
|
29
92
|
});
|
|
30
93
|
}
|
|
94
|
+
const MAINTENANCE_ADMIN_SECRET_PATTERN = /^[\x21-\x7e]{32,256}$/;
|
|
95
|
+
const FLEET_SPEC_DIGEST_PATTERN = /^[a-f0-9]{64}$/;
|
|
96
|
+
async function maintenanceAdminResponse(request, env) {
|
|
97
|
+
const url = new URL(request.url);
|
|
98
|
+
const operation = url.pathname === '/admin/ensure-maintenance'
|
|
99
|
+
? 'ensure'
|
|
100
|
+
: url.pathname === '/admin/maintenance-status'
|
|
101
|
+
? 'status'
|
|
102
|
+
: undefined;
|
|
103
|
+
if (!operation)
|
|
104
|
+
return null;
|
|
105
|
+
const expectedMethod = operation === 'ensure' ? 'POST' : 'GET';
|
|
106
|
+
if (request.method !== expectedMethod) {
|
|
107
|
+
return json({ error: 'method not allowed' }, 405);
|
|
108
|
+
}
|
|
109
|
+
const expected = env.MAINTENANCE_ADMIN_SECRET;
|
|
110
|
+
if (expected === undefined &&
|
|
111
|
+
env.FLEET_MAINTENANCE_CAPABILITIES !== 'required') {
|
|
112
|
+
console.error(JSON.stringify({
|
|
113
|
+
type: 'config-error',
|
|
114
|
+
var: 'MAINTENANCE_ADMIN_SECRET',
|
|
115
|
+
reason: 'maintenance administration is not configured',
|
|
116
|
+
}));
|
|
117
|
+
return json({ error: 'maintenance administration unavailable' }, 503);
|
|
118
|
+
}
|
|
119
|
+
if (expected !== undefined &&
|
|
120
|
+
!MAINTENANCE_ADMIN_SECRET_PATTERN.test(expected)) {
|
|
121
|
+
console.error(JSON.stringify({
|
|
122
|
+
type: 'config-error',
|
|
123
|
+
var: 'MAINTENANCE_ADMIN_SECRET',
|
|
124
|
+
reason: 'maintenance administration is not configured',
|
|
125
|
+
}));
|
|
126
|
+
return json({ error: 'maintenance administration unavailable' }, 503);
|
|
127
|
+
}
|
|
128
|
+
if (expected !== undefined &&
|
|
129
|
+
(await credentialsMatch(expected, env.DEPLOYMENT_IDENTITY_SECRET))) {
|
|
130
|
+
console.error(JSON.stringify({
|
|
131
|
+
type: 'config-error',
|
|
132
|
+
var: 'MAINTENANCE_ADMIN_SECRET',
|
|
133
|
+
reason: 'maintenance and deployment identity credentials must differ',
|
|
134
|
+
}));
|
|
135
|
+
return json({ error: 'maintenance administration unavailable' }, 503);
|
|
136
|
+
}
|
|
137
|
+
const actual = request.headers
|
|
138
|
+
.get('authorization')
|
|
139
|
+
?.match(/^Bearer\s+(.+)$/i)?.[1];
|
|
140
|
+
const maximumCredentialLength = expected === undefined ? 2_048 : 256;
|
|
141
|
+
if (!actual || actual.length > maximumCredentialLength) {
|
|
142
|
+
return json({ error: 'authentication required' }, 401);
|
|
143
|
+
}
|
|
144
|
+
if (expected !== undefined && !(await credentialsMatch(actual, expected))) {
|
|
145
|
+
return json({ error: 'authentication required' }, 401);
|
|
146
|
+
}
|
|
147
|
+
const deploymentSpecDigest = env.FLEET_SPEC_DIGEST;
|
|
148
|
+
if (deploymentSpecDigest !== undefined &&
|
|
149
|
+
!FLEET_SPEC_DIGEST_PATTERN.test(deploymentSpecDigest)) {
|
|
150
|
+
console.error(JSON.stringify({
|
|
151
|
+
type: 'config-error',
|
|
152
|
+
var: 'FLEET_SPEC_DIGEST',
|
|
153
|
+
reason: 'fleet specification digest is malformed',
|
|
154
|
+
}));
|
|
155
|
+
return json({ error: 'maintenance administration unavailable' }, 503);
|
|
156
|
+
}
|
|
157
|
+
const id = env.MAINTENANCE.idFromName(MAINTENANCE_INSTANCE_NAME);
|
|
158
|
+
const response = await env.MAINTENANCE.get(id).fetch(`http://maintenance/${operation}`, {
|
|
159
|
+
method: expectedMethod,
|
|
160
|
+
headers: expected === undefined
|
|
161
|
+
? { authorization: `Bearer ${actual}` }
|
|
162
|
+
: deploymentIdentityHeaders(env.DEPLOYMENT_IDENTITY_SECRET),
|
|
163
|
+
});
|
|
164
|
+
if (expected === undefined)
|
|
165
|
+
return response;
|
|
166
|
+
if (!response.ok || deploymentSpecDigest === undefined)
|
|
167
|
+
return response;
|
|
168
|
+
const payload = await response.json();
|
|
169
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
170
|
+
return json({ error: 'maintenance response is malformed' }, 502);
|
|
171
|
+
}
|
|
172
|
+
return json({
|
|
173
|
+
...payload,
|
|
174
|
+
deploymentSpecDigest,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
31
177
|
export function createFlowsafeWorker(config) {
|
|
32
|
-
//
|
|
33
|
-
// approval store to the verified actor's tenant before any service exists —
|
|
34
|
-
// there is no pre-auth store for a rushed fix to reach.
|
|
178
|
+
// Authenticate before constructing request-scoped services.
|
|
35
179
|
const buildResolve = (env, topology, waitUntil, notify, selfDecision, stream) => {
|
|
36
|
-
const base =
|
|
180
|
+
const base = createActorResolver({
|
|
37
181
|
authenticate: bearerActorAuthenticator(config.buildVerifier(env)),
|
|
38
182
|
storeFactory: approvalStoreFactoryFor(env.DB),
|
|
183
|
+
deploymentTag: env.DEPLOYMENT_TENANT,
|
|
39
184
|
buildService: (store) => buildHostApprovalService(store, {
|
|
40
|
-
|
|
185
|
+
deploymentTag: env.DEPLOYMENT_TENANT,
|
|
186
|
+
systemPrincipalId: config.systemPrincipalId,
|
|
41
187
|
defaultSlaSeconds: numberVar(env.APPROVAL_SLA_SECONDS, 4 * 60 * 60, 'APPROVAL_SLA_SECONDS'),
|
|
42
188
|
// The one topology-specific piece: decisions resume the run
|
|
43
189
|
// through its DO stub.
|
|
44
190
|
resumeRun: config.buildResumeRun
|
|
45
191
|
? config.buildResumeRun(topology.resumeRecord, env)
|
|
46
192
|
: topology.resumeRecord,
|
|
47
|
-
queue: env
|
|
193
|
+
queue: auditQueueFor(env),
|
|
48
194
|
waitUntil,
|
|
49
195
|
notify,
|
|
50
196
|
// Fetch-scope hub fan-out: every request-path mutation reaches the
|
|
51
|
-
//
|
|
197
|
+
// deployment hub, kept alive by ctx.waitUntil (DL-020). Undefined when no
|
|
52
198
|
// HUB is bound, so a non-streaming host is byte-identical to before.
|
|
53
199
|
stream,
|
|
54
200
|
allowSelfDecision: selfDecision,
|
|
@@ -58,7 +204,7 @@ export function createFlowsafeWorker(config) {
|
|
|
58
204
|
// /workflows echo can never contradict decide().
|
|
59
205
|
allowSelfDecision: selfDecision,
|
|
60
206
|
});
|
|
61
|
-
return
|
|
207
|
+
return base;
|
|
62
208
|
};
|
|
63
209
|
// Parse ONCE per deployment value, memoized by the RAW
|
|
64
210
|
// APPROVAL_ALLOW_SELF_DECISION string — NOT by env identity: a host mutates
|
|
@@ -81,9 +227,24 @@ export function createFlowsafeWorker(config) {
|
|
|
81
227
|
selfDecisionCache.set(raw, policy);
|
|
82
228
|
return policy;
|
|
83
229
|
};
|
|
84
|
-
async function runPurgeMaintenance(env,
|
|
230
|
+
async function runPurgeMaintenance(env, trigger) {
|
|
231
|
+
const failures = [];
|
|
232
|
+
const recordFailure = (surface, error) => {
|
|
233
|
+
const failure = String(error);
|
|
234
|
+
failures.push(`${surface}: ${failure}`);
|
|
235
|
+
console.error(JSON.stringify({
|
|
236
|
+
type: 'maintenance-error',
|
|
237
|
+
surface,
|
|
238
|
+
trigger,
|
|
239
|
+
error: failure,
|
|
240
|
+
}));
|
|
241
|
+
};
|
|
85
242
|
let purged;
|
|
86
243
|
try {
|
|
244
|
+
// The resource registry is lazy like Mastra's snapshot table. Retention
|
|
245
|
+
// may be the first resource-aware operation in a fresh deployment, so
|
|
246
|
+
// initialize it before asking the atomic purge to reference it.
|
|
247
|
+
await createResourceOwnershipSchema(env.DB);
|
|
87
248
|
purged = await purgeExpiredWorkflowRuns(env.DB, {
|
|
88
249
|
// allowZero: RUN_RETENTION_DAYS=0 means "purge terminal runs now".
|
|
89
250
|
ttlMs: numberVar(env.RUN_RETENTION_DAYS, 30, 'RUN_RETENTION_DAYS', {
|
|
@@ -97,25 +258,28 @@ export function createFlowsafeWorker(config) {
|
|
|
97
258
|
// (artifacts BEFORE the row — the row is the only record of their keys).
|
|
98
259
|
// Undefined on hosts that wire no R2, so the purge stays byte-identical.
|
|
99
260
|
artifactStore: config.artifactStore,
|
|
261
|
+
// Snapshot + owner release share one D1 transaction, so retention
|
|
262
|
+
// cannot leave unbounded run-ownership tombstones or expose a live row
|
|
263
|
+
// without its authorization record.
|
|
264
|
+
resourceOwnerTable: RESOURCE_OWNERSHIP_TABLE,
|
|
100
265
|
});
|
|
101
266
|
}
|
|
102
267
|
catch (error) {
|
|
103
|
-
|
|
104
|
-
type: 'maintenance-error',
|
|
105
|
-
surface: 'retention-purge',
|
|
106
|
-
cron,
|
|
107
|
-
error: String(error),
|
|
108
|
-
}));
|
|
268
|
+
recordFailure('retention-purge', error);
|
|
109
269
|
}
|
|
110
270
|
// Own containment (inside runApprovalRetentionPurge), same isolation as
|
|
111
271
|
// the snapshot purge above: a failure in any one purge duty must never
|
|
112
272
|
// stop the others.
|
|
113
|
-
const
|
|
114
|
-
store: approvalStoreFactoryFor(env.DB).
|
|
273
|
+
const approvalPurge = await runApprovalRetentionPurge({
|
|
274
|
+
store: approvalStoreFactoryFor(env.DB).store(),
|
|
115
275
|
retentionDays: env.APPROVAL_RETENTION_DAYS,
|
|
116
|
-
|
|
276
|
+
trigger,
|
|
117
277
|
});
|
|
118
|
-
|
|
278
|
+
if (!approvalPurge.ok) {
|
|
279
|
+
failures.push(`approval-retention-purge: ${approvalPurge.error}`);
|
|
280
|
+
}
|
|
281
|
+
const approvalsPurged = approvalPurge.ok ? approvalPurge.value : undefined;
|
|
282
|
+
// Agent-memory thread TTL, opt-in.
|
|
119
283
|
// Its OWN try/catch, like every sibling duty above and below: isolating one
|
|
120
284
|
// loop while a sibling shares its failure is a defect this codebase has
|
|
121
285
|
// already shipped once — a wedged thread purge must cost the run-snapshot
|
|
@@ -137,12 +301,7 @@ export function createFlowsafeWorker(config) {
|
|
|
137
301
|
threadMessagesPurged = purgedThreads.messages;
|
|
138
302
|
}
|
|
139
303
|
catch (error) {
|
|
140
|
-
|
|
141
|
-
type: 'maintenance-error',
|
|
142
|
-
surface: 'thread-retention-purge',
|
|
143
|
-
cron,
|
|
144
|
-
error: String(error),
|
|
145
|
-
}));
|
|
304
|
+
recordFailure('thread-retention-purge', error);
|
|
146
305
|
}
|
|
147
306
|
}
|
|
148
307
|
// Background-task TTL cleanup (Track B, opt-in). Its OWN try/catch, like
|
|
@@ -157,12 +316,7 @@ export function createFlowsafeWorker(config) {
|
|
|
157
316
|
});
|
|
158
317
|
}
|
|
159
318
|
catch (error) {
|
|
160
|
-
|
|
161
|
-
type: 'maintenance-error',
|
|
162
|
-
surface: 'background-task-purge',
|
|
163
|
-
cron,
|
|
164
|
-
error: String(error),
|
|
165
|
-
}));
|
|
319
|
+
recordFailure('background-task-purge', error);
|
|
166
320
|
}
|
|
167
321
|
}
|
|
168
322
|
// Track C notification TTL (opt-in). Its OWN try/catch, like every sibling
|
|
@@ -176,12 +330,7 @@ export function createFlowsafeWorker(config) {
|
|
|
176
330
|
});
|
|
177
331
|
}
|
|
178
332
|
catch (error) {
|
|
179
|
-
|
|
180
|
-
type: 'maintenance-error',
|
|
181
|
-
surface: 'notification-purge',
|
|
182
|
-
cron,
|
|
183
|
-
error: String(error),
|
|
184
|
-
}));
|
|
333
|
+
recordFailure('notification-purge', error);
|
|
185
334
|
}
|
|
186
335
|
}
|
|
187
336
|
// Track C thread-state TTL (opt-in). Same isolation + opt-in posture.
|
|
@@ -194,17 +343,12 @@ export function createFlowsafeWorker(config) {
|
|
|
194
343
|
});
|
|
195
344
|
}
|
|
196
345
|
catch (error) {
|
|
197
|
-
|
|
198
|
-
type: 'maintenance-error',
|
|
199
|
-
surface: 'thread-state-purge',
|
|
200
|
-
cron,
|
|
201
|
-
error: String(error),
|
|
202
|
-
}));
|
|
346
|
+
recordFailure('thread-state-purge', error);
|
|
203
347
|
}
|
|
204
348
|
}
|
|
205
349
|
// Track D schedule-trigger history TTL (opt-in). Same isolation + opt-in
|
|
206
350
|
// posture. Only the fire HISTORY expires; schedule config rows are reaped
|
|
207
|
-
// only at
|
|
351
|
+
// only at deployment teardown.
|
|
208
352
|
let scheduleTriggersPurged;
|
|
209
353
|
const scheduleTriggerRetentionDays = optionalNumberVar(env.SCHEDULE_TRIGGER_RETENTION_DAYS, 'SCHEDULE_TRIGGER_RETENTION_DAYS', { allowZero: true });
|
|
210
354
|
if (scheduleTriggerRetentionDays !== undefined) {
|
|
@@ -214,31 +358,21 @@ export function createFlowsafeWorker(config) {
|
|
|
214
358
|
});
|
|
215
359
|
}
|
|
216
360
|
catch (error) {
|
|
217
|
-
|
|
218
|
-
type: 'maintenance-error',
|
|
219
|
-
surface: 'schedule-trigger-purge',
|
|
220
|
-
cron,
|
|
221
|
-
error: String(error),
|
|
222
|
-
}));
|
|
361
|
+
recordFailure('schedule-trigger-purge', error);
|
|
223
362
|
}
|
|
224
363
|
}
|
|
225
364
|
let extra = {};
|
|
226
365
|
if (config.extraPurgeDuties) {
|
|
227
366
|
try {
|
|
228
|
-
extra = await config.extraPurgeDuties(env
|
|
367
|
+
extra = await config.extraPurgeDuties(env);
|
|
229
368
|
}
|
|
230
369
|
catch (error) {
|
|
231
|
-
|
|
232
|
-
type: 'maintenance-error',
|
|
233
|
-
surface: 'extra-purge-duties',
|
|
234
|
-
cron,
|
|
235
|
-
error: String(error),
|
|
236
|
-
}));
|
|
370
|
+
recordFailure('extra-purge-duties', error);
|
|
237
371
|
}
|
|
238
372
|
}
|
|
239
373
|
console.log(JSON.stringify({
|
|
240
374
|
type: 'maintenance',
|
|
241
|
-
|
|
375
|
+
trigger,
|
|
242
376
|
purged,
|
|
243
377
|
approvalsPurged,
|
|
244
378
|
threadsPurged,
|
|
@@ -250,44 +384,58 @@ export function createFlowsafeWorker(config) {
|
|
|
250
384
|
scheduleTriggersPurged,
|
|
251
385
|
...extra,
|
|
252
386
|
}));
|
|
387
|
+
return failures.length === 0
|
|
388
|
+
? { ok: true, value: undefined }
|
|
389
|
+
: { ok: false, error: failures.join('; ') };
|
|
253
390
|
}
|
|
254
391
|
// Track D schedule tick — its OWN failure-isolated duty (own try/catch, own log
|
|
255
|
-
// line)
|
|
256
|
-
// sweep + purge nothing, and vice versa (the
|
|
257
|
-
async function runScheduleTickDuty(env,
|
|
392
|
+
// line). A wedged fire pass must cost the
|
|
393
|
+
// sweep + purge nothing, and vice versa (the same failure-isolation rationale).
|
|
394
|
+
async function runScheduleTickDuty(env, trigger) {
|
|
258
395
|
const tick = config.scheduleTick?.(env);
|
|
259
396
|
if (!tick) {
|
|
260
|
-
// The tick
|
|
397
|
+
// The tick duty ran but no scheduleTick builder is wired — a misconfig.
|
|
261
398
|
// Do NOT fall through to sweep/purge (this invocation is the tick's).
|
|
262
399
|
console.error(JSON.stringify({
|
|
263
400
|
type: 'config-error',
|
|
264
|
-
var: '
|
|
265
|
-
|
|
266
|
-
reason: 'tick
|
|
401
|
+
var: 'maintenance.tick',
|
|
402
|
+
trigger,
|
|
403
|
+
reason: 'tick duty ran but no scheduleTick builder is configured',
|
|
267
404
|
}));
|
|
268
|
-
return
|
|
405
|
+
return {
|
|
406
|
+
ok: false,
|
|
407
|
+
error: 'tick duty ran but no scheduleTick builder is configured',
|
|
408
|
+
};
|
|
269
409
|
}
|
|
270
410
|
try {
|
|
271
411
|
const result = await tick();
|
|
272
|
-
console.log(JSON.stringify({ type: 'schedule-tick',
|
|
412
|
+
console.log(JSON.stringify({ type: 'schedule-tick', trigger, result }));
|
|
413
|
+
return { ok: true, value: undefined };
|
|
273
414
|
}
|
|
274
415
|
catch (error) {
|
|
416
|
+
const failure = String(error);
|
|
275
417
|
console.error(JSON.stringify({
|
|
276
418
|
type: 'schedule-tick-error',
|
|
277
|
-
|
|
278
|
-
error:
|
|
419
|
+
trigger,
|
|
420
|
+
error: failure,
|
|
279
421
|
}));
|
|
422
|
+
return { ok: false, error: failure };
|
|
280
423
|
}
|
|
281
424
|
}
|
|
282
425
|
return {
|
|
283
426
|
async fetch(request, env, ctx) {
|
|
284
427
|
try {
|
|
428
|
+
await ensureDeploymentIdentityBindings(env);
|
|
429
|
+
await validateFleetChannelTopology(env);
|
|
285
430
|
const url = new URL(request.url);
|
|
286
431
|
if (request.method === 'GET' && url.pathname === '/healthz') {
|
|
287
432
|
return json({ ok: true });
|
|
288
433
|
}
|
|
434
|
+
const maintenanceAdmin = await maintenanceAdminResponse(request, env);
|
|
435
|
+
if (maintenanceAdmin)
|
|
436
|
+
return maintenanceAdmin;
|
|
289
437
|
const waitUntil = (promise) => ctx.waitUntil(promise);
|
|
290
|
-
const topology = createDoRunTopology(env.RUNNER);
|
|
438
|
+
const topology = createDoRunTopology(env.RUNNER, env.DEPLOYMENT_IDENTITY_SECRET);
|
|
291
439
|
const notify = config.notify?.(env);
|
|
292
440
|
const selfDecision = parseSelfDecision(env);
|
|
293
441
|
// Fetch-scope live fan-out sink: present iff a hub is bound (streaming is
|
|
@@ -296,7 +444,7 @@ export function createFlowsafeWorker(config) {
|
|
|
296
444
|
const hub = env.HUB;
|
|
297
445
|
let streamSink;
|
|
298
446
|
if (hub) {
|
|
299
|
-
const hubTopology = createHubTopology(hub);
|
|
447
|
+
const hubTopology = createHubTopology(hub, env.DEPLOYMENT_IDENTITY_SECRET);
|
|
300
448
|
streamSink = (event) => waitUntil(hubTopology.publish(event).catch((error) => console.error(JSON.stringify({
|
|
301
449
|
type: 'stream-publish-error',
|
|
302
450
|
reason: error instanceof Error ? error.message : String(error),
|
|
@@ -327,6 +475,8 @@ export function createFlowsafeWorker(config) {
|
|
|
327
475
|
ticketSecret: env.STREAM_TICKET_SECRET,
|
|
328
476
|
hub: env.HUB,
|
|
329
477
|
runner: env.RUNNER,
|
|
478
|
+
runStatus: topology.status,
|
|
479
|
+
deploymentIdentitySecret: env.DEPLOYMENT_IDENTITY_SECRET,
|
|
330
480
|
})(request);
|
|
331
481
|
if (streamResponse)
|
|
332
482
|
return streamResponse;
|
|
@@ -363,26 +513,37 @@ export function createFlowsafeWorker(config) {
|
|
|
363
513
|
const approvalResponse = await createApprovalRouter({ resolve })(request);
|
|
364
514
|
if (approvalResponse)
|
|
365
515
|
return approvalResponse;
|
|
516
|
+
const beforeStart = config.beforeStart;
|
|
517
|
+
const beforeResume = config.beforeResume;
|
|
366
518
|
const runResponse = await createRunRouter({
|
|
367
519
|
workflows: config.workflows,
|
|
368
520
|
resolve,
|
|
369
|
-
|
|
370
|
-
start:
|
|
371
|
-
? config.wrapStart(topology.start, env)
|
|
372
|
-
: topology.start,
|
|
521
|
+
systemPrincipalId: config.systemPrincipalId,
|
|
522
|
+
start: topology.start,
|
|
373
523
|
status: topology.status,
|
|
374
|
-
resume:
|
|
375
|
-
|
|
376
|
-
|
|
524
|
+
resume: topology.resume,
|
|
525
|
+
beforeStart: beforeStart
|
|
526
|
+
? (context, workflowId, inputData) => beforeStart(context, env, workflowId, inputData)
|
|
527
|
+
: undefined,
|
|
528
|
+
beforeResume: beforeResume
|
|
529
|
+
? (context, workflowId, runId, body) => beforeResume(context, env, workflowId, runId, body)
|
|
530
|
+
: undefined,
|
|
377
531
|
// D4 self-healing, waitUntil-detached — the shared wrapper owns the
|
|
378
532
|
// detach + reconcile-error logging.
|
|
379
|
-
reconcileApprovals: reconcileApprovalsOnStatusDetached(config.
|
|
533
|
+
reconcileApprovals: reconcileApprovalsOnStatusDetached(config.systemPrincipalId, waitUntil),
|
|
380
534
|
})(request);
|
|
381
535
|
if (runResponse)
|
|
382
536
|
return runResponse;
|
|
383
537
|
return json({ error: 'not found' }, 404);
|
|
384
538
|
}
|
|
385
539
|
catch (error) {
|
|
540
|
+
if (error instanceof DeploymentIdentityError) {
|
|
541
|
+
console.error(JSON.stringify({
|
|
542
|
+
type: 'deployment-identity-error',
|
|
543
|
+
reason: error.message,
|
|
544
|
+
}));
|
|
545
|
+
return json({ error: 'deployment unavailable' }, 503);
|
|
546
|
+
}
|
|
386
547
|
// Backstop: a mounted router (or any handler fault) that THROWS before
|
|
387
548
|
// returning a Response — e.g. a future unguarded path decode — is
|
|
388
549
|
// contained as a generic 500 here rather than rejecting out of fetch()
|
|
@@ -394,63 +555,380 @@ export function createFlowsafeWorker(config) {
|
|
|
394
555
|
return json({ error: 'internal error' }, 500);
|
|
395
556
|
}
|
|
396
557
|
},
|
|
397
|
-
async
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
558
|
+
async runMaintenanceDuty(duty, env) {
|
|
559
|
+
try {
|
|
560
|
+
await ensureDeploymentIdentityBindings(env);
|
|
561
|
+
await validateFleetChannelTopology(env);
|
|
562
|
+
if (duty === 'sweep') {
|
|
563
|
+
const hub = env.HUB;
|
|
564
|
+
const hubTopology = hub
|
|
565
|
+
? createHubTopology(hub, env.DEPLOYMENT_IDENTITY_SECRET)
|
|
566
|
+
: undefined;
|
|
567
|
+
return await runSlaSweepMaintenance({
|
|
568
|
+
store: approvalStoreFactoryFor(env.DB).store(),
|
|
569
|
+
systemPrincipal: maintenancePrincipal(config.systemPrincipalId),
|
|
570
|
+
deploymentTag: env.DEPLOYMENT_TENANT,
|
|
571
|
+
queue: auditQueueFor(env),
|
|
572
|
+
trigger: duty,
|
|
573
|
+
notify: config.notify?.(env),
|
|
574
|
+
stream: hubTopology
|
|
575
|
+
? (event) => hubTopology.publish(event)
|
|
576
|
+
: undefined,
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
if (duty === 'purge') {
|
|
580
|
+
return await runPurgeMaintenance(env, duty);
|
|
581
|
+
}
|
|
582
|
+
return await runScheduleTickDuty(env, duty);
|
|
583
|
+
}
|
|
584
|
+
catch (error) {
|
|
585
|
+
const failure = String(error);
|
|
586
|
+
console.error(JSON.stringify({
|
|
587
|
+
type: 'maintenance-error',
|
|
588
|
+
surface: `${duty}-duty`,
|
|
589
|
+
trigger: duty,
|
|
590
|
+
error: failure,
|
|
591
|
+
}));
|
|
592
|
+
return { ok: false, error: failure };
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
const MAINTENANCE_HEALTH_KEY = 'flowsafe:maintenance-health:v1';
|
|
598
|
+
const MAINTENANCE_NONCES_KEY = 'flowsafe:maintenance-nonces:v1';
|
|
599
|
+
const DUTY_ORDER = ['sweep', 'purge', 'tick'];
|
|
600
|
+
/** Build the fixed-name maintenance Durable Object class for one host. */
|
|
601
|
+
export function createFlowsafeMaintenanceDurableObject(config) {
|
|
602
|
+
const intervals = {
|
|
603
|
+
sweep: config.maintenance.sweepIntervalMs,
|
|
604
|
+
purge: config.maintenance.purgeIntervalMs,
|
|
605
|
+
tick: config.maintenance.tickIntervalMs,
|
|
606
|
+
};
|
|
607
|
+
for (const duty of DUTY_ORDER) {
|
|
608
|
+
const interval = intervals[duty];
|
|
609
|
+
if (interval !== undefined &&
|
|
610
|
+
(!Number.isSafeInteger(interval) || interval <= 0)) {
|
|
611
|
+
throw new Error(`${duty} maintenance interval must be a positive integer`);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if ((intervals.tick === undefined) !== (config.scheduleTick === undefined)) {
|
|
615
|
+
throw new Error('tickIntervalMs and scheduleTick must either both be configured or both be absent');
|
|
616
|
+
}
|
|
617
|
+
const worker = createFlowsafeWorker(config);
|
|
618
|
+
return class FlowsafeMaintenanceDurableObject {
|
|
619
|
+
#state;
|
|
620
|
+
#env;
|
|
621
|
+
constructor(state, env) {
|
|
622
|
+
this.#state = state;
|
|
623
|
+
this.#env = env;
|
|
624
|
+
}
|
|
625
|
+
#assertInstanceName() {
|
|
626
|
+
if (this.#state.id.name !== MAINTENANCE_INSTANCE_NAME) {
|
|
627
|
+
throw new Error(`maintenance must be addressed as '${MAINTENANCE_INSTANCE_NAME}'`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
async #health(now) {
|
|
631
|
+
const stored = await this.#state.storage.get(MAINTENANCE_HEALTH_KEY);
|
|
632
|
+
const health = {
|
|
633
|
+
nextSweepAt: validTime(stored?.nextSweepAt) ?? now,
|
|
634
|
+
nextPurgeAt: validTime(stored?.nextPurgeAt) ?? now,
|
|
417
635
|
};
|
|
418
|
-
if (
|
|
419
|
-
|
|
636
|
+
if (intervals.tick !== undefined) {
|
|
637
|
+
health.nextTickAt = validTime(stored?.nextTickAt) ?? now;
|
|
420
638
|
}
|
|
421
|
-
|
|
422
|
-
|
|
639
|
+
for (const duty of DUTY_ORDER) {
|
|
640
|
+
if (intervals[duty] === undefined)
|
|
641
|
+
continue;
|
|
642
|
+
const last = validTime(lastAt(stored, duty));
|
|
643
|
+
if (last !== undefined)
|
|
644
|
+
setLastAt(health, duty, last);
|
|
645
|
+
const attempted = validTime(lastAttemptAt(stored, duty));
|
|
646
|
+
if (attempted !== undefined)
|
|
647
|
+
setLastAttemptAt(health, duty, attempted);
|
|
648
|
+
const error = lastError(stored, duty);
|
|
649
|
+
if (error !== undefined)
|
|
650
|
+
setLastError(health, duty, error);
|
|
423
651
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
652
|
+
return health;
|
|
653
|
+
}
|
|
654
|
+
async #persistAndArm(health, alarmAt) {
|
|
655
|
+
await this.#state.storage.transaction(async (transaction) => {
|
|
656
|
+
await transaction.put(MAINTENANCE_HEALTH_KEY, health);
|
|
657
|
+
await transaction.setAlarm(alarmAt);
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
async #recordOutcome(duty, completedAt, outcome) {
|
|
661
|
+
await this.#state.storage.transaction(async (transaction) => {
|
|
662
|
+
const latest = await transaction.get(MAINTENANCE_HEALTH_KEY);
|
|
663
|
+
if (!latest)
|
|
664
|
+
throw new Error('maintenance health disappeared');
|
|
665
|
+
setLastAttemptAt(latest, duty, completedAt);
|
|
666
|
+
if (outcome.ok) {
|
|
667
|
+
setLastAt(latest, duty, completedAt);
|
|
668
|
+
clearLastError(latest, duty);
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
setLastError(latest, duty, outcome.error);
|
|
672
|
+
}
|
|
673
|
+
await transaction.put(MAINTENANCE_HEALTH_KEY, latest);
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
async #authorizeCapability(request, operation) {
|
|
677
|
+
const token = request.headers
|
|
678
|
+
.get('authorization')
|
|
679
|
+
?.match(/^Bearer\s+(.+)$/i)?.[1];
|
|
680
|
+
if (!token || token.length > 2_048)
|
|
681
|
+
return undefined;
|
|
682
|
+
const environment = this.#env.FLEET_ENVIRONMENT;
|
|
683
|
+
if (!environment)
|
|
684
|
+
return undefined;
|
|
685
|
+
let capability;
|
|
686
|
+
if (this.#env.FLEET_MAINTENANCE_CAPABILITIES === 'required') {
|
|
687
|
+
const encodedPublicKey = this.#env.FLEET_MAINTENANCE_CAPABILITY_PUBLIC_KEY;
|
|
688
|
+
if (!encodedPublicKey)
|
|
689
|
+
return undefined;
|
|
690
|
+
let publicKey;
|
|
691
|
+
try {
|
|
692
|
+
const parsed = JSON.parse(encodedPublicKey);
|
|
693
|
+
if (!parsed ||
|
|
694
|
+
typeof parsed !== 'object' ||
|
|
695
|
+
Array.isArray(parsed) ||
|
|
696
|
+
parsed.kty !== 'OKP' ||
|
|
697
|
+
parsed.crv !== 'Ed25519' ||
|
|
698
|
+
parsed.alg !== 'EdDSA' ||
|
|
699
|
+
typeof parsed.kid !== 'string' ||
|
|
700
|
+
!parsed.kid ||
|
|
701
|
+
typeof parsed.x !== 'string' ||
|
|
702
|
+
parsed.d !== undefined) {
|
|
703
|
+
return undefined;
|
|
704
|
+
}
|
|
705
|
+
publicKey = {
|
|
706
|
+
kty: parsed.kty,
|
|
707
|
+
crv: parsed.crv,
|
|
708
|
+
alg: parsed.alg,
|
|
709
|
+
kid: parsed.kid,
|
|
710
|
+
x: parsed.x,
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
catch {
|
|
714
|
+
return undefined;
|
|
715
|
+
}
|
|
716
|
+
capability = await verifyAsymmetricMaintenanceCapability({
|
|
717
|
+
publicKey,
|
|
718
|
+
token,
|
|
719
|
+
operation,
|
|
720
|
+
tenantTag: this.#env.DEPLOYMENT_TENANT,
|
|
721
|
+
environment,
|
|
722
|
+
});
|
|
429
723
|
}
|
|
430
724
|
else {
|
|
725
|
+
const secret = this.#env.MAINTENANCE_ADMIN_SECRET;
|
|
726
|
+
if (!secret || !MAINTENANCE_ADMIN_SECRET_PATTERN.test(secret)) {
|
|
727
|
+
return undefined;
|
|
728
|
+
}
|
|
729
|
+
capability = await verifyMaintenanceCapability({
|
|
730
|
+
secret,
|
|
731
|
+
token,
|
|
732
|
+
operation,
|
|
733
|
+
tenantTag: this.#env.DEPLOYMENT_TENANT,
|
|
734
|
+
environment,
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
if (!capability)
|
|
738
|
+
return undefined;
|
|
739
|
+
if (operation === 'maintenance-status')
|
|
740
|
+
return capability;
|
|
741
|
+
const nowSeconds = Math.floor(Date.now() / 1_000);
|
|
742
|
+
const consumed = await this.#state.storage.transaction(async (transaction) => {
|
|
743
|
+
const stored = (await transaction.get(MAINTENANCE_NONCES_KEY)) ?? {};
|
|
744
|
+
if (stored[capability.nonce] !== undefined)
|
|
745
|
+
return false;
|
|
746
|
+
const live = Object.fromEntries(Object.entries(stored).filter(([, expiresAt]) => Number.isSafeInteger(expiresAt) && expiresAt >= nowSeconds));
|
|
747
|
+
live[capability.nonce] = capability.expiresAt;
|
|
748
|
+
await transaction.put(MAINTENANCE_NONCES_KEY, live);
|
|
749
|
+
return true;
|
|
750
|
+
});
|
|
751
|
+
return consumed ? capability : undefined;
|
|
752
|
+
}
|
|
753
|
+
async #authorizedRequest(request, operation) {
|
|
754
|
+
if (this.#env.FLEET_MAINTENANCE_CAPABILITIES !== 'required' &&
|
|
755
|
+
request.headers.has(DEPLOYMENT_IDENTITY_HEADER)) {
|
|
756
|
+
await verifyDurableObjectDeploymentRequest(request, this.#state, this.#env);
|
|
757
|
+
return undefined;
|
|
758
|
+
}
|
|
759
|
+
await verifyDurableObjectDeploymentIdentity(this.#state, this.#env);
|
|
760
|
+
return (await this.#authorizeCapability(request, operation)) ?? false;
|
|
761
|
+
}
|
|
762
|
+
async #response(payload, capability) {
|
|
763
|
+
if (!capability)
|
|
764
|
+
return json(payload);
|
|
765
|
+
const secret = this.#env.MAINTENANCE_ADMIN_SECRET;
|
|
766
|
+
if (!secret)
|
|
767
|
+
return json({ error: 'maintenance unavailable' }, 503);
|
|
768
|
+
const receipt = await mintMaintenanceReceipt(secret, capability, payload);
|
|
769
|
+
const response = json(payload);
|
|
770
|
+
response.headers.set(MAINTENANCE_RECEIPT_HEADER, receipt);
|
|
771
|
+
return response;
|
|
772
|
+
}
|
|
773
|
+
async fetch(request) {
|
|
774
|
+
try {
|
|
775
|
+
this.#assertInstanceName();
|
|
776
|
+
const path = new URL(request.url).pathname;
|
|
777
|
+
if (request.method === 'POST' && path === '/ensure') {
|
|
778
|
+
const authorized = await this.#authorizedRequest(request, 'ensure-maintenance');
|
|
779
|
+
if (authorized === false) {
|
|
780
|
+
return json({ error: 'authentication required' }, 401);
|
|
781
|
+
}
|
|
782
|
+
const now = Date.now();
|
|
783
|
+
const health = await this.#health(now);
|
|
784
|
+
const next = nextAlarmAt(health, intervals);
|
|
785
|
+
await this.#persistAndArm(health, Math.max(now, next));
|
|
786
|
+
return this.#response({ ...health, alarmAt: Math.max(now, next) }, authorized);
|
|
787
|
+
}
|
|
788
|
+
if (request.method === 'GET' && path === '/status') {
|
|
789
|
+
const authorized = await this.#authorizedRequest(request, 'maintenance-status');
|
|
790
|
+
if (authorized === false) {
|
|
791
|
+
return json({ error: 'authentication required' }, 401);
|
|
792
|
+
}
|
|
793
|
+
const health = await this.#state.storage.get(MAINTENANCE_HEALTH_KEY);
|
|
794
|
+
return this.#response({
|
|
795
|
+
...(health ?? {}),
|
|
796
|
+
alarmAt: await this.#state.storage.getAlarm(),
|
|
797
|
+
}, authorized);
|
|
798
|
+
}
|
|
799
|
+
return json({ error: 'not found' }, 404);
|
|
800
|
+
}
|
|
801
|
+
catch (error) {
|
|
802
|
+
if (error instanceof DeploymentIdentityError) {
|
|
803
|
+
return json({ error: 'deployment unavailable' }, 503);
|
|
804
|
+
}
|
|
431
805
|
console.error(JSON.stringify({
|
|
432
|
-
type: '
|
|
433
|
-
|
|
434
|
-
raw: controller.cron,
|
|
435
|
-
reason: 'unknown cron expression; running both maintenance surfaces',
|
|
806
|
+
type: 'maintenance-do-error',
|
|
807
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
436
808
|
}));
|
|
437
|
-
|
|
438
|
-
// Both duties are self-contained today, but the JOIN must not
|
|
439
|
-
// depend on that: `.then(f)` skips f on rejection, and the whole
|
|
440
|
-
// point of this fallback is availability of BOTH duties.
|
|
441
|
-
.catch(() => { })
|
|
442
|
-
.then(() => runPurgeMaintenance(env, controller.cron)));
|
|
809
|
+
return json({ error: 'internal error' }, 500);
|
|
443
810
|
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
await
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
811
|
+
}
|
|
812
|
+
async alarm() {
|
|
813
|
+
this.#assertInstanceName();
|
|
814
|
+
await verifyDurableObjectDeploymentIdentity(this.#state, this.#env);
|
|
815
|
+
const now = Date.now();
|
|
816
|
+
const health = await this.#health(now);
|
|
817
|
+
const duty = DUTY_ORDER.find((candidate) => {
|
|
818
|
+
const dueAt = nextAt(health, candidate);
|
|
819
|
+
return (intervals[candidate] !== undefined &&
|
|
820
|
+
dueAt !== undefined &&
|
|
821
|
+
dueAt <= now);
|
|
822
|
+
});
|
|
823
|
+
if (!duty) {
|
|
824
|
+
await this.#persistAndArm(health, nextAlarmAt(health, intervals));
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
const interval = intervals[duty];
|
|
828
|
+
const dueAt = nextAt(health, duty);
|
|
829
|
+
if (interval === undefined || dueAt === undefined) {
|
|
830
|
+
throw new Error(`maintenance duty '${duty}' has no schedule`);
|
|
831
|
+
}
|
|
832
|
+
setNextAt(health, duty, advancePast(dueAt, interval, now));
|
|
833
|
+
const followUpAt = nextAlarmAt(health, intervals);
|
|
834
|
+
await this.#persistAndArm(health, hasDueDuty(health, intervals, now) ? now : followUpAt);
|
|
835
|
+
const outcome = await worker.runMaintenanceDuty(duty, this.#env);
|
|
836
|
+
await this.#recordOutcome(duty, Date.now(), outcome);
|
|
837
|
+
}
|
|
454
838
|
};
|
|
455
839
|
}
|
|
840
|
+
function validTime(value) {
|
|
841
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0
|
|
842
|
+
? value
|
|
843
|
+
: undefined;
|
|
844
|
+
}
|
|
845
|
+
function nextAt(health, duty) {
|
|
846
|
+
if (duty === 'sweep')
|
|
847
|
+
return health?.nextSweepAt;
|
|
848
|
+
if (duty === 'purge')
|
|
849
|
+
return health?.nextPurgeAt;
|
|
850
|
+
return health?.nextTickAt;
|
|
851
|
+
}
|
|
852
|
+
function lastAt(health, duty) {
|
|
853
|
+
if (duty === 'sweep')
|
|
854
|
+
return health?.lastSweepAt;
|
|
855
|
+
if (duty === 'purge')
|
|
856
|
+
return health?.lastPurgeAt;
|
|
857
|
+
return health?.lastTickAt;
|
|
858
|
+
}
|
|
859
|
+
function lastAttemptAt(health, duty) {
|
|
860
|
+
if (duty === 'sweep')
|
|
861
|
+
return health?.lastSweepAttemptAt;
|
|
862
|
+
if (duty === 'purge')
|
|
863
|
+
return health?.lastPurgeAttemptAt;
|
|
864
|
+
return health?.lastTickAttemptAt;
|
|
865
|
+
}
|
|
866
|
+
function lastError(health, duty) {
|
|
867
|
+
const value = duty === 'sweep'
|
|
868
|
+
? health?.lastSweepError
|
|
869
|
+
: duty === 'purge'
|
|
870
|
+
? health?.lastPurgeError
|
|
871
|
+
: health?.lastTickError;
|
|
872
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
873
|
+
}
|
|
874
|
+
function setNextAt(health, duty, value) {
|
|
875
|
+
if (duty === 'sweep')
|
|
876
|
+
health.nextSweepAt = value;
|
|
877
|
+
else if (duty === 'purge')
|
|
878
|
+
health.nextPurgeAt = value;
|
|
879
|
+
else
|
|
880
|
+
health.nextTickAt = value;
|
|
881
|
+
}
|
|
882
|
+
function setLastAt(health, duty, value) {
|
|
883
|
+
if (duty === 'sweep')
|
|
884
|
+
health.lastSweepAt = value;
|
|
885
|
+
else if (duty === 'purge')
|
|
886
|
+
health.lastPurgeAt = value;
|
|
887
|
+
else
|
|
888
|
+
health.lastTickAt = value;
|
|
889
|
+
}
|
|
890
|
+
function setLastAttemptAt(health, duty, value) {
|
|
891
|
+
if (duty === 'sweep')
|
|
892
|
+
health.lastSweepAttemptAt = value;
|
|
893
|
+
else if (duty === 'purge')
|
|
894
|
+
health.lastPurgeAttemptAt = value;
|
|
895
|
+
else
|
|
896
|
+
health.lastTickAttemptAt = value;
|
|
897
|
+
}
|
|
898
|
+
function setLastError(health, duty, value) {
|
|
899
|
+
const bounded = value.slice(0, 1_024);
|
|
900
|
+
if (duty === 'sweep')
|
|
901
|
+
health.lastSweepError = bounded;
|
|
902
|
+
else if (duty === 'purge')
|
|
903
|
+
health.lastPurgeError = bounded;
|
|
904
|
+
else
|
|
905
|
+
health.lastTickError = bounded;
|
|
906
|
+
}
|
|
907
|
+
function clearLastError(health, duty) {
|
|
908
|
+
if (duty === 'sweep')
|
|
909
|
+
delete health.lastSweepError;
|
|
910
|
+
else if (duty === 'purge')
|
|
911
|
+
delete health.lastPurgeError;
|
|
912
|
+
else
|
|
913
|
+
delete health.lastTickError;
|
|
914
|
+
}
|
|
915
|
+
function advancePast(dueAt, interval, now) {
|
|
916
|
+
return dueAt + (Math.floor((now - dueAt) / interval) + 1) * interval;
|
|
917
|
+
}
|
|
918
|
+
function hasDueDuty(health, intervals, now) {
|
|
919
|
+
return DUTY_ORDER.some((duty) => {
|
|
920
|
+
const dueAt = nextAt(health, duty);
|
|
921
|
+
return intervals[duty] !== undefined && dueAt !== undefined && dueAt <= now;
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
function nextAlarmAt(health, intervals) {
|
|
925
|
+
const scheduled = DUTY_ORDER.flatMap((duty) => {
|
|
926
|
+
const dueAt = nextAt(health, duty);
|
|
927
|
+
return intervals[duty] !== undefined && dueAt !== undefined ? [dueAt] : [];
|
|
928
|
+
});
|
|
929
|
+
const next = Math.min(...scheduled);
|
|
930
|
+
if (!Number.isFinite(next))
|
|
931
|
+
throw new Error('maintenance has no duties');
|
|
932
|
+
return next;
|
|
933
|
+
}
|
|
456
934
|
//# sourceMappingURL=flowsafe-worker.js.map
|