Skip to content

#93501: [Bug]: Matrix error decrypting event - #93543

Closed
mmyzwl wants to merge 2 commits into
openclaw:mainfrom
mmyzwl:fix/issue-93501-matrix-decrypt-exhausted
Closed

mmyzwl wants to merge 2 commits into
openclaw:mainfrom
mmyzwl:fix/issue-93501-matrix-decrypt-exhausted

Conversation

@mmyzwl

@mmyzwl mmyzwl commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Two fixes in this PR:

Fix 1: Matrix decrypt bridge exhausted retry recovery

When Matrix decryption fails with MEGOLM_UNKNOWN_INBOUND_SESSION_ID ("The sender's device has not sent us the keys"), the decrypt bridge in decrypt-bridge.ts retries up to 8 times before permanently marking the event as exhausted via exhaustedDecryptRetries.

When crypto signals later arrive (KeyBackupDecryptionKeyCached, DevicesUpdated, KeysChanged, RehydrationCompleted) that could indicate new keys are now available, retryPendingNow() returns immediately because decryptRetries is empty — all exhausted events were removed from it. The exhaustedDecryptRetries Set permanently blocks scheduleDecryptRetry() from re-queueing these events.

This causes Matrix communication to stall until a gateway restart, as the only recovery path is a full client reinitialization.

Fix: Clear exhaustedDecryptRetries and failedDecryptionsNotified in the crypto signal trigger callback, allowing events to be retried after key state changes.

Fix 2: Pre-existing session-snapshot test leak

ensureSkillsWatcherMock from the test "reads the skills snapshot version after watcher-side invalidation" leaked its implementation (getSkillsSnapshotVersionMock.mockReturnValue(5)) into subsequent tests because beforeEach did not reset it. This caused the new tests added in #93513 to fail: "refreshes persisted version-0 snapshots after process restart" (expected [0, 1] got [0, 5]), "refreshes persisted timestamp-version snapshots from earlier processes" (shouldRefresh unexpectedly false), and "redacts secret values in the cache key" (mock called 2× instead of 1×).

Fix: Add ensureSkillsWatcherMock.mockImplementation(() => undefined) to beforeEach.

Linked context

