eutaxy 0.13.0 → 0.14.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.
@@ -1061,6 +1061,9 @@ function patchGlobals(seed, options = {}) {
1061
1061
  libraryIo(perform) {
1062
1062
  return libraryScope ? libraryScope.run(true, perform) : perform();
1063
1063
  },
1064
+ handlerCode(work) {
1065
+ return libraryScope ? libraryScope.run(false, work) : work();
1066
+ },
1064
1067
  restore() {
1065
1068
  if (restored) return;
1066
1069
  restored = true;
@@ -1392,6 +1395,16 @@ var Frames = class {
1392
1395
  const live = store?.active && store.invocation === invocation ? store : void 0;
1393
1396
  return effectScope ? live ?? this.hosting.get(invocation)?.at(-1) : this.stack.at(-1);
1394
1397
  }
1398
+ /**
1399
+ * The effect whose perform the current code runs inside, whatever
1400
+ * invocation it belongs to: the live frame of the async context, else the
1401
+ * innermost being performed. Where another invocation's code is hosted.
1402
+ */
1403
+ current() {
1404
+ if (!effectScope) return this.stack.at(-1);
1405
+ const store = effectScope.getStore();
1406
+ return store?.active ? store : void 0;
1407
+ }
1395
1408
  /** An effect starts being performed: it is in flight. */
1396
1409
  enter(frame) {
1397
1410
  this.inFlight++;
@@ -1694,6 +1707,12 @@ var Run = class {
1694
1707
  invocations = /* @__PURE__ */ new Map();
1695
1708
  reasons = [];
1696
1709
  frames = new Frames();
1710
+ /**
1711
+ * Invocations whose code runs inside an effect of another's (a called
1712
+ * Worker's code inside the call's effect), with the frame hosting each; see
1713
+ * `hosted`.
1714
+ */
1715
+ hosts = /* @__PURE__ */ new Map();
1697
1716
  patched = null;
1698
1717
  restoreFetch = null;
1699
1718
  /** The invocation whose code is running (one at a time); outbound fetches are attributed to it. */
@@ -1761,16 +1780,14 @@ var Run = class {
1761
1780
  if (this.concurrent) this.progressed();
1762
1781
  });
1763
1782
  } });
1764
- if (this.concurrent) {
1765
- const clock = this.patched.clock;
1766
- clock.ownerNow = () => this.running();
1767
- clock.bind = (callback) => {
1768
- const cell = deliveryScope?.getStore();
1769
- if (!cell || !deliveryScope) return callback;
1770
- const scope = deliveryScope;
1771
- return (...args) => scope.run(cell, () => callback(...args));
1772
- };
1773
- }
1783
+ const clock = this.patched.clock;
1784
+ clock.ownerNow = () => this.running();
1785
+ clock.bind = (callback) => {
1786
+ if (!deliveryScope) return callback;
1787
+ const scope = deliveryScope;
1788
+ const cell = scope.getStore() ?? { id: this.running() };
1789
+ return (...args) => scope.run(cell, () => callback(...args));
1790
+ };
1774
1791
  }
