Skip to content
Source
Sign inSign up
TanStack/workflow

Harden runtime lease recovery and event publishing #15

Mergedtannerlinsleymerged 1 commit intomain fromtaren/runtime-lease-recoverymerged by tannerlinsley

tannerlinsleycommented

What changed

  • heartbeat active run leases while the runtime is driving workflow code
  • recover expired running executions during sweep() by replaying durable checkpoints
  • add best-effort live event publishing at runtime and per-call scope
  • expose recovered sweep results and limits
  • cover initialized and pre-initialization crash recovery across runtime and store contracts

Why

The execution-store contract already supported lease heartbeats and stale-run claims, but the runtime driver did not use them. A worker crash could therefore leave a run stuck as running, while long steps could outlive their lease. The runtime also collected core events to completion without exposing core's live publisher hook.

This makes leases operational, gives sweeps a complete crash-recovery path, and provides the live event seam needed by streaming integrations without changing durable event semantics.

Impact

Existing workflows continue to run unchanged. sweep() now returns a recovered collection and accepts maxRecoveredRuns. Valid lease durations behave as before; invalid non-positive or non-finite durations now fail before mutating the store.

Validation

  • pnpm test:pr
  • core and runtime durability tests
  • shared store contract against in-memory, Cloudflare D1, and Drizzle Postgres
  • package type checks, lint, docs checks, and builds

Summary by CodeRabbit

  • New Features

    • Added recovery for interrupted workflow runs during runtime sweeps.
    • Added automatic lease heartbeats for long-running executions.
    • Added best-effort live event publishing at runtime and per-operation levels.
    • Added recovery limits and recovered-run details to sweep results.
    • Exported the event publisher type for integration use.
  • Documentation

    • Updated runtime documentation with lease, recovery, sweep, and live event behavior.
  • Tests

    • Added coverage for execution recovery, lease renewal, event publishing, and stale-run handling.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 70089aff-1a1f-448b-be04-48b2cf945e7a

      📥 Commits

      Reviewing files that changed from the base of the PR and between fa3290351713c95db92657fbe1bbf04387282adc and 0bc426bdf0be6415d1d4727f835419e3155a95a5.

      📒 Files selected for processing (10)
      • .changeset/bright-runs-recover.md
      • docs/api/runtime.md
      • docs/guide/runtime-model.md
      • packages/workflow-core/src/engine/run-workflow.ts
      • packages/workflow-core/tests/engine.durability.test.ts
      • packages/workflow-runtime/src/index.ts
      • packages/workflow-runtime/src/runtime-driver.ts
      • packages/workflow-runtime/src/types.ts
      • packages/workflow-runtime/tests/contracts/workflow-execution-store.contract.ts
      • packages/workflow-runtime/tests/runtime-driver.test.ts

      📝 Walkthrough

      Walkthrough

      The workflow core now supports checkpoint recovery for reclaimed runs. The runtime renews active leases, recovers stale executions during sweeps, exposes recovery limits and results, and supports best-effort live event publishing through configured or per-call publishers.

      Changes

      Workflow runtime reliability

      Layer / File(s)Summary
      Checkpoint recovery contract
      packages/workflow-core/src/engine/run-workflow.ts, packages/workflow-core/tests/engine.durability.test.ts, docs/guide/runtime-model.md
      Recovery dispatch resumes interrupted executions from durable checkpoints, with durability tests covering single execution of checkpointed steps.
      Lease normalization and heartbeats
      packages/workflow-runtime/src/runtime-driver.ts, packages/workflow-runtime/tests/contracts/*, packages/workflow-runtime/tests/runtime-driver.test.ts
      Runtime drive paths normalize lease durations, renew active leases, stop heartbeats after completion, and propagate heartbeat failures.
      Live event publishing
      packages/workflow-runtime/src/types.ts, packages/workflow-runtime/src/index.ts, packages/workflow-runtime/src/runtime-driver.ts, packages/workflow-runtime/tests/runtime-driver.test.ts, docs/api/runtime.md, docs/guide/runtime-model.md
      Configured and per-call publishers receive live events without requiring event retention in run results; publisher failures are best-effort.
      Sweep recovery orchestration
      packages/workflow-runtime/src/runtime-driver.ts, packages/workflow-runtime/tests/runtime-driver.test.ts, docs/api/runtime.md, docs/guide/runtime-model.md, .changeset/bright-runs-recover.md
      Sweeps claim stale executions up to maxRecoveredRuns, replay them with recovery enabled, and include recovered runs in summaries and documentation.

      Estimated code review effort: 4 (Complex) | ~60 minutes

      Sequence Diagram(s)

      sequenceDiagram
        participant WorkflowRuntime
        participant ExecutionStore
        participant RunWorkflow
        participant EventPublisher
        WorkflowRuntime->>ExecutionStore: claim stale execution
        WorkflowRuntime->>RunWorkflow: drive with recovery enabled
        RunWorkflow->>ExecutionStore: replay checkpoints
        RunWorkflow-->>WorkflowRuntime: emit workflow events
        WorkflowRuntime->>EventPublisher: best-effort publish events
        WorkflowRuntime-->>WorkflowRuntime: return recovered result

      Possibly related PRs

      • TanStack/workflow#2: Introduced the workflow core engine entrypoint that this change extends with recovery dispatch.
      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the main change: runtime lease recovery plus live event publishing.
      Description check✅ PassedThe description covers the changes, motivation, impact, and validation, though it uses custom headings instead of the template.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `taren/runtime-lease-recovery`

      Comment `@coderabbitai help` to get the list of available commands.
    2. 🚀 Changeset Version Preview

      2 package(s) bumped directly, 6 bumped as dependents.

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/workflow-core0.0.4 → 0.0.5Changeset
      @tanstack/workflow-runtime0.0.3 → 0.0.4Changeset
      @tanstack/workflow-cloudflare0.0.3 → 0.0.4Dependent
      @tanstack/workflow-netlify0.0.4 → 0.0.5Dependent
      @tanstack/workflow-railway0.0.3 → 0.0.4Dependent
      @tanstack/workflow-store-cloudflare-d10.0.4 → 0.0.5Dependent
      @tanstack/workflow-store-drizzle-postgres0.0.5 → 0.0.6Dependent
      @tanstack/workflow-vercel0.0.4 → 0.0.5Dependent
    3. More templates

      @tanstack/react-template

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/react-template@15

      @tanstack/react-template-devtools

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/react-template-devtools@15

      @tanstack/solid-template

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/solid-template@15

      @tanstack/solid-template-devtools

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/solid-template-devtools@15

      @tanstack/template

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/template@15

      @tanstack/template-devtools

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/template-devtools@15

      @tanstack/workflow-cloudflare

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-cloudflare@15

      @tanstack/workflow-core

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-core@15

      @tanstack/workflow-netlify

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-netlify@15

      @tanstack/workflow-railway

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-railway@15

      @tanstack/workflow-runtime

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-runtime@15

      @tanstack/workflow-store-cloudflare-d1

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-store-cloudflare-d1@15

      @tanstack/workflow-store-drizzle-postgres

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-store-drizzle-postgres@15

      @tanstack/workflow-vercel

      npm i https://pkg.pr.new/TanStack/workflow/@tanstack/workflow-vercel@15

      commit: 0bc426b

    4. tannerlinsley merged thisb928717

    5. tannerlinsley closed this

Sign in to join this conversation.