@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.
- package/CHANGELOG.md +24 -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 +540 -504
- package/dist/approval/effects.d.ts +36 -27
- package/dist/approval/effects.js +1 -0
- package/dist/approval/module.d.ts +20 -2
- 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 +87 -14
- 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/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 +30 -8
- 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 +40 -4
- 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 +1 -1
- package/dist/debugger/app/assets/routes-lt_6J025.js +0 -47
|
@@ -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-CMRl_aWQ.js"></script>
|
|
8
8
|
<link rel="modulepreload" crossorigin href="./assets/rolldown-runtime-CbXtAM7H.js">
|
|
9
9
|
<link rel="stylesheet" crossorigin href="./assets/index-B7GdT1UA.css">
|
|
10
10
|
</head>
|
|
@@ -94,6 +94,8 @@ export function summarizeProtocolEvent(event) {
|
|
|
94
94
|
return { title: 'effect timed out', detail: jsonText(payload.timeout) };
|
|
95
95
|
case 'runtime.effect.ambiguous':
|
|
96
96
|
return { title: 'effect outcome ambiguous', detail: jsonText(payload.error) };
|
|
97
|
+
case 'runtime.effect.dispatch.deferred':
|
|
98
|
+
return { title: 'effect dispatch deferred', detail: jsonText(payload.error) };
|
|
97
99
|
case 'runtime.effect.retry.scheduled':
|
|
98
100
|
return { title: 'effect retry scheduled', detail: jsonText(payload.error) };
|
|
99
101
|
case 'runtime.snapshot.taken':
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Schema } from 'effect';
|
|
2
2
|
import { EvaluatorDefinitionsTag } from './definitions-store';
|
|
3
3
|
import { EvaluatorTag } from './evaluator';
|
|
4
|
-
export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").
|
|
4
|
+
export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
|
|
5
5
|
readonly evaluationId: string;
|
|
6
6
|
readonly definitionName?: string | undefined;
|
|
7
7
|
readonly definitionVersion?: string | undefined;
|
|
@@ -84,6 +84,15 @@ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").Effect
|
|
|
84
84
|
readonly effectId: string;
|
|
85
85
|
readonly attempt: number;
|
|
86
86
|
};
|
|
87
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
88
|
+
readonly type: "runtime.effect.dispatch.deferred";
|
|
89
|
+
readonly payload: {
|
|
90
|
+
readonly effectId: string;
|
|
91
|
+
readonly attempt: number;
|
|
92
|
+
readonly retryAt: number;
|
|
93
|
+
readonly error: string;
|
|
94
|
+
readonly try: number;
|
|
95
|
+
};
|
|
87
96
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
88
97
|
readonly type: "runtime.effect.dispatched";
|
|
89
98
|
readonly payload: {
|
|
@@ -32,6 +32,7 @@ function failed(threadId, error) {
|
|
|
32
32
|
}
|
|
33
33
|
export const evaluateEffect = evaluatorModule.effect({
|
|
34
34
|
type: 'evaluator.evaluate',
|
|
35
|
+
placement: 'local',
|
|
35
36
|
input: EvaluateInput,
|
|
36
37
|
execute: (input, ctx) => Effect.gen(function* () {
|
|
37
38
|
const threadId = ctx.threadId;
|
|
@@ -36,7 +36,7 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
36
36
|
}>>;
|
|
37
37
|
}>;
|
|
38
38
|
}>, {
|
|
39
|
-
evaluate: import("@swirls/looms/core").
|
|
39
|
+
evaluate: import("@swirls/looms/core").LocalEffectDefinition<{
|
|
40
40
|
readonly evaluationId: string;
|
|
41
41
|
readonly definitionName?: string | undefined;
|
|
42
42
|
readonly definitionVersion?: string | undefined;
|
|
@@ -119,6 +119,15 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
119
119
|
readonly effectId: string;
|
|
120
120
|
readonly attempt: number;
|
|
121
121
|
};
|
|
122
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
123
|
+
readonly type: "runtime.effect.dispatch.deferred";
|
|
124
|
+
readonly payload: {
|
|
125
|
+
readonly effectId: string;
|
|
126
|
+
readonly attempt: number;
|
|
127
|
+
readonly retryAt: number;
|
|
128
|
+
readonly error: string;
|
|
129
|
+
readonly try: number;
|
|
130
|
+
};
|
|
122
131
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
123
132
|
readonly type: "runtime.effect.dispatched";
|
|
124
133
|
readonly payload: {
|
|
@@ -373,7 +382,7 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
373
382
|
}>;
|
|
374
383
|
}, import("effect/Context").Service.Any> & {
|
|
375
384
|
readonly effects: {
|
|
376
|
-
evaluate: import("@swirls/looms/core").
|
|
385
|
+
evaluate: import("@swirls/looms/core").LocalEffectDefinition<{
|
|
377
386
|
readonly evaluationId: string;
|
|
378
387
|
readonly definitionName?: string | undefined;
|
|
379
388
|
readonly definitionVersion?: string | undefined;
|
|
@@ -456,6 +465,15 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
456
465
|
readonly effectId: string;
|
|
457
466
|
readonly attempt: number;
|
|
458
467
|
};
|
|
468
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
469
|
+
readonly type: "runtime.effect.dispatch.deferred";
|
|
470
|
+
readonly payload: {
|
|
471
|
+
readonly effectId: string;
|
|
472
|
+
readonly attempt: number;
|
|
473
|
+
readonly retryAt: number;
|
|
474
|
+
readonly error: string;
|
|
475
|
+
readonly try: number;
|
|
476
|
+
};
|
|
459
477
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
460
478
|
readonly type: "runtime.effect.dispatched";
|
|
461
479
|
readonly payload: {
|
|
@@ -26,7 +26,7 @@ function planCancellation(input) {
|
|
|
26
26
|
!isWorkerEffect({ runId, item, execution, registry, worker: input.worker })) {
|
|
27
27
|
continue;
|
|
28
28
|
}
|
|
29
|
-
if (execution.status === 'retry_wait') {
|
|
29
|
+
if (execution.status === 'retry_wait' || execution.status === 'dispatch_wait') {
|
|
30
30
|
effectEvents.push(createEvent(runId, {
|
|
31
31
|
type: 'runtime.effect.cancelled',
|
|
32
32
|
payload: { effectId: item.effectId, attempt: execution.attempt },
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Context, ManagedRuntime } from 'effect';
|
|
2
2
|
import { Effect } from 'effect';
|
|
3
|
-
import { type ComposedRegistry, type
|
|
4
|
-
export declare function dispatchEffect(registry: ComposedRegistry, services: ManagedRuntime.ManagedRuntime<Context.Service.Any, never>, definitions: DefinitionResolver, launcher: RunLauncher, effect: RuntimeEffect, ctx:
|
|
3
|
+
import { type ComposedRegistry, type LocalEffectContext, type EventInput, type DefinitionResolver, type RunLauncher, type RuntimeEffect } from '@swirls/looms/core';
|
|
4
|
+
export declare function dispatchEffect(registry: ComposedRegistry, services: ManagedRuntime.ManagedRuntime<Context.Service.Any, never>, definitions: DefinitionResolver, launcher: RunLauncher, effect: RuntimeEffect, ctx: LocalEffectContext): Effect.Effect<ReadonlyArray<EventInput>, Error>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Predicate } from 'effect';
|
|
2
|
-
import { asJson, isPrimitiveEffect, isWaitOnTimer, readEffectFailure, RunLauncherTag, } from '@swirls/looms/core';
|
|
2
|
+
import { asJson, EffectFailure, isLocalEffectDefinition, isPrimitiveEffect, isWaitOnTimer, readEffectFailure, RunLauncherTag, } from '@swirls/looms/core';
|
|
3
3
|
import { threadStartedEvents } from './helpers.js';
|
|
4
4
|
function causeMessage(cause) {
|
|
5
5
|
if (Predicate.isString(cause)) {
|
|
@@ -108,16 +108,24 @@ export function dispatchEffect(registry, services, definitions, launcher, effect
|
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
const
|
|
112
|
-
if (!
|
|
111
|
+
const definition = registry.effects.get(eff.type);
|
|
112
|
+
if (!definition) {
|
|
113
113
|
return Effect.succeed([
|
|
114
114
|
failedAttempt(effCtx.effectId, currentThreadId, `No handler for ${eff.type}`),
|
|
115
115
|
]);
|
|
116
116
|
}
|
|
117
|
+
if (!isLocalEffectDefinition(definition)) {
|
|
118
|
+
return Effect.succeed([
|
|
119
|
+
failedAttempt(effCtx.effectId, currentThreadId, new EffectFailure({
|
|
120
|
+
error: `${eff.type} is worker-placed; the host cannot run it`,
|
|
121
|
+
nonRetryable: true,
|
|
122
|
+
})),
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
117
125
|
const input = 'input' in eff ? eff.input : {};
|
|
118
126
|
const execution = Effect.gen(function* () {
|
|
119
127
|
const serviceContext = yield* services.contextEffect;
|
|
120
|
-
return yield*
|
|
128
|
+
return yield* definition
|
|
121
129
|
.execute(input, effCtx)
|
|
122
130
|
.pipe(Effect.provideService(RunLauncherTag, launcher), Effect.provide(serviceContext));
|
|
123
131
|
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
import { type ComposedRegistry, type EventEnvelope, type OutstandingEffect, type RetryPolicy, type RunAttributes, type SecretContext, type TraceContext } from '@swirls/looms/core';
|
|
3
|
+
import type { RuntimeObservation } from './observer';
|
|
4
|
+
export type AdmissionDecision = {
|
|
5
|
+
readonly _tag: 'admit';
|
|
6
|
+
} | {
|
|
7
|
+
readonly _tag: 'reject';
|
|
8
|
+
readonly error: string;
|
|
9
|
+
readonly nonRetryable: boolean;
|
|
10
|
+
readonly retryAfterMs?: number;
|
|
11
|
+
};
|
|
12
|
+
export type AdmitOrFailResult = {
|
|
13
|
+
readonly _tag: 'admit';
|
|
14
|
+
} | {
|
|
15
|
+
readonly _tag: 'reject';
|
|
16
|
+
readonly event: EventEnvelope;
|
|
17
|
+
readonly retrying: boolean;
|
|
18
|
+
/** Present when the reject schedules another attempt under the retry policy. */
|
|
19
|
+
readonly observation?: RuntimeObservation;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Run every composed admission hook before a local attempt or a worker dispatch.
|
|
23
|
+
* A `failEffect` (or other thrown/failed cause) becomes a reject decision.
|
|
24
|
+
*/
|
|
25
|
+
export declare function runEffectAdmission(registry: ComposedRegistry, item: OutstandingEffect, context: {
|
|
26
|
+
readonly runId: string;
|
|
27
|
+
readonly attributes: RunAttributes;
|
|
28
|
+
readonly secrets: SecretContext;
|
|
29
|
+
readonly trace: TraceContext | null;
|
|
30
|
+
}): Effect.Effect<AdmissionDecision>;
|
|
31
|
+
/**
|
|
32
|
+
* Gate an attempt, then turn a reject into the durable failure/retry event both the local
|
|
33
|
+
* executor and the wake engine append. Call sites only append the event (and notify observer).
|
|
34
|
+
*/
|
|
35
|
+
export declare function admitOrFailAttempt(input: {
|
|
36
|
+
readonly registry: ComposedRegistry;
|
|
37
|
+
readonly item: OutstandingEffect;
|
|
38
|
+
readonly runId: string;
|
|
39
|
+
readonly attempt: number;
|
|
40
|
+
readonly now: number;
|
|
41
|
+
readonly attributes: RunAttributes;
|
|
42
|
+
readonly secrets: SecretContext;
|
|
43
|
+
readonly trace: TraceContext | null;
|
|
44
|
+
readonly retry: RetryPolicy | undefined;
|
|
45
|
+
}): Effect.Effect<AdmitOrFailResult>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
import { isPrimitiveEffect, readEffectFailure, } from '@swirls/looms/core';
|
|
3
|
+
import { planAttemptFailure } from './retry-policy.js';
|
|
4
|
+
/**
|
|
5
|
+
* Run every composed admission hook before a local attempt or a worker dispatch.
|
|
6
|
+
* A `failEffect` (or other thrown/failed cause) becomes a reject decision.
|
|
7
|
+
*/
|
|
8
|
+
export function runEffectAdmission(registry, item, context) {
|
|
9
|
+
if (isPrimitiveEffect(item.effect) || registry.admission.length === 0) {
|
|
10
|
+
return Effect.succeed({ _tag: 'admit' });
|
|
11
|
+
}
|
|
12
|
+
const admissionCtx = {
|
|
13
|
+
effectId: item.effectId,
|
|
14
|
+
runId: context.runId,
|
|
15
|
+
threadId: item.threadId,
|
|
16
|
+
causingEventId: item.causingEventId,
|
|
17
|
+
attributes: context.attributes,
|
|
18
|
+
secrets: context.secrets,
|
|
19
|
+
trace: context.trace,
|
|
20
|
+
};
|
|
21
|
+
const effect = item.effect;
|
|
22
|
+
return Effect.forEach(registry.admission, (hook) => hook(effect, admissionCtx), {
|
|
23
|
+
discard: true,
|
|
24
|
+
}).pipe(Effect.as({ _tag: 'admit' }), Effect.catch((cause) => {
|
|
25
|
+
const failure = readEffectFailure(cause);
|
|
26
|
+
return Effect.succeed({
|
|
27
|
+
_tag: 'reject',
|
|
28
|
+
error: failure?.error ?? (cause instanceof Error ? cause.message : String(cause)),
|
|
29
|
+
nonRetryable: failure?.nonRetryable === true,
|
|
30
|
+
retryAfterMs: failure?.retryAfterMs,
|
|
31
|
+
});
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Gate an attempt, then turn a reject into the durable failure/retry event both the local
|
|
36
|
+
* executor and the wake engine append. Call sites only append the event (and notify observer).
|
|
37
|
+
*/
|
|
38
|
+
export function admitOrFailAttempt(input) {
|
|
39
|
+
return runEffectAdmission(input.registry, input.item, input).pipe(Effect.map((decision) => {
|
|
40
|
+
if (decision._tag === 'admit') {
|
|
41
|
+
return { _tag: 'admit' };
|
|
42
|
+
}
|
|
43
|
+
const failure = planAttemptFailure({
|
|
44
|
+
runId: input.runId,
|
|
45
|
+
item: input.item,
|
|
46
|
+
attempt: input.attempt,
|
|
47
|
+
error: decision.error,
|
|
48
|
+
now: input.now,
|
|
49
|
+
retry: input.retry,
|
|
50
|
+
origin: { type: 'system' },
|
|
51
|
+
nonRetryable: decision.nonRetryable,
|
|
52
|
+
retryAfterMs: decision.retryAfterMs,
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
_tag: 'reject',
|
|
56
|
+
event: failure.event,
|
|
57
|
+
retrying: failure.retrying,
|
|
58
|
+
observation: failure.retrying
|
|
59
|
+
? {
|
|
60
|
+
type: 'effect.retry',
|
|
61
|
+
runId: input.runId,
|
|
62
|
+
at: input.now,
|
|
63
|
+
effectId: input.item.effectId,
|
|
64
|
+
attempt: input.attempt,
|
|
65
|
+
detail: decision.error,
|
|
66
|
+
}
|
|
67
|
+
: undefined,
|
|
68
|
+
};
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
@@ -7,6 +7,9 @@ export interface WorkerTaskExtras {
|
|
|
7
7
|
readonly details?: JsonValue | null;
|
|
8
8
|
readonly secrets?: SecretContext;
|
|
9
9
|
readonly trace?: TraceContext | null;
|
|
10
|
+
readonly attributes?: {
|
|
11
|
+
readonly [key: string]: string;
|
|
12
|
+
};
|
|
10
13
|
readonly retry?: RetryPolicy;
|
|
11
14
|
readonly scheduleToStartAt?: number | null;
|
|
12
15
|
}
|
|
@@ -59,7 +62,9 @@ export declare function createEffectFailedEvent(runId: string, item: {
|
|
|
59
62
|
effectId: string;
|
|
60
63
|
causingEventId: string;
|
|
61
64
|
threadId: string;
|
|
62
|
-
}, error: string
|
|
65
|
+
}, error: string, options?: {
|
|
66
|
+
readonly nonRetryable?: boolean;
|
|
67
|
+
}): EventEnvelope;
|
|
63
68
|
export declare function materializeEffectOutcome(catalogs: readonly EventCatalog[], runId: string, item: {
|
|
64
69
|
effectId: string;
|
|
65
70
|
causingEventId: string;
|
package/dist/runtime/helpers.js
CHANGED
|
@@ -7,7 +7,12 @@ export function workerTask(runId, item, attempt, extras = {}) {
|
|
|
7
7
|
attempt,
|
|
8
8
|
type: item.effect.type,
|
|
9
9
|
input: 'input' in item.effect ? item.effect.input : null,
|
|
10
|
+
threadId: item.threadId,
|
|
11
|
+
causingEventId: item.causingEventId,
|
|
10
12
|
};
|
|
13
|
+
if (extras.attributes && hasEntries(extras.attributes)) {
|
|
14
|
+
task.attributes = extras.attributes;
|
|
15
|
+
}
|
|
11
16
|
if (extras.details != null) {
|
|
12
17
|
task.details = extras.details;
|
|
13
18
|
}
|
|
@@ -266,13 +271,17 @@ export function waitSatisfiedEvents(state, events) {
|
|
|
266
271
|
}
|
|
267
272
|
return produced;
|
|
268
273
|
}
|
|
269
|
-
export function createEffectFailedEvent(runId, item, error) {
|
|
274
|
+
export function createEffectFailedEvent(runId, item, error, options = {}) {
|
|
275
|
+
const payload = {
|
|
276
|
+
effectId: item.effectId,
|
|
277
|
+
error,
|
|
278
|
+
};
|
|
279
|
+
if (options.nonRetryable) {
|
|
280
|
+
payload.nonRetryable = true;
|
|
281
|
+
}
|
|
270
282
|
return createEvent(runId, {
|
|
271
283
|
type: 'runtime.effect.failed',
|
|
272
|
-
payload
|
|
273
|
-
effectId: item.effectId,
|
|
274
|
-
error,
|
|
275
|
-
},
|
|
284
|
+
payload,
|
|
276
285
|
threadId: item.threadId,
|
|
277
286
|
effectId: item.effectId,
|
|
278
287
|
causationId: item.causingEventId,
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { createRuntime, createTimeoutScheduler, DuplicateEffectDispatchError, ForkConflictError, MaxWakeIterationsError, RunNotFinishedError, RunNotFoundError, RunOverloadedError, RunTerminalError, StartRunConflictError, type CreateRuntimeOptions, type EffectWorker, type EffectWorkerDeadlines, type EffectWorkerTask, type LoomsRuntime, type RunOperationalStatus, type RegisteredDefinition, type StartRunArgs, type WakeError, type WakeScheduler, type WorkerCallback, type WorkerCallbackInput, type WorkerCallbackResult, } from './runtime';
|
|
2
|
+
export { listEffectPlacements } from './worker-effect-dispatch';
|
|
2
3
|
export { createLooms, type Looms, type CreateLoomsOptions, type StartResult } from './looms';
|
|
3
4
|
export { loadSubscriptionOpening, readSubscriptionBounds } from './subscription';
|
|
4
5
|
export { serveHttp, createFetchHandler, isLoomsApiPath, type FetchHandlerOptions, authAllowed, authDenied, authResponse, bearerAuth, defaultAuthorize, grantAuth, issueRunGrant, grantSubprotocol, runGrantToken, verifyRunGrant, GrantError, type GrantAuthOptions, type GrantFailure, type RunGrant, runRouteInfo, type AuthDecision, type AuthorizedAccess, type Authorize, type BearerTokens, type RouteAccess, type RouteInfo, type RunningServer, type LoomsFetchResult, } from './server';
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { createRuntime, createTimeoutScheduler, DuplicateEffectDispatchError, ForkConflictError, MaxWakeIterationsError, RunNotFinishedError, RunNotFoundError, RunOverloadedError, RunTerminalError, StartRunConflictError, } from './runtime.js';
|
|
2
|
+
export { listEffectPlacements } from './worker-effect-dispatch.js';
|
|
2
3
|
export { createLooms } from './looms.js';
|
|
3
4
|
export { loadSubscriptionOpening, readSubscriptionBounds } from './subscription.js';
|
|
4
5
|
export { serveHttp, createFetchHandler, isLoomsApiPath, authAllowed, authDenied, authResponse, bearerAuth, defaultAuthorize, grantAuth, issueRunGrant, grantSubprotocol, runGrantToken, verifyRunGrant, GrantError, runRouteInfo, } from './server.js';
|
|
@@ -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,
|
|
@@ -155,8 +177,11 @@ export function executeLocalEffects(options, input) {
|
|
|
155
177
|
continue;
|
|
156
178
|
}
|
|
157
179
|
const { outcomes } = settled;
|
|
158
|
-
|
|
159
|
-
|
|
180
|
+
// A handler that returns no events succeeded with nothing to report (a launch, a
|
|
181
|
+
// cancel, or a scheduler with nothing ready). Record the attempt as completed, as the
|
|
182
|
+
// worker path does; treating it as a failure made the thread re-request the same
|
|
183
|
+
// effect from the failure event and spin the wake loop.
|
|
184
|
+
if (outcomes.length === 0) {
|
|
160
185
|
const attempt = input.attempts.get(item.effectId) ?? 1;
|
|
161
186
|
produced.push(createEvent(input.runId, {
|
|
162
187
|
type: 'runtime.effect.completed',
|
|
@@ -167,9 +192,6 @@ export function executeLocalEffects(options, input) {
|
|
|
167
192
|
origin: { type: 'system' },
|
|
168
193
|
}));
|
|
169
194
|
}
|
|
170
|
-
else if (outcomes.length === 0) {
|
|
171
|
-
produced.push(createEffectFailedEvent(input.runId, item, 'empty-outcome'));
|
|
172
|
-
}
|
|
173
195
|
for (const outcome of outcomes) {
|
|
174
196
|
if (outcome.type === 'runtime.effect.failed') {
|
|
175
197
|
const attempt = input.attempts.get(item.effectId) ?? 1;
|
|
@@ -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);
|