@@ -61,13 +61,16 @@ function openBindingStore(env: NodeJS.ProcessEnv) {
6161
6262async function createBindingMigrationFixture ( options : {
6363 binding ?: Record < string , unknown > ;
64+ legacySharedRoot ?: boolean ;
6465 name : string ;
6566 sessionIndex ?: Record < string , unknown > ;
6667 threadId : string ;
6768} ) {
6869 const stateDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-codex-doctor-" ) ) ;
6970 const env = { ...process . env , OPENCLAW_STATE_DIR : stateDir } ;
70- const sessionsDir = path . join ( stateDir , "agents" , "main" , "sessions" ) ;
71+ const sessionsDir = options . legacySharedRoot
72+ ? path . join ( stateDir , "sessions" )
73+ : path . join ( stateDir , "agents" , "main" , "sessions" ) ;
7174 const storePath = path . join ( sessionsDir , "sessions.json" ) ;
7275 const transcriptPath = path . join ( sessionsDir , `${ options . name } .jsonl` ) ;
7376 const sidecarPath = `${ transcriptPath } .codex-app-server.json` ;
@@ -293,6 +296,82 @@ describe("codex doctor contract", () => {
293296 await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
294297 } ) ;
295298
299+ it ( "preserves ambiguous shared-root bindings for explicit multi-agent configs" , async ( ) => {
300+ const fixture = await createBindingMigrationFixture ( {
301+ legacySharedRoot : true ,
302+ name : "explicit-owner" ,
303+ sessionIndex : {
304+ legacy : {
305+ sessionId : "explicit-owner" ,
306+ sessionFile : "explicit-owner.jsonl" ,
307+ updatedAt : 1 ,
308+ } ,
309+ } ,
310+ threadId : "thread-explicit-owner" ,
311+ } ) ;
312+ const params = {
313+ ...fixture . params ,
314+ config : {
315+ agents : { ownership : "explicit" as const , entries : { main : { } , ops : { } } } ,
316+ } ,
317+ } ;
318+
319+ await expect ( fixture . migration . detectLegacyState ( params ) ) . resolves . toMatchObject ( {
320+ preview : [ expect . stringContaining ( "legacy sidecar" ) ] ,
321+ } ) ;
322+ const result = await fixture . migration . migrateLegacyState ( params ) ;
323+
324+ expect ( result . changes ) . toEqual ( [ ] ) ;
325+ expect ( result . warnings ) . toEqual ( [
326+ expect . stringContaining ( "session ownership is indeterminate" ) ,
327+ ] ) ;
328+ await expect ( fs . access ( fixture . sidecarPath ) ) . resolves . toBeUndefined ( ) ;
329+ await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . rejects . toThrow ( ) ;
330+ await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
331+
332+ await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
333+ } ) ;
334+
335+ it ( "keeps an agent-scoped shared-root binding with its explicit owner" , async ( ) => {
336+ const sessionKey = "agent:ops:legacy" ;
337+ const fixture = await createBindingMigrationFixture ( {
338+ legacySharedRoot : true ,
339+ name : "explicit-ops-owner" ,
340+ sessionIndex : {
341+ [ sessionKey ] : {
342+ sessionId : "explicit-ops-owner" ,
343+ sessionFile : "explicit-ops-owner.jsonl" ,
344+ updatedAt : 1 ,
345+ } ,
346+ } ,
347+ threadId : "thread-explicit-ops-owner" ,
348+ } ) ;
349+ const params = {
350+ ...fixture . params ,
351+ config : {
352+ agents : { ownership : "explicit" as const , entries : { main : { } , ops : { } } } ,
353+ } ,
354+ } ;
355+
356+ await expect ( fixture . migration . migrateLegacyState ( params ) ) . resolves . toMatchObject ( {
357+ changes : [ expect . stringContaining ( "Migrated 1" ) ] ,
358+ warnings : [ ] ,
359+ } ) ;
360+ await expect (
361+ openBindingStore ( fixture . env ) . lookup (
362+ bindingStoreKey ( {
363+ kind : "session" ,
364+ agentId : "ops" ,
365+ sessionId : "explicit-ops-owner" ,
366+ sessionKey,
367+ } ) ,
368+ ) ,
369+ ) . resolves . toMatchObject ( { sessionId : "explicit-ops-owner" } ) ;
370+ await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
371+
372+ await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
373+ } ) ;
374+
296375 it ( "bounds oversized legacy fingerprints before plugin-state import" , async ( ) => {
297376 const rawDynamicToolsFingerprint = JSON . stringify ( [
298377 { name : "legacy_large_tool" , inputSchema : { description : "dynamic-marker" . repeat ( 8_000 ) } } ,
0 commit comments