Skip to content

fix: stop answering old Matrix messages again 30 days after they were handled - #208571

Merged
obviyus merged 1 commit into
mainfrom
fix/matrix-cached-replay-expiry
Nov 29, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/matrix-cached-replay-expiry

Conversation

@obviyus

@obviyus obviyus commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Root cause: extensions/matrix/src/matrix/client/file-sync-store.ts:118-120 replays the whole saved Matrix sync cache into ingress on every client start (Gateway restart, or the Matrix channel restart that a room join's config write triggers); extensions/matrix/src/matrix/monitor/ingress.ts:136-142 only marks first-login events as history, so cached events are admitted as deliverable; their only guard is the event-ID replay tombstone (src/channels/message/ingress-queue.ts:358-376), which shared retention prunes after 30 days (src/channels/message/ingress-monitor-owner.ts:16-17), so the next restart answers month-old messages as new turns.

Fixes #191476 (the replay of old inbound reported on 2026.11.12). The closed-consumer failure from the original report was fixed by #201554.

What Problem This Solves

After a Gateway restart or a Matrix room join, OpenClaw answered Matrix messages that were 30 to 60 days old as if they had just arrived. It only happened in quiet rooms whose last message was at least 30 days old. In the report, 32 replies went to stale messages over two days, the same two images were answered four times over two months, and a month-old instruction rewrote a spreadsheet.

User Impact

Restarts and room joins no longer revive Matrix messages that were already handled. Messages that arrived but were not yet answered when the Gateway stopped are still answered once after restart, and messages that arrive while the Gateway is down are still answered. No action or migration is needed.

Why This Change Was Made

Matrix stores each sync batch in durable ingress before saving its cursor, so everything in the saved sync cache was already admitted. Replaying the cache on startup is meant to find those existing rows (pending ones keep their delivery). Once a completed row's 30-day replay guard is pruned, the replay was creating a brand-new deliverable row instead. The plugin now classifies cached replay the way it already classifies first-login history: an existing row is left as is, and an event without a row is recorded as intentionally not delivered. Shared retention, storage and the ingress queue are unchanged.

Evidence

Isolated Gateway from a built checkout, local tuwunel homeserver, deterministic OpenAI-compatible model fixture. The Gateway answers a message in two rooms, then restarts with only its own clock moved forward 31 days (so the hourly prune removes the replay guards), then gets a channels.matrix.groups config change, which restarts the Matrix channel like a room join does.

Run After the channel restart
main, +31 days both 31-day-old events dispatched again (message processed ... messageId=<old event>), and the bot posted a new message in each quiet room
main, no clock change nothing dispatched
this PR, +31 days channel restarted, nothing dispatched, no new bot messages

On main the replayed turn in this setup ends with Input completion retry conflicts with the committed input, because the same session still holds the original input, and the room gets "⚠️ OpenClaw couldn't finish this request". The reporter's month-old sessions produced full fresh replies instead.

Safety checks, same result on main and this PR:

  • A message queued behind a running turn when the Gateway was killed with SIGKILL (its ingress row was claimed) is answered exactly once after restart.
  • A message sent while the Gateway was down is answered exactly once after restart.

Regression test handler.restart.test.ts "does not answer a cached message again after its replay tombstone expires" (real sync store, SQLite ingress queue) fails on main with $unadopted dispatched twice and passes here.

Thanks @hartmark for the report, and @7s9ffcyjq5-lgtm for the detailed 2026.11.12 replay timeline.

Merge danger: two-way door. A revert restores the old classification; no stored state, schema or config changes. Blast radius: Matrix inbound only, at client start.

Observed proof lines: #208571 (comment)

Cached sync is admitted before its cursor is saved, so bind-time replay only finds existing ingress rows. Once the 30-day replay tombstone is pruned, the replay re-admitted month-old events as new turns on every Gateway or channel restart. Classify cached replay as history.
@clawsweeper

clawsweeper Bot commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added channel: matrix Channel integration: matrix size: S labels Nov 29, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. labels Nov 29, 2026
@obviyus

obviyus commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Observed proof lines (isolated Gateway, built checkout, local tuwunel homeserver, deterministic model fixture; only the Gateway process clock shifted):

== base-31d
== phase1-answered 19:22:37Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase2-after-restart 19:23:17Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase3-after-channel-reload 19:23:54Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
[gateway/channels] restarting matrix channel
[matrix] [default] starting account (reason: config-reload)
[diagnostic] message processed: channel=matrix chatId=room:!-glFnJ0ZkozDPFeW57Jl471IEtjVGHqRmJ0wAef8iZs messageId=$qL4euAEtHv-c-gzajhjIyYb4ZI1yhnLcSY6vCSBKkYI sessionId=a6f3038c-df9e-4cbf-a788-2c70339
[diagnostic] message processed: channel=matrix chatId=room:!2cbnuTx34zf-oq8FpEPxPIDox3iP6W6XtS7o-CITxfs messageId=$6wsgqNThLvkZOaNmcc0op4EjRXZt1QLJNFPSNrCGbmA sessionId=109a7578-c32b-455a-8b84-88959a9
{"ts":"2026-11-29T19:22:33.858Z","sender":"bot","id":"$2IbmfIuDrDUYW7D4A_rLJlNEH8r889rm7T02UNkdRJs","body":"P191476-REPLY to: P191476-MSG-old"}
{"ts":"2026-11-29T19:23:25.744Z","sender":"bot","id":"$hch7-UeTEo-2JmEENyeC5hTU_6VYYBPCvSMxzEXgTFY","body":"⚠️ OpenClaw couldn't finish this request. Check 
{"ts":"2026-11-29T19:22:33.875Z","sender":"bot","id":"$y0nL-Cr6EugnRzFdxbswoIlutheWALjBYUQl182LAFk","body":"P191476-REPLY to: P191476-MSG-live"}
{"ts":"2026-11-29T19:23:25.523Z","sender":"bot","id":"$9T-M9JXXMMP3altZc6InhpKE2CFffEm-s7EgoY_jEz0","body":"⚠️ OpenClaw couldn't finish this request. Check 
== base-control
== phase1-answered 19:26:59Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase2-after-restart 19:28:37Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase3-after-channel-reload 19:29:11Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
[gateway/channels] restarting matrix channel
[matrix] [default] starting account (reason: config-reload)
{"ts":"2026-11-29T19:26:55.336Z","sender":"bot","id":"$s-1GJJihmJHHKyNa9uWLQCWi7Mb-9zZs9lPUwAAokbM","body":"P191476-REPLY to: P191476-MSG-old"}
{"ts":"2026-11-29T19:26:55.376Z","sender":"bot","id":"$oz6mb-eQyUY7bn7I2reM185nuzFIY6cGEiLR6DmtxTM","body":"P191476-REPLY to: P191476-MSG-live"}
== cand-31d
== phase1-answered 19:37:27Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase2-after-restart 19:38:00Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
== phase3-after-channel-reload 19:38:33Z
replies dormant(MSG-old)=1 live(MSG-live)=1
model requests so far=2
[gateway/channels] restarting matrix channel
[matrix] [default] starting account (reason: config-reload)
{"ts":"2026-11-29T19:37:23.363Z","sender":"bot","id":"$VRFuNbjw3nmG2-_njCBirObSykPiHPpm1I-YXFho5qI","body":"P191476-REPLY to: P191476-MSG-old"}
{"ts":"2026-11-29T19:37:23.347Z","sender":"bot","id":"$3jI5NgeTL119YHHKyasCIN2ftkzi0th7mIGTgCUS_sY","body":"P191476-REPLY to: P191476-MSG-live"}
== base-safety
== phase1-crashed (SIGKILL while MSG-hang turn in flight and MSG-queued waiting)
MSG-queued event=$iKJyu9VZhwSbd0OkECy8TcHJcdKB_wAxdGDK0ZB77Js
ingress rows:
$iKJyu9VZhwSbd0OkECy8TcHJcdKB_wAxdGDK0ZB77Js|claimed
== phase1 19:30:10Z
replies dormant(MSG-old)=1 live(MSG-live)=0
model requests so far=2
MSG-down sent while Gateway was down event=$3h_6mJ-6ZzsFb3tHnqmlBjmAIpEbT-9YocLWxG_LxqI
== phase2-after-restart
replies MSG-old=1 MSG-down=1 MSG-hang=1 MSG-queued=1
ingress rows:
== cand-safety
== phase1-crashed (SIGKILL while MSG-hang turn in flight and MSG-queued waiting)
MSG-queued event=$wVN9TdyGGQ0x8DTMVn6W55TWRUMIwfCO-J2ZU2z4Ykc
ingress rows:
$wVN9TdyGGQ0x8DTMVn6W55TWRUMIwfCO-J2ZU2z4Ykc|claimed
== phase1 19:40:08Z
replies dormant(MSG-old)=1 live(MSG-live)=0
model requests so far=2
MSG-down sent while Gateway was down event=$p7yjRNFuZ9ONnSUEvFKSCDLkL3MlvuidOmElmrBHtfw
== phase2-after-restart
replies MSG-old=1 MSG-down=1 MSG-hang=1 MSG-queued=1
ingress rows:

base 207f869af221 (origin/main; same files as v2026.11.12) + new test only
ocx test extensions/matrix/src/matrix/monitor/handler.restart.test.ts  (tier: vitest-source)
 x does not answer a cached message again after its replay tombstone expires
   expected [ '$unadopted', '$fresh' ], received [ '$unadopted', '$unadopted', '$fresh' ]
 Tests 1 failed | 3 passed (4)

@obviyus
obviyus merged commit d692ddc into main Nov 29, 2026
129 of 131 checks passed
@obviyus

obviyus commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Landed in d692ddc. Thanks @hartmark for the dated reports that made this traceable. After a Gateway restart or a Matrix channel restart (for example from a room join), the plugin no longer answers cached Matrix messages again once their handled-message records have expired after 30 days: cached replay is treated as history. Messages that were mid-turn during a crash and messages sent while the Gateway was down are still answered exactly once.

@obviyus
obviyus deleted the fix/matrix-cached-replay-expiry branch November 29, 2026 20:27
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
… handled (#208571)

Root cause: `extensions/matrix/src/matrix/client/file-sync-store.ts:118-120` replays the whole saved Matrix sync cache into ingress on every client start (Gateway restart, or the Matrix channel restart that a room join's config write triggers); `extensions/matrix/src/matrix/monitor/ingress.ts:136-142` only marks first-login events as history, so cached events are admitted as deliverable; their only guard is the event-ID replay tombstone (`src/channels/message/ingress-queue.ts:358-376`), which shared retention prunes after 30 days (`src/channels/message/ingress-monitor-owner.ts:16-17`), so the next restart answers month-old messages as new turns.

Fixes #191476 (the replay of old inbound reported on 2026.11.12). The closed-consumer failure from the original report was fixed by #201554.

## What Problem This Solves

After a Gateway restart or a Matrix room join, OpenClaw answered Matrix messages that were 30 to 60 days old as if they had just arrived. It only happened in quiet rooms whose last message was at least 30 days old. In the report, 32 replies went to stale messages over two days, the same two images were answered four times over two months, and a month-old instruction rewrote a spreadsheet.

## User Impact

Restarts and room joins no longer revive Matrix messages that were already handled. Messages that arrived but were not yet answered when the Gateway stopped are still answered once after restart, and messages that arrive while the Gateway is down are still answered. No action or migration is needed.

## Why This Change Was Made

Matrix stores each sync batch in durable ingress before saving its cursor, so everything in the saved sync cache was already admitted. Replaying the cache on startup is meant to find those existing rows (pending ones keep their delivery). Once a completed row's 30-day replay guard is pruned, the replay was creating a brand-new deliverable row instead. The plugin now classifies cached replay the way it already classifies first-login history: an existing row is left as is, and an event without a row is recorded as intentionally not delivered. Shared retention, storage and the ingress queue are unchanged.

## Evidence

Isolated Gateway from a built checkout, local tuwunel homeserver, deterministic OpenAI-compatible model fixture. The Gateway answers a message in two rooms, then restarts with only its own clock moved forward 31 days (so the hourly prune removes the replay guards), then gets a `channels.matrix.groups` config change, which restarts the Matrix channel like a room join does.

| Run | After the channel restart |
|---|---|
| main, +31 days | both 31-day-old events dispatched again (`message processed ... messageId=<old event>`), and the bot posted a new message in each quiet room |
| main, no clock change | nothing dispatched |
| this PR, +31 days | channel restarted, nothing dispatched, no new bot messages |

On main the replayed turn in this setup ends with `Input completion retry conflicts with the committed input`, because the same session still holds the original input, and the room gets "⚠️ OpenClaw couldn't finish this request". The reporter's month-old sessions produced full fresh replies instead.

Safety checks, same result on main and this PR:
- A message queued behind a running turn when the Gateway was killed with SIGKILL (its ingress row was `claimed`) is answered exactly once after restart.
- A message sent while the Gateway was down is answered exactly once after restart.

Regression test `handler.restart.test.ts` "does not answer a cached message again after its replay tombstone expires" (real sync store, SQLite ingress queue) fails on main with `$unadopted` dispatched twice and passes here.

Thanks @hartmark for the report, and @7s9ffcyjq5-lgtm for the detailed 2026.11.12 replay timeline.

Merge danger: two-way door. A revert restores the old classification; no stored state, schema or config changes. Blast radius: Matrix inbound only, at client start.

Observed proof lines: #208571 (comment)

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit d692ddc)

Follow-up audit: No later same-path, PR-number or SHA references through be407a4.

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
… handled (#208571)

Root cause: `extensions/matrix/src/matrix/client/file-sync-store.ts:118-120` replays the whole saved Matrix sync cache into ingress on every client start (Gateway restart, or the Matrix channel restart that a room join's config write triggers); `extensions/matrix/src/matrix/monitor/ingress.ts:136-142` only marks first-login events as history, so cached events are admitted as deliverable; their only guard is the event-ID replay tombstone (`src/channels/message/ingress-queue.ts:358-376`), which shared retention prunes after 30 days (`src/channels/message/ingress-monitor-owner.ts:16-17`), so the next restart answers month-old messages as new turns.

Fixes #191476 (the replay of old inbound reported on 2026.11.12). The closed-consumer failure from the original report was fixed by #201554.

## What Problem This Solves

After a Gateway restart or a Matrix room join, OpenClaw answered Matrix messages that were 30 to 60 days old as if they had just arrived. It only happened in quiet rooms whose last message was at least 30 days old. In the report, 32 replies went to stale messages over two days, the same two images were answered four times over two months, and a month-old instruction rewrote a spreadsheet.

## User Impact

Restarts and room joins no longer revive Matrix messages that were already handled. Messages that arrived but were not yet answered when the Gateway stopped are still answered once after restart, and messages that arrive while the Gateway is down are still answered. No action or migration is needed.

## Why This Change Was Made

Matrix stores each sync batch in durable ingress before saving its cursor, so everything in the saved sync cache was already admitted. Replaying the cache on startup is meant to find those existing rows (pending ones keep their delivery). Once a completed row's 30-day replay guard is pruned, the replay was creating a brand-new deliverable row instead. The plugin now classifies cached replay the way it already classifies first-login history: an existing row is left as is, and an event without a row is recorded as intentionally not delivered. Shared retention, storage and the ingress queue are unchanged.

## Evidence

Isolated Gateway from a built checkout, local tuwunel homeserver, deterministic OpenAI-compatible model fixture. The Gateway answers a message in two rooms, then restarts with only its own clock moved forward 31 days (so the hourly prune removes the replay guards), then gets a `channels.matrix.groups` config change, which restarts the Matrix channel like a room join does.

| Run | After the channel restart |
|---|---|
| main, +31 days | both 31-day-old events dispatched again (`message processed ... messageId=<old event>`), and the bot posted a new message in each quiet room |
| main, no clock change | nothing dispatched |
| this PR, +31 days | channel restarted, nothing dispatched, no new bot messages |

On main the replayed turn in this setup ends with `Input completion retry conflicts with the committed input`, because the same session still holds the original input, and the room gets "⚠️ OpenClaw couldn't finish this request". The reporter's month-old sessions produced full fresh replies instead.

Safety checks, same result on main and this PR:
- A message queued behind a running turn when the Gateway was killed with SIGKILL (its ingress row was `claimed`) is answered exactly once after restart.
- A message sent while the Gateway was down is answered exactly once after restart.

Regression test `handler.restart.test.ts` "does not answer a cached message again after its replay tombstone expires" (real sync store, SQLite ingress queue) fails on main with `$unadopted` dispatched twice and passes here.

Thanks @hartmark for the report, and @7s9ffcyjq5-lgtm for the detailed 2026.11.12 replay timeline.

Merge danger: two-way door. A revert restores the old classification; no stored state, schema or config changes. Blast radius: Matrix inbound only, at client start.

Observed proof lines: #208571 (comment)

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit d692ddc)

Co-authored-by: RomneyDa <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 30, 2026
… handled (openclaw#208571)

Root cause: `extensions/matrix/src/matrix/client/file-sync-store.ts:118-120` replays the whole saved Matrix sync cache into ingress on every client start (Gateway restart, or the Matrix channel restart that a room join's config write triggers); `extensions/matrix/src/matrix/monitor/ingress.ts:136-142` only marks first-login events as history, so cached events are admitted as deliverable; their only guard is the event-ID replay tombstone (`src/channels/message/ingress-queue.ts:358-376`), which shared retention prunes after 30 days (`src/channels/message/ingress-monitor-owner.ts:16-17`), so the next restart answers month-old messages as new turns.

Fixes openclaw#191476 (the replay of old inbound reported on 2026.11.12). The closed-consumer failure from the original report was fixed by openclaw#201554.

## What Problem This Solves

After a Gateway restart or a Matrix room join, OpenClaw answered Matrix messages that were 30 to 60 days old as if they had just arrived. It only happened in quiet rooms whose last message was at least 30 days old. In the report, 32 replies went to stale messages over two days, the same two images were answered four times over two months, and a month-old instruction rewrote a spreadsheet.

## User Impact

Restarts and room joins no longer revive Matrix messages that were already handled. Messages that arrived but were not yet answered when the Gateway stopped are still answered once after restart, and messages that arrive while the Gateway is down are still answered. No action or migration is needed.

## Why This Change Was Made

Matrix stores each sync batch in durable ingress before saving its cursor, so everything in the saved sync cache was already admitted. Replaying the cache on startup is meant to find those existing rows (pending ones keep their delivery). Once a completed row's 30-day replay guard is pruned, the replay was creating a brand-new deliverable row instead. The plugin now classifies cached replay the way it already classifies first-login history: an existing row is left as is, and an event without a row is recorded as intentionally not delivered. Shared retention, storage and the ingress queue are unchanged.

## Evidence

Isolated Gateway from a built checkout, local tuwunel homeserver, deterministic OpenAI-compatible model fixture. The Gateway answers a message in two rooms, then restarts with only its own clock moved forward 31 days (so the hourly prune removes the replay guards), then gets a `channels.matrix.groups` config change, which restarts the Matrix channel like a room join does.

| Run | After the channel restart |
|---|---|
| main, +31 days | both 31-day-old events dispatched again (`message processed ... messageId=<old event>`), and the bot posted a new message in each quiet room |
| main, no clock change | nothing dispatched |
| this PR, +31 days | channel restarted, nothing dispatched, no new bot messages |

On main the replayed turn in this setup ends with `Input completion retry conflicts with the committed input`, because the same session still holds the original input, and the room gets "⚠️ OpenClaw couldn't finish this request". The reporter's month-old sessions produced full fresh replies instead.

Safety checks, same result on main and this PR:
- A message queued behind a running turn when the Gateway was killed with SIGKILL (its ingress row was `claimed`) is answered exactly once after restart.
- A message sent while the Gateway was down is answered exactly once after restart.

Regression test `handler.restart.test.ts` "does not answer a cached message again after its replay tombstone expires" (real sync store, SQLite ingress queue) fails on main with `$unadopted` dispatched twice and passes here.

Thanks @hartmark for the report, and @7s9ffcyjq5-lgtm for the detailed 2026.11.12 replay timeline.

Merge danger: two-way door. A revert restores the old classification; no stored state, schema or config changes. Blast radius: Matrix inbound only, at client start.

Observed proof lines: openclaw#208571 (comment)

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

Labels

channel: matrix Channel integration: matrix impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. P1 High-priority user-facing bug, regression, or broken workflow. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Matrix room stuck on "Plugin matrix consumer is closed" after image_generate completion run races a new inbound message

1 participant