From a13efbe2b5818a2ebf632afa591f94fb8c108a78 Mon Sep 17 00:00:00 2001 From: Clawdbot Date: Mon, 2 Feb 2026 17:05:55 +0100 Subject: [PATCH] fix: pass threadId/to/accountId from parent to subagent gateway call When spawning a subagent, the requesterOrigin's threadId, to, and accountId were not forwarded to the callGateway({method:'agent'}) params. This meant the subagent's runContext had no currentThreadTs or currentChannelId, so resolveTelegramAutoThreadId could not auto-inject the forum topic thread ID when the subagent used the message tool. Changes: - sessions-spawn-tool: pass to, accountId, threadId from requesterOrigin - run-context: populate currentChannelId from opts.to as fallback Fixes subagent messages landing in General Topic instead of the correct Telegram DM topic thread. --- src/agents/tools/sessions-spawn-tool.ts | 4 ++++ src/commands/agent/run-context.ts | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/agents/tools/sessions-spawn-tool.ts b/src/agents/tools/sessions-spawn-tool.ts index 6fe582c528a..d73b8c4a0d5 100644 --- a/src/agents/tools/sessions-spawn-tool.ts +++ b/src/agents/tools/sessions-spawn-tool.ts @@ -231,6 +231,10 @@ export function createSessionsSpawnTool(opts?: { message: task, sessionKey: childSessionKey, channel: requesterOrigin?.channel, + to: requesterOrigin?.to ?? undefined, + accountId: requesterOrigin?.accountId ?? undefined, + threadId: + requesterOrigin?.threadId != null ? String(requesterOrigin.threadId) : undefined, idempotencyKey: childIdem, deliver: false, lane: AGENT_LANE_SUBAGENT, diff --git a/src/commands/agent/run-context.ts b/src/commands/agent/run-context.ts index 445e03a5dba..cf8dacd711a 100644 --- a/src/commands/agent/run-context.ts +++ b/src/commands/agent/run-context.ts @@ -42,5 +42,14 @@ export function resolveAgentRunContext(opts: AgentCommandOpts): AgentRunContext merged.currentThreadTs = String(opts.threadId); } + // Populate currentChannelId from the outbound target so that + // resolveTelegramAutoThreadId can match the originating chat. + if (!merged.currentChannelId && opts.to) { + const trimmedTo = opts.to.trim(); + if (trimmedTo) { + merged.currentChannelId = trimmedTo; + } + } + return merged; }