From 343f2a8a6148b8004d6f4d74fb779b4f30c9686a Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 03:11:22 +0100 Subject: M1-M5: sessions, six tools, approvals, prompt, Ctrl-C, target gate - append-only JSONL sessions with --continue/--session; reasoning stored, stripped on replay - read/write/edit/grep/glob + bash with timeout and capped output - diff-preview approval for the first edit of a session, per-call approval for bash - system prompt frozen per session with AGENTS.md snapshot and change warning - SIGINT aborts the turn, second Ctrl-C exits - glibc gate ignores weak symbols; verified in linux/386 debian:12 - acceptance test (calculator in C) passes on both x86-64 and i386 --- src/ui.rs | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 155 insertions(+), 16 deletions(-) (limited to 'src/ui.rs') diff --git a/src/ui.rs b/src/ui.rs index 13440b2..2ec0821 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,5 @@ use std::io::{IsTerminal, Write}; +use std::sync::OnceLock; use std::time::Duration; use crate::provider::{Outcome, Sink}; @@ -7,18 +8,33 @@ pub fn stdout_is_tty() -> bool { std::io::stdout().is_terminal() } +pub fn stdin_is_tty() -> bool { + std::io::stdin().is_terminal() +} + +fn color() -> bool { + static COLOR: OnceLock = OnceLock::new(); + *COLOR.get_or_init(stdout_is_tty) +} + +fn paint(code: &str, text: &str) -> String { + if color() { + format!("\x1b[{code}m{text}\x1b[0m") + } else { + text.to_string() + } +} + /// Streams to stdout. Colour is used only when stdout is a terminal, so piping /// the output somewhere gives clean text. pub struct Renderer { - color: bool, dimmed: bool, wrote_text: bool, } impl Renderer { - pub fn new(color: bool) -> Self { + pub fn new() -> Self { Self { - color, dimmed: false, wrote_text: false, } @@ -41,7 +57,7 @@ impl Sink for Renderer { } fn reasoning(&mut self, s: &str) { - if !self.color { + if !color() { return; } if !self.dimmed { @@ -52,24 +68,147 @@ impl Sink for Renderer { let _ = std::io::stdout().flush(); } - fn done(&mut self, outcome: &Outcome, elapsed: Duration) { + fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { self.undim(); if self.wrote_text { println!(); + self.wrote_text = false; + } + } +} + +pub fn tool_line(name: &str, args: &serde_json::Value) { + let detail = match name { + "bash" => args + .get("cmd") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + "grep" => format!( + "{} in {}", + args.get("pattern").and_then(|v| v.as_str()).unwrap_or(""), + args.get("path").and_then(|v| v.as_str()).unwrap_or(".") + ), + "glob" => args + .get("pattern") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + _ => args + .get("path") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + }; + println!("{} {}", paint("1;36", &format!("● {name}")), detail); + let _ = std::io::stdout().flush(); +} + +pub fn tool_result(result: &Result) { + let text = match result { + Ok(output) => output, + Err(error) => error, + }; + let first = text.lines().find(|l| !l.trim().is_empty()).unwrap_or(""); + let summary = crate::provider::truncate(first.trim(), 100); + println!(" {} {}", paint("2", "└"), paint("2", &summary)); + let _ = std::io::stdout().flush(); +} + +pub fn status(steps: usize, elapsed: Duration, outcome: &Outcome) { + let mut parts = vec![ + if steps == 1 { + "1 step".to_string() + } else { + format!("{steps} steps") + }, + format!("{:.1}s", elapsed.as_secs_f64()), + ]; + let (input, output) = outcome.tokens(); + if let (Some(i), Some(o)) = (input, output) { + parts.push(format!("{i} in · {o} out")); + } + if let Some(allowance) = outcome.allowance() { + if allowance > 0.0 { + parts.push(format!("${allowance:.6}")); + } + } + println!("{}", paint("2", &format!("─ {}", parts.join(" · ")))); + let _ = std::io::stdout().flush(); +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Answer { + No, + Yes, + Always, +} + +pub struct Approver { + yolo: bool, + interactive: bool, + edits_allowed: bool, +} + +impl Approver { + pub fn new(yolo: bool) -> Self { + Self { + yolo, + interactive: stdin_is_tty(), + edits_allowed: false, + } + } + + pub fn yolo(&self) -> bool { + self.yolo + } + + fn ask(&self, question: &str) -> Answer { + if self.yolo { + return Answer::Yes; + } + if !self.interactive { + println!( + "{}", + paint("33", " no terminal to ask; denied (use --yolo to allow)") + ); + return Answer::No; } - let mut parts = vec![format!("{:.1}s", elapsed.as_secs_f64())]; - if let Some(usage) = &outcome.usage { - let input = usage.get("prompt_tokens").and_then(|v| v.as_u64()); - let output = usage.get("completion_tokens").and_then(|v| v.as_u64()); - if let (Some(i), Some(o)) = (input, output) { - parts.push(format!("{i} in · {o} out")); + print!("\n{} {} ", question, paint("2", "[y/N/a]")); + let _ = std::io::stdout().flush(); + + let mut line = String::new(); + if std::io::stdin().read_line(&mut line).unwrap_or(0) == 0 { + return Answer::No; + } + match line.trim().to_ascii_lowercase().as_str() { + "y" | "yes" => Answer::Yes, + "a" | "always" => Answer::Always, + _ => Answer::No, + } + } + + /// One prompt for the first edit of a session, then silent. + pub fn allow_edit(&mut self, path: &str, preview: &str) -> bool { + if self.yolo || self.edits_allowed { + return true; + } + println!("{preview}"); + match self.ask(&format!("apply this change to {path}?")) { + Answer::Yes => true, + Answer::Always => { + self.edits_allowed = true; + true } + Answer::No => false, } - let line = format!("─ {}", parts.join(" · ")); - if self.color { - println!("\x1b[2m{line}\x1b[0m"); - } else { - println!("{line}"); + } + + /// Every call, no memory: the point is seeing the command. + pub fn allow_bash(&mut self, cmd: &str) -> bool { + if self.yolo { + return true; } + self.ask(&format!("run: {}", crate::provider::truncate(cmd, 200))) == Answer::Yes } } -- cgit v1.2.3