From dd40892ad5ea0644a20692a25ec3583ab5321d78 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Tue, 14 Jul 2026 12:31:24 -0600 Subject: [PATCH] fix: Make fs_read more tolerant of various arg invocation formats. --- assets/functions/tools/fs_read.sh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/assets/functions/tools/fs_read.sh b/assets/functions/tools/fs_read.sh index 9a3dc84..9b5ffe1 100644 --- a/assets/functions/tools/fs_read.sh +++ b/assets/functions/tools/fs_read.sh @@ -8,8 +8,8 @@ set -e # Use the grep tool to find specific content before reading, then read with offset to target the relevant section. # @option --path! The absolute path to the file or directory to read -# @option --offset The line number to start reading from (1-indexed, default: 1) -# @option --limit The maximum number of lines to read (default: 2000) +# @option --offset The line number to start reading from (1-indexed, default: 1) +# @option --limit The maximum number of lines to read (default: 2000) # @env LLM_OUTPUT=/dev/stdout The output path @@ -33,9 +33,20 @@ main() { fi local total_lines file_bytes - total_lines=$(wc -l < "$target" 2>/dev/null || echo 0) + # awk counts a final line that lacks a trailing newline; wc -l would undercount it by one. + total_lines=$(awk 'END { print NR }' "$target" 2>/dev/null || echo 0) file_bytes=$(wc -c < "$target" 2>/dev/null || echo 0) + if [[ "$total_lines" -eq 0 ]]; then + echo "(file is empty: $target)" >> "$LLM_OUTPUT" + return 0 + fi + + if [[ "$offset" -gt "$total_lines" ]]; then + echo "(offset $offset is past the end of the file, which has $total_lines lines)" >> "$LLM_OUTPUT" + return 0 + fi + if [[ "$file_bytes" -gt "$MAX_BYTES" ]] && [[ "$offset" -eq 1 ]] && [[ "$limit" -ge 2000 ]]; then { echo "Warning: Large file (${file_bytes} bytes, ${total_lines} lines). Showing first ${limit} lines." @@ -48,7 +59,8 @@ main() { sed -n "${offset},${end_line}p" "$target" 2>/dev/null | { local line_num=$offset - while IFS= read -r line; do + # `|| [[ -n "$line" ]]` keeps the final line when the file has no trailing newline. + while IFS= read -r line || [[ -n "$line" ]]; do if [[ ${#line} -gt $MAX_LINE_LENGTH ]]; then line="${line:0:$MAX_LINE_LENGTH}... (truncated)" fi