@swirls/looms 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/actor/cell.d.ts +1 -1
- package/dist/actor/routing.d.ts +5 -1
- package/dist/actor/routing.js +14 -3
- package/dist/agent/effects.d.ts +20 -2
- package/dist/agent/effects.js +2 -0
- package/dist/agent/module.d.ts +40 -4
- package/dist/approval/effects.d.ts +10 -1
- package/dist/approval/effects.js +1 -0
- package/dist/approval/module.d.ts +20 -2
- package/dist/cli/commands.js +65 -1
- package/dist/client/client.d.ts +38 -10
- package/dist/client/client.js +2 -1
- package/dist/cloudflare/durable-object.d.ts +1 -1
- package/dist/cloudflare/index.d.ts +1 -1
- package/dist/cloudflare/index.js +1 -1
- package/dist/cloudflare/queue-consumer.d.ts +17 -10
- package/dist/cloudflare/queue-consumer.js +63 -27
- package/dist/cloudflare/queue-message.d.ts +4 -1
- package/dist/cloudflare/queue-message.js +4 -1
- package/dist/cloudflare/queue-worker.d.ts +7 -3
- package/dist/cloudflare/queue-worker.js +7 -2
- package/dist/cloudflare/queues.d.ts +1 -1
- package/dist/cloudflare/queues.js +1 -1
- package/dist/core/compose.d.ts +2 -1
- package/dist/core/compose.js +5 -0
- package/dist/core/effects.d.ts +84 -39
- package/dist/core/effects.js +29 -8
- package/dist/core/fold.js +34 -0
- package/dist/core/module-scope.d.ts +22 -8
- package/dist/core/module-scope.js +3 -0
- package/dist/core/module.d.ts +40 -9
- package/dist/core/module.js +25 -6
- package/dist/core/protocol.d.ts +12 -1
- package/dist/core/protocol.js +12 -0
- package/dist/core/published.d.ts +52 -0
- package/dist/core/published.js +45 -0
- package/dist/core/replay.d.ts +4 -2
- package/dist/core/state.d.ts +8 -2
- package/dist/core/state.js +2 -0
- package/dist/debugger/app/assets/{index-CUO1xyYW.js → index-CMRl_aWQ.js} +2 -2
- package/dist/debugger/app/assets/routes-CfXxKG45.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/events/protocol-summarize.js +2 -0
- package/dist/evaluator/effects.d.ts +10 -1
- package/dist/evaluator/effects.js +1 -0
- package/dist/evaluator/module.d.ts +20 -2
- package/dist/projectors/delivery.d.ts +8 -3
- package/dist/projectors/delivery.js +29 -2
- package/dist/projectors/event-log.d.ts +8 -2
- package/dist/projectors/event-log.js +47 -61
- package/dist/projectors/index.d.ts +1 -0
- package/dist/projectors/index.js +1 -0
- package/dist/projectors/postgres.d.ts +4 -1
- package/dist/projectors/postgres.js +6 -4
- package/dist/projectors/projector.d.ts +16 -0
- package/dist/projectors/projector.js +3 -0
- package/dist/projectors/replica/drizzle.d.ts +1192 -0
- package/dist/projectors/replica/drizzle.js +119 -0
- package/dist/projectors/replica/migrations.d.ts +35 -0
- package/dist/projectors/replica/migrations.js +230 -0
- package/dist/projectors/replica/postgres.d.ts +57 -0
- package/dist/projectors/replica/postgres.js +356 -0
- package/dist/projectors/schema.d.ts +2 -1
- package/dist/projectors/schema.js +2 -31
- package/dist/projectors/sqlite-delivery.js +9 -0
- package/dist/runtime/cancellation.js +1 -1
- package/dist/runtime/dispatch-effect.d.ts +2 -2
- package/dist/runtime/dispatch-effect.js +12 -4
- package/dist/runtime/effect-admission.d.ts +45 -0
- package/dist/runtime/effect-admission.js +70 -0
- package/dist/runtime/helpers.d.ts +6 -1
- package/dist/runtime/helpers.js +14 -5
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +1 -0
- package/dist/runtime/local-effect-executor.js +25 -3
- package/dist/runtime/retry-policy.js +6 -1
- package/dist/runtime/runtime.js +7 -2
- package/dist/runtime/server/routes/definitions.js +8 -1
- package/dist/runtime/types.d.ts +20 -5
- package/dist/runtime/wake-engine.js +20 -0
- package/dist/runtime/worker-callbacks.js +2 -2
- package/dist/runtime/worker-effect-dispatch.d.ts +35 -9
- package/dist/runtime/worker-effect-dispatch.js +105 -26
- package/dist/schedule/effects.d.ts +20 -2
- package/dist/schedule/effects.js +2 -0
- package/dist/schedule/module.d.ts +76 -40
- package/dist/testing/corpus-module.d.ts +2 -2
- package/dist/testing/corpus-module.js +27 -19
- package/dist/testing/index.js +12 -23
- package/dist/workflow/effects.d.ts +20 -2
- package/dist/workflow/effects.js +2 -0
- package/dist/workflow/module.d.ts +40 -4
- package/package.json +29 -1
- package/dist/debugger/app/assets/routes-lt_6J025.js +0 -47
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Effect, Option, Predicate } from 'effect';
|
|
2
2
|
import { createEvent, createRunId, DEFAULT_DEFINITION_VERSION, validateEventInput, withdrawnError, } from '@swirls/looms/core';
|
|
3
3
|
import { dispatchEffect } from './dispatch-effect.js';
|
|
4
|
+
import { admitOrFailAttempt } from './effect-admission.js';
|
|
4
5
|
import { RuntimeExecutionError } from './errors.js';
|
|
5
6
|
import { createEffectFailedEvent, createLiveEvent, groupOutstanding, materializeEffectOutcome, } from './helpers.js';
|
|
6
7
|
import { planAttemptFailure } from './retry-policy.js';
|
|
@@ -92,14 +93,35 @@ export function executeLocalEffects(options, input) {
|
|
|
92
93
|
yield* liveAppends.push(input.runId, liveEvent);
|
|
93
94
|
}
|
|
94
95
|
}));
|
|
96
|
+
const retry = registry.effects.get(item.effect.type)?.retry;
|
|
97
|
+
const launched = [];
|
|
98
|
+
const attemptNumber = input.attempts.get(item.effectId) ?? 1;
|
|
99
|
+
const admission = yield* admitOrFailAttempt({
|
|
100
|
+
registry,
|
|
101
|
+
item,
|
|
102
|
+
runId: input.runId,
|
|
103
|
+
attempt: attemptNumber,
|
|
104
|
+
now: input.now,
|
|
105
|
+
attributes: input.context.attributes,
|
|
106
|
+
secrets: input.context.secrets,
|
|
107
|
+
trace: input.context.trace,
|
|
108
|
+
retry,
|
|
109
|
+
});
|
|
110
|
+
if (admission._tag === 'reject') {
|
|
111
|
+
if (admission.observation) {
|
|
112
|
+
observe(admission.observation);
|
|
113
|
+
}
|
|
114
|
+
produced.push(admission.event);
|
|
115
|
+
failedEffectId = item.effectId;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
95
118
|
const controller = makeAbortController();
|
|
96
119
|
executionContext.activeEffects.set(item.effectId, {
|
|
97
120
|
controller,
|
|
98
121
|
threadId: item.threadId,
|
|
99
122
|
});
|
|
100
|
-
const retry = registry.effects.get(item.effect.type)?.retry;
|
|
101
|
-
const launched = [];
|
|
102
123
|
const dispatched = dispatchEffect(registry, services, definitionResolver, launcherFor(launcher, input.runId, input.context, item, launched), item.effect, {
|
|
124
|
+
placement: 'local',
|
|
103
125
|
effectId: item.effectId,
|
|
104
126
|
runId: input.runId,
|
|
105
127
|
threadId: item.threadId,
|
|
@@ -108,6 +130,7 @@ export function executeLocalEffects(options, input) {
|
|
|
108
130
|
secrets: input.context.secrets,
|
|
109
131
|
trace: input.context.trace,
|
|
110
132
|
signal: controller.signal,
|
|
133
|
+
heartbeat: () => Promise.resolve(),
|
|
111
134
|
emit: appendLive,
|
|
112
135
|
}).pipe(Effect.map((outcomes) => ({ _tag: 'completed', outcomes })), Effect.raceFirst(abortion(controller.signal)));
|
|
113
136
|
const settled = yield* (retry?.startToCloseTimeoutMs
|
|
@@ -123,7 +146,6 @@ export function executeLocalEffects(options, input) {
|
|
|
123
146
|
const before = produced.length;
|
|
124
147
|
if (settled._tag === 'timed-out') {
|
|
125
148
|
controller.abort(new RuntimeExecutionError('start-to-close timeout'));
|
|
126
|
-
const attemptNumber = input.attempts.get(item.effectId) ?? 1;
|
|
127
149
|
observe({
|
|
128
150
|
type: 'effect.timeout',
|
|
129
151
|
runId: input.runId,
|
|
@@ -17,7 +17,12 @@ export function retryDelay(retry, attempt, retryAfterMs) {
|
|
|
17
17
|
export function planAttemptFailure(input) {
|
|
18
18
|
const { runId, item, attempt, error, retry } = input;
|
|
19
19
|
if (input.nonRetryable || !retry || attempt >= retry.maxAttempts) {
|
|
20
|
-
return {
|
|
20
|
+
return {
|
|
21
|
+
event: createEffectFailedEvent(runId, item, error, {
|
|
22
|
+
nonRetryable: input.nonRetryable === true,
|
|
23
|
+
}),
|
|
24
|
+
retrying: false,
|
|
25
|
+
};
|
|
21
26
|
}
|
|
22
27
|
return {
|
|
23
28
|
retrying: true,
|
package/dist/runtime/runtime.js
CHANGED
|
@@ -12,7 +12,7 @@ import { assertSameDurableStart } from './run-start.js';
|
|
|
12
12
|
import { createWakeEngine } from './wake-engine.js';
|
|
13
13
|
import { createTimeoutScheduler } from './wake-scheduler.js';
|
|
14
14
|
import { createWorkerCallbacks } from './worker-callbacks.js';
|
|
15
|
-
import { assertWorkerDeadlines } from './worker-effect-dispatch.js';
|
|
15
|
+
import { assertWorkerClaimsExplicit, assertWorkerConfigured, assertWorkerDeadlines, listEffectPlacements, } from './worker-effect-dispatch.js';
|
|
16
16
|
export { createTimeoutScheduler } from './wake-scheduler.js';
|
|
17
17
|
export { DuplicateEffectDispatchError, ForkConflictError, MaxWakeIterationsError, RunNotFinishedError, RunNotFoundError, RunTerminalError, StartRunConflictError, } from './errors.js';
|
|
18
18
|
export { RunOverloadedError } from './ingress.js';
|
|
@@ -79,6 +79,8 @@ export function createRuntime(options) {
|
|
|
79
79
|
throw new Error('localLauncher requires options.store');
|
|
80
80
|
}
|
|
81
81
|
const registry = composeModules(options.modules);
|
|
82
|
+
assertWorkerConfigured(registry, options.worker);
|
|
83
|
+
assertWorkerClaimsExplicit(registry, options.worker);
|
|
82
84
|
assertWorkerDeadlines(registry, options.worker);
|
|
83
85
|
const staticResolver = staticDefinitionResolver(registry.definitions.map((definition) => ({
|
|
84
86
|
...definition.value,
|
|
@@ -277,6 +279,7 @@ export function createRuntime(options) {
|
|
|
277
279
|
runtime = {
|
|
278
280
|
modules: options.modules,
|
|
279
281
|
registry,
|
|
282
|
+
effectPlacements: listEffectPlacements(registry),
|
|
280
283
|
...workerCallbacks,
|
|
281
284
|
listRuns: Effect.gen(function* () {
|
|
282
285
|
const store = yield* EventStoreTag;
|
|
@@ -325,6 +328,7 @@ export function createRuntime(options) {
|
|
|
325
328
|
effectId: item.effectId,
|
|
326
329
|
threadId: item.threadId,
|
|
327
330
|
type: item.effect.type,
|
|
331
|
+
placement: registry.effects.get(item.effect.type)?.placement,
|
|
328
332
|
attempt: execution?.attempt ?? 0,
|
|
329
333
|
status: execution?.status ?? 'pending',
|
|
330
334
|
deadlineAt: execution?.deadlineAt ?? null,
|
|
@@ -343,7 +347,8 @@ export function createRuntime(options) {
|
|
|
343
347
|
}
|
|
344
348
|
if (execution.status !== 'ambiguous' &&
|
|
345
349
|
execution.status !== 'timed_out' &&
|
|
346
|
-
execution.status !== 'cancel_requested'
|
|
350
|
+
execution.status !== 'cancel_requested' &&
|
|
351
|
+
execution.status !== 'dispatch_wait') {
|
|
347
352
|
return yield* new RuntimeExecutionError(`Effect "${effectId}" cannot be retried from status "${execution.status}"`);
|
|
348
353
|
}
|
|
349
354
|
const now = yield* Clock.currentTimeMillis;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
import { publishDefinition } from '@swirls/looms/core';
|
|
2
|
+
import { publishDefinition, publishEffects } from '@swirls/looms/core';
|
|
3
3
|
import { route } from '../route-table.js';
|
|
4
4
|
export function definitionRoutes({ runtime }) {
|
|
5
5
|
return [
|
|
@@ -16,5 +16,12 @@ export function definitionRoutes({ runtime }) {
|
|
|
16
16
|
return Effect.succeed(Response.json(catalog));
|
|
17
17
|
},
|
|
18
18
|
}),
|
|
19
|
+
route({
|
|
20
|
+
method: 'GET',
|
|
21
|
+
path: '/definitions/effects',
|
|
22
|
+
name: 'definitions.effects',
|
|
23
|
+
access: 'read',
|
|
24
|
+
handle: () => Effect.succeed(Response.json({ effects: publishEffects(runtime.registry.effects.values()) })),
|
|
25
|
+
}),
|
|
19
26
|
];
|
|
20
27
|
}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -64,6 +64,12 @@ export interface EffectWorkerTask {
|
|
|
64
64
|
readonly attempt: number;
|
|
65
65
|
readonly type: string;
|
|
66
66
|
readonly input: JsonValue;
|
|
67
|
+
/** Thread that requested the effect, so a worker handler can build an EffectContext. */
|
|
68
|
+
readonly threadId: string;
|
|
69
|
+
/** Event that caused the outstanding effect. */
|
|
70
|
+
readonly causingEventId: string;
|
|
71
|
+
/** Immutable run labels; safe to put on a durable queue message. */
|
|
72
|
+
readonly attributes?: RunAttributes;
|
|
67
73
|
/** Progress from the previous attempt's last heartbeat, when it reported any. */
|
|
68
74
|
readonly details?: JsonValue;
|
|
69
75
|
/** The run's W3C trace context, for parenting the worker's spans. */
|
|
@@ -92,11 +98,13 @@ export interface EffectWorker {
|
|
|
92
98
|
cancel?(task: EffectWorkerTask): void | Promise<void>;
|
|
93
99
|
/**
|
|
94
100
|
* What a rejected `dispatch` means. With `'unacknowledged'` (the default) the task may still have
|
|
95
|
-
* arrived, so the attempt
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
101
|
+
* arrived, so the attempt enters `dispatch_wait` and the host relays the same attempt later
|
|
102
|
+
* (outbox); workers must treat `(runId, effectId, attempt)` as idempotent. Exhausted relays park
|
|
103
|
+
* as ambiguous for an operator. With `'acknowledged'` a rejection means the transport refused it,
|
|
104
|
+
* so the attempt fails under the retry policy. A transport can still lose an accepted message, so
|
|
105
|
+
* every effect such a worker claims must set `retry.scheduleToStartTimeoutMs`, and either
|
|
106
|
+
* `startToCloseTimeoutMs` or `heartbeatTimeoutMs` so a lost terminal callback cannot leave the
|
|
107
|
+
* attempt running forever.
|
|
100
108
|
*/
|
|
101
109
|
readonly dispatchSemantics?: 'unacknowledged' | 'acknowledged';
|
|
102
110
|
}
|
|
@@ -139,6 +147,8 @@ export interface RunOperationalStatus {
|
|
|
139
147
|
readonly effectId: string;
|
|
140
148
|
readonly threadId: string;
|
|
141
149
|
readonly type: string;
|
|
150
|
+
/** From the effect definition when registered; `undefined` for unknown types. */
|
|
151
|
+
readonly placement: 'local' | 'worker' | undefined;
|
|
142
152
|
readonly attempt: number;
|
|
143
153
|
readonly status: string;
|
|
144
154
|
readonly deadlineAt: number | null;
|
|
@@ -148,6 +158,11 @@ export interface RunOperationalStatus {
|
|
|
148
158
|
export interface LoomsRuntime<TModules extends readonly AnyRuntimeModule[] = readonly AnyRuntimeModule[]> {
|
|
149
159
|
readonly modules: TModules;
|
|
150
160
|
readonly registry: ComposedRegistry;
|
|
161
|
+
/** Declared effect placements from the composed registry (`local` | `worker`). */
|
|
162
|
+
readonly effectPlacements: ReadonlyArray<{
|
|
163
|
+
readonly type: string;
|
|
164
|
+
readonly placement: 'local' | 'worker';
|
|
165
|
+
}>;
|
|
151
166
|
startRun(args: StartRunArgs): Effect.Effect<StartResult, Error | EventStoreAppendError | EventStoreError, EventStoreTag>;
|
|
152
167
|
/**
|
|
153
168
|
* Commit a run's start, then schedule a wake instead of waiting for it. A duplicate start with
|
|
@@ -2,6 +2,7 @@ import { Clock, Effect, Exit, Option } 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
|
+
import { admitOrFailAttempt } from './effect-admission.js';
|
|
5
6
|
import { DuplicateEffectDispatchError, MaxWakeIterationsError, RuntimeExecutionError, } from './errors.js';
|
|
6
7
|
import { cascadedCancellations, stripSeq, synthesizedThreadFailed, waitSatisfiedEvents, } from './helpers.js';
|
|
7
8
|
import { executeLocalEffects } from './local-effect-executor.js';
|
|
@@ -186,6 +187,25 @@ export function createWakeEngine(options) {
|
|
|
186
187
|
});
|
|
187
188
|
if (workerPlans.length > 0) {
|
|
188
189
|
for (const workerPlan of workerPlans) {
|
|
190
|
+
const admission = yield* admitOrFailAttempt({
|
|
191
|
+
registry,
|
|
192
|
+
item: workerPlan.item,
|
|
193
|
+
runId,
|
|
194
|
+
attempt: workerPlan.attempt,
|
|
195
|
+
now,
|
|
196
|
+
attributes: state.attributes ?? {},
|
|
197
|
+
secrets,
|
|
198
|
+
trace: state.startIdentity?.trace ?? null,
|
|
199
|
+
retry: workerPlan.retry,
|
|
200
|
+
});
|
|
201
|
+
if (admission._tag === 'reject') {
|
|
202
|
+
if (admission.observation) {
|
|
203
|
+
observe(admission.observation);
|
|
204
|
+
}
|
|
205
|
+
const failureAppend = yield* appendObserved(store, runId, stripSeq([admission.event]));
|
|
206
|
+
cursor = yield* applyAppend(store, runId, cursor, stripSeq([admission.event]), failureAppend);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
189
209
|
if (workerPlan.queuedEvent) {
|
|
190
210
|
const queuedAppend = yield* appendObserved(store, runId, stripSeq([workerPlan.queuedEvent]));
|
|
191
211
|
cursor = yield* applyAppend(store, runId, cursor, stripSeq([workerPlan.queuedEvent]), queuedAppend);
|
|
@@ -71,12 +71,12 @@ function planWorkerCallback(input) {
|
|
|
71
71
|
});
|
|
72
72
|
switch (callback.kind) {
|
|
73
73
|
case 'started': {
|
|
74
|
-
return Effect.succeed(item && execution.status === 'dispatched'
|
|
74
|
+
return Effect.succeed(item && (execution.status === 'dispatched' || execution.status === 'dispatch_wait')
|
|
75
75
|
? accepted([workerStartedEvent(runId, item, callback.attempt, retry, now)])
|
|
76
76
|
: STALE);
|
|
77
77
|
}
|
|
78
78
|
case 'heartbeat': {
|
|
79
|
-
const implicitStart = execution.status === 'dispatched';
|
|
79
|
+
const implicitStart = execution.status === 'dispatched' || execution.status === 'dispatch_wait';
|
|
80
80
|
if (!item ||
|
|
81
81
|
(!implicitStart && execution.status !== 'started' && execution.status !== 'heartbeat')) {
|
|
82
82
|
return Effect.succeed(STALE);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
import { type ComposedRegistry, type EffectExecutionRecord, type EventEnvelope, type OutstandingEffect, type RetryPolicy, type RunState, type SecretContext } from '@swirls/looms/core';
|
|
2
|
+
import { type ComposedRegistry, type EffectDefinition, type EffectExecutionRecord, type EffectPlacement, type EventEnvelope, type OutstandingEffect, type RetryPolicy, type RunState, type SecretContext } from '@swirls/looms/core';
|
|
3
3
|
import type { EffectWorker, EffectWorkerTask } from './types';
|
|
4
|
+
/** Caps how many times one attempt's unacknowledged dispatch can be deferred before parking as ambiguous. */
|
|
5
|
+
export declare const DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS = 32;
|
|
4
6
|
export interface WorkerEffectDispatchPlan {
|
|
5
7
|
readonly item: OutstandingEffect;
|
|
6
8
|
readonly attempt: number;
|
|
@@ -8,12 +10,13 @@ export interface WorkerEffectDispatchPlan {
|
|
|
8
10
|
readonly task: EffectWorkerTask;
|
|
9
11
|
readonly retry: RetryPolicy | undefined;
|
|
10
12
|
readonly scheduleToStartDeadlineAt: number | null;
|
|
13
|
+
/** Next outbox dispatch try for this attempt (1 on first push). */
|
|
14
|
+
readonly dispatchTry: number;
|
|
11
15
|
}
|
|
12
16
|
/**
|
|
13
17
|
* Decides where an outstanding effect runs. An attempt already in flight keeps
|
|
14
|
-
* its durable placement. Otherwise
|
|
15
|
-
*
|
|
16
|
-
* go to the worker.
|
|
18
|
+
* its durable placement. Otherwise the definition's `placement` wins. Unregistered
|
|
19
|
+
* types go to a configured worker unless it declines them.
|
|
17
20
|
*/
|
|
18
21
|
export declare function isWorkerEffect(input: {
|
|
19
22
|
readonly runId: string;
|
|
@@ -32,12 +35,19 @@ export declare function planWorkerEffects(input: {
|
|
|
32
35
|
readonly secrets?: SecretContext;
|
|
33
36
|
}): ReadonlyArray<WorkerEffectDispatchPlan>;
|
|
34
37
|
/**
|
|
35
|
-
* Hand a planned attempt to the worker.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
38
|
+
* Hand a planned attempt to the worker.
|
|
39
|
+
*
|
|
40
|
+
* - **acknowledged** reject: nothing was sent → fail under the retry policy.
|
|
41
|
+
* - **unacknowledged** reject: enter `dispatch_wait` (`runtime.effect.dispatch.deferred`) and the
|
|
42
|
+
* wake engine retries later with dedicated outbox backoff. After
|
|
43
|
+
* {@link DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS} tries, park as ambiguous for an operator.
|
|
44
|
+
* Workers must treat `(runId, effectId, attempt)` as idempotent — a rejected push may still have
|
|
45
|
+
* arrived, so a later `started` from `dispatch_wait` is accepted.
|
|
46
|
+
* - Missing worker: ambiguous (misconfiguration).
|
|
39
47
|
*/
|
|
40
|
-
export declare function dispatchWorkerEffect(runId: string, plan: WorkerEffectDispatchPlan, worker: EffectWorker | undefined, now: number
|
|
48
|
+
export declare function dispatchWorkerEffect(runId: string, plan: WorkerEffectDispatchPlan, worker: EffectWorker | undefined, now: number, options?: {
|
|
49
|
+
readonly maxDispatchDeferrals?: number;
|
|
50
|
+
}): Effect.Effect<EventEnvelope>;
|
|
41
51
|
/**
|
|
42
52
|
* An acknowledged transport can lose a message it accepted, for example to a dead-letter queue.
|
|
43
53
|
* schedule-to-start covers a message that never starts. After `started` is acked, only
|
|
@@ -45,3 +55,19 @@ export declare function dispatchWorkerEffect(runId: string, plan: WorkerEffectDi
|
|
|
45
55
|
* effect needs one of those as well.
|
|
46
56
|
*/
|
|
47
57
|
export declare function assertWorkerDeadlines(registry: ComposedRegistry, worker: EffectWorker | undefined): void;
|
|
58
|
+
/** Registered effect types with their declared placement, sorted by type for stable output. */
|
|
59
|
+
export declare function listEffectPlacements(registry: ComposedRegistry): ReadonlyArray<{
|
|
60
|
+
readonly type: string;
|
|
61
|
+
readonly placement: EffectPlacement;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* Worker-placed effects need a configured `EffectWorker`. Without one they would park as
|
|
65
|
+
* ambiguous on every attempt.
|
|
66
|
+
*/
|
|
67
|
+
export declare function assertWorkerConfigured(registry: ComposedRegistry, worker: EffectWorker | undefined): void;
|
|
68
|
+
/**
|
|
69
|
+
* A worker must not claim a local-placed effect. Declare the effect with `placement: 'worker'` (or a
|
|
70
|
+
* hybrid when that lands) instead of overlapping `handles` with a local definition.
|
|
71
|
+
*/
|
|
72
|
+
export declare function assertWorkerClaimsExplicit(registry: ComposedRegistry, worker: EffectWorker | undefined): void;
|
|
73
|
+
export declare function claimsType(worker: EffectWorker, definition: EffectDefinition): boolean;
|
|
@@ -2,7 +2,15 @@ import { Effect, Predicate } from 'effect';
|
|
|
2
2
|
import { createEvent, deferredUntil, isPrimitiveEffect, } from '@swirls/looms/core';
|
|
3
3
|
import { RuntimeExecutionError } from './errors.js';
|
|
4
4
|
import { workerTask } from './helpers.js';
|
|
5
|
-
import { planAttemptFailure } from './retry-policy.js';
|
|
5
|
+
import { planAttemptFailure, retryBackoff } from './retry-policy.js';
|
|
6
|
+
/** Caps how many times one attempt's unacknowledged dispatch can be deferred before parking as ambiguous. */
|
|
7
|
+
export const DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS = 32;
|
|
8
|
+
const DISPATCH_BACKOFF = {
|
|
9
|
+
maxAttempts: DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS,
|
|
10
|
+
backoffMs: 100,
|
|
11
|
+
maxBackoffMs: 30_000,
|
|
12
|
+
backoffMultiplier: 2,
|
|
13
|
+
};
|
|
6
14
|
function workerClaims(worker, runId, item) {
|
|
7
15
|
if (!worker.handles && !worker.canDispatch) {
|
|
8
16
|
return undefined;
|
|
@@ -15,6 +23,7 @@ function workerClaims(worker, runId, item) {
|
|
|
15
23
|
}
|
|
16
24
|
const WORKER_ATTEMPT_STATUSES = new Set([
|
|
17
25
|
'queued',
|
|
26
|
+
'dispatch_wait',
|
|
18
27
|
'dispatched',
|
|
19
28
|
'started',
|
|
20
29
|
'heartbeat',
|
|
@@ -22,11 +31,14 @@ const WORKER_ATTEMPT_STATUSES = new Set([
|
|
|
22
31
|
'timed_out',
|
|
23
32
|
'ambiguous',
|
|
24
33
|
]);
|
|
34
|
+
const SAME_ATTEMPT_STATUSES = new Set([
|
|
35
|
+
'queued',
|
|
36
|
+
'dispatch_wait',
|
|
37
|
+
]);
|
|
25
38
|
/**
|
|
26
39
|
* Decides where an outstanding effect runs. An attempt already in flight keeps
|
|
27
|
-
* its durable placement. Otherwise
|
|
28
|
-
*
|
|
29
|
-
* go to the worker.
|
|
40
|
+
* its durable placement. Otherwise the definition's `placement` wins. Unregistered
|
|
41
|
+
* types go to a configured worker unless it declines them.
|
|
30
42
|
*/
|
|
31
43
|
export function isWorkerEffect(input) {
|
|
32
44
|
const { item, execution, worker } = input;
|
|
@@ -40,16 +52,13 @@ export function isWorkerEffect(input) {
|
|
|
40
52
|
return true;
|
|
41
53
|
}
|
|
42
54
|
const definition = input.registry.effects.get(item.effect.type);
|
|
43
|
-
if (definition
|
|
44
|
-
return definition.
|
|
55
|
+
if (definition) {
|
|
56
|
+
return definition.placement === 'worker';
|
|
45
57
|
}
|
|
46
58
|
const claim = worker ? workerClaims(worker, input.runId, item) : undefined;
|
|
47
59
|
if (claim) {
|
|
48
60
|
return true;
|
|
49
61
|
}
|
|
50
|
-
if (definition) {
|
|
51
|
-
return !definition.hasHandler;
|
|
52
|
-
}
|
|
53
62
|
return worker !== undefined && claim !== false;
|
|
54
63
|
}
|
|
55
64
|
export function planWorkerEffects(input) {
|
|
@@ -62,13 +71,17 @@ export function planWorkerEffects(input) {
|
|
|
62
71
|
const execution = input.state.effectExecutions[item.effectId];
|
|
63
72
|
return (!execution ||
|
|
64
73
|
execution.status === 'queued' ||
|
|
74
|
+
(execution.status === 'dispatch_wait' &&
|
|
75
|
+
execution.nextAttemptAt !== null &&
|
|
76
|
+
execution.nextAttemptAt <= input.now + 5) ||
|
|
65
77
|
(execution.status === 'retry_wait' &&
|
|
66
78
|
execution.nextAttemptAt !== null &&
|
|
67
79
|
execution.nextAttemptAt <= input.now + 5));
|
|
68
80
|
});
|
|
69
81
|
return withinInFlightLimit(input, workerItems, due).map((item) => {
|
|
70
82
|
const existing = input.state.effectExecutions[item.effectId];
|
|
71
|
-
const
|
|
83
|
+
const sameAttempt = existing !== undefined && SAME_ATTEMPT_STATUSES.has(existing.status);
|
|
84
|
+
const attempt = sameAttempt ? existing.attempt : (existing?.attempt ?? 0) + 1;
|
|
72
85
|
const retry = input.registry.effects.get(item.effect.type)?.retry;
|
|
73
86
|
const scheduleToStartDeadlineAt = retry?.scheduleToStartTimeoutMs
|
|
74
87
|
? input.now + retry.scheduleToStartTimeoutMs
|
|
@@ -77,7 +90,8 @@ export function planWorkerEffects(input) {
|
|
|
77
90
|
item,
|
|
78
91
|
attempt,
|
|
79
92
|
retry,
|
|
80
|
-
|
|
93
|
+
dispatchTry: sameAttempt ? existing.dispatchTry + 1 : 1,
|
|
94
|
+
queuedEvent: sameAttempt
|
|
81
95
|
? undefined
|
|
82
96
|
: createEvent(input.runId, {
|
|
83
97
|
type: 'runtime.effect.queued',
|
|
@@ -89,6 +103,7 @@ export function planWorkerEffects(input) {
|
|
|
89
103
|
task: workerTask(input.runId, item, attempt, {
|
|
90
104
|
details: existing?.heartbeatDetails,
|
|
91
105
|
secrets: input.secrets,
|
|
106
|
+
attributes: input.state.attributes,
|
|
92
107
|
trace: input.state.startIdentity?.trace,
|
|
93
108
|
retry,
|
|
94
109
|
scheduleToStartAt: scheduleToStartDeadlineAt,
|
|
@@ -115,12 +130,17 @@ function withinInFlightLimit(input, workerItems, due) {
|
|
|
115
130
|
return due.slice(0, Math.max(0, max - inFlight));
|
|
116
131
|
}
|
|
117
132
|
/**
|
|
118
|
-
* Hand a planned attempt to the worker.
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
133
|
+
* Hand a planned attempt to the worker.
|
|
134
|
+
*
|
|
135
|
+
* - **acknowledged** reject: nothing was sent → fail under the retry policy.
|
|
136
|
+
* - **unacknowledged** reject: enter `dispatch_wait` (`runtime.effect.dispatch.deferred`) and the
|
|
137
|
+
* wake engine retries later with dedicated outbox backoff. After
|
|
138
|
+
* {@link DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS} tries, park as ambiguous for an operator.
|
|
139
|
+
* Workers must treat `(runId, effectId, attempt)` as idempotent — a rejected push may still have
|
|
140
|
+
* arrived, so a later `started` from `dispatch_wait` is accepted.
|
|
141
|
+
* - Missing worker: ambiguous (misconfiguration).
|
|
122
142
|
*/
|
|
123
|
-
export function dispatchWorkerEffect(runId, plan, worker, now) {
|
|
143
|
+
export function dispatchWorkerEffect(runId, plan, worker, now, options = {}) {
|
|
124
144
|
const dispatched = createEvent(runId, {
|
|
125
145
|
type: 'runtime.effect.dispatched',
|
|
126
146
|
payload: {
|
|
@@ -158,9 +178,23 @@ export function dispatchWorkerEffect(runId, plan, worker, now) {
|
|
|
158
178
|
origin: { type: 'system' },
|
|
159
179
|
}).event;
|
|
160
180
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
:
|
|
181
|
+
const maxDeferrals = options.maxDispatchDeferrals ?? DEFAULT_MAX_WORKER_DISPATCH_DEFERRALS;
|
|
182
|
+
if (plan.dispatchTry >= maxDeferrals) {
|
|
183
|
+
return ambiguous(`worker dispatch relay exhausted after ${plan.dispatchTry} tries: ${error.message}`);
|
|
184
|
+
}
|
|
185
|
+
return createEvent(runId, {
|
|
186
|
+
type: 'runtime.effect.dispatch.deferred',
|
|
187
|
+
payload: {
|
|
188
|
+
effectId: plan.item.effectId,
|
|
189
|
+
attempt: plan.attempt,
|
|
190
|
+
retryAt: now + retryBackoff(DISPATCH_BACKOFF, plan.dispatchTry),
|
|
191
|
+
error: `worker dispatch deferred: ${error.message}`,
|
|
192
|
+
try: plan.dispatchTry,
|
|
193
|
+
},
|
|
194
|
+
threadId: plan.item.threadId,
|
|
195
|
+
causationId: plan.item.causingEventId,
|
|
196
|
+
origin: { type: 'system' },
|
|
197
|
+
});
|
|
164
198
|
},
|
|
165
199
|
onSuccess: () => dispatched,
|
|
166
200
|
}));
|
|
@@ -189,13 +223,58 @@ export function assertWorkerDeadlines(registry, worker) {
|
|
|
189
223
|
throw new Error(`An acknowledged worker claims effects without retry.startToCloseTimeoutMs or retry.heartbeatTimeoutMs: ${missingClose.join(', ')}`);
|
|
190
224
|
}
|
|
191
225
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
226
|
+
/** Registered effect types with their declared placement, sorted by type for stable output. */
|
|
227
|
+
export function listEffectPlacements(registry) {
|
|
228
|
+
return [...registry.effects.values()]
|
|
229
|
+
.map((definition) => ({ type: definition.type, placement: definition.placement }))
|
|
230
|
+
.toSorted((left, right) => left.type.localeCompare(right.type));
|
|
231
|
+
}
|
|
232
|
+
function formatEffectPlacements(registry) {
|
|
233
|
+
return listEffectPlacements(registry)
|
|
234
|
+
.map((entry) => `${entry.type}=${entry.placement}`)
|
|
235
|
+
.join(', ');
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Worker-placed effects need a configured `EffectWorker`. Without one they would park as
|
|
239
|
+
* ambiguous on every attempt.
|
|
240
|
+
*/
|
|
241
|
+
export function assertWorkerConfigured(registry, worker) {
|
|
242
|
+
if (worker) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const needingWorker = [...registry.effects.values()]
|
|
246
|
+
.filter((definition) => definition.placement === 'worker')
|
|
247
|
+
.map((definition) => definition.type);
|
|
248
|
+
if (needingWorker.length > 0) {
|
|
249
|
+
throw new Error(`Effects require an EffectWorker but none is configured: ${needingWorker.join(', ')} (registry: ${formatEffectPlacements(registry)})`);
|
|
195
250
|
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* A worker must not claim a local-placed effect. Declare the effect with `placement: 'worker'` (or a
|
|
254
|
+
* hybrid when that lands) instead of overlapping `handles` with a local definition.
|
|
255
|
+
*/
|
|
256
|
+
export function assertWorkerClaimsExplicit(registry, worker) {
|
|
257
|
+
if (!worker) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const claimedLocal = [...registry.effects.values()]
|
|
261
|
+
.filter((definition) => definition.placement === 'local' && workerHandlesType(worker, definition.type))
|
|
262
|
+
.map((definition) => definition.type);
|
|
263
|
+
if (claimedLocal.length > 0) {
|
|
264
|
+
throw new Error(`Worker claims local-placed effects; declare them with placement: 'worker' (or drop the claim): ${claimedLocal.join(', ')} (registry: ${formatEffectPlacements(registry)})`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
export function claimsType(worker, definition) {
|
|
268
|
+
if (definition.placement !== 'worker') {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
// A worker with no claim filter takes every worker-placed effect.
|
|
272
|
+
if (!worker.handles && !worker.canDispatch) {
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
return workerHandlesType(worker, definition.type);
|
|
276
|
+
}
|
|
277
|
+
function workerHandlesType(worker, type) {
|
|
196
278
|
const { handles } = worker;
|
|
197
|
-
|
|
198
|
-
? handles(definition.type)
|
|
199
|
-
: (handles?.includes(definition.type) ?? false);
|
|
200
|
-
return handled || !definition.hasHandler;
|
|
279
|
+
return Predicate.isFunction(handles) ? handles(type) : (handles?.includes(type) ?? false);
|
|
201
280
|
}
|
|
@@ -54,7 +54,7 @@ declare const FireInputSchema: Schema.Struct<{
|
|
|
54
54
|
}>;
|
|
55
55
|
type PlanInput = Schema.Schema.Type<typeof PlanInputSchema>;
|
|
56
56
|
type FireInput = Schema.Schema.Type<typeof FireInputSchema>;
|
|
57
|
-
export declare const planEffect: NoInfer<import("@swirls/looms/core").
|
|
57
|
+
export declare const planEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
|
|
58
58
|
readonly spec: {
|
|
59
59
|
readonly type: "cron";
|
|
60
60
|
readonly expr: string;
|
|
@@ -118,6 +118,15 @@ export declare const planEffect: NoInfer<import("@swirls/looms/core").EffectDefi
|
|
|
118
118
|
readonly effectId: string;
|
|
119
119
|
readonly attempt: number;
|
|
120
120
|
};
|
|
121
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
122
|
+
readonly type: "runtime.effect.dispatch.deferred";
|
|
123
|
+
readonly payload: {
|
|
124
|
+
readonly effectId: string;
|
|
125
|
+
readonly attempt: number;
|
|
126
|
+
readonly retryAt: number;
|
|
127
|
+
readonly error: string;
|
|
128
|
+
readonly try: number;
|
|
129
|
+
};
|
|
121
130
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
122
131
|
readonly type: "runtime.effect.dispatched";
|
|
123
132
|
readonly payload: {
|
|
@@ -387,7 +396,7 @@ export declare const planEffect: NoInfer<import("@swirls/looms/core").EffectDefi
|
|
|
387
396
|
readonly definitionVersion?: string | undefined;
|
|
388
397
|
};
|
|
389
398
|
})>>;
|
|
390
|
-
export declare const fireEffect: NoInfer<import("@swirls/looms/core").
|
|
399
|
+
export declare const fireEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
|
|
391
400
|
readonly definitionName: string;
|
|
392
401
|
readonly definitionVersion: string;
|
|
393
402
|
readonly spec: {
|
|
@@ -460,6 +469,15 @@ export declare const fireEffect: NoInfer<import("@swirls/looms/core").EffectDefi
|
|
|
460
469
|
readonly effectId: string;
|
|
461
470
|
readonly attempt: number;
|
|
462
471
|
};
|
|
472
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
473
|
+
readonly type: "runtime.effect.dispatch.deferred";
|
|
474
|
+
readonly payload: {
|
|
475
|
+
readonly effectId: string;
|
|
476
|
+
readonly attempt: number;
|
|
477
|
+
readonly retryAt: number;
|
|
478
|
+
readonly error: string;
|
|
479
|
+
readonly try: number;
|
|
480
|
+
};
|
|
463
481
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
464
482
|
readonly type: "runtime.effect.dispatched";
|
|
465
483
|
readonly payload: {
|
package/dist/schedule/effects.js
CHANGED
|
@@ -38,6 +38,7 @@ class ScheduleEffectError extends Data.TaggedError('ScheduleEffectError') {
|
|
|
38
38
|
}
|
|
39
39
|
export const planEffect = scheduleModule.effect({
|
|
40
40
|
type: 'schedule.plan',
|
|
41
|
+
placement: 'local',
|
|
41
42
|
input: PlanInputSchema,
|
|
42
43
|
retry: RETRY,
|
|
43
44
|
execute: (input, ctx) => Effect.gen(function* () {
|
|
@@ -54,6 +55,7 @@ export const planEffect = scheduleModule.effect({
|
|
|
54
55
|
});
|
|
55
56
|
export const fireEffect = scheduleModule.effect({
|
|
56
57
|
type: 'schedule.fire',
|
|
58
|
+
placement: 'local',
|
|
57
59
|
input: FireInputSchema,
|
|
58
60
|
retry: RETRY,
|
|
59
61
|
execute: (input, ctx) => Effect.gen(function* () {
|