Skip to content

fix(web): element context preview no longer ends in a split surrogate - #42585

Merged
juliusmarminge merged 3 commits into
pingdotgg:mainfrom
only21mil:fix/web-element-context-surrogate-truncate
Nov 25, 2026
Merged

juliusmarminge merged 3 commits into
pingdotgg:mainfrom
only21mil:fix/web-element-context-surrogate-truncate

Conversation

@only21mil

Copy link
Copy Markdown
Contributor

Problem

truncateString in apps/web/src/lib/elementContext.ts clamps a picked element's htmlPreview and styles to 4000 UTF-16 code units with slice(0, 3999) + "…". When a surrogate pair (such as an emoji) straddles index 3999, the slice leaves a lone high surrogate before the ellipsis, producing an ill-formed string that renders as �… instead of dropping the split pair cleanly.

This reproduces in the desktop Browser panel when picking an element with a long title (react-grab keeps attributes like id, title, alt, aria-label, and src whole, so the preview exceeds 4000 characters), adding a comment, attaching, and sending the annotation through previewAnnotationContextRecord → normalizeElementContextSelection: the resulting preview ends in �….

Change

truncateString in apps/web/src/lib/elementContext.ts now computes cut = Math.max(0, limit - 1) and reuses the existing splitsSurrogatePair helper from @t3tools/shared/assistantCitations: when cut splits a surrogate pair, it slices to cut - 1 before appending "…" so no lone high surrogate remains.

Added the unit test "drops a straddling surrogate pair instead of leaving a lone high surrogate" in apps/web/src/lib/elementContext.test.ts covering both htmlPreview and styles.

Scope

Bug fix only. This is a small fix of an obvious bug in a single helper, so no separate issue was filed.

Verification

Setup: An AI agent (Claude Opus 5.5) ran the desktop verification on Linux (Ubuntu 26.04) with the T3 Code desktop dev build (vp run dev:desktop, Electron 44.10.1) on a 2560x1600 virtual display, light theme, fresh isolated T3 home with synthetic data on each commit, server named "Test machine", and the free Replay (dev) provider (the replay agent script from apps/server/scripts, added to the isolated settings because desktop dev does not register it), against a locally served fixture page containing <div id="note" title="…">Release notes</div> whose title is 3976 "a" followed by 30 "😀".

Steps: Open a thread in sample-app and send "Hello" → open the Browser panel (Ctrl+Shift+J) → load the fixture page → Annotate preview → select the "Release notes" card → type "Check this card" → Attach → Send → open the sent message's "Preview annotation, Check this card" chip details → scroll the HTML preview to its end.

