@cloudhal/schedules 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +103 -2
- package/dist/src/automation.d.ts +82 -0
- package/dist/src/automation.js +199 -0
- package/dist/src/budget-meter.d.ts +58 -0
- package/dist/src/budget-meter.js +130 -0
- package/dist/src/budget.d.ts +41 -0
- package/dist/src/budget.js +76 -0
- package/dist/src/builtins.d.ts +132 -0
- package/dist/src/builtins.js +473 -0
- package/dist/src/engine.d.ts +16 -0
- package/dist/src/engine.js +30 -0
- package/dist/src/errors.d.ts +22 -0
- package/dist/src/errors.js +56 -0
- package/dist/src/feed-check.d.ts +56 -0
- package/dist/src/feed-check.js +365 -0
- package/dist/src/findings.d.ts +185 -0
- package/dist/src/findings.js +287 -0
- package/dist/src/ids.d.ts +24 -0
- package/dist/src/ids.js +96 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/instant.d.ts +19 -0
- package/dist/src/instant.js +50 -0
- package/dist/src/notify.d.ts +50 -0
- package/dist/src/notify.js +60 -0
- package/dist/src/recurrence.d.ts +155 -0
- package/dist/src/recurrence.js +939 -0
- package/dist/src/release-when.d.ts +23 -0
- package/dist/src/release-when.js +55 -0
- package/dist/src/release.d.ts +198 -0
- package/dist/src/release.js +552 -0
- package/dist/src/scheduler.d.ts +490 -0
- package/dist/src/scheduler.js +1343 -0
- package/dist/src/sql/finish.d.ts +32 -0
- package/dist/src/sql/finish.js +103 -0
- package/dist/src/sql/index.d.ts +6 -0
- package/dist/src/sql/index.js +15 -0
- package/dist/src/sql/rows.d.ts +125 -0
- package/dist/src/sql/rows.js +297 -0
- package/dist/src/sql/store.d.ts +467 -0
- package/dist/src/sql/store.js +2166 -0
- package/dist/src/taint.d.ts +65 -0
- package/dist/src/taint.js +138 -0
- package/dist/src/text.d.ts +6 -0
- package/dist/src/text.js +25 -0
- package/dist/src/time-entry.d.ts +6 -0
- package/dist/src/time-entry.js +6 -0
- package/dist/src/time.d.ts +61 -0
- package/dist/src/time.js +140 -0
- package/package.json +69 -3
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Findings of the built-in jobs (RFC-0008 §4.5, §11.1): Studio-only JSON on the run row
|
|
3
|
+
* (`schedule_runs.findings`), at most 200 per run, each with a stable `code`. They are not a
|
|
4
|
+
* contract (no schema, no Rust mirror); the codes are, in the sense that Studio, the digest and
|
|
5
|
+
* the counts key on them, so a code is never renamed — a new meaning gets a new code.
|
|
6
|
+
*
|
|
7
|
+
* Text that came from visitors or from a fetched document (404 paths, feed contents) is marked
|
|
8
|
+
* `untrusted: true`: Studio renders it as plain text and agents receive it quoted (§11.4).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** At most this many findings are stored per run; `counts` stay exact (§4.5). */
|
|
12
|
+
export const MAX_FINDINGS_PER_RUN = 200;
|
|
13
|
+
|
|
14
|
+
/** `staleAfterDays` when the automation does not set one (§11.1). */
|
|
15
|
+
export const DEFAULT_STALE_AFTER_DAYS = 365;
|
|
16
|
+
|
|
17
|
+
/** How long runs (and with them their findings) are kept (§11.1 `cleanup`, §4.4). */
|
|
18
|
+
export const RUN_RETENTION_DAYS = 90;
|
|
19
|
+
/** How long terminal releases are kept (§5.2). */
|
|
20
|
+
export const RELEASE_RETENTION_DAYS = 400;
|
|
21
|
+
/** Drafts untouched this long get a reminder from `cleanup` (§11.1). */
|
|
22
|
+
export const STALE_DRAFT_DAYS = 90;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/** Where in a node a reference was found: `body`, `coverImage`, `sections.<id>.body`, `items.<id>`… */
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
export const BROKEN_LINK_CODES = [
|
|
30
|
+
/** An internal link in live content to a path nothing answers (page, entry, archive, redirect). */
|
|
31
|
+
"broken-internal-link",
|
|
32
|
+
/** `asset:<id>`, an image's `assetId` or a `/media/…` URL that is not a live asset. */
|
|
33
|
+
"broken-media-reference",
|
|
34
|
+
/** A live menu item whose page, entry or term is not live. */
|
|
35
|
+
"broken-menu-target",
|
|
36
|
+
/** A link to live content that is embargoed or expired right now (a 404 until its gate). */
|
|
37
|
+
"link-to-gated-content",
|
|
38
|
+
/** A top 404 path from analytics (visitor-supplied), with a redirect proposal when one fits. */
|
|
39
|
+
"not-found-path",
|
|
40
|
+
] ;
|
|
41
|
+
|
|
42
|
+
export const STALE_CONTENT_CODES = [
|
|
43
|
+
/** A live, published entry not updated for `staleAfterDays`. */
|
|
44
|
+
"stale-entry",
|
|
45
|
+
] ;
|
|
46
|
+
|
|
47
|
+
export const FEED_CHECK_CODES = [
|
|
48
|
+
/** The document could not be fetched or did not answer 200. */
|
|
49
|
+
"feed-unreachable",
|
|
50
|
+
/** Not well-formed XML, or not an RSS 2.0 channel / a sitemap `urlset`. */
|
|
51
|
+
"feed-invalid",
|
|
52
|
+
/** An RSS item without a title or a link, a sitemap URL without `loc`. */
|
|
53
|
+
"feed-item-incomplete",
|
|
54
|
+
/** A `pubDate` or `lastmod` in the future. */
|
|
55
|
+
"feed-future-date",
|
|
56
|
+
/** One of the newest published entries is missing. */
|
|
57
|
+
"feed-missing-entry",
|
|
58
|
+
/** The gate self-check (§7.2): an embargoed or expired node appears. Never expected. */
|
|
59
|
+
"feed-gated-node",
|
|
60
|
+
/** A `/media/…` URL in the feed that is not a live asset. */
|
|
61
|
+
"feed-asset-not-live",
|
|
62
|
+
] ;
|
|
63
|
+
|
|
64
|
+
export const DIGEST_CODES = [
|
|
65
|
+
"digest-published",
|
|
66
|
+
"digest-scheduled",
|
|
67
|
+
"digest-attention",
|
|
68
|
+
"digest-open-findings",
|
|
69
|
+
] ;
|
|
70
|
+
|
|
71
|
+
export const CLEANUP_CODES = [
|
|
72
|
+
/** A draft untouched for 90 days: a reminder, nothing is deleted (§11.1). */
|
|
73
|
+
"stale-draft",
|
|
74
|
+
] ;
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
/** Every finding code, for Studio's word lists and tests. */
|
|
88
|
+
export const FINDING_CODES = [
|
|
89
|
+
...BROKEN_LINK_CODES,
|
|
90
|
+
...STALE_CONTENT_CODES,
|
|
91
|
+
...FEED_CHECK_CODES,
|
|
92
|
+
...DIGEST_CODES,
|
|
93
|
+
...CLEANUP_CODES,
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
/** A reference in live content that resolves to nothing (or to gated content). */
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
/** A redirect a person can create with one click (RFC-0002), never created by the job. */
|
|
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
|
+
// ───────────────────────────── the weekly digest ─────────────────────────────
|
|
187
|
+
|
|
188
|
+
/** One line of a digest section: a release, an entry, an automation's open findings. */
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
/** What the weekly digest reports (§11.1): templated, no language model. */
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
/** Items per digest section kept on the run (counts stay exact). */
|
|
219
|
+
export const DIGEST_ITEMS = 10;
|
|
220
|
+
|
|
221
|
+
// ───────────────────────────── helpers ─────────────────────────────
|
|
222
|
+
|
|
223
|
+
/** Appends findings up to the cap; returns how many were dropped. */
|
|
224
|
+
export function addFindings(
|
|
225
|
+
into ,
|
|
226
|
+
more ,
|
|
227
|
+
max = MAX_FINDINGS_PER_RUN,
|
|
228
|
+
) {
|
|
229
|
+
const room = Math.max(0, max - into.length);
|
|
230
|
+
into.push(...more.slice(0, room));
|
|
231
|
+
return Math.max(0, more.length - room);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Adds `n` to `counts[key]` (creating it), for exact counts beside capped findings. */
|
|
235
|
+
export function bump(counts , key , n = 1) {
|
|
236
|
+
if (n === 0 && key in counts) return;
|
|
237
|
+
counts[key] = (counts[key] ?? 0) + n;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Counts of findings by code. */
|
|
241
|
+
export function countByCode(findings ) {
|
|
242
|
+
const out = {};
|
|
243
|
+
for (const f of findings) bump(out, f.code);
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The counts keys that total a run's open findings (one per kind, no double counting). */
|
|
248
|
+
export const OPEN_FINDING_KEYS = [
|
|
249
|
+
"broken-links",
|
|
250
|
+
"link-to-gated-content",
|
|
251
|
+
"not-found-path",
|
|
252
|
+
"stale-entries",
|
|
253
|
+
"feed-problems",
|
|
254
|
+
"stale-drafts",
|
|
255
|
+
] ;
|
|
256
|
+
|
|
257
|
+
/** The open findings a built-in run's counts report, by the keys above (0 when none). */
|
|
258
|
+
export function openFindings(counts ) {
|
|
259
|
+
const out = {};
|
|
260
|
+
for (const k of OPEN_FINDING_KEYS) if ((counts[k] ?? 0) > 0) out[k] = counts[k] ;
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The digest as a plain-text report (the run's `summary` and the notification's text). */
|
|
265
|
+
export function digestSummary(d ) {
|
|
266
|
+
const lines = [];
|
|
267
|
+
const section = (title , s ) => {
|
|
268
|
+
lines.push(`${title}: ${s.count}`);
|
|
269
|
+
for (const i of s.items) {
|
|
270
|
+
const when = i.at ? ` (${i.at.slice(0, 16).replace("T", " ")} UTC)` : "";
|
|
271
|
+
const state = i.state ? ` [${i.state}]` : "";
|
|
272
|
+
const counts = i.counts
|
|
273
|
+
? ` — ${Object.entries(i.counts)
|
|
274
|
+
.map(([k, v]) => `${v} ${k}`)
|
|
275
|
+
.join(", ")}`
|
|
276
|
+
: "";
|
|
277
|
+
lines.push(`- ${i.title}${when}${state}${counts}`);
|
|
278
|
+
}
|
|
279
|
+
if (s.count > s.items.length) lines.push(`- and ${s.count - s.items.length} more`);
|
|
280
|
+
};
|
|
281
|
+
lines.push(`Week of ${d.from.slice(0, 10)} to ${d.now.slice(0, 10)}`);
|
|
282
|
+
section("Published", d.published);
|
|
283
|
+
section("Scheduled next week", d.scheduled);
|
|
284
|
+
section("Needs attention", d.attention);
|
|
285
|
+
section("Open findings", d.openFindings);
|
|
286
|
+
return lines.join("\n");
|
|
287
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const CROCKFORD32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
2
|
+
export declare const SCHEDULE_ID_PREFIX: {
|
|
3
|
+
readonly release: "rel";
|
|
4
|
+
readonly automation: "aut";
|
|
5
|
+
readonly run: "run";
|
|
6
|
+
readonly lease: "lease";
|
|
7
|
+
};
|
|
8
|
+
/** A new random release id (`rel_…`). */
|
|
9
|
+
export declare function newReleaseId(): string;
|
|
10
|
+
/** A new random automation id (`aut_…`). */
|
|
11
|
+
export declare function newAutomationId(): string;
|
|
12
|
+
/** A new random lease token; one per claim. */
|
|
13
|
+
export declare function newLeaseToken(): string;
|
|
14
|
+
/** Lower-case hex SHA-256 of UTF-8 text. */
|
|
15
|
+
export declare function sha256Hex(text: string): Promise<string>;
|
|
16
|
+
/** The first `chars × 5` bits of `bytes`, in Crockford base32, most significant bit first. */
|
|
17
|
+
export declare function base32Prefix(bytes: Uint8Array, chars: number): string;
|
|
18
|
+
/** The deterministic id of a subject's occurrence (see the module comment for the exact rule). */
|
|
19
|
+
export declare function runId(subjectId: string, occurrenceAt: string | Date): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* A small deterministic 32-bit hash (FNV-1a over UTF-16 code units). Used where a stable spread is
|
|
22
|
+
* enough and no digest is needed, e.g. the preset minute offset (`presetMinuteOffset`).
|
|
23
|
+
*/
|
|
24
|
+
export declare function fnv1a32(text: string): number;
|
package/dist/src/ids.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifiers (RFC-0008 §4, §8.5). Worker-safe: WebCrypto only.
|
|
3
|
+
*
|
|
4
|
+
* - Release and automation ids are random: `rel_` / `aut_` + 20 Crockford base32 characters.
|
|
5
|
+
* - **Run ids are derived** (stable across versions; changing this breaks idempotency of runs that
|
|
6
|
+
* are already stored, so never change it):
|
|
7
|
+
*
|
|
8
|
+
* `runId(subjectId, occurrenceAt) = "run_" + base32(first 100 bits of SHA-256(utf8(subjectId + "\n" + iso)))`
|
|
9
|
+
*
|
|
10
|
+
* where `iso` is the occurrence as `Date#toISOString()` (UTC, milliseconds, `Z`, e.g.
|
|
11
|
+
* `2026-11-02T06:00:00.000Z`), and `base32` is Crockford's alphabet
|
|
12
|
+
* `0123456789ABCDEFGHJKMNPQRSTVWXYZ`, most significant bit first, 5 bits per character, 20
|
|
13
|
+
* characters. Every tick that reaches the same occurrence computes the same id, which is the
|
|
14
|
+
* occurrence's idempotency key (`schedule_runs.id`, `UNIQUE (subject_id, occurrence_at)`).
|
|
15
|
+
* - Lease tokens are random (`lease_` + 24 characters): unique per claim, so statements later in a
|
|
16
|
+
* batch can be guarded on "the row carries my token" (AGENTS.md rule 4).
|
|
17
|
+
*/
|
|
18
|
+
import { canonicalInstant } from "./instant.js";
|
|
19
|
+
|
|
20
|
+
export const CROCKFORD32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
21
|
+
|
|
22
|
+
export const SCHEDULE_ID_PREFIX = {
|
|
23
|
+
release: "rel",
|
|
24
|
+
automation: "aut",
|
|
25
|
+
run: "run",
|
|
26
|
+
lease: "lease",
|
|
27
|
+
} ;
|
|
28
|
+
|
|
29
|
+
function randomBase32(length ) {
|
|
30
|
+
const bytes = new Uint8Array(length);
|
|
31
|
+
crypto.getRandomValues(bytes);
|
|
32
|
+
let out = "";
|
|
33
|
+
for (const b of bytes) out += CROCKFORD32[b % 32];
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A new random release id (`rel_…`). */
|
|
38
|
+
export function newReleaseId() {
|
|
39
|
+
return `${SCHEDULE_ID_PREFIX.release}_${randomBase32(20)}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A new random automation id (`aut_…`). */
|
|
43
|
+
export function newAutomationId() {
|
|
44
|
+
return `${SCHEDULE_ID_PREFIX.automation}_${randomBase32(20)}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A new random lease token; one per claim. */
|
|
48
|
+
export function newLeaseToken() {
|
|
49
|
+
return `${SCHEDULE_ID_PREFIX.lease}_${randomBase32(24)}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Lower-case hex SHA-256 of UTF-8 text. */
|
|
53
|
+
export async function sha256Hex(text ) {
|
|
54
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
55
|
+
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The first `chars × 5` bits of `bytes`, in Crockford base32, most significant bit first. */
|
|
59
|
+
export function base32Prefix(bytes , chars ) {
|
|
60
|
+
let out = "";
|
|
61
|
+
let acc = 0;
|
|
62
|
+
let bits = 0;
|
|
63
|
+
let i = 0;
|
|
64
|
+
while (out.length < chars) {
|
|
65
|
+
if (bits < 5) {
|
|
66
|
+
acc = (acc << 8) | (bytes[i++] ?? 0);
|
|
67
|
+
bits += 8;
|
|
68
|
+
}
|
|
69
|
+
bits -= 5;
|
|
70
|
+
out += CROCKFORD32[(acc >> bits) & 31];
|
|
71
|
+
acc &= (1 << bits) - 1;
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The deterministic id of a subject's occurrence (see the module comment for the exact rule). */
|
|
77
|
+
export async function runId(subjectId , occurrenceAt ) {
|
|
78
|
+
const text = `${subjectId}\n${canonicalInstant(occurrenceAt)}`;
|
|
79
|
+
const digest = new Uint8Array(
|
|
80
|
+
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)),
|
|
81
|
+
);
|
|
82
|
+
return `${SCHEDULE_ID_PREFIX.run}_${base32Prefix(digest, 20)}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A small deterministic 32-bit hash (FNV-1a over UTF-16 code units). Used where a stable spread is
|
|
87
|
+
* enough and no digest is needed, e.g. the preset minute offset (`presetMinuteOffset`).
|
|
88
|
+
*/
|
|
89
|
+
export function fnv1a32(text ) {
|
|
90
|
+
let h = 0x811c9dc5;
|
|
91
|
+
for (let i = 0; i < text.length; i++) {
|
|
92
|
+
h ^= text.charCodeAt(i);
|
|
93
|
+
h = Math.imul(h, 0x01000193);
|
|
94
|
+
}
|
|
95
|
+
return h >>> 0;
|
|
96
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules — releases, the time model and recurrence, automations and the
|
|
3
|
+
* scheduler (RFC-0008, ADR-0023). Worker-safe: no Node-only import at any entry; the only Temporal
|
|
4
|
+
* entry point is `temporal-polyfill/implementation`, and `now` is always injected.
|
|
5
|
+
*
|
|
6
|
+
* Entries: this one (pure model and `runDue`), `./recurrence` (the RRULE subset alone),
|
|
7
|
+
* `./sql` (`SqlScheduleStore` over `SqlDriver`), `./testing` (vitest suites).
|
|
8
|
+
*/
|
|
9
|
+
export * from "./automation.js";
|
|
10
|
+
export * from "./budget.js";
|
|
11
|
+
export * from "./budget-meter.js";
|
|
12
|
+
export * from "./builtins.js";
|
|
13
|
+
export * from "./errors.js";
|
|
14
|
+
export * from "./feed-check.js";
|
|
15
|
+
export * from "./findings.js";
|
|
16
|
+
export * from "./ids.js";
|
|
17
|
+
export * from "./notify.js";
|
|
18
|
+
export * from "./recurrence.js";
|
|
19
|
+
export * from "./release.js";
|
|
20
|
+
export * from "./release-when.js";
|
|
21
|
+
export * from "./scheduler.js";
|
|
22
|
+
export * from "./taint.js";
|
|
23
|
+
export * from "./text.js";
|
|
24
|
+
export * from "./time.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules — releases, the time model and recurrence, automations and the
|
|
3
|
+
* scheduler (RFC-0008, ADR-0023). Worker-safe: no Node-only import at any entry; the only Temporal
|
|
4
|
+
* entry point is `temporal-polyfill/implementation`, and `now` is always injected.
|
|
5
|
+
*
|
|
6
|
+
* Entries: this one (pure model and `runDue`), `./recurrence` (the RRULE subset alone),
|
|
7
|
+
* `./sql` (`SqlScheduleStore` over `SqlDriver`), `./testing` (vitest suites).
|
|
8
|
+
*/
|
|
9
|
+
export * from "./automation.js";
|
|
10
|
+
export * from "./budget.js";
|
|
11
|
+
export * from "./budget-meter.js";
|
|
12
|
+
export * from "./builtins.js";
|
|
13
|
+
export * from "./errors.js";
|
|
14
|
+
export * from "./feed-check.js";
|
|
15
|
+
export * from "./findings.js";
|
|
16
|
+
export * from "./ids.js";
|
|
17
|
+
export * from "./notify.js";
|
|
18
|
+
export * from "./recurrence.js";
|
|
19
|
+
export * from "./release.js";
|
|
20
|
+
export * from "./release-when.js";
|
|
21
|
+
export * from "./scheduler.js";
|
|
22
|
+
export * from "./taint.js";
|
|
23
|
+
export * from "./text.js";
|
|
24
|
+
export * from "./time.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Absent site zone means UTC (D1). */
|
|
2
|
+
export declare const UTC = "UTC";
|
|
3
|
+
/** The contract's shape for zone names (`common.schema.json#/$defs/TimeZone`). */
|
|
4
|
+
export declare const TIME_ZONE_PATTERN: RegExp;
|
|
5
|
+
/** The contract's wall-clock shape (`common.schema.json#/$defs/LocalDateTime`), to the minute. */
|
|
6
|
+
export declare const LOCAL_DATE_TIME_PATTERN: RegExp;
|
|
7
|
+
export declare const MINUTE_MS = 60000;
|
|
8
|
+
export declare const HOUR_MS: number;
|
|
9
|
+
export declare const DAY_MS: number;
|
|
10
|
+
/** Milliseconds since the epoch of an instant given as a string, a `Date` or a number. */
|
|
11
|
+
export declare function instantMs(value: string | Date | number): number;
|
|
12
|
+
/** The canonical stored form of an instant: `Date#toISOString()` (UTC, milliseconds, `Z`). */
|
|
13
|
+
export declare function canonicalInstant(value: string | Date | number): string;
|
|
14
|
+
/** The instant truncated to its minute (seconds dropped, §9.5). */
|
|
15
|
+
export declare function floorToMinute(value: string | Date | number): number;
|
|
16
|
+
/** Whole seconds `now` is past `at` (0 when not yet due). Lateness round-trips as an integer. */
|
|
17
|
+
export declare function latenessSeconds(at: string | Date | number, now: string | Date | number): number;
|
|
18
|
+
/** True when `at` is not in the future of `now` (instants only; no zone needed). */
|
|
19
|
+
export declare function isDue(at: string | Date | number, now: string | Date | number): boolean;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instants without time zones (RFC-0008 §9): the stored form, lateness, minute arithmetic and the
|
|
3
|
+
* contract's patterns. No Temporal and no clock, so the Temporal-free `./engine` entry (what
|
|
4
|
+
* `@cloudhal/core-ts` imports, and so the site Worker bundle) can use them; `time.ts` adds zones.
|
|
5
|
+
*/
|
|
6
|
+
import { ScheduleError } from "./errors.js";
|
|
7
|
+
|
|
8
|
+
/** Absent site zone means UTC (D1). */
|
|
9
|
+
export const UTC = "UTC";
|
|
10
|
+
|
|
11
|
+
/** The contract's shape for zone names (`common.schema.json#/$defs/TimeZone`). */
|
|
12
|
+
export const TIME_ZONE_PATTERN = /^(?:UTC|[A-Z][A-Za-z_-]+(?:\/[A-Za-z0-9_+-]+){1,2})$/;
|
|
13
|
+
|
|
14
|
+
/** The contract's wall-clock shape (`common.schema.json#/$defs/LocalDateTime`), to the minute. */
|
|
15
|
+
export const LOCAL_DATE_TIME_PATTERN =
|
|
16
|
+
/^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
|
|
17
|
+
|
|
18
|
+
export const MINUTE_MS = 60_000;
|
|
19
|
+
export const HOUR_MS = 60 * MINUTE_MS;
|
|
20
|
+
export const DAY_MS = 24 * HOUR_MS;
|
|
21
|
+
|
|
22
|
+
/** Milliseconds since the epoch of an instant given as a string, a `Date` or a number. */
|
|
23
|
+
export function instantMs(value ) {
|
|
24
|
+
const ms =
|
|
25
|
+
typeof value === "number" ? value : value instanceof Date ? value.getTime() : Date.parse(value);
|
|
26
|
+
if (!Number.isFinite(ms))
|
|
27
|
+
throw new ScheduleError("invalid_timestamp", `not an instant: ${String(value)}`);
|
|
28
|
+
return ms;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The canonical stored form of an instant: `Date#toISOString()` (UTC, milliseconds, `Z`). */
|
|
32
|
+
export function canonicalInstant(value ) {
|
|
33
|
+
return new Date(instantMs(value)).toISOString();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The instant truncated to its minute (seconds dropped, §9.5). */
|
|
37
|
+
export function floorToMinute(value ) {
|
|
38
|
+
const ms = instantMs(value);
|
|
39
|
+
return ms - (((ms % MINUTE_MS) + MINUTE_MS) % MINUTE_MS);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whole seconds `now` is past `at` (0 when not yet due). Lateness round-trips as an integer. */
|
|
43
|
+
export function latenessSeconds(at , now ) {
|
|
44
|
+
return Math.max(0, Math.floor((instantMs(now) - instantMs(at)) / 1000));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** True when `at` is not in the future of `now` (instants only; no zone needed). */
|
|
48
|
+
export function isDue(at , now ) {
|
|
49
|
+
return instantMs(at) <= instantMs(now);
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The notification seam of automations (RFC-0008 §14). Studio's notifications are derived from
|
|
3
|
+
* state on every `GET /api/notifications` (nothing is stored, ADR-0016 addendum): the run row
|
|
4
|
+
* (`state`, `outcome`, `summary`, `counts`, `findings`) is the record a derived `automation-digest`
|
|
5
|
+
* or `automation-failed` item is built from. `ScheduleNotifier` is the push side for deliveries
|
|
6
|
+
* that need one (a mail sender, a webhook, a stored inbox — none exists yet); `runDue` calls it
|
|
7
|
+
* after a run's end is committed, never before, and a notifier that throws never fails a run.
|
|
8
|
+
*/
|
|
9
|
+
import type { WeeklyDigest } from "./findings.js";
|
|
10
|
+
export type ScheduleNotificationKind =
|
|
11
|
+
/** A `weekly-digest` run finished: the templated digest (§11.1, §14). */
|
|
12
|
+
"automation-digest"
|
|
13
|
+
/** A built-in run finished with findings (broken links, stale entries, feed problems). */
|
|
14
|
+
| "automation-findings"
|
|
15
|
+
/** A run failed (§14 `automation-failed`). */
|
|
16
|
+
| "automation-failed"
|
|
17
|
+
/** The automation was auto-paused (three failures, a budget) (§14 `automation-paused`). */
|
|
18
|
+
| "automation-paused";
|
|
19
|
+
export interface ScheduleNotification {
|
|
20
|
+
/** Stable, so a client shows it once: `<kind>:<runId>` (paused: `automation-paused:<aut>:<runId>`). */
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly kind: ScheduleNotificationKind;
|
|
23
|
+
readonly siteId: string;
|
|
24
|
+
readonly automationId: string;
|
|
25
|
+
readonly automationName: string;
|
|
26
|
+
readonly runId: string;
|
|
27
|
+
/** User ids it is for (§14: the automation's owner). */
|
|
28
|
+
readonly recipients: readonly string[];
|
|
29
|
+
readonly at: string;
|
|
30
|
+
/** The run's plain-text report line (English; Studio words its own from `kind` and `counts`). */
|
|
31
|
+
readonly summary?: string;
|
|
32
|
+
readonly counts?: Readonly<Record<string, number>>;
|
|
33
|
+
/** `automation-digest` only. */
|
|
34
|
+
readonly digest?: WeeklyDigest;
|
|
35
|
+
/** `automation-failed` only. */
|
|
36
|
+
readonly error?: {
|
|
37
|
+
readonly code: string;
|
|
38
|
+
readonly message?: string;
|
|
39
|
+
};
|
|
40
|
+
/** `automation-paused` only. */
|
|
41
|
+
readonly pausedReason?: "failures" | "budget";
|
|
42
|
+
}
|
|
43
|
+
/** Delivers notifications. Implementations must not throw for a delivery failure they can absorb. */
|
|
44
|
+
export interface ScheduleNotifier {
|
|
45
|
+
notify(notification: ScheduleNotification): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
/** Delivers nothing: the run row is the record (the default). */
|
|
48
|
+
export declare const NOOP_NOTIFIER: ScheduleNotifier;
|
|
49
|
+
/** One log line per notification (the admin Worker's and the Node ticker's default). */
|
|
50
|
+
export declare function logNotifier(log: (line: string) => void): ScheduleNotifier;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The notification seam of automations (RFC-0008 §14). Studio's notifications are derived from
|
|
3
|
+
* state on every `GET /api/notifications` (nothing is stored, ADR-0016 addendum): the run row
|
|
4
|
+
* (`state`, `outcome`, `summary`, `counts`, `findings`) is the record a derived `automation-digest`
|
|
5
|
+
* or `automation-failed` item is built from. `ScheduleNotifier` is the push side for deliveries
|
|
6
|
+
* that need one (a mail sender, a webhook, a stored inbox — none exists yet); `runDue` calls it
|
|
7
|
+
* after a run's end is committed, never before, and a notifier that throws never fails a run.
|
|
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
|
+
/** Delivers notifications. Implementations must not throw for a delivery failure they can absorb. */
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
/** Delivers nothing: the run row is the record (the default). */
|
|
49
|
+
export const NOOP_NOTIFIER = { notify: async () => {} };
|
|
50
|
+
|
|
51
|
+
/** One log line per notification (the admin Worker's and the Node ticker's default). */
|
|
52
|
+
export function logNotifier(log ) {
|
|
53
|
+
return {
|
|
54
|
+
async notify(n) {
|
|
55
|
+
log(
|
|
56
|
+
`schedules notification ${n.kind} for ${n.recipients.join(", ") || "nobody"}: ${n.automationName} (${n.runId})${n.summary ? `: ${n.summary.split("\n", 1)[0]}` : ""}`,
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|