@anureo/workflow 0.3.2 → 0.3.4
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 +94 -96
- package/package.json +4 -5
- package/src/agent-mcp.test.ts +19 -0
- package/src/agent-outcome.ts +21 -0
- package/src/agent-pi.test.ts +21 -0
- package/src/agent.test.ts +51 -237
- package/src/agent.ts +90 -115
- package/src/fixtures/workflow-crash.ts +17 -54
- package/src/index.ts +5 -6
- package/src/journal-call.test.ts +68 -0
- package/src/journal-call.ts +93 -0
- package/src/native.test.ts +71 -0
- package/src/output-schema.test.ts +12 -45
- package/src/output-schema.ts +96 -208
- package/src/parallel.test.ts +0 -1
- package/src/parallel.ts +8 -26
- package/src/persistence/atomic.ts +32 -0
- package/src/persistence/journal.ts +92 -104
- package/src/persistence/json.ts +32 -58
- package/src/persistence/records.ts +56 -82
- package/src/persistence/replay.ts +30 -74
- package/src/persistence/store.ts +120 -259
- package/src/pipeline.test.ts +0 -19
- package/src/pipeline.ts +23 -69
- package/src/run-context.ts +19 -0
- package/src/workflow-state.ts +18 -72
- package/src/workflow-types.ts +2 -20
- package/src/workflow.test.ts +104 -199
- package/src/workflow.ts +30 -88
- package/src/agent-adapters.test.ts +0 -121
- package/src/agent-execution.ts +0 -220
- package/src/persistence/checkpoint-files.ts +0 -83
- package/src/test-helpers.ts +0 -23
package/README.md
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
# @anureo/workflow
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
`agent`
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
Directly imported primitives for ordinary TypeScript workflows:
|
|
4
|
+
`agent`, `parallel`, `pipeline`, and optional `workflow`. The host creates SDK Clients, imports its
|
|
5
|
+
workflow functions, awaits their results, and handles errors with normal TS.
|
|
6
|
+
|
|
7
|
+
See the [workflow authoring reference](../../docs/workflow/SKILL.md) for current
|
|
8
|
+
APIs, composition patterns, failure handling, and application-owned recovery.
|
|
9
|
+
|
|
10
|
+
The [Workflow primitives reference](../../docs/workflow/README.md) defines
|
|
11
|
+
parameters and result semantics for `agent`, `parallel`, and `pipeline`,
|
|
12
|
+
and records current support.
|
|
13
|
+
|
|
14
|
+
See the [composition comparison](../../draft/docs/workflow-composition-differences.md)
|
|
15
|
+
and [alignment plan](../../draft/docs/workflow-composition-plan.md) for Claude Code
|
|
16
|
+
semantics, the implemented changes, and retained host differences (Chinese).
|
|
17
|
+
|
|
18
|
+
This workspace package uses the revised native lifecycle API and is pending
|
|
19
|
+
publication with that revision. Requires Node.js 22+; TypeScript loading and
|
|
20
|
+
compilation are provided by your project (for example, Bun, tsx, or tsc).
|
|
8
21
|
|
|
9
22
|
## Usage
|
|
10
23
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
the Client. See the [SDK README](../../README.md) for Client setup.
|
|
24
|
+
Create an initialized Client using `createClient` and your chosen adapter as
|
|
25
|
+
described in the [SDK README](../../README.md). A workflow is just a function:
|
|
14
26
|
|
|
15
27
|
```ts
|
|
16
28
|
import type { Client } from '@anureo/sdk'
|
|
@@ -33,43 +45,39 @@ export async function reviewFiles(client: Client, files: readonly string[], cwd:
|
|
|
33
45
|
}
|
|
34
46
|
```
|
|
35
47
|
|
|
36
|
-
`pipeline` and `parallel`
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
In this example, `pipeline` and `parallel` convert internal `agent` rejections to
|
|
49
|
+
`null`; `pipeline` also skips the affected item's remaining stages. The two
|
|
50
|
+
explicit checks then throw summary errors for `null` results, so those errors do
|
|
51
|
+
not preserve the original agent failure classification.
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
|
|
53
|
+
Import `reviewFiles` from another TS file and call it with a Client. Calls do not
|
|
54
|
+
require workflow registration, source-string execution, ambient context, or runtime setup.
|
|
55
|
+
Use [durable workflow steps](../../docs/workflow/persistence.md) when a function
|
|
56
|
+
needs a persisted run ID and recoverable results.
|
|
43
57
|
|
|
44
58
|
## agent
|
|
45
59
|
|
|
46
60
|
```ts
|
|
47
|
-
import type { Client } from '@anureo/sdk'
|
|
48
61
|
import { z } from 'zod'
|
|
49
62
|
import { agent } from '@anureo/workflow'
|
|
50
63
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
64
|
+
const options = { client, cwd: process.cwd() }
|
|
65
|
+
const text: string | null = await agent('Inspect the authentication module.', options)
|
|
66
|
+
const findings = await agent('List authentication issues.', {
|
|
67
|
+
...options,
|
|
68
|
+
schema: z.strictObject({ issues: z.array(z.string()) }),
|
|
69
|
+
})
|
|
70
|
+
// findings: { issues: string[] } | null
|
|
71
|
+
if (findings !== null) console.log(findings.issues)
|
|
59
72
|
```
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
`client` and a `cwd`. You can also supply `model`, `instructions`, and `permissionMode`
|
|
65
|
-
for session creation. The caller owns the Client.
|
|
66
|
-
Omit `schema` to receive the final text, which can be empty, or `null`.
|
|
74
|
+
Each call owns a fresh Session, submits once, waits for its native terminal
|
|
75
|
+
result, and releases its resources. `client` and `cwd` are required; `model` and
|
|
76
|
+
`instructions` are optional. The caller owns the borrowed Client.
|
|
67
77
|
|
|
68
78
|
`schema` accepts a Zod 4 object schema. The library converts it to draft-07 JSON
|
|
69
|
-
Schema,
|
|
70
|
-
and returns `z.output<S>`.
|
|
71
|
-
`turn.outputSchema` support. Set `outputFallback: 'json-text'` to include the schema
|
|
72
|
-
in the prompt when the session lacks that capability.
|
|
79
|
+
Schema, requires effective session `turn.outputSchema` support, validates the raw JSON
|
|
80
|
+
result, then validates it with the original Zod schema and returns `z.output<S>`.
|
|
73
81
|
This package depends on Zod 4.6.7. Applications that import Zod to author schemas
|
|
74
82
|
should also declare it as their own dependency. Schema-associated types infer business fields;
|
|
75
83
|
there is no unbound result-type generic or raw JSON Schema overload.
|
|
@@ -81,11 +89,11 @@ Defaults do not fill omitted model answers. Async refinements are supported
|
|
|
81
89
|
locally; their failures reject without replaying input. See the
|
|
82
90
|
[full schema and failure contract](../../docs/workflow/README.md#schema-mode).
|
|
83
91
|
|
|
84
|
-
Confirmed native failed terminals for authentication, usage
|
|
85
|
-
service unavailability
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
Confirmed native failed terminals for authentication, usage/rate limits, and
|
|
93
|
+
service unavailability return `null`. Other failures and ordinary interruptions
|
|
94
|
+
reject. Trusted adapters may explicitly report user skip or classifier startup
|
|
95
|
+
denial; bundled adapters do not infer these reasons. Unknown execution outcomes
|
|
96
|
+
retain `outcomeUnknown: true` and always reject. No input is automatically retried.
|
|
89
97
|
|
|
90
98
|
Cleanup is attempted for successful, failed and discarded executions. A cleanup
|
|
91
99
|
failure rejects with `RESOURCE_RELEASE_FAILED`, or preserves a primary execution
|
|
@@ -103,12 +111,12 @@ CLIs and real model requests.
|
|
|
103
111
|
input-ordered results. Callbacks may return ordinary values or Promises; inline
|
|
104
112
|
heterogeneous tuples preserve each result's type. An empty batch returns `[]`.
|
|
105
113
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
The array and every entry are validated before any callback starts. The call
|
|
115
|
+
waits for all callbacks to settle and uses `null` at each failed position,
|
|
116
|
+
including synchronous throws. Result types include `null` at every position.
|
|
117
|
+
Invalid arguments still reject the call. Use `value !== null` to filter failures
|
|
118
|
+
without dropping valid `0`, `false`, or empty-string results.
|
|
119
|
+
There is no implicit interruption, shared scheduler, or concurrency limit.
|
|
112
120
|
|
|
113
121
|
## pipeline
|
|
114
122
|
|
|
@@ -129,58 +137,47 @@ stage inputs are compile-time errors. A `null` input or stage result skips that
|
|
|
129
137
|
item's remaining stages. An item failure also skips its remaining stages and
|
|
130
138
|
becomes `null`. The returned Promise waits for all item chains to settle; its
|
|
131
139
|
result type always includes `null`. Stage input types exclude only `null`, so
|
|
132
|
-
`undefined` and other falsy values continue normally.
|
|
133
|
-
|
|
134
|
-
concurrency limits, cancellation, and resources used by input work or stages.
|
|
140
|
+
`undefined` and other falsy values continue normally. Invalid composition
|
|
141
|
+
arguments still reject the call.
|
|
135
142
|
|
|
136
143
|
## Durable workflow
|
|
137
144
|
|
|
138
|
-
`workflow(options, body)`
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
##
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
-
|
|
174
|
-
advances each input through its own typed stage chain.
|
|
175
|
-
- [Workflow](src/workflow.ts) opens the run, invokes the body, drains registered agent work,
|
|
176
|
-
and publishes the result or failure before releasing the run. [Workflow types](src/workflow-types.ts)
|
|
177
|
-
define the application contract. [Run state](src/workflow-state.ts) gives nested agent calls
|
|
178
|
-
access to the active run and tracks admission, pending work and sticky failures.
|
|
179
|
-
- [Persistence](src/persistence/) holds the [target writer](src/persistence/store.ts), the
|
|
180
|
-
[source reader](src/persistence/replay.ts), and the [journal](src/persistence/journal.ts).
|
|
181
|
-
[Checkpoint records](src/persistence/records.ts) define the shared file layout and schemas.
|
|
182
|
-
[Checkpoint file I/O](src/persistence/checkpoint-files.ts) owns atomic publication and
|
|
183
|
-
checked reads. [Canonical JSON](src/persistence/json.ts) supplies copying and hashing rules.
|
|
145
|
+
`workflow(options, body)` saves an initial manifest before passing explicit
|
|
146
|
+
`runId` and `input` to the body. Every `agent()` call made inside the body is
|
|
147
|
+
journaled automatically: identical calls on a `resumeFrom` run return their
|
|
148
|
+
saved results without opening new sessions, and any changed call invalidates
|
|
149
|
+
the calls after it. Existing three-primitive functions keep their independent
|
|
150
|
+
behavior, and `agent()` outside `workflow()` is unchanged. Omitting `directory`
|
|
151
|
+
starts a memory-only run: it journals for its own lifetime (memoization and
|
|
152
|
+
chain keys) but writes no files and cannot be resumed after exit; resuming a
|
|
153
|
+
memory run fails with `WORKFLOW_RESUME_NOT_FOUND`.
|
|
154
|
+
|
|
155
|
+
Live calls use the client's [input and native hooks](../core/README.md#input-hooks).
|
|
156
|
+
The journal records the client's `inputHookRevision` with each started call.
|
|
157
|
+
Resume can reuse a matching result under the same policy revision without
|
|
158
|
+
running callbacks. A different revision, including adding or removing the
|
|
159
|
+
policy, stops resume for that call with `WORKFLOW_INPUT_POLICY_CHANGED`.
|
|
160
|
+
Start a new run after confirming the previous execution's outcome. For a
|
|
161
|
+
matching call whose previous outcome is already known to be uncertain,
|
|
162
|
+
`WORKFLOW_RECONCILIATION_REQUIRED` takes precedence.
|
|
163
|
+
|
|
164
|
+
The [persistence reference](../../docs/workflow/persistence.md) defines the
|
|
165
|
+
journal contract, cache keys, error handling and file guarantees.
|
|
166
|
+
Try `bun run example:workflow-persistence` and then repeat with its printed run
|
|
167
|
+
ID: the resumed run opens zero new sessions. This example uses a scripted
|
|
168
|
+
backend and makes no model requests.
|
|
169
|
+
|
|
170
|
+
## Adapter-facing helpers
|
|
171
|
+
|
|
172
|
+
Root exports also include three functions for adapter authors composing their
|
|
173
|
+
own journaled step primitives (for example `beer()` in
|
|
174
|
+
[`@anureo/adapter-swarm`](../adapter/swarm/README.md#workflow-execution)).
|
|
175
|
+
`journaledCall({ prompt, canonical, inputHookRevision?, droppedCode?,
|
|
176
|
+
failureCode? }, execute, revive?)` runs one memoized step with exactly the
|
|
177
|
+
`agent()` journal semantics: it executes unbundled outside a `workflow()` body.
|
|
178
|
+
`prepareOutputSchema(schema)` turns a zod object into the canonical JSON schema
|
|
179
|
+
plus `parse` used by structured-output steps, and `isDroppedOutcome(error,
|
|
180
|
+
status)` detects drop-class turn failures that map to a journaled null result.
|
|
184
181
|
|
|
185
182
|
## Examples
|
|
186
183
|
|
|
@@ -194,6 +191,7 @@ For `[2, 3, 4]`, the pipeline asks the model to double then square each value, e
|
|
|
194
191
|
`[16, 36, 64]`. Two parallel agent calls then calculate the sum `116` and maximum `64`.
|
|
195
192
|
The eight agent calls each use an independent Session.
|
|
196
193
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
194
|
+
The [migration plan](../../draft/docs/workflow-primitives-plan.md) records the removed
|
|
195
|
+
framework and the [development record](../../draft/docs/development.md#workflow-agent-zod)
|
|
196
|
+
contains current verification evidence. The package has only its root entry;
|
|
197
|
+
there is no `/mcp` subentry.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anureo/workflow",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Agent execution, composition, and durable workflow steps over the Anureo SDK",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"url": "git+https://github.com/hi-youichi/anureo-sdk.git",
|
|
9
9
|
"directory": "packages/workflow"
|
|
10
10
|
},
|
|
11
|
-
"homepage": "https://github.com/hi-youichi/anureo-sdk/tree/main/packages/workflow#readme",
|
|
12
11
|
"publishConfig": {
|
|
13
12
|
"access": "public"
|
|
14
13
|
},
|
|
@@ -22,15 +21,15 @@
|
|
|
22
21
|
"test": "bun test src"
|
|
23
22
|
},
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"@anureo/sdk": "0.3.
|
|
24
|
+
"@anureo/sdk": "0.3.3",
|
|
26
25
|
"@anureo/protocol": "0.3.2",
|
|
27
26
|
"ajv": "8.20.0",
|
|
28
27
|
"zod": "4.6.7"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"@anureo/conformance": "0.3.2",
|
|
32
|
-
"@anureo/adapter-claude": "0.3.
|
|
33
|
-
"@anureo/adapter-codex": "0.3.
|
|
31
|
+
"@anureo/adapter-claude": "0.3.4",
|
|
32
|
+
"@anureo/adapter-codex": "0.3.4",
|
|
34
33
|
"@anureo/adapter-acp": "0.3.2",
|
|
35
34
|
"@anureo/adapter-pi": "0.3.2"
|
|
36
35
|
},
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import { createClient } from '@anureo/sdk'
|
|
4
|
+
import { createAcpAdapter } from '@anureo/adapter-acp'
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
import { rejectionOf } from '../../adapter/acp/tests/test-helpers'
|
|
7
|
+
import { agent } from './agent'
|
|
8
|
+
|
|
9
|
+
test('workflow Zod output uses the same MCP path and retains local refinement failures', async () => {
|
|
10
|
+
const adapter = createAcpAdapter({ command: process.execPath, args: [fileURLToPath(new URL('../../adapter/acp/tests/fixtures/fake-acp.mjs', import.meta.url)), '--scenario', 'mcp-output'] })
|
|
11
|
+
const client = await createClient({ adapter, identity: { adapterId: 'acp', runtimeConfigId: 'workflow-mcp' } })
|
|
12
|
+
try {
|
|
13
|
+
const schema = z.object({ sum: z.number().int() })
|
|
14
|
+
const value = await agent('17 + 25', { client, cwd: process.cwd(), schema })
|
|
15
|
+
expect(value).toEqual({ sum: 42 })
|
|
16
|
+
const failure = await rejectionOf(agent('17 + 25', { client, cwd: process.cwd(), schema: schema.refine(value => value.sum < 0) }))
|
|
17
|
+
expect(failure).toMatchObject({ code: 'AGENT_STRUCTURED_OUTPUT_INVALID', outcomeUnknown: false })
|
|
18
|
+
} finally { await client.dispose() }
|
|
19
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { HarnessError } from '@anureo/protocol'
|
|
2
|
+
import { HarnessSdkError } from '@anureo/sdk'
|
|
3
|
+
|
|
4
|
+
// Only apply to native failed terminal results, never open/submit/observation failures.
|
|
5
|
+
const terminalApiErrors = new Set([
|
|
6
|
+
'AUTH_REQUIRED', 'AUTH_FORBIDDEN', 'USAGE_LIMIT_EXCEEDED', 'RATE_LIMITED', 'NATIVE_SERVICE_UNAVAILABLE',
|
|
7
|
+
])
|
|
8
|
+
|
|
9
|
+
export function isDroppedOutcome(error: HarnessError, status: 'failed' | 'interrupted'): boolean {
|
|
10
|
+
if (error.outcomeUnknown) return false
|
|
11
|
+
if (status === 'interrupted') return error.details?.workflowDisposition === 'skipped'
|
|
12
|
+
return isStartupBlocked(error) || terminalApiErrors.has(error.code)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isStartupBlocked(error: Pick<HarnessSdkError, 'code' | 'outcomeUnknown' | 'details'>): boolean {
|
|
16
|
+
return !error.outcomeUnknown && error.code === 'AGENT_STARTUP_BLOCKED' && error.details?.workflowDisposition === 'startup_blocked'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isStartupBlockException(cause: unknown): cause is HarnessSdkError {
|
|
20
|
+
return cause instanceof HarnessSdkError && isStartupBlocked(cause)
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { createClient, type StructuredOutputToolDefinition } from '@anureo/sdk'
|
|
3
|
+
import { createPiAdapter } from '@anureo/adapter-pi'
|
|
4
|
+
import { fakeFactory } from '../../adapter/pi/tests/fake-host'
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
import { agent } from './agent'
|
|
7
|
+
|
|
8
|
+
test('workflow schema uses direct Pi tools and retains Zod refinement validation', async () => {
|
|
9
|
+
const fake = fakeFactory(host => {
|
|
10
|
+
let output: StructuredOutputToolDefinition | undefined
|
|
11
|
+
Object.assign(host, { async setOutputTool(tool?: StructuredOutputToolDefinition) { output = tool } })
|
|
12
|
+
host.script = async host => { await output!.execute({ sum: 42 }); await host.assistant('submitted') }
|
|
13
|
+
})
|
|
14
|
+
const client = await createClient({ adapter: createPiAdapter({ version: '1.5.2', createHost: fake.factory }), identity: { adapterId: 'pi', runtimeConfigId: 'workflow-direct-tool' } })
|
|
15
|
+
try {
|
|
16
|
+
const schema = z.object({ sum: z.number().int() })
|
|
17
|
+
expect(await agent('17 + 25', { client, cwd: process.cwd(), schema })).toEqual({ sum: 42 })
|
|
18
|
+
await expect(agent('17 + 25', { client, cwd: process.cwd(), schema: schema.refine(value => value.sum < 0) })).rejects.toMatchObject({ code: 'AGENT_STRUCTURED_OUTPUT_INVALID', outcomeUnknown: false })
|
|
19
|
+
expect(fake.opens.every(open => !Object.hasOwn(open, 'mcpServers'))).toBe(true)
|
|
20
|
+
} finally { await client.dispose() }
|
|
21
|
+
})
|