@swirls/looms 0.25.0 → 0.26.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 +18 -0
- package/dist/actor/cell.d.ts +3 -1
- package/dist/actor/cell.js +1 -0
- package/dist/actor/host.d.ts +3 -1
- package/dist/actor/host.js +2 -1
- package/dist/actor/routing.d.ts +6 -3
- package/dist/actor/routing.js +6 -4
- package/dist/agent/agent-session.js +2 -2
- package/dist/agent/debugger/agent-chat.js +4 -3
- package/dist/agent/definitions.d.ts +3 -1
- package/dist/agent/effects.js +5 -3
- package/dist/agent/module.d.ts +730 -730
- package/dist/approval/steps.d.ts +3 -1
- package/dist/approval/steps.js +2 -2
- package/dist/client/client.d.ts +7 -1
- package/dist/client/client.js +5 -2
- package/dist/cloudflare/durable-object.d.ts +3 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.d.ts +3 -1
- package/dist/cloudflare/queue-consumer.js +2 -1
- package/dist/cloudflare/router.d.ts +3 -1
- package/dist/cloudflare/router.js +1 -1
- package/dist/core/compose.d.ts +8 -2
- package/dist/core/compose.js +6 -3
- package/dist/core/effects.d.ts +3 -0
- package/dist/core/envelope.d.ts +2 -0
- package/dist/core/envelope.js +2 -2
- package/dist/core/event-codec.d.ts +4 -3
- package/dist/core/event-codec.js +7 -7
- package/dist/core/fold.d.ts +3 -0
- package/dist/core/fold.js +3 -1
- package/dist/core/fork.js +1 -1
- package/dist/core/ids.d.ts +70 -5
- package/dist/core/ids.js +164 -41
- package/dist/core/snapshots.d.ts +2 -0
- package/dist/core/snapshots.js +2 -1
- package/dist/core/thread.d.ts +8 -0
- package/dist/debugger/app/assets/{index-GLtrulmj.js → index-D_FKxPrN.js} +2 -2
- package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/context.d.ts +4 -1
- package/dist/debugger/context.js +4 -4
- package/dist/debugger/shell/debugger-shell.d.ts +4 -1
- package/dist/debugger/shell/debugger-shell.js +2 -2
- package/dist/debugger/shell/use-start-run.d.ts +1 -1
- package/dist/debugger/shell/use-start-run.js +1 -2
- package/dist/evaluator/effects.d.ts +33 -33
- package/dist/evaluator/module.d.ts +66 -66
- package/dist/evaluator/steps.d.ts +2 -2
- package/dist/evaluator/steps.js +3 -3
- package/dist/react/react.d.ts +8 -3
- package/dist/react/react.js +15 -2
- package/dist/react/store.d.ts +5 -1
- package/dist/react/store.js +5 -4
- package/dist/runtime/cancellation.js +5 -5
- package/dist/runtime/deadline-transition-planner.d.ts +1 -0
- package/dist/runtime/deadline-transition-planner.js +8 -7
- package/dist/runtime/effect-admission.js +1 -0
- package/dist/runtime/helpers.d.ts +6 -5
- package/dist/runtime/helpers.js +8 -8
- package/dist/runtime/local-effect-executor.js +15 -10
- package/dist/runtime/looms.d.ts +6 -1
- package/dist/runtime/looms.js +4 -1
- package/dist/runtime/retry-policy.d.ts +2 -1
- package/dist/runtime/retry-policy.js +2 -1
- package/dist/runtime/runtime.js +10 -10
- package/dist/runtime/server/routes/runs.js +5 -5
- package/dist/runtime/types.d.ts +7 -1
- package/dist/runtime/wake-engine.js +10 -9
- package/dist/runtime/worker-callbacks.js +9 -8
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
- package/dist/runtime/worker-effect-dispatch.js +7 -5
- package/dist/schedule/module.d.ts +36 -36
- package/dist/swarm/steps.d.ts +3 -3
- package/dist/swarm/steps.js +5 -5
- package/dist/swarm/tools.d.ts +3 -2
- package/dist/swarm/tools.js +13 -13
- package/dist/workflow/effects.js +6 -5
- package/dist/workflow/graph.d.ts +3 -3
- package/dist/workflow/graph.js +5 -5
- package/dist/workflow/module.d.ts +8 -2
- package/dist/workflow/threads.d.ts +14 -2
- package/dist/workflow/threads.js +54 -44
- package/package.json +1 -1
- package/dist/debugger/app/assets/routes-CECZJJob.js +0 -47
package/dist/workflow/threads.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Predicate, Schema } from 'effect';
|
|
2
|
-
import { cancel, cancelRun, createWaitId, DEFAULT_DEFINITION_VERSION,
|
|
2
|
+
import { cancel, cancelRun, createWaitId, DEFAULT_DEFINITION_VERSION, EventInputSchema, emit, fail, invoke, isJsonObject, isWithdrawnError, launch, RuntimeEffectSchema, spawn, stableEffectId, wait, utf8JsonBytes, waitOutcome, } from '@swirls/looms/core';
|
|
3
3
|
import { NodeStateSchema } from './definitions.js';
|
|
4
4
|
import { runNodeEffect, scheduleEffect } from './effects.js';
|
|
5
5
|
import { iterationChildWorkflowId, nodeRunKey, WorkflowValueTooLargeError } from './graph.js';
|
|
@@ -33,6 +33,11 @@ export const WorkflowPendingMapSchema = Schema.Struct({
|
|
|
33
33
|
execution: Schema.Union([Schema.Literal('thread'), Schema.Literal('run')]).pipe(Schema.withDecodingDefaultKey(Effect.succeed('thread'))),
|
|
34
34
|
close: Schema.Union([Schema.Literal('terminate'), Schema.Literal('wait')]).pipe(Schema.withDecodingDefaultKey(Effect.succeed('terminate'))),
|
|
35
35
|
});
|
|
36
|
+
/** A child to cancel: a thread of this run, or a run of its own. */
|
|
37
|
+
export const WorkflowPendingCancelSchema = Schema.Struct({
|
|
38
|
+
target: Schema.String,
|
|
39
|
+
execution: Schema.Union([Schema.Literal('thread'), Schema.Literal('run')]),
|
|
40
|
+
});
|
|
36
41
|
export const WorkflowPendingSleepSchema = Schema.Struct({
|
|
37
42
|
waitId: Schema.String,
|
|
38
43
|
wakeAt: Schema.Finite,
|
|
@@ -61,7 +66,7 @@ export const WorkflowStateSchema = Schema.Struct({
|
|
|
61
66
|
pendingEffects: Schema.mutable(Schema.Array(WorkflowPendingEffectsSchema)),
|
|
62
67
|
pendingEmits: Schema.mutable(Schema.Array(WorkflowPendingEmitSchema)),
|
|
63
68
|
/** Children of stopped nodes that were still running; each is cancelled once. */
|
|
64
|
-
pendingCancels: Schema.mutable(Schema.Array(
|
|
69
|
+
pendingCancels: Schema.mutable(Schema.Array(WorkflowPendingCancelSchema)).pipe(Schema.withDecodingDefaultKey(Effect.succeed([]))),
|
|
65
70
|
status: Schema.Union([
|
|
66
71
|
Schema.Literal('running'),
|
|
67
72
|
Schema.Literal('completed'),
|
|
@@ -91,9 +96,9 @@ function runNodeInvocation(state, nodeId) {
|
|
|
91
96
|
nodes: state.nodes,
|
|
92
97
|
}, `runNode_${nodeRunKey(nodeId, attempts, node?.iteration?.index)}`);
|
|
93
98
|
}
|
|
94
|
-
function spawnAndWait(
|
|
99
|
+
function spawnAndWait(ctx, child, childThreadId, input, tag) {
|
|
95
100
|
if (child.execution === 'run') {
|
|
96
|
-
const childRunId =
|
|
101
|
+
const childRunId = childTarget(ctx, childThreadId, 'run');
|
|
97
102
|
return [
|
|
98
103
|
launch({
|
|
99
104
|
runId: childRunId,
|
|
@@ -105,7 +110,7 @@ function spawnAndWait(runId, threadId, child, childThreadId, input, tag) {
|
|
|
105
110
|
tag: `launch_${childRunId}`,
|
|
106
111
|
}),
|
|
107
112
|
wait({
|
|
108
|
-
waitId: createWaitId(threadId, `spawn_${childRunId}`),
|
|
113
|
+
waitId: createWaitId(ctx.threadId, `spawn_${childRunId}`),
|
|
109
114
|
on: { type: 'runtime.child.settled', match: { runId: childRunId } },
|
|
110
115
|
tag,
|
|
111
116
|
}),
|
|
@@ -120,7 +125,7 @@ function spawnAndWait(runId, threadId, child, childThreadId, input, tag) {
|
|
|
120
125
|
input,
|
|
121
126
|
}),
|
|
122
127
|
wait({
|
|
123
|
-
waitId: createWaitId(threadId, `spawn_${childThreadId}`),
|
|
128
|
+
waitId: createWaitId(ctx.threadId, `spawn_${childThreadId}`),
|
|
124
129
|
on: {
|
|
125
130
|
type: ['runtime.thread.completed', 'runtime.thread.failed'],
|
|
126
131
|
match: { threadId: childThreadId },
|
|
@@ -135,11 +140,11 @@ function childMode(execution, close) {
|
|
|
135
140
|
close: close === 'wait' ? 'wait' : 'terminate',
|
|
136
141
|
};
|
|
137
142
|
}
|
|
138
|
-
function mapChildThreadId(
|
|
139
|
-
return iterationChildWorkflowId(threadId, map.nodeId, 'map', map.attempt, index);
|
|
143
|
+
function mapChildThreadId(ctx, map, index) {
|
|
144
|
+
return iterationChildWorkflowId(ctx.threadId, map.nodeId, 'map', map.attempt, index, ctx.ids);
|
|
140
145
|
}
|
|
141
146
|
/** Every effect a node currently needs, attributed to that node. */
|
|
142
|
-
function nodeEffects(state,
|
|
147
|
+
function nodeEffects(state, ctx) {
|
|
143
148
|
const effects = [];
|
|
144
149
|
const add = (nodeId, items) => {
|
|
145
150
|
for (const effect of items) {
|
|
@@ -151,7 +156,7 @@ function nodeEffects(state, threadId, runId) {
|
|
|
151
156
|
}
|
|
152
157
|
for (const item of state.pendingSpawns) {
|
|
153
158
|
const tag = { nodeId: item.nodeId, attempt: item.attempt };
|
|
154
|
-
add(item.nodeId, spawnAndWait(
|
|
159
|
+
add(item.nodeId, spawnAndWait(ctx, item, item.childThreadId, item.input, {
|
|
155
160
|
...tag,
|
|
156
161
|
loopIndex: item.loopIndex,
|
|
157
162
|
}));
|
|
@@ -159,7 +164,11 @@ function nodeEffects(state, threadId, runId) {
|
|
|
159
164
|
for (const map of state.pendingMaps) {
|
|
160
165
|
for (let index = 0; index < map.nextIndex; index++) {
|
|
161
166
|
if (!(String(index) in map.outputs)) {
|
|
162
|
-
add(map.nodeId, spawnAndWait(
|
|
167
|
+
add(map.nodeId, spawnAndWait(ctx, map, mapChildThreadId(ctx, map, index), map.items[index] ?? null, {
|
|
168
|
+
nodeId: map.nodeId,
|
|
169
|
+
attempt: map.attempt,
|
|
170
|
+
index,
|
|
171
|
+
}));
|
|
163
172
|
}
|
|
164
173
|
}
|
|
165
174
|
}
|
|
@@ -173,15 +182,15 @@ function nodeEffects(state, threadId, runId) {
|
|
|
173
182
|
}
|
|
174
183
|
return effects;
|
|
175
184
|
}
|
|
176
|
-
function nodeIdForEffect(state,
|
|
177
|
-
const match = nodeEffects(state,
|
|
185
|
+
function nodeIdForEffect(state, ctx, effectId) {
|
|
186
|
+
const match = nodeEffects(state, ctx).find((item) => stableEffectId(ctx.threadId, item.effect) === effectId);
|
|
178
187
|
return match?.nodeId ?? null;
|
|
179
188
|
}
|
|
180
|
-
function childTarget(
|
|
181
|
-
return execution === 'run' ?
|
|
189
|
+
function childTarget(ctx, childThreadId, execution) {
|
|
190
|
+
return execution === 'run' ? ctx.ids.derive('run', [ctx.runId, childThreadId]) : childThreadId;
|
|
182
191
|
}
|
|
183
192
|
/** Children a node started that have not reported back yet. */
|
|
184
|
-
function unsettledChildren(state, nodeId,
|
|
193
|
+
function unsettledChildren(state, nodeId, ctx) {
|
|
185
194
|
const children = [];
|
|
186
195
|
for (const item of state.pendingSpawns) {
|
|
187
196
|
if (item.nodeId !== nodeId) {
|
|
@@ -190,7 +199,7 @@ function unsettledChildren(state, nodeId, threadId, runId) {
|
|
|
190
199
|
const execution = item.execution === 'run' ? 'run' : 'thread';
|
|
191
200
|
children.push({
|
|
192
201
|
key: item.childThreadId,
|
|
193
|
-
target: childTarget(
|
|
202
|
+
target: childTarget(ctx, item.childThreadId, execution),
|
|
194
203
|
execution,
|
|
195
204
|
close: item.close === 'wait' ? 'wait' : 'terminate',
|
|
196
205
|
});
|
|
@@ -205,10 +214,10 @@ function unsettledChildren(state, nodeId, threadId, runId) {
|
|
|
205
214
|
if (String(index) in map.outputs) {
|
|
206
215
|
continue;
|
|
207
216
|
}
|
|
208
|
-
const key = mapChildThreadId(
|
|
217
|
+
const key = mapChildThreadId(ctx, map, index);
|
|
209
218
|
children.push({
|
|
210
219
|
key,
|
|
211
|
-
target: childTarget(
|
|
220
|
+
target: childTarget(ctx, key, execution),
|
|
212
221
|
execution,
|
|
213
222
|
close,
|
|
214
223
|
});
|
|
@@ -222,11 +231,12 @@ function unsettledChildren(state, nodeId, threadId, runId) {
|
|
|
222
231
|
* cancelled here too; run-level `childClose: 'wait'` is enforced by the runtime's launched-run
|
|
223
232
|
* bookkeeping, not by leaving stale entries in workflow pending state.
|
|
224
233
|
*/
|
|
225
|
-
function withoutPendingWork(state, nodeId,
|
|
226
|
-
const children = unsettledChildren(state, nodeId,
|
|
234
|
+
function withoutPendingWork(state, nodeId, ctx, settled = null) {
|
|
235
|
+
const children = unsettledChildren(state, nodeId, ctx);
|
|
227
236
|
const cancels = children
|
|
228
|
-
.
|
|
229
|
-
.
|
|
237
|
+
.filter((child) => child.target !== settled &&
|
|
238
|
+
!state.pendingCancels.some((pending) => pending.target === child.target))
|
|
239
|
+
.map((child) => ({ target: child.target, execution: child.execution }));
|
|
230
240
|
return {
|
|
231
241
|
...state,
|
|
232
242
|
pendingCancels: cancels.length === 0 ? state.pendingCancels : [...state.pendingCancels, ...cancels],
|
|
@@ -236,40 +246,40 @@ function withoutPendingWork(state, nodeId, threadId, runId, settled = null) {
|
|
|
236
246
|
pendingEffects: state.pendingEffects.filter((item) => item.nodeId !== nodeId),
|
|
237
247
|
};
|
|
238
248
|
}
|
|
239
|
-
function stopNode(state, nodeId,
|
|
249
|
+
function stopNode(state, nodeId, ctx) {
|
|
240
250
|
return {
|
|
241
|
-
...withoutPendingWork(state, nodeId,
|
|
251
|
+
...withoutPendingWork(state, nodeId, ctx),
|
|
242
252
|
runningNodes: state.runningNodes.filter((id) => id !== nodeId),
|
|
243
253
|
};
|
|
244
254
|
}
|
|
245
255
|
/** Drops a node's pending work and emits the event that durably finishes it. */
|
|
246
|
-
function finishNode(state, nodeId, result, error,
|
|
256
|
+
function finishNode(state, nodeId, result, error, ctx, settled = null) {
|
|
247
257
|
const id = `finished_${nodeId}`;
|
|
248
258
|
const event = {
|
|
249
259
|
type: 'workflow.node.finished',
|
|
250
260
|
payload: { nodeId, result, error },
|
|
251
|
-
threadId,
|
|
261
|
+
threadId: ctx.threadId,
|
|
252
262
|
};
|
|
253
263
|
return {
|
|
254
|
-
...withoutPendingWork(state, nodeId,
|
|
264
|
+
...withoutPendingWork(state, nodeId, ctx, settled),
|
|
255
265
|
pendingEmits: [...state.pendingEmits.filter((item) => item.id !== id), { id, event }],
|
|
256
266
|
};
|
|
257
267
|
}
|
|
258
|
-
function recordMapOutput(state, map, index, output,
|
|
268
|
+
function recordMapOutput(state, map, index, output, ctx) {
|
|
259
269
|
if (String(index) in map.outputs) {
|
|
260
270
|
return state;
|
|
261
271
|
}
|
|
262
|
-
const childThreadId = mapChildThreadId(
|
|
263
|
-
const settled = childTarget(
|
|
272
|
+
const childThreadId = mapChildThreadId(ctx, map, index);
|
|
273
|
+
const settled = childTarget(ctx, childThreadId, map.execution === 'run' ? 'run' : 'thread');
|
|
264
274
|
const outputs = { ...map.outputs, [String(index)]: output };
|
|
265
275
|
const size = utf8JsonBytes(outputs);
|
|
266
276
|
if (size > map.limitBytes) {
|
|
267
277
|
const error = new WorkflowValueTooLargeError(`Node ${map.nodeId} outputs`, size, map.limitBytes);
|
|
268
|
-
return finishNode(state, map.nodeId, null, error.message,
|
|
278
|
+
return finishNode(state, map.nodeId, null, error.message, ctx, settled);
|
|
269
279
|
}
|
|
270
280
|
if (Object.keys(outputs).length === map.items.length) {
|
|
271
281
|
const ordered = map.items.map((_, i) => outputs[String(i)] ?? null);
|
|
272
|
-
return finishNode(state, map.nodeId, ordered, null,
|
|
282
|
+
return finishNode(state, map.nodeId, ordered, null, ctx, settled);
|
|
273
283
|
}
|
|
274
284
|
const nextIndex = Math.min(map.items.length, map.nextIndex + 1);
|
|
275
285
|
return {
|
|
@@ -355,7 +365,7 @@ export const workflowThread = workflowModule.thread({
|
|
|
355
365
|
}
|
|
356
366
|
const failed = Predicate.isString(error) && error.length > 0;
|
|
357
367
|
return {
|
|
358
|
-
...stopNode(state, nodeId, ctx
|
|
368
|
+
...stopNode(state, nodeId, ctx),
|
|
359
369
|
nodes: {
|
|
360
370
|
...state.nodes,
|
|
361
371
|
[nodeId]: {
|
|
@@ -374,7 +384,7 @@ export const workflowThread = workflowModule.thread({
|
|
|
374
384
|
case 'workflow.node.skipped': {
|
|
375
385
|
const { nodeId, reason, cause } = event.payload;
|
|
376
386
|
return {
|
|
377
|
-
...stopNode(state, nodeId, ctx
|
|
387
|
+
...stopNode(state, nodeId, ctx),
|
|
378
388
|
nodes: {
|
|
379
389
|
...state.nodes,
|
|
380
390
|
[nodeId]: {
|
|
@@ -468,15 +478,13 @@ export const workflowThread = workflowModule.thread({
|
|
|
468
478
|
return state;
|
|
469
479
|
}
|
|
470
480
|
const effectId = event.payload.effectId;
|
|
471
|
-
const nodeId = Predicate.isString(effectId)
|
|
472
|
-
? nodeIdForEffect(state, ctx.threadId, ctx.runId, effectId)
|
|
473
|
-
: null;
|
|
481
|
+
const nodeId = Predicate.isString(effectId) ? nodeIdForEffect(state, ctx, effectId) : null;
|
|
474
482
|
if (!nodeId) {
|
|
475
483
|
return state;
|
|
476
484
|
}
|
|
477
485
|
const message = `Node ${nodeId} failed: ${error}`;
|
|
478
486
|
return {
|
|
479
|
-
...finishNode(stopNode(state, nodeId, ctx
|
|
487
|
+
...finishNode(stopNode(state, nodeId, ctx), nodeId, null, message, ctx),
|
|
480
488
|
nodes: {
|
|
481
489
|
...state.nodes,
|
|
482
490
|
[nodeId]: {
|
|
@@ -500,7 +508,7 @@ export const workflowThread = workflowModule.thread({
|
|
|
500
508
|
const { result, error } = waitOutcome(event.payload.event, tag, { waited: true });
|
|
501
509
|
const map = state.pendingMaps.find((item) => item.nodeId === nodeId);
|
|
502
510
|
if (error === null && map && Predicate.isNumber(tag.index)) {
|
|
503
|
-
return recordMapOutput(state, map, tag.index, result, ctx
|
|
511
|
+
return recordMapOutput(state, map, tag.index, result, ctx);
|
|
504
512
|
}
|
|
505
513
|
const node = state.nodes[nodeId];
|
|
506
514
|
if (error === null && node && Predicate.isNumber(tag.loopIndex)) {
|
|
@@ -522,7 +530,7 @@ export const workflowThread = workflowModule.thread({
|
|
|
522
530
|
? satisfiedPayload.threadId
|
|
523
531
|
: null
|
|
524
532
|
: null;
|
|
525
|
-
return finishNode(state, nodeId, result, error, ctx
|
|
533
|
+
return finishNode(state, nodeId, result, error, ctx, settled);
|
|
526
534
|
}
|
|
527
535
|
default:
|
|
528
536
|
return state;
|
|
@@ -533,14 +541,16 @@ export const workflowThread = workflowModule.thread({
|
|
|
533
541
|
if (state.needsSchedule) {
|
|
534
542
|
effects.push(scheduleInvocation(state));
|
|
535
543
|
}
|
|
536
|
-
for (const item of nodeEffects(state, ctx
|
|
544
|
+
for (const item of nodeEffects(state, ctx)) {
|
|
537
545
|
effects.push(item.effect);
|
|
538
546
|
}
|
|
539
547
|
for (const emitReq of state.pendingEmits) {
|
|
540
548
|
effects.push(emit(emitReq.event));
|
|
541
549
|
}
|
|
542
|
-
for (const
|
|
543
|
-
effects.push(
|
|
550
|
+
for (const child of state.pendingCancels) {
|
|
551
|
+
effects.push(child.execution === 'run'
|
|
552
|
+
? cancelRun(child.target, `cancel_${child.target}`)
|
|
553
|
+
: cancel(child.target));
|
|
544
554
|
}
|
|
545
555
|
if (state.status === 'failed') {
|
|
546
556
|
effects.push(fail(state.error ?? 'Workflow execution failed'));
|