@@ -15,7 +15,7 @@ import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
1515import { LayerNode } from "@opencode/util/effect/layer-node"
1616import { FSUtil } from "@opencode/util/fs-util"
1717import { Global } from "@opencode/util/global"
18- import { Deferred , Effect , Fiber , Layer , Logger , Schedule , Schema , Scope } from "effect"
18+ import { Deferred , Effect , Fiber , Layer , Logger , PlatformError , Schedule , Schema , Scope } from "effect"
1919import { TestClock } from "effect/testing"
2020import { eq , sql } from "drizzle-orm"
2121import type { SqlClient } from "effect/sql/SqlClient"
@@ -25,6 +25,14 @@ import fs from "fs/promises"
2525import path from "path"
2626
2727const makeDb = EffectDrizzleSqlite . makeWithDefaults ( )
28+ const caseInsensitive = await ( async ( ) => {
29+ await using tmp = await tmpdir ( )
30+ await fs . mkdir ( path . join ( tmp . path , "Case" ) )
31+ return fs . realpath ( path . join ( tmp . path , "case" ) ) . then (
32+ ( ) => true ,
33+ ( ) => false ,
34+ )
35+ } ) ( )
2836const run = < A , E > ( effect : Effect . Effect < A , E , SqlClient | Scope . Scope | Global . Service > ) =>
2937 Effect . runPromise (
3038 Effect . scoped (
@@ -1257,14 +1265,21 @@ describe("V1Migration database workflow", () => {
12571265 )
12581266 } )
12591267
1260- test ( "repairs V1 global sessions left by a completed import" , async ( ) => {
1268+ test . each ( [
1269+ "symlink" ,
1270+ // Probe the fixture filesystem rather than assuming an OS implies case folding.
1271+ ...( caseInsensitive ? [ "case" ] : [ ] ) ,
1272+ ] ) ( "repairs completed imports with %s aliases" , async ( alias ) => {
12611273 await using tmp = await tmpdir ( )
12621274 const plain = path . join ( tmp . path , "plain" )
12631275 const repo = path . join ( tmp . path , "repo" )
12641276 const missing = path . join ( tmp . path , "missing" )
12651277 await fs . mkdir ( plain , { recursive : true } )
12661278 await fs . mkdir ( path . join ( repo , "pkg" , "a" ) , { recursive : true } )
12671279 await initRepo ( repo )
1280+ const linked = path . join ( tmp . path , alias === "case" ? "PLAIN" : "plain-link" )
1281+ if ( alias === "symlink" ) await fs . symlink ( plain , linked , process . platform === "win32" ? "junction" : undefined )
1282+ expect ( await fs . realpath ( linked ) ) . toBe ( await fs . realpath ( plain ) )
12681283 await database (
12691284 Effect . gen ( function * ( ) {
12701285 const { db } = yield * Database . Service
@@ -1277,6 +1292,7 @@ describe("V1Migration database workflow", () => {
12771292 )
12781293 const sessions = [
12791294 { id : "ses_plain" , directory : plain , v1 : true } ,
1295+ { id : "ses_linked" , directory : linked , v1 : true } ,
12801296 { id : "ses_child" , directory : plain , v1 : true } ,
12811297 { id : "ses_repo" , directory : path . join ( repo , "pkg" , "a" ) , v1 : true } ,
12821298 { id : "ses_missing" , directory : missing , v1 : true } ,
@@ -1322,6 +1338,9 @@ describe("V1Migration database workflow", () => {
13221338 INSERT INTO workspace (id, provider, created_at, last_used_at) VALUES ('wrk_explicit', 'local', 1, 2)
13231339 ` )
13241340 yield * db . run ( sql `UPDATE session_v2 SET workspace_id = 'wrk_explicit' WHERE id = 'ses_workspace'` )
1341+ // Retain NULL coverage on the child while exercising stale V1 paths on both owner shapes.
1342+ yield * db . run ( sql `UPDATE session_v2 SET path = 'legacy/plain' WHERE id IN ('ses_plain', 'ses_linked')` )
1343+ yield * db . run ( sql `UPDATE session_v2 SET path = 'legacy/repo' WHERE id = 'ses_repo'` )
13251344 const legacy = yield * db . all ( sql `SELECT * FROM session ORDER BY id` )
13261345 const legacyMessages = yield * db . all ( sql `SELECT * FROM message ORDER BY id` )
13271346 const legacyParts = yield * db . all ( sql `SELECT * FROM part ORDER BY id` )
@@ -1337,6 +1356,7 @@ describe("V1Migration database workflow", () => {
13371356 }
13381357 expect ( yield * db . all ( sql `SELECT id, project_id, path, time_updated FROM session_v2 ORDER BY id` ) ) . toEqual ( [
13391358 { id : "ses_child" , project_id : owners . plain . id , path : "" , time_updated : 2 } ,
1359+ { id : "ses_linked" , project_id : owners . plain . id , path : "" , time_updated : 2 } ,
13401360 { id : "ses_missing" , project_id : "global" , path : null , time_updated : 2 } ,
13411361 { id : "ses_native" , project_id : "global" , path : null , time_updated : 2 } ,
13421362 { id : "ses_owned" , project_id : "owned" , path : "kept" , time_updated : 2 } ,
@@ -1353,27 +1373,41 @@ describe("V1Migration database workflow", () => {
13531373 expect ( yield * db . all ( sql `SELECT * FROM message ORDER BY id` ) ) . toEqual ( legacyMessages )
13541374 expect ( yield * db . all ( sql `SELECT * FROM part ORDER BY id` ) ) . toEqual ( legacyParts )
13551375 expect ( yield * db . all ( sql `SELECT * FROM session_message ORDER BY id` ) ) . toEqual ( messages )
1356- expect ( yield * db . get ( sql `SELECT value FROM kv WHERE key = 'migration.v1-v2.global-sessions'` ) ) . toEqual ( {
1357- value : '{"phase":"completed"}' ,
1358- } )
1376+ expect ( yield * db . get ( sql `SELECT value FROM kv WHERE key = 'migration.v1-v2.global-sessions'` ) ) . toBeUndefined ( )
13591377 const repaired = yield * db . all ( sql `SELECT * FROM session_v2 ORDER BY id` )
13601378 expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
13611379 expect ( yield * db . all ( sql `SELECT * FROM session_v2 ORDER BY id` ) ) . toEqual ( repaired )
13621380 expect ( yield * db . all ( sql `SELECT * FROM session_message ORDER BY id` ) ) . toEqual ( messages )
1381+ yield * Effect . promise ( ( ) => fs . mkdir ( missing ) )
1382+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1383+ const owner = yield * projects . resolve ( AbsolutePath . make ( missing ) )
1384+ expect (
1385+ yield * db . get ( sql `SELECT project_id, path, time_updated FROM session_v2 WHERE id = 'ses_missing'` ) ,
1386+ ) . toEqual ( {
1387+ project_id : owner . id ,
1388+ path : "" ,
1389+ time_updated : 2 ,
1390+ } )
1391+ expect ( yield * db . get ( sql `SELECT value FROM kv WHERE key = 'migration.v1-v2.global-sessions'` ) ) . toEqual ( {
1392+ value : '{"phase":"completed"}' ,
1393+ } )
13631394 } ) ,
13641395 )
13651396 } )
13661397
13671398 test . each ( [
1368- { phase : "completed" , timeoutAt : 1 , lookups : 1 , owned : 0 } ,
1369- { phase : "completed" , timeoutAt : 2 , lookups : 2 , owned : 2 } ,
1370- { phase : "fresh" , timeoutAt : 0 , lookups : 3 , owned : 6 } ,
1371- { phase : "fresh" , timeoutAt : 1 , lookups : 1 , owned : 0 } ,
1372- { phase : "fresh" , timeoutAt : 2 , lookups : 2 , owned : 2 } ,
1373- { phase : "resumed" , timeoutAt : 0 , lookups : 2 , owned : 4 } ,
1374- { phase : "resumed" , timeoutAt : 1 , lookups : 1 , owned : 0 } ,
1375- { phase : "resumed" , timeoutAt : 2 , lookups : 2 , owned : 2 } ,
1376- ] ) ( "retries unfinished V1 global repairs after $phase import (timeout $timeoutAt)" , async ( input ) => {
1399+ { phase : "completed" , timeoutAt : 1 , lookups : 1 , owned : 0 , failure : "none" } ,
1400+ { phase : "completed" , timeoutAt : 2 , lookups : 2 , owned : 2 , failure : "none" } ,
1401+ { phase : "fresh" , timeoutAt : 0 , lookups : 3 , owned : 6 , failure : "none" } ,
1402+ { phase : "fresh" , timeoutAt : 1 , lookups : 1 , owned : 0 , failure : "none" } ,
1403+ { phase : "fresh" , timeoutAt : 2 , lookups : 2 , owned : 2 , failure : "none" } ,
1404+ { phase : "resumed" , timeoutAt : 0 , lookups : 2 , owned : 4 , failure : "none" } ,
1405+ { phase : "resumed" , timeoutAt : 1 , lookups : 1 , owned : 0 , failure : "none" } ,
1406+ { phase : "resumed" , timeoutAt : 2 , lookups : 2 , owned : 2 , failure : "none" } ,
1407+ { phase : "completed" , timeoutAt : 0 , lookups : 2 , owned : 4 , failure : "realpath" } ,
1408+ { phase : "completed" , timeoutAt : 0 , lookups : 2 , owned : 4 , failure : "isDir" } ,
1409+ { phase : "completed" , timeoutAt : 0 , lookups : 3 , owned : 4 , failure : "project" } ,
1410+ ] ) ( "retries V1 repair: $phase, timeout $timeoutAt, failure $failure" , async ( input ) => {
13771411 await using tmp = await tmpdir ( )
13781412 const directories = [ "a" , "b" , "c" ] . map ( ( name ) => path . join ( tmp . path , name ) )
13791413 await Promise . all ( directories . map ( ( directory ) => fs . mkdir ( directory ) ) )
@@ -1382,6 +1416,7 @@ describe("V1Migration database workflow", () => {
13821416 const database = yield * Database . Service
13831417 const db = database . db
13841418 const projects = yield * Project . Service
1419+ const filesystem = yield * FSUtil . Service
13851420 yield * db . run ( sql `
13861421 INSERT INTO project (id, worktree, time_created, time_updated, sandboxes)
13871422 VALUES ('global', '/', 1, 2, '[]')
@@ -1444,11 +1479,31 @@ describe("V1Migration database workflow", () => {
14441479 const stalled = yield * Deferred . make < void > ( )
14451480 const attempted : string [ ] = [ ]
14461481 const fiber = yield * V1Migration . run ( ) . pipe (
1482+ Effect . provideService ( FSUtil . Service , {
1483+ ...filesystem ,
1484+ realPath : ( directory ) =>
1485+ input . failure === "realpath" && directory === directories [ 1 ]
1486+ ? Effect . fail (
1487+ PlatformError . systemError ( {
1488+ _tag : "PermissionDenied" ,
1489+ module : "FileSystem" ,
1490+ method : "realPath" ,
1491+ pathOrDescriptor : directory ,
1492+ } ) ,
1493+ )
1494+ : filesystem . realPath ( directory ) ,
1495+ isDir : ( directory ) =>
1496+ input . failure === "isDir" && directory === directories [ 1 ]
1497+ ? Effect . succeed ( false )
1498+ : filesystem . isDir ( directory ) ,
1499+ } ) ,
14471500 Effect . provideService ( Project . Service , {
14481501 ...projects ,
14491502 resolve : ( directory , options ) =>
14501503 Effect . gen ( function * ( ) {
14511504 attempted . push ( directory )
1505+ if ( input . failure === "project" && directory === directories [ 1 ] )
1506+ return yield * Effect . die ( new Error ( "Project lookup unavailable" ) )
14521507 if ( attempted . length === input . timeoutAt ) {
14531508 yield * Deferred . succeed ( stalled , undefined )
14541509 return yield * Effect . never
0 commit comments