Skip to content

Commit 01b9f18

Browse files
author
opencloud-x
committed
fix: migrate sessions when project ID changes after git init
When git init runs in a directory that already has opencode sessions, the project ID changes from the global project to the git root commit hash. Sessions linked to the old project ID become invisible in the TUI. This adds a migration step in fromDirectory() that detects stale project entries with the same worktree but different IDs, moves their sessions to the current project, and removes the stale entries. Fixes anomalyco#21230
1 parent 40e4cd2 commit 01b9f18

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

‎packages/opencode/src/project/project.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,21 @@ export namespace Project {
318318
.where(and(eq(SessionTable.project_id, ProjectID.global), eq(SessionTable.directory, data.worktree)))
319319
.run(),
320320
)
321+
322+
// Migrate sessions from stale project entries with the same worktree
323+
// This handles the case where project ID changes (e.g. after `git init`)
324+
yield* db((d) => {
325+
const stales = d
326+
.select({ id: ProjectTable.id })
327+
.from(ProjectTable)
328+
.where(eq(ProjectTable.worktree, data.worktree))
329+
.all()
330+
.filter((row) => row.id !== data.id)
331+
for (const stale of stales) {
332+
d.update(SessionTable).set({ project_id: data.id }).where(eq(SessionTable.project_id, stale.id)).run()
333+
d.delete(ProjectTable).where(eq(ProjectTable.id, stale.id)).run()
334+
}
335+
})
321336
}
322337

323338
yield* emitUpdated(result)

0 commit comments

Comments
 (0)