Skip to content

Commit 498de0f

Browse files
committed
fix: restore route threads from thread-scoped session keys only
Do not recover route thread ids from the normalised session store in non-inbound reply paths. Store normalisation can fold origin.threadId back into lastThreadId/deliveryContext, which resurrects stale thread routing after delivery was intentionally cleared. Instead, restore thread context only from: - ctx.MessageThreadId (active inbound turn), or - the active thread-scoped session key (:thread: / :topic:) Also updates dispatch tests to verify that stale origin/store thread metadata cannot override a non-thread session key, while a thread-scoped session key still restores the correct route thread.
1 parent 408a33d commit 498de0f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/auto-reply/reply/dispatch-from-config.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ vi.mock("../../logging/diagnostic.js", () => ({
113113
logMessageProcessed: diagnosticMocks.logMessageProcessed,
114114
logSessionStateChange: diagnosticMocks.logSessionStateChange,
115115
}));
116-
vi.mock("../../config/sessions.js", () => ({
117-
loadSessionStore: sessionStoreMocks.loadSessionStore,
118-
resolveStorePath: sessionStoreMocks.resolveStorePath,
119-
resolveSessionStoreEntry: sessionStoreMocks.resolveSessionStoreEntry,
120-
}));
116+
vi.mock("../../config/sessions.js", async (importOriginal) => {
117+
const actual = await importOriginal<typeof import("../../config/sessions.js")>();
118+
return {
119+
...actual,
120+
loadSessionStore: sessionStoreMocks.loadSessionStore,
121+
resolveStorePath: sessionStoreMocks.resolveStorePath,
122+
resolveSessionStoreEntry: sessionStoreMocks.resolveSessionStoreEntry,
123+
};
124+
});
121125

122126
vi.mock("../../plugins/hook-runner-global.js", () => ({
123127
getGlobalHookRunner: () => hookMocks.runner,
@@ -315,17 +319,19 @@ describe("dispatchReplyFromConfig", () => {
315319
);
316320
});
317321

318-
it("falls back to session deliveryContext threadId when current ctx has no MessageThreadId", async () => {
322+
it("falls back to thread-scoped session key when current ctx has no MessageThreadId", async () => {
319323
setNoAbort();
320324
mocks.routeReply.mockClear();
321325
sessionStoreMocks.currentEntry = {
322326
deliveryContext: {
323327
channel: "mattermost",
324328
to: "channel:CHAN1",
325329
accountId: "default",
326-
threadId: "post-root",
327330
},
328-
lastThreadId: "post-root",
331+
origin: {
332+
threadId: "stale-origin-root",
333+
},
334+
lastThreadId: "stale-origin-root",
329335
};
330336
const cfg = emptyConfig;
331337
const dispatcher = createDispatcher();
@@ -355,17 +361,16 @@ describe("dispatchReplyFromConfig", () => {
355361
it("does not resurrect a cleared route thread from origin metadata", async () => {
356362
setNoAbort();
357363
mocks.routeReply.mockClear();
358-
// Simulate the real store: lastThreadId is normalised from origin.threadId when
359-
// deliveryContext.threadId is absent. Using lastThreadId as a fallback would
360-
// incorrectly revive stale thread routing.
364+
// Simulate the real store: lastThreadId and deliveryContext.threadId may be normalised from
365+
// origin.threadId on read, but a non-thread session key must still route to channel root.
361366
sessionStoreMocks.currentEntry = {
362367
deliveryContext: {
363368
channel: "mattermost",
364369
to: "channel:CHAN1",
365370
accountId: "default",
366-
// threadId deliberately absent — the route was cleared
371+
threadId: "stale-root",
367372
},
368-
lastThreadId: "stale-root", // normalised from origin.threadId by loadSessionStore
373+
lastThreadId: "stale-root",
369374
origin: {
370375
threadId: "stale-root",
371376
},

src/auto-reply/reply/dispatch-from-config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolveSessionAgentId } from "../../agents/agent-scope.js";
22
import type { OpenClawConfig } from "../../config/config.js";
33
import {
44
loadSessionStore,
5+
parseSessionThreadInfo,
56
resolveSessionStoreEntry,
67
resolveStorePath,
78
type SessionEntry,
@@ -172,10 +173,12 @@ export async function dispatchReplyFromConfig(params: {
172173

173174
const sessionStoreEntry = resolveSessionStoreLookup(ctx, cfg);
174175
const acpDispatchSessionKey = sessionStoreEntry.sessionKey ?? sessionKey;
175-
// Only use the active delivery context's threadId. `lastThreadId` is not safe here because
176-
// `loadSessionStore` normalises it from `origin.threadId`, which can outlive an intentionally
177-
// cleared delivery route and would resurrect stale thread routing.
178-
const routeThreadId = ctx.MessageThreadId ?? sessionStoreEntry.entry?.deliveryContext?.threadId;
176+
// Restore route thread context only from the active turn or the thread-scoped session key.
177+
// Do not read thread ids from the normalised session store here: `origin.threadId` can be
178+
// folded back into lastThreadId/deliveryContext during store normalisation and resurrect a
179+
// stale route after thread delivery was intentionally cleared.
180+
const routeThreadId =
181+
ctx.MessageThreadId ?? parseSessionThreadInfo(acpDispatchSessionKey).threadId;
179182
const inboundAudio = isInboundAudioContext(ctx);
180183
const sessionTtsAuto = normalizeTtsAutoMode(sessionStoreEntry.entry?.ttsAuto);
181184
const hookRunner = getGlobalHookRunner();

0 commit comments

Comments
 (0)