mayura 0.1.0-dev.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/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +55 -0
- package/lib/adapter-code-docker/dist/index.d.ts +65 -0
- package/lib/adapter-code-docker/dist/index.d.ts.map +1 -0
- package/lib/adapter-code-docker/dist/index.js +228 -0
- package/lib/adapter-code-docker/dist/index.js.map +1 -0
- package/lib/adapter-code-docker/image/Dockerfile +12 -0
- package/lib/adapter-code-docker/package.json +5 -0
- package/lib/adapter-code-docker/src/index.ts +244 -0
- package/lib/adapter-code-quickjs/dist/index.d.ts +40 -0
- package/lib/adapter-code-quickjs/dist/index.d.ts.map +1 -0
- package/lib/adapter-code-quickjs/dist/index.js +169 -0
- package/lib/adapter-code-quickjs/dist/index.js.map +1 -0
- package/lib/adapter-code-quickjs/dist/worker.d.ts +2 -0
- package/lib/adapter-code-quickjs/dist/worker.d.ts.map +1 -0
- package/lib/adapter-code-quickjs/dist/worker.js +164 -0
- package/lib/adapter-code-quickjs/dist/worker.js.map +1 -0
- package/lib/adapter-code-quickjs/package.json +5 -0
- package/lib/adapter-code-quickjs/src/index.ts +174 -0
- package/lib/adapter-code-quickjs/src/worker.ts +142 -0
- package/lib/adapter-mcp/dist/index.d.ts +29 -0
- package/lib/adapter-mcp/dist/index.d.ts.map +1 -0
- package/lib/adapter-mcp/dist/index.js +54 -0
- package/lib/adapter-mcp/dist/index.js.map +1 -0
- package/lib/adapter-mcp/package.json +5 -0
- package/lib/adapter-mcp/src/index.ts +82 -0
- package/lib/artifacts/dist/contracts.d.ts +139 -0
- package/lib/artifacts/dist/contracts.d.ts.map +1 -0
- package/lib/artifacts/dist/contracts.js +2 -0
- package/lib/artifacts/dist/contracts.js.map +1 -0
- package/lib/artifacts/dist/index.d.ts +3 -0
- package/lib/artifacts/dist/index.d.ts.map +1 -0
- package/lib/artifacts/dist/index.js +2 -0
- package/lib/artifacts/dist/index.js.map +1 -0
- package/lib/artifacts/dist/local-store.d.ts +3 -0
- package/lib/artifacts/dist/local-store.d.ts.map +1 -0
- package/lib/artifacts/dist/local-store.js +922 -0
- package/lib/artifacts/dist/local-store.js.map +1 -0
- package/lib/artifacts/package.json +5 -0
- package/lib/artifacts/src/contracts.ts +159 -0
- package/lib/artifacts/src/index.ts +26 -0
- package/lib/artifacts/src/local-store.ts +816 -0
- package/lib/cli/dist/application.d.ts +85 -0
- package/lib/cli/dist/application.d.ts.map +1 -0
- package/lib/cli/dist/application.js +116 -0
- package/lib/cli/dist/application.js.map +1 -0
- package/lib/cli/dist/bin.d.ts +3 -0
- package/lib/cli/dist/bin.d.ts.map +1 -0
- package/lib/cli/dist/bin.js +394 -0
- package/lib/cli/dist/bin.js.map +1 -0
- package/lib/cli/dist/dev.d.ts +21 -0
- package/lib/cli/dist/dev.d.ts.map +1 -0
- package/lib/cli/dist/dev.js +205 -0
- package/lib/cli/dist/dev.js.map +1 -0
- package/lib/cli/dist/index.d.ts +72 -0
- package/lib/cli/dist/index.d.ts.map +1 -0
- package/lib/cli/dist/index.js +423 -0
- package/lib/cli/dist/index.js.map +1 -0
- package/lib/cli/dist/interactive.d.ts +19 -0
- package/lib/cli/dist/interactive.d.ts.map +1 -0
- package/lib/cli/dist/interactive.js +163 -0
- package/lib/cli/dist/interactive.js.map +1 -0
- package/lib/cli/dist/operations.d.ts +215 -0
- package/lib/cli/dist/operations.d.ts.map +1 -0
- package/lib/cli/dist/operations.js +580 -0
- package/lib/cli/dist/operations.js.map +1 -0
- package/lib/cli/dist/output.d.ts +29 -0
- package/lib/cli/dist/output.d.ts.map +1 -0
- package/lib/cli/dist/output.js +244 -0
- package/lib/cli/dist/output.js.map +1 -0
- package/lib/cli/dist/providers.d.ts +54 -0
- package/lib/cli/dist/providers.d.ts.map +1 -0
- package/lib/cli/dist/providers.js +96 -0
- package/lib/cli/dist/providers.js.map +1 -0
- package/lib/cli/package.json +5 -0
- package/lib/cli/src/application.ts +109 -0
- package/lib/cli/src/bin.ts +261 -0
- package/lib/cli/src/dev.ts +139 -0
- package/lib/cli/src/index.ts +386 -0
- package/lib/cli/src/interactive.ts +132 -0
- package/lib/cli/src/operations.ts +458 -0
- package/lib/cli/src/output.ts +205 -0
- package/lib/cli/src/providers.ts +102 -0
- package/lib/cli/starters/approval-workflow/Dockerfile +20 -0
- package/lib/cli/starters/approval-workflow/README.md +102 -0
- package/lib/cli/starters/approval-workflow/compose.yaml +33 -0
- package/lib/cli/starters/approval-workflow/dot-dockerignore +7 -0
- package/lib/cli/starters/approval-workflow/dot-env.example +41 -0
- package/lib/cli/starters/approval-workflow/dot-github/workflows/ci.yml +13 -0
- package/lib/cli/starters/approval-workflow/dot-gitignore +10 -0
- package/lib/cli/starters/approval-workflow/mayura.project.json +16 -0
- package/lib/cli/starters/approval-workflow/package.json +30 -0
- package/lib/cli/starters/approval-workflow/src/app.ts +19 -0
- package/lib/cli/starters/approval-workflow/src/auth.ts +68 -0
- package/lib/cli/starters/approval-workflow/src/config.ts +107 -0
- package/lib/cli/starters/approval-workflow/src/dev.ts +59 -0
- package/lib/cli/starters/approval-workflow/src/intake.ts +104 -0
- package/lib/cli/starters/approval-workflow/src/model.ts +33 -0
- package/lib/cli/starters/approval-workflow/src/server.ts +56 -0
- package/lib/cli/starters/approval-workflow/src/services.ts +26 -0
- package/lib/cli/starters/approval-workflow/src/token.ts +6 -0
- package/lib/cli/starters/approval-workflow/src/worker.ts +18 -0
- package/lib/cli/starters/approval-workflow/src/workflow.ts +100 -0
- package/lib/cli/starters/approval-workflow/test/refunds.test.ts +140 -0
- package/lib/cli/starters/approval-workflow/tsconfig.json +20 -0
- package/lib/cli/starters/event-automation/Dockerfile +21 -0
- package/lib/cli/starters/event-automation/README.md +150 -0
- package/lib/cli/starters/event-automation/compose.yaml +41 -0
- package/lib/cli/starters/event-automation/dot-dockerignore +7 -0
- package/lib/cli/starters/event-automation/dot-env.example +51 -0
- package/lib/cli/starters/event-automation/dot-github/workflows/ci.yml +13 -0
- package/lib/cli/starters/event-automation/dot-gitignore +10 -0
- package/lib/cli/starters/event-automation/mayura.project.json +17 -0
- package/lib/cli/starters/event-automation/package.json +32 -0
- package/lib/cli/starters/event-automation/src/app.ts +19 -0
- package/lib/cli/starters/event-automation/src/auth.ts +61 -0
- package/lib/cli/starters/event-automation/src/config.ts +138 -0
- package/lib/cli/starters/event-automation/src/dev.ts +54 -0
- package/lib/cli/starters/event-automation/src/ingress.ts +129 -0
- package/lib/cli/starters/event-automation/src/mcp.ts +139 -0
- package/lib/cli/starters/event-automation/src/model.ts +33 -0
- package/lib/cli/starters/event-automation/src/send-sample.ts +29 -0
- package/lib/cli/starters/event-automation/src/server.ts +58 -0
- package/lib/cli/starters/event-automation/src/services.ts +54 -0
- package/lib/cli/starters/event-automation/src/signing.ts +30 -0
- package/lib/cli/starters/event-automation/src/token.ts +7 -0
- package/lib/cli/starters/event-automation/src/tracker/main.ts +18 -0
- package/lib/cli/starters/event-automation/src/tracker/mcp-server.ts +145 -0
- package/lib/cli/starters/event-automation/src/tracker/samples.ts +19 -0
- package/lib/cli/starters/event-automation/src/tracker/tickets.ts +54 -0
- package/lib/cli/starters/event-automation/src/tracker-tools.ts +38 -0
- package/lib/cli/starters/event-automation/src/triage.ts +151 -0
- package/lib/cli/starters/event-automation/src/worker.ts +19 -0
- package/lib/cli/starters/event-automation/src/workflows.ts +57 -0
- package/lib/cli/starters/event-automation/test/automation.test.ts +200 -0
- package/lib/cli/starters/event-automation/tsconfig.json +20 -0
- package/lib/cli/starters/research-team/Dockerfile +22 -0
- package/lib/cli/starters/research-team/README.md +189 -0
- package/lib/cli/starters/research-team/compose.yaml +38 -0
- package/lib/cli/starters/research-team/dot-dockerignore +7 -0
- package/lib/cli/starters/research-team/dot-env.example +49 -0
- package/lib/cli/starters/research-team/dot-github/workflows/ci.yml +13 -0
- package/lib/cli/starters/research-team/dot-gitignore +10 -0
- package/lib/cli/starters/research-team/mayura.project.json +22 -0
- package/lib/cli/starters/research-team/package.json +30 -0
- package/lib/cli/starters/research-team/src/app.ts +19 -0
- package/lib/cli/starters/research-team/src/auth.ts +56 -0
- package/lib/cli/starters/research-team/src/config.ts +170 -0
- package/lib/cli/starters/research-team/src/desk.ts +124 -0
- package/lib/cli/starters/research-team/src/dev.ts +58 -0
- package/lib/cli/starters/research-team/src/library/corpus.ts +76 -0
- package/lib/cli/starters/research-team/src/library/index.ts +109 -0
- package/lib/cli/starters/research-team/src/model.ts +33 -0
- package/lib/cli/starters/research-team/src/server.ts +69 -0
- package/lib/cli/starters/research-team/src/services.ts +48 -0
- package/lib/cli/starters/research-team/src/team.ts +173 -0
- package/lib/cli/starters/research-team/src/telemetry.ts +95 -0
- package/lib/cli/starters/research-team/src/token.ts +6 -0
- package/lib/cli/starters/research-team/src/worker.ts +21 -0
- package/lib/cli/starters/research-team/src/workflow.ts +197 -0
- package/lib/cli/starters/research-team/test/research.test.ts +271 -0
- package/lib/cli/starters/research-team/tsconfig.json +20 -0
- package/lib/cli/starters/support-agent/Dockerfile +21 -0
- package/lib/cli/starters/support-agent/README.md +128 -0
- package/lib/cli/starters/support-agent/compose.yaml +33 -0
- package/lib/cli/starters/support-agent/dot-dockerignore +7 -0
- package/lib/cli/starters/support-agent/dot-env.example +44 -0
- package/lib/cli/starters/support-agent/dot-github/workflows/ci.yml +13 -0
- package/lib/cli/starters/support-agent/dot-gitignore +10 -0
- package/lib/cli/starters/support-agent/mayura.project.json +18 -0
- package/lib/cli/starters/support-agent/package.json +43 -0
- package/lib/cli/starters/support-agent/src/app.ts +19 -0
- package/lib/cli/starters/support-agent/src/assistant.ts +313 -0
- package/lib/cli/starters/support-agent/src/auth.ts +69 -0
- package/lib/cli/starters/support-agent/src/config.ts +120 -0
- package/lib/cli/starters/support-agent/src/dev.ts +115 -0
- package/lib/cli/starters/support-agent/src/guardrails.ts +71 -0
- package/lib/cli/starters/support-agent/src/model.ts +33 -0
- package/lib/cli/starters/support-agent/src/orders.ts +62 -0
- package/lib/cli/starters/support-agent/src/returns.ts +86 -0
- package/lib/cli/starters/support-agent/src/server.ts +72 -0
- package/lib/cli/starters/support-agent/src/services.ts +32 -0
- package/lib/cli/starters/support-agent/src/session.ts +55 -0
- package/lib/cli/starters/support-agent/src/token.ts +9 -0
- package/lib/cli/starters/support-agent/src/worker.ts +19 -0
- package/lib/cli/starters/support-agent/test/support.test.ts +263 -0
- package/lib/cli/starters/support-agent/tsconfig.json +20 -0
- package/lib/cli/starters/support-agent/web/index.html +12 -0
- package/lib/cli/starters/support-agent/web/src/App.tsx +43 -0
- package/lib/cli/starters/support-agent/web/src/components/chat.tsx +161 -0
- package/lib/cli/starters/support-agent/web/src/components/ui/button.tsx +33 -0
- package/lib/cli/starters/support-agent/web/src/components/ui/primitives.tsx +48 -0
- package/lib/cli/starters/support-agent/web/src/env.d.ts +1 -0
- package/lib/cli/starters/support-agent/web/src/lib/api.ts +56 -0
- package/lib/cli/starters/support-agent/web/src/lib/utils.ts +4 -0
- package/lib/cli/starters/support-agent/web/src/main.tsx +6 -0
- package/lib/cli/starters/support-agent/web/src/styles.css +86 -0
- package/lib/cli/starters/support-agent/web/tsconfig.json +9 -0
- package/lib/cli/starters/support-agent/web/vite.config.ts +13 -0
- package/lib/cli/templates/basic-agent.ts +10 -0
- package/lib/cli/templates/capability-policy.ts +18 -0
- package/lib/cli/templates/code-mode-workflow.ts +34 -0
- package/lib/cli/templates/durable-approval.ts +25 -0
- package/lib/cli/templates/guarded-streaming-app.ts +23 -0
- package/lib/cli/templates/native-memory.ts +15 -0
- package/lib/cli/templates/parallel-research.ts +21 -0
- package/lib/cli/templates/typed-tool-runner.ts +15 -0
- package/lib/client/dist/forms.d.ts +77 -0
- package/lib/client/dist/forms.d.ts.map +1 -0
- package/lib/client/dist/forms.js +282 -0
- package/lib/client/dist/forms.js.map +1 -0
- package/lib/client/dist/headless.d.ts +84 -0
- package/lib/client/dist/headless.d.ts.map +1 -0
- package/lib/client/dist/headless.js +376 -0
- package/lib/client/dist/headless.js.map +1 -0
- package/lib/client/dist/index.d.ts +266 -0
- package/lib/client/dist/index.d.ts.map +1 -0
- package/lib/client/dist/index.js +814 -0
- package/lib/client/dist/index.js.map +1 -0
- package/lib/client/dist/workflows.d.ts +128 -0
- package/lib/client/dist/workflows.d.ts.map +1 -0
- package/lib/client/dist/workflows.js +379 -0
- package/lib/client/dist/workflows.js.map +1 -0
- package/lib/client/package.json +5 -0
- package/lib/client/src/forms.ts +215 -0
- package/lib/client/src/headless.ts +265 -0
- package/lib/client/src/index.ts +646 -0
- package/lib/client/src/workflows.ts +307 -0
- package/lib/client-react/dist/components.d.ts +70 -0
- package/lib/client-react/dist/components.d.ts.map +1 -0
- package/lib/client-react/dist/components.js +173 -0
- package/lib/client-react/dist/components.js.map +1 -0
- package/lib/client-react/dist/index.d.ts +29 -0
- package/lib/client-react/dist/index.d.ts.map +1 -0
- package/lib/client-react/dist/index.js +57 -0
- package/lib/client-react/dist/index.js.map +1 -0
- package/lib/client-react/package.json +5 -0
- package/lib/client-react/src/components.ts +183 -0
- package/lib/client-react/src/index.ts +70 -0
- package/lib/code-mode/dist/index.d.ts +142 -0
- package/lib/code-mode/dist/index.d.ts.map +1 -0
- package/lib/code-mode/dist/index.js +498 -0
- package/lib/code-mode/dist/index.js.map +1 -0
- package/lib/code-mode/package.json +5 -0
- package/lib/code-mode/src/index.ts +605 -0
- package/lib/code-mode-workflows/dist/index.d.ts +51 -0
- package/lib/code-mode-workflows/dist/index.d.ts.map +1 -0
- package/lib/code-mode-workflows/dist/index.js +231 -0
- package/lib/code-mode-workflows/dist/index.js.map +1 -0
- package/lib/code-mode-workflows/package.json +5 -0
- package/lib/code-mode-workflows/src/index.ts +259 -0
- package/lib/context/dist/assemble.d.ts +11 -0
- package/lib/context/dist/assemble.d.ts.map +1 -0
- package/lib/context/dist/assemble.js +265 -0
- package/lib/context/dist/assemble.js.map +1 -0
- package/lib/context/dist/cache.d.ts +53 -0
- package/lib/context/dist/cache.d.ts.map +1 -0
- package/lib/context/dist/cache.js +147 -0
- package/lib/context/dist/cache.js.map +1 -0
- package/lib/context/dist/contracts.d.ts +145 -0
- package/lib/context/dist/contracts.d.ts.map +1 -0
- package/lib/context/dist/contracts.js +2 -0
- package/lib/context/dist/contracts.js.map +1 -0
- package/lib/context/dist/index.d.ts +4 -0
- package/lib/context/dist/index.d.ts.map +1 -0
- package/lib/context/dist/index.js +3 -0
- package/lib/context/dist/index.js.map +1 -0
- package/lib/context/package.json +5 -0
- package/lib/context/src/assemble.ts +205 -0
- package/lib/context/src/cache.ts +143 -0
- package/lib/context/src/contracts.ts +124 -0
- package/lib/context/src/index.ts +3 -0
- package/lib/core/dist/budget.d.ts +85 -0
- package/lib/core/dist/budget.d.ts.map +1 -0
- package/lib/core/dist/budget.js +328 -0
- package/lib/core/dist/budget.js.map +1 -0
- package/lib/core/dist/contracts.d.ts +156 -0
- package/lib/core/dist/contracts.d.ts.map +1 -0
- package/lib/core/dist/contracts.js +13 -0
- package/lib/core/dist/contracts.js.map +1 -0
- package/lib/core/dist/errors.d.ts +17 -0
- package/lib/core/dist/errors.d.ts.map +1 -0
- package/lib/core/dist/errors.js +22 -0
- package/lib/core/dist/errors.js.map +1 -0
- package/lib/core/dist/event-stream.d.ts +26 -0
- package/lib/core/dist/event-stream.d.ts.map +1 -0
- package/lib/core/dist/event-stream.js +116 -0
- package/lib/core/dist/event-stream.js.map +1 -0
- package/lib/core/dist/host.d.ts +7 -0
- package/lib/core/dist/host.d.ts.map +1 -0
- package/lib/core/dist/host.js +7 -0
- package/lib/core/dist/host.js.map +1 -0
- package/lib/core/dist/index.d.ts +8 -0
- package/lib/core/dist/index.d.ts.map +1 -0
- package/lib/core/dist/index.js +7 -0
- package/lib/core/dist/index.js.map +1 -0
- package/lib/core/dist/json.d.ts +18 -0
- package/lib/core/dist/json.d.ts.map +1 -0
- package/lib/core/dist/json.js +100 -0
- package/lib/core/dist/json.js.map +1 -0
- package/lib/core/dist/lifecycle.d.ts +45 -0
- package/lib/core/dist/lifecycle.d.ts.map +1 -0
- package/lib/core/dist/lifecycle.js +114 -0
- package/lib/core/dist/lifecycle.js.map +1 -0
- package/lib/core/dist/local-guards.d.ts +4 -0
- package/lib/core/dist/local-guards.d.ts.map +1 -0
- package/lib/core/dist/local-guards.js +47 -0
- package/lib/core/dist/local-guards.js.map +1 -0
- package/lib/core/dist/managed-guards.d.ts +44 -0
- package/lib/core/dist/managed-guards.d.ts.map +1 -0
- package/lib/core/dist/managed-guards.js +146 -0
- package/lib/core/dist/managed-guards.js.map +1 -0
- package/lib/core/dist/schema.d.ts +36 -0
- package/lib/core/dist/schema.d.ts.map +1 -0
- package/lib/core/dist/schema.js +24 -0
- package/lib/core/dist/schema.js.map +1 -0
- package/lib/core/package.json +5 -0
- package/lib/core/src/budget.ts +366 -0
- package/lib/core/src/contracts.ts +107 -0
- package/lib/core/src/errors.ts +33 -0
- package/lib/core/src/event-stream.ts +87 -0
- package/lib/core/src/host.ts +6 -0
- package/lib/core/src/index.ts +7 -0
- package/lib/core/src/json.ts +81 -0
- package/lib/core/src/lifecycle.ts +133 -0
- package/lib/core/src/local-guards.ts +37 -0
- package/lib/core/src/managed-guards.ts +154 -0
- package/lib/core/src/schema.ts +43 -0
- package/lib/exporter-otlp/dist/contracts.d.ts +112 -0
- package/lib/exporter-otlp/dist/contracts.d.ts.map +1 -0
- package/lib/exporter-otlp/dist/contracts.js +2 -0
- package/lib/exporter-otlp/dist/contracts.js.map +1 -0
- package/lib/exporter-otlp/dist/index.d.ts +5 -0
- package/lib/exporter-otlp/dist/index.d.ts.map +1 -0
- package/lib/exporter-otlp/dist/index.js +4 -0
- package/lib/exporter-otlp/dist/index.js.map +1 -0
- package/lib/exporter-otlp/dist/otlp-http-json.d.ts +4 -0
- package/lib/exporter-otlp/dist/otlp-http-json.d.ts.map +1 -0
- package/lib/exporter-otlp/dist/otlp-http-json.js +286 -0
- package/lib/exporter-otlp/dist/otlp-http-json.js.map +1 -0
- package/lib/exporter-otlp/dist/run-traces.d.ts +15 -0
- package/lib/exporter-otlp/dist/run-traces.d.ts.map +1 -0
- package/lib/exporter-otlp/dist/run-traces.js +114 -0
- package/lib/exporter-otlp/dist/run-traces.js.map +1 -0
- package/lib/exporter-otlp/dist/signals.d.ts +4 -0
- package/lib/exporter-otlp/dist/signals.d.ts.map +1 -0
- package/lib/exporter-otlp/dist/signals.js +330 -0
- package/lib/exporter-otlp/dist/signals.js.map +1 -0
- package/lib/exporter-otlp/package.json +5 -0
- package/lib/exporter-otlp/src/contracts.ts +119 -0
- package/lib/exporter-otlp/src/index.ts +23 -0
- package/lib/exporter-otlp/src/otlp-http-json.ts +189 -0
- package/lib/exporter-otlp/src/run-traces.ts +86 -0
- package/lib/exporter-otlp/src/signals.ts +185 -0
- package/lib/guardrails/dist/agent-guard.d.ts +9 -0
- package/lib/guardrails/dist/agent-guard.d.ts.map +1 -0
- package/lib/guardrails/dist/agent-guard.js +23 -0
- package/lib/guardrails/dist/agent-guard.js.map +1 -0
- package/lib/guardrails/dist/auxiliary-helpers.d.ts +63 -0
- package/lib/guardrails/dist/auxiliary-helpers.d.ts.map +1 -0
- package/lib/guardrails/dist/auxiliary-helpers.js +147 -0
- package/lib/guardrails/dist/auxiliary-helpers.js.map +1 -0
- package/lib/guardrails/dist/auxiliary.d.ts +51 -0
- package/lib/guardrails/dist/auxiliary.d.ts.map +1 -0
- package/lib/guardrails/dist/auxiliary.js +230 -0
- package/lib/guardrails/dist/auxiliary.js.map +1 -0
- package/lib/guardrails/dist/batches.d.ts +18 -0
- package/lib/guardrails/dist/batches.d.ts.map +1 -0
- package/lib/guardrails/dist/batches.js +160 -0
- package/lib/guardrails/dist/batches.js.map +1 -0
- package/lib/guardrails/dist/disclosure.d.ts +30 -0
- package/lib/guardrails/dist/disclosure.d.ts.map +1 -0
- package/lib/guardrails/dist/disclosure.js +109 -0
- package/lib/guardrails/dist/disclosure.js.map +1 -0
- package/lib/guardrails/dist/helpers.d.ts +20 -0
- package/lib/guardrails/dist/helpers.d.ts.map +1 -0
- package/lib/guardrails/dist/helpers.js +72 -0
- package/lib/guardrails/dist/helpers.js.map +1 -0
- package/lib/guardrails/dist/index.d.ts +9 -0
- package/lib/guardrails/dist/index.d.ts.map +1 -0
- package/lib/guardrails/dist/index.js +9 -0
- package/lib/guardrails/dist/index.js.map +1 -0
- package/lib/guardrails/dist/managed.d.ts +18 -0
- package/lib/guardrails/dist/managed.d.ts.map +1 -0
- package/lib/guardrails/dist/managed.js +67 -0
- package/lib/guardrails/dist/managed.js.map +1 -0
- package/lib/guardrails/dist/pipeline.d.ts +55 -0
- package/lib/guardrails/dist/pipeline.d.ts.map +1 -0
- package/lib/guardrails/dist/pipeline.js +201 -0
- package/lib/guardrails/dist/pipeline.js.map +1 -0
- package/lib/guardrails/package.json +5 -0
- package/lib/guardrails/src/agent-guard.ts +22 -0
- package/lib/guardrails/src/auxiliary-helpers.ts +146 -0
- package/lib/guardrails/src/auxiliary.ts +202 -0
- package/lib/guardrails/src/batches.ts +130 -0
- package/lib/guardrails/src/disclosure.ts +92 -0
- package/lib/guardrails/src/helpers.ts +67 -0
- package/lib/guardrails/src/index.ts +8 -0
- package/lib/guardrails/src/managed.ts +64 -0
- package/lib/guardrails/src/pipeline.ts +185 -0
- package/lib/helpers/dist/concurrency.d.ts +25 -0
- package/lib/helpers/dist/concurrency.d.ts.map +1 -0
- package/lib/helpers/dist/concurrency.js +106 -0
- package/lib/helpers/dist/concurrency.js.map +1 -0
- package/lib/helpers/dist/configuration.d.ts +42 -0
- package/lib/helpers/dist/configuration.d.ts.map +1 -0
- package/lib/helpers/dist/configuration.js +119 -0
- package/lib/helpers/dist/configuration.js.map +1 -0
- package/lib/helpers/dist/control.d.ts +61 -0
- package/lib/helpers/dist/control.d.ts.map +1 -0
- package/lib/helpers/dist/control.js +220 -0
- package/lib/helpers/dist/control.js.map +1 -0
- package/lib/helpers/dist/credentials.d.ts +42 -0
- package/lib/helpers/dist/credentials.d.ts.map +1 -0
- package/lib/helpers/dist/credentials.js +126 -0
- package/lib/helpers/dist/credentials.js.map +1 -0
- package/lib/helpers/dist/data.d.ts +30 -0
- package/lib/helpers/dist/data.d.ts.map +1 -0
- package/lib/helpers/dist/data.js +90 -0
- package/lib/helpers/dist/data.js.map +1 -0
- package/lib/helpers/dist/index.d.ts +6 -0
- package/lib/helpers/dist/index.d.ts.map +1 -0
- package/lib/helpers/dist/index.js +6 -0
- package/lib/helpers/dist/index.js.map +1 -0
- package/lib/helpers/package.json +5 -0
- package/lib/helpers/src/concurrency.ts +92 -0
- package/lib/helpers/src/configuration.ts +131 -0
- package/lib/helpers/src/control.ts +231 -0
- package/lib/helpers/src/credentials.ts +113 -0
- package/lib/helpers/src/data.ts +105 -0
- package/lib/helpers/src/index.ts +5 -0
- package/lib/memory/dist/contracts.d.ts +146 -0
- package/lib/memory/dist/contracts.d.ts.map +1 -0
- package/lib/memory/dist/contracts.js +2 -0
- package/lib/memory/dist/contracts.js.map +1 -0
- package/lib/memory/dist/index.d.ts +10 -0
- package/lib/memory/dist/index.d.ts.map +1 -0
- package/lib/memory/dist/index.js +316 -0
- package/lib/memory/dist/index.js.map +1 -0
- package/lib/memory/dist/native.d.ts +157 -0
- package/lib/memory/dist/native.d.ts.map +1 -0
- package/lib/memory/dist/native.js +664 -0
- package/lib/memory/dist/native.js.map +1 -0
- package/lib/memory/dist/validation.d.ts +21 -0
- package/lib/memory/dist/validation.d.ts.map +1 -0
- package/lib/memory/dist/validation.js +123 -0
- package/lib/memory/dist/validation.js.map +1 -0
- package/lib/memory/dist/vectors.d.ts +40 -0
- package/lib/memory/dist/vectors.d.ts.map +1 -0
- package/lib/memory/dist/vectors.js +134 -0
- package/lib/memory/dist/vectors.js.map +1 -0
- package/lib/memory/package.json +5 -0
- package/lib/memory/src/contracts.ts +113 -0
- package/lib/memory/src/index.ts +241 -0
- package/lib/memory/src/native.ts +568 -0
- package/lib/memory/src/validation.ts +103 -0
- package/lib/memory/src/vectors.ts +121 -0
- package/lib/memory-remote/dist/index.d.ts +91 -0
- package/lib/memory-remote/dist/index.d.ts.map +1 -0
- package/lib/memory-remote/dist/index.js +434 -0
- package/lib/memory-remote/dist/index.js.map +1 -0
- package/lib/memory-remote/package.json +5 -0
- package/lib/memory-remote/src/index.ts +367 -0
- package/lib/observability/dist/contracts.d.ts +102 -0
- package/lib/observability/dist/contracts.d.ts.map +1 -0
- package/lib/observability/dist/contracts.js +2 -0
- package/lib/observability/dist/contracts.js.map +1 -0
- package/lib/observability/dist/index.d.ts +5 -0
- package/lib/observability/dist/index.d.ts.map +1 -0
- package/lib/observability/dist/index.js +4 -0
- package/lib/observability/dist/index.js.map +1 -0
- package/lib/observability/dist/observer.d.ts +4 -0
- package/lib/observability/dist/observer.d.ts.map +1 -0
- package/lib/observability/dist/observer.js +354 -0
- package/lib/observability/dist/observer.js.map +1 -0
- package/lib/observability/dist/validation.d.ts +6 -0
- package/lib/observability/dist/validation.d.ts.map +1 -0
- package/lib/observability/dist/validation.js +203 -0
- package/lib/observability/dist/validation.js.map +1 -0
- package/lib/observability/package.json +5 -0
- package/lib/observability/src/contracts.ts +85 -0
- package/lib/observability/src/index.ts +4 -0
- package/lib/observability/src/observer.ts +246 -0
- package/lib/observability/src/validation.ts +132 -0
- package/lib/provider-anthropic/dist/index.d.ts +19 -0
- package/lib/provider-anthropic/dist/index.d.ts.map +1 -0
- package/lib/provider-anthropic/dist/index.js +342 -0
- package/lib/provider-anthropic/dist/index.js.map +1 -0
- package/lib/provider-anthropic/package.json +5 -0
- package/lib/provider-anthropic/src/index.ts +317 -0
- package/lib/provider-openai/dist/index.d.ts +77 -0
- package/lib/provider-openai/dist/index.d.ts.map +1 -0
- package/lib/provider-openai/dist/index.js +577 -0
- package/lib/provider-openai/dist/index.js.map +1 -0
- package/lib/provider-openai/package.json +5 -0
- package/lib/provider-openai/src/index.ts +459 -0
- package/lib/runtime/dist/agent.d.ts +63 -0
- package/lib/runtime/dist/agent.d.ts.map +1 -0
- package/lib/runtime/dist/agent.js +134 -0
- package/lib/runtime/dist/agent.js.map +1 -0
- package/lib/runtime/dist/composition.d.ts +21 -0
- package/lib/runtime/dist/composition.d.ts.map +1 -0
- package/lib/runtime/dist/composition.js +54 -0
- package/lib/runtime/dist/composition.js.map +1 -0
- package/lib/runtime/dist/event-buffer.d.ts +18 -0
- package/lib/runtime/dist/event-buffer.d.ts.map +1 -0
- package/lib/runtime/dist/event-buffer.js +66 -0
- package/lib/runtime/dist/event-buffer.js.map +1 -0
- package/lib/runtime/dist/hook-execution.d.ts +40 -0
- package/lib/runtime/dist/hook-execution.d.ts.map +1 -0
- package/lib/runtime/dist/hook-execution.js +186 -0
- package/lib/runtime/dist/hook-execution.js.map +1 -0
- package/lib/runtime/dist/hooks.d.ts +178 -0
- package/lib/runtime/dist/hooks.d.ts.map +1 -0
- package/lib/runtime/dist/hooks.js +120 -0
- package/lib/runtime/dist/hooks.js.map +1 -0
- package/lib/runtime/dist/index.d.ts +6 -0
- package/lib/runtime/dist/index.d.ts.map +1 -0
- package/lib/runtime/dist/index.js +6 -0
- package/lib/runtime/dist/index.js.map +1 -0
- package/lib/runtime/dist/managed-guards.d.ts +27 -0
- package/lib/runtime/dist/managed-guards.d.ts.map +1 -0
- package/lib/runtime/dist/managed-guards.js +183 -0
- package/lib/runtime/dist/managed-guards.js.map +1 -0
- package/lib/runtime/dist/permits.d.ts +9 -0
- package/lib/runtime/dist/permits.d.ts.map +1 -0
- package/lib/runtime/dist/permits.js +77 -0
- package/lib/runtime/dist/permits.js.map +1 -0
- package/lib/runtime/dist/response.d.ts +8 -0
- package/lib/runtime/dist/response.d.ts.map +1 -0
- package/lib/runtime/dist/response.js +83 -0
- package/lib/runtime/dist/response.js.map +1 -0
- package/lib/runtime/dist/router.d.ts +51 -0
- package/lib/runtime/dist/router.d.ts.map +1 -0
- package/lib/runtime/dist/router.js +181 -0
- package/lib/runtime/dist/router.js.map +1 -0
- package/lib/runtime/dist/runtime.d.ts +98 -0
- package/lib/runtime/dist/runtime.d.ts.map +1 -0
- package/lib/runtime/dist/runtime.js +945 -0
- package/lib/runtime/dist/runtime.js.map +1 -0
- package/lib/runtime/dist/stream.d.ts +25 -0
- package/lib/runtime/dist/stream.d.ts.map +1 -0
- package/lib/runtime/dist/stream.js +226 -0
- package/lib/runtime/dist/stream.js.map +1 -0
- package/lib/runtime/package.json +5 -0
- package/lib/runtime/src/agent.ts +167 -0
- package/lib/runtime/src/composition.ts +63 -0
- package/lib/runtime/src/event-buffer.ts +55 -0
- package/lib/runtime/src/hook-execution.ts +188 -0
- package/lib/runtime/src/hooks.ts +227 -0
- package/lib/runtime/src/index.ts +5 -0
- package/lib/runtime/src/managed-guards.ts +176 -0
- package/lib/runtime/src/permits.ts +65 -0
- package/lib/runtime/src/response.ts +68 -0
- package/lib/runtime/src/router.ts +178 -0
- package/lib/runtime/src/runtime.ts +909 -0
- package/lib/runtime/src/stream.ts +130 -0
- package/lib/sdk/dist/index.d.ts +5 -0
- package/lib/sdk/dist/index.d.ts.map +1 -0
- package/lib/sdk/dist/index.js +5 -0
- package/lib/sdk/dist/index.js.map +1 -0
- package/lib/sdk/package.json +5 -0
- package/lib/sdk/src/index.ts +4 -0
- package/lib/server/dist/index.d.ts +343 -0
- package/lib/server/dist/index.d.ts.map +1 -0
- package/lib/server/dist/index.js +1356 -0
- package/lib/server/dist/index.js.map +1 -0
- package/lib/server/dist/inspector-bundle.d.ts +6 -0
- package/lib/server/dist/inspector-bundle.d.ts.map +1 -0
- package/lib/server/dist/inspector-bundle.js +6 -0
- package/lib/server/dist/inspector-bundle.js.map +1 -0
- package/lib/server/dist/inspector.d.ts +3 -0
- package/lib/server/dist/inspector.d.ts.map +1 -0
- package/lib/server/dist/inspector.js +27 -0
- package/lib/server/dist/inspector.js.map +1 -0
- package/lib/server/package.json +5 -0
- package/lib/server/src/index.ts +1033 -0
- package/lib/server/src/inspector-bundle.ts +5 -0
- package/lib/server/src/inspector.ts +27 -0
- package/lib/server-node/dist/hardening.d.ts +19 -0
- package/lib/server-node/dist/hardening.d.ts.map +1 -0
- package/lib/server-node/dist/hardening.js +79 -0
- package/lib/server-node/dist/hardening.js.map +1 -0
- package/lib/server-node/dist/index.d.ts +18 -0
- package/lib/server-node/dist/index.d.ts.map +1 -0
- package/lib/server-node/dist/index.js +53 -0
- package/lib/server-node/dist/index.js.map +1 -0
- package/lib/server-node/dist/probe.d.ts +13 -0
- package/lib/server-node/dist/probe.d.ts.map +1 -0
- package/lib/server-node/dist/probe.js +44 -0
- package/lib/server-node/dist/probe.js.map +1 -0
- package/lib/server-node/dist/production.d.ts +32 -0
- package/lib/server-node/dist/production.d.ts.map +1 -0
- package/lib/server-node/dist/production.js +125 -0
- package/lib/server-node/dist/production.js.map +1 -0
- package/lib/server-node/package.json +5 -0
- package/lib/server-node/src/hardening.ts +79 -0
- package/lib/server-node/src/index.ts +58 -0
- package/lib/server-node/src/probe.ts +36 -0
- package/lib/server-node/src/production.ts +116 -0
- package/lib/storage/dist/index.d.ts +4 -0
- package/lib/storage/dist/index.d.ts.map +1 -0
- package/lib/storage/dist/index.js +5 -0
- package/lib/storage/dist/index.js.map +1 -0
- package/lib/storage/package.json +5 -0
- package/lib/storage/src/index.ts +5 -0
- package/lib/storage-contracts/dist/contracts.d.ts +69 -0
- package/lib/storage-contracts/dist/contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/contracts.js +15 -0
- package/lib/storage-contracts/dist/contracts.js.map +1 -0
- package/lib/storage-contracts/dist/durable-budget-contracts.d.ts +110 -0
- package/lib/storage-contracts/dist/durable-budget-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/durable-budget-contracts.js +430 -0
- package/lib/storage-contracts/dist/durable-budget-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/execution-wait-contracts.d.ts +74 -0
- package/lib/storage-contracts/dist/execution-wait-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/execution-wait-contracts.js +159 -0
- package/lib/storage-contracts/dist/execution-wait-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/index.d.ts +18 -0
- package/lib/storage-contracts/dist/index.d.ts.map +1 -0
- package/lib/storage-contracts/dist/index.js +18 -0
- package/lib/storage-contracts/dist/index.js.map +1 -0
- package/lib/storage-contracts/dist/memory-contracts.d.ts +208 -0
- package/lib/storage-contracts/dist/memory-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/memory-contracts.js +350 -0
- package/lib/storage-contracts/dist/memory-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/scheduled-workflow-contracts.d.ts +171 -0
- package/lib/storage-contracts/dist/scheduled-workflow-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/scheduled-workflow-contracts.js +2 -0
- package/lib/storage-contracts/dist/scheduled-workflow-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/scheduler-contracts.d.ts +133 -0
- package/lib/storage-contracts/dist/scheduler-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/scheduler-contracts.js +2 -0
- package/lib/storage-contracts/dist/scheduler-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/submission-journal.d.ts +21 -0
- package/lib/storage-contracts/dist/submission-journal.d.ts.map +1 -0
- package/lib/storage-contracts/dist/submission-journal.js +51 -0
- package/lib/storage-contracts/dist/submission-journal.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-format2.d.ts +54 -0
- package/lib/storage-contracts/dist/workflow-format2.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-format2.js +398 -0
- package/lib/storage-contracts/dist/workflow-format2.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-format3.d.ts +20 -0
- package/lib/storage-contracts/dist/workflow-format3.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-format3.js +309 -0
- package/lib/storage-contracts/dist/workflow-format3.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-format4.d.ts +48 -0
- package/lib/storage-contracts/dist/workflow-format4.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-format4.js +132 -0
- package/lib/storage-contracts/dist/workflow-format4.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-format5.d.ts +94 -0
- package/lib/storage-contracts/dist/workflow-format5.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-format5.js +365 -0
- package/lib/storage-contracts/dist/workflow-format5.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-graph-contracts.d.ts +69 -0
- package/lib/storage-contracts/dist/workflow-graph-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-graph-contracts.js +2 -0
- package/lib/storage-contracts/dist/workflow-graph-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-graph-discovery.d.ts +45 -0
- package/lib/storage-contracts/dist/workflow-graph-discovery.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-graph-discovery.js +101 -0
- package/lib/storage-contracts/dist/workflow-graph-discovery.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-loop-format1.d.ts +52 -0
- package/lib/storage-contracts/dist/workflow-loop-format1.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-loop-format1.js +134 -0
- package/lib/storage-contracts/dist/workflow-loop-format1.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-saga-format1.d.ts +52 -0
- package/lib/storage-contracts/dist/workflow-saga-format1.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-saga-format1.js +235 -0
- package/lib/storage-contracts/dist/workflow-saga-format1.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-tree-contracts.d.ts +428 -0
- package/lib/storage-contracts/dist/workflow-tree-contracts.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-tree-contracts.js +151 -0
- package/lib/storage-contracts/dist/workflow-tree-contracts.js.map +1 -0
- package/lib/storage-contracts/dist/workflow-tree-discovery.d.ts +38 -0
- package/lib/storage-contracts/dist/workflow-tree-discovery.d.ts.map +1 -0
- package/lib/storage-contracts/dist/workflow-tree-discovery.js +57 -0
- package/lib/storage-contracts/dist/workflow-tree-discovery.js.map +1 -0
- package/lib/storage-contracts/package.json +5 -0
- package/lib/storage-contracts/src/contracts.ts +73 -0
- package/lib/storage-contracts/src/durable-budget-contracts.ts +266 -0
- package/lib/storage-contracts/src/execution-wait-contracts.ts +169 -0
- package/lib/storage-contracts/src/index.ts +17 -0
- package/lib/storage-contracts/src/memory-contracts.ts +322 -0
- package/lib/storage-contracts/src/scheduled-workflow-contracts.ts +88 -0
- package/lib/storage-contracts/src/scheduler-contracts.ts +68 -0
- package/lib/storage-contracts/src/submission-journal.ts +50 -0
- package/lib/storage-contracts/src/workflow-format2.ts +288 -0
- package/lib/storage-contracts/src/workflow-format3.ts +201 -0
- package/lib/storage-contracts/src/workflow-format4.ts +96 -0
- package/lib/storage-contracts/src/workflow-format5.ts +375 -0
- package/lib/storage-contracts/src/workflow-graph-contracts.ts +44 -0
- package/lib/storage-contracts/src/workflow-graph-discovery.ts +107 -0
- package/lib/storage-contracts/src/workflow-loop-format1.ts +102 -0
- package/lib/storage-contracts/src/workflow-saga-format1.ts +165 -0
- package/lib/storage-contracts/src/workflow-tree-contracts.ts +191 -0
- package/lib/storage-contracts/src/workflow-tree-discovery.ts +39 -0
- package/lib/storage-postgres/dist/index.d.ts +2 -0
- package/lib/storage-postgres/dist/index.d.ts.map +1 -0
- package/lib/storage-postgres/dist/index.js +2 -0
- package/lib/storage-postgres/dist/index.js.map +1 -0
- package/lib/storage-postgres/dist/postgres.d.ts +8 -0
- package/lib/storage-postgres/dist/postgres.d.ts.map +1 -0
- package/lib/storage-postgres/dist/postgres.js +233 -0
- package/lib/storage-postgres/dist/postgres.js.map +1 -0
- package/lib/storage-postgres/package.json +5 -0
- package/lib/storage-postgres/src/index.ts +1 -0
- package/lib/storage-postgres/src/postgres.ts +222 -0
- package/lib/storage-sql/dist/aggregate-session.d.ts +31 -0
- package/lib/storage-sql/dist/aggregate-session.d.ts.map +1 -0
- package/lib/storage-sql/dist/aggregate-session.js +78 -0
- package/lib/storage-sql/dist/aggregate-session.js.map +1 -0
- package/lib/storage-sql/dist/contracts.d.ts +2 -0
- package/lib/storage-sql/dist/contracts.d.ts.map +1 -0
- package/lib/storage-sql/dist/contracts.js +3 -0
- package/lib/storage-sql/dist/contracts.js.map +1 -0
- package/lib/storage-sql/dist/durable-budget-database.d.ts +33 -0
- package/lib/storage-sql/dist/durable-budget-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/durable-budget-database.js +191 -0
- package/lib/storage-sql/dist/durable-budget-database.js.map +1 -0
- package/lib/storage-sql/dist/durable-budget-state.d.ts +24 -0
- package/lib/storage-sql/dist/durable-budget-state.d.ts.map +1 -0
- package/lib/storage-sql/dist/durable-budget-state.js +211 -0
- package/lib/storage-sql/dist/durable-budget-state.js.map +1 -0
- package/lib/storage-sql/dist/durable-budget-validation.d.ts +5 -0
- package/lib/storage-sql/dist/durable-budget-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/durable-budget-validation.js +22 -0
- package/lib/storage-sql/dist/durable-budget-validation.js.map +1 -0
- package/lib/storage-sql/dist/execution-completions.d.ts +12 -0
- package/lib/storage-sql/dist/execution-completions.d.ts.map +1 -0
- package/lib/storage-sql/dist/execution-completions.js +60 -0
- package/lib/storage-sql/dist/execution-completions.js.map +1 -0
- package/lib/storage-sql/dist/execution-wait-database.d.ts +22 -0
- package/lib/storage-sql/dist/execution-wait-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/execution-wait-database.js +288 -0
- package/lib/storage-sql/dist/execution-wait-database.js.map +1 -0
- package/lib/storage-sql/dist/execution-wait-validation.d.ts +8 -0
- package/lib/storage-sql/dist/execution-wait-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/execution-wait-validation.js +125 -0
- package/lib/storage-sql/dist/execution-wait-validation.js.map +1 -0
- package/lib/storage-sql/dist/host.d.ts +22 -0
- package/lib/storage-sql/dist/host.d.ts.map +1 -0
- package/lib/storage-sql/dist/host.js +21 -0
- package/lib/storage-sql/dist/host.js.map +1 -0
- package/lib/storage-sql/dist/memory-database.d.ts +24 -0
- package/lib/storage-sql/dist/memory-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/memory-database.js +359 -0
- package/lib/storage-sql/dist/memory-database.js.map +1 -0
- package/lib/storage-sql/dist/scheduled-database.d.ts +74 -0
- package/lib/storage-sql/dist/scheduled-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/scheduled-database.js +1152 -0
- package/lib/storage-sql/dist/scheduled-database.js.map +1 -0
- package/lib/storage-sql/dist/scheduled-validation.d.ts +9 -0
- package/lib/storage-sql/dist/scheduled-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/scheduled-validation.js +200 -0
- package/lib/storage-sql/dist/scheduled-validation.js.map +1 -0
- package/lib/storage-sql/dist/scheduler-contracts.d.ts +2 -0
- package/lib/storage-sql/dist/scheduler-contracts.d.ts.map +1 -0
- package/lib/storage-sql/dist/scheduler-contracts.js +3 -0
- package/lib/storage-sql/dist/scheduler-contracts.js.map +1 -0
- package/lib/storage-sql/dist/scheduler-database.d.ts +55 -0
- package/lib/storage-sql/dist/scheduler-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/scheduler-database.js +655 -0
- package/lib/storage-sql/dist/scheduler-database.js.map +1 -0
- package/lib/storage-sql/dist/scheduler-validation.d.ts +22 -0
- package/lib/storage-sql/dist/scheduler-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/scheduler-validation.js +201 -0
- package/lib/storage-sql/dist/scheduler-validation.js.map +1 -0
- package/lib/storage-sql/dist/validation.d.ts +14 -0
- package/lib/storage-sql/dist/validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/validation.js +106 -0
- package/lib/storage-sql/dist/validation.js.map +1 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-index.d.ts +7 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-index.d.ts.map +1 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-index.js +55 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-index.js.map +1 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-validation.d.ts +5 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-validation.js +22 -0
- package/lib/storage-sql/dist/workflow-graph-discovery-validation.js.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-database.d.ts +270 -0
- package/lib/storage-sql/dist/workflow-tree-database.d.ts.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-database.js +1692 -0
- package/lib/storage-sql/dist/workflow-tree-database.js.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-discovery-validation.d.ts +5 -0
- package/lib/storage-sql/dist/workflow-tree-discovery-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-discovery-validation.js +15 -0
- package/lib/storage-sql/dist/workflow-tree-discovery-validation.js.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-validation.d.ts +9 -0
- package/lib/storage-sql/dist/workflow-tree-validation.d.ts.map +1 -0
- package/lib/storage-sql/dist/workflow-tree-validation.js +576 -0
- package/lib/storage-sql/dist/workflow-tree-validation.js.map +1 -0
- package/lib/storage-sql/package.json +5 -0
- package/lib/storage-sql/src/aggregate-session.ts +72 -0
- package/lib/storage-sql/src/contracts.ts +2 -0
- package/lib/storage-sql/src/durable-budget-database.ts +180 -0
- package/lib/storage-sql/src/durable-budget-state.ts +155 -0
- package/lib/storage-sql/src/durable-budget-validation.ts +23 -0
- package/lib/storage-sql/src/execution-completions.ts +58 -0
- package/lib/storage-sql/src/execution-wait-database.ts +219 -0
- package/lib/storage-sql/src/execution-wait-validation.ts +108 -0
- package/lib/storage-sql/src/host.ts +21 -0
- package/lib/storage-sql/src/memory-database.ts +320 -0
- package/lib/storage-sql/src/scheduled-database.ts +811 -0
- package/lib/storage-sql/src/scheduled-validation.ts +89 -0
- package/lib/storage-sql/src/scheduler-contracts.ts +2 -0
- package/lib/storage-sql/src/scheduler-database.ts +475 -0
- package/lib/storage-sql/src/scheduler-validation.ts +112 -0
- package/lib/storage-sql/src/validation.ts +104 -0
- package/lib/storage-sql/src/workflow-graph-discovery-index.ts +60 -0
- package/lib/storage-sql/src/workflow-graph-discovery-validation.ts +21 -0
- package/lib/storage-sql/src/workflow-tree-database.ts +450 -0
- package/lib/storage-sql/src/workflow-tree-discovery-validation.ts +13 -0
- package/lib/storage-sql/src/workflow-tree-validation.ts +166 -0
- package/lib/storage-sqlite/dist/backup.d.ts +24 -0
- package/lib/storage-sqlite/dist/backup.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/backup.js +84 -0
- package/lib/storage-sqlite/dist/backup.js.map +1 -0
- package/lib/storage-sqlite/dist/index.d.ts +4 -0
- package/lib/storage-sqlite/dist/index.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/index.js +4 -0
- package/lib/storage-sqlite/dist/index.js.map +1 -0
- package/lib/storage-sqlite/dist/migrations.d.ts +6 -0
- package/lib/storage-sqlite/dist/migrations.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/migrations.js +50 -0
- package/lib/storage-sqlite/dist/migrations.js.map +1 -0
- package/lib/storage-sqlite/dist/sqlite-database.d.ts +36 -0
- package/lib/storage-sqlite/dist/sqlite-database.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/sqlite-database.js +179 -0
- package/lib/storage-sqlite/dist/sqlite-database.js.map +1 -0
- package/lib/storage-sqlite/dist/sqlite-worker.d.ts +2 -0
- package/lib/storage-sqlite/dist/sqlite-worker.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/sqlite-worker.js +95 -0
- package/lib/storage-sqlite/dist/sqlite-worker.js.map +1 -0
- package/lib/storage-sqlite/dist/sqlite.d.ts +7 -0
- package/lib/storage-sqlite/dist/sqlite.d.ts.map +1 -0
- package/lib/storage-sqlite/dist/sqlite.js +78 -0
- package/lib/storage-sqlite/dist/sqlite.js.map +1 -0
- package/lib/storage-sqlite/package.json +5 -0
- package/lib/storage-sqlite/src/backup.ts +58 -0
- package/lib/storage-sqlite/src/index.ts +3 -0
- package/lib/storage-sqlite/src/migrations.ts +46 -0
- package/lib/storage-sqlite/src/sqlite-database.ts +172 -0
- package/lib/storage-sqlite/src/sqlite-worker.ts +52 -0
- package/lib/storage-sqlite/src/sqlite.ts +72 -0
- package/lib/testing/dist/index.d.ts +10 -0
- package/lib/testing/dist/index.d.ts.map +1 -0
- package/lib/testing/dist/index.js +24 -0
- package/lib/testing/dist/index.js.map +1 -0
- package/lib/testing/package.json +5 -0
- package/lib/testing/src/index.ts +26 -0
- package/lib/tools/dist/batch.d.ts +71 -0
- package/lib/tools/dist/batch.d.ts.map +1 -0
- package/lib/tools/dist/batch.js +542 -0
- package/lib/tools/dist/batch.js.map +1 -0
- package/lib/tools/dist/budget-binding.d.ts +18 -0
- package/lib/tools/dist/budget-binding.d.ts.map +1 -0
- package/lib/tools/dist/budget-binding.js +65 -0
- package/lib/tools/dist/budget-binding.js.map +1 -0
- package/lib/tools/dist/context.d.ts +17 -0
- package/lib/tools/dist/context.d.ts.map +1 -0
- package/lib/tools/dist/context.js +52 -0
- package/lib/tools/dist/context.js.map +1 -0
- package/lib/tools/dist/host.d.ts +3 -0
- package/lib/tools/dist/host.d.ts.map +1 -0
- package/lib/tools/dist/host.js +3 -0
- package/lib/tools/dist/host.js.map +1 -0
- package/lib/tools/dist/index.d.ts +79 -0
- package/lib/tools/dist/index.d.ts.map +1 -0
- package/lib/tools/dist/index.js +462 -0
- package/lib/tools/dist/index.js.map +1 -0
- package/lib/tools/package.json +5 -0
- package/lib/tools/src/batch.ts +496 -0
- package/lib/tools/src/budget-binding.ts +63 -0
- package/lib/tools/src/context.ts +56 -0
- package/lib/tools/src/host.ts +2 -0
- package/lib/tools/src/index.ts +479 -0
- package/lib/workflows/dist/agents.d.ts +24 -0
- package/lib/workflows/dist/agents.d.ts.map +1 -0
- package/lib/workflows/dist/agents.js +55 -0
- package/lib/workflows/dist/agents.js.map +1 -0
- package/lib/workflows/dist/children-coordinator.d.ts +58 -0
- package/lib/workflows/dist/children-coordinator.d.ts.map +1 -0
- package/lib/workflows/dist/children-coordinator.js +148 -0
- package/lib/workflows/dist/children-coordinator.js.map +1 -0
- package/lib/workflows/dist/children-definition.d.ts +43 -0
- package/lib/workflows/dist/children-definition.d.ts.map +1 -0
- package/lib/workflows/dist/children-definition.js +74 -0
- package/lib/workflows/dist/children-definition.js.map +1 -0
- package/lib/workflows/dist/children-discovery.d.ts +20 -0
- package/lib/workflows/dist/children-discovery.d.ts.map +1 -0
- package/lib/workflows/dist/children-discovery.js +103 -0
- package/lib/workflows/dist/children-discovery.js.map +1 -0
- package/lib/workflows/dist/children-runtime.d.ts +72 -0
- package/lib/workflows/dist/children-runtime.d.ts.map +1 -0
- package/lib/workflows/dist/children-runtime.js +600 -0
- package/lib/workflows/dist/children-runtime.js.map +1 -0
- package/lib/workflows/dist/children.d.ts +9 -0
- package/lib/workflows/dist/children.d.ts.map +1 -0
- package/lib/workflows/dist/children.js +6 -0
- package/lib/workflows/dist/children.js.map +1 -0
- package/lib/workflows/dist/composite-fleet.d.ts +87 -0
- package/lib/workflows/dist/composite-fleet.d.ts.map +1 -0
- package/lib/workflows/dist/composite-fleet.js +259 -0
- package/lib/workflows/dist/composite-fleet.js.map +1 -0
- package/lib/workflows/dist/composite-host.d.ts +45 -0
- package/lib/workflows/dist/composite-host.d.ts.map +1 -0
- package/lib/workflows/dist/composite-host.js +132 -0
- package/lib/workflows/dist/composite-host.js.map +1 -0
- package/lib/workflows/dist/composites.d.ts +4 -0
- package/lib/workflows/dist/composites.d.ts.map +1 -0
- package/lib/workflows/dist/composites.js +3 -0
- package/lib/workflows/dist/composites.js.map +1 -0
- package/lib/workflows/dist/definition.d.ts +61 -0
- package/lib/workflows/dist/definition.d.ts.map +1 -0
- package/lib/workflows/dist/definition.js +112 -0
- package/lib/workflows/dist/definition.js.map +1 -0
- package/lib/workflows/dist/drain.d.ts +21 -0
- package/lib/workflows/dist/drain.d.ts.map +1 -0
- package/lib/workflows/dist/drain.js +53 -0
- package/lib/workflows/dist/drain.js.map +1 -0
- package/lib/workflows/dist/ephemeral.d.ts +24 -0
- package/lib/workflows/dist/ephemeral.d.ts.map +1 -0
- package/lib/workflows/dist/ephemeral.js +122 -0
- package/lib/workflows/dist/ephemeral.js.map +1 -0
- package/lib/workflows/dist/fleet-control.d.ts +129 -0
- package/lib/workflows/dist/fleet-control.d.ts.map +1 -0
- package/lib/workflows/dist/fleet-control.js +356 -0
- package/lib/workflows/dist/fleet-control.js.map +1 -0
- package/lib/workflows/dist/graph-coordinator.d.ts +60 -0
- package/lib/workflows/dist/graph-coordinator.d.ts.map +1 -0
- package/lib/workflows/dist/graph-coordinator.js +266 -0
- package/lib/workflows/dist/graph-coordinator.js.map +1 -0
- package/lib/workflows/dist/graph-definition.d.ts +40 -0
- package/lib/workflows/dist/graph-definition.d.ts.map +1 -0
- package/lib/workflows/dist/graph-definition.js +58 -0
- package/lib/workflows/dist/graph-definition.js.map +1 -0
- package/lib/workflows/dist/graph-discovery.d.ts +27 -0
- package/lib/workflows/dist/graph-discovery.d.ts.map +1 -0
- package/lib/workflows/dist/graph-discovery.js +165 -0
- package/lib/workflows/dist/graph-discovery.js.map +1 -0
- package/lib/workflows/dist/graphs.d.ts +55 -0
- package/lib/workflows/dist/graphs.d.ts.map +1 -0
- package/lib/workflows/dist/graphs.js +10 -0
- package/lib/workflows/dist/graphs.js.map +1 -0
- package/lib/workflows/dist/hook-relay.d.ts +67 -0
- package/lib/workflows/dist/hook-relay.d.ts.map +1 -0
- package/lib/workflows/dist/hook-relay.js +157 -0
- package/lib/workflows/dist/hook-relay.js.map +1 -0
- package/lib/workflows/dist/index.d.ts +16 -0
- package/lib/workflows/dist/index.d.ts.map +1 -0
- package/lib/workflows/dist/index.js +15 -0
- package/lib/workflows/dist/index.js.map +1 -0
- package/lib/workflows/dist/leadership.d.ts +33 -0
- package/lib/workflows/dist/leadership.d.ts.map +1 -0
- package/lib/workflows/dist/leadership.js +109 -0
- package/lib/workflows/dist/leadership.js.map +1 -0
- package/lib/workflows/dist/lifecycle-definition.d.ts +89 -0
- package/lib/workflows/dist/lifecycle-definition.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle-definition.js +137 -0
- package/lib/workflows/dist/lifecycle-definition.js.map +1 -0
- package/lib/workflows/dist/lifecycle-fleet.d.ts +90 -0
- package/lib/workflows/dist/lifecycle-fleet.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle-fleet.js +406 -0
- package/lib/workflows/dist/lifecycle-fleet.js.map +1 -0
- package/lib/workflows/dist/lifecycle-host.d.ts +44 -0
- package/lib/workflows/dist/lifecycle-host.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle-host.js +133 -0
- package/lib/workflows/dist/lifecycle-host.js.map +1 -0
- package/lib/workflows/dist/lifecycle-human-transport.d.ts +67 -0
- package/lib/workflows/dist/lifecycle-human-transport.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle-human-transport.js +149 -0
- package/lib/workflows/dist/lifecycle-human-transport.js.map +1 -0
- package/lib/workflows/dist/lifecycle-runtime.d.ts +115 -0
- package/lib/workflows/dist/lifecycle-runtime.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle-runtime.js +831 -0
- package/lib/workflows/dist/lifecycle-runtime.js.map +1 -0
- package/lib/workflows/dist/lifecycle.d.ts +10 -0
- package/lib/workflows/dist/lifecycle.d.ts.map +1 -0
- package/lib/workflows/dist/lifecycle.js +7 -0
- package/lib/workflows/dist/lifecycle.js.map +1 -0
- package/lib/workflows/dist/loop-definition.d.ts +46 -0
- package/lib/workflows/dist/loop-definition.d.ts.map +1 -0
- package/lib/workflows/dist/loop-definition.js +55 -0
- package/lib/workflows/dist/loop-definition.js.map +1 -0
- package/lib/workflows/dist/loop-runtime.d.ts +47 -0
- package/lib/workflows/dist/loop-runtime.d.ts.map +1 -0
- package/lib/workflows/dist/loop-runtime.js +358 -0
- package/lib/workflows/dist/loop-runtime.js.map +1 -0
- package/lib/workflows/dist/loops.d.ts +6 -0
- package/lib/workflows/dist/loops.d.ts.map +1 -0
- package/lib/workflows/dist/loops.js +4 -0
- package/lib/workflows/dist/loops.js.map +1 -0
- package/lib/workflows/dist/migration-service.d.ts +55 -0
- package/lib/workflows/dist/migration-service.d.ts.map +1 -0
- package/lib/workflows/dist/migration-service.js +73 -0
- package/lib/workflows/dist/migration-service.js.map +1 -0
- package/lib/workflows/dist/migration.d.ts +123 -0
- package/lib/workflows/dist/migration.d.ts.map +1 -0
- package/lib/workflows/dist/migration.js +217 -0
- package/lib/workflows/dist/migration.js.map +1 -0
- package/lib/workflows/dist/operator-transports.d.ts +265 -0
- package/lib/workflows/dist/operator-transports.d.ts.map +1 -0
- package/lib/workflows/dist/operator-transports.js +404 -0
- package/lib/workflows/dist/operator-transports.js.map +1 -0
- package/lib/workflows/dist/reconciliation.d.ts +25 -0
- package/lib/workflows/dist/reconciliation.d.ts.map +1 -0
- package/lib/workflows/dist/reconciliation.js +109 -0
- package/lib/workflows/dist/reconciliation.js.map +1 -0
- package/lib/workflows/dist/runtime.d.ts +65 -0
- package/lib/workflows/dist/runtime.d.ts.map +1 -0
- package/lib/workflows/dist/runtime.js +492 -0
- package/lib/workflows/dist/runtime.js.map +1 -0
- package/lib/workflows/dist/saga-definition.d.ts +46 -0
- package/lib/workflows/dist/saga-definition.d.ts.map +1 -0
- package/lib/workflows/dist/saga-definition.js +71 -0
- package/lib/workflows/dist/saga-definition.js.map +1 -0
- package/lib/workflows/dist/saga-runtime.d.ts +51 -0
- package/lib/workflows/dist/saga-runtime.d.ts.map +1 -0
- package/lib/workflows/dist/saga-runtime.js +499 -0
- package/lib/workflows/dist/saga-runtime.js.map +1 -0
- package/lib/workflows/dist/sagas.d.ts +6 -0
- package/lib/workflows/dist/sagas.d.ts.map +1 -0
- package/lib/workflows/dist/sagas.js +4 -0
- package/lib/workflows/dist/sagas.js.map +1 -0
- package/lib/workflows/dist/scheduled-helpers.d.ts +53 -0
- package/lib/workflows/dist/scheduled-helpers.d.ts.map +1 -0
- package/lib/workflows/dist/scheduled-helpers.js +230 -0
- package/lib/workflows/dist/scheduled-helpers.js.map +1 -0
- package/lib/workflows/dist/scheduled.d.ts +116 -0
- package/lib/workflows/dist/scheduled.d.ts.map +1 -0
- package/lib/workflows/dist/scheduled.js +781 -0
- package/lib/workflows/dist/scheduled.js.map +1 -0
- package/lib/workflows/dist/traces.d.ts +158 -0
- package/lib/workflows/dist/traces.d.ts.map +1 -0
- package/lib/workflows/dist/traces.js +366 -0
- package/lib/workflows/dist/traces.js.map +1 -0
- package/lib/workflows/dist/versions.d.ts +57 -0
- package/lib/workflows/dist/versions.d.ts.map +1 -0
- package/lib/workflows/dist/versions.js +87 -0
- package/lib/workflows/dist/versions.js.map +1 -0
- package/lib/workflows/dist/worker.d.ts +51 -0
- package/lib/workflows/dist/worker.d.ts.map +1 -0
- package/lib/workflows/dist/worker.js +143 -0
- package/lib/workflows/dist/worker.js.map +1 -0
- package/lib/workflows/package.json +5 -0
- package/lib/workflows/src/agents.ts +65 -0
- package/lib/workflows/src/children-coordinator.ts +50 -0
- package/lib/workflows/src/children-definition.ts +88 -0
- package/lib/workflows/src/children-discovery.ts +32 -0
- package/lib/workflows/src/children-runtime.ts +169 -0
- package/lib/workflows/src/children.ts +12 -0
- package/lib/workflows/src/composite-fleet.ts +155 -0
- package/lib/workflows/src/composite-host.ts +67 -0
- package/lib/workflows/src/composites.ts +6 -0
- package/lib/workflows/src/definition.ts +113 -0
- package/lib/workflows/src/drain.ts +56 -0
- package/lib/workflows/src/ephemeral.ts +124 -0
- package/lib/workflows/src/fleet-control.ts +326 -0
- package/lib/workflows/src/graph-coordinator.ts +223 -0
- package/lib/workflows/src/graph-definition.ts +70 -0
- package/lib/workflows/src/graph-discovery.ts +145 -0
- package/lib/workflows/src/graphs.ts +56 -0
- package/lib/workflows/src/hook-relay.ts +177 -0
- package/lib/workflows/src/index.ts +27 -0
- package/lib/workflows/src/leadership.ts +102 -0
- package/lib/workflows/src/lifecycle-definition.ts +194 -0
- package/lib/workflows/src/lifecycle-fleet.ts +346 -0
- package/lib/workflows/src/lifecycle-host.ts +109 -0
- package/lib/workflows/src/lifecycle-human-transport.ts +170 -0
- package/lib/workflows/src/lifecycle-runtime.ts +673 -0
- package/lib/workflows/src/lifecycle.ts +59 -0
- package/lib/workflows/src/loop-definition.ts +62 -0
- package/lib/workflows/src/loop-runtime.ts +260 -0
- package/lib/workflows/src/loops.ts +7 -0
- package/lib/workflows/src/migration-service.ts +83 -0
- package/lib/workflows/src/migration.ts +242 -0
- package/lib/workflows/src/operator-transports.ts +439 -0
- package/lib/workflows/src/reconciliation.ts +121 -0
- package/lib/workflows/src/runtime.ts +348 -0
- package/lib/workflows/src/saga-definition.ts +104 -0
- package/lib/workflows/src/saga-runtime.ts +406 -0
- package/lib/workflows/src/sagas.ts +24 -0
- package/lib/workflows/src/scheduled-helpers.ts +214 -0
- package/lib/workflows/src/scheduled.ts +707 -0
- package/lib/workflows/src/traces.ts +381 -0
- package/lib/workflows/src/versions.ts +114 -0
- package/lib/workflows/src/worker.ts +115 -0
- package/lib/workstream/dist/executions.d.ts +34 -0
- package/lib/workstream/dist/executions.d.ts.map +1 -0
- package/lib/workstream/dist/executions.js +344 -0
- package/lib/workstream/dist/executions.js.map +1 -0
- package/lib/workstream/dist/humans.d.ts +76 -0
- package/lib/workstream/dist/humans.d.ts.map +1 -0
- package/lib/workstream/dist/humans.js +273 -0
- package/lib/workstream/dist/humans.js.map +1 -0
- package/lib/workstream/dist/index.d.ts +61 -0
- package/lib/workstream/dist/index.d.ts.map +1 -0
- package/lib/workstream/dist/index.js +310 -0
- package/lib/workstream/dist/index.js.map +1 -0
- package/lib/workstream/dist/timers.d.ts +41 -0
- package/lib/workstream/dist/timers.d.ts.map +1 -0
- package/lib/workstream/dist/timers.js +240 -0
- package/lib/workstream/dist/timers.js.map +1 -0
- package/lib/workstream/dist/webhooks.d.ts +69 -0
- package/lib/workstream/dist/webhooks.d.ts.map +1 -0
- package/lib/workstream/dist/webhooks.js +313 -0
- package/lib/workstream/dist/webhooks.js.map +1 -0
- package/lib/workstream/package.json +5 -0
- package/lib/workstream/src/executions.ts +253 -0
- package/lib/workstream/src/humans.ts +323 -0
- package/lib/workstream/src/index.ts +279 -0
- package/lib/workstream/src/timers.ts +212 -0
- package/lib/workstream/src/webhooks.ts +180 -0
- package/package.json +281 -0
|
@@ -0,0 +1,1692 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { jsonValue } from '../../core/dist/index.js';
|
|
3
|
+
import { StorageError, assertWorkflowTreeLeafState, assertWorkflowTreeRootState, initialWorkflowTreeLeafState, initialWorkflowTreeRootState, mergeWorkflowReceipt, workflowHashMaterial, workflowManifest, workflowResources, workflowTreeManifest, workflowTreePolicy, workflowTreeRootResources, workflowTreeState, workflowTreeDiscoveryCommand, workflowTreeDiscoveryPage, } from '../../storage-contracts/dist/index.js';
|
|
4
|
+
import { aggregateRecord, createAggregate, initializeOwnership, loadAggregate, lockRunIdentity, lockSql, storageClock, storedInteger, writeAggregate, } from './aggregate-session.js';
|
|
5
|
+
import { WorkflowTreeBudgetDatabase } from './durable-budget-database.js';
|
|
6
|
+
import { createCommand, identifier, nextCounter } from './validation.js';
|
|
7
|
+
import { SchedulerDatabase } from './scheduler-database.js';
|
|
8
|
+
import { initializeWorkflowGraphDiscoveryIndex } from './workflow-graph-discovery-index.js';
|
|
9
|
+
function digest(domain, value) { return createHash('sha256').update(workflowHashMaterial(domain, value)).digest('hex'); }
|
|
10
|
+
function same(left, right) { return workflowHashMaterial('compare', left) === workflowHashMaterial('compare', right); }
|
|
11
|
+
function failed() { throw new StorageError('STORAGE_UNAVAILABLE', 'Stored workflow tree failed integrity validation.'); }
|
|
12
|
+
function conflict() { throw new StorageError('CONFLICT', 'Workflow-tree identity or immutable content changed.'); }
|
|
13
|
+
/** Atomic root enrollment foundation. Execution transitions remain deliberately unavailable. */
|
|
14
|
+
export class WorkflowTreeDatabase {
|
|
15
|
+
backend;
|
|
16
|
+
scheduler;
|
|
17
|
+
initialized = false;
|
|
18
|
+
discoveryInitialized = false;
|
|
19
|
+
budgets;
|
|
20
|
+
constructor(backend, scheduler) {
|
|
21
|
+
this.backend = backend;
|
|
22
|
+
this.scheduler = scheduler;
|
|
23
|
+
this.budgets = new WorkflowTreeBudgetDatabase(backend);
|
|
24
|
+
}
|
|
25
|
+
async execute(method, input) { const value = input; switch (method) {
|
|
26
|
+
case 'initialize': return this.initialize();
|
|
27
|
+
case 'submit': return this.submit(value);
|
|
28
|
+
case 'inspect': {
|
|
29
|
+
const command = input;
|
|
30
|
+
return this.inspect(command.scope, command.rootId, command.rootPolicyHash);
|
|
31
|
+
}
|
|
32
|
+
case 'requestRootApproval': return this.requestRootApproval(value);
|
|
33
|
+
case 'approveRootTool': return this.approveRootTool(value);
|
|
34
|
+
case 'prepareRootTool': return this.prepareRootTool(value);
|
|
35
|
+
case 'claimPreparedRootTool': return this.claimPreparedRootTool(value);
|
|
36
|
+
case 'renewClaimedRootTool': return this.renewClaimedRootTool(value);
|
|
37
|
+
case 'startClaimedRootTool': return this.startClaimedRootTool(value);
|
|
38
|
+
case 'recordRootToolReceipt': return this.recordRootToolReceipt(value);
|
|
39
|
+
case 'completeRootTool': return this.completeRootTool(value);
|
|
40
|
+
case 'inspectChild': return this.inspectChild(value);
|
|
41
|
+
case 'admitChild': return this.admitChild(value);
|
|
42
|
+
case 'requestChildApproval': return this.requestChildApproval(value);
|
|
43
|
+
case 'approveChildTool': return this.approveChildTool(value);
|
|
44
|
+
case 'prepareChildTool': return this.prepareChildTool(value);
|
|
45
|
+
case 'claimPreparedChildTool': return this.claimPreparedChildTool(value);
|
|
46
|
+
case 'renewClaimedChildTool': return this.renewClaimedChildTool(value);
|
|
47
|
+
case 'startClaimedChildTool': return this.startClaimedChildTool(value);
|
|
48
|
+
case 'recordChildToolReceipt': return this.recordChildToolReceipt(value);
|
|
49
|
+
case 'completeChildTool': return this.completeChildTool(value);
|
|
50
|
+
case 'finalizeChild': return this.finalizeChild(value);
|
|
51
|
+
case 'joinChild': return this.joinChild(value);
|
|
52
|
+
case 'finalizeRoot': return this.finalizeRoot(value);
|
|
53
|
+
case 'cancelChild': return this.cancelChild(value);
|
|
54
|
+
case 'cancelRoot': return this.cancelRoot(value);
|
|
55
|
+
case 'recoverExpired': return this.recoverExpired(value);
|
|
56
|
+
case 'pauseRoot': return this.pauseRoot(value);
|
|
57
|
+
case 'resumeRoot': return this.resumeRoot(value);
|
|
58
|
+
case 'migrateRoot': return this.migrateRoot(value);
|
|
59
|
+
} }
|
|
60
|
+
table() { return `${this.backend.prefix}mayura_workflow_tree_members`; }
|
|
61
|
+
owners() { return `${this.backend.prefix}mayura_workflow_owners`; }
|
|
62
|
+
hashes(manifest, policy, resources) {
|
|
63
|
+
return {
|
|
64
|
+
scope: digest('mayura:scope:v1', policy.scope), definition: digest('mayura:workflow-tree:v1', manifest),
|
|
65
|
+
policy: digest('mayura:workflow-tree-policy:v1', policy), resources: digest('mayura:workflow-tree-resources:v1', resources),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async initialize() {
|
|
69
|
+
if (this.initialized)
|
|
70
|
+
return;
|
|
71
|
+
await this.scheduler.execute('initialize', {});
|
|
72
|
+
await this.budgets.execute('initialize', {});
|
|
73
|
+
await this.backend.transaction(async (tx) => {
|
|
74
|
+
if (this.backend.dialect === 'postgres')
|
|
75
|
+
await tx.query('SELECT pg_advisory_xact_lock(hashtext(?))', [`mayura:workflow-tree-schema:${this.backend.prefix}`]);
|
|
76
|
+
await initializeOwnership(tx, this.backend);
|
|
77
|
+
await tx.query(`CREATE TABLE IF NOT EXISTS ${this.table()} (
|
|
78
|
+
scope TEXT NOT NULL,root_id TEXT NOT NULL,aggregate_id TEXT NOT NULL,parent_id TEXT,node_id TEXT,account_id TEXT NOT NULL,
|
|
79
|
+
definition_hash TEXT NOT NULL,policy_hash TEXT NOT NULL,resource_hash TEXT NOT NULL,
|
|
80
|
+
PRIMARY KEY(scope,aggregate_id),UNIQUE(scope,root_id,account_id),UNIQUE(scope,parent_id,node_id),
|
|
81
|
+
FOREIGN KEY(scope,aggregate_id) REFERENCES ${this.owners()}(scope,aggregate_id),
|
|
82
|
+
CHECK((parent_id IS NULL AND node_id IS NULL AND aggregate_id = root_id AND account_id = 'root') OR (parent_id IS NOT NULL AND node_id IS NOT NULL AND aggregate_id <> root_id AND account_id <> 'root')))`);
|
|
83
|
+
await tx.query(`CREATE INDEX IF NOT EXISTS mayura_workflow_tree_members_root_idx ON ${this.table()}(scope,root_id,aggregate_id)`);
|
|
84
|
+
await tx.query(`CREATE TABLE IF NOT EXISTS ${this.backend.prefix}mayura_workflow_tree_jobs (
|
|
85
|
+
scope TEXT NOT NULL,root_id TEXT NOT NULL,aggregate_id TEXT NOT NULL,node_id TEXT NOT NULL,job_id TEXT NOT NULL,
|
|
86
|
+
account_id TEXT NOT NULL,reservation_id TEXT NOT NULL,cost_micros BIGINT NOT NULL CHECK(cost_micros >= 0),
|
|
87
|
+
PRIMARY KEY(scope,aggregate_id,node_id),UNIQUE(scope,job_id),UNIQUE(scope,root_id,reservation_id),
|
|
88
|
+
FOREIGN KEY(scope,aggregate_id) REFERENCES ${this.table()}(scope,aggregate_id),
|
|
89
|
+
FOREIGN KEY(scope,job_id) REFERENCES ${this.backend.prefix}mayura_scheduler_jobs(scope,job_id))`);
|
|
90
|
+
});
|
|
91
|
+
this.initialized = true;
|
|
92
|
+
}
|
|
93
|
+
async initializeDiscovery() { if (this.discoveryInitialized)
|
|
94
|
+
return; await this.initialize(); await this.backend.transaction(async (tx) => { if (this.backend.dialect === 'postgres')
|
|
95
|
+
await tx.query('SELECT pg_advisory_xact_lock(hashtext(?))', [`mayura:workflow-tree-discovery:${this.backend.prefix}`]); await initializeWorkflowGraphDiscoveryIndex(tx, this.backend); }); this.discoveryInitialized = true; }
|
|
96
|
+
/** Finite metadata hints only; each selected root is independently integrity checked. */
|
|
97
|
+
async discover(method, value) {
|
|
98
|
+
const input = workflowTreeDiscoveryCommand(method, value);
|
|
99
|
+
if (method === 'initialize')
|
|
100
|
+
return this.initializeDiscovery();
|
|
101
|
+
if (!this.discoveryInitialized)
|
|
102
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree discovery before use.');
|
|
103
|
+
const command = input;
|
|
104
|
+
try {
|
|
105
|
+
const afterId = command.cursor?.afterId ?? '';
|
|
106
|
+
const collation = this.backend.dialect === 'postgres' ? '"C"' : 'BINARY';
|
|
107
|
+
const rows = await this.backend.transaction(tx => tx.query(`SELECT aggregate_id FROM ${this.owners()} WHERE scope = ? AND policy_hash = ? AND profile = 3 AND aggregate_id COLLATE ${collation} > ? ORDER BY aggregate_id COLLATE ${collation} LIMIT ?`, [command.scope, command.policyHash, afterId, command.limit]));
|
|
108
|
+
if (rows.length > command.limit)
|
|
109
|
+
failed();
|
|
110
|
+
const candidates = [];
|
|
111
|
+
let previous = afterId;
|
|
112
|
+
for (const row of rows) {
|
|
113
|
+
const id = row.aggregate_id;
|
|
114
|
+
if (!/^[a-f0-9]{64}$/.test(id) || id <= previous)
|
|
115
|
+
failed();
|
|
116
|
+
previous = id;
|
|
117
|
+
const candidate = await this.backend.transaction(async (tx) => { const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?`, [command.scope, id]))[0]; if (!member)
|
|
118
|
+
failed(); if (member.root_id !== id || member.parent_id !== null || member.node_id !== null || member.account_id !== 'root')
|
|
119
|
+
return undefined; const root = await this.locked(tx, command.scope, id, command.policyHash); if (!root)
|
|
120
|
+
failed(); const state = workflowTreeState(root.record); if (state.status !== 'running' && state.status !== 'waiting' && state.status !== 'paused')
|
|
121
|
+
return undefined; return { rootId: id, definitionHash: root.record.definitionHash, policyHash: command.policyHash, version: root.record.version, status: state.status }; });
|
|
122
|
+
if (candidate)
|
|
123
|
+
candidates.push(candidate);
|
|
124
|
+
}
|
|
125
|
+
return workflowTreeDiscoveryPage({ candidates, examined: rows.length, nextCursor: rows.length === command.limit ? { format: 1, scope: command.scope, policyHash: command.policyHash, afterId: previous } : null }, command);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return failed();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
command(raw) {
|
|
132
|
+
const manifest = workflowTreeManifest(raw.manifest);
|
|
133
|
+
const policy = workflowTreePolicy(raw.policy);
|
|
134
|
+
const resources = workflowTreeRootResources(raw.resources, manifest);
|
|
135
|
+
const key = identifier(raw.idempotencyKey, 'Submission key');
|
|
136
|
+
if (key.length > 128)
|
|
137
|
+
throw new StorageError('INVALID_INPUT', 'Submission key exceeds its bound.');
|
|
138
|
+
const input = jsonValue(raw.input, { maxBytes: policy.maxOutputBytes });
|
|
139
|
+
const grants = new Set(policy.permissions);
|
|
140
|
+
let cost = 0n;
|
|
141
|
+
let calls = 0;
|
|
142
|
+
for (const node of manifest.graph) {
|
|
143
|
+
if (node.kind === 'tool') {
|
|
144
|
+
cost += BigInt(node.costMicros);
|
|
145
|
+
calls++;
|
|
146
|
+
}
|
|
147
|
+
else if (node.kind === 'child') {
|
|
148
|
+
if (node.policy.permissions.some(grant => !grants.has(grant)) || node.policy.maxCostMicros > policy.maxCostMicros || node.policy.maxCalls > policy.maxCalls || node.policy.maxOutputBytes > policy.maxOutputBytes || node.policy.approvalTtlMs > policy.approvalTtlMs)
|
|
149
|
+
conflict();
|
|
150
|
+
const tools = node.workflow.graph.filter(item => item.kind === 'tool');
|
|
151
|
+
cost += tools.reduce((total, item) => total + BigInt(item.costMicros), 0n);
|
|
152
|
+
calls += tools.length;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (cost > BigInt(policy.maxCostMicros) || calls > policy.maxCalls)
|
|
156
|
+
throw new StorageError('LIMIT_EXCEEDED', 'Root policy cannot fund every required declared tool.');
|
|
157
|
+
return { manifest, policy, resources, input, key, hashes: this.hashes(manifest, policy, resources) };
|
|
158
|
+
}
|
|
159
|
+
async locked(tx, scope, rootId, policyHash) {
|
|
160
|
+
const budget = await this.budgets.inSession(tx).execute('inspect', { scope, id: rootId, policyHash });
|
|
161
|
+
const row = await loadAggregate(tx, this.backend, scope, rootId);
|
|
162
|
+
if (!row) {
|
|
163
|
+
if (budget)
|
|
164
|
+
failed();
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
if (!budget)
|
|
168
|
+
failed();
|
|
169
|
+
const owner = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, rootId]))[0];
|
|
170
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, rootId]))[0];
|
|
171
|
+
if (!owner || !member || Number(owner.profile) !== 3 || storedInteger(owner.aggregate_version) !== storedInteger(row.version) || member.root_id !== rootId || member.parent_id !== null || member.node_id !== null || member.account_id !== 'root')
|
|
172
|
+
failed();
|
|
173
|
+
let data;
|
|
174
|
+
try {
|
|
175
|
+
data = JSON.parse(owner.data);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return failed();
|
|
179
|
+
}
|
|
180
|
+
const manifest = workflowTreeManifest(data.manifest);
|
|
181
|
+
const policy = workflowTreePolicy(data.policy);
|
|
182
|
+
const resources = workflowTreeRootResources(data.resources, manifest);
|
|
183
|
+
const hashes = this.hashes(manifest, policy, resources);
|
|
184
|
+
if (data.format !== 4 || data.rootId !== rootId || hashes.scope !== scope || hashes.definition !== row.definition_hash || hashes.definition !== owner.definition_hash || hashes.definition !== member.definition_hash || hashes.policy !== policyHash || hashes.policy !== owner.policy_hash || hashes.policy !== member.policy_hash || hashes.resources !== owner.resource_hash || hashes.resources !== member.resource_hash || !same(data, { format: 4, rootId, manifest, policy, resources }))
|
|
185
|
+
failed();
|
|
186
|
+
const record = aggregateRecord(row);
|
|
187
|
+
const state = workflowTreeState(record);
|
|
188
|
+
assertWorkflowTreeRootState(state, manifest, policy, resources);
|
|
189
|
+
if (state.rootId !== rootId || state.definition !== hashes.definition || state.policy !== policyHash || state.budgetVersion !== budget.version)
|
|
190
|
+
failed();
|
|
191
|
+
const account = budget.accounts.find(item => item.id === 'root');
|
|
192
|
+
if (!account || account.maxCostMicros !== policy.maxCostMicros || account.maxCalls !== policy.maxCalls)
|
|
193
|
+
failed();
|
|
194
|
+
return { record, profile: 'scheduled-v3', rootId, accountId: 'root', manifestHash: hashes.definition, policyHash: hashes.policy, resourceHash: hashes.resources, budget };
|
|
195
|
+
}
|
|
196
|
+
async rootOwner(tx, scope, rootId) {
|
|
197
|
+
const row = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, rootId]))[0];
|
|
198
|
+
if (!row || Number(row.profile) !== 3)
|
|
199
|
+
failed();
|
|
200
|
+
try {
|
|
201
|
+
const raw = JSON.parse(row.data);
|
|
202
|
+
if (raw.format !== 4 || raw.rootId !== rootId)
|
|
203
|
+
failed();
|
|
204
|
+
return { format: 4, rootId, manifest: workflowTreeManifest(raw.manifest), policy: workflowTreePolicy(raw.policy), resources: workflowTreeRootResources(raw.resources, raw.manifest) };
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return failed();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async childRecord(tx, scope, rootId, parentId, nodeId, childId, accountId, definitionHash, policyHash, resourceHash, inputHash, budget) {
|
|
211
|
+
const row = await loadAggregate(tx, this.backend, scope, childId);
|
|
212
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
213
|
+
const owner = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
214
|
+
if (!row || !member || !owner || Number(owner.profile) !== 3 || storedInteger(owner.aggregate_version) !== storedInteger(row.version) || member.root_id !== rootId || member.parent_id !== parentId || member.node_id !== nodeId || member.account_id !== accountId || member.definition_hash !== definitionHash || member.policy_hash !== policyHash || member.resource_hash !== resourceHash || owner.definition_hash !== definitionHash || owner.policy_hash !== policyHash || owner.resource_hash !== resourceHash)
|
|
215
|
+
failed();
|
|
216
|
+
let data;
|
|
217
|
+
try {
|
|
218
|
+
const raw = JSON.parse(owner.data);
|
|
219
|
+
const manifest = workflowManifest(raw.manifest);
|
|
220
|
+
const policy = workflowTreePolicy(raw.policy);
|
|
221
|
+
const resources = workflowResources(raw.resources, manifest);
|
|
222
|
+
data = { format: raw.format, rootId: raw.rootId, parentId: raw.parentId, nodeId: raw.nodeId, accountId: raw.accountId, manifest, policy, resources, inputHash: raw.inputHash };
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return failed();
|
|
226
|
+
}
|
|
227
|
+
if (data.format !== 4 || data.rootId !== rootId || data.parentId !== parentId || data.nodeId !== nodeId || data.accountId !== accountId || data.inputHash !== inputHash || digest('mayura:workflow:v1', data.manifest) !== definitionHash || digest('mayura:workflow-tree-policy:v1', data.policy) !== policyHash || digest('mayura:workflow-resources:v1', data.resources) !== resourceHash)
|
|
228
|
+
failed();
|
|
229
|
+
const record = aggregateRecord(row);
|
|
230
|
+
const state = workflowTreeState(record);
|
|
231
|
+
assertWorkflowTreeLeafState(state, data.manifest, data.policy, data.resources);
|
|
232
|
+
if (state.rootId !== rootId || state.accountId !== accountId || state.definition !== definitionHash || state.policy !== policyHash || digest('mayura:workflow-tree-child-input:v1', state.input) !== inputHash || state.budgetVersion > budget.version)
|
|
233
|
+
failed();
|
|
234
|
+
const account = budget.accounts.find(item => item.id === accountId);
|
|
235
|
+
if (!account || account.parentId !== 'root' || account.maxCostMicros !== data.policy.maxCostMicros || account.maxCalls !== data.policy.maxCalls)
|
|
236
|
+
failed();
|
|
237
|
+
return record;
|
|
238
|
+
}
|
|
239
|
+
async submit(raw) {
|
|
240
|
+
if (!this.initialized)
|
|
241
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
242
|
+
const input = this.command(raw);
|
|
243
|
+
const id = digest('mayura:workflow-tree-run:v1', { scope: input.hashes.scope, submissionKey: input.key });
|
|
244
|
+
const state = initialWorkflowTreeRootState(input.manifest, input.input, id, input.hashes.definition, input.hashes.policy, input.policy);
|
|
245
|
+
return this.backend.transaction(async (tx) => {
|
|
246
|
+
const ledger = this.budgets.inSession(tx);
|
|
247
|
+
await ledger.execute('create', { scope: input.hashes.scope, id, policyHash: input.hashes.policy, maxCostMicros: input.policy.maxCostMicros, maxCalls: input.policy.maxCalls });
|
|
248
|
+
await lockRunIdentity(tx, this.backend, input.hashes.scope, id);
|
|
249
|
+
await loadAggregate(tx, this.backend, input.hashes.scope, id);
|
|
250
|
+
const now = await storageClock(tx, this.backend);
|
|
251
|
+
const created = await createAggregate(tx, this.backend, createCommand({ scope: input.hashes.scope, id, idempotencyKey: input.key, definitionHash: input.hashes.definition, state: state, events: [{ type: 'run.created', data: {} }, { type: 'workflow.enrolled', data: { profile: 'scheduled-v3' } }] }), now);
|
|
252
|
+
if (!created.created) {
|
|
253
|
+
const snapshot = await this.locked(tx, input.hashes.scope, id, input.hashes.policy);
|
|
254
|
+
if (!snapshot || snapshot.resourceHash !== input.hashes.resources)
|
|
255
|
+
conflict();
|
|
256
|
+
return { snapshot, created: false };
|
|
257
|
+
}
|
|
258
|
+
const data = { format: 4, rootId: id, manifest: input.manifest, policy: input.policy, resources: input.resources };
|
|
259
|
+
await tx.query(`INSERT INTO ${this.owners()} (scope,aggregate_id,profile,aggregate_version,definition_hash,policy_hash,resource_hash,data) VALUES (?,?,?,?,?,?,?,?)`, [input.hashes.scope, id, 3, created.row.version, input.hashes.definition, input.hashes.policy, input.hashes.resources, JSON.stringify(data)]);
|
|
260
|
+
await tx.query(`INSERT INTO ${this.table()} (scope,root_id,aggregate_id,parent_id,node_id,account_id,definition_hash,policy_hash,resource_hash) VALUES (?,?,?,?,?,?,?,?,?)`, [input.hashes.scope, id, id, null, null, 'root', input.hashes.definition, input.hashes.policy, input.hashes.resources]);
|
|
261
|
+
const snapshot = await this.locked(tx, input.hashes.scope, id, input.hashes.policy);
|
|
262
|
+
if (!snapshot)
|
|
263
|
+
failed();
|
|
264
|
+
return { snapshot, created: true };
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
async inspect(scope, rootId, policyHash) {
|
|
268
|
+
if (!this.initialized)
|
|
269
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
270
|
+
return this.backend.transaction(tx => this.locked(tx, scope, rootId, policyHash));
|
|
271
|
+
}
|
|
272
|
+
async requestRootApproval(raw) {
|
|
273
|
+
if (!this.initialized)
|
|
274
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
275
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
276
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
277
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
278
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
279
|
+
throw new StorageError('INVALID_INPUT', 'Root approval request requires exact bounded identities.');
|
|
280
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
281
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !node.approval || !step || step.kind !== 'tool' || step.candidateHash !== null || node.dependsOn.some(dependency => state.steps[dependency]?.status !== 'succeeded'))
|
|
282
|
+
conflict(); const required = [`tool:${node.tool}`, ...node.capabilities, ...(node.effects === 'none' ? [] : [`effect:${node.effects}`])]; if (required.some(grant => !owner.policy.permissions.includes(grant)))
|
|
283
|
+
conflict(); const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes }); const now = await storageClock(tx, this.backend); const review = step.approval; const candidate = (expiresAt) => digest('mayura:workflow-tree-approval:v1', { rootId, memberId: rootId, nodeId, tool: node.tool, toolVersion: node.toolVersion, input, policyHash: raw.rootPolicyHash, expiresAt }); if (review && review.expiresAt > now && review.digest === candidate(review.expiresAt))
|
|
284
|
+
return root; if (review && review.expiresAt > now)
|
|
285
|
+
conflict(); if (root.record.version !== raw.expectedVersion || !['running', 'waiting'].includes(state.status) || !['pending', 'waiting', 'approved'].includes(step.status))
|
|
286
|
+
conflict(); const expiresAt = nextCounter(now, owner.policy.approvalTtlMs); const mutable = structuredClone(state); mutable.steps[nodeId].approval = { digest: candidate(expiresAt), expiresAt, humanId: null }; mutable.steps[nodeId].status = 'waiting'; mutable.status = 'waiting'; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
287
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'approval.requested', data: { nodeId } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
288
|
+
failed(); return current; });
|
|
289
|
+
}
|
|
290
|
+
async approveRootTool(raw) {
|
|
291
|
+
if (!this.initialized)
|
|
292
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
293
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
294
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
295
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
296
|
+
const humanId = identifier(raw.humanId, 'Human');
|
|
297
|
+
if (![rootId, raw.rootPolicyHash, raw.digest].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
298
|
+
throw new StorageError('INVALID_INPUT', 'Root approval requires exact bounded identities.');
|
|
299
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
300
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); const step = state.steps[nodeId]; const node = owner.manifest.graph.find(item => item.id === nodeId); const review = step?.approval; if (!node || node.kind !== 'tool' || !node.approval || !step || step.kind !== 'tool' || !review || review.digest !== raw.digest)
|
|
301
|
+
conflict(); if (step.status === 'approved' && review.humanId === humanId)
|
|
302
|
+
return root; const now = await storageClock(tx, this.backend); if (root.record.version !== raw.expectedVersion || step.status !== 'waiting' || review.humanId !== null || review.expiresAt <= now)
|
|
303
|
+
conflict(); const mutable = structuredClone(state); mutable.steps[nodeId].approval.humanId = humanId; mutable.steps[nodeId].status = 'approved'; if (mutable.status !== 'paused')
|
|
304
|
+
mutable.status = 'running'; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
305
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'approval.resolved', data: { nodeId, humanId } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
306
|
+
failed(); return current; });
|
|
307
|
+
}
|
|
308
|
+
async prepareRootTool(raw) {
|
|
309
|
+
if (!this.initialized)
|
|
310
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
311
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
312
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
313
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
314
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
315
|
+
throw new StorageError('INVALID_INPUT', 'Root tool preparation requires exact bounded identities.');
|
|
316
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
317
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool')
|
|
318
|
+
conflict(); const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes }); const candidateHash = digest('mayura:workflow-tree-candidate:v1', { rootId, memberId: rootId, nodeId, input, policyHash: raw.rootPolicyHash }); const jobId = digest('mayura:workflow-tree-job:v1', { scope, rootId, memberId: rootId, nodeId }); const reservationId = digest('mayura:workflow-tree-ticket:v1', { scope, rootId, memberId: rootId, nodeId }); if (step.candidateHash !== null) {
|
|
319
|
+
if (step.candidateHash !== candidateHash)
|
|
320
|
+
conflict();
|
|
321
|
+
const job = await this.scheduler.inSession(tx, rootId, []).execute('read', { scope, jobId });
|
|
322
|
+
if (!job)
|
|
323
|
+
failed();
|
|
324
|
+
return { root, job, created: false };
|
|
325
|
+
} const required = [`tool:${node.tool}`, ...node.capabilities, ...(node.effects === 'none' ? [] : [`effect:${node.effects}`])]; const now = await storageClock(tx, this.backend); const review = step.approval; const approved = !node.approval || (step.status === 'approved' && review?.humanId && review.expiresAt > now && review.digest === digest('mayura:workflow-tree-approval:v1', { rootId, memberId: rootId, nodeId, tool: node.tool, toolVersion: node.toolVersion, input, policyHash: raw.rootPolicyHash, expiresAt: review.expiresAt })); if (required.some(grant => !owner.policy.permissions.includes(grant)) || !approved || root.record.version !== raw.expectedVersion || !['running', 'waiting'].includes(state.status) || !['pending', 'approved'].includes(step.status) || node.dependsOn.some(dependency => state.steps[dependency]?.status !== 'succeeded'))
|
|
326
|
+
conflict(); const budget = await this.budgets.inSession(tx).execute('reserveBundle', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root', bundleId: jobId, operations: [{ id: reservationId, maxCostMicros: node.costMicros }] }); const events = []; const job = (await this.scheduler.inSession(tx, rootId, events).execute('reserve', { scope, jobId, reservationKey: jobId, runId: rootId, nodeId, invocationId: `${rootId}/step:${nodeId}`, definitionHash: root.manifestHash, candidateHash, intent: { toolId: node.tool, callId: `${rootId}/step:${nodeId}`, policyHash: raw.rootPolicyHash, reservationId }, resourceKeys: owner.resources[nodeId], delayMs: 0 })).job; await tx.query(`INSERT INTO ${this.backend.prefix}mayura_workflow_tree_jobs (scope,root_id,aggregate_id,node_id,job_id,account_id,reservation_id,cost_micros) VALUES (?,?,?,?,?,?,?,?)`, [scope, rootId, rootId, nodeId, jobId, 'root', reservationId, node.costMicros]); const mutable = structuredClone(state); mutable.steps[nodeId].candidateHash = candidateHash; mutable.steps[nodeId].costReserved = node.costMicros; mutable.reservedMicros += node.costMicros; mutable.budgetVersion = budget.version; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
327
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [...events, { type: 'step.prepared', data: { nodeId } }], await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
328
|
+
failed(); return { root: current, job, created: true }; });
|
|
329
|
+
}
|
|
330
|
+
async claimPreparedRootTool(raw) {
|
|
331
|
+
if (!this.initialized)
|
|
332
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
333
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
334
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
335
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
336
|
+
const workerId = identifier(raw.workerId, 'Worker');
|
|
337
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.leaseMs) || raw.leaseMs < 1_000 || raw.leaseMs > 300_000)
|
|
338
|
+
throw new StorageError('INVALID_INPUT', 'Root claim requires exact bounded identities and lease.');
|
|
339
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
340
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); if (workflowTreeState(root.record).status === 'paused')
|
|
341
|
+
return undefined; const state = workflowTreeState(root.record); const step = state.steps[nodeId]; if (!step || step.kind !== 'tool' || step.candidateHash === null)
|
|
342
|
+
conflict(); const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, rootId, nodeId]))[0]; if (!link || link.root_id !== rootId || link.account_id !== 'root')
|
|
343
|
+
failed(); const events = []; const claims = await this.scheduler.inSession(tx, rootId, events, link.job_id).execute('claim', { scope, workerId, limit: 1, leaseMs: raw.leaseMs }); const claimed = claims[0]; if (!claimed)
|
|
344
|
+
return undefined; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
345
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, state, events, await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
346
|
+
failed(); return { root: current, job: claimed.job, claim: claimed.claim }; });
|
|
347
|
+
}
|
|
348
|
+
async renewClaimedRootTool(raw) {
|
|
349
|
+
if (!this.initialized)
|
|
350
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
351
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
352
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
353
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
354
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.leaseMs) || raw.leaseMs < 1_000 || raw.leaseMs > 300_000)
|
|
355
|
+
throw new StorageError('INVALID_INPUT', 'Root renewal requires exact bounded identities and lease.');
|
|
356
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
357
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const state = workflowTreeState(root.record); const step = state.steps[nodeId]; if (!step || step.kind !== 'tool' || step.candidateHash === null || !['pending', 'dispatching'].includes(step.status))
|
|
358
|
+
conflict(); const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, rootId, nodeId]))[0]; if (!link || link.root_id !== rootId || link.account_id !== 'root' || link.job_id !== raw.claim.jobId)
|
|
359
|
+
conflict(); const events = []; const claim = await this.scheduler.inSession(tx, rootId, events, link.job_id).execute('renew', { claim: raw.claim, leaseMs: raw.leaseMs }); const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
360
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, state, events, await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
361
|
+
failed(); return { root: current, claim }; });
|
|
362
|
+
}
|
|
363
|
+
async startClaimedRootTool(raw) {
|
|
364
|
+
if (!this.initialized)
|
|
365
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
366
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
367
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
368
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
369
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
370
|
+
throw new StorageError('INVALID_INPUT', 'Root start requires exact bounded identities.');
|
|
371
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
372
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); this.schedulable(root); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || step.candidateHash === null || (root.record.version !== raw.expectedVersion && step.status !== 'dispatching'))
|
|
373
|
+
conflict(); const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes }); const candidateHash = digest('mayura:workflow-tree-candidate:v1', { rootId, memberId: rootId, nodeId, input, policyHash: raw.rootPolicyHash }); if (candidateHash !== step.candidateHash)
|
|
374
|
+
conflict(); const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, rootId, nodeId]))[0]; if (!link || link.root_id !== rootId || link.account_id !== 'root' || storedInteger(link.cost_micros) !== node.costMicros)
|
|
375
|
+
failed(); const budgetResult = await this.budgets.inSession(tx).execute('start', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root', reservationId: link.reservation_id }); const review = step.approval; const events = []; const schedulerResult = await this.scheduler.inSession(tx, rootId, events, link.job_id, { value: 0 }, node.approval ? (review?.expiresAt ?? 0) : undefined).execute('start', { claim: raw.claim, candidateHash }); if (schedulerResult.status !== budgetResult.status)
|
|
376
|
+
failed(); if (schedulerResult.status === 'already_started')
|
|
377
|
+
return { status: 'already_started', root, job: schedulerResult.job }; const mutable = structuredClone(state); mutable.steps[nodeId].status = 'dispatching'; mutable.status = 'running'; mutable.budgetVersion = budgetResult.snapshot.version; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
378
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [...events, { type: 'step.dispatching', data: { nodeId } }], await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
379
|
+
failed(); return { status: 'started', root: current, job: schedulerResult.job }; });
|
|
380
|
+
}
|
|
381
|
+
async recordRootToolReceipt(raw) {
|
|
382
|
+
if (!this.initialized)
|
|
383
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
384
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
385
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
386
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
387
|
+
const evidenceId = identifier(raw.evidenceId, 'Evidence');
|
|
388
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.fence) || raw.fence < 1)
|
|
389
|
+
throw new StorageError('INVALID_INPUT', 'Root receipt persistence requires exact bounded identities.');
|
|
390
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
391
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || !['dispatching', 'unknown'].includes(step.status))
|
|
392
|
+
conflict(); const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, rootId, nodeId]))[0]; if (!link || link.root_id !== rootId || link.account_id !== 'root' || storedInteger(link.cost_micros) !== node.costMicros)
|
|
393
|
+
failed(); const events = []; const recorded = await this.scheduler.inSession(tx, rootId, events, link.job_id).execute('recordReceipt', { scope, jobId: link.job_id, fence: raw.fence, evidenceId, receipt: raw.receipt }); if (recorded.disposition === 'conflicting')
|
|
394
|
+
return { disposition: recorded.disposition, root, job: recorded.job }; const prior = step.receipt === null ? null : step.receipt; const receipt = mergeWorkflowReceipt(prior, raw.receipt); let budget = root.budget; if (receipt.execution === 'unknown')
|
|
395
|
+
budget = await this.budgets.inSession(tx).execute('markUnknown', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root', reservationId: link.reservation_id });
|
|
396
|
+
else
|
|
397
|
+
budget = (await this.budgets.inSession(tx).execute('settle', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root', reservationId: link.reservation_id, actualMicros: receipt.execution === 'not_started' ? 0 : node.costMicros })).snapshot; if (same(prior, receipt) && budget.version === root.budget.version && events.length === 0)
|
|
398
|
+
return { disposition: recorded.disposition, root, job: recorded.job }; const mutable = structuredClone(state); const target = mutable.steps[nodeId]; target.receipt = receipt; if (receipt.execution === 'unknown') {
|
|
399
|
+
target.status = 'unknown';
|
|
400
|
+
mutable.status = 'outcome_unknown';
|
|
401
|
+
}
|
|
402
|
+
else if (target.costReserved !== 0) {
|
|
403
|
+
mutable.reservedMicros -= target.costReserved;
|
|
404
|
+
if (receipt.execution !== 'not_started')
|
|
405
|
+
mutable.spentMicros += node.costMicros;
|
|
406
|
+
target.costReserved = 0;
|
|
407
|
+
} mutable.budgetVersion = budget.version; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
408
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, events, await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
409
|
+
failed(); return { disposition: recorded.disposition, root: current, job: recorded.job }; });
|
|
410
|
+
}
|
|
411
|
+
async completeRootTool(raw) {
|
|
412
|
+
if (!this.initialized)
|
|
413
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
414
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
415
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
416
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
417
|
+
const commandId = identifier(raw.commandId, 'Command');
|
|
418
|
+
const evidenceId = identifier(raw.evidenceId, 'Evidence');
|
|
419
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || (raw.outcome !== 'succeeded' && raw.output !== null))
|
|
420
|
+
throw new StorageError('INVALID_INPUT', 'Root completion requires exact bounded identities and output semantics.');
|
|
421
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
422
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const owner = await this.rootOwner(tx, scope, rootId); const state = workflowTreeState(root.record); if (state.status === 'outcome_unknown')
|
|
423
|
+
conflict(); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || !['dispatching', raw.outcome].includes(step.status) || step.costReserved !== 0)
|
|
424
|
+
conflict(); const output = raw.outcome === 'succeeded' ? jsonValue(raw.output, { maxBytes: owner.policy.maxOutputBytes }) : null; const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, rootId, nodeId]))[0]; if (!link)
|
|
425
|
+
failed(); const events = []; const job = await this.scheduler.inSession(tx, rootId, events, link.job_id).execute('complete', { claim: raw.claim, commandId, evidenceId, outcome: raw.outcome, output }); if (step.status === raw.outcome) {
|
|
426
|
+
if (!same(step.output, job.output) || !same(step.receipt, job.receipt))
|
|
427
|
+
failed();
|
|
428
|
+
return { root, job };
|
|
429
|
+
} const mutable = structuredClone(state); const target = mutable.steps[nodeId]; target.status = raw.outcome; target.output = job.output; if (!job.receipt)
|
|
430
|
+
failed(); target.receipt = job.receipt; let changed = true; while (changed) {
|
|
431
|
+
changed = false;
|
|
432
|
+
for (const join of owner.manifest.graph)
|
|
433
|
+
if (join.kind === 'join') {
|
|
434
|
+
const current = mutable.steps[join.id];
|
|
435
|
+
if (current.status === 'pending' && join.dependsOn.every(id => mutable.steps[id]?.status === 'succeeded')) {
|
|
436
|
+
current.status = 'succeeded';
|
|
437
|
+
current.output = join.dependsOn.map(id => mutable.steps[id].output);
|
|
438
|
+
changed = true;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
} if (Object.values(mutable.steps).some(item => ['failed', 'blocked', 'unknown'].includes(item.status)))
|
|
442
|
+
mutable.status = Object.values(mutable.steps).some(item => item.status === 'unknown') ? 'outcome_unknown' : Object.values(mutable.steps).some(item => item.status === 'blocked') ? 'blocked' : 'failed'; const row = await loadAggregate(tx, this.backend, scope, rootId); if (!row)
|
|
443
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [...events, { type: 'step.completed', data: { nodeId, outcome: raw.outcome } }], await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
444
|
+
failed(); return { root: current, job }; });
|
|
445
|
+
}
|
|
446
|
+
async inspectChild(raw) {
|
|
447
|
+
if (!this.initialized)
|
|
448
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
449
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
450
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
451
|
+
const childId = identifier(raw.childId, 'Child');
|
|
452
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)))
|
|
453
|
+
throw new StorageError('INVALID_INPUT', 'Child inspection requires exact bounded identities.');
|
|
454
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
455
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0]; const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0]; if (!member || member.root_id !== rootId || member.parent_id !== rootId || !member.node_id || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
456
|
+
conflict(); let owner; try {
|
|
457
|
+
owner = JSON.parse(ownerRow.data);
|
|
458
|
+
}
|
|
459
|
+
catch {
|
|
460
|
+
return failed();
|
|
461
|
+
} const child = await this.childRecord(tx, scope, rootId, rootId, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget); return { root, member: child }; });
|
|
462
|
+
}
|
|
463
|
+
async requestChildApproval(raw) {
|
|
464
|
+
if (!this.initialized)
|
|
465
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
466
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
467
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
468
|
+
const childId = identifier(raw.childId, 'Child');
|
|
469
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
470
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
471
|
+
throw new StorageError('INVALID_INPUT', 'Child approval request requires exact bounded identities.');
|
|
472
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
473
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); this.schedulable(root); const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0]; const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0]; if (!member || member.root_id !== rootId || !member.parent_id || !member.node_id || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
474
|
+
conflict(); let owner; try {
|
|
475
|
+
owner = JSON.parse(ownerRow.data);
|
|
476
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
477
|
+
}
|
|
478
|
+
catch {
|
|
479
|
+
return failed();
|
|
480
|
+
} const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget); const state = workflowTreeState(child); const node = owner.manifest.graph.find(item => item.id === nodeId); const step = state.steps[nodeId]; if (!node || node.kind !== 'tool' || !node.approval || !step || step.kind !== 'tool' || step.candidateHash !== null || node.dependsOn.some(dependency => state.steps[dependency]?.status !== 'succeeded'))
|
|
481
|
+
conflict(); const required = [`tool:${node.tool}`, ...node.capabilities, ...(node.effects === 'none' ? [] : [`effect:${node.effects}`])]; if (required.some(grant => !owner.policy.permissions.includes(grant)))
|
|
482
|
+
conflict(); const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes }); const now = await storageClock(tx, this.backend); const review = step.approval; const candidate = (expiresAt) => digest('mayura:workflow-tree-approval:v1', { rootId, memberId: childId, nodeId, tool: node.tool, toolVersion: node.toolVersion, input, policyHash: raw.childPolicyHash, expiresAt }); if (review && review.expiresAt > now && review.digest === candidate(review.expiresAt))
|
|
483
|
+
return { root, member: child }; if (review && review.expiresAt > now)
|
|
484
|
+
conflict(); if (child.version !== raw.expectedVersion || !['running', 'waiting'].includes(state.status) || !['pending', 'waiting', 'approved'].includes(step.status))
|
|
485
|
+
conflict(); const expiresAt = nextCounter(now, owner.policy.approvalTtlMs); const mutable = structuredClone(state); mutable.steps[nodeId].approval = { digest: candidate(expiresAt), expiresAt, humanId: null }; mutable.steps[nodeId].status = 'waiting'; mutable.status = 'waiting'; const row = await loadAggregate(tx, this.backend, scope, childId); if (!row)
|
|
486
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'approval.requested', data: { nodeId } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]); return { root, member: aggregateRecord(updated) }; });
|
|
487
|
+
}
|
|
488
|
+
async approveChildTool(raw) {
|
|
489
|
+
if (!this.initialized)
|
|
490
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
491
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
492
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
493
|
+
const childId = identifier(raw.childId, 'Child');
|
|
494
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
495
|
+
const humanId = identifier(raw.humanId, 'Human');
|
|
496
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash, raw.digest].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
497
|
+
throw new StorageError('INVALID_INPUT', 'Child approval requires exact bounded identities.');
|
|
498
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
499
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0]; const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0]; if (!member || member.root_id !== rootId || !member.parent_id || !member.node_id || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
500
|
+
conflict(); let owner; try {
|
|
501
|
+
owner = JSON.parse(ownerRow.data);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return failed();
|
|
505
|
+
} const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget); const state = workflowTreeState(child); const step = state.steps[nodeId]; const node = owner.manifest.graph.find(item => item.id === nodeId); const review = step?.approval; if (!node || node.kind !== 'tool' || !node.approval || !step || step.kind !== 'tool' || !review || review.digest !== raw.digest)
|
|
506
|
+
conflict(); if (step.status === 'approved' && review.humanId === humanId)
|
|
507
|
+
return { root, member: child }; const now = await storageClock(tx, this.backend); if (child.version !== raw.expectedVersion || step.status !== 'waiting' || review.humanId !== null || review.expiresAt <= now)
|
|
508
|
+
conflict(); const mutable = structuredClone(state); mutable.steps[nodeId].approval.humanId = humanId; mutable.steps[nodeId].status = 'approved'; mutable.status = 'running'; const row = await loadAggregate(tx, this.backend, scope, childId); if (!row)
|
|
509
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'approval.resolved', data: { nodeId, humanId } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]); return { root, member: aggregateRecord(updated) }; });
|
|
510
|
+
}
|
|
511
|
+
async admitChild(raw) {
|
|
512
|
+
if (!this.initialized)
|
|
513
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
514
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
515
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
516
|
+
const parentId = identifier(raw.parentId, 'Parent');
|
|
517
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
518
|
+
if (!/^[a-f0-9]{64}$/.test(rootId) || !/^[a-f0-9]{64}$/.test(parentId) || !/^[a-f0-9]{64}$/.test(raw.rootPolicyHash) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
519
|
+
throw new StorageError('INVALID_INPUT', 'Child admission requires exact bounded identities.');
|
|
520
|
+
if (parentId !== rootId)
|
|
521
|
+
conflict();
|
|
522
|
+
return this.backend.transaction(async (tx) => {
|
|
523
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
524
|
+
if (!root)
|
|
525
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
526
|
+
const owner = await this.rootOwner(tx, scope, rootId);
|
|
527
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
528
|
+
if (!node || node.kind !== 'child')
|
|
529
|
+
conflict();
|
|
530
|
+
const input = jsonValue(raw.input, { maxBytes: node.policy.maxOutputBytes });
|
|
531
|
+
const inputHash = digest('mayura:workflow-tree-child-input:v1', input);
|
|
532
|
+
const childId = digest('mayura:workflow-tree-child-run:v1', { scope, rootId, parentId, nodeId });
|
|
533
|
+
const accountId = `child/${digest('mayura:workflow-tree-account:v1', { scope, rootId, parentId, nodeId })}`;
|
|
534
|
+
const childPolicy = workflowTreePolicy({ scope: owner.policy.scope, permissions: node.policy.permissions, policyVersion: owner.policy.policyVersion, maxCostMicros: node.policy.maxCostMicros, maxCalls: node.policy.maxCalls, maxOutputBytes: node.policy.maxOutputBytes, approvalTtlMs: node.policy.approvalTtlMs });
|
|
535
|
+
const definitionHash = digest('mayura:workflow:v1', node.workflow);
|
|
536
|
+
const policyHash = digest('mayura:workflow-tree-policy:v1', childPolicy);
|
|
537
|
+
const resourceHash = digest('mayura:workflow-resources:v1', node.resources);
|
|
538
|
+
const state = workflowTreeState(root.record);
|
|
539
|
+
const step = state.steps[nodeId];
|
|
540
|
+
if (!step || step.kind !== 'child')
|
|
541
|
+
failed();
|
|
542
|
+
if (step.child) {
|
|
543
|
+
if (step.child.runId !== childId || step.child.accountId !== accountId || step.child.definitionHash !== definitionHash || step.child.policyHash !== policyHash || step.child.inputHash !== inputHash)
|
|
544
|
+
conflict();
|
|
545
|
+
const child = await this.childRecord(tx, scope, rootId, parentId, nodeId, childId, accountId, definitionHash, policyHash, resourceHash, inputHash, root.budget);
|
|
546
|
+
return { root, child, childId, accountId, definitionHash, policyHash, resourceHash, inputHash, created: false };
|
|
547
|
+
}
|
|
548
|
+
if (root.record.version !== raw.expectedVersion || !['running', 'waiting'].includes(state.status) || step.status !== 'pending' || node.dependsOn.some(dependency => state.steps[dependency]?.status !== 'succeeded'))
|
|
549
|
+
conflict();
|
|
550
|
+
await lockRunIdentity(tx, this.backend, scope, childId);
|
|
551
|
+
if (await loadAggregate(tx, this.backend, scope, childId))
|
|
552
|
+
conflict();
|
|
553
|
+
const ledger = this.budgets.inSession(tx);
|
|
554
|
+
const budget = await ledger.execute('fork', { scope, id: rootId, policyHash: raw.rootPolicyHash, parentId: 'root', accountId, maxCostMicros: childPolicy.maxCostMicros, maxCalls: childPolicy.maxCalls });
|
|
555
|
+
const childState = initialWorkflowTreeLeafState(node.workflow, input, rootId, accountId, definitionHash, policyHash, childPolicy);
|
|
556
|
+
const mutable = structuredClone(childState);
|
|
557
|
+
mutable.accountId = accountId;
|
|
558
|
+
mutable.budgetVersion = budget.version;
|
|
559
|
+
const now = await storageClock(tx, this.backend);
|
|
560
|
+
const created = await createAggregate(tx, this.backend, createCommand({ scope, id: childId, idempotencyKey: digest('mayura:workflow-tree-child-submission:v1', { scope, rootId, parentId, nodeId }), definitionHash, state: mutable, events: [{ type: 'run.created', data: {} }, { type: 'workflow.enrolled', data: { profile: 'scheduled-v3', rootId, parentId, nodeId } }] }), now);
|
|
561
|
+
if (!created.created)
|
|
562
|
+
conflict();
|
|
563
|
+
const childOwner = { format: 4, rootId, parentId, nodeId, accountId, manifest: node.workflow, policy: childPolicy, resources: node.resources, inputHash };
|
|
564
|
+
await tx.query(`INSERT INTO ${this.owners()} (scope,aggregate_id,profile,aggregate_version,definition_hash,policy_hash,resource_hash,data) VALUES (?,?,?,?,?,?,?,?)`, [scope, childId, 3, created.row.version, definitionHash, policyHash, resourceHash, JSON.stringify(childOwner)]);
|
|
565
|
+
await tx.query(`INSERT INTO ${this.table()} (scope,root_id,aggregate_id,parent_id,node_id,account_id,definition_hash,policy_hash,resource_hash) VALUES (?,?,?,?,?,?,?,?,?)`, [scope, rootId, childId, parentId, nodeId, accountId, definitionHash, policyHash, resourceHash]);
|
|
566
|
+
const rootState = structuredClone(state);
|
|
567
|
+
const rootStep = rootState.steps[nodeId];
|
|
568
|
+
rootStep.status = 'waiting';
|
|
569
|
+
rootStep.candidateHash = inputHash;
|
|
570
|
+
rootStep.child = { runId: childId, accountId, definitionHash, policyHash, inputHash, joinedVersion: null };
|
|
571
|
+
rootState.budgetVersion = budget.version;
|
|
572
|
+
rootState.status = 'waiting';
|
|
573
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
574
|
+
if (!rootRow)
|
|
575
|
+
failed();
|
|
576
|
+
const updated = await writeAggregate(tx, this.backend, rootRow, rootState, [{ type: 'workflow.child_admitted', data: { nodeId, childId } }], now);
|
|
577
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]);
|
|
578
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
579
|
+
if (!current)
|
|
580
|
+
failed();
|
|
581
|
+
return { root: current, child: aggregateRecord(created.row), childId, accountId, definitionHash, policyHash, resourceHash, inputHash, created: true };
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
async prepareChildTool(raw) {
|
|
585
|
+
if (!this.initialized)
|
|
586
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
587
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
588
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
589
|
+
const childId = identifier(raw.childId, 'Child');
|
|
590
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
591
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
592
|
+
throw new StorageError('INVALID_INPUT', 'Tool preparation requires exact bounded identities.');
|
|
593
|
+
return this.backend.transaction(async (tx) => {
|
|
594
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
595
|
+
if (!root)
|
|
596
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
597
|
+
this.schedulable(root);
|
|
598
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
599
|
+
if (!member || member.root_id !== rootId || !member.parent_id || !member.node_id || member.policy_hash !== raw.childPolicyHash)
|
|
600
|
+
conflict();
|
|
601
|
+
const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, digest('mayura:workflow-tree-child-input:v1', workflowTreeState(aggregateRecord((await loadAggregate(tx, this.backend, scope, childId)))).input), root.budget);
|
|
602
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
603
|
+
if (!ownerRow)
|
|
604
|
+
failed();
|
|
605
|
+
let owner;
|
|
606
|
+
try {
|
|
607
|
+
owner = JSON.parse(ownerRow.data);
|
|
608
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
609
|
+
}
|
|
610
|
+
catch {
|
|
611
|
+
return failed();
|
|
612
|
+
}
|
|
613
|
+
const state = workflowTreeState(child);
|
|
614
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
615
|
+
const step = state.steps[nodeId];
|
|
616
|
+
if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool')
|
|
617
|
+
conflict();
|
|
618
|
+
const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes });
|
|
619
|
+
const candidateHash = digest('mayura:workflow-tree-candidate:v1', { rootId, memberId: childId, nodeId, input, policyHash: raw.childPolicyHash });
|
|
620
|
+
const jobId = digest('mayura:workflow-tree-job:v1', { scope, rootId, memberId: childId, nodeId });
|
|
621
|
+
const reservationId = digest('mayura:workflow-tree-ticket:v1', { scope, rootId, memberId: childId, nodeId });
|
|
622
|
+
if (step.candidateHash !== null) {
|
|
623
|
+
if (step.candidateHash !== candidateHash)
|
|
624
|
+
conflict();
|
|
625
|
+
const job = await this.scheduler.inSession(tx, childId, []).execute('read', { scope, jobId });
|
|
626
|
+
if (!job)
|
|
627
|
+
failed();
|
|
628
|
+
return { root, member: child, job, created: false };
|
|
629
|
+
}
|
|
630
|
+
const required = [`tool:${node.tool}`, ...node.capabilities, ...(node.effects === 'none' ? [] : [`effect:${node.effects}`])];
|
|
631
|
+
const now = await storageClock(tx, this.backend);
|
|
632
|
+
const review = step.approval;
|
|
633
|
+
const approved = !node.approval || (step.status === 'approved' && review?.humanId && review.expiresAt > now && review.digest === digest('mayura:workflow-tree-approval:v1', { rootId, memberId: childId, nodeId, tool: node.tool, toolVersion: node.toolVersion, input, policyHash: raw.childPolicyHash, expiresAt: review.expiresAt }));
|
|
634
|
+
if (required.some(grant => !owner.policy.permissions.includes(grant)) || !approved || child.version !== raw.expectedVersion || !['running', 'waiting'].includes(state.status) || !['pending', 'approved'].includes(step.status) || node.dependsOn.some(dependency => state.steps[dependency]?.status !== 'succeeded'))
|
|
635
|
+
conflict();
|
|
636
|
+
const budget = await this.budgets.inSession(tx).execute('reserveBundle', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, bundleId: jobId, operations: [{ id: reservationId, maxCostMicros: node.costMicros }] });
|
|
637
|
+
const events = [];
|
|
638
|
+
const job = (await this.scheduler.inSession(tx, childId, events).execute('reserve', { scope, jobId, reservationKey: jobId, runId: childId, nodeId, invocationId: `${childId}/step:${nodeId}`, definitionHash: member.definition_hash, candidateHash, intent: { toolId: node.tool, callId: `${childId}/step:${nodeId}`, policyHash: raw.childPolicyHash, reservationId }, resourceKeys: owner.resources[nodeId], delayMs: 0 })).job;
|
|
639
|
+
await tx.query(`INSERT INTO ${this.backend.prefix}mayura_workflow_tree_jobs (scope,root_id,aggregate_id,node_id,job_id,account_id,reservation_id,cost_micros) VALUES (?,?,?,?,?,?,?,?)`, [scope, rootId, childId, nodeId, jobId, member.account_id, reservationId, node.costMicros]);
|
|
640
|
+
const childState = structuredClone(state);
|
|
641
|
+
childState.steps[nodeId].candidateHash = candidateHash;
|
|
642
|
+
childState.steps[nodeId].costReserved = node.costMicros;
|
|
643
|
+
childState.reservedMicros += node.costMicros;
|
|
644
|
+
childState.budgetVersion = budget.version;
|
|
645
|
+
const childRow = await loadAggregate(tx, this.backend, scope, childId);
|
|
646
|
+
if (!childRow)
|
|
647
|
+
failed();
|
|
648
|
+
const updatedChild = await writeAggregate(tx, this.backend, childRow, childState, [...events, { type: 'step.prepared', data: { nodeId } }], await storageClock(tx, this.backend));
|
|
649
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedChild.version, scope, childId]);
|
|
650
|
+
const rootState = workflowTreeState(root.record);
|
|
651
|
+
const mutableRoot = structuredClone(rootState);
|
|
652
|
+
mutableRoot.budgetVersion = budget.version;
|
|
653
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
654
|
+
if (!rootRow)
|
|
655
|
+
failed();
|
|
656
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId, nodeId } }], await storageClock(tx, this.backend));
|
|
657
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
658
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
659
|
+
if (!current)
|
|
660
|
+
failed();
|
|
661
|
+
return { root: current, member: aggregateRecord(updatedChild), job, created: true };
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
async claimPreparedChildTool(raw) {
|
|
665
|
+
if (!this.initialized)
|
|
666
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
667
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
668
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
669
|
+
const childId = identifier(raw.childId, 'Child');
|
|
670
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
671
|
+
const workerId = identifier(raw.workerId, 'Worker');
|
|
672
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.leaseMs) || raw.leaseMs < 1_000 || raw.leaseMs > 300_000)
|
|
673
|
+
throw new StorageError('INVALID_INPUT', 'Child claim requires exact bounded identities and lease.');
|
|
674
|
+
return this.backend.transaction(async (tx) => {
|
|
675
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
676
|
+
if (!root)
|
|
677
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
678
|
+
if (workflowTreeState(root.record).status === 'paused')
|
|
679
|
+
return undefined;
|
|
680
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
681
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
682
|
+
if (!member || !member.parent_id || !member.node_id || member.root_id !== rootId || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
683
|
+
conflict();
|
|
684
|
+
let owner;
|
|
685
|
+
try {
|
|
686
|
+
owner = JSON.parse(ownerRow.data);
|
|
687
|
+
}
|
|
688
|
+
catch {
|
|
689
|
+
return failed();
|
|
690
|
+
}
|
|
691
|
+
const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
692
|
+
const state = workflowTreeState(child);
|
|
693
|
+
const step = state.steps[nodeId];
|
|
694
|
+
if (!step || step.kind !== 'tool' || step.candidateHash === null)
|
|
695
|
+
conflict();
|
|
696
|
+
const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, childId, nodeId]))[0];
|
|
697
|
+
if (!link || link.root_id !== rootId || link.account_id !== member.account_id)
|
|
698
|
+
failed();
|
|
699
|
+
const events = [];
|
|
700
|
+
const claims = await this.scheduler.inSession(tx, childId, events, link.job_id).execute('claim', { scope, workerId, limit: 1, leaseMs: raw.leaseMs });
|
|
701
|
+
const claimed = claims[0];
|
|
702
|
+
if (!claimed)
|
|
703
|
+
return undefined;
|
|
704
|
+
const row = await loadAggregate(tx, this.backend, scope, childId);
|
|
705
|
+
if (!row)
|
|
706
|
+
failed();
|
|
707
|
+
const updated = await writeAggregate(tx, this.backend, row, state, events, await storageClock(tx, this.backend));
|
|
708
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]);
|
|
709
|
+
return { root, member: aggregateRecord(updated), job: claimed.job, claim: claimed.claim };
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
async renewClaimedChildTool(raw) {
|
|
713
|
+
if (!this.initialized)
|
|
714
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
715
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
716
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
717
|
+
const childId = identifier(raw.childId, 'Child');
|
|
718
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
719
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.leaseMs) || raw.leaseMs < 1_000 || raw.leaseMs > 300_000)
|
|
720
|
+
throw new StorageError('INVALID_INPUT', 'Child renewal requires exact bounded identities and lease.');
|
|
721
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
722
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0]; const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0]; if (!member || member.root_id !== rootId || !member.parent_id || !member.node_id || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
723
|
+
conflict(); let owner; try {
|
|
724
|
+
owner = JSON.parse(ownerRow.data);
|
|
725
|
+
}
|
|
726
|
+
catch {
|
|
727
|
+
return failed();
|
|
728
|
+
} const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget); const state = workflowTreeState(child); const step = state.steps[nodeId]; if (!step || step.kind !== 'tool' || step.candidateHash === null || !['pending', 'dispatching'].includes(step.status))
|
|
729
|
+
conflict(); const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, childId, nodeId]))[0]; if (!link || link.root_id !== rootId || link.account_id !== member.account_id || link.job_id !== raw.claim.jobId)
|
|
730
|
+
conflict(); const events = []; const claim = await this.scheduler.inSession(tx, childId, events, link.job_id).execute('renew', { claim: raw.claim, leaseMs: raw.leaseMs }); const row = await loadAggregate(tx, this.backend, scope, childId); if (!row)
|
|
731
|
+
failed(); const updated = await writeAggregate(tx, this.backend, row, state, events, await storageClock(tx, this.backend)); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]); return { root, member: aggregateRecord(updated), claim }; });
|
|
732
|
+
}
|
|
733
|
+
async startClaimedChildTool(raw) {
|
|
734
|
+
if (!this.initialized)
|
|
735
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
736
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
737
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
738
|
+
const childId = identifier(raw.childId, 'Child');
|
|
739
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
740
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
741
|
+
throw new StorageError('INVALID_INPUT', 'Child start requires exact bounded identities.');
|
|
742
|
+
return this.backend.transaction(async (tx) => {
|
|
743
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
744
|
+
if (!root)
|
|
745
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
746
|
+
this.schedulable(root);
|
|
747
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
748
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
749
|
+
if (!member || !member.parent_id || !member.node_id || member.root_id !== rootId || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
750
|
+
conflict();
|
|
751
|
+
let owner;
|
|
752
|
+
try {
|
|
753
|
+
owner = JSON.parse(ownerRow.data);
|
|
754
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
755
|
+
}
|
|
756
|
+
catch {
|
|
757
|
+
return failed();
|
|
758
|
+
}
|
|
759
|
+
const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
760
|
+
const state = workflowTreeState(child);
|
|
761
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
762
|
+
const step = state.steps[nodeId];
|
|
763
|
+
if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || step.candidateHash === null || (child.version !== raw.expectedVersion && step.status !== 'dispatching'))
|
|
764
|
+
conflict();
|
|
765
|
+
const input = jsonValue(raw.input, { maxBytes: owner.policy.maxOutputBytes });
|
|
766
|
+
const candidateHash = digest('mayura:workflow-tree-candidate:v1', { rootId, memberId: childId, nodeId, input, policyHash: raw.childPolicyHash });
|
|
767
|
+
if (candidateHash !== step.candidateHash)
|
|
768
|
+
conflict();
|
|
769
|
+
const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, childId, nodeId]))[0];
|
|
770
|
+
if (!link || link.root_id !== rootId || link.account_id !== member.account_id || storedInteger(link.cost_micros) !== node.costMicros)
|
|
771
|
+
failed();
|
|
772
|
+
const budgetResult = await this.budgets.inSession(tx).execute('start', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, reservationId: link.reservation_id });
|
|
773
|
+
const review = step.approval;
|
|
774
|
+
const events = [];
|
|
775
|
+
const schedulerResult = await this.scheduler.inSession(tx, childId, events, link.job_id, { value: 0 }, node.approval ? (review?.expiresAt ?? 0) : undefined).execute('start', { claim: raw.claim, candidateHash });
|
|
776
|
+
if (schedulerResult.status !== budgetResult.status)
|
|
777
|
+
failed();
|
|
778
|
+
if (schedulerResult.status === 'already_started')
|
|
779
|
+
return { status: 'already_started', root, member: child, job: schedulerResult.job };
|
|
780
|
+
const childState = structuredClone(state);
|
|
781
|
+
childState.steps[nodeId].status = 'dispatching';
|
|
782
|
+
childState.status = 'running';
|
|
783
|
+
childState.budgetVersion = budgetResult.snapshot.version;
|
|
784
|
+
const now = await storageClock(tx, this.backend);
|
|
785
|
+
const childRow = await loadAggregate(tx, this.backend, scope, childId);
|
|
786
|
+
if (!childRow)
|
|
787
|
+
failed();
|
|
788
|
+
const updatedChild = await writeAggregate(tx, this.backend, childRow, childState, [...events, { type: 'step.dispatching', data: { nodeId } }], now);
|
|
789
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedChild.version, scope, childId]);
|
|
790
|
+
const rootState = workflowTreeState(root.record);
|
|
791
|
+
const mutableRoot = structuredClone(rootState);
|
|
792
|
+
mutableRoot.budgetVersion = budgetResult.snapshot.version;
|
|
793
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
794
|
+
if (!rootRow)
|
|
795
|
+
failed();
|
|
796
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId, nodeId } }], now);
|
|
797
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
798
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
799
|
+
if (!current)
|
|
800
|
+
failed();
|
|
801
|
+
return { status: 'started', root: current, member: aggregateRecord(updatedChild), job: schedulerResult.job };
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
async recordChildToolReceipt(raw) {
|
|
805
|
+
if (!this.initialized)
|
|
806
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
807
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
808
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
809
|
+
const childId = identifier(raw.childId, 'Child');
|
|
810
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
811
|
+
const evidenceId = identifier(raw.evidenceId, 'Evidence');
|
|
812
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.fence) || raw.fence < 1)
|
|
813
|
+
throw new StorageError('INVALID_INPUT', 'Receipt persistence requires exact bounded identities.');
|
|
814
|
+
return this.backend.transaction(async (tx) => {
|
|
815
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
816
|
+
if (!root)
|
|
817
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
818
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
819
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
820
|
+
if (!member || !member.parent_id || !member.node_id || member.root_id !== rootId || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
821
|
+
conflict();
|
|
822
|
+
let owner;
|
|
823
|
+
try {
|
|
824
|
+
owner = JSON.parse(ownerRow.data);
|
|
825
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
826
|
+
}
|
|
827
|
+
catch {
|
|
828
|
+
return failed();
|
|
829
|
+
}
|
|
830
|
+
const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
831
|
+
const state = workflowTreeState(child);
|
|
832
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
833
|
+
const step = state.steps[nodeId];
|
|
834
|
+
if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || !['dispatching', 'unknown'].includes(step.status))
|
|
835
|
+
conflict();
|
|
836
|
+
const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, childId, nodeId]))[0];
|
|
837
|
+
if (!link || link.root_id !== rootId || link.account_id !== member.account_id || storedInteger(link.cost_micros) !== node.costMicros)
|
|
838
|
+
failed();
|
|
839
|
+
const events = [];
|
|
840
|
+
const recorded = await this.scheduler.inSession(tx, childId, events, link.job_id).execute('recordReceipt', { scope, jobId: link.job_id, fence: raw.fence, evidenceId, receipt: raw.receipt });
|
|
841
|
+
if (recorded.disposition === 'conflicting')
|
|
842
|
+
return { disposition: recorded.disposition, root, member: child, job: recorded.job };
|
|
843
|
+
const prior = step.receipt === null ? null : step.receipt;
|
|
844
|
+
const receipt = mergeWorkflowReceipt(prior, raw.receipt);
|
|
845
|
+
let budget = root.budget;
|
|
846
|
+
if (receipt.execution === 'unknown')
|
|
847
|
+
budget = await this.budgets.inSession(tx).execute('markUnknown', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, reservationId: link.reservation_id });
|
|
848
|
+
else
|
|
849
|
+
budget = (await this.budgets.inSession(tx).execute('settle', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, reservationId: link.reservation_id, actualMicros: receipt.execution === 'not_started' ? 0 : node.costMicros })).snapshot;
|
|
850
|
+
if (same(prior, receipt) && budget.version === root.budget.version && events.length === 0)
|
|
851
|
+
return { disposition: recorded.disposition, root, member: child, job: recorded.job };
|
|
852
|
+
const childState = structuredClone(state);
|
|
853
|
+
const childStep = childState.steps[nodeId];
|
|
854
|
+
childStep.receipt = receipt;
|
|
855
|
+
if (receipt.execution === 'unknown') {
|
|
856
|
+
childStep.status = 'unknown';
|
|
857
|
+
childState.status = 'outcome_unknown';
|
|
858
|
+
}
|
|
859
|
+
else if (childStep.costReserved !== 0) {
|
|
860
|
+
childState.reservedMicros -= childStep.costReserved;
|
|
861
|
+
if (receipt.execution !== 'not_started')
|
|
862
|
+
childState.spentMicros += node.costMicros;
|
|
863
|
+
childStep.costReserved = 0;
|
|
864
|
+
}
|
|
865
|
+
if (childState.status === 'outcome_unknown' && childState.reservedMicros === 0)
|
|
866
|
+
budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id });
|
|
867
|
+
childState.budgetVersion = budget.version;
|
|
868
|
+
const now = await storageClock(tx, this.backend);
|
|
869
|
+
const childRow = await loadAggregate(tx, this.backend, scope, childId);
|
|
870
|
+
if (!childRow)
|
|
871
|
+
failed();
|
|
872
|
+
const updatedChild = await writeAggregate(tx, this.backend, childRow, childState, events, now);
|
|
873
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedChild.version, scope, childId]);
|
|
874
|
+
const rootState = workflowTreeState(root.record);
|
|
875
|
+
const mutableRoot = structuredClone(rootState);
|
|
876
|
+
mutableRoot.budgetVersion = budget.version;
|
|
877
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
878
|
+
if (!rootRow)
|
|
879
|
+
failed();
|
|
880
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId, nodeId } }], now);
|
|
881
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
882
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
883
|
+
if (!current)
|
|
884
|
+
failed();
|
|
885
|
+
return { disposition: recorded.disposition, root: current, member: aggregateRecord(updatedChild), job: recorded.job };
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
async completeChildTool(raw) {
|
|
889
|
+
if (!this.initialized)
|
|
890
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
891
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
892
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
893
|
+
const childId = identifier(raw.childId, 'Child');
|
|
894
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
895
|
+
const commandId = identifier(raw.commandId, 'Command');
|
|
896
|
+
const evidenceId = identifier(raw.evidenceId, 'Evidence');
|
|
897
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || (raw.outcome !== 'succeeded' && raw.output !== null))
|
|
898
|
+
throw new StorageError('INVALID_INPUT', 'Completion requires exact bounded identities and output semantics.');
|
|
899
|
+
return this.backend.transaction(async (tx) => {
|
|
900
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
901
|
+
if (!root)
|
|
902
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
903
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
904
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
905
|
+
if (!member || !member.parent_id || !member.node_id || member.root_id !== rootId || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
906
|
+
conflict();
|
|
907
|
+
let owner;
|
|
908
|
+
try {
|
|
909
|
+
owner = JSON.parse(ownerRow.data);
|
|
910
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
911
|
+
}
|
|
912
|
+
catch {
|
|
913
|
+
return failed();
|
|
914
|
+
}
|
|
915
|
+
const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
916
|
+
const state = workflowTreeState(child);
|
|
917
|
+
if (state.status === 'outcome_unknown')
|
|
918
|
+
conflict();
|
|
919
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
920
|
+
const step = state.steps[nodeId];
|
|
921
|
+
if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || !['dispatching', raw.outcome].includes(step.status) || step.costReserved !== 0)
|
|
922
|
+
conflict();
|
|
923
|
+
const output = raw.outcome === 'succeeded' ? jsonValue(raw.output, { maxBytes: owner.policy.maxOutputBytes }) : null;
|
|
924
|
+
const link = (await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? AND node_id = ?${lockSql(this.backend)}`, [scope, childId, nodeId]))[0];
|
|
925
|
+
if (!link)
|
|
926
|
+
failed();
|
|
927
|
+
const events = [];
|
|
928
|
+
const job = await this.scheduler.inSession(tx, childId, events, link.job_id).execute('complete', { claim: raw.claim, commandId, evidenceId, outcome: raw.outcome, output });
|
|
929
|
+
if (step.status === raw.outcome) {
|
|
930
|
+
if (!same(step.output, job.output) || !same(step.receipt, job.receipt))
|
|
931
|
+
failed();
|
|
932
|
+
return { root, member: child, job };
|
|
933
|
+
}
|
|
934
|
+
const childState = structuredClone(state);
|
|
935
|
+
const target = childState.steps[nodeId];
|
|
936
|
+
target.status = raw.outcome;
|
|
937
|
+
target.output = job.output;
|
|
938
|
+
if (!job.receipt)
|
|
939
|
+
failed();
|
|
940
|
+
target.receipt = job.receipt;
|
|
941
|
+
let changed = true;
|
|
942
|
+
while (changed) {
|
|
943
|
+
changed = false;
|
|
944
|
+
for (const join of owner.manifest.graph)
|
|
945
|
+
if (join.kind === 'join') {
|
|
946
|
+
const current = childState.steps[join.id];
|
|
947
|
+
if (current.status === 'pending' && join.dependsOn.every(id => childState.steps[id]?.status === 'succeeded')) {
|
|
948
|
+
current.status = 'succeeded';
|
|
949
|
+
current.output = join.dependsOn.map(id => childState.steps[id].output);
|
|
950
|
+
changed = true;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (Object.values(childState.steps).some(item => ['failed', 'blocked', 'unknown'].includes(item.status)))
|
|
955
|
+
childState.status = Object.values(childState.steps).some(item => item.status === 'unknown') ? 'outcome_unknown' : Object.values(childState.steps).some(item => item.status === 'blocked') ? 'blocked' : 'failed';
|
|
956
|
+
let budget = root.budget;
|
|
957
|
+
if (childState.status !== 'running' && childState.status !== 'waiting' && childState.reservedMicros === 0)
|
|
958
|
+
budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id });
|
|
959
|
+
childState.budgetVersion = budget.version;
|
|
960
|
+
const now = await storageClock(tx, this.backend);
|
|
961
|
+
const row = await loadAggregate(tx, this.backend, scope, childId);
|
|
962
|
+
if (!row)
|
|
963
|
+
failed();
|
|
964
|
+
const updated = await writeAggregate(tx, this.backend, row, childState, [...events, { type: 'step.completed', data: { nodeId, outcome: raw.outcome } }], now);
|
|
965
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]);
|
|
966
|
+
if (budget.version === root.budget.version)
|
|
967
|
+
return { root, member: aggregateRecord(updated), job };
|
|
968
|
+
const rootState = workflowTreeState(root.record);
|
|
969
|
+
const mutableRoot = structuredClone(rootState);
|
|
970
|
+
mutableRoot.budgetVersion = budget.version;
|
|
971
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
972
|
+
if (!rootRow)
|
|
973
|
+
failed();
|
|
974
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId, nodeId } }], now);
|
|
975
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
976
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
977
|
+
if (!current)
|
|
978
|
+
failed();
|
|
979
|
+
return { root: current, member: aggregateRecord(updated), job };
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
async finalizeChild(raw) {
|
|
983
|
+
if (!this.initialized)
|
|
984
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
985
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
986
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
987
|
+
const childId = identifier(raw.childId, 'Child');
|
|
988
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
989
|
+
throw new StorageError('INVALID_INPUT', 'Child finalization requires exact bounded identities.');
|
|
990
|
+
return this.backend.transaction(async (tx) => { const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!root)
|
|
991
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.'); this.schedulable(root); const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0]; const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0]; if (!member || !member.parent_id || !member.node_id || member.root_id !== rootId || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
992
|
+
conflict(); let owner; try {
|
|
993
|
+
owner = JSON.parse(ownerRow.data);
|
|
994
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
995
|
+
}
|
|
996
|
+
catch {
|
|
997
|
+
return failed();
|
|
998
|
+
} const child = await this.childRecord(tx, scope, rootId, member.parent_id, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget); const state = workflowTreeState(child); const output = jsonValue(raw.output, { maxBytes: owner.policy.maxOutputBytes }); if (state.status === 'succeeded') {
|
|
999
|
+
if (!same(state.output, output))
|
|
1000
|
+
conflict();
|
|
1001
|
+
return { root, member: child };
|
|
1002
|
+
} if (child.version !== raw.expectedVersion || state.reservedMicros !== 0 || Object.values(state.steps).some(step => step.status !== 'succeeded'))
|
|
1003
|
+
conflict(); const budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id }); const childState = structuredClone(state); childState.status = 'succeeded'; childState.output = output; childState.budgetVersion = budget.version; const now = await storageClock(tx, this.backend); const row = await loadAggregate(tx, this.backend, scope, childId); if (!row)
|
|
1004
|
+
failed(); const updatedChild = await writeAggregate(tx, this.backend, row, childState, [{ type: 'run.completed', data: { status: 'succeeded' } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedChild.version, scope, childId]); const rootState = workflowTreeState(root.record); const mutableRoot = structuredClone(rootState); mutableRoot.budgetVersion = budget.version; const rootRow = await loadAggregate(tx, this.backend, scope, rootId); if (!rootRow)
|
|
1005
|
+
failed(); const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId } }], now); await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]); const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash); if (!current)
|
|
1006
|
+
failed(); return { root: current, member: aggregateRecord(updatedChild) }; });
|
|
1007
|
+
}
|
|
1008
|
+
async finalizeRoot(raw) {
|
|
1009
|
+
if (!this.initialized)
|
|
1010
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1011
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1012
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1013
|
+
if (!/^[a-f0-9]{64}$/.test(rootId) || !/^[a-f0-9]{64}$/.test(raw.rootPolicyHash) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1014
|
+
throw new StorageError('INVALID_INPUT', 'Root finalization requires exact bounded identities.');
|
|
1015
|
+
return this.backend.transaction(async (tx) => {
|
|
1016
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1017
|
+
if (!root)
|
|
1018
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1019
|
+
this.schedulable(root);
|
|
1020
|
+
const owner = await this.rootOwner(tx, scope, rootId);
|
|
1021
|
+
const state = workflowTreeState(root.record);
|
|
1022
|
+
const output = jsonValue(raw.output, { maxBytes: owner.policy.maxOutputBytes });
|
|
1023
|
+
if (state.status === 'succeeded') {
|
|
1024
|
+
if (!same(state.output, output))
|
|
1025
|
+
conflict();
|
|
1026
|
+
return root;
|
|
1027
|
+
}
|
|
1028
|
+
if (root.record.version !== raw.expectedVersion || state.reservedMicros !== 0 || root.budget.blocked || Object.values(state.steps).some(step => step.status !== 'succeeded') || root.budget.accounts.some(account => account.id !== 'root' && !account.closed) || root.budget.reservations.some(ticket => ticket.status !== 'settled' && ticket.status !== 'cancelled'))
|
|
1029
|
+
conflict();
|
|
1030
|
+
// Root success revalidates each persisted required-child fact rather than trusting the parent's copied projection.
|
|
1031
|
+
const children = owner.manifest.graph.filter(node => node.kind === 'child').map(node => ({ node, step: state.steps[node.id] })).sort((left, right) => (left.step.child?.runId ?? '').localeCompare(right.step.child?.runId ?? ''));
|
|
1032
|
+
for (const { node, step } of children) {
|
|
1033
|
+
const link = step.child;
|
|
1034
|
+
if (!link || link.joinedVersion === null)
|
|
1035
|
+
failed();
|
|
1036
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, link.runId]))[0];
|
|
1037
|
+
if (!member || member.root_id !== rootId || member.parent_id !== rootId || member.node_id !== node.id || member.account_id !== link.accountId || member.definition_hash !== link.definitionHash || member.policy_hash !== link.policyHash)
|
|
1038
|
+
failed();
|
|
1039
|
+
const child = await this.childRecord(tx, scope, rootId, rootId, node.id, link.runId, link.accountId, link.definitionHash, link.policyHash, member.resource_hash, link.inputHash, root.budget);
|
|
1040
|
+
const childState = workflowTreeState(child);
|
|
1041
|
+
if (child.version !== link.joinedVersion || childState.status !== 'succeeded' || !same(childState.output, step.output))
|
|
1042
|
+
failed();
|
|
1043
|
+
}
|
|
1044
|
+
const budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root' });
|
|
1045
|
+
const mutable = structuredClone(state);
|
|
1046
|
+
mutable.status = 'succeeded';
|
|
1047
|
+
mutable.output = output;
|
|
1048
|
+
mutable.budgetVersion = budget.version;
|
|
1049
|
+
const row = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1050
|
+
if (!row)
|
|
1051
|
+
failed();
|
|
1052
|
+
const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'run.completed', data: { status: 'succeeded' } }], await storageClock(tx, this.backend));
|
|
1053
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]);
|
|
1054
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1055
|
+
if (!current)
|
|
1056
|
+
failed();
|
|
1057
|
+
return current;
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
async cancelChild(raw) {
|
|
1061
|
+
if (!this.initialized)
|
|
1062
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1063
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1064
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1065
|
+
const childId = identifier(raw.childId, 'Child');
|
|
1066
|
+
const commandId = identifier(raw.commandId, 'Command');
|
|
1067
|
+
if (![rootId, childId, raw.rootPolicyHash, raw.childPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1068
|
+
throw new StorageError('INVALID_INPUT', 'Child cancellation requires exact bounded identities.');
|
|
1069
|
+
return this.backend.transaction(async (tx) => {
|
|
1070
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1071
|
+
if (!root)
|
|
1072
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1073
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, childId]))[0];
|
|
1074
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, childId]))[0];
|
|
1075
|
+
if (!member || member.root_id !== rootId || member.parent_id !== rootId || !member.node_id || member.policy_hash !== raw.childPolicyHash || !ownerRow)
|
|
1076
|
+
conflict();
|
|
1077
|
+
let owner;
|
|
1078
|
+
try {
|
|
1079
|
+
owner = JSON.parse(ownerRow.data);
|
|
1080
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
1081
|
+
}
|
|
1082
|
+
catch {
|
|
1083
|
+
return failed();
|
|
1084
|
+
}
|
|
1085
|
+
const child = await this.childRecord(tx, scope, rootId, rootId, member.node_id, childId, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
1086
|
+
const initial = workflowTreeState(child);
|
|
1087
|
+
const account = root.budget.accounts.find(item => item.id === member.account_id);
|
|
1088
|
+
if (!account)
|
|
1089
|
+
failed();
|
|
1090
|
+
if (initial.status === 'cancelled' || (initial.status === 'outcome_unknown' && account.closed))
|
|
1091
|
+
return { root, member: child, jobs: [] };
|
|
1092
|
+
if (!['running', 'waiting', 'outcome_unknown'].includes(initial.status) || child.version !== raw.expectedVersion)
|
|
1093
|
+
conflict();
|
|
1094
|
+
const links = await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND aggregate_id = ? ORDER BY job_id${lockSql(this.backend)}`, [scope, childId]);
|
|
1095
|
+
if (links.length !== root.budget.bundles.filter(bundle => bundle.accountId === member.account_id).length)
|
|
1096
|
+
failed();
|
|
1097
|
+
if (links.length) {
|
|
1098
|
+
const placeholders = links.map(() => '?').join(',');
|
|
1099
|
+
const parameters = [scope, ...links.map(link => link.job_id)];
|
|
1100
|
+
const lockedJobs = await tx.query(`SELECT job_id FROM ${this.backend.prefix}mayura_scheduler_jobs WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY job_id${lockSql(this.backend)}`, parameters);
|
|
1101
|
+
if (lockedJobs.length !== links.length)
|
|
1102
|
+
failed();
|
|
1103
|
+
await tx.query(`SELECT resource_key FROM ${this.backend.prefix}mayura_scheduler_resources WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY resource_key,job_id${lockSql(this.backend)}`, parameters);
|
|
1104
|
+
}
|
|
1105
|
+
const state = structuredClone(initial);
|
|
1106
|
+
let budget = root.budget;
|
|
1107
|
+
const events = [];
|
|
1108
|
+
const jobs = [];
|
|
1109
|
+
for (const link of links) {
|
|
1110
|
+
const node = owner.manifest.graph.find(item => item.id === link.node_id);
|
|
1111
|
+
const step = state.steps[link.node_id];
|
|
1112
|
+
if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool' || link.root_id !== rootId || link.account_id !== member.account_id || storedInteger(link.cost_micros) !== node.costMicros)
|
|
1113
|
+
failed();
|
|
1114
|
+
const integrated = this.scheduler.inSession(tx, childId, events, link.job_id);
|
|
1115
|
+
const job = await integrated.execute('read', { scope, jobId: link.job_id });
|
|
1116
|
+
if (!job || job.runId !== childId || job.nodeId !== link.node_id)
|
|
1117
|
+
failed();
|
|
1118
|
+
if (!['ready', 'leased', 'started'].includes(job.state))
|
|
1119
|
+
continue;
|
|
1120
|
+
const cancelled = await integrated.execute('cancel', { scope, jobId: link.job_id, commandId: digest('mayura:workflow-tree-cancel-job:v1', { rootId, childId, commandId, jobId: link.job_id }) });
|
|
1121
|
+
jobs.push(cancelled);
|
|
1122
|
+
if (job.state === 'started') {
|
|
1123
|
+
budget = await this.budgets.inSession(tx).execute('markUnknown', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, reservationId: link.reservation_id });
|
|
1124
|
+
step.status = 'unknown';
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
budget = await this.budgets.inSession(tx).execute('cancelReservation', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id, reservationId: link.reservation_id });
|
|
1128
|
+
if (step.costReserved !== 0) {
|
|
1129
|
+
state.reservedMicros -= step.costReserved;
|
|
1130
|
+
step.costReserved = 0;
|
|
1131
|
+
}
|
|
1132
|
+
step.status = 'skipped';
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: member.account_id });
|
|
1136
|
+
for (const step of Object.values(state.steps))
|
|
1137
|
+
if (['pending', 'waiting', 'approved'].includes(step.status))
|
|
1138
|
+
step.status = 'skipped';
|
|
1139
|
+
state.status = Object.values(state.steps).some(step => step.status === 'unknown') ? 'outcome_unknown' : 'cancelled';
|
|
1140
|
+
state.output = null;
|
|
1141
|
+
state.budgetVersion = budget.version;
|
|
1142
|
+
const now = await storageClock(tx, this.backend);
|
|
1143
|
+
const childRow = await loadAggregate(tx, this.backend, scope, childId);
|
|
1144
|
+
if (!childRow)
|
|
1145
|
+
failed();
|
|
1146
|
+
const updatedChild = await writeAggregate(tx, this.backend, childRow, state, [...events, { type: 'run.cancelled', data: { commandId } }], now);
|
|
1147
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedChild.version, scope, childId]);
|
|
1148
|
+
const rootState = workflowTreeState(root.record);
|
|
1149
|
+
const mutableRoot = structuredClone(rootState);
|
|
1150
|
+
mutableRoot.budgetVersion = budget.version;
|
|
1151
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1152
|
+
if (!rootRow)
|
|
1153
|
+
failed();
|
|
1154
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, [{ type: 'workflow.budget_updated', data: { memberId: childId } }], now);
|
|
1155
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
1156
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1157
|
+
if (!current)
|
|
1158
|
+
failed();
|
|
1159
|
+
return { root: current, member: aggregateRecord(updatedChild), jobs };
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
async cancelRoot(raw) {
|
|
1163
|
+
if (!this.initialized)
|
|
1164
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1165
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1166
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1167
|
+
const commandId = identifier(raw.commandId, 'Command');
|
|
1168
|
+
if (!/^[a-f0-9]{64}$/.test(rootId) || !/^[a-f0-9]{64}$/.test(raw.rootPolicyHash) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1169
|
+
throw new StorageError('INVALID_INPUT', 'Root cancellation requires exact bounded identities.');
|
|
1170
|
+
return this.backend.transaction(async (tx) => {
|
|
1171
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1172
|
+
if (!root)
|
|
1173
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1174
|
+
const rootOwner = await this.rootOwner(tx, scope, rootId);
|
|
1175
|
+
const initialRoot = workflowTreeState(root.record);
|
|
1176
|
+
if (initialRoot.status === 'cancelled')
|
|
1177
|
+
return { root, members: [], jobs: [] };
|
|
1178
|
+
if (initialRoot.status === 'succeeded' || root.record.version !== raw.expectedVersion)
|
|
1179
|
+
conflict();
|
|
1180
|
+
const preserveOutcome = !['running', 'waiting', 'paused'].includes(initialRoot.status);
|
|
1181
|
+
const members = await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND root_id = ? ORDER BY aggregate_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1182
|
+
if (!members.some(member => member.aggregate_id === rootId && member.account_id === 'root'))
|
|
1183
|
+
failed();
|
|
1184
|
+
const records = new Map([[rootId, root.record]]);
|
|
1185
|
+
const states = new Map([[rootId, structuredClone(initialRoot)]]);
|
|
1186
|
+
const manifests = new Map([[rootId, rootOwner.manifest]]);
|
|
1187
|
+
const activeChildren = new Set();
|
|
1188
|
+
for (const member of members.filter(item => item.aggregate_id !== rootId)) {
|
|
1189
|
+
if (member.parent_id !== rootId || !member.node_id)
|
|
1190
|
+
failed();
|
|
1191
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, member.aggregate_id]))[0];
|
|
1192
|
+
if (!ownerRow)
|
|
1193
|
+
failed();
|
|
1194
|
+
let owner;
|
|
1195
|
+
try {
|
|
1196
|
+
owner = JSON.parse(ownerRow.data);
|
|
1197
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
1198
|
+
}
|
|
1199
|
+
catch {
|
|
1200
|
+
return failed();
|
|
1201
|
+
}
|
|
1202
|
+
const child = await this.childRecord(tx, scope, rootId, rootId, member.node_id, member.aggregate_id, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
1203
|
+
records.set(member.aggregate_id, child);
|
|
1204
|
+
states.set(member.aggregate_id, structuredClone(workflowTreeState(child)));
|
|
1205
|
+
manifests.set(member.aggregate_id, owner.manifest);
|
|
1206
|
+
if (['running', 'waiting'].includes(workflowTreeState(child).status))
|
|
1207
|
+
activeChildren.add(member.aggregate_id);
|
|
1208
|
+
}
|
|
1209
|
+
const links = await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND root_id = ? ORDER BY job_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1210
|
+
if (links.length !== root.budget.bundles.length || links.some(link => !root.budget.reservations.some(ticket => ticket.id === link.reservation_id && ticket.bundleId === link.job_id && ticket.accountId === link.account_id)))
|
|
1211
|
+
failed();
|
|
1212
|
+
if (links.length) {
|
|
1213
|
+
const placeholders = links.map(() => '?').join(',');
|
|
1214
|
+
const parameters = [scope, ...links.map(link => link.job_id)];
|
|
1215
|
+
const lockedJobs = await tx.query(`SELECT job_id FROM ${this.backend.prefix}mayura_scheduler_jobs WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY job_id${lockSql(this.backend)}`, parameters);
|
|
1216
|
+
if (lockedJobs.length !== links.length)
|
|
1217
|
+
failed();
|
|
1218
|
+
await tx.query(`SELECT resource_key FROM ${this.backend.prefix}mayura_scheduler_resources WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY resource_key,job_id${lockSql(this.backend)}`, parameters);
|
|
1219
|
+
}
|
|
1220
|
+
let budget = root.budget;
|
|
1221
|
+
const changedJobs = [];
|
|
1222
|
+
const memberEvents = new Map();
|
|
1223
|
+
for (const link of links) {
|
|
1224
|
+
const record = records.get(link.aggregate_id);
|
|
1225
|
+
const state = states.get(link.aggregate_id);
|
|
1226
|
+
const manifest = manifests.get(link.aggregate_id);
|
|
1227
|
+
const membership = members.find(item => item.aggregate_id === link.aggregate_id);
|
|
1228
|
+
const node = manifest?.graph.find(item => item.id === link.node_id);
|
|
1229
|
+
const step = state?.steps[link.node_id];
|
|
1230
|
+
if (!record || !state || !membership || !node || node.kind !== 'tool' || !step || step.kind !== 'tool' || link.account_id !== membership.account_id || storedInteger(link.cost_micros) !== node.costMicros)
|
|
1231
|
+
failed();
|
|
1232
|
+
const events = memberEvents.get(link.aggregate_id) ?? [];
|
|
1233
|
+
memberEvents.set(link.aggregate_id, events);
|
|
1234
|
+
const integrated = this.scheduler.inSession(tx, link.aggregate_id, events, link.job_id);
|
|
1235
|
+
const job = await integrated.execute('read', { scope, jobId: link.job_id });
|
|
1236
|
+
if (!job || job.runId !== link.aggregate_id || job.nodeId !== link.node_id)
|
|
1237
|
+
failed();
|
|
1238
|
+
if (!['ready', 'leased', 'started'].includes(job.state))
|
|
1239
|
+
continue;
|
|
1240
|
+
const cancelled = await integrated.execute('cancel', { scope, jobId: link.job_id, commandId: digest('mayura:workflow-tree-cancel-job:v1', { rootId, commandId, jobId: link.job_id }) });
|
|
1241
|
+
changedJobs.push(cancelled);
|
|
1242
|
+
if (job.state === 'started') {
|
|
1243
|
+
budget = await this.budgets.inSession(tx).execute('markUnknown', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id, reservationId: link.reservation_id });
|
|
1244
|
+
step.status = 'unknown';
|
|
1245
|
+
}
|
|
1246
|
+
else {
|
|
1247
|
+
budget = await this.budgets.inSession(tx).execute('cancelReservation', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id, reservationId: link.reservation_id });
|
|
1248
|
+
if (step.costReserved !== 0) {
|
|
1249
|
+
state.reservedMicros -= step.costReserved;
|
|
1250
|
+
step.costReserved = 0;
|
|
1251
|
+
}
|
|
1252
|
+
step.status = 'skipped';
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: 'root' });
|
|
1256
|
+
const now = await storageClock(tx, this.backend);
|
|
1257
|
+
const updatedMembers = [];
|
|
1258
|
+
for (const member of members.filter(item => item.aggregate_id !== rootId)) {
|
|
1259
|
+
const state = states.get(member.aggregate_id);
|
|
1260
|
+
const record = records.get(member.aggregate_id);
|
|
1261
|
+
if (!state || !record)
|
|
1262
|
+
failed();
|
|
1263
|
+
if (!activeChildren.has(member.aggregate_id))
|
|
1264
|
+
continue;
|
|
1265
|
+
for (const step of Object.values(state.steps))
|
|
1266
|
+
if (['pending', 'waiting', 'approved'].includes(step.status))
|
|
1267
|
+
step.status = 'skipped';
|
|
1268
|
+
state.status = Object.values(state.steps).some(step => step.status === 'unknown') ? 'outcome_unknown' : 'cancelled';
|
|
1269
|
+
state.output = null;
|
|
1270
|
+
state.budgetVersion = budget.version;
|
|
1271
|
+
const row = await loadAggregate(tx, this.backend, scope, member.aggregate_id);
|
|
1272
|
+
if (!row)
|
|
1273
|
+
failed();
|
|
1274
|
+
const updated = await writeAggregate(tx, this.backend, row, state, [...(memberEvents.get(member.aggregate_id) ?? []), { type: 'run.cancelled', data: { rootId } }], now);
|
|
1275
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, member.aggregate_id]);
|
|
1276
|
+
updatedMembers.push(aggregateRecord(updated));
|
|
1277
|
+
}
|
|
1278
|
+
const rootState = states.get(rootId);
|
|
1279
|
+
for (const node of rootOwner.manifest.graph) {
|
|
1280
|
+
const step = rootState.steps[node.id];
|
|
1281
|
+
if (node.kind === 'child' && step.child) {
|
|
1282
|
+
const childState = states.get(step.child.runId);
|
|
1283
|
+
if (childState?.status === 'outcome_unknown')
|
|
1284
|
+
step.status = 'unknown';
|
|
1285
|
+
else if (!preserveOutcome && step.status !== 'succeeded')
|
|
1286
|
+
step.status = 'skipped';
|
|
1287
|
+
else if (!['succeeded', 'failed', 'blocked', 'unknown'].includes(step.status))
|
|
1288
|
+
step.status = 'skipped';
|
|
1289
|
+
}
|
|
1290
|
+
else if (['pending', 'waiting', 'approved'].includes(step.status))
|
|
1291
|
+
step.status = 'skipped';
|
|
1292
|
+
}
|
|
1293
|
+
if (!preserveOutcome)
|
|
1294
|
+
rootState.status = 'cancelled';
|
|
1295
|
+
rootState.output = null;
|
|
1296
|
+
rootState.budgetVersion = budget.version;
|
|
1297
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1298
|
+
if (!rootRow)
|
|
1299
|
+
failed();
|
|
1300
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, rootState, [...(memberEvents.get(rootId) ?? []), { type: preserveOutcome ? 'workflow.terminated' : 'run.cancelled', data: { commandId, status: rootState.status } }], now);
|
|
1301
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
1302
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1303
|
+
if (!current)
|
|
1304
|
+
failed();
|
|
1305
|
+
return { root: current, members: updatedMembers, jobs: changedJobs };
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
/** A paused root schedules nothing anywhere in its tree; approvals may still be recorded. */
|
|
1309
|
+
schedulable(root) { if (workflowTreeState(root.record).status === 'paused')
|
|
1310
|
+
conflict(); }
|
|
1311
|
+
/** Tree-wide quiescent pause. Every root and child job must be unclaimed or settled. */
|
|
1312
|
+
async pauseRoot(raw) {
|
|
1313
|
+
if (!this.initialized)
|
|
1314
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1315
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1316
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1317
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1318
|
+
throw new StorageError('INVALID_INPUT', 'Root pause requires exact bounded identities.');
|
|
1319
|
+
return this.backend.transaction(async (tx) => {
|
|
1320
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1321
|
+
if (!root)
|
|
1322
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1323
|
+
const state = workflowTreeState(root.record);
|
|
1324
|
+
if (state.status === 'paused')
|
|
1325
|
+
return root;
|
|
1326
|
+
if (!['running', 'waiting'].includes(state.status) || root.record.version !== raw.expectedVersion)
|
|
1327
|
+
conflict();
|
|
1328
|
+
const links = await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND root_id = ? ORDER BY job_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1329
|
+
for (const link of links) {
|
|
1330
|
+
const job = await this.scheduler.inSession(tx, link.aggregate_id, [], link.job_id).execute('read', { scope, jobId: link.job_id });
|
|
1331
|
+
if (!job || job.runId !== link.aggregate_id || job.nodeId !== link.node_id)
|
|
1332
|
+
failed();
|
|
1333
|
+
if (job.state === 'leased' || job.state === 'started')
|
|
1334
|
+
conflict();
|
|
1335
|
+
}
|
|
1336
|
+
return this.writeRootStatus(tx, scope, rootId, raw.rootPolicyHash, state, 'paused', 'run.paused');
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
async resumeRoot(raw) {
|
|
1340
|
+
if (!this.initialized)
|
|
1341
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1342
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1343
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1344
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1345
|
+
throw new StorageError('INVALID_INPUT', 'Root resume requires exact bounded identities.');
|
|
1346
|
+
return this.backend.transaction(async (tx) => {
|
|
1347
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1348
|
+
if (!root)
|
|
1349
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1350
|
+
const state = workflowTreeState(root.record);
|
|
1351
|
+
if (state.status !== 'paused' || root.record.version !== raw.expectedVersion)
|
|
1352
|
+
conflict();
|
|
1353
|
+
const status = Object.values(state.steps).some(step => step.status === 'waiting') ? 'waiting' : 'running';
|
|
1354
|
+
return this.writeRootStatus(tx, scope, rootId, raw.rootPolicyHash, state, status, 'run.resumed');
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Apply a reviewed migration to a paused, quiescent root. Everything is re-verified under the root lock: the caller's
|
|
1359
|
+
* plan is advisory. Steps with a scheduler job or an admitted child keep their exact node; the unchanged policy must
|
|
1360
|
+
* still fund every declared tool; other steps may only be carried unchanged or re-enter as fresh pending steps.
|
|
1361
|
+
*/
|
|
1362
|
+
async migrateRoot(raw) {
|
|
1363
|
+
if (!this.initialized)
|
|
1364
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1365
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1366
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1367
|
+
if (![rootId, raw.rootPolicyHash].every(value => /^[a-f0-9]{64}$/.test(value)) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1368
|
+
throw new StorageError('INVALID_INPUT', 'Root migration requires exact bounded identities.');
|
|
1369
|
+
const refuse = (message) => { throw new StorageError('CONFLICT', `Migration refused: ${message}`); };
|
|
1370
|
+
return this.backend.transaction(async (tx) => {
|
|
1371
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1372
|
+
if (!root)
|
|
1373
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1374
|
+
if (root.record.version !== raw.expectedVersion)
|
|
1375
|
+
conflict();
|
|
1376
|
+
const previous = workflowTreeState(root.record);
|
|
1377
|
+
if (previous.status !== 'paused')
|
|
1378
|
+
refuse('the tree must be paused.');
|
|
1379
|
+
const owner = await this.rootOwner(tx, scope, rootId);
|
|
1380
|
+
// The same funding rule as submission, against the unchanged root policy.
|
|
1381
|
+
const checked = this.command({ manifest: raw.manifest, policy: owner.policy, resources: raw.resources, input: previous.input, idempotencyKey: 'migration' });
|
|
1382
|
+
const { manifest, resources, hashes } = checked;
|
|
1383
|
+
if (hashes.definition === root.manifestHash)
|
|
1384
|
+
refuse('the definition is unchanged.');
|
|
1385
|
+
const links = await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND root_id = ? ORDER BY job_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1386
|
+
for (const link of links) {
|
|
1387
|
+
const job = await this.scheduler.inSession(tx, link.aggregate_id, [], link.job_id).execute('read', { scope, jobId: link.job_id });
|
|
1388
|
+
if (!job || job.runId !== link.aggregate_id || job.nodeId !== link.node_id)
|
|
1389
|
+
failed();
|
|
1390
|
+
if (job.state === 'leased' || job.state === 'started')
|
|
1391
|
+
refuse('a root or child job is leased or started.');
|
|
1392
|
+
}
|
|
1393
|
+
const jobbed = new Set(links.filter(link => link.aggregate_id === rootId).map(link => link.node_id));
|
|
1394
|
+
let next;
|
|
1395
|
+
try {
|
|
1396
|
+
next = workflowTreeState({ id: rootId, state: raw.state });
|
|
1397
|
+
assertWorkflowTreeRootState(next, manifest, owner.policy, resources);
|
|
1398
|
+
}
|
|
1399
|
+
catch {
|
|
1400
|
+
return refuse('the migrated state does not satisfy the new definition.');
|
|
1401
|
+
}
|
|
1402
|
+
if (next.rootId !== rootId || next.accountId !== 'root' || next.definition !== hashes.definition || next.policy !== previous.policy || next.status !== 'paused' || !same(next.input, previous.input)
|
|
1403
|
+
|| next.maxCostMicros !== previous.maxCostMicros || next.maxCalls !== previous.maxCalls || next.spentMicros !== previous.spentMicros || next.budgetVersion !== previous.budgetVersion || next.output !== null)
|
|
1404
|
+
refuse('only steps and the definition may change.');
|
|
1405
|
+
const oldNodes = new Map(owner.manifest.graph.map(node => [node.id, node]));
|
|
1406
|
+
const newNodes = new Map(manifest.graph.map(node => [node.id, node]));
|
|
1407
|
+
for (const [id, step] of Object.entries(next.steps)) {
|
|
1408
|
+
const before = previous.steps[id];
|
|
1409
|
+
const history = jobbed.has(id) || (before?.child ?? null) !== null;
|
|
1410
|
+
if (before && same(step, before)) {
|
|
1411
|
+
if (history && (!same(oldNodes.get(id), newNodes.get(id)) || !same(owner.resources[id] ?? null, resources[id] ?? null)))
|
|
1412
|
+
refuse(`step "${id}" has execution history and its definition changed.`);
|
|
1413
|
+
continue;
|
|
1414
|
+
}
|
|
1415
|
+
if (history)
|
|
1416
|
+
refuse(`step "${id}" has execution history and cannot change.`);
|
|
1417
|
+
if (before && !['pending', 'waiting', 'approved'].includes(before.status))
|
|
1418
|
+
refuse(`settled step "${id}" can only be carried unchanged.`);
|
|
1419
|
+
if (step.status !== 'pending' || step.receipt !== null || step.approval !== null || step.candidateHash !== null || step.costReserved !== 0 || step.child !== null || step.output !== null)
|
|
1420
|
+
refuse(`step "${id}" must re-enter as a fresh pending step.`);
|
|
1421
|
+
}
|
|
1422
|
+
for (const [id, before] of Object.entries(previous.steps)) {
|
|
1423
|
+
if (Object.hasOwn(next.steps, id))
|
|
1424
|
+
continue;
|
|
1425
|
+
if (jobbed.has(id) || before.child !== null || ['dispatching', 'unknown'].includes(before.status))
|
|
1426
|
+
refuse(`step "${id}" has execution history and cannot be removed.`);
|
|
1427
|
+
}
|
|
1428
|
+
const row = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1429
|
+
if (!row)
|
|
1430
|
+
failed();
|
|
1431
|
+
await tx.query(`UPDATE ${this.backend.prefix}mayura_aggregates SET definition_hash = ? WHERE scope = ? AND id = ?`, [hashes.definition, scope, rootId]);
|
|
1432
|
+
const updated = await writeAggregate(tx, this.backend, { ...row, definition_hash: hashes.definition }, next, [{ type: 'run.migrated', data: { migrationId: raw.migrationId, from: root.manifestHash, to: hashes.definition, actorId: raw.actorId, commandId: raw.commandId } }], await storageClock(tx, this.backend));
|
|
1433
|
+
const data = { format: 4, rootId, manifest, policy: owner.policy, resources };
|
|
1434
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ?, definition_hash = ?, resource_hash = ?, data = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, hashes.definition, hashes.resources, JSON.stringify(data), scope, rootId]);
|
|
1435
|
+
await tx.query(`UPDATE ${this.table()} SET definition_hash = ?, resource_hash = ? WHERE scope = ? AND aggregate_id = ?`, [hashes.definition, hashes.resources, scope, rootId]);
|
|
1436
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1437
|
+
if (!current)
|
|
1438
|
+
failed();
|
|
1439
|
+
return current;
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
async writeRootStatus(tx, scope, rootId, policyHash, state, status, type) {
|
|
1443
|
+
const mutable = structuredClone(state);
|
|
1444
|
+
mutable.status = status;
|
|
1445
|
+
const row = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1446
|
+
if (!row)
|
|
1447
|
+
failed();
|
|
1448
|
+
const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type, data: { status } }], await storageClock(tx, this.backend));
|
|
1449
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]);
|
|
1450
|
+
const current = await this.locked(tx, scope, rootId, policyHash);
|
|
1451
|
+
if (!current)
|
|
1452
|
+
failed();
|
|
1453
|
+
return current;
|
|
1454
|
+
}
|
|
1455
|
+
async recoverExpired(raw) {
|
|
1456
|
+
if (!this.initialized)
|
|
1457
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1458
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1459
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1460
|
+
if (!/^[a-f0-9]{64}$/.test(rootId) || !/^[a-f0-9]{64}$/.test(raw.rootPolicyHash) || !Number.isSafeInteger(raw.limit) || raw.limit < 1 || raw.limit > 128)
|
|
1461
|
+
throw new StorageError('INVALID_INPUT', 'Recovery requires exact bounded identities and limit.');
|
|
1462
|
+
return this.backend.transaction(async (tx) => {
|
|
1463
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1464
|
+
if (!root)
|
|
1465
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1466
|
+
const rootOwner = await this.rootOwner(tx, scope, rootId);
|
|
1467
|
+
const members = await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND root_id = ? ORDER BY aggregate_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1468
|
+
const records = new Map([[rootId, root.record]]);
|
|
1469
|
+
const states = new Map([[rootId, structuredClone(workflowTreeState(root.record))]]);
|
|
1470
|
+
const manifests = new Map([[rootId, rootOwner.manifest]]);
|
|
1471
|
+
for (const member of members.filter(item => item.aggregate_id !== rootId)) {
|
|
1472
|
+
if (member.parent_id !== rootId || !member.node_id)
|
|
1473
|
+
failed();
|
|
1474
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, member.aggregate_id]))[0];
|
|
1475
|
+
if (!ownerRow)
|
|
1476
|
+
failed();
|
|
1477
|
+
let owner;
|
|
1478
|
+
try {
|
|
1479
|
+
owner = JSON.parse(ownerRow.data);
|
|
1480
|
+
owner = { ...owner, manifest: workflowManifest(owner.manifest), policy: workflowTreePolicy(owner.policy), resources: workflowResources(owner.resources, owner.manifest) };
|
|
1481
|
+
}
|
|
1482
|
+
catch {
|
|
1483
|
+
return failed();
|
|
1484
|
+
}
|
|
1485
|
+
const child = await this.childRecord(tx, scope, rootId, rootId, member.node_id, member.aggregate_id, member.account_id, member.definition_hash, member.policy_hash, member.resource_hash, owner.inputHash, root.budget);
|
|
1486
|
+
records.set(member.aggregate_id, child);
|
|
1487
|
+
states.set(member.aggregate_id, structuredClone(workflowTreeState(child)));
|
|
1488
|
+
manifests.set(member.aggregate_id, owner.manifest);
|
|
1489
|
+
}
|
|
1490
|
+
const links = await tx.query(`SELECT * FROM ${this.backend.prefix}mayura_workflow_tree_jobs WHERE scope = ? AND root_id = ? ORDER BY job_id${lockSql(this.backend)}`, [scope, rootId]);
|
|
1491
|
+
if (links.length) {
|
|
1492
|
+
const placeholders = links.map(() => '?').join(',');
|
|
1493
|
+
const parameters = [scope, ...links.map(link => link.job_id)];
|
|
1494
|
+
const lockedJobs = await tx.query(`SELECT job_id FROM ${this.backend.prefix}mayura_scheduler_jobs WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY job_id${lockSql(this.backend)}`, parameters);
|
|
1495
|
+
if (lockedJobs.length !== links.length)
|
|
1496
|
+
failed();
|
|
1497
|
+
await tx.query(`SELECT resource_key FROM ${this.backend.prefix}mayura_scheduler_resources WHERE scope = ? AND job_id IN (${placeholders}) ORDER BY resource_key,job_id${lockSql(this.backend)}`, parameters);
|
|
1498
|
+
}
|
|
1499
|
+
let budget = root.budget;
|
|
1500
|
+
const recovered = [];
|
|
1501
|
+
const touched = new Set();
|
|
1502
|
+
const memberEvents = new Map();
|
|
1503
|
+
const now = await storageClock(tx, this.backend);
|
|
1504
|
+
for (const link of links) {
|
|
1505
|
+
if (recovered.length >= raw.limit)
|
|
1506
|
+
break;
|
|
1507
|
+
const member = members.find(item => item.aggregate_id === link.aggregate_id);
|
|
1508
|
+
const state = states.get(link.aggregate_id);
|
|
1509
|
+
const manifest = manifests.get(link.aggregate_id);
|
|
1510
|
+
const record = records.get(link.aggregate_id);
|
|
1511
|
+
const node = manifest?.graph.find(item => item.id === link.node_id);
|
|
1512
|
+
const step = state?.steps[link.node_id];
|
|
1513
|
+
if (!member || !state || !manifest || !record || !node || node.kind !== 'tool' || !step || step.kind !== 'tool' || link.account_id !== member.account_id || storedInteger(link.cost_micros) !== node.costMicros)
|
|
1514
|
+
failed();
|
|
1515
|
+
const events = memberEvents.get(link.aggregate_id) ?? [];
|
|
1516
|
+
memberEvents.set(link.aggregate_id, events);
|
|
1517
|
+
const integrated = this.scheduler.inSession(tx, link.aggregate_id, events, link.job_id);
|
|
1518
|
+
const prior = await integrated.execute('read', { scope, jobId: link.job_id });
|
|
1519
|
+
if (!prior)
|
|
1520
|
+
failed();
|
|
1521
|
+
const review = step.approval;
|
|
1522
|
+
if (node.approval && prior.startedAtMs === null && ['ready', 'leased'].includes(prior.state)) {
|
|
1523
|
+
if (!review || typeof review.expiresAt !== 'number' || !Number.isSafeInteger(review.expiresAt) || review.humanId === null)
|
|
1524
|
+
failed();
|
|
1525
|
+
if (review.expiresAt <= now) {
|
|
1526
|
+
const job = await integrated.execute('cancel', { scope, jobId: link.job_id, commandId: digest('mayura:workflow-tree-approval-expired:v1', { rootId, memberId: link.aggregate_id, nodeId: link.node_id, jobId: link.job_id, expiresAt: review.expiresAt }) });
|
|
1527
|
+
recovered.push(job);
|
|
1528
|
+
touched.add(link.aggregate_id);
|
|
1529
|
+
budget = await this.budgets.inSession(tx).execute('cancelReservation', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id, reservationId: link.reservation_id });
|
|
1530
|
+
if (step.costReserved !== 0) {
|
|
1531
|
+
state.reservedMicros -= step.costReserved;
|
|
1532
|
+
step.costReserved = 0;
|
|
1533
|
+
}
|
|
1534
|
+
step.status = 'blocked';
|
|
1535
|
+
state.status = 'blocked';
|
|
1536
|
+
events.push({ type: 'approval.expired', data: { nodeId: link.node_id } });
|
|
1537
|
+
continue;
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
const results = await integrated.execute('recover', { scope, limit: 1 });
|
|
1541
|
+
const job = results[0];
|
|
1542
|
+
if (!job)
|
|
1543
|
+
continue;
|
|
1544
|
+
recovered.push(job);
|
|
1545
|
+
touched.add(link.aggregate_id);
|
|
1546
|
+
if (prior.state === 'started') {
|
|
1547
|
+
if (job.state !== 'outcome_unknown')
|
|
1548
|
+
failed();
|
|
1549
|
+
const knownReceipt = prior.receipt !== null && prior.receipt.execution !== 'unknown';
|
|
1550
|
+
if (knownReceipt) {
|
|
1551
|
+
if (step.receipt === null || !same(step.receipt, prior.receipt) || step.costReserved !== 0)
|
|
1552
|
+
failed();
|
|
1553
|
+
step.status = 'blocked';
|
|
1554
|
+
state.status = 'blocked';
|
|
1555
|
+
events.push({ type: 'step.output_blocked', data: { nodeId: link.node_id, reason: 'RECOVERY_OUTPUT_UNAVAILABLE' } });
|
|
1556
|
+
if (link.account_id !== 'root')
|
|
1557
|
+
budget = await this.budgets.inSession(tx).execute('closeSubtree', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id });
|
|
1558
|
+
}
|
|
1559
|
+
else {
|
|
1560
|
+
budget = await this.budgets.inSession(tx).execute('markUnknown', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id, reservationId: link.reservation_id });
|
|
1561
|
+
step.status = 'unknown';
|
|
1562
|
+
state.status = 'outcome_unknown';
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
else if (job.state === 'cancelled') {
|
|
1566
|
+
budget = await this.budgets.inSession(tx).execute('cancelReservation', { scope, id: rootId, policyHash: raw.rootPolicyHash, accountId: link.account_id, reservationId: link.reservation_id });
|
|
1567
|
+
if (step.costReserved !== 0) {
|
|
1568
|
+
state.reservedMicros -= step.costReserved;
|
|
1569
|
+
step.costReserved = 0;
|
|
1570
|
+
}
|
|
1571
|
+
step.status = 'skipped';
|
|
1572
|
+
state.status = 'blocked';
|
|
1573
|
+
}
|
|
1574
|
+
else if (job.state !== 'ready')
|
|
1575
|
+
failed();
|
|
1576
|
+
}
|
|
1577
|
+
if (recovered.length === 0)
|
|
1578
|
+
return { root, members: [], jobs: [] };
|
|
1579
|
+
const updatedMembers = [];
|
|
1580
|
+
for (const childId of [...touched].filter(id => id !== rootId).sort()) {
|
|
1581
|
+
const state = states.get(childId);
|
|
1582
|
+
if (!state)
|
|
1583
|
+
failed();
|
|
1584
|
+
state.budgetVersion = budget.version;
|
|
1585
|
+
const row = await loadAggregate(tx, this.backend, scope, childId);
|
|
1586
|
+
if (!row)
|
|
1587
|
+
failed();
|
|
1588
|
+
const updated = await writeAggregate(tx, this.backend, row, state, memberEvents.get(childId) ?? [], now);
|
|
1589
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, childId]);
|
|
1590
|
+
updatedMembers.push(aggregateRecord(updated));
|
|
1591
|
+
}
|
|
1592
|
+
if (!touched.has(rootId) && budget.version === root.budget.version)
|
|
1593
|
+
return { root, members: updatedMembers, jobs: recovered };
|
|
1594
|
+
const mutableRoot = states.get(rootId);
|
|
1595
|
+
if (!mutableRoot)
|
|
1596
|
+
failed();
|
|
1597
|
+
mutableRoot.budgetVersion = budget.version;
|
|
1598
|
+
const rootEvents = [...(memberEvents.get(rootId) ?? [])];
|
|
1599
|
+
if (budget.version !== root.budget.version)
|
|
1600
|
+
rootEvents.push({ type: 'workflow.budget_updated', data: { recovered: recovered.length } });
|
|
1601
|
+
const rootRow = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1602
|
+
if (!rootRow)
|
|
1603
|
+
failed();
|
|
1604
|
+
const updatedRoot = await writeAggregate(tx, this.backend, rootRow, mutableRoot, rootEvents, now);
|
|
1605
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updatedRoot.version, scope, rootId]);
|
|
1606
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1607
|
+
if (!current)
|
|
1608
|
+
failed();
|
|
1609
|
+
return { root: current, members: updatedMembers, jobs: recovered };
|
|
1610
|
+
});
|
|
1611
|
+
}
|
|
1612
|
+
async joinChild(raw) {
|
|
1613
|
+
if (!this.initialized)
|
|
1614
|
+
throw new StorageError('STORE_NOT_INITIALIZED', 'Initialize workflow-tree storage first.');
|
|
1615
|
+
const scope = identifier(raw.scope, 'Scope');
|
|
1616
|
+
const rootId = identifier(raw.rootId, 'Root');
|
|
1617
|
+
const nodeId = identifier(raw.nodeId, 'Node');
|
|
1618
|
+
if (!/^[a-f0-9]{64}$/.test(rootId) || !/^[a-f0-9]{64}$/.test(raw.rootPolicyHash) || !Number.isSafeInteger(raw.expectedVersion) || raw.expectedVersion < 1)
|
|
1619
|
+
throw new StorageError('INVALID_INPUT', 'Child join requires exact bounded identities.');
|
|
1620
|
+
return this.backend.transaction(async (tx) => {
|
|
1621
|
+
const root = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1622
|
+
if (!root)
|
|
1623
|
+
throw new StorageError('NOT_FOUND', 'Workflow-tree root was not found.');
|
|
1624
|
+
this.schedulable(root);
|
|
1625
|
+
const owner = await this.rootOwner(tx, scope, rootId);
|
|
1626
|
+
const rootState = workflowTreeState(root.record);
|
|
1627
|
+
const step = rootState.steps[nodeId];
|
|
1628
|
+
const node = owner.manifest.graph.find(item => item.id === nodeId);
|
|
1629
|
+
if (!step || step.kind !== 'child' || !node || node.kind !== 'child' || !step.child)
|
|
1630
|
+
conflict();
|
|
1631
|
+
if (step.child.joinedVersion !== null)
|
|
1632
|
+
return root;
|
|
1633
|
+
if (root.record.version !== raw.expectedVersion)
|
|
1634
|
+
conflict();
|
|
1635
|
+
const member = (await tx.query(`SELECT * FROM ${this.table()} WHERE scope = ? AND aggregate_id = ?${lockSql(this.backend)}`, [scope, step.child.runId]))[0];
|
|
1636
|
+
if (!member || member.root_id !== rootId || member.parent_id !== rootId || member.node_id !== nodeId || member.account_id !== step.child.accountId || member.policy_hash !== step.child.policyHash || member.definition_hash !== step.child.definitionHash)
|
|
1637
|
+
failed();
|
|
1638
|
+
const ownerRow = (await tx.query(`SELECT * FROM ${this.owners()} WHERE scope = ? AND aggregate_id = ?`, [scope, step.child.runId]))[0];
|
|
1639
|
+
if (!ownerRow)
|
|
1640
|
+
failed();
|
|
1641
|
+
try {
|
|
1642
|
+
const childOwner = JSON.parse(ownerRow.data);
|
|
1643
|
+
workflowManifest(childOwner.manifest);
|
|
1644
|
+
workflowTreePolicy(childOwner.policy);
|
|
1645
|
+
workflowResources(childOwner.resources, childOwner.manifest);
|
|
1646
|
+
}
|
|
1647
|
+
catch {
|
|
1648
|
+
return failed();
|
|
1649
|
+
}
|
|
1650
|
+
const child = await this.childRecord(tx, scope, rootId, rootId, nodeId, step.child.runId, step.child.accountId, step.child.definitionHash, step.child.policyHash, member.resource_hash, step.child.inputHash, root.budget);
|
|
1651
|
+
const childState = workflowTreeState(child);
|
|
1652
|
+
if (!['succeeded', 'failed', 'blocked', 'cancelled', 'outcome_unknown'].includes(childState.status))
|
|
1653
|
+
conflict();
|
|
1654
|
+
const mutable = structuredClone(rootState);
|
|
1655
|
+
const target = mutable.steps[nodeId];
|
|
1656
|
+
target.child = { ...target.child, joinedVersion: child.version };
|
|
1657
|
+
if (childState.status === 'succeeded') {
|
|
1658
|
+
target.status = 'succeeded';
|
|
1659
|
+
target.output = childState.output;
|
|
1660
|
+
}
|
|
1661
|
+
else {
|
|
1662
|
+
target.status = childState.status === 'outcome_unknown' ? 'unknown' : childState.status === 'blocked' ? 'blocked' : 'failed';
|
|
1663
|
+
mutable.status = target.status === 'unknown' ? 'outcome_unknown' : target.status === 'blocked' ? 'blocked' : 'failed';
|
|
1664
|
+
}
|
|
1665
|
+
let changed = true;
|
|
1666
|
+
while (changed) {
|
|
1667
|
+
changed = false;
|
|
1668
|
+
for (const join of owner.manifest.graph)
|
|
1669
|
+
if (join.kind === 'join') {
|
|
1670
|
+
const current = mutable.steps[join.id];
|
|
1671
|
+
if (current.status === 'pending' && join.dependsOn.every(id => mutable.steps[id]?.status === 'succeeded')) {
|
|
1672
|
+
current.status = 'succeeded';
|
|
1673
|
+
current.output = join.dependsOn.map(id => mutable.steps[id].output);
|
|
1674
|
+
changed = true;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
if (mutable.status === 'waiting')
|
|
1679
|
+
mutable.status = 'running';
|
|
1680
|
+
const row = await loadAggregate(tx, this.backend, scope, rootId);
|
|
1681
|
+
if (!row)
|
|
1682
|
+
failed();
|
|
1683
|
+
const updated = await writeAggregate(tx, this.backend, row, mutable, [{ type: 'workflow.child_joined', data: { nodeId, childId: child.id, status: childState.status } }], await storageClock(tx, this.backend));
|
|
1684
|
+
await tx.query(`UPDATE ${this.owners()} SET aggregate_version = ? WHERE scope = ? AND aggregate_id = ?`, [updated.version, scope, rootId]);
|
|
1685
|
+
const current = await this.locked(tx, scope, rootId, raw.rootPolicyHash);
|
|
1686
|
+
if (!current)
|
|
1687
|
+
failed();
|
|
1688
|
+
return current;
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
//# sourceMappingURL=workflow-tree-database.js.map
|