Skip to content

Commit 4798310

Browse files
committed
fix(core): preserve global canonical on session moves
1 parent a19285d commit 4798310

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎packages/core/src/project.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ const layer = Layer.effect(
114114
.pipe(Effect.orDie)
115115
yield* upsertProject(db, project).pipe(Effect.orDie)
116116
// Clones share a project ID; only replace a canonical directory that is gone.
117+
// Global is a catch-all, not the identity of any particular checkout.
117118
if (
119+
project.id !== ID.global &&
118120
previous &&
119121
previous.canonical !== project.canonical &&
120122
!(yield* fs.exists(previous.canonical).pipe(Effect.orElseSucceed(() => true)))

‎packages/core/test/session-move.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { FileSystem } from "@opencode/core/filesystem"
22
import { describe, expect } from "bun:test"
3+
import { $ } from "bun"
4+
import { eq } from "drizzle-orm"
35
import path from "path"
46
import { chmod, mkdir, readdir, rm } from "fs/promises"
57
import { Cause, Context, Deferred, Duration, Effect, Exit, Fiber, Layer, LayerMap, Queue } from "effect"
@@ -14,6 +16,7 @@ import { Location } from "@opencode/core/location"
1416
import { LocationServiceMap } from "@opencode/core/location-service-map"
1517
import type { LocationServices } from "@opencode/core/location-services"
1618
import { Project } from "@opencode/core/project"
19+
import { ProjectTable } from "@opencode/core/project/sql"
1720
import { AbsolutePath } from "@opencode/core/schema"
1821
import { Session } from "@opencode/core/session"
1922
import { SessionEvent } from "@opencode/core/session/event"
@@ -87,7 +90,7 @@ const itWithUnavailableDestination = testEffect(
8790
),
8891
)
8992
const itWithExecution = testEffect(
90-
AppNodeBuilder.build(LayerNode.group([Session.node, SessionExecution.node]), [
93+
AppNodeBuilder.build(LayerNode.group([Session.node, SessionExecution.node, Database.node]), [
9194
Global.node.replace(tempGlobalLayer),
9295
offlineModels,
9396
]),
@@ -144,6 +147,50 @@ const sourceProbe = (options: { execution?: boolean } = {}) =>
144147
})
145148

146149
describe("Session.move", () => {
150+
itWithExecution.live(
151+
"does not repoint global when moving from a missing empty repository",
152+
() =>
153+
Effect.gen(function* () {
154+
const tmp = yield* tmpdirScoped()
155+
const source = AbsolutePath.make(path.join(tmp.path, "source"))
156+
const destination = AbsolutePath.make(path.join(tmp.path, "destination"))
157+
yield* Effect.promise(async () => {
158+
await Promise.all([mkdir(source), mkdir(destination)])
159+
await $`git init -q`.cwd(source)
160+
await $`git init -q`.cwd(destination)
161+
})
162+
const sessions = yield* Session.Service
163+
const execution = yield* SessionExecution.Service
164+
const db = (yield* Database.Service).db
165+
const created = yield* sessions.create({ location: Location.Ref.make({ directory: source }) })
166+
const remaining = yield* sessions.create({ location: Location.Ref.make({ directory: source }) })
167+
// Match the reported existing database, where global already points at the source.
168+
yield* db.update(ProjectTable).set({ worktree: source }).where(eq(ProjectTable.id, Project.ID.global)).run()
169+
const before = yield* db.select().from(ProjectTable).where(eq(ProjectTable.id, Project.ID.global)).get()
170+
const other = yield* sessions.create({ location: Location.Ref.make({ directory: destination }) })
171+
172+
yield* Effect.promise(() => rm(source, { recursive: true }))
173+
174+
yield* sessions.move({ sessionID: created.id, directory: destination })
175+
yield* execution.awaitIdle(created.id)
176+
177+
expect(yield* sessions.get(created.id)).toMatchObject({
178+
projectID: Project.ID.global,
179+
location: { directory: destination },
180+
})
181+
expect((yield* sessions.list({ directory: source })).data.map((item) => item.id)).toEqual([remaining.id])
182+
expect((yield* sessions.list({ directory: destination })).data.map((item) => item.id)).toEqual(
183+
expect.arrayContaining([created.id, other.id]),
184+
)
185+
expect(
186+
(yield* db.select().from(ProjectTable).where(eq(ProjectTable.id, Project.ID.global)).get())?.worktree,
187+
).toBe(before?.worktree)
188+
yield* sessions.remove(remaining.id)
189+
expect((yield* sessions.get(created.id)).location.directory).toBe(destination)
190+
}),
191+
{ timeout: 120_000 },
192+
)
193+
147194
itWithInstance.live("moves through the bound service without depending on the Session facade", () =>
148195
Effect.gen(function* () {
149196
const tmp = yield* tmpdirScoped()

0 commit comments

Comments
 (0)