diff --git a/rust/src/runtime/run_observation_store.rs b/rust/src/runtime/run_observation_store.rs index 06e7f470b..339103077 100644 --- a/rust/src/runtime/run_observation_store.rs +++ b/rust/src/runtime/run_observation_store.rs @@ -220,6 +220,11 @@ where run_id, started.as_ref(), result["closed_reason"] == "continued", ).await?; result["command_contract"] = command_contract(started.as_ref()); + // Server #1201: an opted-in run's recorded cleanup, in the portable + // shape embedded RunObservation returns; absent until recorded. + if let Some(observed) = self.observed_timeout_cleanup(run_id, &status, started.as_ref()).await? { + result[super::timeout_cleanup::STARTED_KEY] = observed; + } result["workflow_definition_fingerprint"] = json!( started .as_ref() @@ -247,6 +252,20 @@ where Ok(result) } + async fn observed_timeout_cleanup(&self, run_id: &str, status: &str, started: Option<&Value>) -> Result> { + let opted_in = started.is_some_and(|started| started[super::timeout_cleanup::STARTED_KEY].is_object()); + let Some(event_type) = super::timeout_cleanup::observation_event(status).filter(|_| opted_in) else { + return Ok(None); + }; + let row = Self::query("SELECT payload FROM workflow_history_events WHERE workflow_run_id=$1 AND event_type=$2 ORDER BY sequence DESC LIMIT 1") + .bind(run_id).bind(event_type).fetch_optional(&self.pool).await?; + let Some(row) = row else { + return Ok(None); + }; + let payload = DB::optional_document_row(&row, "payload")?.unwrap_or(Value::Null); + Ok(super::timeout_cleanup::observation(event_type, &payload)) + } + async fn observation_history( &self, workflow: &str, diff --git a/rust/src/runtime/timeout_cleanup.rs b/rust/src/runtime/timeout_cleanup.rs index e8062b6ad..8355fbbec 100644 --- a/rust/src/runtime/timeout_cleanup.rs +++ b/rust/src/runtime/timeout_cleanup.rs @@ -105,6 +105,52 @@ pub(super) fn summary(outcome: &Value) -> Option<&'static str> { } } +/// The history event the bounded run observation reads for a run in +/// `status`, as embedded `TimeoutCleanup::observe` does: the terminal event +/// of a closed run, or the cleanup entry of an open one. A completed run +/// reports nothing. +pub(super) fn observation_event(status: &str) -> Option<&'static str> { + match status { + "failed" => Some("WorkflowTimedOut"), + "cancelled" => Some("WorkflowCancelled"), + "terminated" => Some("WorkflowTerminated"), + "pending" | "running" | "waiting" => Some("WorkflowTimeoutCleanupStarted"), + _ => None, + } +} + +/// The bounded run observation's top-level `timeout_cleanup` from the +/// event `observation_event` chose, in the portable shape embedded Workflow +/// 2.40.0 returns: `outcome: running` with the cause, kind, start and +/// deadlines while cleanup is open, then the outcome recorded on the +/// terminal event. Only string fields are kept; nothing is reported when +/// cleanup was never recorded. +pub(super) fn observation(event_type: &str, payload: &Value) -> Option { + if !payload.is_object() { + return None; + } + let mut observed = serde_json::Map::new(); + if event_type == "WorkflowTimeoutCleanupStarted" { + observed.insert("outcome".into(), json!("running")); + observed.insert("cause".into(), json!(CAUSE_TIMEOUT)); + for (key, from) in [("timeout_kind", "timeout_kind"), ("started_at", "detected_at"), + ("work_deadline_at", "work_deadline_at"), ("cleanup_deadline_at", "cleanup_deadline_at")] { + if let Some(value) = payload.get(from).filter(|value| value.is_string()) { + observed.insert(key.into(), value.clone()); + } + } + return Some(Value::Object(observed)); + } + let recorded = &payload[STARTED_KEY]; + summary(recorded)?; + for key in ["outcome", "timeout_kind", "started_at", "cleanup_deadline_at", "finished_at", "failure_id"] { + if let Some(value) = recorded.get(key).filter(|value| value.is_string()) { + observed.insert(key.into(), value.clone()); + } + } + Some(Value::Object(observed)) +} + /// A registration that delivers the typed timeout interruption: it lists /// the capability and its manifest supports version 1. pub(super) fn worker_supports(capabilities: &Value, manifest: &Value) -> bool { @@ -532,6 +578,27 @@ mod tests { } } + #[test] + fn the_observation_follows_embedded() { + assert_eq!(observation_event("waiting"), Some("WorkflowTimeoutCleanupStarted")); + assert_eq!(observation_event("failed"), Some("WorkflowTimedOut")); + assert_eq!(observation_event("completed"), None, "completed and continued-as-new runs report nothing"); + assert_eq!(observation_event("cancelled"), Some("WorkflowCancelled")); + assert_eq!(observation_event("terminated"), Some("WorkflowTerminated")); + assert_eq!(observation_event("pending"), Some("WorkflowTimeoutCleanupStarted")); + assert_eq!(observation("WorkflowTimeoutCleanupStarted", &Value::Null), None); + let entered = json!({"timeout_kind": "run", "work_deadline_at": "w", "cleanup_deadline_at": "c", + "hard_deadline_at": "h", "detected_at": "d", "workflow_command_id": "x"}); + assert_eq!(observation("WorkflowTimeoutCleanupStarted", &entered), Some(json!({"outcome": "running", + "cause": "timeout", "timeout_kind": "run", "started_at": "d", "work_deadline_at": "w", "cleanup_deadline_at": "c"}))); + let closed = json!({"timeout_cleanup": {"outcome": "failed", "timeout_kind": "execution", "cleanup_deadline_at": "c", + "finished_at": "f", "failure_id": "id", "started_at": null}}); + assert_eq!(observation("WorkflowTimedOut", &closed), Some(json!({"outcome": "failed", "timeout_kind": "execution", + "cleanup_deadline_at": "c", "finished_at": "f", "failure_id": "id"}))); + assert_eq!(observation("WorkflowTimedOut", &json!({"cancellation_cleanup": {"outcome": "preempted"}})), None); + assert_eq!(observation("WorkflowTimedOut", &json!({"timeout_cleanup": {"outcome": "unknown"}})), None); + } + #[test] fn summaries_are_plain() { assert_eq!(summary(&json!({"outcome": "failed"})), Some("Timeout cleanup failed.")); diff --git a/rust/tests/execution/timeout_cleanup.rs b/rust/tests/execution/timeout_cleanup.rs index 598d88cbe..f7b4b8107 100644 --- a/rust/tests/execution/timeout_cleanup.rs +++ b/rust/tests/execution/timeout_cleanup.rs @@ -185,6 +185,9 @@ async fn a_start_without_the_option_keeps_the_hard_timeout_unchanged() { assert_eq!(keys, ["failure_id", "timeout_kind", "failure_category", "message", "exception_class", "execution_deadline_at", "run_deadline_at", "task"]); assert_eq!(timed_out["message"], "Workflow execution deadline expired at 2000-01-01T00:00:00+00:00."); + let (_, observed) = request(&app, "GET", &format!("/api/workflows/tc-default/runs/{}/observation", run.as_str().unwrap()), Value::Null).await; + assert_eq!(observed["status"], "failed", "{observed}"); + assert!(observed.get("timeout_cleanup").is_none(), "{observed}"); } // Vectors 1 and 6, the idle wake and the audited user request. @@ -390,6 +393,8 @@ async fn terminate_and_hard_cancel_preempt_cleanup() { let closed = terminal(&history, event); assert_eq!(closed["timeout_cleanup"]["outcome"], "preempted", "{closed}"); assert_eq!(closed["timeout_cleanup"]["timeout_kind"], "execution"); + let (_, observed) = request(&app, "GET", &format!("/api/workflows/{workflow}/runs/{}/observation", run.as_str().unwrap()), Value::Null).await; + assert_eq!(observed["timeout_cleanup"], closed["timeout_cleanup"], "{observed}"); } } @@ -535,6 +540,9 @@ async fn the_earliest_hard_deadline_names_the_kind_and_describe_follows_the_stat assert_eq!(described["timeout_cleanup"]["state"], "armed", "{described}"); assert_eq!(described["timeout_cleanup"]["cause"], Value::Null); assert_eq!(described["timeout_cleanup"]["outcome"], Value::Null); + let observation = format!("/api/workflows/tc-run-kind/runs/{}/observation", run.as_str().unwrap()); + let (_, observed) = request(&app, "GET", &observation, Value::Null).await; + assert!(observed.get("timeout_cleanup").is_none(), "nothing recorded while armed: {observed}"); assert_eq!(described["timeout_cleanup"]["allowance_seconds"], ALLOWANCE); assert_eq!(described["timeout_cleanup"]["timeout_kind"], "run"); let first = cleanup_poll(&app, "cleanup-worker").await; @@ -551,6 +559,10 @@ async fn the_earliest_hard_deadline_names_the_kind_and_describe_follows_the_stat let view = &described["timeout_cleanup"]; assert_eq!((view["state"].as_str(), view["cause"].as_str()), (Some("cancellation_cleanup_running"), Some("timeout")), "{view}"); assert_eq!(view["outcome"], "running", "the portable open-cleanup outcome"); + let (_, observed) = request(&app, "GET", &observation, Value::Null).await; + assert_eq!(observed["timeout_cleanup"], json!({"outcome": "running", "cause": "timeout", "timeout_kind": "run", + "started_at": entered["detected_at"], "work_deadline_at": entered["work_deadline_at"], + "cleanup_deadline_at": entered["cleanup_deadline_at"]}), "{observed}"); assert_eq!(view["started_at"], entered["detected_at"]); assert_eq!(view["cleanup_deadline_at"], json!(iso)); let task = cleanup_poll(&app, "cleanup-worker").await; @@ -565,6 +577,9 @@ async fn the_earliest_hard_deadline_names_the_kind_and_describe_follows_the_stat let (_, described) = request(&app, "GET", &format!("/api/workflows/tc-run-kind/runs/{}", run.as_str().unwrap()), Value::Null).await; assert_eq!(described["timeout_cleanup"]["state"], "finished", "{described}"); assert_eq!(described["timeout_cleanup"]["outcome"], "completed"); + let (_, observed) = request(&app, "GET", &observation, Value::Null).await; + assert_eq!(observed["timeout_cleanup"], timed_out["timeout_cleanup"], "{observed}"); + assert_eq!(observed["timeout_cleanup"]["outcome"], "completed"); } // Vector 6 and worker replacement: the task holding the cleanup turn moves diff --git a/scripts/conformance/server-parity/native-divergences.json b/scripts/conformance/server-parity/native-divergences.json index e2cd5778d..ca80829ed 100644 --- a/scripts/conformance/server-parity/native-divergences.json +++ b/scripts/conformance/server-parity/native-divergences.json @@ -70,18 +70,18 @@ { "id": "describe-timeout-cleanup-state", "issue": "https://github.com/durable-workflow/server/issues/1201", - "since": "native source PR #1235 (unreleased)", + "since": "native 3.14.0", "applies_to": ["timeout-cleanup"], - "published_php": "Published PHP does not admit timeout cleanup on service starts, and embedded Workflow 2.29.0 run detail and describe show neither the cleanup state nor its outcome; the outcome is visible only on the terminal history event and in the timeout failure message.", - "native": "Describe and run detail of an opted-in run carry timeout_cleanup {state: armed | cancellation_cleanup_running | finished | not_entered, cause (timeout once entered), allowance_seconds, timeout_kind, work_deadline_at, cleanup_deadline_at, started_at, outcome (running while cleanup is open, as embedded Workflow 2.40.0 reports it, then the recorded outcome), failure_id}. A run without the opt-in has no timeout_cleanup key; an opted-in run that has not entered cleanup reports it armed or not_entered where embedded 2.40.0 omits the field.", - "harness": "No recorded corpus run opts in, so no comparator changes. The all-backend execution tests cover every state and outcome." + "published_php": "Published PHP does not admit timeout cleanup on service starts. Embedded Workflow 2.40.0 describe and run detail return timeout_cleanup only once cleanup is recorded: {outcome: running, cause: timeout, timeout_kind, started_at, work_deadline_at, cleanup_deadline_at} while it is open, then the terminal event's {outcome, timeout_kind, started_at, cleanup_deadline_at, finished_at, failure_id}, omitting absent fields; with nothing recorded the field is omitted.", + "native": "Describe and run detail of an opted-in run always carry timeout_cleanup {state, cause, allowance_seconds, timeout_kind, work_deadline_at, cleanup_deadline_at, started_at, outcome, failure_id}, with null for absent values. state is armed before the work deadline is noticed and not_entered when the run closed without entering cleanup, where embedded omits the field; cancellation_cleanup_running while cleanup is open, with outcome running from the release after 3.29.0 (null before); finished with the recorded outcome, without finished_at. A run without the opt-in has no timeout_cleanup key. From the release after 3.29.0 the bounded run observation is not part of this divergence: it returns embedded's shape.", + "harness": "No recorded corpus run opts in, so no comparator changes. The all-backend execution tests cover every describe state and outcome and the observation's absent, running and finished shapes." }, { "id": "child-timeout-cleanup-opt-in", "issue": "https://github.com/durable-workflow/server/issues/1201", - "since": "native source PR #1235 (unreleased)", + "since": "native 3.14.0", "applies_to": ["timeout-cleanup"], - "published_php": "Embedded Workflow 2.29.0 has no child-workflow timeout cleanup option; the portable contract gives a child its own timeouts and its own opt-in.", + "published_php": "Embedded Workflow 2.40.0 has no child-workflow timeout cleanup option; the portable contract gives a child its own timeouts and its own opt-in.", "native": "start_child_workflow accepts timeout_cleanup_seconds under the start's admission rules (1 to 86400, a child timeout required, shorter than each one, a live capable worker on the child's queue); the child records its own WorkflowStarted.timeout_cleanup, and a retried child keeps its allowance. The parent's allowance never carries over.", "harness": "No recorded corpus command carries the field. The all-backend execution tests cover validation, capability refusal without writes, and the child's recorded allowance." },