Skip to content

Commit 430d5e9

Browse files
committed
fix(server): preserve worktree base commit by SHA
1 parent 9c80c8c commit 430d5e9

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,12 +1055,35 @@ it.effect(
10551055
);
10561056

10571057
it.effect.each([
1058-
{ name: "the base is fetched from origin", startFromOrigin: true, hasAnyRef: false },
1059-
{ name: "another ref has a commit", startFromOrigin: false, hasAnyRef: true },
1058+
{
1059+
name: "the base is fetched from origin",
1060+
startFromOrigin: true,
1061+
hasAnyRef: false,
1062+
baseRef: "main",
1063+
requestedBaseHasCommit: false,
1064+
},
1065+
{
1066+
name: "another ref has a commit",
1067+
startFromOrigin: false,
1068+
hasAnyRef: true,
1069+
baseRef: "main",
1070+
requestedBaseHasCommit: false,
1071+
},
1072+
{
1073+
name: "the requested base is a commit by SHA",
1074+
startFromOrigin: false,
1075+
hasAnyRef: false,
1076+
baseRef: "requested-base-sha",
1077+
requestedBaseHasCommit: true,
1078+
},
10601079
])("keeps the worktree when HEAD has no commit but $name", (testCase) =>
10611080
Effect.gen(function* () {
10621081
const harness = makeHarness({
1063-
hasCommit: ({ refName }) => Effect.succeed(refName === "remote-main-sha"),
1082+
hasCommit: ({ refName }) =>
1083+
Effect.succeed(
1084+
refName === "remote-main-sha" ||
1085+
(testCase.requestedBaseHasCommit === true && refName === testCase.baseRef),
1086+
),
10641087
git: { hasAnyRef: () => Effect.succeed(testCase.hasAnyRef) },
10651088
});
10661089
yield* Effect.gen(function* () {
@@ -1070,7 +1093,11 @@ it.effect.each([
10701093
command: `command:launch:empty-repo-${testCase.startFromOrigin}`,
10711094
thread: `thread:launch:empty-repo-${testCase.startFromOrigin}`,
10721095
message: "Build the feature",
1073-
workspace: { type: "worktree", baseRef: "main", startFromOrigin: testCase.startFromOrigin },
1096+
workspace: {
1097+
type: "worktree",
1098+
baseRef: testCase.baseRef,
1099+
startFromOrigin: testCase.startFromOrigin,
1100+
},
10741101
});
10751102
const launched = yield* launches.launch(input);
10761103
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
@@ -1084,7 +1111,7 @@ it.effect.each([
10841111
assert.equal(harness.createWorktree.mock.calls.length, 1);
10851112
assert.equal(
10861113
harness.createWorktree.mock.calls[0]?.[0]?.refName,
1087-
testCase.startFromOrigin ? "remote-main-sha" : "main",
1114+
testCase.startFromOrigin ? "remote-main-sha" : testCase.baseRef,
10881115
);
10891116
}).pipe(Effect.provide(harness.layer));
10901117
}),

‎apps/server/src/orchestration-v2/ThreadLaunchService.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,12 @@ const make = Effect.gen(function* () {
291291
const project = Option.getOrUndefined(yield* projects.getById(projectId));
292292
if (project === undefined) return false;
293293
const cwd = project.workspaceRoot;
294-
// Any ref means a commit exists, so a bad base ref still reports its error.
295-
if ((yield* git.hasCommit({ cwd, refName: "HEAD" })) || (yield* git.hasAnyRef(cwd)))
294+
// A base SHA can be checkoutable even when the repository has no refs.
295+
if (
296+
(yield* git.hasCommit({ cwd, refName: "HEAD" })) ||
297+
(yield* git.hasCommit({ cwd, refName: strategy.baseRef })) ||
298+
(yield* git.hasAnyRef(cwd))
299+
)
296300
return false;
297301
// Provisioning fetches the base first, which may bring the first commit.
298302
return !(

0 commit comments

Comments
 (0)