Skip to content

fix(shared): text cuts no longer leave half an emoji - #261

Merged
AadiJo merged 4 commits into
mainfrom
code-point-safe-truncate
Nov 15, 2026
Merged

AadiJo merged 4 commits into
mainfrom
code-point-safe-truncate

Conversation

@AadiJo

@AadiJo AadiJo commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

.slice() counts UTF-16 units, so a cut that lands inside an emoji leaves half of it behind. In October that lone surrogate made a Codex turn/start line unparsable, and the CAD follow-up was dropped after a 30 second timeout. The fix went into one local clip in CadDrivetrain.ts. Every other cut in the repo still had the bug.

What changed

clip(text, max, keep = "start") now lives in packages/shared/src/String.ts and is the one way to cut text to a length. It returns at most max units, never half of a surrogate pair, and always well formed text. keep: "end" keeps the tail instead of the head.

truncate stays as a second function and now calls clip. The two have different jobs: clip is the hard limit, and truncate is the title cut that trims and appends "...". Other display cuts keep their own ellipsis and budget around clip, so their output is the same as before.

51 call sites in 33 files now go through clip: the 8 existing ones, plus every cut of text for a user, an agent or a wire payload in the server, web, desktop, shared and client-runtime. 8 of them are tail cuts such as slice(-8_000) in the title prompt, which can leave the other half of a pair.

Behavior

No behavior change beyond the fix. A cut that used to land mid-pair now lands one unit earlier. Text that already carries a lone surrogate now leaves a cut with U+FFFD in its place.

clip keeps whole code points, not whole graphemes. A cut can still land inside a ZWJ sequence or a flag.

Left alone

  • Array slices, hashes, ids, index splices and other ASCII-only cuts.
  • packages/featurescript, which has no dependency on @cadsense/shared. Preview.ts:662 still cuts an input's default value at 80 units with slice. The two conformance report cuts carry a lint disable that says why.
  • Dev scripts under scripts/ and apps/server/scripts/.
  • providerInstanceInitials picks word[0]. That needs a first code point, not a length cut.
  • The preview snapshot cuts innerText inside the page, where the helper cannot be imported. The main process now clips the result.

Lint rule

cadsense/no-slice-truncation reports a slice, substring or substr written right beside an ellipsis, in a template literal or a +. On main that shape matches 22 lines: 21 text cuts and 1 hex trace id, which keeps its slice under a disable comment.

The rule cannot see a bare name.slice(0, 120), the shape of the original bug. Without type information that is the same syntax as an array slice.

Verification

  • String.test.ts lists 17 ways a cut can fail, written before the helper, and tests them: a pair on the boundary, a lone surrogate already in the input, a max of 0, ZWJ sequences, negative, fractional and NaN limits.
  • CodexFollowUpRuntime.integration.test.ts runs the October repro through the real path. A part name is padded so an emoji sits on the collision title's 160 unit cut, then goes through draftCadComments, followUpMessage and the real CodexSessionRuntime to the raw turn/start line the mock app-server reads. The line has no lone surrogate escape. With the old slice put back, the test fails on a title that ends in half an emoji.
  • serde_json 1.0.154 parses that line. With \ud83d put back where the old cut ended, it fails with unexpected end of hex escape, the error from October.
  • Each run of the integration test leaves the line it sent at cadsense-follow-up-turn-start.json in the OS temp directory.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
cadsense Skipped Skipped Nov 15, 2026 5:42am UTC

@AadiJo
AadiJo merged commit b2ac959 into main Nov 15, 2026
12 checks passed
@AadiJo
AadiJo deleted the code-point-safe-truncate branch November 15, 2026 05:45

This branch was previously deployed

1 inactive deployment
Preview — f94f6a14 Deployed Nov 15, 2026 by vercel[bot]
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