Before (f46f17e) After (fd23cee)
Base f46f17e: htmlPreview ends "aaaa�…" (length 4000, last 3 code units U+0061 U+D83D U+2026, isWellFormed false)
Base f46f17e: htmlPreview ends "aaaa�…" (length 4000, last 3 code units U+0061 U+D83D U+2026, isWellFormed false)
Head fd23cee: htmlPreview ends "aaaa…" (length 3999, last 3 code units U+0061 U+0061 U+2026, isWellFormed true)
Head fd23cee: htmlPreview ends "aaaa…" (length 3999, last 3 code units U+0061 U+0061 U+2026, isWellFormed true)
revision f46f17e46f1fcd2b7ba9985e043170388fa4fb21 (dev:desktop, Browser panel pick of div#note, sent with Replay (dev))
htmlPreview length 4000
last 3 code units U+0061 U+D83D U+2026
isWellFormed false

revision fd23cee931ef8715e44ce04f29adcae5a33aa89d (dev:desktop, Browser panel pick of div#note, sent with Replay (dev))
htmlPreview length 3999
last 3 code units U+0061 U+0061 U+2026
isWellFormed true

An AI agent (Gemini 4 Neon) ran vp test run apps/web/src/lib/elementContext.test.ts against base f46f17e with the new test (1 failed | 1 passed: AssertionError: expected true to be false at the lone-surrogate check on htmlPreview), then ran it 3 times on head fd23cee (3/3 runs passed, 2/2 tests each) and ran vp run check:changed (format, lint, knip, typecheck, unit test, and React Compiler checks all passed).

Test and check:changed output
# Base f46f17e with new test
 FAIL  |@t3tools/web (unit)| src/lib/elementContext.test.ts > normalizeElementContextSelection > drops a straddling surrogate pair instead of leaving a lone high surrogate
AssertionError: expected true to be false // Object.is equality
 ❯ src/lib/elementContext.test.ts:54:56
     54|     expect(/\p{Surrogate}/u.test(result!.htmlPreview)).toBe(false);
 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)

# Head fd23cee (3 runs)
 ✓ |@t3tools/web (unit)| src/lib/elementContext.test.ts (2 tests) 2ms
 Test Files  1 passed (1)
      Tests  2 passed (2)
 ✓ |@t3tools/web (unit)| src/lib/elementContext.test.ts (2 tests) 3ms
 Test Files  1 passed (1)
      Tests  2 passed (2)
 ✓ |@t3tools/web (unit)| src/lib/elementContext.test.ts (2 tests) 2ms
 Test Files  1 passed (1)
      Tests  2 passed (2)

# vp run check:changed
check:changed: 2 files since f46f17e46f1f (upstream/main)
check:changed summary
  pass  format  0.7s
  pass  lint  0.9s
  pass  knip 1/2  10.6s
  pass  knip 2/2  19.6s
  pass  typecheck (2 packages)  9.2s
  pass  test (1 files)  11.2s
  pass  react compiler (1 files)  9.4s

Not shown: Web-only and mobile clients cannot create a picked element (the Browser panel pick is desktop-only) and only display the stored, already-normalized value, so no separate captures were taken there. Only Linux desktop was run. Grapheme clusters (ZWJ, skin tones) are out of scope: the clamp may still split a multi-code-point emoji between whole code points, which stays well formed.

Generated with Gemini 4 Neon in T3 Code Nightly

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Nov 25, 2026
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

String truncation now checks whether its cut point splits a surrogate pair. When it does, the function shortens the slice before adding an ellipsis. A test covers htmlPreview and styles when the truncation boundary falls within an emoji.

Changes: Surrogate-safe truncation

Layer / File(s) Summary
Adjust truncation and test boundary behavior
apps/web/src/lib/elementContext.ts, apps/web/src/lib/elementContext.test.ts
truncateString checks for a surrogate-pair split and shortens the slice when needed. The test checks the normalized htmlPreview and styles when the truncation boundary falls within an emoji.

Estimated code review effort: 2 (Simple) | ~7 minutes

Change: Bug fix

Suggested labels: bugfix

Suggested reviewers: macroscopeapp, juliusmarminge

Merge Risk: Minimal · up to 5b069

This change prevents element context previews from ending in a lone surrogate when truncation falls inside an emoji. The change is small and covered by a test, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: element context preview truncation no longer leaves a split surrogate. The raw summary and objectives confirm that truncateString now backs up past a split surrogate p…
Description check Passed The description follows the repository template closely. It has Problem, Change, Scope, and Verification sections. The Problem section explains the bug and reproduction steps. The Change section expla…
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.
Full details: Title check

Explanation

The title names the main change: element context preview truncation no longer leaves a split surrogate. The raw summary and objectives confirm that truncateString now backs up past a split surrogate pair before appending the ellipsis. The title is short, specific, and follows the conventional commit format from the template.

Full details: Description check

Explanation

The description follows the repository template closely. It has Problem, Change, Scope, and Verification sections. The Problem section explains the bug and reproduction steps. The Change section explains the fix and names the reused helper. The Scope section explains why no issue was filed. The Verification section gives the focused test results (failing on base, passing 3/3 on head), the check:changed results, and the desktop before/after captures. It also states what was not checked (web, mobile, Linux only, grapheme clusters). The description does not link an issue, but the template allows a short justification for an obvious bug, and the author gave one.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fd23cee and 5b069b9.

Files selected for processing: 1

  • apps/web/src/lib/elementContext.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Nov 25, 2026
@juliusmarminge

Copy link
Copy Markdown
Member

Evidence

Evidence: Linux Electron, fresh synthetic databases, Replay (dev), light theme, 1280×900 on both commits. Serve div#note with a title containing 3976 a characters and 30 😀 emoji; annotate it with “Check this card”, attach, send, open the sent chip and scroll the HTML preview to its end.

Before After
4da8d19d08 5b069b94cd
Before: replacement glyph before ellipsis After: clean ellipsis without broken emoji
4000 UTF-16 units; ends D83D 2026; malformed UTF-16. 3999 UTF-16 units; ends 0061 2026; well-formed UTF-16.
Full capture · Recording Full capture · Recording

Images are 2× crops of the unchanged full captures. Reproduction driver, synthetic seed, fixture and capture scripts.

Not shown:

  • Styles-field truncation was not visually verified: the sent annotation details expose HTML but do not display the selected element's styles field.

@juliusmarminge juliusmarminge added takeover:ready Takeover done; auto-lands when gates pass and removed takeover:in-progress A takeover lead is working on this PR labels Nov 25, 2026
@juliusmarminge
juliusmarminge merged commit 4e6b1fa into pingdotgg:main Nov 25, 2026
57 checks passed
@juliusmarminge juliusmarminge removed the takeover:ready Takeover done; auto-lands when gates pass label Nov 25, 2026
github-actions Bot added a commit to omarcresp/t3code-flake that referenced this pull request Nov 25, 2026
**v0.1.12-nightly.20261125.5230** · 41 changes from 11 contributors

- **Richer workflow questions.** Workflow questions can include described choices, required reasons, links, and records of who answered and when.
- **Thread actions in command palette.** Pin, snooze, settle, archive, and manage the current thread from the command palette.
- **Withdraw workflow questions.** Workflows can withdraw waiting questions without cancelling the run.
- **Antigravity swarm workers.** See Antigravity swarm workers as subagents, including who is working and what they found.
- **Faster thread switching.** Thread timelines now appear in the first frame instead of after a noticeable delay.
- **Browser select menus.** Native dropdown menus now open in streamed browser tabs so you can choose an option.
- **Permanent thread deletion.** Deleting a thread now removes its conversation history instead of only hiding it.

### Plus 32 more bug fixes.

<details>
<summary>Full changelog</summary>

## What's Changed
* fix(cli): exclude spans before the trace summary window by @jadelike-wine in pingdotgg/t3code#42571
* fix(grok): expose context usage by @gujunxiang in pingdotgg/t3code#42568
* fix(web): timeline minimap previews no longer cut an emoji in half by @only21mil in pingdotgg/t3code#42575
* fix(web): Option and Cmd arrows reach full-screen terminal apps by @Mnigos in pingdotgg/t3code#42574
* fix(web): element context preview no longer ends in a split surrogate by @only21mil in pingdotgg/t3code#42585
* fix(server): Antigravity sends undecodable text attachments as a path instead of failing the turn by @juliusmarminge in pingdotgg/t3code#42594
* fix(resource-monitor): Windows stops reading memory and command lines of untracked processes by @juliusmarminge in pingdotgg/t3code#42577
* fix(server): ODT attachments no longer start with text from number styles by @only21mil in pingdotgg/t3code#42592
* feat(server): workflow questions take described choices, required reasons, links, and keep who answered by @juliusmarminge in pingdotgg/t3code#42515
* fix(web): citation comment box stays open when its cited row scrolls out by @juliusmarminge in pingdotgg/t3code#42603
* fix(web,mobile): author avatars keep emoji initials whole by @only21mil in pingdotgg/t3code#42598
* fix(gitlab): incident links are read as issue links by @Michel-Liao in pingdotgg/t3code#42605
* fix(server): workflow schedules given as bigint nanoseconds no longer crash by @only21mil in pingdotgg/t3code#42607
* fix(pi): reject generated text from failed responses by @StiensWout in pingdotgg/t3code#42602
* fix(preview): streamed browser tabs show native select menus by @juliusmarminge in pingdotgg/t3code#31561
* fix(web): long Workflows links no longer end in a broken character by @only21mil in pingdotgg/t3code#42617
* fix(server): filter open device sessions by the selected host by @AppsAtMe in pingdotgg/t3code#42600
* fix(antigravity): link directly to Google account verification on failed turns by @t3dotgg in pingdotgg/t3code#41210
* fix(server): Needs you lists paused runs older than the newest 500 by @KyrosHendrix in pingdotgg/t3code#42618
* feat(server): CI runners don't need telemetry by @t3dotgg in pingdotgg/t3code#42630
* fix: screenshots after a caption render full size instead of as chips by @juliusmarminge in pingdotgg/t3code#42627
* fix(pi): rewind turns after an extension switches sessions by @StiensWout in pingdotgg/t3code#42616
* fix(mobile): Compose without a project opens the picker in one push by @juliusmarminge in pingdotgg/t3code#42593
* fix(web): project name field restores the saved name when cleared by @only21mil in pingdotgg/t3code#42622
* fix(mobile): a share sent while a new-task draft is open joins that draft by @juliusmarminge in pingdotgg/t3code#42597
* fix(telemetry): honor DO_NOT_TRACK by @t3dotgg in pingdotgg/t3code#42629
* fix(server): device host errors no longer cut an emoji in half by @only21mil in pingdotgg/t3code#42628
* fix(pi): let background subagents finish after coordinator errors by @StiensWout in pingdotgg/t3code#42624
* fix(server): deleting a thread actually removes its history by @t3dotgg in pingdotgg/t3code#42119
* fix(server): unreadable sessions no longer block older history by @StiensWout in pingdotgg/t3code#42613
* fix(server): skip incomplete Pi records during project discovery by @StiensWout in pingdotgg/t3code#42636
* fix(server): restore the default provider name when clearing a label by @StiensWout in pingdotgg/t3code#42632
* test(provider-testing): replay workspaces start no background git maintenance by @juliusmarminge in pingdotgg/t3code#42651
* fix(antigravity): /swarm workers show as subagents by @t3dotgg in pingdotgg/t3code#42649
* feat(web): settle, pin, snooze and more for the current thread from cmd+k by @t3dotgg in pingdotgg/t3code#40742
* feat(server): withdraw a waiting workflow question without cancelling its run by @juliusmarminge in pingdotgg/t3code#42506
* fix(web): fade project picker tooltips in place on scroll by @Yash-Singh1 in pingdotgg/t3code#42657
* fix(server): shells with a backslash in their name no longer get zsh flags on Linux and macOS by @only21mil in pingdotgg/t3code#42648
* perf(web): thread switches paint the timeline in their first frame by @juliusmarminge in pingdotgg/t3code#42582
* fix(server): t3_background_wait error says Workflow runs already keep the thread working by @juliusmarminge in pingdotgg/t3code#42661
* fix(server): preserve spaces and tabs in checkpoint LFS storage by @jadelike-wine in pingdotgg/t3code#42634

## New Contributors
* @gujunxiang made their first contribution in pingdotgg/t3code#42568
* @AppsAtMe made their first contribution in pingdotgg/t3code#42600

**Full Changelog**: pingdotgg/t3code@v0.1.12-nightly.20261125.5222...v0.1.12-nightly.20261125.5230

</details>


Upstream release: https://github.com/pingdotgg/t3code/releases/tag/v0.1.12-nightly.20261125.5230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants