Skip to content

Commit 33aefd1

Browse files
committed
refactor(core): simplify V1 relative directory migration
1 parent 79136e8 commit 33aefd1

3 files changed

Lines changed: 23 additions & 42 deletions

File tree

‎packages/core/src/database/path.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function storagePath(input: string) {
88
return input.replaceAll("\\", "/")
99
}
1010

11-
function isWindowsStoragePath(input: string) {
11+
export function isWindowsStoragePath(input: string) {
1212
return /^[A-Za-z]:\//.test(input) || input.startsWith("//")
1313
}
1414

‎packages/core/src/database/v1-migration.bun.ts‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as V1Migration from "./v1-migration.js"
22

33
import { Cause, Effect, Layer, Option, Schema, Semaphore } from "effect"
44
import { Database } from "./database.js"
5+
import { isWindowsStoragePath } from "./path.js"
56
import { SessionMessageTable, SessionTable } from "../session/sql.js"
67
import { SessionV1 } from "@opencode/schema/session-v1"
78
import { SessionMessage } from "../session/message.js"
@@ -612,12 +613,12 @@ export function run(options: Options = {}): Effect.Effect<RunResult, never, Data
612613
sessionID: nextID.id,
613614
projectID: nextID.project_id,
614615
})
615-
// Legacy ACP sessions can store a relative cwd. Anchor it to persisted project data,
616-
// never the migration process's cwd, and leave the original V1 row intact.
617-
const directory = migrateDirectory(
618-
nextID.directory,
619-
projects.get(projectID) ?? path.parse(global.data).root,
620-
)
616+
// Legacy ACP sessions can store a cwd relative to an unknown process directory.
617+
// Use the project worktree instead, and leave the original V1 row intact.
618+
const directory =
619+
!nextID.directory || path.posix.isAbsolute(nextID.directory) || isWindowsStoragePath(nextID.directory)
620+
? nextID.directory
621+
: (projects.get(projectID) ?? path.parse(global.data).root).replaceAll("\\", "/")
621622
if (directory !== nextID.directory)
622623
yield* Effect.logWarning("Normalized V1 session directory", {
623624
sessionID: nextID.id,
@@ -716,18 +717,6 @@ export function run(options: Options = {}): Effect.Effect<RunResult, never, Data
716717
)
717718
}
718719

