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/main.rs | 217 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 162 insertions(+), 55 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 8c9a58c..c53ebb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,13 @@ use std::io::{BufRead, IsTerminal, Write}; -use std::sync::atomic::AtomicBool; +use std::path::PathBuf; +use std::time::SystemTime; -use agent686::{agent, config::Config, ui}; +use serde_json::Value; + +use agent686::config::Config; +use agent686::session::{self, Session}; +use agent686::ui::Approver; +use agent686::{agent, signal}; const HELP: &str = "\ agent686 — a small coding agent @@ -9,33 +15,40 @@ agent686 — a small coding agent USAGE: agent686 [OPTIONS] [PROMPT...] -With a prompt it runs one turn and exits; without one it reads prompts from -stdin, one per line. /exit quits. +With a prompt it runs one turn and exits. Without one it reads prompts from +stdin, one per line. Commands: /paste, /exit, /help. OPTIONS: -m, --model model id (default: deepseek-v4.1-flash) - --base-url provider base URL, e.g. http://127.0.0.1:8080/v1 - --session reuse a session id, including for the provider header + --base-url provider base URL + --session resume a specific session + -c, --continue resume the most recent session + --yolo approve every tool call without asking -h, --help this text ENVIRONMENT: - OPENCODE_API_KEY credential (falls back to ~/.config/agent686/key) - AGENT686_MODEL default model id - AGENT686_BASE_URL default provider base URL + OPENCODE_API_KEY credential (falls back to ~/.config/agent686/key) + AGENT686_MODEL default model id + AGENT686_BASE_URL default provider base URL + AGENT686_DATA where sessions live (default ~/.local/share/agent686) "; fn main() { - let mut prompt: Option = None; + let mut prompt_words: Vec = Vec::new(); let mut model = None; let mut base_url = None; - let mut session = None; + let mut resume = None; + let mut continue_last = false; + let mut yolo = false; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "-m" | "--model" => model = args.next(), "--base-url" => base_url = args.next(), - "--session" => session = args.next(), + "--session" | "--resume" => resume = args.next(), + "-c" | "--continue" => continue_last = true, + "--yolo" => yolo = true, "-h" | "--help" => { print!("{HELP}"); return; @@ -45,16 +58,13 @@ fn main() { print!("{HELP}"); std::process::exit(2); } - word => { - prompt = Some(match prompt.take() { - Some(existing) => format!("{existing} {word}"), - None => word.to_string(), - }) - } + word => prompt_words.push(word.to_string()), } } - let cfg = match Config::resolve(model, base_url, session) { + signal::install(); + + let mut cfg = match Config::resolve(model, base_url, yolo) { Ok(cfg) => cfg, Err(message) => { eprintln!("agent686: {message}"); @@ -62,18 +72,31 @@ fn main() { } }; - let cancel = AtomicBool::new(false); - let mut history = agent::new_history(); - - // One-shot mode: a prompt on the command line means one turn, then exit. - if let Some(prompt) = prompt { - let mut sink = ui::Renderer::new(ui::stdout_is_tty()); - match agent::run_turn(&cfg, &mut history, &prompt, &cancel, &mut sink) { - Ok(outcome) => note_tool_calls(&outcome), - Err(error) => { - eprintln!("agent686: {error}"); - std::process::exit(1); - } + let (mut session, mut history) = match open_session(&cfg, resume, continue_last) { + Ok(pair) => pair, + Err(message) => { + eprintln!("agent686: {message}"); + std::process::exit(1); + } + }; + cfg.session = session.id.clone(); + + let mut approver = Approver::new(cfg.yolo); + let watched = agent::agents_md(); + let mut warned = false; + + if !prompt_words.is_empty() { + let prompt = prompt_words.join(" "); + if let Err(error) = agent::run_turn( + &cfg, + &mut session, + &mut history, + &prompt, + &signal::CANCEL, + &mut approver, + ) { + eprintln!("agent686: {error}"); + std::process::exit(1); } return; } @@ -84,9 +107,9 @@ fn main() { "agent686 · {} · session {}{}", cfg.model, cfg.session, - if is_local(&cfg.base_url) { " · local" } else { "" } + if cfg.yolo { " · yolo" } else { "" } ); - eprintln!("one prompt per line · /exit to quit"); + eprintln!("one prompt per line · /paste · /exit"); } let stdin = std::io::stdin(); @@ -100,37 +123,121 @@ fn main() { if stdin.lock().read_line(&mut line).unwrap_or(0) == 0 { break; } - let text = line.trim_end(); + let mut text = line.trim_end().to_string(); if text.is_empty() { continue; } - if text == "/exit" || text == "/quit" { - break; + + match text.as_str() { + "/exit" | "/quit" => break, + "/help" => { + print!("{HELP}"); + continue; + } + "/paste" => { + let mut buffer = String::new(); + loop { + if interactive { + print!("… "); + let _ = std::io::stdout().flush(); + } + line.clear(); + if stdin.lock().read_line(&mut line).unwrap_or(0) == 0 { + break; + } + let piece = line.trim_end_matches(['\n', '\r']); + if piece == "." { + break; + } + buffer.push_str(piece); + buffer.push('\n'); + } + text = buffer.trim_end().to_string(); + if text.is_empty() { + continue; + } + } + _ => {} } - let mut sink = ui::Renderer::new(ui::stdout_is_tty()); - match agent::run_turn(&cfg, &mut history, text, &cancel, &mut sink) { - Ok(outcome) => note_tool_calls(&outcome), - Err(error) => eprintln!("agent686: {error}"), + + warn_if_changed(&watched, &mut warned); + if let Err(error) = agent::run_turn( + &cfg, + &mut session, + &mut history, + &text, + &signal::CANCEL, + &mut approver, + ) { + eprintln!("agent686: {error}"); } } } -fn note_tool_calls(outcome: &agent686::provider::Outcome) { - if outcome.tool_calls.is_empty() { - return; +fn open_session( + cfg: &Config, + resume: Option, + continue_last: bool, +) -> Result<(Session, Vec), String> { + let id = match resume { + Some(id) => Some(id), + None if continue_last => Some( + session::latest()? + .ok_or_else(|| "no sessions to continue; run without --continue".to_string())?, + ), + None => None, + }; + + match id { + Some(id) => { + let (session, system, messages) = Session::open(&id)?; + if !session.model.is_empty() && session.model != cfg.model { + eprintln!( + "agent686: session used {}; continuing with {}", + session.model, cfg.model + ); + } + // The system message frozen at session start is reused verbatim, so + // the request prefix stays byte-identical across restarts. + let system = if system.is_null() { + agent::system_prompt() + } else { + system + }; + let mut history = agent::new_history(system); + let count = messages.len(); + history.extend(messages); + eprintln!("agent686: resumed {} ({count} messages)", session.id); + Ok((session, history)) + } + None => { + let system = agent::system_prompt(); + let session = Session::create(&cfg.model, &system)?; + eprintln!( + "agent686: new session {} ({})", + session.id, + session.path().display() + ); + Ok((session, agent::new_history(system))) + } } - let names: Vec<&str> = outcome - .tool_calls - .iter() - .map(|call| call.name.as_str()) - .collect(); - eprintln!( - "agent686: model asked for {} tool call(s) [{}]; tools arrive in milestone 2", - names.len(), - names.join(", ") - ); } -fn is_local(base_url: &str) -> bool { - base_url.contains("127.0.0.1") || base_url.contains("localhost") +fn warn_if_changed(watched: &Option<(PathBuf, SystemTime)>, warned: &mut bool) { + let (path, seen) = match watched { + Some(watched) => watched, + None => return, + }; + if *warned { + return; + } + if let Ok(modified) = std::fs::metadata(path).and_then(|m| m.modified()) { + if modified != *seen { + eprintln!( + "agent686: {} changed; start a new session to pick it up", + path.display() + ); + *warned = true; + } + } } -- cgit v1.2.3