stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Integrity-bound inputs for one deterministic graph checkpoint barrier.
//!
//! The graph runtime computes state reduction and routing before opening a
//! storage transaction. This module binds that successor write to the exact
//! immutable result set it consumed. Durable adapters must reload the full
//! base checkpoint and every result, then compare these compact identities
//! before committing the successor.

use std::{collections::BTreeSet, fmt};

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{
    Deserialize, Deserializer, Serialize,
    de::{self, SeqAccess},
};
use thiserror::Error;

use crate::{
    Checkpoint, CheckpointHead, CheckpointWrite, Digest, GraphNamespace, NodeId,
    PendingNodeResultHead, ReadyNodes,
};

const BARRIER_INTENT_DIGEST_DOMAIN: &[u8] = b"stateknot-checkpoint-barrier-intent-v1\0";

/// Canonically ordered, duplicate-free result heads consumed by one barrier.
///
/// The collection is deliberately compact. Full pending results can be large
/// and must be loaded and reduced in bounded pages before constructing the
/// barrier. Storage rechecks these heads against immutable rows under the run
/// lock.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BarrierResultHeads(Box<[PendingNodeResultHead]>);

impl BarrierResultHeads {
    /// Maximum results in one barrier, equal to the checkpoint ready-set bound.
    pub const MAX_LEN: usize = ReadyNodes::MAX_LEN;

    /// Validates, canonicalizes, and constructs a non-empty result-head set.
    ///
    /// Values serialize in ascending `(graph_namespace, node_id)` order.
    /// V1 checkpoint ready sets describe the root graph, so nested namespaces
    /// are rejected until nested checkpoint scheduling has its own exact set.
    ///
    /// # Errors
    ///
    /// Returns [`BarrierResultHeadsError`] for an empty or oversized set,
    /// duplicate activation keys, mixed base checkpoints, or nested results.
    pub fn try_new<I>(values: I) -> Result<Self, BarrierResultHeadsError>
    where
        I: IntoIterator<Item = PendingNodeResultHead>,
    {
        let mut collected = Vec::new();
        let mut identities = BTreeSet::new();
        let mut base: Option<CheckpointHead> = None;
        for value in values {
            if collected.len() == Self::MAX_LEN {
                return Err(BarrierResultHeadsError::TooMany {
                    maximum: Self::MAX_LEN,
                    actual: Self::MAX_LEN + 1,
                });
            }
            let activation = value.activation();
            if !activation.graph_namespace().is_root() {
                return Err(BarrierResultHeadsError::NestedGraphNamespace {
                    namespace: activation.graph_namespace().clone(),
                });
            }
            if let Some(expected) = &base {
                if activation.base_checkpoint() != expected {
                    return Err(BarrierResultHeadsError::MixedBaseCheckpoints);
                }
            } else {
                base = Some(activation.base_checkpoint().clone());
            }
            let identity = (
                activation.graph_namespace().clone(),
                activation.node_id().clone(),
            );
            if !identities.insert(identity.clone()) {
                return Err(BarrierResultHeadsError::DuplicateActivation {
                    namespace: identity.0,
                    node_id: identity.1,
                });
            }
            collected.push(value);
        }
        if collected.is_empty() {
            return Err(BarrierResultHeadsError::Empty);
        }
        collected.sort_unstable_by(|left, right| {
            let left = left.activation();
            let right = right.activation();
            (left.graph_namespace(), left.node_id())
                .cmp(&(right.graph_namespace(), right.node_id()))
        });
        Ok(Self(collected.into_boxed_slice()))
    }

    /// Returns the number of logical activation results.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns whether the collection is empty.
    ///
    /// Valid instances are never empty; this conventional collection method
    /// is provided so generic callers can use the type without assumptions.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Iterates result heads in canonical activation order.
    #[must_use]
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &PendingNodeResultHead> {
        self.0.iter()
    }

    /// Returns the exact canonical result-head slice.
    #[must_use]
    pub const fn as_slice(&self) -> &[PendingNodeResultHead] {
        &self.0
    }
}

impl Serialize for BarrierResultHeads {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for BarrierResultHeads {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(BarrierResultHeadsVisitor)
    }
}

struct BarrierResultHeadsVisitor;

