@@ -14,6 +14,7 @@ import {
1414 CODEX_APP_SERVER_BINDING_MAX_ENTRIES ,
1515 CODEX_APP_SERVER_BINDING_NAMESPACE ,
1616} from "../app-server/session-binding-meta.js" ;
17+ import { archiveBindingSidecar } from "./session-binding-sidecar-archive.js" ;
1718
1819const LEGACY_BINDING_SUFFIX = ".codex-app-server.json" ;
1920const CODEX_AGENT_HARNESS_ID = "codex" ;
@@ -66,6 +67,7 @@ type BindingOwnerCollection = {
6667type SourceMigrationResult = {
6768 archived : boolean ;
6869 importedKeys : number ;
70+ notice ?: string ;
6971 warning ?: string ;
7072} ;
7173
@@ -429,6 +431,11 @@ async function migrateSource(
429431 importedKeys,
430432 warning : `Left Codex binding sidecar in place because ${ reason } : ${ source . sidecarPath } ` ,
431433 } ) ;
434+ const retainNotice = ( reason : string ) : SourceMigrationResult => ( {
435+ archived : false ,
436+ importedKeys,
437+ notice : `Left Codex binding sidecar in place because ${ reason } : ${ source . sidecarPath } ` ,
438+ } ) ;
432439 const owner = candidates . length === 1 ? candidates [ 0 ] : undefined ;
433440 try {
434441 return await withFileLock ( source . sidecarPath , LEGACY_BINDING_LOCK_OPTIONS , async ( ) => {
@@ -467,7 +474,10 @@ async function migrateSource(
467474 return retain ( `${ candidates . length } matching session owners make ownership ambiguous` ) ;
468475 }
469476 if ( owner ?. agentHarnessId && owner . agentHarnessId !== CODEX_AGENT_HARNESS_ID ) {
470- return retain ( `its session is owned by agent harness ${ owner . agentHarnessId } ` ) ;
477+ // Explicit foreign ownership is a complete decision, not an unsafe
478+ // migration conflict. Preserve the sidecar for that harness without
479+ // blocking every later Gateway startup.
480+ return retainNotice ( `its session is owned by agent harness ${ owner . agentHarnessId } ` ) ;
471481 }
472482 const sourceSessionFile =
473483 typeof raw . sessionFile === "string" && raw . sessionFile . trim ( )
@@ -611,7 +621,10 @@ async function migrateSource(
611621 return retain ( `${ ownershipWarning } ; its stale session binding could not be retired` ) ;
612622 }
613623 }
614- return retain ( ownershipWarning ) ;
624+ // Imported active session state is retired before reaching here.
625+ // The remaining sidecar may belong to the new owner, so preserve it
626+ // as a note; failed retirement and revalidation stay warnings above.
627+ return retainNotice ( ownershipWarning ) ;
615628 }
616629 for ( const entry of entries ) {
617630 if ( ! hasExpected ( await store . lookup ( entry . key ) , entry . value ) ) {
@@ -808,32 +821,6 @@ async function pathExists(filePath: string): Promise<boolean> {
808821 }
809822}
810823
811- async function firstFreeArchivePath ( sourcePath : string ) : Promise < string > {
812- for ( let index = 2 ; ; index ++ ) {
813- const candidate = `${ sourcePath } .migrated.${ index } ` ;
814- if ( ! ( await pathExists ( candidate ) ) ) {
815- return candidate ;
816- }
817- }
818- }
819-
820- async function archiveBindingSidecar ( sourcePath : string ) : Promise < void > {
821- const archivePath = `${ sourcePath } .migrated` ;
822- if ( await pathExists ( archivePath ) ) {
823- const [ sourceBytes , archiveBytes ] = await Promise . all ( [
824- fs . readFile ( sourcePath ) ,
825- fs . readFile ( archivePath ) ,
826- ] ) ;
827- if ( sourceBytes . equals ( archiveBytes ) ) {
828- await fs . rm ( sourcePath , { force : true } ) ;
829- return ;
830- }
831- await fs . rename ( sourcePath , await firstFreeArchivePath ( sourcePath ) ) ;
832- return ;
833- }
834- await fs . rename ( sourcePath , archivePath ) ;
835- }
836-
837824export const stateMigrations : PluginDoctorStateMigration [ ] = [
838825 {
839826 id : "codex-app-server-sidecars-to-plugin-state" ,
@@ -851,6 +838,7 @@ export const stateMigrations: PluginDoctorStateMigration[] = [
851838 async migrateLegacyState ( params ) {
852839 const changes : string [ ] = [ ] ;
853840 const warnings : string [ ] = [ ] ;
841+ const notices : string [ ] = [ ] ;
854842 const { sources, surfaces } = await collectLegacyBindingSources ( params ) ;
855843 if ( sources . length === 0 ) {
856844 return { changes, warnings } ;
@@ -876,6 +864,9 @@ export const stateMigrations: PluginDoctorStateMigration[] = [
876864 if ( result . warning ) {
877865 warnings . push ( result . warning ) ;
878866 }
867+ if ( result . notice ) {
868+ notices . push ( result . notice ) ;
869+ }
879870 if ( result . archived ) {
880871 migrated ++ ;
881872 } else {
@@ -892,7 +883,11 @@ export const stateMigrations: PluginDoctorStateMigration[] = [
892883 `Migrated ${ partialImports } safe Codex app-server binding row(s) to plugin state; retained legacy sidecars needing review` ,
893884 ) ;
894885 }
895- return { changes, warnings } ;
886+ return {
887+ changes,
888+ warnings,
889+ ...( notices . length > 0 ? { notices } : { } ) ,
890+ } ;
896891 } ,
897892 } ,
898893] ;
0 commit comments