blyte 0.77.1

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
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use blyte_provider::Message;
use serde::Serialize;

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

#[derive(Debug, Clone, PartialEq)]
pub struct LoadedConversation {
    pub model: Option<String>,
    pub goal: Option<String>,
    pub messages: Vec<Message>,
}

pub struct ConversationStore {
    path: PathBuf,
    last_model: Option<String>,
    last_goal: Option<String>,
    saved_messages: usize,
    file_initialized: bool,
}

#[derive(Serialize)]
struct VersionLine {
    version: u64,
}

#[derive(Serialize)]
struct ModelLine<'a> {
    model: &'a str,
}

#[derive(Serialize)]
struct GoalLine<'a> {
    goal: Option<&'a str>,
}

#[derive(Serialize)]
struct MessageLine<'a> {
    message: &'a Message,
}

/// Returns `<surface token dir>/<surface id>.conversation.jsonl`.
pub fn conversation_path_for_id(surface_id: &str) -> Result<PathBuf, String> {
    blyte_agent::sandbox::validate_surface_id(surface_id)?;
    let dir = blyte_agent::sandbox::surface_token_dir().ok_or_else(|| {
        "cannot determine surface token directory: neither XDG_STATE_HOME nor HOME is set"
            .to_string()
    })?;
    Ok(dir.join(format!("{surface_id}.conversation.jsonl")))
}

fn format_read_error(path: &Path, line_no: usize, reason: &str) -> String {
    format!(
        "cannot read the saved conversation {} (line {line_no}): {reason}; delete that file to resume without the model's memory",
        path.display()
    )
}

fn push_json_line<T: Serialize>(buf: &mut Vec<u8>, value: &T) -> Result<(), String> {
    serde_json::to_writer(&mut *buf, value)
        .map_err(|e| format!("failed to serialize conversation line: {e}"))?;
    buf.push(b'\n');
    Ok(())
}

fn encode_full_file(
    model: Option<&str>,
    goal: Option<&str>,
    messages: &[Message],
) -> Result<Vec<u8>, String> {
    let mut buf = Vec::new();
    push_json_line(&mut buf, &VersionLine { version: 1 })?;
    if let Some(m) = model {
        push_json_line(&mut buf, &ModelLine { model: m })?;
    }
    if goal.is_some() {
        push_json_line(&mut buf, &GoalLine { goal })?;
    }
    for message in messages {
        push_json_line(&mut buf, &MessageLine { message })?;
    }
    Ok(buf)
}

fn ensure_parent_dir(path: &Path) -> Result<(), String> {
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("cannot create {}: {e}", parent.display()))?;
        }
    }
    Ok(())
}

#[cfg(unix)]
fn set_file_mode_0600(path: &Path) -> Result<(), String> {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
        .map_err(|e| format!("cannot set permissions on {}: {e}", path.display()))
}

#[cfg(not(unix))]
fn set_file_mode_0600(_path: &Path) -> Result<(), String> {
    Ok(())
}

fn open_append_0600(path: &Path) -> Result<std::fs::File, String> {
    let mut opts = OpenOptions::new();
    opts.create(true).append(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    opts.open(path)
        .map_err(|e| format!("cannot open {}: {e}", path.display()))
}

fn rewrite_atomic(
    path: &Path,
    model: Option<&str>,
    goal: Option<&str>,
    messages: &[Message],
) -> Result<(), String> {
    let parent = path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .ok_or_else(|| {
            format!(
                "cannot rewrite conversation file {}: path has no parent directory",
                path.display()
            )
        })?;
    let file_name = path.file_name().ok_or_else(|| {
        format!(
            "cannot rewrite conversation file {}: path has no file name",
            path.display()
        )
    })?;
    let base_name = file_name.to_str().ok_or_else(|| {
        format!(
            "cannot rewrite conversation file {}: file name is not valid UTF-8",
            path.display()
        )
    })?;
    ensure_parent_dir(path)?;
    let buf = encode_full_file(model, goal, messages)?;
    let pid = std::process::id();
    let mut attempt = 0u64;
    let (tmp_path, mut file) = loop {
        let seq = TMP_SEQ.fetch_add(1, Ordering::Relaxed);
        let candidate = parent.join(format!(".{base_name}.tmp.{pid}.{seq}.{attempt}"));
        let mut opts = OpenOptions::new();
        opts.write(true).create_new(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            opts.mode(0o600);
        }
        match opts.open(&candidate) {
            Ok(f) => break (candidate, f),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                attempt += 1;
            }
            Err(e) => {
                return Err(format!("cannot create {}: {e}", candidate.display()));
            }
        }
    };
    let write_res = file
        .write_all(&buf)
        .and_then(|()| file.flush())
        .and_then(|()| file.sync_all());
    drop(file);
    if let Err(e) = write_res {
        let _ = std::fs::remove_file(&tmp_path);
        return Err(format!("cannot write {}: {e}", tmp_path.display()));
    }
    if let Err(e) = set_file_mode_0600(&tmp_path) {
        let _ = std::fs::remove_file(&tmp_path);
        return Err(e);
    }
    if let Err(e) = std::fs::rename(&tmp_path, path) {
        let _ = std::fs::remove_file(&tmp_path);
        return Err(format!(
            "cannot rename {} to {}: {e}",
            tmp_path.display(),
            path.display()
        ));
    }
    Ok(())
}

fn strip_trailing_unmatched_tool_calls(messages: &mut Vec<Message>) -> bool {
    let mut changed = false;
    loop {
        let results_start = messages
            .iter()
            .rposition(|m| !matches!(m, Message::ToolResult { .. }))
            .map(|i| i + 1)
            .unwrap_or(0);
        let calls_start = messages[..results_start]
            .iter()
            .rposition(|m| !matches!(m, Message::ToolCall { .. }))
            .map(|i| i + 1)
            .unwrap_or(0);
        let num_calls = results_start - calls_start;
        let num_results = messages.len() - results_start;
        if num_calls > num_results {
            messages.drain(calls_start + num_results..results_start);
            changed = true;
        } else {
            break;
        }
    }
    changed
}

fn parse_version_line(path: &Path, line: &str) -> Result<(), String> {
    let val: serde_json::Value =
        serde_json::from_str(line).map_err(|e| format_read_error(path, 1, &e.to_string()))?;
    let Some(map) = val.as_object() else {
        return Err(format_read_error(
            path,
            1,
            "first line must be {\"version\":1}",
        ));
    };
    if map.len() != 1 {
        if let Some(unknown) = map.keys().find(|k| k.as_str() != "version") {
            return Err(format_read_error(
                path,
                1,
                &format!("unknown key \"{unknown}\""),
            ));
        }
        return Err(format_read_error(
            path,
            1,
            "first line must be {\"version\":1}",
        ));
    }
    match map.get("version") {
        Some(v) if v.as_u64() == Some(1) => Ok(()),
        Some(v) => Err(format_read_error(
            path,
            1,
            &format!("unsupported version {v}; expected 1"),
        )),
        None => {
            if let Some(unknown) = map.keys().next() {
                Err(format_read_error(
                    path,
                    1,
                    &format!("unknown key \"{unknown}\""),
                ))
            } else {
                Err(format_read_error(
                    path,
                    1,
                    "first line must be {\"version\":1}",
                ))
            }
        }
    }
}

impl ConversationStore {
    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn new_for_fresh(path: PathBuf) -> Result<Self, String> {
        match std::fs::symlink_metadata(&path) {
            Ok(_) => Err(format!(
                "conversation file already exists for new surface: {}",
                path.display()
            )),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self {
                path,
                last_model: None,
                last_goal: None,
                saved_messages: 0,
                file_initialized: false,
            }),
            Err(e) => Err(format!("cannot inspect {}: {e}", path.display())),
        }
    }

    pub fn load(path: PathBuf) -> Result<(Self, Option<LoadedConversation>), String> {
        let raw = match std::fs::read(&path) {
            Ok(bytes) => bytes,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                return Ok((
                    Self {
                        path,
                        last_model: None,
                        last_goal: None,
                        saved_messages: 0,
                        file_initialized: false,
                    },
                    None,
                ));
            }
            Err(e) => return Err(format_read_error(&path, 1, &e.to_string())),
        };

        let (complete_bytes, truncate_to) = if !raw.is_empty() && !raw.ends_with(b"\n") {
            let cut = raw
                .iter()
                .rposition(|&b| b == b'\n')
                .map(|i| i + 1)
                .unwrap_or(0);
            (&raw[..cut], Some(cut as u64))
        } else {
            (&raw[..], None)
        };

        let lines: Vec<&[u8]> = if complete_bytes.is_empty() {
            Vec::new()
        } else {
            let trimmed = complete_bytes.strip_suffix(b"\n").unwrap_or(complete_bytes);
            trimmed.split(|&b| b == b'\n').collect()
        };

        let Some(first_raw) = lines.first() else {
            return Err(format_read_error(
                &path,
                1,
                "first line must be {\"version\":1}",
            ));
        };
        let first_str = std::str::from_utf8(first_raw)
            .map_err(|e| format_read_error(&path, 1, &e.to_string()))?;
        parse_version_line(&path, first_str)?;

        let mut model: Option<String> = None;
        let mut goal: Option<String> = None;
        let mut messages: Vec<Message> = Vec::new();

        for (idx, raw_line) in lines.iter().enumerate().skip(1) {
            let line_no = idx + 1;
            let line_str = std::str::from_utf8(raw_line)
                .map_err(|e| format_read_error(&path, line_no, &e.to_string()))?;
            let val: serde_json::Value = serde_json::from_str(line_str)
                .map_err(|e| format_read_error(&path, line_no, &e.to_string()))?;
            let serde_json::Value::Object(map) = val else {
                return Err(format_read_error(&path, line_no, "expected a JSON object"));
            };
            if map.len() != 1 {
                if let Some(unknown) = map
                    .keys()
                    .find(|k| !matches!(k.as_str(), "model" | "goal" | "message"))
                {
                    return Err(format_read_error(
                        &path,
                        line_no,
                        &format!("unknown key \"{unknown}\""),
                    ));
                }
                return Err(format_read_error(
                    &path,
                    line_no,
                    "expected a JSON object with exactly one key",
                ));
            }
            let (key, value) = map.into_iter().next().unwrap();
            match key.as_str() {
                "model" => {
                    let parsed: String = serde_json::from_value(value)
                        .map_err(|e| format_read_error(&path, line_no, &e.to_string()))?;
                    model = Some(parsed);
                }
                "goal" => {
                    let parsed: Option<String> = serde_json::from_value(value)
                        .map_err(|e| format_read_error(&path, line_no, &e.to_string()))?;
                    goal = parsed;
                }
                "message" => {
                    let parsed: Message = serde_json::from_value(value)
                        .map_err(|e| format_read_error(&path, line_no, &e.to_string()))?;
                    messages.push(parsed);
                }
                other => {
                    return Err(format_read_error(
                        &path,
                        line_no,
                        &format!("unknown key \"{other}\""),
                    ));
                }
            }
        }

        let trimmed_tool_calls = strip_trailing_unmatched_tool_calls(&mut messages);
        if trimmed_tool_calls {
            rewrite_atomic(&path, model.as_deref(), goal.as_deref(), &messages)?;
        } else if let Some(cut) = truncate_to {
            let f = OpenOptions::new()
                .write(true)
                .open(&path)
                .map_err(|e| format!("cannot truncate {}: {e}", path.display()))?;
            f.set_len(cut)
                .and_then(|()| f.sync_all())
                .map_err(|e| format!("cannot truncate {}: {e}", path.display()))?;
        }

        let store = Self {
            path,
            last_model: model.clone(),
            last_goal: goal.clone(),
            saved_messages: messages.len(),
            file_initialized: true,
        };
        Ok((
            store,
            Some(LoadedConversation {
                model,
                goal,
                messages,
            }),
        ))
    }

    pub fn save(
        &mut self,
        model: &str,
        goal: Option<&str>,
        messages: &[Message],
    ) -> Result<(), String> {
        if self.file_initialized && messages.len() < self.saved_messages {
            rewrite_atomic(&self.path, Some(model), goal, messages)?;
            self.last_model = Some(model.to_string());
            self.last_goal = goal.map(str::to_string);
            self.saved_messages = messages.len();
            return Ok(());
        }

        let mut buf = Vec::new();
        if !self.file_initialized {
            push_json_line(&mut buf, &VersionLine { version: 1 })?;
        }
        if self.last_model.as_deref() != Some(model) {
            push_json_line(&mut buf, &ModelLine { model })?;
        }
        if self.last_goal.as_deref() != goal {
            push_json_line(&mut buf, &GoalLine { goal })?;
        }
        for message in &messages[self.saved_messages..] {
            push_json_line(&mut buf, &MessageLine { message })?;
        }

        if buf.is_empty() && self.file_initialized {
            return Ok(());
        }

        if !self.file_initialized {
            ensure_parent_dir(&self.path)?;
        }
        let mut file = open_append_0600(&self.path)?;
        if !self.file_initialized {
            set_file_mode_0600(&self.path)?;
        }
        file.write_all(&buf)
            .and_then(|()| file.flush())
            .and_then(|()| file.sync_all())
            .map_err(|e| format!("cannot write {}: {e}", self.path.display()))?;

        self.file_initialized = true;
        self.last_model = Some(model.to_string());
        self.last_goal = goal.map(str::to_string);
        self.saved_messages = messages.len();
        Ok(())
    }

    pub fn rewrite(
        &mut self,
        model: Option<&str>,
        goal: Option<&str>,
        messages: &[Message],
    ) -> Result<(), String> {
        rewrite_atomic(&self.path, model, goal, messages)?;
        self.file_initialized = true;
        self.last_model = model.map(str::to_string);
        self.last_goal = goal.map(str::to_string);
        self.saved_messages = messages.len();
        Ok(())
    }
}

