Skip to content

fix(ui): offload large diff highlighting to a worker - #759

Merged
benvinegar merged 18 commits into
mainfrom
feat/highlight-worker-offload
Aug 16, 2026
Merged

benvinegar merged 18 commits into
mainfrom
feat/highlight-worker-offload

Conversation

@benvinegar

Copy link
Copy Markdown
Member

Summary

  • offload eligible interactive large-diff syntax highlighting to one Bun worker while retaining the existing HAST row pipeline
  • bundle the worker in compiled Hunk binaries and keep static/headless commands on their existing paths
  • fall back to plain text if the worker fails, without re-running the expensive highlight on the terminal event loop

Validation

  • bun run typecheck
  • bun test src/ui/diff/diffRows.test.ts test/pty/highlighting.test.ts
  • bun run build:bin
  • HUNK_TEST_EXECUTABLE=dist/hunk bun test test/pty/highlighting.test.ts test/cli/compiled-headless-native-lib.test.ts
  • bun run test:integration
  • bun run test:tty-smoke

This PR description was generated by Pi using gpt-5.6-sol

@vercel

vercel Bot commented Aug 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hunk-web Ready Ready Preview Aug 16, 2026 6:34pm

Request Review

@greptile-apps

greptile-apps Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR moves eligible large-diff syntax highlighting into a serialized Bun worker while retaining the existing HAST-to-row pipeline and plain-text request fallback.

  • Embeds and registers a dedicated highlighting worker in compiled interactive binaries.
  • Offloads bundled-theme diffs of at least 2,000 lines while leaving custom themes and headless paths unchanged.
  • Adds worker equivalence, failure-fallback, compiled-binary, and PTY responsiveness coverage.

Confidence Score: 4/5

The PR appears safe to merge, with a non-blocking reliability gap where worker construction failure can abort interactive startup rather than degrade to plain text.

The request-time worker path has explicit fallback behavior and targeted tests, but the eager constructor runs before that protection and before the interactive application starts.

Files Needing Attention: src/main.tsx

Important Files Changed

Filename Overview
src/main.tsx Registers and disposes the interactive worker, but eager construction can bypass the request-time plain-text fallback.
src/highlightWorkerClient.ts Introduces a shared serialized request queue with active-request matching, reset, rejection, and disposal behavior.
src/ui/diff/highlightWorker.ts Implements the worker-side protocol and reproduces the existing Pierre/Shiki render options.
src/ui/diff/diffRows.ts Routes eligible large bundled-theme diffs through the worker and retains existing inline and source-remapping paths.
scripts/build-bin.ts Adds the worker module as a second compiled Bun entrypoint.
test/pty/highlighting.test.ts Adds an end-to-end responsiveness and eventual-highlight assertion for a large diff.

Sequence Diagram

sequenceDiagram
  participant UI as Interactive diff UI
  participant Cache as Highlight cache
  participant Client as Worker client queue
  participant Worker as Highlight worker
  UI->>Cache: Request large-diff highlight
  Cache->>Client: Enqueue metadata and theme
  Client->>Worker: postMessage(request)
  Worker->>Worker: Highlight with Pierre/Shiki
  Worker-->>Client: HAST line arrays
  Client-->>Cache: Resolve request
  Cache-->>UI: Build highlighted rows
  alt Request-time worker failure
    Client-->>Cache: Reject request
    Cache-->>UI: Render plain-text rows
  end
Loading
Prompt To Fix All With AI
### Issue 1
src/main.tsx:106
**Eager worker construction bypasses fallback**

If worker construction fails because the entrypoint cannot be resolved or the runtime cannot allocate the worker, this eager constructor rejects `main()` before the TUI starts, causing Hunk to exit instead of degrading syntax highlighting to plain text.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "fix(ui): offload large diff highlighting" | Re-trigger Greptile

Comment thread src/main.tsx Outdated
await runInteractiveApp(startupPlan);
// materialize its embedded native library. Bun only resolves compiled worker entrypoints from
// this executable entrypoint, so start and register the worker here instead of in a UI module.
const highlightWorker = new Worker(new URL("./highlightWorker.js", import.meta.url).href);

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.

P2 Eager worker construction bypasses fallback

If worker construction fails because the entrypoint cannot be resolved or the runtime cannot allocate the worker, this eager constructor rejects main() before the TUI starts, causing Hunk to exit instead of degrading syntax highlighting to plain text.

Knowledge Base Used: CLI Entrypoint and Startup Lifecycle

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/main.tsx
Line: 106

Comment:
**Eager worker construction bypasses fallback**

If worker construction fails because the entrypoint cannot be resolved or the runtime cannot allocate the worker, this eager constructor rejects `main()` before the TUI starts, causing Hunk to exit instead of degrading syntax highlighting to plain text.

**Knowledge Base Used:** [CLI Entrypoint and Startup Lifecycle](https://app.greptile.com/modem/-/custom-context/knowledge-base/modem-dev/hunk/-/docs/cli-entrypoint.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@benvinegar
benvinegar force-pushed the feat/highlight-worker-offload branch from 3148deb to 3feeac8 Compare August 16, 2026 05:06
@benvinegar benvinegar changed the title fix(ui): offload large diff highlighting fix(ui): offload large diff highlighting to a worker Aug 16, 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