@swirls/looms 0.11.3 → 0.12.1
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 +24 -4
- package/dist/actor/routing.d.ts +2 -2
- package/dist/actor/routing.js +9 -3
- package/dist/client/client.d.ts +352 -1
- package/dist/client/client.js +28 -12
- package/dist/debugger/app/assets/{index-D13zm4x1.js → index-B_KJqkH1.js} +2 -2
- package/dist/debugger/app/assets/{routes-BrDGfYv0.js → routes-oKOihm_o.js} +2 -2
- package/dist/debugger/app/index.html +1 -1
- package/dist/runtime/looms.d.ts +9 -1
- package/dist/runtime/looms.js +13 -0
- package/dist/runtime/run-execution-context.d.ts +5 -1
- package/dist/runtime/run-execution-context.js +13 -0
- package/dist/runtime/runtime.js +2 -2
- package/dist/runtime/server/fetch-handler.js +2 -1
- package/dist/runtime/server/routes/runs.js +38 -18
- package/dist/runtime/wake-engine.d.ts +1 -1
- package/dist/runtime/wake-engine.js +149 -80
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Looms debugger</title>
|
|
7
|
-
<script type="module" crossorigin src="./assets/index-
|
|
7
|
+
<script type="module" crossorigin src="./assets/index-B_KJqkH1.js"></script>
|
|
8
8
|
<link rel="modulepreload" crossorigin href="./assets/rolldown-runtime-CbXtAM7H.js">
|
|
9
9
|
<link rel="stylesheet" crossorigin href="./assets/index-CO5gIvV8.css">
|
|
10
10
|
</head>
|
package/dist/runtime/looms.d.ts
CHANGED
|
@@ -93,9 +93,17 @@ export interface Looms<Fields extends RunAttributeFields = RunAttributeFields> {
|
|
|
93
93
|
}>;
|
|
94
94
|
/** Start a run from a definition object; the input type follows the definition's schema. */
|
|
95
95
|
start<TDef extends DefinitionRef>(definition: TDef, ...args: LoomsStartArgs<TDef, Fields>): Promise<StartResult>;
|
|
96
|
+
/**
|
|
97
|
+
* Like `start`, but answers once the start is committed and leaves the first wake to the
|
|
98
|
+
* scheduler.
|
|
99
|
+
*/
|
|
100
|
+
launch<TDef extends DefinitionRef>(definition: TDef, ...args: LoomsStartArgs<TDef, Fields>): Promise<StartResult>;
|
|
96
101
|
/** Start a run by `{ kind, definitionName }` when you only have names (HTTP bodies, CLIs). */
|
|
97
102
|
startRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>;
|
|
98
|
-
/**
|
|
103
|
+
/**
|
|
104
|
+
* Like `startRun`, but answers once the start is committed and leaves the first wake to the
|
|
105
|
+
* scheduler.
|
|
106
|
+
*/
|
|
99
107
|
launchRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>;
|
|
100
108
|
getRun(runId: string): Promise<RunState>;
|
|
101
109
|
getEvents(runId: string, options?: {
|
package/dist/runtime/looms.js
CHANGED
|
@@ -101,6 +101,19 @@ export function createLooms(options = {}) {
|
|
|
101
101
|
secrets: startOpts?.secrets,
|
|
102
102
|
trace: startOpts?.trace,
|
|
103
103
|
})),
|
|
104
|
+
launch: (definition, input, startOpts) => runEffect((i) => i.runtime.launchRun({
|
|
105
|
+
kind: definition.kind,
|
|
106
|
+
definitionName: definition.name,
|
|
107
|
+
definitionVersion: definition.version,
|
|
108
|
+
// SAFETY: input is validated against the definition's schema inside launchRun.
|
|
109
|
+
input: input,
|
|
110
|
+
runId: startOpts?.runId,
|
|
111
|
+
threadId: startOpts?.threadId,
|
|
112
|
+
idempotencyKey: startOpts?.idempotencyKey,
|
|
113
|
+
attributes: startOpts?.attributes,
|
|
114
|
+
secrets: startOpts?.secrets,
|
|
115
|
+
trace: startOpts?.trace,
|
|
116
|
+
})),
|
|
104
117
|
getRun: (runId) => runEffect((i) => i.runtime.getRun(runId)),
|
|
105
118
|
getEvents: (runId, opts) => runEffect((i) => i.runtime.getEvents(runId, opts)),
|
|
106
119
|
signal: (runId, events, signalOpts) => runEffect((i) => i.runtime.signal(runId, events, signalOpts)),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { Deferred, type Exit } from 'effect';
|
|
2
2
|
import type { EventStore, RunState } from '@swirls/looms/core';
|
|
3
3
|
import type { WakeError } from './types';
|
|
4
4
|
export type WakeCompletion = Exit.Exit<RunState, WakeError>;
|
|
@@ -14,12 +14,16 @@ export interface RunExecutionContext {
|
|
|
14
14
|
readonly activeEffects: Map<string, ActiveEffect>;
|
|
15
15
|
waking: boolean;
|
|
16
16
|
wakeAgain: boolean;
|
|
17
|
+
/** Resolved when a joined wake or coalesced signal asks the owner to replan mid-flight. */
|
|
18
|
+
wakeSignal: Deferred.Deferred<void> | null;
|
|
17
19
|
wakeCompletion: Promise<WakeCompletion> | null;
|
|
18
20
|
resolveWakeCompletion: ((completion: WakeCompletion) => void) | null;
|
|
19
21
|
ingress: RunIngressState | null;
|
|
20
22
|
liveStore: EventStore | undefined;
|
|
21
23
|
liveCount: number;
|
|
22
24
|
}
|
|
25
|
+
/** Mark the run for another wake pass and unblock any mid-flight wait on `wakeSignal`. */
|
|
26
|
+
export declare function requestWakeAgain(context: RunExecutionContext): void;
|
|
23
27
|
export declare class RunExecutionContexts {
|
|
24
28
|
private readonly runs;
|
|
25
29
|
get(runId: string): RunExecutionContext;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { Deferred, Effect } from 'effect';
|
|
1
2
|
import { RuntimeExecutionError } from './errors.js';
|
|
2
3
|
function createRunExecutionContext() {
|
|
3
4
|
return {
|
|
4
5
|
activeEffects: new Map(),
|
|
5
6
|
waking: false,
|
|
6
7
|
wakeAgain: false,
|
|
8
|
+
wakeSignal: null,
|
|
7
9
|
wakeCompletion: null,
|
|
8
10
|
resolveWakeCompletion: null,
|
|
9
11
|
ingress: null,
|
|
@@ -11,9 +13,20 @@ function createRunExecutionContext() {
|
|
|
11
13
|
liveCount: 0,
|
|
12
14
|
};
|
|
13
15
|
}
|
|
16
|
+
/** Mark the run for another wake pass and unblock any mid-flight wait on `wakeSignal`. */
|
|
17
|
+
export function requestWakeAgain(context) {
|
|
18
|
+
context.wakeAgain = true;
|
|
19
|
+
const signal = context.wakeSignal;
|
|
20
|
+
if (!signal) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
context.wakeSignal = null;
|
|
24
|
+
Deferred.doneUnsafe(signal, Effect.void);
|
|
25
|
+
}
|
|
14
26
|
function isIdle(context) {
|
|
15
27
|
return (!context.waking &&
|
|
16
28
|
!context.wakeAgain &&
|
|
29
|
+
context.wakeSignal === null &&
|
|
17
30
|
context.wakeCompletion === null &&
|
|
18
31
|
context.resolveWakeCompletion === null &&
|
|
19
32
|
context.ingress === null &&
|
package/dist/runtime/runtime.js
CHANGED
|
@@ -7,7 +7,7 @@ import { admitExternalEvents, moduleServices, resolveSnapshotStore, RunCursorCac
|
|
|
7
7
|
import { createRunIngress } from './ingress.js';
|
|
8
8
|
import { notifyObserver } from './observer.js';
|
|
9
9
|
import { createRuntimeRepository } from './repository.js';
|
|
10
|
-
import { RunExecutionContexts } from './run-execution-context.js';
|
|
10
|
+
import { requestWakeAgain, RunExecutionContexts } from './run-execution-context.js';
|
|
11
11
|
import { assertSameDurableStart } from './run-start.js';
|
|
12
12
|
import { createWakeEngine } from './wake-engine.js';
|
|
13
13
|
import { createTimeoutScheduler } from './wake-scheduler.js';
|
|
@@ -277,7 +277,7 @@ export function createRuntime(options) {
|
|
|
277
277
|
});
|
|
278
278
|
const appendAndWake = (runId, events, signalOpts) => withRunIngress(runId, signalAdmitted(runId, events, signalOpts)).pipe(Effect.flatMap((state) => {
|
|
279
279
|
if (signalOpts?.coalesce && executionContexts.get(runId).waking) {
|
|
280
|
-
executionContexts.get(runId)
|
|
280
|
+
requestWakeAgain(executionContexts.get(runId));
|
|
281
281
|
return Effect.succeed(state);
|
|
282
282
|
}
|
|
283
283
|
return runtime.wake(runId);
|
|
@@ -23,6 +23,7 @@ const StartAttributesSchema = Schema.Struct({
|
|
|
23
23
|
class UnreadableStartBody extends Data.TaggedError('UnreadableStartBody') {
|
|
24
24
|
}
|
|
25
25
|
const noAttributes = {};
|
|
26
|
+
const START_ROUTES = new Set(['runs.start', 'runs.launch']);
|
|
26
27
|
function readRequestAttributes(req) {
|
|
27
28
|
return Effect.tryPromise({
|
|
28
29
|
try: () => req.clone().json(),
|
|
@@ -42,7 +43,7 @@ function authorizeRoute(req, matched, authorize, runtime, store) {
|
|
|
42
43
|
const stored = Predicate.isString(runId)
|
|
43
44
|
? yield* readStoredAttributes(runtime, store, runId)
|
|
44
45
|
: noAttributes;
|
|
45
|
-
const requested =
|
|
46
|
+
const requested = START_ROUTES.has(name) ? yield* readRequestAttributes(req) : noAttributes;
|
|
46
47
|
const attributes = hasEntries(stored) ? stored : requested;
|
|
47
48
|
const route = Predicate.isString(runId)
|
|
48
49
|
? hasEntries(attributes)
|
|
@@ -32,6 +32,27 @@ const CancelBodySchema = Schema.Struct({
|
|
|
32
32
|
const ForkBodySchema = Schema.Struct({
|
|
33
33
|
runId: Schema.optional(Schema.String),
|
|
34
34
|
});
|
|
35
|
+
function readStartRunArgs(req) {
|
|
36
|
+
return Effect.gen(function* () {
|
|
37
|
+
const body = yield* readJson(req).pipe(Effect.flatMap(Schema.decodeUnknownEffect(StartRunBodySchema)));
|
|
38
|
+
const definitionName = body.definitionName ?? body.name;
|
|
39
|
+
const kind = body.kind;
|
|
40
|
+
if (!definitionName || !kind) {
|
|
41
|
+
return Response.json({ error: 'kind and definitionName required' }, { status: 400 });
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
kind,
|
|
45
|
+
definitionName,
|
|
46
|
+
definitionVersion: body.definitionVersion,
|
|
47
|
+
input: body.input ?? null,
|
|
48
|
+
runId: body.runId ?? createRunId(),
|
|
49
|
+
idempotencyKey: extractIdempotencyKey(req, body.idempotencyKey),
|
|
50
|
+
attributes: body.attributes,
|
|
51
|
+
secrets: body.secrets,
|
|
52
|
+
trace: readTraceHeaders(req.headers),
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
}
|
|
35
56
|
function wantsRunStream(req, url) {
|
|
36
57
|
return url.searchParams.get('stream') === 'true' || acceptsEventStream(req);
|
|
37
58
|
}
|
|
@@ -94,25 +115,10 @@ export function runRoutes({ runtime, store }) {
|
|
|
94
115
|
name: 'runs.start',
|
|
95
116
|
access: 'write',
|
|
96
117
|
handle: ({ req, url }) => Effect.gen(function* () {
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (!definitionName || !kind) {
|
|
101
|
-
return Response.json({ error: 'kind and definitionName required' }, { status: 400 });
|
|
118
|
+
const args = yield* readStartRunArgs(req);
|
|
119
|
+
if (args instanceof Response) {
|
|
120
|
+
return args;
|
|
102
121
|
}
|
|
103
|
-
const runId = body.runId ?? createRunId();
|
|
104
|
-
const idempotencyKey = extractIdempotencyKey(req, body.idempotencyKey);
|
|
105
|
-
const args = {
|
|
106
|
-
kind,
|
|
107
|
-
definitionName,
|
|
108
|
-
definitionVersion: body.definitionVersion,
|
|
109
|
-
input: body.input ?? null,
|
|
110
|
-
runId,
|
|
111
|
-
idempotencyKey,
|
|
112
|
-
attributes: body.attributes,
|
|
113
|
-
secrets: body.secrets,
|
|
114
|
-
trace: readTraceHeaders(req.headers),
|
|
115
|
-
};
|
|
116
122
|
if (wantsRunStream(req, url)) {
|
|
117
123
|
return streamStartRun(req, runtime, store, args);
|
|
118
124
|
}
|
|
@@ -120,6 +126,20 @@ export function runRoutes({ runtime, store }) {
|
|
|
120
126
|
return Response.json(result);
|
|
121
127
|
}),
|
|
122
128
|
}),
|
|
129
|
+
route({
|
|
130
|
+
method: 'POST',
|
|
131
|
+
path: '/runs/launch',
|
|
132
|
+
name: 'runs.launch',
|
|
133
|
+
access: 'write',
|
|
134
|
+
handle: ({ req }) => Effect.gen(function* () {
|
|
135
|
+
const args = yield* readStartRunArgs(req);
|
|
136
|
+
if (args instanceof Response) {
|
|
137
|
+
return args;
|
|
138
|
+
}
|
|
139
|
+
const result = yield* provideStore(runtime.launchRun(args), store);
|
|
140
|
+
return Response.json(result);
|
|
141
|
+
}),
|
|
142
|
+
}),
|
|
123
143
|
route({
|
|
124
144
|
method: 'GET',
|
|
125
145
|
path: '/runs/:runId/replay',
|
|
@@ -2,7 +2,7 @@ import { type Context, type ManagedRuntime } from 'effect';
|
|
|
2
2
|
import { type ComposedRegistry, type DefinitionResolver, type RunLauncher } from '@swirls/looms/core';
|
|
3
3
|
import { type RuntimeObserver } from './observer';
|
|
4
4
|
import type { RuntimeRepository } from './repository';
|
|
5
|
-
import type
|
|
5
|
+
import { type RunExecutionContexts } from './run-execution-context';
|
|
6
6
|
import type { LoomsRuntime, EffectWorker } from './types';
|
|
7
7
|
import type { WakeScheduler } from './wake-scheduler';
|
|
8
8
|
export interface WakeEngineOptions {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { Clock, Effect, Exit, Option } from 'effect';
|
|
1
|
+
import { Clock, Deferred, Effect, Exit, Fiber, Option, Queue, } from 'effect';
|
|
2
2
|
import { createEvent, deferredUntil, EventStoreTag, foldRun, isRunTerminal, } from '@swirls/looms/core';
|
|
3
3
|
import { childCloseCancellations, notifyLaunchingParent } from './child-run.js';
|
|
4
4
|
import { nextRuntimeDeadline, planDeadlineTransitions, planTerminalCancellationTransitions, } from './deadline-transition-planner.js';
|
|
5
5
|
import { admitOrFailAttempt } from './effect-admission.js';
|
|
6
6
|
import { DuplicateEffectDispatchError, MaxWakeIterationsError, RuntimeExecutionError, } from './errors.js';
|
|
7
|
-
import { cascadedCancellations, stripSeq, synthesizedThreadFailed, waitSatisfiedEvents, } from './helpers.js';
|
|
7
|
+
import { cascadedCancellations, groupOutstanding, stripSeq, synthesizedThreadFailed, waitSatisfiedEvents, } from './helpers.js';
|
|
8
8
|
import { executeLocalEffects } from './local-effect-executor.js';
|
|
9
9
|
import { notifyObserver } from './observer.js';
|
|
10
|
+
import { requestWakeAgain, } from './run-execution-context.js';
|
|
10
11
|
import { dispatchWorkerEffect, isWorkerEffect, planWorkerEffects } from './worker-effect-dispatch.js';
|
|
11
12
|
function currentTimeMillis() {
|
|
12
13
|
return Math.floor(performance.timeOrigin + performance.now());
|
|
@@ -52,8 +53,10 @@ export function createWakeEngine(options) {
|
|
|
52
53
|
const executionContext = executionContexts.get(runId);
|
|
53
54
|
// Claim synchronously before any yield so concurrent fibers cannot both pass.
|
|
54
55
|
if (executionContext.waking) {
|
|
55
|
-
|
|
56
|
+
// Capture before requestWakeAgain: resolving wakeSignal can let the owner park
|
|
57
|
+
// and clear wakeCompletion before this joiner awaits it.
|
|
56
58
|
const activeCompletion = executionContext.wakeCompletion;
|
|
59
|
+
requestWakeAgain(executionContext);
|
|
57
60
|
if (!activeCompletion) {
|
|
58
61
|
return yield* new RuntimeExecutionError(`Run "${runId}" has an active wake without completion state`);
|
|
59
62
|
}
|
|
@@ -75,6 +78,7 @@ export function createWakeEngine(options) {
|
|
|
75
78
|
released = true;
|
|
76
79
|
executionContext.waking = false;
|
|
77
80
|
executionContext.wakeAgain = false;
|
|
81
|
+
executionContext.wakeSignal = null;
|
|
78
82
|
executionContext.wakeCompletion = null;
|
|
79
83
|
executionContext.resolveWakeCompletion = null;
|
|
80
84
|
executionContext.liveStore = undefined;
|
|
@@ -131,14 +135,54 @@ export function createWakeEngine(options) {
|
|
|
131
135
|
executionContext.liveCount = 0;
|
|
132
136
|
const dispatchedEffectIds = new Set();
|
|
133
137
|
const startedEffectIds = new Set();
|
|
138
|
+
const inFlight = new Map();
|
|
139
|
+
const settled = yield* Queue.unbounded();
|
|
134
140
|
let guard = 0;
|
|
135
141
|
// Deadlines at or before this instant were handled by the last loop iteration.
|
|
136
142
|
let plannedAt = Number.NEGATIVE_INFINITY;
|
|
143
|
+
const foldProduced = (produced) => Effect.gen(function* () {
|
|
144
|
+
if (produced.length === 0) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
yield* liveAppends.drain(runId);
|
|
148
|
+
const beforeFold = cursor.state;
|
|
149
|
+
const intermediateState = foldRun(produced, registry, { runId, initial: beforeFold });
|
|
150
|
+
const synthesized = [
|
|
151
|
+
...synthesizedThreadFailed(beforeFold, intermediateState, produced),
|
|
152
|
+
...cascadedCancellations(intermediateState, produced),
|
|
153
|
+
];
|
|
154
|
+
const synthesizedEvents = synthesized.map((input) => createEvent(runId, input));
|
|
155
|
+
const stateAfterSynth = synthesizedEvents.length > 0
|
|
156
|
+
? foldRun(synthesizedEvents, registry, { runId, initial: intermediateState })
|
|
157
|
+
: intermediateState;
|
|
158
|
+
const allNewEvents = [...produced, ...synthesizedEvents];
|
|
159
|
+
const satisfied = waitSatisfiedEvents(stateAfterSynth, allNewEvents);
|
|
160
|
+
const satisfiedEvents = satisfied.map((input) => createEvent(runId, input));
|
|
161
|
+
const fullBatch = [...allNewEvents, ...satisfiedEvents];
|
|
162
|
+
const appendResult = yield* appendObserved(store, runId, stripSeq(fullBatch));
|
|
163
|
+
cursor = yield* applyAppend(store, runId, cursor, stripSeq(fullBatch), appendResult);
|
|
164
|
+
// Bound replay after a crash inside a long wake.
|
|
165
|
+
if (snapshotEvery > 0 && cursor.durableSinceSnapshot >= snapshotEvery) {
|
|
166
|
+
cursor = yield* persistSnapshot(store, runId, cursor);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
const attemptFor = (item) => {
|
|
170
|
+
const execution = cursor.state.effectExecutions[item.effectId];
|
|
171
|
+
return execution?.status === 'running' && startedEffectIds.has(item.effectId)
|
|
172
|
+
? execution.attempt
|
|
173
|
+
: (execution?.attempt ?? 0) + 1;
|
|
174
|
+
};
|
|
175
|
+
const dispatchKeyFor = (item) => `${item.effectId}:${attemptFor(item)}`;
|
|
137
176
|
while (!isRunTerminal(cursor.state)) {
|
|
138
177
|
if (guard >= maxWakeIterations) {
|
|
139
178
|
return yield* new MaxWakeIterationsError(runId, maxWakeIterations);
|
|
140
179
|
}
|
|
141
180
|
guard += 1;
|
|
181
|
+
// A signal (or coalesced join) may have appended events while local effects ran.
|
|
182
|
+
if (executionContext.wakeAgain) {
|
|
183
|
+
executionContext.wakeAgain = false;
|
|
184
|
+
cursor = yield* loadCursor(store, runId);
|
|
185
|
+
}
|
|
142
186
|
const state = cursor.state;
|
|
143
187
|
const now = yield* Clock.currentTimeMillis;
|
|
144
188
|
plannedAt = now;
|
|
@@ -229,93 +273,118 @@ export function createWakeEngine(options) {
|
|
|
229
273
|
execution.nextAttemptAt !== null &&
|
|
230
274
|
execution.nextAttemptAt > now));
|
|
231
275
|
});
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const attempt = execution?.status === 'running' && startedEffectIds.has(item.effectId)
|
|
238
|
-
? execution.attempt
|
|
239
|
-
: (execution?.attempt ?? 0) + 1;
|
|
240
|
-
return [item.effectId, attempt];
|
|
241
|
-
}));
|
|
276
|
+
// Effects still running in a forked fiber are skipped so a mid-flight signal can
|
|
277
|
+
// plan other threads. Effects that were dispatched, settled with events, and remain
|
|
278
|
+
// outstanding (e.g. a handler that never completed its own effectId) still trip the
|
|
279
|
+
// duplicate guard.
|
|
280
|
+
const inFlightEffectIds = new Set([...inFlight.values()].flatMap((entry) => entry.effectIds));
|
|
242
281
|
for (const item of outstanding) {
|
|
243
|
-
const dispatchKey =
|
|
244
|
-
if (dispatchedEffectIds.has(dispatchKey)) {
|
|
245
|
-
|
|
282
|
+
const dispatchKey = dispatchKeyFor(item);
|
|
283
|
+
if (!dispatchedEffectIds.has(dispatchKey)) {
|
|
284
|
+
continue;
|
|
246
285
|
}
|
|
286
|
+
if (inFlightEffectIds.has(item.effectId)) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
return yield* new DuplicateEffectDispatchError(runId, item.effectId, item.threadId);
|
|
247
290
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
291
|
+
const due = outstanding.filter((item) => !dispatchedEffectIds.has(dispatchKeyFor(item)));
|
|
292
|
+
if (due.length > 0) {
|
|
293
|
+
const attempts = new Map(due.map((item) => [item.effectId, attemptFor(item)]));
|
|
294
|
+
for (const item of due) {
|
|
295
|
+
dispatchedEffectIds.add(`${item.effectId}:${attempts.get(item.effectId) ?? 1}`);
|
|
296
|
+
}
|
|
297
|
+
const attemptEvents = due.map((item) => createEvent(runId, {
|
|
298
|
+
type: 'runtime.effect.attempt.started',
|
|
299
|
+
payload: {
|
|
300
|
+
effectId: item.effectId,
|
|
301
|
+
attempt: attempts.get(item.effectId) ?? 1,
|
|
302
|
+
},
|
|
303
|
+
threadId: item.threadId,
|
|
304
|
+
origin: { type: 'system' },
|
|
305
|
+
}));
|
|
306
|
+
for (const item of due) {
|
|
307
|
+
observe({
|
|
308
|
+
type: 'effect.attempt',
|
|
309
|
+
runId,
|
|
310
|
+
at: now,
|
|
311
|
+
effectId: item.effectId,
|
|
312
|
+
attempt: attempts.get(item.effectId) ?? 1,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
const attemptAppend = yield* appendObserved(store, runId, stripSeq(attemptEvents));
|
|
316
|
+
for (const item of due) {
|
|
317
|
+
startedEffectIds.add(item.effectId);
|
|
318
|
+
}
|
|
319
|
+
cursor = yield* applyAppend(store, runId, cursor, stripSeq(attemptEvents), attemptAppend);
|
|
320
|
+
const executorOptions = {
|
|
321
|
+
registry,
|
|
322
|
+
services,
|
|
323
|
+
definitionResolver,
|
|
324
|
+
executionContext,
|
|
325
|
+
liveAppends,
|
|
326
|
+
runInContext,
|
|
327
|
+
observe,
|
|
328
|
+
launcher: options.launcher(),
|
|
329
|
+
};
|
|
330
|
+
const runContext = {
|
|
286
331
|
correlationId: cursor.state.startIdentity?.correlationId ?? runId,
|
|
287
332
|
attributes: cursor.state.attributes ?? {},
|
|
288
333
|
secrets,
|
|
289
334
|
trace: cursor.state.startIdentity?.trace ?? null,
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
335
|
+
};
|
|
336
|
+
// One fiber per (thread, causingSeq) group keeps in-group order and frees the
|
|
337
|
+
// wake loop to plan other threads while a group is still running.
|
|
338
|
+
for (const groupKey of groupOutstanding(due)) {
|
|
339
|
+
const groupItems = due.filter((item) => `${item.threadId}:${item.causingSeq}` === groupKey);
|
|
340
|
+
// Child fiber: interrupted if the wake itself is interrupted; completions
|
|
341
|
+
// are collected via the settled queue so the wake can replan mid-flight.
|
|
342
|
+
const fiber = yield* Effect.forkChild(executeLocalEffects(executorOptions, {
|
|
343
|
+
runId,
|
|
344
|
+
context: runContext,
|
|
345
|
+
outstanding: groupItems,
|
|
346
|
+
attempts,
|
|
347
|
+
now,
|
|
348
|
+
}).pipe(Effect.exit, Effect.flatMap((exit) => Queue.offer(settled, { groupKey, exit })), Effect.asVoid));
|
|
349
|
+
inFlight.set(groupKey, {
|
|
350
|
+
groupKey,
|
|
351
|
+
fiber,
|
|
352
|
+
effectIds: groupItems.map((item) => item.effectId),
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (inFlight.size === 0) {
|
|
296
357
|
break;
|
|
297
358
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const appendResult = yield* appendObserved(store, runId, stripSeq(fullBatch));
|
|
314
|
-
cursor = yield* applyAppend(store, runId, cursor, stripSeq(fullBatch), appendResult);
|
|
315
|
-
// Bound replay after a crash inside a long wake.
|
|
316
|
-
if (snapshotEvery > 0 && cursor.durableSinceSnapshot >= snapshotEvery) {
|
|
317
|
-
cursor = yield* persistSnapshot(store, runId, cursor);
|
|
359
|
+
const wakeGate = Deferred.makeUnsafe();
|
|
360
|
+
executionContext.wakeSignal = wakeGate;
|
|
361
|
+
if (executionContext.wakeAgain) {
|
|
362
|
+
Deferred.doneUnsafe(wakeGate, Effect.void);
|
|
363
|
+
}
|
|
364
|
+
const progress = yield* Effect.raceFirst(Queue.take(settled).pipe(Effect.map((item) => ({ _tag: 'settled', ...item }))), Deferred.await(wakeGate).pipe(Effect.as({ _tag: 'wake' })));
|
|
365
|
+
if (executionContext.wakeSignal === wakeGate) {
|
|
366
|
+
executionContext.wakeSignal = null;
|
|
367
|
+
}
|
|
368
|
+
if (progress._tag === 'wake') {
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
inFlight.delete(progress.groupKey);
|
|
372
|
+
if (Exit.isFailure(progress.exit)) {
|
|
373
|
+
return yield* Effect.failCause(progress.exit.cause);
|
|
318
374
|
}
|
|
375
|
+
const produced = progress.exit.value;
|
|
376
|
+
yield* foldProduced(produced);
|
|
377
|
+
// Match the previous "empty batch → park" behavior. Aborted attempts produce no
|
|
378
|
+
// events; force a wakeAgain re-entry so attempt claims reset and post-interrupt
|
|
379
|
+
// work (a steered follow-up turn) can run under a fresh dispatchedEffectIds set.
|
|
380
|
+
if (produced.length === 0 && inFlight.size === 0) {
|
|
381
|
+
executionContext.wakeAgain = true;
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (inFlight.size > 0) {
|
|
386
|
+
yield* Fiber.interruptAll([...inFlight.values()].map((entry) => entry.fiber));
|
|
387
|
+
inFlight.clear();
|
|
319
388
|
}
|
|
320
389
|
const root = cursor.state.rootThreadId
|
|
321
390
|
? cursor.state.threads[cursor.state.rootThreadId]
|