@systemfsoftware/effect-sim-kernel 0.1.1 → 1.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @systemfsoftware/effect-sim-kernel
2
2
 
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - Require `effect` `^4` (stable 4.0.0). Release candidates are no longer supported: imports follow 4.0.0's module layout (`effect/http`, `effect/rpc`, `effect/http-api`, `effect/cluster`, `effect/process`, `effect/socket`, `effect/persistence`, `effect/reactivity`, `effect/encoding`, `effect/Arbitrary`), which does not exist in any 4.0.0 RC.
8
+
9
+ ## 0.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - The `nightly` profile is removed. `ProfileName` is now `'local' | 'per-change'`, and `CONFORMANCE_PROFILE=nightly` falls back to `per-change` (250 seeds) instead of deriving a seed count from a 1% miss rate. Callers passing `'nightly'` to `Kernel.seedsFor` no longer type-check.
14
+
15
+ - Kernel runs are serialized. A run started while another is live now waits behind it, in arrival order, instead of failing: two runs never interleave, so a scenario never sees another run's decisions, and a suite whose cases drive the kernel can run its tests concurrently.
16
+
17
+ `@systemfsoftware/vitest` adds `captureRunBinding`, the running test's run binding. `bind(effect)` re-provides it to an effect a library runs on a runtime of its own — its own scheduler, a worker, a simulation kernel — so the checks inside it count as that test's assertions, report softly, and see the same `owned` regions.
18
+
19
+ `@systemfsoftware/effect-spec-runtime` runs a case that declares no live reason through that runner, and a case's annotations now reach the test's report.
20
+
21
+ ### Patch Changes
22
+
23
+ - Kernel.search enumerates each run's branches in linear time. It used to re-count a schedule's spent preemptions from the start of the run for every branch position, which cost about a fifth of a bounded search's time; the schedules explored are unchanged.
24
+
3
25
  ## 0.1.1
4
26
 
5
27
  ### Minor Changes
package/README.md CHANGED
@@ -93,9 +93,9 @@ await Kernel.run(program.pipe(Effect.provide(Kernel.TestClock.layer)))
93
93
  yield* TestClock.adjust('91 seconds')
94
94
  ```
95
95
 
96
- ## Pinned Effect version
96
+ ## Verified Effect version
97
97
 
98
- The hooks read Effect runtime internals (the `Scheduler` service, `FiberImpl` methods, `Ref`/`Deferred` fields), which is allowed only inside this package. They are pinned to `effect` 4.0.0-rc.117 and throw loudly if a field or method moves. A version change must re-run the kernel's checks.
98
+ The hooks read Effect runtime internals (the `Scheduler` service, `FiberImpl` methods, `Ref`/`Deferred` fields), which is allowed only inside this package. They are verified against `effect` 4.0.0 and throw loudly if a field or method moves in a later 4.x release. A version change must re-run the kernel's checks.
99
99
 
100
100
  ## Development
101
101
 
@@ -106,7 +106,7 @@ pnpm --filter @systemfsoftware/effect-sim-kernel typecheck
106
106
  pnpm --filter @systemfsoftware/effect-sim-kernel build
107
107
  ```
108
108
 
109
- The kernel's behaviour suite lives in the private `packages/effect-sim-kernel-tests` package, because the Gherkin runner it is written in runs on this kernel:
109
+ The kernel's behaviour suite lives in the private `packages/sim/effect-sim-kernel-tests` package, because the Gherkin runner it is written in runs on this kernel:
110
110
 
111
111
  ```bash
112
112
  pnpm --filter @systemfsoftware/effect-sim-kernel-tests test
package/dist/index.d.ts CHANGED
@@ -15,27 +15,6 @@ interface Bound {
15
15
  }
16
16
  declare const pruned: Pruning;
17
17
  declare const unpruned: (disabledBy: ReadonlyArray<UnobservedPrimitive>) => Pruning;
18
- //#endregion
19
- //#region src/internal/deadlock.d.ts
20
- /** The process resources that can call back into the program from outside. */
21
- /** @internal */
22
- type WaitKind = 'RealTimer' | 'File' | 'Socket' | 'Other';
23
- /** @internal */
24
- interface SuspendedFiber {
25
- readonly id: number;
26
- readonly frames: ReadonlyArray<string>;
27
- }
28
- /** @internal */
29
- type AnyFiber<A = unknown, E = unknown> = Fiber.Fiber<A, E>;
30
- //#endregion
31
- //#region src/internal/escapeRecorder.d.ts
32
- /** @internal */
33
- type TimerName = 'setTimeout' | 'setInterval' | 'setImmediate';
34
- /** @internal */
35
- interface Escape {
36
- readonly timer: TimerName;
37
- readonly site: string;
38
- }
39
18
  declare namespace TestClock_d_exports {
40
19
  export { TestClock, layer, make };
41
20
  }
@@ -64,6 +43,27 @@ declare const make: Effect.Effect<TestClock>;
64
43
  /** The layer a harness provides in place of `TestClock.layer()`. */
65
44
  declare const layer: Layer.Layer<Clock.Clock>;
66
45
  //#endregion
46
+ //#region src/internal/deadlock.d.ts
47
+ /** The process resources that can call back into the program from outside. */
48
+ /** @internal */
49
+ type WaitKind = 'RealTimer' | 'File' | 'Socket' | 'Other';
50
+ /** @internal */
51
+ interface SuspendedFiber {
52
+ readonly id: number;
53
+ readonly frames: ReadonlyArray<string>;
54
+ }
55
+ /** @internal */
56
+ type AnyFiber<A = unknown, E = unknown> = Fiber.Fiber<A, E>;
57
+ //#endregion
58
+ //#region src/internal/escapeRecorder.d.ts
59
+ /** @internal */
60
+ type TimerName = 'setTimeout' | 'setInterval' | 'setImmediate';
61
+ /** @internal */
62
+ interface Escape {
63
+ readonly timer: TimerName;
64
+ readonly site: string;
65
+ }
66
+ //#endregion
67
67
  //#region src/internal/kernel.d.ts
68
68
  /** An index into the kernel's pending task list: the schedule's unit of choice. */
69
69
  /** @internal */
@@ -108,6 +108,19 @@ interface StepRecord {
108
108
  /** Which fiber an interruption names (R5). */
109
109
  /** @internal */
110
110
  type FiberTarget = 'root' | 'lastRan' | number;
111
+ /**
112
+ * Whether the live run is inside a synchronous step. A library that samples a
113
+ * run from outside can tell work the run caused from work an outside fiber did
114
+ * between steps. `false` when no run is live.
115
+ */
116
+ /** @internal */
117
+ declare const isStepping: () => boolean;
118
+ /**
119
+ * From here on, the schedule may deviate from Effect's order (R15). Under
120
+ * `explore: 'body'`, every decision before it stays on Effect's order.
121
+ */
122
+ /** @internal */
123
+ declare const beginExploration: Effect.Effect<void>;
111
124
  //#endregion
112
125
  //#region src/internal/stepLoop.d.ts
113
126
  /**
@@ -223,23 +236,18 @@ interface RunOptions {
223
236
  readonly onQuiescent?: () => boolean;
224
237
  }
225
238
  /**
226
- * Marks the point in a program where exploration may begin (R15). Under
227
- * `explore: 'body'`, every decision before it stays on Effect's order.
239
+ * Runs one Effect program under one kernel (R2: every clock the program can
240
+ * reach is the kernel's virtual root clock; R37: at quiescence the kernel
241
+ * settles its test clocks, then advances the root clock). Driven by the
242
+ * decision path or chooser in `options`, it returns the exit, the decisions
243
+ * taken, and a per-step record of which fiber ran. A run started while one is
244
+ * active queues behind it, so two runs never share the global hooks.
228
245
  */
229
246
  /** @internal */
230
- declare const beginExploration: Effect.Effect<void>;
231
- //#endregion
232
- //#region src/Kernel/Run.d.ts
233
247
  declare const run: {
234
248
  <A, E>(program: Effect.Effect<A, E>, options?: RunOptions): Promise<RunResult<A, E>>;
235
249
  (options?: RunOptions): <A, E>(program: Effect.Effect<A, E>) => Promise<RunResult<A, E>>;
236
250
  };
237
- /**
238
- * Whether the live run is inside a synchronous step. A library that samples a
239
- * run from outside can tell work the run caused from work an outside fiber did
240
- * between steps. `false` when no run is live.
241
- */
242
- declare const isStepping: () => boolean;
243
251
  //#endregion
244
252
  //#region src/Kernel/Pct.d.ts
245
253
  interface PickOptions {
package/dist/index.mjs CHANGED
@@ -29,42 +29,6 @@ const boundImpl = (requested, profile) => profile === "local" ? localBoundOf(req
29
29
  const preemptionsFor = dual(2, boundImpl);
30
30
  const currentPreemptionsFor = (requested) => Effect.map(currentProfile, (profile) => preemptionsFor(requested, profile));
31
31
  //#endregion
32
- //#region src/internal/runMark.ts
33
- /**
34
- * The one-live-run mark. A module-private slot on the patched fiber prototype
35
- * names the kernel whose run is live: acquisition claims it, release clears it,
36
- * and the concurrency guard reads it. The symbol never leaves this module, so
37
- * the slot is unnameable from consumer code. No module-level mutable state
38
- * lives here — the slot travels on the fiber prototype itself.
39
- */
40
- const ACTIVE_RUN = Symbol("~effect-sim-kernel/activeRun");
41
- const isHostObject$4 = (candidate) => typeof candidate === "object";
42
- const isKernel = (candidate) => "pending" in candidate && "release" in candidate;
43
- const fieldOf$2 = (target, key) => Reflect.get(target, key);
44
- const protoOf$2 = (value) => {
45
- const proto = Object.getPrototypeOf(value);
46
- return isHostObject$4(proto) ? proto : Object.prototype;
47
- };
48
- /** @internal */
49
- const FIBER_PROTOTYPE = protoOf$2(Effect.runFork(Effect.void));
50
- const heldRun = () => fieldOf$2(FIBER_PROTOTYPE, ACTIVE_RUN);
51
- const isLiveKernel = (candidate) => isHostObject$4(candidate) && isKernel(candidate);
52
- /** @internal */
53
- const currentKernel = () => {
54
- const candidate = heldRun();
55
- return isLiveKernel(candidate) ? candidate : void 0;
56
- };
57
- /** @internal */
58
- const isRunLive = () => isHostObject$4(heldRun());
59
- /** @internal */
60
- const claimRun = (kernel) => {
61
- Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, kernel);
62
- };
63
- /** @internal */
64
- const releaseRun = () => {
65
- Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, void 0);
66
- };
67
- //#endregion
68
32
  //#region src/internal/clocks.ts
69
33
  /**
70
34
  * The kernel's clocks (R2, R37). The root clock is virtual time the run owns:
@@ -207,184 +171,6 @@ const makeRunClocks = () => ({
207
171
  */
208
172
  /** @internal */
209
173
  const stepClocks = (clocks) => clocks.testClocks.some((testClock) => testClock.stepTowardRequest()) || clocks.clock.advance();
210
- /**
211
- * Builds the run's test clock and registers it for quiescent settling. Only
212
- * available inside a kernel run, where the run's clocks live.
213
- */
214
- /** @internal */
215
- const kernelTestClock = Effect.sync(() => {
216
- const kernel = currentKernel();
217
- if (kernel === void 0) throw new Error("effect-sim-kernel: the kernel test clock is only available inside a kernel run");
218
- const testClock = makeTestClock(kernel.clocks.clock);
219
- kernel.clocks.testClocks.push(testClock);
220
- return testClock;
221
- });
222
- //#endregion
223
- //#region src/internal/deadlock.ts
224
- const NO_FRAMES = [];
225
- const NO_RESOURCES = [];
226
- const isHostObject$3 = (candidate) => typeof candidate === "object";
227
- const isFrameList = (candidate) => Array.isArray(candidate);
228
- const isHostProcess = (candidate) => isHostObject$3(candidate);
229
- const hostProcess = () => Reflect.get(globalThis, "process");
230
- const resourceNames = () => {
231
- const host = hostProcess();
232
- return isHostProcess(host) ? host.getActiveResourcesInfo() : NO_RESOURCES;
233
- };
234
- const bump = (counts, resource) => {
235
- counts.set(resource, (counts.get(resource) ?? 0) + 1);
236
- };
237
- /** A snapshot of the process resources alive right now, by resource kind. */
238
- /** @internal */
239
- const resourceCounts = () => {
240
- const counts = /* @__PURE__ */ new Map();
241
- for (const resource of resourceNames()) bump(counts, resource);
242
- return counts;
243
- };
244
- const extraOf = (before, entry) => entry[1] - (before.get(entry[0]) ?? 0);
245
- const appendExtra = (appeared, before, entry) => {
246
- const extra = extraOf(before, entry);
247
- if (extra > 0) appeared.push(`${entry[0]} x${extra}`);
248
- };
249
- /** Resource kinds that appeared since `before`, as `"<kind> x<count>"` entries. */
250
- /** @internal */
251
- const newResources = (before) => {
252
- const appeared = [];
253
- for (const entry of resourceCounts()) appendExtra(appeared, before, entry);
254
- return appeared;
255
- };
256
- const FAMILY_PATTERNS = [
257
- [/Socket|TCP|UDP|Pipe|GetAddrInfo/u, "Socket"],
258
- [/FS|File/u, "File"],
259
- [/Timeout|Immediate/u, "RealTimer"]
260
- ];
261
- const matchedFamily = (resource) => {
262
- return FAMILY_PATTERNS.find(([pattern]) => pattern.test(resource))?.[1];
263
- };
264
- const familyOf = (resource) => matchedFamily(resource) ?? "Other";
265
- const isNamed = (family) => family !== "Other";
266
- /**
267
- * What a set of newly appeared resources can wake. Files and sockets name
268
- * themselves first; a leftover kind is still named, through `resources`.
269
- */
270
- /** @internal */
271
- const waitKindOf = (resources) => {
272
- return resources.map(familyOf).find(isNamed) ?? "Other";
273
- };
274
- const stringOn = (described, key) => {
275
- const value = Reflect.get(described, key);
276
- return typeof value === "string" ? value : void 0;
277
- };
278
- const stringFieldOf = (described, key) => {
279
- if (!isHostObject$3(described)) return void 0;
280
- return stringOn(described, key);
281
- };
282
- const argsNameOn = (described) => {
283
- const args = Reflect.get(described, "args");
284
- return typeof args === "function" ? args.name : void 0;
285
- };
286
- const nameOf = (described) => {
287
- if (!isHostObject$3(described)) return void 0;
288
- return argsNameOn(described);
289
- };
290
- const applyToJson = (frame) => {
291
- const toJson = Reflect.get(frame, "toJSON");
292
- return typeof toJson === "function" ? Reflect.apply(toJson, frame, []) : void 0;
293
- };
294
- const describeFrame = (frame) => {
295
- if (!isHostObject$3(frame)) return void 0;
296
- return applyToJson(frame);
297
- };
298
- const namedOp = (op, described) => {
299
- const name = nameOf(described);
300
- return name === void 0 ? op : `${op}(${name})`;
301
- };
302
- const renderFrame = (frame) => {
303
- const described = describeFrame(frame);
304
- const op = stringFieldOf(described, "op") ?? "?";
305
- return namedOp(op, described);
306
- };
307
- const renderedFrames = (stack) => stack.slice(1, 7).map(renderFrame);
308
- const framesOf = (fiber) => {
309
- const stack = Reflect.get(fiber, "_stack");
310
- return isFrameList(stack) ? renderedFrames(stack) : NO_FRAMES;
311
- };
312
- const isUnfinished = (fiber) => fiber.pollUnsafe() === void 0;
313
- const collectSuspended = (suspended, fiber) => {
314
- if (isUnfinished(fiber)) suspended.push({
315
- id: fiber.id,
316
- frames: framesOf(fiber)
317
- });
318
- };
319
- /**
320
- * Every unfinished fiber the kernel ran, innermost frame first. Fibers that
321
- * already exited are not suspended, so they are left out.
322
- */
323
- /** @internal */
324
- const describeSuspended = (fibers) => {
325
- const suspended = [];
326
- for (const fiber of fibers) collectSuspended(suspended, fiber);
327
- return suspended;
328
- };
329
- //#endregion
330
- //#region src/internal/escapeRecorder.ts
331
- const TIMER_NAMES = [
332
- "setTimeout",
333
- "setInterval",
334
- "setImmediate"
335
- ];
336
- const OWN_MODULE = /effect-sim-kernel[/\\](?:src|dist)[/\\]/u;
337
- const BUNDLED_DEPENDENCY = /.*node_modules[/\\]\.pnpm[/\\][^/\\]+[/\\]node_modules[/\\]/u;
338
- const stackFrames = () => ((/* @__PURE__ */ new Error()).stack ?? "").split("\n").slice(2);
339
- const outsideFrames = () => stackFrames().filter((frame) => !OWN_MODULE.test(frame));
340
- const shorten = (frame) => frame.trim().replace(/^at /u, "").replace(BUNDLED_DEPENDENCY, "");
341
- const siteOf = () => outsideFrames().slice(0, 2).map(shorten).join(" <- ");
342
- const isTimer = (candidate) => typeof candidate === "function";
343
- const timerOf = (name) => {
344
- const original = Reflect.get(globalThis, name);
345
- return isTimer(original) ? original : void 0;
346
- };
347
- const wrapperOf = (name, original, report) => (...args) => {
348
- report({
349
- timer: name,
350
- site: siteOf()
351
- });
352
- return Reflect.apply(original, globalThis, args);
353
- };
354
- const captureTimer = (originals, name) => {
355
- const original = timerOf(name);
356
- if (original !== void 0) originals.set(name, original);
357
- };
358
- const capturedTimers = () => {
359
- const originals = /* @__PURE__ */ new Map();
360
- for (const name of TIMER_NAMES) captureTimer(originals, name);
361
- return originals;
362
- };
363
- const reinstall = (originals) => {
364
- for (const [name, original] of originals) Reflect.set(globalThis, name, original);
365
- };
366
- /**
367
- * Wrap the global timer functions so every call made while the run is live is
368
- * reported, then still carried out so the run can be classified. Returns the
369
- * restore function and the untouched host timer the kernel's own machinery
370
- * uses to yield to completed host work; a run installs the recorder at its
371
- * start and restores it in its `finally`, so nothing outside a run is ever
372
- * observed.
373
- */
374
- /** @internal */
375
- const installEscapeRecorder = (report) => {
376
- const originals = capturedTimers();
377
- for (const [name, original] of originals) Reflect.set(globalThis, name, wrapperOf(name, original, report));
378
- return {
379
- restore: () => reinstall(originals),
380
- hostImmediate: hostImmediateOf(originals)
381
- };
382
- };
383
- const hostImmediateOf = (originals) => {
384
- const immediate = originals.get("setImmediate");
385
- return immediate === void 0 ? syncImmediate : immediate;
386
- };
387
- const syncImmediate = (handler) => handler();
388
174
  //#endregion
389
175
  //#region src/internal/kernel.ts
390
176
  /**
@@ -397,8 +183,8 @@ const syncImmediate = (handler) => handler();
397
183
  * module-level mutable state survives a release.
398
184
  *
399
185
  * The hooks read Effect runtime internals, which the plan allows only inside this
400
- * package (R17). They are pinned to `effect` 4.0.0-rc.117 and fail loudly when a
401
- * field or method moves.
186
+ * package (R17). They are verified against `effect` 4.0.0 and fail loudly when a
187
+ * field or method moves in a later 4.x release.
402
188
  */
403
189
  const CURRENT_FIBER = "~effect/Fiber/currentFiber";
404
190
  const FIBER_METHODS = [
@@ -410,7 +196,7 @@ const FIBER_METHODS = [
410
196
  const REF_FIELDS = ["ref"];
411
197
  const DEFERRED_FIELDS = ["effect", "resumes"];
412
198
  const isFiberLike = (candidate) => typeof candidate === "object";
413
- const isHostObject$2 = (candidate) => typeof candidate === "object";
199
+ const isHostObject$3 = (candidate) => typeof candidate === "object";
414
200
  const isMethod = (candidate) => typeof candidate === "function";
415
201
  const fieldOf$1 = (target, key) => Reflect.get(target, key);
416
202
  const methodOf = (target, name) => {
@@ -420,11 +206,48 @@ const methodOf = (target, name) => {
420
206
  const dispatcherOf = (fiber) => fieldOf$1(fiber, "currentDispatcher");
421
207
  const protoOf$1 = (value) => {
422
208
  const proto = Object.getPrototypeOf(value);
423
- return isHostObject$2(proto) ? proto : Object.prototype;
209
+ return isHostObject$3(proto) ? proto : Object.prototype;
210
+ };
211
+ const ACTIVE_RUN = Symbol("~effect-sim-kernel/activeRun");
212
+ const isKernel = (candidate) => "pending" in candidate && "release" in candidate;
213
+ /** @internal */
214
+ const FIBER_PROTOTYPE = protoOf$1(Effect.runFork(Effect.void));
215
+ const heldRun = () => fieldOf$1(FIBER_PROTOTYPE, ACTIVE_RUN);
216
+ const isLiveKernel = (candidate) => isHostObject$3(candidate) && isKernel(candidate);
217
+ /** @internal */
218
+ const currentKernel = () => {
219
+ const candidate = heldRun();
220
+ return isLiveKernel(candidate) ? candidate : void 0;
221
+ };
222
+ /** @internal */
223
+ const isRunLive = () => isHostObject$3(heldRun());
224
+ /** @internal */
225
+ const claimRun = (kernel) => {
226
+ Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, kernel);
424
227
  };
228
+ /** @internal */
229
+ const releaseRun = () => {
230
+ Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, void 0);
231
+ };
232
+ /**
233
+ * Whether the live run is inside a synchronous step. A library that samples a
234
+ * run from outside can tell work the run caused from work an outside fiber did
235
+ * between steps. `false` when no run is live.
236
+ */
237
+ /** @internal */
238
+ const isStepping = () => currentKernel()?.phase === "step";
239
+ /**
240
+ * From here on, the schedule may deviate from Effect's order (R15). Under
241
+ * `explore: 'body'`, every decision before it stays on Effect's order.
242
+ */
243
+ /** @internal */
244
+ const beginExploration = Effect.sync(() => {
245
+ const kernel = currentKernel();
246
+ if (kernel !== void 0) kernel.beginExploration();
247
+ });
425
248
  const captureDescriptor = (proto, field) => {
426
249
  const descriptor = Reflect.getOwnPropertyDescriptor(proto, field);
427
- return isHostObject$2(descriptor) ? descriptor : void 0;
250
+ return isHostObject$3(descriptor) ? descriptor : void 0;
428
251
  };
429
252
  const descriptorEntryOf = (proto, field) => {
430
253
  const descriptor = captureDescriptor(proto, field);
@@ -504,7 +327,7 @@ const DEFAULT_VALUE = "~effect/Context/defaultValue";
504
327
  const FIBER_CACHE = "_fiberCache";
505
328
  const emptyContextRoot = () => {
506
329
  const root = Reflect.get(Context.empty(), "cacheRoot");
507
- if (!isHostObject$2(root)) throw new Error("effect-sim-kernel: Context no longer has a cache root on the pinned Effect version");
330
+ if (!isHostObject$3(root)) throw new Error("effect-sim-kernel: Context no longer has a cache root on the pinned Effect version");
508
331
  return root;
509
332
  };
510
333
  const setAsideFiberCache = (root) => {
@@ -889,6 +712,172 @@ const makeKernel = (options) => {
889
712
  return kernel;
890
713
  };
891
714
  //#endregion
715
+ //#region src/internal/deadlock.ts
716
+ const NO_FRAMES = [];
717
+ const NO_RESOURCES = [];
718
+ const isHostObject$2 = (candidate) => typeof candidate === "object";
719
+ const isFrameList = (candidate) => Array.isArray(candidate);
720
+ const isHostProcess = (candidate) => isHostObject$2(candidate);
721
+ const hostProcess = () => Reflect.get(globalThis, "process");
722
+ const resourceNames = () => {
723
+ const host = hostProcess();
724
+ return isHostProcess(host) ? host.getActiveResourcesInfo() : NO_RESOURCES;
725
+ };
726
+ const bump = (counts, resource) => {
727
+ counts.set(resource, (counts.get(resource) ?? 0) + 1);
728
+ };
729
+ /** A snapshot of the process resources alive right now, by resource kind. */
730
+ /** @internal */
731
+ const resourceCounts = () => {
732
+ const counts = /* @__PURE__ */ new Map();
733
+ for (const resource of resourceNames()) bump(counts, resource);
734
+ return counts;
735
+ };
736
+ const extraOf = (before, entry) => entry[1] - (before.get(entry[0]) ?? 0);
737
+ const appendExtra = (appeared, before, entry) => {
738
+ const extra = extraOf(before, entry);
739
+ if (extra > 0) appeared.push(`${entry[0]} x${extra}`);
740
+ };
741
+ /** Resource kinds that appeared since `before`, as `"<kind> x<count>"` entries. */
742
+ /** @internal */
743
+ const newResources = (before) => {
744
+ const appeared = [];
745
+ for (const entry of resourceCounts()) appendExtra(appeared, before, entry);
746
+ return appeared;
747
+ };
748
+ const FAMILY_PATTERNS = [
749
+ [/Socket|TCP|UDP|Pipe|GetAddrInfo/u, "Socket"],
750
+ [/FS|File/u, "File"],
751
+ [/Timeout|Immediate/u, "RealTimer"]
752
+ ];
753
+ const matchedFamily = (resource) => {
754
+ return FAMILY_PATTERNS.find(([pattern]) => pattern.test(resource))?.[1];
755
+ };
756
+ const familyOf = (resource) => matchedFamily(resource) ?? "Other";
757
+ const isNamed = (family) => family !== "Other";
758
+ /**
759
+ * What a set of newly appeared resources can wake. Files and sockets name
760
+ * themselves first; a leftover kind is still named, through `resources`.
761
+ */
762
+ /** @internal */
763
+ const waitKindOf = (resources) => {
764
+ return resources.map(familyOf).find(isNamed) ?? "Other";
765
+ };
766
+ const stringOn = (described, key) => {
767
+ const value = Reflect.get(described, key);
768
+ return typeof value === "string" ? value : void 0;
769
+ };
770
+ const stringFieldOf = (described, key) => {
771
+ if (!isHostObject$2(described)) return void 0;
772
+ return stringOn(described, key);
773
+ };
774
+ const argsNameOn = (described) => {
775
+ const args = Reflect.get(described, "args");
776
+ return typeof args === "function" ? args.name : void 0;
777
+ };
778
+ const nameOf = (described) => {
779
+ if (!isHostObject$2(described)) return void 0;
780
+ return argsNameOn(described);
781
+ };
782
+ const applyToJson = (frame) => {
783
+ const toJson = Reflect.get(frame, "toJSON");
784
+ return typeof toJson === "function" ? Reflect.apply(toJson, frame, []) : void 0;
785
+ };
786
+ const describeFrame = (frame) => {
787
+ if (!isHostObject$2(frame)) return void 0;
788
+ return applyToJson(frame);
789
+ };
790
+ const namedOp = (op, described) => {
791
+ const name = nameOf(described);
792
+ return name === void 0 ? op : `${op}(${name})`;
793
+ };
794
+ const renderFrame = (frame) => {
795
+ const described = describeFrame(frame);
796
+ const op = stringFieldOf(described, "op") ?? "?";
797
+ return namedOp(op, described);
798
+ };
799
+ const renderedFrames = (stack) => stack.slice(1, 7).map(renderFrame);
800
+ const framesOf = (fiber) => {
801
+ const stack = Reflect.get(fiber, "_stack");
802
+ return isFrameList(stack) ? renderedFrames(stack) : NO_FRAMES;
803
+ };
804
+ const isUnfinished = (fiber) => fiber.pollUnsafe() === void 0;
805
+ const collectSuspended = (suspended, fiber) => {
806
+ if (isUnfinished(fiber)) suspended.push({
807
+ id: fiber.id,
808
+ frames: framesOf(fiber)
809
+ });
810
+ };
811
+ /**
812
+ * Every unfinished fiber the kernel ran, innermost frame first. Fibers that
813
+ * already exited are not suspended, so they are left out.
814
+ */
815
+ /** @internal */
816
+ const describeSuspended = (fibers) => {
817
+ const suspended = [];
818
+ for (const fiber of fibers) collectSuspended(suspended, fiber);
819
+ return suspended;
820
+ };
821
+ //#endregion
822
+ //#region src/internal/escapeRecorder.ts
823
+ const TIMER_NAMES = [
824
+ "setTimeout",
825
+ "setInterval",
826
+ "setImmediate"
827
+ ];
828
+ const OWN_MODULE = /effect-sim-kernel[/\\](?:src|dist)[/\\]/u;
829
+ const BUNDLED_DEPENDENCY = /.*node_modules[/\\]\.pnpm[/\\][^/\\]+[/\\]node_modules[/\\]/u;
830
+ const stackFrames = () => ((/* @__PURE__ */ new Error()).stack ?? "").split("\n").slice(2);
831
+ const outsideFrames = () => stackFrames().filter((frame) => !OWN_MODULE.test(frame));
832
+ const shorten = (frame) => frame.trim().replace(/^at /u, "").replace(BUNDLED_DEPENDENCY, "");
833
+ const siteOf = () => outsideFrames().slice(0, 2).map(shorten).join(" <- ");
834
+ const isTimer = (candidate) => typeof candidate === "function";
835
+ const timerOf = (name) => {
836
+ const original = Reflect.get(globalThis, name);
837
+ return isTimer(original) ? original : void 0;
838
+ };
839
+ const wrapperOf = (name, original, report) => (...args) => {
840
+ report({
841
+ timer: name,
842
+ site: siteOf()
843
+ });
844
+ return Reflect.apply(original, globalThis, args);
845
+ };
846
+ const captureTimer = (originals, name) => {
847
+ const original = timerOf(name);
848
+ if (original !== void 0) originals.set(name, original);
849
+ };
850
+ const capturedTimers = () => {
851
+ const originals = /* @__PURE__ */ new Map();
852
+ for (const name of TIMER_NAMES) captureTimer(originals, name);
853
+ return originals;
854
+ };
855
+ const reinstall = (originals) => {
856
+ for (const [name, original] of originals) Reflect.set(globalThis, name, original);
857
+ };
858
+ /**
859
+ * Wrap the global timer functions so every call made while the run is live is
860
+ * reported, then still carried out so the run can be classified. Returns the
861
+ * restore function and the untouched host timer the kernel's own machinery
862
+ * uses to yield to completed host work; a run installs the recorder at its
863
+ * start and restores it in its `finally`, so nothing outside a run is ever
864
+ * observed.
865
+ */
866
+ /** @internal */
867
+ const installEscapeRecorder = (report) => {
868
+ const originals = capturedTimers();
869
+ for (const [name, original] of originals) Reflect.set(globalThis, name, wrapperOf(name, original, report));
870
+ return {
871
+ restore: () => reinstall(originals),
872
+ hostImmediate: hostImmediateOf(originals)
873
+ };
874
+ };
875
+ const hostImmediateOf = (originals) => {
876
+ const immediate = originals.get("setImmediate");
877
+ return immediate === void 0 ? syncImmediate : immediate;
878
+ };
879
+ const syncImmediate = (handler) => handler();
880
+ //#endregion
892
881
  //#region src/internal/runQueue.ts
893
882
  let tail = Promise.resolve();
894
883
  /** @internal */
@@ -930,15 +919,6 @@ const DEFAULT_MAX_STEPS = 5e4;
930
919
  const DEFAULT_STEP_TURNS = 4;
931
920
  const DEFAULT_QUIESCENCE_TURNS = 1e3;
932
921
  const MAX_HOST_WAITS = 1e4;
933
- /**
934
- * Marks the point in a program where exploration may begin (R15). Under
935
- * `explore: 'body'`, every decision before it stays on Effect's order.
936
- */
937
- /** @internal */
938
- const beginExploration = Effect.sync(() => {
939
- const kernel = currentKernel();
940
- if (kernel !== void 0) kernel.beginExploration();
941
- });
942
922
  /** One microtask checkpoint: already-queued callbacks run before this resumes. */
943
923
  const turn = (value) => Promise.resolve().then(() => value);
944
924
  const drain = (turns) => turns <= 0 ? Promise.resolve() : turn(void 0).then(() => drain(turns - 1));
@@ -1154,19 +1134,20 @@ const queuedRun = (program, options, release) => {
1154
1134
  return Promise.reject(error);
1155
1135
  }
1156
1136
  };
1157
- const runKernelImpl = (program, options = {}) => {
1137
+ const runImpl = (program, options = {}) => {
1158
1138
  rejectAwaitedExploration(options);
1159
1139
  return acquireRun().then((release) => queuedRun(program, options, release));
1160
1140
  };
1161
- //#endregion
1162
- //#region src/Kernel/Run.ts
1163
- const run = dual((args) => args.length > 0 && isProgram(args[0]), runKernelImpl);
1164
1141
  /**
1165
- * Whether the live run is inside a synchronous step. A library that samples a
1166
- * run from outside can tell work the run caused from work an outside fiber did
1167
- * between steps. `false` when no run is live.
1142
+ * Runs one Effect program under one kernel (R2: every clock the program can
1143
+ * reach is the kernel's virtual root clock; R37: at quiescence the kernel
1144
+ * settles its test clocks, then advances the root clock). Driven by the
1145
+ * decision path or chooser in `options`, it returns the exit, the decisions
1146
+ * taken, and a per-step record of which fiber ran. A run started while one is
1147
+ * active queues behind it, so two runs never share the global hooks.
1168
1148
  */
1169
- const isStepping = () => currentKernel()?.phase === "step";
1149
+ /** @internal */
1150
+ const run = dual((args) => args.length > 0 && isProgram(args[0]), runImpl);
1170
1151
  //#endregion
1171
1152
  //#region src/Kernel/Pct.ts
1172
1153
  /**
@@ -1873,7 +1854,13 @@ var TestClock_exports = /* @__PURE__ */ __exportAll({
1873
1854
  * settles its requests only when nothing else can run. Only available inside a
1874
1855
  * kernel run.
1875
1856
  */
1876
- const make = kernelTestClock;
1857
+ const make = Effect.sync(() => {
1858
+ const kernel = currentKernel();
1859
+ if (kernel === void 0) throw new Error("effect-sim-kernel: the kernel test clock is only available inside a kernel run");
1860
+ const testClock = makeTestClock(kernel.clocks.clock);
1861
+ kernel.clocks.testClocks.push(testClock);
1862
+ return testClock;
1863
+ });
1877
1864
  const contextOf = (testClock) => Context.makeUnsafe(/* @__PURE__ */ new Map([[Clock.Clock.key, testClock]]));
1878
1865
  /** The layer a harness provides in place of `TestClock.layer()`. */
1879
1866
  const layer = Layer.effectContext(Effect.map(make, contextOf));
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@systemfsoftware/effect-sim-kernel",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "author": "Ryan Lee <drdgvhbh@gmail.com>",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/systemfsoftware/systemfsoftware.git",
9
- "directory": "packages/effect-sim-kernel"
9
+ "directory": "packages/sim/effect-sim-kernel"
10
10
  },
11
- "homepage": "https://github.com/systemfsoftware/systemfsoftware/tree/main/packages/effect-sim-kernel#readme",
11
+ "homepage": "https://github.com/systemfsoftware/systemfsoftware/tree/main/packages/sim/effect-sim-kernel#readme",
12
12
  "bugs": "https://github.com/systemfsoftware/systemfsoftware/issues",
13
13
  "description": "Deterministic simulation kernel for Effect: a controlled scheduler, in-process wakeups, escape and deadlock detection, interruption at a chosen step, and replay from a decision path.",
14
14
  "keywords": [
@@ -33,13 +33,13 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@microsoft/api-extractor": "^7.59.1",
36
- "@systemfsoftware/oxlint-config-recommended": "^3.1.0",
36
+ "@systemfsoftware/oxlint-config-recommended": "^4.0.0",
37
37
  "@systemfsoftware/tsconfig": "^2.0.1",
38
38
  "@systemfsoftware/tsdown-config": "^0.1.0",
39
- "@systemfsoftware/vitest": "^0.1.0",
39
+ "@systemfsoftware/vitest": "^1.0.0",
40
40
  "@systemfsoftware/vitest-config": "^0.1.0",
41
41
  "@types/node": "^26",
42
- "effect": "4.0.0-rc.117",
42
+ "effect": "^4",
43
43
  "oxlint": "~1.82.0",
44
44
  "rimraf": "^6.1.3",
45
45
  "tsdown": "^0.23.0",
@@ -47,9 +47,10 @@
47
47
  "vitest": "^5"
48
48
  },
49
49
  "peerDependencies": {
50
- "effect": "4.0.0-rc.117"
50
+ "effect": "^4"
51
51
  },
52
52
  "publishConfig": {
53
+ "access": "public",
53
54
  "provenance": true
54
55
  },
55
56
  "scripts": {