Skip to content

fix(mattermost): cap opaque target cache - #103258

Merged
steipete merged 6 commits into
openclaw:mainfrom
mikasa0818:fix/mattermost-target-cache-cap
Jul 10, 2026
Merged

steipete merged 6 commits into
openclaw:mainfrom
mikasa0818:fix/mattermost-target-cache-cap

Conversation

@mikasa0818

Copy link
Copy Markdown
Contributor

What Problem This Solves

  • Fixes an issue where users running long-lived Mattermost bots would retain every distinct opaque 26-character target ID resolved during the process lifetime.
  • Affected surface / workflow: Mattermost outbound target resolution through resolveMattermostOpaqueTarget.
  • Root cause, in product terms: the process-level mattermostOpaqueTargetCache stored positive and negative results by baseUrl::token::id in an unbounded Map with no maximum size, eviction, or expiry.
  • Why it matters / User impact: high-cardinality routing could make retained heap grow continuously until the process restarted.

Why This Change Was Made

  • Fix classification: Root cause fix.
  • Complete shipped solution: cap the opaque target cache at 1024 entries and prune the oldest insertion-order entry synchronously after every positive or negative cache write.
  • Why this is root-cause fix: the broken invariant is that process-local cache retention must have a finite upper bound. Enforcing the cap synchronously at the only two write sites restores that invariant at the cache owner; no caller workaround, periodic cleanup, or public testing API is added.
  • Key design boundary / source of truth: reuse the existing pruneMapToMaxSize helper from openclaw/plugin-sdk/collection-runtime, whose contract preserves the newest inserted entries. Reads intentionally do not refresh recency, so this remains a simple insertion-order cache rather than introducing a new LRU abstraction.
  • Related open PR scan: fresh search found broader overlapping cache work in fix(mattermost): cap outbound and monitor resource caches #101740 and outbound-only work in fix(mattermost): cap outbound target caches to prevent unbounded memory growth #101800. Current origin/main still has the unbounded opaque target map. This PR intentionally isolates the opaque resolution root cause and provides live HTTP proof for that path.
  • Out of scope: Mattermost monitor caches and outbound name/DM caches covered by the broader proposals; target syntax, cache keys, user-versus-channel classification, non-404 error behavior, account resolution, request concurrency behavior, TTL, and configuration.
  • Risk labels considered: merge-risk: availability; merge-risk: security-boundary was considered but is not applicable because the existing SSRF policy, authentication header, token-scoped key, and network request path are unchanged.
  • Risk explanation: eviction can add a user-lookup request when a target older than the newest 1024 entries is used again. There is no loss of persisted state and no classification change while an entry is retained.
  • Why acceptable: the extra request is the same pre-cache resolution behavior, occurs only after overflow, and trades bounded memory for occasional re-resolution. Synchronous pruning after each write means the map cannot remain above the cap between asynchronous resolutions.
  • Compatibility, merge-risk, or maintainer-decision notes: no public API, config, schema, protocol, default, or stored-data contract changes.

User Impact

  • Users/operators/developers can now: run high-cardinality Mattermost target routing without this process cache growing beyond 1024 entries.
  • Existing behavior preserved: both successful user resolutions and 404 channel fallbacks remain cached while retained; repeated cached lookups avoid HTTP requests.
  • What was not tested or remains unavailable: no external Mattermost deployment was required; the live proof used a local Mattermost-compatible HTTP endpoint and the production client/target-resolution request path.

Evidence

  • Environment: Windows Git Bash, Node 24.18.0, local Mattermost-compatible HTTP server bound to loopback with the existing private-network opt-in.
  • Commands run after this patch: focused Mattermost Vitest through scripts/run-vitest.mjs; extension production and test tsgo; scoped run-oxlint.mjs; oxfmt --check; and node --import tsx running a local HTTP server while importing the real target-resolution module.
  • Key results / observations: focused test file passed 5/5. Production and test typechecks passed. Lint reported 0 warnings and 0 errors. Formatting passed.
  • Live behavior proof: the endpoint handled /api/v4/users/:id through the real createMattermostClient and fetchMattermostUser path. After resolving 1025 unique IDs against a 1024-entry cap, the run observed requests_after_fill=1025. A recent read of the earliest cached negative result still had 1 request before overflow, confirming the cache hit did not refresh insertion order. The newest ID remained cached at 1 request, while revisiting the earliest ID increased its request count from 1 to 2; total requests increased to 1026. This demonstrates bounded insertion-order eviction without exposing cache internals.
  • Review findings addressed, if any: none on this branch. The proof directly addresses the real-behavior gap identified on overlapping PR fix(mattermost): cap outbound and monitor resource caches #101740.
  • Regression test plan: exercise a cached positive result, a cached 404 negative result, fill the cache to 1024 entries, recently read the oldest entry without refreshing insertion order, insert the 1025th key, then verify the oldest key is requested again while the newest remains cached.
  • Regression guardrail: tests cover positive caching, negative 404 caching, overflow eviction, insertion-order semantics after a recent read, and retention of the newest entry.
  • Patch quality notes: two-file scoped diff; no dependencies, config, public API, generated files, or lockfiles changed.
  • Maintainer-ready confidence: high; current origin/main still contained the unbounded map at submission-time verification, and the local HTTP proof exercised the production network path.

@openclaw-barnacle openclaw-barnacle Bot added channel: mattermost Channel integration: mattermost size: S labels Jul 10, 2026
@steipete steipete self-assigned this Jul 10, 2026
@steipete
steipete requested a review from a team as a code owner July 10, 2026 04:17
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: discord Channel integration: discord channel: matrix Channel integration: matrix channel: slack Channel integration: slack channel: telegram Channel integration: telegram channel: voice-call Channel integration: voice-call app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui gateway Gateway runtime cli CLI command changes scripts Repository scripts commands Command implementations docker Docker and sandbox tooling agents Agent runtime and tooling channel: feishu Channel integration: feishu extensions: openai extensions: deepseek extensions: codex plugin: google-meet extensions: google extensions: xai extensions: clawrouter Extension: clawrouter labels Jul 10, 2026
@openclaw-barnacle openclaw-barnacle Bot added size: S and removed app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui gateway Gateway runtime extensions: memory-core Extension: memory-core cli CLI command changes scripts Repository scripts commands Command implementations docker Docker and sandbox tooling agents Agent runtime and tooling channel: feishu Channel integration: feishu extensions: openai extensions: deepseek extensions: memory-wiki extensions: codex plugin: google-meet extensions: google extensions: xai extensions: clawrouter Extension: clawrouter size: XL labels Jul 10, 2026
@steipete

Copy link
Copy Markdown
Contributor

Land-ready maintainer pass complete.

  • Kept the 1,024-entry bound at the Mattermost opaque-target cache owner and refactored cached booleans into the existing closed user | channel resolution kind.
  • Replaced the oversized overflow harness with focused oldest-eviction, read-without-refresh, and newest-retention assertions; reviewed positive and negative cache writes plus routing callers.
  • Exact prepared head e7098c9ae8b58afdbc5ba63c06e4e0b21cf46b3a passed full hosted CI: https://github.com/openclaw/openclaw/actions/runs/29070257726
  • Fresh autoreview, review artifacts, and OPENCLAW_TESTBOX=1 scripts/pr prepare-run 103258 passed.

Merging with contributor credit preserved. Thanks @mikasa0818.

@steipete
steipete merged commit 20e1368 into openclaw:main Jul 10, 2026
110 of 118 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 10, 2026
* fix(mattermost): cap opaque target cache

* refactor(mattermost): clarify bounded target cache

* chore: keep release notes in PR

* chore: keep release notes in PR

* refactor(mattermost): clarify bounded target cache

---------

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
RomneyDa pushed a commit that referenced this pull request Aug 30, 2026
* fix(mattermost): cap opaque target cache

* refactor(mattermost): clarify bounded target cache

* chore: keep release notes in PR

* chore: keep release notes in PR

* refactor(mattermost): clarify bounded target cache

---------

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit 20e1368)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: mattermost Channel integration: mattermost size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants