Skip to content

fix(mattermost): backfill thread history from server when in-memory window is empty - #93243

Closed
iloveleon19 wants to merge 5 commits into
openclaw:mainfrom
iloveleon19:fix/mattermost-thread-history-backfill
Closed

iloveleon19 wants to merge 5 commits into
openclaw:mainfrom
iloveleon19:fix/mattermost-thread-history-backfill

Conversation

@iloveleon19

@iloveleon19 iloveleon19 commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Thread/conversation history is in-memory only, so after a gateway restart or a session clear the window for a thread is empty. A user who @mentions the bot inside an existing thread to wake it then gets a reply with no thread context — worst exactly when the bot was unresponsive and is being recovered.
  • Add fetchMattermostThread (GET /posts/{id}/thread) and backfillMattermostThreadHistory, which seeds the history window from the server thread (resolving usernames, trimmed to historyLimit).
  • Recovery-gated: the in-memory window is cleared after every turn, so "window empty" alone would refetch and re-inject the whole server thread on every active-thread follow-up. The backfill is gated on the agent session id (read via getSessionEntry): it only rotates on /new or a restart — exactly when the thread context is actually missing — so the server thread is fetched at most once per session id and skipped while it is unchanged.
  • Wired into the inbound path for thread messages (threadRootId present and a history key); failures are logged and swallowed so inbound handling continues.
  • Unit coverage: seed from server, current-post exclusion + trim, no-overwrite when non-empty, error swallow, plus the recovery gate (active-thread steady state does not refetch, session-id rotation re-seeds, session id recorded after seeding).

Fixes #93204. Part of the #65729 umbrella split.

Scope

Availability (review follow-up)

The added inbound thread lookup is bounded so it cannot slow the reply path:

  • fetchMattermostThread aborts after a 5s timeout (AbortController); on a slow/unresponsive server the reply proceeds without backfilled history.
  • backfillMattermostThreadHistory records the session id even when the fetch fails (including a timeout abort), so a failing/slow server is fetched at most once per session rather than retried on every follow-up turn. Combined with the existing session-id recovery gate, backfill runs at most one bounded REST call per session.

