From e07158e194dc537e2b849a84eed470cfb5adbea4 Mon Sep 17 00:00:00 2001 From: gavrielc Date: Sun, 12 Apr 2026 18:13:42 +0300 Subject: [PATCH] fix(agent-runner): preserve thread_id when sending to current channel send_file and send_message with an explicit `to` parameter were always setting thread_id to null, causing files and messages to land in the Discord channel root instead of the thread the session is bound to. Co-Authored-By: Claude Opus 4.6 (1M context) --- container/agent-runner/src/mcp-tools/core.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/container/agent-runner/src/mcp-tools/core.ts b/container/agent-runner/src/mcp-tools/core.ts index cef0d6c..b805685 100644 --- a/container/agent-runner/src/mcp-tools/core.ts +++ b/container/agent-runner/src/mcp-tools/core.ts @@ -42,8 +42,10 @@ function destinationList(): string { * If `to` is omitted, use the session's default reply routing (channel + * thread the conversation is in) — the agent replies in place. * - * If `to` is specified, look up the named destination; thread_id is null - * because a cross-destination send starts a new conversation elsewhere. + * If `to` is specified, look up the named destination. If it resolves to + * the same channel the session is bound to, the session's thread_id is + * preserved so replies land in the correct thread. Otherwise thread_id + * is null (a cross-destination send starts a new conversation). */ function resolveRouting( to: string | undefined, @@ -75,10 +77,17 @@ function resolveRouting( const dest = findByName(to); if (!dest) return { error: `Unknown destination "${to}". Known: ${destinationList()}` }; if (dest.type === 'channel') { + // If the destination is the same channel the session is bound to, + // preserve the thread_id so replies land in the correct thread. + const session = getSessionRouting(); + const threadId = + session.channel_type === dest.channelType && session.platform_id === dest.platformId + ? session.thread_id + : null; return { channel_type: dest.channelType!, platform_id: dest.platformId!, - thread_id: null, + thread_id: threadId, resolvedName: to, }; }