@niscorp/tide 0.1.10 → 0.1.12
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 +25 -0
- package/DESIGN.md +4 -1
- package/DOCS.md +39 -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-7KHOAJNL.js → chunk-MGGWP6C7.js} +2 -2
- package/dist/chunk-MGGWP6C7.js.map +1 -0
- package/dist/{chunk-ZEEJLISU.js → chunk-SLLCUI65.js} +27 -5
- package/dist/chunk-SLLCUI65.js.map +1 -0
- package/dist/index.cjs +78 -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 +55 -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 +1 -1
- 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 isKnownZone = (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(isKnownZone, { 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,46 @@ 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 = ({
|
|
376
|
+
input,
|
|
377
|
+
effectsOf,
|
|
378
|
+
isolate
|
|
379
|
+
}) => {
|
|
380
|
+
const parsed = [];
|
|
381
|
+
const refused = [];
|
|
382
|
+
const seen = /* @__PURE__ */ new Set();
|
|
383
|
+
for (const candidate of input) {
|
|
384
|
+
const result = ReflexSchema.safeParse(candidate);
|
|
385
|
+
if (!result.success) {
|
|
386
|
+
const message = `reflex did not parse: ${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`;
|
|
387
|
+
if (!isolate) throw new TideError("invalid_reflex", message, { issues: result.error.issues });
|
|
388
|
+
refused.push({ reflexId: idOf(candidate), code: "invalid_reflex", message });
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (seen.has(result.data.id)) {
|
|
392
|
+
const message = `duplicate reflex id "${result.data.id}"`;
|
|
393
|
+
if (!isolate) throw new TideError("duplicate_reflex", message);
|
|
394
|
+
refused.push({ reflexId: result.data.id, code: "duplicate_reflex", message });
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
seen.add(result.data.id);
|
|
398
|
+
parsed.push(result.data);
|
|
399
|
+
}
|
|
400
|
+
let kept = parsed;
|
|
401
|
+
let graph = buildGraph(kept, effectsOf(kept));
|
|
402
|
+
for (let [first] = graph.refusals; first !== void 0; [first] = graph.refusals) {
|
|
403
|
+
if (!isolate) throw new TideError(first.code, `tide refused to load:
|
|
404
|
+
${graph.errors.join("\n ")}`, { errors: graph.errors, refusals: graph.refusals });
|
|
405
|
+
const out = new Set(graph.refusals.flatMap((refusal) => refusal.reflexIds));
|
|
406
|
+
for (const refusal of graph.refusals) for (const reflexId of refusal.reflexIds) refused.push({ reflexId, code: refusal.code, message: refusal.message });
|
|
407
|
+
kept = kept.filter((reflex) => !out.has(reflex.id));
|
|
408
|
+
graph = buildGraph(kept, effectsOf(kept));
|
|
409
|
+
}
|
|
410
|
+
return { reflexes: kept, graph, refused };
|
|
411
|
+
};
|
|
412
|
+
|
|
350
413
|
// src/engine/occurrence.ts
|
|
351
414
|
var HALF_DAY_MS = 432e5;
|
|
352
415
|
var DAY_MS = 864e5;
|
|
@@ -1153,23 +1216,7 @@ var createTide = (config) => {
|
|
|
1153
1216
|
}
|
|
1154
1217
|
};
|
|
1155
1218
|
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 });
|
|
1219
|
+
const { reflexes: parsed, graph, refused } = admitReflexes({ input, effectsOf: mergedEffects, isolate: options?.isolate === true });
|
|
1173
1220
|
const previously = new Set(loaded.keys());
|
|
1174
1221
|
const next = /* @__PURE__ */ new Map();
|
|
1175
1222
|
for (const reflex of parsed) {
|
|
@@ -1182,7 +1229,7 @@ var createTide = (config) => {
|
|
|
1182
1229
|
}
|
|
1183
1230
|
loaded.clear();
|
|
1184
1231
|
for (const [id, entry] of next) loaded.set(id, entry);
|
|
1185
|
-
return { loaded: parsed.length, cycles: graph.cycles, blind: graph.blind, warnings: graph.warnings };
|
|
1232
|
+
return { loaded: parsed.length, cycles: graph.cycles, blind: graph.blind, warnings: graph.warnings, refused };
|
|
1186
1233
|
};
|
|
1187
1234
|
const mergedEffects = (parsed) => {
|
|
1188
1235
|
if (typeof config.effects !== "function") return config.effects ?? {};
|
|
@@ -1566,7 +1613,11 @@ var localDateTime = (instant, tz) => {
|
|
|
1566
1613
|
var anchorDraft = (draft, anchor) => {
|
|
1567
1614
|
if (!("timer" in draft.on)) return ReflexSchema.parse(draft);
|
|
1568
1615
|
const fires = Math.ceil((anchor.at + timerMs(draft.on.timer)) / 1e3) * 1e3;
|
|
1569
|
-
return ReflexSchema.parse({ ...draft, on: { clock:
|
|
1616
|
+
return ReflexSchema.parse({ ...draft, on: { clock: clockAt(fires, anchor.tz) } });
|
|
1617
|
+
};
|
|
1618
|
+
var clockAt = (fires, tz) => {
|
|
1619
|
+
const local = { at: localDateTime(fires, tz), tz };
|
|
1620
|
+
return occurrencesBetween(local, fires - 1e3, fires, 1)[0]?.at === fires ? local : { at: localDateTime(fires, "UTC"), tz: "UTC" };
|
|
1570
1621
|
};
|
|
1571
1622
|
|
|
1572
1623
|
exports.COMPARISON_OPS = COMPARISON_OPS;
|
|
@@ -1582,6 +1633,7 @@ exports.ManualTriggerSchema = ManualTriggerSchema;
|
|
|
1582
1633
|
exports.OpSchema = OpSchema;
|
|
1583
1634
|
exports.PRIMARY_KEY = PRIMARY_KEY;
|
|
1584
1635
|
exports.PolicySchema = PolicySchema;
|
|
1636
|
+
exports.REFLEX_TRIGGERS = REFLEX_TRIGGERS;
|
|
1585
1637
|
exports.ReflexDraftSchema = ReflexDraftSchema;
|
|
1586
1638
|
exports.ReflexSchema = ReflexSchema;
|
|
1587
1639
|
exports.RetrySchema = RetrySchema;
|
|
@@ -1589,6 +1641,7 @@ exports.RunTriggerSchema = RunTriggerSchema;
|
|
|
1589
1641
|
exports.SelectionSchema = SelectionSchema;
|
|
1590
1642
|
exports.SignalTriggerSchema = SignalTriggerSchema;
|
|
1591
1643
|
exports.TideError = TideError;
|
|
1644
|
+
exports.TimeZoneSchema = TimeZoneSchema;
|
|
1592
1645
|
exports.TimerSchema = TimerSchema;
|
|
1593
1646
|
exports.TimerTriggerSchema = TimerTriggerSchema;
|
|
1594
1647
|
exports.TriggerSchema = TriggerSchema;
|
|
@@ -1604,6 +1657,7 @@ exports.draftSchemaOf = draftSchemaOf;
|
|
|
1604
1657
|
exports.isTideError = isTideError;
|
|
1605
1658
|
exports.occurrenceKey = occurrenceKey;
|
|
1606
1659
|
exports.occurrencesBetween = occurrencesBetween;
|
|
1660
|
+
exports.reflexSchemaOf = reflexSchemaOf;
|
|
1607
1661
|
exports.timerMs = timerMs;
|
|
1608
1662
|
exports.versionOf = versionOf;
|
|
1609
1663
|
exports.zonedParts = zonedParts;
|