@anureo/workflow 0.3.4 → 0.3.8
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 +102 -82
- package/package.json +9 -8
- package/src/agent-adapters.test.ts +121 -0
- package/src/agent-execution.ts +228 -0
- package/src/agent.test.ts +237 -51
- package/src/agent.ts +61 -92
- package/src/fixtures/workflow-crash.ts +54 -17
- package/src/index.ts +6 -2
- package/src/journal-call.test.ts +2 -10
- package/src/journal-call.ts +44 -32
- package/src/output-schema.test.ts +45 -12
- package/src/output-schema.ts +208 -96
- package/src/parallel.test.ts +1 -0
- package/src/parallel.ts +26 -8
- package/src/persistence/checkpoint-files.ts +83 -0
- package/src/persistence/journal.ts +98 -82
- package/src/persistence/json.ts +58 -32
- package/src/persistence/records.ts +82 -56
- package/src/persistence/replay.ts +74 -30
- package/src/persistence/store.ts +260 -120
- package/src/pipeline.test.ts +19 -0
- package/src/pipeline.ts +69 -23
- package/src/test-helpers.ts +24 -0
- package/src/workflow-state.ts +72 -18
- package/src/workflow-types.ts +20 -2
- package/src/workflow.test.ts +204 -97
- package/src/workflow.ts +88 -30
- package/src/agent-outcome.ts +0 -21
- package/src/agent-pi.test.ts +0 -21
- package/src/native.test.ts +0 -71
- package/src/persistence/atomic.ts +0 -32
- package/src/run-context.ts +0 -19
package/src/workflow-state.ts
CHANGED
|
@@ -1,56 +1,92 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
1
2
|
import { HarnessSdkError, sdkError } from '@anureo/sdk'
|
|
2
|
-
import type {
|
|
3
|
+
import type { AgentJournal } from './persistence/journal'
|
|
4
|
+
import type { StoredFailure } from './persistence/records'
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/** Internal run context installed for the workflow body and inherited by agent calls. */
|
|
7
|
+
export interface RunContext {
|
|
8
|
+
/** Source replay and target journal used by memoizedAgent. */
|
|
9
|
+
readonly journal: AgentJournal
|
|
10
|
+
/** Admission, pending work and failure state shared with workflow. */
|
|
11
|
+
readonly state: RunState
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Invoke completeRun's callback with context inherited by asynchronous descendants. The caller
|
|
15
|
+
* receives the callback promise or a synchronous throw and owns closing admission and draining work. */
|
|
16
|
+
export function runWith<T>(context: RunContext, body: () => Promise<T>): Promise<T> {
|
|
17
|
+
return storage.run(context, body)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Read the current async context so agent can choose journaled or ordinary execution.
|
|
21
|
+
* Async descendants can retain a closed context. RunState.track then rejects admission. */
|
|
22
|
+
export function currentRun(): RunContext | undefined {
|
|
23
|
+
return storage.getStore()
|
|
8
24
|
}
|
|
9
25
|
|
|
26
|
+
/** Project abortRun's failure into manifest metadata, excluding diagnostic messages and details. */
|
|
10
27
|
export function storedFailure(error: HarnessSdkError): StoredFailure {
|
|
11
|
-
return {
|
|
12
|
-
|
|
28
|
+
return {
|
|
29
|
+
code: error.code,
|
|
30
|
+
retryable: error.retryable,
|
|
31
|
+
outcomeUnknown: error.outcomeUnknown,
|
|
32
|
+
cleanupFailed: cleanupFailed(error),
|
|
33
|
+
}
|
|
13
34
|
}
|
|
14
35
|
|
|
36
|
+
/** Tell memoizedAgent and RunState.track when a failure must stop later dispatch even if caught.
|
|
37
|
+
* Uncertain execution, failed cleanup and broken persistence cannot safely continue the run. */
|
|
15
38
|
export function isFatal(error: HarnessSdkError): boolean {
|
|
16
39
|
return error.outcomeUnknown || cleanupFailed(error)
|
|
17
40
|
|| error.code === 'WORKFLOW_STORAGE_FAILED'
|
|
18
41
|
|| error.code === 'WORKFLOW_CHECKPOINT_INVALID'
|
|
19
42
|
}
|
|
20
43
|
|
|
44
|
+
/** Internal admission and settlement state shared by workflow and its journaled agent calls.
|
|
45
|
+
* Ordinary step failures mark completion incomplete; sticky failures reject the run and block dispatch.
|
|
46
|
+
* workflow's completion and abort phases close admission and drain registered work. */
|
|
21
47
|
export class RunState {
|
|
22
|
-
|
|
23
|
-
private pending = new Set<Promise<void>>()
|
|
24
|
-
private stops = new Set<HarnessSdkError>()
|
|
25
|
-
private rejected = 0
|
|
26
|
-
|
|
48
|
+
/** Preserve a failure for workflow's rejection and prevent later dispatch. Active work keeps settling. */
|
|
27
49
|
stop(error: HarnessSdkError): void { this.stops.add(error) }
|
|
28
50
|
|
|
51
|
+
/** Record a normalized storage failure when abortRun cannot publish or workflow cannot release the handle. */
|
|
52
|
+
noteStorageFailure(): void { this.stop(new HarnessSdkError('WORKFLOW_STORAGE_FAILED')) }
|
|
53
|
+
|
|
54
|
+
/** Return the first sticky code and message with all recorded codes, uncertainty and cleanup evidence.
|
|
55
|
+
* abortRun snapshots this for failure publication. workflow reads it again after lock release. */
|
|
29
56
|
get failure(): HarnessSdkError | undefined {
|
|
30
57
|
const failures = [...this.stops]
|
|
31
58
|
const first = failures[0]
|
|
32
59
|
if (!first) return undefined
|
|
33
60
|
return new HarnessSdkError(first.code, first.message, false,
|
|
34
61
|
failures.some(error => error.outcomeUnknown), {
|
|
62
|
+
...first.details,
|
|
35
63
|
failureCodes: [...new Set(failures.map(error => error.code))],
|
|
36
64
|
cleanupFailed: failures.some(cleanupFailed),
|
|
37
65
|
})
|
|
38
66
|
}
|
|
39
67
|
|
|
68
|
+
/** Publication status for workflow when no sticky failure remains to reject the run. */
|
|
40
69
|
get status(): 'completed' | 'incomplete' {
|
|
41
|
-
return this.
|
|
70
|
+
return this.incomplete ? 'incomplete' : 'completed'
|
|
42
71
|
}
|
|
43
72
|
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
noteIncomplete(): void { this.
|
|
73
|
+
/** Mark a drop from executeJournaledAgent or a rejection observed by track incomplete.
|
|
74
|
+
* Fatality is checked separately. */
|
|
75
|
+
noteIncomplete(): void { this.incomplete = true }
|
|
47
76
|
|
|
77
|
+
/** Throw the sticky aggregate. track checks this before starting registered work, and
|
|
78
|
+
* executeJournaledAgent checks again after publishing started because another call may have
|
|
79
|
+
* stopped dispatch during that await. */
|
|
48
80
|
assertDispatch(): void {
|
|
49
81
|
const failure = this.failure
|
|
50
82
|
if (failure) throw failure
|
|
51
83
|
}
|
|
52
84
|
|
|
53
|
-
/**
|
|
85
|
+
/** Invoke memoizedAgent's operation immediately and register it for workflow's drain.
|
|
86
|
+
* Closed admission returns a WORKFLOW_CLOSED rejection without invoking the operation.
|
|
87
|
+
* Sticky failures and synchronous operation throws become rejected promises. Return the work
|
|
88
|
+
* promise and register a separate observer so catching its rejection cannot hide incomplete
|
|
89
|
+
* work or fatal failures from workflow. */
|
|
54
90
|
track<T>(operation: () => Promise<T>): Promise<T> {
|
|
55
91
|
if (!this.accepting) return Promise.reject(new HarnessSdkError('WORKFLOW_CLOSED'))
|
|
56
92
|
let work: Promise<T>
|
|
@@ -61,7 +97,7 @@ export class RunState {
|
|
|
61
97
|
work = Promise.reject(cause)
|
|
62
98
|
}
|
|
63
99
|
const observed = work.then(() => {}, cause => {
|
|
64
|
-
this.
|
|
100
|
+
this.noteIncomplete()
|
|
65
101
|
const error = sdkError(cause, 'WORKFLOW_STEP_FAILED')
|
|
66
102
|
if (isFatal(error)) this.stop(error)
|
|
67
103
|
})
|
|
@@ -70,8 +106,26 @@ export class RunState {
|
|
|
70
106
|
return work
|
|
71
107
|
}
|
|
72
108
|
|
|
109
|
+
/** Stop admitting agent work synchronously, then await all work already registered by track.
|
|
110
|
+
* completeRun calls this before publication, and abortRun uses it on failure. Work continues
|
|
111
|
+
* settling without cancellation. The observers record operation rejections in run state. */
|
|
73
112
|
async closeAndDrain(): Promise<void> {
|
|
74
113
|
this.accepting = false
|
|
75
114
|
await Promise.all([...this.pending])
|
|
76
115
|
}
|
|
116
|
+
|
|
117
|
+
private accepting = true
|
|
118
|
+
private pending = new Set<Promise<void>>()
|
|
119
|
+
private stops = new Set<HarnessSdkError>()
|
|
120
|
+
private incomplete = false
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const storage = new AsyncLocalStorage<RunContext>()
|
|
124
|
+
|
|
125
|
+
/** Recognize SDK resource-release errors and explicit cleanup evidence on another failure.
|
|
126
|
+
* isFatal, storedFailure and RunState.failure share this rule for dispatch and reporting. */
|
|
127
|
+
function cleanupFailed(error: HarnessSdkError): boolean {
|
|
128
|
+
return error.code === 'RESOURCE_RELEASE_FAILED'
|
|
129
|
+
|| error.details?.cleanupFailed === true
|
|
130
|
+
|| (Array.isArray(error.details?.cleanupErrors) && error.details.cleanupErrors.length > 0)
|
|
77
131
|
}
|
package/src/workflow-types.ts
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
|
+
/** Identify a new durable run and optionally supply a compatible source journal for replay. */
|
|
1
2
|
export interface WorkflowOptions<Input> {
|
|
3
|
+
/** Nonempty workflow name. Resume requires the same name in the source manifest. */
|
|
2
4
|
name: string
|
|
5
|
+
/** Nonempty application-managed revision. A changed revision prevents resuming older runs. */
|
|
3
6
|
revision: string
|
|
4
|
-
/**
|
|
5
|
-
directory
|
|
7
|
+
/** Nonempty storage root. Relative paths are resolved against the process working directory. */
|
|
8
|
+
directory: string
|
|
9
|
+
/** New target directory name, defaulting to a random UUID. Use a nonblank single path
|
|
10
|
+
* component of at most 255 UTF-8 bytes. Control characters, separators, `.` and `..` are
|
|
11
|
+
* invalid. A locked or nonempty target directory rejects. */
|
|
6
12
|
runId?: string
|
|
13
|
+
/** Strict JSON input. The body receives a deeply frozen copy. */
|
|
7
14
|
input: Input
|
|
15
|
+
/** Omit for a fresh journal. A string selects a run under `directory`. An object supplies the
|
|
16
|
+
* source's storage root and ID explicitly. The source must be inactive, compatible by name and
|
|
17
|
+
* revision, and separate from the target tree. The new run copies its journal before invoking the body. */
|
|
8
18
|
resumeFrom?: string | { directory: string; runId: string }
|
|
9
19
|
}
|
|
10
20
|
|
|
21
|
+
/** Body inputs available after the initial manifest has been published. */
|
|
11
22
|
export interface WorkflowContext<Input> {
|
|
23
|
+
/** Identity of the new run, including when replaying a source journal. */
|
|
12
24
|
readonly runId: string
|
|
25
|
+
/** Isolated, deeply frozen JSON copy of the input supplied for this invocation. */
|
|
13
26
|
readonly input: Input
|
|
14
27
|
}
|
|
15
28
|
|
|
29
|
+
/** Published body result returned after registered agent work settles and the writer lock is released. */
|
|
16
30
|
export interface WorkflowResult<Result> {
|
|
31
|
+
/** Identity of the run that published this result. */
|
|
17
32
|
runId: string
|
|
33
|
+
/** Incomplete if registered agent work rejected or was dropped and the workflow could still
|
|
34
|
+
* publish a result. Fatal agent failures reject the workflow promise. */
|
|
18
35
|
status: 'completed' | 'incomplete'
|
|
36
|
+
/** JSON copy of the awaited body return value. */
|
|
19
37
|
result: Result
|
|
20
38
|
}
|