oxide-batch-core 0.5.0

Internal OxideBatch implementation crate; use oxide-batch instead
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
//! Restart-relevant job-definition identity.

use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fmt;

use serde_json::json;
use sha2::{Digest, Sha256};

use crate::{ChunkSize, JobName, StateSchemaId, StateSchemaVersion, StepName};

const MAX_TOKEN_BYTES: usize = 128;
/// The maximum number of nodes one plan may contain.
///
/// The bound is a framework capability rather than durable meaning: the
/// manifest reader enforces the ceiling of the running build, and raising it
/// in a later release must not change a fingerprint.
pub const MAX_NODES: usize = 1_024;
/// The maximum number of transitions one plan may contain.
///
/// The bound is a framework capability, on the same terms as [`MAX_NODES`].
pub const MAX_TRANSITIONS: usize = 4_096;
pub(crate) const MAX_MANIFEST_BYTES: usize = 64 * 1024;
/// The canonical manifest format for a one-step tasklet or chunk definition.
pub const MANIFEST_FORMAT_ONE_STEP: u16 = 1;
/// The canonical manifest format that captures a compiled flow graph.
pub const MANIFEST_FORMAT_FLOW: u16 = 2;
/// The canonical manifest format for bounded M4 local-scale plans.
pub const MANIFEST_FORMAT_LOCAL_SCALE: u16 = 3;
/// The newest canonical manifest format this runtime can interpret.
pub(crate) const SUPPORTED_MANIFEST_FORMAT: u16 = MANIFEST_FORMAT_LOCAL_SCALE;
const LEGACY_REVISION: &str = "__m1_repository_port_v1";
const LEGACY_MANIFEST: &[u8] =
    br#"{"format":1,"repository_port":"m1","revision":"__m1_repository_port_v1"}"#;

/// Validates one application-owned definition token.
///
/// # Errors
///
/// Rejects empty values, values longer than 128 UTF-8 bytes, surrounding
/// whitespace, and control characters.
pub fn validate_token(value: &str, kind: DefinitionTokenKind) -> Result<(), DefinitionError> {
    if value.is_empty() {
        return Err(DefinitionError::EmptyToken { kind });
    }
    if value.len() > MAX_TOKEN_BYTES {
        return Err(DefinitionError::TokenTooLong {
            kind,
            max_bytes: MAX_TOKEN_BYTES,
        });
    }
    if value.trim() != value {
        return Err(DefinitionError::SurroundingWhitespace { kind });
    }
    if value.chars().any(char::is_control) {
        return Err(DefinitionError::ControlCharacter { kind });
    }
    Ok(())
}

/// Declares one validated, application-owned definition token type.
///
/// The generated constructor calls [`validate_token`] and returns
/// [`DefinitionError`], both of which must be in scope where the macro is
/// invoked.
#[macro_export]
macro_rules! definition_token {
    ($name:ident, $kind:expr, $docs:literal) => {
        #[doc = $docs]
        #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
        pub struct $name(String);

        impl $name {
            /// Validates and constructs the token.
            ///
            /// # Errors
            ///
            /// Rejects empty values, values longer than 128 UTF-8 bytes,
            /// surrounding whitespace, and control characters.
            pub fn new(value: impl Into<String>) -> Result<Self, DefinitionError> {
                let value = value.into();
                validate_token(&value, $kind)?;
                Ok(Self(value))
            }

            /// Borrows the validated token.
            #[must_use]
            pub fn as_str(&self) -> &str {
                &self.0
            }
        }
    };
}

definition_token!(
    DefinitionRevision,
    DefinitionTokenKind::Revision,
    "An application-owned audit label for one restart-relevant definition."
);
definition_token!(
    DefinitionUpgradeKey,
    DefinitionTokenKind::Upgrade,
    "An application-owned key for one directed definition compatibility edge."
);

/// One source-to-target durable step mapping for a compatible restart.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StepDefinitionUpgrade {
    source: StepName,
    target: StepName,
}

