@cortex-agent/server 2026.5.19-3 → 2026.5.22-2

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 (134) hide show
  1. package/README.md +5 -1
  2. package/defaults/config/thread-templates.json +65 -39
  3. package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +4 -4
  4. package/defaults/prompts/directives/coder-reviewer.md +9 -4
  5. package/defaults/prompts/directives/coder.md +10 -2
  6. package/dist/core/gateway-generator.js +26 -4
  7. package/dist/core/log.js +61 -12
  8. package/dist/core/profile-generator.js +4 -3
  9. package/dist/core/status-format.js +1 -1
  10. package/dist/core/task-parser.d.ts +1 -1
  11. package/dist/core/task-parser.js +13 -6
  12. package/dist/core/types/thread-types.d.ts +4 -2
  13. package/dist/domain/costs/codex-usage-monitor.d.ts +1 -2
  14. package/dist/domain/costs/codex-usage-monitor.js +2 -2
  15. package/dist/domain/costs/cost-tracker.d.ts +2 -0
  16. package/dist/domain/costs/cost-tracker.js +4 -20
  17. package/dist/domain/costs/rate-limit-throttle.js +1 -4
  18. package/dist/domain/mcp/tools/context.d.ts +4 -2
  19. package/dist/domain/mcp/tools/context.js +7 -5
  20. package/dist/domain/mcp/tools/schedule.d.ts +1 -1
  21. package/dist/domain/mcp/tools/schedule.js +24 -20
  22. package/dist/domain/memory/index-regen.js +8 -5
  23. package/dist/domain/monitor/disk-monitor.js +1 -6
  24. package/dist/domain/projects/index.d.ts +2 -0
  25. package/dist/domain/projects/index.js +3 -0
  26. package/dist/domain/projects/project-store.d.ts +54 -0
  27. package/dist/domain/projects/project-store.js +194 -0
  28. package/dist/domain/projects/project-types.d.ts +11 -0
  29. package/dist/domain/projects/project-types.js +5 -0
  30. package/dist/domain/remote/client-hot-reload.d.ts +1 -0
  31. package/dist/domain/remote/client-hot-reload.js +284 -53
  32. package/dist/domain/scheduling/jobs/_shared.d.ts +1 -1
  33. package/dist/domain/scheduling/jobs/_shared.js +6 -5
  34. package/dist/domain/scheduling/jobs/memory-index-regen.js +2 -2
  35. package/dist/domain/scheduling/jobs/scheduled-task.d.ts +2 -2
  36. package/dist/domain/scheduling/jobs/scheduled-task.js +38 -36
  37. package/dist/domain/scheduling/jobs/sync-public.d.ts +1 -0
  38. package/dist/domain/scheduling/jobs/sync-public.js +34 -0
  39. package/dist/domain/scheduling/jobs/target-dispatch.d.ts +3 -7
  40. package/dist/domain/scheduling/jobs/target-dispatch.js +7 -9
  41. package/dist/domain/scheduling/jobs/task-archive.js +2 -2
  42. package/dist/domain/scheduling/jobs/task-dispatch.js +18 -16
  43. package/dist/domain/scheduling/runner.d.ts +1 -0
  44. package/dist/domain/scheduling/runner.js +2 -0
  45. package/dist/domain/scheduling/schedule-cli.d.ts +1 -0
  46. package/dist/domain/scheduling/schedule-cli.js +14 -14
  47. package/dist/domain/scheduling/schedule-command.js +45 -36
  48. package/dist/domain/scheduling/scheduler.d.ts +1 -1
  49. package/dist/domain/scheduling/scheduler.js +12 -11
  50. package/dist/domain/sessions/session-hooks.d.ts +10 -11
  51. package/dist/domain/sessions/session-hooks.js +26 -27
  52. package/dist/domain/sessions/session-registry.d.ts +10 -5
  53. package/dist/domain/sessions/session-registry.js +24 -9
  54. package/dist/domain/tasks/pending-tracker.js +8 -9
  55. package/dist/domain/tasks/system/task-cli.d.ts +2 -0
  56. package/dist/domain/tasks/system/task-cli.js +189 -29
  57. package/dist/domain/tasks/system/task-lifecycle-edit.js +3 -3
  58. package/dist/domain/threads/hook-runner.js +12 -7
  59. package/dist/domain/threads/runner.d.ts +3 -3
  60. package/dist/domain/threads/runner.js +30 -28
  61. package/dist/domain/threads/state-machine.d.ts +3 -0
  62. package/dist/domain/threads/state-machine.js +4 -0
  63. package/dist/entry/app.js +30 -9
  64. package/dist/entry/cli.js +19 -4
  65. package/dist/entry/daemon.js +28 -2
  66. package/dist/entry/startup-notify.js +8 -6
  67. package/dist/orchestration/agent-runner.js +19 -13
  68. package/dist/orchestration/durable-helpers.js +3 -2
  69. package/dist/orchestration/interactions/ask-user-question.d.ts +2 -3
  70. package/dist/orchestration/interactions/ask-user-question.js +5 -4
  71. package/dist/orchestration/interactions/interaction-handlers.js +8 -5
  72. package/dist/orchestration/interactions/plan-handler.js +3 -3
  73. package/dist/orchestration/lifecycle.js +43 -38
  74. package/dist/orchestration/routing/commands/cancel.js +9 -8
  75. package/dist/orchestration/routing/commands/channel.js +32 -26
  76. package/dist/orchestration/routing/commands/cost.js +5 -3
  77. package/dist/orchestration/routing/commands/device.js +2 -1
  78. package/dist/orchestration/routing/commands/dispatch.js +8 -7
  79. package/dist/orchestration/routing/commands/index.js +7 -4
  80. package/dist/orchestration/routing/commands/mode.js +24 -18
  81. package/dist/orchestration/routing/commands/nvtop.js +14 -12
  82. package/dist/orchestration/routing/commands/orient.js +2 -1
  83. package/dist/orchestration/routing/commands/schedule.js +2 -1
  84. package/dist/orchestration/routing/commands/sendfile.js +12 -9
  85. package/dist/orchestration/routing/commands/session.js +12 -10
  86. package/dist/orchestration/routing/commands/status.js +2 -2
  87. package/dist/orchestration/routing/commands/tail.js +10 -6
  88. package/dist/orchestration/routing/commands/task.js +5 -4
  89. package/dist/orchestration/routing/commands/thread-handlers.js +19 -12
  90. package/dist/orchestration/routing/hook-bridge-subscribers.js +15 -13
  91. package/dist/orchestration/status-helpers.d.ts +4 -5
  92. package/dist/orchestration/status-helpers.js +7 -8
  93. package/dist/orchestration/thread-executor.js +21 -12
  94. package/dist/platform/adapter.d.ts +13 -9
  95. package/dist/platform/adapter.js +1 -1
  96. package/dist/platform/adapters/feishu-output-stream.d.ts +27 -0
  97. package/dist/platform/adapters/feishu-output-stream.js +114 -0
  98. package/dist/platform/adapters/feishu.d.ts +16 -7
  99. package/dist/platform/adapters/feishu.js +64 -18
  100. package/dist/platform/adapters/index.d.ts +4 -1
  101. package/dist/platform/adapters/index.js +1 -1
  102. package/dist/platform/adapters/slack-output-stream.d.ts +55 -0
  103. package/dist/platform/{virtual-message.js → adapters/slack-output-stream.js} +98 -165
  104. package/dist/platform/adapters/slack-project-conduits.d.ts +9 -0
  105. package/dist/platform/adapters/slack-project-conduits.js +40 -0
  106. package/dist/platform/adapters/slack.d.ts +18 -8
  107. package/dist/platform/adapters/slack.js +82 -24
  108. package/dist/platform/index.d.ts +3 -2
  109. package/dist/platform/index.js +1 -1
  110. package/dist/platform/output-stream-chunk.d.ts +6 -0
  111. package/dist/platform/output-stream-chunk.js +45 -0
  112. package/dist/platform/output-stream-helpers.d.ts +6 -0
  113. package/dist/platform/output-stream-helpers.js +11 -0
  114. package/dist/platform/output-stream.d.ts +28 -0
  115. package/dist/platform/output-stream.js +4 -0
  116. package/dist/platform/testing.d.ts +41 -8
  117. package/dist/platform/testing.js +93 -11
  118. package/dist/platform/tool-trace.d.ts +9 -8
  119. package/dist/platform/tool-trace.js +19 -17
  120. package/dist/platform/types.d.ts +21 -1
  121. package/dist/platform/types.js +17 -1
  122. package/dist/store/channel-repo.d.ts +6 -0
  123. package/dist/store/channel-repo.js +9 -0
  124. package/dist/store/json-repository.js +12 -1
  125. package/dist/store/outbound-queue.d.ts +5 -4
  126. package/dist/store/outbound-queue.js +9 -6
  127. package/dist/store/schedule-repo.d.ts +9 -5
  128. package/dist/store/schedule-repo.js +59 -14
  129. package/dist/store/session-registry-repo.d.ts +36 -7
  130. package/dist/store/session-registry-repo.js +186 -25
  131. package/dist/store/thread-repo.d.ts +1 -0
  132. package/dist/store/thread-repo.js +8 -0
  133. package/package.json +1 -1
  134. package/dist/platform/virtual-message.d.ts +0 -68
