@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,212 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin JSON — Claude Code PostToolUse event or PI hook-bridge payload
|
|
3
|
+
// output: { matched: [...], hookSpecificOutput: { additionalContext: "..." } }
|
|
4
|
+
// pos: Read/Grep hook — check if file path matches scoped rules in ~/.cortex/rules/
|
|
5
|
+
// On match, return rule content for Claude (additionalContext) and PI (content mutation) injection
|
|
6
|
+
// Session-level dedup: each rule injected at most once per session (on first match)
|
|
7
|
+
// >>> If I am updated, be sure to update my header comment and the CORTEX.md in the same folder <<<
|
|
8
|
+
|
|
9
|
+
import { readFileSync, readdirSync, statSync, existsSync, mkdirSync, renameSync, writeFileSync } from 'fs';
|
|
10
|
+
import { join, resolve, dirname } from 'path';
|
|
11
|
+
import { homedir } from 'os';
|
|
12
|
+
|
|
13
|
+
const DATA_DIR = process.env.CORTEX_HOME
|
|
14
|
+
? resolve(process.env.CORTEX_HOME)
|
|
15
|
+
: join(homedir(), '.cortex');
|
|
16
|
+
const RULES_DIR = join(DATA_DIR, 'rules');
|
|
17
|
+
const CACHE_DIR = join(DATA_DIR, 'tmp', 'rules-cache');
|
|
18
|
+
const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
19
|
+
|
|
20
|
+
const FRONTMATTER_RE = /^---\n([\s\S]*?)\n---\n?/;
|
|
21
|
+
const PATH_LINE_RE = /^\s*-\s+["']?(.+?)["']?\s*$/;
|
|
22
|
+
const SESSION_ID_RE = /^[A-Za-z0-9._-]+$/;
|
|
23
|
+
|
|
24
|
+
// ── cache helpers ──
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Load the dedup cache for a session. Returns a Set of already-injected rule file names.
|
|
28
|
+
*/
|
|
29
|
+
function loadCache(sessionId) {
|
|
30
|
+
const cache = new Set();
|
|
31
|
+
if (!sessionId || !SESSION_ID_RE.test(sessionId)) return cache;
|
|
32
|
+
const cacheFile = join(CACHE_DIR, `${sessionId}.json`);
|
|
33
|
+
try {
|
|
34
|
+
if (!existsSync(cacheFile)) return cache;
|
|
35
|
+
const raw = readFileSync(cacheFile, 'utf8');
|
|
36
|
+
const data = JSON.parse(raw);
|
|
37
|
+
if (Array.isArray(data)) {
|
|
38
|
+
for (const f of data) {
|
|
39
|
+
if (typeof f === 'string') cache.add(f);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch { /* corrupt/unreadable — start fresh */ }
|
|
43
|
+
return cache;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Persist the dedup cache for a session. Atomic write via tmp file + rename.
|
|
48
|
+
*/
|
|
49
|
+
function saveCache(sessionId, cache) {
|
|
50
|
+
if (!sessionId || !SESSION_ID_RE.test(sessionId)) return;
|
|
51
|
+
try {
|
|
52
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
53
|
+
const cacheFile = join(CACHE_DIR, `${sessionId}.json`);
|
|
54
|
+
const arr = Array.from(cache);
|
|
55
|
+
const tmp = `${cacheFile}.${process.pid}.tmp`;
|
|
56
|
+
writeFileSync(tmp, JSON.stringify(arr), 'utf8');
|
|
57
|
+
renameSync(tmp, cacheFile);
|
|
58
|
+
} catch { /* disk full etc. — degrade gracefully */ }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Remove stale session cache files (> CACHE_TTL_MS old). Best-effort.
|
|
63
|
+
*/
|
|
64
|
+
function maintainCacheDir() {
|
|
65
|
+
try {
|
|
66
|
+
if (!existsSync(CACHE_DIR)) return;
|
|
67
|
+
const now = Date.now();
|
|
68
|
+
for (const name of readdirSync(CACHE_DIR)) {
|
|
69
|
+
const p = join(CACHE_DIR, name);
|
|
70
|
+
try {
|
|
71
|
+
const st = statSync(p);
|
|
72
|
+
if (!st.isFile()) continue;
|
|
73
|
+
if (now - st.mtimeMs > CACHE_TTL_MS) {
|
|
74
|
+
try { require('fs').rmSync(p, { force: true }); } catch { /* ignore */ }
|
|
75
|
+
}
|
|
76
|
+
} catch { /* ignore */ }
|
|
77
|
+
}
|
|
78
|
+
} catch { /* ignore */ }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── rule parsing ──
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Parse YAML frontmatter from rule content.
|
|
85
|
+
* Returns { paths, body } where paths is the `paths:` array and body is text after frontmatter.
|
|
86
|
+
*/
|
|
87
|
+
function parseFrontmatter(content) {
|
|
88
|
+
const m = content.match(FRONTMATTER_RE);
|
|
89
|
+
if (!m) return { paths: [], body: content };
|
|
90
|
+
const fmBlock = m[1];
|
|
91
|
+
const body = content.slice(m[0].length);
|
|
92
|
+
const paths = [];
|
|
93
|
+
for (const line of fmBlock.split('\n')) {
|
|
94
|
+
const pm = line.match(PATH_LINE_RE);
|
|
95
|
+
if (pm) paths.push(pm[1]);
|
|
96
|
+
}
|
|
97
|
+
return { paths, body };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Load all scoped rules (those with non-empty `paths` frontmatter).
|
|
102
|
+
*/
|
|
103
|
+
function loadScopedRules() {
|
|
104
|
+
const rules = [];
|
|
105
|
+
let files;
|
|
106
|
+
try { files = readdirSync(RULES_DIR); } catch { return rules; }
|
|
107
|
+
for (const f of files) {
|
|
108
|
+
if (!f.endsWith('.md')) continue;
|
|
109
|
+
const fp = join(RULES_DIR, f);
|
|
110
|
+
try {
|
|
111
|
+
const st = statSync(fp);
|
|
112
|
+
if (!st.isFile()) continue;
|
|
113
|
+
const content = readFileSync(fp, 'utf8');
|
|
114
|
+
const { paths, body } = parseFrontmatter(content);
|
|
115
|
+
if (paths.length === 0) continue;
|
|
116
|
+
rules.push({ file: f, paths, body, mtimeMs: st.mtimeMs });
|
|
117
|
+
} catch { /* skip unreadable */ }
|
|
118
|
+
}
|
|
119
|
+
return rules;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Simple glob-to-regex. Supports ** (multi-segment) and * (single segment).
|
|
124
|
+
*/
|
|
125
|
+
function globToRegex(pattern) {
|
|
126
|
+
const escaped = pattern
|
|
127
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
128
|
+
.replace(/\*\*/g, '\x00GLOBSTAR\x00')
|
|
129
|
+
.replace(/\*/g, '[^/]*')
|
|
130
|
+
.replace(/\x00GLOBSTAR\x00/g, '.*');
|
|
131
|
+
return new RegExp(escaped);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Match a file path against scoped rules' glob patterns.
|
|
136
|
+
* Returns array of { file, body } for matched rules.
|
|
137
|
+
*/
|
|
138
|
+
function matchRules(filePath, scopedRules) {
|
|
139
|
+
const matched = [];
|
|
140
|
+
for (const rule of scopedRules) {
|
|
141
|
+
for (const pattern of rule.paths) {
|
|
142
|
+
if (globToRegex(pattern).test(filePath)) {
|
|
143
|
+
matched.push({ file: rule.file, body: rule.body });
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return matched;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── main ──
|
|
152
|
+
|
|
153
|
+
function main() {
|
|
154
|
+
let input = '';
|
|
155
|
+
try {
|
|
156
|
+
input = readFileSync(0, 'utf8');
|
|
157
|
+
} catch {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!input.trim()) return;
|
|
162
|
+
|
|
163
|
+
let payload;
|
|
164
|
+
try {
|
|
165
|
+
payload = JSON.parse(input);
|
|
166
|
+
} catch {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const toolName = payload.tool_name;
|
|
171
|
+
if (toolName !== 'Read' && toolName !== 'Grep') return;
|
|
172
|
+
|
|
173
|
+
const filePath = payload.tool_input?.file_path || payload.tool_input?.path;
|
|
174
|
+
if (!filePath) return;
|
|
175
|
+
|
|
176
|
+
const scopedRules = loadScopedRules();
|
|
177
|
+
if (scopedRules.length === 0) return;
|
|
178
|
+
|
|
179
|
+
const matched = matchRules(filePath, scopedRules);
|
|
180
|
+
if (matched.length === 0) return;
|
|
181
|
+
|
|
182
|
+
// ── session dedup ──
|
|
183
|
+
const sessionId = payload.session_id;
|
|
184
|
+
maintainCacheDir();
|
|
185
|
+
const cache = loadCache(sessionId);
|
|
186
|
+
const newMatched = matched.filter(r => !cache.has(r.file));
|
|
187
|
+
|
|
188
|
+
// Update cache with all matched rules (even already-seen ones, to refresh mtime)
|
|
189
|
+
for (const r of matched) cache.add(r.file);
|
|
190
|
+
saveCache(sessionId, cache);
|
|
191
|
+
|
|
192
|
+
if (newMatched.length === 0) return;
|
|
193
|
+
|
|
194
|
+
// Build system-reminder blocks for Claude's additionalContext
|
|
195
|
+
const blocks = newMatched.map(r =>
|
|
196
|
+
`<system-reminder>\nApplied rule from ~/.cortex/rules/${r.file}:\n\n${r.body}\n</system-reminder>`
|
|
197
|
+
);
|
|
198
|
+
const context = blocks.join('\n\n');
|
|
199
|
+
|
|
200
|
+
// Output served to both backends:
|
|
201
|
+
// .matched → PI hook-bridge reads this for content mutation
|
|
202
|
+
// .hookSpecificOutput → Claude Code reads this for additionalContext
|
|
203
|
+
process.stdout.write(JSON.stringify({
|
|
204
|
+
matched: newMatched,
|
|
205
|
+
hookSpecificOutput: {
|
|
206
|
+
hookEventName: 'PostToolUse',
|
|
207
|
+
additionalContext: context,
|
|
208
|
+
},
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
main();
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// input: stdin Claude Code PreToolUse event, node:fs
|
|
3
|
+
// output: Direct-write target file + deny to prevent Claude from re-running built-in Edit/Write
|
|
4
|
+
// pos: PreToolUse hook: bypass Claude Code's hardcoded protection on .claude/ paths
|
|
5
|
+
// >>> If I am updated, be sure to update my header comment and the CLAUDE.md in the same folder <<<
|
|
6
|
+
|
|
7
|
+
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
|
|
8
|
+
import { dirname, resolve } from 'path';
|
|
9
|
+
|
|
10
|
+
function readStdin() {
|
|
11
|
+
return new Promise((res, rej) => {
|
|
12
|
+
const chunks = [];
|
|
13
|
+
process.stdin.setEncoding('utf8');
|
|
14
|
+
process.stdin.on('data', (c) => chunks.push(c));
|
|
15
|
+
process.stdin.on('end', () => res(chunks.join('')));
|
|
16
|
+
process.stdin.on('error', rej);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
let input;
|
|
22
|
+
try {
|
|
23
|
+
input = JSON.parse(await readStdin());
|
|
24
|
+
} catch {
|
|
25
|
+
process.exit(0);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const toolName = input.tool_name;
|
|
29
|
+
const toolInput = input.tool_input || {};
|
|
30
|
+
const filePath = toolInput.file_path || '';
|
|
31
|
+
const cwd = input.cwd || process.cwd();
|
|
32
|
+
const absPath = resolve(cwd, filePath);
|
|
33
|
+
|
|
34
|
+
// Only intercept .claude/ paths
|
|
35
|
+
const relFromCwd = absPath.startsWith(cwd + '/') ? absPath.slice(cwd.length + 1) : '';
|
|
36
|
+
if (!relFromCwd.startsWith('.claude/')) {
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let resultMessage;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
if (toolName === 'Edit') {
|
|
44
|
+
const { old_string, new_string, replace_all } = toolInput;
|
|
45
|
+
if (old_string == null || new_string == null) process.exit(0);
|
|
46
|
+
|
|
47
|
+
const content = readFileSync(absPath, 'utf8');
|
|
48
|
+
|
|
49
|
+
if (!content.includes(old_string)) {
|
|
50
|
+
resultMessage = `[BYPASS ERROR — file NOT modified] old_string not found in ${filePath}. Re-read the file and retry.`;
|
|
51
|
+
} else if (!replace_all) {
|
|
52
|
+
const firstIdx = content.indexOf(old_string);
|
|
53
|
+
if (content.indexOf(old_string, firstIdx + 1) !== -1) {
|
|
54
|
+
resultMessage = `[BYPASS ERROR — file NOT modified] old_string is not unique in ${filePath}. Use replace_all or add more surrounding context.`;
|
|
55
|
+
} else {
|
|
56
|
+
writeFileSync(absPath, content.replace(old_string, () => new_string), 'utf8');
|
|
57
|
+
resultMessage = `[BYPASS OK — edit IS applied on disk] ${filePath} was written directly by the sensitive-file-edit hook. The "hook blocking error" wrapper around this message is expected: Claude Code's built-in .claude/ protection blocks Claude's own Edit call, so this hook performs the write itself then returns deny. No retry needed — continue with next step.`;
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
writeFileSync(absPath, content.replaceAll(old_string, new_string), 'utf8');
|
|
61
|
+
resultMessage = `[BYPASS OK — edit IS applied on disk] All occurrences of old_string in ${filePath} were replaced directly by the sensitive-file-edit hook. The "hook blocking error" wrapper is expected — the write succeeded. No retry needed — continue with next step.`;
|
|
62
|
+
}
|
|
63
|
+
} else if (toolName === 'Write') {
|
|
64
|
+
if (toolInput.content == null) process.exit(0);
|
|
65
|
+
mkdirSync(dirname(absPath), { recursive: true });
|
|
66
|
+
writeFileSync(absPath, toolInput.content, 'utf8');
|
|
67
|
+
resultMessage = `[BYPASS OK — file IS written on disk] ${filePath} was written directly by the sensitive-file-edit hook. The "hook blocking error" wrapper is expected — the write succeeded. No retry needed — continue with next step.`;
|
|
68
|
+
} else {
|
|
69
|
+
process.exit(0);
|
|
70
|
+
}
|
|
71
|
+
} catch (err) {
|
|
72
|
+
resultMessage = `[BYPASS ERROR — file NOT modified] Could not write ${filePath}: ${err.message}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
process.stdout.write(JSON.stringify({
|
|
76
|
+
hookSpecificOutput: {
|
|
77
|
+
hookEventName: 'PreToolUse',
|
|
78
|
+
permissionDecision: 'deny',
|
|
79
|
+
permissionDecisionReason: resultMessage,
|
|
80
|
+
},
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
main();
|
|
@@ -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
|
+
}
|