@swirls/looms 0.12.0 → 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
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @swirls/looms
|
|
2
2
|
|
|
3
|
+
## 0.12.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4609a95: Keep handling signals while local effects are in flight. The wake loop forks each `(thread, causingSeq)` effect group and replans when a joined or coalesced signal arrives, so a long tool call on one thread no longer blocks intake, narration, or steers aimed at other threads.
|
|
8
|
+
|
|
3
9
|
## 0.12.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -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);
|
|
@@ -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]
|