forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule-runner.js
More file actions
277 lines (247 loc) · 9.98 KB
/
Copy pathschedule-runner.js
File metadata and controls
277 lines (247 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// schedule-runner.js — Scan schedule-*.md files, match cron, build commands
const fs = require('fs');
const path = require('path');
const os = require('os');
const crypto = require('crypto');
const { shq } = require('./claude-cmd');
const CLAUDE_DIR = path.join(os.homedir(), '.claude');
const PROJECTS_DIR = path.join(CLAUDE_DIR, 'projects');
// Whitelists for schedule frontmatter CLI values. Schedule files are
// markdown in `<project>/.claude/commands/`; anything (including a compromised
// session) with write access to the project can author one, so we validate
// strictly rather than trusting the markdown.
const PERMISSION_MODES = new Set(['plan', 'acceptEdits', 'default', 'bypassPermissions']);
const ALLOWED_TOOL_NAMES = new Set([
'Bash', 'Read', 'Write', 'Edit', 'MultiEdit', 'Glob', 'Grep',
'WebFetch', 'WebSearch', 'TodoWrite', 'Task', 'NotebookEdit',
'BashOutput', 'KillShell', 'SlashCommand',
]);
const MODEL_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
const UUID_RE = /^[0-9a-fA-F-]{8,64}$/;
const BUDGET_RE = /^\d{1,6}(\.\d{1,4})?$/;
/** Parse YAML-like frontmatter from a markdown file (simple key: value parser). */
function parseFrontmatter(content) {
const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { meta: {}, body: content.trim() };
const meta = {};
let currentKey = null;
const nested = {};
for (const line of match[1].split('\n')) {
if (currentKey && line.match(/^\s+/) && line.includes(':')) {
const m = line.match(/^\s+([^:]+):\s*(.*)$/);
if (m && !m[1].trim().startsWith('#')) {
if (!nested[currentKey]) nested[currentKey] = {};
nested[currentKey][m[1].trim()] = m[2].trim();
}
continue;
}
const kv = line.match(/^([^:]+):\s*(.*)$/);
if (kv) {
const key = kv[1].trim();
const val = kv[2].trim();
if (val === '' || val === undefined) {
currentKey = key;
} else {
meta[key] = val;
currentKey = null;
}
}
}
for (const [k, v] of Object.entries(nested)) {
meta[k] = v;
}
return { meta, body: match[2].trim() };
}
// Check if a cron field matches a value. Supports *, ranges (1-5), lists (1,3,5), and steps.
function cronFieldMatches(field, value) {
if (field === '*') return true;
if (field.startsWith('*/')) {
const step = parseInt(field.slice(2), 10);
return value % step === 0;
}
if (field.includes(',')) {
return field.split(',').some(f => cronFieldMatches(f.trim(), value));
}
if (field.includes('-')) {
const [lo, hi] = field.split('-').map(Number);
return value >= lo && value <= hi;
}
return parseInt(field, 10) === value;
}
/** Check if a 5-field cron expression matches the current time. */
function cronMatches(cronExpr, now) {
const parts = cronExpr.trim().split(/\s+/);
if (parts.length !== 5) return false;
const [minute, hour, dom, month, dow] = parts;
return (
cronFieldMatches(minute, now.getMinutes()) &&
cronFieldMatches(hour, now.getHours()) &&
cronFieldMatches(dom, now.getDate()) &&
cronFieldMatches(month, now.getMonth() + 1) &&
cronFieldMatches(dow, now.getDay())
);
}
/** Scan all projects for schedule-*.md files and return parsed schedule objects. */
function scanSchedules(log) {
const schedules = [];
try {
if (!fs.existsSync(PROJECTS_DIR)) return schedules;
const folders = fs.readdirSync(PROJECTS_DIR, { withFileTypes: true })
.filter(d => d.isDirectory());
for (const folder of folders) {
const folderPath = path.join(PROJECTS_DIR, folder.name);
let projectPath = null;
try {
const jsonlFiles = fs.readdirSync(folderPath).filter(f => f.endsWith('.jsonl'));
for (const jf of jsonlFiles) {
const head = fs.readFileSync(path.join(folderPath, jf), 'utf8').slice(0, 4000);
for (const line of head.split('\n').filter(Boolean)) {
try {
const entry = JSON.parse(line);
if (entry.cwd) { projectPath = entry.cwd; break; }
} catch {}
}
if (projectPath) break;
}
} catch {}
if (!projectPath) continue;
const commandsDir = path.join(projectPath, '.claude', 'commands');
try {
if (!fs.existsSync(commandsDir)) continue;
const files = fs.readdirSync(commandsDir).filter(f => f.startsWith('schedule-') && f.endsWith('.md'));
for (const file of files) {
try {
const content = fs.readFileSync(path.join(commandsDir, file), 'utf8');
const { meta, body } = parseFrontmatter(content);
if (!meta.cron || !body) continue;
if (meta.enabled === 'false') continue;
schedules.push({
file, filePath: path.join(commandsDir, file),
projectPath, folder: folder.name,
name: meta.name || file, cron: meta.cron,
slug: meta.slug || file.replace(/^schedule-/, '').replace(/\.md$/, ''),
cli: meta.cli || {}, prompt: body,
});
} catch (err) {
if (log) log.warn(`[schedule] Failed to parse ${file}:`, err.message);
}
}
} catch {}
}
} catch (err) {
if (log) log.error('[schedule] Error scanning schedules:', err);
}
return schedules;
}
/** Create a pre-seeded JSONL session file with user message and slug for grouping. */
function createScheduleSession(schedule) {
const sessionId = crypto.randomUUID();
const timestamp = new Date().toISOString();
const claudeProjectDir = path.join(PROJECTS_DIR, schedule.folder);
fs.mkdirSync(claudeProjectDir, { recursive: true });
const jsonlPath = path.join(claudeProjectDir, `${sessionId}.jsonl`);
const msgId = crypto.randomUUID();
const lines = [
JSON.stringify({ type: 'user', parentUuid: null, uuid: msgId, sessionId, cwd: schedule.projectPath, slug: schedule.slug, timestamp, message: { role: 'user', content: 'Scheduled Task: ' + schedule.prompt } }),
];
fs.writeFileSync(jsonlPath, lines.join('\n') + '\n');
return { sessionId, jsonlPath };
}
/**
* Build a claude CLI command string for a scheduled task.
* Every value from the schedule's frontmatter is validated or shell-quoted
* before hitting the command line. Returns { ok: false, error } if any field
* fails validation — the caller must not dispatch the run in that case.
*/
function buildScheduleCommand(sessionId, schedule) {
if (!UUID_RE.test(sessionId || '')) {
return { ok: false, error: 'invalid sessionId' };
}
const cli = schedule.cli || {};
let cmd = `claude --resume ${shq(sessionId)} -p ${shq('Run the scheduled task')}`;
const permissionMode = cli['permission-mode'] || 'acceptEdits';
if (!PERMISSION_MODES.has(permissionMode)) {
return { ok: false, error: `invalid permission-mode: ${permissionMode}` };
}
cmd += ` --permission-mode ${shq(permissionMode)}`;
if (cli.model) {
if (!MODEL_RE.test(cli.model)) return { ok: false, error: `invalid model: ${cli.model}` };
cmd += ` --model ${shq(cli.model)}`;
}
if (cli['max-budget-usd']) {
const b = String(cli['max-budget-usd']);
if (!BUDGET_RE.test(b)) return { ok: false, error: `invalid max-budget-usd: ${b}` };
cmd += ` --max-budget-usd ${b}`;
}
const rawTools = cli['allowed-tools'] || 'Bash,Read,Write,Edit,Glob,Grep,WebFetch,WebSearch';
const tools = String(rawTools).split(',').map(t => t.trim()).filter(Boolean);
for (const t of tools) {
if (!ALLOWED_TOOL_NAMES.has(t)) return { ok: false, error: `invalid tool: ${t}` };
}
cmd += ` --allowedTools ${shq(tools.join(','))}`;
if (cli['append-system-prompt']) {
// Full shell-escape of the whole value rather than a quote-only replace.
cmd += ` --append-system-prompt ${shq(cli['append-system-prompt'])}`;
}
if (cli['add-dirs']) {
for (const dir of String(cli['add-dirs']).split(',').map(d => d.trim()).filter(Boolean)) {
// Require absolute paths so a schedule can't reach outside its project
// via a relative traversal.
if (!path.isAbsolute(dir)) {
return { ok: false, error: `add-dir must be absolute: ${dir}` };
}
cmd += ` --add-dir ${shq(path.resolve(dir))}`;
}
}
return { ok: true, cmd };
}
/**
* Start the cron loop. Checks every 60 seconds.
* @param {object} log - Logger
* @param {function} runCommand - Function to spawn a shell command: runCommand(cmd, cwd, name)
* @returns {function} stop - Call to stop the scheduler
*/
function startScheduler(log, runCommand) {
let running = true;
const runningTasks = new Set();
function tick() {
if (!running) return;
const now = new Date();
const schedules = scanSchedules(log);
for (const schedule of schedules) {
if (!cronMatches(schedule.cron, now)) continue;
const taskKey = `${schedule.folder}:${schedule.slug}`;
if (runningTasks.has(taskKey)) {
log.info(`[schedule] Skipping ${schedule.name} — still running from previous trigger`);
continue;
}
log.info(`[schedule] Triggering: ${schedule.name} (${schedule.cron})`);
try {
const { sessionId } = createScheduleSession(schedule);
const built = buildScheduleCommand(sessionId, schedule);
if (!built.ok) {
log.error(`[schedule] Refusing to run ${schedule.name}: ${built.error}`);
continue;
}
runningTasks.add(taskKey);
runCommand(built.cmd, schedule.projectPath, schedule.name, () => {
runningTasks.delete(taskKey);
});
} catch (err) {
log.error(`[schedule] Failed to run ${schedule.name}:`, err);
}
}
}
const msUntilNextMinute = (60 - new Date().getSeconds()) * 1000;
const initialTimer = setTimeout(() => {
tick();
const interval = setInterval(tick, 60 * 1000);
initialTimer._interval = interval;
}, msUntilNextMinute);
return function stop() {
running = false;
clearTimeout(initialTimer);
if (initialTimer._interval) clearInterval(initialTimer._interval);
};
}
module.exports = { parseFrontmatter, cronMatches, scanSchedules, startScheduler, createScheduleSession, buildScheduleCommand };