@swirls/looms 0.5.1 → 0.6.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 +16 -0
- package/dist/actor/cell.d.ts +6 -1
- package/dist/actor/cell.js +1 -0
- package/dist/agent/definitions.d.ts +0 -12
- package/dist/agent/effects.js +0 -24
- package/dist/agent/module.d.ts +507 -507
- package/dist/agent/projections.d.ts +5 -0
- package/dist/agent/projections.js +5 -0
- package/dist/approval/effects.d.ts +26 -26
- package/dist/client/client.d.ts +2 -0
- package/dist/client/client.js +13 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/cloudflare/durable-object.d.ts +7 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.js +6 -1
- package/dist/core/blob-store.js +5 -1
- package/dist/core/definition-resolver.d.ts +16 -0
- package/dist/core/definition-resolver.js +23 -0
- package/dist/core/definition-version.d.ts +3 -0
- package/dist/core/definition-version.js +5 -0
- package/dist/core/effects.js +1 -1
- package/dist/core/module.d.ts +12 -3
- package/dist/core/module.js +18 -3
- package/dist/debugger/app/assets/{index-C31SnCuN.js → index-CUO1xyYW.js} +2 -2
- package/dist/debugger/app/assets/{routes-DPA3O5AF.js → routes-lt_6J025.js} +12 -12
- package/dist/debugger/app/index.html +1 -1
- package/dist/evaluator/effects.d.ts +33 -33
- package/dist/evaluator/module.d.ts +66 -66
- package/dist/react/store.d.ts +1 -1
- package/dist/react/store.js +2 -1
- package/dist/runtime/cancellation.js +21 -0
- package/dist/runtime/dispatch-effect.js +4 -1
- package/dist/runtime/filesystem-blob-store.js +17 -8
- package/dist/runtime/repository.js +2 -1
- package/dist/runtime/server/routes/runs.js +6 -11
- package/dist/runtime/sse.d.ts +3 -1
- package/dist/runtime/sse.js +5 -2
- package/dist/runtime/types.d.ts +2 -1
- package/dist/runtime/wake-engine.js +16 -11
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -1
- package/dist/runtime/worker-effect-dispatch.js +14 -5
- package/dist/schedule/module.d.ts +36 -36
- package/dist/testing/corpus-module.js +11 -2
- package/dist/workflow/threads.js +5 -8
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Looms debugger</title>
|
|
7
|
-
<script type="module" crossorigin src="./assets/index-
|
|
7
|
+
<script type="module" crossorigin src="./assets/index-CUO1xyYW.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>
|
|
@@ -7,6 +7,39 @@ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").Effect
|
|
|
7
7
|
readonly definitionVersion?: string | undefined;
|
|
8
8
|
readonly input?: Schema.Json | undefined;
|
|
9
9
|
}, EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
10
|
+
readonly type: "evaluator.evaluated";
|
|
11
|
+
readonly payload: {
|
|
12
|
+
readonly evaluationId: string;
|
|
13
|
+
readonly definitionName?: string | undefined;
|
|
14
|
+
readonly answers: {
|
|
15
|
+
readonly [x: string]: {
|
|
16
|
+
readonly type: "boolean";
|
|
17
|
+
readonly value: boolean;
|
|
18
|
+
readonly probability: number;
|
|
19
|
+
} | {
|
|
20
|
+
readonly type: "choice";
|
|
21
|
+
readonly value: string;
|
|
22
|
+
} | {
|
|
23
|
+
readonly type: "score";
|
|
24
|
+
readonly value: number;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
readonly route: string;
|
|
28
|
+
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
29
|
+
readonly usage?: {
|
|
30
|
+
readonly input: number;
|
|
31
|
+
readonly output: number;
|
|
32
|
+
} | undefined;
|
|
33
|
+
};
|
|
34
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
35
|
+
readonly type: "evaluator.requested";
|
|
36
|
+
readonly payload: {
|
|
37
|
+
readonly evaluationId: string;
|
|
38
|
+
readonly definitionName?: string | undefined;
|
|
39
|
+
readonly state: string;
|
|
40
|
+
readonly model?: string | undefined;
|
|
41
|
+
};
|
|
42
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
10
43
|
readonly type: "runtime.child.close.requested";
|
|
11
44
|
readonly payload: {
|
|
12
45
|
readonly policy: "terminate" | "wait";
|
|
@@ -243,37 +276,4 @@ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").Effect
|
|
|
243
276
|
readonly payload: Schema.Json;
|
|
244
277
|
};
|
|
245
278
|
};
|
|
246
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
247
|
-
readonly type: "evaluator.evaluated";
|
|
248
|
-
readonly payload: {
|
|
249
|
-
readonly evaluationId: string;
|
|
250
|
-
readonly definitionName?: string | undefined;
|
|
251
|
-
readonly answers: {
|
|
252
|
-
readonly [x: string]: {
|
|
253
|
-
readonly type: "boolean";
|
|
254
|
-
readonly value: boolean;
|
|
255
|
-
readonly probability: number;
|
|
256
|
-
} | {
|
|
257
|
-
readonly type: "choice";
|
|
258
|
-
readonly value: string;
|
|
259
|
-
} | {
|
|
260
|
-
readonly type: "score";
|
|
261
|
-
readonly value: number;
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
readonly route: string;
|
|
265
|
-
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
266
|
-
readonly usage?: {
|
|
267
|
-
readonly input: number;
|
|
268
|
-
readonly output: number;
|
|
269
|
-
} | undefined;
|
|
270
|
-
};
|
|
271
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
272
|
-
readonly type: "evaluator.requested";
|
|
273
|
-
readonly payload: {
|
|
274
|
-
readonly evaluationId: string;
|
|
275
|
-
readonly definitionName?: string | undefined;
|
|
276
|
-
readonly state: string;
|
|
277
|
-
readonly model?: string | undefined;
|
|
278
|
-
};
|
|
279
279
|
})>>;
|
|
@@ -42,6 +42,39 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
42
42
|
readonly definitionVersion?: string | undefined;
|
|
43
43
|
readonly input?: import("effect/Schema").Json | undefined;
|
|
44
44
|
}, import("./definitions-store").EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
45
|
+
readonly type: "evaluator.evaluated";
|
|
46
|
+
readonly payload: {
|
|
47
|
+
readonly evaluationId: string;
|
|
48
|
+
readonly definitionName?: string | undefined;
|
|
49
|
+
readonly answers: {
|
|
50
|
+
readonly [x: string]: {
|
|
51
|
+
readonly type: "boolean";
|
|
52
|
+
readonly value: boolean;
|
|
53
|
+
readonly probability: number;
|
|
54
|
+
} | {
|
|
55
|
+
readonly type: "choice";
|
|
56
|
+
readonly value: string;
|
|
57
|
+
} | {
|
|
58
|
+
readonly type: "score";
|
|
59
|
+
readonly value: number;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
readonly route: string;
|
|
63
|
+
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
64
|
+
readonly usage?: {
|
|
65
|
+
readonly input: number;
|
|
66
|
+
readonly output: number;
|
|
67
|
+
} | undefined;
|
|
68
|
+
};
|
|
69
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
70
|
+
readonly type: "evaluator.requested";
|
|
71
|
+
readonly payload: {
|
|
72
|
+
readonly evaluationId: string;
|
|
73
|
+
readonly definitionName?: string | undefined;
|
|
74
|
+
readonly state: string;
|
|
75
|
+
readonly model?: string | undefined;
|
|
76
|
+
};
|
|
77
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
45
78
|
readonly type: "runtime.child.close.requested";
|
|
46
79
|
readonly payload: {
|
|
47
80
|
readonly policy: "terminate" | "wait";
|
|
@@ -278,39 +311,6 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
278
311
|
readonly payload: import("effect/Schema").Json;
|
|
279
312
|
};
|
|
280
313
|
};
|
|
281
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
282
|
-
readonly type: "evaluator.evaluated";
|
|
283
|
-
readonly payload: {
|
|
284
|
-
readonly evaluationId: string;
|
|
285
|
-
readonly definitionName?: string | undefined;
|
|
286
|
-
readonly answers: {
|
|
287
|
-
readonly [x: string]: {
|
|
288
|
-
readonly type: "boolean";
|
|
289
|
-
readonly value: boolean;
|
|
290
|
-
readonly probability: number;
|
|
291
|
-
} | {
|
|
292
|
-
readonly type: "choice";
|
|
293
|
-
readonly value: string;
|
|
294
|
-
} | {
|
|
295
|
-
readonly type: "score";
|
|
296
|
-
readonly value: number;
|
|
297
|
-
};
|
|
298
|
-
};
|
|
299
|
-
readonly route: string;
|
|
300
|
-
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
301
|
-
readonly usage?: {
|
|
302
|
-
readonly input: number;
|
|
303
|
-
readonly output: number;
|
|
304
|
-
} | undefined;
|
|
305
|
-
};
|
|
306
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
307
|
-
readonly type: "evaluator.requested";
|
|
308
|
-
readonly payload: {
|
|
309
|
-
readonly evaluationId: string;
|
|
310
|
-
readonly definitionName?: string | undefined;
|
|
311
|
-
readonly state: string;
|
|
312
|
-
readonly model?: string | undefined;
|
|
313
|
-
};
|
|
314
314
|
})>;
|
|
315
315
|
}, {
|
|
316
316
|
evaluator: import("@swirls/looms/core").ThreadDefinition<{
|
|
@@ -379,6 +379,39 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
379
379
|
readonly definitionVersion?: string | undefined;
|
|
380
380
|
readonly input?: import("effect/Schema").Json | undefined;
|
|
381
381
|
}, import("./definitions-store").EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
382
|
+
readonly type: "evaluator.evaluated";
|
|
383
|
+
readonly payload: {
|
|
384
|
+
readonly evaluationId: string;
|
|
385
|
+
readonly definitionName?: string | undefined;
|
|
386
|
+
readonly answers: {
|
|
387
|
+
readonly [x: string]: {
|
|
388
|
+
readonly type: "boolean";
|
|
389
|
+
readonly value: boolean;
|
|
390
|
+
readonly probability: number;
|
|
391
|
+
} | {
|
|
392
|
+
readonly type: "choice";
|
|
393
|
+
readonly value: string;
|
|
394
|
+
} | {
|
|
395
|
+
readonly type: "score";
|
|
396
|
+
readonly value: number;
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
readonly route: string;
|
|
400
|
+
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
401
|
+
readonly usage?: {
|
|
402
|
+
readonly input: number;
|
|
403
|
+
readonly output: number;
|
|
404
|
+
} | undefined;
|
|
405
|
+
};
|
|
406
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
407
|
+
readonly type: "evaluator.requested";
|
|
408
|
+
readonly payload: {
|
|
409
|
+
readonly evaluationId: string;
|
|
410
|
+
readonly definitionName?: string | undefined;
|
|
411
|
+
readonly state: string;
|
|
412
|
+
readonly model?: string | undefined;
|
|
413
|
+
};
|
|
414
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
382
415
|
readonly type: "runtime.child.close.requested";
|
|
383
416
|
readonly payload: {
|
|
384
417
|
readonly policy: "terminate" | "wait";
|
|
@@ -615,39 +648,6 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
|
|
|
615
648
|
readonly payload: import("effect/Schema").Json;
|
|
616
649
|
};
|
|
617
650
|
};
|
|
618
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
619
|
-
readonly type: "evaluator.evaluated";
|
|
620
|
-
readonly payload: {
|
|
621
|
-
readonly evaluationId: string;
|
|
622
|
-
readonly definitionName?: string | undefined;
|
|
623
|
-
readonly answers: {
|
|
624
|
-
readonly [x: string]: {
|
|
625
|
-
readonly type: "boolean";
|
|
626
|
-
readonly value: boolean;
|
|
627
|
-
readonly probability: number;
|
|
628
|
-
} | {
|
|
629
|
-
readonly type: "choice";
|
|
630
|
-
readonly value: string;
|
|
631
|
-
} | {
|
|
632
|
-
readonly type: "score";
|
|
633
|
-
readonly value: number;
|
|
634
|
-
};
|
|
635
|
-
};
|
|
636
|
-
readonly route: string;
|
|
637
|
-
readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
|
|
638
|
-
readonly usage?: {
|
|
639
|
-
readonly input: number;
|
|
640
|
-
readonly output: number;
|
|
641
|
-
} | undefined;
|
|
642
|
-
};
|
|
643
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
644
|
-
readonly type: "evaluator.requested";
|
|
645
|
-
readonly payload: {
|
|
646
|
-
readonly evaluationId: string;
|
|
647
|
-
readonly definitionName?: string | undefined;
|
|
648
|
-
readonly state: string;
|
|
649
|
-
readonly model?: string | undefined;
|
|
650
|
-
};
|
|
651
651
|
})>;
|
|
652
652
|
};
|
|
653
653
|
readonly threads: {
|
package/dist/react/store.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type SubscriptionSocketConstructor } from '@swirls/looms/client';
|
|
2
2
|
import { type AnyEventEnvelope, type EventInput, type ProjectionDefinition, type RegisteredEvent, type SubscriptionTransport } from '@swirls/looms/core';
|
|
3
3
|
import { type LiveFoldDefinition } from './derived';
|
|
4
4
|
import { type CoalesceOption } from './notify';
|
package/dist/react/store.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Data, Effect, Fiber, Option, Predicate, Schema } from 'effect';
|
|
2
|
+
import { absoluteHttpBase } from '@swirls/looms/client';
|
|
2
3
|
import { decodeLoomsEvent, readSubscriptionData, stringifyJson, } from '@swirls/looms/core';
|
|
3
4
|
import { EventIndex, ProjectionCache } from './derived.js';
|
|
4
5
|
import { materializeEvents } from './materialize.js';
|
|
@@ -276,7 +277,7 @@ export function createLoomsStore(options) {
|
|
|
276
277
|
}
|
|
277
278
|
let url;
|
|
278
279
|
try {
|
|
279
|
-
const http = new URL(liveUrl());
|
|
280
|
+
const http = new URL(liveUrl(), absoluteHttpBase(endpoint));
|
|
280
281
|
http.protocol = http.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
281
282
|
url = http.toString();
|
|
282
283
|
}
|
|
@@ -101,6 +101,27 @@ export function createCancellation(options) {
|
|
|
101
101
|
idempotencyKey: `signal:child-close:${current.state.rootThreadId ?? runId}`,
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
+
// A prior wait-close may have recorded close.requested then failed mid-cancel.
|
|
105
|
+
// Keep cancelling remaining wait children on later cancel attempts.
|
|
106
|
+
const retryChildIds = rootCancel && current.state.childCloseSent
|
|
107
|
+
? current.state.launchedRuns
|
|
108
|
+
.filter((child) => child.close === 'wait')
|
|
109
|
+
.map((child) => child.runId)
|
|
110
|
+
: [];
|
|
111
|
+
if (retryChildIds.length > 0) {
|
|
112
|
+
return Effect.succeed({
|
|
113
|
+
events: [],
|
|
114
|
+
value: {
|
|
115
|
+
plan: {
|
|
116
|
+
events: [],
|
|
117
|
+
threadIds: new Set(),
|
|
118
|
+
workerCancellations: [],
|
|
119
|
+
},
|
|
120
|
+
now,
|
|
121
|
+
childRunIds: retryChildIds,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
104
125
|
const plan = planCancellation({
|
|
105
126
|
runId,
|
|
106
127
|
seq: current.seq,
|
|
@@ -125,7 +125,10 @@ export function dispatchEffect(registry, services, definitions, launcher, effect
|
|
|
125
125
|
};
|
|
126
126
|
if (registry.middleware && registry.middleware.length > 0) {
|
|
127
127
|
const pipeline = registry.middleware.reduceRight((next, mw) => (e, c) => mw(e, c, next), baseDispatch);
|
|
128
|
-
|
|
128
|
+
// Middleware may call `failEffect` (or otherwise fail) before `next`. Catch those the same
|
|
129
|
+
// way handler failures are caught inside `baseDispatch`, so a deliberate gate becomes
|
|
130
|
+
// `runtime.effect.failed` instead of failing the wake.
|
|
131
|
+
return pipeline(effect, ctx).pipe(Effect.catch((err) => Effect.succeed([failedAttempt(ctx.effectId, currentThreadId, err)])));
|
|
129
132
|
}
|
|
130
133
|
return baseDispatch(effect, ctx);
|
|
131
134
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
2
3
|
import { Data, Effect, Predicate } from 'effect';
|
|
3
4
|
import { blobStoreFromBytes, BlobStoreError } from '@swirls/looms/core';
|
|
4
5
|
class MissingBlobFile extends Data.TaggedError('MissingBlobFile') {
|
|
@@ -13,13 +14,21 @@ export function filesystemBlobStore(directory) {
|
|
|
13
14
|
function filesystemBlobBytes(directory) {
|
|
14
15
|
return {
|
|
15
16
|
read: (key) => readOptional(`${directory}/${key}`, key),
|
|
16
|
-
write: (key, body) =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
write: (key, body) => {
|
|
18
|
+
const folder = `${directory}/${folderOf(key)}`;
|
|
19
|
+
const target = `${directory}/${key}`;
|
|
20
|
+
const temp = join(folder, `.${key.slice(key.lastIndexOf('/') + 1)}.${process.pid}.tmp`);
|
|
21
|
+
return Effect.tryPromise({
|
|
22
|
+
try: () => mkdir(folder, { recursive: true }),
|
|
23
|
+
catch: (cause) => new BlobStoreError(`Unable to create a directory for blob key ${key}`, cause),
|
|
24
|
+
}).pipe(Effect.andThen(Effect.tryPromise({
|
|
25
|
+
try: () => writeFile(temp, body),
|
|
26
|
+
catch: (cause) => new BlobStoreError(`Unable to write blob key ${key}`, cause),
|
|
27
|
+
})), Effect.andThen(Effect.tryPromise({
|
|
28
|
+
try: () => rename(temp, target),
|
|
29
|
+
catch: (cause) => new BlobStoreError(`Unable to finalize blob key ${key}`, cause),
|
|
30
|
+
})));
|
|
31
|
+
},
|
|
23
32
|
remove: (key) => Effect.tryPromise({
|
|
24
33
|
try: () => rm(`${directory}/${key}`, { force: true }),
|
|
25
34
|
catch: (cause) => new BlobStoreError(`Unable to delete blob key ${key}`, cause),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Clock, Data, Effect, Option } from 'effect';
|
|
2
|
-
import { advanceCursor, assignSequences, createCoalescingAppender, emptyCursor, EventStoreTrimmedError, hashRunState, primePinnedDefinitions, summarizeRun, trimEventStoreSafely, } from '@swirls/looms/core';
|
|
2
|
+
import { advanceCursor, assignSequences, createCoalescingAppender, emptyCursor, EventStoreTrimmedError, hashRunState, primePinnedDefinitions, primePinnedDefinitionsFromState, summarizeRun, trimEventStoreSafely, } from '@swirls/looms/core';
|
|
3
3
|
import { RuntimeExecutionError } from './errors.js';
|
|
4
4
|
import { stripSeq } from './helpers.js';
|
|
5
5
|
import { notifyObserver } from './observer.js';
|
|
@@ -129,6 +129,7 @@ export function createRuntimeRepository(options) {
|
|
|
129
129
|
seq: snap.value.cursor,
|
|
130
130
|
durableSinceSnapshot: 0,
|
|
131
131
|
};
|
|
132
|
+
yield* primePinnedDefinitionsFromState(definitionResolver, snap.value.state);
|
|
132
133
|
}
|
|
133
134
|
else {
|
|
134
135
|
observe({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema } from 'effect';
|
|
2
|
-
import { createRunId, EventStoreTag, JsonValueSchema, RunAttributesSchema, readTraceHeaders, SecretContextSchema, stringifyJson, } from '@swirls/looms/core';
|
|
2
|
+
import { createRunId, EventStoreTag, JsonValueSchema, RunAttributesSchema, readTraceHeaders, SecretContextSchema, stringifyJson, subscriptionFilter, subscriptionFromSeq, } from '@swirls/looms/core';
|
|
3
3
|
import { createEventStreamResponse } from '../../sse.js';
|
|
4
4
|
import { acceptsEventStream, EventInputBodySchema, extractIdempotencyKey, provideStore, readIntegerQuery, readJson, readOptionalJson, RequestQueryError, } from '../http.js';
|
|
5
5
|
import { route } from '../route-table.js';
|
|
@@ -35,17 +35,11 @@ function wantsEventStream(req, url) {
|
|
|
35
35
|
return live === 'true' || live === '1' || acceptsEventStream(req);
|
|
36
36
|
}
|
|
37
37
|
function readEventStreamFromSeq(req, url) {
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
if (!Number.isSafeInteger(value) || value < 0) {
|
|
42
|
-
return Effect.fail(new RequestQueryError({
|
|
43
|
-
message: 'Last-Event-ID must be a non-negative integer',
|
|
44
|
-
}));
|
|
45
|
-
}
|
|
46
|
-
return Effect.succeed(value + 1);
|
|
38
|
+
const parsed = subscriptionFromSeq(url, req.headers.get('last-event-id'));
|
|
39
|
+
if (!parsed.ok) {
|
|
40
|
+
return Effect.fail(new RequestQueryError({ message: parsed.message }));
|
|
47
41
|
}
|
|
48
|
-
return
|
|
42
|
+
return Effect.succeed(parsed.fromSeq);
|
|
49
43
|
}
|
|
50
44
|
function streamStartRun(req, runtime, store, args) {
|
|
51
45
|
return createEventStreamResponse({
|
|
@@ -161,6 +155,7 @@ export function runRoutes({ runtime, store }) {
|
|
|
161
155
|
store,
|
|
162
156
|
runId: params.runId,
|
|
163
157
|
fromSeq: yield* readEventStreamFromSeq(req, url),
|
|
158
|
+
filter: subscriptionFilter(url),
|
|
164
159
|
});
|
|
165
160
|
}
|
|
166
161
|
const fromSeq = (yield* readIntegerQuery(url, 'fromSeq', { minimum: 1 })) ?? undefined;
|
package/dist/runtime/sse.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
import { type EventStore } from '@swirls/looms/core';
|
|
2
|
+
import { type EventStore, type SubscriptionFilter } from '@swirls/looms/core';
|
|
3
3
|
export interface EventStreamOptions {
|
|
4
4
|
signal: AbortSignal;
|
|
5
5
|
store: EventStore;
|
|
6
6
|
runId: string;
|
|
7
7
|
fromSeq?: number;
|
|
8
|
+
/** When set, only matching events are written to the stream. Defaults to all events. */
|
|
9
|
+
filter?: SubscriptionFilter;
|
|
8
10
|
heartbeatMs?: number;
|
|
9
11
|
onStart?: (write: (text: string) => void, close: () => void) => void | Promise<void> | Effect.Effect<void>;
|
|
10
12
|
request?: Request;
|
package/dist/runtime/sse.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Fiber, Schedule, Stream } from 'effect';
|
|
2
|
-
import { encodeLoomsEvent, encodeSubscriptionFrame, } from '@swirls/looms/core';
|
|
2
|
+
import { encodeLoomsEvent, encodeSubscriptionFrame, eventMatchesSubscription, } from '@swirls/looms/core';
|
|
3
3
|
import { readSubscriptionBounds } from './subscription.js';
|
|
4
4
|
function disableBunSocketTimeout(request) {
|
|
5
5
|
if (!request) {
|
|
@@ -22,7 +22,7 @@ function disableBunSocketTimeout(request) {
|
|
|
22
22
|
* Handles heartbeats, fiber cancellation on disconnect, event encoding, and optional lifecycle hooks.
|
|
23
23
|
*/
|
|
24
24
|
export function createEventStreamResponse(options) {
|
|
25
|
-
const { signal, store, runId, fromSeq = 1, heartbeatMs = 3_000, onStart, request } = options;
|
|
25
|
+
const { signal, store, runId, fromSeq = 1, filter = { threadId: null, includeEphemeral: true }, heartbeatMs = 3_000, onStart, request, } = options;
|
|
26
26
|
disableBunSocketTimeout(request);
|
|
27
27
|
let closed = false;
|
|
28
28
|
let heartbeatFiber;
|
|
@@ -86,6 +86,9 @@ export function createEventStreamResponse(options) {
|
|
|
86
86
|
tail: bounds.tail,
|
|
87
87
|
})}\n\n`);
|
|
88
88
|
yield* store.subscribe(runId, { fromSeq }).pipe(Stream.tap((event) => Effect.sync(() => {
|
|
89
|
+
if (!eventMatchesSubscription(event, filter)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
89
92
|
write(`id: ${event.seq}\ndata: ${encodeSubscriptionFrame({
|
|
90
93
|
type: 'batch',
|
|
91
94
|
batch: [encodeLoomsEvent(event)],
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -95,7 +95,8 @@ export interface EffectWorker {
|
|
|
95
95
|
* arrived, so the attempt waits for its schedule-to-start timeout or an operator. With
|
|
96
96
|
* `'acknowledged'` a rejection means the transport refused it, so the attempt fails under the
|
|
97
97
|
* retry policy. A transport can still lose an accepted message, so every effect such a worker
|
|
98
|
-
* claims must set `retry.scheduleToStartTimeoutMs
|
|
98
|
+
* claims must set `retry.scheduleToStartTimeoutMs`, and either `startToCloseTimeoutMs` or
|
|
99
|
+
* `heartbeatTimeoutMs` so a lost terminal callback cannot leave the attempt running forever.
|
|
99
100
|
*/
|
|
100
101
|
readonly dispatchSemantics?: 'unacknowledged' | 'acknowledged';
|
|
101
102
|
}
|
|
@@ -21,17 +21,22 @@ export function createWakeEngine(options) {
|
|
|
21
21
|
const closeLaunchedRuns = (store, runId, cursor) => Effect.gen(function* () {
|
|
22
22
|
let next = cursor;
|
|
23
23
|
const state = next.state;
|
|
24
|
-
if (isRunTerminal(state) &&
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
if (isRunTerminal(state) && state.launchedRuns.length > 0) {
|
|
25
|
+
// Record close.requested once so closeWaiting / terminate policy is durable, then cancel
|
|
26
|
+
// every remaining child. Retries keep cancelling while launchedRuns is non-empty even after
|
|
27
|
+
// childCloseSent, so a partial cancel failure cannot orphan the rest.
|
|
28
|
+
if (!state.childCloseSent) {
|
|
29
|
+
const requested = createEvent(runId, {
|
|
30
|
+
type: 'runtime.child.close.requested',
|
|
31
|
+
payload: { policy: 'terminate' },
|
|
32
|
+
threadId: state.rootThreadId,
|
|
33
|
+
origin: { type: 'system' },
|
|
34
|
+
idempotencyKey: `child-close:${runId}`,
|
|
35
|
+
});
|
|
36
|
+
const appended = yield* appendObserved(store, runId, stripSeq([requested]));
|
|
37
|
+
next = yield* applyAppend(store, runId, next, stripSeq([requested]), appended);
|
|
38
|
+
}
|
|
39
|
+
yield* Effect.forEach(next.state.launchedRuns, (child) => options.launcher().cancel(child.runId), {
|
|
35
40
|
concurrency: 1,
|
|
36
41
|
discard: true,
|
|
37
42
|
});
|
|
@@ -40,6 +40,8 @@ export declare function planWorkerEffects(input: {
|
|
|
40
40
|
export declare function dispatchWorkerEffect(runId: string, plan: WorkerEffectDispatchPlan, worker: EffectWorker | undefined, now: number): Effect.Effect<EventEnvelope>;
|
|
41
41
|
/**
|
|
42
42
|
* An acknowledged transport can lose a message it accepted, for example to a dead-letter queue.
|
|
43
|
-
*
|
|
43
|
+
* schedule-to-start covers a message that never starts. After `started` is acked, only
|
|
44
|
+
* start-to-close or a heartbeat timeout notices a lost terminal callback, so every claimed
|
|
45
|
+
* effect needs one of those as well.
|
|
44
46
|
*/
|
|
45
47
|
export declare function assertWorkerDeadlines(registry: ComposedRegistry, worker: EffectWorker | undefined): void;
|
|
@@ -167,17 +167,26 @@ export function dispatchWorkerEffect(runId, plan, worker, now) {
|
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* An acknowledged transport can lose a message it accepted, for example to a dead-letter queue.
|
|
170
|
-
*
|
|
170
|
+
* schedule-to-start covers a message that never starts. After `started` is acked, only
|
|
171
|
+
* start-to-close or a heartbeat timeout notices a lost terminal callback, so every claimed
|
|
172
|
+
* effect needs one of those as well.
|
|
171
173
|
*/
|
|
172
174
|
export function assertWorkerDeadlines(registry, worker) {
|
|
173
175
|
if (worker?.dispatchSemantics !== 'acknowledged') {
|
|
174
176
|
return;
|
|
175
177
|
}
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
+
const claimed = [...registry.effects.values()].filter((definition) => claimsType(worker, definition));
|
|
179
|
+
const missingStart = claimed
|
|
180
|
+
.filter((definition) => !definition.retry?.scheduleToStartTimeoutMs)
|
|
178
181
|
.map((definition) => definition.type);
|
|
179
|
-
if (
|
|
180
|
-
throw new Error(`An acknowledged worker claims effects without retry.scheduleToStartTimeoutMs: ${
|
|
182
|
+
if (missingStart.length > 0) {
|
|
183
|
+
throw new Error(`An acknowledged worker claims effects without retry.scheduleToStartTimeoutMs: ${missingStart.join(', ')}`);
|
|
184
|
+
}
|
|
185
|
+
const missingClose = claimed
|
|
186
|
+
.filter((definition) => !definition.retry?.startToCloseTimeoutMs && !definition.retry?.heartbeatTimeoutMs)
|
|
187
|
+
.map((definition) => definition.type);
|
|
188
|
+
if (missingClose.length > 0) {
|
|
189
|
+
throw new Error(`An acknowledged worker claims effects without retry.startToCloseTimeoutMs or retry.heartbeatTimeoutMs: ${missingClose.join(', ')}`);
|
|
181
190
|
}
|
|
182
191
|
}
|
|
183
192
|
function claimsType(worker, definition) {
|