impl<'de> de::Visitor<'de> for BarrierResultHeadsVisitor {
    type Value = BarrierResultHeads;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "one to {} unique root-graph pending result heads",
            BarrierResultHeads::MAX_LEN
        )
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut values = Vec::with_capacity(
            sequence
                .size_hint()
                .unwrap_or_default()
                .min(BarrierResultHeads::MAX_LEN),
        );
        while let Some(value) = sequence.next_element::<PendingNodeResultHead>()? {
            if values.len() == BarrierResultHeads::MAX_LEN {
                return Err(de::Error::custom(BarrierResultHeadsError::TooMany {
                    maximum: BarrierResultHeads::MAX_LEN,
                    actual: BarrierResultHeads::MAX_LEN + 1,
                }));
            }
            values.push(value);
        }
        BarrierResultHeads::try_new(values).map_err(de::Error::custom)
    }
}

impl JsonSchema for BarrierResultHeads {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "BarrierResultHeads".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::BarrierResultHeads").into()
    }

    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "array",
            "items": generator.subschema_for::<PendingNodeResultHead>(),
            "minItems": 1,
            "maxItems": 1024,
            "uniqueItems": true,
            "description": "Serialized in canonical (graph_namespace, node_id) order; runtime additionally requires one exact base checkpoint."
        })
    }
}

/// Invalid compact result set for a checkpoint barrier.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum BarrierResultHeadsError {
    /// A barrier with no active nodes cannot advance a checkpoint.
    #[error("checkpoint barrier result set must not be empty")]
    Empty,
    /// The ready-set hard ceiling was exceeded.
    #[error("checkpoint barrier contains {actual} results; maximum is {maximum}")]
    TooMany {
        /// Absolute maximum.
        maximum: usize,
        /// First observed count beyond the maximum.
        actual: usize,
    },
    /// Two results named the same logical activation.
    #[error("checkpoint barrier contains a duplicate logical activation")]
    DuplicateActivation {
        /// Duplicated graph namespace.
        namespace: GraphNamespace,
        /// Duplicated node identity.
        node_id: NodeId,
    },
    /// Result heads did not share one exact immutable base checkpoint.
    #[error("checkpoint barrier result heads use different base checkpoints")]
    MixedBaseCheckpoints,
    /// V1 root-ready checkpoints cannot consume nested graph results.
    #[error("checkpoint barrier result belongs to an unsupported nested graph namespace")]
    NestedGraphNamespace {
        /// Rejected nested namespace.
        namespace: GraphNamespace,
    },
}

/// Integrity-bound semantic input to one successor checkpoint transaction.
///
/// This value binds the authentic base checkpoint head and ready set, every
/// pending result consumed, and the exact successor write intent. It does not
/// execute reducers or graph callbacks; those must finish before construction.
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CheckpointBarrier {
    base_checkpoint: CheckpointHead,
    base_ready_nodes: ReadyNodes,
    result_heads: BarrierResultHeads,
    successor: CheckpointWrite,
    intent_digest: Digest,
}

impl CheckpointBarrier {
    /// Constructs a barrier from a fully validated base checkpoint.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointBarrierError`] unless results exactly cover the
    /// base ready set, the successor names that exact base, and integrity bytes
    /// can be canonicalized.
    pub fn new<I>(
        base: &Checkpoint,
        successor: CheckpointWrite,
        result_heads: I,
    ) -> Result<Self, CheckpointBarrierError>
    where
        I: IntoIterator<Item = PendingNodeResultHead>,
    {
        let result_heads =
            BarrierResultHeads::try_new(result_heads).map_err(CheckpointBarrierError::results)?;
        Self::build(
            base.head(),
            base.ready_nodes().clone(),
            result_heads,
            successor,
            None,
        )
    }

    /// Restores a serialized barrier and verifies its complete intent digest.
    ///
    /// Durable adapters must additionally compare `base_ready_nodes` with the
    /// fully restored base checkpoint before accepting this compact proof.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointBarrierError`] for any structural or integrity
    /// mismatch.
    pub fn restore(
        base_checkpoint: CheckpointHead,
        base_ready_nodes: ReadyNodes,
        result_heads: BarrierResultHeads,
        successor: CheckpointWrite,
        intent_digest: Digest,
    ) -> Result<Self, CheckpointBarrierError> {
        Self::build(
            base_checkpoint,
            base_ready_nodes,
            result_heads,
            successor,
            Some(intent_digest),
        )
    }

