Repository navigation
fix(matrix): a long reply with a code block arrives split into broken blocks - #196167
Conversation
… blocks Durable final replies are pre-chunked in core by the channel adapter's chunker before Matrix renders them. Matrix used chunkTextForOutbound, which splits on newlines and spaces without knowing about fences, so a reply over the 4000-character limit that had a code block across the boundary produced one event ending inside an unclosed fence and a second event that lost the code indentation and opened a stray block around the trailing prose. Use the fence-aware chunkMarkdownText (as iMessage already does) for both Matrix outbound adapters. It closes the fence at the end of a chunk and reopens it with the original info string in the next, within the limit.
|
🦞👀 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 changesBoth Matrix outbound adapters use the existing fence-aware Markdown chunker to preserve code blocks across messages. Example: Send a reply containing a 5.2k-character Python code block through Matrix.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR fixes a verified Matrix formatting bug with a focused change and exact-head Synapse proof; no actionable defects remain, and required CI is still pending. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherCore outbound delivery splits reply text using the Matrix adapter before Matrix renders and sends each event to the homeserver. flowchart LR
A[CLI send or final reply] --> B[Core outbound planner]
B --> C[Matrix adapter chunker]
C --> D[Shared Markdown chunker]
D --> E[Matrix event renderer]
E --> F[Matrix homeserver]
Technical reviewBest possible solution: Land the existing narrow adapter repair once required exact-head CI passes. Do we have a high-confidence way to reproduce the issue? Source confirms that fence-unaware pre-chunking bypasses Matrix's later repair opportunity, and supplied Synapse readbacks reproduce the failure against main; this review executed no target code. Is this the best way to solve the issue? This is the smallest complete fix: use the existing Markdown owner in both adapter registrations while retaining lazy loading, fractional-limit handling, and the separate public compatibility facade. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against e23c587c16f3. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource -2, Tests -1. Total -3 across 4 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. 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 HistoryReviewed November 9, 2026, 2:22 PM ET / 19:22 UTC (Revision 3). |
Use chunkMarkdownTextWithMode from reply-chunking instead of the reply-runtime barrel, which pulls reply orchestration into the hot channel.ts/outbound.ts imports.
|
Addressed the P2 import-path finding in 58ffe5f: both Matrix adapters now take the chunker from @clawsweeper re-review 🤖 Addressed by Claude Code |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Real Synapse proof for Setup: Synapse 1.162.0 and both builds in Docker on Node 24.22.1. Each reply went through Every PR event is at most 4000 chars and has an even number of fence lines. Focused tests on this head pass: outbound, channel message adapter, send and reply formatting (4 files, 74 tests). |
|
Landed in e138bd4. Thanks @Chr0mium24. Long Matrix replies now keep their code blocks intact. |
fix(mattermost): use fence-aware chunker for outbound markdown Mattermost split long replies with chunkTextForOutbound, which ignores code fences. A reply over 4000 characters whose code block crossed the split arrived with an open fence in the first post and a stray fence in the next, so code showed as plain text and lost indentation. Mattermost now uses the fence-aware splitter the plugin SDK already exports, the same change #196167 made for Matrix. Evidence: a real Mattermost server and both builds in Docker on node:24.22.1, replies sent with openclaw message send --channel mattermost through a running Gateway and read back over the REST API. For a 4531-character reply, main posted 3996 and 526 characters with the first post ending inside an open fence and 16 code lines shown as plain text. With this change the prose and the whole Python block arrive in balanced, indented posts under 4000 characters. A short reply with code, 9000 characters without code, and Chinese text with emoji across the split are identical on both. Details: #196331 (comment). Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
… blocks (openclaw#196167) fix(matrix): a long reply with a code block arrives split into broken blocks Matrix split long replies with chunkTextForOutbound, which ignores code fences. A reply over 4000 characters whose code block crossed the split arrived as an unclosed code block, then code shown as plain text, then the trailing text wrapped in a stray code block. Matrix now uses the fence-aware splitter the plugin SDK already exports and Feishu already uses. Evidence: Synapse 1.162.0 and both builds in Docker on node:24.22.1, replies sent with openclaw message send through a running Gateway and read back by a second Matrix user, two runs per build. For a 4531-character reply with a Python block crossing 4000, main sent 2 events where the first ended in an open fence, 16 code lines showed as plain text and the trailing note landed in a stray code block. With this change the code block arrives whole and the note is plain text. For a 5.2k code block that has to split inside the fence, main left 40 code lines as plain text; this change closes the fence at the split and reopens it. A short reply, 9000 characters of plain text, a code block ending before the limit, and CJK/emoji/ZWJ text across the split are identical on both. Every event is at most 4000 characters. Details: openclaw#196167 (comment). Thanks @Chr0mium24. Co-authored-by: Ayaan Zaidi <[email protected]>
…law#196331) fix(mattermost): use fence-aware chunker for outbound markdown Mattermost split long replies with chunkTextForOutbound, which ignores code fences. A reply over 4000 characters whose code block crossed the split arrived with an open fence in the first post and a stray fence in the next, so code showed as plain text and lost indentation. Mattermost now uses the fence-aware splitter the plugin SDK already exports, the same change openclaw#196167 made for Matrix. Evidence: a real Mattermost server and both builds in Docker on node:24.22.1, replies sent with openclaw message send --channel mattermost through a running Gateway and read back over the REST API. For a 4531-character reply, main posted 3996 and 526 characters with the first post ending inside an open fence and 16 code lines shown as plain text. With this change the prose and the whole Python block arrive in balanced, indented posts under 4000 characters. A short reply with code, 9000 characters without code, and Chinese text with emoji across the split are identical on both. Details: openclaw#196331 (comment). Thanks @SunnyShu0925. Co-authored-by: Ayaan Zaidi <[email protected]>
Closes #196163
Related: #189312 (adjacent: separator handling in the shared chunker; no files in common)
What Problem This Solves
Fixes: a Matrix reply longer than 4000 characters that has a ``` code block crossing the split point arrives as broken messages: the first event ends inside an unclosed fence, the second renders the rest of the code as a paragraph (indentation lost), and the stray closing fence opens an empty code block around the trailing prose.
User Impact
User impact: long Matrix replies with code (agent final replies with streaming off, which is the default, and
openclaw message send) now split into events that each render a complete, correctly highlighted code block, and each event stays within the 4000-character limit.Why This Change Was Made
Durable finals are pre-chunked in core by the channel adapter's
chunkerbefore Matrix renders them. The Matrix adapters usedchunkTextForOutbound, which breaks on newlines and spaces with no fence awareness (chunkerMode: "markdown"only selects how the optional newline-mode pre-split behaves). Matrix's own fence-awarechunkMatrixTextnever sees the oversize text on this path, because every pre-chunk already fits. Both Matrix outbound adapters (channel.ts,outbound.ts) now use the fence-awarechunkMarkdownTextWithMode(text, limit, "length"), imported from the lightopenclaw/plugin-sdk/reply-chunkingsubpath (the same ownerchunkMarkdownTextdelegates to; Feishu uses the same call). That keeps reply dispatch out of the channel import path, so Matrix's deferred runtime loading is unchanged. It closes the fence at the end of a chunk and reopens it with the original info string in the next, inside the limit.Scope is Matrix only. Mattermost, MS Teams and Signal use the same unaware chunker on this path but need separate checks (Signal renders through its own text-style pipeline and uses
chunkerMode: "text"), so they are left as follow-ups.Evidence
Live proof against a local Synapse (127.0.0.1, SQLite, two users) through the real
openclaw message send --channel matrixwith a 4532-character reply holding a Python block across the 4000 boundary, events read back as the second user (m.room.message):main (
4b077d31b): 2 events, as described above.this branch: 3 events, each one balanced and within 4000.
Tests:
extensions/matrix/src/outbound.test.tsandchannel.message-adapter.test.tsgain fence rows (they fail on main, pass here); test-line ratchet net -1.Import boundary (follow-up commit
58ffe5f):lint:plugins:no-monolithic-plugin-sdk-entry-imports,lint:plugins:plugin-sdk-subpaths-exportedandtest:extensions:memory -- --extension matrixpass (matrix: ok). On the same 4532-character reply the new chunker output is byte-identical to the one used in the live proof above (chunks of 39, 3997 and 508 characters).Not verified: an inbound model-driven reply (it reaches the same
deliverOutboundPayloadspre-chunk path; I did not run a gateway with a fixture model), Mattermost, MS Teams, Signal, encrypted rooms.Rebased onto
151db8d09; the Matrix tests above re-run there (20 passed). None of the files involved changed between the two bases.