@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,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `releaseWhen` (§9.1): a release's time from what a person or agent sent. It resolves wall-clock
|
|
3
|
+
* times in a zone, so it lives apart from `release.ts`, which stays free of Temporal (the
|
|
4
|
+
* `./engine` entry).
|
|
5
|
+
*/
|
|
6
|
+
import type { ReleaseWhen } from "@cloudhal/core-contracts";
|
|
7
|
+
export type WhenInput = {
|
|
8
|
+
at: string | Date | number;
|
|
9
|
+
timeZone?: string;
|
|
10
|
+
} | {
|
|
11
|
+
localTime: string;
|
|
12
|
+
timeZone?: string;
|
|
13
|
+
} | "on-approval";
|
|
14
|
+
/**
|
|
15
|
+
* Builds a `ReleaseWhen` (§9.1): a wall-clock time is kept with its zone (the site's, D1: UTC when
|
|
16
|
+
* absent) and resolved to `at`; an instant is a machine's absolute intent, kept with the zone for
|
|
17
|
+
* display. Checks the lead time (≥ 1 minute) and horizon (≤ 366 days, or `horizonDays`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function releaseWhen(input: WhenInput, options: {
|
|
20
|
+
siteTimeZone?: string;
|
|
21
|
+
now: string | Date | number;
|
|
22
|
+
horizonDays?: number;
|
|
23
|
+
}): ReleaseWhen;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `releaseWhen` (§9.1): a release's time from what a person or agent sent. It resolves wall-clock
|
|
3
|
+
* times in a zone, so it lives apart from `release.ts`, which stays free of Temporal (the
|
|
4
|
+
* `./engine` entry).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ScheduleError } from "./errors.js";
|
|
8
|
+
import { MAX_HORIZON_DAYS, MIN_LEAD_MS } from "./release.js";
|
|
9
|
+
import {
|
|
10
|
+
assertTimeZone,
|
|
11
|
+
canonicalInstant,
|
|
12
|
+
DAY_MS,
|
|
13
|
+
instantMs,
|
|
14
|
+
resolveLocalTime,
|
|
15
|
+
UTC,
|
|
16
|
+
} from "./time.js";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds a `ReleaseWhen` (§9.1): a wall-clock time is kept with its zone (the site's, D1: UTC when
|
|
25
|
+
* absent) and resolved to `at`; an instant is a machine's absolute intent, kept with the zone for
|
|
26
|
+
* display. Checks the lead time (≥ 1 minute) and horizon (≤ 366 days, or `horizonDays`).
|
|
27
|
+
*/
|
|
28
|
+
export function releaseWhen(
|
|
29
|
+
input ,
|
|
30
|
+
options ,
|
|
31
|
+
) {
|
|
32
|
+
if (input === "on-approval") return { kind: "on-approval" };
|
|
33
|
+
const timeZone = input.timeZone ?? options.siteTimeZone ?? UTC;
|
|
34
|
+
assertTimeZone(timeZone);
|
|
35
|
+
const when =
|
|
36
|
+
"localTime" in input
|
|
37
|
+
? {
|
|
38
|
+
kind: "at",
|
|
39
|
+
at: resolveLocalTime(input.localTime, timeZone).at,
|
|
40
|
+
timeZone,
|
|
41
|
+
localTime: input.localTime,
|
|
42
|
+
}
|
|
43
|
+
: { kind: "at", at: canonicalInstant(input.at), timeZone };
|
|
44
|
+
const at = instantMs(when.at);
|
|
45
|
+
const now = instantMs(options.now);
|
|
46
|
+
if (at < now + MIN_LEAD_MS)
|
|
47
|
+
throw new ScheduleError(
|
|
48
|
+
"too_soon",
|
|
49
|
+
"a release time must be at least a minute ahead (use Publish now or on-approval)",
|
|
50
|
+
);
|
|
51
|
+
const horizon = options.horizonDays ?? MAX_HORIZON_DAYS;
|
|
52
|
+
if (at > now + horizon * DAY_MS)
|
|
53
|
+
throw new ScheduleError("too_far", `a release time must be within ${horizon} days`);
|
|
54
|
+
return when;
|
|
55
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
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
|
+
import type { NodeRef, Release, ReleaseApproval, ReleaseConflict, ReleaseItem, ReleaseNodeKind, ReleaseState, ReleaseWhen } from "@cloudhal/core-contracts";
|
|
19
|
+
export declare const RELEASE_HASH_PREFIX = "rl1:";
|
|
20
|
+
/** The system actor scheduled work runs as (on behalf of a person, §6.3 step 6, S14). */
|
|
21
|
+
export declare const SCHEDULER_ACTOR: Readonly<{
|
|
22
|
+
readonly kind: "system";
|
|
23
|
+
readonly id: "sys_scheduler";
|
|
24
|
+
readonly name: "Scheduler";
|
|
25
|
+
}>;
|
|
26
|
+
export declare const MAX_RELEASE_ITEMS = 100;
|
|
27
|
+
export declare const MAX_OPEN_RELEASES_PER_SITE = 200;
|
|
28
|
+
/** A release runs late at most once and only within this many seconds of its time (owner decision 2). */
|
|
29
|
+
export declare const LATE_LIMIT_SECONDS = 3600;
|
|
30
|
+
/** Transient errors retry inside the hour with this back-off, by attempt (§8.6). */
|
|
31
|
+
export declare const RETRY_BACKOFF_SECONDS: readonly number[];
|
|
32
|
+
/** How long a claim holds a release before another tick may recover it (§8.5). */
|
|
33
|
+
export declare const RELEASE_LEASE_MS: number;
|
|
34
|
+
/** Lead time and horizons (§6.1, §10.5). */
|
|
35
|
+
export declare const MIN_LEAD_MS = 60000;
|
|
36
|
+
export declare const MAX_HORIZON_DAYS = 366;
|
|
37
|
+
export declare const REQUEST_HORIZON_DAYS = 30;
|
|
38
|
+
/** At most this many new bodies per request on Workers Free (§6.1 step 4). */
|
|
39
|
+
export declare const MAX_NEW_BODIES_PER_REQUEST = 20;
|
|
40
|
+
/** Node kinds a release may remove (§6.1 step 3). */
|
|
41
|
+
export declare const REMOVABLE_NODE_KINDS: readonly ReleaseNodeKind[];
|
|
42
|
+
export declare const RELEASE_NODE_KINDS: readonly ReleaseNodeKind[];
|
|
43
|
+
/** States whose nodes are held and locked (§3, §6.2). */
|
|
44
|
+
export declare const HOLDING_STATES: readonly ReleaseState[];
|
|
45
|
+
/** States the scheduler never leaves on its own (kept for history; retention deletes them after 400 days). */
|
|
46
|
+
export declare const TERMINAL_RELEASE_STATES: readonly ReleaseState[];
|
|
47
|
+
export declare function isHolding(state: ReleaseState): boolean;
|
|
48
|
+
export declare function isTerminalRelease(state: ReleaseState): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Allowed transitions (§5.2). Beyond the table: `running → canceled` and `running → expired` are the
|
|
51
|
+
* scheduler's own (a vanished site or node, an approval lost at run time — §10.6; people cannot
|
|
52
|
+
* cancel a running release, §8.5); `conflict → draft | awaiting_approval | scheduled` is "Re-pin"
|
|
53
|
+
* (and approve); `missed | expired → running` is a person's "Publish now" (a manual run).
|
|
54
|
+
*/
|
|
55
|
+
export declare const RELEASE_TRANSITIONS: Readonly<Record<ReleaseState, readonly ReleaseState[]>>;
|
|
56
|
+
export declare function canTransitionRelease(from: ReleaseState, to: ReleaseState): boolean;
|
|
57
|
+
export declare function assertReleaseTransition(from: ReleaseState, to: ReleaseState): void;
|
|
58
|
+
/** One canonical item line (see the module comment). */
|
|
59
|
+
export declare function releaseItemLine(item: ReleaseItem): string;
|
|
60
|
+
/** The lines the `rl1:` hash digests, in order. */
|
|
61
|
+
export declare function releaseHashLines(siteId: string, items: readonly ReleaseItem[]): string[];
|
|
62
|
+
/** `rl1:<hex>` over the site id and the pinned items (time not included, D4). */
|
|
63
|
+
export declare function releaseHash(siteId: string, items: readonly ReleaseItem[]): Promise<string>;
|
|
64
|
+
/**
|
|
65
|
+
* What an approval request's `args_hash` covers for release tools (§10.4): the release and its
|
|
66
|
+
* `when`, canonical. The state binding (`state_hash`) is the release's `rl1:` hash.
|
|
67
|
+
*/
|
|
68
|
+
export declare function approvalArgs(releaseId: string, when: ReleaseWhen | undefined): string;
|
|
69
|
+
/** A node version as the store knows it (a snapshot's `NodeRef`). */
|
|
70
|
+
export type NodeVersion = Pick<NodeRef, "id" | "kind" | "rev" | "hash">;
|
|
71
|
+
/** A pinned item plus what the run needs without reading bodies (§5.1 `release_items`). */
|
|
72
|
+
export interface PinnedItem {
|
|
73
|
+
item: ReleaseItem;
|
|
74
|
+
/** Ids the target body references (terms, assets, parents, menu targets); empty for removals. */
|
|
75
|
+
deps: string[];
|
|
76
|
+
/** An asset target's media blob, a GC root while held (RFC-0004). */
|
|
77
|
+
blobSha?: string;
|
|
78
|
+
}
|
|
79
|
+
export interface PinInput {
|
|
80
|
+
siteId: string;
|
|
81
|
+
/** Nodes whose current working-copy version goes live. */
|
|
82
|
+
set?: readonly string[];
|
|
83
|
+
/** Nodes taken out of the live snapshot (entries, pages, redirects). */
|
|
84
|
+
remove?: readonly string[];
|
|
85
|
+
/** The working copy's refs (`currentNodeRefs`). */
|
|
86
|
+
working: Iterable<NodeVersion> | ReadonlyMap<string, NodeVersion>;
|
|
87
|
+
/** The live snapshot's refs (absent when nothing is live yet). */
|
|
88
|
+
live: Iterable<NodeVersion> | ReadonlyMap<string, NodeVersion>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Pins items (§6.1 step 1): each `set` pins the node's working-copy version as target and its live
|
|
92
|
+
* version (if any) as base; each `remove` pins the live version as base. Refuses the site node, a
|
|
93
|
+
* node that does not exist, a removal of a kind other than entry/page/redirect or of a node that
|
|
94
|
+
* is not live, a node given twice, and an item whose target equals its base (`nothing_to_publish`).
|
|
95
|
+
* Items come back sorted by node id.
|
|
96
|
+
*/
|
|
97
|
+
export declare function pinItems(input: PinInput): ReleaseItem[];
|
|
98
|
+
/**
|
|
99
|
+
* The default dependency extractor: every string in a node body (at any depth) that is exactly a
|
|
100
|
+
* node id of a releasable kind, except the node's own id and `siteId`. This covers term
|
|
101
|
+
* assignments (`terms.*.ids`, `primary`), `parentId`/`taxonomyId`, `ImageRef.assetId` and
|
|
102
|
+
* `coverImage`, menu targets (`pageId`, `entryId`, `termId`). Callers with exact knowledge of a
|
|
103
|
+
* kind (core-ts) may pass their own `depsOf` to `dependencyClosure`.
|
|
104
|
+
*/
|
|
105
|
+
export declare function referencedNodeIds(body: unknown): string[];
|
|
106
|
+
export interface ClosureInput {
|
|
107
|
+
items: readonly ReleaseItem[];
|
|
108
|
+
working: Iterable<NodeVersion> | ReadonlyMap<string, NodeVersion>;
|
|
109
|
+
live: Iterable<NodeVersion> | ReadonlyMap<string, NodeVersion>;
|
|
110
|
+
/** Ids a node version references; for most callers `referencedNodeIds(body)`. */
|
|
111
|
+
depsOf: (ref: NodeVersion) => readonly string[] | Promise<readonly string[]>;
|
|
112
|
+
/** An asset version's media blob (RFC-0004), recorded as a GC root while held. */
|
|
113
|
+
blobOf?: (ref: NodeVersion) => string | undefined | Promise<string | undefined>;
|
|
114
|
+
}
|
|
115
|
+
export interface ClosureResult {
|
|
116
|
+
/** The given items and the added ones, sorted by node id, each with its `deps`. */
|
|
117
|
+
items: PinnedItem[];
|
|
118
|
+
/** Ids added because a target needs them and they are not live (`closureAdded`, §6.1 step 2). */
|
|
119
|
+
added: string[];
|
|
120
|
+
/** Referenced ids that exist neither live nor in the working copy (the release would fail). */
|
|
121
|
+
missing: string[];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Dependency closure (§6.1 step 2): adds, transitively, every node a target references that is not
|
|
125
|
+
* live, pinned at its working-copy version (no base). A referenced node that is live (in any
|
|
126
|
+
* version) is satisfied. Records each target's outgoing ids so the run checks the composed snapshot
|
|
127
|
+
* without reading bodies (§6.3 step 5).
|
|
128
|
+
*/
|
|
129
|
+
export declare function dependencyClosure(input: ClosureInput): Promise<ClosureResult>;
|
|
130
|
+
/**
|
|
131
|
+
* `nodes(S) = nodes(L) − ids + targets` (S1, §6.3 step 4): the live refs without every item's node,
|
|
132
|
+
* plus each `set` item's target, sorted by id (code-unit order, as SQLite's `ORDER BY id`). The site
|
|
133
|
+
* ref stays the live one.
|
|
134
|
+
*/
|
|
135
|
+
export declare function composeRelease(live: readonly NodeRef[], items: readonly ReleaseItem[]): NodeRef[];
|
|
136
|
+
/**
|
|
137
|
+
* Conflicts (§6.4, S4): every item whose node's live version is not its pinned base (both absent
|
|
138
|
+
* counts as equal). Empty when the release can be applied.
|
|
139
|
+
*/
|
|
140
|
+
export declare function releaseConflicts(live: readonly NodeRef[], items: readonly ReleaseItem[]): ReleaseConflict[];
|
|
141
|
+
/** The first dependency of a `set` target missing from the composed snapshot (§6.3 step 5). */
|
|
142
|
+
export declare function missingDependency(composed: readonly NodeRef[], items: readonly Pick<PinnedItem, "item" | "deps">[]): {
|
|
143
|
+
nodeId: string;
|
|
144
|
+
dependency: string;
|
|
145
|
+
} | undefined;
|
|
146
|
+
export type ReleasePlan = {
|
|
147
|
+
kind: "conflict";
|
|
148
|
+
conflicts: ReleaseConflict[];
|
|
149
|
+
} | {
|
|
150
|
+
kind: "failed";
|
|
151
|
+
code: "dependency_missing";
|
|
152
|
+
message: string;
|
|
153
|
+
} | {
|
|
154
|
+
kind: "apply";
|
|
155
|
+
nodes: NodeRef[];
|
|
156
|
+
added: number;
|
|
157
|
+
changed: number;
|
|
158
|
+
removed: number;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Steps 3–5 of §6.3 as one pure function: conflicts first (nothing applied partially, S4), then the
|
|
162
|
+
* composition, then the dependency check. `publishRelease` (core-ts) writes the snapshot it returns.
|
|
163
|
+
*/
|
|
164
|
+
export declare function planRelease(live: readonly NodeRef[], items: readonly Pick<PinnedItem, "item" | "deps">[]): ReleasePlan;
|
|
165
|
+
export type ExpiredReason = "unapproved" | "approval_lost" | "auto_window_ended" | "creator_removed";
|
|
166
|
+
export type ApprovalVerdict = {
|
|
167
|
+
ok: true;
|
|
168
|
+
} | {
|
|
169
|
+
ok: false;
|
|
170
|
+
reason: ExpiredReason;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* The approval checks that need no policy (§10.6): a release without an approval is `unapproved`
|
|
174
|
+
* (D5); an `auto` approval past `windowEnds` is `auto_window_ended` (D9). Rights (the approver may
|
|
175
|
+
* still publish it; the key and its creator still exist) are checked by the policy layer, which
|
|
176
|
+
* `runDue` takes as `checkApproval`.
|
|
177
|
+
*/
|
|
178
|
+
export declare function baseApprovalVerdict(approval: ReleaseApproval | undefined, now: string | Date | number): ApprovalVerdict;
|
|
179
|
+
/** `missed` when more than an hour late (owner decision 2, S8). */
|
|
180
|
+
export declare function isPastLateLimit(executeAt: string | Date | number, now: string | Date | number): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* When a release that failed transiently on `attempt` (1-based) may try again: the back-off of
|
|
183
|
+
* §8.6, or undefined when that would be more than an hour after its time (then it is `missed`).
|
|
184
|
+
*/
|
|
185
|
+
export declare function nextRetryAt(executeAt: string | Date | number, now: string | Date | number, attempt: number): string | undefined;
|
|
186
|
+
export interface ReleaseOrderKey {
|
|
187
|
+
id: string;
|
|
188
|
+
executeAt: string;
|
|
189
|
+
}
|
|
190
|
+
/** Releases run one at a time per site, in `execute_at` order, ties by id (§6.5). */
|
|
191
|
+
export declare function orderReleases<T extends ReleaseOrderKey>(releases: readonly T[]): T[];
|
|
192
|
+
/**
|
|
193
|
+
* Nodes in more than one non-terminal release (D15: flagged, not refused), as node id → release
|
|
194
|
+
* ids (sorted). Studio and `get_release` show these as `overlaps`.
|
|
195
|
+
*/
|
|
196
|
+
export declare function releaseOverlaps(releases: readonly Pick<Release, "id" | "state" | "items">[]): Map<string, string[]>;
|
|
197
|
+
/** Releases whose time falls in the same minute (Studio warns: "2 releases at 09:00", §6.5). */
|
|
198
|
+
export declare function sameMinute(a: string | Date | number, b: string | Date | number): boolean;
|