Skip to content

Commit 23ae624

Browse files
fix(gateway): reuse subagent registry snapshot in session listing
1 parent e345f76 commit 23ae624

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

src/gateway/session-utils.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ function resolveRuntimeChildSessionKeys(
381381
): string[] | undefined {
382382
const childSessionKeys = new Set<string>();
383383
const controllerKey = controllerSessionKey.trim();
384-
const runs =
385-
subagentRuns?.runsByControllerSessionKey.get(controllerKey) ??
386-
listSubagentRunsForController(controllerSessionKey);
384+
const runs = subagentRuns
385+
? (subagentRuns.runsByControllerSessionKey.get(controllerKey) ?? [])
386+
: listSubagentRunsForController(controllerSessionKey);
387387
for (const entry of runs) {
388388
const childSessionKey = normalizeOptionalString(entry.childSessionKey);
389389
if (!childSessionKey) {
@@ -1919,11 +1919,21 @@ export function listSessionsFromStore(params: {
19191919
const now = Date.now();
19201920
const sessionListTranscriptUsageMaxBytes = 64 * 1024;
19211921
const sessionListTranscriptFieldRows = 100;
1922-
const rowContext = buildSessionListRowContext({ store, now });
1922+
let rowContext: SessionListRowContext | undefined;
1923+
const getRowContext = () => {
1924+
rowContext ??= buildSessionListRowContext({ store, now });
1925+
return rowContext;
1926+
};
19231927
const includeDerivedTitles = opts.includeDerivedTitles === true;
19241928
const includeLastMessage = opts.includeLastMessage === true;
1929+
const hasSpawnedByFilter = typeof opts.spawnedBy === "string" && opts.spawnedBy.length > 0;
19251930

1926-
const entries = filterAndSortSessionEntries({ store, opts, now, rowContext });
1931+
const entries = filterAndSortSessionEntries({
1932+
store,
1933+
opts,
1934+
now,
1935+
rowContext: hasSpawnedByFilter ? getRowContext() : undefined,
1936+
});
19271937

19281938
const sessions = entries.map(([key, entry], index) => {
19291939
const includeTranscriptFields = index < sessionListTranscriptFieldRows;
@@ -1938,8 +1948,8 @@ export function listSessionsFromStore(params: {
19381948
includeDerivedTitles: includeTranscriptFields && includeDerivedTitles,
19391949
includeLastMessage: includeTranscriptFields && includeLastMessage,
19401950
transcriptUsageMaxBytes: sessionListTranscriptUsageMaxBytes,
1941-
storeChildSessionsByKey: rowContext.storeChildSessionsByKey,
1942-
rowContext,
1951+
storeChildSessionsByKey: getRowContext().storeChildSessionsByKey,
1952+
rowContext: getRowContext(),
19431953
});
19441954
});
19451955

@@ -1973,11 +1983,21 @@ export async function listSessionsFromStoreAsync(params: {
19731983
const now = Date.now();
19741984
const sessionListTranscriptUsageMaxBytes = 64 * 1024;
19751985
const sessionListTranscriptFieldRows = 100;
1976-
const rowContext = buildSessionListRowContext({ store, now });
1986+
let rowContext: SessionListRowContext | undefined;
1987+
const getRowContext = () => {
1988+
rowContext ??= buildSessionListRowContext({ store, now });
1989+
return rowContext;
1990+
};
19771991
const includeDerivedTitles = opts.includeDerivedTitles === true;
19781992
const includeLastMessage = opts.includeLastMessage === true;
1993+
const hasSpawnedByFilter = typeof opts.spawnedBy === "string" && opts.spawnedBy.length > 0;
19791994

1980-
const entries = filterAndSortSessionEntries({ store, opts, now, rowContext });
1995+
const entries = filterAndSortSessionEntries({
1996+
store,
1997+
opts,
1998+
now,
1999+
rowContext: hasSpawnedByFilter ? getRowContext() : undefined,
2000+
});
19812001

19822002
const sessions: GatewaySessionRow[] = [];
19832003
for (let i = 0; i < entries.length; i++) {
@@ -1994,8 +2014,8 @@ export async function listSessionsFromStoreAsync(params: {
19942014
includeDerivedTitles: false,
19952015
includeLastMessage: false,
19962016
transcriptUsageMaxBytes: sessionListTranscriptUsageMaxBytes,
1997-
storeChildSessionsByKey: rowContext.storeChildSessionsByKey,
1998-
rowContext,
2017+
storeChildSessionsByKey: getRowContext().storeChildSessionsByKey,
2018+
rowContext: getRowContext(),
19992019
});
20002020
if (
20012021
entry?.sessionId &&

0 commit comments

Comments
 (0)