Skip to content

fix: prevent reasoning tags from leaking or hiding visible text - #113205

Merged
steipete merged 1 commit into
openclaw:mainfrom
licheer-zte:fix/self-closing-reasoning-tags
Jul 27, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
licheer-zte:fix/self-closing-reasoning-tags

Conversation

@licheer-zte

@licheer-zte licheer-zte commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #113144.
Closes #113148.

What Problem This Solves

Fixes an issue where assistant output could lose visible text after self-closing reasoning markers, or expose private reasoning tags when malformed backtick runs and Markdown block transitions were streamed incrementally.

Why This Change Was Made

Static sanitization and packages/ai streaming now share one block-aware markdown-core contract. CommonMark/GFM parser positions own code-span classification; one iterative, quote-aware tag reducer owns self-closing tags, malformed recovery, orphan closes, and strict/visible modes. Streaming releases only parser-stable prefixes and retains real Markdown context across every chunk boundary.

The new surface is internal-only: markdown-core package exports and plugin-visible APIs are unchanged.

This is +530 net non-test lines because it replaces 497 lines of divergent sanitizer regexes, code-region approximation, and AI streaming state with the shared parser/reducer plus bounded incremental state. The old paths were deleted rather than retained as fallbacks.

User Impact

  • Before<thinking/>After now yields BeforeAfter.
  • Unequal backtick runs no longer preserve private reasoning tags as literal code.
  • Quoted delimiters, nested tags, Unicode whitespace, CR/LF/CRLF boundaries, GFM tables, lists, blockquotes, indented code, and fences use the same ownership rules in static and streamed output.
  • Malformed 110 KB tags recover iteratively without recursion or prefix-reparse blowups.
  • Stable visible output continues streaming without leaking across unresolved Markdown boundaries.
  • Native tool-call flush boundaries and DeepSeek DSML filtering preserve their prior streaming contracts.

Evidence

Exact head: e5829559a9f772a55888d898bf6c94c09893c9d1.

  • Current origin/main defect probe at d3e043751aed3fa8bedecca23f573510b9113278: self-closing static output was Before; unequal-backtick static output preserved <think>private</think>.
  • Direct repository-path proof: the markdown-core, AI provider/partitioner, static sanitizer, assistant-visible-text, and agent transport suites passed 412/412 tests on Blacksmith Testbox.
  • Blacksmith Testbox tbx_01kyhg375ht3sprvnhta0ze0ck: focused tests passed in 55s; pnpm check:changed passed in 9m07s; pnpm build passed in 2m13s.
  • Hosted Testbox run: https://github.com/openclaw/openclaw/actions/runs/30256067542
  • Final rebase range-diff changed only current-main test context; the rebased CLI process suite passed 52/52.
  • Independent adversarial refuter: NO-ACTIONABLE-OBJECTION.
  • Codex branch autoreview: clean, no accepted/actionable findings.

Direct runtime transcript

Exact-head execution through src/shared/text/reasoning-tags.ts and the packages/ai partitioner re-export produced:

{
  "static": {
    "selfClosing": {
      "input": "Before<thinking/>After",
      "output": "BeforeAfter"
    },
    "unequalBackticks": {
      "input": "before ```<think>private</think>`` after",
      "output": "before ````` after"
    }
  },
  "stream": {
    "input": "Paragraph `literal\n- <think>private</think> `visible",
    "chunks": [
      "Paragraph `literal\n",
      "- <thi",
      "nk>private</th",
      "ink> `visible"
    ],
    "text": "Paragraph `literal\n-  `visible",
    "thinking": "private",
    "deltas": [
      { "kind": "text", "text": "Paragraph `literal\n- " },
      { "kind": "thinking", "text": "private" },
      { "kind": "text", "text": " `visible" }
    ],
    "everySplitPointChecked": 53,
    "allSplitPointsMatch": true
  }
}

