fix(sessions): skip cache when initializing session state

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.
This commit is contained in:
Operative-001
2026-02-16 11:41:07 +01:00
committed by Peter Steinberger
parent 5b3873add4
commit 16ddbbc628

View File

@@ -126,7 +126,11 @@ export async function initSessionState(params: {
const sessionScope = sessionCfg?.scope ?? "per-sender";
const storePath = resolveStorePath(sessionCfg?.store, { agentId });
const sessionStore: Record<string, SessionEntry> = 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<string, SessionEntry> = loadSessionStore(storePath, { skipCache: true });
let sessionKey: string | undefined;
let sessionEntry: SessionEntry;