    fn build(
        base_checkpoint: CheckpointHead,
        base_ready_nodes: ReadyNodes,
        result_heads: BarrierResultHeads,
        successor: CheckpointWrite,
        supplied_digest: Option<Digest>,
    ) -> Result<Self, CheckpointBarrierError> {
        validate_barrier_shape(
            &base_checkpoint,
            &base_ready_nodes,
            &result_heads,
            &successor,
        )?;
        let intent_digest = compute_barrier_digest(&CheckpointBarrierDigestWire {
            base_checkpoint: &base_checkpoint,
            base_ready_nodes: &base_ready_nodes,
            result_heads: &result_heads,
            successor_intent_digest: successor.intent_digest(),
        })?;
        if supplied_digest.is_some_and(|supplied| supplied != intent_digest) {
            return Err(CheckpointBarrierError::DigestMismatch);
        }
        Ok(Self {
            base_checkpoint,
            base_ready_nodes,
            result_heads,
            successor,
            intent_digest,
        })
    }

    /// Returns the exact base checkpoint compact identity.
    #[must_use]
    pub const fn base_checkpoint(&self) -> &CheckpointHead {
        &self.base_checkpoint
    }

    /// Returns the ready set copied from the validated base checkpoint.
    #[must_use]
    pub const fn base_ready_nodes(&self) -> &ReadyNodes {
        &self.base_ready_nodes
    }

    /// Returns exact consumed result identities in stable reduction order.
    #[must_use]
    pub const fn result_heads(&self) -> &BarrierResultHeads {
        &self.result_heads
    }

    /// Returns the exact successor checkpoint write intent.
    #[must_use]
    pub const fn successor(&self) -> &CheckpointWrite {
        &self.successor
    }

    /// Consumes the barrier and returns its successor write intent.
    #[must_use]
    pub fn into_successor(self) -> CheckpointWrite {
        self.successor
    }

    /// Returns the domain-separated idempotency fingerprint.
    #[must_use]
    pub const fn intent_digest(&self) -> Digest {
        self.intent_digest
    }
}

impl fmt::Debug for CheckpointBarrier {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CheckpointBarrier")
            .field("base_checkpoint", &self.base_checkpoint)
            .field("base_ready_count", &self.base_ready_nodes.len())
            .field("result_count", &self.result_heads.len())
            .field("successor", &self.successor)
            .field("intent_digest", &self.intent_digest)
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for CheckpointBarrier {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            base_checkpoint: CheckpointHead,
            base_ready_nodes: ReadyNodes,
            result_heads: BarrierResultHeads,
            successor: CheckpointWrite,
            intent_digest: Digest,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::restore(
            wire.base_checkpoint,
            wire.base_ready_nodes,
            wire.result_heads,
            wire.successor,
            wire.intent_digest,
        )
        .map_err(de::Error::custom)
    }
}

/// Invalid or corrupted checkpoint barrier intent.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointBarrierError {
    /// The compact result collection was intrinsically invalid.
    #[error("checkpoint barrier result set is invalid: {source}")]
    Results {
        /// Exact collection error.
        #[source]
        source: BarrierResultHeadsError,
    },
    /// The copied base ready set was empty.
    #[error("checkpoint barrier base ready set must not be empty")]
    EmptyReadySet,
    /// The successor did not name the exact base checkpoint as its parent.
    #[error("checkpoint barrier successor does not name the exact base checkpoint")]
    SuccessorParentMismatch,
    /// Result heads belonged to another immutable base checkpoint.
    #[error("checkpoint barrier result does not belong to the base checkpoint")]
    ResultBaseMismatch,
    /// A result named a node outside the base ready set.
    #[error("checkpoint barrier result node was not ready in the base checkpoint")]
    UnexpectedResultNode {
        /// Rejected node identity.
        node_id: NodeId,
    },
    /// At least one ready node had no result.
    #[error("checkpoint barrier does not contain every base ready node")]
    MissingResultNode {
        /// Missing node identity.
        node_id: NodeId,
    },
    /// Canonical barrier integrity material could not be produced.
    #[error("checkpoint barrier intent integrity calculation failed: {source}")]
    Integrity {
        /// Exact canonicalization failure.
        #[source]
        source: CheckpointBarrierIntegrityError,
    },
    /// Serialized caller-controlled fields did not match the barrier digest.
    #[error("checkpoint barrier intent digest does not match its fields")]
    DigestMismatch,
}

