@cortex-agent/server 2026.5.19-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/defaults/.claude/settings.json +13 -0
- package/defaults/CORTEX.md +15 -0
- package/defaults/config/budget.json +4 -0
- package/defaults/config/session-hooks.json +7 -0
- package/defaults/config/thread-templates.json +528 -0
- package/defaults/context/CORTEX.md +40 -0
- package/defaults/context/decisions/DR-0012.md +54 -0
- package/defaults/context/ideas/CORTEX.md +10 -0
- package/defaults/context/projects/CORTEX.md +25 -0
- package/defaults/context/scans/CORTEX.md +13 -0
- package/defaults/context/user/CORTEX.md +21 -0
- package/defaults/data/schedules.json +52 -0
- package/defaults/gitignore +44 -0
- package/defaults/hooks/ask-user-question-hook.mjs +66 -0
- package/defaults/hooks/cortex-md-injector.mjs +237 -0
- package/defaults/hooks/exit-plan-mode-hook.mjs +84 -0
- package/defaults/hooks/memory-ref-tracker.mjs +154 -0
- package/defaults/hooks/new-session-hook.mjs +83 -0
- package/defaults/hooks/post-task-hook.mjs +63 -0
- package/defaults/hooks/rules-loader.mjs +212 -0
- package/defaults/hooks/sensitive-file-edit.mjs +84 -0
- package/defaults/hooks/session-activity-tracker.mjs +254 -0
- package/defaults/hooks/task-status-check.mjs +218 -0
- package/defaults/hooks/tasks-yaml-guard.mjs +230 -0
- package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-coder/skills/cli-standards/SKILL.md +208 -0
- package/defaults/plugins/cortex-coder/skills/code-standards/SKILL.md +146 -0
- package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +164 -0
- package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-common/skills/compound/SKILL.md +349 -0
- package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +104 -0
- package/defaults/plugins/cortex-common/skills/critique/SKILL.md +88 -0
- package/defaults/plugins/cortex-common/skills/debug-campaign/SKILL.md +508 -0
- package/defaults/plugins/cortex-common/skills/diagnose/SKILL.md +169 -0
- package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +135 -0
- package/defaults/plugins/cortex-common/skills/solution-design/SKILL.md +183 -0
- package/defaults/plugins/cortex-common/skills/synthesize/SKILL.md +120 -0
- package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +89 -0
- package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +163 -0
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +425 -0
- package/defaults/plugins/cortex-system/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-system/skills/approval/SKILL.md +84 -0
- package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +199 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/SKILL.md +201 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/extract_session.py +165 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/scan_logs.py +402 -0
- package/defaults/plugins/cortex-system/skills/evolve/SKILL.md +115 -0
- package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +262 -0
- package/defaults/plugins/cortex-system/skills/gravity/SKILL.md +125 -0
- package/defaults/plugins/cortex-system/skills/orient/SKILL.md +310 -0
- package/defaults/plugins/cortex-system/skills/orient-project/SKILL.md +85 -0
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +167 -0
- package/defaults/plugins/cortex-system/skills/refresh-skills/SKILL.md +153 -0
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +209 -0
- package/defaults/plugins/cortex-system/skills/thread/SKILL.md +478 -0
- package/defaults/plugins/cortex-system/skills/user-learn/SKILL.md +95 -0
- package/defaults/prompts/directives/coder-reviewer.md +106 -0
- package/defaults/prompts/directives/coder.md +90 -0
- package/defaults/prompts/directives/director.md +97 -0
- package/defaults/prompts/directives/doc-reviewer.md +93 -0
- package/defaults/prompts/directives/doc-writer.md +85 -0
- package/defaults/prompts/directives/executor-reviewer.md +96 -0
- package/defaults/prompts/directives/executor.md +85 -0
- package/defaults/prompts/directives/milestone-executor.md +119 -0
- package/defaults/prompts/directives/milestone-reviewer.md +108 -0
- package/defaults/prompts/directives/plan-executor.md +121 -0
- package/defaults/prompts/directives/plan-reviewer.md +99 -0
- package/defaults/prompts/systemPrompts/coder.md +64 -0
- package/defaults/prompts/systemPrompts/direct.md +92 -0
- package/defaults/prompts/systemPrompts/web-opus-4-6.md +68 -0
- package/defaults/prompts/systemPrompts/web-sonnet-4-6.md +76 -0
- package/defaults/prompts/systemPrompts/web.md +50 -0
- package/defaults/prompts/systemPrompts/worker.md +67 -0
- package/defaults/rules/correction-propagation.md +31 -0
- package/defaults/rules/decision-records.md +49 -0
- package/defaults/rules/experiment-format.md +165 -0
- package/defaults/rules/issues-md.md +37 -0
- package/defaults/rules/meta-conventions.md +45 -0
- package/defaults/rules/mission-md.md +47 -0
- package/defaults/rules/overview-md.md +41 -0
- package/defaults/rules/roadmap-md.md +55 -0
- package/defaults/rules/status-md.md +49 -0
- package/defaults/rules/task-system.md +53 -0
- package/dist/agent-adapter/capabilities.d.ts +12 -0
- package/dist/agent-adapter/capabilities.js +52 -0
- package/dist/agent-adapter/claude/adapter-tui.d.ts +140 -0
- package/dist/agent-adapter/claude/adapter-tui.js +422 -0
- package/dist/agent-adapter/claude/adapter.d.ts +85 -0
- package/dist/agent-adapter/claude/adapter.js +771 -0
- package/dist/agent-adapter/claude/cost-from-usage.d.ts +62 -0
- package/dist/agent-adapter/claude/cost-from-usage.js +97 -0
- package/dist/agent-adapter/claude/defaults.d.ts +30 -0
- package/dist/agent-adapter/claude/defaults.js +49 -0
- package/dist/agent-adapter/claude/event-parser.d.ts +28 -0
- package/dist/agent-adapter/claude/event-parser.js +173 -0
- package/dist/agent-adapter/claude/hooks-builder.d.ts +36 -0
- package/dist/agent-adapter/claude/hooks-builder.js +57 -0
- package/dist/agent-adapter/claude/jsonl-tail.d.ts +68 -0
- package/dist/agent-adapter/claude/jsonl-tail.js +344 -0
- package/dist/agent-adapter/claude/spawn-args.d.ts +38 -0
- package/dist/agent-adapter/claude/spawn-args.js +83 -0
- package/dist/agent-adapter/claude/tmux-control.d.ts +69 -0
- package/dist/agent-adapter/claude/tmux-control.js +127 -0
- package/dist/agent-adapter/claude/tool-summarizers.d.ts +2 -0
- package/dist/agent-adapter/claude/tool-summarizers.js +24 -0
- package/dist/agent-adapter/codex/adapter.d.ts +42 -0
- package/dist/agent-adapter/codex/adapter.js +916 -0
- package/dist/agent-adapter/codex/event-parser.d.ts +17 -0
- package/dist/agent-adapter/codex/event-parser.js +41 -0
- package/dist/agent-adapter/codex/spawn-args.d.ts +6 -0
- package/dist/agent-adapter/codex/spawn-args.js +30 -0
- package/dist/agent-adapter/index.d.ts +8 -0
- package/dist/agent-adapter/index.js +33 -0
- package/dist/agent-adapter/normalize/event-stream.d.ts +6 -0
- package/dist/agent-adapter/normalize/event-stream.js +47 -0
- package/dist/agent-adapter/normalize/event-types.d.ts +58 -0
- package/dist/agent-adapter/normalize/event-types.js +5 -0
- package/dist/agent-adapter/normalize/hooks.d.ts +26 -0
- package/dist/agent-adapter/normalize/hooks.js +5 -0
- package/dist/agent-adapter/normalize/prompt-builder.d.ts +6 -0
- package/dist/agent-adapter/normalize/prompt-builder.js +23 -0
- package/dist/agent-adapter/normalize/tool-names.d.ts +6 -0
- package/dist/agent-adapter/normalize/tool-names.js +73 -0
- package/dist/agent-adapter/pi/adapter.d.ts +60 -0
- package/dist/agent-adapter/pi/adapter.js +626 -0
- package/dist/agent-adapter/pi/agent-dir.d.ts +11 -0
- package/dist/agent-adapter/pi/agent-dir.js +34 -0
- package/dist/agent-adapter/pi/event-parser.d.ts +26 -0
- package/dist/agent-adapter/pi/event-parser.js +313 -0
- package/dist/agent-adapter/pi/framing.d.ts +6 -0
- package/dist/agent-adapter/pi/framing.js +35 -0
- package/dist/agent-adapter/pi/hook-bridge.d.ts +59 -0
- package/dist/agent-adapter/pi/hook-bridge.js +233 -0
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +26 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +140 -0
- package/dist/agent-adapter/pi/pi-ext-types.d.ts +90 -0
- package/dist/agent-adapter/pi/pi-ext-types.js +5 -0
- package/dist/agent-adapter/pi/spawn-args.d.ts +20 -0
- package/dist/agent-adapter/pi/spawn-args.js +54 -0
- package/dist/agent-adapter/pi/tool-shims.d.ts +2 -0
- package/dist/agent-adapter/pi/tool-shims.js +181 -0
- package/dist/agent-adapter/types.d.ts +94 -0
- package/dist/agent-adapter/types.js +5 -0
- package/dist/core/async-mutex.d.ts +6 -0
- package/dist/core/async-mutex.js +36 -0
- package/dist/core/cli-utils.d.ts +37 -0
- package/dist/core/cli-utils.js +69 -0
- package/dist/core/config-generator.d.ts +9 -0
- package/dist/core/config-generator.js +58 -0
- package/dist/core/gateway-generator.d.ts +46 -0
- package/dist/core/gateway-generator.js +336 -0
- package/dist/core/log.d.ts +7 -0
- package/dist/core/log.js +103 -0
- package/dist/core/paths.d.ts +32 -0
- package/dist/core/paths.js +49 -0
- package/dist/core/profile-generator.d.ts +65 -0
- package/dist/core/profile-generator.js +310 -0
- package/dist/core/running-executions.d.ts +88 -0
- package/dist/core/running-executions.js +186 -0
- package/dist/core/status-format.d.ts +18 -0
- package/dist/core/status-format.js +31 -0
- package/dist/core/task-parser.d.ts +69 -0
- package/dist/core/task-parser.js +423 -0
- package/dist/core/types/agent-types.d.ts +31 -0
- package/dist/core/types/agent-types.js +5 -0
- package/dist/core/types/thread-types.d.ts +261 -0
- package/dist/core/types/thread-types.js +5 -0
- package/dist/core/utils.d.ts +29 -0
- package/dist/core/utils.js +98 -0
- package/dist/core/version.d.ts +3 -0
- package/dist/core/version.js +12 -0
- package/dist/domain/agents/config.d.ts +58 -0
- package/dist/domain/agents/config.js +313 -0
- package/dist/domain/agents/facade.d.ts +52 -0
- package/dist/domain/agents/facade.js +264 -0
- package/dist/domain/agents/index.d.ts +2 -0
- package/dist/domain/agents/index.js +4 -0
- package/dist/domain/agents/profile-manager.d.ts +54 -0
- package/dist/domain/agents/profile-manager.js +161 -0
- package/dist/domain/costs/codex-event-format.d.ts +4 -0
- package/dist/domain/costs/codex-event-format.js +66 -0
- package/dist/domain/costs/codex-usage-monitor.d.ts +7 -0
- package/dist/domain/costs/codex-usage-monitor.js +72 -0
- package/dist/domain/costs/cost-tracker.d.ts +115 -0
- package/dist/domain/costs/cost-tracker.js +327 -0
- package/dist/domain/costs/gateway-manager.d.ts +6 -0
- package/dist/domain/costs/gateway-manager.js +228 -0
- package/dist/domain/costs/rate-limit-parser.d.ts +11 -0
- package/dist/domain/costs/rate-limit-parser.js +87 -0
- package/dist/domain/costs/rate-limit-throttle.d.ts +32 -0
- package/dist/domain/costs/rate-limit-throttle.js +129 -0
- package/dist/domain/executions/registry.d.ts +20 -0
- package/dist/domain/executions/registry.js +124 -0
- package/dist/domain/mcp/core-server.d.ts +2 -0
- package/dist/domain/mcp/core-server.js +34 -0
- package/dist/domain/mcp/server.d.ts +3 -0
- package/dist/domain/mcp/server.js +78 -0
- package/dist/domain/mcp/tools/context.d.ts +23 -0
- package/dist/domain/mcp/tools/context.js +51 -0
- package/dist/domain/mcp/tools/cortex-md.d.ts +17 -0
- package/dist/domain/mcp/tools/cortex-md.js +36 -0
- package/dist/domain/mcp/tools/cost.d.ts +2 -0
- package/dist/domain/mcp/tools/cost.js +54 -0
- package/dist/domain/mcp/tools/executions.d.ts +2 -0
- package/dist/domain/mcp/tools/executions.js +94 -0
- package/dist/domain/mcp/tools/schedule.d.ts +23 -0
- package/dist/domain/mcp/tools/schedule.js +245 -0
- package/dist/domain/mcp/tools/slack.d.ts +21 -0
- package/dist/domain/mcp/tools/slack.js +124 -0
- package/dist/domain/mcp/tools/task-ops.d.ts +2 -0
- package/dist/domain/mcp/tools/task-ops.js +509 -0
- package/dist/domain/mcp/tools/tui-ask.d.ts +31 -0
- package/dist/domain/mcp/tools/tui-ask.js +108 -0
- package/dist/domain/mcp/tools/tui-plan.d.ts +28 -0
- package/dist/domain/mcp/tools/tui-plan.js +118 -0
- package/dist/domain/mcp/tui-server.d.ts +2 -0
- package/dist/domain/mcp/tui-server.js +69 -0
- package/dist/domain/memory/consolidate.d.ts +55 -0
- package/dist/domain/memory/consolidate.js +471 -0
- package/dist/domain/memory/cortex-md-injector.d.ts +30 -0
- package/dist/domain/memory/cortex-md-injector.js +123 -0
- package/dist/domain/memory/cortex-md-scanner.d.ts +9 -0
- package/dist/domain/memory/cortex-md-scanner.js +67 -0
- package/dist/domain/memory/index-regen.d.ts +19 -0
- package/dist/domain/memory/index-regen.js +398 -0
- package/dist/domain/memory/rules-loader.d.ts +27 -0
- package/dist/domain/memory/rules-loader.js +97 -0
- package/dist/domain/memory/skill-scanner.d.ts +5 -0
- package/dist/domain/memory/skill-scanner.js +132 -0
- package/dist/domain/memory/user-context.d.ts +5 -0
- package/dist/domain/memory/user-context.js +34 -0
- package/dist/domain/memory/watcher.d.ts +2 -0
- package/dist/domain/memory/watcher.js +81 -0
- package/dist/domain/monitor/disk-monitor.d.ts +20 -0
- package/dist/domain/monitor/disk-monitor.js +97 -0
- package/dist/domain/monitor/gpu-monitor.d.ts +26 -0
- package/dist/domain/monitor/gpu-monitor.js +135 -0
- package/dist/domain/remote/client-bootstrap.d.ts +1 -0
- package/dist/domain/remote/client-bootstrap.js +139 -0
- package/dist/domain/remote/client-hot-reload.d.ts +18 -0
- package/dist/domain/remote/client-hot-reload.js +142 -0
- package/dist/domain/remote/client-manager.d.ts +50 -0
- package/dist/domain/remote/client-manager.js +452 -0
- package/dist/domain/remote/cortex-client.d.ts +1 -0
- package/dist/domain/remote/cortex-client.js +704 -0
- package/dist/domain/scheduling/job-registry.d.ts +31 -0
- package/dist/domain/scheduling/job-registry.js +34 -0
- package/dist/domain/scheduling/jobs/_shared.d.ts +14 -0
- package/dist/domain/scheduling/jobs/_shared.js +45 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.d.ts +1 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.js +16 -0
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +11 -0
- package/dist/domain/scheduling/jobs/scheduled-task.js +186 -0
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +41 -0
- package/dist/domain/scheduling/jobs/target-dispatch.js +48 -0
- package/dist/domain/scheduling/jobs/task-archive.d.ts +1 -0
- package/dist/domain/scheduling/jobs/task-archive.js +19 -0
- package/dist/domain/scheduling/jobs/task-dispatch.d.ts +12 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +214 -0
- package/dist/domain/scheduling/runner.d.ts +13 -0
- package/dist/domain/scheduling/runner.js +24 -0
- package/dist/domain/scheduling/schedule-cli.d.ts +16 -0
- package/dist/domain/scheduling/schedule-cli.js +268 -0
- package/dist/domain/scheduling/schedule-command.d.ts +4 -0
- package/dist/domain/scheduling/schedule-command.js +274 -0
- package/dist/domain/scheduling/scheduler.d.ts +86 -0
- package/dist/domain/scheduling/scheduler.js +544 -0
- package/dist/domain/sessions/session-backup.d.ts +49 -0
- package/dist/domain/sessions/session-backup.js +217 -0
- package/dist/domain/sessions/session-hooks.d.ts +103 -0
- package/dist/domain/sessions/session-hooks.js +311 -0
- package/dist/domain/sessions/session-registry.d.ts +21 -0
- package/dist/domain/sessions/session-registry.js +31 -0
- package/dist/domain/sessions/session.d.ts +3 -0
- package/dist/domain/sessions/session.js +14 -0
- package/dist/domain/tasks/archiver.d.ts +9 -0
- package/dist/domain/tasks/archiver.js +123 -0
- package/dist/domain/tasks/dispatch-utils.d.ts +37 -0
- package/dist/domain/tasks/dispatch-utils.js +143 -0
- package/dist/domain/tasks/dispatcher.d.ts +48 -0
- package/dist/domain/tasks/dispatcher.js +285 -0
- package/dist/domain/tasks/lint.d.ts +12 -0
- package/dist/domain/tasks/lint.js +102 -0
- package/dist/domain/tasks/mutator.d.ts +21 -0
- package/dist/domain/tasks/mutator.js +208 -0
- package/dist/domain/tasks/parser.d.ts +1 -0
- package/dist/domain/tasks/parser.js +6 -0
- package/dist/domain/tasks/pending-tracker.d.ts +67 -0
- package/dist/domain/tasks/pending-tracker.js +163 -0
- package/dist/domain/tasks/recommendation/extractor.d.ts +12 -0
- package/dist/domain/tasks/recommendation/extractor.js +563 -0
- package/dist/domain/tasks/recommendation/patterns.d.ts +11 -0
- package/dist/domain/tasks/recommendation/patterns.js +99 -0
- package/dist/domain/tasks/recommendation/types.d.ts +67 -0
- package/dist/domain/tasks/recommendation/types.js +5 -0
- package/dist/domain/tasks/store.d.ts +1 -0
- package/dist/domain/tasks/store.js +3 -0
- package/dist/domain/tasks/system/cortex-run.d.ts +24 -0
- package/dist/domain/tasks/system/cortex-run.js +342 -0
- package/dist/domain/tasks/system/task-cli.d.ts +49 -0
- package/dist/domain/tasks/system/task-cli.js +664 -0
- package/dist/domain/tasks/system/task-completion.d.ts +22 -0
- package/dist/domain/tasks/system/task-completion.js +89 -0
- package/dist/domain/tasks/system/task-id-utils.d.ts +24 -0
- package/dist/domain/tasks/system/task-id-utils.js +84 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +34 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.js +208 -0
- package/dist/domain/tasks/system/task-lock.d.ts +25 -0
- package/dist/domain/tasks/system/task-lock.js +139 -0
- package/dist/domain/tasks/system/task-mutations.d.ts +31 -0
- package/dist/domain/tasks/system/task-mutations.js +110 -0
- package/dist/domain/tasks/system/task-process.d.ts +37 -0
- package/dist/domain/tasks/system/task-process.js +119 -0
- package/dist/domain/tasks/system/task-state.d.ts +70 -0
- package/dist/domain/tasks/system/task-state.js +176 -0
- package/dist/domain/threads/artifact-io.d.ts +18 -0
- package/dist/domain/threads/artifact-io.js +182 -0
- package/dist/domain/threads/auto-thread.d.ts +3 -0
- package/dist/domain/threads/auto-thread.js +19 -0
- package/dist/domain/threads/hook-runner.d.ts +4 -0
- package/dist/domain/threads/hook-runner.js +250 -0
- package/dist/domain/threads/index.d.ts +5 -0
- package/dist/domain/threads/index.js +7 -0
- package/dist/domain/threads/prompt-builder.d.ts +25 -0
- package/dist/domain/threads/prompt-builder.js +176 -0
- package/dist/domain/threads/runner.d.ts +83 -0
- package/dist/domain/threads/runner.js +434 -0
- package/dist/domain/threads/state-machine.d.ts +61 -0
- package/dist/domain/threads/state-machine.js +440 -0
- package/dist/domain/threads/template-loader.d.ts +20 -0
- package/dist/domain/threads/template-loader.js +189 -0
- package/dist/domain/threads/template-resolver.d.ts +1 -0
- package/dist/domain/threads/template-resolver.js +95 -0
- package/dist/domain/threads/utils.d.ts +19 -0
- package/dist/domain/threads/utils.js +49 -0
- package/dist/entry/app.d.ts +1 -0
- package/dist/entry/app.js +234 -0
- package/dist/entry/cli.d.ts +12 -0
- package/dist/entry/cli.js +254 -0
- package/dist/entry/daemon.d.ts +11 -0
- package/dist/entry/daemon.js +521 -0
- package/dist/entry/init.d.ts +93 -0
- package/dist/entry/init.js +1299 -0
- package/dist/entry/startup-helpers.d.ts +2 -0
- package/dist/entry/startup-helpers.js +56 -0
- package/dist/entry/startup-notify.d.ts +10 -0
- package/dist/entry/startup-notify.js +20 -0
- package/dist/events/event-bus.d.ts +42 -0
- package/dist/events/event-bus.js +84 -0
- package/dist/events/event-logger.d.ts +12 -0
- package/dist/events/event-logger.js +135 -0
- package/dist/events/event-replay.d.ts +1 -0
- package/dist/events/event-replay.js +51 -0
- package/dist/events/event-types.d.ts +145 -0
- package/dist/events/event-types.js +5 -0
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +6 -0
- package/dist/hooks/ask-user-question-hook.mjs +66 -0
- package/dist/hooks/cortex-md-injector.mjs +237 -0
- package/dist/hooks/exit-plan-mode-hook.mjs +84 -0
- package/dist/hooks/memory-ref-tracker.mjs +154 -0
- package/dist/hooks/new-session-hook.mjs +83 -0
- package/dist/hooks/post-task-hook.mjs +63 -0
- package/dist/hooks/rules-loader.mjs +212 -0
- package/dist/hooks/sensitive-file-edit.mjs +84 -0
- package/dist/hooks/session-activity-tracker.mjs +254 -0
- package/dist/hooks/task-status-check.mjs +218 -0
- package/dist/hooks/tasks-yaml-guard.mjs +230 -0
- package/dist/orchestration/agent-runner.d.ts +72 -0
- package/dist/orchestration/agent-runner.js +289 -0
- package/dist/orchestration/busy-tracker.d.ts +12 -0
- package/dist/orchestration/busy-tracker.js +35 -0
- package/dist/orchestration/channel-queue.d.ts +18 -0
- package/dist/orchestration/channel-queue.js +32 -0
- package/dist/orchestration/dispatch-reconciler.d.ts +1 -0
- package/dist/orchestration/dispatch-reconciler.js +16 -0
- package/dist/orchestration/durable-helpers.d.ts +4 -0
- package/dist/orchestration/durable-helpers.js +42 -0
- package/dist/orchestration/interactions/ask-user-question.d.ts +32 -0
- package/dist/orchestration/interactions/ask-user-question.js +182 -0
- package/dist/orchestration/interactions/command-action-router.d.ts +47 -0
- package/dist/orchestration/interactions/command-action-router.js +91 -0
- package/dist/orchestration/interactions/interaction-handlers.d.ts +5 -0
- package/dist/orchestration/interactions/interaction-handlers.js +279 -0
- package/dist/orchestration/interactions/plan-approvals.d.ts +47 -0
- package/dist/orchestration/interactions/plan-approvals.js +77 -0
- package/dist/orchestration/interactions/plan-handler.d.ts +6 -0
- package/dist/orchestration/interactions/plan-handler.js +12 -0
- package/dist/orchestration/lifecycle.d.ts +50 -0
- package/dist/orchestration/lifecycle.js +308 -0
- package/dist/orchestration/orchestrator.d.ts +29 -0
- package/dist/orchestration/orchestrator.js +25 -0
- package/dist/orchestration/routing/commands/cancel.d.ts +10 -0
- package/dist/orchestration/routing/commands/cancel.js +136 -0
- package/dist/orchestration/routing/commands/channel.d.ts +11 -0
- package/dist/orchestration/routing/commands/channel.js +219 -0
- package/dist/orchestration/routing/commands/command-context.d.ts +29 -0
- package/dist/orchestration/routing/commands/command-context.js +4 -0
- package/dist/orchestration/routing/commands/cost.d.ts +3 -0
- package/dist/orchestration/routing/commands/cost.js +22 -0
- package/dist/orchestration/routing/commands/device.d.ts +6 -0
- package/dist/orchestration/routing/commands/device.js +83 -0
- package/dist/orchestration/routing/commands/dispatch.d.ts +2 -0
- package/dist/orchestration/routing/commands/dispatch.js +49 -0
- package/dist/orchestration/routing/commands/index.d.ts +15 -0
- package/dist/orchestration/routing/commands/index.js +130 -0
- package/dist/orchestration/routing/commands/mode.d.ts +13 -0
- package/dist/orchestration/routing/commands/mode.js +290 -0
- package/dist/orchestration/routing/commands/nvtop.d.ts +3 -0
- package/dist/orchestration/routing/commands/nvtop.js +103 -0
- package/dist/orchestration/routing/commands/orient.d.ts +2 -0
- package/dist/orchestration/routing/commands/orient.js +3 -0
- package/dist/orchestration/routing/commands/schedule.d.ts +4 -0
- package/dist/orchestration/routing/commands/schedule.js +132 -0
- package/dist/orchestration/routing/commands/sendfile.d.ts +2 -0
- package/dist/orchestration/routing/commands/sendfile.js +92 -0
- package/dist/orchestration/routing/commands/session.d.ts +9 -0
- package/dist/orchestration/routing/commands/session.js +153 -0
- package/dist/orchestration/routing/commands/status.d.ts +7 -0
- package/dist/orchestration/routing/commands/status.js +180 -0
- package/dist/orchestration/routing/commands/tail.d.ts +2 -0
- package/dist/orchestration/routing/commands/tail.js +73 -0
- package/dist/orchestration/routing/commands/task.d.ts +6 -0
- package/dist/orchestration/routing/commands/task.js +142 -0
- package/dist/orchestration/routing/commands/thread-handlers.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread-handlers.js +138 -0
- package/dist/orchestration/routing/commands/thread.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread.js +2 -0
- package/dist/orchestration/routing/edit-handler.d.ts +24 -0
- package/dist/orchestration/routing/edit-handler.js +161 -0
- package/dist/orchestration/routing/file-handler.d.ts +6 -0
- package/dist/orchestration/routing/file-handler.js +23 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.d.ts +4 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.js +79 -0
- package/dist/orchestration/routing/hook-bridge.d.ts +30 -0
- package/dist/orchestration/routing/hook-bridge.js +112 -0
- package/dist/orchestration/routing/message-router.d.ts +6 -0
- package/dist/orchestration/routing/message-router.js +70 -0
- package/dist/orchestration/routing/webhook.d.ts +10 -0
- package/dist/orchestration/routing/webhook.js +239 -0
- package/dist/orchestration/status-helpers.d.ts +66 -0
- package/dist/orchestration/status-helpers.js +222 -0
- package/dist/orchestration/superseded-edits.d.ts +22 -0
- package/dist/orchestration/superseded-edits.js +31 -0
- package/dist/orchestration/thread-executor.d.ts +31 -0
- package/dist/orchestration/thread-executor.js +234 -0
- package/dist/platform/adapter.d.ts +28 -0
- package/dist/platform/adapter.js +5 -0
- package/dist/platform/adapters/feishu.d.ts +66 -0
- package/dist/platform/adapters/feishu.js +558 -0
- package/dist/platform/adapters/index.d.ts +16 -0
- package/dist/platform/adapters/index.js +65 -0
- package/dist/platform/adapters/slack.d.ts +90 -0
- package/dist/platform/adapters/slack.js +630 -0
- package/dist/platform/index.d.ts +12 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/interactive-builder.d.ts +27 -0
- package/dist/platform/interactive-builder.js +120 -0
- package/dist/platform/testing.d.ts +100 -0
- package/dist/platform/testing.js +190 -0
- package/dist/platform/tool-trace.d.ts +33 -0
- package/dist/platform/tool-trace.js +165 -0
- package/dist/platform/types.d.ts +176 -0
- package/dist/platform/types.js +5 -0
- package/dist/platform/utils/rate-limiter.d.ts +92 -0
- package/dist/platform/utils/rate-limiter.js +213 -0
- package/dist/platform/virtual-message.d.ts +68 -0
- package/dist/platform/virtual-message.js +464 -0
- package/dist/store/atomic-write.d.ts +6 -0
- package/dist/store/atomic-write.js +18 -0
- package/dist/store/channel-repo.d.ts +21 -0
- package/dist/store/channel-repo.js +55 -0
- package/dist/store/conversation-ledger-repo.d.ts +86 -0
- package/dist/store/conversation-ledger-repo.js +220 -0
- package/dist/store/cost-repo.d.ts +57 -0
- package/dist/store/cost-repo.js +135 -0
- package/dist/store/execution-repo.d.ts +172 -0
- package/dist/store/execution-repo.js +343 -0
- package/dist/store/in-memory-repository.d.ts +14 -0
- package/dist/store/in-memory-repository.js +31 -0
- package/dist/store/json-repository.d.ts +51 -0
- package/dist/store/json-repository.js +118 -0
- package/dist/store/outbound-queue.d.ts +65 -0
- package/dist/store/outbound-queue.js +269 -0
- package/dist/store/profile-repo.d.ts +36 -0
- package/dist/store/profile-repo.js +144 -0
- package/dist/store/project-dir-repo.d.ts +22 -0
- package/dist/store/project-dir-repo.js +60 -0
- package/dist/store/schedule-repo.d.ts +92 -0
- package/dist/store/schedule-repo.js +88 -0
- package/dist/store/session-registry-repo.d.ts +38 -0
- package/dist/store/session-registry-repo.js +96 -0
- package/dist/store/session-repo.d.ts +14 -0
- package/dist/store/session-repo.js +56 -0
- package/dist/store/task-repo.d.ts +41 -0
- package/dist/store/task-repo.js +137 -0
- package/dist/store/thread-repo.d.ts +50 -0
- package/dist/store/thread-repo.js +190 -0
- package/package.json +58 -0
- package/scripts/postinstall-restart-trigger.mjs +39 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// input: mode-manager, agent-lifecycle, orch/channel-queue, orch/busy-tracker, orch/active-agents
|
|
2
|
+
// output: AgentRunner — wraps executeDefaultAgent + launchDefaultAgent lifecycle [S8]
|
|
3
|
+
// pos: orch/ — sole default-agent execution path
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { channelQueues, enqueue } from './channel-queue.js';
|
|
6
|
+
import { trackPendingTask } from './busy-tracker.js';
|
|
7
|
+
import { getSessionAsync } from '../domain/sessions/session.js';
|
|
8
|
+
import { sessionRegistryRepo } from '../store/session-registry-repo.js';
|
|
9
|
+
import { conversationLedger } from '../store/conversation-ledger-repo.js';
|
|
10
|
+
import { getActiveProfile, getDefaultAgent, resolveBackendForChannel } from '../domain/agents/index.js';
|
|
11
|
+
import { handleAgentSuccess, handleAgentError, initTurnTracking } from './lifecycle.js';
|
|
12
|
+
import { buildSessionTag, buildUserProcessingMessage, makeFallbackNotifier, makeStreamingMessageCallback, computeElapsed, writeStatus, sealStatus, buildStatusActionBlocks, buildSealedStatusActionBlocks, initStatusBlocks } from './status-helpers.js';
|
|
13
|
+
import { readFileSync } from 'fs';
|
|
14
|
+
import { createLogger } from '../core/log.js';
|
|
15
|
+
import { getOutboundQueue } from '../store/outbound-queue.js';
|
|
16
|
+
import { buildDurableHooks } from './durable-helpers.js';
|
|
17
|
+
const log = createLogger('agent-runner');
|
|
18
|
+
import { createToolTrace } from '../platform/index.js';
|
|
19
|
+
import { setStreamingCallback, clearStreamingCallback, publishPlanSubmitted, publishAskUserRequested } from './routing/hook-bridge.js';
|
|
20
|
+
import { maybeNotifyCodexLowUsage } from '../domain/costs/codex-usage-monitor.js';
|
|
21
|
+
import { getAgent, createDefaultThread } from '../domain/threads/index.js';
|
|
22
|
+
import { runThread } from '../domain/threads/runner.js';
|
|
23
|
+
import { downloadFiles as downloadPlatformFiles } from './routing/file-handler.js';
|
|
24
|
+
import { WORKSPACE_DIR } from '../core/utils.js';
|
|
25
|
+
const TEMP_DIR = WORKSPACE_DIR;
|
|
26
|
+
export class AgentRunner {
|
|
27
|
+
_enqueue;
|
|
28
|
+
_track;
|
|
29
|
+
/** Injectable for unit tests — allows verification of track(-1)-in-finally without spawning Claude. */
|
|
30
|
+
_execute;
|
|
31
|
+
constructor(opts = {}) {
|
|
32
|
+
this._enqueue = opts.enqueue ?? enqueue;
|
|
33
|
+
this._track = opts.track ?? trackPendingTask;
|
|
34
|
+
this._execute = opts.execute ?? ((ctx) => this._executeReal(ctx));
|
|
35
|
+
}
|
|
36
|
+
async route(ctx) {
|
|
37
|
+
const { message, channel, adapter } = ctx;
|
|
38
|
+
if (channelQueues.has(channel)) {
|
|
39
|
+
await adapter.addReaction({ channel, messageId: message.ref.messageId }, 'hourglass').catch(() => { });
|
|
40
|
+
}
|
|
41
|
+
this._track(+1);
|
|
42
|
+
this._enqueue(channel, async () => {
|
|
43
|
+
try {
|
|
44
|
+
await this._execute(ctx);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
this._track(-1);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async _executeReal(ctx) {
|
|
52
|
+
const { message, channel, adapter, threadTs, hasFiles, userMessage, agentMessage } = ctx;
|
|
53
|
+
const downloadedFiles = await downloadFiles(message.files, hasFiles, adapter);
|
|
54
|
+
const startTime = Date.now();
|
|
55
|
+
const sessionId = await getSessionAsync(channel, resolveBackendForChannel(channel));
|
|
56
|
+
const sessionName = await resolveSessionName(sessionId, channel, userMessage);
|
|
57
|
+
// 1. Orchestration side effects (keep — ledger, status, hook-bridge)
|
|
58
|
+
const statusText = buildUserProcessingMessage({ startTime, profileName: getActiveProfile(channel), sessionName, sessionId });
|
|
59
|
+
const blocksTemplate = { channel, sessionName, isDm: true };
|
|
60
|
+
// Post status message WITHOUT Cancel button initially — Cancel is added after
|
|
61
|
+
// createDefaultThread gives us a threadId (A3: execution-scoped Cancel).
|
|
62
|
+
const statusMsg = await adapter.postMessage(channel, {
|
|
63
|
+
text: statusText,
|
|
64
|
+
richBlocks: buildSealedStatusActionBlocks(statusText, blocksTemplate),
|
|
65
|
+
}, threadTs ? { threadId: threadTs } : undefined);
|
|
66
|
+
const messageTs = message.ref.messageId;
|
|
67
|
+
await initTurnTracking(channel, sessionId, sessionName, messageTs, userMessage || '', statusMsg.messageId);
|
|
68
|
+
const onMessagePosted = (ref) => void conversationLedger.addResponseTs(channel, messageTs, ref.messageId).catch((e) => log.error(e));
|
|
69
|
+
// 2. Resolve agent config and create default thread (BEFORE execution, replaces createAutoThread)
|
|
70
|
+
const agentConfig = resolveDefaultAgent(agentMessage, channel);
|
|
71
|
+
const thread = createDefaultThread(channel, {
|
|
72
|
+
agentName: agentConfig.defaultAgentName || 'main',
|
|
73
|
+
userMessage: agentMessage,
|
|
74
|
+
userMessageTs: messageTs,
|
|
75
|
+
platformThreadId: threadTs || statusMsg.messageId,
|
|
76
|
+
});
|
|
77
|
+
// 3. Update status message with Cancel button now that we have thread.id
|
|
78
|
+
const blocksTemplateWithThread = { ...blocksTemplate, threadId: thread.id };
|
|
79
|
+
await adapter.updateMessage(statusMsg, {
|
|
80
|
+
text: statusText,
|
|
81
|
+
richBlocks: buildStatusActionBlocks(statusText, blocksTemplateWithThread),
|
|
82
|
+
}).catch(() => { });
|
|
83
|
+
initStatusBlocks(statusMsg, blocksTemplateWithThread);
|
|
84
|
+
// 4. Build agent callbacks (streaming, fallback, progress — passed to runThread)
|
|
85
|
+
const callbacks = buildAgentCallbacks(adapter, channel, statusMsg, threadTs, startTime, sessionName, sessionId, onMessagePosted);
|
|
86
|
+
// 5. Build PI interactive-event callbacks (plan approval / ask-user-question routing)
|
|
87
|
+
const interactiveCallbacks = buildInteractiveCallbacks(channel, sessionId, thread.id);
|
|
88
|
+
// 6. Call runThread instead of runAgent
|
|
89
|
+
let result;
|
|
90
|
+
try {
|
|
91
|
+
const threadResult = await runThread(thread.id, {
|
|
92
|
+
adapter, channel,
|
|
93
|
+
threadTs: threadTs || statusMsg.messageId,
|
|
94
|
+
statusMsg, startTime,
|
|
95
|
+
existingSessionId: sessionId,
|
|
96
|
+
sessionName,
|
|
97
|
+
files: downloadedFiles,
|
|
98
|
+
onAssistantMessage: callbacks.onAssistantMsg,
|
|
99
|
+
onProgress: callbacks.onProgress,
|
|
100
|
+
onFallback: callbacks.onFallback,
|
|
101
|
+
onToolUse: composeToolUse(callbacks.onToolUse, interactiveCallbacks.onToolUse),
|
|
102
|
+
onPlanWritten: interactiveCallbacks.onPlanWritten,
|
|
103
|
+
onAskUserQuestion: interactiveCallbacks.onAskUserQuestion,
|
|
104
|
+
isUserInitiated: true,
|
|
105
|
+
});
|
|
106
|
+
result = threadResult.lastAgentResult;
|
|
107
|
+
clearStreamingCallback(channel);
|
|
108
|
+
await maybeNotifyCodexLowUsage({ adapter, channel, result });
|
|
109
|
+
await handleDefaultAgentResult({
|
|
110
|
+
result, channel, adapter, statusMsg, startTime, userMessage,
|
|
111
|
+
executionId: threadResult.executionId,
|
|
112
|
+
sessionName, sessionId, threadTs, messageTs, callbacks,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
clearStreamingCallback(channel);
|
|
117
|
+
await handleAgentError({
|
|
118
|
+
error: error,
|
|
119
|
+
channel, adapter, statusMsg, startTime,
|
|
120
|
+
executionId: null,
|
|
121
|
+
sessionName, sessionId, threadTs, userMessageTs: messageTs,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export const agentRunner = new AgentRunner();
|
|
127
|
+
// --- Helpers ---
|
|
128
|
+
/** Exposed for unit testing. */
|
|
129
|
+
export function resolveDefaultAgent(agentMessage, channel) {
|
|
130
|
+
const defaultAgentName = getDefaultAgent();
|
|
131
|
+
const defaultAgentDef = defaultAgentName ? getAgent(defaultAgentName) : null;
|
|
132
|
+
const profileForRun = (defaultAgentDef && defaultAgentDef.profile !== '__active__')
|
|
133
|
+
? defaultAgentDef.profile
|
|
134
|
+
: getActiveProfile(channel);
|
|
135
|
+
let effectiveMessage = agentMessage;
|
|
136
|
+
if (defaultAgentDef?.directive) {
|
|
137
|
+
effectiveMessage = defaultAgentDef.directive + '\n\n' + agentMessage;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
effectiveMessage, profileForRun, defaultAgentName,
|
|
141
|
+
claudeAgent: defaultAgentDef?.claudeAgent || null,
|
|
142
|
+
systemPrompt: defaultAgentDef?.systemPrompt || null,
|
|
143
|
+
outputStyle: defaultAgentDef?.outputStyle || null,
|
|
144
|
+
tools: defaultAgentDef?.tools || null,
|
|
145
|
+
pluginDirs: defaultAgentDef?.pluginDirs || null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function handleDefaultAgentResult({ result, channel, adapter, statusMsg, startTime, userMessage, executionId, sessionName, sessionId, threadTs, messageTs, callbacks }) {
|
|
149
|
+
if (result?.rateLimited) {
|
|
150
|
+
const { elapsedStr } = computeElapsed(startTime);
|
|
151
|
+
const fallbackText = `:warning: ${buildSessionTag(sessionName, sessionId)}Rate limited — all fallbacks exhausted (${elapsedStr}s)`;
|
|
152
|
+
await sealStatus(adapter, statusMsg, fallbackText, buildSealedStatusActionBlocks(fallbackText, { channel, sessionName, isDm: true }));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
await handleAgentSuccess({ result, channel, adapter, statusMsg, startTime, userMessage, executionId, trigger: 'user', sessionName, threadTs, userMessageTs: messageTs, onAssistantMessage: callbacks.onAssistantMsg });
|
|
156
|
+
// NOTE: createAutoThread removed — thread was created pre-execution via createDefaultThread
|
|
157
|
+
}
|
|
158
|
+
async function resolveSessionName(sessionId, channel, userMessage) {
|
|
159
|
+
if (sessionId) {
|
|
160
|
+
const existing = await sessionRegistryRepo.lookupBySessionId(sessionId);
|
|
161
|
+
if (existing)
|
|
162
|
+
return existing;
|
|
163
|
+
const name = await sessionRegistryRepo.generateSessionName();
|
|
164
|
+
await sessionRegistryRepo.registerSession(name, { sessionId, channel, backend: resolveBackendForChannel(channel), kind: 'local', label: userMessage?.substring(0, 60), profileName: getActiveProfile(channel) });
|
|
165
|
+
return name;
|
|
166
|
+
}
|
|
167
|
+
return sessionRegistryRepo.generateSessionName();
|
|
168
|
+
}
|
|
169
|
+
function buildAgentCallbacks(adapter, channel, statusMsg, threadTs, startTime, sessionName, sessionId, onMessagePosted) {
|
|
170
|
+
const onFallback = makeFallbackNotifier(channel, statusMsg, adapter);
|
|
171
|
+
const queue = getOutboundQueue();
|
|
172
|
+
const durable = queue ? buildDurableHooks(queue) : null;
|
|
173
|
+
const baseAssistantMsg = makeStreamingMessageCallback(adapter, channel, threadTs, onMessagePosted, durable);
|
|
174
|
+
// Tool trace: when CORTEX_SHOW_TOOL_CALLS is enabled, emit a compact per-tool Slack line
|
|
175
|
+
// that merges consecutive same-tool calls and splits on different tool / assistant text.
|
|
176
|
+
const toolTrace = createToolTrace(baseAssistantMsg.vm);
|
|
177
|
+
const onAssistantMsg = toolTrace
|
|
178
|
+
? Object.assign((text) => { toolTrace.flush(); baseAssistantMsg(text); }, { vm: baseAssistantMsg.vm })
|
|
179
|
+
: baseAssistantMsg;
|
|
180
|
+
const onToolUse = toolTrace ? (name, input) => toolTrace.onToolUse(name, input) : null;
|
|
181
|
+
setStreamingCallback(channel, onAssistantMsg);
|
|
182
|
+
const onProgress = (progress) => {
|
|
183
|
+
writeStatus(adapter, statusMsg, buildUserProcessingMessage({
|
|
184
|
+
startTime,
|
|
185
|
+
elapsed_s: progress?.duration_ms != null ? progress.duration_ms / 1000 : null,
|
|
186
|
+
num_turns: progress?.num_turns ?? null,
|
|
187
|
+
profileName: getActiveProfile(channel), sessionName, sessionId,
|
|
188
|
+
}));
|
|
189
|
+
};
|
|
190
|
+
return { onFallback, onAssistantMsg, onProgress, onToolUse };
|
|
191
|
+
}
|
|
192
|
+
async function downloadFiles(files, hasFiles, adapter) {
|
|
193
|
+
if (!hasFiles || !files)
|
|
194
|
+
return [];
|
|
195
|
+
return downloadPlatformFiles(files, adapter, TEMP_DIR);
|
|
196
|
+
}
|
|
197
|
+
/** Compose two optional onToolUse callbacks so both fire on each tool_use event. */
|
|
198
|
+
function composeToolUse(a, b) {
|
|
199
|
+
if (!a && !b)
|
|
200
|
+
return null;
|
|
201
|
+
if (!a)
|
|
202
|
+
return b;
|
|
203
|
+
if (!b)
|
|
204
|
+
return a;
|
|
205
|
+
return (name, input) => { a(name, input); b(name, input); };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Build interactive callbacks for plan_written, ask_user_question, and tool_use events.
|
|
209
|
+
* These fire during the turn (not after) and publish bus events so the existing
|
|
210
|
+
* Slack interaction flow handles them.
|
|
211
|
+
*
|
|
212
|
+
* Exported so that all runThread call sites (thread-executor, scheduled-task,
|
|
213
|
+
* task-dispatch) can wire these callbacks — without them, ask_user_question
|
|
214
|
+
* events are silently dropped and the subprocess blocks forever.
|
|
215
|
+
*
|
|
216
|
+
* ORDERING INVARIANT: onToolUse fires synchronously before onAskUserQuestion
|
|
217
|
+
* within the same event-loop tick (guaranteed by the PI adapter's sequential
|
|
218
|
+
* event processing in event-parser.ts). The closure variables pendingAskInput
|
|
219
|
+
* and pendingExitPlanMode rely on this ordering — onToolUse captures state
|
|
220
|
+
* that onAskUserQuestion consumes. If the adapter ever processes events
|
|
221
|
+
* asynchronously or out of order, this contract breaks silently.
|
|
222
|
+
*/
|
|
223
|
+
export function buildInteractiveCallbacks(channel, sessionId, threadId = null) {
|
|
224
|
+
let pendingPlanContent = null;
|
|
225
|
+
let pendingPlanPath = null;
|
|
226
|
+
// Captured full tool input from tool_use event — used by onAskUserQuestion
|
|
227
|
+
// so the Slack message gets the full Claude-style schema (header, options
|
|
228
|
+
// with descriptions, multiSelect) instead of the degraded extension_ui_request data.
|
|
229
|
+
let pendingAskInput = null;
|
|
230
|
+
const onPlanWritten = (event) => {
|
|
231
|
+
pendingPlanContent = event.content;
|
|
232
|
+
pendingPlanPath = event.path;
|
|
233
|
+
};
|
|
234
|
+
// Track pending exit_plan_mode so the subsequent confirm() ask_user_question
|
|
235
|
+
// is correctly routed as plan approval even when plan_written didn't fire
|
|
236
|
+
// (happens when the LLM skips writing to a known plan directory).
|
|
237
|
+
let pendingExitPlanMode = null;
|
|
238
|
+
// Capture full tool input when the LLM calls ask_user_question (before the
|
|
239
|
+
// tool shim calls ctx.ui.input which only emits a degraded extension_ui_request).
|
|
240
|
+
const onToolUse = (name, input) => {
|
|
241
|
+
if (name === 'AskUserQuestion' || name === 'ask_user_question') {
|
|
242
|
+
pendingAskInput = input;
|
|
243
|
+
}
|
|
244
|
+
if (name === 'ExitPlanMode' || name === 'exit_plan_mode') {
|
|
245
|
+
pendingExitPlanMode = input || {};
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const onAskUserQuestion = (event) => {
|
|
249
|
+
const requestId = crypto.randomUUID();
|
|
250
|
+
if (pendingPlanContent !== null || pendingExitPlanMode !== null) {
|
|
251
|
+
// This ask_user_question is the confirm() from exit_plan_mode → treat as plan approval.
|
|
252
|
+
// pendingPlanContent comes from plan_written event (Write to a plan directory);
|
|
253
|
+
// pendingExitPlanMode comes from tool_use event (LLM called exit_plan_mode directly).
|
|
254
|
+
let planContent = pendingPlanContent ?? pendingExitPlanMode?.plan ?? '';
|
|
255
|
+
// When a plan file was written, read it for full content (args.plan is often just a summary).
|
|
256
|
+
if (pendingPlanPath && planContent.length < 200) {
|
|
257
|
+
try {
|
|
258
|
+
const fc = readFileSync(pendingPlanPath, 'utf8');
|
|
259
|
+
if (fc.trim())
|
|
260
|
+
planContent = fc;
|
|
261
|
+
}
|
|
262
|
+
catch { }
|
|
263
|
+
}
|
|
264
|
+
publishPlanSubmitted(requestId, channel, sessionId ?? '', planContent, event.toolUseId, threadId);
|
|
265
|
+
pendingPlanContent = null;
|
|
266
|
+
pendingPlanPath = null;
|
|
267
|
+
pendingExitPlanMode = null;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
// Use captured tool input (Claude-style schema) if available; fall back
|
|
271
|
+
// to the degraded extension_ui_request data for backward compatibility.
|
|
272
|
+
let questions;
|
|
273
|
+
if (pendingAskInput?.questions?.length) {
|
|
274
|
+
questions = pendingAskInput.questions;
|
|
275
|
+
pendingAskInput = null;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
questions = event.questions.map((q) => ({
|
|
279
|
+
question: q.question,
|
|
280
|
+
options: q.options?.map((o) => ({ label: o, description: '' })) ?? [],
|
|
281
|
+
multiSelect: false,
|
|
282
|
+
header: q.question.substring(0, 12),
|
|
283
|
+
}));
|
|
284
|
+
}
|
|
285
|
+
publishAskUserRequested(requestId, channel, sessionId ?? '', questions, event.toolUseId, threadId);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
return { onPlanWritten, onAskUserQuestion, onToolUse };
|
|
289
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EventBus } from '../events/index.js';
|
|
2
|
+
export declare class BusyTracker {
|
|
3
|
+
private _count;
|
|
4
|
+
private _bus;
|
|
5
|
+
setBus(bus: EventBus): void;
|
|
6
|
+
trackPendingTask(delta: number): void;
|
|
7
|
+
/** Exposed for testing: current activeLlmCount value. */
|
|
8
|
+
get count(): number;
|
|
9
|
+
}
|
|
10
|
+
export declare const busyTracker: BusyTracker;
|
|
11
|
+
/** Backward-compat named export: call sites change only the import path, not the call. */
|
|
12
|
+
export declare function trackPendingTask(delta: number): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// input: EventBus, delta: number
|
|
2
|
+
// output: busyTracker singleton — activeLlmCount tracking, llm.active-count-delta publish, IPC busy/idle
|
|
3
|
+
// pos: orch/ layer — LLM busy/idle IPC signaling [S6-C]
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
export class BusyTracker {
|
|
6
|
+
_count = 0;
|
|
7
|
+
_bus = null;
|
|
8
|
+
setBus(bus) {
|
|
9
|
+
this._bus = bus;
|
|
10
|
+
// Subscriber-as-source-of-truth: _count is updated by the subscriber itself, so
|
|
11
|
+
// any publisher (including non-tracker bus.publish callers) is correctly counted.
|
|
12
|
+
// Transition detection: 0→1 sends IPC busy, 1→0 sends IPC idle (S13 NTH-A fix).
|
|
13
|
+
bus.subscribe('llm.active-count-delta', (e) => {
|
|
14
|
+
this._count += e.delta;
|
|
15
|
+
if (!process.send)
|
|
16
|
+
return;
|
|
17
|
+
if (this._count === 1 && e.delta > 0)
|
|
18
|
+
process.send({ type: 'busy' });
|
|
19
|
+
if (this._count === 0 && e.delta < 0)
|
|
20
|
+
process.send({ type: 'idle' });
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
trackPendingTask(delta) {
|
|
24
|
+
this._bus?.publish({ type: 'llm.active-count-delta', delta });
|
|
25
|
+
}
|
|
26
|
+
/** Exposed for testing: current activeLlmCount value. */
|
|
27
|
+
get count() {
|
|
28
|
+
return this._count;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export const busyTracker = new BusyTracker();
|
|
32
|
+
/** Backward-compat named export: call sites change only the import path, not the call. */
|
|
33
|
+
export function trackPendingTask(delta) {
|
|
34
|
+
busyTracker.trackPendingTask(delta);
|
|
35
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-channel serial Promise queue. Each channel has at most one tail entry.
|
|
3
|
+
* Consumers may call .has(channel) to check whether a queue is already running
|
|
4
|
+
* (e.g. to show a hourglass reaction), and .delete(channel) to discard a
|
|
5
|
+
* pending tail (e.g. !cancel).
|
|
6
|
+
*/
|
|
7
|
+
export declare const channelQueues: Map<string, Promise<void>>;
|
|
8
|
+
/**
|
|
9
|
+
* Append fn to the channel's serial queue.
|
|
10
|
+
*
|
|
11
|
+
* Returns true if a queue was already running for this channel when enqueue()
|
|
12
|
+
* was called (useful for showing backpressure indicators). Returns false if
|
|
13
|
+
* this is the first entry for the channel.
|
|
14
|
+
*
|
|
15
|
+
* The queue entry is automatically removed once fn resolves or rejects, so
|
|
16
|
+
* the Map never accumulates stale entries for completed channels.
|
|
17
|
+
*/
|
|
18
|
+
export declare function enqueue(channel: string, fn: () => Promise<void>): boolean;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// input: nothing (leaf module)
|
|
2
|
+
// output: channelQueues Map + enqueue() — per-channel serial work queue
|
|
3
|
+
// pos: orch/ layer [S6-B]
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
/**
|
|
6
|
+
* Per-channel serial Promise queue. Each channel has at most one tail entry.
|
|
7
|
+
* Consumers may call .has(channel) to check whether a queue is already running
|
|
8
|
+
* (e.g. to show a hourglass reaction), and .delete(channel) to discard a
|
|
9
|
+
* pending tail (e.g. !cancel).
|
|
10
|
+
*/
|
|
11
|
+
export const channelQueues = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* Append fn to the channel's serial queue.
|
|
14
|
+
*
|
|
15
|
+
* Returns true if a queue was already running for this channel when enqueue()
|
|
16
|
+
* was called (useful for showing backpressure indicators). Returns false if
|
|
17
|
+
* this is the first entry for the channel.
|
|
18
|
+
*
|
|
19
|
+
* The queue entry is automatically removed once fn resolves or rejects, so
|
|
20
|
+
* the Map never accumulates stale entries for completed channels.
|
|
21
|
+
*/
|
|
22
|
+
export function enqueue(channel, fn) {
|
|
23
|
+
const hadExisting = channelQueues.has(channel);
|
|
24
|
+
const prev = channelQueues.get(channel) || Promise.resolve();
|
|
25
|
+
const next = prev.then(fn, fn);
|
|
26
|
+
channelQueues.set(channel, next);
|
|
27
|
+
next.finally(() => {
|
|
28
|
+
if (channelQueues.get(channel) === next)
|
|
29
|
+
channelQueues.delete(channel);
|
|
30
|
+
});
|
|
31
|
+
return hadExisting;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function startDispatchReconciler(): void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// input: executionRegistry, pendingTaskTracker
|
|
2
|
+
// output: startDispatchReconciler() — background interval for stale dispatch cleanup
|
|
3
|
+
// pos: orch/ — extracted from entry/app.ts setInterval (S13 composition-root extraction)
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import * as executionRegistry from '../domain/executions/registry.js';
|
|
6
|
+
import * as pendingTaskTracker from '../domain/tasks/pending-tracker.js';
|
|
7
|
+
const RECONCILE_INTERVAL_MS = 15 * 60 * 1000;
|
|
8
|
+
const DISPATCH_STALE_AGE_MS = 3 * 60 * 60 * 1000;
|
|
9
|
+
export function startDispatchReconciler() {
|
|
10
|
+
setInterval(() => {
|
|
11
|
+
executionRegistry.reconcileStaleDispatches({
|
|
12
|
+
isTaskPending: (taskId) => pendingTaskTracker.getTask(taskId) !== null,
|
|
13
|
+
maxAgeMs: DISPATCH_STALE_AGE_MS,
|
|
14
|
+
});
|
|
15
|
+
}, RECONCILE_INTERVAL_MS);
|
|
16
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DurableHooks } from '../platform/types.js';
|
|
2
|
+
import type { OutboundQueue } from '../store/outbound-queue.js';
|
|
3
|
+
export { durablePost, durableUpdate } from '../store/outbound-queue.js';
|
|
4
|
+
export declare function buildDurableHooks(queue: OutboundQueue): DurableHooks;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// input: OutboundQueue (store), DurableHooks (platform/types)
|
|
2
|
+
// output: buildDurableHooks() + re-exports of durablePost/durableUpdate
|
|
3
|
+
// pos: orch layer bridge — connects store/outbound-queue and platform/types.DurableHooks
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
export { durablePost, durableUpdate } from '../store/outbound-queue.js';
|
|
6
|
+
export function buildDurableHooks(queue) {
|
|
7
|
+
return {
|
|
8
|
+
async beforePost(channel, text, opts) {
|
|
9
|
+
const walId = await queue.enqueue({
|
|
10
|
+
type: 'post',
|
|
11
|
+
channel,
|
|
12
|
+
text,
|
|
13
|
+
threadId: opts?.threadId,
|
|
14
|
+
richBlocks: opts?.richBlocks,
|
|
15
|
+
});
|
|
16
|
+
queue.claim(walId);
|
|
17
|
+
return walId;
|
|
18
|
+
},
|
|
19
|
+
async beforeUpdate(channel, messageId, text, opts) {
|
|
20
|
+
const walId = await queue.enqueue({
|
|
21
|
+
type: 'update',
|
|
22
|
+
channel,
|
|
23
|
+
messageId,
|
|
24
|
+
text,
|
|
25
|
+
richBlocks: opts?.richBlocks,
|
|
26
|
+
});
|
|
27
|
+
queue.claim(walId);
|
|
28
|
+
return walId;
|
|
29
|
+
},
|
|
30
|
+
async afterSent(walId, slackTs) {
|
|
31
|
+
try {
|
|
32
|
+
await queue.markSent(walId, slackTs);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
queue.release(walId);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
onSendFailed(walId) {
|
|
39
|
+
queue.release(walId);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PlatformAdapter } from '../../platform/index.js';
|
|
2
|
+
import type { VirtualMessage } from '../../platform/index.js';
|
|
3
|
+
import { buildQuestionGroupBlocks, buildQuestionModalDefinition } from '../../platform/index.js';
|
|
4
|
+
declare function formatGroupResponse(group: any): string;
|
|
5
|
+
declare function isExpired(record: any): boolean;
|
|
6
|
+
declare function getGroup(groupId: any): any;
|
|
7
|
+
declare function deleteGroup(groupId: any): void;
|
|
8
|
+
declare function getPendingQuestion(pendingId: any): any;
|
|
9
|
+
/** Create a question group from a PreToolUse hook request (not from Claude output).
|
|
10
|
+
* @param extensionUiId — original PI extension_ui_request id; when set, tryResolveHook
|
|
11
|
+
* uses this for sendExtensionUiResponse instead of the hookRequestId. */
|
|
12
|
+
declare function createHookGroup(requestId: any, channel: any, sessionId: any, questions: any, extensionUiId?: string, threadId?: string | null): {
|
|
13
|
+
groupId: string;
|
|
14
|
+
sessionId: any;
|
|
15
|
+
toolUseId: any;
|
|
16
|
+
channel: any;
|
|
17
|
+
messageTs: any;
|
|
18
|
+
responseMessageTs: any;
|
|
19
|
+
hookRequestId: any;
|
|
20
|
+
extensionUiId: string;
|
|
21
|
+
threadId: string;
|
|
22
|
+
questions: any;
|
|
23
|
+
answers: Map<any, any>;
|
|
24
|
+
createdAt: number;
|
|
25
|
+
};
|
|
26
|
+
/** Register a callback to be called when all hook-mode answers are collected. */
|
|
27
|
+
declare function registerHookResolver(requestId: any, resolver: any): void;
|
|
28
|
+
/** If the group is in hook mode and all answers collected, resolve the hook and return true.
|
|
29
|
+
* For PI backend: if a running execution with an agentProcess exists, send extension_ui_response directly. */
|
|
30
|
+
declare function tryResolveHook(group: any): boolean;
|
|
31
|
+
declare function sendMessages(result: any, channel: any, adapter: PlatformAdapter, messageTs: any, threadTs?: any, vm?: VirtualMessage | null): Promise<number>;
|
|
32
|
+
export { sendMessages, formatGroupResponse, buildQuestionGroupBlocks, buildQuestionModalDefinition, isExpired, getGroup, deleteGroup, getPendingQuestion, createHookGroup, registerHookResolver, tryResolveHook, };
|