diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 18:00:12 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 18:00:12 +0100 |
| commit | 8ae5bd48cf233315b5f36a7a6b39002a5e77f74b (patch) | |
| tree | 71c9b71f90f3cc64544dd506041468ee9bcbcda1 /src/session.rs | |
| parent | 0.6.2: resizing no longer duplicates the prompt (diff) | |
| download | agent686-8ae5bd48cf233315b5f36a7a6b39002a5e77f74b.tar.gz agent686-8ae5bd48cf233315b5f36a7a6b39002a5e77f74b.zip | |
0.7.0: resume shows where you left off; no approval for unparsable calls
- --continue prints the last answer, or the last reasoning block if there was none
- tool arguments are validated before prompting, so an empty bash call becomes an
error for the model instead of a blank approval
- empty placeholder tool calls are dropped and reported
- sessions tolerate invalid UTF-8 lines from a torn write
- AGENT686_DEBUG_STREAM=1 dumps SSE frames
Diffstat (limited to '')
| -rw-r--r-- | src/session.rs | 66 |
1 files changed, 59 insertions, 7 deletions
diff --git a/src/session.rs b/src/session.rs index d017eda..8e0ef02 100644 --- a/src/session.rs +++ b/src/session.rs | |||
| @@ -130,8 +130,8 @@ impl Session { | |||
| 130 | /// that was frozen when the session started. | 130 | /// that was frozen when the session started. |
| 131 | pub fn open(id: &str) -> Result<(Session, Value, Vec<Value>), String> { | 131 | pub fn open(id: &str) -> Result<(Session, Value, Vec<Value>), String> { |
| 132 | let path = sessions_dir().join(format!("{id}.jsonl")); | 132 | let path = sessions_dir().join(format!("{id}.jsonl")); |
| 133 | let file = File::open(&path).map_err(|e| format!("{}: {e}", path.display()))?; | 133 | let raw = std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?; |
| 134 | let reader = BufReader::new(file.try_clone().map_err(|e| e.to_string())?); | 134 | let _ = File::open(&path); |
| 135 | 135 | ||
| 136 | let mut model = String::new(); | 136 | let mut model = String::new(); |
| 137 | let mut system = Value::Null; | 137 | let mut system = Value::Null; |
| @@ -140,14 +140,15 @@ impl Session { | |||
| 140 | let mut version = String::new(); | 140 | let mut version = String::new(); |
| 141 | let mut messages = Vec::new(); | 141 | let mut messages = Vec::new(); |
| 142 | 142 | ||
| 143 | for line in reader.lines() { | 143 | for line in raw.split(|&byte| byte == b'\n') { |
| 144 | let line = line.map_err(|e| e.to_string())?; | 144 | if line.is_empty() { |
| 145 | if line.trim().is_empty() { | ||
| 146 | continue; | 145 | continue; |
| 147 | } | 146 | } |
| 148 | let value: Value = match serde_json::from_str(&line) { | 147 | // Lossy decoding: a torn write can split a multi-byte character, and |
| 148 | // one bad byte must not make the whole transcript unopenable. | ||
| 149 | let text = String::from_utf8_lossy(line); | ||
| 150 | let value: Value = match serde_json::from_str(&text) { | ||
| 149 | Ok(v) => v, | 151 | Ok(v) => v, |
| 150 | // A torn last line is expected after kill -9; ignore it. | ||
| 151 | Err(_) => continue, | 152 | Err(_) => continue, |
| 152 | }; | 153 | }; |
| 153 | match value.get("t").and_then(|t| t.as_str()) { | 154 | match value.get("t").and_then(|t| t.as_str()) { |
| @@ -232,6 +233,57 @@ impl Session { | |||
| 232 | } | 233 | } |
| 233 | } | 234 | } |
| 234 | 235 | ||
| 236 | pub struct LastReply { | ||
| 237 | pub content: String, | ||
| 238 | pub reasoning: String, | ||
| 239 | } | ||
| 240 | |||
| 241 | /// The last thing the agent said, for the resume banner. Prefers a real answer; | ||
| 242 | /// falls back to reasoning when the last block had no content. | ||
| 243 | pub fn last_reply(id: &str) -> Option<LastReply> { | ||
| 244 | let raw = std::fs::read(sessions_dir().join(format!("{id}.jsonl"))).ok()?; | ||
| 245 | let mut with_content: Option<LastReply> = None; | ||
| 246 | let mut with_reasoning: Option<LastReply> = None; | ||
| 247 | |||
| 248 | for line in raw.split(|&byte| byte == b'\n') { | ||
| 249 | if line.is_empty() { | ||
| 250 | continue; | ||
| 251 | } | ||
| 252 | let text = String::from_utf8_lossy(line); | ||
| 253 | let Ok(value) = serde_json::from_str::<Value>(&text) else { | ||
| 254 | continue; | ||
| 255 | }; | ||
| 256 | if value.get("role").and_then(|role| role.as_str()) != Some("assistant") { | ||
| 257 | continue; | ||
| 258 | } | ||
| 259 | let content = value | ||
| 260 | .get("content") | ||
| 261 | .and_then(|c| c.as_str()) | ||
| 262 | .unwrap_or("") | ||
| 263 | .to_string(); | ||
| 264 | let reasoning = value | ||
| 265 | .get("_reasoning") | ||
| 266 | .and_then(|r| r.as_str()) | ||
| 267 | .unwrap_or("") | ||
| 268 | .to_string(); | ||
| 269 | let reply = LastReply { | ||
| 270 | content, | ||
| 271 | reasoning, | ||
| 272 | }; | ||
| 273 | if !reply.content.trim().is_empty() { | ||
| 274 | with_content = Some(LastReply { | ||
| 275 | content: reply.content.clone(), | ||
| 276 | reasoning: reply.reasoning.clone(), | ||
| 277 | }); | ||
| 278 | } | ||
| 279 | if !reply.reasoning.trim().is_empty() { | ||
| 280 | with_reasoning = Some(reply); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | with_content.or(with_reasoning) | ||
| 285 | } | ||
| 286 | |||
| 235 | pub fn exists(id: &str) -> bool { | 287 | pub fn exists(id: &str) -> bool { |
| 236 | sessions_dir().join(format!("{id}.jsonl")).is_file() | 288 | sessions_dir().join(format!("{id}.jsonl")).is_file() |
| 237 | } | 289 | } |
