Skip to content

Commit b051e91

Browse files
committed
fix(core): load strategies before worktree removal
1 parent b8a7f03 commit b051e91

8 files changed

Lines changed: 17 additions & 41 deletions

File tree

‎packages/core/src/worktree.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * as Worktree from "./worktree.js"
22

3-
import { Context, Effect, Layer, Option, Schema } from "effect"
3+
import { Context, Effect, Layer, Schema } from "effect"
44
import { and, asc, desc, eq, isNull, sql } from "drizzle-orm"
55
import path from "path"
66
import { AbsolutePath } from "./schema.js"
@@ -266,19 +266,8 @@ const layer = Layer.effect(
266266
const worktreeDirectory = yield* canonical(fs, input.directory)
267267
const stored = yield* ops.find(worktreeDirectory)
268268
if (!stored?.strategy) return yield* new InvalidDirectoryError({ directory: worktreeDirectory })
269-
// Inspect only an already-loaded canonical registry. Removing must never boot config or plugins.
270-
const strategies =
271-
current?.directory === row.worktree
272-
? current.get().strategies
273-
: yield* locations.contextEffectOption(Location.Ref.make({ directory: row.worktree })).pipe(
274-
Effect.map(
275-
Option.match({
276-
onSome: (context) => Context.get(context, WorktreeStrategies.Service).get().strategies,
277-
onNone: () => new Map([[gitStrategy.id, gitStrategy]]),
278-
}),
279-
),
280-
)
281-
const strategy = yield* getStrategy(StrategyID.make(stored.strategy), strategies)
269+
const settings = yield* load(row.worktree, current)
270+
const strategy = yield* getStrategy(StrategyID.make(stored.strategy), settings.strategies)
282271
yield* strategy
283272
.remove({
284273
directory: worktreeDirectory,

‎packages/protocol/openapi.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12220,7 +12220,7 @@
1222012220
}
1222112221
}
1222212222
},
12223-
"description": "Remove a saved project worktree using its recorded, already-available strategy. Does not load configuration.",
12223+
"description": "Load the project's canonical configuration and remove a saved worktree using its recorded strategy.",
1222412224
"summary": "Remove worktree",
1222512225
"requestBody": {
1222612226
"content": {

‎packages/protocol/src/groups/worktree.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const WorktreeGroup = HttpApiGroup.make("server.worktree")
5656
identifier: "worktree.remove",
5757
summary: "Remove worktree",
5858
description:
59-
"Remove a saved project worktree using its recorded, already-available strategy. Does not load configuration.",
59+
"Load the project's canonical configuration and remove a saved worktree using its recorded strategy.",
6060
}),
6161
),
6262
)

‎packages/server/test/worktree.test.ts‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ it.live("refresh discovers both clones while list alone never discovers external
8787
}),
8888
)
8989

