telltale-machine 17.0.0

Protocol machine for choreographic session type protocols
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
// Effect-request validation and ownership for threaded mode.
impl ThreadedProtocolMachine {
    fn coro_owner_id(coro_id: usize) -> String {
        format!("coro:{coro_id}")
    }

    fn ensure_effect_request_allowed(&self, request: &EffectRequest) -> Result<(), EffectFailure> {
        request.metadata.validate()?;
        let incoming = crate::effect::EffectResponsibilityDomain {
            footprint_key: request
                .session
                .map(|session| format!("session:{session}"))
                .unwrap_or_else(|| request.metadata.interface_name.clone()),
            operation_id: request.operation_id.clone(),
            fragment_id: request.session.map(|session| format!("session:{session}")),
            owner_id: None,
        };
        for effect in self.outstanding_effects.iter().filter(|effect| {
            matches!(
                effect.status,
                OutstandingEffectStatus::Pending | OutstandingEffectStatus::Blocked
            )
        }) {
            let active = crate::effect::EffectResponsibilityDomain {
                footprint_key: effect
                    .session
                    .map(|session| format!("session:{session}"))
                    .unwrap_or_else(|| effect.handler_identity.clone()),
                operation_id: Some(effect.operation_id.clone()),
                fragment_id: effect.session.map(|session| format!("session:{session}")),
                owner_id: effect.owner_id.clone(),
            };
            if !request.metadata.reentrancy_admissible(&active, &incoming) {
                return Err(EffectFailure::contract_violation(format!(
                    "reentrancy rejected for effect {} on footprint {}",
                    effect.effect_id, active.footprint_key
                )));
            }
        }
        Ok(())
    }

    fn record_effect_exchange(
        &mut self,
        request: &EffectRequest,
        outcome: &EffectOutcome,
        handler_identity: &str,
        effect_id: u64,
    ) {
        let mut request = request.clone();
        request.effect_id = Some(effect_id);
        let exchange = EffectExchangeRecord {
            effect_id,
            handler_identity: handler_identity.to_string(),
            ordering_key: self.clock.tick,
            request,
            outcome: outcome.clone(),
        };
        self.sync_runtime_effect_from_exchange(&exchange);
        self.effect_exchanges.push(exchange);
        self.next_effect_id = self.next_effect_id.max(effect_id.saturating_add(1));
    }

    fn current_operation_owner(&self, session: Option<SessionId>) -> Option<String> {
        session.and_then(|sid| {
            self.sessions.get(sid).and_then(|session| {
                let session = session.lock().ok()?;
                session
                    .ownership()
                    .current
                    .as_ref()
                    .map(|capability| capability.owner_id.clone())
            })
        })
    }

    fn current_wal_sync_request(
        &self,
        session: Option<SessionId>,
        coro_id: usize,
    ) -> crate::durable::WalSyncRequest {
        let objects = self.semantic_objects();
        let agreement_state = objects
            .agreement_states
            .into_iter()
            .filter(|state| state.session == session)
            .max_by_key(|state| (state.last_updated_tick.unwrap_or(0), state.level.rank()));
        let operation_id = agreement_state
            .as_ref()
            .map(|state| state.operation_id.clone())
            .unwrap_or_else(|| format!("wal_sync:coro:{coro_id}"));
        let agreement_evidence = objects
            .agreement_evidence
            .into_iter()
            .filter(|evidence| evidence.operation_id == operation_id)
            .collect();
        crate::durable::WalSyncRequest {
            operation_id,
            downstream_coroutine_id: format!("coro:{coro_id}"),
            gate_level: agreement_state
                .as_ref()
                .map(|state| state.level)
                .unwrap_or(crate::semantic_objects::AgreementLevel::Finalized),
            agreement_state,
            agreement_evidence,
            tick: self.clock.tick,
        }
    }

    fn effect_operation_id(effect_id: u64) -> String {
        format!("effect:{effect_id}")
    }

