Repository navigation
feat(matrix): add draft streaming (edit-in-place partial replies) - #56387
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8b36fc7b66
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Greptile SummaryThis PR adds edit-in-place streaming ( Key findings from the review:
Confidence Score: 5/5Safe to merge; both remaining findings are non-blocking style/efficiency improvements. All findings are P2. The missing formatted_body prefix affects only clients that don't support MSC2676 edits (a minority), and the redundant final edit is a benign extra API call rather than a functional defect. Core streaming logic, error recovery, multi-block handling, and test coverage are solid. extensions/matrix/src/matrix/send.ts (formatted_body prefix) and extensions/matrix/src/matrix/monitor/handler.ts (redundant final edit) are worth revisiting before the rate-limit behavior review is completed.
|
| Filename | Overview |
|---|---|
| extensions/matrix/src/matrix/send.ts | Adds editMessageMatrix implementing MSC2676 m.replace edits. The body fallback correctly carries the * prefix but formatted_body does not, which is a minor spec compliance gap for legacy clients. |
| extensions/matrix/src/matrix/draft-stream.ts | New coalescing state machine for edit-in-place streaming. Throttle, no-op dedup, error recovery, and reset-for-next-block logic are all sound. Unit test coverage is thorough. |
| extensions/matrix/src/matrix/monitor/handler.ts | Wires draft stream into the reply dispatcher. The materializedTextLength tracking and per-block reset logic are correct, but the deliver callback makes a redundant final edit after stop() already flushed the latest draft text, risking rate limit double-spend. |
| extensions/matrix/src/matrix/monitor/index.ts | Resolves streaming config from account settings and passes it to the handler. Normalises true → partial and false/off → off correctly. |
| extensions/matrix/src/types.ts | Adds streaming field to MatrixConfig with a clear JSDoc comment and boolean compat mapping. No issues. |
| extensions/matrix/src/matrix/draft-stream.test.ts | Ten focused unit tests covering send, edit, coalescing, throttle, stop, reset, and error paths. Good fake-timer usage for throttle assertions. |
| extensions/matrix/src/matrix/monitor/handler.test.ts | Adds streaming: off to an existing test fixture to satisfy the updated handler params type. No functional changes. |
| extensions/matrix/src/matrix/monitor/handler.test-helpers.ts | Adds streaming to the test harness options and wires it through with a sensible off default. Clean addition. |
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/send.ts
Line: 300-305
Comment:
**`formatted_body` missing `* ` fallback prefix**
Per [MSC2676](https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/2676-message-editing.md), the _outer_ event (the fallback seen by clients that don't understand `m.replace`) should prefix **both** `body` and `formatted_body` with `* `. Right now `body` is correctly set to `* ${convertedText}`, but the `formatted_body` from the spread of `newContent` is left as the rendered HTML without any prefix, so HTML-capable legacy clients will display the raw HTML without the visual edit indicator.
```suggestion
const content: Record<string, unknown> = {
...newContent,
body: `* ${convertedText}`,
...(newContent.formatted_body
? { formatted_body: `* ${newContent.formatted_body}` }
: {}),
"m.new_content": newContent,
"m.relates_to": replaceRelation,
};
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/handler.ts
Line: 937-946
Comment:
**Redundant final edit may double-consume rate limit budget**
`draftStream.stop()` (line 937) calls `loop.flush()` which drains any pending text and sends/edits the message. Immediately after, `editMessageMatrix` is called again with `payload.text`. If `payload.text` equals the text that was just flushed (which is the common case when `onPartialReply` has already received the final token), this results in two consecutive edits with identical content.
Synapse's default rate limit is effectively 1 edit per second. With the PR still marking rate-limit review as outstanding, this redundant call can push the stream over budget — especially at the end of a block that was actively throttled (the flush fires the queued edit AND the explicit final edit land close together).
If the intent is to guarantee the clean final version is sent (in case streaming text differs from `deliver`'s post-processed text), consider skipping the explicit edit when the texts match:
```typescript
const finalText = payload.text;
// stop() already flushes the last draft text; only re-edit if
// the final payload text differs from what was streamed.
if (draftEventId && finalText && !hasMedia && finalText !== draftStream.lastSentText()) {
await editMessageMatrix(roomId, draftEventId, finalText, { ... });
}
```
(Alternatively, expose a `lastSentText` accessor from `MatrixDraftStream` and skip the edit when they match.)
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat(matrix): add draft streaming (edit-..." | Re-trigger Greptile
f3c18c5 to
14db266
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 14db266744
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
14db266 to
e259d0b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e259d0bf1d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
e259d0b to
07ba45d
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 07ba45d0d7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
07ba45d to
1e81553
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1e815531dc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
1e81553 to
d98497b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d98497b33a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
d98497b to
a577d34
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a577d3435a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
b5d7a05 to
687a429
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b5d7a05754
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
687a429 to
c1b3c38
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c1b3c38764
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
c1b3c38 to
df10ca2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df10ca2137
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
df10ca2 to
c15a7d2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c15a7d2ddd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
c15a7d2 to
fa8a8a8
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa8a8a872f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
New function that edits an existing Matrix event using MSC2676 m.replace relations. Supports thread-aware edits and HTML formatting via buildTextContent. No callers yet. Co-Authored-By: Claude claude-opus-4-6 <[email protected]> Signed-off-by: Jakub Rusz <[email protected]>
Introduce createMatrixDraftStream — a throttled state machine that manages edit-in-place streaming for Matrix. Uses the shared createDraftStreamLoop from plugin-sdk with a 1000ms throttle. Handles send/edit failures gracefully by stopping the stream and letting the deliver callback fall through to normal delivery. Includes preserveReplyId option for replyToMode "all" so reset() restores the original replyToId instead of clearing it. 10 unit tests covering create, throttle, dedup, flush, stop, reset, error handling, and preserveReplyId behavior. Co-Authored-By: Claude claude-opus-4-6 <[email protected]> Signed-off-by: Jakub Rusz <[email protected]>
Integrate createMatrixDraftStream into the Matrix message handler when streaming: "partial" is configured. - Resolve streaming config in monitor/index.ts - Wire onPartialReply to feed accumulated text into draft stream, using materializedTextLength offset to isolate per-block text - Wire onAssistantMessageStart to reset offsets on message boundaries so post-tool blocks stream correctly - Final delivery: edit draft with final text, fall back to deliverMatrixReplies on edit failure - Disable block streaming when draft streaming is active (draft streaming replaces it with edit-in-place updates) - Only reset draft stream on block delivery, not final (prevents ghost messages from late async callbacks) - Add finally block to stop draft stream on handler exit (prevents timer leaks on model timeout/error) - Honor payload.replyToId: detect mismatch with draft's reply target and fall through to deliverMatrixReplies (Matrix edits cannot change reply relations); skip check when replyToMode is "off" - Apply mismatch guard to both text-only and media branches 5 handler-level integration tests covering edit failure fallback, post-final behavior, message boundary reset, compaction notice bypass, and cleanup on handler error. Co-Authored-By: Claude claude-opus-4-6 <[email protected]> Signed-off-by: Jakub Rusz <[email protected]>
62413db to
53e566b
Compare
|
Merged via squash.
Thanks @jrusz! |
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
- Append 🧵<threadRootId> to session key when a Matrix thread root is present, giving each thread its own isolated session (mirrors Mattermost replyToMode=all behavior) - Add dm.threadReplies config option to override threadReplies for DMs independently of group rooms - Update handler tests to expect thread-scoped session keys - Add route.test.ts coverage for thread session key isolation Streaming support was already merged upstream in PR openclaw#56387.
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…enclaw#56387) Merged via squash. Prepared head SHA: 53e566b Co-authored-by: jrusz <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
Summary
Adds a
streaming: "partial"config option for the Matrix plugin that edits a single message in place as the model generates text, matching Telegram's streaming UX. When active, block streaming is automatically disabled.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause / Regression History (if applicable)
N/A — new feature.
Regression Test Plan (if applicable)
N/A — new feature. Coverage:
createMatrixDraftStreamImplementation
Split into 4 commits:
Config schema prep — Add
streamingfield toMatrixConfigtype andMatrixConfigSchemaso the field survives config validation. Regenerate baselines.editMessageMatrix— New send helper using Matrixm.replacerelation (MSC2676), thread-aware for Synapse compatibility.createMatrixDraftStream— Coalescing state machine built on the sharedcreateDraftStreamLoopfromopenclaw/plugin-sdk/channel-lifecyclewith 1000ms throttle (matching Telegram/Slack). Stops on send/edit failure. SupportspreserveReplyIdforreplyToMode: "all".Handler wiring —
onPartialReplyfeeds incremental text to the draft stream usingmaterializedTextLengthoffset to isolate per-block text.delivercallback finalizes via edit (text-only) or falls back todeliverMatrixReplies(media/failure). Additional hardening:finallyblock stops draft stream on handler exit (prevents timer leaks on model timeout/error)onAssistantMessageStartresets text offsets so post-tool blocks stream correctlypayload.replyToIdmismatch detection: falls through todeliverMatrixReplieswhen the payload wants a different reply target (Matrix edits can't change reply relations)replyToMode: "off"(replies stripped anyway) orthreadTargetis set (thread relations take precedence)Configuration
{ "channels": { "matrix": { "streaming": "partial" // or true, "off", false (default: "off") } } }Per-account configuration is also supported under
channels.matrix.accounts.<id>.streaming.User-visible / Behavior Changes
streamingconfig field for Matrix channel (default: off, no change for existing users)Diagram (if applicable)
Security Impact (required)
editMessageMatrixsends m.replace events to the Matrix homeserver (same auth as existing sends)Repro + Verification
Environment
glm-5:cloud(125K context)streaming: "partial",allowPrivateNetwork: trueSteps
channels.matrix.streamingto"partial"in configExpected
Actual
Evidence
Human Verification (required)
Review Conversations
Compatibility / Migration
streamingfieldRisks and Mitigations
🤖 Generated with Claude Code