Skip to content

Commit 4e47c4c

Browse files
authored
refactor(gateway): consolidate archived transcript reads (#117343)
1 parent a88f3ff commit 4e47c4c

8 files changed

Lines changed: 584 additions & 904 deletions

‎src/gateway/session-transcript-anchor-reader.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
toTranscriptReadScope,
99
type ReadRecentSessionMessagesResult,
1010
} from "./session-transcript-readers.js";
11-
import { readSessionMessagesAroundIdWithStatsAsync as readSessionMessagesAroundIdWithStatsAsyncFile } from "./session-utils.fs-anchor.js";
11+
import { ArchivedTranscriptReader } from "./session-utils.fs.js";
1212

1313
type ReadSessionMessagesAroundIdResult = ReadRecentSessionMessagesResult & {
1414
found: boolean;
@@ -31,13 +31,12 @@ export async function readSessionMessagesAroundIdWithStatsAsync(
3131
const page = readSessionTranscriptMessageAnchorPage(toTranscriptReadScope(target), opts);
3232
if (!page.found) {
3333
if (opts.allowResetArchiveFallback === true) {
34-
return await readSessionMessagesAroundIdWithStatsAsyncFile(
35-
target.sessionId,
36-
target.storePath,
34+
return await new ArchivedTranscriptReader({
35+
agentId: target.agentId,
3736
sessionFile,
38-
opts,
39-
target.agentId,
40-
);
37+
sessionId: target.sessionId,
38+
storePath: target.storePath,
39+
}).readAroundId({ ...opts, resetArchiveOnly: true });
4140
}
4241
return {
4342
found: false,

‎src/gateway/session-transcript-files.fs.ts‎

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export type ArchivedSessionTranscript = {
2828
};
2929

3030
const MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES = 2048;
31-
const MAX_RESET_ARCHIVE_HEADER_MATCH_CACHE_ENTRIES = 4096;
3231
const MAX_RESET_ARCHIVE_CANDIDATES_PER_TRANSCRIPT = 128;
3332

3433
const resetArchiveDiscoveryCache = new Map<
@@ -39,63 +38,8 @@ const resetArchiveDiscoveryCache = new Map<
3938
archives: ResetArchiveCandidate[];
4039
}
4140
>();
42-
const resetArchiveHeaderMatchCache = new Map<
43-
string,
44-
{
45-
mtimeMs: number;
46-
size: number;
47-
matches: boolean;
48-
}
49-
>();
50-
5141
function clearSessionTranscriptResetArchiveDiscoveryCache(): void {
5242
resetArchiveDiscoveryCache.clear();
53-
resetArchiveHeaderMatchCache.clear();
54-
}
55-
56-
function deleteResetArchiveHeaderMatchesForArchives(archives: ResetArchiveCandidate[]): void {
57-
if (archives.length === 0 || resetArchiveHeaderMatchCache.size === 0) {
58-
return;
59-
}
60-
const archivePaths = new Set(archives.map((archive) => archive.archivePath));
61-
for (const cacheKey of resetArchiveHeaderMatchCache.keys()) {
62-
const archivePath = cacheKey.slice(cacheKey.indexOf("\0") + 1);
63-
if (archivePaths.has(archivePath)) {
64-
resetArchiveHeaderMatchCache.delete(cacheKey);
65-
}
66-
}
67-
}
68-
69-
function setResetArchiveDiscoveryCacheEntry(
70-
cacheKey: string,
71-
entry: { dirMtimeMs: number; dirSize: number; archives: ResetArchiveCandidate[] },
72-
): void {
73-
resetArchiveDiscoveryCache.set(cacheKey, entry);
74-
while (resetArchiveDiscoveryCache.size > MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES) {
75-
const oldestKey = resetArchiveDiscoveryCache.keys().next().value;
76-
if (typeof oldestKey !== "string") {
77-
break;
78-
}
79-
const oldestEntry = resetArchiveDiscoveryCache.get(oldestKey);
80-
if (oldestEntry) {
81-
deleteResetArchiveHeaderMatchesForArchives(oldestEntry.archives);
82-
}
83-
resetArchiveDiscoveryCache.delete(oldestKey);
84-
}
85-
}
86-
87-
function setResetArchiveHeaderMatchCacheEntry(
88-
cacheKey: string,
89-
entry: { mtimeMs: number; size: number; matches: boolean },
90-
): void {
91-
resetArchiveHeaderMatchCache.set(cacheKey, entry);
92-
while (resetArchiveHeaderMatchCache.size > MAX_RESET_ARCHIVE_HEADER_MATCH_CACHE_ENTRIES) {
93-
const oldestKey = resetArchiveHeaderMatchCache.keys().next().value;
94-
if (typeof oldestKey !== "string") {
95-
break;
96-
}
97-
resetArchiveHeaderMatchCache.delete(oldestKey);
98-
}
9943
}
10044

10145
function classifySessionTranscriptCandidate(
@@ -193,15 +137,6 @@ async function resetArchiveHeaderMatchesSessionId(
193137
if (!stat?.isFile()) {
194138
return false;
195139
}
196-
const cacheKey = `${sessionId}\0${archivePath}`;
197-
const cached = resetArchiveHeaderMatchCache.get(cacheKey);
198-
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
199-
resetArchiveHeaderMatchCache.delete(cacheKey);
200-
resetArchiveHeaderMatchCache.set(cacheKey, cached);
201-
return cached.matches;
202-
}
203-
204-
let matches = false;
205140
const handle = await fs.promises.open(probePath, "r").catch(() => null);
206141
if (!handle) {
207142
return false;
@@ -216,24 +151,19 @@ async function resetArchiveHeaderMatchesSessionId(
216151
continue;
217152
}
218153
const record = JSON.parse(trimmed) as unknown;
219-
matches =
154+
return (
220155
Boolean(record) &&
221156
typeof record === "object" &&
222157
!Array.isArray(record) &&
223158
(record as { type?: unknown; id?: unknown }).type === "session" &&
224-
(record as { type?: unknown; id?: unknown }).id === sessionId;
225-
return matches;
159+
(record as { type?: unknown; id?: unknown }).id === sessionId
160+
);
226161
}
227162
return false;
228163
} catch {
229164
return false;
230165
} finally {
231166
await handle.close().catch(() => undefined);
232-
setResetArchiveHeaderMatchCacheEntry(cacheKey, {
233-
mtimeMs: stat.mtimeMs,
234-
size: stat.size,
235-
matches,
236-
});
237167
}
238168
}
239169

@@ -276,11 +206,14 @@ async function listResetArchiveCandidatesForTranscriptAsync(
276206
(left, right) => right.timestamp - left.timestamp || right.name.localeCompare(left.name),
277207
);
278208
const boundedArchives = archives.slice(0, MAX_RESET_ARCHIVE_CANDIDATES_PER_TRANSCRIPT);
279-
setResetArchiveDiscoveryCacheEntry(cacheKey, {
209+
resetArchiveDiscoveryCache.set(cacheKey, {
280210
dirMtimeMs: dirStat.mtimeMs,
281211
dirSize: dirStat.size,
282212
archives: boundedArchives,
283213
});
214+
if (resetArchiveDiscoveryCache.size > MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES) {
215+
resetArchiveDiscoveryCache.delete(resetArchiveDiscoveryCache.keys().next().value ?? "");
216+
}
284217
return boundedArchives;
285218
}
286219

0 commit comments

Comments
 (0)