@systemfsoftware/effect-sim-kernel 0.2.0 → 2.0.5
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 +2 -38
- package/README.md +15 -2
- package/dist/index.d.ts +58 -46
- package/dist/index.mjs +331 -266
- package/package.json +13 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,41 +1,5 @@
|
|
|
1
1
|
# @systemfsoftware/effect-sim-kernel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @systemfsoftware/effect-sim-kernel@2.0.5
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- 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.
|
|
8
|
-
|
|
9
|
-
- 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.
|
|
10
|
-
|
|
11
|
-
`@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.
|
|
12
|
-
|
|
13
|
-
`@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.
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- 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.
|
|
18
|
-
|
|
19
|
-
## 0.1.1
|
|
20
|
-
|
|
21
|
-
### Minor Changes
|
|
22
|
-
|
|
23
|
-
- 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.
|
|
24
|
-
|
|
25
|
-
## 0.1.0
|
|
26
|
-
|
|
27
|
-
### Minor Changes
|
|
28
|
-
|
|
29
|
-
- Kernel runs in one process queue behind each other in arrival order instead of refusing a second run that starts while one is live. New `Kernel.isStepping()` returns true only while the kernel is executing a step of a run.
|
|
30
|
-
|
|
31
|
-
- New package: a deterministic simulation kernel for Effect v4. `Kernel.run(program, options)` runs any Effect program under a schedule you control and returns its `Exit`, the decisions the schedule took, and a record of which fiber ran at every step. A failed run names why: `Escape` (the program reached a real timer), `Blocked` (it waits on a file or socket outside the process), `Deadlock` (nothing can wake a suspended fiber; every suspended fiber is listed with its frames), or `Runaway`. Replay a run exactly with `path`, steer it with `choose`, and interrupt a fiber at a chosen step with `interrupt`. `external: 'await'` waits for real file and socket I/O on Effect's own order. `Kernel.search`, `Kernel.pct` and `Kernel.shrink` explore schedules up to a stated preemption bound and budget, and shrink a failing schedule to its smallest form. `Kernel.TestClock.layer` stands in for Effect's `TestClock`, so `TestClock.adjust` and `setTime` move the kernel's virtual time. Only one kernel run can be active at a time, and a second one started beside it is refused.
|
|
32
|
-
|
|
33
|
-
A runtime the program starts for itself during a run (`Effect.runFork` or `Effect.runPromise` with no scheduler of its own) runs under the same schedule and virtual clock, and a runtime started after the run finishes runs on the host again.
|
|
34
|
-
|
|
35
|
-
- New `local` conformance profile (`CONFORMANCE_PROFILE=local`): seeded runs use `Kernel.localSeeds` (25) instead of 250, and `Kernel.preemptionsFor` / `Kernel.currentPreemptionsFor` cap a search's preemption bound at `Kernel.localPreemptions` (1). An unset profile is still `per-change`, and `Kernel.search` itself is never capped.
|
|
36
|
-
|
|
37
|
-
- 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.
|
|
38
|
-
|
|
39
|
-
`@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.
|
|
40
|
-
|
|
41
|
-
`@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.
|
|
5
|
+
Follows Effect 4.1.4's automatic yields. A resumed fiber continues inline instead of being queued as a new step, so a run stopped after any step releases the resources its scope acquired. Previously, under Effect 4.1.4, such a stop could leave an acquired resource unreleased, or leave the run waiting with no runnable step.
|
package/README.md
CHANGED
|
@@ -77,6 +77,19 @@ await Kernel.run(program, { explore: 'body' })
|
|
|
77
77
|
|
|
78
78
|
Decisions before `Kernel.beginExploration` stay on Effect's order and are not recorded.
|
|
79
79
|
|
|
80
|
+
## Seeded schedules
|
|
81
|
+
|
|
82
|
+
`Kernel.pick({ seed, steps })` is a seeded PCT chooser: a seed replays its schedule exactly, and its priority change points fall among the first `steps` explored decisions. `steps` is a `Kernel.PctStepCount`, a whole number of at least 0, usually `Kernel.PctStepCount.make(baseline.decisions.length)`. Its optional `depth` is a `Kernel.PctDepth`, a whole number of at least 1 (`Kernel.PctDepth.make(4)`); it defaults to `Kernel.pctDepth`, 3. `Kernel.seedsFor(baseline, profile)` and `Kernel.currentSeedsFor(baseline)` say how many seeds a baseline run is worth:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
const baseline = await Kernel.run(program, { explore: 'body' })
|
|
86
|
+
const seeds = await Effect.runPromise(Kernel.currentSeedsFor(baseline))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
A baseline with a **choice point** — an explored step where more than one runnable task was pending, the running fiber's own continuation included — gets the profile's count: 250 at `per-change`, 25 at `local` (`CONFORMANCE_PROFILE=local`). A baseline without one has a single schedule, so every seed would replay it: it gets 0. The fiber count does not decide this — a fiber forked and interrupted before it ever runs is a choice point, and steps before `Kernel.beginExploration` never count.
|
|
90
|
+
|
|
91
|
+
`Kernel.pct(program)` applies the same rule when no `seeds` option is given, and its `bound.runs` counts the runs it made: 1 for a choice-free program. That one run is judged, and a failure there is reported as seed 0, which replays it. An explicit `seeds` is a `Kernel.PctSeedCount`, a whole number of at least 0, and is always honoured.
|
|
92
|
+
|
|
80
93
|
## Test time
|
|
81
94
|
|
|
82
95
|
Every clock the program can reach is the kernel's virtual root clock: time moves only when nothing can run, no in-process wait reaches a real timer, and a program that touches one fails the run.
|
|
@@ -93,9 +106,9 @@ await Kernel.run(program.pipe(Effect.provide(Kernel.TestClock.layer)))
|
|
|
93
106
|
yield* TestClock.adjust('91 seconds')
|
|
94
107
|
```
|
|
95
108
|
|
|
96
|
-
##
|
|
109
|
+
## Verified Effect version
|
|
97
110
|
|
|
98
|
-
The hooks read Effect runtime internals (the `Scheduler` service, `FiberImpl` methods, `Ref`/`Deferred` fields), which is allowed only inside this package. They are
|
|
111
|
+
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
112
|
|
|
100
113
|
## Development
|
|
101
114
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Clock, Config, Duration, Effect, Exit, Fiber, Layer, Scheduler } from "effect";
|
|
1
|
+
import { Clock, Config, Duration, Effect, Exit, Fiber, Layer, Scheduler, Schema } from "effect";
|
|
2
2
|
//#region src/Kernel/Bound.d.ts
|
|
3
3
|
type UnobservedPrimitive = 'Queue' | 'PubSub' | 'Semaphore' | 'Latch' | 'Scope finalizer';
|
|
4
4
|
interface Pruning {
|
|
@@ -16,26 +16,13 @@ interface Bound {
|
|
|
16
16
|
declare const pruned: Pruning;
|
|
17
17
|
declare const unpruned: (disabledBy: ReadonlyArray<UnobservedPrimitive>) => Pruning;
|
|
18
18
|
//#endregion
|
|
19
|
-
//#region src/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
}
|
|
19
|
+
//#region src/Kernel/Pct.schema.d.ts
|
|
20
|
+
declare const PctDepth: Schema.brand<Schema.Int, "PctDepth">;
|
|
21
|
+
type PctDepth = typeof PctDepth.Type;
|
|
22
|
+
declare const PctSeedCount: Schema.brand<Schema.Int, "PctSeedCount">;
|
|
23
|
+
type PctSeedCount = typeof PctSeedCount.Type;
|
|
24
|
+
declare const PctStepCount: Schema.brand<Schema.Int, "PctStepCount">;
|
|
25
|
+
type PctStepCount = typeof PctStepCount.Type;
|
|
39
26
|
declare namespace TestClock_d_exports {
|
|
40
27
|
export { TestClock, layer, make };
|
|
41
28
|
}
|
|
@@ -64,6 +51,27 @@ declare const make: Effect.Effect<TestClock>;
|
|
|
64
51
|
/** The layer a harness provides in place of `TestClock.layer()`. */
|
|
65
52
|
declare const layer: Layer.Layer<Clock.Clock>;
|
|
66
53
|
//#endregion
|
|
54
|
+
//#region src/internal/deadlock.d.ts
|
|
55
|
+
/** The process resources that can call back into the program from outside. */
|
|
56
|
+
/** @internal */
|
|
57
|
+
type WaitKind = 'RealTimer' | 'File' | 'Socket' | 'Other';
|
|
58
|
+
/** @internal */
|
|
59
|
+
interface SuspendedFiber {
|
|
60
|
+
readonly id: number;
|
|
61
|
+
readonly frames: ReadonlyArray<string>;
|
|
62
|
+
}
|
|
63
|
+
/** @internal */
|
|
64
|
+
type AnyFiber<A = unknown, E = unknown> = Fiber.Fiber<A, E>;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/internal/escapeRecorder.d.ts
|
|
67
|
+
/** @internal */
|
|
68
|
+
type TimerName = 'setTimeout' | 'setInterval' | 'setImmediate';
|
|
69
|
+
/** @internal */
|
|
70
|
+
interface Escape {
|
|
71
|
+
readonly timer: TimerName;
|
|
72
|
+
readonly site: string;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
67
75
|
//#region src/internal/kernel.d.ts
|
|
68
76
|
/** An index into the kernel's pending task list: the schedule's unit of choice. */
|
|
69
77
|
/** @internal */
|
|
@@ -108,6 +116,19 @@ interface StepRecord {
|
|
|
108
116
|
/** Which fiber an interruption names (R5). */
|
|
109
117
|
/** @internal */
|
|
110
118
|
type FiberTarget = 'root' | 'lastRan' | number;
|
|
119
|
+
/**
|
|
120
|
+
* Whether the live run is inside a synchronous step. A library that samples a
|
|
121
|
+
* run from outside can tell work the run caused from work an outside fiber did
|
|
122
|
+
* between steps. `false` when no run is live.
|
|
123
|
+
*/
|
|
124
|
+
/** @internal */
|
|
125
|
+
declare const isStepping: () => boolean;
|
|
126
|
+
/**
|
|
127
|
+
* From here on, the schedule may deviate from Effect's order (R15). Under
|
|
128
|
+
* `explore: 'body'`, every decision before it stays on Effect's order.
|
|
129
|
+
*/
|
|
130
|
+
/** @internal */
|
|
131
|
+
declare const beginExploration: Effect.Effect<void>;
|
|
111
132
|
//#endregion
|
|
112
133
|
//#region src/internal/stepLoop.d.ts
|
|
113
134
|
/**
|
|
@@ -223,35 +244,30 @@ interface RunOptions {
|
|
|
223
244
|
readonly onQuiescent?: () => boolean;
|
|
224
245
|
}
|
|
225
246
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
247
|
+
* Runs one Effect program under one kernel (R2: every clock the program can
|
|
248
|
+
* reach is the kernel's virtual root clock; R37: at quiescence the kernel
|
|
249
|
+
* settles its test clocks, then advances the root clock). Driven by the
|
|
250
|
+
* decision path or chooser in `options`, it returns the exit, the decisions
|
|
251
|
+
* taken, and a per-step record of which fiber ran. A run started while one is
|
|
252
|
+
* active queues behind it, so two runs never share the global hooks.
|
|
228
253
|
*/
|
|
229
254
|
/** @internal */
|
|
230
|
-
declare const beginExploration: Effect.Effect<void>;
|
|
231
|
-
//#endregion
|
|
232
|
-
//#region src/Kernel/Run.d.ts
|
|
233
255
|
declare const run: {
|
|
234
256
|
<A, E>(program: Effect.Effect<A, E>, options?: RunOptions): Promise<RunResult<A, E>>;
|
|
235
257
|
(options?: RunOptions): <A, E>(program: Effect.Effect<A, E>) => Promise<RunResult<A, E>>;
|
|
236
258
|
};
|
|
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
259
|
//#endregion
|
|
244
260
|
//#region src/Kernel/Pct.d.ts
|
|
245
261
|
interface PickOptions {
|
|
246
262
|
readonly seed: number;
|
|
247
|
-
readonly depth?:
|
|
248
|
-
readonly steps:
|
|
263
|
+
readonly depth?: PctDepth;
|
|
264
|
+
readonly steps: PctStepCount;
|
|
249
265
|
}
|
|
250
266
|
declare const pick: (options: PickOptions) => (choice: Choice) => Decision | undefined;
|
|
251
267
|
interface PctOptions<A, E> {
|
|
252
|
-
readonly seeds?:
|
|
253
|
-
readonly depth?:
|
|
254
|
-
readonly steps?:
|
|
268
|
+
readonly seeds?: PctSeedCount;
|
|
269
|
+
readonly depth?: PctDepth;
|
|
270
|
+
readonly steps?: PctStepCount;
|
|
255
271
|
readonly isFailure?: (result: RunResult<A, E>) => boolean;
|
|
256
272
|
readonly maxSteps?: number;
|
|
257
273
|
}
|
|
@@ -276,20 +292,16 @@ declare const pct: {
|
|
|
276
292
|
* not the verdict: the same checks run at the per-change budget in CI.
|
|
277
293
|
*/
|
|
278
294
|
type ProfileName = 'local' | 'per-change';
|
|
279
|
-
declare const pctDepth
|
|
295
|
+
declare const pctDepth: PctDepth;
|
|
280
296
|
declare const perChangeSeeds = 250;
|
|
281
297
|
declare const localSeeds = 25;
|
|
282
298
|
declare const localPreemptions = 1;
|
|
283
299
|
declare const currentProfile: Effect.Effect<ProfileName, Config.ConfigError>;
|
|
284
|
-
interface Budget {
|
|
285
|
-
readonly fibers: number;
|
|
286
|
-
readonly steps: number;
|
|
287
|
-
}
|
|
288
300
|
declare const seedsFor: {
|
|
289
|
-
(
|
|
290
|
-
(profile?: ProfileName): (
|
|
301
|
+
(baseline: RunHistory, profile?: ProfileName): number;
|
|
302
|
+
(profile?: ProfileName): (baseline: RunHistory) => number;
|
|
291
303
|
};
|
|
292
|
-
declare const currentSeedsFor: (
|
|
304
|
+
declare const currentSeedsFor: (baseline: RunHistory) => Effect.Effect<number, Config.ConfigError>;
|
|
293
305
|
declare const preemptionsFor: {
|
|
294
306
|
(requested: number | undefined, profile: ProfileName): number | undefined;
|
|
295
307
|
(profile: ProfileName): (requested: number | undefined) => number | undefined;
|
|
@@ -359,7 +371,7 @@ declare const shrinkCommands: {
|
|
|
359
371
|
<C>(stillFails: (candidate: ReadonlyArray<C>) => Promise<boolean>): (commands: ReadonlyArray<C>) => Promise<ReadonlyArray<C>>;
|
|
360
372
|
};
|
|
361
373
|
declare namespace mod_d_exports {
|
|
362
|
-
export { AnyFiber, BlockedFailure, Bound,
|
|
374
|
+
export { AnyFiber, BlockedFailure, Bound, Choice, ChoiceOption, DeadlockFailure, Decision, Escape, EscapeFailure, FiberTarget, Interruption, OverBudgetOutcome, PctDepth, PctFailure, PctOptions, PctOutcome, PctSeedCount, PctStepCount, PickOptions, ProfileName, Pruning, RunCompleted, RunFailed, RunFailure, RunHistory, RunOptions, RunResult, RunawayFailure, SearchFailure, SearchOptions, SearchOutcome, SearchReport, ShrinkOptions, ShrinkOutcome, StepRecord, SuspendedFiber, TestClock_d_exports as TestClock, TimerName, UnobservedPrimitive, WaitKind, beginExploration, currentPreemptionsFor, currentProfile, currentSeedsFor, isStepping, localPreemptions, localSeeds, runOutcomeTags as outcomeTags, pct, pctDepth, perChangeSeeds, pick, preemptionsFor, pruned, run, search, searchTags, seedsFor, shrink, shrinkCommands, unpruned };
|
|
363
375
|
}
|
|
364
376
|
//#endregion
|
|
365
377
|
export { mod_d_exports as Kernel };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
-
import { Clock, Config, Context, Deferred, Duration, Effect, Latch, Layer, Match, Option, PubSub, Queue, Ref, Scheduler, Scope, Semaphore } from "effect";
|
|
2
|
+
import { Clock, Config, Context, Deferred, Duration, Effect, Latch, Layer, Match, Option, Order, PubSub, Queue, Ref, Scheduler, Schema, Scope, Semaphore } from "effect";
|
|
3
3
|
import { dual } from "effect/Function";
|
|
4
4
|
//#region src/Kernel/Bound.ts
|
|
5
5
|
const pruned = {
|
|
@@ -10,60 +10,29 @@ const unpruned = (disabledBy) => ({
|
|
|
10
10
|
enabled: false,
|
|
11
11
|
disabledBy
|
|
12
12
|
});
|
|
13
|
-
const PROFILES = { local: "local" };
|
|
14
|
-
const profileOf = (name) => PROFILES[name] ?? "per-change";
|
|
15
|
-
const currentProfile = Effect.map(Config.option(Config.String("CONFORMANCE_PROFILE")), Option.match({
|
|
16
|
-
onNone: () => profileOf(""),
|
|
17
|
-
onSome: profileOf
|
|
18
|
-
}));
|
|
19
|
-
const SEEDS = {
|
|
20
|
-
local: () => 25,
|
|
21
|
-
"per-change": () => 250
|
|
22
|
-
};
|
|
23
|
-
const seedsForImpl = (budget, profile = "per-change") => SEEDS[profile](budget);
|
|
24
|
-
const isBudgetFirst = (args) => args.length === 0 || typeof args[0] !== "string";
|
|
25
|
-
const seedsFor = dual(isBudgetFirst, seedsForImpl);
|
|
26
|
-
const currentSeedsFor = (budget) => Effect.map(currentProfile, (profile) => seedsFor(budget, profile));
|
|
27
|
-
const localBoundOf = (requested) => Math.min(requested ?? 1, 1);
|
|
28
|
-
const boundImpl = (requested, profile) => profile === "local" ? localBoundOf(requested) : requested;
|
|
29
|
-
const preemptionsFor = dual(2, boundImpl);
|
|
30
|
-
const currentPreemptionsFor = (requested) => Effect.map(currentProfile, (profile) => preemptionsFor(requested, profile));
|
|
31
13
|
//#endregion
|
|
32
|
-
//#region src/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
};
|
|
14
|
+
//#region src/Kernel/Pct.schema.ts
|
|
15
|
+
const generatedBetween = (minimum, maximum) => Schema.makeFilter(() => void 0, { arbitraryConstraint: {
|
|
16
|
+
order: Order.Number,
|
|
17
|
+
minimum,
|
|
18
|
+
maximum
|
|
19
|
+
} });
|
|
20
|
+
const PctDepth = Schema.Int.pipe(Schema.check(Schema.isBetween({
|
|
21
|
+
minimum: 1,
|
|
22
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
23
|
+
}, { message: "a PCT depth is a whole number of at least 1" })), Schema.check(generatedBetween(1, 5)), Schema.brand("PctDepth"));
|
|
24
|
+
const PctSeedCount = Schema.Int.pipe(Schema.check(Schema.isBetween({
|
|
25
|
+
minimum: 0,
|
|
26
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
27
|
+
}, { message: "a PCT seed count is a whole number of at least 0" })), Schema.check(generatedBetween(0, 3)), Schema.brand("PctSeedCount"));
|
|
28
|
+
const PctStepCount = Schema.Int.pipe(Schema.check(Schema.isBetween({
|
|
29
|
+
minimum: 0,
|
|
30
|
+
maximum: Number.MAX_SAFE_INTEGER
|
|
31
|
+
}, { message: "a PCT step count is a whole number of explored decisions, at least 0" })), Schema.check(generatedBetween(0, 1e4)), Schema.brand("PctStepCount"));
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/internal/profile.schema.ts
|
|
63
34
|
/** @internal */
|
|
64
|
-
const
|
|
65
|
-
Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, void 0);
|
|
66
|
-
};
|
|
35
|
+
const ProfileNames = Schema.Literals(["local", "per-change"]);
|
|
67
36
|
//#endregion
|
|
68
37
|
//#region src/internal/clocks.ts
|
|
69
38
|
/**
|
|
@@ -207,184 +176,6 @@ const makeRunClocks = () => ({
|
|
|
207
176
|
*/
|
|
208
177
|
/** @internal */
|
|
209
178
|
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
179
|
//#endregion
|
|
389
180
|
//#region src/internal/kernel.ts
|
|
390
181
|
/**
|
|
@@ -397,8 +188,8 @@ const syncImmediate = (handler) => handler();
|
|
|
397
188
|
* module-level mutable state survives a release.
|
|
398
189
|
*
|
|
399
190
|
* The hooks read Effect runtime internals, which the plan allows only inside this
|
|
400
|
-
* package (R17). They are
|
|
401
|
-
* field or method moves.
|
|
191
|
+
* package (R17). They are verified against `effect` 4.0.0 and fail loudly when a
|
|
192
|
+
* field or method moves in a later 4.x release.
|
|
402
193
|
*/
|
|
403
194
|
const CURRENT_FIBER = "~effect/Fiber/currentFiber";
|
|
404
195
|
const FIBER_METHODS = [
|
|
@@ -410,7 +201,7 @@ const FIBER_METHODS = [
|
|
|
410
201
|
const REF_FIELDS = ["ref"];
|
|
411
202
|
const DEFERRED_FIELDS = ["effect", "resumes"];
|
|
412
203
|
const isFiberLike = (candidate) => typeof candidate === "object";
|
|
413
|
-
const isHostObject$
|
|
204
|
+
const isHostObject$3 = (candidate) => typeof candidate === "object";
|
|
414
205
|
const isMethod = (candidate) => typeof candidate === "function";
|
|
415
206
|
const fieldOf$1 = (target, key) => Reflect.get(target, key);
|
|
416
207
|
const methodOf = (target, name) => {
|
|
@@ -420,11 +211,48 @@ const methodOf = (target, name) => {
|
|
|
420
211
|
const dispatcherOf = (fiber) => fieldOf$1(fiber, "currentDispatcher");
|
|
421
212
|
const protoOf$1 = (value) => {
|
|
422
213
|
const proto = Object.getPrototypeOf(value);
|
|
423
|
-
return isHostObject$
|
|
214
|
+
return isHostObject$3(proto) ? proto : Object.prototype;
|
|
424
215
|
};
|
|
216
|
+
const ACTIVE_RUN = Symbol("~effect-sim-kernel/activeRun");
|
|
217
|
+
const isKernel = (candidate) => "pending" in candidate && "release" in candidate;
|
|
218
|
+
/** @internal */
|
|
219
|
+
const FIBER_PROTOTYPE = protoOf$1(Effect.runFork(Effect.void));
|
|
220
|
+
const heldRun = () => fieldOf$1(FIBER_PROTOTYPE, ACTIVE_RUN);
|
|
221
|
+
const isLiveKernel = (candidate) => isHostObject$3(candidate) && isKernel(candidate);
|
|
222
|
+
/** @internal */
|
|
223
|
+
const currentKernel = () => {
|
|
224
|
+
const candidate = heldRun();
|
|
225
|
+
return isLiveKernel(candidate) ? candidate : void 0;
|
|
226
|
+
};
|
|
227
|
+
/** @internal */
|
|
228
|
+
const isRunLive = () => isHostObject$3(heldRun());
|
|
229
|
+
/** @internal */
|
|
230
|
+
const claimRun = (kernel) => {
|
|
231
|
+
Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, kernel);
|
|
232
|
+
};
|
|
233
|
+
/** @internal */
|
|
234
|
+
const releaseRun = () => {
|
|
235
|
+
Reflect.set(FIBER_PROTOTYPE, ACTIVE_RUN, void 0);
|
|
236
|
+
};
|
|
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
|
+
/** @internal */
|
|
243
|
+
const isStepping = () => currentKernel()?.phase === "step";
|
|
244
|
+
/**
|
|
245
|
+
* From here on, the schedule may deviate from Effect's order (R15). Under
|
|
246
|
+
* `explore: 'body'`, every decision before it stays on Effect's order.
|
|
247
|
+
*/
|
|
248
|
+
/** @internal */
|
|
249
|
+
const beginExploration = Effect.sync(() => {
|
|
250
|
+
const kernel = currentKernel();
|
|
251
|
+
if (kernel !== void 0) kernel.beginExploration();
|
|
252
|
+
});
|
|
425
253
|
const captureDescriptor = (proto, field) => {
|
|
426
254
|
const descriptor = Reflect.getOwnPropertyDescriptor(proto, field);
|
|
427
|
-
return isHostObject$
|
|
255
|
+
return isHostObject$3(descriptor) ? descriptor : void 0;
|
|
428
256
|
};
|
|
429
257
|
const descriptorEntryOf = (proto, field) => {
|
|
430
258
|
const descriptor = captureDescriptor(proto, field);
|
|
@@ -504,7 +332,7 @@ const DEFAULT_VALUE = "~effect/Context/defaultValue";
|
|
|
504
332
|
const FIBER_CACHE = "_fiberCache";
|
|
505
333
|
const emptyContextRoot = () => {
|
|
506
334
|
const root = Reflect.get(Context.empty(), "cacheRoot");
|
|
507
|
-
if (!isHostObject$
|
|
335
|
+
if (!isHostObject$3(root)) throw new Error("effect-sim-kernel: Context no longer has a cache root on the pinned Effect version");
|
|
508
336
|
return root;
|
|
509
337
|
};
|
|
510
338
|
const setAsideFiberCache = (root) => {
|
|
@@ -584,10 +412,33 @@ const dispatchObserved = (fiber, kernel, original, args) => {
|
|
|
584
412
|
noteOtherFiberWork(fiber, kernel);
|
|
585
413
|
return applyOriginal(original, fiber, args);
|
|
586
414
|
};
|
|
415
|
+
/**
|
|
416
|
+
* The fibers whose original `evaluate` is on the stack right now. Effect 4.0.1
|
|
417
|
+
* restores a fiber's async context by calling `evaluate` again from inside its
|
|
418
|
+
* own evaluation (`FiberImpl.evaluate` re-enters through
|
|
419
|
+
* `AsyncResource.runInAsyncScope`), so one resume arrives as two nested calls.
|
|
420
|
+
* The nested call is that first resume continuing, not a new one: running the
|
|
421
|
+
* original inline keeps a resume one kernel step, as it was under 4.0.0, where
|
|
422
|
+
* queueing the nested call would spend an extra step on every slice. Only the
|
|
423
|
+
* outermost call marks the fiber; a nested call returns before marking, so the
|
|
424
|
+
* slot never outlives the evaluation it names.
|
|
425
|
+
*/
|
|
426
|
+
const evaluating = /* @__PURE__ */ new WeakSet();
|
|
427
|
+
const withEvaluation = (fiber, run) => {
|
|
428
|
+
evaluating.add(fiber);
|
|
429
|
+
try {
|
|
430
|
+
return run();
|
|
431
|
+
} finally {
|
|
432
|
+
evaluating.delete(fiber);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
const isEvaluating = (fiber) => evaluating.has(fiber);
|
|
587
436
|
const isResumableBy = (fiber, kernel) => fiber.pollUnsafe() === void 0 && dispatcherOf(fiber) === kernel.dispatcher;
|
|
588
437
|
const isResumingRun = (fiber, kernel) => kernel.running && isResumableBy(fiber, kernel);
|
|
589
438
|
const resumeExternallyThrough = (fiber, kernel, original, args) => {
|
|
590
|
-
kernel.resumeExternally(fiber, () =>
|
|
439
|
+
kernel.resumeExternally(fiber, () => {
|
|
440
|
+
withEvaluation(fiber, () => applyOriginal(original, fiber, args));
|
|
441
|
+
});
|
|
591
442
|
};
|
|
592
443
|
/**
|
|
593
444
|
* A fiber's first evaluation inside a step is a start, not a resume: Effect runs
|
|
@@ -598,12 +449,15 @@ const resumeExternallyThrough = (fiber, kernel, original, args) => {
|
|
|
598
449
|
const startsInStep = (fiber, kernel) => kernel.firstEvaluation(fiber) && kernel.phase === "step";
|
|
599
450
|
const queuedOrObserved = (fiber, kernel, original, args) => {
|
|
600
451
|
if (isResumingRun(fiber, kernel)) return resumeExternallyThrough(fiber, kernel, original, args);
|
|
601
|
-
return dispatchObserved(fiber, kernel, original, args);
|
|
452
|
+
return withEvaluation(fiber, () => dispatchObserved(fiber, kernel, original, args));
|
|
602
453
|
};
|
|
603
|
-
const
|
|
604
|
-
if (startsInStep(fiber, kernel)) return dispatchObserved(fiber, kernel, original, args);
|
|
454
|
+
const dispatchOutermost = (fiber, kernel, original, args) => {
|
|
455
|
+
if (startsInStep(fiber, kernel)) return withEvaluation(fiber, () => dispatchObserved(fiber, kernel, original, args));
|
|
605
456
|
return queuedOrObserved(fiber, kernel, original, args);
|
|
606
457
|
};
|
|
458
|
+
const isAutomaticResume = (fiber) => Reflect.get(fiber, "_autoYielded") === true;
|
|
459
|
+
const dispatchResumption = (fiber, kernel, original, args) => isAutomaticResume(fiber) ? withEvaluation(fiber, () => dispatchObserved(fiber, kernel, original, args)) : dispatchOutermost(fiber, kernel, original, args);
|
|
460
|
+
const dispatchEvaluate = (fiber, kernel, original, args) => isEvaluating(fiber) ? dispatchObserved(fiber, kernel, original, args) : dispatchResumption(fiber, kernel, original, args);
|
|
607
461
|
const interruptAgain = (fiber, args) => () => {
|
|
608
462
|
Reflect.apply(Reflect.get(fiber, "interruptUnsafe"), fiber, args);
|
|
609
463
|
};
|
|
@@ -889,6 +743,172 @@ const makeKernel = (options) => {
|
|
|
889
743
|
return kernel;
|
|
890
744
|
};
|
|
891
745
|
//#endregion
|
|
746
|
+
//#region src/internal/deadlock.ts
|
|
747
|
+
const NO_FRAMES = [];
|
|
748
|
+
const NO_RESOURCES = [];
|
|
749
|
+
const isHostObject$2 = (candidate) => typeof candidate === "object";
|
|
750
|
+
const isFrameList = (candidate) => Array.isArray(candidate);
|
|
751
|
+
const isHostProcess = (candidate) => isHostObject$2(candidate);
|
|
752
|
+
const hostProcess = () => Reflect.get(globalThis, "process");
|
|
753
|
+
const resourceNames = () => {
|
|
754
|
+
const host = hostProcess();
|
|
755
|
+
return isHostProcess(host) ? host.getActiveResourcesInfo() : NO_RESOURCES;
|
|
756
|
+
};
|
|
757
|
+
const bump = (counts, resource) => {
|
|
758
|
+
counts.set(resource, (counts.get(resource) ?? 0) + 1);
|
|
759
|
+
};
|
|
760
|
+
/** A snapshot of the process resources alive right now, by resource kind. */
|
|
761
|
+
/** @internal */
|
|
762
|
+
const resourceCounts = () => {
|
|
763
|
+
const counts = /* @__PURE__ */ new Map();
|
|
764
|
+
for (const resource of resourceNames()) bump(counts, resource);
|
|
765
|
+
return counts;
|
|
766
|
+
};
|
|
767
|
+
const extraOf = (before, entry) => entry[1] - (before.get(entry[0]) ?? 0);
|
|
768
|
+
const appendExtra = (appeared, before, entry) => {
|
|
769
|
+
const extra = extraOf(before, entry);
|
|
770
|
+
if (extra > 0) appeared.push(`${entry[0]} x${extra}`);
|
|
771
|
+
};
|
|
772
|
+
/** Resource kinds that appeared since `before`, as `"<kind> x<count>"` entries. */
|
|
773
|
+
/** @internal */
|
|
774
|
+
const newResources = (before) => {
|
|
775
|
+
const appeared = [];
|
|
776
|
+
for (const entry of resourceCounts()) appendExtra(appeared, before, entry);
|
|
777
|
+
return appeared;
|
|
778
|
+
};
|
|
779
|
+
const FAMILY_PATTERNS = [
|
|
780
|
+
[/Socket|TCP|UDP|Pipe|GetAddrInfo/u, "Socket"],
|
|
781
|
+
[/FS|File/u, "File"],
|
|
782
|
+
[/Timeout|Immediate/u, "RealTimer"]
|
|
783
|
+
];
|
|
784
|
+
const matchedFamily = (resource) => {
|
|
785
|
+
return FAMILY_PATTERNS.find(([pattern]) => pattern.test(resource))?.[1];
|
|
786
|
+
};
|
|
787
|
+
const familyOf = (resource) => matchedFamily(resource) ?? "Other";
|
|
788
|
+
const isNamed = (family) => family !== "Other";
|
|
789
|
+
/**
|
|
790
|
+
* What a set of newly appeared resources can wake. Files and sockets name
|
|
791
|
+
* themselves first; a leftover kind is still named, through `resources`.
|
|
792
|
+
*/
|
|
793
|
+
/** @internal */
|
|
794
|
+
const waitKindOf = (resources) => {
|
|
795
|
+
return resources.map(familyOf).find(isNamed) ?? "Other";
|
|
796
|
+
};
|
|
797
|
+
const stringOn = (described, key) => {
|
|
798
|
+
const value = Reflect.get(described, key);
|
|
799
|
+
return typeof value === "string" ? value : void 0;
|
|
800
|
+
};
|
|
801
|
+
const stringFieldOf = (described, key) => {
|
|
802
|
+
if (!isHostObject$2(described)) return void 0;
|
|
803
|
+
return stringOn(described, key);
|
|
804
|
+
};
|
|
805
|
+
const argsNameOn = (described) => {
|
|
806
|
+
const args = Reflect.get(described, "args");
|
|
807
|
+
return typeof args === "function" ? args.name : void 0;
|
|
808
|
+
};
|
|
809
|
+
const nameOf = (described) => {
|
|
810
|
+
if (!isHostObject$2(described)) return void 0;
|
|
811
|
+
return argsNameOn(described);
|
|
812
|
+
};
|
|
813
|
+
const applyToJson = (frame) => {
|
|
814
|
+
const toJson = Reflect.get(frame, "toJSON");
|
|
815
|
+
return typeof toJson === "function" ? Reflect.apply(toJson, frame, []) : void 0;
|
|
816
|
+
};
|
|
817
|
+
const describeFrame = (frame) => {
|
|
818
|
+
if (!isHostObject$2(frame)) return void 0;
|
|
819
|
+
return applyToJson(frame);
|
|
820
|
+
};
|
|
821
|
+
const namedOp = (op, described) => {
|
|
822
|
+
const name = nameOf(described);
|
|
823
|
+
return name === void 0 ? op : `${op}(${name})`;
|
|
824
|
+
};
|
|
825
|
+
const renderFrame = (frame) => {
|
|
826
|
+
const described = describeFrame(frame);
|
|
827
|
+
const op = stringFieldOf(described, "op") ?? "?";
|
|
828
|
+
return namedOp(op, described);
|
|
829
|
+
};
|
|
830
|
+
const renderedFrames = (stack) => stack.slice(1, 7).map(renderFrame);
|
|
831
|
+
const framesOf = (fiber) => {
|
|
832
|
+
const stack = Reflect.get(fiber, "_stack");
|
|
833
|
+
return isFrameList(stack) ? renderedFrames(stack) : NO_FRAMES;
|
|
834
|
+
};
|
|
835
|
+
const isUnfinished = (fiber) => fiber.pollUnsafe() === void 0;
|
|
836
|
+
const collectSuspended = (suspended, fiber) => {
|
|
837
|
+
if (isUnfinished(fiber)) suspended.push({
|
|
838
|
+
id: fiber.id,
|
|
839
|
+
frames: framesOf(fiber)
|
|
840
|
+
});
|
|
841
|
+
};
|
|
842
|
+
/**
|
|
843
|
+
* Every unfinished fiber the kernel ran, innermost frame first. Fibers that
|
|
844
|
+
* already exited are not suspended, so they are left out.
|
|
845
|
+
*/
|
|
846
|
+
/** @internal */
|
|
847
|
+
const describeSuspended = (fibers) => {
|
|
848
|
+
const suspended = [];
|
|
849
|
+
for (const fiber of fibers) collectSuspended(suspended, fiber);
|
|
850
|
+
return suspended;
|
|
851
|
+
};
|
|
852
|
+
//#endregion
|
|
853
|
+
//#region src/internal/escapeRecorder.ts
|
|
854
|
+
const TIMER_NAMES = [
|
|
855
|
+
"setTimeout",
|
|
856
|
+
"setInterval",
|
|
857
|
+
"setImmediate"
|
|
858
|
+
];
|
|
859
|
+
const OWN_MODULE = /effect-sim-kernel[/\\](?:src|dist)[/\\]/u;
|
|
860
|
+
const BUNDLED_DEPENDENCY = /.*node_modules[/\\]\.pnpm[/\\][^/\\]+[/\\]node_modules[/\\]/u;
|
|
861
|
+
const stackFrames = () => ((/* @__PURE__ */ new Error()).stack ?? "").split("\n").slice(2);
|
|
862
|
+
const outsideFrames = () => stackFrames().filter((frame) => !OWN_MODULE.test(frame));
|
|
863
|
+
const shorten = (frame) => frame.trim().replace(/^at /u, "").replace(BUNDLED_DEPENDENCY, "");
|
|
864
|
+
const siteOf = () => outsideFrames().slice(0, 2).map(shorten).join(" <- ");
|
|
865
|
+
const isTimer = (candidate) => typeof candidate === "function";
|
|
866
|
+
const timerOf = (name) => {
|
|
867
|
+
const original = Reflect.get(globalThis, name);
|
|
868
|
+
return isTimer(original) ? original : void 0;
|
|
869
|
+
};
|
|
870
|
+
const wrapperOf = (name, original, report) => (...args) => {
|
|
871
|
+
report({
|
|
872
|
+
timer: name,
|
|
873
|
+
site: siteOf()
|
|
874
|
+
});
|
|
875
|
+
return Reflect.apply(original, globalThis, args);
|
|
876
|
+
};
|
|
877
|
+
const captureTimer = (originals, name) => {
|
|
878
|
+
const original = timerOf(name);
|
|
879
|
+
if (original !== void 0) originals.set(name, original);
|
|
880
|
+
};
|
|
881
|
+
const capturedTimers = () => {
|
|
882
|
+
const originals = /* @__PURE__ */ new Map();
|
|
883
|
+
for (const name of TIMER_NAMES) captureTimer(originals, name);
|
|
884
|
+
return originals;
|
|
885
|
+
};
|
|
886
|
+
const reinstall = (originals) => {
|
|
887
|
+
for (const [name, original] of originals) Reflect.set(globalThis, name, original);
|
|
888
|
+
};
|
|
889
|
+
/**
|
|
890
|
+
* Wrap the global timer functions so every call made while the run is live is
|
|
891
|
+
* reported, then still carried out so the run can be classified. Returns the
|
|
892
|
+
* restore function and the untouched host timer the kernel's own machinery
|
|
893
|
+
* uses to yield to completed host work; a run installs the recorder at its
|
|
894
|
+
* start and restores it in its `finally`, so nothing outside a run is ever
|
|
895
|
+
* observed.
|
|
896
|
+
*/
|
|
897
|
+
/** @internal */
|
|
898
|
+
const installEscapeRecorder = (report) => {
|
|
899
|
+
const originals = capturedTimers();
|
|
900
|
+
for (const [name, original] of originals) Reflect.set(globalThis, name, wrapperOf(name, original, report));
|
|
901
|
+
return {
|
|
902
|
+
restore: () => reinstall(originals),
|
|
903
|
+
hostImmediate: hostImmediateOf(originals)
|
|
904
|
+
};
|
|
905
|
+
};
|
|
906
|
+
const hostImmediateOf = (originals) => {
|
|
907
|
+
const immediate = originals.get("setImmediate");
|
|
908
|
+
return immediate === void 0 ? syncImmediate : immediate;
|
|
909
|
+
};
|
|
910
|
+
const syncImmediate = (handler) => handler();
|
|
911
|
+
//#endregion
|
|
892
912
|
//#region src/internal/runQueue.ts
|
|
893
913
|
let tail = Promise.resolve();
|
|
894
914
|
/** @internal */
|
|
@@ -930,15 +950,6 @@ const DEFAULT_MAX_STEPS = 5e4;
|
|
|
930
950
|
const DEFAULT_STEP_TURNS = 4;
|
|
931
951
|
const DEFAULT_QUIESCENCE_TURNS = 1e3;
|
|
932
952
|
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
953
|
/** One microtask checkpoint: already-queued callbacks run before this resumes. */
|
|
943
954
|
const turn = (value) => Promise.resolve().then(() => value);
|
|
944
955
|
const drain = (turns) => turns <= 0 ? Promise.resolve() : turn(void 0).then(() => drain(turns - 1));
|
|
@@ -1154,19 +1165,56 @@ const queuedRun = (program, options, release) => {
|
|
|
1154
1165
|
return Promise.reject(error);
|
|
1155
1166
|
}
|
|
1156
1167
|
};
|
|
1157
|
-
const
|
|
1168
|
+
const runImpl = (program, options = {}) => {
|
|
1158
1169
|
rejectAwaitedExploration(options);
|
|
1159
1170
|
return acquireRun().then((release) => queuedRun(program, options, release));
|
|
1160
1171
|
};
|
|
1172
|
+
/**
|
|
1173
|
+
* Runs one Effect program under one kernel (R2: every clock the program can
|
|
1174
|
+
* reach is the kernel's virtual root clock; R37: at quiescence the kernel
|
|
1175
|
+
* settles its test clocks, then advances the root clock). Driven by the
|
|
1176
|
+
* decision path or chooser in `options`, it returns the exit, the decisions
|
|
1177
|
+
* taken, and a per-step record of which fiber ran. A run started while one is
|
|
1178
|
+
* active queues behind it, so two runs never share the global hooks.
|
|
1179
|
+
*/
|
|
1180
|
+
/** @internal */
|
|
1181
|
+
const run = dual((args) => args.length > 0 && isProgram(args[0]), runImpl);
|
|
1161
1182
|
//#endregion
|
|
1162
|
-
//#region src/Kernel/
|
|
1163
|
-
const
|
|
1183
|
+
//#region src/Kernel/Profile.ts
|
|
1184
|
+
const pctDepth = PctDepth.make(3);
|
|
1185
|
+
const isProfileName = Schema.is(ProfileNames);
|
|
1186
|
+
const profileOf = (name) => isProfileName(name) ? name : "per-change";
|
|
1187
|
+
const currentProfile = Effect.map(Config.option(Config.String("CONFORMANCE_PROFILE")), Option.match({
|
|
1188
|
+
onNone: () => profileOf(""),
|
|
1189
|
+
onSome: profileOf
|
|
1190
|
+
}));
|
|
1191
|
+
const SEEDS = {
|
|
1192
|
+
local: 25,
|
|
1193
|
+
"per-change": 250
|
|
1194
|
+
};
|
|
1164
1195
|
/**
|
|
1165
|
-
* Whether the
|
|
1166
|
-
*
|
|
1167
|
-
*
|
|
1196
|
+
* Whether the explored part of a run ever had more than one runnable task to
|
|
1197
|
+
* pick from — the running fiber's own continuation counts, since a sliced
|
|
1198
|
+
* fiber waits in the pending list like any other task. A run without a choice
|
|
1199
|
+
* point has exactly one schedule: a seeded replay can only take the decisions
|
|
1200
|
+
* the run already took. Steps before `beginExploration` (a case's layer build)
|
|
1201
|
+
* are never explored, so they do not count.
|
|
1168
1202
|
*/
|
|
1169
|
-
const
|
|
1203
|
+
const hasChoicePoint = (history) => history.steps.slice(history.steps.length - history.decisions.length).some((step) => step.options > 1);
|
|
1204
|
+
const seedsWhen = (choicePoint, profile) => choicePoint ? SEEDS[profile] : 0;
|
|
1205
|
+
/**
|
|
1206
|
+
* The seeded replays a baseline run is worth under a profile. A baseline with
|
|
1207
|
+
* no choice point has one schedule, so a seed could only replay it: it gets
|
|
1208
|
+
* none. Any other baseline gets the profile's seed count.
|
|
1209
|
+
*/
|
|
1210
|
+
const seedsForImpl = (baseline, profile = "per-change") => seedsWhen(hasChoicePoint(baseline), profile);
|
|
1211
|
+
const isBaselineFirst = (args) => typeof args[0] === "object" && args[0] !== null;
|
|
1212
|
+
const seedsFor = dual(isBaselineFirst, seedsForImpl);
|
|
1213
|
+
const currentSeedsFor = (baseline) => Effect.map(currentProfile, (profile) => seedsFor(baseline, profile));
|
|
1214
|
+
const localBoundOf = (requested) => Math.min(requested ?? 1, 1);
|
|
1215
|
+
const boundImpl = (requested, profile) => profile === "local" ? localBoundOf(requested) : requested;
|
|
1216
|
+
const preemptionsFor = dual(2, boundImpl);
|
|
1217
|
+
const currentPreemptionsFor = (requested) => Effect.map(currentProfile, (profile) => preemptionsFor(requested, profile));
|
|
1170
1218
|
//#endregion
|
|
1171
1219
|
//#region src/Kernel/Pct.ts
|
|
1172
1220
|
/**
|
|
@@ -1175,7 +1223,8 @@ const isStepping = () => currentKernel()?.phase === "step";
|
|
|
1175
1223
|
* points among the first `steps` explored decisions lower the running fiber's
|
|
1176
1224
|
* priority. A seed replays the whole schedule deterministically, and a
|
|
1177
1225
|
* parent-owned child-start task is targeted positionally so a newly started
|
|
1178
|
-
* child still gets its own chance to run.
|
|
1226
|
+
* child still gets its own chance to run. A baseline with no choice point has
|
|
1227
|
+
* one schedule, so unless the caller names a seed count it gets no seeds.
|
|
1179
1228
|
*/
|
|
1180
1229
|
const mulberry32 = (seed) => {
|
|
1181
1230
|
let state = seed | 0;
|
|
@@ -1231,7 +1280,7 @@ const applyChange = (picker, choice, best) => {
|
|
|
1231
1280
|
};
|
|
1232
1281
|
const pick = (options) => {
|
|
1233
1282
|
const rng = mulberry32(options.seed);
|
|
1234
|
-
const depth = options.depth ??
|
|
1283
|
+
const depth = options.depth ?? pctDepth;
|
|
1235
1284
|
const picker = {
|
|
1236
1285
|
priorities: /* @__PURE__ */ new Map(),
|
|
1237
1286
|
changes: changePoints(rng, depth, options.steps),
|
|
@@ -1295,23 +1344,29 @@ const exploreSeeds = (state, seed) => {
|
|
|
1295
1344
|
};
|
|
1296
1345
|
const absorbBaseline = (state, baseline) => {
|
|
1297
1346
|
const survey = surveyed(baseline, state.survey);
|
|
1298
|
-
state.
|
|
1347
|
+
state.seeds = state.requestedSeeds ?? seedsFor(baseline, "per-change");
|
|
1348
|
+
state.steps = PctStepCount.make(survey.steps);
|
|
1299
1349
|
state.survey.fibers = survey.fibers;
|
|
1300
1350
|
state.survey.steps = survey.steps;
|
|
1301
1351
|
};
|
|
1352
|
+
const derivedChoiceFree = (state) => state.requestedSeeds === void 0 && state.seeds === 0;
|
|
1353
|
+
const judgedAlone = (state, baseline) => {
|
|
1354
|
+
if (derivedChoiceFree(state)) collectedFailure(state, 0, baseline);
|
|
1355
|
+
};
|
|
1302
1356
|
const baselineOptions = (state) => {
|
|
1303
1357
|
if (state.maxSteps === void 0) return {};
|
|
1304
1358
|
return { maxSteps: state.maxSteps };
|
|
1305
1359
|
};
|
|
1306
|
-
const
|
|
1307
|
-
const
|
|
1308
|
-
const stepsOf = (options) => options.steps ??
|
|
1360
|
+
const depthOf = (options) => options.depth ?? pctDepth;
|
|
1361
|
+
const NO_STEPS = PctStepCount.make(0);
|
|
1362
|
+
const stepsOf = (options) => options.steps ?? NO_STEPS;
|
|
1309
1363
|
const failureCheckOf = (options) => options.isFailure ?? failKept$2;
|
|
1310
1364
|
const pctImpl = (program, options = {}) => {
|
|
1311
1365
|
const state = {
|
|
1312
1366
|
program,
|
|
1313
1367
|
depth: depthOf(options),
|
|
1314
|
-
|
|
1368
|
+
requestedSeeds: options.seeds,
|
|
1369
|
+
seeds: 0,
|
|
1315
1370
|
steps: stepsOf(options),
|
|
1316
1371
|
isFailure: failureCheckOf(options),
|
|
1317
1372
|
maxSteps: options.maxSteps,
|
|
@@ -1323,6 +1378,7 @@ const pctImpl = (program, options = {}) => {
|
|
|
1323
1378
|
};
|
|
1324
1379
|
return run(program, baselineOptions(state)).then((baseline) => {
|
|
1325
1380
|
absorbBaseline(state, baseline);
|
|
1381
|
+
judgedAlone(state, baseline);
|
|
1326
1382
|
return exploreSeeds(state, 0).then((failures) => ({
|
|
1327
1383
|
failures,
|
|
1328
1384
|
bound: boundOf$1(state.survey, state.depth, state.seeds + 1)
|
|
@@ -1873,13 +1929,22 @@ var TestClock_exports = /* @__PURE__ */ __exportAll({
|
|
|
1873
1929
|
* settles its requests only when nothing else can run. Only available inside a
|
|
1874
1930
|
* kernel run.
|
|
1875
1931
|
*/
|
|
1876
|
-
const make =
|
|
1932
|
+
const make = Effect.sync(() => {
|
|
1933
|
+
const kernel = currentKernel();
|
|
1934
|
+
if (kernel === void 0) throw new Error("effect-sim-kernel: the kernel test clock is only available inside a kernel run");
|
|
1935
|
+
const testClock = makeTestClock(kernel.clocks.clock);
|
|
1936
|
+
kernel.clocks.testClocks.push(testClock);
|
|
1937
|
+
return testClock;
|
|
1938
|
+
});
|
|
1877
1939
|
const contextOf = (testClock) => Context.makeUnsafe(/* @__PURE__ */ new Map([[Clock.Clock.key, testClock]]));
|
|
1878
1940
|
/** The layer a harness provides in place of `TestClock.layer()`. */
|
|
1879
1941
|
const layer = Layer.effectContext(Effect.map(make, contextOf));
|
|
1880
1942
|
//#endregion
|
|
1881
1943
|
//#region src/Kernel/mod.ts
|
|
1882
1944
|
var Kernel_exports = /* @__PURE__ */ __exportAll({
|
|
1945
|
+
PctDepth: () => PctDepth,
|
|
1946
|
+
PctSeedCount: () => PctSeedCount,
|
|
1947
|
+
PctStepCount: () => PctStepCount,
|
|
1883
1948
|
TestClock: () => TestClock_exports,
|
|
1884
1949
|
beginExploration: () => beginExploration,
|
|
1885
1950
|
currentPreemptionsFor: () => currentPreemptionsFor,
|
|
@@ -1890,7 +1955,7 @@ var Kernel_exports = /* @__PURE__ */ __exportAll({
|
|
|
1890
1955
|
localSeeds: () => 25,
|
|
1891
1956
|
outcomeTags: () => runOutcomeTags,
|
|
1892
1957
|
pct: () => pct,
|
|
1893
|
-
pctDepth: () =>
|
|
1958
|
+
pctDepth: () => pctDepth,
|
|
1894
1959
|
perChangeSeeds: () => 250,
|
|
1895
1960
|
pick: () => pick,
|
|
1896
1961
|
preemptionsFor: () => preemptionsFor,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@systemfsoftware/effect-sim-kernel",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"author": "Ryan Lee <drdgvhbh@gmail.com>",
|
|
6
6
|
"repository": {
|
|
@@ -32,22 +32,22 @@
|
|
|
32
32
|
"./package.json": "./package.json"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@microsoft/api-extractor": "^7.59.
|
|
36
|
-
"@systemfsoftware/oxlint-config-recommended": "^
|
|
37
|
-
"@systemfsoftware/tsconfig": "^2.0
|
|
38
|
-
"@systemfsoftware/tsdown-config": "^0.1.
|
|
39
|
-
"@systemfsoftware/vitest": "^
|
|
40
|
-
"@systemfsoftware/vitest-config": "^0.1.
|
|
41
|
-
"@types/node": "^26",
|
|
42
|
-
"effect": "4.
|
|
43
|
-
"oxlint": "~1.
|
|
35
|
+
"@microsoft/api-extractor": "^7.59.4",
|
|
36
|
+
"@systemfsoftware/oxlint-config-recommended": "^6.0.1",
|
|
37
|
+
"@systemfsoftware/tsconfig": "^2.1.0",
|
|
38
|
+
"@systemfsoftware/tsdown-config": "^0.1.2",
|
|
39
|
+
"@systemfsoftware/vitest": "^4.1.1",
|
|
40
|
+
"@systemfsoftware/vitest-config": "^0.1.3",
|
|
41
|
+
"@types/node": "^26.6.5",
|
|
42
|
+
"effect": "^4.1.2",
|
|
43
|
+
"oxlint": "~1.89.0",
|
|
44
44
|
"rimraf": "^6.1.3",
|
|
45
45
|
"tsdown": "^0.23.0",
|
|
46
|
-
"typescript": "^7",
|
|
47
|
-
"vitest": "^5"
|
|
46
|
+
"typescript": "^7.0.2",
|
|
47
|
+
"vitest": "^5.1.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"effect": "4.
|
|
50
|
+
"effect": "^4.1.2"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|
|
53
53
|
"access": "public",
|