fix(slack): preserve string thread context in queue + DM route (#23804)

* fix(slack): preserve thread_ts in queue drain and deliveryContext

Two related fixes for Slack thread reply routing:

1. Queue drain drops string thread_ts (#11195)
   - `typeof threadId === "number"` in drain.ts only matches Telegram numeric
     topic IDs. Slack thread_ts is a string like "1770474140.187459" which
     fails the check, causing threadKey to become empty.
   - Changed to `threadId != null && threadId !== ""` to accept both number
     and string thread IDs.
   - Applies to all 3 occurrences in drain.ts: cross-channel detection,
     thread key building, and collected originatingThreadId extraction.

2. DM deliveryContext missing thread_ts (#10837)
   - updateLastRoute calls for Slack DMs in both prepare.ts and dispatch.ts
     built deliveryContext without threadId, so the session's delivery context
     never included thread_ts for DM threads.
   - Added threadId from threadContext.messageThreadId / ctxPayload.MessageThreadId
     to both updateLastRoute call sites.

Tests: 3 new cases in queue.collect-routing.test.ts
- Collects messages with matching string thread_ts (same Slack thread)
- Separates messages with different string thread_ts (different threads)
- Treats empty string threadId same as absent

Closes #10837, closes #11195

* fix(slack): preserve string thread context in queue + DM route updates

---------

Co-authored-by: RobClawd <clawd@RobClawds-Mac-mini.local>
This commit is contained in:
Vincent Koc
2026-02-22 13:26:31 -05:00
committed by GitHub
parent b13bba9c35
commit cd7b2814af
4 changed files with 9 additions and 4 deletions

View File

@@ -30,7 +30,7 @@ export function scheduleFollowupDrain(
// Once the batch is mixed, never collect again within this drain.
// Prevents “collect after shift” collapsing different targets.
//
// Debug: `pnpm test src/auto-reply/reply/queue.collect-routing.test.ts`
// Debug: `pnpm test src/auto-reply/reply/reply-flow.test.ts`
// Check if messages span multiple channels.
// If so, process individually to preserve per-message routing.
const isCrossChannel = hasCrossChannelItems(queue.items, (item) => {
@@ -38,13 +38,14 @@ export function scheduleFollowupDrain(
const to = item.originatingTo;
const accountId = item.originatingAccountId;
const threadId = item.originatingThreadId;
if (!channel && !to && !accountId && threadId == null) {
if (!channel && !to && !accountId && (threadId == null || threadId === "")) {
return {};
}
if (!isRoutableChannel(channel) || !to) {
return { cross: true };
}
const threadKey = threadId != null ? String(threadId) : "";
// Support both number (Telegram topic IDs) and string (Slack thread_ts) thread IDs.
const threadKey = threadId != null && threadId !== "" ? String(threadId) : "";
return {
key: [channel, to, accountId || "", threadKey].join("|"),
};
@@ -76,8 +77,9 @@ export function scheduleFollowupDrain(
const originatingAccountId = items.find(
(i) => i.originatingAccountId,
)?.originatingAccountId;
// Support both number (Telegram topic) and string (Slack thread_ts) thread IDs.
const originatingThreadId = items.find(
(i) => i.originatingThreadId != null,
(i) => i.originatingThreadId != null && i.originatingThreadId !== "",
)?.originatingThreadId;
const prompt = buildCollectPrompt({