@bastani/atomic 0.8.13-0 → 0.8.14-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/host-html-template.ts +1 -1
- package/dist/builtin/mcp/init.ts +15 -2
- package/dist/builtin/mcp/mcp-callback-server.ts +10 -9
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/mcp/ui-session.ts +9 -6
- package/dist/builtin/subagents/CHANGELOG.md +8 -1
- package/dist/builtin/subagents/README.md +39 -32
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/skills/subagent/SKILL.md +11 -11
- package/dist/builtin/subagents/src/agents/agent-management.ts +6 -1
- package/dist/builtin/subagents/src/agents/agent-serializer.ts +2 -0
- package/dist/builtin/subagents/src/agents/agents.ts +44 -19
- package/dist/builtin/subagents/src/extension/config.ts +16 -0
- package/dist/builtin/subagents/src/extension/fanout-child.ts +246 -0
- package/dist/builtin/subagents/src/extension/index.ts +466 -603
- package/dist/builtin/subagents/src/intercom/intercom-bridge.ts +6 -4
- package/dist/builtin/subagents/src/intercom/result-intercom.ts +109 -1
- package/dist/builtin/subagents/src/runs/background/async-execution.ts +124 -19
- package/dist/builtin/subagents/src/runs/background/async-job-tracker.ts +41 -6
- package/dist/builtin/subagents/src/runs/background/async-resume.ts +28 -15
- package/dist/builtin/subagents/src/runs/background/async-status.ts +60 -30
- package/dist/builtin/subagents/src/runs/background/result-watcher.ts +111 -54
- package/dist/builtin/subagents/src/runs/background/run-id-resolver.ts +83 -0
- package/dist/builtin/subagents/src/runs/background/run-status.ts +79 -3
- package/dist/builtin/subagents/src/runs/background/stale-run-reconciler.ts +46 -1
- package/dist/builtin/subagents/src/runs/background/subagent-runner.ts +66 -14
- package/dist/builtin/subagents/src/runs/foreground/chain-execution.ts +10 -3
- package/dist/builtin/subagents/src/runs/foreground/execution.ts +14 -2
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor.ts +320 -23
- package/dist/builtin/subagents/src/runs/shared/completion-guard.ts +23 -1
- package/dist/builtin/subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +369 -0
- package/dist/builtin/subagents/src/runs/shared/nested-events.ts +935 -0
- package/dist/builtin/subagents/src/runs/shared/nested-path.ts +52 -0
- package/dist/builtin/subagents/src/runs/shared/nested-render.ts +115 -0
- package/dist/builtin/subagents/src/runs/shared/parallel-utils.ts +1 -0
- package/dist/builtin/subagents/src/runs/shared/pi-args.ts +82 -9
- package/dist/builtin/subagents/src/runs/shared/pi-spawn.ts +1 -1
- package/dist/builtin/subagents/src/runs/shared/single-output.ts +12 -2
- package/dist/builtin/subagents/src/runs/shared/subagent-prompt-runtime.ts +32 -10
- package/dist/builtin/subagents/src/runs/shared/worktree.ts +3 -2
- package/dist/builtin/subagents/src/shared/artifacts.ts +0 -1
- package/dist/builtin/subagents/src/shared/types.ts +96 -1
- package/dist/builtin/subagents/src/shared/utils.ts +10 -2
- package/dist/builtin/subagents/src/slash/slash-commands.ts +468 -625
- package/dist/builtin/subagents/src/tui/render.ts +1227 -2093
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +24 -0
- package/dist/builtin/workflows/README.md +28 -11
- package/dist/builtin/workflows/builtin/deep-research-codebase.ts +323 -40
- package/dist/builtin/workflows/builtin/ralph.ts +362 -176
- package/dist/builtin/workflows/package.json +2 -5
- package/dist/builtin/workflows/skills/research-codebase/SKILL.md +1 -1
- package/dist/builtin/workflows/skills/skill-creator/LICENSE.txt +202 -0
- package/dist/builtin/workflows/skills/skill-creator/SKILL.md +489 -0
- package/dist/builtin/workflows/skills/skill-creator/agents/analyzer.md +274 -0
- package/dist/builtin/workflows/skills/skill-creator/agents/comparator.md +202 -0
- package/dist/builtin/workflows/skills/skill-creator/agents/grader.md +223 -0
- package/dist/builtin/workflows/skills/skill-creator/assets/eval_review.html +146 -0
- package/dist/builtin/workflows/skills/skill-creator/eval-viewer/generate_review.py +471 -0
- package/dist/builtin/workflows/skills/skill-creator/eval-viewer/viewer.html +1325 -0
- package/dist/builtin/workflows/skills/skill-creator/references/schemas.md +430 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/__init__.py +0 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/aggregate_benchmark.py +401 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/generate_report.py +326 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/improve_description.py +247 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/package_skill.py +136 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/quick_validate.py +103 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/run_eval.py +310 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/run_loop.py +328 -0
- package/dist/builtin/workflows/skills/skill-creator/scripts/utils.py +47 -0
- package/dist/builtin/workflows/src/extension/index.ts +869 -93
- package/dist/builtin/workflows/src/extension/render-call.ts +34 -1
- package/dist/builtin/workflows/src/extension/render-result.ts +126 -21
- package/dist/builtin/workflows/src/extension/runtime.ts +91 -3
- package/dist/builtin/workflows/src/extension/wiring.ts +38 -12
- package/dist/builtin/workflows/src/extension/workflow-schema.ts +62 -5
- package/dist/builtin/workflows/src/runs/background/runner.ts +3 -3
- package/dist/builtin/workflows/src/runs/background/status.ts +42 -8
- package/dist/builtin/workflows/src/runs/foreground/executor.ts +410 -95
- package/dist/builtin/workflows/src/runs/foreground/stage-control-registry.ts +5 -2
- package/dist/builtin/workflows/src/runs/foreground/stage-runner.ts +8 -0
- package/dist/builtin/workflows/src/runs/shared/model-fallback.ts +6 -4
- package/dist/builtin/workflows/src/runs/shared/worktree.ts +3 -2
- package/dist/builtin/workflows/src/shared/persistence-restore.ts +138 -5
- package/dist/builtin/workflows/src/shared/persistence-session-entries.ts +30 -0
- package/dist/builtin/workflows/src/shared/render-inputs-schema.ts +78 -120
- package/dist/builtin/workflows/src/shared/stage-ui-broker.ts +193 -0
- package/dist/builtin/workflows/src/shared/store-types.ts +26 -1
- package/dist/builtin/workflows/src/shared/store.ts +145 -17
- package/dist/builtin/workflows/src/shared/timing.ts +6 -2
- package/dist/builtin/workflows/src/shared/workflow-failures.ts +375 -0
- package/dist/builtin/workflows/src/tui/chat-surface.ts +68 -17
- package/dist/builtin/workflows/src/tui/connectors.ts +2 -2
- package/dist/builtin/workflows/src/tui/dispatch-confirm.ts +24 -26
- package/dist/builtin/workflows/src/tui/graph-canvas.ts +4 -8
- package/dist/builtin/workflows/src/tui/graph-view.ts +17 -14
- package/dist/builtin/workflows/src/tui/header.ts +38 -0
- package/dist/builtin/workflows/src/tui/inline-form-card.ts +161 -238
- package/dist/builtin/workflows/src/tui/inline-form-editor.ts +68 -73
- package/dist/builtin/workflows/src/tui/inline-form-overlay.ts +2 -3
- package/dist/builtin/workflows/src/tui/inline-form-store.ts +2 -1
- package/dist/builtin/workflows/src/tui/inputs-overlay.ts +1 -3
- package/dist/builtin/workflows/src/tui/inputs-picker.ts +286 -399
- package/dist/builtin/workflows/src/tui/keybindings-adapter.ts +11 -0
- package/dist/builtin/workflows/src/tui/node-card.ts +2 -1
- package/dist/builtin/workflows/src/tui/overlay-adapter.ts +9 -1
- package/dist/builtin/workflows/src/tui/prompt-card.ts +46 -19
- package/dist/builtin/workflows/src/tui/run-detail.ts +63 -80
- package/dist/builtin/workflows/src/tui/session-confirm.ts +9 -3
- package/dist/builtin/workflows/src/tui/session-picker.ts +19 -16
- package/dist/builtin/workflows/src/tui/stage-chat-layout.ts +88 -0
- package/dist/builtin/workflows/src/tui/stage-chat-view.ts +368 -879
- package/dist/builtin/workflows/src/tui/status-helpers.ts +4 -0
- package/dist/builtin/workflows/src/tui/status-list.ts +67 -75
- package/dist/builtin/workflows/src/tui/store-widget-installer.ts +50 -12
- package/dist/builtin/workflows/src/tui/submit-pane.ts +164 -0
- package/dist/builtin/workflows/src/tui/switcher.ts +27 -4
- package/dist/builtin/workflows/src/tui/text-helpers.ts +98 -4
- package/dist/builtin/workflows/src/tui/widget.ts +90 -68
- package/dist/builtin/workflows/src/tui/workflow-attach-pane.ts +23 -2
- package/dist/builtin/workflows/src/tui/workflow-list.ts +44 -68
- package/dist/cli/file-processor.d.ts.map +1 -1
- package/dist/cli/file-processor.js +2 -3
- package/dist/cli/file-processor.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +3 -10
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +2 -1
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +3 -2
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +6 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +16 -2
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/atomic-guide-command.d.ts.map +1 -1
- package/dist/core/atomic-guide-command.js +8 -9
- package/dist/core/atomic-guide-command.js.map +1 -1
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +3 -2
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +2 -1
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/export-html/index.d.ts.map +1 -1
- package/dist/core/export-html/index.js +8 -6
- package/dist/core/export-html/index.js.map +1 -1
- package/dist/core/export-html/template.js +6 -3
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +12 -29
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +5 -1
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/package-manager.d.ts +8 -0
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +145 -58
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +6 -20
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +38 -31
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +9 -4
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +32 -24
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +8 -15
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +8 -22
- package/dist/core/skills.js.map +1 -1
- package/dist/core/tools/ask-user-question/state/questionnaire-session.d.ts +5 -4
- package/dist/core/tools/ask-user-question/state/questionnaire-session.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/state/questionnaire-session.js +34 -11
- package/dist/core/tools/ask-user-question/state/questionnaire-session.js.map +1 -1
- package/dist/core/tools/ask-user-question/state/selectors/contract.d.ts +1 -0
- package/dist/core/tools/ask-user-question/state/selectors/contract.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/state/selectors/contract.js.map +1 -1
- package/dist/core/tools/ask-user-question/state/selectors/projections.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/state/selectors/projections.js +1 -0
- package/dist/core/tools/ask-user-question/state/selectors/projections.js.map +1 -1
- package/dist/core/tools/ask-user-question/state/state-reducer.d.ts +1 -2
- package/dist/core/tools/ask-user-question/state/state-reducer.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/state/state-reducer.js +26 -9
- package/dist/core/tools/ask-user-question/state/state-reducer.js.map +1 -1
- package/dist/core/tools/ask-user-question/state/state.d.ts +4 -0
- package/dist/core/tools/ask-user-question/state/state.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/state/state.js.map +1 -1
- package/dist/core/tools/ask-user-question/view/components/option-list-view.d.ts +1 -0
- package/dist/core/tools/ask-user-question/view/components/option-list-view.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/view/components/option-list-view.js +1 -0
- package/dist/core/tools/ask-user-question/view/components/option-list-view.js.map +1 -1
- package/dist/core/tools/ask-user-question/view/components/wrapping-select.d.ts +9 -6
- package/dist/core/tools/ask-user-question/view/components/wrapping-select.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/view/components/wrapping-select.js +28 -7
- package/dist/core/tools/ask-user-question/view/components/wrapping-select.js.map +1 -1
- package/dist/core/tools/ask-user-question/view/props-adapter.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/view/props-adapter.js +4 -1
- package/dist/core/tools/ask-user-question/view/props-adapter.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +56 -53
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit-diff.d.ts +3 -1
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +8 -1
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/core/tools/edit.d.ts +3 -1
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +44 -81
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/file-mutation-queue.d.ts.map +1 -1
- package/dist/core/tools/file-mutation-queue.js +27 -12
- package/dist/core/tools/file-mutation-queue.js.map +1 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +2 -3
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +3 -3
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +5 -5
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/output-accumulator.d.ts +2 -0
- package/dist/core/tools/output-accumulator.d.ts.map +1 -1
- package/dist/core/tools/output-accumulator.js +11 -4
- package/dist/core/tools/output-accumulator.js.map +1 -1
- package/dist/core/tools/path-utils.d.ts +2 -0
- package/dist/core/tools/path-utils.d.ts.map +1 -1
- package/dist/core/tools/path-utils.js +39 -21
- package/dist/core/tools/path-utils.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +9 -8
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/truncate.d.ts.map +1 -1
- package/dist/core/tools/truncate.js +12 -2
- package/dist/core/tools/truncate.js.map +1 -1
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +20 -35
- package/dist/core/tools/write.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +5 -6
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/chat-input-actions.d.ts +24 -0
- package/dist/modes/interactive/chat-input-actions.d.ts.map +1 -0
- package/dist/modes/interactive/chat-input-actions.js +179 -0
- package/dist/modes/interactive/chat-input-actions.js.map +1 -0
- package/dist/modes/interactive/components/chat-message-renderer.d.ts +1 -0
- package/dist/modes/interactive/components/chat-message-renderer.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-message-renderer.js +14 -3
- package/dist/modes/interactive/components/chat-message-renderer.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts +157 -0
- package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -0
- package/dist/modes/interactive/components/chat-session-host.js +1007 -0
- package/dist/modes/interactive/components/chat-session-host.js.map +1 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/config-selector.js +1 -1
- package/dist/modes/interactive/components/config-selector.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +14 -5
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts +9 -1
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +29 -4
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +18 -67
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/utils/child-process.d.ts +1 -0
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +8 -0
- package/dist/utils/child-process.js.map +1 -1
- package/dist/utils/clipboard-native.d.ts +3 -1
- package/dist/utils/clipboard-native.d.ts.map +1 -1
- package/dist/utils/clipboard-native.js +14 -8
- package/dist/utils/clipboard-native.js.map +1 -1
- package/dist/utils/image-resize-core.d.ts +30 -0
- package/dist/utils/image-resize-core.d.ts.map +1 -0
- package/dist/utils/image-resize-core.js +124 -0
- package/dist/utils/image-resize-core.js.map +1 -0
- package/dist/utils/image-resize-worker.d.ts +2 -0
- package/dist/utils/image-resize-worker.d.ts.map +1 -0
- package/dist/utils/image-resize-worker.js +31 -0
- package/dist/utils/image-resize-worker.js.map +1 -0
- package/dist/utils/image-resize.d.ts +7 -27
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +75 -115
- package/dist/utils/image-resize.js.map +1 -1
- package/dist/utils/paths.d.ts +16 -1
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +49 -7
- package/dist/utils/paths.js.map +1 -1
- package/docs/changelog.mdx +29 -0
- package/docs/compaction.md +1 -1
- package/docs/custom-provider.md +2 -2
- package/docs/development.md +1 -1
- package/docs/docs.json +98 -143
- package/docs/extensions.md +29 -16
- package/docs/favicon.svg +29 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/images/workflow-command.png +0 -0
- package/docs/images/workflow-graph.png +0 -0
- package/docs/images/workflow-input-picker.png +0 -0
- package/docs/images/workflow-list.png +0 -0
- package/docs/index.md +10 -1
- package/docs/logo.svg +59 -0
- package/docs/packages.md +3 -3
- package/docs/providers.md +1 -1
- package/docs/quickstart.md +98 -2
- package/docs/rpc.md +8 -8
- package/docs/sdk.md +23 -12
- package/docs/sessions.md +1 -1
- package/docs/skills.md +15 -1
- package/docs/termux.md +11 -1
- package/docs/themes.md +6 -6
- package/docs/tui.md +18 -18
- package/docs/usage.md +1 -1
- package/docs/workflows.md +172 -2
- package/examples/extensions/subagent/index.ts +2 -1
- package/package.json +6 -6
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/SKILL.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/element-attributes.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/playwright-tests.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/request-mocking.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/running-code.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/session-management.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/spec-driven-testing.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/storage-state.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/test-generation.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/tracing.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/playwright-cli/references/video-recording.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/SKILL.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/deep-modules.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/interface-design.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/mocking.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/refactoring.md +0 -0
- /package/dist/builtin/{workflows → subagents}/skills/tdd/tests.md +0 -0
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
ASYNC_DIR,
|
|
6
|
+
RESULTS_DIR,
|
|
7
|
+
TEMP_ROOT_DIR,
|
|
8
|
+
type AsyncJobState,
|
|
9
|
+
type AsyncStatus,
|
|
10
|
+
type NestedRouteInfo,
|
|
11
|
+
type NestedRunSummary,
|
|
12
|
+
type NestedRunState,
|
|
13
|
+
type NestedStepSummary,
|
|
14
|
+
type SubagentRunMode,
|
|
15
|
+
type SubagentState,
|
|
16
|
+
} from "../../shared/types.ts";
|
|
17
|
+
import { isSafeNestedPathId, parseNestedPathEnv, sanitizeNestedPath, type NestedPathEntry } from "./nested-path.ts";
|
|
18
|
+
import {
|
|
19
|
+
SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV,
|
|
20
|
+
SUBAGENT_PARENT_CHILD_INDEX_ENV,
|
|
21
|
+
SUBAGENT_PARENT_CONTROL_INBOX_ENV,
|
|
22
|
+
SUBAGENT_PARENT_DEPTH_ENV,
|
|
23
|
+
SUBAGENT_PARENT_EVENT_SINK_ENV,
|
|
24
|
+
SUBAGENT_PARENT_PATH_ENV,
|
|
25
|
+
SUBAGENT_PARENT_ROOT_RUN_ID_ENV,
|
|
26
|
+
SUBAGENT_PARENT_RUN_ID_ENV,
|
|
27
|
+
SUBAGENT_PARENT_MAX_DEPTH,
|
|
28
|
+
} from "./pi-args.ts";
|
|
29
|
+
import { APP_NAME, getEnvValue } from "@bastani/atomic";
|
|
30
|
+
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
31
|
+
|
|
32
|
+
export const NESTED_EVENTS_DIR = path.join(TEMP_ROOT_DIR, "nested-subagent-events");
|
|
33
|
+
export const NESTED_RUNS_DIR = path.join(TEMP_ROOT_DIR, "nested-subagent-runs");
|
|
34
|
+
const ROUTE_FILE = "route.json";
|
|
35
|
+
const REGISTRY_FILE = "registry.json";
|
|
36
|
+
export const MAX_NESTED_EVENT_BYTES = 64 * 1024;
|
|
37
|
+
export const MAX_NESTED_STEPS = 12;
|
|
38
|
+
export const MAX_NESTED_CHILDREN = 16;
|
|
39
|
+
export const MAX_NESTED_DEPTH = SUBAGENT_PARENT_MAX_DEPTH;
|
|
40
|
+
export const MAX_PROCESSED_NESTED_EVENTS = 20_000;
|
|
41
|
+
const REGISTRY_LOCK_DIR = ".registry.lock";
|
|
42
|
+
const REGISTRY_LOCK_TIMEOUT_MS = 2_000;
|
|
43
|
+
const REGISTRY_LOCK_STALE_MS = 30_000;
|
|
44
|
+
const REGISTRY_LOCK_POLL_MS = 10;
|
|
45
|
+
|
|
46
|
+
type NestedStatusEventType = "subagent.nested.started" | "subagent.nested.updated" | "subagent.nested.completed";
|
|
47
|
+
type NestedControlResultEventType = "subagent.nested.control-result";
|
|
48
|
+
|
|
49
|
+
export type NestedRoute = NestedRouteInfo;
|
|
50
|
+
|
|
51
|
+
export interface NestedEventRecord {
|
|
52
|
+
type: NestedStatusEventType;
|
|
53
|
+
ts: number;
|
|
54
|
+
rootRunId: string;
|
|
55
|
+
parentRunId: string;
|
|
56
|
+
parentStepIndex?: number;
|
|
57
|
+
capabilityToken: string;
|
|
58
|
+
child: NestedRunSummary;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface NestedControlResultRecord {
|
|
62
|
+
type: NestedControlResultEventType;
|
|
63
|
+
ts: number;
|
|
64
|
+
rootRunId: string;
|
|
65
|
+
capabilityToken: string;
|
|
66
|
+
requestId: string;
|
|
67
|
+
targetRunId: string;
|
|
68
|
+
ok: boolean;
|
|
69
|
+
message: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface NestedControlRequestRecord {
|
|
73
|
+
type: "subagent.nested.control-request";
|
|
74
|
+
ts: number;
|
|
75
|
+
rootRunId: string;
|
|
76
|
+
capabilityToken: string;
|
|
77
|
+
requestId: string;
|
|
78
|
+
targetRunId: string;
|
|
79
|
+
action: "interrupt" | "resume";
|
|
80
|
+
message?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface NestedRegistry {
|
|
84
|
+
rootRunId: string;
|
|
85
|
+
updatedAt: number;
|
|
86
|
+
children: NestedRunSummary[];
|
|
87
|
+
processedEvents: string[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function isSafeNestedId(value: unknown): value is string {
|
|
91
|
+
return isSafeNestedPathId(value);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function assertSafeNestedId(label: string, value: string): void {
|
|
95
|
+
if (!isSafeNestedId(value)) throw new Error(`${label} must be a non-empty safe id token.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function assertSafeId(label: string, value: string): void {
|
|
99
|
+
assertSafeNestedId(label, value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function containedPath(base: string, candidate: string): boolean {
|
|
103
|
+
const resolvedBase = path.resolve(base);
|
|
104
|
+
const resolvedCandidate = path.resolve(candidate);
|
|
105
|
+
return resolvedCandidate === resolvedBase || resolvedCandidate.startsWith(`${resolvedBase}${path.sep}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function commonRouteRoot(route: Pick<NestedRoute, "eventSink" | "controlInbox">): string {
|
|
109
|
+
return path.dirname(path.resolve(route.eventSink));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function validateNestedRouteShape(route: NestedRoute): void {
|
|
113
|
+
assertSafeId("rootRunId", route.rootRunId);
|
|
114
|
+
assertSafeId("capabilityToken", route.capabilityToken);
|
|
115
|
+
if (!containedPath(NESTED_EVENTS_DIR, route.eventSink)) throw new Error("Nested event sink is outside the subagent nested event root.");
|
|
116
|
+
if (!containedPath(NESTED_EVENTS_DIR, route.controlInbox)) throw new Error("Nested control inbox is outside the subagent nested event root.");
|
|
117
|
+
if (commonRouteRoot(route) !== path.dirname(path.resolve(route.controlInbox))) throw new Error("Nested event sink and control inbox must share one route root.");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function validateRouteShape(route: NestedRoute): void {
|
|
121
|
+
validateNestedRouteShape(route);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function createNestedRoute(rootRunId: string): NestedRoute {
|
|
125
|
+
assertSafeId("rootRunId", rootRunId);
|
|
126
|
+
const capabilityToken = randomUUID();
|
|
127
|
+
const routeRoot = path.join(NESTED_EVENTS_DIR, `${rootRunId}-${capabilityToken}`);
|
|
128
|
+
const eventSink = path.join(routeRoot, "events");
|
|
129
|
+
const controlInbox = path.join(routeRoot, "controls");
|
|
130
|
+
fs.mkdirSync(eventSink, { recursive: true, mode: 0o700 });
|
|
131
|
+
fs.mkdirSync(controlInbox, { recursive: true, mode: 0o700 });
|
|
132
|
+
fs.writeFileSync(path.join(routeRoot, ROUTE_FILE), `${JSON.stringify({ rootRunId, capabilityToken, createdAt: Date.now() })}\n`, { mode: 0o600 });
|
|
133
|
+
return { rootRunId, eventSink, controlInbox, capabilityToken };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function newestMtimeMs(filePath: string): number {
|
|
137
|
+
let newest = fs.statSync(filePath).mtimeMs;
|
|
138
|
+
let entries: string[];
|
|
139
|
+
try {
|
|
140
|
+
entries = fs.readdirSync(filePath);
|
|
141
|
+
} catch {
|
|
142
|
+
return newest;
|
|
143
|
+
}
|
|
144
|
+
for (const entry of entries) {
|
|
145
|
+
const childPath = path.join(filePath, entry);
|
|
146
|
+
try {
|
|
147
|
+
const stat = fs.statSync(childPath);
|
|
148
|
+
newest = Math.max(newest, stat.isDirectory() ? newestMtimeMs(childPath) : stat.mtimeMs);
|
|
149
|
+
} catch {
|
|
150
|
+
// Nested runtime cleanup is best-effort housekeeping.
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return newest;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function cleanupOldSubdirectories(root: string, maxAgeDays: number): void {
|
|
157
|
+
let entries: string[];
|
|
158
|
+
try {
|
|
159
|
+
entries = fs.readdirSync(root);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
const cutoff = Date.now() - maxAgeDays * 24 * 60 * 60 * 1000;
|
|
165
|
+
for (const entry of entries) {
|
|
166
|
+
const entryPath = path.join(root, entry);
|
|
167
|
+
try {
|
|
168
|
+
if (newestMtimeMs(entryPath) < cutoff) fs.rmSync(entryPath, { recursive: true, force: true });
|
|
169
|
+
} catch {
|
|
170
|
+
// Keep startup resilient if a child process removes or rewrites an entry while scanning.
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function cleanupOldNestedRuntimeDirs(maxAgeDays: number): void {
|
|
176
|
+
cleanupOldSubdirectories(NESTED_EVENTS_DIR, maxAgeDays);
|
|
177
|
+
cleanupOldSubdirectories(NESTED_RUNS_DIR, maxAgeDays);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function readSubagentEnv(env: NodeJS.ProcessEnv, name: string): string | undefined {
|
|
181
|
+
if (env === process.env) return getEnvValue(name);
|
|
182
|
+
// Atomic keeps reading legacy pi-prefixed env vars so older parent processes can route nested children.
|
|
183
|
+
const legacyName = name.replace(/^[A-Z0-9]+_/, "PI_");
|
|
184
|
+
return env[name] ?? (legacyName === name ? undefined : env[legacyName]);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function resolveNestedRouteFromEnv(env: NodeJS.ProcessEnv = process.env): NestedRoute | undefined {
|
|
188
|
+
const rootRunId = readSubagentEnv(env, SUBAGENT_PARENT_ROOT_RUN_ID_ENV);
|
|
189
|
+
const eventSink = readSubagentEnv(env, SUBAGENT_PARENT_EVENT_SINK_ENV);
|
|
190
|
+
const controlInbox = readSubagentEnv(env, SUBAGENT_PARENT_CONTROL_INBOX_ENV);
|
|
191
|
+
const capabilityToken = readSubagentEnv(env, SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV);
|
|
192
|
+
if (!rootRunId || !eventSink || !controlInbox || !capabilityToken) return undefined;
|
|
193
|
+
const route = { rootRunId, eventSink, controlInbox, capabilityToken };
|
|
194
|
+
validateRouteShape(route);
|
|
195
|
+
const routeFile = path.join(commonRouteRoot(route), ROUTE_FILE);
|
|
196
|
+
const metadata = JSON.parse(fs.readFileSync(routeFile, "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
|
|
197
|
+
if (metadata.rootRunId !== rootRunId || metadata.capabilityToken !== capabilityToken) {
|
|
198
|
+
throw new Error("Nested event route metadata does not match the provided root id and capability token.");
|
|
199
|
+
}
|
|
200
|
+
return route;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function resolveInheritedNestedRouteFromEnv(env: NodeJS.ProcessEnv = process.env): NestedRoute | undefined {
|
|
204
|
+
try {
|
|
205
|
+
return resolveNestedRouteFromEnv(env);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
console.error("Ignoring invalid nested subagent event route:", error);
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function resolveNestedParentAddressFromEnv(env: NodeJS.ProcessEnv = process.env): { parentRunId: string; parentStepIndex?: number; depth: number; path: NestedPathEntry[] } | undefined {
|
|
213
|
+
const parentRunId = readSubagentEnv(env, SUBAGENT_PARENT_RUN_ID_ENV);
|
|
214
|
+
if (!isSafeNestedId(parentRunId)) return undefined;
|
|
215
|
+
const rawIndex = readSubagentEnv(env, SUBAGENT_PARENT_CHILD_INDEX_ENV);
|
|
216
|
+
const parentStepIndex = rawIndex && /^\d+$/.test(rawIndex) ? Number(rawIndex) : undefined;
|
|
217
|
+
const depth = Math.min(Math.max(1, clampNumber(Number(readSubagentEnv(env, SUBAGENT_PARENT_DEPTH_ENV))) ?? 1), MAX_NESTED_DEPTH);
|
|
218
|
+
const parsedPath = parseNestedPathEnv(readSubagentEnv(env, SUBAGENT_PARENT_PATH_ENV));
|
|
219
|
+
const nestedPath = parsedPath.length ? parsedPath : [{ runId: parentRunId, ...(parentStepIndex !== undefined ? { stepIndex: parentStepIndex } : {}) }];
|
|
220
|
+
return { parentRunId, ...(parentStepIndex !== undefined ? { parentStepIndex } : {}), depth, path: nestedPath };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function resolveNestedAsyncDir(rootRunId: string, run: NestedRunSummary): string | undefined {
|
|
224
|
+
if (!run.asyncDir) return undefined;
|
|
225
|
+
const resolved = path.resolve(run.asyncDir);
|
|
226
|
+
const nestedRoot = path.resolve(NESTED_RUNS_DIR, rootRunId, run.id);
|
|
227
|
+
const relative = path.relative(nestedRoot, resolved);
|
|
228
|
+
return resolved === nestedRoot || (!relative.startsWith("..") && !path.isAbsolute(relative)) ? resolved : undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function clampNumber(value: unknown): number | undefined {
|
|
232
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function stringValue(value: unknown, max = 512): string | undefined {
|
|
236
|
+
return typeof value === "string" && value.length > 0 ? value.slice(0, max) : undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function sanitizeTokenUsage(value: unknown): NestedRunSummary["totalTokens"] | undefined {
|
|
240
|
+
if (!value || typeof value !== "object") return undefined;
|
|
241
|
+
const raw = value as Record<string, unknown>;
|
|
242
|
+
const input = clampNumber(raw.input);
|
|
243
|
+
const output = clampNumber(raw.output);
|
|
244
|
+
const total = clampNumber(raw.total);
|
|
245
|
+
return input !== undefined && output !== undefined && total !== undefined
|
|
246
|
+
? { input, output, total }
|
|
247
|
+
: undefined;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function sanitizeState(value: unknown, fallback: NestedRunState): NestedRunState {
|
|
251
|
+
return value === "queued" || value === "running" || value === "complete" || value === "failed" || value === "paused"
|
|
252
|
+
? value
|
|
253
|
+
: fallback;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function sanitizeStep(input: unknown, depth: number): NestedStepSummary | undefined {
|
|
257
|
+
if (!input || typeof input !== "object") return undefined;
|
|
258
|
+
const raw = input as Record<string, unknown>;
|
|
259
|
+
const agent = stringValue(raw.agent, 128);
|
|
260
|
+
if (!agent) return undefined;
|
|
261
|
+
const status = raw.status === "pending" || raw.status === "running" || raw.status === "complete" || raw.status === "completed" || raw.status === "failed" || raw.status === "paused"
|
|
262
|
+
? raw.status
|
|
263
|
+
: "pending";
|
|
264
|
+
return {
|
|
265
|
+
agent,
|
|
266
|
+
status: status === "completed" ? "complete" : status,
|
|
267
|
+
...(stringValue(raw.sessionFile, 2048) ? { sessionFile: stringValue(raw.sessionFile, 2048) } : {}),
|
|
268
|
+
...(raw.activityState === "active_long_running" || raw.activityState === "needs_attention" ? { activityState: raw.activityState } : {}),
|
|
269
|
+
...(clampNumber(raw.lastActivityAt) !== undefined ? { lastActivityAt: clampNumber(raw.lastActivityAt) } : {}),
|
|
270
|
+
...(stringValue(raw.currentTool, 128) ? { currentTool: stringValue(raw.currentTool, 128) } : {}),
|
|
271
|
+
...(clampNumber(raw.currentToolStartedAt) !== undefined ? { currentToolStartedAt: clampNumber(raw.currentToolStartedAt) } : {}),
|
|
272
|
+
...(stringValue(raw.currentPath, 2048) ? { currentPath: stringValue(raw.currentPath, 2048) } : {}),
|
|
273
|
+
...(clampNumber(raw.turnCount) !== undefined ? { turnCount: clampNumber(raw.turnCount) } : {}),
|
|
274
|
+
...(clampNumber(raw.toolCount) !== undefined ? { toolCount: clampNumber(raw.toolCount) } : {}),
|
|
275
|
+
...(clampNumber(raw.startedAt) !== undefined ? { startedAt: clampNumber(raw.startedAt) } : {}),
|
|
276
|
+
...(clampNumber(raw.endedAt) !== undefined ? { endedAt: clampNumber(raw.endedAt) } : {}),
|
|
277
|
+
...(stringValue(raw.error, 1024) ? { error: stringValue(raw.error, 1024) } : {}),
|
|
278
|
+
...(depth < MAX_NESTED_DEPTH && Array.isArray(raw.children) ? { children: raw.children.map((child) => sanitizeSummary(child, depth + 1)).filter((child): child is NestedRunSummary => Boolean(child)).slice(0, MAX_NESTED_CHILDREN) } : {}),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function sanitizeSummary(input: unknown, depth = 0): NestedRunSummary | undefined {
|
|
283
|
+
if (!input || typeof input !== "object") return undefined;
|
|
284
|
+
const raw = input as Record<string, unknown>;
|
|
285
|
+
if (!isSafeNestedId(raw.id) || !isSafeNestedId(raw.parentRunId)) return undefined;
|
|
286
|
+
const pathParts = sanitizeNestedPath(raw.path);
|
|
287
|
+
const steps = Array.isArray(raw.steps)
|
|
288
|
+
? raw.steps.map((step) => sanitizeStep(step, depth + 1)).filter((step): step is NestedStepSummary => Boolean(step)).slice(0, MAX_NESTED_STEPS)
|
|
289
|
+
: undefined;
|
|
290
|
+
const totalTokens = sanitizeTokenUsage(raw.totalTokens);
|
|
291
|
+
return {
|
|
292
|
+
id: raw.id,
|
|
293
|
+
parentRunId: raw.parentRunId,
|
|
294
|
+
...(clampNumber(raw.parentStepIndex) !== undefined ? { parentStepIndex: clampNumber(raw.parentStepIndex) } : {}),
|
|
295
|
+
...(stringValue(raw.parentAgent, 128) ? { parentAgent: stringValue(raw.parentAgent, 128) } : {}),
|
|
296
|
+
depth: Math.min(Math.max(0, clampNumber(raw.depth) ?? 0), MAX_NESTED_DEPTH),
|
|
297
|
+
path: pathParts,
|
|
298
|
+
state: sanitizeState(raw.state, "running"),
|
|
299
|
+
...(stringValue(raw.asyncDir, 2048) ? { asyncDir: stringValue(raw.asyncDir, 2048) } : {}),
|
|
300
|
+
...(clampNumber(raw.pid) !== undefined && clampNumber(raw.pid)! > 0 && Number.isInteger(clampNumber(raw.pid)) ? { pid: clampNumber(raw.pid) } : {}),
|
|
301
|
+
...(stringValue(raw.sessionId, 256) ? { sessionId: stringValue(raw.sessionId, 256) } : {}),
|
|
302
|
+
...(stringValue(raw.sessionFile, 2048) ? { sessionFile: stringValue(raw.sessionFile, 2048) } : {}),
|
|
303
|
+
...(stringValue(raw.intercomTarget, 256) ? { intercomTarget: stringValue(raw.intercomTarget, 256) } : {}),
|
|
304
|
+
...(stringValue(raw.ownerIntercomTarget, 256) ? { ownerIntercomTarget: stringValue(raw.ownerIntercomTarget, 256) } : {}),
|
|
305
|
+
...(stringValue(raw.leafIntercomTarget, 256) ? { leafIntercomTarget: stringValue(raw.leafIntercomTarget, 256) } : {}),
|
|
306
|
+
...(raw.ownerState === "live" || raw.ownerState === "gone" || raw.ownerState === "unknown" ? { ownerState: raw.ownerState } : {}),
|
|
307
|
+
...(stringValue(raw.controlInbox, 2048) ? { controlInbox: stringValue(raw.controlInbox, 2048) } : {}),
|
|
308
|
+
...(stringValue(raw.capabilityToken, 128) ? { capabilityToken: stringValue(raw.capabilityToken, 128) } : {}),
|
|
309
|
+
...(raw.mode === "single" || raw.mode === "parallel" || raw.mode === "chain" ? { mode: raw.mode } : {}),
|
|
310
|
+
...(stringValue(raw.agent, 128) ? { agent: stringValue(raw.agent, 128) } : {}),
|
|
311
|
+
...(Array.isArray(raw.agents) ? { agents: raw.agents.map((agent) => stringValue(agent, 128)).filter((agent): agent is string => Boolean(agent)).slice(0, MAX_NESTED_STEPS) } : {}),
|
|
312
|
+
...(clampNumber(raw.currentStep) !== undefined ? { currentStep: clampNumber(raw.currentStep) } : {}),
|
|
313
|
+
...(clampNumber(raw.chainStepCount) !== undefined ? { chainStepCount: clampNumber(raw.chainStepCount) } : {}),
|
|
314
|
+
...(raw.activityState === "active_long_running" || raw.activityState === "needs_attention" ? { activityState: raw.activityState } : {}),
|
|
315
|
+
...(clampNumber(raw.lastActivityAt) !== undefined ? { lastActivityAt: clampNumber(raw.lastActivityAt) } : {}),
|
|
316
|
+
...(stringValue(raw.currentTool, 128) ? { currentTool: stringValue(raw.currentTool, 128) } : {}),
|
|
317
|
+
...(clampNumber(raw.currentToolStartedAt) !== undefined ? { currentToolStartedAt: clampNumber(raw.currentToolStartedAt) } : {}),
|
|
318
|
+
...(stringValue(raw.currentPath, 2048) ? { currentPath: stringValue(raw.currentPath, 2048) } : {}),
|
|
319
|
+
...(clampNumber(raw.turnCount) !== undefined ? { turnCount: clampNumber(raw.turnCount) } : {}),
|
|
320
|
+
...(clampNumber(raw.toolCount) !== undefined ? { toolCount: clampNumber(raw.toolCount) } : {}),
|
|
321
|
+
...(totalTokens ? { totalTokens } : {}),
|
|
322
|
+
...(clampNumber(raw.startedAt) !== undefined ? { startedAt: clampNumber(raw.startedAt) } : {}),
|
|
323
|
+
...(clampNumber(raw.endedAt) !== undefined ? { endedAt: clampNumber(raw.endedAt) } : {}),
|
|
324
|
+
...(clampNumber(raw.lastUpdate) !== undefined ? { lastUpdate: clampNumber(raw.lastUpdate) } : {}),
|
|
325
|
+
...(stringValue(raw.error, 1024) ? { error: stringValue(raw.error, 1024) } : {}),
|
|
326
|
+
...(steps && steps.length > 0 ? { steps } : {}),
|
|
327
|
+
...(depth < MAX_NESTED_DEPTH && Array.isArray(raw.children) ? { children: raw.children.map((child) => sanitizeSummary(child, depth + 1)).filter((child): child is NestedRunSummary => Boolean(child)).slice(0, MAX_NESTED_CHILDREN) } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function parseRecord(content: string, route: NestedRoute): NestedEventRecord | undefined {
|
|
332
|
+
if (Buffer.byteLength(content, "utf-8") > MAX_NESTED_EVENT_BYTES) return undefined;
|
|
333
|
+
let parsed: unknown;
|
|
334
|
+
try {
|
|
335
|
+
parsed = JSON.parse(content);
|
|
336
|
+
} catch {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
340
|
+
const raw = parsed as Record<string, unknown>;
|
|
341
|
+
if (raw.type !== "subagent.nested.started" && raw.type !== "subagent.nested.updated" && raw.type !== "subagent.nested.completed") return undefined;
|
|
342
|
+
if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
|
|
343
|
+
if (!isSafeNestedId(raw.parentRunId)) return undefined;
|
|
344
|
+
const ts = clampNumber(raw.ts);
|
|
345
|
+
if (ts === undefined) return undefined;
|
|
346
|
+
const child = sanitizeSummary(raw.child);
|
|
347
|
+
if (!child || child.id === route.rootRunId) return undefined;
|
|
348
|
+
const routedChild: NestedRunSummary = {
|
|
349
|
+
...child,
|
|
350
|
+
controlInbox: route.controlInbox,
|
|
351
|
+
capabilityToken: route.capabilityToken,
|
|
352
|
+
ownerState: child.ownerState ?? "unknown",
|
|
353
|
+
};
|
|
354
|
+
return {
|
|
355
|
+
type: raw.type,
|
|
356
|
+
ts,
|
|
357
|
+
rootRunId: route.rootRunId,
|
|
358
|
+
parentRunId: raw.parentRunId,
|
|
359
|
+
...(clampNumber(raw.parentStepIndex) !== undefined ? { parentStepIndex: clampNumber(raw.parentStepIndex) } : {}),
|
|
360
|
+
capabilityToken: route.capabilityToken,
|
|
361
|
+
child: routedChild,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function parseNestedEventRecords(content: string, route: NestedRoute): NestedEventRecord[] {
|
|
366
|
+
if (!content.includes("\n")) {
|
|
367
|
+
const record = parseRecord(content.trim(), route);
|
|
368
|
+
return record ? [record] : [];
|
|
369
|
+
}
|
|
370
|
+
return content.split("\n")
|
|
371
|
+
.slice(0, content.endsWith("\n") ? undefined : -1)
|
|
372
|
+
.map((line) => line.trim() ? parseRecord(line, route) : undefined)
|
|
373
|
+
.filter((event): event is NestedEventRecord => Boolean(event));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function terminal(state: NestedRunState): boolean {
|
|
377
|
+
return state === "complete" || state === "failed" || state === "paused";
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function mergeSummary(existing: NestedRunSummary | undefined, event: NestedEventRecord): NestedRunSummary {
|
|
381
|
+
const incomingState = event.type === "subagent.nested.completed" && event.child.state === "running" ? "complete" : event.child.state;
|
|
382
|
+
const incoming = { ...event.child, state: incomingState, lastUpdate: event.child.lastUpdate ?? event.ts };
|
|
383
|
+
if (!existing) return incoming;
|
|
384
|
+
const existingUpdate = existing.lastUpdate ?? 0;
|
|
385
|
+
const incomingUpdate = incoming.lastUpdate ?? event.ts;
|
|
386
|
+
if (incomingUpdate < existingUpdate) return existing;
|
|
387
|
+
if (terminal(existing.state) && !terminal(incoming.state)) return existing;
|
|
388
|
+
if (terminal(existing.state) && terminal(incoming.state) && incomingUpdate === existingUpdate) return existing;
|
|
389
|
+
return { ...existing, ...incoming, state: incoming.state, lastUpdate: Math.max(existingUpdate, incomingUpdate) };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function attachChild(children: NestedRunSummary[], event: NestedEventRecord): NestedRunSummary[] {
|
|
393
|
+
let updated = false;
|
|
394
|
+
const walk = (items: NestedRunSummary[]): NestedRunSummary[] => items.map((item) => {
|
|
395
|
+
if (item.id === event.parentRunId) {
|
|
396
|
+
const existingChildren = item.children ?? [];
|
|
397
|
+
const childIndex = existingChildren.findIndex((child) => child.id === event.child.id);
|
|
398
|
+
const nextChild = mergeSummary(childIndex >= 0 ? existingChildren[childIndex] : undefined, event);
|
|
399
|
+
const nextChildren = childIndex >= 0
|
|
400
|
+
? existingChildren.map((child, index) => index === childIndex ? nextChild : child)
|
|
401
|
+
: [...existingChildren, nextChild];
|
|
402
|
+
updated = true;
|
|
403
|
+
return { ...item, children: nextChildren.slice(0, MAX_NESTED_CHILDREN), lastUpdate: Math.max(item.lastUpdate ?? 0, event.ts) };
|
|
404
|
+
}
|
|
405
|
+
if (!item.children?.length) return item;
|
|
406
|
+
const nextChildren = walk(item.children);
|
|
407
|
+
return nextChildren === item.children ? item : { ...item, children: nextChildren };
|
|
408
|
+
});
|
|
409
|
+
const next = walk(children);
|
|
410
|
+
if (updated) return next;
|
|
411
|
+
const childIndex = next.findIndex((child) => child.id === event.child.id);
|
|
412
|
+
const nextChild = mergeSummary(childIndex >= 0 ? next[childIndex] : undefined, event);
|
|
413
|
+
return childIndex >= 0
|
|
414
|
+
? next.map((child, index) => index === childIndex ? nextChild : child)
|
|
415
|
+
: [...next, nextChild].slice(0, MAX_NESTED_CHILDREN);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function applyNestedEvent(registry: NestedRegistry, event: NestedEventRecord): NestedRegistry {
|
|
419
|
+
return {
|
|
420
|
+
...registry,
|
|
421
|
+
updatedAt: Math.max(registry.updatedAt, event.ts),
|
|
422
|
+
children: attachChild(registry.children, event),
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function registryPath(route: NestedRoute): string {
|
|
427
|
+
return path.join(commonRouteRoot(route), REGISTRY_FILE);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function registryLockPath(route: NestedRoute): string {
|
|
431
|
+
return path.join(commonRouteRoot(route), REGISTRY_LOCK_DIR);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function sleepSync(ms: number): void {
|
|
435
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function acquireRegistryLock(route: NestedRoute): () => void {
|
|
439
|
+
const lockPath = registryLockPath(route);
|
|
440
|
+
const deadline = Date.now() + REGISTRY_LOCK_TIMEOUT_MS;
|
|
441
|
+
while (true) {
|
|
442
|
+
try {
|
|
443
|
+
fs.mkdirSync(lockPath, { mode: 0o700 });
|
|
444
|
+
try {
|
|
445
|
+
fs.writeFileSync(path.join(lockPath, "owner.json"), `${JSON.stringify({ pid: process.pid, createdAt: Date.now() })}\n`, { mode: 0o600 });
|
|
446
|
+
} catch {
|
|
447
|
+
// Lock ownership metadata is diagnostic only.
|
|
448
|
+
}
|
|
449
|
+
return () => fs.rmSync(lockPath, { recursive: true, force: true });
|
|
450
|
+
} catch (error) {
|
|
451
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
452
|
+
if (code !== "EEXIST") throw error;
|
|
453
|
+
try {
|
|
454
|
+
const stat = fs.statSync(lockPath);
|
|
455
|
+
if (Date.now() - stat.mtimeMs > REGISTRY_LOCK_STALE_MS) {
|
|
456
|
+
fs.rmSync(lockPath, { recursive: true, force: true });
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
} catch (statError) {
|
|
460
|
+
if ((statError as NodeJS.ErrnoException).code !== "ENOENT") throw statError;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (Date.now() >= deadline) throw new Error(`Timed out waiting for nested registry lock for root '${route.rootRunId}'.`);
|
|
464
|
+
sleepSync(REGISTRY_LOCK_POLL_MS);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function findNestedRouteForRootId(rootRunId: string): NestedRoute | undefined {
|
|
470
|
+
assertSafeId("rootRunId", rootRunId);
|
|
471
|
+
let entries: string[];
|
|
472
|
+
try {
|
|
473
|
+
entries = fs.readdirSync(NESTED_EVENTS_DIR);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
476
|
+
throw error;
|
|
477
|
+
}
|
|
478
|
+
for (const entry of entries) {
|
|
479
|
+
if (!entry.startsWith(`${rootRunId}-`)) continue;
|
|
480
|
+
const routeRoot = path.join(NESTED_EVENTS_DIR, entry);
|
|
481
|
+
try {
|
|
482
|
+
const metadata = JSON.parse(fs.readFileSync(path.join(routeRoot, ROUTE_FILE), "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
|
|
483
|
+
if (metadata.rootRunId !== rootRunId || typeof metadata.capabilityToken !== "string") continue;
|
|
484
|
+
const route = {
|
|
485
|
+
rootRunId,
|
|
486
|
+
eventSink: path.join(routeRoot, "events"),
|
|
487
|
+
controlInbox: path.join(routeRoot, "controls"),
|
|
488
|
+
capabilityToken: metadata.capabilityToken,
|
|
489
|
+
};
|
|
490
|
+
validateRouteShape(route);
|
|
491
|
+
return route;
|
|
492
|
+
} catch (error) {
|
|
493
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
494
|
+
console.error(`Ignoring unreadable nested route metadata under '${routeRoot}':`, error);
|
|
495
|
+
}
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export function projectNestedRegistryForRoot(rootRunId: string): NestedRegistry | undefined {
|
|
503
|
+
const route = findNestedRouteForRootId(rootRunId);
|
|
504
|
+
return route ? projectNestedEvents(route) : undefined;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export function findNestedRun(children: NestedRunSummary[] | undefined, id: string): NestedRunSummary | undefined {
|
|
508
|
+
if (!children?.length) return undefined;
|
|
509
|
+
for (const child of children) {
|
|
510
|
+
if (child.id === id) return child;
|
|
511
|
+
const nested = findNestedRun(child.children, id) ?? findNestedRun(child.steps?.flatMap((step) => step.children ?? []), id);
|
|
512
|
+
if (nested) return nested;
|
|
513
|
+
}
|
|
514
|
+
return undefined;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export interface NestedRunMatch {
|
|
518
|
+
rootRunId: string;
|
|
519
|
+
route: NestedRoute;
|
|
520
|
+
run: NestedRunSummary;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export interface NestedRunResolutionScope {
|
|
524
|
+
routes: NestedRoute[];
|
|
525
|
+
descendantOf?: { parentRunId: string; parentStepIndex?: number };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function collectNestedRuns(children: NestedRunSummary[] | undefined, output: NestedRunSummary[] = []): NestedRunSummary[] {
|
|
529
|
+
for (const child of children ?? []) {
|
|
530
|
+
output.push(child);
|
|
531
|
+
collectNestedRuns(child.children, output);
|
|
532
|
+
collectNestedRuns(child.steps?.flatMap((step) => step.children ?? []), output);
|
|
533
|
+
}
|
|
534
|
+
return output;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function collectScopedNestedRuns(children: NestedRunSummary[] | undefined, scope: NestedRunResolutionScope["descendantOf"], output: NestedRunSummary[] = []): NestedRunSummary[] {
|
|
538
|
+
if (!scope) return collectNestedRuns(children, output);
|
|
539
|
+
for (const child of children ?? []) {
|
|
540
|
+
if (child.parentRunId === scope.parentRunId && (scope.parentStepIndex === undefined || child.parentStepIndex === scope.parentStepIndex)) {
|
|
541
|
+
collectNestedRuns([child], output);
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
collectScopedNestedRuns(child.children, scope, output);
|
|
545
|
+
collectScopedNestedRuns(child.steps?.flatMap((step) => step.children ?? []), scope, output);
|
|
546
|
+
}
|
|
547
|
+
return output;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function listNestedRoutes(): NestedRoute[] {
|
|
551
|
+
let entries: string[];
|
|
552
|
+
try {
|
|
553
|
+
entries = fs.readdirSync(NESTED_EVENTS_DIR);
|
|
554
|
+
} catch (error) {
|
|
555
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
556
|
+
throw error;
|
|
557
|
+
}
|
|
558
|
+
const routes: NestedRoute[] = [];
|
|
559
|
+
for (const entry of entries) {
|
|
560
|
+
const routeRoot = path.join(NESTED_EVENTS_DIR, entry);
|
|
561
|
+
try {
|
|
562
|
+
const metadata = JSON.parse(fs.readFileSync(path.join(routeRoot, ROUTE_FILE), "utf-8")) as { rootRunId?: unknown; capabilityToken?: unknown };
|
|
563
|
+
if (typeof metadata.rootRunId !== "string" || typeof metadata.capabilityToken !== "string") continue;
|
|
564
|
+
const route = {
|
|
565
|
+
rootRunId: metadata.rootRunId,
|
|
566
|
+
eventSink: path.join(routeRoot, "events"),
|
|
567
|
+
controlInbox: path.join(routeRoot, "controls"),
|
|
568
|
+
capabilityToken: metadata.capabilityToken,
|
|
569
|
+
};
|
|
570
|
+
validateRouteShape(route);
|
|
571
|
+
routes.push(route);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
574
|
+
console.error(`Ignoring unreadable nested route metadata under '${routeRoot}':`, error);
|
|
575
|
+
}
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return routes;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export function findNestedRunMatchesById(id: string, options: { prefix?: boolean; scope?: NestedRunResolutionScope } = {}): NestedRunMatch[] {
|
|
583
|
+
assertSafeId("id", id);
|
|
584
|
+
const matches: NestedRunMatch[] = [];
|
|
585
|
+
for (const route of options.scope?.routes ?? listNestedRoutes()) {
|
|
586
|
+
try {
|
|
587
|
+
const registry = projectNestedEvents(route);
|
|
588
|
+
for (const run of collectScopedNestedRuns(registry.children, options.scope?.descendantOf)) {
|
|
589
|
+
if (options.prefix ? run.id.startsWith(id) : run.id === id) matches.push({ rootRunId: route.rootRunId, route, run });
|
|
590
|
+
}
|
|
591
|
+
} catch {
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return matches;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export function findNestedRunById(id: string): { rootRunId: string; run: NestedRunSummary } | undefined {
|
|
599
|
+
const match = findNestedRunMatchesById(id)[0];
|
|
600
|
+
return match ? { rootRunId: match.rootRunId, run: match.run } : undefined;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function readNestedRegistry(route: NestedRoute): NestedRegistry {
|
|
604
|
+
validateRouteShape(route);
|
|
605
|
+
try {
|
|
606
|
+
const parsed = JSON.parse(fs.readFileSync(registryPath(route), "utf-8")) as NestedRegistry;
|
|
607
|
+
return {
|
|
608
|
+
rootRunId: route.rootRunId,
|
|
609
|
+
updatedAt: typeof parsed.updatedAt === "number" ? parsed.updatedAt : 0,
|
|
610
|
+
children: Array.isArray(parsed.children) ? parsed.children.map((child) => sanitizeSummary(child)).filter((child): child is NestedRunSummary => Boolean(child)) : [],
|
|
611
|
+
processedEvents: Array.isArray(parsed.processedEvents) ? parsed.processedEvents.filter((item): item is string => typeof item === "string").slice(-MAX_PROCESSED_NESTED_EVENTS) : [],
|
|
612
|
+
};
|
|
613
|
+
} catch (error) {
|
|
614
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
615
|
+
return { rootRunId: route.rootRunId, updatedAt: 0, children: [], processedEvents: [] };
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
export function projectNestedEvents(route: NestedRoute): NestedRegistry {
|
|
620
|
+
validateRouteShape(route);
|
|
621
|
+
const release = acquireRegistryLock(route);
|
|
622
|
+
try {
|
|
623
|
+
let registry = readNestedRegistry(route);
|
|
624
|
+
const seen = new Set(registry.processedEvents);
|
|
625
|
+
let changed = false;
|
|
626
|
+
let entries: string[] = [];
|
|
627
|
+
try {
|
|
628
|
+
entries = fs.readdirSync(route.eventSink).filter((entry) => entry.endsWith(".json") || entry.endsWith(".jsonl")).sort();
|
|
629
|
+
} catch (error) {
|
|
630
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
631
|
+
}
|
|
632
|
+
for (const entry of entries) {
|
|
633
|
+
if (seen.has(entry)) continue;
|
|
634
|
+
const eventPath = path.join(route.eventSink, entry);
|
|
635
|
+
if (!containedPath(route.eventSink, eventPath)) continue;
|
|
636
|
+
let content: string;
|
|
637
|
+
try {
|
|
638
|
+
const stat = fs.statSync(eventPath);
|
|
639
|
+
if (!stat.isFile() || stat.size > MAX_NESTED_EVENT_BYTES) continue;
|
|
640
|
+
content = fs.readFileSync(eventPath, "utf-8");
|
|
641
|
+
} catch {
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
for (const event of parseNestedEventRecords(content, route)) {
|
|
645
|
+
registry = applyNestedEvent(registry, event);
|
|
646
|
+
changed = true;
|
|
647
|
+
}
|
|
648
|
+
seen.add(entry);
|
|
649
|
+
changed = true;
|
|
650
|
+
}
|
|
651
|
+
if (changed) {
|
|
652
|
+
// Event files are immutable; retain enough filenames for worst-case bounded fanout without unbounded registry growth.
|
|
653
|
+
registry = { ...registry, processedEvents: [...seen].slice(-MAX_PROCESSED_NESTED_EVENTS) };
|
|
654
|
+
// Registry projection is lock-serialized across parent and fanout-child processes.
|
|
655
|
+
// Child and runner processes only create immutable event files, so parent status.json
|
|
656
|
+
// remains owned by the existing runner writer and is never rewritten here.
|
|
657
|
+
writeAtomicJson(registryPath(route), registry);
|
|
658
|
+
}
|
|
659
|
+
return registry;
|
|
660
|
+
} finally {
|
|
661
|
+
release();
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function writeRouteRecord(dir: string, ts: number, payload: object): string {
|
|
666
|
+
const content = `${JSON.stringify(payload)}\n`;
|
|
667
|
+
if (Buffer.byteLength(content, "utf-8") > MAX_NESTED_EVENT_BYTES) throw new Error("Nested route record exceeds the maximum size.");
|
|
668
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
669
|
+
const name = `${String(ts).padStart(13, "0")}-${randomUUID()}.json`;
|
|
670
|
+
const tmp = path.join(dir, `.${name}.tmp`);
|
|
671
|
+
const finalPath = path.join(dir, name);
|
|
672
|
+
fs.writeFileSync(tmp, content, { mode: 0o600 });
|
|
673
|
+
fs.renameSync(tmp, finalPath);
|
|
674
|
+
return finalPath;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
export function writeNestedEvent(route: NestedRoute, event: Omit<NestedEventRecord, "rootRunId" | "capabilityToken">): void {
|
|
678
|
+
// Child and runner processes append immutable route events; parent projection owns registry/status aggregation.
|
|
679
|
+
validateRouteShape(route);
|
|
680
|
+
const record: NestedEventRecord = {
|
|
681
|
+
...event,
|
|
682
|
+
rootRunId: route.rootRunId,
|
|
683
|
+
capabilityToken: route.capabilityToken,
|
|
684
|
+
};
|
|
685
|
+
const sanitized = parseRecord(JSON.stringify(record), route);
|
|
686
|
+
if (!sanitized) throw new Error("Nested event record failed validation.");
|
|
687
|
+
writeRouteRecord(route.eventSink, sanitized.ts, sanitized);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
export function parseNestedControlRequest(content: string, route: NestedRoute): NestedControlRequestRecord | undefined {
|
|
691
|
+
if (Buffer.byteLength(content, "utf-8") > MAX_NESTED_EVENT_BYTES) return undefined;
|
|
692
|
+
let parsed: unknown;
|
|
693
|
+
try {
|
|
694
|
+
parsed = JSON.parse(content);
|
|
695
|
+
} catch {
|
|
696
|
+
return undefined;
|
|
697
|
+
}
|
|
698
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
699
|
+
const raw = parsed as Record<string, unknown>;
|
|
700
|
+
if (raw.type !== "subagent.nested.control-request") return undefined;
|
|
701
|
+
if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
|
|
702
|
+
if (!isSafeNestedId(raw.requestId) || !isSafeNestedId(raw.targetRunId)) return undefined;
|
|
703
|
+
if (raw.action !== "interrupt" && raw.action !== "resume") return undefined;
|
|
704
|
+
const ts = clampNumber(raw.ts);
|
|
705
|
+
if (ts === undefined) return undefined;
|
|
706
|
+
return {
|
|
707
|
+
type: "subagent.nested.control-request",
|
|
708
|
+
ts,
|
|
709
|
+
rootRunId: route.rootRunId,
|
|
710
|
+
capabilityToken: route.capabilityToken,
|
|
711
|
+
requestId: raw.requestId,
|
|
712
|
+
targetRunId: raw.targetRunId,
|
|
713
|
+
action: raw.action,
|
|
714
|
+
...(stringValue(raw.message, 16_000) ? { message: stringValue(raw.message, 16_000) } : {}),
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export function parseNestedControlResult(content: string, route: NestedRoute): NestedControlResultRecord | undefined {
|
|
719
|
+
if (Buffer.byteLength(content, "utf-8") > MAX_NESTED_EVENT_BYTES) return undefined;
|
|
720
|
+
let parsed: unknown;
|
|
721
|
+
try {
|
|
722
|
+
parsed = JSON.parse(content);
|
|
723
|
+
} catch {
|
|
724
|
+
return undefined;
|
|
725
|
+
}
|
|
726
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
727
|
+
const raw = parsed as Record<string, unknown>;
|
|
728
|
+
if (raw.type !== "subagent.nested.control-result") return undefined;
|
|
729
|
+
if (raw.rootRunId !== route.rootRunId || raw.capabilityToken !== route.capabilityToken) return undefined;
|
|
730
|
+
if (!isSafeNestedId(raw.requestId) || !isSafeNestedId(raw.targetRunId)) return undefined;
|
|
731
|
+
const ts = clampNumber(raw.ts);
|
|
732
|
+
if (ts === undefined || typeof raw.ok !== "boolean") return undefined;
|
|
733
|
+
return {
|
|
734
|
+
type: "subagent.nested.control-result",
|
|
735
|
+
ts,
|
|
736
|
+
rootRunId: route.rootRunId,
|
|
737
|
+
capabilityToken: route.capabilityToken,
|
|
738
|
+
requestId: raw.requestId,
|
|
739
|
+
targetRunId: raw.targetRunId,
|
|
740
|
+
ok: raw.ok,
|
|
741
|
+
message: stringValue(raw.message, 16_000) ?? (raw.ok ? "Control request completed." : "Control request failed."),
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
export function writeNestedControlRequest(route: NestedRoute, request: Omit<NestedControlRequestRecord, "type" | "rootRunId" | "capabilityToken">): string {
|
|
746
|
+
validateRouteShape(route);
|
|
747
|
+
assertSafeId("requestId", request.requestId);
|
|
748
|
+
assertSafeId("targetRunId", request.targetRunId);
|
|
749
|
+
const record: NestedControlRequestRecord = {
|
|
750
|
+
type: "subagent.nested.control-request",
|
|
751
|
+
...request,
|
|
752
|
+
rootRunId: route.rootRunId,
|
|
753
|
+
capabilityToken: route.capabilityToken,
|
|
754
|
+
};
|
|
755
|
+
const sanitized = parseNestedControlRequest(JSON.stringify(record), route);
|
|
756
|
+
if (!sanitized) throw new Error("Nested control request failed validation.");
|
|
757
|
+
return writeRouteRecord(route.controlInbox, sanitized.ts, sanitized);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export function readNestedControlRequests(route: NestedRoute): Array<NestedControlRequestRecord & { filePath: string }> {
|
|
761
|
+
validateRouteShape(route);
|
|
762
|
+
let entries: string[] = [];
|
|
763
|
+
try {
|
|
764
|
+
entries = fs.readdirSync(route.controlInbox).filter((entry) => entry.endsWith(".json")).sort();
|
|
765
|
+
} catch (error) {
|
|
766
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
767
|
+
}
|
|
768
|
+
const requests: Array<NestedControlRequestRecord & { filePath: string }> = [];
|
|
769
|
+
for (const entry of entries) {
|
|
770
|
+
const filePath = path.join(route.controlInbox, entry);
|
|
771
|
+
if (!containedPath(route.controlInbox, filePath)) continue;
|
|
772
|
+
try {
|
|
773
|
+
const stat = fs.statSync(filePath);
|
|
774
|
+
if (!stat.isFile() || stat.size > MAX_NESTED_EVENT_BYTES) continue;
|
|
775
|
+
const request = parseNestedControlRequest(fs.readFileSync(filePath, "utf-8"), route);
|
|
776
|
+
if (request) requests.push({ ...request, filePath });
|
|
777
|
+
} catch {
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return requests;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
export function writeNestedControlResult(route: NestedRoute, result: Omit<NestedControlResultRecord, "type" | "rootRunId" | "capabilityToken">): void {
|
|
785
|
+
validateRouteShape(route);
|
|
786
|
+
assertSafeId("requestId", result.requestId);
|
|
787
|
+
assertSafeId("targetRunId", result.targetRunId);
|
|
788
|
+
const record: NestedControlResultRecord = {
|
|
789
|
+
type: "subagent.nested.control-result",
|
|
790
|
+
...result,
|
|
791
|
+
rootRunId: route.rootRunId,
|
|
792
|
+
capabilityToken: route.capabilityToken,
|
|
793
|
+
};
|
|
794
|
+
const sanitized = parseNestedControlResult(JSON.stringify(record), route);
|
|
795
|
+
if (!sanitized) throw new Error("Nested control result failed validation.");
|
|
796
|
+
writeRouteRecord(route.eventSink, sanitized.ts, sanitized);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
export function readNestedControlResults(route: NestedRoute): NestedControlResultRecord[] {
|
|
800
|
+
validateRouteShape(route);
|
|
801
|
+
let entries: string[] = [];
|
|
802
|
+
try {
|
|
803
|
+
entries = fs.readdirSync(route.eventSink).filter((entry) => entry.endsWith(".json") || entry.endsWith(".jsonl")).sort();
|
|
804
|
+
} catch (error) {
|
|
805
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
806
|
+
}
|
|
807
|
+
const results: NestedControlResultRecord[] = [];
|
|
808
|
+
for (const entry of entries) {
|
|
809
|
+
const eventPath = path.join(route.eventSink, entry);
|
|
810
|
+
if (!containedPath(route.eventSink, eventPath)) continue;
|
|
811
|
+
try {
|
|
812
|
+
const stat = fs.statSync(eventPath);
|
|
813
|
+
if (!stat.isFile() || stat.size > MAX_NESTED_EVENT_BYTES) continue;
|
|
814
|
+
const content = fs.readFileSync(eventPath, "utf-8");
|
|
815
|
+
const lines = content.includes("\n") ? content.split("\n").filter((line) => line.trim()) : [content];
|
|
816
|
+
for (const line of lines) {
|
|
817
|
+
const result = parseNestedControlResult(line, route);
|
|
818
|
+
if (result) results.push(result);
|
|
819
|
+
}
|
|
820
|
+
} catch {
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return results;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
export function nestedRouteEnv(route: NestedRoute): Record<string, string> {
|
|
828
|
+
return {
|
|
829
|
+
[SUBAGENT_PARENT_EVENT_SINK_ENV]: route.eventSink,
|
|
830
|
+
[SUBAGENT_PARENT_CONTROL_INBOX_ENV]: route.controlInbox,
|
|
831
|
+
[SUBAGENT_PARENT_ROOT_RUN_ID_ENV]: route.rootRunId,
|
|
832
|
+
[SUBAGENT_PARENT_CAPABILITY_TOKEN_ENV]: route.capabilityToken,
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
export function attachRootChildrenToSteps<T extends { children?: NestedRunSummary[]; index?: number }>(rootRunId: string, steps: T[] | undefined, children: NestedRunSummary[] | undefined): void {
|
|
837
|
+
if (!steps?.length) return;
|
|
838
|
+
for (const step of steps) {
|
|
839
|
+
step.children = undefined;
|
|
840
|
+
}
|
|
841
|
+
if (!children?.length) return;
|
|
842
|
+
for (const child of children) {
|
|
843
|
+
if (child.parentRunId !== rootRunId || child.parentStepIndex === undefined) continue;
|
|
844
|
+
const step = steps.find((candidate, index) => (candidate.index ?? index) === child.parentStepIndex);
|
|
845
|
+
if (!step) continue;
|
|
846
|
+
step.children ??= [];
|
|
847
|
+
step.children = [...step.children.filter((existing) => existing.id !== child.id), child].slice(0, MAX_NESTED_CHILDREN);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
export function updateAsyncJobNestedProjection(job: AsyncJobState): void {
|
|
852
|
+
if (!job.nestedRoute) return;
|
|
853
|
+
const registry = projectNestedEvents(job.nestedRoute);
|
|
854
|
+
job.nestedChildren = registry.children;
|
|
855
|
+
attachRootChildrenToSteps(job.asyncId, job.steps, registry.children);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
export function updateForegroundNestedProjection(control: SubagentState["foregroundControls"] extends Map<string, infer T> ? T : never): void {
|
|
859
|
+
if (!control.nestedRoute) return;
|
|
860
|
+
const registry = projectNestedEvents(control.nestedRoute);
|
|
861
|
+
control.nestedChildren = registry.children;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export function hasLiveNestedDescendants(children: NestedRunSummary[] | undefined): boolean {
|
|
865
|
+
if (!children?.length) return false;
|
|
866
|
+
for (const child of children) {
|
|
867
|
+
if (!terminal(child.state)) return true;
|
|
868
|
+
if (hasLiveNestedDescendants(child.children)) return true;
|
|
869
|
+
if (hasLiveNestedDescendants(child.steps?.flatMap((step) => step.children ?? []))) return true;
|
|
870
|
+
}
|
|
871
|
+
return false;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
export function nestedSummaryFromAsyncStatus(status: AsyncStatus, asyncDir: string, fallback: { id: string; parentRunId: string; parentStepIndex?: number; depth: number; path?: Array<{ runId: string; stepIndex?: number; agent?: string }>; mode?: SubagentRunMode; ts: number }): NestedRunSummary {
|
|
875
|
+
return {
|
|
876
|
+
id: status.runId || fallback.id,
|
|
877
|
+
parentRunId: fallback.parentRunId,
|
|
878
|
+
...(fallback.parentStepIndex !== undefined ? { parentStepIndex: fallback.parentStepIndex } : {}),
|
|
879
|
+
depth: fallback.depth,
|
|
880
|
+
path: fallback.path ?? [{ runId: fallback.parentRunId, ...(fallback.parentStepIndex !== undefined ? { stepIndex: fallback.parentStepIndex } : {}) }],
|
|
881
|
+
asyncDir,
|
|
882
|
+
...(status.pid ? { pid: status.pid } : {}),
|
|
883
|
+
...(status.sessionId ? { sessionId: status.sessionId } : {}),
|
|
884
|
+
mode: status.mode ?? fallback.mode,
|
|
885
|
+
state: status.state,
|
|
886
|
+
...(status.currentStep !== undefined ? { currentStep: status.currentStep } : {}),
|
|
887
|
+
...(status.chainStepCount !== undefined ? { chainStepCount: status.chainStepCount } : {}),
|
|
888
|
+
...(status.activityState ? { activityState: status.activityState } : {}),
|
|
889
|
+
...(status.lastActivityAt !== undefined ? { lastActivityAt: status.lastActivityAt } : {}),
|
|
890
|
+
...(status.currentTool ? { currentTool: status.currentTool } : {}),
|
|
891
|
+
...(status.currentToolStartedAt !== undefined ? { currentToolStartedAt: status.currentToolStartedAt } : {}),
|
|
892
|
+
...(status.currentPath ? { currentPath: status.currentPath } : {}),
|
|
893
|
+
...(status.turnCount !== undefined ? { turnCount: status.turnCount } : {}),
|
|
894
|
+
...(status.toolCount !== undefined ? { toolCount: status.toolCount } : {}),
|
|
895
|
+
...(status.totalTokens ? { totalTokens: status.totalTokens } : {}),
|
|
896
|
+
...(status.startedAt !== undefined ? { startedAt: status.startedAt } : { startedAt: fallback.ts }),
|
|
897
|
+
...(status.endedAt !== undefined ? { endedAt: status.endedAt } : {}),
|
|
898
|
+
lastUpdate: status.lastUpdate ?? fallback.ts,
|
|
899
|
+
...(status.sessionFile ? { sessionFile: status.sessionFile } : {}),
|
|
900
|
+
...(status.steps?.length ? { steps: status.steps.map((step) => ({
|
|
901
|
+
agent: step.agent,
|
|
902
|
+
status: step.status,
|
|
903
|
+
...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),
|
|
904
|
+
...(step.activityState ? { activityState: step.activityState } : {}),
|
|
905
|
+
...(step.lastActivityAt !== undefined ? { lastActivityAt: step.lastActivityAt } : {}),
|
|
906
|
+
...(step.currentTool ? { currentTool: step.currentTool } : {}),
|
|
907
|
+
...(step.currentToolStartedAt !== undefined ? { currentToolStartedAt: step.currentToolStartedAt } : {}),
|
|
908
|
+
...(step.currentPath ? { currentPath: step.currentPath } : {}),
|
|
909
|
+
...(step.turnCount !== undefined ? { turnCount: step.turnCount } : {}),
|
|
910
|
+
...(step.toolCount !== undefined ? { toolCount: step.toolCount } : {}),
|
|
911
|
+
...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),
|
|
912
|
+
...(step.endedAt !== undefined ? { endedAt: step.endedAt } : {}),
|
|
913
|
+
...(step.error ? { error: step.error } : {}),
|
|
914
|
+
})).slice(0, MAX_NESTED_STEPS) } : {}),
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
export function nestedArtifactEnv(rootRunId: string, parentRunId: string): Record<string, string> {
|
|
919
|
+
const envPrefix = APP_NAME.toUpperCase();
|
|
920
|
+
return {
|
|
921
|
+
[`${envPrefix}_SUBAGENT_NESTED_ROOT_RUN_ID`]: rootRunId,
|
|
922
|
+
[`${envPrefix}_SUBAGENT_NESTED_PARENT_RUN_ID`]: parentRunId,
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
export function isTopLevelAsyncDir(asyncDir: string): boolean {
|
|
927
|
+
const resolved = path.resolve(asyncDir);
|
|
928
|
+
return containedPath(ASYNC_DIR, resolved) && !containedPath(NESTED_RUNS_DIR, resolved);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
export function nestedResultsPath(rootRunId: string, id: string): string {
|
|
932
|
+
assertSafeId("rootRunId", rootRunId);
|
|
933
|
+
assertSafeId("id", id);
|
|
934
|
+
return path.join(RESULTS_DIR, "nested", rootRunId, `${id}.json`);
|
|
935
|
+
}
|