#479

fix(tools): reject empty content in terminal write_file

Openpei711wants to merge intofrom

Description of the Change

Problem

When the agent issues action=write_file for the terminal file tool with an empty string (content: "") or an omitted content payload, WriteFile in backend/pkg/tools/terminal.go proceeded to pack a 0-byte archive and reported:

Successfully wrote 0 bytes to /path/to/file

Treating an empty payload as a successful write disguises agent errors or truncated outputs as successful completions, misleading the LLM into believing it wrote valid file contents rather than triggering an error response and retry.

Solution

Added validation in WriteFile in backend/pkg/tools/terminal.go to ensure content is non-empty before writing to the container:

if content == "" {
    return "", fmt.Errorf("content is required and cannot be empty")
}

If empty content is passed, the tool execution returns an error (terminal tool 'file' handled with error: content is required and cannot be empty) and does not write to the container sandbox. Diff-based empty files via EditFile continue to function as expected via writeFileToContainer.

Closes #478

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Areas Affected

  • Security Tools Integration

Testing and Verification

Test Configuration

Host OS: Linux x86_64
Go Version: go1.24+

Test Steps

  1. Run target unit tests covering terminal write file handling:
    cd backend && go test -v -run TestTerminal_WriteFile ./pkg/tools/...
  2. Verify both the error string and soft tool failure wrapping behavior.

Test Results

=== RUN   TestTerminal_WriteFile_RefusesANameTheArchiveCannotCarry
--- PASS: TestTerminal_WriteFile_RefusesANameTheArchiveCannotCarry (0.00s)
=== RUN   TestTerminal_WriteFile_RefusesEmptyContent
--- PASS: TestTerminal_WriteFile_RefusesEmptyContent (0.00s)
PASS
ok  	pentagi/pkg/tools	0.016s

Security Considerations

No security risks introduced; prevents accidental creation or truncation of files when LLM inputs are blank or missing.

Performance Impact

Negligible (single string check prior to tar serialization).

Documentation Updates

None required.

Deployment Notes

None.

Checklist

  • My code follows the project's coding standards
  • I have added tests to cover my changes
  • All new and existing tests pass
  • I have run go fmt and go vet (for Go code)
  • Changes maintain or improve the security model
  • Changes are backward compatible

Activity

2
chatgpt-codex-connector[bot]12d ago

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

ReviewStatusCommitReview trigger
📝 Code Review🔄 Running since 2026-10-27T01:24:25.367607Zdd95b76PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.