Skip to content

refactor(tui): stop sanitizing each streamed markdown chunk twice - #127838

Merged
vyctorbrzezowski merged 1 commit into
mainfrom
brzezowski/hunt-mac-05
Aug 22, 2026
Merged

vyctorbrzezowski merged 1 commit into
mainfrom
brzezowski/hunt-mac-05

Conversation

@vyctorbrzezowski

Copy link
Copy Markdown
Member

What Problem This Solves

Streaming assistant snapshots crossed the TUI terminal-safety sanitizer in the stream assembler and then crossed the same sanitizer again at the markdown rendering boundary. A 400-delta cumulative stream therefore scanned 5,535,190 bytes while producing a final 13,890-byte response.

Why This Change Was Made

The stream-only extractors now preserve assistant text until HyperlinkMarkdown, the terminal rendering boundary that already owns markdown sanitization, URL extraction, and rendered-line isolation. Generic message extraction, history rendering, command output, attachment labels, errors, and non-assistant fallback paths retain their existing explicit sanitization.

Production LOC: +17/-17 (net 0). Tests: +9/-0.

User Impact

Streaming output is unchanged. Each cumulative assistant snapshot now crosses markdown source sanitization once instead of twice, reducing duplicate sanitizer work without adding a cache, trusted-text bypass, fallback, or compatibility path.

Evidence

Blacksmith Testbox proof: https://github.com/openclaw/openclaw/actions/runs/32565865725

400 cumulative deltas, 5 runs Before After Delta
Bytes processed by sanitizeMarkdownSource 5,535,190 2,767,595 -50.0%
End-to-end harness median (IQR) 646.857 ms (3.022) 617.064 ms (2.917) -4.61%

The timing change is incidental; this change removes the duplicate sanitization pass.

Regression proof:

FAIL TuiStreamAssembler > defers streamed terminal sanitization to the markdown boundary
Expected: before<OSC52>after
Received: beforeafter

Validation:

  • node scripts/run-vitest.mjs src/tui/tui-formatters.test.ts src/tui/tui-stream-assembler.test.ts src/tui/components/hyperlink-markdown.test.ts (120 passed)
  • node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts -t "authenticates a streamed prefix before the complete ordered final frame" (passed)
  • node scripts/check-changed.mjs -- src/tui/tui-formatters.ts src/tui/tui-stream-assembler.test.ts (passed)
  • git diff --check origin/main...HEAD (passed)

The full PTY suite encountered an unrelated timeout in reconciles active and terminal runs after reconnect history; the focused streaming PTY proof passed before and after the final rebase. Autoreview was attempted three times, including against the final branch, but its Codex subprocess exited before returning structured findings each time.

@vyctorbrzezowski vyctorbrzezowski self-assigned this Aug 22, 2026
@clawsweeper

clawsweeper Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added size: XS maintainer Maintainer-authored PR labels Aug 22, 2026
@vyctorbrzezowski vyctorbrzezowski changed the title refactor(tui): sanitize streamed markdown once per delta refactor(tui): sanitize each streamed markdown chunk only once Aug 22, 2026
@vyctorbrzezowski vyctorbrzezowski changed the title refactor(tui): sanitize each streamed markdown chunk only once refactor(tui): stop sanitizing each streamed markdown chunk twice Aug 22, 2026
@vyctorbrzezowski
vyctorbrzezowski marked this pull request as ready for review August 22, 2026 09:54
@vyctorbrzezowski
vyctorbrzezowski deleted the brzezowski/hunt-mac-05 branch August 22, 2026 10:01
@vyctorbrzezowski
vyctorbrzezowski restored the brzezowski/hunt-mac-05 branch August 22, 2026 10:01
@clawsweeper

clawsweeper Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 22, 2026, 9:17 AM ET / 13:17 UTC.

ClawSweeper review

What this changes

The PR stops the streamed-assistant assembler from sanitizing text that the TUI markdown renderer already sanitizes immediately before terminal rendering.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open for maintainer landing review: the focused refactor leaves terminal sanitization at the existing markdown boundary, and no actionable patch defect was found. The maintainer label prevents automated cleanup; the current head’s merge-result checks are still incomplete.

Priority: P3
Reviewed head: 46e4a40c760f15c6eac30c03a3f5554e030d2990
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, net-neutral refactor with source-backed terminal-boundary safety and no review findings; current-head merge verification remains before landing.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable: the PR author is a repository MEMBER; the PR body nevertheless links an after-fix Testbox run and focused PTY validation.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable: the PR author is a repository MEMBER; the PR body nevertheless links an after-fix Testbox run and focused PTY validation.
Evidence reviewed 5 items Current-main duplicate pass: Current main sanitizes assistant string and array content in the stream-facing extractor, while the renderer also sanitizes markdown source before constructing or updating its Markdown component.
Rendering owner remains safe: HyperlinkMarkdown sanitizes both initial and updated text before passing it to the Markdown component, then isolates rendered RTL lines; this is the terminal-facing owner boundary.
Stream-only caller boundary: The changed extractor is called by TuiStreamAssembler, whose delta output is immediately sent to the chat log’s AssistantMessageComponent and therefore the HyperlinkMarkdown boundary.
Findings None None.
Security None None.

