stateknot-runtime 0.1.0-alpha.1

Executable registries and durable graph driving for StateKnot
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Public, integrity-verifying access to durable Agent runs and results.

use std::fmt;

use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, de};
use stateknot_core::{
    AgentResult, AgentResultProvenance, AgentResultValidationError, AgentSubmissionKey,
    BudgetUsage, Digest, Failure, FailureCategory, GraphReference, GraphSchemaValidationError,
    RetryAdvice, RunId, RunRevision, RunStatus, TenantId, Timestamp,
};
use stateknot_store_postgres::{
    AgentAdmissionCommitOutcome, AgentSubmissionCommitOutcome, PostgresStore, StoreError,
    StoredAgentAdmission,
};
use thiserror::Error;

use crate::{
    DurableAgentAdmission, DurableAgentAdmissionBuildError, DurableAgentAdmissionError,
    DurableAgentAdmissionRequest, ExecutableGraphRegistry,
};

/// A closed terminal outcome returned by the durable Agent run facade.
///
/// Successful output is the compact, schema-bound [`AgentResult`]. Failure and
/// cancellation expose only the framework's public-safe [`Failure`] plus exact
/// terminal accounting; private diagnostics remain outside this value.
#[derive(Clone, Debug, JsonSchema, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
#[non_exhaustive]
pub enum AgentRunTerminalOutcome {
    /// The Agent committed a verified final output.
    Succeeded {
        /// Schema-bound output, artifacts, provenance, and cumulative usage.
        result: AgentResult,
    },
    /// The Agent committed a non-cancellation terminal failure.
    Failed {
        /// Public-safe terminal failure occurrence.
        failure: Failure,
        /// Authoritative durable completion observation.
        completed_at: Timestamp,
        /// Complete cumulative run accounting.
        usage: BudgetUsage,
    },
    /// A previously committed cancellation request reached acknowledgement.
    Cancelled {
        /// Public-safe cancellation occurrence.
        failure: Failure,
        /// Authoritative durable acknowledgement observation.
        completed_at: Timestamp,
        /// Complete cumulative run accounting.
        usage: BudgetUsage,
    },
}

impl AgentRunTerminalOutcome {
    /// Returns the lifecycle status represented by this terminal value.
    #[must_use]
    pub const fn status(&self) -> RunStatus {
        match self {
            Self::Succeeded { .. } => RunStatus::Succeeded,
            Self::Failed { .. } => RunStatus::Failed,
            Self::Cancelled { .. } => RunStatus::Cancelled,
        }
    }

    /// Returns the authoritative durable completion observation.
    #[must_use]
    pub const fn completed_at(&self) -> Timestamp {
        match self {
            Self::Succeeded { result } => result.completed_at(),
            Self::Failed { completed_at, .. } | Self::Cancelled { completed_at, .. } => {
                *completed_at
            }
        }
    }

    /// Returns complete cumulative run accounting.
    #[must_use]
    pub const fn usage(&self) -> &BudgetUsage {
        match self {
            Self::Succeeded { result } => result.usage(),
            Self::Failed { usage, .. } | Self::Cancelled { usage, .. } => usage,
        }
    }

    /// Returns the successful result, when this is a successful outcome.
    #[must_use]
    pub const fn result(&self) -> Option<&AgentResult> {
        match self {
            Self::Succeeded { result } => Some(result),
            Self::Failed { .. } | Self::Cancelled { .. } => None,
        }
    }

    /// Returns the public failure for failed or cancelled outcomes.
    #[must_use]
    pub const fn failure(&self) -> Option<&Failure> {
        match self {
            Self::Succeeded { .. } => None,
            Self::Failed { failure, .. } | Self::Cancelled { failure, .. } => Some(failure),
        }
    }
}

impl<'de> Deserialize<'de> for AgentRunTerminalOutcome {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
        enum Wire {
            Succeeded {
                result: AgentResult,
            },
            Failed {
                failure: Failure,
                completed_at: Timestamp,
                usage: BudgetUsage,
            },
            Cancelled {
                failure: Failure,
                completed_at: Timestamp,
                usage: BudgetUsage,
            },
        }

        let outcome = match Wire::deserialize(deserializer)? {
            Wire::Succeeded { result } => Self::Succeeded { result },
            Wire::Failed {
                failure,
                completed_at,
                usage,
            } => Self::Failed {
                failure,
                completed_at,
                usage,
            },
            Wire::Cancelled {
                failure,
                completed_at,
                usage,
            } => Self::Cancelled {
                failure,
                completed_at,
                usage,
            },
        };
        if !terminal_failure_kind_matches(&outcome) {
            return Err(de::Error::custom(
                "Agent run failure does not match its terminal lifecycle path",
            ));
        }
        Ok(outcome)
    }
}

fn terminal_failure_kind_matches(outcome: &AgentRunTerminalOutcome) -> bool {
    match outcome {
        AgentRunTerminalOutcome::Succeeded { .. } => true,
        AgentRunTerminalOutcome::Failed { failure, .. } => {
            failure.category() != FailureCategory::Cancelled
        }
        AgentRunTerminalOutcome::Cancelled { failure, .. } => {
            failure.category() == FailureCategory::Cancelled
                && failure.retry_advice() == RetryAdvice::Never
        }
    }
}

/// Public, fully revalidated snapshot of one durably admitted Agent run.
///
/// The snapshot intentionally excludes authorization evidence, request input,
/// graph state, leases, scheduler internals, and private diagnostics. Pollers
/// can use `(run_id, revision)` as a monotonic observation key. A quarantined
/// run remains visible but must not be represented as executable work.
#[derive(Clone, JsonSchema, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AgentRunSnapshot {
    provenance: AgentResultProvenance,
    graph: GraphReference,
    admission_digest: Digest,
    admitted_at: Timestamp,
    revision: RunRevision,
    status: RunStatus,
    changed_at: Timestamp,
    quarantined: bool,
    #[schemars(required)]
    outcome: Option<AgentRunTerminalOutcome>,
}

impl AgentRunSnapshot {
    #[allow(clippy::too_many_arguments)]
    fn from_parts(
        provenance: AgentResultProvenance,
        graph: GraphReference,
        admission_digest: Digest,
        admitted_at: Timestamp,
        revision: RunRevision,
        status: RunStatus,
        changed_at: Timestamp,
        quarantined: bool,
        outcome: Option<AgentRunTerminalOutcome>,
    ) -> Result<Self, AgentRunSnapshotError> {
        if status == RunStatus::Pending {
            return Err(AgentRunSnapshotError::PendingAtomicAdmission);
        }
        if revision == RunRevision::ZERO {
            return Err(AgentRunSnapshotError::AdvancedAtZeroRevision);
        }
        if changed_at < admitted_at {
            return Err(AgentRunSnapshotError::ClockRegression);
        }
        validate_outcome_shape(status, outcome.as_ref(), &provenance, changed_at)?;
        Ok(Self {
            provenance,
            graph,
            admission_digest,
            admitted_at,
            revision,
            status,
            changed_at,
            quarantined,
            outcome,
        })
    }

    /// Returns trusted tenant, run, thread, invocation, and Agent identity.
    #[must_use]
    pub const fn provenance(&self) -> &AgentResultProvenance {
        &self.provenance
    }

    /// Returns the immutable graph version pinned at admission.
    #[must_use]
    pub const fn graph(&self) -> &GraphReference {
        &self.graph
    }

    /// Returns the complete immutable admission checksum.
    #[must_use]
    pub const fn admission_digest(&self) -> Digest {
        self.admission_digest
    }

    /// Returns the authoritative database admission observation.
    #[must_use]
    pub const fn admitted_at(&self) -> Timestamp {
        self.admitted_at
    }

    /// Returns the current monotonic lifecycle revision.
    #[must_use]
    pub const fn revision(&self) -> RunRevision {
        self.revision
    }

    /// Returns the current protocol-neutral lifecycle status.
    #[must_use]
    pub const fn status(&self) -> RunStatus {
        self.status
    }

