Skip to content

perf(streaming): defer partial HTML tag prefixes - #180775

Closed
xiaopan-xydt wants to merge 2 commits into
openclaw:mainfrom
xiaopan-xydt:perf/defer-partial-html-prefixes-180720
Closed

xiaopan-xydt wants to merge 2 commits into
openclaw:mainfrom
xiaopan-xydt:perf/defer-partial-html-prefixes-180720

Conversation

@xiaopan-xydt

Copy link
Copy Markdown
Contributor

Fixes #180720

What Problem This Solves

A long ordinary HTML answer could still make the Gateway pause while it streamed because every partial tag such as <p triggered a full Markdown parse of the entire growing reply. Partial prefixes are now carried as an unscanned tail until a later chunk proves they belong to a tool-call tag; normal HTML remains a cheap pass-through.

  • Problem: toolCallXmlTextFilter(..., true) deferred a partial prefix only when it was inside a settled fenced block. Outside that case, <p, <param, and similar ordinary HTML prefixes called findCodeRegions() and then transformed the whole accumulated reply. Four-character chunks repeatedly landed on these prefixes, producing superlinear work.
  • Solution: defer every partial prefix that is not already a complete recognizable tool-call tag, without parsing Markdown ownership. Continue scanning later openings so a complete tool tag elsewhere in the same delta still activates the canonical transform.
  • What changed: the streaming activation probe in src/shared/text/tool-call-xml.ts now treats partial prefixes as pending text. The work-budget fixture was changed from fenced JSX to 16 KB of ordinary unfenced HTML whose chunk boundaries land on partial tags.
  • What did NOT change: complete tool-call tags still use the canonical code-region resolver. Complete tags immediately followed by more text still activate stripping immediately.

Why This Change Was Made

A partial prefix cannot be stripped before it is complete, so parsing the whole document to decide ownership is wasted work. Deferring it preserves the exact canonical decision for the later delta that completes the tag, while a prefix that resolves to ordinary HTML never needs the resolver at all.

  • Best fix: move the settled-fence shortcut to the general partial-prefix case. The safety property is local: an incomplete candidate is not actionable yet, and the scan continues instead of stopping.
  • Alternative: cache code-region results across deltas. That is more state, more invalidation risk, and still parses at least once for every partial prefix; it does not address the growth pattern directly.
  • Risk / non-goals: a very long stream with an unfinished partial tool-call prefix keeps that one suffix unscanned until it resolves. This PR does not change completed-tag stripping, fence parsing, inline-code ownership, or final visible text for completed markup.

User Impact

Long HTML, JSX, XML, and comparison-heavy replies no longer monopolize the shared Gateway event loop at every chunk that ends inside a tag name. Users see the same visible text, but unrelated sessions and scheduled work can continue responding while the answer streams.

Evidence

Shipped createAssistantVisibleStreamText() path, 65 KB HTML, four-character chunks, fresh Node process:

$ node --import ./scripts/tsx.mjs .../180720-entrypoint-probe.ts
multiline chars=65520 wallMs=772.0 cpuMs=880.8 visible=65519
single chars=65536 wallMs=987.3 cpuMs=1012.2 visible=65536

The issue's pre-fix measurements for the same fixture were 4840.5 ms and 5175.8 ms; visible text and character counts remain unchanged.

Deterministic base-red on the new unfenced 16 KB HTML work-budget fixture, before changing the production filter:

$ OPENCLAW_VITEST_RUNTIME=node pnpm test src/shared/text/tool-call-xml.streaming-work.test.ts --maxWorkers=1 -t 'does not parse Markdown ownership'
AssertionError: expected 6812244 to be less than 37738
Tests  1 failed | 4 skipped (5)

After the filter change:

