aion_core/event.rs
1//! Workflow history events and their deterministic recording envelope.
2
3use std::collections::HashMap;
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8use crate::{
9 ActivityError, ActivityId, PackageVersion, Payload, RunId, ScheduleConfig, ScheduleId,
10 SearchAttributeValue, TimerId, WorkflowError, WorkflowId,
11};
12
13/// Metadata recorded with every workflow history event.
14#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq, Eq)]
15pub struct EventEnvelope {
16 /// Monotonic sequence number within the owning workflow history.
17 pub seq: u64,
18 /// Recorded UTC timestamp for this event.
19 ///
20 /// This timestamp is the determinism source for `workflow.now`; replay must use the recorded
21 /// value rather than consulting wall-clock time.
22 pub recorded_at: DateTime<Utc>,
23 /// Workflow history that owns this event.
24 pub workflow_id: WorkflowId,
25}
26
27/// The named default task queue: the single sanctioned fallback when no explicit task queue was
28/// selected (no SDK-level selection exists yet — that is NSTQ-4) and the replay-safe decode value
29/// for [`Event::ActivityScheduled`] events recorded before the `task_queue` field existed.
30///
31/// This is the canonical task-queue default for the whole workspace. `aion_store::DEFAULT_OUTBOX_ROUTE`
32/// and `aion_server::worker::registry::DEFAULT_TASK_QUEUE` both alias/re-export this constant rather
33/// than redeclaring the literal, so a history-derived task queue and an outbox-row-derived task queue
34/// cannot drift.
35pub const DEFAULT_TASK_QUEUE: &str = "default";
36
37/// serde default for [`Event::ActivityScheduled::task_queue`]: the named [`DEFAULT_TASK_QUEUE`].
38///
39/// Used by `#[serde(default = ...)]` so an old recorded history that has no `task_queue` on its
40/// `ActivityScheduled` events decodes deterministically to `"default"`.
41fn default_task_queue() -> String {
42 String::from(DEFAULT_TASK_QUEUE)
43}
44
45/// Sentinel `attempt` value for activity lifecycle events decoded from a history recorded BEFORE the
46/// `attempt` field existed on [`Event::ActivityStarted`] / [`Event::ActivityCompleted`] /
47/// [`Event::ActivityCancelled`] (NOI-0).
48///
49/// Activity attempts are **one-based** everywhere they are produced (see [`Event::ActivityFailed`]'s
50/// `attempt`, which is documented "One-based activity attempt number", and the engine's
51/// `FIRST_DELIVERY_ATTEMPT = 1`). A real attempt is therefore always `>= 1`, so `0` can never collide
52/// with a genuine attempt: it is a distinguishable "legacy / unknown attempt" marker. Old histories
53/// that predate the field decode to this sentinel via `#[serde(default = "legacy_activity_attempt")]`
54/// — deterministically, never panicking, never differing run-to-run — while the compiler still forces
55/// every LIVE construction site to supply the genuine one-based attempt (there is no blanket
56/// `Default` on the variant).
57const LEGACY_ACTIVITY_ATTEMPT: u32 = 0;
58
59/// serde default for the `attempt` field on the activity lifecycle events that gained it in NOI-0.
60///
61/// Returns [`LEGACY_ACTIVITY_ATTEMPT`] (`0`) so a history recorded before the field existed decodes
62/// deterministically to the legacy/unknown sentinel rather than failing. See
63/// [`LEGACY_ACTIVITY_ATTEMPT`] for why `0` is a safe distinguishable value under one-based attempts.
64fn legacy_activity_attempt() -> u32 {
65 LEGACY_ACTIVITY_ATTEMPT
66}
67
68/// Search attribute name that records the task queue a workflow was STARTED on.
69///
70/// The server stamps this attribute durably in the SAME atomic append as
71/// [`Event::WorkflowStarted`] (via [`Event::SearchAttributesUpdated`]) when the
72/// start request selected a task queue — mirroring the `aion.namespace`
73/// attribute that records the owning namespace. It is therefore part of
74/// RECORDED HISTORY: recovery/replay re-derive the identical value, so an
75/// activity that falls back to its workflow's start-time queue (#144) resolves
76/// to the same queue on every replay. The attribute is absent when the start
77/// did not select a queue (the legacy / "no selection anywhere" case).
78///
79/// This is the canonical name for the whole workspace;
80/// `aion_server::TASK_QUEUE_ATTRIBUTE` re-exports it rather than redeclaring the
81/// literal, so a history-derived start-time queue and the server's recorded
82/// attribute cannot drift.
83pub const START_TIME_TASK_QUEUE_ATTRIBUTE: &str = "aion.task_queue";
84
85/// The task queue a workflow was STARTED on, projected from recorded history.
86///
87/// Reads the [`START_TIME_TASK_QUEUE_ATTRIBUTE`] search attribute folded from
88/// the run's [`Event::SearchAttributesUpdated`] events (the server records it in
89/// the same append as [`Event::WorkflowStarted`]). Returns `None` when the start
90/// recorded no task-queue selection — a legacy history, or a start that left the
91/// queue unset — so callers fall back to the named [`DEFAULT_TASK_QUEUE`].
92///
93/// Because the value is read purely from recorded history (never from live or
94/// wall-clock state), it is replay-deterministic: the same history always
95/// projects the same start-time queue.
96#[must_use]
97pub fn start_time_task_queue(events: &[Event]) -> Option<String> {
98 let attributes = crate::search_attributes_from_events(events);
99 match attributes.get(START_TIME_TASK_QUEUE_ATTRIBUTE) {
100 Some(crate::SearchAttributeValue::String(queue)) => Some(queue.clone()),
101 _ => None,
102 }
103}
104
105/// Search attribute name that carries a workflow's operator-facing display
106/// name (#211).
107///
108/// The display name is a LABEL over the workflow's UUID identity, never an
109/// address. Stated exactly, because the label rule is about ACTS and not about
110/// whether a name may appear on the wire:
111///
112/// 1. **No operation acts on a name.** Every act-verb — start, signal, query,
113/// cancel, pause, resume, reopen, rename, intervene — takes a `WorkflowId`
114/// (and optionally a `RunId`). None of them accepts a name as its target.
115/// 2. **Name-shaped FILTERING returns lists and acts on nothing.** This
116/// attribute is an ordinary search attribute, so it is filterable like any
117/// other: a visibility predicate on it yields candidate summaries. That is
118/// the first half of a picker (name in, candidates out, an operator
119/// chooses, a UUID acts), and it is deliberately supported.
120/// 3. **The composition is prohibited.** No SDK, CLI verb, MCP tool, or
121/// convenience function may compose name-filter → take-first → act. That
122/// composition re-imports addressing one layer up: the system, not the
123/// caller, would be choosing the referent. The CHOOSING STEP STAYS WITH THE
124/// CALLER, BY CONTRACT. A future "find by name" helper RETURNS CANDIDATES
125/// AND STOPS.
126///
127/// The sweep predicate, written down so the next audit checks the right thing
128/// rather than re-deriving it: *for every verb, HTTP route, RPC, MCP tool, and
129/// SDK function that CHANGES state, the target is identified by a UUID; a
130/// display name appears only as a value being SET, never as the selector, and
131/// no single call turns a name into an act.* The MCP tool catalogue enforces
132/// this mechanically (`aion_server::mcp::catalog` — see its
133/// `name_is_not_an_address_tests`); the other surfaces are Rust types rather
134/// than data and are checked by reading against that predicate.
135///
136/// It is recorded durably as an ordinary [`Event::SearchAttributesUpdated`]
137/// search attribute — stamped in the same atomic append as
138/// [`Event::WorkflowStarted`] when the start named one, and re-recorded by
139/// every rename — so history keeps every name the workflow has worn and
140/// replay/failover re-derive the current one.
141///
142/// The recorded attribute carries NO run id. A rename is addressed to a
143/// workflow AND a run, but what it records is a workflow-level attribute, and
144/// every reader folds it over the whole history (see [`display_name`]). So a
145/// continue-as-new successor inherits the name its predecessor was wearing, and
146/// describing any run of the workflow reads back the same, current name.
147///
148/// This is the canonical name for the whole workspace;
149/// `aion_server::DISPLAY_NAME_ATTRIBUTE` re-exports it rather than redeclaring
150/// the literal, so the projector below and the server's recorded attribute
151/// cannot drift.
152pub const DISPLAY_NAME_ATTRIBUTE: &str = "aion.display_name";
153
154/// The workflow's current display name, projected from recorded history.
155///
156/// Reads the [`DISPLAY_NAME_ATTRIBUTE`] search attribute folded over EVERY
157/// [`Event::SearchAttributesUpdated`] in `events`, last write wins — so a
158/// rename's later update supersedes the start-time name while history keeps
159/// both. Returns `None` when no name was ever recorded (the unnamed workflow
160/// renders as its bare UUID).
161///
162/// The fold is per WORKFLOW, not per run: it is not run-scoped the way
163/// [`crate::failure_projection`] and [`crate::current_lease_terminal`] are, and
164/// the recorded attribute carries no run id to scope it by. Hand it a whole
165/// workflow history and the newest recorded name wins whichever run recorded
166/// it — so a continue-as-new successor reads back the name its predecessor was
167/// wearing until something renames it.
168///
169/// Because the value is read purely from recorded history (never from live or
170/// wall-clock state), it is replay-deterministic: the same history always
171/// projects the same display name.
172#[must_use]
173pub fn display_name(events: &[Event]) -> Option<String> {
174 display_name_from_attributes(&crate::search_attributes_from_events(events))
175}
176
177/// The display name carried by an already-folded search-attribute map.
178///
179/// The same projection as [`display_name`], for readers that hold the folded
180/// map rather than the events — the visibility projection's
181/// `search_attributes`, which the store keeps refreshed on every append. Both
182/// entry points share this one lookup so the attribute name and the
183/// String-typed expectation cannot drift between the history reader and the
184/// visibility reader. The map is itself folded over a whole workflow history,
185/// so this reads the same per-workflow label with the same last-write-wins
186/// rule.
187#[must_use]
188pub fn display_name_from_attributes<S: std::hash::BuildHasher>(
189 attributes: &std::collections::HashMap<String, crate::SearchAttributeValue, S>,
190) -> Option<String> {
191 match attributes.get(DISPLAY_NAME_ATTRIBUTE) {
192 Some(crate::SearchAttributeValue::String(name)) => Some(name.clone()),
193 _ => None,
194 }
195}
196
197/// A recorded workflow history event.
198///
199/// User data is carried as opaque [`Payload`] values, while failures use the closed workflow and
200/// activity error types from this crate.
201#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq)]
202#[serde(tag = "type", content = "data")]
203pub enum Event {
204 /// A workflow execution started with a type name and input payload.
205 WorkflowStarted {
206 /// Recording metadata for this event.
207 envelope: EventEnvelope,
208 /// Workflow type selected by the caller.
209 workflow_type: String,
210 /// Opaque workflow input payload.
211 input: Payload,
212 /// Concrete run identifier started by this event.
213 run_id: RunId,
214 /// Parent run that continued as this run, when this start is part of a
215 /// continue-as-new chain.
216 parent_run_id: Option<RunId>,
217 /// The parent WORKFLOW that spawned this run as a child (aion#77):
218 /// the child wears its parent, so a handoff chain is traversable
219 /// backward from any run and the console can group an arc without
220 /// scanning every parent history. `None` for operator-started roots
221 /// and for histories recorded before the field existed (the serde
222 /// default, exactly as `task_queue` and `attempt` decode legacies).
223 #[serde(default)]
224 parent_workflow_id: Option<WorkflowId>,
225 /// Package version this run was resolved against at record time.
226 ///
227 /// Recovery and replay resolve workflow code from this recorded
228 /// version; they never re-resolve a "latest" version.
229 package_version: PackageVersion,
230 },
231 /// A workflow execution completed successfully; this terminal event projects to Completed.
232 WorkflowCompleted {
233 /// Recording metadata for this event.
234 envelope: EventEnvelope,
235 /// Opaque workflow result payload.
236 result: Payload,
237 },
238 /// A workflow execution failed terminally; this terminal event projects to Failed.
239 WorkflowFailed {
240 /// Recording metadata for this event.
241 envelope: EventEnvelope,
242 /// Terminal workflow failure.
243 error: WorkflowError,
244 },
245 /// A workflow execution was cancelled; this terminal event projects to Cancelled.
246 WorkflowCancelled {
247 /// Recording metadata for this event.
248 envelope: EventEnvelope,
249 /// Human-readable cancellation reason.
250 reason: String,
251 },
252 /// A workflow execution timed out; this terminal event projects to `TimedOut`.
253 WorkflowTimedOut {
254 /// Recording metadata for this event.
255 envelope: EventEnvelope,
256 /// Descriptor identifying the timeout that elapsed.
257 ///
258 /// Intentionally stringly-typed: the closed set of timeout kinds is defined by cluster AT
259 /// (timers and signals), not by the core event model.
260 timeout: String,
261 },
262 /// A workflow execution continued as a new run; this terminal event projects to
263 /// `ContinuedAsNew`.
264 WorkflowContinuedAsNew {
265 /// Recording metadata for this event.
266 envelope: EventEnvelope,
267 /// Opaque workflow input payload carried into the new run.
268 input: Payload,
269 /// Workflow type override for the new run, when migration changes the workflow type.
270 ///
271 /// When absent, the new run uses the current workflow type.
272 workflow_type: Option<String>,
273 /// Run identifier for the current run that is being continued.
274 parent_run_id: RunId,
275 },
276 /// A failed run was reopened.
277 ///
278 /// Engine-internal — never authored by workflow or SDK code. This is the
279 /// compensating event that reconciles reopen with the status-is-a-projection
280 /// invariant: under the last-lifecycle-event-wins scan it supersedes the
281 /// run's prior terminal event and returns the run to Running, exactly as a
282 /// replacement [`Event::WorkflowStarted`] does for continue-as-new. Terminal
283 /// detection is scoped to "since the last reopen point", so a run holds
284 /// exactly one terminal event per lease.
285 WorkflowReopened {
286 /// Recording metadata for this event.
287 envelope: EventEnvelope,
288 /// Run being reopened — the run that recorded the superseded terminal
289 /// event and that the reopened execution continues.
290 run_id: RunId,
291 /// Activities to re-dispatch on replay: those that ended in a terminal
292 /// failure in this run with no later successful attempt. The history
293 /// cursor treats each as a reset point so the recorded failure is
294 /// superseded and the activity resolves to live re-dispatch.
295 reopened: Vec<ActivityId>,
296 },
297 /// A running workflow was paused by an operator.
298 ///
299 /// Engine-internal — never authored by workflow or SDK code. A NON-terminal
300 /// lifecycle marker: under the last-lifecycle-event-wins scan it projects the
301 /// run to [`crate::WorkflowStatus::Paused`], holding new activity dispatch at
302 /// the outbox while every durable record path (timer fires, signal receipts,
303 /// drained completions) keeps recording. It is invisible to the replay cursor
304 /// (it is neither a terminal nor a run-start reset), so a paused-then-resumed
305 /// history replays byte-identically to one that was never paused.
306 WorkflowPaused {
307 /// Recording metadata for this event.
308 envelope: EventEnvelope,
309 /// Run being paused — the live, non-terminal run the operator held.
310 run_id: RunId,
311 /// Optional operator-supplied pause reason.
312 reason: Option<String>,
313 /// Optional identity of the operator who issued the pause.
314 operator: Option<String>,
315 },
316 /// A paused workflow was resumed by an operator.
317 ///
318 /// Engine-internal — never authored by workflow or SDK code. Supersedes the
319 /// run's prior [`Event::WorkflowPaused`] under the last-lifecycle-event-wins
320 /// scan, returning the run to [`crate::WorkflowStatus::Running`], and — like
321 /// [`Event::WorkflowPaused`] — is invisible to the replay cursor.
322 WorkflowResumed {
323 /// Recording metadata for this event.
324 envelope: EventEnvelope,
325 /// Run being resumed.
326 run_id: RunId,
327 /// Optional identity of the operator who issued the resume.
328 operator: Option<String>,
329 },
330 /// Workflow search attributes were updated for visibility and query projection.
331 SearchAttributesUpdated {
332 /// Recording metadata for this event.
333 envelope: EventEnvelope,
334 /// Workflow whose search attributes changed.
335 workflow_id: WorkflowId,
336 /// Updated search attributes keyed by attribute name.
337 attributes: HashMap<String, SearchAttributeValue>,
338 },
339 /// An activity was scheduled by workflow code.
340 ActivityScheduled {
341 /// Recording metadata for this event.
342 envelope: EventEnvelope,
343 /// Deterministic activity identifier derived from the scheduling sequence position.
344 activity_id: ActivityId,
345 /// Activity type selected by workflow code.
346 activity_type: String,
347 /// Opaque activity input payload.
348 input: Payload,
349 /// Pool/flavour selector this activity dispatches to within the workflow's namespace
350 /// (NSTQ-3). This is the durable source-of-truth for re-targeting the **same** task queue
351 /// on reopen/recovery, mirroring how the namespace is recovered from history but recorded
352 /// **per-activity** rather than as a workflow-level search attribute.
353 ///
354 /// Replay-safety: histories recorded before this field existed have no `task_queue` on
355 /// their `ActivityScheduled` events. Decode defaults the missing value to
356 /// [`DEFAULT_TASK_QUEUE`] (`"default"`) via `#[serde(default = ...)]`, so an old history
357 /// deterministically re-derives `task_queue = "default"` — never panics, never differs
358 /// run-to-run. The encoding of the existing fields is untouched.
359 #[serde(default = "default_task_queue")]
360 task_queue: String,
361 /// OPTIONAL node affinity this activity dispatches to (NODE-3). `None` = no affinity (the
362 /// genuine current value; SDK-level node selection is NODE-4). This is the durable
363 /// source-of-truth for re-targeting the **same** node on reopen/recovery, recorded
364 /// **per-activity** alongside `task_queue`.
365 ///
366 /// Replay-safety: histories recorded before this field existed have no `node` key on their
367 /// `ActivityScheduled` events. serde's `Option` default is `None`, so `#[serde(default)]`
368 /// decodes a missing `node` deterministically to `None` — never a sentinel, never panics,
369 /// never differs run-to-run. The encoding of the existing fields is untouched.
370 #[serde(default)]
371 node: Option<String>,
372 },
373 /// The engine DISPATCHED an activity attempt to its task queue.
374 ///
375 /// **This does NOT mean a worker has taken the work.** The event is written
376 /// by the engine at dispatch time, in the SAME atomic append as its
377 /// [`Event::ActivityScheduled`] and under the same `recorded_at`, before any
378 /// worker has been selected — let alone leased the attempt. Worker selection
379 /// happens afterwards, in the server, and may wait indefinitely: a dispatch
380 /// to a task queue nobody serves records this event and then nothing, so a
381 /// run parked forever and a run a worker is actively executing have
382 /// identical history shapes and both project
383 /// [`WorkflowStatus::Running`](crate::WorkflowStatus::Running). That is not
384 /// a projection bug — history genuinely holds no terminal event — but it
385 /// means this event alone can never answer "is anyone working on it".
386 ///
387 /// Every producing seam behaves this way: the single-dispatch and in-VM
388 /// seams (`aion::runtime::nif_activity_dispatch`,
389 /// `aion::runtime::nif_activity_in_vm`), the retry delivery
390 /// (`aion::runtime::nif_activity_retry_dispatch`, which records the next
391 /// attempt's start "before it goes on the wire"), and the fan-out batch
392 /// (`aion::durability::recorder::fan_out`).
393 ///
394 /// The name is therefore wrong and the behaviour is not. A rename to
395 /// `ActivityDispatched` is PROPOSED and PENDING an owner ruling, together
396 /// with the question of whether a separate lease-time event should exist;
397 /// see `docs/design/aion-authoring/ACTIVITY-STARTED-SEMANTICS-DECISION.md`.
398 /// Deferring THIS event to lease time is recommended against there, because
399 /// it would silently change what every already-recorded `ActivityStarted`
400 /// meant.
401 ///
402 /// To ask whether an in-flight activity can still reach a worker, join its
403 /// recorded address to the live fleet — `aion_server`'s
404 /// `worker::ActivityReachability`, surfaced on `POST /workflows/describe`.
405 ActivityStarted {
406 /// Recording metadata for this event.
407 envelope: EventEnvelope,
408 /// Activity that was dispatched.
409 activity_id: ActivityId,
410 /// One-based activity attempt number this start belongs to (NOI-0).
411 ///
412 /// Matches the `attempt` on the [`Event::ActivityFailed`] / [`Event::ActivityCompleted`] /
413 /// [`Event::ActivityCancelled`] that terminates the SAME attempt, so
414 /// `(workflow, activity, attempt)` is a stable identity across the whole lifecycle — the key
415 /// the NOI dedupe/guard/session-id design is built on.
416 ///
417 /// Replay-safety: histories recorded before this field existed have no `attempt` key on their
418 /// `ActivityStarted` events. Decode defaults the missing value to
419 /// [`LEGACY_ACTIVITY_ATTEMPT`] (`0`) via `#[serde(default = ...)]` — never panics, never
420 /// differs run-to-run. Because real attempts are one-based, `0` is a distinguishable
421 /// legacy/unknown sentinel, never a genuine attempt. The encoding of the existing fields is
422 /// untouched.
423 #[serde(default = "legacy_activity_attempt")]
424 attempt: u32,
425 },
426 /// Recovery offered a previously dispatched, still-dangling activity
427 /// attempt back to the worker adoption path.
428 ///
429 /// This is deliberately an OFFER, not a claim that the worker found a live
430 /// holder: the engine cannot observe a harness process directly. It records
431 /// the truthful recovery decision — retain and redeliver the same execution
432 /// identity instead of falsely terminating it as server death. The worker's
433 /// single-flight/spawn gate then either joins the holder or starts it when no
434 /// holder survived.
435 ActivityAdoptionOffered {
436 /// Recording metadata for this event.
437 envelope: EventEnvelope,
438 /// Activity whose dangling attempt is being offered for adoption.
439 activity_id: ActivityId,
440 /// One-based attempt identity retained across the recovery redelivery.
441 attempt: u32,
442 },
443 /// An activity completed successfully.
444 ActivityCompleted {
445 /// Recording metadata for this event.
446 envelope: EventEnvelope,
447 /// Activity that produced the result.
448 activity_id: ActivityId,
449 /// Opaque activity result payload.
450 result: Payload,
451 /// One-based activity attempt number that produced this completion (NOI-0).
452 ///
453 /// Matches the `attempt` on the [`Event::ActivityStarted`] of the SAME attempt, so a
454 /// completed activity carries one consistent `attempt` readable off both its start and its
455 /// terminal — the negative-control invariant NOI-0 gates on.
456 ///
457 /// Replay-safety: histories recorded before this field existed have no `attempt` key on their
458 /// `ActivityCompleted` events. Decode defaults the missing value to
459 /// [`LEGACY_ACTIVITY_ATTEMPT`] (`0`) via `#[serde(default = ...)]` — never panics, never
460 /// differs run-to-run. The encoding of the existing fields is untouched.
461 #[serde(default = "legacy_activity_attempt")]
462 attempt: u32,
463 },
464 /// An activity attempt failed.
465 ///
466 /// The `attempt` field together with [`ActivityError`]'s retryable or terminal classification
467 /// lets replay distinguish a retryable interim failure from a terminal one for the same
468 /// [`ActivityId`].
469 ActivityFailed {
470 /// Recording metadata for this event.
471 envelope: EventEnvelope,
472 /// Activity whose attempt failed.
473 activity_id: ActivityId,
474 /// Classified activity failure.
475 error: ActivityError,
476 /// One-based activity attempt number that produced this failure.
477 attempt: u32,
478 },
479 /// An ADVISORY activity spent its whole attempt budget and failed for good
480 /// (RUNTIME-OPERATIONS.md R5).
481 ///
482 /// The warning the class promises: an advisory action is a side channel
483 /// (a heartbeat, a notification), so its exhaustion never faults the
484 /// calling step — but it must never be silent either. This event is that
485 /// visibility. It ACCOMPANIES the activity's honest terminal
486 /// [`Event::ActivityFailed`]; it never replaces it, because the activity
487 /// really did fail and history says so.
488 ///
489 /// Non-terminal for the workflow: it says nothing about the run's
490 /// outcome, so status projection deliberately ignores it.
491 ActivityAdvisoryExhausted {
492 /// Recording metadata for this event.
493 envelope: EventEnvelope,
494 /// Advisory activity whose attempt budget was spent.
495 activity_id: ActivityId,
496 /// Activity type of the exhausted advisory activity.
497 activity_type: String,
498 /// The last attempt's failure reason, verbatim — the same string the
499 /// accompanying terminal [`Event::ActivityFailed`] carries.
500 reason: String,
501 /// One-based number of the attempt that spent the budget.
502 attempt: u32,
503 },
504 /// A policy-refused activity execution was durably routed to a fallback queue.
505 ///
506 /// This event is nonterminal and status-invisible. Its recorded destination
507 /// is the durable replay answer; recovery reuses it rather than choosing again.
508 ActivityFallbackRouted {
509 /// Recording metadata for this event.
510 envelope: EventEnvelope,
511 /// Activity whose refusing execution caused the hop.
512 activity_id: ActivityId,
513 /// One-based execution attempt whose policy refusal caused this hop.
514 attempt: u32,
515 /// Queue the refusing execution was dispatched on.
516 from_task_queue: String,
517 /// Queue the next execution is dispatched on.
518 to_task_queue: String,
519 /// Zero-based position consumed from the authored fallback list.
520 fallback_index: u32,
521 },
522 /// An activity was cancelled as an explicit cancellation outcome.
523 ActivityCancelled {
524 /// Recording metadata for this event.
525 envelope: EventEnvelope,
526 /// Activity that was cancelled.
527 activity_id: ActivityId,
528 /// One-based activity attempt number that was cancelled (NOI-0).
529 ///
530 /// Matches the `attempt` on the [`Event::ActivityStarted`] of the SAME attempt, so the
531 /// cancellation terminal is attributable to a specific attempt exactly like
532 /// [`Event::ActivityFailed`] is.
533 ///
534 /// Replay-safety: histories recorded before this field existed have no `attempt` key on their
535 /// `ActivityCancelled` events. Decode defaults the missing value to
536 /// [`LEGACY_ACTIVITY_ATTEMPT`] (`0`) via `#[serde(default = ...)]` — never panics, never
537 /// differs run-to-run. The encoding of the existing fields is untouched.
538 #[serde(default = "legacy_activity_attempt")]
539 attempt: u32,
540 },
541 /// A timer was scheduled to fire at a deterministic timestamp.
542 TimerStarted {
543 /// Recording metadata for this event.
544 envelope: EventEnvelope,
545 /// Timer selected by workflow code or assigned by the engine.
546 timer_id: TimerId,
547 /// UTC timestamp at which the timer becomes eligible to fire.
548 fire_at: DateTime<Utc>,
549 },
550 /// A timer fired.
551 TimerFired {
552 /// Recording metadata for this event.
553 envelope: EventEnvelope,
554 /// Timer that fired.
555 timer_id: TimerId,
556 },
557 /// A timer was cancelled as an explicit cancellation outcome.
558 TimerCancelled {
559 /// Recording metadata for this event.
560 envelope: EventEnvelope,
561 /// Timer that was cancelled.
562 timer_id: TimerId,
563 /// Who retired the timer. Decides reopen behavior: a
564 /// [`TimerCancelCause::CancelTeardown`] cancellation is re-armed when the
565 /// run is reopened; a [`TimerCancelCause::WorkflowIntent`] cancellation is
566 /// permanent.
567 ///
568 /// Replay-safety: histories recorded before this field existed have no
569 /// `cause` key. Decode defaults the missing value to
570 /// [`TimerCancelCause::WorkflowIntent`] via `#[serde(default)]` — the
571 /// pre-field behavior (never resurrected), never panics, never differs
572 /// run-to-run. The encoding of the existing fields is untouched.
573 #[serde(default)]
574 cause: TimerCancelCause,
575 },
576 /// A `with_timeout` operation reached a durable terminal outcome.
577 WithTimeoutCompleted {
578 /// Recording metadata for this event.
579 envelope: EventEnvelope,
580 /// Timer that bounded the operation.
581 timer_id: TimerId,
582 /// Recorded timeout outcome.
583 outcome: WithTimeoutOutcome,
584 /// JSON-encoded BEAM term payload for completed operation results.
585 result: Option<Payload>,
586 },
587 /// A signal was delivered to the workflow.
588 SignalReceived {
589 /// Recording metadata for this event.
590 envelope: EventEnvelope,
591 /// Signal name selected by the sender.
592 name: String,
593 /// Opaque signal payload.
594 payload: Payload,
595 },
596 /// A signal was sent by this workflow to another workflow.
597 SignalSent {
598 /// Recording metadata for this event.
599 envelope: EventEnvelope,
600 /// Target workflow identifier selected by workflow code.
601 target_workflow_id: WorkflowId,
602 /// Signal name selected by workflow code.
603 name: String,
604 /// Opaque signal payload.
605 payload: Payload,
606 },
607 /// A child workflow was started.
608 ChildWorkflowStarted {
609 /// Recording metadata for this event.
610 envelope: EventEnvelope,
611 /// Child workflow identifier.
612 child_workflow_id: WorkflowId,
613 /// Child workflow type selected by the parent.
614 workflow_type: String,
615 /// Opaque child workflow input payload.
616 input: Payload,
617 /// Package version resolved for the child at record time.
618 ///
619 /// The crash-repair sweep and the child's own start use exactly this
620 /// recorded version, so the crash path resolves identically to the
621 /// crash-free path.
622 package_version: PackageVersion,
623 },
624 /// A child workflow completed successfully.
625 ChildWorkflowCompleted {
626 /// Recording metadata for this event.
627 envelope: EventEnvelope,
628 /// Child workflow that produced the result.
629 child_workflow_id: WorkflowId,
630 /// Opaque child workflow result payload.
631 result: Payload,
632 },
633 /// A child workflow failed terminally.
634 ChildWorkflowFailed {
635 /// Recording metadata for this event.
636 envelope: EventEnvelope,
637 /// Child workflow that failed.
638 child_workflow_id: WorkflowId,
639 /// Terminal child workflow failure.
640 error: WorkflowError,
641 },
642 /// A child workflow was cancelled as an explicit cancellation outcome.
643 ChildWorkflowCancelled {
644 /// Recording metadata for this event.
645 envelope: EventEnvelope,
646 /// Child workflow that was cancelled.
647 child_workflow_id: WorkflowId,
648 },
649 /// A schedule resource was created.
650 ScheduleCreated {
651 /// Recording metadata for this event.
652 envelope: EventEnvelope,
653 /// Schedule resource that was created.
654 schedule_id: ScheduleId,
655 /// Persisted schedule configuration.
656 config: ScheduleConfig,
657 },
658 /// A schedule resource was updated.
659 ScheduleUpdated {
660 /// Recording metadata for this event.
661 envelope: EventEnvelope,
662 /// Schedule resource that was updated.
663 schedule_id: ScheduleId,
664 /// Updated schedule configuration.
665 config: ScheduleConfig,
666 },
667 /// A schedule resource was paused.
668 SchedulePaused {
669 /// Recording metadata for this event.
670 envelope: EventEnvelope,
671 /// Schedule resource that was paused.
672 schedule_id: ScheduleId,
673 },
674 /// A paused schedule resource was resumed.
675 ScheduleResumed {
676 /// Recording metadata for this event.
677 envelope: EventEnvelope,
678 /// Schedule resource that was resumed.
679 schedule_id: ScheduleId,
680 },
681 /// A schedule resource was deleted.
682 ScheduleDeleted {
683 /// Recording metadata for this event.
684 envelope: EventEnvelope,
685 /// Schedule resource that was deleted.
686 schedule_id: ScheduleId,
687 },
688 /// A schedule tick started a workflow execution.
689 ScheduleTriggered {
690 /// Recording metadata for this event.
691 envelope: EventEnvelope,
692 /// Schedule resource that fired.
693 schedule_id: ScheduleId,
694 /// Workflow execution started by the schedule tick.
695 workflow_id: WorkflowId,
696 /// Run started by the schedule tick.
697 run_id: RunId,
698 },
699 /// An engine-side cadence window fired for a workloop (workloop brief
700 /// R1.4/R4.3): the dead-man clock ticked and the fire was durably recorded
701 /// through the loop's single Recorder, exactly as any other asynchronous
702 /// arrival. Nonterminal and status-invisible.
703 CadenceFired {
704 /// Recording metadata for this event.
705 envelope: EventEnvelope,
706 /// One-based sequence number of the cadence window that fired.
707 window_seq: u64,
708 },
709 /// A workloop iteration closed its bounded history generation (R3.1). The
710 /// iteration's terminal routes land as health samples against the loop's
711 /// invariants per the declared confirms mapping (R3.3). Nonterminal and
712 /// status-invisible: the accompanying [`Event::WorkflowContinuedAsNew`]
713 /// carries the generation boundary itself.
714 IterationClosed {
715 /// Recording metadata for this event.
716 envelope: EventEnvelope,
717 /// Routes the iteration took, in order; the last is its terminal.
718 routes: Vec<String>,
719 /// Health samples derived from the routes against the loop's
720 /// invariants — every invariant is sampled on the same tick (R2.2).
721 health_samples: Vec<crate::HealthSample>,
722 },
723 /// A workloop was retired: the declared, recorded way to stop that is not
724 /// failure (R2.5). Nonterminal and status-invisible on its own — the
725 /// terminal is the [`Event::WorkflowCompleted`] recorded in the SAME
726 /// append, so retirement reads as an intentional stop, never an outage,
727 /// without any new terminal machinery or status variant.
728 LoopRetired {
729 /// Recording metadata for this event.
730 envelope: EventEnvelope,
731 /// Declared retirement reason, named at the point of stopping.
732 reason: String,
733 },
734 /// A detached top-level workflow was hatched by this run (R13.1). NOT a
735 /// child: no lifecycle tie, no supervision edge, no awaited terminal — the
736 /// hatched workflow outlives the hatching iteration and owes it nothing.
737 /// Nonterminal and status-invisible.
738 WorkflowHatched {
739 /// Recording metadata for this event.
740 envelope: EventEnvelope,
741 /// Deterministic identity of the hatched workflow, derived by
742 /// [`crate::hatch_workflow_id`] from (namespace, workflow type, key) —
743 /// so a retry or replay re-mints the SAME id and a duplicate hatch is
744 /// a recorded no-op returning it.
745 child_workflow_id: WorkflowId,
746 /// Dedupe key derived from the observed subject (e.g. a task id).
747 key: String,
748 },
749 /// The ONE alarm path (R4.2): an invariant is not confirmed held. A missed
750 /// window, a red sample, a dead loop, and duration-form silence are all
751 /// THIS event — cause is a field, never a separate alarm channel.
752 /// Nonterminal and status-invisible; trigger selectors (Leg 3) arm on
753 /// named causes as allowlists (R5.2a).
754 InvariantUnconfirmed {
755 /// Recording metadata for this event.
756 envelope: EventEnvelope,
757 /// Invariant that is not confirmed held.
758 invariant: String,
759 /// Why confirmation is missing — the evidence class, named.
760 cause: crate::AlarmCause,
761 /// Cadence window at which tolerance was exceeded; `None` on a
762 /// signal-only loop, which has no windows.
763 window_seq: Option<u64>,
764 /// When the invariant was last confirmed, if ever — part of the
765 /// alarm's completeness claim (R4.4): what was sampled and when.
766 last_confirmed_at: Option<DateTime<Utc>>,
767 /// Consecutive unconfirmed samples/windows observed at alarm time —
768 /// the other half of the completeness claim.
769 consecutive_unconfirmed: u64,
770 },
771}
772
773/// Durable terminal outcome for a `with_timeout` operation.
774#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq, Eq)]
775pub enum WithTimeoutOutcome {
776 /// The operation closure returned before the deadline.
777 OperationCompleted,
778 /// The deadline fired before the operation completed.
779 TimedOut,
780}
781
782/// Who retired a durable timer, recorded on [`Event::TimerCancelled`].
783///
784/// The distinction decides reopen semantics. A timer the WORKFLOW retired —
785/// an SDK `cancel_timer` call or a `with_timeout` scope settling because the
786/// racing operation won — is a business fact: reopen must never resurrect it.
787/// A timer the ENGINE retired while tearing down a cancelled run
788/// (`Engine::cancel`'s in-flight timer cleanup) is bookkeeping: the deadline
789/// itself was never reached or waived, so reopening the run re-arms it at its
790/// original `fire_at`.
791#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Copy, Debug, Default, PartialEq, Eq)]
792pub enum TimerCancelCause {
793 /// Workflow code retired the timer (SDK cancel or a settled timeout scope).
794 ///
795 /// The serde default: histories recorded before this field existed decode
796 /// as workflow intent, preserving their pre-field never-resurrected
797 /// behavior.
798 #[default]
799 WorkflowIntent,
800 /// The engine retired the timer while cancelling its workflow run.
801 CancelTeardown,
802}
803
804impl Event {
805 /// Returns the envelope recorded with this event.
806 #[must_use]
807 pub const fn envelope(&self) -> &EventEnvelope {
808 match self {
809 Self::WorkflowStarted { envelope, .. }
810 | Self::WorkflowCompleted { envelope, .. }
811 | Self::WorkflowFailed { envelope, .. }
812 | Self::WorkflowCancelled { envelope, .. }
813 | Self::WorkflowTimedOut { envelope, .. }
814 | Self::WorkflowContinuedAsNew { envelope, .. }
815 | Self::WorkflowReopened { envelope, .. }
816 | Self::WorkflowPaused { envelope, .. }
817 | Self::WorkflowResumed { envelope, .. }
818 | Self::SearchAttributesUpdated { envelope, .. }
819 | Self::ActivityScheduled { envelope, .. }
820 | Self::ActivityStarted { envelope, .. }
821 | Self::ActivityAdoptionOffered { envelope, .. }
822 | Self::ActivityCompleted { envelope, .. }
823 | Self::ActivityFailed { envelope, .. }
824 | Self::ActivityAdvisoryExhausted { envelope, .. }
825 | Self::ActivityFallbackRouted { envelope, .. }
826 | Self::ActivityCancelled { envelope, .. }
827 | Self::TimerStarted { envelope, .. }
828 | Self::TimerFired { envelope, .. }
829 | Self::TimerCancelled { envelope, .. }
830 | Self::WithTimeoutCompleted { envelope, .. }
831 | Self::SignalReceived { envelope, .. }
832 | Self::SignalSent { envelope, .. }
833 | Self::ChildWorkflowStarted { envelope, .. }
834 | Self::ChildWorkflowCompleted { envelope, .. }
835 | Self::ChildWorkflowFailed { envelope, .. }
836 | Self::ChildWorkflowCancelled { envelope, .. }
837 | Self::ScheduleCreated { envelope, .. }
838 | Self::ScheduleUpdated { envelope, .. }
839 | Self::SchedulePaused { envelope, .. }
840 | Self::ScheduleResumed { envelope, .. }
841 | Self::ScheduleDeleted { envelope, .. }
842 | Self::ScheduleTriggered { envelope, .. }
843 | Self::CadenceFired { envelope, .. }
844 | Self::IterationClosed { envelope, .. }
845 | Self::LoopRetired { envelope, .. }
846 | Self::WorkflowHatched { envelope, .. }
847 | Self::InvariantUnconfirmed { envelope, .. } => envelope,
848 }
849 }
850
851 /// Returns the monotonic sequence number recorded for this event.
852 #[must_use]
853 pub const fn seq(&self) -> u64 {
854 self.envelope().seq
855 }
856
857 /// Returns the deterministic recorded timestamp for this event.
858 #[must_use]
859 pub const fn recorded_at(&self) -> &DateTime<Utc> {
860 &self.envelope().recorded_at
861 }
862
863 /// Returns the workflow history that owns this event.
864 #[must_use]
865 pub const fn workflow_id(&self) -> &WorkflowId {
866 &self.envelope().workflow_id
867 }
868}
869
870#[cfg(test)]
871mod tests {
872 use std::collections::HashMap;
873
874 use chrono::{DateTime, Utc};
875 use serde_json::json;
876
877 use super::{
878 DEFAULT_TASK_QUEUE, Event, EventEnvelope, LEGACY_ACTIVITY_ATTEMPT, TimerCancelCause,
879 };
880 use crate::{
881 ActivityError, ActivityErrorKind, ActivityId, CatchUpPolicy, OverlapPolicy, PackageVersion,
882 Payload, RunId, ScheduleConfig, ScheduleId, SearchAttributeValue, TimerId, TriggerSpec,
883 WorkflowError, WorkflowId,
884 };
885
886 fn package_version() -> PackageVersion {
887 PackageVersion::new("a".repeat(64))
888 }
889
890 fn recorded_at() -> DateTime<Utc> {
891 DateTime::from_timestamp(1_700_000_000, 123_000_000).unwrap_or_default()
892 }
893
894 fn envelope(seq: u64) -> EventEnvelope {
895 EventEnvelope {
896 seq,
897 recorded_at: recorded_at(),
898 workflow_id: WorkflowId::new(uuid::Uuid::nil()),
899 }
900 }
901
902 fn payload(label: &str) -> Result<Payload, crate::PayloadError> {
903 Payload::from_json(&json!({ "label": label }))
904 }
905
906 fn schedule_config(label: &str) -> Result<ScheduleConfig, crate::PayloadError> {
907 Ok(ScheduleConfig {
908 trigger: TriggerSpec::Cron {
909 expression: String::from("0 0 * * *"),
910 },
911 overlap_policy: OverlapPolicy::Skip,
912 catch_up_policy: CatchUpPolicy::One,
913 workflow_type: String::from("checkout"),
914 input: payload(label)?,
915 search_attributes: HashMap::from([(
916 String::from("aion.namespace"),
917 crate::SearchAttributeValue::String(String::from("tenant-a")),
918 )]),
919 })
920 }
921
922 fn workflow_error(message: &str) -> WorkflowError {
923 WorkflowError {
924 message: String::from(message),
925 details: None,
926 }
927 }
928
929 fn activity_error(kind: ActivityErrorKind, message: &str) -> ActivityError {
930 ActivityError {
931 kind,
932 message: String::from(message),
933 details: None,
934 }
935 }
936
937 fn round_trip(event: &Event) -> Result<(), serde_json::Error> {
938 let json = serde_json::to_string(event)?;
939 let decoded = serde_json::from_str::<Event>(&json)?;
940 assert_eq!(*event, decoded);
941 Ok(())
942 }
943
944 /// NSTQ-3: a recorded `ActivityScheduled` carries its `task_queue` through the durable JSON
945 /// wire so reopen/recovery can re-target the same pool.
946 #[test]
947 fn activity_scheduled_records_and_reads_back_its_task_queue()
948 -> Result<(), Box<dyn std::error::Error>> {
949 let event = Event::ActivityScheduled {
950 envelope: envelope(6),
951 activity_id: ActivityId::from_sequence_position(6),
952 activity_type: String::from("charge-card"),
953 input: payload("activity-input")?,
954 task_queue: String::from("claude"),
955 node: None,
956 };
957
958 let json = serde_json::to_string(&event)?;
959 let decoded = serde_json::from_str::<Event>(&json)?;
960
961 match decoded {
962 Event::ActivityScheduled { task_queue, .. } => {
963 assert_eq!(
964 task_queue, "claude",
965 "the recorded task queue must survive the round-trip"
966 );
967 }
968 other => return Err(format!("expected ActivityScheduled, got {other:?}").into()),
969 }
970 Ok(())
971 }
972
973 /// NSTQ-3 replay-safety (the load-bearing test): an OLD recorded history that has no
974 /// `task_queue` key on its `ActivityScheduled` events MUST still decode, defaulting the missing
975 /// value to the named `"default"` task queue, deterministically — never panic, never differ
976 /// run-to-run. The old wire form is the exact pre-field bytes: the current serialization with
977 /// the `task_queue` key removed.
978 #[test]
979 fn activity_scheduled_decodes_old_history_without_task_queue_as_default()
980 -> Result<(), Box<dyn std::error::Error>> {
981 // Build a current event, serialize, then strip the `task_queue` key to reconstruct exactly
982 // what a history recorded before the field existed looks like on the wire.
983 let current = Event::ActivityScheduled {
984 envelope: envelope(6),
985 activity_id: ActivityId::from_sequence_position(6),
986 activity_type: String::from("charge-card"),
987 input: payload("activity-input")?,
988 task_queue: String::from("ignored-when-stripped"),
989 node: Some(String::from("ignored-when-stripped")),
990 };
991 let mut value = serde_json::to_value(¤t)?;
992 let data = value
993 .get_mut("data")
994 .and_then(serde_json::Value::as_object_mut)
995 .ok_or("ActivityScheduled must serialize to a tagged object with a `data` map")?;
996 assert!(
997 data.remove("task_queue").is_some(),
998 "the current wire form must contain task_queue before we strip it"
999 );
1000
1001 // Decode the stripped (old-shape) wire form repeatedly: it must succeed and always read
1002 // back the named default, deterministically.
1003 let old_wire = serde_json::to_string(&value)?;
1004 for _ in 0..4 {
1005 let decoded = serde_json::from_str::<Event>(&old_wire)?;
1006 match &decoded {
1007 Event::ActivityScheduled { task_queue, .. } => {
1008 assert_eq!(
1009 task_queue, DEFAULT_TASK_QUEUE,
1010 "a missing task_queue must default to the named default queue"
1011 );
1012 assert_eq!(task_queue, "default");
1013 }
1014 other => return Err(format!("expected ActivityScheduled, got {other:?}").into()),
1015 }
1016 }
1017 Ok(())
1018 }
1019
1020 /// NODE-3: a recorded `ActivityScheduled` carries its OPTIONAL `node` affinity through the
1021 /// durable JSON wire so reopen/recovery can re-target the same node.
1022 #[test]
1023 fn activity_scheduled_records_and_reads_back_its_node() -> Result<(), Box<dyn std::error::Error>>
1024 {
1025 let event = Event::ActivityScheduled {
1026 envelope: envelope(6),
1027 activity_id: ActivityId::from_sequence_position(6),
1028 activity_type: String::from("charge-card"),
1029 input: payload("activity-input")?,
1030 task_queue: String::from("claude"),
1031 node: Some(String::from("box-7")),
1032 };
1033
1034 let json = serde_json::to_string(&event)?;
1035 let decoded = serde_json::from_str::<Event>(&json)?;
1036
1037 match decoded {
1038 Event::ActivityScheduled { node, .. } => {
1039 assert_eq!(
1040 node.as_deref(),
1041 Some("box-7"),
1042 "the recorded node affinity must survive the round-trip"
1043 );
1044 }
1045 other => return Err(format!("expected ActivityScheduled, got {other:?}").into()),
1046 }
1047 Ok(())
1048 }
1049
1050 /// NODE-3 replay-safety (the load-bearing test): an OLD recorded history that has no `node` key
1051 /// on its `ActivityScheduled` events MUST still decode, defaulting the missing value to `None`
1052 /// (no affinity) deterministically — never a sentinel, never panic, never differ run-to-run.
1053 /// The old wire form is the exact pre-field bytes: the current serialization with the `node`
1054 /// key removed.
1055 #[test]
1056 fn activity_scheduled_decodes_old_history_without_node_as_none()
1057 -> Result<(), Box<dyn std::error::Error>> {
1058 // Build a current event with a node set, serialize, then strip the `node` key to
1059 // reconstruct exactly what a history recorded before the field existed looks like on the
1060 // wire.
1061 let current = Event::ActivityScheduled {
1062 envelope: envelope(6),
1063 activity_id: ActivityId::from_sequence_position(6),
1064 activity_type: String::from("charge-card"),
1065 input: payload("activity-input")?,
1066 task_queue: String::from("default"),
1067 node: Some(String::from("ignored-when-stripped")),
1068 };
1069 let mut value = serde_json::to_value(¤t)?;
1070 let data = value
1071 .get_mut("data")
1072 .and_then(serde_json::Value::as_object_mut)
1073 .ok_or("ActivityScheduled must serialize to a tagged object with a `data` map")?;
1074 assert!(
1075 data.remove("node").is_some(),
1076 "the current wire form must contain node before we strip it"
1077 );
1078
1079 // Decode the stripped (old-shape) wire form repeatedly: it must succeed and always read
1080 // back `None`, deterministically.
1081 let old_wire = serde_json::to_string(&value)?;
1082 for _ in 0..4 {
1083 let decoded = serde_json::from_str::<Event>(&old_wire)?;
1084 match &decoded {
1085 Event::ActivityScheduled { node, .. } => {
1086 assert_eq!(
1087 *node, None,
1088 "a missing node must default to None (no affinity)"
1089 );
1090 }
1091 other => return Err(format!("expected ActivityScheduled, got {other:?}").into()),
1092 }
1093 }
1094 Ok(())
1095 }
1096
1097 /// NOI-0 positive round-trip: `ActivityStarted`, `ActivityCompleted`, and `ActivityCancelled`
1098 /// each carry a genuine one-based `attempt` through the durable JSON wire, so replay reads back
1099 /// the same attempt that was recorded — a completed activity has one consistent attempt readable
1100 /// off BOTH its start and its terminal (the invariant the NOI design keys on).
1101 #[test]
1102 fn activity_lifecycle_records_and_reads_back_its_attempt()
1103 -> Result<(), Box<dyn std::error::Error>> {
1104 let started = Event::ActivityStarted {
1105 envelope: envelope(7),
1106 activity_id: ActivityId::from_sequence_position(6),
1107 attempt: 3,
1108 };
1109 let completed = Event::ActivityCompleted {
1110 envelope: envelope(8),
1111 activity_id: ActivityId::from_sequence_position(6),
1112 result: payload("activity-result")?,
1113 attempt: 3,
1114 };
1115 let cancelled = Event::ActivityCancelled {
1116 envelope: envelope(9),
1117 activity_id: ActivityId::from_sequence_position(6),
1118 attempt: 3,
1119 };
1120
1121 for event in [&started, &completed, &cancelled] {
1122 round_trip(event)?;
1123 }
1124
1125 // Read the attempt back off each decoded terminal — it must be the recorded value, not the
1126 // legacy sentinel.
1127 match serde_json::from_str::<Event>(&serde_json::to_string(&started)?)? {
1128 Event::ActivityStarted { attempt, .. } => assert_eq!(attempt, 3),
1129 other => return Err(format!("expected ActivityStarted, got {other:?}").into()),
1130 }
1131 match serde_json::from_str::<Event>(&serde_json::to_string(&completed)?)? {
1132 Event::ActivityCompleted { attempt, .. } => assert_eq!(attempt, 3),
1133 other => return Err(format!("expected ActivityCompleted, got {other:?}").into()),
1134 }
1135 match serde_json::from_str::<Event>(&serde_json::to_string(&cancelled)?)? {
1136 Event::ActivityCancelled { attempt, .. } => assert_eq!(attempt, 3),
1137 other => return Err(format!("expected ActivityCancelled, got {other:?}").into()),
1138 }
1139 Ok(())
1140 }
1141
1142 /// NOI-0 replay-safety (the load-bearing negative control): an OLD recorded history that has no
1143 /// `attempt` key on its `ActivityStarted` / `ActivityCompleted` / `ActivityCancelled` events MUST
1144 /// still decode without panic, defaulting the missing value to the legacy sentinel
1145 /// [`LEGACY_ACTIVITY_ATTEMPT`] (`0`) deterministically — never differ run-to-run. Because real
1146 /// attempts are one-based, `0` can never collide with a genuine attempt. The old wire form is the
1147 /// exact pre-field bytes: the current serialization with the `attempt` key removed.
1148 #[test]
1149 fn activity_lifecycle_decodes_old_history_without_attempt_as_legacy_sentinel()
1150 -> Result<(), Box<dyn std::error::Error>> {
1151 // One current event per variant, each with a NON-sentinel attempt so we can prove the strip
1152 // (not the value) is what drives the default on decode.
1153 let started = Event::ActivityStarted {
1154 envelope: envelope(7),
1155 activity_id: ActivityId::from_sequence_position(6),
1156 attempt: 5,
1157 };
1158 let completed = Event::ActivityCompleted {
1159 envelope: envelope(8),
1160 activity_id: ActivityId::from_sequence_position(6),
1161 result: payload("activity-result")?,
1162 attempt: 5,
1163 };
1164 let cancelled = Event::ActivityCancelled {
1165 envelope: envelope(9),
1166 activity_id: ActivityId::from_sequence_position(6),
1167 attempt: 5,
1168 };
1169
1170 // Strip the `attempt` key from each to reconstruct exactly what a pre-NOI-0 history looks
1171 // like on the wire, then decode the stripped form repeatedly: it must succeed and always read
1172 // back the legacy sentinel, deterministically.
1173 for current in [&started, &completed, &cancelled] {
1174 let mut value = serde_json::to_value(current)?;
1175 let data = value
1176 .get_mut("data")
1177 .and_then(serde_json::Value::as_object_mut)
1178 .ok_or("activity lifecycle event must serialize to a tagged object with `data`")?;
1179 assert!(
1180 data.remove("attempt").is_some(),
1181 "the current wire form must contain attempt before we strip it"
1182 );
1183 let old_wire = serde_json::to_string(&value)?;
1184 for _ in 0..4 {
1185 let decoded = serde_json::from_str::<Event>(&old_wire)?;
1186 let attempt = match &decoded {
1187 Event::ActivityStarted { attempt, .. }
1188 | Event::ActivityCompleted { attempt, .. }
1189 | Event::ActivityCancelled { attempt, .. } => *attempt,
1190 other => {
1191 return Err(
1192 format!("expected an activity lifecycle event, got {other:?}").into(),
1193 );
1194 }
1195 };
1196 assert_eq!(
1197 attempt, LEGACY_ACTIVITY_ATTEMPT,
1198 "a missing attempt must default to the legacy sentinel (0)"
1199 );
1200 assert_eq!(attempt, 0);
1201 }
1202 }
1203 Ok(())
1204 }
1205
1206 /// Replay-safety proof for the `cause` field on `TimerCancelled` (#222):
1207 /// a history recorded BEFORE the field existed has no `cause` key and MUST
1208 /// decode without panic, defaulting to `WorkflowIntent` — the pre-field
1209 /// behavior (a reopen never resurrects it) — deterministically. The old
1210 /// wire form is the exact pre-field bytes: the current serialization with
1211 /// the `cause` key removed.
1212 #[test]
1213 fn timer_cancelled_decodes_old_history_without_cause_as_workflow_intent()
1214 -> Result<(), Box<dyn std::error::Error>> {
1215 // A NON-default cause proves the strip (not the value) drives the default.
1216 let cancelled = Event::TimerCancelled {
1217 envelope: envelope(7),
1218 timer_id: TimerId::named("deadline")?,
1219 cause: TimerCancelCause::CancelTeardown,
1220 };
1221
1222 let mut value = serde_json::to_value(&cancelled)?;
1223 let data = value
1224 .get_mut("data")
1225 .and_then(serde_json::Value::as_object_mut)
1226 .ok_or("TimerCancelled must serialize to a tagged object with `data`")?;
1227 assert!(
1228 data.remove("cause").is_some(),
1229 "the current wire form must contain cause before we strip it"
1230 );
1231 let old_wire = serde_json::to_string(&value)?;
1232 for _ in 0..4 {
1233 let decoded = serde_json::from_str::<Event>(&old_wire)?;
1234 match &decoded {
1235 Event::TimerCancelled { cause, .. } => assert_eq!(
1236 *cause,
1237 TimerCancelCause::WorkflowIntent,
1238 "a missing cause must default to WorkflowIntent (never resurrected)"
1239 ),
1240 other => {
1241 return Err(format!("expected TimerCancelled, got {other:?}").into());
1242 }
1243 }
1244 }
1245 Ok(())
1246 }
1247
1248 /// Pause/resume (#204) round-trip: the two new NON-terminal lifecycle markers
1249 /// carry plain fields and survive the durable JSON wire unchanged.
1250 #[test]
1251 fn pause_resume_events_round_trip_through_json() -> Result<(), Box<dyn std::error::Error>> {
1252 let events = vec![
1253 Event::WorkflowPaused {
1254 envelope: envelope(2),
1255 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1256 reason: Some(String::from("operator hold")),
1257 operator: Some(String::from("tom")),
1258 },
1259 Event::WorkflowPaused {
1260 envelope: envelope(3),
1261 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1262 reason: None,
1263 operator: None,
1264 },
1265 Event::WorkflowResumed {
1266 envelope: envelope(4),
1267 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1268 operator: Some(String::from("tom")),
1269 },
1270 ];
1271 for event in &events {
1272 round_trip(event)?;
1273 }
1274 Ok(())
1275 }
1276
1277 /// GATE-6 back-compat: an OLD history serialized before pause/resume existed
1278 /// decodes byte-identically — it simply never contains the new variants. We
1279 /// prove the whole event enum still decodes an old-shape history with no new
1280 /// variants present (the decode round-trip test the brief requires), and that
1281 /// adding the variants did not change the encoding of any existing variant.
1282 #[test]
1283 fn old_history_without_pause_resume_decodes_unchanged() -> Result<(), Box<dyn std::error::Error>>
1284 {
1285 let started = Event::WorkflowStarted {
1286 envelope: envelope(1),
1287 workflow_type: String::from("checkout"),
1288 input: payload("input")?,
1289 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1290 parent_run_id: None,
1291 parent_workflow_id: None,
1292 package_version: package_version(),
1293 };
1294 let completed = Event::WorkflowCompleted {
1295 envelope: envelope(2),
1296 result: payload("result")?,
1297 };
1298 // Serialize an old-shape history and decode it back: no new variant is
1299 // present, and every existing variant round-trips exactly.
1300 let history = vec![started, completed];
1301 let json = serde_json::to_string(&history)?;
1302 let decoded = serde_json::from_str::<Vec<Event>>(&json)?;
1303 assert_eq!(history, decoded);
1304 Ok(())
1305 }
1306
1307 /// #144: the start-time task queue projects from the `aion.task_queue`
1308 /// search attribute recorded by `SearchAttributesUpdated`, mirroring the
1309 /// `aion.namespace` projection. A later update overrides an earlier value.
1310 #[test]
1311 fn start_time_task_queue_projects_from_recorded_attribute()
1312 -> Result<(), Box<dyn std::error::Error>> {
1313 use super::{START_TIME_TASK_QUEUE_ATTRIBUTE, start_time_task_queue};
1314 use crate::SearchAttributeValue;
1315
1316 let events = vec![
1317 Event::WorkflowStarted {
1318 envelope: envelope(1),
1319 workflow_type: String::from("checkout"),
1320 input: payload("input")?,
1321 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1322 parent_run_id: None,
1323 parent_workflow_id: None,
1324 package_version: package_version(),
1325 },
1326 Event::SearchAttributesUpdated {
1327 envelope: envelope(2),
1328 workflow_id: WorkflowId::new(uuid::Uuid::nil()),
1329 attributes: HashMap::from([(
1330 START_TIME_TASK_QUEUE_ATTRIBUTE.to_owned(),
1331 SearchAttributeValue::String(String::from("gpu")),
1332 )]),
1333 },
1334 ];
1335
1336 assert_eq!(start_time_task_queue(&events).as_deref(), Some("gpu"));
1337 Ok(())
1338 }
1339
1340 /// #144 back-compat: a history with no recorded `aion.task_queue` attribute
1341 /// projects `None`, so callers fall back to the named default.
1342 #[test]
1343 fn start_time_task_queue_is_none_without_the_attribute()
1344 -> Result<(), Box<dyn std::error::Error>> {
1345 use super::start_time_task_queue;
1346
1347 let events = vec![Event::WorkflowStarted {
1348 envelope: envelope(1),
1349 workflow_type: String::from("checkout"),
1350 input: payload("input")?,
1351 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1352 parent_run_id: None,
1353 parent_workflow_id: None,
1354 package_version: package_version(),
1355 }];
1356
1357 assert_eq!(start_time_task_queue(&events), None);
1358 Ok(())
1359 }
1360
1361 /// #211: the display name projects from the `aion.display_name` search
1362 /// attribute recorded by `SearchAttributesUpdated`, mirroring the
1363 /// `aion.task_queue` projection.
1364 #[test]
1365 fn display_name_projects_from_recorded_attribute() -> Result<(), Box<dyn std::error::Error>> {
1366 use super::{DISPLAY_NAME_ATTRIBUTE, display_name};
1367 use crate::SearchAttributeValue;
1368
1369 let events = vec![
1370 Event::WorkflowStarted {
1371 envelope: envelope(1),
1372 workflow_type: String::from("checkout"),
1373 input: payload("input")?,
1374 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1375 parent_run_id: None,
1376 parent_workflow_id: None,
1377 package_version: package_version(),
1378 },
1379 Event::SearchAttributesUpdated {
1380 envelope: envelope(2),
1381 workflow_id: WorkflowId::new(uuid::Uuid::nil()),
1382 attributes: HashMap::from([(
1383 DISPLAY_NAME_ATTRIBUTE.to_owned(),
1384 SearchAttributeValue::String(String::from("Nightly settlement")),
1385 )]),
1386 },
1387 ];
1388
1389 assert_eq!(display_name(&events).as_deref(), Some("Nightly settlement"));
1390 Ok(())
1391 }
1392
1393 /// #211 back-compat: a history with no recorded `aion.display_name`
1394 /// attribute projects `None`, so the unnamed run renders as its bare UUID.
1395 #[test]
1396 fn display_name_is_none_without_the_attribute() -> Result<(), Box<dyn std::error::Error>> {
1397 use super::display_name;
1398
1399 let events = vec![Event::WorkflowStarted {
1400 envelope: envelope(1),
1401 workflow_type: String::from("checkout"),
1402 input: payload("input")?,
1403 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1404 parent_run_id: None,
1405 parent_workflow_id: None,
1406 package_version: package_version(),
1407 }];
1408
1409 assert_eq!(display_name(&events), None);
1410 Ok(())
1411 }
1412
1413 /// #211 rename invariant: a later `SearchAttributesUpdated` overrides the
1414 /// earlier name (last write wins) while BOTH remain in history — a rename
1415 /// is a recorded event, never an overwrite of the past.
1416 #[test]
1417 fn display_name_later_update_overrides_earlier() -> Result<(), Box<dyn std::error::Error>> {
1418 use super::{DISPLAY_NAME_ATTRIBUTE, display_name};
1419 use crate::SearchAttributeValue;
1420
1421 let name_event = |seq: u64, name: &str| Event::SearchAttributesUpdated {
1422 envelope: envelope(seq),
1423 workflow_id: WorkflowId::new(uuid::Uuid::nil()),
1424 attributes: HashMap::from([(
1425 DISPLAY_NAME_ATTRIBUTE.to_owned(),
1426 SearchAttributeValue::String(String::from(name)),
1427 )]),
1428 };
1429 let events = vec![
1430 Event::WorkflowStarted {
1431 envelope: envelope(1),
1432 workflow_type: String::from("checkout"),
1433 input: payload("input")?,
1434 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1435 parent_run_id: None,
1436 parent_workflow_id: None,
1437 package_version: package_version(),
1438 },
1439 name_event(2, "first name"),
1440 name_event(3, "second name"),
1441 ];
1442
1443 assert_eq!(display_name(&events).as_deref(), Some("second name"));
1444 // NOTE: the "history keeps both names" half of this invariant is NOT
1445 // asserted here. `events` is this test's own literal, so counting it
1446 // would measure the `vec!` rather than any behaviour. It is proven
1447 // where it can be — against events a real recorder appended — by
1448 // `aion/tests/rename_e2e.rs`
1449 // `renaming_twice_supersedes_while_history_keeps_both_names`.
1450 Ok(())
1451 }
1452
1453 #[test]
1454 fn event_accessors_return_envelope_fields() -> Result<(), Box<dyn std::error::Error>> {
1455 let workflow_id = WorkflowId::new_v4();
1456 let recorded_at = recorded_at();
1457 let envelope = EventEnvelope {
1458 seq: 17,
1459 recorded_at,
1460 workflow_id: workflow_id.clone(),
1461 };
1462 let event = Event::WorkflowStarted {
1463 envelope,
1464 workflow_type: String::from("checkout"),
1465 input: payload("input")?,
1466 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1467 parent_run_id: None,
1468 parent_workflow_id: None,
1469 package_version: package_version(),
1470 };
1471
1472 assert_eq!(event.seq(), 17);
1473 assert_eq!(event.recorded_at(), &recorded_at);
1474 assert_eq!(event.workflow_id(), &workflow_id);
1475 Ok(())
1476 }
1477
1478 #[test]
1479 fn events_round_trip_through_json() -> Result<(), Box<dyn std::error::Error>> {
1480 let fire_at = DateTime::from_timestamp(1_700_000_100, 0).unwrap_or_default();
1481 let events = vec![
1482 Event::WorkflowStarted {
1483 envelope: envelope(1),
1484 workflow_type: String::from("checkout"),
1485 input: payload("workflow-input")?,
1486 run_id: RunId::new(uuid::Uuid::from_u128(1)),
1487 parent_run_id: None,
1488 parent_workflow_id: None,
1489 package_version: package_version(),
1490 },
1491 Event::WorkflowCompleted {
1492 envelope: envelope(2),
1493 result: payload("workflow-result")?,
1494 },
1495 Event::WorkflowFailed {
1496 envelope: envelope(3),
1497 error: workflow_error("workflow failed"),
1498 },
1499 Event::WorkflowCancelled {
1500 envelope: envelope(4),
1501 reason: String::from("caller requested cancellation"),
1502 },
1503 Event::WorkflowTimedOut {
1504 envelope: envelope(5),
1505 timeout: String::from("execution"),
1506 },
1507 Event::ActivityScheduled {
1508 envelope: envelope(6),
1509 activity_id: ActivityId::from_sequence_position(6),
1510 activity_type: String::from("charge-card"),
1511 input: payload("activity-input")?,
1512 task_queue: String::from("claude"),
1513 node: Some(String::from("box-7")),
1514 },
1515 Event::ActivityStarted {
1516 envelope: envelope(7),
1517 activity_id: ActivityId::from_sequence_position(6),
1518 attempt: 1,
1519 },
1520 Event::ActivityCompleted {
1521 envelope: envelope(8),
1522 activity_id: ActivityId::from_sequence_position(6),
1523 result: payload("activity-result")?,
1524 attempt: 1,
1525 },
1526 Event::ActivityFailed {
1527 envelope: envelope(9),
1528 activity_id: ActivityId::from_sequence_position(6),
1529 error: activity_error(ActivityErrorKind::Retryable, "temporary outage"),
1530 attempt: 1,
1531 },
1532 Event::ActivityCancelled {
1533 envelope: envelope(10),
1534 activity_id: ActivityId::from_sequence_position(6),
1535 attempt: 1,
1536 },
1537 Event::TimerStarted {
1538 envelope: envelope(11),
1539 timer_id: TimerId::anonymous(11),
1540 fire_at,
1541 },
1542 Event::TimerFired {
1543 envelope: envelope(12),
1544 timer_id: TimerId::anonymous(11),
1545 },
1546 Event::TimerCancelled {
1547 envelope: envelope(13),
1548 timer_id: TimerId::named("reminder")?,
1549 cause: TimerCancelCause::WorkflowIntent,
1550 },
1551 Event::SignalReceived {
1552 envelope: envelope(14),
1553 name: String::from("approve"),
1554 payload: payload("signal")?,
1555 },
1556 Event::SignalSent {
1557 envelope: envelope(15),
1558 target_workflow_id: WorkflowId::new(uuid::Uuid::from_u128(5)),
1559 name: String::from("approve"),
1560 payload: payload("signal-sent")?,
1561 },
1562 ];
1563
1564 for event in events {
1565 round_trip(&event)?;
1566 }
1567 Ok(())
1568 }
1569
1570 #[test]
1571 fn child_events_round_trip_through_json() -> Result<(), Box<dyn std::error::Error>> {
1572 let child_workflow_id = WorkflowId::new(uuid::Uuid::from_u128(1));
1573 let events = vec![
1574 Event::ChildWorkflowStarted {
1575 envelope: envelope(16),
1576 child_workflow_id: child_workflow_id.clone(),
1577 workflow_type: String::from("fulfillment"),
1578 input: payload("child-input")?,
1579 package_version: package_version(),
1580 },
1581 Event::ChildWorkflowCompleted {
1582 envelope: envelope(16),
1583 child_workflow_id: child_workflow_id.clone(),
1584 result: payload("child-result")?,
1585 },
1586 Event::ChildWorkflowFailed {
1587 envelope: envelope(17),
1588 child_workflow_id: child_workflow_id.clone(),
1589 error: workflow_error("child failed"),
1590 },
1591 Event::ChildWorkflowCancelled {
1592 envelope: envelope(18),
1593 child_workflow_id,
1594 },
1595 ];
1596
1597 for event in events {
1598 round_trip(&event)?;
1599 }
1600 Ok(())
1601 }
1602
1603 #[test]
1604 fn extended_events_round_trip_through_json() -> Result<(), Box<dyn std::error::Error>> {
1605 let schedule_id = ScheduleId::new(uuid::Uuid::from_u128(2));
1606 let triggered_workflow_id = WorkflowId::new(uuid::Uuid::from_u128(3));
1607 let triggered_run_id = RunId::new(uuid::Uuid::from_u128(4));
1608 let events = vec![
1609 Event::WorkflowContinuedAsNew {
1610 envelope: envelope(19),
1611 input: payload("continued-input")?,
1612 workflow_type: Some(String::from("checkout-v2")),
1613 parent_run_id: RunId::new(uuid::Uuid::from_u128(2)),
1614 },
1615 Event::SearchAttributesUpdated {
1616 envelope: envelope(20),
1617 workflow_id: WorkflowId::new(uuid::Uuid::nil()),
1618 attributes: HashMap::from([(
1619 String::from("customer_id"),
1620 SearchAttributeValue::String(String::from("cust-123")),
1621 )]),
1622 },
1623 Event::ScheduleCreated {
1624 envelope: envelope(20),
1625 schedule_id: schedule_id.clone(),
1626 config: schedule_config("schedule-created")?,
1627 },
1628 Event::ScheduleUpdated {
1629 envelope: envelope(21),
1630 schedule_id: schedule_id.clone(),
1631 config: schedule_config("schedule-updated")?,
1632 },
1633 Event::SchedulePaused {
1634 envelope: envelope(22),
1635 schedule_id: schedule_id.clone(),
1636 },
1637 Event::ScheduleResumed {
1638 envelope: envelope(23),
1639 schedule_id: schedule_id.clone(),
1640 },
1641 Event::ScheduleDeleted {
1642 envelope: envelope(24),
1643 schedule_id: schedule_id.clone(),
1644 },
1645 Event::ScheduleTriggered {
1646 envelope: envelope(25),
1647 schedule_id,
1648 workflow_id: triggered_workflow_id,
1649 run_id: triggered_run_id,
1650 },
1651 ];
1652
1653 for event in events {
1654 round_trip(&event)?;
1655 }
1656 Ok(())
1657 }
1658
1659 #[test]
1660 fn workloop_events_round_trip_through_json() -> Result<(), Box<dyn std::error::Error>> {
1661 let events = vec![
1662 Event::CadenceFired {
1663 envelope: envelope(26),
1664 window_seq: 41,
1665 },
1666 Event::IterationClosed {
1667 envelope: envelope(27),
1668 routes: vec![String::from("sweep"), String::from("start")],
1669 health_samples: vec![crate::HealthSample {
1670 invariant: String::from("serving"),
1671 status: crate::HealthStatus::Confirmed,
1672 window_seq: Some(41),
1673 }],
1674 },
1675 Event::LoopRetired {
1676 envelope: envelope(28),
1677 reason: String::from("queue decommissioned; drained on operator signal"),
1678 },
1679 Event::WorkflowHatched {
1680 envelope: envelope(29),
1681 child_workflow_id: crate::hatch_workflow_id("default", "process_task", "task-7")?,
1682 key: String::from("task-7"),
1683 },
1684 Event::InvariantUnconfirmed {
1685 envelope: envelope(30),
1686 invariant: String::from("serving"),
1687 cause: crate::AlarmCause::WindowMissed,
1688 window_seq: Some(42),
1689 last_confirmed_at: Some(recorded_at()),
1690 consecutive_unconfirmed: 4,
1691 },
1692 Event::InvariantUnconfirmed {
1693 envelope: envelope(31),
1694 invariant: String::from("serving"),
1695 cause: crate::AlarmCause::UnconfirmedUnknown,
1696 window_seq: None,
1697 last_confirmed_at: None,
1698 consecutive_unconfirmed: 0,
1699 },
1700 ];
1701
1702 for event in events {
1703 round_trip(&event)?;
1704 }
1705 Ok(())
1706 }
1707}