@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,471 @@
|
|
|
1
|
+
// input: projects/<p>/experiments + knowledge + patterns atoms
|
|
2
|
+
// output: JSON lifecycle report (candidates/stale/contradict)
|
|
3
|
+
// pos: knowledge lifecycle detection engine CLI
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import * as fs from 'fs';
|
|
6
|
+
import * as path from 'path';
|
|
7
|
+
import { PROJECTS_DIR, isMainModule } from '../../core/utils.js';
|
|
8
|
+
import { scanAtomicFiles } from './index-regen.js';
|
|
9
|
+
import { createLogger } from '../../core/log.js';
|
|
10
|
+
const log = createLogger('consolidate');
|
|
11
|
+
// --- Helpers ---
|
|
12
|
+
const INACTIVE_STATUSES = ['superseded', 'deprecated', 'invalidated'];
|
|
13
|
+
function isInactiveStatus(status) {
|
|
14
|
+
if (!status)
|
|
15
|
+
return false;
|
|
16
|
+
const s = String(status).toLowerCase();
|
|
17
|
+
return INACTIVE_STATUSES.some(prefix => s.startsWith(prefix));
|
|
18
|
+
}
|
|
19
|
+
function getStatusTarget(status) {
|
|
20
|
+
const m = String(status).match(/^(?:superseded|refined|invalidated|corrected|challenged):(.+)/i);
|
|
21
|
+
return m ? m[1].trim() : null;
|
|
22
|
+
}
|
|
23
|
+
function daysSince(dateStr) {
|
|
24
|
+
if (!dateStr)
|
|
25
|
+
return null;
|
|
26
|
+
const d = new Date(String(dateStr));
|
|
27
|
+
if (isNaN(d.getTime()))
|
|
28
|
+
return null;
|
|
29
|
+
return (Date.now() - d.getTime()) / 86400000;
|
|
30
|
+
}
|
|
31
|
+
// --- Detector 1: Tag co-occurrence (unchanged logic) ---
|
|
32
|
+
function detectTagClusters(projectName, experiments, patterns) {
|
|
33
|
+
const candidates = [];
|
|
34
|
+
const activeExps = experiments.filter(e => !isInactiveStatus(e.meta.status));
|
|
35
|
+
const tagToExps = new Map();
|
|
36
|
+
for (const exp of activeExps) {
|
|
37
|
+
const tags = Array.isArray(exp.meta.tags) ? exp.meta.tags : [];
|
|
38
|
+
for (const tag of tags) {
|
|
39
|
+
const normalized = tag.toLowerCase().trim();
|
|
40
|
+
if (!normalized)
|
|
41
|
+
continue;
|
|
42
|
+
if (!tagToExps.has(normalized))
|
|
43
|
+
tagToExps.set(normalized, []);
|
|
44
|
+
tagToExps.get(normalized).push(exp.id);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const patternTagSets = patterns.map(pat => ({
|
|
48
|
+
id: pat.id,
|
|
49
|
+
tags: new Set((Array.isArray(pat.meta.tags) ? pat.meta.tags : []).map(t => t.toLowerCase())),
|
|
50
|
+
}));
|
|
51
|
+
const tagList = [...tagToExps.keys()].filter(t => (tagToExps.get(t)?.length ?? 0) >= 2);
|
|
52
|
+
const seenClusters = new Set();
|
|
53
|
+
for (let i = 0; i < tagList.length; i++) {
|
|
54
|
+
for (let j = i + 1; j < tagList.length; j++) {
|
|
55
|
+
const tagA = tagList[i];
|
|
56
|
+
const tagB = tagList[j];
|
|
57
|
+
const expsA = new Set(tagToExps.get(tagA));
|
|
58
|
+
const shared = tagToExps.get(tagB).filter(id => expsA.has(id));
|
|
59
|
+
if (shared.length < 3)
|
|
60
|
+
continue;
|
|
61
|
+
const clusterKey = [...shared].sort().join(',');
|
|
62
|
+
if (seenClusters.has(clusterKey))
|
|
63
|
+
continue;
|
|
64
|
+
seenClusters.add(clusterKey);
|
|
65
|
+
const existingPats = patternTagSets
|
|
66
|
+
.filter(p => p.tags.has(tagA) && p.tags.has(tagB))
|
|
67
|
+
.map(p => p.id);
|
|
68
|
+
candidates.push({
|
|
69
|
+
type: 'tag-cluster', project: projectName,
|
|
70
|
+
tags: [tagA, tagB], experiments: shared,
|
|
71
|
+
existingPatterns: existingPats,
|
|
72
|
+
reason: `${shared.length} experiments share tags [${tagA}, ${tagB}]`,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return candidates;
|
|
77
|
+
}
|
|
78
|
+
// --- Detector 2: Link graph connected components ---
|
|
79
|
+
function detectLinkClusters(projectName, experiments) {
|
|
80
|
+
const candidates = [];
|
|
81
|
+
const activeExps = experiments.filter(e => !isInactiveStatus(e.meta.status));
|
|
82
|
+
const linkGraph = new Map();
|
|
83
|
+
for (const exp of activeExps) {
|
|
84
|
+
const links = Array.isArray(exp.meta.links) ? exp.meta.links : [];
|
|
85
|
+
if (links.length === 0)
|
|
86
|
+
continue;
|
|
87
|
+
if (!linkGraph.has(exp.id))
|
|
88
|
+
linkGraph.set(exp.id, new Set());
|
|
89
|
+
for (const link of links) {
|
|
90
|
+
linkGraph.get(exp.id).add(link.toUpperCase().trim());
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const visited = new Set();
|
|
94
|
+
for (const [startId] of linkGraph) {
|
|
95
|
+
if (visited.has(startId))
|
|
96
|
+
continue;
|
|
97
|
+
const component = [];
|
|
98
|
+
const queue = [startId];
|
|
99
|
+
while (queue.length > 0) {
|
|
100
|
+
const current = queue.pop();
|
|
101
|
+
if (visited.has(current))
|
|
102
|
+
continue;
|
|
103
|
+
visited.add(current);
|
|
104
|
+
component.push(current);
|
|
105
|
+
const neighbors = linkGraph.get(current);
|
|
106
|
+
if (neighbors) {
|
|
107
|
+
for (const n of neighbors) {
|
|
108
|
+
if (!visited.has(n) && linkGraph.has(n))
|
|
109
|
+
queue.push(n);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (component.length >= 3) {
|
|
114
|
+
candidates.push({
|
|
115
|
+
type: 'link-cluster', project: projectName,
|
|
116
|
+
experiments: component, existingPatterns: [],
|
|
117
|
+
reason: `${component.length} experiments form a connected link cluster`,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return candidates;
|
|
122
|
+
}
|
|
123
|
+
// --- Detector 3: Supersession chains ---
|
|
124
|
+
function detectSupersedeChains(projectName, experiments) {
|
|
125
|
+
const candidates = [];
|
|
126
|
+
const supersedeNext = new Map();
|
|
127
|
+
for (const exp of experiments) {
|
|
128
|
+
const status = String(exp.meta.status || '');
|
|
129
|
+
const match = status.match(/^(?:superseded|refined):(.+)/i);
|
|
130
|
+
if (match) {
|
|
131
|
+
supersedeNext.set(exp.id.toUpperCase(), match[1].toUpperCase().trim());
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const allNewIds = new Set(supersedeNext.values());
|
|
135
|
+
const roots = [...supersedeNext.keys()].filter(id => !allNewIds.has(id));
|
|
136
|
+
for (const root of roots) {
|
|
137
|
+
const chain = [root];
|
|
138
|
+
let current = root;
|
|
139
|
+
while (supersedeNext.has(current)) {
|
|
140
|
+
current = supersedeNext.get(current);
|
|
141
|
+
chain.push(current);
|
|
142
|
+
if (chain.length > 20)
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
if (chain.length >= 3) {
|
|
146
|
+
candidates.push({
|
|
147
|
+
type: 'supersede-chain', project: projectName,
|
|
148
|
+
experiments: chain, existingPatterns: [],
|
|
149
|
+
reason: `Supersession chain of length ${chain.length}: ${chain.join(' → ')}`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return candidates;
|
|
154
|
+
}
|
|
155
|
+
// --- Detector 4: Contradiction — K/PAT entries whose evidence experiments are invalidated ---
|
|
156
|
+
function detectContradictions(projectName, experiments, knowledge, patterns) {
|
|
157
|
+
const candidates = [];
|
|
158
|
+
// Build experiment status lookup
|
|
159
|
+
const expStatus = new Map();
|
|
160
|
+
for (const exp of experiments) {
|
|
161
|
+
expStatus.set(exp.id.toUpperCase(), String(exp.meta.status || 'active').toLowerCase());
|
|
162
|
+
}
|
|
163
|
+
// Check K and PAT entries that have evidence/source-experiments fields
|
|
164
|
+
const derivedEntries = [
|
|
165
|
+
...knowledge.map(k => ({ ...k, entryType: 'knowledge', dir: 'knowledge' })),
|
|
166
|
+
...patterns.map(p => ({ ...p, entryType: 'pattern', dir: 'patterns' })),
|
|
167
|
+
];
|
|
168
|
+
for (const entry of derivedEntries) {
|
|
169
|
+
// Collect source experiment IDs from evidence, source-experiments, or source fields
|
|
170
|
+
const sourceIds = [];
|
|
171
|
+
for (const field of ['evidence', 'source-experiments', 'source']) {
|
|
172
|
+
const val = entry.meta[field];
|
|
173
|
+
if (Array.isArray(val)) {
|
|
174
|
+
sourceIds.push(...val.map(s => s.toUpperCase().trim()));
|
|
175
|
+
}
|
|
176
|
+
else if (typeof val === 'string') {
|
|
177
|
+
// Parse "EXP-001, EXP-003" format
|
|
178
|
+
const matches = String(val).matchAll(/EXP-\d+/gi);
|
|
179
|
+
for (const m of matches)
|
|
180
|
+
sourceIds.push(m[0].toUpperCase());
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (sourceIds.length === 0)
|
|
184
|
+
continue;
|
|
185
|
+
const compromised = [];
|
|
186
|
+
const remaining = [];
|
|
187
|
+
for (const srcId of sourceIds) {
|
|
188
|
+
const status = expStatus.get(srcId);
|
|
189
|
+
if (!status)
|
|
190
|
+
continue; // experiment not found, might be cross-project ref
|
|
191
|
+
if (isInactiveStatus(status)) {
|
|
192
|
+
compromised.push({ id: srcId, status });
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
remaining.push(srcId);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (compromised.length === 0)
|
|
199
|
+
continue;
|
|
200
|
+
const severity = remaining.length === 0 ? 'all-compromised' : 'partial';
|
|
201
|
+
candidates.push({
|
|
202
|
+
entryId: entry.id,
|
|
203
|
+
entryFile: `${entry.dir}/${entry.filename}`,
|
|
204
|
+
entryType: entry.entryType,
|
|
205
|
+
project: projectName,
|
|
206
|
+
compromisedSources: compromised,
|
|
207
|
+
remainingSources: remaining,
|
|
208
|
+
severity,
|
|
209
|
+
reason: severity === 'all-compromised'
|
|
210
|
+
? `${entry.id} has NO remaining valid evidence (all sources invalidated/superseded)`
|
|
211
|
+
: `${entry.id} has ${compromised.length}/${sourceIds.length} sources compromised`,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return candidates;
|
|
215
|
+
}
|
|
216
|
+
// --- Detector 5: Staleness — entries with refs=0 and age > threshold ---
|
|
217
|
+
function detectStale(projectName, experiments, knowledge, patterns, thresholdDays = 30) {
|
|
218
|
+
const candidates = [];
|
|
219
|
+
const allEntries = [
|
|
220
|
+
...experiments.map(e => ({ ...e, entryType: 'experiment' })),
|
|
221
|
+
...knowledge.map(k => ({ ...k, entryType: 'knowledge' })),
|
|
222
|
+
...patterns.map(p => ({ ...p, entryType: 'pattern' })),
|
|
223
|
+
];
|
|
224
|
+
for (const entry of allEntries) {
|
|
225
|
+
// Skip already inactive
|
|
226
|
+
if (isInactiveStatus(entry.meta.status))
|
|
227
|
+
continue;
|
|
228
|
+
// Skip already marked stale or challenged
|
|
229
|
+
const status = String(entry.meta.status || '').toLowerCase();
|
|
230
|
+
if (status === 'stale' || status.startsWith('challenged'))
|
|
231
|
+
continue;
|
|
232
|
+
const refs = typeof entry.meta.refs === 'number' ? entry.meta.refs : 0;
|
|
233
|
+
if (refs > 0)
|
|
234
|
+
continue;
|
|
235
|
+
const age = daysSince(entry.meta.date);
|
|
236
|
+
if (age === null || age <= thresholdDays)
|
|
237
|
+
continue;
|
|
238
|
+
candidates.push({
|
|
239
|
+
entryId: entry.id,
|
|
240
|
+
entryFile: entry.filename,
|
|
241
|
+
entryType: entry.entryType,
|
|
242
|
+
project: projectName,
|
|
243
|
+
ageDays: Math.floor(age),
|
|
244
|
+
refs,
|
|
245
|
+
reason: `${entry.id} has 0 refs and is ${Math.floor(age)} days old (threshold: ${thresholdDays}d)`,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return candidates;
|
|
249
|
+
}
|
|
250
|
+
// --- Detector 6: Anti-pattern candidates — recurring process deficiencies across experiments ---
|
|
251
|
+
function detectAntiPatterns(projectName, experiments) {
|
|
252
|
+
const candidates = [];
|
|
253
|
+
const activeExps = experiments.filter(e => !isInactiveStatus(e.meta.status));
|
|
254
|
+
// Read experiment bodies and extract process deficiency content
|
|
255
|
+
const projectDir = path.join(PROJECTS_DIR, projectName);
|
|
256
|
+
const expDir = path.join(projectDir, 'experiments');
|
|
257
|
+
if (!fs.existsSync(expDir))
|
|
258
|
+
return [];
|
|
259
|
+
// Extract deficiency text from each experiment
|
|
260
|
+
const deficiencies = [];
|
|
261
|
+
for (const exp of activeExps) {
|
|
262
|
+
const filePath = path.join(expDir, exp.filename);
|
|
263
|
+
let content;
|
|
264
|
+
try {
|
|
265
|
+
content = fs.readFileSync(filePath, 'utf8');
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
// Extract process deficiency section
|
|
271
|
+
const defMatch = content.match(/\*\*过程缺陷\*\*[::]\s*(.+?)(?=\n\*\*|\n####|\n##|$)/s);
|
|
272
|
+
if (defMatch && defMatch[1].trim() && defMatch[1].trim() !== '无' && defMatch[1].trim() !== 'None') {
|
|
273
|
+
deficiencies.push({ id: exp.id, text: defMatch[1].trim() });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (deficiencies.length < 2)
|
|
277
|
+
return [];
|
|
278
|
+
// Keyword-based clustering with noise filtering
|
|
279
|
+
const keywordMap = new Map(); // keyword → [exp IDs]
|
|
280
|
+
// Stop words and noise patterns to filter out
|
|
281
|
+
const stopWords = new Set(['的', '了', '在', '是', '和', '与', '或', '但', '因为', '所以',
|
|
282
|
+
'the', 'a', 'an', 'is', 'are', 'was', 'were', 'and', 'or', 'but', 'for', 'to', 'in', 'on',
|
|
283
|
+
'没有', '不', '无', '需要', '应该', '可以', 'with', 'from', 'that', 'this', 'have', 'been']);
|
|
284
|
+
// Filter out markdown formatting and numbered list artifacts
|
|
285
|
+
const noisePatterns = /^[\*\#\(\)\d\.\-\+\>\`]+$|^\*\*.*\*\*:?$|^\(\d+\)$/;
|
|
286
|
+
for (const { id, text } of deficiencies) {
|
|
287
|
+
// Strip markdown formatting before tokenizing
|
|
288
|
+
const cleaned = text
|
|
289
|
+
.replace(/\*\*[^*]+\*\*/g, '') // remove bold markers and their content
|
|
290
|
+
.replace(/\([^)]*\)/g, '') // remove parenthetical refs
|
|
291
|
+
.replace(/`[^`]+`/g, '') // remove inline code
|
|
292
|
+
.replace(/[,。;:、!?\n\r\-\*\#\>\+]/g, ' ');
|
|
293
|
+
const tokens = cleaned
|
|
294
|
+
.split(/\s+/)
|
|
295
|
+
.filter(t => t.length >= 3 && !stopWords.has(t.toLowerCase()) && !noisePatterns.test(t))
|
|
296
|
+
.map(t => t.toLowerCase());
|
|
297
|
+
for (const token of tokens) {
|
|
298
|
+
if (!keywordMap.has(token))
|
|
299
|
+
keywordMap.set(token, []);
|
|
300
|
+
const list = keywordMap.get(token);
|
|
301
|
+
if (!list.includes(id))
|
|
302
|
+
list.push(id);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// Find keywords appearing in 3+ experiments (raised threshold for better signal)
|
|
306
|
+
const seenClusters = new Set();
|
|
307
|
+
for (const [keyword, expIds] of keywordMap) {
|
|
308
|
+
if (expIds.length < 3)
|
|
309
|
+
continue;
|
|
310
|
+
const clusterKey = [...expIds].sort().join(',');
|
|
311
|
+
if (seenClusters.has(clusterKey))
|
|
312
|
+
continue;
|
|
313
|
+
seenClusters.add(clusterKey);
|
|
314
|
+
// Reconstruct the deficiency texts for these experiments
|
|
315
|
+
const relevantTexts = deficiencies
|
|
316
|
+
.filter(d => expIds.includes(d.id))
|
|
317
|
+
.map(d => `${d.id}: ${d.text.slice(0, 80)}`)
|
|
318
|
+
.join('; ');
|
|
319
|
+
candidates.push({
|
|
320
|
+
project: projectName,
|
|
321
|
+
pattern: keyword,
|
|
322
|
+
experiments: expIds,
|
|
323
|
+
reason: `Keyword "${keyword}" appears in ${expIds.length} experiments' deficiency sections: ${relevantTexts.slice(0, 200)}`,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
// Sort by cluster size desc
|
|
327
|
+
candidates.sort((a, b) => b.experiments.length - a.experiments.length);
|
|
328
|
+
// Return top 10 to avoid noise
|
|
329
|
+
return candidates.slice(0, 10);
|
|
330
|
+
}
|
|
331
|
+
// --- Core analysis ---
|
|
332
|
+
function analyzeProject(projectName) {
|
|
333
|
+
const projectDir = path.join(PROJECTS_DIR, projectName);
|
|
334
|
+
const expDir = path.join(projectDir, 'experiments');
|
|
335
|
+
const knDir = path.join(projectDir, 'knowledge');
|
|
336
|
+
const patDir = path.join(projectDir, 'patterns');
|
|
337
|
+
const experiments = scanAtomicFiles(expDir);
|
|
338
|
+
const knowledge = scanAtomicFiles(knDir);
|
|
339
|
+
const patterns = scanAtomicFiles(patDir);
|
|
340
|
+
// Run all detectors
|
|
341
|
+
const tagClusters = detectTagClusters(projectName, experiments, patterns);
|
|
342
|
+
const linkClusters = detectLinkClusters(projectName, experiments);
|
|
343
|
+
const supersedeChains = detectSupersedeChains(projectName, experiments);
|
|
344
|
+
const consolidation = [...tagClusters, ...linkClusters, ...supersedeChains];
|
|
345
|
+
consolidation.sort((a, b) => {
|
|
346
|
+
const typeOrder = { 'tag-cluster': 0, 'link-cluster': 1, 'supersede-chain': 2 };
|
|
347
|
+
const typeDiff = (typeOrder[a.type] ?? 9) - (typeOrder[b.type] ?? 9);
|
|
348
|
+
if (typeDiff !== 0)
|
|
349
|
+
return typeDiff;
|
|
350
|
+
return b.experiments.length - a.experiments.length;
|
|
351
|
+
});
|
|
352
|
+
const contradictions = detectContradictions(projectName, experiments, knowledge, patterns);
|
|
353
|
+
const stale = detectStale(projectName, experiments, knowledge, patterns);
|
|
354
|
+
const antiPatterns = detectAntiPatterns(projectName, experiments);
|
|
355
|
+
return {
|
|
356
|
+
project: projectName,
|
|
357
|
+
experimentCount: experiments.length,
|
|
358
|
+
knowledgeCount: knowledge.length,
|
|
359
|
+
patternCount: patterns.length,
|
|
360
|
+
consolidation,
|
|
361
|
+
contradictions,
|
|
362
|
+
stale,
|
|
363
|
+
antiPatterns,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
// --- Public API ---
|
|
367
|
+
export function consolidateAll() {
|
|
368
|
+
const projects = fs.readdirSync(PROJECTS_DIR).filter(d => {
|
|
369
|
+
const dir = path.join(PROJECTS_DIR, d);
|
|
370
|
+
return fs.statSync(dir).isDirectory()
|
|
371
|
+
&& fs.existsSync(path.join(dir, 'experiments'));
|
|
372
|
+
});
|
|
373
|
+
return projects.map(p => analyzeProject(p));
|
|
374
|
+
}
|
|
375
|
+
export function consolidateProject(projectName) {
|
|
376
|
+
return analyzeProject(projectName);
|
|
377
|
+
}
|
|
378
|
+
// --- CLI ---
|
|
379
|
+
function printReport(reports) {
|
|
380
|
+
const reportsWithFindings = reports.filter(r => r.consolidation.length > 0 || r.contradictions.length > 0 ||
|
|
381
|
+
r.stale.length > 0 || r.antiPatterns.length > 0);
|
|
382
|
+
if (reportsWithFindings.length === 0) {
|
|
383
|
+
log.info('No findings across all projects.');
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
for (const r of reportsWithFindings) {
|
|
387
|
+
log.info(`\n=== ${r.project} (${r.experimentCount} exp, ${r.knowledgeCount} K, ${r.patternCount} PAT) ===`);
|
|
388
|
+
if (r.contradictions.length > 0) {
|
|
389
|
+
log.info(`\n ⚠ CONTRADICTIONS (${r.contradictions.length}):`);
|
|
390
|
+
for (const c of r.contradictions) {
|
|
391
|
+
const sev = c.severity === 'all-compromised' ? '🔴' : '🟡';
|
|
392
|
+
log.info(` ${sev} ${c.reason}`);
|
|
393
|
+
log.info(` Compromised: ${c.compromisedSources.map(s => `${s.id}(${s.status})`).join(', ')}`);
|
|
394
|
+
if (c.remainingSources.length > 0) {
|
|
395
|
+
log.info(` Remaining: ${c.remainingSources.join(', ')}`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (r.stale.length > 0) {
|
|
400
|
+
log.info(`\n ⏳ STALE (${r.stale.length}):`);
|
|
401
|
+
for (const s of r.stale.slice(0, 10)) {
|
|
402
|
+
log.info(` ${s.entryId} (${s.entryType}, ${s.ageDays}d old, ${s.refs} refs)`);
|
|
403
|
+
}
|
|
404
|
+
if (r.stale.length > 10)
|
|
405
|
+
log.info(` ... and ${r.stale.length - 10} more`);
|
|
406
|
+
}
|
|
407
|
+
if (r.antiPatterns.length > 0) {
|
|
408
|
+
log.info(`\n 🔁 ANTI-PATTERNS (${r.antiPatterns.length}):`);
|
|
409
|
+
for (const a of r.antiPatterns.slice(0, 5)) {
|
|
410
|
+
log.info(` "${a.pattern}" in ${a.experiments.join(', ')}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (r.consolidation.length > 0) {
|
|
414
|
+
const uncovered = r.consolidation.filter(c => c.existingPatterns.length === 0);
|
|
415
|
+
log.info(`\n 📦 CONSOLIDATION (${r.consolidation.length} total, ${uncovered.length} uncovered):`);
|
|
416
|
+
for (const c of uncovered.slice(0, 5)) {
|
|
417
|
+
log.info(` [${c.type}] ${c.reason}`);
|
|
418
|
+
}
|
|
419
|
+
if (uncovered.length > 5)
|
|
420
|
+
log.info(` ... and ${uncovered.length - 5} more`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
// Summary
|
|
424
|
+
const totals = {
|
|
425
|
+
contradictions: reports.reduce((s, r) => s + r.contradictions.length, 0),
|
|
426
|
+
allCompromised: reports.reduce((s, r) => s + r.contradictions.filter(c => c.severity === 'all-compromised').length, 0),
|
|
427
|
+
stale: reports.reduce((s, r) => s + r.stale.length, 0),
|
|
428
|
+
antiPatterns: reports.reduce((s, r) => s + r.antiPatterns.length, 0),
|
|
429
|
+
consolidation: reports.reduce((s, r) => s + r.consolidation.length, 0),
|
|
430
|
+
uncovered: reports.reduce((s, r) => s + r.consolidation.filter(c => c.existingPatterns.length === 0).length, 0),
|
|
431
|
+
};
|
|
432
|
+
log.info(`\n--- Summary ---`);
|
|
433
|
+
log.info(`Contradictions: ${totals.contradictions} (${totals.allCompromised} fully compromised)`);
|
|
434
|
+
log.info(`Stale entries: ${totals.stale}`);
|
|
435
|
+
log.info(`Anti-pattern signals: ${totals.antiPatterns}`);
|
|
436
|
+
log.info(`Consolidation candidates: ${totals.consolidation} (${totals.uncovered} uncovered)`);
|
|
437
|
+
}
|
|
438
|
+
if (isMainModule(import.meta.url)) {
|
|
439
|
+
const args = process.argv.slice(2);
|
|
440
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
441
|
+
log.info(`Usage: memory-consolidate.ts [--all | <project-name>] [--json]
|
|
442
|
+
|
|
443
|
+
Knowledge lifecycle detector — scans experiments, knowledge, and patterns for:
|
|
444
|
+
• Contradictions: K/PAT entries whose source experiments are invalidated
|
|
445
|
+
• Staleness: entries with 0 refs and age > 30 days
|
|
446
|
+
• Anti-patterns: recurring deficiency keywords across experiments
|
|
447
|
+
• Consolidation candidates: tag clusters, link graphs, supersede chains
|
|
448
|
+
|
|
449
|
+
Options:
|
|
450
|
+
--all Scan all projects (default)
|
|
451
|
+
<project-name> Scan specific project
|
|
452
|
+
--json Output raw JSON (for programmatic use)
|
|
453
|
+
--help Show this help`);
|
|
454
|
+
process.exit(0);
|
|
455
|
+
}
|
|
456
|
+
const jsonMode = args.includes('--json');
|
|
457
|
+
const filteredArgs = args.filter(a => a !== '--json');
|
|
458
|
+
let reports;
|
|
459
|
+
if (filteredArgs.includes('--all') || filteredArgs.length === 0) {
|
|
460
|
+
reports = consolidateAll();
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
reports = filteredArgs.map(p => consolidateProject(p));
|
|
464
|
+
}
|
|
465
|
+
if (jsonMode) {
|
|
466
|
+
log.info(JSON.stringify(reports, null, 2));
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
printReport(reports);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface CortexMDEntry {
|
|
2
|
+
path: string;
|
|
3
|
+
content: string;
|
|
4
|
+
mtimeMs: number;
|
|
5
|
+
}
|
|
6
|
+
export interface CortexMDBlock {
|
|
7
|
+
type: 'text';
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
export interface CortexMDInjectorOptions {
|
|
11
|
+
/** Session id — usually process.env.CORTEX_SESSION_ID. Undefined/invalid → in-memory only. */
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
/** Directory where per-session cache files live. */
|
|
14
|
+
cacheDir?: string;
|
|
15
|
+
/** Direct cache file override (used by tests to bypass session-id logic). */
|
|
16
|
+
cacheFile?: string | null;
|
|
17
|
+
}
|
|
18
|
+
export declare class CortexMDInjector {
|
|
19
|
+
private cache;
|
|
20
|
+
private readonly cacheFile;
|
|
21
|
+
constructor(options?: CortexMDInjectorOptions);
|
|
22
|
+
private loadCache;
|
|
23
|
+
private persistCache;
|
|
24
|
+
/** markOnlyPaths: entries whose path is in this set update the mtime cache
|
|
25
|
+
* (so future reads of sibling files skip them) but do not emit a block.
|
|
26
|
+
* Intended for the tool's own target file — the agent already has its
|
|
27
|
+
* content as the primary tool response and does not need a duplicate. */
|
|
28
|
+
buildBlocks(device: string, entries: CortexMDEntry[], markOnlyPaths?: Set<string>): CortexMDBlock[];
|
|
29
|
+
}
|
|
30
|
+
export declare function getDefaultCortexInjector(): CortexMDInjector;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// input: fs, device name, CortexMDEntry[], session id
|
|
2
|
+
// output: CortexMDInjector class + singleton
|
|
3
|
+
// pos: remote CORTEX.md injection into MCP response dedup cache
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import * as fs from 'fs';
|
|
6
|
+
import * as path from 'path';
|
|
7
|
+
import { WORKSPACE_DIR } from '../../core/utils.js';
|
|
8
|
+
const DEFAULT_CACHE_DIR = path.join(WORKSPACE_DIR, 'mcp-cortexmd-cache');
|
|
9
|
+
const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
10
|
+
const SESSION_ID_RE = /^[A-Za-z0-9._-]+$/;
|
|
11
|
+
function resolveCacheFile(cacheDir, sessionId) {
|
|
12
|
+
if (!sessionId || !SESSION_ID_RE.test(sessionId))
|
|
13
|
+
return null;
|
|
14
|
+
return path.join(cacheDir, `${sessionId}.json`);
|
|
15
|
+
}
|
|
16
|
+
/** Remove stale session cache files and the legacy global cache. Best-effort, swallows errors. */
|
|
17
|
+
function maintainCacheDir(cacheDir) {
|
|
18
|
+
try {
|
|
19
|
+
if (!fs.existsSync(cacheDir))
|
|
20
|
+
return;
|
|
21
|
+
const now = Date.now();
|
|
22
|
+
for (const name of fs.readdirSync(cacheDir)) {
|
|
23
|
+
const p = path.join(cacheDir, name);
|
|
24
|
+
try {
|
|
25
|
+
const st = fs.statSync(p);
|
|
26
|
+
if (!st.isFile())
|
|
27
|
+
continue;
|
|
28
|
+
if (now - st.mtimeMs > CACHE_TTL_MS)
|
|
29
|
+
fs.rmSync(p, { force: true });
|
|
30
|
+
}
|
|
31
|
+
catch { /* ignore */ }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch { /* ignore */ }
|
|
35
|
+
}
|
|
36
|
+
export class CortexMDInjector {
|
|
37
|
+
cache = new Map();
|
|
38
|
+
cacheFile;
|
|
39
|
+
constructor(options = {}) {
|
|
40
|
+
const cacheDir = options.cacheDir ?? DEFAULT_CACHE_DIR;
|
|
41
|
+
if (options.cacheFile !== undefined) {
|
|
42
|
+
this.cacheFile = options.cacheFile;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const sessionId = options.sessionId ?? process.env.CORTEX_SESSION_ID;
|
|
46
|
+
this.cacheFile = resolveCacheFile(cacheDir, sessionId);
|
|
47
|
+
}
|
|
48
|
+
maintainCacheDir(cacheDir);
|
|
49
|
+
this.loadCache();
|
|
50
|
+
}
|
|
51
|
+
loadCache() {
|
|
52
|
+
if (!this.cacheFile)
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
if (!fs.existsSync(this.cacheFile))
|
|
56
|
+
return;
|
|
57
|
+
const raw = fs.readFileSync(this.cacheFile, 'utf8');
|
|
58
|
+
const data = JSON.parse(raw);
|
|
59
|
+
if (data && typeof data === 'object') {
|
|
60
|
+
for (const [k, v] of Object.entries(data)) {
|
|
61
|
+
if (typeof v === 'number')
|
|
62
|
+
this.cache.set(k, v);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// corrupt/unreadable cache — start fresh, will overwrite on next persist
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
persistCache() {
|
|
71
|
+
if (!this.cacheFile)
|
|
72
|
+
return;
|
|
73
|
+
try {
|
|
74
|
+
fs.mkdirSync(path.dirname(this.cacheFile), { recursive: true });
|
|
75
|
+
const obj = {};
|
|
76
|
+
for (const [k, v] of this.cache)
|
|
77
|
+
obj[k] = v;
|
|
78
|
+
const tmp = `${this.cacheFile}.${process.pid}.tmp`;
|
|
79
|
+
fs.writeFileSync(tmp, JSON.stringify(obj), 'utf8');
|
|
80
|
+
fs.renameSync(tmp, this.cacheFile);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// disk full, permission denied, partial write interrupted — degrade gracefully
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** markOnlyPaths: entries whose path is in this set update the mtime cache
|
|
87
|
+
* (so future reads of sibling files skip them) but do not emit a block.
|
|
88
|
+
* Intended for the tool's own target file — the agent already has its
|
|
89
|
+
* content as the primary tool response and does not need a duplicate. */
|
|
90
|
+
buildBlocks(device, entries, markOnlyPaths) {
|
|
91
|
+
if (!entries || entries.length === 0)
|
|
92
|
+
return [];
|
|
93
|
+
const blocks = [];
|
|
94
|
+
let changed = false;
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
const key = `${device}:${entry.path}`;
|
|
97
|
+
if (this.cache.get(key) === entry.mtimeMs)
|
|
98
|
+
continue;
|
|
99
|
+
this.cache.set(key, entry.mtimeMs);
|
|
100
|
+
changed = true;
|
|
101
|
+
if (markOnlyPaths?.has(entry.path))
|
|
102
|
+
continue;
|
|
103
|
+
blocks.push({
|
|
104
|
+
type: 'text',
|
|
105
|
+
text: `<system-reminder>\n` +
|
|
106
|
+
`Auto-loaded CORTEX.md from ${device}:${entry.path} ` +
|
|
107
|
+
`(ancestor of accessed path on remote device). ` +
|
|
108
|
+
`These instructions apply to files under this directory on that device.\n\n` +
|
|
109
|
+
entry.content +
|
|
110
|
+
`\n</system-reminder>`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (changed)
|
|
114
|
+
this.persistCache();
|
|
115
|
+
return blocks;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
let defaultCortexInjector = null;
|
|
119
|
+
export function getDefaultCortexInjector() {
|
|
120
|
+
if (!defaultCortexInjector)
|
|
121
|
+
defaultCortexInjector = new CortexMDInjector();
|
|
122
|
+
return defaultCortexInjector;
|
|
123
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface CortexMDEntry {
|
|
2
|
+
path: string;
|
|
3
|
+
content: string;
|
|
4
|
+
mtimeMs: number;
|
|
5
|
+
}
|
|
6
|
+
/** Walk from the directory containing `targetFilePath` up to the filesystem root,
|
|
7
|
+
* collecting CORTEX.md and CORTEX.local.md at each level. Also appends the
|
|
8
|
+
* remote user's ~/.cortex/CORTEX.md if present. Returns leaf→root order. */
|
|
9
|
+
export declare function scanCortexMDChain(targetFilePath: string): CortexMDEntry[];
|