Skip to content

feat(workflows): bounded workflow execution engine with safe resume - #26

Open
spaxe-dev wants to merge 3 commits into
mainfrom
phase7/workflow-execution-engine-20261203
Open

spaxe-dev wants to merge 3 commits into
mainfrom
phase7/workflow-execution-engine-20261203

Conversation

@spaxe-dev

Copy link
Copy Markdown
Owner

Summary

Bounded, declarative workflow execution engine for Zen MCP. It executes a saved WorkflowStore checkpoint by dispatching each allowlisted step through the same guarded tool path as a direct MCP call, so policy, read-only mode, consent, Pause and audit stay authoritative for every step. The checkpoint ledger remains the durable source of truth; the executor adds dependency-aware scheduling, resource bounds, cancellation and safe resume.

Base: origin/main at c9681b250119c5dc4c00933db1901c6df9eeb36b.
Branch: phase7/workflow-execution-engine-20261203.
HEAD: e703a0d9761d3e58f2b790afc4f429704fd448b9 (on top of 1417a6a and bc29824).

What changed

  • Executor (src/zen/workflow-executor.ts): schedules steps whose dependencies are all settled, respects maxConcurrency, maxSteps, maxDurationMs, per-step and workflow-wide retry budgets, tab/source budgets and a cap on intermediate result size. It never talks to the browser directly; an injected invoker runs each step through the guarded path.
  • Ledger (src/zen/workflows.ts): persisted run state (outcome, stop reason, counts, limits) with optimistic revisions and strict transition validation.
  • Projection/values (src/zen/workflow-projection.ts, src/zen/workflow-values.ts): only proven, allowlisted, bounded fields become step results; URLs, page text and raw tool output are never stored.
  • Tooling (src/mcp/server.ts, catalog.ts, guard.ts, policy.ts): one new mutating tool, zen_workflow_run (actions run / cancel), wired into the capability policy and mutating-tool guard. Unknown tool errors are attributed in src/mcp/tool-error-attribution.ts.

Checkpoint and recovery semantics

  • pending → running is persisted before a step is dispatched, so an abrupt exit leaves a detectable running checkpoint.
  • A step becomes completed only when the invoker proves the outcome. Every other settled-but-unverified result becomes unknown.
  • recoveryRequired blocks a new run until uncertain steps are explicitly resolved with zen_workflow_save; completed steps are never replayed.
  • Readiness fix: a run with no pending steps is refused, so a pre-completed checkpoint can never report a zero-step completed. Each step in a run result now carries statusProvenance: "agent_reported" and an executed flag, so pre-existing state is observable rather than implied verified.

Cancellation / deadline race fix

The reviewed race fix is included: a cancellation requested before the deadline stays cancelled even if the deadline fires during the abort grace, and a deadline that fires first stays timed_out. Cancellation wakes the scheduler immediately rather than waiting for maxDurationMs; in-flight work is aborted where supported and recorded unknown when its settlement is not observed. There is no rollback claim.

Unknown outcomes and no automatic replay

Uncertain, interrupted or cancelled-in-flight work is recorded unknown, never failed by the executor and never replayed. A denial proven to happen before dispatch (policy, Pause, consent, capability, invalid arguments) leaves the step pending and stops the run as blocked, which is not a recovery case.

Resource and retry limits

Run bounds may tighten but never widen the workflow's fixed limits: maxSteps, maxConcurrency, maxDurationMs, maxRetriesPerStep, maxTotalRetries, maxTabsCreated, maxSourcesCollected, maxIntermediateResultBytes. maxCheckpointBytes is fixed at workflow creation. Retries apply only to proven non-application, and each retry or resume spends the per-step and workflow-wide budgets, which include earlier runs.

Evidence Mode accounting

Evidence Mode tabs and research sources are charged against the run's tab/source budgets, including across resumes, so repeated resumes cannot exceed the declared allowance.

Validation performed (Windows, Node 22.15.1)

  • npm run typecheck — passed.
  • npm run build — passed.
  • npm test — 722 tests, 707 passed, 15 skipped, 0 failed.
  • npm run context:check — passed (61 tools; budget met).
  • npm run check:skill — passed (1,970 / 2,000 tokens).
  • git diff --check — clean.
  • Disposable-profile live workflow test (separate Windows run): executes a research workflow, cancels an in-flight read, resumes without duplicate tabs — passed. The test owns a throwaway profile under the temp directory; it does not touch the everyday Zen profile.

Remaining limitations

  • macOS/Linux live verification is not done; those platforms remain CI-only.
  • Desktop consent UI is not live-verified (the live suite runs headless and consent-free).
  • Private-window behavior remains best-effort.
  • Workflow state is agent-reported; a run result exposes provenance rather than upgrading it.

Not done here

No merge, no release, and no push to main. main was not modified.

- Fix the cancellation/deadline race: a cancellation requested before the
  deadline now stays cancelled even if the deadline fires during the abort
  grace, and a deadline that fires first stays timed_out.
- Project step results conservatively: only proven outcomes become completed;
  uncertain or interrupted work is recorded unknown and never replayed.
- Enforce run, retry, concurrency, tab, source and intermediate-result bounds,
  including Evidence Mode tab accounting across resumes.
- Refuse a run with no pending steps so a pre-completed checkpoint can never
  report zero-step success, and expose per-step provenance plus whether this
  run executed the step.
- Add regression tests for races, recovery, budgets and provenance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant