@niscorp/tide 0.1.6 → 0.1.7

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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { UNIQUE_BY, PRIMARY_KEY, COMPARISON_OPS } from './chunk-4CUQXWVY.js';
2
- export { COMPARISON_OPS, PRIMARY_KEY, UNIQUE_BY } from './chunk-4CUQXWVY.js';
3
- import { isRecurring, ReflexSchema, FactInputSchema, clockOf, factOf, WEEKDAYS, isManual } from './chunk-UEG3UZDG.js';
4
- export { ClockOnceSchema, ClockRecurringSchema, ClockTriggerSchema, ClockUnitSchema, DRAFT_TRIGGERS, EffectRefSchema, FactInputSchema, FactKindSchema, ManualTriggerSchema, OpSchema, PolicySchema, ReflexDraftSchema, ReflexSchema, RetrySchema, RunTriggerSchema, SelectionSchema, SignalTriggerSchema, TimerSchema, TimerTriggerSchema, TriggerSchema, WEEKDAYS, WriteTriggerSchema, draftSchemaOf } from './chunk-UEG3UZDG.js';
1
+ import { UNIQUE_BY, PRIMARY_KEY, COMPARISON_OPS } from './chunk-7KHOAJNL.js';
2
+ export { COMPARISON_OPS, PRIMARY_KEY, UNIQUE_BY } from './chunk-7KHOAJNL.js';
3
+ import { isRecurring, ReflexSchema, FactInputSchema, clockOf, factOf, WEEKDAYS, isManual } from './chunk-4TXWZFRE.js';
4
+ export { ClockOnceSchema, ClockRecurringSchema, ClockTriggerSchema, ClockUnitSchema, DRAFT_TRIGGERS, EffectRefSchema, FactInputSchema, FactKindSchema, ManualTriggerSchema, OpSchema, PolicySchema, ReflexDraftSchema, ReflexSchema, RetrySchema, RunTriggerSchema, SelectionSchema, SignalTriggerSchema, TimerSchema, TimerTriggerSchema, TriggerSchema, WEEKDAYS, WriteTriggerSchema, draftSchemaOf } from './chunk-4TXWZFRE.js';
5
5
 
6
6
  // src/errors.ts
