Skip to content

fix(tools): remove dead code in _is_likely_binary and harden _check_lint against brace paths - #6950

Closed
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/file-operations-binary-check-and-lint
Closed

fix(tools): remove dead code in _is_likely_binary and harden _check_lint against brace paths#6950
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/file-operations-binary-check-and-lint

Conversation

@luyao618

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes two edge-case bugs in ShellFileOperations:

  1. Dead code in _is_likely_binary() — An unreachable if not content_sample branch was nested inside a if content_sample truthy check, meaning the inner guard could never execute. This dead code reduces readability and suggests the original intent was an early return for empty strings (which is already handled by the outer falsy check on empty "").

  2. _check_lint() crashes on brace-containing file pathslinter_cmd.format(file=...) interprets { and } in the file path as format-string placeholders, raising KeyError or ValueError for paths like src/{test}.py. Replaced with linter_cmd.replace("{file}", ...) which is immune to this issue.

Related Issue

No existing issue — found during code audit.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✅ Tests (adding or improving test coverage)

Changes Made

  • tools/file_operations.py:368-370 — Removed 2 lines of dead code (if not content_sample: return False) from _is_likely_binary()
  • tools/file_operations.py:741 — Changed linter_cmd.format(file=...)linter_cmd.replace("{file}", ...) in _check_lint()
  • Added tests/tools/test_file_operations_edge_cases.py — 16 unit tests covering:
    • _is_likely_binary(): binary extensions, text/binary content thresholds, empty/None samples, tab/newline exclusion, >1000 char samples
    • _check_lint(): normal paths, curly-brace paths, nested braces, missing linters, lint failures

How to Test

  1. pytest tests/tools/test_file_operations_edge_cases.py -v — all 16 tests pass
  2. pytest tests/ -q --ignore=tests/integration --ignore=tests/e2e — full suite passes

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 15 (Apple Silicon), Python 3.11.14

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

…int against brace paths

- Remove unreachable `if not content_sample` branch inside the truthy
  `if content_sample` block in `_is_likely_binary()` (dead code that
  could never execute).
- Replace `linter_cmd.format(file=...)` with `linter_cmd.replace("{file}", ...)`
  in `_check_lint()` so file paths containing curly braces (e.g.
  `src/{test}.py`) no longer raise KeyError/ValueError.
- Add 16 unit tests covering both fixes and edge cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@luyao618
luyao618 force-pushed the fix/file-operations-binary-check-and-lint branch from a1391b9 to 73dbc94 Compare April 10, 2026 04:20
@teknium1

Copy link
Copy Markdown
Contributor

Merged via #7541 with authorship preserved. Thanks for the contribution!

@teknium1 teknium1 closed this Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants