Skip to content

Commit 99e5422

Browse files
author
solar
committed
fix: tighten agent session key resolution
1 parent 38a6d25 commit 99e5422

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/agents/command/session.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { OpenClawConfig } from "../../config/types.openclaw.js";
2424
import {
2525
normalizeAgentId,
2626
normalizeMainKey,
27+
toAgentRequestSessionKey,
2728
toAgentStoreSessionKey,
2829
} from "../../routing/session-key.js";
2930
import { 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;

src/commands/agent/session.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ describe("resolveSessionKeyForRequest", () => {
267267
expect(result.storePath).toBe(MYBOT_STORE_PATH);
268268
});
269269

270+
it("treats a blank sessionKey as unset before deriving an agent-scoped --to key", async () => {
271+
setupMainAndMybotStorePaths();
272+
mockStoresByPath({
273+
[MYBOT_STORE_PATH]: {},
274+
});
275+
276+
const result = resolveSessionKeyForRequest({
277+
cfg: baseCfg,
278+
agentId: "mybot",
279+
to: "cw_111",
280+
sessionKey: " ",
281+
});
282+
283+
expect(result.sessionKey).toBe("agent:mybot:cw_111");
284+
expect(result.storePath).toBe(MYBOT_STORE_PATH);
285+
});
286+
287+
it("rewrites qualified --to keys to the selected agent scope", async () => {
288+
setupMainAndMybotStorePaths();
289+
mockStoresByPath({
290+
[MYBOT_STORE_PATH]: {},
291+
});
292+
293+
const result = resolveSessionKeyForRequest({
294+
cfg: baseCfg,
295+
agentId: "mybot",
296+
to: "agent:main:main",
297+
});
298+
299+
expect(result.sessionKey).toBe("agent:mybot:main");
300+
expect(result.storePath).toBe(MYBOT_STORE_PATH);
301+
});
302+
270303
it("skips already-searched primary store when iterating agents", async () => {
271304
setupMainAndMybotStorePaths();
272305
mocks.loadSessionStore.mockReturnValue({});

0 commit comments

Comments
 (0)