Skip to content

Bound deadline-close child cascades and back off failing closes - #1292

Merged
rmcdaniel merged 9 commits into
mainfrom
general/workflow-1290
Nov 26, 2026
Merged

rmcdaniel merged 9 commits into
mainfrom
general/workflow-1290

Conversation

@rmcdaniel

@rmcdaniel rmcdaniel commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #1290 (items 1–4; item 5 is unchanged, see below).

Why

The bounded deadline close (#1116) counted every item as one history event and five rows. That undercounted in two ways:

  • A child closed by its parent's policy runs its whole close in the parent's transaction: its open work, accepted updates and own children. Up to a thousand of them could land in one transaction (item 1).
  • A failed accepted update writes two events and eleven rows. A close could write about twice the event limit (item 3).

Separately, a failing ordinary close was retried with no backoff and stayed at the front of the oldest-deadline scan, so 25 of them starved every other expired run in scope (item 2).

What changes

Charged by what it writes (items 1 and 3). Each close, batch and settlement transaction now has a DeadlineCloseBudget: the limit's events (history_transaction_size) and rows (completion_rows). Every kind of work is charged the events and rows it writes, including projections:

Work Events Rows
The close itself 1 10
A pass that does not close the run 0 6
A cancelled activity with its task 1 7
A cancelled timer 1 6
Another open task 0 2
A failed accepted update 2 11
A child closed by terminate / request_cancel, before its own work 3 19
A child asked to cancel (request_cancellation) 3 17

A terminated child is charged its own open activities, timers and tasks, its accepted updates, and recursively the children its own policy closes. The child's counts come from one withCount query. Counting stops once the total no longer fits.

  • Work is taken in the same order as before while it fits.
  • A batch or settlement pass always takes its first item, even one larger than the budget, so a close always finishes.
  • The close transaction takes cascade work only when it fits.
  • A run with no open work left always closes, even under a budget smaller than the close.
  • The history of a batched close is unchanged: it equals the single close.

Backoff for a failing ordinary close (item 2). When the watchdog's close of an ordinary expired run fails, the close rolls back. In its own transaction the watchdog then updates the workflow task it closed with:

  • last_error holds the error;
  • attempt_count goes up by one;
  • the new payload field deadline_close_failures goes up by one;
  • available_at moves out by the settlement's backoff (10 s, doubling, capped at 1 h).

The deadline scan skips the run until the task is due. A task held for a settlement or under a live lease is never touched.

MySQL projection rewrite. On MySQL every run projection rewrote each of the run's lineage rows, so every pass wrote one more row per child link. Cause: MySQL reorders JSON keys, and Eloquent compares decoded arrays with ===. IdempotentProjectionUpsert now keeps a stored JSON value that differs only in object key order. List order still counts as a change.

No operator release (item 4). This is a documented decision, not a command (see docs/architecture/completion-budget.md). Releasing a held settlement would leave accepted updates waiting forever and leave children running that the policy closes. A failing pass is retried with backoff and stays visible in metrics, queue visibility and describe.

WorkflowExecutor::deadlineCloseBatchSize() is removed. Nothing in the org calls it, and the budget replaces it.

Decisions for review

  1. A single item larger than the budget is taken alone. In practice that is a child whose own terminate is huge; with default limits it needs about 5,000 open items. Bounding it would need a bounded terminate command. Recommendation: accept.
  2. No operator release command. Recommendation: keep it that way. If operators need it, a guarded "retry now" command that clears the backoff after the cause is fixed is a small, safe follow-up.
  3. The projection key-order fix is included here. The row bound does not hold on MySQL without it. It only skips writes whose stored JSON is equal apart from key order, and it also removes O(links) rewrites from ordinary MySQL projections. Recommendation: keep it in this PR. It can be split out if preferred.
  4. Backoff covers only ordinary closes, as the issue scopes it. A failing cleanup-deadline or cooperative-cancellation close is unchanged. Recommendation: accept, and follow up only if one is seen.
  5. Cache-store writes are not charged. On a database cache store sharing the workflow connection, each event or task change also writes about one long-poll wake row. Recommendation: accept, because those writes depend on deployment configuration and are not workflow storage.
  6. Item 5 (opposite lock order) is unchanged. It is low severity: the database resolves the deadlock and the pass retries. It is not in the done-when.

Tests

tests/Unit/V2/CompletionWriteFamilyBudgetTest.php. All performance assertions are statement budgets measured from the SQL itself. None are wall-clock limits.

  • testEachDeadlineCloseItemIsChargedWhatItWrites (11 kinds): each kind writes no more than its charge, both in the close and in a pass that takes it alone. The charge is also within one row of what the item writes in the close.
  • testEveryTransactionOfABoundedCloseStaysWithinTheLimits (event limit, row limit, both): a parent with every kind of work. Every transaction stays within the limits, and the history equals the single close.
  • testAChildIsChargedItsWholeTerminate: four children with five activities each take two transactions, where they all used to land in the close.
  • testABoundedCloseSettlesUpdatesAndChildrenAcrossPassesWithTheSingleCloseHistory: reworked to a 201-event limit. The close fails 100 updates, then the passes go 100, 100 updates and then 67, 67, 66 children.
  • testAFailingOrdinaryCloseBacksOffAndCannotStarveOtherCloses: covers the backoff, the doubling and the eventual close.
  • IdempotentProjectionUpsertTest::testPrefetchedRowKeepsAPayloadThatDiffersOnlyInKeyOrder.

V2EmbeddedReplayRegressionCorpusTest::testDeadlineCloseCorpusChargesAChildItsTerminate uses the new replay evidence deadline-close-child-charged-its-terminate.json. A terminate-policy child under a 20-row limit is terminated in a later transaction than WorkflowTimedOut, by the settlement. I checked locally that this test fails on the base revision, where the close terminates the child in its own transaction.

The statement measurement leaves out writes to Laravel's cache and cache_locks tables. Long-poll wake signals land there only when an application uses the database cache store on the same connection. Run fixtures are projected first, as a real run would be.

CI adds a MySQL step to pr-feature-mysql for these cases. It took 52 s on the first run.

Local runs, in Docker with PHP 8.3:

  • ECS and PHPStan: clean on the changed files.
  • test-build-routing.py, verify-feature-shards.php, validate-regression-corpus.py --skip-counterfactual, test-regression-corpus-policy.py and check-public-boundary.sh: pass.
  • CompletionWriteFamilyBudgetTest and IdempotentProjectionUpsertTest: pass on SQLite and PostgreSQL 16 (62 tests). The 20 deadline-close cases pass on MySQL 8.0.
  • The new corpus test passes on a SQLite file database.

🤖 Generated with Claude Code

A bounded deadline close (#1116) counted every item as one history event
and five rows. A child's termination runs that child's whole close in the
parent's transaction, and a failed accepted update writes two events and
eleven rows, so one close transaction could write about twice the event
limit and up to a thousand child terminates.

Each transaction now has a DeadlineCloseBudget of the limit's events and
rows, and every kind of work is charged what it writes with its
projections: a child is charged its own open work, accepted updates and
the children its own policy closes. Work is taken while it fits; a batch
or settlement pass takes at least its first item, and a run with no open
work left always closes.

A failing ordinary close records its error, attempt and the settlement's
backoff on the run's workflow task, and the watchdog skips the run until
it is due, so failing closes no longer starve other expired runs.

A run projection keeps a stored JSON value that differs only in key
order. MySQL reorders JSON keys, so every projection rewrote each lineage
row and a pass wrote one more row per child link.

There is deliberately no operator release for a held settlement; the
completion budget guide records why.

Fixes #1290

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmcdaniel rmcdaniel added the agent:general Work owned by General label Nov 25, 2026
rmcdaniel and others added 3 commits November 25, 2026 18:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A terminate-policy child under a 20-row limit is terminated by the
settlement, not by the close transaction, and the parent keeps the single
close's history. On the base revision the close terminates it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A long-poll wake signal lands in Laravel's cache tables only when the
application uses the database cache store on the same connection, so the
statement budget tests leave those tables out. The child-cost fixture
projects its runs first, as a real run is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rmcdaniel and others added 3 commits November 25, 2026 21:05
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A timed-out child's close still resumes its live parent in the same
transaction; the changelog and guide no longer claim every close stays
within the limits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmcdaniel

Copy link
Copy Markdown
Member Author

Independent review: PASS at 7733469b01ab0030b445a13cb458d61d6618e11a

The reviewer worked in a fresh context and reviewed this PR's own changes against main, not my reasoning.

PASS at 5e691a1d04ce4d4cfd94755b63af3deb4c800f83, nothing blocking. The reviewer verified:

  • Termination: batch and settlement passes take their first item; a run with no open work always closes; the close transaction never forces cascade work; retries rebuild the budget.
  • Correctness: no work is lost or duplicated. Link order is kept, settled children are skipped, and the history equals the single close.
  • Disabled limits: with the limits off, the paths are unchanged.
  • Portability: withCount (orderings are cleared) and whereNotNull('payload->…') work on SQLite, MySQL and PostgreSQL.
  • Locking: recordCloseFailure respects the lock and guard scope.
  • Abuse: cyclic or deep child trees and a failing run's backoff are both bounded.
  • Key-order fix: the projection change is confined to dirty JSON casts on the prefetched path, and list order still counts.

Should-fix #1 is resolved. The reviewer found that a timed-out child's close also resumes its live parent in the same transaction, and those writes are not charged. The behaviour predates this PR, but the CHANGELOG claimed every close stays within the limits. The docs-only delta 5e691a1d..7733469b narrows the claim in CHANGELOG.md and docs/architecture/completion-budget.md, and the reviewer gave it PASS at 7733469b.

Nits left for follow-up:

  • the child's open-work count is read without the child lock (a rare race);
  • deadline_close_failures never resets after a successful pass;
  • repair can clear last_error between backoff attempts;
  • edge cases of the key-order fix: {} vs [], and the non-prefetched updateOrCreate paths;
  • fits() refuses a zero-cost dimension that is already overdrawn;
  • missing tests for recordCloseFailure skipping held, live-leased and non-workflow tasks.

🤖 Generated with Claude Code

@rmcdaniel
rmcdaniel enabled auto-merge November 26, 2026 03:57
@sonarqubecloud

Copy link
Copy Markdown

@rmcdaniel
rmcdaniel merged commit 12fc6b7 into main Nov 26, 2026
49 checks passed
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.

Bound the cost of deadline-close child cascades and back off failing closes

1 participant