Skip to content

Commit 0f0a4be

Browse files
fix(project): restore remote project identity (anomalyco#29495)
1 parent 0e88dd9 commit 0f0a4be

3 files changed

Lines changed: 116 additions & 12 deletions

File tree

‎packages/core/src/project.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const layer = Layer.effect(
108108
if (!repo) return { id: ID.global, directory: input, vcs: undefined }
109109

110110
const previous = yield* cached(repo.store)
111-
const id = previous ?? (yield* root(repo))
111+
const id = (yield* remote(repo)) ?? previous ?? (yield* root(repo))
112112

113113
return {
114114
previous,

‎packages/core/test/project.test.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import path from "path"
55
import { Effect } from "effect"
66
import { Project } from "@opencode-ai/core/project"
77
import { AbsolutePath } from "@opencode-ai/core/schema"
8+
import { Hash } from "@opencode-ai/core/util/hash"
89
import { tmpdir } from "./fixture/tmpdir"
910
import { testEffect } from "./lib/effect"
1011

1112
const it = testEffect(Project.defaultLayer)
1213

14+
function remoteID(remote: string) {
15+
return Project.ID.make(Hash.fast(`git-remote:${remote}`))
16+
}
17+
1318
function abs(value: string) {
1419
return AbsolutePath.make(value)
1520
}
@@ -86,7 +91,7 @@ describe("ProjectV2.resolve", () => {
8691
}),
8792
)
8893

89-
it.live("uses root commit when origin exists", () =>
94+
it.live("prefers normalized origin over root commit", () =>
9095
Effect.gen(function* () {
9196
const tmp = yield* Effect.acquireRelease(
9297
Effect.promise(() => tmpdir()),
@@ -97,13 +102,36 @@ describe("ProjectV2.resolve", () => {
97102

98103
const result = yield* project.resolve(abs(tmp.path))
99104

100-
expect(result.id).toBe(Project.ID.make(yield* Effect.promise(() => rootCommit(tmp.path))))
105+
expect(result.id).toBe(remoteID("github.com/Acme/App"))
106+
expect(result.id).not.toBe(Project.ID.make(yield* Effect.promise(() => rootCommit(tmp.path))))
101107
expect(result.directory).toBe(yield* real(tmp.path))
102108
expect(result.vcs?.type).toBe("git")
103109
}),
104110
)
105111

106-
it.live("uses root commit when local remote exists", () =>
112+
it.live("normalizes ssh and https remotes to the same id", () =>
113+
Effect.gen(function* () {
114+
const ssh = yield* Effect.acquireRelease(
115+
Effect.promise(() => tmpdir()),
116+
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
117+
)
118+
const https = yield* Effect.acquireRelease(
119+
Effect.promise(() => tmpdir()),
120+
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
121+
)
122+
yield* Effect.promise(() => initRepo(ssh.path, { commit: true, remote: "[email protected]:owner/repo.git" }))
123+
yield* Effect.promise(() => initRepo(https.path, { commit: true, remote: "https://github.com/owner/repo.git" }))
124+
const project = yield* Project.Service
125+
126+
const a = yield* project.resolve(abs(ssh.path))
127+
const b = yield* project.resolve(abs(https.path))
128+
129+
expect(a.id).toBe(remoteID("github.com/owner/repo"))
130+
expect(b.id).toBe(a.id)
131+
}),
132+
)
133+
134+
it.live("ignores file remotes and falls back to root commit", () =>
107135
Effect.gen(function* () {
108136
const tmp = yield* Effect.acquireRelease(
109137
Effect.promise(() => tmpdir()),
@@ -118,7 +146,7 @@ describe("ProjectV2.resolve", () => {
118146
}),
119147
)
120148

121-
it.live("prefers previous cached id over origin", () =>
149+
it.live("returns previous cached id from common dir", () =>
122150
Effect.gen(function* () {
123151
const tmp = yield* Effect.acquireRelease(
124152
Effect.promise(() => tmpdir()),
@@ -131,7 +159,7 @@ describe("ProjectV2.resolve", () => {
131159
const result = yield* project.resolve(abs(tmp.path))
132160

133161
expect(result.previous).toBe(Project.ID.make("old-id"))
134-
expect(result.id).toBe(Project.ID.make("old-id"))
162+
expect(result.id).toBe(remoteID("github.com/owner/repo"))
135163
}),
136164
)
137165

@@ -185,7 +213,7 @@ describe("ProjectV2.resolve", () => {
185213

186214
expect(result.directory).toBe(yield* real(worktree))
187215
expect(result.previous).toBe(Project.ID.make("old-id"))
188-
expect(result.id).toBe(Project.ID.make("old-id"))
216+
expect(result.id).toBe(remoteID("github.com/owner/repo"))
189217
expect(result.vcs?.type).toBe("git")
190218
}),
191219
)

‎packages/opencode/test/project/project.test.ts‎

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import path from "path"
77
import { tmpdirScoped } from "../fixture/fixture"
88
import { GlobalBus } from "../../src/bus/global"
99
import { 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"
1019
import { Cause, Effect, Exit, Layer, Stream } from "effect"
1120
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
1221
import { 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

Comments
 (0)