@@ -51,39 +51,95 @@ export namespace Project {
5151 const git = await matches . next ( ) . then ( ( x ) => x . value )
5252 await matches . return ( )
5353 if ( git ) {
54+ // Task 1.1: Resolve worktree path early before ID generation
5455 let worktree = path . dirname ( git )
55- let id = await Bun . file ( path . join ( git , "opencode" ) )
56+ worktree = await $ `git rev-parse --show-toplevel`
57+ . quiet ( )
58+ . nothrow ( )
59+ . cwd ( worktree )
60+ . text ( )
61+ . then ( ( x ) => path . resolve ( worktree , x . trim ( ) ) )
62+
63+ // Task 1.2: Resolve actual gitDir (handles linked worktrees where .git is a file)
64+ // May be relative for main worktrees, absolute for linked
65+ const gitDirRaw = await $ `git rev-parse --git-dir`
66+ . quiet ( )
67+ . nothrow ( )
68+ . cwd ( worktree )
5669 . text ( )
5770 . then ( ( x ) => x . trim ( ) )
58- . catch ( ( ) => { } )
59- if ( ! id ) {
60- const roots = await $ `git rev-list --max-parents=0 --all`
61- . quiet ( )
62- . nothrow ( )
63- . cwd ( worktree )
64- . text ( )
65- . then ( ( x ) =>
66- x
67- . split ( "\n" )
68- . filter ( Boolean )
69- . map ( ( x ) => x . trim ( ) )
70- . toSorted ( ) ,
71- )
72- id = roots [ 0 ]
73- if ( id ) Bun . file ( path . join ( git , "opencode" ) ) . write ( id )
71+ // Fall back to Filesystem.up result if git command fails
72+ const gitDir = gitDirRaw ? path . resolve ( worktree , gitDirRaw ) : git
73+
74+ // Task 1.3: Detect linked worktree (case-insensitive for Windows)
75+ const normalizedGitDir = path . normalize ( gitDir ) . toLowerCase ( )
76+ const worktreeMarker = path . join ( ".git" , "worktrees" ) . toLowerCase ( )
77+ const isLinkedWorktree = normalizedGitDir . includes ( worktreeMarker )
78+ log . info ( "worktree detection" , { isLinkedWorktree, gitDir : normalizedGitDir } )
79+
80+ // Task 1.4: Read caches
81+ const cachedRootCommit = await Bun . file ( path . join ( gitDir , "opencode" ) )
82+ . text ( )
83+ . then ( ( x ) => x . trim ( ) )
84+ . catch ( ( ) => undefined )
85+ const cachedWorktreeHash = isLinkedWorktree
86+ ? await Bun . file ( path . join ( gitDir , "opencode-worktree" ) )
87+ . text ( )
88+ . then ( ( x ) => x . trim ( ) )
89+ . catch ( ( ) => undefined )
90+ : undefined
91+
92+ // Return early with cached ID if both are available
93+ if ( cachedRootCommit && ( ! isLinkedWorktree || cachedWorktreeHash ) ) {
94+ const id = isLinkedWorktree ? `${ cachedRootCommit } -${ cachedWorktreeHash } ` : cachedRootCommit
95+ return { id, worktree, vcs : "git" }
7496 }
75- if ( ! id )
97+
98+ // Compute root commit if needed
99+ const roots = await $ `git rev-list --max-parents=0 --all`
100+ . quiet ( )
101+ . nothrow ( )
102+ . cwd ( worktree )
103+ . text ( )
104+ . then ( ( x ) =>
105+ x
106+ . split ( "\n" )
107+ . filter ( Boolean )
108+ . map ( ( x ) => x . trim ( ) )
109+ . toSorted ( ) ,
110+ )
111+ const rootCommit = roots [ 0 ]
112+ if ( ! rootCommit ) {
76113 return {
77114 id : "global" ,
78115 worktree,
79116 vcs : "git" ,
80117 }
81- worktree = await $ `git rev-parse --show-toplevel`
82- . quiet ( )
83- . nothrow ( )
84- . cwd ( worktree )
85- . text ( )
86- . then ( ( x ) => path . resolve ( worktree , x . trim ( ) ) )
118+ }
119+
120+ // Task 1.5: Generate differentiated ID (cross-platform safe)
121+ // Normalize path separators for consistent hashing across platforms (WSL + Windows)
122+ const normalizedPath = worktree . replace ( / \\ / g, "/" )
123+ const worktreeHash = isLinkedWorktree ? Bun . hash ( normalizedPath ) . toString ( 16 ) : undefined
124+ const id = isLinkedWorktree ? `${ rootCommit } -${ worktreeHash } ` : rootCommit
125+
126+ // Task 1.6: Write caches (awaited - fixes existing bug where writes weren't awaited)
127+ if ( isLinkedWorktree && worktreeHash ) {
128+ await Bun . file ( path . join ( gitDir , "opencode-worktree" ) ) . write ( worktreeHash )
129+ }
130+ if ( ! cachedRootCommit ) {
131+ await Bun . file ( path . join ( gitDir , "opencode" ) ) . write ( rootCommit )
132+ }
133+
134+ // Task 2.1-2.3: Migration hook (linked worktrees only, with race protection)
135+ if ( isLinkedWorktree ) {
136+ const newProjectExists = await Storage . read ( [ "project" , id ] ) . catch ( ( ) => undefined )
137+ if ( ! newProjectExists ) {
138+ await migrateWorktreeSessions ( rootCommit , id , worktree )
139+ await cleanupLegacyProject ( rootCommit )
140+ }
141+ }
142+
87143 return { id, worktree, vcs : "git" }
88144 }
89145
@@ -182,6 +238,55 @@ export namespace Project {
182238 } )
183239 }
184240
241+ // Project ID formats:
242+ // - Main worktree: "{rootCommit}" (e.g., "a1b2c3d4...")
243+ // - Linked worktree: "{rootCommit}-{pathHash}" (e.g., "a1b2c3d4...-7f8a9b2c")
244+ // The separator is "-" (not "|") because project IDs are used in filesystem paths
245+
246+ // Task 2.2: Migrate sessions from old project ID to new project ID for linked worktrees
247+ async function migrateWorktreeSessions ( oldProjectID : string , newProjectID : string , worktree : string ) {
248+ const oldSessions = await Storage . list ( [ "session" , oldProjectID ] ) . catch ( ( ) => [ ] )
249+ if ( oldSessions . length === 0 ) return
250+
251+ log . info ( "migrating worktree sessions" , {
252+ from : oldProjectID ,
253+ to : newProjectID ,
254+ worktree,
255+ count : oldSessions . length ,
256+ } )
257+
258+ await work ( 10 , oldSessions , async ( key ) => {
259+ const sessionID = key [ key . length - 1 ]
260+ const session = await Storage . read < Session . Info > ( key ) . catch ( ( ) => undefined )
261+ if ( ! session ) return
262+ // Only migrate sessions that match this worktree
263+ if ( session . directory !== worktree ) return
264+
265+ // Idempotency check: skip if already migrated
266+ const existingSession = await Storage . read ( [ "session" , newProjectID , sessionID ] ) . catch ( ( ) => undefined )
267+ if ( existingSession ) {
268+ log . info ( "session already migrated, skipping" , { sessionID } )
269+ return
270+ }
271+
272+ session . projectID = newProjectID
273+ log . info ( "migrating session" , { sessionID, from : oldProjectID , to : newProjectID } )
274+ await Storage . write ( [ "session" , newProjectID , sessionID ] , session )
275+ await Storage . remove ( key )
276+ } ) . catch ( ( error ) => {
277+ log . error ( "failed to migrate worktree sessions" , { error, from : oldProjectID , to : newProjectID } )
278+ } )
279+ }
280+
281+ // Task 2.3: Clean up empty legacy project entry after migration
282+ async function cleanupLegacyProject ( oldProjectID : string ) {
283+ const remainingSessions = await Storage . list ( [ "session" , oldProjectID ] ) . catch ( ( ) => [ ] )
284+ if ( remainingSessions . length === 0 ) {
285+ log . info ( "removing empty legacy project entry" , { projectID : oldProjectID } )
286+ await Storage . remove ( [ "project" , oldProjectID ] ) . catch ( ( ) => { } )
287+ }
288+ }
289+
185290 export async function setInitialized ( projectID : string ) {
186291 await Storage . update < Info > ( [ "project" , projectID ] , ( draft ) => {
187292 draft . time . initialized = Date . now ( )
0 commit comments