Skip to content

fix(slack): keep thread label snippet truncation UTF-16 safe#101782

Closed
hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/slack-thread-label-utf16
Closed

fix(slack): keep thread label snippet truncation UTF-16 safe#101782
hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/slack-thread-label-utf16

Conversation

@hugenshen

@hugenshen hugenshen commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where Slack thread labels injected into the agent prompt context could contain invalid Unicode when thread starter messages contain emoji near the 80-character snippet boundary.

When a Slack thread reply arrives, OpenClaw builds a human-readable label for the thread (e.g. "Slack thread #general: Great idea about the 🚀 launch…") and injects it into the LLM's context. The snippet is produced by collapsing whitespace then calling .slice(0, 80). Emoji and supplementary-plane characters are represented as two UTF-16 code units (a surrogate pair). If the cut point lands between the two halves, the result contains an isolated surrogate — invalid Unicode — which is then passed to the model as part of its context string.

This affects two sibling helper functions:

  • prepareSlackThreadContext in prepare-thread-context.ts — builds threadLabel for live thread replies
  • resolveSlackAssistantRootThreadLabel / formatSlackBotStarterThreadLabel in prepare-thread-context-root.ts — builds the label for assistant-root thread sessions

Why This Change Was Made

Replace .slice(0, 80) with truncateUtf16Safe(..., 80) in both helpers. The surrounding .replace(/\s+/g, " ") and .trim() calls are unchanged; only the final truncation step is made surrogate-safe. truncateUtf16Safe from openclaw/plugin-sdk/text-utility-runtime is the established helper for this pattern throughout the codebase.

Both sites are fixed in the same PR because they share the identical bug pattern and are tested together.

User Impact

Slack users who start threads with emoji-rich messages long enough to reach the 80-character boundary (common in practice — a single sentence with a few emoji easily hits this) would cause the agent to receive a malformed thread label in its prompt context. This can produce garbled context, confuse the model about the thread subject, or trigger a rejected API call on providers that validate Unicode input. After this fix, thread labels are always valid Unicode strings.

Evidence

  • prepare-thread-context.ts:228 (post-fix): const snippet = truncateUtf16Safe(starter.text.replace(/\s+/g, " "), 80);snippet is used on the next line as threadLabel = \Slack thread ${params.roomLabel}: ${snippet}``.
  • prepare-thread-context-root.ts:112 (post-fix): const snippet = truncateUtf16Safe(params.starterText.replace(/\s+/g, " "), 80).trim();snippet is returned as part of the label string \${base} (assistant root): ${snippet}``.
  • Both labels are consumed by downstream prompt-context builders and reach the LLM.
  • Fix: truncateUtf16Safe(..., 80) — same length cap, surrogate-pair-safe.
  • Diff: two production files + surrogate-boundary regression in prepare-thread-context-root.test.ts.

Behavior proof

1) Node runtime through production formatSlackBotStarterThreadLabel (no Vitest mocks)

Imports and calls the real exported helper from prepare-thread-context-root.ts on a surrogate-boundary starter (79 × 'a' + 🐱tail):

=== Slack thread label UTF-16 reproduction (Node runtime, production helpers, no mocks) ===
Node: v22.22.0
starterText code units: 85
old .slice(0, 80).trim() code units: 80
old snippet has unpaired surrogate: true
truncateUtf16Safe(..., 80).trim() code units: 79
fixed snippet has unpaired surrogate: false

[formatSlackBotStarterThreadLabel — production export]
label: Slack thread DM (assistant root): aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
label has unpaired surrogate: false
label equals expected 79-a form: true

[prepare-thread-context.ts label shape]
threadLabel: Slack thread #general: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
threadLabel has unpaired surrogate: false

This shows both fixed call-site shapes — assistant-root labels and live thread labels — emit surrogate-safe snippets at the 80 code-unit cap.

2) Call-site regression test

$ pnpm test extensions/slack/src/monitor/message-handler/prepare-thread-context-root.test.ts -t "surrogate" --reporter=verbose

 ✓ formatSlackBotStarterThreadLabel > drops a surrogate-pair emoji whole when it straddles the 80-char snippet limit

 Test Files  1 passed (1)
      Tests  1 passed | 23 skipped (24)

Regression asserts formatSlackBotStarterThreadLabel({ roomLabel: "DM", starterText: "a".repeat(79) + "🐱tail" }) drops the boundary emoji whole and leaves no unpaired surrogate in the final label.

Proof gap: No redacted live Slack workspace transcript in this validation environment. The Node runtime call and regression cover the production label builders; optional supplemental proof is a redacted thread-reply log showing a long emoji-near-boundary starter produces a well-formed threadLabel.

  • AI-assisted (Cursor / Claude Sonnet 4.6)
  • I understand what the code does
  • Change is focused and does not mix unrelated concerns

@openclaw-barnacle openclaw-barnacle Bot added channel: slack Channel integration: slack size: XS labels Jul 7, 2026
@clawsweeper

clawsweeper Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 8, 2026, 1:45 AM ET / 05:45 UTC.

Summary
Replaces two Slack thread-label raw .slice(0, 80) truncations with truncateUtf16Safe(..., 80) and adds a surrogate-boundary regression test for assistant-root labels.

PR surface: Source +3, Tests +7. Total +10 across 3 files.

Reproducibility: yes. source-level: current main has two Slack thread-label paths that truncate whitespace-collapsed starter text with raw .slice(0, 80), so a starter like 79 ASCII characters plus an emoji can leave a dangling high surrogate. I did not run a live Slack workspace in this read-only review.

Review metrics: none identified.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🦞 diamond lobster
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:

  • none.

Risk before merge

  • [P1] The PR branch is behind current main, so exact-head merge checks should be refreshed before merge even though the focused diff has no supported correctness finding.

Maintainer options:

  1. Decide the mitigation before merge
    Land the focused Slack helper-based fix after ordinary maintainer rebase and exact-head merge checks.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • No ClawSweeper repair job is needed; the remaining action is ordinary maintainer merge or rebase handling.

Security
Cleared: Cleared: the diff imports an existing SDK text helper and changes only Slack string truncation plus a focused test, with no dependency, secret, permission, workflow, or execution-surface change.

Review details

Best possible solution:

Land the focused Slack helper-based fix after ordinary maintainer rebase and exact-head merge checks.

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

Yes, source-level: current main has two Slack thread-label paths that truncate whitespace-collapsed starter text with raw .slice(0, 80), so a starter like 79 ASCII characters plus an emoji can leave a dangling high surrogate. I did not run a live Slack workspace in this read-only review.

Is this the best way to solve the issue?

Yes. Reusing the existing truncateUtf16Safe SDK helper at both Slack label truncation sites is the narrowest maintainable fix; Slack's truncateSlackText would change these prompt labels by trimming and adding an ellipsis.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a normal Slack prompt-context correctness fix with limited channel-specific blast radius.
  • 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): Sufficient: the PR body includes copied Node runtime output exercising the production label helper and fixed live-label shape, with regression test output as supplemental proof.
  • proof: sufficient: Contributor real behavior proof is sufficient. Sufficient: the PR body includes copied Node runtime output exercising the production label helper and fixed live-label shape, with regression test output as supplemental proof.
Evidence reviewed

PR surface:

Source +3, Tests +7. Total +10 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 5 2 +3
Tests 1 7 0 +7
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 12 2 +10

What I checked:

Likely related people:

  • vincentkoc: Current blame for both affected snippet lines points to the Slack helper extraction commit from PR refactor(deadcode): trim private helper exports #101886, authored and merged by this account. (role: recent area contributor; confidence: high; commits: 1b6f3e43d1e5; files: extensions/slack/src/monitor/message-handler/prepare-thread-context.ts, extensions/slack/src/monitor/message-handler/prepare-thread-context-root.ts)
  • sxxtony: The assistant-root Slack thread helper and tests were added in the same central path by PR fix(slack): include bot root message in new thread sessions (#79338) #80409. (role: introduced adjacent assistant-root behavior; confidence: high; commits: b2dab308ae68; files: extensions/slack/src/monitor/message-handler/prepare-thread-context-root.ts, extensions/slack/src/monitor/message-handler/prepare-thread-context-root.test.ts, extensions/slack/src/monitor/message-handler/prepare-thread-context.ts)
  • bek91: GitHub PR metadata shows this account merged the assistant-root Slack thread-context fix that introduced the helper surface. (role: merger and adjacent contributor; confidence: medium; commits: b2dab308ae68; files: extensions/slack/src/monitor/message-handler/prepare-thread-context.ts, extensions/slack/src/monitor/message-handler/prepare-thread-context-root.ts)
  • scoootscooob: The Slack channel move into extensions/slack/src carried the earlier thread-context implementation into the current plugin boundary. (role: historical refactor owner; confidence: medium; commits: 8746362f5ebf; files: extensions/slack/src/monitor/message-handler/prepare-thread-context.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.
Review history (5 earlier review cycles)
  • reviewed 2026-07-07T16:20:06.637Z sha ec0c968 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T02:15:33.230Z sha 7c3661d :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T02:22:02.284Z sha 7c3661d :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T02:33:53.230Z sha 7c3661d :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T03:21:18.467Z sha 7c3661d :: needs maintainer review before merge. :: none

@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 Jul 7, 2026
@hugenshen
hugenshen force-pushed the fix/slack-thread-label-utf16 branch from ec0c968 to 718bbfa Compare July 8, 2026 02:09
Replace raw .slice(0, 80) thread label snippets with truncateUtf16Safe so
emoji straddling the cap are dropped whole instead of leaving unpaired
surrogates in Slack thread labels.

Co-authored-by: Cursor <[email protected]>
@hugenshen
hugenshen force-pushed the fix/slack-thread-label-utf16 branch from 718bbfa to 7c3661d Compare July 8, 2026 02:11
@hugenshen

Copy link
Copy Markdown
Contributor Author

Squashed to a single commit and rebased on latest main. Behavior proof is covered by colocated regression tests (no scripts/proof-*.mjs).

Test run

pnpm test extensions/slack/src/monitor/message-handler/prepare-thread-context-root.test.ts — 24 passed

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@hugenshen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@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. 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. labels Jul 8, 2026
vincentkoc added a commit that referenced this pull request Jul 8, 2026
Consolidates #102007, #101818, and #101782.

Co-authored-by: 杨浩宇0668001029 <[email protected]>
Co-authored-by: NIO <[email protected]>
Co-authored-by: Cursor <[email protected]>
@vincentkoc vincentkoc self-assigned this Jul 8, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Landed the canonical fix on main as 7e03242.

The valid Slack thread-label truncation fix is included for both assistant-root and ordinary user/third-party-bot paths, and @hugenshen's authorship is preserved with a co-author trailer. I direct-landed the consolidation because this branch conflicted with current main, and #102007, #101818, and #101782 all fix the same UTF-16 boundary class. The landed shape shares one local Slack formatter instead of duplicating normalization logic.

Proof on the landed patch:

Closing this PR as included in the landed main commit, not rejected.

@vincentkoc vincentkoc closed this Jul 8, 2026
giodl73-repo pushed a commit to giodl73-repo/openclaw that referenced this pull request Jul 8, 2026
Consolidates openclaw#102007, openclaw#101818, and openclaw#101782.

Co-authored-by: 杨浩宇0668001029 <[email protected]>
Co-authored-by: NIO <[email protected]>
Co-authored-by: Cursor <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 9, 2026
Consolidates openclaw#102007, openclaw#101818, and openclaw#101782.

Co-authored-by: 杨浩宇0668001029 <[email protected]>
Co-authored-by: NIO <[email protected]>
Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: slack Channel integration: slack 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.

2 participants