    /// Returns the latest committed lifecycle observation.
    #[must_use]
    pub const fn changed_at(&self) -> Timestamp {
        self.changed_at
    }

    /// Returns whether integrity or operator policy removed this run from execution.
    #[must_use]
    pub const fn is_quarantined(&self) -> bool {
        self.quarantined
    }

    /// Returns a terminal outcome only after one has durably committed.
    #[must_use]
    pub const fn outcome(&self) -> Option<&AgentRunTerminalOutcome> {
        self.outcome.as_ref()
    }
}

impl fmt::Debug for AgentRunSnapshot {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("AgentRunSnapshot")
            .field("provenance", &self.provenance)
            .field("graph", &self.graph)
            .field("admission_digest", &self.admission_digest)
            .field("admitted_at", &self.admitted_at)
            .field("revision", &self.revision)
            .field("status", &self.status)
            .field("changed_at", &self.changed_at)
            .field("quarantined", &self.quarantined)
            .field("has_outcome", &self.outcome.is_some())
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for AgentRunSnapshot {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            provenance: AgentResultProvenance,
            graph: GraphReference,
            admission_digest: Digest,
            admitted_at: Timestamp,
            revision: RunRevision,
            status: RunStatus,
            changed_at: Timestamp,
            quarantined: bool,
            #[serde(deserialize_with = "deserialize_nullable_outcome")]
            outcome: Option<AgentRunTerminalOutcome>,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::from_parts(
            wire.provenance,
            wire.graph,
            wire.admission_digest,
            wire.admitted_at,
            wire.revision,
            wire.status,
            wire.changed_at,
            wire.quarantined,
            wire.outcome,
        )
        .map_err(de::Error::custom)
    }
}

fn deserialize_nullable_outcome<'de, D>(
    deserializer: D,
) -> Result<Option<AgentRunTerminalOutcome>, D::Error>
where
    D: Deserializer<'de>,
{
    Option::<AgentRunTerminalOutcome>::deserialize(deserializer)
}

fn validate_outcome_shape(
    status: RunStatus,
    outcome: Option<&AgentRunTerminalOutcome>,
    provenance: &AgentResultProvenance,
    changed_at: Timestamp,
) -> Result<(), AgentRunSnapshotError> {
    match (status.is_terminal(), outcome) {
        (false, None) => return Ok(()),
        (true, Some(outcome)) if outcome.status() == status => {
            if outcome.completed_at() != changed_at {
                return Err(AgentRunSnapshotError::CompletionTimeMismatch);
            }
            if !terminal_failure_kind_matches(outcome) {
                return Err(AgentRunSnapshotError::FailureKindMismatch);
            }
            if let Some(result) = outcome.result() {
                if result.provenance() != provenance {
                    return Err(AgentRunSnapshotError::ResultProvenanceMismatch);
                }
            }
            return Ok(());
        }
        _ => {}
    }
    Err(AgentRunSnapshotError::OutcomeStatusMismatch)
}

/// Invalid public Agent run snapshot shape.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum AgentRunSnapshotError {
    /// Atomic Agent admission cannot expose its internal pending precursor.
    #[error("atomic Agent admission cannot expose a pending lifecycle")]
    PendingAtomicAdmission,
    /// Every visible atomic Agent run has advanced past revision zero.
    #[error("visible Agent lifecycle cannot remain at revision zero")]
    AdvancedAtZeroRevision,
    /// A lifecycle observation preceded admission.
    #[error("Agent run change observation precedes admission")]
    ClockRegression,
    /// Terminal status and outcome presence or kind differed.
    #[error("Agent run terminal outcome does not match lifecycle status")]
    OutcomeStatusMismatch,
    /// The terminal outcome observation differed from lifecycle time.
    #[error("Agent run terminal completion does not match lifecycle time")]
    CompletionTimeMismatch,
    /// A successful result named another trusted run identity.
    #[error("Agent run result provenance does not match lifecycle provenance")]
    ResultProvenanceMismatch,
    /// Failure category or retry semantics did not match its terminal path.
    #[error("Agent run failure does not match its terminal lifecycle path")]
    FailureKindMismatch,
}

