@agentpro/scroll 0.0.0-stage → 2.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.
- package/AGENTS.md +55 -0
- package/LICENSE +21 -0
- package/README.md +293 -2
- package/SPEC.md +1079 -0
- package/bin/scroll.js +8 -0
- package/docs/prompt-caching.md +128 -0
- package/index.js +33 -0
- package/lib/audit.js +358 -0
- package/lib/auth.js +849 -0
- package/lib/chat-sdk/admin-controller.js +289 -0
- package/lib/chat-sdk/debouncer.js +94 -0
- package/lib/chat-sdk/group-grants.js +150 -0
- package/lib/chat-sdk/index.js +222 -0
- package/lib/chat-sdk/long-task-runner.js +413 -0
- package/lib/chat-session.js +1788 -0
- package/lib/chat-telemetry.js +557 -0
- package/lib/codex-app-server.js +851 -0
- package/lib/digest.js +112 -0
- package/lib/effects.js +157 -0
- package/lib/eval.js +408 -0
- package/lib/grounding.js +356 -0
- package/lib/installers.js +176 -0
- package/lib/knowledge.js +335 -0
- package/lib/ledger.js +38 -0
- package/lib/loop.js +197 -0
- package/lib/mcp-client.js +926 -0
- package/lib/memory-cli.js +66 -0
- package/lib/memory-tracker.js +1472 -0
- package/lib/memory.js +1279 -0
- package/lib/people-cli.js +81 -0
- package/lib/people.js +534 -0
- package/lib/permissions.js +272 -0
- package/lib/providers.js +1572 -0
- package/lib/runtime.js +928 -0
- package/lib/sandbox.js +744 -0
- package/lib/scheduler.js +402 -0
- package/lib/scroll.js +1225 -0
- package/lib/skill-cli.js +120 -0
- package/lib/skill-eval.js +156 -0
- package/lib/skills.js +860 -0
- package/lib/style.js +126 -0
- package/lib/tool-retriever.js +489 -0
- package/lib/tools.js +1833 -0
- package/lib/user.js +711 -0
- package/lib/vault.js +151 -0
- package/package.json +88 -4
- package/templates/agent/IDENTITY.md +56 -0
- package/templates/agent/SOUL.md +23 -0
- package/templates/agent/TOOLS.md +25 -0
- package/templates/agent/evals/case-02-citation-discipline.md +29 -0
- package/templates/agent/evals/case-03-boundary.md +27 -0
- package/templates/agent/evals/case-example.md +43 -0
- package/templates/agent/hard-rules.md +11 -0
- package/templates/agent/memory/lessons.md +5 -0
- package/templates/agent/memory/log/.gitkeep +0 -0
- package/templates/external-safety-contract.md +5 -0
- package/templates/long-task-planner.md +17 -0
- package/templates/work/LOOP.md +37 -0
- package/templates/work/WORK.md +50 -0
- package/types/chat.d.ts +61 -0
- package/types/index.d.ts +112 -0
package/lib/scheduler.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// SCROLL 2.0 -- §49 Timezone-Aware Cron, Heartbeat & Reminder Scheduler.
|
|
2
|
+
//
|
|
3
|
+
// Features:
|
|
4
|
+
// - Standard 5-field cron expressions (`min hour dom month dow`) evaluated in any IANA timezone
|
|
5
|
+
// (e.g. `Asia/Ho_Chi_Minh`, `UTC`, `America/New_York`) via native `Intl.DateTimeFormat`.
|
|
6
|
+
// - Interval (`everyMs`) and one-shot (`at` ISO timestamp) reminders.
|
|
7
|
+
// - Atomic `0600` JSON persistence outside git.
|
|
8
|
+
// - Exponential backoff on transient / 429 failures.
|
|
9
|
+
// - Global kill-switch (`paused`) and per-job `maxDailyRuns` budget ceiling.
|
|
10
|
+
// - Owner DM authority: `canManageJob(actor, job)` permits a verified `owner`/`admin` in DM
|
|
11
|
+
// to inspect, pause, edit, or delete ANY group's scheduled job.
|
|
12
|
+
//
|
|
13
|
+
// Zero runtime dependencies. Node >= 20 built-ins only.
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import crypto from 'node:crypto';
|
|
17
|
+
|
|
18
|
+
export const SCHEDULER_SCHEMA = 'scroll-scheduler/1';
|
|
19
|
+
|
|
20
|
+
function parseCronField(expr, min, max, { mapSevenToZero = false } = {}) {
|
|
21
|
+
const allowed = new Set();
|
|
22
|
+
const parts = String(expr || '').trim().split(',').filter(Boolean);
|
|
23
|
+
if (!parts.length) throw new Error(`invalid_cron_field:${expr}`);
|
|
24
|
+
for (const part of parts) {
|
|
25
|
+
const [rangePart, stepPart] = part.split('/');
|
|
26
|
+
const step = stepPart !== undefined ? Number(stepPart) : 1;
|
|
27
|
+
if (!Number.isInteger(step) || step < 1) throw new Error(`invalid_cron_step:${part}`);
|
|
28
|
+
let start, end;
|
|
29
|
+
if (rangePart === '*') {
|
|
30
|
+
start = min;
|
|
31
|
+
end = max;
|
|
32
|
+
} else if (rangePart.includes('-')) {
|
|
33
|
+
const [a, b] = rangePart.split('-').map(Number);
|
|
34
|
+
if (!Number.isInteger(a) || !Number.isInteger(b) || a > b) throw new Error(`invalid_cron_range:${part}`);
|
|
35
|
+
start = a;
|
|
36
|
+
end = b;
|
|
37
|
+
} else {
|
|
38
|
+
const v = Number(rangePart);
|
|
39
|
+
if (!Number.isInteger(v)) throw new Error(`invalid_cron_value:${part}`);
|
|
40
|
+
start = v;
|
|
41
|
+
end = v;
|
|
42
|
+
}
|
|
43
|
+
if (start < min || end > (mapSevenToZero ? 7 : max)) throw new Error(`cron_out_of_bounds:${part}`);
|
|
44
|
+
for (let i = start; i <= end; i += step) {
|
|
45
|
+
allowed.add(mapSevenToZero && i === 7 ? 0 : i);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return allowed;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function parseCron(expression) {
|
|
52
|
+
const fields = String(expression || '').trim().split(/\s+/);
|
|
53
|
+
if (fields.length !== 5) {
|
|
54
|
+
throw new Error(`cron_must_have_5_fields:${expression}`);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
expression: fields.join(' '),
|
|
58
|
+
minute: parseCronField(fields[0], 0, 59),
|
|
59
|
+
hour: parseCronField(fields[1], 0, 23),
|
|
60
|
+
dom: parseCronField(fields[2], 1, 31),
|
|
61
|
+
month: parseCronField(fields[3], 1, 12),
|
|
62
|
+
dow: parseCronField(fields[4], 0, 6, { mapSevenToZero: true }),
|
|
63
|
+
domWildcard: fields[2] === '*',
|
|
64
|
+
dowWildcard: fields[4] === '*',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const WEEKDAY_MAP = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };
|
|
69
|
+
|
|
70
|
+
export function zonedParts(date, timeZone = 'UTC') {
|
|
71
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
72
|
+
const fmt = new Intl.DateTimeFormat('en-US', {
|
|
73
|
+
timeZone,
|
|
74
|
+
year: 'numeric',
|
|
75
|
+
month: '2-digit',
|
|
76
|
+
day: '2-digit',
|
|
77
|
+
weekday: 'short',
|
|
78
|
+
hour: '2-digit',
|
|
79
|
+
minute: '2-digit',
|
|
80
|
+
hourCycle: 'h23',
|
|
81
|
+
});
|
|
82
|
+
const map = {};
|
|
83
|
+
for (const p of fmt.formatToParts(d)) map[p.type] = p.value;
|
|
84
|
+
return {
|
|
85
|
+
year: Number(map.year),
|
|
86
|
+
month: Number(map.month),
|
|
87
|
+
day: Number(map.day),
|
|
88
|
+
hour: Number(map.hour) % 24,
|
|
89
|
+
minute: Number(map.minute),
|
|
90
|
+
dow: WEEKDAY_MAP[map.weekday] ?? 0,
|
|
91
|
+
dateKey: `${map.year}-${map.month}-${map.day}`,
|
|
92
|
+
minuteKey: `${map.year}-${map.month}-${map.day}T${map.hour}:${map.minute}`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function matchesCron(parsed, date, timeZone = 'UTC') {
|
|
97
|
+
const p = zonedParts(date, timeZone);
|
|
98
|
+
if (!parsed.minute.has(p.minute)) return false;
|
|
99
|
+
if (!parsed.hour.has(p.hour)) return false;
|
|
100
|
+
if (!parsed.month.has(p.month)) return false;
|
|
101
|
+
const domMatch = parsed.dom.has(p.day);
|
|
102
|
+
const dowMatch = parsed.dow.has(p.dow);
|
|
103
|
+
if (!parsed.domWildcard && !parsed.dowWildcard) return domMatch || dowMatch;
|
|
104
|
+
return domMatch && dowMatch;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function writeAtomic(filePath, obj) {
|
|
108
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
109
|
+
const tmp = `${filePath}.tmp.${process.pid}.${crypto.randomBytes(4).toString('hex')}`;
|
|
110
|
+
fs.writeFileSync(tmp, JSON.stringify(obj, null, 2) + '\n', { mode: 0o600 });
|
|
111
|
+
fs.renameSync(tmp, filePath);
|
|
112
|
+
try { fs.chmodSync(filePath, 0o600); } catch { /* ignore */ }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function canManageJob(actor, job) {
|
|
116
|
+
if (!actor || !job) return false;
|
|
117
|
+
const roles = Array.isArray(actor.roles) ? actor.roles.map((r) => String(r).toLowerCase()) : [];
|
|
118
|
+
const isOwnerOrAdmin = roles.includes('owner') || roles.includes('admin');
|
|
119
|
+
// Verified owner/admin in DM can manage ANY job (including group crons)
|
|
120
|
+
if (actor.isInternal && isOwnerOrAdmin && actor.isDm) return true;
|
|
121
|
+
// Owner/admin in same chat or explicit job creator
|
|
122
|
+
if (actor.isInternal && isOwnerOrAdmin) return true;
|
|
123
|
+
if (actor.isInternal && actor.id && job.ownerId && actor.id === job.ownerId) return true;
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function createScheduler({
|
|
128
|
+
filePath = null,
|
|
129
|
+
defaultTz = 'UTC',
|
|
130
|
+
clock = () => Date.now(),
|
|
131
|
+
onTrigger = null,
|
|
132
|
+
tickIntervalMs = 15_000,
|
|
133
|
+
baseRetryMs = 30_000,
|
|
134
|
+
maxRetryMs = 600_000,
|
|
135
|
+
} = {}) {
|
|
136
|
+
const absPath = filePath ? path.resolve(filePath) : null;
|
|
137
|
+
let timer = null;
|
|
138
|
+
const runningJobs = new Set();
|
|
139
|
+
|
|
140
|
+
function empty() {
|
|
141
|
+
return { schema: SCHEDULER_SCHEMA, paused: false, jobs: {} };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function load() {
|
|
145
|
+
if (!absPath || !fs.existsSync(absPath)) return empty();
|
|
146
|
+
const raw = JSON.parse(fs.readFileSync(absPath, 'utf8'));
|
|
147
|
+
return {
|
|
148
|
+
schema: raw.schema || SCHEDULER_SCHEMA,
|
|
149
|
+
paused: Boolean(raw.paused),
|
|
150
|
+
jobs: raw.jobs && typeof raw.jobs === 'object' ? { ...raw.jobs } : {},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let state = load();
|
|
155
|
+
|
|
156
|
+
function persist() {
|
|
157
|
+
if (absPath) writeAtomic(absPath, state);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function validateTz(tz) {
|
|
161
|
+
try { new Intl.DateTimeFormat('en-US', { timeZone: tz }); return tz; }
|
|
162
|
+
catch { throw new Error(`invalid_timezone:${tz}`); }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function addJob({
|
|
166
|
+
id = null,
|
|
167
|
+
name = 'Scheduled job',
|
|
168
|
+
cron = null,
|
|
169
|
+
everyMs = null,
|
|
170
|
+
at = null,
|
|
171
|
+
tz = defaultTz,
|
|
172
|
+
targetChatId = null,
|
|
173
|
+
agentName = null,
|
|
174
|
+
prompt = '',
|
|
175
|
+
enabled = true,
|
|
176
|
+
maxDailyRuns = 24,
|
|
177
|
+
maxRetries = 3,
|
|
178
|
+
ownerId = null,
|
|
179
|
+
metadata = {},
|
|
180
|
+
} = {}) {
|
|
181
|
+
const validTz = validateTz(tz || defaultTz);
|
|
182
|
+
if (!cron && !everyMs && !at) {
|
|
183
|
+
throw new Error('addJob: one of cron, everyMs, or at is required');
|
|
184
|
+
}
|
|
185
|
+
if (cron) parseCron(cron); // validates syntax upfront
|
|
186
|
+
if (everyMs != null && (!Number.isFinite(Number(everyMs)) || Number(everyMs) < 1000)) {
|
|
187
|
+
throw new Error('addJob: everyMs must be >= 1000');
|
|
188
|
+
}
|
|
189
|
+
if (at && Number.isNaN(Date.parse(at))) {
|
|
190
|
+
throw new Error(`addJob: invalid ISO timestamp "${at}"`);
|
|
191
|
+
}
|
|
192
|
+
const jobId = id || `job_${crypto.randomBytes(4).toString('hex')}`;
|
|
193
|
+
const nowIso = new Date(clock()).toISOString();
|
|
194
|
+
const job = {
|
|
195
|
+
id: jobId,
|
|
196
|
+
name: String(name || jobId),
|
|
197
|
+
cron: cron ? String(cron).trim() : null,
|
|
198
|
+
everyMs: everyMs != null ? Number(everyMs) : null,
|
|
199
|
+
at: at ? new Date(at).toISOString() : null,
|
|
200
|
+
tz: validTz,
|
|
201
|
+
targetChatId: targetChatId != null ? String(targetChatId) : null,
|
|
202
|
+
agentName: agentName || null,
|
|
203
|
+
prompt: String(prompt || ''),
|
|
204
|
+
enabled: Boolean(enabled),
|
|
205
|
+
maxDailyRuns: Math.max(1, Number(maxDailyRuns || 24)),
|
|
206
|
+
maxRetries: Math.max(0, Number(maxRetries ?? 3)),
|
|
207
|
+
ownerId: ownerId || null,
|
|
208
|
+
metadata: metadata || {},
|
|
209
|
+
createdAt: nowIso,
|
|
210
|
+
updatedAt: nowIso,
|
|
211
|
+
lastRunAt: null,
|
|
212
|
+
lastMinuteKey: null,
|
|
213
|
+
lastStatus: null,
|
|
214
|
+
lastError: null,
|
|
215
|
+
dailyDateKey: null,
|
|
216
|
+
dailyRuns: 0,
|
|
217
|
+
consecutiveFailures: 0,
|
|
218
|
+
retryAt: null,
|
|
219
|
+
};
|
|
220
|
+
state.jobs[jobId] = job;
|
|
221
|
+
persist();
|
|
222
|
+
return { ...job };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function updateJob(id, patch = {}) {
|
|
226
|
+
const cur = state.jobs[id];
|
|
227
|
+
if (!cur) throw new Error(`job_not_found:${id}`);
|
|
228
|
+
if (patch.cron) parseCron(patch.cron);
|
|
229
|
+
if (patch.tz) validateTz(patch.tz);
|
|
230
|
+
const next = {
|
|
231
|
+
...cur,
|
|
232
|
+
...patch,
|
|
233
|
+
id: cur.id,
|
|
234
|
+
updatedAt: new Date(clock()).toISOString(),
|
|
235
|
+
};
|
|
236
|
+
state.jobs[id] = next;
|
|
237
|
+
persist();
|
|
238
|
+
return { ...next };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function removeJob(id) {
|
|
242
|
+
if (!state.jobs[id]) return { ok: false, removed: false };
|
|
243
|
+
delete state.jobs[id];
|
|
244
|
+
persist();
|
|
245
|
+
return { ok: true, removed: true };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function getJob(id) {
|
|
249
|
+
return state.jobs[id] ? { ...state.jobs[id] } : null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function listJobs({ chatId = null, agentName = null, enabledOnly = false } = {}) {
|
|
253
|
+
return Object.values(state.jobs)
|
|
254
|
+
.filter((j) => (!chatId || j.targetChatId === String(chatId))
|
|
255
|
+
&& (!agentName || j.agentName === agentName)
|
|
256
|
+
&& (!enabledOnly || j.enabled))
|
|
257
|
+
.sort((a, b) => String(a.id).localeCompare(String(b.id)))
|
|
258
|
+
.map((j) => ({ ...j }));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function setPaused(paused) {
|
|
262
|
+
state.paused = Boolean(paused);
|
|
263
|
+
persist();
|
|
264
|
+
return { paused: state.paused };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function isJobDue(job, nowMs) {
|
|
268
|
+
if (!job.enabled) return { due: false, reason: 'disabled' };
|
|
269
|
+
if (runningJobs.has(job.id)) return { due: false, reason: 'running' };
|
|
270
|
+
const parts = zonedParts(nowMs, job.tz || defaultTz);
|
|
271
|
+
const runsToday = job.dailyDateKey === parts.dateKey ? Number(job.dailyRuns || 0) : 0;
|
|
272
|
+
if (runsToday >= Number(job.maxDailyRuns || 24)) {
|
|
273
|
+
return { due: false, reason: 'daily_budget_cap' };
|
|
274
|
+
}
|
|
275
|
+
// Retry window takes precedence when a previous attempt failed
|
|
276
|
+
if (job.retryAt) {
|
|
277
|
+
const retryMs = Date.parse(job.retryAt);
|
|
278
|
+
if (Number.isFinite(retryMs) && nowMs >= retryMs) {
|
|
279
|
+
return { due: true, reason: 'retry', parts };
|
|
280
|
+
}
|
|
281
|
+
return { due: false, reason: 'retry_backoff' };
|
|
282
|
+
}
|
|
283
|
+
if (job.at) {
|
|
284
|
+
const atMs = Date.parse(job.at);
|
|
285
|
+
if (!job.lastRunAt && Number.isFinite(atMs) && nowMs >= atMs) {
|
|
286
|
+
return { due: true, reason: 'oneshot', parts };
|
|
287
|
+
}
|
|
288
|
+
return { due: false, reason: 'oneshot_not_due' };
|
|
289
|
+
}
|
|
290
|
+
if (job.everyMs) {
|
|
291
|
+
const lastMs = job.lastRunAt ? Date.parse(job.lastRunAt) : 0;
|
|
292
|
+
if (!lastMs || nowMs - lastMs >= job.everyMs) {
|
|
293
|
+
return { due: true, reason: 'interval', parts };
|
|
294
|
+
}
|
|
295
|
+
return { due: false, reason: 'interval_wait' };
|
|
296
|
+
}
|
|
297
|
+
if (job.cron) {
|
|
298
|
+
if (job.lastMinuteKey === parts.minuteKey) return { due: false, reason: 'already_ran_minute' };
|
|
299
|
+
const parsed = parseCron(job.cron);
|
|
300
|
+
if (matchesCron(parsed, nowMs, job.tz || defaultTz)) {
|
|
301
|
+
return { due: true, reason: 'cron', parts };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return { due: false, reason: 'not_matched' };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function executeJob(job, reason, parts, handler) {
|
|
308
|
+
runningJobs.add(job.id);
|
|
309
|
+
const live = state.jobs[job.id];
|
|
310
|
+
const nowIso = new Date(clock()).toISOString();
|
|
311
|
+
try {
|
|
312
|
+
if (live.dailyDateKey !== parts.dateKey) {
|
|
313
|
+
live.dailyDateKey = parts.dateKey;
|
|
314
|
+
live.dailyRuns = 0;
|
|
315
|
+
}
|
|
316
|
+
live.dailyRuns += 1;
|
|
317
|
+
live.lastRunAt = nowIso;
|
|
318
|
+
live.lastMinuteKey = parts.minuteKey;
|
|
319
|
+
live.retryAt = null;
|
|
320
|
+
persist();
|
|
321
|
+
|
|
322
|
+
const out = typeof handler === 'function' ? await handler({ ...live }, { reason, parts }) : null;
|
|
323
|
+
live.lastStatus = 'ok';
|
|
324
|
+
live.lastError = null;
|
|
325
|
+
live.consecutiveFailures = 0;
|
|
326
|
+
if (live.at && !live.cron && !live.everyMs) {
|
|
327
|
+
live.enabled = false; // one-shot completes once
|
|
328
|
+
}
|
|
329
|
+
live.updatedAt = new Date(clock()).toISOString();
|
|
330
|
+
persist();
|
|
331
|
+
return { jobId: live.id, ok: true, output: out };
|
|
332
|
+
} catch (err) {
|
|
333
|
+
const msg = String(err?.message || err);
|
|
334
|
+
live.lastStatus = 'error';
|
|
335
|
+
live.lastError = msg.slice(0, 300);
|
|
336
|
+
live.consecutiveFailures = Number(live.consecutiveFailures || 0) + 1;
|
|
337
|
+
if (live.consecutiveFailures <= Number(live.maxRetries ?? 3)) {
|
|
338
|
+
const delay = err?.retryAfterMs
|
|
339
|
+
? Math.max(1000, Number(err.retryAfterMs))
|
|
340
|
+
: Math.min(maxRetryMs, baseRetryMs * Math.pow(2, live.consecutiveFailures - 1));
|
|
341
|
+
live.retryAt = new Date(clock() + delay).toISOString();
|
|
342
|
+
} else {
|
|
343
|
+
live.retryAt = null;
|
|
344
|
+
}
|
|
345
|
+
live.updatedAt = new Date(clock()).toISOString();
|
|
346
|
+
persist();
|
|
347
|
+
return { jobId: live.id, ok: false, error: msg, retryAt: live.retryAt };
|
|
348
|
+
} finally {
|
|
349
|
+
runningJobs.delete(job.id);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async function tick({ handler = onTrigger } = {}) {
|
|
354
|
+
if (state.paused) return { paused: true, triggered: [], skipped: [] };
|
|
355
|
+
const nowMs = clock();
|
|
356
|
+
const triggered = [];
|
|
357
|
+
const skipped = [];
|
|
358
|
+
for (const job of Object.values(state.jobs)) {
|
|
359
|
+
const check = isJobDue(job, nowMs);
|
|
360
|
+
if (!check.due) {
|
|
361
|
+
if (check.reason === 'daily_budget_cap' || check.reason === 'retry_backoff') {
|
|
362
|
+
skipped.push({ jobId: job.id, reason: check.reason });
|
|
363
|
+
}
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const res = await executeJob(job, check.reason, check.parts, handler);
|
|
367
|
+
triggered.push(res);
|
|
368
|
+
}
|
|
369
|
+
return { paused: false, triggered, skipped };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function start({ handler = onTrigger } = {}) {
|
|
373
|
+
if (timer) return;
|
|
374
|
+
timer = setInterval(() => {
|
|
375
|
+
tick({ handler }).catch(() => {});
|
|
376
|
+
}, Math.max(1000, Number(tickIntervalMs || 15_000)));
|
|
377
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function stop() {
|
|
381
|
+
if (timer) {
|
|
382
|
+
clearInterval(timer);
|
|
383
|
+
timer = null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
filePath: absPath,
|
|
389
|
+
get paused() { return state.paused; },
|
|
390
|
+
addJob,
|
|
391
|
+
updateJob,
|
|
392
|
+
removeJob,
|
|
393
|
+
getJob,
|
|
394
|
+
listJobs,
|
|
395
|
+
setPaused,
|
|
396
|
+
canManageJob,
|
|
397
|
+
isJobDue,
|
|
398
|
+
tick,
|
|
399
|
+
start,
|
|
400
|
+
stop,
|
|
401
|
+
};
|
|
402
|
+
}
|