speccoder-runtime 0.8.2

Application services of speccoder: the only crate that executes anything.
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
//! What a person sees when a call needs them: built live from the
//! journaled [`Request`] and what only the live run knows — the file as it
//! is now, the sandbox — and never journaled itself, so replay does not
//! depend on the worktree. The terminal prompt, editors (ACP), the
//! `approvals` listing and the dashboard all render this one value.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use super::Request;
use crate::tools::policy::Object;

/// Lines of a body shown before "v to view all".
pub const HEAD: usize = 20;

/// Files larger than this are described, not shown.
const MAX_BYTES: usize = 1024 * 1024;

/// What only the live run knows.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PresentContext {
    /// The worktree, to read the file a write or edit changes.
    pub root: Option<PathBuf>,
    /// The sandbox mechanism (`Seatbelt`, `Landlock+seccomp`); `None`: off.
    pub sandbox: Option<String>,
    /// What a command's network is without asking (`none`, `loopback`).
    pub network: String,
    /// The file's text, given instead of read from `root` (a saved attempt's).
    pub file_text: Option<String>,
}

/// One choice: `once`, `task`, `run`, `always`, `reject` or `stop`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Choice {
    /// Its name.
    pub choice: String,
    /// Its key (`y`, `t`, `r`, `a`, `n`, `s`).
    pub key: char,
    /// Its number, fixed whether or not the others are offered.
    pub number: u8,
    /// What it does, the scope included.
    pub label: String,
}

/// The call as a person sees it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Presentation {
    /// The question.
    pub request: Request,
    /// `Write this file?`, `Edit this file?`, `Run this command?`,
    /// `Use the network?`, `Call MCP tool?`.
    pub title: String,
    /// The path, the whole command, or the MCP tool — never cut.
    pub subject: String,
    /// The object a decision covers, as lines: the file (and the name as
    /// asked when it differs), the argv with its hash and the executable,
    /// the tool with its digest; and what changed since an approval of the
    /// same name, when something did.
    pub object: Vec<String>,
    /// Where arguments of the call were copied from: `contains text read
    /// from README.md (turn 3): "…"`, then `and N more`.
    pub sources: Vec<String>,
    /// Said once, on the prompt that uses the run's asking budget.
    pub notice: Option<String>,
    /// The body as shown: a diff, a file's head, the command, arguments.
    pub body: Vec<String>,
    /// The whole body (`v`).
    pub full: Vec<String>,
    /// What the call can reach.
    pub sandbox: String,
    /// The choices, in order.
    pub options: Vec<Choice>,
    /// What an approval for the task or run covers (`this path, any content`).
    pub scope: String,
    /// The file's text before and after, for editors that show a diff.
    pub old_text: Option<String>,
    /// The file's text after the call.
    pub new_text: Option<String>,
    /// The file's absolute path, for a write or edit in a known worktree.
    pub file: Option<String>,
}

/// Model text made safe for a terminal: escape (`\x1b`) as `␛`, other
/// control characters as `^X`; newlines and tabs kept.
#[must_use]
pub fn escape(text: &str) -> String {
    text.chars()
        .map(|c| match c {
            '\x1b' => "␛".to_owned(),
            '\n' | '\t' => c.to_string(),
            c if (c as u32) < 0x20 => format!("^{}", char::from(b'@' + c as u8)),
            '\x7f' => "^?".to_owned(),
            c => c.to_string(),
        })
        .collect()
}

/// A word an argv element needs no quotes for.
fn plain(a: &str) -> bool {
    !a.is_empty()
        && a.chars()
            .all(|c| c.is_ascii_alphanumeric() || "-_./=:,+@%".contains(c))
}

