cymule-core 0.1.4

Small trusted semantic kernel for Cymule
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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::{CoreError, Result, canonical_digest, content_id};

/// Semantic specification version.
pub const SEMANTIC_VERSION: &str = "cymule.semantic/1";
/// Canonical event version.
pub const EVENT_VERSION: &str = "cymule.event/1";
/// Public command version.
pub const COMMAND_VERSION: &str = "cymule.command/1";
/// Stable root scope identifier within every Run.
pub const ROOT_SCOPE_ID: &str = "scope:root";

/// Immutable artifact reference.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ArtifactRef {
    /// Content-addressed identity.
    pub artifact_id: String,
    /// Stable artifact type.
    pub kind: String,
}

/// Immutable artifact bytes held by the embedded store.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ArtifactRecord {
    /// Reference derived from type and bytes.
    pub reference: ArtifactRef,
    /// Immutable bytes.
    pub bytes: Vec<u8>,
}

/// A preconditioned, idempotent command proposal.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CommandEnvelope {
    /// Command protocol version.
    pub command_version: String,
    /// Caller-generated idempotency identity.
    pub command_id: String,
    /// Authenticated actor reference. The embedded profile treats it as opaque.
    pub actor: String,
    /// Target Run identity.
    pub run_id: String,
    /// Token from the caller's current Run view.
    #[serde(default)]
    pub expected_precondition: Option<String>,
    /// Typed semantic proposal.
    pub command: Command,
}

/// Commands admitted by the semantic kernel.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Command {
    /// Start a new Run under an immutable plan and future-default binding context.
    StartRun {
        /// Sealed plan ID.
        plan_id: String,
        /// Default realization context for future occurrences.
        binding_context: String,
    },
    /// Begin a fenced Attempt.
    BeginAttempt {
        /// Attempt identity.
        attempt_id: String,
        /// Continuation identity.
        continuation_id: String,
        /// Occurrence-level immutable binding.
        occurrence_binding: String,
        /// Expected continuation epoch.
        epoch: u64,
    },
    /// Yield the active Attempt at a safe point.
    YieldAttempt {
        /// Attempt identity.
        attempt_id: String,
        /// Expected continuation epoch.
        epoch: u64,
    },
    /// Advance the Run epoch and fence prior attempts.
    AdvanceEpoch,
    /// Open a nested state/evidence scope.
    OpenScope {
        /// New scope identity.
        scope_id: String,
        /// Existing parent scope.
        parent_scope: String,
    },
    /// Propose a structurally identified external effect.
    ProposeEffect {
        /// Owning open scope.
        scope_id: String,
        /// Invocation identity.
        invocation_id: String,
        /// Stable IR effect site.
        site_id: String,
        /// Intentional occurrence key.
        occurrence: String,
        /// Abstract effect operation.
        operation: String,
        /// Canonical argument artifact.
        args: ArtifactRef,
        /// Immutable occurrence binding.
        occurrence_binding: String,
    },
    /// Advance one axis of an existing effect.
    TransitionEffect {
        /// Structural intent identity.
        intent_id: String,
        /// Legal next transition.
        transition: EffectTransition,
    },
    /// Commit internal scope state and transfer unresolved effect obligations.
    CommitScope {
        /// Scope identity.
        scope_id: String,
    },
    /// Abort a scope before release.
    AbortScope {
        /// Scope identity.
        scope_id: String,
    },
    /// Change the realization default for future occurrences.
    UpdateBinding {
        /// New immutable Binding Context reference.
        binding_context: String,
    },
    /// Append an independent immutable fact for causal conformance tests.
    RecordFact {
        /// Stable logical fact key.
        key: String,
        /// Immutable value digest or reference.
        value: String,
    },
    /// Finish a Run after required obligations have settled.
    CompleteRun {
        /// Optional typed Result artifact.
        #[serde(default)]
        result: Option<ArtifactRef>,
    },
}

/// Legal effect transitions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum EffectTransition {
    /// Preparation completed.
    Prepare,
    /// Governing policy authorized release.
    AuthorizeRelease,
    /// External dispatch began.
    StartDispatch,
    /// Record the best authoritative world observation currently available.
    Observe(WorldOutcome),
    /// Reconcile an earlier unknown outcome.
    Reconcile(ReconciliationResolution),
}