impl StepDefinitionUpgrade {
    /// Constructs one directed step mapping.
    #[must_use]
    pub const fn new(source: StepName, target: StepName) -> Self {
        Self { source, target }
    }

    /// Borrows the checkpoint-producing step name.
    #[must_use]
    pub const fn source(&self) -> &StepName {
        &self.source
    }

    /// Borrows the step name in the proposed definition.
    #[must_use]
    pub const fn target(&self) -> &StepName {
        &self.target
    }
}

/// One explicit, directed definition compatibility edge.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DefinitionUpgrade {
    key: DefinitionUpgradeKey,
    from: DefinitionIdentity,
    to: DefinitionIdentity,
    step_mapping: BTreeMap<StepName, StepName>,
}

impl DefinitionUpgrade {
    /// Validates a direct, non-transitive compatibility edge.
    ///
    /// The M2 edge preserves checkpoint and context bytes unchanged, so
    /// applications may use it only when the mapped steps retain the same
    /// state schemas and semantics.
    ///
    /// # Errors
    ///
    /// Rejects self-edges, empty mappings, and mappings that reuse a target.
    pub fn new(
        key: DefinitionUpgradeKey,
        from: DefinitionIdentity,
        to: DefinitionIdentity,
        steps: impl IntoIterator<Item = StepDefinitionUpgrade>,
    ) -> Result<Self, DefinitionError> {
        if from.manifest_digest() == to.manifest_digest() {
            return Err(DefinitionError::UpgradeSelfEdge);
        }
        let mut step_mapping = BTreeMap::new();
        let mut targets = BTreeSet::new();
        for step in steps {
            if step_mapping
                .insert(step.source().clone(), step.target().clone())
                .is_some()
            {
                return Err(DefinitionError::DuplicateSourceStep);
            }
            if !targets.insert(step.target().clone()) {
                return Err(DefinitionError::DuplicateTargetStep);
            }
        }
        if step_mapping.is_empty() {
            return Err(DefinitionError::EmptyStepMapping);
        }
        Ok(Self {
            key,
            from,
            to,
            step_mapping,
        })
    }

    /// Borrows the application-owned upgrade key.
    #[must_use]
    pub const fn key(&self) -> &DefinitionUpgradeKey {
        &self.key
    }

    /// Borrows the checkpoint-producing definition.
    #[must_use]
    pub const fn from(&self) -> &DefinitionIdentity {
        &self.from
    }

    /// Borrows the proposed definition.
    #[must_use]
    pub const fn to(&self) -> &DefinitionIdentity {
        &self.to
    }

    /// Borrows the durable source-to-target step mapping.
    ///
    /// Durable adapters replay the mapping when they resolve a compatible
    /// restart, so the order is the validated declaration order.
    #[must_use]
    pub fn step_mapping(&self) -> &BTreeMap<StepName, StepName> {
        &self.step_mapping
    }
}
definition_token!(
    ComponentRevision,
    DefinitionTokenKind::Component,
    "An application-owned revision token for one opaque executable component."
);
definition_token!(
    ClassifierRevision,
    DefinitionTokenKind::Classifier,
    "An application-owned revision token for one bounded fault classifier."
);

/// Component revisions for a one-step chunk definition.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChunkComponentRevisions {
    reader: ComponentRevision,
    processor: ComponentRevision,
    writer: ComponentRevision,
    checkpoint: ComponentRevision,
    restart: ChunkRestartContract,
}

impl ChunkComponentRevisions {
    /// Constructs the four restart-relevant chunk component revisions.
    #[must_use]
    pub const fn new(
        reader: ComponentRevision,
        processor: ComponentRevision,
        writer: ComponentRevision,
        checkpoint: ComponentRevision,
        restart: ChunkRestartContract,
    ) -> Self {
        Self {
            reader,
            processor,
            writer,
            checkpoint,
            restart,
        }
    }

    /// Returns the delivery mode declared by the restart contract.
    #[must_use]
    pub const fn delivery_mode(&self) -> ChunkDeliveryMode {
        self.restart.delivery_mode
    }

