Skip to content

fix(msteams): truncate reflection prompt on a UTF-16 boundary#96578

Merged
vincentkoc merged 1 commit into
openclaw:mainfrom
WeeLi-009:fix/msteams-reflection-truncate-surrogate
Jun 27, 2026
Merged

fix(msteams): truncate reflection prompt on a UTF-16 boundary#96578
vincentkoc merged 1 commit into
openclaw:mainfrom
WeeLi-009:fix/msteams-reflection-truncate-surrogate

Conversation

@WeeLi-009

@WeeLi-009 WeeLi-009 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary: The Teams feedback reflection prompt was truncated with a raw UTF-16 slice, so an emoji at the boundary became a lone surrogate fed into the LLM prompt. This uses truncateUtf16Safe to truncate on a code-point boundary.

What Problem This Solves

The Microsoft Teams feedback reflection prompt truncated the thumbed-down response with raw UTF-16 slicing. When the response crossed the 500-code-unit cap at an emoji or other surrogate-pair boundary, the prompt could include a lone surrogate.

Trigger input used for proof:

  • "a".repeat(499) + "🦞" + "b".repeat(20)
  • Old truncation with .slice(0, 500) keeps only the high surrogate d83e, then appends ...
  • That malformed string can fail downstream UTF-8/URI encoding and can produce replacement-character behavior in logs or transport text

Fix

extensions/msteams/src/feedback-reflection-prompt.ts now reuses the shared truncateUtf16Safe helper from openclaw/plugin-sdk/text-utility-runtime when capping the thumbed-down response before appending the reflection prompt ellipsis.

That keeps the existing 500-character range check and prompt escaping behavior, but avoids cutting a UTF-16 surrogate pair in half. If the boundary emoji does not fully fit, the helper trims before the pair and the prompt remains well-formed.

Evidence

Command, run from a temporary worktree on fix/msteams-reflection-truncate-surrogate:

node --import tsx demo.mts

The demo imports the real fixed function:

import { buildReflectionPrompt } from "./extensions/msteams/src/feedback-reflection-prompt.ts";

Terminal output:

input.length = 521
emoji code point = 0x1f99e
BEFORE raw slice tail = "aaaa\ud83e..."
BEFORE raw slice tail code units = 0061 0061 0061 0061 d83e 002e 002e 002e
BEFORE lone-surrogate = true
BEFORE encodeURIComponent = URIError
AFTER prompt excerpt tail = "aaaaa..."
AFTER prompt excerpt tail code units = 0061 0061 0061 0061 0061 002e 002e 002e
AFTER lone-surrogate = false
AFTER contains boundary emoji = false

The fixed buildReflectionPrompt output is well-formed for this surrogate-boundary emoji input: the lone-surrogate regex reports false, and the boundary emoji is dropped rather than split.

Tests

Focused Vitest coverage exercises the prompt builder regression cases:

  • does not split UTF-16 surrogate pairs when truncating a thumbed-down response
  • keeps a boundary emoji when it fully fits before the truncation cap

Validation command:

node scripts/run-vitest.mjs extensions/msteams/src/feedback-reflection.test.ts

Result:

Test Files  1 passed (1)
Tests       20 passed (20)
[test] passed 1 Vitest shard in 78.96s

@openclaw-barnacle openclaw-barnacle Bot added channel: msteams Channel integration: msteams size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 24, 2026
@clawsweeper

clawsweeper Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 26, 2026, 12:15 AM ET / 04:15 UTC.

Summary
The PR replaces raw .slice(0, 500) truncation in the Microsoft Teams feedback reflection prompt with truncateUtf16Safe and adds surrogate-pair regression tests.

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

Reproducibility: yes. Current main and v2026.6.10 use raw UTF-16 slicing at the 500-code-unit cap, and the PR body supplies a boundary input that produces a lone surrogate before the fix and well-formed output after it; I did not run tests because this review is read-only.

Review metrics: none identified.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Rerun or resolve the failed live status checks before merge.

Risk before merge

  • [P1] Live PR status still includes failed Real behavior proof runs and checks-node-compact-small-whole-2, so merge should wait for rerun or maintainer resolution even though the patch review is clean.

Maintainer options:

  1. Decide the mitigation before merge
    Merge the narrow SDK-helper reuse after maintainer review and clean required checks; keep the sibling parent-context UTF-16 fix in fix(msteams): keep truncated parent context text well-formed #96569 as separate work.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P2] No automated code repair is indicated; maintainers should review the correct narrow patch and rerun or resolve the failed status checks before merge.

Security
Cleared: The diff only changes string truncation through an existing SDK helper and adds tests; it does not touch dependencies, workflows, secrets, package resolution, or code execution surfaces.

Review details

Best possible solution:

Merge the narrow SDK-helper reuse after maintainer review and clean required checks; keep the sibling parent-context UTF-16 fix in #96569 as separate work.

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

Yes. Current main and v2026.6.10 use raw UTF-16 slicing at the 500-code-unit cap, and the PR body supplies a boundary input that produces a lone surrogate before the fix and well-formed output after it; I did not run tests because this review is read-only.

Is this the best way to solve the issue?

