From 16ddbbc6281060eee27ef64f43d251c221365dcf Mon Sep 17 00:00:00 2001 From: Operative-001 Date: Mon, 16 Feb 2026 11:41:07 +0100 Subject: [PATCH] fix(sessions): skip cache when initializing session state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #17971 When initSessionState() reads the session store, use skipCache: true to ensure fresh data from disk. The session store cache is process-local and uses mtime-based invalidation, which can fail in these scenarios: 1. Multiple gateway processes (each has separate in-memory cache) 2. Windows file system where mtime granularity may miss rapid writes 3. Race conditions between messages 6-8 seconds apart Symptoms: 134+ orphaned .jsonl transcript files, each with only 1 exchange. Session rotates on every incoming message even when sessionKey is stable. Root cause: loadSessionStore() returns stale cache → entry not found for sessionKey → new sessionId generated → new transcript file. The fix ensures session identity (sessionId) is always resolved from the latest on-disk state, not potentially-stale cache. --- src/auto-reply/reply/session.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/auto-reply/reply/session.ts b/src/auto-reply/reply/session.ts index 0a0594ddd9..4d08ddb061 100644 --- a/src/auto-reply/reply/session.ts +++ b/src/auto-reply/reply/session.ts @@ -126,7 +126,11 @@ export async function initSessionState(params: { const sessionScope = sessionCfg?.scope ?? "per-sender"; const storePath = resolveStorePath(sessionCfg?.store, { agentId }); - const sessionStore: Record = loadSessionStore(storePath); + // CRITICAL: Skip cache to ensure fresh data when resolving session identity. + // Stale cache (especially with multiple gateway processes or on Windows where + // mtime granularity may miss rapid writes) can cause incorrect sessionId + // generation, leading to orphaned transcript files. See #17971. + const sessionStore: Record = loadSessionStore(storePath, { skipCache: true }); let sessionKey: string | undefined; let sessionEntry: SessionEntry;