diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 14:59:31 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 14:59:31 +0100 |
| commit | f192c40b2a3643a9ab97303310db8dbc7547ee35 (patch) | |
| tree | 1b2133afa1b3318f8c0da032a7da278691e577ca /src/session.rs | |
| parent | M1-M5: sessions, six tools, approvals, prompt, Ctrl-C, target gate (diff) | |
| download | agent686-f192c40b2a3643a9ab97303310db8dbc7547ee35.tar.gz agent686-f192c40b2a3643a9ab97303310db8dbc7547ee35.zip | |
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 <id> creates the session when the id does not exist
- probes recorded: session header is mandatory, consecutive user messages allowed
Diffstat (limited to 'src/session.rs')
| -rw-r--r-- | src/session.rs | 78 |
1 files changed, 77 insertions, 1 deletions
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 { | |||
| 48 | file: File, | 48 | file: File, |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | pub struct SessionInfo { | ||
| 52 | pub id: String, | ||
| 53 | pub model: String, | ||
| 54 | pub messages: usize, | ||
| 55 | pub modified: SystemTime, | ||
| 56 | pub path: PathBuf, | ||
| 57 | } | ||
| 58 | |||
| 51 | impl Session { | 59 | impl Session { |
| 52 | pub fn create(model: &str, system: &Value) -> Result<Session, String> { | 60 | pub fn create(model: &str, system: &Value) -> Result<Session, String> { |
| 53 | let id = new_id(); | 61 | Self::create_as(new_id(), model, system) |
| 62 | } | ||
| 63 | |||
| 64 | /// Creates a session with a caller-chosen id, for `--session <id>` on a | ||
| 65 | /// conversation that does not exist yet. | ||
| 66 | pub fn create_as(id: String, model: &str, system: &Value) -> Result<Session, String> { | ||
| 54 | let dir = sessions_dir(); | 67 | let dir = sessions_dir(); |
| 55 | std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; | 68 | std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| 56 | let path = dir.join(format!("{id}.jsonl")); | 69 | let path = dir.join(format!("{id}.jsonl")); |
| @@ -163,6 +176,69 @@ impl Session { | |||
| 163 | } | 176 | } |
| 164 | } | 177 | } |
| 165 | 178 | ||
| 179 | pub fn exists(id: &str) -> bool { | ||
| 180 | sessions_dir().join(format!("{id}.jsonl")).is_file() | ||
| 181 | } | ||
| 182 | |||
| 183 | /// Every session, most recently used first. Reads each file once; fine for the | ||
| 184 | /// handful of conversations a person accumulates. | ||
| 185 | pub fn list() -> Result<Vec<SessionInfo>, String> { | ||
| 186 | let dir = sessions_dir(); | ||
| 187 | let entries = match std::fs::read_dir(&dir) { | ||
| 188 | Ok(entries) => entries, | ||
| 189 | Err(_) => return Ok(Vec::new()), | ||
| 190 | }; | ||
| 191 | |||
| 192 | let mut sessions = Vec::new(); | ||
| 193 | for entry in entries.flatten() { | ||
| 194 | let path = entry.path(); | ||
| 195 | if path.extension().and_then(|e| e.to_str()) != Some("jsonl") { | ||
| 196 | continue; | ||
| 197 | } | ||
| 198 | let id = match path.file_stem().and_then(|s| s.to_str()) { | ||
| 199 | Some(id) => id.to_string(), | ||
| 200 | None => continue, | ||
| 201 | }; | ||
| 202 | let modified = entry | ||
| 203 | .metadata() | ||
| 204 | .and_then(|m| m.modified()) | ||
| 205 | .unwrap_or(UNIX_EPOCH); | ||
| 206 | let (model, messages) = scan(&path); | ||
| 207 | sessions.push(SessionInfo { | ||
| 208 | id, | ||
| 209 | model, | ||
| 210 | messages, | ||
| 211 | modified, | ||
| 212 | path, | ||
| 213 | }); | ||
| 214 | } | ||
| 215 | sessions.sort_by(|a, b| b.modified.cmp(&a.modified)); | ||
| 216 | Ok(sessions) | ||
| 217 | } | ||
| 218 | |||
| 219 | fn scan(path: &PathBuf) -> (String, usize) { | ||
| 220 | let mut model = String::from("?"); | ||
| 221 | let mut messages = 0usize; | ||
| 222 | if let Ok(file) = File::open(path) { | ||
| 223 | for line in BufReader::new(file).lines().map_while(Result::ok) { | ||
| 224 | let value: Value = match serde_json::from_str(&line) { | ||
| 225 | Ok(value) => value, | ||
| 226 | Err(_) => continue, | ||
| 227 | }; | ||
| 228 | match value.get("t").and_then(|t| t.as_str()) { | ||
| 229 | Some("header") => { | ||
| 230 | if let Some(name) = value.get("model").and_then(|m| m.as_str()) { | ||
| 231 | model = name.to_string(); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | Some("msg") => messages += 1, | ||
| 235 | _ => {} | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | (model, messages) | ||
| 240 | } | ||
| 241 | |||
| 166 | /// Most recently written session, for --continue. | 242 | /// Most recently written session, for --continue. |
| 167 | pub fn latest() -> Result<Option<String>, String> { | 243 | pub fn latest() -> Result<Option<String>, String> { |
| 168 | let dir = sessions_dir(); | 244 | let dir = sessions_dir(); |
