fix(agent): search all agent stores when resolving --session-id (#13579)

* fix(agent): search all agent stores when resolving --session-id

When `--session-id` was provided without `--to` or `--agent`, the reverse
lookup only searched the default agent's session store. Sessions created
under a specific agent (e.g. `--agent mybot`) live in that agent's store
file, so the lookup silently failed and the session was not reused.

Now `resolveSessionKeyForRequest` iterates all configured agent stores
when the primary store doesn't contain the requested sessionId.

Fixes #12881

* fix: search other agent stores when --to key does not match --session-id

When --to derives a session key whose stored sessionId doesn't match the
requested --session-id, the cross-store search now also runs. This handles
the case where a user provides both --to and --session-id targeting a
session in a different agent's store.
This commit is contained in:
Marcus Castro
2026-02-13 14:46:54 -03:00
committed by GitHub
parent 649826e435
commit eed8cd383f
2 changed files with 253 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import crypto from "node:crypto";
import type { MsgContext } from "../../auto-reply/templating.js";
import type { OpenClawConfig } from "../../config/config.js";
import { listAgentIds } from "../../agents/agent-scope.js";
import {
normalizeThinkLevel,
normalizeVerboseLevel,
@@ -78,6 +79,31 @@ export function resolveSessionKeyForRequest(opts: {
}
}
// When sessionId was provided but not found in the primary store, search all agent stores.
// Sessions created under a specific agent live in that agent's store file; the primary
// store (derived from the default agent) won't contain them.
// Also covers the case where --to derived a sessionKey that doesn't match the requested sessionId.
if (
opts.sessionId &&
!explicitSessionKey &&
(!sessionKey || sessionStore[sessionKey]?.sessionId !== opts.sessionId)
) {
const allAgentIds = listAgentIds(opts.cfg);
for (const agentId of allAgentIds) {
if (agentId === storeAgentId) {
continue;
}
const altStorePath = resolveStorePath(sessionCfg?.store, { agentId });
const altStore = loadSessionStore(altStorePath);
const foundKey = Object.keys(altStore).find(
(key) => altStore[key]?.sessionId === opts.sessionId,
);
if (foundKey) {
return { sessionKey: foundKey, sessionStore: altStore, storePath: altStorePath };
}
}
}
return { sessionKey, sessionStore, storePath };
}