1775
1792
  if (!this.restoreFetch) this.restoreFetch = installScriptedFetch(externals, {
1776
1793
  hooks: this,
@@ -1818,7 +1835,6 @@ var Run = class {
1818
1835
  return queued;
1819
1836
  }
1820
1837
  running() {
1821
- if (!this.concurrent) return this.current;
1822
1838
  return deliveryScope?.getStore()?.id ?? this.current;
1823
1839
  }
1824
1840
  runAs(id) {
@@ -1892,6 +1908,19 @@ var Run = class {
1892
1908
  const enclosing = this.frames.enclosing(point.invocation);
1893
1909
  const nested = enclosing !== void 0;
1894
1910
  if (this.concurrent && !nested) {
1911
+ const host = this.hosts.get(point.invocation);
1912
+ if (host?.active) {
1913
+ this.frames.adjust(host, () => {
1914
+ host.children++;
1915
+ host.waitingChildren++;
1916
+ });
1917
+ await new Promise((go) => this.turns.park(inv, point.target, go, host));
1918
+ try {
1919
+ return await this.performEffect(point, perform, summarize, options, inv, void 0, host);
1920
+ } finally {
1921
+ this.frames.adjust(host, () => host.children--);
1922
+ }
1923
+ }
1895
1924
  await new Promise((go) => this.turns.park(inv, point.target, go));
1896
1925
  if (inv.isCrashed()) return hang();
1897
1926
  } else if (this.concurrent && enclosing?.hosts && enclosing.mayPark()) {
@@ -2066,12 +2095,14 @@ var Run = class {
2066
2095
  const inv = this.invocation(invocation);
2067
2096
  if (this.concurrent) return Promise.race([promise, inv.crashed.then(() => hang())]);
2068
2097
  const patched = this.patched;
2069
- if (patched) patched.clock.owner = invocation;
2070
- this.current = invocation;
2098
+ if (!this.hosts.has(invocation)) {
2099
+ if (patched) patched.clock.owner = invocation;
2100
+ this.current = invocation;
2101
+ }
2071
2102
  if (!patched) return Promise.race([promise, inv.crashed.then(() => hang())]);
2072
2103
  return settleOn(promise, {
2073
2104
  clock: patched.clock,
2074
- inFlight: () => this.frames.inFlight,
2105
+ inFlight: () => this.frames.inFlight + this.hostedBy(invocation),
2075
2106
  crashed: () => inv.isCrashed(),
2076
2107
  stuck: () => {
2077
2108
  this.unsupported(waitingUnseen(invocation));
@@ -2079,6 +2110,47 @@ var Run = class {
2079
2110
  }
2080
2111
  });
2081
2112
  }
2113
+ /**
2114
+ * Run `work` as the code of a new invocation `id` inside the effect being
2115
+ * performed now (see `hostInvocation`): the invocation is started in a
2116
+ * delivery context of its own, so async context attributes its effects to
2117
+ * it while the host's own code stays the host's, the hosting effect waits
2118
+ * on it without counting as I/O of its own (the hosted code's effects and
2119
+ * tracked work count), and the hosting invocation is the running one again
2120
+ * once it ends. A fault that crashes `id` ends `id` alone: nothing here
2121
+ * tells the host's frame, so what the host sees is `work`'s to decide.
2122
+ */
2123
+ async hosted(id, work) {
2124
+ const frame = this.frames.current();
2125
+ if (!frame) throw new Error(`invocation ${id} can only be hosted inside an effect being performed`);
2126
+ if (!deliveryScope) throw new Error("hosting an invocation needs AsyncLocalStorage (node:async_hooks)");
2127
+ const run = async () => {
2128
+ const inv = this.track(id);
2129
+ if (this.concurrent) this.turns.started(id);
2130
+ this.hosts.set(id, frame);
2131
+ this.frames.adjust(frame, () => {
2132
+ frame.children++;
2133
+ frame.waitingChildren++;
2134
+ });
2135
+ try {
2136
+ return await work(inv);
2137
+ } finally {
2138
+ this.frames.adjust(frame, () => {
2139
+ frame.children--;
2140
+ frame.waitingChildren--;
2141
+ });
2142
+ this.hosts.delete(id);
2143
+ }
2144
+ };
2145
+ const asHandler = () => this.patched ? this.patched.handlerCode(run) : run();
2146
+ return deliveryScope.run({ id }, asHandler);
2147
+ }
2148
+ /** How many invocations are hosted inside effects of `invocation` now. */
2149
+ hostedBy(invocation) {
2150
+ let n = 0;
2151
+ for (const frame of this.hosts.values()) if (frame.invocation === invocation) n++;
2152
+ return n;
2153
+ }
2082
2154
  /** Concurrent runs: start a delivery in its own async context. */
2083
2155
  startDelivery(deliver) {
2084
2156
  if (!deliveryScope) throw new Error("concurrent invocations need AsyncLocalStorage");
@@ -2208,6 +2280,21 @@ async function runInvocation(hooks, inv, work) {
2208
2280
  });
2209
2281
  return Promise.race([hooks.settle(outcome, inv.id), inv.crashed.then(() => ({ status: "crashed" }))]);
2210
2282
  }
2283
+ /**
2284
+ * Run user code as an invocation of its own inside the effect being performed
2285
+ * (a called Worker's code inside the call's effect). The engine attributes
2286
+ * the effects `work` makes to `id` and schedules them as turns of their own,
2287
+ * has the hosting effect wait on the work without counting as I/O, and makes
2288
+ * the hosting invocation the running one again afterwards. `work` runs the
2289
+ * user code through `runInvocation` (and whatever follows it, such as
2290
+ * background work) so that it ends as an outcome; a fault that crashes `id`
2291
+ * ends `id` alone, and what the hosting code then throws or returns is the
2292
+ * platform's to decide.
2293
+ */
2294
+ function hostInvocation(hooks, id, work) {
2295
+ if (!(hooks instanceof Run)) throw new Error("hostInvocation needs the engine's run");
2296
+ return hooks.hosted(id, work);
2297
+ }
2211
2298
  // packages/engine/src/exhaustive.ts
2212
2299
  /**
2213
2300
  * Exhaustive exploration: every schedule within the bounds, each once. A
@@ -2855,6 +2942,6 @@ function observeCore(namespace, functions) {
2855
2942
  }
2856
2943
  };
2857
2944
  }
2858
- export { writeFaults as A, toJson as C, checkInvariants as D, checkInvariant as E, getPath as F, canonicalJson as I, nextInt as M, nextU32 as N, decisionFaults as O, seedPrng as P, patchProperty as S, validateScenario as T, withoutNetwork as _, explore as a, settleOn as b, runInvocation as c, findResponse as d, installScriptedFetch as f, toResponse as g, replyOf as h, proxiedSide as i, nextFloat as j, readFaults as k, CrashSignal as l, parseBody as m, DifferentialStuck as n, replay as o, isRedirect as p, compareSides as r, restrictSpec as s, observeCore as t, InjectedEffectError as u, patchGlobals as v, defaultBounds as w, virtualEpoch as x, realTick as y };
2945
+ export { readFaults as A, patchProperty as C, checkInvariant as D, validateScenario as E, seedPrng as F, getPath as I, canonicalJson as L, nextFloat as M, nextInt as N, checkInvariants as O, nextU32 as P, virtualEpoch as S, defaultBounds as T, toResponse as _, explore as a, realTick as b, hostInvocation as c, InjectedEffectError as d, findResponse as f, replyOf as g, parseBody as h, proxiedSide as i, writeFaults as j, decisionFaults as k, runInvocation as l, isRedirect as m, DifferentialStuck as n, replay as o, installScriptedFetch as p, compareSides as r, restrictSpec as s, observeCore as t, CrashSignal as u, withoutNetwork as v, toJson as w, settleOn as x, patchGlobals as y };
2859
2946
 
2860
- //# sourceMappingURL=src-DGh7w877.mjs.map
2947
+ //# sourceMappingURL=src-DPCF2XGv.mjs.map