Skip to content

Commit d53e0d0

Browse files
t3dotggclaude
andcommitted
fix(server): worktree threads in a repo with no commits run in the project folder
`git worktree add` cannot start from an unborn branch, so a new worktree thread in a freshly initialized repository failed with "git worktree add failed". When neither HEAD nor the base ref has a commit and no origin fetch can supply one, prepare the thread in the project folder instead. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent eac52f0 commit d53e0d0

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ interface HarnessOptions {
9999
readonly managedFolders?: Layer.Layer<ManagedProjectFolders.ManagedProjectFolders>;
100100
readonly createWorktree?: GitWorkflow.GitWorkflowService["Service"]["createWorktree"];
101101
readonly fetchRemote?: GitWorkflow.GitWorkflowService["Service"]["fetchRemote"];
102+
readonly hasCommit?: GitWorkflow.GitWorkflowService["Service"]["hasCommit"];
102103
readonly renameBranch?: GitWorkflow.GitWorkflowService["Service"]["renameBranch"];
103104
readonly runSetup?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]["runForThread"];
104105
readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"];
@@ -167,6 +168,7 @@ function makeHarness(options: HarnessOptions = {}) {
167168
createWorktree,
168169
renameBranch,
169170
fetchRemote: options.fetchRemote ?? (() => Effect.void),
171+
hasCommit: options.hasCommit ?? (() => Effect.succeed(true)),
170172
remoteExists: () => Effect.succeed(true),
171173
remoteBranchExists: () => Effect.succeed(true),
172174
removeWorktree,
@@ -1110,6 +1112,53 @@ it.effect("names the worktree itself when the client provides no branch", () =>
11101112
}),
11111113
);
11121114

1115+
it.effect("runs in the project folder when the repository has no commits", () =>
1116+
Effect.gen(function* () {
1117+
const harness = makeHarness({ hasCommit: () => Effect.succeed(false) });
1118+
yield* Effect.gen(function* () {
1119+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1120+
const threads = yield* ThreadManagement.ThreadManagementService;
1121+
const launched = yield* launches.launch(
1122+
launchInput({
1123+
command: "command:launch:empty-repo",
1124+
thread: "thread:launch:empty-repo",
1125+
message: "Build the feature",
1126+
workspace: { type: "worktree", baseRef: "main" },
1127+
}),
1128+
);
1129+
yield* waitUntil(() =>
1130+
threads
1131+
.getThreadProjection(launched.threadId)
1132+
.pipe(Effect.map((projection) => projection.runs[0]?.status === "starting")),
1133+
);
1134+
const { thread } = yield* threads.getThreadProjection(launched.threadId);
1135+
assert.isNull(thread.worktreePath);
1136+
assert.isNull(thread.branch);
1137+
assert.equal(harness.createWorktree.mock.calls.length, 0);
1138+
assert.equal(harness.runSetup.mock.calls[0]?.[0]?.worktreePath, project.workspaceRoot);
1139+
}).pipe(Effect.provide(harness.layer));
1140+
}),
1141+
);
1142+
1143+
it.effect("still checks out from origin when only the local repository has no commits", () =>
1144+
Effect.gen(function* () {
1145+
const harness = makeHarness({ hasCommit: () => Effect.succeed(false) });
1146+
yield* Effect.gen(function* () {
1147+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1148+
yield* launches.launch(
1149+
launchInput({
1150+
command: "command:launch:empty-repo-origin",
1151+
thread: "thread:launch:empty-repo-origin",
1152+
message: "Build the feature",
1153+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1154+
}),
1155+
);
1156+
yield* waitUntil(() => Effect.sync(() => harness.createWorktree.mock.calls.length === 1));
1157+
assert.equal(harness.createWorktree.mock.calls[0]?.[0]?.refName, "remote-main-sha");
1158+
}).pipe(Effect.provide(harness.layer));
1159+
}),
1160+
);
1161+
11131162
it.effect("renames a temporary t3code/<hash> branch off the provisioning critical path", () =>
11141163
Effect.gen(function* () {
11151164
const branchNameStarted = yield* Deferred.make<void>();

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,45 @@ const make = Effect.gen(function* () {
224224
}
225225
});
226226

227+
// `git worktree add` needs a commit to start from. True when neither HEAD
228+
// nor the base ref has one and no origin fetch could bring the base ref.
229+
const hasNothingToCheckOut = Effect.fn("ThreadLaunchService.hasNothingToCheckOut")(function* (
230+
cwd: string,
231+
strategy: Extract<ThreadLaunchWorkspaceStrategy, { readonly type: "worktree" }>,
232+
) {
233+
if (yield* git.hasCommit({ cwd, refName: "HEAD" })) return false;
234+
if (yield* git.hasCommit({ cwd, refName: strategy.baseRef })) return false;
235+
return !(
236+
strategy.startFromOrigin === true && (yield* git.remoteExists({ cwd, remoteName: "origin" }))
237+
);
238+
});
239+
227240
const prepareInBackground = Effect.fn("ThreadLaunchService.prepareInBackground")(function* (
228-
input: PreparationInput,
241+
requested: PreparationInput,
229242
threadId: ThreadId,
230243
runId: RunId | null,
231244
) {
232-
const project = yield* projects.getById(input.projectId).pipe(
233-
Effect.mapError(mapError(input, "resolve-project", threadId)),
245+
const project = yield* projects.getById(requested.projectId).pipe(
246+
Effect.mapError(mapError(requested, "resolve-project", threadId)),
234247
Effect.flatMap(
235248
Option.match({
236249
onNone: () =>
237-
Effect.fail(mapError(input, "resolve-project", threadId)("Project no longer exists.")),
250+
Effect.fail(
251+
mapError(requested, "resolve-project", threadId)("Project no longer exists."),
252+
),
238253
onSome: Effect.succeed,
239254
}),
240255
),
241256
);
257+
// A repository with no commits yet runs the thread in its project folder,
258+
// as a non-Git project does: a worktree of it would be empty.
259+
const input: PreparationInput =
260+
requested.workspaceStrategy.type === "worktree" &&
261+
(yield* hasNothingToCheckOut(project.workspaceRoot, requested.workspaceStrategy).pipe(
262+
Effect.mapError(mapError(requested, "provision-worktree", threadId)),
263+
))
264+
? { ...requested, workspaceStrategy: { type: "root" } }
265+
: requested;
242266

243267
const reused = input.reusedWorktree;
244268
const tracked = input.workspaceStrategy.type === "worktree" || reused !== undefined;

0 commit comments

Comments
 (0)