Skip to content

Commit 756ec52

Browse files
committed
perf(gateway): share one node.list snapshot across session catalog providers
1 parent 1695854 commit 756ec52

11 files changed

Lines changed: 174 additions & 30 deletions

‎extensions/acpx/src/pi-session-catalog-plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ async function listPiHosts(
382382
}
383383
let nodes: Awaited<ReturnType<PluginRuntime["nodes"]["list"]>>["nodes"];
384384
try {
385-
nodes = (await runtime.nodes.list()).nodes;
385+
nodes = (await (query.listNodes?.() ?? runtime.nodes.list())).nodes;
386386
} catch {
387387
return hosts;
388388
}

‎extensions/acpx/src/pi-session-catalog.test.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -745,19 +745,20 @@ describe("Pi session catalog", () => {
745745
}),
746746
};
747747
const invoke = vi.fn().mockResolvedValue(page);
748+
const nodes = [
749+
{
750+
nodeId: "node-1",
751+
connected: true,
752+
commands: [PI_SESSIONS_LIST_COMMAND, PI_TERMINAL_RESUME_COMMAND],
753+
},
754+
];
755+
const runtimeListNodes = vi.fn().mockResolvedValue({ nodes });
756+
const requestListNodes = vi.fn().mockResolvedValue({ nodes });
748757
registerPiSessionCatalog({
749758
pluginConfig: {},
750759
runtime: {
751760
nodes: {
752-
list: vi.fn().mockResolvedValue({
753-
nodes: [
754-
{
755-
nodeId: "node-1",
756-
connected: true,
757-
commands: [PI_SESSIONS_LIST_COMMAND, PI_TERMINAL_RESUME_COMMAND],
758-
},
759-
],
760-
}),
761+
list: runtimeListNodes,
761762
invoke,
762763
},
763764
},
@@ -768,7 +769,13 @@ describe("Pi session catalog", () => {
768769
registerNodeInvokePolicy: vi.fn(),
769770
} as unknown as OpenClawPluginApi);
770771

771-
await expect(provider!.list({ hostIds: ["node:node-1"], search: "remote" })).resolves.toEqual([
772+
await expect(
773+
provider!.list({
774+
hostIds: ["node:node-1"],
775+
search: "remote",
776+
listNodes: requestListNodes,
777+
}),
778+
).resolves.toEqual([
772779
expect.objectContaining({
773780
sessions: [
774781
expect.objectContaining({
@@ -779,6 +786,8 @@ describe("Pi session catalog", () => {
779786
],
780787
}),
781788
]);
789+
expect(requestListNodes).toHaveBeenCalledOnce();
790+
expect(runtimeListNodes).not.toHaveBeenCalled();
782791
expect(invoke).toHaveBeenNthCalledWith(1, {
783792
nodeId: "node-1",
784793
command: PI_SESSIONS_LIST_COMMAND,

‎extensions/anthropic/session-catalog.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,20 +2261,33 @@ describe("Claude session catalog", () => {
22612261
await openGate;
22622262
return await originalOpen(...args);
22632263
});
2264-
const listNodes = vi.fn(async () => ({ nodes: [] }));
2264+
const runtimeListNodes = vi.fn(async () => ({ nodes: [] }));
2265+
const requestListNodes = vi.fn(async () => ({ nodes: [] }));
22652266
const provider = captureCatalogProvider({
2266-
nodes: { list: listNodes },
2267+
nodes: { list: runtimeListNodes },
22672268
} as unknown as PluginRuntime);
22682269

2269-
const listing = provider.list({});
2270+
const listing = provider.list({ listNodes: requestListNodes });
22702271
await opened;
2271-
expect(listNodes).toHaveBeenCalledOnce();
2272+
expect(requestListNodes).toHaveBeenCalledOnce();
2273+
expect(runtimeListNodes).not.toHaveBeenCalled();
22722274
releaseOpen();
22732275
await expect(listing).resolves.toMatchObject([
22742276
{ hostId: "gateway:local", sessions: [expect.objectContaining({ threadId: sessionId })] },
22752277
]);
22762278
});
22772279

2280+
it("falls back to the plugin node runtime without a request snapshot", async () => {
2281+
const runtimeListNodes = vi.fn(async () => ({ nodes: [] }));
2282+
const provider = captureCatalogProvider({
2283+
nodes: { list: runtimeListNodes },
2284+
} as unknown as PluginRuntime);
2285+
2286+
await expect(provider.list({ hostIds: ["node:missing"] })).resolves.toEqual([]);
2287+
2288+
expect(runtimeListNodes).toHaveBeenCalledOnce();
2289+
});
2290+
22782291
it("keeps the underlying paired-node list failure", async () => {
22792292
const runtime = {
22802293
nodes: {

‎extensions/anthropic/session-catalog.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ function parseGatewayQuery(value: unknown): {
11161116
async function listClaudeSessionCatalog(params: {
11171117
runtime: PluginRuntime;
11181118
query?: unknown;
1119+
listNodes?: Parameters<SessionCatalogProvider["list"]>[0]["listNodes"];
11191120
onHost?: (host: ClaudeSessionCatalogHost) => void;
11201121
}): Promise<ClaudeSessionCatalogResult> {
11211122
const query = parseGatewayQuery(params.query);
@@ -1165,7 +1166,7 @@ async function listClaudeSessionCatalog(params: {
11651166
}
11661167
let nodes: Awaited<ReturnType<PluginRuntime["nodes"]["list"]>>["nodes"];
11671168
try {
1168-
nodes = (await params.runtime.nodes.list()).nodes;
1169+
nodes = (await (params.listNodes?.() ?? params.runtime.nodes.list())).nodes;
11691170
} catch (error) {
11701171
const registryHost: ClaudeSessionCatalogHost = {
11711172
hostId: "node:registry",
@@ -1598,12 +1599,13 @@ export function registerClaudeSessionCatalog(api: OpenClawPluginApi): void {
15981599
list: async (query) => {
15991600
const adopted = listBoundClaudeSessions(api, query.sessionEntries);
16001601
const localCliAvailable = catalogTerminal.isClaudeCliAvailable();
1601-
const { onHost, sessionEntries: _sessionEntries, ...gatewayQuery } = query;
1602+
const { listNodes, onHost, sessionEntries: _sessionEntries, ...gatewayQuery } = query;
16021603
const mapHost = (host: ClaudeSessionCatalogHost) =>
16031604
toGenericClaudeHost(host, adopted, localCliAvailable);
16041605
const result = await listClaudeSessionCatalog({
16051606
runtime: api.runtime,
16061607
query: gatewayQuery,
1608+
listNodes,
16071609
...(onHost ? { onHost: (host) => onHost(mapHost(host)) } : {}),
16081610
});
16091611
return result.hosts.map(mapHost);

‎extensions/codex/src/session-catalog.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,29 @@ describe("Codex supervision catalog", () => {
13881388
expect(runtime.nodes.list).not.toHaveBeenCalled();
13891389
});
13901390

1391+
it("prefers the request node snapshot and retains the plugin runtime fallback", async () => {
1392+
const control = createControl();
1393+
const { runtime } = createRuntime();
1394+
const { api, getProvider } = createGatewayApi(runtime);
1395+
registerCodexSessionCatalog({
1396+
api,
1397+
bindingStore: createCodexTestBindingStore(),
1398+
control,
1399+
getRuntimeConfig: () => config,
1400+
});
1401+
const provider = getProvider();
1402+
const requestListNodes = vi.fn(async () => ({ nodes: [] }));
1403+
1404+
await expect(
1405+
provider?.list({ hostIds: ["node:missing"], listNodes: requestListNodes }),
1406+
).resolves.toEqual([]);
1407+
expect(requestListNodes).toHaveBeenCalledOnce();
1408+
expect(runtime.nodes.list).not.toHaveBeenCalled();
1409+
1410+
await expect(provider?.list({ hostIds: ["node:missing"] })).resolves.toEqual([]);
1411+
expect(runtime.nodes.list).toHaveBeenCalledOnce();
1412+
});
1413+
13911414
it("enriches only the local source row with its adopted OpenClaw session", async () => {
13921415
const control = createControl({
13931416
listPage: vi.fn(async () => ({

‎extensions/codex/src/session-catalog.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ async function listCodexSessionCatalog(params: {
422422
runtime: PluginRuntime;
423423
control: CodexSessionCatalogControl;
424424
query?: CodexSessionCatalogParams;
425+
listNodes?: Parameters<SessionCatalogProvider["list"]>[0]["listNodes"];
425426
onHost?: (host: CodexSessionCatalogHost) => void;
426427
sessionEntries?: SessionCatalogEntrySnapshot;
427428
}): Promise<CodexSessionCatalogResult> {
@@ -452,7 +453,7 @@ async function listCodexSessionCatalog(params: {
452453
}
453454
let nodes: CatalogNode[];
454455
try {
455-
nodes = (await params.runtime.nodes.list()).nodes
456+
nodes = (await (params.listNodes?.() ?? params.runtime.nodes.list())).nodes
456457
.filter(
457458
(node) =>
458459
node.commands?.includes(CODEX_APP_SERVER_THREADS_LIST_COMMAND) &&
@@ -1273,7 +1274,7 @@ function registerCodexSessionCatalog(params: {
12731274
),
12741275
list: async (query) => {
12751276
const localTerminalAvailable = resolveLocalCodexTerminalExecutable() !== undefined;
1276-
const { onHost, sessionEntries, ...gatewayQuery } = query;
1277+
const { listNodes, onHost, sessionEntries, ...gatewayQuery } = query;
12771278
const mapHost = (host: CodexSessionCatalogHost) =>
12781279
toGenericCatalogHost(host, localTerminalAvailable);
12791280
return (
@@ -1283,6 +1284,7 @@ function registerCodexSessionCatalog(params: {
12831284
runtime: params.api.runtime,
12841285
control: params.control,
12851286
query: gatewayQuery,
1287+
listNodes,
12861288
sessionEntries,
12871289
...(onHost ? { onHost: (host) => onHost(mapHost(host)) } : {}),
12881290
})

‎extensions/opencode/session-catalog-plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ async function listOpenCodeHosts(
374374
}
375375
let nodes: Awaited<ReturnType<PluginRuntime["nodes"]["list"]>>["nodes"];
376376
try {
377-
nodes = (await runtime.nodes.list()).nodes;
377+
nodes = (await (query.listNodes?.() ?? runtime.nodes.list())).nodes;
378378
} catch {
379379
return hosts;
380380
}

‎extensions/opencode/session-catalog.test.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -688,19 +688,20 @@ describe("OpenCode session catalog", () => {
688688
}),
689689
};
690690
const invoke = vi.fn().mockResolvedValue(page);
691+
const nodes = [
692+
{
693+
nodeId: "node-1",
694+
connected: true,
695+
commands: [OPENCODE_SESSIONS_LIST_COMMAND, OPENCODE_TERMINAL_RESUME_COMMAND],
696+
},
697+
];
698+
const runtimeListNodes = vi.fn().mockResolvedValue({ nodes });
699+
const requestListNodes = vi.fn().mockResolvedValue({ nodes });
691700
registerOpenCodeSessionCatalog({
692701
pluginConfig: {},
693702
runtime: {
694703
nodes: {
695-
list: vi.fn().mockResolvedValue({
696-
nodes: [
697-
{
698-
nodeId: "node-1",
699-
connected: true,
700-
commands: [OPENCODE_SESSIONS_LIST_COMMAND, OPENCODE_TERMINAL_RESUME_COMMAND],
701-
},
702-
],
703-
}),
704+
list: runtimeListNodes,
704705
invoke,
705706
},
706707
},
@@ -711,7 +712,13 @@ describe("OpenCode session catalog", () => {
711712
registerNodeInvokePolicy: vi.fn(),
712713
} as unknown as OpenClawPluginApi);
713714

714-
await expect(provider!.list({ hostIds: ["node:node-1"], search: "remote" })).resolves.toEqual([
715+
await expect(
716+
provider!.list({
717+
hostIds: ["node:node-1"],
718+
search: "remote",
719+
listNodes: requestListNodes,
720+
}),
721+
).resolves.toEqual([
715722
expect.objectContaining({
716723
sessions: [
717724
expect.objectContaining({
@@ -722,6 +729,8 @@ describe("OpenCode session catalog", () => {
722729
],
723730
}),
724731
]);
732+
expect(requestListNodes).toHaveBeenCalledOnce();
733+
expect(runtimeListNodes).not.toHaveBeenCalled();
725734
expect(invoke).toHaveBeenNthCalledWith(1, {
726735
nodeId: "node-1",
727736
command: OPENCODE_SESSIONS_LIST_COMMAND,

‎src/gateway/server-methods/session-catalog.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,72 @@ describe("session catalog Gateway methods", () => {
350350
});
351351
});
352352

353+
it("shares one lazy Gateway node snapshot across catalog providers", async () => {
354+
const previousNodesRuntime = gatewaySubagentState.nodes;
355+
const dispatchNodeList = vi.fn(async () => ({
356+
nodes: [{ nodeId: "shared-node", connected: true }],
357+
}));
358+
gatewaySubagentState.nodes = {
359+
list: dispatchNodeList,
360+
invoke: vi.fn(async () => undefined),
361+
};
362+
try {
363+
const catalogUsingNodes = (id: string) =>
364+
provider(id, {
365+
list: vi.fn(async ({ listNodes }) => {
366+
expect(await listNodes?.()).toEqual({
367+
nodes: [{ nodeId: "shared-node", connected: true }],
368+
});
369+
return [];
370+
}),
371+
});
372+
hoisted.activeRegistry.sessionCatalogs = [
373+
{ provider: catalogUsingNodes("zeta") },
374+
{ provider: catalogUsingNodes("alpha") },
375+
];
376+
377+
await call("sessions.catalog.list", {});
378+
379+
expect(dispatchNodeList).toHaveBeenCalledOnce();
380+
} finally {
381+
gatewaySubagentState.nodes = previousNodesRuntime;
382+
}
383+
});
384+
385+
it("keeps catalog-filtered Gateway node snapshots lazy", async () => {
386+
const previousNodesRuntime = gatewaySubagentState.nodes;
387+
const dispatchNodeList = vi.fn(async () => ({ nodes: [] }));
388+
gatewaySubagentState.nodes = {
389+
list: dispatchNodeList,
390+
invoke: vi.fn(async () => undefined),
391+
};
392+
try {
393+
const selectedList = vi.fn(async () => []);
394+
hoisted.activeRegistry.sessionCatalogs = [
395+
{
396+
provider: provider("selected", { list: selectedList }),
397+
},
398+
{
399+
provider: provider("unselected", {
400+
list: vi.fn(async ({ listNodes }) => {
401+
await listNodes?.();
402+
return [];
403+
}),
404+
}),
405+
},
406+
];
407+
408+
await call("sessions.catalog.list", { catalogId: "selected" });
409+
410+
expect(selectedList).toHaveBeenCalledWith(
411+
expect.objectContaining({ listNodes: expect.any(Function) }),
412+
);
413+
expect(dispatchNodeList).not.toHaveBeenCalled();
414+
} finally {
415+
gatewaySubagentState.nodes = previousNodesRuntime;
416+
}
417+
});
418+
353419
it("uses the pinned Gateway catalog runtime after active registry churn", async () => {
354420
const previousNodesRuntime = gatewaySubagentState.nodes;
355421
const listNodes = vi.fn(async () => ({ nodes: [] }));

‎src/gateway/server-methods/session-catalog.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import {
1414
validateSessionsCatalogReadParams,
1515
} from "../../../packages/gateway-protocol/src/index.js";
1616
import { getActivePluginSessionExtensionRegistry } from "../../plugins/runtime.js";
17+
import { gatewaySubagentState } from "../../plugins/runtime/gateway-bindings.js";
1718
import type {
1819
SessionCatalogCreateTarget,
20+
SessionCatalogListProviderParams,
1921
SessionCatalogProvider,
2022
} from "../../plugins/session-catalog.js";
2123
import { bindPluginSessionConversation } from "../../plugins/session-conversation-binding.js";
@@ -29,6 +31,20 @@ import { assertValidParams } from "./validation.js";
2931

3032
const SESSION_CATALOG_SEARCH_MAX_UTF16_UNITS = 500;
3133

34+
function createSessionCatalogRequestNodeSnapshot(): NonNullable<
35+
SessionCatalogListProviderParams["listNodes"]
36+
> {
37+
let request: ReturnType<NonNullable<SessionCatalogListProviderParams["listNodes"]>> | undefined;
38+
return () => {
39+
// Every provider sees the same promise so one catalog request cannot multiply the
40+
// pairing-store scans performed by the Gateway node.list runtime.
41+
request ??=
42+
gatewaySubagentState.nodes?.list() ??
43+
Promise.reject(new Error("Plugin node runtime is only available inside the Gateway."));
44+
return request;
45+
};
46+
}
47+
3248
function normalizeSessionCatalogSearch(search: string | undefined): string | undefined {
3349
const normalized = normalizeOptionalString(search);
3450
return normalized
@@ -204,6 +220,7 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
204220
cfg: config,
205221
fallbackAgentId: resolvedAgent.agentId,
206222
});
223+
const listNodes = createSessionCatalogRequestNodeSnapshot();
207224
const catalogList = await Promise.all(
208225
selected.map(async (provider): Promise<SessionCatalog> => {
209226
const createTarget = resolveProviderCreateTarget(provider, resolvedAgent.agentId);
@@ -236,6 +253,7 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
236253
hostIds: request.hostIds,
237254
...(request.cursors !== undefined ? { cursors: request.cursors } : {}),
238255
sessionEntries: requestEntries.sessionEntries,
256+
listNodes,
239257
...(onHost ? { onHost } : {}),
240258
});
241259
return catalogResult(

0 commit comments

Comments
 (0)