Skip to content

fix(cli): prevent truncate helpers from overflowing at small widths#11876

Open
Tamsi wants to merge 1 commit into
Kilo-Org:mainfrom
Tamsi:fix-truncate-overflow
Open

fix(cli): prevent truncate helpers from overflowing at small widths#11876
Tamsi wants to merge 1 commit into
Kilo-Org:mainfrom
Tamsi:fix-truncate-overflow

Conversation

@Tamsi

@Tamsi Tamsi commented Jul 2, 2026

Copy link
Copy Markdown

Summary

Locale.truncateLeft and Locale.truncateMiddle (packages/opencode/src/util/locale.ts) build their tail slice with str.slice(-keep). When keep is 0, str.slice(-0) evaluates to str.slice(0) and returns the entire string — so instead of truncating, the helper returns something longer than the input:

truncateLeft("src/components/very-long-file-name.tsx", 1)
// → "…src/components/very-long-file-name.tsx"  (39 chars for a width of 1)

truncateMiddle("src/components/very-long-file-name.tsx", 2)
// → "s…src/components/very-long-file-name.tsx"  (40 chars for a width of 2)

keep reaches 0 at maxLength 1 for both helpers, and at maxLength 2 for truncateMiddle.

These aren't purely theoretical widths — the TUI calls these helpers with dynamic widths that bottom out at 1, e.g. dialog-select.tsx:

{Locale.truncateMiddle(detail, Math.max(1, Math.min(76, dimensions().width - 12)))}

On a very narrow pane the "truncated" string is emitted at full length and blows out the layout it was supposed to fit.

Fix

Drop the tail explicitly when keep <= 0 (so the result is just head + ellipsis, or a lone ellipsis at width 1), leaving all larger-width behavior unchanged.

locale.ts is a shared upstream (OpenCode) file, so the edits are wrapped in kilocode_change markers per AGENTS.md, and bun run script/check-opencode-annotations.ts passes. The regression test lives under packages/opencode/test/kilocode/ (Kilo-specific test path).

Test plan

  • Added packages/opencode/test/kilocode/locale.test.ts covering the length-1 / length-2 edges and asserting the output never exceeds maxLength.
  • New tests pass with the fix (9 passed).
  • Confirmed the tests fail on main without the fix (5 failed — e.g. truncateMiddle(long, 2) returned "s…src/components/very-long-file-name.tsx" instead of "s…").
  • bun run script/check-opencode-annotations.ts → "All shared upstream changes are annotated with kilocode_change markers."

@kilo-code-bot

kilo-code-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

This update wraps the existing fix in locale.ts with kilocode_change markers and moves the regression test into packages/opencode/test/kilocode/ — housekeeping only, no logic changes. The import path (../../src/util/locale) still resolves correctly after the move. Markers are placed correctly around only the changed lines.

Note: No changeset was added yet. This still fixes a user-visible TUI overflow bug (truncateLeft/truncateMiddle emitting the full string instead of an ellipsis at very narrow widths), so per repo convention consider adding a patch changeset describing the fix from the user's perspective.

Files Reviewed (2 files)
  • packages/opencode/src/util/locale.ts
  • packages/opencode/test/kilocode/locale.test.ts
Previous Review Summary (commit b8b90ee)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit b8b90ee)

Status: No Issues Found | Recommendation: Merge

Note: No changeset was added. This fixes a user-visible TUI overflow bug (truncateLeft/truncateMiddle emitting the full string instead of an ellipsis at very narrow widths), so per repo convention consider adding a patch changeset describing the fix from the user's perspective.

Files Reviewed (2 files)
  • packages/opencode/src/util/locale.ts
  • packages/opencode/test/util/locale.test.ts

Reviewed by claude-sonnet-5-20260630 · Input: 32 · Output: 6.6K · Cached: 627.5K

Review guidance: REVIEW.md from base branch main

`truncateLeft` and `truncateMiddle` build their tail with `str.slice(-keep)`.
When `keep` is 0 — which happens at `maxLength` 1 (both) and 2 (middle) —
`str.slice(-0)` is `str.slice(0)` and returns the *entire* string, so the
"truncated" result is longer than the input instead of shorter.

These helpers are called from TUI components with dynamic widths that reach 1
(e.g. `dialog-select.tsx` uses `Math.max(1, …)`), so a narrow pane could emit
a value far wider than the available space and blow out the layout.

Drop the tail explicitly when `keep <= 0`. The shared-file edits are wrapped in
`kilocode_change` markers; the regression test lives under `test/kilocode/`.
@Tamsi Tamsi force-pushed the fix-truncate-overflow branch from b8b90ee to 27b9698 Compare July 2, 2026 06:31
@Tamsi Tamsi changed the title fix(opencode): prevent truncate helpers from overflowing at small widths fix(cli): prevent truncate helpers from overflowing at small widths Jul 2, 2026
@johnnyeric johnnyeric requested a review from catrielmuller July 3, 2026 11:21
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.

1 participant