Explainer

A Terminal Crash Caused by a Line I Never Measured

A Pi terminal overlay crashed when a 141-column row reached a 59-column terminal. The fix made visible width a boundary contract instead of shortening one field by hand.

On this page

Deck: A Pi extension overlay crashed when it printed a session-file path 141 visible columns wide into a 59-column terminal. The fix was not “shorten the path”; it was to make width a contract every line the overlay emits has to satisfy.

The failure was data-dependent, not layout-dependent

The crash report was unambiguous and, at first, confusing. The offending row had a visible width of 141. The terminal was 59 columns wide. Nothing about the layout engine had changed.

The line came from an overlay that displays session metadata. One field held a long session-file path; when that value reached the renderer without truncation, the row grew to 141 columns — roughly 2.4 times the available width. The renderer rejected it.

That detail matters: the renderer did not quietly wrap or clip the row, it refused it. So the failure mode is a hard error, not a cosmetic one, and it arrives at runtime rather than at build time. This is the class of terminal bug I most under-appreciate: a value whose length is decided by runtime data crosses a layout boundary with no size check. Looking at the layout alone cannot establish that every possible value fits, because the layout never saw the value that broke it.

Width is a contract, and I had been assuming it

The renderer is handed a width. Before this fix, the overlay trusted that every line it produced fit inside that width. It did not verify that assumption; it inherited it from the fields that happened to be short during development.

The fix clamps at the boundary instead. Every line an overlay can emit now passes through the same pair of helpers: visibleWidth measures how many columns the string will actually occupy, and truncateToWidth cuts it down to a supplied limit. Overlay code no longer emits a line it has not measured. visibleWidth is the important half. A string’s character count is not its column count — wide East Asian glyphs and combining marks occupy a different number of columns than they contribute characters — so measuring means asking the same question the renderer would ask.

For label/value pairs I added fitValue. A key/value row has two parts competing for one line, so truncating naively can cut the tail of the value a reader needed while leaving room the row never uses, or cut the label and leave a value nobody can identify. fitValue tail-truncates the pair and marks the cut with a leading ellipsis, so a truncated field is visibly truncated rather than silently mangled. The reader can tell that something was clipped, which matters in a tool whose whole job is showing state.

The clamp uses the same visible-width measurement as the renderer, so both sides agree on the width budget. I did not test every Unicode-width edge case; the claim is narrower: with the project’s own measurement, no tested overlay line exceeds its supplied width.

The regression test is a matrix of payloads, not one reproducer

A single reproducer would have proved the crash is fixed and nothing else. I wrote the regression test as a small matrix instead: 7 cases, all passing.

The matrix has two axes.

Terminal widths: 59 (the width that actually crashed), 20 (deliberately hostile), and 200 (a wider layout).

Payload shapes, each pushed through an overlay:

  • a long model identifier,
  • an oversized error message,
  • a long tool trace,
  • an ephemeral session that has no session file at all.

Those two axes would cross to twelve combinations. The suite asserts seven cases — fewer than a full sweep, chosen for the pairings I judged worth asserting rather than enumerating every combination. That is a judgement I could get wrong in a way a full sweep would not be, and I cannot reconstruct from what I have which width was paired with which payload, so I am reporting the count and the axes rather than a pairing table. The count is also why I describe the suite as a matrix rather than a proof: seven cases cannot fill a twelve-combination grid, and I cannot name the pairings that were omitted.

That last payload is the one I would have skipped if I were testing only the reported crash. The original failure was a value that was too long; an absent field is the opposite problem, and it can take a different formatting path — a missing value often gets a placeholder, and a placeholder still has to fit. It belongs in the matrix precisely because it was not the original failure.

The identifier, error-message, and tool-trace payloads all resemble the original bug in that they carry unbounded text; the missing session file does not. I do not want to overstate the coverage, though: the matrix proves that unbounded text coming out of the overlay is clamped at the widths I tested, in the shapes I tested. It does not prove the UI is correct at every width or with every kind of text, nor that every width/payload pairing behaves the same way.

What I actually changed my mind about

The first instinct when a UI crashes on a long string is to shorten the string. I resisted it for two reasons: the value in that field is information the user asked for, and shortening one field fixes one field. The next long value — an error string from a dependency, a tool argument, an identifier I did not design — would crash the same overlay in the same way. A formatting fix is a fix per field; there are more fields than I can audit.

So the rule I settled on is a boundary rule rather than a formatting rule: a component that receives a width owns the obligation to never emit a wider line. Producers may generate arbitrarily long text; the boundary that hands text to the layout must measure and clamp. Enforcing that at the boundary is also cheaper to keep correct than auditing every call site, because new fields inherit the safety instead of having to remember it.

The second change is about how I test terminal code. Widths 20 and 59 are uncomfortable on purpose. A layout validated only in a wide window is a layout I have not tested. Now the hostile width is in the suite, alongside the shape I did not think to try: the field that is not there.