@niscorp/tide 0.1.10 → 0.1.11
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/CHANGELOG.md +14 -0
- package/DESIGN.md +4 -1
- package/DOCS.md +37 -9
- package/dist/agent.cjs +10 -2
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +1 -1
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +1 -1
- package/dist/{chunk-ZEEJLISU.js → chunk-2HCRTQTB.js} +27 -5
- package/dist/chunk-2HCRTQTB.js.map +1 -0
- package/dist/{chunk-7KHOAJNL.js → chunk-MGGWP6C7.js} +2 -2
- package/dist/chunk-MGGWP6C7.js.map +1 -0
- package/dist/index.cjs +74 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -17
- package/dist/index.d.ts +13 -17
- package/dist/index.js +51 -26
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.cjs +10 -2
- package/dist/migrations/index.cjs.map +1 -1
- package/dist/migrations/index.js +1 -1
- package/dist/postgres.cjs +10 -2
- package/dist/postgres.cjs.map +1 -1
- package/dist/postgres.d.cts +1 -1
- package/dist/postgres.d.ts +1 -1
- package/dist/postgres.js +2 -2
- package/dist/{reflex.schema-DW1znJfS.d.cts → reflex.schema-wJ0MHKQn.d.cts} +62 -1
- package/dist/{reflex.schema-DW1znJfS.d.ts → reflex.schema-wJ0MHKQn.d.ts} +62 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/{types-lAPfp7JD.d.ts → types-Bpep20hD.d.cts} +15 -1
- package/dist/{types-lAPfp7JD.d.cts → types-Bpep20hD.d.ts} +15 -1
- package/package.json +3 -3
- package/dist/chunk-7KHOAJNL.js.map +0 -1
- package/dist/chunk-ZEEJLISU.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -7,6 +7,14 @@ var WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
|
7
7
|
var HHMM = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
8
8
|
var MMDD = /^(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
9
9
|
var LOCAL_DATETIME = /^\d{4}-\d{2}-\d{2}T([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/;
|
|
10
|
+
var knownZone = (tz) => {
|
|
11
|
+
try {
|
|
12
|
+
return new Intl.DateTimeFormat("en-US", { timeZone: tz }).resolvedOptions().timeZone !== "";
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var TimeZoneSchema = zod.z.string().refine(knownZone, { message: "not a time zone Intl knows" }).describe("IANA timezone name.");
|
|
10
18
|
var ClockUnitSchema = zod.z.enum(["day", "week", "month", "year"]);
|
|
11
19
|
var ClockRecurringSchema = zod.z.object({
|
|
12
20
|
every: ClockUnitSchema.describe("The calendar period it repeats on. A repeating clock fires on every such period until the reflex is removed \u2014 only for a request that says it repeats."),
|
|
@@ -14,7 +22,7 @@ var ClockRecurringSchema = zod.z.object({
|
|
|
14
22
|
'week \u2192 a weekday, or a list of them: ["mon","tue","wed","thu","fri"] is every weekday; month \u2192 1..31, where 31 is the last day of every month (a shorter month clamps to its end); year \u2192 "MM-DD". Omitted for day.'
|
|
15
23
|
),
|
|
16
24
|
at: zod.z.string().regex(HHMM).describe('Local wall-clock time, "HH:MM".'),
|
|
17
|
-
tz:
|
|
25
|
+
tz: TimeZoneSchema
|
|
18
26
|
}).strict().superRefine((clock, ctx) => {
|
|
19
27
|
const fail = (message) => ctx.addIssue({ code: "custom", message, path: ["on"] });
|
|
20
28
|
if (clock.every === "day" && clock.on !== void 0) fail("a daily clock takes no `on`");
|
|
@@ -28,7 +36,7 @@ var ClockRecurringSchema = zod.z.object({
|
|
|
28
36
|
}).describe("Repeats: fires on every day, week, month or year, at a time of day, until the reflex is removed.");
|
|
29
37
|
var ClockOnceSchema = zod.z.object({
|
|
30
38
|
at: zod.z.string().regex(LOCAL_DATETIME).describe('A fixed local date and time, once: "YYYY-MM-DDTHH:MM" or, to the second, "YYYY-MM-DDTHH:MM:SS".'),
|
|
31
|
-
tz:
|
|
39
|
+
tz: TimeZoneSchema
|
|
32
40
|
}).strict().describe("Once: fires at one date and time, then never again.");
|
|
33
41
|
var ClockTriggerSchema = zod.z.object({ clock: zod.z.union([ClockRecurringSchema, ClockOnceSchema]) }).strict().describe("Fires on the calendar: once, at a date and time, or repeating at a time of day.");
|
|
34
42
|
var OpSchema = zod.z.enum(["insert", "update", "delete"]);
|
|
@@ -153,6 +161,20 @@ var reflexRules = (reflex, ctx) => {
|
|
|
153
161
|
ctx.addIssue({ code: "custom", message: "`each` mode needs a unitKey \u2014 it is the task's idempotency grain", path: ["select", "unitKey"] });
|
|
154
162
|
};
|
|
155
163
|
var ReflexSchema = zod.z.object(reflexFields).strict().superRefine(reflexRules);
|
|
164
|
+
var REFLEX_TRIGGERS = {
|
|
165
|
+
clock: ClockTriggerSchema,
|
|
166
|
+
write: WriteTriggerSchema,
|
|
167
|
+
signal: SignalTriggerSchema,
|
|
168
|
+
run: RunTriggerSchema,
|
|
169
|
+
manual: ManualTriggerSchema
|
|
170
|
+
};
|
|
171
|
+
var reflexSchemaOf = (choice = {}) => {
|
|
172
|
+
if (choice.triggers === void 0) return ReflexSchema;
|
|
173
|
+
const [first, ...rest] = choice.triggers.map((kind) => REFLEX_TRIGGERS[kind]);
|
|
174
|
+
if (first === void 0) throw new Error("tide: a reflex needs at least one trigger kind");
|
|
175
|
+
const on = rest.length === 0 ? first : zod.z.union([first, ...rest]);
|
|
176
|
+
return zod.z.object({ ...reflexFields, on }).strict().superRefine(reflexRules);
|
|
177
|
+
};
|
|
156
178
|
var TimerSchema = zod.z.object({
|
|
157
179
|
hours: zod.z.number().int().min(0).optional(),
|
|
158
180
|
minutes: zod.z.number().int().min(0).optional(),
|
|
@@ -262,13 +284,13 @@ var buildGraph = (reflexes, effects) => {
|
|
|
262
284
|
const existing = runWatchers.get(fact.run) ?? [];
|
|
263
285
|
runWatchers.set(fact.run, [...existing, reflex.id]);
|
|
264
286
|
if (fact.run === reflex.id) warnings.push(`${reflex.id} subscribes to its own run \u2014 a drain loop; make sure it narrows`);
|
|
265
|
-
if (!byId.has(fact.run)) refusals.push({ code: "unknown_reflex", message: `${reflex.id} watches the run of unknown reflex "${fact.run}"
|
|
287
|
+
if (!byId.has(fact.run)) refusals.push({ code: "unknown_reflex", message: `${reflex.id} watches the run of unknown reflex "${fact.run}"`, reflexIds: [reflex.id] });
|
|
266
288
|
}
|
|
267
289
|
}
|
|
268
290
|
for (const reflex of reflexes) {
|
|
269
291
|
const handler = effects[reflex.effect.name];
|
|
270
292
|
if (handler === void 0) {
|
|
271
|
-
refusals.push({ code: "unknown_effect", message: `${reflex.id} names effect "${reflex.effect.name}", which is not registered
|
|
293
|
+
refusals.push({ code: "unknown_effect", message: `${reflex.id} names effect "${reflex.effect.name}", which is not registered`, reflexIds: [reflex.id] });
|
|
272
294
|
continue;
|
|
273
295
|
}
|
|
274
296
|
if (handler.writes === void 0) {
|
|
@@ -291,7 +313,8 @@ var buildGraph = (reflexes, effects) => {
|
|
|
291
313
|
if (!cycle.guarded)
|
|
292
314
|
refusals.push({
|
|
293
315
|
code: "unguarded_cycle",
|
|
294
|
-
message: `unguarded cycle: ${cycle.reflexIds.join(" \u2192 ")} \u2192 ${cycle.reflexIds[0]} \u2014 no selection and no \`when\` anywhere on the loop, so it diverges by construction
|
|
316
|
+
message: `unguarded cycle: ${cycle.reflexIds.join(" \u2192 ")} \u2192 ${cycle.reflexIds[0]} \u2014 no selection and no \`when\` anywhere on the loop, so it diverges by construction`,
|
|
317
|
+
reflexIds: cycle.reflexIds
|
|
295
318
|
});
|
|
296
319
|
return { edges, cycles, blind, errors: refusals.map((refusal) => refusal.message), refusals, warnings };
|
|
297
320
|
};
|
|
@@ -347,6 +370,42 @@ var findCycles = (reflexes, edges) => {
|
|
|
347
370
|
});
|
|
348
371
|
};
|
|
349
372
|
|
|
373
|
+
// src/engine/admit.ts
|
|
374
|
+
var idOf = (candidate) => typeof candidate === "object" && candidate !== null && "id" in candidate && typeof candidate.id === "string" ? candidate.id : void 0;
|
|
375
|
+
var admitReflexes = (input, effectsOf, isolate) => {
|
|
376
|
+
const parsed = [];
|
|
377
|
+
const refused = [];
|
|
378
|
+
const seen = /* @__PURE__ */ new Set();
|
|
379
|
+
for (const candidate of input) {
|
|
380
|
+
const result = ReflexSchema.safeParse(candidate);
|
|
381
|
+
if (!result.success) {
|
|
382
|
+
const message = `reflex did not parse: ${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`;
|
|
383
|
+
if (!isolate) throw new TideError("invalid_reflex", message, { issues: result.error.issues });
|
|
384
|
+
refused.push({ reflexId: idOf(candidate), code: "invalid_reflex", message });
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (seen.has(result.data.id)) {
|
|
388
|
+
const message = `duplicate reflex id "${result.data.id}"`;
|
|
389
|
+
if (!isolate) throw new TideError("duplicate_reflex", message);
|
|
390
|
+
refused.push({ reflexId: result.data.id, code: "duplicate_reflex", message });
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
seen.add(result.data.id);
|
|
394
|
+
parsed.push(result.data);
|
|
395
|
+
}
|
|
396
|
+
let kept = parsed;
|
|
397
|
+
let graph = buildGraph(kept, effectsOf(kept));
|
|
398
|
+
for (let [first] = graph.refusals; first !== void 0; [first] = graph.refusals) {
|
|
399
|
+
if (!isolate) throw new TideError(first.code, `tide refused to load:
|
|
400
|
+
${graph.errors.join("\n ")}`, { errors: graph.errors, refusals: graph.refusals });
|
|
401
|
+
const out = new Set(graph.refusals.flatMap((refusal) => refusal.reflexIds));
|
|
402
|
+
for (const refusal of graph.refusals) for (const reflexId of refusal.reflexIds) refused.push({ reflexId, code: refusal.code, message: refusal.message });
|
|
403
|
+
kept = kept.filter((reflex) => !out.has(reflex.id));
|
|
404
|
+
graph = buildGraph(kept, effectsOf(kept));
|
|
405
|
+
}
|
|
406
|
+
return { reflexes: kept, graph, refused };
|
|
407
|
+
};
|
|
408
|
+
|
|
350
409
|
// src/engine/occurrence.ts
|
|
351
410
|
var HALF_DAY_MS = 432e5;
|
|
352
411
|
var DAY_MS = 864e5;
|
|
@@ -1153,23 +1212,7 @@ var createTide = (config) => {
|
|
|
1153
1212
|
}
|
|
1154
1213
|
};
|
|
1155
1214
|
const load = async (input, options) => {
|
|
1156
|
-
const parsed =
|
|
1157
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1158
|
-
for (const candidate of input) {
|
|
1159
|
-
const result = ReflexSchema.safeParse(candidate);
|
|
1160
|
-
if (!result.success)
|
|
1161
|
-
throw new TideError("invalid_reflex", `reflex did not parse: ${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`, {
|
|
1162
|
-
issues: result.error.issues
|
|
1163
|
-
});
|
|
1164
|
-
if (seen.has(result.data.id)) throw new TideError("duplicate_reflex", `duplicate reflex id "${result.data.id}"`);
|
|
1165
|
-
seen.add(result.data.id);
|
|
1166
|
-
parsed.push(result.data);
|
|
1167
|
-
}
|
|
1168
|
-
const graph = buildGraph(parsed, mergedEffects(parsed));
|
|
1169
|
-
const refused = graph.refusals[0];
|
|
1170
|
-
if (refused !== void 0)
|
|
1171
|
-
throw new TideError(refused.code, `tide refused to load:
|
|
1172
|
-
${graph.errors.join("\n ")}`, { errors: graph.errors, refusals: graph.refusals });
|
|
1215
|
+
const { reflexes: parsed, graph, refused } = admitReflexes(input, mergedEffects, options?.isolate === true);
|
|
1173
1216
|
const previously = new Set(loaded.keys());
|
|
1174
1217
|
const next = /* @__PURE__ */ new Map();
|
|
1175
1218
|
for (const reflex of parsed) {
|
|
@@ -1182,7 +1225,7 @@ var createTide = (config) => {
|
|
|
1182
1225
|
}
|
|
1183
1226
|
loaded.clear();
|
|
1184
1227
|
for (const [id, entry] of next) loaded.set(id, entry);
|
|
1185
|
-
return { loaded: parsed.length, cycles: graph.cycles, blind: graph.blind, warnings: graph.warnings };
|
|
1228
|
+
return { loaded: parsed.length, cycles: graph.cycles, blind: graph.blind, warnings: graph.warnings, refused };
|
|
1186
1229
|
};
|
|
1187
1230
|
const mergedEffects = (parsed) => {
|
|
1188
1231
|
if (typeof config.effects !== "function") return config.effects ?? {};
|
|
@@ -1566,7 +1609,11 @@ var localDateTime = (instant, tz) => {
|
|
|
1566
1609
|
var anchorDraft = (draft, anchor) => {
|
|
1567
1610
|
if (!("timer" in draft.on)) return ReflexSchema.parse(draft);
|
|
1568
1611
|
const fires = Math.ceil((anchor.at + timerMs(draft.on.timer)) / 1e3) * 1e3;
|
|
1569
|
-
return ReflexSchema.parse({ ...draft, on: { clock:
|
|
1612
|
+
return ReflexSchema.parse({ ...draft, on: { clock: clockAt(fires, anchor.tz) } });
|
|
1613
|
+
};
|
|
1614
|
+
var clockAt = (fires, tz) => {
|
|
1615
|
+
const local = { at: localDateTime(fires, tz), tz };
|
|
1616
|
+
return occurrencesBetween(local, fires - 1e3, fires, 1)[0]?.at === fires ? local : { at: localDateTime(fires, "UTC"), tz: "UTC" };
|
|
1570
1617
|
};
|
|
1571
1618
|
|
|
1572
1619
|
exports.COMPARISON_OPS = COMPARISON_OPS;
|
|
@@ -1582,6 +1629,7 @@ exports.ManualTriggerSchema = ManualTriggerSchema;
|
|
|
1582
1629
|
exports.OpSchema = OpSchema;
|
|
1583
1630
|
exports.PRIMARY_KEY = PRIMARY_KEY;
|
|
1584
1631
|
exports.PolicySchema = PolicySchema;
|
|
1632
|
+
exports.REFLEX_TRIGGERS = REFLEX_TRIGGERS;
|
|
1585
1633
|
exports.ReflexDraftSchema = ReflexDraftSchema;
|
|
1586
1634
|
exports.ReflexSchema = ReflexSchema;
|
|
1587
1635
|
exports.RetrySchema = RetrySchema;
|
|
@@ -1589,6 +1637,7 @@ exports.RunTriggerSchema = RunTriggerSchema;
|
|
|
1589
1637
|
exports.SelectionSchema = SelectionSchema;
|
|
1590
1638
|
exports.SignalTriggerSchema = SignalTriggerSchema;
|
|
1591
1639
|
exports.TideError = TideError;
|
|
1640
|
+
exports.TimeZoneSchema = TimeZoneSchema;
|
|
1592
1641
|
exports.TimerSchema = TimerSchema;
|
|
1593
1642
|
exports.TimerTriggerSchema = TimerTriggerSchema;
|
|
1594
1643
|
exports.TriggerSchema = TriggerSchema;
|
|
@@ -1604,6 +1653,7 @@ exports.draftSchemaOf = draftSchemaOf;
|
|
|
1604
1653
|
exports.isTideError = isTideError;
|
|
1605
1654
|
exports.occurrenceKey = occurrenceKey;
|
|
1606
1655
|
exports.occurrencesBetween = occurrencesBetween;
|
|
1656
|
+
exports.reflexSchemaOf = reflexSchemaOf;
|
|
1607
1657
|
exports.timerMs = timerMs;
|
|
1608
1658
|
exports.versionOf = versionOf;
|
|
1609
1659
|
exports.zonedParts = zonedParts;
|