返回 CodeWhale
lib.rs
根目录 / crates / workflow-js / src / lib.rs
1 //! Dynamic Workflow runtime for CodeWhale.
2 //!
3 //! This crate is the imperative half of Workflow: a sandboxed QuickJS
4 //! (rquickjs) runtime that executes a model-authored JS program which
5 //! dispatches fleet-routed subagents via `task()`, fans out with
6 //! `parallel()`/`pipeline()`, and reports progress with `log()`/`phase()`. The static,
7 //! declarative IR (record/replay, model policy) stays in `codewhale-workflow`;
8 //! this crate only speaks to the outside world through the
9 //! [`WorkflowDriver`] seam, so it is fully testable without spawning a real
10 //! subagent (see [`testing::FakeDriver`]).
11 //!
12 //! # Script surface
13 //!
14 //! Every script runs inside an async function with these globals:
15 //!
16 //! * `args` — the invocation input, verbatim.
17 //! * `await task(opts)` — dispatch one subagent; resolves to the full result
18 //! text, or to a parsed + schema-validated object when `opts.responseSchema`
19 //! is set. Throws on rejection, failure, cancellation or an owner budget
20 //! exhaustion. A reply that fails `responseSchema` gets up to
21 //! [`SCHEMA_REPAIR_MAX_ATTEMPTS`] bounded repairs (#5583) before it throws;
22 //! the failed attempts are reported as driver receipts either way.
23 //! * `parallel(thunks, opts)` — fan-out, at most [`PARALLEL_MAX_ITEMS`] items.
24 //! `opts.mode` is `settled` (default), `fail-fast`, or `partial`; an
25 //! unrecognized mode throws. `settled` keeps today's ergonomics — an
26 //! ordinary failed slot resolves to `null` — while schema-contract failures
27 //! and run cancellation still fail the run. `fail-fast` rejects the fan-out
28 //! with the first non-fatal slot error instead of nulling it. `partial`
29 //! resolves every non-cancellation failure to
30 //! `{ __taskError: { index, kind, message } }`.
31 //! * `pipeline(items, ...stages)` — per-item stage chains with no barrier
32 //! between stages; same cap and the same three modes via the
33 //! `pipeline(items, { stages, mode })` overload.
34 //!
35 //! Both fan-outs attach a non-enumerable `errors` array to the resolved
36 //! result — `[{ index, kind, message }]`, ordered by index, empty when
37 //! nothing failed. The array's own contents, length, and JSON encoding are
38 //! unchanged, so a dropped slot is now inspectable instead of erased.
39 //!
40 //! `kind` is a [`TaskErrorKind`] wire string (`admission`, `budget`,
41 //! `cancelled`, `agent`, `schema`, `driver`) assigned by the host where the
42 //! failure happened, or `script` for an error the script threw itself. It is
43 //! read from the thrown `Error`'s `.kind`, never inferred from message text.
44 //! * `log(msg)` / `phase(title)` — progress events forwarded to the driver.
45 //! * `budget` — constant `{ total: null, metered: false, spent: () => null, remaining: () => null }`.
46 //!
47 //! `Date.now()`, `new Date()`, `Date.parse/UTC`, and `Math.random()` throw:
48 //! runs must be deterministic so recorded traces can be replayed.
49 //!
50 //! # Ownership boundaries
51 //!
52 //! Fleet roster resolution for `profile` happens driver-side; this crate
53 //! normalizes and token-validates the profile string, nothing more.
54
55 mod driver;
56 mod error;
57 mod schema;
58 pub mod testing;
59 mod vm;
60
61 pub use driver::{
62 ProgressEvent, SpawnedTask, TaskCompletion, TaskRequest, ToolCallRequest, ToolCallResponse,
63 ToolInvoker, WorkflowDriver, normalize_profile,
64 };
65 pub use error::{DriverError, TaskErrorKind, WorkflowJsError};
66 pub use schema::{
67 ReplyDecodeError, SCHEMA_RAW_CARRY_CHARS, SCHEMA_RAW_PREVIEW_CHARS, SCHEMA_REPAIR_MAX_ATTEMPTS,
68 compile_schema, decode_reply, repair_prompt,
69 };
70 pub use vm::{VmLimits, WorkflowRunCancel, WorkflowVm, normalize_task_cwd, normalize_task_cwd_in};
71
72 /// Maximum concurrently executing agents within one Workflow run.
73 ///
74 /// Fan-out may *declare* more work via `parallel()` / `pipeline()`, but the
75 /// host admits at most this many live `task()` children at once; additional
76 /// spawns wait for a slot.
77 pub const WORKFLOW_MAX_CONCURRENT: usize = 128;
78
79 /// Maximum items per `parallel()` or `pipeline()` call (design §4.2).
80 /// Bounds one materialized fan-out array; it does not limit total work.
81 pub const PARALLEL_MAX_ITEMS: usize = 1000;
82
82 lines RUST