    /// Returns the shutdown policy of an already-open chunk.
    #[must_use]
    pub const fn in_flight_policy(&self) -> InFlightPolicy {
        self.restart.in_flight_policy
    }

    /// Borrows the revision of the component that reads items.
    #[must_use]
    pub const fn reader(&self) -> &ComponentRevision {
        &self.reader
    }

    /// Borrows the revision of the component that processes items.
    #[must_use]
    pub const fn processor(&self) -> &ComponentRevision {
        &self.processor
    }

    /// Borrows the revision of the component that writes items.
    #[must_use]
    pub const fn writer(&self) -> &ComponentRevision {
        &self.writer
    }

    /// Borrows the revision of the component that produces checkpoints.
    #[must_use]
    pub const fn checkpoint(&self) -> &ComponentRevision {
        &self.checkpoint
    }

    /// Borrows the declared checkpoint schema.
    #[must_use]
    pub const fn checkpoint_schema(&self) -> &StateSchemaId {
        &self.restart.checkpoint_schema
    }

    /// Returns the declared checkpoint schema version.
    #[must_use]
    pub const fn checkpoint_schema_version(&self) -> StateSchemaVersion {
        self.restart.checkpoint_schema_version
    }

    /// Borrows the declared execution-context schema.
    #[must_use]
    pub const fn context_schema(&self) -> &StateSchemaId {
        &self.restart.context_schema
    }

    /// Returns the declared execution-context schema version.
    #[must_use]
    pub const fn context_schema_version(&self) -> StateSchemaVersion {
        self.restart.context_schema_version
    }
}

/// The accepted shutdown behavior for an already-open chunk.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum InFlightPolicy {
    /// Complete and commit the open chunk, then stop at its boundary.
    #[default]
    FinishChunk,
    /// Roll back the open chunk and preserve the prior checkpoint.
    RollbackChunk,
}

/// Declared delivery boundary included in a chunk definition fingerprint.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ChunkDeliveryMode {
    /// Business writes and progress share one `PostgreSQL` transaction.
    AtomicSameResource,
    /// The resource may observe a duplicate after restart.
    AtLeastOnce,
}

impl ChunkDeliveryMode {
    /// Returns the stable name this mode is recorded under in a manifest.
    ///
    /// The name is durable: it is hashed into a definition fingerprint, so it
    /// is fixed for the life of the mode rather than a display string.
    #[must_use]
    pub const fn manifest_name(self) -> &'static str {
        match self {
            Self::AtomicSameResource => "atomic_same_resource",
            Self::AtLeastOnce => "at_least_once",
        }
    }
}

/// Restart-state schemas and delivery mode for a chunk definition.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChunkRestartContract {
    checkpoint_schema: StateSchemaId,
    checkpoint_schema_version: StateSchemaVersion,
    context_schema: StateSchemaId,
    context_schema_version: StateSchemaVersion,
    delivery_mode: ChunkDeliveryMode,
    in_flight_policy: InFlightPolicy,
}

impl ChunkRestartContract {
    /// Constructs the restart-relevant state and delivery declaration.
    #[must_use]
    pub const fn new(
        checkpoint_schema: StateSchemaId,
        checkpoint_schema_version: StateSchemaVersion,
        context_schema: StateSchemaId,
        context_schema_version: StateSchemaVersion,
        delivery_mode: ChunkDeliveryMode,
    ) -> Self {
        Self {
            checkpoint_schema,
            checkpoint_schema_version,
            context_schema,
            context_schema_version,
            delivery_mode,
            in_flight_policy: InFlightPolicy::FinishChunk,
        }
    }

    /// Selects the restart-relevant open-chunk shutdown policy.
    #[must_use]
    pub const fn with_in_flight_policy(mut self, policy: InFlightPolicy) -> Self {
        self.in_flight_policy = policy;
        self
    }
}

