@@ -112,14 +112,17 @@ impl WorkspaceCli {
112112/// Find workspace root by looking for .cortex directory or .git
113113fn find_workspace_root ( ) -> PathBuf {
114114 let cwd = std:: env:: current_dir ( ) . unwrap_or_else ( |_| PathBuf :: from ( "." ) ) ;
115+ find_workspace_root_from ( cwd)
116+ }
115117
118+ fn find_workspace_root_from ( cwd : PathBuf ) -> PathBuf {
116119 let mut current = cwd. clone ( ) ;
117120 loop {
118121 // Check for .cortex directory
119- if current. join ( ".cortex" ) . exists ( ) {
122+ if current. join ( ".cortex" ) . is_dir ( ) {
120123 return current;
121124 }
122- // Check for .git directory (fallback root indicator)
125+ // Check for .git marker (fallback root indicator; worktrees use a file )
123126 if current. join ( ".git" ) . exists ( ) {
124127 return current;
125128 }
@@ -417,6 +420,46 @@ async fn run_edit(args: WorkspaceEditArgs) -> Result<()> {
417420#[ cfg( test) ]
418421mod tests {
419422 use super :: * ;
423+ use tempfile:: TempDir ;
424+
425+ // ==========================================================================
426+ // Workspace root detection tests
427+ // ==========================================================================
428+
429+ #[ test]
430+ fn test_find_workspace_root_uses_cortex_directory ( ) {
431+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
432+ let root = temp_dir. path ( ) . join ( "root" ) ;
433+ let nested = root. join ( "nested" ) . join ( "sub" ) ;
434+ std:: fs:: create_dir_all ( root. join ( ".cortex" ) ) . unwrap ( ) ;
435+ std:: fs:: create_dir_all ( & nested) . unwrap ( ) ;
436+
437+ assert_eq ! ( find_workspace_root_from( nested) , root) ;
438+ }
439+
440+ #[ test]
441+ fn test_find_workspace_root_ignores_cortex_file ( ) {
442+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
443+ let root = temp_dir. path ( ) . join ( "root" ) ;
444+ let nested = root. join ( "nested" ) ;
445+ let subdir = nested. join ( "sub" ) ;
446+ std:: fs:: create_dir_all ( root. join ( ".cortex" ) ) . unwrap ( ) ;
447+ std:: fs:: create_dir_all ( & subdir) . unwrap ( ) ;
448+ std:: fs:: write ( nested. join ( ".cortex" ) , "not a directory" ) . unwrap ( ) ;
449+
450+ assert_eq ! ( find_workspace_root_from( subdir) , root) ;
451+ }
452+
453+ #[ test]
454+ fn test_find_workspace_root_keeps_git_file_fallback ( ) {
455+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
456+ let root = temp_dir. path ( ) . join ( "root" ) ;
457+ let nested = root. join ( "nested" ) . join ( "sub" ) ;
458+ std:: fs:: create_dir_all ( & nested) . unwrap ( ) ;
459+ std:: fs:: write ( root. join ( ".git" ) , "gitdir: ../actual.git" ) . unwrap ( ) ;
460+
461+ assert_eq ! ( find_workspace_root_from( nested) , root) ;
462+ }
420463
421464 // ==========================================================================
422465 // WorkspaceSettings tests
0 commit comments