/// A canonical event.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Event {
    /// Content-addressed event identity.
    pub event_id: String,
    /// Event schema version.
    pub event_version: String,
    /// Admitted command identity.
    pub command_id: String,
    /// Canonical command semantics digest.
    pub command_hash: String,
    /// Run identity.
    pub run_id: String,
    /// Explicit causal parents.
    pub parents: Vec<String>,
    /// Logical point/predicate reads.
    pub reads: BTreeSet<String>,
    /// Logical writes.
    pub writes: BTreeSet<String>,
    /// Optional non-monotone coordination domain.
    #[serde(default)]
    pub coordination_key: Option<String>,
    /// Trusted semantic transition.
    pub payload: EventPayload,
}

#[derive(Serialize)]
struct EventPreimage<'a> {
    event_version: &'a str,
    command_id: &'a str,
    command_hash: &'a str,
    run_id: &'a str,
    parents: &'a [String],
    reads: &'a BTreeSet<String>,
    writes: &'a BTreeSet<String>,
    coordination_key: &'a Option<String>,
    payload: &'a EventPayload,
}

impl Event {
    /// Construct and content-address a trusted event.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        command_id: String,
        command_hash: String,
        run_id: String,
        mut parents: Vec<String>,
        reads: BTreeSet<String>,
        writes: BTreeSet<String>,
        coordination_key: Option<String>,
        payload: EventPayload,
    ) -> Result<Self> {
        parents.sort();
        parents.dedup();
        let preimage = EventPreimage {
            event_version: EVENT_VERSION,
            command_id: &command_id,
            command_hash: &command_hash,
            run_id: &run_id,
            parents: &parents,
            reads: &reads,
            writes: &writes,
            coordination_key: &coordination_key,
            payload: &payload,
        };
        let event_id = content_id("cymule.event/1", &preimage)?;
        Ok(Self {
            event_id,
            event_version: EVENT_VERSION.to_owned(),
            command_id,
            command_hash,
            run_id,
            parents,
            reads,
            writes,
            coordination_key,
            payload,
        })
    }

    /// Verify event schema and content identity.
    pub fn verify(&self) -> Result<()> {
        if self.event_version != EVENT_VERSION {
            return Err(CoreError::Validation(format!(
                "unsupported event version {:?}",
                self.event_version
            )));
        }
        let preimage = EventPreimage {
            event_version: &self.event_version,
            command_id: &self.command_id,
            command_hash: &self.command_hash,
            run_id: &self.run_id,
            parents: &self.parents,
            reads: &self.reads,
            writes: &self.writes,
            coordination_key: &self.coordination_key,
            payload: &self.payload,
        };
        let expected = content_id("cymule.event/1", &preimage)?;
        if expected != self.event_id {
            return Err(CoreError::IdentityMismatch(format!(
                "event ID {} does not match {expected}",
                self.event_id
            )));
        }
        Ok(())
    }
}

/// Canonical transition payloads.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum EventPayload {
    /// A Run became canonical.
    RunStarted {
        /// Plan identity.
        plan_id: String,
        /// Future-default realization context.
        binding_context: String,
    },
    /// An Attempt received a fenced lease.
    AttemptStarted {
        /// Attempt identity.
        attempt_id: String,
        /// Continuation identity.
        continuation_id: String,
        /// Pinned occurrence binding.
        occurrence_binding: String,
        /// Pinned epoch.
        epoch: u64,
    },
    /// An Attempt yielded at a safe point.
    AttemptYielded {
        /// Attempt identity.
        attempt_id: String,
        /// Pinned epoch.
        epoch: u64,
    },
    /// The Run epoch advanced.
    EpochAdvanced {
        /// New epoch.
        epoch: u64,
    },
    /// A nested scope opened.
    ScopeOpened {
        /// Scope identity.
        scope_id: String,
        /// Parent scope identity.
        parent_scope: String,
    },
    /// An effect was admitted with an immutable occurrence binding.
    EffectProposed {
        /// Structural intent identity.
        intent_id: String,
        /// Owning scope.
        scope_id: String,
        /// Abstract operation ID.
        operation: String,
        /// Whether it mutates the world.
        mutating: bool,
        /// Canonical argument artifact.
        args: ArtifactRef,
        /// Pinned occurrence binding.
        occurrence_binding: String,
    },
    /// One effect state axis advanced.
    EffectTransitioned {
        /// Structural intent identity.
        intent_id: String,
        /// Transition.
        transition: EffectTransition,
    },
    /// Internal scope state committed and obligations transferred.
    ScopeCommitted {
        /// Scope identity.
        scope_id: String,
        /// Deterministically derived obligations.
        obligations: Vec<ObligationProjection>,
    },
    /// A scope aborted before effect release.
    ScopeAborted {
        /// Scope identity.
        scope_id: String,
    },
    /// Future occurrences use a new default binding context.
    BindingUpdated {
        /// Previous context.
        previous: String,
        /// New context.
        current: String,
    },
    /// An append-only fact was recorded.
    FactRecorded {
        /// Logical fact key.
        key: String,
        /// Immutable value.
        value: String,
    },
    /// A Run entered a terminal completed state.
    RunCompleted {
        /// Optional Result artifact.
        result: Option<ArtifactRef>,
    },
}

/// Public command outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CommandReceiptStatus {
    /// A canonical event was admitted.
    Applied,
    /// The caller's precondition was stale.
    Conflict,
}

/// Durable command receipt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CommandReceipt {
    /// Command identity.
    pub command_id: String,
    /// Stable status.
    pub status: CommandReceiptStatus,
    /// Admitted event when applied.
    #[serde(default)]
    pub event_id: Option<String>,
    /// Stable structured error code for a conflict.
    #[serde(default)]
    pub error_code: Option<String>,
    /// Human-readable explanation.
    #[serde(default)]
    pub message: Option<String>,
    /// Caller-provided token.
    #[serde(default)]
    pub observed_precondition: Option<String>,
    /// Current token after application or conflict.
    #[serde(default)]
    pub current_precondition: Option<String>,
}

/// Rebuildable full projection.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Projection {
    /// Run projections.
    pub runs: BTreeMap<String, RunProjection>,
    /// Append-only facts used by explainability and conformance.
    pub facts: BTreeMap<String, String>,
}

/// Rebuildable Run projection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RunProjection {
    /// Run identity.
    pub run_id: String,
    /// Initial immutable plan.
    pub initial_plan: String,
    /// Current semantic plan. Plan migration is not implemented in profile 1.
    pub current_plan: String,
    /// Initial realization default.
    pub initial_binding_context: String,
    /// Future-occurrence realization default.
    pub current_binding_context: String,
    /// Fencing epoch.
    pub epoch: u64,
    /// Public Run status.
    pub status: RunStatus,
    /// Scope projections.
    pub scopes: BTreeMap<String, ScopeProjection>,
    /// Effect projections.
    pub effects: BTreeMap<String, EffectProjection>,
    /// Outstanding or settled obligations.
    pub obligations: BTreeMap<String, ObligationProjection>,
    /// Attempt projections.
    pub attempts: BTreeMap<String, AttemptProjection>,
    /// Optional terminal Result.
    pub result: Option<ArtifactRef>,
    /// Last applied event in the embedded linear frontier.
    pub last_event: String,
}

impl RunProjection {
    /// Current stale-action protection token.
    pub fn precondition_token(&self) -> String {
        format!("pre:{}:{}", self.epoch, self.last_event)
    }
}

/// Public Run status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunStatus {
    /// New work may be admitted.
    Active,
    /// Waiting for a durable condition.
    Waiting,
    /// Terminal successful state.
    Completed,
    /// Terminal failure state.
    Failed,
    /// Terminal cancellation state.
    Cancelled,
}

/// Scope projection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ScopeProjection {
    /// Scope identity.
    pub scope_id: String,
    /// Optional parent.
    pub parent_scope: Option<String>,
    /// Scope lifecycle state.
    pub status: ScopeStatus,
    /// Effects admitted in the scope.
    pub intents: BTreeSet<String>,
}

/// Scope lifecycle state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScopeStatus {
    /// Scope accepts work.
    Open,
    /// Internal state/evidence was committed and the scope is closed.
    ClosedCommitted,
    /// Overlay and unreleased mutation were discarded.
    ClosedAborted,
}

