Skip to content

fix(imessage): keep fenced role-marker mapping keys out of the outbound stripper - #117159

Merged
vincentkoc merged 2 commits into
openclaw:mainfrom
harjothkhara:oss-find/openclaw-2026-07-31-d
Aug 1, 2026
Merged

vincentkoc merged 2 commits into
openclaw:mainfrom
harjothkhara:oss-find/openclaw-2026-07-31-d

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Every outbound iMessage reply runs through sanitizeOutboundText, which strips three
internal-marker patterns. One of them, ROLE_TURN_MARKER_RE (extensions/imessage/src/monitor/sanitize-outbound.ts:12),
deletes any line that is only user:, system: or assistant: — a leaked turn boundary.
The regex is anchored per line and allows leading whitespace, and it runs with no code-fence
awareness. So a bare YAML mapping key on its own line inside a fenced block matches and is
silently deleted before delivery.

The recipient gets YAML that still parses but now nests the child keys under the wrong parent:

- name: ensure account
  user:            # <- this line is deleted
    name: alice    # now a sibling of `- name:`, no longer under `user:`
    state: present

Trigger is ordinary technical chat, not an edge case: Ansible user: modules, docker-compose
top-level user:, cloud-init, netplan, and LLM prompt templates with system: / assistant:
lines all hit it. The corruption is silent — nothing signals a line was removed. The two sibling
patterns on the same path (INTERNAL_SEPARATOR_RE, ASSISTANT_ROLE_MARKER_RE) share the blind spot.

Closes #116942.

Why This Change Was Made

The role-marker strippers exist to remove leaked turn scaffolding from prose. Inside a code
region a bare user: mapping key or a #+# line is the user's own content, not scaffolding,
so the strip must skip it. The plugin already owns the exact tool: findCodeRegions /
isInsideCode are exported from openclaw/plugin-sdk/text-chunking and the sibling
extensions/imessage/src/monitor/reflection-guard.ts on this same channel already uses them to
ignore matches inside code. Core's assistant-visible-text.ts uses the identical
recompute-between-passes shape. So this is the established, narrowest boundary — it preserves the
existing "strip leaked markers in prose" contract while restoring fenced content, rather than
weakening or deleting the strip.

Root cause is fully contained in sanitizeOutboundText; everything downstream operates on the
already-corrupted text. Fix = one file, one small helper applied to all three patterns; regions
are recomputed per pass because each strip shifts later offsets. Blast radius: sanitizeOutboundText
has three callers, all in the iMessage extension (deliver.ts:39, deliver.ts:89, channel.ts:412).
Grep confirms no other channel carries a role-turn stripper, so this is iMessage-only.

User Impact

Fenced code blocks in iMessage replies are delivered intact. A user: / system: / assistant:
mapping key inside a fence survives, and its children stay correctly nested. Leaked standalone
role markers in prose are still stripped, unchanged.

Evidence

AI-assisted.

Focused regression proof (also the ClawSweeper-listed acceptance command):

node scripts/run-vitest.mjs extensions/imessage/src/monitor.behavior.test.ts

Three new cases in sanitize-outbound.test-support.ts fail on the unmodified sanitizer and pass
after the fix; full suite 178/178 (185/185 including deliver.test.ts).

Real-behavior proof — a harness drives the real production delivery transform chain exactly as
deliver.ts:39-46 composes it (sanitizeOutboundText → real convertMarkdownTables → real
chunkTextWithMode) and captures the exact payload handed to the send boundary. Negative control
on unfixed main vs this branch, same command:

# BEFORE (unfixed main) — negative control, FAIL
CASE ansible-user-module
  DELIVERED "```yaml\n- name: ensure account\n\n    name: alice\n    state: present\n```"   # user: deleted
CASE compose-top-level-user
  DELIVERED "...\n    image: nginx\n\n  name: alice\n  uid: 1000\n```"                       # user: deleted
CASE system-and-assistant-keys
  DELIVERED "```yaml\n\n  role: primary\n\n  role: helper\n```"                              # both deleted
RESULT: FAIL — BUG SIGNATURE

# AFTER (this branch) — PASS
CASE ansible-user-module
  DELIVERED "```yaml\n- name: ensure account\n  user:\n    name: alice\n    state: present\n```"
CASE compose-top-level-user
  DELIVERED "Your compose file needs a user block:\n\n```yaml\nservices:\n  app:\n    image: nginx\nuser:\n  name: alice\n  uid: 1000\n```"
CASE system-and-assistant-keys
  DELIVERED "```yaml\nsystem:\n  role: primary\nassistant:\n  role: helper\n```"
  [PASS] leaked standalone 'assistant:' marker in prose is still stripped
