edit_file diff rendering panics on invalid UTF-8 text

Open Beginner friendly
#4,955 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@TDnorthgarden is already working on this.

Since Nov 18, 2026.

  • #4956 by @TDnorthgarden — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
testing-qa

Research direction

Start with pkg/tui/components/tool/editfile/render.go, especially wrapTokens around the reported line 520, and reproduce the panic with the described a\xff input. Add a regression test for invalid UTF-8 in wrapTokens, then verify that the diff renders without panicking and run the editfile package test.

Written by the indexing model from the issue text.

Description

area/tui
Description

The edit-diff / confirmation-preview renderer crashes the whole TUI when the edited file contains invalid UTF-8 bytes (Latin-1 sources, GBK comments, binary-ish files).

wrapTokens (pkg/tui/components/tool/editfile/render.go:495-525) wraps syntax-highlighted tokens to a width. It accumulates the taken bytes with utf8.RuneLen(r) while iterating for _, r := range text — range decodes an invalid byte as U+FFFD consuming one source byte, but utf8.RuneLen(U+FFFD) is 3, so fitLen overcounts and text[:fitLen] slices past the string (:520).

Verified minimal repro (a file containing the bytes a\xff), from a unit test against the real package:

$ go test ./pkg/tui/components/tool/editfile -run TestWrapTokensInvalidUTF8
panic: runtime error: slice bounds out of range [:4] with length 2
...editfile.wrapTokens(...) render.go:520

Reachability: handleEditFile has no binary-content guard before persisting the result meta, and the in-process EditFileMeta reaches the renderer without a JSON roundtrip that would sanitize invalid bytes, so any edit_file on such a file panics the bubbletea View and kills the process.

Expected Behavior

Rendering an edit diff never panics; invalid bytes render safely (highlighted or replaced).

Actual Behavior

The process panics with slice bounds out of range mid-render.

Steps to Reproduce
  1. Create a file whose content includes an invalid UTF-8 byte (e.g. a Latin-1 é saved as 0xE9, or printf 'a\xff' > /tmp/x.txt).
  2. Ask an agent to edit_file it.
  3. The confirmation/diff view panics.
Docker Agent version

No response

OS & terminal

No response

Model used

No response

Error output

Screenshots

No response

Additional context

Fix: take the byte length from the range position (for i, r := range text with the rune's encoded size) or decode with utf8.DecodeRuneInString; add a wrapTokens test with invalid UTF-8. Verified on main @ 19a735644. Happy to submit the fix.

Dominant language
Go
Stars
4.7k
Forks
565
Avg merge
18h 52m
Merged PRs (30d)
221

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from docker/docker-agent

All issues in docker/docker-agent

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.