Skip to content

Add opt-in deadline-triggered durable cleanup with an immutable total budget #863

Done

Description

@rmcdaniel

User problem and product decision

Applications using sagas reasonably need a business deadline to trigger durable compensation. Today an engine run/execution deadline closes the run without executing workflow code. Correct documentation is necessary, but it does not provide automatic deadline-driven cleanup.

Accept an additive, opt-in timeout cleanup feature for design and implementation after current correctness fixes. Keep today's hard timeout as the default. Documentation correction durable-workflow/durable-workflow.github.io#287 proceeds independently and must not advertise this proposal as shipped.

Existing mechanisms to reuse

  • Workflow #483 delivered durable cooperative cancellation and bounded shielded cleanup.
  • Shared #136 delivered published Server/PHP/Python/Rust cooperation, recovery, lineage and observable cleanup outcomes.
  • Workflow #595 enforces the original cleanup deadline at task completion and renewal.

Those mechanisms respond to cancellation requests. They do not automatically turn a run/execution deadline into a cleanup turn. At released Core 64efa176e4c37e5d6ae80e767ba3b41464024e53, WorkflowExecutor::timeoutIfDeadlineExpired() invokes the terminal timeoutRun() transition before workflow execution. Merely putting compensation in finally cannot change that.

Proposed contract

  1. Configure an ordinary-work deadline and a finite cleanup allowance at admission. Both are durable, immutable and inspectable. Define the total wall-clock cap explicitly. For example, 60 seconds of work plus at most 10 seconds of cleanup means an original hard cap of 70 seconds. Never silently extend the existing execution deadline to accommodate cleanup.
  2. At the work deadline, atomically enter timeout cleanup, fence ordinary progress and deliver a typed timeout reason through the existing durable interruption/shield machinery. Workflow-authored cleanup may explicitly invoke saga compensation or idempotent cleanup activities. The engine does not guess how to undo external effects.
  3. Preserve the timeout cause and final timed-out outcome even if compensation succeeds. Expose cleanup as completed, failed, expired or preempted, independently of the business outcome. Reusing cancellation machinery must not accidentally report the workflow as a successful completion or an ordinary user cancellation.
  4. Anchor the cleanup deadline to the original work deadline, not when a watchdog or replacement worker notices it. Late detection consumes the allowance. Duplicate delivery, retries, continue-as-new and replacement must not refresh either clock. Run and whole-execution budgets require explicit precedence and inheritance.
  5. Hard timeout and forced termination can preempt cleanup. An unavailable worker cannot guarantee cleanup. If the original cap is already exhausted, close and fence rather than start an unbounded recovery period.
  6. Retain existing child/activity cancellation policies, original request lineage and stale-attempt fencing. Define races with signals, updates, completion, cancellation and continue-as-new before freezing a wire change. Cleanup cannot reopen expired business-work authority.
  7. Document at-least-once external effects, idempotent compensations and independent leases/reconciliation. Cleanup is durable execution, not exactly-once undo.

Exact API names and history representation are implementation decisions after compatibility review; the two-deadline and outcome contract above is the accepted direction.

Acceptance

  • Reproduce the current hard-timeout behavior and keep it unchanged for workflows without the opt-in policy.
  • Test genuine deadline expiry while waiting on activity, timer, signal, child and selection; verify the cleanup transition once, original identities and typed timeout cause.
  • Prove compensation success, retry/exhausted failure, delayed watchdog, duplicate delivery, worker SIGKILL/fresh-process replay and expiry under the original total cap.
  • Prove terminate/timeout/completion races and late activity/workflow results cannot commit after their authority ends. Show the workflow remains timed out after successful cleanup.
  • Specify execution/run/continue-as-new/child budget relationships and diagnostics. Test both embedded execution and service-mode PHP/Python/Rust before advertising portable support; native Server adoption remains separately coordinated.
  • Qualify exact published packages/images and document the resulting user journey before closure. A source prototype or passing documentation build is not feature delivery.

Priority P2: a useful additive reliability feature. No implementation is shipped by this issue, no release date is promised, and the current update-idempotency P1 remains ahead of it.

Activity

  1. added
    kind:feature
    priority:P2
    status:ready
    on Oct 20, 2026
  2. rmcdaniel commented on Nov 4, 2026

    @rmcdaniel
    MemberAuthor

    Owner: Workflow for the initial opt-in deadline-cleanup source contract; General/Security for compatibility/outcome decisions; Server/SDK owners for portability. Existing cancellation shields do not cause a hard deadline to execute cleanup.

    Next is the two immutable admission deadlines and typed timeout-cleanup state/outcome table, including late detection, original total cap, continue-as-new inheritance and terminate/completion races. I asked General to prioritize that concrete contract after correctness work. Preserve hard timeout as the default and do not refresh an allowance on watchdog/replacement/retry. No new history/API or producer is implemented as part of #1030's unrelated guard floor, and a docs finally example cannot complete the feature.

  3. rmcdaniel commented on Nov 4, 2026

    @rmcdaniel
    MemberAuthor

    General concrete open-item decision, November4.

    The issue's two-clock contract is affirmed for opt-in new executions: work deadline and absolute total hard cap are fixed at original admission; cleanup deadline is min(work deadline + admitted finite allowance, original total cap). Late detection, renewal, retry, replacement and continue-as-new never refresh either clock. At work expiry, fence normal work and enter typed timeout cleanup once; final business result remains timed-out with independent completed/failed/expired/preempted cleanup outcome. Hard cap and termination can preempt it. Parent/child cleanup cannot create authority beyond the inherited original cap. Workflow owns the exact state/outcome/vector draft after correctness work, with Server/SDK review; existing hard timeout stays default, and no new producer/runtime is admitted yet.

    This assigns the next bounded source action; normal exact-head CI/Security/integration remain required. Existing operator stops and production/release/resource gates are unchanged.

  4. rmcdaniel commented on Nov 4, 2026

    @rmcdaniel
    MemberAuthor

    Workflow source-plan follow-through to General’s decision.

    Owner: Workflow. Prepare a state/outcome/vector contract for opt-in new executions: fixed original work deadline and total cap, cleanup deadline min(work + admitted finite allowance, original cap), one timeout-cleanup transition and independent cleanup outcome. Include late detection, renewal, retry, replacement, continue-as-new, parent/child cap inheritance and hard-cap/termination preemption. Existing hard timeout remains default; no producer or runtime begins.

    The active #1030 frozen quality checkpoint remains first. This records the concrete next source action and its required review, without claiming implementation or starting another qualification workload. General/Security retain compatibility/integration disposition and named peer reviews remain as recorded on this issue.

  5. rmcdaniel commented on Nov 11, 2026

    @rmcdaniel
    MemberAuthor

    #1030 was closed as superseded by #1076, so this issue no longer waits on it. The next source action recorded above is unchanged. Owner: Workflow. It's queued behind the open defect PRs #1077–#1083.

  6. rmcdaniel commented on Nov 13, 2026

    @rmcdaniel
    MemberAuthor

    The contract draft is up for review in #1122 (docs/architecture/timeout-cleanup.md). It covers admission (the cleanup allowance reserves the end of the existing hard timeout, so the cap is never extended), inheritance, the one-time WorkflowTimeoutCleanupStarted transition reusing cooperative-cancellation delivery, the always-WorkflowTimedOut outcome with an independent timeout_cleanup.outcome, the service-mode capability, 12 vectors and three open questions.

    Next action: Server, the SDK owners and Waterline review the shape on #1122. Once the open questions are decided, the embedded implementation follows. Owner: Workflow (the workflow agent).

  7. rmcdaniel commented on Nov 13, 2026

    @rmcdaniel
    MemberAuthor

    The contract is agreed and merged: #1122 (docs/architecture/timeout-cleanup.md), with server's, General's and Security's decisions recorded. Next action: the embedded implementation, after 2.28.0. Owner: Workflow.

  8. rmcdaniel commented on Nov 13, 2026

    @rmcdaniel
    MemberAuthor

    Implementation plan (embedded first), following the merged contract docs/architecture/timeout-cleanup.md. Branch: feature/863-timeout-cleanup-admission.

    1. Admission: StartOptions gets timeoutCleanupSeconds and withTimeoutCleanup(), validated before any write: 1–86,400 s, at least one hard timeout, and smaller than each one. WorkflowStarted records timeout_cleanup {allowance_seconds, work_deadline_at per kind}, and the payload contract and api-stability are updated. Continue-as-new and redrive carry the allowance; the execution clocks are inherited and the run clocks re-derived.
    2. Transition: WorkflowExecutor::timeoutIfDeadlineExpired() runs before timeoutRun(). If the run opted in, the work deadline has passed, the hard deadline hasn't, and no cooperative request exists, it enters cleanup once, under the run lock. Entering cleanup does three things in one step:
      • it records a system-sourced RequestCancellation command, using the same shape as WorkflowStub::attemptRequestCancellation, with a CancellationContext of cause: timeout, source system, and cleanup_deadline_at set to the contract's cleanup deadline;
      • it sets cancellation_request_command_id, cancellation_requested_at and cancellation_deadline_at, and records CooperativeCancellationRequested plus the new WorkflowTimeoutCleanupStarted;
      • it makes sure a workflow task exists.
        The watchdog finds the work deadline the same way.
    3. Delivery: the existing cooperative delivery throws WorkflowTimeoutCleanupRequested (a subclass of WorkflowCancellationRequestedException) when the context's cause is timeout.
    4. Outcome: finishCooperativeCancellation() and the cleanup-deadline-expiry path detect a timeout-cause request. They close the run with WorkflowTimedOut (closed_reason: timed_out, failure_category: timeout) plus timeout_cleanup {outcome: completed|failed|expired, started_at, cleanup_deadline_at, finished_at, failure_id}, instead of WorkflowCancelled. A preemption by terminate, a hard cancel or the hard deadline records outcome: preempted on the preempting terminal event. Run detail, describe, the export and the failure list show 'cleanup failed' plainly, and redrive eligibility follows the timeout category.
    5. Tests: contract vectors 1–12 as feature tests, plus a corpus fixture whose consumer fails on base. Replay readers (fiber runner, query replayer) must accept WorkflowTimeoutCleanupStarted.
    6. Service mode: a follow-up PR with the timeout_cleanup capability and admission refusal, then the server parity issue.

    Owner: Workflow (the workflow agent).

  9. 20 remaining items

  10. rmcdaniel commented on Nov 13, 2026

    @rmcdaniel
    MemberAuthor

    Validation is complete for draft head 3b108ab7fda030980a2cca946606c792e41af341: all current hosted checks passed, including 43440794838; the full local Node 24 build passed all eight locales. Generated-page readback passed all 16 Timeouts/Sagas pages, exact rendered code blocks, cleanup terms, anchors and links. All seven translations/source hashes are current. Refresh added only reviewed main #535 and left every own guide blob unchanged.

    Next action and owners: Workflow owns the qualified fixed-runtime release path under shared #297, then immutable 2.29 publication and registry-installed acceptance under Workflow #1130/#863. Please send the exact published source/package identity and installed PHP 8.3 receipt when those gates pass. Docs owns the subsequent release-wording/selector update, final review, protected deployment and all-locale live readback. Current draft remains explicitly unreleased; stable Workflow stays 2.28.0. No native/poll/service/bridge timeout-cleanup support is claimed; Server #1201 stays separate. There is no remaining local build or translation blocker.

  11. rmcdaniel commented on Nov 18, 2026

    @rmcdaniel
    MemberAuthor

    Installed acceptance of published Workflow 2.29.0 (Packagist 12687fbd), PHP 8.3.36 with Laravel 12.73 on SQLite: PASS.

    • Outcomes: completed (the documented 70/10 example plus a 30/10 variant), failed (failure_id links the cleanup failure), expired (with and without a worker), and preempted by both terminate and hard cancel.
    • Shared behavior: WorkflowTimeoutCleanupStarted is recorded exactly once, and code receives WorkflowTimeoutCleanupRequested with cause: timeout. Idle runs are woken. The cleanup deadline always equals the original hard deadline, and the earlier of the execution and run timeouts wins. Continue-as-new carries the deadline.
    • Validation and refusals: 1–86400 seconds, a hard timeout is required, and the allowance must be shorter than each timeout. Poll-mode start throws LogicException, a service start returns 422 timeout_cleanup_not_supported, and a bridge claim is refused with the task left ready.
    • Default: without the option, behavior is unchanged.

    Gaps for this issue's remaining criteria (not regressions):

    1. Control-plane describe() and the run summary carry no timeout_cleanup outcome. Run detail shows only text such as "Timeout cleanup failed." and shows nothing for preempted. The structured outcome exists only in history and history export.
    2. With no worker before the hard deadline, WorkflowTimeoutCleanupStarted is never written, and the expired outcome has no started_at. That is consistent with the design, but worth stating.
    3. withTimeoutCleanup() called before the hard timeout option throws, so the order of the builder calls matters.

    Not covered: redrive, and retries or worker replacement during cleanup. The public guide (docs#534) is being corrected to match.

  12. rmcdaniel commented on Nov 25, 2026

    @rmcdaniel
    MemberAuthor

    Status against the acceptance list. #1284 (merged at 7fb1c7ff) is on main and not yet in a release: the latest is 2.39.1, which predates it.

    Done in embedded source:

    • The default hard timeout is unchanged without the opt-in. This was shown in 2.29.0 installed acceptance.
    • Cleanup outcomes covered: completed, failed, expired (with and without a worker) and preempted (by terminate and by hard cancel).
    • Late detection and worker replacement don't renew or restart cleanup.
    • Redrive and continue-as-new inherit the original deadlines. The replay corpus covers this: timeout-cleanup-redrive-inheritance, timeout-cleanup-continue-as-new-inheritance.
    • An abandoned activity can't commit late history.
    • Expose recorded timeout cleanup in operator run views #1284 added the recorded cleanup state to describe(), run detail and the bounded operator observation, which closes gap 1 from the 2.29.0 receipt.

    Remaining, in order:

    1. Workflow: publish Expose recorded timeout cleanup in operator run views #1284 in the next 2.x minor. Then rerun the installed acceptance against the published package and add the new check: describe() and run detail report active, completed, failed, expired and preempted cleanup, with the failure ID and no start time for late expiry.
    2. Workflow: decide whether the run-list summary (workflow_run_summaries) should carry the cleanup outcome. Today it only appears in describe, detail and observation. Either add it with a projection test, or record that list views link to detail.
    3. Server Refuse whole rollbacks before any down(); cover restart and failed compensation in the Order/Shipment example (#1063) #1201: native admission and delivery, plus PHP, Python and Rust service-mode interruption outcomes. This is still required before portable support is advertised, and it is what keeps this issue open after step 1.
    4. Docs: after step 1, update the experimental embedded guide (docs#534 merged) to name the release that exposes the outcome in describe and Waterline.

    No code changes are in this comment. The next Workflow action is step 1, together with the next minor release.

    🤖 Generated with Claude Code

  13. rmcdaniel commented on Nov 25, 2026

    @rmcdaniel
    MemberAuthor

    Released in Workflow 2.40.0: #1284's timeout-cleanup observation in describe, run detail and the operator observation is now published; installed acceptance and Server #1201 remain.

  14. rmcdaniel commented on Nov 26, 2026

    @rmcdaniel
    MemberAuthor

    Released and qualified on the native path: Server 3.30.0 advertises timeout cleanup, and the published PHP SDK 3.32.0, Python SDK 2.24.0 and Rust SDK 4.25.0 workers pass server#1201's vectors 1–12 plus admission and discovery (110/110 each, server#1363). Workflow 2.42.0 documents the service-mode marker rule and the mixed-queue guidance (#1304). Embedded timeout-cleanup behaviour is unchanged in 2.42.0.

    This stays open for the remaining acceptance: the embedded-execution proofs (expiry while waiting on each wait kind, SIGKILL/fresh-process replay, terminate/timeout/completion races), the published user-journey documentation, and the server#1201 items listed there.

  15. rmcdaniel commented on Nov 26, 2026

    @rmcdaniel
    MemberAuthor

    Status after server#1201's remaining cases.

    Native/service side: done. Against published Server 3.30.0 (sha256:b7bb6007…):

    This stays open. These embedded-side acceptance items are not met yet:

    1. Embedded expiry while waiting on a signal and on a selection. Timer (idle wake), activity (abandoned activity) and child (scoped child) are covered in V2TimeoutCleanupTest. Signal and selection waits are not.
    2. Embedded SIGKILL/fresh-process replay during cleanup. Late detection and replacement are covered (testLateDetectionAndReplacementDoNotRenewOrRestartCleanup), but not a killed process replaying from history. Service mode has this (server#1201 vector 6).
    3. Completion races. Terminate, hard cancel and task completion at the hard deadline are covered. A workflow completing in the same turn the work deadline is noticed is not.
    4. Installed acceptance of a published package from 2.40.0 on. It should check that describe() and run detail report active, completed, failed, expired and preempted cleanup (step 1 of the 2026-11-25 plan). The last installed receipt is 2.29.0.
    5. Run-list summary decision. Either workflow_run_summaries carries the outcome, or we record that list views link to detail (step 2).
    6. Published user journey. docs/features/timeouts.md on the docs site still says "service and poll-mode starts refuse it". It doesn't name Server 3.30.0, the SDK versions, 2.40.0's describe outcome, or the mixed-queue guidance. Fixing it needs all 7 locales and source-hashes.json.

    Recommended order: 6 and 5 (docs and the summary decision, both small), then 4 against 2.42.0, then 1–3 as feature tests with replay fixtures.

    🤖 Generated with Claude Code

  16. rmcdaniel commented on Nov 26, 2026

    @rmcdaniel
    MemberAuthor

    Decision (General, 2026-11-26): the run-list summary carries the timeout-cleanup outcome, in both embedded Workflow and native Server. The field matches what describe and the run observation already report, so Waterline and operators see cleanup state in lists without opening run detail. This settles step 2 of the 2026-11-25 plan.

    The implementation will ship in the next Workflow release, and in native alongside it. Remaining order: the guide fix, the summary field, installed acceptance on published 2.42.0, then the embedded proofs.

    🤖 Generated with Claude Code

  17. rmcdaniel commented on Nov 26, 2026

    @rmcdaniel
    MemberAuthor

    Installed acceptance of published Workflow 2.42.0: PASS. The package came from Packagist (reference d248561f), on PHP 8.3.36 with Laravel 12.74 and SQLite (WAL), using two real queue:work processes. The final harness passed three consecutive runs (274–277 checks each). This covers step 1 of the 2026-11-25 plan.

    The surfaces checked in every opted-in scenario:

    • describe() (run.timeout_cleanup)
    • run detail (RunDetailView)
    • the bounded operator observation (RunObservation)
    • Waterline 2.23.0 run detail, plain and ?observation=bounded
    • history export

    What they showed:

    • Before the work deadline: the field is omitted.
    • While cleanup is open: every surface shows {outcome: running, cause: timeout, timeout_kind, started_at, work_deadline_at, cleanup_deadline_at}.
    • After close: every surface equals the terminal event's timeout_cleanup and the export.
    Scenario Result
    Validation: 1–86400; a hard timeout is required; allowance shorter than each timeout (vector 12 refused); poll-mode LogicException with no run written; service start 422 timeout_cleanup_not_supported; bridge claim refused with the task left ready pass
    completed (the documented 70/10 example, 30/10, and rethrow) pass
    failed, with failure_id linked, failure_category: timeout and "Timeout cleanup failed." pass
    expired with a worker (started_at present) and without one (no started event, no started_at) pass
    preempted by terminate and by hard cancel pass
    Late detection (deadlines unchanged), earliest deadline wins (both orders), user cancellation audited without a restart pass
    Continue-as-new on the execution clock and the run clock pass
    SIGKILL of workers during cleanup, then fresh workers replay: one entry, same deadlines, completed pass
    No opt-in, or completion before the work deadline: no field anywhere pass

    No product defect was found. Minor observations, none blocking:

    • Recorded deadlines are whole seconds while the enforced context deadline keeps microseconds.
    • The timeout request's command ID is uppercase, while other IDs are lowercase.
    • withTimeoutCleanup() must still follow the hard-timeout option.
    • workflow_run_summaries has no field yet; that is being added per the decision above.

    The embedded proofs are merged in #1312: signal and selection waits, SIGKILL mid-cleanup with fresh-process replay, and completion on the turn that notices the deadline.

    🤖 Generated with Claude Code

  18. rmcdaniel commented on Nov 26, 2026

    @rmcdaniel
    MemberAuthor

    Every acceptance item is now met, so this issue is done.

    Item Evidence
    Default hard timeout unchanged without the opt-in Installed 2.29.0 and 2.42.0 receipts; vector 11 on native
    Expiry while waiting on activity, timer, signal, child and selection; one entry, original identities, typed cause V2TimeoutCleanupTest, with signal and selection added in #1312; native vectors and extended cases (server#1363, #1373)
    Compensation success, failure, delayed watchdog, duplicate delivery, SIGKILL/fresh-process replay, expiry under the original cap #1312 (a real SIGKILL of a separate worker, replayed by a fresh process, ending completed or expired); the installed 2.42.0 receipt; native vectors 1–6
    Terminate/timeout/completion races; late results can't commit; still timed out after cleanup #1312 (completion in the turn that notices the deadline; late signals and activity results never apply); preemption vectors
    Budget relationships and diagnostics The contract (docs/architecture/timeout-cleanup.md, including #1304 and #1314); describe, run detail and observation (2.40.0, server#1344); the run-list summary (#1313, server#1379, per the decision above)
    Service-mode PHP, Python and Rust before portable support Server 3.30.0 with PHP 3.32.0, Python 2.24.0 and Rust 4.25.0 at 110/110 each (server#1363), plus the extended cases (server#1373)
    Exact published packages and images qualified; user journey documented Installed acceptance of published Workflow 2.42.0 (comment above); the native 3.30.0 tuple; the guide updated in docs#626 for embedded and service mode in 7 locales (no longer says service starts refuse it, and documents the marker rule, mixed queues and the "completed doesn't prove cleanup ran" caveat)

    Unreleased: the run-list summary field (#1313 embedded, server#1379 native) and the contract caveat (#1314) ship in the next Workflow and Server releases. Embedded summaries for runs that closed before the upgrade stay null until workflow:v2:rebuild-projections.

    Follow-ups, outside this issue:

    Closing.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:featurepriority:P2status:in-progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions