Skip to content

Prove embedded timeout cleanup at signal/selection waits, under worker loss, and on the deadline turn - #1312

Merged
rmcdaniel merged 1 commit into
mainfrom
general/workflow-863-embedded-cleanup-proofs
Nov 26, 2026
Merged

rmcdaniel merged 1 commit into
mainfrom
general/workflow-863-embedded-cleanup-proofs

Conversation

@rmcdaniel

Copy link
Copy Markdown
Member

Adds the missing embedded-execution proofs for deadline-triggered timeout cleanup, for #863. The change is tests only and touches no guarded source, so the regression-corpus validator passes without a new fixture. All new tests are in tests/Feature/V2/V2TimeoutCleanupTest.php.

1. Expiry while waiting on a signal (testExpiryWhileWaitingOnASignalInterruptsTheWaitAndALateSignalNeverApplies)

  • The run is blocked on await('approve') at its work deadline. The test asserts exactly one WorkflowTimeoutCleanupStarted and one CooperativeCancellationRequested, and exactly one CooperativeCancellationDelivered at the signal wait's sequence (call_kind: signal).
  • Workflow code records the interruption it receives in its memo. That record shows WorkflowTimeoutCleanupRequested with cause: timeout, a request id equal to the cleanup event's command id, the same root instance and run (still one run), no parent request, and the original cleanup deadline.
  • A signal sent during cleanup is accepted. It is never applied, even when its own workflow task runs before cleanup finishes. At close it gets SignalRejected with run_closed_before_signal_applied.
  • The run ends WorkflowTimedOut with cleanup outcome completed. There is no WorkflowCompleted and the output is null. A signal sent after close is rejected.

2. Expiry while waiting on a selection (testExpiryWhileWaitingOnASelectionCancelsEveryBranchAndNoBranchCommits)

  • The run is blocked on select() over a signal wait and an activity that a worker is already running. It gets the same once-only and identity assertions, with delivery at the selection (call_kind: parallel).
  • The running branch gets ActivityCancelled under the timeout request. Its worker's late heartbeat returns can_continue: false and cancel_requested: true, and its late completion is not recorded.
  • A late signal never resolves the selection. There is no SelectionResolved and no SignalApplied, and the work activity has no ActivityCompleted. The run ends timed out with cleanup outcome completed.

3. Kill-and-replay during cleanup (testAWorkerKilledMidCleanupIsReplacedFromHistoryWithoutRestartingCleanup, two cases)

  • This uses the repo's real process-loss pattern (WorkerLossControl, with the new worker tests/Support/TimeoutCleanupProcessLossProcessTest.php). A separate PHP worker process runs the cleanup turn and dies by SIGKILL while its workflow task is writing the compensation memo, before that write commits. The parent checks the turn is still leased by the dead worker, the run is open, and nothing from that turn committed.
  • A fresh process starts with no in-memory state. The production watchdog repairs the lease, and the run finishes from persisted history.
  • Assertions: the same single WorkflowTimeoutCleanupStarted, an unchanged cleanup deadline equal to the original hard deadline, and one delivery. The compensation activity is scheduled and completed once, so it is replayed rather than repeated.
  • If the fresh process resumes inside the window, the outcome is completed and the memo is written. If it first runs after the hard deadline, the outcome is expired and there is no memo.
  • The test is skipped only without POSIX signals or with an in-memory database, as the existing process-loss tests are.

4. Completion in the task that notices the work deadline (testCompletingInTheTaskThatNoticesTheWorkDeadlineEndsTimedOut, timer and activity cases, plus testAnActivityResultRecordedAfterCleanupStartsIsDiscardedForTheInterruption)

  • A timer fires at the work deadline, or an activity completes after it, before anything detects the deadline. The workflow would return in the same task in which the engine enters cleanup.
  • Cleanup is entered once, in that task. The run ends WorkflowTimedOut with outcome completed and the original deadline. The business result is discarded: no WorkflowCompleted and a null output.
  • No interruption is delivered, so the cleanup branch never runs. A result recorded before the request wins cooperative delivery, as with any cooperative cancellation. The test pins this behavior.
  • Contrast case: an activity result recorded after WorkflowTimeoutCleanupStarted is discarded, and the interruption is delivered at that activity call instead.

I checked locally that the new tests fail when the behavior regresses. Skipping signal or selection delivery fails proofs 1 and 2. Letting a run with a pending timeout request complete normally fails proofs 1, 2 and 4. Delivering over an earlier-recorded result fails proof 4. Allowing cleanup re-entry fails proof 3.

🤖 Generated with Claude Code

…orker loss, and on the deadline turn

Adds the missing embedded-execution proofs for deadline-triggered timeout
cleanup (#863):

- expiry while blocked on a signal wait: cleanup entered once, the typed
  WorkflowTimeoutCleanupRequested (cause: timeout) reaches the wait with the
  original request, instance and run identities, and a signal sent during
  cleanup is never applied and is rejected when the run closes;
- expiry while blocked on a select() of a signal wait and a running activity:
  the interruption is delivered at the selection, the running branch is
  cancelled under the timeout request and its late heartbeat and result are
  refused, the signal branch never resolves the selection;
- a worker process holding the cleanup turn dies by SIGKILL before the turn
  commits, and a fresh process repairs the lease and finishes from history:
  one WorkflowTimeoutCleanupStarted, the original cleanup deadline, the
  compensation replayed rather than repeated, outcome completed inside the
  window or expired after the hard deadline;
- a timer or activity result recorded at or after the work deadline, before
  detection, lets the workflow return in the task that notices the deadline:
  cleanup is entered once, the run ends timed out with outcome completed and
  the business result discarded; a result recorded after cleanup started is
  discarded for the interruption instead.

Tests only; no guarded source changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmcdaniel rmcdaniel added the agent:general Work owned by General label Nov 26, 2026
@sonarqubecloud

Copy link
Copy Markdown

@rmcdaniel

Copy link
Copy Markdown
Member Author

Independent review: PASS at 3848b5cdfa4ccb862ce23717460f47766b35a505. The reviewer saw only this PR, #863 and the contract.

  • Each proof can fail if behaviour regresses:
    • Signal wait: delivery is checked at the wait's own sequence. The late signal's task runs while cleanup is open and never applies.
    • Selection: the activity branch is really claimed before the deadline, then cancelled under the timeout request. Its late heartbeat is told to stop and its late result is not recorded.
    • SIGKILL: a separate process is killed mid-task, before the memo write commits. A fresh process finishes the run with one entry, the original deadline and a single compensation activity. Both completed and expired are covered.
    • Same-turn completion: cleanup is entered once in that task, with no WorkflowCompleted and a null output.
  • Process-loss helper: safe. It reuses WorkerLossControl, the child kills only itself, a finally cleans up, and the clocks are fixed. CI's MySQL job ran all 26 tests in the file, none skipped.
  • Findings:
    1. A completed outcome doesn't guarantee cleanup code ran, when the result was already in history. This is consistent with the contract table, but not stated there; it is being written into the contract.
    2. A late signal's SignalReceived/SignalRejected carry the interrupted wait's signal_wait_id. This affects diagnostics only and will be followed up separately.

🤖 Generated with Claude Code

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

Labels

agent:general Work owned by General

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant