Skip to content

Commit b94e110

Browse files
fix(opencode): sessions lost after git init in existing project (anomalyco#16814)
Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
1 parent f0bba10 commit b94e110

File tree

3 files changed

+160
-17
lines changed

3 files changed

+160
-17
lines changed

packages/opencode/src/project/project.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,18 @@ export namespace Project {
218218
})
219219

220220
const row = Database.use((db) => db.select().from(ProjectTable).where(eq(ProjectTable.id, data.id)).get())
221-
const existing = await iife(async () => {
222-
if (row) return fromRow(row)
223-
const fresh: Info = {
224-
id: data.id,
225-
worktree: data.worktree,
226-
vcs: data.vcs as Info["vcs"],
227-
sandboxes: [],
228-
time: {
229-
created: Date.now(),
230-
updated: Date.now(),
231-
},
232-
}
233-
if (data.id !== ProjectID.global) {
234-
await migrateFromGlobal(data.id, data.worktree)
235-
}
236-
return fresh
237-
})
221+
const existing = row
222+
? fromRow(row)
223+
: {
224+
id: data.id,
225+
worktree: data.worktree,
226+
vcs: data.vcs as Info["vcs"],
227+
sandboxes: [] as string[],
228+
time: {
229+
created: Date.now(),
230+
updated: Date.now(),
231+
},
232+
}
238233

239234
if (Flag.OPENCODE_EXPERIMENTAL_ICON_DISCOVERY) discover(existing)
240235

