@@ -24,6 +24,7 @@ import type { OpenClawConfig } from "../../config/types.openclaw.js";
2424import {
2525 normalizeAgentId ,
2626 normalizeMainKey ,
27+ toAgentRequestSessionKey ,
2728 toAgentStoreSessionKey ,
2829} from "../../routing/session-key.js" ;
2930import { resolveSessionIdMatchSelection } from "../../sessions/session-id-resolution.js" ;
@@ -149,9 +150,10 @@ export function resolveSessionKeyForRequest(opts: {
149150 const mainKey = normalizeMainKey ( sessionCfg ?. mainKey ) ;
150151 const requestedAgentId = opts . agentId ?. trim ( ) ? normalizeAgentId ( opts . agentId ) : undefined ;
151152 const requestedSessionId = opts . sessionId ?. trim ( ) || undefined ;
153+ const requestTo = opts . to ?. trim ( ) ;
152154 const explicitSessionKey = opts . sessionKey ?. trim ( ) || undefined ;
153155 const explicitAgentSessionKey =
154- ! explicitSessionKey && ! requestedSessionId && ! opts . to ?. trim ( )
156+ ! explicitSessionKey && ! requestedSessionId && ! requestTo
155157 ? resolveExplicitAgentSessionKey ( {
156158 cfg : opts . cfg ,
157159 agentId : requestedAgentId ,
@@ -165,16 +167,18 @@ export function resolveSessionKeyForRequest(opts: {
165167 } ) ;
166168 const sessionStore = loadSessionStore ( storePath ) ;
167169
168- const ctx : MsgContext | undefined = opts . to ?. trim ( ) ? { From : opts . to } : undefined ;
170+ const ctx : MsgContext | undefined = requestTo ? { From : requestTo } : undefined ;
171+ const agentScopedRequestKey =
172+ requestedAgentId && requestTo ? ( toAgentRequestSessionKey ( requestTo ) ?? requestTo ) : requestTo ;
169173 const derivedToSessionKey =
170- ! explicitSessionKey && ctx
174+ ! explicitSessionKey && requestTo
171175 ? requestedAgentId
172176 ? toAgentStoreSessionKey ( {
173177 agentId : requestedAgentId ,
174- requestKey : opts . to ,
178+ requestKey : agentScopedRequestKey ,
175179 mainKey,
176180 } )
177- : resolveSessionKey ( scope , ctx , mainKey )
181+ : resolveSessionKey ( scope , ctx as MsgContext , mainKey )
178182 : undefined ;
179183 let sessionKey : string | undefined =
180184 explicitSessionKey ?? derivedToSessionKey ?? explicitAgentSessionKey ;
0 commit comments