ledgence-orchestration-api 0.1.1

Portable task delivery and orchestration contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! Checkpoint workflow contracts. Workflow state never occupies CloudEvent data.
//!
//! A logical activation is an ordinary leased task plus an explicit, immutable
//! continuation context. Its retries share one local-step journal. A terminal
//! controller result is interpreted as a decision only for registered activations.
use crate::*;
use ledgence_worker_api::{ProgramDescriptor, ProgramRef, validate_wire_value};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};

pub const WORKFLOW_RUNTIME_SCHEMA: &str = "ledgence.workflow.activation.v1";
pub const WORKFLOW_VERSION: u8 = 1;
pub const WORKFLOW_MAX_COMMANDS: usize = 64;
pub const WORKFLOW_MAX_LOCAL_STEPS: usize = 128;
pub const WORKFLOW_CHECKPOINT_MAX_BYTES: usize = 64 * 1024;
pub const WORKFLOW_DECISION_MAX_BYTES: usize = 256 * 1024;
pub const WORKFLOW_INPUTS_MAX_BYTES: usize = 256 * 1024;
pub const WORKFLOW_LOCAL_RECORD_MAX_BYTES: usize = 128 * 1024;
pub const WORKFLOW_LOCAL_LEDGER_MAX_BYTES: usize = 256 * 1024;
pub const WORKFLOW_CONTEXT_MAX_BYTES: usize = 640 * 1024;
pub const WORKFLOW_MAX_WORK_BATCH: u32 = 16;

/// Controller results include a platform decision envelope around application
/// values. Ordinary task output retains its depth-64 contract; registered
/// activations allow metadata depth 96 before the coordinator validates each
/// application value and the exact decision shape. The lifecycle core verifies
/// the report's activation identity against the acquired task before acceptance.
pub fn validate_task_output(output: &Value, workflow_activation: bool) -> Result<()> {
    if workflow_activation {
        ledgence_worker_api::validate_runtime_payload(output, SETTLEMENT_MAX_BYTES)?;
    } else {
        validate_wire_value(output)?;
    }
    Ok(())
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocalStepRecord {
    pub key: String,
    pub callable: String,
    #[serde(deserialize_with = "crate::observation::required_value")]
    pub input: Value,
    #[serde(deserialize_with = "crate::observation::required_value")]
    pub output: Value,
}
impl LocalStepRecord {
    pub fn validate(&self) -> Result<()> {
        validate_text(&self.key, 128)?;
        validate_text(&self.callable, 512)?;
        validate_wire_value(&self.input)?;
        validate_wire_value(&self.output)?;
        bounded(self, WORKFLOW_LOCAL_RECORD_MAX_BYTES, "local step")
    }

    /// Binding and value are immutable once acknowledged, including numeric
    /// representation (1 and 1.0 are different). JSON object order is irrelevant.
    pub fn matches(&self, other: &Self) -> Result<bool> {
        self.validate()?;
        other.validate()?;
        Ok(
            canonical_json_bytes(&serde_json::to_value(self).map_err(encoding)?)?
                == canonical_json_bytes(&serde_json::to_value(other).map_err(encoding)?)?,
        )
    }
}

/// Frozen activation inputs and checkpoint, plus the current committed journal.
/// New child completions do not mutate the frozen input batch or revision.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkflowActivationContext {
    /// Present together only for a nested owned workflow; roots omit both.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_workflow_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub root_workflow_id: Option<String>,
    pub v: u8,
    pub workflow_id: String,
    pub activation_id: String,
    pub revision: u64,
    pub continuation: String,
    #[serde(deserialize_with = "crate::observation::required_value")]
    pub state: Value,
    pub inputs: BTreeMap<String, WorkflowChildResult>,
    pub local_steps: Vec<LocalStepRecord>,
    /// Frozen result of one external event/timer wait. Absent on older contexts
    /// and ordinary child continuations; never merged into user CloudEvent data.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub wake: Option<WorkflowWake>,
}
impl WorkflowActivationContext {
    pub fn validate(&self) -> Result<()> {
        version(self.v)?;
        validate_workflow_lineage(
            Some(&self.workflow_id),
            self.parent_workflow_id.as_deref(),
            self.root_workflow_id.as_deref(),
        )?;
        validate_text(&self.activation_id, 128)?;
        validate_text(&self.continuation, 128)?;
        checkpoint(&self.state)?;
        if self.inputs.len() > WORKFLOW_MAX_COMMANDS
            || self.local_steps.len() > WORKFLOW_MAX_LOCAL_STEPS
        {
            return Err(invalid("workflow context has too many entries"));
        }
        for (key, child) in &self.inputs {
            validate_text(key, 128)?;
            child.validate()?;
        }
        if let Some(wake) = &self.wake {
            wake.validate()?;
            #[derive(Serialize)]
            struct FrozenInputs<'a> {
                inputs: &'a BTreeMap<String, WorkflowChildResult>,
                wake: &'a WorkflowWake,
            }
            bounded(
                &FrozenInputs {
                    inputs: &self.inputs,
                    wake,
                },
                WORKFLOW_INPUTS_MAX_BYTES,
                "workflow inputs and wake",
            )?;
        } else {
            bounded(&self.inputs, WORKFLOW_INPUTS_MAX_BYTES, "workflow inputs")?;
        }
        let mut keys = BTreeSet::new();
        for step in &self.local_steps {
            step.validate()?;
            if !keys.insert(&step.key) {
                return Err(invalid("duplicate local step key"));
            }
        }
        bounded(
            &self.local_steps,
            WORKFLOW_LOCAL_LEDGER_MAX_BYTES,
            "local step ledger",
        )?;
        bounded(self, WORKFLOW_CONTEXT_MAX_BYTES, "workflow context")
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkflowChildCommand {
    #[serde(default, skip_serializing_if = "WorkflowChildKind::is_task")]
    pub kind: WorkflowChildKind,
    pub key: String,
    pub program: ProgramRef,
    pub queue: String,
    #[serde(deserialize_with = "crate::observation::required_value")]
    pub data: Value,
    #[serde(default)]
    pub retry_policy: RetryPolicy,
    #[serde(default = "attempt_timeout")]
    pub attempt_timeout_ms: u64,
}
const fn attempt_timeout() -> u64 {
    300_000
}
/// Compatibility name for the original task-only command contract.
pub type WorkflowTaskCommand = WorkflowChildCommand;

impl WorkflowChildCommand {
    pub fn submission(&self, scope: &Scope, correlation_key: Option<String>) -> SubmitTask {
        SubmitTask {
            tenant_id: scope.tenant_id.clone(),
            namespace: scope.namespace.clone(),
            queue: self.queue.clone(),
            program: self.program.clone(),
            correlation_key,
            data: self.data.clone(),
            retry_policy: self.retry_policy.clone(),
            attempt_timeout_ms: self.attempt_timeout_ms,
        }
    }
    pub fn validate(&self) -> Result<()> {
        validate_text(&self.key, 128)?;
        self.submission(
            &Scope {
                tenant_id: "validation".into(),
                namespace: "validation".into(),
            },
            None,
        )
        .validate()?;
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowDecision {
    pub v: u8,
    pub activation_id: String,
    pub revision: u64,
    #[serde(flatten)]
    pub action: WorkflowAction,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum WorkflowAction {
    Wait {
        state: Value,
        continuation: String,
        commands: Vec<WorkflowChildCommand>,
        wait: WorkflowWait,
    },
    Suspend {
        state: Value,
        continuation: String,
        commands: Vec<WorkflowChildCommand>,
        /// Sealed all-terminal membership. Empty membership is immediately ready.
        until: Vec<String>,
    },
    Continue {
        state: Value,
        continuation: String,
        commands: Vec<WorkflowChildCommand>,
    },
    Complete {
        output: Value,
    },
    Fail {
        error: ApplicationError,
    },
}
impl WorkflowDecision {
    /// Strictly decode a controller outcome; serde flatten alone cannot reject
    /// unknown fields reliably, so the exact top-level shape is checked first.
    pub fn decode(value: &Value) -> Result<Self> {
        // Bound a borrowed value before deserialization clones its payload.
        bounded(value, WORKFLOW_DECISION_MAX_BYTES, "workflow decision")?;
        let object = value
            .as_object()
            .ok_or_else(|| invalid("workflow decision must be an object"))?;
        let action_fields: &[&str] = match object.get("kind").and_then(Value::as_str) {
            Some("wait") => &["state", "continuation", "commands", "wait"],
            Some("suspend") => &["state", "continuation", "commands", "until"],
            Some("continue") => &["state", "continuation", "commands"],
            Some("complete") => &["output"],
            Some("fail") => &["error"],
            _ => return Err(invalid("unknown workflow decision kind")),
        };
        let common = ["v", "activation_id", "revision", "kind"];
        if object.len() != common.len() + action_fields.len()
            || object.keys().any(|key| {
                !common.contains(&key.as_str()) && !action_fields.contains(&key.as_str())
            })
        {
            return Err(invalid("unknown or missing workflow decision fields"));
        }
        let decision: Self = serde_json::from_value(value.clone()).map_err(encoding)?;
        decision.validate()?;
        Ok(decision)
    }
    pub fn validate(&self) -> Result<()> {
        version(self.v)?;
        validate_text(&self.activation_id, 128)?;
        match &self.action {
            WorkflowAction::Wait {
                state,
                continuation,
                commands,
                wait,
            } => {
                validate_continuation(state, continuation, commands)?;
                wait.validate()?;
            }
            WorkflowAction::Suspend {
                state,
                continuation,
                commands,
                until,
            } => {
                validate_continuation(state, continuation, commands)?;
                if until.len() > WORKFLOW_MAX_COMMANDS {
                    return Err(invalid("wait membership exceeds supported limit"));
                }
                let mut keys = BTreeSet::new();
                for key in until {
                    validate_text(key, 128)?;
                    if !keys.insert(key) {
                        return Err(invalid("duplicate wait member"));
                    }
                }
            }
            WorkflowAction::Continue {
                state,
                continuation,
                commands,
            } => validate_continuation(state, continuation, commands)?,
            WorkflowAction::Complete { output } => validate_wire_value(output)?,
            WorkflowAction::Fail { error } => validate_error(error)?,
        }
        bounded(self, WORKFLOW_DECISION_MAX_BYTES, "workflow decision")
    }
    pub fn commands(&self) -> &[WorkflowChildCommand] {
        match &self.action {
            WorkflowAction::Wait { commands, .. }
            | WorkflowAction::Suspend { commands, .. }
            | WorkflowAction::Continue { commands, .. } => commands,
            WorkflowAction::Complete { .. } | WorkflowAction::Fail { .. } => &[],
        }
    }
}
fn validate_continuation(
    state: &Value,
    continuation: &str,
    commands: &[WorkflowChildCommand],
) -> Result<()> {
    checkpoint(state)?;
    validate_text(continuation, 128)?;
    if commands.len() > WORKFLOW_MAX_COMMANDS {
        return Err(invalid("workflow command batch exceeds supported limit"));
    }
    let mut keys = BTreeSet::new();
    for command in commands {
        command.validate()?;
        if !keys.insert(&command.key) {
            return Err(invalid("duplicate workflow command key"));
        }
    }
    Ok(())
}
fn checkpoint(state: &Value) -> Result<()> {
    validate_wire_value(state)?;
    bounded(state, WORKFLOW_CHECKPOINT_MAX_BYTES, "workflow checkpoint")
}
fn version(v: u8) -> Result<()> {
    if v != WORKFLOW_VERSION {
        return Err(invalid("unsupported workflow version"));
    }
    Ok(())
}
pub fn validate_workflow_error(error: &ApplicationError) -> Result<()> {
    validate_error(error)
}
fn validate_error(error: &ApplicationError) -> Result<()> {
    validate_text(&error.kind, 128)?;
    if error.message.len() > 4096 {
        return Err(invalid("workflow error message exceeds 4096 bytes"));
    }
    Ok(())
}
fn invalid(message: &str) -> ContractError {
    ContractError::InvalidInput(message.into())
}
fn encoding(error: serde_json::Error) -> ContractError {
    ContractError::InvalidInput(format!("invalid workflow JSON: {error}"))
}
fn bounded(value: &impl Serialize, bytes: usize, label: &str) -> Result<()> {
    crate::submission::check_encoded_size(value, bytes, label).map_err(Into::into)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowState {
    Running,
    Waiting,
    Failing,
    Cancelling,
    Succeeded,
    Failed,
    Cancelled,
}
impl WorkflowState {
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Succeeded | Self::Failed | Self::Cancelled)
    }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkflowSnapshot {
    /// Present together only for a nested owned workflow; roots omit both.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_workflow_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub root_workflow_id: Option<String>,
    pub workflow_id: String,
    pub scope: Scope,
    pub state: WorkflowState,
    pub revision: u64,
    #[serde(deserialize_with = "crate::observation::required_option")]
    pub activation_id: Option<String>,
    pub submitted_at: Timestamp,
    #[serde(deserialize_with = "crate::observation::required_option")]
    pub terminal_at: Option<Timestamp>,
    #[serde(deserialize_with = "crate::observation::required_option")]
    pub correlation_key: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum WorkflowOutcome {
    Succeeded {
        #[serde(deserialize_with = "crate::observation::required_value")]
        output: Value,
    },
    Failed {
        error: ApplicationError,
    },
    Cancelled {},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkflowResult {
    pub workflow: WorkflowSnapshot,
    #[serde(deserialize_with = "crate::observation::required_option")]
    pub outcome: Option<WorkflowOutcome>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocalResultCommand {
    pub owner: LeaseOwner,
    pub record: LocalStepRecord,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocalResultReceipt {
    pub key: String,
    pub already_accepted: bool,
}
#[derive(Debug, Clone)]
pub struct WorkflowWork {
    pub id: String,
    pub token: String,
    pub workflow_id: String,
    pub source: WorkflowWorkSource,
    /// Only controller outcomes are loaded here. Child completion notifications
    /// stay compact; their payloads are read once a continuation needs them.
    pub outcome: Option<TaskOutcome>,
    /// Previously registered child bindings; their pinned descriptors win on replay.
    pub resolved_children: Vec<ResolvedWorkflowChild>,
}
#[derive(Debug, Clone)]
pub struct ResolvedWorkflowChild {
    pub kind: WorkflowChildKind,
    pub key: String,
    pub descriptor: ProgramDescriptor,
}
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct WorkflowProgress {
    pub processed: u32,
    pub activations_scheduled: u32,
    /// Newly registered ordinary tasks and owned workflow runs, combined.
    pub children_scheduled: u32,
}

/// Optional workflow persistence over the same transactional authority as the
/// application's task store. Implementations must atomically create tasks and
/// dispatch obligations, append terminal completion work with task finalization,
/// and apply checkpoints/child bindings/waits with their scheduling obligations.
/// Separate, non-atomic task and workflow backends do not satisfy this port.
///
/// Child keys are workflow-scoped and retain their first normalized submission
/// and pinned descriptor. Local keys are activation-scoped: exact records may be
/// acknowledged again to their original accepting owner after expiry; a different
/// attempt must hold current live authority. New records always require that
/// authority and must be fenced against cancellation/continuation changes.
/// Frozen activation inputs and revision never change as child events arrive.
/// Work claims are bounded, leased, recoverable, and released before external
/// program resolution; application rechecks ownership and persisted source state.
/// Successful replies follow commit of every required write. Transport loss may
/// leave a committed operation whose immutable identity must be reconciled.
pub trait WorkflowStore: Send + Sync {
    /// Accept a directly addressed, one-shot event after committing its receipt.
    /// Exact source/ID/key/payload replays must reconcile before terminal checks.
    /// Implementations serialize acceptance and wait resolution under workflow
    /// authority; caller timestamps never decide event/deadline eligibility.
    fn send_workflow_event<'a>(
        &'a self,
        command: &'a WorkflowEventCommand,
    ) -> ContractFuture<'a, WorkflowEventReceipt>;
    fn lookup_workflow_submission<'a>(
        &'a self,
        scope: &'a Scope,
        key: &'a str,
    ) -> ContractFuture<'a, Option<WorkflowSnapshot>>;
    /// Replays the original normalized submission before resolving packages.
    fn replay_workflow_submission<'a>(
        &'a self,
        command: &'a SubmitCommand,
    ) -> ContractFuture<'a, Option<WorkflowSnapshot>>;
    fn accept_resolved_workflow<'a>(
        &'a self,
        command: &'a SubmitCommand,
        controller: &'a ProgramDescriptor,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
    fn workflow_status<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
    fn workflow_result<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowResult>;
    fn activation_context<'a>(
        &'a self,
        owner: &'a LeaseOwner,
    ) -> ContractFuture<'a, WorkflowActivationContext>;
    fn record_local_result<'a>(
        &'a self,
        command: &'a LocalResultCommand,
    ) -> ContractFuture<'a, LocalResultReceipt>;
    /// Work leases own coordinator application, never worker execution. Claiming
    /// releases database locks before any external descriptor resolution.
    fn claim_work(&self, limit: u32) -> ContractFuture<'_, Vec<WorkflowWork>>;
    fn apply_work<'a>(
        &'a self,
        work: &'a WorkflowWork,
        resolved: &'a [ResolvedWorkflowChild],
    ) -> ContractFuture<'a, WorkflowProgress>;
    fn retry_work<'a>(&'a self, work: &'a WorkflowWork, reason: &'a str) -> ContractFuture<'a, ()>;
    /// Persist permanent decision/application failure and drain owned children.
    fn reject_work<'a>(
        &'a self,
        work: &'a WorkflowWork,
        error: &'a ApplicationError,
    ) -> ContractFuture<'a, ()>;
    fn cancel_workflow<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
}

