diff --git a/src/gateway/server-bridge.ts b/src/gateway/server-bridge.ts index 6f676d26d..55f7f1f22 100644 --- a/src/gateway/server-bridge.ts +++ b/src/gateway/server-bridge.ts @@ -24,6 +24,7 @@ import { buildConfigSchema } from "../config/schema.js"; import { loadSessionStore, mergeSessionEntry, + resolveAgentMainSessionKey, resolveMainSessionKeyFromConfig, type SessionEntry, saveSessionStore, @@ -35,7 +36,10 @@ import { } from "../infra/voicewake.js"; import { clearCommandLane } from "../process/command-queue.js"; import { normalizeProviderId } from "../providers/plugins/index.js"; -import { normalizeMainKey } from "../routing/session-key.js"; +import { + normalizeMainKey, + resolveAgentIdFromSessionKey, +} from "../routing/session-key.js"; import { defaultRuntime } from "../runtime.js"; import { abortChatRunById, @@ -918,8 +922,14 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) { clientRunId, }); + // Normalize short main key alias to canonical form before store write + const agentId = resolveAgentIdFromSessionKey(p.sessionKey); + const mainSessionKey = resolveAgentMainSessionKey({ cfg, agentId }); + const rawMainKey = normalizeMainKey(cfg.session?.mainKey); + const storeKey = + p.sessionKey === rawMainKey ? mainSessionKey : p.sessionKey; if (store) { - store[p.sessionKey] = sessionEntry; + store[storeKey] = sessionEntry; if (storePath) { await saveSessionStore(storePath, store); } @@ -1032,12 +1042,18 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) { if (text.length > 20_000) return; const sessionKeyRaw = typeof obj.sessionKey === "string" ? obj.sessionKey.trim() : ""; - const mainKey = normalizeMainKey(loadConfig().session?.mainKey); - const sessionKey = sessionKeyRaw.length > 0 ? sessionKeyRaw : mainKey; + const cfg = loadConfig(); + const rawMainKey = normalizeMainKey(cfg.session?.mainKey); + const sessionKey = sessionKeyRaw.length > 0 ? sessionKeyRaw : rawMainKey; const { storePath, store, entry } = loadSessionEntry(sessionKey); + // Normalize short main key alias to canonical form before store write + const agentId = resolveAgentIdFromSessionKey(sessionKey); + const mainSessionKey = resolveAgentMainSessionKey({ cfg, agentId }); + const storeKey = + sessionKey === rawMainKey ? mainSessionKey : sessionKey; const now = Date.now(); const sessionId = entry?.sessionId ?? randomUUID(); - store[sessionKey] = { + store[storeKey] = { sessionId, updatedAt: now, thinkingLevel: entry?.thinkingLevel, @@ -1112,9 +1128,19 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) { const sessionKey = sessionKeyRaw.length > 0 ? sessionKeyRaw : `node-${nodeId}`; const { storePath, store, entry } = loadSessionEntry(sessionKey); + // Normalize short main key alias to canonical form before store write + const nodeCfg = loadConfig(); + const nodeAgentId = resolveAgentIdFromSessionKey(sessionKey); + const nodeMainSessionKey = resolveAgentMainSessionKey({ + cfg: nodeCfg, + agentId: nodeAgentId, + }); + const nodeRawMainKey = normalizeMainKey(nodeCfg.session?.mainKey); + const nodeStoreKey = + sessionKey === nodeRawMainKey ? nodeMainSessionKey : sessionKey; const now = Date.now(); const sessionId = entry?.sessionId ?? randomUUID(); - store[sessionKey] = { + store[nodeStoreKey] = { sessionId, updatedAt: now, thinkingLevel: entry?.thinkingLevel, diff --git a/src/gateway/server-methods/agent.ts b/src/gateway/server-methods/agent.ts index 386755492..ce4e64b5f 100644 --- a/src/gateway/server-methods/agent.ts +++ b/src/gateway/server-methods/agent.ts @@ -189,12 +189,6 @@ export const agentHandlers: GatewayRequestHandlers = { ); return; } - if (store) { - store[requestedSessionKey] = nextEntry; - if (storePath) { - await saveSessionStore(storePath, store); - } - } resolvedSessionId = sessionId; const agentId = resolveAgentIdFromSessionKey(requestedSessionKey); const mainSessionKey = resolveAgentMainSessionKey({ @@ -202,6 +196,15 @@ export const agentHandlers: GatewayRequestHandlers = { agentId, }); const rawMainKey = normalizeMainKey(cfg.session?.mainKey); + // Normalize short main key alias to canonical form before store write + const storeKey = + requestedSessionKey === rawMainKey ? mainSessionKey : requestedSessionKey; + if (store) { + store[storeKey] = nextEntry; + if (storePath) { + await saveSessionStore(storePath, store); + } + } if ( requestedSessionKey === mainSessionKey || requestedSessionKey === rawMainKey diff --git a/src/gateway/server-methods/chat.ts b/src/gateway/server-methods/chat.ts index ac9647182..3da182034 100644 --- a/src/gateway/server-methods/chat.ts +++ b/src/gateway/server-methods/chat.ts @@ -3,7 +3,15 @@ import { randomUUID } from "node:crypto"; import { resolveThinkingDefault } from "../../agents/model-selection.js"; import { resolveAgentTimeoutMs } from "../../agents/timeout.js"; import { agentCommand } from "../../commands/agent.js"; -import { mergeSessionEntry, saveSessionStore } from "../../config/sessions.js"; +import { + mergeSessionEntry, + resolveAgentMainSessionKey, + saveSessionStore, +} from "../../config/sessions.js"; +import { + normalizeMainKey, + resolveAgentIdFromSessionKey, +} from "../../routing/session-key.js"; import { registerAgentRunContext } from "../../infra/agent-events.js"; import { defaultRuntime } from "../../runtime.js"; import { resolveSendPolicy } from "../../sessions/send-policy.js"; @@ -306,8 +314,14 @@ export const chatHandlers: GatewayRequestHandlers = { clientRunId, }); + // Normalize short main key alias to canonical form before store write + const agentId = resolveAgentIdFromSessionKey(p.sessionKey); + const mainSessionKey = resolveAgentMainSessionKey({ cfg, agentId }); + const rawMainKey = normalizeMainKey(cfg.session?.mainKey); + const storeKey = + p.sessionKey === rawMainKey ? mainSessionKey : p.sessionKey; if (store) { - store[p.sessionKey] = sessionEntry; + store[storeKey] = sessionEntry; if (storePath) { await saveSessionStore(storePath, store); } diff --git a/src/gateway/session-utils.ts b/src/gateway/session-utils.ts index 9ddc90b8d..aacd004f0 100644 --- a/src/gateway/session-utils.ts +++ b/src/gateway/session-utils.ts @@ -15,6 +15,7 @@ import { buildGroupDisplayName, loadSessionStore, resolveAgentIdFromSessionKey, + resolveAgentMainSessionKey, resolveSessionTranscriptPath, resolveStorePath, type SessionEntry, @@ -172,7 +173,16 @@ export function loadSessionEntry(sessionKey: string) { const store = loadSessionStore(storePath); const parsed = parseAgentSessionKey(sessionKey); const legacyKey = parsed?.rest; - const entry = store[sessionKey] ?? (legacyKey ? store[legacyKey] : undefined); + // Also try the canonical key if sessionKey is the short mainKey alias + const rawMainKey = normalizeMainKey(sessionCfg?.mainKey); + const canonicalKey = + sessionKey === rawMainKey + ? resolveAgentMainSessionKey({ cfg, agentId }) + : undefined; + const entry = + store[sessionKey] ?? + (legacyKey ? store[legacyKey] : undefined) ?? + (canonicalKey ? store[canonicalKey] : undefined); return { cfg, storePath, store, entry }; }