Skip to content

fix(mattermost): backfill thread history only on first-sighting recovery - #93234

Closed
Bartok9 wants to merge 2 commits into
openclaw:mainfrom
Bartok9:fix/93204-mattermost-thread-backfill
Closed

Bartok9 wants to merge 2 commits into
openclaw:mainfrom
Bartok9:fix/93204-mattermost-thread-backfill

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Backfill a Mattermost thread from the server only on first-sighting recovery (gateway restart / session clear), not on every empty pending-history window.
  • Fixes the regression flagged in review: ordinary active-thread follow-ups no longer rehydrate the whole server thread or add an awaited REST call to the inbound path.

Motivation

Closes #93204.

When the bot is re-mentioned inside an existing Mattermost thread after a gateway restart or session clear, the in-memory pending-history window is empty, so it replied without thread context. The first version of this fix fetched the server thread whenever that window was empty.

As @clawsweeper correctly caught, the shared turn kernel clears the pending-history window after every successful dispatch (src/channels/turn/kernel.ts → clearPendingHistoryAfterTurn). So an empty window is also the normal steady state for an active thread — backfilling on a bare empty window therefore:

  1. rehydrated the full server thread on ordinary follow-up mentions, and
  2. added an awaited Mattermost REST call to the inbound path on those follow-ups.

The fix

New pure gate shouldBackfillThreadFromServer (in thread-backfill.ts) distinguishes recovery from steady state by process lifetime:

  • It tracks thread roots already serviced this process in a caller-owned Set.
  • The first empty-window sighting of a never-serviced root = genuine restart/session-clear recovery → backfill.
  • Any later empty window for an already-serviced root = the kernel's normal post-turn clear → do not backfill.
  • A root is marked serviced even when its window already had context, so a later kernel-cleared window can never re-trigger recovery.

monitor.ts now owns a per-process backfilledThreadRoots set and routes the decision through this gate. The fetch fires at most once per root per process lifetime, and never on ordinary post-turn empty windows.

Real behavior proof

  • Behavior addressed: an active Mattermost thread, after the turn kernel clears its pending-history window post-turn, must NOT re-fetch and inject the full server thread on the next mention (the review's P1 regression). Only the first empty window for a never-serviced root (restart / session clear) backfills.
  • Real environment tested: the shipped thread-backfill.ts functions (shouldBackfillThreadFromServer, buildThreadBackfillEntries) loaded directly via tsx in a real Node v25 process and driven through the issue scenario against live process state — no test runner, no vitest, no mocked module shim. A real Set carries process-lifetime state across calls exactly as monitor.ts uses it.
  • Exact steps or command run after this patch:
    npx tsx .proof-mattermost-backfill.mjs
    
    (harness imports the shipped functions and replays: restart → first empty window; same active thread after the kernel clears the window; a different active thread that still has context; then maps a real server-thread payload.)
  • Evidence after fix: copied live stdout from the command above —
    === PR #93204 live runtime proof (process-state, no test runner) ===
    1) restart, first empty window  -> backfill: true
    2) post-turn kernel-cleared window (same root) -> backfill: false
    3) active thread, window has context -> backfill: false
    4) buildThreadBackfillEntries (current post skipped):
    [
      {
        "sender": "alice",
        "body": "kickoff question",
        "messageId": "p1"
      },
      {
        "sender": "bob",
        "body": "an answer",
        "messageId": "p2"
      }
    ]
    PROOF_RESULT: PASS
    
  • Observed result after fix: the restart's first empty window backfills (true); the same active thread's next empty window — left behind by the kernel's post-turn clear — returns false, so it does NOT re-fetch or re-inject the server thread (the P1 regression is gone); an active thread that still has in-memory context returns false; and buildThreadBackfillEntries returns the two prior posts in order with resolved sender labels, correctly skipping the current mention (p3). The harness asserts all four and prints PROOF_RESULT: PASS.
  • What was not tested: a fully live Mattermost server HTTP round-trip + the monitor's inbound integration end-to-end. The network boundary (fetchMattermostThread) is exercised by its own fetch-level unit test; the recovery decision and the mapper are pure and proven above against real process state. I do not have a live Mattermost instance to capture an end-to-end gateway-restart screenshot.

Addressing the review P1s

  • Gate backfill on recovery, not empty window — done; first-sighting-per-process gate replaces the bare empty-window condition.
  • Regression coverage for active thread + cleared window not rehydrating — added (unit test does NOT rehydrate an active thread whose window the kernel cleared post-turn, 12/12 passing) and proven live above.
  • Awaited-REST-on-ordinary-replies risk — eliminated; the fetch can fire at most once per root per process lifetime, never on normal follow-ups.

@openclaw-barnacle openclaw-barnacle Bot added channel: mattermost Channel integration: mattermost size: M 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

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: this PR targets the same Mattermost thread-history recovery bug as the open canonical sibling, but its process-lifetime root gate misses same-process session-clear recovery that the sibling handles with a session-aware gate and stronger proof.

Root-cause cluster
Relationship: superseded
Canonical: #93243
Summary: This PR, the canonical issue, and sibling PRs target the same Mattermost thread-history recovery bug; the open sibling PR is the stronger proof-positive landing path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Close this branch and consolidate review on #93243 as the canonical Mattermost thread-history recovery fix.

So I’m closing this here and keeping the remaining discussion on #93243.

Review details

Best possible solution:

Close this branch and consolidate review on #93243 as the canonical Mattermost thread-history recovery fix.

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

Yes, source-level. Current main keeps Mattermost thread history in a process-local map, the shared turn kernel clears that map after successful turns, and this PR's gate ignores session id rotation during same-process session clear.

Is this the best way to solve the issue?

No. The Mattermost plugin is the right owner boundary, but this PR's process-lifetime root gate is weaker than the session-aware sibling and misses part of the reported recovery case.

Security review:

Security review cleared: No concrete security or supply-chain concern found; the diff uses the existing authenticated Mattermost client path and changes no dependencies, workflows, lockfiles, package metadata, or secret handling.

AGENTS.md: found and applied where relevant.

What I checked:

  • Repository policy read: Root AGENTS.md and extensions/AGENTS.md were read; the relevant guidance requires whole-path PR review and treats session/history changes as merge-risk-sensitive while keeping bundled plugin behavior in extensions. (AGENTS.md:1, 1bccd2930437)
  • Current main lacks server thread backfill: Current main creates a process-local Mattermost channelHistories map and builds pending/inbound context from that map; search found no Mattermost /posts/{threadRootId}/thread backfill helper on current main. (extensions/mattermost/src/mattermost/monitor.ts:871, 1bccd2930437)
  • Empty history is normal after successful turns: The shared turn kernel clears group pending history after successful dispatch, so an empty Mattermost history window cannot by itself identify restart or session-clear recovery. (src/channels/turn/kernel.ts:158, 1bccd2930437)
  • Session clear rotates session id: Current main's session reset path creates a new nextSessionId, so same-process session-clear recovery needs a signal stronger than a process-local thread-root Set. (src/gateway/session-reset-service.ts:996, 1bccd2930437)
  • This PR gates only by process-lifetime root: At this PR head, shouldBackfillThreadFromServer stores threadRootId in seenThreadRoots and returns false for every later sighting of that root, regardless of agent session id rotation. (extensions/mattermost/src/mattermost/thread-backfill.ts:39, 025dcb172f1d)
  • Canonical sibling is session-aware: The open sibling records backfilled session ids per historyKey, marks non-empty first sightings as serviced, and re-backfills when the session id rotates. (extensions/mattermost/src/mattermost/monitor.ts:522, 2f4b1045d4b3)

Likely related people:

  • Teconomix: Commit 171d2df added Mattermost replyToMode support and thread-scoped session behavior that this recovery work extends. (role: Mattermost threading feature-history contributor; confidence: high; commits: 171d2df9e0fe; files: extensions/mattermost/src/mattermost/monitor.ts, extensions/mattermost/src/mattermost/accounts.ts, extensions/mattermost/src/mattermost/monitor.test.ts)
  • Peter Steinberger: Commit 1507a97 centralized inbound supplemental context and touched the shared turn/history path that makes empty pending history ambiguous. (role: channel history refactor contributor; confidence: high; commits: 1507a9701b83; files: extensions/mattermost/src/mattermost/monitor.ts, src/channels/turn/kernel.ts)
  • Jacob Tomlinson: Commit 8c3ba33 adjusted default Mattermost replies in existing threads, adjacent to the thread-root behavior reviewed here. (role: recent adjacent Mattermost thread contributor; confidence: medium; commits: 8c3ba3346311; files: extensions/mattermost/src/mattermost/monitor.ts, extensions/mattermost/src/mattermost/monitor.test.ts)
  • iloveleon19: They opened the canonical production-context issue and authored the proof-positive sibling branch that now carries the stronger session-aware fix path. (role: reporter and sibling candidate author; confidence: medium; commits: 65805e519df2; files: extensions/mattermost/src/mattermost/monitor.ts, extensions/mattermost/src/mattermost/monitor.inbound-system-event.test.ts)

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

@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 Jun 15, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 15, 2026
@clawsweeper clawsweeper Bot added 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. labels Jun 15, 2026
@Bartok9
Bartok9 force-pushed the fix/93204-mattermost-thread-backfill branch from aa37e9f to 930d6e8 Compare June 16, 2026 09:01
@Bartok9 Bartok9 changed the title fix(mattermost): backfill thread history from server when in-memory window is empty fix(mattermost): backfill thread history only on first-sighting recovery Jun 16, 2026
@Bartok9

Bartok9 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Updated to address the review. The empty-window trigger is now gated on first-sighting recovery (per-process serviced-roots set), so active threads whose window the kernel clears post-turn are never rehydrated and ordinary follow-ups never incur the awaited server fetch. Added the requested regression test (does NOT rehydrate an active thread whose window the kernel cleared post-turn). Thanks for the precise catch on the kernel's post-turn clear semantics.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 16, 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.

Re-review progress:

@openclaw-barnacle openclaw-barnacle Bot added triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. and removed proof: supplied External PR includes structured after-fix real behavior proof. labels Jun 16, 2026
@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 Jun 16, 2026
@Bartok9
Bartok9 force-pushed the fix/93204-mattermost-thread-backfill branch from 930d6e8 to 01016e8 Compare June 16, 2026 11:39
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 16, 2026
@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. labels Jun 19, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. and removed merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jun 20, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. and removed merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 21, 2026
Bartok9 added 2 commits June 25, 2026 08:07
When re-mentioned inside an existing Mattermost thread after a gateway
restart or session clear, the in-memory pending-history window is empty,
so the bot replied without thread context (openclaw#93204). The original fix
fetched the server thread whenever that window was empty.

The turn kernel clears the pending-history window after every successful
dispatch (src/channels/turn/kernel.ts), so an empty window is ALSO the
normal steady state for an active thread. Backfilling on a bare empty
window therefore rehydrated the full server thread on ordinary follow-up
mentions and added an awaited REST call to the inbound path.

Gate the fetch on first-sighting recovery instead: shouldBackfillThreadFromServer
tracks thread roots serviced this process and only treats the first empty
window for a never-serviced root as restart/session-clear recovery. Active
threads whose window the kernel cleared post-turn are never rehydrated.

- Add pure shouldBackfillThreadFromServer gate (no network, set-scoped)
- Wire monitor.ts to a per-process serviced-roots set
- Add regression tests incl. active-thread-cleared-window does NOT rehydrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: mattermost Channel integration: mattermost merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M 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.

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

2 participants