    fn effect_invalidation_token(effect_id: u64) -> String {
        format!("effect:{effect_id}:live")
    }

    fn effect_status_phase(status: OutstandingEffectStatus) -> OperationPhase {
        match status {
            OutstandingEffectStatus::Pending => OperationPhase::Pending,
            OutstandingEffectStatus::Blocked => OperationPhase::Blocked,
            OutstandingEffectStatus::Succeeded => OperationPhase::Succeeded,
            OutstandingEffectStatus::Failed => OperationPhase::Failed,
            OutstandingEffectStatus::Cancelled => OperationPhase::Cancelled,
            OutstandingEffectStatus::Invalidated => OperationPhase::Failed,
        }
    }

    fn effect_terminal_publication(status: OutstandingEffectStatus) -> Option<String> {
        match status {
            OutstandingEffectStatus::Pending => None,
            OutstandingEffectStatus::Blocked => Some("effect.blocked".to_string()),
            OutstandingEffectStatus::Succeeded => Some("effect.succeeded".to_string()),
            OutstandingEffectStatus::Failed => Some("effect.failed".to_string()),
            OutstandingEffectStatus::Cancelled => Some("effect.cancelled".to_string()),
            OutstandingEffectStatus::Invalidated => Some("effect.invalidated".to_string()),
        }
    }

    fn progress_state_for_effect_status(status: OutstandingEffectStatus) -> ProgressState {
        match status {
            OutstandingEffectStatus::Pending => ProgressState::Pending,
            OutstandingEffectStatus::Blocked => ProgressState::Blocked,
            OutstandingEffectStatus::Succeeded => ProgressState::Succeeded,
            OutstandingEffectStatus::Failed => ProgressState::Failed,
            OutstandingEffectStatus::Cancelled => ProgressState::Cancelled,
            OutstandingEffectStatus::Invalidated => ProgressState::TimedOut,
        }
    }

    fn set_progress_contract_state(
        &mut self,
        operation_id: &str,
        session: Option<SessionId>,
        state: ProgressState,
        budget_ticks: Option<u64>,
        reason: Option<String>,
        refresh_progress_tick: bool,
    ) {
        let ordering_key = Some(self.clock.tick);
        if let Some(contract) = self
            .progress_contracts
            .iter_mut()
            .find(|contract| contract.operation_id == operation_id)
        {
            let previous = contract.state;
            contract.session = session.or(contract.session);
            contract.last_ordering_key = ordering_key;
            contract.bounded = budget_ticks.is_some();
            contract.budget_ticks = budget_ticks;
            if refresh_progress_tick || contract.last_progress_tick.is_none() {
                contract.last_progress_tick = ordering_key;
            }
            if previous != state
                && matches!(
                    state,
                    ProgressState::Blocked
                        | ProgressState::NoProgress
                        | ProgressState::Degraded
                        | ProgressState::TimedOut
                )
            {
                contract.escalated_at_tick = ordering_key;
            }
            contract.reason = reason.clone();
            contract.state = state;
            if previous != state {
                self.progress_transitions.push(ProgressTransition {
                    operation_id: operation_id.to_string(),
                    session,
                    from_state: previous,
                    to_state: state,
                    tick: self.clock.tick,
                    reason,
                });
            }
            return;
        }

        self.progress_contracts.push(ProgressContract {
            operation_id: operation_id.to_string(),
            session,
            state,
            last_ordering_key: ordering_key,
            bounded: budget_ticks.is_some(),
            budget_ticks,
            last_progress_tick: ordering_key,
            escalated_at_tick: matches!(
                state,
                ProgressState::Blocked
                    | ProgressState::NoProgress
                    | ProgressState::Degraded
                    | ProgressState::TimedOut
            )
            .then_some(self.clock.tick),
            reason,
        });
    }

    #[allow(clippy::too_many_lines)]
    fn evaluate_progress_contracts(&mut self) -> Result<(), ProtocolMachineError> {
        let active_effect_ids: Vec<_> = self
            .outstanding_effects
            .iter()
            .filter(|effect| {
                matches!(
                    effect.status,
                    OutstandingEffectStatus::Pending | OutstandingEffectStatus::Blocked
                )
            })
            .map(|effect| effect.effect_id)
            .collect();

        for effect_id in active_effect_ids {
            let Some(effect_index) = self
                .outstanding_effects
                .iter()
                .position(|effect| effect.effect_id == effect_id)
            else {
                continue;
            };
            let effect = self.outstanding_effects[effect_index].clone();
            let budget = effect.budget_ticks.ok_or_else(|| {
                ProtocolMachineError::HandlerError(EffectFailure::contract_violation(format!(
                    "[host-contract] semantic-path effect {} requires a bounded wait budget",
                    effect.effect_id
                )))
            })?;
            let Some(contract) = self
                .progress_contracts
                .iter()
                .find(|contract| contract.operation_id == effect.operation_id)
            else {
                return Err(ProtocolMachineError::HandlerError(
                    EffectFailure::contract_violation(format!(
                        "[host-contract] outstanding effect {} requires a live progress contract",
                        effect.effect_id
                    )),
                ));
            };
            let since = contract
                .escalated_at_tick
                .or(contract.last_progress_tick)
                .unwrap_or(effect.ordering_key);
            let elapsed = self.clock.tick.saturating_sub(since);
            let target_state = match contract.state {
                ProgressState::Pending if elapsed < budget => ProgressState::Pending,
                ProgressState::Pending => ProgressState::Blocked,
                ProgressState::Blocked if elapsed < budget => ProgressState::Blocked,
                ProgressState::Blocked => ProgressState::NoProgress,
                ProgressState::NoProgress if elapsed < budget => ProgressState::NoProgress,
                ProgressState::NoProgress => ProgressState::Degraded,
                ProgressState::Degraded if elapsed < budget => ProgressState::Degraded,
                ProgressState::Degraded => ProgressState::TimedOut,
                ProgressState::Succeeded
                | ProgressState::Failed
                | ProgressState::Cancelled
                | ProgressState::TimedOut
                | ProgressState::HandedOff => continue,
            };

            let reason = match target_state {
                ProgressState::Pending => None,
                ProgressState::Blocked => {
                    Some("waiting on bounded semantic-path effect".to_string())
                }
                ProgressState::NoProgress => {
                    Some("no progress observed within bounded wait budget".to_string())
                }
                ProgressState::Degraded => {
                    Some("bounded wait degraded after repeated no-progress windows".to_string())
                }
                ProgressState::TimedOut => {
                    Some("bounded wait exhausted; late results are invalid".to_string())
                }
                ProgressState::Succeeded
                | ProgressState::Failed
                | ProgressState::Cancelled
                | ProgressState::HandedOff => None,
            };

            if matches!(target_state, ProgressState::Blocked)
                && effect.status == OutstandingEffectStatus::Pending
            {
                if let Some(effect_mut) = self
                    .outstanding_effects
                    .iter_mut()
                    .find(|effect_mut| effect_mut.effect_id == effect_id)
                {
                    effect_mut.status = OutstandingEffectStatus::Blocked;
                    effect_mut.outputs = json!({
                        "status": "blocked",
                        "reason": reason.clone().unwrap_or_default(),
                    });
                    effect_mut.ordering_key = self.clock.tick;
                }
                if let Some(operation) = self
                    .operation_instances
                    .iter_mut()
                    .find(|operation| operation.operation_id == effect.operation_id)
                {
                    operation.phase = OperationPhase::Blocked;
                    operation.terminal_publication = Some("effect.blocked".to_string());
                }
            }

            if matches!(
                target_state,
                ProgressState::NoProgress | ProgressState::Degraded
            ) {
                if let Some(operation) = self
                    .operation_instances
                    .iter_mut()
                    .find(|operation| operation.operation_id == effect.operation_id)
                {
                    operation.phase = OperationPhase::Blocked;
                    operation.terminal_publication = Some(match target_state {
                        ProgressState::NoProgress => "effect.no_progress".to_string(),
                        ProgressState::Degraded => "effect.degraded".to_string(),
                        _ => unreachable!(),
                    });
                }
            }

            if target_state == ProgressState::TimedOut {
                if let Some(effect_mut) = self
                    .outstanding_effects
                    .iter_mut()
                    .find(|effect_mut| effect_mut.effect_id == effect_id)
                {
                    effect_mut.status = OutstandingEffectStatus::Invalidated;
                    effect_mut.outputs = json!({
                        "status": "invalidated",
                        "reason": reason.clone().unwrap_or_default(),
                    });
                    effect_mut.completed_at_tick = Some(self.clock.tick);
                    effect_mut.ordering_key = self.clock.tick;
                }
                if let Some(operation) = self
                    .operation_instances
                    .iter_mut()
                    .find(|operation| operation.operation_id == effect.operation_id)
                {
                    operation.phase = OperationPhase::TimedOut;
                    operation.terminal_publication = Some("effect.timed_out".to_string());
                }
            }

            self.set_progress_contract_state(
                &effect.operation_id,
                effect.session,
                target_state,
                Some(budget),
                reason,
                false,
            );
        }

        Ok(())
    }

    #[allow(dead_code)]
    fn issue_runtime_effect(
        &mut self,
        effect_kind: &str,
        session: Option<SessionId>,
        handler_identity: &str,
        inputs: serde_json::Value,
    ) -> u64 {
        let effect_id = self.next_effect_id;
        self.next_effect_id = self.next_effect_id.saturating_add(1);
        let operation_id = Self::effect_operation_id(effect_id);
        let (effect_interface, effect_operation) =
            infer_effect_interface_and_operation(effect_kind);
        let owner_id = self.current_operation_owner(session);
        let budget_ticks = Some(1);

        self.outstanding_effects.push(OutstandingEffect {
            effect_id,
            operation_id: operation_id.clone(),
            session,
            owner_id: owner_id.clone(),
            effect_interface: effect_interface.clone(),
            effect_operation: effect_operation.clone(),
            effect_kind: effect_kind.to_string(),
            handler_identity: handler_identity.to_string(),
            status: OutstandingEffectStatus::Pending,
            ordering_key: self.clock.tick,
            budget_ticks,
            retry_policy: "forbid_late_results".to_string(),
            invalidation_token: Self::effect_invalidation_token(effect_id),
            completed_at_tick: None,
            inputs,
            outputs: json!({ "status": "pending" }),
        });
        self.operation_instances.push(OperationInstance {
            operation_id: operation_id.clone(),
            session,
            owner_id,
            kind: effect_operation.unwrap_or_else(|| effect_kind.to_string()),
            phase: OperationPhase::Pending,
            handler_identity: Some(handler_identity.to_string()),
            effect_ids: vec![effect_id],
            dependent_operation_ids: Vec::new(),
            terminal_publication: None,
            budget_ticks,
            requires_proof: false,
        });
        self.set_progress_contract_state(
            &operation_id,
            session,
            ProgressState::Pending,
            budget_ticks,
            None,
            true,
        );
        effect_id
    }

    fn sync_runtime_effect_from_trace_entry(&mut self, entry: &EffectTraceEntry) {
        if self
            .outstanding_effects
            .iter()
            .any(|effect| effect.effect_id == entry.effect_id)
        {
            return;
        }

        let session = entry
            .inputs
            .get("session")
            .and_then(serde_json::Value::as_u64)
            .and_then(|sid| usize::try_from(sid).ok())
            .or_else(|| {
                entry
                    .inputs
                    .get("sid")
                    .and_then(serde_json::Value::as_u64)
                    .and_then(|sid| usize::try_from(sid).ok())
            });
        let owner_id = self.current_operation_owner(session);
        let operation_id = Self::effect_operation_id(entry.effect_id);
        let status = match entry
            .outputs
            .get("status")
            .and_then(serde_json::Value::as_str)
        {
            Some("blocked") => OutstandingEffectStatus::Blocked,
            Some("failure") => OutstandingEffectStatus::Failed,
            Some("cancelled") => OutstandingEffectStatus::Cancelled,
            Some("invalidated") => OutstandingEffectStatus::Invalidated,
            _ => OutstandingEffectStatus::Succeeded,
        };
        let budget_ticks = Some(1);

        self.outstanding_effects.push(OutstandingEffect {
            effect_id: entry.effect_id,
            operation_id: operation_id.clone(),
            session,
            owner_id: owner_id.clone(),
            effect_interface: entry.effect_interface.clone(),
            effect_operation: entry.effect_operation.clone(),
            effect_kind: entry.effect_kind.clone(),
            handler_identity: entry.handler_identity.clone(),
            status,
            ordering_key: entry.ordering_key,
            budget_ticks,
            retry_policy: "forbid_late_results".to_string(),
            invalidation_token: Self::effect_invalidation_token(entry.effect_id),
            completed_at_tick: Some(self.clock.tick),
            inputs: entry.inputs.clone(),
            outputs: entry.outputs.clone(),
        });
        self.operation_instances.push(OperationInstance {
            operation_id: operation_id.clone(),
            session,
            owner_id,
            kind: entry
                .effect_operation
                .clone()
                .unwrap_or_else(|| entry.effect_kind.clone()),
            phase: Self::effect_status_phase(status),
            handler_identity: Some(entry.handler_identity.clone()),
            effect_ids: vec![entry.effect_id],
            dependent_operation_ids: Vec::new(),
            terminal_publication: Self::effect_terminal_publication(status),
            budget_ticks,
            requires_proof: false,
        });
        self.set_progress_contract_state(
            &operation_id,
            session,
            Self::progress_state_for_effect_status(status),
            budget_ticks,
            entry
                .outputs
                .get("reason")
                .and_then(serde_json::Value::as_str)
                .map(ToString::to_string),
            true,
        );
    }

    fn sync_runtime_effect_from_exchange(&mut self, exchange: &EffectExchangeRecord) {
        if self
            .outstanding_effects
            .iter()
            .any(|effect| effect.effect_id == exchange.effect_id)
        {
            return;
        }

        let status = match &exchange.outcome.status {
            crate::effect::EffectOutcomeStatus::Success => OutstandingEffectStatus::Succeeded,
            crate::effect::EffectOutcomeStatus::Blocked => OutstandingEffectStatus::Blocked,
            crate::effect::EffectOutcomeStatus::Failure { .. } => OutstandingEffectStatus::Failed,
        };
        let effect_kind = match &exchange.request.body {
            crate::effect::EffectRequestBody::SendDecision { .. } => "send_decision",
            crate::effect::EffectRequestBody::Receive { .. } => "handle_recv",
            crate::effect::EffectRequestBody::Choose { .. } => "handle_choose",
            crate::effect::EffectRequestBody::InvokeStep { .. } => "invoke_step",
            crate::effect::EffectRequestBody::Acquire { .. } => "handle_acquire",
            crate::effect::EffectRequestBody::Release { .. } => "handle_release",
            crate::effect::EffectRequestBody::TopologyEvents { .. } => "topology_events",
            crate::effect::EffectRequestBody::WalSync { .. } => "wal_sync",
            crate::effect::EffectRequestBody::OutputConditionHint { .. } => "output_condition_hint",
        };
        let operation_id = exchange
            .request
            .operation_id
            .clone()
            .unwrap_or_else(|| Self::effect_operation_id(exchange.effect_id));
        let session = exchange.request.session;
        let owner_id = self.current_operation_owner(session);
        let budget_ticks = Some(1);
        let inputs =
            serde_json::to_value(&exchange.request).expect("typed effect request should serialize");
        let outputs =
            serde_json::to_value(&exchange.outcome).expect("typed effect outcome should serialize");

        self.outstanding_effects.push(OutstandingEffect {
            effect_id: exchange.effect_id,
            operation_id: operation_id.clone(),
            session,
            owner_id: owner_id.clone(),
            effect_interface: Some(exchange.request.metadata.interface_name.clone()),
            effect_operation: Some(exchange.request.metadata.operation_name.clone()),
            effect_kind: effect_kind.to_string(),
            handler_identity: exchange.handler_identity.clone(),
            status,
            ordering_key: exchange.ordering_key,
            budget_ticks,
            retry_policy: "forbid_late_results".to_string(),
            invalidation_token: Self::effect_invalidation_token(exchange.effect_id),
            completed_at_tick: Some(self.clock.tick),
            inputs,
            outputs,
        });
        self.operation_instances.push(OperationInstance {
            operation_id: operation_id.clone(),
            session,
            owner_id,
            kind: exchange.request.metadata.operation_name.clone(),
            phase: Self::effect_status_phase(status),
            handler_identity: Some(exchange.handler_identity.clone()),
            effect_ids: vec![exchange.effect_id],
            dependent_operation_ids: Vec::new(),
            terminal_publication: Self::effect_terminal_publication(status),
            budget_ticks,
            requires_proof: false,
        });
        self.set_progress_contract_state(
            &operation_id,
            session,
            Self::progress_state_for_effect_status(status),
            budget_ticks,
            match &exchange.outcome.status {
                crate::effect::EffectOutcomeStatus::Failure { failure } => {
                    Some(failure.message.clone())
                }
                crate::effect::EffectOutcomeStatus::Success
                | crate::effect::EffectOutcomeStatus::Blocked => None,
            },
            true,
        );
    }

    #[allow(dead_code)]
    fn complete_runtime_effect(
        &mut self,
        effect_id: u64,
        status: OutstandingEffectStatus,
        outputs: serde_json::Value,
    ) -> Result<(), ProtocolMachineError> {
        let Some(effect_index) = self
            .outstanding_effects
            .iter()
            .position(|effect| effect.effect_id == effect_id)
        else {
            return Err(ProtocolMachineError::HandlerError(EffectFailure::contract_violation(
                format!(
                    "[host-contract] completion for effect {effect_id} requires a live outstanding-effect record"
                ),
            )));
        };
        if !matches!(
            self.outstanding_effects[effect_index].status,
            OutstandingEffectStatus::Pending | OutstandingEffectStatus::Blocked
        ) {
            return Err(ProtocolMachineError::HandlerError(
                EffectFailure::contract_violation(format!(
                    "[host-contract] late result for effect {effect_id} rejected after {:?}",
                    self.outstanding_effects[effect_index].status
                )),
            ));
        }
        let operation_id;
        let session;
        let budget_ticks;
        let reason;
        {
            let effect = &mut self.outstanding_effects[effect_index];
            effect.status = status;
            effect.outputs = outputs;
            effect.completed_at_tick = Some(self.clock.tick);
            effect.ordering_key = self.clock.tick;
            operation_id = effect.operation_id.clone();
            session = effect.session;
            budget_ticks = effect.budget_ticks;
            reason = effect
                .outputs
                .get("reason")
                .and_then(serde_json::Value::as_str)
                .map(ToString::to_string);
        }

        if let Some(operation) = self
            .operation_instances
            .iter_mut()
            .find(|operation| operation.operation_id == operation_id)
        {
            operation.phase = Self::effect_status_phase(status);
            operation.terminal_publication = Self::effect_terminal_publication(status);
        }
        self.set_progress_contract_state(
            &operation_id,
            session,
            Self::progress_state_for_effect_status(status),
            budget_ticks,
            reason,
            true,
        );
        Ok(())
    }

