orchestral-runtime 0.3.1

A runtime for reliable, interactive AI agents.
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
//! Host-side control plane for one provider binding.
//!
//! This is the first production vertical slice over Agent Protocol v1. It owns
//! normalized sequencing and the reducer projection for process-lifetime Runs.
//! Durable storage is deliberately a later store implementation; callers must
//! not mistake this in-memory controller for crash recovery.

use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

use futures_util::StreamExt;
use orchestral_core::agent_protocol::{
    reference::{
        AgentContinuityState, AgentRunReducer, AgentRunStatus, ApplyOutcome,
        ReconciliationProofVerifier, RecoveryReplayPolicy, SequencedApply,
    },
    spi::{
        AgentJournalStore, AgentJournalStoreError, AgentProvider, AgentProviderStream,
        AgentRecoveryRequest, AgentRunCatalogEntry, AgentRunRegistration, AgentStartError,
        InMemoryAgentJournalStore, StoredAgentRun,
    },
    wire::{
        AgentCommand, AgentCommandEnvelope, AgentEvent, AgentEventAuthority, AgentEventDraft,
        AgentEventId, AgentExecutionRef, AgentJournalRecord, AgentProtocolError,
        AgentProtocolErrorCode, AgentProviderStreamItem, AgentRunEnvelope, AgentRunState,
        AgentRunView, AgentStartRequest, AgentTelemetryEnvelope, CommandAck, CommandId, Content,
        Digest, ProviderBindingRef, ReconciliationProof, ReconciliationProofRef, RunId,
    },
};
use thiserror::Error;
use tokio::sync::{broadcast, Mutex, Notify, RwLock};
use tokio::time::timeout;

const RECOVERY_PREFIX_ITEM_TIMEOUT: Duration = Duration::from_secs(2);

/// Ordered durable facts and best-effort live telemetry emitted by a Run.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum AgentControlEvent {
    Durable(Arc<AgentJournalRecord>),
    Telemetry(AgentTelemetryEnvelope),
}

/// Host control-plane failure. Run failures remain durable Agent events and
/// are not represented by this infrastructure error.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AgentControlError {
    #[error(transparent)]
    Protocol(#[from] AgentProtocolError),
    #[error(transparent)]
    Start(#[from] AgentStartError),
    #[error(transparent)]
    Journal(#[from] AgentJournalStoreError),
    #[error("Agent Run not found: {0}")]
    RunNotFound(RunId),
    #[error("Agent Run continuity is unknown: {0}")]
    ContinuityUnknown(RunId),
    #[error("Agent recovery evidence did not match the committed Run prefix: {0}")]
    RecoveryMismatch(RunId),
}

struct RunEntry {
    request: AgentStartRequest,
    execution: AgentExecutionRef,
    reducer: AgentRunReducer,
    journal: Vec<AgentJournalRecord>,
}

struct RunSlot {
    entry: Mutex<RunEntry>,
    recovery_gate: Mutex<()>,
    events: broadcast::Sender<AgentControlEvent>,
    changed: Notify,
}

impl RunSlot {
    fn publish(&self, event: AgentControlEvent) {
        let _ = self.events.send(event);
        self.changed.notify_waiters();
    }
}

/// Process-lifetime Agent controller for one immutable Provider binding.
///
/// The controller, not the Provider, owns normalized `run_seq`, journal
/// records, public inspection, command acknowledgement, and EOF-to-Unknown
/// handling. One controller may host many isolated Runs.
pub struct AgentController {
    provider: Arc<dyn AgentProvider>,
    binding_ref: ProviderBindingRef,
    descriptor: orchestral_core::agent_protocol::wire::AgentDescriptorEnvelope,
    journal_store: Arc<dyn AgentJournalStore>,
    runs: RwLock<BTreeMap<RunId, Arc<RunSlot>>>,
    start_gate: Mutex<()>,
    event_buffer: usize,
}

impl AgentController {
    pub fn new(
        provider: Arc<dyn AgentProvider>,
        binding_ref: ProviderBindingRef,
    ) -> Result<Self, AgentProtocolError> {
        Self::with_event_buffer(provider, binding_ref, 256)
    }

    pub fn with_event_buffer(
        provider: Arc<dyn AgentProvider>,
        binding_ref: ProviderBindingRef,
        event_buffer: usize,
    ) -> Result<Self, AgentProtocolError> {
        Self::with_journal_store_and_event_buffer(
            provider,
            binding_ref,
            Arc::new(InMemoryAgentJournalStore::default()),
            event_buffer,
        )
    }

    pub fn with_journal_store(
        provider: Arc<dyn AgentProvider>,
        binding_ref: ProviderBindingRef,
        journal_store: Arc<dyn AgentJournalStore>,
    ) -> Result<Self, AgentProtocolError> {
        Self::with_journal_store_and_event_buffer(provider, binding_ref, journal_store, 256)
    }

    pub fn with_journal_store_and_event_buffer(
        provider: Arc<dyn AgentProvider>,
        binding_ref: ProviderBindingRef,
        journal_store: Arc<dyn AgentJournalStore>,
        event_buffer: usize,
    ) -> Result<Self, AgentProtocolError> {
        if binding_ref.is_empty() || event_buffer == 0 {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::InvalidSpec,
                "Agent controller requires a Provider binding and a non-zero event buffer",
            ));
        }
        let descriptor = provider.describe();
        descriptor.validate_integrity()?;
        Ok(Self {
            provider,
            binding_ref,
            descriptor,
            journal_store,
            runs: RwLock::new(BTreeMap::new()),
            start_gate: Mutex::new(()),
            event_buffer,
        })
    }

    pub fn descriptor(&self) -> &orchestral_core::agent_protocol::wire::AgentDescriptorEnvelope {
        &self.descriptor
    }

    /// Starts or idempotently reopens one immutable Run and begins consuming
    /// its atomic Provider stream in the background.
    pub async fn start(
        self: &Arc<Self>,
        run: AgentRunEnvelope,
    ) -> Result<AgentExecutionRef, AgentControlError> {
        let request = AgentStartRequest::new(run, self.binding_ref.clone(), &self.descriptor)?;
        let run_id = request.run.spec.run_id.clone();
        let _start_guard = self.start_gate.lock().await;

        if let Some(slot) = self.runs.read().await.get(&run_id).cloned() {
            let entry = slot.entry.lock().await;
            if entry.request != request {
                return Err(AgentProtocolError::new(
                    AgentProtocolErrorCode::RunIdConflict,
                    "run_id already belongs to another immutable start request",
                )
                .into());
            }
            return Ok(entry.execution.clone());
        }

        if let Some(stored) = self.journal_store.load_run(&run_id).await? {
            if stored.registration.request != request {
                return Err(AgentProtocolError::new(
                    AgentProtocolErrorCode::RunIdConflict,
                    "run_id already belongs to another durable start request",
                )
                .into());
            }
            let slot = self.slot_from_stored(stored)?;
            let execution = slot.entry.lock().await.execution.clone();
            self.runs.write().await.insert(run_id.clone(), slot.clone());
            let view = slot.entry.lock().await.reducer.view();
            if !view.state.is_terminal() && view.state.status() != AgentRunStatus::Unknown {
                self.mark_continuity_lost(
                    &run_id,
                    &slot,
                    "Host controller restarted without a continuously attached Provider stream"
                        .to_owned(),
                )
                .await?;
            }
            return Ok(execution);
        }

        let observed_descriptor = self.provider.describe();
        if observed_descriptor != self.descriptor {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::RunIdConflict,
                "Provider descriptor changed after controller binding",
            )
            .into());
        }
        let start = self.provider.start(request.clone()).await?;
        start.execution.validate_for(&request, &self.descriptor)?;
        let compatibility = self
            .descriptor
            .descriptor
            .check_run_compatibility(&request.run)
            .map_err(|rejection| {
                AgentProtocolError::new(AgentProtocolErrorCode::Unsupported, rejection.to_string())
                    .with_details(rejection.details)
            })?;
        start
            .admission
            .validate_against(&request.run, &compatibility)?;

        let mut reducer = AgentRunReducer::new(
            start.execution.clone(),
            &request,
            &self.descriptor,
            start.admission.clone(),
        )?;
        let mut journal = Vec::with_capacity(start.admission.skipped_optional_bindings.len() + 4);
        push_sequenced(
            &mut journal,
            reducer.apply_host_draft(AgentEventDraft {
                event_id: AgentEventId::new(format!("host-run-accepted-{}", run_id.as_str())),
                run_id: run_id.clone(),
                causation_id: None,
                source_fingerprint: None,
                payload: AgentEvent::RunAccepted {
                    session_id: request.run.spec.session_id.clone(),
                    spec_digest: request.run.spec_digest.clone(),
                },
            })?,
        );
        for skip in &start.admission.skipped_optional_bindings {
            push_sequenced(
                &mut journal,
                reducer.apply_host_draft(AgentEventDraft {
                    event_id: AgentEventId::new(format!(
                        "host-resource-skip-{}-{}",
                        run_id.as_str(),
                        skip.binding_id.as_str()
                    )),
                    run_id: run_id.clone(),
                    causation_id: None,
                    source_fingerprint: None,
                    payload: AgentEvent::ResourceBindingSkipped { skip: skip.clone() },
                })?,
            );
        }

        let (events, _) = broadcast::channel(self.event_buffer);
        self.journal_store
            .create_run(StoredAgentRun {
                registration: AgentRunRegistration {
                    request: request.clone(),
                    execution: start.execution.clone(),
                    admission: start.admission.clone(),
                },
                records: journal.clone(),
            })
            .await?;
        let slot = Arc::new(RunSlot {
            entry: Mutex::new(RunEntry {
                request,
                execution: start.execution.clone(),
                reducer,
                journal: journal.clone(),
            }),
            recovery_gate: Mutex::new(()),
            events,
            changed: Notify::new(),
        });
        self.runs.write().await.insert(run_id.clone(), slot.clone());
        for record in journal {
            slot.publish(AgentControlEvent::Durable(Arc::new(record)));
        }

        let controller = Arc::clone(self);
        tokio::spawn(async move {
            controller.drive_stream(run_id, slot, start.stream).await;
        });
        Ok(start.execution)
    }

    /// Returns the bounded Host projection for a Run.
    pub async fn inspect(&self, run_id: &RunId) -> Result<AgentRunView, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        let view = slot.entry.lock().await.reducer.view();
        Ok(view)
    }

    /// Returns the immutable initial input from the registered Run spec.
    ///
    /// This is separate from the bounded public Run projection because it is
    /// conversation content, not reducible execution state. Authenticated Host
    /// surfaces can use it to reconstruct a transcript without duplicating the
    /// input in a transport-specific registry.
    pub async fn initial_input(&self, run_id: &RunId) -> Result<Vec<Content>, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        let input = slot.entry.lock().await.request.run.spec.input.clone();
        Ok(input)
    }

    /// Returns digest-bound metadata from the immutable Run specification.
    pub async fn run_extensions(
        &self,
        run_id: &RunId,
    ) -> Result<orchestral_core::agent_protocol::wire::Extensions, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        let extensions = slot.entry.lock().await.request.run.spec.extensions.clone();
        Ok(extensions)
    }

    /// Lists durable Runs directly from the Host journal. Transport-specific
    /// session registries must not maintain a second Run ownership index.
    pub async fn catalog_runs(&self) -> Result<Vec<AgentRunCatalogEntry>, AgentControlError> {
        Ok(self.journal_store.catalog_runs().await?)
    }

    /// Reports whether a durable Run was registered against this controller's
    /// current immutable Provider contract.
    ///
    /// Discovery surfaces use this before enriching a native session with a
    /// Host-controlled Run. A Provider upgrade may legitimately change its
    /// descriptor digest; those older journals remain durable history, but
    /// they cannot be rehydrated or controlled by the new binding.
    pub async fn can_control_run(&self, run_id: &RunId) -> Result<bool, AgentControlError> {
        if let Some(slot) = self.runs.read().await.get(run_id).cloned() {
            let entry = slot.entry.lock().await;
            return Ok(self.registration_matches_current(&entry.request, &entry.execution));
        }
        let Some(stored) = self.journal_store.load_run(run_id).await? else {
            return Ok(false);
        };
        stored.validate_shape()?;
        Ok(self.registration_matches_current(
            &stored.registration.request,
            &stored.registration.execution,
        ))
    }

    pub async fn has_run(&self, run_id: &RunId) -> Result<bool, AgentControlError> {
        if self.runs.read().await.contains_key(run_id) {
            return Ok(true);
        }
        Ok(self.journal_store.load_run(run_id).await?.is_some())
    }

    /// Replays normalized durable facts after the supplied Run sequence.
    pub async fn events(
        &self,
        run_id: &RunId,
        after_run_seq: u64,
    ) -> Result<Vec<AgentJournalRecord>, AgentControlError> {
        self.run_slot(run_id).await?;
        Ok(self.journal_store.records(run_id, after_run_seq).await?)
    }

    /// Subscribes to events published after subscription. Call `events` first
    /// (and again after broadcast lag) for lossless durable replay.
    pub async fn subscribe(
        &self,
        run_id: &RunId,
    ) -> Result<broadcast::Receiver<AgentControlEvent>, AgentControlError> {
        Ok(self.run_slot(run_id).await?.events.subscribe())
    }

    /// Records, forwards, and durably projects one idempotent Provider command.
    /// Replays return the existing Host acknowledgement without forwarding.
    pub async fn command(
        &self,
        command: AgentCommandEnvelope,
    ) -> Result<CommandAck, AgentControlError> {
        command.verify_digest()?;
        if orchestral_core::agent_protocol::wire::QueuedInputOperation::from_command(&command)?
            .is_some()
            && !orchestral_core::agent_protocol::wire::QueuedInputOperation::is_supported(
                &self.descriptor.descriptor,
            )
        {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::Unsupported,
                "this Agent does not support queued input; use immediate Steer explicitly",
            )
            .into());
        }
        let slot = self.run_slot(&command.run_id).await?;
        let mut entry = slot.entry.lock().await;
        if entry
            .reducer
            .recorded_command(&command.command_id)
            .is_some_and(|recorded| recorded != &command)
        {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::DuplicateConflict,
                "command_id was already used with a different immutable command",
            )
            .into());
        }
        if let Ok(ack) = entry.reducer.command_ack(&command.command_id, true) {
            return Ok(ack);
        }

        let mut next_reducer = entry.reducer.clone();
        let received = next_reducer.apply_host_draft(AgentEventDraft {
            event_id: AgentEventId::new(format!(
                "host-command-received-{}-{}",
                command.run_id.as_str(),
                command.command_id.as_str()
            )),
            run_id: command.run_id.clone(),
            causation_id: Some(command.command_id.clone()),
            source_fingerprint: None,
            payload: AgentEvent::CommandReceived {
                command: command.clone(),
            },
        })?;
        let duplicate = matches!(received.outcome, ApplyOutcome::ExactDuplicate);
        self.commit_sequenced(&slot, &mut entry, next_reducer, received)
            .await?;

        let disposition = self
            .provider
            .command(&entry.execution, command.clone())
            .await?;
        disposition.validate_for(&command)?;
        let mut next_reducer = entry.reducer.clone();
        let disposition_event = next_reducer.apply_provider_draft(disposition.to_event_draft()?)?;
        self.commit_sequenced(&slot, &mut entry, next_reducer, disposition_event)
            .await?;
        Ok(entry.reducer.command_ack(&command.command_id, duplicate)?)
    }

    /// Requests cancellation with a fresh idempotency identity. Callers that
    /// need retry-stable command IDs should construct an `AgentCommandEnvelope`
    /// once and use [`Self::command`] directly.
    pub async fn cancel(
        &self,
        run_id: &RunId,
        reason: impl Into<String>,
    ) -> Result<CommandAck, AgentControlError> {
        let command = AgentCommandEnvelope::new(
            CommandId::new(format!("host-cancel-{}", uuid::Uuid::new_v4())),
            run_id.clone(),
            None,
            AgentCommand::Cancel {
                reason: reason.into(),
            },
        )?;
        self.command(command).await
    }

    /// Reads immutable command identity without recovering or attaching native
    /// work. Session-level retries use this even after the target Run ends.
    pub async fn recorded_command(
        &self,
        run_id: &RunId,
        command_id: &CommandId,
    ) -> Result<Option<AgentCommandEnvelope>, AgentControlError> {
        if let Some(slot) = self.runs.read().await.get(run_id).cloned() {
            return Ok(slot
                .entry
                .lock()
                .await
                .reducer
                .recorded_command(command_id)
                .cloned());
        }
        let Some(stored) = self.journal_store.load_run(run_id).await? else {
            return Ok(None);
        };
        stored.validate_shape()?;
        Ok(stored
            .records
            .into_iter()
            .find_map(|record| match record.event.payload {
                AgentEvent::CommandReceived { command } if &command.command_id == command_id => {
                    Some(command)
                }
                _ => None,
            }))
    }

    pub async fn command_ack(
        &self,
        run_id: &RunId,
        command_id: &CommandId,
    ) -> Result<CommandAck, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        let entry = slot.entry.lock().await;
        let ack = entry.reducer.command_ack(command_id, true)?;
        Ok(ack)
    }

    /// Conservatively reattaches a Provider stream after a Host-recorded EOF.
    ///
    /// The reference controller requires the recovered stream to replay the
    /// complete committed Provider prefix with stable event IDs and draft
    /// digests. It restores continuity only after that prefix matches; opaque
    /// adapters that cannot provide such evidence remain `Unknown` rather
    /// than being guessed healthy.
    pub async fn recover(
        self: &Arc<Self>,
        run_id: &RunId,
    ) -> Result<AgentRunView, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        let _recovery_guard = slot.recovery_gate.lock().await;
        let (
            start_request,
            execution,
            expected_provider_prefix,
            committed_provider_prefix,
            last_confirmed_seq,
            loss_event_digest,
        ) = {
            let entry = slot.entry.lock().await;
            let AgentRunState::Unknown {
                last_confirmed_seq, ..
            } = entry.reducer.state()
            else {
                return Err(AgentProtocolError::new(
                    AgentProtocolErrorCode::InvalidTransition,
                    "recover requires a Run whose continuity is Unknown",
                )
                .into());
            };
            let loss_record = entry.journal.last().ok_or_else(|| {
                AgentProtocolError::new(
                    AgentProtocolErrorCode::InvalidTransition,
                    "Unknown Run has no continuity-loss journal record",
                )
            })?;
            if !matches!(
                (&loss_record.authority, &loss_record.event.payload),
                (
                    AgentEventAuthority::Host { .. },
                    AgentEvent::ContinuityLost { .. }
                )
            ) {
                return Err(AgentProtocolError::new(
                    AgentProtocolErrorCode::InvalidTransition,
                    "automatic recovery is allowed only after a Host-recorded stream loss",
                )
                .into());
            }
            let pending_request_ids = entry
                .reducer
                .view()
                .pending_requests
                .iter()
                .map(|request| request.request_id.clone())
                .collect();
            let provider_records = entry
                .journal
                .iter()
                .filter(|record| {
                    record.event.run_seq <= last_confirmed_seq
                        && matches!(record.authority, AgentEventAuthority::Provider)
                })
                .collect::<Vec<_>>();
            let prefix = provider_records
                .iter()
                .map(|record| {
                    (
                        record.event.event_id.clone(),
                        record.draft_digest.clone(),
                        record
                            .event
                            .payload
                            .recovery_replay_policy(&pending_request_ids)
                            == RecoveryReplayPolicy::ProviderEvidenceRequired,
                    )
                })
                .collect::<Vec<_>>();
            let committed_provider_prefix = provider_records
                .iter()
                .map(|record| AgentEventDraft {
                    event_id: record.event.event_id.clone(),
                    run_id: record.event.run_id.clone(),
                    causation_id: record.event.causation_id.clone(),
                    source_fingerprint: record.event.source_fingerprint.clone(),
                    payload: record.event.payload.clone(),
                })
                .collect::<Vec<_>>();
            (
                entry.request.clone(),
                entry.execution.clone(),
                prefix,
                committed_provider_prefix,
                last_confirmed_seq,
                loss_record.event.event_digest.clone(),
            )
        };

        let recovery =
            AgentRecoveryRequest::new(start_request, execution.clone(), &self.descriptor)?
                .with_committed_provider_prefix(committed_provider_prefix)?;
        let recovered = self.provider.recover(recovery).await?;
        let (mut stream, confirmation) = recovered.into_parts();
        let mut matched_digests = Vec::with_capacity(expected_provider_prefix.len());
        let mut optional_stream_replays = Vec::new();
        for (expected_event_id, expected_draft_digest, requires_stream_replay) in
            &expected_provider_prefix
        {
            if !requires_stream_replay {
                matched_digests.push(expected_draft_digest.clone());
                optional_stream_replays
                    .push((expected_event_id.clone(), expected_draft_digest.clone()));
                continue;
            }
            loop {
                let recovered_item = timeout(RECOVERY_PREFIX_ITEM_TIMEOUT, stream.next())
                    .await
                    .map_err(|_| AgentControlError::RecoveryMismatch(run_id.clone()))?;
                match recovered_item {
                    Some(Ok(AgentProviderStreamItem::Telemetry(telemetry))) => {
                        telemetry.validate_integrity()?;
                        if telemetry.run_id != *run_id {
                            return Err(AgentControlError::RecoveryMismatch(run_id.clone()));
                        }
                        slot.publish(AgentControlEvent::Telemetry(telemetry));
                    }
                    Some(Ok(AgentProviderStreamItem::Event(draft))) => {
                        let draft_digest = draft.computed_digest()?;
                        if draft.event_id == *expected_event_id
                            && draft_digest == *expected_draft_digest
                        {
                            // Any earlier optional command dispositions that
                            // were not emitted are already proven by the Host
                            // command journal. They cannot validly appear
                            // after this later native observation.
                            optional_stream_replays.clear();
                            matched_digests.push(expected_draft_digest.clone());
                            break;
                        }
                        if let Some(position) = optional_stream_replays.iter().position(
                            |(optional_event_id, optional_digest)| {
                                draft.event_id == *optional_event_id
                                    && draft_digest == *optional_digest
                            },
                        ) {
                            optional_stream_replays.drain(..=position);
                            continue;
                        }
                        return Err(AgentControlError::RecoveryMismatch(run_id.clone()));
                    }
                    Some(Ok(_)) | Some(Err(_)) | None => {
                        return Err(AgentControlError::RecoveryMismatch(run_id.clone()));
                    }
                }
            }
        }

        let mut recovered_fingerprint = format!("{run_id}:{last_confirmed_seq}");
        for digest in &matched_digests {
            recovered_fingerprint.push(':');
            recovered_fingerprint.push_str(digest.as_str());
        }
        let proof = ReconciliationProof::new(
            ReconciliationProofRef::new(format!(
                "host-recovery/{}/{}",
                run_id.as_str(),
                last_confirmed_seq
            )),
            last_confirmed_seq,
            loss_event_digest,
            Digest::sha256(recovered_fingerprint),
        )?;
        let verifier = ExactReconciliationVerifier {
            execution: execution.clone(),
            proof: proof.clone(),
        };
        {
            let mut entry = slot.entry.lock().await;
            let mut next_reducer = entry.reducer.clone();
            let restored = next_reducer.apply_verified_reconciliation(
                AgentEventDraft {
                    event_id: AgentEventId::new(format!(
                        "host-continuity-restored-{}-{}",
                        run_id.as_str(),
                        last_confirmed_seq
                    )),
                    run_id: run_id.clone(),
                    causation_id: None,
                    source_fingerprint: None,
                    payload: AgentEvent::ContinuityRestored {
                        proof,
                        reason: "Provider recovery prefix matched the Host journal".to_owned(),
                    },
                },
                &verifier,
            )?;
            self.commit_sequenced(&slot, &mut entry, next_reducer, restored)
                .await?;
        }

        if let Err(error) = confirmation.await {
            self.mark_continuity_lost(
                run_id,
                &slot,
                format!("Provider could not resume after reconciliation: {error}"),
            )
            .await?;
            return Err(error.into());
        }

        let controller = Arc::clone(self);
        let recovered_run_id = run_id.clone();
        let recovered_slot = slot.clone();
        tokio::spawn(async move {
            controller
                .drive_stream(recovered_run_id, recovered_slot, stream)
                .await;
        });
        self.inspect(run_id).await
    }

    /// Waits until the Run reaches an authoritative terminal. Unknown is
    /// returned explicitly rather than guessed as success or cancellation.
    pub async fn wait_for_terminal(
        &self,
        run_id: &RunId,
    ) -> Result<AgentRunView, AgentControlError> {
        let slot = self.run_slot(run_id).await?;
        loop {
            let changed = slot.changed.notified();
            let view = slot.entry.lock().await.reducer.view();
            if view.state.is_terminal() {
                return Ok(view);
            }
            if view.state.status() == AgentRunStatus::Unknown {
                return Err(AgentControlError::ContinuityUnknown(run_id.clone()));
            }
            changed.await;
        }
    }

    async fn run_slot(&self, run_id: &RunId) -> Result<Arc<RunSlot>, AgentControlError> {
        if let Some(slot) = self.runs.read().await.get(run_id).cloned() {
            return Ok(slot);
        }
        let _start_guard = self.start_gate.lock().await;
        if let Some(slot) = self.runs.read().await.get(run_id).cloned() {
            return Ok(slot);
        }
        let stored = self
            .journal_store
            .load_run(run_id)
            .await?
            .ok_or_else(|| AgentControlError::RunNotFound(run_id.clone()))?;
        let slot = self.slot_from_stored(stored)?;
        self.runs.write().await.insert(run_id.clone(), slot.clone());
        let view = slot.entry.lock().await.reducer.view();
        if !view.state.is_terminal() && view.state.status() != AgentRunStatus::Unknown {
            self.mark_continuity_lost(
                run_id,
                &slot,
                "Host controller restarted without a continuously attached Provider stream"
                    .to_owned(),
            )
            .await?;
        }
        Ok(slot)
    }

    fn slot_from_stored(&self, stored: StoredAgentRun) -> Result<Arc<RunSlot>, AgentControlError> {
        stored.validate_shape()?;
        let registration = &stored.registration;
        registration
            .execution
            .validate_for(&registration.request, &self.descriptor)?;
        let compatibility = self
            .descriptor
            .descriptor
            .check_run_compatibility(&registration.request.run)
            .map_err(|rejection| {
                AgentProtocolError::new(AgentProtocolErrorCode::Unsupported, rejection.to_string())
                    .with_details(rejection.details)
            })?;
        registration
            .admission
            .validate_against(&registration.request.run, &compatibility)?;
        let mut reducer = AgentRunReducer::new(
            registration.execution.clone(),
            &registration.request,
            &self.descriptor,
            registration.admission.clone(),
        )?;
        for record in &stored.records {
            reducer.replay_journal_record(record.clone())?;
        }
        let (events, _) = broadcast::channel(self.event_buffer);
        Ok(Arc::new(RunSlot {
            entry: Mutex::new(RunEntry {
                request: registration.request.clone(),
                execution: registration.execution.clone(),
                reducer,
                journal: stored.records,
            }),
            recovery_gate: Mutex::new(()),
            events,
            changed: Notify::new(),
        }))
    }

    fn registration_matches_current(
        &self,
        request: &AgentStartRequest,
        execution: &AgentExecutionRef,
    ) -> bool {
        request.provider_binding == self.binding_ref
            && request.expected_descriptor_digest == self.descriptor.descriptor_digest
            && execution.binding_ref == self.binding_ref
            && execution.descriptor_digest == self.descriptor.descriptor_digest
    }

    async fn drive_stream(
        self: Arc<Self>,
        run_id: RunId,
        slot: Arc<RunSlot>,
        mut stream: AgentProviderStream,
    ) {
        let mut end_reason = "Provider stream ended before an authoritative terminal".to_owned();
        while let Some(item) = stream.next().await {
            match item {
                Ok(AgentProviderStreamItem::Event(draft)) => {
                    let mut entry = slot.entry.lock().await;
                    let mut next_reducer = entry.reducer.clone();
                    match next_reducer.apply_provider_draft(*draft) {
                        Ok(sequenced) => {
                            if let Err(error) = self
                                .commit_sequenced(&slot, &mut entry, next_reducer, sequenced)
                                .await
                            {
                                end_reason =
                                    format!("Provider event could not be journaled: {error}");
                                break;
                            }
                            if entry.reducer.state().is_terminal()
                                || entry.reducer.state().status() == AgentRunStatus::Unknown
                            {
                                return;
                            }
                        }
                        Err(error) => {
                            end_reason = format!("Provider event was rejected: {error}");
                            break;
                        }
                    }
                }
                Ok(AgentProviderStreamItem::Telemetry(telemetry)) => {
                    if let Err(error) = telemetry.validate_integrity() {
                        end_reason = format!("Provider telemetry was invalid: {error}");
                        break;
                    }
                    if telemetry.run_id != run_id {
                        end_reason = "Provider telemetry crossed a Run boundary".to_owned();
                        break;
                    }
                    slot.publish(AgentControlEvent::Telemetry(telemetry));
                }
                Ok(_) => {
                    end_reason = "Provider emitted an unsupported stream item".to_owned();
                    break;
                }
                Err(error) => {
                    end_reason = format!("Provider stream failed: {error}");
                    break;
                }
            }
        }
        if let Err(error) = self.mark_continuity_lost(&run_id, &slot, end_reason).await {
            tracing::error!(run_id = %run_id, error = %error, "failed to journal continuity loss");
        }
    }

    async fn commit_sequenced(
        &self,
        slot: &RunSlot,
        entry: &mut RunEntry,
        next_reducer: AgentRunReducer,
        sequenced: SequencedApply,
    ) -> Result<(), AgentControlError> {
        if matches!(sequenced.outcome, ApplyOutcome::ExactDuplicate) {
            return Ok(());
        }
        let expected_previous = entry.reducer.view().last_run_seq.unwrap_or(0);
        self.journal_store
            .append_record(
                &entry.execution.run_id,
                expected_previous,
                sequenced.record.clone(),
            )
            .await?;
        entry.reducer = next_reducer;
        entry.journal.push(sequenced.record.clone());
        slot.publish(AgentControlEvent::Durable(Arc::new(sequenced.record)));
        Ok(())
    }

    async fn mark_continuity_lost(
        &self,
        run_id: &RunId,
        slot: &Arc<RunSlot>,
        reason: String,
    ) -> Result<(), AgentControlError> {
        let mut entry = slot.entry.lock().await;
        let view = entry.reducer.view();
        if view.state.is_terminal() || view.state.status() == AgentRunStatus::Unknown {
            return Ok(());
        }
        let last_confirmed_seq = view.last_run_seq.unwrap_or(0);
        let draft = AgentEventDraft {
            event_id: AgentEventId::new(format!(
                "host-continuity-lost-{}-{}",
                run_id.as_str(),
                last_confirmed_seq + 1
            )),
            run_id: run_id.clone(),
            causation_id: None,
            source_fingerprint: None,
            payload: AgentEvent::ContinuityLost {
                last_confirmed_seq,
                reason,
            },
        };
        let mut next_reducer = entry.reducer.clone();
        let sequenced = next_reducer.apply_host_draft(draft)?;
        self.commit_sequenced(slot, &mut entry, next_reducer, sequenced)
            .await?;
        Ok(())
    }
}

fn push_sequenced(journal: &mut Vec<AgentJournalRecord>, sequenced: SequencedApply) {
    if !matches!(sequenced.outcome, ApplyOutcome::ExactDuplicate) {
        journal.push(sequenced.record);
    }
}

struct ExactReconciliationVerifier {
    execution: AgentExecutionRef,
    proof: ReconciliationProof,
}

impl ReconciliationProofVerifier for ExactReconciliationVerifier {
    fn verify(
        &self,
        execution: &AgentExecutionRef,
        continuity: &AgentContinuityState,
        proof: &ReconciliationProof,
    ) -> Result<(), AgentProtocolError> {
        if execution != &self.execution || proof != &self.proof {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::InvalidDigest,
                "reconciliation proof is not the controller-verified evidence",
            ));
        }
        let AgentContinuityState::Unknown {
            last_confirmed_seq,
            loss_event_digest,
            ..
        } = continuity
        else {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::InvalidTransition,
                "reconciliation requires Unknown continuity",
            ));
        };
        if *last_confirmed_seq != proof.last_confirmed_seq
            || *loss_event_digest != proof.loss_event_digest
        {
            return Err(AgentProtocolError::new(
                AgentProtocolErrorCode::InvalidDigest,
                "reconciliation proof is not bound to the current continuity loss",
            ));
        }
        proof.verify_integrity()
    }
}