@abelxiaoxing/cadence 1.3.0 → 1.4.1
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 +172 -13
- package/agents/design-explorer.md +17 -11
- package/agents/diagnosis-worker.md +9 -1
- package/agents/implementation-worker.md +3 -1
- package/config/plan-draft.example.json +122 -0
- package/config/plan-draft.multiple-tasks.example.json +345 -0
- package/package.json +9 -5
- package/prompts/abel-design.md +141 -38
- package/prompts/abel-diagnose.md +10 -1
- package/prompts/abel-implement.md +118 -20
- package/prompts/abel-init.md +9 -2
- package/src/activity-contracts.ts +82 -0
- package/src/apply-transaction.ts +16 -14
- package/src/artifact-store.ts +17 -0
- package/src/candidate-artifact.ts +143 -0
- package/src/candidate-patch.ts +33 -12
- package/src/candidate-workspace.ts +74 -0
- package/src/canonical.ts +16 -0
- package/src/change-contract-schema.ts +191 -0
- package/src/change-contract.ts +273 -0
- package/src/change-verification.ts +896 -0
- package/src/child-model.ts +129 -0
- package/src/child-session.ts +296 -328
- package/src/contracts.ts +325 -109
- package/src/control-contracts.ts +51 -1
- package/src/delivery-compiler.ts +241 -141
- package/src/delivery-revision.ts +42 -0
- package/src/design-control.ts +182 -15
- package/src/design-diagnostics.ts +150 -0
- package/src/design-journal.ts +216 -72
- package/src/durable-contracts.ts +210 -0
- package/src/durable-workflow.ts +963 -0
- package/src/evidence-draft.ts +122 -0
- package/src/implement-graph.ts +14 -16
- package/src/implement-plan.ts +70 -0
- package/src/index.ts +341 -1405
- package/src/isolation-backend.ts +84 -23
- package/src/model-source.ts +22 -0
- package/src/package-candidate.ts +224 -0
- package/src/package-delivery.ts +486 -0
- package/src/package-verification.ts +509 -0
- package/src/package-workflow.ts +253 -0
- package/src/packet-runtime.ts +27 -48
- package/src/parent-provider.ts +86 -198
- package/src/phase-execution.ts +2248 -0
- package/src/pi-adapter.ts +19 -0
- package/src/plan-draft-summary.ts +146 -0
- package/src/plan-draft.ts +493 -0
- package/src/route-policy.ts +1 -11
- package/src/run-state.ts +4 -16
- package/src/run-store.ts +5 -144
- package/src/scoped-grep-worker.mjs +23 -0
- package/src/scoped-tools.ts +312 -116
- package/src/sqlite-schema.ts +171 -0
- package/src/storage-schema.ts +352 -0
- package/src/subagent-activity.ts +46 -67
- package/src/submit-tool.ts +40 -61
- package/src/task-ledger.ts +14 -55
- package/src/transport-budget.ts +32 -0
- package/src/verification-capability.ts +298 -102
- package/src/verification-environment.ts +213 -0
- package/src/verification-identity.ts +77 -0
- package/src/worker-broker.ts +134 -91
- package/src/workflow-engine.ts +28 -8285
- package/src/workflow-policy.ts +1388 -0
- package/src/workflow-recovery-policy.ts +95 -0
- package/src/workflow-scheduling.ts +105 -0
- package/src/workflow-state-machine.ts +4086 -0
- package/src/workflow-status.ts +384 -0
- package/src/workspace-io-entry.mjs +58 -0
- package/src/workspace-io-worker.mjs +991 -0
- package/src/workspace-io.ts +84 -0
- package/src/workspace-store.ts +125 -19
- package/skills/abel-workflow/SKILL.md +0 -205
- package/src/empty-resource-loader.ts +0 -38
- package/src/parent-payload-bridge.ts +0 -523
|
@@ -0,0 +1,963 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChangeVerification,
|
|
3
|
+
parseVerificationBaseline,
|
|
4
|
+
sha256Bytes,
|
|
5
|
+
verificationBaselineFact,
|
|
6
|
+
} from "./change-verification.ts";
|
|
7
|
+
import type {
|
|
8
|
+
DurableExecutionResources,
|
|
9
|
+
DurablePhaseFact,
|
|
10
|
+
DurableResourceInput,
|
|
11
|
+
DurableWorkflowEngineOptions,
|
|
12
|
+
} from "./durable-contracts.ts";
|
|
13
|
+
import { PhaseExecution } from "./phase-execution.ts";
|
|
14
|
+
|
|
15
|
+
export type {
|
|
16
|
+
DurableChangeVerificationResult,
|
|
17
|
+
DurablePhaseVerificationResult,
|
|
18
|
+
DurableWorkflowEngineOptions,
|
|
19
|
+
} from "./durable-contracts.ts";
|
|
20
|
+
|
|
21
|
+
interface DurableRunResources extends DurableExecutionResources {
|
|
22
|
+
transactions: ApplyTransaction;
|
|
23
|
+
closed: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
import { lstatSync, mkdirSync, readFileSync, rmSync } from "node:fs";
|
|
27
|
+
import path from "node:path";
|
|
28
|
+
import { DatabaseSync } from "node:sqlite";
|
|
29
|
+
import { ApplyTransaction } from "./apply-transaction.ts";
|
|
30
|
+
import { ArtifactStore } from "./artifact-store.ts";
|
|
31
|
+
import { compareCanonicalStrings } from "./canonical.ts";
|
|
32
|
+
import type { ImplementPlan, PlanTaskDraft } from "./delivery-compiler.ts";
|
|
33
|
+
import type { RoutePolicy } from "./route-policy.ts";
|
|
34
|
+
import { observeSafePath } from "./safe-path.ts";
|
|
35
|
+
import { configureSqlite, ensureSqliteSchema } from "./sqlite-schema.ts";
|
|
36
|
+
import { ROUTE_HEALTH_SCHEMA } from "./storage-schema.ts";
|
|
37
|
+
import { TaskLedger } from "./task-ledger.ts";
|
|
38
|
+
import { type RouteHealthStore, RunWorkerBroker } from "./worker-broker.ts";
|
|
39
|
+
import {
|
|
40
|
+
deliveryBoundPaths,
|
|
41
|
+
deliveryTrackingPath,
|
|
42
|
+
hash,
|
|
43
|
+
hasVerificationLifecycle,
|
|
44
|
+
IDENTIFIER,
|
|
45
|
+
implementationRouteRequirements,
|
|
46
|
+
isRecord,
|
|
47
|
+
SHA256,
|
|
48
|
+
type WorkflowApplication,
|
|
49
|
+
type WorkflowApplicationContext,
|
|
50
|
+
type WorkflowChangeVerifier,
|
|
51
|
+
type WorkflowRunLifecycle,
|
|
52
|
+
type WorkflowWorker,
|
|
53
|
+
workspaceEntryEqual,
|
|
54
|
+
} from "./workflow-policy.ts";
|
|
55
|
+
import { WorkflowEngine } from "./workflow-state-machine.ts";
|
|
56
|
+
import { type WorkspaceRevision, WorkspaceStore } from "./workspace-store.ts";
|
|
57
|
+
|
|
58
|
+
type DurableRouteHealth = Parameters<RouteHealthStore["set"]>[1];
|
|
59
|
+
|
|
60
|
+
class DurableRouteHealthStore implements RouteHealthStore {
|
|
61
|
+
readonly #databasePath: string;
|
|
62
|
+
#database?: DatabaseSync;
|
|
63
|
+
#closed = false;
|
|
64
|
+
|
|
65
|
+
constructor(databasePath: string) {
|
|
66
|
+
this.#databasePath = databasePath;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#open(): DatabaseSync {
|
|
70
|
+
if (this.#closed) throw new Error("route-health-store-closed");
|
|
71
|
+
if (this.#database) return this.#database;
|
|
72
|
+
const database = new DatabaseSync(this.#databasePath);
|
|
73
|
+
try {
|
|
74
|
+
configureSqlite(database);
|
|
75
|
+
ensureSqliteSchema(database, ROUTE_HEALTH_SCHEMA);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
database.close();
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.#database = database;
|
|
82
|
+
return database;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get(fingerprint: string): DurableRouteHealth | undefined {
|
|
86
|
+
if (!SHA256.test(fingerprint)) return undefined;
|
|
87
|
+
const row = this.#open()
|
|
88
|
+
.prepare(
|
|
89
|
+
`SELECT state, next_half_open_at, projection_json
|
|
90
|
+
FROM route_health WHERE route_fingerprint = ?`,
|
|
91
|
+
)
|
|
92
|
+
.get(fingerprint) as
|
|
93
|
+
| {
|
|
94
|
+
state: string;
|
|
95
|
+
next_half_open_at: number | null;
|
|
96
|
+
projection_json: string;
|
|
97
|
+
}
|
|
98
|
+
| undefined;
|
|
99
|
+
if (!row || !["healthy", "open", "half-open"].includes(row.state)) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
let projection: unknown;
|
|
103
|
+
try {
|
|
104
|
+
projection = JSON.parse(row.projection_json);
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
if (!isRecord(projection) || projection.state !== row.state) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
const retryAt = row.next_half_open_at ?? undefined;
|
|
112
|
+
const lastCode =
|
|
113
|
+
typeof projection.lastCode === "string" &&
|
|
114
|
+
projection.lastCode.length > 0 &&
|
|
115
|
+
projection.lastCode.length <= 128
|
|
116
|
+
? projection.lastCode
|
|
117
|
+
: undefined;
|
|
118
|
+
const probeExpiresAt =
|
|
119
|
+
Number.isSafeInteger(projection.probeExpiresAt) &&
|
|
120
|
+
(projection.probeExpiresAt as number) >= 0
|
|
121
|
+
? (projection.probeExpiresAt as number)
|
|
122
|
+
: undefined;
|
|
123
|
+
return {
|
|
124
|
+
state: row.state as DurableRouteHealth["state"],
|
|
125
|
+
...(retryAt === undefined ? {} : { retryAt }),
|
|
126
|
+
...(projection.probeInFlight === true ? { probeInFlight: true } : {}),
|
|
127
|
+
...(probeExpiresAt === undefined ? {} : { probeExpiresAt }),
|
|
128
|
+
...(lastCode === undefined ? {} : { lastCode }),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
set(fingerprint: string, health: DurableRouteHealth): void {
|
|
133
|
+
if (
|
|
134
|
+
!SHA256.test(fingerprint) ||
|
|
135
|
+
!["healthy", "open", "half-open"].includes(health.state) ||
|
|
136
|
+
(health.retryAt !== undefined &&
|
|
137
|
+
(!Number.isSafeInteger(health.retryAt) || health.retryAt < 0)) ||
|
|
138
|
+
(health.probeExpiresAt !== undefined &&
|
|
139
|
+
(!Number.isSafeInteger(health.probeExpiresAt) ||
|
|
140
|
+
health.probeExpiresAt < 0))
|
|
141
|
+
) {
|
|
142
|
+
throw new Error("route-health-fact-invalid");
|
|
143
|
+
}
|
|
144
|
+
const projection = {
|
|
145
|
+
state: health.state,
|
|
146
|
+
...(health.probeInFlight ? { probeInFlight: true } : {}),
|
|
147
|
+
...(health.probeExpiresAt === undefined
|
|
148
|
+
? {}
|
|
149
|
+
: { probeExpiresAt: health.probeExpiresAt }),
|
|
150
|
+
...(health.lastCode ? { lastCode: health.lastCode.slice(0, 128) } : {}),
|
|
151
|
+
};
|
|
152
|
+
this.#open()
|
|
153
|
+
.prepare(
|
|
154
|
+
`INSERT INTO route_health(
|
|
155
|
+
route_fingerprint, state, next_half_open_at, projection_json
|
|
156
|
+
) VALUES (?, ?, ?, ?)
|
|
157
|
+
ON CONFLICT(route_fingerprint) DO UPDATE SET
|
|
158
|
+
state = excluded.state,
|
|
159
|
+
next_half_open_at = excluded.next_half_open_at,
|
|
160
|
+
projection_json = excluded.projection_json`,
|
|
161
|
+
)
|
|
162
|
+
.run(
|
|
163
|
+
fingerprint,
|
|
164
|
+
health.state,
|
|
165
|
+
health.retryAt ?? null,
|
|
166
|
+
JSON.stringify(projection),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
close(): void {
|
|
171
|
+
if (this.#closed) return;
|
|
172
|
+
this.#database?.close();
|
|
173
|
+
this.#closed = true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function ensurePrivateRunDirectory(directory: string): void {
|
|
178
|
+
const existing = lstatSync(directory, { throwIfNoEntry: false });
|
|
179
|
+
if (existing) {
|
|
180
|
+
if (existing.isSymbolicLink() || !existing.isDirectory()) {
|
|
181
|
+
throw new Error("workflow-run-data-unsafe");
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
186
|
+
const created = lstatSync(directory);
|
|
187
|
+
if (created.isSymbolicLink() || !created.isDirectory()) {
|
|
188
|
+
throw new Error("workflow-run-data-unsafe");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
class DurableWorkflowComposition
|
|
193
|
+
implements
|
|
194
|
+
WorkflowWorker,
|
|
195
|
+
WorkflowChangeVerifier,
|
|
196
|
+
WorkflowApplication,
|
|
197
|
+
WorkflowRunLifecycle
|
|
198
|
+
{
|
|
199
|
+
readonly #phase: PhaseExecution;
|
|
200
|
+
readonly #verification: ChangeVerification;
|
|
201
|
+
readonly #options: DurableWorkflowEngineOptions;
|
|
202
|
+
readonly #routeHealth: DurableRouteHealthStore;
|
|
203
|
+
#broker: RunWorkerBroker;
|
|
204
|
+
readonly #runs = new Map<string, DurableRunResources>();
|
|
205
|
+
|
|
206
|
+
constructor(options: DurableWorkflowEngineOptions) {
|
|
207
|
+
this.#options = options;
|
|
208
|
+
this.#routeHealth = new DurableRouteHealthStore(
|
|
209
|
+
options.stateRoot.databasePath,
|
|
210
|
+
);
|
|
211
|
+
this.#broker = new RunWorkerBroker(options.routePolicy, {
|
|
212
|
+
...(options.now ? { now: options.now } : {}),
|
|
213
|
+
healthStore: this.#routeHealth,
|
|
214
|
+
});
|
|
215
|
+
this.#verification = new ChangeVerification(options, {
|
|
216
|
+
prepareResources: (input, signal) =>
|
|
217
|
+
this.#prepareResources(input, signal),
|
|
218
|
+
ledger: (resources, revision) => this.#ledger(resources, revision),
|
|
219
|
+
run: (runId) => this.#runs.get(runId),
|
|
220
|
+
revalidatePhasePolicy: (...args) =>
|
|
221
|
+
this.#phase.revalidatePhasePolicy(...args),
|
|
222
|
+
});
|
|
223
|
+
this.#phase = new PhaseExecution(options, this.#broker, {
|
|
224
|
+
prepareResources: (input, signal) =>
|
|
225
|
+
this.#prepareResources(input, signal),
|
|
226
|
+
ledger: (resources, revision) => this.#ledger(resources, revision),
|
|
227
|
+
ensureVerificationBaseline: (input) =>
|
|
228
|
+
this.#verification.ensureVerificationBaseline(input),
|
|
229
|
+
verifyTaskAffected: (input) =>
|
|
230
|
+
this.#verification.verifyTaskAffected(input),
|
|
231
|
+
verifyPhase: (input) => this.#verification.verifyPhase(input),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
updateRoutePolicy(policy: RoutePolicy): void {
|
|
236
|
+
this.#broker.updatePolicy(policy);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
routePolicyStatus(): Record<string, unknown> {
|
|
240
|
+
return this.#broker.status();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#runRoot(runId: string): string {
|
|
244
|
+
if (!IDENTIFIER.test(runId)) throw new Error("workflow-run-id-invalid");
|
|
245
|
+
const dataRoot = path.join(this.#options.stateRoot.rootDir, "run-data");
|
|
246
|
+
const root = path.join(dataRoot, runId);
|
|
247
|
+
const relative = path.relative(dataRoot, root);
|
|
248
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
249
|
+
throw new Error("workflow-run-data-unsafe");
|
|
250
|
+
}
|
|
251
|
+
return root;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#captureBaseline(
|
|
255
|
+
workspaces: WorkspaceStore,
|
|
256
|
+
plan: ImplementPlan,
|
|
257
|
+
signal: AbortSignal,
|
|
258
|
+
) {
|
|
259
|
+
const candidates = new Set<string>();
|
|
260
|
+
const absent = new Set<string>();
|
|
261
|
+
for (const relative of deliveryBoundPaths(plan)) {
|
|
262
|
+
const observation = observeSafePath(this.#options.consumerRoot, relative);
|
|
263
|
+
if (observation.kind === "file") candidates.add(relative);
|
|
264
|
+
else if (observation.kind === "absent") absent.add(relative);
|
|
265
|
+
else throw new Error("workflow-baseline-path-unsafe");
|
|
266
|
+
}
|
|
267
|
+
return workspaces.captureBaselineAsync(
|
|
268
|
+
{
|
|
269
|
+
consumerRoot: this.#options.consumerRoot,
|
|
270
|
+
approvedUntracked: [...candidates].sort(),
|
|
271
|
+
absent: [...absent].sort(),
|
|
272
|
+
},
|
|
273
|
+
signal,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
readonly #initializing = new Map<string, Promise<DurableRunResources>>();
|
|
278
|
+
|
|
279
|
+
async #prepareResources(
|
|
280
|
+
input: DurableResourceInput,
|
|
281
|
+
signal: AbortSignal,
|
|
282
|
+
): Promise<DurableRunResources> {
|
|
283
|
+
signal.throwIfAborted();
|
|
284
|
+
const pending = this.#initializing.get(input.runId);
|
|
285
|
+
if (pending) return pending;
|
|
286
|
+
if (
|
|
287
|
+
this.#runs.has(input.runId) ||
|
|
288
|
+
(input.baselineRevisionId && input.currentWorkspaceRevisionId)
|
|
289
|
+
)
|
|
290
|
+
return this.#resources(input);
|
|
291
|
+
const initialize = async () => {
|
|
292
|
+
if (
|
|
293
|
+
!input.plan ||
|
|
294
|
+
input.baselineRevisionId ||
|
|
295
|
+
input.currentWorkspaceRevisionId
|
|
296
|
+
)
|
|
297
|
+
throw new Error("workflow-workspace-revision-unavailable");
|
|
298
|
+
const root = this.#runRoot(input.runId);
|
|
299
|
+
ensurePrivateRunDirectory(root);
|
|
300
|
+
const artifacts = new ArtifactStore(path.join(root, "artifacts"));
|
|
301
|
+
const workspaces = new WorkspaceStore(
|
|
302
|
+
path.join(root, "workspaces"),
|
|
303
|
+
artifacts,
|
|
304
|
+
);
|
|
305
|
+
const baseline = await this.#captureBaseline(
|
|
306
|
+
workspaces,
|
|
307
|
+
input.plan,
|
|
308
|
+
signal,
|
|
309
|
+
);
|
|
310
|
+
signal.throwIfAborted();
|
|
311
|
+
return this.#resources({
|
|
312
|
+
...input,
|
|
313
|
+
baselineRevisionId: baseline.revisionId,
|
|
314
|
+
currentWorkspaceRevisionId: baseline.revisionId,
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
const promise = initialize();
|
|
318
|
+
this.#initializing.set(input.runId, promise);
|
|
319
|
+
try {
|
|
320
|
+
return await promise;
|
|
321
|
+
} finally {
|
|
322
|
+
this.#initializing.delete(input.runId);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
#resources(input: {
|
|
327
|
+
runId: string;
|
|
328
|
+
deliveryRevision: number;
|
|
329
|
+
plan?: ImplementPlan;
|
|
330
|
+
baselineRevisionId?: string;
|
|
331
|
+
currentWorkspaceRevisionId?: string;
|
|
332
|
+
}): DurableRunResources {
|
|
333
|
+
const existing = this.#runs.get(input.runId);
|
|
334
|
+
if (existing) {
|
|
335
|
+
if (
|
|
336
|
+
input.baselineRevisionId &&
|
|
337
|
+
existing.baselineRevisionId !== input.baselineRevisionId
|
|
338
|
+
) {
|
|
339
|
+
throw new Error("workflow-baseline-revision-conflict");
|
|
340
|
+
}
|
|
341
|
+
if (input.plan) {
|
|
342
|
+
existing.plan = structuredClone(input.plan);
|
|
343
|
+
existing.deliveryRevision = input.deliveryRevision;
|
|
344
|
+
}
|
|
345
|
+
return existing;
|
|
346
|
+
}
|
|
347
|
+
if (!input.plan) {
|
|
348
|
+
throw new Error("workflow-plan-unavailable");
|
|
349
|
+
}
|
|
350
|
+
const root = this.#runRoot(input.runId);
|
|
351
|
+
ensurePrivateRunDirectory(root);
|
|
352
|
+
const artifacts = new ArtifactStore(path.join(root, "artifacts"));
|
|
353
|
+
const workspaces = new WorkspaceStore(
|
|
354
|
+
path.join(root, "workspaces"),
|
|
355
|
+
artifacts,
|
|
356
|
+
);
|
|
357
|
+
const baselineRevisionId = input.baselineRevisionId;
|
|
358
|
+
const currentRevisionId = input.currentWorkspaceRevisionId;
|
|
359
|
+
if (!baselineRevisionId || !currentRevisionId)
|
|
360
|
+
throw new Error("workflow-workspace-revision-unavailable");
|
|
361
|
+
workspaces.getRevision(baselineRevisionId);
|
|
362
|
+
workspaces.getRevision(currentRevisionId);
|
|
363
|
+
let resources!: DurableRunResources;
|
|
364
|
+
const transactions = new ApplyTransaction({
|
|
365
|
+
root: path.join(root, "transactions"),
|
|
366
|
+
artifacts,
|
|
367
|
+
workspaces,
|
|
368
|
+
hooks: {
|
|
369
|
+
postApply: ({ transactionId, root: verificationRoot, signal }) =>
|
|
370
|
+
this.#verification.verifyPostApply(
|
|
371
|
+
resources,
|
|
372
|
+
verificationRoot,
|
|
373
|
+
signal,
|
|
374
|
+
transactionId,
|
|
375
|
+
),
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
resources = {
|
|
379
|
+
runId: input.runId,
|
|
380
|
+
root,
|
|
381
|
+
plan: structuredClone(input.plan),
|
|
382
|
+
deliveryRevision: input.deliveryRevision,
|
|
383
|
+
artifacts,
|
|
384
|
+
workspaces,
|
|
385
|
+
ledgers: new Map(),
|
|
386
|
+
transactions,
|
|
387
|
+
baselineRevisionId,
|
|
388
|
+
currentRevisionId,
|
|
389
|
+
mergeTail: Promise.resolve(),
|
|
390
|
+
baselinePromises: new Map(),
|
|
391
|
+
closed: false,
|
|
392
|
+
};
|
|
393
|
+
this.#runs.set(input.runId, resources);
|
|
394
|
+
return resources;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
#closeResources(resources: DurableRunResources): void {
|
|
398
|
+
if (resources.closed) return;
|
|
399
|
+
for (const ledger of resources.ledgers.values()) ledger.close();
|
|
400
|
+
resources.ledgers.clear();
|
|
401
|
+
resources.transactions.close();
|
|
402
|
+
resources.closed = true;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
#ledger(
|
|
406
|
+
resources: DurableExecutionResources,
|
|
407
|
+
deliveryRevision: number,
|
|
408
|
+
): TaskLedger {
|
|
409
|
+
const existing = resources.ledgers.get(deliveryRevision);
|
|
410
|
+
if (existing) return existing;
|
|
411
|
+
const ledger = new TaskLedger({
|
|
412
|
+
root: path.join(
|
|
413
|
+
resources.root,
|
|
414
|
+
"ledgers",
|
|
415
|
+
`revision-${deliveryRevision}`,
|
|
416
|
+
),
|
|
417
|
+
artifacts: resources.artifacts,
|
|
418
|
+
});
|
|
419
|
+
resources.ledgers.set(deliveryRevision, ledger);
|
|
420
|
+
return ledger;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
#expandedBaseline(
|
|
424
|
+
resources: DurableRunResources,
|
|
425
|
+
plan: ImplementPlan,
|
|
426
|
+
): WorkspaceRevision {
|
|
427
|
+
const baseline = resources.workspaces.getRevision(
|
|
428
|
+
resources.baselineRevisionId,
|
|
429
|
+
);
|
|
430
|
+
const candidates = new Map<string, boolean>(
|
|
431
|
+
deliveryBoundPaths(plan).map((relative) => [relative, false]),
|
|
432
|
+
);
|
|
433
|
+
for (const task of plan.tasks) {
|
|
434
|
+
for (const phase of Object.values(task.phases)) {
|
|
435
|
+
for (const relative of phase.read) candidates.set(relative, false);
|
|
436
|
+
for (const relative of [...phase.write, ...phase.delete]) {
|
|
437
|
+
candidates.set(relative, true);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
for (const output of plan.outputs) candidates.set(output.path, true);
|
|
442
|
+
for (const task of plan.tasks) {
|
|
443
|
+
if (task.agents.impact === "none" || !task.agents.target) continue;
|
|
444
|
+
candidates.set(task.agents.target, task.agents.impact === "create-index");
|
|
445
|
+
}
|
|
446
|
+
const trackingPath = deliveryTrackingPath(plan);
|
|
447
|
+
if (
|
|
448
|
+
trackingPath &&
|
|
449
|
+
(Object.hasOwn(baseline.entries, trackingPath) ||
|
|
450
|
+
observeSafePath(this.#options.consumerRoot, trackingPath).kind ===
|
|
451
|
+
"file")
|
|
452
|
+
) {
|
|
453
|
+
candidates.set(trackingPath, false);
|
|
454
|
+
}
|
|
455
|
+
const changes: Record<
|
|
456
|
+
string,
|
|
457
|
+
{ kind: "absent" } | { kind: "file"; bytes: Uint8Array; mode: number }
|
|
458
|
+
> = {};
|
|
459
|
+
for (const [relative, mayBeAbsent] of [...candidates].sort(
|
|
460
|
+
([left], [right]) => compareCanonicalStrings(left, right),
|
|
461
|
+
)) {
|
|
462
|
+
if (relative === ".") continue;
|
|
463
|
+
const observation = observeSafePath(this.#options.consumerRoot, relative);
|
|
464
|
+
if (observation.kind === "absent") {
|
|
465
|
+
if (baseline.entries[relative]?.kind === "absent") continue;
|
|
466
|
+
if (!mayBeAbsent)
|
|
467
|
+
throw new Error("workflow-baseline-input-unavailable");
|
|
468
|
+
changes[relative] = { kind: "absent" };
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
if (observation.kind !== "file") {
|
|
472
|
+
throw new Error("workflow-baseline-path-unsafe");
|
|
473
|
+
}
|
|
474
|
+
const target = path.join(
|
|
475
|
+
this.#options.consumerRoot,
|
|
476
|
+
...relative.split("/"),
|
|
477
|
+
);
|
|
478
|
+
const stat = lstatSync(target);
|
|
479
|
+
const bytes = readFileSync(target);
|
|
480
|
+
const mode = stat.mode & 0o777;
|
|
481
|
+
const prior = baseline.entries[relative];
|
|
482
|
+
if (
|
|
483
|
+
prior?.kind === "file" &&
|
|
484
|
+
prior.hash === sha256Bytes(bytes) &&
|
|
485
|
+
prior.bytes === bytes.byteLength &&
|
|
486
|
+
prior.mode === mode
|
|
487
|
+
) {
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
changes[relative] = {
|
|
491
|
+
kind: "file",
|
|
492
|
+
bytes,
|
|
493
|
+
mode,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
return Object.keys(changes).length === 0
|
|
497
|
+
? baseline
|
|
498
|
+
: resources.workspaces.createRevision({
|
|
499
|
+
parentRevisionId: baseline.revisionId,
|
|
500
|
+
changes,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
#orderedTasks(plan: ImplementPlan): PlanTaskDraft[] {
|
|
505
|
+
const remaining = new Map(plan.tasks.map((task) => [task.taskId, task]));
|
|
506
|
+
const ordered: PlanTaskDraft[] = [];
|
|
507
|
+
const admitted = new Set<string>();
|
|
508
|
+
while (remaining.size > 0) {
|
|
509
|
+
let progressed = false;
|
|
510
|
+
for (const task of plan.tasks) {
|
|
511
|
+
if (
|
|
512
|
+
!remaining.has(task.taskId) ||
|
|
513
|
+
task.dependsOn.some((dependency) => !admitted.has(dependency))
|
|
514
|
+
) {
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
remaining.delete(task.taskId);
|
|
518
|
+
admitted.add(task.taskId);
|
|
519
|
+
ordered.push(task);
|
|
520
|
+
progressed = true;
|
|
521
|
+
}
|
|
522
|
+
if (!progressed) throw new Error("workflow-plan-dependency-cycle");
|
|
523
|
+
}
|
|
524
|
+
return ordered;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
#phaseFacts(
|
|
528
|
+
resources: DurableRunResources,
|
|
529
|
+
task: {
|
|
530
|
+
taskId: string;
|
|
531
|
+
deliveryRevision: number;
|
|
532
|
+
phase: "red" | "green" | "refactor";
|
|
533
|
+
},
|
|
534
|
+
): DurablePhaseFact[] {
|
|
535
|
+
let projection: unknown;
|
|
536
|
+
try {
|
|
537
|
+
projection = this.#ledger(resources, task.deliveryRevision).projection({
|
|
538
|
+
runId: resources.runId,
|
|
539
|
+
taskId: task.taskId,
|
|
540
|
+
nextPhase: task.phase,
|
|
541
|
+
});
|
|
542
|
+
} catch (error) {
|
|
543
|
+
if (
|
|
544
|
+
error instanceof Error &&
|
|
545
|
+
error.message === "ledger-task-unavailable"
|
|
546
|
+
) {
|
|
547
|
+
return [];
|
|
548
|
+
}
|
|
549
|
+
throw error;
|
|
550
|
+
}
|
|
551
|
+
if (!isRecord(projection) || !Array.isArray(projection.history)) {
|
|
552
|
+
throw new Error("workflow-ledger-projection-invalid");
|
|
553
|
+
}
|
|
554
|
+
return projection.history.flatMap((event): DurablePhaseFact[] => {
|
|
555
|
+
if (
|
|
556
|
+
!isRecord(event) ||
|
|
557
|
+
(event.kind !== "phase-verified" && event.kind !== "repair-verified")
|
|
558
|
+
) {
|
|
559
|
+
return [];
|
|
560
|
+
}
|
|
561
|
+
if (
|
|
562
|
+
!["red", "green", "refactor"].includes(String(event.phase)) ||
|
|
563
|
+
typeof event.isolatedRevisionId !== "string" ||
|
|
564
|
+
!SHA256.test(event.isolatedRevisionId) ||
|
|
565
|
+
!Array.isArray(event.outputFacts)
|
|
566
|
+
) {
|
|
567
|
+
throw new Error("workflow-ledger-phase-invalid");
|
|
568
|
+
}
|
|
569
|
+
const outputFacts = event.outputFacts.map((fact) => {
|
|
570
|
+
if (
|
|
571
|
+
!isRecord(fact) ||
|
|
572
|
+
typeof fact.path !== "string" ||
|
|
573
|
+
(fact.kind !== "absent" && fact.kind !== "file")
|
|
574
|
+
) {
|
|
575
|
+
throw new Error("workflow-ledger-phase-invalid");
|
|
576
|
+
}
|
|
577
|
+
if (fact.kind === "absent") {
|
|
578
|
+
return { path: fact.path, kind: "absent" as const };
|
|
579
|
+
}
|
|
580
|
+
if (
|
|
581
|
+
typeof fact.hash !== "string" ||
|
|
582
|
+
!SHA256.test(fact.hash) ||
|
|
583
|
+
!Number.isSafeInteger(fact.bytes) ||
|
|
584
|
+
(fact.bytes as number) < 0
|
|
585
|
+
) {
|
|
586
|
+
throw new Error("workflow-ledger-phase-invalid");
|
|
587
|
+
}
|
|
588
|
+
return {
|
|
589
|
+
path: fact.path,
|
|
590
|
+
kind: "file" as const,
|
|
591
|
+
hash: fact.hash,
|
|
592
|
+
bytes: fact.bytes as number,
|
|
593
|
+
};
|
|
594
|
+
});
|
|
595
|
+
return [
|
|
596
|
+
{
|
|
597
|
+
kind: event.kind,
|
|
598
|
+
phase: event.phase as DurablePhaseFact["phase"],
|
|
599
|
+
isolatedRevisionId: event.isolatedRevisionId,
|
|
600
|
+
outputFacts,
|
|
601
|
+
},
|
|
602
|
+
];
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
revalidateDelivery(
|
|
607
|
+
input: Parameters<NonNullable<WorkflowWorker["revalidateDelivery"]>>[0],
|
|
608
|
+
): ReturnType<NonNullable<WorkflowWorker["revalidateDelivery"]>> {
|
|
609
|
+
const resources = this.#resources({
|
|
610
|
+
runId: input.runId,
|
|
611
|
+
deliveryRevision: input.deliveryRevision,
|
|
612
|
+
plan: input.plan,
|
|
613
|
+
baselineRevisionId: input.baselineRevisionId,
|
|
614
|
+
currentWorkspaceRevisionId: input.currentWorkspaceRevisionId,
|
|
615
|
+
});
|
|
616
|
+
const expandedBaseline = this.#expandedBaseline(resources, input.plan);
|
|
617
|
+
const rows = new Map(input.tasks.map((task) => [task.taskId, task]));
|
|
618
|
+
const facts = new Map(
|
|
619
|
+
input.tasks.map((task) => [
|
|
620
|
+
task.taskId,
|
|
621
|
+
this.#phaseFacts(resources, task),
|
|
622
|
+
]),
|
|
623
|
+
);
|
|
624
|
+
const invalidated = new Set(input.invalidatedTaskIds);
|
|
625
|
+
const ordered = this.#orderedTasks(input.plan);
|
|
626
|
+
let rebuilt = expandedBaseline;
|
|
627
|
+
for (;;) {
|
|
628
|
+
let grew = false;
|
|
629
|
+
rebuilt = expandedBaseline;
|
|
630
|
+
for (const task of ordered) {
|
|
631
|
+
const row = rows.get(task.taskId);
|
|
632
|
+
if (!row || invalidated.has(task.taskId)) continue;
|
|
633
|
+
if (task.dependsOn.some((dependency) => invalidated.has(dependency))) {
|
|
634
|
+
invalidated.add(task.taskId);
|
|
635
|
+
grew = true;
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
for (const fact of facts.get(task.taskId) ?? []) {
|
|
639
|
+
const phase = task.phases[fact.phase];
|
|
640
|
+
if (!phase) {
|
|
641
|
+
invalidated.add(task.taskId);
|
|
642
|
+
grew = true;
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
const original = resources.workspaces.getRevision(
|
|
646
|
+
fact.isolatedRevisionId,
|
|
647
|
+
);
|
|
648
|
+
if (!original.parentRevisionId) {
|
|
649
|
+
throw new Error("workflow-ledger-revision-base-invalid");
|
|
650
|
+
}
|
|
651
|
+
const originalBase = resources.workspaces.getRevision(
|
|
652
|
+
original.parentRevisionId,
|
|
653
|
+
);
|
|
654
|
+
const current = resources.workspaces.getRevision(rebuilt.revisionId);
|
|
655
|
+
const boundaries =
|
|
656
|
+
fact.kind === "repair-verified"
|
|
657
|
+
? Object.values(task.phases)
|
|
658
|
+
: [phase];
|
|
659
|
+
const boundPaths = [
|
|
660
|
+
...new Set(
|
|
661
|
+
boundaries.flatMap((boundary) => [
|
|
662
|
+
...(row.contextReadPaths ?? []),
|
|
663
|
+
...boundary.read,
|
|
664
|
+
...boundary.write,
|
|
665
|
+
...boundary.delete,
|
|
666
|
+
...boundary.verificationInputs.flatMap((binding) =>
|
|
667
|
+
binding.kind === "workspace" ? [binding.path] : [],
|
|
668
|
+
),
|
|
669
|
+
]),
|
|
670
|
+
),
|
|
671
|
+
];
|
|
672
|
+
if (
|
|
673
|
+
boundPaths.some(
|
|
674
|
+
(relative) =>
|
|
675
|
+
!workspaceEntryEqual(
|
|
676
|
+
originalBase.entries[relative],
|
|
677
|
+
current.entries[relative],
|
|
678
|
+
),
|
|
679
|
+
)
|
|
680
|
+
) {
|
|
681
|
+
invalidated.add(task.taskId);
|
|
682
|
+
grew = true;
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
const changes: Record<
|
|
686
|
+
string,
|
|
687
|
+
| { kind: "absent" }
|
|
688
|
+
| { kind: "file"; bytes: Uint8Array; mode: number }
|
|
689
|
+
> = {};
|
|
690
|
+
for (const output of fact.outputFacts) {
|
|
691
|
+
const entry = original.entries[output.path];
|
|
692
|
+
if (
|
|
693
|
+
output.kind === "absent"
|
|
694
|
+
? entry?.kind !== "absent"
|
|
695
|
+
: entry?.kind !== "file" ||
|
|
696
|
+
entry.hash !== output.hash ||
|
|
697
|
+
entry.bytes !== output.bytes
|
|
698
|
+
) {
|
|
699
|
+
throw new Error("workflow-ledger-output-integrity-invalid");
|
|
700
|
+
}
|
|
701
|
+
changes[output.path] =
|
|
702
|
+
entry.kind === "absent"
|
|
703
|
+
? { kind: "absent" }
|
|
704
|
+
: {
|
|
705
|
+
kind: "file",
|
|
706
|
+
bytes: resources.artifacts.read(entry.hash),
|
|
707
|
+
mode: entry.mode,
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
rebuilt = resources.workspaces.createRevision({
|
|
711
|
+
parentRevisionId: rebuilt.revisionId,
|
|
712
|
+
changes,
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
for (const output of input.plan.outputs) {
|
|
717
|
+
if (invalidated.has(output.producer.taskId)) continue;
|
|
718
|
+
const produced = (facts.get(output.producer.taskId) ?? []).some(
|
|
719
|
+
(fact) => fact.phase === output.producer.phase,
|
|
720
|
+
);
|
|
721
|
+
if (
|
|
722
|
+
produced &&
|
|
723
|
+
rebuilt.entries[output.path]?.kind !== "file" &&
|
|
724
|
+
!invalidated.has(output.producer.taskId)
|
|
725
|
+
) {
|
|
726
|
+
invalidated.add(output.producer.taskId);
|
|
727
|
+
grew = true;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
if (!grew) break;
|
|
731
|
+
}
|
|
732
|
+
if (hasVerificationLifecycle(input.plan)) {
|
|
733
|
+
const retainedRevisions = new Set(
|
|
734
|
+
input.tasks
|
|
735
|
+
.filter((task) => !invalidated.has(task.taskId))
|
|
736
|
+
.map((task) => task.deliveryRevision),
|
|
737
|
+
);
|
|
738
|
+
for (const deliveryRevision of retainedRevisions) {
|
|
739
|
+
const ledger = this.#ledger(resources, deliveryRevision);
|
|
740
|
+
const stored = ledger.durableFact(
|
|
741
|
+
this.#verification.baselineFactKey(resources),
|
|
742
|
+
);
|
|
743
|
+
resources.baselinePromises.delete(deliveryRevision);
|
|
744
|
+
if (stored === undefined) continue;
|
|
745
|
+
const baseline = parseVerificationBaseline(stored, resources.artifacts);
|
|
746
|
+
if (baseline.revisionId === expandedBaseline.revisionId) continue;
|
|
747
|
+
if (baseline.revisionId !== input.baselineRevisionId) {
|
|
748
|
+
throw new Error("workflow-verification-baseline-conflict");
|
|
749
|
+
}
|
|
750
|
+
ledger.replaceDurableFact(
|
|
751
|
+
this.#verification.baselineFactKey(resources),
|
|
752
|
+
stored,
|
|
753
|
+
verificationBaselineFact(
|
|
754
|
+
{
|
|
755
|
+
...baseline,
|
|
756
|
+
revisionId: expandedBaseline.revisionId,
|
|
757
|
+
},
|
|
758
|
+
resources.artifacts,
|
|
759
|
+
),
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
resources.baselineRevisionId = expandedBaseline.revisionId;
|
|
764
|
+
resources.currentRevisionId = rebuilt.revisionId;
|
|
765
|
+
if (hasVerificationLifecycle(input.plan)) {
|
|
766
|
+
for (const task of input.tasks) {
|
|
767
|
+
if (task.state === "verified" && !invalidated.has(task.taskId)) {
|
|
768
|
+
this.#phase.completeTracking(resources, task.taskId);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return {
|
|
773
|
+
invalidatedTaskIds: [...invalidated].sort(),
|
|
774
|
+
baselineRevisionId: resources.baselineRevisionId,
|
|
775
|
+
currentWorkspaceRevisionId: resources.currentRevisionId,
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
isContextReadAvailable(
|
|
780
|
+
input: Parameters<NonNullable<WorkflowWorker["isContextReadAvailable"]>>[0],
|
|
781
|
+
): boolean {
|
|
782
|
+
const resources = this.#resources(input);
|
|
783
|
+
const revision = resources.workspaces.getRevision(
|
|
784
|
+
input.currentWorkspaceRevisionId,
|
|
785
|
+
);
|
|
786
|
+
return revision.entries[input.path]?.kind === "file";
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
rebind(input: Parameters<WorkflowWorker["rebind"]>[0]) {
|
|
790
|
+
const rebound = this.#broker.rebind({
|
|
791
|
+
runId: input.runId,
|
|
792
|
+
taskId: input.taskId,
|
|
793
|
+
role: input.role,
|
|
794
|
+
routeId: input.routeId,
|
|
795
|
+
requirements: implementationRouteRequirements({ task: input.task }),
|
|
796
|
+
});
|
|
797
|
+
return rebound.ok
|
|
798
|
+
? {
|
|
799
|
+
ok: true as const,
|
|
800
|
+
routeId: rebound.route.id,
|
|
801
|
+
routeFingerprint: rebound.route.fingerprint,
|
|
802
|
+
}
|
|
803
|
+
: { ok: false as const, code: rebound.code };
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
async prepare(
|
|
807
|
+
input: Parameters<WorkflowApplication["begin"]>[0],
|
|
808
|
+
): Promise<Record<string, unknown>> {
|
|
809
|
+
if (!input.baselineRevisionId || !input.currentWorkspaceRevisionId) {
|
|
810
|
+
return { state: "paused", code: "workspace-revision-unavailable" };
|
|
811
|
+
}
|
|
812
|
+
const resources = await this.#prepareResources(
|
|
813
|
+
{
|
|
814
|
+
runId: input.runId,
|
|
815
|
+
deliveryRevision: input.deliveryRevision,
|
|
816
|
+
plan: input.plan,
|
|
817
|
+
baselineRevisionId: input.baselineRevisionId,
|
|
818
|
+
currentWorkspaceRevisionId: input.currentWorkspaceRevisionId,
|
|
819
|
+
},
|
|
820
|
+
input.signal,
|
|
821
|
+
);
|
|
822
|
+
if (!resources.verificationFact) {
|
|
823
|
+
return { state: "paused", code: "verification-fact-unavailable" };
|
|
824
|
+
}
|
|
825
|
+
if (this.#options.verificationEnvironment) {
|
|
826
|
+
if (!resources.environmentIdentity)
|
|
827
|
+
return {
|
|
828
|
+
state: "paused",
|
|
829
|
+
code: "verification-environment-unavailable",
|
|
830
|
+
};
|
|
831
|
+
this.#ledger(resources, resources.deliveryRevision).putDurableFact(
|
|
832
|
+
`apply-environment-${hash(input.transactionId).slice(0, 40)}`,
|
|
833
|
+
{ identity: resources.environmentIdentity },
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
const prepared = await resources.transactions.prepare({
|
|
837
|
+
transactionId: input.transactionId,
|
|
838
|
+
consumerRoot: input.consumerRoot,
|
|
839
|
+
baselineRevisionId: resources.baselineRevisionId,
|
|
840
|
+
finalRevisionId: resources.currentRevisionId,
|
|
841
|
+
boundPaths: [
|
|
842
|
+
...new Set([
|
|
843
|
+
...deliveryBoundPaths(input.plan),
|
|
844
|
+
...(input.contextReadPaths ?? []),
|
|
845
|
+
]),
|
|
846
|
+
].sort(),
|
|
847
|
+
verificationFact: resources.verificationFact,
|
|
848
|
+
});
|
|
849
|
+
return prepared;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
async applyPrepared(
|
|
853
|
+
input: Parameters<WorkflowApplication["begin"]>[0],
|
|
854
|
+
): Promise<Record<string, unknown>> {
|
|
855
|
+
const resources = await this.#prepareResources(
|
|
856
|
+
{
|
|
857
|
+
runId: input.runId,
|
|
858
|
+
deliveryRevision: input.deliveryRevision,
|
|
859
|
+
plan: input.plan,
|
|
860
|
+
...(input.baselineRevisionId
|
|
861
|
+
? { baselineRevisionId: input.baselineRevisionId }
|
|
862
|
+
: {}),
|
|
863
|
+
...(input.currentWorkspaceRevisionId
|
|
864
|
+
? { currentWorkspaceRevisionId: input.currentWorkspaceRevisionId }
|
|
865
|
+
: {}),
|
|
866
|
+
},
|
|
867
|
+
input.signal,
|
|
868
|
+
);
|
|
869
|
+
return resources.transactions.apply(input.transactionId, input.signal);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
async begin(
|
|
873
|
+
input: Parameters<WorkflowApplication["begin"]>[0],
|
|
874
|
+
): Promise<Record<string, unknown>> {
|
|
875
|
+
const prepared = await this.prepare(input);
|
|
876
|
+
return prepared.state === "prepared" ? this.applyPrepared(input) : prepared;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
#resourcesForApplication(
|
|
880
|
+
context: WorkflowApplicationContext | undefined,
|
|
881
|
+
): DurableRunResources {
|
|
882
|
+
if (!context) throw new Error("workflow-application-context-unavailable");
|
|
883
|
+
return this.#resources({
|
|
884
|
+
runId: context.runId,
|
|
885
|
+
deliveryRevision: context.deliveryRevision,
|
|
886
|
+
plan: context.plan,
|
|
887
|
+
...(context.baselineRevisionId
|
|
888
|
+
? { baselineRevisionId: context.baselineRevisionId }
|
|
889
|
+
: {}),
|
|
890
|
+
...(context.currentWorkspaceRevisionId
|
|
891
|
+
? { currentWorkspaceRevisionId: context.currentWorkspaceRevisionId }
|
|
892
|
+
: {}),
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
requestControl(
|
|
897
|
+
transactionId: string,
|
|
898
|
+
intent: "cancel" | "discard",
|
|
899
|
+
context?: WorkflowApplicationContext,
|
|
900
|
+
): Record<string, unknown> {
|
|
901
|
+
return this.#resourcesForApplication(context).transactions.requestControl(
|
|
902
|
+
transactionId,
|
|
903
|
+
intent,
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
recover(
|
|
908
|
+
transactionId: string,
|
|
909
|
+
context?: WorkflowApplicationContext,
|
|
910
|
+
): Promise<Record<string, unknown>> {
|
|
911
|
+
return this.#resourcesForApplication(context).transactions.recover(
|
|
912
|
+
transactionId,
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
cleanup(runId: string): void {
|
|
917
|
+
const resources = this.#runs.get(runId);
|
|
918
|
+
if (resources) {
|
|
919
|
+
this.#closeResources(resources);
|
|
920
|
+
this.#runs.delete(runId);
|
|
921
|
+
}
|
|
922
|
+
rmSync(this.#runRoot(runId), { recursive: true, force: true });
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
close(): void {
|
|
926
|
+
for (const resources of this.#runs.values()) {
|
|
927
|
+
this.#closeResources(resources);
|
|
928
|
+
}
|
|
929
|
+
this.#runs.clear();
|
|
930
|
+
this.#routeHealth.close();
|
|
931
|
+
}
|
|
932
|
+
runAttempt(input: Parameters<WorkflowWorker["runAttempt"]>[0]) {
|
|
933
|
+
return this.#phase.runAttempt(input);
|
|
934
|
+
}
|
|
935
|
+
hasPendingVerification(input: Parameters<WorkflowWorker["runAttempt"]>[0]) {
|
|
936
|
+
return this.#phase.hasPendingVerification(input);
|
|
937
|
+
}
|
|
938
|
+
verify(input: Parameters<WorkflowChangeVerifier["verify"]>[0]) {
|
|
939
|
+
return this.#verification.verify(input);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
export function openDurableWorkflowEngine(
|
|
944
|
+
options: DurableWorkflowEngineOptions,
|
|
945
|
+
): WorkflowEngine {
|
|
946
|
+
const composition = new DurableWorkflowComposition(options);
|
|
947
|
+
return WorkflowEngine.open({
|
|
948
|
+
consumerRoot: options.consumerRoot,
|
|
949
|
+
stateRoot: options.stateRoot,
|
|
950
|
+
deliverySource: options.deliverySource,
|
|
951
|
+
worker: composition,
|
|
952
|
+
...(options.workHardLimit !== undefined
|
|
953
|
+
? { workHardLimit: options.workHardLimit }
|
|
954
|
+
: {}),
|
|
955
|
+
changeVerifier: composition,
|
|
956
|
+
application: composition,
|
|
957
|
+
lifecycle: composition,
|
|
958
|
+
...(options.now ? { now: options.now } : {}),
|
|
959
|
+
...(options.leaseTtlMs !== undefined
|
|
960
|
+
? { leaseTtlMs: options.leaseTtlMs }
|
|
961
|
+
: {}),
|
|
962
|
+
});
|
|
963
|
+
}
|