bitrouter 1.0.0-alpha.34

BitRouter: an LLM API router. CLI + assembly library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
//! Interactive Thread client; the server owns execution and durable context.
mod presentation;

use crate::agent_local::{self, Operation, ReplyResult, ThreadClient};
use anyhow::Result;
use bitrouter_ai::types::Content;
use bitrouter_orchestrator::service::{ErrorCode, ServiceError};
use bitrouter_orchestrator::thread::{
    ThreadChange, ThreadDirectoryPage, ThreadEvent, ThreadObservation, ThreadStatus, ThreadView,
};
use bitrouter_orchestrator::turn::TurnLifecycle;
use bitrouter_orchestrator::turn::TurnSnapshot;
use bitrouter_tui::agents_menu::MenuEntry;
use bitrouter_tui::editor::{Edit, Editor, press};
use bitrouter_tui::native_agent::{
    NativeEntryKind, NativeLive, NativeState, NativeView, ToolStatus,
};
use crossterm::event::{Event, EventStream, KeyCode, KeyModifiers};
use futures::{FutureExt, StreamExt};
use presentation::{tool_result, tool_title};
use std::path::{Path, PathBuf};
use std::time::Duration;

struct PendingSubmission {
    text: String,
    key: String,
    mode: SubmissionMode,
    uncertain: bool,
}
enum SubmissionMode {
    Start,
    Enqueue,
    Steer(String),
}
#[derive(Default)]
struct Session {
    model_mode: bitrouter_orchestrator::core::protocol::ModelMode,
    max_output_tokens: Option<u32>,
    model_config: Option<bitrouter_sdk::config::Config>,
    thread_id: Option<String>,
    create_key: String,
    create_uncertain: bool,
    resume_key: Option<String>,
    pending: Option<PendingSubmission>,
}

#[derive(Default)]
struct Directory {
    after: u64,
    cutoff: Option<u64>,
    next: Option<u64>,
    previous: Vec<u64>,
}

fn inventory_request(
    client: &ThreadClient,
    directory: &Directory,
) -> futures::future::BoxFuture<'static, Result<ThreadDirectoryPage>> {
    let client = client.clone();
    let operation = Operation::ListThreads {
        after: directory.after,
        cutoff: directory.cutoff,
        limit: 16,
    };
    async move {
        match client.request(operation).await? {
            ReplyResult::Directory { page } => Ok(page),
            _ => anyhow::bail!("unexpected directory reply"),
        }
    }
    .boxed()
}

fn show_directory(state: &mut NativeState, directory: &mut Directory, page: ThreadDirectoryPage) {
    directory.cutoff = Some(page.cutoff);
    directory.next = page.next_after;
    state.inventory = page
        .entries
        .into_iter()
        .map(|entry| {
            let thread = entry.thread;
            let status = match thread.status {
                ThreadStatus::Paused => "paused",
                ThreadStatus::RecoveryRequired => "recovery required",
                ThreadStatus::Closing => "closing",
                _ if entry.needs_input => "needs input",
                ThreadStatus::Busy => "working",
                _ => "inactive",
            };
            MenuEntry {
                id: thread.thread_id.clone(),
                label: format!(
                    "{} · {}",
                    thread.model,
                    thread.thread_id.get(..8).unwrap_or(&thread.thread_id)
                ),
                directory: thread.workspace.display().to_string(),
                status: status.into(),
                needs_input: entry.needs_input,
                working: thread.status == ThreadStatus::Busy && !entry.needs_input,
                metadata: vec![
                    format!("Thread: {}", thread.thread_id),
                    format!(
                        "Model: {} · Directory: {}",
                        thread.model,
                        thread.workspace.display()
                    ),
                    format!(
                        "Turn: {} · {} · Queue: {}",
                        entry.turn_id.as_deref().unwrap_or("none"),
                        entry
                            .turn_status
                            .and_then(|status| serde_json::to_value(status).ok())
                            .and_then(|value| value.as_str().map(str::to_owned))
                            .unwrap_or_else(|| "none".into()),
                        thread.queued.len()
                    ),
                    format!(
                        "Permissions: {}",
                        match thread.permission_profile {
                            bitrouter_orchestrator::thread::PermissionProfile::ReadOnly =>
                                "read only",
                            bitrouter_orchestrator::thread::PermissionProfile::Ask => "ask",
                            bitrouter_orchestrator::thread::PermissionProfile::AllowEffects =>
                                "allow effects",
                        }
                    ),
                    thread.pause_reason.unwrap_or_default(),
                ],
            }
        })
        .collect();
    state.menu.receive_entries(&state.inventory);
    state.inventory_help = format!(
        "↑↓ · Tab · / search page · Enter view · r refresh{}{} · Esc back",
        if directory.next.is_some() {
            " · n next"
        } else {
            ""
        },
        if directory.previous.is_empty() {
            ""
        } else {
            " · p previous"
        }
    );
}

pub struct Options {
    pub read_only: bool,
    pub model_policy: bool,
    pub max_output_tokens: Option<u32>,
}

