Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(web): load file grammar before enabling edits #9947

Mergedjuliusmarmingemerged 3 commits intomain fromrefs/pull/9947/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Opening a worker-highlighted TSX file could leave the editor's main-thread highlighter without its grammar. The first edit then threw Grammar for language "tsx" not loaded, after emitting the changed contents but before completing the incremental render.

Move the existing file-language assignment before the worker branch in FileRenderer.hydrate and FileRenderer.renderFile. The editor already awaits highlighter initialization before enabling edits; that initialization now loads the actual file language. Read-only worker highlighting still does not load editor grammars on the main thread.

Native before and after

Both runs used a fresh Linux Chromium client, the same 8,323-line TSX fixture, and the app at f47a3fe9. The after run changed only this grammar patch. The existing stale-highlight fix was present in both; neither run included the separate viewport candidate. Read-only inspection captured actual editor state, without replacing methods or mocking the highlighter.

At the first native EnterBeforeAfter
Worker readytruetrue
Renderer languagetexttsx
Main-thread loaded languages[]["tsx"]
New page errors1, missing TSX grammar0

The after run also completed native undo/redo without new exceptions. A typed marker persisted on disk after closing the file and remained visible on reopening it.

After save and reopen, showing persistence rather than a layout fix:

After: the saved TSX marker remains visible when the file is reopened

Short native reopen recording

The first-edit screenshots look alike because the separate viewport jump remains. The runtime observations above and the regression tests establish the grammar repair; the screenshot and clip establish the saved-file result.

Verification

vp test run apps/web/src/components/files/fileEditorLanguageReadiness.test.ts apps/web/src/components/files/fileEditorHighlight.test.ts apps/web/src/components/files/fileContentRevision.test.ts apps/web/src/components/files/FilePreviewPanel.test.ts --maxWorkers=2

The tests use the installed Pierre worker and actual renderer, tokenizer and document classes. Worker-highlighted cases await the completed highlight, then apply the first edit without waiting for the tokenizer's delayed prebuild. On the unpatched main implementation at c7dc3cbd, six readiness cases fail with the exact missing-grammar exception and plain text passes. After the fix, all seven readiness cases pass, covering both rendering entry points, explicit language overrides, language switches and non-worker hydration.

All 26 focused tests, targeted lint and web typecheck pass on 61d8322a, refreshed onto 7a089b2b. The intervening main changes do not touch the editor. Independent review and reruns of the unchanged fix also pass.

This addresses a separate cold-language failure found while investigating #7907. It does not include the wrapped-row height-cache change or fix intermittent caret positioning on redo. Keep that report open.

GPT 6 Astra via Codex in T3 Code.


Note

Medium Risk Changes vendored renderer/highlighter lifecycle and worker cache eviction on edit; behavior is covered by new integration tests but touches core editing and highlighting paths.

Overview Fixes a crash on the first edit of worker-highlighted files (e.g. TSX) where the main-thread highlighter had not loaded the file’s grammar (Grammar for language "tsx" not loaded), even though read-only highlighting already ran in a worker.

The @pierre/diffs patch moves computedLang (explicit file.lang or inferred from the filename) to run before the worker vs main-thread branches in FileRenderer.hydrate and renderFile, so initializeHighlighter() (already awaited before edits) loads the correct language instead of staying on plain text. Read-only worker AST highlighting still does not pull editor grammars onto the main thread until the editor initializes. Incremental tokenization also cleans worker tasks and evicts the file highlight cache when applying dirty lines, and default editor options in the patch drop forced gutter/selection overrides while keeping useTokenTransformer: true.

Adds fileEditorLanguageReadiness.test.ts, which drives the real Pierre worker (via a Node worker transport), FileRenderer, tokenizer, and a synchronous “first Enter” edit to lock in hydrate/render paths, explicit language overrides, language switches, non-worker hydration, and plain-text files. pnpm-lock.yaml updates the patched @pierre/diffs hash.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 61d8322a4332f527afab8ea45b9587d535a57c25. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Compute file language before worker branch in FileRenderer.hydrate and renderFile

  • Moves the explicit-or-filename-inferred language computation ahead of the worker-manager branch in both FileRenderer.hydrate and FileRenderer.renderFile, so worker-backed rendering records the correct language before highlighting
  • Previously the language was only set in the local-highlighter path, so the first edit on a worker-backed file could lack the right grammar
  • Adds a Node WorkerTransport adapter and a suite of readiness tests in fileEditorLanguageReadiness.test.ts covering worker-backed hydrate/renderFile, explicit language, renderer reuse, and plain-text edits
  • Risk: renderFile now updates computedLang for every rendered file rather than only local-highlighted files; consumers relying on computedLang being unset for worker-backed renders should verify behavior
Macroscope summarized 61d8322.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire13.6 KiB13.6 KiB+9 B (+0.1%)15.1 KiB✅
      CodexThread snapshot wire7.0 KiB7.0 KiB+2 B (+0.0%)7.3 KiB✅
      CodexLive turn WebSocket wire6.6 KiB6.6 KiB+7 B (+0.1%)7.8 KiB✅
      CodexLive turn WebSocket decoded57.1 KiB57.1 KiB0 B (0.0%)66.4 KiB✅
      CodexLive turn messages10100 (0.0%)21✅
      ClaudeTotal thread wire13.6 KiB13.6 KiB+8 B (+0.1%)15.1 KiB✅
      ClaudeThread snapshot wire7.0 KiB7.0 KiB+4 B (+0.1%)7.3 KiB✅
      ClaudeLive turn WebSocket wire6.6 KiB6.6 KiB+4 B (+0.1%)7.8 KiB✅
      ClaudeLive turn WebSocket decoded57.9 KiB57.9 KiB0 B (0.0%)66.4 KiB✅
      ClaudeLive turn messages10100 (0.0%)21✅

      Baseline: 7a089b2 · PR result: 61d8322 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 113.8 KiB
      • Claude decoded thread snapshot: 114.5 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    3. Approvability

      Verdict: Approved at 61d8322

      Macroscope's review found this PR approvable — This is a narrowly scoped renderer bug fix that ensures worker-highlighted files initialize the correct grammar before editing. The runtime change is localized and backed by integration coverage for the affected rendering paths and language cases.

      Notes:

      • No code objects were reviewed. Approvability was decided on eligibility alone.
      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. juliusmarminge merged this2fa5ef4

    5. juliusmarminge closed this

Sign in to join this conversation.