@cloudhal/schedules 0.0.0-stage → 0.1.1
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,552 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The release model (RFC-0008 §5.2, §6, §8.6, §10.4, D4, D5, D15). Pure and Worker-safe: no
|
|
3
|
+
* storage, no clock; callers pass node refs, items and `now`.
|
|
4
|
+
*
|
|
5
|
+
* **The `rl1:` hash (stable; changing it voids every stored approval):**
|
|
6
|
+
*
|
|
7
|
+
* ```
|
|
8
|
+
* rl1 = "rl1:" + hex(SHA-256(utf8(lines.join("\n"))))
|
|
9
|
+
* lines = ["site <siteId>"] + one line per item, sorted by node id (code-unit order):
|
|
10
|
+
* "<set|remove> <nodeId> <nodeKind> <targetRev>:<targetHash>|- <baseRev>:<baseHash>|-"
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* `-` stands for an absent target (a removal) or an absent base (a node that was not live). The
|
|
14
|
+
* time is **not** in the hash (D4): a person who may approve the release moves it and keeps the
|
|
15
|
+
* approval; the approval request's `args_hash` covers `{ releaseId, when }` instead
|
|
16
|
+
* (`approvalArgs`). Any change to the items changes the hash.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
import { ScheduleError } from "./errors.js";
|
|
30
|
+
import { sha256Hex } from "./ids.js";
|
|
31
|
+
import { canonicalInstant, instantMs, latenessSeconds, MINUTE_MS } from "./instant.js";
|
|
32
|
+
|
|
33
|
+
export const RELEASE_HASH_PREFIX = "rl1:";
|
|
34
|
+
|
|
35
|
+
/** The system actor scheduled work runs as (on behalf of a person, §6.3 step 6, S14). */
|
|
36
|
+
export const SCHEDULER_ACTOR = Object.freeze({
|
|
37
|
+
kind: "system",
|
|
38
|
+
id: "sys_scheduler",
|
|
39
|
+
name: "Scheduler",
|
|
40
|
+
} );
|
|
41
|
+
export const MAX_RELEASE_ITEMS = 100;
|
|
42
|
+
export const MAX_OPEN_RELEASES_PER_SITE = 200;
|
|
43
|
+
/** A release runs late at most once and only within this many seconds of its time (owner decision 2). */
|
|
44
|
+
export const LATE_LIMIT_SECONDS = 3_600;
|
|
45
|
+
/** Transient errors retry inside the hour with this back-off, by attempt (§8.6). */
|
|
46
|
+
export const RETRY_BACKOFF_SECONDS = [30, 60, 120, 300, 600];
|
|
47
|
+
/** How long a claim holds a release before another tick may recover it (§8.5). */
|
|
48
|
+
export const RELEASE_LEASE_MS = 10 * MINUTE_MS;
|
|
49
|
+
/** Lead time and horizons (§6.1, §10.5). */
|
|
50
|
+
export const MIN_LEAD_MS = MINUTE_MS;
|
|
51
|
+
export const MAX_HORIZON_DAYS = 366;
|
|
52
|
+
export const REQUEST_HORIZON_DAYS = 30;
|
|
53
|
+
/** At most this many new bodies per request on Workers Free (§6.1 step 4). */
|
|
54
|
+
export const MAX_NEW_BODIES_PER_REQUEST = 20;
|
|
55
|
+
|
|
56
|
+
/** Node kinds a release may remove (§6.1 step 3). */
|
|
57
|
+
export const REMOVABLE_NODE_KINDS = [
|
|
58
|
+
"contentEntry",
|
|
59
|
+
"page",
|
|
60
|
+
"redirect",
|
|
61
|
+
];
|
|
62
|
+
export const RELEASE_NODE_KINDS = [
|
|
63
|
+
"page",
|
|
64
|
+
"contentEntry",
|
|
65
|
+
"taxonomy",
|
|
66
|
+
"term",
|
|
67
|
+
"redirect",
|
|
68
|
+
"menu",
|
|
69
|
+
"asset",
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
/** States whose nodes are held and locked (§3, §6.2). */
|
|
73
|
+
export const HOLDING_STATES = [
|
|
74
|
+
"awaiting_approval",
|
|
75
|
+
"scheduled",
|
|
76
|
+
"running",
|
|
77
|
+
];
|
|
78
|
+
/** States the scheduler never leaves on its own (kept for history; retention deletes them after 400 days). */
|
|
79
|
+
export const TERMINAL_RELEASE_STATES = [
|
|
80
|
+
"published",
|
|
81
|
+
"conflict",
|
|
82
|
+
"missed",
|
|
83
|
+
"expired",
|
|
84
|
+
"failed",
|
|
85
|
+
"canceled",
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
export function isHolding(state ) {
|
|
89
|
+
return HOLDING_STATES.includes(state);
|
|
90
|
+
}
|
|
91
|
+
export function isTerminalRelease(state ) {
|
|
92
|
+
return TERMINAL_RELEASE_STATES.includes(state);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Allowed transitions (§5.2). Beyond the table: `running → canceled` and `running → expired` are the
|
|
97
|
+
* scheduler's own (a vanished site or node, an approval lost at run time — §10.6; people cannot
|
|
98
|
+
* cancel a running release, §8.5); `conflict → draft | awaiting_approval | scheduled` is "Re-pin"
|
|
99
|
+
* (and approve); `missed | expired → running` is a person's "Publish now" (a manual run).
|
|
100
|
+
*/
|
|
101
|
+
export const RELEASE_TRANSITIONS = {
|
|
102
|
+
draft: ["awaiting_approval", "scheduled", "canceled"],
|
|
103
|
+
awaiting_approval: ["scheduled", "draft", "expired", "canceled"],
|
|
104
|
+
scheduled: [
|
|
105
|
+
"scheduled",
|
|
106
|
+
"awaiting_approval",
|
|
107
|
+
"draft",
|
|
108
|
+
"running",
|
|
109
|
+
"missed",
|
|
110
|
+
"expired",
|
|
111
|
+
"canceled",
|
|
112
|
+
],
|
|
113
|
+
running: ["published", "conflict", "failed", "missed", "expired", "canceled"],
|
|
114
|
+
published: [],
|
|
115
|
+
conflict: ["draft", "awaiting_approval", "scheduled", "canceled"],
|
|
116
|
+
missed: ["running", "canceled"],
|
|
117
|
+
expired: ["running", "canceled"],
|
|
118
|
+
failed: [],
|
|
119
|
+
canceled: [],
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export function canTransitionRelease(from , to ) {
|
|
123
|
+
return RELEASE_TRANSITIONS[from].includes(to);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function assertReleaseTransition(from , to ) {
|
|
127
|
+
if (!canTransitionRelease(from, to))
|
|
128
|
+
throw new ScheduleError("invalid_transition", `a release cannot go from ${from} to ${to}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ───────────────────────────── hash ─────────────────────────────
|
|
132
|
+
|
|
133
|
+
const version = (v ) => (v ? `${v.rev}:${v.hash}` : "-");
|
|
134
|
+
|
|
135
|
+
/** One canonical item line (see the module comment). */
|
|
136
|
+
export function releaseItemLine(item ) {
|
|
137
|
+
const target = item.kind === "set" ? item.target : undefined;
|
|
138
|
+
return `${item.kind} ${item.nodeId} ${item.nodeKind} ${version(target)} ${version(item.base)}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const byId = (a , b ) => (a < b ? -1 : a > b ? 1 : 0);
|
|
142
|
+
|
|
143
|
+
/** The lines the `rl1:` hash digests, in order. */
|
|
144
|
+
export function releaseHashLines(siteId , items ) {
|
|
145
|
+
const sorted = [...items].sort((a, b) => byId(a.nodeId, b.nodeId));
|
|
146
|
+
return [`site ${siteId}`, ...sorted.map(releaseItemLine)];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** `rl1:<hex>` over the site id and the pinned items (time not included, D4). */
|
|
150
|
+
export async function releaseHash(siteId , items ) {
|
|
151
|
+
return `${RELEASE_HASH_PREFIX}${await sha256Hex(releaseHashLines(siteId, items).join("\n"))}`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* What an approval request's `args_hash` covers for release tools (§10.4): the release and its
|
|
156
|
+
* `when`, canonical. The state binding (`state_hash`) is the release's `rl1:` hash.
|
|
157
|
+
*/
|
|
158
|
+
export function approvalArgs(releaseId , when ) {
|
|
159
|
+
if (!when) return JSON.stringify({ releaseId, when: null });
|
|
160
|
+
if (when.kind === "on-approval")
|
|
161
|
+
return JSON.stringify({ releaseId, when: { kind: "on-approval" } });
|
|
162
|
+
return JSON.stringify({
|
|
163
|
+
releaseId,
|
|
164
|
+
when: {
|
|
165
|
+
at: canonicalInstant(when.at),
|
|
166
|
+
kind: "at",
|
|
167
|
+
...(when.localTime ? { localTime: when.localTime } : {}),
|
|
168
|
+
timeZone: when.timeZone,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ───────────────────────────── pin and closure ─────────────────────────────
|
|
174
|
+
|
|
175
|
+
/** A node version as the store knows it (a snapshot's `NodeRef`). */
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
/** A pinned item plus what the run needs without reading bodies (§5.1 `release_items`). */
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
function refMap(
|
|
188
|
+
refs ,
|
|
189
|
+
) {
|
|
190
|
+
if (refs instanceof Map) return refs ;
|
|
191
|
+
return new Map([...(refs )].map((r) => [r.id, r]));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const nodeVersion = (r ) => ({ rev: r.rev, hash: r.hash });
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Pins items (§6.1 step 1): each `set` pins the node's working-copy version as target and its live
|
|
210
|
+
* version (if any) as base; each `remove` pins the live version as base. Refuses the site node, a
|
|
211
|
+
* node that does not exist, a removal of a kind other than entry/page/redirect or of a node that
|
|
212
|
+
* is not live, a node given twice, and an item whose target equals its base (`nothing_to_publish`).
|
|
213
|
+
* Items come back sorted by node id.
|
|
214
|
+
*/
|
|
215
|
+
export function pinItems(input ) {
|
|
216
|
+
const working = refMap(input.working);
|
|
217
|
+
const live = refMap(input.live);
|
|
218
|
+
const seen = new Set ();
|
|
219
|
+
const items = [];
|
|
220
|
+
const once = (id ) => {
|
|
221
|
+
if (seen.has(id)) throw new ScheduleError("duplicate_item", `${id} is in the release twice`);
|
|
222
|
+
seen.add(id);
|
|
223
|
+
};
|
|
224
|
+
for (const id of input.set ?? []) {
|
|
225
|
+
once(id);
|
|
226
|
+
const target = working.get(id);
|
|
227
|
+
if (!target) throw new ScheduleError("node_not_found", `${id} does not exist`);
|
|
228
|
+
if (target.kind === "site" || target.kind === "glossary")
|
|
229
|
+
throw new ScheduleError(
|
|
230
|
+
"site_node_in_release",
|
|
231
|
+
"site settings and the glossary are published the ordinary way",
|
|
232
|
+
);
|
|
233
|
+
const base = live.get(id);
|
|
234
|
+
if (base && base.rev === target.rev && base.hash === target.hash)
|
|
235
|
+
throw new ScheduleError("nothing_to_publish", `${id} is already live in this version`);
|
|
236
|
+
items.push({
|
|
237
|
+
kind: "set",
|
|
238
|
+
nodeId: id,
|
|
239
|
+
nodeKind: target.kind,
|
|
240
|
+
target: nodeVersion(target),
|
|
241
|
+
...(base ? { base: nodeVersion(base) } : {}),
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
for (const id of input.remove ?? []) {
|
|
245
|
+
once(id);
|
|
246
|
+
const base = live.get(id);
|
|
247
|
+
const known = base ?? working.get(id);
|
|
248
|
+
if (!known) throw new ScheduleError("node_not_found", `${id} does not exist`);
|
|
249
|
+
if (known.kind === "site" || known.kind === "glossary")
|
|
250
|
+
throw new ScheduleError(
|
|
251
|
+
"site_node_in_release",
|
|
252
|
+
"site settings and the glossary are published the ordinary way",
|
|
253
|
+
);
|
|
254
|
+
if (!REMOVABLE_NODE_KINDS.includes(known.kind))
|
|
255
|
+
throw new ScheduleError(
|
|
256
|
+
"remove_not_allowed",
|
|
257
|
+
`releases remove entries, pages and redirects only (${id} is a ${known.kind})`,
|
|
258
|
+
);
|
|
259
|
+
if (!base) throw new ScheduleError("nothing_to_publish", `${id} is not live`);
|
|
260
|
+
items.push({ kind: "remove", nodeId: id, nodeKind: known.kind, base: nodeVersion(base) });
|
|
261
|
+
}
|
|
262
|
+
if (items.length > MAX_RELEASE_ITEMS)
|
|
263
|
+
throw new ScheduleError("too_many_items", `at most ${MAX_RELEASE_ITEMS} items per release`);
|
|
264
|
+
return items.sort((a, b) => byId(a.nodeId, b.nodeId));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Id prefixes of graph nodes a body may reference (`@cloudhal/core-ts` `ID_PREFIX`), never the site. */
|
|
268
|
+
const NODE_REF = /^(?:page|entry|tax|term|redir|menu|asset)_[A-Za-z0-9]{6,32}$/;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The default dependency extractor: every string in a node body (at any depth) that is exactly a
|
|
272
|
+
* node id of a releasable kind, except the node's own id and `siteId`. This covers term
|
|
273
|
+
* assignments (`terms.*.ids`, `primary`), `parentId`/`taxonomyId`, `ImageRef.assetId` and
|
|
274
|
+
* `coverImage`, menu targets (`pageId`, `entryId`, `termId`). Callers with exact knowledge of a
|
|
275
|
+
* kind (core-ts) may pass their own `depsOf` to `dependencyClosure`.
|
|
276
|
+
*/
|
|
277
|
+
export function referencedNodeIds(body ) {
|
|
278
|
+
const self =
|
|
279
|
+
body && typeof body === "object" && "id" in body ? (body ).id : undefined;
|
|
280
|
+
const out = new Set ();
|
|
281
|
+
const walk = (v , key ) => {
|
|
282
|
+
if (typeof v === "string") {
|
|
283
|
+
if (key !== "siteId" && v !== self && NODE_REF.test(v)) out.add(v);
|
|
284
|
+
} else if (Array.isArray(v)) for (const x of v) walk(x, key);
|
|
285
|
+
else if (v && typeof v === "object") for (const [k, x] of Object.entries(v)) walk(x, k);
|
|
286
|
+
};
|
|
287
|
+
walk(body, undefined);
|
|
288
|
+
return [...out].sort(byId);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Dependency closure (§6.1 step 2): adds, transitively, every node a target references that is not
|
|
312
|
+
* live, pinned at its working-copy version (no base). A referenced node that is live (in any
|
|
313
|
+
* version) is satisfied. Records each target's outgoing ids so the run checks the composed snapshot
|
|
314
|
+
* without reading bodies (§6.3 step 5).
|
|
315
|
+
*/
|
|
316
|
+
export async function dependencyClosure(input ) {
|
|
317
|
+
const working = refMap(input.working);
|
|
318
|
+
const live = refMap(input.live);
|
|
319
|
+
const byNode = new Map ();
|
|
320
|
+
const queue = [];
|
|
321
|
+
for (const item of input.items) {
|
|
322
|
+
byNode.set(item.nodeId, { item, deps: [] });
|
|
323
|
+
queue.push(item);
|
|
324
|
+
}
|
|
325
|
+
const added = [];
|
|
326
|
+
const missing = new Set ();
|
|
327
|
+
while (queue.length > 0) {
|
|
328
|
+
const item = queue.shift() ;
|
|
329
|
+
if (item.kind !== "set") continue;
|
|
330
|
+
const ref = { id: item.nodeId, kind: item.nodeKind, ...item.target };
|
|
331
|
+
const deps = [...new Set(await input.depsOf(ref))].filter((d) => d !== item.nodeId).sort(byId);
|
|
332
|
+
const pinned = byNode.get(item.nodeId) ;
|
|
333
|
+
pinned.deps = deps;
|
|
334
|
+
if (item.nodeKind === "asset" && input.blobOf) {
|
|
335
|
+
const sha = await input.blobOf(ref);
|
|
336
|
+
if (sha) pinned.blobSha = sha;
|
|
337
|
+
}
|
|
338
|
+
for (const dep of deps) {
|
|
339
|
+
if (byNode.has(dep) || live.has(dep)) continue;
|
|
340
|
+
const w = working.get(dep);
|
|
341
|
+
if (!w || w.kind === "site" || w.kind === "glossary") {
|
|
342
|
+
missing.add(dep);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const next = {
|
|
346
|
+
kind: "set",
|
|
347
|
+
nodeId: dep,
|
|
348
|
+
nodeKind: w.kind,
|
|
349
|
+
target: nodeVersion(w),
|
|
350
|
+
};
|
|
351
|
+
byNode.set(dep, { item: next, deps: [] });
|
|
352
|
+
added.push(dep);
|
|
353
|
+
queue.push(next);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (byNode.size > MAX_RELEASE_ITEMS)
|
|
357
|
+
throw new ScheduleError(
|
|
358
|
+
"too_many_items",
|
|
359
|
+
`the release and what it needs come to ${byNode.size} items (at most ${MAX_RELEASE_ITEMS})`,
|
|
360
|
+
);
|
|
361
|
+
return {
|
|
362
|
+
items: [...byNode.values()].sort((a, b) => byId(a.item.nodeId, b.item.nodeId)),
|
|
363
|
+
added: added.sort(byId),
|
|
364
|
+
missing: [...missing].sort(byId),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ───────────────────────────── apply at T ─────────────────────────────
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* `nodes(S) = nodes(L) − ids + targets` (S1, §6.3 step 4): the live refs without every item's node,
|
|
372
|
+
* plus each `set` item's target, sorted by id (code-unit order, as SQLite's `ORDER BY id`). The site
|
|
373
|
+
* ref stays the live one.
|
|
374
|
+
*/
|
|
375
|
+
export function composeRelease(live , items ) {
|
|
376
|
+
const ids = new Set(items.map((i) => i.nodeId));
|
|
377
|
+
const out = live.filter((n) => !ids.has(n.id));
|
|
378
|
+
for (const item of items)
|
|
379
|
+
if (item.kind === "set")
|
|
380
|
+
out.push({
|
|
381
|
+
id: item.nodeId,
|
|
382
|
+
kind: item.nodeKind,
|
|
383
|
+
rev: item.target.rev,
|
|
384
|
+
hash: item.target.hash,
|
|
385
|
+
});
|
|
386
|
+
return out.sort((a, b) => byId(a.id, b.id));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const sameVersion = (a , b ) =>
|
|
390
|
+
a === undefined || b === undefined ? a === b : a.rev === b.rev && a.hash === b.hash;
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Conflicts (§6.4, S4): every item whose node's live version is not its pinned base (both absent
|
|
394
|
+
* counts as equal). Empty when the release can be applied.
|
|
395
|
+
*/
|
|
396
|
+
export function releaseConflicts(
|
|
397
|
+
live ,
|
|
398
|
+
items ,
|
|
399
|
+
) {
|
|
400
|
+
const byNode = new Map(live.map((n) => [n.id, n]));
|
|
401
|
+
const out = [];
|
|
402
|
+
for (const item of [...items].sort((a, b) => byId(a.nodeId, b.nodeId))) {
|
|
403
|
+
const found = byNode.get(item.nodeId);
|
|
404
|
+
const foundVersion = found ? { rev: found.rev, hash: found.hash } : undefined;
|
|
405
|
+
if (!sameVersion(item.base, foundVersion))
|
|
406
|
+
out.push({
|
|
407
|
+
nodeId: item.nodeId,
|
|
408
|
+
...(item.base ? { expected: item.base } : {}),
|
|
409
|
+
...(foundVersion ? { found: foundVersion } : {}),
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
return out;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** The first dependency of a `set` target missing from the composed snapshot (§6.3 step 5). */
|
|
416
|
+
export function missingDependency(
|
|
417
|
+
composed ,
|
|
418
|
+
items ,
|
|
419
|
+
) {
|
|
420
|
+
const present = new Set(composed.map((n) => n.id));
|
|
421
|
+
for (const { item, deps } of [...items].sort((a, b) => byId(a.item.nodeId, b.item.nodeId))) {
|
|
422
|
+
if (item.kind !== "set") continue;
|
|
423
|
+
for (const dep of deps) if (!present.has(dep)) return { nodeId: item.nodeId, dependency: dep };
|
|
424
|
+
}
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Steps 3–5 of §6.3 as one pure function: conflicts first (nothing applied partially, S4), then the
|
|
435
|
+
* composition, then the dependency check. `publishRelease` (core-ts) writes the snapshot it returns.
|
|
436
|
+
*/
|
|
437
|
+
export function planRelease(
|
|
438
|
+
live ,
|
|
439
|
+
items ,
|
|
440
|
+
) {
|
|
441
|
+
const plain = items.map((i) => i.item);
|
|
442
|
+
const conflicts = releaseConflicts(live, plain);
|
|
443
|
+
if (conflicts.length > 0) return { kind: "conflict", conflicts };
|
|
444
|
+
const nodes = composeRelease(live, plain);
|
|
445
|
+
const missing = missingDependency(nodes, items);
|
|
446
|
+
if (missing)
|
|
447
|
+
return {
|
|
448
|
+
kind: "failed",
|
|
449
|
+
code: "dependency_missing",
|
|
450
|
+
message: `${missing.nodeId} needs ${missing.dependency}, which would not be live`,
|
|
451
|
+
};
|
|
452
|
+
let added = 0;
|
|
453
|
+
let changed = 0;
|
|
454
|
+
let removed = 0;
|
|
455
|
+
for (const i of plain) {
|
|
456
|
+
if (i.kind === "remove") removed++;
|
|
457
|
+
else if (i.base) changed++;
|
|
458
|
+
else added++;
|
|
459
|
+
}
|
|
460
|
+
return { kind: "apply", nodes, added, changed, removed };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// ───────────────────────────── time, approval and lateness at run time ─────────────────────────────
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* The approval checks that need no policy (§10.6): a release without an approval is `unapproved`
|
|
474
|
+
* (D5); an `auto` approval past `windowEnds` is `auto_window_ended` (D9). Rights (the approver may
|
|
475
|
+
* still publish it; the key and its creator still exist) are checked by the policy layer, which
|
|
476
|
+
* `runDue` takes as `checkApproval`.
|
|
477
|
+
*/
|
|
478
|
+
export function baseApprovalVerdict(
|
|
479
|
+
approval ,
|
|
480
|
+
now ,
|
|
481
|
+
) {
|
|
482
|
+
if (!approval) return { ok: false, reason: "unapproved" };
|
|
483
|
+
if (approval.kind === "auto" && instantMs(now) > instantMs(approval.windowEnds))
|
|
484
|
+
return { ok: false, reason: "auto_window_ended" };
|
|
485
|
+
return { ok: true };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** `missed` when more than an hour late (owner decision 2, S8). */
|
|
489
|
+
export function isPastLateLimit(
|
|
490
|
+
executeAt ,
|
|
491
|
+
now ,
|
|
492
|
+
) {
|
|
493
|
+
return latenessSeconds(executeAt, now) > LATE_LIMIT_SECONDS;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* When a release that failed transiently on `attempt` (1-based) may try again: the back-off of
|
|
498
|
+
* §8.6, or undefined when that would be more than an hour after its time (then it is `missed`).
|
|
499
|
+
*/
|
|
500
|
+
export function nextRetryAt(
|
|
501
|
+
executeAt ,
|
|
502
|
+
now ,
|
|
503
|
+
attempt ,
|
|
504
|
+
) {
|
|
505
|
+
const delay =
|
|
506
|
+
RETRY_BACKOFF_SECONDS[Math.min(Math.max(attempt, 1), RETRY_BACKOFF_SECONDS.length) - 1] ?? 600;
|
|
507
|
+
const at = instantMs(now) + delay * 1000;
|
|
508
|
+
if (at - instantMs(executeAt) > LATE_LIMIT_SECONDS * 1000) return undefined;
|
|
509
|
+
return canonicalInstant(at);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// ───────────────────────────── order and overlap (D15) ─────────────────────────────
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
/** Releases run one at a time per site, in `execute_at` order, ties by id (§6.5). */
|
|
520
|
+
export function orderReleases (releases ) {
|
|
521
|
+
return [...releases].sort((a, b) => {
|
|
522
|
+
const d = instantMs(a.executeAt) - instantMs(b.executeAt);
|
|
523
|
+
return d !== 0 ? d : byId(a.id, b.id);
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Nodes in more than one non-terminal release (D15: flagged, not refused), as node id → release
|
|
529
|
+
* ids (sorted). Studio and `get_release` show these as `overlaps`.
|
|
530
|
+
*/
|
|
531
|
+
export function releaseOverlaps(
|
|
532
|
+
releases ,
|
|
533
|
+
) {
|
|
534
|
+
const by = new Map ();
|
|
535
|
+
for (const r of releases) {
|
|
536
|
+
if (isTerminalRelease(r.state)) continue;
|
|
537
|
+
for (const item of r.items) {
|
|
538
|
+
const list = by.get(item.nodeId) ?? [];
|
|
539
|
+
list.push(r.id);
|
|
540
|
+
by.set(item.nodeId, list);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const out = new Map ();
|
|
544
|
+
for (const [node, ids] of [...by].sort((a, b) => byId(a[0], b[0])))
|
|
545
|
+
if (ids.length > 1) out.set(node, ids.sort(byId));
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** Releases whose time falls in the same minute (Studio warns: "2 releases at 09:00", §6.5). */
|
|
550
|
+
export function sameMinute(a , b ) {
|
|
551
|
+
return Math.floor(instantMs(a) / MINUTE_MS) === Math.floor(instantMs(b) / MINUTE_MS);
|
|
552
|
+
}
|