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

@@ -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) {