Skip to content

fix: reduce repeated scans in assistant prose streams - #183345

Merged
steipete merged 4 commits into
openclaw:mainfrom
gdpu11:perf/streaming-paragraph-rescan
Oct 27, 2026
Merged

steipete merged 4 commits into
openclaw:mainfrom
gdpu11:perf/streaming-paragraph-rescan

Conversation

@gdpu11

@gdpu11 gdpu11 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes repeated full-reply scans while a long assistant answer streams after an early duplicate paragraph.

User Impact

Long prose replies use less projection CPU while retaining the same visible text, correction deltas, code protection, and raw conversation history. The measurements below retain the observed memory and tail-latency tradeoffs.

Why This Change Was Made

Once a duplicate appeared, each subsequent paragraph boundary reran canonical collapse on the growing reply. Reuse the existing incremental paragraph state while the existing Markdown guard proves code is absent, then retain canonical protection for ambiguous syntax. A three-character tail detects split indentation. The broad channel-benchmark extension was removed; a fixed owner benchmark remains with no new CLI options.

Production: +9 lines (+10/-1); tests: +20; benchmark: +79. The two state fields and bounded probe preserve late code ownership without a new parser, cache, or duplicated collapse implementation.

Evidence

  • Current-main regression red at 12bb61686cc7: the final-answer stream preserved expected text and delta reconstruction but scanned 505,518 Markdown characters against a 127,488 budget (one intended failure, 52 skipped; 9 seconds Vitest / 13.11 seconds wrapper).
  • node scripts/run-vitest.mjs src/shared/text/assistant-visible-text.streaming-work.test.ts src/shared/text/text-projection.test.ts --maxWorkers=1: all 76 tests passed after integrating current main (29.40 seconds Vitest / 31.19 seconds wrapper). This covers late fenced and indented code across every split; the merge preserves current CRLF paragraph separators. The new bounded-work case takes 13 ms.
  • Targeted oxlint, oxfmt, core source and shared-test typechecks, and diff checks passed.
  • Independent Codex review through P2, with the complete projector owner and Markdown helper supplied, found no actionable findings.
  • Exact-head CI: https://github.com/openclaw/openclaw/actions/runs/40584258320 (completed; inherited lint failure only).

The current-main acceptance rule was recorded before these measurements: fixed 128-paragraph input plus no-duplicate control, four-character chunks, five interleaved base/head processes; exact output/delta reconstruction, approximately linear scan work, at least 75% fewer scanned characters, and no unexplained material projector regression. Run node --import ./scripts/tsx.mjs scripts/bench-duplicate-paragraphs.mts. Node v26.12.1, host load 26.8–29.3. RSS is captured before separate scan-count instrumentation. First projection and warmed projection are reported separately; this does not measure cold Gateway startup.

Distinct paragraphs Input characters Main scanned characters Fix scanned characters
32 2,764 46,608 2,852
64 5,484 180,080 5,572
128 10,924 708,144 11,012
128, no duplicate 10,923 10,923 10,923

All ten processes preserved exact text and delta reconstruction. Duplicate workload correction count stayed one; control stayed zero. Scan work at 128 paragraphs fell 98.44%.

Times are median / empirical p95 (maximum of five), milliseconds:

Workload / phase Main Fix
Duplicate / first projection 29.775 / 31.150 21.928 / 23.055
Duplicate / warmed projection 35.575 / 36.340 17.106 / 17.287
Control / first projection 9.449 / 10.113 8.911 / 9.176
Control / warmed projection 9.684 / 9.788 9.368 / 9.636

Thread CPU medians: duplicate first 29.655 → 21.758 ms, warm 35.515 → 17.079; control first 9.464 → 8.919, warm 9.685 → 9.316. Process peak RSS median 246,688 → 254,080 KiB (+3.0%); sample maximum 257,376 → 257,360 KiB. The memory median moved adversely despite the flat maximum.