@steipete steipete self-assigned this Jul 25, 2026
@steipete
steipete force-pushed the fix/self-closing-reasoning-tags branch from 6243e20 to 77d3bff Compare July 25, 2026 11:37
@steipete steipete changed the title fix: handle self-closing thinking tags without dropping following text (#113144) fix(text): harden reasoning tag sanitization (#113144, #113148) Jul 25, 2026
@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. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Jul 25, 2026
@clawsweeper

clawsweeper Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 27, 2026, 7:37 AM ET / 11:37 UTC.

ClawSweeper review

What this changes

This PR moves reasoning-tag recognition and Markdown code ownership into Markdown Core, then uses that shared logic to prevent static and streamed assistant output from leaking reasoning markup or dropping visible text.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep this PR open for normal maintainer review. It is an active, mergeable candidate fix for two open deterministic assistant-output bugs, has direct after-fix runtime evidence, and the supplied context does not establish a merged replacement on current main.

Priority: P2
Reviewed head: e5829559a9f772a55888d898bf6c94c09893c9d1

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch has a coherent shared-owner design, direct runtime proof, and broad focused validation; a current-main refresh is the remaining practical risk reducer.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR body provides exact-head terminal-style runtime output for both static and chunked streaming behavior, alongside focused Testbox, changed-check, and build results; redact any private data if this evidence is reposted. (github.com)
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides exact-head terminal-style runtime output for both static and chunked streaming behavior, alongside focused Testbox, changed-check, and build results; redact any private data if this evidence is reposted. (github.com)
Evidence reviewed 5 items Shared parser boundary: The proposed head adds Markdown Core reasoning-tag parsing and incremental reduction, replacing the separate static and streaming recognizers with one ownership contract.
Both delivery paths use the shared contract: The branch replaces the AI reasoning-tag partitioner implementation with the Markdown Core re-export and delegates shared-text reasoning sanitization to Markdown Core, covering streamed and complete assistant output.
After-fix behavior proof: The PR body includes an exact-head runtime transcript showing both Before&lt;thinking/>After becoming BeforeAfter and a chunked streaming case emitting visible and thinking deltas correctly; it also reports focused Testbox and build/check evidence for the same head. (github.com)
Findings None None.
Security None None.

How this fits together

Model responses reach users through a static visible-text sanitizer or a streaming partitioner that separates visible output from hidden reasoning. The PR gives both paths one Markdown-aware parser and reducer so they classify the same model text consistently before channel delivery.

flowchart LR
  A[Model response or stream chunks] --> B[Markdown Core ownership parser]
  B --> C[Reasoning tag reducer]
  C --> D[Static visible-text sanitization]
  C --> E[Streaming text partitioning]
  D --> F[Assistant-visible delivery]
  E --> F
Loading

Before merge

  • Resolve merge risk (P1) - This is a common visibility boundary: a parser regression could suppress legitimate assistant text or expose reasoning markup in static or streamed delivery.
  • Resolve merge risk (P1) - The PR is behind current main, so maintainers should refresh the exact-head focused behavior proof after rebasing or merging the current base before landing.
  • Complete next step (P2) - No concrete repair finding remains for automation; this active, maintainer-engaged PR needs ordinary merge review and a current-main refresh rather than a replacement fix PR.
Agent review details

Security

None.

PR surface

Source +536, Tests +571, Config +3, Other +9. Total +1119 across 13 files.

View PR surface stats
Area Files Added Removed Net
Source 5 1033 497 +536
Tests 6 781 210 +571
Docs 0 0 0 0
Config 1 3 0 +3
Generated 0 0 0 0
Other 1 9 0 +9
Total 13 1826 707 +1119

Review metrics

Metric Value Why it matters
Parser dependencies 3 direct dependencies added Markdown Core now directly owns the CommonMark/GFM parsing stack used on the assistant-output visibility path, so the authorized dependency delta remains a release-relevant review surface.

Merge-risk options

Maintainer options:

  1. Refresh proof on the current base (recommended)
    Rebase or merge current main, then rerun the focused static and streamed behavior cases so the reviewed head remains proven after the merge refresh.
  2. Accept the shared parser boundary
    Merge after confirming that the new Markdown Core dependency ownership and visible-versus-hidden output contract are the intended long-term boundary.

Technical review

Best possible solution:

Land one shared Markdown-aware reasoning-tag contract only after a current-main refresh confirms that static sanitization and streamed partitioning retain the same visible-versus-hidden behavior.

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

Yes, from the supplied deterministic inputs and branch evidence: self-closing reasoning tags previously discarded a visible suffix, and unequal backtick runs could preserve reasoning markup. I could not independently execute the current-main path in this read-only review environment.

Is this the best way to solve the issue?

Yes. A single Markdown-aware parser and reducer for both complete and streamed output is more maintainable than preserving divergent visibility grammars, provided the rebased head keeps the demonstrated behavior.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: The PR repairs deterministic assistant-output loss and reasoning-markup exposure with limited but user-visible blast radius.
  • merge-risk: 🚨 message-delivery: The shared reducer decides whether output is emitted as visible assistant text or withheld as reasoning in both complete and streamed delivery.
  • merge-risk: 🚨 security-boundary: Incorrect classification can expose model reasoning markup that the product intends to suppress from assistant-visible output.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides exact-head terminal-style runtime output for both static and chunked streaming behavior, alongside focused Testbox, changed-check, and build results; redact any private data if this evidence is reposted. (github.com)
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides exact-head terminal-style runtime output for both static and chunked streaming behavior, alongside focused Testbox, changed-check, and build results; redact any private data if this evidence is reposted. (github.com)

Evidence

What I checked:

  • Shared parser boundary: The proposed head adds Markdown Core reasoning-tag parsing and incremental reduction, replacing the separate static and streaming recognizers with one ownership contract. (packages/markdown-core/src/reasoning-tags.ts, e5829559a9f7)
  • Both delivery paths use the shared contract: The branch replaces the AI reasoning-tag partitioner implementation with the Markdown Core re-export and delegates shared-text reasoning sanitization to Markdown Core, covering streamed and complete assistant output. (packages/ai/src/utils/reasoning-tag-text-partitioner.ts, e5829559a9f7)
  • After-fix behavior proof: The PR body includes an exact-head runtime transcript showing both Before<thinking/>After becoming BeforeAfter and a chunked streaming case emitting visible and thinking deltas correctly; it also reports focused Testbox and build/check evidence for the same head. (github.com) (e5829559a9f7)
  • Dependency authorization: Markdown Core adds three direct Markdown parser dependencies. The supplied PR discussion records exact-head authorization for that dependency-graph change and states that Markdown Core owns the parser contract. (github.com) (packages/markdown-core/package.json, e5829559a9f7)
  • Related-work status: The PR is explicitly linked as the candidate fix for the open self-closing-tag and unequal-backtick reports; the narrower unequal-backtick PR remains open, while this branch implements the broader shared-parser approach. (github.com) (e5829559a9f7)

Likely related people:

  • steipete: The supplied timeline shows assignment to this PR, repeated head updates, authorship of the current branch commit, and exact-head authorization of the Markdown dependency change. (role: active area contributor and likely merge-decision owner; confidence: medium; commits: e5829559a9f7; files: packages/markdown-core/src/reasoning-tags.ts, packages/markdown-core/src/reasoning-tag-parser.ts, src/shared/text/reasoning-tags.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Refresh the focused static and streamed runtime cases after rebasing or merging current main.

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 (10 earlier review cycles; latest 8 shown)
  • reviewed 2026-07-25T12:59:05.853Z sha 5632c67d7a345e32f52dee5b8aeec4e25813382f :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T06:30:23.100Z sha 5632c67d7a345e32f52dee5b8aeec4e25813382f :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T06:37:29.562Z sha 5632c67d7a345e32f52dee5b8aeec4e25813382f :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T08:32:36.729Z sha e900af5b77d016c10fc0dbf47a9c03c61b3801c5 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T09:58:18.319Z sha 1a133e26b062c16b0d0c8513a40576ad9f34a97a :: needs real behavior proof before merge. :: [P1] Authorize or remove the parser dependency graph change
  • reviewed 2026-07-27T10:19:31.251Z sha 1a133e26b062c16b0d0c8513a40576ad9f34a97a :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T11:19:51.060Z sha e582955 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-27T11:29:30.301Z sha e582955 :: needs maintainer review before merge. :: none

@steipete steipete changed the title fix(text): harden reasoning tag sanitization (#113144, #113148) fix(text): handle self-closing reasoning tags (#113144) Jul 25, 2026
@steipete
steipete force-pushed the fix/self-closing-reasoning-tags branch 4 times, most recently from 32297ee to 5632c67 Compare July 25, 2026 12:54
@licheer-zte

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 27, 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:

@steipete
steipete force-pushed the fix/self-closing-reasoning-tags branch from 5632c67 to 1adeb44 Compare July 27, 2026 08:06
@steipete
steipete requested a review from a team as a code owner July 27, 2026 08:06
@steipete steipete changed the title fix(text): handle self-closing reasoning tags (#113144) fix: prevent reasoning tags from leaking or hiding visible text Jul 27, 2026
@github-actions github-actions Bot added the dependencies-changed PR changes dependency-related files label Jul 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Guard

This PR changes dependency-related files. Maintainers should confirm these changes are intentional.

Changed files:

  • packages/markdown-core/package.json
  • pnpm-lock.yaml

Maintainer follow-up:

  • Review whether the dependency changes are intentional.
  • Inspect resolved package deltas when lockfiles or workspace dependency policy changes are present.
  • Treat pnpm-lock.yaml and package-lock.json diffs as dependency security-review surfaces.
  • Run pnpm deps:changes:report -- --base-ref origin/main --markdown /tmp/dependency-changes.md --json /tmp/dependency-changes.json locally for detailed release-style evidence.

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Dependency graph change authorized

This PR includes dependency graph changes. A repository admin or member of @openclaw/openclaw-secops authorized this exact head SHA with /allow-dependencies-change.

  • Approved SHA: e5829559a9f772a55888d898bf6c94c09893c9d1
  • Approved by: @steipete
  • Reason: markdown-core directly owns its existing mdast/micromark parser dependencies for the commissioned shared static and streaming sanitization contract at this exact head.

A later push changes the PR head SHA and requires a fresh security approval.

@steipete
steipete force-pushed the fix/self-closing-reasoning-tags branch from 1adeb44 to e900af5 Compare July 27, 2026 08:17
@steipete

Copy link
Copy Markdown
Contributor

/allow-dependencies-change markdown-core now directly owns its existing mdast/micromark parser dependencies for the commissioned shared static and streaming sanitization contract.

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. label Jul 27, 2026
@steipete

Copy link
Copy Markdown
Contributor

/allow-dependencies-change markdown-core directly owns its existing mdast/micromark parser dependencies for the commissioned shared static and streaming sanitization contract at this exact head.

@licheer-zte

Copy link
Copy Markdown
Contributor Author

CI is green. @clawsweeper re-review

@steipete
steipete force-pushed the fix/self-closing-reasoning-tags branch from 1a133e2 to e582955 Compare July 27, 2026 11:15
@openclaw-barnacle openclaw-barnacle Bot added the cli CLI command changes label Jul 27, 2026
@steipete

Copy link
Copy Markdown
Contributor

/allow-dependencies-change markdown-core directly owns its existing mdast/micromark parser dependencies for the commissioned shared static and streaming sanitization contract at this exact head.

@steipete

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 27, 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 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 27, 2026
@steipete
steipete merged commit 97d3d60 into openclaw:main Jul 27, 2026
205 of 220 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes dependencies-changed PR changes dependency-related files merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. 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: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

2 participants