aboutsummaryrefslogtreecommitdiffstats
path: root/src/session.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-18 12:28:29 +0100
committersalmonik <salmonik@tuta.io>2026-11-18 12:28:29 +0100
commit84e70c0e73460e5db1297c47d02d9b76802bfdf7 (patch)
treeff0cd75141a28d7112b4f82b2c74bdba91b332a3 /src/session.rs
parent0.9.0: distinguishable tool errors, separated stderr, configurable limits (diff)
downloadagent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.tar.gz
agent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.zip
0.9.1: correctness fixes from the audit
- 64 MiB limit detected by file length, so pipelines and redirects are reported - bash temp files removed on every path via a scope guard - validate requires content/new as the schemas declare (present, may be empty) - Session::open folds in model-switch records; resume matches --list - combining mark at a wrap boundary no longer counts as a new row - --flag=value accepted; flag-shaped values rejected - dead code removed; last_reply folded into open (one read per resume)
Diffstat (limited to 'src/session.rs')
-rw-r--r--src/session.rs75
1 files changed, 29 insertions, 46 deletions
diff --git a/src/session.rs b/src/session.rs
index a1da28f..881b1ed 100644
--- a/src/session.rs
+++ b/src/session.rs
@@ -68,6 +68,9 @@ pub struct Session {
68 pub cwd: String, 68 pub cwd: String,
69 /// Harness version that created the session, for prefix-change warnings. 69 /// Harness version that created the session, for prefix-change warnings.
70 pub version: String, 70 pub version: String,
71 /// Last assistant block, for the resume banner. Computed during the same pass
72 /// that loads messages, so resume does not read the file twice.
73 pub last_reply: Option<LastReply>,
71 path: PathBuf, 74 path: PathBuf,
72 file: File, 75 file: File,
73} 76}
@@ -121,6 +124,7 @@ impl Session {
121 model: model.to_string(), 124 model: model.to_string(),
122 cwd, 125 cwd,
123 version: env!("CARGO_PKG_VERSION").to_string(), 126 version: env!("CARGO_PKG_VERSION").to_string(),
127 last_reply: None,
124 path, 128 path,
125 file, 129 file,
126 }) 130 })
@@ -138,6 +142,8 @@ impl Session {
138 let mut cwd = String::new(); 142 let mut cwd = String::new();
139 let mut version = String::new(); 143 let mut version = String::new();
140 let mut messages = Vec::new(); 144 let mut messages = Vec::new();
145 let mut last_content: Option<LastReply> = None;
146 let mut last_reasoning: Option<LastReply> = None;
141 147
142 for line in raw.split(|&byte| byte == b'\n') { 148 for line in raw.split(|&byte| byte == b'\n') {
143 if line.is_empty() { 149 if line.is_empty() {
@@ -168,7 +174,29 @@ impl Session {
168 version = v.to_string(); 174 version = v.to_string();
169 } 175 }
170 } 176 }
177 Some("model") => {
178 if let Some(name) = value.get("model").and_then(|m| m.as_str()) {
179 model = name.to_string();
180 }
181 }
171 Some("msg") => { 182 Some("msg") => {
183 if value.get("role").and_then(|r| r.as_str()) == Some("assistant") {
184 let content = value
185 .get("content")
186 .and_then(|c| c.as_str())
187 .unwrap_or("")
188 .to_string();
189 let reasoning = value
190 .get("_reasoning")
191 .and_then(|r| r.as_str())
192 .unwrap_or("")
193 .to_string();
194 if !content.trim().is_empty() {
195 last_content = Some(LastReply { content, reasoning });
196 } else if !reasoning.trim().is_empty() {
197 last_reasoning = Some(LastReply { content, reasoning });
198 }
199 }
172 let mut message = value.clone(); 200 let mut message = value.clone();
173 if let Some(object) = message.as_object_mut() { 201 if let Some(object) = message.as_object_mut() {
174 object.remove("t"); 202 object.remove("t");
@@ -194,6 +222,7 @@ impl Session {
194 model, 222 model,
195 cwd, 223 cwd,
196 version, 224 version,
225 last_reply: last_content.or(last_reasoning),
197 path, 226 path,
198 file, 227 file,
199 }, 228 },
@@ -237,52 +266,6 @@ pub struct LastReply {
237 pub reasoning: String, 266 pub reasoning: String,
238} 267}
239 268
240/// The last thing the agent said, for the resume banner. Prefers a real answer;
241/// falls back to reasoning when the last block had no content.
242pub fn last_reply(id: &str) -> Option<LastReply> {
243 let raw = std::fs::read(sessions_dir().join(format!("{id}.jsonl"))).ok()?;
244 let mut with_content: Option<LastReply> = None;
245 let mut with_reasoning: Option<LastReply> = None;
246
247 for line in raw.split(|&byte| byte == b'\n') {
248 if line.is_empty() {
249 continue;
250 }
251 let text = String::from_utf8_lossy(line);
252 let Ok(value) = serde_json::from_str::<Value>(&text) else {
253 continue;
254 };
255 if value.get("role").and_then(|role| role.as_str()) != Some("assistant") {
256 continue;
257 }
258 let content = value
259 .get("content")
260 .and_then(|c| c.as_str())
261 .unwrap_or("")
262 .to_string();
263 let reasoning = value
264 .get("_reasoning")
265 .and_then(|r| r.as_str())
266 .unwrap_or("")
267 .to_string();
268 let reply = LastReply {
269 content,
270 reasoning,
271 };
272 if !reply.content.trim().is_empty() {
273 with_content = Some(LastReply {
274 content: reply.content.clone(),
275 reasoning: reply.reasoning.clone(),
276 });
277 }
278 if !reply.reasoning.trim().is_empty() {
279 with_reasoning = Some(reply);
280 }
281 }
282
283 with_content.or(with_reasoning)
284}
285
286pub fn exists(id: &str) -> bool { 269pub fn exists(id: &str) -> bool {
287 sessions_dir().join(format!("{id}.jsonl")).is_file() 270 sessions_dir().join(format!("{id}.jsonl")).is_file()
288} 271}