Skip to content

fix(beam): clip mirrored session text without splitting surrogate pairs - #117671

Merged
steipete merged 1 commit into
openclaw:mainfrom
zenglingbiao:fix/beam-mirror-utf16-clip
Aug 2, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
zenglingbiao:fix/beam-mirror-utf16-clip

Conversation

@zenglingbiao

Copy link
Copy Markdown
Contributor

Summary

Clip beam-mirrored session titles and transcript text with truncateUtf16Safe instead of UTF-16 code-unit slice, so emoji/CJK characters straddling the clip boundary no longer reach the Beam receiver as a lone surrogate (tofu).

What Problem This Solves

extensions/beam/src/mirror.ts uploads local coding-session metadata to a remote Beam receiver. Two user-visible text fields are bounded by raw UTF-16 code-unit slicing:

  • mirror.ts:137 — clipText cuts transcript message text at BEAM_MAX_ITEM_CHARS (6000) with text.slice(0, ...).
  • mirror.ts:328 — the upload title is cut at 160 with candidate.title.slice(0, 160). The title comes from the session name, which users control.

Any character outside the Basic Multilingual Plane (emoji, CJK ext-B) occupies two UTF-16 code units. When such a character straddles the clip boundary, slice keeps only the leading high surrogate, producing a lone surrogate that renders as tofu/invalid glyph on the Beam receiver — on every mirrored session row whose name has an emoji at the boundary, and on any clipped long message.

Code evidence: extensions/beam/src/mirror.ts:137 and mirror.ts:328 both bounded user-controlled text with code-unit slice(0, n).

Root Cause

  • Introduced by commit 7e8afba7032 ("feat(plugins): mirror local coding sessions to a remote Beam receiver", 2026-07-27, feat(plugins): mirror local coding sessions to a remote Beam receiver #114735), which added both clip sites with code-unit slicing.
  • Violated invariant: "clip to N characters" only holds for BMP text when implemented as slice(0, N); astral characters occupy two UTF-16 units, so the cut can keep half a character.
  • Trigger: name a session so that code unit 160 falls inside an emoji (e.g. 159 ASCII chars + 🙂), or send a long message whose 6000th code unit is the first half of an emoji, then let the mirror upload it.

Why This Fix

  • Use truncateUtf16Safe from openclaw/plugin-sdk/text-utility-runtime — the repo's established convention for exactly this bug class (already used in sessions-cleanup, signal install, and elsewhere; fix(terminal): keep Unicode cron columns aligned #117062 introduced it for display-width text).
  • truncateUtf16Safe(text, n) is a drop-in for the length > n ? slice(0, n) : text shape (same length contract) but drops a dangling surrogate at the cut instead of emitting half a character.
  • Kept the surrounding logic untouched: same caps (6000 / 160), same truncation flags, no wire-shape changes.
  • Alternative considered: Array.from(text).slice(0, n).join("") (code-point clip) — changes the cap semantics from UTF-16 units to code points and allocates the whole string; the receiver contract is unit-based, so truncateUtf16Safe is the minimal correct fix.

User Impact

  • Affected operations: every mirrored session whose name or message text is clipped at an astral character boundary — the Beam receiver shows tofu in the session list title / transcript excerpt.
  • Before: receiver gets "Fix flow: xxx…\uD83D" — a dangling surrogate rendered as an invalid glyph.
  • After: receiver gets the same text clipped cleanly before the emoji ("Fix flow: xxx…"), no broken glyphs.

Evidence

  • Before: on main, a real loopback Beam receiver captures an upload whose session name places 🙂 across the 160-unit cap — received title is 160 units ending U+0078 U+D83D (lone high surrogate) → FAIL.
  • After: on this branch, the same upload arrives at 159 units ending U+0078 U+0078 — emoji dropped whole → PASS.

Real Behavior Proof

The proof runs the real createBeamMirrorRunner pipeline (config parse → candidate selection → upload build → real HTTP POST) against a real loopback Beam receiver and inspects the received title's trailing code units. Session name: 159 code units + 🙂 (161 units), so the emoji straddles the 160-unit cap.

Before (on main)

$ node --import tsx proof.mjs
session_name_units=161
received_title_units=160 tail_units=U+78 U+d83d
FAIL: beam received a title ending in a lone UTF-16 surrogate (tofu)

After (with fix)

$ node --import tsx proof.mjs
session_name_units=161
received_title_units=159 tail_units=U+78 U+78
PASS: title clipped without splitting the emoji

Tests and Validation

  • Added two regression tests in extensions/beam/src/mirror.test.ts: buildBeamMirrorItems must not split a surrogate pair when clipping message text at the 6000-unit cap, and the runner must upload a session title with no lone surrogate when the emoji straddles the 160-unit cap. Both fail on main and pass with the fix; the other 15 tests are unchanged.
  • node scripts/run-vitest.mjs extensions/beam/src/mirror.test.ts — 17/17 tests passed.
  • oxfmt --check and oxlint clean on both changed files; git diff --check clean.
  • Sibling audit: these are the only two text-clip sites in the mirror path (fitBeamMirrorUpload drops whole items by byte budget and never slices text); the same truncateUtf16Safe convention was applied to sessions cleanup in fix(cli): align sessions cleanup label summary by visible width #117459.

@openclaw-barnacle openclaw-barnacle Bot added plugin: beam Beam session sharing plugin size: XS labels Aug 1, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 6:49 PM ET / 22:49 UTC.

ClawSweeper review

What this changes

The PR replaces raw UTF-16 slicing of Beam mirror transcript text and session titles with surrogate-safe truncation, then adds regressions for emoji at each clipping boundary.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open: current main still slices Beam mirror text at raw UTF-16 code-unit boundaries, while this branch uses the already-published surrogate-safe SDK helper at both affected limits without changing the caps or upload shape. The PR body provides before/after loopback receiver output and the patch adds focused regression coverage. Likely related people: steipete (high-confidence feature owner) and zenglingbiao (medium-confidence adjacent shared-helper contributor).

Priority: P2
Reviewed head: 9fb9dadf15af4cf1b9001ba7a484985da54bdcb1

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A narrow owner-boundary repair with direct before/after behavior evidence, regression tests, and no actionable correctness or security finding.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.
Evidence reviewed 5 items Current-main defect: Current main still truncates transcript text with String.slice at the 6,000-code-unit cap and session titles with String.slice at the 160-code-unit cap; either can emit a lone high surrogate when an astral character crosses the boundary.
Shared helper contract: truncateUtf16Safe preserves inputs within the limit and otherwise delegates to sliceUtf16Safe, which moves a cut left when it would split a high/low surrogate pair. The plugin SDK facade exports that helper through the public text-utility runtime seam.
Focused patch and regressions: The proposed head imports the public plugin SDK helper and applies it at both current-main clipping sites; its two tests cover a 6,000-unit transcript boundary and a 160-unit title boundary through the mirror runner.
Findings None None.
Security None None.

How this fits together

Beam mirror reads active local coding-session catalogs, reduces their transcripts to a redacted bounded upload, and POSTs that upload to a remote Beam receiver. Titles and message text are constrained before payload fitting so receiver rows remain valid and readable.

flowchart LR
  Catalogs[Local session catalogs] --> Mirror[Beam mirror]
  Mirror --> Reduce[Redact and reduce transcript]
  Reduce --> Clip[UTF-16-safe text clipping]
  Clip --> Fit[Bounded upload payload]
  Fit --> Receiver[Remote Beam receiver]
Loading

Before merge

  • Complete next step (P2) - This is already a focused implementation PR with no discrete repair finding; it should proceed through normal current-head CI and maintainer merge review rather than a replacement fix lane.
Agent review details

Security

None.

PR surface

Source +1, Tests +25. Total +26 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 3 2 +1
Tests 1 25 0 +25
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 28 2 +26

Review metrics

None.

Technical review

Best possible solution:

Retain the existing UTF-16 unit caps and wire format, apply the public surrogate-safe SDK helper at both Beam mirror clip sites, and merge after normal current-head CI and maintainer review complete.

Do we have a high-confidence way to reproduce the issue?

Yes, a high-confidence source reproduction is available: on current main, a title of 159 ASCII code units followed by an emoji reaches the 160-unit slice with only the emoji's high surrogate. The PR also supplies before/after loopback receiver output for that exact path.

Is this the best way to solve the issue?

Yes. Using the existing public plugin SDK truncation helper fixes the owner-boundary defect at both clip sites while retaining the receiver's code-unit limits; code-point slicing would alter those limits and duplicate the established contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against e49c8339b857.

Labels

Label changes:

  • add P2: The patch repairs a bounded but user-visible corruption of mirrored Beam titles and transcript excerpts when a clipped value crosses an astral-character boundary.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.

Label justifications:

  • P2: The patch repairs a bounded but user-visible corruption of mirrored Beam titles and transcript excerpts when a clipped value crosses an astral-character boundary.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes before/after terminal output from a real loopback Beam receiver using the mirror runner and HTTP upload path, directly showing the removed lone-surrogate result.

Evidence

What I checked:

  • Current-main defect: Current main still truncates transcript text with String.slice at the 6,000-code-unit cap and session titles with String.slice at the 160-code-unit cap; either can emit a lone high surrogate when an astral character crosses the boundary. (extensions/beam/src/mirror.ts:137, e49c8339b857)
  • Shared helper contract: truncateUtf16Safe preserves inputs within the limit and otherwise delegates to sliceUtf16Safe, which moves a cut left when it would split a high/low surrogate pair. The plugin SDK facade exports that helper through the public text-utility runtime seam. (packages/normalization-core/src/utf16-slice.ts:58, e49c8339b857)
  • Focused patch and regressions: The proposed head imports the public plugin SDK helper and applies it at both current-main clipping sites; its two tests cover a 6,000-unit transcript boundary and a 160-unit title boundary through the mirror runner. (extensions/beam/src/mirror.ts:138, 9fb9dadf15af)
  • Feature provenance: The mirror feature dates to the merged Beam mirror implementation; current blame attributes the surviving clipping lines to the later Beam-file maintenance commit, so both the original feature owner and recent area maintainer are relevant routing contacts. (extensions/beam/src/mirror.ts:137, 7e8afba70325)
  • After-fix behavior proof: The PR body records a real loopback Beam receiver run: a 161-unit title ending in an emoji arrives from main as 160 units ending in a lone high surrogate, while the proposed branch sends the safe 159-unit prefix. The supplied proof also states that the real runner performed candidate selection, payload construction, and an HTTP POST. (9fb9dadf15af)

Likely related people:

  • steipete: Peter Steinberger authored the merged Beam mirror feature commit, and current blame attributes the surviving mirror clipping lines to a later Beam-file maintenance commit by the same contributor. (role: feature owner and recent area contributor; confidence: high; commits: 7e8afba70325, 586e1fe10e11; files: extensions/beam/src/mirror.ts)
  • zenglingbiao: The contributor previously authored the merged sessions-cleanup Unicode repair that used the same truncation family, providing adjacent history for the chosen shared-helper approach. (role: adjacent shared-helper contributor; confidence: medium; commits: 37e7307b0147; files: src/commands/sessions-cleanup.ts, extensions/beam/src/mirror.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete

steipete commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Maintainer verification for exact contributor head 9fb9dadf15af4cf1b9001ba7a484985da54bdcb1:

  • Root cause: Beam’s outbound mirror owner split UTF-16 surrogate pairs at both existing caps (session titles: 160; transcript items: 6,000). The fix reuses the existing public SDK truncateUtf16Safe helper at both producer sites; no new dependency, configuration, API, or compatibility path.
  • Independently verified real production mirror sender → loopback TCP/HTTP → actual Beam receiver → persisted store → session catalog before and after: main produces lone surrogates/UTF-8 replacement corruption at both boundaries; this head persists well-formed Unicode at 159/5,999 units, while complete boundary emoji remain intact. Existing catalog consent, transcript redaction, bearer authentication, receiver scopes, and privacy boundaries are unchanged.
  • Focused proof: node scripts/run-vitest.mjs extensions/beam/src/mirror.test.ts — 17/17 passed. Fresh gpt-5.6-sol autoreview at xhigh reasoning: clean, confidence 0.99. Exact-head CI 30721924161 and Workflow Sanity 30721924144 both succeeded.

Thanks @zenglingbiao for the precise producer-owned fix and regression coverage.

@steipete
steipete merged commit 846a454 into openclaw:main Aug 2, 2026
94 of 101 checks passed
@steipete

steipete commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. plugin: beam Beam session sharing plugin proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants