@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,56 @@
|
|
|
1
|
+
// input: fs, path, DATA_DIR
|
|
2
|
+
// output: cleanupLogs / ensureMcpConfig
|
|
3
|
+
// pos: collection of pure helper functions for the startup phase
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { readdirSync, statSync, unlinkSync } from 'fs';
|
|
6
|
+
import * as path from 'path';
|
|
7
|
+
import { DATA_DIR } from '../core/utils.js';
|
|
8
|
+
import { createLogger } from '../core/log.js';
|
|
9
|
+
import { generateMcpConfig } from '../core/config-generator.js';
|
|
10
|
+
const log = createLogger('startup');
|
|
11
|
+
const LOGS_DIR = path.join(DATA_DIR, 'logs', 'sessions');
|
|
12
|
+
const LOG_RETENTION_MS = {
|
|
13
|
+
json: 30 * 24 * 60 * 60 * 1000,
|
|
14
|
+
txt: 30 * 24 * 60 * 60 * 1000,
|
|
15
|
+
};
|
|
16
|
+
export function cleanupLogs() {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
let deleted = 0;
|
|
19
|
+
let files;
|
|
20
|
+
try {
|
|
21
|
+
files = readdirSync(LOGS_DIR);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
for (const f of files) {
|
|
27
|
+
if (f === 'daemon.log')
|
|
28
|
+
continue;
|
|
29
|
+
const ext = path.extname(f).toLowerCase();
|
|
30
|
+
const maxAge = (ext === '.json' || ext === '.jsonl') ? LOG_RETENTION_MS.json
|
|
31
|
+
: ext === '.txt' ? LOG_RETENTION_MS.txt
|
|
32
|
+
: null;
|
|
33
|
+
if (!maxAge)
|
|
34
|
+
continue;
|
|
35
|
+
const fp = path.join(LOGS_DIR, f);
|
|
36
|
+
try {
|
|
37
|
+
const { mtimeMs } = statSync(fp);
|
|
38
|
+
if (now - mtimeMs > maxAge) {
|
|
39
|
+
unlinkSync(fp);
|
|
40
|
+
deleted++;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch { }
|
|
44
|
+
}
|
|
45
|
+
if (deleted > 0)
|
|
46
|
+
log.info(`Log cleanup: deleted ${deleted} old file(s).`);
|
|
47
|
+
}
|
|
48
|
+
export function ensureMcpConfig() {
|
|
49
|
+
try {
|
|
50
|
+
generateMcpConfig();
|
|
51
|
+
log.info('MCP config ensured');
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
log.warn(`Failed to ensure MCP config: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PlatformAdapter } from '../platform/index.js';
|
|
2
|
+
declare function buildStartupMessage({ machine, restartReason }: {
|
|
3
|
+
machine?: string;
|
|
4
|
+
restartReason?: string;
|
|
5
|
+
}): string;
|
|
6
|
+
declare function sendStartupDmIfConfigured(adapter: PlatformAdapter, { machine, restartReason }?: {
|
|
7
|
+
machine?: string;
|
|
8
|
+
restartReason?: string;
|
|
9
|
+
}): Promise<boolean>;
|
|
10
|
+
export { buildStartupMessage, sendStartupDmIfConfigured };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// input: PlatformAdapter + startup metadata
|
|
2
|
+
// output: buildStartupMessage + sendStartupDmIfConfigured
|
|
3
|
+
// pos: send DM notification to admin channel during startup
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
function buildStartupMessage({ machine, restartReason }) {
|
|
6
|
+
const machineLabel = machine || 'unknown-machine';
|
|
7
|
+
const verb = restartReason ? 'restarted' : 'started';
|
|
8
|
+
const base = `Cortex agent ${verb} on ${machineLabel}.`;
|
|
9
|
+
return restartReason ? `${base} Reason: ${restartReason}.` : base;
|
|
10
|
+
}
|
|
11
|
+
async function sendStartupDmIfConfigured(adapter, { machine, restartReason } = {}) {
|
|
12
|
+
const adminChannel = adapter.getAdminChannel();
|
|
13
|
+
if (!adminChannel)
|
|
14
|
+
return false;
|
|
15
|
+
await adapter.postMessage(adminChannel, {
|
|
16
|
+
text: buildStartupMessage({ machine, restartReason }),
|
|
17
|
+
});
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
export { buildStartupMessage, sendStartupDmIfConfigured };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CortexEvent, CortexEventInput } from './event-types.js';
|
|
2
|
+
export interface Subscription {
|
|
3
|
+
unsubscribe(): void;
|
|
4
|
+
}
|
|
5
|
+
export declare class EventBus {
|
|
6
|
+
private _entries;
|
|
7
|
+
private _closeHooks;
|
|
8
|
+
/** Guard against re-entrant event-bus.handler-failed loops. */
|
|
9
|
+
private _inHandlerFailed;
|
|
10
|
+
/**
|
|
11
|
+
* Subscribe to a specific event type or '*' for all events.
|
|
12
|
+
* Handlers are called in subscription-registration order on publish.
|
|
13
|
+
*
|
|
14
|
+
* Overloads narrow `e` to the matching variant when a specific K is given;
|
|
15
|
+
* the '*' form receives the full CortexEvent union.
|
|
16
|
+
*/
|
|
17
|
+
subscribe<K extends CortexEvent['type']>(type: K, handler: (e: Extract<CortexEvent, {
|
|
18
|
+
type: K;
|
|
19
|
+
}>) => void | Promise<void>): Subscription;
|
|
20
|
+
subscribe(type: '*', handler: (e: CortexEvent) => void | Promise<void>): Subscription;
|
|
21
|
+
/**
|
|
22
|
+
* Publish an event. `ts` is injected here; callers omit it.
|
|
23
|
+
*
|
|
24
|
+
* Fan-out is synchronous: all matching handlers are called in subscription order
|
|
25
|
+
* before publish() returns. If a handler returns a Promise it is fire-and-forget
|
|
26
|
+
* (errors are caught and logged). If a handler throws synchronously the error is
|
|
27
|
+
* logged and an `event-bus.handler-failed` meta-event is published; the remaining
|
|
28
|
+
* handlers continue to run.
|
|
29
|
+
*/
|
|
30
|
+
publish(e: CortexEventInput): void;
|
|
31
|
+
private _emitHandlerFailed;
|
|
32
|
+
/**
|
|
33
|
+
* Register a hook called by close(). EventLogger uses this to flush its buffer
|
|
34
|
+
* before the process exits on SIGTERM.
|
|
35
|
+
*/
|
|
36
|
+
registerCloseHook(fn: () => Promise<void>): void;
|
|
37
|
+
/**
|
|
38
|
+
* Flush all registered close hooks (e.g., EventLogger buffer drain).
|
|
39
|
+
* Called from the app.ts SIGTERM handler alongside repo flushes.
|
|
40
|
+
*/
|
|
41
|
+
close(): Promise<void>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// input: CortexEvent (event-types.ts)
|
|
2
|
+
// output: EventBus class — subscribe / publish / close
|
|
3
|
+
// pos: events/ layer, only depends on event-types
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { createLogger } from '../core/log.js';
|
|
6
|
+
const log = createLogger('event-bus');
|
|
7
|
+
export class EventBus {
|
|
8
|
+
_entries = [];
|
|
9
|
+
_closeHooks = [];
|
|
10
|
+
/** Guard against re-entrant event-bus.handler-failed loops. */
|
|
11
|
+
_inHandlerFailed = false;
|
|
12
|
+
subscribe(type, handler) {
|
|
13
|
+
const entry = { matchType: type, handler };
|
|
14
|
+
this._entries.push(entry);
|
|
15
|
+
return {
|
|
16
|
+
unsubscribe: () => {
|
|
17
|
+
const idx = this._entries.indexOf(entry);
|
|
18
|
+
if (idx !== -1)
|
|
19
|
+
this._entries.splice(idx, 1);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Publish an event. `ts` is injected here; callers omit it.
|
|
25
|
+
*
|
|
26
|
+
* Fan-out is synchronous: all matching handlers are called in subscription order
|
|
27
|
+
* before publish() returns. If a handler returns a Promise it is fire-and-forget
|
|
28
|
+
* (errors are caught and logged). If a handler throws synchronously the error is
|
|
29
|
+
* logged and an `event-bus.handler-failed` meta-event is published; the remaining
|
|
30
|
+
* handlers continue to run.
|
|
31
|
+
*/
|
|
32
|
+
publish(e) {
|
|
33
|
+
const event = { ...e, ts: new Date().toISOString() };
|
|
34
|
+
// Snapshot to avoid mutation during iteration (unsubscribe inside a handler is safe)
|
|
35
|
+
const entries = this._entries.slice();
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
if (entry.matchType !== '*' && entry.matchType !== event.type)
|
|
38
|
+
continue;
|
|
39
|
+
let result;
|
|
40
|
+
try {
|
|
41
|
+
result = entry.handler(event);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
log.error(`handler for "${entry.matchType}" threw:`, err);
|
|
45
|
+
this._emitHandlerFailed(entry.matchType, err);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (result && typeof result.then === 'function') {
|
|
49
|
+
result.catch((err) => {
|
|
50
|
+
log.error(`async handler for "${entry.matchType}" rejected:`, err);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
_emitHandlerFailed(handlerType, err) {
|
|
56
|
+
if (this._inHandlerFailed)
|
|
57
|
+
return; // prevent re-entrant loops
|
|
58
|
+
this._inHandlerFailed = true;
|
|
59
|
+
try {
|
|
60
|
+
this.publish({
|
|
61
|
+
type: 'event-bus.handler-failed',
|
|
62
|
+
handlerType,
|
|
63
|
+
error: err instanceof Error ? err.message : String(err),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
this._inHandlerFailed = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Register a hook called by close(). EventLogger uses this to flush its buffer
|
|
72
|
+
* before the process exits on SIGTERM.
|
|
73
|
+
*/
|
|
74
|
+
registerCloseHook(fn) {
|
|
75
|
+
this._closeHooks.push(fn);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Flush all registered close hooks (e.g., EventLogger buffer drain).
|
|
79
|
+
* Called from the app.ts SIGTERM handler alongside repo flushes.
|
|
80
|
+
*/
|
|
81
|
+
async close() {
|
|
82
|
+
await Promise.allSettled(this._closeHooks.map((fn) => fn()));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EventBus } from './event-bus.js';
|
|
2
|
+
export interface EventLoggerOptions {
|
|
3
|
+
/** Override for testing; production default is 1024. */
|
|
4
|
+
bufferSize?: number;
|
|
5
|
+
/** Override log directory; production default is <agent-server>/logs/events. */
|
|
6
|
+
logDir?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface EventLogger {
|
|
9
|
+
/** Flush all buffered entries to disk. Called automatically by bus.close(). */
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export declare function createEventLogger(bus: EventBus, opts?: EventLoggerOptions): EventLogger;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// input: EventBus (event-bus.ts), CortexEvent (event-types.ts)
|
|
2
|
+
// output: createEventLogger — writes events to daily rolling jsonl files
|
|
3
|
+
// pos: events/ layer, depends on event-bus + event-types + core/async-mutex
|
|
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 { AsyncMutex } from '../core/async-mutex.js';
|
|
8
|
+
import { DATA_DIR } from '../core/paths.js';
|
|
9
|
+
// ── Constants ────────────────────────────────────────────────────────────────
|
|
10
|
+
const DEFAULT_BUFFER_SIZE = 1024;
|
|
11
|
+
const FLUSH_INTERVAL_MS = 100;
|
|
12
|
+
const RETENTION_DAYS = 14;
|
|
13
|
+
/**
|
|
14
|
+
* Meta-events emitted by the bus infrastructure itself.
|
|
15
|
+
* The logger skips adding these to its own ring buffer to prevent re-entrant
|
|
16
|
+
* backpressure loops (e.g., event-logger.dropped triggering another drop).
|
|
17
|
+
* They are still published on the bus and visible to other subscribers.
|
|
18
|
+
*/
|
|
19
|
+
const META_EVENTS = new Set([
|
|
20
|
+
'event-bus.handler-failed',
|
|
21
|
+
'event-logger.dropped',
|
|
22
|
+
]);
|
|
23
|
+
// ── Implementation ────────────────────────────────────────────────────────────
|
|
24
|
+
export function createEventLogger(bus, opts) {
|
|
25
|
+
const bufferSize = opts?.bufferSize ?? DEFAULT_BUFFER_SIZE;
|
|
26
|
+
const disabled = process.env.CORTEX_EVENT_LOG === 'off';
|
|
27
|
+
const logDir = opts?.logDir ?? path.join(DATA_DIR, 'logs', 'events');
|
|
28
|
+
let buffer = [];
|
|
29
|
+
let seq = 0;
|
|
30
|
+
let flushTimer = null;
|
|
31
|
+
const flushMutex = new AsyncMutex();
|
|
32
|
+
let _inDrop = false;
|
|
33
|
+
let _lastCleanupDate = ''; // NTH-2: run retention cleanup at most once per day
|
|
34
|
+
function todayYYYYMMDD() {
|
|
35
|
+
return new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
36
|
+
}
|
|
37
|
+
function onEvent(e) {
|
|
38
|
+
if (disabled)
|
|
39
|
+
return;
|
|
40
|
+
// Skip meta-events to prevent re-entrant backpressure loops (see META_EVENTS comment above)
|
|
41
|
+
if (META_EVENTS.has(e.type))
|
|
42
|
+
return;
|
|
43
|
+
// Backpressure: drop oldest entry if ring buffer is full
|
|
44
|
+
if (buffer.length >= bufferSize) {
|
|
45
|
+
const dropped = buffer.shift();
|
|
46
|
+
if (!_inDrop) {
|
|
47
|
+
_inDrop = true;
|
|
48
|
+
try {
|
|
49
|
+
bus.publish({
|
|
50
|
+
type: 'event-logger.dropped',
|
|
51
|
+
droppedSeq: dropped.seq,
|
|
52
|
+
droppedType: dropped.type,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
_inDrop = false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
buffer.push({
|
|
61
|
+
ts: e.ts,
|
|
62
|
+
type: e.type,
|
|
63
|
+
payload: e,
|
|
64
|
+
pid: process.pid,
|
|
65
|
+
seq: seq++,
|
|
66
|
+
});
|
|
67
|
+
// Trigger immediate flush when buffer hits capacity (spec: "buffer full, flush immediately")
|
|
68
|
+
if (buffer.length >= bufferSize) {
|
|
69
|
+
void flushNow();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function flushNow() {
|
|
73
|
+
if (disabled)
|
|
74
|
+
return;
|
|
75
|
+
// Snapshot buffer outside the mutex to minimise lock hold time
|
|
76
|
+
if (buffer.length === 0)
|
|
77
|
+
return;
|
|
78
|
+
return flushMutex.run(async () => {
|
|
79
|
+
if (buffer.length === 0)
|
|
80
|
+
return;
|
|
81
|
+
const entries = buffer.splice(0);
|
|
82
|
+
// Note: we use flush-time date for file routing (NTH-4 acknowledged).
|
|
83
|
+
// Each entry carries its own `ts` field so replay is always timestamp-correct.
|
|
84
|
+
const dateStr = todayYYYYMMDD();
|
|
85
|
+
const logFile = path.join(logDir, `events-${dateStr}.jsonl`);
|
|
86
|
+
await fs.mkdir(logDir, { recursive: true });
|
|
87
|
+
await fs.appendFile(logFile, entries.map((e) => JSON.stringify(e)).join('\n') + '\n');
|
|
88
|
+
// Retention: delete files older than RETENTION_DAYS — at most once per calendar day (NTH-2)
|
|
89
|
+
if (dateStr !== _lastCleanupDate) {
|
|
90
|
+
_lastCleanupDate = dateStr;
|
|
91
|
+
void runRetentionCleanup();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async function runRetentionCleanup() {
|
|
96
|
+
try {
|
|
97
|
+
const entries = await fs.readdir(logDir);
|
|
98
|
+
const cutoff = new Date();
|
|
99
|
+
cutoff.setDate(cutoff.getDate() - RETENTION_DAYS);
|
|
100
|
+
const cutoffStr = cutoff.toISOString().slice(0, 10).replace(/-/g, '');
|
|
101
|
+
await Promise.all(entries
|
|
102
|
+
.filter((f) => /^events-\d{8}\.jsonl$/.test(f))
|
|
103
|
+
.filter((f) => {
|
|
104
|
+
const dateInFile = f.slice(7, 15); // extract YYYYMMDD from "events-YYYYMMDD.jsonl"
|
|
105
|
+
return dateInFile < cutoffStr;
|
|
106
|
+
})
|
|
107
|
+
.map((f) => fs.unlink(path.join(logDir, f)).catch(() => { })));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Best-effort; missing directory is fine on first run
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Flush remaining buffer entries and stop the interval timer.
|
|
115
|
+
* Registered as a close hook on the bus; called from bus.close() on SIGTERM.
|
|
116
|
+
* NTH-3: explicit clearInterval + return flushNow() pattern.
|
|
117
|
+
*/
|
|
118
|
+
async function close() {
|
|
119
|
+
if (flushTimer !== null) {
|
|
120
|
+
clearInterval(flushTimer);
|
|
121
|
+
flushTimer = null;
|
|
122
|
+
}
|
|
123
|
+
return flushNow();
|
|
124
|
+
}
|
|
125
|
+
// ── Wire up ────────────────────────────────────────────────────────────────
|
|
126
|
+
if (!disabled) {
|
|
127
|
+
bus.subscribe('*', onEvent);
|
|
128
|
+
flushTimer = setInterval(() => { void flushNow(); }, FLUSH_INTERVAL_MS);
|
|
129
|
+
// Prevent the timer from keeping the process alive after bus.close()
|
|
130
|
+
if (flushTimer.unref)
|
|
131
|
+
flushTimer.unref();
|
|
132
|
+
}
|
|
133
|
+
bus.registerCloseHook(close);
|
|
134
|
+
return { close };
|
|
135
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// input: --date YYYY-MM-DD, --type <event-type> (optional)
|
|
2
|
+
// output: matching jsonl lines from the daily events log printed to stdout
|
|
3
|
+
// pos: debug CLI — node events/event-replay.ts --date 2026-04-25 --type agent.failed
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { createReadStream } from 'node:fs';
|
|
6
|
+
import { createInterface } from 'node:readline';
|
|
7
|
+
import * as path from 'node:path';
|
|
8
|
+
import { parseArgs } from 'node:util';
|
|
9
|
+
import { existsSync } from 'node:fs';
|
|
10
|
+
import { createLogger } from '../core/log.js';
|
|
11
|
+
const log = createLogger('event-replay');
|
|
12
|
+
const { values } = parseArgs({
|
|
13
|
+
options: {
|
|
14
|
+
date: { type: 'string' },
|
|
15
|
+
type: { type: 'string' },
|
|
16
|
+
},
|
|
17
|
+
strict: false,
|
|
18
|
+
});
|
|
19
|
+
const rawDate = typeof values.date === 'string' ? values.date : undefined;
|
|
20
|
+
const dateStr = rawDate ?? new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
21
|
+
// Normalise: allow YYYY-MM-DD input and convert to YYYYMMDD
|
|
22
|
+
const normalizedDate = dateStr.replace(/-/g, '');
|
|
23
|
+
const typeFilter = typeof values.type === 'string' ? values.type : undefined;
|
|
24
|
+
// Resolve log dir relative to this file: src/events/ → ../../logs/events
|
|
25
|
+
const logDir = path.join(path.dirname(new URL(import.meta.url).pathname), '..', '..', 'logs', 'events');
|
|
26
|
+
const logFile = path.join(logDir, `events-${normalizedDate}.jsonl`);
|
|
27
|
+
if (!existsSync(logFile)) {
|
|
28
|
+
log.error(`No log file found: ${logFile}`);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
const rl = createInterface({
|
|
32
|
+
input: createReadStream(logFile, { encoding: 'utf8' }),
|
|
33
|
+
crlfDelay: Infinity,
|
|
34
|
+
});
|
|
35
|
+
rl.on('line', (line) => {
|
|
36
|
+
if (!line.trim())
|
|
37
|
+
return;
|
|
38
|
+
try {
|
|
39
|
+
const entry = JSON.parse(line);
|
|
40
|
+
if (typeFilter && entry.type !== typeFilter)
|
|
41
|
+
return;
|
|
42
|
+
console.log(line);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Skip malformed lines
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
rl.on('error', (err) => {
|
|
49
|
+
log.error('Error reading log file:', err.message);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** All CortexEvent variants. `ts` is ISO-8601, injected by EventBus.publish(). */
|
|
2
|
+
export type CortexEvent = {
|
|
3
|
+
type: 'message.received';
|
|
4
|
+
ts: string;
|
|
5
|
+
channel: string;
|
|
6
|
+
user: string;
|
|
7
|
+
text: string;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'message.edited';
|
|
10
|
+
ts: string;
|
|
11
|
+
channel: string;
|
|
12
|
+
user: string;
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'plan.submitted';
|
|
16
|
+
ts: string;
|
|
17
|
+
requestId: string;
|
|
18
|
+
channel: string;
|
|
19
|
+
sessionId: string;
|
|
20
|
+
threadId?: string | null;
|
|
21
|
+
planContent: string;
|
|
22
|
+
toolInput: any;
|
|
23
|
+
dryRun?: boolean;
|
|
24
|
+
extensionUiId?: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'plan.approved';
|
|
27
|
+
ts: string;
|
|
28
|
+
channel: string;
|
|
29
|
+
executionId: string;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'ask-user.requested';
|
|
32
|
+
ts: string;
|
|
33
|
+
requestId: string;
|
|
34
|
+
channel: string;
|
|
35
|
+
sessionId: string;
|
|
36
|
+
threadId?: string | null;
|
|
37
|
+
questions: any[];
|
|
38
|
+
dryRun?: boolean;
|
|
39
|
+
extensionUiId?: string;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'ask-user.answered';
|
|
42
|
+
ts: string;
|
|
43
|
+
channel: string;
|
|
44
|
+
requestId?: string;
|
|
45
|
+
sessionId: string;
|
|
46
|
+
answer: string;
|
|
47
|
+
} | {
|
|
48
|
+
type: 'agent.started';
|
|
49
|
+
ts: string;
|
|
50
|
+
channel: string;
|
|
51
|
+
executionId: string;
|
|
52
|
+
backend: string;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'agent.completed';
|
|
55
|
+
ts: string;
|
|
56
|
+
executionId: string;
|
|
57
|
+
cost: number;
|
|
58
|
+
durationMs: number;
|
|
59
|
+
} | {
|
|
60
|
+
type: 'agent.failed';
|
|
61
|
+
ts: string;
|
|
62
|
+
executionId: string;
|
|
63
|
+
error: string;
|
|
64
|
+
} | {
|
|
65
|
+
type: 'agent.superseded';
|
|
66
|
+
ts: string;
|
|
67
|
+
executionId: string;
|
|
68
|
+
reason: string;
|
|
69
|
+
} | {
|
|
70
|
+
type: 'thread.created';
|
|
71
|
+
ts: string;
|
|
72
|
+
threadId: string;
|
|
73
|
+
templateName: string;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'thread.step.started';
|
|
76
|
+
ts: string;
|
|
77
|
+
threadId: string;
|
|
78
|
+
step: string;
|
|
79
|
+
} | {
|
|
80
|
+
type: 'thread.step.finished';
|
|
81
|
+
ts: string;
|
|
82
|
+
threadId: string;
|
|
83
|
+
step: string;
|
|
84
|
+
result: string;
|
|
85
|
+
} | {
|
|
86
|
+
type: 'thread.transitioned';
|
|
87
|
+
ts: string;
|
|
88
|
+
threadId: string;
|
|
89
|
+
from: string;
|
|
90
|
+
to: string;
|
|
91
|
+
} | {
|
|
92
|
+
type: 'thread.completed';
|
|
93
|
+
ts: string;
|
|
94
|
+
threadId: string;
|
|
95
|
+
} | {
|
|
96
|
+
type: 'thread.failed';
|
|
97
|
+
ts: string;
|
|
98
|
+
threadId: string;
|
|
99
|
+
error: string;
|
|
100
|
+
} | {
|
|
101
|
+
type: 'task.claimed';
|
|
102
|
+
ts: string;
|
|
103
|
+
taskId: string;
|
|
104
|
+
by: string;
|
|
105
|
+
} | {
|
|
106
|
+
type: 'task.completed';
|
|
107
|
+
ts: string;
|
|
108
|
+
taskId: string;
|
|
109
|
+
} | {
|
|
110
|
+
type: 'task.dispatched';
|
|
111
|
+
ts: string;
|
|
112
|
+
taskId: string;
|
|
113
|
+
machine: string;
|
|
114
|
+
} | {
|
|
115
|
+
type: 'llm.active-count-delta';
|
|
116
|
+
ts: string;
|
|
117
|
+
delta: number;
|
|
118
|
+
} | {
|
|
119
|
+
type: 'scheduler.tick';
|
|
120
|
+
ts: string;
|
|
121
|
+
jobKey: string;
|
|
122
|
+
} | {
|
|
123
|
+
type: 'rate-limit.breach';
|
|
124
|
+
ts: string;
|
|
125
|
+
provider: string;
|
|
126
|
+
percent: number;
|
|
127
|
+
} | {
|
|
128
|
+
type: 'event-bus.handler-failed';
|
|
129
|
+
ts: string;
|
|
130
|
+
handlerType: string;
|
|
131
|
+
error: string;
|
|
132
|
+
} | {
|
|
133
|
+
type: 'event-logger.dropped';
|
|
134
|
+
ts: string;
|
|
135
|
+
droppedSeq: number;
|
|
136
|
+
droppedType: string;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Distributive Omit — correctly removes a key from each union member.
|
|
140
|
+
* Plain `Omit<CortexEvent, 'ts'>` is non-distributive and collapses the union
|
|
141
|
+
* to only shared properties. Use this when you need per-member omission.
|
|
142
|
+
*/
|
|
143
|
+
export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
|
|
144
|
+
/** Argument type for EventBus.publish — CortexEvent without the injected `ts` field. */
|
|
145
|
+
export type CortexEventInput = DistributiveOmit<CortexEvent, 'ts'>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { CortexEvent, CortexEventInput, DistributiveOmit } from './event-types.js';
|
|
2
|
+
export { EventBus } from './event-bus.js';
|
|
3
|
+
export type { Subscription } from './event-bus.js';
|
|
4
|
+
export { createEventLogger } from './event-logger.js';
|
|
5
|
+
export type { EventLogger, EventLoggerOptions } from './event-logger.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// input: event-bus, event-types, event-logger
|
|
2
|
+
// output: public API for events/ layer
|
|
3
|
+
// pos: events/ barrel — import from './index' or './events/index.js'
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
export { EventBus } from './event-bus.js';
|
|
6
|
+
export { createEventLogger } from './event-logger.js';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin Claude Code PreToolUse event, node:http
|
|
3
|
+
// output: stdout updatedInput containing user answers received from Slack
|
|
4
|
+
// pos: PreToolUse hook: intercept AskUserQuestion and forward to Slack to collect answers
|
|
5
|
+
// >>> If I am updated, be sure to update my header comment and the CLAUDE.md in the same folder <<<
|
|
6
|
+
|
|
7
|
+
import http from 'node:http';
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
let input = '';
|
|
11
|
+
for await (const chunk of process.stdin) input += chunk;
|
|
12
|
+
|
|
13
|
+
const hookInput = JSON.parse(input);
|
|
14
|
+
const channel = process.env.SLACK_CHANNEL;
|
|
15
|
+
const port = parseInt(process.env.WEBHOOK_PORT || '3001', 10);
|
|
16
|
+
const sessionId = hookInput.session_id;
|
|
17
|
+
const questions = hookInput.tool_input?.questions;
|
|
18
|
+
|
|
19
|
+
if (!channel || !questions?.length) {
|
|
20
|
+
// No channel or no questions — let Claude handle natively
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const threadId = process.env.CORTEX_THREAD_ID || null;
|
|
25
|
+
const body = JSON.stringify({ sessionId, channel, threadId, questions });
|
|
26
|
+
|
|
27
|
+
const response = await new Promise((resolve, reject) => {
|
|
28
|
+
const req = http.request({
|
|
29
|
+
hostname: '127.0.0.1',
|
|
30
|
+
port,
|
|
31
|
+
path: '/hook/ask-user-question',
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) },
|
|
34
|
+
timeout: 60 * 60 * 1000, // 60 minutes — user may take time to respond
|
|
35
|
+
}, (res) => {
|
|
36
|
+
let data = '';
|
|
37
|
+
res.on('data', (chunk) => { data += chunk; });
|
|
38
|
+
res.on('end', () => {
|
|
39
|
+
try { resolve(JSON.parse(data)); }
|
|
40
|
+
catch { reject(new Error(`Invalid response: ${data}`)); }
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
req.on('error', reject);
|
|
44
|
+
req.on('timeout', () => { req.destroy(); reject(new Error('timeout')); });
|
|
45
|
+
req.write(body);
|
|
46
|
+
req.end();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (response.error) {
|
|
50
|
+
// Server reported an error (e.g. timeout) — fallback
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
process.stdout.write(JSON.stringify({
|
|
55
|
+
hookSpecificOutput: {
|
|
56
|
+
hookEventName: 'PreToolUse',
|
|
57
|
+
permissionDecision: 'allow',
|
|
58
|
+
updatedInput: {
|
|
59
|
+
...hookInput.tool_input,
|
|
60
|
+
answers: response.answers || {},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
main().catch(() => process.exit(1));
|