Yes. Reusing the existing public truncateUtf16Safe helper is the narrowest maintainable fix for this prompt path; local surrogate logic or broader Teams refactors would add unnecessary drift.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a focused Microsoft Teams prompt correctness fix with limited blast radius and no outage, data-loss, or security evidence.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes terminal proof from a temporary worktree importing the real fixed function and showing the after-fix prompt has no lone surrogate.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes terminal proof from a temporary worktree importing the real fixed function and showing the after-fix prompt has no lone surrogate.
Evidence reviewed

PR surface:

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

View PR surface stats
Area Files Added Removed Net
Source 1 2 1 +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 27 1 +26

What I checked:

Likely related people:

  • SidU: The merged Teams AI UX PR added feedback reflection behavior and the original feedback reflection tests, including the prompt path this PR repairs. (role: introduced feature; confidence: high; commits: cd90130877f1, 15158d358acc; files: extensions/msteams/src/feedback-reflection.ts, extensions/msteams/src/feedback-reflection.test.ts)
  • jacobtomlinson: A later merged Teams feedback PR changed feedback invoke authorization around the same feedback/reflection workflow boundary. (role: recent adjacent contributor; confidence: medium; commits: c5415a474bb0, 1dede6d05fee; files: extensions/msteams/src/monitor-handler.ts, extensions/msteams/src/monitor-handler.feedback-authz.test.ts)
  • steipete: A recent plugin-state refactor touched Microsoft Teams feedback learning storage and feedback-reflection.test.ts, making them a useful routing candidate for adjacent feedback-state context. (role: recent adjacent contributor; confidence: medium; commits: 33c246dbbaaa, 26d5557c0e77; files: extensions/msteams/src/feedback-reflection-store.ts, extensions/msteams/src/feedback-reflection.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jun 24, 2026
@WeeLi-009
WeeLi-009 force-pushed the fix/msteams-reflection-truncate-surrogate branch from e8f027b to d4da0d3 Compare June 25, 2026 00:23
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 25, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 25, 2026
buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.
@WeeLi-009
WeeLi-009 force-pushed the fix/msteams-reflection-truncate-surrogate branch from d4da0d3 to 32f9331 Compare June 26, 2026 06:24
@vincentkoc
vincentkoc merged commit 6c5a9fd into openclaw:main Jun 27, 2026
87 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 28, 2026
…#96578)

buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.
QiuYuang pushed a commit to QiuYuang/openclaw that referenced this pull request Jul 1, 2026
…#96578)

buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
…#96578)

buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 2, 2026
…#96578)

buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.

(cherry picked from commit 6c5a9fd)
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 3, 2026
…#96578)

buildReflectionPrompt truncated the thumbed-down response with
String.slice(0, 500) on a UTF-16 code-unit index, so an astral
character straddling the 500-char cap was cut into a lone surrogate in
the reflection prompt built for the LLM.

Use truncateUtf16Safe so truncation never splits a surrogate pair,
keeping the existing 500-char budget and '...' suffix.

Adds tests asserting the prompt stays UTF-16 well formed when truncating
and that a boundary emoji is preserved when it fits.
hugenshen added a commit to hugenshen/openclaw that referenced this pull request Jul 7, 2026
…void lone surrogates

The Mattermost inbound handler in extensions/mattermost/src/mattermost/monitor.ts
truncates the message body preview with bodyText.slice(0, 200), which can split a
UTF-16 surrogate pair when an emoji (e.g. 🎉, 🦞) or other astral character lands at
the 200-char boundary. The resulting lone surrogate half leaks into the verbose log
preview and could propagate to downstream consumers that reject ill-formed strings.

Replace the raw .slice(0, 200) with the shared truncateUtf16Safe helper (from
openclaw/plugin-sdk/text-utility-runtime), which backs up one code unit when the cut
would land inside a surrogate pair. This matches the same pattern already applied to
the Slack, Discord, Telegram, IRC, Line, MS Teams, and Feishu channel previews
(openclaw#96456, openclaw#96569, openclaw#96572, openclaw#96577, openclaw#96578, openclaw#97462, openclaw#97472, openclaw#98994).

The preview is only used for the verbose inbound log line so the behavioral change
is cosmetic: when the 200th code unit would split an emoji, the preview now includes
one fewer code unit rather than emitting a broken surrogate.
hugenshen added a commit to hugenshen/openclaw that referenced this pull request Jul 7, 2026
…void lone surrogates

The Mattermost inbound handler in extensions/mattermost/src/mattermost/monitor.ts
truncates the message body preview with bodyText.slice(0, 200), which can split a
UTF-16 surrogate pair when an emoji (e.g. 🎉, 🦞) or other astral character lands at
the 200-char boundary. The resulting lone surrogate half leaks into the verbose log
preview and could propagate to downstream consumers that reject ill-formed strings.

Replace the raw .slice(0, 200) with the shared truncateUtf16Safe helper (from
openclaw/plugin-sdk/text-utility-runtime), which backs up one code unit when the cut
would land inside a surrogate pair. This matches the same pattern already applied to
the Slack, Discord, Telegram, IRC, Line, MS Teams, and Feishu channel previews
(openclaw#96456, openclaw#96569, openclaw#96572, openclaw#96577, openclaw#96578, openclaw#97462, openclaw#97472, openclaw#98994).

The preview is only used for the verbose inbound log line so the behavioral change
is cosmetic: when the 200th code unit would split an emoji, the preview now includes
one fewer code unit rather than emitting a broken surrogate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: msteams Channel integration: msteams P2 Normal backlog priority with limited blast radius. 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.

3 participants