@cortex-agent/server 2026.5.19-3 → 2026.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/defaults/config/thread-templates.json +65 -39
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +4 -4
- package/defaults/prompts/directives/coder-reviewer.md +9 -4
- package/defaults/prompts/directives/coder.md +10 -2
- package/dist/core/log.js +61 -12
- package/dist/core/status-format.js +1 -1
- package/dist/core/task-parser.d.ts +1 -1
- package/dist/core/task-parser.js +13 -6
- package/dist/core/types/thread-types.d.ts +4 -2
- package/dist/domain/costs/codex-usage-monitor.d.ts +1 -2
- package/dist/domain/costs/codex-usage-monitor.js +2 -2
- package/dist/domain/costs/cost-tracker.d.ts +2 -0
- package/dist/domain/costs/cost-tracker.js +4 -20
- package/dist/domain/costs/rate-limit-throttle.js +1 -4
- package/dist/domain/mcp/tools/context.d.ts +4 -2
- package/dist/domain/mcp/tools/context.js +7 -5
- package/dist/domain/mcp/tools/schedule.d.ts +1 -1
- package/dist/domain/mcp/tools/schedule.js +24 -20
- package/dist/domain/memory/index-regen.js +8 -5
- package/dist/domain/monitor/disk-monitor.js +1 -6
- package/dist/domain/projects/index.d.ts +2 -0
- package/dist/domain/projects/index.js +3 -0
- package/dist/domain/projects/project-store.d.ts +54 -0
- package/dist/domain/projects/project-store.js +194 -0
- package/dist/domain/projects/project-types.d.ts +11 -0
- package/dist/domain/projects/project-types.js +5 -0
- package/dist/domain/remote/client-hot-reload.d.ts +1 -0
- package/dist/domain/remote/client-hot-reload.js +284 -53
- package/dist/domain/scheduling/jobs/_shared.d.ts +1 -1
- package/dist/domain/scheduling/jobs/_shared.js +6 -5
- package/dist/domain/scheduling/jobs/memory-index-regen.js +2 -2
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +2 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +38 -36
- package/dist/domain/scheduling/jobs/sync-public.d.ts +1 -0
- package/dist/domain/scheduling/jobs/sync-public.js +34 -0
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +3 -7
- package/dist/domain/scheduling/jobs/target-dispatch.js +7 -9
- package/dist/domain/scheduling/jobs/task-archive.js +2 -2
- package/dist/domain/scheduling/jobs/task-dispatch.js +18 -16
- package/dist/domain/scheduling/runner.d.ts +1 -0
- package/dist/domain/scheduling/runner.js +2 -0
- package/dist/domain/scheduling/schedule-cli.d.ts +1 -0
- package/dist/domain/scheduling/schedule-cli.js +14 -14
- package/dist/domain/scheduling/schedule-command.js +45 -36
- package/dist/domain/scheduling/scheduler.d.ts +1 -1
- package/dist/domain/scheduling/scheduler.js +12 -11
- package/dist/domain/sessions/session-hooks.d.ts +10 -11
- package/dist/domain/sessions/session-hooks.js +26 -27
- package/dist/domain/sessions/session-registry.d.ts +10 -5
- package/dist/domain/sessions/session-registry.js +24 -9
- package/dist/domain/tasks/pending-tracker.js +8 -9
- package/dist/domain/tasks/system/task-cli.d.ts +2 -0
- package/dist/domain/tasks/system/task-cli.js +189 -29
- package/dist/domain/tasks/system/task-lifecycle-edit.js +3 -3
- package/dist/domain/threads/hook-runner.js +12 -7
- package/dist/domain/threads/runner.d.ts +3 -3
- package/dist/domain/threads/runner.js +30 -28
- package/dist/domain/threads/state-machine.d.ts +3 -0
- package/dist/domain/threads/state-machine.js +4 -0
- package/dist/entry/app.js +30 -9
- package/dist/entry/cli.js +19 -4
- package/dist/entry/daemon.js +28 -2
- package/dist/entry/startup-notify.js +8 -6
- package/dist/orchestration/agent-runner.js +19 -13
- package/dist/orchestration/durable-helpers.js +3 -2
- package/dist/orchestration/interactions/ask-user-question.d.ts +2 -3
- package/dist/orchestration/interactions/ask-user-question.js +5 -4
- package/dist/orchestration/interactions/interaction-handlers.js +8 -5
- package/dist/orchestration/interactions/plan-handler.js +3 -3
- package/dist/orchestration/lifecycle.js +43 -38
- package/dist/orchestration/routing/commands/cancel.js +9 -8
- package/dist/orchestration/routing/commands/channel.js +32 -26
- package/dist/orchestration/routing/commands/cost.js +5 -3
- package/dist/orchestration/routing/commands/device.js +2 -1
- package/dist/orchestration/routing/commands/dispatch.js +8 -7
- package/dist/orchestration/routing/commands/index.js +7 -4
- package/dist/orchestration/routing/commands/mode.js +24 -18
- package/dist/orchestration/routing/commands/nvtop.js +14 -12
- package/dist/orchestration/routing/commands/orient.js +2 -1
- package/dist/orchestration/routing/commands/schedule.js +2 -1
- package/dist/orchestration/routing/commands/sendfile.js +12 -9
- package/dist/orchestration/routing/commands/session.js +12 -10
- package/dist/orchestration/routing/commands/status.js +2 -2
- package/dist/orchestration/routing/commands/tail.js +10 -6
- package/dist/orchestration/routing/commands/task.js +5 -4
- package/dist/orchestration/routing/commands/thread-handlers.js +19 -12
- package/dist/orchestration/routing/hook-bridge-subscribers.js +15 -13
- package/dist/orchestration/status-helpers.d.ts +4 -5
- package/dist/orchestration/status-helpers.js +7 -8
- package/dist/orchestration/thread-executor.js +21 -12
- package/dist/platform/adapter.d.ts +13 -9
- package/dist/platform/adapter.js +1 -1
- package/dist/platform/adapters/feishu-output-stream.d.ts +27 -0
- package/dist/platform/adapters/feishu-output-stream.js +114 -0
- package/dist/platform/adapters/feishu.d.ts +16 -7
- package/dist/platform/adapters/feishu.js +64 -18
- package/dist/platform/adapters/index.d.ts +4 -1
- package/dist/platform/adapters/index.js +1 -1
- package/dist/platform/adapters/slack-output-stream.d.ts +55 -0
- package/dist/platform/{virtual-message.js → adapters/slack-output-stream.js} +98 -165
- package/dist/platform/adapters/slack-project-conduits.d.ts +9 -0
- package/dist/platform/adapters/slack-project-conduits.js +40 -0
- package/dist/platform/adapters/slack.d.ts +18 -8
- package/dist/platform/adapters/slack.js +82 -24
- package/dist/platform/index.d.ts +3 -2
- package/dist/platform/index.js +1 -1
- package/dist/platform/output-stream-chunk.d.ts +6 -0
- package/dist/platform/output-stream-chunk.js +45 -0
- package/dist/platform/output-stream-helpers.d.ts +6 -0
- package/dist/platform/output-stream-helpers.js +11 -0
- package/dist/platform/output-stream.d.ts +28 -0
- package/dist/platform/output-stream.js +4 -0
- package/dist/platform/testing.d.ts +41 -8
- package/dist/platform/testing.js +93 -11
- package/dist/platform/tool-trace.d.ts +9 -8
- package/dist/platform/tool-trace.js +19 -17
- package/dist/platform/types.d.ts +21 -1
- package/dist/platform/types.js +17 -1
- package/dist/store/channel-repo.d.ts +6 -0
- package/dist/store/channel-repo.js +9 -0
- package/dist/store/json-repository.js +12 -1
- package/dist/store/outbound-queue.d.ts +5 -4
- package/dist/store/outbound-queue.js +9 -6
- package/dist/store/schedule-repo.d.ts +9 -5
- package/dist/store/schedule-repo.js +59 -14
- package/dist/store/session-registry-repo.d.ts +36 -7
- package/dist/store/session-registry-repo.js +186 -25
- package/dist/store/thread-repo.d.ts +1 -0
- package/dist/store/thread-repo.js +8 -0
- package/package.json +1 -1
- package/dist/platform/virtual-message.d.ts +0 -68
|
@@ -73,7 +73,7 @@ function taskConfigHash(task) {
|
|
|
73
73
|
const keys = { interval: 'intervalMs', daily: 'time', once: 'runAt', weekly: 'dayOfWeek' };
|
|
74
74
|
const key = keys[task.type];
|
|
75
75
|
const extra = task.type === 'weekly' ? `${task.dayOfWeek}:${task.time}` : (String(task[key] || ''));
|
|
76
|
-
return `${task.type}:${extra}:${task.message}:${task.
|
|
76
|
+
return `${task.type}:${extra}:${task.message}:${task.projectId}:${task.profile || ''}:${task.dispatchType || ''}:${task.preCheck || ''}`;
|
|
77
77
|
}
|
|
78
78
|
function validateTime(time) {
|
|
79
79
|
if (!/^\d{2}:\d{2}$/.test(time))
|
|
@@ -102,7 +102,7 @@ function computeTaskTiming(type, task) {
|
|
|
102
102
|
return {};
|
|
103
103
|
}
|
|
104
104
|
function validateTaskPatch(task, patch) {
|
|
105
|
-
const commonFields = new Set(['message', '
|
|
105
|
+
const commonFields = new Set(['message', 'projectId', 'profile', 'dispatchType', 'preCheck']);
|
|
106
106
|
const typeFields = {
|
|
107
107
|
interval: new Set(['intervalMs']),
|
|
108
108
|
daily: new Set(['time']),
|
|
@@ -129,20 +129,20 @@ function resolveTaskProfile(profile) {
|
|
|
129
129
|
return profile || getDefaultProfileName();
|
|
130
130
|
}
|
|
131
131
|
function buildTask(type, id, now, options) {
|
|
132
|
-
const { message,
|
|
132
|
+
const { message, projectId, profile = null, preCheck = null } = options;
|
|
133
133
|
const resolvedProfile = resolveTaskProfile(profile);
|
|
134
134
|
const base = { preCheck: preCheck || undefined };
|
|
135
135
|
if (type === 'interval') {
|
|
136
136
|
const { intervalMs } = options;
|
|
137
137
|
if (!intervalMs)
|
|
138
138
|
throw new Error('intervalMs required');
|
|
139
|
-
return { id, type: 'interval', intervalMs, message,
|
|
139
|
+
return { id, type: 'interval', intervalMs, message, projectId, profile: resolvedProfile, ...base, lastRun: null, nextRun: now + intervalMs, createdAt: now };
|
|
140
140
|
}
|
|
141
141
|
if (type === 'daily') {
|
|
142
142
|
const { time } = options;
|
|
143
143
|
if (!time)
|
|
144
144
|
throw new Error('time required (HH:MM)');
|
|
145
|
-
return { id, type: 'daily', time, message,
|
|
145
|
+
return { id, type: 'daily', time, message, projectId, profile: resolvedProfile, ...base, lastRun: null, nextRun: now + nextDailyMs(time), createdAt: now };
|
|
146
146
|
}
|
|
147
147
|
if (type === 'weekly') {
|
|
148
148
|
const { dayOfWeek, time } = options;
|
|
@@ -150,13 +150,13 @@ function buildTask(type, id, now, options) {
|
|
|
150
150
|
throw new Error('dayOfWeek required (0=Sun..6=Sat)');
|
|
151
151
|
if (!time)
|
|
152
152
|
throw new Error('time required (HH:MM)');
|
|
153
|
-
return { id, type: 'weekly', dayOfWeek, time, message,
|
|
153
|
+
return { id, type: 'weekly', dayOfWeek, time, message, projectId, profile: resolvedProfile, ...base, lastRun: null, nextRun: now + nextWeeklyMs(dayOfWeek, time), createdAt: now };
|
|
154
154
|
}
|
|
155
155
|
if (type === 'once') {
|
|
156
156
|
const { delay } = options;
|
|
157
157
|
if (!delay)
|
|
158
158
|
throw new Error('delay required');
|
|
159
|
-
return { id, type: 'once', runAt: now + delay, message,
|
|
159
|
+
return { id, type: 'once', runAt: now + delay, message, projectId, profile: resolvedProfile, ...base, createdAt: now };
|
|
160
160
|
}
|
|
161
161
|
throw new Error(`Unknown type: ${type}. Use: interval, daily, weekly, once`);
|
|
162
162
|
}
|
|
@@ -510,25 +510,26 @@ class Scheduler {
|
|
|
510
510
|
// Internal: invoke the runner for a task
|
|
511
511
|
async _runTask(task) {
|
|
512
512
|
const profileName = resolveTaskProfile(task.profile);
|
|
513
|
-
|
|
513
|
+
const resolvedChannel = task.projectId;
|
|
514
|
+
log.info(`Firing task ${task.id} (${task.type}, dispatch=${task.dispatchType || 'llm'}, profile=${profileName}, channel=${resolvedChannel}): "${task.message}"`);
|
|
514
515
|
try {
|
|
515
516
|
if (task.dispatchType === 'task-dispatch' && this.taskDispatchRunner) {
|
|
516
517
|
await this.taskDispatchRunner({
|
|
517
|
-
channel:
|
|
518
|
+
channel: resolvedChannel,
|
|
518
519
|
scheduleTaskId: task.id,
|
|
519
520
|
profileName,
|
|
520
521
|
});
|
|
521
522
|
}
|
|
522
523
|
else if (task.dispatchType && this.programmaticHandlers[task.dispatchType]) {
|
|
523
524
|
await this.programmaticHandlers[task.dispatchType]({
|
|
524
|
-
channel:
|
|
525
|
+
channel: resolvedChannel,
|
|
525
526
|
scheduleTaskId: task.id,
|
|
526
527
|
});
|
|
527
528
|
}
|
|
528
529
|
else {
|
|
529
530
|
await this.runner({
|
|
530
531
|
message: `[Scheduled Task] ${task.message}`,
|
|
531
|
-
|
|
532
|
+
projectId: task.projectId,
|
|
532
533
|
scheduleTaskId: task.id,
|
|
533
534
|
profileName,
|
|
534
535
|
target: task.target,
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { PlatformAdapter } from '../../platform/index.js';
|
|
2
|
-
import { VirtualMessage } from '../../platform/virtual-message.js';
|
|
1
|
+
import type { PlatformAdapter, OutputStream } from '../../platform/index.js';
|
|
3
2
|
export interface SessionHookConfig {
|
|
4
3
|
command: string;
|
|
5
4
|
args?: string[];
|
|
@@ -21,7 +20,7 @@ export interface SessionHookContext {
|
|
|
21
20
|
profile?: string | null;
|
|
22
21
|
}
|
|
23
22
|
/** Caller-supplied formatters — keep wording per-hook so existing UX text doesn't change.
|
|
24
|
-
* All formatters return short single-line strings; runSessionHook appends them via
|
|
23
|
+
* All formatters return short single-line strings; runSessionHook appends them via OutputStream,
|
|
25
24
|
* so they share the same Slack thread/group as surrounding agent output. */
|
|
26
25
|
export interface SessionHookFormat {
|
|
27
26
|
/** Status line shown immediately when the hook starts. */
|
|
@@ -34,7 +33,7 @@ export interface SessionHookFormat {
|
|
|
34
33
|
emptyLine?: () => string | null;
|
|
35
34
|
}
|
|
36
35
|
/** When set, the hook stdout is injected as a fresh agent turn against `targetSessionId`.
|
|
37
|
-
* All assistant output is appended into the same
|
|
36
|
+
* All assistant output is appended into the same OutputStream so it visually
|
|
38
37
|
* continues the hook's status/preview lines. */
|
|
39
38
|
export interface SessionHookInject {
|
|
40
39
|
targetSessionId: string;
|
|
@@ -50,14 +49,14 @@ export interface SessionHookSpec {
|
|
|
50
49
|
}
|
|
51
50
|
/** Unified pipeline: post status line → spawn subprocess → post preview/error line →
|
|
52
51
|
* optionally inject stdout as a fresh agent turn. Every Slack write goes through
|
|
53
|
-
* `
|
|
52
|
+
* `stream` so hook output and the follow-up agent turn share one continuous thread.
|
|
54
53
|
*
|
|
55
|
-
* Caller controls the `
|
|
56
|
-
* - onMessageEnd: pass the assistant turn's
|
|
54
|
+
* Caller controls the `stream`:
|
|
55
|
+
* - onMessageEnd: pass the assistant turn's stream so hook lines extend the same
|
|
57
56
|
* reply chain (no top-level leak).
|
|
58
|
-
* - onNew: pass a fresh
|
|
57
|
+
* - onNew: pass a fresh stream anchored at the last assistant message's thread parent
|
|
59
58
|
* (resolved via resolveSessionThreadTs). */
|
|
60
|
-
export declare function runSessionHook(spec: SessionHookSpec,
|
|
59
|
+
export declare function runSessionHook(spec: SessionHookSpec, stream: OutputStream): Promise<void>;
|
|
61
60
|
/** Resolve the profile name to use when injecting the onNew hook's stdout as a fresh
|
|
62
61
|
* agent turn. Priority order:
|
|
63
62
|
* 1. session-registry (per-session truth — correct for thread-spawned sessions whose
|
|
@@ -93,9 +92,9 @@ export interface OnMessageEndArgs {
|
|
|
93
92
|
sessionName: string;
|
|
94
93
|
executionId: string;
|
|
95
94
|
profile?: string | null;
|
|
96
|
-
/**
|
|
95
|
+
/** OutputStream to extend — pass the assistant turn's stream so the hook lines
|
|
97
96
|
* thread under the just-finished reply rather than leaking to top-level. */
|
|
98
|
-
|
|
97
|
+
stream: OutputStream;
|
|
99
98
|
}
|
|
100
99
|
/** Run the onMessageEnd hook against the just-finished assistant turn's vm.
|
|
101
100
|
* No-op when not configured. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// input: agent-server/session-hooks.json (lazy-read), spawn() subprocess,
|
|
1
|
+
// input: agent-server/session-hooks.json (lazy-read), spawn() subprocess, OutputStream
|
|
2
2
|
// output: runSessionHook() unified pipeline + fireAndForgetPreCloseHook (onNew) + onMessageEnd helpers
|
|
3
3
|
// pos: session-level hook subsystem — config loading, subprocess spawn, unified Slack-display pipeline
|
|
4
4
|
// (parallel to thread-hook system, scoped to channel/session rather than thread)
|
|
@@ -10,9 +10,8 @@ import { DATA_DIR, CONFIG_DIR } from '../../core/paths.js';
|
|
|
10
10
|
import { createLogger } from '../../core/log.js';
|
|
11
11
|
import { runAgent, resolveBackendForChannel } from '../agents/index.js';
|
|
12
12
|
import { getSessionAsync } from '../sessions/session.js';
|
|
13
|
-
import {
|
|
13
|
+
import { sessionStore } from '../../store/session-registry-repo.js';
|
|
14
14
|
import { conversationLedger } from '../../store/conversation-ledger-repo.js';
|
|
15
|
-
import { VirtualMessage } from '../../platform/virtual-message.js';
|
|
16
15
|
const log = createLogger('session-hook');
|
|
17
16
|
const CONFIG_FILE = path.join(CONFIG_DIR, 'session-hooks.json');
|
|
18
17
|
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
@@ -124,19 +123,19 @@ function buildHookStdin(name, ctx) {
|
|
|
124
123
|
}
|
|
125
124
|
/** Unified pipeline: post status line → spawn subprocess → post preview/error line →
|
|
126
125
|
* optionally inject stdout as a fresh agent turn. Every Slack write goes through
|
|
127
|
-
* `
|
|
126
|
+
* `stream` so hook output and the follow-up agent turn share one continuous thread.
|
|
128
127
|
*
|
|
129
|
-
* Caller controls the `
|
|
130
|
-
* - onMessageEnd: pass the assistant turn's
|
|
128
|
+
* Caller controls the `stream`:
|
|
129
|
+
* - onMessageEnd: pass the assistant turn's stream so hook lines extend the same
|
|
131
130
|
* reply chain (no top-level leak).
|
|
132
|
-
* - onNew: pass a fresh
|
|
131
|
+
* - onNew: pass a fresh stream anchored at the last assistant message's thread parent
|
|
133
132
|
* (resolved via resolveSessionThreadTs). */
|
|
134
|
-
export async function runSessionHook(spec,
|
|
133
|
+
export async function runSessionHook(spec, stream) {
|
|
135
134
|
const cfg = loadHookConfig(spec.name);
|
|
136
135
|
if (!cfg)
|
|
137
136
|
return;
|
|
138
137
|
const label = (cfg.args && cfg.args.length) ? `${cfg.command} ${cfg.args.join(' ')}` : cfg.command;
|
|
139
|
-
|
|
138
|
+
stream.emitText(spec.format.statusLine());
|
|
140
139
|
const result = await spawnHookProcess({
|
|
141
140
|
cfg,
|
|
142
141
|
stdinPayload: buildHookStdin(spec.name, spec.ctx),
|
|
@@ -144,20 +143,20 @@ export async function runSessionHook(spec, vm) {
|
|
|
144
143
|
label,
|
|
145
144
|
});
|
|
146
145
|
if (result.error) {
|
|
147
|
-
|
|
148
|
-
await
|
|
146
|
+
stream.emitText(spec.format.errorLine(result.error));
|
|
147
|
+
await stream.flush().catch((e) => log.warn(`stream.flush after error failed (${label}): ${e?.message || e}`));
|
|
149
148
|
return;
|
|
150
149
|
}
|
|
151
150
|
if (!result.stdout) {
|
|
152
151
|
const empty = spec.format.emptyLine?.();
|
|
153
152
|
if (empty)
|
|
154
|
-
|
|
155
|
-
await
|
|
153
|
+
stream.emitText(empty);
|
|
154
|
+
await stream.flush().catch((e) => log.warn(`stream.flush on empty failed (${label}): ${e?.message || e}`));
|
|
156
155
|
return;
|
|
157
156
|
}
|
|
158
|
-
|
|
157
|
+
stream.emitText(spec.format.previewLine(result.stdout));
|
|
159
158
|
if (!spec.inject) {
|
|
160
|
-
await
|
|
159
|
+
await stream.flush().catch((e) => log.warn(`stream.flush no-inject failed (${label}): ${e?.message || e}`));
|
|
161
160
|
return;
|
|
162
161
|
}
|
|
163
162
|
try {
|
|
@@ -168,17 +167,17 @@ export async function runSessionHook(spec, vm) {
|
|
|
168
167
|
isUserInitiated: false,
|
|
169
168
|
profileName: spec.inject.profileName,
|
|
170
169
|
trigger: spec.inject.trigger ?? `hook:${spec.name}`,
|
|
171
|
-
onAssistantMessage: (text) =>
|
|
170
|
+
onAssistantMessage: (text) => stream.emitText(text),
|
|
172
171
|
});
|
|
173
172
|
await handle.promise;
|
|
174
173
|
}
|
|
175
174
|
catch (err) {
|
|
176
175
|
const msg = err?.message || String(err);
|
|
177
176
|
log.error(`hook ${spec.name} injected agent failed: ${msg}`);
|
|
178
|
-
|
|
177
|
+
stream.emitText(`:warning: ${spec.name} hook follow-up failed: ${msg}`);
|
|
179
178
|
}
|
|
180
179
|
finally {
|
|
181
|
-
await
|
|
180
|
+
await stream.flush().catch((e) => log.warn(`stream.flush after inject failed (${label}): ${e?.message || e}`));
|
|
182
181
|
}
|
|
183
182
|
}
|
|
184
183
|
// ── onNew (pre-close) entry points ────────────────────────────────────────────
|
|
@@ -227,17 +226,17 @@ export async function resolveOnNewProfileName(channel, sessionId, deps) {
|
|
|
227
226
|
/** Default binding of the registry lookup — composes lookupBySessionId + lookupSession.
|
|
228
227
|
* Extracted so prepareOnNewRun's call site stays one line and tests can swap in fakes. */
|
|
229
228
|
async function defaultLookupRegistryProfile(sessionId) {
|
|
230
|
-
const name = await
|
|
229
|
+
const name = await sessionStore.lookupBySessionId(sessionId);
|
|
231
230
|
if (!name)
|
|
232
231
|
return null;
|
|
233
|
-
const record = await
|
|
232
|
+
const record = await sessionStore.lookupSession(name);
|
|
234
233
|
return record?.profileName ?? null;
|
|
235
234
|
}
|
|
236
235
|
async function defaultLookupLedgerProfile(channel) {
|
|
237
236
|
const conv = await conversationLedger.getConversation(channel);
|
|
238
237
|
return conv?.profileName ?? null;
|
|
239
238
|
}
|
|
240
|
-
/** Build the onNew SessionHookSpec + a fresh
|
|
239
|
+
/** Build the onNew SessionHookSpec + a fresh stream. Returns null when the hook isn't
|
|
241
240
|
* configured or there's no live session to capture context from. */
|
|
242
241
|
async function prepareOnNewRun(channel, adapter, threadTs) {
|
|
243
242
|
if (!isOnNewHookConfigured())
|
|
@@ -248,7 +247,7 @@ async function prepareOnNewRun(channel, adapter, threadTs) {
|
|
|
248
247
|
log.info('onNew hook skipped: no active session for channel', channel);
|
|
249
248
|
return null;
|
|
250
249
|
}
|
|
251
|
-
const sessionName = (await
|
|
250
|
+
const sessionName = (await sessionStore.lookupBySessionId(sessionId)) || sessionId.slice(0, 8);
|
|
252
251
|
// Resolve profile BEFORE handleNewCmd clears the ledger (sync, runs after this fn returns).
|
|
253
252
|
// Registry is the per-session truth; ledger is the channel-level fallback.
|
|
254
253
|
const profileName = await resolveOnNewProfileName(channel, sessionId, {
|
|
@@ -256,14 +255,14 @@ async function prepareOnNewRun(channel, adapter, threadTs) {
|
|
|
256
255
|
lookupLedgerProfile: defaultLookupLedgerProfile,
|
|
257
256
|
});
|
|
258
257
|
const anchor = await resolveOnNewThreadAnchor(channel, threadTs);
|
|
259
|
-
const
|
|
258
|
+
const stream = adapter.openOutputStream({ type: 'interactive-reply', conduit: channel, sessionId: '' }, { threadId: anchor });
|
|
260
259
|
const spec = {
|
|
261
260
|
name: 'onNew',
|
|
262
261
|
ctx: { channel, sessionId, sessionName, profile: profileName },
|
|
263
262
|
format: ONNEW_FORMAT,
|
|
264
263
|
inject: { targetSessionId: sessionId, profileName, trigger: 'hook:onNew' },
|
|
265
264
|
};
|
|
266
|
-
return { spec,
|
|
265
|
+
return { spec, stream };
|
|
267
266
|
}
|
|
268
267
|
/** Fire-and-forget onNew hook used by the !new command and the "New" status button.
|
|
269
268
|
* Returns immediately; the hook subprocess and any injected agent turn run async
|
|
@@ -273,7 +272,7 @@ export async function fireAndForgetPreCloseHook(channel, adapter, threadTs) {
|
|
|
273
272
|
const prepared = await prepareOnNewRun(channel, adapter, threadTs);
|
|
274
273
|
if (!prepared)
|
|
275
274
|
return;
|
|
276
|
-
void runSessionHook(prepared.spec, prepared.
|
|
275
|
+
void runSessionHook(prepared.spec, prepared.stream).catch((err) => {
|
|
277
276
|
log.error('onNew hook async completion failed:', err?.message || err);
|
|
278
277
|
});
|
|
279
278
|
}
|
|
@@ -284,7 +283,7 @@ export async function runPreCloseHook(channel, adapter, threadTs) {
|
|
|
284
283
|
const prepared = await prepareOnNewRun(channel, adapter, threadTs);
|
|
285
284
|
if (!prepared)
|
|
286
285
|
return;
|
|
287
|
-
await runSessionHook(prepared.spec, prepared.
|
|
286
|
+
await runSessionHook(prepared.spec, prepared.stream);
|
|
288
287
|
}
|
|
289
288
|
/** Run the onMessageEnd hook against the just-finished assistant turn's vm.
|
|
290
289
|
* No-op when not configured. */
|
|
@@ -307,5 +306,5 @@ export async function runMessageEndSessionHook(args) {
|
|
|
307
306
|
trigger: 'hook:onMessageEnd',
|
|
308
307
|
},
|
|
309
308
|
};
|
|
310
|
-
await runSessionHook(spec, args.
|
|
309
|
+
await runSessionHook(spec, args.stream);
|
|
311
310
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export { sessionRegistryRepo, type
|
|
1
|
+
export { sessionStore, sessionRegistryRepo, type Session, type SessionRegistryData } from '../../store/session-registry-repo.js';
|
|
2
2
|
export declare function generateSessionName(): Promise<string>;
|
|
3
3
|
export declare function registerSession(name: string, opts: {
|
|
4
4
|
sessionId: string;
|
|
5
5
|
channel: string;
|
|
6
6
|
backend: string;
|
|
7
7
|
kind: 'local' | 'scheduled';
|
|
8
|
+
projectId?: string;
|
|
8
9
|
label?: string | null;
|
|
9
10
|
profileName?: string | null;
|
|
10
11
|
}): Promise<void>;
|
|
@@ -12,10 +13,14 @@ export declare function updateSession(name: string, updates: {
|
|
|
12
13
|
sessionId?: string;
|
|
13
14
|
lastUsedAt?: string;
|
|
14
15
|
label?: string | null;
|
|
16
|
+
profileName?: string | null;
|
|
15
17
|
}): Promise<void>;
|
|
16
|
-
export declare function lookupSession(name: string): Promise<import("../../store/session-registry-repo.js").
|
|
18
|
+
export declare function lookupSession(name: string): Promise<import("../../store/session-registry-repo.js").Session>;
|
|
17
19
|
export declare function lookupBySessionId(sessionId: string): Promise<string>;
|
|
18
|
-
export declare function listRecentSessions(limit?: number): Promise<(
|
|
19
|
-
name: string;
|
|
20
|
-
} & import("../../store/session-registry-repo.js").SessionRecord)[]>;
|
|
20
|
+
export declare function listRecentSessions(limit?: number): Promise<import("../../store/session-registry-repo.js").Session[]>;
|
|
21
21
|
export declare function getActiveSessionName(channel: string, backend: string): Promise<string>;
|
|
22
|
+
export declare function getById(sessionId: string): Promise<import("../../store/session-registry-repo.js").Session>;
|
|
23
|
+
export declare function listByProject(projectId: string): Promise<import("../../store/session-registry-repo.js").Session[]>;
|
|
24
|
+
export declare function listResumable(projectId?: string): Promise<import("../../store/session-registry-repo.js").Session[]>;
|
|
25
|
+
export declare function markUsed(sessionId: string): Promise<void>;
|
|
26
|
+
export declare function pruneStale(maxAgeMs: number): Promise<number>;
|
|
@@ -6,26 +6,41 @@
|
|
|
6
6
|
// This file is kept only so any external tooling that still imports this path
|
|
7
7
|
// continues to compile until the S12 git-mv sweep.
|
|
8
8
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
9
|
-
import {
|
|
10
|
-
export { sessionRegistryRepo } from '../../store/session-registry-repo.js';
|
|
9
|
+
import { sessionStore } from '../../store/session-registry-repo.js';
|
|
10
|
+
export { sessionStore, sessionRegistryRepo } from '../../store/session-registry-repo.js';
|
|
11
11
|
export function generateSessionName() {
|
|
12
|
-
return
|
|
12
|
+
return sessionStore.generateSessionName();
|
|
13
13
|
}
|
|
14
14
|
export function registerSession(name, opts) {
|
|
15
|
-
return
|
|
15
|
+
return sessionStore.registerSession(name, opts);
|
|
16
16
|
}
|
|
17
17
|
export function updateSession(name, updates) {
|
|
18
|
-
return
|
|
18
|
+
return sessionStore.updateSession(name, updates);
|
|
19
19
|
}
|
|
20
20
|
export function lookupSession(name) {
|
|
21
|
-
return
|
|
21
|
+
return sessionStore.lookupSession(name);
|
|
22
22
|
}
|
|
23
23
|
export function lookupBySessionId(sessionId) {
|
|
24
|
-
return
|
|
24
|
+
return sessionStore.lookupBySessionId(sessionId);
|
|
25
25
|
}
|
|
26
26
|
export function listRecentSessions(limit = 10) {
|
|
27
|
-
return
|
|
27
|
+
return sessionStore.listRecentSessions(limit);
|
|
28
28
|
}
|
|
29
29
|
export function getActiveSessionName(channel, backend) {
|
|
30
|
-
return
|
|
30
|
+
return sessionStore.getActiveSessionName(channel, backend);
|
|
31
|
+
}
|
|
32
|
+
export function getById(sessionId) {
|
|
33
|
+
return sessionStore.getById(sessionId);
|
|
34
|
+
}
|
|
35
|
+
export function listByProject(projectId) {
|
|
36
|
+
return sessionStore.listByProject(projectId);
|
|
37
|
+
}
|
|
38
|
+
export function listResumable(projectId) {
|
|
39
|
+
return sessionStore.listResumable(projectId);
|
|
40
|
+
}
|
|
41
|
+
export function markUsed(sessionId) {
|
|
42
|
+
return sessionStore.markUsed(sessionId);
|
|
43
|
+
}
|
|
44
|
+
export function pruneStale(maxAgeMs) {
|
|
45
|
+
return sessionStore.pruneStale(maxAgeMs);
|
|
31
46
|
}
|
|
@@ -6,7 +6,6 @@ import { readFileSync, writeFileSync } from 'fs';
|
|
|
6
6
|
import * as path from 'path';
|
|
7
7
|
import { STORE_DIR, formatDurationCompact } from '../../core/utils.js';
|
|
8
8
|
import { createLogger } from '../../core/log.js';
|
|
9
|
-
import { channelRepo } from '../../store/channel-repo.js';
|
|
10
9
|
import * as executionRegistry from '../executions/registry.js';
|
|
11
10
|
const log = createLogger('pending-task-tracker');
|
|
12
11
|
const PENDING_TASKS_FILE = path.join(STORE_DIR, 'pending-tasks.json');
|
|
@@ -56,11 +55,10 @@ function init(adapter) {
|
|
|
56
55
|
_adapter = adapter;
|
|
57
56
|
}
|
|
58
57
|
async function onTaskLaunched({ taskId, machine, channel, scheduleTaskId, taskText, taskHash, project, sessionName, tmuxName, pid }) {
|
|
59
|
-
const trackingChannel = (project && await channelRepo.getProjectChannel(project)) || channel;
|
|
60
58
|
executionRegistry.registerDispatchExecution({
|
|
61
59
|
taskId,
|
|
62
60
|
machine,
|
|
63
|
-
channel
|
|
61
|
+
channel,
|
|
64
62
|
project,
|
|
65
63
|
scheduleTaskId,
|
|
66
64
|
taskText,
|
|
@@ -70,18 +68,19 @@ async function onTaskLaunched({ taskId, machine, channel, scheduleTaskId, taskTe
|
|
|
70
68
|
pid,
|
|
71
69
|
});
|
|
72
70
|
pendingTasks.set(taskId, {
|
|
73
|
-
channel:
|
|
71
|
+
channel: channel, machine, launchedAt: Date.now(),
|
|
74
72
|
scheduleTaskId: scheduleTaskId || null, taskText: taskText || null, taskHash: taskHash || null,
|
|
75
73
|
project: project || null, trackingTs: null,
|
|
76
74
|
sessionName: sessionName || null, tmuxName: tmuxName || null, pid: pid || null,
|
|
77
75
|
});
|
|
78
76
|
savePendingTasks();
|
|
79
|
-
log.info(`Task registered: ${taskId} on ${machine} (channel: ${
|
|
77
|
+
log.info(`Task registered: ${taskId} on ${machine} (channel: ${channel}, project: ${project || 'none'}, schedule: ${scheduleTaskId || 'none'}, pending: ${pendingTasks.size})`);
|
|
80
78
|
const trackingText = buildTrackingMessage({ taskId, machine, taskText, startedAtMs: Date.now(), elapsed_s: 0, turn_count: 0, status: 'running' });
|
|
81
|
-
if (
|
|
79
|
+
if (channel && _adapter) {
|
|
82
80
|
(async () => {
|
|
83
81
|
try {
|
|
84
|
-
const
|
|
82
|
+
const trackingDest = { type: 'project-report', projectId: project || 'general', trigger: 'dispatch', sessionId: '' };
|
|
83
|
+
const ref = await _adapter.postMessage(trackingDest, { text: trackingText });
|
|
85
84
|
const t = pendingTasks.get(taskId);
|
|
86
85
|
if (t) {
|
|
87
86
|
t.trackingTs = ref.messageId;
|
|
@@ -109,11 +108,11 @@ function handleTaskProgress({ task_id, machine, cost_usd, turn_count, elapsed_s
|
|
|
109
108
|
}
|
|
110
109
|
if (!t || !t.trackingTs || !_adapter)
|
|
111
110
|
return;
|
|
112
|
-
const
|
|
111
|
+
const channel = t.channel;
|
|
113
112
|
const text = buildTrackingMessage({ taskId: task_id, machine: machine || t.machine, taskText: t.taskText, startedAtMs: t.launchedAt, elapsed_s, turn_count, cost_usd, status: 'running' });
|
|
114
113
|
(async () => {
|
|
115
114
|
try {
|
|
116
|
-
await _adapter.updateMessage({ channel:
|
|
115
|
+
await _adapter.updateMessage({ channel: channel, messageId: t.trackingTs }, { text });
|
|
117
116
|
}
|
|
118
117
|
catch (e) {
|
|
119
118
|
log.error(`Failed to update tracking for ${task_id}:`, e.message);
|