From f192c40b2a3643a9ab97303310db8dbc7547ee35 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 14:59:31 +0100 Subject: Robustness: retry with backoff, explained errors, --list, --session creates - four attempts on 408/425/429/500/502/503/504 and transport errors, honouring Retry-After - backoff is interruptible so Ctrl-C is not held for up to 8s - context overflow, auth failure and rate limit messages say what to do - --list shows id/model/messages/age without needing a key - --session creates the session when the id does not exist - probes recorded: session header is mandatory, consecutive user messages allowed --- src/session.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'src/session.rs') diff --git a/src/session.rs b/src/session.rs index 7090dcf..9f2b699 100644 --- a/src/session.rs +++ b/src/session.rs @@ -48,9 +48,22 @@ pub struct Session { file: File, } +pub struct SessionInfo { + pub id: String, + pub model: String, + pub messages: usize, + pub modified: SystemTime, + pub path: PathBuf, +} + impl Session { pub fn create(model: &str, system: &Value) -> Result { - let id = new_id(); + Self::create_as(new_id(), model, system) + } + + /// Creates a session with a caller-chosen id, for `--session ` on a + /// conversation that does not exist yet. + pub fn create_as(id: String, model: &str, system: &Value) -> Result { let dir = sessions_dir(); std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; let path = dir.join(format!("{id}.jsonl")); @@ -163,6 +176,69 @@ impl Session { } } +pub fn exists(id: &str) -> bool { + sessions_dir().join(format!("{id}.jsonl")).is_file() +} + +/// Every session, most recently used first. Reads each file once; fine for the +/// handful of conversations a person accumulates. +pub fn list() -> Result, String> { + let dir = sessions_dir(); + let entries = match std::fs::read_dir(&dir) { + Ok(entries) => entries, + Err(_) => return Ok(Vec::new()), + }; + + let mut sessions = Vec::new(); + for entry in entries.flatten() { + let path = entry.path(); + if path.extension().and_then(|e| e.to_str()) != Some("jsonl") { + continue; + } + let id = match path.file_stem().and_then(|s| s.to_str()) { + Some(id) => id.to_string(), + None => continue, + }; + let modified = entry + .metadata() + .and_then(|m| m.modified()) + .unwrap_or(UNIX_EPOCH); + let (model, messages) = scan(&path); + sessions.push(SessionInfo { + id, + model, + messages, + modified, + path, + }); + } + sessions.sort_by(|a, b| b.modified.cmp(&a.modified)); + Ok(sessions) +} + +fn scan(path: &PathBuf) -> (String, usize) { + let mut model = String::from("?"); + let mut messages = 0usize; + if let Ok(file) = File::open(path) { + for line in BufReader::new(file).lines().map_while(Result::ok) { + let value: Value = match serde_json::from_str(&line) { + Ok(value) => value, + Err(_) => continue, + }; + match value.get("t").and_then(|t| t.as_str()) { + Some("header") => { + if let Some(name) = value.get("model").and_then(|m| m.as_str()) { + model = name.to_string(); + } + } + Some("msg") => messages += 1, + _ => {} + } + } + } + (model, messages) +} + /// Most recently written session, for --continue. pub fn latest() -> Result, String> { let dir = sessions_dir(); -- cgit v1.2.3