/// Replaces loaded messages with a single digest message in memory when the starting model's
/// provider kind differs from the last recorded model's provider kind.
pub fn digest_for_provider_in_memory(
    loaded: &mut LoadedConversation,
    starting_model: &str,
) -> bool {
    if loaded.messages.is_empty() {
        return false;
    }
    let Some(ref saved_model) = loaded.model else {
        return false;
    };
    let saved_kind = blyte_provider::detect_provider_kind(Some(saved_model.as_str()));
    let starting_kind = blyte_provider::detect_provider_kind(Some(starting_model));
    if saved_kind == starting_kind {
        return false;
    }
    let summary = blyte_agent::context::digest(&loaded.messages, 1, loaded.messages.len());
    loaded.messages = vec![summary];
    loaded.model = Some(starting_model.to_string());
    true
}

/// Replaces loaded messages with a single digest message and rewrites the store when the starting
/// model's provider kind differs from the last recorded model's provider kind.
pub fn maybe_digest_for_provider(
    store: &mut ConversationStore,
    loaded: &mut LoadedConversation,
    starting_model: &str,
) -> Result<bool, String> {
    if !digest_for_provider_in_memory(loaded, starting_model) {
        return Ok(false);
    }
    store.rewrite(
        Some(starting_model),
        loaded.goal.as_deref(),
        &loaded.messages,
    )?;
    Ok(true)
}