Skip to content

Commit 83f0190

Browse files
committed
fix(sessions-send): normalize legacy DM sourceSessionKey to dmScope-resolved canonical key
Parse the requester key and only canonicalize when the rest contains a "direct" peer-kind segment (legacy per-peer / per-channel-peer / per-account-channel-peer DM keys). Group, cron, hook, subagent, and other non-DM callers preserve their exact session key for correct routing, watch registration, and A2A ping-pong bookkeeping. Fixes #107399
1 parent c1b55ff commit 83f0190

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

‎src/agents/tools/sessions-send-tool.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { OpenClawConfig } from "../../config/types.openclaw.js";
1515
import { callGateway } from "../../gateway/call.js";
1616
import { formatErrorMessage } from "../../infra/errors.js";
1717
import {
18+
buildAgentMainSessionKey,
1819
isSubagentSessionKey,
1920
normalizeAgentId,
2021
resolveAgentIdFromSessionKey,
@@ -558,18 +559,43 @@ export function createSessionsSendTool(opts?: {
558559
}
559560

560561
const requesterSessionKey = opts?.agentSessionKey ? effectiveRequesterKey : undefined;
562+
// When dmScope is "main", DMs route to agent:{agentId}:{mainKey}.
563+
// A session originally created under a per-peer / per-channel-peer /
564+
// per-account-channel-peer regime stores a key like
565+
// agent:main:feishu:default:direct:ou_XXXX
566+
// Embedding that stale key in inter-session provenance causes the
567+
// receiving agent to reply into a ghost session the sender never
568+
// monitors — permanent one-way communication failure.
569+
//
570+
// Only canonicalize keys whose parsed rest contains a "direct" peer-kind
571+
// segment (legacy DM keys). Group, cron, hook, subagent, and other
572+
// non-DM callers must preserve their exact session key for correct
573+
// routing, watch registration, and A2A ping-pong bookkeeping.
574+
const parsedRequester = requesterSessionKey
575+
? parseAgentSessionKey(requesterSessionKey)
576+
: null;
577+
const isLegacyDmRequester = parsedRequester
578+
? parsedRequester.rest.split(":").includes("direct")
579+
: false;
580+
const normalizedRequesterKey =
581+
requesterSessionKey && (cfg.session?.dmScope ?? "main") === "main" && isLegacyDmRequester
582+
? buildAgentMainSessionKey({
583+
agentId: resolveAgentIdFromSessionKey(requesterSessionKey),
584+
mainKey,
585+
})
586+
: requesterSessionKey;
561587
const requesterChannel = opts?.agentChannel;
562-
const sameSessionA2A = requesterSessionKey === resolvedKey;
563-
const isIsolatedCronRequester = isCronRunSessionKey(requesterSessionKey);
588+
const sameSessionA2A = normalizedRequesterKey === resolvedKey;
589+
const isIsolatedCronRequester = isCronRunSessionKey(normalizedRequesterKey);
564590
// Watch registration follows successful dispatch: a failed send must not leave
565591
// a hidden watch, and cron run-scoped sends can fall back to the durable parent
566592
// session, which is the key that receives future state changes.
567593
const watchRequested = params.watch === true;
568594
const registerWatchIfRequested = (targetSessionKey: string) => {
569595
const watched =
570-
watchRequested && requesterSessionKey && requesterSessionKey !== targetSessionKey
596+
watchRequested && normalizedRequesterKey && normalizedRequesterKey !== targetSessionKey
571597
? registerSessionStateWatch({
572-
watcherSessionKey: requesterSessionKey,
598+
watcherSessionKey: normalizedRequesterKey,
573599
targetSessionKey,
574600
})
575601
: false;
@@ -613,13 +639,13 @@ export function createSessionsSendTool(opts?: {
613639
: undefined;
614640

615641
const agentMessageContext = buildAgentToAgentMessageContext({
616-
requesterSessionKey,
642+
requesterSessionKey: normalizedRequesterKey,
617643
requesterChannel,
618644
targetSessionKey: displayKey,
619645
});
620646
const inputProvenance = {
621647
kind: "inter_session" as const,
622-
sourceSessionKey: requesterSessionKey,
648+
sourceSessionKey: normalizedRequesterKey,
623649
sourceChannel: requesterChannel,
624650
sourceTool: "sessions_send",
625651
};
@@ -699,7 +725,7 @@ export function createSessionsSendTool(opts?: {
699725
// Cron runs are isolated jobs; target replies must not become new
700726
// requester turns, but the target-side announce still runs.
701727
maxPingPongTurns: isIsolatedCronRequester ? 0 : maxPingPongTurns,
702-
requesterSessionKey,
728+
requesterSessionKey: normalizedRequesterKey,
703729
requesterChannel,
704730
baseline: flowBaseline,
705731
roundOneReply,

0 commit comments

Comments
 (0)