@chroxy/server 0.10.0 → 0.11.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/CONFIG.md +411 -14
- package/hooks/permission-hook.sh +253 -3
- package/package.json +8 -15
- package/sidecar/node_modules/.package-lock.json +29 -0
- package/sidecar/node_modules/ws/LICENSE +20 -0
- package/sidecar/node_modules/ws/README.md +548 -0
- package/sidecar/node_modules/ws/browser.js +8 -0
- package/sidecar/node_modules/ws/index.js +22 -0
- package/sidecar/node_modules/ws/lib/buffer-util.js +131 -0
- package/sidecar/node_modules/ws/lib/constants.js +19 -0
- package/sidecar/node_modules/ws/lib/event-target.js +292 -0
- package/sidecar/node_modules/ws/lib/extension.js +203 -0
- package/sidecar/node_modules/ws/lib/limiter.js +55 -0
- package/sidecar/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/sidecar/node_modules/ws/lib/receiver.js +706 -0
- package/sidecar/node_modules/ws/lib/sender.js +602 -0
- package/sidecar/node_modules/ws/lib/stream.js +161 -0
- package/sidecar/node_modules/ws/lib/subprotocol.js +62 -0
- package/sidecar/node_modules/ws/lib/validation.js +152 -0
- package/sidecar/node_modules/ws/lib/websocket-server.js +554 -0
- package/sidecar/node_modules/ws/lib/websocket.js +1393 -0
- package/sidecar/node_modules/ws/package.json +70 -0
- package/sidecar/node_modules/ws/wrapper.mjs +21 -0
- package/src/base-session.js +151 -6
- package/src/billing-budget.js +5 -1
- package/src/binary-provenance-trust.js +95 -0
- package/src/built-in-tools/file-ops.js +5 -4
- package/src/byok-mcp-client.js +1061 -13
- package/src/byok-mcp-config.js +1201 -4
- package/src/byok-mcp-fleet.js +374 -29
- package/src/byok-mcp-oauth-store.js +327 -0
- package/src/byok-mcp-oauth.js +635 -0
- package/src/byok-mcp-trust.js +254 -37
- package/src/byok-session.js +789 -13
- package/src/byok-tool-executor.js +20 -13
- package/src/checkpoint-manager.js +33 -6
- package/src/chroxy-host-metadata.js +122 -0
- package/src/claude-stream-parser.js +56 -0
- package/src/claude-tui/pty-driver.js +54 -14
- package/src/claude-tui-session.js +106 -7
- package/src/cli/config-cmd.js +2 -2
- package/src/cli/deploy-cmd.js +1 -1
- package/src/cli/init-cmd.js +1 -1
- package/src/cli/providers-cmd.js +3 -3
- package/src/cli/schedule-cmd.js +775 -0
- package/src/cli/server-cmd.js +16 -1
- package/src/cli/service-cmd.js +39 -19
- package/src/cli/shared.js +7 -4
- package/src/cli/tokens-cmd.js +213 -0
- package/src/cli/tunnel-cmd.js +1 -1
- package/src/cli-session.js +66 -10
- package/src/cli.js +4 -0
- package/src/codex-app-server-session.js +671 -39
- package/src/codex-session.js +121 -18
- package/src/config.js +453 -27
- package/src/control-room/integrations.js +16 -10
- package/src/control-room/skills-inventory.js +6 -1
- package/src/control-room/survey.js +32 -0
- package/src/credential-store.js +256 -10
- package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
- package/src/diagnostics.js +3 -0
- package/src/docker-sdk-session.js +16 -1
- package/src/docker-session.js +9 -0
- package/src/doctor.js +19 -8
- package/src/environments/backends/docker.js +8 -0
- package/src/environments/backends/k8s.js +9 -0
- package/src/event-ingest.js +19 -1
- package/src/event-normalizer.js +354 -19
- package/src/external-session-registry.js +0 -0
- package/src/gemini-session.js +19 -2
- package/src/github-webhook.js +188 -4
- package/src/handler-utils.js +42 -8
- package/src/handlers/checkpoint-handlers.js +126 -19
- package/src/handlers/control-room-handlers.js +48 -18
- package/src/handlers/conversation-handlers.js +127 -2
- package/src/handlers/credential-handlers.js +43 -2
- package/src/handlers/file-handlers.js +47 -3
- package/src/handlers/github-webhook-handlers.js +204 -0
- package/src/handlers/input-handlers.js +4 -0
- package/src/handlers/orchestration-handlers.js +238 -0
- package/src/handlers/scheduler-handlers.js +510 -0
- package/src/handlers/session-handlers.js +10 -2
- package/src/handlers/settings-handlers.js +510 -11
- package/src/handlers/token-handlers.js +11 -2
- package/src/http-routes.js +159 -0
- package/src/jsonl-reader.js +60 -6
- package/src/jsonl-subprocess-session.js +35 -6
- package/src/keychain.js +159 -12
- package/src/logger.js +182 -19
- package/src/notification-prefs.js +88 -6
- package/src/notifications/discord-webhook-sink.js +30 -1
- package/src/orchestration/build-manager.js +56 -0
- package/src/orchestration/decision-contract.js +245 -0
- package/src/orchestration/git-ops.js +347 -0
- package/src/orchestration/orchestration-manager.js +1295 -0
- package/src/orchestration/permission-gate.js +109 -0
- package/src/orchestration/role-prompts.js +136 -0
- package/src/orchestration/run-budget.js +81 -0
- package/src/orchestration/run-ledger.js +487 -0
- package/src/orchestration/run-model.js +205 -0
- package/src/orchestration/run-record.js +336 -0
- package/src/orchestration/run-report.js +194 -0
- package/src/orchestration/to-wire.js +201 -0
- package/src/orchestration/turn-driver.js +398 -0
- package/src/pairing.js +146 -1
- package/src/permission-audit.js +79 -3
- package/src/permission-floor.js +455 -0
- package/src/permission-manager.js +492 -40
- package/src/permission-resolver.js +55 -7
- package/src/permission-rule-store.js +411 -0
- package/src/platform.js +367 -23
- package/src/providers.js +21 -2
- package/src/schedule-parser.js +311 -0
- package/src/scheduled-task-store.js +653 -0
- package/src/scheduler.js +1029 -0
- package/src/sdk-session.js +338 -5
- package/src/server-cli/server-orchestrator.js +23 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +3 -2
- package/src/server-cli.js +238 -26
- package/src/server-identity.js +28 -4
- package/src/service.js +291 -33
- package/src/session-manager.js +417 -25
- package/src/session-message-history.js +27 -21
- package/src/session-title.js +168 -0
- package/src/session-token-store.js +60 -35
- package/src/skills-usage.js +20 -2
- package/src/statusline.js +584 -0
- package/src/subagent-counter.js +9 -1
- package/src/supervisor.js +77 -27
- package/src/token-manager.js +39 -9
- package/src/transcript-tasks.js +41 -6
- package/src/tunnel/cloudflare.js +115 -8
- package/src/tunnel/index.js +4 -1
- package/src/tunnel-check.js +1 -1
- package/src/turn-tracker.js +0 -0
- package/src/usage-normalize.js +118 -0
- package/src/user-shell-session.js +98 -32
- package/src/utils/componentwise-resolver.js +205 -0
- package/src/utils/composite-key.js +45 -0
- package/src/utils/iso-datetime.js +51 -0
- package/src/utils/preflight.js +140 -28
- package/src/utils/spawn-env.js +8 -2
- package/src/utils/verify-binary.js +276 -0
- package/src/utils/verify-provenance.js +241 -0
- package/src/utils/wire-counters.js +48 -0
- package/src/worktree-gc.js +4 -1
- package/src/ws-file-ops/browser.js +42 -11
- package/src/ws-file-ops/common.js +68 -1
- package/src/ws-file-ops/git.js +221 -3
- package/src/ws-file-ops/index.js +5 -0
- package/src/ws-file-ops/memory.js +358 -0
- package/src/ws-file-ops/reader.js +124 -0
- package/src/ws-forwarding.js +44 -16
- package/src/ws-handler-context.js +19 -0
- package/src/ws-history.js +84 -11
- package/src/ws-message-handlers.js +6 -0
- package/src/ws-outbound-schemas.js +207 -0
- package/src/ws-permissions.js +285 -4
- package/src/ws-schemas.js +1 -0
- package/src/ws-server.js +301 -17
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// Dependency-free schedule parsing + next-run computation for the scheduled-task
|
|
2
|
+
// registry (#6862, foundation slice of epic #6784). This module ONLY parses a
|
|
3
|
+
// cadence and COMPUTES the next fire time — it never fires anything. The headless
|
|
4
|
+
// engine that actually spawns sessions is a sibling slice (#6865); it will import
|
|
5
|
+
// computeNextRun to advance a task's schedule after a run.
|
|
6
|
+
//
|
|
7
|
+
// Three cadence kinds are supported, all without any npm dependency:
|
|
8
|
+
// - once: { kind: 'once', at } — a single future timestamp
|
|
9
|
+
// - interval: { kind: 'interval', everyMs[, anchor] } — every N ms from an anchor
|
|
10
|
+
// - cron: { kind: 'cron', expression } — a 5-field crontab expression
|
|
11
|
+
//
|
|
12
|
+
// Deliberately distinct from `ScheduleWakeup` (transcript-tasks.js), which is an
|
|
13
|
+
// intra-session, single-shot, transcript-derived self-resume ({delaySeconds,
|
|
14
|
+
// reason, prompt}). This is a standing, persisted, recurring registry cadence.
|
|
15
|
+
|
|
16
|
+
// Minimum interval a task may fire on. Sub-second scheduling is meaningless for a
|
|
17
|
+
// cadence that spawns a whole agent session, and a 0/tiny value would be a
|
|
18
|
+
// runaway. The engine (#6865) enforces its own real-world minimum; this is a
|
|
19
|
+
// data-integrity floor so the stored model can never carry an absurd interval.
|
|
20
|
+
export const MIN_INTERVAL_MS = 1000
|
|
21
|
+
|
|
22
|
+
// Forward-search horizon for cron next-run. A legitimate crontab always fires
|
|
23
|
+
// within a few years even for the rare leap-day-only expression
|
|
24
|
+
// (`0 0 29 2 *` — next match up to ~4 years out); 5 years gives headroom. An
|
|
25
|
+
// impossible expression (e.g. day-of-month 31 in a 30-day-only month set) yields
|
|
26
|
+
// no match inside the horizon and computeNextRun returns null rather than
|
|
27
|
+
// looping forever.
|
|
28
|
+
const CRON_HORIZON_MS = 5 * 366 * 24 * 60 * 60 * 1000
|
|
29
|
+
|
|
30
|
+
const MONTH_NAMES = {
|
|
31
|
+
jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6,
|
|
32
|
+
jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12,
|
|
33
|
+
}
|
|
34
|
+
const DOW_NAMES = {
|
|
35
|
+
sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const FIELD_BOUNDS = {
|
|
39
|
+
minute: { min: 0, max: 59 },
|
|
40
|
+
hour: { min: 0, max: 23 },
|
|
41
|
+
dom: { min: 1, max: 31 },
|
|
42
|
+
month: { min: 1, max: 12 },
|
|
43
|
+
dow: { min: 0, max: 7 }, // 0 and 7 both mean Sunday
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Error thrown when a cron expression cannot be parsed. Carries the offending
|
|
48
|
+
* expression so a caller (CLI #6868 / dashboard #6871) can surface a precise
|
|
49
|
+
* message rather than a bare "invalid".
|
|
50
|
+
*/
|
|
51
|
+
export class CronParseError extends Error {
|
|
52
|
+
constructor(message, expression) {
|
|
53
|
+
super(message)
|
|
54
|
+
this.name = 'CronParseError'
|
|
55
|
+
this.expression = expression
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Parse a single crontab field (one of minute/hour/dom/month/dow) into a Set of
|
|
61
|
+
* the integer values it matches. Supports `*`, `?` (alias for `*`), single
|
|
62
|
+
* values, `a-b` ranges, star-step and range-step forms (`* / s`, `a-b / s`,
|
|
63
|
+
* written without the spaces), and comma lists combining any
|
|
64
|
+
* of those. Month and day-of-week also accept 3-letter names (jan..dec /
|
|
65
|
+
* sun..sat, case-insensitive).
|
|
66
|
+
* @param {string} raw
|
|
67
|
+
* @param {'minute'|'hour'|'dom'|'month'|'dow'} field
|
|
68
|
+
* @param {string} expression - full expression, for error messages
|
|
69
|
+
* @returns {{ values: Set<number>, star: boolean }}
|
|
70
|
+
*/
|
|
71
|
+
function parseField(raw, field, expression) {
|
|
72
|
+
const { min, max } = FIELD_BOUNDS[field]
|
|
73
|
+
const names = field === 'month' ? MONTH_NAMES : field === 'dow' ? DOW_NAMES : null
|
|
74
|
+
|
|
75
|
+
const resolveToken = (tok) => {
|
|
76
|
+
const lower = tok.toLowerCase()
|
|
77
|
+
if (names && Object.prototype.hasOwnProperty.call(names, lower)) return names[lower]
|
|
78
|
+
if (!/^\d+$/.test(tok)) {
|
|
79
|
+
throw new CronParseError(`Invalid ${field} token '${tok}'`, expression)
|
|
80
|
+
}
|
|
81
|
+
return Number(tok)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const values = new Set()
|
|
85
|
+
// Vixie star flag: set when the field's FIRST CHARACTER is `*` (or the Quartz
|
|
86
|
+
// `?` alias) — which deliberately INCLUDES step forms like `*/5`. Vixie's
|
|
87
|
+
// DOM_STAR / DOW_STAR are set off the leading `*` even though `*/5` also
|
|
88
|
+
// restricts the value set, and that flag drives the dom/dow OR-vs-AND rule in
|
|
89
|
+
// dayMatches. A literal-star-only check (`raw === '*'`) would wrongly treat
|
|
90
|
+
// `*/5` as non-star and OR it against a weekday instead of AND-ing (#6879).
|
|
91
|
+
const star = raw[0] === '*' || raw[0] === '?'
|
|
92
|
+
|
|
93
|
+
for (const part of raw.split(',')) {
|
|
94
|
+
if (part === '') throw new CronParseError(`Empty ${field} list item`, expression)
|
|
95
|
+
// Split an optional step: "<range>/<step>".
|
|
96
|
+
const [rangeText, stepText, ...rest] = part.split('/')
|
|
97
|
+
if (rest.length > 0) throw new CronParseError(`Malformed ${field} step '${part}'`, expression)
|
|
98
|
+
let step = 1
|
|
99
|
+
if (stepText !== undefined) {
|
|
100
|
+
if (!/^\d+$/.test(stepText) || Number(stepText) === 0) {
|
|
101
|
+
throw new CronParseError(`Invalid ${field} step '${stepText}'`, expression)
|
|
102
|
+
}
|
|
103
|
+
step = Number(stepText)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Determine the [lo, hi] range this part iterates over.
|
|
107
|
+
let lo
|
|
108
|
+
let hi
|
|
109
|
+
if (rangeText === '*' || rangeText === '?') {
|
|
110
|
+
lo = min
|
|
111
|
+
hi = max
|
|
112
|
+
} else if (rangeText.includes('-')) {
|
|
113
|
+
const [a, b, ...more] = rangeText.split('-')
|
|
114
|
+
if (more.length > 0) throw new CronParseError(`Malformed ${field} range '${rangeText}'`, expression)
|
|
115
|
+
lo = resolveToken(a)
|
|
116
|
+
hi = resolveToken(b)
|
|
117
|
+
} else {
|
|
118
|
+
lo = resolveToken(rangeText)
|
|
119
|
+
// A bare value with a step (`5/10`) iterates from the value up to the max.
|
|
120
|
+
hi = stepText !== undefined ? max : lo
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (lo < min || hi > max || lo > hi) {
|
|
124
|
+
throw new CronParseError(`${field} value out of range in '${part}' (allowed ${min}-${max})`, expression)
|
|
125
|
+
}
|
|
126
|
+
for (let v = lo; v <= hi; v += step) values.add(v)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Normalize day-of-week 7 -> 0 (both are Sunday) so matching against JS
|
|
130
|
+
// Date.getDay() (0=Sun..6=Sat) is a straight Set lookup.
|
|
131
|
+
if (field === 'dow' && values.has(7)) {
|
|
132
|
+
values.delete(7)
|
|
133
|
+
values.add(0)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { values, star }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Parse a standard 5-field crontab expression (minute hour day-of-month month
|
|
141
|
+
* day-of-week) into matched-value sets. Whitespace between fields is collapsed.
|
|
142
|
+
* Throws {@link CronParseError} on any malformed field.
|
|
143
|
+
* @param {string} expression
|
|
144
|
+
* @returns {{minute:Set<number>,hour:Set<number>,dom:Set<number>,month:Set<number>,dow:Set<number>,domStar:boolean,dowStar:boolean}}
|
|
145
|
+
*/
|
|
146
|
+
export function parseCron(expression) {
|
|
147
|
+
if (typeof expression !== 'string') {
|
|
148
|
+
throw new CronParseError('Cron expression must be a string', expression)
|
|
149
|
+
}
|
|
150
|
+
const fields = expression.trim().split(/\s+/)
|
|
151
|
+
if (fields.length !== 5) {
|
|
152
|
+
throw new CronParseError(`Expected 5 cron fields, got ${fields.length}`, expression)
|
|
153
|
+
}
|
|
154
|
+
const [min, hour, dom, month, dow] = fields
|
|
155
|
+
const minute = parseField(min, 'minute', expression)
|
|
156
|
+
const hourF = parseField(hour, 'hour', expression)
|
|
157
|
+
const domF = parseField(dom, 'dom', expression)
|
|
158
|
+
const monthF = parseField(month, 'month', expression)
|
|
159
|
+
const dowF = parseField(dow, 'dow', expression)
|
|
160
|
+
return {
|
|
161
|
+
minute: minute.values,
|
|
162
|
+
hour: hourF.values,
|
|
163
|
+
dom: domF.values,
|
|
164
|
+
month: monthF.values,
|
|
165
|
+
dow: dowF.values,
|
|
166
|
+
domStar: domF.star,
|
|
167
|
+
dowStar: dowF.star,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Whether a Date's calendar day matches a parsed cron's day-of-month /
|
|
173
|
+
* day-of-week fields, using the standard Vixie-cron rule (#6879 — this mirrors
|
|
174
|
+
* cron.c's `(DOM_STAR || DOW_STAR) ? (dom && dow) : (dom || dow)`):
|
|
175
|
+
*
|
|
176
|
+
* - If EITHER field is a star (its first char is `*`, which INCLUDES a
|
|
177
|
+
* step form such as star-slash-5), the two constraints are AND-ed: the day
|
|
178
|
+
* must satisfy BOTH. Because a bare `*` field matches every value, this
|
|
179
|
+
* reduces to "use the non-star field" for the common `0 0 15 * *` /
|
|
180
|
+
* `0 0 * * 1` cases — but for a dom step against a weekday (star-slash-5 in
|
|
181
|
+
* dom, Monday in dow) it correctly means "a Monday that ALSO falls on a day
|
|
182
|
+
* in the step set", NOT an OR of the two.
|
|
183
|
+
* - If NEITHER field is a star (both are explicitly restricted, e.g.
|
|
184
|
+
* `0 0 1,15 * 5` or `0 0 1-7 * 1`), the constraints are OR-ed: the day
|
|
185
|
+
* matches if EITHER matches.
|
|
186
|
+
*
|
|
187
|
+
* @param {ReturnType<typeof parseCron>} c
|
|
188
|
+
* @param {Date} d - local Date
|
|
189
|
+
* @returns {boolean}
|
|
190
|
+
*/
|
|
191
|
+
function dayMatches(c, d) {
|
|
192
|
+
const domOk = c.dom.has(d.getDate())
|
|
193
|
+
const dowOk = c.dow.has(d.getDay())
|
|
194
|
+
if (c.domStar || c.dowStar) return domOk && dowOk
|
|
195
|
+
return domOk || dowOk
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Compute the next time a parsed cron fires strictly AFTER `fromMs`, in the
|
|
200
|
+
* daemon's LOCAL time zone. Returns an epoch-ms timestamp, or null if no match
|
|
201
|
+
* falls within the search horizon (an impossible expression).
|
|
202
|
+
*
|
|
203
|
+
* Local time is deliberate: `0 9 * * *` should mean 9am wall-clock.
|
|
204
|
+
*
|
|
205
|
+
* DST is an INTENTIONAL product choice (#6879), not an accident of the
|
|
206
|
+
* arithmetic: on a spring-forward day a wall-clock time that DOES NOT EXIST
|
|
207
|
+
* (e.g. `30 2 * * *` when the clock jumps 02:00->03:00) is SKIPPED for that day
|
|
208
|
+
* rather than fired at the shifted 03:00 — we would rather miss the
|
|
209
|
+
* non-existent slot for one day than run at a time the user did not ask for.
|
|
210
|
+
* This falls out of reading getHours()/getMinutes() back off a local Date after
|
|
211
|
+
* each step: the Date never reports the non-existent 02:xx (setHours(2) on that
|
|
212
|
+
* day normalizes to 03:00), so the 02:xx minute is never matched. On a fall-back
|
|
213
|
+
* day the duplicated hour fires once. Explicit per-task time zones are a future
|
|
214
|
+
* concern.
|
|
215
|
+
* @param {ReturnType<typeof parseCron>} c
|
|
216
|
+
* @param {number} fromMs
|
|
217
|
+
* @returns {number|null}
|
|
218
|
+
*/
|
|
219
|
+
export function computeCronNextRun(c, fromMs) {
|
|
220
|
+
const horizon = fromMs + CRON_HORIZON_MS
|
|
221
|
+
const d = new Date(fromMs)
|
|
222
|
+
// Advance to the next whole minute strictly after fromMs.
|
|
223
|
+
d.setSeconds(0, 0)
|
|
224
|
+
d.setMinutes(d.getMinutes() + 1)
|
|
225
|
+
|
|
226
|
+
while (d.getTime() <= horizon) {
|
|
227
|
+
if (!c.month.has(d.getMonth() + 1)) {
|
|
228
|
+
// Jump to the first day of the next month at 00:00 local.
|
|
229
|
+
d.setMonth(d.getMonth() + 1, 1)
|
|
230
|
+
d.setHours(0, 0, 0, 0)
|
|
231
|
+
continue
|
|
232
|
+
}
|
|
233
|
+
if (!dayMatches(c, d)) {
|
|
234
|
+
d.setDate(d.getDate() + 1)
|
|
235
|
+
d.setHours(0, 0, 0, 0)
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
if (!c.hour.has(d.getHours())) {
|
|
239
|
+
d.setHours(d.getHours() + 1, 0, 0, 0)
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
if (!c.minute.has(d.getMinutes())) {
|
|
243
|
+
d.setMinutes(d.getMinutes() + 1, 0, 0)
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
return d.getTime()
|
|
247
|
+
}
|
|
248
|
+
return null
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Compute the next fire time strictly AFTER `fromMs` for an interval cadence.
|
|
253
|
+
* The anchor (defaulting to the caller's supplied base, typically the task's
|
|
254
|
+
* createdAt) fixes the phase so the boundaries do not drift with computation
|
|
255
|
+
* time. When the anchor itself is still in the future, that first anchor time is
|
|
256
|
+
* the next run.
|
|
257
|
+
* @param {number} everyMs
|
|
258
|
+
* @param {number} anchorMs
|
|
259
|
+
* @param {number} fromMs
|
|
260
|
+
* @returns {number|null}
|
|
261
|
+
*/
|
|
262
|
+
export function computeIntervalNextRun(everyMs, anchorMs, fromMs) {
|
|
263
|
+
if (!Number.isFinite(everyMs) || everyMs < MIN_INTERVAL_MS) return null
|
|
264
|
+
if (!Number.isFinite(anchorMs)) return null
|
|
265
|
+
if (fromMs < anchorMs) return anchorMs
|
|
266
|
+
const steps = Math.floor((fromMs - anchorMs) / everyMs) + 1
|
|
267
|
+
return anchorMs + steps * everyMs
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Compute a task's next run time (epoch ms) strictly after `fromMs`, or null
|
|
272
|
+
* when the task will not run again: it is disabled, it is a one-time task that
|
|
273
|
+
* has already run, its cadence is malformed, or a cron match falls beyond the
|
|
274
|
+
* search horizon. Pure — computes only, never fires.
|
|
275
|
+
*
|
|
276
|
+
* A one-time task that has NOT run yet returns its `at` even when `at` is in the
|
|
277
|
+
* past: that "overdue" state is meaningful for display, and the engine (#6865)
|
|
278
|
+
* decides whether to fire it immediately.
|
|
279
|
+
* @param {object} task - normalized scheduled task (see scheduled-task-store.js)
|
|
280
|
+
* @param {object} [opts]
|
|
281
|
+
* @param {number} [opts.from] - reference time (defaults to Date.now())
|
|
282
|
+
* @returns {number|null}
|
|
283
|
+
*/
|
|
284
|
+
export function computeNextRun(task, { from = Date.now() } = {}) {
|
|
285
|
+
if (!task || task.enabled === false) return null
|
|
286
|
+
const cadence = task.cadence
|
|
287
|
+
if (!cadence || typeof cadence !== 'object') return null
|
|
288
|
+
|
|
289
|
+
switch (cadence.kind) {
|
|
290
|
+
case 'once': {
|
|
291
|
+
if (task.lastRun) return null
|
|
292
|
+
return Number.isFinite(cadence.at) ? cadence.at : null
|
|
293
|
+
}
|
|
294
|
+
case 'interval': {
|
|
295
|
+
const anchor = Number.isFinite(cadence.anchor) ? cadence.anchor
|
|
296
|
+
: (Number.isFinite(task.createdAt) ? task.createdAt : from)
|
|
297
|
+
return computeIntervalNextRun(cadence.everyMs, anchor, from)
|
|
298
|
+
}
|
|
299
|
+
case 'cron': {
|
|
300
|
+
let parsed
|
|
301
|
+
try {
|
|
302
|
+
parsed = parseCron(cadence.expression)
|
|
303
|
+
} catch {
|
|
304
|
+
return null
|
|
305
|
+
}
|
|
306
|
+
return computeCronNextRun(parsed, from)
|
|
307
|
+
}
|
|
308
|
+
default:
|
|
309
|
+
return null
|
|
310
|
+
}
|
|
311
|
+
}
|