✓ does not parse Markdown ownership for ordinary HTML/JSX markup 29ms
✓ still strips a tool-call tag whose name is split across deltas 5ms
✓ preserves a quoted tool-call example inside fenced code 9ms
✓ still strips a tool call that follows ordinary markup 41ms
✓ still strips a tool call behind a malformed backtick fence 9ms
Tests  5 passed (5)

Broader projection regression run:

$ OPENCLAW_VITEST_RUNTIME=node pnpm test src/shared/text/tool-call-xml.streaming-work.test.ts src/shared/text/assistant-visible-text.streaming-work.test.ts src/shared/text/assistant-visible-text.test.ts --maxWorkers=1
Test Files  3 passed (3)
Tests  108 passed (108)

What was not tested: a live multi-session Gateway under sustained 64 KB HTML output. The probe calls the shipped projection entry point and the tests cover the filter composition path. Full TypeScript checking with the repository's tsgo runner was unavailable on this host because its downloaded Linux binary requires a newer glibc than the host provides; CI will run the repository type checks.

AI Assistance

This PR is AI-assisted. Used rg, git diff, the existing streaming work-budget test, focused Vitest runs, and a fresh-process probe through the shipped visible-stream API to locate the residual parse path and verify the result. The implementer understands the code: an incomplete tag prefix is deferred without ownership parsing, while a completed tool-call tag still takes the canonical resolver.

@clawsweeper

clawsweeper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 22, 2026
@clawsweeper

clawsweeper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head 003361296e7fbfdda8a357dc9c23eec040228077, but the PR head is now a3962c21b7fa7dca510e937714af2fa3ca01e647. Its old verdict and PR readiness labels are no longer current.

Next step
Run or wait for a fresh ClawSweeper review on the current PR head.

Review history (1 earlier review cycle)
  • reviewed 2026-10-22T16:39:13.056Z sha 0033612 :: needs real behavior proof before merge. :: [P2] Preserve withholding of unresolved tool-tag prefixes | [P2] Retire only candidates with settled block-code ownership

Hold unresolved tool-tag prefixes until they resolve instead of emitting and retracting them, and keep inline-code candidates on the canonical resolver because later references can revoke their ownership. Track prefix holds by length so the ordinary-HTML fast path stays linear.
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

Addressed both P2 projection regressions in a3962c2:

  • Unresolved tool-tag prefixes are now withheld through a prefix-hold projection instead of being emitted and retracted. Before. <too stays Before. , and completing it produces an empty delta rather than a full replacement.
  • Complete candidates are retired only inside settled block code. Inline-code candidates keep canonical processing, so the image-reference case now strips the tool call once [id]: /url revokes the inline-code ownership.

Verification: the full src/shared/text suite passes (269 tests), including new regressions for both cases. The 76 KB unfenced-HTML / four-character-chunk probe is 86 ms with unchanged text. oxlint and oxfmt are clean. tsgo could not run in the temporary checkout because tsrs is absent from the local install; CI will cover the core typecheck.

The live Gateway/client proof requested in the first checklist item is still outstanding.

@steipete

Copy link
Copy Markdown
Contributor

The canonical fix landed in #180765 at edbe9a2. It handles partial HTML prefixes in the XML projection owner, preserves unfinished and completed-line code ownership, and keeps the generic projector API unchanged.

Thank you @xiaopan-xydt for investigating this and proposing a fix. Leaving this draft for you to manage.

@clawsweeper clawsweeper Bot removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 23, 2026
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

Closing as superseded. Main now handles the unresolved-prefix stream path in #180765 (edbe9a2), including prefix holding, the fence fast path, and chunk-boundary parity coverage. That implementation explicitly avoids the broader hold API proposed here, and its tests cover the scenarios added by this PR. The branch also conflicts with the newer tool-call-xml.ts refactor, so there are no unique changes left to land.

@xiaopan-xydt
xiaopan-xydt deleted the perf/defer-partial-html-prefixes-180720 branch October 24, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: partial HTML tag prefixes still re-parse the whole streamed reply

2 participants