/// Canonical restart-relevant identity persisted with every execution.
#[derive(Clone, Eq, PartialEq)]
pub struct DefinitionIdentity {
    job_name: Option<JobName>,
    revision: DefinitionRevision,
    manifest_format: u16,
    manifest_digest: [u8; 32],
    canonical_manifest: Box<[u8]>,
}

impl DefinitionIdentity {
    /// Builds the identity durable rows written before manifests carried one.
    ///
    /// The bytes are frozen: a durable row that predates manifest identity
    /// must keep resolving to exactly this value.
    #[must_use]
    pub fn legacy() -> Self {
        Self::from_canonical(
            None,
            DefinitionRevision(LEGACY_REVISION.to_owned()),
            LEGACY_MANIFEST.to_vec(),
            MANIFEST_FORMAT_ONE_STEP,
        )
    }

    /// Builds the canonical identity for a one-step tasklet definition.
    ///
    /// # Errors
    ///
    /// Returns [`DefinitionError::ManifestEncoding`] if the bounded canonical
    /// manifest cannot be encoded.
    pub fn tasklet(
        job_name: &JobName,
        step_name: &StepName,
        revision: DefinitionRevision,
        component_revision: &ComponentRevision,
    ) -> Result<Self, DefinitionError> {
        let manifest = json!({
            "component": {
                "tasklet": component_revision.as_str()
            },
            "delivery_mode": "best_effort",
            "format": MANIFEST_FORMAT_ONE_STEP,
            "job": job_name.as_str(),
            "kind": "tasklet",
            "restart_state": "none",
            "step": step_name.as_str(),
            "transaction_boundary": "tasklet_completion"
        });
        Self::encode(job_name.clone(), revision, &manifest)
    }

    /// Builds the canonical identity for a one-step chunk definition.
    ///
    /// # Errors
    ///
    /// Returns [`DefinitionError::ManifestEncoding`] if the bounded canonical
    /// manifest cannot be encoded.
    pub fn chunk(
        job_name: &JobName,
        step_name: &StepName,
        chunk_size: ChunkSize,
        revision: DefinitionRevision,
        components: &ChunkComponentRevisions,
    ) -> Result<Self, DefinitionError> {
        let mut manifest = json!({
            "chunk_size": chunk_size.get(),
            "components": {
                "checkpoint": components.checkpoint.as_str(),
                "processor": components.processor.as_str(),
                "reader": components.reader.as_str(),
                "writer": components.writer.as_str()
            },
            "context": {
                "schema": components.restart.context_schema.as_str(),
                "version": components.restart.context_schema_version.get()
            },
            "checkpoint": {
                "schema": components.restart.checkpoint_schema.as_str(),
                "version": components.restart.checkpoint_schema_version.get()
            },
            "delivery_mode": components.restart.delivery_mode.manifest_name(),
            "format": MANIFEST_FORMAT_ONE_STEP,
            "job": job_name.as_str(),
            "kind": "chunk",
            "step": step_name.as_str(),
            "transaction_boundary": "chunk"
        });
        if components.restart.in_flight_policy == InFlightPolicy::RollbackChunk
            && let Some(object) = manifest.as_object_mut()
        {
            object.insert(
                "in_flight_policy".to_owned(),
                serde_json::Value::String("rollback_chunk".to_owned()),
            );
        }
        Self::encode(job_name.clone(), revision, &manifest)
    }

    /// Builds the canonical identity for a compiled flow graph.
    ///
    /// The bytes must already be the canonical manifest the plan compiler
    /// normalized and encoded; this constructor validates, bounds, and hashes
    /// them. Taking bytes rather than a parsed document keeps the serializer
    /// out of this contract and makes the encoding the caller is responsible
    /// for explicit rather than implied.
    ///
    /// # Errors
    ///
    /// Returns [`DefinitionError::ManifestEncoding`] when the bytes exceed the
    /// bounded encoding, are not a canonical JSON object, or do not declare a
    /// supported flow manifest format.
    pub fn from_flow_manifest(
        job_name: &JobName,
        revision: DefinitionRevision,
        canonical: &[u8],
    ) -> Result<Self, DefinitionError> {
        if canonical.len() > MAX_MANIFEST_BYTES {
            return Err(DefinitionError::ManifestTooLarge {
                max_bytes: MAX_MANIFEST_BYTES,
            });
        }
        let document: serde_json::Value =
            serde_json::from_slice(canonical).map_err(|_| DefinitionError::ManifestEncoding)?;
        let reencoded =
            serde_json::to_vec(&document).map_err(|_| DefinitionError::ManifestEncoding)?;
        if !document.is_object() || reencoded != canonical {
            return Err(DefinitionError::ManifestEncoding);
        }
        let format = document
            .get("format")
            .and_then(serde_json::Value::as_u64)
            .and_then(|value| u16::try_from(value).ok())
            .filter(|value| matches!(*value, MANIFEST_FORMAT_FLOW | MANIFEST_FORMAT_LOCAL_SCALE))
            .ok_or(DefinitionError::ManifestEncoding)?;

        Ok(Self::from_canonical(
            Some(job_name.clone()),
            revision,
            canonical.to_vec(),
            format,
        ))
    }

    /// Encodes a one-step manifest this crate composed itself.
    fn encode(
        job_name: JobName,
        revision: DefinitionRevision,
        manifest: &serde_json::Value,
    ) -> Result<Self, DefinitionError> {
        let canonical =
            serde_json::to_vec(manifest).map_err(|_| DefinitionError::ManifestEncoding)?;
        if canonical.len() > MAX_MANIFEST_BYTES {
            return Err(DefinitionError::ManifestTooLarge {
                max_bytes: MAX_MANIFEST_BYTES,
            });
        }
        Ok(Self::from_canonical(
            Some(job_name),
            revision,
            canonical,
            MANIFEST_FORMAT_ONE_STEP,
        ))
    }

    fn from_canonical(
        job_name: Option<JobName>,
        revision: DefinitionRevision,
        canonical: Vec<u8>,
        format: u16,
    ) -> Self {
        let digest: [u8; 32] = Sha256::digest(&canonical).into();
        Self {
            job_name,
            revision,
            manifest_format: format,
            manifest_digest: digest,
            canonical_manifest: canonical.into_boxed_slice(),
        }
    }

    /// Borrows the application-owned definition revision.
    #[must_use]
    pub const fn revision(&self) -> &DefinitionRevision {
        &self.revision
    }

    /// Borrows the job name bound into a framework-produced manifest.
    ///
    /// Legacy direct repository calls use an internal compatibility manifest
    /// without a bound name.
    #[must_use]
    pub const fn job_name(&self) -> Option<&JobName> {
        self.job_name.as_ref()
    }

    /// Returns the canonical manifest format version.
    #[must_use]
    pub const fn manifest_format(&self) -> u16 {
        self.manifest_format
    }

    /// Returns the framework-produced SHA-256 manifest digest.
    #[must_use]
    pub const fn manifest_digest(&self) -> &[u8; 32] {
        &self.manifest_digest
    }

    /// Borrows the exact canonical manifest bytes that produced the digest.
    ///
    /// The manifest records names, logical identifiers, revisions, schema
    /// versions, and bounded policy values. Parameters, contexts, item values,
    /// credentials, endpoints, and component-private state are never encoded
    /// into it, so operators may inspect and archive these bytes.
    #[must_use]
    pub fn canonical_manifest(&self) -> &[u8] {
        &self.canonical_manifest
    }
}

/// Returns whether this runtime can interpret `format`.
///
/// # Errors
///
/// Returns [`ManifestError::UnsupportedFormat`] for a newer format and
/// [`ManifestError::MissingFormat`] for zero.
pub const fn check_manifest_format(format: u16) -> Result<(), ManifestError> {
    if format == 0 {
        return Err(ManifestError::MissingFormat);
    }
    if format > SUPPORTED_MANIFEST_FORMAT {
        return Err(ManifestError::UnsupportedFormat {
            format,
            supported: SUPPORTED_MANIFEST_FORMAT,
        });
    }
    Ok(())
}

impl fmt::Debug for DefinitionIdentity {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DefinitionIdentity")
            .field("job_name", &self.job_name)
            .field("revision", &self.revision)
            .field("manifest_format", &self.manifest_format)
            .field(
                "digest_prefix",
                &DigestPrefix([
                    self.manifest_digest[0],
                    self.manifest_digest[1],
                    self.manifest_digest[2],
                    self.manifest_digest[3],
                ]),
            )
            .field("canonical_manifest", &"<redacted>")
            .finish()
    }
}

struct DigestPrefix([u8; 4]);

impl fmt::Debug for DigestPrefix {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.0 {
            write!(formatter, "{byte:02x}")?;
        }
        Ok(())
    }
}

/// A validated, read-only view of one canonical definition manifest.
///
/// The reader accepts every manifest format this runtime understands, so a
/// deployment can inspect a definition persisted by an older release. It never
/// guesses: a newer format, a non-canonical encoding, a floating-point value,
/// an out-of-bound graph, or a digest that does not match the supplied bytes
/// fails closed.
///
/// The runnable example lives in the `oxide-batch` facade documentation,
/// because the supported import path is `oxide_batch`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DefinitionManifest {
    format: u16,
    digest: [u8; 32],
    job_name: Option<JobName>,
    node_count: Option<usize>,
    transition_count: Option<usize>,
}

impl DefinitionManifest {
    /// Reads and validates canonical manifest bytes.
    ///
    /// # Errors
    ///
    /// Returns [`ManifestError`] when the bytes exceed the durable bound, are
    /// not a canonical JSON object, omit or overflow the format member, declare
    /// a format this runtime cannot interpret, contain a floating-point value,
    /// or describe a graph outside the accepted bounds.
    pub fn read(bytes: &[u8]) -> Result<Self, ManifestError> {
        if bytes.len() > MAX_MANIFEST_BYTES {
            return Err(ManifestError::TooLarge {
                max_bytes: MAX_MANIFEST_BYTES,
            });
        }
        let document: serde_json::Value =
            serde_json::from_slice(bytes).map_err(|_| ManifestError::MalformedJson)?;
        let members = document.as_object().ok_or(ManifestError::NotAnObject)?;
        if contains_float(&document) {
            return Err(ManifestError::FloatValue);
        }
        let reencoded = serde_json::to_vec(&document).map_err(|_| ManifestError::MalformedJson)?;
        if reencoded != bytes {
            return Err(ManifestError::NonCanonicalEncoding);
        }
        let format = members
            .get("format")
            .and_then(serde_json::Value::as_u64)
            .and_then(|format| u16::try_from(format).ok())
            .ok_or(ManifestError::MissingFormat)?;
        check_manifest_format(format)?;
        let job_name = members
            .get("job")
            .and_then(serde_json::Value::as_str)
            .map(JobName::new)
            .transpose()
            .map_err(|_| ManifestError::InvalidJobName)?;
        let (node_count, transition_count) =
            if matches!(format, MANIFEST_FORMAT_FLOW | MANIFEST_FORMAT_LOCAL_SCALE) {
                let nodes = array_len(members.get("nodes"))?;
                let transitions = array_len(members.get("transitions"))?;
                if nodes > MAX_NODES || transitions > MAX_TRANSITIONS {
                    return Err(ManifestError::GraphOutOfBounds {
                        max_nodes: MAX_NODES,
                        max_transitions: MAX_TRANSITIONS,
                    });
                }
                (Some(nodes), Some(transitions))
            } else {
                (None, None)
            };
        Ok(Self {
            format,
            digest: Sha256::digest(bytes).into(),
            job_name,
            node_count,
            transition_count,
        })
    }

    /// Reads canonical bytes and requires them to hash to `expected`.
    ///
    /// # Errors
    ///
    /// Returns every [`ManifestError`] [`read`](Self::read) returns, plus
    /// [`ManifestError::DigestMismatch`] when the bytes were altered.
    pub fn read_verified(bytes: &[u8], expected: &[u8; 32]) -> Result<Self, ManifestError> {
        let manifest = Self::read(bytes)?;
        if &manifest.digest != expected {
            return Err(ManifestError::DigestMismatch);
        }
        Ok(manifest)
    }

    /// Returns the declared canonical manifest format.
    #[must_use]
    pub const fn format(&self) -> u16 {
        self.format
    }

    /// Returns the SHA-256 digest of the exact bytes that were read.
    #[must_use]
    pub const fn digest(&self) -> &[u8; 32] {
        &self.digest
    }

    /// Borrows the job name bound into the manifest, when present.
    #[must_use]
    pub const fn job_name(&self) -> Option<&JobName> {
        self.job_name.as_ref()
    }

    /// Returns the compiled node count of a flow manifest.
    #[must_use]
    pub const fn node_count(&self) -> Option<usize> {
        self.node_count
    }

    /// Returns the compiled transition count of a flow manifest.
    #[must_use]
    pub const fn transition_count(&self) -> Option<usize> {
        self.transition_count
    }
}

fn array_len(value: Option<&serde_json::Value>) -> Result<usize, ManifestError> {
    value
        .and_then(serde_json::Value::as_array)
        .map(Vec::len)
        .ok_or(ManifestError::MalformedGraph)
}

fn contains_float(value: &serde_json::Value) -> bool {
    match value {
        serde_json::Value::Number(number) => number.as_i64().is_none() && number.as_u64().is_none(),
        serde_json::Value::Array(values) => values.iter().any(contains_float),
        serde_json::Value::Object(members) => members.values().any(contains_float),
        _ => false,
    }
}

/// A canonical definition manifest that cannot be interpreted.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ManifestError {
    /// The bytes exceeded the durable manifest bound.
    TooLarge {
        /// Maximum accepted byte length.
        max_bytes: usize,
    },
    /// The bytes are not valid JSON.
    MalformedJson,
    /// The document is not a JSON object.
    NotAnObject,
    /// Re-encoding the document did not reproduce the supplied bytes.
    NonCanonicalEncoding,
    /// The document contains a floating-point number.
    FloatValue,
    /// The format member is absent or not a `u16`.
    MissingFormat,
    /// The manifest is newer than this runtime understands.
    UnsupportedFormat {
        /// Format found in the manifest.
        format: u16,
        /// Newest format this runtime interprets.
        supported: u16,
    },
    /// A flow manifest omitted or malformed its graph members.
    MalformedGraph,
    /// A flow manifest declared a graph larger than this runtime accepts.
    GraphOutOfBounds {
        /// Maximum accepted node count.
        max_nodes: usize,
        /// Maximum accepted transition count.
        max_transitions: usize,
    },
    /// The bound job name is not a valid [`JobName`].
    InvalidJobName,
    /// The bytes do not hash to the expected fingerprint.
    DigestMismatch,
}

impl fmt::Display for ManifestError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooLarge { max_bytes } => {
                write!(formatter, "definition manifest exceeds {max_bytes} bytes")
            }
            Self::MalformedJson => formatter.write_str("definition manifest is not valid JSON"),
            Self::NotAnObject => formatter.write_str("definition manifest is not a JSON object"),
            Self::NonCanonicalEncoding => {
                formatter.write_str("definition manifest is not canonically encoded")
            }
            Self::FloatValue => {
                formatter.write_str("definition manifest contains a floating-point value")
            }
            Self::MissingFormat => {
                formatter.write_str("definition manifest has no usable format member")
            }
            Self::UnsupportedFormat { format, supported } => write!(
                formatter,
                "definition manifest format {format} is newer than the supported format {supported}"
            ),
            Self::MalformedGraph => {
                formatter.write_str("flow manifest has no readable node and transition members")
            }
            Self::GraphOutOfBounds {
                max_nodes,
                max_transitions,
            } => write!(
                formatter,
                "flow manifest exceeds {max_nodes} nodes or {max_transitions} transitions"
            ),
            Self::InvalidJobName => {
                formatter.write_str("definition manifest binds an invalid job name")
            }
            Self::DigestMismatch => {
                formatter.write_str("definition manifest does not match its fingerprint")
            }
        }
    }
}