@@ -23,29 +23,32 @@ function formatProfileList(channel) {
23
23
  }
24
24
  export async function handleModeCmd(channel, adapter) {
25
25
  const { newMode } = switchMode();
26
- await adapter.postMessage(channel, { text: `:arrows_counterclockwise: Switched to *${newMode === 'api' ? 'API' : 'Plan'}* mode` });
26
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
27
+ await adapter.postMessage(dest, { text: `:arrows_counterclockwise: Switched to *${newMode === 'api' ? 'API' : 'Plan'}* mode` });
27
28
  }
28
29
  export async function handleBackendCmd(channel, adapter, trimmedMessage) {
29
30
  const arg = trimmedMessage.split(/\s+/)[1];
30
31
  const newBackend = (arg === 'claude' || arg === 'codex') ? arg : (getActiveBackend() === 'claude' ? 'codex' : 'claude');
31
32
  setActiveBackend(newBackend);
32
- await adapter.postMessage(channel, {
33
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
34
+ await adapter.postMessage(dest, {
33
35
  text: `:arrows_counterclockwise: Backend: *${newBackend === 'claude' ? 'Claude Code' : 'Codex'}*`,
34
36
  });
35
37
  }
36
38
  export async function handleModelCmd(channel, adapter, trimmedMessage) {
37
39
  const args = trimmedMessage.split(/\s+/).slice(1);
40
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
38
41
  if (args.length === 0) {
39
- await adapter.postMessage(channel, { text: `Current Claude model: *${getClaudeModel()}*` });
42
+ await adapter.postMessage(dest, { text: `Current Claude model: *${getClaudeModel()}*` });
40
43
  return;
41
44
  }
42
45
  const model = args.join(' ').trim();
43
46
  if (!model) {
44
- await adapter.postMessage(channel, { text: ':x: Usage: `!model <model-name>`' });
47
+ await adapter.postMessage(dest, { text: ':x: Usage: `!model <model-name>`' });
45
48
  return;
46
49
  }
47
50
  setClaudeModel(model);
48
- await adapter.postMessage(channel, { text: `:white_check_mark: Claude model set to *${getClaudeModel()}*` });
51
+ await adapter.postMessage(dest, { text: `:white_check_mark: Claude model set to *${getClaudeModel()}*` });
49
52
  }
50
53
  const MAX_PROFILE_BUTTONS = 10;
51
54
  function buildProfileText(channel) {
@@ -105,28 +108,29 @@ export function createProfileHandler(router) {
105
108
  }
106
109
  return async function handleProfileCmdInteractive(channel, adapter, trimmedMessage) {
107
110
  const args = trimmedMessage.split(/\s+/).slice(1);
111
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
108
112
  if (args.length > 0) {
109
113
  if (args[0] === 'reset') {
110
114
  clearChannelProfile(channel);
111
115
  const fallback = getActiveProfile() || getDefaultProfileName();
112
- await adapter.postMessage(channel, { text: `:white_check_mark: Channel profile cleared, using global: *${fallback}*` });
116
+ await adapter.postMessage(dest, { text: `:white_check_mark: Channel profile cleared, using global: *${fallback}*` });
113
117
  await handleNewCmd(channel, adapter, { skipHook: true });
114
118
  return;
115
119
  }
116
120
  if (args[0] === 'global') {
117
121
  if (args.length < 2) {
118
- await adapter.postMessage(channel, { text: ':x: Usage: `!profile global <name>`' });
122
+ await adapter.postMessage(dest, { text: ':x: Usage: `!profile global <name>`' });
119
123
  return;
120
124
  }
121
125
  const profileName = args[1];
122
126
  try {
123
127
  resolveProfile(profileName);
124
128
  setActiveProfile(profileName);
125
- await adapter.postMessage(channel, { text: `:white_check_mark: Global profile set to *${profileName}*\n${formatProfileList(channel)}` });
129
+ await adapter.postMessage(dest, { text: `:white_check_mark: Global profile set to *${profileName}*\n${formatProfileList(channel)}` });
126
130
  await handleNewCmd(channel, adapter, { skipHook: true });
127
131
  }
128
132
  catch (error) {
129
- await adapter.postMessage(channel, { text: `:x: ${error.message}` });
133
+ await adapter.postMessage(dest, { text: `:x: ${error.message}` });
130
134
  }
131
135
  return;
132
136
  }
@@ -134,17 +138,17 @@ export function createProfileHandler(router) {
134
138
  try {
135
139
  resolveProfile(profileName);
136
140
  setActiveProfile(profileName, channel);
137
- await adapter.postMessage(channel, { text: `:white_check_mark: Channel profile set to *${profileName}*\n${formatProfileList(channel)}` });
141
+ await adapter.postMessage(dest, { text: `:white_check_mark: Channel profile set to *${profileName}*\n${formatProfileList(channel)}` });
138
142
  await handleNewCmd(channel, adapter, { skipHook: true });
139
143
  }
140
144
  catch (error) {
141
- await adapter.postMessage(channel, { text: `:x: ${error.message}` });
145
+ await adapter.postMessage(dest, { text: `:x: ${error.message}` });
142
146
  }
143
147
  return;
144
148
  }
145
149
  const text = buildProfileText(channel);
146
150
  if (!router) {
147
- await adapter.postMessage(channel, { text });
151
+ await adapter.postMessage(dest, { text });
148
152
  return;
149
153
  }
150
154
  return {
@@ -161,8 +165,9 @@ export async function handleProfileCmd(channel, adapter, trimmedMessage) {
161
165
  }
162
166
  export async function handleSkillsCmd(channel, adapter) {
163
167
  const groups = getDisplaySkillGroups();
168
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
164
169
  if (groups.length === 0) {
165
- await adapter.postMessage(channel, { text: 'No skills found.' });
170
+ await adapter.postMessage(dest, { text: 'No skills found.' });
166
171
  return;
167
172
  }
168
173
  const lines = ['*Available skills*'];
@@ -172,7 +177,7 @@ export async function handleSkillsCmd(channel, adapter) {
172
177
  lines.push(`• \`${skill}\``);
173
178
  }
174
179
  }
175
- await adapter.postMessage(channel, { text: lines.join('\n') });
180
+ await adapter.postMessage(dest, { text: lines.join('\n') });
176
181
  }
177
182
  const MAX_AGENT_BUTTONS = 10;
178
183
  function buildAgentText() {
@@ -253,27 +258,28 @@ export function createAgentHandler(router) {
253
258
  }
254
259
  return async function handleAgentCmdInteractive(channel, adapter, trimmedMessage) {
255
260
  const args = trimmedMessage.replace(/^!agent\s*/, '').trim();
261
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
256
262
  if (args) {
257
263
  const name = args.split(/\s+/)[0];
258
264
  if (name === 'off' || name === 'none' || name === 'disable') {
259
265
  setDefaultAgent(null);
260
- await adapter.postMessage(channel, { text: ':white_check_mark: Default agent disabled.' });
266
+ await adapter.postMessage(dest, { text: ':white_check_mark: Default agent disabled.' });
261
267
  return;
262
268
  }
263
269
  const agentDef = getAgent(name);
264
270
  if (!agentDef) {
265
271
  const available = listAgents().map(a => `\`${a.name}\``).join(', ');
266
- await adapter.postMessage(channel, { text: `:x: Unknown agent: \`${name}\`\nAvailable: ${available}` });
272
+ await adapter.postMessage(dest, { text: `:x: Unknown agent: \`${name}\`\nAvailable: ${available}` });
267
273
  return;
268
274
  }
269
275
  setDefaultAgent(name);
270
276
  const claudeAgentStr = agentDef.claudeAgent ? ` · agent:${agentDef.claudeAgent}` : '';
271
- await adapter.postMessage(channel, { text: `:white_check_mark: Default agent set to *${name}* (${agentDef.profile}${claudeAgentStr})` });
277
+ await adapter.postMessage(dest, { text: `:white_check_mark: Default agent set to *${name}* (${agentDef.profile}${claudeAgentStr})` });
272
278
  return;
273
279
  }
274
280
  const text = buildAgentText();
275
281
  if (!router) {
276
- await adapter.postMessage(channel, { text });
282
+ await adapter.postMessage(dest, { text });
277
283
  return;
278
284
  }
279
285
  return {
@@ -25,55 +25,57 @@ async function updateNvtopMessage(channel, adapter) {
25
25
  export async function handleNvidiaSmiCmd(channel, adapter, trimmedMessage) {
26
26
  const args = trimmedMessage.split(/\s+/).slice(1);
27
27
  const machine = args[0] || getLocalMachine();
28
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
28
29
  const reg = getMachineRegistry()[machine];
29
30
  if (!reg) {
30
31
  const valid = Object.keys(getMachineRegistry()).filter(m => getMachineRegistry()[m].gpuCount > 0).map(m => `\`${m}\``).join(', ');
31
- await adapter.postMessage(channel, { text: `:x: Unknown machine: \`${machine}\`\nGPU machines: ${valid}` });
32
+ await adapter.postMessage(dest, { text: `:x: Unknown machine: \`${machine}\`\nGPU machines: ${valid}` });
32
33
  return;
33
34
  }
34
35
  if (reg.gpuCount === 0) {
35
- await adapter.postMessage(channel, { text: `:x: \`${machine}\` has no GPU.` });
36
+ await adapter.postMessage(dest, { text: `:x: \`${machine}\` has no GPU.` });
36
37
  return;
37
38
  }
38
39
  if (!isDeviceOnline(machine)) {
39
- await adapter.postMessage(channel, { text: `:x: Device \`${machine}\` is not online.` });
40
+ await adapter.postMessage(dest, { text: `:x: Device \`${machine}\` is not online.` });
40
41
  return;
41
42
  }
42
43
  try {
43
44
  const result = await sendCommand(machine, { action: 'bash', params: { command: 'nvidia-smi' }, timeout: 15000 });
44
45
  const output = result.stdout || result.stderr || '(no output)';
45
- await adapter.postMessage(channel, { text: `*nvidia-smi* on \`${machine}\`\n\`\`\`\n${output}\n\`\`\`` });
46
+ await adapter.postMessage(dest, { text: `*nvidia-smi* on \`${machine}\`\n\`\`\`\n${output}\n\`\`\`` });
46
47
  }
47
48
  catch (err) {
48
- await adapter.postMessage(channel, { text: `:x: nvidia-smi failed on \`${machine}\`: ${err.message}` });
49
+ await adapter.postMessage(dest, { text: `:x: nvidia-smi failed on \`${machine}\`: ${err.message}` });
49
50
  }
50
51
  }
51
52
  export async function handleNvtopCmd(channel, adapter, trimmedMessage) {
52
53
  const args = trimmedMessage.split(/\s+/).slice(1);
54
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
53
55
  if (args[0] === 'stop') {
54
56
  const active = activeNvtopMonitors.get(channel);
55
57
  if (!active) {
56
- await adapter.postMessage(channel, { text: 'No active nvtop monitor in this channel.' });
58
+ await adapter.postMessage(dest, { text: 'No active nvtop monitor in this channel.' });
57
59
  return;
58
60
  }
59
61
  clearInterval(active.interval);
60
62
  activeNvtopMonitors.delete(channel);
61
- await adapter.postMessage(channel, { text: `:octagonal_sign: nvtop stopped on \`${active.machine}\`.` });
63
+ await adapter.postMessage(dest, { text: `:octagonal_sign: nvtop stopped on \`${active.machine}\`.` });
62
64
  return;
63
65
  }
64
66
  if (activeNvtopMonitors.has(channel)) {
65
- await adapter.postMessage(channel, { text: 'nvtop already running. Use `!nvtop stop` first.' });
67
+ await adapter.postMessage(dest, { text: 'nvtop already running. Use `!nvtop stop` first.' });
66
68
  return;
67
69
  }
68
70
  const machine = args[0] || getLocalMachine();
69
71
  const reg = getMachineRegistry()[machine];
70
72
  if (!reg) {
71
73
  const valid = Object.keys(getMachineRegistry()).filter(m => getMachineRegistry()[m].gpuCount > 0).map(m => `\`${m}\``).join(', ');
72
- await adapter.postMessage(channel, { text: `:x: Unknown machine: \`${machine}\`\nGPU machines: ${valid}` });
74
+ await adapter.postMessage(dest, { text: `:x: Unknown machine: \`${machine}\`\nGPU machines: ${valid}` });
73
75
  return;
74
76
  }
75
77
  if (reg.gpuCount === 0) {
76
- await adapter.postMessage(channel, { text: `:x: nvtop monitoring is not supported on \`${machine}\` yet.` });
78
+ await adapter.postMessage(dest, { text: `:x: nvtop monitoring is not supported on \`${machine}\` yet.` });
77
79
  return;
78
80
  }
79
81
  try {
@@ -81,7 +83,7 @@ export async function handleNvtopCmd(channel, adapter, trimmedMessage) {
81
83
  const snapshot = await queryGpuSnapshot(machine);
82
84
  rememberGpuHistory(historyByGpu, snapshot);
83
85
  const text = renderGpuSnapshot(snapshot, historyByGpu, NVTOP_REFRESH_MS / 1000);
84
- const result = await adapter.postMessage(channel, { text });
86
+ const result = await adapter.postMessage(dest, { text });
85
87
  const interval = setInterval(async () => {
86
88
  try {
87
89
  await updateNvtopMessage(channel, adapter);
@@ -98,6 +100,6 @@ export async function handleNvtopCmd(channel, adapter, trimmedMessage) {
98
100
  });
99
101
  }
100
102
  catch (err) {
101
- await adapter.postMessage(channel, { text: `:x: nvtop failed on \`${machine}\`: ${err.message}` });
103
+ await adapter.postMessage(dest, { text: `:x: nvtop failed on \`${machine}\`: ${err.message}` });
102
104
  }
103
105
  }
@@ -1,3 +1,4 @@
1
1
  export async function handleOrientCmd(channel, adapter) {
2
- await adapter.postMessage(channel, { text: '`!orient` command not yet implemented.' });
2
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
3
+ await adapter.postMessage(dest, { text: '`!orient` command not yet implemented.' });
3
4
  }
@@ -111,12 +111,13 @@ export function createScheduleHandler(scheduler, router) {
111
111
  });
112
112
  }
113
113
  return async function handleScheduleCmd(channel, adapter, trimmedMessage) {
114
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
114
115
  const parts = trimmedMessage.split(/\s+/);
115
116
  const sub = parts[1];
116
117
  if (router && scheduler && (!sub || sub === 'list')) {
117
118
  const tasks = await scheduler.list();
118
119
  if (tasks.length === 0) {
119
- await adapter.postMessage(channel, { text: 'No scheduled tasks.' });
120
+ await adapter.postMessage(dest, { text: 'No scheduled tasks.' });
120
121
  return;
121
122
  }
122
123
  const now = Date.now();
@@ -5,6 +5,7 @@ import * as os from 'os';
5
5
  import * as path from 'path';
6
6
  import { WORKSPACE_DIR } from '../../../core/utils.js';
7
7
  async function sendLocalFile(channel, adapter, reg, filePath, machine) {
8
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
8
9
  let resolved;
9
10
  if (filePath.startsWith('~/') || filePath === '~') {
10
11
  resolved = filePath.replace(/^~/, process.env.HOME || os.homedir());
@@ -16,18 +17,19 @@ async function sendLocalFile(channel, adapter, reg, filePath, machine) {
16
17
  resolved = path.resolve(reg.cortexPath, filePath);
17
18
  }
18
19
  if (!existsSync(resolved)) {
19
- await adapter.postMessage(channel, { text: `:x: File not found: \`${resolved}\`` });
20
+ await adapter.postMessage(dest, { text: `:x: File not found: \`${resolved}\`` });
20
21
  return;
21
22
  }
22
23
  const stat = statSync(resolved);
23
24
  if (!stat.isFile()) {
24
- await adapter.postMessage(channel, { text: `:x: Not a file: \`${resolved}\`` });
25
+ await adapter.postMessage(dest, { text: `:x: Not a file: \`${resolved}\`` });
25
26
  return;
26
27
  }
27
- await adapter.uploadFile(channel, resolved, { filename: path.basename(resolved) });
28
- await adapter.postMessage(channel, { text: `:white_check_mark: Sent \`${path.basename(resolved)}\` (${(stat.size / 1024).toFixed(1)} KB) from ${machine}` });
28
+ await adapter.uploadFile(dest, resolved, { filename: path.basename(resolved) });
29
+ await adapter.postMessage(dest, { text: `:white_check_mark: Sent \`${path.basename(resolved)}\` (${(stat.size / 1024).toFixed(1)} KB) from ${machine}` });
29
30
  }
30
31
  async function sendRemoteFile(channel, adapter, reg, filePath, machine) {
32
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
31
33
  let remotePath;
32
34
  if (filePath.startsWith('~/') || filePath === '~') {
33
35
  remotePath = filePath;
@@ -58,13 +60,13 @@ async function sendRemoteFile(channel, adapter, reg, filePath, machine) {
58
60
  });
59
61
  }
60
62
  catch (err) {
61
- await adapter.postMessage(channel, { text: `:x: Failed to fetch from ${machine}: ${err.message}` });
63
+ await adapter.postMessage(dest, { text: `:x: Failed to fetch from ${machine}: ${err.message}` });
62
64
  return;
63
65
  }
64
66
  try {
65
67
  const tmpStat = statSync(localTmp);
66
- await adapter.uploadFile(channel, localTmp, { filename: fileName });
67
- await adapter.postMessage(channel, { text: `:white_check_mark: Sent \`${fileName}\` (${(tmpStat.size / 1024).toFixed(1)} KB) from ${machine}` });
68
+ await adapter.uploadFile(dest, localTmp, { filename: fileName });
69
+ await adapter.postMessage(dest, { text: `:white_check_mark: Sent \`${fileName}\` (${(tmpStat.size / 1024).toFixed(1)} KB) from ${machine}` });
68
70
  }
69
71
  finally {
70
72
  try {
@@ -74,16 +76,17 @@ async function sendRemoteFile(channel, adapter, reg, filePath, machine) {
74
76
  }
75
77
  }
76
78
  export async function handleSendFileCmd(channel, adapter, trimmedMessage) {
79
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
77
80
  const args = trimmedMessage.split(/\s+/).slice(1);
78
81
  if (args.length < 2) {
79
- await adapter.postMessage(channel, { text: 'Usage: `!sendFile <machine> <path>`\nPath can be absolute, relative to Cortex root, or start with `~`' });
82
+ await adapter.postMessage(dest, { text: 'Usage: `!sendFile <machine> <path>`\nPath can be absolute, relative to Cortex root, or start with `~`' });
80
83
  return;
81
84
  }
82
85
  const machine = args[0];
83
86
  const filePath = args.slice(1).join(' ');
84
87
  const reg = getMachineRegistry()[machine];
85
88
  if (!reg) {
86
- await adapter.postMessage(channel, { text: `:x: Unknown machine: \`${machine}\`. Known: ${Object.keys(getMachineRegistry()).join(', ')}` });
89
+ await adapter.postMessage(dest, { text: `:x: Unknown machine: \`${machine}\`. Known: ${Object.keys(getMachineRegistry()).join(', ')}` });
87
90
  return;
88
91
  }
89
92
  if (!reg.ssh)
@@ -2,7 +2,7 @@ import { createLogger } from '../../../core/log.js';
2
2
  import { closeSession, getActiveBackend, getActiveProfile, setActiveProfile } from '../../../domain/agents/index.js';
3
3
  import { deleteSessionAsync, setSessionAsync } from '../../../domain/sessions/session.js';
4
4
  import { fireAndForgetPreCloseHook } from '../../../domain/sessions/session-hooks.js';
5
- import { sessionRegistryRepo } from '../../../store/session-registry-repo.js';
5
+ import { sessionStore } from '../../../store/session-registry-repo.js';
6
6
  import { conversationLedger } from '../../../store/conversation-ledger-repo.js';
7
7
  import * as sessionBackup from '../../../domain/sessions/session-backup.js';
8
8
  import { planApprovals } from '../../interactions/plan-approvals.js';
@@ -38,6 +38,7 @@ async function resolveSessionThreadTs(channel, threadTs) {
38
38
  return null;
39
39
  }
40
40
  export async function handleNewCmd(channel, adapter, opts = {}, threadTs) {
41
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
41
42
  if (!opts.skipHook) {
42
43
  const resolvedThreadTs = await resolveSessionThreadTs(channel, threadTs);
43
44
  void fireAndForgetPreCloseHook(channel, adapter, resolvedThreadTs);
@@ -59,7 +60,7 @@ export async function handleNewCmd(channel, adapter, opts = {}, threadTs) {
59
60
  if (cleared > 0)
60
61
  log.info('Cleared pending plan for channel:', channel);
61
62
  log.info('New conversation started in channel:', channel);
62
- await adapter.postMessage(channel, { text: `--- new conversation --- (profile: ${profileName})` });
63
+ await adapter.postMessage(dest, { text: `--- new conversation --- (profile: ${profileName})` });
63
64
  }
64
65
  const MAX_RESUME_BUTTONS = 10;
65
66
  export function createResumeHandler(router) {
@@ -69,7 +70,7 @@ export function createResumeHandler(router) {
69
70
  if (!adapter)
70
71
  return;
71
72
  const name = ctx.value;
72
- const record = await sessionRegistryRepo.lookupSession(name);
73
+ const record = await sessionStore.lookupSession(name);
73
74
  if (!record) {
74
75
  if (ctx.messageRef) {
75
76
  await adapter.updateMessage(ctx.messageRef, {
@@ -97,12 +98,13 @@ export function createResumeHandler(router) {
97
98
  });
98
99
  }
99
100
  return async function handleResumeCmdInteractive(channel, adapter, trimmedMessage) {
101
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
100
102
  const args = trimmedMessage.split(/\s+/).slice(1);
101
103
  if (args.length > 0) {
102
104
  const name = args[0];
103
- const record = await sessionRegistryRepo.lookupSession(name);
105
+ const record = await sessionStore.lookupSession(name);
104
106
  if (!record) {
105
- await adapter.postMessage(channel, { text: `:x: Session \`${name}\` not found. Run \`!resume\` to list sessions.` });
107
+ await adapter.postMessage(dest, { text: `:x: Session \`${name}\` not found. Run \`!resume\` to list sessions.` });
106
108
  return;
107
109
  }
108
110
  if (record.profileName)
@@ -110,15 +112,15 @@ export function createResumeHandler(router) {
110
112
  await setSessionAsync(channel, record.sessionId, record.backend);
111
113
  await conversationLedger.switchSession(channel, { sessionId: record.sessionId, sessionName: name, backend: record.backend, profileName: record.profileName });
112
114
  const profileNote = record.profileName ? ` (profile: ${record.profileName})` : '';
113
- await adapter.postMessage(channel, { text: `:arrows_counterclockwise: Switched to session \`${name}\`${profileNote}` });
115
+ await adapter.postMessage(dest, { text: `:arrows_counterclockwise: Switched to session \`${name}\`${profileNote}` });
114
116
  return;
115
117
  }
116
- const sessions = await sessionRegistryRepo.listRecentSessions(10);
118
+ const sessions = await sessionStore.listRecentSessions(10);
117
119
  if (sessions.length === 0) {
118
- await adapter.postMessage(channel, { text: 'No sessions recorded yet.' });
120
+ await adapter.postMessage(dest, { text: 'No sessions recorded yet.' });
119
121
  return;
120
122
  }
121
- const activeId = await sessionRegistryRepo.getActiveSessionName(channel, getActiveBackend());
123
+ const activeId = await sessionStore.getActiveSessionName(channel, getActiveBackend());
122
124
  const now = Date.now();
123
125
  const lines = ['*Recent sessions*'];
124
126
  for (const s of sessions) {
@@ -131,7 +133,7 @@ export function createResumeHandler(router) {
131
133
  }
132
134
  const text = lines.join('\n');
133
135
  if (!router) {
134
- await adapter.postMessage(channel, { text });
136
+ await adapter.postMessage(dest, { text });
135
137
  return;
136
138
  }
137
139
  return {
@@ -31,7 +31,7 @@ export function createStatusHandler(getExecutionStatusReport, router) {
31
31
  return async function handleStatusCmd(channel, adapter) {
32
32
  const text = getExecutionStatusReport ? getExecutionStatusReport() : 'Execution status reporting is not available in this process.';
33
33
  if (!router) {
34
- await adapter.postMessage(channel, { text });
34
+ await adapter.postMessage({ type: 'interactive-reply', conduit: channel, sessionId: '' }, { text });
35
35
  return;
36
36
  }
37
37
  return {
@@ -163,7 +163,7 @@ export function createHelpHandler(router) {
163
163
  return async function handleHelpCmd(channel, adapter) {
164
164
  const text = buildHelpText();
165
165
  if (!router) {
166
- await adapter.postMessage(channel, { text });
166
+ await adapter.postMessage({ type: 'interactive-reply', conduit: channel, sessionId: '' }, { text });
167
167
  return;
168
168
  }
169
169
  return {
@@ -4,16 +4,18 @@ import { moduleDir } from '../../../core/utils.js';
4
4
  const DAEMON_LOG_PATH = path.join(moduleDir(import.meta.url), '..', '..', '..', '..', 'logs', 'daemon.log');
5
5
  const activeTails = new Map();
6
6
  async function stopTail(channel, adapter) {
7
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
7
8
  const active = activeTails.get(channel);
8
9
  if (!active) {
9
- await adapter.postMessage(channel, { text: 'No active tail in this channel.' });
10
+ await adapter.postMessage(dest, { text: 'No active tail in this channel.' });
10
11
  return;
11
12
  }
12
13
  clearInterval(active.interval);
13
14
  activeTails.delete(channel);
14
- await adapter.postMessage(channel, { text: ':octagonal_sign: Tail stopped.' });
15
+ await adapter.postMessage(dest, { text: ':octagonal_sign: Tail stopped.' });
15
16
  }
16
17
  async function sendInitialTailPreview(channel, adapter) {
18
+ const previewDest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
17
19
  try {
18
20
  const stat = statSync(DAEMON_LOG_PATH);
19
21
  const previewBytes = Math.min(4096, stat.size);
@@ -22,15 +24,16 @@ async function sendInitialTailPreview(channel, adapter) {
22
24
  readSync(fd, buf, 0, previewBytes, stat.size - previewBytes);
23
25
  closeSync(fd);
24
26
  const lines = buf.toString('utf-8').split('\n').filter(Boolean).slice(-30);
25
- await adapter.postMessage(channel, { text: `:scroll: *Tailing daemon.log* (last ${lines.length} lines, refresh every 3s)\n\`\`\`\n${lines.join('\n')}\n\`\`\`\nUse \`!tail stop\` to stop.` });
27
+ await adapter.postMessage(previewDest, { text: `:scroll: *Tailing daemon.log* (last ${lines.length} lines, refresh every 3s)\n\`\`\`\n${lines.join('\n')}\n\`\`\`\nUse \`!tail stop\` to stop.` });
26
28
  return stat.size;
27
29
  }
28
30
  catch (err) {
29
- await adapter.postMessage(channel, { text: `:x: Cannot read daemon.log: ${err.message}` });
31
+ await adapter.postMessage(previewDest, { text: `:x: Cannot read daemon.log: ${err.message}` });
30
32
  return null;
31
33
  }
32
34
  }
33
35
  function startTailInterval(channel, adapter, offset) {
36
+ const tailIntervalDest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
34
37
  const interval = setInterval(async () => {
35
38
  try {
36
39
  const stat = statSync(DAEMON_LOG_PATH);
@@ -50,7 +53,7 @@ function startTailInterval(channel, adapter, offset) {
50
53
  active.offset += bytesToRead;
51
54
  const newContent = buf.toString('utf-8').trim();
52
55
  if (newContent)
53
- await adapter.postMessage(channel, { text: `\`\`\`\n${newContent}\n\`\`\`` });
56
+ await adapter.postMessage(tailIntervalDest, { text: `\`\`\`\n${newContent}\n\`\`\`` });
54
57
  }
55
58
  catch {
56
59
  // File gone or unreadable — silently skip
@@ -62,8 +65,9 @@ export async function handleTailCmd(channel, adapter, trimmedMessage) {
62
65
  const args = trimmedMessage.split(/\s+/).slice(1);
63
66
  if (args[0] === 'stop')
64
67
  return stopTail(channel, adapter);
68
+ const cmdDest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
65
69
  if (activeTails.has(channel)) {
66
- await adapter.postMessage(channel, { text: 'Already tailing. Use `!tail stop` to stop first.' });
70
+ await adapter.postMessage(cmdDest, { text: 'Already tailing. Use `!tail stop` to stop first.' });
67
71
  return;
68
72
  }
69
73
  const offset = await sendInitialTailPreview(channel, adapter);
@@ -107,25 +107,26 @@ export function createTasksHandler(router) {
107
107
  });
108
108
  }
109
109
  return async function handleTasksCmdInteractive(channel, adapter, trimmedMessage) {
110
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
110
111
  const args = trimmedMessage.split(/\s+/).slice(1);
111
112
  if (args.length === 0) {
112
- await adapter.postMessage(channel, { text: ':x: Usage: `!tasks <project>`' });
113
+ await adapter.postMessage(dest, { text: ':x: Usage: `!tasks <project>`' });
113
114
  return;
114
115
  }
115
116
  const project = args[0];
116
117
  const projectDir = path.join(PROJECTS_DIR, project);
117
118
  if (!existsSync(projectDir)) {
118
- await adapter.postMessage(channel, { text: `:x: Project not found: \`${project}\`` });
119
+ await adapter.postMessage(dest, { text: `:x: Project not found: \`${project}\`` });
119
120
  return;
120
121
  }
121
122
  const tasks = scanAllTasks(project);
122
123
  if (tasks.length === 0) {
123
- await adapter.postMessage(channel, { text: `No tasks found for \`${project}\`.` });
124
+ await adapter.postMessage(dest, { text: `No tasks found for \`${project}\`.` });
124
125
  return;
125
126
  }
126
127
  const text = formatTaskList(tasks, 'all', project);
127
128
  if (!router) {
128
- await adapter.postMessage(channel, { text });
129
+ await adapter.postMessage(dest, { text });
129
130
  return;
130
131
  }
131
132
  return {
@@ -9,9 +9,10 @@ import { runningExecutions } from '../../../core/running-executions.js';
9
9
  import { channelQueues } from '../../channel-queue.js';
10
10
  const log = createLogger('thread-handlers');
11
11
  async function handleThreadStatus(channel, adapter) {
12
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
12
13
  const active = threadStore.findActive(channel);
13
14
  if (!active) {
14
- await adapter.postMessage(channel, { text: 'No active thread in this channel.' });
15
+ await adapter.postMessage(dest, { text: 'No active thread in this channel.' });
15
16
  return;
16
17
  }
17
18
  const agents = Object.keys(active.agents).join(', ');
@@ -25,12 +26,13 @@ async function handleThreadStatus(channel, adapter) {
25
26
  if (active.status === 'aborted' && active.abortReason) {
26
27
  lines.push(`Abort reason: ${active.abortReason}`);
27
28
  }
28
- await adapter.postMessage(channel, { text: lines.join('\n') });
29
+ await adapter.postMessage(dest, { text: lines.join('\n') });
29
30
  }
30
31
  async function handleThreadTemplates(channel, adapter) {
32
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
31
33
  const templates = listTemplates();
32
34
  if (templates.length === 0) {
33
- await adapter.postMessage(channel, { text: 'No thread templates configured.' });
35
+ await adapter.postMessage(dest, { text: 'No thread templates configured.' });
34
36
  return;
35
37
  }
36
38
  const lines = ['*Available Thread Templates*'];
@@ -38,9 +40,10 @@ async function handleThreadTemplates(channel, adapter) {
38
40
  const agentList = t.agents.map(a => typeof a === 'string' ? a : a.ref).join(', ');
39
41
  lines.push(`• \`${t.name}\` — ${t.description} [agents: ${agentList}]`);
40
42
  }
41
- await adapter.postMessage(channel, { text: lines.join('\n') });
43
+ await adapter.postMessage(dest, { text: lines.join('\n') });
42
44
  }
43
45
  async function handleThreadAgents(channel, adapter) {
46
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
44
47
  const agentDefs = listAgents();
45
48
  const lines = [];
46
49
  if (agentDefs.length > 0) {
@@ -62,23 +65,25 @@ async function handleThreadAgents(channel, adapter) {
62
65
  lines.push(` • *${slotId}* (${slot.profile}) — ${slot.status} | ${sessionStr}${isActiveAgent}`);
63
66
  }
64
67
  }
65
- await adapter.postMessage(channel, { text: lines.join('\n') });
68
+ await adapter.postMessage(dest, { text: lines.join('\n') });
66
69
  }
67
70
  async function handleThreadCancelAlias(channel, adapter) {
71
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
68
72
  // Alias for !cancel — delegate to the same killByKey behavior
69
73
  if (runningExecutions.killByKey(channel)) {
70
74
  log.info('Cancel requested for channel:', channel);
71
75
  channelQueues.delete(channel);
72
- await adapter.postMessage(channel, { text: ':octagonal_sign: Cancelled. Session preserved — next message will resume.' });
76
+ await adapter.postMessage(dest, { text: ':octagonal_sign: Cancelled. Session preserved — next message will resume.' });
73
77
  }
74
78
  else {
75
- await adapter.postMessage(channel, { text: 'Nothing running to cancel.' });
79
+ await adapter.postMessage(dest, { text: 'Nothing running to cancel.' });
76
80
  }
77
81
  }
78
82
  async function handleThreadList(channel, adapter) {
83
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
79
84
  const threads = threadStore.findByChannel(channel).slice(0, 10);
80
85
  if (threads.length === 0) {
81
- await adapter.postMessage(channel, { text: 'No threads found for this channel.' });
86
+ await adapter.postMessage(dest, { text: 'No threads found for this channel.' });
82
87
  return;
83
88
  }
84
89
  const lines = ['*Recent Threads*'];
@@ -86,12 +91,13 @@ async function handleThreadList(channel, adapter) {
86
91
  const modeLabel = t.templateName || 'ad-hoc';
87
92
  lines.push(`• \`${t.id}\` ${modeLabel} — ${t.status} | ${t.steps.length} steps | $${t.totalCostUsd.toFixed(4)}`);
88
93
  }
89
- await adapter.postMessage(channel, { text: lines.join('\n') });
94
+ await adapter.postMessage(dest, { text: lines.join('\n') });
90
95
  }
91
96
  async function handleThreadListRunning(channel, adapter) {
97
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
92
98
  const executions = runningExecutions.getAll().filter(e => e.threadId);
93
99
  if (executions.length === 0) {
94
- await adapter.postMessage(channel, { text: 'No running threads.' });
100
+ await adapter.postMessage(dest, { text: 'No running threads.' });
95
101
  return;
96
102
  }
97
103
  const lines = ['*Running Threads*'];
@@ -102,10 +108,11 @@ async function handleThreadListRunning(channel, adapter) {
102
108
  const cost = thread?.totalCostUsd ?? 0;
103
109
  lines.push(`• \`${exec.threadId}\` — ${exec.channel || '?'} | ${modeLabel} | ${stepCount} step(s) | $${cost.toFixed(4)}`);
104
110
  }
105
- await adapter.postMessage(channel, { text: lines.join('\n') });
111
+ await adapter.postMessage(dest, { text: lines.join('\n') });
106
112
  }
107
113
  async function sendThreadUsage(channel, adapter) {
108
- await adapter.postMessage(channel, {
114
+ const dest = { type: 'interactive-reply', conduit: channel, sessionId: '' };
115
+ await adapter.postMessage(dest, {
109
116
  text: [
110
117
  '*Thread Commands*',
111
118
  '`!thread` — show active thread status',