RESULT: PASS

git diff --check clean; oxfmt clean. Production delta +17 LOC (one helper).

What was not tested: the send RPC itself needs a signed-in Mac + imsg bridge, so the harness
stops at the send boundary and captures the pre-send payload — the same limitation the reporter
noted. The corruption is introduced entirely by sanitizeOutboundText, upstream of that boundary,
so the captured payload proves it. Typecheck was left to CI (the linked worktree can't run the
tsgo lane locally without a dependency reconcile).

Maintainer validation

  • Decision: accept literal marker-shaped text inside Markdown code regions. The repaired sanitizer still strips the same #+#, assistant-role, and user: / system: / assistant: markers in prose.
  • Canonical owner: extensions/imessage/src/monitor/sanitize-outbound.ts, where outbound text is corrupted before formatting, chunking, or send. The branch uses the existing public code-region helpers already used by the sibling iMessage reflection guard.
  • Coverage added: exact-output table cases for user:, system:, and assistant: inside and outside code; backtick fences, tilde fences, indented code, and an unterminated fence with earlier removals that shift offsets.
  • Provenance: PR fix(imessage): prevent echo loop from leaking internal metadata and amplifying NO_REPLY into queue overflow #33295 / adb9234d0375df93d1ec96ce4008107788cc46c2 introduced the sanitizer (@joelnishanth, merged by @vincentkoc, March 7, 2026). PR fix(imessage): only strip standalone role-turn markers, not prose ending in a role word #96392 / 770b19f496c1637774e660d31306105b671d1526 (@ly-wang19, merged by @vincentkoc) narrowed role stripping to standalone lines but did not protect code regions.
  • Release exposure: v2026.7.1 and every v2026.7.2-beta.1 through v2026.7.2-beta.5 contain sanitizer blob cd230ee1baef18b3e6ef44669c77bb02ce4d7ff9.
  • Exact repaired head: 0bfdf3cbe9034358fa7af67e1019f923a147251a, rebuilt on origin/main 6922ddd936106c95c2f1388812bc49e8f7920dd0; the empty CI-retrigger commit was removed and contributor authorship was preserved.
  • Focused proof: node scripts/run-vitest.mjs extensions/imessage/src/monitor.behavior.test.ts - 179/179 passed.
  • Changed gate: Blacksmith Testbox passed all selected ratchets, formatting, extension typechecks, lint, dead-code, plugin-boundary, database-first, sidecar-loader, and import-cycle checks: https://github.com/openclaw/openclaw/actions/runs/30686352634
  • Autoreview: fresh branch review clean, no accepted/actionable findings, correctness 0.99.

@openclaw-barnacle openclaw-barnacle Bot added channel: imessage Channel integration: imessage size: XS labels Aug 1, 2026
@harjothkhara
harjothkhara marked this pull request as ready for review August 1, 2026 02:02
@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. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. 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, 4:47 AM ET / 08:47 UTC.

ClawSweeper review

What this changes

This PR makes iMessage outbound sanitization preserve literal user:, system:, assistant:, separator, and assistant-role text inside Markdown code regions while retaining existing stripping in prose.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

The focused iMessage repair remains necessary because current main still strips marker-shaped YAML keys without Markdown code-region awareness. The exact rebased patch uses the established Plugin SDK code-region contract, preserves prose stripping, has no actionable correctness or security finding, and is ready for normal maintainer merge once the active exact-head check finishes.

Priority: P1
Reviewed head: b746c8bfcbc8940509c071c6fac61ccd23ad2c01

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused, owner-approved repair with direct delivery-path proof, regression coverage, and no identified correctness or security defect.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The PR supplies before/after captured payloads from the real iMessage delivery transform up to the native send boundary, and the assigned owner reports exact-head focused proof across the monitor, delivery, and plugin-payload suites.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR supplies before/after captured payloads from the real iMessage delivery transform up to the native send boundary, and the assigned owner reports exact-head focused proof across the monitor, delivery, and plugin-payload suites.
Evidence reviewed 6 items Current-main defect remains: Current main applies all three marker regexes with direct replacement and no code-region check, so a standalone fenced YAML key such as user: is still removed before delivery.
Patch repairs the owning transform: The PR computes Markdown code regions for each stripping pass and preserves only matches inside those regions; it keeps the existing prose-marker patterns and post-strip normalization intact.
Shared Markdown contract supports the boundary: The reused public Plugin SDK helper delegates to Markdown Core spans for fenced, indented, and inline code, and treats a match offset within a span as code-owned.
Findings None None.
Security None None.

How this fits together

iMessage replies pass through an outbound sanitizer before formatting, chunking, and the native send boundary. The sanitizer removes leaked model scaffolding, so it must distinguish internal prose markers from literal code supplied to recipients.