impl Error for ManifestError {}

/// Definition token category used by validation diagnostics.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DefinitionTokenKind {
    /// Definition revision.
    Revision,
    /// Opaque component revision.
    Component,
    /// Directed compatibility edge key.
    Upgrade,
    /// Bounded fault-classifier revision.
    Classifier,
    /// Stable flow-graph node identifier.
    Node,
    /// Bounded decider revision.
    Decider,
}

/// Failure to construct a bounded restart definition.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DefinitionError {
    /// A start limit of zero can never start its step.
    ZeroStartLimit,
    /// A required token was empty.
    EmptyToken {
        /// Rejected token category.
        kind: DefinitionTokenKind,
    },
    /// A token exceeded its UTF-8 byte bound.
    TokenTooLong {
        /// Rejected token category.
        kind: DefinitionTokenKind,
        /// Maximum accepted byte length.
        max_bytes: usize,
    },
    /// A token had leading or trailing whitespace.
    SurroundingWhitespace {
        /// Rejected token category.
        kind: DefinitionTokenKind,
    },
    /// A token contained a control character.
    ControlCharacter {
        /// Rejected token category.
        kind: DefinitionTokenKind,
    },
    /// The canonical manifest could not be encoded.
    ManifestEncoding,
    /// The canonical manifest exceeded its durable bound.
    ManifestTooLarge {
        /// Maximum accepted byte length.
        max_bytes: usize,
    },
    /// A directed edge pointed from a definition to itself.
    UpgradeSelfEdge,
    /// A directed edge omitted its durable step mapping.
    EmptyStepMapping,
    /// A source step appeared more than once.
    DuplicateSourceStep,
    /// Two source steps mapped to the same target step.
    DuplicateTargetStep,
    /// A step's fault runtime declared a different delivery mode than its
    /// restart contract.
    DeliveryModeMismatch,
    /// A one-step wrapper could not be lowered into its compatibility plan.
    ///
    /// The framework derives the compatibility graph from values it has
    /// already validated, so this variant reports a framework invariant rather
    /// than an application mistake.
    CompatibilityLowering,
}

impl fmt::Display for DefinitionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ZeroStartLimit => formatter.write_str("start limit must be nonzero"),
            Self::EmptyToken { kind } => write!(formatter, "{kind:?} token must not be empty"),
            Self::TokenTooLong { kind, max_bytes } => {
                write!(formatter, "{kind:?} token exceeds {max_bytes} bytes")
            }
            Self::SurroundingWhitespace { kind } => {
                write!(formatter, "{kind:?} token has surrounding whitespace")
            }
            Self::ControlCharacter { kind } => {
                write!(formatter, "{kind:?} token contains a control character")
            }
            Self::ManifestEncoding => formatter.write_str("definition manifest encoding failed"),
            Self::ManifestTooLarge { max_bytes } => {
                write!(formatter, "definition manifest exceeds {max_bytes} bytes")
            }
            Self::UpgradeSelfEdge => formatter.write_str("definition upgrade is a self-edge"),
            Self::EmptyStepMapping => formatter.write_str("definition upgrade has no step mapping"),
            Self::DuplicateSourceStep => {
                formatter.write_str("definition upgrade repeats a source step")
            }
            Self::DuplicateTargetStep => {
                formatter.write_str("definition upgrade reuses a target step")
            }
            Self::DeliveryModeMismatch => formatter
                .write_str("fault runtime and restart contract declare different delivery modes"),
            Self::CompatibilityLowering => {
                formatter.write_str("one-step compatibility lowering produced an invalid plan")
            }
        }
    }
}

impl Error for DefinitionError {}