infinity-daemon 0.2.0

The Infinity daemon: hosts durable agent sessions and serves local clients.
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
//! Daemon-level tests of the session wiring on top of the core agent system:
//! per-round model resolution via [`CatalogModelSource`], model inheritance
//! for spawned threads, and the [`DaemonObserver`]'s state persistence.
//!
//! The generic driver behaviors (batching, interruption, deferral, idling,
//! compaction) are tested in `infinity_agent_core::system::tests`.

use infinity_agent_core::ThreadId;
use std::sync::Arc;

use infinity_agent_core::message::{InputMessage, InputMessageContent, UserChoiceRequired};
use infinity_agent_core::system::local::{ChannelSender, RunningSystem, ThreadLifecycleState};
use infinity_agent_core::system::{AgentSystemBuilder, UserChoice};
use infinity_agent_core::tools::Tool;
use infinity_agent_core::traits::{ConversationStore, SpawnContext, StateStore};
use infinity_protocol::{DaemonMessage, SessionStatus};
use infinity_provider_protocol::message::UserContent;
use infinity_provider_protocol::mock::mock_model;
use infinity_provider_protocol::{ModelEntry, ModelProvider, SingleModelProvider};
use tokio::sync::mpsc;

use super::observer::{DaemonObserver, SubscribeRequest, Subscriber, SubscriberMap};
use super::{SessionManager, SessionManagerConfig};
use crate::ids::SequentialIdSource;
use crate::memory_store::{PersistentConversationStore, PersistentStateStore};
use crate::models::{CatalogModelSource, ModelCatalog};

fn model1_ref() -> infinity_protocol::ModelRef {
    infinity_protocol::ModelRef {
        provider_id: "provider1".to_owned(),
        model_id: "model1".to_owned(),
    }
}

fn model2_ref() -> infinity_protocol::ModelRef {
    infinity_protocol::ModelRef {
        provider_id: "provider2".to_owned(),
        model_id: "model2".to_owned(),
    }
}

async fn two_model_catalog(
    model1: infinity_provider_protocol::mock::MockCompletionModel,
    model2: infinity_provider_protocol::mock::MockCompletionModel,
) -> Arc<ModelCatalog> {
    let entry = |id: &str| ModelEntry {
        model_id: id.to_owned(),
        display_name: id.to_owned(),
        context_window: 0,
        max_output_tokens: None,
        supports_image_input: false,
    };
    Arc::new(
        ModelCatalog::new(vec![
            (
                "provider1".to_owned(),
                Arc::new(SingleModelProvider::new(entry("model1"), model1)) as _,
            ),
            (
                "provider2".to_owned(),
                Arc::new(SingleModelProvider::new(entry("model2"), model2)) as _,
            ),
        ])
        .await
        .expect("build two-model catalog"),
    )
}

fn tmp_stores(
    default_model: infinity_protocol::ModelRef,
) -> (
    PersistentConversationStore,
    infinity_agent_core::stores::InMemoryStateStore,
    tempfile::TempDir,
) {
    let dir = tempfile::tempdir().expect("create temp dir");
    let conv = PersistentConversationStore::new_with_dir(
        dir.path().join("threads"),
        default_model,
        Arc::new(crate::ids::UuidIdSource),
    );
    let state = infinity_agent_core::stores::InMemoryStateStore::new();
    (conv, state, dir)
}

/// Start an agent system wired exactly like a daemon session: a
/// [`CatalogModelSource`] over the catalog + conversation store, and a
/// [`DaemonObserver`] per thread. Returns the running handle and a subscriber
/// channel receiving the root thread's `DaemonMessage`s.
fn start_daemon_system(
    root: &ThreadId<str>,
    conv: PersistentConversationStore,
    state: impl StateStore + 'static,
    catalog: Arc<ModelCatalog>,
    tools: Vec<Box<dyn Tool<ChannelSender>>>,
) -> (
    RunningSystem<SubscribeRequest>,
    mpsc::UnboundedReceiver<DaemonMessage>,
    SubscriberMap,
) {
    let system = AgentSystemBuilder::new_local(
        conv.clone(),
        state,
        CatalogModelSource {
            catalog,
            conversation_store: conv.clone(),
        },
    )
    .tools(tools)
    .build_local();

    let (client_tx, client_rx) = mpsc::unbounded_channel();
    let subscriber_map: SubscriberMap = Default::default();
    subscriber_map.lock().expect("bug: mutex poisoned").insert(
        root.to_owned(),
        Arc::new(std::sync::Mutex::new(vec![Subscriber {
            tx: client_tx,
            keeps_session_alive: true,
        }])),
    );

    let make_observer = {
        let subscriber_map = subscriber_map.clone();
        move |thread_id: &ThreadId<str>| {
            let parent_subs = {
                let parent_id = conv.get_thread_parent_id(thread_id);
                let smap = subscriber_map.lock().expect("bug: mutex poisoned");
                let source = parent_id.as_deref().unwrap_or(thread_id);
                smap.get(source)
                    .map(|arc| arc.lock().expect("bug: mutex poisoned").clone())
                    .unwrap_or_default()
            };
            let subscribers = subscriber_map
                .lock()
                .expect("bug: mutex poisoned")
                .entry(thread_id.to_owned())
                .or_insert_with(|| Arc::new(std::sync::Mutex::new(parent_subs)))
                .clone();
            DaemonObserver {
                subscribers,
                conversation_store: conv.clone(),
            }
        }
    };
    let running = system.start_with_observer(make_observer);
    (running, client_rx, subscriber_map)
}

async fn collect_until_done(rx: &mut mpsc::UnboundedReceiver<DaemonMessage>) -> Vec<String> {
    let mut texts = Vec::new();
    loop {
        match tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()).await {
            Ok(Some(DaemonMessage::TextChunk { chunk, .. })) => texts.push(chunk),
            Ok(Some(DaemonMessage::ResponseDone { .. })) => break,
            Ok(Some(_)) => {}
            Ok(None) => break,
            Err(_) => panic!("timed out waiting for ResponseDone"),
        }
    }
    texts
}

fn tool_result_input(group_id: &ThreadId<str>, id: &str, text: &str) -> InputMessage {
    InputMessage {
        content: InputMessageContent::User(UserContent::ToolResult(
            infinity_provider_protocol::message::ToolResult {
                id: id.into(),
                call_id: None,
                content: vec![
                    infinity_provider_protocol::message::ToolResultContent::Text(
                        infinity_provider_protocol::message::Text { text: text.into() },
                    ),
                ],
            },
        )),
        group_id: group_id.to_owned(),
        metadata: None,
        synthetic: None,
        display_as: None,
        subscription: false,
    }
}

/// An async tool whose result is delivered later via the input queue —
/// keeps the driver alive between completion rounds.
struct AsyncStubTool;

#[async_trait::async_trait]
impl Tool<ChannelSender> for AsyncStubTool {
    fn name(&self) -> &str {
        "async_tool"
    }
    fn description(&self) -> &str {
        "a"
    }
    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type":"object","properties":{}})
    }
    async fn execute(
        &self,
        _: serde_json::Value,
        _: rap_protocol::ToolCallId,
        _: Option<rap_protocol::ProviderCallId>,
        _: &infinity_agent_core::tools::ToolContext<ChannelSender>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        Ok(())
    }
}

/// A model switch persisted while the thread waits for an async tool result
/// is applied to the next completion round.
#[tokio::test(flavor = "current_thread")]
async fn model_switch_applies_to_next_completion() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (conv, state, _dir) = tmp_stores(model1_ref());
            let (model1, mut ctrl1) = mock_model();
            let (model2, mut ctrl2) = mock_model();
            let catalog = two_model_catalog(model1, model2).await;
            conv.ensure_root_thread(ThreadId::from_ref("t1"))
                .await
                .expect("ensure root");

            let (running, mut display_rx, _smap) = start_daemon_system(
                ThreadId::from_ref("t1"),
                conv.clone(),
                state,
                catalog,
                vec![Box::new(AsyncStubTool)],
            );

            // First round runs on model1 and leaves an async tool call
            // pending (so the driver stays alive).
            running
                .send_user_text(ThreadId::from_ref("t1"), "use the tool")
                .await;
            let _req = ctrl1.next_request().await;
            ctrl1.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
            ctrl1.finish();
            collect_until_done(&mut display_rx).await;

            // Switch while the driver waits for the tool result. (The session
            // manager persists the selection; each round resolves it fresh.)
            conv.set_thread_model(ThreadId::from_ref("t1"), model2_ref());

            // The tool result triggers the next round — on model2.
            running
                .send(
                    tool_result_input(ThreadId::from_ref("t1"), "tc-1", "tool done"),
                    "res-1",
                )
                .await;
            let _req2 = ctrl2.next_request().await;
            ctrl2.send_text("hello from model2");
            ctrl2.finish();
            collect_until_done(&mut display_rx).await;
            assert!(
                ctrl1.try_next_request().is_none(),
                "model1 should not receive requests after the switch"
            );
        })
        .await;
}

/// A switch persisted while a completion is in flight does not disturb that
/// completion — it finishes on the old model — but the next round uses the
/// new one.
#[tokio::test(flavor = "current_thread")]
async fn model_switch_during_completion_applies_to_next_round() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (conv, state, _dir) = tmp_stores(model1_ref());
            let (model1, mut ctrl1) = mock_model();
            let (model2, mut ctrl2) = mock_model();
            let catalog = two_model_catalog(model1, model2).await;
            conv.ensure_root_thread(ThreadId::from_ref("t1"))
                .await
                .expect("ensure root");

            let (running, mut display_rx, _smap) = start_daemon_system(
                ThreadId::from_ref("t1"),
                conv.clone(),
                state,
                catalog,
                vec![Box::new(AsyncStubTool)],
            );

            // Start a completion on model1 and leave it in flight.
            running
                .send_user_text(ThreadId::from_ref("t1"), "start")
                .await;
            let _req = ctrl1.next_request().await;
            ctrl1.send_text("streaming on model1...");
            loop {
                match tokio::time::timeout(std::time::Duration::from_secs(2), display_rx.recv())
                    .await
                {
                    Ok(Some(DaemonMessage::TextChunk { .. })) => break,
                    Ok(Some(_)) => {}
                    _ => panic!("timed out waiting for text chunk"),
                }
            }

            // Switch mid-completion: the in-flight round keeps streaming on
            // model1 (it resolved its model at round start).
            conv.set_thread_model(ThreadId::from_ref("t1"), model2_ref());

            // The in-flight completion finishes undisturbed on model1
            // (ending with an async tool call so the driver stays alive).
            ctrl1.send_tool_call("tc-1", "async_tool", serde_json::json!({}));
            ctrl1.finish();
            collect_until_done(&mut display_rx).await;

            // The next round (tool result) goes to model2.
            running
                .send(
                    tool_result_input(ThreadId::from_ref("t1"), "tc-1", "tool done"),
                    "res-1",
                )
                .await;
            let _req2 = ctrl2.next_request().await;
            ctrl2.send_text("hello from model2");
            ctrl2.finish();
            collect_until_done(&mut display_rx).await;
            assert!(
                ctrl1.try_next_request().is_none(),
                "model1 should not receive requests after the switch"
            );
        })
        .await;
}

/// Regression test for issue #32: a spawned thread inherits the parent's
/// model, not the global default.
#[tokio::test(flavor = "current_thread")]
async fn spawned_thread_inherits_parent_model() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (conv, state, _dir) = tmp_stores(model1_ref());
            let (model1, mut ctrl1) = mock_model();
            let (model2, mut ctrl2) = mock_model();
            let catalog = two_model_catalog(model1, model2).await;

            conv.ensure_root_thread(ThreadId::from_ref("root"))
                .await
                .expect("ensure root");
            // Set root thread to use the non-default model (provider2/model2).
            conv.set_thread_model(ThreadId::from_ref("root"), model2_ref());

            // The built-in spawn_thread tool comes from the system builder.
            let (running, mut display_rx, _smap) = start_daemon_system(
                ThreadId::from_ref("root"),
                conv.clone(),
                state,
                catalog,
                vec![],
            );

            // Send user input to root thread (which uses model2).
            running
                .send_user_text(ThreadId::from_ref("root"), "spawn a child")
                .await;

            // Root thread uses model2, so ctrl2 gets the request.
            let _req = ctrl2.next_request().await;
            ctrl2.send_tool_call(
                "tc-spawn",
                "spawn_thread",
                serde_json::json!({
                    "instructions": "do something",
                    "child_of": ["root"]
                }),
            );
            ctrl2.finish();

            // After spawn_thread, the parent loops back on ctrl2.
            let parent_followup = ctrl2.next_request().await;
            let is_parent = parent_followup.chat_history.iter().any(|m| {
                if let infinity_provider_protocol::message::Message::User { content } = m
                    && let Some(UserContent::ToolResult(r)) = content.first()
                    && let Some(infinity_provider_protocol::message::ToolResultContent::Text(t)) =
                        r.content.first()
                {
                    return t.text.contains("successfully spawned");
                }
                false
            });
            assert!(is_parent, "expected parent follow-up request on ctrl2");
            ctrl2.send_text("ok");
            ctrl2.finish();
            collect_until_done(&mut display_rx).await;

            // The child thread should also use model2 (inherited from the
            // parent). With the bug, the child would use the default model
            // (provider1), so ctrl1 would get the request instead.
            let child_req =
                tokio::time::timeout(std::time::Duration::from_secs(5), ctrl2.next_request())
                    .await
                    .expect(
                        "child thread should use model2 (parent's model), not model1 (default)",
                    );
            let has_instructions = child_req.chat_history.iter().any(|m| {
                if let infinity_provider_protocol::message::Message::User { content } = m
                    && let Some(UserContent::ToolResult(r)) = content.first()
                    && let Some(infinity_provider_protocol::message::ToolResultContent::Text(t)) =
                        r.content.first()
                {
                    return t.text.contains("do something");
                }
                false
            });
            assert!(
                has_instructions,
                "child thread should have received spawn instructions"
            );
            assert!(
                ctrl1.try_next_request().is_none(),
                "default model (provider1/model1) should not have received any requests"
            );

            ctrl2.send_text("child done");
            ctrl2.finish();
        })
        .await;
}

/// The daemon observer persists token usage on `CompletionFinished` and
/// resets it when compaction is applied — synchronously at the emission
/// point, before the event reaches any client.
#[tokio::test(flavor = "current_thread")]
async fn observer_persists_usage_and_resets_on_compaction() {
    use infinity_agent_core::system::{AgentEvent, ThreadObserver};

    let (conv, _state, _dir) = tmp_stores(model1_ref());
    conv.ensure_root_thread(ThreadId::from_ref("t1"))
        .await
        .expect("ensure root");
    let observer = DaemonObserver {
        subscribers: Default::default(),
        conversation_store: conv.clone(),
    };

    observer.on_event(
        ThreadId::from_ref("t1"),
        &AgentEvent::CompletionFinished {
            usage: Some(infinity_provider_protocol::Usage {
                input_tokens: 40,
                output_tokens: 2,
                total_tokens: 42,
                cached_input_tokens: 0,
            }),
        },
    );
    assert_eq!(conv.get_total_tokens_used(ThreadId::from_ref("t1")), 42);

    // A usage-less response must not reset the stored total.
    observer.on_event(
        ThreadId::from_ref("t1"),
        &AgentEvent::CompletionFinished { usage: None },
    );
    assert_eq!(conv.get_total_tokens_used(ThreadId::from_ref("t1")), 42);

    // Compaction resets the stale pre-compaction total.
    observer.on_event(ThreadId::from_ref("t1"), &AgentEvent::CompactionApplied);
    assert_eq!(conv.get_total_tokens_used(ThreadId::from_ref("t1")), 0);
}