/// Result of public-facade Agent admission.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum AgentRunAdmissionOutcome {
    /// A new atomic Agent run committed.
    Committed(AgentRunSnapshot),
    /// The exact retained admission or ingress-keyed submission had already committed.
    Idempotent(AgentRunSnapshot),
}

impl AgentRunAdmissionOutcome {
    /// Returns the public verified run snapshot in either case.
    #[must_use]
    pub const fn snapshot(&self) -> &AgentRunSnapshot {
        match self {
            Self::Committed(snapshot) | Self::Idempotent(snapshot) => snapshot,
        }
    }
}

/// Public durable Agent admission and run/result query facade.
///
/// This value keeps the exact executable/schema deployment registry beside the
/// store. Every read reloads the immutable admission and current lifecycle in
/// one repeatable-read snapshot, then revalidates request, authority, initial
/// state, audit event, graph closure, terminal output, provenance, and budget
/// before returning a public value.
#[derive(Clone)]
pub struct DurableAgentRuns {
    admission: DurableAgentAdmission,
    store: PostgresStore,
    registry: ExecutableGraphRegistry,
}

impl DurableAgentRuns {
    pub(crate) const fn executable_registry(&self) -> &ExecutableGraphRegistry {
        &self.registry
    }

    /// Binds the durability provider to one immutable executable deployment.
    ///
    /// # Errors
    ///
    /// Rejects an unavailable or malformed standard Agent-admission schema.
    pub fn new(
        store: PostgresStore,
        registry: ExecutableGraphRegistry,
    ) -> Result<Self, DurableAgentRunsBuildError> {
        let admission = DurableAgentAdmission::new(store.clone(), registry.clone())?;
        Ok(Self {
            admission,
            store,
            registry,
        })
    }

    /// Validates, atomically admits, and returns a public run snapshot.
    ///
    /// Ambiguous retries must retain the exact [`DurableAgentAdmissionRequest`].
    /// Ingress idempotency-key allocation is deliberately a stronger boundary
    /// and is not emulated with process-local memory by this method.
    ///
    /// # Errors
    ///
    /// Returns a closed admission, storage, deployment, schema, integrity, or
    /// terminal-accounting failure.
    pub async fn admit(
        &self,
        request: DurableAgentAdmissionRequest,
    ) -> Result<AgentRunAdmissionOutcome, DurableAgentRunsError> {
        let outcome = self.admission.admit(request).await?;
        let committed = match &outcome {
            AgentAdmissionCommitOutcome::Committed(_) => true,
            AgentAdmissionCommitOutcome::Idempotent(_) => false,
            _ => return Err(DurableAgentRunsError::InvalidDurableSnapshot),
        };
        let snapshot = self.snapshot(outcome.stored())?;
        Ok(if committed {
            AgentRunAdmissionOutcome::Committed(snapshot)
        } else {
            AgentRunAdmissionOutcome::Idempotent(snapshot)
        })
    }

    /// Submits one candidate under a durable tenant-scoped idempotency key.
    ///
    /// Unlike [`Self::admit`], callers do not retain candidate IDs after an
    /// ambiguous response. They recreate the same caller content with the same
    /// key; the provider returns the originally selected run even if the new
    /// request contains a fresh [`crate::AgentRunIds`] bundle.
    ///
    /// # Errors
    ///
    /// Reusing a key for changed content returns a durable conflict. Deployment,
    /// schema, integrity, and database failures remain closed and payload-safe.
    pub async fn submit(
        &self,
        key: &AgentSubmissionKey,
        request: DurableAgentAdmissionRequest,
    ) -> Result<AgentRunAdmissionOutcome, DurableAgentRunsError> {
        let outcome = self.admission.submit(key, request).await?;
        let committed = match &outcome {
            AgentSubmissionCommitOutcome::Committed(_) => true,
            AgentSubmissionCommitOutcome::Idempotent(_) => false,
            _ => return Err(DurableAgentRunsError::InvalidDurableSnapshot),
        };
        let snapshot = self.snapshot(outcome.stored().admission())?;
        Ok(if committed {
            AgentRunAdmissionOutcome::Committed(snapshot)
        } else {
            AgentRunAdmissionOutcome::Idempotent(snapshot)
        })
    }

    /// Loads one tenant-scoped Agent run and fully revalidates its public view.
    ///
    /// Calling services remain responsible for authenticating and authorizing
    /// the tenant/run lookup before invoking this trusted server-side method.
    ///
    /// # Errors
    ///
    /// Returns not-found, corruption, deployment drift, schema rejection,
    /// invalid terminal accounting, or database failures.
    pub async fn load(
        &self,
        tenant_id: &TenantId,
        run_id: RunId,
    ) -> Result<AgentRunSnapshot, DurableAgentRunsError> {
        let stored = self.store.load_agent_admission(tenant_id, run_id).await?;
        self.snapshot(&stored)
    }

    /// Resolves an ingress key and loads its current fully verified public run.
    ///
    /// # Errors
    ///
    /// Returns key-not-found, corruption, deployment drift, schema rejection,
    /// invalid terminal result, or database failures.
    pub async fn load_by_key(
        &self,
        tenant_id: &TenantId,
        key: &AgentSubmissionKey,
    ) -> Result<AgentRunSnapshot, DurableAgentRunsError> {
        let stored = self.store.load_agent_submission(tenant_id, key).await?;
        self.snapshot(stored.admission())
    }

    fn snapshot(
        &self,
        stored: &StoredAgentAdmission,
    ) -> Result<AgentRunSnapshot, DurableAgentRunsError> {
        let admission = stored.admission();
        let intent = admission.intent();
        let executable = self
            .registry
            .resolve(intent.graph())
            .ok_or(DurableAgentRunsError::ExecutableGraphUnavailable)?;
        if intent.descriptor().input_schema() != executable.graph().input_schema() {
            return Err(DurableAgentRunsError::GraphInputSchemaMismatch);
        }
        if intent.descriptor().output_schema() != executable.graph().output_schema() {
            return Err(DurableAgentRunsError::GraphOutputSchemaMismatch);
        }

        self.registry
            .schemas()
            .validate_bounded(intent.request().input_schema(), intent.request().input())
            .map_err(DurableAgentRunsError::input_schema)?;
        self.registry
            .schemas()
            .validate_bounded(
                intent.authority().evidence().schema(),
                intent.authority().evidence().data(),
            )
            .map_err(DurableAgentRunsError::authority_schema)?;
        self.registry
            .schemas()
            .validate_bounded(
                stored.checkpoint().state().schema(),
                stored.checkpoint().state().data(),
            )
            .map_err(DurableAgentRunsError::initial_state_schema)?;

        let event_schema = self.admission.event_schema();
        if stored.event().payload().schema() != event_schema {
            return Err(DurableAgentRunsError::AdmissionEventSchemaMismatch);
        }
        self.registry
            .schemas()
            .validate_bounded(event_schema, stored.event().payload().data())
            .map_err(DurableAgentRunsError::event_schema)?;

        let run = stored.run();
        let lifecycle = run.lifecycle();
        let outcome = match lifecycle.status() {
            RunStatus::Succeeded => {
                let result = lifecycle
                    .result()
                    .cloned()
                    .ok_or(DurableAgentRunsError::InvalidDurableSnapshot)?;
                result
                    .validate_for(
                        intent.provenance(),
                        intent.request(),
                        intent.descriptor(),
                        intent.budget(),
                    )
                    .map_err(DurableAgentRunsError::result_validation)?;
                self.registry
                    .schemas()
                    .validate_bounded(result.output_schema(), result.output())
                    .map_err(DurableAgentRunsError::output_schema)?;
                Some(AgentRunTerminalOutcome::Succeeded { result })
            }
            RunStatus::Failed | RunStatus::Cancelled => {
                let failure = lifecycle
                    .terminal_failure()
                    .cloned()
                    .ok_or(DurableAgentRunsError::InvalidDurableSnapshot)?;
                let usage = lifecycle
                    .terminal_usage()
                    .cloned()
                    .ok_or(DurableAgentRunsError::InvalidDurableSnapshot)?;
                Some(if lifecycle.status() == RunStatus::Failed {
                    AgentRunTerminalOutcome::Failed {
                        failure,
                        completed_at: lifecycle.changed_at(),
                        usage,
                    }
                } else {
                    AgentRunTerminalOutcome::Cancelled {
                        failure,
                        completed_at: lifecycle.changed_at(),
                        usage,
                    }
                })
            }
            RunStatus::Pending
            | RunStatus::Active
            | RunStatus::Waiting
            | RunStatus::CancellationRequested => None,
        };

        AgentRunSnapshot::from_parts(
            lifecycle.provenance().clone(),
            intent.graph().clone(),
            admission.digest(),
            admission.admitted_at(),
            lifecycle.revision(),
            lifecycle.status(),
            lifecycle.changed_at(),
            run.is_quarantined(),
            outcome,
        )
        .map_err(DurableAgentRunsError::Snapshot)
    }
}

