merry-runtime 0.1.4

Structured session, task ledger, context compaction, memory, permission, and tool runtime for Merry.
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
use super::commands::InteractiveCommand;
use super::{
    InteractiveError, InteractiveRunId, InteractiveSettingsUpdate, InterruptReason, QueuedInputId,
    types::{InputRecord, InputRecords},
};
use crate::bridge::BridgeToolResultCommand;
use crate::hooks::bridge::HookDecisionCommand;
use crate::{
    CitationCompactionPolicy, CompactionOutcome, FileSessionStore, HookDecision, HookInvocation,
    HookInvocationId, UserMessageInput,
};
use merry_core::{PendingToolCallBatch, PlanId, RuntimeEvent, ToolCallBatchId, ToolCallId};
use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};
use tokio::{
    sync::{mpsc, oneshot, watch},
    task::JoinHandle,
};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;

/// Runtime message contract shared by interactive and non-interactive runs.
pub use crate::agent_loop::AgentRunMessage as InteractiveRunMessage;

/// Single-consumer output stream for one interactive run.
///
/// This stream intentionally does not implement `futures::Stream`: a host
/// handoff is a protocol phase, not an ordinary event. Callers use
/// [`Self::next_message`] and must submit the complete batch before reading the
/// next message. This contract is shared by Rust and future foreign-language
/// bindings.
pub struct InteractiveRunEventStream {
    inner: Option<ReceiverStream<InteractiveRunMessage>>,
    inner_closed: bool,
    rate_updates: watch::Receiver<Option<RuntimeEvent>>,
    rate_updates_closed: bool,
    cancellation_token: CancellationToken,
    producer_handle: Option<JoinHandle<Result<(), InteractiveError>>>,
    bridge_sender: mpsc::Sender<BridgeToolResultCommand>,
    run_id: InteractiveRunId,
    pending_tool_invocations: Option<PendingToolCallBatch>,
    unread_message: Option<InteractiveRunMessage>,
    bridge_resolution_epoch: Arc<AtomicU64>,
    observed_bridge_resolution_epoch: u64,
    hook_sender: mpsc::Sender<HookDecisionCommand>,
    pending_hook_invocation: Option<HookInvocation>,
    hook_resolution_epoch: Arc<AtomicU64>,
    observed_hook_resolution_epoch: u64,
    closed_event_observed: bool,
}

impl InteractiveRunEventStream {
    #[allow(clippy::too_many_arguments)]
    pub(super) fn new(
        run_id: InteractiveRunId,
        inner: ReceiverStream<InteractiveRunMessage>,
        rate_updates: watch::Receiver<Option<RuntimeEvent>>,
        cancellation_token: CancellationToken,
        producer_handle: JoinHandle<Result<(), InteractiveError>>,
        bridge_sender: mpsc::Sender<BridgeToolResultCommand>,
        bridge_resolution_epoch: Arc<AtomicU64>,
        hook_sender: mpsc::Sender<HookDecisionCommand>,
        hook_resolution_epoch: Arc<AtomicU64>,
    ) -> Self {
        let observed_bridge_resolution_epoch = bridge_resolution_epoch.load(Ordering::Acquire);
        let observed_hook_resolution_epoch = hook_resolution_epoch.load(Ordering::Acquire);
        Self {
            inner: Some(inner),
            inner_closed: false,
            rate_updates,
            rate_updates_closed: false,
            cancellation_token,
            producer_handle: Some(producer_handle),
            bridge_sender,
            run_id,
            pending_tool_invocations: None,
            unread_message: None,
            bridge_resolution_epoch,
            observed_bridge_resolution_epoch,
            hook_sender,
            pending_hook_invocation: None,
            hook_resolution_epoch,
            observed_hook_resolution_epoch,
            closed_event_observed: false,
        }
    }