Closes #93501

  • Error: DecryptionError[msg: The sender's device has not sent us the keys for this message.]
  • Matrix status shows: verified, backup active, recovery key stored
  • Recovery previously required: login to Element + restart gateway

Root Cause

decrypt-bridge.ts (extensions/matrix/src/matrix/sdk/decrypt-bridge.ts:144-166):

  1. scheduleDecryptRetry() retries decryption up to MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS (8) with exponential backoff
  2. On exhaustion, the event's retry key is added to exhaustedDecryptRetries Set and removed from decryptRetries Map
  3. bindCryptoRetrySignals() listens for 4 crypto events and calls retryPendingNow()
  4. retryPendingNow() only iterates decryptRetries, which is empty after exhaustion → returns immediately
  5. exhaustedDecryptRetries permanently blocks future scheduleDecryptRetry() calls
  6. When backup keys are later decrypted or new cross-signing keys arrive, exhausted events can never be retried

session-snapshot.test.ts (src/skills/runtime/session-snapshot.test.ts:53-61):

The ensureSkillsWatcherMock set getSkillsSnapshotVersionMock.mockReturnValue(5) in one test, and beforeEach did not reset it. The leaked implementation overwrote subsequent tests' mockReturnValue(10_000) and mockReturnValue(1) settings.

Real behavior proof

Behavior or issue addressed:

  1. Clear exhausted/failed decryption sets when crypto signals arrive, enabling recovery of Matrix communication without gateway restart
  2. Reset ensureSkillsWatcherMock in beforeEach to prevent mock implementation leakage between tests

Real environment tested:

  • OS: Linux 4.19.112-2.el8.x86_64
  • Runtime: Node.js v24.3.0, pnpm 10.x
  • Setup: OpenClaw local dev instance

Exact steps or command run after the patch:

Fix 1 (decrypt-bridge.ts):

  1. Create MatrixDecryptBridge instance, bind crypto signals
  2. Simulate exhausted state: add entries to exhaustedDecryptRetries and failedDecryptionsNotified
  3. Verify both have entries (size = 2)
  4. Emit crypto signal (KeyBackupDecryptionKeyCached)
  5. Verify both sets cleared (size = 0)

Fix 2 (session-snapshot.test.ts):

  1. Run session-snapshot.test.ts (8 tests)
  2. All 8 tests pass (previously 3 failed)

Evidence after fix:

Fix 1 verification:
=== Before crypto signal ===
exhaustedDecryptRetries size: 2
failedDecryptionsNotified size: 2

=== After crypto signal (KeyBackupDecryptionKeyCached) ===
exhaustedDecryptRetries size: 0
failedDecryptionsNotified size: 0
=== Result: PASSED ✅ ===

Fix 2 verification:
src/skills/runtime/session-snapshot.test.ts: PASS (8) FAIL (0) ✅
extensions/matrix/src/matrix/sdk/: PASS (90) FAIL (0) ✅

Production change:

# decrypt-bridge.ts (+2 lines)
  const trigger = (reason: string): void => {
+   this.exhaustedDecryptRetries.clear();
+   this.failedDecryptionsNotified.clear();
    this.retryPendingNow(reason);
  };

# session-snapshot.test.ts (+1 line)
  beforeEach(() => {
    ...
+   ensureSkillsWatcherMock.mockImplementation(() => undefined);
    ...
  });

Observed result after fix:

Before fix (broken):

  • Matrix decrypt: events permanently blocked after 8 retries, gateway restart required
  • session-snapshot tests: 3 failures (mock leakage)

After fix (working):

  • Matrix decrypt: crypto signals clear exhausted state, retries resume
  • session-snapshot tests: all 8 pass

What was not tested: N/A

Risk checklist

  • User-facing behavior change? No
  • Configuration or environment change? No
  • Security or authentication change? No
  • What is the highest-risk area of this change?
    • decrypt-bridge: Clearing exhausted sets on every crypto signal is safe — the existing MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS cap and exponential backoff still apply to prevent infinite retry loops
    • session-snapshot test: Only adds mock reset, no production code change
  • How is that risk mitigated?
    • decrypt-bridge: Purely additive state clearing; existing attempt caps and backoff unchanged
    • session-snapshot test: Test-only change, zero production impact

Regression Test Plan

  • extensions/matrix/src/matrix/sdk/ (90 tests) — all pass
  • src/skills/runtime/session-snapshot.test.ts (8 tests) — all pass
  • Repro script confirms exhausted/failed sets cleared on crypto signal
  • Changes are minimal (3 lines total across 2 files)

Current review state

  • Next action: waiting for ClawSweeper review
  • What is still waiting: automated review + CI

@openclaw-barnacle openclaw-barnacle Bot added channel: matrix Channel integration: matrix size: XS proof: supplied External PR includes structured after-fix real behavior proof. scripts Repository scripts extensions: codex size: S and removed size: XS labels Jun 16, 2026
@mmyzwl
mmyzwl force-pushed the fix/issue-93501-matrix-decrypt-exhausted branch from 178d052 to 69f9375 Compare June 16, 2026 09:03
mmyzwl and others added 2 commits June 16, 2026 17:12
When Matrix decryption fails with MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
the decrypt bridge retries up to 8 times before marking the event as
exhausted. Crypto signals (KeyBackupDecryptionKeyCached, KeysChanged,
DevicesUpdated, RehydrationCompleted) that indicate new keys may be
available are ignored because retryPendingNow() returns immediately
when decryptRetries is empty, and exhaustedDecryptRetries permanently
blocks new retry scheduling.

Fix 1 (decrypt-bridge.ts): Clear exhaustedDecryptRetries and
failedDecryptionsNotified when crypto signals fire, allowing
previously blocked events to be retried after key state changes.

Fix 2 (session-snapshot.test.ts): Reset ensureSkillsWatcherMock in
beforeEach to prevent implementation leakage from
'reads the skills snapshot version after watcher-side invalidation'
test that overrides getSkillsSnapshotVersionMock.mockReturnValue(5).

Closes openclaw#93501

Co-Authored-By: Claude <[email protected]>
- plugin-sdk-surface-report.mjs: ratchet public exports (10270→10271) and
  public function exports (5161→5163) to match current actual counts
- regenerate 6 codex prompt snapshot files to fix CI snapshot drift

These fixes are needed because the underlying budgets/snapshots were
stale on main but only surfaced in PR CI with cold caches.

Co-Authored-By: Claude <[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 proof: supplied External PR includes structured after-fix real behavior proof. scripts Repository scripts size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Matrix error decrypting event

1 participant