@@ -353,6 +353,156 @@ describe("codex doctor contract", () => {
353353 await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
354354 } ) ;
355355
356+ it ( "normalizes a partial raw conversation import before copying the session row" , async ( ) => {
357+ const threadId = "thread-partial-import" ;
358+ const sessionId = "partial-import" ;
359+ const sessionKey = "agent:main:partial-import" ;
360+ const emptyConversation : StoredCodexAppServerBinding = {
361+ version : 1 ,
362+ state : "active" ,
363+ binding : {
364+ threadId,
365+ cwd : "" ,
366+ dynamicToolsFingerprint : "" ,
367+ } ,
368+ } ;
369+ const rawFingerprint = "x" . repeat (
370+ 65_535 - Buffer . byteLength ( JSON . stringify ( emptyConversation ) ) ,
371+ ) ;
372+ const rawConversation : StoredCodexAppServerBinding = {
373+ ...emptyConversation ,
374+ binding : {
375+ ...emptyConversation . binding ,
376+ dynamicToolsFingerprint : rawFingerprint ,
377+ } ,
378+ } ;
379+ const rawSession = { ...rawConversation , sessionId } ;
380+ expect ( Buffer . byteLength ( JSON . stringify ( rawConversation ) ) ) . toBe ( 65_535 ) ;
381+ expect ( Buffer . byteLength ( JSON . stringify ( rawSession ) ) ) . toBeGreaterThan ( 65_536 ) ;
382+
383+ const fixture = await createBindingMigrationFixture ( {
384+ name : sessionId ,
385+ sessionIndex : {
386+ [ sessionKey ] : {
387+ sessionId,
388+ sessionFile : `${ sessionId } .jsonl` ,
389+ } ,
390+ } ,
391+ threadId,
392+ binding : { dynamicToolsFingerprint : rawFingerprint } ,
393+ } ) ;
394+ const conversationKey = bindingStoreKey ( {
395+ kind : "conversation" ,
396+ bindingId : legacyCodexConversationBindingId ( fixture . transcriptPath ) ,
397+ } ) ;
398+ const sessionBindingKey = bindingStoreKey ( {
399+ kind : "session" ,
400+ agentId : "main" ,
401+ sessionId,
402+ sessionKey,
403+ } ) ;
404+ const store = openBindingStore ( fixture . env ) ;
405+ await store . register ( conversationKey , rawConversation ) ;
406+
407+ await expect ( fixture . migration . migrateLegacyState ( fixture . params ) ) . resolves . toEqual ( {
408+ changes : [
409+ "Migrated 1 Codex app-server binding sidecar(s) to plugin state and archived the legacy sources" ,
410+ ] ,
411+ warnings : [ ] ,
412+ } ) ;
413+
414+ const expectedFingerprint = hashCodexAppServerBindingFingerprint ( rawFingerprint ) ;
415+ await expect ( store . lookup ( conversationKey ) ) . resolves . toMatchObject ( {
416+ state : "active" ,
417+ binding : { dynamicToolsFingerprint : expectedFingerprint } ,
418+ } ) ;
419+ await expect ( store . lookup ( sessionBindingKey ) ) . resolves . toMatchObject ( {
420+ state : "active" ,
421+ sessionId,
422+ binding : { dynamicToolsFingerprint : expectedFingerprint } ,
423+ } ) ;
424+ await expect ( fs . access ( fixture . sidecarPath ) ) . rejects . toThrow ( ) ;
425+ await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
426+ await expect ( fixture . migration . migrateLegacyState ( fixture . params ) ) . resolves . toEqual ( {
427+ changes : [ ] ,
428+ warnings : [ ] ,
429+ } ) ;
430+
431+ await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
432+ } ) ;
433+
434+ it ( "normalizes retained raw conversation and session rows before comparison" , async ( ) => {
435+ const threadId = "thread-retained-import" ;
436+ const sessionId = "retained-import" ;
437+ const sessionKey = "agent:main:retained-import" ;
438+ const rawFingerprint = "x" . repeat ( 60_000 ) ;
439+ const rawConversation : StoredCodexAppServerBinding = {
440+ version : 1 ,
441+ state : "active" ,
442+ binding : {
443+ threadId,
444+ cwd : "" ,
445+ dynamicToolsFingerprint : rawFingerprint ,
446+ } ,
447+ } ;
448+ const rawSession : StoredCodexAppServerBinding = {
449+ ...rawConversation ,
450+ sessionId,
451+ } ;
452+ expect ( Buffer . byteLength ( JSON . stringify ( rawSession ) ) ) . toBeLessThan ( 65_536 ) ;
453+
454+ const fixture = await createBindingMigrationFixture ( {
455+ name : sessionId ,
456+ sessionIndex : {
457+ [ sessionKey ] : {
458+ sessionId,
459+ sessionFile : `${ sessionId } .jsonl` ,
460+ } ,
461+ } ,
462+ threadId,
463+ binding : { dynamicToolsFingerprint : rawFingerprint } ,
464+ } ) ;
465+ const conversationKey = bindingStoreKey ( {
466+ kind : "conversation" ,
467+ bindingId : legacyCodexConversationBindingId ( fixture . transcriptPath ) ,
468+ } ) ;
469+ const sessionBindingKey = bindingStoreKey ( {
470+ kind : "session" ,
471+ agentId : "main" ,
472+ sessionId,
473+ sessionKey,
474+ } ) ;
475+ const store = openBindingStore ( fixture . env ) ;
476+ await store . register ( conversationKey , rawConversation ) ;
477+ await store . register ( sessionBindingKey , rawSession ) ;
478+
479+ await expect ( fixture . migration . migrateLegacyState ( fixture . params ) ) . resolves . toEqual ( {
480+ changes : [
481+ "Migrated 1 Codex app-server binding sidecar(s) to plugin state and archived the legacy sources" ,
482+ ] ,
483+ warnings : [ ] ,
484+ } ) ;
485+
486+ const expectedFingerprint = hashCodexAppServerBindingFingerprint ( rawFingerprint ) ;
487+ await expect ( store . lookup ( conversationKey ) ) . resolves . toMatchObject ( {
488+ state : "active" ,
489+ binding : { dynamicToolsFingerprint : expectedFingerprint } ,
490+ } ) ;
491+ await expect ( store . lookup ( sessionBindingKey ) ) . resolves . toMatchObject ( {
492+ state : "active" ,
493+ sessionId,
494+ binding : { dynamicToolsFingerprint : expectedFingerprint } ,
495+ } ) ;
496+ await expect ( fs . access ( fixture . sidecarPath ) ) . rejects . toThrow ( ) ;
497+ await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
498+ await expect ( fixture . migration . migrateLegacyState ( fixture . params ) ) . resolves . toEqual ( {
499+ changes : [ ] ,
500+ warnings : [ ] ,
501+ } ) ;
502+
503+ await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
504+ } ) ;
505+
356506 it ( "matches an owner through the contained fallback for a stale session file locator" , async ( ) => {
357507 const sessionKey = "agent:main:stale-locator" ;
358508 const fixture = await createBindingMigrationFixture ( {
0 commit comments