Live Verification

Command: pnpm openclaw tui --help

Result: PASS (completed)

pnpm openclaw tui --help
runner@runnervm76f27:/tmp/clawsweeper-live-proof-127838-21m1DL/target$ pnpm openclaw tui --help
$ node scripts/run-node.mjs tui --help
pnpm openclaw tui --help
[openclaw] Building TypeScript (dist is stale: missing_build_stamp - build stamp missing).
[openclaw] Building bundled plugin assets.
[canvas] build: node scripts/bundle-a2ui.mjs
[log] ‹DIR›/a2ui.bundle.js  chunk │ size: 399.96 kB
[log] ‹DIR›/a2ui-v0.9.bundle.js  chunk │ size: 615.66 kB
[log]
[success] rolldown v1.2.0 Finished in 110.96 ms
[diffs] build: node --import tsx ../../scripts/build-diffs-viewer-runtime.mts curated
[diffs-language-pack] build: node --import tsx ../../scripts/build-diffs-viewer-runtime.mts full
[discord] build: node --import tsx ../../scripts/build-discord-activity-sdk.mts
[tsdown-build] invocation 1/2 finished in 0.7s
node_modules/web-tree-sitter/web-tree-sitter.js (2343:30) [EVAL] Use of direct ˋevalˋ function is strongly discouraged as it poses security risks and may cause
issues with minification.
      ╭─[ node_modules/web-tree-sitter/web-tree-sitter.js:2343:31 ]
      │
 2343 │           ASM_CONSTS[start] = eval(func);
      │                               ──┬─
      │                                 ╰─── Use of direct ˋevalˋ here.
      │
      │ Help: Consider using indirect eval. For more information, check the documentation: https://rolldown.rs/guide/troubleshooting#avoiding-direct-eval
──────╯

node_modules/web-tree-sitter/web-tree-sitter.js (2366:32) [EVAL] Use of direct ˋevalˋ function is strongly discouraged as it poses security risks and may cause
issues with minification.
      ╭─[ node_modules/web-tree-sitter/web-tree-sitter.js:2366:33 ]
      │
 2366 │           moduleExports[name] = eval(func);
      │                                 ──┬─
      │                                   ╰─── Use of direct ˋevalˋ here.
      │
      │ Help: Consider using indirect eval. For more information, check the documentation: https://rolldown.rs/guide/troubleshooting#avoiding-direct-eval
──────╯

node_modules/playwright-core/lib/coreBundle.js (65821:28) [EVAL] Use of direct ˋevalˋ function is strongly discouraged as it poses security risks and may cause
issues with minification.
       ╭─[ node_modules/playwright-core/lib/coreBundle.js:65821:29 ]
       │
 65821 │               const value = eval(ˋ(${expr})ˋ);
       │                             ──┬─
       │                               ╰─── Use of direct ˋevalˋ here.
       │
       │ Help: Consider using indirect eval. For more information, check the documentation: https://rolldown.rs/guide/troubleshooting#avoiding-direct-eval
───────╯

node_modules/playwright-core/lib/coreBundle.js (65828:28) [EVAL] Use of direct ˋevalˋ function is strongly discouraged as it poses security risks and may cause
issues with minification.
       ╭─[ node_modules/playwright-core/lib/coreBundle.js:65828:29 ]
       │
 65828 │               const value = eval(ˋ(${expr})ˋ);
       │                             ──┬─
       │                               ╰─── Use of direct ˋevalˋ here.
       │
       │ Help: Consider using indirect eval. For more information, check the documentation: https://rolldown.rs/guide/troubleshooting#avoiding-direct-eval
───────╯

[tsdown-build] invocation 2/2 finished in 14.1s
[plugin-control-plane-loads] verified 35 built modules with native require and checked doctor closures
runtime-postbuild: 11 phases completed in 4254ms (slowest: built plugin control-plane loads 3506ms)
│
◇

OpenClaw 2026.8.1 (46e4a40) — All your chats, one OpenClaw.

Usage: openclaw tui|terminal [options] [target]

Open a terminal UI connected to the Gateway

Arguments:
  target                      Control UI URL, host/agent/ref, short ref, or agent:... key

Options:
  --deliver                   Deliver assistant replies (default: false)
  -h, --help                  Display help for command
  --history-limit ‹n›         History entries to load (default: "200")
  
