diff options
Diffstat (limited to '')
| -rw-r--r-- | CHANGELOG.md | 15 | ||||
| -rw-r--r-- | Cargo.lock | 9 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/line.rs | 45 | ||||
| -rw-r--r-- | src/session.rs | 15 | ||||
| -rw-r--r-- | src/tools/fs.rs | 17 | ||||
| -rw-r--r-- | tests/loop.rs | 7 | ||||
| -rw-r--r-- | tests/sessions.rs | 22 |
8 files changed, 129 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 38d38e2..7d63d98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
| @@ -7,6 +7,21 @@ break the interface, **PATCH** for bug fixes. | |||
| 7 | file, and the tool schemas the model sees. Anything a script or a resumed session | 7 | file, and the tool schemas the model sees. Anything a script or a resumed session |
| 8 | could depend on counts as API. | 8 | could depend on counts as API. |
| 9 | 9 | ||
| 10 | ## 0.5.3 | ||
| 11 | |||
| 12 | ### Fixed | ||
| 13 | - Two processes could append to the same session and interleave messages, which | ||
| 14 | corrupts a transcript permanently. A session now takes an exclusive `flock` and | ||
| 15 | the second process is told the session is already open. | ||
| 16 | - `edit` on a file that is not valid UTF-8 failed with a raw codec error after | ||
| 17 | reading it; it now says so up front and points at `bash`. Files over 64 MiB are | ||
| 18 | refused the same way, instead of failing mid-rewrite on the 32-bit target. | ||
| 19 | - The line editor counted every character as one column, so CJK text, combining | ||
| 20 | marks and pasted tabs put the caret in the wrong place. Widths now come from | ||
| 21 | `unicode-width`, with tabs expanded to the next multiple of eight. | ||
| 22 | - A terminal resize during editing left the redraw's row count stale and could | ||
| 23 | erase output above the prompt; the editor now starts a fresh prompt line. | ||
| 24 | |||
| 10 | ## 0.5.2 | 25 | ## 0.5.2 |
| 11 | 26 | ||
| 12 | ### Fixed | 27 | ### Fixed |
| @@ -10,13 +10,14 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" | |||
| 10 | 10 | ||
| 11 | [[package]] | 11 | [[package]] |
| 12 | name = "agent686" | 12 | name = "agent686" |
| 13 | version = "0.5.2" | 13 | version = "0.5.3" |
| 14 | dependencies = [ | 14 | dependencies = [ |
| 15 | "crossterm", | 15 | "crossterm", |
| 16 | "libc", | 16 | "libc", |
| 17 | "regex", | 17 | "regex", |
| 18 | "serde", | 18 | "serde", |
| 19 | "serde_json", | 19 | "serde_json", |
| 20 | "unicode-width", | ||
| 20 | "ureq", | 21 | "ureq", |
| 21 | ] | 22 | ] |
| 22 | 23 | ||
| @@ -567,6 +568,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 567 | checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" | 568 | checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" |
| 568 | 569 | ||
| 569 | [[package]] | 570 | [[package]] |
| 571 | name = "unicode-width" | ||
| 572 | version = "0.2.3" | ||
| 573 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 574 | checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" | ||
| 575 | |||
| 576 | [[package]] | ||
| 570 | name = "untrusted" | 577 | name = "untrusted" |
| 571 | version = "0.9.0" | 578 | version = "0.9.0" |
| 572 | source = "registry+https://github.com/rust-lang/crates.io-index" | 579 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -1,6 +1,6 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "agent686" | 2 | name = "agent686" |
| 3 | version = "0.5.2" | 3 | version = "0.5.3" |
| 4 | edition = "2021" | 4 | edition = "2021" |
| 5 | description = "A small coding agent for a 32-bit Debian box" | 5 | description = "A small coding agent for a 32-bit Debian box" |
| 6 | publish = false | 6 | publish = false |
| @@ -12,6 +12,7 @@ ureq = { version = "3", features = ["rustls"] } | |||
| 12 | libc = "0.2" | 12 | libc = "0.2" |
| 13 | crossterm = "0.29.0" | 13 | crossterm = "0.29.0" |
| 14 | regex = "1.13.1" | 14 | regex = "1.13.1" |
| 15 | unicode-width = "0.2.3" | ||
| 15 | 16 | ||
| 16 | [profile.release] | 17 | [profile.release] |
| 17 | strip = true | 18 | strip = true |
diff --git a/src/line.rs b/src/line.rs index accf6cd..1ae210a 100644 --- a/src/line.rs +++ b/src/line.rs | |||
| @@ -57,6 +57,14 @@ impl Editor { | |||
| 57 | insert(&mut buffer, &mut cursor, &text); | 57 | insert(&mut buffer, &mut cursor, &text); |
| 58 | draw(prompt, &buffer, cursor, &mut rows)?; | 58 | draw(prompt, &buffer, cursor, &mut rows)?; |
| 59 | } | 59 | } |
| 60 | Event::Resize(_, _) => { | ||
| 61 | // The terminal reflowed the block we were tracking, so the | ||
| 62 | // old row count is meaningless. Start a fresh prompt line | ||
| 63 | // rather than risk erasing output above it. | ||
| 64 | println!(); | ||
| 65 | rows = 1; | ||
| 66 | draw(prompt, &buffer, cursor, &mut rows)?; | ||
| 67 | } | ||
| 60 | Event::Key(key) if key.kind == KeyEventKind::Press => { | 68 | Event::Key(key) if key.kind == KeyEventKind::Press => { |
| 61 | match handle_key(key, &mut buffer, &mut cursor, self) { | 69 | match handle_key(key, &mut buffer, &mut cursor, self) { |
| 62 | Action::Continue => {} | 70 | Action::Continue => {} |
| @@ -266,7 +274,17 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u | |||
| 266 | col = 0; | 274 | col = 0; |
| 267 | pending_wrap = false; | 275 | pending_wrap = false; |
| 268 | } | 276 | } |
| 269 | col += 1; | 277 | |
| 278 | let cells = cells(ch, col); | ||
| 279 | if cells == 0 { | ||
| 280 | // Combining marks draw on top of the previous cell. | ||
| 281 | continue; | ||
| 282 | } | ||
| 283 | if col + cells > width { | ||
| 284 | row += 1; | ||
| 285 | col = 0; | ||
| 286 | } | ||
| 287 | col += cells; | ||
| 270 | if col >= width { | 288 | if col >= width { |
| 271 | col = width - 1; | 289 | col = width - 1; |
| 272 | pending_wrap = true; | 290 | pending_wrap = true; |
| @@ -276,6 +294,15 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u | |||
| 276 | (row, col) | 294 | (row, col) |
| 277 | } | 295 | } |
| 278 | 296 | ||
| 297 | /// Columns a character occupies at the given cursor position. Terminals expand | ||
| 298 | /// a tab to the next multiple of eight; wide characters occupy two cells. | ||
| 299 | fn cells(ch: char, col: usize) -> usize { | ||
| 300 | if ch == '\t' { | ||
| 301 | return 8 - (col % 8); | ||
| 302 | } | ||
| 303 | unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) | ||
| 304 | } | ||
| 305 | |||
| 279 | fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { | 306 | fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { |
| 280 | let width = terminal_width(); | 307 | let width = terminal_width(); |
| 281 | let mut out = io::stdout(); | 308 | let mut out = io::stdout(); |
| @@ -407,6 +434,22 @@ mod tests { | |||
| 407 | } | 434 | } |
| 408 | 435 | ||
| 409 | #[test] | 436 | #[test] |
| 437 | fn layout_counts_wide_and_combining_characters() { | ||
| 438 | let wide: Vec<char> = "日本語".chars().collect(); | ||
| 439 | assert_eq!(layout("> ", &wide, 3, 20), (0, 8)); | ||
| 440 | |||
| 441 | let combining: Vec<char> = "e\u{0301}".chars().collect(); | ||
| 442 | assert_eq!(layout("", &combining, 2, 20), (0, 1)); | ||
| 443 | } | ||
| 444 | |||
| 445 | #[test] | ||
| 446 | fn layout_expands_tabs_to_the_next_stop() { | ||
| 447 | let tab: Vec<char> = "\t".chars().collect(); | ||
| 448 | assert_eq!(layout("> ", &tab, 1, 80), (0, 8)); | ||
| 449 | assert_eq!(layout("", &tab, 1, 80), (0, 8)); | ||
| 450 | } | ||
| 451 | |||
| 452 | #[test] | ||
| 410 | fn insert_normalises_line_endings() { | 453 | fn insert_normalises_line_endings() { |
| 411 | let mut buffer = vec!['a']; | 454 | let mut buffer = vec!['a']; |
| 412 | let mut cursor = 1; | 455 | let mut cursor = 1; |
diff --git a/src/session.rs b/src/session.rs index 6e3c1c1..6416b56 100644 --- a/src/session.rs +++ b/src/session.rs | |||
| @@ -2,6 +2,7 @@ use std::fs::{File, OpenOptions}; | |||
| 2 | use std::hash::{Hash, Hasher}; | 2 | use std::hash::{Hash, Hasher}; |
| 3 | use std::io::{BufRead, BufReader, Read, Write}; | 3 | use std::io::{BufRead, BufReader, Read, Write}; |
| 4 | use std::os::unix::fs::PermissionsExt; | 4 | use std::os::unix::fs::PermissionsExt; |
| 5 | use std::os::unix::io::AsRawFd; | ||
| 5 | use std::path::Path; | 6 | use std::path::Path; |
| 6 | use std::path::PathBuf; | 7 | use std::path::PathBuf; |
| 7 | use std::time::{SystemTime, UNIX_EPOCH}; | 8 | use std::time::{SystemTime, UNIX_EPOCH}; |
| @@ -48,6 +49,18 @@ fn restrict(path: &Path, mode: u32) { | |||
| 48 | let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)); | 49 | let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)); |
| 49 | } | 50 | } |
| 50 | 51 | ||
| 52 | /// Two processes appending to one transcript interleave messages and corrupt it | ||
| 53 | /// permanently, because history is never rewritten. Refuse the second one. | ||
| 54 | fn lock_exclusive(file: &File, id: &str) -> Result<(), String> { | ||
| 55 | let locked = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; | ||
| 56 | if locked != 0 { | ||
| 57 | return Err(format!( | ||
| 58 | "session {id} is already open in another process" | ||
| 59 | )); | ||
| 60 | } | ||
| 61 | Ok(()) | ||
| 62 | } | ||
| 63 | |||
| 51 | pub struct Session { | 64 | pub struct Session { |
| 52 | pub id: String, | 65 | pub id: String, |
| 53 | pub model: String, | 66 | pub model: String, |
| @@ -84,6 +97,7 @@ impl Session { | |||
| 84 | .open(&path) | 97 | .open(&path) |
| 85 | .map_err(|e| format!("{}: {e}", path.display()))?; | 98 | .map_err(|e| format!("{}: {e}", path.display()))?; |
| 86 | restrict(&path, 0o600); | 99 | restrict(&path, 0o600); |
| 100 | lock_exclusive(&file, &id)?; | ||
| 87 | 101 | ||
| 88 | let cwd = std::env::current_dir() | 102 | let cwd = std::env::current_dir() |
| 89 | .map(|path| path.display().to_string()) | 103 | .map(|path| path.display().to_string()) |
| @@ -164,6 +178,7 @@ impl Session { | |||
| 164 | .map_err(|e| format!("{}: {e}", path.display()))?; | 178 | .map_err(|e| format!("{}: {e}", path.display()))?; |
| 165 | // Older builds created these 0644. | 179 | // Older builds created these 0644. |
| 166 | restrict(&path, 0o600); | 180 | restrict(&path, 0o600); |
| 181 | lock_exclusive(&file, &session_id)?; | ||
| 167 | 182 | ||
| 168 | Ok(( | 183 | Ok(( |
| 169 | Session { | 184 | Session { |
diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 947649f..ac0a475 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs | |||
| @@ -10,6 +10,9 @@ const MAX_LINE_BYTES: usize = 4096; | |||
| 10 | const MAX_READ_LINES: usize = 2000; | 10 | const MAX_READ_LINES: usize = 2000; |
| 11 | const MAX_MATCHES: usize = 200; | 11 | const MAX_MATCHES: usize = 200; |
| 12 | const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; | 12 | const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; |
| 13 | /// Edit rewrites the whole file, so it has to fit comfortably in the 32-bit | ||
| 14 | /// address space together with its own copy. | ||
| 15 | const MAX_EDIT_BYTES: u64 = 64 * 1024 * 1024; | ||
| 13 | const PREVIEW_BUDGET: usize = 60; | 16 | const PREVIEW_BUDGET: usize = 60; |
| 14 | 17 | ||
| 15 | const SKIP_DIRS: &[&str] = &[ | 18 | const SKIP_DIRS: &[&str] = &[ |
| @@ -239,7 +242,19 @@ pub fn plan_edit(args: &Value) -> Result<Change, String> { | |||
| 239 | if old.is_empty() { | 242 | if old.is_empty() { |
| 240 | return Err("`old` must not be empty; use write to create a file".into()); | 243 | return Err("`old` must not be empty; use write to create a file".into()); |
| 241 | } | 244 | } |
| 242 | let text = fs::read_to_string(&path).map_err(|e| format!("{path}: {e}"))?; | 245 | let bytes = fs::read(&path).map_err(|e| format!("{path}: {e}"))?; |
| 246 | if bytes.len() as u64 > MAX_EDIT_BYTES { | ||
| 247 | return Err(format!( | ||
| 248 | "{path} is {} MiB, too large to edit safely; use bash (sed) instead", | ||
| 249 | bytes.len() / (1024 * 1024) | ||
| 250 | )); | ||
| 251 | } | ||
| 252 | let text = String::from_utf8(bytes).map_err(|_| { | ||
| 253 | format!( | ||
| 254 | "{path} is not valid UTF-8, so edit cannot round-trip it without \ | ||
| 255 | corrupting the file; use bash (sed, perl) instead" | ||
| 256 | ) | ||
| 257 | })?; | ||
| 243 | // Fail before the approval prompt if the match is unusable. | 258 | // Fail before the approval prompt if the match is unusable. |
| 244 | locate(&text, &old)?; | 259 | locate(&text, &old)?; |
| 245 | Ok(Change::Edit { path, old, new }) | 260 | Ok(Change::Edit { path, old, new }) |
diff --git a/tests/loop.rs b/tests/loop.rs index ac137da..d6df43c 100644 --- a/tests/loop.rs +++ b/tests/loop.rs | |||
| @@ -7,6 +7,9 @@ use std::thread; | |||
| 7 | 7 | ||
| 8 | use serde_json::json; | 8 | use serde_json::json; |
| 9 | 9 | ||
| 10 | /// Tests in this binary share process-wide environment variables. | ||
| 11 | static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); | ||
| 12 | |||
| 10 | use agent686::agent; | 13 | use agent686::agent; |
| 11 | use agent686::config::Config; | 14 | use agent686::config::Config; |
| 12 | use agent686::session::Session; | 15 | use agent686::session::Session; |
| @@ -57,6 +60,7 @@ fn temp_dir(tag: &str) -> PathBuf { | |||
| 57 | 60 | ||
| 58 | #[test] | 61 | #[test] |
| 59 | fn runs_a_tool_call_then_finishes_and_records_every_message() { | 62 | fn runs_a_tool_call_then_finishes_and_records_every_message() { |
| 63 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 60 | let dir = temp_dir("write"); | 64 | let dir = temp_dir("write"); |
| 61 | let target = dir.join("hello.txt"); | 65 | let target = dir.join("hello.txt"); |
| 62 | let arguments = json!({ | 66 | let arguments = json!({ |
| @@ -128,6 +132,7 @@ fn runs_a_tool_call_then_finishes_and_records_every_message() { | |||
| 128 | 132 | ||
| 129 | #[test] | 133 | #[test] |
| 130 | fn a_denied_tool_call_is_returned_as_data() { | 134 | fn a_denied_tool_call_is_returned_as_data() { |
| 135 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 131 | let dir = temp_dir("denied"); | 136 | let dir = temp_dir("denied"); |
| 132 | let target = dir.join("never.txt"); | 137 | let target = dir.join("never.txt"); |
| 133 | let arguments = json!({ | 138 | let arguments = json!({ |
| @@ -201,6 +206,7 @@ fn tool_call(index: usize, id: &str, name: &str, arguments: serde_json::Value) - | |||
| 201 | 206 | ||
| 202 | #[test] | 207 | #[test] |
| 203 | fn aborting_between_tool_calls_answers_every_call() { | 208 | fn aborting_between_tool_calls_answers_every_call() { |
| 209 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 204 | let dir = temp_dir("abort-mid-calls"); | 210 | let dir = temp_dir("abort-mid-calls"); |
| 205 | let slow = sse(&format!( | 211 | let slow = sse(&format!( |
| 206 | "data: {}\n\ndata: [DONE]\n\n", | 212 | "data: {}\n\ndata: [DONE]\n\n", |
| @@ -271,6 +277,7 @@ fn aborting_between_tool_calls_answers_every_call() { | |||
| 271 | 277 | ||
| 272 | #[test] | 278 | #[test] |
| 273 | fn opening_a_session_with_unanswered_calls_repairs_it() { | 279 | fn opening_a_session_with_unanswered_calls_repairs_it() { |
| 280 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 274 | let dir = temp_dir("repair"); | 281 | let dir = temp_dir("repair"); |
| 275 | std::env::set_var("AGENT686_DATA", dir.join("data")); | 282 | std::env::set_var("AGENT686_DATA", dir.join("data")); |
| 276 | 283 | ||
diff --git a/tests/sessions.rs b/tests/sessions.rs index b445242..f21be24 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs | |||
| @@ -4,6 +4,9 @@ use std::process::Command; | |||
| 4 | 4 | ||
| 5 | use serde_json::json; | 5 | use serde_json::json; |
| 6 | 6 | ||
| 7 | /// Tests in this binary share process-wide environment variables. | ||
| 8 | static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); | ||
| 9 | |||
| 7 | use agent686::session::{self, Session}; | 10 | use agent686::session::{self, Session}; |
| 8 | 11 | ||
| 9 | fn temp_dir(tag: &str) -> PathBuf { | 12 | fn temp_dir(tag: &str) -> PathBuf { |
| @@ -15,6 +18,7 @@ fn temp_dir(tag: &str) -> PathBuf { | |||
| 15 | 18 | ||
| 16 | #[test] | 19 | #[test] |
| 17 | fn sessions_create_list_and_replay() { | 20 | fn sessions_create_list_and_replay() { |
| 21 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 18 | let dir = temp_dir("roundtrip"); | 22 | let dir = temp_dir("roundtrip"); |
| 19 | std::env::set_var("AGENT686_DATA", &dir); | 23 | std::env::set_var("AGENT686_DATA", &dir); |
| 20 | 24 | ||
| @@ -50,6 +54,8 @@ fn sessions_create_list_and_replay() { | |||
| 50 | assert_eq!(listed[0].model, "model-a"); | 54 | assert_eq!(listed[0].model, "model-a"); |
| 51 | assert_eq!(listed[0].messages, 2); | 55 | assert_eq!(listed[0].messages, 2); |
| 52 | 56 | ||
| 57 | // A session holds an exclusive lock while open. | ||
| 58 | drop(alpha); | ||
| 53 | let (reopened, system_back, messages) = Session::open("alpha").unwrap(); | 59 | let (reopened, system_back, messages) = Session::open("alpha").unwrap(); |
| 54 | assert_eq!(reopened.id, "alpha"); | 60 | assert_eq!(reopened.id, "alpha"); |
| 55 | assert_eq!(system_back["content"], "frozen"); | 61 | assert_eq!(system_back["content"], "frozen"); |
| @@ -73,3 +79,19 @@ fn cli_lists_sessions_without_a_key() { | |||
| 73 | let stdout = String::from_utf8_lossy(&output.stdout); | 79 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 74 | assert!(stdout.contains("no sessions"), "{stdout}"); | 80 | assert!(stdout.contains("no sessions"), "{stdout}"); |
| 75 | } | 81 | } |
| 82 | |||
| 83 | #[test] | ||
| 84 | fn a_session_cannot_be_open_twice() { | ||
| 85 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 86 | let dir = temp_dir("lock"); | ||
| 87 | std::env::set_var("AGENT686_DATA", &dir); | ||
| 88 | |||
| 89 | let system = json!({ "role": "system", "content": "x" }); | ||
| 90 | let first = Session::create_as("locked".into(), "m", &system).unwrap(); | ||
| 91 | |||
| 92 | let error = Session::open("locked").err().expect("second open must fail"); | ||
| 93 | assert!(error.contains("already open"), "{error}"); | ||
| 94 | |||
| 95 | drop(first); | ||
| 96 | Session::open("locked").expect("after the first handle closes it is available"); | ||
| 97 | } | ||
