@@ -26,6 +26,7 @@ import { bindPluginSessionConversation } from "../../plugins/session-conversatio
2626import { resolveAgentIdFromSessionKey } from "../../routing/session-key.js" ;
2727import { recordSessionStateEvent } from "../../sessions/session-state-events.js" ;
2828import { upsertSessionUpstreamLink } from "../../sessions/session-upstream-links.js" ;
29+ import type { GatewayBroadcastToConnIdsFn } from "../server-broadcast-types.js" ;
2930import { resolveAgentIdOrRespondError } from "./agent-id-shared.js" ;
3031import { createSessionCatalogRequestEntrySnapshot } from "./session-catalog-entry-snapshot.js" ;
3132import type { GatewayRequestHandlers , RespondFn } from "./types.js" ;
@@ -129,8 +130,15 @@ const providerCreateTargetsByConfig = new WeakMap<
129130
130131type CatalogListResult = { catalogs : SessionCatalog [ ] } ;
131132
133+ type CatalogListProgressSubscriber = {
134+ broadcastToConnIds : GatewayBroadcastToConnIdsFn ;
135+ connId : string ;
136+ progressId : string ;
137+ } ;
138+
132139type CatalogListCacheEntry = {
133140 expiresAt ?: number ;
141+ progressSubscribers : Map < string , CatalogListProgressSubscriber > ;
134142 result : Promise < CatalogListResult > ;
135143} ;
136144
@@ -350,8 +358,15 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
350358 const cache = catalogListCache ( config , catalogRegistrations ) ;
351359 const cached = cache . get ( listKey ) ;
352360 if ( cached && ( cached . expiresAt === undefined || cached . expiresAt > Date . now ( ) ) ) {
353- // progressId is connection-owned and excluded from the work key. Followers skip progressive
354- // frames and receive only the authoritative final result emitted for every caller below.
361+ // progressId is connection-owned and excluded from the work key. Active followers register
362+ // for the remaining host frames; settled followers receive only the authoritative result.
363+ if ( cached . expiresAt === undefined && progressConnId && progressId ) {
364+ cached . progressSubscribers . set ( `${ progressConnId } \0${ progressId } ` , {
365+ broadcastToConnIds : context . broadcastToConnIds ,
366+ connId : progressConnId ,
367+ progressId,
368+ } ) ;
369+ }
355370 cache . delete ( listKey ) ;
356371 cache . set ( listKey , cached ) ;
357372 respond ( true , await cached . result ) ;
@@ -360,6 +375,14 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
360375 if ( cached ) {
361376 cache . delete ( listKey ) ;
362377 }
378+ const progressSubscribers = new Map < string , CatalogListProgressSubscriber > ( ) ;
379+ if ( progressConnId && progressId ) {
380+ progressSubscribers . set ( `${ progressConnId } \0${ progressId } ` , {
381+ broadcastToConnIds : context . broadcastToConnIds ,
382+ connId : progressConnId ,
383+ progressId,
384+ } ) ;
385+ }
363386 const operation = ( async ( ) => {
364387 const requestEntries = createSessionCatalogRequestEntrySnapshot ( {
365388 cfg : config ,
@@ -370,27 +393,28 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
370393 selected . map ( async ( provider ) : Promise < SessionCatalog > => {
371394 const createTarget = resolveProviderCreateTarget ( provider , resolvedAgent . agentId , config ) ;
372395 const createSession = createTarget . ok ? { model : createTarget . target . model } : undefined ;
373- const onHost = progressConnId
374- ? ( host : SessionCatalog [ "hosts" ] [ number ] ) => {
375- // Progressive frames are an optimization. The final RPC response remains
376- // authoritative when a slow client drops an intermediate host update.
377- context . broadcastToConnIds (
378- "sessions.catalog.host" ,
379- {
380- progressId,
381- agentId : resolvedAgent . agentId ,
382- catalog : catalogResult (
383- provider ,
384- [ requestEntries . projectHostCreatedActors ( host ) ] ,
385- undefined ,
386- createSession ,
387- ) ,
388- } ,
389- new Set ( [ progressConnId ] ) ,
390- { dropIfSlow : true } ,
391- ) ;
392- }
393- : undefined ;
396+ const onHost = ( host : SessionCatalog [ "hosts" ] [ number ] ) => {
397+ const catalog = catalogResult (
398+ provider ,
399+ [ requestEntries . projectHostCreatedActors ( host ) ] ,
400+ undefined ,
401+ createSession ,
402+ ) ;
403+ // Progressive frames are an optimization. The final RPC response remains
404+ // authoritative when a slow client drops an intermediate host update.
405+ for ( const subscriber of progressSubscribers . values ( ) ) {
406+ subscriber . broadcastToConnIds (
407+ "sessions.catalog.host" ,
408+ {
409+ progressId : subscriber . progressId ,
410+ agentId : resolvedAgent . agentId ,
411+ catalog,
412+ } ,
413+ new Set ( [ subscriber . connId ] ) ,
414+ { dropIfSlow : true } ,
415+ ) ;
416+ }
417+ } ;
394418 try {
395419 const hosts = await provider . list ( {
396420 search,
@@ -399,7 +423,7 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
399423 ...( request . cursors !== undefined ? { cursors : request . cursors } : { } ) ,
400424 sessionEntries : requestEntries . sessionEntries ,
401425 listNodes,
402- ... ( onHost ? { onHost } : { } ) ,
426+ onHost,
403427 } ) ;
404428 return catalogResult (
405429 provider ,
@@ -414,7 +438,7 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
414438 ) ;
415439 return { catalogs : catalogList } ;
416440 } ) ( ) ;
417- const entry : CatalogListCacheEntry = { result : operation } ;
441+ const entry : CatalogListCacheEntry = { progressSubscribers , result : operation } ;
418442 // Exact request/config/registration results remain shareable for 3s after settling. This catches
419443 // out-of-phase clients but expires before the UI's 5s fast follow, so changed rows surface there.
420444 // Expired and rejected work is removed; retaining it would mask provider recovery or new sessions.
@@ -437,6 +461,8 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
437461 cache . delete ( listKey ) ;
438462 }
439463 throw error ;
464+ } finally {
465+ progressSubscribers . clear ( ) ;
440466 }
441467 } ,
442468
@@ -581,3 +607,40 @@ export const sessionCatalogHandlers: GatewayRequestHandlers = {
581607 }
582608 } ,
583609} ;
610+
611+ /** Fill the same list single-flight and provider caches used by the Gateway RPC. */
612+ export async function prewarmSessionCatalogList ( params : {
613+ config : OpenClawConfig ;
614+ agentId : string ;
615+ limitPerHost : number ;
616+ } ) : Promise < void > {
617+ const handler = sessionCatalogHandlers [ "sessions.catalog.list" ] ;
618+ if ( ! handler ) {
619+ throw new Error ( "sessions.catalog.list handler is unavailable" ) ;
620+ }
621+ let responded = false ;
622+ let responseError : string | undefined ;
623+ const respond : RespondFn = ( ok , _payload , error ) => {
624+ responded = true ;
625+ if ( ! ok ) {
626+ responseError = error ?. message || "sessions.catalog.list prewarm failed" ;
627+ }
628+ } ;
629+ await handler ( {
630+ params : {
631+ agentId : params . agentId ,
632+ limitPerHost : params . limitPerHost ,
633+ } ,
634+ client : null ,
635+ // A headless leader intentionally has no progress id or connection subscription.
636+ // Concurrent clients still share its authoritative final result through the normal cache.
637+ context : { getRuntimeConfig : ( ) => params . config } ,
638+ respond,
639+ } as never ) ;
640+ if ( ! responded ) {
641+ throw new Error ( "sessions.catalog.list prewarm returned no result" ) ;
642+ }
643+ if ( responseError ) {
644+ throw new Error ( responseError ) ;
645+ }
646+ }
0 commit comments