fix(server): recreate a thread's worktree before starting a turn (#7839)

This commit is contained in:
MacKinley Smith authored and GitHub committed 2026-08-24 03:48:33 -07:00
1 parent 17822fab70
commit 01fc7d228d
5 files changed
+124

No files matched your search

@@ -84,6 +84,9 @@ export class GitWorkflowService extends Context.Service<
readonly removeWorktree: (
input: VcsRemoveWorktreeInput,
) => Effect.Effect<void, GitCommandError>;
readonly pruneWorktrees: (input: {
readonly cwd: string;
}) => Effect.Effect<void, GitCommandError>;
readonly createRef: (
input: VcsCreateRefInput,
) => Effect.Effect<VcsCreateRefResult, GitCommandError>;
@@ -319,6 +322,10 @@ export const make = Effect.gen(function* () {
ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe(
Effect.andThen(git.removeWorktree(input)),
),
pruneWorktrees: (input) =>
ensureGitCommand("GitWorkflowService.pruneWorktrees", input.cwd).pipe(
Effect.andThen(git.pruneWorktrees(input)),
),
createRef: (input) =>
ensureGitCommand("GitWorkflowService.createRef", input.cwd).pipe(
Effect.andThen(git.createRef(input)),
@@ -270,6 +270,11 @@ describe("ProviderCommandReactor", () => {
: "renamed-branch",
}),
);
const pruneWorktrees = vi.fn((_: { readonly cwd: string }) => Effect.void);
const createWorktree = vi.fn(
(input: { readonly refName: string; readonly path: string | null }) =>
Effect.succeed({ worktree: { path: input.path ?? "", refName: input.refName } }),
);
const refreshStatus = vi.fn((_: string) =>
Effect.succeed({
isRepo: true,
@@ -402,6 +407,8 @@ describe("ProviderCommandReactor", () => {
Layer.provideMerge(
Layer.mock(GitWorkflowService.GitWorkflowService)({
renameBranch,
pruneWorktrees,
createWorktree,
} satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>),
),
Layer.provideMerge(
@@ -506,6 +513,8 @@ describe("ProviderCommandReactor", () => {
respondToUserInput,
stopSession,
renameBranch,
pruneWorktrees,
createWorktree,
refreshStatus,
generateBranchName,
generateThreadTitle,
@@ -1517,6 +1526,50 @@ describe("ProviderCommandReactor", () => {
expect(harness.refreshStatus.mock.calls[0]?.[0]).toBe("/tmp/provider-project-worktree");
});
it("recreates a missing worktree from the thread branch before starting a turn", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
const worktreePath = NodePath.join(harness.stateDir, "missing-worktree");
await harness.runEffect(
harness.engine.dispatch({
type: "thread.meta.update",
commandId: CommandId.make("cmd-thread-missing-worktree"),
threadId: ThreadId.make("thread-1"),
branch: "feature/restore",
worktreePath,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make("cmd-turn-start-missing-worktree"),
threadId: ThreadId.make("thread-1"),
message: {
messageId: asMessageId("user-message-missing-worktree"),
role: "user",
text: "continue",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt: now,
}),
);
await waitFor(() => harness.startSession.mock.calls.length === 1);
expect(harness.pruneWorktrees).toHaveBeenCalledWith({ cwd: "/tmp/provider-project" });
expect(harness.createWorktree).toHaveBeenCalledWith({
cwd: "/tmp/provider-project",
refName: "feature/restore",
path: worktreePath,
});
expect(harness.createWorktree.mock.invocationCallOrder[0]).toBeLessThan(
harness.startSession.mock.invocationCallOrder[0]!,
);
});
it("forwards codex model options through session start and turn send", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
@@ -19,6 +19,7 @@ import * as Crypto from "effect/Crypto";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Equal from "effect/Equal";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
@@ -305,6 +306,7 @@ const make = Effect.gen(function* () {
const providerService = yield* ProviderService;
const providerRegistry = yield* ProviderRegistry;
const gitWorkflow = yield* GitWorkflowService;
const fileSystem = yield* FileSystem.FileSystem;
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
const textGeneration = yield* TextGeneration;
const serverSettingsService = yield* ServerSettingsService;
@@ -428,6 +430,52 @@ const make = Effect.gen(function* () {
.pipe(Effect.map(Option.getOrUndefined));
});
/**
* Recreates a thread's worktree from its branch when the directory has
* disappeared. Provider sessions resume into the persisted cwd, so a missing
* worktree makes every later turn fail as a bogus "session not found".
* Best-effort: on failure the turn proceeds and reports the real error.
*/
const ensureThreadWorktree = Effect.fnUntraced(function* (thread: {
readonly id: ThreadId;
readonly projectId: ProjectId;
readonly branch: string | null;
readonly worktreePath: string | null;
}) {
const { worktreePath, branch } = thread;
if (!worktreePath || !branch) {
return;
}
const exists = yield* fileSystem.exists(worktreePath).pipe(Effect.orElseSucceed(() => true));
if (exists) {
return;
}
const project = yield* resolveProject(thread.projectId);
if (!project) {
return;
}
const cwd = project.workspaceRoot;
yield* Effect.logWarning("provider command reactor recreating missing worktree", {
threadId: thread.id,
worktreePath,
branch,
});
// A directory deleted without `git worktree remove` leaves an admin entry
// that makes `git worktree add` refuse the path; prune clears it.
yield* gitWorkflow.pruneWorktrees({ cwd }).pipe(
Effect.andThen(gitWorkflow.createWorktree({ cwd, refName: branch, path: worktreePath })),
Effect.catchCause((cause) =>
Cause.hasInterruptsOnly(cause)
? Effect.failCause(cause)
: Effect.logWarning("provider command reactor failed to recreate worktree", {
threadId: thread.id,
worktreePath,
cause: Cause.pretty(cause),
}),
),
);
});
const resolveThread = Effect.fnUntraced(function* (threadId: ThreadId) {
return yield* projectionSnapshotQuery
.getThreadDetailById(threadId)
@@ -1083,6 +1131,8 @@ const make = Effect.gen(function* () {
return;
}
yield* ensureThreadWorktree(thread);
const isFirstUserMessageTurn =
thread.messages.filter((entry) => entry.role === "user").length === 1;
if (isFirstUserMessageTurn) {
+4
View File
@@ -310,6 +310,10 @@ export class GitVcsDriver extends Context.Service<
readonly removeWorktree: (
input: VcsRemoveWorktreeInput,
) => Effect.Effect<void, GitCommandError>;
/** Drops worktree admin entries whose directory is already gone (`git worktree prune`). */
readonly pruneWorktrees: (input: {
readonly cwd: string;
}) => Effect.Effect<void, GitCommandError>;
readonly renameBranch: (
input: GitRenameBranchInput,
) => Effect.Effect<GitRenameBranchResult, GitCommandError>;
+10
View File
@@ -3017,6 +3017,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
});
});
const pruneWorktrees: GitVcsDriver.GitVcsDriver["Service"]["pruneWorktrees"] = Effect.fn(
"pruneWorktrees",
)(function* (input) {
yield* executeGit("GitVcsDriver.pruneWorktrees", input.cwd, ["worktree", "prune"], {
timeoutMs: 15_000,
fallbackErrorDetail: "git worktree prune failed",
});
});
const renameBranch: GitVcsDriver.GitVcsDriver["Service"]["renameBranch"] = Effect.fn(
"renameBranch",
)(function* (input) {
@@ -3222,6 +3231,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
withListRefsInvalidation(input.cwd, fetchRemoteTrackingBranch(input)),
setBranchUpstream: (input) => withListRefsInvalidation(input.cwd, setBranchUpstream(input)),
removeWorktree: (input) => withListRefsInvalidation(input.cwd, removeWorktree(input)),
pruneWorktrees: (input) => withListRefsInvalidation(input.cwd, pruneWorktrees(input)),
renameBranch: (input) => withListRefsInvalidation(input.cwd, renameBranch(input)),
createRef: (input) => withListRefsInvalidation(input.cwd, createRef(input)),
switchRef: (input) => withListRefsInvalidation(input.cwd, switchRef(input)),