    fn invalidate_outstanding_effects_for_session(&mut self, session: SessionId, reason: &str) {
        let mut invalidated = Vec::new();
        for effect in &mut self.outstanding_effects {
            if effect.session != Some(session) {
                continue;
            }
            if !matches!(
                effect.status,
                OutstandingEffectStatus::Pending | OutstandingEffectStatus::Blocked
            ) {
                continue;
            }
            effect.status = OutstandingEffectStatus::Invalidated;
            effect.outputs = json!({
                "status": "invalidated",
                "reason": reason,
            });
            effect.completed_at_tick = Some(self.clock.tick);
            effect.ordering_key = self.clock.tick;
            invalidated.push(effect.operation_id.clone());
        }

        for operation in &mut self.operation_instances {
            if invalidated.contains(&operation.operation_id) {
                operation.phase = OperationPhase::Failed;
                operation.terminal_publication = Some("effect.invalidated".to_string());
            }
        }
        for operation_id in invalidated {
            self.set_progress_contract_state(
                &operation_id,
                Some(session),
                ProgressState::Failed,
                Some(1),
                Some(reason.to_string()),
                true,
            );
        }
    }

    fn apply_semantic_handoff_obligations(&mut self, receipt: &DelegationReceipt) {
        let handoff_operation_id = format!("handoff:{}", receipt.receipt_id);
        let old_owner = Self::coro_owner_id(receipt.from_coro);
        let new_owner = Self::coro_owner_id(receipt.to_coro);
        let mut invalidated_operations = Vec::new();

        for effect in &mut self.outstanding_effects {
            if effect.session != Some(receipt.session) {
                continue;
            }
            match effect.status {
                OutstandingEffectStatus::Pending => {
                    effect.owner_id = Some(new_owner.clone());
                }
                OutstandingEffectStatus::Blocked => {
                    effect.status = OutstandingEffectStatus::Invalidated;
                    effect.owner_id = Some(new_owner.clone());
                    effect.outputs = json!({
                        "status": "invalidated",
                        "reason": "semantic handoff invalidated blocked effect",
                        "revoked_owner_id": old_owner,
                        "activated_owner_id": new_owner,
                        "handoff_id": receipt.receipt_id,
                    });
                    effect.completed_at_tick = Some(self.clock.tick);
                    effect.ordering_key = self.clock.tick;
                    invalidated_operations.push(effect.operation_id.clone());
                }
                OutstandingEffectStatus::Succeeded
                | OutstandingEffectStatus::Failed
                | OutstandingEffectStatus::Cancelled
                | OutstandingEffectStatus::Invalidated => {}
            }
        }

        for operation in &mut self.operation_instances {
            if operation.session != Some(receipt.session) {
                continue;
            }
            if !operation
                .dependent_operation_ids
                .iter()
                .any(|id| id == &handoff_operation_id)
            {
                operation
                    .dependent_operation_ids
                    .push(handoff_operation_id.clone());
            }
            match operation.phase {
                OperationPhase::Pending | OperationPhase::Blocked => {
                    operation.owner_id = Some(new_owner.clone());
                }
                OperationPhase::Succeeded
                | OperationPhase::Failed
                | OperationPhase::Cancelled
                | OperationPhase::TimedOut
                | OperationPhase::HandedOff => {}
            }
            if invalidated_operations.contains(&operation.operation_id) {
                operation.phase = OperationPhase::Failed;
                operation.terminal_publication = Some("effect.invalidated".to_string());
            }
        }
        for operation_id in invalidated_operations {
            self.set_progress_contract_state(
                &operation_id,
                Some(receipt.session),
                ProgressState::Failed,
                Some(1),
                Some("semantic handoff invalidated blocked effect".to_string()),
                true,
            );
        }
        self.set_progress_contract_state(
            &handoff_operation_id,
            Some(receipt.session),
            ProgressState::HandedOff,
            Some(1),
            Some("semantic handoff committed".to_string()),
            true,
        );
    }
}