clinkx 1.2.0 → 1.4.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 +60 -0
- package/README.md +17 -8
- package/clinkx-workflows/dist/architecture-arena/admission.d.ts +16 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js +107 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/contract.d.ts +1368 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js +254 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.d.ts +53 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js +511 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js.map +1 -0
- package/clinkx-workflows/dist/architecture-comparison/admission.d.ts +17 -1
- package/clinkx-workflows/dist/architecture-comparison/admission.js +27 -4
- package/clinkx-workflows/dist/architecture-comparison/admission.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/contract.d.ts +59 -62
- package/clinkx-workflows/dist/architecture-comparison/contract.js +4 -21
- package/clinkx-workflows/dist/architecture-comparison/contract.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/runtime.d.ts +4 -3
- package/clinkx-workflows/dist/architecture-comparison/runtime.js +22 -67
- package/clinkx-workflows/dist/architecture-comparison/runtime.js.map +1 -1
- package/clinkx-workflows/dist/artifacts.d.ts +19 -4
- package/clinkx-workflows/dist/artifacts.js +16 -7
- package/clinkx-workflows/dist/artifacts.js.map +1 -1
- package/clinkx-workflows/dist/backend.d.ts +30 -0
- package/clinkx-workflows/dist/child-env.d.ts +3 -0
- package/clinkx-workflows/dist/child-env.js +11 -0
- package/clinkx-workflows/dist/child-env.js.map +1 -1
- package/clinkx-workflows/dist/clink-client.d.ts +8 -2
- package/clinkx-workflows/dist/clink-client.js +4 -2
- package/clinkx-workflows/dist/clink-client.js.map +1 -1
- package/clinkx-workflows/dist/context-preflight.d.ts +6 -0
- package/clinkx-workflows/dist/context-preflight.js.map +1 -1
- package/clinkx-workflows/dist/council-execution-policy.d.ts +3 -1
- package/clinkx-workflows/dist/council-execution-policy.js +4 -0
- package/clinkx-workflows/dist/council-execution-policy.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/api-routes.d.ts +21 -0
- package/clinkx-workflows/dist/dashboard/api-routes.js +27 -0
- package/clinkx-workflows/dist/dashboard/api-routes.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/challenge-store.d.ts +13 -0
- package/clinkx-workflows/dist/dashboard/auth/challenge-store.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/controller.d.ts +27 -0
- package/clinkx-workflows/dist/dashboard/auth/controller.js +262 -23
- package/clinkx-workflows/dist/dashboard/auth/controller.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.d.ts +62 -0
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.js +70 -0
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/contracts.d.ts +586 -586
- package/clinkx-workflows/dist/dashboard/progress-report-contract.d.ts +28 -28
- package/clinkx-workflows/dist/dashboard/request-admission.d.ts +6 -0
- package/clinkx-workflows/dist/dashboard/request-admission.js +9 -0
- package/clinkx-workflows/dist/dashboard/request-admission.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/server-factory.js +15 -1
- package/clinkx-workflows/dist/dashboard/server-factory.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/session-log-contract.d.ts +32 -32
- package/clinkx-workflows/dist/dashboard/settings-contract.d.ts +1806 -0
- package/clinkx-workflows/dist/dashboard/settings-contract.js +630 -0
- package/clinkx-workflows/dist/dashboard/settings-contract.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.d.ts +48 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.js +195 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.d.ts +87 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.js +690 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.js.map +1 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/index-DSOtuS0p.js +122 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/style-C5guUjwN.css +1 -0
- package/clinkx-workflows/dist/dashboard-ui/index.html +2 -2
- package/clinkx-workflows/dist/delegate-signature.d.ts +4 -3
- package/clinkx-workflows/dist/delegate-signature.js +4 -3
- package/clinkx-workflows/dist/delegate-signature.js.map +1 -1
- package/clinkx-workflows/dist/durable-file-core.d.ts +76 -0
- package/clinkx-workflows/dist/durable-file-core.js +146 -0
- package/clinkx-workflows/dist/durable-file-core.js.map +1 -0
- package/clinkx-workflows/dist/durable-file.d.ts +13 -0
- package/clinkx-workflows/dist/durable-file.js +22 -0
- package/clinkx-workflows/dist/durable-file.js.map +1 -0
- package/clinkx-workflows/dist/engine.d.ts +42 -7
- package/clinkx-workflows/dist/engine.js +196 -24
- package/clinkx-workflows/dist/engine.js.map +1 -1
- package/clinkx-workflows/dist/index.js +68 -2
- package/clinkx-workflows/dist/index.js.map +1 -1
- package/clinkx-workflows/dist/ping-pong/admission.d.ts +127 -0
- package/clinkx-workflows/dist/ping-pong/admission.js +522 -0
- package/clinkx-workflows/dist/ping-pong/admission.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/contract.d.ts +2539 -0
- package/clinkx-workflows/dist/ping-pong/contract.js +819 -0
- package/clinkx-workflows/dist/ping-pong/contract.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/control.d.ts +67 -0
- package/clinkx-workflows/dist/ping-pong/control.js +12 -0
- package/clinkx-workflows/dist/ping-pong/control.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.d.ts +24 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js +55 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime.d.ts +235 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js +1183 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/settle.d.ts +57 -0
- package/clinkx-workflows/dist/ping-pong/settle.js +58 -0
- package/clinkx-workflows/dist/ping-pong/settle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.d.ts +65 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js +138 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js.map +1 -0
- package/clinkx-workflows/dist/plan-review/publication.d.ts +2 -1
- package/clinkx-workflows/dist/plan-review/publication.js +5 -6
- package/clinkx-workflows/dist/plan-review/publication.js.map +1 -1
- package/clinkx-workflows/dist/plan-review/snapshot.d.ts +3 -2
- package/clinkx-workflows/dist/plan-review/snapshot.js +8 -8
- package/clinkx-workflows/dist/plan-review/snapshot.js.map +1 -1
- package/clinkx-workflows/dist/recipe-host.d.ts +315 -0
- package/clinkx-workflows/dist/recipe-host.js +183 -0
- package/clinkx-workflows/dist/recipe-host.js.map +1 -0
- package/clinkx-workflows/dist/recipe-seats.d.ts +96 -0
- package/clinkx-workflows/dist/recipe-seats.js +163 -0
- package/clinkx-workflows/dist/recipe-seats.js.map +1 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.d.ts +67 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js +111 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js.map +1 -0
- package/clinkx-workflows/dist/reducers/index.d.ts +9 -1
- package/clinkx-workflows/dist/reducers/index.js +30 -0
- package/clinkx-workflows/dist/reducers/index.js.map +1 -1
- package/clinkx-workflows/dist/run-lock-reclaim.d.ts +44 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js +86 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js.map +1 -0
- package/clinkx-workflows/dist/settings/audit.d.ts +101 -0
- package/clinkx-workflows/dist/settings/audit.js +318 -0
- package/clinkx-workflows/dist/settings/audit.js.map +1 -0
- package/clinkx-workflows/dist/settings/bootstrap.d.ts +35 -0
- package/clinkx-workflows/dist/settings/bootstrap.js +52 -0
- package/clinkx-workflows/dist/settings/bootstrap.js.map +1 -0
- package/clinkx-workflows/dist/settings/codec.d.ts +51 -0
- package/clinkx-workflows/dist/settings/codec.js +410 -0
- package/clinkx-workflows/dist/settings/codec.js.map +1 -0
- package/clinkx-workflows/dist/settings/durable-write.d.ts +48 -0
- package/clinkx-workflows/dist/settings/durable-write.js +31 -0
- package/clinkx-workflows/dist/settings/durable-write.js.map +1 -0
- package/clinkx-workflows/dist/settings/index.d.ts +20 -0
- package/clinkx-workflows/dist/settings/index.js +23 -0
- package/clinkx-workflows/dist/settings/index.js.map +1 -0
- package/clinkx-workflows/dist/settings/loader.d.ts +80 -0
- package/clinkx-workflows/dist/settings/loader.js +577 -0
- package/clinkx-workflows/dist/settings/loader.js.map +1 -0
- package/clinkx-workflows/dist/settings/lock.d.ts +108 -0
- package/clinkx-workflows/dist/settings/lock.js +344 -0
- package/clinkx-workflows/dist/settings/lock.js.map +1 -0
- package/clinkx-workflows/dist/settings/paths.d.ts +65 -0
- package/clinkx-workflows/dist/settings/paths.js +173 -0
- package/clinkx-workflows/dist/settings/paths.js.map +1 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.d.ts +47 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.js +305 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.js.map +1 -0
- package/clinkx-workflows/dist/settings/registry.d.ts +178 -0
- package/clinkx-workflows/dist/settings/registry.js +1285 -0
- package/clinkx-workflows/dist/settings/registry.js.map +1 -0
- package/clinkx-workflows/dist/settings/schema.d.ts +404 -0
- package/clinkx-workflows/dist/settings/schema.js +415 -0
- package/clinkx-workflows/dist/settings/schema.js.map +1 -0
- package/clinkx-workflows/dist/settings/store.d.ts +157 -0
- package/clinkx-workflows/dist/settings/store.js +842 -0
- package/clinkx-workflows/dist/settings/store.js.map +1 -0
- package/clinkx-workflows/dist/state-schema.d.ts +11 -0
- package/clinkx-workflows/dist/state-schema.js +4 -1
- package/clinkx-workflows/dist/state-schema.js.map +1 -1
- package/clinkx-workflows/dist/state.d.ts +178 -3
- package/clinkx-workflows/dist/state.js +618 -83
- package/clinkx-workflows/dist/state.js.map +1 -1
- package/clinkx-workflows/dist/stdio-entry.d.ts +2 -0
- package/clinkx-workflows/dist/stdio-entry.js +52 -0
- package/clinkx-workflows/dist/stdio-entry.js.map +1 -0
- package/clinkx-workflows/dist/transport.d.ts +2 -1
- package/clinkx-workflows/dist/transport.js +5 -0
- package/clinkx-workflows/dist/transport.js.map +1 -1
- package/clinkx-workflows/dist/validators/architecture-arena.d.ts +6 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js +16 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js.map +1 -0
- package/clinkx-workflows/dist/validators/index.js +4 -0
- package/clinkx-workflows/dist/validators/index.js.map +1 -1
- package/clinkx-workflows/dist/validators/ping-pong.d.ts +6 -0
- package/clinkx-workflows/dist/validators/ping-pong.js +10 -0
- package/clinkx-workflows/dist/validators/ping-pong.js.map +1 -0
- package/clinkx-workflows/dist/workflow-tools.d.ts +51 -5
- package/clinkx-workflows/dist/workflow-tools.js +785 -34
- package/clinkx-workflows/dist/workflow-tools.js.map +1 -1
- package/clinkx-workflows/dist/workflow-verification-contract.d.ts +4 -4
- package/clinkx-workflows/templates/architecture-arena/candidate.txt +39 -0
- package/clinkx-workflows/templates/architecture-arena/judge.txt +34 -0
- package/clinkx-workflows/templates/ping-pong/implement.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/remediate.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/review.txt +27 -0
- package/clinkx-workflows/workflows/architecture-arena.yaml +127 -0
- package/clinkx-workflows/workflows/ping-pong.yaml +158 -0
- package/dist/context-builder/bundle.js +4 -3
- package/dist/context-builder/bundle.js.map +1 -1
- package/dist/context-builder/delegate-packet.d.ts +1 -0
- package/dist/context-builder/delegate-packet.js +66 -7
- package/dist/context-builder/delegate-packet.js.map +1 -1
- package/dist/context-builder/delegate-validation.d.ts +12 -3
- package/dist/context-builder/delegate-validation.js +23 -13
- package/dist/context-builder/delegate-validation.js.map +1 -1
- package/dist/context-builder/external-gaps.d.ts +14 -3
- package/dist/context-builder/external-gaps.js +124 -18
- package/dist/context-builder/external-gaps.js.map +1 -1
- package/dist/context-builder/handler.js +108 -191
- package/dist/context-builder/handler.js.map +1 -1
- package/dist/context-builder/review-coverage.js +27 -1
- package/dist/context-builder/review-coverage.js.map +1 -1
- package/dist/context-builder/secrets.js +1 -1
- package/dist/context-builder/secrets.js.map +1 -1
- package/dist/context-builder/versions.d.ts +10 -4
- package/dist/context-builder/versions.js +10 -4
- package/dist/context-builder/versions.js.map +1 -1
- package/dist/context-builder/workflow-runner.js +33 -24
- package/dist/context-builder/workflow-runner.js.map +1 -1
- package/dist/dashboard-cli.d.ts +2 -0
- package/dist/dashboard-cli.js +197 -58
- package/dist/dashboard-cli.js.map +1 -1
- package/dist/dashboard-companion/client.d.ts +10 -0
- package/dist/dashboard-companion/client.js +43 -1
- package/dist/dashboard-companion/client.js.map +1 -1
- package/dist/dashboard-companion/config.d.ts +19 -1
- package/dist/dashboard-companion/config.js +104 -3
- package/dist/dashboard-companion/config.js.map +1 -1
- package/dist/dashboard-companion/protocol.d.ts +16 -0
- package/dist/dashboard-companion/protocol.js +55 -1
- package/dist/dashboard-companion/protocol.js.map +1 -1
- package/dist/dashboard-companion/workstation-binding.d.ts +69 -0
- package/dist/dashboard-companion/workstation-binding.js +192 -0
- package/dist/dashboard-companion/workstation-binding.js.map +1 -0
- package/dist/dashboard-gateway/admin-cli.d.ts +10 -0
- package/dist/dashboard-gateway/admin-cli.js +266 -0
- package/dist/dashboard-gateway/admin-cli.js.map +1 -0
- package/dist/dashboard-gateway/config.d.ts +50 -0
- package/dist/dashboard-gateway/config.js +235 -1
- package/dist/dashboard-gateway/config.js.map +1 -1
- package/dist/dashboard-gateway/connection-registry.d.ts +100 -0
- package/dist/dashboard-gateway/connection-registry.js +228 -0
- package/dist/dashboard-gateway/connection-registry.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/account-auth.d.ts +78 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js +198 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/billing.d.ts +264 -0
- package/dist/dashboard-gateway/control-plane/billing.js +787 -0
- package/dist/dashboard-gateway/control-plane/billing.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/contracts.d.ts +1293 -0
- package/dist/dashboard-gateway/control-plane/contracts.js +280 -0
- package/dist/dashboard-gateway/control-plane/contracts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/cookies.d.ts +13 -0
- package/dist/dashboard-gateway/control-plane/cookies.js +46 -0
- package/dist/dashboard-gateway/control-plane/cookies.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/credentials.d.ts +63 -0
- package/dist/dashboard-gateway/control-plane/credentials.js +66 -0
- package/dist/dashboard-gateway/control-plane/credentials.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/entitlements.d.ts +118 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js +329 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/errors.d.ts +17 -0
- package/dist/dashboard-gateway/control-plane/errors.js +23 -0
- package/dist/dashboard-gateway/control-plane/errors.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/hosts.d.ts +6 -0
- package/dist/dashboard-gateway/control-plane/hosts.js +16 -0
- package/dist/dashboard-gateway/control-plane/hosts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/http.d.ts +34 -0
- package/dist/dashboard-gateway/control-plane/http.js +331 -0
- package/dist/dashboard-gateway/control-plane/http.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/schema.d.ts +9 -0
- package/dist/dashboard-gateway/control-plane/schema.js +247 -0
- package/dist/dashboard-gateway/control-plane/schema.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/secrets.d.ts +20 -0
- package/dist/dashboard-gateway/control-plane/secrets.js +39 -0
- package/dist/dashboard-gateway/control-plane/secrets.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/store.d.ts +579 -0
- package/dist/dashboard-gateway/control-plane/store.js +1115 -0
- package/dist/dashboard-gateway/control-plane/store.js.map +1 -0
- package/dist/dashboard-gateway/cookie-boundary.d.ts +60 -0
- package/dist/dashboard-gateway/cookie-boundary.js +205 -0
- package/dist/dashboard-gateway/cookie-boundary.js.map +1 -0
- package/dist/dashboard-gateway/index.js +271 -494
- package/dist/dashboard-gateway/index.js.map +1 -1
- package/dist/dashboard-gateway/main.js +7 -2
- package/dist/dashboard-gateway/main.js.map +1 -1
- package/dist/dashboard-gateway/routing.d.ts +43 -0
- package/dist/dashboard-gateway/routing.js +65 -0
- package/dist/dashboard-gateway/routing.js.map +1 -0
- package/dist/dashboard-gateway/tenant-gateway.d.ts +56 -0
- package/dist/dashboard-gateway/tenant-gateway.js +989 -0
- package/dist/dashboard-gateway/tenant-gateway.js.map +1 -0
- package/dist/dashboard-gateway/tenant-main.d.ts +41 -0
- package/dist/dashboard-gateway/tenant-main.js +303 -0
- package/dist/dashboard-gateway/tenant-main.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-errors.d.ts +16 -0
- package/dist/dashboard-gateway/tunnel-errors.js +22 -0
- package/dist/dashboard-gateway/tunnel-errors.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-http.d.ts +21 -0
- package/dist/dashboard-gateway/tunnel-http.js +195 -0
- package/dist/dashboard-gateway/tunnel-http.js.map +1 -0
- package/dist/elaris/config.d.ts +10 -0
- package/dist/elaris/config.js +27 -1
- package/dist/elaris/config.js.map +1 -1
- package/dist/elaris/tools.js +4 -1
- package/dist/elaris/tools.js.map +1 -1
- package/dist/handler.d.ts +6 -0
- package/dist/handler.js +17 -1
- package/dist/handler.js.map +1 -1
- package/dist/index.d.ts +0 -31
- package/dist/index.js +41 -404
- package/dist/index.js.map +1 -1
- package/dist/lib.d.ts +17 -0
- package/dist/lib.js +17 -0
- package/dist/lib.js.map +1 -0
- package/dist/local-clink-backend.d.ts +2 -1
- package/dist/local-clink-backend.js +28 -4
- package/dist/local-clink-backend.js.map +1 -1
- package/dist/parsers/extract.d.ts +19 -1
- package/dist/parsers/extract.js +68 -3
- package/dist/parsers/extract.js.map +1 -1
- package/dist/pipeline.d.ts +17 -1
- package/dist/pipeline.js +34 -6
- package/dist/pipeline.js.map +1 -1
- package/dist/process-group.d.ts +65 -0
- package/dist/process-group.js +153 -0
- package/dist/process-group.js.map +1 -0
- package/dist/progress-report/lock.d.ts +17 -59
- package/dist/progress-report/lock.js +25 -329
- package/dist/progress-report/lock.js.map +1 -1
- package/dist/progress-report/schema.d.ts +23 -23
- package/dist/project-verification/files.js +1 -1
- package/dist/project-verification/files.js.map +1 -1
- package/dist/project-verification/process.js +23 -44
- package/dist/project-verification/process.js.map +1 -1
- package/dist/project-verification/service.js +9 -1
- package/dist/project-verification/service.js.map +1 -1
- package/dist/prompt.d.ts +7 -0
- package/dist/prompt.js +18 -1
- package/dist/prompt.js.map +1 -1
- package/dist/recipe-host/git-trust.d.ts +43 -0
- package/dist/recipe-host/git-trust.js +360 -0
- package/dist/recipe-host/git-trust.js.map +1 -0
- package/dist/recipe-host/git.d.ts +42 -0
- package/dist/recipe-host/git.js +207 -0
- package/dist/recipe-host/git.js.map +1 -0
- package/dist/recipe-host/native-recipe-host.d.ts +127 -0
- package/dist/recipe-host/native-recipe-host.js +880 -0
- package/dist/recipe-host/native-recipe-host.js.map +1 -0
- package/dist/recipe-host/seat-cwd.d.ts +16 -0
- package/dist/recipe-host/seat-cwd.js +170 -0
- package/dist/recipe-host/seat-cwd.js.map +1 -0
- package/dist/runner.d.ts +43 -1
- package/dist/runner.js +234 -35
- package/dist/runner.js.map +1 -1
- package/dist/server.js +84 -5
- package/dist/server.js.map +1 -1
- package/dist/settings-cli/clients.d.ts +137 -0
- package/dist/settings-cli/clients.js +1124 -0
- package/dist/settings-cli/clients.js.map +1 -0
- package/dist/settings-cli/commands.d.ts +67 -0
- package/dist/settings-cli/commands.js +726 -0
- package/dist/settings-cli/commands.js.map +1 -0
- package/dist/settings-cli/format.d.ts +34 -0
- package/dist/settings-cli/format.js +40 -0
- package/dist/settings-cli/format.js.map +1 -0
- package/dist/settings-cli/main.d.ts +18 -0
- package/dist/settings-cli/main.js +256 -0
- package/dist/settings-cli/main.js.map +1 -0
- package/dist/stdio-main.d.ts +73 -0
- package/dist/stdio-main.js +455 -0
- package/dist/stdio-main.js.map +1 -0
- package/docs/configuration.md +362 -40
- package/docs/engineering-recipes.md +291 -0
- package/docs/security.md +234 -6
- package/docs/verification/project-checks.md +24 -2
- package/package.json +6 -2
- package/clinkx-workflows/dist/dashboard-ui/assets/index-BdpmbPps.js +0 -115
- package/clinkx-workflows/dist/dashboard-ui/assets/style-Im5i1iAs.css +0 -1
|
@@ -0,0 +1,1115 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { MAX_WORKSTATIONS, SubscriptionStateSchema } from "./contracts.js";
|
|
3
|
+
import { evaluateEntitlementAccess } from "./entitlements.js";
|
|
4
|
+
import { ControlError, ControlStoreUnavailableError } from "./errors.js";
|
|
5
|
+
import { applyControlPlaneSchema } from "./schema.js";
|
|
6
|
+
import { constantTimeEqual, generateControlId } from "./secrets.js";
|
|
7
|
+
export { MAX_WORKSTATIONS };
|
|
8
|
+
export const DEFAULT_BUSY_TIMEOUT_MS = 5_000;
|
|
9
|
+
const WORKSTATION_LABEL_PATTERN = /^[\x20-\x7e]{1,64}$/;
|
|
10
|
+
export const DEFAULT_MAX_LIVE_PENDING_LOGINS = 10_000;
|
|
11
|
+
/** Extended result codes; `@types/node` does not declare `errcode`, so it is narrowed at runtime. */
|
|
12
|
+
export const SQLITE_BUSY = 5;
|
|
13
|
+
export const SQLITE_CONSTRAINT_UNIQUE = 2067;
|
|
14
|
+
export const SQLITE_CONSTRAINT_PRIMARYKEY = 1555;
|
|
15
|
+
export function sqliteErrcode(error) {
|
|
16
|
+
if (typeof error !== "object" || error === null || !("errcode" in error))
|
|
17
|
+
return undefined;
|
|
18
|
+
const value = error.errcode;
|
|
19
|
+
return typeof value === "number" ? value : undefined;
|
|
20
|
+
}
|
|
21
|
+
function isUniqueViolation(error) {
|
|
22
|
+
const code = sqliteErrcode(error);
|
|
23
|
+
return code === SQLITE_CONSTRAINT_UNIQUE || code === SQLITE_CONSTRAINT_PRIMARYKEY;
|
|
24
|
+
}
|
|
25
|
+
function text(row, key) {
|
|
26
|
+
const value = row[key];
|
|
27
|
+
if (typeof value !== "string")
|
|
28
|
+
throw new Error(`control store column ${key} is not text`);
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
function optionalText(row, key) {
|
|
32
|
+
const value = row[key];
|
|
33
|
+
if (value === null)
|
|
34
|
+
return null;
|
|
35
|
+
if (typeof value !== "string")
|
|
36
|
+
throw new Error(`control store column ${key} is not text`);
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
function integer(row, key) {
|
|
40
|
+
const value = row[key];
|
|
41
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) {
|
|
42
|
+
throw new Error(`control store column ${key} is not an integer`);
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function optionalInteger(row, key) {
|
|
47
|
+
return row[key] === null ? null : integer(row, key);
|
|
48
|
+
}
|
|
49
|
+
export function tenantActorOf(context) {
|
|
50
|
+
return { sessionId: context.sessionId, accountId: context.account.id, authorityRevision: context.account.authorityRevision };
|
|
51
|
+
}
|
|
52
|
+
export function accountActorOf(context) {
|
|
53
|
+
return { sessionId: context.sessionId, accountId: context.account.id, authorityRevision: context.account.authorityRevision };
|
|
54
|
+
}
|
|
55
|
+
/** The longest operator grant, counted from the moment it is written. */
|
|
56
|
+
export const MAX_OPERATOR_GRANT_MS = 400 * 24 * 60 * 60_000;
|
|
57
|
+
const OPERATOR_REASON_PATTERN = /^[\x20-\x7e]{1,200}$/;
|
|
58
|
+
const ACCOUNT_COLUMNS = "a.id as a_id, a.issuer as a_issuer, a.subject as a_subject, a.display_name as a_display_name, a.authority_revision as a_revision";
|
|
59
|
+
const TENANT_COLUMNS = "t.id as t_id";
|
|
60
|
+
function accountFrom(row) {
|
|
61
|
+
return {
|
|
62
|
+
id: text(row, "a_id"),
|
|
63
|
+
issuer: text(row, "a_issuer"),
|
|
64
|
+
subject: text(row, "a_subject"),
|
|
65
|
+
displayName: optionalText(row, "a_display_name"),
|
|
66
|
+
authorityRevision: integer(row, "a_revision"),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function tenantFrom(row) {
|
|
70
|
+
return { id: text(row, "t_id") };
|
|
71
|
+
}
|
|
72
|
+
function workstationFrom(row) {
|
|
73
|
+
return {
|
|
74
|
+
companionId: text(row, "c_id"),
|
|
75
|
+
tenantId: text(row, "c_tenant_id"),
|
|
76
|
+
label: text(row, "c_label"),
|
|
77
|
+
createdAt: integer(row, "c_created_at"),
|
|
78
|
+
activeCredentialId: optionalText(row, "k_id"),
|
|
79
|
+
keyIssuedAt: optionalInteger(row, "k_created_at"),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function role(value) {
|
|
83
|
+
if (value !== "owner")
|
|
84
|
+
throw new Error("unknown membership role");
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
const SUBSCRIPTION_STATES = new Set(SubscriptionStateSchema.options);
|
|
88
|
+
function subscriptionState(value) {
|
|
89
|
+
if (!SUBSCRIPTION_STATES.has(value))
|
|
90
|
+
throw new Error("unknown subscription state");
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
function environment(value) {
|
|
94
|
+
if (value === null || value === "test" || value === "live")
|
|
95
|
+
return value;
|
|
96
|
+
throw new Error("unknown billing environment");
|
|
97
|
+
}
|
|
98
|
+
function entitlementFrom(row) {
|
|
99
|
+
return {
|
|
100
|
+
tenantId: text(row, "tenant_id"),
|
|
101
|
+
revision: integer(row, "revision"),
|
|
102
|
+
environment: environment(optionalText(row, "environment")),
|
|
103
|
+
stripeCustomerId: optionalText(row, "stripe_customer_id"),
|
|
104
|
+
stripeSubscriptionId: optionalText(row, "stripe_subscription_id"),
|
|
105
|
+
subscriptionState: subscriptionState(text(row, "subscription_state")),
|
|
106
|
+
currentPeriodEnd: optionalInteger(row, "current_period_end"),
|
|
107
|
+
graceInvoiceId: optionalText(row, "grace_invoice_id"),
|
|
108
|
+
graceDeadline: optionalInteger(row, "grace_deadline"),
|
|
109
|
+
suspendedAt: optionalInteger(row, "suspended_at"),
|
|
110
|
+
suspendedReason: optionalText(row, "suspended_reason"),
|
|
111
|
+
grantExpiresAt: optionalInteger(row, "grant_expires_at"),
|
|
112
|
+
grantReason: optionalText(row, "grant_reason"),
|
|
113
|
+
grantUpdatedAt: optionalInteger(row, "grant_updated_at"),
|
|
114
|
+
lastReconciledAt: optionalInteger(row, "last_reconciled_at"),
|
|
115
|
+
updatedAt: integer(row, "updated_at"),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const BILLING_JOB_TRIGGERS = new Set(["webhook", "startup", "periodic"]);
|
|
119
|
+
const BILLING_JOB_STATES = new Set(["pending", "running", "succeeded", "failed"]);
|
|
120
|
+
const MAX_BILLING_JOB_ERROR_LENGTH = 1000;
|
|
121
|
+
function billingEnvironment(value) {
|
|
122
|
+
if (value !== "test" && value !== "live")
|
|
123
|
+
throw new Error("unknown billing environment");
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
function billingJobFrom(row) {
|
|
127
|
+
const trigger = text(row, "trigger");
|
|
128
|
+
const state = text(row, "state");
|
|
129
|
+
if (!BILLING_JOB_TRIGGERS.has(trigger))
|
|
130
|
+
throw new Error("unknown billing job trigger");
|
|
131
|
+
if (!BILLING_JOB_STATES.has(state))
|
|
132
|
+
throw new Error("unknown billing job state");
|
|
133
|
+
return {
|
|
134
|
+
id: text(row, "id"),
|
|
135
|
+
environment: billingEnvironment(text(row, "environment")),
|
|
136
|
+
trigger: trigger,
|
|
137
|
+
eventId: optionalText(row, "event_id"),
|
|
138
|
+
tenantId: optionalText(row, "tenant_id"),
|
|
139
|
+
stripeCustomerId: optionalText(row, "stripe_customer_id"),
|
|
140
|
+
stripeSubscriptionId: optionalText(row, "stripe_subscription_id"),
|
|
141
|
+
stripeInvoiceId: optionalText(row, "stripe_invoice_id"),
|
|
142
|
+
state: state,
|
|
143
|
+
attempts: integer(row, "attempts"),
|
|
144
|
+
nextAttemptAt: integer(row, "next_attempt_at"),
|
|
145
|
+
lastError: optionalText(row, "last_error"),
|
|
146
|
+
createdAt: integer(row, "created_at"),
|
|
147
|
+
updatedAt: integer(row, "updated_at"),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function assertBillingEnvironment(value) {
|
|
151
|
+
if (value !== "test" && value !== "live")
|
|
152
|
+
throw new Error("unknown billing environment");
|
|
153
|
+
}
|
|
154
|
+
function sameBinding(record, binding) {
|
|
155
|
+
return (record.environment === binding.environment &&
|
|
156
|
+
record.stripeCustomerId === binding.stripeCustomerId &&
|
|
157
|
+
record.stripeSubscriptionId === binding.stripeSubscriptionId);
|
|
158
|
+
}
|
|
159
|
+
function assertProviderId(name, value) {
|
|
160
|
+
if (value !== null && !/^[\x21-\x7e]{1,255}$/.test(value)) {
|
|
161
|
+
throw new Error(`${name} must be 1-255 printable ASCII characters`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function assertEntitlementUpdate(update) {
|
|
165
|
+
const { binding } = update;
|
|
166
|
+
if (binding.environment !== null && binding.environment !== "test" && binding.environment !== "live") {
|
|
167
|
+
throw new Error("invalid entitlement update: unknown billing environment");
|
|
168
|
+
}
|
|
169
|
+
assertProviderId("stripeCustomerId", binding.stripeCustomerId);
|
|
170
|
+
assertProviderId("stripeSubscriptionId", binding.stripeSubscriptionId);
|
|
171
|
+
if (binding.stripeSubscriptionId !== null && binding.stripeCustomerId === null) {
|
|
172
|
+
throw new Error("invalid entitlement update: a subscription binding requires a customer");
|
|
173
|
+
}
|
|
174
|
+
if (binding.stripeCustomerId !== null && binding.environment === null) {
|
|
175
|
+
throw new Error("invalid entitlement update: a customer binding requires an environment");
|
|
176
|
+
}
|
|
177
|
+
if (!SUBSCRIPTION_STATES.has(update.subscriptionState)) {
|
|
178
|
+
throw new Error("invalid entitlement update: unknown subscription state");
|
|
179
|
+
}
|
|
180
|
+
if (update.currentPeriodEnd !== null && (!Number.isSafeInteger(update.currentPeriodEnd) || update.currentPeriodEnd <= 0)) {
|
|
181
|
+
throw new Error("invalid entitlement update: currentPeriodEnd must be positive UTC milliseconds or null");
|
|
182
|
+
}
|
|
183
|
+
if (update.grace !== "keep" && update.grace !== "clear") {
|
|
184
|
+
throw new Error("invalid entitlement update: grace must be keep or clear");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function expiryAfter(now, ttlMs) {
|
|
188
|
+
if (!Number.isSafeInteger(ttlMs) || ttlMs <= 0)
|
|
189
|
+
throw new Error("lifetime must be a positive integer of milliseconds");
|
|
190
|
+
return now + ttlMs;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Durable control-plane state on one SQLite file and one writer process.
|
|
194
|
+
*
|
|
195
|
+
* Every public method either returns a decision computed from committed state
|
|
196
|
+
* or throws `ControlStoreUnavailableError`; it never returns a partial or
|
|
197
|
+
* default value that a caller could read as access. Methods accept verifiers,
|
|
198
|
+
* never plaintext secrets.
|
|
199
|
+
*/
|
|
200
|
+
export class ControlPlaneStore {
|
|
201
|
+
db;
|
|
202
|
+
clock;
|
|
203
|
+
statements = new Map();
|
|
204
|
+
entitlementListeners = new Set();
|
|
205
|
+
maxLivePendingLogins;
|
|
206
|
+
constructor(db, clock, maxLivePendingLogins) {
|
|
207
|
+
this.db = db;
|
|
208
|
+
this.clock = clock;
|
|
209
|
+
this.maxLivePendingLogins = maxLivePendingLogins;
|
|
210
|
+
}
|
|
211
|
+
static open(options) {
|
|
212
|
+
const maxLivePendingLogins = options.maxLivePendingLogins ?? DEFAULT_MAX_LIVE_PENDING_LOGINS;
|
|
213
|
+
if (!Number.isSafeInteger(maxLivePendingLogins) || maxLivePendingLogins <= 0) {
|
|
214
|
+
throw new Error("maxLivePendingLogins must be a positive integer");
|
|
215
|
+
}
|
|
216
|
+
let db;
|
|
217
|
+
try {
|
|
218
|
+
db = new DatabaseSync(options.path, {
|
|
219
|
+
timeout: options.busyTimeoutMs ?? DEFAULT_BUSY_TIMEOUT_MS,
|
|
220
|
+
enableForeignKeyConstraints: true,
|
|
221
|
+
});
|
|
222
|
+
const mode = db.prepare("pragma journal_mode = wal").get();
|
|
223
|
+
if (mode?.journal_mode !== "wal")
|
|
224
|
+
throw new Error("control store could not enable WAL journaling");
|
|
225
|
+
db.exec("pragma synchronous = full");
|
|
226
|
+
applyControlPlaneSchema(db);
|
|
227
|
+
return new ControlPlaneStore(db, options.now ?? Date.now, maxLivePendingLogins);
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
if (db?.isOpen)
|
|
231
|
+
db.close();
|
|
232
|
+
if (error instanceof ControlError)
|
|
233
|
+
throw error;
|
|
234
|
+
throw new ControlStoreUnavailableError("control store could not be opened", { cause: error });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
get isOpen() {
|
|
238
|
+
return this.db.isOpen;
|
|
239
|
+
}
|
|
240
|
+
close() {
|
|
241
|
+
if (this.db.isOpen)
|
|
242
|
+
this.db.close();
|
|
243
|
+
}
|
|
244
|
+
statement(sql) {
|
|
245
|
+
let prepared = this.statements.get(sql);
|
|
246
|
+
if (prepared === undefined) {
|
|
247
|
+
prepared = this.db.prepare(sql);
|
|
248
|
+
this.statements.set(sql, prepared);
|
|
249
|
+
}
|
|
250
|
+
return prepared;
|
|
251
|
+
}
|
|
252
|
+
get(sql, ...params) {
|
|
253
|
+
return this.statement(sql).get(...params);
|
|
254
|
+
}
|
|
255
|
+
all(sql, ...params) {
|
|
256
|
+
return this.statement(sql).all(...params);
|
|
257
|
+
}
|
|
258
|
+
changes(sql, ...params) {
|
|
259
|
+
return Number(this.statement(sql).run(...params).changes);
|
|
260
|
+
}
|
|
261
|
+
/** Read paths sample the clock immediately before their single query. */
|
|
262
|
+
read(operation) {
|
|
263
|
+
return this.guard(() => operation(this.readClock()));
|
|
264
|
+
}
|
|
265
|
+
guard(operation) {
|
|
266
|
+
if (!this.db.isOpen)
|
|
267
|
+
throw new ControlStoreUnavailableError("control store is closed");
|
|
268
|
+
try {
|
|
269
|
+
return operation();
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
if (error instanceof ControlStoreUnavailableError)
|
|
273
|
+
throw error;
|
|
274
|
+
throw new ControlStoreUnavailableError("control store operation failed", { cause: error });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
readClock() {
|
|
278
|
+
const now = this.clock();
|
|
279
|
+
if (!Number.isSafeInteger(now) || now <= 0)
|
|
280
|
+
throw new Error("control-store clock returned an invalid time");
|
|
281
|
+
return now;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Run `operation` inside BEGIN IMMEDIATE. A returned result commits, so a
|
|
285
|
+
* denial that burns a single-use secret is durable; a thrown error rolls back.
|
|
286
|
+
*
|
|
287
|
+
* `now` is sampled only after the write lock is held. Acquiring it can wait
|
|
288
|
+
* up to the busy timeout, so a time taken earlier could let a session or code
|
|
289
|
+
* that expired during the wait still authorize the write.
|
|
290
|
+
*/
|
|
291
|
+
transaction(operation) {
|
|
292
|
+
return this.guard(() => {
|
|
293
|
+
if (this.db.isTransaction)
|
|
294
|
+
throw new Error("nested control-store transaction");
|
|
295
|
+
this.db.exec("begin immediate");
|
|
296
|
+
try {
|
|
297
|
+
const result = operation(this.readClock());
|
|
298
|
+
if (result instanceof Promise)
|
|
299
|
+
throw new Error("control-store transactions must be synchronous");
|
|
300
|
+
this.db.exec("commit");
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
try {
|
|
305
|
+
if (this.db.isTransaction)
|
|
306
|
+
this.db.exec("rollback");
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
// The original failure is the useful diagnostic; the connection is unusable either way.
|
|
310
|
+
}
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
upsertAccount(input) {
|
|
316
|
+
return this.transaction((now) => {
|
|
317
|
+
const existing = this.get(`select ${ACCOUNT_COLUMNS} from account a where a.issuer = ? and a.subject = ?`, input.issuer, input.subject);
|
|
318
|
+
if (existing !== undefined) {
|
|
319
|
+
const account = accountFrom(existing);
|
|
320
|
+
if (account.displayName !== input.displayName) {
|
|
321
|
+
this.changes("update account set display_name = ?, updated_at = ? where id = ?", input.displayName, now, account.id);
|
|
322
|
+
}
|
|
323
|
+
return { ...account, displayName: input.displayName };
|
|
324
|
+
}
|
|
325
|
+
const id = generateControlId("acc");
|
|
326
|
+
this.changes("insert into account (id, issuer, subject, display_name, created_at, updated_at) values (?, ?, ?, ?, ?, ?)", id, input.issuer, input.subject, input.displayName, now, now);
|
|
327
|
+
return { id, issuer: input.issuer, subject: input.subject, displayName: input.displayName, authorityRevision: 1 };
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
getAccount(accountId) {
|
|
331
|
+
return this.guard(() => {
|
|
332
|
+
const row = this.get(`select ${ACCOUNT_COLUMNS} from account a where a.id = ?`, accountId);
|
|
333
|
+
return row === undefined ? null : accountFrom(row);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/** Invalidate every account session of this account by advancing its authority revision. */
|
|
337
|
+
revokeAccountSessions(accountId) {
|
|
338
|
+
return this.transaction((now) => this.changes("update account set authority_revision = authority_revision + 1, updated_at = ? where id = ?", now, accountId) === 1);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The account's workspace: its tenant, owner membership, and a no-access
|
|
342
|
+
* entitlement row, created together on first call. Idempotent; an account
|
|
343
|
+
* owns at most one workspace.
|
|
344
|
+
*/
|
|
345
|
+
ensureWorkspace(accountId) {
|
|
346
|
+
return this.transaction((now) => {
|
|
347
|
+
if (this.get("select 1 from account where id = ?", accountId) === undefined) {
|
|
348
|
+
return { status: "account_missing" };
|
|
349
|
+
}
|
|
350
|
+
const existing = this.get(`select ${TENANT_COLUMNS} from membership m join tenant t on t.id = m.tenant_id
|
|
351
|
+
where m.account_id = ? and m.role = 'owner'`, accountId);
|
|
352
|
+
if (existing !== undefined)
|
|
353
|
+
return { status: "ok", tenant: tenantFrom(existing), created: false };
|
|
354
|
+
const id = generateControlId("ten");
|
|
355
|
+
this.changes("insert into tenant (id, created_at) values (?, ?)", id, now);
|
|
356
|
+
this.changes("insert into membership (tenant_id, account_id, role, created_at) values (?, ?, 'owner', ?)", id, accountId, now);
|
|
357
|
+
this.changes("insert into entitlement (tenant_id, updated_at) values (?, ?)", id, now);
|
|
358
|
+
return { status: "ok", tenant: { id }, created: true };
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
getTenant(tenantId) {
|
|
362
|
+
return this.guard(() => {
|
|
363
|
+
const row = this.get(`select ${TENANT_COLUMNS} from tenant t where t.id = ?`, tenantId);
|
|
364
|
+
return row === undefined ? null : tenantFrom(row);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
listMemberships(accountId) {
|
|
368
|
+
return this.guard(() => this.all(`select ${TENANT_COLUMNS}, m.role as m_role from membership m join tenant t on t.id = m.tenant_id
|
|
369
|
+
where m.account_id = ? order by t.id limit 100`, accountId).map((row) => ({ tenant: tenantFrom(row), role: role(text(row, "m_role")) })));
|
|
370
|
+
}
|
|
371
|
+
getMembershipRole(tenantId, accountId) {
|
|
372
|
+
return this.guard(() => {
|
|
373
|
+
const row = this.get("select role from membership where tenant_id = ? and account_id = ?", tenantId, accountId);
|
|
374
|
+
return row === undefined ? null : role(text(row, "role"));
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
/** Remove a membership; sessions stop resolving the workspace at once, because membership is read on every request. */
|
|
378
|
+
removeMembership(tenantId, accountId) {
|
|
379
|
+
return this.transaction(() => this.changes("delete from membership where tenant_id = ? and account_id = ?", tenantId, accountId) === 1);
|
|
380
|
+
}
|
|
381
|
+
/** Refused when the tenant already has an owner or the account already owns a workspace. */
|
|
382
|
+
addOwnerMembership(tenantId, accountId) {
|
|
383
|
+
return this.transaction((now) => {
|
|
384
|
+
if (this.get("select 1 from membership where tenant_id = ? and role = 'owner'", tenantId) !== undefined) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
try {
|
|
388
|
+
this.changes("insert into membership (tenant_id, account_id, role, created_at) values (?, ?, 'owner', ?)", tenantId, accountId, now);
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
if (isUniqueViolation(error))
|
|
392
|
+
return false;
|
|
393
|
+
throw error;
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Record an anonymous login start. At the live-row cap nothing is written and
|
|
400
|
+
* `rate_limited` is returned, never thrown, so the guard does not report it
|
|
401
|
+
* as a store failure.
|
|
402
|
+
*/
|
|
403
|
+
insertPendingLogin(input) {
|
|
404
|
+
return this.transaction((now) => {
|
|
405
|
+
const live = this.get("select count(*) as n from pending_login where expires_at > ? and consumed_at is null", now) ?? {};
|
|
406
|
+
if (integer(live, "n") >= this.maxLivePendingLogins)
|
|
407
|
+
return { status: "rate_limited" };
|
|
408
|
+
this.changes(`insert into pending_login (state_verifier, binding_verifier, code_verifier, nonce, created_at, expires_at)
|
|
409
|
+
values (?, ?, ?, ?, ?, ?)`, input.stateVerifier, input.bindingVerifier, input.codeVerifier, input.nonce, now, expiryAfter(now, input.ttlMs));
|
|
410
|
+
return { status: "ok" };
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
/** Single use: the login is consumed on first presentation, including when it has expired. */
|
|
414
|
+
consumePendingLogin(stateVerifier) {
|
|
415
|
+
return this.transaction((now) => {
|
|
416
|
+
const row = this.get("select * from pending_login where state_verifier = ?", stateVerifier);
|
|
417
|
+
if (row === undefined)
|
|
418
|
+
return { status: "missing" };
|
|
419
|
+
if (row.consumed_at !== null)
|
|
420
|
+
return { status: "consumed" };
|
|
421
|
+
if (this.changes("update pending_login set consumed_at = ?, code_verifier = null, nonce = null where state_verifier = ? and consumed_at is null", now, stateVerifier) !== 1) {
|
|
422
|
+
return { status: "consumed" };
|
|
423
|
+
}
|
|
424
|
+
if (integer(row, "expires_at") <= now)
|
|
425
|
+
return { status: "expired" };
|
|
426
|
+
return {
|
|
427
|
+
status: "ok",
|
|
428
|
+
login: {
|
|
429
|
+
bindingVerifier: text(row, "binding_verifier"),
|
|
430
|
+
codeVerifier: text(row, "code_verifier"),
|
|
431
|
+
nonce: text(row, "nonce"),
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
/** Returns the session's expiry, computed from the transaction's clock. */
|
|
437
|
+
insertAccountSession(input) {
|
|
438
|
+
return this.transaction((now) => {
|
|
439
|
+
const row = this.get("select authority_revision from account where id = ?", input.accountId);
|
|
440
|
+
if (row === undefined)
|
|
441
|
+
throw new Error("account session for a missing account");
|
|
442
|
+
this.changes(`insert into account_session (id, verifier, account_id, authority_revision, created_at, expires_at)
|
|
443
|
+
values (?, ?, ?, ?, ?, ?)`, input.id, input.verifier, input.accountId, integer(row, "authority_revision"), now, expiryAfter(now, input.ttlMs));
|
|
444
|
+
return expiryAfter(now, input.ttlMs);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
resolveAccountSession(verifier) {
|
|
448
|
+
return this.read((now) => {
|
|
449
|
+
const row = this.get(`select s.id as s_id, s.verifier as s_verifier, s.expires_at as s_expires_at, ${ACCOUNT_COLUMNS}
|
|
450
|
+
from account_session s join account a on a.id = s.account_id
|
|
451
|
+
where s.verifier = ? and s.revoked_at is null and s.expires_at > ? and s.authority_revision = a.authority_revision`, verifier, now);
|
|
452
|
+
if (row === undefined || !constantTimeEqual(text(row, "s_verifier"), verifier))
|
|
453
|
+
return null;
|
|
454
|
+
return { sessionId: text(row, "s_id"), account: accountFrom(row), expiresAt: integer(row, "s_expires_at") };
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* The browser authority for workspace and tunneled access: a live account
|
|
459
|
+
* session together with the workspace its account owns, read in one query.
|
|
460
|
+
* `null` when the session does not resolve or the account owns no workspace.
|
|
461
|
+
*/
|
|
462
|
+
resolveTenantMember(verifier) {
|
|
463
|
+
return this.read((now) => {
|
|
464
|
+
const row = this.get(`select s.id as s_id, s.verifier as s_verifier, s.expires_at as s_expires_at, m.role as m_role,
|
|
465
|
+
${ACCOUNT_COLUMNS}, ${TENANT_COLUMNS}
|
|
466
|
+
from account_session s
|
|
467
|
+
join account a on a.id = s.account_id
|
|
468
|
+
join membership m on m.account_id = s.account_id and m.role = 'owner'
|
|
469
|
+
join tenant t on t.id = m.tenant_id
|
|
470
|
+
where s.verifier = ? and s.revoked_at is null and s.expires_at > ? and s.authority_revision = a.authority_revision`, verifier, now);
|
|
471
|
+
if (row === undefined || !constantTimeEqual(text(row, "s_verifier"), verifier))
|
|
472
|
+
return null;
|
|
473
|
+
return {
|
|
474
|
+
sessionId: text(row, "s_id"),
|
|
475
|
+
account: accountFrom(row),
|
|
476
|
+
tenant: tenantFrom(row),
|
|
477
|
+
role: role(text(row, "m_role")),
|
|
478
|
+
expiresAt: integer(row, "s_expires_at"),
|
|
479
|
+
};
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
/** Sign-out: revoke one account session. */
|
|
483
|
+
revokeAccountSession(verifier) {
|
|
484
|
+
return this.transaction((now) => this.changes("update account_session set revoked_at = ? where verifier = ? and revoked_at is null", now, verifier) === 1);
|
|
485
|
+
}
|
|
486
|
+
accountSessionAuthorizes(actor, now) {
|
|
487
|
+
return (this.get(`select 1 from account_session s join account a on a.id = s.account_id
|
|
488
|
+
where s.id = ? and s.account_id = ? and s.revoked_at is null and s.expires_at > ?
|
|
489
|
+
and s.authority_revision = a.authority_revision and a.authority_revision = ?`, actor.sessionId, actor.accountId, now, actor.authorityRevision) !== undefined);
|
|
490
|
+
}
|
|
491
|
+
/** Current owner authority for a workspace mutation: a live, unsuperseded account session of the owner. */
|
|
492
|
+
tenantOwnerAuthorizes(tenantId, actor, now) {
|
|
493
|
+
return (this.accountSessionAuthorizes(actor, now) &&
|
|
494
|
+
this.get("select 1 from membership where tenant_id = ? and account_id = ? and role = 'owner'", tenantId, actor.accountId) !== undefined);
|
|
495
|
+
}
|
|
496
|
+
workstationRow(tenantId, companionId) {
|
|
497
|
+
return this.get(`select c.id as c_id, c.tenant_id as c_tenant_id, c.label as c_label, c.created_at as c_created_at,
|
|
498
|
+
c.revoked_at as c_revoked_at, k.id as k_id, k.created_at as k_created_at
|
|
499
|
+
from companion c
|
|
500
|
+
left join device_credential k on k.companion_id = c.id and k.revoked_at is null
|
|
501
|
+
where c.id = ? and c.tenant_id = ?`, companionId, tenantId);
|
|
502
|
+
}
|
|
503
|
+
/** The workspace's live workstations, oldest first. Never returns a verifier. */
|
|
504
|
+
listWorkstations(tenantId) {
|
|
505
|
+
return this.guard(() => this.all(`select c.id as c_id, c.tenant_id as c_tenant_id, c.label as c_label, c.created_at as c_created_at,
|
|
506
|
+
k.id as k_id, k.created_at as k_created_at
|
|
507
|
+
from companion c
|
|
508
|
+
left join device_credential k on k.companion_id = c.id and k.revoked_at is null
|
|
509
|
+
where c.tenant_id = ? and c.revoked_at is null
|
|
510
|
+
order by c.created_at, c.rowid`, tenantId).map(workstationFrom));
|
|
511
|
+
}
|
|
512
|
+
/** One live workstation of this workspace, or `null`: unknown, revoked, or another workspace's. */
|
|
513
|
+
findLiveWorkstation(tenantId, companionId) {
|
|
514
|
+
return this.guard(() => {
|
|
515
|
+
const row = this.workstationRow(tenantId, companionId);
|
|
516
|
+
return row === undefined || row.c_revoked_at !== null ? null : workstationFrom(row);
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
insertCredential(tenantId, companionId, credential, now) {
|
|
520
|
+
const credentialId = generateControlId("cred");
|
|
521
|
+
this.changes(`insert into device_credential (id, tenant_id, companion_id, purpose, verifier, created_at)
|
|
522
|
+
values (?, ?, ?, 'companion_agent', ?, ?)`, credentialId, tenantId, companionId, credential.verifier, now);
|
|
523
|
+
return credentialId;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Create a workstation and its API key in one commit. Requires the owner's
|
|
527
|
+
* current authority, an entitlement that grants access, and fewer than
|
|
528
|
+
* `MAX_WORKSTATIONS` live workstations; the cap is counted under the write lock.
|
|
529
|
+
*/
|
|
530
|
+
createWorkstation(input) {
|
|
531
|
+
if (!WORKSTATION_LABEL_PATTERN.test(input.label))
|
|
532
|
+
throw new Error("workstation label must be 1-64 printable ASCII characters");
|
|
533
|
+
return this.transaction((now) => {
|
|
534
|
+
if (!this.tenantOwnerAuthorizes(input.tenantId, input.actor, now))
|
|
535
|
+
return { status: "unauthorized" };
|
|
536
|
+
const entitlement = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
537
|
+
const access = evaluateEntitlementAccess(entitlement === undefined ? null : entitlementFrom(entitlement), now);
|
|
538
|
+
if (!access.allowed)
|
|
539
|
+
return { status: "entitlement_inactive" };
|
|
540
|
+
const live = this.get("select count(*) as n from companion where tenant_id = ? and revoked_at is null", input.tenantId) ?? {};
|
|
541
|
+
if (integer(live, "n") >= MAX_WORKSTATIONS)
|
|
542
|
+
return { status: "workstation_limit" };
|
|
543
|
+
const companionId = generateControlId("cmp");
|
|
544
|
+
this.changes("insert into companion (id, tenant_id, label, created_at) values (?, ?, ?, ?)", companionId, input.tenantId, input.label, now);
|
|
545
|
+
const credentialId = this.insertCredential(input.tenantId, companionId, input.credential, now);
|
|
546
|
+
return {
|
|
547
|
+
status: "ok",
|
|
548
|
+
credentialId,
|
|
549
|
+
workstation: {
|
|
550
|
+
companionId,
|
|
551
|
+
tenantId: input.tenantId,
|
|
552
|
+
label: input.label,
|
|
553
|
+
createdAt: now,
|
|
554
|
+
activeCredentialId: credentialId,
|
|
555
|
+
keyIssuedAt: now,
|
|
556
|
+
},
|
|
557
|
+
};
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Replace a live workstation's key in one commit: the old key is revoked and
|
|
562
|
+
* the new one issued, with no overlap. Owner only; a revoked workstation is
|
|
563
|
+
* `not_active`. Needs no entitlement, as a key is unusable while access is denied.
|
|
564
|
+
*/
|
|
565
|
+
rotateWorkstationKey(input) {
|
|
566
|
+
return this.transaction((now) => {
|
|
567
|
+
if (!this.tenantOwnerAuthorizes(input.tenantId, input.actor, now))
|
|
568
|
+
return { status: "unauthorized" };
|
|
569
|
+
const companion = this.get("select revoked_at from companion where id = ? and tenant_id = ?", input.companionId, input.tenantId);
|
|
570
|
+
if (companion === undefined)
|
|
571
|
+
return { status: "not_found" };
|
|
572
|
+
if (companion.revoked_at !== null)
|
|
573
|
+
return { status: "not_active" };
|
|
574
|
+
const previous = this.get("select id from device_credential where companion_id = ? and revoked_at is null", input.companionId);
|
|
575
|
+
this.changes("update device_credential set revoked_at = ?, revoked_reason = 'rotation' where companion_id = ? and revoked_at is null", now, input.companionId);
|
|
576
|
+
const credentialId = this.insertCredential(input.tenantId, input.companionId, input.credential, now);
|
|
577
|
+
return {
|
|
578
|
+
status: "ok",
|
|
579
|
+
credentialId,
|
|
580
|
+
revokedCredentialId: previous === undefined ? null : text(previous, "id"),
|
|
581
|
+
issuedAt: now,
|
|
582
|
+
};
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
/** Revoke a workstation and its key in one commit, freeing its cap slot. Permanent; owner only. */
|
|
586
|
+
revokeWorkstation(input) {
|
|
587
|
+
return this.transaction((now) => {
|
|
588
|
+
if (!this.tenantOwnerAuthorizes(input.tenantId, input.actor, now))
|
|
589
|
+
return { status: "unauthorized" };
|
|
590
|
+
const companion = this.get("select revoked_at from companion where id = ? and tenant_id = ?", input.companionId, input.tenantId);
|
|
591
|
+
if (companion === undefined)
|
|
592
|
+
return { status: "not_found" };
|
|
593
|
+
const revokedAt = optionalInteger(companion, "revoked_at");
|
|
594
|
+
if (revokedAt !== null)
|
|
595
|
+
return { status: "ok", revokedAt, alreadyRevoked: true };
|
|
596
|
+
this.changes("update companion set revoked_at = ? where id = ? and revoked_at is null", now, input.companionId);
|
|
597
|
+
this.changes("update device_credential set revoked_at = ?, revoked_reason = 'owner_request' where companion_id = ? and revoked_at is null", now, input.companionId);
|
|
598
|
+
return { status: "ok", revokedAt: now, alreadyRevoked: false };
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
verifyDeviceCredential(verifier) {
|
|
602
|
+
return this.guard(() => {
|
|
603
|
+
const row = this.get(`select d.id as d_id, d.verifier as d_verifier, d.companion_id as d_companion_id, d.revoked_at as d_revoked_at,
|
|
604
|
+
c.revoked_at as c_revoked_at, ${TENANT_COLUMNS}
|
|
605
|
+
from device_credential d
|
|
606
|
+
join companion c on c.id = d.companion_id and c.tenant_id = d.tenant_id
|
|
607
|
+
join tenant t on t.id = d.tenant_id
|
|
608
|
+
where d.verifier = ? and d.purpose = 'companion_agent'`, verifier);
|
|
609
|
+
if (row === undefined || !constantTimeEqual(text(row, "d_verifier"), verifier))
|
|
610
|
+
return { status: "unknown" };
|
|
611
|
+
if (row.d_revoked_at !== null || row.c_revoked_at !== null)
|
|
612
|
+
return { status: "revoked" };
|
|
613
|
+
return {
|
|
614
|
+
status: "ok",
|
|
615
|
+
credentialId: text(row, "d_id"),
|
|
616
|
+
companionId: text(row, "d_companion_id"),
|
|
617
|
+
tenant: tenantFrom(row),
|
|
618
|
+
};
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
getEntitlement(tenantId) {
|
|
622
|
+
return this.guard(() => {
|
|
623
|
+
const row = this.get("select * from entitlement where tenant_id = ?", tenantId);
|
|
624
|
+
return row === undefined ? null : entitlementFrom(row);
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
/** One tenant's entitlement together with the store clock sampled for the same read. */
|
|
628
|
+
readEntitlementAccess(tenantId) {
|
|
629
|
+
return this.read((now) => {
|
|
630
|
+
const row = this.get("select * from entitlement where tenant_id = ?", tenantId);
|
|
631
|
+
return { now, entitlement: row === undefined ? null : entitlementFrom(row) };
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
/** Every entitlement, ordered by tenant ID, with the store clock sampled for the same read. */
|
|
635
|
+
snapshotEntitlements() {
|
|
636
|
+
return this.read((now) => ({
|
|
637
|
+
now,
|
|
638
|
+
entitlements: this.all("select * from entitlement order by tenant_id").map(entitlementFrom),
|
|
639
|
+
}));
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Called synchronously after each committed entitlement change, outside the
|
|
643
|
+
* transaction. Returns an unsubscribe function.
|
|
644
|
+
*/
|
|
645
|
+
onEntitlementChange(listener) {
|
|
646
|
+
this.entitlementListeners.add(listener);
|
|
647
|
+
return () => {
|
|
648
|
+
this.entitlementListeners.delete(listener);
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
notifyEntitlementChange(tenantId) {
|
|
652
|
+
for (const listener of [...this.entitlementListeners]) {
|
|
653
|
+
try {
|
|
654
|
+
listener(tenantId);
|
|
655
|
+
}
|
|
656
|
+
catch {
|
|
657
|
+
// The write has already committed; a listener failure must not report it as failed.
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Operator suspension. Independent of billing and of operator grants: no
|
|
663
|
+
* entitlement update, payment, or grant clears it. Like every operator write
|
|
664
|
+
* it advances the revision, and with `expectedRevision` it applies only if
|
|
665
|
+
* the entitlement has not changed since the caller read it.
|
|
666
|
+
*/
|
|
667
|
+
suspendTenant(input) {
|
|
668
|
+
if (input.reason.length < 1 || input.reason.length > 200)
|
|
669
|
+
throw new Error("suspension reason must be 1-200 characters");
|
|
670
|
+
const result = this.transaction((now) => {
|
|
671
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
672
|
+
if (row === undefined)
|
|
673
|
+
return { status: "not_found" };
|
|
674
|
+
const current = entitlementFrom(row);
|
|
675
|
+
if (input.expectedRevision !== undefined && current.revision !== input.expectedRevision) {
|
|
676
|
+
return { status: "stale_revision", current };
|
|
677
|
+
}
|
|
678
|
+
if (current.suspendedAt !== null)
|
|
679
|
+
return { status: "ok", suspendedAt: current.suspendedAt, alreadySuspended: true };
|
|
680
|
+
this.changes(`update entitlement set suspended_at = ?, suspended_reason = ?, revision = revision + 1, updated_at = ?
|
|
681
|
+
where tenant_id = ?`, now, input.reason, now, input.tenantId);
|
|
682
|
+
return { status: "ok", suspendedAt: now, alreadySuspended: false };
|
|
683
|
+
});
|
|
684
|
+
if (result.status === "ok" && !result.alreadySuspended)
|
|
685
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
686
|
+
return result;
|
|
687
|
+
}
|
|
688
|
+
unsuspendTenant(tenantId, options = {}) {
|
|
689
|
+
const result = this.transaction((now) => {
|
|
690
|
+
const row = this.get("select * from entitlement where tenant_id = ?", tenantId);
|
|
691
|
+
if (row === undefined)
|
|
692
|
+
return { status: "not_found" };
|
|
693
|
+
const current = entitlementFrom(row);
|
|
694
|
+
if (options.expectedRevision !== undefined && current.revision !== options.expectedRevision) {
|
|
695
|
+
return { status: "stale_revision", current };
|
|
696
|
+
}
|
|
697
|
+
if (current.suspendedAt === null)
|
|
698
|
+
return { status: "ok", wasSuspended: false };
|
|
699
|
+
this.changes(`update entitlement set suspended_at = null, suspended_reason = null, revision = revision + 1, updated_at = ?
|
|
700
|
+
where tenant_id = ?`, now, tenantId);
|
|
701
|
+
return { status: "ok", wasSuspended: true };
|
|
702
|
+
});
|
|
703
|
+
if (result.status === "ok" && result.wasSuspended)
|
|
704
|
+
this.notifyEntitlementChange(tenantId);
|
|
705
|
+
return result;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Set or replace the operator grant: access until `expiresAt`, whatever the
|
|
709
|
+
* billing state, unless suspended. The cutoff must lie after the store clock
|
|
710
|
+
* and at most `MAX_OPERATOR_GRANT_MS` beyond it, so a grant is never perpetual.
|
|
711
|
+
*/
|
|
712
|
+
grantTenantAccess(input) {
|
|
713
|
+
if (!OPERATOR_REASON_PATTERN.test(input.reason))
|
|
714
|
+
throw new Error("grant reason must be 1-200 printable ASCII characters");
|
|
715
|
+
if (!Number.isSafeInteger(input.expiresAt) || input.expiresAt <= 0) {
|
|
716
|
+
throw new Error("grant expiry must be positive UTC milliseconds");
|
|
717
|
+
}
|
|
718
|
+
const result = this.transaction((now) => {
|
|
719
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
720
|
+
if (row === undefined)
|
|
721
|
+
return { status: "not_found" };
|
|
722
|
+
const current = entitlementFrom(row);
|
|
723
|
+
if (input.expectedRevision !== undefined && current.revision !== input.expectedRevision) {
|
|
724
|
+
return { status: "stale_revision", current };
|
|
725
|
+
}
|
|
726
|
+
if (input.expiresAt <= now || input.expiresAt > now + MAX_OPERATOR_GRANT_MS)
|
|
727
|
+
return { status: "invalid_expiry", now };
|
|
728
|
+
this.changes(`update entitlement set grant_expires_at = ?, grant_reason = ?, grant_updated_at = ?, revision = revision + 1,
|
|
729
|
+
updated_at = ?
|
|
730
|
+
where tenant_id = ?`, input.expiresAt, input.reason, now, now, input.tenantId);
|
|
731
|
+
return { status: "ok", entitlement: this.entitlementInTransaction(input.tenantId) };
|
|
732
|
+
});
|
|
733
|
+
if (result.status === "ok")
|
|
734
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
/** Remove the operator grant. Access falls back to the purchase at once. */
|
|
738
|
+
revokeTenantGrant(input) {
|
|
739
|
+
const result = this.transaction((now) => {
|
|
740
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
741
|
+
if (row === undefined)
|
|
742
|
+
return { status: "not_found" };
|
|
743
|
+
const current = entitlementFrom(row);
|
|
744
|
+
if (input.expectedRevision !== undefined && current.revision !== input.expectedRevision) {
|
|
745
|
+
return { status: "stale_revision", current };
|
|
746
|
+
}
|
|
747
|
+
if (current.grantExpiresAt === null)
|
|
748
|
+
return { status: "ok", entitlement: current, hadGrant: false };
|
|
749
|
+
this.changes(`update entitlement set grant_expires_at = null, grant_reason = null, grant_updated_at = ?, revision = revision + 1,
|
|
750
|
+
updated_at = ?
|
|
751
|
+
where tenant_id = ?`, now, now, input.tenantId);
|
|
752
|
+
return { status: "ok", entitlement: this.entitlementInTransaction(input.tenantId), hadGrant: true };
|
|
753
|
+
});
|
|
754
|
+
if (result.status === "ok" && result.hadGrant)
|
|
755
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
756
|
+
return result;
|
|
757
|
+
}
|
|
758
|
+
entitlementInTransaction(tenantId) {
|
|
759
|
+
const row = this.get("select * from entitlement where tenant_id = ?", tenantId);
|
|
760
|
+
if (row === undefined)
|
|
761
|
+
throw new Error("entitlement vanished inside its transaction");
|
|
762
|
+
return entitlementFrom(row);
|
|
763
|
+
}
|
|
764
|
+
/** Every workspace with its owner, live workstation count, and entitlement, read at one store time. */
|
|
765
|
+
listTenantsForOperator() {
|
|
766
|
+
return this.read((now) => ({
|
|
767
|
+
now,
|
|
768
|
+
tenants: this.all(`select t.id as t_id, t.created_at as t_created_at, m.account_id as m_account_id,
|
|
769
|
+
(select count(*) from companion c where c.tenant_id = t.id and c.revoked_at is null) as live_workstations,
|
|
770
|
+
e.*
|
|
771
|
+
from tenant t
|
|
772
|
+
left join membership m on m.tenant_id = t.id and m.role = 'owner'
|
|
773
|
+
left join entitlement e on e.tenant_id = t.id
|
|
774
|
+
order by t.created_at, t.id`).map((row) => ({
|
|
775
|
+
tenantId: text(row, "t_id"),
|
|
776
|
+
createdAt: integer(row, "t_created_at"),
|
|
777
|
+
ownerAccountId: optionalText(row, "m_account_id"),
|
|
778
|
+
liveWorkstations: integer(row, "live_workstations"),
|
|
779
|
+
entitlement: row.tenant_id === null ? null : entitlementFrom(row),
|
|
780
|
+
})),
|
|
781
|
+
}));
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Apply a past-due purchase in one write: the stored revision and complete
|
|
785
|
+
* purchase binding must still equal `expected`, so an invoice of a replaced
|
|
786
|
+
* subscription cannot install grace on its replacement. Each invoice still
|
|
787
|
+
* open at the provider is an obligation; the first record of one fixes its
|
|
788
|
+
* deadline at `min(now, failedSinceMs) + graceMs` and later records keep it,
|
|
789
|
+
* including after the invoice was paid and reopened. Grace is the earliest
|
|
790
|
+
* deadline among exactly these open invoices, so a paid invoice no longer
|
|
791
|
+
* holds it and nothing extends an open one; with none open there is no grace.
|
|
792
|
+
* Every conflict is found before anything is written.
|
|
793
|
+
*/
|
|
794
|
+
applyPastDue(input) {
|
|
795
|
+
const { binding } = input.expected;
|
|
796
|
+
if (binding.environment !== "test" && binding.environment !== "live")
|
|
797
|
+
throw new Error("unknown billing environment");
|
|
798
|
+
if (binding.stripeCustomerId === null || binding.stripeSubscriptionId === null) {
|
|
799
|
+
throw new Error("a past-due purchase needs a complete purchase binding");
|
|
800
|
+
}
|
|
801
|
+
assertProviderId("stripeCustomerId", binding.stripeCustomerId);
|
|
802
|
+
assertProviderId("stripeSubscriptionId", binding.stripeSubscriptionId);
|
|
803
|
+
if (!Number.isSafeInteger(input.graceMs) || input.graceMs <= 0)
|
|
804
|
+
throw new Error("graceMs must be a positive integer");
|
|
805
|
+
const { currentPeriodEnd } = input;
|
|
806
|
+
if (currentPeriodEnd !== null && (!Number.isSafeInteger(currentPeriodEnd) || currentPeriodEnd <= 0)) {
|
|
807
|
+
throw new Error("currentPeriodEnd must be positive UTC milliseconds or null");
|
|
808
|
+
}
|
|
809
|
+
const invoices = new Map();
|
|
810
|
+
for (const invoice of input.openInvoices) {
|
|
811
|
+
assertProviderId("invoiceId", invoice.invoiceId);
|
|
812
|
+
const since = invoice.failedSinceMs;
|
|
813
|
+
if (since !== null && (!Number.isSafeInteger(since) || since <= 0))
|
|
814
|
+
throw new Error("failedSinceMs must be a positive integer");
|
|
815
|
+
if (!invoices.has(invoice.invoiceId))
|
|
816
|
+
invoices.set(invoice.invoiceId, since);
|
|
817
|
+
}
|
|
818
|
+
const result = this.transaction((now) => {
|
|
819
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
820
|
+
if (row === undefined)
|
|
821
|
+
return { status: "not_found" };
|
|
822
|
+
const current = entitlementFrom(row);
|
|
823
|
+
if (current.revision !== input.expected.revision)
|
|
824
|
+
return { status: "stale_revision", current };
|
|
825
|
+
if (!sameBinding(current, binding))
|
|
826
|
+
return { status: "binding_mismatch", current };
|
|
827
|
+
const deadlines = new Map();
|
|
828
|
+
const missing = [];
|
|
829
|
+
for (const [invoiceId, since] of invoices) {
|
|
830
|
+
const obligation = this.get(`select tenant_id, stripe_customer_id, stripe_subscription_id, grace_deadline
|
|
831
|
+
from billing_grace_obligation where environment = ? and invoice_id = ?`, binding.environment, invoiceId);
|
|
832
|
+
if (obligation === undefined) {
|
|
833
|
+
const firstFailedAt = since === null ? now : Math.min(now, since);
|
|
834
|
+
const deadline = expiryAfter(firstFailedAt, input.graceMs);
|
|
835
|
+
if (!Number.isSafeInteger(deadline))
|
|
836
|
+
throw new Error("grace deadline overflows");
|
|
837
|
+
missing.push({ invoiceId, firstFailedAt, deadline });
|
|
838
|
+
deadlines.set(invoiceId, deadline);
|
|
839
|
+
}
|
|
840
|
+
else if (text(obligation, "tenant_id") !== input.tenantId ||
|
|
841
|
+
text(obligation, "stripe_customer_id") !== binding.stripeCustomerId ||
|
|
842
|
+
text(obligation, "stripe_subscription_id") !== binding.stripeSubscriptionId) {
|
|
843
|
+
return { status: "binding_mismatch", current };
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
deadlines.set(invoiceId, integer(obligation, "grace_deadline"));
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
for (const obligation of missing) {
|
|
850
|
+
this.changes(`insert into billing_grace_obligation
|
|
851
|
+
(environment, invoice_id, tenant_id, stripe_customer_id, stripe_subscription_id, first_failed_at, grace_deadline)
|
|
852
|
+
values (?, ?, ?, ?, ?, ?, ?)`, binding.environment, obligation.invoiceId, input.tenantId, binding.stripeCustomerId, binding.stripeSubscriptionId, obligation.firstFailedAt, obligation.deadline);
|
|
853
|
+
}
|
|
854
|
+
let grace = null;
|
|
855
|
+
for (const [invoiceId, deadline] of deadlines) {
|
|
856
|
+
if (grace === null || deadline < grace.deadline || (deadline === grace.deadline && invoiceId < grace.invoiceId)) {
|
|
857
|
+
grace = { invoiceId, deadline };
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
this.changes(`update entitlement set subscription_state = 'past_due', current_period_end = ?,
|
|
861
|
+
grace_invoice_id = ?, grace_deadline = ?, last_reconciled_at = ?, revision = revision + 1, updated_at = ?
|
|
862
|
+
where tenant_id = ? and revision = ?`, currentPeriodEnd, grace?.invoiceId ?? null, grace?.deadline ?? null, now, now, input.tenantId, current.revision);
|
|
863
|
+
const updated = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
864
|
+
if (updated === undefined)
|
|
865
|
+
throw new Error("entitlement vanished inside its transaction");
|
|
866
|
+
return { status: "ok", entitlement: entitlementFrom(updated) };
|
|
867
|
+
});
|
|
868
|
+
if (result.status === "ok")
|
|
869
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
870
|
+
return result;
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* The billing write path. Applies only when the stored revision and purchase
|
|
874
|
+
* binding still equal what the caller compared against provider state, so a
|
|
875
|
+
* stale fetch or an old subscription cannot overwrite its replacement. The
|
|
876
|
+
* caller fetches provider state outside any transaction and, on a stale
|
|
877
|
+
* result, fetches again. Never touches suspension or device credentials.
|
|
878
|
+
*/
|
|
879
|
+
applyEntitlementUpdate(input) {
|
|
880
|
+
assertEntitlementUpdate(input.update);
|
|
881
|
+
const result = this.transaction((now) => {
|
|
882
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
883
|
+
if (row === undefined)
|
|
884
|
+
return { status: "not_found" };
|
|
885
|
+
const current = entitlementFrom(row);
|
|
886
|
+
if (current.revision !== input.expected.revision)
|
|
887
|
+
return { status: "stale_revision", current };
|
|
888
|
+
if (!sameBinding(current, input.expected.binding))
|
|
889
|
+
return { status: "binding_mismatch", current };
|
|
890
|
+
const { binding } = input.update;
|
|
891
|
+
const clearGrace = input.update.grace === "clear" || !sameBinding(current, binding);
|
|
892
|
+
try {
|
|
893
|
+
this.changes(`update entitlement set environment = ?, stripe_customer_id = ?, stripe_subscription_id = ?,
|
|
894
|
+
subscription_state = ?, current_period_end = ?,
|
|
895
|
+
grace_invoice_id = case when ? then null else grace_invoice_id end,
|
|
896
|
+
grace_deadline = case when ? then null else grace_deadline end,
|
|
897
|
+
last_reconciled_at = ?, revision = revision + 1, updated_at = ?
|
|
898
|
+
where tenant_id = ? and revision = ?`, binding.environment, binding.stripeCustomerId, binding.stripeSubscriptionId, input.update.subscriptionState, input.update.currentPeriodEnd, clearGrace ? 1 : 0, clearGrace ? 1 : 0, now, now, input.tenantId, current.revision);
|
|
899
|
+
}
|
|
900
|
+
catch (error) {
|
|
901
|
+
if (isUniqueViolation(error))
|
|
902
|
+
return { status: "binding_conflict" };
|
|
903
|
+
throw error;
|
|
904
|
+
}
|
|
905
|
+
const updated = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
906
|
+
if (updated === undefined)
|
|
907
|
+
throw new Error("entitlement vanished inside its transaction");
|
|
908
|
+
return { status: "ok", entitlement: entitlementFrom(updated) };
|
|
909
|
+
});
|
|
910
|
+
if (result.status === "ok")
|
|
911
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
912
|
+
return result;
|
|
913
|
+
}
|
|
914
|
+
/** How many tenants are bound to a provider customer outside `environment`. */
|
|
915
|
+
countForeignEnvironmentBindings(environment) {
|
|
916
|
+
assertBillingEnvironment(environment);
|
|
917
|
+
return this.guard(() => {
|
|
918
|
+
const row = this.get("select count(*) as n from entitlement where stripe_customer_id is not null and environment <> ?", environment);
|
|
919
|
+
return integer(row ?? {}, "n");
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
/** How many tenants hold a purchase in a state that `evaluateEntitlementAccess` can grant on. */
|
|
923
|
+
countAccessGrantingPurchases() {
|
|
924
|
+
return this.guard(() => {
|
|
925
|
+
const row = this.get("select count(*) as n from entitlement where subscription_state in ('active','trialing','past_due')");
|
|
926
|
+
return integer(row ?? {}, "n");
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
/** The entitlement bound to a provider customer in one billing environment. */
|
|
930
|
+
findEntitlementByCustomer(environment, stripeCustomerId) {
|
|
931
|
+
return this.guard(() => {
|
|
932
|
+
const row = this.get("select * from entitlement where environment = ? and stripe_customer_id = ?", environment, stripeCustomerId);
|
|
933
|
+
return row === undefined ? null : entitlementFrom(row);
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
/** The tenant's entitlement, read under the owner's current authority. */
|
|
937
|
+
readBillingAccount(input) {
|
|
938
|
+
return this.transaction((now) => {
|
|
939
|
+
if (!this.tenantOwnerAuthorizes(input.tenantId, input.actor, now))
|
|
940
|
+
return { status: "unauthorized" };
|
|
941
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
942
|
+
if (row === undefined)
|
|
943
|
+
return { status: "not_found" };
|
|
944
|
+
return { status: "ok", entitlement: entitlementFrom(row) };
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Store the provider customer for a tenant that has none, under the owner's
|
|
949
|
+
* current authority and the revision the caller read before creating the
|
|
950
|
+
* customer. Grants nothing: the subscription state is left unchanged.
|
|
951
|
+
*/
|
|
952
|
+
bindBillingCustomer(input) {
|
|
953
|
+
assertBillingEnvironment(input.environment);
|
|
954
|
+
if (input.stripeCustomerId.length === 0)
|
|
955
|
+
throw new Error("stripeCustomerId is required");
|
|
956
|
+
assertProviderId("stripeCustomerId", input.stripeCustomerId);
|
|
957
|
+
const result = this.transaction((now) => {
|
|
958
|
+
if (!this.tenantOwnerAuthorizes(input.tenantId, input.actor, now))
|
|
959
|
+
return { status: "unauthorized" };
|
|
960
|
+
const row = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
961
|
+
if (row === undefined)
|
|
962
|
+
return { status: "not_found" };
|
|
963
|
+
const current = entitlementFrom(row);
|
|
964
|
+
if (current.revision !== input.expectedRevision || current.stripeCustomerId !== null) {
|
|
965
|
+
return { status: "stale_revision", current };
|
|
966
|
+
}
|
|
967
|
+
try {
|
|
968
|
+
this.changes(`update entitlement set environment = ?, stripe_customer_id = ?, revision = revision + 1, updated_at = ?
|
|
969
|
+
where tenant_id = ? and revision = ?`, input.environment, input.stripeCustomerId, now, input.tenantId, current.revision);
|
|
970
|
+
}
|
|
971
|
+
catch (error) {
|
|
972
|
+
if (isUniqueViolation(error))
|
|
973
|
+
return { status: "binding_conflict" };
|
|
974
|
+
throw error;
|
|
975
|
+
}
|
|
976
|
+
const updated = this.get("select * from entitlement where tenant_id = ?", input.tenantId);
|
|
977
|
+
if (updated === undefined)
|
|
978
|
+
throw new Error("entitlement vanished inside its transaction");
|
|
979
|
+
return { status: "ok", entitlement: entitlementFrom(updated) };
|
|
980
|
+
});
|
|
981
|
+
if (result.status === "ok")
|
|
982
|
+
this.notifyEntitlementChange(input.tenantId);
|
|
983
|
+
return result;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Durably record a verified webhook event and, when it names a customer, its
|
|
987
|
+
* reconciliation job, in one transaction. A repeated event ID records nothing.
|
|
988
|
+
* The caller acknowledges the webhook only after this returns.
|
|
989
|
+
*/
|
|
990
|
+
recordBillingEvent(input) {
|
|
991
|
+
assertBillingEnvironment(input.environment);
|
|
992
|
+
if (!/^evt_[A-Za-z0-9]{1,255}$/.test(input.eventId))
|
|
993
|
+
throw new Error("invalid billing event ID");
|
|
994
|
+
if (input.eventType.length < 1 || input.eventType.length > 255)
|
|
995
|
+
throw new Error("invalid billing event type");
|
|
996
|
+
const trigger = input.trigger;
|
|
997
|
+
if (trigger !== null) {
|
|
998
|
+
assertProviderId("stripeCustomerId", trigger.stripeCustomerId);
|
|
999
|
+
assertProviderId("stripeSubscriptionId", trigger.stripeSubscriptionId);
|
|
1000
|
+
assertProviderId("stripeInvoiceId", trigger.stripeInvoiceId);
|
|
1001
|
+
}
|
|
1002
|
+
return this.transaction((now) => {
|
|
1003
|
+
if (this.get("select 1 from billing_event_receipt where environment = ? and event_id = ?", input.environment, input.eventId)) {
|
|
1004
|
+
return { status: "duplicate" };
|
|
1005
|
+
}
|
|
1006
|
+
this.changes("insert into billing_event_receipt (environment, event_id, event_type, received_at) values (?, ?, ?, ?)", input.environment, input.eventId, input.eventType, now);
|
|
1007
|
+
if (trigger === null)
|
|
1008
|
+
return { status: "recorded", jobId: null };
|
|
1009
|
+
const tenant = trigger.stripeCustomerId === null
|
|
1010
|
+
? undefined
|
|
1011
|
+
: this.get("select tenant_id from entitlement where environment = ? and stripe_customer_id = ?", input.environment, trigger.stripeCustomerId);
|
|
1012
|
+
const jobId = generateControlId("bjob");
|
|
1013
|
+
this.changes(`insert into billing_job (id, environment, trigger, event_id, tenant_id, stripe_customer_id, stripe_subscription_id,
|
|
1014
|
+
stripe_invoice_id, next_attempt_at, created_at, updated_at)
|
|
1015
|
+
values (?, ?, 'webhook', ?, ?, ?, ?, ?, ?, ?, ?)`, jobId, input.environment, input.eventId, tenant === undefined ? null : text(tenant, "tenant_id"), trigger.stripeCustomerId, trigger.stripeSubscriptionId, trigger.stripeInvoiceId, now, now, now);
|
|
1016
|
+
return { status: "recorded", jobId };
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Queue one repair job for every entitlement bound to a customer in this
|
|
1021
|
+
* environment, whether or not any webhook arrived. A tenant that already has
|
|
1022
|
+
* an unfinished repair job gets no second one. Returns how many were queued.
|
|
1023
|
+
*/
|
|
1024
|
+
enqueueBillingSweep(input) {
|
|
1025
|
+
assertBillingEnvironment(input.environment);
|
|
1026
|
+
return this.transaction((now) => {
|
|
1027
|
+
const tenants = this.all(`select e.tenant_id, e.stripe_customer_id from entitlement e
|
|
1028
|
+
where e.environment = ? and e.stripe_customer_id is not null
|
|
1029
|
+
and not exists (
|
|
1030
|
+
select 1 from billing_job j
|
|
1031
|
+
where j.tenant_id = e.tenant_id and j.environment = e.environment
|
|
1032
|
+
and j.trigger in ('startup','periodic') and j.state in ('pending','running'))
|
|
1033
|
+
order by e.tenant_id`, input.environment);
|
|
1034
|
+
for (const row of tenants) {
|
|
1035
|
+
this.changes(`insert into billing_job (id, environment, trigger, tenant_id, stripe_customer_id, next_attempt_at, created_at, updated_at)
|
|
1036
|
+
values (?, ?, ?, ?, ?, ?, ?, ?)`, generateControlId("bjob"), input.environment, input.trigger, text(row, "tenant_id"), text(row, "stripe_customer_id"), now, now, now);
|
|
1037
|
+
}
|
|
1038
|
+
return tenants.length;
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
/** Mark the next due job running and count the attempt; `null` when nothing is due. */
|
|
1042
|
+
claimBillingJob(environment) {
|
|
1043
|
+
assertBillingEnvironment(environment);
|
|
1044
|
+
return this.transaction((now) => {
|
|
1045
|
+
const row = this.get(`select id from billing_job where environment = ? and state = 'pending' and next_attempt_at <= ?
|
|
1046
|
+
order by next_attempt_at, created_at, id limit 1`, environment, now);
|
|
1047
|
+
if (row === undefined)
|
|
1048
|
+
return null;
|
|
1049
|
+
const id = text(row, "id");
|
|
1050
|
+
this.changes("update billing_job set state = 'running', attempts = attempts + 1, updated_at = ? where id = ? and state = 'pending'", now, id);
|
|
1051
|
+
const claimed = this.get("select * from billing_job where id = ?", id);
|
|
1052
|
+
if (claimed === undefined)
|
|
1053
|
+
throw new Error("billing job vanished inside its transaction");
|
|
1054
|
+
return billingJobFrom(claimed);
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
completeBillingJob(id) {
|
|
1058
|
+
return this.transaction((now) => this.changes("update billing_job set state = 'succeeded', last_error = null, updated_at = ? where id = ? and state = 'running'", now, id) === 1);
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Record a failed attempt. With a retry delay the job returns to `pending`
|
|
1062
|
+
* at `now + retryDelayMs`; without one it is terminally `failed`.
|
|
1063
|
+
*/
|
|
1064
|
+
failBillingJob(input) {
|
|
1065
|
+
if (input.retryDelayMs !== null && (!Number.isSafeInteger(input.retryDelayMs) || input.retryDelayMs < 0)) {
|
|
1066
|
+
throw new Error("retryDelayMs must be a non-negative integer");
|
|
1067
|
+
}
|
|
1068
|
+
const error = input.error.slice(0, MAX_BILLING_JOB_ERROR_LENGTH);
|
|
1069
|
+
return this.transaction((now) => {
|
|
1070
|
+
if (input.retryDelayMs === null) {
|
|
1071
|
+
return (this.changes("update billing_job set state = 'failed', last_error = ?, updated_at = ? where id = ? and state = 'running'", error, now, input.id) === 1);
|
|
1072
|
+
}
|
|
1073
|
+
return (this.changes(`update billing_job set state = 'pending', last_error = ?, next_attempt_at = ?, updated_at = ?
|
|
1074
|
+
where id = ? and state = 'running'`, error, now + input.retryDelayMs, now, input.id) === 1);
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Return `running` jobs other than `exceptJobId` to `pending`, due now: jobs
|
|
1079
|
+
* left by a process that stopped mid-job, or whose durable record failed.
|
|
1080
|
+
* Only the single serialized worker may call this.
|
|
1081
|
+
*/
|
|
1082
|
+
requeueRunningBillingJobs(environment, exceptJobId = null) {
|
|
1083
|
+
assertBillingEnvironment(environment);
|
|
1084
|
+
return this.transaction((now) => this.changes(`update billing_job set state = 'pending', next_attempt_at = ?, updated_at = ?
|
|
1085
|
+
where environment = ? and state = 'running' and id is not ?`, now, now, environment, exceptJobId));
|
|
1086
|
+
}
|
|
1087
|
+
/** UTC milliseconds of the earliest pending job, or `null`. */
|
|
1088
|
+
nextBillingJobAt(environment) {
|
|
1089
|
+
return this.guard(() => {
|
|
1090
|
+
const row = this.get("select min(next_attempt_at) as next_at from billing_job where environment = ? and state = 'pending'", environment);
|
|
1091
|
+
return row === undefined ? null : optionalInteger(row, "next_at");
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
listBillingJobs(environment) {
|
|
1095
|
+
return this.guard(() => this.all("select * from billing_job where environment = ? order by created_at, id", environment).map(billingJobFrom));
|
|
1096
|
+
}
|
|
1097
|
+
/** Delete succeeded jobs last updated more than `retentionMs` ago. Event receipts are kept for duplicate detection. */
|
|
1098
|
+
pruneBillingJobs(retentionMs) {
|
|
1099
|
+
if (!Number.isSafeInteger(retentionMs) || retentionMs <= 0)
|
|
1100
|
+
throw new Error("retentionMs must be a positive integer");
|
|
1101
|
+
return this.transaction((now) => this.changes("delete from billing_job where state = 'succeeded' and updated_at < ?", now - retentionMs));
|
|
1102
|
+
}
|
|
1103
|
+
/** Delete pending logins at their expiry, and account sessions that expired more than `retentionMs` ago. */
|
|
1104
|
+
pruneExpired(retentionMs = 7 * 24 * 60 * 60 * 1000) {
|
|
1105
|
+
return this.transaction((now) => {
|
|
1106
|
+
const cutoff = now - retentionMs;
|
|
1107
|
+
return (this.changes("delete from pending_login where expires_at <= ?", now) +
|
|
1108
|
+
this.changes("delete from account_session where expires_at < ?", cutoff));
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
export function openControlPlaneStore(options) {
|
|
1113
|
+
return ControlPlaneStore.open(options);
|
|
1114
|
+
}
|
|
1115
|
+
//# sourceMappingURL=store.js.map
|