Skip to content

fix(tui): pad autocomplete rows by display width, not byte length - #1256

Merged
edenreich merged 3 commits into
mainfrom
fix/autocomplete-display-width-panic
Sep 20, 2026
Merged

edenreich merged 3 commits into
mainfrom
fix/autocomplete-display-width-panic

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

The crash

infer chat panics and tears down the TUI the moment / is typed:

strings: negative Repeat count
strings.Repeat(...)
  tui/autocomplete.(*AutocompleteImpl).renderItems(..., 0x3, 0x1c, 0xb3)
    autocomplete.go:981

Root cause — bytes used as display columns

formatting.TruncateText cuts to display columns (ansi.StringWidth / ansi.Truncate), but renderItems padded the result with len() — bytes:

description := formatting.TruncateText(cmd.Description, descWidth)
paddedDescription := description + strings.Repeat(" ", descWidth-len(description))

One multibyte rune in a description long enough to be truncated pushes len() past the column budget and the count goes negative.

Reproduced with the exact values from the trace: at a 214-column terminal descWidth = 214 - 28 - 7 = 179 (the 0xb3). The motion-graphics skill description truncated to 179 columns is 181 bytes → strings.Repeat(" ", -2).

Why it surfaced now

The code was correct until the contract changed under it:

when what
2026-06-27 #667 flips TruncateText from len(text) <= maxLength (bytes) to ansi.StringWidth (columns) — this caller was not touched, and its padding silently became wrong
2026-07-31 #973 wires the skills catalog into the autocomplete list, so skill descriptions reach this renderer
2026-09-16 inference-gateway/skills#132 adds motion-graphics — the only one of 25 catalog entries with a non-ASCII description (— U+2014, → U+2192)

Before #667 len(truncated) <= maxLength was guaranteed, so the subtraction could never go negative. Nothing in the CLI changed in September; the catalog data did.

The fix

One helper beside the function whose contract caused it, so truncation and padding always happen in the same unit:

func PadText(text string, width int) string {
	text = TruncateText(text, width)
	return text + strings.Repeat(" ", max(0, width-ansi.StringWidth(text)))
}
  • autocomplete.go — both columns route through PadText; calculateMaxShortcutWidth measures with ansi.StringWidth instead of len.
  • task_management_view.go — the two separators now use the getSeparatorWidth() guard that already existed for exactly this case (strings.Repeat("─", t.width-4) panics below 4 columns, and t.width is 0 until the first WindowSizeMsg). One copy had no guard; the other inlined a duplicate of it, now deleted.

lipgloss.NewStyle().Width(n) was considered and rejected — it wraps overflow instead of truncating, which would turn one table row into several.

Tests

  • TestPadText — table-driven, asserts ansi.StringWidth(got) == width and valid UTF-8 across narrow-multibyte, wide-rune and ASCII inputs.
  • TestAutocomplete_RenderMultibyteDescription — feeds the real failing shape at 214/60/20 columns and asserts the rows stay column-aligned.

Verified the regression test earns its keep: reverting just the two pad lines makes it panic with the identical strings: negative Repeat count; restoring the fix passes.

Checks

  • task precommit:run — 0 issues
  • go test ./internal/presentation/... ./internal/platform/... — green

Notes

No docs ticket: bug fix, no public surface change. No cross-repo impact — the trigger data lives in inference-gateway/skills, but nothing there needs changing.

Found during the sweep, left for separate PRs:

  • task_management_view.go:1116-1152 pads with %-38s (rune count) against a column budget — misalignment only, no panic.
  • agent/infrastructure/formatter.go:258 still does a raw text[:maxLength-3] byte slice and emits invalid UTF-8 on multibyte input; its own test documents this at formatter_units_test.go:86.

TruncateText cuts to display columns (ansi.Truncate), but renderItems padded
the result with len() - bytes. Any description holding one multibyte rune
comes back longer in bytes than the column budget, so

    strings.Repeat(" ", descWidth-len(description))

got a negative count and panicked the whole TUI the moment "/" was typed.

The contract flipped from bytes to columns in #667 without updating this
caller, and stayed dormant until the skills catalog shipped motion-graphics,
its first entry with a non-ASCII description (em dash, arrow).

Add formatting.PadText, which truncates and pads in the same unit, and route
both autocomplete columns through it. Measure the shortcut column budget with
ansi.StringWidth for the same reason. Guard the two task-manager separators
with the getSeparatorWidth helper that already existed for exactly this - one
copy was missing it, the other inlined a duplicate.
@edenreich
edenreich requested a review from a team as a code owner September 20, 2026 11:52
@edenreich
edenreich merged commit 7c8d97f into main Sep 20, 2026
1 check passed
@edenreich
edenreich deleted the fix/autocomplete-display-width-panic branch September 20, 2026 11:57
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.205.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant