heyio 0.3.0 → 0.5.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.
@@ -11,6 +11,10 @@ import { abortOrchestrator } from "../copilot/orchestrator.js";
11
11
  import { getActiveTasks, getTask, listRecentTasks } from "../store/tasks.js";
12
12
  import { IO_VERSION } from "../paths.js";
13
13
  import { requireAuth } from "./auth.js";
14
+ import { listSchedules, getSchedule, deleteSchedule, setScheduleEnabled } from "../store/schedules.js";
15
+ import { listIoSchedules, getIoSchedule, deleteIoSchedule, setIoScheduleEnabled } from "../store/io-schedules.js";
16
+ import { runScheduleNow } from "../copilot/scheduler.js";
17
+ import { runIoScheduleNow } from "../copilot/io-scheduler.js";
14
18
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
15
19
  const WEB_DIST = path.resolve(__dirname, "../../web-dist");
16
20
  let messageHandler;
@@ -29,7 +33,7 @@ export async function startApiServer() {
29
33
  app.use(express.json());
30
34
  app.use((_req, res, next) => {
31
35
  res.setHeader("Access-Control-Allow-Origin", "*");
32
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
36
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
33
37
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
34
38
  next();
35
39
  });
@@ -226,6 +230,172 @@ export async function startApiServer() {
226
230
  res.status(500).json({ error: "Failed to cancel task" });
227
231
  }
228
232
  });
233
+ // Schedules endpoints
234
+ api.get("/schedules", (_req, res) => {
235
+ try {
236
+ const io = listIoSchedules();
237
+ const squads = listSchedules();
238
+ res.json({ io, squads });
239
+ }
240
+ catch (e) {
241
+ console.error("Error listing schedules:", e);
242
+ res.status(500).json({ error: "Failed to list schedules" });
243
+ }
244
+ });
245
+ // Squad schedule lifecycle
246
+ api.post("/schedules/squads/:id/pause", (req, res) => {
247
+ const id = Number(req.params.id);
248
+ if (Number.isNaN(id)) {
249
+ res.status(400).json({ error: "Invalid schedule id" });
250
+ return;
251
+ }
252
+ try {
253
+ const ok = setScheduleEnabled(id, false);
254
+ if (!ok) {
255
+ res.status(404).json({ error: "Squad schedule not found" });
256
+ return;
257
+ }
258
+ res.json({ ok: true, schedule: getSchedule(id) });
259
+ }
260
+ catch (e) {
261
+ console.error("Error pausing squad schedule:", e);
262
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
263
+ }
264
+ });
265
+ api.post("/schedules/squads/:id/resume", (req, res) => {
266
+ const id = Number(req.params.id);
267
+ if (Number.isNaN(id)) {
268
+ res.status(400).json({ error: "Invalid schedule id" });
269
+ return;
270
+ }
271
+ try {
272
+ const ok = setScheduleEnabled(id, true);
273
+ if (!ok) {
274
+ res.status(404).json({ error: "Squad schedule not found" });
275
+ return;
276
+ }
277
+ res.json({ ok: true, schedule: getSchedule(id) });
278
+ }
279
+ catch (e) {
280
+ console.error("Error resuming squad schedule:", e);
281
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
282
+ }
283
+ });
284
+ api.post("/schedules/squads/:id/run-now", async (req, res) => {
285
+ const id = Number(req.params.id);
286
+ if (Number.isNaN(id)) {
287
+ res.status(400).json({ error: "Invalid schedule id" });
288
+ return;
289
+ }
290
+ try {
291
+ const result = await runScheduleNow(id);
292
+ if (!result.ok) {
293
+ res.status(404).json({ error: result.error ?? "Squad schedule not found" });
294
+ return;
295
+ }
296
+ res.json({ ok: true });
297
+ }
298
+ catch (e) {
299
+ console.error("Error running squad schedule now:", e);
300
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
301
+ }
302
+ });
303
+ api.delete("/schedules/squads/:id", (req, res) => {
304
+ const id = Number(req.params.id);
305
+ if (Number.isNaN(id)) {
306
+ res.status(400).json({ error: "Invalid schedule id" });
307
+ return;
308
+ }
309
+ try {
310
+ const ok = deleteSchedule(id);
311
+ if (!ok) {
312
+ res.status(404).json({ error: "Squad schedule not found" });
313
+ return;
314
+ }
315
+ res.json({ ok: true });
316
+ }
317
+ catch (e) {
318
+ console.error("Error deleting squad schedule:", e);
319
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
320
+ }
321
+ });
322
+ // IO schedule lifecycle
323
+ api.post("/schedules/io/:id/pause", (req, res) => {
324
+ const id = Number(req.params.id);
325
+ if (Number.isNaN(id)) {
326
+ res.status(400).json({ error: "Invalid schedule id" });
327
+ return;
328
+ }
329
+ try {
330
+ const ok = setIoScheduleEnabled(id, false);
331
+ if (!ok) {
332
+ res.status(404).json({ error: "IO schedule not found" });
333
+ return;
334
+ }
335
+ res.json({ ok: true, schedule: getIoSchedule(id) });
336
+ }
337
+ catch (e) {
338
+ console.error("Error pausing IO schedule:", e);
339
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
340
+ }
341
+ });
342
+ api.post("/schedules/io/:id/resume", (req, res) => {
343
+ const id = Number(req.params.id);
344
+ if (Number.isNaN(id)) {
345
+ res.status(400).json({ error: "Invalid schedule id" });
346
+ return;
347
+ }
348
+ try {
349
+ const ok = setIoScheduleEnabled(id, true);
350
+ if (!ok) {
351
+ res.status(404).json({ error: "IO schedule not found" });
352
+ return;
353
+ }
354
+ res.json({ ok: true, schedule: getIoSchedule(id) });
355
+ }
356
+ catch (e) {
357
+ console.error("Error resuming IO schedule:", e);
358
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
359
+ }
360
+ });
361
+ api.post("/schedules/io/:id/run-now", async (req, res) => {
362
+ const id = Number(req.params.id);
363
+ if (Number.isNaN(id)) {
364
+ res.status(400).json({ error: "Invalid schedule id" });
365
+ return;
366
+ }
367
+ try {
368
+ const ok = await runIoScheduleNow(id);
369
+ if (!ok) {
370
+ res.status(404).json({ error: "IO schedule not found" });
371
+ return;
372
+ }
373
+ res.json({ ok: true });
374
+ }
375
+ catch (e) {
376
+ console.error("Error running IO schedule now:", e);
377
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
378
+ }
379
+ });
380
+ api.delete("/schedules/io/:id", (req, res) => {
381
+ const id = Number(req.params.id);
382
+ if (Number.isNaN(id)) {
383
+ res.status(400).json({ error: "Invalid schedule id" });
384
+ return;
385
+ }
386
+ try {
387
+ const ok = deleteIoSchedule(id);
388
+ if (!ok) {
389
+ res.status(404).json({ error: "IO schedule not found" });
390
+ return;
391
+ }
392
+ res.json({ ok: true });
393
+ }
394
+ catch (e) {
395
+ console.error("Error deleting IO schedule:", e);
396
+ res.status(500).json({ error: (e instanceof Error ? e.message : String(e)) });
397
+ }
398
+ });
229
399
  // Chat endpoints
