mayura 0.1.0-dev.0 → 1.0.0-rc.2
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/README.md +152 -35
- package/docs/README.md +82 -0
- package/docs/ai-agents.md +73 -0
- package/docs/cli/dev.md +111 -0
- package/docs/cli/init.md +192 -0
- package/docs/cli/operations.md +235 -0
- package/docs/cli/overview.md +129 -0
- package/docs/cli/run.md +182 -0
- package/docs/concepts/agent.md +197 -0
- package/docs/concepts/costs-and-budgets.md +177 -0
- package/docs/concepts/outcomes.md +154 -0
- package/docs/concepts/permissions.md +155 -0
- package/docs/concepts/runtime.md +172 -0
- package/docs/concepts/tools.md +202 -0
- package/docs/concepts/workflows.md +134 -0
- package/docs/guides/approvals-and-human-input.md +278 -0
- package/docs/guides/artifacts.md +123 -0
- package/docs/guides/child-agents.md +158 -0
- package/docs/guides/code-mode.md +233 -0
- package/docs/guides/deployment.md +644 -0
- package/docs/guides/durable-workflows.md +463 -0
- package/docs/guides/guardrails.md +205 -0
- package/docs/guides/helpers.md +176 -0
- package/docs/guides/lifecycle-hooks.md +187 -0
- package/docs/guides/mcp.md +123 -0
- package/docs/guides/memory-and-context.md +293 -0
- package/docs/guides/model-providers.md +371 -0
- package/docs/guides/model-routing.md +138 -0
- package/docs/guides/observability.md +238 -0
- package/docs/guides/operator-console.md +140 -0
- package/docs/guides/react.md +276 -0
- package/docs/guides/sagas-and-loops.md +212 -0
- package/docs/guides/server-and-client.md +559 -0
- package/docs/guides/skills.md +152 -0
- package/docs/guides/storage.md +251 -0
- package/docs/guides/streaming.md +146 -0
- package/docs/guides/terminal.md +166 -0
- package/docs/guides/testing.md +220 -0
- package/docs/guides/vision.md +155 -0
- package/docs/guides/webhooks.md +172 -0
- package/docs/guides/workflow-composition.md +141 -0
- package/docs/guides/workflow-operations.md +269 -0
- package/docs/installation.md +90 -0
- package/docs/introduction.md +74 -0
- package/docs/project/credits.md +78 -0
- package/docs/project/security.md +178 -0
- package/docs/project/support.md +91 -0
- package/docs/project/versioning.md +122 -0
- package/docs/quickstart.md +190 -0
- package/docs/reference/entry-points.md +146 -0
- package/lib/adapter-code-docker/dist/index.d.ts +8 -1
- package/lib/adapter-code-docker/dist/index.d.ts.map +1 -1
- package/lib/adapter-code-docker/dist/index.js +64 -67
- package/lib/adapter-code-docker/dist/index.js.map +1 -1
- package/lib/adapter-code-docker/dist/options.d.ts +15 -0
- package/lib/adapter-code-docker/dist/options.d.ts.map +1 -0
- package/lib/adapter-code-docker/dist/options.js +56 -0
- package/lib/adapter-code-docker/dist/options.js.map +1 -0
- package/lib/adapter-code-docker/image/Dockerfile +4 -2
- package/lib/adapter-code-docker/package.json +1 -1
- package/lib/adapter-code-docker/src/index.ts +63 -65
- package/lib/adapter-code-docker/src/options.ts +51 -0
- package/lib/adapter-code-quickjs/dist/index.d.ts +16 -4
- package/lib/adapter-code-quickjs/dist/index.d.ts.map +1 -1
- package/lib/adapter-code-quickjs/dist/index.js +94 -50
- package/lib/adapter-code-quickjs/dist/index.js.map +1 -1
- package/lib/adapter-code-quickjs/dist/worker-command.d.ts +27 -0
- package/lib/adapter-code-quickjs/dist/worker-command.d.ts.map +1 -0
- package/lib/adapter-code-quickjs/dist/worker-command.js +66 -0
- package/lib/adapter-code-quickjs/dist/worker-command.js.map +1 -0
- package/lib/adapter-code-quickjs/dist/worker.js +421 -104
- package/lib/adapter-code-quickjs/dist/worker.js.map +1 -1
- package/lib/adapter-code-quickjs/package.json +1 -1
- package/lib/adapter-code-quickjs/src/index.ts +88 -44
- package/lib/adapter-code-quickjs/src/worker-command.ts +68 -0
- package/lib/adapter-code-quickjs/src/worker.ts +315 -89
- package/lib/adapter-mcp/package.json +1 -1
- package/lib/artifacts/dist/contracts.d.ts +6 -2
- package/lib/artifacts/dist/contracts.d.ts.map +1 -1
- package/lib/artifacts/dist/contracts.js.map +1 -1
- package/lib/artifacts/dist/index.d.ts +1 -0
- package/lib/artifacts/dist/index.d.ts.map +1 -1
- package/lib/artifacts/dist/index.js +1 -0
- package/lib/artifacts/dist/index.js.map +1 -1
- package/lib/artifacts/dist/local-store.d.ts.map +1 -1
- package/lib/artifacts/dist/local-store.js +4 -7
- package/lib/artifacts/dist/local-store.js.map +1 -1
- package/lib/artifacts/dist/media.d.ts +11 -0
- package/lib/artifacts/dist/media.d.ts.map +1 -0
- package/lib/artifacts/dist/media.js +18 -0
- package/lib/artifacts/dist/media.js.map +1 -0
- package/lib/artifacts/package.json +1 -1
- package/lib/artifacts/src/contracts.ts +6 -2
- package/lib/artifacts/src/index.ts +1 -0
- package/lib/artifacts/src/local-store.ts +5 -7
- package/lib/artifacts/src/media.ts +20 -0
- package/lib/cli/dist/agent-guide.d.ts +10 -0
- package/lib/cli/dist/agent-guide.d.ts.map +1 -0
- package/lib/cli/dist/agent-guide.js +48 -0
- package/lib/cli/dist/agent-guide.js.map +1 -0
- package/lib/cli/dist/application.d.ts +24 -0
- package/lib/cli/dist/application.d.ts.map +1 -1
- package/lib/cli/dist/application.js +21 -0
- package/lib/cli/dist/application.js.map +1 -1
- package/lib/cli/dist/bin.js +26 -6
- package/lib/cli/dist/bin.js.map +1 -1
- package/lib/cli/dist/dev.d.ts.map +1 -1
- package/lib/cli/dist/dev.js +7 -1
- package/lib/cli/dist/dev.js.map +1 -1
- package/lib/cli/dist/index.d.ts +3 -3
- package/lib/cli/dist/index.d.ts.map +1 -1
- package/lib/cli/dist/index.js +19 -9
- package/lib/cli/dist/index.js.map +1 -1
- package/lib/cli/dist/interactive.d.ts.map +1 -1
- package/lib/cli/dist/interactive.js +52 -11
- package/lib/cli/dist/interactive.js.map +1 -1
- package/lib/cli/dist/operations.d.ts +25 -2
- package/lib/cli/dist/operations.d.ts.map +1 -1
- package/lib/cli/dist/operations.js +125 -12
- package/lib/cli/dist/operations.js.map +1 -1
- package/lib/cli/dist/output.d.ts.map +1 -1
- package/lib/cli/dist/output.js +3 -0
- package/lib/cli/dist/output.js.map +1 -1
- package/lib/cli/dist/providers.d.ts +34 -4
- package/lib/cli/dist/providers.d.ts.map +1 -1
- package/lib/cli/dist/providers.js +26 -5
- package/lib/cli/dist/providers.js.map +1 -1
- package/lib/cli/package.json +1 -1
- package/lib/cli/src/agent-guide.ts +47 -0
- package/lib/cli/src/application.ts +24 -0
- package/lib/cli/src/bin.ts +22 -5
- package/lib/cli/src/dev.ts +5 -1
- package/lib/cli/src/index.ts +18 -10
- package/lib/cli/src/interactive.ts +44 -12
- package/lib/cli/src/operations.ts +82 -12
- package/lib/cli/src/output.ts +3 -0
- package/lib/cli/src/providers.ts +45 -10
- package/lib/cli/starters/approval-workflow/dot-env.example +11 -0
- package/lib/cli/starters/approval-workflow/package.json +1 -2
- package/lib/cli/starters/approval-workflow/src/config.ts +25 -7
- package/lib/cli/starters/approval-workflow/src/intake.ts +1 -2
- package/lib/cli/starters/approval-workflow/src/model.ts +11 -4
- package/lib/cli/starters/approval-workflow/src/workflow.ts +1 -2
- package/lib/cli/starters/cli-agent/README.md +121 -0
- package/lib/cli/starters/cli-agent/dot-env.example +37 -0
- package/lib/cli/starters/cli-agent/dot-github/workflows/ci.yml +13 -0
- package/lib/cli/starters/cli-agent/dot-gitignore +10 -0
- package/lib/cli/starters/cli-agent/mayura.project.json +15 -0
- package/lib/cli/starters/cli-agent/package.json +28 -0
- package/lib/cli/starters/cli-agent/skills/release-notes/SKILL.md +18 -0
- package/lib/cli/starters/cli-agent/skills/release-notes/references/example.md +12 -0
- package/lib/cli/starters/cli-agent/src/assistant.ts +124 -0
- package/lib/cli/starters/cli-agent/src/cli.ts +72 -0
- package/lib/cli/starters/cli-agent/src/config.ts +117 -0
- package/lib/cli/starters/cli-agent/src/model.ts +34 -0
- package/lib/cli/starters/cli-agent/src/workspace.ts +170 -0
- package/lib/cli/starters/cli-agent/test/assistant.test.ts +205 -0
- package/lib/cli/starters/cli-agent/tsconfig.json +20 -0
- package/lib/cli/starters/event-automation/README.md +4 -3
- package/lib/cli/starters/event-automation/dot-env.example +11 -0
- package/lib/cli/starters/event-automation/package.json +1 -2
- package/lib/cli/starters/event-automation/src/config.ts +25 -7
- package/lib/cli/starters/event-automation/src/ingress.ts +3 -5
- package/lib/cli/starters/event-automation/src/model.ts +11 -4
- package/lib/cli/starters/event-automation/src/send-sample.ts +1 -1
- package/lib/cli/starters/event-automation/src/tracker/main.ts +1 -1
- package/lib/cli/starters/event-automation/src/tracker/mcp-server.ts +1 -1
- package/lib/cli/starters/event-automation/src/tracker/tickets.ts +1 -1
- package/lib/cli/starters/event-automation/src/tracker-tools.ts +1 -1
- package/lib/cli/starters/event-automation/src/triage.ts +15 -22
- package/lib/cli/starters/event-automation/src/workflows.ts +1 -1
- package/lib/cli/starters/event-automation/test/automation.test.ts +3 -3
- package/lib/cli/starters/research-team/README.md +2 -2
- package/lib/cli/starters/research-team/dot-env.example +11 -0
- package/lib/cli/starters/research-team/package.json +1 -2
- package/lib/cli/starters/research-team/src/config.ts +25 -7
- package/lib/cli/starters/research-team/src/desk.ts +1 -2
- package/lib/cli/starters/research-team/src/library/index.ts +1 -2
- package/lib/cli/starters/research-team/src/model.ts +11 -4
- package/lib/cli/starters/research-team/src/services.ts +1 -1
- package/lib/cli/starters/research-team/src/team.ts +1 -2
- package/lib/cli/starters/research-team/src/workflow.ts +45 -59
- package/lib/cli/starters/support-agent/dot-env.example +11 -0
- package/lib/cli/starters/support-agent/package.json +1 -2
- package/lib/cli/starters/support-agent/src/assistant.ts +5 -5
- package/lib/cli/starters/support-agent/src/config.ts +25 -7
- package/lib/cli/starters/support-agent/src/guardrails.ts +3 -1
- package/lib/cli/starters/support-agent/src/model.ts +11 -4
- package/lib/cli/starters/support-agent/src/returns.ts +1 -2
- package/lib/cli/starters/support-agent/test/support.test.ts +25 -1
- package/lib/cli/templates/basic-agent.ts +1 -2
- package/lib/cli/templates/capability-policy.ts +1 -2
- package/lib/cli/templates/code-mode-workflow.ts +1 -1
- package/lib/cli/templates/durable-approval.ts +1 -1
- package/lib/cli/templates/parallel-research.ts +1 -2
- package/lib/cli/templates/typed-tool-runner.ts +1 -2
- package/lib/client/dist/forms.d.ts.map +1 -1
- package/lib/client/dist/forms.js +3 -4
- package/lib/client/dist/forms.js.map +1 -1
- package/lib/client/dist/headless.d.ts +3 -1
- package/lib/client/dist/headless.d.ts.map +1 -1
- package/lib/client/dist/headless.js +10 -6
- package/lib/client/dist/headless.js.map +1 -1
- package/lib/client/dist/index.d.ts +95 -6
- package/lib/client/dist/index.d.ts.map +1 -1
- package/lib/client/dist/index.js +397 -124
- package/lib/client/dist/index.js.map +1 -1
- package/lib/client/dist/workflows.d.ts +1 -1
- package/lib/client/dist/workflows.d.ts.map +1 -1
- package/lib/client/dist/workflows.js +7 -6
- package/lib/client/dist/workflows.js.map +1 -1
- package/lib/client/package.json +1 -1
- package/lib/client/src/forms.ts +3 -4
- package/lib/client/src/headless.ts +13 -7
- package/lib/client/src/index.ts +344 -74
- package/lib/client/src/workflows.ts +8 -7
- package/lib/client-react/package.json +1 -1
- package/lib/code-mode/dist/index.d.ts +32 -1
- package/lib/code-mode/dist/index.d.ts.map +1 -1
- package/lib/code-mode/dist/index.js +289 -221
- package/lib/code-mode/dist/index.js.map +1 -1
- package/lib/code-mode/package.json +1 -1
- package/lib/code-mode/src/index.ts +288 -184
- package/lib/code-mode-workflows/package.json +1 -1
- package/lib/context/package.json +1 -1
- package/lib/core/dist/budget.d.ts.map +1 -1
- package/lib/core/dist/budget.js +17 -2
- package/lib/core/dist/budget.js.map +1 -1
- package/lib/core/dist/contracts.d.ts +57 -0
- package/lib/core/dist/contracts.d.ts.map +1 -1
- package/lib/core/dist/contracts.js +36 -0
- package/lib/core/dist/contracts.js.map +1 -1
- package/lib/core/dist/destination.d.ts +12 -0
- package/lib/core/dist/destination.d.ts.map +1 -0
- package/lib/core/dist/destination.js +37 -0
- package/lib/core/dist/destination.js.map +1 -0
- package/lib/core/dist/event-stream.d.ts +1 -1
- package/lib/core/dist/event-stream.d.ts.map +1 -1
- package/lib/core/dist/event-stream.js +7 -0
- package/lib/core/dist/event-stream.js.map +1 -1
- package/lib/core/dist/host.d.ts +3 -0
- package/lib/core/dist/host.d.ts.map +1 -1
- package/lib/core/dist/host.js +3 -0
- package/lib/core/dist/host.js.map +1 -1
- package/lib/core/dist/index.d.ts +2 -0
- package/lib/core/dist/index.d.ts.map +1 -1
- package/lib/core/dist/index.js +2 -0
- package/lib/core/dist/index.js.map +1 -1
- package/lib/core/dist/media.d.ts +98 -0
- package/lib/core/dist/media.d.ts.map +1 -0
- package/lib/core/dist/media.js +227 -0
- package/lib/core/dist/media.js.map +1 -0
- package/lib/core/dist/model-schema.d.ts +37 -0
- package/lib/core/dist/model-schema.d.ts.map +1 -0
- package/lib/core/dist/model-schema.js +154 -0
- package/lib/core/dist/model-schema.js.map +1 -0
- package/lib/core/package.json +1 -1
- package/lib/core/src/budget.ts +17 -2
- package/lib/core/src/contracts.ts +71 -3
- package/lib/core/src/destination.ts +33 -0
- package/lib/core/src/event-stream.ts +5 -1
- package/lib/core/src/host.ts +3 -0
- package/lib/core/src/index.ts +2 -0
- package/lib/core/src/media.ts +228 -0
- package/lib/core/src/model-schema.ts +129 -0
- package/lib/exporter-otlp/dist/otlp-http-json.js +1 -1
- package/lib/exporter-otlp/dist/otlp-http-json.js.map +1 -1
- package/lib/exporter-otlp/dist/signals.js +2 -2
- package/lib/exporter-otlp/dist/signals.js.map +1 -1
- package/lib/exporter-otlp/package.json +1 -1
- package/lib/exporter-otlp/src/otlp-http-json.ts +1 -1
- package/lib/exporter-otlp/src/signals.ts +2 -2
- package/lib/guardrails/package.json +1 -1
- package/lib/helpers/package.json +1 -1
- package/lib/memory/dist/index.js +1 -1
- package/lib/memory/dist/index.js.map +1 -1
- package/lib/memory/dist/native.d.ts.map +1 -1
- package/lib/memory/dist/native.js +4 -3
- package/lib/memory/dist/native.js.map +1 -1
- package/lib/memory/package.json +1 -1
- package/lib/memory/src/index.ts +1 -1
- package/lib/memory/src/native.ts +4 -3
- package/lib/memory-remote/package.json +1 -1
- package/lib/observability/package.json +1 -1
- package/lib/provider-anthropic/dist/index.d.ts +36 -3
- package/lib/provider-anthropic/dist/index.d.ts.map +1 -1
- package/lib/provider-anthropic/dist/index.js +128 -78
- package/lib/provider-anthropic/dist/index.js.map +1 -1
- package/lib/provider-anthropic/package.json +1 -1
- package/lib/provider-anthropic/src/index.ts +152 -76
- package/lib/provider-openai/dist/index.d.ts +71 -4
- package/lib/provider-openai/dist/index.d.ts.map +1 -1
- package/lib/provider-openai/dist/index.js +259 -96
- package/lib/provider-openai/dist/index.js.map +1 -1
- package/lib/provider-openai/package.json +1 -1
- package/lib/provider-openai/src/index.ts +282 -84
- package/lib/runtime/dist/agent.d.ts +18 -1
- package/lib/runtime/dist/agent.d.ts.map +1 -1
- package/lib/runtime/dist/agent.js +59 -3
- package/lib/runtime/dist/agent.js.map +1 -1
- package/lib/runtime/dist/composition.d.ts.map +1 -1
- package/lib/runtime/dist/composition.js +2 -1
- package/lib/runtime/dist/composition.js.map +1 -1
- package/lib/runtime/dist/hooks.d.ts +15 -3
- package/lib/runtime/dist/hooks.d.ts.map +1 -1
- package/lib/runtime/dist/hooks.js.map +1 -1
- package/lib/runtime/dist/index.d.ts +1 -1
- package/lib/runtime/dist/index.d.ts.map +1 -1
- package/lib/runtime/dist/index.js.map +1 -1
- package/lib/runtime/dist/response.d.ts +7 -1
- package/lib/runtime/dist/response.d.ts.map +1 -1
- package/lib/runtime/dist/response.js +23 -2
- package/lib/runtime/dist/response.js.map +1 -1
- package/lib/runtime/dist/router.d.ts +10 -3
- package/lib/runtime/dist/router.d.ts.map +1 -1
- package/lib/runtime/dist/router.js +45 -8
- package/lib/runtime/dist/router.js.map +1 -1
- package/lib/runtime/dist/runtime.d.ts +9 -1
- package/lib/runtime/dist/runtime.d.ts.map +1 -1
- package/lib/runtime/dist/runtime.js +50 -22
- package/lib/runtime/dist/runtime.js.map +1 -1
- package/lib/runtime/package.json +1 -1
- package/lib/runtime/src/agent.ts +69 -3
- package/lib/runtime/src/composition.ts +2 -1
- package/lib/runtime/src/hooks.ts +10 -4
- package/lib/runtime/src/index.ts +1 -1
- package/lib/runtime/src/response.ts +20 -2
- package/lib/runtime/src/router.ts +43 -10
- package/lib/runtime/src/runtime.ts +58 -27
- package/lib/sdk/dist/index.d.ts +9 -1
- package/lib/sdk/dist/index.d.ts.map +1 -1
- package/lib/sdk/dist/index.js +9 -1
- package/lib/sdk/dist/index.js.map +1 -1
- package/lib/sdk/package.json +1 -1
- package/lib/sdk/src/index.ts +9 -1
- package/lib/server/dist/index.d.ts +201 -3
- package/lib/server/dist/index.d.ts.map +1 -1
- package/lib/server/dist/index.js +701 -149
- package/lib/server/dist/index.js.map +1 -1
- package/lib/server/dist/inspector-bundle.d.ts +1 -1
- package/lib/server/dist/inspector-bundle.d.ts.map +1 -1
- package/lib/server/dist/inspector-bundle.js +4 -4
- package/lib/server/dist/inspector-bundle.js.map +1 -1
- package/lib/server/package.json +1 -1
- package/lib/server/src/index.ts +611 -135
- package/lib/server/src/inspector-bundle.ts +4 -4
- package/lib/server-node/dist/hardening.d.ts +1 -1
- package/lib/server-node/dist/hardening.d.ts.map +1 -1
- package/lib/server-node/dist/hardening.js +4 -2
- package/lib/server-node/dist/hardening.js.map +1 -1
- package/lib/server-node/dist/index.d.ts +2 -1
- package/lib/server-node/dist/index.d.ts.map +1 -1
- package/lib/server-node/dist/index.js.map +1 -1
- package/lib/server-node/dist/probe.js +2 -2
- package/lib/server-node/dist/probe.js.map +1 -1
- package/lib/server-node/dist/production.d.ts +8 -1
- package/lib/server-node/dist/production.d.ts.map +1 -1
- package/lib/server-node/dist/production.js +15 -2
- package/lib/server-node/dist/production.js.map +1 -1
- package/lib/server-node/package.json +1 -1
- package/lib/server-node/src/hardening.ts +5 -3
- package/lib/server-node/src/index.ts +2 -1
- package/lib/server-node/src/probe.ts +2 -2
- package/lib/server-node/src/production.ts +23 -3
- package/lib/skills/dist/index.d.ts +81 -0
- package/lib/skills/dist/index.d.ts.map +1 -0
- package/lib/skills/dist/index.js +311 -0
- package/lib/skills/dist/index.js.map +1 -0
- package/lib/skills/package.json +5 -0
- package/lib/skills/src/index.ts +260 -0
- package/lib/storage/dist/index.d.ts +1 -1
- package/lib/storage/dist/index.d.ts.map +1 -1
- package/lib/storage/dist/index.js.map +1 -1
- package/lib/storage/package.json +1 -1
- package/lib/storage/src/index.ts +1 -1
- package/lib/storage-contracts/dist/contracts.d.ts +12 -5
- package/lib/storage-contracts/dist/contracts.d.ts.map +1 -1
- package/lib/storage-contracts/dist/contracts.js +24 -6
- package/lib/storage-contracts/dist/contracts.js.map +1 -1
- package/lib/storage-contracts/dist/index.d.ts +2 -0
- package/lib/storage-contracts/dist/index.d.ts.map +1 -1
- package/lib/storage-contracts/dist/index.js +2 -0
- package/lib/storage-contracts/dist/index.js.map +1 -1
- package/lib/storage-contracts/dist/memory-contracts.js +1 -1
- package/lib/storage-contracts/dist/memory-contracts.js.map +1 -1
- package/lib/storage-contracts/dist/run-records.d.ts +100 -0
- package/lib/storage-contracts/dist/run-records.d.ts.map +1 -0
- package/lib/storage-contracts/dist/run-records.js +464 -0
- package/lib/storage-contracts/dist/run-records.js.map +1 -0
- package/lib/storage-contracts/dist/schema-digest.d.ts +17 -0
- package/lib/storage-contracts/dist/schema-digest.d.ts.map +1 -0
- package/lib/storage-contracts/dist/schema-digest.js +94 -0
- package/lib/storage-contracts/dist/schema-digest.js.map +1 -0
- package/lib/storage-contracts/dist/submission-journal.js +1 -1
- package/lib/storage-contracts/dist/submission-journal.js.map +1 -1
- package/lib/storage-contracts/dist/workflow-format5.d.ts +30 -2
- package/lib/storage-contracts/dist/workflow-format5.d.ts.map +1 -1
- package/lib/storage-contracts/dist/workflow-format5.js +57 -5
- package/lib/storage-contracts/dist/workflow-format5.js.map +1 -1
- package/lib/storage-contracts/package.json +1 -1
- package/lib/storage-contracts/src/contracts.ts +29 -5
- package/lib/storage-contracts/src/index.ts +2 -0
- package/lib/storage-contracts/src/memory-contracts.ts +1 -1
- package/lib/storage-contracts/src/run-records.ts +365 -0
- package/lib/storage-contracts/src/schema-digest.ts +71 -0
- package/lib/storage-contracts/src/submission-journal.ts +1 -1
- package/lib/storage-contracts/src/workflow-format5.ts +70 -6
- package/lib/storage-postgres/dist/index.d.ts +1 -1
- package/lib/storage-postgres/dist/index.d.ts.map +1 -1
- package/lib/storage-postgres/dist/index.js.map +1 -1
- package/lib/storage-postgres/dist/postgres.d.ts +15 -0
- package/lib/storage-postgres/dist/postgres.d.ts.map +1 -1
- package/lib/storage-postgres/dist/postgres.js +18 -7
- package/lib/storage-postgres/dist/postgres.js.map +1 -1
- package/lib/storage-postgres/package.json +1 -1
- package/lib/storage-postgres/src/index.ts +1 -1
- package/lib/storage-postgres/src/postgres.ts +34 -8
- package/lib/storage-sql/dist/aggregate-session.js +1 -1
- package/lib/storage-sql/dist/aggregate-session.js.map +1 -1
- package/lib/storage-sql/dist/scheduled-database.js +1 -1
- package/lib/storage-sql/dist/scheduled-database.js.map +1 -1
- package/lib/storage-sql/dist/workflow-tree-validation.js +1 -1
- package/lib/storage-sql/dist/workflow-tree-validation.js.map +1 -1
- package/lib/storage-sql/package.json +1 -1
- package/lib/storage-sql/src/aggregate-session.ts +1 -1
- package/lib/storage-sql/src/scheduled-database.ts +1 -1
- package/lib/storage-sql/src/workflow-tree-validation.ts +1 -1
- package/lib/storage-sqlite/dist/sqlite-database.js +4 -4
- package/lib/storage-sqlite/dist/sqlite-database.js.map +1 -1
- package/lib/storage-sqlite/dist/sqlite-worker.js +2 -2
- package/lib/storage-sqlite/dist/sqlite-worker.js.map +1 -1
- package/lib/storage-sqlite/dist/sqlite.js +1 -1
- package/lib/storage-sqlite/dist/sqlite.js.map +1 -1
- package/lib/storage-sqlite/package.json +1 -1
- package/lib/storage-sqlite/src/sqlite-database.ts +4 -4
- package/lib/storage-sqlite/src/sqlite-worker.ts +2 -2
- package/lib/storage-sqlite/src/sqlite.ts +1 -1
- package/lib/terminal/dist/index.d.ts +92 -0
- package/lib/terminal/dist/index.d.ts.map +1 -0
- package/lib/terminal/dist/index.js +397 -0
- package/lib/terminal/dist/index.js.map +1 -0
- package/lib/terminal/package.json +5 -0
- package/lib/terminal/src/index.ts +273 -0
- package/lib/testing/dist/index.d.ts +44 -1
- package/lib/testing/dist/index.d.ts.map +1 -1
- package/lib/testing/dist/index.js +63 -10
- package/lib/testing/dist/index.js.map +1 -1
- package/lib/testing/package.json +1 -1
- package/lib/testing/src/index.ts +83 -10
- package/lib/tools/dist/index.d.ts +32 -4
- package/lib/tools/dist/index.d.ts.map +1 -1
- package/lib/tools/dist/index.js +64 -11
- package/lib/tools/dist/index.js.map +1 -1
- package/lib/tools/package.json +1 -1
- package/lib/tools/src/index.ts +74 -13
- package/lib/workflows/dist/abandoned.d.ts +15 -0
- package/lib/workflows/dist/abandoned.d.ts.map +1 -0
- package/lib/workflows/dist/abandoned.js +33 -0
- package/lib/workflows/dist/abandoned.js.map +1 -0
- package/lib/workflows/dist/agent-step.d.ts +83 -0
- package/lib/workflows/dist/agent-step.d.ts.map +1 -0
- package/lib/workflows/dist/agent-step.js +161 -0
- package/lib/workflows/dist/agent-step.js.map +1 -0
- package/lib/workflows/dist/children-runtime.js +8 -8
- package/lib/workflows/dist/children-runtime.js.map +1 -1
- package/lib/workflows/dist/composite-fleet.js +2 -2
- package/lib/workflows/dist/composite-fleet.js.map +1 -1
- package/lib/workflows/dist/composite-host.d.ts.map +1 -1
- package/lib/workflows/dist/composite-host.js +26 -2
- package/lib/workflows/dist/composite-host.js.map +1 -1
- package/lib/workflows/dist/hook-relay.js +2 -2
- package/lib/workflows/dist/hook-relay.js.map +1 -1
- package/lib/workflows/dist/index.d.ts +1 -1
- package/lib/workflows/dist/index.d.ts.map +1 -1
- package/lib/workflows/dist/index.js.map +1 -1
- package/lib/workflows/dist/leadership.js +3 -3
- package/lib/workflows/dist/leadership.js.map +1 -1
- package/lib/workflows/dist/lifecycle-definition.d.ts +26 -4
- package/lib/workflows/dist/lifecycle-definition.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle-definition.js +31 -6
- package/lib/workflows/dist/lifecycle-definition.js.map +1 -1
- package/lib/workflows/dist/lifecycle-fleet.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle-fleet.js +9 -11
- package/lib/workflows/dist/lifecycle-fleet.js.map +1 -1
- package/lib/workflows/dist/lifecycle-host.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle-host.js +26 -2
- package/lib/workflows/dist/lifecycle-host.js.map +1 -1
- package/lib/workflows/dist/lifecycle-human-transport.d.ts +22 -2
- package/lib/workflows/dist/lifecycle-human-transport.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle-human-transport.js +115 -33
- package/lib/workflows/dist/lifecycle-human-transport.js.map +1 -1
- package/lib/workflows/dist/lifecycle-runtime.d.ts +62 -1
- package/lib/workflows/dist/lifecycle-runtime.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle-runtime.js +268 -64
- package/lib/workflows/dist/lifecycle-runtime.js.map +1 -1
- package/lib/workflows/dist/lifecycle.d.ts +6 -4
- package/lib/workflows/dist/lifecycle.d.ts.map +1 -1
- package/lib/workflows/dist/lifecycle.js +2 -0
- package/lib/workflows/dist/lifecycle.js.map +1 -1
- package/lib/workflows/dist/loop-runtime.d.ts.map +1 -1
- package/lib/workflows/dist/loop-runtime.js +38 -26
- package/lib/workflows/dist/loop-runtime.js.map +1 -1
- package/lib/workflows/dist/migration.d.ts +1 -1
- package/lib/workflows/dist/migration.d.ts.map +1 -1
- package/lib/workflows/dist/migration.js +3 -3
- package/lib/workflows/dist/migration.js.map +1 -1
- package/lib/workflows/dist/operator-transports.d.ts +24 -2
- package/lib/workflows/dist/operator-transports.d.ts.map +1 -1
- package/lib/workflows/dist/operator-transports.js +49 -3
- package/lib/workflows/dist/operator-transports.js.map +1 -1
- package/lib/workflows/dist/runtime.d.ts.map +1 -1
- package/lib/workflows/dist/runtime.js +34 -22
- package/lib/workflows/dist/runtime.js.map +1 -1
- package/lib/workflows/dist/saga-runtime.d.ts.map +1 -1
- package/lib/workflows/dist/saga-runtime.js +44 -30
- package/lib/workflows/dist/saga-runtime.js.map +1 -1
- package/lib/workflows/dist/scheduled-helpers.d.ts.map +1 -1
- package/lib/workflows/dist/scheduled-helpers.js +3 -3
- package/lib/workflows/dist/scheduled-helpers.js.map +1 -1
- package/lib/workflows/dist/storage-failure.d.ts +16 -0
- package/lib/workflows/dist/storage-failure.d.ts.map +1 -0
- package/lib/workflows/dist/storage-failure.js +38 -0
- package/lib/workflows/dist/storage-failure.js.map +1 -0
- package/lib/workflows/dist/sweep-position.d.ts +22 -0
- package/lib/workflows/dist/sweep-position.d.ts.map +1 -0
- package/lib/workflows/dist/sweep-position.js +45 -0
- package/lib/workflows/dist/sweep-position.js.map +1 -0
- package/lib/workflows/dist/traces.js +2 -2
- package/lib/workflows/dist/traces.js.map +1 -1
- package/lib/workflows/dist/worker.d.ts +31 -0
- package/lib/workflows/dist/worker.d.ts.map +1 -1
- package/lib/workflows/dist/worker.js +94 -0
- package/lib/workflows/dist/worker.js.map +1 -1
- package/lib/workflows/package.json +1 -1
- package/lib/workflows/src/abandoned.ts +32 -0
- package/lib/workflows/src/agent-step.ts +199 -0
- package/lib/workflows/src/children-runtime.ts +8 -8
- package/lib/workflows/src/composite-fleet.ts +2 -2
- package/lib/workflows/src/composite-host.ts +21 -3
- package/lib/workflows/src/hook-relay.ts +2 -2
- package/lib/workflows/src/index.ts +2 -1
- package/lib/workflows/src/leadership.ts +3 -3
- package/lib/workflows/src/lifecycle-definition.ts +50 -9
- package/lib/workflows/src/lifecycle-fleet.ts +7 -8
- package/lib/workflows/src/lifecycle-host.ts +21 -5
- package/lib/workflows/src/lifecycle-human-transport.ts +110 -26
- package/lib/workflows/src/lifecycle-runtime.ts +270 -60
- package/lib/workflows/src/lifecycle.ts +7 -0
- package/lib/workflows/src/loop-runtime.ts +34 -25
- package/lib/workflows/src/migration.ts +2 -2
- package/lib/workflows/src/operator-transports.ts +48 -4
- package/lib/workflows/src/runtime.ts +28 -22
- package/lib/workflows/src/saga-runtime.ts +38 -28
- package/lib/workflows/src/scheduled-helpers.ts +3 -3
- package/lib/workflows/src/storage-failure.ts +37 -0
- package/lib/workflows/src/sweep-position.ts +52 -0
- package/lib/workflows/src/traces.ts +2 -2
- package/lib/workflows/src/worker.ts +79 -1
- package/lib/workstream/dist/humans.d.ts +7 -3
- package/lib/workstream/dist/humans.d.ts.map +1 -1
- package/lib/workstream/dist/humans.js +13 -2
- package/lib/workstream/dist/humans.js.map +1 -1
- package/lib/workstream/dist/index.js +2 -2
- package/lib/workstream/dist/index.js.map +1 -1
- package/lib/workstream/dist/timers.js +2 -2
- package/lib/workstream/dist/timers.js.map +1 -1
- package/lib/workstream/dist/webhooks.d.ts +6 -2
- package/lib/workstream/dist/webhooks.d.ts.map +1 -1
- package/lib/workstream/dist/webhooks.js +17 -7
- package/lib/workstream/dist/webhooks.js.map +1 -1
- package/lib/workstream/package.json +1 -1
- package/lib/workstream/src/humans.ts +15 -5
- package/lib/workstream/src/index.ts +2 -2
- package/lib/workstream/src/timers.ts +2 -2
- package/lib/workstream/src/webhooks.ts +21 -8
- package/llms-full.txt +9766 -0
- package/llms.txt +92 -0
- package/package.json +18 -6
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Durable workflows"
|
|
3
|
+
description: "Define and run workflows that survive restarts, wait for people and timers, and never repeat a step whose outcome is unknown."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
A durable workflow is a fixed graph of steps whose progress is stored in a database. Each step is recorded before it
|
|
7
|
+
starts and after it finishes, so a run can wait days for an approval or a date, survive a deploy or a crash, and carry
|
|
8
|
+
on in another process. Use one when a sequence of tool calls must finish reliably and must not repeat an effect such as
|
|
9
|
+
a payment or an email.
|
|
10
|
+
|
|
11
|
+
Durable workflows live in `mayura/workflows/lifecycle`. If you have not read [Workflows](../concepts/workflows.md)
|
|
12
|
+
yet, start there for the vocabulary.
|
|
13
|
+
|
|
14
|
+
## A complete example
|
|
15
|
+
|
|
16
|
+
This workflow reserves stock and then sends a confirmation. It runs on SQLite (install `better-sqlite3`).
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { defineTool, z } from 'mayura';
|
|
20
|
+
import { createSqliteStore } from 'mayura/storage-sqlite';
|
|
21
|
+
import { createWorkflowLifecycleRuntime, defineWorkflowLifecycle } from 'mayura/workflows/lifecycle';
|
|
22
|
+
|
|
23
|
+
const order = z.object({ orderId: z.string(), email: z.string() });
|
|
24
|
+
const reservation = z.object({ orderId: z.string(), email: z.string(), reservationId: z.string() });
|
|
25
|
+
|
|
26
|
+
const reserve = defineTool({
|
|
27
|
+
id: 'orders.reserve', version: '1', description: 'Reserve stock for an order.',
|
|
28
|
+
input: order, output: reservation, effects: 'write', capabilities: ['inventory:reserve'],
|
|
29
|
+
execute: async request => ({ ...request, reservationId: `res_${request.orderId}` }),
|
|
30
|
+
});
|
|
31
|
+
const confirm = defineTool({
|
|
32
|
+
id: 'orders.confirm', version: '1', description: 'Email the customer that the order is reserved.',
|
|
33
|
+
input: reservation, output: z.object({ messageId: z.string() }), effects: 'write', capabilities: ['email:send'],
|
|
34
|
+
execute: async request => ({ messageId: `msg_${request.reservationId}` }),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const fulfil = defineWorkflowLifecycle({
|
|
38
|
+
id: 'orders.fulfil',
|
|
39
|
+
version: '1',
|
|
40
|
+
input: order,
|
|
41
|
+
output: z.object({ messageId: z.string() }),
|
|
42
|
+
nodes: [
|
|
43
|
+
{ kind: 'tool', id: 'reserve', tool: reserve, input: { kind: 'input', path: [] } },
|
|
44
|
+
{ kind: 'tool', id: 'confirm', tool: confirm, dependsOn: ['reserve'], input: { kind: 'step', stepId: 'reserve', path: [] } },
|
|
45
|
+
],
|
|
46
|
+
result: { kind: 'step', stepId: 'confirm', path: [] },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const store = createSqliteStore({ filename: 'workflows.sqlite' });
|
|
50
|
+
await store.initialize();
|
|
51
|
+
|
|
52
|
+
const runtime = createWorkflowLifecycleRuntime({
|
|
53
|
+
store,
|
|
54
|
+
scope: { principalId: 'orders-service', projectId: 'shop' },
|
|
55
|
+
permissions: { allow: ['tool:orders.reserve', 'inventory:reserve', 'tool:orders.confirm', 'email:send', 'effect:write'] },
|
|
56
|
+
policyVersion: '1',
|
|
57
|
+
maxCostMicros: 0,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const run = await runtime.submit(fulfil, { input: { orderId: 'o-1001', email: 'ada@example.com' }, idempotencyKey: 'o-1001' });
|
|
62
|
+
const settled = await runtime.runUntilSettled(fulfil, run.id);
|
|
63
|
+
console.log(settled.status, settled.output); // succeeded { messageId: 'msg_res_o-1001' }
|
|
64
|
+
} finally {
|
|
65
|
+
runtime.close();
|
|
66
|
+
await store.close();
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`submit` validates the input and stores a new run. The run id is derived from `idempotencyKey`, so submitting the same
|
|
71
|
+
key again returns the same run instead of starting a second one. `runUntilSettled` then advances the run in the calling
|
|
72
|
+
process until it finishes, waits or is paused.
|
|
73
|
+
|
|
74
|
+
## Runtime options
|
|
75
|
+
|
|
76
|
+
| Option | Required | Meaning |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `store` | yes | An initialized store from `mayura/storage-sqlite` or `mayura/storage-postgres`. |
|
|
79
|
+
| `scope` | yes | `{ principalId, projectId }`. Runs are stored and looked up per scope. |
|
|
80
|
+
| `permissions` | yes | The explicit grants every step needs: `tool:<id>`, each capability the tool declares, and `effect:<kind>` for tools with effects. A step without its grants ends `blocked`. |
|
|
81
|
+
| `policyVersion` | yes | A label for this set of settings, recorded with each run. |
|
|
82
|
+
| `maxCostMicros` | yes | The cost budget of each run, in micros, shared by all its steps. `0` allows only free tools. |
|
|
83
|
+
| `verifyHuman` | for approvals and `respond` | Turns a credential into a verified person. See [Approvals and human input](approvals-and-human-input.md). |
|
|
84
|
+
| `approvalTtlMs` | no | How long an approval request stays valid. Default 1 hour. |
|
|
85
|
+
| `maxOutputBytes` | no | Largest input, step output or result a run stores. Default 1 MiB. |
|
|
86
|
+
| `previousPolicies` | no | The settings of earlier releases (up to 16), so runs they started can finish. See [Changing runtime settings](#changing-runtime-settings). |
|
|
87
|
+
| `now` | no | The clock, for tests. Timers and deadlines use it. |
|
|
88
|
+
|
|
89
|
+
## How a run advances
|
|
90
|
+
|
|
91
|
+
`runUntilSettled` works in waves. Each wave starts every step whose dependencies are done, runs sibling steps in
|
|
92
|
+
parallel, and stores each result. It returns a snapshot when there is nothing left to do right now:
|
|
93
|
+
|
|
94
|
+
| Status | Meaning |
|
|
95
|
+
|---|---|
|
|
96
|
+
| `running` | Work remains; call `runUntilSettled` again. |
|
|
97
|
+
| `waiting` | Waiting for an approval, a person, a timer or a signal. `nextWakeAtMs` is the earliest deadline or due time. |
|
|
98
|
+
| `paused` | An operator paused it. Nothing runs until it is resumed. |
|
|
99
|
+
| `succeeded` | Every step is done and `output` holds the validated result. |
|
|
100
|
+
| `failed` | A step failed, a human request or signal timed out, or the result did not match the output schema. |
|
|
101
|
+
| `blocked` | A step lacked a permission or the run budget could not cover it. |
|
|
102
|
+
| `cancelled` | Cancelled with `runtime.cancel(runId)`. |
|
|
103
|
+
| `outcome_unknown` | A step with effects may or may not have happened. See [Unknown outcomes](#unknown-outcomes). |
|
|
104
|
+
|
|
105
|
+
When a step fails or is blocked, the steps that depend on it are skipped and the run ends. There are no automatic
|
|
106
|
+
retries. The snapshot also has `steps` (each step's status and output) and `budget` (`spentMicros`, `reservedMicros`
|
|
107
|
+
and `maxCostMicros`). `runtime.inspect(runId)` returns the same snapshot without advancing the run, and
|
|
108
|
+
`runtime.events(runId)` returns its stored event log.
|
|
109
|
+
|
|
110
|
+
## Tool steps
|
|
111
|
+
|
|
112
|
+
Before a tool step runs, the runtime checks its grants and reserves the tool's `costMicros` from the run budget. If the
|
|
113
|
+
remaining budget cannot cover it, the step is `blocked`. After the step, the run is charged what the tool reported with
|
|
114
|
+
`context.reportUsage`, or its declared cost if it reported nothing.
|
|
115
|
+
|
|
116
|
+
How a thrown error is recorded depends on the tool's `effects`:
|
|
117
|
+
|
|
118
|
+
- A `none` or `read` tool that throws simply fails the step: it changed nothing outside, so there is nothing to
|
|
119
|
+
reconcile.
|
|
120
|
+
- A `write` or `host` tool that throws is recorded as `unknown`, because it may have acted before failing. The run
|
|
121
|
+
ends `outcome_unknown`.
|
|
122
|
+
- Throw `ToolRefusal` (from `mayura`) when the tool decided not to act, for example because the order does not exist.
|
|
123
|
+
The step fails cleanly, its reserved cost is released, and there is nothing to reconcile.
|
|
124
|
+
|
|
125
|
+
Every step's tool receives a stable `context.callId` (`<runId>/step:<nodeId>`). Pass it, or a business key such as the
|
|
126
|
+
order id, to your provider as an idempotency key.
|
|
127
|
+
|
|
128
|
+
## Agent steps
|
|
129
|
+
|
|
130
|
+
`agentStep` turns an agent into a tool for a step. Each time the step runs, it runs the agent in its own in-process
|
|
131
|
+
runtime with the run's scope, and charges the run what the agent actually spent:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { agentStep } from 'mayura/workflows/lifecycle';
|
|
135
|
+
|
|
136
|
+
const summarize = agentStep(summarizer, {
|
|
137
|
+
id: 'tickets.summarize', // the workflow grants tool:tickets.summarize
|
|
138
|
+
capabilities: ['tickets:summarize'], // and each of these
|
|
139
|
+
permissions: ['model:openai.responses'], // what the agent itself may use
|
|
140
|
+
limits: { maxCostMicros: 50_000, maxDurationMs: 60_000 }, // the step reserves 50,000 from the run budget
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const nodes = [{ kind: 'tool' as const, id: 'summarize', tool: summarize, input: { kind: 'input' as const, path: [] } }];
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The agent's grants are separate from the workflow's: the workflow grants the step (`tool:tickets.summarize`, its
|
|
147
|
+
`capabilities`, and `effect:<kind>` when the agent's tools have effects); the agent gets only `permissions`. The step's
|
|
148
|
+
outcome follows the agent's:
|
|
149
|
+
|
|
150
|
+
| Agent outcome | Step | Charged |
|
|
151
|
+
|---|---|---|
|
|
152
|
+
| `succeeded` | succeeds with the agent's output | what the agent spent |
|
|
153
|
+
| `failed`, `blocked`, `cancelled` | fails; its dependents are skipped | what the agent spent |
|
|
154
|
+
| `outcome_unknown` | `unknown`: the run ends `outcome_unknown` and is left for you to reconcile | its ceiling stays reserved |
|
|
155
|
+
|
|
156
|
+
Cancelling the run or reaching the step's timeout cancels the agent. If the agent's tools can change things (`write`
|
|
157
|
+
or `host` effects), the interrupted step is `unknown`, because the agent may have been in the middle of one. The step's
|
|
158
|
+
timeout defaults to the agent's `maxDurationMs` plus 15 seconds, so the agent's own limit fires first.
|
|
159
|
+
|
|
160
|
+
| Option | Meaning |
|
|
161
|
+
|---|---|
|
|
162
|
+
| `id`, `version`, `description` | The step tool's identity. `version` defaults to the agent's. |
|
|
163
|
+
| `permissions` | Grants for the agent's own model and tool calls. |
|
|
164
|
+
| `limits` | The agent's runtime limits. `maxCostMicros` is the step's ceiling (0 for a model that costs nothing). |
|
|
165
|
+
| `capabilities` | Extra grants the workflow needs to run the step. |
|
|
166
|
+
| `input`, `output`, `prepare`, `finish` | Give the step its own schemas: `prepare` turns the step input into the agent input, and `finish` turns the agent output into the step output, or throws to fail the step (for example after checking citations). |
|
|
167
|
+
| `onRun` | Called with the agent's run handle, for example to trace it. A function it returns is awaited when the run settles. Neither can fail the step. |
|
|
168
|
+
| `timeoutMs` | The step's deadline. |
|
|
169
|
+
|
|
170
|
+
To build the agent for each run, for example with tools that record what this run read, pass a function instead of
|
|
171
|
+
the agent. It then needs `input`, `output`, and `effects`, the strongest effect its tools may have:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
const investigate = agentStep((task: { question: string }) => researcherFor(task.question), {
|
|
175
|
+
id: 'research.investigate', input: taskSchema, output: findingsSchema, effects: 'read',
|
|
176
|
+
permissions: researcherGrants, limits: { maxCostMicros: 20_000 },
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The model loop inside a step is not checkpointed: a crash mid-step does not resume the agent (see
|
|
181
|
+
[Restarts and unknown outcomes](#restarts-and-unknown-outcomes)).
|
|
182
|
+
|
|
183
|
+
## Optional steps
|
|
184
|
+
|
|
185
|
+
Any step can declare `when`, a binding over the input or one of its dependencies. The step runs only when the value is
|
|
186
|
+
something other than `null` or `false`; a path that does not exist counts as `null`. Otherwise the step is bypassed:
|
|
187
|
+
it costs nothing, and its dependents continue and see its output as `null`.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import type { WorkflowLifecycleNode } from 'mayura/workflows/lifecycle';
|
|
191
|
+
|
|
192
|
+
const legalReviewStep: WorkflowLifecycleNode = {
|
|
193
|
+
kind: 'tool', id: 'legal-review', tool: legalReview, dependsOn: ['classify'],
|
|
194
|
+
input: { kind: 'step', stepId: 'classify', path: ['contract'] },
|
|
195
|
+
when: { kind: 'step', stepId: 'classify', path: ['needsLegalReview'] },
|
|
196
|
+
};
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
The condition is decided once, when the step's dependencies are done.
|
|
200
|
+
|
|
201
|
+
## Parallel slots
|
|
202
|
+
|
|
203
|
+
`fanOut` runs one tool per item of an array, in parallel, up to a fixed maximum. It creates slots `<id>.1` to
|
|
204
|
+
`<id>.<max>` and a join `<id>` that collects them. A slot with no item is bypassed and reserves nothing.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { defineWorkflowLifecycle, fanOut } from 'mayura/workflows/lifecycle';
|
|
208
|
+
|
|
209
|
+
const research = defineWorkflowLifecycle({
|
|
210
|
+
id: 'research', version: '1', input: question, output: report,
|
|
211
|
+
nodes: [
|
|
212
|
+
{ kind: 'tool', id: 'plan', tool: plan, input: { kind: 'input', path: [] } },
|
|
213
|
+
// research.1 .. research.4 run `investigate` on plan.assignments[0..3].
|
|
214
|
+
...fanOut({ id: 'research', items: { stepId: 'plan', path: ['assignments'] }, max: 4, tool: investigate }),
|
|
215
|
+
// The join outputs one entry per slot, in order, with null for a bypassed slot.
|
|
216
|
+
{ kind: 'tool', id: 'write', tool: write, dependsOn: ['research'], input: { kind: 'step', stepId: 'research', path: [] } },
|
|
217
|
+
],
|
|
218
|
+
result: { kind: 'step', stepId: 'write', path: [] },
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`max` is at most 64 and is part of the definition. Items beyond `max` are ignored, so cap the array in the step that
|
|
223
|
+
produces it.
|
|
224
|
+
|
|
225
|
+
## Timers
|
|
226
|
+
|
|
227
|
+
A `timer` step waits until an absolute time, in milliseconds since the Unix epoch, read from a binding:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import type { WorkflowLifecycleNode } from 'mayura/workflows/lifecycle';
|
|
231
|
+
|
|
232
|
+
const sendAt: WorkflowLifecycleNode = { kind: 'timer', id: 'send-at', dependsOn: ['draft'], fireAtMs: { kind: 'input', path: ['sendAtMs'] } };
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
While it waits, the run is `waiting` and holds no memory, timer handle or process. Its output is
|
|
236
|
+
`{ fireAtMs, firedAtMs }`. Something must call `runUntilSettled` again at or after `nextWakeAtMs`; in production the
|
|
237
|
+
worker host does this for you.
|
|
238
|
+
|
|
239
|
+
## Waiting for people
|
|
240
|
+
|
|
241
|
+
Two step types stop a run until a person acts:
|
|
242
|
+
|
|
243
|
+
- **Approvals.** Add `approval: true` to a tool step. The run waits until someone approves that exact tool call.
|
|
244
|
+
- **Human requests.** A `human` step asks a typed question (information, a correction or a choice of plan) and
|
|
245
|
+
continues with the validated answer as the step's output.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import type { WorkflowLifecycleNode } from 'mayura/workflows/lifecycle';
|
|
249
|
+
import { z } from 'mayura';
|
|
250
|
+
|
|
251
|
+
const review: WorkflowLifecycleNode = {
|
|
252
|
+
kind: 'human', id: 'review', dependsOn: ['draft'],
|
|
253
|
+
request: {
|
|
254
|
+
kind: 'information',
|
|
255
|
+
schemaId: 'posts.review', // with a Zod response, the schema digest is derived from it
|
|
256
|
+
prompt: 'Is this post ready to publish?',
|
|
257
|
+
response: z.object({ publish: z.boolean(), note: z.string().optional() }),
|
|
258
|
+
context: { kind: 'step', stepId: 'draft', path: [] },
|
|
259
|
+
deadlineAtMs: { kind: 'input', path: ['reviewBy'] },
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Responding, verifying who may answer, and the HTTP, CLI and UI surfaces are covered in
|
|
265
|
+
[Approvals and human input](approvals-and-human-input.md).
|
|
266
|
+
|
|
267
|
+
## Signals
|
|
268
|
+
|
|
269
|
+
A `signal` step waits for an event from another system, such as a payment arriving. The run is `waiting` and holds
|
|
270
|
+
nothing while it waits. The signal's payload is validated with `payload` and becomes the step's output, which later
|
|
271
|
+
steps bind to like any other output:
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
import { defineWorkflowLifecycle } from 'mayura/workflows/lifecycle';
|
|
275
|
+
import { z } from 'mayura';
|
|
276
|
+
|
|
277
|
+
const checkout = defineWorkflowLifecycle({
|
|
278
|
+
id: 'orders.checkout', version: '1', input: order, output: z.object({ messageId: z.string() }),
|
|
279
|
+
nodes: [
|
|
280
|
+
{ kind: 'tool', id: 'reserve', tool: reserve, input: { kind: 'input', path: [] } },
|
|
281
|
+
{ kind: 'signal', id: 'paid', name: 'payment.received', dependsOn: ['reserve'],
|
|
282
|
+
payload: z.object({ amountCents: z.number().int().positive() }),
|
|
283
|
+
deadlineAtMs: { kind: 'input', path: ['payBy'] } },
|
|
284
|
+
{ kind: 'tool', id: 'ship', tool: ship, dependsOn: ['paid'], input: { kind: 'step', stepId: 'paid', path: ['amountCents'] } },
|
|
285
|
+
],
|
|
286
|
+
result: { kind: 'step', stepId: 'ship', path: [] },
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Deliver a signal from code with the runtime (use the fleet runtime in production, so the worker sees the change):
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
await runtime.signal(checkout, {
|
|
294
|
+
id: runId, name: 'payment.received', signalId: 'payment-7731', payload: { amountCents: 4_200 },
|
|
295
|
+
});
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
Operators and other services can deliver it over HTTP with `client.signalWorkflow` or `mayura workflow-signal`; see
|
|
299
|
+
[Operating workflows](workflow-operations.md#signals). Either way:
|
|
300
|
+
|
|
301
|
+
- **Idempotent.** `signalId` identifies the signal. Delivering the same id and payload again changes nothing, so a
|
|
302
|
+
sender can retry freely. The same id with another payload, or a second signal for a step that already has one, is
|
|
303
|
+
refused with `CONFLICT`: a signal step takes exactly one signal.
|
|
304
|
+
- **Validated.** A payload the schema rejects is refused with `INVALID_INPUT` and the step keeps waiting. An unknown
|
|
305
|
+
signal name is `NOT_FOUND`.
|
|
306
|
+
- **Early signals are kept.** A signal that arrives before the step starts (its dependencies are still running) is
|
|
307
|
+
stored with the step and completes it as soon as it starts. If the step is bypassed or the run is cancelled first,
|
|
308
|
+
the signal is dropped with it.
|
|
309
|
+
- **Deadlines.** With `deadlineAtMs`, a step with no signal by then is `timed_out`, the run fails, and later signals
|
|
310
|
+
are refused. A kept early signal counts only if it arrived before the deadline.
|
|
311
|
+
- `name` defaults to the node id and is unique within a definition. The run continues on its next `runUntilSettled`;
|
|
312
|
+
the worker host does this for you.
|
|
313
|
+
|
|
314
|
+
To start a new run for each event instead, see [Webhooks](webhooks.md).
|
|
315
|
+
|
|
316
|
+
## Storage
|
|
317
|
+
|
|
318
|
+
Durable workflows need an initialized store: `createSqliteStore` from `mayura/storage-sqlite` for a single machine, or
|
|
319
|
+
`createPostgresStore` from `mayura/storage-postgres` when several processes share it. Call `store.initialize()` once
|
|
320
|
+
at startup. Every process that touches the same runs must use the same store and scope. The runtime never closes the
|
|
321
|
+
store; you do. See [Storage](storage.md).
|
|
322
|
+
|
|
323
|
+
## Workers
|
|
324
|
+
|
|
325
|
+
In production, one process submits runs and a separate worker advances them. Submit through the fleet runtime, which
|
|
326
|
+
keeps an index of active runs, and run a host that sweeps that index:
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
import { hostname } from 'node:os';
|
|
330
|
+
import { createWorkflowLeadership, createWorkflowWorker } from 'mayura/workflows';
|
|
331
|
+
import { createWorkflowLifecycleFleetRuntime, createWorkflowLifecycleHost } from 'mayura/workflows/lifecycle';
|
|
332
|
+
|
|
333
|
+
const options = { store, scope, permissions: { allow: grants }, policyVersion: '1', maxCostMicros: 500_000 };
|
|
334
|
+
|
|
335
|
+
// Server side: submit (and approve, respond, pause) through the fleet runtime so workers can find the run.
|
|
336
|
+
const runtime = createWorkflowLifecycleFleetRuntime(options);
|
|
337
|
+
await runtime.submit(fulfil, { input: request, idempotencyKey: request.orderId });
|
|
338
|
+
|
|
339
|
+
// Worker side: advance every indexed run on a registered definition, about once a second.
|
|
340
|
+
const host = createWorkflowLifecycleHost({ ...options, definitions: [fulfil], intervalMs: 1_000, maxBackoffMs: 30_000 });
|
|
341
|
+
const worker = createWorkflowWorker({
|
|
342
|
+
units: [host],
|
|
343
|
+
leadership: createWorkflowLeadership({ store, scope, role: 'orders-worker', holderId: `${hostname()}-${process.pid}` }),
|
|
344
|
+
});
|
|
345
|
+
worker.start();
|
|
346
|
+
|
|
347
|
+
// On shutdown: start nothing new, let running steps finish (up to 30 s), then hand over the lease.
|
|
348
|
+
await worker.drain({ timeoutMs: 30_000 });
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
- The host runs waiting runs only when they are due, and skips runs whose definition is not in `definitions`.
|
|
352
|
+
- Leadership is a durable lease: run as many worker replicas as you like, and one advances the fleet at a time. If the
|
|
353
|
+
leader dies, another takes over when its lease expires (15 s by default).
|
|
354
|
+
- In tests, call `host.runOnce()` instead of starting a timer.
|
|
355
|
+
- With `defineMayuraApplication` from `mayura/cli`, return the worker from `worker()` and `mayura worker` runs it. See
|
|
356
|
+
[Running in production](../cli/run.md).
|
|
357
|
+
|
|
358
|
+
## Restarts and unknown outcomes
|
|
359
|
+
|
|
360
|
+
Every state change is written to the store before the next one begins, so after a restart a new process continues each
|
|
361
|
+
run from its last recorded state: waiting steps keep waiting, due timers fire, finished steps are not run again.
|
|
362
|
+
|
|
363
|
+
The one gap is a step that was running when the process died. Mayura records a step as started before it calls the
|
|
364
|
+
tool, so it knows the tool may have acted, but not whether it did. Such a step is never run again. Once the tool's
|
|
365
|
+
`timeoutMs` and a further minute have passed since it started, no live process can still finish it, so the next
|
|
366
|
+
process that advances the run (a worker, or your own `runUntilSettled`) records the step as `unknown`, or `blocked`
|
|
367
|
+
when its receipt shows the tool finished, and the run ends that way too. Until then the run stays unfinished and cannot
|
|
368
|
+
be paused. To record it sooner, after you check the outside system, call `recoverAbandoned`:
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
const snapshot = await runtime.recoverAbandoned(runId);
|
|
372
|
+
// The interrupted step is now `unknown` and the run ends `outcome_unknown`.
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### Unknown outcomes
|
|
376
|
+
|
|
377
|
+
A run ends `outcome_unknown` when a step with effects may or may not have happened: it threw, timed out, or was cut
|
|
378
|
+
off by a crash. Mayura never replays it. Look at the outside system (did the refund go out?), then either finish the
|
|
379
|
+
work by hand or submit a new run. Operators find these runs in the settled view of the run list; see
|
|
380
|
+
[Operating workflows](workflow-operations.md). The best defence is an idempotent tool: pass a stable key to your
|
|
381
|
+
provider so a repeated call is harmless.
|
|
382
|
+
|
|
383
|
+
## Tracing
|
|
384
|
+
|
|
385
|
+
`createWorkflowTraceExport` from `mayura/workflows` exports each settled run as one OpenTelemetry trace, with a span per
|
|
386
|
+
step. It keeps a durable outbox, so a restarted worker neither loses nor duplicates traces. Add its unit to the worker:
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import { createOtlpHttpJsonTraceExporter } from 'mayura/exporter-otlp';
|
|
390
|
+
import { createWorkflowTraceExport, createWorkflowWorker, lifecycleFleetTarget } from 'mayura/workflows';
|
|
391
|
+
|
|
392
|
+
const exporter = createOtlpHttpJsonTraceExporter({ endpoint: 'https://collector.example/v1/traces', serviceName: 'orders' });
|
|
393
|
+
const traces = createWorkflowTraceExport({
|
|
394
|
+
source: host.runtime, store, scope, exportId: 'primary', definitions: [fulfil], sink: exporter.sink,
|
|
395
|
+
});
|
|
396
|
+
const worker = createWorkflowWorker({ units: [host, traces.unit({ targets: [lifecycleFleetTarget(host.runtime)] })] });
|
|
397
|
+
|
|
398
|
+
// Where you submit runs, record them so short runs are not missed:
|
|
399
|
+
await traces.track(runId);
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Spans carry names, ids, times, statuses and budget numbers, never inputs, outputs or prompts. See
|
|
403
|
+
[Observability](observability.md).
|
|
404
|
+
|
|
405
|
+
## Changing runtime settings
|
|
406
|
+
|
|
407
|
+
Every run records the settings it started with: `scope`, `permissions`, `policyVersion`, `maxCostMicros`,
|
|
408
|
+
`maxOutputBytes` and `approvalTtlMs`. A runtime continues a run only if those are its own settings or are listed in
|
|
409
|
+
`previousPolicies`; any other run stops with `CONFLICT`. So when a release changes a setting, for example to grant a
|
|
410
|
+
tool that a new workflow version calls, list the settings the previous release ran with:
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
import { createWorkflowLifecycleFleetRuntime, type WorkflowLifecyclePolicy } from 'mayura/workflows/lifecycle';
|
|
414
|
+
|
|
415
|
+
// Exactly what release 1 passed. Keep it listed until release 1's runs have finished.
|
|
416
|
+
const release1: WorkflowLifecyclePolicy = {
|
|
417
|
+
permissions: { allow: ['tool:orders.reserve', 'inventory:reserve', 'effect:write'] },
|
|
418
|
+
policyVersion: '1',
|
|
419
|
+
maxCostMicros: 500_000,
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const runtime = createWorkflowLifecycleFleetRuntime({
|
|
423
|
+
store,
|
|
424
|
+
scope,
|
|
425
|
+
permissions: { allow: ['tool:orders.reserve', 'inventory:reserve', 'tool:orders.confirm', 'email:send', 'effect:write'] },
|
|
426
|
+
policyVersion: '2',
|
|
427
|
+
maxCostMicros: 500_000,
|
|
428
|
+
previousPolicies: [release1],
|
|
429
|
+
});
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
- **A run keeps its own settings until it finishes.** Its steps are checked against the permissions it started with,
|
|
433
|
+
and it keeps its own budget, output limit and approval lifetime. A grant added later is never given to it: a step
|
|
434
|
+
that needs one ends `blocked`. An approval requested before the deploy can still be approved.
|
|
435
|
+
- **New runs use the current settings.** A child that a saga or loop starts later uses its parent's settings.
|
|
436
|
+
- **To move a run onto the new settings, migrate it** to a new definition version; see
|
|
437
|
+
[Operating workflows](workflow-operations.md). This also works for a run whose settings are no longer listed.
|
|
438
|
+
- `maxOutputBytes` and `approvalTtlMs` default as they do for the runtime, so copy exactly what the old release
|
|
439
|
+
passed. Remove an entry once no active run uses it. Sagas, loops, fleet runtimes and hosts take the same option.
|
|
440
|
+
|
|
441
|
+
## Good to know
|
|
442
|
+
|
|
443
|
+
- **Use the same settings everywhere.** Give the server and the worker the same options object. When a release
|
|
444
|
+
changes the settings, list the old ones in `previousPolicies`; see
|
|
445
|
+
[Changing runtime settings](#changing-runtime-settings).
|
|
446
|
+
- **Never edit a definition that has runs in flight.** Changing any step changes the definition's digest, and runs
|
|
447
|
+
pinned to the old one stop. Add a new `version` instead; see [Operating workflows](workflow-operations.md).
|
|
448
|
+
- **Use the fleet runtime everywhere in production.** Approving, responding, signalling or pausing through a plain
|
|
449
|
+
runtime leaves the worker's index stale.
|
|
450
|
+
- **Errors.** Every error the runtimes and stores throw is a `MayuraError` with a stable `code`. Reusing an
|
|
451
|
+
idempotency key with other input, another definition version or other runtime settings is `CONFLICT`; resubmit the
|
|
452
|
+
identical request to get the existing run. See [Storage](storage.md#errors).
|
|
453
|
+
- **Limits.** At most 128 steps, 32 human steps, 64 timers and 64 signal steps per definition; prompts up to 1 KiB.
|
|
454
|
+
- `runUntilSettled` runs steps in the calling process. A plain runtime starts no background timers; waiting runs move
|
|
455
|
+
only when something calls it again.
|
|
456
|
+
|
|
457
|
+
## Related
|
|
458
|
+
|
|
459
|
+
- [Workflows](../concepts/workflows.md)
|
|
460
|
+
- [Approvals and human input](approvals-and-human-input.md)
|
|
461
|
+
- [Sagas and loops](sagas-and-loops.md)
|
|
462
|
+
- [Operating workflows](workflow-operations.md)
|
|
463
|
+
- [Storage](storage.md)
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Guardrails"
|
|
3
|
+
description: "Check, redact or block what goes into and comes out of an agent with local guards, processor pipelines and model-backed moderation."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
A guard is a check that runs on content at an agent's boundaries: on the input before the model sees it, on every tool
|
|
7
|
+
result before it goes back to the model, and on the final answer before your application receives it. A guard can
|
|
8
|
+
allow the content, block it, or rewrite it (for example to redact an email address). Use guards for policy you want
|
|
9
|
+
enforced on every run, whatever the model decides to do.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { defineAgent, type Guard, z } from 'mayura';
|
|
13
|
+
|
|
14
|
+
const noInternalCodes: Guard = {
|
|
15
|
+
id: 'no-internal-codes',
|
|
16
|
+
check: value => JSON.stringify(value).includes('INTERNAL-') ? { decision: 'block' } : { decision: 'allow' },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const agent = defineAgent({
|
|
20
|
+
id: 'support.assistant',
|
|
21
|
+
version: '1',
|
|
22
|
+
instructions: 'Answer questions about orders.',
|
|
23
|
+
model,
|
|
24
|
+
tools: [],
|
|
25
|
+
input: z.string(),
|
|
26
|
+
output: z.string(),
|
|
27
|
+
guards: { input: [noInternalCodes], output: [noInternalCodes] },
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
When a guard blocks, the run ends with status `blocked` and error code `GUARD_BLOCKED`. When a guard throws, times out
|
|
32
|
+
or returns something other than a valid verdict, the run also ends `blocked`, with `GUARD_UNAVAILABLE`. Nothing is
|
|
33
|
+
released on a failure: guards fail closed.
|
|
34
|
+
|
|
35
|
+
## Where guards run
|
|
36
|
+
|
|
37
|
+
| Position | Checks | Runs |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| `guards.input` | The agent's input, after its schema validated it | Once, before the first model call |
|
|
40
|
+
| `guards.output` | Every tool result the model would see | After each successful tool call |
|
|
41
|
+
| `guards.output` | The final answer, after the output schema validated it | Before the run succeeds |
|
|
42
|
+
|
|
43
|
+
A blocked tool result is withheld: the model never sees it and the run stops. The tool itself already ran, so any
|
|
44
|
+
effect it had (an email sent, a row written) stays done. Put checks that must happen before an effect on the tool
|
|
45
|
+
(below) or in a [lifecycle hook](lifecycle-hooks.md).
|
|
46
|
+
|
|
47
|
+
## Verdicts
|
|
48
|
+
|
|
49
|
+
A guard is an object with an `id` and a `check(value, context)` function, which may be async. `value` is plain JSON.
|
|
50
|
+
`context` carries `runId`, `callId`, `scope`, `boundary` (`'input'` or `'output'`) and an abort `signal`.
|
|
51
|
+
|
|
52
|
+
| Verdict | Effect |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `{ decision: 'allow' }` | The content passes unchanged. |
|
|
55
|
+
| `{ decision: 'block' }` | The run ends `blocked`. An optional `reason` is not shown to callers. |
|
|
56
|
+
| `{ decision: 'rewrite', value }` | `value` replaces the content. |
|
|
57
|
+
|
|
58
|
+
Guards in one list run in the order you declare them, each on the content the previous one allowed or rewrote. A
|
|
59
|
+
rewritten value is validated again against the same schema (the agent's input or output schema, or the tool's output
|
|
60
|
+
schema), so a rewrite can never hand on a value the boundary would refuse. The model sees the rewritten tool result,
|
|
61
|
+
never the original. Each boundary accepts up to 32 guards with unique ids.
|
|
62
|
+
|
|
63
|
+
## Tool guards
|
|
64
|
+
|
|
65
|
+
Tools accept guards too. They run in the tool broker around that one tool, and they only allow or block:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { defineTool, z } from 'mayura';
|
|
69
|
+
|
|
70
|
+
const refund = defineTool({
|
|
71
|
+
id: 'orders.refund', version: '1', description: 'Refund an order.',
|
|
72
|
+
input: z.object({ orderId: z.string(), cents: z.number().int().positive() }),
|
|
73
|
+
output: z.object({ refundId: z.string() }),
|
|
74
|
+
effects: 'write', capabilities: ['orders:refund'],
|
|
75
|
+
guards: { input: [{ id: 'refund-cap', check: value => {
|
|
76
|
+
const cents = (value as { cents?: number }).cents ?? 0;
|
|
77
|
+
return { decision: cents <= 50_000 ? 'allow' : 'block' };
|
|
78
|
+
} }] },
|
|
79
|
+
execute: async ({ orderId }) => ({ refundId: `refund-${orderId}` }),
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A tool input guard runs after the input schema and before `execute`, so a block prevents the effect.
|
|
84
|
+
|
|
85
|
+
## Processors and pipelines
|
|
86
|
+
|
|
87
|
+
`mayura/guardrails` builds reusable content pipelines. A pipeline runs ordered **processors**, which transform the
|
|
88
|
+
content, then all of its **guards** in parallel on the final result. It returns an outcome, and only a successful
|
|
89
|
+
outcome carries the processed content.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { createPipeline, normalizeUserMessage, protectLiterals, redactPII } from 'mayura/guardrails';
|
|
93
|
+
|
|
94
|
+
const pipeline = createPipeline({
|
|
95
|
+
processors: [normalizeUserMessage(), redactPII({ email: true, phone: true })],
|
|
96
|
+
guards: [protectLiterals({ literals: ['BEGIN INTERNAL POLICY'] })],
|
|
97
|
+
timeoutMs: 2_000,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const outcome = await pipeline.process(untrustedMessage, {
|
|
101
|
+
runId: 'run-1', callId: 'message-1', boundary: 'input',
|
|
102
|
+
scope: { principalId: 'customer-42', projectId: 'support' }, signal: AbortSignal.timeout(5_000),
|
|
103
|
+
});
|
|
104
|
+
if (outcome.status === 'succeeded') {
|
|
105
|
+
// Use outcome.output.value, never the original message.
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
| Helper | What it does |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `normalizeUserMessage()` | Turns a string or a `{ content }` envelope into `{ role: 'user', content }`, discarding any role or tool fields a client supplied. |
|
|
112
|
+
| `redactPII({ email, phone })` | Replaces email addresses (on by default) and phone numbers (off by default) in every string with fixed labels such as `[EMAIL]`. |
|
|
113
|
+
| `protectLiterals({ literals })` | A guard that blocks when any configured string appears in a value or key. Optionally case-insensitive. |
|
|
114
|
+
|
|
115
|
+
Pipelines take up to 32 processors and 32 guards, a `timeoutMs` (default 5 seconds) and a `maxBytes` (default 1 MiB).
|
|
116
|
+
An `onBlocked` callback receives metadata about a block, never the content. A processor is any object with an `id`, a
|
|
117
|
+
`version` and `process(snapshot, context)`, which returns the new JSON value from `snapshot.value`.
|
|
118
|
+
|
|
119
|
+
To use a pipeline as an agent guard, wrap it with `pipelineGuard`. It blocks when the pipeline blocks, rewrites when a
|
|
120
|
+
processor changed the content, and allows otherwise:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { createPipeline, pipelineGuard, redactPII } from 'mayura/guardrails';
|
|
124
|
+
|
|
125
|
+
const redact = pipelineGuard('pii.redact', createPipeline({ processors: [redactPII({ email: true, phone: true })] }));
|
|
126
|
+
// Pass it to an agent: guards: { input: [redact], output: [redact] }
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Personal data
|
|
130
|
+
|
|
131
|
+
`redactPII` is a heuristic safety net, not complete PII detection or a compliance guarantee. It can miss unusual
|
|
132
|
+
formats and can flag long digit strings that are not phone numbers. The
|
|
133
|
+
[support-agent starter](https://github.com/KartikeyAI/mayura/blob/main/packages/cli/starters/support-agent/src/guardrails.ts)
|
|
134
|
+
shows a sturdier setup:
|
|
135
|
+
|
|
136
|
+
1. A custom processor redacts payment card numbers (checked with the Luhn algorithm), then `redactPII` handles emails
|
|
137
|
+
and phone numbers.
|
|
138
|
+
2. The agent's input and output schemas run that pipeline, so the model, memory and the reply never hold the raw values.
|
|
139
|
+
3. An output guard re-runs the redaction on every tool result and the final answer and blocks if anything would still
|
|
140
|
+
change, as a backstop.
|
|
141
|
+
|
|
142
|
+
## Model-backed moderation
|
|
143
|
+
|
|
144
|
+
`defineModerationGuard` creates a guard that asks a model for a verdict. The agent's runtime makes the call, charges it
|
|
145
|
+
to the run's budget, and counts it against `maxModelCalls`.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { createRuntime, defineAgent } from 'mayura';
|
|
149
|
+
import { defineModerationGuard } from 'mayura/guardrails';
|
|
150
|
+
|
|
151
|
+
const moderation = defineModerationGuard({
|
|
152
|
+
id: 'content-policy',
|
|
153
|
+
version: '1',
|
|
154
|
+
model: moderationModel,
|
|
155
|
+
instructions: 'Apply our content policy. Return a decision and the categories that apply.',
|
|
156
|
+
egressGuards: [],
|
|
157
|
+
limits: { timeoutMs: 5_000, maxOutputTokens: 256 },
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const agent = defineAgent({ ...options, guards: { input: [moderation], output: [moderation] } });
|
|
161
|
+
|
|
162
|
+
const runtime = createRuntime({
|
|
163
|
+
profile: 'ephemeral',
|
|
164
|
+
permissions: { allow: [`model:${primaryModel.id}`, `model:${moderationModel.id}`] },
|
|
165
|
+
limits: { maxCostMicros: 100_000, maxModelCalls: 12 },
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- The moderation model receives the one piece of content, your instructions, no tools and no conversation. It must
|
|
170
|
+
answer exactly `{ decision: 'allow' | 'block', categories: string[] }`; anything else counts as unavailable and
|
|
171
|
+
blocks.
|
|
172
|
+
- The runtime needs `model:<id>` for the moderation model, and the run cost limit must cover the moderation calls. The
|
|
173
|
+
runtime reserves the cost of the output checks before each model or tool call, so a run cannot start work it cannot
|
|
174
|
+
afford to check.
|
|
175
|
+
- `egressGuards` is required. List local guards that must allow the content before it is sent to the moderation
|
|
176
|
+
provider, or pass `[]` explicitly to send without that check.
|
|
177
|
+
- Default limits: 10 seconds, 64 KiB in and out, 1,024 output tokens.
|
|
178
|
+
- Moderation guards run after the local guards at the same boundary. They allow or block; they never rewrite.
|
|
179
|
+
|
|
180
|
+
For moderation outside an agent run, `createModerationGuard`, `createAuxiliaryCheck` and `detectAndTranslate` take an
|
|
181
|
+
explicit model, `Budget` and permissions, and you call them yourself.
|
|
182
|
+
|
|
183
|
+
## Streaming
|
|
184
|
+
|
|
185
|
+
When an agent streams its answer, each batch of text passes the local guards in `stream.guards` before it is
|
|
186
|
+
released. Text already released cannot be taken back, and the complete answer still passes `guards.output` before the
|
|
187
|
+
run succeeds. See [streaming](streaming.md).
|
|
188
|
+
|
|
189
|
+
## Good to know
|
|
190
|
+
|
|
191
|
+
- Guards are your code running in your process. They are not a sandbox, and a guard can do anything your code can.
|
|
192
|
+
- Nothing here detects prompt injection in general. `protectLiterals` matches exact strings; moderation models can be
|
|
193
|
+
wrong. Keep tool permissions narrow so a manipulated model cannot do much.
|
|
194
|
+
- Input guards see the agent's input, not its instructions or the full prompt. To inspect each model request, use a
|
|
195
|
+
`beforeModelCall` [lifecycle hook](lifecycle-hooks.md).
|
|
196
|
+
- Guard evidence and block callbacks never include the content or the guard's reason, so logs do not leak what was
|
|
197
|
+
blocked.
|
|
198
|
+
|
|
199
|
+
## Related
|
|
200
|
+
|
|
201
|
+
- [Lifecycle hooks](lifecycle-hooks.md)
|
|
202
|
+
- [Streaming](streaming.md)
|
|
203
|
+
- [Agents](../concepts/agent.md)
|
|
204
|
+
- [Outcomes](../concepts/outcomes.md)
|
|
205
|
+
- [Costs and budgets](../concepts/costs-and-budgets.md)
|