Historical contributor built-Gateway/QA-transport proof at d9e4bb4ceeec completed 110 total base/candidate turns using a local Responses fixture: exact delivered text, one final outbound per turn, unchanged request counts, and raw provider bytes retained in history. It also measured Gateway cold RSS median +12.57%, warmed final-outbound p95 +2.52%, and no-duplicate projector warm-wall p95 +6.07%. Those measurements are retained as historical evidence, not silently superseded by this owner benchmark. A fresh integrated-head Gateway/channel run was not repeated because rebuilding that harness exceeds the bounded live-proof budget.

Original contribution by @gdpu11. Co-authored-by: liqiang2 [email protected]

A fresh base/head run of the committed workload on the integrated source preserves exact output and reconstruction, with one correction for the duplicate workload and zero for the no-duplicate control. At 128 paragraphs, scanned characters remain 708,144 → 11,012; the control remains 10,923 → 10,923. This single pair verifies correctness and work counts; the timing/RSS study above remains the earlier five-pair measurement.

CI note: exact-head run 40584258320 is complete. Only check-lint-core-4 and the resulting CI gate failed. Its five eslint(curly) errors in two unrelated UI browser tests exactly match scheduled main run 40578163761, check-lint-core-4. This PR changes neither UI file. The automatic report leaves attribution unknown because selected scopes differ. No unrelated UI repair or CI rerun was made.

@clawsweeper

clawsweeper Bot commented Oct 25, 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.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: M labels Oct 25, 2026
@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. clawsweeper:intake:needs-repro 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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge.

What this changes

Reuse incremental paragraph results after duplicate prose appears, retaining canonical Markdown protection when code syntax becomes possible.

Example: Two identical opening paragraphs followed by 128 distinct paragraphs stream in four-character chunks.

  • Before: The recorded 10,924-character workload scans 708,144 characters.
  • After: It scans 11,012 characters while preserving deduplicated text and reconstructed deltas.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused, behavior-proven optimization with no actionable findings; the reported performance tradeoff still requires explicit owner acceptance.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (logs): Recorded built-Gateway QA-transport proof at d9e4bb4 exercised duplicate prose through agent execution, delivery, and history across 110 baseline/candidate turns; the introduced optimization remains the same, with refreshed integrated-source output, reconstruction, and work-count evidence supporting reuse without claiming a fresh integrated Gateway run.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Long prose replies can consume unnecessary Gateway CPU after an early repeated paragraph.
Reason: The patch removes measured redundant work at the shared owner while preserving output, correction semantics, public contracts, and configuration.

Merge readiness

⛔ Blocked before merge - 1 item remains

This PR provides a focused, useful optimization with no actionable code findings; explicit performance-owner acceptance of the reported tradeoffs remains outstanding.

Priority: P2
Reviewed head: 8d8005d4d94f1271621332337ac6ea65c4e3bd2f
Owner decision: Required. See Decision needed.

Decision needed

  • Question: Are this PR's recorded memory and tail-latency increases acceptable for its measured reduction in streaming scan work?
  • Recommendation: Accept the measured tradeoff: The performance owner explicitly accepts the reported adverse cells given substantially lower scan work, lower measured projection CPU, and unchanged output.
  • Why: The performance policy states, "A worse metric needs the owner's explicit acceptance in the PR." No such acceptance is recorded for this PR; acceptance on the separate checkpoint PR covers different measurements.

Before merge

  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.

Findings

None.

Agent review details

How this fits together

Provider text enters the shared assistant visibility and sanitization pipeline, which emits visible reply text and correction deltas for delivery.

flowchart LR
  A[Provider text deltas] --> B[Assistant visibility filters]
  B --> C[Duplicate paragraph projector]
  C --> D[Markdown ownership guard]
  D --> E[Incremental prose state]
  D --> F[Canonical code protection]
  E --> G[Visible text and deltas]
  F --> G
Loading

Technical review

Best possible solution:

Retain the existing-owner optimization if the performance owner explicitly accepts its recorded memory and latency tradeoffs.

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

Current-main source exposes accumulated-text reconciliation after duplicate paragraphs, and the supplied base-red regression records excessive scan work; this read-only review executed no target code.

Is this the best way to solve the issue?

The smallest complete fix reuses the existing incremental paragraph state while its Markdown guard proves code absence, preserving canonical protection without another parser or collapse owner.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against fd611c760e08.

Provenance checked

  • Incremental duplicate-paragraph projection keeps the original intent (6c1dde5: Reduce CPU and allocation while streaming long replies without changing canonical sanitizer behavior.)
  • Canonical code protection keeps the original intent (cfe80b3: Preserve fenced code bytes, indentation, and repeated code lines during prose duplicate collapse.)
  • Checkpoint ownership and global duplicate correction keeps the original intent (0bad3b4: Resume canonical ownership from stable checkpoints while preserving existing global normalization when actual duplicate corrections occur.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 6c1dde5 adds src/shared/text/text-projection.ts:161 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 6c1dde5291ad; files: src/shared/text/text-projection.ts)
  • ruel225: Raw commit cfe80b3 adds src/shared/text/text-projection.ts:164 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: cfe80b3edb67; files: src/shared/text/text-projection.ts)

PR surface

Source +9, Tests +20, Other +79. Total +108 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 10 1 +9
Tests 2 20 0 +20
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 79 0 +79
Total 4 109 1 +108

Review metrics

Metric Value Why it matters
Runtime and regression delta Production +10/-1; tests +20/-0 The small runtime addition reuses existing paragraph and Markdown owners; the tests protect bounded scan work and late code ownership.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: A focused CPU improvement for long streamed replies containing adjacent duplicate paragraphs.
  • merge-risk: 🚨 other: Reported memory and tail-latency increases require explicit performance-owner acceptance that green CI cannot establish.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (3 earlier review cycles)
  • reviewed 2026-10-25T06:46:12.701Z sha d9e4bb4 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-25T07:06:31.375Z sha d9e4bb4 :: blocked before merge. :: none
  • reviewed 2026-10-26T01:17:55.076Z sha 5c49cf5 :: blocked before merge. :: none

Reviewed October 27, 2026, 5:22 PM ET / 21:22 UTC (Revision 4).

@gdpu11
gdpu11 marked this pull request as ready for review October 25, 2026 06:50
@gdpu11

gdpu11 commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added merge-risk: 🚨 other 🚨 Merging this PR has meaningful risk outside the owned taxonomy. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and 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 25, 2026
steipete and others added 2 commits October 25, 2026 18:11
Retain incremental duplicate-paragraph results until code syntax needs the canonical owner. Keep fixed workload measurements and late code-ownership regressions.

Co-authored-by: liqiang2 <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
JasonBuildAI added a commit to JasonBuildAI/openclaw that referenced this pull request Oct 27, 2026
Integrate current main while preserving CRLF separators and canonical code protection. The bounded prose probe avoids repeated scans without changing visible text or correction deltas.

Co-authored-by: liqiang2 <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 27, 2026
@steipete
steipete merged commit 14081a8 into openclaw:main Oct 27, 2026
162 of 168 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
Reuse the existing incremental paragraph state after duplicate prose instead of repeatedly scanning the accumulated stream. Preserve canonical fallback, overlap handling, and CRLF behavior.

Validated with the base scan-budget regression, 76 focused tests, targeted checks, and exact output/delta benchmark comparison. Benchmark scans fell from 708,144 to 11,012; control remained 10,923. Historical owner measurements reported roughly 3% higher RSS. No fresh Gateway proof was run. CI completed with the five unrelated UI eslint(curly) diagnostics also present on scheduled main run 40578163761; those inherited failures are retained under the authorized exception.

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: liqiang2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 other 🚨 Merging this PR has meaningful risk outside the owned taxonomy. 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. scripts Repository scripts size: S status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants