@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,269 @@
|
|
|
1
|
+
// input: PlatformAdapter, AsyncMutex, fs
|
|
2
|
+
// output: OutboundQueue — WAL-based durable outbound message queue
|
|
3
|
+
// pos: Store layer. Ensures critical messages (streaming text, final status, errors) are not lost on restart.
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import fs from 'node:fs/promises';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
import { createLogger } from '../core/log.js';
|
|
9
|
+
import { AsyncMutex } from '../core/async-mutex.js';
|
|
10
|
+
import { atomicWrite } from './atomic-write.js';
|
|
11
|
+
const log = createLogger('outbound-queue');
|
|
12
|
+
import { STORE_DIR } from '../core/paths.js';
|
|
13
|
+
const DEFAULT_TTL_MS = 30 * 60 * 1000; // 30 minutes
|
|
14
|
+
const COMPACT_THRESHOLD = 200;
|
|
15
|
+
export class OutboundQueue {
|
|
16
|
+
walPath;
|
|
17
|
+
adapter;
|
|
18
|
+
ttlMs;
|
|
19
|
+
mutex = new AsyncMutex();
|
|
20
|
+
pending = new Map();
|
|
21
|
+
sentIds = new Set();
|
|
22
|
+
/**
|
|
23
|
+
* IDs currently being processed by the inline send path (VirtualMessage /
|
|
24
|
+
* durablePost). drain() skips these to prevent double-sends. Entries are
|
|
25
|
+
* claimed by buildDurableHooks.beforePost/beforeUpdate and released by
|
|
26
|
+
* afterSent (on success) or onSendFailed (on permanent failure).
|
|
27
|
+
* On restart, inFlight is empty — unfinished entries fall back to drain().
|
|
28
|
+
*/
|
|
29
|
+
inFlight = new Set();
|
|
30
|
+
opCount = 0;
|
|
31
|
+
drainTimer = null;
|
|
32
|
+
stopped = false;
|
|
33
|
+
constructor(opts) {
|
|
34
|
+
this.walPath = opts.walPath ?? path.join(STORE_DIR, 'outbound-wal.jsonl');
|
|
35
|
+
this.adapter = opts.adapter;
|
|
36
|
+
this.ttlMs = opts.ttlMs ?? DEFAULT_TTL_MS;
|
|
37
|
+
}
|
|
38
|
+
async enqueue(input) {
|
|
39
|
+
const id = randomUUID();
|
|
40
|
+
const op = {
|
|
41
|
+
op: 'enqueue',
|
|
42
|
+
id,
|
|
43
|
+
ts: new Date().toISOString(),
|
|
44
|
+
type: input.type,
|
|
45
|
+
channel: input.channel,
|
|
46
|
+
text: input.text,
|
|
47
|
+
...(input.richBlocks && { richBlocks: input.richBlocks }),
|
|
48
|
+
...(input.threadId && { threadId: input.threadId }),
|
|
49
|
+
...(input.messageId && { messageId: input.messageId }),
|
|
50
|
+
status: 'pending',
|
|
51
|
+
};
|
|
52
|
+
await this.mutex.run(async () => {
|
|
53
|
+
await this._appendOp(op);
|
|
54
|
+
this.pending.set(id, op);
|
|
55
|
+
this.opCount++;
|
|
56
|
+
});
|
|
57
|
+
return id;
|
|
58
|
+
}
|
|
59
|
+
/** Mark an entry as "being sent by the inline path" so drain() skips it. */
|
|
60
|
+
claim(id) { this.inFlight.add(id); }
|
|
61
|
+
/** Release an in-flight claim (after success or permanent failure). */
|
|
62
|
+
release(id) { this.inFlight.delete(id); }
|
|
63
|
+
async markSent(id, slackTs) {
|
|
64
|
+
const op = {
|
|
65
|
+
op: 'sent',
|
|
66
|
+
id,
|
|
67
|
+
ts: new Date().toISOString(),
|
|
68
|
+
...(slackTs && { slackTs }),
|
|
69
|
+
};
|
|
70
|
+
await this.mutex.run(async () => {
|
|
71
|
+
// Update in-memory state FIRST to prevent drain() from re-sending.
|
|
72
|
+
// WAL persist is best-effort: on crash, recover() sees the entry as
|
|
73
|
+
// pending and drain re-sends (acceptable crash-recovery duplicate),
|
|
74
|
+
// but we avoid the far more common live-duplicate scenario.
|
|
75
|
+
this.pending.delete(id);
|
|
76
|
+
this.sentIds.add(id);
|
|
77
|
+
this.opCount++;
|
|
78
|
+
try {
|
|
79
|
+
await this._appendOp(op);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
log.warn(`WAL persist failed for markSent(${id}):`, e.message);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async recover() {
|
|
87
|
+
return this.mutex.run(async () => {
|
|
88
|
+
const ops = await this._readWAL();
|
|
89
|
+
this.pending.clear();
|
|
90
|
+
this.sentIds.clear();
|
|
91
|
+
this.opCount = ops.length;
|
|
92
|
+
for (const op of ops) {
|
|
93
|
+
if (op.op === 'enqueue') {
|
|
94
|
+
this.pending.set(op.id, op);
|
|
95
|
+
}
|
|
96
|
+
else if (op.op === 'sent') {
|
|
97
|
+
this.pending.delete(op.id);
|
|
98
|
+
this.sentIds.add(op.id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return this.pending.size;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async drain() {
|
|
105
|
+
const entries = await this.mutex.run(async () => {
|
|
106
|
+
// Skip entries currently being processed by the inline send path.
|
|
107
|
+
return [...this.pending.values()].filter(e => !this.inFlight.has(e.id));
|
|
108
|
+
});
|
|
109
|
+
if (entries.length === 0)
|
|
110
|
+
return;
|
|
111
|
+
const now = Date.now();
|
|
112
|
+
const coalesced = this._coalesce(entries);
|
|
113
|
+
for (const entry of coalesced) {
|
|
114
|
+
const age = now - new Date(entry.ts).getTime();
|
|
115
|
+
if (age > this.ttlMs) {
|
|
116
|
+
await this.markSent(entry.id);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
if (entry.type === 'post') {
|
|
121
|
+
await this.adapter.postMessage(entry.channel, {
|
|
122
|
+
text: entry.text,
|
|
123
|
+
...(entry.richBlocks && { richBlocks: entry.richBlocks }),
|
|
124
|
+
}, entry.threadId ? { threadId: entry.threadId } : undefined);
|
|
125
|
+
await this.markSent(entry.id);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
try {
|
|
129
|
+
await this.adapter.updateMessage({ channel: entry.channel, messageId: entry.messageId }, { text: entry.text, ...(entry.richBlocks && { richBlocks: entry.richBlocks }) });
|
|
130
|
+
await this.markSent(entry.id);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
await this.adapter.postMessage(entry.channel, {
|
|
134
|
+
text: entry.text,
|
|
135
|
+
...(entry.richBlocks && { richBlocks: entry.richBlocks }),
|
|
136
|
+
});
|
|
137
|
+
await this.markSent(entry.id);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
log.error(`Failed to drain entry ${entry.id}: ${e.message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (this.opCount > COMPACT_THRESHOLD) {
|
|
146
|
+
await this.compact();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async compact() {
|
|
150
|
+
await this.mutex.run(async () => {
|
|
151
|
+
const pendingOps = [...this.pending.values()];
|
|
152
|
+
const content = pendingOps.length > 0
|
|
153
|
+
? pendingOps.map(op => JSON.stringify(op)).join('\n') + '\n'
|
|
154
|
+
: '';
|
|
155
|
+
await atomicWrite(this.walPath, content);
|
|
156
|
+
this.sentIds.clear();
|
|
157
|
+
this.opCount = pendingOps.length;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
startDrainLoop(intervalMs = 5000) {
|
|
161
|
+
if (this.drainTimer)
|
|
162
|
+
return;
|
|
163
|
+
this.stopped = false;
|
|
164
|
+
this.drainTimer = setInterval(() => {
|
|
165
|
+
if (!this.stopped)
|
|
166
|
+
this.drain().catch(e => log.error(`Drain error: ${e.message}`));
|
|
167
|
+
}, intervalMs);
|
|
168
|
+
if (typeof this.drainTimer.unref === 'function')
|
|
169
|
+
this.drainTimer.unref();
|
|
170
|
+
}
|
|
171
|
+
async stop() {
|
|
172
|
+
this.stopped = true;
|
|
173
|
+
if (this.drainTimer) {
|
|
174
|
+
clearInterval(this.drainTimer);
|
|
175
|
+
this.drainTimer = null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async flush() {
|
|
179
|
+
await this.mutex.run(async () => { });
|
|
180
|
+
}
|
|
181
|
+
getPendingCount() {
|
|
182
|
+
return this.pending.size;
|
|
183
|
+
}
|
|
184
|
+
_coalesce(entries) {
|
|
185
|
+
const updateMap = new Map();
|
|
186
|
+
const result = [];
|
|
187
|
+
const superseded = new Set();
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
if (entry.type === 'update' && entry.messageId) {
|
|
190
|
+
const key = `${entry.channel}:${entry.messageId}`;
|
|
191
|
+
const prev = updateMap.get(key);
|
|
192
|
+
if (prev)
|
|
193
|
+
superseded.add(prev.id);
|
|
194
|
+
updateMap.set(key, entry);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
for (const entry of entries) {
|
|
198
|
+
if (superseded.has(entry.id)) {
|
|
199
|
+
this.markSent(entry.id).catch(() => { });
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
result.push(entry);
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
async _appendOp(op) {
|
|
207
|
+
await fs.mkdir(path.dirname(this.walPath), { recursive: true });
|
|
208
|
+
await fs.appendFile(this.walPath, JSON.stringify(op) + '\n', 'utf8');
|
|
209
|
+
}
|
|
210
|
+
async _readWAL() {
|
|
211
|
+
try {
|
|
212
|
+
const content = await fs.readFile(this.walPath, 'utf8');
|
|
213
|
+
return content.split('\n')
|
|
214
|
+
.filter(l => l.trim() !== '')
|
|
215
|
+
.map(l => JSON.parse(l));
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
if (err.code === 'ENOENT')
|
|
219
|
+
return [];
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export const outboundQueue = { instance: null };
|
|
225
|
+
export function initOutboundQueue(adapter, opts) {
|
|
226
|
+
const queue = new OutboundQueue({ adapter, ...opts });
|
|
227
|
+
outboundQueue.instance = queue;
|
|
228
|
+
return queue;
|
|
229
|
+
}
|
|
230
|
+
export function getOutboundQueue() {
|
|
231
|
+
return outboundQueue.instance;
|
|
232
|
+
}
|
|
233
|
+
export async function durablePost(queue, sender, channel, content, opts) {
|
|
234
|
+
const walId = await queue.enqueue({
|
|
235
|
+
type: 'post',
|
|
236
|
+
channel,
|
|
237
|
+
text: content.text,
|
|
238
|
+
richBlocks: content.richBlocks,
|
|
239
|
+
threadId: opts?.threadId,
|
|
240
|
+
});
|
|
241
|
+
queue.claim(walId);
|
|
242
|
+
try {
|
|
243
|
+
const ref = await sender.postMessage(channel, content, opts);
|
|
244
|
+
await queue.markSent(walId, ref.messageId);
|
|
245
|
+
return ref;
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
queue.release(walId); // Allow drain() to retry
|
|
249
|
+
throw e;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
export async function durableUpdate(queue, sender, ref, content) {
|
|
253
|
+
const walId = await queue.enqueue({
|
|
254
|
+
type: 'update',
|
|
255
|
+
channel: ref.channel,
|
|
256
|
+
messageId: ref.messageId,
|
|
257
|
+
text: content.text,
|
|
258
|
+
richBlocks: content.richBlocks,
|
|
259
|
+
});
|
|
260
|
+
queue.claim(walId);
|
|
261
|
+
try {
|
|
262
|
+
await sender.updateMessage(ref, content);
|
|
263
|
+
await queue.markSent(walId);
|
|
264
|
+
}
|
|
265
|
+
catch (e) {
|
|
266
|
+
queue.release(walId); // Allow drain() to retry
|
|
267
|
+
throw e;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ProfilesFile } from '../domain/agents/profile-manager.js';
|
|
2
|
+
export declare const PROFILES_FILE: string;
|
|
3
|
+
export declare class ProfileRepo {
|
|
4
|
+
private readonly _repo;
|
|
5
|
+
private readonly _filePath;
|
|
6
|
+
private _syncCache;
|
|
7
|
+
constructor(filePath?: string);
|
|
8
|
+
read(): Promise<ProfilesFile>;
|
|
9
|
+
/**
|
|
10
|
+
* Synchronous read for legacy sync callers (profile-manager.ts public API).
|
|
11
|
+
* First call reads from disk; subsequent calls serve from cache. save()/mutate()
|
|
12
|
+
* update the cache on success so sync readers see fresh data.
|
|
13
|
+
*/
|
|
14
|
+
readSync(): ProfilesFile;
|
|
15
|
+
save(data: ProfilesFile): Promise<void>;
|
|
16
|
+
mutate<R>(fn: (cur: ProfilesFile) => {
|
|
17
|
+
next: ProfilesFile;
|
|
18
|
+
result: R;
|
|
19
|
+
}): Promise<R>;
|
|
20
|
+
/** Drop the in-memory cache so the next read() fetches from disk. Test hook. */
|
|
21
|
+
invalidate(): void;
|
|
22
|
+
/** Wait for any in-flight mutate() to complete. For graceful SIGTERM drain. */
|
|
23
|
+
flush(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare const profileRepo: ProfileRepo;
|
|
26
|
+
export declare function setAdminNotifier(fn: (text: string) => void): void;
|
|
27
|
+
/**
|
|
28
|
+
* Watch profiles.json for external edits and hot-reload the cache.
|
|
29
|
+
* Mirrors the pattern used by startMachineRegistryWatcher() in dispatch-utils.ts.
|
|
30
|
+
*
|
|
31
|
+
* Returns a stop function — call it to tear down the watcher (e.g. in tests or SIGTERM).
|
|
32
|
+
*
|
|
33
|
+
* @param repo ProfileRepo instance to invalidate on change (defaults to singleton).
|
|
34
|
+
* @param filePath Path to watch (defaults to PROFILES_FILE).
|
|
35
|
+
*/
|
|
36
|
+
export declare function startProfileWatcher(repo?: ProfileRepo, filePath?: string): () => void;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// input: profiles.json + JsonRepository
|
|
2
|
+
// output: ProfileRepo (read / readSync / save / mutate / flush) + startProfileWatcher
|
|
3
|
+
// pos: Profile persistence layer. Based on JsonRepository abstraction (Pattern A), AsyncMutex serializes reads/writes of profiles.json.
|
|
4
|
+
// Read-heavy, write-light; profile-manager.ts historically used sync readFileSync-driven config reading. To avoid
|
|
5
|
+
// converting the sync API to async (which would cascade to mode-manager/scheduler/command-handlers/tests and dozens of other call sites),
|
|
6
|
+
// readSync() is provided: the first read uses readFileSync, then shares the cache; subsequent writes use async save()/mutate()
|
|
7
|
+
// and update the cache synchronously, ensuring sync reads always see the latest value.
|
|
8
|
+
// startProfileWatcher() watches profiles.json for changes, invalidates the cache and reloads, making external edits effective immediately.
|
|
9
|
+
// ValidationException is handled by the caller (profile-manager.ts) to avoid
|
|
10
|
+
// forming a circular dependency (type-only import).
|
|
11
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
12
|
+
import { readFileSync, watch, existsSync } from 'fs';
|
|
13
|
+
import * as path from 'path';
|
|
14
|
+
import { JsonRepository } from './json-repository.js';
|
|
15
|
+
import { CONFIG_DIR } from '../core/paths.js';
|
|
16
|
+
import { createLogger } from '../core/log.js';
|
|
17
|
+
const log = createLogger('profile-repo');
|
|
18
|
+
export const PROFILES_FILE = path.join(CONFIG_DIR, 'profiles.json');
|
|
19
|
+
export class ProfileRepo {
|
|
20
|
+
_repo;
|
|
21
|
+
_filePath;
|
|
22
|
+
_syncCache = null;
|
|
23
|
+
constructor(filePath = PROFILES_FILE) {
|
|
24
|
+
this._filePath = filePath;
|
|
25
|
+
this._repo = new JsonRepository({
|
|
26
|
+
filePath,
|
|
27
|
+
// profiles.json is required; throw immediately if not present.
|
|
28
|
+
// Matches existing loadProfilesFile() behavior which throws on ENOENT.
|
|
29
|
+
defaultValue: () => { throw new Error(`profiles.json not found at ${filePath}`); },
|
|
30
|
+
// Basic cast; full schema validation is performed by profile-manager.ts callers
|
|
31
|
+
// (validateProfilesFile) to avoid a circular runtime import.
|
|
32
|
+
migrate: (raw) => raw,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async read() {
|
|
36
|
+
const data = await this._repo.read();
|
|
37
|
+
this._syncCache = data;
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Synchronous read for legacy sync callers (profile-manager.ts public API).
|
|
42
|
+
* First call reads from disk; subsequent calls serve from cache. save()/mutate()
|
|
43
|
+
* update the cache on success so sync readers see fresh data.
|
|
44
|
+
*/
|
|
45
|
+
readSync() {
|
|
46
|
+
if (this._syncCache)
|
|
47
|
+
return this._syncCache;
|
|
48
|
+
const raw = readFileSync(this._filePath, 'utf8');
|
|
49
|
+
const parsed = JSON.parse(raw);
|
|
50
|
+
this._syncCache = parsed;
|
|
51
|
+
return parsed;
|
|
52
|
+
}
|
|
53
|
+
async save(data) {
|
|
54
|
+
await this._repo.write(data);
|
|
55
|
+
this._syncCache = data;
|
|
56
|
+
}
|
|
57
|
+
async mutate(fn) {
|
|
58
|
+
return this._repo.mutate((cur) => {
|
|
59
|
+
const { next, result } = fn(cur);
|
|
60
|
+
this._syncCache = next;
|
|
61
|
+
return { next, result };
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Drop the in-memory cache so the next read() fetches from disk. Test hook. */
|
|
65
|
+
invalidate() {
|
|
66
|
+
this._repo.invalidate();
|
|
67
|
+
this._syncCache = null;
|
|
68
|
+
}
|
|
69
|
+
/** Wait for any in-flight mutate() to complete. For graceful SIGTERM drain. */
|
|
70
|
+
flush() {
|
|
71
|
+
return this._repo.flush();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export const profileRepo = new ProfileRepo();
|
|
75
|
+
// --- Admin notification (hot-reload → Slack) ---
|
|
76
|
+
let _adminNotifier = null;
|
|
77
|
+
export function setAdminNotifier(fn) { _adminNotifier = fn; }
|
|
78
|
+
/**
|
|
79
|
+
* Watch profiles.json for external edits and hot-reload the cache.
|
|
80
|
+
* Mirrors the pattern used by startMachineRegistryWatcher() in dispatch-utils.ts.
|
|
81
|
+
*
|
|
82
|
+
* Returns a stop function — call it to tear down the watcher (e.g. in tests or SIGTERM).
|
|
83
|
+
*
|
|
84
|
+
* @param repo ProfileRepo instance to invalidate on change (defaults to singleton).
|
|
85
|
+
* @param filePath Path to watch (defaults to PROFILES_FILE).
|
|
86
|
+
*/
|
|
87
|
+
export function startProfileWatcher(repo = profileRepo, filePath = PROFILES_FILE) {
|
|
88
|
+
if (!existsSync(filePath))
|
|
89
|
+
return () => { };
|
|
90
|
+
let watcher = null;
|
|
91
|
+
let reloadTimer = null;
|
|
92
|
+
const reload = () => {
|
|
93
|
+
try {
|
|
94
|
+
// Validate the file is parseable BEFORE touching the cache.
|
|
95
|
+
// This prevents invalidate() from leaving cache null if the file is corrupt.
|
|
96
|
+
const raw = readFileSync(filePath, 'utf8');
|
|
97
|
+
JSON.parse(raw); // throws on invalid JSON — cache stays intact
|
|
98
|
+
repo.invalidate();
|
|
99
|
+
repo.readSync(); // re-fill cache from the validated content
|
|
100
|
+
log.info('Hot-reload: profiles.json reloaded');
|
|
101
|
+
_adminNotifier?.(':arrows_counterclockwise: `profiles.json` hot-reloaded');
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
log.error(`Hot-reload profiles.json failed: ${e.message} — keeping previous config`);
|
|
105
|
+
_adminNotifier?.(`:warning: \`profiles.json\` hot-reload FAILED — keeping previous config`);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const setup = () => {
|
|
109
|
+
try {
|
|
110
|
+
if (watcher)
|
|
111
|
+
watcher.close();
|
|
112
|
+
watcher = watch(filePath, (eventType) => {
|
|
113
|
+
if (eventType === 'rename') {
|
|
114
|
+
// File was atomically replaced (inode changed); reload now and re-create
|
|
115
|
+
// the watcher on the new inode after a short settle delay.
|
|
116
|
+
reload();
|
|
117
|
+
setTimeout(() => setup(), 100);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// 'change' event — debounce to coalesce rapid writes.
|
|
121
|
+
if (reloadTimer)
|
|
122
|
+
clearTimeout(reloadTimer);
|
|
123
|
+
reloadTimer = setTimeout(() => {
|
|
124
|
+
reloadTimer = null;
|
|
125
|
+
reload();
|
|
126
|
+
}, 300);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
log.error(`Failed to watch profiles.json: ${e.message}`);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
setup();
|
|
134
|
+
return () => {
|
|
135
|
+
if (watcher) {
|
|
136
|
+
watcher.close();
|
|
137
|
+
watcher = null;
|
|
138
|
+
}
|
|
139
|
+
if (reloadTimer) {
|
|
140
|
+
clearTimeout(reloadTimer);
|
|
141
|
+
reloadTimer = null;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ChannelRepo } from './channel-repo.js';
|
|
2
|
+
/** Shape of project-dirs.json: `{"projectName": {"machineName": "dirPath", ...}, ...}` */
|
|
3
|
+
export type ProjectDirsData = Record<string, Record<string, string>>;
|
|
4
|
+
interface ProjectDirRepoOptions {
|
|
5
|
+
filePath?: string;
|
|
6
|
+
channelRepoOverride?: ChannelRepo;
|
|
7
|
+
}
|
|
8
|
+
export declare class ProjectDirRepo {
|
|
9
|
+
private readonly _repo;
|
|
10
|
+
private readonly _channelRepo;
|
|
11
|
+
constructor(opts?: ProjectDirRepoOptions);
|
|
12
|
+
getProjectDir(project: string, machine: string): Promise<string | null>;
|
|
13
|
+
setProjectDir(project: string, machine: string, dirPath: string): Promise<void>;
|
|
14
|
+
removeProjectDir(project: string, machine: string): Promise<void>;
|
|
15
|
+
getAllProjectDirs(): Promise<Record<string, Record<string, string>>>;
|
|
16
|
+
/** Reverse-lookup: channelId → project name (via channel-registry.json). */
|
|
17
|
+
getChannelProject(channelId: string): Promise<string | null>;
|
|
18
|
+
/** Wait for any in-flight mutate() to complete. For graceful SIGTERM drain. */
|
|
19
|
+
flush(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export declare const projectDirRepo: ProjectDirRepo;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// input: project-dirs.json (+ channel-registry.json for reverse lookup)
|
|
2
|
+
// output: ProjectDirRepo (async getProjectDir / setProjectDir / removeProjectDir / getAllProjectDirs / getChannelProject)
|
|
3
|
+
// pos: Project-device code directory mapping persistence layer. Based on JsonRepository abstraction, AsyncMutex serializes reads/writes of project-dirs.json.
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { JsonRepository } from './json-repository.js';
|
|
7
|
+
import { STORE_DIR } from '../core/paths.js';
|
|
8
|
+
import { channelRepo } from './channel-repo.js';
|
|
9
|
+
const PROJECT_DIRS_FILE = path.join(STORE_DIR, 'project-dirs.json');
|
|
10
|
+
export class ProjectDirRepo {
|
|
11
|
+
_repo;
|
|
12
|
+
_channelRepo;
|
|
13
|
+
constructor(opts = {}) {
|
|
14
|
+
this._repo = new JsonRepository({
|
|
15
|
+
filePath: opts.filePath ?? PROJECT_DIRS_FILE,
|
|
16
|
+
defaultValue: () => ({}),
|
|
17
|
+
migrate: (raw) => (typeof raw === 'object' && raw !== null ? raw : ({})),
|
|
18
|
+
});
|
|
19
|
+
this._channelRepo = opts.channelRepoOverride ?? channelRepo;
|
|
20
|
+
}
|
|
21
|
+
async getProjectDir(project, machine) {
|
|
22
|
+
const data = await this._repo.read();
|
|
23
|
+
return (data[project] && data[project][machine]) ?? null;
|
|
24
|
+
}
|
|
25
|
+
async setProjectDir(project, machine, dirPath) {
|
|
26
|
+
await this._repo.mutate((data) => {
|
|
27
|
+
if (!data[project])
|
|
28
|
+
data[project] = {};
|
|
29
|
+
data[project][machine] = dirPath;
|
|
30
|
+
return { next: data, result: undefined };
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async removeProjectDir(project, machine) {
|
|
34
|
+
await this._repo.mutate((data) => {
|
|
35
|
+
if (data[project]) {
|
|
36
|
+
delete data[project][machine];
|
|
37
|
+
if (Object.keys(data[project]).length === 0)
|
|
38
|
+
delete data[project];
|
|
39
|
+
}
|
|
40
|
+
return { next: data, result: undefined };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async getAllProjectDirs() {
|
|
44
|
+
return { ...(await this._repo.read()) };
|
|
45
|
+
}
|
|
46
|
+
/** Reverse-lookup: channelId → project name (via channel-registry.json). */
|
|
47
|
+
async getChannelProject(channelId) {
|
|
48
|
+
const channelReg = await this._channelRepo.getAllRegistrations();
|
|
49
|
+
for (const [project, ch] of Object.entries(channelReg)) {
|
|
50
|
+
if (ch === channelId)
|
|
51
|
+
return project;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
/** Wait for any in-flight mutate() to complete. For graceful SIGTERM drain. */
|
|
56
|
+
flush() {
|
|
57
|
+
return this._repo.flush();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export const projectDirRepo = new ProjectDirRepo();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export declare const SCHEDULES_FILE: string;
|
|
2
|
+
/** Where a fired scheduled-task should land. Resolved by the cortex_schedule_add MCP tool
|
|
3
|
+
* (or schedule-cli) at create time — `__current__` placeholders are concretized then, so
|
|
4
|
+
* list/get always show real IDs. The 4 kinds map to the dispatch branches in
|
|
5
|
+
* scheduling/jobs/scheduled-task.ts. */
|
|
6
|
+
export type ScheduleTarget = {
|
|
7
|
+
kind: 'fresh';
|
|
8
|
+
}
|
|
9
|
+
/** Fire into the channel — reuses any active thread / channel-default session. */
|
|
10
|
+
| {
|
|
11
|
+
kind: 'channel';
|
|
12
|
+
channel: string;
|
|
13
|
+
}
|
|
14
|
+
/** Fire into a specific cortex-XXXX session, reusing its sessionId. Channel+sessionId are locked at create time. */
|
|
15
|
+
| {
|
|
16
|
+
kind: 'session';
|
|
17
|
+
sessionName: string;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
channel: string;
|
|
20
|
+
}
|
|
21
|
+
/** Continue a specific thread; only valid while thread.status is running|waiting. */
|
|
22
|
+
| {
|
|
23
|
+
kind: 'thread';
|
|
24
|
+
threadId: string;
|
|
25
|
+
channel: string;
|
|
26
|
+
};
|
|
27
|
+
export interface ScheduleTask {
|
|
28
|
+
id: string;
|
|
29
|
+
type: 'interval' | 'daily' | 'weekly' | 'once';
|
|
30
|
+
message: string;
|
|
31
|
+
channel: string;
|
|
32
|
+
profile: string | null;
|
|
33
|
+
intervalMs?: number;
|
|
34
|
+
time?: string;
|
|
35
|
+
dayOfWeek?: number;
|
|
36
|
+
delay?: number;
|
|
37
|
+
runAt?: number;
|
|
38
|
+
nextRun?: number | null;
|
|
39
|
+
createdAt: number;
|
|
40
|
+
isPaused?: boolean;
|
|
41
|
+
pausedAt?: number | null;
|
|
42
|
+
pausedBy?: 'user' | 'rate-limit' | null;
|
|
43
|
+
lastRun?: number | null;
|
|
44
|
+
lastSkipped?: number | null;
|
|
45
|
+
dispatchType?: string;
|
|
46
|
+
preCheck?: string;
|
|
47
|
+
taskConfigHash?: string;
|
|
48
|
+
/** Dispatch routing — defaults to { kind: 'fresh' } for legacy records. */
|
|
49
|
+
target?: ScheduleTarget;
|
|
50
|
+
/** What to do when target session/thread no longer exists at fire time.
|
|
51
|
+
* fresh: silently fall back to fresh-thread dispatch.
|
|
52
|
+
* skip: record lastSkipped, post a Slack one-liner, do not run.
|
|
53
|
+
* wait: reschedule short delay (max 3 retries), then fall back to fresh. */
|
|
54
|
+
fallback?: 'fresh' | 'skip' | 'wait';
|
|
55
|
+
}
|
|
56
|
+
export interface SchedulesData {
|
|
57
|
+
tasks: ScheduleTask[];
|
|
58
|
+
rateLimitThrottle?: {
|
|
59
|
+
resetsAt: number;
|
|
60
|
+
activatedAt: number;
|
|
61
|
+
modes?: string[];
|
|
62
|
+
} | null;
|
|
63
|
+
}
|
|
64
|
+
export declare class ScheduleRepo {
|
|
65
|
+
private _repo;
|
|
66
|
+
constructor(filePath?: string);
|
|
67
|
+
read(): Promise<SchedulesData>;
|
|
68
|
+
addTask(task: ScheduleTask): Promise<void>;
|
|
69
|
+
removeTask(id: string): Promise<boolean>;
|
|
70
|
+
updateTask(id: string, fn: (task: ScheduleTask) => void): Promise<ScheduleTask | null>;
|
|
71
|
+
findTask(id: string): Promise<ScheduleTask | null>;
|
|
72
|
+
setRateLimitThrottle(meta: {
|
|
73
|
+
resetsAt: number;
|
|
74
|
+
activatedAt: number;
|
|
75
|
+
modes?: string[];
|
|
76
|
+
} | null): Promise<void>;
|
|
77
|
+
getRateLimitThrottle(): Promise<{
|
|
78
|
+
resetsAt: number;
|
|
79
|
+
activatedAt: number;
|
|
80
|
+
modes?: string[];
|
|
81
|
+
} | null>;
|
|
82
|
+
/** Generic mutate passthrough for composite operations. */
|
|
83
|
+
mutate<R>(fn: (data: SchedulesData) => {
|
|
84
|
+
next: SchedulesData;
|
|
85
|
+
result: R;
|
|
86
|
+
}): Promise<R>;
|
|
87
|
+
/** Drop the in-memory cache so the next read() fetches from disk. Test hook. */
|
|
88
|
+
invalidate(): void;
|
|
89
|
+
/** Wait for any in-flight mutate() to complete. For graceful SIGTERM drain. */
|
|
90
|
+
flush(): Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
export declare const scheduleRepo: ScheduleRepo;
|