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