/// Client and interactive-worker operations. Unsupported implementations must
/// reject explicitly instead of silently submitting an ordinary task.
pub trait WorkflowService: Send + Sync {
    /// Accept a directly addressed, one-shot event after committing its receipt.
    /// Exact source/ID/key/payload replays must reconcile before terminal checks.
    /// Implementations serialize acceptance and wait resolution under workflow
    /// authority; caller timestamps never decide event/deadline eligibility.
    fn send_workflow_event<'a>(
        &'a self,
        command: &'a WorkflowEventCommand,
    ) -> ContractFuture<'a, WorkflowEventReceipt>;
    fn submit_workflow<'a>(
        &'a self,
        command: &'a SubmitCommand,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
    fn workflow_status<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
    fn workflow_result<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowResult>;
    fn activation_context<'a>(
        &'a self,
        owner: &'a LeaseOwner,
    ) -> ContractFuture<'a, WorkflowActivationContext>;
    fn record_local_result<'a>(
        &'a self,
        command: &'a LocalResultCommand,
    ) -> ContractFuture<'a, LocalResultReceipt>;
    fn cancel_workflow<'a>(
        &'a self,
        scope: &'a Scope,
        id: &'a str,
    ) -> ContractFuture<'a, WorkflowSnapshot>;
}

impl WorkflowSnapshot {
    pub fn validate(&self) -> Result<()> {
        self.scope.validate()?;
        validate_workflow_lineage(
            Some(&self.workflow_id),
            self.parent_workflow_id.as_deref(),
            self.root_workflow_id.as_deref(),
        )?;
        if let Some(id) = &self.activation_id {
            validate_text(id, 128)?;
        }
        if self.state.is_terminal() != self.terminal_at.is_some()
            || (self.state.is_terminal() && self.activation_id.is_some())
            || (self.state == WorkflowState::Running && self.activation_id.is_none())
            || self.terminal_at.is_some_and(|at| at < self.submitted_at)
            || self.submitted_at > 253_402_300_799_999
            || self.terminal_at.is_some_and(|at| at > 253_402_300_799_999)
        {
            return Err(invalid("inconsistent workflow status"));
        }
        if self
            .correlation_key
            .as_ref()
            .is_some_and(|key| key.len() > 512 || key.chars().any(char::is_control))
        {
            return Err(invalid("invalid workflow correlation key"));
        }
        Ok(())
    }
}
impl WorkflowResult {
    pub fn validate(&self) -> Result<()> {
        self.workflow.validate()?;
        match (&self.outcome, self.workflow.state) {
            (None, state) if !state.is_terminal() => Ok(()),
            (Some(WorkflowOutcome::Succeeded { output }), WorkflowState::Succeeded) => {
                validate_wire_value(output)?;
                bounded(output, WORKFLOW_DECISION_MAX_BYTES, "workflow output")
            }
            (Some(WorkflowOutcome::Failed { error }), WorkflowState::Failed) => {
                validate_error(error)
            }
            (Some(WorkflowOutcome::Cancelled {}), WorkflowState::Cancelled) => Ok(()),
            _ => Err(invalid("inconsistent workflow result")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    fn decision() -> Value {
        json!({"v":1,"activation_id":"task_1","revision":0,"kind":"suspend","continuation":"after","state":null,"commands":[],"until":[]})
    }
    #[test]
    fn decisions_require_exact_versioned_shape_and_sealed_unique_membership() {
        assert!(WorkflowDecision::decode(&decision()).is_ok());
        for (key, value) in [
            ("v", json!(2)),
            ("commands", json!(null)),
            ("until", json!(["a", "a"])),
            ("extra", json!(true)),
        ] {
            let mut input = decision();
            input[key] = value;
            assert!(WorkflowDecision::decode(&input).is_err(), "{key}");
        }
        let mut input = decision();
        input.as_object_mut().unwrap().remove("state");
        assert!(WorkflowDecision::decode(&input).is_err());
    }
    #[test]
    fn durable_local_replay_binds_numeric_representation_and_result() {
        let record = LocalStepRecord {
            key: "fetch".into(),
            callable: "app:fetch".into(),
            input: json!({"a":1,"b":2}),
            output: Value::Null,
        };
        let mut replay = record.clone();
        replay.input = json!({"b":2,"a":1});
        assert!(record.matches(&replay).unwrap());
        replay.input = json!({"a":1.0,"b":2});
        assert!(!record.matches(&replay).unwrap());
        replay = record.clone();
        replay.output = json!(false);
        assert!(!record.matches(&replay).unwrap());
    }
    #[test]
    fn oversized_checkpoints_fail_before_persistence() {
        let mut input = decision();
        input["state"] = Value::String("x".repeat(WORKFLOW_CHECKPOINT_MAX_BYTES));
        assert!(WorkflowDecision::decode(&input).is_err());
    }
    #[test]
    fn activation_context_rejects_nested_child_values_beyond_the_application_bound() {
        let mut output = Value::Null;
        for _ in 0..64 {
            output = json!([output]);
        }
        let child = WorkflowChildResult::Task(WorkflowTaskResult {
            task_id: "child".into(),
            state: TaskState::Succeeded,
            outcome: TaskOutcome::Succeeded {
                attempt_id: "attempt".into(),
                quiescence: Quiescence::Confirmed,
                execution_may_have_started: true,
                output,
            },
        });
        let mut context = WorkflowActivationContext {
            parent_workflow_id: None,
            root_workflow_id: None,
            v: 1,
            workflow_id: "workflow".into(),
            activation_id: "activation".into(),
            revision: 0,
            continuation: "next".into(),
            state: Value::Null,
            inputs: BTreeMap::from([("child".into(), child)]),
            local_steps: vec![],
            wake: None,
        };
        context.validate().unwrap();
        let WorkflowChildResult::Task(child) = context.inputs.get_mut("child").unwrap() else {
            unreachable!()
        };
        let TaskOutcome::Succeeded { output, .. } = &mut child.outcome else {
            unreachable!()
        };
        *output = json!([output.take()]);
        assert!(context.validate().is_err());
    }

    #[test]
    fn workflow_wire_distinguishes_explicit_null_from_missing_required_fields() {
        let snapshot = json!({"workflow_id":"workflow","scope":{"tenant_id":"t","namespace":"n"},"state":"waiting","revision":1,"activation_id":null,"submitted_at":1,"terminal_at":null,"correlation_key":null});
        serde_json::from_value::<WorkflowSnapshot>(snapshot.clone())
            .unwrap()
            .validate()
            .unwrap();
        for key in ["activation_id", "terminal_at", "correlation_key"] {
            let mut missing = snapshot.clone();
            missing.as_object_mut().unwrap().remove(key);
            assert!(
                serde_json::from_value::<WorkflowSnapshot>(missing).is_err(),
                "{key}"
            );
        }
        assert!(
            serde_json::from_value::<WorkflowResult>(json!({"workflow":snapshot,"outcome":null}))
                .is_ok()
        );
        assert!(serde_json::from_value::<WorkflowResult>(json!({"workflow":snapshot})).is_err());
        assert!(
            serde_json::from_value::<WorkflowOutcome>(json!({"kind":"succeeded","output":null}))
                .is_ok()
        );
        assert!(serde_json::from_value::<WorkflowOutcome>(json!({"kind":"succeeded"})).is_err());
        let local = json!({"key":"key","callable":"app:f","input":null,"output":null});
        serde_json::from_value::<LocalStepRecord>(local.clone())
            .unwrap()
            .validate()
            .unwrap();
        for key in ["input", "output"] {
            let mut missing = local.clone();
            missing.as_object_mut().unwrap().remove(key);
            assert!(serde_json::from_value::<LocalStepRecord>(missing).is_err());
        }
    }
}