@@ -728,6 +728,68 @@ describe("createOptimizedContextStore checkpoint", () => {
728728 expect ( tree ) . not . toContain ( "partial.jsonl" ) ;
729729 expect ( tree ) . not . toContain ( "untracked-junk.txt" ) ;
730730 } ) ;
731+
732+ test ( "commits an on-disk extra turn segment that is not in HEAD without writeTurns" , async ( ) => {
733+ const dir = tempDir ( ) ;
734+ const store = await createOptimizedContextStore ( dir ) ;
735+ await store . writeTurns ( [ turn ( "a" ) ] ) ;
736+ await store . writePrompt ( [ turn ( "p0" ) ] ) ;
737+ await store . writeMetadata ( EMPTY_CHECKPOINT_METADATA ) ;
738+ const first = await store . commit ( { message : "base" } ) ;
739+
740+ const extraTurns = segmentFileName ( TURNS_FILE , 1 ) ;
741+ const extraPrompt = segmentFileName ( "prompt.jsonl" , 1 ) ;
742+ fs . writeFileSync ( path . join ( dir , extraTurns ) , jsonl ( [ turn ( "b" ) ] ) ) ;
743+ fs . writeFileSync ( path . join ( dir , extraPrompt ) , jsonl ( [ turn ( "p" ) ] ) ) ;
744+
745+ const crashed = await createOptimizedContextStore ( dir ) ;
746+ const second = await crashed . commit ( { message : "heal extra segments" } ) ;
747+ expect ( second . hash ) . not . toBe ( first . hash ) ;
748+
749+ const tree = await gitLsTree ( dir ) ;
750+ expect ( tree ) . toContain ( extraTurns ) ;
751+ expect ( tree ) . toContain ( extraPrompt ) ;
752+ } ) ;
753+
754+ test ( "pending extra turn segment prevents empty-checkpoint skip" , async ( ) => {
755+ const dir = tempDir ( ) ;
756+ const store = await createOptimizedContextStore ( dir ) ;
757+ const turns : ConversationTurn [ ] = [ ] ;
758+ const big = "x" . repeat ( 20_000 ) ;
759+ for ( let i = 0 ; i < 14 ; i ++ ) {
760+ turns . push ( turn ( `${ i } -${ big } ` ) ) ;
761+ await store . writeTurns ( [ ...turns ] ) ;
762+ }
763+ await store . writeMetadata ( EMPTY_CHECKPOINT_METADATA ) ;
764+ const first = await store . commit ( { message : "base" } ) ;
765+
766+ for ( let i = 14 ; i < 18 ; i ++ ) {
767+ turns . push ( turn ( `${ i } -${ big } ` ) ) ;
768+ await store . writeTurns ( [ ...turns ] ) ;
769+ }
770+ const extraTurns = segmentFileName ( TURNS_FILE , 1 ) ;
771+ expect ( fs . existsSync ( path . join ( dir , extraTurns ) ) ) . toBe ( true ) ;
772+
773+ const second = await store . commit ( { message : "pending extra" } ) ;
774+ expect ( second . hash ) . not . toBe ( first . hash ) ;
775+ expect ( await gitLsTree ( dir ) ) . toContain ( extraTurns ) ;
776+ } ) ;
777+
778+ test ( "staged compact with unchanged disk still commits instead of returning HEAD" , async ( ) => {
779+ const dir = tempDir ( ) ;
780+ const store = await createOptimizedContextStore ( dir ) ;
781+ await store . writeTurns ( [ turn ( "keep-a" ) , turn ( "keep-b" ) , turn ( "drop-me" ) ] ) ;
782+ await store . writeMetadata ( EMPTY_CHECKPOINT_METADATA ) ;
783+ const first = await store . commit ( { message : "published original" } ) ;
784+
785+ await store . writeTurns ( [ turn ( "[Compacted prior context]" ) , turn ( "keep-b" ) ] ) ;
786+ const second = await store . commit ( { message : "publish compact" } ) ;
787+ expect ( second . hash ) . not . toBe ( first . hash ) ;
788+ expect ( turnTexts ( ( await store . load ( ) ) . turns ) ) . toEqual ( [
789+ "[Compacted prior context]" ,
790+ "keep-b" ,
791+ ] ) ;
792+ } ) ;
731793} ) ;
732794
733795describe ( "createSessionStores" , ( ) => {
0 commit comments