impl fmt::Debug for DurableAgentRuns {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DurableAgentRuns")
            .field("registry", &self.registry)
            .finish_non_exhaustive()
    }
}

/// Startup failure for the public durable Agent run facade.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum DurableAgentRunsBuildError {
    /// The underlying atomic admission facade could not bind its standard schema.
    #[error(transparent)]
    Admission(#[from] DurableAgentAdmissionBuildError),
}

/// Payload-redacted failure from public durable Agent admission or reads.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DurableAgentRunsError {
    /// Atomic Agent admission failed.
    #[error(transparent)]
    Admission(#[from] DurableAgentAdmissionError),
    /// Durable storage or integrity verification failed.
    #[error(transparent)]
    Store(#[from] StoreError),
    /// The exact graph and executable closure are absent from this deployment.
    #[error("Agent run graph is unavailable in the executable registry")]
    ExecutableGraphUnavailable,
    /// The Agent and executable graph disagree about input schema.
    #[error("Agent input schema does not match the executable graph input schema")]
    GraphInputSchemaMismatch,
    /// The Agent and executable graph disagree about output schema.
    #[error("Agent output schema does not match the executable graph output schema")]
    GraphOutputSchemaMismatch,
    /// Durable request input no longer passes its pinned schema.
    #[error("durable Agent input schema validation failed: {source}")]
    InputSchema {
        /// Closed schema validation result.
        #[source]
        source: GraphSchemaValidationError,
    },
    /// Durable authorization evidence no longer passes its pinned schema.
    #[error("durable Agent authorization evidence validation failed: {source}")]
    AuthoritySchema {
        /// Closed schema validation result.
        #[source]
        source: GraphSchemaValidationError,
    },
    /// Durable initial state no longer passes its pinned schema.
    #[error("durable Agent initial state schema validation failed: {source}")]
    InitialStateSchema {
        /// Closed schema validation result.
        #[source]
        source: GraphSchemaValidationError,
    },
    /// The immutable first event named another schema version.
    #[error("durable Agent admission event schema does not match this runtime release")]
    AdmissionEventSchemaMismatch,
    /// Durable admission audit data no longer passes the standard schema.
    #[error("durable Agent admission event schema validation failed: {source}")]
    EventSchema {
        /// Closed schema validation result.
        #[source]
        source: GraphSchemaValidationError,
    },
    /// A successful terminal output no longer passes its pinned schema.
    #[error("durable Agent result schema validation failed: {source}")]
    OutputSchema {
        /// Closed schema validation result.
        #[source]
        source: GraphSchemaValidationError,
    },
    /// A successful result failed provenance, request, schema, or accounting binding.
    #[error("durable Agent result integrity validation failed: {source}")]
    ResultValidation {
        /// Core result validation failure.
        #[source]
        source: AgentResultValidationError,
    },
    /// Durable facts formed a relationship the public snapshot cannot represent.
    #[error("durable Agent run snapshot is inconsistent")]
    InvalidDurableSnapshot,
    /// Public snapshot fields formed an impossible lifecycle view.
    #[error(transparent)]
    Snapshot(#[from] AgentRunSnapshotError),
}

impl DurableAgentRunsError {
    const fn input_schema(source: GraphSchemaValidationError) -> Self {
        Self::InputSchema { source }
    }

    const fn authority_schema(source: GraphSchemaValidationError) -> Self {
        Self::AuthoritySchema { source }
    }

    const fn initial_state_schema(source: GraphSchemaValidationError) -> Self {
        Self::InitialStateSchema { source }
    }

    const fn event_schema(source: GraphSchemaValidationError) -> Self {
        Self::EventSchema { source }
    }

    const fn output_schema(source: GraphSchemaValidationError) -> Self {
        Self::OutputSchema { source }
    }

    const fn result_validation(source: AgentResultValidationError) -> Self {
        Self::ResultValidation { source }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn terminal_status_requires_the_matching_outcome_kind() {
        assert_eq!(
            validate_outcome_shape(
                RunStatus::Succeeded,
                None,
                &test_provenance(),
                test_timestamp(),
            ),
            Err(AgentRunSnapshotError::OutcomeStatusMismatch)
        );
        assert!(
            validate_outcome_shape(
                RunStatus::Active,
                None,
                &test_provenance(),
                test_timestamp(),
            )
            .is_ok()
        );
    }

    #[test]
    fn snapshot_schema_is_a_closed_response_object() {
        let schema = serde_json::to_value(schemars::schema_for!(AgentRunSnapshot)).unwrap();
        assert_eq!(
            schema["additionalProperties"],
            serde_json::Value::Bool(false)
        );
        assert_eq!(schema["required"].as_array().map(Vec::len), Some(9));
    }

    #[test]
    fn terminal_failure_kind_remains_bound_to_its_lifecycle_path() {
        use stateknot_core::{FailureCode, FailureId, FailureMessage, FailureOrigin};

        let failure = Failure::new(
            FailureId::generate(),
            FailureCategory::Internal,
            FailureCode::new("agent.public_failed").unwrap(),
            FailureOrigin::new("test.public-run").unwrap(),
            FailureMessage::new("The public run failed safely.").unwrap(),
            RetryAdvice::Never,
        )
        .unwrap();
        let outcome = AgentRunTerminalOutcome::Cancelled {
            failure,
            completed_at: test_timestamp(),
            usage: BudgetUsage::zero(),
        };
        assert_eq!(
            validate_outcome_shape(
                RunStatus::Cancelled,
                Some(&outcome),
                &test_provenance(),
                test_timestamp(),
            ),
            Err(AgentRunSnapshotError::FailureKindMismatch)
        );
        assert!(
            serde_json::from_value::<AgentRunTerminalOutcome>(
                serde_json::to_value(&outcome).unwrap()
            )
            .is_err()
        );
    }

    fn test_provenance() -> AgentResultProvenance {
        use stateknot_core::{
            CapabilityIdentity, CapabilityName, CapabilityReference, IssuerId, PrincipalIdentity,
            SubjectId, Version,
        };

        AgentResultProvenance::new(
            TenantId::new("tenant-public-run").unwrap(),
            RunId::generate(),
            stateknot_core::ThreadId::generate(),
            stateknot_core::InvocationId::generate(),
            CapabilityIdentity::new(
                PrincipalIdentity::new(
                    IssuerId::new("https://issuer.example.com").unwrap(),
                    SubjectId::new("public-run-tests").unwrap(),
                ),
                CapabilityReference::new(
                    CapabilityName::new("agent.public-run").unwrap(),
                    Version::new(1, 0, 0),
                ),
            ),
        )
    }

    fn test_timestamp() -> Timestamp {
        "2026-09-01T00:00:00.000000Z".parse().unwrap()
    }
}