Skip to content

perf(process): stop padding rendered terminal lines to full width - #1450

Merged
kaoru0822-kitauji merged 1 commit into
mainfrom
perf/process-trim-rendered-lines
Oct 10, 2026
Merged

kaoru0822-kitauji merged 1 commit into
mainfrom
perf/process-trim-rendered-lines

Conversation

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Summary

Process capture padded every visible terminal row with trailing spaces to the terminal width (TerminalRenderer used padEnd(columns)). Each rendered frame repeats all rows, so blank and short rows cost a full row in every frame. The padding carried no information: each frame records columns, and serialized_state keeps the exact terminal state.

  • Rendered lines are now trimmed of trailing U+0020 after normalization. line.padEnd(frame.columns) restores the previous value exactly, so nothing is lost.
  • The comparison contract now commits rendered_line_format: "trailing-spaces-trimmed-v1". This follows the port_normalization_version precedent: comparing a capture with padded lines against a new capture reports an incompatible contract that names this field, instead of reporting terminal changes that are only whitespace.
  • rendered_terminal byte accounting counts the trimmed lines, so limits.output_bytes retains more frames before truncating.
  • docs/process-capture.md documents the line format.

No field was removed, and text/structured results are unchanged (#723).

Measurements

Real capture_process_scenario calls through stdio MCP on macOS arm64, comparing whole structuredContent bytes. "Before" means the same capture with its lines padded back to full width.

Command Rendered frames Before After Saved
#1250 sparse workload (65 lines over 1.3 s) 65 227,922 138,241 39%
git log --oneline -15 6 28,216 20,511 27%
echo hello 1 8,134 6,219 24%
ls -la /Applications 4 24,266 20,788 14%

MCP results carry the Evidence in both text and structured content, so a call saves twice these bytes.

Verification

  • New src/process/capture/TerminalRenderer.test.ts covers trimmed rows, preserved blank rows and inner spacing, padEnd(columns) restoration, byte accounting, and normalization still seeing the full-width row.
  • tests/boundary/process/processCaptureNormalization.test.ts pins the committed contract field.
  • npm run test:focused -- all of tests/boundary/process/*, src/domain/process/*, src/process/**, tests/boundary/mcp/toolResult.test.ts, src/application/ReconstructionObligationCandidates.test.ts: 52 files, 336 passed, 3 skipped (Linux-only).
  • tests/acceptance/tools/localToolContracts.test.ts passed. npm run check:changed, npm run typecheck and npm run docs:check pass.
  • Real PTY captures on macOS (table above). No Hopper or Ghidra is involved.

Refs #723, #1065.

🤖 Generated with Claude Code

Rendered frames padded every visible row with trailing spaces to the
terminal width. Blank rows cost a full row each and every frame repeats
them, so the padding made up 14-39% of capture Evidence (2 KB of sparse
output produced 228 KB). The padding carried no information: columns is
recorded and serialized_state keeps the exact terminal state.

Lines are now trimmed after normalization, so line.padEnd(columns)
restores the previous value. The comparison contract commits
rendered_line_format so older padded captures do not compare as the
same contract.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@kaoru0822-kitauji
kaoru0822-kitauji merged commit 2781cad into main Oct 10, 2026
22 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the perf/process-trim-rendered-lines branch October 10, 2026 06:18
@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: This is a post-merge check. I tested the PR head, and the merged commit has the same patch. I ran real Linux captures on the old and new code. The new captures are smaller, and old vs new captures are refused cleanly. No visible text is lost. But padding a line back to columns does not always rebuild the old line. It misses for two causes, shown below. Edit: I first wrote "two cases". The table has five rows, from two causes.

Size (same PTY chunks, rendered_frames bytes)

Capture Cols Base Head Saved
echo hello 80 2,147 232 89%
vim -R file, then :q! 80 6,754 1,161 83%
60 short lines 80 147,556 40,038 73%
colored bash lines 80 5,402 1,838 64%
less -R file, space, q 80 8,635 3,967 54%
ls --color=always -la /usr/bin | head -200 200 64,513 51,163 20%
same ls 80 54,386 52,286 4%
top -b -n 1 80 510,763 507,592 1%

At 80 columns, ls -la wraps, so its rows are already full. Wall time did not change beyond noise. The limits.output_bytes claim holds. With a 20,000 byte budget, the 60-line run kept 10 of 60 frames on base. Head kept 54 of 60.

Does head line + padEnd(columns) equal the base line?

Case Equal?
Plain, colored, blank and wrapped rows yes
Real trailing spaces the program printed yes
Tab, NBSP, U+3000 at the end yes (kept, not trimmed)
Wide CJK at the right edge yes
Row exactly columns wide yes
Red background spaces (\e[41m ) yes
\e[K with blue background, cursor jump past text yes
Combining marks (e + U+0301) no
Emoji with U+FE0F, ZWJ family no
Own pid (echo pid $$) no
Port in localhost:8080 no
A patterns entry that changes length no

Every miss is trailing spaces only. The trimmed text matches. serialized_state matched base byte for byte in every frame. Colored spaces stay in serialized_state.

Here is why it misses. Base pads first, then normalizes. So a 7-digit pid becomes <pid>, and that base row is 38 units at 40 columns. Head pads pid <pid> back to 40. Combining marks go the other way. padEnd counts UTF-16 units, not cells. So that base row is 50 units at 40 columns. Head pads it to 40. top -b -n 1 hits the pid case when its own row is on screen.

A small follow-up could make the docs line say "up to trailing spaces". Your call.

Contract

Old vs new is refused both ways. Both builds name the field: these scenario fields differ: rendered_line_format. Pairs from one build still compare. Terminal shows unchanged. One small nit: the message says to capture both scenarios with the same values. This field is not in the scenario. The fix is to capture the old side again with the new REA.

Tests

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
PR test files on head 5 pass 5 pass 5 pass
PR test files on base not run 2 fail, 3 pass not run
tests/boundary/process + src/process on head not run 298 pass, 7 skip not run
src/domain/process + toolResult on head not run 32 pass not run

On base, the two new checks fail as they should. The test "trims only after normalization sees the full-width row" passes on base too. So it guards the order, not the trim.

Not run

  • PR test files on base with Node 22 and 26
  • Process suites on Node 22 and 26
  • localToolContracts, check:changed, typecheck, docs:check
  • macOS and Windows
  • htop and other full-screen apps besides less and vim
  • The MCP capture ran only for echo and less

Thanks kaoru0822-kitauji for a tidy PR and a clear contract bump.

How I measured
  • Base is the PR parent 33b9b31a. It is not gh's base ref. main had already moved on.
  • Each scenario ran three times per build through rea capture-process --json. I used env -i, a fake HOME and a short TMPDIR.
  • PTY chunking changes run to run, so frame counts differ between real runs. For a fair size test, I fed the same raw chunks into both builds' TerminalRenderer. I used the real normalizeProcessText and the recovered pid. The table shows the median of six chunk sets.
  • The equality check used that same replay. It compared every row of every frame.
  • For the base test row, I copied the two head test files into the base tree.
  • Over stdio MCP, echo structured content went from 8,101 to 6,234 bytes. The text block carries the same Evidence, so the whole response went from 16,924 to 13,194.
Edge cases
printf 'trail   \r\n'
printf 'a\tb\t\r\n'
printf 'nbsp \r\n'
printf 'ideo \r\n'
printf '%*s漢\r\n' 38 'cjk-edge'
printf 'éééééééééé\r\n'
printf '❤️ ❤️ \U0001F468‍\U0001F469‍\U0001F467\r\n'
printf 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n'
printf '\e[41m   \e[0m\r\n'
printf '\e[44mbg-erase\e[K\e[0m\r\n'
printf 'pid %s\r\n' "$$"
printf 'url localhost:8080\r\n'
printf '\e[14;30Hfar'

Base vs head row lengths in UTF-16 units, 40 columns:

Row Base Head + padEnd
éééééééééé 50 40
❤️ ❤️ 👨‍👩‍👧 47 40
pid <pid> 38 40
url localhost:<port> 42 40
short → much-longer-replacement 58 40
LONGTOKEN y → x y 32 40
Test setup

🤖 Generated with Claude Code (Opus 5.5) 🦊

darsrc pushed a commit to darsrc/rea that referenced this pull request Oct 10, 2026
…rluto#1450)

Rendered frames padded every visible row with trailing spaces to the
terminal width. Blank rows cost a full row each and every frame repeats
them, so the padding made up 14-39% of capture Evidence (2 KB of sparse
output produced 228 KB). The padding carried no information: columns is
recorded and serialized_state keeps the exact terminal state.

Lines are now trimmed after normalization, so line.padEnd(columns)
restores the previous value. The comparison contract commits
rendered_line_format so older padded captures do not compare as the
same contract.
@morluto morluto added performance Performance regressions, scaling limits, and bounded-resource work area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
@morluto morluto added the area: runtime Process capture and runtime observation label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior area: runtime Process capture and runtime observation performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants