@goodandready/dsh-cron 0.3.43 → 0.3.44
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/lib/client.js +998 -3987
- package/lib/llm-ask.js +2 -1
- package/lib/routes/api-actions.js +36 -6
- package/lib/routes/api-item.js +1 -1
- package/lib/routes/api-webhook.js +2 -4
- package/lib/runtimes/runner-external.js +2 -3
- package/lib/runtimes/runner-session.js +385 -0
- package/lib/runtimes/runner-turn.js +255 -0
- package/lib/runtimes/runner.js +43 -616
- package/lib/scheduler/scheduler-execution.js +4 -1
- package/lib/scheduler/scheduler-lifecycle.js +65 -0
- package/lib/scheduler/scheduler-parser.js +261 -0
- package/lib/scheduler/scheduler.js +13 -319
- package/lib/store/store-archive.js +195 -0
- package/lib/store/store-io.js +331 -0
- package/lib/store/store-loader.js +165 -0
- package/lib/store/store-migration.js +38 -0
- package/lib/store/store-settings.js +126 -0
- package/lib/store/store-stats.js +55 -0
- package/lib/store/store.js +75 -888
- package/package.json +8 -6
- package/lib/client-src/00-open.js +0 -63
- package/lib/client-src/10-locales.js +0 -540
- package/lib/client-src/20-styles.js +0 -64
- package/lib/client-src/30-utils.js +0 -372
- package/lib/client-src/40-delivery-settings.js +0 -96
- package/lib/client-src/45-dag-graph-view.js +0 -287
- package/lib/client-src/46-live-logs-modal.js +0 -108
- package/lib/client-src/47-analytics-digest-modal.js +0 -166
- package/lib/client-src/48-timeline-heatmap-view.js +0 -176
- package/lib/client-src/50-cron-screen-state.js +0 -457
- package/lib/client-src/52-cron-screen-actions.js +0 -330
- package/lib/client-src/53-cron-screen-modal-actions.js +0 -677
- package/lib/client-src/54-cron-screen-analytics-actions.js +0 -68
- package/lib/client-src/55-cron-screen-view.js +0 -378
- package/lib/client-src/60-modal-task-form-head.js +0 -581
- package/lib/client-src/62-modal-task-form-tail.js +0 -563
- package/lib/client-src/65-modal-dialogs.js +0 -350
- package/lib/client-src/70-settings-card.js +0 -453
- package/lib/client-src/75-quick-action-bar.js +0 -146
- package/lib/client-src/80-sidebar-jobs.js +0 -200
- package/lib/client-src/90-close.js +0 -219
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { estimateTokenCost } from "../integrations.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { logger } from "../logger.js";
|
|
4
|
+
import { bestEffort } from "../best-effort.js";
|
|
5
|
+
|
|
6
|
+
export async function executeAgentTurn(runner, task, prep) {
|
|
7
|
+
let handle = prep?.handle || null;
|
|
8
|
+
let isResumed = false;
|
|
9
|
+
try {
|
|
10
|
+
// Resolve agent preset (#GH-1, #GH-2): scheduled llm sessions must join an agent preset
|
|
11
|
+
// (default or task-configured), otherwise tools resolve against the empty global layer.
|
|
12
|
+
let presets = null;
|
|
13
|
+
try {
|
|
14
|
+
if (typeof runner.ctx.get === 'function') {
|
|
15
|
+
presets = runner.ctx.get('agentPresets');
|
|
16
|
+
} else {
|
|
17
|
+
presets = runner.ctx.agentPresets;
|
|
18
|
+
}
|
|
19
|
+
} catch {
|
|
20
|
+
presets = null;
|
|
21
|
+
}
|
|
22
|
+
let preset = null;
|
|
23
|
+
if (presets && typeof presets.resolve === 'function') {
|
|
24
|
+
try {
|
|
25
|
+
preset = await presets.resolve(task.agentPreset || undefined);
|
|
26
|
+
if (preset && typeof presets.standingKeyFor === 'function') {
|
|
27
|
+
await presets.standingKeyFor(preset.id);
|
|
28
|
+
}
|
|
29
|
+
} catch (presetErr) {
|
|
30
|
+
logger.warn('[dsh-cron] agent preset unavailable, running without one:', presetErr && presetErr.message);
|
|
31
|
+
preset = null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const presetSetup = preset ? {
|
|
36
|
+
setup: async (agentCtx) => {
|
|
37
|
+
if (typeof presets.mount === 'function') {
|
|
38
|
+
await presets.mount(agentCtx, preset.id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} : {};
|
|
42
|
+
|
|
43
|
+
// If targetSessionId is set, attempt to borrow or resume the existing session (#143, #346, GH #9, GH #23)
|
|
44
|
+
let resumeFailure = null;
|
|
45
|
+
const agents = runner._getAgentsService();
|
|
46
|
+
if (prep.targetSessionId && agents) {
|
|
47
|
+
let liveAgent = null;
|
|
48
|
+
try {
|
|
49
|
+
if (typeof agents.get === 'function') {
|
|
50
|
+
liveAgent = agents.get(prep.targetSessionId);
|
|
51
|
+
}
|
|
52
|
+
} catch (_getErr) {
|
|
53
|
+
liveAgent = null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (liveAgent) {
|
|
57
|
+
handle = { agent: liveAgent, __borrowedLive: true };
|
|
58
|
+
isResumed = true;
|
|
59
|
+
logger.info(`[dsh-cron] borrowed active live session: ${prep.targetSessionId}`);
|
|
60
|
+
} else if (typeof agents.resume === 'function') {
|
|
61
|
+
try {
|
|
62
|
+
handle = await agents.resume({
|
|
63
|
+
sessionId: prep.targetSessionId,
|
|
64
|
+
resumeSessionId: prep.targetSessionId,
|
|
65
|
+
meta: {
|
|
66
|
+
cwd: prep.cwd,
|
|
67
|
+
...(preset ? { agentPreset: preset.id } : {})
|
|
68
|
+
},
|
|
69
|
+
agentOptions: prep.provider && prep.model ? { provider: prep.provider, model: prep.model } : undefined,
|
|
70
|
+
...presetSetup,
|
|
71
|
+
});
|
|
72
|
+
isResumed = true;
|
|
73
|
+
logger.info(`[dsh-cron] resumed existing persistent session: ${prep.targetSessionId}`);
|
|
74
|
+
} catch (resumeErr) {
|
|
75
|
+
resumeFailure = resumeErr;
|
|
76
|
+
logger.warn(`[dsh-cron] could not resume session ${prep.targetSessionId} (${resumeErr.message}), fallback creating new session`);
|
|
77
|
+
handle = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Create new session if not resumed
|
|
83
|
+
if (!handle) {
|
|
84
|
+
const isLockedOrColliding = resumeFailure && /already (?:active|owned|exists)|write handle|locked|collision/i.test(resumeFailure.message || '');
|
|
85
|
+
const targetId = prep.mintSessionId({ fresh: isLockedOrColliding });
|
|
86
|
+
try {
|
|
87
|
+
handle = await agents.create({
|
|
88
|
+
sessionId: targetId,
|
|
89
|
+
meta: {
|
|
90
|
+
cwd: prep.cwd,
|
|
91
|
+
ephemeral: !prep.targetSessionId,
|
|
92
|
+
internal: !prep.targetSessionId,
|
|
93
|
+
...(preset ? { agentPreset: preset.id } : {})
|
|
94
|
+
},
|
|
95
|
+
agentOptions: prep.provider && prep.model ? { provider: prep.provider, model: prep.model } : undefined,
|
|
96
|
+
...presetSetup,
|
|
97
|
+
});
|
|
98
|
+
} catch (createErr) {
|
|
99
|
+
if (prep.targetSessionId && targetId === prep.targetSessionId) {
|
|
100
|
+
logger.warn(`[dsh-cron] failed to create session with targetSessionId ${prep.targetSessionId} (${createErr.message}), retrying with fresh session ID`);
|
|
101
|
+
try {
|
|
102
|
+
handle = await agents.create({
|
|
103
|
+
sessionId: prep.mintSessionId({ fresh: true }),
|
|
104
|
+
meta: {
|
|
105
|
+
cwd: prep.cwd,
|
|
106
|
+
ephemeral: !prep.targetSessionId,
|
|
107
|
+
internal: !prep.targetSessionId,
|
|
108
|
+
...(preset ? { agentPreset: preset.id } : {})
|
|
109
|
+
},
|
|
110
|
+
agentOptions: prep.provider && prep.model ? { provider: prep.provider, model: prep.model } : undefined,
|
|
111
|
+
...presetSetup,
|
|
112
|
+
});
|
|
113
|
+
} catch (retryErr) {
|
|
114
|
+
if (resumeFailure) {
|
|
115
|
+
throw new Error(`could not resume session "${prep.targetSessionId}" (${resumeFailure.message}): ${retryErr.message}`);
|
|
116
|
+
}
|
|
117
|
+
throw retryErr;
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
if (resumeFailure) {
|
|
121
|
+
throw new Error(`could not resume session "${prep.targetSessionId}" (${resumeFailure.message}): ${createErr.message}`);
|
|
122
|
+
}
|
|
123
|
+
throw createErr;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// workspace attach & archive: register the new session with its workspace ledger so it shows
|
|
129
|
+
// up under the project instead of "ungrouped". attachSession validates
|
|
130
|
+
// that the session cwd resolves to the workspace path, so skip when the
|
|
131
|
+
// run uses an isolated worktree (different cwd) or the workspaceId does
|
|
132
|
+
// not resolve to a registered workspace.
|
|
133
|
+
if (!isResumed && !prep.taskWorktree && task.workspaceId && handle?.agent?.session?.id) {
|
|
134
|
+
await bestEffort('attach-session-to-workspace', () =>
|
|
135
|
+
runner._resolveWorkspace(task)?.attachSession(handle.agent.session.id)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
runner._applyPermissionPreset(task, handle);
|
|
140
|
+
runner._nameSession(task, handle, isResumed);
|
|
141
|
+
await prep.raceAbort(handle.agent.whenIdle());
|
|
142
|
+
|
|
143
|
+
// 1. Capture starting sequence before sending user message (#227, #228)
|
|
144
|
+
const session = handle.agent.session;
|
|
145
|
+
const startSeq = (session && typeof session.seq === 'number')
|
|
146
|
+
? session.seq
|
|
147
|
+
: (Array.isArray(session?.log) ? session.log.length : 0);
|
|
148
|
+
|
|
149
|
+
handle.agent.followup(prep.createUserMessage({
|
|
150
|
+
content: [{ type: 'text', text: prep.agentPrompt }],
|
|
151
|
+
source: { kind: 'plugin:dsh-cron', form: 'cron-execute' },
|
|
152
|
+
}));
|
|
153
|
+
// Await agent turn completion (#67), abortable on timeout/overlap (#88)
|
|
154
|
+
if (typeof handle.agent.whenIdle === 'function') {
|
|
155
|
+
await prep.raceAbort(handle.agent.whenIdle());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 2. Snapshot events from this turn only (#227, #228)
|
|
159
|
+
let turnEvents = [];
|
|
160
|
+
if (typeof session?.snapshotEvents === 'function') {
|
|
161
|
+
turnEvents = session.snapshotEvents(startSeq) || [];
|
|
162
|
+
} else if (Array.isArray(session?.log)) {
|
|
163
|
+
turnEvents = session.log.slice(startSeq);
|
|
164
|
+
} else if (Array.isArray(handle.agent?.log)) {
|
|
165
|
+
turnEvents = handle.agent.log.slice(startSeq);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Extract usage FIRST so failed turns and budget checks have accurate token and cost accounting (#228)
|
|
169
|
+
const usage = runner._extractUsage(handle, turnEvents);
|
|
170
|
+
const costUsd = estimateTokenCost(prep.model, usage);
|
|
171
|
+
|
|
172
|
+
// 3. Terminal turn failure and settlement check (#227)
|
|
173
|
+
let hasTerminalEvent = false;
|
|
174
|
+
for (const ev of turnEvents) {
|
|
175
|
+
if (!ev) continue;
|
|
176
|
+
if (ev.type === 'turn/end' || ev.type === 'turn/error' || ev.type === 'turn/failure') {
|
|
177
|
+
hasTerminalEvent = true;
|
|
178
|
+
const reason = ev.data?.reason || ev.reason;
|
|
179
|
+
const reasonKind = (reason && typeof reason === 'object') ? reason.kind : reason;
|
|
180
|
+
if (['error', 'interrupted', 'failed', 'aborted', 'blocked'].includes(reasonKind) || ev.data?.error || ev.error) {
|
|
181
|
+
const errMsg = ev.data?.error?.message || ev.error?.message || reason?.message || (typeof reason === 'string' ? reason : `Agent turn ended with status: ${reasonKind || 'error'}`);
|
|
182
|
+
const err = new Error(errMsg);
|
|
183
|
+
err.reason = reason;
|
|
184
|
+
err.reasonKind = reasonKind;
|
|
185
|
+
err.turnFailed = true;
|
|
186
|
+
err.usage = usage;
|
|
187
|
+
err.costUsd = costUsd;
|
|
188
|
+
err.sessionId = handle.agent.session?.id || null;
|
|
189
|
+
throw err;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const hasEventLog = typeof session?.snapshotEvents === 'function' || Array.isArray(session?.log) || Array.isArray(handle.agent?.log);
|
|
195
|
+
if (hasEventLog && !hasTerminalEvent) {
|
|
196
|
+
const err = new Error(
|
|
197
|
+
turnEvents.length === 0
|
|
198
|
+
? 'Agent turn produced no events before quiescence'
|
|
199
|
+
: 'Agent turn ended without terminal completion event'
|
|
200
|
+
);
|
|
201
|
+
err.turnFailed = true;
|
|
202
|
+
err.usage = usage;
|
|
203
|
+
err.costUsd = costUsd;
|
|
204
|
+
err.sessionId = handle.agent.session?.id || null;
|
|
205
|
+
throw err;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 4. Extract assistant message text from turn events (#227)
|
|
209
|
+
const messageTexts = [];
|
|
210
|
+
const chunkTexts = [];
|
|
211
|
+
for (const ev of turnEvents) {
|
|
212
|
+
if (!ev) continue;
|
|
213
|
+
if (ev.type === 'assistant/message') {
|
|
214
|
+
const msg = ev.data?.message || ev.data;
|
|
215
|
+
if (msg) {
|
|
216
|
+
if (typeof msg.content === 'string' && msg.content.trim()) {
|
|
217
|
+
messageTexts.push(msg.content.trim());
|
|
218
|
+
} else if (Array.isArray(msg.content)) {
|
|
219
|
+
const parts = msg.content
|
|
220
|
+
.filter((p) => p && p.type === 'text' && typeof p.text === 'string')
|
|
221
|
+
.map((p) => p.text)
|
|
222
|
+
.join('');
|
|
223
|
+
if (parts.trim()) messageTexts.push(parts.trim());
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
} else if (ev.type === 'assistant/chunk' && ev.data?.chunk) {
|
|
227
|
+
const chunk = ev.data.chunk;
|
|
228
|
+
if (chunk.type === 'text' && typeof chunk.text === 'string') {
|
|
229
|
+
chunkTexts.push(chunk.text);
|
|
230
|
+
} else if (chunk.type === 'text-delta' && typeof chunk.delta === 'string') {
|
|
231
|
+
chunkTexts.push(chunk.delta);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
let outputText = messageTexts.length > 0 ? messageTexts.join('\n') : chunkTexts.join('');
|
|
236
|
+
|
|
237
|
+
// Fallback if no assistant message text was extracted from turn events
|
|
238
|
+
if (!outputText.trim()) {
|
|
239
|
+
const sessionNote = isResumed
|
|
240
|
+
? `resumed: ${prep.targetSessionId}`
|
|
241
|
+
: `session: ${handle.agent.session?.id || 'cron'}`;
|
|
242
|
+
outputText = `[dsh-cron] Agent finished turn for task "${task.title}" (${sessionNote})`;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
output: outputText,
|
|
247
|
+
usage,
|
|
248
|
+
costUsd,
|
|
249
|
+
sessionId: handle.agent.session?.id || null,
|
|
250
|
+
};
|
|
251
|
+
} finally {
|
|
252
|
+
await runner._disposeAgentRun(task, prep, handle);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|