@ymy88/pi-subagents 0.0.0-stage → 0.24.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.
Files changed (196) hide show
  1. package/CHANGELOG.md +904 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1170 -2
  5. package/SECURITY.md +95 -0
  6. package/dist/abortable.d.ts +12 -0
  7. package/dist/abortable.js +42 -0
  8. package/dist/agent-color.d.ts +35 -0
  9. package/dist/agent-color.js +123 -0
  10. package/dist/agent-file-toggle.d.ts +133 -0
  11. package/dist/agent-file-toggle.js +287 -0
  12. package/dist/agent-manager.d.ts +552 -0
  13. package/dist/agent-manager.js +1466 -0
  14. package/dist/agent-record-entry.d.ts +96 -0
  15. package/dist/agent-record-entry.js +121 -0
  16. package/dist/agent-runner.d.ts +310 -0
  17. package/dist/agent-runner.js +1195 -0
  18. package/dist/agent-source-badge.d.ts +50 -0
  19. package/dist/agent-source-badge.js +74 -0
  20. package/dist/agent-types.d.ts +119 -0
  21. package/dist/agent-types.js +286 -0
  22. package/dist/child-context.d.ts +2 -0
  23. package/dist/child-context.js +12 -0
  24. package/dist/completion-outbox.d.ts +63 -0
  25. package/dist/completion-outbox.js +104 -0
  26. package/dist/context.d.ts +12 -0
  27. package/dist/context.js +56 -0
  28. package/dist/cross-extension-rpc.d.ts +72 -0
  29. package/dist/cross-extension-rpc.js +153 -0
  30. package/dist/custom-agents.d.ts +77 -0
  31. package/dist/custom-agents.js +396 -0
  32. package/dist/default-agents.d.ts +7 -0
  33. package/dist/default-agents.js +121 -0
  34. package/dist/enabled-models.d.ts +49 -0
  35. package/dist/enabled-models.js +145 -0
  36. package/dist/env.d.ts +6 -0
  37. package/dist/env.js +28 -0
  38. package/dist/group-join.d.ts +32 -0
  39. package/dist/group-join.js +116 -0
  40. package/dist/index.d.ts +50 -0
  41. package/dist/index.js +4111 -0
  42. package/dist/invocation-config.d.ts +108 -0
  43. package/dist/invocation-config.js +83 -0
  44. package/dist/memory.d.ts +53 -0
  45. package/dist/memory.js +165 -0
  46. package/dist/mention-clone.d.ts +79 -0
  47. package/dist/mention-clone.js +155 -0
  48. package/dist/mention.d.ts +81 -0
  49. package/dist/mention.js +131 -0
  50. package/dist/model-resolver.d.ts +36 -0
  51. package/dist/model-resolver.js +95 -0
  52. package/dist/model-scope.d.ts +49 -0
  53. package/dist/model-scope.js +48 -0
  54. package/dist/nested-tools.d.ts +57 -0
  55. package/dist/nested-tools.js +303 -0
  56. package/dist/output-file.d.ts +43 -0
  57. package/dist/output-file.js +153 -0
  58. package/dist/package-resources.d.ts +240 -0
  59. package/dist/package-resources.js +519 -0
  60. package/dist/prompts.d.ts +76 -0
  61. package/dist/prompts.js +121 -0
  62. package/dist/schedule-store.d.ts +38 -0
  63. package/dist/schedule-store.js +155 -0
  64. package/dist/schedule.d.ts +109 -0
  65. package/dist/schedule.js +359 -0
  66. package/dist/settings.d.ts +398 -0
  67. package/dist/settings.js +282 -0
  68. package/dist/skill-loader.d.ts +24 -0
  69. package/dist/skill-loader.js +93 -0
  70. package/dist/status-note.d.ts +61 -0
  71. package/dist/status-note.js +85 -0
  72. package/dist/structured-output.d.ts +61 -0
  73. package/dist/structured-output.js +112 -0
  74. package/dist/types.d.ts +391 -0
  75. package/dist/types.js +5 -0
  76. package/dist/ui/agent-mention.d.ts +82 -0
  77. package/dist/ui/agent-mention.js +187 -0
  78. package/dist/ui/agent-widget.d.ts +219 -0
  79. package/dist/ui/agent-widget.js +585 -0
  80. package/dist/ui/conversation-viewer.d.ts +177 -0
  81. package/dist/ui/conversation-viewer.js +905 -0
  82. package/dist/ui/fleet-list.d.ts +211 -0
  83. package/dist/ui/fleet-list.js +489 -0
  84. package/dist/ui/schedule-menu.d.ts +16 -0
  85. package/dist/ui/schedule-menu.js +94 -0
  86. package/dist/ui/select-item.d.ts +27 -0
  87. package/dist/ui/select-item.js +34 -0
  88. package/dist/ui/viewer-keys.d.ts +22 -0
  89. package/dist/ui/viewer-keys.js +18 -0
  90. package/dist/ui/workflow-card.d.ts +175 -0
  91. package/dist/ui/workflow-card.js +332 -0
  92. package/dist/ui/workflow-dialog.d.ts +305 -0
  93. package/dist/ui/workflow-dialog.js +843 -0
  94. package/dist/ui/workflow-menu.d.ts +60 -0
  95. package/dist/ui/workflow-menu.js +147 -0
  96. package/dist/usage.d.ts +135 -0
  97. package/dist/usage.js +120 -0
  98. package/dist/workflow/collisions.d.ts +95 -0
  99. package/dist/workflow/collisions.js +88 -0
  100. package/dist/workflow/entry.d.ts +32 -0
  101. package/dist/workflow/entry.js +29 -0
  102. package/dist/workflow/host.d.ts +62 -0
  103. package/dist/workflow/host.js +419 -0
  104. package/dist/workflow/journal.d.ts +97 -0
  105. package/dist/workflow/journal.js +120 -0
  106. package/dist/workflow/json-schema.d.ts +52 -0
  107. package/dist/workflow/json-schema.js +112 -0
  108. package/dist/workflow/meta.d.ts +67 -0
  109. package/dist/workflow/meta.js +317 -0
  110. package/dist/workflow/progress.d.ts +224 -0
  111. package/dist/workflow/progress.js +361 -0
  112. package/dist/workflow/runtime.d.ts +334 -0
  113. package/dist/workflow/runtime.js +830 -0
  114. package/dist/workflow/saved.d.ts +102 -0
  115. package/dist/workflow/saved.js +247 -0
  116. package/dist/workflow/task.d.ts +136 -0
  117. package/dist/workflow/task.js +207 -0
  118. package/dist/workflow/tool-description.d.ts +38 -0
  119. package/dist/workflow/tool-description.js +199 -0
  120. package/dist/workflow/worker-source.d.ts +47 -0
  121. package/dist/workflow/worker-source.js +778 -0
  122. package/dist/worktree.d.ts +52 -0
  123. package/dist/worktree.js +164 -0
  124. package/dist/xml.d.ts +10 -0
  125. package/dist/xml.js +12 -0
  126. package/docs/packages.md +211 -0
  127. package/docs/rpc.md +192 -0
  128. package/docs/workflows.md +447 -0
  129. package/examples/agent-tool-description.md +42 -0
  130. package/examples/workflows/compose.js +51 -0
  131. package/examples/workflows/fan-out-audit.js +47 -0
  132. package/examples/workflows/gated-fix.js +60 -0
  133. package/examples/workflows/lib/count-child.js +27 -0
  134. package/examples/workflows/review-panel.js +63 -0
  135. package/examples/workflows/structured-findings.js +78 -0
  136. package/package.json +74 -4
  137. package/src/abortable.ts +43 -0
  138. package/src/agent-color.ts +161 -0
  139. package/src/agent-file-toggle.ts +297 -0
  140. package/src/agent-manager.ts +1735 -0
  141. package/src/agent-record-entry.ts +153 -0
  142. package/src/agent-runner.ts +1468 -0
  143. package/src/agent-source-badge.ts +75 -0
  144. package/src/agent-types.ts +346 -0
  145. package/src/child-context.ts +15 -0
  146. package/src/completion-outbox.ts +116 -0
  147. package/src/context.ts +58 -0
  148. package/src/cross-extension-rpc.ts +221 -0
  149. package/src/custom-agents.ts +452 -0
  150. package/src/default-agents.ts +125 -0
  151. package/src/enabled-models.ts +180 -0
  152. package/src/env.ts +33 -0
  153. package/src/group-join.ts +141 -0
  154. package/src/index.ts +4433 -0
  155. package/src/invocation-config.ts +155 -0
  156. package/src/memory.ts +179 -0
  157. package/src/mention-clone.ts +195 -0
  158. package/src/mention.ts +141 -0
  159. package/src/model-resolver.ts +118 -0
  160. package/src/model-scope.ts +70 -0
  161. package/src/nested-tools.ts +426 -0
  162. package/src/output-file.ts +164 -0
  163. package/src/package-resources.ts +652 -0
  164. package/src/prompts.ts +171 -0
  165. package/src/schedule-store.ts +153 -0
  166. package/src/schedule.ts +386 -0
  167. package/src/settings.ts +650 -0
  168. package/src/skill-loader.ts +102 -0
  169. package/src/status-note.ts +90 -0
  170. package/src/structured-output.ts +130 -0
  171. package/src/types.ts +404 -0
  172. package/src/ui/agent-mention.ts +216 -0
  173. package/src/ui/agent-widget.ts +661 -0
  174. package/src/ui/conversation-viewer.ts +928 -0
  175. package/src/ui/fleet-list.ts +559 -0
  176. package/src/ui/schedule-menu.ts +105 -0
  177. package/src/ui/select-item.ts +45 -0
  178. package/src/ui/viewer-keys.ts +43 -0
  179. package/src/ui/workflow-card.ts +470 -0
  180. package/src/ui/workflow-dialog.ts +1115 -0
  181. package/src/ui/workflow-menu.ts +193 -0
  182. package/src/usage.ts +167 -0
  183. package/src/workflow/collisions.ts +123 -0
  184. package/src/workflow/entry.ts +47 -0
  185. package/src/workflow/host.ts +467 -0
  186. package/src/workflow/journal.ts +164 -0
  187. package/src/workflow/json-schema.ts +129 -0
  188. package/src/workflow/meta.ts +325 -0
  189. package/src/workflow/progress.ts +551 -0
  190. package/src/workflow/runtime.ts +1219 -0
  191. package/src/workflow/saved.ts +259 -0
  192. package/src/workflow/task.ts +302 -0
  193. package/src/workflow/tool-description.ts +200 -0
  194. package/src/workflow/worker-source.ts +781 -0
  195. package/src/worktree.ts +205 -0
  196. package/src/xml.ts +13 -0