@@ -277,6 +272,12 @@ export namespace Project {
277272
Database.use((db) =>
278273
db.insert(ProjectTable).values(insert).onConflictDoUpdate({ target: ProjectTable.id, set: updateSet }).run(),
279274
)
275+
// Runs after upsert so the target project row exists (FK constraint).
276+
// Runs on every startup because sessions created before git init
277+
// accumulate under "global" and need migrating whenever they appear.
278+
if (data.id !== ProjectID.global) {
279+
await migrateFromGlobal(data.id, data.worktree)
280+
}
280281
GlobalBus.emit("event", {
281282
payload: {
282283
type: Event.Updated.type,

packages/opencode/test/fixture/fixture.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export async function tmpdir<T>(options?: TmpDirOptions<T>) {
4242
if (options?.git) {
4343
await $`git init`.cwd(dirpath).quiet()
4444
await $`git config core.fsmonitor false`.cwd(dirpath).quiet()
45+
await $`git config user.email "test@opencode.test"`.cwd(dirpath).quiet()
46+
await $`git config user.name "Test"`.cwd(dirpath).quiet()
4547
await $`git commit --allow-empty -m "root commit ${dirpath}"`.cwd(dirpath).quiet()
4648
}
4749
if (options?.config) {
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { Project } from "../../src/project/project"
3+
import { Database, eq } from "../../src/storage/db"
4+
import { SessionTable } from "../../src/session/session.sql"
5+
import { ProjectTable } from "../../src/project/project.sql"
6+
import { ProjectID } from "../../src/project/schema"
7+
import { SessionID } from "../../src/session/schema"
8+
import { Log } from "../../src/util/log"
9+
import { $ } from "bun"
10+
import { tmpdir } from "../fixture/fixture"
11+
12+
Log.init({ print: false })
13+
14+
function uid() {
15+
return SessionID.make(crypto.randomUUID())
16+
}
17+
18+
function seed(opts: { id: SessionID; dir: string; project: ProjectID }) {
19+
const now = Date.now()
20+
Database.use((db) =>
21+
db
22+
.insert(SessionTable)
23+
.values({
24+
id: opts.id,
25+
project_id: opts.project,
26+
slug: opts.id,
27+
directory: opts.dir,
28+
title: "test",
29+
version: "0.0.0-test",
30+
time_created: now,
31+
time_updated: now,
32+
})
33+
.run(),
34+
)
35+
}
36+
37+
function ensureGlobal() {
38+
Database.use((db) =>
39+
db
40+
.insert(ProjectTable)
41+
.values({
42+
id: ProjectID.global,
43+
worktree: "/",
44+
time_created: Date.now(),
45+
time_updated: Date.now(),
46+
sandboxes: [],
47+
})
48+
.onConflictDoNothing()
49+
.run(),
50+
)
51+
}
52+
53+
describe("migrateFromGlobal", () => {
54+
test("migrates global sessions on first project creation", async () => {
55+
// 1. Start with git init but no commits — creates "global" project row
56+
await using tmp = await tmpdir()
57+
await $`git init`.cwd(tmp.path).quiet()
58+
await $`git config user.name "Test"`.cwd(tmp.path).quiet()
59+
await $`git config user.email "test@opencode.test"`.cwd(tmp.path).quiet()
60+
const { project: pre } = await Project.fromDirectory(tmp.path)
61+
expect(pre.id).toBe(ProjectID.global)
62+
63+
// 2. Seed a session under "global" with matching directory
64+
const id = uid()
65+
seed({ id, dir: tmp.path, project: ProjectID.global })
66+
67+
// 3. Make a commit so the project gets a real ID
68+
await $`git commit --allow-empty -m "root"`.cwd(tmp.path).quiet()
69+
70+
const { project: real } = await Project.fromDirectory(tmp.path)
71+
expect(real.id).not.toBe(ProjectID.global)
72+
73+
// 4. The session should have been migrated to the real project ID
74+
const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
75+
expect(row).toBeDefined()
76+
expect(row!.project_id).toBe(real.id)
77+
})
78+
79+
test("migrates global sessions even when project row already exists", async () => {
80+
// 1. Create a repo with a commit — real project ID created immediately
81+
await using tmp = await tmpdir({ git: true })
82+
const { project } = await Project.fromDirectory(tmp.path)
83+
expect(project.id).not.toBe(ProjectID.global)
84+
85+
// 2. Ensure "global" project row exists (as it would from a prior no-git session)
86+
ensureGlobal()
87+
88+
// 3. Seed a session under "global" with matching directory.
89+
// This simulates a session created before git init that wasn't
90+
// present when the real project row was first created.
91+
const id = uid()
92+
seed({ id, dir: tmp.path, project: ProjectID.global })
93+
94+
// 4. Call fromDirectory again — project row already exists,
95+
// so the current code skips migration entirely. This is the bug.
96+
await Project.fromDirectory(tmp.path)
97+
98+
const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
99+
expect(row).toBeDefined()
100+
expect(row!.project_id).toBe(project.id)
101+
})
102+
103+
test("migrates sessions with empty directory", async () => {
104+
await using tmp = await tmpdir({ git: true })
105+
const { project } = await Project.fromDirectory(tmp.path)
106+
expect(project.id).not.toBe(ProjectID.global)
107+
108+
ensureGlobal()
109+
110+
// Legacy sessions may lack a directory value
111+
const id = uid()
112+
seed({ id, dir: "", project: ProjectID.global })
113+
114+
await Project.fromDirectory(tmp.path)
115+
116+
const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
117+
expect(row).toBeDefined()
118+
// Empty directory means "no known origin" — should be claimed
119+
expect(row!.project_id).toBe(project.id)
120+
})
121+
122+
test("does not steal sessions from unrelated directories", async () => {
123+
await using tmp = await tmpdir({ git: true })
124+
const { project } = await Project.fromDirectory(tmp.path)
125+
expect(project.id).not.toBe(ProjectID.global)
126+
127+
ensureGlobal()
128+
129+
// Seed a session under "global" but for a DIFFERENT directory
130+
const id = uid()
131+
seed({ id, dir: "/some/other/dir", project: ProjectID.global })
132+
133+
await Project.fromDirectory(tmp.path)
134+
135+
const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
136+
expect(row).toBeDefined()
137+
// Should remain under "global" — not stolen
138+
expect(row!.project_id).toBe(ProjectID.global)
139+
})
140+
})

0 commit comments

Comments
 (0)