Repository navigation
fix: keep the Gateway responsive during reply-tagged streams - #166782
Conversation
…ies stream Once a streamed reply contains a reply directive tag such as [[reply_to_current]], reply directive parsing and block-reply snapshots re-parse the whole reply's Markdown code ownership about ten times per delta, so a tagged reply costs O(n^2) CPU: 8,000 characters streamed as token-sized deltas took about 70 s. Cache parseMarkdownOwnership for calls without options. Ownership before a settled top-level block is reused and parsing resumes at that block, and the last results are reused for repeated texts. A settled block starts with a visible character at column 0 after a blank line that follows a paragraph, heading, thematic break, or fenced code block, where micromark's container, lazy-line, and interrupt state is reset, and parsing from it must reproduce the whole parse. Text that may hold a link reference definition is never resumed.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesThis PR reuses settled directive text and prepared appends to avoid repeatedly parsing complete streamed replies. Example: An 8,180-character answer containing [[reply_to_current]] arrives in 20-character SSE deltas.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR fixes a demonstrated Gateway streaming slowdown that remains on current main; the focused patch has relevant Gateway proof and no actionable review findings. Priority: P1 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe embedded-agent subscriber converts provider text events into visible partial replies and block replies, interpreting delivery directives before channel delivery. flowchart LR
A[Provider text events] --> B[Message update owner]
B --> C[Streaming directive projection]
C --> D[Canonical parser fallback]
C --> E[Partial reply output]
B --> F[Block reply chunker]
F --> G[Channel delivery]
Technical reviewBest possible solution: Land the focused stream-owner optimization once the required checks pass. Do we have a high-confidence way to reproduce the issue? Current-main source exposes repeated complete directive parsing and block snapshots, supported by supplied before/after Gateway observations; this read-only review ran no target code. Is this the best way to solve the issue? Reusing the stream owner's prepared append addresses the demonstrated cause while preserving canonical parsing for uncertain contexts and avoiding a separate Markdown ownership cache. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against c5b1c315d052. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +31, Tests +19. Total +50 across 3 files. Added test files: 0. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (6 earlier review cycles)
Reviewed October 12, 2026, 11:14 PM ET / October 13, 2026, 03:14 UTC (Revision 7). |
|
@clawsweeper re-review Added the requested running-Gateway proof to the PR body ("Running Gateway (QA Lab, isolated state)" under Evidence): a long |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed in 3a2160c. Thanks @boeingchoco! Verified on a built Gateway: a long reply-tagged stream now delivers in about 1.2 s instead of 14 s, with identical final text and reply targets. We replaced the Markdown cache with an append fast path in the existing stream owner, so production went from +257 to +31 lines. |
…w#166782) fix(agents): reuse settled directive text while streaming long tagged replies Long streamed replies containing reply directives re-parsed the whole Markdown document on every delta, holding the Gateway event loop and delaying delivery by many seconds. When a delta is a proven append after a settled directive (no code openers before the marker, no new markers and no reference definitions), the stream owner now reuses the previous cleaned text and appends the delta. Anything uncertain still takes the full parse. Proof: built Gateway, QA channel, scripted Responses stream with an 8,180-character tagged reply: delivery took 13,973 ms on main and 1,230 ms on the candidate; health median went from 41 ms to 11 ms. Final text hashes and reply targets are identical for the tagged, untagged and fenced-directive controls. Untagged delivery median: 620 ms on main, 497 ms on the candidate. Fixes openclaw#166781. Thanks @boeingchoco. Co-authored-by: Ayaan Zaidi <[email protected]>
fix(agents): reuse settled directive text while streaming long tagged replies Long streamed replies containing reply directives re-parsed the whole Markdown document on every delta, holding the Gateway event loop and delaying delivery by many seconds. When a delta is a proven append after a settled directive (no code openers before the marker, no new markers and no reference definitions), the stream owner now reuses the previous cleaned text and appends the delta. Anything uncertain still takes the full parse. Proof: built Gateway, QA channel, scripted Responses stream with an 8,180-character tagged reply: delivery took 13,973 ms on main and 1,230 ms on the candidate; health median went from 41 ms to 11 ms. Final text hashes and reply targets are identical for the tagged, untagged and fenced-directive controls. Untagged delivery median: 620 ms on main, 497 ms on the candidate. Fixes #166781. Thanks @boeingchoco. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 3a2160c) Backport note: the release branch wraps the block-reply plain-append check in an isPhasePendingText guard; the widened plainAppend condition is applied inside it. resolveStreamingReply is byte-identical to main after the port.
fix(agents): reuse settled directive text while streaming long tagged replies Long streamed replies containing reply directives re-parsed the whole Markdown document on every delta, holding the Gateway event loop and delaying delivery by many seconds. When a delta is a proven append after a settled directive (no code openers before the marker, no new markers and no reference definitions), the stream owner now reuses the previous cleaned text and appends the delta. Anything uncertain still takes the full parse. Proof: built Gateway, QA channel, scripted Responses stream with an 8,180-character tagged reply: delivery took 13,973 ms on main and 1,230 ms on the candidate; health median went from 41 ms to 11 ms. Final text hashes and reply targets are identical for the tagged, untagged and fenced-directive controls. Untagged delivery median: 620 ms on main, 497 ms on the candidate. Fixes #166781. Thanks @boeingchoco. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 3a2160c) Backport adaptation: applied the plainAppend condition change inside the release branch's phase-pending guard block.
#172710) fix(agents): reuse settled directive text while streaming long tagged replies Long streamed replies containing reply directives re-parsed the whole Markdown document on every delta, holding the Gateway event loop and delaying delivery by many seconds. When a delta is a proven append after a settled directive (no code openers before the marker, no new markers and no reference definitions), the stream owner now reuses the previous cleaned text and appends the delta. Anything uncertain still takes the full parse. Proof: built Gateway, QA channel, scripted Responses stream with an 8,180-character tagged reply: delivery took 13,973 ms on main and 1,230 ms on the candidate; health median went from 41 ms to 11 ms. Final text hashes and reply targets are identical for the tagged, untagged and fenced-directive controls. Untagged delivery median: 620 ms on main, 497 ms on the candidate. Fixes #166781. Thanks @boeingchoco. Backport note: on 2026.9.33 the block-chunker plainAppend check sits inside the phase-pending guard; the widened append condition is applied there unchanged. (cherry picked from commit 3a2160c) Co-authored-by: boeingchoco <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
…reset for 8.35-C Backport OpenClaw PRs openclaw#168400, openclaw#169728, openclaw#166782 and openclaw#135653 onto the existing 2026.8.35 custom release. Preserve the 8.35 UI, locked dependencies, schema versions, bundled Codex and previous A/B maintenance repairs. Canonical build source: 06aadf04949b9aaa0d2665c952124df700ccc191 Verified complete source tree: 4d9e205 Upstream base: 713ba57 Runtime checks run inside the single resource-bounded Docker build. Memory reset is an explicit operator command and is not executed during build or deployment.
Closes #166781
What Problem This Solves
Fixes: a long streamed reply containing a reply directive repeatedly parses the entire Markdown document, keeping the Gateway busy and potentially timing out delivery.
User Impact
Long replies whose already-consumed directives cannot change on append keep streaming without repeated whole-document parsing. Final text, reply targets, code examples, and existing silent/audio behavior are preserved. No configuration or migration is needed.
Why This Change Was Made
The repeated work originates in
handleMessageUpdate:resolveStreamingReplyre-parses the accumulated reply, then previous/next block snapshots parse it again. The caller already owns the previous rendered text and the exact append.Reuse those facts when the previous directive has a settled visible suffix, no code opener precedes it, and the append introduces neither a new marker nor a reference definition or media directive. All other cases retain the canonical full-context parser. The block chunker also consumes an already-proven append instead of reconstructing identical snapshots.
This replaces the proposed Markdown ownership cache entirely: no new cache module, global retained text, JSON comparisons, or ownership copies. The original contributor commits remain credited. Production delta against the merge base is +34/-3 (net +31); tests +19/-0.
Evidence
Built, isolated Gateway with QA channel and a scripted Responses provider. Baseline
d6b01a200752e8f121bc21263c6563bee058b1b4; candidate437aaf724d38b57bd0987619f6b5226fac6cef9a. Same 8,180-character body in 20-character SSE deltas; health RPC sampled every 50 ms.These are individual observations, not a tail-latency guarantee. To check the apparently slower untagged pair, three alternating fresh-Gateway runs measured two untagged deliveries each:
The initial 313-to-549/656 ms comparison was not a reproducible common-path regression. The existing untagged fast path remains first and does not enter the new directive work. The caller-only experiment also matched the discarded cache's tagged delivery time: 1,278 ms versus 1,253 ms, with identical output.
All final text hashes match baseline, including the fenced control, and every observed reply targets its inbound message. Tagged and untagged SHA-256:
25c686744f794a630a822e7a6f6ccdcc2ae6067712bdd6f0ae9baa57ce0c1aae. Fenced control:513fc12d020013efe1b6a0fb589cf7f92dbe91f10d6f682eea5c2451e6a14e19; the literal tag survives.With the original token-sized fixture, the baseline saturated a CPU for about 89 seconds and its Responses request timed out without the expected final answer. The bounded-delta comparison above permits completed-output equivalence checks.
openai/gpt-5.4-minireturnsOPENCLAW_STREAM_DIRECTIVE_LIVE_OKon the candidate.a2bbcbf4068: commentary final-message handling and quiet Slack preview policy are untouched.Thanks to @boeingchoco for reporting the issue and the original fix and proof.
Merge danger: two-way door; a revert restores previous processing with no persisted-state changes. Blast radius: streaming directive projection and block replies; uncertain Markdown contexts continue using the existing parser.