Skip to content

Commit 3baea2a

Browse files
Kenoclaude
andcommitted
Enhance bash tool output handling
- Add 30KB output truncation with clear message when exceeded - Display '<system>Tool ran without output or errors</system>' for successful commands with no output - Improve error handling for commands that fail with no output - Better user experience for both edge cases This helps prevent overwhelming the UI with large outputs and clearly indicates when commands succeed silently. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9716bfb commit 3baea2a

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/tools/bash.jl

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,36 +140,64 @@ function execute(tool::BashTool, params::Dict)
140140
stdout_str = String(take!(stdout_buf))
141141
stderr_str = String(take!(stderr_buf))
142142
exit_code = proc.exitcode
143-
143+
144+
# Truncate output if needed (30KB = 30720 bytes)
145+
max_output_bytes = 30720
146+
truncated = false
147+
148+
# Check and truncate stdout
149+
if sizeof(stdout_str) > max_output_bytes
150+
stdout_str = String(codeunits(stdout_str)[1:max_output_bytes])
151+
truncated = true
152+
end
153+
154+
# Check and truncate stderr (allow some space for both)
155+
stderr_max = max(1024, max_output_bytes - sizeof(stdout_str)) # At least 1KB for stderr
156+
if sizeof(stderr_str) > stderr_max
157+
stderr_str = String(codeunits(stderr_str)[1:stderr_max])
158+
truncated = true
159+
end
160+
144161
# Format the response
145162
response_parts = String[]
146-
163+
147164
# Add stdout if not empty
148165
if !isempty(stdout_str)
149166
push!(response_parts, stdout_str)
150167
end
151-
168+
152169
# Add stderr if not empty (prefix it to distinguish)
153170
if !isempty(stderr_str)
154171
if !isempty(response_parts)
155172
push!(response_parts, "\n--- stderr ---\n")
156173
end
157174
push!(response_parts, stderr_str)
158175
end
159-
176+
177+
# Add truncation notice if output was truncated
178+
if truncated
179+
if !isempty(response_parts)
180+
push!(response_parts, "\n")
181+
end
182+
push!(response_parts, "--- Output truncated (exceeded 30KB limit) ---")
183+
end
184+
160185
# Add exit code if non-zero
161186
if exit_code != 0
162187
if !isempty(response_parts)
163188
push!(response_parts, "\n")
164189
end
165190
push!(response_parts, "Exit code: $exit_code")
166191
end
167-
168-
# If no output at all, indicate success
169-
if isempty(response_parts)
170-
push!(response_parts, "Command completed successfully (no output)")
192+
193+
# If no output at all and success, use special format
194+
if isempty(response_parts) && exit_code == 0
195+
push!(response_parts, "<system>Tool ran without output or errors</system>")
196+
elseif isempty(response_parts)
197+
# Had an error but no output
198+
push!(response_parts, "Command failed with exit code: $exit_code (no output)")
171199
end
172-
200+
173201
result_text = join(response_parts)
174202

175203
# Return success - non-zero exit codes are part of normal command output, not MCP errors

0 commit comments

Comments
 (0)