7
7
  var TideError = class extends Error {
@@ -278,8 +278,6 @@ var openRun = async (deps, loaded, input) => {
278
278
  const base = {
279
279
  reflexId: reflex.id,
280
280
  version,
281
- // Written down so the ledger is scopeable by an ordinary host rule
282
- // rather than by parsing reflex ids. Opaque to tide.
283
281
  as: reflex.as,
284
282
  cause: input.cause,
285
283
  occurrence: input.occurrence,
@@ -302,8 +300,6 @@ var openRun = async (deps, loaded, input) => {
302
300
  ...base,
303
301
  state: "skipped",
304
302
  settledAt: input.now,
305
- // Nothing waits on it and nothing announces it: a run that never
306
- // started has no settlement to report.
307
303
  drained: true,
308
304
  note: `overlap: the previous run (${unsettled.id}) is still unsettled`
309
305
  });
@@ -383,8 +379,6 @@ var matchFacts = async (deps, now, limit) => {
383
379
  now,
384
380
  occurrence: fact.occurrence,
385
381
  factIds: [fact.id],
386
- // A write, a signal or another reflex's settlement happened once and
387
- // will not happen again; a human pressing the button twice will.
388
382
  repeat: fact.kind === "manual"
389
383
  });
390
384
  if (run !== void 0) report.runsCreated += 1;
@@ -520,8 +514,6 @@ var commitFanout = async (deps, run, tasks, selected, now) => deps.store.transac
520
514
  {
521
515
  selected,
522
516
  total: written,
523
- // A zero-task run settles immediately and still announces itself —
524
- // "nothing was due" is an answer a digest may legitimately want.
525
517
  state: settledNow ? "settled" : "fanned",
526
518
  settledAt: settledNow ? now : void 0
527
519
  }
@@ -532,12 +524,9 @@ var taskOf = (unit, run, loaded, now) => ({
532
524
  runId: run.id,
533
525
  reflexId: loaded.reflex.id,
534
526
  unit: unit.unit,
535
- // UNIQUE(runId, unit) — the idempotency grain, written BEFORE the effect
536
- // runs. There is no path to the effect that skips this row.
537
527
  cause: run.cause,
538
528
  env: unit.env,
539
- // Carried, not looked up later: a swept run must not reset the chain
540
- // ceiling for work that is still in flight.
529
+ // Carried on the task: a swept run must not reset the chain ceiling.
541
530
  depth: run.depth,
542
531
  state: "pending",
543
532
  attempt: 0,
@@ -624,22 +613,13 @@ var claimTasks = async (deps, now, limit, token) => {
624
613
  where: {
625
614
  state: { in: ["pending", "retrying", "claimed"] },
626
615
  notBefore: { lte: now },
627
- // Either it was never claimed, or the claim has lapsed. Expressed as
628
- // one predicate because a second query would be a second decision.
629
616
  claimedUntil: { lte: now }
630
617
  },
631
- // `unit` is the tiebreaker, and it is not cosmetic. Every task of one
632
- // fan-out shares a `notBefore` and a `createdAt`, so with two keys the
633
- // order was whatever the storage engine felt like — insertion order in a
634
- // Map, heap order in Postgres. Two stores answering differently is the
635
- // thing the contract exists to prevent, and a headless check asserting on
636
- // the order effects ran in deserves the same answer everywhere.
618
+ // `unit` breaks ties: one fan-out's tasks share `notBefore` and `createdAt`,
619
+ // and two stores must order them the same way.
637
620
  order: [{ by: "notBefore" }, { by: "createdAt" }, { by: "unit" }],
638
621
  limit,
639
622
  set: { state: "claimed", token, claimedUntil: now + deps.leaseMs, attempt: { inc: 1 } },
640
- // `order: 'serial'` — at most one live claim per reflex, counting what
641
- // another claim already holds. Inside the claim, because a caller that
642
- // looks first and claims second loses the race to its own second tick.
643
623
  onePer: serialReflexIds.length === 0 ? void 0 : { column: "reflexId", held: { state: "claimed", claimedUntil: { gt: now } }, only: serialReflexIds }
644
624
  });
645
625
  };
@@ -675,21 +655,8 @@ var executeTasks = async (deps, now, limit) => {
675
655
  depth: task.depth,
676
656
  now,
677
657
  actor: deps.actorFor(reflex.as),
678
- // VALIDATED, and narrowed to two kinds.
679
- //
680
- // `emit` used to accept anything: a handler could emit `kind: 'manual'`
681
- // and fire a DISARMED reflex, because manual facts are checked before
682
- // enablement on purpose — arming gates triggers, not people. A handler
683
- // is not a person. `run` is tide's own bookkeeping and is minted by the
684
- // settler, so it is refused for the same reason.
685
- //
686
- // A malformed emit THROWS, inside the handler, so the attempt fails
687
- // visibly. The alternative is a fact that stores, matches nothing and
688
- // vanishes — a chain that silently stops one hop in.
689
- //
690
- // Depth is stamped from the task below — the handler never sets it,
691
- // because a handler that could choose its own depth could escape the
692
- // chain ceiling.
658
+ // `write` and `signal` only: a `manual` fact fires a disabled reflex, and
659
+ // `run` facts are minted by the settler alone.
693
660
  emit: (fact) => {
694
661
  if (fact.kind !== "write" && fact.kind !== "signal")
695
662
  throw new TideError("invalid_fact", `${reflex.id}: an effect may emit \`write\` or \`signal\` facts, not \`${fact.kind}\``);
@@ -731,8 +698,7 @@ var executeTasks = async (deps, now, limit) => {
731
698
  notBefore: next.state === "retrying" ? next.notBefore : task.notBefore,
732
699
  output,
733
700
  error,
734
- // A throwing attempt discards its buffer: otherwise every retry of an
735
- // emit-then-throw handler would double-mint facts and fire the chain twice.
701
+ // A failed attempt's emits are dropped: its retry would mint them again.
736
702
  emits: outcome === "ok" ? emits : [],
737
703
  at: now
738
704
  });
@@ -819,8 +785,6 @@ var settleRuns = async (deps, now) => {
819
785
  at: now,
820
786
  depth: run.depth + 1,
821
787
  cause: `run:${run.id}`,
822
- // A settlement is news about THIS reflex's work — counts of rows it
823
- // selected. Another tenant's digest has no business waking on it.
824
788
  as: run.as
825
789
  }, deps.maxChainFacts);
826
790
  announceFact(deps.emit, admission);
@@ -1064,8 +1028,6 @@ var createTide = (config) => {
1064
1028
  if (!loaded.has(reflexId)) throw new TideError("unknown_reflex", `no reflex "${reflexId}" is loaded`);
1065
1029
  return ingest(
1066
1030
  { kind: "manual", target: reflexId, payload: options.input, by: options.by ?? "operator", at: options.now },
1067
- // Carried so the ledger reads consistently. Matching a manual fact does
1068
- // not depend on it — `target` names one reflex, which is narrower.
1069
1031
  { as: loaded.get(reflexId)?.reflex.as }
1070
1032
  );
1071
1033
  };
@@ -1125,19 +1087,14 @@ var createTide = (config) => {
1125
1087
  },
1126
1088
  fact: (id) => one("fact", id),
1127
1089
  causeChain,
1128
- // Clears the park AND records that a human overrode the ceiling —
1129
- // clearing the park alone is a ping-pong, because the depth that
1130
- // parked it has not changed and never will.
1090
+ // `released` as well: with `parked` alone cleared, the depth check would
1091
+ // park the fact again on the next advance.
1131
1092
  releaseParked: async (factId) => {
1132
1093
  const fact = await one("fact", factId);
1133
1094
  if (fact?.parked === void 0) return false;
1134
1095
  return config.store.cas("fact", factId, {}, { parked: void 0, released: true });
1135
1096
  }
1136
1097
  },
1137
- // RETENTION, and the one place the ledger shrinks. Runs cascade to their
1138
- // tasks in the store: a run and its members are one fact about the world,
1139
- // and keeping half destroys the UNIQUE(runId, unit) row that IS the "this
1140
- // unit already ran" record.
1141
1098
  sweep: async (now, retention) => {
1142
1099
  let removed = 0;
1143
1100
  if (retention.facts !== void 0)