operon-agents-core 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +55 -0
- package/README.md +7 -0
- package/dist/agent/agent.d.ts +65 -0
- package/dist/agent/agent.js +39 -0
- package/dist/agent/compose-hooks.d.ts +13 -0
- package/dist/agent/compose-hooks.js +197 -0
- package/dist/agent/concurrency.d.ts +22 -0
- package/dist/agent/concurrency.js +70 -0
- package/dist/agent/context-report.d.ts +60 -0
- package/dist/agent/context-report.js +45 -0
- package/dist/agent/graph.d.ts +48 -0
- package/dist/agent/graph.js +129 -0
- package/dist/agent/guardrail.d.ts +105 -0
- package/dist/agent/guardrail.js +314 -0
- package/dist/agent/handoff-executor.d.ts +21 -0
- package/dist/agent/handoff-executor.js +95 -0
- package/dist/agent/handoff.d.ts +42 -0
- package/dist/agent/handoff.js +70 -0
- package/dist/agent/index.d.ts +25 -0
- package/dist/agent/index.js +19 -0
- package/dist/agent/instruction-context.d.ts +33 -0
- package/dist/agent/instruction-context.js +100 -0
- package/dist/agent/pause.d.ts +12 -0
- package/dist/agent/pause.js +82 -0
- package/dist/agent/profile-builtins.d.ts +8 -0
- package/dist/agent/profile-builtins.js +130 -0
- package/dist/agent/profiles.d.ts +94 -0
- package/dist/agent/profiles.js +264 -0
- package/dist/agent/recovery.d.ts +10 -0
- package/dist/agent/recovery.js +151 -0
- package/dist/agent/run-support.d.ts +79 -0
- package/dist/agent/run-support.js +211 -0
- package/dist/agent/runner.d.ts +246 -0
- package/dist/agent/runner.js +786 -0
- package/dist/agent/session.d.ts +363 -0
- package/dist/agent/session.js +937 -0
- package/dist/agent/subagent-tools.d.ts +18 -0
- package/dist/agent/subagent-tools.js +650 -0
- package/dist/agent/subagent.d.ts +56 -0
- package/dist/agent/subagent.js +50 -0
- package/dist/agent/toolset.d.ts +24 -0
- package/dist/agent/toolset.js +112 -0
- package/dist/agent/workflow/index.d.ts +9 -0
- package/dist/agent/workflow/index.js +8 -0
- package/dist/agent/workflow/journal.d.ts +180 -0
- package/dist/agent/workflow/journal.js +224 -0
- package/dist/agent/workflow/manager.d.ts +27 -0
- package/dist/agent/workflow/manager.js +33 -0
- package/dist/agent/workflow/parse.d.ts +17 -0
- package/dist/agent/workflow/parse.js +174 -0
- package/dist/agent/workflow/prompt.d.ts +8 -0
- package/dist/agent/workflow/prompt.js +84 -0
- package/dist/agent/workflow/runtime.d.ts +64 -0
- package/dist/agent/workflow/runtime.js +380 -0
- package/dist/agent/workflow/sandbox.d.ts +35 -0
- package/dist/agent/workflow/sandbox.js +90 -0
- package/dist/agent/workflow/snapshot.d.ts +36 -0
- package/dist/agent/workflow/snapshot.js +9 -0
- package/dist/agent/workflow/structured.d.ts +35 -0
- package/dist/agent/workflow/structured.js +79 -0
- package/dist/agent/workflow/tool.d.ts +14 -0
- package/dist/agent/workflow/tool.js +569 -0
- package/dist/agent/workflow/types.d.ts +100 -0
- package/dist/agent/workflow/types.js +17 -0
- package/dist/agent/worktree.d.ts +38 -0
- package/dist/agent/worktree.js +56 -0
- package/dist/capabilities/assembler.d.ts +39 -0
- package/dist/capabilities/assembler.js +171 -0
- package/dist/capabilities/background/agent-task.d.ts +51 -0
- package/dist/capabilities/background/agent-task.js +101 -0
- package/dist/capabilities/background/command-task.d.ts +43 -0
- package/dist/capabilities/background/command-task.js +86 -0
- package/dist/capabilities/background/index.d.ts +20 -0
- package/dist/capabilities/background/index.js +57 -0
- package/dist/capabilities/background/manager.d.ts +194 -0
- package/dist/capabilities/background/manager.js +1131 -0
- package/dist/capabilities/background/persist-disk.d.ts +17 -0
- package/dist/capabilities/background/persist-disk.js +66 -0
- package/dist/capabilities/background/persist.d.ts +61 -0
- package/dist/capabilities/background/persist.js +117 -0
- package/dist/capabilities/background/question-task.d.ts +32 -0
- package/dist/capabilities/background/question-task.js +56 -0
- package/dist/capabilities/background/registrar.d.ts +12 -0
- package/dist/capabilities/background/registrar.js +9 -0
- package/dist/capabilities/background/swappable-sink.d.ts +20 -0
- package/dist/capabilities/background/swappable-sink.js +20 -0
- package/dist/capabilities/background/task.d.ts +114 -0
- package/dist/capabilities/background/task.js +20 -0
- package/dist/capabilities/background/tools.d.ts +6 -0
- package/dist/capabilities/background/tools.js +190 -0
- package/dist/capabilities/background/workflow-task.d.ts +48 -0
- package/dist/capabilities/background/workflow-task.js +91 -0
- package/dist/capabilities/capability-state.d.ts +13 -0
- package/dist/capabilities/capability-state.js +37 -0
- package/dist/capabilities/capability.d.ts +145 -0
- package/dist/capabilities/capability.js +2 -0
- package/dist/capabilities/compaction/full.d.ts +27 -0
- package/dist/capabilities/compaction/full.js +155 -0
- package/dist/capabilities/compaction/index.d.ts +17 -0
- package/dist/capabilities/compaction/index.js +145 -0
- package/dist/capabilities/compaction/instruction.d.ts +2 -0
- package/dist/capabilities/compaction/instruction.js +31 -0
- package/dist/capabilities/compaction/micro.d.ts +17 -0
- package/dist/capabilities/compaction/micro.js +57 -0
- package/dist/capabilities/compaction/service.d.ts +28 -0
- package/dist/capabilities/compaction/service.js +52 -0
- package/dist/capabilities/compaction/strategy.d.ts +43 -0
- package/dist/capabilities/compaction/strategy.js +116 -0
- package/dist/capabilities/compaction/tokens.d.ts +5 -0
- package/dist/capabilities/compaction/tokens.js +28 -0
- package/dist/capabilities/goal/goal-store.d.ts +69 -0
- package/dist/capabilities/goal/goal-store.js +172 -0
- package/dist/capabilities/goal/index.d.ts +6 -0
- package/dist/capabilities/goal/index.js +31 -0
- package/dist/capabilities/goal/injector.d.ts +9 -0
- package/dist/capabilities/goal/injector.js +79 -0
- package/dist/capabilities/goal/tools.d.ts +6 -0
- package/dist/capabilities/goal/tools.js +86 -0
- package/dist/capabilities/index.d.ts +25 -0
- package/dist/capabilities/index.js +22 -0
- package/dist/capabilities/injection.d.ts +44 -0
- package/dist/capabilities/injection.js +112 -0
- package/dist/capabilities/plan/index.d.ts +6 -0
- package/dist/capabilities/plan/index.js +25 -0
- package/dist/capabilities/plan/injector.d.ts +13 -0
- package/dist/capabilities/plan/injector.js +83 -0
- package/dist/capabilities/plan/plan-mode.d.ts +38 -0
- package/dist/capabilities/plan/plan-mode.js +113 -0
- package/dist/capabilities/plan/policies.d.ts +6 -0
- package/dist/capabilities/plan/policies.js +89 -0
- package/dist/capabilities/plan/tools.d.ts +15 -0
- package/dist/capabilities/plan/tools.js +87 -0
- package/dist/capabilities/skills/flow-provider.d.ts +6 -0
- package/dist/capabilities/skills/flow-provider.js +50 -0
- package/dist/capabilities/skills/index.d.ts +43 -0
- package/dist/capabilities/skills/index.js +48 -0
- package/dist/capabilities/skills/injector.d.ts +11 -0
- package/dist/capabilities/skills/injector.js +27 -0
- package/dist/capabilities/skills/parser.d.ts +36 -0
- package/dist/capabilities/skills/parser.js +334 -0
- package/dist/capabilities/skills/registry.d.ts +33 -0
- package/dist/capabilities/skills/registry.js +118 -0
- package/dist/capabilities/skills/scanner.d.ts +26 -0
- package/dist/capabilities/skills/scanner.js +154 -0
- package/dist/capabilities/skills/service.d.ts +39 -0
- package/dist/capabilities/skills/service.js +68 -0
- package/dist/capabilities/skills/skill-tool.d.ts +13 -0
- package/dist/capabilities/skills/skill-tool.js +72 -0
- package/dist/capabilities/skills/types.d.ts +62 -0
- package/dist/capabilities/skills/types.js +26 -0
- package/dist/capabilities/task/index.d.ts +23 -0
- package/dist/capabilities/task/index.js +38 -0
- package/dist/capabilities/task/injector.d.ts +9 -0
- package/dist/capabilities/task/injector.js +62 -0
- package/dist/capabilities/task/persist-disk.d.ts +19 -0
- package/dist/capabilities/task/persist-disk.js +84 -0
- package/dist/capabilities/task/persist.d.ts +52 -0
- package/dist/capabilities/task/persist.js +84 -0
- package/dist/capabilities/task/task-store.d.ts +44 -0
- package/dist/capabilities/task/task-store.js +106 -0
- package/dist/capabilities/task/tools.d.ts +12 -0
- package/dist/capabilities/task/tools.js +164 -0
- package/dist/capabilities/todo/index.d.ts +8 -0
- package/dist/capabilities/todo/index.js +21 -0
- package/dist/capabilities/todo/injector.d.ts +9 -0
- package/dist/capabilities/todo/injector.js +68 -0
- package/dist/capabilities/todo/todo-store.d.ts +19 -0
- package/dist/capabilities/todo/todo-store.js +36 -0
- package/dist/capabilities/todo/tools.d.ts +6 -0
- package/dist/capabilities/todo/tools.js +70 -0
- package/dist/capabilities/tool-provider.d.ts +12 -0
- package/dist/capabilities/tool-provider.js +20 -0
- package/dist/capabilities/user-hooks/engine.d.ts +22 -0
- package/dist/capabilities/user-hooks/engine.js +105 -0
- package/dist/capabilities/user-hooks/index.d.ts +13 -0
- package/dist/capabilities/user-hooks/index.js +126 -0
- package/dist/capabilities/user-hooks/types.d.ts +26 -0
- package/dist/capabilities/user-hooks/types.js +19 -0
- package/dist/capabilities/workflow/index.d.ts +16 -0
- package/dist/capabilities/workflow/index.js +15 -0
- package/dist/commands/index.d.ts +31 -0
- package/dist/commands/index.js +318 -0
- package/dist/config/config-store.d.ts +29 -0
- package/dist/config/config-store.js +103 -0
- package/dist/config/file-source.d.ts +40 -0
- package/dist/config/file-source.js +78 -0
- package/dist/config/flags.d.ts +13 -0
- package/dist/config/flags.js +25 -0
- package/dist/config/index.d.ts +17 -0
- package/dist/config/index.js +10 -0
- package/dist/config/merge.d.ts +5 -0
- package/dist/config/merge.js +57 -0
- package/dist/config/model-catalog.d.ts +17 -0
- package/dist/config/model-catalog.js +37 -0
- package/dist/config/paths.d.ts +31 -0
- package/dist/config/paths.js +28 -0
- package/dist/config/provider-manager.d.ts +57 -0
- package/dist/config/provider-manager.js +344 -0
- package/dist/config/resolver.d.ts +40 -0
- package/dist/config/resolver.js +52 -0
- package/dist/config/schema.d.ts +389 -0
- package/dist/config/schema.js +132 -0
- package/dist/events/events.d.ts +348 -0
- package/dist/events/events.js +146 -0
- package/dist/events/index.d.ts +10 -0
- package/dist/events/index.js +5 -0
- package/dist/events/projection.d.ts +207 -0
- package/dist/events/projection.js +438 -0
- package/dist/events/publisher.d.ts +48 -0
- package/dist/events/publisher.js +404 -0
- package/dist/events/redacting-sink.d.ts +11 -0
- package/dist/events/redacting-sink.js +31 -0
- package/dist/home.d.ts +16 -0
- package/dist/home.js +20 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +22 -0
- package/dist/internal.d.ts +33 -0
- package/dist/internal.js +29 -0
- package/dist/llm/auth.d.ts +22 -0
- package/dist/llm/auth.js +112 -0
- package/dist/llm/capability.d.ts +13 -0
- package/dist/llm/capability.js +45 -0
- package/dist/llm/define-model.d.ts +110 -0
- package/dist/llm/define-model.js +252 -0
- package/dist/llm/errors.d.ts +33 -0
- package/dist/llm/errors.js +154 -0
- package/dist/llm/index.d.ts +14 -0
- package/dist/llm/index.js +8 -0
- package/dist/llm/model.d.ts +43 -0
- package/dist/llm/model.js +9 -0
- package/dist/llm/retry.d.ts +35 -0
- package/dist/llm/retry.js +160 -0
- package/dist/llm/runtime.d.ts +25 -0
- package/dist/llm/runtime.js +38 -0
- package/dist/logging/diagnostics.d.ts +21 -0
- package/dist/logging/diagnostics.js +46 -0
- package/dist/logging/index.d.ts +11 -0
- package/dist/logging/index.js +7 -0
- package/dist/logging/logger.d.ts +25 -0
- package/dist/logging/logger.js +54 -0
- package/dist/logging/paths.d.ts +11 -0
- package/dist/logging/paths.js +20 -0
- package/dist/logging/policy.d.ts +5 -0
- package/dist/logging/policy.js +19 -0
- package/dist/logging/redact.d.ts +10 -0
- package/dist/logging/redact.js +95 -0
- package/dist/logging/sinks.d.ts +67 -0
- package/dist/logging/sinks.js +117 -0
- package/dist/loop/context.d.ts +69 -0
- package/dist/loop/context.js +129 -0
- package/dist/loop/errors.d.ts +8 -0
- package/dist/loop/errors.js +27 -0
- package/dist/loop/events.d.ts +14 -0
- package/dist/loop/events.js +2 -0
- package/dist/loop/index.d.ts +10 -0
- package/dist/loop/index.js +7 -0
- package/dist/loop/interruption.d.ts +118 -0
- package/dist/loop/interruption.js +191 -0
- package/dist/loop/run-turn.d.ts +54 -0
- package/dist/loop/run-turn.js +203 -0
- package/dist/loop/scheduler.d.ts +18 -0
- package/dist/loop/scheduler.js +71 -0
- package/dist/loop/steer.d.ts +122 -0
- package/dist/loop/steer.js +209 -0
- package/dist/loop/tool-call.d.ts +41 -0
- package/dist/loop/tool-call.js +326 -0
- package/dist/loop/turn-step.d.ts +57 -0
- package/dist/loop/turn-step.js +298 -0
- package/dist/loop/types.d.ts +217 -0
- package/dist/loop/types.js +2 -0
- package/dist/loop/usage.d.ts +6 -0
- package/dist/loop/usage.js +44 -0
- package/dist/mcp/auth-tool.d.ts +12 -0
- package/dist/mcp/auth-tool.js +92 -0
- package/dist/mcp/capability.d.ts +7 -0
- package/dist/mcp/capability.js +91 -0
- package/dist/mcp/config-loader.d.ts +28 -0
- package/dist/mcp/config-loader.js +115 -0
- package/dist/mcp/filter.d.ts +18 -0
- package/dist/mcp/filter.js +35 -0
- package/dist/mcp/index.d.ts +23 -0
- package/dist/mcp/index.js +13 -0
- package/dist/mcp/manager.d.ts +51 -0
- package/dist/mcp/manager.js +412 -0
- package/dist/mcp/mock-transport.d.ts +40 -0
- package/dist/mcp/mock-transport.js +68 -0
- package/dist/mcp/oauth/callback-server.d.ts +14 -0
- package/dist/mcp/oauth/callback-server.js +122 -0
- package/dist/mcp/oauth/index.d.ts +9 -0
- package/dist/mcp/oauth/index.js +5 -0
- package/dist/mcp/oauth/provider.d.ts +39 -0
- package/dist/mcp/oauth/provider.js +121 -0
- package/dist/mcp/oauth/service.d.ts +34 -0
- package/dist/mcp/oauth/service.js +130 -0
- package/dist/mcp/oauth/store.d.ts +40 -0
- package/dist/mcp/oauth/store.js +134 -0
- package/dist/mcp/output.d.ts +12 -0
- package/dist/mcp/output.js +110 -0
- package/dist/mcp/provider.d.ts +7 -0
- package/dist/mcp/provider.js +56 -0
- package/dist/mcp/sdk-transport.d.ts +25 -0
- package/dist/mcp/sdk-transport.js +228 -0
- package/dist/mcp/server.d.ts +77 -0
- package/dist/mcp/server.js +120 -0
- package/dist/mcp/tool-naming.d.ts +4 -0
- package/dist/mcp/tool-naming.js +26 -0
- package/dist/mcp/types.d.ts +75 -0
- package/dist/mcp/types.js +2 -0
- package/dist/net/index.d.ts +3 -0
- package/dist/net/index.js +2 -0
- package/dist/net/proxy.d.ts +16 -0
- package/dist/net/proxy.js +42 -0
- package/dist/permission/auto-approver/index.d.ts +3 -0
- package/dist/permission/auto-approver/index.js +3 -0
- package/dist/permission/auto-approver/llm-auto-approver.d.ts +53 -0
- package/dist/permission/auto-approver/llm-auto-approver.js +122 -0
- package/dist/permission/auto-approver/prompts-data.d.ts +9 -0
- package/dist/permission/auto-approver/prompts-data.js +155 -0
- package/dist/permission/auto-approver/prompts.d.ts +18 -0
- package/dist/permission/auto-approver/prompts.js +40 -0
- package/dist/permission/auto-approver/transcript.d.ts +22 -0
- package/dist/permission/auto-approver/transcript.js +75 -0
- package/dist/permission/auto-approver/xml.d.ts +5 -0
- package/dist/permission/auto-approver/xml.js +25 -0
- package/dist/permission/index.d.ts +7 -0
- package/dist/permission/index.js +5 -0
- package/dist/permission/manager.d.ts +74 -0
- package/dist/permission/manager.js +162 -0
- package/dist/permission/matches-rule.d.ts +27 -0
- package/dist/permission/matches-rule.js +54 -0
- package/dist/permission/policies.d.ts +14 -0
- package/dist/permission/policies.js +233 -0
- package/dist/permission/types.d.ts +84 -0
- package/dist/permission/types.js +2 -0
- package/dist/plugins/archive.d.ts +3 -0
- package/dist/plugins/archive.js +58 -0
- package/dist/plugins/github-resolver.d.ts +13 -0
- package/dist/plugins/github-resolver.js +20 -0
- package/dist/plugins/hooks.d.ts +28 -0
- package/dist/plugins/hooks.js +296 -0
- package/dist/plugins/index.d.ts +23 -0
- package/dist/plugins/index.js +23 -0
- package/dist/plugins/injector.d.ts +11 -0
- package/dist/plugins/injector.js +32 -0
- package/dist/plugins/manager.d.ts +37 -0
- package/dist/plugins/manager.js +330 -0
- package/dist/plugins/manifest.d.ts +9 -0
- package/dist/plugins/manifest.js +234 -0
- package/dist/plugins/marketplace.d.ts +85 -0
- package/dist/plugins/marketplace.js +205 -0
- package/dist/plugins/source.d.ts +23 -0
- package/dist/plugins/source.js +87 -0
- package/dist/plugins/store.d.ts +20 -0
- package/dist/plugins/store.js +30 -0
- package/dist/plugins/types.d.ts +118 -0
- package/dist/plugins/types.js +5 -0
- package/dist/protocol/index.d.ts +4 -0
- package/dist/protocol/index.js +2 -0
- package/dist/protocol/tool-schema.d.ts +6 -0
- package/dist/protocol/tool-schema.js +2 -0
- package/dist/store/blob-store.d.ts +45 -0
- package/dist/store/blob-store.js +120 -0
- package/dist/store/catalog-store.d.ts +32 -0
- package/dist/store/catalog-store.js +73 -0
- package/dist/store/disk-store.d.ts +58 -0
- package/dist/store/disk-store.js +369 -0
- package/dist/store/index.d.ts +23 -0
- package/dist/store/index.js +15 -0
- package/dist/store/lock.d.ts +86 -0
- package/dist/store/lock.js +104 -0
- package/dist/store/log-store.d.ts +89 -0
- package/dist/store/log-store.js +296 -0
- package/dist/store/migrate.d.ts +26 -0
- package/dist/store/migrate.js +32 -0
- package/dist/store/origin.d.ts +89 -0
- package/dist/store/origin.js +16 -0
- package/dist/store/pg.d.ts +90 -0
- package/dist/store/pg.js +344 -0
- package/dist/store/redacting-store.d.ts +21 -0
- package/dist/store/redacting-store.js +46 -0
- package/dist/store/redis.d.ts +89 -0
- package/dist/store/redis.js +370 -0
- package/dist/store/repository.d.ts +150 -0
- package/dist/store/repository.js +508 -0
- package/dist/store/session-catalog.d.ts +32 -0
- package/dist/store/session-catalog.js +117 -0
- package/dist/store/storage.d.ts +27 -0
- package/dist/store/storage.js +54 -0
- package/dist/store/store.d.ts +262 -0
- package/dist/store/store.js +129 -0
- package/dist/store/workdir-key.d.ts +4 -0
- package/dist/store/workdir-key.js +28 -0
- package/dist/tool/access.d.ts +37 -0
- package/dist/tool/access.js +79 -0
- package/dist/tool/ask.d.ts +22 -0
- package/dist/tool/ask.js +38 -0
- package/dist/tool/background.d.ts +109 -0
- package/dist/tool/background.js +39 -0
- package/dist/tool/builtin/ask-user-question.d.ts +2 -0
- package/dist/tool/builtin/ask-user-question.js +175 -0
- package/dist/tool/builtin/bash.d.ts +2 -0
- package/dist/tool/builtin/bash.js +407 -0
- package/dist/tool/builtin/edit.d.ts +2 -0
- package/dist/tool/builtin/edit.js +202 -0
- package/dist/tool/builtin/glob.d.ts +3 -0
- package/dist/tool/builtin/glob.js +260 -0
- package/dist/tool/builtin/grep.d.ts +2 -0
- package/dist/tool/builtin/grep.js +574 -0
- package/dist/tool/builtin/line-endings.d.ts +10 -0
- package/dist/tool/builtin/line-endings.js +41 -0
- package/dist/tool/builtin/read.d.ts +28 -0
- package/dist/tool/builtin/read.js +498 -0
- package/dist/tool/builtin/write.d.ts +2 -0
- package/dist/tool/builtin/write.js +179 -0
- package/dist/tool/define.d.ts +46 -0
- package/dist/tool/define.js +45 -0
- package/dist/tool/file-freshness.d.ts +81 -0
- package/dist/tool/file-freshness.js +67 -0
- package/dist/tool/filesystem-tools.d.ts +16 -0
- package/dist/tool/filesystem-tools.js +19 -0
- package/dist/tool/git-context.d.ts +20 -0
- package/dist/tool/git-context.js +114 -0
- package/dist/tool/index.d.ts +37 -0
- package/dist/tool/index.js +34 -0
- package/dist/tool/machine-base.d.ts +138 -0
- package/dist/tool/machine-base.js +388 -0
- package/dist/tool/machine-local.d.ts +35 -0
- package/dist/tool/machine-local.js +316 -0
- package/dist/tool/machine-null.d.ts +36 -0
- package/dist/tool/machine-null.js +64 -0
- package/dist/tool/machine-ssh.d.ts +86 -0
- package/dist/tool/machine-ssh.js +576 -0
- package/dist/tool/machine.d.ts +333 -0
- package/dist/tool/machine.js +15 -0
- package/dist/tool/policies/path-access.d.ts +48 -0
- package/dist/tool/policies/path-access.js +186 -0
- package/dist/tool/policies/sensitive.d.ts +3 -0
- package/dist/tool/policies/sensitive.js +60 -0
- package/dist/tool/questions.d.ts +40 -0
- package/dist/tool/questions.js +8 -0
- package/dist/tool/search/activation.d.ts +13 -0
- package/dist/tool/search/activation.js +31 -0
- package/dist/tool/search/deferral.d.ts +37 -0
- package/dist/tool/search/deferral.js +128 -0
- package/dist/tool/search/search-tool.d.ts +9 -0
- package/dist/tool/search/search-tool.js +62 -0
- package/dist/tool/shell-env.d.ts +9 -0
- package/dist/tool/shell-env.js +34 -0
- package/dist/tool/support/bash-destructive.d.ts +24 -0
- package/dist/tool/support/bash-destructive.js +63 -0
- package/dist/tool/support/bash-paths.d.ts +7 -0
- package/dist/tool/support/bash-paths.js +247 -0
- package/dist/tool/support/bash-read-only.d.ts +8 -0
- package/dist/tool/support/bash-read-only.js +165 -0
- package/dist/tool/support/bash-rule-match.d.ts +20 -0
- package/dist/tool/support/bash-rule-match.js +191 -0
- package/dist/tool/support/file-type.d.ts +10 -0
- package/dist/tool/support/file-type.js +149 -0
- package/dist/tool/support/git-worktree.d.ts +10 -0
- package/dist/tool/support/git-worktree.js +63 -0
- package/dist/tool/support/image-compress.d.ts +69 -0
- package/dist/tool/support/image-compress.js +265 -0
- package/dist/tool/support/machine-ops.d.ts +34 -0
- package/dist/tool/support/machine-ops.js +34 -0
- package/dist/tool/support/path-glob-match.d.ts +12 -0
- package/dist/tool/support/path-glob-match.js +84 -0
- package/dist/tool/support/result-builder.d.ts +27 -0
- package/dist/tool/support/result-builder.js +108 -0
- package/dist/tool/support/rg-locator.d.ts +20 -0
- package/dist/tool/support/rg-locator.js +42 -0
- package/dist/tool/support/ripgrep-run.d.ts +65 -0
- package/dist/tool/support/ripgrep-run.js +40 -0
- package/dist/tool/support/rule-match.d.ts +6 -0
- package/dist/tool/support/rule-match.js +23 -0
- package/dist/tool/support/tool-path.d.ts +8 -0
- package/dist/tool/support/tool-path.js +28 -0
- package/dist/tool/support/workspace.d.ts +5 -0
- package/dist/tool/support/workspace.js +2 -0
- package/dist/tool/types.d.ts +131 -0
- package/dist/tool/types.js +2 -0
- package/dist/tool/web/fetch-url.d.ts +4 -0
- package/dist/tool/web/fetch-url.js +61 -0
- package/dist/tool/web/index.d.ts +11 -0
- package/dist/tool/web/index.js +7 -0
- package/dist/tool/web/providers/direct-fetch.d.ts +33 -0
- package/dist/tool/web/providers/direct-fetch.js +198 -0
- package/dist/tool/web/providers/firecrawl.d.ts +16 -0
- package/dist/tool/web/providers/firecrawl.js +59 -0
- package/dist/tool/web/providers/tavily.d.ts +14 -0
- package/dist/tool/web/providers/tavily.js +43 -0
- package/dist/tool/web/types.d.ts +41 -0
- package/dist/tool/web/types.js +7 -0
- package/dist/tool/web/web-search.d.ts +4 -0
- package/dist/tool/web/web-search.js +68 -0
- package/dist/tool/workspace-spec.d.ts +74 -0
- package/dist/tool/workspace-spec.js +177 -0
- package/dist/tracing/bridge.d.ts +7 -0
- package/dist/tracing/bridge.js +171 -0
- package/dist/tracing/index.d.ts +9 -0
- package/dist/tracing/index.js +5 -0
- package/dist/tracing/otel.d.ts +38 -0
- package/dist/tracing/otel.js +221 -0
- package/dist/tracing/processor.d.ts +47 -0
- package/dist/tracing/processor.js +94 -0
- package/dist/tracing/spans.d.ts +94 -0
- package/dist/tracing/spans.js +56 -0
- package/dist/ui/ai-sdk.d.ts +114 -0
- package/dist/ui/ai-sdk.js +175 -0
- package/dist/ui/index.d.ts +2 -0
- package/dist/ui/index.js +2 -0
- package/package.json +101 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Tool } from "../../tool/types.ts";
|
|
2
|
+
import type { RunState, SubagentSpawner } from "../runner.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The Workflow tool — a deterministic JS orchestration runtime over subagents.
|
|
5
|
+
* The script runs in a hardened VM (see workflow/sandbox.ts); its agent() hook spawns
|
|
6
|
+
* subagents through the shared `SubagentSpawner` (the same `Engine.run` the Agent tool
|
|
7
|
+
* uses), so token usage feeds a REAL budget and structured output is enforced by
|
|
8
|
+
* injecting a StructuredOutput tool into the subagent.
|
|
9
|
+
*
|
|
10
|
+
* Independent of the Runner: it receives the engine's spawner + the parent runtime, and
|
|
11
|
+
* runs sub-agents by forking a child frame (`spawner.derive`) and calling `spawner.run`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildWorkflowTool<TContext>(spawner: SubagentSpawner<TContext>, parent: RunState<TContext>): Tool;
|
|
14
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { addUsage, emptyUsage } from "../../loop/usage.js";
|
|
4
|
+
import { errorMessage, isAbortError } from "../../loop/errors.js";
|
|
5
|
+
import { ConversationContext } from "../../loop/context.js";
|
|
6
|
+
import { historyChangeEmitter } from "../run-support.js";
|
|
7
|
+
import { SteerBus } from "../../loop/steer.js";
|
|
8
|
+
import { defineTool } from "../../tool/define.js";
|
|
9
|
+
import { ToolAccesses } from "../../tool/access.js";
|
|
10
|
+
import { resolveToolPath } from "../../tool/support/tool-path.js";
|
|
11
|
+
import { readTextFile, writeTextFile } from "../../tool/support/machine-ops.js";
|
|
12
|
+
import { joinAddress } from "../../events/index.js";
|
|
13
|
+
import { WorkflowBackgroundTask } from "../../capabilities/background/workflow-task.js";
|
|
14
|
+
import { asTaskRegistrar } from "../../capabilities/background/registrar.js";
|
|
15
|
+
import { newAgentId } from "../subagent.js";
|
|
16
|
+
import { guardSubagentInput } from "../subagent-tools.js";
|
|
17
|
+
import { createWorktree } from "../worktree.js";
|
|
18
|
+
import { runWorkflow, parseWorkflow, WorkflowJournal, journalAddress, WorkflowManager, workflowPrompt, makeStructuredOutputTool, cloneAgentWithTool, validateValue, structuredInstruction, WORKFLOW_AGENT_TIMEOUT_MS, } from "./index.js";
|
|
19
|
+
/**
|
|
20
|
+
* The Workflow tool — a deterministic JS orchestration runtime over subagents.
|
|
21
|
+
* The script runs in a hardened VM (see workflow/sandbox.ts); its agent() hook spawns
|
|
22
|
+
* subagents through the shared `SubagentSpawner` (the same `Engine.run` the Agent tool
|
|
23
|
+
* uses), so token usage feeds a REAL budget and structured output is enforced by
|
|
24
|
+
* injecting a StructuredOutput tool into the subagent.
|
|
25
|
+
*
|
|
26
|
+
* Independent of the Runner: it receives the engine's spawner + the parent runtime, and
|
|
27
|
+
* runs sub-agents by forking a child frame (`spawner.derive`) and calling `spawner.run`.
|
|
28
|
+
*/
|
|
29
|
+
export function buildWorkflowTool(spawner, parent) {
|
|
30
|
+
// Build the host hooks. `emitProgress` + `abortSignal` differ between the
|
|
31
|
+
// foreground (ctx.onUpdate) and background (task sink) paths; subagent usage
|
|
32
|
+
// accumulates into `usageBox` so the caller can fold it into parent.usage.
|
|
33
|
+
const buildHooks = (emitProgress, abortSignal, tokenBudget, usageBox, parentToolCallId) => {
|
|
34
|
+
const baseline = parent.usage.output;
|
|
35
|
+
return {
|
|
36
|
+
concurrency: Math.min(16, Math.max(2, os.cpus().length - 2)),
|
|
37
|
+
abortSignal,
|
|
38
|
+
budget: { total: tokenBudget, getTurnSpent: () => baseline },
|
|
39
|
+
resolveWorkflowScript: (name) => readNamedWorkflow(parent.machine, name),
|
|
40
|
+
emitProgress,
|
|
41
|
+
runAgent: async ({ prompt, agentType, schema, isolation, signal, onStart }) => {
|
|
42
|
+
const sub = await spawner.resolve(agentType ?? spawner.defaultType);
|
|
43
|
+
if (sub === undefined) {
|
|
44
|
+
throw new Error(`unknown agentType "${agentType ?? spawner.defaultType}". Available: ${spawner.available.map((a) => a.name).join(", ")}`);
|
|
45
|
+
}
|
|
46
|
+
const agentId = newAgentId(sub.name);
|
|
47
|
+
const address = joinAddress(parent.address, agentId);
|
|
48
|
+
// Publish + journal the stable identity before any work runs, so the orchestration
|
|
49
|
+
// event can be joined to this child's Session AgentEvents and interruption is resumable.
|
|
50
|
+
onStart({ agentId, address });
|
|
51
|
+
// Optional worktree isolation — degrade (with a log, never silently) if unsupported.
|
|
52
|
+
let runMachine = parent.machine;
|
|
53
|
+
let cleanupWorktree;
|
|
54
|
+
if (isolation === "worktree") {
|
|
55
|
+
const wt = await createWorktree(parent.machine, { label: agentId });
|
|
56
|
+
if (wt) {
|
|
57
|
+
runMachine = wt.machine;
|
|
58
|
+
cleanupWorktree = wt.cleanup;
|
|
59
|
+
emitProgress({ type: "log", message: `🌳 ${agentId}: isolated worktree at ${wt.cwd}` });
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
emitProgress({ type: "log", message: `isolation:'worktree' unavailable on machine '${parent.machine.name}' — running in the shared workspace` });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// Per-agent abort: workflow signal OR an absolute stall timeout.
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
const onAbort = () => controller.abort();
|
|
68
|
+
if (signal.aborted)
|
|
69
|
+
controller.abort();
|
|
70
|
+
else
|
|
71
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
72
|
+
const timer = setTimeout(() => controller.abort(), WORKFLOW_AGENT_TIMEOUT_MS);
|
|
73
|
+
// Each concurrent subagent gets its OWN SteerBus: the parent's tracks a
|
|
74
|
+
// single activeTurnId, so sharing it across parallel agents would corrupt
|
|
75
|
+
// the parent turn's steering state. Subagents are ephemeral and take no steers.
|
|
76
|
+
const childState = spawner.derive({ address, machine: runMachine, signal: controller.signal, steer: new SteerBus(), parentToolCallId });
|
|
77
|
+
const childContext = new ConversationContext({
|
|
78
|
+
store: parent.store,
|
|
79
|
+
address,
|
|
80
|
+
...(parent.store === undefined
|
|
81
|
+
? { onHistoryChange: historyChangeEmitter(parent.events, parent.sessionId) }
|
|
82
|
+
: {}),
|
|
83
|
+
});
|
|
84
|
+
let outputTokens = 0;
|
|
85
|
+
try {
|
|
86
|
+
// Plain agent — return the final text.
|
|
87
|
+
if (schema === undefined) {
|
|
88
|
+
const promptMessage = { role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() };
|
|
89
|
+
await guardSubagentInput(sub, [promptMessage], childState, spawner);
|
|
90
|
+
childContext.seed([promptMessage]);
|
|
91
|
+
const result = await spawner.run(sub, childContext, childState);
|
|
92
|
+
usageBox.usage = addUsage(usageBox.usage, result.usage);
|
|
93
|
+
return { value: result.output, outputTokens: result.usage.output, agentId, address };
|
|
94
|
+
}
|
|
95
|
+
// Structured agent — inject StructuredOutput, nudge-retry in-conversation.
|
|
96
|
+
const { tool, capture } = makeStructuredOutputTool(schema);
|
|
97
|
+
const childAgent = cloneAgentWithTool(sub, tool);
|
|
98
|
+
const promptMessage = {
|
|
99
|
+
role: "user",
|
|
100
|
+
content: [{ type: "text", text: prompt + structuredInstruction(schema) }],
|
|
101
|
+
timestamp: Date.now(),
|
|
102
|
+
};
|
|
103
|
+
await guardSubagentInput(childAgent, [promptMessage], childState, spawner);
|
|
104
|
+
childContext.seed([promptMessage]);
|
|
105
|
+
const maxAttempts = 3;
|
|
106
|
+
let lastError = "";
|
|
107
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
108
|
+
capture.called = false;
|
|
109
|
+
capture.value = undefined;
|
|
110
|
+
const result = await spawner.run(childAgent, childContext, childState);
|
|
111
|
+
usageBox.usage = addUsage(usageBox.usage, result.usage);
|
|
112
|
+
outputTokens += result.usage.output;
|
|
113
|
+
if (capture.called) {
|
|
114
|
+
const verdict = validateValue(schema, capture.value);
|
|
115
|
+
if (verdict.ok)
|
|
116
|
+
return { value: capture.value, outputTokens, agentId, address };
|
|
117
|
+
lastError = verdict.error;
|
|
118
|
+
childContext.appendMessage({
|
|
119
|
+
role: "user",
|
|
120
|
+
content: [{ type: "text", text: `Your StructuredOutput argument failed schema validation: ${lastError}. Call StructuredOutput again with corrected JSON.` }],
|
|
121
|
+
timestamp: Date.now(),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
lastError = "you did not call StructuredOutput";
|
|
126
|
+
childContext.appendMessage({
|
|
127
|
+
role: "user",
|
|
128
|
+
content: [{ type: "text", text: "You did not call the StructuredOutput tool. You MUST call it now with your answer." }],
|
|
129
|
+
timestamp: Date.now(),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
throw new Error(`subagent produced no schema-valid structured output after ${maxAttempts} attempts: ${lastError}`);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
clearTimeout(timer);
|
|
137
|
+
signal.removeEventListener("abort", onAbort);
|
|
138
|
+
await childContext.flush();
|
|
139
|
+
if (cleanupWorktree)
|
|
140
|
+
await cleanupWorktree();
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
const input = z.object({
|
|
146
|
+
script: z.string().optional().describe("Self-contained workflow script. Must begin with `export const meta = { name, description, phases }` (pure literal, no computed values) followed by the script body using agent()/parallel()/pipeline()/phase()."),
|
|
147
|
+
name: z.string().optional().describe("Name of a predefined workflow (from the workspace `.agents/workflows/`). Resolves to a self-contained script."),
|
|
148
|
+
scriptPath: z.string().optional().describe("Path to a workflow script file in the workspace. Every Workflow invocation persists its script under `.agents/runs/` and returns the path in the tool result. To iterate, edit that file with Write/Edit and re-invoke Workflow with the same `scriptPath` instead of re-sending the full script. Takes precedence over `script` and `name`."),
|
|
149
|
+
args: z.unknown().optional().describe("Value exposed to the script as the global `args` (pass real JSON, not a stringified value)."),
|
|
150
|
+
resumeFromRunId: z.string().optional().describe("Resume a prior run; unchanged agent() calls return cached results instantly."),
|
|
151
|
+
tokenBudget: z.number().optional().describe("Output-token target for the run; enables budget.remaining()-guarded loops in the script."),
|
|
152
|
+
run_in_background: z.boolean().optional().describe("Run detached and return a task_id immediately (recommended for long workflows). Poll progress + result with BackgroundOutput. Requires a BackgroundManager."),
|
|
153
|
+
});
|
|
154
|
+
return defineTool({
|
|
155
|
+
name: "Workflow",
|
|
156
|
+
description: workflowPrompt(spawner.available.map((a) => a.name)),
|
|
157
|
+
params: input,
|
|
158
|
+
resolve: (args) => ({
|
|
159
|
+
approvalRule: "Workflow",
|
|
160
|
+
accesses: ToolAccesses.all(),
|
|
161
|
+
controlFlow: true,
|
|
162
|
+
display: { title: "Workflow", detail: args.name ?? args.scriptPath ?? "inline script" },
|
|
163
|
+
run: async (ctx) => {
|
|
164
|
+
if (args.run_in_background === true && parent.store === undefined) {
|
|
165
|
+
return errResult("Background Workflow execution requires a durable session store because the result lives in the workflow journal.");
|
|
166
|
+
}
|
|
167
|
+
// ── Resolve the script source. Precedence: scriptPath > script > name > the journal. ──
|
|
168
|
+
//
|
|
169
|
+
// The journal entry is what makes a resume self-contained. Every other source is a
|
|
170
|
+
// path into the workspace — a file the model may have edited since, that a later run
|
|
171
|
+
// of the same workflow may have replaced, and that does not exist at all if the
|
|
172
|
+
// machine is a sandbox that has since been rebuilt. Resuming against a DIFFERENT
|
|
173
|
+
// script is worse than failing: the journal keys are chained hashes of each agent's
|
|
174
|
+
// prompt, so one changed prompt misses every cache entry after it and silently
|
|
175
|
+
// re-runs the whole workflow for real money.
|
|
176
|
+
let script = null;
|
|
177
|
+
const resumeId = args.resumeFromRunId;
|
|
178
|
+
const noExplicitSource = (args.scriptPath === undefined || args.scriptPath.length === 0) &&
|
|
179
|
+
(args.script === undefined || args.script.trim().length === 0) &&
|
|
180
|
+
(args.name === undefined || args.name.length === 0);
|
|
181
|
+
if (resumeId !== undefined && resumeId.length > 0 && noExplicitSource) {
|
|
182
|
+
const priorJournal = (parent.session.workflow ?? fallbackWorkflowManagerFor(parent.session)).newJournal(resumeId, ctx.toolCallId);
|
|
183
|
+
await priorJournal.load();
|
|
184
|
+
const recorded = priorJournal.recordedScript();
|
|
185
|
+
if (recorded === undefined) {
|
|
186
|
+
return errResult(`No recorded script for runId "${resumeId}" — that run predates script recording, or its journal is gone. Pass \`script\`/\`scriptPath\` explicitly.`);
|
|
187
|
+
}
|
|
188
|
+
script = recorded;
|
|
189
|
+
}
|
|
190
|
+
else if (args.scriptPath !== undefined && args.scriptPath.length > 0) {
|
|
191
|
+
try {
|
|
192
|
+
const safePath = await resolveToolPath(args.scriptPath, ctx.machine, "read");
|
|
193
|
+
script = await readTextFile(ctx.machine, safePath);
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
return errResult(`Could not read scriptPath: ${err instanceof Error ? err.message : String(err)}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else if (args.script !== undefined && args.script.trim().length > 0) {
|
|
200
|
+
script = args.script;
|
|
201
|
+
}
|
|
202
|
+
else if (args.name !== undefined && args.name.length > 0) {
|
|
203
|
+
script = await readNamedWorkflow(ctx.machine, args.name);
|
|
204
|
+
if (script === null)
|
|
205
|
+
return errResult(`No saved workflow named "${args.name}" in the workspace .agents/workflows/.`);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
return errResult("Provide one of: `scriptPath` (workspace file), `script` (inline), `name` (saved workflow), or `resumeFromRunId` alone to replay a recorded run.");
|
|
209
|
+
}
|
|
210
|
+
const parsed = parseWorkflow(script);
|
|
211
|
+
if ("error" in parsed)
|
|
212
|
+
return errResult(`Invalid workflow script: ${parsed.error}`);
|
|
213
|
+
const { meta, scriptBody } = parsed;
|
|
214
|
+
const runId = args.resumeFromRunId ?? newAgentId("wf");
|
|
215
|
+
const tokenBudget = args.tokenBudget ?? null;
|
|
216
|
+
// Persist the resolved script to the workspace and hand back its path, so the
|
|
217
|
+
// model can Write/Edit it and re-run via `scriptPath` instead of re-sending the whole
|
|
218
|
+
// script. Goes through the Machine → on a server it lands in the sandbox, never the
|
|
219
|
+
// host disk. Best-effort: a persist failure never fails the run (path is left undefined).
|
|
220
|
+
const persistedScriptPath = await persistRunScript(ctx.machine, meta.name, runId, script);
|
|
221
|
+
// Resume journals go through the workflow capability (session.workflow); with no
|
|
222
|
+
// capability attached, a per-session fallback manager binds them to the session's
|
|
223
|
+
// store (or in-memory when the session has none). Discovery needs no writes: the
|
|
224
|
+
// tool results below carry the run's lifecycle in `details`, and the `/workflows`
|
|
225
|
+
// list is a fold over the conversation (snapshot.ts).
|
|
226
|
+
const wf = parent.session.workflow ?? fallbackWorkflowManagerFor(parent.session);
|
|
227
|
+
const mkJournal = (id) => wf.newJournal(id, ctx.toolCallId);
|
|
228
|
+
// Timestamps are stamped HOST-side (Date.now is banned only inside the sandbox).
|
|
229
|
+
const startedAt = new Date().toISOString();
|
|
230
|
+
const runDetails = (status, endedAt, outputTokens) => ({
|
|
231
|
+
workflow: meta.name,
|
|
232
|
+
runId,
|
|
233
|
+
status,
|
|
234
|
+
background: args.run_in_background === true,
|
|
235
|
+
description: meta.description,
|
|
236
|
+
startedAt,
|
|
237
|
+
endedAt,
|
|
238
|
+
outputTokens,
|
|
239
|
+
});
|
|
240
|
+
const emitStorelessProgress = (event) => {
|
|
241
|
+
if (parent.store !== undefined)
|
|
242
|
+
return;
|
|
243
|
+
void parent.events.emit({
|
|
244
|
+
address: journalAddress(runId),
|
|
245
|
+
sessionId: parent.sessionId,
|
|
246
|
+
type: "workflow.progress",
|
|
247
|
+
runId,
|
|
248
|
+
...(ctx.toolCallId !== undefined ? { toolCallId: ctx.toolCallId } : {}),
|
|
249
|
+
progress: event,
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
/** Write the inputs before anything runs, so an interrupted run is still replayable. */
|
|
253
|
+
const openJournal = async (journal) => {
|
|
254
|
+
await journal.load();
|
|
255
|
+
// The SOURCE, not `scriptBody`: parseWorkflow strips the `meta` block off the front,
|
|
256
|
+
// so a body cannot be parsed again. A resume re-parses what it reads back.
|
|
257
|
+
await journal.recordRun(meta.name, script, args.args, {
|
|
258
|
+
parentAddress: parent.address,
|
|
259
|
+
parentToolCallId: ctx.toolCallId,
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
/** Write what the run produced. Makes the address self-contained: inputs, every agent
|
|
263
|
+
* result, and the payload — so a reader never has to replay just to see the answer. */
|
|
264
|
+
const closeJournal = async (journal, outcome) => {
|
|
265
|
+
await journal.recordOutcome({
|
|
266
|
+
status: outcome.ok ? "completed" : "failed",
|
|
267
|
+
ok: outcome.ok,
|
|
268
|
+
...(outcome.ok ? { result: outcome.result } : { error: outcome.error }),
|
|
269
|
+
failures: outcome.failures,
|
|
270
|
+
agentCount: outcome.agentCount,
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
/** The one execution boundary for every mode. It owns both journal brackets, so callers
|
|
274
|
+
* cannot forget the terminal record on failure or abort. */
|
|
275
|
+
const executeJournaled = async (journal, hooks) => {
|
|
276
|
+
await openJournal(journal);
|
|
277
|
+
try {
|
|
278
|
+
const outcome = await runWorkflow({ meta, scriptBody, args: args.args, hooks, journal });
|
|
279
|
+
await closeJournal(journal, outcome);
|
|
280
|
+
return outcome;
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
const events = journal.readEvents();
|
|
284
|
+
const failures = events.flatMap((event) => (event.type === "error" ? [event.error] : []));
|
|
285
|
+
await journal.recordOutcome({
|
|
286
|
+
status: isAbortError(error) ? "aborted" : "failed",
|
|
287
|
+
ok: false,
|
|
288
|
+
error: errorMessage(error),
|
|
289
|
+
failures,
|
|
290
|
+
agentCount: events.filter((event) => event.type === "started").length,
|
|
291
|
+
});
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
const payloadOf = (outcome) => safeJsonStringify({
|
|
296
|
+
runId,
|
|
297
|
+
name: meta.name,
|
|
298
|
+
ok: outcome.ok,
|
|
299
|
+
result: outcome.ok ? outcome.result : undefined,
|
|
300
|
+
error: outcome.ok ? undefined : outcome.error,
|
|
301
|
+
failures: outcome.failures,
|
|
302
|
+
agentCount: outcome.agentCount,
|
|
303
|
+
// Editable via Write/Edit; pass back as `scriptPath` to re-run without resending.
|
|
304
|
+
scriptPath: persistedScriptPath,
|
|
305
|
+
});
|
|
306
|
+
// ── Background path: register a task, return task_id, run detached ──
|
|
307
|
+
if (args.run_in_background === true) {
|
|
308
|
+
const registrar = asTaskRegistrar(parent.background);
|
|
309
|
+
if (registrar === undefined) {
|
|
310
|
+
return errResult("run_in_background requested but no BackgroundManager is attached to this session.");
|
|
311
|
+
}
|
|
312
|
+
const controller = new AbortController();
|
|
313
|
+
// `runStatus` rides the task into the settle notification, whose journaled tag
|
|
314
|
+
// is the durable settle record the workflow fold reads.
|
|
315
|
+
const run = async (_sink) => {
|
|
316
|
+
const journal = mkJournal(runId);
|
|
317
|
+
const usageBox = { usage: emptyUsage() };
|
|
318
|
+
// Public workflow.progress events come from the journal's publication-aware
|
|
319
|
+
// append. Background execution has no attached courtesy update outlet.
|
|
320
|
+
const hooks = buildHooks(() => undefined, controller.signal, tokenBudget, usageBox, ctx.toolCallId);
|
|
321
|
+
const outcome = await executeJournaled(journal, hooks);
|
|
322
|
+
return { runStatus: outcome.ok ? "completed" : "failed" };
|
|
323
|
+
};
|
|
324
|
+
const taskId = registrar.registerTask(new WorkflowBackgroundTask(run, meta.name, {
|
|
325
|
+
timeoutMs: 60 * 60 * 1000,
|
|
326
|
+
workflowName: meta.name,
|
|
327
|
+
runId,
|
|
328
|
+
address: journalAddress(runId),
|
|
329
|
+
parentAddress: parent.address,
|
|
330
|
+
toolCallId: ctx.toolCallId,
|
|
331
|
+
abort: () => controller.abort(),
|
|
332
|
+
}));
|
|
333
|
+
const status = registrar.getTask(taskId)?.status ?? "running";
|
|
334
|
+
return {
|
|
335
|
+
content: [
|
|
336
|
+
{
|
|
337
|
+
type: "text",
|
|
338
|
+
text: [
|
|
339
|
+
`task_id: ${taskId}`,
|
|
340
|
+
`runId: ${runId}`,
|
|
341
|
+
`workflow: ${meta.name}`,
|
|
342
|
+
`status: ${status}`,
|
|
343
|
+
"automatic_notification: true",
|
|
344
|
+
"",
|
|
345
|
+
`description: ${meta.description}`,
|
|
346
|
+
...(meta.phases ? [`phases: ${meta.phases.map((p) => p.title).join(" → ")}`] : []),
|
|
347
|
+
...(persistedScriptPath !== undefined ? [`persisted_script: ${persistedScriptPath} (edit with Write/Edit, re-run via scriptPath)`] : []),
|
|
348
|
+
"",
|
|
349
|
+
`next_step: A completion notice arrives automatically. BackgroundOutput(task_id="${taskId}", block=false) reads live progress or the final result; re-run cached with resumeFromRunId="${runId}".`,
|
|
350
|
+
].join("\n"),
|
|
351
|
+
},
|
|
352
|
+
],
|
|
353
|
+
// The spawn ack IS the discovery row: the fold opens the record from these details.
|
|
354
|
+
details: { ...runDetails("running"), taskId },
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
// ── Foreground path: run to completion, return the result ──
|
|
358
|
+
const journal = mkJournal(runId);
|
|
359
|
+
const usageBox = { usage: emptyUsage() };
|
|
360
|
+
const detachSignal = ctx.detachSignal;
|
|
361
|
+
const fgRegistrar = detachSignal !== undefined && parent.store !== undefined ? asTaskRegistrar(parent.background) : undefined;
|
|
362
|
+
// One emitter for the whole run, foreground or not. It used to be swappable — detach
|
|
363
|
+
// rewired progress from `ctx.onUpdate` to the background task's memory tail — which is
|
|
364
|
+
// exactly the split that made a detached run's progress a different thing from an
|
|
365
|
+
// attached one's.
|
|
366
|
+
//
|
|
367
|
+
// Two outlets, neither of them a buffer. The EVENT is how a watcher sees a step as it
|
|
368
|
+
// happens, and it is addressed to the run rather than to the tool call, so it keeps
|
|
369
|
+
// arriving after the run detaches and that call is long gone. `ctx.onUpdate` stays as
|
|
370
|
+
// the attached UI's courtesy, and does nothing once nobody is attached.
|
|
371
|
+
const emitTo = (event) => {
|
|
372
|
+
emitStorelessProgress(event);
|
|
373
|
+
ctx.onUpdate?.({ kind: "custom", customKind: "workflow", customData: event, text: renderWorkflowProgress(event) });
|
|
374
|
+
};
|
|
375
|
+
// Run on our OWN controller (bridged from ctx.signal) so detach can drop the kill bridge
|
|
376
|
+
// and hand the still-running workflow to a background task.
|
|
377
|
+
const controller = new AbortController();
|
|
378
|
+
const bridge = () => controller.abort();
|
|
379
|
+
if (ctx.signal.aborted)
|
|
380
|
+
controller.abort();
|
|
381
|
+
else
|
|
382
|
+
ctx.signal.addEventListener("abort", bridge, { once: true });
|
|
383
|
+
const hooks = buildHooks((event) => emitTo(event), controller.signal, tokenBudget, usageBox, ctx.toolCallId);
|
|
384
|
+
const outcomePromise = executeJournaled(journal, hooks);
|
|
385
|
+
// Non-detachable (no trigger / no manager): await inline as before.
|
|
386
|
+
if (detachSignal === undefined || fgRegistrar === undefined) {
|
|
387
|
+
try {
|
|
388
|
+
const outcome = await outcomePromise;
|
|
389
|
+
return {
|
|
390
|
+
content: [{ type: "text", text: payloadOf(outcome) }],
|
|
391
|
+
details: {
|
|
392
|
+
...runDetails(outcome.ok ? "completed" : "failed", new Date().toISOString(), usageBox.usage.output),
|
|
393
|
+
usage: usageBox.usage,
|
|
394
|
+
},
|
|
395
|
+
isError: !outcome.ok,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
return {
|
|
400
|
+
content: [{ type: "text", text: `Workflow run ${ctx.signal.aborted ? "aborted" : "failed"}: ${error instanceof Error ? error.message : String(error)}` }],
|
|
401
|
+
isError: true,
|
|
402
|
+
details: runDetails(ctx.signal.aborted ? "aborted" : "failed", new Date().toISOString(), usageBox.usage.output),
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
finally {
|
|
406
|
+
ctx.signal.removeEventListener("abort", bridge);
|
|
407
|
+
parent.usage = addUsage(parent.usage, usageBox.usage);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
// Detachable: race the run against the detach trigger.
|
|
411
|
+
ctx.onUpdate?.({ kind: "custom", customKind: "detachable" }); // UI: offer "move to background"
|
|
412
|
+
const detachP = new Promise((resolve) => {
|
|
413
|
+
const onDetach = () => resolve("detach");
|
|
414
|
+
if (detachSignal.aborted)
|
|
415
|
+
onDetach();
|
|
416
|
+
else
|
|
417
|
+
detachSignal.addEventListener("abort", onDetach, { once: true });
|
|
418
|
+
});
|
|
419
|
+
const winner = await Promise.race([outcomePromise.then(() => "done", () => "done"), detachP]);
|
|
420
|
+
if (winner === "detach") {
|
|
421
|
+
ctx.signal.removeEventListener("abort", bridge); // turn-end must not kill the now-background run
|
|
422
|
+
const taskId = fgRegistrar.registerTask(new WorkflowBackgroundTask(async () => {
|
|
423
|
+
const oc = await outcomePromise;
|
|
424
|
+
return { runStatus: oc.ok ? "completed" : "failed" };
|
|
425
|
+
}, meta.name, {
|
|
426
|
+
workflowName: meta.name,
|
|
427
|
+
runId,
|
|
428
|
+
address: journalAddress(runId),
|
|
429
|
+
parentAddress: parent.address,
|
|
430
|
+
toolCallId: ctx.toolCallId,
|
|
431
|
+
abort: () => controller.abort(),
|
|
432
|
+
}));
|
|
433
|
+
ctx.onUpdate?.({ kind: "custom", customKind: "detached", customData: { taskId } });
|
|
434
|
+
parent.usage = addUsage(parent.usage, usageBox.usage); // fold spend accrued so far
|
|
435
|
+
return {
|
|
436
|
+
content: [
|
|
437
|
+
{
|
|
438
|
+
type: "text",
|
|
439
|
+
text: [
|
|
440
|
+
`Moved to background task ${taskId}.`,
|
|
441
|
+
`runId: ${runId}`,
|
|
442
|
+
`workflow: ${meta.name}`,
|
|
443
|
+
"automatic_notification: true",
|
|
444
|
+
"",
|
|
445
|
+
`next_step: A completion notice arrives automatically. BackgroundOutput(task_id="${taskId}", block=false) reads live progress or the final result; re-run cached with resumeFromRunId="${runId}".`,
|
|
446
|
+
].join("\n"),
|
|
447
|
+
},
|
|
448
|
+
],
|
|
449
|
+
details: { ...runDetails("running"), taskId, movedToBackground: true },
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
// Completed in foreground.
|
|
453
|
+
ctx.signal.removeEventListener("abort", bridge);
|
|
454
|
+
try {
|
|
455
|
+
const outcome = await outcomePromise;
|
|
456
|
+
return {
|
|
457
|
+
content: [{ type: "text", text: payloadOf(outcome) }],
|
|
458
|
+
details: {
|
|
459
|
+
...runDetails(outcome.ok ? "completed" : "failed", new Date().toISOString(), usageBox.usage.output),
|
|
460
|
+
usage: usageBox.usage,
|
|
461
|
+
},
|
|
462
|
+
isError: !outcome.ok,
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
catch (error) {
|
|
466
|
+
return {
|
|
467
|
+
content: [{ type: "text", text: `Workflow run ${ctx.signal.aborted ? "aborted" : "failed"}: ${error instanceof Error ? error.message : String(error)}` }],
|
|
468
|
+
isError: true,
|
|
469
|
+
details: runDetails(ctx.signal.aborted ? "aborted" : "failed", new Date().toISOString(), usageBox.usage.output),
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
finally {
|
|
473
|
+
parent.usage = addUsage(parent.usage, usageBox.usage);
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
}),
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
// ── Local helpers (moved out of runner.ts with the tool) ──
|
|
480
|
+
const WORKFLOW_DIR = ".agents/workflows";
|
|
481
|
+
// Run-scratch lives OUTSIDE `.agents/workflows` so a read-only provisioned workflow library
|
|
482
|
+
// (e.g. an R2 mount at `.agents/workflows` on a server) never blocks the persist write.
|
|
483
|
+
const WORKFLOW_RUNS_DIR = ".agents/runs";
|
|
484
|
+
/**
|
|
485
|
+
* Persist the resolved script to `.agents/workflows/runs/<name>.js` in the workspace (via the
|
|
486
|
+
* Machine — sandbox on a server, never the host disk) and return that path.
|
|
487
|
+
* Every invocation drops an editable copy so the model can Write/Edit it and re-run via
|
|
488
|
+
* `scriptPath`. Best-effort — returns undefined (never throws) if the write fails, so persistence
|
|
489
|
+
* never blocks a run. Keyed by workflow name (stable across iterations of the "same" workflow);
|
|
490
|
+
* curated named workflows live one level up in `.agents/workflows/`, so `runs/` never shadows them.
|
|
491
|
+
*/
|
|
492
|
+
async function persistRunScript(machine, name, runId, script) {
|
|
493
|
+
// Named per RUN, not per workflow. Sharing a filename across runs of the same workflow meant
|
|
494
|
+
// the second run silently overwrote the first — taking with it any edit the model had made,
|
|
495
|
+
// and leaving a `resumeFromRunId` for the older run pointed at a script whose prompts no
|
|
496
|
+
// longer match its journal keys, so every cached agent misses and re-runs for real.
|
|
497
|
+
const safeName = name.replace(/[^a-zA-Z0-9._-]/g, "_").replace(/^\.+/, "") || "workflow";
|
|
498
|
+
const rel = `${WORKFLOW_RUNS_DIR}/${safeName}.${runId}.js`;
|
|
499
|
+
try {
|
|
500
|
+
await machine.mkdir(await resolveToolPath(WORKFLOW_RUNS_DIR, machine, "write"), { parents: true, existOk: true });
|
|
501
|
+
await writeTextFile(machine, await resolveToolPath(rel, machine, "write"), script);
|
|
502
|
+
return rel;
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
return undefined;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Read a saved workflow by name from the workspace `.agents/workflows/` dir, VIA the
|
|
510
|
+
* machine — so it resolves inside the sandbox workspace, never the host
|
|
511
|
+
* filesystem (the model writes these with Write; it reads them back from the same
|
|
512
|
+
* place). Tries `<name>`, `<name>.js`, `<name>.mjs`; returns null if none exist.
|
|
513
|
+
*/
|
|
514
|
+
async function readNamedWorkflow(machine, name) {
|
|
515
|
+
if (!/^[a-zA-Z0-9._-]+$/.test(name) || name.includes(".."))
|
|
516
|
+
return null;
|
|
517
|
+
for (const ext of ["", ".js", ".mjs"]) {
|
|
518
|
+
try {
|
|
519
|
+
const safePath = await resolveToolPath(`${WORKFLOW_DIR}/${name}${ext}`, machine, "read");
|
|
520
|
+
return await readTextFile(machine, safePath);
|
|
521
|
+
}
|
|
522
|
+
catch {
|
|
523
|
+
// not found / not this extension — try the next
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
function errResult(text) {
|
|
529
|
+
return { content: [{ type: "text", text }], isError: true };
|
|
530
|
+
}
|
|
531
|
+
function renderWorkflowProgress(event) {
|
|
532
|
+
if (event.type === "started")
|
|
533
|
+
return `▸ workflow: ${event.name}`;
|
|
534
|
+
if (event.type === "phase")
|
|
535
|
+
return `▸ phase: ${event.title}`;
|
|
536
|
+
if (event.type === "log")
|
|
537
|
+
return event.message;
|
|
538
|
+
if (event.type === "outcome")
|
|
539
|
+
return `${event.ok ? "✓" : "✗"} workflow ${event.status}${event.error ? `: ${event.error}` : ""}`;
|
|
540
|
+
const r = event.record;
|
|
541
|
+
if (r.state === "queued")
|
|
542
|
+
return `… [${r.index}] ${r.label}${r.phase ? ` (${r.phase})` : ""}`;
|
|
543
|
+
if (r.state === "running")
|
|
544
|
+
return `→ [${r.index}] ${r.label}${r.phase ? ` (${r.phase})` : ""}`;
|
|
545
|
+
if (r.state === "error")
|
|
546
|
+
return `✗ [${r.index}] ${r.label}: ${r.error ?? ""}`;
|
|
547
|
+
return `✓ [${r.index}] ${r.label}`;
|
|
548
|
+
}
|
|
549
|
+
function safeJsonStringify(value) {
|
|
550
|
+
try {
|
|
551
|
+
return JSON.stringify(value, null, 2);
|
|
552
|
+
}
|
|
553
|
+
catch {
|
|
554
|
+
return String(value);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
// One fallback per session (not per run): resume journals of every workflow run in the
|
|
558
|
+
// session must land in the same store. Keyed by the session object's identity (the port
|
|
559
|
+
// is the same Session instance across every frame of a session).
|
|
560
|
+
const fallbackWorkflowManagers = new WeakMap();
|
|
561
|
+
function fallbackWorkflowManagerFor(session) {
|
|
562
|
+
let manager = fallbackWorkflowManagers.get(session);
|
|
563
|
+
if (manager === undefined) {
|
|
564
|
+
manager = new WorkflowManager(session.store);
|
|
565
|
+
fallbackWorkflowManagers.set(session, manager);
|
|
566
|
+
}
|
|
567
|
+
return manager;
|
|
568
|
+
}
|
|
569
|
+
//# sourceMappingURL=tool.js.map
|