@nathanstephenson/flow 0.1.9 → 0.1.10-beta.10
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/README.md +60 -21
- package/build/backend-worker-chunks/chunk-3KDHJA5P.mjs +162 -0
- package/build/backend-worker-chunks/{chunk-UQDH2COY.mjs → chunk-K774KOGL.mjs} +2 -7
- package/build/backend-worker-chunks/chunk-LAEUXDNH.mjs +130 -0
- package/build/backend-worker-chunks/{chunk-MLOUI3ZM.mjs → chunk-NZZME2I3.mjs} +6 -1
- package/build/backend-worker-chunks/claude-JG2FXYBQ.mjs +35859 -0
- package/build/backend-worker-chunks/{pi-M4OA2FGL.mjs → pi-KJSP23AO.mjs} +2138 -93
- package/build/backend-worker.mjs +77 -123
- package/build/workflow-runtime.cjs +12 -12
- package/dist/backend/claude/background-calls.js +7 -3
- package/dist/backend/claude/import.js +415 -0
- package/dist/backend/claude/index.js +201 -21
- package/dist/backend/claude/session-tools.js +14 -0
- package/dist/backend/claude/subagents.js +41 -9
- package/dist/backend/claude/todo-list.js +114 -0
- package/dist/backend/claude/workflow-parent.js +1 -1
- package/dist/backend/diff-excerpt-instructions.js +8 -0
- package/dist/backend/fake/index.js +54 -6
- package/dist/backend/mcp.js +4 -1
- package/dist/backend/pi/agent-permissions.js +1 -1
- package/dist/backend/pi/cache-warming.js +8 -0
- package/dist/backend/pi/canonical-json.js +120 -0
- package/dist/backend/pi/compaction-model.js +3 -2
- package/dist/backend/pi/host-auth.js +258 -0
- package/dist/backend/pi/import.js +269 -0
- package/dist/backend/pi/index.js +125 -21
- package/dist/backend/pi/model-auth.js +159 -0
- package/dist/backend/pi/model-catalog.js +120 -0
- package/dist/backend/pi/model-config.js +5 -0
- package/dist/backend/pi/model-selection.js +32 -0
- package/dist/backend/pi/plan-limits.js +41 -0
- package/dist/backend/pi/session-tools.js +21 -0
- package/dist/backend/pi/state.js +19 -1
- package/dist/backend/pi/subagents.js +7 -3
- package/dist/backend/pi/todo-list.js +23 -0
- package/dist/backend/pi/work.js +9 -1
- package/dist/backend/pi/workflow-parent.js +1 -1
- package/dist/backend/pi/workflow-subagent.js +4 -3
- package/dist/backend/plan-limits.js +17 -0
- package/dist/backend/session-tools.js +27 -0
- package/dist/backend/worker/claude-state.js +48 -0
- package/dist/backend/worker/entry.js +49 -8
- package/dist/backend/worker/index.js +155 -17
- package/dist/backend/worker/launcher.js +11 -4
- package/dist/backend/worker/protocol.js +2 -1
- package/dist/backend/worker/rpc.js +13 -0
- package/dist/backend/worker/sandbox-mcp.js +18 -7
- package/dist/backend/workflow-tools.js +9 -6
- package/dist/build-id +1 -1
- package/dist/cli/application.js +48 -42
- package/dist/cli/bootstrap.js +2 -2
- package/dist/cli/host-control.js +1 -1
- package/dist/cli/install-guard.js +2 -2
- package/dist/cli/main.js +4 -4
- package/dist/cli/systemd-update.js +3 -3
- package/dist/cli/update.js +6 -6
- package/dist/cli/web-update.js +6 -6
- package/dist/client/connection.js +46 -0
- package/dist/client/context-usage.js +3 -4
- package/dist/client/diff.js +40 -0
- package/dist/client/enquiry.js +0 -6
- package/dist/client/markdown.js +3 -2
- package/dist/client/model-choices.js +1 -1
- package/dist/client/permission.js +1 -2
- package/dist/client/project-name.js +3 -1
- package/dist/client/reduce.js +77 -3
- package/dist/client/relative-time.js +1 -1
- package/dist/client/scope-kind.js +6 -3
- package/dist/client/search.js +1 -1
- package/dist/client/status.js +1 -1
- package/dist/client/subagent-tree.js +49 -0
- package/dist/client/tool-chains.js +1 -1
- package/dist/client/tool-summary.js +28 -18
- package/dist/daemon/changes.js +128 -0
- package/dist/daemon/commit.js +84 -0
- package/dist/daemon/config-store.js +8 -3
- package/dist/daemon/config.js +105 -4
- package/dist/daemon/diff-excerpts.js +52 -0
- package/dist/daemon/git-state.js +197 -0
- package/dist/daemon/git.js +53 -10
- package/dist/daemon/github-image.js +18 -0
- package/dist/daemon/github.js +54 -0
- package/dist/daemon/host-control-routes.js +1 -1
- package/dist/daemon/host.js +1010 -116
- package/dist/daemon/http.js +4 -1
- package/dist/daemon/log.js +3 -0
- package/dist/daemon/mcp-auth.js +1 -1
- package/dist/daemon/models.js +2 -2
- package/dist/daemon/pi-model-cache.js +44 -0
- package/dist/daemon/pull-request.js +223 -138
- package/dist/daemon/push.js +43 -0
- package/dist/daemon/repository.js +123 -0
- package/dist/daemon/runtime.js +103 -22
- package/dist/daemon/secret-store.js +1 -1
- package/dist/daemon/server.js +111 -14
- package/dist/daemon/session-tool-views.js +22 -0
- package/dist/daemon/skills.js +24 -0
- package/dist/daemon/spend-ledger.js +118 -0
- package/dist/daemon/stack-metadata.js +12 -5
- package/dist/daemon/stack.js +162 -657
- package/dist/daemon/store.js +2 -0
- package/dist/daemon/summariser.js +30 -12
- package/dist/daemon/tour.js +56 -0
- package/dist/daemon/whisper.js +144 -0
- package/dist/daemon/workflow-authoring-scope-checks.js +1 -1
- package/dist/daemon/workflow-authoring-scope.js +1 -1
- package/dist/daemon/workflow-builder-files.js +2 -2
- package/dist/daemon/workflow-builder-guidance.js +1 -1
- package/dist/daemon/workflow-builder-routes.js +2 -2
- package/dist/daemon/workflow-builder.js +44 -17
- package/dist/daemon/workflow-environment.js +193 -0
- package/dist/daemon/workflow-execution-launch.js +77 -0
- package/dist/daemon/workflow-execution-resources.js +76 -0
- package/dist/daemon/workflow-execution-routes.js +17 -8
- package/dist/daemon/workflow-execution-values.js +128 -0
- package/dist/daemon/workflow-executions.js +434 -305
- package/dist/daemon/workflow-mcp-authoring-routes.js +2 -2
- package/dist/daemon/workflow-mcp-authoring.js +4 -4
- package/dist/daemon/workflow-mcp.js +1 -1
- package/dist/daemon/workflow-routes.js +60 -2
- package/dist/daemon/workflow-schedule-driver.js +251 -0
- package/dist/daemon/workflow-schedule-routes.js +137 -0
- package/dist/daemon/workflow-schedules.js +342 -0
- package/dist/isolation/filesystem.js +6 -2
- package/dist/isolation/sandbox.js +61 -18
- package/dist/isolation/shared-sandbox.js +3 -3
- package/dist/isolation/toolchain-assets.js +1 -1
- package/dist/protocol/events.js +3 -0
- package/dist/protocol/plan-limits.js +1 -0
- package/dist/protocol/settings.js +1 -0
- package/dist/protocol/spend.js +2 -0
- package/dist/protocol/stack.js +12 -39
- package/dist/protocol/workflow-schedules.js +1 -0
- package/dist/workflows/calls.js +53 -0
- package/dist/workflows/errors.js +2 -0
- package/dist/workflows/executors.js +6 -5
- package/dist/workflows/graph.js +83 -9
- package/dist/workflows/invocations.js +23 -0
- package/dist/workflows/mcp.js +1 -1
- package/dist/workflows/owner.js +45 -0
- package/dist/workflows/records.js +150 -10
- package/dist/workflows/recovery.js +87 -2
- package/dist/workflows/runtime-settings.js +16 -16
- package/dist/workflows/runtime.js +2 -2
- package/dist/workflows/schedule-errors.js +2 -0
- package/dist/workflows/schedule-store.js +385 -0
- package/dist/workflows/scheduler.js +389 -98
- package/dist/workflows/schedules.js +130 -0
- package/dist/workflows/schema.js +3 -1
- package/dist/workflows/store.js +126 -13
- package/package.json +10 -6
- package/web/dist/assets/index-C76t-z4w.css +2 -0
- package/web/dist/assets/{index-DRfeO6GJ.js → index-WPfbYVQ4.js} +39 -23
- package/web/dist/assets/{switch-DIogWclb.js → select-C5Ly2OpB.js} +1 -1
- package/web/dist/assets/{settings-mcp-Clp_jMWW.js → settings-mcp-BF-yqpeQ.js} +1 -1
- package/web/dist/assets/{settings-secrets-BIO_hXZ1.js → settings-secrets-WLarr2QP.js} +1 -1
- package/web/dist/assets/settings-workflows-DdcdRWFD.js +23 -0
- package/web/dist/assets/workflow-api-GjAmHc22.js +6 -0
- package/web/dist/assets/workflows-BhY3Uibo.js +149 -0
- package/web/dist/assets/workflows-pane-DElCcEpN.js +1 -0
- package/web/dist/index.html +6 -5
- package/build/backend-worker-chunks/chunk-J2DBNTL7.mjs +0 -94
- package/build/backend-worker-chunks/claude-GTW5KHY5.mjs +0 -30778
- package/dist/daemon/publish.js +0 -289
- package/dist/tui/app.js +0 -612
- package/dist/tui/keys.js +0 -90
- package/dist/tui/render.js +0 -411
- package/web/dist/assets/index-DgQFpKbn.css +0 -2
- package/web/dist/assets/settings-workflows-H-6eCff3.js +0 -23
- package/web/dist/assets/workflow-api-BfExddae.js +0 -6
- package/web/dist/assets/workflow-editors-CsbVx4WJ.js +0 -139
- package/web/dist/assets/workflows-Bv0sj7d0.js +0 -7
- package/web/dist/assets/workflows-pane-DHm44w7P.js +0 -1
- /package/dist/protocol/{publish.js → imports.js} +0 -0
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import { executionOwner } from './records.js';
|
|
2
|
+
import { executionAccessibleBy, executionAssociation, executionIdentity, executionOwnerKey, executionOwnerOf } from './owner.js';
|
|
1
3
|
import { randomUUID } from 'node:crypto';
|
|
2
4
|
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
-
import { resolveMapping, validateDefinition } from
|
|
4
|
-
import { declaredOutputSchema, parseValue, valueAt } from
|
|
5
|
-
import { validateMcpOutput } from
|
|
6
|
-
import { WorkflowStore } from
|
|
5
|
+
import { resolveMapping, validateDefinition } from './graph.js';
|
|
6
|
+
import { declaredOutputSchema, parseValue, valueAt } from './schema.js';
|
|
7
|
+
import { validateMcpOutput } from './mcp.js';
|
|
8
|
+
import { WorkflowStore } from './store.js';
|
|
9
|
+
import { recoverySteps, savedDefinitionRevision, validateCompatibleDefinition } from './recovery.js';
|
|
10
|
+
import { resolveWorkflowCalls } from './calls.js';
|
|
11
|
+
import { invocationCheckpoint, invocationExecution } from './invocations.js';
|
|
7
12
|
export class WorkflowStepError extends Error {
|
|
8
13
|
partialOutput;
|
|
9
14
|
constructor(message, partialOutput) { super(message); this.partialOutput = partialOutput; }
|
|
@@ -16,62 +21,71 @@ export class WorkflowScheduler {
|
|
|
16
21
|
slots = new Map();
|
|
17
22
|
store;
|
|
18
23
|
executors;
|
|
19
|
-
|
|
24
|
+
onResume;
|
|
25
|
+
constructor(store, executors, onResume) {
|
|
20
26
|
this.store = store;
|
|
21
27
|
this.executors = executors;
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
this.onResume = onResume;
|
|
29
|
+
for (const owner of store.listOwners()) {
|
|
30
|
+
let records;
|
|
31
|
+
try {
|
|
32
|
+
records = store.listOwnedExecutions(owner);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
console.error('Workflow Execution owner records are invalid. Files retained for manual review.');
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
for (const record of records) {
|
|
24
39
|
if (!occupied(record))
|
|
25
40
|
continue;
|
|
26
|
-
if (this.slots.has(
|
|
27
|
-
throw new Error('Multiple workflow executions occupy one
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
continue;
|
|
32
|
-
step.status = 'interrupted';
|
|
33
|
-
const attempt = step.attempts.at(-1);
|
|
34
|
-
attempt.error = { kind: 'interrupted', message: 'Execution was interrupted; effects may already have occurred' };
|
|
35
|
-
attempt.finishedAt = Date.now();
|
|
36
|
-
}
|
|
37
|
-
record.status = 'recovery-required';
|
|
38
|
-
store.saveExecution(record);
|
|
39
|
-
}
|
|
40
|
-
this.install(record);
|
|
41
|
+
if (this.slots.has(executionOwnerKey(owner)))
|
|
42
|
+
throw new Error('Multiple workflow executions occupy one execution owner');
|
|
43
|
+
const active = this.install(record);
|
|
44
|
+
this.reconcile(active);
|
|
45
|
+
this.persist(active);
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
|
-
occupied(sessionId) { return this.slots.has(sessionId); }
|
|
49
|
+
occupied(sessionId) { return this.slots.has(sessionId) || [...this.active.values()].some(active => executionAssociation(active.record) === sessionId && occupied(active.record)); }
|
|
45
50
|
/** Whether the slot contains a full execution doing work, excluding tests and recovery waits. */
|
|
46
51
|
activeFull(sessionId) {
|
|
47
|
-
const executionId = this.slots.get(sessionId);
|
|
52
|
+
const executionId = this.slots.get(sessionId) ?? [...this.active.values()].find(active => executionAssociation(active.record) === sessionId && occupied(active.record))?.record.id;
|
|
48
53
|
const record = executionId ? this.active.get(executionId)?.record : undefined;
|
|
49
54
|
return record?.status === 'running' && record.testStepId === undefined;
|
|
50
55
|
}
|
|
51
56
|
forgetSession(sessionId) {
|
|
52
|
-
const executions = [...this.active.values()].filter(active => active.record
|
|
53
|
-
if (executions.some(active => active
|
|
57
|
+
const executions = [...this.active.values()].filter(active => executionOwner(active.record) === sessionId);
|
|
58
|
+
if (executions.some(active => this.hasWork(active) || active.record.status === 'running'))
|
|
54
59
|
throw new Error('Workflow work must stop before forgetting an Agent Session');
|
|
55
60
|
for (const active of executions)
|
|
56
61
|
this.active.delete(active.record.id);
|
|
57
62
|
this.slots.delete(sessionId);
|
|
58
63
|
}
|
|
59
|
-
start(definition, session, input, testStepId, launchId, namingEligible = false) {
|
|
60
|
-
if (this.occupied(session
|
|
64
|
+
start(definition, session, input, testStepId, launchId, namingEligible = false, capturedDefinitions) {
|
|
65
|
+
if (this.occupied(executionOwner(session)))
|
|
61
66
|
throw new Error('Agent Session workflow slot is occupied');
|
|
62
67
|
const graph = validateDefinition(definition);
|
|
63
68
|
if (graph.definition.backend !== session.backend)
|
|
64
69
|
throw new Error('Backend Adapter mismatch');
|
|
65
|
-
if (graph.definition.projectId && graph.definition.projectId
|
|
70
|
+
if (graph.definition.projectId && !(session.matchesProject?.(graph.definition.projectId) ?? graph.definition.projectId === session.projectId))
|
|
66
71
|
throw new Error('Workflow is restricted to another Project');
|
|
67
72
|
const testStep = testStepId === undefined ? undefined : graph.order.find(step => step.id === testStepId);
|
|
68
73
|
if (testStepId !== undefined && !testStep)
|
|
69
74
|
throw new Error('Unknown test step');
|
|
70
|
-
|
|
75
|
+
const retainedDefinitions = resolveWorkflowCalls(graph.definition, id => {
|
|
76
|
+
if (!capturedDefinitions)
|
|
77
|
+
return this.store.getDefinition(id);
|
|
78
|
+
const retained = capturedDefinitions[id];
|
|
79
|
+
if (!retained)
|
|
80
|
+
throw new Error('Missing captured Workflow Definition');
|
|
81
|
+
return retained;
|
|
82
|
+
}, testStepId);
|
|
83
|
+
this.check(testStep ? [testStep] : graph.order, session, retainedDefinitions);
|
|
71
84
|
const parsed = parseValue(testStep ? graph.inputSchemas.get(testStep.id) : graph.definition.inputSchema, input);
|
|
72
85
|
const record = {
|
|
73
|
-
|
|
86
|
+
...executionIdentity(session), ...(session.runtime ? { runtime: session.runtime } : {}), id: randomUUID(), scope: session.scope,
|
|
74
87
|
definition: graph.definition, input: parsed, status: 'running', startedAt: Date.now(),
|
|
88
|
+
...(graph.order.some(step => step.kind === 'workflow') ? { retainedDefinitions, invocations: {} } : {}),
|
|
75
89
|
...(launchId ? { launchId } : {}),
|
|
76
90
|
...(namingEligible && testStepId === undefined ? { naming: 'pending' } : {}),
|
|
77
91
|
steps: Object.fromEntries(graph.order.map(step => [step.id, { status: testStep && step.id !== testStep.id ? 'skipped' : 'pending', attempts: [] }])),
|
|
@@ -82,16 +96,25 @@ export class WorkflowScheduler {
|
|
|
82
96
|
this.pump(active);
|
|
83
97
|
return structuredClone(record);
|
|
84
98
|
}
|
|
99
|
+
associate(occurrenceId, executionId, sessionId) {
|
|
100
|
+
const active = this.require(executionOwnerKey({ kind: 'occurrence', occurrenceId }), executionId);
|
|
101
|
+
if (executionOwnerOf(active.record).kind !== 'occurrence' || (executionAssociation(active.record) && executionAssociation(active.record) !== sessionId))
|
|
102
|
+
throw new Error('Invalid execution association');
|
|
103
|
+
const visit = (node) => { node.record.sessionId = sessionId; for (const call of node.calls.values())
|
|
104
|
+
visit(call.child); };
|
|
105
|
+
visit(active);
|
|
106
|
+
this.persist(active);
|
|
107
|
+
}
|
|
85
108
|
get(sessionId, executionId) {
|
|
86
109
|
const active = this.active.get(executionId);
|
|
87
|
-
if (active
|
|
110
|
+
if (active !== undefined && executionAccessibleBy(active.record, sessionId))
|
|
88
111
|
return structuredClone(active.record);
|
|
89
112
|
return this.store.getExecution(sessionId, executionId);
|
|
90
113
|
}
|
|
91
114
|
/** Atomically consume an automatic naming request before model work starts. */
|
|
92
115
|
claimNaming(sessionId, executionId) {
|
|
93
116
|
const active = this.active.get(executionId);
|
|
94
|
-
const record = active
|
|
117
|
+
const record = active !== undefined && executionAccessibleBy(active.record, sessionId) ? active.record : this.store.getExecution(sessionId, executionId);
|
|
95
118
|
if (record.naming !== 'pending' || record.testStepId)
|
|
96
119
|
return false;
|
|
97
120
|
record.naming = 'requested';
|
|
@@ -105,25 +128,20 @@ export class WorkflowScheduler {
|
|
|
105
128
|
const active = this.require(sessionId, executionId);
|
|
106
129
|
if (active.persistenceError)
|
|
107
130
|
return Promise.reject(active.persistenceError);
|
|
108
|
-
if (active.record.status !== 'running' && !active
|
|
131
|
+
if (active.record.status !== 'running' && !this.hasWork(active))
|
|
109
132
|
return Promise.resolve(structuredClone(active.record));
|
|
110
133
|
return new Promise((resolve, reject) => active.waiters.push({ resolve, reject }));
|
|
111
134
|
}
|
|
135
|
+
hasPendingDeadline(sessionId, executionId) {
|
|
136
|
+
const pending = (active) => !active.stopped && [...active.calls.values()].some(call => call.timer !== undefined || pending(call.child));
|
|
137
|
+
const active = this.active.get(executionId);
|
|
138
|
+
return active !== undefined && executionAccessibleBy(active.record, sessionId) && pending(active);
|
|
139
|
+
}
|
|
112
140
|
async cancel(sessionId, executionId) {
|
|
113
141
|
const active = this.require(sessionId, executionId);
|
|
114
142
|
if (!occupied(active.record))
|
|
115
143
|
return structuredClone(active.record);
|
|
116
|
-
active
|
|
117
|
-
active.record.status = 'cancelled';
|
|
118
|
-
for (const step of Object.values(active.record.steps))
|
|
119
|
-
if (['pending', 'blocked', 'running', 'interrupted'].includes(step.status))
|
|
120
|
-
step.status = 'cancelled';
|
|
121
|
-
this.persist(active);
|
|
122
|
-
for (const running of active.running.values())
|
|
123
|
-
running.controller.abort();
|
|
124
|
-
await Promise.all([...active.running.values()].map(running => running.done));
|
|
125
|
-
active.record.finishedAt = Date.now();
|
|
126
|
-
this.persist(active);
|
|
144
|
+
await this.stopTree(active, 'cancelled');
|
|
127
145
|
this.settle(active);
|
|
128
146
|
return structuredClone(active.record);
|
|
129
147
|
}
|
|
@@ -131,31 +149,32 @@ export class WorkflowScheduler {
|
|
|
131
149
|
const active = this.require(sessionId, executionId);
|
|
132
150
|
if (!occupied(active.record))
|
|
133
151
|
return structuredClone(active.record);
|
|
134
|
-
active
|
|
135
|
-
active.record.status = 'recovery-required';
|
|
136
|
-
for (const step of Object.values(active.record.steps)) {
|
|
137
|
-
if (step.status !== 'running')
|
|
138
|
-
continue;
|
|
139
|
-
step.status = 'interrupted';
|
|
140
|
-
const attempt = step.attempts.at(-1);
|
|
141
|
-
attempt.error = { kind: 'interrupted', message: 'Execution was interrupted; effects may already have occurred' };
|
|
142
|
-
attempt.finishedAt = Date.now();
|
|
143
|
-
}
|
|
144
|
-
this.persist(active);
|
|
145
|
-
for (const running of active.running.values())
|
|
146
|
-
running.controller.abort();
|
|
147
|
-
await Promise.all([...active.running.values()].map(running => running.done));
|
|
152
|
+
await this.stopTree(active, 'interrupted');
|
|
148
153
|
this.settle(active);
|
|
149
154
|
return structuredClone(active.record);
|
|
150
155
|
}
|
|
151
|
-
|
|
152
|
-
|
|
156
|
+
/** replacementDefinition is resolved from saved storage by the host, never from request JSON. */
|
|
157
|
+
recover(session, executionId, action, replacementDefinition) {
|
|
158
|
+
const root = this.require(executionOwner(session), executionId);
|
|
159
|
+
const active = action.invocationId ? this.findInvocation(root, action.invocationId) : root;
|
|
153
160
|
const record = active.record;
|
|
154
|
-
if (record.status !== 'recovery-required' || active
|
|
161
|
+
if (record.status !== 'recovery-required' || this.hasWork(active))
|
|
155
162
|
throw new Error('Execution is not ready for recovery');
|
|
156
163
|
if (record.definition.backend !== session.backend || record.scope !== session.scope)
|
|
157
164
|
throw new Error('Recovery requires the original Scope and Backend Adapter');
|
|
158
|
-
|
|
165
|
+
let graph = active.graph;
|
|
166
|
+
if (action.kind === 'retry-updated') {
|
|
167
|
+
if (!replacementDefinition)
|
|
168
|
+
throw new Error('Updated recovery requires a saved Workflow Definition');
|
|
169
|
+
if (savedDefinitionRevision(replacementDefinition) !== action.definitionRevision)
|
|
170
|
+
throw new Error('Saved Workflow Definition revision changed');
|
|
171
|
+
graph = validateCompatibleDefinition(record.definition, replacementDefinition, action.stepId);
|
|
172
|
+
}
|
|
173
|
+
else if (replacementDefinition !== undefined)
|
|
174
|
+
throw new Error('Replacement definition requires retry-updated');
|
|
175
|
+
// Check the corrected configuration, not the obsolete one. No checkpoint/graph mutation
|
|
176
|
+
// happens until compatibility, authority, eligibility and all runtime checks pass.
|
|
177
|
+
this.check(recoverySteps(root.record, action, replacementDefinition), session, root.record.retainedDefinitions);
|
|
159
178
|
const limits = Object.entries(record.loops ?? {}).filter(([, loop]) => loop.phase === 'limit');
|
|
160
179
|
if (action.kind === 'continue' && limits.length)
|
|
161
180
|
throw new WorkflowLoopConflict('Loop limit requires an extra try or cancellation');
|
|
@@ -169,14 +188,17 @@ export class WorkflowScheduler {
|
|
|
169
188
|
loop.phase = 'active';
|
|
170
189
|
}
|
|
171
190
|
else if (action.kind !== 'continue') {
|
|
172
|
-
const step =
|
|
191
|
+
const step = graph.order.find(step => step.id === action.stepId);
|
|
173
192
|
const state = record.steps[action.stepId];
|
|
174
193
|
if (!step || !state || !['interrupted', 'failed', 'timed-out'].includes(state.status))
|
|
175
194
|
throw new Error('Step does not require recovery');
|
|
176
|
-
if (state.status !== 'interrupted' && !record.testStepId && !state.recovery &&
|
|
195
|
+
if (state.status !== 'interrupted' && !record.testStepId && !state.recovery && graph.outgoing.get(step.id).some(edge => edge.outcome === state.outcome))
|
|
177
196
|
throw new Error('Recover the failed handling step, not the handled failure');
|
|
178
197
|
if (action.kind === 'supply') {
|
|
179
198
|
const output = this.validateOutput(active, step, action.output);
|
|
199
|
+
this.releaseCancelledCall(active, step.id);
|
|
200
|
+
if (active.calls.has(step.id))
|
|
201
|
+
throw new Error('Cancel the owned call subtree before replacing its output');
|
|
180
202
|
const attempt = { number: state.attempts.length + 1, action: 'supply', startedAt: Date.now(), finishedAt: Date.now(), input: state.attempts.at(-1).input, output, ...(state.attempts.at(-1).loops ? { loops: structuredClone(state.attempts.at(-1).loops) } : {}) };
|
|
181
203
|
state.attempts.push(attempt);
|
|
182
204
|
state.output = output;
|
|
@@ -184,7 +206,14 @@ export class WorkflowScheduler {
|
|
|
184
206
|
state.outcome = step.kind === 'branch' ? output ? 'true' : 'false' : 'success';
|
|
185
207
|
}
|
|
186
208
|
else {
|
|
187
|
-
this.check([step], session);
|
|
209
|
+
this.check([step], session, root.record.retainedDefinitions);
|
|
210
|
+
this.releaseCancelledCall(active, step.id);
|
|
211
|
+
if (action.kind === 'retry-updated') {
|
|
212
|
+
const previousStep = record.definition.steps.find(previous => previous.id === step.id);
|
|
213
|
+
(record.definitionUpdates ??= []).push({ stepId: step.id, previousStep: structuredClone(previousStep), nextStep: structuredClone(step), appliedAt: Date.now(), attempt: state.attempts.length + 1 });
|
|
214
|
+
record.definition = graph.definition;
|
|
215
|
+
active.graph = graph;
|
|
216
|
+
}
|
|
188
217
|
state.status = 'pending';
|
|
189
218
|
delete state.outcome;
|
|
190
219
|
}
|
|
@@ -213,14 +242,37 @@ export class WorkflowScheduler {
|
|
|
213
242
|
delete record.finishedAt;
|
|
214
243
|
active.stopped = false;
|
|
215
244
|
delete active.persistenceError;
|
|
245
|
+
for (let parent = active.parent; parent; parent = parent.parent) {
|
|
246
|
+
parent.stopped = false;
|
|
247
|
+
parent.record.status = 'running';
|
|
248
|
+
delete parent.record.finishedAt;
|
|
249
|
+
}
|
|
216
250
|
this.persist(active);
|
|
251
|
+
delete root.persistenceError;
|
|
252
|
+
this.resumeCalls(active);
|
|
253
|
+
for (let parent = active.parent; parent; parent = parent.parent)
|
|
254
|
+
this.resumeCalls(parent);
|
|
217
255
|
this.pump(active);
|
|
218
|
-
return structuredClone(record);
|
|
256
|
+
return structuredClone(root.record);
|
|
219
257
|
}
|
|
220
|
-
check(steps, session) {
|
|
258
|
+
check(steps, session, manifest, checked = new Set()) {
|
|
221
259
|
for (const step of steps) {
|
|
222
260
|
if (step.kind === 'branch' || step.kind === 'join')
|
|
223
261
|
continue;
|
|
262
|
+
if (step.kind === 'workflow') {
|
|
263
|
+
const target = manifest?.[step.workflowId];
|
|
264
|
+
if (!target)
|
|
265
|
+
throw new Error('Missing retained called Workflow Definition');
|
|
266
|
+
if (target.backend !== session.backend)
|
|
267
|
+
throw new Error('Backend Adapter mismatch');
|
|
268
|
+
if (target.projectId && !(session.matchesProject?.(target.projectId) ?? target.projectId === session.projectId))
|
|
269
|
+
throw new Error('Workflow is restricted to another Project');
|
|
270
|
+
if (!checked.has(target.id)) {
|
|
271
|
+
checked.add(target.id);
|
|
272
|
+
this.check(target.steps, session, manifest, checked);
|
|
273
|
+
}
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
224
276
|
const executor = this.executors[step.kind];
|
|
225
277
|
if (!executor)
|
|
226
278
|
throw new Error(`Executor is unavailable: ${step.kind}`);
|
|
@@ -228,14 +280,234 @@ export class WorkflowScheduler {
|
|
|
228
280
|
}
|
|
229
281
|
}
|
|
230
282
|
install(record) {
|
|
231
|
-
const active = { record, graph: validateDefinition(record.definition), running: new Map(), waiters: [], stopped: record.status !== 'running' };
|
|
283
|
+
const active = { record, graph: validateDefinition(record.definition), running: new Map(), calls: new Map(), waiters: [], stopped: record.status !== 'running' };
|
|
232
284
|
this.active.set(record.id, active);
|
|
233
|
-
this.slots.set(record
|
|
285
|
+
this.slots.set(executionOwner(record), record.id);
|
|
286
|
+
const installed = new Map();
|
|
287
|
+
const restore = (id) => {
|
|
288
|
+
const cached = installed.get(id);
|
|
289
|
+
if (cached)
|
|
290
|
+
return cached;
|
|
291
|
+
const invocation = record.invocations[id];
|
|
292
|
+
const parent = invocation.parentInvocationId ? restore(invocation.parentInvocationId) : active;
|
|
293
|
+
const local = invocationExecution(record, id);
|
|
294
|
+
const step = parent.graph.order.find(step => step.id === invocation.callStepId);
|
|
295
|
+
const child = { record: local, graph: validateDefinition(local.definition), parent, root: active, callStepId: invocation.callStepId, callAttempt: invocation.callAttempt,
|
|
296
|
+
inheritedAsk: parent.inheritedAsk || parent.record.definition.permission === 'ask' || step.permission === 'ask', running: new Map(), calls: new Map(), waiters: [], stopped: local.status !== 'running' };
|
|
297
|
+
installed.set(id, child);
|
|
298
|
+
const state = parent.record.steps[invocation.callStepId];
|
|
299
|
+
if (state.attempts.length === invocation.callAttempt && state.status === 'running')
|
|
300
|
+
parent.calls.set(invocation.callStepId, { child });
|
|
301
|
+
return child;
|
|
302
|
+
};
|
|
303
|
+
for (const id of Object.keys(record.invocations ?? {}))
|
|
304
|
+
restore(id);
|
|
234
305
|
return active;
|
|
235
306
|
}
|
|
307
|
+
hasWork(active) {
|
|
308
|
+
return active.running.size > 0 || [...active.calls.values()].some(call => !!call.stopping || this.hasWork(call.child));
|
|
309
|
+
}
|
|
310
|
+
findInvocation(active, id) {
|
|
311
|
+
const pending = [active];
|
|
312
|
+
while (pending.length) {
|
|
313
|
+
const current = pending.pop();
|
|
314
|
+
if (current !== active && current.record.id === id)
|
|
315
|
+
return current;
|
|
316
|
+
pending.push(...[...current.calls.values()].map(call => call.child));
|
|
317
|
+
}
|
|
318
|
+
throw new Error('Invocation does not require recovery');
|
|
319
|
+
}
|
|
320
|
+
syncInvocations(active) {
|
|
321
|
+
const root = active.root ?? active;
|
|
322
|
+
for (const call of active.calls.values()) {
|
|
323
|
+
const child = call.child;
|
|
324
|
+
this.syncInvocations(child);
|
|
325
|
+
(root.record.invocations ??= {})[child.record.id] = invocationCheckpoint(child.record, {
|
|
326
|
+
...(active.parent ? { parentInvocationId: active.record.id } : {}), callStepId: child.callStepId, callAttempt: child.callAttempt, definitionId: child.record.definition.id,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
reconcile(active) {
|
|
331
|
+
if (occupied(active.record)) {
|
|
332
|
+
for (const [id, state] of Object.entries(active.record.steps)) {
|
|
333
|
+
if (state.status !== 'running' || active.calls.has(id))
|
|
334
|
+
continue;
|
|
335
|
+
state.status = 'interrupted';
|
|
336
|
+
const attempt = state.attempts.at(-1);
|
|
337
|
+
attempt.error = { kind: 'interrupted', message: 'Execution was interrupted; effects may already have occurred' };
|
|
338
|
+
attempt.finishedAt ??= Date.now();
|
|
339
|
+
}
|
|
340
|
+
active.record.status = 'recovery-required';
|
|
341
|
+
active.stopped = !!active.parent?.stopped || Object.values(active.record.steps).some(state => state.status === 'interrupted');
|
|
342
|
+
for (const id of active.calls.keys())
|
|
343
|
+
this.armCall(active, id);
|
|
344
|
+
}
|
|
345
|
+
for (const call of active.calls.values())
|
|
346
|
+
this.reconcile(call.child);
|
|
347
|
+
}
|
|
348
|
+
releaseCancelledCall(active, stepId) {
|
|
349
|
+
const call = active.calls.get(stepId);
|
|
350
|
+
if (call?.child.record.status !== 'cancelled')
|
|
351
|
+
return;
|
|
352
|
+
clearTimeout(call.timer);
|
|
353
|
+
this.syncInvocations(active);
|
|
354
|
+
active.calls.delete(stepId);
|
|
355
|
+
}
|
|
356
|
+
failStorage(active) {
|
|
357
|
+
active.stopped = true;
|
|
358
|
+
if (!active.parent || occupied(active.record))
|
|
359
|
+
active.record.status = 'recovery-required';
|
|
360
|
+
for (const call of active.calls.values()) {
|
|
361
|
+
clearTimeout(call.timer);
|
|
362
|
+
this.failStorage(call.child);
|
|
363
|
+
}
|
|
364
|
+
for (const [id, state] of Object.entries(active.record.steps)) {
|
|
365
|
+
if (state.status !== 'running' || active.calls.has(id))
|
|
366
|
+
continue;
|
|
367
|
+
state.status = 'interrupted';
|
|
368
|
+
const attempt = state.attempts.at(-1);
|
|
369
|
+
attempt.error = { kind: 'interrupted', message: 'Workflow storage failed; effects may already have occurred' };
|
|
370
|
+
attempt.finishedAt ??= Date.now();
|
|
371
|
+
}
|
|
372
|
+
for (const running of active.running.values())
|
|
373
|
+
running.controller.abort();
|
|
374
|
+
}
|
|
375
|
+
async stopTree(active, reason) {
|
|
376
|
+
const tree = [active];
|
|
377
|
+
for (const current of tree)
|
|
378
|
+
tree.push(...[...current.calls.values()].map(call => call.child));
|
|
379
|
+
for (const current of tree) {
|
|
380
|
+
current.stopped = true;
|
|
381
|
+
if (!occupied(current.record))
|
|
382
|
+
continue;
|
|
383
|
+
current.record.status = reason === 'cancelled' ? 'cancelled' : 'recovery-required';
|
|
384
|
+
for (const [id, state] of Object.entries(current.record.steps)) {
|
|
385
|
+
if (reason === 'cancelled' && ['pending', 'blocked', 'running', 'interrupted'].includes(state.status))
|
|
386
|
+
state.status = 'cancelled';
|
|
387
|
+
else if (reason === 'interrupted' && state.status === 'running' && !current.calls.has(id)) {
|
|
388
|
+
state.status = 'interrupted';
|
|
389
|
+
const attempt = state.attempts.at(-1);
|
|
390
|
+
attempt.error = { kind: 'interrupted', message: 'Execution was interrupted; effects may already have occurred' };
|
|
391
|
+
attempt.finishedAt ??= Date.now();
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const work = tree.flatMap(current => [...current.running.values()]);
|
|
396
|
+
try {
|
|
397
|
+
this.persist(active);
|
|
398
|
+
}
|
|
399
|
+
finally {
|
|
400
|
+
for (const current of tree)
|
|
401
|
+
for (const call of current.calls.values())
|
|
402
|
+
clearTimeout(call.timer);
|
|
403
|
+
for (const running of work)
|
|
404
|
+
running.controller.abort();
|
|
405
|
+
await Promise.all(work.map(running => running.done));
|
|
406
|
+
for (const current of tree)
|
|
407
|
+
if (reason === 'cancelled' && current.record.status === 'cancelled') {
|
|
408
|
+
current.record.finishedAt ??= Date.now();
|
|
409
|
+
for (const state of Object.values(current.record.steps))
|
|
410
|
+
if (state.status === 'cancelled') {
|
|
411
|
+
const attempt = state.attempts.at(-1);
|
|
412
|
+
if (attempt)
|
|
413
|
+
attempt.finishedAt ??= Date.now();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
this.persist(active);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
startCall(active, step, attempt) {
|
|
420
|
+
const root = active.root ?? active;
|
|
421
|
+
const definition = root.record.retainedDefinitions?.[step.workflowId];
|
|
422
|
+
if (!definition)
|
|
423
|
+
throw new Error('Missing retained called Workflow Definition');
|
|
424
|
+
const graph = validateDefinition(definition);
|
|
425
|
+
const record = {
|
|
426
|
+
...executionIdentity(root.record), id: randomUUID(), scope: root.record.scope, definition: graph.definition,
|
|
427
|
+
input: structuredClone(attempt.input), status: 'running', startedAt: Date.now(),
|
|
428
|
+
steps: Object.fromEntries(graph.order.map(step => [step.id, { status: 'pending', attempts: [] }])),
|
|
429
|
+
loops: Object.fromEntries(graph.loops.map(loop => [loop.headerId, { activation: 0, try: 0, phase: 'inactive', grants: [] }])),
|
|
430
|
+
};
|
|
431
|
+
const child = { record, graph, parent: active, root, callStepId: step.id, callAttempt: attempt.number,
|
|
432
|
+
inheritedAsk: active.inheritedAsk || active.record.definition.permission === 'ask' || step.permission === 'ask',
|
|
433
|
+
running: new Map(), calls: new Map(), waiters: [], stopped: false };
|
|
434
|
+
active.calls.set(step.id, { child });
|
|
435
|
+
if (step.timeoutMs !== undefined)
|
|
436
|
+
attempt.deadlineAt = attempt.startedAt + step.timeoutMs;
|
|
437
|
+
this.persist(active);
|
|
438
|
+
this.armCall(active, step.id);
|
|
439
|
+
if (attempt.deadlineAt === undefined || attempt.deadlineAt > Date.now())
|
|
440
|
+
this.pump(child);
|
|
441
|
+
}
|
|
442
|
+
armCall(active, stepId) {
|
|
443
|
+
const call = active.calls.get(stepId);
|
|
444
|
+
clearTimeout(call.timer);
|
|
445
|
+
const deadline = active.record.steps[stepId].attempts.at(-1).deadlineAt;
|
|
446
|
+
if (deadline === undefined || call.stopping)
|
|
447
|
+
return;
|
|
448
|
+
call.timer = setTimeout(() => {
|
|
449
|
+
call.stopping = this.stopTree(call.child, 'cancelled').then(() => {
|
|
450
|
+
if (active.stopped)
|
|
451
|
+
return;
|
|
452
|
+
const state = active.record.steps[stepId];
|
|
453
|
+
const attempt = state.attempts.at(-1);
|
|
454
|
+
state.status = 'timed-out';
|
|
455
|
+
state.outcome = 'timeout';
|
|
456
|
+
attempt.error = { kind: 'timeout', message: 'Step timed out' };
|
|
457
|
+
attempt.finishedAt = Date.now();
|
|
458
|
+
this.persist(active);
|
|
459
|
+
active.calls.delete(stepId);
|
|
460
|
+
const root = active.root ?? active;
|
|
461
|
+
const resumed = root.record.status === 'recovery-required';
|
|
462
|
+
for (let current = active; current; current = current.parent) {
|
|
463
|
+
current.record.status = 'running';
|
|
464
|
+
delete current.record.finishedAt;
|
|
465
|
+
}
|
|
466
|
+
if (resumed)
|
|
467
|
+
this.onResume?.(structuredClone(root.record));
|
|
468
|
+
this.pump(active);
|
|
469
|
+
}).catch(error => { (active.root ?? active).persistenceError = error; this.failStorage(active.root ?? active); this.settle(active.root ?? active); }).finally(() => { delete call.stopping; });
|
|
470
|
+
}, Math.max(0, deadline - Date.now()));
|
|
471
|
+
}
|
|
472
|
+
resumeCalls(active) {
|
|
473
|
+
for (const [stepId, call] of active.calls) {
|
|
474
|
+
this.armCall(active, stepId);
|
|
475
|
+
if (call.child.record.status === 'completed' || call.child.record.status === 'completed-with-recovery')
|
|
476
|
+
this.completeCall(call.child);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
completeCall(child) {
|
|
480
|
+
const parent = child.parent;
|
|
481
|
+
const stepId = child.callStepId;
|
|
482
|
+
const call = parent.calls.get(stepId);
|
|
483
|
+
if (!call || call.child !== child || call.stopping || parent.stopped)
|
|
484
|
+
return;
|
|
485
|
+
if (child.record.status === 'completed' || child.record.status === 'completed-with-recovery') {
|
|
486
|
+
clearTimeout(call.timer);
|
|
487
|
+
const state = parent.record.steps[stepId];
|
|
488
|
+
const attempt = state.attempts.at(-1);
|
|
489
|
+
const step = parent.graph.order.find(step => step.id === stepId);
|
|
490
|
+
try {
|
|
491
|
+
const output = this.validateOutput(parent, step, child.record.result);
|
|
492
|
+
state.status = 'completed';
|
|
493
|
+
state.outcome = 'success';
|
|
494
|
+
state.output = output;
|
|
495
|
+
attempt.output = output;
|
|
496
|
+
}
|
|
497
|
+
catch (error) {
|
|
498
|
+
state.status = 'failed';
|
|
499
|
+
state.outcome = 'failure';
|
|
500
|
+
attempt.error = { kind: 'failure', message: error instanceof Error ? error.message : String(error) };
|
|
501
|
+
}
|
|
502
|
+
attempt.finishedAt = Date.now();
|
|
503
|
+
this.persist(child);
|
|
504
|
+
parent.calls.delete(stepId);
|
|
505
|
+
}
|
|
506
|
+
this.pump(parent);
|
|
507
|
+
}
|
|
236
508
|
require(sessionId, executionId) {
|
|
237
509
|
const active = this.active.get(executionId);
|
|
238
|
-
if (!active || active.record
|
|
510
|
+
if (!active || !executionAccessibleBy(active.record, sessionId))
|
|
239
511
|
throw new Error('Unknown workflow execution');
|
|
240
512
|
return active;
|
|
241
513
|
}
|
|
@@ -256,8 +528,17 @@ export class WorkflowScheduler {
|
|
|
256
528
|
const handled = !state.recovery && active.graph.outgoing.get(edge.from).some(candidate => candidate.outcome === state.outcome);
|
|
257
529
|
return handled || edge.outcome === 'failure' || edge.outcome === 'timeout' ? 'skipped' : 'blocked';
|
|
258
530
|
}
|
|
531
|
+
hasExpiredAncestor(active) {
|
|
532
|
+
const now = Date.now();
|
|
533
|
+
for (let child = active; child.parent; child = child.parent) {
|
|
534
|
+
const deadline = child.parent.record.steps[child.callStepId].attempts[child.callAttempt - 1].deadlineAt;
|
|
535
|
+
if (deadline !== undefined && deadline <= now)
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
259
540
|
pump(active) {
|
|
260
|
-
if (active.stopped || active.record.status !== 'running')
|
|
541
|
+
if (active.stopped || active.record.status !== 'running' || this.hasExpiredAncestor(active))
|
|
261
542
|
return;
|
|
262
543
|
this.transitionLoops(active);
|
|
263
544
|
for (const step of active.graph.order) {
|
|
@@ -314,23 +595,25 @@ export class WorkflowScheduler {
|
|
|
314
595
|
state.attempts.push({ number: state.attempts.length + 1, action: retry ? 'retry' : 'execute', startedAt: Date.now(), input, ...(identities.length ? { loops: identities } : {}) });
|
|
315
596
|
const done = Promise.resolve().then(() => this.execute(active, step, controller, inputError)).finally(() => {
|
|
316
597
|
active.running.delete(step.id);
|
|
598
|
+
const root = active.root ?? active;
|
|
317
599
|
if (active.stopped) {
|
|
318
|
-
if (!
|
|
319
|
-
this.settle(
|
|
600
|
+
if (!this.hasWork(root) && root.persistenceError)
|
|
601
|
+
this.settle(root);
|
|
320
602
|
return;
|
|
321
603
|
}
|
|
322
604
|
this.pump(active);
|
|
323
605
|
}).catch(error => {
|
|
324
|
-
active.
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
606
|
+
const root = active.root ?? active;
|
|
607
|
+
root.persistenceError = error;
|
|
608
|
+
this.failStorage(root);
|
|
609
|
+
if (!this.hasWork(root))
|
|
610
|
+
this.settle(root);
|
|
328
611
|
});
|
|
329
612
|
state.status = 'running';
|
|
330
613
|
active.running.set(step.id, { controller, done });
|
|
331
614
|
}
|
|
332
615
|
this.persist(active);
|
|
333
|
-
if (active
|
|
616
|
+
if (this.hasWork(active))
|
|
334
617
|
return;
|
|
335
618
|
if (this.transitionLoops(active)) {
|
|
336
619
|
this.persist(active);
|
|
@@ -338,13 +621,15 @@ export class WorkflowScheduler {
|
|
|
338
621
|
return;
|
|
339
622
|
}
|
|
340
623
|
const states = Object.entries(active.record.steps);
|
|
341
|
-
const unresolved = Object.values(active.record.loops ?? {}).some(loop => loop.phase === 'limit') || states.some(([id, state]) => ['pending', 'interrupted', 'blocked'].includes(state.status) || ((state.status === 'failed' || state.status === 'timed-out') && (active.record.testStepId || state.recovery || !active.graph.outgoing.get(id).some(edge => edge.outcome === state.outcome))));
|
|
624
|
+
const unresolved = active.calls.size > 0 || Object.values(active.record.loops ?? {}).some(loop => loop.phase === 'limit') || states.some(([id, state]) => ['pending', 'interrupted', 'blocked'].includes(state.status) || ((state.status === 'failed' || state.status === 'timed-out') && (active.record.testStepId || state.recovery || !active.graph.outgoing.get(id).some(edge => edge.outcome === state.outcome))));
|
|
342
625
|
if (unresolved)
|
|
343
626
|
active.record.status = 'recovery-required';
|
|
344
627
|
else {
|
|
345
628
|
const terminals = active.graph.order.filter(step => active.record.steps[step.id].status === 'completed' && (active.record.testStepId || !active.graph.outgoing.get(step.id).some(edge => this.edgeState(active, edge) === 'selected')));
|
|
346
629
|
active.record.result = terminals.length === 1 ? active.record.steps[terminals[0].id].output : Object.fromEntries(terminals.map(step => [step.name, active.record.steps[step.id].output]));
|
|
347
|
-
|
|
630
|
+
const invocations = (active.root ?? active).record.invocations ?? {};
|
|
631
|
+
const descends = (id) => id !== undefined && (id === active.record.id || descends(invocations[id].parentInvocationId));
|
|
632
|
+
active.record.status = Object.values(active.record.loops ?? {}).some(loop => loop.grants.length) || Object.values(invocations).some(invocation => invocation.status === 'completed-with-recovery' && (!active.parent || descends(invocation.parentInvocationId))) || states.some(([, state]) => state.attempts.some(attempt => attempt.error || attempt.action !== 'execute')) ? 'completed-with-recovery' : 'completed';
|
|
348
633
|
active.record.finishedAt = Date.now();
|
|
349
634
|
}
|
|
350
635
|
this.persist(active);
|
|
@@ -453,8 +738,13 @@ export class WorkflowScheduler {
|
|
|
453
738
|
return copy;
|
|
454
739
|
}
|
|
455
740
|
settle(active) {
|
|
741
|
+
if (active.parent) {
|
|
742
|
+
if (!active.stopped)
|
|
743
|
+
this.completeCall(active);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
456
746
|
if (!occupied(active.record))
|
|
457
|
-
this.slots.delete(active.record
|
|
747
|
+
this.slots.delete(executionOwner(active.record));
|
|
458
748
|
for (const waiter of active.waiters.splice(0)) {
|
|
459
749
|
if (active.persistenceError)
|
|
460
750
|
waiter.reject(active.persistenceError);
|
|
@@ -463,23 +753,14 @@ export class WorkflowScheduler {
|
|
|
463
753
|
}
|
|
464
754
|
}
|
|
465
755
|
persist(active) {
|
|
756
|
+
const root = active.root ?? active;
|
|
466
757
|
try {
|
|
467
|
-
this.
|
|
758
|
+
this.syncInvocations(root);
|
|
759
|
+
this.store.saveExecution(root.record);
|
|
468
760
|
}
|
|
469
761
|
catch (error) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
active.record.status = 'recovery-required';
|
|
473
|
-
for (const step of Object.values(active.record.steps)) {
|
|
474
|
-
if (step.status !== 'running')
|
|
475
|
-
continue;
|
|
476
|
-
step.status = 'interrupted';
|
|
477
|
-
const attempt = step.attempts.at(-1);
|
|
478
|
-
attempt.error = { kind: 'interrupted', message: 'Workflow storage failed; effects may already have occurred' };
|
|
479
|
-
attempt.finishedAt = Date.now();
|
|
480
|
-
}
|
|
481
|
-
for (const running of active.running.values())
|
|
482
|
-
running.controller.abort();
|
|
762
|
+
root.persistenceError = error;
|
|
763
|
+
this.failStorage(root);
|
|
483
764
|
throw error;
|
|
484
765
|
}
|
|
485
766
|
}
|
|
@@ -519,6 +800,8 @@ export class WorkflowScheduler {
|
|
|
519
800
|
this.persist(active);
|
|
520
801
|
return;
|
|
521
802
|
}
|
|
803
|
+
if (this.hasExpiredAncestor(active))
|
|
804
|
+
return;
|
|
522
805
|
const attempt = state.attempts.at(-1);
|
|
523
806
|
let timer;
|
|
524
807
|
let timedOut = false;
|
|
@@ -527,7 +810,14 @@ export class WorkflowScheduler {
|
|
|
527
810
|
throw inputError;
|
|
528
811
|
if (step.inputSchema)
|
|
529
812
|
attempt.input = parseValue(step.inputSchema, attempt.input);
|
|
813
|
+
if (step.kind === 'workflow') {
|
|
814
|
+
attempt.input = parseValue(step.contract.inputSchema, attempt.input);
|
|
815
|
+
this.startCall(active, step, attempt);
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
530
818
|
this.persist(active);
|
|
819
|
+
if (this.hasExpiredAncestor(active))
|
|
820
|
+
return;
|
|
531
821
|
const timeout = step.timeoutMs ?? (step.kind === 'shell' || step.kind === 'typescript' || step.kind === 'mcp' ? 60_000 : undefined);
|
|
532
822
|
if (timeout !== undefined)
|
|
533
823
|
timer = setTimeout(() => { timedOut = true; controller.abort(); }, timeout);
|
|
@@ -558,7 +848,7 @@ export class WorkflowScheduler {
|
|
|
558
848
|
}
|
|
559
849
|
}
|
|
560
850
|
else
|
|
561
|
-
value = await this.executors[step.kind].execute({ sessionId: active.record.sessionId, scope: active.record.scope, executionId: active.record.id, step: this.guidedStep(active, step, attempt), input: structuredClone(attempt.input), inputSchema: active.graph.inputSchemas.get(step.id), permission: step.permission ?? active.record.definition.permission ?? 'auto-accept', signal: controller.signal });
|
|
851
|
+
value = await this.executors[step.kind].execute({ owner: executionOwnerOf(active.record), ownerId: executionOwner(active.record), ...(active.record.sessionId ? { sessionId: active.record.sessionId } : {}), ...(active.record.occurrenceId ? { occurrenceId: active.record.occurrenceId } : {}), scope: active.record.scope, executionId: (active.root ?? active).record.id, ...(active.parent ? { invocationId: active.record.id } : {}), attempt: attempt.number, step: this.guidedStep(active, step, attempt), input: structuredClone(attempt.input), inputSchema: active.graph.inputSchemas.get(step.id), permission: active.inheritedAsk ? 'ask' : step.permission ?? active.record.definition.permission ?? 'auto-accept', signal: controller.signal });
|
|
562
852
|
if (active.stopped)
|
|
563
853
|
return;
|
|
564
854
|
if (timedOut)
|
|
@@ -580,7 +870,8 @@ export class WorkflowScheduler {
|
|
|
580
870
|
}
|
|
581
871
|
finally {
|
|
582
872
|
clearTimeout(timer);
|
|
583
|
-
|
|
873
|
+
if (!active.calls.has(step.id))
|
|
874
|
+
attempt.finishedAt ??= Date.now();
|
|
584
875
|
this.persist(active);
|
|
585
876
|
}
|
|
586
877
|
}
|