/// The argv as a person would type it.
#[must_use]
pub fn shell_quote(argv: &[String]) -> String {
    argv.iter()
        .map(|a| {
            if plain(a) {
                a.clone()
            } else {
                format!("'{}'", a.replace('\'', "'\\''"))
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// What "for the task" or "for this run" covers, by kind: always this
/// exact object.
#[must_use]
pub fn scope_text(kind: &str) -> &'static str {
    match kind {
        "run" => "exactly this command in this directory",
        "network" => "exactly this command in this directory, with the network",
        "mcp" => "this tool as defined now, any arguments",
        "agent" => "exactly this call again",
        "reads" => "the agent's plain reads of one file each, inside the worktree",
        _ => "this file, any content",
    }
}

fn first12(hex: &str) -> &str {
    hex.get(..12).unwrap_or(hex)
}

/// The object's lines: what a decision covers, shown in full.
#[must_use]
pub fn object_lines(object: Option<&Object>, changed: Option<&str>) -> Vec<String> {
    let mut out = Vec::new();
    match object {
        Some(Object::Path { canonical, asked }) => {
            out.push(format!("file  {}", escape(canonical)));
            if asked != canonical {
                out.push(format!(
                    "asked as  {} (resolves to the file above)",
                    escape(asked)
                ));
            }
        }
        Some(Object::Command {
            argv,
            argv_sha256,
            cwd,
            executable,
            network,
        }) => {
            let json = serde_json::to_string(argv).unwrap_or_default();
            out.push(format!(
                "argv  {}  #{}",
                escape(&json),
                first12(argv_sha256)
            ));
            out.push(format!("runs  {}  in  {}", escape(executable), escape(cwd)));
            if *network {
                out.push("network  yes".to_owned());
            }
        }
        Some(Object::Tool { name, digest }) => {
            out.push(format!("tool  {}  @{}", escape(name), first12(digest)));
        }
        Some(Object::Agent {
            agent,
            call,
            argvs,
            paths,
            ..
        }) => {
            out.push(format!(
                "{} itself ({}), outside speccoder's sandbox",
                escape(agent),
                escape(call)
            ));
            for a in argvs {
                out.push(format!("runs  {}", escape(&shell_quote(a))));
            }
            for p in paths {
                out.push(format!("file  {}", escape(p)));
            }
        }
        None => {}
    }
    if let Some(c) = changed {
        out.push(format!("changed since approved: {}", escape(c)));
    }
    out
}

/// The source lines of a request.
#[must_use]
pub fn source_lines(request: &Request) -> Vec<String> {
    let mut out: Vec<String> = request
        .sources
        .iter()
        .map(|s| {
            let when = if s.turn == 0 {
                "the attempt's prompt".to_owned()
            } else {
                format!("turn {}", s.turn)
            };
            format!(
                "contains text read from {} ({when}): \"{}\"",
                escape(&s.from),
                escape(&s.argument)
            )
        })
        .collect();
    if request.more > 0 {
        out.push(format!("and {} more", request.more));
    }
    out
}

/// The notice on the prompt that uses the run's asking budget.
#[must_use]
pub fn budget_notice(request: &Request) -> Option<String> {
    (request.budget > 0 && request.asked == request.budget && !request.batched).then(|| {
        format!(
            "This is prompt {0} of {0} for this run; further calls will wait in Needs You.",
            request.budget
        )
    })
}

fn head(lines: &[String]) -> (Vec<String>, usize) {
    let shown = lines.iter().take(HEAD).cloned().collect();
    (shown, lines.len().saturating_sub(HEAD))
}

/// Numbered lines of a new file.
fn numbered(text: &str) -> Vec<String> {
    text.lines()
        .enumerate()
        .map(|(i, l)| format!("{:>5} │ {}", i + 1, escape(l)))
        .collect()
}

/// A unified diff with three lines of context and old/new line numbers.
#[must_use]
pub fn diff(old: &str, new: &str) -> Vec<String> {
    let d = similar::TextDiff::from_lines(old, new);
    let mut out = Vec::new();
    for (i, group) in d.grouped_ops(3).iter().enumerate() {
        if i > 0 {
            out.push("          ┈".to_owned());
        }
        for op in group {
            for change in d.iter_changes(op) {
                let (sign, old_no, new_no) = match change.tag() {
                    similar::ChangeTag::Equal => (' ', change.old_index(), change.new_index()),
                    similar::ChangeTag::Delete => ('-', change.old_index(), None),
                    similar::ChangeTag::Insert => ('+', None, change.new_index()),
                };
                let no = |n: Option<usize>| n.map_or(String::new(), |n| (n + 1).to_string());
                let line = format!(
                    "{:>5} {:>5} {sign} {}",
                    no(old_no),
                    no(new_no),
                    escape(change.value().trim_end_matches(['\n', '\r']))
                );
                out.push(line.trim_end().to_owned());
            }
        }
    }
    out
}

/// The text of `path` under `root`, or why it cannot be shown.
fn read(context: &PresentContext, path: &str) -> Result<Option<String>, String> {
    if let Some(text) = &context.file_text {
        return Ok(Some(text.clone()));
    }
    let Some(root) = context.root.as_deref() else {
        return Ok(None);
    };
    let file = root.join(path);
    let Ok(bytes) = std::fs::read(&file) else {
        return Ok(None);
    };
    if bytes.len() > MAX_BYTES {
        return Err(format!("{} bytes — too large to show", bytes.len()));
    }
    String::from_utf8(bytes)
        .map(Some)
        .map_err(|_| "a binary file — not shown".to_owned())
}

/// Builds what the person sees.
#[must_use]
pub fn present(request: &Request, context: &PresentContext) -> Presentation {
    let args = &request.arguments;
    let s = |k: &str| args[k].as_str().unwrap_or_default().to_owned();
    let path = s("path");
    let kind = request.kind.as_str();
    let mut old_text = None;
    let mut new_text = None;
    let (title, subject, full) = match kind {
        "run" | "network" => {
            let argv: Vec<String> = args["argv"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(|v| v.as_str().map(str::to_owned))
                .collect();
            let cmd = escape(&shell_quote(&argv));
            let cwd = args["cwd"]
                .as_str()
                .filter(|c| !c.is_empty())
                .unwrap_or(".");
            let net = if kind == "network" {
                "this call".to_owned()
            } else {
                context.network.clone()
            };
            (
                if kind == "network" {
                    "Use the network?"
                } else {
                    "Run this command?"
                },
                cmd.clone(),
                vec![format!("$ {cmd}"), format!("cwd: {cwd} · network: {net}")],
            )
        }
        "mcp" => {
            let pretty = serde_json::to_string_pretty(args).unwrap_or_default();
            (
                "Call MCP tool?",
                request.tool.clone(),
                pretty.lines().map(escape).collect(),
            )
        }
        // An external agent's own call speccoder cannot classify: what the
        // agent said about it, as it said it.
        "agent" | "reads" => {
            let pretty = serde_json::to_string_pretty(&args["rawInput"]).unwrap_or_default();
            let mut lines = vec![format!(
                "kind: {}",
                escape(args["kind"].as_str().unwrap_or("not said"))
            )];
            lines.extend(pretty.lines().map(escape));
            (
                "Let the agent make this call?",
                escape(args["title"].as_str().unwrap_or("an untitled call")),
                lines,
            )
        }
        _ => {
            let current = read(context, &path);
            // A write over a file that exists now is an edit of it.
            let title = if kind == "write" && matches!(current, Ok(None)) {
                "Write this file?"
            } else {
                "Edit this file?"
            };
            let lines = match current {
                Err(note) => vec![note],
                Ok(current) => match (request.tool.as_str(), current) {
                    ("write_file", None) => {
                        let content = s("content");
                        new_text = Some(content.clone());
                        numbered(&content)
                    }
                    ("write_file", Some(old)) => {
                        let content = s("content");
                        let d = diff(&old, &content);
                        old_text = Some(old);
                        new_text = Some(content);
                        d
                    }
                    (_, Some(old)) => {
                        let (from, to) = (s("old_string"), s("new_string"));
                        let all = args["replace_all"] == true;
                        if !from.is_empty() && old.contains(&from) {
                            let new = if all {
                                old.replace(&from, &to)
                            } else {
                                old.replacen(&from, &to, 1)
                            };
                            let d = diff(&old, &new);
                            old_text = Some(old);
                            new_text = Some(new);
                            d
                        } else {
                            let mut l = vec![
                                "the text to replace is not in the file as it is now:".to_owned(),
                            ];
                            l.extend(from.lines().map(|x| format!("  - {}", escape(x))));
                            l.extend(to.lines().map(|x| format!("  + {}", escape(x))));
                            l
                        }
                    }
                    (_, None) => {
                        let (from, to) = (s("old_string"), s("new_string"));
                        let mut l: Vec<String> =
                            from.lines().map(|x| format!("  - {}", escape(x))).collect();
                        l.extend(to.lines().map(|x| format!("  + {}", escape(x))));
                        l
                    }
                },
            };
            (title, escape(&path), lines)
        }
    };
    let (mut body, more) = head(&full);
    if more > 0 {
        body.push(format!("… {more} more lines (v to view all)"));
    }
    let sandbox = match kind {
        "run" | "network" => match &context.sandbox {
            Some(m) => format!(
                "sandbox on ({m}) · network: {} · writes: worktree",
                if kind == "network" {
                    "this call"
                } else {
                    context.network.as_str()
                }
            ),
            None => "sandbox off — runs unconfined".to_owned(),
        },
        "mcp" => "the MCP server runs outside speccoder's sandbox".to_owned(),
        "agent" | "reads" => {
            "the agent's own call — it runs outside speccoder's sandbox".to_owned()
        }
        _ => "a file write — nothing is executed".to_owned(),
    };
    let scope = scope_text(kind).to_owned();
    let task = request.task.rsplit('/').next().unwrap_or(&request.task);
    let options = request
        .options
        .iter()
        .filter_map(|o| choice(o, task, &scope))
        .collect();
    Presentation {
        request: request.clone(),
        title: title.to_owned(),
        subject,
        object: object_lines(request.object.as_ref(), request.changed.as_deref()),
        sources: source_lines(request),
        notice: budget_notice(request),
        body,
        full,
        sandbox,
        options,
        scope,
        old_text,
        new_text,
        file: context
            .root
            .as_ref()
            .filter(|_| !path.is_empty() && matches!(kind, "write" | "edit"))
            .map(|r| r.join(&path).display().to_string()),
    }
}

/// The text a terminal shows for a bug's reproductions: each with its
/// statement, file, test and how it failed, then the digest the answer is
/// bound to.
#[must_use]
pub fn confirm_text(r: &super::ConfirmRequest) -> String {
    let mut out = format!(
        "Bug {} — {}: {} reproduction(s) fail on the untouched code by an assertion.\n",
        r.bug,
        r.title,
        r.reproductions
            .iter()
            .filter(|x| x.statement.starts_with('E'))
            .count()
    );
    for x in &r.reproductions {
        out.push_str(&format!(
            "\n{} — {}\n  {} :: {}\n  $ {}\n",
            x.statement,
            x.words,
            x.file,
            x.test,
            x.argv.join(" ")
        ));
        for l in x.excerpt.lines().take(8) {
            out.push_str(&format!("    | {l}\n"));
        }
    }
    if let Some(w) = &r.warning {
        out.push_str(&format!("\n! {w}\n"));
    }
    out.push_str(&format!("\ndigest {}\n", r.digest));
    out
}

/// The choice `o` as every interface offers it: its key, its fixed number
/// and its label.
#[must_use]
pub fn choice(o: &str, task: &str, scope: &str) -> Option<Choice> {
    let (key, number, label) = match o {
        "once" => ('y', 1, "Yes, once".to_owned()),
        "task" => ('t', 2, format!("Yes, for the rest of {task}: {scope}")),
        "run" => ('r', 3, format!("Yes, for this run: {scope}, in any task")),
        "always" => (
            'a',
            4,
            "Always allow exactly this command (adds it to speccoder.toml)".to_owned(),
        ),
        "reject" => ('n', 5, "No, and tell the model why …".to_owned()),
        "stop" => (
            's',
            6,
            "Stop this task here (it waits; decide later)".to_owned(),
        ),
        _ => return None,
    };
    Some(Choice {
        choice: o.to_owned(),
        key,
        number,
        label,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::path::Path;

    fn request(tool: &str, arguments: serde_json::Value, kind: &str, risk: &str) -> Request {
        Request {
            task: "001-slugify/T004".to_owned(),
            task_title: "Handle Unicode input".to_owned(),
            turn: 8,
            tool: tool.to_owned(),
            arguments,
            kind: kind.to_owned(),
            reason: "why".to_owned(),
            risk: risk.to_owned(),
            options: crate::approvals::options(risk, true, false, kind, risk),
            ..Request::default()
        }
    }

    fn ctx(root: &Path) -> PresentContext {
        PresentContext {
            root: Some(root.to_path_buf()),
            sandbox: Some("Seatbelt".to_owned()),
            network: "none".to_owned(),
            file_text: None,
        }
    }

    #[test]
    fn a_new_file_shows_its_head() {
        let d = tempfile::tempdir().unwrap();
        let content: String = (1..=25).map(|i| format!("line {i}\n")).collect();
        let p = present(
            &request(
                "write_file",
                json!({ "path": "src/new.rs", "content": content }),
                "write",
                "low",
            ),
            &ctx(d.path()),
        );
        assert_eq!(
            (p.title.as_str(), p.subject.as_str()),
            ("Write this file?", "src/new.rs")
        );
        assert_eq!(p.body[0], "    1 │ line 1");
        assert_eq!(p.body.last().unwrap(), "… 5 more lines (v to view all)");
        assert_eq!(p.full.len(), 25);
        assert_eq!(p.sandbox, "a file write — nothing is executed");
        assert!(p.old_text.is_none() && p.new_text.is_some());
    }

    #[test]
    fn an_edit_is_a_numbered_diff() {
        let d = tempfile::tempdir().unwrap();
        std::fs::write(
            d.path().join("Cargo.toml"),
            "[package]\nname = \"x\"\n\n[dependencies]\nserde = \"1\"\n",
        )
        .unwrap();
        let p = present(
            &request(
                "edit_file",
                json!({ "path": "Cargo.toml", "old_string": "serde = \"1\"\n", "new_string": "serde = \"1\"\nunicode-normalization = \"0.1\"\n" }),
                "edit",
                "medium",
            ),
            &ctx(d.path()),
        );
        assert_eq!(p.title, "Edit this file?");
        assert!(
            p.body
                .contains(&"          6 + unicode-normalization = \"0.1\"".to_owned()),
            "{:#?}",
            p.body
        );
        assert!(
            p.body.contains(&"    5     5   serde = \"1\"".to_owned()),
            "{:#?}",
            p.body
        );
        let labels: Vec<&str> = p.options.iter().map(|o| o.label.as_str()).collect();
        assert_eq!(
            labels[1],
            "Yes, for the rest of T004: this file, any content"
        );
        assert_eq!(
            labels[2],
            "Yes, for this run: this file, any content, in any task"
        );
    }

    #[test]
    fn a_missing_old_string_is_said_and_shown() {
        let d = tempfile::tempdir().unwrap();
        std::fs::write(d.path().join("a.rs"), "fn a() {}\n").unwrap();
        let p = present(
            &request(
                "edit_file",
                json!({ "path": "a.rs", "old_string": "fn b() {}", "new_string": "fn c() {}" }),
                "edit",
                "low",
            ),
            &ctx(d.path()),
        );
        assert_eq!(
            p.body[0],
            "the text to replace is not in the file as it is now:"
        );
        assert_eq!(
            p.body[1..],
            ["  - fn b() {}".to_owned(), "  + fn c() {}".to_owned()]
        );
    }

    #[test]
    fn commands_network_and_mcp() {
        let d = tempfile::tempdir().unwrap();
        let p = present(
            &request(
                "run_command",
                json!({ "argv": ["sh", "-c", "echo 'hi' > x"] }),
                "run",
                "high",
            ),
            &ctx(d.path()),
        );
        assert_eq!(p.title, "Run this command?");
        assert_eq!(
            p.body,
            ["$ sh -c 'echo '\\''hi'\\'' > x'", "cwd: . · network: none"]
        );
        assert_eq!(
            p.sandbox,
            "sandbox on (Seatbelt) · network: none · writes: worktree"
        );
        // High risk: no "for this run", no "always"; the numbers stay fixed.
        let numbers: Vec<u8> = p.options.iter().map(|o| o.number).collect();
        assert_eq!(numbers, [1, 2, 5, 6]);
        let p = present(
            &request(
                "run_command",
                json!({ "argv": ["curl", "x"], "network": true }),
                "network",
                "high",
            ),
            &PresentContext {
                sandbox: None,
                ..ctx(d.path())
            },
        );
        assert_eq!(p.title, "Use the network?");
        assert_eq!(p.body[1], "cwd: . · network: this call");
        assert_eq!(p.sandbox, "sandbox off — runs unconfined");
        let p = present(
            &request(
                "mcp__gh__create_issue",
                json!({ "title": "x" }),
                "mcp",
                "medium",
            ),
            &ctx(d.path()),
        );
        assert_eq!(
            (p.title.as_str(), p.subject.as_str()),
            ("Call MCP tool?", "mcp__gh__create_issue")
        );
        assert_eq!(p.sandbox, "the MCP server runs outside speccoder's sandbox");
        assert!(
            p.options[1]
                .label
                .ends_with("this tool as defined now, any arguments")
        );
    }

    #[test]
    fn model_text_cannot_drive_the_terminal() {
        assert_eq!(escape("a\x1b[2Jb\x07c\nd"), "a␛[2Jb^Gc\nd");
        let d = tempfile::tempdir().unwrap();
        std::fs::write(d.path().join("bin"), [0u8, 159, 146, 150]).unwrap();
        let p = present(
            &request(
                "write_file",
                json!({ "path": "bin", "content": "x" }),
                "edit",
                "low",
            ),
            &ctx(d.path()),
        );
        assert_eq!(p.body, ["a binary file — not shown"]);
    }

    #[test]
    fn the_object_sources_and_budget_are_shown() {
        let d = tempfile::tempdir().unwrap();
        let mut r = request(
            "run_command",
            json!({ "argv": ["make", "check"] }),
            "run",
            "low",
        );
        r.object = Some(Object::command(
            vec!["make".to_owned(), "check".to_owned()],
            ".".to_owned(),
            "/w/bin/make".to_owned(),
            false,
        ));
        r.changed = Some("executable /usr/bin/make → /w/bin/make".to_owned());
        r.sources = vec![crate::approvals::Source {
            argument: "make check".to_owned(),
            from: "README.md".to_owned(),
            turn: 2,
        }];
        r.more = 2;
        r.asked = 12;
        r.budget = 12;
        r.options = crate::approvals::options("low", true, false, "run", "low");
        let p = present(&r, &ctx(d.path()));
        assert_eq!(
            p.object[0],
            format!(
                "argv  [\"make\",\"check\"]  #{}",
                &crate::tools::policy::argv_sha256(&["make".to_owned(), "check".to_owned()])[..12]
            )
        );
        assert_eq!(p.object[1], "runs  /w/bin/make  in  .");
        assert_eq!(
            p.object[2],
            "changed since approved: executable /usr/bin/make → /w/bin/make"
        );
        assert_eq!(
            p.sources,
            [
                "contains text read from README.md (turn 2): \"make check\"",
                "and 2 more"
            ]
        );
        assert!(
            p.notice
                .as_deref()
                .is_some_and(|n| n.starts_with("This is prompt 12 of 12"))
        );
        let keys: Vec<char> = p.options.iter().map(|o| o.key).collect();
        assert_eq!(keys, ['y', 't', 'r', 'a', 'n', 's']);
        // A path asked by another name shows both.
        let mut w = request(
            "write_file",
            json!({ "path": "src/a/x.rs", "content": "" }),
            "write",
            "low",
        );
        w.object = Some(Object::Path {
            canonical: "src/b/x.rs".to_owned(),
            asked: "src/a/x.rs".to_owned(),
        });
        let p = present(&w, &ctx(d.path()));
        assert_eq!(
            p.object,
            [
                "file  src/b/x.rs",
                "asked as  src/a/x.rs (resolves to the file above)"
            ]
        );
    }
}