diff --git a/src/server/proxy-core/runtime/codexSessionResponseStore.test.ts b/src/server/proxy-core/runtime/codexSessionResponseStore.test.ts index 6543758..ba25742 100644 --- a/src/server/proxy-core/runtime/codexSessionResponseStore.test.ts +++ b/src/server/proxy-core/runtime/codexSessionResponseStore.test.ts @@ -126,4 +126,32 @@ describe('codexSessionResponseStore', () => { resetCodexSessionResponseStore(); }); + + it('keeps the latest continuation id when the same downstream session drifts back to an earlier scope', () => { + resetCodexSessionResponseStore(); + + const originalScopedKey = buildCodexSessionResponseStoreKey({ + sessionId: 'session-roundtrip', + siteId: 10, + accountId: 20, + channelId: 30, + }); + const driftedScopedKey = buildCodexSessionResponseStoreKey({ + sessionId: 'session-roundtrip', + siteId: 10, + accountId: 21, + channelId: 31, + }); + + setCodexSessionResponseId(originalScopedKey, 'resp-roundtrip-1'); + + expect(getCodexSessionResponseId(driftedScopedKey)).toBe('resp-roundtrip-1'); + + setCodexSessionResponseId(driftedScopedKey, 'resp-roundtrip-2'); + + expect(getCodexSessionResponseId(driftedScopedKey)).toBe('resp-roundtrip-2'); + expect(getCodexSessionResponseId(originalScopedKey)).toBe('resp-roundtrip-2'); + + resetCodexSessionResponseStore(); + }); }); diff --git a/src/server/proxy-core/runtime/codexSessionResponseStore.ts b/src/server/proxy-core/runtime/codexSessionResponseStore.ts index 7baff1b..487b6f8 100644 --- a/src/server/proxy-core/runtime/codexSessionResponseStore.ts +++ b/src/server/proxy-core/runtime/codexSessionResponseStore.ts @@ -57,6 +57,16 @@ function getSessionStoreKeys(sessionId: string): string[] { ]; } +function reconcileScopedSessionFallback(bareSessionKey: string): void { + if (!bareSessionKey) return; + + for (const key of codexSessionResponseIds.keys()) { + if (key === bareSessionKey) continue; + if (getBareSessionStoreKey(key) !== bareSessionKey) continue; + codexSessionResponseIds.delete(key); + } +} + function normalizeSessionId(sessionId: string): string { return sessionId.trim(); } @@ -87,7 +97,10 @@ export function getCodexSessionResponseId(sessionId: string): string | null { for (const fallbackKey of getFallbackSessionStoreKeys(normalized)) { const fallback = codexSessionResponseIds.get(fallbackKey); - if (fallback) return fallback; + if (fallback) { + reconcileScopedSessionFallback(fallbackKey); + return fallback; + } } return null;