Skip to content

fix(tui): highlight multi-line code when the addon lacks HighlightStream.preview - #21252

Open
andreyors wants to merge 1 commit into
can1357:mainfrom
andreyors:fix/tui-highlight-preview-addon
Open

andreyors wants to merge 1 commit into
can1357:mainfrom
andreyors:fix/tui-highlight-preview-addon

Conversation

@andreyors

Copy link
Copy Markdown

What

I reviewed the diff; multi-line code crashed the TUI whenever the native addon was older than the JS, so the cached highlight path now falls back to full highlighting when preview is missing.

mapCachedBlock calls stream.preview(...) for the provisional tail of every multi-line block. When the loaded native addon's HighlightStream predates preview (a workspace tree runs its previous release's addon by design, validateLoadedBindings skips validation there), that call throws TypeError: stream.preview is not a function and takes the TUI down on the first edit diff, write card or code fence. The loader's missingNativeExport guard covers exports, not methods.

mapCachedBlock now checks typeof HighlightStream.prototype.preview once and returns undefined, so callers keep their existing uncached path (correct highlighting, no incremental reuse). A regression test removes preview from the prototype; the incremental "parses only new lines" test is it.skipIf without it, since it needs preview.

Why

Fatal Uncaught exception (session exit reason uncaught_exception) in a locally compiled binary whose embedded addon is older than the JS calling it; reproduced with the published pi-natives-darwin-arm64@19.4.0 addon under the post-19.4.0 tree.

Testing

  • Before: highlightCode("const x = 1;\n/* open\nstill open", "typescript", theme) with the 19.4.0 addon throws stream.preview is not a function. (In 'stream.preview(code.slice(end))', 'stream.preview' is undefined). After: 3 rows; renderDiff on a .ts diff renders 5 rows.
  • omp --resume of the crashing session in a PTY: old binary exits 1 (uncaught_exception, fatal); rebuilt binary renders and exits 0 (dispose, normal). Resumed interactively on the rebuilt binary afterwards: no crash.
  • This revision: bun test packages/tui/test/text 178 pass / 30 skip / 0 fail; packages/tui bun run check passes; root bun run check:ts passes (23 packages). check:rs not run: no Rust toolchain locally.

  • bun check passes (TypeScript half; check:rs not runnable locally)
  • Tested locally
  • CHANGELOG updated with the required attribution (PR link and contributor credit added after creation)

…eam.preview

- `mapCachedBlock` called `stream.preview` on every multi-line block, so
  an addon that predates that method (a workspace tree runs its previous
  release's addon by design) threw `stream.preview is not a function` and
  took the TUI down with every edit diff, write card and code fence.
- The loader's `missingNativeExport` guard covers exports, not methods;
  `mapCachedBlock` now checks the prototype and returns undefined, so
  callers keep their uncached path.
- The regression test removes `preview` from the prototype; the
  incremental "parses only new lines" test skips without it.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-12-01T08:30:14.514080Z
We couldn’t complete this review. Comment @codex review to try again.
f6fe15a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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.

@roboomp roboomp added fix review:p2 triaged tui Terminal UI rendering and display labels Dec 1, 2026

@roboomp roboomp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @andreyors. The diagnosis is clear and the fix works for the skew it targets: mapCachedBlock returns undefined before calling stream.preview, callers fall back to uncachedSpans, and the regression test strips preview from the prototype and restores it in finally.
Rank P2, a maintainer call: released installs and compiled binaries validate the addon version, so only workspace/locally built trees hit this. The loader's documented policy for those trees is to fail loudly with rebuild instructions (missingNativeExport/missingNativeClass). This PR adds a silent per-method fallback at the callsite instead. See the inline question: loader-level method stubs, or keep the fallback plus a one-time warning.
should-fix: the CHANGELOG entry needs the PR link and contributor credit.

// runs its previous release's addon by design (see the loader's `validateLoadedBindings`),
// whose `missingNativeExport` guard covers exports, not methods. Without a provisional
// tail there is no block to reuse: the caller highlights from scratch.
if (typeof HighlightStream.prototype.preview !== "function") return undefined;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question for the maintainer (design): validateLoadedBindings deliberately skips validation for workspace loads, and the comment at packages/natives/native/loader-state.js:487-490 says the mismatch "is not swallowed silently". Exports that a stale addon lacks get missingNativeExport/missingNativeClass stubs that throw with the addon path, both versions, and the rebuild command. This probe goes the other way for methods: on a stale addon it turns off block reuse with no log and no nativeAddonStatus() diagnostic, so a dev tree just gets slower and nothing tells you to rebuild. It also sets a per-callsite precedent, so every method later added to a native class needs its own typeof prototype.x guard. Two options: handle method drift in the loader so the class-level stub reports it with the same message, or keep this fallback and add a one-time logger.warn that includes nativeAddonStatus(). Released installs and compiled binaries validate the addon version, so they never reach this branch.

Comment thread packages/tui/CHANGELOG.md
- Key hints draw a theme's `symbols.overrides` for `key.*` keys, so a theme's Esc glyph now reaches the interrupt hint ([#20926](https://github.com/can1357/oh-my-pi/issues/20926), [#21128](https://github.com/can1357/oh-my-pi/pull/21128) by [@JamieJ5926](https://github.com/JamieJ5926)).
- Edit diffs preserve each line’s original spacing when highlighting changed words. Long replacements with many differing words skip word highlighting to keep rendering responsive ([#21201](https://github.com/can1357/oh-my-pi/pull/21201) by [@JustasMasiulis](https://github.com/JustasMasiulis)).
- A bracketed paste that passes its byte cap no longer delivers the rest of its body as keystrokes; the body is dropped up to the end marker ([#21124](https://github.com/can1357/oh-my-pi/pull/21124) by [@JamieJ5926](https://github.com/JamieJ5926)).
- Multi-line code in diffs, file previews and tool output renders again instead of crashing with `stream.preview is not a function` when the loaded native addon is older than the JavaScript that calls it (a workspace tree on its previous release's addon).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fix: this external contribution needs the attribution required by AGENTS.md: ([#21252](https://github.com/can1357/oh-my-pi/pull/21252) by [@andreyors](https://github.com/andreyors)). The PR checklist marks it done, but the entry doesn't have it. Also, released installs validate the addon version, so only workspace and locally built trees hit this. Consider saying that, so users of published builds don't read it as a fix for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix review:p2 triaged tui Terminal UI rendering and display

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants