diff --git a/src/config/sessions.ts b/src/config/sessions.ts index 922df7461..d94e10dd1 100644 --- a/src/config/sessions.ts +++ b/src/config/sessions.ts @@ -133,6 +133,17 @@ export type SessionEntry = { skillsSnapshot?: SessionSkillSnapshot; }; +export function mergeSessionEntry( + existing: SessionEntry | undefined, + patch: Partial, +): SessionEntry { + const sessionId = + patch.sessionId ?? existing?.sessionId ?? crypto.randomUUID(); + const updatedAt = patch.updatedAt ?? existing?.updatedAt ?? Date.now(); + if (!existing) return { ...patch, sessionId, updatedAt }; + return { ...existing, ...patch, sessionId, updatedAt }; +} + export type GroupKeyResolution = { key: string; legacyKey?: string; @@ -487,17 +498,14 @@ export async function updateLastRoute(params: { const store = loadSessionStore(storePath); const existing = store[sessionKey]; const now = Date.now(); - const sessionId = existing?.sessionId ?? crypto.randomUUID(); - const next: SessionEntry = { - ...(existing ?? { sessionId, updatedAt: 0 }), - sessionId, + const next = mergeSessionEntry(existing, { updatedAt: Math.max(existing?.updatedAt ?? 0, now), lastProvider: provider, lastTo: to?.trim() ? to.trim() : undefined, lastAccountId: accountId?.trim() ? accountId.trim() : existing?.lastAccountId, - }; + }); store[sessionKey] = next; await saveSessionStore(storePath, store); return next; diff --git a/src/gateway/server-bridge.ts b/src/gateway/server-bridge.ts index bd1753362..56ac4bf55 100644 --- a/src/gateway/server-bridge.ts +++ b/src/gateway/server-bridge.ts @@ -23,6 +23,7 @@ import { import { buildConfigSchema } from "../config/schema.js"; import { loadSessionStore, + mergeSessionEntry, resolveMainSessionKeyFromConfig, type SessionEntry, saveSessionStore, @@ -810,16 +811,10 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) { }); const now = Date.now(); const sessionId = entry?.sessionId ?? randomUUID(); - const sessionEntry: SessionEntry = { + const sessionEntry = mergeSessionEntry(entry, { sessionId, updatedAt: now, - thinkingLevel: entry?.thinkingLevel, - verboseLevel: entry?.verboseLevel, - reasoningLevel: entry?.reasoningLevel, - systemSent: entry?.systemSent, - lastProvider: entry?.lastProvider, - lastTo: entry?.lastTo, - }; + }); const clientRunId = p.idempotencyKey; registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey }); diff --git a/src/gateway/server-methods/chat.ts b/src/gateway/server-methods/chat.ts index 1dca7c8f1..f0f36cbaf 100644 --- a/src/gateway/server-methods/chat.ts +++ b/src/gateway/server-methods/chat.ts @@ -3,7 +3,7 @@ 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 { type SessionEntry, saveSessionStore } from "../../config/sessions.js"; +import { mergeSessionEntry, saveSessionStore } from "../../config/sessions.js"; import { registerAgentRunContext } from "../../infra/agent-events.js"; import { defaultRuntime } from "../../runtime.js"; import { resolveSendPolicy } from "../../sessions/send-policy.js"; @@ -197,17 +197,10 @@ export const chatHandlers: GatewayRequestHandlers = { }); const now = Date.now(); const sessionId = entry?.sessionId ?? randomUUID(); - const sessionEntry: SessionEntry = { + const sessionEntry = mergeSessionEntry(entry, { sessionId, updatedAt: now, - thinkingLevel: entry?.thinkingLevel, - verboseLevel: entry?.verboseLevel, - reasoningLevel: entry?.reasoningLevel, - systemSent: entry?.systemSent, - sendPolicy: entry?.sendPolicy, - lastProvider: entry?.lastProvider, - lastTo: entry?.lastTo, - }; + }); const clientRunId = p.idempotencyKey; registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });