@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.
Files changed (503) hide show
  1. package/defaults/.claude/settings.json +13 -0
  2. package/defaults/CORTEX.md +15 -0
  3. package/defaults/config/budget.json +4 -0
  4. package/defaults/config/session-hooks.json +7 -0
  5. package/defaults/config/thread-templates.json +528 -0
  6. package/defaults/context/CORTEX.md +40 -0
  7. package/defaults/context/decisions/DR-0012.md +54 -0
  8. package/defaults/context/ideas/CORTEX.md +10 -0
  9. package/defaults/context/projects/CORTEX.md +25 -0
  10. package/defaults/context/scans/CORTEX.md +13 -0
  11. package/defaults/context/user/CORTEX.md +21 -0
  12. package/defaults/data/schedules.json +52 -0
  13. package/defaults/gitignore +44 -0
  14. package/defaults/hooks/ask-user-question-hook.mjs +66 -0
  15. package/defaults/hooks/cortex-md-injector.mjs +237 -0
  16. package/defaults/hooks/exit-plan-mode-hook.mjs +84 -0
  17. package/defaults/hooks/memory-ref-tracker.mjs +154 -0
  18. package/defaults/hooks/new-session-hook.mjs +83 -0
  19. package/defaults/hooks/post-task-hook.mjs +63 -0
  20. package/defaults/hooks/rules-loader.mjs +212 -0
  21. package/defaults/hooks/sensitive-file-edit.mjs +84 -0
  22. package/defaults/hooks/session-activity-tracker.mjs +254 -0
  23. package/defaults/hooks/task-status-check.mjs +218 -0
  24. package/defaults/hooks/tasks-yaml-guard.mjs +230 -0
  25. package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +5 -0
  26. package/defaults/plugins/cortex-coder/skills/cli-standards/SKILL.md +208 -0
  27. package/defaults/plugins/cortex-coder/skills/code-standards/SKILL.md +146 -0
  28. package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +164 -0
  29. package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +5 -0
  30. package/defaults/plugins/cortex-common/skills/compound/SKILL.md +349 -0
  31. package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +104 -0
  32. package/defaults/plugins/cortex-common/skills/critique/SKILL.md +88 -0
  33. package/defaults/plugins/cortex-common/skills/debug-campaign/SKILL.md +508 -0
  34. package/defaults/plugins/cortex-common/skills/diagnose/SKILL.md +169 -0
  35. package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +135 -0
  36. package/defaults/plugins/cortex-common/skills/solution-design/SKILL.md +183 -0
  37. package/defaults/plugins/cortex-common/skills/synthesize/SKILL.md +120 -0
  38. package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +5 -0
  39. package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +89 -0
  40. package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +163 -0
  41. package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +425 -0
  42. package/defaults/plugins/cortex-system/.claude-plugin/plugin.json +5 -0
  43. package/defaults/plugins/cortex-system/skills/approval/SKILL.md +84 -0
  44. package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +199 -0
  45. package/defaults/plugins/cortex-system/skills/deep-retrospective/CORTEX.md +8 -0
  46. package/defaults/plugins/cortex-system/skills/deep-retrospective/SKILL.md +201 -0
  47. package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/CORTEX.md +8 -0
  48. package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/extract_session.py +165 -0
  49. package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/scan_logs.py +402 -0
  50. package/defaults/plugins/cortex-system/skills/evolve/SKILL.md +115 -0
  51. package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +262 -0
  52. package/defaults/plugins/cortex-system/skills/gravity/SKILL.md +125 -0
  53. package/defaults/plugins/cortex-system/skills/orient/SKILL.md +310 -0
  54. package/defaults/plugins/cortex-system/skills/orient-project/SKILL.md +85 -0
  55. package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +167 -0
  56. package/defaults/plugins/cortex-system/skills/refresh-skills/SKILL.md +153 -0
  57. package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +209 -0
  58. package/defaults/plugins/cortex-system/skills/thread/SKILL.md +478 -0
  59. package/defaults/plugins/cortex-system/skills/user-learn/SKILL.md +95 -0
  60. package/defaults/prompts/directives/coder-reviewer.md +106 -0
  61. package/defaults/prompts/directives/coder.md +90 -0
  62. package/defaults/prompts/directives/director.md +97 -0
  63. package/defaults/prompts/directives/doc-reviewer.md +93 -0
  64. package/defaults/prompts/directives/doc-writer.md +85 -0
  65. package/defaults/prompts/directives/executor-reviewer.md +96 -0
  66. package/defaults/prompts/directives/executor.md +85 -0
  67. package/defaults/prompts/directives/milestone-executor.md +119 -0
  68. package/defaults/prompts/directives/milestone-reviewer.md +108 -0
  69. package/defaults/prompts/directives/plan-executor.md +121 -0
  70. package/defaults/prompts/directives/plan-reviewer.md +99 -0
  71. package/defaults/prompts/systemPrompts/coder.md +64 -0
  72. package/defaults/prompts/systemPrompts/direct.md +92 -0
  73. package/defaults/prompts/systemPrompts/web-opus-4-6.md +68 -0
  74. package/defaults/prompts/systemPrompts/web-sonnet-4-6.md +76 -0
  75. package/defaults/prompts/systemPrompts/web.md +50 -0
  76. package/defaults/prompts/systemPrompts/worker.md +67 -0
  77. package/defaults/rules/correction-propagation.md +31 -0
  78. package/defaults/rules/decision-records.md +49 -0
  79. package/defaults/rules/experiment-format.md +165 -0
  80. package/defaults/rules/issues-md.md +37 -0
  81. package/defaults/rules/meta-conventions.md +45 -0
  82. package/defaults/rules/mission-md.md +47 -0
  83. package/defaults/rules/overview-md.md +41 -0
  84. package/defaults/rules/roadmap-md.md +55 -0
  85. package/defaults/rules/status-md.md +49 -0
  86. package/defaults/rules/task-system.md +53 -0
  87. package/dist/agent-adapter/capabilities.d.ts +12 -0
  88. package/dist/agent-adapter/capabilities.js +52 -0
  89. package/dist/agent-adapter/claude/adapter-tui.d.ts +140 -0
  90. package/dist/agent-adapter/claude/adapter-tui.js +422 -0
  91. package/dist/agent-adapter/claude/adapter.d.ts +85 -0
  92. package/dist/agent-adapter/claude/adapter.js +771 -0
  93. package/dist/agent-adapter/claude/cost-from-usage.d.ts +62 -0
  94. package/dist/agent-adapter/claude/cost-from-usage.js +97 -0
  95. package/dist/agent-adapter/claude/defaults.d.ts +30 -0
  96. package/dist/agent-adapter/claude/defaults.js +49 -0
  97. package/dist/agent-adapter/claude/event-parser.d.ts +28 -0
  98. package/dist/agent-adapter/claude/event-parser.js +173 -0
  99. package/dist/agent-adapter/claude/hooks-builder.d.ts +36 -0
  100. package/dist/agent-adapter/claude/hooks-builder.js +57 -0
  101. package/dist/agent-adapter/claude/jsonl-tail.d.ts +68 -0
  102. package/dist/agent-adapter/claude/jsonl-tail.js +344 -0
  103. package/dist/agent-adapter/claude/spawn-args.d.ts +38 -0
  104. package/dist/agent-adapter/claude/spawn-args.js +83 -0
  105. package/dist/agent-adapter/claude/tmux-control.d.ts +69 -0
  106. package/dist/agent-adapter/claude/tmux-control.js +127 -0
  107. package/dist/agent-adapter/claude/tool-summarizers.d.ts +2 -0
  108. package/dist/agent-adapter/claude/tool-summarizers.js +24 -0
  109. package/dist/agent-adapter/codex/adapter.d.ts +42 -0
  110. package/dist/agent-adapter/codex/adapter.js +916 -0
  111. package/dist/agent-adapter/codex/event-parser.d.ts +17 -0
  112. package/dist/agent-adapter/codex/event-parser.js +41 -0
  113. package/dist/agent-adapter/codex/spawn-args.d.ts +6 -0
  114. package/dist/agent-adapter/codex/spawn-args.js +30 -0
  115. package/dist/agent-adapter/index.d.ts +8 -0
  116. package/dist/agent-adapter/index.js +33 -0
  117. package/dist/agent-adapter/normalize/event-stream.d.ts +6 -0
  118. package/dist/agent-adapter/normalize/event-stream.js +47 -0
  119. package/dist/agent-adapter/normalize/event-types.d.ts +58 -0
  120. package/dist/agent-adapter/normalize/event-types.js +5 -0
  121. package/dist/agent-adapter/normalize/hooks.d.ts +26 -0
  122. package/dist/agent-adapter/normalize/hooks.js +5 -0
  123. package/dist/agent-adapter/normalize/prompt-builder.d.ts +6 -0
  124. package/dist/agent-adapter/normalize/prompt-builder.js +23 -0
  125. package/dist/agent-adapter/normalize/tool-names.d.ts +6 -0
  126. package/dist/agent-adapter/normalize/tool-names.js +73 -0
  127. package/dist/agent-adapter/pi/adapter.d.ts +60 -0
  128. package/dist/agent-adapter/pi/adapter.js +626 -0
  129. package/dist/agent-adapter/pi/agent-dir.d.ts +11 -0
  130. package/dist/agent-adapter/pi/agent-dir.js +34 -0
  131. package/dist/agent-adapter/pi/event-parser.d.ts +26 -0
  132. package/dist/agent-adapter/pi/event-parser.js +313 -0
  133. package/dist/agent-adapter/pi/framing.d.ts +6 -0
  134. package/dist/agent-adapter/pi/framing.js +35 -0
  135. package/dist/agent-adapter/pi/hook-bridge.d.ts +59 -0
  136. package/dist/agent-adapter/pi/hook-bridge.js +233 -0
  137. package/dist/agent-adapter/pi/mcp-bridge.d.ts +26 -0
  138. package/dist/agent-adapter/pi/mcp-bridge.js +140 -0
  139. package/dist/agent-adapter/pi/pi-ext-types.d.ts +90 -0
  140. package/dist/agent-adapter/pi/pi-ext-types.js +5 -0
  141. package/dist/agent-adapter/pi/spawn-args.d.ts +20 -0
  142. package/dist/agent-adapter/pi/spawn-args.js +54 -0
  143. package/dist/agent-adapter/pi/tool-shims.d.ts +2 -0
  144. package/dist/agent-adapter/pi/tool-shims.js +181 -0
  145. package/dist/agent-adapter/types.d.ts +94 -0
  146. package/dist/agent-adapter/types.js +5 -0
  147. package/dist/core/async-mutex.d.ts +6 -0
  148. package/dist/core/async-mutex.js +36 -0
  149. package/dist/core/cli-utils.d.ts +37 -0
  150. package/dist/core/cli-utils.js +69 -0
  151. package/dist/core/config-generator.d.ts +9 -0
  152. package/dist/core/config-generator.js +58 -0
  153. package/dist/core/gateway-generator.d.ts +46 -0
  154. package/dist/core/gateway-generator.js +336 -0
  155. package/dist/core/log.d.ts +7 -0
  156. package/dist/core/log.js +103 -0
  157. package/dist/core/paths.d.ts +32 -0
  158. package/dist/core/paths.js +49 -0
  159. package/dist/core/profile-generator.d.ts +65 -0
  160. package/dist/core/profile-generator.js +310 -0
  161. package/dist/core/running-executions.d.ts +88 -0
  162. package/dist/core/running-executions.js +186 -0
  163. package/dist/core/status-format.d.ts +18 -0
  164. package/dist/core/status-format.js +31 -0
  165. package/dist/core/task-parser.d.ts +69 -0
  166. package/dist/core/task-parser.js +423 -0
  167. package/dist/core/types/agent-types.d.ts +31 -0
  168. package/dist/core/types/agent-types.js +5 -0
  169. package/dist/core/types/thread-types.d.ts +261 -0
  170. package/dist/core/types/thread-types.js +5 -0
  171. package/dist/core/utils.d.ts +29 -0
  172. package/dist/core/utils.js +98 -0
  173. package/dist/core/version.d.ts +3 -0
  174. package/dist/core/version.js +12 -0
  175. package/dist/domain/agents/config.d.ts +58 -0
  176. package/dist/domain/agents/config.js +313 -0
  177. package/dist/domain/agents/facade.d.ts +52 -0
  178. package/dist/domain/agents/facade.js +264 -0
  179. package/dist/domain/agents/index.d.ts +2 -0
  180. package/dist/domain/agents/index.js +4 -0
  181. package/dist/domain/agents/profile-manager.d.ts +54 -0
  182. package/dist/domain/agents/profile-manager.js +161 -0
  183. package/dist/domain/costs/codex-event-format.d.ts +4 -0
  184. package/dist/domain/costs/codex-event-format.js +66 -0
  185. package/dist/domain/costs/codex-usage-monitor.d.ts +7 -0
  186. package/dist/domain/costs/codex-usage-monitor.js +72 -0
  187. package/dist/domain/costs/cost-tracker.d.ts +115 -0
  188. package/dist/domain/costs/cost-tracker.js +327 -0
  189. package/dist/domain/costs/gateway-manager.d.ts +6 -0
  190. package/dist/domain/costs/gateway-manager.js +228 -0
  191. package/dist/domain/costs/rate-limit-parser.d.ts +11 -0
  192. package/dist/domain/costs/rate-limit-parser.js +87 -0
  193. package/dist/domain/costs/rate-limit-throttle.d.ts +32 -0
  194. package/dist/domain/costs/rate-limit-throttle.js +129 -0
  195. package/dist/domain/executions/registry.d.ts +20 -0
  196. package/dist/domain/executions/registry.js +124 -0
  197. package/dist/domain/mcp/core-server.d.ts +2 -0
  198. package/dist/domain/mcp/core-server.js +34 -0
  199. package/dist/domain/mcp/server.d.ts +3 -0
  200. package/dist/domain/mcp/server.js +78 -0
  201. package/dist/domain/mcp/tools/context.d.ts +23 -0
  202. package/dist/domain/mcp/tools/context.js +51 -0
  203. package/dist/domain/mcp/tools/cortex-md.d.ts +17 -0
  204. package/dist/domain/mcp/tools/cortex-md.js +36 -0
  205. package/dist/domain/mcp/tools/cost.d.ts +2 -0
  206. package/dist/domain/mcp/tools/cost.js +54 -0
  207. package/dist/domain/mcp/tools/executions.d.ts +2 -0
  208. package/dist/domain/mcp/tools/executions.js +94 -0
  209. package/dist/domain/mcp/tools/schedule.d.ts +23 -0
  210. package/dist/domain/mcp/tools/schedule.js +245 -0
  211. package/dist/domain/mcp/tools/slack.d.ts +21 -0
  212. package/dist/domain/mcp/tools/slack.js +124 -0
  213. package/dist/domain/mcp/tools/task-ops.d.ts +2 -0
  214. package/dist/domain/mcp/tools/task-ops.js +509 -0
  215. package/dist/domain/mcp/tools/tui-ask.d.ts +31 -0
  216. package/dist/domain/mcp/tools/tui-ask.js +108 -0
  217. package/dist/domain/mcp/tools/tui-plan.d.ts +28 -0
  218. package/dist/domain/mcp/tools/tui-plan.js +118 -0
  219. package/dist/domain/mcp/tui-server.d.ts +2 -0
  220. package/dist/domain/mcp/tui-server.js +69 -0
  221. package/dist/domain/memory/consolidate.d.ts +55 -0
  222. package/dist/domain/memory/consolidate.js +471 -0
  223. package/dist/domain/memory/cortex-md-injector.d.ts +30 -0
  224. package/dist/domain/memory/cortex-md-injector.js +123 -0
  225. package/dist/domain/memory/cortex-md-scanner.d.ts +9 -0
  226. package/dist/domain/memory/cortex-md-scanner.js +67 -0
  227. package/dist/domain/memory/index-regen.d.ts +19 -0
  228. package/dist/domain/memory/index-regen.js +398 -0
  229. package/dist/domain/memory/rules-loader.d.ts +27 -0
  230. package/dist/domain/memory/rules-loader.js +97 -0
  231. package/dist/domain/memory/skill-scanner.d.ts +5 -0
  232. package/dist/domain/memory/skill-scanner.js +132 -0
  233. package/dist/domain/memory/user-context.d.ts +5 -0
  234. package/dist/domain/memory/user-context.js +34 -0
  235. package/dist/domain/memory/watcher.d.ts +2 -0
  236. package/dist/domain/memory/watcher.js +81 -0
  237. package/dist/domain/monitor/disk-monitor.d.ts +20 -0
  238. package/dist/domain/monitor/disk-monitor.js +97 -0
  239. package/dist/domain/monitor/gpu-monitor.d.ts +26 -0
  240. package/dist/domain/monitor/gpu-monitor.js +135 -0
  241. package/dist/domain/remote/client-bootstrap.d.ts +1 -0
  242. package/dist/domain/remote/client-bootstrap.js +139 -0
  243. package/dist/domain/remote/client-hot-reload.d.ts +18 -0
  244. package/dist/domain/remote/client-hot-reload.js +142 -0
  245. package/dist/domain/remote/client-manager.d.ts +50 -0
  246. package/dist/domain/remote/client-manager.js +452 -0
  247. package/dist/domain/remote/cortex-client.d.ts +1 -0
  248. package/dist/domain/remote/cortex-client.js +704 -0
  249. package/dist/domain/scheduling/job-registry.d.ts +31 -0
  250. package/dist/domain/scheduling/job-registry.js +34 -0
  251. package/dist/domain/scheduling/jobs/_shared.d.ts +14 -0
  252. package/dist/domain/scheduling/jobs/_shared.js +45 -0
  253. package/dist/domain/scheduling/jobs/memory-index-regen.d.ts +1 -0
  254. package/dist/domain/scheduling/jobs/memory-index-regen.js +16 -0
  255. package/dist/domain/scheduling/jobs/scheduled-task.d.ts +11 -0
  256. package/dist/domain/scheduling/jobs/scheduled-task.js +186 -0
  257. package/dist/domain/scheduling/jobs/target-dispatch.d.ts +41 -0
  258. package/dist/domain/scheduling/jobs/target-dispatch.js +48 -0
  259. package/dist/domain/scheduling/jobs/task-archive.d.ts +1 -0
  260. package/dist/domain/scheduling/jobs/task-archive.js +19 -0
  261. package/dist/domain/scheduling/jobs/task-dispatch.d.ts +12 -0
  262. package/dist/domain/scheduling/jobs/task-dispatch.js +214 -0
  263. package/dist/domain/scheduling/runner.d.ts +13 -0
  264. package/dist/domain/scheduling/runner.js +24 -0
  265. package/dist/domain/scheduling/schedule-cli.d.ts +16 -0
  266. package/dist/domain/scheduling/schedule-cli.js +268 -0
  267. package/dist/domain/scheduling/schedule-command.d.ts +4 -0
  268. package/dist/domain/scheduling/schedule-command.js +274 -0
  269. package/dist/domain/scheduling/scheduler.d.ts +86 -0
  270. package/dist/domain/scheduling/scheduler.js +544 -0
  271. package/dist/domain/sessions/session-backup.d.ts +49 -0
  272. package/dist/domain/sessions/session-backup.js +217 -0
  273. package/dist/domain/sessions/session-hooks.d.ts +103 -0
  274. package/dist/domain/sessions/session-hooks.js +311 -0
  275. package/dist/domain/sessions/session-registry.d.ts +21 -0
  276. package/dist/domain/sessions/session-registry.js +31 -0
  277. package/dist/domain/sessions/session.d.ts +3 -0
  278. package/dist/domain/sessions/session.js +14 -0
  279. package/dist/domain/tasks/archiver.d.ts +9 -0
  280. package/dist/domain/tasks/archiver.js +123 -0
  281. package/dist/domain/tasks/dispatch-utils.d.ts +37 -0
  282. package/dist/domain/tasks/dispatch-utils.js +143 -0
  283. package/dist/domain/tasks/dispatcher.d.ts +48 -0
  284. package/dist/domain/tasks/dispatcher.js +285 -0
  285. package/dist/domain/tasks/lint.d.ts +12 -0
  286. package/dist/domain/tasks/lint.js +102 -0
  287. package/dist/domain/tasks/mutator.d.ts +21 -0
  288. package/dist/domain/tasks/mutator.js +208 -0
  289. package/dist/domain/tasks/parser.d.ts +1 -0
  290. package/dist/domain/tasks/parser.js +6 -0
  291. package/dist/domain/tasks/pending-tracker.d.ts +67 -0
  292. package/dist/domain/tasks/pending-tracker.js +163 -0
  293. package/dist/domain/tasks/recommendation/extractor.d.ts +12 -0
  294. package/dist/domain/tasks/recommendation/extractor.js +563 -0
  295. package/dist/domain/tasks/recommendation/patterns.d.ts +11 -0
  296. package/dist/domain/tasks/recommendation/patterns.js +99 -0
  297. package/dist/domain/tasks/recommendation/types.d.ts +67 -0
  298. package/dist/domain/tasks/recommendation/types.js +5 -0
  299. package/dist/domain/tasks/store.d.ts +1 -0
  300. package/dist/domain/tasks/store.js +3 -0
  301. package/dist/domain/tasks/system/cortex-run.d.ts +24 -0
  302. package/dist/domain/tasks/system/cortex-run.js +342 -0
  303. package/dist/domain/tasks/system/task-cli.d.ts +49 -0
  304. package/dist/domain/tasks/system/task-cli.js +664 -0
  305. package/dist/domain/tasks/system/task-completion.d.ts +22 -0
  306. package/dist/domain/tasks/system/task-completion.js +89 -0
  307. package/dist/domain/tasks/system/task-id-utils.d.ts +24 -0
  308. package/dist/domain/tasks/system/task-id-utils.js +84 -0
  309. package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +34 -0
  310. package/dist/domain/tasks/system/task-lifecycle-edit.js +208 -0
  311. package/dist/domain/tasks/system/task-lock.d.ts +25 -0
  312. package/dist/domain/tasks/system/task-lock.js +139 -0
  313. package/dist/domain/tasks/system/task-mutations.d.ts +31 -0
  314. package/dist/domain/tasks/system/task-mutations.js +110 -0
  315. package/dist/domain/tasks/system/task-process.d.ts +37 -0
  316. package/dist/domain/tasks/system/task-process.js +119 -0
  317. package/dist/domain/tasks/system/task-state.d.ts +70 -0
  318. package/dist/domain/tasks/system/task-state.js +176 -0
  319. package/dist/domain/threads/artifact-io.d.ts +18 -0
  320. package/dist/domain/threads/artifact-io.js +182 -0
  321. package/dist/domain/threads/auto-thread.d.ts +3 -0
  322. package/dist/domain/threads/auto-thread.js +19 -0
  323. package/dist/domain/threads/hook-runner.d.ts +4 -0
  324. package/dist/domain/threads/hook-runner.js +250 -0
  325. package/dist/domain/threads/index.d.ts +5 -0
  326. package/dist/domain/threads/index.js +7 -0
  327. package/dist/domain/threads/prompt-builder.d.ts +25 -0
  328. package/dist/domain/threads/prompt-builder.js +176 -0
  329. package/dist/domain/threads/runner.d.ts +83 -0
  330. package/dist/domain/threads/runner.js +434 -0
  331. package/dist/domain/threads/state-machine.d.ts +61 -0
  332. package/dist/domain/threads/state-machine.js +440 -0
  333. package/dist/domain/threads/template-loader.d.ts +20 -0
  334. package/dist/domain/threads/template-loader.js +189 -0
  335. package/dist/domain/threads/template-resolver.d.ts +1 -0
  336. package/dist/domain/threads/template-resolver.js +95 -0
  337. package/dist/domain/threads/utils.d.ts +19 -0
  338. package/dist/domain/threads/utils.js +49 -0
  339. package/dist/entry/app.d.ts +1 -0
  340. package/dist/entry/app.js +234 -0
  341. package/dist/entry/cli.d.ts +12 -0
  342. package/dist/entry/cli.js +254 -0
  343. package/dist/entry/daemon.d.ts +11 -0
  344. package/dist/entry/daemon.js +521 -0
  345. package/dist/entry/init.d.ts +93 -0
  346. package/dist/entry/init.js +1299 -0
  347. package/dist/entry/startup-helpers.d.ts +2 -0
  348. package/dist/entry/startup-helpers.js +56 -0
  349. package/dist/entry/startup-notify.d.ts +10 -0
  350. package/dist/entry/startup-notify.js +20 -0
  351. package/dist/events/event-bus.d.ts +42 -0
  352. package/dist/events/event-bus.js +84 -0
  353. package/dist/events/event-logger.d.ts +12 -0
  354. package/dist/events/event-logger.js +135 -0
  355. package/dist/events/event-replay.d.ts +1 -0
  356. package/dist/events/event-replay.js +51 -0
  357. package/dist/events/event-types.d.ts +145 -0
  358. package/dist/events/event-types.js +5 -0
  359. package/dist/events/index.d.ts +5 -0
  360. package/dist/events/index.js +6 -0
  361. package/dist/hooks/ask-user-question-hook.mjs +66 -0
  362. package/dist/hooks/cortex-md-injector.mjs +237 -0
  363. package/dist/hooks/exit-plan-mode-hook.mjs +84 -0
  364. package/dist/hooks/memory-ref-tracker.mjs +154 -0
  365. package/dist/hooks/new-session-hook.mjs +83 -0
  366. package/dist/hooks/post-task-hook.mjs +63 -0
  367. package/dist/hooks/rules-loader.mjs +212 -0
  368. package/dist/hooks/sensitive-file-edit.mjs +84 -0
  369. package/dist/hooks/session-activity-tracker.mjs +254 -0
  370. package/dist/hooks/task-status-check.mjs +218 -0
  371. package/dist/hooks/tasks-yaml-guard.mjs +230 -0
  372. package/dist/orchestration/agent-runner.d.ts +72 -0
  373. package/dist/orchestration/agent-runner.js +289 -0
  374. package/dist/orchestration/busy-tracker.d.ts +12 -0
  375. package/dist/orchestration/busy-tracker.js +35 -0
  376. package/dist/orchestration/channel-queue.d.ts +18 -0
  377. package/dist/orchestration/channel-queue.js +32 -0
  378. package/dist/orchestration/dispatch-reconciler.d.ts +1 -0
  379. package/dist/orchestration/dispatch-reconciler.js +16 -0
  380. package/dist/orchestration/durable-helpers.d.ts +4 -0
  381. package/dist/orchestration/durable-helpers.js +42 -0
  382. package/dist/orchestration/interactions/ask-user-question.d.ts +32 -0
  383. package/dist/orchestration/interactions/ask-user-question.js +182 -0
  384. package/dist/orchestration/interactions/command-action-router.d.ts +47 -0
  385. package/dist/orchestration/interactions/command-action-router.js +91 -0
  386. package/dist/orchestration/interactions/interaction-handlers.d.ts +5 -0
  387. package/dist/orchestration/interactions/interaction-handlers.js +279 -0
  388. package/dist/orchestration/interactions/plan-approvals.d.ts +47 -0
  389. package/dist/orchestration/interactions/plan-approvals.js +77 -0
  390. package/dist/orchestration/interactions/plan-handler.d.ts +6 -0
  391. package/dist/orchestration/interactions/plan-handler.js +12 -0
  392. package/dist/orchestration/lifecycle.d.ts +50 -0
  393. package/dist/orchestration/lifecycle.js +308 -0
  394. package/dist/orchestration/orchestrator.d.ts +29 -0
  395. package/dist/orchestration/orchestrator.js +25 -0
  396. package/dist/orchestration/routing/commands/cancel.d.ts +10 -0
  397. package/dist/orchestration/routing/commands/cancel.js +136 -0
  398. package/dist/orchestration/routing/commands/channel.d.ts +11 -0
  399. package/dist/orchestration/routing/commands/channel.js +219 -0
  400. package/dist/orchestration/routing/commands/command-context.d.ts +29 -0
  401. package/dist/orchestration/routing/commands/command-context.js +4 -0
  402. package/dist/orchestration/routing/commands/cost.d.ts +3 -0
  403. package/dist/orchestration/routing/commands/cost.js +22 -0
  404. package/dist/orchestration/routing/commands/device.d.ts +6 -0
  405. package/dist/orchestration/routing/commands/device.js +83 -0
  406. package/dist/orchestration/routing/commands/dispatch.d.ts +2 -0
  407. package/dist/orchestration/routing/commands/dispatch.js +49 -0
  408. package/dist/orchestration/routing/commands/index.d.ts +15 -0
  409. package/dist/orchestration/routing/commands/index.js +130 -0
  410. package/dist/orchestration/routing/commands/mode.d.ts +13 -0
  411. package/dist/orchestration/routing/commands/mode.js +290 -0
  412. package/dist/orchestration/routing/commands/nvtop.d.ts +3 -0
  413. package/dist/orchestration/routing/commands/nvtop.js +103 -0
  414. package/dist/orchestration/routing/commands/orient.d.ts +2 -0
  415. package/dist/orchestration/routing/commands/orient.js +3 -0
  416. package/dist/orchestration/routing/commands/schedule.d.ts +4 -0
  417. package/dist/orchestration/routing/commands/schedule.js +132 -0
  418. package/dist/orchestration/routing/commands/sendfile.d.ts +2 -0
  419. package/dist/orchestration/routing/commands/sendfile.js +92 -0
  420. package/dist/orchestration/routing/commands/session.d.ts +9 -0
  421. package/dist/orchestration/routing/commands/session.js +153 -0
  422. package/dist/orchestration/routing/commands/status.d.ts +7 -0
  423. package/dist/orchestration/routing/commands/status.js +180 -0
  424. package/dist/orchestration/routing/commands/tail.d.ts +2 -0
  425. package/dist/orchestration/routing/commands/tail.js +73 -0
  426. package/dist/orchestration/routing/commands/task.d.ts +6 -0
  427. package/dist/orchestration/routing/commands/task.js +142 -0
  428. package/dist/orchestration/routing/commands/thread-handlers.d.ts +2 -0
  429. package/dist/orchestration/routing/commands/thread-handlers.js +138 -0
  430. package/dist/orchestration/routing/commands/thread.d.ts +2 -0
  431. package/dist/orchestration/routing/commands/thread.js +2 -0
  432. package/dist/orchestration/routing/edit-handler.d.ts +24 -0
  433. package/dist/orchestration/routing/edit-handler.js +161 -0
  434. package/dist/orchestration/routing/file-handler.d.ts +6 -0
  435. package/dist/orchestration/routing/file-handler.js +23 -0
  436. package/dist/orchestration/routing/hook-bridge-subscribers.d.ts +4 -0
  437. package/dist/orchestration/routing/hook-bridge-subscribers.js +79 -0
  438. package/dist/orchestration/routing/hook-bridge.d.ts +30 -0
  439. package/dist/orchestration/routing/hook-bridge.js +112 -0
  440. package/dist/orchestration/routing/message-router.d.ts +6 -0
  441. package/dist/orchestration/routing/message-router.js +70 -0
  442. package/dist/orchestration/routing/webhook.d.ts +10 -0
  443. package/dist/orchestration/routing/webhook.js +239 -0
  444. package/dist/orchestration/status-helpers.d.ts +66 -0
  445. package/dist/orchestration/status-helpers.js +222 -0
  446. package/dist/orchestration/superseded-edits.d.ts +22 -0
  447. package/dist/orchestration/superseded-edits.js +31 -0
  448. package/dist/orchestration/thread-executor.d.ts +31 -0
  449. package/dist/orchestration/thread-executor.js +234 -0
  450. package/dist/platform/adapter.d.ts +28 -0
  451. package/dist/platform/adapter.js +5 -0
  452. package/dist/platform/adapters/feishu.d.ts +66 -0
  453. package/dist/platform/adapters/feishu.js +558 -0
  454. package/dist/platform/adapters/index.d.ts +16 -0
  455. package/dist/platform/adapters/index.js +65 -0
  456. package/dist/platform/adapters/slack.d.ts +90 -0
  457. package/dist/platform/adapters/slack.js +630 -0
  458. package/dist/platform/index.d.ts +12 -0
  459. package/dist/platform/index.js +10 -0
  460. package/dist/platform/interactive-builder.d.ts +27 -0
  461. package/dist/platform/interactive-builder.js +120 -0
  462. package/dist/platform/testing.d.ts +100 -0
  463. package/dist/platform/testing.js +190 -0
  464. package/dist/platform/tool-trace.d.ts +33 -0
  465. package/dist/platform/tool-trace.js +165 -0
  466. package/dist/platform/types.d.ts +176 -0
  467. package/dist/platform/types.js +5 -0
  468. package/dist/platform/utils/rate-limiter.d.ts +92 -0
  469. package/dist/platform/utils/rate-limiter.js +213 -0
  470. package/dist/platform/virtual-message.d.ts +68 -0
  471. package/dist/platform/virtual-message.js +464 -0
  472. package/dist/store/atomic-write.d.ts +6 -0
  473. package/dist/store/atomic-write.js +18 -0
  474. package/dist/store/channel-repo.d.ts +21 -0
  475. package/dist/store/channel-repo.js +55 -0
  476. package/dist/store/conversation-ledger-repo.d.ts +86 -0
  477. package/dist/store/conversation-ledger-repo.js +220 -0
  478. package/dist/store/cost-repo.d.ts +57 -0
  479. package/dist/store/cost-repo.js +135 -0
  480. package/dist/store/execution-repo.d.ts +172 -0
  481. package/dist/store/execution-repo.js +343 -0
  482. package/dist/store/in-memory-repository.d.ts +14 -0
  483. package/dist/store/in-memory-repository.js +31 -0
  484. package/dist/store/json-repository.d.ts +51 -0
  485. package/dist/store/json-repository.js +118 -0
  486. package/dist/store/outbound-queue.d.ts +65 -0
  487. package/dist/store/outbound-queue.js +269 -0
  488. package/dist/store/profile-repo.d.ts +36 -0
  489. package/dist/store/profile-repo.js +144 -0
  490. package/dist/store/project-dir-repo.d.ts +22 -0
  491. package/dist/store/project-dir-repo.js +60 -0
  492. package/dist/store/schedule-repo.d.ts +92 -0
  493. package/dist/store/schedule-repo.js +88 -0
  494. package/dist/store/session-registry-repo.d.ts +38 -0
  495. package/dist/store/session-registry-repo.js +96 -0
  496. package/dist/store/session-repo.d.ts +14 -0
  497. package/dist/store/session-repo.js +56 -0
  498. package/dist/store/task-repo.d.ts +41 -0
  499. package/dist/store/task-repo.js +137 -0
  500. package/dist/store/thread-repo.d.ts +50 -0
  501. package/dist/store/thread-repo.js +190 -0
  502. package/package.json +58 -0
  503. package/scripts/postinstall-restart-trigger.mjs +39 -0