@@ -0,0 +1,359 @@
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` path. No new delivery code.
16
+ */
17
+ import { Cron } from "croner";
18
+ import { nanoid } from "nanoid";
19
+ import { normalizeMaxTurns } from "./agent-runner.js";
20
+ import { resolveSpawnType } from "./agent-types.js";
21
+ import { resolveModel } from "./model-resolver.js";
22
+ export class SubagentScheduler {
23
+ jobs = new Map();
24
+ intervals = new Map();
25
+ store;
26
+ pi;
27
+ ctx;
28
+ manager;
29
+ /** Start the scheduler: bind to a session's store and arm enabled jobs. */
30
+ start(pi, ctx, manager, store) {
31
+ this.pi = pi;
32
+ this.ctx = ctx;
33
+ this.manager = manager;
34
+ this.store = store;
35
+ for (const job of store.list()) {
36
+ if (job.enabled)
37
+ this.scheduleJob(job);
38
+ }
39
+ }
40
+ /** Stop all timers; drop refs. Safe to call repeatedly. */
41
+ stop() {
42
+ for (const cron of this.jobs.values())
43
+ cron.stop();
44
+ this.jobs.clear();
45
+ for (const t of this.intervals.values())
46
+ clearTimeout(t);
47
+ this.intervals.clear();
48
+ this.store = undefined;
49
+ this.pi = undefined;
50
+ this.ctx = undefined;
51
+ this.manager = undefined;
52
+ }
53
+ /** True if start() has bound a store and the scheduler is active. */
54
+ isActive() {
55
+ return this.store !== undefined;
56
+ }
57
+ list() {
58
+ return this.store?.list() ?? [];
59
+ }
60
+ /**
61
+ * Build a `ScheduledSubagent` from user input. Validates the schedule
62
+ * format and tags `scheduleType`. Throws on invalid input.
63
+ */
64
+ buildJob(input) {
65
+ const detected = SubagentScheduler.detectSchedule(input.schedule);
66
+ return {
67
+ id: nanoid(10),
68
+ name: input.name,
69
+ description: input.description,
70
+ schedule: detected.normalized,
71
+ scheduleType: detected.type,
72
+ intervalMs: detected.intervalMs,
73
+ subagent_type: input.subagent_type,
74
+ prompt: input.prompt,
75
+ model: input.model,
76
+ thinking: input.thinking,
77
+ max_turns: input.max_turns,
78
+ isolated: input.isolated,
79
+ isolation: input.isolation,
80
+ enabled: true,
81
+ createdAt: new Date().toISOString(),
82
+ runCount: 0,
83
+ };
84
+ }
85
+ /** Add a job, persist, and arm if enabled. Returns the stored job. */
86
+ addJob(input) {
87
+ const store = this.requireStore();
88
+ if (store.hasName(input.name)) {
89
+ throw new Error(`A scheduled job named "${input.name}" already exists.`);
90
+ }
91
+ const job = this.buildJob(input);
92
+ store.add(job);
93
+ if (job.enabled)
94
+ this.scheduleJob(job);
95
+ this.emit({ type: "added", job });
96
+ return job;
97
+ }
98
+ removeJob(id) {
99
+ const store = this.requireStore();
100
+ if (!store.get(id))
101
+ return false;
102
+ this.unscheduleJob(id);
103
+ const ok = store.remove(id);
104
+ if (ok)
105
+ this.emit({ type: "removed", jobId: id });
106
+ return ok;
107
+ }
108
+ /** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
109
+ updateJob(id, patch) {
110
+ const store = this.requireStore();
111
+ const updated = store.update(id, patch);
112
+ if (!updated)
113
+ return undefined;
114
+ this.unscheduleJob(id);
115
+ if (updated.enabled)
116
+ this.scheduleJob(updated);
117
+ this.emit({ type: "updated", job: updated });
118
+ return updated;
119
+ }
120
+ /** Next-run time as ISO, or undefined if not currently armed. */
121
+ getNextRun(jobId) {
122
+ const cron = this.jobs.get(jobId);
123
+ if (cron)
124
+ return cron.nextRun()?.toISOString();
125
+ const job = this.store?.get(jobId);
126
+ if (!job?.enabled)
127
+ return undefined;
128
+ if (job.scheduleType === "once")
129
+ return job.schedule;
130
+ if (job.scheduleType === "interval" && job.intervalMs) {
131
+ // Before the first fire there's no `lastRun`, so fall back to "now" —
132
+ // accurate at create time (setInterval was just armed) and within
133
+ // intervalMs of correct in any pre-first-fire view.
134
+ const base = job.lastRun ? new Date(job.lastRun).getTime() : Date.now();
135
+ return new Date(base + job.intervalMs).toISOString();
136
+ }
137
+ return undefined;
138
+ }
139
+ // ── Scheduling primitives ────────────────────────────────────────────
140
+ scheduleJob(job) {
141
+ const store = this.store;
142
+ if (!store)
143
+ return;
144
+ try {
145
+ if (job.scheduleType === "interval" && job.intervalMs) {
146
+ const t = setInterval(() => this.executeJob(job.id), job.intervalMs);
147
+ this.intervals.set(job.id, t);
148
+ }
149
+ else if (job.scheduleType === "once") {
150
+ const target = new Date(job.schedule).getTime();
151
+ const delay = target - Date.now();
152
+ if (delay > 0) {
153
+ const t = setTimeout(() => {
154
+ this.executeJob(job.id);
155
+ // Auto-disable one-shots after they fire (mirrors pi-cron-schedule)
156
+ store.update(job.id, { enabled: false });
157
+ const updated = store.get(job.id);
158
+ if (updated)
159
+ this.emit({ type: "updated", job: updated });
160
+ }, delay);
161
+ this.intervals.set(job.id, t);
162
+ }
163
+ else {
164
+ // Past timestamp — disable, mark error, never fire
165
+ store.update(job.id, { enabled: false, lastStatus: "error" });
166
+ this.emit({ type: "error", jobId: job.id, error: `Scheduled time ${job.schedule} is in the past` });
167
+ }
168
+ }
169
+ else {
170
+ const cron = new Cron(job.schedule, () => this.executeJob(job.id));
171
+ this.jobs.set(job.id, cron);
172
+ }
173
+ }
174
+ catch (err) {
175
+ this.emit({ type: "error", jobId: job.id, error: err instanceof Error ? err.message : String(err) });
176
+ }
177
+ }
178
+ unscheduleJob(id) {
179
+ const cron = this.jobs.get(id);
180
+ if (cron) {
181
+ cron.stop();
182
+ this.jobs.delete(id);
183
+ }
184
+ const t = this.intervals.get(id);
185
+ if (t) {
186
+ clearTimeout(t);
187
+ clearInterval(t);
188
+ this.intervals.delete(id);
189
+ }
190
+ }
191
+ /**
192
+ * Fire a job: persist running state, spawn (bypassing the concurrency
193
+ * queue), persist completion. Fire-and-forget: the timer tick returns
194
+ * immediately so other jobs keep firing.
195
+ */
196
+ executeJob(id) {
197
+ const store = this.store;
198
+ const pi = this.pi;
199
+ const ctx = this.ctx;
200
+ const manager = this.manager;
201
+ if (!store || !pi || !ctx || !manager)
202
+ return;
203
+ const job = store.get(id);
204
+ if (!job?.enabled)
205
+ return;
206
+ store.update(id, { lastStatus: "running" });
207
+ // Resolve model at fire time — registry contents may have changed since the
208
+ // job was created (auth added/removed). Fall back silently to spawn-default
209
+ // if resolution fails; the spawn path handles undefined model gracefully.
210
+ let resolvedModel;
211
+ if (job.model) {
212
+ const r = resolveModel(job.model, ctx.modelRegistry);
213
+ if (typeof r !== "string")
214
+ resolvedModel = r;
215
+ }
216
+ let agentId;
217
+ try {
218
+ // Re-resolve at fire time against the registry as it stands. This does not
219
+ // reload from disk (the scheduler has no reason to rebuild process-global
220
+ // state from a timer), so it catches changes that went through /agents or
221
+ // an Agent call — not a file deleted directly from a shell. The catch below turns
222
+ // this into lastStatus: "error" plus an error event, like any other
223
+ // fire-time failure.
224
+ const dispatch = resolveSpawnType(job.subagent_type);
225
+ if (!dispatch.ok)
226
+ throw new Error(dispatch.message);
227
+ agentId = manager.spawn(pi, ctx, dispatch.type, job.prompt, {
228
+ description: job.description,
229
+ isBackground: true,
230
+ bypassQueue: true,
231
+ model: resolvedModel,
232
+ maxTurns: job.max_turns,
233
+ isolated: job.isolated,
234
+ thinkingLevel: job.thinking,
235
+ isolation: job.isolation,
236
+ // A scheduled run has no tool call to build this, so without it the
237
+ // conversation viewer shows nothing about how the job was configured.
238
+ // The model is left out on purpose: agent-manager fills in the effective
239
+ // one when the session reports it, and naming the pre-session pick here
240
+ // would only be right until then.
241
+ invocation: {
242
+ thinking: job.thinking,
243
+ // Normalized like the Agent tool's own snapshot: `0` means unlimited,
244
+ // and rendering it as "max turns: 0" would read as a limit of none.
245
+ maxTurns: normalizeMaxTurns(job.max_turns),
246
+ isolated: job.isolated,
247
+ runInBackground: true,
248
+ isolation: job.isolation,
249
+ },
250
+ });
251
+ }
252
+ catch (err) {
253
+ const error = err instanceof Error ? err.message : String(err);
254
+ store.update(id, { lastRun: new Date().toISOString(), lastStatus: "error" });
255
+ this.emit({ type: "error", jobId: id, error });
256
+ return;
257
+ }
258
+ this.emit({ type: "fired", jobId: id, agentId, name: job.name });
259
+ const finalize = (status) => {
260
+ const next = this.getNextRun(id);
261
+ const current = store.get(id);
262
+ store.update(id, {
263
+ lastRun: new Date().toISOString(),
264
+ lastStatus: status,
265
+ runCount: (current?.runCount ?? 0) + 1,
266
+ nextRun: next,
267
+ });
268
+ };
269
+ // AgentManager's promise resolves either way (its .catch returns ""), so we
270
+ // can't infer success/failure from the promise — read record.status instead.
271
+ // Terminal states: completed/steered = success; error/aborted/stopped = error.
272
+ // awaitStartup first: with isolation: "worktree" the run promise only exists
273
+ // once the repo copy is made, and a failed copy rejects here.
274
+ manager.awaitStartup(agentId)
275
+ .then(() => manager.getRecord(agentId)?.promise)
276
+ .then(() => {
277
+ const r = manager.getRecord(agentId);
278
+ const failed = r?.status === "error" || r?.status === "aborted" || r?.status === "stopped";
279
+ finalize(failed ? "error" : "success");
280
+ })
281
+ .catch(() => finalize("error"));
282
+ }
283
+ emit(event) {
284
+ if (this.pi)
285
+ this.pi.events.emit("subagents:scheduled", event);
286
+ }
287
+ requireStore() {
288
+ if (!this.store)
289
+ throw new Error("Scheduler not started — no active session.");
290
+ return this.store;
291
+ }
292
+ // ── Format detection / parsers (statics — pure) ──────────────────────
293
+ /**
294
+ * Sniff a schedule string and tag its type. Throws on invalid input.
295
+ * Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
296
+ * relative requires the leading "+" to disambiguate.
297
+ */
298
+ static detectSchedule(s) {
299
+ const trimmed = s.trim();
300
+ // "+10m" — relative one-shot
301
+ const rel = SubagentScheduler.parseRelativeTime(trimmed);
302
+ if (rel !== null)
303
+ return { type: "once", normalized: rel };
304
+ // "5m" — interval
305
+ const ivl = SubagentScheduler.parseInterval(trimmed);
306
+ if (ivl !== null)
307
+ return { type: "interval", intervalMs: ivl, normalized: trimmed };
308
+ // ISO timestamp — one-shot. Reject past timestamps upfront so we never
309
+ // create a dead-on-arrival record (scheduleJob's safety net still catches
310
+ // micro-races from `+0s`-style relatives).
311
+ if (/^\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
312
+ const d = new Date(trimmed);
313
+ if (!Number.isNaN(d.getTime())) {
314
+ if (d.getTime() <= Date.now()) {
315
+ throw new Error(`Scheduled time ${d.toISOString()} is in the past.`);
316
+ }
317
+ return { type: "once", normalized: d.toISOString() };
318
+ }
319
+ }
320
+ // Cron — 6-field
321
+ const cronCheck = SubagentScheduler.validateCronExpression(trimmed);
322
+ if (cronCheck.valid)
323
+ return { type: "cron", normalized: trimmed };
324
+ throw new Error(`Invalid schedule "${s}". Use 6-field cron (e.g. "0 0 9 * * 1" — 9am every Monday), interval ("5m"/"1h"), or one-shot ("+10m" / ISO).`);
325
+ }
326
+ /** 6-field cron — 'second minute hour dom month dow'. */
327
+ static validateCronExpression(expr) {
328
+ const fields = expr.trim().split(/\s+/);
329
+ if (fields.length !== 6) {
330
+ return {
331
+ valid: false,
332
+ error: `Cron must have 6 fields (second minute hour dom month dow), got ${fields.length}. Example: "0 0 9 * * 1" for 9am every Monday.`,
333
+ };
334
+ }
335
+ try {
336
+ // Croner validates by construction.
337
+ new Cron(expr, () => { });
338
+ return { valid: true };
339
+ }
340
+ catch (e) {
341
+ return { valid: false, error: e instanceof Error ? e.message : "Invalid cron expression" };
342
+ }
343
+ }
344
+ /** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
345
+ static parseRelativeTime(s) {
346
+ const m = s.match(/^\+(\d+)(s|m|h|d)$/);
347
+ if (!m)
348
+ return null;
349
+ const ms = parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2]];
350
+ return new Date(Date.now() + ms).toISOString();
351
+ }
352
+ /** "10s"/"5m"/"1h"/"2d" → milliseconds. */
353
+ static parseInterval(s) {
354
+ const m = s.match(/^(\d+)(s|m|h|d)$/);
355
+ if (!m)
356
+ return null;
357
+ return parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2]];
358
+ }
359
+ }