diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 17:13:24 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 17:14:22 +0100 |
| commit | b7b01494926a134f39daf8102281889702ddf62b (patch) | |
| tree | 813aeefd68352968b363aa7328f81eec796b8287 /src | |
| parent | 0.5.2: lazy-wrap redraw, and reasoning ends its own line (diff) | |
| download | agent686-b7b01494926a134f39daf8102281889702ddf62b.tar.gz agent686-b7b01494926a134f39daf8102281889702ddf62b.zip | |
0.5.3: session locking, UTF-8 and size guards for edit, correct character widths
- exclusive flock on session files
- edit refuses non-UTF-8 and >64 MiB files with actionable messages
- editor uses unicode-width, expands tabs, handles resize
Diffstat (limited to '')
| -rw-r--r-- | src/line.rs | 45 | ||||
| -rw-r--r-- | src/session.rs | 15 | ||||
| -rw-r--r-- | src/tools/fs.rs | 17 |
3 files changed, 75 insertions, 2 deletions
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 }) |