    fn synchronize_bridge_resolution(&mut self) {
        let epoch = self.bridge_resolution_epoch.load(Ordering::Acquire);
        if epoch != self.observed_bridge_resolution_epoch {
            self.pending_tool_invocations = None;
            self.unread_message = None;
            self.observed_bridge_resolution_epoch = epoch;
        }
        let hook_epoch = self.hook_resolution_epoch.load(Ordering::Acquire);
        if hook_epoch != self.observed_hook_resolution_epoch {
            self.pending_hook_invocation = None;
            self.unread_message = None;
            self.observed_hook_resolution_epoch = hook_epoch;
        }
    }

    /// Returns the next ordered event, host tool handoff, or subscribed hook invocation.
    pub async fn next_message(
        &mut self,
    ) -> Result<Option<InteractiveRunMessage>, InteractiveError> {
        self.synchronize_bridge_resolution();
        if self.pending_tool_invocations.is_some() {
            return Err(InteractiveError::ToolInvocationsPending {
                run_id: self.run_id,
            });
        }
        if self.pending_hook_invocation.is_some() {
            return Err(InteractiveError::HookInvocationPending {
                run_id: self.run_id,
            });
        }

        let message = self.receive_message().await;
        let Some(message) = message else {
            self.finish_after_eof().await?;
            return Ok(None);
        };

        self.observe_terminal_message(&message);
        self.mark_pending_tool_invocations(&message)?;
        Ok(Some(message))
    }

    async fn receive_message(&mut self) -> Option<InteractiveRunMessage> {
        if let Some(message) = self.unread_message.take() {
            return Some(message);
        }
        use futures_util::StreamExt;
        loop {
            if self.inner_closed && self.rate_updates_closed {
                return None;
            }
            if self.inner_closed {
                match self.rate_updates.changed().await {
                    Ok(()) => {
                        if let Some(event) = self.rate_updates.borrow_and_update().clone() {
                            return Some(InteractiveRunMessage::Event(event));
                        }
                    }
                    Err(_) => self.rate_updates_closed = true,
                }
                continue;
            }
            let Some(inner) = self.inner.as_mut() else {
                self.inner_closed = true;
                continue;
            };
            if self.rate_updates_closed {
                let message = inner.next().await;
                if message.is_none() {
                    self.inner_closed = true;
                }
                return message;
            }
            tokio::select! {
                biased;
                message = inner.next() => {
                    if message.is_none() {
                        self.inner_closed = true;
                    }
                    return message;
                }
                changed = self.rate_updates.changed() => {
                    match changed {
                        Ok(()) => {
                            if let Some(event) = self.rate_updates.borrow_and_update().clone() {
                                return Some(InteractiveRunMessage::Event(event));
                            }
                        }
                        Err(_) => self.rate_updates_closed = true,
                    }
                }
            }
        }
    }

    fn observe_terminal_message(&mut self, message: &InteractiveRunMessage) {
        if matches!(message, InteractiveRunMessage::Event(RuntimeEvent::Closed)) {
            self.closed_event_observed = true;
        }
    }

    async fn finish_producer(&mut self) -> Result<(), InteractiveError> {
        let Some(handle) = self.producer_handle.take() else {
            return Ok(());
        };
        match handle.await {
            Ok(result) => result,
            Err(error) => {
                let reason = error.to_string();
                tracing::error!(
                    run_id = self.run_id.as_u64(),
                    reason = %reason,
                    "interactive run producer task failed"
                );
                Err(InteractiveError::ProducerTaskFailed {
                    run_id: self.run_id,
                    reason,
                })
            }
        }
    }

    async fn finish_after_eof(&mut self) -> Result<(), InteractiveError> {
        self.finish_producer().await?;
        if self.closed_event_observed {
            Ok(())
        } else {
            Err(InteractiveError::ProducerStopped {
                run_id: self.run_id,
                message: "output channel closed unexpectedly",
            })
        }
    }

    fn mark_pending_tool_invocations(
        &mut self,
        message: &InteractiveRunMessage,
    ) -> Result<(), InteractiveError> {
        match message {
            InteractiveRunMessage::ToolInvocations { batch } => {
                if batch.calls().is_empty() {
                    return Err(InteractiveError::InvalidToolInvocationBatch {
                        run_id: self.run_id,
                    });
                }
                self.pending_tool_invocations = Some(batch.clone());
            }
            InteractiveRunMessage::HookInvocation { invocation } => {
                self.pending_hook_invocation = Some(invocation.clone());
            }
            InteractiveRunMessage::Event(_) => {}
        }
        Ok(())
    }

    /// Returns the next runtime event when no host handoff is encountered.
    ///
    /// Hosts that register bridge tools or hooks must use [`Self::next_message`].
    pub async fn next_event(&mut self) -> Result<Option<RuntimeEvent>, InteractiveError> {
        self.synchronize_bridge_resolution();
        if self.pending_tool_invocations.is_some() {
            return Err(InteractiveError::ToolInvocationsPending {
                run_id: self.run_id,
            });
        }
        if self.pending_hook_invocation.is_some() {
            return Err(InteractiveError::HookInvocationPending {
                run_id: self.run_id,
            });
        }
        let Some(message) = self.receive_message().await else {
            self.finish_after_eof().await?;
            return Ok(None);
        };
        match message {
            InteractiveRunMessage::Event(event) => {
                if matches!(event, RuntimeEvent::Closed) {
                    self.closed_event_observed = true;
                }
                Ok(Some(event))
            }
            InteractiveRunMessage::ToolInvocations { batch } => {
                let count = batch.calls().len();
                self.unread_message = Some(InteractiveRunMessage::ToolInvocations { batch });
                Err(InteractiveError::ToolInvocationsRequireMessageProtocol {
                    run_id: self.run_id,
                    count,
                })
            }
            InteractiveRunMessage::HookInvocation { invocation } => {
                self.unread_message = Some(InteractiveRunMessage::HookInvocation { invocation });
                Err(InteractiveError::HookInvocationRequiresMessageProtocol {
                    run_id: self.run_id,
                })
            }
        }
    }

    /// Submits a typed decision for the active host-bridged hook invocation.
    pub async fn submit_hook_decision(
        &mut self,
        invocation_id: &HookInvocationId,
        decision: HookDecision,
    ) -> Result<(), InteractiveError> {
        self.submit_hook_outcome(invocation_id, Ok(decision)).await
    }

    /// Reports a fail-closed host hook error for the active hook invocation.
    pub async fn fail_hook_invocation(
        &mut self,
        invocation_id: &HookInvocationId,
        message: String,
    ) -> Result<(), InteractiveError> {
        self.submit_hook_outcome(invocation_id, Err(message)).await
    }

    async fn submit_hook_outcome(
        &mut self,
        invocation_id: &HookInvocationId,
        outcome: Result<HookDecision, String>,
    ) -> Result<(), InteractiveError> {
        self.synchronize_bridge_resolution();
        if self.pending_hook_invocation.is_none() {
            return Err(InteractiveError::NoPendingHookInvocation {
                run_id: self.run_id,
            });
        }
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.hook_sender
            .send(HookDecisionCommand {
                invocation_id: invocation_id.clone(),
                outcome,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::RunClosed {
                run_id: self.run_id,
            })?;
        let result = ack_receiver
            .await
            .map_err(|_| InteractiveError::RunClosed {
                run_id: self.run_id,
            })?;
        match result {
            Ok(()) => {
                self.pending_hook_invocation = None;
                Ok(())
            }
            Err(source) => Err(InteractiveError::Runtime { source }),
        }
    }

    /// Submits all outcomes for the currently emitted host tool batch.
    ///
    /// The runtime validates call ids, content, and result status. Validation
    /// failures that are safe to correct are returned while the batch remains
    /// pending. If runtime has already recorded the calls as failed, the
    /// rejection is returned but the batch is released and the producer can
    /// continue.
    pub async fn submit_tool_invocation_outcomes(
        &mut self,
        batch_id: &ToolCallBatchId,
        outcomes: Vec<(ToolCallId, crate::ToolExecutionOutcome)>,
    ) -> Result<(), InteractiveError> {
        if self.pending_tool_invocations.is_none() {
            return Err(InteractiveError::NoPendingToolInvocations {
                run_id: self.run_id,
            });
        }
        if outcomes.is_empty() {
            return Err(InteractiveError::InvalidToolInvocationBatch {
                run_id: self.run_id,
            });
        }
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.bridge_sender
            .send(BridgeToolResultCommand::outcomes(
                batch_id.clone(),
                outcomes,
                ack_sender,
            ))
            .await
            .map_err(|_| InteractiveError::RunClosed {
                run_id: self.run_id,
            })?;
        let result = ack_receiver
            .await
            .map_err(|_| InteractiveError::RunClosed {
                run_id: self.run_id,
            })?;
        if result.is_ok() {
            self.pending_tool_invocations = None;
        } else if let Err(error) = result {
            if !error.is_retryable_bridge_tool_result() {
                self.pending_tool_invocations = None;
            }
            return Err(InteractiveError::Runtime { source: error });
        }
        Ok(())
    }

    /// Requests cancellation of the producer without awaiting its shutdown.
    ///
    /// This is used by synchronous drop guards at the facade boundary. Callers
    /// that own the async lifecycle should use `wait_until_closed` afterwards.
    pub fn request_cancel(&self) {
        self.cancellation_token.cancel();
    }

    /// Waits for the interactive producer to emit its terminal closed event.
    ///
    /// Ordinary runtime events are drained. If a host-tool handoff is reached,
    /// this returns [`InteractiveError::ToolInvocationsRequireMessageProtocol`]
    /// without consuming the handoff, so the caller can switch to
    /// [`Self::next_message`] and resolve it. This method is therefore a
    /// lifecycle drain, not an implicit host-tool executor.
    pub async fn wait_until_closed(&mut self) -> Result<(), InteractiveError> {
        while let Some(event) = self.next_event().await? {
            if matches!(event, RuntimeEvent::Closed) {
                break;
            }
        }
        self.inner.take();
        self.finish_producer().await
    }

    /// Cancels the producer, drains all buffered events and handoffs, and waits
    /// for durable shutdown (including settling any pending bridge tool calls).
    pub async fn cancel_and_wait_until_closed(&mut self) -> Result<(), InteractiveError> {
        self.cancellation_token.cancel();
        self.pending_tool_invocations = None;
        self.pending_hook_invocation = None;
        self.unread_message = None;
        while let Some(message) = self.receive_message().await {
            self.observe_terminal_message(&message);
            if self.closed_event_observed {
                break;
            }
        }
        self.inner.take();
        self.finish_producer().await
    }
}

impl Drop for InteractiveRunEventStream {
    fn drop(&mut self) {
        self.inner.take();
        self.cancellation_token.cancel();

        if let Some(handle) = self.producer_handle.take() {
            handle.abort();
        }
    }
}

pub struct InteractiveAgentRun {
    stream: InteractiveRunEventStream,
    input: AgentLoopInput,
    control: AgentLoopControl,
}

impl InteractiveAgentRun {
    pub(super) fn new(
        stream: InteractiveRunEventStream,
        input: AgentLoopInput,
        control: AgentLoopControl,
    ) -> Self {
        Self {
            stream,
            input,
            control,
        }
    }

    #[must_use]
    pub fn split(self) -> (InteractiveRunEventStream, AgentLoopInput, AgentLoopControl) {
        (self.stream, self.input, self.control)
    }
}

#[derive(Clone)]
pub struct AgentLoopInput {
    run_id: InteractiveRunId,
    command_sender: mpsc::Sender<InteractiveCommand>,
}

impl AgentLoopInput {
    pub(super) fn new(
        run_id: InteractiveRunId,
        command_sender: mpsc::Sender<InteractiveCommand>,
    ) -> Self {
        Self {
            run_id,
            command_sender,
        }
    }

    #[must_use]
    pub fn run_id(&self) -> InteractiveRunId {
        self.run_id
    }

    /// Appends text to the pending queue (runs immediately when idle and not paused).
    pub async fn submit(&self, text: &str) -> Result<InteractiveInputItem, InteractiveError> {
        let message = UserMessageInput::text_only(text)?;
        self.submit_message(message).await
    }

    /// Appends a user message to the pending queue (runs immediately when idle and not paused).
    pub async fn submit_message(
        &self,
        message: UserMessageInput,
    ) -> Result<InteractiveInputItem, InteractiveError> {
        let text = message.text().to_owned();
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Submit {
                message,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
            .map(|id| InteractiveInputItem::new(self.clone(), id, text))
    }

    /// Updates a pending item's text by its stable [`QueuedInputId`].
    pub async fn update_by_id(
        &self,
        id: QueuedInputId,
        text: &str,
    ) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Update {
                id,
                text: text.to_owned(),
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Removes a pending item by its stable [`QueuedInputId`].
    pub async fn remove_by_id(&self, id: QueuedInputId) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Remove { id, ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Pauses or resumes automatic dispatch of pending items.
    ///
    /// While paused (for example, when any pending item is being edited in a UI),
    /// pending items will not auto-send at turn boundaries. Passing `false` resumes
    /// auto-dispatch and immediately sends all pending items together if the loop is idle.
    pub async fn set_paused(&self, paused: bool) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::SetPendingPaused { paused, ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Resumes and immediately dispatches all pending items together if the loop is idle.
    pub async fn flush(&self) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::FlushPending { ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    pub async fn replace_pending_order(
        &self,
        items: &[InteractiveInputItem],
    ) -> Result<(), InteractiveError> {
        if items.iter().any(|item| item.input.run_id != self.run_id) {
            return Err(InteractiveError::InvalidPendingOrder {
                reason: "input item belongs to another interactive run",
            });
        }
        let ids = items.iter().map(|item| item.id).collect::<Vec<_>>();
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::ReplacePendingOrder { ids, ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    pub async fn snapshot(&self) -> Result<InteractiveInputSnapshot, InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Snapshot { ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        let records = ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        Ok(InteractiveInputSnapshot::from_records(
            self.clone(),
            records,
        ))
    }
}

#[derive(Clone)]
pub struct InteractiveInputItem {
    input: AgentLoopInput,
    id: QueuedInputId,
    text: String,
}

impl InteractiveInputItem {
    fn new(input: AgentLoopInput, id: QueuedInputId, text: String) -> Self {
        Self { input, id, text }
    }

    #[must_use]
    pub fn id(&self) -> QueuedInputId {
        self.id
    }

    #[must_use]
    pub fn text(&self) -> &str {
        self.text.as_str()
    }

    pub async fn update(&mut self, text: impl Into<String>) -> Result<(), InteractiveError> {
        let text = text.into();
        self.input.update_by_id(self.id, &text).await?;
        self.text = text;
        Ok(())
    }

    pub async fn remove(self) -> Result<(), InteractiveError> {
        self.input.remove_by_id(self.id).await
    }
}

#[derive(Clone)]
pub struct InteractiveInputSnapshot {
    pub items: Vec<InteractiveInputItem>,
    pub paused: bool,
}

impl InteractiveInputSnapshot {
    fn from_records(input: AgentLoopInput, records: InputRecords) -> Self {
        Self {
            items: records_to_items(input, records.items),
            paused: records.paused,
        }
    }
}

fn records_to_items(input: AgentLoopInput, records: Vec<InputRecord>) -> Vec<InteractiveInputItem> {
    records
        .into_iter()
        .map(|record| InteractiveInputItem::new(input.clone(), record.id, record.text))
        .collect()
}

#[derive(Clone)]
pub struct AgentLoopControl {
    run_id: InteractiveRunId,
    command_sender: mpsc::Sender<InteractiveCommand>,
}

impl AgentLoopControl {
    pub(super) fn new(
        run_id: InteractiveRunId,
        command_sender: mpsc::Sender<InteractiveCommand>,
    ) -> Self {
        Self {
            run_id,
            command_sender,
        }
    }

    #[must_use]
    pub fn run_id(&self) -> InteractiveRunId {
        self.run_id
    }

    pub async fn update_settings(
        &self,
        update: InteractiveSettingsUpdate,
    ) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::UpdateSettings {
                update: Box::new(update),
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    pub async fn interrupt(&self, reason: InterruptReason) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Interrupt { reason, ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Saves the session only while the interactive producer is at an idle boundary.
    ///
    /// Returns [`InteractiveError::SessionSaveRequiresIdle`] immediately when a model, tool, or
    /// interrupt phase is active.
    pub async fn save_session_to(&self, store: FileSessionStore) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::SaveSession { store, ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Manually compacts session history at an idle boundary, optionally
    /// emphasizing `focus` instructions in the generated summary.
    ///
    /// Returns `Ok(None)` when no compressible history exists before the
    /// retained tail, and [`InteractiveError::CompactRequiresIdle`] immediately
    /// when a model, tool, compaction, or interrupt phase is already active.
    pub async fn compact_context(
        &self,
        focus: Option<String>,
    ) -> Result<Option<CompactionOutcome>, InteractiveError> {
        self.compact_context_with_policy(focus, None).await
    }

    /// Manually compacts session history at an idle boundary with optional
    /// `focus` instructions and an optional per-call [`CitationCompactionPolicy`]
    /// override.
    ///
    /// When `policy` is `None`, the runtime's configured compaction policy is used.
    pub async fn compact_context_with_policy(
        &self,
        focus: Option<String>,
        policy: Option<CitationCompactionPolicy>,
    ) -> Result<Option<CompactionOutcome>, InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::CompactContext {
                focus,
                policy,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Toggles Plan Mode on or off (works both while idle and mid-turn).
    pub async fn set_plan_mode(&self, enabled: bool) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::SetPlanMode {
                enabled,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Approves a submitted plan, exits Plan Mode, and continues execution.
    pub async fn approve_plan(
        &self,
        plan_id: PlanId,
        note: Option<String>,
    ) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::ApprovePlan {
                plan_id,
                note,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Keeps Plan Mode active and delivers revision feedback to the agent.
    pub async fn request_plan_changes(
        &self,
        plan_id: PlanId,
        feedback: String,
    ) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::RequestPlanChanges {
                plan_id,
                feedback,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    /// Abandons a submitted plan and exits Plan Mode without implementing.
    pub async fn abandon_plan(&self, plan_id: PlanId) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::AbandonPlan {
                plan_id,
                ack_sender,
            })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }

    pub async fn close(&self) -> Result<(), InteractiveError> {
        let (ack_sender, ack_receiver) = oneshot::channel();
        self.command_sender
            .send(InteractiveCommand::Close { ack_sender })
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?;
        ack_receiver
            .await
            .map_err(|_| InteractiveError::CommandChannelClosed {
                run_id: self.run_id,
            })?
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn producer_panic_preserves_join_error_reason() {
        let (message_tx, message_rx) = mpsc::channel(1);
        let (rate_tx, rate_rx) = watch::channel(None);
        let (bridge_tx, _bridge_rx) = mpsc::channel(1);
        let (hook_tx, _hook_rx) = mpsc::channel(1);
        let producer = tokio::spawn(async move {
            let _tx = message_tx;
            let _rate = rate_tx;
            panic!("injected thread spawn failure: os error 11");
        });

        let mut stream = InteractiveRunEventStream::new(
            InteractiveRunId(42),
            ReceiverStream::new(message_rx),
            rate_rx,
            CancellationToken::new(),
            producer,
            bridge_tx,
            Arc::new(AtomicU64::new(0)),
            hook_tx,
            Arc::new(AtomicU64::new(0)),
        );

        let err = stream
            .next_message()
            .await
            .expect_err("panicked producer should surface ProducerTaskFailed");
        match &err {
            InteractiveError::ProducerTaskFailed { run_id, reason } => {
                assert_eq!(*run_id, InteractiveRunId(42));
                assert!(
                    reason.contains("injected thread spawn failure: os error 11"),
                    "unexpected reason: {reason}"
                );
            }
            other => panic!("expected ProducerTaskFailed, got {other:?}"),
        }
        assert!(
            err.to_string()
                .contains("injected thread spawn failure: os error 11")
        );
    }
}