impl CheckpointBarrierError {
    const fn results(source: BarrierResultHeadsError) -> Self {
        Self::Results { source }
    }
}

impl From<CheckpointBarrierIntegrityError> for CheckpointBarrierError {
    fn from(source: CheckpointBarrierIntegrityError) -> Self {
        Self::Integrity { source }
    }
}

fn validate_barrier_shape(
    base_checkpoint: &CheckpointHead,
    base_ready_nodes: &ReadyNodes,
    result_heads: &BarrierResultHeads,
    successor: &CheckpointWrite,
) -> Result<(), CheckpointBarrierError> {
    if base_ready_nodes.is_empty() {
        return Err(CheckpointBarrierError::EmptyReadySet);
    }
    if successor.parent() != Some(base_checkpoint) {
        return Err(CheckpointBarrierError::SuccessorParentMismatch);
    }
    let mut observed = BTreeSet::new();
    for head in result_heads.iter() {
        let activation = head.activation();
        if activation.base_checkpoint() != base_checkpoint {
            return Err(CheckpointBarrierError::ResultBaseMismatch);
        }
        if !base_ready_nodes.contains(activation.node_id()) {
            return Err(CheckpointBarrierError::UnexpectedResultNode {
                node_id: activation.node_id().clone(),
            });
        }
        observed.insert(activation.node_id().clone());
    }
    for node_id in base_ready_nodes {
        if !observed.contains(node_id) {
            return Err(CheckpointBarrierError::MissingResultNode {
                node_id: node_id.clone(),
            });
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct CheckpointBarrierDigestWire<'a> {
    base_checkpoint: &'a CheckpointHead,
    base_ready_nodes: &'a ReadyNodes,
    result_heads: &'a BarrierResultHeads,
    successor_intent_digest: Digest,
}

fn compute_barrier_digest(
    value: &CheckpointBarrierDigestWire<'_>,
) -> Result<Digest, CheckpointBarrierIntegrityError> {
    let canonical = serde_json_canonicalizer::to_vec(value)
        .map_err(|_| CheckpointBarrierIntegrityError::CanonicalSerialization)?;
    let mut preimage = Vec::with_capacity(BARRIER_INTENT_DIGEST_DOMAIN.len() + canonical.len());
    preimage.extend_from_slice(BARRIER_INTENT_DIGEST_DOMAIN);
    preimage.extend_from_slice(&canonical);
    Ok(Digest::sha256(preimage))
}

/// Failure to canonicalize checkpoint barrier integrity material.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum CheckpointBarrierIntegrityError {
    /// A closed typed checksum preimage could not be canonicalized.
    #[error("checkpoint barrier checksum preimage serialization failed")]
    CanonicalSerialization,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AttemptId, CheckpointId, FencingEpoch, GraphNamespace, JournalHead, JournalSequence,
        NodeActivation, PendingNodeResult, PendingNodeResultIntent, RunFence, Timestamp,
    };
    use serde_json::{Value, from_value, json, to_value};

    fn checkpoint() -> Checkpoint {
        let fixture: Value =
            serde_json::from_str(include_str!("../tests/fixtures/core-checkpoint-v1.json"))
                .unwrap();
        from_value(fixture["checkpoints"][0].clone()).unwrap()
    }

    fn result(base: &Checkpoint, node_id: NodeId, sequence: u64) -> PendingNodeResult {
        let activation = NodeActivation::new(
            base.head(),
            GraphNamespace::root(),
            node_id,
            Digest::sha256(sequence.to_be_bytes()),
        );
        let intent = PendingNodeResultIntent::new(
            activation.clone(),
            crate::NodeStateChange::Unchanged,
            crate::NodeControl::Continue,
            crate::NodeInvocationBindings::empty(),
        )
        .unwrap();
        let recorded_at = Timestamp::from_unix_micros(
            base.journal_head().recorded_at().unix_micros()
                + i64::try_from(sequence).unwrap() * 1_000_000,
        )
        .unwrap();
        let event_id = format!("01912345-6789-7abc-8def-0123456789{sequence:02x}")
            .parse()
            .unwrap();
        let journal = JournalHead::new(
            base.tenant_id().clone(),
            base.run_id(),
            JournalSequence::new(base.journal_head().sequence().get() + sequence).unwrap(),
            event_id,
            recorded_at,
            Digest::sha256(format!("result-{sequence}")),
        );
        let fence = RunFence::new(
            base.tenant_id().clone(),
            base.run_id(),
            format!("01912345-6789-7abc-8def-0123456788{sequence:02x}")
                .parse::<AttemptId>()
                .unwrap(),
            FencingEpoch::new(sequence).unwrap(),
        );
        PendingNodeResult::commit(intent, fence, journal).unwrap()
    }

    fn successor(base: &Checkpoint) -> CheckpointWrite {
        CheckpointWrite::successor(
            CheckpointId::generate(),
            base,
            base.state().clone(),
            ReadyNodes::empty(),
        )
        .unwrap()
    }

    #[test]
    fn exact_ready_coverage_is_canonical_and_integrity_bound() {
        let base = checkpoint();
        let heads = base
            .ready_nodes()
            .iter()
            .rev()
            .enumerate()
            .map(|(index, node_id)| {
                result(&base, node_id.clone(), u64::try_from(index + 1).unwrap()).head()
            })
            .collect::<Vec<_>>();
        let barrier = CheckpointBarrier::new(&base, successor(&base), heads).unwrap();
        assert_eq!(barrier.result_heads().len(), base.ready_nodes().len());
        assert!(
            barrier
                .result_heads()
                .iter()
                .map(|head| head.activation().node_id())
                .is_sorted()
        );
        assert_eq!(
            from_value::<CheckpointBarrier>(to_value(&barrier).unwrap()).unwrap(),
            barrier
        );

        let mut changed = to_value(&barrier).unwrap();
        changed["successor"]["ready_nodes"] = json!(["substituted"]);
        assert!(from_value::<CheckpointBarrier>(changed).is_err());
    }

    #[test]
    fn barrier_rejects_missing_unexpected_mixed_and_nested_results() {
        let base = checkpoint();
        let first = base.ready_nodes().iter().next().unwrap().clone();
        let one = result(&base, first.clone(), 1).head();
        if base.ready_nodes().len() > 1 {
            assert!(matches!(
                CheckpointBarrier::new(&base, successor(&base), [one.clone()]),
                Err(CheckpointBarrierError::MissingResultNode { .. })
            ));
        }

        let unexpected = result(&base, NodeId::new("unexpected").unwrap(), 2).head();
        assert!(matches!(
            CheckpointBarrier::new(&base, successor(&base), [unexpected]),
            Err(CheckpointBarrierError::UnexpectedResultNode { .. })
        ));

        let other_base = Checkpoint::commit(successor(&base), one.journal_head().clone()).unwrap();
        let mixed = result(&other_base, NodeId::new("nested-result").unwrap(), 3).head();
        assert!(matches!(
            BarrierResultHeads::try_new([one.clone(), mixed]),
            Err(BarrierResultHeadsError::MixedBaseCheckpoints)
        ));

        let nested_activation = NodeActivation::new(
            base.head(),
            GraphNamespace::new("nested").unwrap(),
            first,
            Digest::sha256(b"nested"),
        );
        let nested_intent = PendingNodeResultIntent::new(
            nested_activation.clone(),
            crate::NodeStateChange::Unchanged,
            crate::NodeControl::Continue,
            crate::NodeInvocationBindings::empty(),
        )
        .unwrap();
        let nested = PendingNodeResult::commit(
            nested_intent,
            RunFence::new(
                base.tenant_id().clone(),
                base.run_id(),
                "01912345-6789-7abc-8def-0123456788ff".parse().unwrap(),
                FencingEpoch::FIRST,
            ),
            one.journal_head().clone(),
        )
        .unwrap();
        assert!(matches!(
            BarrierResultHeads::try_new([nested.head()]),
            Err(BarrierResultHeadsError::NestedGraphNamespace { .. })
        ));
    }
}