Skip to content

Match main terminal mirror character widths to the renderer - #7148

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/main-emulator-unicode-width
Jul 2, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/main-emulator-unicode-width

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Symptom

Follow-up to #7133 (shipped in v1.4.118). User feedback: "a lot better, but have some left at the bottom" — residual garbage confined to the bottom rows of restored agent terminals: a merged status line, a wrapped text fragment on the next row, and a fossil usage-bar row. @brennanb2025 saw the same residue when reproducing.

Root cause

The renderer's xterm measures character widths with Unicode 11 + ZWJ emoji joining (the orca-11-zwj provider activated in pane-lifecycle.ts). The daemon's headless mirror (headless-emulator.ts) parsed the same PTY byte stream with xterm's default v6 width tables — no unicode provider at all.

Emoji advance one cell there instead of two, so every cursor-positioned write after an emoji lands shifted in the mirror. Emoji-dense rows — exactly the agent TUI's bottom region (🤖 📁 🌿 ⚡ status line, usage bars, spinners) — accumulate interleaved cell tears. This matches the anomaly captured in the first live investigation, where the main-process buffer itself held a torn status line (FSonn t 5, wolfl el — one-cell mid-string shifts), stable across snapshots and identical on every PTY: deterministic parse divergence, not a race.

#7133's alt-screen clear made restores paint exactly what the mirror holds — the ASCII body restores clean, and only the emoji rows still tear. The tear also reaches everything else that reads the mirror: orca terminal read previews, reattach snapshots, and mobile snapshots.

Fix

Load Unicode 11 tables in the headless emulator and activate the same shared provider the renderer uses. The provider moves from pane-manager/pane-terminal-unicode-provider.ts to src/shared/terminal-unicode-provider.ts so both parsers stay pinned to identical width semantics.

Tests

  • New headless-emulator-unicode-width.test.ts with positioned-overwrite oracles through the public emulator API. Red on the old code with exactly the observed tear shape ('🤖AB Z' vs '🤖ABZ' — the same one-cell shift as wolfl el); green with the fix. A second oracle pins ZWJ joining against a bare-Unicode-11 regression.
  • Full src/main/daemon + pane-manager suites: 947 passing (the one pty-subprocess WSL failure is pre-existing on main, verified against a pristine checkout). Typecheck clean.

Made with Orca 🐋

The renderer's xterm measures with Unicode 11 widths plus ZWJ emoji
joining (orca-11-zwj); the daemon's headless mirror parsed the same PTY
bytes with xterm's default v6 tables. Emoji advance one cell there
instead of two, so every positioned write after an emoji lands shifted
in the mirror — emoji-dense rows (agent status lines, usage bars)
accumulate interleaved cell tears that are stable across snapshots and
identical for every PTY.

Snapshot restores then paint those torn rows back into the pane, which
is the residual bottom-of-terminal garbage reported after the alt-screen
restore clear shipped in v1.4.118 (the ASCII body restores clean; only
emoji rows diverge). The same tear also reaches anything else that reads
the mirror: terminal read previews, reattach and mobile snapshots.

Fix: load Unicode 11 tables in the headless emulator and activate the
same shared provider the renderer uses. The provider moves to
src/shared/ so both parsers stay pinned to identical width semantics.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This change applies Orca's Unicode 11 cursor/width provider to the headless terminal emulator so its buffer measurement matches the renderer's cell-width model, preventing snapshot/rehydration drift for wide characters like emoji and ZWJ sequences. A new test suite validates this behavior. Additionally, the import path for the shared unicode provider utility used in the renderer's pane lifecycle was updated to a new shared module location, with a corresponding comment update in pane terminal options.

Changes

Cohort / File(s) Summary
Headless emulator unicode support
src/main/daemon/headless-emulator.ts
Imports Unicode11Addon and activateOrcaTerminalUnicodeProvider; loads the addon and activates the provider on the terminal in the constructor.
src/main/daemon/headless-emulator-unicode-width.test.ts New test suite verifying cursor advancement for single emoji and ZWJ-joined emoji sequences.
Shared provider path update
src/renderer/src/lib/pane-manager/pane-lifecycle.ts
Updates import path for activateOrcaTerminalUnicodeProvider to a new shared module location.
src/renderer/src/lib/pane-manager/pane-terminal-options.ts Updates a comment referencing the renamed/relocated unicode provider file.

Sequence Diagram(s)

sequenceDiagram
  participant HeadlessEmulator
  participant Terminal
  participant Unicode11Addon
  participant activateOrcaTerminalUnicodeProvider

  HeadlessEmulator->>Terminal: create instance
  HeadlessEmulator->>Unicode11Addon: loadAddon(terminal)
  HeadlessEmulator->>activateOrcaTerminalUnicodeProvider: activate(terminal)