Real behavior proof

  • Behavior or issue addressed: A thread whose in-memory window is empty after restart/session-clear must re-seed thread context from the server (Mattermost: thread context lost after restart/session-clear — no server-side backfill (split from #65729) #93204), AND it must NOT refetch the whole server thread on every normal active-thread follow-up (the window is cleared each turn).
  • Real environment tested: Self-hosted OpenClaw on base image ghcr.io/openclaw/openclaw:2026.6.5 running the equivalent change (Codex runtime), connected to a live Mattermost server, bot user @openclaw.
  • Exact steps or command run after this patch: In a Mattermost channel thread, sent several follow-up messages to the bot, then /new, then another follow-up — while watching the backfill verbose logs.
  • Observed result after fix: the server thread was fetched once on first touch, skipped on the active follow-ups (no refetch while the session id was unchanged), and fetched again after /new (session id rotated → context re-seeded); the post-/new follow-up was answered from the recovered thread context.
  • Evidence after fix: redacted runtime logs from the recovery-gated build (session keys truncated), plus a screenshot of context surviving /new:
    thread backfill seeded 2 entries for agent:main:mattermost:group:…:thread:…
    thread backfill skipped: same session for agent:main:mattermost:group:…:thread:…
    thread backfill skipped: same session for agent:main:mattermost:group:…:thread:…
    thread backfill seeded 8 entries for agent:main:mattermost:group:…:thread:…   (after /new)
    
    thread context survives session clear
  • What was not tested: DM threads (out of scope here — channel-only; DM track is Mattermost: make DM threading configurable (dmReplyToMode) — split from #65729 #93203). The exact verbose seed/skip log strings above are from the equivalent dist-patched deployment, not this branch build (this branch keeps the helper's error-only logging); the gate behavior itself is covered deterministically by the added unit tests.

Tests

  • node scripts/run-vitest.mjs extensions/mattermost/src/mattermost/monitor.test.ts extensions/mattermost/src/mattermost/client.test.ts — all passed (incl. 7 backfillMattermostThreadHistory tests: 4 prior + 3 recovery-gate)
  • corepack pnpm format:check extensions/mattermost/src/mattermost/monitor.ts extensions/mattermost/src/mattermost/monitor.test.ts

@openclaw-barnacle openclaw-barnacle Bot added channel: mattermost Channel integration: mattermost size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 15, 2026
@clawsweeper

clawsweeper Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head 35c2cdf8688a63a703794d6478df0217cf52d31e, but the PR head is now 366cc60fb90aac735102a13dd7e10e640cc5dbba. Its old verdict and PR readiness labels are no longer current.

Next step
Run or wait for a fresh ClawSweeper review on the current PR head.

Review history (2 earlier review cycles)
  • reviewed 2026-07-04T16:05:52.416Z sha 35c2cdf :: needs changes before merge. :: [P1] Cap backfill before resolving users
  • reviewed 2026-07-04T16:12:51.380Z sha 35c2cdf :: needs changes before merge. :: [P1] Cap backfill before resolving users

@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. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jun 15, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label Jun 15, 2026
@openclaw-barnacle openclaw-barnacle Bot added size: M proof: supplied External PR includes structured after-fix real behavior proof. and removed size: S proof: sufficient ClawSweeper judged the real behavior proof convincing. labels Jun 17, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. and removed proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. labels Jun 19, 2026
@iloveleon19

Copy link
Copy Markdown
Contributor Author

Follow-up proof — non-empty first-sighting gate fix (commit 2f4b104)

The latest commit records the session id before the non-empty-window return in backfillMattermostThreadHistory, so an active thread no longer refetches the server thread on an ordinary same-session follow-up after the turn kernel clears the in-memory window. Verified on a live Mattermost deployment running the equivalent gate.

Group thread, single agent session (redacted IDs):

[openclaw-patch] thread backfill seeded 2 entries        …group:…:thread:cwby…   <- first @mention (cold thread → recovery fetch)
[openclaw-patch] thread backfill skipped: same session   …:thread:cwby…          <- same-session follow-up (NO refetch) ✅
[openclaw-patch] thread backfill skipped: same session   …:thread:cwby…          <- @mention carrying /new (still same session, NO refetch) ✅
--- /new executed (agent session id rotates) ---
[openclaw-patch] thread backfill seeded 8 entries        …:thread:cwby…          <- first @mention after /new (recovery fetch) ✅

A companion DM thread shows the same gate holding: one seeded followed by 7 consecutive skipped: same session across the session — exactly one fetch per session, never a per-follow-up refetch.

End-to-end, the bot still answered with the pre-/new timeline after /new (it extrapolated 2029 from the 2027/2028 it could only have via the re-seeded thread), confirming the recovered context reached the agent:

Mattermost thread keeps context after /new

Acceptance commands pass on the PR branch:

  • node scripts/run-vitest.mjs extensions/mattermost/src/mattermost/monitor.test.ts extensions/mattermost/src/mattermost/client.test.ts → 85 passed
  • corepack pnpm format:check extensions/mattermost/src/mattermost/monitor.ts extensions/mattermost/src/mattermost/client.ts extensions/mattermost/src/mattermost/monitor.test.ts → clean

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 22, 2026
@iloveleon19
iloveleon19 force-pushed the fix/mattermost-thread-history-backfill branch from 2f4b104 to 02a6e2d Compare July 3, 2026 12:31
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jul 3, 2026
…ession

Addresses the availability risk of the added inbound thread REST lookup:

- fetchMattermostThread now aborts after a 5s timeout (AbortController), so
  a slow or unresponsive server cannot stall the inbound reply — the caller
  proceeds without backfilled history.
- backfillMattermostThreadHistory records the session id even when the fetch
  fails (including a timeout abort), so a failing/slow server is fetched at
  most once per session instead of being retried on every follow-up turn.
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 4, 2026
Cap the per-post work before username resolution: filter out missing and
current posts and trim to the last historyLimit eligible posts, then
resolve usernames only for that bounded set. Previously every returned
server post triggered an awaited resolveUserInfo lookup before the final
trim, so a large cold thread could issue many sequential Mattermost user
lookups on the inbound reply path. Adds a long-thread regression test
proving resolveUserInfo is called only for the retained entries.
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jul 6, 2026
@steipete

Copy link
Copy Markdown
Contributor

Thanks for the implementation and live proof. Closing as superseded by the more complete recovery design in #93865. This branch still fetches the full thread and lacks bounded marker eviction, in-flight deduplication, and stale-session completion protection; #93865 covers those concurrency and lifecycle cases with bounded server requests and broader integration tests. #93204 remains open until the selected fix lands.

@steipete steipete added duplicate This issue or pull request already exists close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster labels Jul 13, 2026
@steipete steipete closed this Jul 13, 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 close:duplicate Closed as duplicate dedupe:child Duplicate issue/PR child in dedupe cluster duplicate This issue or pull request already exists P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mattermost: thread context lost after restart/session-clear — no server-side backfill (split from #65729)

4 participants