@swirls/looms 0.6.0 → 0.7.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.
Files changed (75) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/actor/cell.d.ts +1 -1
  3. package/dist/actor/routing.d.ts +5 -1
  4. package/dist/actor/routing.js +14 -3
  5. package/dist/agent/effects.d.ts +20 -2
  6. package/dist/agent/effects.js +2 -0
  7. package/dist/agent/module.d.ts +540 -504
  8. package/dist/approval/effects.d.ts +36 -27
  9. package/dist/approval/effects.js +1 -0
  10. package/dist/approval/module.d.ts +20 -2
  11. package/dist/client/client.d.ts +38 -10
  12. package/dist/client/client.js +2 -1
  13. package/dist/cloudflare/durable-object.d.ts +1 -1
  14. package/dist/cloudflare/index.d.ts +1 -1
  15. package/dist/cloudflare/index.js +1 -1
  16. package/dist/cloudflare/queue-consumer.d.ts +17 -10
  17. package/dist/cloudflare/queue-consumer.js +63 -27
  18. package/dist/cloudflare/queue-message.d.ts +4 -1
  19. package/dist/cloudflare/queue-message.js +4 -1
  20. package/dist/cloudflare/queue-worker.d.ts +7 -3
  21. package/dist/cloudflare/queue-worker.js +7 -2
  22. package/dist/cloudflare/queues.d.ts +1 -1
  23. package/dist/cloudflare/queues.js +1 -1
  24. package/dist/core/compose.d.ts +2 -1
  25. package/dist/core/compose.js +5 -0
  26. package/dist/core/effects.d.ts +84 -39
  27. package/dist/core/effects.js +29 -8
  28. package/dist/core/fold.js +87 -14
  29. package/dist/core/module-scope.d.ts +22 -8
  30. package/dist/core/module-scope.js +3 -0
  31. package/dist/core/module.d.ts +40 -9
  32. package/dist/core/module.js +25 -6
  33. package/dist/core/protocol.d.ts +12 -1
  34. package/dist/core/protocol.js +12 -0
  35. package/dist/core/published.d.ts +52 -0
  36. package/dist/core/published.js +45 -0
  37. package/dist/core/replay.d.ts +4 -2
  38. package/dist/core/state.d.ts +8 -2
  39. package/dist/core/state.js +2 -0
  40. package/dist/debugger/app/assets/{index-CUO1xyYW.js → index-CMRl_aWQ.js} +2 -2
  41. package/dist/debugger/app/assets/routes-CfXxKG45.js +47 -0
  42. package/dist/debugger/app/index.html +1 -1
  43. package/dist/debugger/events/protocol-summarize.js +2 -0
  44. package/dist/evaluator/effects.d.ts +10 -1
  45. package/dist/evaluator/effects.js +1 -0
  46. package/dist/evaluator/module.d.ts +20 -2
  47. package/dist/runtime/cancellation.js +1 -1
  48. package/dist/runtime/dispatch-effect.d.ts +2 -2
  49. package/dist/runtime/dispatch-effect.js +12 -4
  50. package/dist/runtime/effect-admission.d.ts +45 -0
  51. package/dist/runtime/effect-admission.js +70 -0
  52. package/dist/runtime/helpers.d.ts +6 -1
  53. package/dist/runtime/helpers.js +14 -5
  54. package/dist/runtime/index.d.ts +1 -0
  55. package/dist/runtime/index.js +1 -0
  56. package/dist/runtime/local-effect-executor.js +30 -8
  57. package/dist/runtime/retry-policy.js +6 -1
  58. package/dist/runtime/runtime.js +7 -2
  59. package/dist/runtime/server/routes/definitions.js +8 -1
  60. package/dist/runtime/types.d.ts +20 -5
  61. package/dist/runtime/wake-engine.js +20 -0
  62. package/dist/runtime/worker-callbacks.js +2 -2
  63. package/dist/runtime/worker-effect-dispatch.d.ts +35 -9
  64. package/dist/runtime/worker-effect-dispatch.js +105 -26
  65. package/dist/schedule/effects.d.ts +20 -2
  66. package/dist/schedule/effects.js +2 -0
  67. package/dist/schedule/module.d.ts +40 -4
  68. package/dist/testing/corpus-module.d.ts +2 -2
  69. package/dist/testing/corpus-module.js +27 -19
  70. package/dist/testing/index.js +12 -23
  71. package/dist/workflow/effects.d.ts +20 -2
  72. package/dist/workflow/effects.js +2 -0
  73. package/dist/workflow/module.d.ts +40 -4
  74. package/package.json +1 -1
  75. package/dist/debugger/app/assets/routes-lt_6J025.js +0 -47
@@ -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 an explicit `execution` wins, then a worker
15
- * claim; effects with a local handler run in process and handler-less effects
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. When an unacknowledged dispatch rejects, the task may still
36
- * have reached the worker, so with a schedule-to-start deadline it is recorded as dispatched and
37
- * recovered by that timeout; without one it is parked as ambiguous for an operator. An acknowledged
38
- * rejection means nothing was sent, so the attempt fails under the retry policy.
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): Effect.Effect<EventEnvelope>;
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 an explicit `execution` wins, then a worker
28
- * claim; effects with a local handler run in process and handler-less effects
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?.execution) {
44
- return definition.execution === 'worker';
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 attempt = existing?.status === 'queued' ? existing.attempt : (existing?.attempt ?? 0) + 1;
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
- queuedEvent: existing?.status === 'queued'
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. When an unacknowledged dispatch rejects, the task may still
119
- * have reached the worker, so with a schedule-to-start deadline it is recorded as dispatched and
120
- * recovered by that timeout; without one it is parked as ambiguous for an operator. An acknowledged
121
- * rejection means nothing was sent, so the attempt fails under the retry policy.
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
- return plan.scheduleToStartDeadlineAt === null
162
- ? ambiguous(`worker dispatch outcome is ambiguous: ${error.message}`)
163
- : dispatched;
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
- function claimsType(worker, definition) {
193
- if (definition.execution) {
194
- return definition.execution === 'worker';
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
- const handled = Predicate.isFunction(handles)
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").EffectDefinition<{
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").EffectDefinition<{
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: {
@@ -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* () {
@@ -64,7 +64,7 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
64
64
  readonly reason: import("effect/Schema").String;
65
65
  }>;
66
66
  }>, {
67
- plan: import("@swirls/looms/core").EffectDefinition<{
67
+ plan: import("@swirls/looms/core").LocalEffectDefinition<{
68
68
  readonly spec: {
69
69
  readonly type: "cron";
70
70
  readonly expr: string;
@@ -128,6 +128,15 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
128
128
  readonly effectId: string;
129
129
  readonly attempt: number;
130
130
  };
131
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
132
+ readonly type: "runtime.effect.dispatch.deferred";
133
+ readonly payload: {
134
+ readonly effectId: string;
135
+ readonly attempt: number;
136
+ readonly retryAt: number;
137
+ readonly error: string;
138
+ readonly try: number;
139
+ };
131
140
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
132
141
  readonly type: "runtime.effect.dispatched";
133
142
  readonly payload: {
@@ -397,7 +406,7 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
397
406
  readonly definitionVersion?: string | undefined;
398
407
  };
399
408
  })>;
400
- fire: import("@swirls/looms/core").EffectDefinition<{
409
+ fire: import("@swirls/looms/core").LocalEffectDefinition<{
401
410
  readonly definitionName: string;
402
411
  readonly definitionVersion: string;
403
412
  readonly spec: {
@@ -470,6 +479,15 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
470
479
  readonly effectId: string;
471
480
  readonly attempt: number;
472
481
  };
482
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
483
+ readonly type: "runtime.effect.dispatch.deferred";
484
+ readonly payload: {
485
+ readonly effectId: string;
486
+ readonly attempt: number;
487
+ readonly retryAt: number;
488
+ readonly error: string;
489
+ readonly try: number;
490
+ };
473
491
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
474
492
  readonly type: "runtime.effect.dispatched";
475
493
  readonly payload: {
@@ -767,7 +785,7 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
767
785
  }>;
768
786
  }, import("effect/Context").Service.Any> & {
769
787
  readonly effects: {
770
- plan: import("@swirls/looms/core").EffectDefinition<{
788
+ plan: import("@swirls/looms/core").LocalEffectDefinition<{
771
789
  readonly spec: {
772
790
  readonly type: "cron";
773
791
  readonly expr: string;
@@ -831,6 +849,15 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
831
849
  readonly effectId: string;
832
850
  readonly attempt: number;
833
851
  };
852
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
853
+ readonly type: "runtime.effect.dispatch.deferred";
854
+ readonly payload: {
855
+ readonly effectId: string;
856
+ readonly attempt: number;
857
+ readonly retryAt: number;
858
+ readonly error: string;
859
+ readonly try: number;
860
+ };
834
861
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
835
862
  readonly type: "runtime.effect.dispatched";
836
863
  readonly payload: {
@@ -1100,7 +1127,7 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
1100
1127
  readonly definitionVersion?: string | undefined;
1101
1128
  };
1102
1129
  })>;
1103
- fire: import("@swirls/looms/core").EffectDefinition<{
1130
+ fire: import("@swirls/looms/core").LocalEffectDefinition<{
1104
1131
  readonly definitionName: string;
1105
1132
  readonly definitionVersion: string;
1106
1133
  readonly spec: {
@@ -1173,6 +1200,15 @@ export declare function schedule(options?: ScheduleModuleOptions): import("@swir
1173
1200
  readonly effectId: string;
1174
1201
  readonly attempt: number;
1175
1202
  };
1203
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1204
+ readonly type: "runtime.effect.dispatch.deferred";
1205
+ readonly payload: {
1206
+ readonly effectId: string;
1207
+ readonly attempt: number;
1208
+ readonly retryAt: number;
1209
+ readonly error: string;
1210
+ readonly try: number;
1211
+ };
1176
1212
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
1177
1213
  readonly type: "runtime.effect.dispatched";
1178
1214
  readonly payload: {
@@ -21,8 +21,8 @@ export declare function corpusModule(options?: {
21
21
  decision: 'approve' | 'reject';
22
22
  }>;
23
23
  }>, {
24
- charge: import("@swirls/looms/core").EffectDefinition<JsonValue, never, EventInput>;
25
- chargeV2: import("@swirls/looms/core").EffectDefinition<JsonValue, never, EventInput>;
24
+ charge: import("@swirls/looms/core").LocalEffectDefinition<JsonValue, never, EventInput> | import("@swirls/looms/core").WorkerEffectDefinition<JsonValue, EventInput>;
25
+ chargeV2: import("@swirls/looms/core").LocalEffectDefinition<JsonValue, never, EventInput> | import("@swirls/looms/core").WorkerEffectDefinition<JsonValue, EventInput>;
26
26
  }, {
27
27
  'corpus.checkout': import("@swirls/looms/core").ThreadDefinition<{
28
28
  readonly amount: number;
@@ -17,25 +17,33 @@ const CheckoutInputSchema = Schema.Finite;
17
17
  const decodeApproved = Schema.decodeUnknownOption(ApprovedPayloadSchema);
18
18
  const decodeCheckoutInput = Schema.decodeUnknownOption(CheckoutInputSchema);
19
19
  export function corpusModule(options) {
20
- const charge = defineEffect({
21
- type: corpusChargeV1,
22
- retry: {
23
- maxAttempts: 3,
24
- backoffMs: 1,
25
- scheduleToStartTimeoutMs: 60_000,
26
- startToCloseTimeoutMs: 60_000,
27
- },
28
- execute: options?.charge,
29
- });
30
- const chargeV2 = defineEffect({
31
- type: corpusChargeV2,
32
- retry: {
33
- maxAttempts: 1,
34
- scheduleToStartTimeoutMs: 60_000,
35
- startToCloseTimeoutMs: 60_000,
36
- },
37
- execute: options?.charge,
38
- });
20
+ const chargeRetry = {
21
+ maxAttempts: 3,
22
+ backoffMs: 1,
23
+ scheduleToStartTimeoutMs: 60_000,
24
+ startToCloseTimeoutMs: 60_000,
25
+ };
26
+ const charge = options?.charge
27
+ ? defineEffect({
28
+ type: corpusChargeV1,
29
+ placement: 'local',
30
+ retry: chargeRetry,
31
+ execute: options.charge,
32
+ })
33
+ : defineEffect({ type: corpusChargeV1, placement: 'worker', retry: chargeRetry });
34
+ const chargeV2Retry = {
35
+ maxAttempts: 1,
36
+ scheduleToStartTimeoutMs: 60_000,
37
+ startToCloseTimeoutMs: 60_000,
38
+ };
39
+ const chargeV2 = options?.charge
40
+ ? defineEffect({
41
+ type: corpusChargeV2,
42
+ placement: 'local',
43
+ retry: chargeV2Retry,
44
+ execute: options.charge,
45
+ })
46
+ : defineEffect({ type: corpusChargeV2, placement: 'worker', retry: chargeV2Retry });
39
47
  return defineRuntimeModule({
40
48
  namespace: 'corpus',
41
49
  protocolVersion: '1.0.0',
@@ -1,21 +1,7 @@
1
- import { Effect, Predicate } from 'effect';
1
+ import { Effect } from 'effect';
2
2
  import { composeModules, EventStoreTag, makeMemoryEventStore, ModuleCompositionError, EventStoreError, snapshotStoreOf, withSnapshotStore, } from '@swirls/looms/core';
3
3
  import { createRuntime } from '@swirls/looms/runtime';
4
4
  import { assertReplayDeterministicEvents } from './replay-corpus.js';
5
- function namedEffects(effects) {
6
- const listed = [];
7
- for (const key of Object.keys(effects)) {
8
- const value = effects[key];
9
- if (!value || !Predicate.isString(value.type)) {
10
- continue;
11
- }
12
- listed.push({
13
- type: value.type,
14
- hasExecute: 'execute' in value,
15
- });
16
- }
17
- return listed;
18
- }
19
5
  export function createTestRuntime(modules) {
20
6
  return Effect.runPromise(makeMemoryEventStore.pipe(Effect.map((store) => {
21
7
  const runtime = createRuntime({ modules, store });
@@ -44,14 +30,17 @@ export function moduleConformance(module) {
44
30
  if (module.events && module.events.namespace !== module.namespace) {
45
31
  errors.push(`catalog namespace ${module.events.namespace} !== module ${module.namespace}`);
46
32
  }
47
- if (module.effects) {
48
- for (const effect of namedEffects(module.effects)) {
49
- if (!effect.type.startsWith(`${module.namespace}.`)) {
50
- errors.push(`effect ${effect.type} is not namespaced under ${module.namespace}`);
51
- }
52
- if (!effect.hasExecute) {
53
- errors.push(`effect ${effect.type} is missing execute`);
54
- }
33
+ for (const effect of Object.values(module.effects ?? {})) {
34
+ const { type, placement } = effect;
35
+ const hasExecute = 'execute' in effect;
36
+ if (!type.startsWith(`${module.namespace}.`)) {
37
+ errors.push(`effect ${type} is not namespaced under ${module.namespace}`);
38
+ }
39
+ if (placement === 'local' && !hasExecute) {
40
+ errors.push(`local effect ${type} is missing execute`);
41
+ }
42
+ if (placement === 'worker' && hasExecute) {
43
+ errors.push(`worker effect ${type} must not define execute`);
55
44
  }
56
45
  }
57
46
  try {
@@ -4,7 +4,7 @@ import { type NodeState, type WorkflowDefinition } from './definitions';
4
4
  import { WorkflowDefinitionsTag } from './definitions-store';
5
5
  import { type WorkflowEvent } from './scope';
6
6
  type WorkflowEvents = EventInputOf<WorkflowEvent>[];
7
- export declare const scheduleEffect: NoInfer<import("@swirls/looms/core").EffectDefinition<{
7
+ export declare const scheduleEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
8
8
  readonly definitionName?: string | undefined;
9
9
  readonly definitionVersion: string;
10
10
  readonly nodes?: {
@@ -67,6 +67,15 @@ export declare const scheduleEffect: NoInfer<import("@swirls/looms/core").Effect
67
67
  readonly effectId: string;
68
68
  readonly attempt: number;
69
69
  };
70
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
71
+ readonly type: "runtime.effect.dispatch.deferred";
72
+ readonly payload: {
73
+ readonly effectId: string;
74
+ readonly attempt: number;
75
+ readonly retryAt: number;
76
+ readonly error: string;
77
+ readonly try: number;
78
+ };
70
79
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
71
80
  readonly type: "runtime.effect.dispatched";
72
81
  readonly payload: {
@@ -398,7 +407,7 @@ export declare function scheduleEvents(definition: WorkflowDefinition, binding:
398
407
  nodes: Record<string, NodeState>;
399
408
  input: JsonValue;
400
409
  }, threadId: string): Effect.Effect<WorkflowEvents>;
401
- export declare const runNodeEffect: NoInfer<import("@swirls/looms/core").EffectDefinition<{
410
+ export declare const runNodeEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
402
411
  readonly definitionName?: string | undefined;
403
412
  readonly definitionVersion: string;
404
413
  readonly nodeId: string;
@@ -467,6 +476,15 @@ export declare const runNodeEffect: NoInfer<import("@swirls/looms/core").EffectD
467
476
  readonly effectId: string;
468
477
  readonly attempt: number;
469
478
  };
479
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
480
+ readonly type: "runtime.effect.dispatch.deferred";
481
+ readonly payload: {
482
+ readonly effectId: string;
483
+ readonly attempt: number;
484
+ readonly retryAt: number;
485
+ readonly error: string;
486
+ readonly try: number;
487
+ };
470
488
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
471
489
  readonly type: "runtime.effect.dispatched";
472
490
  readonly payload: {
@@ -45,6 +45,7 @@ function threadFailed(threadId, error) {
45
45
  }
46
46
  export const scheduleEffect = workflowModule.effect({
47
47
  type: 'workflow.schedule',
48
+ placement: 'local',
48
49
  input: ScheduleInput,
49
50
  deterministic: true,
50
51
  execute: (input, ctx) => Effect.gen(function* () {
@@ -148,6 +149,7 @@ export function scheduleEvents(definition, binding, threadId) {
148
149
  }
149
150
  export const runNodeEffect = workflowModule.effect({
150
151
  type: 'workflow.runNode',
152
+ placement: 'local',
151
153
  input: RunNodeInput,
152
154
  execute: (input, ctx) => Effect.gen(function* () {
153
155
  const workflows = yield* WorkflowDefinitionsTag;