@@ -480,6 +480,187 @@ const runCleanupTest = <A, E>(
480480 > ,
481481) => effect . pipe ( Effect . provide ( cleanupTestLayer ) , Effect . scoped ) ;
482482
483+ const submoduleCleanupFixture = ( ) =>
484+ Effect . gen ( function * ( ) {
485+ const fixture = yield * cleanupFixture ( ) ;
486+ const { command, fs, worktree } = fixture ;
487+ const source = yield * fs . makeTempDirectoryScoped ( { prefix : "cleanup-submodule-" } ) ;
488+ const remote = yield * fs . makeTempDirectoryScoped ( { prefix : "cleanup-submodule-remote-" } ) ;
489+ yield * command ( source , [ "init" , "-b" , "main" ] ) ;
490+ yield * command ( source , [ "config" , "user.email" , "[email protected] " ] ) ; 491+ yield * command ( source , [ "config" , "user.name" , "Cleanup test" ] ) ;
492+ yield * fs . writeFileString ( `${ source } /tracked.txt` , "original\n" ) ;
493+ yield * fs . writeFileString ( `${ source } /.gitignore` , ".env\nnode_modules/\n" ) ;
494+ yield * command ( source , [ "add" , "." ] ) ;
495+ yield * command ( source , [ "commit" , "-m" , "initial" ] ) ;
496+ yield * command ( source , [ "clone" , "--bare" , source , remote ] ) ;
497+ yield * command ( worktree , [
498+ "-c" ,
499+ "protocol.file.allow=always" ,
500+ "submodule" ,
501+ "add" ,
502+ remote ,
503+ "module" ,
504+ ] ) ;
505+ yield * command ( `${ worktree } /module` , [ "config" , "user.name" , "Cleanup test" ] ) ;
506+ yield * command ( `${ worktree } /module` , [ "config" , "user.email" , "[email protected] " ] ) ; 507+ yield * command ( worktree , [ "commit" , "-am" , "add submodule" ] ) ;
508+ const submodule = `${ worktree } /module` ;
509+ const addNested = ( ) =>
510+ Effect . gen ( function * ( ) {
511+ yield * command ( submodule , [
512+ "-c" ,
513+ "protocol.file.allow=always" ,
514+ "submodule" ,
515+ "add" ,
516+ remote ,
517+ "child" ,
518+ ] ) ;
519+ yield * command ( submodule , [ "commit" , "-am" , "add child" ] ) ;
520+ yield * command ( submodule , [ "push" , "origin" , "HEAD:main" ] ) ;
521+ yield * command ( worktree , [ "commit" , "-am" , "update module" ] ) ;
522+ return `${ submodule } /child` ;
523+ } ) ;
524+ return { ...fixture , submodule, addNested } ;
525+ } ) ;
526+
527+ it . live . each ( [ "populated" , "deinitialized" ] ) (
528+ "removes a clean worktree with %s nested submodules" ,
529+ ( state ) =>
530+ runCleanupTest (
531+ Effect . gen ( function * ( ) {
532+ const { service, fs, worktree, submodule, command, addNested } =
533+ yield * submoduleCleanupFixture ( ) ;
534+ yield * addNested ( ) ;
535+ const unpushed = yield * command ( submodule , [
536+ "rev-list" ,
537+ "--count" ,
538+ "HEAD" ,
539+ "--not" ,
540+ "--remotes" ,
541+ ] ) ;
542+ expect ( unpushed . stdout . trim ( ) ) . toBe ( "0" ) ;
543+ if ( state === "deinitialized" ) yield * command ( worktree , [ "submodule" , "deinit" , "--all" ] ) ;
544+ const report = yield * service . runNow ;
545+ expect ( report . entries . find ( ( entry ) => entry . path === worktree ) ) . toMatchObject ( {
546+ outcome : "removed" ,
547+ } ) ;
548+ expect ( yield * fs . exists ( worktree ) ) . toBe ( false ) ;
549+ } ) ,
550+ ) ,
551+ ) ;
552+
553+ it . live . each ( [ "HEAD" , "local branch" ] ) ( "keeps an unpushed submodule %s" , ( unpushed ) =>
554+ runCleanupTest (
555+ Effect . gen ( function * ( ) {
556+ const { service, fs, worktree, submodule, command } = yield * submoduleCleanupFixture ( ) ;
557+ yield * command ( submodule , [ "checkout" , "-b" , "local-only" ] ) ;
558+ yield * command ( submodule , [ "commit" , "--allow-empty" , "-m" , "local only" ] ) ;
559+ if ( unpushed === "local branch" ) yield * command ( submodule , [ "checkout" , "main" ] ) ;
560+ yield * command ( worktree , [ "add" , "module" ] ) ;
561+ yield * command ( worktree , [ "commit" , "--allow-empty" , "-m" , "record module" ] ) ;
562+ const report = yield * service . runNow ;
563+ expect ( report . entries [ 0 ] ) . toMatchObject ( { outcome : "kept" } ) ;
564+ expect ( report . entries [ 0 ] ?. reason ) . toContain ( "not on a remote-tracking ref" ) ;
565+ expect ( yield * fs . exists ( submodule ) ) . toBe ( true ) ;
566+ } ) ,
567+ ) ,
568+ ) ;
569+
570+ it . live . each ( [
571+ [ "tracked.txt" , "uncommitted-changes" ] ,
572+ [ ".env" , "any-local-files" ] ,
573+ ] as const ) ( "keeps submodule %s under %s" , ( [ file , policy ] ) =>
574+ runCleanupTest (
575+ Effect . gen ( function * ( ) {
576+ const { service, fs, submodule, setPolicy } = yield * submoduleCleanupFixture ( ) ;
577+ setPolicy ( policy ) ;
578+ yield * fs . writeFileString ( `${ submodule } /${ file } ` , "local edit\n" ) ;
579+ const report = yield * service . runNow ;
580+ expect ( report . entries [ 0 ] ) . toMatchObject ( { outcome : "kept" } ) ;
581+ if ( file === ".env" ) expect ( report . entries [ 0 ] ?. reason ) . toContain ( "ignored files (.env)" ) ;
582+ expect ( yield * fs . readFileString ( `${ submodule } /${ file } ` ) ) . toBe ( "local edit\n" ) ;
583+ } ) ,
584+ ) ,
585+ ) ;
586+
587+ it . live ( "keeps a deinitialized nested submodule's local branch in an embedded parent" , ( ) =>
588+ runCleanupTest (
589+ Effect . gen ( function * ( ) {
590+ const { service, fs, worktree, submodule, command, addNested } =
591+ yield * submoduleCleanupFixture ( ) ;
592+ const child = yield * addNested ( ) ;
593+ for ( const [ key , value ] of [
594+ [ "user.name" , "Cleanup test" ] ,
595+ [ "user.email" , "[email protected] " ] , 596+ ] as const )
597+ yield * command ( child , [ "config" , key , value ] ) ;
598+ yield * command ( child , [ "checkout" , "-b" , "local-only" ] ) ;
599+ yield * command ( child , [ "commit" , "--allow-empty" , "-m" , "local only" ] ) ;
600+ const local = ( yield * command ( child , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
601+ yield * command ( child , [ "checkout" , "main" ] ) ;
602+ yield * command ( submodule , [ "submodule" , "deinit" , "--all" ] ) ;
603+ const parentGit = ( yield * command ( submodule , [
604+ "rev-parse" ,
605+ "--absolute-git-dir" ,
606+ ] ) ) . stdout . trim ( ) ;
607+ yield * command ( submodule , [ "config" , "core.worktree" , submodule ] ) ;
608+ yield * fs . remove ( `${ submodule } /.git` ) ;
609+ yield * fs . rename ( parentGit , `${ submodule } /.git` ) ;
610+ const childGit = `${ submodule } /.git/modules/child` ;
611+ const report = yield * service . runNow ;
612+ expect ( report . entries [ 0 ] ) . toMatchObject ( { outcome : "kept" } ) ;
613+ expect ( report . entries [ 0 ] ?. reason ) . toContain ( "not on a remote-tracking ref" ) ;
614+ expect ( yield * fs . exists ( `${ childGit } /HEAD` ) ) . toBe ( true ) ;
615+ expect (
616+ ( yield * command ( worktree , [
617+ "--git-dir" ,
618+ childGit ,
619+ "rev-parse" ,
620+ "refs/heads/local-only" ,
621+ ] ) ) . stdout . trim ( ) ,
622+ ) . toBe ( local ) ;
623+ } ) ,
624+ ) ,
625+ ) ;
626+
627+ it . live ( "keeps a worktree when a stored submodule cannot be inspected" , ( ) =>
628+ runCleanupTest (
629+ Effect . gen ( function * ( ) {
630+ const { service, fs, worktree, command } = yield * submoduleCleanupFixture ( ) ;
631+ const admin = ( yield * command ( worktree , [ "rev-parse" , "--absolute-git-dir" ] ) ) . stdout . trim ( ) ;
632+ yield * fs . makeDirectory ( `${ admin } /modules/broken` ) ;
633+ yield * fs . writeFileString ( `${ admin } /modules/broken/HEAD` , "not a ref\n" ) ;
634+ const report = yield * service . runNow ;
635+ expect ( report . entries [ 0 ] ) . toMatchObject ( { outcome : "kept" } ) ;
636+ expect ( report . entries [ 0 ] ?. reason ) . toContain ( "could not be inspected" ) ;
637+ expect ( yield * fs . exists ( worktree ) ) . toBe ( true ) ;
638+ expect ( yield * fs . readFileString ( `${ admin } /modules/broken/HEAD` ) ) . toBe ( "not a ref\n" ) ;
639+ } ) ,
640+ ) ,
641+ ) ;
642+
643+ it . live ( "preserves late tracked edits with never-initialized submodules" , ( ) =>
644+ runCleanupTest (
645+ Effect . gen ( function * ( ) {
646+ const fixture = yield * cleanupFixture ( ) ;
647+ const { service, fs, worktree, command } = fixture ;
648+ const head = ( yield * command ( worktree , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
649+ yield * fs . writeFileString (
650+ `${ worktree } /.gitmodules` ,
651+ '[submodule "module"]\n\tpath = module\n\turl = /unused\n' ,
652+ ) ;
653+ yield * command ( worktree , [ "add" , ".gitmodules" ] ) ;
654+ yield * command ( worktree , [ "update-index" , "--add" , "--cacheinfo" , `160000,${ head } ,module` ] ) ;
655+ yield * command ( worktree , [ "commit" , "-m" , "add uninitialized module" ] ) ;
656+ yield * fs . makeDirectory ( `${ worktree } /module` ) ;
657+ fixture . editBeforeRemoval ( ) ;
658+ expect ( ( yield * service . runNow ) . entries [ 0 ] ) . toMatchObject ( { outcome : "failed" } ) ;
659+ expect ( yield * fs . readFileString ( `${ worktree } /tracked.txt` ) ) . toBe ( "late edit\n" ) ;
660+ } ) ,
661+ ) ,
662+ ) ;
663+
483664describe ( "subagent worktree ownership" , ( ) => {
484665 it . effect . each ( [
485666 "idle" ,
0 commit comments