flowchart LR
  A[Agent reply text] --> B[iMessage delivery transform]
  B --> C[Outbound sanitizer]
  C --> D{Match inside Markdown code?}
  D -->|No| E[Remove leaked internal marker]
  D -->|Yes| F[Preserve literal code content]
  E --> G[Format and chunk reply]
  F --> G
  G --> H[Native iMessage send boundary]
Loading

Before merge

  • Complete next step (P2) - No repair lane is needed: the exact-head patch has no actionable review finding; it awaits ordinary exact-head check completion and maintainer merge.
Agent review details

Security

None.

PR surface

Source +50. Total +50 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 2 54 4 +50
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 54 4 +50

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #116942
Summary: This PR is the focused candidate fix for the open iMessage fenced-code corruption report; the earlier role-marker PR addressed a distinct prose false positive.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Merge the focused owner-boundary fix after the active exact-head check completes, keeping Markdown code content literal while retaining the established prose-only leak stripping.

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

Yes — current main directly replaces the standalone role-marker match before delivery, and the PR's colocated cases provide a focused fenced-YAML reproduction shape. This review did not execute the test suite because the checkout is read-only.

Is this the best way to solve the issue?

Yes — the patch fixes the corruption at sanitizeOutboundText, the producer of the bad payload, using the existing public Markdown ownership helper rather than weakening prose sanitization or compensating downstream.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: Current main silently corrupts ordinary fenced technical content on a live channel-delivery path.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster 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 supplies before/after captured payloads from the real iMessage delivery transform up to the native send boundary, and the assigned owner reports exact-head focused proof across the monitor, delivery, and plugin-payload suites.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR supplies before/after captured payloads from the real iMessage delivery transform up to the native send boundary, and the assigned owner reports exact-head focused proof across the monitor, delivery, and plugin-payload suites.

Evidence

What I checked:

Likely related people:

  • vincentkoc: The maintainer re-review explicitly accepts the code-region exception, authored the current-head regression coverage, and recently maintained the adjacent delivery boundary. (role: assigned iMessage owner and recent area contributor; confidence: high; commits: b746c8bfcbc8, 338a7ccb2f70; files: extensions/imessage/src/monitor/sanitize-outbound.test-support.ts, extensions/imessage/src/monitor/deliver.ts)
  • joelnishanth: The linked merged iMessage sanitizer introduction is the provenance for the outbound marker-removal behavior this patch narrows in code regions. (role: original sanitizer contributor; confidence: high; commits: adb9234d0375; files: extensions/imessage/src/monitor/sanitize-outbound.ts)
  • ly-wang19: The linked merged follow-up established the current standalone role-marker regex whose remaining fenced-code false positive this PR repairs. (role: prior marker-policy contributor; confidence: high; commits: 770b19f496c1; files: extensions/imessage/src/monitor/sanitize-outbound.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.

History

Review history (6 earlier review cycles)
  • reviewed 2026-08-01T02:07:25.353Z sha 7e887a4ca803e0c007777d24ec8174ccf85a9f8a :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T02:20:32.067Z sha 44d90761aba7a8bbf78b7a0d32a6d2930bd627f4 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T03:00:29.856Z sha 9e91a2e9e79980a45ac41d9bed3baa299e31ead9 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T05:42:40.385Z sha 3d4550174e3182c481accd1c41c8a30bddd2221a :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T05:54:29.197Z sha 0bfdf3cbe9034358fa7af67e1019f923a147251a :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T08:34:47.285Z sha b746c8b :: needs maintainer review before merge. :: none

@harjothkhara
harjothkhara force-pushed the oss-find/openclaw-2026-07-31-d branch from 7e887a4 to 44d9076 Compare August 1, 2026 02:15
@harjothkhara

Copy link
Copy Markdown
Contributor Author

The red check-lint here isn't from this diff.

The failing shard is oxlint:core, which lints src / ui / packages (scripts/run-oxlint-shards.mjs:39). This PR only touches extensions/imessage/src/monitor/ (2 files), which lint in a separate extensions shard, and oxlint on them is clean. The core shard isn't reporting a lint diagnostic — it's killed at ~100s by an external SIGTERM (exit 143 / "The operation was canceled"), well under its 15-min shard timeout and the 20-min job timeout. So the job is being cancelled, not failing lint — and the cancel always claims oxlint:core because it's the slowest shard still running when it lands. Reproduced on all three heads, while other current PRs pass the same shard.

Could a maintainer re-run check-lint (or @clawsweeper fix ci)? The change is otherwise proof: sufficient and mergeable. Thanks!

@vincentkoc
vincentkoc force-pushed the oss-find/openclaw-2026-07-31-d branch from 9e91a2e to 3d45501 Compare August 1, 2026 05:36
@vincentkoc vincentkoc self-assigned this Aug 1, 2026
@vincentkoc
vincentkoc force-pushed the oss-find/openclaw-2026-07-31-d branch from 3d45501 to 0bfdf3c Compare August 1, 2026 05:49
@vincentkoc

Copy link
Copy Markdown
Member

@clawsweeper re-review: Assigned iMessage owner accepts literal marker-shaped text inside Markdown code regions while preserving prose stripping. Correct provenance: PR #33295 / adb9234 introduced the sanitizer (@joelnishanth; merged by @vincentkoc); PR #96392 / 770b19f only narrowed it to standalone role lines and did not add code-region protection. Release exposure: v2026.7.1 and v2026.7.2-beta.1 through v2026.7.2-beta.5 all contain sanitizer blob cd230ee1baef18b3e6ef44669c77bb02ce4d7ff9. Recheck repaired exact head 0bfdf3cbe9034358fa7af67e1019f923a147251a and replace any stale provenance in the durable review.

@clawsweeper

clawsweeper Bot commented Aug 1, 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: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

harjothkhara and others added 2 commits August 1, 2026 16:29
sanitizeOutboundText strips standalone `user:`/`system:`/`assistant:`
lines (leaked turn boundaries), `#+#` separators and `assistant to=`
markers, but the three regexes had no code-fence awareness. A bare YAML
mapping key like `user:` on its own line inside a ```yaml block matched
ROLE_TURN_MARKER_RE and was silently deleted before iMessage delivery,
reparenting its children under the wrong node — still-valid YAML, wrong
meaning, and nothing signalling a line was removed.

Skip matches whose offset falls inside a markdown code region for all
three patterns, reusing findCodeRegions/isInsideCode already applied by
the sibling reflection-guard on this channel. Regions are recomputed per
pass because each strip shifts later offsets. Leaked markers in prose are
still stripped.

Refs: openclaw#116942

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@vincentkoc
vincentkoc force-pushed the oss-find/openclaw-2026-07-31-d branch from 0bfdf3c to b746c8b Compare August 1, 2026 08:30
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Aug 1, 2026
@vincentkoc

Copy link
Copy Markdown
Member

@clawsweeper re-review: rebased the unchanged focused repair onto live main b0b708d after #117282 rewrote the downstream iMessage delivery boundary. Review exact head b746c8b. Sanitized AWS proof on that exact head passed 193/193 across monitor.behavior, deliver integration, and plugin-payload tests; targeted oxfmt and oxlint passed with zero findings; fresh autoreview is clean. The exact-head check-lint failure has no diagnostic and is another externally canceled core shard (exit 143); one failed-only rerun is active.

@clawsweeper

clawsweeper Bot commented Aug 1, 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: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. label Aug 1, 2026

@vincentkoc vincentkoc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer review complete on exact head b746c8b against live main ea3b5df. The sanitizer-owner fix is the best repair: it reuses the canonical Markdown code-region contract, preserves literal fenced/indented content, and keeps prose leak stripping intact. Sanitized AWS proof passed 193/193 focused iMessage tests; targeted formatting and oxlint passed; fresh autoreview and exact-head ClawSweeper found no actionable defects; required CI is green; zero unresolved threads; no iMessage drift since the rebase; merge tree is clean. Approved.

@vincentkoc
vincentkoc merged commit 41051fe into openclaw:main Aug 1, 2026
143 of 147 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 2, 2026
…nd stripper (openclaw#117159)

* fix(imessage): stop the outbound sanitizer deleting fenced role keys

sanitizeOutboundText strips standalone `user:`/`system:`/`assistant:`
lines (leaked turn boundaries), `#+#` separators and `assistant to=`
markers, but the three regexes had no code-fence awareness. A bare YAML
mapping key like `user:` on its own line inside a ```yaml block matched
ROLE_TURN_MARKER_RE and was silently deleted before iMessage delivery,
reparenting its children under the wrong node — still-valid YAML, wrong
meaning, and nothing signalling a line was removed.

Skip matches whose offset falls inside a markdown code region for all
three patterns, reusing findCodeRegions/isInsideCode already applied by
the sibling reflection-guard on this channel. Regions are recomputed per
pass because each strip shifts later offsets. Leaked markers in prose are
still stripped.

Refs: openclaw#116942

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* test(imessage): cover code-aware marker stripping

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Vincent Koc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: imessage Channel integration: imessage P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S 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.

iMessage outbound sanitizer deletes bare YAML mapping keys inside fenced code blocks

2 participants