From 12dd9f61535aa0945229d46a4a66720aefc2722e Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:51:06 +0000 Subject: [PATCH] Apply parallel group policy eligibility to query and validation tasks Query tasks and update-validation tasks replay a run's history, but they skipped the replay eligibility checks. A worker without a policy the run recorded could answer a query from a misread replay, or record an update validation decision from one. Query and update-validation dispatch now apply the same policy check as workflow tasks, in claims and in task offers. A query waits for a capable worker. An update whose fresh validators cannot replay the run is refused at once with update_validator_parallel_group_policy_unsupported, and nothing is recorded. Fixes #1382 Refs durable-workflow/.github#232 Co-Authored-By: Claude Opus 5.5 --- rust/src/runtime/queries.rs | 6 + rust/src/runtime/update_validation.rs | 28 ++++- rust/tests/execution.rs | 2 + .../execution/parallel_group_replay_tasks.rs | 116 ++++++++++++++++++ 4 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 rust/tests/execution/parallel_group_replay_tasks.rs diff --git a/rust/src/runtime/queries.rs b/rust/src/runtime/queries.rs index 7aed07abf..ed2413cae 100644 --- a/rust/src/runtime/queries.rs +++ b/rust/src/runtime/queries.rs @@ -1310,6 +1310,7 @@ where } let mut task: Value = serde_json::from_str(&DB::string(&row, "value")?)?; if task["query_task_id"] != task_id || !self.query_matches_poll(&task, queue, &types) + || !Self::group_policies_supported(&mut *read, &worker, task["run_id"].as_str().unwrap_or_default()).await? || self.query_has_live_lease(&mut *read, &task, config).await? || Self::query_hold(&mut *read, &task).await?.is_some() { @@ -1434,6 +1435,11 @@ where if !self.query_matches_poll(&task, queue, &types) { continue; } + // A query replays the run's history: the worker must register + // every explicit parallel group policy it records (.github#232). + if !Self::group_policies_supported(&mut **tx, &worker, task["run_id"].as_str().unwrap_or_default()).await? { + continue; + } if self.query_has_live_lease(&mut **tx, &task, config).await? { let existing_owner = task["lease_owner"].as_str().unwrap_or(""); // A live lease belongs to the poll request that claimed it. As in diff --git a/rust/src/runtime/update_validation.rs b/rust/src/runtime/update_validation.rs index b56502931..068a546fd 100644 --- a/rust/src/runtime/update_validation.rs +++ b/rust/src/runtime/update_validation.rs @@ -321,7 +321,20 @@ where return Ok(Some(("update_validator_worker_incompatible", "Active workers do not advertise a validator-capable implementation matching this workflow run.", true))); } - Ok(None) + // .github#232: a validator replays the run, so it must also register + // every explicit parallel group policy the run's history records. + let mut connection = self.pool.acquire().await?; + for row in &rows { + let worker = self.registration(row)?; + if worker.fresh && worker.capable + && worker.serves(request.workflow_type, request.name, request.compatibility, request.fingerprint) + && Self::group_policies_supported(&mut *connection, row, request.run_id).await? + { + return Ok(None); + } + } + Ok(Some(("update_validator_parallel_group_policy_unsupported", + "Active validator workers do not register every parallel group policy this workflow run's history records.", true))) } /// `findOrCreateTask`: one task per run, update name and request id. @@ -535,7 +548,9 @@ where let worker = self.registration(&row)?; if worker.fresh && worker.capable { for task in self.validation_claim_candidates(&mut read, queue, now(), None).await? { - if !Self::validator_matches_task(&worker, &task)? { + if !Self::validator_matches_task(&worker, &task)? + || !Self::group_policies_supported(&mut *read, &row, &DB::string(&task, "workflow_run_id")?).await? + { continue; } let observed = DB::number(&task, "attempt_count")?; @@ -561,7 +576,7 @@ where if tx.claim_only.as_ref().is_some_and(|target| target.kind != "update_validation") { return Ok(None); } - let Some(worker) = Self::query("SELECT * FROM workflow_worker_registrations WHERE worker_id=$1 AND namespace=$2") + let Some(row) = Self::query("SELECT * FROM workflow_worker_registrations WHERE worker_id=$1 AND namespace=$2") .bind(worker_id) .bind(self.namespace.as_ref()) .fetch_optional(&mut **tx) @@ -569,7 +584,7 @@ where else { return Ok(None); }; - let worker = self.registration(&worker)?; + let worker = self.registration(&row)?; if !worker.fresh || !worker.capable { return Ok(None); } @@ -577,7 +592,10 @@ where let target = tx.claim_only.clone(); let tasks = self.validation_claim_candidates(tx, task_queue, at, target.as_ref().map(|scope| scope.task_id.as_str())).await?; for task in tasks { - if !Self::validator_matches_task(&worker, &task)? { + // A validator replays the run (.github#232 policy eligibility). + if !Self::validator_matches_task(&worker, &task)? + || !Self::group_policies_supported(&mut **tx, &row, &DB::string(&task, "workflow_run_id")?).await? + { continue; } let attempt = DB::number(&task, "attempt_count")?; diff --git a/rust/tests/execution.rs b/rust/tests/execution.rs index 0400d35a1..b80164487 100644 --- a/rust/tests/execution.rs +++ b/rust/tests/execution.rs @@ -73,6 +73,8 @@ mod signal_claims; mod parallel_cancellation; #[path = "execution/parallel_group_policies.rs"] mod parallel_group_policies; +#[path = "execution/parallel_group_replay_tasks.rs"] +mod parallel_group_replay_tasks; #[path = "execution/runtime_credentials.rs"] mod runtime_credentials; #[path = "execution/selected_run_signals.rs"] diff --git a/rust/tests/execution/parallel_group_replay_tasks.rs b/rust/tests/execution/parallel_group_replay_tasks.rs new file mode 100644 index 000000000..575e013a8 --- /dev/null +++ b/rust/tests/execution/parallel_group_replay_tasks.rs @@ -0,0 +1,116 @@ +//! server#1382 (.github#232): query and update-validation tasks replay the +//! run's history, so they take the same parallel group policy eligibility as +//! workflow tasks. A worker without a recorded policy never receives them. +use super::*; + +const FLAG: &str = "parallel_group_policies"; + +fn int_contract(name: &str) -> Value { + json!({"name": name, "parameters": [{"name": "value", "position": 0, "type": "int", "required": true, "variadic": false, + "default_available": false, "allows_null": false}]}) +} + +/// A worker on `test` that answers queries and validates `approve_me`, with +/// or without the policy descriptor. +async fn register_replayer(app: &Router, worker: &str, policies: Option<&[&str]>) { + let entry = json!({"supported": false, "minimum_protocol_version": "1.18"}); + let mut manifest = json!({"local_activities": entry, "worker_sessions": entry, "sticky_execution": entry}); + let mut capabilities = vec!["query_tasks", "workflow_updates", "update_validation_tasks"]; + if let Some(policies) = policies { + capabilities.push(FLAG); + manifest[FLAG] = json!({"supported": true, "version": 1, "policies": policies}); + } + let contract = json!({"queries": ["state"], "query_contracts": [int_contract("state")], "signals": [], "signal_contracts": [], + "updates": ["approve_me"], "update_contracts": [int_contract("approve_me")], "update_validators": ["approve_me"]}); + let (status, body) = request_protocol(app, "POST", "/api/worker/register", json!({"worker_id": worker, "task_queue": "test", + "runtime": "python", "supported_workflow_types": ["echo"], "supported_activity_types": ["echo_activity"], + "capabilities": capabilities, "capability_manifest": manifest, "workflow_command_contracts": {"echo": contract}}), "1.20").await; + assert_eq!(status, StatusCode::CREATED, "{body}"); +} + +/// A run whose first task admitted an explicit `propagate` group. Its +/// members stay open, so no workflow task holds a query, and the author is +/// then silent. +async fn explicit_run(database: &TestDatabase, app: &Router, workflow: &str) -> Value { + register_replayer(app, "author", Some(&["propagate"])).await; + let run = start(app, workflow).await; + let task = poll(app, "author", "workflow").await; + let commands = json!((0..2).map(|index| { + let frame = json!({"parallel_group_id": "parallel-activities:1:2", "parallel_group_kind": "activity", + "parallel_group_base_sequence": 1, "parallel_group_size": 2, "parallel_group_index": index, + "parallel_group_policy": "propagate"}); + let mut command = json!({"type": "schedule_activity", "activity_type": "echo_activity", "arguments": envelope(Payload::Array(vec![]))}); + for (field, value) in frame.as_object().unwrap() { + command[field] = value.clone(); + } + command["parallel_group_path"] = json!([frame]); + command + }).collect::>()); + let admitted = finish_task(app, &task, commands).await; + assert_eq!(admitted.0, StatusCode::OK, "{}", admitted.1); + database.execute("UPDATE workflow_worker_registrations SET last_heartbeat_at='2000-01-01 00:00:00' WHERE worker_id='author'").await; + run +} + +#[tokio::test] +async fn a_legacy_worker_never_receives_an_explicit_runs_query() { + let database = TestDatabase::new().await; + let runtime = database.open().await.unwrap(); + let app = router(runtime.clone()); + let run = explicit_run(&database, &app, "policy-query").await; + register_replayer(&app, "legacy", None).await; + register_replayer(&app, "capable", Some(&["propagate"])).await; + let pending = { + let app = app.clone(); + let path = format!("/api/workflows/policy-query/runs/{}/query/state", run["run_id"].as_str().unwrap()); + tokio::spawn(async move { request(&app, "POST", &path, json!({"input": envelope(Payload::Array(vec![Payload::Long(1)]))})).await }) + }; + // The legacy worker keeps polling and never gets it. + let deadline = tokio::time::Instant::now() + std::time::Duration::from_millis(800); + while tokio::time::Instant::now() < deadline { + assert!(poll(&app, "legacy", "query").await.is_null(), "a legacy worker cannot replay the explicit run"); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + } + let claim = await_query(&app, "capable").await; + assert_eq!(claim["run_id"], run["run_id"], "{claim}"); + pending.abort(); + runtime.close().await; + database.remove().await; +} + +#[tokio::test] +async fn a_legacy_validator_never_receives_an_explicit_runs_update_validation() { + let database = TestDatabase::new().await; + let runtime = database.open().await.unwrap(); + let app = router(runtime.clone()); + let run = explicit_run(&database, &app, "policy-update").await; + register_replayer(&app, "legacy", None).await; + let body = |request_id: &str| json!({"input": envelope(Payload::Array(vec![Payload::Long(1)])), "request_id": request_id}); + // With only a legacy validator, the update is refused at once with a + // stable reason, and nothing is recorded. + let before = run_history(&app, &run["workflow_id"], &run["run_id"]).await; + let (status, refused) = request(&app, "POST", "/api/workflows/policy-update/update/approve_me", body("u1")).await; + assert_eq!(status, StatusCode::CONFLICT, "{refused}"); + assert_eq!(refused["reason"], "update_validator_parallel_group_policy_unsupported", "{refused}"); + assert_eq!(run_history(&app, &run["workflow_id"], &run["run_id"]).await, before); + // A capable validator arrives: the task goes to it and never to the + // legacy validator. + register_replayer(&app, "capable", Some(&["propagate"])).await; + let pending = { + let app = app.clone(); + tokio::spawn(async move { request(&app, "POST", "/api/workflows/policy-update/update/approve_me", body("u2")).await }) + }; + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + let (status, legacy) = request(&app, "POST", "/api/worker/update-validation-tasks/poll", + json!({"worker_id": "legacy", "task_queue": "test", "timeout_seconds": 0})).await; + assert_eq!(status, StatusCode::OK, "{legacy}"); + assert!(legacy["task"].is_null(), "{legacy}"); + let (status, capable) = request(&app, "POST", "/api/worker/update-validation-tasks/poll", + json!({"worker_id": "capable", "task_queue": "test", "timeout_seconds": 5})).await; + assert_eq!(status, StatusCode::OK, "{capable}"); + assert_eq!(capable["poll_status"], "leased", "{capable}"); + assert_eq!(capable["task"]["workflow_run_id"].as_str().or(capable["task"]["run_id"].as_str()), run["run_id"].as_str(), "{capable}"); + pending.abort(); + runtime.close().await; + database.remove().await; +}