From 971d5efadffb5eedf466dae4e6f9edc0626b492c Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:44:29 +0000 Subject: [PATCH 1/3] Carry the timeout cleanup outcome in the run summary Run lists now show what describe returns under run.timeout_cleanup (#863). The run summary gains a nullable timeout_cleanup JSON column, written from TimeoutCleanup::observe() whenever the whole run is projected. Cleanup entry and every close project the whole run, so the summary holds outcome running while cleanup is open and the recorded completed, failed, expired or preempted outcome after close; it is null for ordinary, armed, not-entered and completed runs. Bounded projections leave the column as it is. The model casts the column, so Waterline's serialized summaries carry it, and RunListItemView returns timeout_cleanup (null when absent) in its declared field list. The migration follows the task_attention column: outside the migrator transaction, under the schema lock budget, repeatable, no backfill. The additive-column rollback guard now covers both summary columns: the cached column check is shared, and a write that hits a dropped column retries without either one. Co-Authored-By: Claude Opus 5.5 --- .github/feature-test-timings.json | 2 + CHANGELOG.md | 10 +++ docs/architecture/timeout-cleanup.md | 11 +++ src/V2/Models/WorkflowRunSummary.php | 1 + src/V2/Support/RunListItemView.php | 7 ++ src/V2/Support/RunSummaryProjector.php | 3 + src/V2/Support/TimeoutCleanup.php | 24 ++++++ src/V2/Support/WorkflowTaskLeaseLoss.php | 30 +++++-- ...eout_cleanup_to_workflow_run_summaries.php | 62 +++++++++++++++ .../V2TimeoutCleanupSummaryMigrationTest.php | 78 +++++++++++++++++++ tests/Feature/V2/V2TimeoutCleanupTest.php | 57 ++++++++++++++ 11 files changed, 277 insertions(+), 8 deletions(-) create mode 100644 src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php create mode 100644 tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php diff --git a/.github/feature-test-timings.json b/.github/feature-test-timings.json index 574aaa79f..2e1d17612 100644 --- a/.github/feature-test-timings.json +++ b/.github/feature-test-timings.json @@ -228,6 +228,7 @@ "tests/Feature/V2/V2TerminalReasonDiagnosticsTest.php": 23.591816, "tests/Feature/V2/V2TimeoutCleanupAdmissionTest.php": 4.969863, "tests/Feature/V2/V2TimeoutCleanupMigrationTest.php": 10.047079, + "tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php": 10.047079, "tests/Feature/V2/V2TimeoutCleanupTest.php": 22.127049, "tests/Feature/V2/V2TimerDeadlineMigrationTest.php": 35.432759, "tests/Feature/V2/V2TimerWorkflowTest.php": 12.113366, @@ -498,6 +499,7 @@ "tests/Feature/V2/V2TerminalReasonDiagnosticsTest.php": 1.951918, "tests/Feature/V2/V2TimeoutCleanupAdmissionTest.php": 7.510203, "tests/Feature/V2/V2TimeoutCleanupMigrationTest.php": 2.120066, + "tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php": 2.120066, "tests/Feature/V2/V2TimeoutCleanupTest.php": 15.088411, "tests/Feature/V2/V2TimerDeadlineMigrationTest.php": 7.377256, "tests/Feature/V2/V2TimerWorkflowTest.php": 12.996575, diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ecc0e627..071350c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ ## Unreleased +- Run lists show the timeout cleanup outcome (#863). The run summary gains a + nullable `timeout_cleanup` JSON column holding what describe returns under + `run.timeout_cleanup` (`outcome: running` while cleanup is open, then the + recorded `completed`, `failed`, `expired` or `preempted` outcome), and + `RunListItemView` returns it, null when no cleanup was recorded. Migration + `2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries` adds the + column outside the migrator transaction under the schema lock budget, with + no backfill. To fill it for opted-in runs that closed before the upgrade, + rebuild them with `workflow:v2:rebuild-projections --run-id=...`. + ## 2.42.0 - 2026-11-26 2.42.0 has no database migration. It adds a replay-aware workflow logger, diff --git a/docs/architecture/timeout-cleanup.md b/docs/architecture/timeout-cleanup.md index b57bdd669..47bf0957b 100644 --- a/docs/architecture/timeout-cleanup.md +++ b/docs/architecture/timeout-cleanup.md @@ -160,6 +160,17 @@ history export. When no cleanup was recorded, the response omits the field. An expired outcome can omit `started_at` if the first detection was already past the hard deadline. +Run lists carry the same value. The run summary (`workflow_run_summaries`) +stores it in a nullable `timeout_cleanup` JSON column, written whenever the +whole run is projected, which includes cleanup entry and every close, so +Waterline's run list and `RunListItemView` return `timeout_cleanup`, null +when no cleanup was recorded. The column is added without a backfill. A +summary projected before the upgrade gains the value the next time its run is +projected; an already closed opted-in run (`workflow_runs` with +`timeout_cleanup_work_deadline_at` set) keeps null until +`workflow:v2:rebuild-projections --run-id=...` rebuilds it. Describe, run +detail and the observation read history and are correct either way. + ## Defaults and compatibility - Without `withTimeoutCleanup()`, nothing changes: the hard timeout closes diff --git a/src/V2/Models/WorkflowRunSummary.php b/src/V2/Models/WorkflowRunSummary.php index 9b7d7bf1a..d1f384d3d 100644 --- a/src/V2/Models/WorkflowRunSummary.php +++ b/src/V2/Models/WorkflowRunSummary.php @@ -43,6 +43,7 @@ class WorkflowRunSummary extends Model 'is_current_run' => 'bool', 'repair_attention' => 'bool', 'task_attention' => 'array', + 'timeout_cleanup' => 'array', 'task_problem' => 'bool', 'visibility_labels' => 'array', 'projection_schema_version' => 'integer', diff --git a/src/V2/Support/RunListItemView.php b/src/V2/Support/RunListItemView.php index 751a78402..7fd661f95 100644 --- a/src/V2/Support/RunListItemView.php +++ b/src/V2/Support/RunListItemView.php @@ -79,6 +79,12 @@ public static function fromSummary(WorkflowRunSummary $summary): array 'task_attention' => array_key_exists('task_attention', $summary->getAttributes()) ? $summary->task_attention : null, + // Additive (#863): the timeout cleanup state describe returns + // under run.timeout_cleanup; null when none was recorded. + 'timeout_cleanup' => array_key_exists('timeout_cleanup', $summary->getAttributes()) + && is_array($summary->timeout_cleanup) + ? $summary->timeout_cleanup + : null, 'task_problem' => (bool) $summary->task_problem, 'task_problem_badge' => WorkflowTaskProblem::metadata( (bool) $summary->task_problem, @@ -149,6 +155,7 @@ public static function fields(): array 'repair_blocked_reason', 'repair_blocked', 'task_attention', + 'timeout_cleanup', 'task_problem', 'task_problem_badge', 'declared_entry_mode', diff --git a/src/V2/Support/RunSummaryProjector.php b/src/V2/Support/RunSummaryProjector.php index 01f0b0bb6..82fadd47d 100644 --- a/src/V2/Support/RunSummaryProjector.php +++ b/src/V2/Support/RunSummaryProjector.php @@ -561,6 +561,9 @@ public static function project(WorkflowRun $run, bool $disposableRun = false): W 'repair_blocked_reason' => $repairBlockedReason, 'repair_attention' => RepairBlockedReason::needsAttention($repairBlockedReason), ...WorkflowTaskLeaseLoss::summaryAttributes($run), + // Cleanup is entered and closed only by transitions that + // project the whole run, so bounded projections keep it. + ...TimeoutCleanup::summaryAttributes($run), 'task_problem' => $taskProblem, 'exception_count' => count($failureSnapshots), 'history_event_count' => $historyBudget['history_event_count'], diff --git a/src/V2/Support/TimeoutCleanup.php b/src/V2/Support/TimeoutCleanup.php index 142148f72..039d6518c 100644 --- a/src/V2/Support/TimeoutCleanup.php +++ b/src/V2/Support/TimeoutCleanup.php @@ -257,6 +257,30 @@ public static function observe(WorkflowRun $run, mixed $startedPayload): ?array ])), is_string(...)); } + /** + * The run summary's additive `timeout_cleanup` attribute: what `observe` + * returns, so run lists match describe and the run observation. None + * when the summary table has no such column. + * + * @return array{timeout_cleanup?: array|null} + */ + public static function summaryAttributes(WorkflowRun $run): array + { + if (! WorkflowTaskLeaseLoss::summaryHasColumn(self::STARTED_KEY)) { + return []; + } + + $started = $run->relationLoaded('historyEvents') + ? $run->historyEvents->first( + static fn (WorkflowHistoryEvent $event): bool => $event->event_type === HistoryEventType::WorkflowStarted + ) + : null; + + return [ + self::STARTED_KEY => self::observe($run, $started?->payload), + ]; + } + /** * @return array{0: string|null, 1: CarbonInterface|null} */ diff --git a/src/V2/Support/WorkflowTaskLeaseLoss.php b/src/V2/Support/WorkflowTaskLeaseLoss.php index 8adb28f72..64be4fc78 100644 --- a/src/V2/Support/WorkflowTaskLeaseLoss.php +++ b/src/V2/Support/WorkflowTaskLeaseLoss.php @@ -50,6 +50,12 @@ final class WorkflowTaskLeaseLoss */ private const SCHEMA_RECHECK_SECONDS = 60; + /** + * Run-summary columns added after the table, which a schema rollback can + * remove again. + */ + private const ADDITIVE_SUMMARY_COLUMNS = ['task_attention', 'timeout_cleanup']; + /** * @var array */ @@ -284,7 +290,16 @@ public static function tracked(bool $fresh = false): bool */ public static function summaryTracked(bool $fresh = false): bool { - return self::hasColumn(self::summaryModel(), 'task_attention', $fresh); + return self::summaryHasColumn('task_attention', $fresh); + } + + /** + * Whether the configured run-summary table has an additive column, with + * the same cached, rollback-aware check as `task_attention`. + */ + public static function summaryHasColumn(string $column, bool $fresh = false): bool + { + return self::hasColumn(self::summaryModel(), $column, $fresh); } /** @@ -317,11 +332,12 @@ public static function untrackedOnMissingColumn(Model $model, callable $query): } /** - * Write a run summary whose values may carry task_attention. + * Write a run summary whose values may carry an additive column + * (`task_attention`, `timeout_cleanup`). * - * A schema rollback can drop the column while the cached check still + * A schema rollback can drop such a column while the cached check still * reports it. On that error the cache is forgotten and $write runs again - * without task_attention. A failed statement aborts a caller's open + * without the additive columns. A failed statement aborts a caller's open * PostgreSQL transaction, so there the error is rethrown instead and the * caller's retry sees the current schema. * @@ -332,7 +348,7 @@ public static function untrackedOnMissingColumn(Model $model, callable $query): */ public static function writeSummary(array $values, callable $write): mixed { - if (! array_key_exists('task_attention', $values)) { + if (array_intersect_key($values, array_flip(self::ADDITIVE_SUMMARY_COLUMNS)) === []) { return $write($values); } @@ -350,9 +366,7 @@ public static function writeSummary(array $values, callable $write): mixed throw $exception; } - unset($values['task_attention']); - - return $write($values); + return $write(array_diff_key($values, array_flip(self::ADDITIVE_SUMMARY_COLUMNS))); } } diff --git a/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php b/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php new file mode 100644 index 000000000..986f28b72 --- /dev/null +++ b/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php @@ -0,0 +1,62 @@ +getConnection()); + + foreach (self::summaryTables() as $table) { + if ($schema->hasTable($table) && ! $schema->hasColumn($table, 'timeout_cleanup')) { + $this->withSchemaLockBudget(static function () use ($schema, $table): void { + $schema->table($table, static function (Blueprint $blueprint): void { + $blueprint->json('timeout_cleanup') + ->nullable(); + }); + }); + } + } + } + + public function down(): void + { + $schema = Schema::connection($this->getConnection()); + + foreach (self::summaryTables() as $table) { + if ($schema->hasTable($table) && $schema->hasColumn($table, 'timeout_cleanup')) { + $schema->table($table, static function (Blueprint $blueprint): void { + $blueprint->dropColumn('timeout_cleanup'); + }); + } + } + } + + /** + * @return list + */ + private static function summaryTables(): array + { + $model = ConfiguredV2Models::resolve('run_summary_model', WorkflowRunSummary::class); + + return array_values(array_unique(['workflow_run_summaries', (new $model())->getTable()])); + } +}; diff --git a/tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php b/tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php new file mode 100644 index 000000000..86f66a735 --- /dev/null +++ b/tests/Feature/V2/V2TimeoutCleanupSummaryMigrationTest.php @@ -0,0 +1,78 @@ +migration(); + $this->assertFalse($migration->withinTransaction); + + $source = (string) file_get_contents(self::path()); + $this->assertSame(1, substr_count($source, 'withSchemaLockBudget')); + // No backfill: a summary gains the value when its run is projected. + $this->assertDoesNotMatchRegularExpression('/->\s*(update|insert|upsert|delete)\s*\(/', $source); + } + + public function testItAddsANullableColumnToAPopulatedTableAndCanRunAgain(): void + { + $migration = $this->migration(); + $migration->down(); + $this->assertFalse(Schema::hasColumn('workflow_run_summaries', 'timeout_cleanup')); + + $ids = []; + foreach (range(1, 5) as $number) { + $ids[] = $id = (string) Str::ulid(); + DB::table('workflow_run_summaries')->insert([ + 'id' => $id, + 'workflow_instance_id' => 'migration-instance-' . $number, + 'run_number' => 1, + 'class' => 'App\\Workflows\\Example', + 'workflow_type' => 'example', + 'status' => 'running', + 'status_bucket' => 'running', + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + $migration->up(); + // An interrupted or repeated run skips work already done. + $migration->up(); + + $this->assertTrue(Schema::hasColumn('workflow_run_summaries', 'timeout_cleanup')); + $this->assertSame( + 5, + DB::table('workflow_run_summaries') + ->whereIn('id', $ids) + ->whereNull('timeout_cleanup') + ->count(), + ); + } + + private function migration(): WorkflowMigration + { + return require self::path(); + } + + private static function path(): string + { + return dirname(__DIR__, 3) . '/src/migrations/' . self::NAME . '.php'; + } +} diff --git a/tests/Feature/V2/V2TimeoutCleanupTest.php b/tests/Feature/V2/V2TimeoutCleanupTest.php index c76996e56..dd8054657 100644 --- a/tests/Feature/V2/V2TimeoutCleanupTest.php +++ b/tests/Feature/V2/V2TimeoutCleanupTest.php @@ -4,7 +4,9 @@ namespace Tests\Feature\V2; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Queue; +use Illuminate\Support\Facades\Schema; use PHPUnit\Framework\Attributes\DataProvider; use Tests\Fixtures\V2\TestTimeoutCleanupWorkflow; use Tests\TestCase; @@ -18,8 +20,11 @@ use Workflow\V2\Jobs\RunWorkflowTask; use Workflow\V2\Models\WorkflowHistoryEvent; use Workflow\V2\Models\WorkflowRun; +use Workflow\V2\Models\WorkflowRunSummary; use Workflow\V2\Models\WorkflowTask; use Workflow\V2\StartOptions; +use Workflow\V2\Support\RunSummaryProjector; +use Workflow\V2\Support\WorkflowTaskLeaseLoss; use Workflow\V2\TaskWatchdog; use Workflow\V2\WorkflowStub; @@ -59,6 +64,8 @@ public function testAnIdleRunIsWokenAtItsWorkDeadlineAndEndsTimedOut( $runId = $workflow->runId(); $this->runReadyWorkflowTask($runId); $this->assertSame(RunStatus::Waiting, WorkflowRun::query()->findOrFail($runId)->status); + // Armed, not entered: nothing to report yet. + $this->assertVisibleTimeoutCleanup(WorkflowRun::query()->findOrFail($runId), null); // Past the work deadline, before the hard deadline: the watchdog wakes it. $this->travel(61) @@ -496,6 +503,37 @@ public function testUnavailableWorkersCannotKeepAnOptedInRunOpen(bool $lateDetec $this->assertSame($count, WorkflowHistoryEvent::query()->where('workflow_run_id', $runId)->count()); } + public function testASummaryColumnRollbackKeepsProjectingWithoutTheListValue(): void + { + [, $run] = $this->enterCleanupAt(61); + $this->assertTrue(WorkflowTaskLeaseLoss::summaryHasColumn('timeout_cleanup')); + + // The process still believes the summary column exists. + Schema::table('workflow_run_summaries', static function (Blueprint $table): void { + $table->dropColumn('timeout_cleanup'); + }); + + try { + $summary = RunSummaryProjector::project($run->refresh()); + $this->assertSame($run->id, $summary->id); + $this->assertFalse(WorkflowTaskLeaseLoss::summaryHasColumn('timeout_cleanup')); + $item = $this->app->make(OperatorObservabilityRepository::class) + ->listItem(WorkflowRunSummary::query()->findOrFail($run->id)); + $this->assertNull($item['timeout_cleanup']); + // Describe still reads history. + $this->assertSame('running', $this->app->make(WorkflowControlPlane::class) + ->describe($run->workflow_instance_id, [ + 'run_id' => $run->id, + ])['run']['timeout_cleanup']['outcome'] ?? null); + } finally { + Schema::table('workflow_run_summaries', static function (Blueprint $table): void { + $table->json('timeout_cleanup') + ->nullable(); + }); + WorkflowTaskLeaseLoss::summaryHasColumn('timeout_cleanup', true); + } + } + /** * @param array|null $expected */ @@ -515,6 +553,25 @@ private function assertVisibleTimeoutCleanup(WorkflowRun $run, ?array $expected) $this->assertSame($expected, $surface['timeout_cleanup'] ?? null); } } + + // Run lists: the projected summary (Waterline serializes the model) + // and the list item carry the same value, null when none. A JSON + // column may reorder keys, so only the members are compared. + $summary = WorkflowRunSummary::query()->findOrFail($run->id); + $item = $observer->listItem($summary); + $this->assertArrayHasKey('timeout_cleanup', $item); + foreach ([ + $summary->timeout_cleanup, + $summary->toArray()['timeout_cleanup'], + $item['timeout_cleanup'], + ] as $listed) { + if ($expected === null) { + $this->assertNull($listed); + } else { + $this->assertIsArray($listed); + $this->assertEquals($expected, $listed); + } + } } /** From 1344d9e913d983f62e973d65f5183970289b6f3b Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:53:50 +0000 Subject: [PATCH 2/3] Add replay evidence for the listed timeout cleanup outcome The summary projector and RunListItemView sit under the replay corpus guard, so the change carries a regression fixture. The corpus test starts an opted-in run, checks the list item is null while armed, outcome running after the watchdog enters cleanup, and the recorded completed outcome after the cleanup turn closes the run. The summary migration iterates tables that have or lack the column through one helper instead of repeating the lost-lease migration's loops. Co-Authored-By: Claude Opus 5.5 --- ...eout_cleanup_to_workflow_run_summaries.php | 51 ++++++++++------- .../V2EmbeddedReplayRegressionCorpusTest.php | 46 +++++++++++++++ .../timeout-cleanup-summary-listed.json | 57 +++++++++++++++++++ 3 files changed, 133 insertions(+), 21 deletions(-) create mode 100644 tests/Fixtures/V2/ReplayRegression/timeout-cleanup-summary-listed.json diff --git a/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php b/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php index 986f28b72..2860837d7 100644 --- a/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php +++ b/src/migrations/2026_11_26_000100_add_timeout_cleanup_to_workflow_run_summaries.php @@ -3,12 +3,15 @@ declare(strict_types=1); use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; use Illuminate\Support\Facades\Schema; use Workflow\Support\WorkflowMigration; use Workflow\V2\Models\WorkflowRunSummary; use Workflow\V2\Support\ConfiguredV2Models; return new class() extends WorkflowMigration { + private const COLUMN = 'timeout_cleanup'; + /** * Deadline-triggered cleanup (#863): the run summary's additive * `timeout_cleanup` field, the same portable shape describe and the run @@ -23,40 +26,46 @@ public function up(): void { - $schema = Schema::connection($this->getConnection()); - - foreach (self::summaryTables() as $table) { - if ($schema->hasTable($table) && ! $schema->hasColumn($table, 'timeout_cleanup')) { - $this->withSchemaLockBudget(static function () use ($schema, $table): void { - $schema->table($table, static function (Blueprint $blueprint): void { - $blueprint->json('timeout_cleanup') - ->nullable(); - }); - }); - } + $schema = $this->schema(); + + foreach ($this->summaryTables($schema, withColumn: false) as $table) { + $this->withSchemaLockBudget(static fn () => $schema->table( + $table, + static fn (Blueprint $blueprint) => $blueprint->json(self::COLUMN) + ->nullable(), + )); } } public function down(): void { - $schema = Schema::connection($this->getConnection()); - - foreach (self::summaryTables() as $table) { - if ($schema->hasTable($table) && $schema->hasColumn($table, 'timeout_cleanup')) { - $schema->table($table, static function (Blueprint $blueprint): void { - $blueprint->dropColumn('timeout_cleanup'); - }); - } + $schema = $this->schema(); + + foreach ($this->summaryTables($schema, withColumn: true) as $table) { + $schema->table($table, static fn (Blueprint $blueprint) => $blueprint->dropColumn(self::COLUMN)); } } + private function schema(): Builder + { + return Schema::connection($this->getConnection()); + } + /** + * The existing run-summary tables (the package table and a configured + * model's table) that have, or lack, the column. + * * @return list */ - private static function summaryTables(): array + private function summaryTables(Builder $schema, bool $withColumn): array { $model = ConfiguredV2Models::resolve('run_summary_model', WorkflowRunSummary::class); + $tables = array_unique(['workflow_run_summaries', (new $model())->getTable()]); - return array_values(array_unique(['workflow_run_summaries', (new $model())->getTable()])); + return array_values(array_filter( + $tables, + static fn (string $table): bool => $schema->hasTable($table) + && $schema->hasColumn($table, self::COLUMN) === $withColumn, + )); } }; diff --git a/tests/Feature/V2/V2EmbeddedReplayRegressionCorpusTest.php b/tests/Feature/V2/V2EmbeddedReplayRegressionCorpusTest.php index e859245d9..379d3562a 100644 --- a/tests/Feature/V2/V2EmbeddedReplayRegressionCorpusTest.php +++ b/tests/Feature/V2/V2EmbeddedReplayRegressionCorpusTest.php @@ -498,6 +498,47 @@ public function testTimeoutCleanupObservationCorpusKeepsTheRecordedClockAndOutco $this->assertEquals($scenario['terminal'], $observer->runDetail($run)['timeout_cleanup'] ?? null); } + public function testTimeoutCleanupSummaryCorpusListsTheRecordedOutcome(): void + { + $path = self::FIXTURE_DIR . '/timeout-cleanup-summary-listed.json'; + if (! is_file($path)) { + $this->markTestSkipped('The timeout cleanup summary evidence is absent.'); + } + $fixture = json_decode((string) file_get_contents($path), true, flags: JSON_THROW_ON_ERROR); + $scenario = $fixture['summary']; + config([ + 'queue.default' => 'database', + ]); + Queue::fake(); + $this->travelTo(Carbon::parse($fixture['history'][0]['recorded_at'])); + $stub = WorkflowStub::make($fixture['workflow']['type'], $fixture['id']); + $stub->start($fixture['workflow']['arguments'][0], new StartOptions( + executionTimeoutSeconds: $scenario['execution_timeout_seconds'], + timeoutCleanupSeconds: $scenario['timeout_cleanup_seconds'], + )); + $runId = $stub->runId(); + $this->runReadyTaskOfType($runId, TaskType::Workflow); + $observer = $this->app->make(OperatorObservabilityRepository::class); + $listed = static fn (): mixed => $observer->listItem( + WorkflowRunSummary::query()->findOrFail($runId) + )['timeout_cleanup']; + $this->assertNull($listed()); + + // The watchdog enters cleanup and projects the run. + $this->travel($scenario['detect_after_seconds'])->seconds(); + TaskWatchdog::runPass(runIds: [$runId]); + $this->assertEquals($scenario['running'], $listed()); + + // The cleanup turn returns; the close projects the recorded outcome. + $this->runReadyTaskOfType($runId, TaskType::Workflow); + $this->assertSame(RunStatus::Failed, WorkflowRun::query()->findOrFail($runId)->status); + $this->assertEquals($scenario['terminal'], $listed()); + $this->assertEquals( + $scenario['terminal'], + WorkflowRunSummary::query()->findOrFail($runId)->toArray()['timeout_cleanup'] + ); + } + public function testTimeoutCleanupTaskExpiryCorpusKeepsTheOutcome(): void { $path = self::FIXTURE_DIR . '/timeout-cleanup-task-expiry.json'; @@ -3303,6 +3344,11 @@ public function testFixturesExecuteThroughDeclaredReplayConsumers(): void continue; } + if ($fixture['id'] === 'timeout-cleanup-summary-listed') { + // The live projection at entry and close runs in the dedicated corpus test. + continue; + } + if ($fixture['id'] === 'version-usage-retained-reused-decision') { config()->set('app.key', 'version-usage-corpus-signing-key'); $this->assertVersionUsageRetainedDecision($fixture); diff --git a/tests/Fixtures/V2/ReplayRegression/timeout-cleanup-summary-listed.json b/tests/Fixtures/V2/ReplayRegression/timeout-cleanup-summary-listed.json new file mode 100644 index 000000000..67389789d --- /dev/null +++ b/tests/Fixtures/V2/ReplayRegression/timeout-cleanup-summary-listed.json @@ -0,0 +1,57 @@ +{ + "$schema": "https://raw.githubusercontent.com/durable-workflow/.github/main/regression-corpus/evidence-schema.json", + "fixture_schema": "durable-workflow.replay-regression/v1", + "id": "timeout-cleanup-summary-listed", + "protocol_version": "1.20", + "bindings": ["php"], + "consumers": ["workflow-fiber-runner"], + "workflow": { + "type": "Tests\\Fixtures\\V2\\TestTimeoutCleanupWorkflow", + "arguments": ["complete"], + "payload_codec": "avro" + }, + "history": [ + { + "sequence": 1, + "event_type": "WorkflowStarted", + "payload": { + "timeout_cleanup": { + "allowance_seconds": 10, + "work_deadlines": { + "execution": "2026-11-24T00:01:00+00:00" + } + } + }, + "recorded_at": "2026-11-24T00:00:00.000000Z" + }, + { + "sequence": 2, + "event_type": "TimerScheduled", + "payload": {"timer_id": "timeout-cleanup-summary-timer", "sequence": 1, "delay_seconds": 3600}, + "recorded_at": "2026-11-24T00:00:00.000000Z" + } + ], + "expected": {"completed": false, "result": null, "commands": []}, + "summary": { + "issue": "durable-workflow/workflow#863", + "execution_timeout_seconds": 70, + "timeout_cleanup_seconds": 10, + "detect_after_seconds": 61, + "running": { + "outcome": "running", + "cause": "timeout", + "timeout_kind": "execution", + "started_at": "2026-11-24T00:01:01+00:00", + "work_deadline_at": "2026-11-24T00:01:00+00:00", + "cleanup_deadline_at": "2026-11-24T00:01:10+00:00" + }, + "terminal": { + "outcome": "completed", + "timeout_kind": "execution", + "started_at": "2026-11-24T00:01:01+00:00", + "cleanup_deadline_at": "2026-11-24T00:01:10+00:00", + "finished_at": "2026-11-24T00:01:01+00:00" + }, + "note": "The run summary and list item carry what describe returns under run.timeout_cleanup: null while armed, outcome running from the projection at cleanup entry, then the recorded outcome from the projection at close." + } +} From c5a91d8ad5c47751d23099380bf1d75d0180c217 Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:12:10 +0000 Subject: [PATCH 3/3] Check listed cleanup outcomes on every timed-out cleanup path Review notes on the run-summary change: - assertTimedOutAfterCleanup, shared by the executor-entry, same-turn, signal, selection, late activity result and worker-loss tests, now also checks describe, observation, detail, the projected summary and the list item against the recorded outcome. - A cleanup turn that runs a compensation activity: after the activity outcome, the bounded open-workflow-task projection is the summary's last writer, and the listed value is still outcome running. - The summary column and list field have their own constant, TimeoutCleanup::SUMMARY_COLUMN, instead of reusing STARTED_KEY. Co-Authored-By: Claude Opus 5.5 --- src/V2/Support/RunListItemView.php | 6 +-- src/V2/Support/TimeoutCleanup.php | 11 ++++-- src/V2/Support/WorkflowTaskLeaseLoss.php | 2 +- tests/Feature/V2/V2TimeoutCleanupTest.php | 46 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/V2/Support/RunListItemView.php b/src/V2/Support/RunListItemView.php index 7fd661f95..bcb771dd5 100644 --- a/src/V2/Support/RunListItemView.php +++ b/src/V2/Support/RunListItemView.php @@ -81,9 +81,9 @@ public static function fromSummary(WorkflowRunSummary $summary): array : null, // Additive (#863): the timeout cleanup state describe returns // under run.timeout_cleanup; null when none was recorded. - 'timeout_cleanup' => array_key_exists('timeout_cleanup', $summary->getAttributes()) - && is_array($summary->timeout_cleanup) - ? $summary->timeout_cleanup + 'timeout_cleanup' => array_key_exists(TimeoutCleanup::SUMMARY_COLUMN, $summary->getAttributes()) + && is_array($summary->getAttribute(TimeoutCleanup::SUMMARY_COLUMN)) + ? $summary->getAttribute(TimeoutCleanup::SUMMARY_COLUMN) : null, 'task_problem' => (bool) $summary->task_problem, 'task_problem_badge' => WorkflowTaskProblem::metadata( diff --git a/src/V2/Support/TimeoutCleanup.php b/src/V2/Support/TimeoutCleanup.php index 039d6518c..654788de2 100644 --- a/src/V2/Support/TimeoutCleanup.php +++ b/src/V2/Support/TimeoutCleanup.php @@ -38,6 +38,11 @@ final class TimeoutCleanup */ public const STARTED_KEY = 'timeout_cleanup'; + /** + * The additive `workflow_run_summaries` column and list-item field. + */ + public const SUMMARY_COLUMN = 'timeout_cleanup'; + public static function assertEmbeddedAdmission(?int $allowanceSeconds): void { if ($allowanceSeconds !== null && config('workflows.v2.task_dispatch_mode') === 'poll') { @@ -262,11 +267,11 @@ public static function observe(WorkflowRun $run, mixed $startedPayload): ?array * returns, so run lists match describe and the run observation. None * when the summary table has no such column. * - * @return array{timeout_cleanup?: array|null} + * @return array|null> */ public static function summaryAttributes(WorkflowRun $run): array { - if (! WorkflowTaskLeaseLoss::summaryHasColumn(self::STARTED_KEY)) { + if (! WorkflowTaskLeaseLoss::summaryHasColumn(self::SUMMARY_COLUMN)) { return []; } @@ -277,7 +282,7 @@ public static function summaryAttributes(WorkflowRun $run): array : null; return [ - self::STARTED_KEY => self::observe($run, $started?->payload), + self::SUMMARY_COLUMN => self::observe($run, $started?->payload), ]; } diff --git a/src/V2/Support/WorkflowTaskLeaseLoss.php b/src/V2/Support/WorkflowTaskLeaseLoss.php index 64be4fc78..6cb55484d 100644 --- a/src/V2/Support/WorkflowTaskLeaseLoss.php +++ b/src/V2/Support/WorkflowTaskLeaseLoss.php @@ -54,7 +54,7 @@ final class WorkflowTaskLeaseLoss * Run-summary columns added after the table, which a schema rollback can * remove again. */ - private const ADDITIVE_SUMMARY_COLUMNS = ['task_attention', 'timeout_cleanup']; + private const ADDITIVE_SUMMARY_COLUMNS = ['task_attention', TimeoutCleanup::SUMMARY_COLUMN]; /** * @var array diff --git a/tests/Feature/V2/V2TimeoutCleanupTest.php b/tests/Feature/V2/V2TimeoutCleanupTest.php index 78d6bb5c9..72208675e 100644 --- a/tests/Feature/V2/V2TimeoutCleanupTest.php +++ b/tests/Feature/V2/V2TimeoutCleanupTest.php @@ -746,6 +746,50 @@ public function testAnActivityResultRecordedAfterCleanupStartsIsDiscardedForTheI $this->assertTimedOutAfterCleanup($workflow, $runId, $started, 'completed'); } + public function testABoundedProjectionAfterACleanupActivityKeepsTheListedValue(): void + { + $this->travelTo(now()->startOfSecond()); + $workflow = WorkflowStub::make(TestTimeoutCleanupCompensationWorkflow::class, 'timeout-cleanup-bounded'); + $workflow->start(new StartOptions(executionTimeoutSeconds: 70, timeoutCleanupSeconds: 10)); + $runId = $workflow->runId(); + $this->runReadyWorkflowTask($runId); + $this->travel(61) + ->seconds(); + TaskWatchdog::runPass(runIds: [$runId]); + // The cleanup turn schedules the compensation activity. + $this->runReadyWorkflowTask($runId); + $started = $this->soleEvent($runId, HistoryEventType::WorkflowTimeoutCleanupStarted); + $running = [ + 'outcome' => 'running', + 'cause' => 'timeout', + 'timeout_kind' => 'execution', + 'started_at' => $started->payload['detected_at'], + 'work_deadline_at' => $started->payload['work_deadline_at'], + 'cleanup_deadline_at' => $started->payload['cleanup_deadline_at'], + ]; + $this->assertVisibleTimeoutCleanup(WorkflowRun::query()->findOrFail($runId), $running); + $before = WorkflowRunSummary::query()->findOrFail($runId); + + // The activity outcome opens the resume workflow task, and the bounded + // open-workflow-task projection is the summary's last writer. It + // writes only the task, wait and history columns. + $this->runReadyActivityTask($runId); + $completed = $this->soleEvent($runId, HistoryEventType::ActivityCompleted); + $after = WorkflowRunSummary::query()->findOrFail($runId); + $resume = WorkflowTask::query() + ->where('workflow_run_id', $runId) + ->where('task_type', TaskType::Workflow->value) + ->where('status', TaskStatus::Ready->value) + ->sole(); + $this->assertSame($resume->id, $after->next_task_id); + $this->assertSame((int) $completed->sequence, (int) $after->history_event_count); + $this->assertGreaterThan((int) $before->history_event_count, (int) $after->history_event_count); + $this->assertVisibleTimeoutCleanup(WorkflowRun::query()->findOrFail($runId), $running); + + $this->runReadyWorkflowTask($runId); + $this->assertTimedOutAfterCleanup($workflow, $runId, $started, 'completed'); + } + /** * @return iterable */ @@ -909,6 +953,8 @@ private function assertTimedOutAfterCleanup( $started->payload['cleanup_deadline_at'], $terminal->payload['timeout_cleanup']['cleanup_deadline_at'], ); + // Describe, observation, detail, the summary and the list item agree. + $this->assertVisibleTimeoutCleanup($run, $terminal->payload['timeout_cleanup']); } /**