719-
function migrateDirectory(directory: string, worktree: string) {
720-
if (!directory || path.posix.isAbsolute(directory)) return directory
721-
if (/^[A-Za-z]:[\\/]/.test(directory) || directory.startsWith("\\\\")) return directory.replaceAll("\\", "/")
722-
if (!/^[A-Za-z]:[\\/]/.test(worktree) && !worktree.startsWith("//") && !worktree.startsWith("\\\\"))
723-
return path.posix.resolve(worktree, directory)
724-
725-
// A different drive has no persisted cwd. Use its root rather than Node's process-local drive cwd.
726-
const drive = /^[A-Za-z]:/.exec(directory)?.[0]
727-
const base = drive && !worktree.toLowerCase().startsWith(drive.toLowerCase()) ? `${drive}/` : worktree
728-
return path.win32.resolve(base, directory).replaceAll("\\", "/")
729-
}
730-
731720
function nextPath(options: Options, data: string) {
732721
if (options.nextDatabasePath) return options.nextDatabasePath
733722
if (process.env.OPENCODE_DB === ":memory:") return undefined

‎packages/core/test/v1-migration.test.ts‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,21 +1121,17 @@ describe("V1Migration database workflow", () => {
11211121
})
11221122

11231123
test.each([
1124-
{ worktree: "/repo", directory: "\\child", expected: "/repo/\\child" },
1124+
{ worktree: "/repo", directory: ".", expected: "/repo" },
1125+
{ worktree: "/repo", directory: "src", expected: "/repo" },
11251126
{ worktree: "C:/repo", directory: ".", expected: "C:/repo" },
1126-
{ worktree: "C:/repo", directory: "src\\client", expected: "C:/repo/src/client" },
1127-
{ worktree: "C:/repo", directory: "\\child", expected: "C:/child" },
1128-
{ worktree: "C:/repo", directory: "C:child", expected: "C:/repo/child" },
1129-
{ worktree: "D:/repo", directory: "C:child", expected: "C:/child" },
1130-
{ worktree: "//server/share/repo", directory: "\\child", expected: "//server/share/child" },
1131-
{ worktree: "/repo", directory: "C:\\other", expected: "C:/other" },
1132-
{ worktree: "/repo", directory: "\\\\server\\share\\other", expected: "//server/share/other" },
1127+
{ worktree: "//server/share/repo", directory: ".", expected: "//server/share/repo" },
1128+
{ worktree: "/repo", directory: "C:/other", expected: "C:/other" },
1129+
{ worktree: "/repo", directory: "//server/share/other", expected: "//server/share/other" },
11331130
{ worktree: "/repo", directory: "", expected: "" },
11341131
])("migrates $directory in $worktree", async (input) => {
11351132
await database(
11361133
Effect.gen(function* () {
1137-
const database = yield* Database.Service
1138-
const db = database.db
1134+
const { db } = yield* Database.Service
11391135
yield* db.run(sql`
11401136
INSERT INTO project (id, worktree, time_created, time_updated, sandboxes)
11411137
VALUES ('saved-project', ${input.worktree}, 1, 2, '[]')
@@ -1149,6 +1145,9 @@ describe("V1Migration database workflow", () => {
11491145
expect(yield* db.get(sql`SELECT directory FROM session_v2 WHERE id = 'ses_test'`)).toEqual({
11501146
directory: input.expected,
11511147
})
1148+
expect(yield* db.get(sql`SELECT directory FROM session WHERE id = 'ses_test'`)).toEqual({
1149+
directory: input.directory,
1150+
})
11521151
}),
11531152
)
11541153
})
@@ -1179,7 +1178,7 @@ describe("V1Migration database workflow", () => {
11791178
)
11801179
})
11811180

1182-
test("migrates relative directories and preserves V1 history", async () => {
1181+
test("replaces projections containing apostrophes and checkpoints completion", async () => {
11831182
await database(
11841183
Effect.gen(function* () {
11851184
const { db } = yield* Database.Service
@@ -1191,7 +1190,7 @@ describe("V1Migration database workflow", () => {
11911190
tokens_cache_read, tokens_cache_write, revert, agent, model, metadata, time_created, time_updated,
11921191
time_compacting, time_archived
11931192
) VALUES (
1194-
'ses_test', 'global', 'test', '.', 'Test', '1', 99, 99, 99, 99, 99, 99, '{}', 'preserved',
1193+
'ses_test', 'global', 'test', '/tmp/test', 'Test', '1', 99, 99, 99, 99, 99, 99, '{}', 'preserved',
11951194
'{"id":"selected","providerID":"selected-provider","variant":"selected-variant"}', '{"keep":true}',
11961195
1, 2, 3, 4
11971196
)`)
@@ -1229,10 +1228,9 @@ describe("V1Migration database workflow", () => {
12291228
expect(yield* db.all(sql`SELECT id FROM event`)).toEqual([])
12301229
expect(
12311230
yield* db.get(
1232-
sql`SELECT directory, agent, model, metadata, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, revert, time_created, time_updated, time_compacting, time_archived FROM session_v2 WHERE id = 'ses_test'`,
1231+
sql`SELECT agent, model, metadata, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, revert, time_created, time_updated, time_compacting, time_archived FROM session_v2 WHERE id = 'ses_test'`,
12331232
),
12341233
).toEqual({
1235-
directory: "/tmp/test",
12361234
agent: "preserved",
12371235
model: '{"id":"selected","providerID":"selected-provider","variant":"selected-variant"}',
12381236
metadata: '{"keep":true}',
@@ -1248,10 +1246,7 @@ describe("V1Migration database workflow", () => {
12481246
time_compacting: null,
12491247
time_archived: 4,
12501248
})
1251-
expect(
1252-
yield* db.get(sql`SELECT directory, cost, revert, time_compacting FROM session WHERE id = 'ses_test'`),
1253-
).toEqual({
1254-
directory: ".",
1249+
expect(yield* db.get(sql`SELECT cost, revert, time_compacting FROM session WHERE id = 'ses_test'`)).toEqual({
12551250
cost: 99,
12561251
revert: "{}",
12571252
time_compacting: 3,
@@ -1272,7 +1267,7 @@ describe("V1Migration database workflow", () => {
12721267
)
12731268
yield* Effect.forEach(["ses_a", "ses_b", "ses_c"], (id) =>
12741269
db.run(
1275-
sql`INSERT INTO session (id, project_id, slug, directory, title, version, cost, time_created, time_updated) VALUES (${id}, 'global', ${id}, ${id === "ses_b" ? "." : "/tmp/test"}, 'Test', '1', 99, 1, 2)`,
1270+
sql`INSERT INTO session (id, project_id, slug, directory, title, version, cost, time_created, time_updated) VALUES (${id}, 'global', ${id}, '/tmp/test', 'Test', '1', 99, 1, 2)`,
12761271
),
12771272
)
12781273
yield* db.run(
@@ -1328,10 +1323,7 @@ describe("V1Migration database workflow", () => {
13281323
Effect.filterOrFail((status) => status.status === "completed"),
13291324
Effect.retry(Schedule.spaced("10 millis")),
13301325
)
1331-
expect(yield* db.get(sql`SELECT directory, cost FROM session_v2 WHERE id = 'ses_b'`)).toEqual({
1332-
directory: "/tmp/test",
1333-
cost: 0,
1334-
})
1326+
expect(yield* db.get(sql`SELECT cost FROM session_v2 WHERE id = 'ses_b'`)).toEqual({ cost: 0 })
13351327
expect(yield* db.all(sql`SELECT id FROM session_message WHERE session_id = 'ses_b'`)).toEqual([])
13361328
expect(yield* db.get(sql`SELECT seq, owner_id FROM event_sequence WHERE aggregate_id = 'ses_b'`)).toEqual({
13371329
seq: -1,

0 commit comments

Comments
 (0)