Loading

Estimated code review effort: 2 (~30 minutes)

Related PRs: None identified.

Suggested labels: terminal, unicode, bugfix

Suggested reviewers: None identified.

Poem

A rabbit hops through cells of text,
Counting widths so none are vexed,
Emoji wide and ZWJ paired,
Now snapshot drift is safely spared,
Two cells for robot, laptop too—
The headless twin now sees it true. 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers symptom, root cause, fix, and tests, but it omits required template sections like Summary, Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections, including Summary, Screenshots, Testing checklist, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: aligning the terminal mirror's width handling with the renderer.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/main/daemon/headless-emulator.ts (1)

57-64: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Activation ordering is correct.

Loading Unicode11Addon then calling activateOrcaTerminalUnicodeProvider immediately in the constructor, before any write()/writeSync() call is reachable, mirrors the same ordering rationale used in the renderer's openTerminal (activate before any caller-driven write so widths are baked correctly). This closes the root-cause gap described in the PR.

One minor nit: the comment at Lines 57-61 spans 5 lines. As per path instructions, **/*.{ts,tsx,js,jsx} guidelines state to "Keep comments short — one or two lines, capturing only the non-obvious reason". Consider trimming this to the single non-obvious fact (mismatch causes cell-shifted tears) rather than restating the full bug narrative already covered in the PR description.

Source: Path instructions

src/main/daemon/headless-emulator-unicode-width.test.ts (1)

1-28: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Tests correctly validate positioned-overwrite and ZWJ-joining behavior.

Column arithmetic in both tests is internally consistent with the described Unicode 11 + ZWJ semantics, and both clean up via emulator.dispose().

Minor nit: the header comment at Lines 4-7 spans 4 lines. As per path instructions, **/*.{ts,tsx,js,jsx} guidelines call for comments explaining non-obvious behavior to "Keep comments short — one or two lines, capturing only the non-obvious reason".

Source: Path instructions


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6a257151-d9d5-4b23-bf3a-8b6abe27cd31

📥 Commits

Reviewing files that changed from the base of the PR and between e6ca308 and 5932185.

📒 Files selected for processing (5)
  • src/main/daemon/headless-emulator-unicode-width.test.ts
  • src/main/daemon/headless-emulator.ts
  • src/renderer/src/lib/pane-manager/pane-lifecycle.ts
  • src/renderer/src/lib/pane-manager/pane-terminal-options.ts
  • src/shared/terminal-unicode-provider.ts

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Parse-throughput measurement (the emulator parses every PTY byte synchronously in the daemon, so this is the hot path this PR touches). A/B/C benchmark on the real @xterm/headless + provider stack, 64KB chunks, best of 3 runs after warmup, alternating variants to cancel JIT/GC drift:

workload v6 (before) v11 tables v11 + Orca ZWJ provider (after)
ASCII bulk (12MB, build-log shape) 22.8 MB/s +7.0% +7.9%
emoji TUI frames (6MB, status-line shape) 20.1 MB/s +2.8% +8.2%
SGR-heavy (8MB) 22.5 MB/s +4.3% +0.2%

Run-to-run noise is ±5% (a second run showed 5.6%/14.7%/6.9%), so the honest summary is: single-digit-percent parse-cost increase, worst observed ~8–15% on synthetic emoji floods.

Why this is acceptable in absolute terms:

  • Throughput stays ~19–22 MB/s per emulator; real PTY streams sustain low single-digit MB/s at most (node-pty/IPC bound), and agent TUI output is KB/s — 4–10× headroom either way.
  • A full multi-minute agent session (~2–5MB of output) costs ~10–20ms of extra daemon CPU total, off the renderer thread.
  • The renderer has always paid exactly this provider cost for every visible pane; the daemon is adopting a cost profile already proven at full TUI volumes. Identical width semantics (mirror ≡ renderer) is what fixes the tear — there is no cheaper correct variant, since any divergence recreates the bug by construction.

Memory: the v11 BMP width table is a module-level singleton (verified in the addon source: if (!c) c = new Uint8Array(65536)) — one 64KB allocation per process, not per PTY. Measured ~83–120KB total heap per emulator across 200 instances, unchanged shape.

If we ever want the last few points back, the v11-plain→Orca-wrapper gap on emoji content (~5pts) could be shaved with a fast-path reorder in the shared provider's charProperties — that would speed up the renderer too. Not done here to keep this PR minimal.

@brennanb2025
brennanb2025 merged commit e44daf3 into main Jul 2, 2026
4 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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