@@ -60,21 +60,50 @@ function extractProjectMcpConfig(projectDir: string): any {
6060 }
6161}
6262
63+ // Get a safe directory name from project path, with collision handling
64+ function getProjectWorktreeDirName ( projectDir : string ) : string {
65+ const baseName = path . basename ( projectDir ) ;
66+ const worktreesBaseDir = path . join ( os . homedir ( ) , "worktrees" ) ;
67+ const candidatePath = path . join ( worktreesBaseDir , baseName ) ;
68+
69+ // If directory doesn't exist or points to the same project, use base name
70+ if ( ! fs . existsSync ( candidatePath ) ) {
71+ return baseName ;
72+ }
73+
74+ // Check if existing directory is for the same project by reading a marker file
75+ const markerFile = path . join ( candidatePath , ".fleetcode-project" ) ;
76+ if ( fs . existsSync ( markerFile ) ) {
77+ const existingProjectPath = fs . readFileSync ( markerFile , "utf-8" ) . trim ( ) ;
78+ if ( existingProjectPath === projectDir ) {
79+ return baseName ;
80+ }
81+ }
82+
83+ // Collision detected - append short hash
84+ const crypto = require ( "crypto" ) ;
85+ const hash = crypto . createHash ( "md5" ) . update ( projectDir ) . digest ( "hex" ) . substring ( 0 , 6 ) ;
86+ return `${ baseName } -${ hash } ` ;
87+ }
88+
6389// Write MCP config file for a project (shared across all sessions)
6490function writeMcpConfigFile ( projectDir : string , mcpServers : any ) : string | null {
6591 try {
66- // Create a hash of the project directory for unique filename
67- const crypto = require ( "crypto" ) ;
68- const hash = crypto . createHash ( "md5" ) . update ( projectDir ) . digest ( "hex" ) . substring ( 0 , 8 ) ;
69-
92+ const projectDirName = getProjectWorktreeDirName ( projectDir ) ;
7093 const worktreesDir = path . join ( os . homedir ( ) , "worktrees" ) ;
7194 if ( ! fs . existsSync ( worktreesDir ) ) {
7295 fs . mkdirSync ( worktreesDir , { recursive : true } ) ;
7396 }
7497
75- const configFilePath = path . join ( worktreesDir , ` mcp-config- ${ hash } .json` ) ;
98+ const configFilePath = path . join ( worktreesDir , projectDirName , " mcp-config.json" ) ;
7699 const configContent = JSON . stringify ( { mcpServers } , null , 2 ) ;
77100
101+ // Ensure project worktree directory exists
102+ const projectWorktreeDir = path . join ( worktreesDir , projectDirName ) ;
103+ if ( ! fs . existsSync ( projectWorktreeDir ) ) {
104+ fs . mkdirSync ( projectWorktreeDir , { recursive : true } ) ;
105+ }
106+
78107 fs . writeFileSync ( configFilePath , configContent , "utf8" ) ;
79108
80109 return configFilePath ;
@@ -273,12 +302,9 @@ function spawnSessionPty(
273302async function createWorktree ( projectDir : string , parentBranch : string , sessionNumber : number , sessionUuid : string , customBranchName ?: string ) : Promise < { worktreePath : string ; branchName : string } > {
274303 const git = simpleGit ( projectDir ) ;
275304
276- // Create a hash of the project directory for unique worktree directory
277- const crypto = require ( "crypto" ) ;
278- const hash = crypto . createHash ( "md5" ) . update ( projectDir ) . digest ( "hex" ) . substring ( 0 , 8 ) ;
279-
305+ const projectDirName = getProjectWorktreeDirName ( projectDir ) ;
280306 const worktreesBaseDir = path . join ( os . homedir ( ) , "worktrees" ) ;
281- const projectWorktreeDir = path . join ( worktreesBaseDir , hash ) ;
307+ const projectWorktreeDir = path . join ( worktreesBaseDir , projectDirName ) ;
282308 const worktreeName = customBranchName || `session${ sessionNumber } ` ;
283309 const worktreePath = path . join ( projectWorktreeDir , worktreeName ) ;
284310
@@ -295,6 +321,10 @@ async function createWorktree(projectDir: string, parentBranch: string, sessionN
295321 // Create worktrees directory if it doesn't exist
296322 if ( ! fs . existsSync ( projectWorktreeDir ) ) {
297323 fs . mkdirSync ( projectWorktreeDir , { recursive : true } ) ;
324+
325+ // Write marker file to track which project this directory belongs to
326+ const markerFile = path . join ( projectWorktreeDir , ".fleetcode-project" ) ;
327+ fs . writeFileSync ( markerFile , projectDir , "utf-8" ) ;
298328 }
299329
300330 // Check if worktree already exists and remove it
0 commit comments