fix(server): thread handoff no longer splits emoji when truncating long messages #48289
main fromrefs/pull/48289/head·opened View on GitHubProblem#
When handing off a thread to a linked environment, ThreadHandoff.ts truncates long conversation messages with message.text.slice(0, 200_000). If a UTF-16 surrogate pair (such as an emoji) straddles UTF-16 index 199,999–200,000, the slice leaves a lone trailing high surrogate (\uD83D) in the imported message stored in the destination thread's projection.
Deterministic repro on origin/main (4b8199987cb63f82912b8f3dbe9de6a96cdb057b):
- Apply the added regression test in
apps/server/src/peer/handoff/ThreadHandoff.test.tson top of unmodifiedapps/server/src/peer/handoff/ThreadHandoff.tsfrom4b8199987cb63f82912b8f3dbe9de6a96cdb057b(a thread with an assistant message"a".repeat(199_999) + "🚀"handed off to a linked environment), and runvp test run apps/server/src/peer/handoff/ThreadHandoff.test.ts -t "does not split an emoji surrogate pair". - Observed on
origin/main:imported.text.isWellFormed()isfalsebecause the truncated message ends in lone high surrogate0xD83D. - Expected: The truncated message drops the trailing high surrogate at the cut boundary so
imported.text.isWellFormed()istrue(length === 199_999).
Change#
Added a small sliceSafe(text, max) helper in apps/server/src/peer/handoff/ThreadHandoff.ts that returns text unchanged when text.length <= max and backs up by one UTF-16 code unit when text.charCodeAt(max - 1) is a high surrogate (0xD800–0xDBFF), and applied it to conversation() (sliceSafe(message.text, 200_000)). Other .slice( calls under apps/server/src/peer/handoff/ only slice ASCII hex IDs (handoffId, packed.snapshot) or short ASCII upload-route refusal strings from apps/server/src/http.ts.
Scope#
Small fix for an obvious bug; no issue needed under CONTRIBUTING.md.
Verification#
- Base (
4b8199987cb63f82912b8f3dbe9de6a96cdb057b+ regression test only):truncating a long exported message does not split an emoji surrogate pairfailed (AssertionError: expected false to be trueonisWellFormed()). - Head (
7cf9312b5d7309ae5ed92fa871a5b559ffa73b20): The targeted regression test passed (1 passed | 12 skipped (13)). vp run check:changed: Exited0on7cf9312b5d7309ae5ed92fa871a5b559ffa73b20(format, lint, knip, graphql, typecheck across 3 packages, and all 15 tests across 2 affected test files including all 13 tests inThreadHandoff.test.ts).- Before/after +
check:changedtranscript: https://gist.github.com/saphid/38f1d421a87f4c4ed628fe084ff49c80 - Not shown: Repo-wide test suite and live multi-host handoff UI rendering (this fix is server-only text truncation; CI owns the full suite).
Authored by Gemini 4 Neon (Antigravity); reviewed by GPT-6.2 Sol (Codex).
Not shown#
- before-after-transcript.md:3 — The linked gist shows the older two-fix, 14-test patch and does not contain the claimed check:changed transcript. Replace it with SHA-tagged base-plus-regression and 7cf9312b5d73 output, including check:changed exit 0, or state that the exact-head logs are not linked.
- apps/server/src/orchestration-v2/http.ts:202 — Make the PR's observable result explicit: after handoff, fetch the destination thread snapshot and inspect the imported message in projection.messages; the decoded API response ends with a lone high surrogate on base and does not on head.
- scripts/prove-scenario.ts:2 — The deterministic Vitest repro is not an e2e scenario accepted by scenarios:prove; note that in Not shown.
- 1
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
Approvability#
Verdict: Approved at
7cf9312Macroscope's review found this PR approvable — Thread handoff now avoids splitting UTF-16 surrogate pairs when truncating long messages. Existing transfer limits and message selection remain unchanged, with no material expansion of risk.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).No actionable comments were generated in the recent review.
Walkthrough
-
Thread handoff now truncates messages without leaving a trailing high surrogate at the 200,000-code-unit limit. A new test checks the imported message when truncation reaches an emoji.
Changes: Thread handoff truncation
Layer / File(s) Summary Safe truncation and boundary test
apps/server/src/peer/handoff/ThreadHandoff.ts,apps/server/src/peer/handoff/ThreadHandoff.test.tsconversationusessliceSafeat the 200,000-code-unit limit. The test hands off a message with 199,999 ASCII characters followed by an emoji, then checks that the imported text is well-formed and has length 199,999.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers:
juliusmarminge
Merge Risk:
Minimal · up to7cf93-
Thread handoff now avoids cutting an emoji in half when a very long message is truncated, and a regression test covers the boundary case. No concrete merge-blocking risk remains after review.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the main change: thread handoff no longer splits an emoji when truncating long messages. It matches the changeset, which adds sliceSafe in ThreadHandoff.ts and applies it in conversati… Description check The description follows the repository template. It has Problem, Change, Scope, and Verification sections. The problem, the root cause (slice at 200_000 splitting a surrogate pair), the fix (sliceSafe… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the main change: thread handoff no longer splits an emoji when truncating long messages. It matches the changeset, which adds sliceSafe in ThreadHandoff.ts and applies it in conversation() to avoid leaving a lone high surrogate at the 200,000-code-unit limit. It follows the conventional commit format and is specific enough for history scanning.
Full details: Description check
-
Explanation
The description follows the repository template. It has Problem, Change, Scope, and Verification sections. The problem, the root cause (slice at 200_000 splitting a surrogate pair), the fix (sliceSafe applied in conversation()), and the scope rationale are all clearly stated. Verification gives base and head results with SHAs and a check:changed exit code. Reviewer notes flag gaps: the linked gist does not match the claimed check:changed transcript, the API-level observable result is not shown, and the repro is not an e2e scenario. The template also asks for the model and harness used; the author names Gemini 4 Neon and GPT-6.2 Sol, so that item is covered. The description is mostly complete, so it passes, but the verification evidence should be corrected.
-
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-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: main
- File: .coderabbit.config.ts
- Commit: 4b81999
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f86885a1-03c0-4c3b-afbe-eefde64a8bc6
Commits-
Reviewing files that changed from the base of the PR and between 4b8199987cb63f82912b8f3dbe9de6a96cdb057b and 7cf9312b5d7309ae5ed92fa871a5b559ffa73b20.
Files selected for processing: 2-
apps/server/src/peer/handoff/ThreadHandoff.test.tsapps/server/src/peer/handoff/ThreadHandoff.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
-
-
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes