fix: prefer deterministic transcript session keys

This commit is contained in:
Tak Hoffman
2026-03-24 21:30:54 -05:00
parent 7a7e4cd4c4
commit 3c46e0307a
2 changed files with 31 additions and 2 deletions

View File

@@ -113,4 +113,18 @@ describe("resolveSessionKeyForTranscriptFile", () => {
expect(resolveSessionKeyForTranscriptFile(" ")).toBeUndefined();
expect(loadCombinedSessionStoreForGatewayMock).not.toHaveBeenCalled();
});
it("prefers the deterministic session key when duplicate sessionIds share a transcript path", () => {
const store = {
"agent:other:main": { sessionId: "run-dup", updatedAt: now + 1 },
"agent:main:acp:run-dup": { sessionId: "run-dup", updatedAt: now },
} satisfies Record<string, SessionEntry>;
loadCombinedSessionStoreForGatewayMock.mockReturnValue({
storePath: "(multiple)",
store,
});
resolveSessionTranscriptCandidatesMock.mockReturnValue(["/tmp/shared.jsonl"]);
expect(resolveSessionKeyForTranscriptFile("/tmp/shared.jsonl")).toBe("agent:main:acp:run-dup");
});
});

View File

@@ -3,6 +3,7 @@ import path from "node:path";
import { loadConfig } from "../config/config.js";
import type { SessionEntry } from "../config/sessions/types.js";
import { normalizeAgentId } from "../routing/session-key.js";
import { resolvePreferredSessionKeyForSessionIdMatches } from "../sessions/session-id-resolution.js";
import {
loadCombinedSessionStoreForGateway,
resolveGatewaySessionStoreTarget,
@@ -74,6 +75,7 @@ export function resolveSessionKeyForTranscriptFile(sessionFile: string): string
return cachedKey;
}
const matchingEntries: Array<[string, SessionEntry]> = [];
for (const [key, entry] of Object.entries(store)) {
if (!entry?.sessionId || key === cachedKey) {
continue;
@@ -86,8 +88,21 @@ export function resolveSessionKeyForTranscriptFile(sessionFile: string): string
targetPath,
})
) {
TRANSCRIPT_SESSION_KEY_CACHE.set(targetPath, key);
return key;
matchingEntries.push([key, entry]);
}
}
if (matchingEntries.length > 0) {
const firstSessionId = matchingEntries[0]?.[1].sessionId;
const sameSessionMatches = matchingEntries.filter(
(entry): entry is [string, SessionEntry] => entry[1].sessionId === firstSessionId,
);
const resolvedKey =
resolvePreferredSessionKeyForSessionIdMatches(sameSessionMatches, firstSessionId) ??
matchingEntries[0]?.[0];
if (resolvedKey) {
TRANSCRIPT_SESSION_KEY_CACHE.set(targetPath, resolvedKey);
return resolvedKey;
}
}