Make generic run-control mutations atomic (workflow cancel/finish race) - #492
Merged
chubes4 merged 8 commits intoAug 26, 2026
Merged
Conversation
The generic lifecycle mutations on WP_Agent_Run_Control (start_run, save_run, finish_run, request_cancel) performed a non-atomic state() -> mutate -> save_state() read-modify-write with no locking. These generic methods back workflow run-control (WP_Agent_Workflow_Runner, WP_Agent_Workflow_Request_Controller) and runtime-package runs, so a workflow request_cancel (register-agents-workflow-abilities.php) could race the runner's finish_run (class-wp-agent-workflow-runner.php) on the same store and run_id: both read the same base state and the second save silently overwrites the first, losing the cancel or the terminal status. Route all four mutations through the store's atomic read-modify-write path (mutate_state / mutate_workspace_state) via a new private mutate_run_state() helper, closing the lost-update window. This mirrors commit 8980da5, which hardened the chat layer (WP_Agent_Chat_Run_Control) to the atomic path but left these generic methods non-atomic. Stores without atomic support, and environments without a database connection (pure-PHP execution before WordPress boots), keep their historical non-atomic behavior, matching the chat layer's wpdb-availability guard. Adds tests/run-control-atomic-mutations-smoke.php, which uses a store spy to assert the four mutations take the atomic mutate path (not plain save_state) and that an interleaved concurrent write is not lost. The test fails without the fix and passes with it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…n-control # Conflicts: # composer.json
chubes4
marked this pull request as ready for review
August 26, 2026 21:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make generic run-control lifecycle mutations atomic and route every terminal producer through its authoritative stored winner.
Changes
skippedsupport in generic run-control so skipped runtime-package results remain terminal and replay without handler re-execution.budget_exceeded,stalled, andinterruptedwinners map to honest failed workflow results with stable error codes.Testing
tests/run-control-atomic-mutations-smoke.php: 56 assertions pass.tests/runtime-package-run-contract-smoke.php: 71 assertions pass, including skipped storage/replay and all error-exit races.tests/workflow-runner-smoke.php: 100 assertions pass, including generic terminal winners, recorder-start completion, and checked resume-failure idempotency.tests/workflow-as-branch-smoke.php: 141 assertions pass, including reconcile-recovery winner parity.composer test: pass.composer phpstan: no errors.git diff --check: pass.Current
origin/mainremainsd269bc5; no additional mainline merge was needed and no history was rewritten.