diff options
Diffstat (limited to '')
| -rw-r--r-- | CHANGELOG.md | 32 | ||||
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | src/tools/fs.rs | 156 | ||||
| -rw-r--r-- | tests/tools.rs | 63 |
6 files changed, 227 insertions, 34 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f68a04f --- /dev/null +++ b/CHANGELOG.md | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | # Changelog | ||
| 2 | |||
| 3 | Versioning: **MAJOR** for API-breaking changes, **MINOR** for features that do not | ||
| 4 | break the interface, **PATCH** for bug fixes. | ||
| 5 | |||
| 6 | "Interface" means: CLI flags and their meaning, the session file format, the config | ||
| 7 | file, and the tool schemas the model sees. Anything a script or a resumed session | ||
| 8 | could depend on counts as API. | ||
| 9 | |||
| 10 | ## 0.2.0 | ||
| 11 | |||
| 12 | ### Added | ||
| 13 | - `read` streams instead of loading the whole file. It seeks to `offset` with a | ||
| 14 | buffered reader and consumes long lines without keeping them, so memory stays | ||
| 15 | flat: 16 MB RSS reading a 200 MB file at any offset, 0.8 s to reach line | ||
| 16 | 999,000. | ||
| 17 | - Single lines longer than 4 KiB are clipped and marked `…(line truncated)`. | ||
| 18 | |||
| 19 | ### Changed | ||
| 20 | - The truncation marker is now `… more lines; continue with offset=N`. | ||
| 21 | |||
| 22 | ## 0.1.0 | ||
| 23 | |||
| 24 | Baseline: the harness as first built, before versioning was defined here. | ||
| 25 | |||
| 26 | - Streaming provider client for OpenCode Go (`chat/completions`), stable | ||
| 27 | `x-opencode-session` header, retry with backoff. | ||
| 28 | - Append-only JSONL sessions, `--continue`, `--session`, `--list`. | ||
| 29 | - Six tools: read, write, edit, bash, grep, glob. | ||
| 30 | - Approval: first edit of a session with a diff, every bash call. | ||
| 31 | - Line editor with history and bracketed paste. | ||
| 32 | - i686 release gate: `GLIBC_2.34` required, verified on `linux/386 debian:12`. | ||
| @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" | |||
| 10 | 10 | ||
| 11 | [[package]] | 11 | [[package]] |
| 12 | name = "agent686" | 12 | name = "agent686" |
| 13 | version = "0.1.0" | 13 | version = "0.2.0" |
| 14 | dependencies = [ | 14 | dependencies = [ |
| 15 | "crossterm", | 15 | "crossterm", |
| 16 | "libc", | 16 | "libc", |
| @@ -1,6 +1,6 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "agent686" | 2 | name = "agent686" |
| 3 | version = "0.1.0" | 3 | version = "0.2.0" |
| 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 |
| @@ -64,6 +64,12 @@ docker run --rm --platform linux/386 debian:12 /b/agent686 # target-shaped gat | |||
| 64 | inside UTF-8 characters are covered. `tests/loop.rs` proves a tool call is | 64 | inside UTF-8 characters are covered. `tests/loop.rs` proves a tool call is |
| 65 | executed, recorded, and that a denial is returned to the model as data. | 65 | executed, recorded, and that a denial is returned to the model as data. |
| 66 | 66 | ||
| 67 | ## Versioning | ||
| 68 | |||
| 69 | MAJOR for API-breaking changes, MINOR for features that do not break the | ||
| 70 | interface, PATCH for bug fixes. The interface is the CLI flags, the session file | ||
| 71 | format, the config file, and the tool schemas. See [CHANGELOG.md](CHANGELOG.md). | ||
| 72 | |||
| 67 | ## Design notes | 73 | ## Design notes |
| 68 | 74 | ||
| 69 | - [CONTEXT.md](CONTEXT.md) — what the words mean (session, turn, step, approval). | 75 | - [CONTEXT.md](CONTEXT.md) — what the words mean (session, turn, step, approval). |
diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 6addf8d..e543cb6 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs | |||
| @@ -1,9 +1,11 @@ | |||
| 1 | use std::fs; | 1 | use std::fs; |
| 2 | use std::io::{BufRead, BufReader}; | ||
| 2 | use std::path::{Path, PathBuf}; | 3 | use std::path::{Path, PathBuf}; |
| 3 | 4 | ||
| 4 | use serde_json::Value; | 5 | use serde_json::Value; |
| 5 | 6 | ||
| 6 | const MAX_LINE_CHARS: usize = 400; | 7 | const MAX_LINE_CHARS: usize = 400; |
| 8 | const MAX_LINE_BYTES: usize = 4096; | ||
| 7 | const MAX_READ_LINES: usize = 2000; | 9 | const MAX_READ_LINES: usize = 2000; |
| 8 | const MAX_MATCHES: usize = 200; | 10 | const MAX_MATCHES: usize = 200; |
| 9 | const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; | 11 | const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; |
| @@ -138,42 +140,134 @@ pub fn read(args: &Value) -> Result<String, String> { | |||
| 138 | .unwrap_or(MAX_READ_LINES as u64) | 140 | .unwrap_or(MAX_READ_LINES as u64) |
| 139 | .clamp(1, MAX_READ_LINES as u64) as usize; | 141 | .clamp(1, MAX_READ_LINES as u64) as usize; |
| 140 | 142 | ||
| 141 | let bytes = fs::read(&path).map_err(|e| format!("{path}: {e}"))?; | 143 | let meta = fs::metadata(&path).map_err(|e| format!("{path}: {e}"))?; |
| 142 | if bytes.iter().take(8192).any(|&b| b == 0) { | 144 | if meta.is_dir() { |
| 143 | return Err(format!( | 145 | return Err(format!("{path} is a directory; use glob or grep")); |
| 144 | "{path}: looks like a binary file and will not be shown" | ||
| 145 | )); | ||
| 146 | } | 146 | } |
| 147 | let text = String::from_utf8_lossy(&bytes); | 147 | |
| 148 | let lines: Vec<&str> = text.lines().collect(); | 148 | let file = fs::File::open(&path).map_err(|e| format!("{path}: {e}"))?; |
| 149 | if lines.is_empty() { | 149 | let mut reader = BufReader::with_capacity(64 * 1024, file); |
| 150 | return Ok(format!("{path}: empty file")); | 150 | |
| 151 | } | 151 | if let Ok(head) = reader.fill_buf() { |
| 152 | if offset > lines.len() { | 152 | if head.iter().take(8192).any(|&b| b == 0) { |
| 153 | return Ok(format!( | 153 | return Err(format!( |
| 154 | "{path} has {} lines; offset {offset} is past the end", | 154 | "{path}: looks like a binary file and will not be shown" |
| 155 | lines.len() | 155 | )); |
| 156 | )); | 156 | } |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | let end = (offset + limit - 1).min(lines.len()); | 159 | // Skip to the first requested line without buffering what is skipped, so a |
| 160 | let mut out = String::with_capacity((end - offset + 2) * 40); | 160 | // 500 MB log with offset near the end costs a scan, not a 500 MB allocation. |
| 161 | for number in offset..=end { | 161 | for _ in 1..offset { |
| 162 | out.push_str(&format!( | 162 | match skip_line(&mut reader).map_err(|e| format!("{path}: {e}"))? { |
| 163 | "{number:>6}\t{}\n", | 163 | true => {} |
| 164 | clip(lines[number - 1], MAX_LINE_CHARS) | 164 | false => return Ok(format!("{path} has fewer than {offset} lines")), |
| 165 | )); | 165 | } |
| 166 | } | 166 | } |
| 167 | if end < lines.len() { | 167 | |
| 168 | out.push_str(&format!( | 168 | let mut out = String::new(); |
| 169 | "… {} more line(s); continue with offset={}\n", | 169 | let mut produced = 0usize; |
| 170 | lines.len() - end, | 170 | let mut number = offset; |
| 171 | end + 1 | 171 | |
| 172 | )); | 172 | while produced < limit { |
| 173 | let (bytes, truncated) = match read_line_limited(&mut reader, MAX_LINE_BYTES) | ||
| 174 | .map_err(|e| format!("{path}: {e}"))? | ||
| 175 | { | ||
| 176 | Some(line) => line, | ||
| 177 | None => break, | ||
| 178 | }; | ||
| 179 | let text = String::from_utf8_lossy(&bytes); | ||
| 180 | let text = text.trim_end_matches('\n'); | ||
| 181 | let mut rendered = clip(text, MAX_LINE_CHARS); | ||
| 182 | if truncated { | ||
| 183 | rendered.push_str(" …(line truncated)"); | ||
| 184 | } | ||
| 185 | out.push_str(&format!("{number:>6}\t{rendered}\n")); | ||
| 186 | produced += 1; | ||
| 187 | number += 1; | ||
| 188 | } | ||
| 189 | |||
| 190 | if produced == 0 { | ||
| 191 | return Ok(if offset == 1 { | ||
| 192 | format!("{path}: empty file") | ||
| 193 | } else { | ||
| 194 | format!("{path} has fewer than {offset} lines") | ||
| 195 | }); | ||
| 196 | } | ||
| 197 | |||
| 198 | if let Ok(head) = reader.fill_buf() { | ||
| 199 | if !head.is_empty() { | ||
| 200 | out.push_str(&format!("… more lines; continue with offset={number}\n")); | ||
| 201 | } | ||
| 173 | } | 202 | } |
| 203 | |||
| 174 | Ok(out) | 204 | Ok(out) |
| 175 | } | 205 | } |
| 176 | 206 | ||
| 207 | /// Consumes one line without keeping it. False at end of file. | ||
| 208 | fn skip_line<R: BufRead>(reader: &mut R) -> std::io::Result<bool> { | ||
| 209 | let mut any = false; | ||
| 210 | loop { | ||
| 211 | let buf = reader.fill_buf()?; | ||
| 212 | if buf.is_empty() { | ||
| 213 | return Ok(any); | ||
| 214 | } | ||
| 215 | any = true; | ||
| 216 | match buf.iter().position(|&b| b == b'\n') { | ||
| 217 | Some(index) => { | ||
| 218 | reader.consume(index + 1); | ||
| 219 | return Ok(true); | ||
| 220 | } | ||
| 221 | None => { | ||
| 222 | let len = buf.len(); | ||
| 223 | reader.consume(len); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | /// Reads one line, keeping at most `max` bytes but consuming the whole line. | ||
| 230 | /// Returns `(bytes, truncated)`; `None` at end of file. | ||
| 231 | fn read_line_limited<R: BufRead>( | ||
| 232 | reader: &mut R, | ||
| 233 | max: usize, | ||
| 234 | ) -> std::io::Result<Option<(Vec<u8>, bool)>> { | ||
| 235 | let mut kept: Vec<u8> = Vec::new(); | ||
| 236 | let mut any = false; | ||
| 237 | let mut truncated = false; | ||
| 238 | |||
| 239 | loop { | ||
| 240 | let buf = reader.fill_buf()?; | ||
| 241 | if buf.is_empty() { | ||
| 242 | return Ok(if any { Some((kept, truncated)) } else { None }); | ||
| 243 | } | ||
| 244 | any = true; | ||
| 245 | |||
| 246 | let newline = buf.iter().position(|&b| b == b'\n'); | ||
| 247 | let take = match newline { | ||
| 248 | Some(index) => index + 1, | ||
| 249 | None => buf.len(), | ||
| 250 | }; | ||
| 251 | |||
| 252 | if kept.len() < max { | ||
| 253 | let room = max - kept.len(); | ||
| 254 | let slice = &buf[..take.min(room)]; | ||
| 255 | kept.extend_from_slice(slice); | ||
| 256 | if take > slice.len() { | ||
| 257 | truncated = true; | ||
| 258 | } | ||
| 259 | } else { | ||
| 260 | truncated = true; | ||
| 261 | } | ||
| 262 | |||
| 263 | reader.consume(take); | ||
| 264 | |||
| 265 | if newline.is_some() { | ||
| 266 | return Ok(Some((kept, truncated))); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 177 | pub fn grep(args: &Value) -> Result<String, String> { | 271 | pub fn grep(args: &Value) -> Result<String, String> { |
| 178 | let pattern = req(args, "pattern")?; | 272 | let pattern = req(args, "pattern")?; |
| 179 | if pattern.is_empty() { | 273 | if pattern.is_empty() { |
diff --git a/tests/tools.rs b/tests/tools.rs index 0189af8..3df353f 100644 --- a/tests/tools.rs +++ b/tests/tools.rs | |||
| @@ -76,7 +76,7 @@ fn read_numbers_lines_and_reports_truncation() { | |||
| 76 | let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); | 76 | let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); |
| 77 | assert!(text.contains(" 1\tone"), "{text}"); | 77 | assert!(text.contains(" 1\tone"), "{text}"); |
| 78 | assert!(text.contains(" 2\ttwo"), "{text}"); | 78 | assert!(text.contains(" 2\ttwo"), "{text}"); |
| 79 | assert!(text.contains("2 more line(s); continue with offset=3"), "{text}"); | 79 | assert!(text.contains("continue with offset=3"), "{text}"); |
| 80 | assert!(!text.contains("three"), "{text}"); | 80 | assert!(!text.contains("three"), "{text}"); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| @@ -140,3 +140,64 @@ fn unknown_tool_is_an_error_not_a_panic() { | |||
| 140 | let error = tools::run("teleport", &json!({})).err().unwrap(); | 140 | let error = tools::run("teleport", &json!({})).err().unwrap(); |
| 141 | assert!(error.contains("unknown tool"), "{error}"); | 141 | assert!(error.contains("unknown tool"), "{error}"); |
| 142 | } | 142 | } |
| 143 | |||
| 144 | #[test] | ||
| 145 | fn read_streams_large_files_and_reports_the_next_offset() { | ||
| 146 | let dir = temp_dir("read-large"); | ||
| 147 | let path = dir.join("big.txt"); | ||
| 148 | let mut body = String::new(); | ||
| 149 | for n in 1..=1000 { | ||
| 150 | body.push_str(&format!("line {n}\n")); | ||
| 151 | } | ||
| 152 | fs::write(&path, body).unwrap(); | ||
| 153 | |||
| 154 | let text = tools::run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); | ||
| 155 | assert!(text.contains(" 990\tline 990"), "{text}"); | ||
| 156 | assert!(text.contains(" 994\tline 994"), "{text}"); | ||
| 157 | assert!(!text.contains("line 995"), "{text}"); | ||
| 158 | assert!(text.contains("continue with offset=995"), "{text}"); | ||
| 159 | } | ||
| 160 | |||
| 161 | #[test] | ||
| 162 | fn read_clips_a_single_enormous_line() { | ||
| 163 | let dir = temp_dir("read-huge-line"); | ||
| 164 | let path = dir.join("one-line.txt"); | ||
| 165 | fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); | ||
| 166 | |||
| 167 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | ||
| 168 | assert!(text.starts_with(" 1\tx"), "{text}"); | ||
| 169 | assert!(text.contains("line truncated"), "{text}"); | ||
| 170 | // Bounded by the per-line cap, not by the file size. | ||
| 171 | assert!(text.len() < 1000, "read produced {} bytes", text.len()); | ||
| 172 | } | ||
| 173 | |||
| 174 | #[test] | ||
| 175 | fn read_reports_an_offset_past_the_end() { | ||
| 176 | let dir = temp_dir("read-eof"); | ||
| 177 | let path = dir.join("short.txt"); | ||
| 178 | fs::write(&path, "only\n").unwrap(); | ||
| 179 | let text = tools::run("read", &json!({ "path": path, "offset": 9 })).unwrap(); | ||
| 180 | assert!(text.contains("fewer than 9 lines"), "{text}"); | ||
| 181 | } | ||
| 182 | |||
| 183 | #[test] | ||
| 184 | fn read_refuses_binaries_and_directories() { | ||
| 185 | let dir = temp_dir("read-binary"); | ||
| 186 | let binary = dir.join("blob.bin"); | ||
| 187 | fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); | ||
| 188 | let error = tools::run("read", &json!({ "path": binary })).err().unwrap(); | ||
| 189 | assert!(error.contains("binary"), "{error}"); | ||
| 190 | |||
| 191 | let error = tools::run("read", &json!({ "path": dir })).err().unwrap(); | ||
| 192 | assert!(error.contains("is a directory"), "{error}"); | ||
| 193 | } | ||
| 194 | |||
| 195 | #[test] | ||
| 196 | fn read_handles_a_missing_trailing_newline() { | ||
| 197 | let dir = temp_dir("read-no-newline"); | ||
| 198 | let path = dir.join("f.txt"); | ||
| 199 | fs::write(&path, "one\ntwo").unwrap(); | ||
| 200 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | ||
| 201 | assert!(text.contains(" 2\ttwo"), "{text}"); | ||
| 202 | assert!(!text.contains("continue with offset"), "{text}"); | ||
| 203 | } | ||