… output truncated …

Assertions:

  • PASS expect_output: Open a terminal UI connected to the Gateway

How this fits together

Gateway assistant snapshots flow through the TUI stream assembler into the chat log, then through the markdown renderer before terminal output. The markdown renderer owns source sanitization, URL extraction, and rendered-line isolation.

flowchart LR
A[Assistant stream snapshots] --> B[Stream assembler]
B --> C[Chat log updates]
C --> D[Markdown source sanitization]
D --> E[Markdown renderer]
E --> F[Terminal output]
Loading

Decision needed

Question Recommendation
Should this behavior-neutral TUI performance refactor be landed after the current merge-result checks complete? Land after current-head verification: Accept the single-boundary design once the exact head is checked against current main.

Why: The PR is maintainer-labeled and its remaining gate is a human landing decision over the current merge result, not a mechanical defect.

Before merge

  • Resolve merge risk (P1) - GitHub reports a clean but behind branch and the exact-head check rollup is incomplete, so the current merge result still needs normal pre-merge verification.
Agent review details

Security

None.

PR surface

Source 0, Tests +9. Total +9 across 2 files.

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

Review metrics

Metric Value Why it matters
Production versus test LOC production +17/-17 (net 0); tests +9/-0 The refactor removes duplicate work without growing production code; the added test protects the stream-to-renderer boundary.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep raw assistant stream snapshots until HyperlinkMarkdown’s existing markdown-source sanitizer, then land after the current merge result and exact-head checks are verified.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep raw assistant stream snapshots until HyperlinkMarkdown’s existing markdown-source sanitizer, then land after the current merge result and exact-head checks are verified.

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

Yes, at source level: current main sanitizes each streamed assistant snapshot before the markdown renderer sanitizes it again; the new regression encodes that exact boundary change. No test was executed because this is a read-only review.

Is this the best way to solve the issue?

Yes. HyperlinkMarkdown already sanitizes raw markdown before rendering, while non-stream/history extraction retains explicit sanitization, so moving this stream-only duplicate pass to its existing owner is the narrowest solution.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P3: This is a focused TUI performance cleanup with no indicated user-visible behavior change.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable: the PR author is a repository MEMBER; the PR body nevertheless links an after-fix Testbox run and focused PTY validation.

Evidence

What I checked:

  • Current-main duplicate pass: Current main sanitizes assistant string and array content in the stream-facing extractor, while the renderer also sanitizes markdown source before constructing or updating its Markdown component. (src/tui/tui-formatters.ts:523, 4f5905373d17)
  • Rendering owner remains safe: HyperlinkMarkdown sanitizes both initial and updated text before passing it to the Markdown component, then isolates rendered RTL lines; this is the terminal-facing owner boundary. (src/tui/components/hyperlink-markdown.ts:37, 46e4a40c760f)
  • Stream-only caller boundary: The changed extractor is called by TuiStreamAssembler, whose delta output is immediately sent to the chat log’s AssistantMessageComponent and therefore the HyperlinkMarkdown boundary. (src/tui/tui-stream-assembler.ts:160, 46e4a40c760f)
  • Focused regression coverage: The added regression proves the stream assembler now preserves an OSC-containing snapshot for the renderer; on current main, the earlier extractor sanitizes that value before it reaches the boundary. (src/tui/tui-stream-assembler.test.ts:75, 46e4a40c760f)
  • Area provenance: Recent history shows Peter Steinberger’s TUI stream and terminal-hardening work, including the unsafe-stream handling commit, on the affected formatter path. (src/tui/tui-formatters.ts:516, eeba5a343317)

Likely related people:

  • Peter Steinberger: History covers the formatter, stream assembler, and markdown-rendering neighborhood, including recent streamed-reply hardening. (role: recent TUI stream and terminal-hardening contributor; confidence: high; commits: eeba5a343317, b83a966c0562, 1de8b74e7029; files: src/tui/tui-formatters.ts, src/tui/tui-stream-assembler.ts, src/tui/components/hyperlink-markdown.ts)

Rank-up moves

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

  • Let the current exact-head checks finish and verify the clean merge result against 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 (2 earlier review cycles)
  • reviewed 2026-08-22T09:59:51.574Z sha e08e5087b6c4255070c12a7c9537d34683c64510 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-22T10:44:42.939Z sha e08e5087b6c4255070c12a7c9537d34683c64510 :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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. labels Aug 22, 2026
@vyctorbrzezowski
vyctorbrzezowski merged commit ea1cd39 into main Aug 22, 2026
227 checks passed
@vyctorbrzezowski

Copy link
Copy Markdown
Member Author

Merged via squash.

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

Labels

maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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.

1 participant