90-
it.live("remove uses bundled Git without loading configuration and enforces project ownership", () =>
90+
it.live("remove loads canonical strategies and enforces project ownership", () =>
9191
Effect.gen(function* () {
9292
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-remove-")))
9393
const directory = path.join(tmp.path, "repo")
@@ -107,13 +107,12 @@ it.live("remove uses bundled Git without loading configuration and enforces proj
107107
yield* Effect.promise(async () => {
108108
const session = await api.session.create({ location: { directory: linked } })
109109
const foreign = await api.session.create({ location: { directory: other } })
110-
const loaded = await api.debug.location.list()
111110
await expect(
112111
api.worktree.remove({ projectID: foreign.projectID, directory: linked, force: true }),
113112
).rejects.toMatchObject({ name: "WorktreeError" })
114113
expect(await fs.stat(linked).then((stat) => stat.isDirectory())).toBe(true)
115114
await api.worktree.remove({ projectID: session.projectID, directory: linked, force: false })
116-
expect(await api.debug.location.list()).toEqual(loaded)
115+
expect(await api.debug.location.list()).toContainEqual({ directory })
117116
expect(await api.worktree.list({ projectID: session.projectID })).toEqual([{ directory }])
118117
})
119118
}),
@@ -212,7 +211,7 @@ it.live(
212211
)
213212

214213
it.live(
215-
"uses canonical plugins for shared clones and remove only uses already-loaded strategies",
214+
"uses canonical plugins for shared clones and recorded strategy removal",
216215
() =>
217216
Effect.gen(function* () {
218217
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-worktree-plugins-")))
@@ -261,21 +260,7 @@ it.live(
261260

262261
await Bun.write(path.join(custom.directory, "dirty.txt"), "keep me")
263262
await api.debug.location.evict({ location: { directory: first } })
264-
const loaded = await api.debug.location.list()
265263
const remove = new URL("/api/worktree", server.base)
266-
const unavailable = await fetch(remove, {
267-
method: "DELETE",
268-
headers: { ...server.headers, "content-type": "application/json" },
269-
body: JSON.stringify({ projectID, directory: custom.directory, force: true }),
270-
})
271-
expect(unavailable.status).toBe(400)
272-
expect(await unavailable.json()).toMatchObject({
273-
data: { message: "Worktree strategy unavailable: test-copy" },
274-
})
275-
expect(await Bun.file(path.join(custom.directory, "dirty.txt")).text()).toBe("keep me")
276-
expect(await api.worktree.list({ projectID })).toEqual(rows)
277-
expect(await api.debug.location.list()).toEqual(loaded)
278-
await api.worktree.refresh({ projectID })
279264
const failure = await fetch(remove, {
280265
method: "DELETE",
281266
headers: { ...server.headers, "content-type": "application/json" },
@@ -284,6 +269,8 @@ it.live(
284269
expect(failure.status).toBe(400)
285270
expect(await failure.json()).toMatchObject({ data: { forceRequired: true } })
286271
expect(await Bun.file(path.join(custom.directory, "dirty.txt")).text()).toBe("keep me")
272+
expect(await api.worktree.list({ projectID })).toEqual(rows)
273+
expect(await api.debug.location.list()).toContainEqual({ directory: first })
287274

288275
await api.worktree.remove({
289276
projectID,

‎services/www/openapi.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12220,7 +12220,7 @@
1222012220
}
1222112221
}
1222212222
},
12223-
"description": "Remove a saved project worktree using its recorded, already-available strategy. Does not load configuration.",
12223+
"description": "Load the project's canonical configuration and remove a saved worktree using its recorded strategy.",
1222412224
"summary": "Remove worktree",
1222512225
"requestBody": {
1222612226
"content": {

‎services/www/public/openapi.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12220,7 +12220,7 @@
1222012220
}
1222112221
}
1222212222
},
12223-
"description": "Remove a saved project worktree using its recorded, already-available strategy. Does not load configuration.",
12223+
"description": "Load the project's canonical configuration and remove a saved worktree using its recorded strategy.",
1222412224
"summary": "Remove worktree",
1222512225
"requestBody": {
1222612226
"content": {

‎services/www/src/docs/content/build/plugins/index.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,9 @@ Rift's native snapshot operation, for example, has no ref-selection option.
10211021
Create and refresh wait for the canonical checkout's plugins to activate. Calls made during that checkout's plugin
10221022
setup see registrations made so far and do not wait for their own activation.
10231023

1024-
Remove uses the worktree's recorded strategy from the already-loaded canonical registry, or bundled Git. It does not
1025-
load configuration or activate plugins. An unavailable owner produces an error, including after a restart before its
1026-
plugin has loaded. The worktree's destination can be elsewhere:
1024+
Remove loads the canonical configuration and activates its plugins, then uses the worktree's recorded strategy rather
1025+
than the currently selected default. An unavailable recorded owner produces an error. The worktree's destination can
1026+
be elsewhere:
10271027

10281028
```ts
10291029
await ctx.worktree.remove({

‎services/www/src/docs/content/build/sdk/index.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ for the session and event groups.
4545
## Worktrees
4646

4747
Worktree operations require a `projectID`. Create and refresh load configuration and plugins from the project's saved
48-
canonical checkout. List reads saved inventory only; remove uses the recorded strategy without loading configuration
49-
and fails if that strategy is unavailable.
48+
canonical checkout. List reads saved inventory only; remove activates canonical plugins and uses the recorded strategy,
49+
failing if that strategy is unavailable.
5050

5151
```ts
5252
const projectID = session.projectID

0 commit comments

Comments
 (0)