@belboy47/brc-agent 0.0.0-stage → 2.0.17-lab.1
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 +30 -0
- package/LICENSE-desktop-commander +21 -0
- package/NOTICE +13 -0
- package/README.md +58 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +354 -0
- package/dist/cli.js.map +1 -0
- package/dist/context.d.ts +31 -0
- package/dist/context.js +11 -0
- package/dist/context.js.map +1 -0
- package/dist/core/command-manager.d.ts +8 -0
- package/dist/core/command-manager.js +242 -0
- package/dist/core/command-manager.js.map +1 -0
- package/dist/core/config-field-definitions.d.ts +41 -0
- package/dist/core/config-field-definitions.js +38 -0
- package/dist/core/config-field-definitions.js.map +1 -0
- package/dist/core/config-manager.d.ts +102 -0
- package/dist/core/config-manager.js +451 -0
- package/dist/core/config-manager.js.map +1 -0
- package/dist/core/config.d.ts +6 -0
- package/dist/core/config.js +20 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/custom-stdio.d.ts +9 -0
- package/dist/core/custom-stdio.js +2 -0
- package/dist/core/custom-stdio.js.map +1 -0
- package/dist/core/error-handlers.d.ts +7 -0
- package/dist/core/error-handlers.js +16 -0
- package/dist/core/error-handlers.js.map +1 -0
- package/dist/core/file-transfer.d.ts +16 -0
- package/dist/core/file-transfer.js +316 -0
- package/dist/core/file-transfer.js.map +1 -0
- package/dist/core/handlers/edit-search-handlers.d.ts +6 -0
- package/dist/core/handlers/edit-search-handlers.js +7 -0
- package/dist/core/handlers/edit-search-handlers.js.map +1 -0
- package/dist/core/handlers/filesystem-handlers.d.ts +38 -0
- package/dist/core/handlers/filesystem-handlers.js +535 -0
- package/dist/core/handlers/filesystem-handlers.js.map +1 -0
- package/dist/core/handlers/history-handlers.d.ts +12 -0
- package/dist/core/handlers/history-handlers.js +68 -0
- package/dist/core/handlers/history-handlers.js.map +1 -0
- package/dist/core/handlers/index.d.ts +7 -0
- package/dist/core/handlers/index.js +9 -0
- package/dist/core/handlers/index.js.map +1 -0
- package/dist/core/handlers/process-handlers.d.ts +9 -0
- package/dist/core/handlers/process-handlers.js +16 -0
- package/dist/core/handlers/process-handlers.js.map +1 -0
- package/dist/core/handlers/search-handlers.d.ts +17 -0
- package/dist/core/handlers/search-handlers.js +336 -0
- package/dist/core/handlers/search-handlers.js.map +1 -0
- package/dist/core/handlers/terminal-handlers.d.ts +21 -0
- package/dist/core/handlers/terminal-handlers.js +36 -0
- package/dist/core/handlers/terminal-handlers.js.map +1 -0
- package/dist/core/handlers/transfer-handlers.d.ts +4 -0
- package/dist/core/handlers/transfer-handlers.js +76 -0
- package/dist/core/handlers/transfer-handlers.js.map +1 -0
- package/dist/core/search-manager.d.ts +162 -0
- package/dist/core/search-manager.js +929 -0
- package/dist/core/search-manager.js.map +1 -0
- package/dist/core/terminal-manager.d.ts +126 -0
- package/dist/core/terminal-manager.js +671 -0
- package/dist/core/terminal-manager.js.map +1 -0
- package/dist/core/tool-definitions.d.ts +9 -0
- package/dist/core/tool-definitions.js +1020 -0
- package/dist/core/tool-definitions.js.map +1 -0
- package/dist/core/tools/config.d.ts +97 -0
- package/dist/core/tools/config.js +265 -0
- package/dist/core/tools/config.js.map +1 -0
- package/dist/core/tools/edit.d.ts +49 -0
- package/dist/core/tools/edit.js +540 -0
- package/dist/core/tools/edit.js.map +1 -0
- package/dist/core/tools/feedback.d.ts +8 -0
- package/dist/core/tools/feedback.js +133 -0
- package/dist/core/tools/feedback.js.map +1 -0
- package/dist/core/tools/filesystem.d.ts +84 -0
- package/dist/core/tools/filesystem.js +991 -0
- package/dist/core/tools/filesystem.js.map +1 -0
- package/dist/core/tools/fuzzySearch.d.ts +14 -0
- package/dist/core/tools/fuzzySearch.js +141 -0
- package/dist/core/tools/fuzzySearch.js.map +1 -0
- package/dist/core/tools/fuzzySearchCore.d.ts +52 -0
- package/dist/core/tools/fuzzySearchCore.js +126 -0
- package/dist/core/tools/fuzzySearchCore.js.map +1 -0
- package/dist/core/tools/improved-process-tools.d.ts +24 -0
- package/dist/core/tools/improved-process-tools.js +645 -0
- package/dist/core/tools/improved-process-tools.js.map +1 -0
- package/dist/core/tools/mime-types.d.ts +3 -0
- package/dist/core/tools/mime-types.js +29 -0
- package/dist/core/tools/mime-types.js.map +1 -0
- package/dist/core/tools/pdf/extract-images.d.ts +34 -0
- package/dist/core/tools/pdf/extract-images.js +133 -0
- package/dist/core/tools/pdf/extract-images.js.map +1 -0
- package/dist/core/tools/pdf/index.d.ts +6 -0
- package/dist/core/tools/pdf/index.js +4 -0
- package/dist/core/tools/pdf/index.js.map +1 -0
- package/dist/core/tools/pdf/lib/pdf2md.d.ts +36 -0
- package/dist/core/tools/pdf/lib/pdf2md.js +77 -0
- package/dist/core/tools/pdf/lib/pdf2md.js.map +1 -0
- package/dist/core/tools/pdf/manipulations.d.ts +13 -0
- package/dist/core/tools/pdf/manipulations.js +100 -0
- package/dist/core/tools/pdf/manipulations.js.map +1 -0
- package/dist/core/tools/pdf/markdown.d.ts +26 -0
- package/dist/core/tools/pdf/markdown.js +276 -0
- package/dist/core/tools/pdf/markdown.js.map +1 -0
- package/dist/core/tools/pdf/utils.d.ts +12 -0
- package/dist/core/tools/pdf/utils.js +35 -0
- package/dist/core/tools/pdf/utils.js.map +1 -0
- package/dist/core/tools/process.d.ts +17 -0
- package/dist/core/tools/process.js +88 -0
- package/dist/core/tools/process.js.map +1 -0
- package/dist/core/tools/prompts.d.ts +28 -0
- package/dist/core/tools/prompts.js +260 -0
- package/dist/core/tools/prompts.js.map +1 -0
- package/dist/core/tools/schemas.d.ts +594 -0
- package/dist/core/tools/schemas.js +282 -0
- package/dist/core/tools/schemas.js.map +1 -0
- package/dist/core/tools/usage.d.ts +5 -0
- package/dist/core/tools/usage.js +25 -0
- package/dist/core/tools/usage.js.map +1 -0
- package/dist/core/types.d.ts +83 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/ui/file-preview/shared/preview-file-types.d.ts +5 -0
- package/dist/core/ui/file-preview/shared/preview-file-types.js +60 -0
- package/dist/core/ui/file-preview/shared/preview-file-types.js.map +1 -0
- package/dist/core/utils/ab-test.d.ts +12 -0
- package/dist/core/utils/ab-test.js +105 -0
- package/dist/core/utils/ab-test.js.map +1 -0
- package/dist/core/utils/capture.d.ts +12 -0
- package/dist/core/utils/capture.js +38 -0
- package/dist/core/utils/capture.js.map +1 -0
- package/dist/core/utils/dockerPrompt.d.ts +17 -0
- package/dist/core/utils/dockerPrompt.js +54 -0
- package/dist/core/utils/dockerPrompt.js.map +1 -0
- package/dist/core/utils/feature-flags.d.ts +59 -0
- package/dist/core/utils/feature-flags.js +218 -0
- package/dist/core/utils/feature-flags.js.map +1 -0
- package/dist/core/utils/files/base.d.ts +173 -0
- package/dist/core/utils/files/base.js +6 -0
- package/dist/core/utils/files/base.js.map +1 -0
- package/dist/core/utils/files/binary.d.ts +21 -0
- package/dist/core/utils/files/binary.js +66 -0
- package/dist/core/utils/files/binary.js.map +1 -0
- package/dist/core/utils/files/docx.d.ts +47 -0
- package/dist/core/utils/files/docx.js +678 -0
- package/dist/core/utils/files/docx.js.map +1 -0
- package/dist/core/utils/files/excel.d.ts +24 -0
- package/dist/core/utils/files/excel.js +438 -0
- package/dist/core/utils/files/excel.js.map +1 -0
- package/dist/core/utils/files/factory.d.ts +29 -0
- package/dist/core/utils/files/factory.js +103 -0
- package/dist/core/utils/files/factory.js.map +1 -0
- package/dist/core/utils/files/image.d.ts +21 -0
- package/dist/core/utils/files/image.js +79 -0
- package/dist/core/utils/files/image.js.map +1 -0
- package/dist/core/utils/files/index.d.ts +9 -0
- package/dist/core/utils/files/index.js +15 -0
- package/dist/core/utils/files/index.js.map +1 -0
- package/dist/core/utils/files/pdf.d.ts +32 -0
- package/dist/core/utils/files/pdf.js +141 -0
- package/dist/core/utils/files/pdf.js.map +1 -0
- package/dist/core/utils/files/text.d.ts +70 -0
- package/dist/core/utils/files/text.js +552 -0
- package/dist/core/utils/files/text.js.map +1 -0
- package/dist/core/utils/fuzzySearchLogger.d.ts +26 -0
- package/dist/core/utils/fuzzySearchLogger.js +9 -0
- package/dist/core/utils/fuzzySearchLogger.js.map +1 -0
- package/dist/core/utils/lineEndingHandler.d.ts +21 -0
- package/dist/core/utils/lineEndingHandler.js +78 -0
- package/dist/core/utils/lineEndingHandler.js.map +1 -0
- package/dist/core/utils/logger.d.ts +17 -0
- package/dist/core/utils/logger.js +5 -0
- package/dist/core/utils/logger.js.map +1 -0
- package/dist/core/utils/mcp-ui-ab-test.d.ts +13 -0
- package/dist/core/utils/mcp-ui-ab-test.js +63 -0
- package/dist/core/utils/mcp-ui-ab-test.js.map +1 -0
- package/dist/core/utils/open-browser.d.ts +9 -0
- package/dist/core/utils/open-browser.js +47 -0
- package/dist/core/utils/open-browser.js.map +1 -0
- package/dist/core/utils/process-detection.d.ts +23 -0
- package/dist/core/utils/process-detection.js +151 -0
- package/dist/core/utils/process-detection.js.map +1 -0
- package/dist/core/utils/ripgrep-resolver.d.ts +9 -0
- package/dist/core/utils/ripgrep-resolver.js +96 -0
- package/dist/core/utils/ripgrep-resolver.js.map +1 -0
- package/dist/core/utils/system-info.d.ts +72 -0
- package/dist/core/utils/system-info.js +723 -0
- package/dist/core/utils/system-info.js.map +1 -0
- package/dist/core/utils/toolHistory.d.ts +31 -0
- package/dist/core/utils/toolHistory.js +17 -0
- package/dist/core/utils/toolHistory.js.map +1 -0
- package/dist/core/utils/trackTools.d.ts +1 -0
- package/dist/core/utils/trackTools.js +3 -0
- package/dist/core/utils/trackTools.js.map +1 -0
- package/dist/core/utils/unsupportedParams.d.ts +24 -0
- package/dist/core/utils/unsupportedParams.js +67 -0
- package/dist/core/utils/unsupportedParams.js.map +1 -0
- package/dist/core/utils/usageTracker.d.ts +125 -0
- package/dist/core/utils/usageTracker.js +475 -0
- package/dist/core/utils/usageTracker.js.map +1 -0
- package/dist/core/utils/welcome-onboarding.d.ts +11 -0
- package/dist/core/utils/welcome-onboarding.js +100 -0
- package/dist/core/utils/welcome-onboarding.js.map +1 -0
- package/dist/core/utils/withTimeout.d.ts +28 -0
- package/dist/core/utils/withTimeout.js +86 -0
- package/dist/core/utils/withTimeout.js.map +1 -0
- package/dist/core/version.d.ts +1 -0
- package/dist/core/version.js +2 -0
- package/dist/core/version.js.map +1 -0
- package/dist/dispatcher.d.ts +7 -0
- package/dist/dispatcher.js +179 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/cli-commands.d.ts +5 -0
- package/dist/plugins/cli-commands.js +240 -0
- package/dist/plugins/cli-commands.js.map +1 -0
- package/dist/plugins/defaults.d.ts +62 -0
- package/dist/plugins/defaults.js +260 -0
- package/dist/plugins/defaults.js.map +1 -0
- package/dist/plugins/first-run.d.ts +16 -0
- package/dist/plugins/first-run.js +89 -0
- package/dist/plugins/first-run.js.map +1 -0
- package/dist/plugins/host.d.ts +20 -0
- package/dist/plugins/host.js +175 -0
- package/dist/plugins/host.js.map +1 -0
- package/dist/plugins/installer.d.ts +21 -0
- package/dist/plugins/installer.js +350 -0
- package/dist/plugins/installer.js.map +1 -0
- package/dist/plugins/locate.d.ts +15 -0
- package/dist/plugins/locate.js +99 -0
- package/dist/plugins/locate.js.map +1 -0
- package/dist/plugins/manager.d.ts +32 -0
- package/dist/plugins/manager.js +139 -0
- package/dist/plugins/manager.js.map +1 -0
- package/dist/plugins/paths.d.ts +32 -0
- package/dist/plugins/paths.js +49 -0
- package/dist/plugins/paths.js.map +1 -0
- package/dist/plugins/python.d.ts +55 -0
- package/dist/plugins/python.js +234 -0
- package/dist/plugins/python.js.map +1 -0
- package/dist/plugins/store.d.ts +12 -0
- package/dist/plugins/store.js +76 -0
- package/dist/plugins/store.js.map +1 -0
- package/dist/plugins/types.d.ts +112 -0
- package/dist/plugins/types.js +9 -0
- package/dist/plugins/types.js.map +1 -0
- package/dist/plugins/util.d.ts +37 -0
- package/dist/plugins/util.js +283 -0
- package/dist/plugins/util.js.map +1 -0
- package/dist/runtime/activity.d.ts +49 -0
- package/dist/runtime/activity.js +158 -0
- package/dist/runtime/activity.js.map +1 -0
- package/dist/runtime/browser-workflow.d.ts +21 -0
- package/dist/runtime/browser-workflow.js +51 -0
- package/dist/runtime/browser-workflow.js.map +1 -0
- package/dist/runtime/code-graph.d.ts +156 -0
- package/dist/runtime/code-graph.js +330 -0
- package/dist/runtime/code-graph.js.map +1 -0
- package/dist/runtime/code-structure.d.ts +53 -0
- package/dist/runtime/code-structure.js +152 -0
- package/dist/runtime/code-structure.js.map +1 -0
- package/dist/runtime/coding-session.d.ts +97 -0
- package/dist/runtime/coding-session.js +142 -0
- package/dist/runtime/coding-session.js.map +1 -0
- package/dist/runtime/docker-client.d.ts +47 -0
- package/dist/runtime/docker-client.js +40 -0
- package/dist/runtime/docker-client.js.map +1 -0
- package/dist/runtime/errors.d.ts +9 -0
- package/dist/runtime/errors.js +56 -0
- package/dist/runtime/errors.js.map +1 -0
- package/dist/runtime/failure-log.d.ts +22 -0
- package/dist/runtime/failure-log.js +112 -0
- package/dist/runtime/failure-log.js.map +1 -0
- package/dist/runtime/fast-batch.d.ts +55 -0
- package/dist/runtime/fast-batch.js +104 -0
- package/dist/runtime/fast-batch.js.map +1 -0
- package/dist/runtime/file-safety.d.ts +7 -0
- package/dist/runtime/file-safety.js +134 -0
- package/dist/runtime/file-safety.js.map +1 -0
- package/dist/runtime/generic-code.d.ts +9 -0
- package/dist/runtime/generic-code.js +123 -0
- package/dist/runtime/generic-code.js.map +1 -0
- package/dist/runtime/graph-outline.d.ts +54 -0
- package/dist/runtime/graph-outline.js +118 -0
- package/dist/runtime/graph-outline.js.map +1 -0
- package/dist/runtime/harness.d.ts +61 -0
- package/dist/runtime/harness.js +102 -0
- package/dist/runtime/harness.js.map +1 -0
- package/dist/runtime/instruction-context.d.ts +23 -0
- package/dist/runtime/instruction-context.js +136 -0
- package/dist/runtime/instruction-context.js.map +1 -0
- package/dist/runtime/lab-mode.d.ts +4 -0
- package/dist/runtime/lab-mode.js +18 -0
- package/dist/runtime/lab-mode.js.map +1 -0
- package/dist/runtime/languages.d.ts +30 -0
- package/dist/runtime/languages.js +59 -0
- package/dist/runtime/languages.js.map +1 -0
- package/dist/runtime/nav-cache.d.ts +2 -0
- package/dist/runtime/nav-cache.js +12 -0
- package/dist/runtime/nav-cache.js.map +1 -0
- package/dist/runtime/operations.d.ts +40 -0
- package/dist/runtime/operations.js +86 -0
- package/dist/runtime/operations.js.map +1 -0
- package/dist/runtime/platform-capabilities.d.ts +20 -0
- package/dist/runtime/platform-capabilities.js +36 -0
- package/dist/runtime/platform-capabilities.js.map +1 -0
- package/dist/runtime/program-runner.d.ts +4 -0
- package/dist/runtime/program-runner.js +73 -0
- package/dist/runtime/program-runner.js.map +1 -0
- package/dist/runtime/project-context.d.ts +53 -0
- package/dist/runtime/project-context.js +90 -0
- package/dist/runtime/project-context.js.map +1 -0
- package/dist/runtime/project-files.d.ts +2 -0
- package/dist/runtime/project-files.js +3 -0
- package/dist/runtime/project-files.js.map +1 -0
- package/dist/runtime/project-index.d.ts +40 -0
- package/dist/runtime/project-index.js +158 -0
- package/dist/runtime/project-index.js.map +1 -0
- package/dist/runtime/resource-limits.d.ts +42 -0
- package/dist/runtime/resource-limits.js +50 -0
- package/dist/runtime/resource-limits.js.map +1 -0
- package/dist/runtime/scratch-budget.d.ts +11 -0
- package/dist/runtime/scratch-budget.js +107 -0
- package/dist/runtime/scratch-budget.js.map +1 -0
- package/dist/runtime/search-diagnostics.d.ts +3 -0
- package/dist/runtime/search-diagnostics.js +45 -0
- package/dist/runtime/search-diagnostics.js.map +1 -0
- package/dist/runtime/toolchains.d.ts +25 -0
- package/dist/runtime/toolchains.js +70 -0
- package/dist/runtime/toolchains.js.map +1 -0
- package/dist/runtime/worker-coordination.d.ts +314 -0
- package/dist/runtime/worker-coordination.js +564 -0
- package/dist/runtime/worker-coordination.js.map +1 -0
- package/dist/runtime/workspace.d.ts +151 -0
- package/dist/runtime/workspace.js +267 -0
- package/dist/runtime/workspace.js.map +1 -0
- package/dist/service.d.ts +3 -0
- package/dist/service.js +102 -0
- package/dist/service.js.map +1 -0
- package/dist/terminal-setup.d.ts +33 -0
- package/dist/terminal-setup.js +219 -0
- package/dist/terminal-setup.js.map +1 -0
- package/dist/terminal-shortcuts.d.ts +22 -0
- package/dist/terminal-shortcuts.js +145 -0
- package/dist/terminal-shortcuts.js.map +1 -0
- package/dist/terminal-ui.d.ts +20 -0
- package/dist/terminal-ui.js +53 -0
- package/dist/terminal-ui.js.map +1 -0
- package/dist/tools/ast-grep.d.ts +132 -0
- package/dist/tools/ast-grep.js +625 -0
- package/dist/tools/ast-grep.js.map +1 -0
- package/dist/tools/brc-tools.d.ts +112 -0
- package/dist/tools/brc-tools.js +599 -0
- package/dist/tools/brc-tools.js.map +1 -0
- package/dist/tools/check-tools.d.ts +4 -0
- package/dist/tools/check-tools.js +29 -0
- package/dist/tools/check-tools.js.map +1 -0
- package/dist/tools/coding-tools.d.ts +33 -0
- package/dist/tools/coding-tools.js +190 -0
- package/dist/tools/coding-tools.js.map +1 -0
- package/dist/tools/command-shell.d.ts +17 -0
- package/dist/tools/command-shell.js +84 -0
- package/dist/tools/command-shell.js.map +1 -0
- package/dist/tools/dev-extras.d.ts +42 -0
- package/dist/tools/dev-extras.js +135 -0
- package/dist/tools/dev-extras.js.map +1 -0
- package/dist/tools/edit-tools.d.ts +4 -0
- package/dist/tools/edit-tools.js +293 -0
- package/dist/tools/edit-tools.js.map +1 -0
- package/dist/tools/fast-exec.d.ts +11 -0
- package/dist/tools/fast-exec.js +202 -0
- package/dist/tools/fast-exec.js.map +1 -0
- package/dist/tools/image-tools.d.ts +37 -0
- package/dist/tools/image-tools.js +293 -0
- package/dist/tools/image-tools.js.map +1 -0
- package/dist/tools/jobs.d.ts +227 -0
- package/dist/tools/jobs.js +608 -0
- package/dist/tools/jobs.js.map +1 -0
- package/dist/tools/ops-tools.d.ts +8 -0
- package/dist/tools/ops-tools.js +58 -0
- package/dist/tools/ops-tools.js.map +1 -0
- package/dist/tools/plugin-tools.d.ts +38 -0
- package/dist/tools/plugin-tools.js +133 -0
- package/dist/tools/plugin-tools.js.map +1 -0
- package/dist/tools/rtk.d.ts +60 -0
- package/dist/tools/rtk.js +245 -0
- package/dist/tools/rtk.js.map +1 -0
- package/dist/tools/sandbox-tools.d.ts +41 -0
- package/dist/tools/sandbox-tools.js +146 -0
- package/dist/tools/sandbox-tools.js.map +1 -0
- package/dist/tools/worker-tools.d.ts +231 -0
- package/dist/tools/worker-tools.js +55 -0
- package/dist/tools/worker-tools.js.map +1 -0
- package/dist/tools/workspace-batch.d.ts +262 -0
- package/dist/tools/workspace-batch.js +188 -0
- package/dist/tools/workspace-batch.js.map +1 -0
- package/dist/transport/pairing.d.ts +22 -0
- package/dist/transport/pairing.js +154 -0
- package/dist/transport/pairing.js.map +1 -0
- package/dist/transport/plugin-admin.d.ts +33 -0
- package/dist/transport/plugin-admin.js +156 -0
- package/dist/transport/plugin-admin.js.map +1 -0
- package/dist/transport/relay-url.d.ts +3 -0
- package/dist/transport/relay-url.js +38 -0
- package/dist/transport/relay-url.js.map +1 -0
- package/dist/transport/result-budget.d.ts +11 -0
- package/dist/transport/result-budget.js +45 -0
- package/dist/transport/result-budget.js.map +1 -0
- package/dist/transport/ws-client.d.ts +64 -0
- package/dist/transport/ws-client.js +242 -0
- package/dist/transport/ws-client.js.map +1 -0
- package/package.json +70 -5
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BRC workers: background jobs and parallel fan-out for the controlling AI.
|
|
3
|
+
*
|
|
4
|
+
* The AI in the chat (Claude / ChatGPT / Gemini) stays the only "brain". It splits work and
|
|
5
|
+
* hands pieces to workers on this machine, which run them in parallel and report back:
|
|
6
|
+
*
|
|
7
|
+
* spawn_job - start a background job (a command, or a sequence of BRC tool calls); returns an id
|
|
8
|
+
* wait_jobs - block up to ~100 s until all (or any) selected jobs finish; returns their results
|
|
9
|
+
* get_job - full status / output of one job
|
|
10
|
+
* list_jobs - one line per job
|
|
11
|
+
* cancel_job - stop jobs (kills running command process trees)
|
|
12
|
+
* run_parallel - run several tool calls / commands at once and return all results in one call;
|
|
13
|
+
* anything still running at the deadline keeps going as a background job
|
|
14
|
+
*
|
|
15
|
+
* Jobs are not bound by the relay's 120 s per-call timeout (default job limit 30 min, max 6 h).
|
|
16
|
+
* Every step goes through the normal tool dispatcher, so allowedDirectories and blockedCommands apply.
|
|
17
|
+
* Finished jobs are persisted to $BRC_HOME/jobs/<id>.json and reloaded after an agent restart.
|
|
18
|
+
*/
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import fs from 'node:fs/promises';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { z } from 'zod';
|
|
23
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
24
|
+
import { runCommandCore } from './brc-tools.js';
|
|
25
|
+
import { homeDir } from '../transport/pairing.js';
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// Limits
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
const MAX_WAIT_MS = 100_000; // stay well below the relay's 120 s call timeout
|
|
30
|
+
const DEFAULT_WAIT_MS = 60_000;
|
|
31
|
+
const DEFAULT_JOB_TIMEOUT_MS = 30 * 60_000;
|
|
32
|
+
const MAX_JOB_TIMEOUT_MS = 6 * 60 * 60_000;
|
|
33
|
+
// Bounded even if the environment variable is accidentally set very high.
|
|
34
|
+
const configuredJobs = Number(process.env.BRC_MAX_JOBS);
|
|
35
|
+
const MAX_CONCURRENT_JOBS = Number.isInteger(configuredJobs) && configuredJobs > 0 ? Math.min(8, configuredJobs) : 4;
|
|
36
|
+
const MAX_STORED_STEP_OUTPUT = 32_000;
|
|
37
|
+
const KEEP_IN_MEMORY = 64;
|
|
38
|
+
const MAX_MCP_JOB_OUTPUT = 128_000;
|
|
39
|
+
const KEEP_ON_DISK = 500;
|
|
40
|
+
export const JOB_TOOL_NAMES = new Set(['spawn_job', 'wait_jobs', 'get_job', 'list_jobs', 'cancel_job', 'run_parallel']);
|
|
41
|
+
// One enormous combined job report can break an MCP client's message stream.
|
|
42
|
+
const capJobReply = (s) => s.length <= MAX_MCP_JOB_OUTPUT ? s :
|
|
43
|
+
s.slice(0, MAX_MCP_JOB_OUTPUT - 360) +
|
|
44
|
+
'\n[OUTPUT TRUNCATED: MCP job response reached 128000 characters. Request fewer jobs, smaller output_chars, or use get_job with step_offset/step_limit. Already-started jobs are not cancelled; inspect them before retrying mutations.]';
|
|
45
|
+
const text = (t, isError = false) => ({
|
|
46
|
+
content: [{ type: 'text', text: capJobReply(t) }], ...(isError ? { isError: true } : {})
|
|
47
|
+
});
|
|
48
|
+
let executor = null;
|
|
49
|
+
export function setJobToolExecutor(fn) { executor = fn; }
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Schemas
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
const CommandStepSchema = z.object({
|
|
54
|
+
command: z.string().min(1).describe('Shell command to run (non-interactive; stdin is closed).'),
|
|
55
|
+
cwd: z.string().optional().describe('Working directory (absolute, inside allowed directories).'),
|
|
56
|
+
timeout_ms: z.number().int().min(1).optional().describe('Per-command timeout. Default: whatever is left of the job timeout.'),
|
|
57
|
+
shell: z.string().optional(),
|
|
58
|
+
raw: z.boolean().optional().describe('Bypass rtk output compression (full output).'),
|
|
59
|
+
label: z.string().max(120).optional(),
|
|
60
|
+
});
|
|
61
|
+
const ToolStepSchema = z.object({
|
|
62
|
+
tool: z.string().min(1).describe('Name of any BRC tool, e.g. read_file, edit_block, start_search, git_diff, run_command.'),
|
|
63
|
+
args: z.record(z.unknown()).optional().default({}).describe('Arguments for that tool, exactly as you would pass them directly.'),
|
|
64
|
+
label: z.string().max(120).optional(),
|
|
65
|
+
});
|
|
66
|
+
const StepSchema = z.union([CommandStepSchema, ToolStepSchema]);
|
|
67
|
+
export const SpawnJobArgsSchema = z.object({
|
|
68
|
+
command: z.string().optional().describe('Shortcut for a one-command job. Use either command or steps.'),
|
|
69
|
+
cwd: z.string().optional().describe('Working directory for the command shortcut.'),
|
|
70
|
+
steps: z.array(StepSchema).min(1).max(50).optional().describe('Sequence run in order: each item is {command, cwd?, timeout_ms?} or {tool, args}.'),
|
|
71
|
+
label: z.string().max(120).optional().describe('Short human name shown in results, e.g. "unit tests".'),
|
|
72
|
+
group: z.string().max(60).optional().describe('Group name; wait_jobs/cancel_job/list_jobs can target a whole group.'),
|
|
73
|
+
continue_on_error: z.boolean().optional().default(false).describe('Keep running later steps after a failed step (default: stop).'),
|
|
74
|
+
timeout_ms: z.number().int().min(1_000).max(MAX_JOB_TIMEOUT_MS).optional().default(DEFAULT_JOB_TIMEOUT_MS)
|
|
75
|
+
.describe(`Whole-job time limit (default ${DEFAULT_JOB_TIMEOUT_MS / 60_000} min, max ${MAX_JOB_TIMEOUT_MS / 3_600_000} h).`),
|
|
76
|
+
});
|
|
77
|
+
export const WaitJobsArgsSchema = z.object({
|
|
78
|
+
ids: z.array(z.string()).optional().describe('Job ids to wait for.'),
|
|
79
|
+
group: z.string().optional().describe('Wait for every job in this group.'),
|
|
80
|
+
mode: z.enum(['all', 'any']).optional().default('all').describe('all: return when every selected job is done. any: return as soon as one more finishes.'),
|
|
81
|
+
timeout_ms: z.number().int().min(0).max(MAX_WAIT_MS).optional().default(DEFAULT_WAIT_MS),
|
|
82
|
+
output_chars: z.number().int().min(200).max(50_000).optional().default(3_000).describe('Max output characters shown per step.'),
|
|
83
|
+
});
|
|
84
|
+
export const GetJobArgsSchema = z.object({
|
|
85
|
+
id: z.string().min(1),
|
|
86
|
+
output_chars: z.number().int().min(200).max(32_000).optional().default(8_000),
|
|
87
|
+
step_offset: z.number().int().min(0).max(50).optional().default(0),
|
|
88
|
+
step_limit: z.number().int().min(1).max(20).optional().default(10),
|
|
89
|
+
});
|
|
90
|
+
export const ListJobsArgsSchema = z.object({
|
|
91
|
+
group: z.string().optional(),
|
|
92
|
+
status: z.enum(['queued', 'running', 'succeeded', 'failed', 'cancelled', 'timed_out', 'active', 'finished']).optional(),
|
|
93
|
+
limit: z.number().int().min(1).max(500).optional().default(50),
|
|
94
|
+
});
|
|
95
|
+
export const CancelJobArgsSchema = z.object({
|
|
96
|
+
ids: z.array(z.string()).optional(),
|
|
97
|
+
group: z.string().optional(),
|
|
98
|
+
all: z.boolean().optional().default(false).describe('Cancel every queued or running job.'),
|
|
99
|
+
});
|
|
100
|
+
export const RunParallelArgsSchema = z.object({
|
|
101
|
+
calls: z.array(StepSchema).min(1).max(50).describe('Items run concurrently: {tool, args} or {command, cwd?, timeout_ms?}.'),
|
|
102
|
+
max_concurrency: z.number().int().min(1).max(16).optional().default(8),
|
|
103
|
+
timeout_ms: z.number().int().min(1_000).max(MAX_WAIT_MS).optional().default(90_000)
|
|
104
|
+
.describe('How long to wait for results. Calls still running then continue as background jobs.'),
|
|
105
|
+
output_chars: z.number().int().min(200).max(50_000).optional().default(4_000).describe('Max output characters shown per call.'),
|
|
106
|
+
});
|
|
107
|
+
const jobs = new Map();
|
|
108
|
+
const queue = [];
|
|
109
|
+
let runningPooled = 0;
|
|
110
|
+
let historyLoaded = false;
|
|
111
|
+
const isFinished = (j) => j.status !== 'queued' && j.status !== 'running';
|
|
112
|
+
const jobsDir = () => path.join(homeDir(), 'jobs');
|
|
113
|
+
function newId() {
|
|
114
|
+
return `j${Date.now().toString(36)}${randomBytes(2).toString('hex')}`;
|
|
115
|
+
}
|
|
116
|
+
function stepName(s) {
|
|
117
|
+
if ('command' in s)
|
|
118
|
+
return s.label ?? `$ ${s.command.length > 80 ? s.command.slice(0, 77) + '...' : s.command}`;
|
|
119
|
+
return s.label ?? s.tool;
|
|
120
|
+
}
|
|
121
|
+
function createJob(steps, opts) {
|
|
122
|
+
const job = {
|
|
123
|
+
id: newId(),
|
|
124
|
+
label: opts.label,
|
|
125
|
+
group: opts.group,
|
|
126
|
+
status: 'queued',
|
|
127
|
+
steps,
|
|
128
|
+
results: [],
|
|
129
|
+
currentStep: -1,
|
|
130
|
+
continueOnError: !!opts.continueOnError,
|
|
131
|
+
timeoutMs: opts.timeoutMs ?? DEFAULT_JOB_TIMEOUT_MS,
|
|
132
|
+
createdAt: Date.now(),
|
|
133
|
+
reported: false,
|
|
134
|
+
abort: new AbortController(),
|
|
135
|
+
};
|
|
136
|
+
job.done = new Promise((r) => { job.resolveDone = r; });
|
|
137
|
+
jobs.set(job.id, job);
|
|
138
|
+
return job;
|
|
139
|
+
}
|
|
140
|
+
function resultText(r) {
|
|
141
|
+
const parts = (r.content ?? []).map((c) => (c?.type === 'text' ? String(c.text ?? '') : `[${c?.type ?? 'unknown'} content omitted]`));
|
|
142
|
+
const s = parts.join('\n').replace(/\s+$/, '');
|
|
143
|
+
return s.length > MAX_STORED_STEP_OUTPUT
|
|
144
|
+
? `${s.slice(0, MAX_STORED_STEP_OUTPUT / 4)}\n\n[... ${s.length - MAX_STORED_STEP_OUTPUT} characters truncated ...]\n\n${s.slice(-(MAX_STORED_STEP_OUTPUT * 3) / 4)}`
|
|
145
|
+
: s;
|
|
146
|
+
}
|
|
147
|
+
function abortable(p, signal, onAbort) {
|
|
148
|
+
if (signal.aborted)
|
|
149
|
+
return Promise.resolve(onAbort());
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const h = () => resolve(onAbort());
|
|
152
|
+
signal.addEventListener('abort', h, { once: true });
|
|
153
|
+
p.then((v) => { signal.removeEventListener('abort', h); resolve(v); }, (e) => { signal.removeEventListener('abort', h); reject(e); });
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
async function execStep(step, signal, remainingMs) {
|
|
157
|
+
if ('command' in step) {
|
|
158
|
+
return runCommandCore({ command: step.command, cwd: step.cwd, shell: step.shell, raw: step.raw, timeout_ms: Math.max(1, Math.min(step.timeout_ms ?? remainingMs, remainingMs)) }, { maxTimeoutMs: remainingMs, signal, failOnNonZero: true });
|
|
159
|
+
}
|
|
160
|
+
if (JOB_TOOL_NAMES.has(step.tool))
|
|
161
|
+
return text(`Tool "${step.tool}" cannot be used inside a job.`, true);
|
|
162
|
+
if (step.tool === 'run_command') {
|
|
163
|
+
const a = step.args;
|
|
164
|
+
const t = typeof a.timeout_ms === 'number' ? Math.min(a.timeout_ms, remainingMs) : remainingMs;
|
|
165
|
+
return runCommandCore({ ...a, timeout_ms: Math.max(1, t) }, { maxTimeoutMs: remainingMs, signal, failOnNonZero: true });
|
|
166
|
+
}
|
|
167
|
+
if (!executor)
|
|
168
|
+
return text('Job executor not initialised', true);
|
|
169
|
+
return abortable(executor(step.tool, step.args), signal, () => text('CANCELLED', true));
|
|
170
|
+
}
|
|
171
|
+
async function runJob(job) {
|
|
172
|
+
if (job.status !== 'queued')
|
|
173
|
+
return;
|
|
174
|
+
job.status = 'running';
|
|
175
|
+
job.startedAt = Date.now();
|
|
176
|
+
const signal = job.abort.signal;
|
|
177
|
+
const timer = setTimeout(() => { job.timedOut = true; job.abort.abort(); }, job.timeoutMs);
|
|
178
|
+
let anyFailed = false;
|
|
179
|
+
try {
|
|
180
|
+
for (let i = 0; i < job.steps.length; i++) {
|
|
181
|
+
if (signal.aborted)
|
|
182
|
+
break;
|
|
183
|
+
job.currentStep = i;
|
|
184
|
+
const step = job.steps[i];
|
|
185
|
+
const t0 = Date.now();
|
|
186
|
+
let res;
|
|
187
|
+
try {
|
|
188
|
+
res = await execStep(step, signal, Math.max(1, job.timeoutMs - (t0 - job.startedAt)));
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
res = text(`Error: ${e instanceof Error ? e.message : String(e)}`, true);
|
|
192
|
+
}
|
|
193
|
+
const ok = !res.isError;
|
|
194
|
+
job.results.push({ index: i, name: stepName(step), ok, ms: Date.now() - t0, output: resultText(res) });
|
|
195
|
+
if (!ok) {
|
|
196
|
+
anyFailed = true;
|
|
197
|
+
if (!job.continueOnError)
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
clearTimeout(timer);
|
|
204
|
+
job.status = job.cancelRequested ? 'cancelled' : job.timedOut ? 'timed_out' : anyFailed ? 'failed' : 'succeeded';
|
|
205
|
+
job.endedAt = Date.now();
|
|
206
|
+
job.currentStep = -1;
|
|
207
|
+
job.resolveDone();
|
|
208
|
+
void persist(job);
|
|
209
|
+
prune();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/** Pooled jobs (spawn_job) share MAX_CONCURRENT_JOBS slots. */
|
|
213
|
+
function enqueue(job) {
|
|
214
|
+
queue.push(job);
|
|
215
|
+
pump();
|
|
216
|
+
}
|
|
217
|
+
/** Recover from unexpected job executor faults without losing the next jobs.
|
|
218
|
+
* Ordinary tool failures are already handled by runJob; this is the last-resort
|
|
219
|
+
* path for unforeseen formatter/persistence exceptions.
|
|
220
|
+
*/
|
|
221
|
+
function markInternalJobFailure(job, error) {
|
|
222
|
+
const message = (error instanceof Error ? error.message : String(error)).slice(0, 800);
|
|
223
|
+
job.status = 'failed';
|
|
224
|
+
job.endedAt = Date.now();
|
|
225
|
+
job.currentStep = -1;
|
|
226
|
+
job.results.push({ index: job.results.length, name: 'internal job execution', ok: false, ms: 0, output: 'JOB_FAILED: ' + message });
|
|
227
|
+
job.resolveDone?.();
|
|
228
|
+
}
|
|
229
|
+
function pump() {
|
|
230
|
+
while (runningPooled < MAX_CONCURRENT_JOBS && queue.length) {
|
|
231
|
+
const job = queue.shift();
|
|
232
|
+
if (job.status !== 'queued')
|
|
233
|
+
continue; // cancelled while queued
|
|
234
|
+
runningPooled++;
|
|
235
|
+
void runJob(job).catch(e => markInternalJobFailure(job, e))
|
|
236
|
+
.finally(() => { runningPooled--; pump(); });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function cancel(job) {
|
|
240
|
+
if (isFinished(job))
|
|
241
|
+
return false;
|
|
242
|
+
job.cancelRequested = true;
|
|
243
|
+
if (job.status === 'queued') {
|
|
244
|
+
job.status = 'cancelled';
|
|
245
|
+
job.endedAt = Date.now();
|
|
246
|
+
job.resolveDone?.();
|
|
247
|
+
void persist(job);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
job.abort?.abort();
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
// Persistence (best effort)
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
function serialisable(job) {
|
|
258
|
+
const { abort, done, resolveDone, cancelRequested, timedOut, ...rest } = job;
|
|
259
|
+
return rest;
|
|
260
|
+
}
|
|
261
|
+
async function persist(job) {
|
|
262
|
+
try {
|
|
263
|
+
const dir = jobsDir();
|
|
264
|
+
await fs.mkdir(dir, { recursive: true });
|
|
265
|
+
await fs.writeFile(path.join(dir, `${job.id}.json`), JSON.stringify(serialisable(job)), 'utf8');
|
|
266
|
+
const files = (await fs.readdir(dir)).filter((f) => f.endsWith('.json')).sort();
|
|
267
|
+
for (const f of files.slice(0, Math.max(0, files.length - KEEP_ON_DISK)))
|
|
268
|
+
await fs.unlink(path.join(dir, f)).catch(() => { });
|
|
269
|
+
}
|
|
270
|
+
catch { /* ignore */ }
|
|
271
|
+
}
|
|
272
|
+
async function loadHistory() {
|
|
273
|
+
if (historyLoaded)
|
|
274
|
+
return;
|
|
275
|
+
historyLoaded = true;
|
|
276
|
+
try {
|
|
277
|
+
const files = (await fs.readdir(jobsDir())).filter((f) => f.endsWith('.json')).sort().slice(-100);
|
|
278
|
+
for (const f of files) {
|
|
279
|
+
try {
|
|
280
|
+
const j = JSON.parse(await fs.readFile(path.join(jobsDir(), f), 'utf8'));
|
|
281
|
+
if (!j?.id || jobs.has(j.id))
|
|
282
|
+
continue;
|
|
283
|
+
if (j.status === 'queued' || j.status === 'running')
|
|
284
|
+
j.status = 'cancelled';
|
|
285
|
+
j.reported = true;
|
|
286
|
+
j.done = Promise.resolve();
|
|
287
|
+
jobs.set(j.id, j);
|
|
288
|
+
}
|
|
289
|
+
catch { /* skip bad file */ }
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch { /* no dir yet */ }
|
|
293
|
+
}
|
|
294
|
+
function prune() {
|
|
295
|
+
const finished = [...jobs.values()].filter(isFinished).sort((a, b) => (a.endedAt ?? 0) - (b.endedAt ?? 0));
|
|
296
|
+
for (const j of finished.slice(0, Math.max(0, finished.length - KEEP_IN_MEMORY)))
|
|
297
|
+
jobs.delete(j.id);
|
|
298
|
+
}
|
|
299
|
+
// ---------------------------------------------------------------------------
|
|
300
|
+
// Formatting
|
|
301
|
+
// ---------------------------------------------------------------------------
|
|
302
|
+
const secs = (ms) => (ms < 1000 ? `${ms}ms` : `${(ms / 1000).toFixed(1)}s`);
|
|
303
|
+
function clip(s, max) {
|
|
304
|
+
if (s.length <= max)
|
|
305
|
+
return s;
|
|
306
|
+
const head = Math.floor(max * 0.3);
|
|
307
|
+
const tail = max - head;
|
|
308
|
+
return `${s.slice(0, head)}\n[... ${s.length - max} chars omitted; use get_job for more ...]\n${s.slice(-tail)}`;
|
|
309
|
+
}
|
|
310
|
+
function headline(j) {
|
|
311
|
+
const name = j.label ? ` "${j.label}"` : '';
|
|
312
|
+
const grp = j.group ? ` group=${j.group}` : '';
|
|
313
|
+
const now = Date.now();
|
|
314
|
+
let timing = '';
|
|
315
|
+
if (j.status === 'queued')
|
|
316
|
+
timing = `queued ${secs(now - j.createdAt)}`;
|
|
317
|
+
else if (j.status === 'running')
|
|
318
|
+
timing = `running ${secs(now - (j.startedAt ?? now))}, step ${j.currentStep + 1}/${j.steps.length}: ${stepName(j.steps[j.currentStep] ?? j.steps[0])}`;
|
|
319
|
+
else
|
|
320
|
+
timing = `${secs((j.endedAt ?? now) - (j.startedAt ?? j.createdAt))}`;
|
|
321
|
+
const okCount = j.results.filter((r) => r.ok).length;
|
|
322
|
+
const failCount = j.results.length - okCount;
|
|
323
|
+
const counts = isFinished(j) ? ` · ${okCount}/${j.steps.length} steps ok${failCount ? `, ${failCount} failed` : ''}` : '';
|
|
324
|
+
return `[${j.id}${name}${grp}] ${j.status.toUpperCase()} · ${timing}${counts}`;
|
|
325
|
+
}
|
|
326
|
+
function detail(j, outputChars, stepOffset = 0, stepLimit = 10) {
|
|
327
|
+
let s = headline(j);
|
|
328
|
+
const selected = j.results.slice(stepOffset, stepOffset + stepLimit);
|
|
329
|
+
let rendered = 0;
|
|
330
|
+
for (const r of selected) {
|
|
331
|
+
const head = `\n ${r.index + 1}. ${r.ok ? 'OK ' : 'ERR'} ${r.name} (${secs(r.ms)})`;
|
|
332
|
+
const body = r.output ? '\n' + clip(r.output, Math.min(outputChars, 32_000)).split('\n').map(l => ' ' + l).join('\n') : '';
|
|
333
|
+
if (s.length + head.length + body.length > MAX_MCP_JOB_OUTPUT - 600)
|
|
334
|
+
break;
|
|
335
|
+
s += head + body;
|
|
336
|
+
rendered++;
|
|
337
|
+
}
|
|
338
|
+
const next = stepOffset + rendered;
|
|
339
|
+
if (next < j.results.length)
|
|
340
|
+
s += `\n[MORE JOB STEPS: ${j.results.length - next} results remain. Call get_job({id:"${j.id}",step_offset:${next},step_limit:${stepLimit},output_chars:${Math.min(outputChars, 8000)}}).]`;
|
|
341
|
+
const skipped = j.steps.length - j.results.length;
|
|
342
|
+
if (isFinished(j) && skipped > 0)
|
|
343
|
+
s += `\n (${skipped} step(s) not run)`;
|
|
344
|
+
return s;
|
|
345
|
+
}
|
|
346
|
+
function select(a) {
|
|
347
|
+
if (a.ids?.length) {
|
|
348
|
+
const missing = a.ids.filter((id) => !jobs.has(id));
|
|
349
|
+
return { picked: a.ids.map((id) => jobs.get(id)).filter((j) => !!j), missing };
|
|
350
|
+
}
|
|
351
|
+
if (a.group)
|
|
352
|
+
return { picked: [...jobs.values()].filter((j) => j.group === a.group), missing: [] };
|
|
353
|
+
return { picked: [...jobs.values()].filter((j) => !isFinished(j) || !j.reported), missing: [] };
|
|
354
|
+
}
|
|
355
|
+
function summary(picked, outputChars, missing = []) {
|
|
356
|
+
const finished = picked.filter(isFinished);
|
|
357
|
+
const running = picked.filter((j) => j.status === 'running');
|
|
358
|
+
const queued = picked.filter((j) => j.status === 'queued');
|
|
359
|
+
const failed = finished.filter((j) => j.status !== 'succeeded');
|
|
360
|
+
let s = `${finished.length} finished (${failed.length} not successful), ${running.length} running, ${queued.length} queued.`;
|
|
361
|
+
if (missing.length)
|
|
362
|
+
s += `\nUnknown job ids: ${missing.join(', ')}`;
|
|
363
|
+
const fresh = finished.filter((j) => !j.reported);
|
|
364
|
+
const old = finished.filter((j) => j.reported);
|
|
365
|
+
const displayed = [];
|
|
366
|
+
for (const j of fresh) {
|
|
367
|
+
const report = '\n\n' + detail(j, Math.min(outputChars, 6000), 0, 10);
|
|
368
|
+
if (s.length + report.length > MAX_MCP_JOB_OUTPUT - 1200)
|
|
369
|
+
break;
|
|
370
|
+
s += report;
|
|
371
|
+
displayed.push(j);
|
|
372
|
+
}
|
|
373
|
+
if (displayed.length < fresh.length)
|
|
374
|
+
s += `\n[MORE JOB REPORTS: ${fresh.length - displayed.length} not yet shown. Call wait_jobs again with this group or fewer ids; these reports remain unreported.]`;
|
|
375
|
+
if (old.length) {
|
|
376
|
+
const report = '\n\nAlready reported earlier:\n' + old.map(j => ' ' + headline(j)).join('\n');
|
|
377
|
+
s += report.slice(0, Math.max(0, MAX_MCP_JOB_OUTPUT - s.length - 200));
|
|
378
|
+
}
|
|
379
|
+
if (running.length || queued.length) {
|
|
380
|
+
s += `\n\nStill in progress:\n${[...running, ...queued].map((j) => ` ${headline(j)}`).join('\n')}`;
|
|
381
|
+
s += '\nCall wait_jobs again to keep waiting.';
|
|
382
|
+
}
|
|
383
|
+
for (const j of displayed)
|
|
384
|
+
j.reported = true;
|
|
385
|
+
return capJobReply(s);
|
|
386
|
+
}
|
|
387
|
+
async function waitUntil(picked, cond, timeoutMs) {
|
|
388
|
+
const deadline = Date.now() + timeoutMs;
|
|
389
|
+
while (!cond()) {
|
|
390
|
+
const left = deadline - Date.now();
|
|
391
|
+
if (left <= 0)
|
|
392
|
+
return;
|
|
393
|
+
const pending = picked.filter((j) => !isFinished(j)).map((j) => j.done);
|
|
394
|
+
if (!pending.length)
|
|
395
|
+
return;
|
|
396
|
+
let t;
|
|
397
|
+
await Promise.race([...pending.map((p) => p.then(() => undefined)), new Promise((r) => { t = setTimeout(r, left); })]);
|
|
398
|
+
if (t)
|
|
399
|
+
clearTimeout(t);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// ---------------------------------------------------------------------------
|
|
403
|
+
// Handlers
|
|
404
|
+
// ---------------------------------------------------------------------------
|
|
405
|
+
export async function handleSpawnJob(args) {
|
|
406
|
+
const parsed = SpawnJobArgsSchema.safeParse(args);
|
|
407
|
+
if (!parsed.success)
|
|
408
|
+
return text(`Invalid arguments for spawn_job: ${parsed.error.message}`, true);
|
|
409
|
+
const a = parsed.data;
|
|
410
|
+
const steps = [...(a.command ? [{ command: a.command, cwd: a.cwd }] : []), ...(a.steps ?? [])];
|
|
411
|
+
if (!steps.length)
|
|
412
|
+
return text('spawn_job needs "command" or "steps".', true);
|
|
413
|
+
const bad = steps.find((s) => 'tool' in s && JOB_TOOL_NAMES.has(s.tool));
|
|
414
|
+
if (bad)
|
|
415
|
+
return text(`Tool "${bad.tool}" cannot be used inside a job.`, true);
|
|
416
|
+
await loadHistory();
|
|
417
|
+
const job = createJob(steps, { label: a.label, group: a.group, continueOnError: a.continue_on_error, timeoutMs: a.timeout_ms });
|
|
418
|
+
enqueue(job);
|
|
419
|
+
const pos = job.status === 'queued' ? ` (queued: ${MAX_CONCURRENT_JOBS} jobs run at once)` : '';
|
|
420
|
+
return text(`Started job ${job.id}${job.label ? ` "${job.label}"` : ''}${job.group ? ` in group ${job.group}` : ''} with ${steps.length} step(s)${pos}.\nUse wait_jobs to collect the result.`);
|
|
421
|
+
}
|
|
422
|
+
export async function handleWaitJobs(args) {
|
|
423
|
+
const parsed = WaitJobsArgsSchema.safeParse(args);
|
|
424
|
+
if (!parsed.success)
|
|
425
|
+
return text(`Invalid arguments for wait_jobs: ${parsed.error.message}`, true);
|
|
426
|
+
const a = parsed.data;
|
|
427
|
+
await loadHistory();
|
|
428
|
+
const { picked, missing } = select(a);
|
|
429
|
+
if (!picked.length)
|
|
430
|
+
return text(missing.length ? `Unknown job ids: ${missing.join(', ')}` : 'No jobs to wait for.', !!missing.length);
|
|
431
|
+
const cond = a.mode === 'any'
|
|
432
|
+
? () => picked.every(isFinished) || picked.some((j) => isFinished(j) && !j.reported)
|
|
433
|
+
: () => picked.every(isFinished);
|
|
434
|
+
await waitUntil(picked, cond, a.timeout_ms);
|
|
435
|
+
return text(summary(picked, a.output_chars, missing));
|
|
436
|
+
}
|
|
437
|
+
export async function handleGetJob(args) {
|
|
438
|
+
const parsed = GetJobArgsSchema.safeParse(args);
|
|
439
|
+
if (!parsed.success)
|
|
440
|
+
return text(`Invalid arguments for get_job: ${parsed.error.message}`, true);
|
|
441
|
+
await loadHistory();
|
|
442
|
+
const j = jobs.get(parsed.data.id);
|
|
443
|
+
if (!j)
|
|
444
|
+
return text(`Unknown job id: ${parsed.data.id}`, true);
|
|
445
|
+
const out = detail(j, parsed.data.output_chars, parsed.data.step_offset, parsed.data.step_limit);
|
|
446
|
+
if (isFinished(j) && !out.includes('[MORE JOB STEPS:'))
|
|
447
|
+
j.reported = true;
|
|
448
|
+
return text(out);
|
|
449
|
+
}
|
|
450
|
+
export async function handleListJobs(args) {
|
|
451
|
+
const parsed = ListJobsArgsSchema.safeParse(args);
|
|
452
|
+
if (!parsed.success)
|
|
453
|
+
return text(`Invalid arguments for list_jobs: ${parsed.error.message}`, true);
|
|
454
|
+
const a = parsed.data;
|
|
455
|
+
await loadHistory();
|
|
456
|
+
let list = [...jobs.values()];
|
|
457
|
+
if (a.group)
|
|
458
|
+
list = list.filter((j) => j.group === a.group);
|
|
459
|
+
if (a.status === 'active')
|
|
460
|
+
list = list.filter((j) => !isFinished(j));
|
|
461
|
+
else if (a.status === 'finished')
|
|
462
|
+
list = list.filter(isFinished);
|
|
463
|
+
else if (a.status)
|
|
464
|
+
list = list.filter((j) => j.status === a.status);
|
|
465
|
+
list.sort((x, y) => y.createdAt - x.createdAt);
|
|
466
|
+
if (!list.length)
|
|
467
|
+
return text('No jobs.');
|
|
468
|
+
const shown = list.slice(0, a.limit);
|
|
469
|
+
return text(`${list.length} job(s)${list.length > shown.length ? `, showing ${shown.length}` : ''}:\n${shown.map((j) => ` ${headline(j)}`).join('\n')}`);
|
|
470
|
+
}
|
|
471
|
+
export async function handleCancelJob(args) {
|
|
472
|
+
const parsed = CancelJobArgsSchema.safeParse(args);
|
|
473
|
+
if (!parsed.success)
|
|
474
|
+
return text(`Invalid arguments for cancel_job: ${parsed.error.message}`, true);
|
|
475
|
+
const a = parsed.data;
|
|
476
|
+
if (!a.all && !a.ids?.length && !a.group)
|
|
477
|
+
return text('cancel_job needs ids, group or all=true.', true);
|
|
478
|
+
const picked = a.all ? [...jobs.values()] : select(a).picked;
|
|
479
|
+
const cancelled = picked.filter(cancel);
|
|
480
|
+
await Promise.race([Promise.all(cancelled.map((j) => j.done)), new Promise((r) => setTimeout(r, 5_000))]);
|
|
481
|
+
return text(cancelled.length
|
|
482
|
+
? `Cancelled ${cancelled.length} job(s):\n${cancelled.map((j) => ` ${headline(j)}`).join('\n')}`
|
|
483
|
+
: 'Nothing to cancel (no matching queued or running jobs).');
|
|
484
|
+
}
|
|
485
|
+
export async function handleRunParallel(args) {
|
|
486
|
+
const parsed = RunParallelArgsSchema.safeParse(args);
|
|
487
|
+
if (!parsed.success)
|
|
488
|
+
return text(`Invalid arguments for run_parallel: ${parsed.error.message}`, true);
|
|
489
|
+
const a = parsed.data;
|
|
490
|
+
const bad = a.calls.find((s) => 'tool' in s && JOB_TOOL_NAMES.has(s.tool));
|
|
491
|
+
if (bad)
|
|
492
|
+
return text(`Tool "${bad.tool}" cannot be used inside run_parallel.`, true);
|
|
493
|
+
await loadHistory();
|
|
494
|
+
const group = `par-${Date.now().toString(36)}`;
|
|
495
|
+
const batch = a.calls.map((c, i) => createJob([c], { label: c.label ?? `#${i + 1} ${stepName(c)}`, group }));
|
|
496
|
+
// Own worker pool, separate from spawn_job's slots, so quick reads are never stuck behind long jobs.
|
|
497
|
+
let next = 0;
|
|
498
|
+
const worker = async () => {
|
|
499
|
+
while (next < batch.length) {
|
|
500
|
+
const job = batch[next++];
|
|
501
|
+
try {
|
|
502
|
+
await runJob(job);
|
|
503
|
+
}
|
|
504
|
+
catch (e) {
|
|
505
|
+
markInternalJobFailure(job, e);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
for (let w = 0; w < Math.min(a.max_concurrency, batch.length); w++)
|
|
510
|
+
void worker().catch(() => { });
|
|
511
|
+
await waitUntil(batch, () => batch.every(isFinished), a.timeout_ms);
|
|
512
|
+
const finished = batch.filter(isFinished);
|
|
513
|
+
const failed = finished.filter((j) => j.status !== 'succeeded');
|
|
514
|
+
let s = `run_parallel: ${finished.length}/${batch.length} done${failed.length ? `, ${failed.length} failed` : ''}.`;
|
|
515
|
+
let shown = 0;
|
|
516
|
+
for (const j of batch) {
|
|
517
|
+
const r = j.results[0];
|
|
518
|
+
let part = '\n\n' + (isFinished(j)
|
|
519
|
+
? `=== ${j.label} -> ${j.status === 'succeeded' ? 'OK' : j.status.toUpperCase()} (${secs((j.endedAt ?? 0) - (j.startedAt ?? j.createdAt))}) jobId=${j.id}`
|
|
520
|
+
: `=== ${j.label} -> STILL ${j.status.toUpperCase()} as background job ${j.id}`);
|
|
521
|
+
if (isFinished(j) && r?.output)
|
|
522
|
+
part += '\n' + clip(r.output, Math.min(a.output_chars, 16000));
|
|
523
|
+
if (s.length + part.length > MAX_MCP_JOB_OUTPUT - 800)
|
|
524
|
+
break;
|
|
525
|
+
s += part;
|
|
526
|
+
shown++;
|
|
527
|
+
if (isFinished(j))
|
|
528
|
+
j.reported = true;
|
|
529
|
+
}
|
|
530
|
+
if (shown < batch.length)
|
|
531
|
+
s += `\n[MORE PARALLEL RESULTS: ${batch.length - shown} omitted from this reply. Call wait_jobs({group:"${group}"}) for unreported results or get_job({id:"..."}) by job ID. Do not rerun already-completed mutations.]`;
|
|
532
|
+
if (finished.length < batch.length)
|
|
533
|
+
s += `\n\nUnfinished calls keep running. Collect them with wait_jobs({"group":"${group}"}).`;
|
|
534
|
+
return text(s);
|
|
535
|
+
}
|
|
536
|
+
// ---------------------------------------------------------------------------
|
|
537
|
+
// Registry
|
|
538
|
+
// ---------------------------------------------------------------------------
|
|
539
|
+
export const JOB_TOOL_HANDLERS = {
|
|
540
|
+
spawn_job: handleSpawnJob,
|
|
541
|
+
wait_jobs: handleWaitJobs,
|
|
542
|
+
get_job: handleGetJob,
|
|
543
|
+
list_jobs: handleListJobs,
|
|
544
|
+
cancel_job: handleCancelJob,
|
|
545
|
+
run_parallel: handleRunParallel,
|
|
546
|
+
};
|
|
547
|
+
export function buildJobToolDefinitions() {
|
|
548
|
+
return [
|
|
549
|
+
{
|
|
550
|
+
name: 'run_parallel',
|
|
551
|
+
description: [
|
|
552
|
+
'Run independent tool calls/shell commands concurrently on this device. Total combined reply is capped at 128K characters; use wait_jobs(group) and get_job(id,step_offset) for omitted results without rerunning side effects.',
|
|
553
|
+
'Use it whenever you would otherwise make several independent calls in a row: reading many files, several searches,',
|
|
554
|
+
'git_status in many repos, running lint + typecheck + tests together, etc.',
|
|
555
|
+
'Each item is {tool, args} (any BRC tool, args exactly as for a direct call) or {command, cwd?, timeout_ms?}.',
|
|
556
|
+
'Results come back in input order, with bounded output and an explicit group for follow-up. Do not run writes to the same file concurrently. Items still running at timeout_ms (default 90s, max 100s) continue as',
|
|
557
|
+
'background jobs; collect them with wait_jobs({group}). Commands that exit non-zero are reported as failed.',
|
|
558
|
+
].join('\n'),
|
|
559
|
+
inputSchema: zodToJsonSchema(RunParallelArgsSchema),
|
|
560
|
+
annotations: { title: 'Run in parallel', readOnlyHint: false, destructiveHint: true, openWorldHint: true },
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
name: 'spawn_job',
|
|
564
|
+
description: [
|
|
565
|
+
'Start a background worker job on this device and return its id immediately.',
|
|
566
|
+
'Use for work that takes longer than one call (builds, test suites, installs, long scripts) or to run several',
|
|
567
|
+
'workstreams in parallel: spawn one job per piece, then call wait_jobs. A job is either a single "command"',
|
|
568
|
+
'or "steps": a list run in order, each {command, cwd?, timeout_ms?} or {tool, args} (e.g. edit_block then run tests).',
|
|
569
|
+
'A failed step stops the job unless continue_on_error=true. Jobs are not limited by the 2-minute call timeout',
|
|
570
|
+
`(default limit ${DEFAULT_JOB_TIMEOUT_MS / 60_000} min, max 6 h). Up to ${MAX_CONCURRENT_JOBS} jobs run at once; more are queued.`,
|
|
571
|
+
'Give related jobs the same "group" to wait for or cancel them together.',
|
|
572
|
+
].join('\n'),
|
|
573
|
+
inputSchema: zodToJsonSchema(SpawnJobArgsSchema),
|
|
574
|
+
annotations: { title: 'Spawn background job', readOnlyHint: false, destructiveHint: true, openWorldHint: true },
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
name: 'wait_jobs',
|
|
578
|
+
description: [
|
|
579
|
+
`Wait (up to timeout_ms, default ${DEFAULT_WAIT_MS / 1000}s, max ${MAX_WAIT_MS / 1000}s) for background jobs and return their results.`,
|
|
580
|
+
'Select jobs by ids or group; with neither, waits for all unfinished jobs plus finished ones not yet reported.',
|
|
581
|
+
'mode=all (default) returns when every selected job is done; mode=any returns as soon as one more finishes,',
|
|
582
|
+
'so you can react to results one by one. Each finished job\'s output is shown once; if some are still running,',
|
|
583
|
+
'call wait_jobs again.',
|
|
584
|
+
].join('\n'),
|
|
585
|
+
inputSchema: zodToJsonSchema(WaitJobsArgsSchema),
|
|
586
|
+
annotations: { title: 'Wait for jobs', readOnlyHint: true },
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
name: 'get_job',
|
|
590
|
+
description: 'Get the status and full step outputs of one background job (larger output limit than wait_jobs).',
|
|
591
|
+
inputSchema: zodToJsonSchema(GetJobArgsSchema),
|
|
592
|
+
annotations: { title: 'Get job', readOnlyHint: true },
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
name: 'list_jobs',
|
|
596
|
+
description: 'List background jobs (newest first), optionally filtered by group or status (queued, running, succeeded, failed, cancelled, timed_out, active, finished). Includes recent jobs from before an agent restart.',
|
|
597
|
+
inputSchema: zodToJsonSchema(ListJobsArgsSchema),
|
|
598
|
+
annotations: { title: 'List jobs', readOnlyHint: true },
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
name: 'cancel_job',
|
|
602
|
+
description: 'Cancel queued or running background jobs by ids, by group, or all=true. Running commands are killed with their whole process tree.',
|
|
603
|
+
inputSchema: zodToJsonSchema(CancelJobArgsSchema),
|
|
604
|
+
annotations: { title: 'Cancel jobs', readOnlyHint: false, destructiveHint: true },
|
|
605
|
+
},
|
|
606
|
+
];
|
|
607
|
+
}
|
|
608
|
+
//# sourceMappingURL=jobs.js.map
|