ReadFile returns path's contents, truncated to the shared tool-output budget (Truncate). The model gets exact bytes, not a shell-mangled approximation. Per the bash/write/edit convention, filesystem errors come back in the output string, never as a Go error: the model reacts to them like a non-zero
(path string)
| 12 | // re-fire on the window and drop its middle, silently reinstating the |
| 13 | // unrecoverable dead end that offset/limit exists to remove. |
| 14 | const readChunkBytes = 20000 |
| 15 | |
| 16 | // maxFileBytes gates read_file and edit_file: both slurp the whole file with |
| 17 | // os.ReadFile, so a multi-GB log would OOM-kill the TUI - the same hazard |
| 18 | // bash's headTailBuffer capture cap exists to stop, unguarded in the file |
| 19 | // tools. 5MB covers every real source file; anything bigger is a log or an |
| 20 | // artifact, which grep/sed handle without loading it. |
| 21 | const maxFileBytes = 5 << 20 |
| 22 | |
| 23 | // ReadFile returns a window of path's contents: whole lines from offset |
| 24 | // (1-indexed, 0 meaning the start) up to limit lines, bounded by |
| 25 | // readChunkBytes. When the window stops short, the result names the exact |
| 26 | // offset to continue from, so a big file is read by repeated calls instead of |
| 27 | // guessed at from a truncated middle. |
| 28 | // |
| 29 | // Per the bash/write/edit convention, filesystem errors come back in the |
| 30 | // output string, never as a Go error: the model reacts to them like a non-zero |
| 31 | // exit. |
| 32 | func ReadFile(path string, offset, limit int) string { |
| 33 | if path == "" { |
no outgoing calls