@anureo/workflow 0.0.0-stage → 0.3.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/LICENSE +201 -0
- package/README.md +172 -2
- package/package.json +36 -4
- package/src/agent-outcome.d.ts +15 -0
- package/src/agent-outcome.js +28 -0
- package/src/agent.d.ts +49 -0
- package/src/agent.js +206 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +5 -0
- package/src/output-schema.d.ts +27 -0
- package/src/output-schema.js +192 -0
- package/src/parallel.d.ts +18 -0
- package/src/parallel.js +25 -0
- package/src/persistence/atomic.d.ts +9 -0
- package/src/persistence/atomic.js +48 -0
- package/src/persistence/journal.d.ts +54 -0
- package/src/persistence/journal.js +106 -0
- package/src/persistence/json.d.ts +17 -0
- package/src/persistence/json.js +75 -0
- package/src/persistence/read-json.d.ts +11 -0
- package/src/persistence/read-json.js +46 -0
- package/src/persistence/records.d.ts +92 -0
- package/src/persistence/records.js +70 -0
- package/src/persistence/replay.d.ts +11 -0
- package/src/persistence/replay.js +79 -0
- package/src/persistence/store.d.ts +45 -0
- package/src/persistence/store.js +238 -0
- package/src/pipeline.d.ts +41 -0
- package/src/pipeline.js +24 -0
- package/src/run-context.d.ts +16 -0
- package/src/run-context.js +13 -0
- package/src/test-helpers.d.ts +11 -0
- package/src/test-helpers.js +21 -0
- package/src/workflow-state.d.ts +44 -0
- package/src/workflow-state.js +96 -0
- package/src/workflow-types.d.ts +39 -0
- package/src/workflow-types.js +0 -0
- package/src/workflow.d.ts +17 -0
- package/src/workflow.js +104 -0
package/src/workflow.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { HarnessSdkError, sdkError } from '@anureo/sdk';
|
|
3
|
+
import { RunState, storedFailure } from "./workflow-state.js";
|
|
4
|
+
import { runWith } from "./run-context.js";
|
|
5
|
+
import { freezeJson, jsonCopy } from "./persistence/json.js";
|
|
6
|
+
import { openFileRun } from "./persistence/store.js";
|
|
7
|
+
/**
|
|
8
|
+
* Run a body with a durable identity, frozen JSON input and journaled agent calls.
|
|
9
|
+
* After saving the initial manifest, invoke the body with the new run's ID and input.
|
|
10
|
+
* On resume, the body runs from the beginning and eligible agent calls reuse source journal results.
|
|
11
|
+
* The body owns any side effects outside those reused calls and must account for re-execution.
|
|
12
|
+
* Supply strict JSON for the input and the awaited body result, and await any background work
|
|
13
|
+
* other than registered agent calls inside the body. Keep borrowed clients alive until the
|
|
14
|
+
* workflow promise settles and dispose them separately.
|
|
15
|
+
*
|
|
16
|
+
* Resolve with a JSON copy of the body result after registered agent work settles, publication
|
|
17
|
+
* succeeds and the writer lock is released. Drops and recovered ordinary agent failures can
|
|
18
|
+
* yield an incomplete result. Invalid inputs, body errors, fatal agent failures, and storage
|
|
19
|
+
* failures reject. Run failures report the run ID and storage root in SDK error details. A
|
|
20
|
+
* lock-release failure can reject after the result and terminal manifest have been published.
|
|
21
|
+
*/
|
|
22
|
+
export async function workflow(options, body) {
|
|
23
|
+
const runId = options?.runId ?? randomUUID();
|
|
24
|
+
const state = new RunState();
|
|
25
|
+
let handle;
|
|
26
|
+
let publishedResult;
|
|
27
|
+
try {
|
|
28
|
+
const { storageOptions, context } = prepareRun(options, body, runId);
|
|
29
|
+
handle = await openFileRun(storageOptions);
|
|
30
|
+
publishedResult = await completeRun({ handle, state, body, context });
|
|
31
|
+
}
|
|
32
|
+
catch (cause) {
|
|
33
|
+
await abortRun({ cause, state, handle });
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
if (handle) {
|
|
37
|
+
try {
|
|
38
|
+
await handle.close();
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
state.noteStorageFailure();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const failure = state.failure;
|
|
46
|
+
if (failure) {
|
|
47
|
+
throw new HarnessSdkError(failure.code, failure.message, false, failure.outcomeUnknown, {
|
|
48
|
+
...failure.details,
|
|
49
|
+
runId,
|
|
50
|
+
directory: handle?.identity.directory ?? options?.directory,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
// A failed publication also stops the run, so only published results reach this return.
|
|
54
|
+
return { runId, status: state.status, result: publishedResult };
|
|
55
|
+
}
|
|
56
|
+
/** Prepare workflow's storage options and body context from the same frozen input snapshot.
|
|
57
|
+
* Missing or non-object options, a non-function body or non-JSON input throw before storage opens. */
|
|
58
|
+
function prepareRun(options, body, runId) {
|
|
59
|
+
if (!options || typeof options !== 'object' || typeof body !== 'function') {
|
|
60
|
+
throw new HarnessSdkError('INVALID_PARAMS');
|
|
61
|
+
}
|
|
62
|
+
const input = freezeJson(jsonCopy(options.input));
|
|
63
|
+
const resumeFrom = options.resumeFrom && typeof options.resumeFrom === 'object'
|
|
64
|
+
? { ...options.resumeFrom } : options.resumeFrom;
|
|
65
|
+
return {
|
|
66
|
+
storageOptions: { ...options, runId, input: input, resumeFrom },
|
|
67
|
+
context: { runId, input },
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** Run workflow's body in the journal context and stop dispatch if it throws or rejects.
|
|
71
|
+
* Drain before checking sticky failure so agent settlement can still change the outcome.
|
|
72
|
+
* Return the JSON copy after publication. A rejection sends workflow through abortRun before release. */
|
|
73
|
+
async function completeRun({ handle, state, body, context }) {
|
|
74
|
+
let value;
|
|
75
|
+
try {
|
|
76
|
+
value = await runWith({ journal: handle.journal, state }, async () => body(context));
|
|
77
|
+
}
|
|
78
|
+
catch (cause) {
|
|
79
|
+
state.stop(sdkError(cause, 'WORKFLOW_BODY_FAILED'));
|
|
80
|
+
}
|
|
81
|
+
await state.closeAndDrain();
|
|
82
|
+
const failure = state.failure;
|
|
83
|
+
if (failure)
|
|
84
|
+
throw failure;
|
|
85
|
+
const result = jsonCopy(value);
|
|
86
|
+
await handle.finish(state.status, result);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/** Record workflow's failure and drain before publishing the aggregate to an acquired handle.
|
|
90
|
+
* Initialization failures can arrive before a handle exists. An abort publication error is
|
|
91
|
+
* recorded as a storage failure. workflow still releases the handle and reports the final aggregate. */
|
|
92
|
+
async function abortRun({ cause, state, handle }) {
|
|
93
|
+
const error = sdkError(cause, 'WORKFLOW_FAILED');
|
|
94
|
+
state.stop(error);
|
|
95
|
+
await state.closeAndDrain();
|
|
96
|
+
if (handle) {
|
|
97
|
+
try {
|
|
98
|
+
await handle.abort(storedFailure(state.failure ?? error));
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
state.noteStorageFailure();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|