@@ -7,6 +7,15 @@ import path from "path"
77import { tmpdirScoped } from "../fixture/fixture"
88import { GlobalBus } from "../../src/bus/global"
99import { ProjectID } from "../../src/project/schema"
10+ import { Database } from "@/storage/db"
11+ import { ProjectTable } from "@/project/project.sql"
12+ import { SessionTable } from "@/session/session.sql"
13+ import { PermissionTable } from "@/session/session.sql"
14+ import { WorkspaceTable } from "@/control-plane/workspace.sql"
15+ import { eq } from "drizzle-orm"
16+ import { Hash } from "@opencode-ai/core/util/hash"
17+ import { SessionID } from "@/session/schema"
18+ import { WorkspaceID } from "@/control-plane/schema"
1019import { Cause , Effect , Exit , Layer , Stream } from "effect"
1120import { ChildProcess , ChildProcessSpawner } from "effect/unstable/process"
1221import { NodePath } from "@effect/platform-node"
@@ -31,6 +40,10 @@ function run<A, E>(fn: (svc: Project.Interface) => Effect.Effect<A, E>) {
3140 } )
3241}
3342
43+ function remoteProjectID ( remote : string ) {
44+ return ProjectID . make ( Hash . fast ( `git-remote:${ remote } ` ) )
45+ }
46+
3447/**
3548 * Creates a mock ChildProcessSpawner layer that intercepts git subcommands
3649 * matching `failArg` and returns exit code 128, while delegating everything
@@ -154,28 +167,91 @@ describe("Project.fromDirectory", () => {
154167 } ) ,
155168 )
156169
157- it . live ( "keeps root commit identity when origin exists " , ( ) =>
170+ it . live ( "prefers normalized origin remote over root commit " , ( ) =>
158171 Effect . gen ( function * ( ) {
159172 const tmp = yield * tmpdirScoped ( { git : true } )
160173 yield * Effect . promise ( ( ) => $ `git remote add origin [email protected] :Test-Org/Test-Repo.git` . cwd ( tmp ) . quiet ( ) ) 161174
162175 const { project } = yield * run ( ( svc ) => svc . fromDirectory ( tmp ) )
163- const root = ( yield * Effect . promise ( ( ) => $ `git rev-list --max-parents=0 HEAD` . cwd ( tmp ) . text ( ) ) ) . trim ( )
164176
165- expect ( project . id ) . toBe ( ProjectID . make ( root ) )
177+ expect ( project . id ) . toBe ( remoteProjectID ( "github.com/Test-Org/Test-Repo" ) )
178+ } ) ,
179+ )
180+
181+ it . live ( "normalizes equivalent origin URL forms to the same project ID" , ( ) =>
182+ Effect . gen ( function * ( ) {
183+ const ssh = yield * tmpdirScoped ( { git : true } )
184+ const https = yield * tmpdirScoped ( { git : true } )
185+ yield * Effect . promise ( ( ) => $ `git remote add origin [email protected] :owner/repo.git` . cwd ( ssh ) . quiet ( ) ) 186+ yield * Effect . promise ( ( ) => $ `git remote add origin https://github.com/owner/repo.git` . cwd ( https ) . quiet ( ) )
187+
188+ const { project : a } = yield * run ( ( svc ) => svc . fromDirectory ( ssh ) )
189+ const { project : b } = yield * run ( ( svc ) => svc . fromDirectory ( https ) )
190+
191+ expect ( a . id ) . toBe ( remoteProjectID ( "github.com/owner/repo" ) )
192+ expect ( b . id ) . toBe ( a . id )
166193 } ) ,
167194 )
168195
169- it . live ( "keeps cached project identity when origin becomes available" , ( ) =>
196+ it . live ( "migrates cached root project data when origin becomes available" , ( ) =>
170197 Effect . gen ( function * ( ) {
171198 const tmp = yield * tmpdirScoped ( { git : true } )
172199 const projects = yield * Project . Service
173200 const { project : rootProject } = yield * projects . fromDirectory ( tmp )
201+ const remoteID = remoteProjectID ( "github.com/acme/app" )
202+ const sessionID = crypto . randomUUID ( ) as SessionID
203+ const workspaceID = WorkspaceID . ascending ( )
204+
205+ yield * Effect . sync ( ( ) => {
206+ Database . use ( ( db ) => {
207+ db . insert ( SessionTable )
208+ . values ( {
209+ id : sessionID ,
210+ project_id : rootProject . id ,
211+ slug : sessionID ,
212+ directory : tmp ,
213+ title : "test" ,
214+ version : "0.0.0-test" ,
215+ time_created : Date . now ( ) ,
216+ time_updated : Date . now ( ) ,
217+ } )
218+ . run ( )
219+ db . insert ( PermissionTable )
220+ . values ( {
221+ project_id : rootProject . id ,
222+ data : [ { permission : "edit" , pattern : "*" , action : "allow" } ] ,
223+ time_created : Date . now ( ) ,
224+ time_updated : Date . now ( ) ,
225+ } )
226+ . run ( )
227+ db . insert ( WorkspaceTable )
228+ . values ( {
229+ id : workspaceID ,
230+ type : "local" ,
231+ name : "test" ,
232+ project_id : rootProject . id ,
233+ } )
234+ . run ( )
235+ } )
236+ } )
174237 yield * Effect . promise ( ( ) => $ `git remote add origin [email protected] :acme/app.git` . cwd ( tmp ) . quiet ( ) ) 175238
176239 const { project } = yield * projects . fromDirectory ( tmp )
177240
178- expect ( project . id ) . toBe ( rootProject . id )
241+ expect ( project . id ) . toBe ( remoteID )
242+ expect (
243+ Database . use ( ( db ) => db . select ( ) . from ( ProjectTable ) . where ( eq ( ProjectTable . id , rootProject . id ) ) . get ( ) ) ,
244+ ) . toBeUndefined ( )
245+ expect (
246+ Database . use ( ( db ) => db . select ( ) . from ( SessionTable ) . where ( eq ( SessionTable . id , sessionID ) ) . get ( ) ) ?. project_id ,
247+ ) . toBe ( remoteID )
248+ expect (
249+ Database . use ( ( db ) => db . select ( ) . from ( PermissionTable ) . where ( eq ( PermissionTable . project_id , remoteID ) ) . get ( ) ) ,
250+ ) . toBeDefined ( )
251+ expect (
252+ Database . use ( ( db ) => db . select ( ) . from ( WorkspaceTable ) . where ( eq ( WorkspaceTable . id , workspaceID ) ) . get ( ) )
253+ ?. project_id ,
254+ ) . toBe ( remoteID )
179255 } ) ,
180256 )
181257} )
0 commit comments