/// Effect control phase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EffectPhase {
    /// Intent was admitted.
    Admitted,
    /// Payload and evidence were prepared.
    Prepared,
    /// Governing policy authorized release.
    ReleaseAuthorized,
    /// External dispatch began.
    DispatchStarted,
    /// Unreleased effect was cancelled by scope abort.
    CancelledBeforeRelease,
}

/// Observed external-world outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorldOutcome {
    /// No external-world observation exists yet.
    Unobserved,
    /// The external action occurred.
    Applied,
    /// The external action did not occur.
    NotApplied,
    /// Dispatch occurred but the outcome cannot currently be determined.
    Unknown,
}

/// Reconciliation result transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconciliationResolution {
    /// Observation proved the action occurred.
    ResolvedApplied,
    /// Observation proved the action did not occur.
    ResolvedNotApplied,
    /// The outcome remains unknown and may be queried again.
    StillUnknown,
    /// Automatic resolution is unavailable.
    GovernanceRequired,
}

/// Reconciliation state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconciliationState {
    /// No reconciliation is required for the observed outcome.
    NotRequired,
    /// Unknown outcome awaits reconciliation.
    Pending,
    /// Unknown outcome was authoritatively resolved.
    Resolved,
    /// Governance must decide how to proceed.
    GovernanceRequired,
}

/// Effect projection with immutable occurrence binding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EffectProjection {
    /// Structural intent identity.
    pub intent_id: String,
    /// Owning scope.
    pub scope_id: String,
    /// Abstract operation.
    pub operation: String,
    /// External mutation flag.
    pub mutating: bool,
    /// Canonical arguments.
    pub args: ArtifactRef,
    /// Immutable historical realization.
    pub occurrence_binding: String,
    /// Control phase.
    pub phase: EffectPhase,
    /// World outcome.
    pub outcome: WorldOutcome,
    /// Reconciliation state.
    pub reconciliation: ReconciliationState,
}

/// Scope-transferred world-effect obligation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObligationProjection {
    /// Deterministic obligation identity.
    pub obligation_id: String,
    /// Effect intent.
    pub intent_id: String,
    /// Whether this obligation blocks normal Run completion.
    pub blocking: bool,
    /// Whether an authoritative terminal outcome is known.
    pub resolved: bool,
}

/// Fenced attempt projection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AttemptProjection {
    /// Attempt identity.
    pub attempt_id: String,
    /// Continuation identity.
    pub continuation_id: String,
    /// Immutable occurrence binding.
    pub occurrence_binding: String,
    /// Fencing epoch.
    pub epoch: u64,
    /// Whether the Attempt currently owns execution.
    pub active: bool,
}

/// Explicit exact-replay capability.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum ReplayAvailability {
    /// All required canonical inputs are available.
    Exact,
    /// State projections remain available but complete nondeterminism does not.
    ProjectionOnly {
        /// Missing or redacted references.
        missing: Vec<String>,
    },
    /// Even the requested projection cannot be reconstructed.
    Unavailable {
        /// Stable explanation.
        reason: String,
    },
}

/// Compaction witness. The Embedded profile constructs and validates this type
/// but does not delete canonical history automatically.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CompactionCertificate {
    /// Causally closed source frontier.
    pub source_frontier: Vec<String>,
    /// Digest of the summarized projection.
    pub projection_digest: String,
    /// Unresolved obligations preserved by the summary.
    pub unresolved_obligations: Vec<String>,
    /// Historical occurrence bindings retained for interpretation.
    pub occurrence_bindings: Vec<String>,
    /// Resulting replay capability.
    pub replay_availability: ReplayAvailability,
}

impl Projection {
    /// Apply one verified canonical event.
    pub fn apply_event(&mut self, event: &Event) -> Result<()> {
        match &event.payload {
            EventPayload::RunStarted {
                plan_id,
                binding_context,
            } => {
                if self.runs.contains_key(&event.run_id) {
                    return Err(CoreError::IllegalTransition(format!(
                        "Run {} already exists",
                        event.run_id
                    )));
                }
                let mut scopes = BTreeMap::new();
                scopes.insert(
                    ROOT_SCOPE_ID.to_owned(),
                    ScopeProjection {
                        scope_id: ROOT_SCOPE_ID.to_owned(),
                        parent_scope: None,
                        status: ScopeStatus::Open,
                        intents: BTreeSet::new(),
                    },
                );
                self.runs.insert(
                    event.run_id.clone(),
                    RunProjection {
                        run_id: event.run_id.clone(),
                        initial_plan: plan_id.clone(),
                        current_plan: plan_id.clone(),
                        initial_binding_context: binding_context.clone(),
                        current_binding_context: binding_context.clone(),
                        epoch: 0,
                        status: RunStatus::Active,
                        scopes,
                        effects: BTreeMap::new(),
                        obligations: BTreeMap::new(),
                        attempts: BTreeMap::new(),
                        result: None,
                        last_event: event.event_id.clone(),
                    },
                );
                return Ok(());
            }
            EventPayload::FactRecorded { key, value } => {
                if let Some(existing) = self.facts.get(key) {
                    if existing != value {
                        return Err(CoreError::IllegalTransition(format!(
                            "fact {key:?} already has a different value"
                        )));
                    }
                } else {
                    self.facts.insert(key.clone(), value.clone());
                }
            }
            _ => {}
        }

        let run = self
            .runs
            .get_mut(&event.run_id)
            .ok_or_else(|| CoreError::NotFound(format!("Run {} does not exist", event.run_id)))?;
        if run.status == RunStatus::Completed {
            return Err(CoreError::IllegalTransition(format!(
                "Run {} is already completed",
                event.run_id
            )));
        }

        match &event.payload {
            EventPayload::RunStarted { .. } | EventPayload::FactRecorded { .. } => {}
            EventPayload::AttemptStarted {
                attempt_id,
                continuation_id,
                occurrence_binding,
                epoch,
            } => {
                if *epoch != run.epoch {
                    return Err(CoreError::IllegalTransition(format!(
                        "attempt epoch {epoch} does not match Run epoch {}",
                        run.epoch
                    )));
                }
                if run.attempts.contains_key(attempt_id) {
                    return Err(CoreError::IllegalTransition(format!(
                        "attempt {attempt_id} already exists"
                    )));
                }
                run.attempts.insert(
                    attempt_id.clone(),
                    AttemptProjection {
                        attempt_id: attempt_id.clone(),
                        continuation_id: continuation_id.clone(),
                        occurrence_binding: occurrence_binding.clone(),
                        epoch: *epoch,
                        active: true,
                    },
                );
            }
            EventPayload::AttemptYielded { attempt_id, epoch } => {
                let attempt = run.attempts.get_mut(attempt_id).ok_or_else(|| {
                    CoreError::NotFound(format!("attempt {attempt_id} does not exist"))
                })?;
                if !attempt.active || attempt.epoch != *epoch {
                    return Err(CoreError::IllegalTransition(format!(
                        "attempt {attempt_id} is stale or inactive"
                    )));
                }
                attempt.active = false;
            }
            EventPayload::EpochAdvanced { epoch } => {
                if *epoch != run.epoch + 1 {
                    return Err(CoreError::IllegalTransition(format!(
                        "epoch must advance from {} to {}; received {epoch}",
                        run.epoch,
                        run.epoch + 1
                    )));
                }
                run.epoch = *epoch;
                for attempt in run.attempts.values_mut() {
                    attempt.active = false;
                }
            }
            EventPayload::ScopeOpened {
                scope_id,
                parent_scope,
            } => {
                if run.scopes.contains_key(scope_id) {
                    return Err(CoreError::IllegalTransition(format!(
                        "scope {scope_id} already exists"
                    )));
                }
                let parent = run.scopes.get(parent_scope).ok_or_else(|| {
                    CoreError::NotFound(format!("parent scope {parent_scope} does not exist"))
                })?;
                if parent.status != ScopeStatus::Open {
                    return Err(CoreError::IllegalTransition(format!(
                        "parent scope {parent_scope} is not open"
                    )));
                }
                run.scopes.insert(
                    scope_id.clone(),
                    ScopeProjection {
                        scope_id: scope_id.clone(),
                        parent_scope: Some(parent_scope.clone()),
                        status: ScopeStatus::Open,
                        intents: BTreeSet::new(),
                    },
                );
            }
            EventPayload::EffectProposed {
                intent_id,
                scope_id,
                operation,
                mutating,
                args,
                occurrence_binding,
            } => {
                if run.effects.contains_key(intent_id) {
                    return Err(CoreError::IllegalTransition(format!(
                        "effect intent {intent_id} already exists"
                    )));
                }
                let scope = run.scopes.get_mut(scope_id).ok_or_else(|| {
                    CoreError::NotFound(format!("scope {scope_id} does not exist"))
                })?;
                if scope.status != ScopeStatus::Open {
                    return Err(CoreError::IllegalTransition(format!(
                        "scope {scope_id} is not open"
                    )));
                }
                scope.intents.insert(intent_id.clone());
                run.effects.insert(
                    intent_id.clone(),
                    EffectProjection {
                        intent_id: intent_id.clone(),
                        scope_id: scope_id.clone(),
                        operation: operation.clone(),
                        mutating: *mutating,
                        args: args.clone(),
                        occurrence_binding: occurrence_binding.clone(),
                        phase: EffectPhase::Admitted,
                        outcome: WorldOutcome::Unobserved,
                        reconciliation: ReconciliationState::NotRequired,
                    },
                );
            }
            EventPayload::EffectTransitioned {
                intent_id,
                transition,
            } => {
                let effect = run.effects.get_mut(intent_id).ok_or_else(|| {
                    CoreError::NotFound(format!("effect {intent_id} does not exist"))
                })?;
                apply_effect_transition(effect, transition)?;
                update_obligation(run, intent_id);
            }
            EventPayload::ScopeCommitted {
                scope_id,
                obligations,
            } => {
                let scope = run.scopes.get_mut(scope_id).ok_or_else(|| {
                    CoreError::NotFound(format!("scope {scope_id} does not exist"))
                })?;
                if scope.status != ScopeStatus::Open {
                    return Err(CoreError::IllegalTransition(format!(
                        "scope {scope_id} is not open"
                    )));
                }
                scope.status = ScopeStatus::ClosedCommitted;
                for obligation in obligations {
                    if !scope.intents.contains(&obligation.intent_id) {
                        return Err(CoreError::IllegalTransition(format!(
                            "obligation {} does not belong to scope {scope_id}",
                            obligation.obligation_id
                        )));
                    }
                    if run
                        .obligations
                        .insert(obligation.obligation_id.clone(), obligation.clone())
                        .is_some()
                    {
                        return Err(CoreError::IllegalTransition(format!(
                            "obligation {} already exists",
                            obligation.obligation_id
                        )));
                    }
                }
            }
            EventPayload::ScopeAborted { scope_id } => {
                let scope = run.scopes.get_mut(scope_id).ok_or_else(|| {
                    CoreError::NotFound(format!("scope {scope_id} does not exist"))
                })?;
                if scope.status != ScopeStatus::Open {
                    return Err(CoreError::IllegalTransition(format!(
                        "scope {scope_id} is not open"
                    )));
                }
                for intent_id in &scope.intents {
                    let effect = run.effects.get_mut(intent_id).ok_or_else(|| {
                        CoreError::NotFound(format!("effect {intent_id} does not exist"))
                    })?;
                    if matches!(
                        effect.phase,
                        EffectPhase::ReleaseAuthorized | EffectPhase::DispatchStarted
                    ) {
                        return Err(CoreError::IllegalTransition(format!(
                            "scope {scope_id} cannot abort after effect release"
                        )));
                    }
                    effect.phase = EffectPhase::CancelledBeforeRelease;
                }
                scope.status = ScopeStatus::ClosedAborted;
            }
            EventPayload::BindingUpdated { previous, current } => {
                if &run.current_binding_context != previous {
                    return Err(CoreError::IllegalTransition(format!(
                        "binding context changed from expected {previous}"
                    )));
                }
                run.current_binding_context.clone_from(current);
            }
            EventPayload::RunCompleted { result } => {
                let unresolved = run
                    .obligations
                    .values()
                    .any(|obligation| obligation.blocking && !obligation.resolved);
                if unresolved {
                    return Err(CoreError::IllegalTransition(
                        "Run has unresolved blocking effect obligations".to_owned(),
                    ));
                }
                if run
                    .scopes
                    .values()
                    .any(|scope| scope.status == ScopeStatus::Open)
                {
                    return Err(CoreError::IllegalTransition(
                        "Run has an open scope".to_owned(),
                    ));
                }
                run.status = RunStatus::Completed;
                run.result.clone_from(result);
            }
        }
        run.last_event.clone_from(&event.event_id);
        Ok(())
    }

    /// Deterministic digest of the complete rebuildable projection.
    pub fn digest(&self) -> Result<String> {
        canonical_digest(self)
    }
}

fn apply_effect_transition(
    effect: &mut EffectProjection,
    transition: &EffectTransition,
) -> Result<()> {
    match transition {
        EffectTransition::Prepare if effect.phase == EffectPhase::Admitted => {
            effect.phase = EffectPhase::Prepared;
        }
        EffectTransition::AuthorizeRelease if effect.phase == EffectPhase::Prepared => {
            effect.phase = EffectPhase::ReleaseAuthorized;
        }
        EffectTransition::StartDispatch if effect.phase == EffectPhase::ReleaseAuthorized => {
            effect.phase = EffectPhase::DispatchStarted;
        }
        EffectTransition::Observe(outcome)
            if effect.phase == EffectPhase::DispatchStarted
                && effect.outcome == WorldOutcome::Unobserved
                && *outcome != WorldOutcome::Unobserved =>
        {
            effect.outcome = *outcome;
            effect.reconciliation = if *outcome == WorldOutcome::Unknown {
                ReconciliationState::Pending
            } else {
                ReconciliationState::NotRequired
            };
        }
        EffectTransition::Reconcile(resolution)
            if effect.phase == EffectPhase::DispatchStarted
                && effect.outcome == WorldOutcome::Unknown =>
        {
            match resolution {
                ReconciliationResolution::ResolvedApplied => {
                    effect.outcome = WorldOutcome::Applied;
                    effect.reconciliation = ReconciliationState::Resolved;
                }
                ReconciliationResolution::ResolvedNotApplied => {
                    effect.outcome = WorldOutcome::NotApplied;
                    effect.reconciliation = ReconciliationState::Resolved;
                }
                ReconciliationResolution::StillUnknown => {
                    effect.reconciliation = ReconciliationState::Pending;
                }
                ReconciliationResolution::GovernanceRequired => {
                    effect.reconciliation = ReconciliationState::GovernanceRequired;
                }
            }
        }
        _ => {
            return Err(CoreError::IllegalTransition(format!(
                "illegal effect transition {transition:?} from phase {:?}, outcome {:?}, reconciliation {:?}",
                effect.phase, effect.outcome, effect.reconciliation
            )));
        }
    }
    Ok(())
}

fn update_obligation(run: &mut RunProjection, intent_id: &str) {
    let resolved = run.effects.get(intent_id).is_some_and(|effect| {
        matches!(
            effect.outcome,
            WorldOutcome::Applied | WorldOutcome::NotApplied
        )
    });
    for obligation in run
        .obligations
        .values_mut()
        .filter(|obligation| obligation.intent_id == intent_id)
    {
        obligation.resolved = resolved;
    }
}

/// Derive the structural identity of one intentional effect occurrence.
#[allow(clippy::too_many_arguments)]
pub fn effect_intent_id(
    run_id: &str,
    invocation_id: &str,
    site_id: &str,
    scope_id: &str,
    scope_epoch: u64,
    occurrence: &str,
    args: &ArtifactRef,
    effect_schema_version: &str,
) -> Result<String> {
    #[derive(Serialize)]
    struct IntentPreimage<'a> {
        run_id: &'a str,
        invocation_id: &'a str,
        site_id: &'a str,
        scope_id: &'a str,
        scope_epoch: u64,
        occurrence: &'a str,
        args: &'a ArtifactRef,
        effect_schema_version: &'a str,
    }
    content_id(
        "cymule.effect-intent/1",
        &IntentPreimage {
            run_id,
            invocation_id,
            site_id,
            scope_id,
            scope_epoch,
            occurrence,
            args,
            effect_schema_version,
        },
    )
}

/// Derive a stable obligation identity from an intent.
pub fn effect_obligation_id(intent_id: &str) -> Result<String> {
    content_id("cymule.effect-obligation/1", &intent_id)
}