1- // Session store target resolution for the per-agent deterministic store hot path .
1+ // Session store target resolution for bounded retired/manual lookups .
22import nodeFs from "node:fs" ;
3+ import { syncBuiltinESMExports } from "node:module" ;
34import path from "node:path" ;
45import { withTempHome } from "openclaw/plugin-sdk/test-env" ;
56import { describe , expect , it , vi } from "vitest" ;
67import type { OpenClawConfig } from "../config.js" ;
7- import { replaceSessionEntry } from "./session-accessor.sqlite-entry.js" ;
88import { resolveExistingAgentSessionStoreTargetsSync } from "./targets.js" ;
9- import { createAgentSessionStores , createCustomRootCfg } from "./targets.test-support.js" ;
9+ import { countMatching , createAgentSessionStores } from "./targets.test-support.js" ;
1010
11- describe ( "resolveExistingAgentSessionStoreTargetsSync deterministic store" , ( ) => {
12- it ( "does not touch other agents' store files when the deterministic store exists " , async ( ) => {
11+ describe ( "resolveExistingAgentSessionStoreTargetsSync retired store" , ( ) => {
12+ it ( "does not resolve unrelated registered store identities " , async ( ) => {
1313 await withTempHome ( async ( home ) => {
1414 const stateDir = path . join ( home , ".openclaw" ) ;
15- const storePaths = await createAgentSessionStores ( stateDir , [ "ops" , "retired" ] ) ;
15+ const unrelatedAgentIds = Array . from ( { length : 12 } , ( _ , index ) => `extra-${ index } ` ) ;
16+ const storePaths = await createAgentSessionStores ( stateDir , [
17+ "retired" ,
18+ ...unrelatedAgentIds ,
19+ ] ) ;
1620 const cfg : OpenClawConfig = {
17- agents : { list : [ { id : "ops " , default : true } ] } ,
21+ agents : { list : [ { id : "main " , default : true } ] } ,
1822 } ;
19- const readdirSpy = vi . spyOn ( nodeFs , "readdirSync" ) ;
20- const lstatSpy = vi . spyOn ( nodeFs , "lstatSync" ) ;
21- const realpathSpy = vi . spyOn ( nodeFs . realpathSync , "native" ) ;
22-
23- expect ( resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , { env : process . env } ) ) . toEqual (
24- [ { agentId : "ops" , storePath : storePaths . ops } ] ,
25- ) ;
26- // Configured agents take the bounded canonical path, which resolves the store directly
27- // without listing the agents directory. No retired agent store file is ever statted or
28- // realpathed on the hot path — the bounded path never reaches directory discovery.
29- expect ( readdirSpy ) . toHaveBeenCalledTimes ( 0 ) ;
30- const touched = [ ...lstatSpy . mock . calls , ...realpathSpy . mock . calls ]
31- . map ( ( [ firstArg ] ) => ( typeof firstArg === "string" ? firstArg : "" ) )
32- . filter ( ( candidate ) => candidate . includes ( "retired" ) ) ;
33- expect ( touched ) . toEqual ( [ ] ) ;
34- readdirSpy . mockRestore ( ) ;
35- lstatSpy . mockRestore ( ) ;
36- realpathSpy . mockRestore ( ) ;
37- } ) ;
38- } ) ;
39-
40- it ( "falls back to directory discovery when the deterministic store is missing" , async ( ) => {
41- await withTempHome ( async ( home ) => {
42- const stateDir = path . join ( home , ".openclaw" ) ;
43- const storePaths = await createAgentSessionStores ( stateDir , [ "ops" ] ) ;
44- const cfg : OpenClawConfig = {
45- agents : { list : [ { id : "ops" , default : true } ] } ,
46- session : {
47- store : path . join ( home , "external" , "sessions-{agentId}.json" ) ,
48- } ,
49- } ;
50-
51- expect ( resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , { env : process . env } ) ) . toEqual (
52- [ { agentId : "ops" , storePath : storePaths . ops } ] ,
53- ) ;
54- } ) ;
55- } ) ;
56-
57- it ( "keeps an alternate same-agent store in another root visible when the deterministic store exists" , async ( ) => {
58- await withTempHome ( async ( home ) => {
59- const stateDir = path . join ( home , ".openclaw" ) ;
60- const customRoot = path . join ( home , "custom-state" ) ;
61- const customStorePaths = await createAgentSessionStores ( customRoot , [ "ops" ] ) ;
62- const defaultStorePaths = await createAgentSessionStores ( stateDir , [ "ops" ] ) ;
63- const cfg = createCustomRootCfg ( customRoot ) ;
64-
65- const targets = resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , {
66- env : process . env ,
67- } ) ;
68-
69- expect ( targets . map ( ( target ) => target . storePath ) . toSorted ( ) ) . toEqual (
70- [ customStorePaths . ops ! , defaultStorePaths . ops ! ] . toSorted ( ) ,
71- ) ;
72- } ) ;
73- } ) ;
74-
75- it ( "does not discover a non-round-tripping same-agent alias directory for a configured agent" , async ( ) => {
76- await withTempHome ( async ( home ) => {
77- const stateDir = path . join ( home , ".openclaw" ) ;
78- const storePaths = await createAgentSessionStores ( stateDir , [ "ops" ] ) ;
79- const aliasStorePaths = await createAgentSessionStores ( stateDir , [ "Ops" ] ) ;
80- const cfg : OpenClawConfig = {
81- agents : { list : [ { id : "ops" , default : true } ] } ,
82- } ;
83-
84- const targets = resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , {
85- env : process . env ,
86- } ) ;
87-
88- // Configured agents take the bounded canonical path, which resolves only the configured
89- // agent's own store. A same-root alias directory whose name does not round-trip through
90- // normalizeAgentId (e.g. "Ops" vs "ops") is intentionally not recovered — that cross-alias
91- // visibility is out of scope for the bounded-discovery contract and was never part of main's
92- // configured-agent path. Only the configured "ops" store is returned.
93- expect ( targets . map ( ( target ) => target . storePath ) . toSorted ( ) ) . toEqual (
94- [ storePaths . ops ! ] . toSorted ( ) ,
95- ) ;
96- expect ( targets . map ( ( target ) => target . storePath ) ) . not . toContain ( aliasStorePaths . Ops ) ;
23+ const lstat = vi . spyOn ( nodeFs , "lstatSync" ) ;
24+ const stat = vi . spyOn ( nodeFs , "statSync" ) ;
25+ const realpath = vi . spyOn ( nodeFs . realpathSync , "native" ) ;
26+ syncBuiltinESMExports ( ) ;
27+ try {
28+ expect (
29+ resolveExistingAgentSessionStoreTargetsSync ( cfg , "retired" , { env : process . env } ) ,
30+ ) . toEqual ( [ { agentId : "retired" , storePath : storePaths . retired } ] ) ;
31+
32+ const isUnrelatedAgentPath = ( [ candidate ] : readonly unknown [ ] ) =>
33+ typeof candidate === "string" && candidate . includes ( `${ path . sep } agents${ path . sep } extra-` ) ;
34+ expect ( {
35+ lstat : countMatching ( lstat . mock . calls , isUnrelatedAgentPath ) ,
36+ stat : countMatching ( stat . mock . calls , isUnrelatedAgentPath ) ,
37+ realpath : countMatching ( realpath . mock . calls , isUnrelatedAgentPath ) ,
38+ } ) . toEqual ( { lstat : 0 , stat : 0 , realpath : 0 } ) ;
39+ } finally {
40+ lstat . mockRestore ( ) ;
41+ stat . mockRestore ( ) ;
42+ realpath . mockRestore ( ) ;
43+ syncBuiltinESMExports ( ) ;
44+ }
9745 } ) ;
9846 } ) ;
9947
100- it ( "does not discover a configured agent's store under another configured agent's template root" , async ( ) => {
101- // A template may carry {agentId} before the final agents/<agentId> segment, so each configured
102- // agent expansion resolves a distinct agents root and an alternate same-agent store may live
103- // under another agent's root (e.g. the ops store under the work expansion). The bounded
104- // configured-agent path does not enumerate other expansions, so that cross-root store is
105- // intentionally not recovered — recovering it would require scanning one root per configured
106- // agent, which is exactly the O(configured agents) fan-out issue #123439 reports.
48+ it ( "finds a store under another configured agent's template root" , async ( ) => {
10749 await withTempHome ( async ( home ) => {
10850 const storesRoot = path . join ( home , "stores" ) ;
51+ const storePaths = await createAgentSessionStores ( path . join ( storesRoot , "work" ) , [ "old" ] ) ;
10952 const cfg : OpenClawConfig = {
11053 session : {
11154 store : path . join (
@@ -119,118 +62,10 @@ describe("resolveExistingAgentSessionStoreTargetsSync deterministic store", () =
11962 } ,
12063 agents : { list : [ { id : "ops" , default : true } , { id : "work" } ] } ,
12164 } ;
122- // Deterministic ops store under the ops expansion.
123- const deterministicStorePath = path . join (
124- storesRoot ,
125- "ops" ,
126- "agents" ,
127- "ops" ,
128- "sessions" ,
129- "sessions.json" ,
130- ) ;
131- // Alternate ops store under the work expansion — not visible under the bounded contract.
132- const alternateStorePath = path . join (
133- storesRoot ,
134- "work" ,
135- "agents" ,
136- "ops" ,
137- "sessions" ,
138- "sessions.json" ,
139- ) ;
140- await replaceSessionEntry (
141- { storePath : deterministicStorePath , sessionKey : "main" } ,
142- { sessionId : "sid-deterministic" , updatedAt : 1 } ,
143- ) ;
144- await replaceSessionEntry (
145- { storePath : alternateStorePath , sessionKey : "main" } ,
146- { sessionId : "sid-alternate" , updatedAt : 2 } ,
147- ) ;
148-
149- const targets = resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , {
150- env : process . env ,
151- } ) ;
152-
153- expect ( targets . map ( ( target ) => target . storePath ) . toSorted ( ) ) . toEqual (
154- [ path . resolve ( deterministicStorePath ) ] . toSorted ( ) ,
155- ) ;
156- expect ( targets . map ( ( target ) => target . storePath ) ) . not . toContain (
157- path . resolve ( alternateStorePath ) ,
158- ) ;
159- } ) ;
160- } ) ;
161-
162- it ( "discovers a retired/manual agent's store under another configured agent's template root" , async ( ) => {
163- // A template may carry {agentId} before the final agents/<agentId> segment, so each configured
164- // agent expansion resolves a distinct agents root. A non-configured (retired/manual) agent has
165- // no canonical path, and its persisted store may live under another configured agent's
166- // expansion (e.g. the retired "old" store under the work expansion). Non-configured discovery
167- // enumerates every configured expansion plus the default root — the same root set main's
168- // all-agent discovery searches — so that cross-root store stays visible after configuration
169- // removal. Only the requested agent's directories are validated, so other agents' stores are
170- // never statted on this path.
171- await withTempHome ( async ( home ) => {
172- const storesRoot = path . join ( home , "stores" ) ;
173- const cfg : OpenClawConfig = {
174- session : {
175- store : path . join (
176- storesRoot ,
177- "{agentId}" ,
178- "agents" ,
179- "{agentId}" ,
180- "sessions" ,
181- "sessions.json" ,
182- ) ,
183- } ,
184- agents : { list : [ { id : "ops" , default : true } , { id : "work" } ] } ,
185- } ;
186- // Retired "old" store under the work expansion — the cross-root case main preserves.
187- const retiredStorePath = path . join (
188- storesRoot ,
189- "work" ,
190- "agents" ,
191- "old" ,
192- "sessions" ,
193- "sessions.json" ,
194- ) ;
195- await replaceSessionEntry (
196- { storePath : retiredStorePath , sessionKey : "main" } ,
197- { sessionId : "sid-retired-cross-root" , updatedAt : 1 } ,
198- ) ;
199-
200- const targets = resolveExistingAgentSessionStoreTargetsSync ( cfg , "old" , {
201- env : process . env ,
202- } ) ;
203-
204- expect ( targets . map ( ( target ) => target . storePath ) ) . toContain ( path . resolve ( retiredStorePath ) ) ;
205- expect ( targets . map ( ( target ) => target . agentId ) ) . toContain ( "old" ) ;
206- } ) ;
207- } ) ;
208-
209- it ( "does not discover same-agent alias directories for a configured agent under an external template" , async ( ) => {
210- await withTempHome ( async ( home ) => {
211- const stateDir = path . join ( home , ".openclaw" ) ;
212- // Two alias directories that both normalize to "ops", but the requested agent is configured.
213- const lowerStorePaths = await createAgentSessionStores ( stateDir , [ "ops" ] ) ;
214- const upperStorePaths = await createAgentSessionStores ( stateDir , [ "Ops" ] ) ;
215- const cfg : OpenClawConfig = {
216- agents : { list : [ { id : "ops" , default : true } ] } ,
217- session : {
218- store : path . join ( home , "external" , "sessions-{agentId}.json" ) ,
219- } ,
220- } ;
221-
222- const targets = resolveExistingAgentSessionStoreTargetsSync ( cfg , "ops" , {
223- env : process . env ,
224- } ) ;
22565
226- // Configured agents take the bounded canonical path even under an external template: the
227- // default-root store is resolved directly, and same-root alias directories ("Ops" vs "ops")
228- // are not enumerated. Only the configured "ops" store is returned; alias discovery and its
229- // deterministic ordering no longer apply on the configured-agent path.
230- expect ( targets . map ( ( target ) => target . storePath ) . toSorted ( ) ) . toEqual (
231- [ lowerStorePaths . ops ! ] . toSorted ( ) ,
66+ expect ( resolveExistingAgentSessionStoreTargetsSync ( cfg , "old" , { env : process . env } ) ) . toEqual (
67+ [ { agentId : "old" , storePath : storePaths . old } ] ,
23268 ) ;
233- expect ( targets . map ( ( target ) => target . storePath ) ) . not . toContain ( upperStorePaths . Ops ) ;
23469 } ) ;
23570 } ) ;
23671} ) ;
0 commit comments