#[tokio::test(flavor = "current_thread")]
async fn answered_user_choice_emits_complete_and_disappears_from_replay() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (conv, state, _dir) = tmp_stores(model1_ref());
            let (model, mut ctrl) = mock_model();
            let catalog = two_model_catalog(model.clone(), model).await;
            conv.ensure_root_thread(ThreadId::from_ref("t1"))
                .await
                .expect("ensure root");
            let (running, mut display_rx, _) = start_daemon_system(
                ThreadId::from_ref("t1"),
                conv,
                state.clone(),
                catalog,
                vec![Box::new(AsyncStubTool)],
            );

            running
                .send_user_text(ThreadId::from_ref("t1"), "use the tool")
                .await;
            let _request = ctrl.next_request().await;
            ctrl.send_tool_call("tc-choice", "async_tool", serde_json::json!({}));
            ctrl.finish();
            collect_until_done(&mut display_rx).await;

            running
                .send(
                    InputMessage {
                        content: InputMessageContent::UserChoice(UserChoiceRequired {
                            content_type: "user_choice_required".to_owned(),
                            id: "tc-choice".into(),
                            call_id: None,
                            prompt: "Pick one".to_owned(),
                            choices: vec!["A".to_owned(), "B".to_owned()],
                            default: 0,
                            response_url: "http://example.test/choice".to_owned(),
                        }),
                        group_id: "t1".into(),
                        metadata: None,
                        synthetic: None,
                        display_as: None,
                        subscription: false,
                    },
                    "choice-message",
                )
                .await;
            assert!(matches!(
                display_rx.recv().await,
                Some(DaemonMessage::UserChoiceRequired { id, .. }) if id.as_str() == "tc-choice"
            ));

            running
                .send(
                    tool_result_input(ThreadId::from_ref("t1"), "tc-choice", "selected A"),
                    "choice-result",
                )
                .await;
            let mut saw_complete = false;
            while !saw_complete {
                match tokio::time::timeout(std::time::Duration::from_secs(5), display_rx.recv())
                    .await
                    .expect("complete event timeout")
                    .expect("display channel closed")
                {
                    DaemonMessage::UserChoiceComplete { choice_id } => {
                        assert_eq!(choice_id.as_str(), "tc-choice");
                        saw_complete = true;
                    }
                    DaemonMessage::StartOutput { .. } => {
                        ctrl.send_text("done");
                        ctrl.finish();
                    }
                    _ => {}
                }
            }
            assert!(
                state
                    .get_pending_user_choices(ThreadId::from_ref("t1"))
                    .await
                    .expect("load choices")
                    .is_empty()
            );

            let (replay_tx, mut replay_rx) = mpsc::unbounded_channel();
            running
                .subscribe(
                    ThreadId::from_ref("t1"),
                    SubscribeRequest {
                        tx: replay_tx,
                        wants_replay: true,
                        keeps_session_alive: true,
                    },
                )
                .await;
            match tokio::time::timeout(std::time::Duration::from_secs(5), replay_rx.recv())
                .await
                .expect("replay timeout")
                .expect("replay channel closed")
            {
                DaemonMessage::Replay {
                    pending_choices, ..
                } => assert!(pending_choices.is_empty()),
                other => panic!("expected replay, got {other:?}"),
            }
        })
        .await;
}

/// A pending choice on any child thread represents session-wide waiting state,
/// even though choice lookup and response routing remain thread-scoped.
#[tokio::test(flavor = "current_thread")]
async fn child_pending_choice_updates_root_session_status() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (model, _ctrl) = mock_model();
            let entry = ModelEntry {
                model_id: "model1".to_owned(),
                display_name: "model1".to_owned(),
                context_window: 100_000,
                max_output_tokens: None,
                supports_image_input: false,
            };
            let state_dir = tempfile::tempdir().expect("create daemon state dir");
            let cwd = tempfile::tempdir().expect("create session cwd");
            let manager = SessionManager::with_providers(
                SessionManagerConfig {
                    state_dir: state_dir.path().to_path_buf(),
                    callback_url: "http://127.0.0.1:0".to_owned(),
                    user_rap_config: None,
                    id_source: Arc::new(SequentialIdSource::new()),
                },
                vec![(
                    "provider1".to_owned(),
                    Arc::new(SingleModelProvider::new(entry, model)) as Arc<dyn ModelProvider>,
                )],
                vec![],
            )
            .await
            .expect("build session manager");

            let mut emit = |_message| async {};
            let session_id = manager
                .create_session(cwd.path(), model1_ref(), &mut emit)
                .await
                .expect("create session");
            let child_id = manager
                .conversation_store
                .spawn_thread(
                    &session_id,
                    rap_protocol::ToolCallId::from_ref("spawn-call"),
                    false,
                    SpawnContext::InheritUpTo(0),
                )
                .await
                .expect("spawn child thread");
            let choice = UserChoice {
                id: "choice-1".into(),
                prompt: "Choose".to_owned(),
                choices: vec!["one".to_owned(), "two".to_owned()],
                default: 0,
                response_url: "http://127.0.0.1:0/choice".to_owned(),
            };

            manager
                .state_store
                .add_pending_user_choice(&child_id, choice.clone())
                .await
                .expect("add pending choice to child");
            assert!(manager.state_store.has_pending_choices(&child_id));
            assert!(!manager.state_store.has_pending_choices(&session_id));
            assert_eq!(
                manager
                    .list_sessions(None)
                    .await
                    .get(&infinity_protocol::ThreadRef::local(session_id.clone()))
                    .expect("created session is listed")
                    .status,
                SessionStatus::WaitingForChoice
            );

            manager
                .state_store
                .remove_pending_user_choice(&child_id, &choice.id)
                .await
                .expect("dismiss child pending choice");
            assert_eq!(
                manager
                    .list_sessions(None)
                    .await
                    .get(&infinity_protocol::ThreadRef::local(session_id.clone()))
                    .expect("created session is still listed")
                    .status,
                SessionStatus::Running
            );

            manager
                .state_store
                .add_pending_user_choice(&child_id, choice)
                .await
                .expect("restore pending choice before cleanup");
            manager.cleanup_session(&session_id).await;
            assert!(!manager.state_store.has_pending_choices(&child_id));
            assert_eq!(
                manager
                    .list_sessions(None)
                    .await
                    .get(&infinity_protocol::ThreadRef::local(session_id.clone()))
                    .expect("cleaned-up session is listed")
                    .status,
                SessionStatus::Stopped
            );
        })
        .await;
}

/// A callback-style event (a tool result with no live driver) for a session
/// the user shut down is refused by the stopped-thread policy inside the
/// router, while the same event for a live session wakes its thread. This
/// keeps stale RAP callbacks from reviving stopped sessions regardless of
/// which input surface delivered them.
#[tokio::test(flavor = "current_thread")]
async fn shut_down_session_events_do_not_wake_threads() {
    let local = tokio::task::LocalSet::new();
    local
        .run_until(async {
            let (model, mut ctrl) = mock_model();
            let (model2, _ctrl2) = mock_model();
            let catalog = two_model_catalog(model, model2).await;
            let (conv, _state, dir) = tmp_stores(model1_ref());
            conv.ensure_root_thread(ThreadId::from_ref("live"))
                .await
                .expect("ensure root");
            conv.ensure_root_thread(ThreadId::from_ref("stopped"))
                .await
                .expect("ensure root");

            // Session registry: both sessions exist; one is shut down.
            let (change_tx, _change_rx) = mpsc::unbounded_channel();
            let session_store = Arc::new(tokio::sync::Mutex::new(
                crate::session_store::SessionStore::load(
                    &dir.path().join("sessions.json").to_string_lossy(),
                    change_tx,
                ),
            ));
            {
                let mut store = session_store.lock().await;
                store.create(ThreadId::from_ref("live"), dir.path().to_path_buf());
                store.create(ThreadId::from_ref("stopped"), dir.path().to_path_buf());
                store.mark_shut_down(ThreadId::from_ref("stopped"));
            }
            let state =
                PersistentStateStore::new(dir.path().join("state"), conv.clone(), session_store);

            let (mut running, mut display_rx, _smap) = start_daemon_system(
                ThreadId::from_ref("live"),
                conv.clone(),
                state,
                catalog,
                vec![],
            );

            // The stopped session's event is dropped: no driver wakes, so no
            // thread exit is ever reported for it.
            running
                .send(
                    tool_result_input(ThreadId::from_ref("stopped"), "tc-stale", "late result"),
                    "cb-stale",
                )
                .await;

            // The live session processes user text normally afterwards,
            // proving the router did not stall on the dropped event.
            running
                .send_user_text(ThreadId::from_ref("live"), "hello")
                .await;
            let _req = ctrl.next_request().await;
            ctrl.send_text("hi");
            ctrl.finish();
            collect_until_done(&mut display_rx).await;

            // Every lifecycle transition must belong to the live session;
            // the stopped session's event may never wake a driver.
            let mut saw_live_idle = false;
            while !saw_live_idle {
                let event = tokio::time::timeout(
                    std::time::Duration::from_secs(5),
                    running.next_lifecycle_event(),
                )
                .await
                .expect("timed out waiting for the live thread to idle")
                .expect("thread lifecycle channel closed");
                assert_eq!(
                    event.thread_id.as_str(),
                    "live",
                    "only the live session's driver may run"
                );
                if event.state == ThreadLifecycleState::Idle {
                    saw_live_idle = true;
                }
            }
            assert!(
                running.is_idle(),
                "the stopped session must not have a live driver"
            );
        })
        .await;
}