@@ -0,0 +1,478 @@
1
+ ---
2
+ name: thread
3
+ description: "Use when working with the Cortex thread system — understanding thread architecture, writing or modifying thread-templates.json (agents, templates, transitions, hooks), debugging thread execution, or when the user asks about multi-agent pipelines, agent orchestration, or the !thread command. Also trigger when modifying prompts/directives/, prompts/systemPrompts/, or prompts/promptTemplates/ files that feed into threads."
4
+ author: Cortex
5
+ version: 1.0.0
6
+ allowed-tools:
7
+ - Read
8
+ - Write
9
+ - Edit
10
+ - Bash
11
+ - Grep
12
+ - Glob
13
+ date: 2026-04-12
14
+ ---
15
+
16
+ # Thread System
17
+
18
+ You are Cortex, working with the Thread multi-agent orchestration system.
19
+
20
+ ## Arguments
21
+ $ARGUMENTS
22
+
23
+ ---
24
+
25
+ ## What Is a Thread?
26
+
27
+ A Thread is a **multi-agent orchestration work unit** — a managed pipeline that coordinates one or more Claude Code agent instances. Each thread has:
28
+
29
+ - A unique `ThreadId` (`thr_XXXXX`)
30
+ - A file-system workspace at `{REPO_ROOT}/tmp/threads/thr_XXXX/`
31
+ - A shared `artifact.md` file through which agents communicate
32
+ - A lifecycle with states: `running` → `completed` | `failed` | `cancelled` | `aborted`
33
+ - Cost tracking and step history
34
+
35
+ Threads can be **template-based** (following a predefined multi-agent pipeline) or **ad-hoc** (dynamically assembled by adding agents one at a time).
36
+
37
+ ### Architecture Layer Diagram
38
+
39
+ ```
40
+ Slack message / Scheduler / Task Dispatch
41
+ │
42
+ message-router.ts / scheduled-runner.ts
43
+ │
44
+ thread-manager.ts (orchestration: create, step, transition, prompt assembly)
45
+ │
46
+ thread-runner.ts (execution loop: run agents, hooks, Slack output)
47
+ │
48
+ runAgent() → claude-bridge.ts → Claude Code CLI process
49
+ │
50
+ VirtualSlackMessage → Slack
51
+ ```
52
+
53
+ ### Thread Lifecycle States
54
+
55
+ | State | Meaning | Terminal? |
56
+ |-------|---------|-----------|
57
+ | `running` | An agent step is in progress | No |
58
+ | `waiting` | Paused, waiting for user input | No |
59
+ | `completed` | All steps done | Yes |
60
+ | `failed` | Unrecoverable error | Yes |
61
+ | `cancelled` | User cancelled via `!thread cancel` | Yes |
62
+ | `aborted` | Agent self-aborted via `[ABORT]` marker in artifact | Yes |
63
+
64
+ ---
65
+
66
+ ## Abort Mechanism
67
+
68
+ When an agent determines its task cannot be completed or is blocked by an irrecoverable condition, it can terminate the thread early by writing an abort marker to the artifact file.
69
+
70
+ ### How Agents Know About Abort
71
+
72
+ The abort capability is **auto-injected** by the thread system — you do **not** need to add anything to an agent's directive. `thread-manager.buildStepPrompt` prepends a short `THREAD_PROTOCOL_PREAMBLE` block (defined in `agent-server/src/thread-manager.ts`) to every step prompt of any thread that owns a workspace artifact. The preamble is skipped for auto-record / default / direct paths (no artifact to write to) and skipped on `--resume` of a persistent session (already delivered on first trigger).
73
+
74
+ Practical consequence: adding a new artifact-writing agent to `thread-templates.json` automatically gets the abort capability without editing directive / promptTemplate / systemPrompt.
75
+
76
+ ### Marker Format
77
+
78
+ - `[ABORT]` — terminate with no reason
79
+ - `[ABORT: <reason>]` — terminate with a free-text reason (trimmed, stored in `thread.abortReason`)
80
+
81
+ The marker is matched with regex `/\[ABORT(?::\s*([^\]\n]*))?\]/`. It may appear anywhere in the artifact, but placement at the end is recommended for clarity.
82
+
83
+ ### When to Use
84
+
85
+ An agent should write `[ABORT: <reason>]` when:
86
+
87
+ - **Upstream is missing or malformed** — required inputs (previous agent's artifact section, referenced files, dependencies) are absent or corrupt and cannot be produced by retrying.
88
+ - **Hardware / external resource unavailable** — e.g., target machine offline, GPU exhausted, external API permanently unreachable.
89
+ - **Specification unresolvable** — the user/task requirements contain a contradiction that cannot be decided autonomously.
90
+ - **Repeated failure without signal of progress** — the agent is stuck in a loop that more retries will not fix.
91
+
92
+ Do **not** abort for:
93
+
94
+ - Normal retry situations (use the `[REVISED]` convention and let the reviewer loop handle it).
95
+ - Minor warnings that do not block the task.
96
+ - Disagreements with the plan that can be recorded as feedback in the artifact.
97
+
98
+ ### Execution Semantics
99
+
100
+ When the thread-runner detects the marker after a step completes:
101
+
102
+ 1. Thread status → `aborted`; `abortReason` is recorded.
103
+ 2. No further steps execute (transitions are skipped).
104
+ 3. `onEnd` hook still fires — `/compound-simple`, git commit, and other cleanup work normally.
105
+ 4. `finalizeThread` reads the artifact as the final output and flushes to Slack.
106
+
107
+ ### Comparison with Other Terminators
108
+
109
+ | Mechanism | Who writes it | Effect |
110
+ |-----------|---------------|--------|
111
+ | `[APPROVED]` / `[IMPL-APPROVED]` | Reviewer agent | Convergence marker — reviewer approves, transition to next agent |
112
+ | `[REVISED]` | Doer agent (after retry) | Signals retry complete; `output_not_contains` transition terminates loop |
113
+ | `[ABORT[: reason]]` | Any agent | Global abort — thread enters `aborted` terminal state, overrides all transitions |
114
+ | `!thread cancel` | User (Slack command) | Thread enters `cancelled` terminal state |
115
+
116
+ ### Quoting Caveat
117
+
118
+ If an agent needs to quote or reference the literal string `[ABORT]` (e.g., reviewer explaining the mechanism), it should wrap it in backticks (`` `[ABORT]` ``) or escape the brackets. The marker regex matches unescaped bracketed text anywhere in the artifact, so unquoted references can accidentally trigger abort.
119
+
120
+ ---
121
+
122
+ ## Configuration File: thread-templates.json
123
+
124
+ Location: `agent-server/thread-templates.json`
125
+
126
+ This file has two top-level sections: `agents` (independent agent definitions) and `templates` (multi-agent pipeline templates). The config supports **hot-reload** — changes are detected automatically via `fs.watch` with a 300ms debounce.
127
+
128
+ ### Agents Section
129
+
130
+ Each agent is an independent definition with its own identity, profile, and prompt configuration. Agents are referenced by templates but can also be used standalone via `!thread <agent-name> <message>`.
131
+
132
+ ```json
133
+ {
134
+ "agents": {
135
+ "my-agent": {
136
+ "name": "my-agent",
137
+ "description": "Human-readable description",
138
+ "profile": "execute",
139
+ "persistSession": false,
140
+ "directive": "You are a specialized agent for...",
141
+ "promptTemplate": "{{input}}\n\nWrite your output to {{artifactPath}}.",
142
+ "tools": "Agent,Bash,Edit,Glob,Grep,Read,Write"
143
+ }
144
+ }
145
+ }
146
+ ```
147
+
148
+ #### Agent Fields Reference
149
+
150
+ | Field | Required | Type | Description |
151
+ |-------|----------|------|-------------|
152
+ | `name` | Yes | string | Agent ID, must match the key in the agents map |
153
+ | `description` | No | string | Human-readable label shown in `!thread agents` |
154
+ | `profile` | Yes | string | Profile from `profiles.json`, or `"__active__"` for current runtime profile |
155
+ | `persistSession` | Yes | boolean | `true`: reuse Claude Code session across loop iterations. `false`: fresh session each time |
156
+ | `directive` | No | string | Role/identity text prepended to the prompt. Supports `file:filename.md` to load from `prompts/directives/` |
157
+ | `systemPrompt` | No | string | Full system prompt override via `--system-prompt` flag. Supports `file:filename.md` to load from `prompts/systemPrompts/` |
158
+ | `promptTemplate` | Yes | string | Handlebars-style template with variables. Supports `file:filename.md` to load from `prompts/promptTemplates/` |
159
+ | `claudeAgent` | No | string | Claude Code agent name, loaded via `--agent` flag from `.claude/agents/` |
160
+ | `outputStyle` | No | string | Injected via Claude CLI `--settings '{"outputStyle":"<value>"}'` |
161
+ | `tools` | No | string | Comma-separated tool list for `--tools` flag. Overrides the default tool set |
162
+ | `pluginDirs` | No | string[] | Plugin directories passed via `--plugin-dir` flags |
163
+
164
+ #### persistSession Explained
165
+
166
+ - `true` — The Claude Code process stays alive between loop iterations. The agent retains full conversation history. Use for agents that iterate on feedback (planner revising after review, writer revising after critique).
167
+ - `false` — A fresh Claude Code session is spawned each time the agent runs. Use for stateless evaluators (reviewer, QA) where independence from prior context is important.
168
+
169
+ #### file: Reference Mechanism
170
+
171
+ Directive, systemPrompt, and promptTemplate values starting with `file:` are loaded from corresponding subdirectories under `prompts/`:
172
+
173
+ ```
174
+ prompts/
175
+ ├── directives/ ← for directive: "file:director.md"
176
+ ├── systemPrompts/ ← for systemPrompt: "file:web.md"
177
+ └── promptTemplates/ ← for promptTemplate: "file:my-template.md"
178
+ ```
179
+
180
+ Loaded files are processed through `template-resolver.ts`, which supports:
181
+ - **YAML frontmatter** with `extends:` (template inheritance) and variable declarations
182
+ - **`@fill(name)` / `@endfill`** blocks to fill template slots
183
+ - **`@block(name)` / `@endblock`** for default content slots in base templates
184
+ - **`${var}` / `${var:-default}`** variable interpolation
185
+ - **`@if(var)` / `@if(!var)` / `@endif`** conditional sections
186
+ - **`{{runtime_vars}}`** (e.g., `{{currentDateTime}}`) pass through untouched for runtime resolution
187
+
188
+ ### Templates Section
189
+
190
+ Templates define multi-agent pipelines by composing agents with transition rules.
191
+
192
+ ```json
193
+ {
194
+ "templates": {
195
+ "my-pipeline": {
196
+ "name": "my-pipeline",
197
+ "description": "Description of what this pipeline does",
198
+ "agents": ["agent-a", {"ref": "agent-b", "promptTemplate": "override..."}],
199
+ "transitions": [
200
+ {"from": "agent-a", "to": "agent-b", "condition": {"type": "always"}}
201
+ ],
202
+ "entryAgent": "agent-a",
203
+ "maxTotalSteps": 6,
204
+ "maxTotalCostUsd": 5.0,
205
+ "hooks": {
206
+ "onEnd": {
207
+ "command": "node ~/.cortex/hooks/post-task-hook.mjs",
208
+ "args": ["agent-a"],
209
+ "timeout": 10000
210
+ }
211
+ }
212
+ }
213
+ }
214
+ }
215
+ ```
216
+
217
+ #### Template Fields Reference
218
+
219
+ | Field | Required | Type | Description |
220
+ |-------|----------|------|-------------|
221
+ | `name` | Yes | string | Template ID, must match the key |
222
+ | `description` | Yes | string | Human-readable description |
223
+ | `agents` | Yes | TemplateAgentRef[] | Ordered list of agent references (string name or override object) |
224
+ | `transitions` | Yes | TransitionRule[] | Rules governing agent-to-agent transitions |
225
+ | `entryAgent` | Yes | string | Which agent starts the pipeline |
226
+ | `maxTotalSteps` | Yes | number | Hard cap on total agent steps |
227
+ | `maxTotalCostUsd` | No | number | Cost limit in USD. Pipeline stops when exceeded |
228
+ | `hooks` | No | ThreadHooks | Lifecycle hooks (onStart, onTransition, onEnd) |
229
+
230
+ #### Agent References in Templates
231
+
232
+ Templates reference agents in two ways:
233
+
234
+ **Simple reference** (string): Uses the agent definition as-is.
235
+ ```json
236
+ "agents": ["planner", "reviewer"]
237
+ ```
238
+
239
+ **Override reference** (object): Overrides specific fields for this template only.
240
+ ```json
241
+ "agents": [
242
+ {
243
+ "ref": "planner",
244
+ "promptTemplate": "Custom template for this pipeline...",
245
+ "persistSession": true
246
+ },
247
+ "reviewer"
248
+ ]
249
+ ```
250
+
251
+ Override objects support: `ref` (required), `promptTemplate`, `directive`, `systemPrompt`, `persistSession`, `claudeAgent`, `outputStyle`, `tools`, `pluginDirs`.
252
+
253
+ ---
254
+
255
+ ## Prompt Template Variables
256
+
257
+ The `promptTemplate` field supports these variables, resolved at step execution time:
258
+
259
+ | Variable | Description |
260
+ |----------|-------------|
261
+ | `{{input}}` | The original user message that started the thread |
262
+ | `{{artifactPath}}` | Absolute path to the workspace's `artifact.md` file |
263
+ | `{{previousOutput}}` | The full output from the last completed agent step |
264
+ | `{{modifiedFiles}}` | Bullet list of files edited/written by the previous agent (from session-activity logs) |
265
+ | `{{modifiedFilesWithDiff}}` | Same files plus a fenced ```diff``` block per file showing the previous agent's net change. Covers local Edit/Write **and** remote `mcp__cortex__remote_edit/write`. Reconstructed without trusting `record[i>0].originalFile` so other agents touching the same file between/after operations don't pollute attribution. Falls back to raw hunks + warning when external interleaving breaks patch context. |
266
+ | `{{currentDateTime}}` | Current timestamp in Asia/Shanghai timezone |
267
+
268
+ Conditional blocks are supported:
269
+ ```
270
+ {{#if previousOutput}}
271
+ Review feedback:
272
+ {{previousOutput}}
273
+ Please revise based on the feedback.
274
+ {{/if}}
275
+ ```
276
+
277
+ Directive and systemPrompt fields also support `{{currentDateTime}}` and other system variables via `resolveSystemVars()`.
278
+
279
+ ---
280
+
281
+ ## Transition Rules
282
+
283
+ Transitions define how agents hand off to each other. Each rule has `from`, `to`, and a `condition`.
284
+
285
+ ### Condition Types
286
+
287
+ | Type | Fields | Behavior |
288
+ |------|--------|----------|
289
+ | `always` | — | Always transition to the next agent |
290
+ | `convergence` | `marker`, `maxIterations` | Loop until `marker` string found in artifact content, or `maxIterations` reached |
291
+ | `output_contains` | `pattern` | Transition if the artifact content matches the regex `pattern` |
292
+ | `output_not_contains` | `pattern` | Transition if the artifact content does NOT match the regex `pattern` |
293
+
294
+ ### Common Transition Patterns
295
+
296
+ **Linear pipeline** (A → B → C):
297
+ ```json
298
+ "transitions": [
299
+ {"from": "A", "to": "B", "condition": {"type": "always"}},
300
+ {"from": "B", "to": "C", "condition": {"type": "always"}}
301
+ ]
302
+ ```
303
+
304
+ **Convergence loop** (A ↔ B until marker):
305
+ ```json
306
+ "transitions": [
307
+ {"from": "A", "to": "B", "condition": {"type": "always"}},
308
+ {"from": "B", "to": "A", "condition": {"type": "convergence", "marker": "[APPROVED]", "maxIterations": 5}}
309
+ ]
310
+ ```
311
+
312
+ The convergence condition means: if `[APPROVED]` is NOT found in the artifact, loop back to A. If found (or maxIterations hit), the pipeline stops.
313
+
314
+ **Conditional branching** (B → C if approved, B → A if not):
315
+ ```json
316
+ "transitions": [
317
+ {"from": "A", "to": "B", "condition": {"type": "always"}},
318
+ {"from": "B", "to": "C", "condition": {"type": "output_contains", "pattern": "\\[APPROVED\\]"}},
319
+ {"from": "B", "to": "A", "condition": {"type": "output_not_contains", "pattern": "\\[APPROVED\\]"}}
320
+ ]
321
+ ```
322
+
323
+ Transitions are evaluated in order — the first matching rule wins.
324
+
325
+ ---
326
+
327
+ ## Hooks System
328
+
329
+ Templates can define lifecycle hooks that execute external scripts at key moments.
330
+
331
+ ```json
332
+ "hooks": {
333
+ "onStart": { "script": "path/to/script.mjs", "args": ["arg1"], "timeout": 30000 },
334
+ "onTransition": { "script": "...", "timeout": 10000 },
335
+ "onEnd": { "script": "...", "args": ["target-agent"], "timeout": 10000 }
336
+ }
337
+ ```
338
+
339
+ | Hook | When | Use Case |
340
+ |------|------|----------|
341
+ | `onStart` | Before the first agent step | Setup, validation |
342
+ | `onTransition` | After each transition, before the next step | Intermediate processing |
343
+ | `onEnd` | After all steps complete | Cleanup, compound, git commit |
344
+
345
+ Hook scripts receive `HookContext` on stdin (JSON) and return `HookResult` on stdout (JSON):
346
+
347
+ **HookContext** (input):
348
+ ```typescript
349
+ {
350
+ threadId, templateName, phase, currentStepIndex,
351
+ steps, activeAgent, previousAgent,
352
+ artifactContent, userMessage, totalCostUsd
353
+ }
354
+ ```
355
+
356
+ **HookResult** (output) — two modes:
357
+ 1. `insertAgent: true` — Creates a temporary new agent to execute the prompt
358
+ 2. `targetAgent: "slotId"` — Sends a prompt to an existing agent's persistent session (process alive → stdin, dead → `--resume`)
359
+
360
+ Example hook script (`post-task-hook.mjs`): Checks if `/compound-simple` should run and if there are uncommitted git changes, then sends a combined prompt to the worker agent's session.
361
+
362
+ ---
363
+
364
+ ## Agent Communication
365
+
366
+ Agents in a thread communicate through three mechanisms:
367
+
368
+ 1. **Artifact file** (`{{artifactPath}}`): A shared `artifact.md` in the thread workspace. Agents read from and write to this file. This is the primary structured communication channel.
369
+
370
+ 2. **Output passing** (`{{previousOutput}}`): The full text output of the previous agent step is available to the next agent via this variable.
371
+
372
+ 3. **Modified files list** (`{{modifiedFiles}}`): A bullet list of files the previous agent edited/wrote, extracted from session-activity JSONL logs.
373
+
374
+ 4. **Modified files with diff** (`{{modifiedFilesWithDiff}}`): Same files plus a per-file fenced ```diff``` block showing the previous agent's net change. Covers local Edit/Write **and** remote `mcp__cortex__remote_edit/write` (uses the diff snapshot embedded by mcp-server in the tool response). Reconstruction never re-reads disk and never trusts `record[i>0].originalFile`, so other agents touching the same file between or after the previous agent's operations don't pollute attribution. When external interleaving breaks patch context, the variable falls back to raw hunks plus a warning.
375
+
376
+ For ad-hoc threads (no template), the previous agent's output is automatically injected into the next agent's prompt even without `{{previousOutput}}` in the template.
377
+
378
+ ---
379
+
380
+ ## Task Dispatch Integration
381
+
382
+ Tasks in TASKS.md use the `[template: <name>]` tag to specify which thread template to use when dispatched:
383
+
384
+ ```markdown
385
+ - [ ] Run ablation study [template: <template-name>]
386
+ Why: Need to isolate tactile contribution
387
+ Done when: Results in experiments/EXP-NNN.md
388
+ ```
389
+
390
+ The `[template:]` tag is **required** for all dispatchable tasks. Tasks without it are flagged by the `task-parser.ts` lint check (`missing-template`).
391
+
392
+ Dispatch flow: `task-dispatcher.ts` selects a task → extracts template name → `createThread({templateName})` → `runThreadExec()`.
393
+
394
+ ---
395
+
396
+ ## Key Source Files
397
+
398
+ | File | Role |
399
+ |------|------|
400
+ | `agent-server/thread-templates.json` | Agent + template configuration |
401
+ | `agent-server/src/thread-types.ts` | All TypeScript interfaces |
402
+ | `agent-server/src/thread-store.ts` | In-memory cache + threads.json persistence |
403
+ | `agent-server/src/thread-manager.ts` | Core orchestration (create, step, transition, prompt assembly) |
404
+ | `agent-server/src/thread-runner.ts` | Execution loop (run agents, hooks, Slack output) |
405
+ | `agent-server/src/template-resolver.ts` | `file:` reference expansion for prompts |
406
+ | `agent-server/src/message-router.ts` | Slack message → thread routing |
407
+ | `agent-server/src/scheduled-runner.ts` | Scheduler + dispatch → thread creation |
408
+ | `agent-server/src/task-dispatcher.ts` | Task selection and dispatch prompt building |
409
+ | `~/.cortex/hooks/post-task-hook.mjs` | onEnd hook for scheduler/worker templates |
410
+
411
+ ---
412
+
413
+ ## How to Add a New Agent
414
+
415
+ 1. Add the agent definition to the `agents` section of `thread-templates.json`
416
+ 2. If using a `file:` directive, create the directive file in `prompts/directives/`
417
+ 3. If using a `file:` systemPrompt, create it in `prompts/systemPrompts/`
418
+ 4. Choose the right `profile` (plan/execute/qa/__active__)
419
+ 5. Choose `persistSession` based on whether the agent needs to retain context across iterations
420
+ 6. The config hot-reloads — no restart needed
421
+
422
+ ## How to Add a New Template
423
+
424
+ 1. Define any new agents needed in the `agents` section
425
+ 2. Add the template to the `templates` section with:
426
+ - `agents`: list of agent refs (simple strings or override objects)
427
+ - `transitions`: rules connecting agents
428
+ - `entryAgent`: the starting agent
429
+ - `maxTotalSteps`: safety cap
430
+ 3. Optionally add `maxTotalCostUsd` for cost limits
431
+ 4. Optionally add `hooks` for lifecycle automation
432
+ 5. Test via `!thread <template-name> <test-message>` in Slack
433
+
434
+ ## Common Patterns for New Templates
435
+
436
+ **Single-agent with hook** (like worker/scheduler):
437
+ ```json
438
+ {
439
+ "name": "my-task",
440
+ "agents": ["my-agent"],
441
+ "transitions": [],
442
+ "entryAgent": "my-agent",
443
+ "maxTotalSteps": 1,
444
+ "hooks": { "onEnd": { "command": "node ~/.cortex/hooks/post-task-hook.mjs", "args": ["my-agent"], "timeout": 10000 } }
445
+ }
446
+ ```
447
+
448
+ **Two-agent convergence loop** (doer ↔ checker iteration):
449
+ ```json
450
+ {
451
+ "name": "my-loop",
452
+ "agents": ["doer", "checker"],
453
+ "transitions": [
454
+ {"from": "doer", "to": "checker", "condition": {"type": "always"}},
455
+ {"from": "checker", "to": "doer", "condition": {"type": "convergence", "marker": "[PASS]", "maxIterations": 3}}
456
+ ],
457
+ "entryAgent": "doer",
458
+ "maxTotalSteps": 8
459
+ }
460
+ ```
461
+
462
+ **Linear pipeline with optional prompt overrides**:
463
+ ```json
464
+ {
465
+ "name": "my-pipeline",
466
+ "agents": [
467
+ {"ref": "scout", "promptTemplate": "Focus on {{input}}\n\nWrite findings to {{artifactPath}}."},
468
+ "analyzer",
469
+ "reporter"
470
+ ],
471
+ "transitions": [
472
+ {"from": "scout", "to": "analyzer", "condition": {"type": "always"}},
473
+ {"from": "analyzer", "to": "reporter", "condition": {"type": "always"}}
474
+ ],
475
+ "entryAgent": "scout",
476
+ "maxTotalSteps": 4
477
+ }
478
+ ```
@@ -0,0 +1,95 @@
1
+ ---
2
+ name: user-learn
3
+ description: "Use when the user states a personal preference, communication style, or working habit that should persist across sessions. Also trigger when /feedback identifies a user-personal directive or tuning request. Examples: 'speak Chinese', 'no emoji', 'I prefer bullet points', 'call me X'."
4
+ author: Cortex
5
+ version: 1.0.0
6
+ allowed-tools:
7
+ - Read
8
+ - Edit
9
+ - Bash
10
+ - Grep
11
+ - Glob
12
+ date: 2026-04-27
13
+ ---
14
+
15
+ # User-Learn
16
+
17
+ You are Cortex, updating the user's persistent profile. Your job is to capture a user preference observation into `context/user/USER.md` so that all future direct-conversation sessions reflect it.
18
+
19
+ **If no observation is provided, stop immediately.** Say: "No preference provided. Usage: `/user-learn <what you observed about the user>`" and do nothing else.
20
+
21
+ ## The observation
22
+ $ARGUMENTS
23
+
24
+ ---
25
+
26
+ ## Constraints
27
+
28
+ - **USER.md hard limit: 3KB (3072 bytes).** Never let the file exceed this size.
29
+ - USER.md is injected into every direct-conversation thread prompt. Bloat here costs tokens on every turn.
30
+ - Prefer updating existing entries over adding new ones.
31
+ - Use terse, structured format (bullet lists, not prose).
32
+
33
+ ## Step 1: Parse the Observation
34
+
35
+ Classify what was observed:
36
+
37
+ | Type | Signal | Example |
38
+ |---|---|---|
39
+ | **Identity** | Name, role, affiliation | "Call me Fang Xin" |
40
+ | **Communication** | Language, tone, verbosity | "Speak Chinese", "Be more concise" |
41
+ | **Output style** | Format, structure, conventions | "Use bullet points", "No emoji" |
42
+ | **Technical context** | Tools, stacks, domains | "I mainly use PyTorch" |
43
+ | **Working style** | Workflow patterns, habits | "I review PRs in the morning" |
44
+
45
+ State the type and a one-sentence restatement.
46
+
47
+ Determine the source:
48
+ - **Explicit**: User directly stated the preference
49
+ - **Inferred**: Observed from repeated corrections or behavior patterns
50
+ - **Corrected**: User corrected a previous agent behavior
51
+
52
+ ## Step 2: Check Existing Profile
53
+
54
+ Read `context/user/USER.md`.
55
+
56
+ 1. Check if this preference is already recorded. If yes and unchanged, stop — say "Already recorded in USER.md" and do nothing.
57
+ 2. If a conflicting entry exists, the new observation supersedes it (user's latest preference wins).
58
+ 3. Note the current file size in bytes: `wc -c < context/user/USER.md`
59
+
60
+ ## Step 3: Size Gate
61
+
62
+ If current file size > 2500 bytes:
63
+ 1. Enter **compression mode**
64
+ 2. Review all entries — can any be merged, abbreviated, or removed?
65
+ 3. Compress to make room for the new entry
66
+ 4. If compression cannot bring the file under 2500 bytes, ask the user which entries to deprioritize
67
+
68
+ If current file size <= 2500 bytes, proceed directly to Step 4.
69
+
70
+ ## Step 4: Update USER.md
71
+
72
+ Using the Edit tool, modify the appropriate section:
73
+
74
+ - **New preference in existing section**: Add a bullet point or modify an existing one
75
+ - **New section needed**: Add under the most relevant `##` heading, or create a new one if none fits
76
+ - **Conflicting entry**: Replace the old entry with the new one
77
+ - Update the `last-updated` date in frontmatter to today
78
+ - Update `updated-by` to `Cortex`
79
+
80
+ **Format rules**:
81
+ - One bullet per preference
82
+ - No prose explanations — just the preference statement
83
+ - Use the same terse style as existing entries
84
+
85
+ ## Step 5: Verify Size
86
+
87
+ Run: `wc -c < context/user/USER.md`
88
+
89
+ If the file exceeds 3072 bytes, revert the change and report: "USER.md would exceed 3KB limit. Current size: X bytes. Please run `/user-learn` with compression guidance."
90
+
91
+ ## Step 6: Commit
92
+
93
+ ```bash
94
+ git add context/user/USER.md && git commit -m "user-profile: <one-line summary of change>"
95
+ ```