Skip to content

fix(mattermost): cap outbound target caches to prevent unbounded memory growth - #101800

Closed
hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/mattermost-outbound-cache-cap
Closed

hugenshen wants to merge 1 commit into
openclaw:mainfrom
hugenshen:fix/mattermost-outbound-cache-cap

Conversation

@hugenshen

@hugenshen hugenshen commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where long-running Mattermost-connected OpenClaw processes gradually accumulate unbounded heap memory proportional to the number of unique users, channel names, and DM pairs they have ever addressed. On active servers with many distinct recipients the four module-level Maps in send.ts — botUserCache, userByNameCache, channelByNameCache, dmChannelCache — grow without limit and are never evicted.

Concrete example: An OpenClaw instance routing messages to 5 000 distinct Mattermost users over a week records 5 000 entries in userByNameCache and another 5 000 in dmChannelCache. No TTL, no eviction, no bound — every unique (baseUrl, token, target) triple stays in memory for the life of the process.

Why This Change Was Made

Added four named cap constants and one pruneMapToMaxSize call after each cache .set(). pruneMapToMaxSize (from openclaw/plugin-sdk/collection-runtime) evicts the oldest insertion-order entry whenever the map exceeds its limit — the same LRU-like strategy used in src/infra/map-size.ts.

Cap values follow the existing Slack precedent (SLACK_DM_CHANNEL_CACHE_MAX = 1024 at extensions/slack/src/send.ts:49):

Cache Cap
botUserCache 64 (bounded by distinct bot accounts)
userByNameCache 512
channelByNameCache 512
dmChannelCache 1024

User Impact

Before: Memory in the Mattermost send path grows linearly with the number of unique targets seen since process start. Operators on large servers must restart periodically to reclaim memory.

After: Each outbound cache is capped; the total overhead per cache is bounded and stable under sustained traffic.

Evidence

  • extensions/mattermost/src/mattermost/send.ts:70–73 — four unbounded Map declarations, no cap or TTL before this PR
  • extensions/slack/src/send.ts:49,572–578 — direct Slack precedent with SLACK_DM_CHANNEL_CACHE_MAX
  • src/infra/map-size.ts — pruneMapToMaxSize implementation; openclaw/plugin-sdk/collection-runtime re-exports it
  • Diff: production change is +13 lines in extensions/mattermost/src/mattermost/send.ts; call-site regressions added in extensions/mattermost/src/mattermost/send.test.ts

Behavior proof

$ pnpm test extensions/mattermost/src/mattermost/send.test.ts

 Test Files  1 passed (1)
      Tests  35 passed (35)

New regressions in extensions/mattermost/src/mattermost/send.test.ts assert outbound caches stay bounded:

  • evicts oldest DM channel cache entries after the configured cap — 1025 distinct user: sends force a cache miss on the first entry after the 1024 cap.
  • evicts oldest user-by-name cache entries after the configured cap — 513 distinct @username sends force a username re-lookup for the first entry after the 512 cap.

This mirrors the Slack DM cache precedent and reuses the shared pruneMapToMaxSize helper contract from src/infra/map-size.ts.


AI-assisted PR — Generated with Cursor/Claude. Logic verified against src/infra/map-size.ts and extensions/slack/src/send.ts precedent. Diff is minimal and non-breaking: existing callers are unaffected, cache semantics are preserved, only the maximum retained size changes.

Made with Cursor

@openclaw-barnacle openclaw-barnacle Bot added channel: mattermost Channel integration: mattermost size: XS labels Jul 7, 2026
@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 7, 2026, 10:35 PM ET / 02:35 UTC.

Summary
The PR caps four Mattermost outbound lookup caches with the existing pruneMapToMaxSize helper and adds two eviction regression tests.

PR surface: Source +13, Tests +80. Total +93 across 2 files.

Reproducibility: yes. by source inspection: current main declares process-wide Mattermost outbound Maps and writes them without max-size pruning. I did not establish a live high-cardinality Mattermost gateway run.

Review metrics: 1 noteworthy metric.

  • Mattermost outbound cache caps: 4 added. The diff changes process-memory behavior for bot-user, username, channel-name, and DM-channel lookup caches in one channel plugin.

Stored data model
Persistent data-model change detected: persistent cache schema: extensions/mattermost/src/mattermost/send.test.ts, vector/embedding metadata: extensions/mattermost/src/mattermost/send.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🦞 diamond lobster
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body and comments provide mocked/local Vitest output only, with no live Mattermost or gateway logs, terminal transcript, recording, or other real setup proof. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Contributor proof is mock-only, so maintainers still lack real Mattermost or gateway evidence that the bounded-cache behavior works in an actual runtime.
  • [P1] fix(mattermost): cap outbound and monitor resource caches #101740 overlaps the same Mattermost send-cache surface, so maintainers should avoid landing duplicate cache-cap variants.

Maintainer options:

  1. Decide the mitigation before merge
    Land one proven Mattermost cache-cap path: this narrow outbound-only fix after real proof, or the broader fix(mattermost): cap outbound and monitor resource caches #101740 if maintainers choose that as the canonical scope.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] No automated repair is appropriate; the remaining blocker is contributor-supplied real Mattermost/gateway proof plus coordination with the overlapping broader PR.

Security
Cleared: No concrete security or supply-chain concern found; the diff only changes Mattermost plugin TypeScript/tests and imports an existing SDK helper.

Review details

Best possible solution:

Land one proven Mattermost cache-cap path: this narrow outbound-only fix after real proof, or the broader #101740 if maintainers choose that as the canonical scope.

Do we have a high-confidence way to reproduce the issue?

Yes by source inspection: current main declares process-wide Mattermost outbound Maps and writes them without max-size pruning. I did not establish a live high-cardinality Mattermost gateway run.

Is this the best way to solve the issue?

Yes for the code shape: the patch stays inside the Mattermost plugin boundary, uses an existing SDK helper, and avoids new config or persistence. It is not merge-ready until real behavior proof is supplied and the overlapping PR path is coordinated.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against fdc98242a1e5.

Label changes

Label changes:

  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦞 diamond lobster.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body and comments provide mocked/local Vitest output only, with no live Mattermost or gateway logs, terminal transcript, recording, or other real setup proof. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: This is a normal-priority resource-growth bugfix for one bundled channel plugin with limited blast radius and no evidence of an active outage.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦞 diamond lobster.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body and comments provide mocked/local Vitest output only, with no live Mattermost or gateway logs, terminal transcript, recording, or other real setup proof. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +13, Tests +80. Total +93 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 13 0 +13
Tests 1 80 0 +80
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 93 0 +93

What I checked:

Likely related people:

  • mukhtharcm: CONTRIBUTING.md lists Muhammed Mukhthar for Mattermost, and related Mattermost send/DM history includes reviewer or adjacent ownership signals. (role: listed Mattermost area owner and reviewer; confidence: high; commits: 6d0547dc2eab, 2145eb5908c0; files: CONTRIBUTING.md, extensions/mattermost/src/mattermost/send.ts, extensions/mattermost/src/mattermost/client.ts)
  • teconomix: PR mattermost: fix DM media upload for unprefixed user IDs #29925 introduced the user-first Mattermost DM routing adjacent to the DM cache now being capped. (role: feature-history contributor; confidence: high; commits: 6d0547dc2eab; files: extensions/mattermost/src/mattermost/send.ts, extensions/mattermost/src/mattermost/target-resolution.ts)
  • Jonathan Jing: Commit 2145eb5 added Mattermost DM channel retry handling used by the same DM-channel resolution path. (role: adjacent feature contributor; confidence: medium; commits: 2145eb5908c0; files: extensions/mattermost/src/mattermost/send.ts, extensions/mattermost/src/mattermost/client.ts)
  • Peter Steinberger: Current blame for the inspected Mattermost send cache declarations points to a recent broad current-main refactor commit, and path history shows repeated recent Mattermost runtime touches. (role: recent area contributor; confidence: medium; commits: a6768d9de567; files: extensions/mattermost/src/mattermost/send.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (2 earlier review cycles)
  • reviewed 2026-07-07T16:46:27.095Z sha a297bbe7358b99a3589bb8ebbb85b43faaf5883d :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T02:23:16.129Z sha 56f2411 :: needs real behavior proof before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 7, 2026
@hugenshen
hugenshen force-pushed the fix/mattermost-outbound-cache-cap branch from a297bbe to 17c415a Compare July 8, 2026 02:02
@hugenshen

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main, added outbound cache-cap regressions, and refreshed behavior proof.

Test run

$ pnpm test extensions/mattermost/src/mattermost/send.test.ts

 Test Files  1 passed (1)
      Tests  35 passed (35)

New regressions

  • DM cache: 1025 distinct user: sends → first entry evicted and DM resolution runs again
  • Username cache: 513 distinct @username sends → first username lookup runs again after cap

Prior CI failure was unrelated (src/tui/tui-pty-local.e2e.test.ts); branch is now rebased on current main.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Add outbound cache cap regressions.

Co-authored-by: Cursor <[email protected]>
@hugenshen
hugenshen force-pushed the fix/mattermost-outbound-cache-cap branch from 17c415a to 56f2411 Compare July 8, 2026 02:13
@clawsweeper clawsweeper Bot removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 8, 2026
@hugenshen

Copy link
Copy Markdown
Contributor Author

Squashed to a single commit and rebased on latest main. Behavior proof is covered by colocated regression tests (no scripts/proof-*.mjs).

Test run

pnpm test extensions/mattermost/src/mattermost/send.test.ts — 35 passed

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 8, 2026
@vincentkoc vincentkoc self-assigned this Jul 8, 2026
@steipete

Copy link
Copy Markdown
Contributor

Closing as superseded by #101740, which landed as 514829f0e1457e5b8f1c64ae4ce7a55889361f36.

The landed canonical repair includes this PR's four outbound cache owners, with a 64-entry bot identity cap and 1,024-entry username, channel-name, and DM-channel caps. It also bounds the two monitor resource caches, while #103258 owns the already-landed opaque-target classifier cap.

Focused Mattermost proof passed 2 files / 48 tests in Blacksmith Testbox run 29073327959, and hosted CI passed in run 29072789577. The landed commit preserves your authorship and co-authorship.

Thanks @hugenshen for identifying and fixing these retention paths.

@steipete steipete closed this Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: mattermost Channel integration: mattermost P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants