@crewx/cron 0.1.10-rc.20 → 0.1.10-rc.201

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.
@@ -1,819 +1,5 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.DATA_PATHS = void 0;
37
- exports.getLocalTimestamp = getLocalTimestamp;
38
- exports.logUsage = logUsage;
39
- exports.getProjectRoot = getProjectRoot;
40
- exports.normalizeMode = normalizeMode;
41
- exports.loadSchedules = loadSchedules;
42
- exports.saveSchedules = saveSchedules;
43
- exports.acquireLock = acquireLock;
44
- exports.releaseLock = releaseLock;
45
- exports.shouldRunNow = shouldRunNow;
46
- exports.describeCron = describeCron;
47
- exports.calculateNextRun = calculateNextRun;
48
- exports.isDaemonRunning = isDaemonRunning;
49
- exports.startDaemon = startDaemon;
50
- exports.stopDaemon = stopDaemon;
51
- exports.ensureDaemonRunning = ensureDaemonRunning;
52
- exports.getDaemonStatus = getDaemonStatus;
53
- exports.executeSchedule = executeSchedule;
54
- exports.daemonTick = daemonTick;
55
- exports.runDaemonForeground = runDaemonForeground;
56
- exports.addSchedule = addSchedule;
57
- exports.removeSchedule = removeSchedule;
58
- exports.enableSchedule = enableSchedule;
59
- exports.disableSchedule = disableSchedule;
60
- exports.updateSchedule = updateSchedule;
61
- const child_process_1 = require("child_process");
62
- const path = __importStar(require("path"));
63
- const fs = __importStar(require("fs"));
64
- const sdk_1 = require("@crewx/sdk");
65
- const DATA_DIR = path.join(process.cwd(), '.crewx');
66
- const SCHEDULES_FILE = path.join(DATA_DIR, '.cron-data.json');
67
- const LOG_FILE = path.join(DATA_DIR, '.cron-daemon.log');
68
- const LOCK_FILE = path.join(DATA_DIR, '.cron-data.json.lock');
69
- const USAGE_LOG_FILE = path.join(DATA_DIR, 'cron-usage.log');
70
- const PID_FILE = path.join(DATA_DIR, '.cron-daemon.pid');
71
- function resolveDataDir(dir) {
72
- return dir ? path.join(dir, '.crewx') : DATA_DIR;
73
- }
74
- function resolveSchedulesFile(dir) {
75
- return path.join(resolveDataDir(dir), '.cron-data.json');
76
- }
77
- function resolvePidFile(dir) {
78
- return path.join(resolveDataDir(dir), '.cron-daemon.pid');
79
- }
80
- const DAEMON_INTERVAL_MS = 60 * 1000;
81
- const runningSchedules = new Set();
82
- function getLocalTimestamp() {
83
- const now = new Date();
84
- const y = now.getFullYear();
85
- const m = String(now.getMonth() + 1).padStart(2, '0');
86
- const d = String(now.getDate()).padStart(2, '0');
87
- const h = String(now.getHours()).padStart(2, '0');
88
- const min = String(now.getMinutes()).padStart(2, '0');
89
- const s = String(now.getSeconds()).padStart(2, '0');
90
- return `${y}-${m}-${d} ${h}:${min}:${s}`;
91
- }
92
- function logUsage(...args) {
93
- ensureDataDir();
94
- const timestamp = getLocalTimestamp();
95
- const message = args.join(' ');
96
- const logLine = `${timestamp} | [cron] ${message}\n`;
97
- fs.appendFileSync(USAGE_LOG_FILE, logLine);
98
- }
99
- function ensureDataDir(dir) {
100
- const dataDir = resolveDataDir(dir);
101
- if (!fs.existsSync(dataDir)) {
102
- fs.mkdirSync(dataDir, { recursive: true });
103
- }
104
- }
105
- function safeReadJsonFile(filePath) {
106
- try {
107
- return JSON.parse(fs.readFileSync(filePath, 'utf8'));
108
- }
109
- catch {
110
- return null;
111
- }
112
- }
113
- function getProjectRoot() {
114
- try {
115
- return (0, child_process_1.execSync)('git rev-parse --show-toplevel', { encoding: 'utf8' }).trim();
116
- }
117
- catch {
118
- return process.cwd();
119
- }
120
- }
121
- function getBuiltinMemoryCliPath() {
122
- return path.join(getProjectRoot(), 'packages', 'built-in', 'memory', 'dist', 'cli.js');
123
- }
124
- function maybeRewriteLegacyMemoryCommand(command) {
125
- if (!command.includes('/skills/memory-v2/memory-v2.js')) {
126
- return command;
127
- }
128
- const builtinMemoryCli = getBuiltinMemoryCliPath();
129
- return `node ${builtinMemoryCli} summarize-dirty`;
130
- }
131
- function migrateLegacyMemoryDebounceSchedule() {
132
- ensureDataDir();
133
- const legacyFile = path.join(getProjectRoot(), 'skills', 'cron', '.cron-data.json');
134
- if (!fs.existsSync(legacyFile)) {
135
- return;
136
- }
137
- const legacySchedules = safeReadJsonFile(legacyFile);
138
- if (!Array.isArray(legacySchedules)) {
139
- return;
140
- }
141
- const legacySchedule = legacySchedules.find(s => s.name === 'memory-debounce');
142
- if (!legacySchedule) {
143
- return;
144
- }
145
- const targetSchedules = fs.existsSync(SCHEDULES_FILE) ? safeReadJsonFile(SCHEDULES_FILE) ?? [] : [];
146
- if (targetSchedules.some(s => s.name === 'memory-debounce')) {
147
- return;
148
- }
149
- const migratedSchedule = {
150
- ...legacySchedule,
151
- command: maybeRewriteLegacyMemoryCommand(legacySchedule.command),
152
- updated_at: new Date().toISOString(),
153
- next_run: calculateNextRun(legacySchedule.cron, legacySchedule.timezone),
154
- };
155
- saveSchedules([...targetSchedules, migratedSchedule]);
156
- }
157
- function normalizeMode(mode) {
158
- if (mode === 'query')
159
- return 'query';
160
- if (mode === 'command')
161
- return 'command';
162
- return 'execute';
163
- }
164
- function loadSchedules(dir) {
165
- ensureDataDir(dir);
166
- if (!dir) {
167
- migrateLegacyMemoryDebounceSchedule();
168
- }
169
- const schedulesFile = resolveSchedulesFile(dir);
170
- if (!fs.existsSync(schedulesFile)) {
171
- return [];
172
- }
173
- try {
174
- const data = fs.readFileSync(schedulesFile, 'utf8');
175
- return JSON.parse(data);
176
- }
177
- catch (e) {
178
- const err = e;
179
- console.error('Failed to load schedules:', err.message);
180
- return [];
181
- }
182
- }
183
- function saveSchedules(schedules, dir) {
184
- ensureDataDir(dir);
185
- const schedulesFile = resolveSchedulesFile(dir);
186
- const tempFile = `${schedulesFile}.tmp`;
187
- fs.writeFileSync(tempFile, JSON.stringify(schedules, null, 2));
188
- fs.renameSync(tempFile, schedulesFile);
189
- }
190
- async function acquireLock(timeout = 5000) {
191
- ensureDataDir();
192
- const startTime = Date.now();
193
- while (Date.now() - startTime < timeout) {
194
- try {
195
- fs.writeFileSync(LOCK_FILE, String(process.pid), { flag: 'wx' });
196
- return true;
197
- }
198
- catch (e) {
199
- const err = e;
200
- if (err.code === 'EEXIST') {
201
- try {
202
- const stat = fs.statSync(LOCK_FILE);
203
- if (Date.now() - stat.mtimeMs > 30000) {
204
- fs.unlinkSync(LOCK_FILE);
205
- continue;
206
- }
207
- }
208
- catch {
209
- continue;
210
- }
211
- await new Promise(resolve => setTimeout(resolve, 100));
212
- }
213
- else {
214
- throw e;
215
- }
216
- }
217
- }
218
- return false;
219
- }
220
- function releaseLock() {
221
- try {
222
- fs.unlinkSync(LOCK_FILE);
223
- }
224
- catch {
225
- }
226
- }
227
- const DAY_NAMES = {
228
- SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6,
229
- };
230
- const MONTH_NAMES = {
231
- JAN: 1, FEB: 2, MAR: 3, APR: 4, MAY: 5, JUN: 6,
232
- JUL: 7, AUG: 8, SEP: 9, OCT: 10, NOV: 11, DEC: 12,
233
- };
234
- function resolveValue(token, fieldIdx) {
235
- const upper = token.toUpperCase();
236
- if (fieldIdx === 4) {
237
- const day = DAY_NAMES[upper];
238
- if (day !== undefined)
239
- return day;
240
- const num = parseInt(token, 10);
241
- return num === 7 ? 0 : num;
242
- }
243
- if (fieldIdx === 3) {
244
- const month = MONTH_NAMES[upper];
245
- if (month !== undefined)
246
- return month;
247
- }
248
- return parseInt(token, 10);
249
- }
250
- function matchSegment(segment, value, fieldIdx) {
251
- if (segment === '*')
252
- return true;
253
- if (segment.includes('/')) {
254
- const slashPos = segment.indexOf('/');
255
- const rangePart = segment.slice(0, slashPos);
256
- const step = parseInt(segment.slice(slashPos + 1), 10);
257
- if (isNaN(step) || step <= 0)
258
- return false;
259
- if (rangePart === '*') {
260
- return value % step === 0;
261
- }
262
- if (rangePart.includes('-')) {
263
- const dashPos = rangePart.indexOf('-');
264
- const start = resolveValue(rangePart.slice(0, dashPos), fieldIdx);
265
- const end = resolveValue(rangePart.slice(dashPos + 1), fieldIdx);
266
- return value >= start && value <= end && (value - start) % step === 0;
267
- }
268
- const base = resolveValue(rangePart, fieldIdx);
269
- return value >= base && (value - base) % step === 0;
270
- }
271
- if (segment.includes('-')) {
272
- const dashPos = segment.indexOf('-');
273
- const start = resolveValue(segment.slice(0, dashPos), fieldIdx);
274
- const end = resolveValue(segment.slice(dashPos + 1), fieldIdx);
275
- if (start <= end) {
276
- return value >= start && value <= end;
277
- }
278
- return value >= start || value <= end;
279
- }
280
- return resolveValue(segment, fieldIdx) === value;
281
- }
282
- function matchField(field, value, fieldIdx) {
283
- return field.split(',').some(seg => matchSegment(seg.trim(), value, fieldIdx));
284
- }
285
- function isValidTimezone(tz) {
286
- try {
287
- Intl.DateTimeFormat('en-US', { timeZone: tz });
288
- return true;
289
- }
290
- catch {
291
- return false;
292
- }
293
- }
294
- function shouldRunNow(cronExpr, now, timezone) {
295
- let checkDate = now;
296
- if (timezone) {
297
- if (!isValidTimezone(timezone)) {
298
- console.error(`Invalid timezone: ${timezone}, skipping execution`);
299
- return false;
300
- }
301
- else {
302
- const formatter = new Intl.DateTimeFormat('en-US', {
303
- timeZone: timezone,
304
- year: 'numeric',
305
- month: '2-digit',
306
- day: '2-digit',
307
- hour: '2-digit',
308
- minute: '2-digit',
309
- hour12: false,
310
- });
311
- const parts = formatter.formatToParts(now);
312
- const getPart = (type) => parseInt(parts.find(p => p.type === type)?.value ?? '0', 10);
313
- const year = getPart('year');
314
- const month = getPart('month');
315
- const day = getPart('day');
316
- checkDate = {
317
- getMinutes: () => getPart('minute'),
318
- getHours: () => getPart('hour'),
319
- getDate: () => getPart('day'),
320
- getMonth: () => getPart('month') - 1,
321
- getDay: () => new Date(year, month - 1, day).getDay(),
322
- };
323
- }
324
- }
325
- const fields = cronExpr.trim().split(/\s+/);
326
- if (fields.length !== 5) {
327
- console.error(`Invalid cron expression: ${cronExpr}`);
328
- return false;
329
- }
330
- const [minute, hour, dayOfMonth, month, dayOfWeek] = fields;
331
- const m = checkDate.getMinutes();
332
- const h = checkDate.getHours();
333
- const dom = checkDate.getDate();
334
- const mon = checkDate.getMonth() + 1;
335
- const dow = checkDate.getDay();
336
- return (matchField(minute, m, 0) &&
337
- matchField(hour, h, 1) &&
338
- matchField(dayOfMonth, dom, 2) &&
339
- matchField(month, mon, 3) &&
340
- matchField(dayOfWeek, dow, 4));
341
- }
342
- function describeCron(cronExpr) {
343
- const fields = cronExpr.trim().split(/\s+/);
344
- if (fields.length !== 5)
345
- return cronExpr;
346
- const [minute, hour, dayOfMonth, month, dayOfWeek] = fields;
347
- if (minute === '0' && hour !== '*' && dayOfMonth === '*' && month === '*' && dayOfWeek === '*') {
348
- return `Daily at ${hour}:00`;
349
- }
350
- if (minute !== '*' && hour !== '*' && dayOfMonth === '*' && month === '*' && dayOfWeek === '*') {
351
- return `Daily at ${hour}:${minute.padStart(2, '0')}`;
352
- }
353
- if (cronExpr === '* * * * *') {
354
- return 'Every minute';
355
- }
356
- if (minute === '*/5' && hour === '*' && dayOfMonth === '*' && month === '*' && dayOfWeek === '*') {
357
- return 'Every 5 minutes';
358
- }
359
- return cronExpr;
360
- }
361
- function getDateParts(d, timezone) {
362
- if (timezone && isValidTimezone(timezone)) {
363
- const formatter = new Intl.DateTimeFormat('en-US', {
364
- timeZone: timezone,
365
- year: 'numeric', month: '2-digit', day: '2-digit',
366
- hour: '2-digit', minute: '2-digit', hour12: false,
367
- weekday: 'short',
368
- });
369
- const parts = formatter.formatToParts(d);
370
- const getPart = (type) => parseInt(parts.find(p => p.type === type)?.value ?? '0', 10);
371
- const weekdayStr = parts.find(p => p.type === 'weekday')?.value ?? '';
372
- const weekdayMap = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };
373
- return {
374
- minute: getPart('minute'),
375
- hour: getPart('hour'),
376
- day: getPart('day'),
377
- month: getPart('month'),
378
- weekday: weekdayMap[weekdayStr] ?? d.getDay(),
379
- };
380
- }
381
- return {
382
- minute: d.getMinutes(),
383
- hour: d.getHours(),
384
- day: d.getDate(),
385
- month: d.getMonth() + 1,
386
- weekday: d.getDay(),
387
- };
388
- }
389
- function getTimezoneOffsetMinutes(date, timezone) {
390
- const utcStr = date.toLocaleString('en-US', { timeZone: 'UTC' });
391
- const tzStr = date.toLocaleString('en-US', { timeZone: timezone });
392
- return (new Date(tzStr).getTime() - new Date(utcStr).getTime()) / 60000;
393
- }
394
- function calculateNextRun(cronExpr, timezone) {
395
- const fields = cronExpr.trim().split(/\s+/);
396
- if (fields.length !== 5)
397
- return null;
398
- const [minuteField, hourField, dayField, monthField, weekdayField] = fields;
399
- const matchingHours = [];
400
- for (let h = 0; h < 24; h++) {
401
- if (matchField(hourField, h, 1))
402
- matchingHours.push(h);
403
- }
404
- const matchingMinutes = [];
405
- for (let m = 0; m < 60; m++) {
406
- if (matchField(minuteField, m, 0))
407
- matchingMinutes.push(m);
408
- }
409
- if (matchingHours.length === 0 || matchingMinutes.length === 0)
410
- return null;
411
- const now = new Date();
412
- const useTz = timezone && isValidTimezone(timezone);
413
- const startParts = getDateParts(now, timezone);
414
- for (let dayOffset = 0; dayOffset <= 366; dayOffset++) {
415
- const refDate = new Date(now.getTime() + dayOffset * 86400000);
416
- const dp = getDateParts(refDate, timezone);
417
- if (!matchField(monthField, dp.month, 3))
418
- continue;
419
- if (!matchField(dayField, dp.day, 2))
420
- continue;
421
- if (!matchField(weekdayField, dp.weekday, 4))
422
- continue;
423
- for (const h of matchingHours) {
424
- for (const m of matchingMinutes) {
425
- if (dayOffset === 0 && (h < startParts.hour || (h === startParts.hour && m <= startParts.minute))) {
426
- continue;
427
- }
428
- let candidate;
429
- if (useTz) {
430
- const offsetMin = getTimezoneOffsetMinutes(refDate, timezone);
431
- candidate = new Date(refDate);
432
- candidate.setUTCHours(h - Math.floor(offsetMin / 60), m - (offsetMin % 60), 0, 0);
433
- }
434
- else {
435
- candidate = new Date(refDate);
436
- candidate.setHours(h, m, 0, 0);
437
- }
438
- if (candidate.getTime() <= now.getTime())
439
- continue;
440
- const cp = getDateParts(candidate, timezone);
441
- if (cp.hour === h && cp.minute === m &&
442
- matchField(dayField, cp.day, 2) &&
443
- matchField(monthField, cp.month, 3) &&
444
- matchField(weekdayField, cp.weekday, 4)) {
445
- return candidate.toISOString();
446
- }
447
- }
448
- }
449
- }
450
- return null;
451
- }
452
- function readDaemonPid(dir) {
453
- try {
454
- const raw = fs.readFileSync(resolvePidFile(dir), 'utf8').trim();
455
- const pid = Number.parseInt(raw, 10);
456
- return Number.isInteger(pid) && pid > 0 ? pid : null;
457
- }
458
- catch {
459
- return null;
460
- }
461
- }
462
- function writeDaemonPid(pid, dir) {
463
- ensureDataDir(dir);
464
- fs.writeFileSync(resolvePidFile(dir), `${pid}\n`);
465
- }
466
- function removeDaemonPidFile(dir) {
467
- try {
468
- fs.unlinkSync(resolvePidFile(dir));
469
- }
470
- catch {
471
- }
472
- }
473
- function isPidAlive(pid) {
474
- try {
475
- process.kill(pid, 0);
476
- return true;
477
- }
478
- catch (e) {
479
- const err = e;
480
- if (err.code === 'EPERM')
481
- return true;
482
- return false;
483
- }
484
- }
485
- function getDaemonCliPath() {
486
- const distCli = path.join(__dirname, '..', 'cli.js');
487
- if (fs.existsSync(distCli)) {
488
- return distCli;
489
- }
490
- throw new Error(`Cron CLI entrypoint not found: ${distCli}`);
491
- }
492
- function isDaemonRunning(dir) {
493
- const pid = readDaemonPid(dir);
494
- if (!pid) {
495
- return false;
496
- }
497
- const alive = isPidAlive(pid);
498
- if (!alive) {
499
- removeDaemonPidFile(dir);
500
- }
501
- return alive;
502
- }
503
- function startDaemon(dir) {
504
- if (isDaemonRunning(dir)) {
505
- const pid = readDaemonPid(dir);
506
- if (pid)
507
- return pid;
508
- throw new Error('Daemon is already running');
509
- }
510
- ensureDataDir(dir);
511
- const cliPath = getDaemonCliPath();
512
- const child = (0, child_process_1.spawn)('node', [cliPath, 'daemon', '--foreground'], {
513
- cwd: dir || undefined,
514
- detached: true,
515
- stdio: 'ignore',
516
- windowsHide: true,
517
- });
518
- if (!child.pid) {
519
- throw new Error('Failed to start daemon: no PID returned');
520
- }
521
- child.unref();
522
- writeDaemonPid(child.pid, dir);
523
- return child.pid;
524
- }
525
- function stopDaemon(dir) {
526
- const pid = readDaemonPid(dir);
527
- if (!pid) {
528
- removeDaemonPidFile(dir);
529
- return false;
530
- }
531
- if (!isPidAlive(pid)) {
532
- removeDaemonPidFile(dir);
533
- return false;
534
- }
535
- process.kill(pid, 'SIGTERM');
536
- return true;
537
- }
538
- function ensureDaemonRunning(dir) {
539
- if (isDaemonRunning(dir))
540
- return;
541
- console.log('⚡ cron 데몬이 꺼져 있어서 자동 시작합니다...');
542
- try {
543
- startDaemon(dir);
544
- }
545
- catch (e) {
546
- const err = e;
547
- console.error('Failed to auto-start daemon:', err.message);
548
- process.exit(1);
549
- }
550
- }
551
- function getDaemonStatus(dir) {
552
- const schedules = loadSchedules(dir);
553
- const enabled = schedules.filter(s => s.enabled).length;
554
- const pid = readDaemonPid(dir);
555
- const running = pid !== null && isDaemonRunning(dir);
556
- let started_at = null;
557
- const pidFile = resolvePidFile(dir);
558
- if (running && fs.existsSync(pidFile)) {
559
- try {
560
- started_at = fs.statSync(pidFile).mtime.toISOString();
561
- }
562
- catch {
563
- started_at = null;
564
- }
565
- }
566
- return {
567
- running,
568
- pid: running ? pid : null,
569
- started_at,
570
- schedules: {
571
- total: schedules.length,
572
- enabled,
573
- },
574
- };
575
- }
576
- async function executeSchedule(schedule, dir) {
577
- if (!schedule.allow_concurrent && runningSchedules.has(schedule.id)) {
578
- console.log(`[${getLocalTimestamp()}] Schedule ${schedule.id} already running, skipping`);
579
- return { success: false, error: 'Already running' };
580
- }
581
- runningSchedules.add(schedule.id);
582
- const workingDir = schedule.working_directory || getProjectRoot();
583
- const mode = schedule.mode;
584
- const modeFlag = mode === 'execute' ? 'x' : 'q';
585
- console.log(`[${getLocalTimestamp()}] Executing schedule ${schedule.id}: ${schedule.name ?? schedule.cron}`);
586
- console.log(`[${getLocalTimestamp()}] Command: ${schedule.command}`);
587
- return new Promise((resolve) => {
588
- const env = { ...process.env, ...schedule.env };
589
- delete env['CLAUDECODE'];
590
- const child = mode === 'command'
591
- ? (0, child_process_1.spawn)('sh', ['-c', schedule.command], {
592
- cwd: workingDir,
593
- stdio: 'inherit',
594
- windowsHide: true,
595
- env,
596
- })
597
- : (0, child_process_1.spawn)('crewx', [modeFlag, schedule.command], {
598
- cwd: workingDir,
599
- stdio: 'inherit',
600
- windowsHide: true,
601
- env,
602
- });
603
- const timeout = setTimeout(() => {
604
- console.log(`[${getLocalTimestamp()}] Schedule ${schedule.id} timeout, killing process`);
605
- try {
606
- child.kill('SIGTERM');
607
- }
608
- catch (e) {
609
- const err = e;
610
- console.error(`Failed to kill process: ${err.message}`);
611
- }
612
- runningSchedules.delete(schedule.id);
613
- resolve({ success: false, error: 'Timeout' });
614
- }, schedule.timeout_ms || 600000);
615
- child.on('close', (code) => {
616
- clearTimeout(timeout);
617
- runningSchedules.delete(schedule.id);
618
- const schedules = loadSchedules(dir);
619
- const idx = schedules.findIndex(s => s.id === schedule.id);
620
- if (idx !== -1) {
621
- const s = schedules[idx];
622
- s.last_run = new Date().toISOString();
623
- s.run_count = (s.run_count || 0) + 1;
624
- s.next_run = calculateNextRun(schedule.cron, schedule.timezone);
625
- saveSchedules(schedules, dir);
626
- }
627
- if (code === 0) {
628
- console.log(`[${getLocalTimestamp()}] Schedule ${schedule.id} completed successfully`);
629
- resolve({ success: true });
630
- }
631
- else {
632
- console.log(`[${getLocalTimestamp()}] Schedule ${schedule.id} failed with code ${code}`);
633
- resolve({ success: false, error: `Exit code: ${code}` });
634
- }
635
- });
636
- child.on('error', (err) => {
637
- clearTimeout(timeout);
638
- runningSchedules.delete(schedule.id);
639
- console.error(`[${getLocalTimestamp()}] Schedule ${schedule.id} error: ${err.message}`);
640
- resolve({ success: false, error: err.message });
641
- });
642
- });
643
- }
644
- function getLocalMinuteKey(now, timezone) {
645
- if (!timezone || !isValidTimezone(timezone)) {
646
- return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
647
- }
648
- const fmt = new Intl.DateTimeFormat('en-CA', {
649
- timeZone: timezone,
650
- year: 'numeric', month: '2-digit', day: '2-digit',
651
- hour: '2-digit', minute: '2-digit', hour12: false,
652
- });
653
- return fmt.format(now);
654
- }
655
- async function daemonTick(dir) {
656
- const now = new Date();
657
- const schedules = loadSchedules(dir);
658
- const enabledSchedules = schedules.filter(s => s.enabled);
659
- if (enabledSchedules.length === 0) {
660
- return;
661
- }
662
- for (const schedule of enabledSchedules) {
663
- if (shouldRunNow(schedule.cron, now, schedule.timezone)) {
664
- const minuteKey = getLocalMinuteKey(now, schedule.timezone);
665
- if (schedule.last_run) {
666
- const lastRunMinuteKey = getLocalMinuteKey(new Date(schedule.last_run), schedule.timezone);
667
- if (lastRunMinuteKey === minuteKey) {
668
- continue;
669
- }
670
- }
671
- await executeSchedule(schedule, dir);
672
- }
673
- }
674
- }
675
- function runDaemonForeground(dir) {
676
- writeDaemonPid(process.pid, dir);
677
- let cleanedUp = false;
678
- const cleanup = () => {
679
- if (cleanedUp)
680
- return;
681
- cleanedUp = true;
682
- const pid = readDaemonPid(dir);
683
- if (pid === process.pid) {
684
- removeDaemonPidFile(dir);
685
- }
686
- };
687
- process.on('SIGTERM', () => {
688
- cleanup();
689
- process.exit(0);
690
- });
691
- process.on('SIGINT', () => {
692
- cleanup();
693
- process.exit(0);
694
- });
695
- process.on('exit', cleanup);
696
- console.log(`[${getLocalTimestamp()}] Cron daemon starting in foreground mode`);
697
- console.log(`[${getLocalTimestamp()}] Check interval: 1 minute`);
698
- console.log(`[${getLocalTimestamp()}] Press Ctrl+C to stop\n`);
699
- const schedules = loadSchedules(dir);
700
- const enabled = schedules.filter(s => s.enabled).length;
701
- console.log(`[${getLocalTimestamp()}] Active schedules: ${enabled}/${schedules.length}\n`);
702
- void daemonTick(dir);
703
- setInterval(() => {
704
- void daemonTick(dir);
705
- }, DAEMON_INTERVAL_MS);
706
- }
707
- function addSchedule(cron, command, options, dataDir) {
708
- if (options.timezone && !isValidTimezone(options.timezone)) {
709
- throw new Error(`Invalid timezone: ${options.timezone}`);
710
- }
711
- const schedule = {
712
- id: (0, sdk_1.generateId)('cron'),
713
- cron,
714
- command,
715
- mode: normalizeMode(options.mode ?? 'execute'),
716
- enabled: true,
717
- name: options.name ?? null,
718
- working_directory: options.dir ?? null,
719
- timezone: options.timezone ?? null,
720
- timeout_ms: parseInt(options.timeout ?? '600000', 10),
721
- allow_concurrent: options.allowConcurrent ?? false,
722
- created_at: new Date().toISOString(),
723
- updated_at: new Date().toISOString(),
724
- run_count: 0,
725
- };
726
- schedule.next_run = calculateNextRun(cron, schedule.timezone);
727
- const schedules = loadSchedules(dataDir);
728
- schedules.push(schedule);
729
- saveSchedules(schedules, dataDir);
730
- return schedule;
731
- }
732
- function removeSchedule(id, dir) {
733
- const schedules = loadSchedules(dir);
734
- const idx = schedules.findIndex(s => s.id === id);
735
- if (idx === -1) {
736
- throw new Error(`Schedule not found: ${id}`);
737
- }
738
- const removed = schedules.splice(idx, 1)[0];
739
- saveSchedules(schedules, dir);
740
- return removed;
741
- }
742
- function enableSchedule(id, dir) {
743
- const schedules = loadSchedules(dir);
744
- const schedule = schedules.find(s => s.id === id);
745
- if (!schedule) {
746
- throw new Error(`Schedule not found: ${id}`);
747
- }
748
- schedule.enabled = true;
749
- schedule.updated_at = new Date().toISOString();
750
- schedule.next_run = calculateNextRun(schedule.cron, schedule.timezone);
751
- saveSchedules(schedules, dir);
752
- return schedule;
753
- }
754
- function disableSchedule(id, dir) {
755
- const schedules = loadSchedules(dir);
756
- const schedule = schedules.find(s => s.id === id);
757
- if (!schedule) {
758
- throw new Error(`Schedule not found: ${id}`);
759
- }
760
- schedule.enabled = false;
761
- schedule.updated_at = new Date().toISOString();
762
- saveSchedules(schedules, dir);
763
- return schedule;
764
- }
765
- function updateSchedule(id, options, dir) {
766
- const schedules = loadSchedules(dir);
767
- const schedule = schedules.find(s => s.id === id);
768
- if (!schedule) {
769
- throw new Error(`Schedule not found: ${id}`);
770
- }
771
- if (options.timezone !== undefined && options.timezone !== null && !isValidTimezone(options.timezone)) {
772
- throw new Error(`Invalid timezone: ${options.timezone}`);
773
- }
774
- const changes = [];
775
- if (options.cron !== undefined) {
776
- schedule.cron = options.cron;
777
- changes.push(`cron: ${options.cron}`);
778
- }
779
- if (options.command !== undefined) {
780
- schedule.command = options.command;
781
- changes.push(`command: ${options.command}`);
782
- }
783
- if (options.name !== undefined) {
784
- schedule.name = options.name;
785
- changes.push(`name: ${options.name}`);
786
- }
787
- if (options.mode !== undefined) {
788
- schedule.mode = normalizeMode(options.mode);
789
- changes.push(`mode: ${schedule.mode}`);
790
- }
791
- if (options.timezone !== undefined) {
792
- schedule.timezone = options.timezone;
793
- changes.push(`timezone: ${options.timezone}`);
794
- }
795
- if (options.working_directory !== undefined) {
796
- schedule.working_directory = options.working_directory;
797
- changes.push(`working_directory: ${options.working_directory}`);
798
- }
799
- if (options.timeout_ms !== undefined) {
800
- schedule.timeout_ms = options.timeout_ms;
801
- changes.push(`timeout: ${options.timeout_ms}ms`);
802
- }
803
- if (options.allow_concurrent !== undefined) {
804
- schedule.allow_concurrent = options.allow_concurrent;
805
- changes.push(`allow_concurrent: ${options.allow_concurrent}`);
806
- }
807
- schedule.updated_at = new Date().toISOString();
808
- schedule.next_run = calculateNextRun(schedule.cron, schedule.timezone);
809
- saveSchedules(schedules, dir);
810
- return { schedule, changes };
811
- }
812
- exports.DATA_PATHS = {
813
- DATA_DIR,
814
- SCHEDULES_FILE,
815
- LOG_FILE,
816
- USAGE_LOG_FILE,
817
- PID_FILE,
818
- };
819
- //# sourceMappingURL=engine.js.map
1
+ "use strict";var Qe=exports&&exports.__createBinding||(Object.create?(function(e,n,t,r){if(r===void 0)r=t;var i=Object.getOwnPropertyDescriptor(n,t);if(!i||("get"in i?!n.__esModule:i.writable||i.configurable)){i={enumerable:true,get:function(){return n[t]}}}Object.defineProperty(e,r,i)}):(function(e,n,t,r){if(r===void 0)r=t;e[r]=n[t]}));var en=exports&&exports.__setModuleDefault||(Object.create?(function(e,n){Object.defineProperty(e,"default",{enumerable:true,value:n})}):function(e,n){e["default"]=n});var Ce=exports&&exports.__importStar||(function(){var e=function(n){e=Object.getOwnPropertyNames||function(t){var r=[];for(var i in t)if(Object.prototype.hasOwnProperty.call(t,i))r[r.length]=i;return r};return e(n)};return function(n){if(n&&n.__esModule)return n;var t={};if(n!=null){for(var r=e(n),i=0;i<r.length;i++)if(r[i]!=="default")Qe(t,n,r[i])}en(t,n);return t}})();Object.defineProperty(exports,"__esModule",{value:true});exports.DATA_PATHS=exports.scheduledPeriodicAvailability=exports.CRON_RUNTIME_PROCESS_LEASE_FILENAME=exports.createCronRuntime=exports.getProjectRoot=exports.BUILTIN_SCHEDULES=exports.CronStoreError=void 0;exports.getLocalTimestamp=y;exports.logUsage=pn;exports.acquireWorkspaceLock=Y;exports.releaseWorkspaceLock=W;exports.migrateLegacyMemoryDebounceSchedule=hn;exports.normalizeMode=de;exports.effectiveEnabled=Q;exports.createScheduleStore=I;exports.loadSchedules=Cn;exports.saveSchedules=In;exports.getRemovedBuiltins=Ue;exports.seedBuiltinSchedules=kn;exports.acquireLock=Tn;exports.releaseLock=vn;exports.releaseRunLock=Fe;exports.shouldRunNow=Me;exports.describeCron=xn;exports.calculateNextRun=M;exports.isDaemonRunning=ye;exports.startDaemon=Wn;exports.stopDaemon=Gn;exports.startDaemonSafe=We;exports.ensureDaemonRunning=qn;exports.getDaemonStatus=Vn;exports.executeSchedule=Ge;exports.getLocalMinuteKey=qe;exports.buildOccurrenceKey=Ve;exports.daemonTick=ge;exports.runDaemonForeground=ct;exports.addSchedule=at;exports.removeSchedule=st;exports.enableSchedule=ut;exports.disableSchedule=dt;exports.updateSchedule=lt;const j=require("child_process");const Z=require("crypto");const m=Ce(require("path"));const f=Ce(require("fs"));const O=require("./types");Object.defineProperty(exports,"CronStoreError",{enumerable:true,get:function(){return O.CronStoreError}});const g=require("@crewx/sdk");const x=require("./builtins");Object.defineProperty(exports,"BUILTIN_SCHEDULES",{enumerable:true,get:function(){return x.BUILTIN_SCHEDULES}});Object.defineProperty(exports,"getProjectRoot",{enumerable:true,get:function(){return x.getProjectRoot}});const w=m.resolve(process.cwd());const H=m.join(w,".crewx");const nn=m.join(H,".cron-data.json");const tn=m.join(H,".cron-daemon.log");const rn=m.join(H,".cron-data.json.lock");const on=m.join(H,"cron-usage.log");const cn=m.join(H,".cron-daemon.pid");const an=m.join(H,".cron-builtins.json");function F(e){return m.join(m.resolve(e??w),".crewx")}function ae(e){return m.join(F(e),".cron-data.json")}function Ie(e){return m.join(F(e),".cron-data.json.lock")}function sn(e){return m.join(F(e),"cron-usage.log")}function K(e){return m.join(F(e),".cron-daemon.pid")}function Ae(e){return m.join(F(e),".cron-builtins.json")}function un(e,n,t){const r=(0,g.reserveUpdateAdmission)(e,n,t);if(!r){console.error(`[${y()}] Failed to record update-admission reservation for task ${e}`)}return r}function dn(e){(0,g.releaseUpdateAdmissionReservation)(e)}function Pe(e,n){return m.join(F(n),`.cron-run-${e}.lock`)}const ln=60*1e3;const ke=5e3;const fn=25;const mn=3e4;const Te=5e3;function y(){const e=new Date;const n=e.getFullYear();const t=String(e.getMonth()+1).padStart(2,"0");const r=String(e.getDate()).padStart(2,"0");const i=String(e.getHours()).padStart(2,"0");const c=String(e.getMinutes()).padStart(2,"0");const o=String(e.getSeconds()).padStart(2,"0");return`${n}-${t}-${r} ${i}:${c}:${o}`}function pn(e,...n){const t=a=>a!==void 0&&(a==="."||a===".."||m.isAbsolute(a)||a.includes(m.sep)||a.includes("/")||a.includes("\\")||a.endsWith(".crewx")||(()=>{try{return f.statSync(a).isDirectory()}catch{return false}})());let r;let i;if(n.length>0&&(e===void 0||t(e))){r=e;i=n}else if(n.length===0&&t(e)){r=e;i=[]}else if(n.length===1&&t(n[0])){r=n[0];i=e===void 0?[]:[e]}else{r=void 0;i=e===void 0?n:[e,...n]}z(r);const c=y();const o=i.join(" ");const s=`${c} | [cron] ${o}
2
+ `;f.appendFileSync(sn(r),s)}function z(e){const n=F(e);if(!f.existsSync(n)){f.mkdirSync(n,{recursive:true})}}function se(e){try{return JSON.parse(f.readFileSync(e,"utf8"))}catch{return null}}const Sn=new Int32Array(new SharedArrayBuffer(4));function yn(){Atomics.wait(Sn,0,0,fn)}function ve(e){try{const n=JSON.parse(f.readFileSync(e,"utf8"));if(typeof n.token!=="string"||typeof n.pid!=="number"||typeof n.created_at!=="number"){return null}return n}catch{return null}}function Y(e,n=ke){const t=m.resolve(e);z(t);const r=Ie(t);const i=(0,Z.randomUUID)();const c=Date.now()+Math.max(0,n);const o={workspacePath:t,lockPath:r,token:i};while(true){const s={token:i,pid:process.pid,created_at:Date.now()};try{f.writeFileSync(r,JSON.stringify(s),{flag:"wx",mode:384});return o}catch(a){const d=a;if(d.code!=="EEXIST")throw a;const l=ve(r);if(l&&Date.now()-l.created_at>mn){W({...o,token:l.token});continue}if(Date.now()>=c){throw new O.CronStoreError("CRON_STORE_LOCK_TIMEOUT",`Timed out waiting for cron store lock: ${r}`,r)}yn()}}}function W(e){const n=ve(e.lockPath);if(!n||n.token!==e.token)return false;try{f.unlinkSync(e.lockPath);return true}catch(t){const r=t;if(r.code==="ENOENT")return false;throw t}}const ue=(0,x.findBuiltinById)("memory-debounce");function _n(e){if(!e.includes("/skills/memory-v2/memory-v2.js")){return e}return ue.buildCommand()}function hn(e=(0,x.getProjectRoot)()){const n=m.resolve(e);const t=m.join(n,"skills","cron",".cron-data.json");if(!f.existsSync(t)){return}const r=se(t);if(!Array.isArray(r)){return}const i=r.find(a=>a.name===ue.name);if(!i){return}const c=I(n);const o=c.list();if(o.some(a=>a.name===ue.name)){return}const s={...i,command:_n(i.command),updated_at:new Date().toISOString(),next_run:M(i.cron,i.timezone)};c.create(s)}function Re(e){if(e.includes("--overdrive")){console.warn("warning: --overdrive in cron command is ignored and may silently override --model")}}function de(e){if(e==="query")return"query";if(e==="command")return"command";return"execute"}function C(e){return{...e,...e.env?{env:{...e.env}}:{}}}function ne(e){let n;try{n=f.readFileSync(e,"utf8")}catch(r){const i=r;if(i.code==="ENOENT")return[];throw r}let t;try{t=JSON.parse(n)}catch(r){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store is not valid JSON: ${e}`,e)}if(!Array.isArray(t)){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store must contain a top-level array: ${e}`,e)}for(const r of t)wn(r,e);return t}const gn=["apps","skills","bundled"];function Q(e){return e.app!==void 0?e.app.enabled===true:e.enabled===true}function wn(e,n){if(typeof e!=="object"||e===null||Array.isArray(e)){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store has a row that is not an object: ${n}`,n)}const t=e;if(t.mode==="app"){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store has a row in the retired app mode: ${n}`,n)}if(t.app!==void 0&&!En(t.app)){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store has a malformed app binding: ${n}`,n)}if(t.last_error_code!==void 0&&(t.app===void 0||!(0,g.isAppScheduleCode)(t.last_error_code))){throw new O.CronStoreError("CRON_STORE_CORRUPT",`Cron schedule store has an error code that is not a managed row's stable code: ${n}`,n)}}function Le(e){if(typeof e!=="object"||e===null)return false;const n=e;return typeof n.appId==="string"&&n.appId.length>0&&typeof n.actionId==="string"&&n.actionId.length>0&&typeof n.source==="string"&&gn.includes(n.source)}function En(e){return Le(e)&&typeof e.enabled==="boolean"}function Ne(e,n){if(n===void 0||n===null)delete e.last_error_code;else e.last_error_code=n}function le(e){return new O.CronStoreError("CRON_SCHEDULE_MANAGED",`CRON_SCHEDULE_MANAGED: ${e.id} is managed by app ${e.app?.appId??"(unknown)"}; change it from the App`)}function Dn(e,n){if(e.app===void 0)return;const t=Object.entries(n).every(([r,i])=>i===void 0||r==="enabled"&&i===false);if(!t)throw le(e)}function On(e){if(process.platform==="win32")return;try{const n=f.openSync(e,"r");try{f.fsyncSync(n)}finally{f.closeSync(n)}}catch{}}function $e(e,n){const t=m.dirname(e);const r=m.join(t,`.${m.basename(e)}.${process.pid}.${(0,Z.randomUUID)()}.tmp`);const i=JSON.stringify(n,null,2);let c;try{c=f.openSync(r,"wx",384);f.writeFileSync(c,i);f.fsyncSync(c);f.closeSync(c);c=void 0;f.renameSync(r,e);On(t)}finally{if(c!==void 0){try{f.closeSync(c)}catch{}}try{f.unlinkSync(r)}catch{}}}function G(e,n){const t=e.find(r=>r.id===n);if(!t){throw new O.CronStoreError("CRON_SCHEDULE_NOT_FOUND",`Schedule not found: ${n}`)}return t}function N(e,n){const t=Y(e);try{const r=ae(e);const i=ne(r);const c=n(i);$e(r,i);return c}finally{W(t)}}function I(e=w){const n=m.resolve(e);const t=F(n);const r=ae(n);const i=Ie(n);return{workspacePath:n,dataDir:t,schedulesFile:r,lockFile:i,list(){return ne(r).map(C)},get(c){const o=ne(r).find(s=>s.id===c);return o?C(o):void 0},create(c){if(c.app!==void 0){throw new O.CronStoreError("CRON_SCHEDULE_MANAGED","CRON_SCHEDULE_MANAGED: an app schedule is created only by the App path")}return N(n,o=>{if(o.some(s=>s.id===c.id)){throw new O.CronStoreError("CRON_SCHEDULE_EXISTS",`Schedule already exists: ${c.id}`)}o.push(C(c));return C(c)})},update(c,o){return N(n,s=>{const a=G(s,c);Dn(a,o);if(o.cron!==void 0)a.cron=o.cron;if(o.command!==void 0)a.command=o.command;if(o.mode!==void 0)a.mode=o.mode;if(o.enabled!==void 0){if(a.app!==void 0)a.app={...a.app,enabled:o.enabled};else a.enabled=o.enabled}if(o.name!==void 0)a.name=o.name;if(o.working_directory!==void 0)a.working_directory=o.working_directory;if(o.timezone!==void 0)a.timezone=o.timezone;if(o.timeout_ms!==void 0)a.timeout_ms=o.timeout_ms;if(o.allow_concurrent!==void 0)a.allow_concurrent=o.allow_concurrent;if(o.env!==void 0){if(o.env===null)delete a.env;else a.env={...o.env}}if(o.model!==void 0){if(o.model===null)delete a.model;else a.model=o.model}if(o.provider!==void 0){if(o.provider===null)delete a.provider;else a.provider=o.provider}a.updated_at=new Date().toISOString();a.next_run=a.app!==void 0&&!a.app.enabled?null:M(a.cron,a.timezone);return C(a)})},remove(c){return N(n,o=>{const s=o.findIndex(d=>d.id===c);if(s===-1){throw new O.CronStoreError("CRON_SCHEDULE_NOT_FOUND",`Schedule not found: ${c}`)}if(o[s].app!==void 0)throw le(o[s]);const[a]=o.splice(s,1);return C(a)})},toggle(c,o){return N(n,s=>{const a=G(s,c);const d=o??!Q(a);const l=new Date().toISOString();if(a.app!==void 0){if(d)throw le(a);a.app={...a.app,enabled:false};a.next_run=null;a.updated_at=l;return C(a)}a.enabled=d;a.updated_at=l;if(a.enabled){a.next_run=M(a.cron,a.timezone)}return C(a)})},setAppSchedule(c,o,s){if(!Le(c))throw new TypeError("setAppSchedule needs an owner with appId, actionId and source");return N(n,a=>{const d=new Date().toISOString();let l=a.find(_=>_.app?.appId===c.appId);const P=l?.app?.enabled===true;if(l===void 0){l={id:(0,g.generateId)("cron"),cron:o.cron,command:g.APP_SCHEDULE_INERT_COMMAND,mode:"command",enabled:false,name:o.name,working_directory:null,timezone:null,timeout_ms:o.timeout_ms,allow_concurrent:false,created_at:d,updated_at:d,run_count:0};a.push(l)}const D=s??l.app?.enabled??false;l.mode="command";l.command=g.APP_SCHEDULE_INERT_COMMAND;l.enabled=false;l.app={appId:c.appId,actionId:c.actionId,source:c.source,enabled:D};l.cron=o.cron;l.timeout_ms=o.timeout_ms;l.name=o.name;l.allow_concurrent=false;l.next_run=D?M(l.cron,l.timezone):null;l.updated_at=d;return{schedule:C(l),started:!P&&D}})},recordSkipped(c,o){if(o.errorCode!==null&&!(0,g.isAppScheduleCode)(o.errorCode)){throw new TypeError("recordSkipped needs a stable error code")}return N(n,s=>{const a=G(s,c);if(a.app===void 0)throw new TypeError("recordSkipped closes only a managed row");const d=o.finishedAt??new Date().toISOString();a.last_run=d;a.last_outcome=o.outcome;Ne(a,o.errorCode);a.updated_at=d;return C(a)})},removeAppSchedule(c){return N(n,o=>{const s=o.findIndex(d=>d.app?.appId===c);if(s===-1)return void 0;const[a]=o.splice(s,1);return C(a)})},claimOccurrence(c,o){return N(n,s=>{const a=G(s,typeof c==="string"?c:c.id);if(a.last_occurrence_key===o.occurrenceKey){return{claimed:false,schedule:C(a)}}const d=o.startedAt??new Date().toISOString();a.last_started_at=d;a.last_occurrence_key=o.occurrenceKey;a.updated_at=d;return{claimed:true,schedule:C(a)}})},recordStarted(c,o){return N(n,s=>{const a=G(s,typeof c==="string"?c:c.id);const d=typeof o==="string"?{startedAt:o}:o;const l=d?.startedAt??new Date().toISOString();a.last_started_at=l;if(d?.occurrenceKey!==void 0){a.last_occurrence_key=d.occurrenceKey}a.updated_at=l;return C(a)})},recordFinished(c,o){if(o?.errorCode!==void 0&&!(0,g.isAppScheduleCode)(o.errorCode)){throw new TypeError("recordFinished needs a stable error code")}return N(n,s=>{const a=G(s,typeof c==="string"?c:c.id);const d=o?.finishedAt??new Date().toISOString();a.last_run=d;a.run_count=(a.run_count||0)+1;a.next_run=o?.nextRun!==void 0?o.nextRun:M(a.cron,a.timezone);if(o?.outcome!==void 0)a.last_outcome=o.outcome;else if(o?.success!==void 0)a.last_outcome=o.success?"success":"failure";if(a.app!==void 0)Ne(a,o?.errorCode);a.updated_at=d;return C(a)})}}}function Cn(e){return I(e??w).list()}function In(e,n){const t=m.resolve(n??w);const r=Y(t);try{const i=ae(t);ne(i);$e(i,e.map(C))}finally{W(r)}}function be(e){const n=se(Ae(e));return{removed_builtins:Array.isArray(n?.removed_builtins)?n.removed_builtins:[]}}function An(e,n){z(n);const t=Ae(n);const r=`${t}.tmp`;f.writeFileSync(r,JSON.stringify(e,null,2));f.renameSync(r,t)}function Ue(e){return be(e).removed_builtins}function Pn(e,n){const t=be(n);if(!t.removed_builtins.includes(e)){t.removed_builtins.push(e);An(t,n)}}function kn(e){const n=I(e??w);const t=n.list();const r=new Set(t.map(o=>o.name));const i=new Set(Ue(e));const c=[];for(const o of x.BUILTIN_SCHEDULES){if(r.has(o.name))continue;if(i.has(o.builtin_id))continue;const s=new Date().toISOString();const a={id:(0,g.generateId)("cron"),cron:o.cron,command:o.buildCommand(),mode:o.mode,enabled:true,name:o.name,working_directory:null,timezone:null,timeout_ms:6e5,allow_concurrent:false,created_at:s,updated_at:s,run_count:0,next_run:M(o.cron,null)};n.create(a);r.add(o.name);c.push(o.builtin_id)}return c}const fe=new Map;async function Tn(e=ke,n){const t=m.resolve(n??w);try{fe.set(t,Y(t,e));return true}catch(r){if(r instanceof O.CronStoreError&&r.code==="CRON_STORE_LOCK_TIMEOUT")return false;throw r}}function vn(e){const n=m.resolve(e??w);const t=fe.get(n);if(!t)return;fe.delete(n);W(t)}function ee(e){return se(e)}function Rn(e,n){const t=m.dirname(e);const r=m.join(t,`.${m.basename(e)}.${process.pid}.${(0,Z.randomUUID)()}.tmp`);let i;try{i=f.openSync(r,"wx",384);f.writeFileSync(i,JSON.stringify(n));f.fsyncSync(i);f.closeSync(i);i=void 0;f.renameSync(r,e)}finally{if(i!==void 0){try{f.closeSync(i)}catch{}}try{f.unlinkSync(r)}catch{}}}function me(e,n,t){const r=Y(e);try{return t(Pe(n,e))}finally{W(r)}}function Ln(e){try{return Math.max(0,Date.now()-f.statSync(e).mtimeMs)}catch{return null}}function Nn(e,n){if(e.startTime!==null&&n.startTime!==null){return e.startTime===n.startTime}if(e.commandLine!==null&&n.commandLine!==null){return e.commandLine===n.commandLine}return null}function $n(e,n,t){if(e.phase!=="child"){const s=Ln(n);if(s===null)return false;const a=e.hostPid??e.pid;const d=Se(a);return d===false||s>t+Te}const r=e.childPid??e.pid;const i=Se(r);if(i===false)return true;if(i!==true)return false;if(!e.childIdentity)return false;const c=He(r);if(!c)return false;const o=Nn(e.childIdentity,c);if(o===false)return true;return false}function bn(e,n,t,r){const i=m.resolve(r??w);z(i);const c=Pe(e,i);const o=(0,Z.randomUUID)();const s=(0,Z.randomUUID)();const a={leaseId:o,ownerToken:s,phase:"admitting",hostPid:process.pid,pid:process.pid,taskId:n,startedAt:new Date().toISOString()};return me(i,e,()=>{for(let l=0;l<2;l++){try{f.writeFileSync(c,JSON.stringify(a),{flag:"wx",mode:384});return{acquired:true,lease:{scheduleId:e,workspacePath:i,lockFile:c,leaseId:o,ownerToken:s,taskId:n}}}catch(P){const D=P;if(D.code!=="EEXIST")throw D;const _=ee(c);if(!_){return{acquired:false,runningTaskId:"unknown",reason:"execution_lease"}}if($n(_,c,t)){const T=ee(c);if(T?.leaseId!==void 0&&T.leaseId!==_.leaseId){continue}try{f.unlinkSync(c)}catch{}continue}return{acquired:false,runningTaskId:_.taskId,reason:"execution_lease"}}}const d=ee(c);return{acquired:false,runningTaskId:d?.taskId??"unknown",reason:"execution_lease"}})}function Un(e,n,t){return me(e.workspacePath,e.scheduleId,r=>{const i=ee(r);if(!i||i.leaseId!==e.leaseId||i.ownerToken!==e.ownerToken){return false}Rn(r,{...i,phase:"child",pid:n,childPid:n,childIdentity:t});return true})}function Fe(e,n,t){const r=m.resolve(t??w);z(r);return me(r,e,i=>{const c=ee(i);if(!c||c.leaseId!==n)return false;try{f.unlinkSync(i);return true}catch(o){const s=o;if(s.code==="ENOENT")return false;throw o}})}const Fn={SUN:0,MON:1,TUE:2,WED:3,THU:4,FRI:5,SAT:6};const Mn={JAN:1,FEB:2,MAR:3,APR:4,MAY:5,JUN:6,JUL:7,AUG:8,SEP:9,OCT:10,NOV:11,DEC:12};function q(e,n){const t=e.toUpperCase();if(n===4){const r=Fn[t];if(r!==void 0)return r;const i=parseInt(e,10);return i===7?0:i}if(n===3){const r=Mn[t];if(r!==void 0)return r}return parseInt(e,10)}function jn(e,n,t){if(e==="*")return true;if(e.includes("/")){const r=e.indexOf("/");const i=e.slice(0,r);const c=parseInt(e.slice(r+1),10);if(isNaN(c)||c<=0)return false;if(i==="*"){return n%c===0}if(i.includes("-")){const s=i.indexOf("-");const a=q(i.slice(0,s),t);const d=q(i.slice(s+1),t);return n>=a&&n<=d&&(n-a)%c===0}const o=q(i,t);return n>=o&&(n-o)%c===0}if(e.includes("-")){const r=e.indexOf("-");const i=q(e.slice(0,r),t);const c=q(e.slice(r+1),t);if(i<=c){return n>=i&&n<=c}return n>=i||n<=c}return q(e,t)===n}function k(e,n,t){return e.split(",").some(r=>jn(r.trim(),n,t))}function V(e){try{Intl.DateTimeFormat("en-US",{timeZone:e});return true}catch{return false}}function Me(e,n,t){let r=n;if(t){if(!V(t)){console.error(`Invalid timezone: ${t}, skipping execution`);return false}else{const b=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hour12:false});const A=b.formatToParts(n);const E=S=>parseInt(A.find(h=>h.type===S)?.value??"0",10);const p=E("year");const $=E("month");const u=E("day");r={getMinutes:()=>E("minute"),getHours:()=>E("hour"),getDate:()=>E("day"),getMonth:()=>E("month")-1,getDay:()=>new Date(p,$-1,u).getDay()}}}const i=e.trim().split(/\s+/);if(i.length!==5){console.error(`Invalid cron expression: ${e}`);return false}const[c,o,s,a,d]=i;const l=r.getMinutes();const P=r.getHours();const D=r.getDate();const _=r.getMonth()+1;const T=r.getDay();return k(c,l,0)&&k(o,P,1)&&k(s,D,2)&&k(a,_,3)&&k(d,T,4)}function xn(e){const n=e.trim().split(/\s+/);if(n.length!==5)return e;const[t,r,i,c,o]=n;if(t==="0"&&r!=="*"&&i==="*"&&c==="*"&&o==="*"){return`Daily at ${r}:00`}if(t!=="*"&&r!=="*"&&i==="*"&&c==="*"&&o==="*"){return`Daily at ${r}:${t.padStart(2,"0")}`}if(e==="* * * * *"){return"Every minute"}if(t==="*/5"&&r==="*"&&i==="*"&&c==="*"&&o==="*"){return"Every 5 minutes"}return e}function pe(e,n){if(n&&V(n)){const t=new Intl.DateTimeFormat("en-US",{timeZone:n,year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hour12:false,weekday:"short"});const r=t.formatToParts(e);const i=s=>parseInt(r.find(a=>a.type===s)?.value??"0",10);const c=r.find(s=>s.type==="weekday")?.value??"";const o={Sun:0,Mon:1,Tue:2,Wed:3,Thu:4,Fri:5,Sat:6};return{minute:i("minute"),hour:i("hour"),day:i("day"),month:i("month"),weekday:o[c]??e.getDay()}}return{minute:e.getMinutes(),hour:e.getHours(),day:e.getDate(),month:e.getMonth()+1,weekday:e.getDay()}}function Hn(e,n){const t=e.toLocaleString("en-US",{timeZone:"UTC"});const r=e.toLocaleString("en-US",{timeZone:n});return(new Date(r).getTime()-new Date(t).getTime())/6e4}function M(e,n){const t=e.trim().split(/\s+/);if(t.length!==5)return null;const[r,i,c,o,s]=t;const a=[];for(let _=0;_<24;_++){if(k(i,_,1))a.push(_)}const d=[];for(let _=0;_<60;_++){if(k(r,_,0))d.push(_)}if(a.length===0||d.length===0)return null;const l=new Date;const P=n&&V(n);const D=pe(l,n);for(let _=0;_<=366;_++){const T=new Date(l.getTime()+_*864e5);const b=pe(T,n);if(!k(o,b.month,3))continue;if(!k(c,b.day,2))continue;if(!k(s,b.weekday,4))continue;for(const A of a){for(const E of d){if(_===0&&(A<D.hour||A===D.hour&&E<=D.minute)){continue}let p;if(P){const u=Hn(T,n);p=new Date(T);p.setUTCHours(A-Math.floor(u/60),E-u%60,0,0)}else{p=new Date(T);p.setHours(A,E,0,0)}if(p.getTime()<=l.getTime())continue;const $=pe(p,n);if($.hour===A&&$.minute===E&&k(c,$.day,2)&&k(o,$.month,3)&&k(s,$.weekday,4)){return p.toISOString()}}}}return null}function B(e){try{const n=f.readFileSync(K(e),"utf8").trim();if(!/^\d+$/.test(n))return null;const t=Number(n);return Number.isSafeInteger(t)&&t>0?t:null}catch{return null}}function je(e,n){z(n);f.writeFileSync(K(n),`${e}
3
+ `)}function te(e){try{f.unlinkSync(K(e))}catch{}}function Se(e){try{process.kill(e,0);return true}catch(n){const t=n;return t.code==="ESRCH"?false:"unknown"}}function xe(e){try{const n=process.platform==="win32"?(0,j.execFileSync)("powershell.exe",["-NoLogo","-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CommandLine`],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}):(0,j.execFileSync)("ps",["-ww","-p",String(e),"-o","command="],{encoding:"utf8",stdio:["ignore","pipe","ignore"]});return n.trim()||null}catch{return null}}function zn(e){try{const n=process.platform==="win32"?(0,j.execFileSync)("powershell.exe",["-NoLogo","-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CreationDate.ToUniversalTime().ToString("o")`],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}):(0,j.execFileSync)("ps",["-p",String(e),"-o","lstart="],{encoding:"utf8",stdio:["ignore","pipe","ignore"]});return n.trim()||null}catch{return null}}function He(e){const n={commandLine:xe(e),startTime:zn(e)};return n.commandLine!==null||n.startTime!==null?n:null}const J={isAlive:Se,getCommandLine:xe};function ze(e){const n=e.replace(/\\/g,"/");return process.platform==="win32"?n.toLowerCase():n}function Bn(){try{const e=ze(Be()).split("/").filter(Boolean);const n=e.slice(-3).join("/");return n||null}catch{return null}}function Kn(e){if(!e?.trim())return"unknown";const n=Bn();if(!n)return"unknown";const t=ze(e);return t.includes("daemon --foreground")&&t.includes(n)?"running":"stale"}function Be(){const e=[m.join(__dirname,"..","cli.js"),m.join(__dirname,"..","dist","cli.js")];const n=e.find(t=>f.existsSync(t));if(n){return n}throw new Error(`Cron CLI entrypoint not found: ${e[0]}`)}function re(e,n=J){const t=B(e);if(!t){te(e);return"stale"}let r;try{r=n.isAlive(t)}catch{r="unknown"}if(!r){te(e);return"stale"}let i;try{i=n.getCommandLine(t)}catch{return"unknown"}const c=Kn(i);if(c==="stale"){te(e)}return c}function ye(e,n=J){return re(e,n)==="running"}function Ke(e,n=j.spawn){if((0,g.isDurablyQuiescing)()){throw new Error("Cannot start cron daemon: a desktop update is draining active work")}z(e);const t=Be();const r={...process.env};if(process.versions.electron){r.ELECTRON_RUN_AS_NODE="1"}const i=n(process.execPath,[t,"daemon","--foreground"],{cwd:e||void 0,detached:true,stdio:"ignore",windowsHide:true,env:r});if(!i.pid){throw new Error("Failed to start daemon: no PID returned")}i.unref();je(i.pid,e);return i.pid}function Wn(e,n=J){const t=re(e,n);if(t==="running"){const r=B(e);if(r)return r;throw new Error("Daemon is already running")}if(t==="unknown"){const r=B(e);throw new Error(`Unable to determine cron daemon liveness (pid=${r??"unknown"}, pidFile=${K(e)}); refusing to start`)}return Ke(e,n.spawnImpl)}function Gn(e,n=J){if(re(e,n)!=="running")return false;const t=B(e);if(!t)return false;process.kill(t,"SIGTERM");return true}function We(e,n=J){const t=re(e,n);if(t==="running"){return{started:false}}if(t==="unknown"){return{started:false,error:`Unable to determine cron daemon liveness (pid=${B(e)??"unknown"}, pidFile=${K(e)}); refusing to start`}}try{const r=Ke(e,n.spawnImpl);return{started:true,pid:r}}catch(r){const i=r;return{started:false,error:i.message}}}function qn(e,n=J){if(ye(e,n))return;console.log("\u26A1 Cron daemon is not running; starting it automatically...");const t=We(e,n);if(!t.started){console.error("Failed to auto-start daemon:",t.error);process.exit(1)}}function Vn(e){const n=I(e??w).list();const t=n.filter(s=>Q(s)).length;const r=ye(e);const i=r?B(e):null;let c=null;const o=K(e);if(r&&f.existsSync(o)){try{c=f.statSync(o).mtime.toISOString()}catch{c=null}}return{running:r,pid:r?i:null,started_at:c,schedules:{total:n.length,enabled:t}}}async function Ge(e,n){const t=n?.dir;const r=m.resolve(t??w);const i=n?.trigger??"cron";const c=e.timeout_ms||6e5;const o=(0,g.generateId)("tsk");if(e.app!==void 0){if(e.mode!=="command"||e.command!==g.APP_SCHEDULE_INERT_COMMAND){return{status:"blocked",reason:"APP_ROW_INVALID"}}if(n?.launchApp===void 0)return{status:"blocked",reason:"APP_LAUNCHER_UNAVAILABLE"}}else{if(!rt(e.mode))return{status:"blocked",reason:"MODE_UNSUPPORTED"};if(i==="app_enable")return{status:"blocked",reason:"TRIGGER_UNSUPPORTED"}}let s;if(!e.allow_concurrent){const u=bn(e.id,o,c,t);if(!u.acquired){console.log(`[${y()}] Schedule ${e.id} already running (task ${u.runningTaskId}), skipping`);return{status:"conflict",runningTaskId:u.runningTaskId,reason:u.reason}}s=u.lease}const a=()=>{if(s){Fe(e.id,s.leaseId,t);s=void 0}dn(o)};if(!un(o,e.id,c)){console.log(`[${y()}] Schedule ${e.id} blocked: could not durably record the update-admission reservation`);a();return{status:"blocked",reason:"ADMISSION_UNKNOWN"}}if((0,g.isDurablyQuiescing)()){console.log(`[${y()}] Schedule ${e.id} blocked: a desktop update is draining active work`);a();return{status:"blocked",reason:"UPDATE_QUIESCING"}}try{const u=I(r);const S=new Date().toISOString();if(n?.occurrenceKey!==void 0){const h=u.claimOccurrence(e.id,{occurrenceKey:n.occurrenceKey,startedAt:S});if(!h.claimed){a();console.log(`[${y()}] Schedule ${e.id} occurrence ${n.occurrenceKey} already claimed, skipping`);return{status:"conflict",runningTaskId:"",reason:"occurrence_claim"}}}else{if(u.get(e.id)){u.recordStarted(e.id,{startedAt:S})}}}catch(u){a();throw u}let d;let l;if(e.app!==void 0){let u;try{u=await n.launchApp(e);if(u.kind!=="spawn")it(r,e,u)}catch(S){he(r,e.id,"APP_LAUNCH_FAILED");a();throw S}if(u.kind!=="spawn"){a();const S=u.kind==="refresh"?"APP_SCHEDULE_REFRESHED":u.code;console.log(`[${y()}] Schedule ${e.id} app row ${u.kind}: ${S}`);return{status:"skipped",reason:S,lifecycle:u.kind}}d=u.child;l=typeof u.settle==="function"?u.settle:void 0}const P=e.working_directory||n?.dir||(0,x.getProjectRoot)();const D=e.mode;const _=D==="execute"?"x":"q";const T=n?.overrides?.model??e.model;const b=n?.overrides?.provider??e.provider;console.log(`[${y()}] Executing schedule ${e.id}: ${e.name??e.cron} (task ${o})`);console.log(`[${y()}] Command: ${e.command}`);const A=(0,g.withoutCrewxRootContext)({...process.env,...e.env,CREWX_TRACE_ID:o});delete A["CLAUDECODE"];if(!e.env||!("CREWX_CONFIG"in e.env)){delete A["CREWX_CONFIG"]}A["CREWX_WORKSPACE"]=P;const E=[_,e.command];if(T){E.push("--model",T)}if(D!=="command"&&b){E.push("--provider",b)}E.push("--metadata",JSON.stringify({trigger:i,schedule_id:e.id}));let p;try{if(d!==void 0){p=d;if(p.pid===void 0)throw new Error("App launcher returned a child that did not start")}else if(e.app!==void 0){throw new Error("managed row has no launched child")}else if(D==="command"){p=(0,j.spawn)("sh",["-c",e.command],{cwd:P,stdio:"ignore",windowsHide:true,detached:true,env:A})}else{const u=(0,g.resolveCrewxExecutable)({env:A,cwd:P});if(!u.ok){const R=new Error((0,g.formatCrewxExecutableFailure)(u));Object.assign(R,{code:"CLI_UNRESOLVABLE"});throw R}const S=(0,g.createCrewxChildInvocation)(u,{baseEnv:A});const h=(0,g.resolveWindowsSpawnProgram)({command:S.command,env:S.env,cwd:P,packageName:"crewx",allowShellFallback:true});const v=(0,g.materializeWindowsSpawnProgram)(h,[...S.args,...E]);p=(0,j.spawn)(v.command,v.argv,{cwd:P,stdio:"ignore",windowsHide:v.windowsHide,detached:true,env:S.env,shell:v.shell??false})}if(d===void 0){await new Promise((u,S)=>{p.once("spawn",()=>u());p.once("error",h=>S(h))})}}catch(u){const S=u;console.error(`[${y()}] Schedule ${e.id} failed to spawn: ${S.message}`);if(e.app!==void 0)he(r,e.id,"APP_SPAWN_FAILED");a();throw S}if(s&&p.pid){const u=Un(s,p.pid,He(p.pid));if(!u){const S=new Error("Cron execution lease was replaced before child ownership was established");console.error(`[${y()}] Schedule ${e.id} failed to hand off its execution lease: ${S.message}`);try{p.kill("SIGTERM")}catch{}if(e.app!==void 0)he(r,e.id,"APP_HANDOFF_FAILED");a();throw S}}p.unref();const $=new Promise(u=>{let S=false;let h=false;let v;let R;let Ee=false;const De=()=>{if(R!==void 0||!p.stdout&&!p.stderr)return;R=setTimeout(()=>{Ee=true;p.stdout?.destroy();p.stderr?.destroy()},Xn);if(typeof R==="object"&&R!==null&&"unref"in R){R.unref()}};if(e.app!==void 0){p.once("exit",De);if(p.exitCode!==null||p.signalCode!==null)De()}const Oe=setTimeout(()=>{S=true;console.log(`[${y()}] Schedule ${e.id} timeout, killing process`);try{p.kill("SIGTERM")}catch(L){const ie=L;console.error(`Failed to kill process: ${ie.message}`)}v=setTimeout(()=>{try{p.kill("SIGKILL")}catch{}},Te);if(typeof v==="object"&&v!==null&&"unref"in v){v.unref()}if(!h){h=true;u({success:false,error:"Timeout"})}},c);p.on("close",(L,ie)=>{clearTimeout(Oe);if(v)clearTimeout(v);if(R)clearTimeout(R);a();const U=e.app!==void 0&&!S?Zn({code:L,signal:ie},l,Ee):void 0;const Je=new Date().toISOString();const Xe=S?"timeout":U!==void 0?U.outcome:L===0?"success":"failure";const Ze=e.app===void 0?void 0:S?Jn:U?.outcome==="failure"?U.code:void 0;try{I(r).recordFinished(e.id,{finishedAt:Je,outcome:Xe,errorCode:Ze})}catch(ce){const Ye=ce;if(!(ce instanceof O.CronStoreError&&ce.code==="CRON_SCHEDULE_NOT_FOUND")){console.error(`[${y()}] Failed to record schedule ${e.id} completion: ${Ye.message}`)}}if(S){console.log(`[${y()}] Schedule ${e.id} terminated after timeout`);if(!h){h=true;u({success:false,error:"Timeout"})}}else if(U!==void 0){if(U.outcome==="success"){console.log(`[${y()}] Schedule ${e.id} completed successfully`);if(!h){h=true;u({success:true})}}else{console.log(`[${y()}] Schedule ${e.id} failed with ${U.code}`);if(!h){h=true;u({success:false,error:U.code})}}}else if(L===0){console.log(`[${y()}] Schedule ${e.id} completed successfully`);if(!h){h=true;u({success:true})}}else{console.log(`[${y()}] Schedule ${e.id} failed with code ${L}`);if(!h){h=true;u({success:false,error:`Exit code: ${L}`})}}});p.on("error",L=>{clearTimeout(Oe);console.error(`[${y()}] Schedule ${e.id} error: ${L.message}`);if(!h){h=true;u({success:false,error:L.message})}})});return{status:"running",taskId:o,done:$}}const Jn="APP_TIMEOUT";const Xn=5e3;function Zn(e,n,t){if(e.signal!==null)return X("APP_SIGNALED");if(e.code!==0)return X("APP_EXIT_NONZERO");if(t)return X("APP_STDIO_NOT_DRAINED");if(n===void 0)return X("APP_VERDICT_FAILED");let r;try{r=n(e)}catch{return X("APP_VERDICT_FAILED")}return et(r)}const Yn=64;const Qn=3;function et(e){try{if(typeof e==="object"&&e!==null&&!Array.isArray(e)&&typeof e.then!=="function"){const n=Object.keys(e);const{outcome:t,code:r}=e;if(t==="success"&&n.length===1)return{outcome:"success"};if(t==="failure"&&n.length===2&&n.includes("code")){return(0,g.isAppScheduleCode)(r)?{outcome:"failure",code:r}:_e(e,"APP_OUTPUT_INVALID")}}return _e(e,"APP_VERDICT_FAILED")}catch{return _e(e,"APP_VERDICT_FAILED")}}function _e(e,n){nt(e);return X(n)}const oe=()=>void 0;function nt(e){const n=[{value:e,depth:0}];for(let t=0;t<Yn&&n.length>0;t+=1){const{value:r,depth:i}=n.shift();if(typeof r!=="object"&&typeof r!=="function"||r===null)continue;try{if(Object.getPrototypeOf(r)===Promise.prototype){Promise.prototype.then.call(r,oe,oe);continue}const c=r.then;if(typeof c==="function"){c.call(r,oe,oe);continue}if(i<Qn){for(const o of Object.keys(r)){const s=Object.getOwnPropertyDescriptor(r,o);if(s!==void 0&&"value"in s)n.push({value:s.value,depth:i+1})}}}catch{}}}function X(e){return{outcome:"failure",code:e}}const tt=["query","execute","command"];function rt(e){return tt.includes(e)}function ot(e){return{appId:e.appId,actionId:e.actionId,source:e.source}}function it(e,n,t){const r=I(e);const i=ot(n.app);const c={cron:n.cron,timeout_ms:n.timeout_ms,name:n.name};switch(t.kind){case"skip":{const o=r.setAppSchedule(i,c).schedule;r.recordSkipped(o.id,{outcome:t.code,errorCode:t.code});return}case"disable":{const o=r.setAppSchedule(i,c,false).schedule;r.recordSkipped(o.id,{outcome:t.code,errorCode:t.code});return}case"remove":r.removeAppSchedule(i.appId);return;case"refresh":{const o=r.setAppSchedule(t.owner,t.template).schedule;r.recordSkipped(o.id,{outcome:"APP_SCHEDULE_REFRESHED",errorCode:null});return}}}function he(e,n,t){try{I(e).recordSkipped(n,{outcome:t,errorCode:t})}catch(r){if(r instanceof O.CronStoreError&&r.code==="CRON_SCHEDULE_NOT_FOUND")return;console.error(`[${y()}] Schedule ${n} could not record its ${t} outcome: ${r.message}`)}}function qe(e,n){if(!n||!V(n)){return`${e.getFullYear()}-${String(e.getMonth()+1).padStart(2,"0")}-${String(e.getDate()).padStart(2,"0")} ${String(e.getHours()).padStart(2,"0")}:${String(e.getMinutes()).padStart(2,"0")}`}const t=new Intl.DateTimeFormat("en-CA",{timeZone:n,year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hour12:false});return t.format(e)}function Ve(e,n){return`${e.trim()}|${n}`}async function ge(e){if((0,g.isDurablyQuiescing)()){console.log(`[${y()}] Daemon tick skipped: a desktop update is draining active work`);return}const n=new Date;const t=I(e??w).list();const r=t.filter(i=>Q(i));if(r.length===0){return}for(const i of r){if(Me(i.cron,n,i.timezone)){const c=qe(n,i.timezone);const o=Ve(i.cron,c);try{await Ge(i,{dir:e,occurrenceKey:o})}catch(s){const a=s;console.error(`[${y()}] Schedule ${i.id} failed to spawn: ${a.message}`)}}}}var we=require("./runtime");Object.defineProperty(exports,"createCronRuntime",{enumerable:true,get:function(){return we.createCronRuntime}});Object.defineProperty(exports,"CRON_RUNTIME_PROCESS_LEASE_FILENAME",{enumerable:true,get:function(){return we.CRON_RUNTIME_PROCESS_LEASE_FILENAME}});Object.defineProperty(exports,"scheduledPeriodicAvailability",{enumerable:true,get:function(){return we.scheduledPeriodicAvailability}});function ct(e){je(process.pid,e);let n=false;const t=()=>{if(n)return;n=true;const c=B(e);if(c===process.pid){te(e)}};process.on("SIGTERM",()=>{t();process.exit(0)});process.on("SIGINT",()=>{t();process.exit(0)});process.on("exit",t);console.log(`[${y()}] Cron daemon starting in foreground mode`);console.log(`[${y()}] Check interval: 1 minute`);console.log(`[${y()}] Press Ctrl+C to stop
4
+ `);const r=I(e??w).list();const i=r.filter(c=>Q(c)).length;console.log(`[${y()}] Active schedules: ${i}/${r.length}
5
+ `);void ge(e);setInterval(()=>{void ge(e)},ln)}function at(e,n,t,r){if(t.timezone&&!V(t.timezone)){throw new Error(`Invalid timezone: ${t.timezone}`)}Re(n);const i={id:(0,g.generateId)("cron"),cron:e,command:n,mode:de(t.mode??"execute"),enabled:true,name:t.name??null,working_directory:t.dir??null,timezone:t.timezone??null,timeout_ms:parseInt(t.timeout??"600000",10),allow_concurrent:t.allowConcurrent??false,created_at:new Date().toISOString(),updated_at:new Date().toISOString(),run_count:0};if(t.model){i.model=t.model}if(t.provider){i.provider=t.provider}i.next_run=M(e,i.timezone);return I(r??w).create(i)}function st(e,n){const t=I(n??w).remove(e);const r=(0,x.findBuiltinByName)(t.name);if(r){Pn(r.builtin_id,n)}return t}function ut(e,n){return I(n??w).toggle(e,true)}function dt(e,n){return I(n??w).toggle(e,false)}function lt(e,n,t){if(n.timezone!==void 0&&n.timezone!==null&&!V(n.timezone)){throw new Error(`Invalid timezone: ${n.timezone}`)}const r=I(t??w);const i=r.get(e);if(!i){throw new Error(`Schedule not found: ${e}`)}const c=[];const o={};if(n.cron!==void 0){o.cron=n.cron;c.push(`cron: ${n.cron}`)}if(n.command!==void 0){Re(n.command);o.command=n.command;c.push(`command: ${n.command}`)}if(n.name!==void 0){o.name=n.name;c.push(`name: ${n.name}`)}if(n.mode!==void 0){o.mode=de(n.mode);c.push(`mode: ${o.mode}`)}if(n.timezone!==void 0){o.timezone=n.timezone;c.push(`timezone: ${n.timezone}`)}if(n.working_directory!==void 0){o.working_directory=n.working_directory;c.push(`working_directory: ${n.working_directory}`)}if(n.timeout_ms!==void 0){o.timeout_ms=n.timeout_ms;c.push(`timeout: ${n.timeout_ms}ms`)}if(n.allow_concurrent!==void 0){o.allow_concurrent=n.allow_concurrent;c.push(`allow_concurrent: ${n.allow_concurrent}`)}if(n.model!==void 0){if(n.model===null){o.model=null;c.push("model: (cleared, reverts to agent default)")}else{o.model=n.model;c.push(`model: ${n.model}`)}}if(n.provider!==void 0){if(n.provider===null){o.provider=null;c.push("provider: (cleared, reverts to agent default)")}else{o.provider=n.provider;c.push(`provider: ${n.provider}`)}}return{schedule:r.update(i.id,o),changes:c}}exports.DATA_PATHS={DATA_DIR:H,SCHEDULES_FILE:nn,LOG_FILE:tn,LOCK_FILE:rn,USAGE_LOG_FILE:on,PID_FILE:cn,BUILTINS_META_FILE:an};