@@ -17,6 +17,8 @@ import {
1717import {
1818 listOpenClawRegisteredAgentDatabases ,
1919 listOpenIncognitoAgentDatabases ,
20+ readOpenClawAgentDatabaseRegistryToken ,
21+ readOpenIncognitoAgentDatabaseGeneration ,
2022} from "../../state/openclaw-agent-db.js" ;
2123import { resolveSessionStoreCompatibilityAgentId } from "../legacy.default-agent-owner.js" ;
2224import type { OpenClawConfig } from "../types.openclaw.js" ;
@@ -36,7 +38,6 @@ import {
3638 listKnownSessionStoreAgentIds ,
3739 resolveAgentSessionStoreTargetsSync ,
3840 resolveAllAgentSessionStoreTargetsSync ,
39- resolveSessionStoreTargets ,
4041} from "./targets.js" ;
4142import type { SessionEntry } from "./types.js" ;
4243
@@ -52,13 +53,26 @@ type GatewaySessionStoreOptions = {
5253type ResolvedGatewaySessionStoreTargets = {
5354 configuredAgentIds ?: ReadonlySet < string > ;
5455 defaultAgentId : string ;
55- diagnostics : string [ ] ;
56- durableTargets : Array < { agentId : string ; storePath : string } > ;
57- incognitoTargets : Array < { agentId : string ; storePath : string } > ;
56+ diagnostics : readonly string [ ] ;
57+ durableStorePath ?: string ;
58+ durableTargets : ReadonlyArray < { agentId : string ; storePath : string } > ;
59+ incognitoTargets : ReadonlyArray < { agentId : string ; storePath : string } > ;
5860 requestedAgentId ?: string ;
5961 storeConfig ?: string ;
6062} ;
6163
64+ type PreparedConfiguredSessionStoreTargets = {
65+ cfg : OpenClawConfig ;
66+ includeIncognito : boolean ;
67+ incognitoGeneration : number ;
68+ registryToken : symbol ;
69+ resolved : ResolvedGatewaySessionStoreTargets ;
70+ } ;
71+
72+ // Gateway aliases, config, registry, and incognito topology are process-stable until
73+ // an explicit generation change or restart; generic CLI/Doctor dedupe stays fresh.
74+ let preparedConfiguredSessionStoreTargets : PreparedConfiguredSessionStoreTargets | undefined ;
75+
6276// Template-backed stores need per-agent scans before they can be merged for Gateway views.
6377function isStorePathTemplate ( store ?: string ) : boolean {
6478 return typeof store === "string" && store . includes ( "{agentId}" ) ;
@@ -73,9 +87,13 @@ function resolveCombinedStorePath(paths: string[], storeConfig?: string): string
7387}
7488
7589function resolveCombinedDatabasePath (
76- targets : Array < { agentId : string ; storePath : string } > ,
90+ targets : ReadonlyArray < { agentId : string ; storePath : string } > ,
7791 defaultAgentId : string ,
92+ physicallyDeduped = false ,
7893) : string {
94+ if ( physicallyDeduped && targets . length !== 1 ) {
95+ return "(multiple)" ;
96+ }
7997 const paths = [
8098 ...new Set (
8199 targets . map (
@@ -147,7 +165,7 @@ function mergeOpenIncognitoStores(params: {
147165 cfg : OpenClawConfig ;
148166 combined : Record < string , SessionEntry > ;
149167 projection : GatewaySessionEntryProjection ;
150- targets : Array < { agentId : string ; storePath : string } > ;
168+ targets : ReadonlyArray < { agentId : string ; storePath : string } > ;
151169} ) : string [ ] {
152170 const storePaths : string [ ] = [ ] ;
153171 for ( const target of params . targets ) {
@@ -205,6 +223,74 @@ function filterCombinedStoreToConfiguredAgents(params: {
205223 }
206224}
207225
226+ function resolvePreparedConfiguredSessionStoreTargets (
227+ cfg : OpenClawConfig ,
228+ includeIncognito : boolean ,
229+ ) : ResolvedGatewaySessionStoreTargets {
230+ const registryToken = readOpenClawAgentDatabaseRegistryToken ( ) ;
231+ const incognitoGeneration = readOpenIncognitoAgentDatabaseGeneration ( ) ;
232+ const cached = preparedConfiguredSessionStoreTargets ;
233+ if (
234+ cached ?. cfg === cfg &&
235+ cached . registryToken === registryToken &&
236+ cached . incognitoGeneration === incognitoGeneration &&
237+ cached . includeIncognito === includeIncognito
238+ ) {
239+ return cached . resolved ;
240+ }
241+
242+ const storeConfig = cfg . session ?. store ;
243+ const defaultAgentId = normalizeAgentId ( resolveSessionStoreCompatibilityAgentId ( cfg ) ) ;
244+ const configuredIds = listConfiguredSessionStoreAgentIds ( cfg ) ;
245+ const configuredAgentIds = new Set ( configuredIds ) ;
246+ const incognitoTargets = includeIncognito ? listOpenIncognitoAgentDatabases ( ) : [ ] ;
247+ const incognitoTargetKeys = new Set (
248+ incognitoTargets . map ( ( target ) => `${ target . agentId } \0${ target . storePath } ` ) ,
249+ ) ;
250+ const diagnostics : string [ ] = [ ] ;
251+ const candidates = dedupeSessionStoreTargetsBySqliteTarget (
252+ [
253+ ...listOpenClawRegisteredAgentDatabases ( ) . map ( ( { agentId, path } ) => ( {
254+ agentId,
255+ storePath : path ,
256+ } ) ) ,
257+ ...configuredIds . map ( ( agentId ) => ( {
258+ agentId,
259+ storePath : resolveSessionStorePathCore ( storeConfig , { agentId } ) ,
260+ } ) ) ,
261+ ...incognitoTargets ,
262+ ] ,
263+ {
264+ defaultAgentId,
265+ onDiagnostic : ( diagnostic ) => diagnostics . push ( diagnostic . message ) ,
266+ } ,
267+ ) ;
268+ const durableTargets = candidates . filter (
269+ ( target ) => ! incognitoTargetKeys . has ( `${ target . agentId } \0${ target . storePath } ` ) ,
270+ ) ;
271+ const resolved = Object . freeze ( {
272+ configuredAgentIds,
273+ defaultAgentId,
274+ diagnostics : Object . freeze ( diagnostics ) ,
275+ durableStorePath : resolveCombinedDatabasePath ( durableTargets , defaultAgentId , true ) ,
276+ durableTargets : Object . freeze ( durableTargets . map ( ( target ) => Object . freeze ( { ...target } ) ) ) ,
277+ incognitoTargets : Object . freeze (
278+ candidates
279+ . filter ( ( target ) => incognitoTargetKeys . has ( `${ target . agentId } \0${ target . storePath } ` ) )
280+ . map ( ( target ) => Object . freeze ( { ...target } ) ) ,
281+ ) ,
282+ storeConfig,
283+ } ) ;
284+ preparedConfiguredSessionStoreTargets = {
285+ cfg,
286+ includeIncognito,
287+ incognitoGeneration,
288+ registryToken,
289+ resolved,
290+ } ;
291+ return resolved ;
292+ }
293+
208294function resolveGatewaySessionStoreTargets (
209295 cfg : OpenClawConfig ,
210296 opts : GatewaySessionStoreOptions ,
@@ -215,50 +301,17 @@ function resolveGatewaySessionStoreTargets(
215301 typeof opts . agentId === "string" && opts . agentId . trim ( )
216302 ? normalizeAgentId ( opts . agentId )
217303 : undefined ;
304+ if ( opts . configuredAgentsOnly === true && ! requestedAgentId ) {
305+ return resolvePreparedConfiguredSessionStoreTargets ( cfg , opts . includeIncognito !== false ) ;
306+ }
218307 const defaultAgentId = normalizeAgentId ( resolveSessionStoreCompatibilityAgentId ( cfg ) ) ;
219- const configuredAgentIds =
220- opts . configuredAgentsOnly === true && ! requestedAgentId
221- ? new Set ( listConfiguredSessionStoreAgentIds ( cfg ) )
222- : undefined ;
223308 const incognitoTargets =
224309 opts . includeIncognito === false
225310 ? [ ]
226311 : listOpenIncognitoAgentDatabases ( ) . filter (
227312 ( target ) => ! requestedAgentId || target . agentId === requestedAgentId ,
228313 ) ;
229314
230- if ( configuredAgentIds ) {
231- const incognitoTargetKeys = new Set (
232- incognitoTargets . map ( ( target ) => `${ target . agentId } \0${ target . storePath } ` ) ,
233- ) ;
234- const candidates = dedupeSessionStoreTargetsBySqliteTarget (
235- [
236- ...listOpenClawRegisteredAgentDatabases ( ) . map ( ( { agentId, path } ) => ( {
237- agentId,
238- storePath : path ,
239- } ) ) ,
240- ...resolveSessionStoreTargets ( cfg , { allAgents : true } ) ,
241- ...incognitoTargets ,
242- ] ,
243- {
244- defaultAgentId,
245- onDiagnostic : ( diagnostic ) => diagnostics . push ( diagnostic . message ) ,
246- } ,
247- ) ;
248- return {
249- configuredAgentIds,
250- defaultAgentId,
251- diagnostics,
252- durableTargets : candidates . filter (
253- ( target ) => ! incognitoTargetKeys . has ( `${ target . agentId } \0${ target . storePath } ` ) ,
254- ) ,
255- incognitoTargets : candidates . filter ( ( target ) =>
256- incognitoTargetKeys . has ( `${ target . agentId } \0${ target . storePath } ` ) ,
257- ) ,
258- storeConfig,
259- } ;
260- }
261-
262315 if ( storeConfig && ! isStorePathTemplate ( storeConfig ) ) {
263316 const ownerIds = [
264317 ...new Set ( [
@@ -279,7 +332,6 @@ function resolveGatewaySessionStoreTargets(
279332 } ,
280333 ) ;
281334 return {
282- configuredAgentIds,
283335 defaultAgentId,
284336 diagnostics,
285337 durableTargets,
@@ -290,10 +342,12 @@ function resolveGatewaySessionStoreTargets(
290342 }
291343
292344 const durableTargets = requestedAgentId
293- ? resolveAgentSessionStoreTargetsSync ( cfg , requestedAgentId )
345+ ? dedupeSessionStoreTargetsBySqliteTarget (
346+ resolveAgentSessionStoreTargetsSync ( cfg , requestedAgentId ) ,
347+ { defaultAgentId } ,
348+ )
294349 : resolveAllAgentSessionStoreTargetsSync ( cfg ) ;
295350 return {
296- configuredAgentIds,
297351 defaultAgentId,
298352 diagnostics,
299353 durableTargets,
@@ -311,10 +365,13 @@ export function canPrewarmCombinedSessionStoresForGateway(
311365 let totalRows = 0 ;
312366 for ( const agentId of params . agentIds ) {
313367 const resolved = resolveGatewaySessionStoreTargets ( cfg , { agentId } ) ;
314- const projectionTargets = dedupeSessionStoreTargetsBySqliteTarget (
315- [ ...resolved . durableTargets , ...resolved . incognitoTargets ] ,
316- { defaultAgentId : resolved . defaultAgentId } ,
317- ) ;
368+ const projectionTargets =
369+ resolved . incognitoTargets . length === 0
370+ ? resolved . durableTargets
371+ : dedupeSessionStoreTargetsBySqliteTarget (
372+ [ ...resolved . durableTargets , ...resolved . incognitoTargets ] ,
373+ { defaultAgentId : resolved . defaultAgentId } ,
374+ ) ;
318375 for ( const target of projectionTargets ) {
319376 totalRows += countSessionEntryRowsReadOnly ( target ) ;
320377 if ( totalRows > params . maxRows ) {
@@ -330,9 +387,9 @@ export function loadCombinedSessionStoreForGatewayCore(
330387 cfg : OpenClawConfig ,
331388 opts : GatewaySessionStoreOptions = { } ,
332389) : {
333- diagnostics ?: string [ ] ;
390+ diagnostics ?: readonly string [ ] ;
334391 durableStorePath ?: string ;
335- durableTargets : Array < { agentId : string ; storePath : string } > ;
392+ durableTargets : ReadonlyArray < { agentId : string ; storePath : string } > ;
336393 storePath : string ;
337394 store : Record < string , SessionEntry > ;
338395} {
@@ -343,6 +400,7 @@ export function loadCombinedSessionStoreForGatewayCore(
343400 configuredAgentIds,
344401 defaultAgentId,
345402 diagnostics,
403+ durableStorePath : preparedDurableStorePath ,
346404 durableTargets,
347405 incognitoTargets,
348406 requestedAgentId,
@@ -391,7 +449,8 @@ export function loadCombinedSessionStoreForGatewayCore(
391449 }
392450
393451 const durableStorePaths = durableTargets . map ( ( target ) => target . storePath ) ;
394- const durableStorePath = resolveCombinedDatabasePath ( durableTargets , defaultAgentId ) ;
452+ const durableStorePath =
453+ preparedDurableStorePath ?? resolveCombinedDatabasePath ( durableTargets , defaultAgentId ) ;
395454 if ( storeConfig && ! isStorePathTemplate ( storeConfig ) ) {
396455 return {
397456 diagnostics,
0 commit comments