pub async fn run(
    config: Option<&Path>,
    control_override: Option<&Path>,
    model_override: Option<String>,
    reattach: Option<String>,
    check: Option<String>,
    options: Options,
    workspace_override: Option<PathBuf>,
) -> Result<()> {
    let read_only = options.read_only;
    let source = crate::paths::resolve_config(config)?;
    let cfg = crate::paths::load_config(&source).await?;
    let control_socket = control_override
        .map(Path::to_path_buf)
        .unwrap_or_else(|| crate::daemon::socket_path_for(&source, &cfg));
    let socket = if control_override.is_some() {
        let socket = agent_local::socket_path(&control_socket);
        match agent_local::request(&socket, Operation::Capabilities).await? {
            ReplyResult::Capabilities { .. } => socket,
            _ => anyhow::bail!("unexpected capabilities reply"),
        }
    } else {
        agent_local::connect_or_start(&source, &control_socket).await?
    };
    let client = ThreadClient::connect(&socket).await?;
    let workspace = workspace_override.unwrap_or(std::env::current_dir()?);
    let mut state = NativeState {
        model: model_override
            .or_else(|| crate::policy_lock::native_default_model(&cfg).map(str::to_owned))
            .unwrap_or_default(),
        workspace: workspace.display().to_string(),
        status: "idle".into(),
        verification: "unavailable".into(),
        ..NativeState::default()
    };
    let mut session = Session {
        max_output_tokens: options.max_output_tokens,
        model_mode: if options.model_policy {
            bitrouter_orchestrator::core::protocol::ModelMode::Policy
        } else if state.model.is_empty() || reattach.is_some() {
            bitrouter_orchestrator::core::protocol::ModelMode::Fixed
        } else {
            crate::policy_lock::native_model_mode(&cfg, &state.model)?
        },
        model_config: Some(cfg),
        thread_id: reattach,
        create_key: uuid::Uuid::new_v4().to_string(),
        pending: None,
        ..Default::default()
    };
    if state.model.is_empty() {
        state.model_editor = Some(Editor::default());
    }
    let mut directory = Directory::default();
    let mut inventory_poll = None;
    let mut inventory_ticks = 0_u8;
    let mut projection: Option<ThreadView> = None;
    let mut subscription = None;
    let mut cursor = 0;
    let mut reconnect = true;
    if let Some(id) = &session.thread_id {
        let view = restore_history(&client, id, &mut state).await?;
        state.push(format!("Reattached Thread: {id}"));
        update_view(&mut state, &view);
        cursor = view.thread.cursor;
        subscription = Some(client.observe(id, Some(cursor)).await?);
        projection = Some(view);
    }
    let mut view = NativeView::open()?;
    let mut events = Some(EventStream::new());
    let mut shutdown = crate::chat::signals::Shutdown::install();
    let mut ticker = tokio::time::interval(Duration::from_millis(500));
    loop {
        view.draw(&mut state)?;
        tokio::select! {
            next = async { match subscription.as_mut() { Some(stream) => stream.next().await, None => std::future::pending().await } } => {
                match next {
                    Ok(Some(ThreadObservation::Snapshot { view:fresh, resynchronized, catchup })) => {
                        if fresh.thread.cursor < cursor { continue; }
                        if resynchronized && let Some(id) = &session.thread_id {
                            match restore_history_at(&client, id, &fresh, &mut state).await {
                                Ok(()) => state.push("History cache expired; committed history restored"),
                                Err(error) => state.push(format!("History unavailable: {error}")),
                            }
                        }
                        for event in catchup { if event.seq > cursor { show_event(&mut state, &event); } }
                        cursor = fresh.thread.cursor; update_view(&mut state, &fresh); projection = Some(*fresh);
                    },
                    Ok(Some(ThreadObservation::Event { event })) => {
                        if event.seq <= cursor { continue; }
                        cursor = event.seq; show_event(&mut state, &event);
                        if let Some(view) = projection.as_mut() { view.apply(&event); update_view(&mut state, view); }
                    },
                    Ok(Some(ThreadObservation::Live { event, after_cursor })) => {
                        if after_cursor != cursor { continue; }
                        if let Some(view) = projection.as_mut() && let Some(turn) = &mut view.latest_turn && turn.turn_id == event.turn_id { turn.apply(&event); update_view(&mut state, view); }
                    },
                    Ok(None) => { subscription = None; state.status="disconnected".into(); state.pending_input_id=None; state.push("Connection lost; Thread remains on the server"); },
                    Err(error) => {
                        subscription = None;
                        reconnect = !error.downcast_ref::<ServiceError>().is_some_and(|error| matches!(error.code, ErrorCode::InstanceChanged | ErrorCode::UnknownThread));
                        state.status = if reconnect { "disconnected" } else { "server_instance_lost" }.into(); state.pending_input_id=None; state.push(error.to_string());
                    },
                }
            },
            result = async { match inventory_poll.as_mut() { Some(poll) => poll.await, None => std::future::pending().await } } => {
                inventory_poll = None;
                match result {
                    Ok(page) => show_directory(&mut state, &mut directory, page),
                    Err(error) => state.menu.set_error(Some(format!("BRO inventory unavailable: {error}"))),
                }
            },
            _ = ticker.tick() => {
                inventory_ticks = inventory_ticks.wrapping_add(1);
                if state.menu.is_open() && inventory_poll.is_none() && inventory_ticks.is_multiple_of(4) {
                    inventory_poll = Some(inventory_request(&client, &directory));
                }
                if subscription.is_none() && reconnect && let Some(id) = &session.thread_id { match client.observe(id, Some(cursor)).await { Ok(stream) => subscription=Some(stream), Err(error) => {
                    if error.downcast_ref::<ServiceError>().is_some_and(|error| matches!(error.code, ErrorCode::InstanceChanged | ErrorCode::UnknownThread)) {
                        reconnect=false; state.status="server_instance_lost".into();
                    }
                    state.push(format!("Reconnect pending: {error}"));
                } } }
            },
            signal = shutdown.recv() => match signal {
                crate::chat::signals::TerminalSignal::Shutdown => break,
                crate::chat::signals::TerminalSignal::Suspend => {
                    drop(events.take()); view.suspend()?;
                    crate::chat::signals::suspend_current_process()?;
                    view.resume()?; events = Some(EventStream::new());
                }
            },
            next = async { match events.as_mut() { Some(events) => events.next().await, None => std::future::pending().await } } => {
                let Some(next) = next else { break; };
                let event = next?;
                if bitrouter_tui::editor::is_redraw(&event) { view.invalidate(); continue; }
                if state.menu.is_open() {
                    if let Some(key) = press(&event) {
                        if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
                            if handle_event(&client, &workspace, &check, read_only, &mut state, &mut session, &event).await? { break; }
                            continue;
                        }
                        if key.code == KeyCode::Char('d') && key.modifiers.contains(KeyModifiers::CONTROL) { break; }
                        if key.code == KeyCode::Char('o') && key.modifiers.is_empty() && state.input_ready()
                            && let Some(id) = state.menu.previewed_id().map(str::to_owned)
                        {
                            if session.pending.is_some() || session.create_uncertain || !state.editor.is_empty() {
                                state.menu.set_error(Some("Resolve the pending acceptance before switching conversations".into()));
                            } else {
                                let mut next = NativeState { inventory: std::mem::take(&mut state.inventory), ..Default::default() };
                                match restore_history(&client, &id, &mut next).await {
                                    Ok(fresh) => match client.observe(&id, Some(fresh.thread.cursor)).await {
                                        Ok(stream) => {
                                            cursor = fresh.thread.cursor; projection = Some(fresh);
                                            subscription = Some(stream); reconnect = true;
                                            session = Session { thread_id: Some(id), create_key: uuid::Uuid::new_v4().to_string(), ..Default::default() };
                                            next.menu = std::mem::take(&mut state.menu); next.menu.close();
                                            state = next;
                                        },
                                        Err(error) => { state.inventory = next.inventory; state.menu.set_error(Some(error.to_string())); },
                                    },
                                    Err(error) => { state.inventory = next.inventory; state.menu.set_error(Some(error.to_string())); },
                                }
                            }
                            continue;
                        }
                        if key.modifiers.is_empty() && state.menu.is_listing() && inventory_poll.is_none() {
                            let changed = match key.code {
                                KeyCode::Char('n') => if let Some(next) = directory.next { directory.previous.push(directory.after); directory.after = next; true } else { false },
                                KeyCode::Char('p') => if let Some(previous) = directory.previous.pop() { directory.after = previous; true } else { false },
                                KeyCode::Char('r') => { directory = Directory::default(); true },
                                _ => false,
                            };
                            if changed { inventory_poll = Some(inventory_request(&client, &directory)); continue; }
                        }
                    }
                    // Menu keys never reach approval, steering or the composer.
                    let page_size = state.viewport.map_or(4, |size| usize::from((size.height.saturating_mul(2) / 5).max(6).saturating_sub(3)).max(1));
                    state.menu.event_entries(&event, &state.inventory, page_size);
                    continue;
                }
                if let Some(key) = press(&event) && key.code == KeyCode::Left && key.modifiers.is_empty() && state.can_open_agents() {
                    state.menu.open_entries(&state.inventory);
                    inventory_poll = Some(inventory_request(&client, &directory));
                    continue;
                }
                if handle_event(&client, &workspace, &check, read_only, &mut state, &mut session, &event).await? { break; }
            },
        }
    }
    Ok(())
}

async fn handle_event(
    client: &ThreadClient,
    workspace: &Path,
    check: &Option<String>,
    read_only: bool,
    state: &mut NativeState,
    session: &mut Session,
    event: &Event,
) -> Result<bool> {
    let steering = matches!(event, Event::Key(key) if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Enter);
    if let Event::Key(key) = event {
        if key.kind != crossterm::event::KeyEventKind::Press {
            return Ok(false);
        }
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('d') {
            return Ok(true);
        }
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            if let (Some(thread_id), Some(turn_id)) = (&session.thread_id, &state.turn_id) {
                match client
                    .request(Operation::CancelTurn {
                        thread_id: thread_id.clone(),
                        turn_id: turn_id.clone(),
                        idempotency_key: format!("cancel-{turn_id}"),
                    })
                    .await
                {
                    Ok(_) => state.push("Cancellation requested"),
                    Err(error) => state.push(error.to_string()),
                }
            } else {
                return Ok(true);
            }
            return Ok(false);
        }
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('r') {
            if let Some(thread_id) = &session.thread_id {
                match client
                    .request(Operation::ResumeQueue {
                        thread_id: thread_id.clone(),
                        idempotency_key: session
                            .resume_key
                            .get_or_insert_with(|| uuid::Uuid::new_v4().to_string())
                            .clone(),
                    })
                    .await
                {
                    Ok(_) => {
                        session.resume_key = None;
                        state.push("Queue resume requested");
                    }
                    Err(error) => state.push(error.to_string()),
                }
            }
            return Ok(false);
        }
        if !state.input_ready()
            && (key.code == KeyCode::Enter
                || (state.editor.is_empty()
                    && state.pending_input_id.is_some()
                    && matches!(key.code, KeyCode::Char('y' | 'n'))))
        {
            state.push("Resize to at least 40×16 before submitting or approving");
            return Ok(false);
        }
        if state.editor.is_empty()
            && let (Some(thread_id), Some(turn_id), Some(request_id)) =
                (&session.thread_id, &state.turn_id, &state.pending_input_id)
        {
            let approved = match key.code {
                KeyCode::Char('y') => Some(true),
                KeyCode::Char('n') => Some(false),
                _ => None,
            };
            if let Some(approved) = approved {
                match client
                    .request(Operation::Input {
                        thread_id: thread_id.clone(),
                        turn_id: turn_id.clone(),
                        request_id: request_id.clone(),
                        approved,
                        idempotency_key: format!("{request_id}-{approved}"),
                    })
                    .await
                {
                    Ok(_) => {
                        state.pending_input_id = None;
                        state.push(if approved {
                            "Tool approved"
                        } else {
                            "Tool denied"
                        });
                    }
                    Err(error) => state.push(error.to_string()),
                }
                return Ok(false);
            }
        }
    }
    if let Some(editor) = state.model_editor.as_mut() {
        if bitrouter_tui::native_agent::edit(editor, event) == Edit::Submitted
            && !editor.text().trim().is_empty()
        {
            let model = editor.text().trim().to_owned();
            if session.model_mode == bitrouter_orchestrator::core::protocol::ModelMode::Fixed
                && let Some(config) = &session.model_config
            {
                match crate::policy_lock::native_model_mode(config, &model) {
                    Ok(mode) => session.model_mode = mode,
                    Err(error) => {
                        state.push(error.to_string());
                        return Ok(false);
                    }
                }
            }
            state.model = model;
            state.model_editor = None;
            state.push(format!("Model selected: {}", state.model));
        }
        return Ok(false);
    }
    let edit = if steering {
        Edit::Submitted
    } else {
        state.edit(event)
    };
    match edit {
        Edit::Submitted => {
            let input = state.editor.text().to_owned();
            if input.trim().is_empty() {
                return Ok(false);
            }
            if matches!(
                state.status.as_str(),
                "disconnected" | "server_instance_lost"
            ) {
                state.push("Reconnect before submitting; draft retained");
                return Ok(false);
            }
            if state.model.is_empty() {
                state.model_editor = Some(Editor::default());
                return Ok(false);
            }
            if let Some(pending) = &session.pending
                && pending.text != input
            {
                state.push("Previous acceptance is unresolved; restore its input to retry with the same key");
                return Ok(false);
            }
            if session.pending.is_none() {
                let mode = if steering {
                    let Some(id) = &state.turn_id else {
                        state.push("No active Turn to steer");
                        return Ok(false);
                    };
                    SubmissionMode::Steer(id.clone())
                } else if matches!(
                    state.status.as_str(),
                    "accepted" | "running" | "waiting_for_input" | "busy" | "paused"
                ) {
                    SubmissionMode::Enqueue
                } else {
                    SubmissionMode::Start
                };
                session.pending = Some(PendingSubmission {
                    text: input.clone(),
                    key: uuid::Uuid::new_v4().to_string(),
                    mode,
                    uncertain: false,
                });
            }
            if session.thread_id.is_none() {
                match client
                    .request(Operation::CreateThread {
                        workspace: workspace.into(),
                        model: state.model.clone(),
                        model_mode: session.model_mode,
                        effort: None,
                        max_output_tokens: session.max_output_tokens,
                        read_only,
                        verification_command: check.clone(),
                        idempotency_key: session.create_key.clone(),
                    })
                    .await
                {
                    Ok(ReplyResult::Thread { snapshot }) => {
                        session.thread_id = Some(snapshot.thread_id.clone());
                        state.thread_id = Some(snapshot.thread_id);
                    }
                    Ok(_) => anyhow::bail!("unexpected create_thread reply"),
                    Err(error) => {
                        if known_rejection(&error) && !session.create_uncertain {
                            session.pending = None;
                            session.create_key = uuid::Uuid::new_v4().to_string();
                        } else {
                            session.create_uncertain = true;
                        }
                        state.push(format!("Create outcome: {error}; draft retained"));
                        return Ok(false);
                    }
                }
            }
            let pending = session
                .pending
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("missing pending input"))?;
            let id = session
                .thread_id
                .clone()
                .ok_or_else(|| anyhow::anyhow!("missing Thread identity"))?;
            let operation = match &pending.mode {
                SubmissionMode::Start => Operation::StartTurn {
                    thread_id: id,
                    prompt: input.clone(),
                    idempotency_key: pending.key.clone(),
                },
                SubmissionMode::Enqueue => Operation::EnqueueTurn {
                    thread_id: id,
                    prompt: input.clone(),
                    idempotency_key: pending.key.clone(),
                },
                SubmissionMode::Steer(turn_id) => Operation::Steer {
                    thread_id: id,
                    expected_turn_id: turn_id.clone(),
                    text: input.clone(),
                    idempotency_key: pending.key.clone(),
                },
            };
            match client.request(operation).await {
                Ok(ReplyResult::Receipt { receipt }) => {
                    if matches!(pending.mode, SubmissionMode::Start) {
                        state.turn_id = Some(receipt.turn_id.clone());
                        state.status = "accepted".into();
                    }
                    state.push(format!(
                        "Turn ID: {} ({:?})",
                        receipt.turn_id, receipt.status
                    ));
                }
                Ok(ReplyResult::Steering { receipt }) => {
                    state.push(format!("Steering received: {}", receipt.input_id));
                }
                Ok(_) => anyhow::bail!("unexpected input receipt"),
                Err(error) => {
                    state.push(format!("Acceptance outcome: {error}; draft retained"));
                    if known_rejection(&error) && !pending.uncertain {
                        session.pending = None;
                    } else if let Some(pending) = session.pending.as_mut() {
                        pending.uncertain = true;
                    }
                    return Ok(false);
                }
            }
            state.editor.clear();
            state.editor.push_history(input.clone());
            session.pending = None;
        }
        Edit::Ended | Edit::ExitRequested => return Ok(true),
        _ => {}
    }
    Ok(false)
}
async fn read(client: &ThreadClient, thread_id: &str) -> Result<ThreadView> {
    match client
        .request(Operation::ReadThread {
            thread_id: thread_id.into(),
        })
        .await?
    {
        ReplyResult::View { view } => Ok(*view),
        _ => anyhow::bail!("unexpected Thread view"),
    }
}
fn update_view(state: &mut NativeState, view: &ThreadView) {
    state.thread_id = Some(view.thread.thread_id.clone());
    state.model = view.thread.model.clone();
    state.model_editor = None;
    state.workspace = view.thread.workspace.display().to_string();
    state.queued = view.thread.queued.len();
    if let Some(turn) = &view.latest_turn {
        update_snapshot(state, turn);
    } else {
        state.status = "idle".into();
        state.live = None;
        state.pending_input_id = None;
        state.pending_input_detail = None;
    }
    state.turn_id = view.thread.active_turn_id.clone();
    if matches!(
        view.thread.status,
        ThreadStatus::Paused | ThreadStatus::RecoveryRequired
    ) {
        state.status = if view.thread.status == ThreadStatus::Paused {
            "paused"
        } else {
            "recovery_required"
        }
        .into();
    }
}
fn message_text(message: &bitrouter_ai::types::Message) -> String {
    message
        .content
        .iter()
        .filter_map(|part| match part {
            Content::Text { text, .. } => Some(text.as_str()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn show_event(state: &mut NativeState, event: &ThreadEvent) {
    for change in &event.changes {
        let (id, text, kind) = match change {
            ThreadChange::ContextRouting {
                turn_id,
                inspection,
            } => (
                format!("context:{turn_id}"),
                context_status(inspection),
                NativeEntryKind::Detail,
            ),
            ThreadChange::TurnQueued {
                user_item_id,
                prompt,
                ..
            } => (user_item_id.clone(), prompt.clone(), NativeEntryKind::User),
            ThreadChange::AssistantResponse {
                item_id, message, ..
            } => (
                item_id.clone(),
                message_text(message),
                NativeEntryKind::Assistant,
            ),
            ThreadChange::AssistantInterrupted {
                item_id,
                partial,
                detail,
                ..
            } => (
                item_id.clone(),
                format!("{}\nAssistant interrupted: {detail}", message_text(partial)),
                NativeEntryKind::Assistant,
            ),
            ThreadChange::ToolIntent { call, .. } => (
                call.item_id.clone(),
                tool_title(&call.name, &call.arguments),
                NativeEntryKind::Tool {
                    status: ToolStatus::Running,
                    detail: String::new(),
                },
            ),
            ThreadChange::ToolResult {
                item_id,
                message,
                effect,
                ..
            } => {
                let (text, kind) = tool_result(state, item_id, message, *effect);
                (item_id.clone(), text, kind)
            }
            ThreadChange::VerificationResult {
                call,
                evidence,
                effect,
                ..
            } => (
                call.item_id.clone(),
                format!("check {}", evidence.command),
                NativeEntryKind::Tool {
                    status: if *effect == bitrouter_orchestrator::store::EffectStatus::Unknown {
                        ToolStatus::Unknown
                    } else if *effect == bitrouter_orchestrator::store::EffectStatus::NotExecuted {
                        ToolStatus::Denied
                    } else if evidence.exit_status == Some(0)
                        && !evidence.timed_out
                        && evidence.error.is_none()
                    {
                        ToolStatus::Succeeded
                    } else {
                        ToolStatus::Failed
                    },
                    detail: evidence.error.clone().unwrap_or_else(|| {
                        if evidence.timed_out {
                            "Timed out".into()
                        } else {
                            evidence
                                .exit_status
                                .filter(|code| *code != 0)
                                .map(|code| format!("Exit {code}"))
                                .unwrap_or_default()
                        }
                    }),
                },
            ),
            ThreadChange::TurnLifecycle {
                turn_id,
                lifecycle: TurnLifecycle::Finished { status, detail, .. },
            } if *status == bitrouter_orchestrator::turn::TurnStatus::Failed => (
                format!("failure:{turn_id}"),
                format!("Turn failed: {detail}"),
                NativeEntryKind::Detail,
            ),
            ThreadChange::TurnLifecycle {
                lifecycle:
                    TurnLifecycle::InputRequested {
                        request_id,
                        tool_name,
                        arguments,
                        ..
                    },
                ..
            } => (
                format!("approval:{request_id}"),
                format!("Approval required: {tool_name}\n{arguments}"),
                NativeEntryKind::Detail,
            ),
            ThreadChange::TurnLifecycle {
                lifecycle:
                    TurnLifecycle::InputResolved {
                        request_id,
                        approved,
                    },
                ..
            } => (
                format!("approval:{request_id}"),
                format!("Approval {}", if *approved { "accepted" } else { "denied" }),
                NativeEntryKind::Detail,
            ),
            ThreadChange::TurnLifecycle {
                lifecycle: TurnLifecycle::SteeringUpdated { receipt, text },
                ..
            } => {
                let id = format!("steering:{}", receipt.input_id);
                let prompt = text
                    .as_deref()
                    .or_else(|| {
                        state
                            .entries
                            .iter()
                            .find(|entry| entry.id == id)
                            .and_then(|entry| {
                                entry
                                    .text
                                    .split_once("\nSteering status:")
                                    .map(|(prompt, _)| prompt)
                            })
                    })
                    .unwrap_or("Steering input");
                let status = match receipt.status {
                    bitrouter_orchestrator::turn::SteeringStatus::Received => "received",
                    bitrouter_orchestrator::turn::SteeringStatus::Applied => "applied",
                    bitrouter_orchestrator::turn::SteeringStatus::NotApplied => "not applied",
                };
                (
                    id,
                    format!(
                        "{prompt}\nSteering status: {status}{}",
                        receipt
                            .reason
                            .as_ref()
                            .map(|reason| format!(" · {reason}"))
                            .unwrap_or_default()
                    ),
                    NativeEntryKind::User,
                )
            }
            _ => continue,
        };
        state.upsert(id, text, kind);
    }
}

async fn restore_history(
    client: &ThreadClient,
    thread_id: &str,
    state: &mut NativeState,
) -> Result<ThreadView> {
    let view = read(client, thread_id).await?;
    restore_history_at(client, thread_id, &view, state).await?;
    update_view(state, &view);
    Ok(view)
}

async fn restore_history_at(
    client: &ThreadClient,
    thread_id: &str,
    view: &ThreadView,
    state: &mut NativeState,
) -> Result<()> {
    let cutoff = view.thread.cursor;
    let mut after = 0;
    loop {
        let ReplyResult::History { page } = client
            .request(Operation::History {
                thread_id: thread_id.into(),
                after,
                cutoff: Some(cutoff),
                limit: 128,
            })
            .await?
        else {
            anyhow::bail!("unexpected history reply");
        };
        anyhow::ensure!(
            page.cutoff == cutoff && page.thread_id == thread_id,
            "history identity changed"
        );
        for event in page.events {
            anyhow::ensure!(
                event.seq > after && event.seq <= cutoff,
                "history sequence changed"
            );
            after = event.seq;
            show_event(state, &event);
        }
        match page.next_after {
            Some(next) => {
                anyhow::ensure!(next == after && next > 0, "history made no progress");
            }
            None => break,
        }
    }
    anyhow::ensure!(after == cutoff, "history lacks its committed cutoff");
    Ok(())
}

fn context_status(
    inspection: &bitrouter_orchestrator::core::context_router::inspection::Inspection,
) -> String {
    let decisions = &inspection.decisions;
    format!(
        "Routing · {} tasks · {} decision calls · {} pending · {} failed",
        inspection.tasks.len(),
        decisions.attempts,
        decisions.pending,
        decisions.failures
    )
}

fn update_snapshot(state: &mut NativeState, snapshot: &TurnSnapshot) {
    if snapshot.status == bitrouter_orchestrator::turn::TurnStatus::Failed
        && let Some(detail) = &snapshot.detail
    {
        state.upsert(
            format!("failure:{}", snapshot.turn_id),
            format!("Turn failed: {detail}"),
            NativeEntryKind::Detail,
        );
    }
    if let Some(inspection) = &snapshot.context_routing {
        state.upsert(
            format!("context:{}", snapshot.turn_id),
            context_status(inspection),
            NativeEntryKind::Detail,
        );
    }
    state.model = snapshot.model.clone();
    state.status = serde_json::to_value(snapshot.status)
        .ok()
        .and_then(|value| value.as_str().map(str::to_string))
        .unwrap_or_else(|| "unknown".into());
    state.verification = serde_json::to_value(snapshot.verification)
        .ok()
        .and_then(|value| value.as_str().map(str::to_string))
        .unwrap_or_else(|| "unknown".into());
    state.pending_input_id = snapshot.pending_input_id.clone();
    state.live = snapshot
        .live
        .as_ref()
        .filter(|live| {
            live.kind == "assistant"
                && !live.text.is_empty()
                && !state
                    .entries
                    .iter()
                    .any(|entry| live.item_id.as_deref() == Some(entry.id.as_str()))
        })
        .map(|live| NativeLive {
            item_id: live.item_id.clone(),
            text: live.text.clone(),
            truncated: live.truncated,
        });
    state.pending_input_detail = snapshot
        .pending_input
        .as_ref()
        .map(|input| format!("Approve {} {}? (y/n)", input.tool_name, input.arguments));
}

fn known_rejection(error: &anyhow::Error) -> bool {
    error.downcast_ref::<ServiceError>().is_some_and(|error| {
        matches!(
            error.code,
            ErrorCode::InvalidRequest
                | ErrorCode::Unauthorized
                | ErrorCode::Conflict
                | ErrorCode::ShuttingDown
        )
    })
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;
    use bitrouter_orchestrator::turn::TurnStatus;
    use crossterm::event::KeyEvent;
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

    #[test]
    fn failed_turn_renders_resource_cause_once_on_events_and_reconnect() -> Result<()> {
        let detail = "checkpoint capacity exhausted; run cleanup required";
        let mut state = NativeState::default();
        let event = ThreadEvent {
            server_instance_id: "instance".into(),
            thread_id: "thread".into(),
            seq: 2,
            timestamp_ms: 0,
            changes: vec![ThreadChange::TurnLifecycle {
                turn_id: "turn".into(),
                lifecycle: TurnLifecycle::Finished {
                    status: TurnStatus::Failed,
                    detail: detail.into(),
                    final_answer: None,
                    verification: bitrouter_orchestrator::turn::VerificationStatus::Unavailable,
                    verification_evidence: None,
                    unknown_effect: false,
                },
            }],
        };
        show_event(&mut state, &event);
        let snapshot = TurnSnapshot {
            context_routing: None,
            resources: None,
            steering: vec![],
            thread_id: "thread".into(),
            server_instance_id: "instance".into(),
            model: "fixture".into(),
            turn_id: "turn".into(),
            status: TurnStatus::Failed,
            cursor: 2,
            workspace: Default::default(),
            tool_mode: Default::default(),
            final_answer: None,
            detail: Some(detail.into()),
            unknown_effect: false,
            verification: bitrouter_orchestrator::turn::VerificationStatus::Unavailable,
            verification_evidence: None,
            pending_input_id: None,
            pending_input: None,
            live: None,
        };
        update_snapshot(&mut state, &snapshot);
        assert_eq!(state.entries.len(), 1);
        assert!(state.entries[0].text.contains(detail));
        let mut reconnected = NativeState::default();
        update_snapshot(&mut reconnected, &snapshot);
        assert_eq!(reconnected.entries[0].text, state.entries[0].text);
        Ok(())
    }

    #[tokio::test]
    async fn busy_enter_enqueues_and_control_enter_targets_active_turn() -> Result<()> {
        let home = tempfile::tempdir()?;
        let socket = home.path().join("controls.sock");
        let listener = tokio::net::UnixListener::bind(&socket)?;
        let server = tokio::spawn(async move {
            for index in 0..3 {
                let (stream, _) = listener.accept().await?;
                let (read, mut write) = stream.into_split();
                let mut line = String::new();
                tokio::io::BufReader::new(read).read_line(&mut line).await?;
                let command: agent_local::ThreadCommand = serde_json::from_str(&line)?;
                let result = match (index, command.operation) {
                    (0, Operation::Capabilities) => ReplyResult::Capabilities {
                        runtime: Box::new(bitrouter_orchestrator::service::RuntimeCapabilities {
                            server_instance_id: "epoch".into(),
                            limits: Default::default(),
                            execution_ownership: None,
                            startup_discovery: None,
                        }),
                        operations: vec![],
                    },
                    (
                        1,
                        Operation::EnqueueTurn {
                            thread_id, prompt, ..
                        },
                    ) => {
                        anyhow::ensure!(thread_id == "thread" && prompt == "follow up");
                        ReplyResult::Receipt {
                            receipt: bitrouter_orchestrator::turn::TurnReceipt {
                                thread_id,
                                turn_id: "queued".into(),
                                queue_order: 2,
                                status: TurnStatus::Accepted,
                            },
                        }
                    }
                    (
                        2,
                        Operation::Steer {
                            thread_id,
                            expected_turn_id,
                            text,
                            ..
                        },
                    ) => {
                        anyhow::ensure!(
                            thread_id == "thread"
                                && expected_turn_id == "active"
                                && text == "adjust course"
                        );
                        ReplyResult::Steering {
                            receipt: bitrouter_orchestrator::turn::SteeringReceipt {
                                input_id: "steering".into(),
                                turn_id: expected_turn_id,
                                order: 1,
                                status: bitrouter_orchestrator::turn::SteeringStatus::Received,
                                context_version: None,
                                next_step_id: None,
                                reason: None,
                            },
                        }
                    }
                    _ => anyhow::bail!("input reached the wrong operation"),
                };
                let reply = agent_local::ThreadReply {
                    version: agent_local::CONTRACT_VERSION,
                    command_id: Some(command.command_id),
                    result,
                };
                write.write_all(&serde_json::to_vec(&reply)?).await?;
                write.write_all(b"\n").await?;
            }
            Ok::<_, anyhow::Error>(())
        });
        let client = ThreadClient::connect(&socket).await?;
        let mut state = NativeState {
            model: "fixture-model".into(),
            status: "busy".into(),
            turn_id: Some("active".into()),
            ..Default::default()
        };
        let mut session = Session {
            thread_id: Some("thread".into()),
            ..Default::default()
        };
        for (text, modifiers) in [
            ("follow up", KeyModifiers::NONE),
            ("adjust course", KeyModifiers::CONTROL),
        ] {
            state.editor.set_text(text);
            let event = Event::Key(KeyEvent::new(KeyCode::Enter, modifiers));
            let mut release = KeyEvent::new(KeyCode::Enter, modifiers);
            release.kind = crossterm::event::KeyEventKind::Release;
            handle_event(
                &client,
                home.path(),
                &None,
                false,
                &mut state,
                &mut session,
                &Event::Key(release),
            )
            .await?;
            assert_eq!(state.editor.text(), text);
            state.viewport = Some((39, 15).into());
            handle_event(
                &client,
                home.path(),
                &None,
                false,
                &mut state,
                &mut session,
                &event,
            )
            .await?;
            assert_eq!(state.editor.text(), text);
            assert!(session.pending.is_none());
            state.viewport = None;
            handle_event(
                &client,
                home.path(),
                &None,
                false,
                &mut state,
                &mut session,
                &event,
            )
            .await?;
            assert!(state.editor.is_empty());
            assert_eq!(state.turn_id.as_deref(), Some("active"));
        }
        server.await??;
        Ok(())
    }

    #[tokio::test]
    async fn ambiguous_overload_preserves_draft_and_original_acceptance_key() -> Result<()> {
        let home = tempfile::tempdir()?;
        let socket = home.path().join("retry.sock");
        let listener = tokio::net::UnixListener::bind(&socket)?;
        let server = tokio::spawn(async move {
            let mut original = None;
            for index in 0..4 {
                let (stream, _) = listener.accept().await?;
                let (read, mut write) = stream.into_split();
                let mut line = String::new();
                tokio::io::BufReader::new(read).read_line(&mut line).await?;
                let command: agent_local::ThreadCommand = serde_json::from_str(&line)?;
                let result = if index == 0 {
                    ReplyResult::Capabilities {
                        runtime: Box::new(bitrouter_orchestrator::service::RuntimeCapabilities {
                            server_instance_id: "epoch".into(),
                            limits: Default::default(),
                            execution_ownership: None,
                            startup_discovery: None,
                        }),
                        operations: vec![],
                    }
                } else {
                    anyhow::ensure!(command.server_instance_id.as_deref() == Some("epoch"));
                    let Operation::StartTurn {
                        thread_id,
                        prompt,
                        idempotency_key,
                    } = command.operation
                    else {
                        anyhow::bail!("unexpected input operation");
                    };
                    anyhow::ensure!(thread_id == "thread" && prompt == "draft y");
                    if index == 1 {
                        original = Some(idempotency_key);
                        ReplyResult::Error {
                            code: ErrorCode::Overloaded,
                            message: "receipt readers full".into(),
                        }
                    } else if index == 2 {
                        anyhow::ensure!(original.as_ref() == Some(&idempotency_key));
                        ReplyResult::Error {
                            code: ErrorCode::Unauthorized,
                            message: "grant temporarily unavailable".into(),
                        }
                    } else {
                        anyhow::ensure!(original.as_ref() == Some(&idempotency_key));
                        ReplyResult::Receipt {
                            receipt: bitrouter_orchestrator::turn::TurnReceipt {
                                thread_id,
                                turn_id: "original-turn".into(),
                                queue_order: 1,
                                status: TurnStatus::Completed,
                            },
                        }
                    }
                };
                let reply = agent_local::ThreadReply {
                    version: agent_local::CONTRACT_VERSION,
                    command_id: Some(command.command_id),
                    result,
                };
                write.write_all(&serde_json::to_vec(&reply)?).await?;
                write.write_all(b"\n").await?;
            }
            Ok::<_, anyhow::Error>(())
        });
        let client = ThreadClient::connect(&socket).await?;
        let mut state = NativeState {
            model: "fixture-model".into(),
            status: "idle".into(),
            ..Default::default()
        };
        let mut session = Session {
            thread_id: Some("thread".into()),
            create_key: "create".into(),
            pending: None,
            ..Default::default()
        };
        for character in "draft y".chars() {
            state.edit(&Event::Key(KeyEvent::new(
                KeyCode::Char(character),
                KeyModifiers::NONE,
            )));
        }
        let submit = Event::Key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        handle_event(
            &client,
            home.path(),
            &None,
            false,
            &mut state,
            &mut session,
            &submit,
        )
        .await?;
        assert_eq!(state.editor.text(), "draft y");
        assert!(session.pending.is_some());
        handle_event(
            &client,
            home.path(),
            &None,
            false,
            &mut state,
            &mut session,
            &submit,
        )
        .await?;
        assert_eq!(state.editor.text(), "draft y");
        assert!(session.pending.is_some());
        handle_event(
            &client,
            home.path(),
            &None,
            false,
            &mut state,
            &mut session,
            &submit,
        )
        .await?;
        assert!(state.editor.is_empty());
        assert!(session.pending.is_none());
        server.await??;
        Ok(())
    }
}