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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,+BAA+B,EAC/B,4BAA4B,GAI7B,MAAM,yBAAyB,CAAC;AAejC,MAAM,cAAc,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAC;AAC1C,IAAI,KAAK,GAAG,EAAE,CAAC;AACf,IAAI,OAAO,GAAG,KAAK,CAAC;AACpB,IAAI,OAAmC,CAAC;AACxC,IAAI,OAAmC,CAAC;AACxC,IAAI,YAAuC,CAAC;AAC5C,IAAI,aAAa,GAAG,CAAC,CAAC;AACtB,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkC,CAAC;AAE3D,SAAS,I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AAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE;IACzC,KAAK,IAAI,KAAK,CAAC;IACf,IAAI,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,cAAc,EAAE,CAAC;QAAC,IAAI,EAAE,CAAC;QAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;QAAC,OAAO;IAAC,CAAC;IAC3F,SAAS,CAAC;QACR,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,OAAO,GAAG,CAAC;YAAE,MAAM;QACvB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;AACH,CAAC,CAAC,CAAC;AACH,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,YAAY;IAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC","sourcesContent":["import {\n newQuickJSWASMModuleFromVariant,\n shouldInterruptAfterDeadline,\n type QuickJSContext,\n type QuickJSDeferredPromise,\n type QuickJSRuntime,\n} from 'quickjs-emscripten-core';\n\ninterface StartMessage {\n readonly v: 1;\n readonly type: 'start';\n readonly source: string;\n readonly input: unknown;\n readonly manifest: {\n readonly language: string;\n readonly approvedImports: readonly string[];\n readonly limits: { readonly cpuMillis: number; readonly memoryBytes: number; readonly maxOutputBytes: number };\n };\n}\n\ninterface ToolResultMessage { readonly v: 1; readonly type: 'toolResult'; readonly requestId: number; readonly outcome: unknown }\nconst MAX_LINE_BYTES = 16 * 1_024 * 1_024;\nlet stdin = '';\nlet started = false;\nlet context: QuickJSContext | undefined;\nlet runtime: QuickJSRuntime | undefined;\nlet finalPromise: Promise<void> | undefined;\nlet nextRequestId = 0;\nconst deferred = new Map<number, QuickJSDeferredPromise>();\n\nfunction emit(value: unknown): void {\n try {\n const line = `${JSON.stringify(value)}\\n`;\n if (Buffer.byteLength(line) > MAX_LINE_BYTES) throw new Error();\n process.stdout.write(line);\n } catch { process.exitCode = 1; }\n}\n\nfunction fail(): void {\n emit({ v: 1, type: 'result', status: 'failed' });\n process.exitCode = 1;\n}\n\nfunction dataRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;\n}\n\nfunction start(message: StartMessage): void {\n if (started || message.manifest.language !== 'javascript' || message.manifest.approvedImports.length !== 0) { fail(); return; }\n started = true;\n finalPromise = (async () => {\n try {\n const quickjs = await newQuickJSWASMModuleFromVariant(import('@jitl/quickjs-wasmfile-release-sync'));\n runtime = quickjs.newRuntime();\n runtime.setMemoryLimit(message.manifest.limits.memoryBytes);\n runtime.setMaxStackSize(Math.max(64 * 1_024, Math.min(512 * 1_024, Math.floor(message.manifest.limits.memoryBytes / 4))));\n runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + message.manifest.limits.cpuMillis));\n context = runtime.newContext();\n const bridge = context.newFunction('__mayuraToolCall', payload => {\n try {\n const decoded: unknown = JSON.parse(context!.getString(payload));\n if (!dataRecord(decoded) || typeof decoded['toolId'] !== 'string' || !Object.hasOwn(decoded, 'input')) throw new Error();\n const requestId = ++nextRequestId;\n const pending = context!.newPromise();\n deferred.set(requestId, pending);\n emit({ v: 1, type: 'tool', requestId, toolId: decoded['toolId'], input: decoded['input'] });\n return pending.handle;\n } catch { return context!.undefined; }\n });\n context.setProp(context.global, '__mayuraToolCall', bridge);\n bridge.dispose();\n const input = JSON.stringify(message.input);\n if (input === undefined) throw new Error();\n const wrapped = `(async () => {\n \"use strict\";\n const input = JSON.parse(${JSON.stringify(input)});\n const tools = Object.freeze({ call: async (toolId, value) => JSON.parse(await __mayuraToolCall(JSON.stringify({ toolId, input: value }))) });\n const program = (${message.source});\n if (typeof program !== \"function\") throw new Error(\"invalid program\");\n return JSON.stringify(await program(input, tools));\n })()`;\n const evaluated = context.evalCode(wrapped, 'mayura-code.js');\n if (evaluated.error) { evaluated.error.dispose(); throw new Error(); }\n const promiseHandle = evaluated.value;\n const resolution = context.resolvePromise(promiseHandle);\n runtime.executePendingJobs();\n const result = await resolution;\n promiseHandle.dispose();\n if (result.error) { result.error.dispose(); throw new Error(); }\n const serialized = context.getString(result.value);\n result.value.dispose();\n if (Buffer.byteLength(serialized) > message.manifest.limits.maxOutputBytes * 6 + 2) throw new Error();\n const output: unknown = JSON.parse(serialized);\n emit({ v: 1, type: 'result', status: 'succeeded', output });\n } catch { fail(); }\n finally {\n for (const pending of deferred.values()) pending.dispose();\n deferred.clear();\n context?.dispose();\n runtime?.dispose();\n }\n })();\n}\n\nfunction toolResult(message: ToolResultMessage): void {\n const pending = deferred.get(message.requestId);\n if (!pending || !context || !runtime) { fail(); return; }\n deferred.delete(message.requestId);\n try {\n const serialized = JSON.stringify(message.outcome);\n if (serialized === undefined || Buffer.byteLength(serialized) > MAX_LINE_BYTES) throw new Error();\n const handle = context.newString(serialized);\n pending.resolve(handle);\n handle.dispose();\n const jobs = runtime.executePendingJobs();\n if (jobs.error) { jobs.error.dispose(); fail(); }\n } catch { pending.dispose(); fail(); }\n}\n\nfunction receive(line: string): void {\n try {\n const message: unknown = JSON.parse(line);\n if (!dataRecord(message) || message['v'] !== 1 || typeof message['type'] !== 'string') throw new Error();\n if (message['type'] === 'start') start(message as unknown as StartMessage);\n else if (message['type'] === 'toolResult' && Number.isSafeInteger(message['requestId']) && Object.hasOwn(message, 'outcome')) {\n toolResult(message as unknown as ToolResultMessage);\n } else throw new Error();\n } catch { fail(); }\n}\n\nprocess.stdin.setEncoding('utf8');\nprocess.stdin.on('data', (chunk: string) => {\n stdin += chunk;\n if (Buffer.byteLength(stdin) > MAX_LINE_BYTES) { fail(); process.stdin.destroy(); return; }\n for (;;) {\n const newline = stdin.indexOf('\\n');\n if (newline < 0) break;\n const line = stdin.slice(0, newline); stdin = stdin.slice(newline + 1);\n receive(line);\n }\n});\nprocess.stdin.once('end', () => { if (!finalPromise) fail(); });\n"]}
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,oHAAoH;AACpH,8GAA8G;AAC9G,OAAO,EACL,+BAA+B,EAC/B,UAAU,GAMX,MAAM,yBAAyB,CAAC;AAuBjC,qGAAqG;AACrG,MAAM,gBAAgB,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAC;AAC5C,iFAAiF;AACjF,MAAM,oBAAoB,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAC;AAChD,0EAA0E;AAC1E,MAAM,aAAa,GAAG,KAAK,CAAC;AAC5B,MAAM,SAAS,GAAG,MAAM,CAAC;AACzB;;;GAGG;AACH,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B;;;;GAIG;AACH,MAAM,eAAe,GAAG,GAAG,GAAG,KAAK,CAAC;AAEpC,6GAA6G;AAC7G,0GAA0G;AAC1G,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA6BX,CAAC;AAEN,IAAI,OAAO,GAAG,EAAE,CAAC;AACjB,IAAI,OAAO,GAAG,KAAK,CAAC;AACpB,IAAI,QAAQ,GAAG,KAAK,CAAC;AACrB,IAAI,OAAmC,CAAC;AACxC,IAAI,OAAmC,CAAC;AACxC,IAAI,GAA8B,CAAC;AACnC,IAAI,QAAmC,CAAC;AACxC,IAAI,cAAyC,CAAC;AAC9C,IAAI,MAA0B,CAAC;AAC/B,IAAI,aAAa,GAAG,CAAC,CAAC;AACtB,IAAI,SAAS,GAAG,CAAC,CAAC;AAClB,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkC,CAAC;AAE3D;;;GAGG;AACH,IAAI,KAAmC,C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,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QAAC,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;QAC7E,OAAO,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;AACH,CAAC,CAAC,CAAC;AACH,mGAAmG;AACnG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC","sourcesContent":["// Disposable QuickJS worker: one process runs exactly one program, then the host kills it.\n// Guest code runs only inside the QuickJS WebAssembly interpreter. This file is the trusted glue around it: it owns\n// the metering, the tool bridge and the result encoding, and never evaluates guest text with the host engine.\nimport {\n newQuickJSWASMModuleFromVariant,\n newVariant,\n type QuickJSContext,\n type QuickJSDeferredPromise,\n type QuickJSHandle,\n type QuickJSRuntime,\n type QuickJSSyncVariant,\n} from 'quickjs-emscripten-core';\n\ntype FailureReason = 'program_error' | 'cpu_limit' | 'memory_limit' | 'invalid_output' | 'unsupported_program' | 'sandbox_error';\n\ninterface Limits {\n readonly cpuMillis: number;\n readonly wallTimeMillis: number;\n readonly memoryBytes: number;\n readonly maxOutputBytes: number;\n readonly maxToolInputBytes: number;\n readonly maxToolCalls: number;\n}\n\ninterface StartMessage {\n readonly v: 1;\n readonly type: 'start';\n readonly source: string;\n readonly input: unknown;\n readonly manifest: { readonly language: string; readonly approvedImports: readonly string[]; readonly limits: Limits };\n}\n\ninterface ToolResultMessage { readonly v: 1; readonly type: 'toolResult'; readonly requestId: number; readonly outcome: unknown }\n\n/** The start line carries up to 1 MiB of source and 16 MiB of input; JSON escaping can grow both. */\nconst MAX_INBOUND_LINE = 64 * 1_024 * 1_024;\n/** Unsent protocol output the worker will hold while the host is not reading. */\nconst MAX_OUTBOUND_BACKLOG = 64 * 1_024 * 1_024;\n/** Room for the `{\"toolId\":…,\"input\":…}` envelope around a tool input. */\nconst TOOL_ENVELOPE = 1_024;\nconst WASM_PAGE = 65_536;\n/**\n * The interpreter's own WebAssembly memory before any program runs: static data, its C stack and runtime state.\n * The program's heap may grow the memory by `memoryBytes` beyond this and no further.\n */\nconst INTERPRETER_PAGES = 256;\nconst MAX_WASM_PAGES = 32_768;\n/**\n * QuickJS checks its recursion depth against its own stack, but QuickJS's C recursion also uses the host engine's\n * native stack. 256 KiB of QuickJS stack (about 1,500 plain JavaScript frames) stays below the interpreter's real\n * C stack; a deep native recursion that exhausts the host stack first is caught below as a stack overflow.\n */\nconst MAX_STACK_BYTES = 256 * 1_024;\n\n// The prelude runs before any program code, so the intrinsics it captures cannot be replaced by the program.\n// It removes the host bridge from the global object; the program reaches tools only through `tools.call`.\nconst PRELUDE = `(() => {\n \"use strict\";\n const bridge = globalThis.__mayuraToolCall;\n delete globalThis.__mayuraToolCall;\n const { stringify, parse } = JSON;\n const freeze = Object.freeze;\n const StringValue = String;\n const cut = Function.prototype.call.bind(String.prototype.slice);\n const text = (value, max) => typeof value === \"string\" ? cut(value, 0, max) : undefined;\n const tools = freeze({ call: freeze(async (toolId, input) => parse(await bridge(stringify({ toolId, input })))) });\n const run = async (program, inputText) => {\n if (typeof program !== \"function\") throw new TypeError(\"The program source must be a function expression, such as (input, tools) => output.\");\n const result = await program(parse(inputText), tools);\n let output;\n try { output = stringify(result); } catch { return undefined; }\n return output;\n };\n const describe = error => {\n if (error !== null && (typeof error === \"object\" || typeof error === \"function\")) {\n let name; let message;\n try { name = text(error.name, 128); } catch {}\n try { message = text(error.message, 1024); } catch {}\n return stringify({ name: name ?? \"Error\", message: message ?? \"\" });\n }\n let message = \"\";\n try { message = cut(StringValue(error), 0, 1024); } catch {}\n return stringify({ name: \"Error\", message });\n };\n return freeze([run, describe]);\n})()`;\n\nlet inbound = '';\nlet started = false;\nlet finished = false;\nlet context: QuickJSContext | undefined;\nlet runtime: QuickJSRuntime | undefined;\nlet run: QuickJSHandle | undefined;\nlet describe: QuickJSHandle | undefined;\nlet programPromise: QuickJSHandle | undefined;\nlet limits: Limits | undefined;\nlet nextRequestId = 0;\nlet toolCalls = 0;\nconst deferred = new Map<number, QuickJSDeferredPromise>();\n\n/**\n * Set when the interpreter was unwound by the host engine itself, for example a native stack overflow or a\n * WebAssembly trap. Its memory may then be inconsistent, so it is never entered or torn down again.\n */\nlet crash: 'stack' | 'trap' | undefined;\nclass InterpreterCrash extends Error {}\n\n// Metering. `cpuMillis` counts only time spent inside the interpreter (monotonic clock), not time waiting for\n// tool calls. The wall deadline also stops a process whose host has gone away.\nlet usedMillis = 0;\nlet sliceStart: number | undefined;\nlet stopped: 'cpu' | 'wall' | undefined;\nlet wallDeadline = Number.POSITIVE_INFINITY;\n\nfunction interrupt(): boolean {\n if (stopped) return true;\n const now = performance.now();\n if (now >= wallDeadline) stopped = 'wall';\n else if (sliceStart !== undefined && usedMillis + (now - sliceStart) > limits!.cpuMillis) stopped = 'cpu';\n return stopped !== undefined;\n}\n\n/** Every entry into the interpreter goes through here: it meters CPU time and contains host-level unwinding. */\nfunction guest<T>(work: () => T): T {\n if (crash) throw new InterpreterCrash();\n if (sliceStart !== undefined) return work();\n sliceStart = performance.now();\n try { return work(); }\n catch (error) {\n // QuickJS reports program errors as results, never as host exceptions. A host exception here means the\n // interpreter itself was unwound.\n crash = error instanceof RangeError ? 'stack' : 'trap';\n throw new InterpreterCrash();\n } finally { usedMillis += performance.now() - sliceStart; sliceStart = undefined; }\n}\n\n/** Set when the interpreter asked for more WebAssembly memory than memoryBytes allows: the program hit its limit. */\nlet growthRefused = false;\nconst memoryExhausted = (): boolean => growthRefused;\n\nfunction emit(value: unknown): boolean {\n try {\n const line = `${JSON.stringify(value)}\\n`;\n if (process.stdout.writableLength + Buffer.byteLength(line) > MAX_OUTBOUND_BACKLOG) return false;\n process.stdout.write(line);\n return true;\n } catch { return false; }\n}\n\nfunction dispose(): void {\n if (crash) { context = undefined; runtime = undefined; return; }\n for (const pending of deferred.values()) { try { pending.dispose(); } catch { /* Runtime teardown follows. */ } }\n deferred.clear();\n for (const handle of [programPromise, run, describe]) { try { if (handle?.alive) handle.dispose(); } catch { /* Teardown only. */ } }\n try { context?.dispose(); } catch { /* A failed teardown still ends with process exit. */ }\n try { runtime?.dispose(); } catch { /* A failed teardown still ends with process exit. */ }\n context = undefined; runtime = undefined;\n}\n\nfunction finish(result: { readonly status: 'succeeded'; readonly output: unknown } | { readonly status: 'failed'; readonly reason: FailureReason; readonly programError?: unknown }): void {\n if (finished) return;\n finished = true;\n if (!emit({ v: 1, type: 'result', ...result })) process.exitCode = 1;\n if (result.status === 'failed') process.exitCode = 1;\n dispose();\n}\n\nconst fail = (reason: FailureReason, programError?: unknown): void => finish({ status: 'failed', reason, ...(programError ? { programError } : {}) });\n\n/** Reports an execution whose interpreter can no longer be entered. */\nfunction abandon(): void {\n if (stopped === 'cpu') { fail('cpu_limit'); return; }\n if (crash === 'stack') { fail('program_error', { name: 'InternalError', message: 'stack overflow' }); return; }\n fail(memoryExhausted() ? 'memory_limit' : 'sandbox_error');\n}\n\n/** Classifies a thrown guest value. The metering flags are authoritative; the program can only choose its own error text. */\nfunction failWith(error: QuickJSHandle | undefined): void {\n if (stopped === 'cpu') { error?.dispose(); fail('cpu_limit'); return; }\n if (stopped === 'wall') { error?.dispose(); fail('sandbox_error'); return; }\n let described: { name: string; message: string } | undefined;\n if (error && context && describe) {\n try {\n const result = guest(() => context!.callFunction(describe!, context!.undefined, error));\n if (result.error) result.error.dispose();\n else {\n if (context.typeof(result.value) === 'string') {\n const parsed: unknown = JSON.parse(context.getString(result.value));\n if (parsed && typeof parsed === 'object' && typeof (parsed as Record<string, unknown>)['name'] === 'string'\n && typeof (parsed as Record<string, unknown>)['message'] === 'string') described = parsed as { name: string; message: string };\n }\n result.value.dispose();\n }\n } catch { /* The error stays undescribed. */ }\n }\n if (crash) { abandon(); return; }\n error?.dispose();\n if (stopped === 'cpu') { fail('cpu_limit'); return; }\n // A refused memory growth is authoritative: whatever the program then threw (QuickJS may even throw null when it\n // cannot allocate the error), it failed after reaching memoryBytes.\n if (memoryExhausted()) {\n fail('memory_limit'); return;\n }\n fail('program_error', described);\n}\n\n/** Runs queued guest jobs, then settles the execution if the program finished, stalled or was stopped. */\nfunction pump(): void {\n try { settle(); } catch { abandon(); }\n}\n\nfunction settle(): void {\n if (finished || !runtime || !context || !programPromise) return;\n const jobs = guest(() => runtime!.executePendingJobs());\n if (jobs.error) { failWith(jobs.error); return; }\n const state = context.getPromiseState(programPromise);\n if (state.type === 'fulfilled') {\n const value = state.value;\n try {\n if (context.typeof(value) !== 'string') { fail('invalid_output'); return; }\n // UTF-8 is never shorter than the UTF-16 length, so an oversized string is rejected before it is copied out.\n const lengthHandle = context.getProp(value, 'length');\n const length = context.getNumber(lengthHandle); lengthHandle.dispose();\n if (!(length <= limits!.maxOutputBytes)) { fail('invalid_output'); return; }\n const serialized = context.getString(value);\n if (Buffer.byteLength(serialized) > limits!.maxOutputBytes) { fail('invalid_output'); return; }\n finish({ status: 'succeeded', output: JSON.parse(serialized) as unknown });\n } catch { fail('invalid_output'); }\n finally { if (value.alive) value.dispose(); }\n return;\n }\n if (state.type === 'rejected') { failWith(state.error); return; }\n if (stopped) { failWith(undefined); return; }\n // Nothing queued and no tool call outstanding: the program's promise can never settle.\n if (deferred.size === 0 && !runtime.hasPendingJob()) fail('program_error');\n}\n\nfunction resolveLocally(pending: QuickJSDeferredPromise, outcome: unknown): void {\n guest(() => {\n const handle = context!.newString(JSON.stringify(outcome));\n try { pending.resolve(handle); } finally { handle.dispose(); }\n });\n}\n\nfunction refused(code: 'INVALID_INPUT' | 'LIMIT_EXCEEDED'): unknown {\n return code === 'INVALID_INPUT'\n ? { status: 'failed', error: { code, message: 'The tool input was rejected: it is not plain JSON within maxToolInputBytes, or it does not match the tool schema.' } }\n : { status: 'failed', error: { code, message: 'The tool call exceeded a limit, such as the program\\'s maxToolCalls or maxToolConcurrency.' } };\n}\n\n/** Host function behind `tools.call`. It never throws into the guest; every call returns a promise. */\nfunction toolCall(payload: QuickJSHandle): QuickJSHandle {\n const pending = context!.newPromise();\n try {\n if (finished) { resolveLocally(pending, refused('LIMIT_EXCEEDED')); return pending.handle; }\n if (context!.typeof(payload) !== 'string') { resolveLocally(pending, refused('INVALID_INPUT')); return pending.handle; }\n const lengthHandle = context!.getProp(payload, 'length');\n const length = context!.getNumber(lengthHandle); lengthHandle.dispose();\n if (!(length <= limits!.maxToolInputBytes + TOOL_ENVELOPE)) { resolveLocally(pending, refused('INVALID_INPUT')); return pending.handle; }\n // The host enforces the same bound; answering here keeps a runaway loop from flooding the protocol.\n if (toolCalls >= limits!.maxToolCalls) { resolveLocally(pending, refused('LIMIT_EXCEEDED')); return pending.handle; }\n const decoded: unknown = JSON.parse(context!.getString(payload));\n if (!decoded || typeof decoded !== 'object' || Array.isArray(decoded) || typeof (decoded as Record<string, unknown>)['toolId'] !== 'string') {\n resolveLocally(pending, refused('INVALID_INPUT')); return pending.handle;\n }\n toolCalls++;\n const requestId = ++nextRequestId;\n deferred.set(requestId, pending);\n const input = (decoded as Record<string, unknown>)['input'];\n if (!emit({ v: 1, type: 'tool', requestId, toolId: (decoded as Record<string, unknown>)['toolId'], input: input === undefined ? null : input })) {\n deferred.delete(requestId);\n resolveLocally(pending, refused('LIMIT_EXCEEDED'));\n setImmediate(() => fail('sandbox_error'));\n }\n return pending.handle;\n } catch {\n try { resolveLocally(pending, refused('INVALID_INPUT')); } catch { /* Interrupted: the metering flag reports it. */ }\n return pending.handle;\n }\n}\n\nasync function start(message: StartMessage): Promise<void> {\n limits = message.manifest.limits;\n wallDeadline = performance.now() + limits.wallTimeMillis;\n // A worker whose host vanished stops itself at the wall deadline even while waiting for a tool result.\n setTimeout(() => { stopped ??= 'wall'; process.exit(1); }, limits.wallTimeMillis + 1_000).unref();\n if (message.manifest.language !== 'javascript' || message.manifest.approvedImports.length !== 0) { fail('unsupported_program'); return; }\n // This QuickJS build cannot measure allocation sizes (no malloc_usable_size), so its own memory limit would count\n // allocations rather than bytes: it would fail programs with many small objects and not bound large ones. It is\n // left unset. The hard bound is the WebAssembly memory itself: it cannot grow past the interpreter's base plus\n // memoryBytes, and an allocation beyond that fails inside QuickJS as \"out of memory\".\n const maximumPages = Math.min(MAX_WASM_PAGES, INTERPRETER_PAGES + Math.ceil(limits.memoryBytes / WASM_PAGE));\n const memory = new WebAssembly.Memory({ initial: INTERPRETER_PAGES, maximum: maximumPages });\n // Emscripten grows the heap through this method; a refusal is the authoritative sign of an exhausted limit.\n const grow = memory.grow.bind(memory);\n Object.defineProperty(memory, 'grow', { value: (delta: number): number => {\n try { return grow(delta); } catch (error) { growthRefused = true; throw error; }\n } });\n const variant = (await import('@jitl/quickjs-wasmfile-release-sync')).default as unknown as QuickJSSyncVariant;\n const quickjs = await newQuickJSWASMModuleFromVariant(newVariant(variant, { wasmMemory: memory }));\n runtime = quickjs.newRuntime();\n runtime.setMaxStackSize(Math.max(64 * 1_024, Math.min(MAX_STACK_BYTES, Math.floor(limits.memoryBytes / 4))));\n runtime.setInterruptHandler(interrupt);\n context = runtime.newContext();\n const bridge = context.newFunction('__mayuraToolCall', toolCall);\n context.setProp(context.global, '__mayuraToolCall', bridge);\n bridge.dispose();\n const prelude = guest(() => context!.evalCode(PRELUDE, 'mayura-prelude.js', { type: 'global', strict: true }));\n if (prelude.error) { prelude.error.dispose(); fail(stopped === 'cpu' ? 'cpu_limit' : 'sandbox_error'); return; }\n run = context.getProp(prelude.value, 0);\n describe = context.getProp(prelude.value, 1);\n prelude.value.dispose();\n // The trailing newline keeps a final line comment in the source from swallowing the closing parenthesis.\n const compiled = guest(() => context!.evalCode(`(${message.source}\\n)`, 'program.js', { type: 'global', strict: true }));\n if (compiled.error) { failWith(compiled.error); return; }\n const inputText = context.newString(JSON.stringify(message.input) ?? 'null');\n const called = guest(() => context!.callFunction(run!, context!.undefined, compiled.value, inputText));\n compiled.value.dispose(); inputText.dispose();\n if (called.error) { failWith(called.error); return; }\n programPromise = called.value;\n pump();\n}\n\nfunction toolResult(message: ToolResultMessage): void {\n if (finished) return;\n const pending = deferred.get(message.requestId);\n if (!pending || !context) { fail('sandbox_error'); return; }\n deferred.delete(message.requestId);\n try {\n const serialized = JSON.stringify(message.outcome);\n if (serialized === undefined || Buffer.byteLength(serialized) > MAX_INBOUND_LINE) throw new Error('invalid outcome');\n resolveLocally(pending, message.outcome);\n } catch {\n if (crash) abandon();\n else { pending.dispose(); fail('sandbox_error'); }\n return;\n }\n pump();\n}\n\nfunction receive(line: string): void {\n try {\n const message: unknown = JSON.parse(line);\n if (!message || typeof message !== 'object' || Array.isArray(message)) throw new Error('invalid message');\n const record = message as Record<string, unknown>;\n if (record['v'] !== 1) throw new Error('invalid message');\n if (record['type'] === 'start' && !started) {\n started = true;\n void start(message as StartMessage).catch(() => abandon());\n } else if (record['type'] === 'toolResult' && started && Number.isSafeInteger(record['requestId']) && Object.hasOwn(record, 'outcome')) {\n toolResult(message as ToolResultMessage);\n } else throw new Error('invalid message');\n } catch { fail('sandbox_error'); }\n}\n\nprocess.stdin.setEncoding('utf8');\nprocess.stdin.on('data', (chunk: string) => {\n inbound += chunk;\n if (inbound.length > MAX_INBOUND_LINE) { fail('sandbox_error'); process.stdin.destroy(); return; }\n for (;;) {\n const newline = inbound.indexOf('\\n');\n if (newline < 0) break;\n const line = inbound.slice(0, newline); inbound = inbound.slice(newline + 1);\n receive(line);\n }\n});\n// The host closes stdin when it is done or gone; either way nothing can use this process any more.\nprocess.stdin.once('end', () => { process.exit(finished ? process.exitCode ?? 0 : 1); });\n"]}
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { MayuraError } from '@mayura/core';
|
|
5
|
+
import {
|
|
6
|
+
defineSandboxAdapter,
|
|
7
|
+
type CodeToolOutcome,
|
|
8
|
+
type SandboxAdapter,
|
|
9
|
+
type SandboxExecutionRequest,
|
|
10
|
+
type SandboxExecutionResult,
|
|
11
|
+
type SandboxFailureReason,
|
|
12
|
+
type SandboxQualification,
|
|
13
|
+
} from '@mayura/code-mode';
|
|
14
|
+
import { workerCommand, workerReadRoots } from './worker-command.js';
|
|
6
15
|
|
|
7
16
|
/** Minimal transport contract so outer adapters do not expose Node types to consumers. */
|
|
8
17
|
export interface QuickJsChildProcess {
|
|
@@ -17,22 +26,33 @@ export interface QuickJsChildProcess {
|
|
|
17
26
|
|
|
18
27
|
export interface QuickJsWorkerProcess {
|
|
19
28
|
readonly child: QuickJsChildProcess;
|
|
29
|
+
/** Stops the worker and releases everything it holds. Called exactly once per execution, however it ends. */
|
|
20
30
|
terminate(): void;
|
|
21
31
|
}
|
|
22
32
|
|
|
23
33
|
export interface QuickJsProtocolAdapterOptions {
|
|
24
34
|
readonly id: string;
|
|
25
35
|
readonly version: string;
|
|
36
|
+
/**
|
|
37
|
+
* The isolation your `launch` provides. Defaults to `test`, which `createCodeMode` accepts only with
|
|
38
|
+
* `allowTestAdapter: true`. Declare `production` only when the launched worker meets the guarantees you document.
|
|
39
|
+
*/
|
|
40
|
+
readonly qualification?: SandboxQualification;
|
|
26
41
|
readonly isAvailable?: () => boolean | Promise<boolean>;
|
|
42
|
+
/** Starts one worker running the QuickJS worker protocol for this request. */
|
|
27
43
|
readonly launch: (request: SandboxExecutionRequest) => QuickJsWorkerProcess;
|
|
28
44
|
}
|
|
29
45
|
|
|
30
46
|
interface ToolMessage { readonly v: 1; readonly type: 'tool'; readonly requestId: number; readonly toolId: string; readonly input: unknown }
|
|
31
|
-
interface ResultMessage { readonly v: 1; readonly type: 'result'; readonly status: 'succeeded' | 'failed'; readonly output?: unknown }
|
|
47
|
+
interface ResultMessage { readonly v: 1; readonly type: 'result'; readonly status: 'succeeded' | 'failed'; readonly output?: unknown; readonly reason?: unknown; readonly programError?: unknown }
|
|
32
48
|
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
49
|
+
/** Envelope, tool id and escaped program-error text around the largest payload a line may carry. */
|
|
50
|
+
const PROTOCOL_OVERHEAD = 64 * 1_024;
|
|
51
|
+
const MAX_STDERR_BYTES = 64 * 1_024;
|
|
52
|
+
const MIB = 1_024 * 1_024;
|
|
53
|
+
const reasons: ReadonlySet<string> = new Set<SandboxFailureReason>(['program_error', 'cpu_limit', 'memory_limit', 'invalid_output',
|
|
54
|
+
'unsupported_program', 'sandbox_error']);
|
|
55
|
+
const failed = (reason: SandboxFailureReason): SandboxExecutionResult => Object.freeze({ status: 'failed', reason });
|
|
36
56
|
|
|
37
57
|
function record(value: unknown): value is Record<string, unknown> {
|
|
38
58
|
return value !== null && typeof value === 'object' && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
|
|
@@ -53,10 +73,19 @@ function parseMessage(line: string): ToolMessage | ResultMessage | undefined {
|
|
|
53
73
|
return undefined;
|
|
54
74
|
}
|
|
55
75
|
|
|
56
|
-
function
|
|
76
|
+
function resultOf(message: ResultMessage): SandboxExecutionResult {
|
|
77
|
+
if (message.status === 'succeeded') return Object.hasOwn(message, 'output') ? Object.freeze({ status: 'succeeded', output: message.output }) : failed('sandbox_error');
|
|
78
|
+
if (typeof message.reason !== 'string' || !reasons.has(message.reason)) return failed('sandbox_error');
|
|
79
|
+
const programError = message.reason === 'program_error' && record(message.programError)
|
|
80
|
+
&& typeof message.programError['name'] === 'string' && typeof message.programError['message'] === 'string'
|
|
81
|
+
? Object.freeze({ name: message.programError['name'], message: message.programError['message'] }) : undefined;
|
|
82
|
+
return Object.freeze({ status: 'failed', reason: message.reason as SandboxFailureReason, ...(programError ? { programError } : {}) });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function write(child: QuickJsChildProcess, value: unknown, maxBytes: number): boolean {
|
|
57
86
|
try {
|
|
58
87
|
const line = `${JSON.stringify(value)}\n`;
|
|
59
|
-
if (Buffer.byteLength(line) >
|
|
88
|
+
if (Buffer.byteLength(line) > maxBytes || child.stdin.destroyed) return false;
|
|
60
89
|
child.stdin.write(line);
|
|
61
90
|
return true;
|
|
62
91
|
} catch { return false; }
|
|
@@ -76,6 +105,7 @@ function terminate(child: QuickJsChildProcess): void {
|
|
|
76
105
|
const taskkill = join(systemRoot, 'System32', 'taskkill.exe');
|
|
77
106
|
if (existsSync(taskkill)) {
|
|
78
107
|
const killer = spawn(taskkill, ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true });
|
|
108
|
+
killer.on('error', () => undefined);
|
|
79
109
|
killer.unref();
|
|
80
110
|
}
|
|
81
111
|
}
|
|
@@ -83,91 +113,105 @@ function terminate(child: QuickJsChildProcess): void {
|
|
|
83
113
|
|
|
84
114
|
function run(request: SandboxExecutionRequest, launch: QuickJsProtocolAdapterOptions['launch']): Promise<SandboxExecutionResult> {
|
|
85
115
|
if (request.manifest.language !== 'javascript' || request.manifest.approvedImports.length !== 0) {
|
|
86
|
-
return Promise.resolve(
|
|
116
|
+
return Promise.resolve(failed('unsupported_program'));
|
|
87
117
|
}
|
|
118
|
+
const limits = request.manifest.limits;
|
|
119
|
+
const maxInboundLine = Math.max(limits.maxOutputBytes, limits.maxToolInputBytes) + PROTOCOL_OVERHEAD;
|
|
120
|
+
const maxOutboundLine = 4 * (limits.maxInputBytes + limits.maxOutputBytes + 2 * MIB) + PROTOCOL_OVERHEAD;
|
|
88
121
|
return new Promise(resolve => {
|
|
89
122
|
let settled = false;
|
|
90
123
|
let stdout = '';
|
|
124
|
+
let stdoutBytes = 0;
|
|
91
125
|
let stderrBytes = 0;
|
|
92
126
|
const requestIds = new Set<number>();
|
|
93
|
-
|
|
94
|
-
const child = worker.child;
|
|
127
|
+
let worker: QuickJsWorkerProcess;
|
|
95
128
|
const finish = (result: SandboxExecutionResult): void => {
|
|
96
129
|
if (settled) return;
|
|
97
130
|
settled = true;
|
|
98
131
|
request.signal.removeEventListener('abort', abort);
|
|
99
|
-
worker
|
|
100
|
-
resolve(
|
|
132
|
+
try { worker?.terminate(); } catch { /* Termination is best effort; the worker also stops at its own deadline. */ }
|
|
133
|
+
resolve(result);
|
|
101
134
|
};
|
|
102
|
-
const abort = (): void => finish(
|
|
135
|
+
const abort = (): void => finish(failed('sandbox_error'));
|
|
103
136
|
request.signal.addEventListener('abort', abort, { once: true });
|
|
104
137
|
if (request.signal.aborted) { abort(); return; }
|
|
105
|
-
|
|
106
|
-
child
|
|
138
|
+
try { worker = launch(request); } catch { finish(failed('sandbox_error')); return; }
|
|
139
|
+
const child = worker.child;
|
|
140
|
+
child.once('error', () => finish(failed('sandbox_error')));
|
|
141
|
+
child.once('exit', () => finish(failed('sandbox_error')));
|
|
107
142
|
child.stderr.on('data', (chunk: Uint8Array) => {
|
|
108
143
|
stderrBytes += chunk.byteLength;
|
|
109
|
-
if (stderrBytes >
|
|
144
|
+
if (stderrBytes > MAX_STDERR_BYTES) finish(failed('sandbox_error'));
|
|
110
145
|
});
|
|
111
146
|
child.stdout.setEncoding('utf8');
|
|
112
147
|
child.stdout.on('data', (chunk: string) => {
|
|
113
148
|
if (settled) return;
|
|
114
149
|
stdout += chunk;
|
|
115
|
-
|
|
150
|
+
stdoutBytes += Buffer.byteLength(chunk);
|
|
116
151
|
for (;;) {
|
|
117
152
|
const newline = stdout.indexOf('\n');
|
|
118
153
|
if (newline < 0) break;
|
|
119
154
|
const line = stdout.slice(0, newline); stdout = stdout.slice(newline + 1);
|
|
155
|
+
stdoutBytes = Buffer.byteLength(stdout);
|
|
156
|
+
if (Buffer.byteLength(line) > maxInboundLine) { finish(failed('sandbox_error')); return; }
|
|
120
157
|
const message = parseMessage(line);
|
|
121
|
-
if (!message) { finish(
|
|
122
|
-
if (message.type === 'result') {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (requestIds.has(message.requestId)) { finish({ status: 'failed' }); return; }
|
|
158
|
+
if (!message) { finish(failed('sandbox_error')); return; }
|
|
159
|
+
if (message.type === 'result') { finish(resultOf(message)); return; }
|
|
160
|
+
// The worker answers calls beyond maxToolCalls itself, so more requests than that mean a broken worker.
|
|
161
|
+
if (requestIds.has(message.requestId) || requestIds.size >= limits.maxToolCalls) { finish(failed('sandbox_error')); return; }
|
|
128
162
|
requestIds.add(message.requestId);
|
|
129
163
|
void request.tools.call(message.toolId, message.input).then(
|
|
130
|
-
outcome => { if (!settled && !write(child, { v: 1, type: 'toolResult', requestId: message.requestId, outcome })) finish(
|
|
164
|
+
outcome => { if (!settled && !write(child, { v: 1, type: 'toolResult', requestId: message.requestId, outcome }, maxOutboundLine)) finish(failed('sandbox_error')); },
|
|
131
165
|
() => { if (!settled && !write(child, { v: 1, type: 'toolResult', requestId: message.requestId,
|
|
132
|
-
outcome: { status: 'failed', error: { code: 'TOOL_FAILED', message: '
|
|
166
|
+
outcome: { status: 'failed', error: { code: 'TOOL_FAILED', message: 'The tool call failed.' } } satisfies CodeToolOutcome }, maxOutboundLine)) finish(failed('sandbox_error')); },
|
|
133
167
|
);
|
|
134
168
|
}
|
|
169
|
+
if (stdoutBytes > maxInboundLine) finish(failed('sandbox_error'));
|
|
135
170
|
});
|
|
136
171
|
if (!write(child, { v: 1, type: 'start', executionId: request.executionId, manifest: request.manifest,
|
|
137
|
-
source: request.source, input: request.input })) finish(
|
|
172
|
+
source: request.source, input: request.input }, 64 * MIB)) finish(failed('sandbox_error'));
|
|
138
173
|
});
|
|
139
174
|
}
|
|
140
175
|
|
|
141
|
-
|
|
176
|
+
function configError(message: string): MayuraError { return new MayuraError('INVALID_CONFIG', message); }
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Builds an adapter that speaks the QuickJS worker protocol to a worker your `launch` starts, for example inside an
|
|
180
|
+
* outer sandbox. `mayura/adapter-code-docker` is built on it.
|
|
181
|
+
*/
|
|
142
182
|
export function createQuickJsProtocolAdapter(options: QuickJsProtocolAdapterOptions): SandboxAdapter {
|
|
143
|
-
if (!options || typeof options
|
|
144
|
-
|
|
145
|
-
|
|
183
|
+
if (!options || typeof options !== 'object') throw configError('QuickJS protocol adapter options must be an object.');
|
|
184
|
+
if (typeof options.launch !== 'function') throw configError('QuickJS protocol adapter launch must be a function.');
|
|
185
|
+
if (options.isAvailable !== undefined && typeof options.isAvailable !== 'function') throw configError('QuickJS protocol adapter isAvailable must be a function.');
|
|
186
|
+
if (options.qualification !== undefined && options.qualification !== 'test' && options.qualification !== 'production') {
|
|
187
|
+
throw configError('QuickJS protocol adapter qualification must be "production" or "test".');
|
|
146
188
|
}
|
|
189
|
+
const launch = options.launch;
|
|
147
190
|
return defineSandboxAdapter({
|
|
148
191
|
id: options.id,
|
|
149
192
|
version: options.version,
|
|
150
|
-
qualification: 'test',
|
|
193
|
+
qualification: options.qualification ?? 'test',
|
|
151
194
|
isAvailable: options.isAvailable ?? (() => true),
|
|
152
|
-
execute: request => run(request,
|
|
195
|
+
execute: request => run(request, launch),
|
|
153
196
|
});
|
|
154
197
|
}
|
|
155
198
|
|
|
156
199
|
/**
|
|
157
|
-
* Creates the
|
|
158
|
-
*
|
|
200
|
+
* Creates the QuickJS sandbox adapter. Each execution runs in a new Node.js child process with an empty environment,
|
|
201
|
+
* the Node.js permission model (read-only access to the QuickJS packages; no file writes, child processes, worker
|
|
202
|
+
* threads, native addons or WASI) and a fresh QuickJS WebAssembly interpreter with the program's CPU, memory and
|
|
203
|
+
* stack limits. The program itself sees only standard JavaScript and `tools.call`.
|
|
159
204
|
*/
|
|
160
205
|
export function createQuickJsSandboxAdapter(): SandboxAdapter {
|
|
161
206
|
return createQuickJsProtocolAdapter({
|
|
162
207
|
id: 'mayura.quickjs-child',
|
|
163
|
-
version: '
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}) as unknown as QuickJsChildProcess;
|
|
208
|
+
version: '1.0.0',
|
|
209
|
+
qualification: 'production',
|
|
210
|
+
isAvailable: () => workerReadRoots() !== null,
|
|
211
|
+
launch: request => {
|
|
212
|
+
const command = workerCommand(request.manifest.limits);
|
|
213
|
+
if (!command) throw new Error('QuickJS packages are unavailable.');
|
|
214
|
+
const child = spawn(command.command, [...command.args], { ...command.options, stdio: ['pipe', 'pipe', 'pipe'] }) as unknown as QuickJsChildProcess;
|
|
171
215
|
return Object.freeze({ child, terminate: () => terminate(child) });
|
|
172
216
|
},
|
|
173
217
|
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// How the local adapter starts its worker process. Internal: not part of the package's exports.
|
|
2
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { dirname, join, parse as parsePath, sep } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const adjacentWorker = fileURLToPath(new URL('./worker.js', import.meta.url));
|
|
7
|
+
export const workerPath = existsSync(adjacentWorker) ? adjacentWorker : fileURLToPath(new URL('../dist/worker.js', import.meta.url));
|
|
8
|
+
const MIB = 1_024 * 1_024;
|
|
9
|
+
const permissionFlag = process.allowedNodeEnvironmentFlags.has('--permission') ? '--permission' : '--experimental-permission';
|
|
10
|
+
|
|
11
|
+
/** Outermost `node_modules` directory above a path, or undefined when the path is not inside one. */
|
|
12
|
+
function outermostNodeModules(path: string): string | undefined {
|
|
13
|
+
const root = parsePath(path).root;
|
|
14
|
+
const parts = path.slice(root.length).split(sep);
|
|
15
|
+
const index = parts.indexOf('node_modules');
|
|
16
|
+
return index < 0 ? undefined : root + parts.slice(0, index + 1).join(sep);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Nearest directory at or above `start` that holds a package.json. */
|
|
20
|
+
function packageRoot(start: string): string {
|
|
21
|
+
for (let directory = start; ; directory = dirname(directory)) {
|
|
22
|
+
if (existsSync(join(directory, 'package.json')) || dirname(directory) === directory) return directory;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let readRoots: readonly string[] | null | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The only paths the worker process may read: the worker's own package and the installed packages it imports.
|
|
29
|
+
* `null` means the QuickJS packages cannot be resolved, so the adapter reports itself unavailable.
|
|
30
|
+
*/
|
|
31
|
+
export function workerReadRoots(): readonly string[] | null {
|
|
32
|
+
if (readRoots !== undefined) return readRoots;
|
|
33
|
+
try {
|
|
34
|
+
const worker = realpathSync(workerPath);
|
|
35
|
+
const roots = new Set<string>([packageRoot(dirname(worker))]);
|
|
36
|
+
const outer = outermostNodeModules(worker); if (outer) roots.add(outer);
|
|
37
|
+
for (const specifier of ['quickjs-emscripten-core', '@jitl/quickjs-wasmfile-release-sync']) {
|
|
38
|
+
const resolved = realpathSync(fileURLToPath(import.meta.resolve(specifier)));
|
|
39
|
+
roots.add(outermostNodeModules(resolved) ?? packageRoot(dirname(resolved)));
|
|
40
|
+
}
|
|
41
|
+
readRoots = Object.freeze([...roots]);
|
|
42
|
+
} catch { readRoots = null; }
|
|
43
|
+
return readRoots;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface WorkerCommand {
|
|
47
|
+
readonly command: string;
|
|
48
|
+
readonly args: readonly string[];
|
|
49
|
+
readonly options: { readonly cwd: string; readonly env: Readonly<Record<string, string>>; readonly windowsHide: true };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The worker runs under the Node.js permission model: it may read only the roots above, and cannot write files,
|
|
54
|
+
* start processes or worker threads, load native addons, use WASI or open the inspector. Code generation from
|
|
55
|
+
* strings is disabled, the environment is empty, and its own JavaScript heap is bounded by the data it must hold.
|
|
56
|
+
* The QuickJS heap is WebAssembly memory, bounded separately inside the worker.
|
|
57
|
+
*/
|
|
58
|
+
export function workerCommand(limits: { readonly maxInputBytes: number; readonly maxOutputBytes: number; readonly maxToolInputBytes: number }): WorkerCommand | undefined {
|
|
59
|
+
const roots = workerReadRoots();
|
|
60
|
+
if (!roots) return undefined;
|
|
61
|
+
const heapMiB = 128 + Math.ceil((8 * (limits.maxInputBytes + limits.maxOutputBytes + limits.maxToolInputBytes)) / MIB);
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
command: process.execPath,
|
|
64
|
+
args: Object.freeze([permissionFlag, ...roots.map(root => `--allow-fs-read=${root}`), '--disallow-code-generation-from-strings',
|
|
65
|
+
`--max-old-space-size=${heapMiB}`, workerPath]),
|
|
66
|
+
options: Object.freeze({ cwd: dirname(workerPath), env: Object.freeze({}), windowsHide: true as const }),
|
|
67
|
+
});
|
|
68
|
+
}
|