230
400
  api.post("/message", async (req, res) => {
231
401
  const { text } = req.body;
@@ -240,21 +240,38 @@ async function getOrCreateAgentSession(squadSlug, agent, taskDescription) {
240
240
  const tierRank = { high: 3, medium: 2, low: 1 };
241
241
  const effectiveTier = tierRank[taskTier] >= tierRank[agentTier] ? taskTier : agentTier;
242
242
  const model = getModelForTier(effectiveTier);
243
- // If we have a cached session, check if the model matches; if not, destroy and recreate
243
+ // If we have a cached session, check if the model matches AND the agent
244
+ // hasn't been left in an error state by a previous task. If either is off,
245
+ // destroy and recreate. Reusing a session whose underlying SDK process has
246
+ // been throwing is how Panthro got "stuck" in error after the issue #42
247
+ // delegation timeout (issue #55).
244
248
  const existing = agentSessions.get(key);
245
249
  if (existing) {
246
- // Sessions don't expose their model, so track it separately
247
- const cachedModel = agentSessionModels.get(key);
248
- if (cachedModel === model)
249
- return existing;
250
- // Model changed — destroy old session for the upgraded model
251
- console.error(`[io] Agent ${agent.character_name}: upgrading model ${cachedModel} → ${model} for task complexity`);
252
- try {
253
- await existing.destroy();
250
+ const fresh = getSquadAgent(squadSlug, agent.character_name);
251
+ const persistedStatus = fresh?.status ?? agent.status;
252
+ if (persistedStatus === "error") {
253
+ console.error(`[io] Agent ${agent.character_name}: previous session ended in error — discarding cached session and recreating`);
254
+ try {
255
+ await existing.destroy();
256
+ }
257
+ catch { /* best-effort */ }
258
+ agentSessions.delete(key);
259
+ agentSessionModels.delete(key);
260
+ }
261
+ else {
262
+ // Sessions don't expose their model, so track it separately
263
+ const cachedModel = agentSessionModels.get(key);
264
+ if (cachedModel === model)
265
+ return existing;
266
+ // Model changed — destroy old session for the upgraded model
267
+ console.error(`[io] Agent ${agent.character_name}: upgrading model ${cachedModel} → ${model} for task complexity`);
268
+ try {
269
+ await existing.destroy();
270
+ }
271
+ catch { /* best-effort */ }
272
+ agentSessions.delete(key);
273
+ agentSessionModels.delete(key);
254
274
  }
255
- catch { /* best-effort */ }
256
- agentSessions.delete(key);
257
- agentSessionModels.delete(key);
258
275
  }
259
276
  const squad = getSquad(squadSlug);
260
277
  const client = await getClient();
@@ -0,0 +1,132 @@
1
+ // IO-level scheduler — fires recurring tasks for IO itself, independent of
2
+ // any squad. Mirrors the squad scheduler in shape (TICK_MS loop, in-flight
3
+ // guard, reconcile on startup) but dispatches into the orchestrator via
4
+ // sendToOrchestrator with a `background` source so IO can handle the prompt
5
+ // the same way it handles any other user message.
6
+ import { listIoSchedules, listDueIoSchedules, recordIoScheduleRun, setIoScheduleTimestamps, updateIoScheduleNextRun, } from "../store/io-schedules.js";
7
+ import { sendToOrchestrator } from "./orchestrator.js";
8
+ import { nextRun } from "./cron.js";
9
+ const TICK_MS = 30_000;
10
+ let timer;
11
+ const inFlight = new Set();
12
+ function buildPrompt(schedule) {
13
+ const header = `# Scheduled task: ${schedule.name}\n\n_This prompt was fired automatically by the IO scheduler. Cron expression: \`${schedule.cron_expr}\`._`;
14
+ const notes = schedule.notes
15
+ ? `\n\n**Operator notes:** ${schedule.notes}`
16
+ : "";
17
+ return `${header}\n\n${schedule.prompt}${notes}`;
18
+ }
19
+ async function fireSchedule(schedule) {
20
+ if (inFlight.has(schedule.id))
21
+ return;
22
+ inFlight.add(schedule.id);
23
+ const ranAt = new Date();
24
+ let nextIso = null;
25
+ try {
26
+ nextIso = nextRun(schedule.cron_expr, ranAt).toISOString();
27
+ }
28
+ catch (err) {
29
+ console.error(`[io] io-scheduler: cron parse error for schedule ${schedule.id}:`, err instanceof Error ? err.message : err);
30
+ }
31
+ recordIoScheduleRun(schedule.id, ranAt, nextIso);
32
+ console.log(`[io] io-scheduler: firing schedule "${schedule.name}" (next run: ${nextIso ?? "never"})`);
33
+ try {
34
+ await sendToOrchestrator(buildPrompt(schedule), { type: "background" }, () => {
35
+ // No-op: scheduled work is fire-and-forget; output is captured in
36
+ // the orchestrator's conversation log.
37
+ });
38
+ }
39
+ catch (err) {
40
+ console.error(`[io] io-scheduler: failed to dispatch schedule ${schedule.id}:`, err instanceof Error ? err.message : err);
41
+ }
42
+ finally {
43
+ inFlight.delete(schedule.id);
44
+ }
45
+ }
46
+ async function tick() {
47
+ let due;
48
+ try {
49
+ due = listDueIoSchedules(new Date());
50
+ }
51
+ catch (err) {
52
+ console.error("[io] io-scheduler tick failed:", err instanceof Error ? err.message : err);
53
+ return;
54
+ }
55
+ for (const s of due) {
56
+ await fireSchedule(s);
57
+ }
58
+ }
59
+ /**
60
+ * Backfill next_run_at for any IO schedules that are NULL or stale. We
61
+ * advance to the next future occurrence rather than replaying missed runs
62
+ * — same semantics as the squad scheduler.
63
+ */
64
+ export function reconcileIoSchedules(now = new Date()) {
65
+ for (const s of listIoSchedules()) {
66
+ if (!s.enabled)
67
+ continue;
68
+ let needsUpdate = false;
69
+ if (!s.next_run_at) {
70
+ needsUpdate = true;
71
+ }
72
+ else {
73
+ const next = new Date(s.next_run_at);
74
+ if (Number.isNaN(next.getTime()) || next <= now)
75
+ needsUpdate = true;
76
+ }
77
+ if (!needsUpdate)
78
+ continue;
79
+ try {
80
+ const next = nextRun(s.cron_expr, now);
81
+ updateIoScheduleNextRun(s.id, next.toISOString());
82
+ }
83
+ catch (err) {
84
+ console.error(`[io] io-scheduler: invalid cron "${s.cron_expr}" on schedule ${s.id}; clearing next_run_at:`, err instanceof Error ? err.message : err);
85
+ updateIoScheduleNextRun(s.id, null);
86
+ }
87
+ }
88
+ }
89
+ export function startIoScheduler() {
90
+ if (timer)
91
+ return;
92
+ reconcileIoSchedules();
93
+ timer = setInterval(() => {
94
+ void tick();
95
+ }, TICK_MS);
96
+ // Don't keep the event loop alive on shutdown
97
+ timer.unref?.();
98
+ }
99
+ export function stopIoScheduler() {
100
+ if (timer) {
101
+ clearInterval(timer);
102
+ timer = undefined;
103
+ }
104
+ }
105
+ /**
106
+ * Force a schedule to run immediately. Used by the `schedule_run_now` tool.
107
+ *
108
+ * The regular tick path (`fireSchedule`) advances `last_run_at` and
109
+ * `next_run_at` as a side effect, which is correct for an automatic firing
110
+ * but is the wrong behaviour for a manual one — a user testing a schedule at
111
+ * 04:30 should not have the 05:00 occurrence skipped or the schedule shifted.
112
+ * We therefore snapshot both timestamps before firing and restore them after,
113
+ * leaving the persisted schedule untouched.
114
+ */
115
+ export async function runIoScheduleNow(id) {
116
+ const all = listIoSchedules();
117
+ const s = all.find((x) => x.id === id);
118
+ if (!s)
119
+ return false;
120
+ const previousLast = s.last_run_at;
121
+ const previousNext = s.next_run_at;
122
+ try {
123
+ await fireSchedule(s);
124
+ }
125
+ finally {
126
+ // Restore the original timestamps even if fireSchedule threw, so a
127
+ // failed manual run cannot silently shift the schedule either.
128
+ setIoScheduleTimestamps(id, previousLast, previousNext);
129
+ }
130
+ return true;
131
+ }
132
+ //# sourceMappingURL=io-scheduler.js.map
@@ -0,0 +1,57 @@
1
+ import { getDb } from "../store/db.js";
2
+ /**
3
+ * Surgically correct historical peer-review rows that were stored as REJECTED
4
+ * (approved=0) but whose comments unambiguously begin with `APPROVED` (issue
5
+ * #50). Earlier daemon builds (pre-#43) inspected only the literal first line,
6
+ * which silently flipped many APPROVED reviews into REJECTED whenever the
7
+ * agent began its response with a markdown rule, blank line, header, or a
8
+ * short prose preamble.
9
+ *
10
+ * We only flip 0 -> 1, and only when the *current* parser sees an explicit
11
+ * line-leading APPROVED token in the prose. We never flip 1 -> 0: doing so
12
+ * would destroy data on legitimate prose-only approvals (e.g. "Excellent
13
+ * work — ships it") that the conservative parser would otherwise downgrade.
14
+ *
15
+ * Returns the number of rows updated.
16
+ */
17
+ export function backfillReviewVerdicts() {
18
+ const db = getDb();
19
+ const rows = db
20
+ .prepare("SELECT id, approved, comments FROM squad_task_reviews WHERE approved = 0 AND comments IS NOT NULL AND comments != ''")
21
+ .all();
22
+ const update = db.prepare("UPDATE squad_task_reviews SET approved = 1 WHERE id = ?");
23
+ let fixed = 0;
24
+ const tx = db.transaction((batch) => {
25
+ for (const r of batch) {
26
+ if (hasExplicitApproval(r.comments ?? "")) {
27
+ update.run(r.id);
28
+ fixed++;
29
+ }
30
+ }
31
+ });
32
+ tx(rows);
33
+ return fixed;
34
+ }
35
+ /**
36
+ * True when the comment body unambiguously starts with an APPROVED verdict —
37
+ * either as the first non-empty line (after stripping markdown noise) or as
38
+ * the verdict the current parser extracts before any REJECTED token. Anything
39
+ * shorter than that is left as the daemon originally recorded it.
40
+ */
41
+ function hasExplicitApproval(content) {
42
+ const stripped = content.replace(/[*_`#>]/g, "");
43
+ const lines = stripped
44
+ .split(/\r?\n/)
45
+ .map((l) => l.trim())
46
+ .filter(Boolean)
47
+ .slice(0, 10);
48
+ for (const line of lines) {
49
+ const lead = line
50
+ .toUpperCase()
51
+ .match(/^[^A-Z]*\b(APPROVED|REJECTED)\b/);
52
+ if (lead)
53
+ return lead[1] === "APPROVED";
54
+ }
55
+ return false;
56
+ }
57
+ //# sourceMappingURL=review-backfill.js.map
@@ -10,7 +10,7 @@
10
10
  //
11
11
  // Schedules survive daemon restarts because next_run_at is persisted. On
12
12
  // startup we backfill any next_run_at fields that became stale (or are NULL).
13
- import { listSchedules, listDueSchedules, recordScheduleRun, updateNextRun } from "../store/schedules.js";
13
+ import { listSchedules, listDueSchedules, recordScheduleRun, setScheduleTimestamps, updateNextRun } from "../store/schedules.js";
14
14
  import { getSquad } from "../store/squads.js";
15
15
  import { delegateToAgent } from "./agents.js";
16
16
  import { nextRun } from "./cron.js";
@@ -143,13 +143,29 @@ export function stopScheduler() {
143
143
  clearInterval(timer);
144
144
  timer = undefined;
145
145
  }
146
- /** Manually fire a schedule. Used by squad_schedule_run_now. */
146
+ /**
147
+ * Manually fire a schedule. Used by squad_schedule_run_now.
148
+ *
149
+ * Snapshots last_run_at and next_run_at before firing and restores them
150
+ * after, so a manual fire never disturbs the regular schedule (a user
151
+ * testing a 05:00 schedule at 04:30 should not have today's 05:00 run
152
+ * skipped or shifted). The fireSchedule path itself advances both fields
153
+ * because that's correct for an automatic firing — only manual runs need
154
+ * to leave the schedule untouched.
155
+ */
147
156
  export async function runScheduleNow(scheduleId) {
148
157
  const all = listSchedules();
149
158
  const s = all.find((x) => x.id === scheduleId);
150
159
  if (!s)
151
160
  return { ok: false, error: `Schedule ${scheduleId} not found` };
152
- await fireSchedule(s);
161
+ const previousLast = s.last_run_at;
162
+ const previousNext = s.next_run_at;
163
+ try {
164
+ await fireSchedule(s);
165
+ }
166
+ finally {
167
+ setScheduleTimestamps(scheduleId, previousLast, previousNext);
168
+ }
153
169
  return { ok: true };
154
170
  }
155
171
  //# sourceMappingURL=scheduler.js.map
@@ -116,6 +116,14 @@ Squads can be put on a recurring cron-style schedule. At the scheduled time IO w
116
116
 
117
117
  When a user asks something like "have the IO squad meet every weekday at 5AM to triage and prioritize", call \`squad_schedule_create\` with \`cron: "0 5 * * 1-5"\` and \`agenda: ["triage", "prioritize"]\`.
118
118
 
119
+ ### IO-level Schedules (squad-independent)
120
+ For recurring work that doesn't belong to any project squad — daily digests, periodic health checks, monitoring loops, reminders — use the IO-level scheduler instead of inventing a placeholder squad.
121
+
122
+ - \`schedule_create\` — create a recurring task. Each tick the configured prompt is delivered to the orchestrator as if a user had typed it. Cron is the same 5-field syntax as squad schedules.
123
+ - \`schedule_list\`, \`schedule_pause\`, \`schedule_resume\`, \`schedule_delete\`, \`schedule_run_now\` mirror the squad-schedule lifecycle.
124
+
125
+ When a user asks "remind me at 9AM every day to check the dashboard" or "every hour, summarize my open PRs", reach for \`schedule_create\` — not a squad.
126
+
119
127
  ### Agent Roles Are Dynamic
120
128
  **Do NOT use generic roles** like "developer" or "tester". Analyze the project first and create roles that match its actual technology stack. Examples:
121
129
  - IO project → "Copilot SDK Specialist", "Vue.js Frontend Dev", "Express API Engineer"
@@ -6,6 +6,8 @@ import { join, dirname, resolve } from "path";
6
6
  import { homedir } from "os";
7
7
  import { UNIVERSES } from "./universes.js";
8
8
  import { validateCron, nextRun } from "./cron.js";
9
+ import { createIoSchedule, deleteIoSchedule, getIoSchedule, listIoSchedules, setIoScheduleEnabled, updateIoScheduleNextRun, } from "../store/io-schedules.js";
10
+ import { runIoScheduleNow } from "./io-scheduler.js";
9
11
  import { createSchedule, deleteSchedule, getSchedule, listSchedules, setScheduleEnabled, } from "../store/schedules.js";
10
12
  import { runScheduleNow } from "./scheduler.js";
11
13
  // ---------------------------------------------------------------------------
@@ -1253,7 +1255,7 @@ export function createTools(deps) {
1253
1255
  },
1254
1256
  });
1255
1257
  const squadScheduleRunNow = defineTool("squad_schedule_run_now", {
1256
- description: "Manually fire a squad schedule immediately (useful for testing). Does not affect the next scheduled occurrence.",
1258
+ description: "Manually fire a squad schedule immediately (useful for testing). last_run_at and next_run_at are preserved so the regular schedule is untouched.",
1257
1259
  skipPermission: true,
1258
1260
  parameters: z.object({ id: z.number().int().describe("Schedule id") }),
1259
1261
  handler: async ({ id }) => {
@@ -1263,7 +1265,118 @@ export function createTools(deps) {
1263
1265
  return `🚀 Fired schedule ${id} now. Use squad_task_status to follow the resulting stand-up.`;
1264
1266
  },
1265
1267
  });
1266
- return [wikiRead, wikiWrite, wikiSearch, wikiDelete, wikiList, squadCreate, squadRecall, squadStatus, squadLogDecision, squadDelegate, squadTaskStatus, squadDelete, squadAnalyze, squadAddAgent, squadAgents, squadRemoveAgent, squadSetLead, squadSetQA, squadTaskReviews, squadScheduleCreate, squadScheduleList, squadScheduleDelete, squadSchedulePause, squadScheduleResume, squadScheduleRunNow, skillList, skillInstall, skillRemove, skillSearch, configUpdate, checkUpdate, shell, fileOps, bash, readFile, viewTool, grepTool, strReplaceEditor, github];
1268
+ // -------------------------------------------------------------------------
1269
+ // IO-level (squad-independent) schedules.
1270
+ // -------------------------------------------------------------------------
1271
+ const scheduleCreate = defineTool("schedule_create", {
1272
+ description: "Schedule a recurring task for IO itself (no squad required). At the scheduled time, the prompt is delivered to the orchestrator as a background message, just like any TUI/Telegram input. Use for daily digests, health checks, monitoring, or any automation that does not belong to a project squad.",
1273
+ skipPermission: true,
1274
+ parameters: z.object({
1275
+ name: z
1276
+ .string()
1277
+ .describe("Human-friendly name, e.g. 'Morning digest' or 'Hourly health check'"),
1278
+ cron: z
1279
+ .string()
1280
+ .describe("Standard 5-field cron expression: 'minute hour dom month dow'. Examples: '0 5 * * *' = daily at 5:00, '0 9 * * 1-5' = 9AM weekdays, '*/15 * * * *' = every 15 minutes."),
1281
+ prompt: z
1282
+ .string()
1283
+ .describe("The prompt to send to the orchestrator each time the schedule fires. Treat it like the message a user would type — concrete, action-oriented."),
1284
+ notes: z
1285
+ .string()
1286
+ .optional()
1287
+ .describe("Optional operator notes appended to the prompt."),
1288
+ }),
1289
+ handler: async ({ name, cron, prompt, notes }) => {
1290
+ const v = validateCron(cron);
1291
+ if (!v.ok)
1292
+ return `Invalid cron expression: ${v.error}`;
1293
+ const created = createIoSchedule({
1294
+ name,
1295
+ cronExpr: cron,
1296
+ prompt,
1297
+ notes: notes ?? null,
1298
+ nextRunAt: v.next.toISOString(),
1299
+ });
1300
+ return `⏰ Scheduled IO task "${created.name}" (id ${created.id}).\n- Cron: \`${cron}\`\n- Next run: ${v.next.toISOString()}`;
1301
+ },
1302
+ });
1303
+ const scheduleList = defineTool("schedule_list", {
1304
+ description: "List all IO-level schedules (those not attached to a squad). For squad schedules, use squad_schedule_list.",
1305
+ skipPermission: true,
1306
+ parameters: z.object({}),
1307
+ handler: async () => {
1308
+ const schedules = listIoSchedules();
1309
+ if (schedules.length === 0) {
1310
+ return "No IO schedules configured.";
1311
+ }
1312
+ return schedules
1313
+ .map((s) => {
1314
+ const enabled = s.enabled ? "▶️ enabled" : "⏸️ paused";
1315
+ const last = s.last_run_at ? `last ${s.last_run_at}` : "never run";
1316
+ const next = s.next_run_at ?? "—";
1317
+ const promptPreview = s.prompt.length > 120 ? s.prompt.slice(0, 120) + "…" : s.prompt;
1318
+ return `- **${s.name}** (id ${s.id}) — ${enabled}\n cron: \`${s.cron_expr}\`\n next: ${next} — ${last}\n prompt: ${promptPreview.replace(/\n/g, " ")}${s.notes ? `\n notes: ${s.notes}` : ""}`;
1319
+ })
1320
+ .join("\n");
1321
+ },
1322
+ });
1323
+ const scheduleDelete = defineTool("schedule_delete", {
1324
+ description: "Delete an IO schedule by id.",
1325
+ skipPermission: true,
1326
+ parameters: z.object({
1327
+ id: z.number().int().describe("Schedule id (from schedule_list)"),
1328
+ }),
1329
+ handler: async ({ id }) => {
1330
+ const existing = getIoSchedule(id);
1331
+ if (!existing)
1332
+ return `IO schedule ${id} not found.`;
1333
+ deleteIoSchedule(id);
1334
+ return `🗑️ Deleted IO schedule "${existing.name}" (id ${id}).`;
1335
+ },
1336
+ });
1337
+ const schedulePause = defineTool("schedule_pause", {
1338
+ description: "Pause an IO schedule so it stops firing (preserves config — resume with schedule_resume).",
1339
+ skipPermission: true,
1340
+ parameters: z.object({ id: z.number().int().describe("Schedule id") }),
1341
+ handler: async ({ id }) => {
1342
+ const existing = getIoSchedule(id);
1343
+ if (!existing)
1344
+ return `IO schedule ${id} not found.`;
1345
+ setIoScheduleEnabled(id, false);
1346
+ return `⏸️ Paused IO schedule "${existing.name}" (id ${id}).`;
1347
+ },
1348
+ });
1349
+ const scheduleResume = defineTool("schedule_resume", {
1350
+ description: "Resume a paused IO schedule. The next run is computed from now using the stored cron expression.",
1351
+ skipPermission: true,
1352
+ parameters: z.object({ id: z.number().int().describe("Schedule id") }),
1353
+ handler: async ({ id }) => {
1354
+ const existing = getIoSchedule(id);
1355
+ if (!existing)
1356
+ return `IO schedule ${id} not found.`;
1357
+ setIoScheduleEnabled(id, true);
1358
+ try {
1359
+ const next = nextRun(existing.cron_expr);
1360
+ updateIoScheduleNextRun(id, next.toISOString());
1361
+ return `▶️ Resumed IO schedule "${existing.name}" (id ${id}). Next run: ${next.toISOString()}`;
1362
+ }
1363
+ catch (err) {
1364
+ return `Resumed IO schedule "${existing.name}" but failed to compute next run: ${err instanceof Error ? err.message : String(err)}`;
1365
+ }
1366
+ },
1367
+ });
1368
+ const scheduleRunNow = defineTool("schedule_run_now", {
1369
+ description: "Manually fire an IO schedule immediately (useful for testing). last_run_at and next_run_at are preserved so the regular schedule is untouched.",
1370
+ skipPermission: true,
1371
+ parameters: z.object({ id: z.number().int().describe("Schedule id") }),
1372
+ handler: async ({ id }) => {
1373
+ const ok = await runIoScheduleNow(id);
1374
+ if (!ok)
1375
+ return `IO schedule ${id} not found.`;
1376
+ return `🚀 Fired IO schedule ${id} now.`;
1377
+ },
1378
+ });
1379
+ return [wikiRead, wikiWrite, wikiSearch, wikiDelete, wikiList, squadCreate, squadRecall, squadStatus, squadLogDecision, squadDelegate, squadTaskStatus, squadDelete, squadAnalyze, squadAddAgent, squadAgents, squadRemoveAgent, squadSetLead, squadSetQA, squadTaskReviews, squadScheduleCreate, squadScheduleList, squadScheduleDelete, squadSchedulePause, squadScheduleResume, squadScheduleRunNow, scheduleCreate, scheduleList, scheduleDelete, schedulePause, scheduleResume, scheduleRunNow, skillList, skillInstall, skillRemove, skillSearch, configUpdate, checkUpdate, shell, fileOps, bash, readFile, viewTool, grepTool, strReplaceEditor, github];
1267
1380
  }
1268
1381
  function walkDirectory(dir, maxDepth = 3, depth = 0) {
1269
1382
  if (depth >= maxDepth)