From 226c44dc770dd0ea42e322af4d0edd6c14ce14db Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Nov 2026 12:45:05 +0100 Subject: 0.11.0: /undo via git checkpoints, /compact, provider presets, leniency, system CA - /undo resets to a git checkpoint taken before each turn - /compact summarises and rewrites the transcript (ADR 0003, the one sanctioned rewrite) - --provider presets for six OpenAI-compatible gateways, local ones lenient by default - --lenient and --system-ca - proxies already honoured via ureq; documented --- src/main.rs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 12 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a425d71..8f53d9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::io::{BufRead, IsTerminal, Write}; +use std::path::Path; use serde_json::json; use std::time::UNIX_EPOCH; use std::path::PathBuf; @@ -37,6 +38,10 @@ OPTIONS: --show [ID] print a transcript (most recent if no ID) and exit --doctor check key, directories, provider and model, then exit --yolo approve every tool call without asking + --provider preset: opencode-go, openai, openrouter, ollama, + llamacpp, vllm + --lenient tolerate providers that omit [DONE]/usage + --system-ca verify TLS with the system trust store -V, --version print the version and exit -h, --help this text @@ -63,6 +68,9 @@ struct Args { doctor: bool, yolo: bool, json: bool, + provider: Option, + lenient: bool, + system_ca: bool, } fn main() { @@ -88,6 +96,9 @@ fn parse_args() -> Result { doctor: false, yolo: false, json: false, + provider: None, + lenient: false, + system_ca: false, }; let mut words: Vec = Vec::new(); let mut iter = std::env::args().skip(1).peekable(); @@ -118,6 +129,10 @@ fn parse_args() -> Result { args.show = Some(Some(value.to_string())); continue; } + "--provider" => { + args.provider = Some(value.to_string()); + continue; + } _ => return Err(format!("unknown flag {flag}\n\n{HELP}")), } } @@ -142,6 +157,9 @@ fn parse_args() -> Result { "--doctor" => args.doctor = true, "--yolo" => args.yolo = true, "--json" => args.json = true, + "--provider" => args.provider = Some(take_value(&mut iter, "--provider")?), + "--lenient" => args.lenient = true, + "--system-ca" => args.system_ca = true, "-V" | "--version" => { println!("agent686 {}", env!("CARGO_PKG_VERSION")); std::process::exit(0); @@ -216,13 +234,27 @@ fn run(args: Args) { } if args.doctor { - let mut config = Config::resolve_lenient(args.model, args.base_url, args.yolo); + let mut config = Config::resolve_lenient( + args.model, + args.base_url, + args.yolo, + args.provider, + args.lenient, + args.system_ca, + ); // `/models` needs the session header too, even though this is not a turn. config.session = session::new_id(); std::process::exit(doctor::run(&config, args.json)); } - let mut cfg = match Config::resolve(args.model, args.base_url, args.yolo) { + let mut cfg = match Config::resolve( + args.model, + args.base_url, + args.yolo, + args.provider, + args.lenient, + args.system_ca, + ) { Ok(cfg) => cfg, Err(message) => { eprintln!("agent686: {message}"); @@ -287,6 +319,7 @@ fn run(args: Args) { let mut editor = Editor::new(); let interactive = editor.is_interactive(); + let mut checkpoint: Option = None; if !interactive && !cfg.yolo { eprintln!( "agent686: stdin is not a terminal, so write/edit/bash will be denied; \ @@ -330,6 +363,14 @@ fn run(args: Args) { forget_sessions(&text, &session); continue; } + if text == "/undo" { + undo(checkpoint.as_deref()); + continue; + } + if text == "/compact" { + compact(&cfg, &mut session, &mut history); + continue; + } match text.as_str() { "/exit" | "/quit" => break, @@ -364,6 +405,7 @@ fn run(args: Args) { } warn_if_changed(&watched, &mut warned); + checkpoint = git_checkpoint(); if let Err(error) = agent::run_turn( &cfg, &mut session, @@ -439,6 +481,63 @@ fn open_session( /// The system prompt is frozen with the directory the session started in. If the /// user resumes from somewhere else, tools and prompt disagree. +/// Git is the documented undo, so before a turn we snapshot the tree with a +/// commit when it is dirty. `/undo` resets back to this. +fn git_checkpoint() -> Option { + let Ok(cwd) = std::env::current_dir() else { + return None; + }; + agent686::git::checkpoint(&cwd) +} + +fn undo(checkpoint: Option<&str>) { + let Some(sha) = checkpoint else { + eprintln!("agent686: /undo needs a git repository"); + return; + }; + let Ok(cwd) = std::env::current_dir() else { + return; + }; + agent686::git::undo(&cwd, sha); + eprintln!("agent686: restored to {sha}"); +} + +/// `/compact`: summarise the conversation and start again from the summary, in +/// the same session id. The only sanctioned rewrite of the transcript (ADR 0003). +fn compact(cfg: &Config, session: &mut Session, history: &mut Vec) { + let system = history.first().cloned().unwrap_or_else(|| agent::system_prompt()); + let mut messages = history.clone(); + messages.push(json!({ + "role": "user", + "content": "Summarize the conversation so far into one concise paragraph that \ + preserves the goal, decisions made, files changed, and the current \ + state. Omit pleasantries." + })); + let summary = match agent686::provider::summarize(cfg, &messages) { + Ok(summary) if !summary.trim().is_empty() => summary, + Ok(_) => { + eprintln!("agent686: /compact produced an empty summary; aborting"); + return; + } + Err(error) => { + eprintln!("agent686: /compact failed: {error}"); + return; + } + }; + + let user = json!({ + "role": "user", + "content": format!("[earlier conversation, summarized]\n{summary}"), + }); + let new_history = vec![system.clone(), user]; + if let Err(error) = session.rewrite(&system, &new_history[1..]) { + eprintln!("agent686: /compact could not rewrite the session: {error}"); + return; + } + *history = new_history; + eprintln!("agent686: compacted into a {}-character summary", summary.len()); +} + fn list_models(cfg: &Config) { match agent686::provider::models(cfg) { Ok(models) => { @@ -640,16 +739,8 @@ fn refuse_yolo(cfg: &Config) { } } -fn in_git_repo(dir: &std::path::Path) -> bool { - let mut current = dir.to_path_buf(); - loop { - if current.join(".git").exists() { - return true; - } - if !current.pop() { - return false; - } - } +fn in_git_repo(dir: &Path) -> bool { + agent686::git::in_repo(dir) } fn style(code: &str, text: &str, color: bool) -> String { -- cgit v1.2.3