@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,254 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin Claude Code PostToolUse event, node:fs
|
|
3
|
+
// output: Appends records to logs/session-activity/<session_id>.jsonl
|
|
4
|
+
// pos: PostToolUse hook: track Read/Edit/Write/Skill for diff reconstruction
|
|
5
|
+
|
|
6
|
+
import { readFileSync, mkdirSync, appendFileSync, unlinkSync } from 'fs';
|
|
7
|
+
import { join, resolve } from 'path';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import { homedir } from 'os';
|
|
10
|
+
|
|
11
|
+
const DATA_DIR = process.env.CORTEX_HOME
|
|
12
|
+
? resolve(process.env.CORTEX_HOME)
|
|
13
|
+
: join(homedir(), '.cortex');
|
|
14
|
+
|
|
15
|
+
const MCP_REMOTE_EDIT_TOOL = 'mcp__cortex__remote_edit';
|
|
16
|
+
const MCP_REMOTE_WRITE_TOOL = 'mcp__cortex__remote_write';
|
|
17
|
+
const DIFF_MARKER_RE = /<!--cortex-diff-data\s*([\s\S]*?)\s*-->/;
|
|
18
|
+
const TOOL_USE_ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
19
|
+
|
|
20
|
+
function diffMarkersDir() {
|
|
21
|
+
return join(resolveDataDir(), 'tmp', 'diff-markers');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function resolveDataDir() {
|
|
25
|
+
const override = process.env.CORTEX_HOME;
|
|
26
|
+
return override ? resolve(override) : DATA_DIR;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function resolveSessionId(payload) {
|
|
30
|
+
const envSession = process.env.CORTEX_SESSION_ID?.trim();
|
|
31
|
+
if (envSession) return envSession;
|
|
32
|
+
|
|
33
|
+
const directSession = typeof payload.session_id === 'string' ? payload.session_id.trim() : '';
|
|
34
|
+
if (directSession) return directSession;
|
|
35
|
+
|
|
36
|
+
const camelSession = typeof payload.sessionId === 'string' ? payload.sessionId.trim() : '';
|
|
37
|
+
if (camelSession) return camelSession;
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getLogPath(sessionId) {
|
|
43
|
+
const logsDir = join(resolveDataDir(), 'logs', 'session-activity');
|
|
44
|
+
mkdirSync(logsDir, { recursive: true });
|
|
45
|
+
return join(logsDir, `${sessionId}.jsonl`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isSkillSuccess(payload) {
|
|
49
|
+
if (payload.is_error === true) return false;
|
|
50
|
+
|
|
51
|
+
const response = payload.tool_response;
|
|
52
|
+
if (response && typeof response === 'object') {
|
|
53
|
+
if (response.success === false) return false;
|
|
54
|
+
if (response.is_error === true) return false;
|
|
55
|
+
if (response.error) return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const output = payload.tool_output;
|
|
59
|
+
if (output && typeof output === 'object') {
|
|
60
|
+
if (output.is_error === true) return false;
|
|
61
|
+
if (output.error) return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isPlainObject(v) {
|
|
68
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function extractLocalMutation(toolName, response) {
|
|
72
|
+
if (!isPlainObject(response)) return {};
|
|
73
|
+
const out = {};
|
|
74
|
+
if ('originalFile' in response) {
|
|
75
|
+
const v = response.originalFile;
|
|
76
|
+
if (typeof v === 'string' || v === null) out.originalFile = v;
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(response.structuredPatch)) {
|
|
79
|
+
out.structuredPatch = response.structuredPatch;
|
|
80
|
+
}
|
|
81
|
+
if (toolName === 'Write' && typeof response.content === 'string') {
|
|
82
|
+
out.writtenContent = response.content;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function payloadToMutation(data) {
|
|
88
|
+
const out = {};
|
|
89
|
+
if (data.degraded === true) out.diffDegraded = true;
|
|
90
|
+
if (typeof data.originalFile === 'string' || data.originalFile === null) {
|
|
91
|
+
out.originalFile = data.originalFile;
|
|
92
|
+
}
|
|
93
|
+
if (Array.isArray(data.structuredPatch)) {
|
|
94
|
+
out.structuredPatch = data.structuredPatch;
|
|
95
|
+
}
|
|
96
|
+
if (typeof data.writtenContent === 'string') {
|
|
97
|
+
out.writtenContent = data.writtenContent;
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function readSidebandDiff(toolUseId) {
|
|
103
|
+
if (!toolUseId || !TOOL_USE_ID_RE.test(toolUseId)) return {};
|
|
104
|
+
const p = join(diffMarkersDir(), `${toolUseId}.json`);
|
|
105
|
+
let raw;
|
|
106
|
+
try {
|
|
107
|
+
raw = readFileSync(p, 'utf8');
|
|
108
|
+
} catch {
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
try { unlinkSync(p); } catch { /* ignore */ }
|
|
112
|
+
try {
|
|
113
|
+
const data = JSON.parse(raw);
|
|
114
|
+
return payloadToMutation(data);
|
|
115
|
+
} catch {
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function extractRemoteMutation(response, toolUseId) {
|
|
121
|
+
const side = readSidebandDiff(toolUseId);
|
|
122
|
+
if (Object.keys(side).length > 0) return side;
|
|
123
|
+
|
|
124
|
+
if (!Array.isArray(response)) return {};
|
|
125
|
+
for (const block of response) {
|
|
126
|
+
if (!isPlainObject(block) || block.type !== 'text' || typeof block.text !== 'string') continue;
|
|
127
|
+
const m = block.text.match(DIFF_MARKER_RE);
|
|
128
|
+
if (!m) continue;
|
|
129
|
+
let data;
|
|
130
|
+
try { data = JSON.parse(m[1]); } catch { return {}; }
|
|
131
|
+
return payloadToMutation(data);
|
|
132
|
+
}
|
|
133
|
+
return {};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function toRecord(payload, sessionId) {
|
|
137
|
+
const toolName = payload.tool_name;
|
|
138
|
+
if (toolName === 'Read') {
|
|
139
|
+
const filePath = payload.tool_input?.file_path;
|
|
140
|
+
if (!filePath) return null;
|
|
141
|
+
return {
|
|
142
|
+
ts: new Date().toISOString(),
|
|
143
|
+
session_id: sessionId,
|
|
144
|
+
tool: 'Read',
|
|
145
|
+
event: 'read_file',
|
|
146
|
+
file_path: resolve(filePath),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (toolName === 'Edit') {
|
|
151
|
+
const filePath = payload.tool_input?.file_path;
|
|
152
|
+
if (!filePath) return null;
|
|
153
|
+
return {
|
|
154
|
+
ts: new Date().toISOString(),
|
|
155
|
+
session_id: sessionId,
|
|
156
|
+
tool: 'Edit',
|
|
157
|
+
event: 'edit_file',
|
|
158
|
+
file_path: resolve(filePath),
|
|
159
|
+
...extractLocalMutation('Edit', payload.tool_response),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (toolName === 'Write') {
|
|
164
|
+
const filePath = payload.tool_input?.file_path;
|
|
165
|
+
if (!filePath) return null;
|
|
166
|
+
const mutation = extractLocalMutation('Write', payload.tool_response);
|
|
167
|
+
if (mutation.writtenContent === undefined && typeof payload.tool_input?.content === 'string') {
|
|
168
|
+
mutation.writtenContent = payload.tool_input.content;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
ts: new Date().toISOString(),
|
|
172
|
+
session_id: sessionId,
|
|
173
|
+
tool: 'Write',
|
|
174
|
+
event: 'write_file',
|
|
175
|
+
file_path: resolve(filePath),
|
|
176
|
+
...mutation,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (toolName === MCP_REMOTE_EDIT_TOOL || toolName === MCP_REMOTE_WRITE_TOOL) {
|
|
181
|
+
const filePath = payload.tool_input?.file_path;
|
|
182
|
+
if (!filePath) return null;
|
|
183
|
+
const isEdit = toolName === MCP_REMOTE_EDIT_TOOL;
|
|
184
|
+
return {
|
|
185
|
+
ts: new Date().toISOString(),
|
|
186
|
+
session_id: sessionId,
|
|
187
|
+
tool: isEdit ? 'Edit' : 'Write',
|
|
188
|
+
event: isEdit ? 'edit_file' : 'write_file',
|
|
189
|
+
file_path: filePath,
|
|
190
|
+
device: payload.tool_input?.device,
|
|
191
|
+
...extractRemoteMutation(payload.tool_response, payload.tool_use_id),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (toolName === 'Skill') {
|
|
196
|
+
const skill = payload.tool_input?.skill;
|
|
197
|
+
if (!skill) return null;
|
|
198
|
+
if (!isSkillSuccess(payload)) return null;
|
|
199
|
+
return {
|
|
200
|
+
ts: new Date().toISOString(),
|
|
201
|
+
session_id: sessionId,
|
|
202
|
+
tool: 'Skill',
|
|
203
|
+
event: 'skill_use',
|
|
204
|
+
skill: skill.toLowerCase(),
|
|
205
|
+
success: true,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function appendRecord(record) {
|
|
213
|
+
const logPath = getLogPath(record.session_id);
|
|
214
|
+
appendFileSync(logPath, `${JSON.stringify(record)}\n`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function processPayload(payload) {
|
|
218
|
+
const sessionId = resolveSessionId(payload);
|
|
219
|
+
if (!sessionId) return;
|
|
220
|
+
|
|
221
|
+
const record = toRecord(payload, sessionId);
|
|
222
|
+
if (!record) return;
|
|
223
|
+
|
|
224
|
+
appendRecord(record);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function main() {
|
|
228
|
+
let input = '';
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
input = readFileSync(0, 'utf8');
|
|
232
|
+
} catch {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (!input.trim()) return;
|
|
237
|
+
|
|
238
|
+
let payload;
|
|
239
|
+
try {
|
|
240
|
+
payload = JSON.parse(input);
|
|
241
|
+
} catch {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
processPayload(payload);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Run directly when invoked as a hook script
|
|
249
|
+
const isMain = process.argv[1] && (
|
|
250
|
+
resolve(process.argv[1]) === resolve(fileURLToPath(import.meta.url))
|
|
251
|
+
);
|
|
252
|
+
if (isMain) {
|
|
253
|
+
main();
|
|
254
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin HookContext, argv [project, taskId]
|
|
3
|
+
// output: HookResult — checks TASKS.yaml task status after thread ends
|
|
4
|
+
// pos: task-dispatch thread onEnd status check hook
|
|
5
|
+
// >>> If I am updated, be sure to update my header comment and the CORTEX.md in the same folder <<<
|
|
6
|
+
|
|
7
|
+
import { readFileSync, existsSync, readdirSync } from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
import { homedir } from 'os';
|
|
11
|
+
|
|
12
|
+
const DATA_DIR = process.env.CORTEX_HOME
|
|
13
|
+
? path.resolve(process.env.CORTEX_HOME)
|
|
14
|
+
: path.join(homedir(), '.cortex');
|
|
15
|
+
const PROJECTS_DIR = process.env.CORTEX_PROJECTS_DIR
|
|
16
|
+
? path.resolve(process.env.CORTEX_PROJECTS_DIR)
|
|
17
|
+
: path.join(DATA_DIR, 'context', 'projects');
|
|
18
|
+
|
|
19
|
+
const CORTEX_ROOT = process.env.CORTEX_ROOT
|
|
20
|
+
? path.resolve(process.env.CORTEX_ROOT)
|
|
21
|
+
: path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
22
|
+
const CORTEX_RUN_DIR = path.join(CORTEX_ROOT, 'tmp', 'cortex-run');
|
|
23
|
+
|
|
24
|
+
function noop() {
|
|
25
|
+
console.log(JSON.stringify({ insertAgent: false }));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Parse a single TASKS.yaml task block into an object.
|
|
30
|
+
* A task block starts with " - id:" and spans until the next " - id:" or EOF.
|
|
31
|
+
* We only extract fields we care about: id, status, claimed-by, blocked-by.
|
|
32
|
+
*/
|
|
33
|
+
function findTaskById(content, taskId) {
|
|
34
|
+
const lines = content.split('\n');
|
|
35
|
+
let inBlock = false;
|
|
36
|
+
let blockStart = -1;
|
|
37
|
+
|
|
38
|
+
// Find the task block containing our taskId
|
|
39
|
+
for (let i = 0; i < lines.length; i++) {
|
|
40
|
+
const line = lines[i];
|
|
41
|
+
const idMatch = line.match(/^\s+- id:\s*(.+)$/);
|
|
42
|
+
if (idMatch) {
|
|
43
|
+
// Extract the ID value (handle both quoted and unquoted)
|
|
44
|
+
const thisId = idMatch[1].trim().replace(/^["']|["']$/g, '');
|
|
45
|
+
if (thisId === taskId) {
|
|
46
|
+
inBlock = true;
|
|
47
|
+
blockStart = i;
|
|
48
|
+
} else if (inBlock) {
|
|
49
|
+
// We were in our block but hit the next task — stop
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
} else if (inBlock && /^\s+- id:/.test(line)) {
|
|
53
|
+
// Fallback: next task block starts
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (blockStart === -1) return null;
|
|
59
|
+
|
|
60
|
+
// Parse fields from the task block
|
|
61
|
+
const task = { id: taskId };
|
|
62
|
+
for (let i = blockStart + 1; i < lines.length; i++) {
|
|
63
|
+
const line = lines[i];
|
|
64
|
+
// Stop at next top-level key or next task
|
|
65
|
+
if (/^\S/.test(line) && !/^\s/.test(line)) break;
|
|
66
|
+
if (/^\s+- id:/.test(line)) break;
|
|
67
|
+
|
|
68
|
+
const fieldMatch = line.match(/^\s{4,}([a-zA-Z_-]+):\s*(.*)$/);
|
|
69
|
+
if (fieldMatch) {
|
|
70
|
+
const key = fieldMatch[1];
|
|
71
|
+
let value = fieldMatch[2].trim();
|
|
72
|
+
// Unquote string values
|
|
73
|
+
value = value.replace(/^["']|["']$/g, '');
|
|
74
|
+
// Handle boolean/null
|
|
75
|
+
if (value === 'true') value = true;
|
|
76
|
+
else if (value === 'false') value = false;
|
|
77
|
+
else if (value === 'null' || value === '') value = null;
|
|
78
|
+
task[key] = value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return task;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Find a cortex-run state file that references the given task. */
|
|
86
|
+
function findStateFileForTask(project, taskId) {
|
|
87
|
+
try {
|
|
88
|
+
if (!existsSync(CORTEX_RUN_DIR)) return null;
|
|
89
|
+
for (const entry of readdirSync(CORTEX_RUN_DIR, { withFileTypes: true })) {
|
|
90
|
+
if (!entry.isDirectory()) continue;
|
|
91
|
+
const statePath = path.join(CORTEX_RUN_DIR, entry.name, 'state.json');
|
|
92
|
+
if (!existsSync(statePath)) continue;
|
|
93
|
+
try {
|
|
94
|
+
const state = JSON.parse(readFileSync(statePath, 'utf8'));
|
|
95
|
+
if (state.task_project === project && state.task_id === taskId) {
|
|
96
|
+
return { runName: entry.name, ...state };
|
|
97
|
+
}
|
|
98
|
+
} catch {}
|
|
99
|
+
}
|
|
100
|
+
} catch {}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Check if a PID is still alive. */
|
|
105
|
+
function pidAlive(pid) {
|
|
106
|
+
try {
|
|
107
|
+
process.kill(pid, 0);
|
|
108
|
+
return true;
|
|
109
|
+
} catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function main() {
|
|
115
|
+
let input = '';
|
|
116
|
+
for await (const chunk of process.stdin) input += chunk;
|
|
117
|
+
const ctx = input.trim() ? JSON.parse(input) : {};
|
|
118
|
+
const [project, taskId] = process.argv.slice(2);
|
|
119
|
+
|
|
120
|
+
if (!project || !taskId) { noop(); return; }
|
|
121
|
+
|
|
122
|
+
const tasksPath = path.join(PROJECTS_DIR, project, 'TASKS.yaml');
|
|
123
|
+
if (!existsSync(tasksPath)) { noop(); return; }
|
|
124
|
+
|
|
125
|
+
let content;
|
|
126
|
+
try {
|
|
127
|
+
content = readFileSync(tasksPath, 'utf8');
|
|
128
|
+
} catch {
|
|
129
|
+
noop();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const task = findTaskById(content, taskId);
|
|
134
|
+
if (!task) { noop(); return; }
|
|
135
|
+
|
|
136
|
+
// Task is done — nothing to do.
|
|
137
|
+
if (task.status === 'done') { noop(); return; }
|
|
138
|
+
|
|
139
|
+
// Task is pending — cortex-run is handling it. No recovery; just verify.
|
|
140
|
+
if (task.status === 'pending') {
|
|
141
|
+
const stateFile = findStateFileForTask(project, taskId);
|
|
142
|
+
if (!stateFile || stateFile.status === 'failed') {
|
|
143
|
+
// cortex-run state file missing or run failed — but per design, do not auto-recover.
|
|
144
|
+
// Just report so it's visible.
|
|
145
|
+
const targetAgent = ctx.previousAgent;
|
|
146
|
+
if (targetAgent) {
|
|
147
|
+
const note = !stateFile
|
|
148
|
+
? 'no state file found (cortex-run may not have started)'
|
|
149
|
+
: `state file shows status=${stateFile.status} (exit ${stateFile.exit_code})`;
|
|
150
|
+
console.log(JSON.stringify({
|
|
151
|
+
insertAgent: false,
|
|
152
|
+
targetAgent,
|
|
153
|
+
prompt: [
|
|
154
|
+
`FYI: task [id: ${taskId}] in project "${project}" is still [pending] but cortex-run ${note}.`,
|
|
155
|
+
'Per current policy, pending→done recovery is not automatic. Check manually if needed.',
|
|
156
|
+
].join('\n'),
|
|
157
|
+
}));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
noop();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Task is blocked — nothing to do.
|
|
166
|
+
if (task['blocked-by']) { noop(); return; }
|
|
167
|
+
|
|
168
|
+
// Task is still claimed — thread ended but task not transitioned.
|
|
169
|
+
if (task['claimed-by']) {
|
|
170
|
+
const targetAgent = ctx.previousAgent;
|
|
171
|
+
if (!targetAgent) { noop(); return; }
|
|
172
|
+
|
|
173
|
+
// Check if there's a cortex-run process handling this task
|
|
174
|
+
const stateFile = findStateFileForTask(project, taskId);
|
|
175
|
+
|
|
176
|
+
if (stateFile && stateFile.status === 'running') {
|
|
177
|
+
// cortex-run is running — this is a legitimate pending case.
|
|
178
|
+
// Engineer forgot to call cortex-task pending; auto-transition.
|
|
179
|
+
console.log(JSON.stringify({
|
|
180
|
+
insertAgent: false,
|
|
181
|
+
targetAgent,
|
|
182
|
+
prompt: [
|
|
183
|
+
`Auto-detected: task [id: ${taskId}] in project "${project}" is still claimed,`,
|
|
184
|
+
`but a cortex-run process "(${stateFile.runName})" is running for this task (PID ${stateFile.pid}).`,
|
|
185
|
+
`The engineer likely forgot to mark the task pending. Call:`,
|
|
186
|
+
` cortex-task pending --project ${project} --task-id ${taskId}`,
|
|
187
|
+
].join('\n'),
|
|
188
|
+
}));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// No cortex-run state file — genuine orphan.
|
|
193
|
+
// Unclaim and prompt agent to resolve.
|
|
194
|
+
console.log(JSON.stringify({
|
|
195
|
+
insertAgent: false,
|
|
196
|
+
targetAgent,
|
|
197
|
+
prompt: [
|
|
198
|
+
`Status check: task [id: ${taskId}] in project "${project}" is still marked [claimed] in TASKS.yaml, but the thread has finished. No running cortex-run process found for this task.`,
|
|
199
|
+
'',
|
|
200
|
+
'Resolve the task status now — do not leave it in limbo:',
|
|
201
|
+
`- If done-when conditions are satisfied: cortex-task complete --project ${project} --task-id ${taskId} --note "<what you did>"`,
|
|
202
|
+
`- If blocked by external reasons: cortex-task block --project ${project} --task-id ${taskId} --reason "<reason>"`,
|
|
203
|
+
`- If a cortex-run is still running: cortex-task pending --project ${project} --task-id ${taskId}`,
|
|
204
|
+
`- If none of the above apply: cortex-task unclaim --project ${project} --task-id ${taskId}`,
|
|
205
|
+
'',
|
|
206
|
+
'After updating the task, commit the TASKS.yaml change.',
|
|
207
|
+
].join('\n'),
|
|
208
|
+
}));
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
noop();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
main().catch((err) => {
|
|
216
|
+
console.error(`[task-status-check] Fatal: ${err.message}`);
|
|
217
|
+
console.log(JSON.stringify({ insertAgent: false }));
|
|
218
|
+
});
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin JSON — Claude Code PreToolUse event payload
|
|
3
|
+
// output: stdout JSON — { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision, permissionDecisionReason } }
|
|
4
|
+
// pos: PreToolUse hook — intercepts Edit/Write on **/TASKS.yaml, checks project lock
|
|
5
|
+
// allow when current process holds the lock; deny + helpful message otherwise
|
|
6
|
+
// Owner identity: process.env.CORTEX_EXECUTION_ID ?? manual:<user>:<pid>
|
|
7
|
+
// >>> If I am updated, be sure to update my header comment and the CORTEX.md in the same folder <<<
|
|
8
|
+
|
|
9
|
+
import { readFileSync, existsSync } from 'fs';
|
|
10
|
+
import { basename, resolve } from 'path';
|
|
11
|
+
import { userInfo } from 'os';
|
|
12
|
+
|
|
13
|
+
// ── Constants ──
|
|
14
|
+
|
|
15
|
+
const TASKS_YAML = 'TASKS.yaml';
|
|
16
|
+
const TOOLS = new Set(['Edit', 'Write']);
|
|
17
|
+
|
|
18
|
+
// ── Helpers ──
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get the current owner identity.
|
|
22
|
+
* Priority: CORTEX_EXECUTION_ID env var > manual:<user>:<pid>
|
|
23
|
+
*/
|
|
24
|
+
function getOwnerIdentity() {
|
|
25
|
+
if (process.env.CORTEX_EXECUTION_ID) return process.env.CORTEX_EXECUTION_ID;
|
|
26
|
+
let user;
|
|
27
|
+
try {
|
|
28
|
+
user = userInfo().username;
|
|
29
|
+
} catch {
|
|
30
|
+
user = process.env.USER || 'unknown';
|
|
31
|
+
}
|
|
32
|
+
return `manual:${user}:${process.pid}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse the `lock:` top-level field from a TASKS.yaml string.
|
|
37
|
+
* Returns null if no lock section or if YAML is unparseable.
|
|
38
|
+
*
|
|
39
|
+
* Lock format:
|
|
40
|
+
* lock:
|
|
41
|
+
* owner: <string>
|
|
42
|
+
* acquired_at: <ISO string>
|
|
43
|
+
* expires_at: <ISO string>
|
|
44
|
+
* note?: <string>
|
|
45
|
+
*/
|
|
46
|
+
function parseLock(content) {
|
|
47
|
+
const lines = content.split('\n');
|
|
48
|
+
let inLock = false;
|
|
49
|
+
let lockIndent = 0;
|
|
50
|
+
const lock = {};
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < lines.length; i++) {
|
|
53
|
+
const line = lines[i];
|
|
54
|
+
const trimmed = line.trimEnd();
|
|
55
|
+
|
|
56
|
+
// Detect top-level `lock:` key (at column 0)
|
|
57
|
+
if (!inLock) {
|
|
58
|
+
const lockMatch = trimmed.match(/^lock:\s*(.*)$/);
|
|
59
|
+
if (lockMatch) {
|
|
60
|
+
inLock = true;
|
|
61
|
+
lockIndent = line.length - trimmed.length; // leading whitespace
|
|
62
|
+
// If inline value after lock: (e.g. lock: null), skip
|
|
63
|
+
if (lockMatch[1].trim() && lockMatch[1].trim() !== '') {
|
|
64
|
+
// Inline value — not an object, so no lock
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// We're in the lock block. Check if this line is still part of it.
|
|
73
|
+
const leadingSpace = line.length - line.trimStart().length;
|
|
74
|
+
|
|
75
|
+
// Empty line — skip
|
|
76
|
+
if (trimmed === '') continue;
|
|
77
|
+
|
|
78
|
+
// Line at same or lesser indent than the `lock:` key → lock block is done
|
|
79
|
+
if (leadingSpace <= lockIndent && line.trim()) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Parse key: value within lock block
|
|
84
|
+
const kvMatch = trimmed.match(/^(\s*)([a-zA-Z_-]+):\s*(.*)$/);
|
|
85
|
+
if (kvMatch) {
|
|
86
|
+
const key = kvMatch[2];
|
|
87
|
+
let value = kvMatch[3].trim();
|
|
88
|
+
// Unquote strings
|
|
89
|
+
value = value.replace(/^["']|["']$/g, '');
|
|
90
|
+
lock[key] = value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Validate required fields
|
|
95
|
+
if (lock.owner && lock.acquired_at && lock.expires_at) {
|
|
96
|
+
return { owner: lock.owner, acquired_at: lock.acquired_at, expires_at: lock.expires_at };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Build a friendly deny message with cortex-task usage examples.
|
|
104
|
+
*/
|
|
105
|
+
function buildDenyMessage(lock, owner) {
|
|
106
|
+
const lines = [
|
|
107
|
+
'Direct Edit/Write of TASKS.yaml is not allowed without holding the project lock.',
|
|
108
|
+
'',
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
if (!lock) {
|
|
112
|
+
lines.push('No lock is currently held on this TASKS.yaml.');
|
|
113
|
+
} else {
|
|
114
|
+
const expiresAt = new Date(lock.expires_at);
|
|
115
|
+
if (expiresAt <= new Date()) {
|
|
116
|
+
lines.push(`The lock expired at ${lock.expires_at}. The previous owner was: ${lock.owner}`);
|
|
117
|
+
} else {
|
|
118
|
+
lines.push(`Lock is held by: ${lock.owner} (current identity: ${owner})`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
lines.push(
|
|
123
|
+
'',
|
|
124
|
+
'Use the cortex-task CLI to safely modify tasks:',
|
|
125
|
+
' cortex-task lock-acquire --project <project> Acquire the project lock',
|
|
126
|
+
' cortex-task add --project <project> Add a new task',
|
|
127
|
+
' cortex-task edit --project <project> --task-id <id> Edit an existing task',
|
|
128
|
+
' cortex-task lock-release --project <project> Release the lock when done',
|
|
129
|
+
'',
|
|
130
|
+
'The cortex-task CLI handles lock checking automatically.',
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
return lines.join('\n');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Check lock state and return decision.
|
|
138
|
+
*/
|
|
139
|
+
function checkLock(absPath, owner) {
|
|
140
|
+
let content;
|
|
141
|
+
try {
|
|
142
|
+
content = readFileSync(absPath, 'utf8');
|
|
143
|
+
} catch {
|
|
144
|
+
// File doesn't exist or can't be read — not our concern
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const lock = parseLock(content);
|
|
149
|
+
|
|
150
|
+
// No lock → deny (with helpful message to acquire one)
|
|
151
|
+
if (!lock) {
|
|
152
|
+
return {
|
|
153
|
+
permissionDecision: 'deny',
|
|
154
|
+
permissionDecisionReason: buildDenyMessage(null, owner),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Check expiry
|
|
159
|
+
const expiresAt = new Date(lock.expires_at);
|
|
160
|
+
if (isNaN(expiresAt.getTime()) || expiresAt <= new Date()) {
|
|
161
|
+
return {
|
|
162
|
+
permissionDecision: 'deny',
|
|
163
|
+
permissionDecisionReason: buildDenyMessage(lock, owner),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Lock held by different owner
|
|
168
|
+
if (lock.owner !== owner) {
|
|
169
|
+
return {
|
|
170
|
+
permissionDecision: 'deny',
|
|
171
|
+
permissionDecisionReason: buildDenyMessage(lock, owner),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Lock held by current owner
|
|
176
|
+
return {
|
|
177
|
+
permissionDecision: 'allow',
|
|
178
|
+
permissionDecisionReason: `Lock held by ${owner} (acquired ${lock.acquired_at}, expires ${lock.expires_at}) — proceed with edit.`,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ── Main ──
|
|
183
|
+
|
|
184
|
+
function main() {
|
|
185
|
+
let input = '';
|
|
186
|
+
try {
|
|
187
|
+
input = readFileSync(0, 'utf8');
|
|
188
|
+
} catch {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (!input.trim()) return;
|
|
193
|
+
|
|
194
|
+
let payload;
|
|
195
|
+
try {
|
|
196
|
+
payload = JSON.parse(input);
|
|
197
|
+
} catch {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Only intercept Edit and Write tools
|
|
202
|
+
const toolName = payload.tool_name;
|
|
203
|
+
if (!TOOLS.has(toolName)) return;
|
|
204
|
+
|
|
205
|
+
// Extract file path
|
|
206
|
+
const toolInput = payload.tool_input || {};
|
|
207
|
+
const filePath = toolInput.file_path || '';
|
|
208
|
+
if (!filePath) return;
|
|
209
|
+
|
|
210
|
+
const cwd = payload.cwd || process.cwd();
|
|
211
|
+
const absPath = resolve(cwd, filePath);
|
|
212
|
+
|
|
213
|
+
// Only guard TASKS.yaml files
|
|
214
|
+
if (basename(absPath) !== TASKS_YAML) return;
|
|
215
|
+
|
|
216
|
+
const owner = getOwnerIdentity();
|
|
217
|
+
const decision = checkLock(absPath, owner);
|
|
218
|
+
|
|
219
|
+
if (!decision) return;
|
|
220
|
+
|
|
221
|
+
process.stdout.write(JSON.stringify({
|
|
222
|
+
hookSpecificOutput: {
|
|
223
|
+
hookEventName: 'PreToolUse',
|
|
224
|
+
permissionDecision: decision.permissionDecision,
|
|
225
|
+
permissionDecisionReason: decision.permissionDecisionReason,
|
|
226
|
+
},
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
main();
|