workflow 0.0.1-preview.1
Incremental workflow engine: steps, dependencies, fingerprints, caching, and services
workflow#
An incremental workflow execution engine for Zena: dependency graphs, content fingerprints, cache keys with early cutoff, fine-grained per-output invalidation, and on-demand service lifecycles.
workflow performs no I/O of its own. Hosts supply leaf fingerprints, a digest function, a step cache, and step callbacks; the engine schedules work in dependency order with bounded parallelism and returns the updated state.
Installation#
zena pkg add zena-lang:workflow
Example#
import { NullCache } from 'zena-lang:workflow/cache';
import { Engine, StepSource, Succeeded } from 'zena-lang:workflow/engine';
import { Fingerprint } from 'zena-lang:workflow/fingerprint';
import { Graph } from 'zena-lang:workflow/graph';
import { OutputEntry, OutputManifest } from 'zena-lang:workflow/manifest';
import { EngineState } from 'zena-lang:workflow/state';
import { LeafInput, StepKey, StepOutputs, TaskStep } from 'zena-lang:workflow/step';
let compile = new TaskStep(
new StepKey('compile'),
new Fingerprint('cmd:compile-v1'),
[new LeafInput('src/main.zena')],
async (ctx) => {
return new OutputManifest([
new OutputEntry('out/main.wasm', new Fingerprint('wasm-hash-1')),
]);
},
);
let bundle = new TaskStep(
new StepKey('bundle'),
new Fingerprint('cmd:bundle-v1'),
[new StepOutputs(new StepKey('compile'))],
async (ctx) => {
return new OutputManifest([
new OutputEntry('dist/app.wasm', new Fingerprint('bundle-hash-1')),
]);
},
);
let graph = new Graph([compile, bundle]);
let leaves = {'src/main.zena' => new Fingerprint('src-hash-1')};
let engine = new Engine((data) => new Fingerprint( sha256(data) ), new NullCache());
// First run executes both steps in topological order.
let first = await engine.run(graph, leaves, EngineState.empty());
// Second run with identical inputs and prior state is a no-op (StepSource.Fresh).
let second = await engine.run(graph, leaves, first.state);
Core Concepts#
- Steps and inputs (
workflow/step):TaskStep: runs to completion and returns anOutputManifest.ServiceStep: starts on demand only when a dependent step actually executes, and stops when its last dependent settles (or stays pinned across watch runs in aServicePool).LeafInput(name): a named external input resolved from theleavesmap passed toEngine.run.LeafSet(name, resolve): a lazy input set resolved after upstream dependencies finish (for example, expanding globs over generated files).StepOutputs(step): a dependency on another step whose output manifest (or service identity) enters this step's cache key. Because dependents key on upstream output fingerprints rather than upstream input fingerprints, an upstream step that reruns and produces identical outputs leaves downstream dependents fresh (early cutoff).StepOrder(step): an ordering-only dependency (like Wireit's"cascade": false) that waits for the upstream step without folding its outputs into the dependent's cache key.
- Validated graph (
workflow/graph):Graphchecks that step keys are unique, all referenced steps exist, no step lists duplicate inputs, and the dependency graph is acyclic (reporting the cycle path inWorkflowError), producing a deterministic topological order. - Caching and freshness (
workflow/engine,workflow/cache,workflow/state):- If a step's
CacheKeymatches theStepRecordin the previousEngineState(and optionalRunOptions.verifyOutputssucceeds), the step isStepSource.Freshwithout consulting the cache or starting services. - Otherwise
StepCache.getis checked (StepSource.Cache), falling back to executing the step callback (StepSource.Ran) under the run's parallelism limit.
- If a step's
- Incremental per-output deltas (
workflow/delta,workflow/manifest):- Each
OutputEntryin anOutputManifestcan recorddependsOn(the namespaced inputsleaf:...,step:...it was derived from). - When a step reruns with the same
configfingerprint,RunContextprovidesdelta(changed,added,removed),previous(OutputManifest), andstaleOutputs. The engine verifies before caching that any output carried over as non-stale kept its previous fingerprint.
- Each
Libraries#
workflow/engine—Engine,RunOptions,RunResult,StepOutcome(Succeeded,Failed,Skipped),StepSource,ServicePool, andPinnedService.workflow/step—Step,TaskStep,ServiceStep,StepKey,InputRef(LeafInput,LeafSet,StepOutputs,StepOrder),RunContext,ServiceContext, andServiceHandle.workflow/graph—Graphvalidation and deterministic topological sorting.workflow/manifest—OutputManifestandOutputEntry.workflow/state—EngineStateandStepRecordfor persisting prior-run state across runs.workflow/cache—StepCacheinterface andNullCache.workflow/fingerprint—Fingerprint,CacheKey, andDigest.workflow/canonical— Canonical length-prefixed encodings (cacheKeyFor,manifestFingerprint,ResolvedInput,compareStrings,sortBy).workflow/delta—InputDelta,computeDelta, andstaleOutputNames.workflow/errors—WorkflowError.
License#
MIT
1.5 KiB
Raw
import {OrderedHashMap} from 'zena:collections';
import {ResolvedInput} from './canonical.zena';
import {CacheKey, Fingerprint} from './fingerprint.zena';
import {OutputManifest} from './manifest.zena';
import {StepKey} from './step.zena';
/**
* What the engine remembers about one step between runs: enough to
* detect freshness (the cache key), to serve dependents (the
* manifest), and to compute an input delta for incremental reruns
* (the config fingerprint and resolved inputs of the recording run).
*/
export class StepRecord(
config: Fingerprint,
cacheKey: CacheKey,
manifest: OutputManifest,
inputs: Array<ResolvedInput>,
)
/**
* Prior-run state: each step's cache key and output manifest. Plain
* data — hosts persist it between runs and hand it back; an empty
* state is a valid first run. Iteration order is insertion order, so
* serialization is deterministic.
*/
export class EngineState {
#records: OrderedHashMap<String, StepRecord>;
new() : #records = new OrderedHashMap<String, StepRecord>();
static empty(): EngineState {
return new EngineState();
}
size: i32 {
get {
return this.#records.size;
}
}
recordOf(key: StepKey): StepRecord? {
if (let (true, record) = this.#records.get(key.value)) {
return record;
}
return null;
}
setRecord(key: StepKey, record: StepRecord): void {
this.#records[key.value] = record;
}
forEach(callback: (key: String, record: StepRecord) => void): void {
this.#records.forEach(callback);
}
}
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.1-preview.1 | 2026-11-19 | none |
|