@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,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Taint accounting for unattended runs (RFC-0008 §11.4, D10). Pure: the MCP layer (rollout steps
|
|
3
|
+
* 8 and 10) calls `checkRunCall` before a tool runs inside an automation run and `afterRunCall`
|
|
4
|
+
* after it, and stores the returned state on the run (`schedule_runs.taint`).
|
|
5
|
+
*
|
|
6
|
+
* 1. Taint per run: a call of a tool that returns visitor-supplied text taints the run; from then on
|
|
7
|
+
* only `read`-class tools, `label_comments` and `reduce`-class tools are allowed (`tainted`).
|
|
8
|
+
* 2. No egress: `fetch_asset_url` is unavailable inside runs in v1.
|
|
9
|
+
* 3. Declared input profile: an automation that reads untrusted text holds no write scopes.
|
|
10
|
+
* 4. Anomaly pause: more than 50 `reduce` calls, or 10 consecutive refusals, end the run (`killed`).
|
|
11
|
+
*/
|
|
12
|
+
/** Per-argument call classes (RFC-0007 plus RFC-0008's `publish`, §10.3). */
|
|
13
|
+
export type CallClass = "read" | "advisory" | "reduce" | "publish" | "person";
|
|
14
|
+
/** Tools whose results carry visitor-supplied text (§11.4 item 1). */
|
|
15
|
+
export declare const TAINTING_TOOLS: readonly string[];
|
|
16
|
+
/** Tools that are not `read`/`reduce` but stay allowed in a tainted run. */
|
|
17
|
+
export declare const TAINTED_RUN_EXTRA_TOOLS: readonly string[];
|
|
18
|
+
/** Tools unavailable inside any run (§11.4 item 2). */
|
|
19
|
+
export declare const NO_EGRESS_TOOLS: readonly string[];
|
|
20
|
+
/** Scopes an automation that reads untrusted text may not hold (§11.4 item 3). */
|
|
21
|
+
export declare const UNTRUSTED_FORBIDDEN_SCOPES: readonly string[];
|
|
22
|
+
/** Scope prefixes refused for such automations (`asset:*`). */
|
|
23
|
+
export declare const UNTRUSTED_FORBIDDEN_SCOPE_PREFIXES: readonly string[];
|
|
24
|
+
export declare const ANOMALY_LIMITS: {
|
|
25
|
+
readonly maxReduceCalls: 50;
|
|
26
|
+
readonly maxConsecutiveRefusals: 10;
|
|
27
|
+
};
|
|
28
|
+
/** What a run remembers between calls. */
|
|
29
|
+
export interface RunTaintState {
|
|
30
|
+
taint?: {
|
|
31
|
+
at: string;
|
|
32
|
+
tool: string;
|
|
33
|
+
};
|
|
34
|
+
reduceCalls: number;
|
|
35
|
+
consecutiveRefusals: number;
|
|
36
|
+
}
|
|
37
|
+
export declare const CLEAN_RUN: RunTaintState;
|
|
38
|
+
export interface RunCall {
|
|
39
|
+
tool: string;
|
|
40
|
+
class: CallClass;
|
|
41
|
+
}
|
|
42
|
+
export type RunCallCheck = {
|
|
43
|
+
allowed: true;
|
|
44
|
+
} | {
|
|
45
|
+
allowed: false;
|
|
46
|
+
code: "tainted" | "no_egress_in_runs";
|
|
47
|
+
message: string;
|
|
48
|
+
};
|
|
49
|
+
/** Whether a call may run inside an automation run in this taint state. */
|
|
50
|
+
export declare function checkRunCall(state: RunTaintState, call: RunCall): RunCallCheck;
|
|
51
|
+
export interface AfterRunCall {
|
|
52
|
+
state: RunTaintState;
|
|
53
|
+
/** The run must end as `killed` and its automation pause (anomaly, §11.4 item 6). */
|
|
54
|
+
kill?: "too_many_reduce_calls" | "too_many_refusals";
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The run's state after a call: tainted from the first tainting tool on (`at` the call's time), a
|
|
58
|
+
* count of `reduce` calls and of consecutive refusals (policy or taint).
|
|
59
|
+
*/
|
|
60
|
+
export declare function afterRunCall(state: RunTaintState, call: RunCall, result: "allowed" | "refused", at: string): AfterRunCall;
|
|
61
|
+
export declare function isUntrustedForbiddenScope(scope: string): boolean;
|
|
62
|
+
/** The scopes refused for an automation that reads untrusted text (empty: allowed). */
|
|
63
|
+
export declare function forbiddenUntrustedScopes(scopes: readonly string[]): string[];
|
|
64
|
+
/** The key's scopes as they apply on every call of such an automation: write scopes stripped. */
|
|
65
|
+
export declare function untrustedEffectiveScopes(scopes: readonly string[]): string[];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Taint accounting for unattended runs (RFC-0008 §11.4, D10). Pure: the MCP layer (rollout steps
|
|
3
|
+
* 8 and 10) calls `checkRunCall` before a tool runs inside an automation run and `afterRunCall`
|
|
4
|
+
* after it, and stores the returned state on the run (`schedule_runs.taint`).
|
|
5
|
+
*
|
|
6
|
+
* 1. Taint per run: a call of a tool that returns visitor-supplied text taints the run; from then on
|
|
7
|
+
* only `read`-class tools, `label_comments` and `reduce`-class tools are allowed (`tainted`).
|
|
8
|
+
* 2. No egress: `fetch_asset_url` is unavailable inside runs in v1.
|
|
9
|
+
* 3. Declared input profile: an automation that reads untrusted text holds no write scopes.
|
|
10
|
+
* 4. Anomaly pause: more than 50 `reduce` calls, or 10 consecutive refusals, end the run (`killed`).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Per-argument call classes (RFC-0007 plus RFC-0008's `publish`, §10.3). */
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/** Tools whose results carry visitor-supplied text (§11.4 item 1). */
|
|
17
|
+
export const TAINTING_TOOLS = [
|
|
18
|
+
"list_comments",
|
|
19
|
+
"get_comment_thread",
|
|
20
|
+
"broken_links",
|
|
21
|
+
"get_analytics",
|
|
22
|
+
"top_content",
|
|
23
|
+
"fetch_asset_url",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** Tools that are not `read`/`reduce` but stay allowed in a tainted run. */
|
|
27
|
+
export const TAINTED_RUN_EXTRA_TOOLS = [
|
|
28
|
+
"label_comments",
|
|
29
|
+
"pause_automation",
|
|
30
|
+
"end_run",
|
|
31
|
+
"get_mode",
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
/** Tools unavailable inside any run (§11.4 item 2). */
|
|
35
|
+
export const NO_EGRESS_TOOLS = ["fetch_asset_url"];
|
|
36
|
+
|
|
37
|
+
/** Scopes an automation that reads untrusted text may not hold (§11.4 item 3). */
|
|
38
|
+
export const UNTRUSTED_FORBIDDEN_SCOPES = [
|
|
39
|
+
"content:write",
|
|
40
|
+
"content:write:own",
|
|
41
|
+
"site:publish",
|
|
42
|
+
"site:publish:own",
|
|
43
|
+
"schedules:write",
|
|
44
|
+
"schedules:write:own",
|
|
45
|
+
"releases:publish",
|
|
46
|
+
"releases:publish:own",
|
|
47
|
+
];
|
|
48
|
+
/** Scope prefixes refused for such automations (`asset:*`). */
|
|
49
|
+
export const UNTRUSTED_FORBIDDEN_SCOPE_PREFIXES = ["asset:"];
|
|
50
|
+
|
|
51
|
+
export const ANOMALY_LIMITS = { maxReduceCalls: 50, maxConsecutiveRefusals: 10 } ;
|
|
52
|
+
|
|
53
|
+
/** What a run remembers between calls. */
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
export const CLEAN_RUN = Object.freeze({ reduceCalls: 0, consecutiveRefusals: 0 });
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/** Whether a call may run inside an automation run in this taint state. */
|
|
72
|
+
export function checkRunCall(state , call ) {
|
|
73
|
+
if (NO_EGRESS_TOOLS.includes(call.tool))
|
|
74
|
+
return {
|
|
75
|
+
allowed: false,
|
|
76
|
+
code: "no_egress_in_runs",
|
|
77
|
+
message: `${call.tool} is not available inside runs`,
|
|
78
|
+
};
|
|
79
|
+
if (!state.taint) return { allowed: true };
|
|
80
|
+
if (
|
|
81
|
+
call.class === "read" ||
|
|
82
|
+
call.class === "reduce" ||
|
|
83
|
+
TAINTED_RUN_EXTRA_TOOLS.includes(call.tool)
|
|
84
|
+
)
|
|
85
|
+
return { allowed: true };
|
|
86
|
+
return {
|
|
87
|
+
allowed: false,
|
|
88
|
+
code: "tainted",
|
|
89
|
+
message: `this run read visitor-supplied text (${state.taint.tool}); it may only read, label, hide and pause`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The run's state after a call: tainted from the first tainting tool on (`at` the call's time), a
|
|
101
|
+
* count of `reduce` calls and of consecutive refusals (policy or taint).
|
|
102
|
+
*/
|
|
103
|
+
export function afterRunCall(
|
|
104
|
+
state ,
|
|
105
|
+
call ,
|
|
106
|
+
result ,
|
|
107
|
+
at ,
|
|
108
|
+
) {
|
|
109
|
+
const next = {
|
|
110
|
+
...(state.taint ? { taint: state.taint } : {}),
|
|
111
|
+
reduceCalls: state.reduceCalls + (result === "allowed" && call.class === "reduce" ? 1 : 0),
|
|
112
|
+
consecutiveRefusals: result === "refused" ? state.consecutiveRefusals + 1 : 0,
|
|
113
|
+
};
|
|
114
|
+
if (!next.taint && result === "allowed" && TAINTING_TOOLS.includes(call.tool))
|
|
115
|
+
next.taint = { at, tool: call.tool };
|
|
116
|
+
if (next.reduceCalls > ANOMALY_LIMITS.maxReduceCalls)
|
|
117
|
+
return { state: next, kill: "too_many_reduce_calls" };
|
|
118
|
+
if (next.consecutiveRefusals >= ANOMALY_LIMITS.maxConsecutiveRefusals)
|
|
119
|
+
return { state: next, kill: "too_many_refusals" };
|
|
120
|
+
return { state: next };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function isUntrustedForbiddenScope(scope ) {
|
|
124
|
+
return (
|
|
125
|
+
UNTRUSTED_FORBIDDEN_SCOPES.includes(scope) ||
|
|
126
|
+
UNTRUSTED_FORBIDDEN_SCOPE_PREFIXES.some((p) => scope.startsWith(p))
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The scopes refused for an automation that reads untrusted text (empty: allowed). */
|
|
131
|
+
export function forbiddenUntrustedScopes(scopes ) {
|
|
132
|
+
return scopes.filter(isUntrustedForbiddenScope);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The key's scopes as they apply on every call of such an automation: write scopes stripped. */
|
|
136
|
+
export function untrustedEffectiveScopes(scopes ) {
|
|
137
|
+
return scopes.filter((s) => !isUntrustedForbiddenScope(s));
|
|
138
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** One line of plain text the contracts accept: unsafe characters become spaces, at most `max` long. */
|
|
2
|
+
export declare function plainLine(text: string, max?: number): string;
|
|
3
|
+
/** Plain text for a run summary (tabs and new lines kept), at most `max` long. */
|
|
4
|
+
export declare function plainText(text: string, max?: number): string;
|
|
5
|
+
/** Findings counts as the contract takes them: lower-case keys, whole non-negative numbers, at most 20. */
|
|
6
|
+
export declare function cleanCounts(counts: Record<string, number>): Record<string, number>;
|
package/dist/src/text.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Control characters and bidi overrides the contracts refuse in one-line text (messages, errors). */
|
|
2
|
+
const UNSAFE_LINE = /[\p{Cc}--]/gu;
|
|
3
|
+
/** The same, except tab and line feed, which a run summary may hold (`ScheduleRun.summary`). */
|
|
4
|
+
const UNSAFE_TEXT = /[^\P{Cc}\t\n]|[--]/gu;
|
|
5
|
+
const COUNT_KEY = /^[a-z][a-z0-9-]{0,40}$/;
|
|
6
|
+
|
|
7
|
+
/** One line of plain text the contracts accept: unsafe characters become spaces, at most `max` long. */
|
|
8
|
+
export function plainLine(text , max = 500) {
|
|
9
|
+
return text.replace(UNSAFE_LINE, " ").slice(0, max);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Plain text for a run summary (tabs and new lines kept), at most `max` long. */
|
|
13
|
+
export function plainText(text , max = 2_000) {
|
|
14
|
+
return text.replace(UNSAFE_TEXT, " ").slice(0, max);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Findings counts as the contract takes them: lower-case keys, whole non-negative numbers, at most 20. */
|
|
18
|
+
export function cleanCounts(counts ) {
|
|
19
|
+
const out = {};
|
|
20
|
+
for (const [k, v] of Object.entries(counts)) {
|
|
21
|
+
if (Object.keys(out).length >= 20) break;
|
|
22
|
+
if (COUNT_KEY.test(k) && Number.isFinite(v)) out[k] = Math.max(0, Math.floor(v));
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules/time — the time model alone (RFC-0008 §9): instants, wall-clock times in a
|
|
3
|
+
* zone, never-early recomputation. `@cloudhal/core-ts/time` re-exports from here, so Cloudhal has
|
|
4
|
+
* one time model. Uses `temporal-polyfill/implementation`: keep it out of the site Worker.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./time.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules/time — the time model alone (RFC-0008 §9): instants, wall-clock times in a
|
|
3
|
+
* zone, never-early recomputation. `@cloudhal/core-ts/time` re-exports from here, so Cloudhal has
|
|
4
|
+
* one time model. Uses `temporal-polyfill/implementation`: keep it out of the site Worker.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./time.js";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The time model (RFC-0008 §9). Wall-clock time plus an IANA zone, resolved to an instant with
|
|
3
|
+
* `temporal-polyfill/implementation` — the only Temporal entry point in Cloudhal: it never touches
|
|
4
|
+
* a native or global `Temporal`, and nothing here reads a clock. `now` is always injected by the
|
|
5
|
+
* caller (`Date.now()` at the edge of the system), so DST, tzdata and lateness are testable with a
|
|
6
|
+
* fake clock (S16).
|
|
7
|
+
*
|
|
8
|
+
* DST (D2): `disambiguation: "compatible"` — a wall-clock time inside a spring-forward gap moves to
|
|
9
|
+
* the next valid time (02:30 → 03:30 in Berlin on 29 March 2026, `adjusted: "dst-gap"`); a time
|
|
10
|
+
* that occurs twice resolves to the first occurrence (+02:00 on 25 October 2026).
|
|
11
|
+
*
|
|
12
|
+
* Never early (§9.1): `recomputeExecuteAt` re-resolves a stored wall-clock time with the runtime's
|
|
13
|
+
* current tzdata and only ever moves the instant later.
|
|
14
|
+
*/
|
|
15
|
+
import { Temporal } from "temporal-polyfill/implementation";
|
|
16
|
+
export * from "./instant.js";
|
|
17
|
+
/**
|
|
18
|
+
* True when `name` has the contract's shape and the runtime's time-zone data knows it (aliases
|
|
19
|
+
* such as `Europe/Kiev` included). Writers call this before storing a zone (§4.1).
|
|
20
|
+
*/
|
|
21
|
+
export declare function isTimeZone(name: unknown): name is string;
|
|
22
|
+
export declare function assertTimeZone(name: unknown): asserts name is string;
|
|
23
|
+
export declare function isLocalDateTime(value: unknown): value is string;
|
|
24
|
+
/** An instant resolved from a wall-clock time. */
|
|
25
|
+
export interface ResolvedTime {
|
|
26
|
+
/** The instant, canonical (`2026-11-02T06:00:00.000Z`). */
|
|
27
|
+
at: string;
|
|
28
|
+
/** The zone's UTC offset at that instant (`+03:00`). */
|
|
29
|
+
offset: string;
|
|
30
|
+
/** Present when the wall-clock time did not exist and moved forward (D2). */
|
|
31
|
+
adjusted?: "dst-gap";
|
|
32
|
+
}
|
|
33
|
+
/** Resolves a Temporal wall-clock value in `zone` with D2 (gap → later, overlap → first). */
|
|
34
|
+
export declare function resolvePlain(plain: Temporal.PlainDateTime, zone: string): ResolvedTime;
|
|
35
|
+
/** `2026-11-02T09:00` in `Europe/Istanbul` → `{ at: "2026-11-02T06:00:00.000Z", offset: "+03:00" }`. */
|
|
36
|
+
export declare function resolveLocalTime(local: string, zone: string): ResolvedTime;
|
|
37
|
+
/** The wall-clock time (to the minute, seconds dropped) of an instant in `zone`. */
|
|
38
|
+
export declare function toLocalDateTime(instant: string | Date | number, zone: string): string;
|
|
39
|
+
/** The UTC offset (`+03:00`) of `zone` at an instant. */
|
|
40
|
+
export declare function zoneOffset(instant: string | Date | number, zone: string): string;
|
|
41
|
+
/** When a release (or any one-off) is due: the contract's `ReleaseAt` without its `kind`. */
|
|
42
|
+
export interface WhenAt {
|
|
43
|
+
at: string;
|
|
44
|
+
timeZone: string;
|
|
45
|
+
localTime?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface RecomputedTime {
|
|
48
|
+
/** The instant to run at: never earlier than the stored one, never earlier than the wall clock. */
|
|
49
|
+
at: string;
|
|
50
|
+
/** True when `at` moved. */
|
|
51
|
+
changed: boolean;
|
|
52
|
+
/** `tzdata`: the zone's rules now put the wall-clock time later than the stored instant. */
|
|
53
|
+
adjusted?: "tzdata";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Never early (§9.1, S2). With a stored wall-clock time, re-resolve it with the runtime's current
|
|
57
|
+
* tzdata; if that instant is later than the stored `at`, the release moves to it (`tzdata`). If it
|
|
58
|
+
* is earlier, the stored `at` stands (running later than the wall clock is late, not early). An
|
|
59
|
+
* instant without `localTime` is a machine's absolute intent and never moves.
|
|
60
|
+
*/
|
|
61
|
+
export declare function recomputeExecuteAt(when: WhenAt): RecomputedTime;
|
package/dist/src/time.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The time model (RFC-0008 §9). Wall-clock time plus an IANA zone, resolved to an instant with
|
|
3
|
+
* `temporal-polyfill/implementation` — the only Temporal entry point in Cloudhal: it never touches
|
|
4
|
+
* a native or global `Temporal`, and nothing here reads a clock. `now` is always injected by the
|
|
5
|
+
* caller (`Date.now()` at the edge of the system), so DST, tzdata and lateness are testable with a
|
|
6
|
+
* fake clock (S16).
|
|
7
|
+
*
|
|
8
|
+
* DST (D2): `disambiguation: "compatible"` — a wall-clock time inside a spring-forward gap moves to
|
|
9
|
+
* the next valid time (02:30 → 03:30 in Berlin on 29 March 2026, `adjusted: "dst-gap"`); a time
|
|
10
|
+
* that occurs twice resolves to the first occurrence (+02:00 on 25 October 2026).
|
|
11
|
+
*
|
|
12
|
+
* Never early (§9.1): `recomputeExecuteAt` re-resolves a stored wall-clock time with the runtime's
|
|
13
|
+
* current tzdata and only ever moves the instant later.
|
|
14
|
+
*/
|
|
15
|
+
import { Temporal } from "temporal-polyfill/implementation";
|
|
16
|
+
import { ScheduleError } from "./errors.js";
|
|
17
|
+
import {
|
|
18
|
+
canonicalInstant,
|
|
19
|
+
floorToMinute,
|
|
20
|
+
instantMs,
|
|
21
|
+
LOCAL_DATE_TIME_PATTERN,
|
|
22
|
+
TIME_ZONE_PATTERN,
|
|
23
|
+
} from "./instant.js";
|
|
24
|
+
|
|
25
|
+
export * from "./instant.js";
|
|
26
|
+
|
|
27
|
+
const zoneCache = new Map ();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* True when `name` has the contract's shape and the runtime's time-zone data knows it (aliases
|
|
31
|
+
* such as `Europe/Kiev` included). Writers call this before storing a zone (§4.1).
|
|
32
|
+
*/
|
|
33
|
+
export function isTimeZone(name ) {
|
|
34
|
+
if (typeof name !== "string" || name.length > 64 || !TIME_ZONE_PATTERN.test(name)) return false;
|
|
35
|
+
const cached = zoneCache.get(name);
|
|
36
|
+
if (cached !== undefined) return cached;
|
|
37
|
+
let ok = false;
|
|
38
|
+
try {
|
|
39
|
+
Temporal.Instant.fromEpochMilliseconds(0).toZonedDateTimeISO(name);
|
|
40
|
+
ok = true;
|
|
41
|
+
} catch {
|
|
42
|
+
ok = false;
|
|
43
|
+
}
|
|
44
|
+
zoneCache.set(name, ok);
|
|
45
|
+
return ok;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function assertTimeZone(name ) {
|
|
49
|
+
if (!isTimeZone(name))
|
|
50
|
+
throw new ScheduleError("invalid_time_zone", `unknown time zone: ${String(name)}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isLocalDateTime(value ) {
|
|
54
|
+
if (typeof value !== "string" || !LOCAL_DATE_TIME_PATTERN.test(value)) return false;
|
|
55
|
+
try {
|
|
56
|
+
Temporal.PlainDateTime.from(value, { overflow: "reject" });
|
|
57
|
+
return true;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function plainDateTime(local ) {
|
|
64
|
+
if (!isLocalDateTime(local))
|
|
65
|
+
throw new ScheduleError(
|
|
66
|
+
"invalid_local_time",
|
|
67
|
+
`not a wall-clock time (YYYY-MM-DDTHH:mm): ${local}`,
|
|
68
|
+
);
|
|
69
|
+
return Temporal.PlainDateTime.from(local, { overflow: "reject" });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** An instant resolved from a wall-clock time. */
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
/** Resolves a Temporal wall-clock value in `zone` with D2 (gap → later, overlap → first). */
|
|
83
|
+
export function resolvePlain(plain , zone ) {
|
|
84
|
+
const zdt = plain.toZonedDateTime(zone, { disambiguation: "compatible" });
|
|
85
|
+
const at = new Date(zdt.epochMilliseconds).toISOString();
|
|
86
|
+
const moved = Temporal.PlainDateTime.compare(zdt.toPlainDateTime(), plain) !== 0;
|
|
87
|
+
return { at, offset: zdt.offset, ...(moved ? { adjusted: "dst-gap" } : {}) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `2026-11-02T09:00` in `Europe/Istanbul` → `{ at: "2026-11-02T06:00:00.000Z", offset: "+03:00" }`. */
|
|
91
|
+
export function resolveLocalTime(local , zone ) {
|
|
92
|
+
assertTimeZone(zone);
|
|
93
|
+
return resolvePlain(plainDateTime(local), zone);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The wall-clock time (to the minute, seconds dropped) of an instant in `zone`. */
|
|
97
|
+
export function toLocalDateTime(instant , zone ) {
|
|
98
|
+
assertTimeZone(zone);
|
|
99
|
+
const zdt = Temporal.Instant.fromEpochMilliseconds(floorToMinute(instant)).toZonedDateTimeISO(
|
|
100
|
+
zone,
|
|
101
|
+
);
|
|
102
|
+
return zdt.toPlainDateTime().toString({ smallestUnit: "minute" });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The UTC offset (`+03:00`) of `zone` at an instant. */
|
|
106
|
+
export function zoneOffset(instant , zone ) {
|
|
107
|
+
assertTimeZone(zone);
|
|
108
|
+
return Temporal.Instant.fromEpochMilliseconds(instantMs(instant)).toZonedDateTimeISO(zone).offset;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** When a release (or any one-off) is due: the contract's `ReleaseAt` without its `kind`. */
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Never early (§9.1, S2). With a stored wall-clock time, re-resolve it with the runtime's current
|
|
129
|
+
* tzdata; if that instant is later than the stored `at`, the release moves to it (`tzdata`). If it
|
|
130
|
+
* is earlier, the stored `at` stands (running later than the wall clock is late, not early). An
|
|
131
|
+
* instant without `localTime` is a machine's absolute intent and never moves.
|
|
132
|
+
*/
|
|
133
|
+
export function recomputeExecuteAt(when ) {
|
|
134
|
+
const stored = canonicalInstant(when.at);
|
|
135
|
+
if (!when.localTime) return { at: stored, changed: false };
|
|
136
|
+
const resolved = resolveLocalTime(when.localTime, when.timeZone);
|
|
137
|
+
if (Date.parse(resolved.at) > Date.parse(stored))
|
|
138
|
+
return { at: resolved.at, changed: true, adjusted: "tzdata" };
|
|
139
|
+
return { at: stored, changed: false };
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudhal/schedules",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cloudhal schedules (RFC-0008): the time model, the RRULE subset and its sentences, releases (pin, closure, rl1: hash, compose, conflicts, state machine), automation and run state, taint and budget accounting, the Scheduler and runDue, and the SQLite/D1 store. Worker-safe.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://cloudhal.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/cloudhal-dev/cloudhal.git",
|
|
10
|
+
"directory": "packages/schedules"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/cloudhal-dev/cloudhal/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/src/index.d.ts",
|
|
19
|
+
"default": "./dist/src/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./engine": {
|
|
22
|
+
"types": "./dist/src/engine.d.ts",
|
|
23
|
+
"default": "./dist/src/engine.js"
|
|
24
|
+
},
|
|
25
|
+
"./time": {
|
|
26
|
+
"types": "./dist/src/time-entry.d.ts",
|
|
27
|
+
"default": "./dist/src/time-entry.js"
|
|
28
|
+
},
|
|
29
|
+
"./recurrence": {
|
|
30
|
+
"types": "./dist/src/recurrence.d.ts",
|
|
31
|
+
"default": "./dist/src/recurrence.js"
|
|
32
|
+
},
|
|
33
|
+
"./sql": {
|
|
34
|
+
"types": "./dist/src/sql/index.d.ts",
|
|
35
|
+
"default": "./dist/src/sql/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./testing": {
|
|
38
|
+
"types": "./dist/src/testing.d.ts",
|
|
39
|
+
"default": "./dist/src/testing.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md"
|
|
45
|
+
],
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@cloudhal/core-contracts": "0.1.0",
|
|
48
|
+
"@cloudhal/storage": "0.1.0",
|
|
49
|
+
"temporal-polyfill": "1.0.5"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@cloudflare/workers-types": "^5.20261007.1",
|
|
53
|
+
"@types/better-sqlite3": "^9.6.0",
|
|
54
|
+
"@types/node": "^26.6.5",
|
|
55
|
+
"better-sqlite3": "13.0.3",
|
|
56
|
+
"miniflare": "4.20260730.0",
|
|
57
|
+
"rrule-temporal": "2.2.8",
|
|
58
|
+
"typescript": "7.0.2",
|
|
59
|
+
"vitest": "5.0.3"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=24"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"check": "pnpm typecheck && pnpm test"
|
|
71
|
+
}
|
|
6
72
|
}
|