@@ -876,6 +876,20 @@ export const make = Effect.gen(function* () {
876876 return `path:${ normalizeProjectPathForComparison ( realPath ) } ` ;
877877 } ) ;
878878
879+ /** Stats a directory that may be offered as a project, or none when it is missing or excluded. */
880+ const projectDirectoryStats = Effect . fn ( "AgentSessionScanner.projectDirectoryStats" ) ( function * (
881+ directory : string ,
882+ ) {
883+ if ( isExcludedProjectPath ( directory ) ) return Option . none < FileSystem . File . Info > ( ) ;
884+ const stats = yield * statOption ( directory ) ;
885+ if ( Option . isNone ( stats ) || stats . value . type !== "Directory" ) return Option . none ( ) ;
886+ const realPath = yield * fileSystem
887+ . realPath ( directory )
888+ . pipe ( Effect . orElseSucceed ( ( ) => directory ) ) ;
889+ // Symlinks can disguise excluded worktree paths; re-check their targets.
890+ return isExcludedProjectPath ( realPath ) ? Option . none ( ) : stats ;
891+ } ) ;
892+
879893 // Paths whose own read outlived the deadline, mapped to when they may be read
880894 // again. Without this every later scan would wait out the deadline again for
881895 // each phase that touches them.
@@ -929,7 +943,11 @@ export const make = Effect.gen(function* () {
929943 ) {
930944 type GitIdentity =
931945 | { readonly _tag : "Repository" ; readonly git : AgentSessionProjectGit | null }
932- | { readonly _tag : "Worktree" }
946+ | {
947+ readonly _tag : "Worktree" ;
948+ readonly mainCheckout : string | null ;
949+ readonly git : AgentSessionProjectGit | null ;
950+ }
933951 | { readonly _tag : "NotGit" } ;
934952 const identities = new Map < string , GitIdentity > ( ) ;
935953 const repositories : Array < { directory : string ; gitDir : string ; linkedWorktree : boolean } > = [ ] ;
@@ -962,8 +980,9 @@ export const make = Effect.gen(function* () {
962980 ) ;
963981 for ( const { directory, gitDir } of linkedWorktrees ) {
964982 const commonDir = ( commonDirTexts . get ( path . join ( gitDir , "commondir" ) ) ?? "" ) . trim ( ) ;
965- if ( commonDir . length === 0 ) identities . set ( directory , { _tag : "Worktree" } ) ;
966- else
983+ if ( commonDir . length === 0 ) {
984+ identities . set ( directory , { _tag : "Worktree" , mainCheckout : null , git : null } ) ;
985+ } else
967986 repositories . push ( {
968987 directory,
969988 gitDir : path . resolve ( gitDir , commonDir ) ,
@@ -976,20 +995,25 @@ export const make = Effect.gen(function* () {
976995 ) ;
977996 for ( const { directory, gitDir, linkedWorktree } of repositories ) {
978997 const configText = configTexts . get ( path . join ( gitDir , "config" ) ) ?? "" ;
998+ const originUrl = parseOriginUrlFromGitConfig ( configText ) ;
999+ const git : AgentSessionProjectGit = {
1000+ remoteKey :
1001+ originUrl === null ? null : sourceControlDefinitions . repositoryKeyFromRemote ( originUrl ) ,
1002+ repository : repositoryPathFromRemoteUrl ( originUrl ) ,
1003+ } ;
9791004 // Bare repositories have no main checkout, so offer their linked worktrees independently.
9801005 if ( linkedWorktree && ! isBareGitConfig ( configText ) ) {
981- identities . set ( directory , { _tag : "Worktree" } ) ;
1006+ identities . set ( directory , {
1007+ _tag : "Worktree" ,
1008+ // Only a common dir named .git sits inside its main checkout, and an
1009+ // unreadable config can't rule out a bare repository.
1010+ mainCheckout :
1011+ configText . length > 0 && path . basename ( gitDir ) === ".git" ? path . dirname ( gitDir ) : null ,
1012+ git,
1013+ } ) ;
9821014 continue ;
9831015 }
984- const originUrl = parseOriginUrlFromGitConfig ( configText ) ;
985- identities . set ( directory , {
986- _tag : "Repository" ,
987- git : {
988- remoteKey :
989- originUrl === null ? null : sourceControlDefinitions . repositoryKeyFromRemote ( originUrl ) ,
990- repository : repositoryPathFromRemoteUrl ( originUrl ) ,
991- } ,
992- } ) ;
1016+ identities . set ( directory , { _tag : "Repository" , git } ) ;
9931017 }
9941018 return identities ;
9951019 } ) ;
@@ -1701,54 +1725,67 @@ export const make = Effect.gen(function* () {
17011725 const expanded = expandHomePath ( candidate . cwd . trim ( ) , homeDir ) ;
17021726 if ( ! path . isAbsolute ( expanded ) ) continue ;
17031727 const resolved = path . resolve ( expanded ) ;
1704- if ( directories . has ( resolved ) || isExcludedProjectPath ( resolved ) ) continue ;
1705- const stats = yield * statOption ( resolved ) ;
1706- if ( Option . isNone ( stats ) || stats . value . type !== "Directory" ) {
1707- directories . set ( resolved , Option . none ( ) ) ;
1708- continue ;
1709- }
1710- const realPath = yield * fileSystem
1711- . realPath ( resolved )
1712- . pipe ( Effect . orElseSucceed ( ( ) => resolved ) ) ;
1713- // Symlinks can disguise excluded worktree paths; re-check their targets.
1714- directories . set ( resolved , isExcludedProjectPath ( realPath ) ? Option . none ( ) : stats ) ;
1728+ if ( directories . has ( resolved ) ) continue ;
1729+ directories . set ( resolved , yield * projectDirectoryStats ( resolved ) ) ;
17151730 }
17161731 const directoryGitIdentities = yield * readGitIdentities (
17171732 Array . from ( directories ) . flatMap ( ( [ directory , stats ] ) =>
17181733 Option . isSome ( stats ) ? [ directory ] : [ ] ,
17191734 ) ,
17201735 ) ;
17211736
1722- const directoryKeys = new Map < string , string > ( ) ;
1723- const gitIdentities = new Map < string , AgentSessionProjectGit | null > ( ) ;
1737+ const directoryProjects = new Map <
1738+ string ,
1739+ {
1740+ readonly key : string ;
1741+ readonly path : string ;
1742+ readonly git : AgentSessionProjectGit | null ;
1743+ } | null
1744+ > ( ) ;
17241745
17251746 for ( const candidate of raw ) {
17261747 const expanded = expandHomePath ( candidate . cwd . trim ( ) , homeDir ) ;
17271748 if ( ! path . isAbsolute ( expanded ) ) continue ;
17281749 const resolved = path . resolve ( expanded ) ;
17291750 const stats = directories . get ( resolved ) ;
17301751 if ( stats === undefined || Option . isNone ( stats ) ) continue ;
1731- let key = directoryKeys . get ( resolved ) ;
1732- if ( key === undefined ) {
1752+ let project = directoryProjects . get ( resolved ) ;
1753+ if ( project === undefined ) {
17331754 const gitIdentity = directoryGitIdentities . get ( resolved ) ;
1734- if ( gitIdentity === undefined || gitIdentity . _tag === "Worktree" ) {
1735- key = "" ;
1755+ if ( gitIdentity === undefined ) {
1756+ project = null ;
1757+ } else if ( gitIdentity . _tag === "Worktree" ) {
1758+ // A linked worktree's sessions count toward its main checkout's project.
1759+ const { mainCheckout } = gitIdentity ;
1760+ const mainStats =
1761+ mainCheckout === null ? Option . none ( ) : yield * projectDirectoryStats ( mainCheckout ) ;
1762+ project =
1763+ mainCheckout !== null && Option . isSome ( mainStats )
1764+ ? {
1765+ key : yield * directoryIdentity ( mainCheckout , mainStats . value ) ,
1766+ path : mainCheckout ,
1767+ git : gitIdentity . git ,
1768+ }
1769+ : null ;
17361770 } else {
1737- key = yield * directoryIdentity ( resolved , stats . value ) ;
1738- gitIdentities . set ( key , gitIdentity . _tag === "Repository" ? gitIdentity . git : null ) ;
1771+ project = {
1772+ key : yield * directoryIdentity ( resolved , stats . value ) ,
1773+ path : resolved ,
1774+ git : gitIdentity . _tag === "Repository" ? gitIdentity . git : null ,
1775+ } ;
17391776 }
1740- directoryKeys . set ( resolved , key ) ;
1777+ directoryProjects . set ( resolved , project ) ;
17411778 }
1742- if ( key === "" ) continue ;
1779+ if ( project === null ) continue ;
17431780
1744- const existing = merged . get ( key ) ;
1781+ const existing = merged . get ( project . key ) ;
17451782 if ( ! existing ) {
1746- merged . set ( key , {
1747- path : resolved ,
1783+ merged . set ( project . key , {
1784+ path : project . path ,
17481785 sources : [ candidate . source ] ,
17491786 threadCount : candidate . threadCount ,
17501787 lastActiveAtMs : candidate . lastActiveAtMs ,
1751- git : gitIdentities . get ( key ) ?? null ,
1788+ git : project . git ,
17521789 } ) ;
17531790 continue ;
17541791 }
0 commit comments