@gotgenes/pi-subagents 1.0.1 → 2.0.0
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/CHANGELOG.md +29 -0
- package/README.md +0 -35
- package/docs/architecture/architecture.md +4 -8
- package/docs/decisions/0001-deferred-patches.md +7 -4
- package/docs/plans/0051-update-adr-0001-hard-fork.md +74 -0
- package/docs/plans/0052-remove-scheduled-subagents.md +131 -0
- package/docs/retro/0051-update-adr-0001-hard-fork.md +33 -0
- package/package.json +1 -2
- package/src/agent-manager.ts +2 -2
- package/src/index.ts +2 -135
- package/src/settings.ts +0 -14
- package/src/types.ts +0 -43
- package/src/schedule-store.ts +0 -143
- package/src/schedule.ts +0 -365
- package/src/ui/schedule-menu.ts +0 -104
package/src/schedule.ts
DELETED
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* schedule.ts — `SubagentScheduler`: timer-driven dispatcher of scheduled subagents.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors the engine shape of pi-cron-schedule/src/scheduler.ts:
|
|
5
|
-
* - two-Map split (jobs = croner Cron, intervals = setInterval/setTimeout)
|
|
6
|
-
* - addJob/removeJob/updateJob/scheduleJob/unscheduleJob/executeJob
|
|
7
|
-
* - static parsers for cron / "+10m" / "5m" / ISO formats
|
|
8
|
-
*
|
|
9
|
-
* Differences vs pi-cron-schedule:
|
|
10
|
-
* - Persistence is via ScheduleStore (PID-locked, session-scoped, atomic).
|
|
11
|
-
* - `executeJob` calls `manager.spawn(..., { bypassQueue: true })` instead
|
|
12
|
-
* of dispatching a user message — schedule fires bypass maxConcurrent so
|
|
13
|
-
* a 5-minute interval can't be deferred behind 4 long-running agents.
|
|
14
|
-
* - Result delivery is implicit: spawn → background completion → existing
|
|
15
|
-
* `subagent-notification` followUp path. No new delivery code.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
19
|
-
import { Cron } from "croner";
|
|
20
|
-
import { nanoid } from "nanoid";
|
|
21
|
-
import type { AgentManager } from "./agent-manager.js";
|
|
22
|
-
import { resolveModel } from "./model-resolver.js";
|
|
23
|
-
import type { ScheduleStore } from "./schedule-store.js";
|
|
24
|
-
import type { IsolationMode, ScheduledSubagent, SubagentType, ThinkingLevel } from "./types.js";
|
|
25
|
-
|
|
26
|
-
/** Event emitted on `pi.events` for cross-extension consumers. */
|
|
27
|
-
export type ScheduleChangeEvent =
|
|
28
|
-
| { type: "added"; job: ScheduledSubagent }
|
|
29
|
-
| { type: "removed"; jobId: string }
|
|
30
|
-
| { type: "updated"; job: ScheduledSubagent }
|
|
31
|
-
| { type: "fired"; jobId: string; agentId: string; name: string }
|
|
32
|
-
| { type: "error"; jobId: string; error: string };
|
|
33
|
-
|
|
34
|
-
/** Params accepted at job creation — ID, timestamps, and state are derived. */
|
|
35
|
-
export interface NewJobInput {
|
|
36
|
-
name: string;
|
|
37
|
-
description: string;
|
|
38
|
-
schedule: string;
|
|
39
|
-
subagent_type: SubagentType;
|
|
40
|
-
prompt: string;
|
|
41
|
-
model?: string;
|
|
42
|
-
thinking?: ThinkingLevel;
|
|
43
|
-
max_turns?: number;
|
|
44
|
-
isolated?: boolean;
|
|
45
|
-
isolation?: IsolationMode;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export class SubagentScheduler {
|
|
49
|
-
private jobs = new Map<string, Cron>();
|
|
50
|
-
private intervals = new Map<string, NodeJS.Timeout>();
|
|
51
|
-
private store: ScheduleStore | undefined;
|
|
52
|
-
private pi: ExtensionAPI | undefined;
|
|
53
|
-
private ctx: ExtensionContext | undefined;
|
|
54
|
-
private manager: AgentManager | undefined;
|
|
55
|
-
|
|
56
|
-
/** Start the scheduler: bind to a session's store and arm enabled jobs. */
|
|
57
|
-
start(pi: ExtensionAPI, ctx: ExtensionContext, manager: AgentManager, store: ScheduleStore): void {
|
|
58
|
-
this.pi = pi;
|
|
59
|
-
this.ctx = ctx;
|
|
60
|
-
this.manager = manager;
|
|
61
|
-
this.store = store;
|
|
62
|
-
|
|
63
|
-
for (const job of store.list()) {
|
|
64
|
-
if (job.enabled) this.scheduleJob(job);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Stop all timers; drop refs. Safe to call repeatedly. */
|
|
69
|
-
stop(): void {
|
|
70
|
-
for (const cron of this.jobs.values()) cron.stop();
|
|
71
|
-
this.jobs.clear();
|
|
72
|
-
for (const t of this.intervals.values()) clearTimeout(t);
|
|
73
|
-
this.intervals.clear();
|
|
74
|
-
this.store = undefined;
|
|
75
|
-
this.pi = undefined;
|
|
76
|
-
this.ctx = undefined;
|
|
77
|
-
this.manager = undefined;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** True if start() has bound a store and the scheduler is active. */
|
|
81
|
-
isActive(): boolean {
|
|
82
|
-
return this.store !== undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
list(): ScheduledSubagent[] {
|
|
86
|
-
return this.store?.list() ?? [];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Build a `ScheduledSubagent` from user input. Validates the schedule
|
|
91
|
-
* format and tags `scheduleType`. Throws on invalid input.
|
|
92
|
-
*/
|
|
93
|
-
buildJob(input: NewJobInput): ScheduledSubagent {
|
|
94
|
-
const detected = SubagentScheduler.detectSchedule(input.schedule);
|
|
95
|
-
return {
|
|
96
|
-
id: nanoid(10),
|
|
97
|
-
name: input.name,
|
|
98
|
-
description: input.description,
|
|
99
|
-
schedule: detected.normalized,
|
|
100
|
-
scheduleType: detected.type,
|
|
101
|
-
intervalMs: detected.intervalMs,
|
|
102
|
-
subagent_type: input.subagent_type,
|
|
103
|
-
prompt: input.prompt,
|
|
104
|
-
model: input.model,
|
|
105
|
-
thinking: input.thinking,
|
|
106
|
-
max_turns: input.max_turns,
|
|
107
|
-
isolated: input.isolated,
|
|
108
|
-
isolation: input.isolation,
|
|
109
|
-
enabled: true,
|
|
110
|
-
createdAt: new Date().toISOString(),
|
|
111
|
-
runCount: 0,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Add a job, persist, and arm if enabled. Returns the stored job. */
|
|
116
|
-
addJob(input: NewJobInput): ScheduledSubagent {
|
|
117
|
-
const store = this.requireStore();
|
|
118
|
-
if (store.hasName(input.name)) {
|
|
119
|
-
throw new Error(`A scheduled job named "${input.name}" already exists.`);
|
|
120
|
-
}
|
|
121
|
-
const job = this.buildJob(input);
|
|
122
|
-
store.add(job);
|
|
123
|
-
if (job.enabled) this.scheduleJob(job);
|
|
124
|
-
this.emit({ type: "added", job });
|
|
125
|
-
return job;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
removeJob(id: string): boolean {
|
|
129
|
-
const store = this.requireStore();
|
|
130
|
-
if (!store.get(id)) return false;
|
|
131
|
-
this.unscheduleJob(id);
|
|
132
|
-
const ok = store.remove(id);
|
|
133
|
-
if (ok) this.emit({ type: "removed", jobId: id });
|
|
134
|
-
return ok;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
|
|
138
|
-
updateJob(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
|
|
139
|
-
const store = this.requireStore();
|
|
140
|
-
const updated = store.update(id, patch);
|
|
141
|
-
if (!updated) return undefined;
|
|
142
|
-
this.unscheduleJob(id);
|
|
143
|
-
if (updated.enabled) this.scheduleJob(updated);
|
|
144
|
-
this.emit({ type: "updated", job: updated });
|
|
145
|
-
return updated;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** Next-run time as ISO, or undefined if not currently armed. */
|
|
149
|
-
getNextRun(jobId: string): string | undefined {
|
|
150
|
-
const cron = this.jobs.get(jobId);
|
|
151
|
-
if (cron) return cron.nextRun()?.toISOString();
|
|
152
|
-
const job = this.store?.get(jobId);
|
|
153
|
-
if (!job?.enabled) return undefined;
|
|
154
|
-
if (job.scheduleType === "once") return job.schedule;
|
|
155
|
-
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
156
|
-
// Before the first fire there's no `lastRun`, so fall back to "now" —
|
|
157
|
-
// accurate at create time (setInterval was just armed) and within
|
|
158
|
-
// intervalMs of correct in any pre-first-fire view.
|
|
159
|
-
const base = job.lastRun ? new Date(job.lastRun).getTime() : Date.now();
|
|
160
|
-
return new Date(base + job.intervalMs).toISOString();
|
|
161
|
-
}
|
|
162
|
-
return undefined;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// ── Scheduling primitives ────────────────────────────────────────────
|
|
166
|
-
|
|
167
|
-
private scheduleJob(job: ScheduledSubagent): void {
|
|
168
|
-
const store = this.store;
|
|
169
|
-
if (!store) return;
|
|
170
|
-
try {
|
|
171
|
-
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
172
|
-
const t = setInterval(() => this.executeJob(job.id), job.intervalMs);
|
|
173
|
-
this.intervals.set(job.id, t);
|
|
174
|
-
} else if (job.scheduleType === "once") {
|
|
175
|
-
const target = new Date(job.schedule).getTime();
|
|
176
|
-
const delay = target - Date.now();
|
|
177
|
-
if (delay > 0) {
|
|
178
|
-
const t = setTimeout(() => {
|
|
179
|
-
this.executeJob(job.id);
|
|
180
|
-
// Auto-disable one-shots after they fire (mirrors pi-cron-schedule)
|
|
181
|
-
store.update(job.id, { enabled: false });
|
|
182
|
-
const updated = store.get(job.id);
|
|
183
|
-
if (updated) this.emit({ type: "updated", job: updated });
|
|
184
|
-
}, delay);
|
|
185
|
-
this.intervals.set(job.id, t);
|
|
186
|
-
} else {
|
|
187
|
-
// Past timestamp — disable, mark error, never fire
|
|
188
|
-
store.update(job.id, { enabled: false, lastStatus: "error" });
|
|
189
|
-
this.emit({ type: "error", jobId: job.id, error: `Scheduled time ${job.schedule} is in the past` });
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
const cron = new Cron(job.schedule, () => this.executeJob(job.id));
|
|
193
|
-
this.jobs.set(job.id, cron);
|
|
194
|
-
}
|
|
195
|
-
} catch (err) {
|
|
196
|
-
this.emit({ type: "error", jobId: job.id, error: err instanceof Error ? err.message : String(err) });
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private unscheduleJob(id: string): void {
|
|
201
|
-
const cron = this.jobs.get(id);
|
|
202
|
-
if (cron) {
|
|
203
|
-
cron.stop();
|
|
204
|
-
this.jobs.delete(id);
|
|
205
|
-
}
|
|
206
|
-
const t = this.intervals.get(id);
|
|
207
|
-
if (t) {
|
|
208
|
-
clearTimeout(t);
|
|
209
|
-
clearInterval(t);
|
|
210
|
-
this.intervals.delete(id);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Fire a job: persist running state, spawn (bypassing the concurrency
|
|
216
|
-
* queue), persist completion. Fire-and-forget: the timer tick returns
|
|
217
|
-
* immediately so other jobs keep firing.
|
|
218
|
-
*/
|
|
219
|
-
private executeJob(id: string): void {
|
|
220
|
-
const store = this.store;
|
|
221
|
-
const pi = this.pi;
|
|
222
|
-
const ctx = this.ctx;
|
|
223
|
-
const manager = this.manager;
|
|
224
|
-
if (!store || !pi || !ctx || !manager) return;
|
|
225
|
-
const job = store.get(id);
|
|
226
|
-
if (!job?.enabled) return;
|
|
227
|
-
|
|
228
|
-
store.update(id, { lastStatus: "running" });
|
|
229
|
-
|
|
230
|
-
// Resolve model at fire time — registry contents may have changed since the
|
|
231
|
-
// job was created (auth added/removed). Fall back silently to spawn-default
|
|
232
|
-
// if resolution fails; the spawn path handles undefined model gracefully.
|
|
233
|
-
let resolvedModel: any | undefined;
|
|
234
|
-
if (job.model) {
|
|
235
|
-
const r = resolveModel(job.model, ctx.modelRegistry);
|
|
236
|
-
if (typeof r !== "string") resolvedModel = r;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
let agentId: string;
|
|
240
|
-
try {
|
|
241
|
-
agentId = manager.spawn(pi, ctx, job.subagent_type, job.prompt, {
|
|
242
|
-
description: job.description,
|
|
243
|
-
isBackground: true,
|
|
244
|
-
bypassQueue: true,
|
|
245
|
-
model: resolvedModel,
|
|
246
|
-
maxTurns: job.max_turns,
|
|
247
|
-
isolated: job.isolated,
|
|
248
|
-
thinkingLevel: job.thinking,
|
|
249
|
-
isolation: job.isolation,
|
|
250
|
-
});
|
|
251
|
-
} catch (err) {
|
|
252
|
-
const error = err instanceof Error ? err.message : String(err);
|
|
253
|
-
store.update(id, { lastRun: new Date().toISOString(), lastStatus: "error" });
|
|
254
|
-
this.emit({ type: "error", jobId: id, error });
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
this.emit({ type: "fired", jobId: id, agentId, name: job.name });
|
|
259
|
-
|
|
260
|
-
const record = manager.getRecord(agentId);
|
|
261
|
-
const finalize = (status: "success" | "error") => {
|
|
262
|
-
const next = this.getNextRun(id);
|
|
263
|
-
const current = store.get(id);
|
|
264
|
-
store.update(id, {
|
|
265
|
-
lastRun: new Date().toISOString(),
|
|
266
|
-
lastStatus: status,
|
|
267
|
-
runCount: (current?.runCount ?? 0) + 1,
|
|
268
|
-
nextRun: next,
|
|
269
|
-
});
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
// AgentManager's promise resolves either way (its .catch returns ""), so we
|
|
273
|
-
// can't infer success/failure from the promise — read record.status instead.
|
|
274
|
-
// Terminal states: completed/steered = success; error/aborted/stopped = error.
|
|
275
|
-
if (record?.promise) {
|
|
276
|
-
record.promise
|
|
277
|
-
.then(() => {
|
|
278
|
-
const r = manager.getRecord(agentId);
|
|
279
|
-
const failed = r?.status === "error" || r?.status === "aborted" || r?.status === "stopped";
|
|
280
|
-
finalize(failed ? "error" : "success");
|
|
281
|
-
})
|
|
282
|
-
.catch(() => finalize("error"));
|
|
283
|
-
} else {
|
|
284
|
-
// Spawn returned without a promise (defensive — bypassQueue path always sets one).
|
|
285
|
-
finalize("success");
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
private emit(event: ScheduleChangeEvent): void {
|
|
290
|
-
if (this.pi) this.pi.events.emit("subagents:scheduled", event);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
private requireStore(): ScheduleStore {
|
|
294
|
-
if (!this.store) throw new Error("Scheduler not started — no active session.");
|
|
295
|
-
return this.store;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// ── Format detection / parsers (statics — pure) ──────────────────────
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Sniff a schedule string and tag its type. Throws on invalid input.
|
|
302
|
-
* Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
|
|
303
|
-
* relative requires the leading "+" to disambiguate.
|
|
304
|
-
*/
|
|
305
|
-
static detectSchedule(s: string): { type: "cron" | "once" | "interval"; intervalMs?: number; normalized: string } {
|
|
306
|
-
const trimmed = s.trim();
|
|
307
|
-
// "+10m" — relative one-shot
|
|
308
|
-
const rel = SubagentScheduler.parseRelativeTime(trimmed);
|
|
309
|
-
if (rel !== null) return { type: "once", normalized: rel };
|
|
310
|
-
// "5m" — interval
|
|
311
|
-
const ivl = SubagentScheduler.parseInterval(trimmed);
|
|
312
|
-
if (ivl !== null) return { type: "interval", intervalMs: ivl, normalized: trimmed };
|
|
313
|
-
// ISO timestamp — one-shot. Reject past timestamps upfront so we never
|
|
314
|
-
// create a dead-on-arrival record (scheduleJob's safety net still catches
|
|
315
|
-
// micro-races from `+0s`-style relatives).
|
|
316
|
-
if (/^\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
|
|
317
|
-
const d = new Date(trimmed);
|
|
318
|
-
if (!Number.isNaN(d.getTime())) {
|
|
319
|
-
if (d.getTime() <= Date.now()) {
|
|
320
|
-
throw new Error(`Scheduled time ${d.toISOString()} is in the past.`);
|
|
321
|
-
}
|
|
322
|
-
return { type: "once", normalized: d.toISOString() };
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
// Cron — 6-field
|
|
326
|
-
const cronCheck = SubagentScheduler.validateCronExpression(trimmed);
|
|
327
|
-
if (cronCheck.valid) return { type: "cron", normalized: trimmed };
|
|
328
|
-
throw new Error(
|
|
329
|
-
`Invalid schedule "${s}". Use 6-field cron (e.g. "0 0 9 * * 1" — 9am every Monday), interval ("5m"/"1h"), or one-shot ("+10m" / ISO).`
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** 6-field cron — 'second minute hour dom month dow'. */
|
|
334
|
-
static validateCronExpression(expr: string): { valid: boolean; error?: string } {
|
|
335
|
-
const fields = expr.trim().split(/\s+/);
|
|
336
|
-
if (fields.length !== 6) {
|
|
337
|
-
return {
|
|
338
|
-
valid: false,
|
|
339
|
-
error: `Cron must have 6 fields (second minute hour dom month dow), got ${fields.length}. Example: "0 0 9 * * 1" for 9am every Monday.`,
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
try {
|
|
343
|
-
// Croner validates by construction.
|
|
344
|
-
new Cron(expr, () => {});
|
|
345
|
-
return { valid: true };
|
|
346
|
-
} catch (e) {
|
|
347
|
-
return { valid: false, error: e instanceof Error ? e.message : "Invalid cron expression" };
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
|
|
352
|
-
static parseRelativeTime(s: string): string | null {
|
|
353
|
-
const m = s.match(/^\+(\d+)(s|m|h|d)$/);
|
|
354
|
-
if (!m) return null;
|
|
355
|
-
const ms = parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
|
|
356
|
-
return new Date(Date.now() + ms).toISOString();
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/** "10s"/"5m"/"1h"/"2d" → milliseconds. */
|
|
360
|
-
static parseInterval(s: string): number | null {
|
|
361
|
-
const m = s.match(/^(\d+)(s|m|h|d)$/);
|
|
362
|
-
if (!m) return null;
|
|
363
|
-
return parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
|
|
364
|
-
}
|
|
365
|
-
}
|
package/src/ui/schedule-menu.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* schedule-menu.ts — `/agents → Scheduled jobs` submenu.
|
|
3
|
-
*
|
|
4
|
-
* Minimal v1 surface: list scheduled jobs, select one to inspect details +
|
|
5
|
-
* confirm cancellation. No create wizard (the `Agent` tool's `schedule` param
|
|
6
|
-
* is the canonical creation path), no toggle/cleanup (cancel is enough for
|
|
7
|
-
* "I scheduled something dumb, get rid of it"). Add management surfaces here
|
|
8
|
-
* if real demand emerges.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
12
|
-
import type { SubagentScheduler } from "../schedule.js";
|
|
13
|
-
import type { ScheduledSubagent } from "../types.js";
|
|
14
|
-
|
|
15
|
-
/** Format an ISO timestamp as relative time ("in 4h", "2d ago", "—"). */
|
|
16
|
-
function relTime(iso: string | undefined, now = Date.now()): string {
|
|
17
|
-
if (!iso) return "—";
|
|
18
|
-
const t = new Date(iso).getTime();
|
|
19
|
-
if (Number.isNaN(t)) return "—";
|
|
20
|
-
const diff = t - now;
|
|
21
|
-
const abs = Math.abs(diff);
|
|
22
|
-
const future = diff > 0;
|
|
23
|
-
if (abs < 60_000) return future ? "in <1m" : "<1m ago";
|
|
24
|
-
const m = Math.round(abs / 60_000);
|
|
25
|
-
if (m < 60) return future ? `in ${m}m` : `${m}m ago`;
|
|
26
|
-
const h = Math.round(abs / 3_600_000);
|
|
27
|
-
if (h < 24) return future ? `in ${h}h` : `${h}h ago`;
|
|
28
|
-
const d = Math.round(abs / 86_400_000);
|
|
29
|
-
return future ? `in ${d}d` : `${d}d ago`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** One-line status icon. */
|
|
33
|
-
function statusIcon(j: ScheduledSubagent): string {
|
|
34
|
-
if (!j.enabled) return "✗";
|
|
35
|
-
if (j.lastStatus === "error") return "!";
|
|
36
|
-
if (j.lastStatus === "running") return "⋯";
|
|
37
|
-
return "✓";
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Compact selectable row — name, schedule, agent type, next/last run, count. */
|
|
41
|
-
function formatJob(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
|
|
42
|
-
const next = scheduler.getNextRun(j.id);
|
|
43
|
-
return [
|
|
44
|
-
statusIcon(j),
|
|
45
|
-
j.name.padEnd(18).slice(0, 18),
|
|
46
|
-
j.schedule.padEnd(14).slice(0, 14),
|
|
47
|
-
`[${j.subagent_type}]`,
|
|
48
|
-
`next ${relTime(next)}`,
|
|
49
|
-
`last ${relTime(j.lastRun)}`,
|
|
50
|
-
`runs ${j.runCount}`,
|
|
51
|
-
].join(" ");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Multi-line details block for the cancel confirm. */
|
|
55
|
-
function formatDetails(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
|
|
56
|
-
const next = scheduler.getNextRun(j.id) ?? "—";
|
|
57
|
-
return [
|
|
58
|
-
`name: ${j.name}`,
|
|
59
|
-
`schedule: ${j.schedule} (${j.scheduleType})`,
|
|
60
|
-
`agent: ${j.subagent_type}`,
|
|
61
|
-
`prompt: ${j.prompt.slice(0, 200)}${j.prompt.length > 200 ? "…" : ""}`,
|
|
62
|
-
`created: ${j.createdAt}`,
|
|
63
|
-
`last run: ${j.lastRun ?? "—"} (${j.lastStatus ?? "—"})`,
|
|
64
|
-
`next run: ${next}`,
|
|
65
|
-
`runs: ${j.runCount}`,
|
|
66
|
-
].join("\n");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* List scheduled jobs; selecting one opens a cancel-confirm with details.
|
|
71
|
-
* Returns when the user backs out or after a cancellation.
|
|
72
|
-
*/
|
|
73
|
-
export async function showSchedulesMenu(
|
|
74
|
-
ctx: ExtensionCommandContext,
|
|
75
|
-
scheduler: SubagentScheduler,
|
|
76
|
-
): Promise<void> {
|
|
77
|
-
if (!scheduler.isActive()) {
|
|
78
|
-
ctx.ui.notify("Scheduler is not active in this session.", "warning");
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const jobs = scheduler.list();
|
|
83
|
-
if (jobs.length === 0) {
|
|
84
|
-
ctx.ui.notify("No scheduled jobs.", "info");
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const labels = jobs.map(j => formatJob(j, scheduler));
|
|
89
|
-
const choice = await ctx.ui.select(
|
|
90
|
-
`Scheduled jobs (${jobs.length}) — select to cancel`,
|
|
91
|
-
labels,
|
|
92
|
-
);
|
|
93
|
-
if (!choice) return;
|
|
94
|
-
|
|
95
|
-
const idx = labels.indexOf(choice);
|
|
96
|
-
if (idx < 0) return;
|
|
97
|
-
const job = jobs[idx];
|
|
98
|
-
|
|
99
|
-
const ok = await ctx.ui.confirm(`Cancel "${job.name}"?`, formatDetails(job, scheduler));
|
|
100
|
-
if (!ok) return;
|
|
101
|
-
|
|
102
|
-
scheduler.removeJob(job.id);
|
|
103
|
-
ctx.ui.notify(`Cancelled "${job.name}".`, "info");
|
|
104
|
-
}
|