@atolis-hq/wake 0.3.17 → 0.3.18

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.
@@ -108,4 +108,4 @@ export function resolveWakeVersion(options = {}) {
108
108
  return `g${headHash.slice(0, 7)}`;
109
109
  return '0.1.0-dev';
110
110
  }
111
- export const wakeVersion = "g4cf4a17";
111
+ export const wakeVersion = "gaebb12d";
@@ -1,3 +1,4 @@
1
+ import { CronExpressionParser } from 'cron-parser';
1
2
  export class SchedulePolicy {
2
3
  elapsedSlots(config, now, checkpoint) {
3
4
  const end = minute(new Date(now));
@@ -7,13 +8,22 @@ export class SchedulePolicy {
7
8
  const fields = config.cron.trim().split(/\s+/);
8
9
  if (fields.length !== 5)
9
10
  throw new Error(`Schedule ${config.id} must use a five-field cron expression`);
11
+ if (fields.some(hasUnsupportedSyntax))
12
+ throw new Error(`Schedule ${config.id} uses unsupported cron syntax`);
13
+ const expression = CronExpressionParser.parse(config.cron, {
14
+ currentDate: new Date(start - 1),
15
+ endDate: new Date(end),
16
+ tz: 'UTC',
17
+ });
18
+ const dayOfMonth = fieldExpression(fields, 2, 4);
19
+ const dayOfWeek = fieldExpression(fields, 4, 2);
10
20
  const slots = [];
11
- for (let cursor = start; cursor <= end; cursor += 60_000) {
12
- const date = new Date(cursor);
13
- if (matches(fields, date)) {
14
- const at = date.toISOString();
15
- slots.push({ identity: `schedule:${config.id}:${at}`, at });
16
- }
21
+ while (expression.hasNext()) {
22
+ const date = expression.next().toDate();
23
+ if (!matchesDays(date, dayOfMonth, dayOfWeek))
24
+ continue;
25
+ const at = date.toISOString();
26
+ slots.push({ identity: `schedule:${config.id}:${at}`, at });
17
27
  }
18
28
  return slots;
19
29
  }
@@ -23,24 +33,16 @@ function minute(value) {
23
33
  throw new Error('Schedule timestamps must be valid dates');
24
34
  return Math.floor(value.getTime() / 60_000) * 60_000;
25
35
  }
26
- function matches(fields, date) {
27
- return (matchesField(fields[0], date.getUTCMinutes(), 0, 59) &&
28
- matchesField(fields[1], date.getUTCHours(), 0, 23) &&
29
- matchesField(fields[2], date.getUTCDate(), 1, 31) &&
30
- matchesField(fields[3], date.getUTCMonth() + 1, 1, 12) &&
31
- matchesField(fields[4], date.getUTCDay(), 0, 6));
36
+ function hasUnsupportedSyntax(field) {
37
+ return /[?#]|(^|[,*/-])H(?=$|[,*/-])|(^|[,*/-])L(?=$|[,*/-])|\dL\b/i.test(field);
32
38
  }
33
- function matchesField(expression, value, minimum, maximum) {
34
- return expression.split(',').some((part) => {
35
- const [base, stepText] = part.split('/');
36
- const step = stepText === undefined ? 1 : Number(stepText);
37
- if (!Number.isInteger(step) || step < 1)
38
- throw new Error(`Invalid cron step: ${part}`);
39
- if (base === '*')
40
- return (value - minimum) % step === 0;
41
- const number = Number(base);
42
- if (!Number.isInteger(number) || number < minimum || number > maximum)
43
- throw new Error(`Invalid cron field: ${part}`);
44
- return value === number;
45
- });
39
+ function fieldExpression(fields, index, wildcardIndex) {
40
+ if (fields[index] === '*')
41
+ return null;
42
+ const constrained = [...fields];
43
+ constrained[wildcardIndex] = '*';
44
+ return CronExpressionParser.parse(constrained.join(' '), { tz: 'UTC' });
45
+ }
46
+ function matchesDays(date, dayOfMonth, dayOfWeek) {
47
+ return (dayOfMonth?.includesDate(date) ?? true) && (dayOfWeek?.includesDate(date) ?? true);
46
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.17",
3
+ "version": "0.3.18",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -66,6 +66,7 @@
66
66
  },
67
67
  "dependencies": {
68
68
  "@octokit/rest": "^22.0.0",
69
+ "cron-parser": "^5.10.0",
69
70
  "execa": "^10.0.1",
70
71
  "handlebars": "^4.7.9",
71
72
  "ulid": "^3.0.2",