blyte 0.64.0

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
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
use std::collections::{HashMap, VecDeque};
use std::fs::{File, OpenOptions};
use std::io::{ErrorKind, Write};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

pub const RESTART_NOTICE_TEXT: &str =
    "blyte restarted: the entries above are from before; the model does not remember them";

static SESSION_COUNTER: AtomicU64 = AtomicU64::new(0);

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Entry {
    pub seq: u64,
    pub ts: u64,
    #[serde(flatten)]
    pub body: EntryBody,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum EntryBody {
    UserMessage {
        text: String,
        reports: Vec<String>,
        queued: bool,
    },
    Turn {
        n: u32,
        text: String,
        input_tokens: u64,
        output_tokens: u64,
        cost_usd: Option<f64>,
    },
    ToolStarted {
        name: String,
        input: String,
    },
    ToolFinished {
        name: String,
        summary: String,
        is_error: bool,
    },
    Retry {
        attempt: u32,
        max: u32,
        wait_ms: u64,
        reason: String,
    },
    Error {
        message: String,
    },
    Idle {
        turns: u32,
        cost_usd: Option<f64>,
    },
    ModelChanged {
        model: String,
    },
    EffortChanged {
        effort: Option<String>,
    },
    Decision {
        run: String,
        action: String,
        note: Option<String>,
    },
    ApprovalRequested {
        id: String,
        command: String,
        reason: String,
    },
    ApprovalAnswered {
        id: String,
        command: String,
        answer: String,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ApprovalAnswer {
    Yes,
    No,
}

impl ApprovalAnswer {
    pub fn as_str(self) -> &'static str {
        match self {
            ApprovalAnswer::Yes => "yes",
            ApprovalAnswer::No => "no",
        }
    }

    fn to_approval(self) -> blyte_agent::approval::Approval {
        match self {
            ApprovalAnswer::Yes => blyte_agent::approval::Approval::Approved,
            ApprovalAnswer::No => blyte_agent::approval::Approval::Denied,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AnswerError {
    NotFound,
    AlreadyAnswered,
}

pub struct SurfaceApprover {
    shared: Arc<Shared>,
}

impl blyte_agent::approval::Approver for SurfaceApprover {
    fn ask(&self, command: &str, reason: &str) -> blyte_agent::approval::Approval {
        let mut state = self.shared.lock_state();
        if state.closed {
            return blyte_agent::approval::Approval::Denied;
        }
        state.next_approval_id += 1;
        let order = state.next_approval_id;
        let id = format!("approval-{order}");
        state.approvals.insert(
            id.clone(),
            ApprovalRecord {
                order,
                command: command.to_string(),
                answer: None,
            },
        );
        state.push_entry(EntryBody::ApprovalRequested {
            id: id.clone(),
            command: command.to_string(),
            reason: reason.to_string(),
        });
        self.shared.log_cv.notify_all();

        loop {
            if let Some(answer) = state.approvals.get(&id).and_then(|r| r.answer) {
                return answer.to_approval();
            }
            if state.closed {
                return blyte_agent::approval::Approval::Denied;
            }
            state = self
                .shared
                .approval_cv
                .wait(state)
                .unwrap_or_else(|e| e.into_inner());
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Attachment {
    pub run: String,
    pub text: String,
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct SessionInfo {
    pub model: String,
    pub effort: Option<String>,
    pub effort_supported: bool,
    pub busy: bool,
    pub queued: usize,
    pub last_seq: u64,
}

pub trait PromptRunner: Send + 'static {
    fn run(
        &mut self,
        prompt: &str,
        model: &str,
        emit: &(dyn Fn(EntryBody) + Sync),
    ) -> Result<(), String>;
}

pub struct AgentRunner {
    repl: crate::repl::ReplSession,
    approver: Arc<dyn blyte_agent::approval::Approver>,
}

impl AgentRunner {
    pub fn new(
        config: &crate::repl::ReplConfig,
        approver: Arc<dyn blyte_agent::approval::Approver>,
    ) -> Self {
        let mut cfg = config.clone();
        cfg.converse = true;
        let repl = crate::repl::ReplSession::try_new_with_approver(&cfg, Some(approver.clone()))
            .expect("failed to initialize REPL session");
        Self { repl, approver }
    }
}

impl PromptRunner for AgentRunner {
    fn run(
        &mut self,
        prompt: &str,
        model: &str,
        emit: &(dyn Fn(EntryBody) + Sync),
    ) -> Result<(), String> {
        self.repl.model = model.to_string();
        self.repl.state.brief.goal = prompt.to_string();
        if let Some(ref gate) = self.repl.gate {
            self.repl.state.brief.verification_cmd = gate.clone();
        }
        let task_config = self.repl.task_config_for_prompt(prompt);
        let pricing = blyte_provider::pricing(model);
        let on_event = |event: &blyte_agent::run::Event<'_>| match event {
            blyte_agent::run::Event::Turn { n, text, usage } => {
                emit(EntryBody::Turn {
                    n: *n,
                    text: (*text).to_string(),
                    input_tokens: usage.input + usage.cached,
                    output_tokens: usage.output + usage.thinking,
                    cost_usd: pricing.map(|p| p.cost(usage)),
                });
            }
            blyte_agent::run::Event::ToolStart { name, input } => {
                emit(EntryBody::ToolStarted {
                    name: (*name).to_string(),
                    input: input.clone(),
                });
            }
            blyte_agent::run::Event::Tool {
                name,
                summary,
                is_error,
                ..
            } => {
                emit(EntryBody::ToolFinished {
                    name: (*name).to_string(),
                    summary: summary.clone(),
                    is_error: *is_error,
                });
            }
            blyte_agent::run::Event::Retry {
                attempt,
                max,
                wait_ms,
                reason,
            } => {
                emit(EntryBody::Retry {
                    attempt: *attempt,
                    max: *max,
                    wait_ms: *wait_ms,
                    reason: reason.clone(),
                });
            }
            _ => {}
        };
        let (outcome, _) = crate::task_runner::run_task_with_approver(
            &task_config,
            &mut self.repl.state,
            Some(self.approver.clone()),
            &on_event,
        )?;
        match outcome {
            blyte_agent::run::Outcome::Verified | blyte_agent::run::Outcome::Finished => Ok(()),
            blyte_agent::run::Outcome::BudgetExhausted => Err("budget exhausted".to_string()),
            blyte_agent::run::Outcome::GateFailed => Err("gate failed".to_string()),
            blyte_agent::run::Outcome::Blocked(msg) | blyte_agent::run::Outcome::Error(msg) => {
                Err(msg)
            }
        }
    }
}

struct ApprovalRecord {
    order: u64,
    command: String,
    answer: Option<ApprovalAnswer>,
}

struct QueuedMessage {
    request_id: String,
    prompt: String,
}

struct EntryLog {
    path: PathBuf,
    file: File,
    fail_writes_with: Option<String>,
}

impl EntryLog {
    fn open(path: PathBuf, err: &mut dyn Write) -> Result<(Self, Vec<Entry>), String> {
        let (entries, truncate_to, needs_newline) = load_entries_file(&path, err)?;
        if let Some(parent) = path.parent() {
            if !parent.as_os_str().is_empty() {
                std::fs::create_dir_all(parent)
                    .map_err(|e| format!("failed to create directory {}: {e}", parent.display()))?;
            }
        }
        if let Some(len) = truncate_to {
            let f = OpenOptions::new()
                .write(true)
                .open(&path)
                .map_err(|e| format!("failed to truncate {}: {e}", path.display()))?;
            f.set_len(len)
                .map_err(|e| format!("failed to truncate {}: {e}", path.display()))?;
        }
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .map_err(|e| format!("failed to open {}: {e}", path.display()))?;
        if needs_newline {
            file.write_all(b"\n")
                .and_then(|_| file.flush())
                .map_err(|e| format!("failed to write to {}: {e}", path.display()))?;
        }
        Ok((
            Self {
                path,
                file,
                fail_writes_with: None,
            },
            entries,
        ))
    }

    fn append(&mut self, entry: &Entry) -> Result<(), String> {
        let mut line = match serde_json::to_string(entry) {
            Ok(s) => s,
            Err(e) => {
                eprintln!(
                    "blyte: failed to serialize entry for {}: {e}",
                    self.path.display()
                );
                return Err(e.to_string());
            }
        };
        line.push('\n');
        let write_result = match &self.fail_writes_with {
            Some(reason) => Err(std::io::Error::other(reason.clone())),
            None => self
                .file
                .write_all(line.as_bytes())
                .and_then(|_| self.file.flush()),
        };
        if let Err(e) = write_result {
            eprintln!("blyte: failed to write to {}: {e}", self.path.display());
            return Err(e.to_string());
        }
        Ok(())
    }
}

fn gap_notice_message(missing: u64) -> String {
    if missing == 1 {
        "blyte: 1 entry was not saved here".to_string()
    } else {
        format!("blyte: {missing} entries were not saved here")
    }
}

fn load_entries_file(
    path: &Path,
    err: &mut dyn Write,
) -> Result<(Vec<Entry>, Option<u64>, bool), String> {
    let bytes = match std::fs::read(path) {
        Ok(b) => b,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok((Vec::new(), None, false)),
        Err(e) => return Err(format!("failed to read {}: {e}", path.display())),
    };
    if bytes.is_empty() {
        return Ok((Vec::new(), None, false));
    }

    let mut entries = Vec::new();
    let mut prev_seq = 0u64;
    let mut pos = 0usize;
    let mut line_no = 0usize;
    let mut truncate_to = None;
    let mut valid_end = 0usize;

    while pos < bytes.len() {
        line_no += 1;
        let start = pos;
        let end = match bytes[pos..].iter().position(|&b| b == b'\n') {
            Some(rel) => pos + rel + 1,
            None => bytes.len(),
        };
        // A cut-off write never ends in a newline; a complete bad line is corruption.
        let is_last = end == bytes.len() && !bytes[start..end].ends_with(b"\n");
        let line_slice = &bytes[start..end];
        let parsed = std::str::from_utf8(line_slice)
            .map_err(|e| e.to_string())
            .and_then(|s| serde_json::from_str::<Entry>(s).map_err(|e| e.to_string()));
        match parsed {
            Ok(entry) => {
                if entry.seq <= prev_seq {
                    return Err(format!(
                        "invalid entry in {} at line {}: seq {} does not follow seq {}",
                        path.display(),
                        line_no,
                        entry.seq,
                        prev_seq
                    ));
                }
                if entry.seq > prev_seq + 1 {
                    let missing = entry.seq - prev_seq - 1;
                    entries.push(Entry {
                        seq: prev_seq + 1,
                        ts: entry.ts,
                        body: EntryBody::Error {
                            message: gap_notice_message(missing),
                        },
                    });
                }
                prev_seq = entry.seq;
                entries.push(entry);
                valid_end = end;
            }
            Err(reason) => {
                if is_last {
                    let _ = writeln!(
                        err,
                        "blyte: dropping truncated last line in {} at line {}: {}",
                        path.display(),
                        line_no,
                        reason
                    );
                    let _ = err.flush();
                    truncate_to = Some(start as u64);
                } else {
                    return Err(format!(
                        "invalid entry in {} at line {}: {}",
                        path.display(),
                        line_no,
                        reason
                    ));
                }
            }
        }
        pos = end;
    }

    let needs_newline = valid_end > 0 && bytes[valid_end - 1] != b'\n';
    Ok((entries, truncate_to, needs_newline))
}

fn max_approval_id(entries: &[Entry]) -> u64 {
    entries
        .iter()
        .filter_map(|e| match &e.body {
            EntryBody::ApprovalRequested { id, .. } | EntryBody::ApprovalAnswered { id, .. } => {
                id.strip_prefix("approval-")?.parse::<u64>().ok()
            }
            _ => None,
        })
        .max()
        .unwrap_or(0)
}

struct SessionState {
    entries: Vec<Entry>,
    last_seq: u64,
    log: Option<EntryLog>,
    write_failure_noticed: bool,
    queue: VecDeque<QueuedMessage>,
    session_prefix: String,
    request_seq: u64,
    model: String,
    effort: Option<String>,
    busy: bool,
    closed: bool,
    approvals: HashMap<String, ApprovalRecord>,
    next_approval_id: u64,
}

impl SessionState {
    fn push_entry(&mut self, body: EntryBody) -> u64 {
        self.last_seq += 1;
        let seq = self.last_seq;
        let ts = now_secs();
        let entry = Entry { seq, ts, body };
        let append_failure = self
            .log
            .as_mut()
            .and_then(|log| log.append(&entry).err().map(|err| (log.path.clone(), err)));
        self.entries.push(entry);
        if let Some((path, err)) = append_failure {
            if !self.write_failure_noticed {
                self.write_failure_noticed = true;
                self.push_entry(EntryBody::Error {
                    message: format!(
                        "blyte: could not save this session's entries to {}: {}; entries from here on may be missing after a restart",
                        path.display(),
                        err
                    ),
                });
            }
        }
        seq
    }

    fn next_request_id(&mut self) -> String {
        self.request_seq += 1;
        format!("{}-{}", self.session_prefix, self.request_seq)
    }

    fn effective_model(&self) -> String {
        match &self.effort {
            Some(effort) if model_supports_effort(&self.model) => {
                format!("{}-{}", self.model, effort)
            }
            _ => self.model.clone(),
        }
    }

    fn deny_all_pending(&mut self) -> bool {
        let mut pending: Vec<(u64, String, String)> = self
            .approvals
            .iter()
            .filter(|(_, record)| record.answer.is_none())
            .map(|(id, record)| (record.order, id.clone(), record.command.clone()))
            .collect();
        if pending.is_empty() {
            return false;
        }
        pending.sort_by_key(|(order, _, _)| *order);
        for (_, id, command) in pending {
            if let Some(record) = self.approvals.get_mut(&id) {
                record.answer = Some(ApprovalAnswer::No);
            }
            self.push_entry(EntryBody::ApprovalAnswered {
                id,
                command,
                answer: ApprovalAnswer::No.as_str().to_string(),
            });
        }
        true
    }
}

struct Shared {
    state: Mutex<SessionState>,
    log_cv: Condvar,
    work_cv: Condvar,
    approval_cv: Condvar,
}

impl Shared {
    fn new(model: String, effort: Option<String>) -> Self {
        Self {
            state: Mutex::new(SessionState {
                entries: Vec::new(),
                last_seq: 0,
                log: None,
                write_failure_noticed: false,
                queue: VecDeque::new(),
                session_prefix: new_session_prefix(),
                request_seq: 0,
                model,
                effort,
                busy: false,
                closed: false,
                approvals: HashMap::new(),
                next_approval_id: 0,
            }),
            log_cv: Condvar::new(),
            work_cv: Condvar::new(),
            approval_cv: Condvar::new(),
        }
    }

    fn new_with_log(
        model: String,
        effort: Option<String>,
        log_path: Option<PathBuf>,
        err: &mut dyn Write,
    ) -> Result<Self, String> {
        let (log, entries) = match log_path {
            Some(path) => {
                let (log, entries) = EntryLog::open(path, err)?;
                (Some(log), entries)
            }
            None => (None, Vec::new()),
        };
        let last_seq = entries.iter().map(|e| e.seq).max().unwrap_or(0);
        let next_approval_id = max_approval_id(&entries);
        let had_entries = !entries.is_empty();
        let mut state = SessionState {
            entries,
            last_seq,
            log,
            write_failure_noticed: false,
            queue: VecDeque::new(),
            session_prefix: new_session_prefix(),
            request_seq: 0,
            model,
            effort,
            busy: false,
            closed: false,
            approvals: HashMap::new(),
            next_approval_id,
        };
        if had_entries {
            state.push_entry(EntryBody::Error {
                message: RESTART_NOTICE_TEXT.to_string(),
            });
        }
        Ok(Self {
            state: Mutex::new(state),
            log_cv: Condvar::new(),
            work_cv: Condvar::new(),
            approval_cv: Condvar::new(),
        })
    }

    fn lock_state(&self) -> MutexGuard<'_, SessionState> {
        self.state.lock().unwrap_or_else(|e| e.into_inner())
    }
}

struct SessionGuard {
    shared: Arc<Shared>,
}

impl Drop for SessionGuard {
    fn drop(&mut self) {
        let mut state = self.shared.lock_state();
        state.closed = true;
        state.deny_all_pending();
        self.shared.work_cv.notify_all();
        self.shared.log_cv.notify_all();
        self.shared.approval_cv.notify_all();
    }
}

#[derive(Clone)]
pub struct Session {
    guard: Arc<SessionGuard>,
}

struct TurnTotals {
    turns: u32,
    cost_usd: Option<f64>,
}

impl Session {
    pub fn start(runner: Box<dyn PromptRunner>, model: String) -> Session {
        Self::start_with(model, |_| runner)
    }

    pub fn start_with<F>(model: String, build_runner: F) -> Session
    where
        F: FnOnce(Arc<SurfaceApprover>) -> Box<dyn PromptRunner>,
    {
        let (base_model, effort) = parse_model_and_effort(&model);
        let shared = Arc::new(Shared::new(base_model, effort));
        let approver = Arc::new(SurfaceApprover {
            shared: Arc::clone(&shared),
        });
        let runner = build_runner(approver);
        let worker_shared = Arc::clone(&shared);
        std::thread::spawn(move || worker_loop(runner, worker_shared));
        Session {
            guard: Arc::new(SessionGuard { shared }),
        }
    }

    pub fn start_with_log<F>(
        model: String,
        log_path: Option<PathBuf>,
        build_runner: F,
    ) -> Result<Session, String>
    where
        F: FnOnce(Arc<SurfaceApprover>) -> Box<dyn PromptRunner>,
    {
        let mut stderr = std::io::stderr();
        Self::start_with_log_io(model, log_path, &mut stderr, build_runner)
    }

    pub fn start_with_log_io<F>(
        model: String,
        log_path: Option<PathBuf>,
        err: &mut dyn Write,
        build_runner: F,
    ) -> Result<Session, String>
    where
        F: FnOnce(Arc<SurfaceApprover>) -> Box<dyn PromptRunner>,
    {
        let (base_model, effort) = parse_model_and_effort(&model);
        let shared = Arc::new(Shared::new_with_log(base_model, effort, log_path, err)?);
        let approver = Arc::new(SurfaceApprover {
            shared: Arc::clone(&shared),
        });
        let runner = build_runner(approver);
        let worker_shared = Arc::clone(&shared);
        std::thread::spawn(move || worker_loop(runner, worker_shared));
        Ok(Session {
            guard: Arc::new(SessionGuard { shared }),
        })
    }

    #[doc(hidden)]
    pub fn start_paused(
        runner: Box<dyn PromptRunner>,
        model: String,
    ) -> (Session, std::sync::mpsc::Sender<()>) {
        let (base_model, effort) = parse_model_and_effort(&model);
        let shared = Arc::new(Shared::new(base_model, effort));
        let (tx, rx) = std::sync::mpsc::channel();
        let worker_shared = Arc::clone(&shared);
        std::thread::spawn(move || {
            if rx.recv().is_ok() {
                worker_loop(runner, worker_shared);
            }
        });
        (
            Session {
                guard: Arc::new(SessionGuard { shared }),
            },
            tx,
        )
    }

    #[doc(hidden)]
    pub fn fail_log_writes(&self, reason: &str) {
        let mut state = self.guard.shared.lock_state();
        if let Some(log) = &mut state.log {
            log.fail_writes_with = Some(reason.to_string());
        }
    }

    pub fn approver(&self) -> Arc<SurfaceApprover> {
        Arc::new(SurfaceApprover {
            shared: Arc::clone(&self.guard.shared),
        })
    }

    pub fn answer_approval(&self, id: &str, answer: ApprovalAnswer) -> Result<u64, AnswerError> {
        let mut state = self.guard.shared.lock_state();
        let command = match state.approvals.get_mut(id) {
            None => return Err(AnswerError::NotFound),
            Some(record) if record.answer.is_some() => return Err(AnswerError::AlreadyAnswered),
            Some(record) => {
                record.answer = Some(answer);
                record.command.clone()
            }
        };
        let seq = state.push_entry(EntryBody::ApprovalAnswered {
            id: id.to_string(),
            command,
            answer: answer.as_str().to_string(),
        });
        self.guard.shared.approval_cv.notify_all();
        self.guard.shared.log_cv.notify_all();
        Ok(seq)
    }

    pub fn stop_run(&self) {
        let mut state = self.guard.shared.lock_state();
        if state.deny_all_pending() {
            self.guard.shared.approval_cv.notify_all();
            self.guard.shared.log_cv.notify_all();
        }
    }

    pub fn submit(&self, text: &str, attachments: Vec<Attachment>) -> Result<u64, String> {
        if text.trim().is_empty() {
            return Err("a message needs text".to_string());
        }
        let reports = attachments.iter().map(|a| a.run.clone()).collect();
        let full_prompt = format_prompt(text, &attachments);
        let mut state = self.guard.shared.lock_state();
        let queued = state.busy || !state.queue.is_empty();
        let seq = state.push_entry(EntryBody::UserMessage {
            text: text.to_string(),
            reports,
            queued,
        });
        let request_id = state.next_request_id();
        state.queue.push_back(QueuedMessage {
            request_id,
            prompt: full_prompt,
        });
        self.guard.shared.work_cv.notify_one();
        self.guard.shared.log_cv.notify_all();
        Ok(seq)
    }

    pub fn entries_after(&self, after: u64, wait: Duration) -> Vec<Entry> {
        let mut state = self.guard.shared.lock_state();
        if state.last_seq <= after && !wait.is_zero() {
            let deadline = Instant::now() + wait;
            while state.last_seq <= after {
                let remaining = deadline.saturating_duration_since(Instant::now());
                if remaining.is_zero() {
                    break;
                }
                let (guard, _) = self
                    .guard
                    .shared
                    .log_cv
                    .wait_timeout(state, remaining)
                    .unwrap_or_else(|e| e.into_inner());
                state = guard;
            }
        }
        state
            .entries
            .iter()
            .filter(|e| e.seq > after)
            .cloned()
            .collect()
    }

    pub fn info(&self) -> SessionInfo {
        let state = self.guard.shared.lock_state();
        SessionInfo {
            effort_supported: model_supports_effort(&state.model),
            model: state.model.clone(),
            effort: state.effort.clone(),
            busy: state.busy,
            queued: state.queue.len(),
            last_seq: state.last_seq,
        }
    }

    pub fn set_model(&self, model: &str) -> Result<(), String> {
        let trimmed = model.trim();
        if trimmed.is_empty() {
            return Err("model cannot be empty".to_string());
        }
        let mut state = self.guard.shared.lock_state();
        let supported = model_supports_effort(trimmed);
        if supported {
            let (base, parsed_effort) = blyte_provider::gemini::split_effort(trimmed);
            state.model = base.clone();
            state.push_entry(EntryBody::ModelChanged { model: base });
            if let Some(new_effort) = parsed_effort {
                if state.effort.as_deref() != Some(new_effort.as_str()) {
                    state.effort = Some(new_effort.clone());
                    state.push_entry(EntryBody::EffortChanged {
                        effort: Some(new_effort),
                    });
                }
            }
        } else {
            state.model = trimmed.to_string();
            state.push_entry(EntryBody::ModelChanged {
                model: trimmed.to_string(),
            });
            if state.effort.is_some() {
                state.effort = None;
                state.push_entry(EntryBody::EffortChanged { effort: None });
            }
        }
        self.guard.shared.log_cv.notify_all();
        Ok(())
    }

    pub fn set_effort(&self, effort: Option<&str>) -> Result<(), String> {
        let normalized = match effort {
            None => None,
            Some("low") => Some("low".to_string()),
            Some("medium") => Some("medium".to_string()),
            Some("high") => Some("high".to_string()),
            Some(_) => return Err("effort must be low, medium or high".to_string()),
        };
        let mut state = self.guard.shared.lock_state();
        if !model_supports_effort(&state.model) {
            return Err(format!("{} does not take an effort setting", state.model));
        }
        state.effort = normalized.clone();
        state.push_entry(EntryBody::EffortChanged { effort: normalized });
        self.guard.shared.log_cv.notify_all();
        Ok(())
    }

    pub fn record_decision(
        &self,
        run: &str,
        action: &str,
        note: Option<&str>,
        conversation_message: Option<&str>,
    ) -> Result<u64, String> {
        if let Some(msg) = conversation_message {
            if msg.trim().is_empty() {
                return Err("a message needs text".to_string());
            }
        }
        let mut state = self.guard.shared.lock_state();
        let seq = state.push_entry(EntryBody::Decision {
            run: run.to_string(),
            action: action.to_string(),
            note: note.map(str::to_string),
        });
        if let Some(msg) = conversation_message {
            let queued = state.busy || !state.queue.is_empty();
            state.push_entry(EntryBody::UserMessage {
                text: msg.to_string(),
                reports: Vec::new(),
                queued,
            });
            let request_id = state.next_request_id();
            state.queue.push_back(QueuedMessage {
                request_id,
                prompt: msg.to_string(),
            });
            self.guard.shared.work_cv.notify_one();
        }
        self.guard.shared.log_cv.notify_all();
        Ok(seq)
    }

    pub fn queue_for_request(&self, request_id: &str, text: &str) -> Result<u64, String> {
        if text.trim().is_empty() {
            return Err("a message needs text".to_string());
        }
        let mut state = self.guard.shared.lock_state();
        let queued = state.busy || !state.queue.is_empty();
        let seq = state.push_entry(EntryBody::UserMessage {
            text: text.to_string(),
            reports: Vec::new(),
            queued,
        });
        state.queue.push_back(QueuedMessage {
            request_id: request_id.to_string(),
            prompt: text.to_string(),
        });
        self.guard.shared.work_cv.notify_one();
        self.guard.shared.log_cv.notify_all();
        Ok(seq)
    }

    pub fn record_error(&self, message: &str) -> u64 {
        let mut state = self.guard.shared.lock_state();
        let seq = state.push_entry(EntryBody::Error {
            message: message.to_string(),
        });
        self.guard.shared.log_cv.notify_all();
        seq
    }
}

struct RequestGuard;

impl RequestGuard {
    fn enter(request_id: String) -> Self {
        blyte_agent::tools::set_blyte_request(Some(request_id));
        Self
    }
}

impl Drop for RequestGuard {
    fn drop(&mut self) {
        blyte_agent::tools::set_blyte_request(None);
    }
}

fn worker_loop(mut runner: Box<dyn PromptRunner>, shared: Arc<Shared>) {
    loop {
        let (msg, effective_model) = {
            let mut state = shared.lock_state();
            loop {
                if state.closed {
                    return;
                }
                if let Some(msg) = state.queue.pop_front() {
                    state.busy = true;
                    let model = state.effective_model();
                    break (msg, model);
                }
                state = shared
                    .work_cv
                    .wait(state)
                    .unwrap_or_else(|e| e.into_inner());
            }
        };

        let totals = Mutex::new(TurnTotals {
            turns: 0,
            cost_usd: Some(0.0),
        });
        let emit_shared = Arc::clone(&shared);
        let emit = |body: EntryBody| {
            if let EntryBody::Turn { cost_usd, .. } = &body {
                let mut t = totals.lock().unwrap_or_else(|e| e.into_inner());
                t.turns += 1;
                match (t.cost_usd, cost_usd) {
                    (Some(acc), Some(c)) => t.cost_usd = Some(acc + c),
                    _ => t.cost_usd = None,
                }
            }
            let mut state = emit_shared.lock_state();
            state.push_entry(body);
            emit_shared.log_cv.notify_all();
        };

        let outcome = catch_unwind(AssertUnwindSafe(|| {
            let _req_guard = RequestGuard::enter(msg.request_id);
            runner.run(&msg.prompt, &effective_model, &emit)
        }));

        let (turns, cost_usd) = {
            let t = totals.lock().unwrap_or_else(|e| e.into_inner());
            (t.turns, t.cost_usd)
        };

        let mut state = shared.lock_state();
        match outcome {
            Ok(Ok(())) => {}
            Ok(Err(message)) => {
                state.push_entry(EntryBody::Error { message });
            }
            Err(payload) => {
                let message = panic_message(payload);
                state.push_entry(EntryBody::Error { message });
            }
        }
        state.push_entry(EntryBody::Idle { turns, cost_usd });
        state.busy = false;
        shared.log_cv.notify_all();
    }
}

fn format_prompt(text: &str, attachments: &[Attachment]) -> String {
    let mut out = text.to_string();
    for att in attachments {
        out.push_str("\n\nResearch report ");
        out.push_str(&att.run);
        out.push_str(":\n");
        out.push_str(&att.text);
    }
    out
}

pub(crate) fn model_supports_effort(model: &str) -> bool {
    blyte_provider::detect_provider_kind(Some(model)) == blyte_provider::ProviderKind::Gemini
}

fn parse_model_and_effort(model: &str) -> (String, Option<String>) {
    let trimmed = model.trim();
    if model_supports_effort(trimmed) {
        blyte_provider::gemini::split_effort(trimmed)
    } else {
        (trimmed.to_string(), None)
    }
}

fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String {
    if let Some(s) = payload.downcast_ref::<&str>() {
        (*s).to_string()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "runner panicked".to_string()
    }
}

fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn now_millis() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0)
}

fn new_session_prefix() -> String {
    let seq = SESSION_COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("r{}-{}-{}", now_millis(), std::process::id(), seq)
}