fix(server): recover threads whose worktree was removed #6197
Closedmackinleysmith(GitHub profile, opens in a new tab)wants to merge 11 commits into
main fromrefs/pull/6197/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
- apps/server/src/provider/Layers/ProviderService.ts line 431OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -402,0 +411,21 @@411+ // worktree surfaces as a generic "session not found" and the thread looks412+ // unrecoverable even though the conversation is intact. Detect it here so413+ // callers get an error they can actually repair.414+ if (persistedCwd !== undefined) {415+ // Only block the resume when the directory is positively known to be416+ // gone; a failed stat must not strand an otherwise healthy thread.417+ const workspaceExists = yield* fileSystem418+ .exists(persistedCwd)419+ .pipe(Effect.orElseSucceed(() => true));420+ if (!workspaceExists) {421+ yield* Effect.logWarning("provider.session.workspace-missing", {422+ threadId: input.binding.threadId,423+ provider: input.binding.provider,424+ cwd: persistedCwd,425+ });426+ return yield* new ProviderSessionWorkspaceMissingError({427+ threadId: input.binding.threadId,428+ cwd: persistedCwd,429+ });430+ }431+ }
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/Layers/ProviderService.ts line 667Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -595,3 +644,13 @@595644 : "none",596645 "provider.cwd.effective": effectiveCwd ?? "",597646 });647+ // Only when resuming: a fresh session with a missing cwd is a648+ // different problem, and failing here would break thread creation if649+ // the workspace is still being provisioned.650+ if (effectiveResumeCursor !== undefined) {651+ yield* assertResumeWorkspaceExists({652+ threadId,653+ cwd: effectiveCwd,654+ provider: resolvedProvider,655+ });656+ }
- apps/server/src/provider/Layers/ProviderService.ts line 384OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -357,0 +364,21 @@364+ /**365+ * Resuming into a directory that no longer exists does not fail cleanly:366+ * provider CLIs key their transcript store off the cwd, so a removed worktree367+ * surfaces as a generic "session not found" and the thread looks368+ * unrecoverable even though the conversation is intact on both sides.369+ *370+ * Guards every path that hands a resume cursor to an adapter — both371+ * `startSession` (which turn start reaches via the reactor's ensure-session372+ * step) and `recoverSessionForThread`.373+ */374+ const assertResumeWorkspaceExists = Effect.fnUntraced(function* (input: {375+ readonly threadId: ThreadId;376+ readonly cwd: string | undefined;377+ readonly provider?: string;378+ }) {379+ if (input.cwd === undefined) {380+ return;381+ }382+ // Only block the resume when the directory is positively known to be gone;383+ // a failed stat must not strand an otherwise healthy thread.384+ const workspaceExists = yield* fileSystem
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts lines 445 to 445OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -380,0 +425,21 @@425+ */426+ const ensureThreadWorkspace = Effect.fnUntraced(function* (input: {427+ readonly thread: {428+ readonly threadId: ThreadId;429+ readonly projectId: ProjectId;430+ readonly branch: string | null;431+ readonly worktreePath: string | null;432+ };433+ readonly createdAt: string;434+ }) {435+ const worktreePath = input.thread.worktreePath;436+ if (!worktreePath) {437+ return;438+ }439+ const exists = yield* fileSystem.exists(worktreePath).pipe(Effect.orElseSucceed(() => true));440+ if (exists) {441+ return;442+ }443+444+ const branch = input.thread.branch;445+ const project = yield* resolveProject(input.thread.projectId);@@ -2980,3 +2980,11 @@29802980 });29812981 });298229822983+ const pruneWorktrees: GitVcsDriver.GitVcsDriver["Service"]["pruneWorktrees"] = Effect.fn(2984+ "pruneWorktrees",2985+ )(function* (input) {2986+ // Only drops administrative entries whose working directory is already2987+ // gone, so this is safe to run before re-adding a worktree at a path a2988+ // stale entry still claims. Without it `git worktree add` refuses the2989+ // path outright.2990+ yield* executeGit("GitVcsDriver.pruneWorktrees", input.cwd, ["worktree", "prune"], {
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 519Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -380,0 +493,21 @@493+ cause: Cause.pretty(cause),494+ }).pipe(Effect.as(false)),495+ ),496+ );497+498+ yield* appendWorkspaceActivity({499+ threadId: input.thread.threadId,500+ tone: restored ? "info" : "error",501+ summary: restored ? "Worktree restored" : "Worktree could not be restored",502+ detail: restored503+ ? `The worktree for this thread was missing and has been recreated at ${worktreePath} from branch '${branch}'. Any uncommitted changes it held were not recoverable.`504+ : `The worktree for this thread is missing at ${worktreePath} and could not be recreated from branch '${branch}'.`,505+ createdAt: input.createdAt,506+ }).pipe(507+ Effect.catchCause((cause) =>508+ Effect.logWarning("provider.workspace.restore.activity-failed", {509+ threadId: input.thread.threadId,510+ cause: Cause.pretty(cause),511+ }),512+ ),513+ );
- @@ -368,0 +480,21 @@480+ workspaceRoot,481+ cause: Cause.pretty(cause),482+ }),483+ ),484+ );485+486+ const restored = yield* gitWorkflow487+ .createWorktree({ cwd: workspaceRoot, refName: branch, path: worktreePath })488+ .pipe(489+ Effect.as(true),490+ Effect.catchCause((cause) =>491+ Cause.hasInterruptsOnly(cause)492+ ? Effect.failCause(cause)493+ : Effect.logWarning("provider.workspace.restore.failed", {494+ threadId: input.thread.threadId,495+ worktreePath,496+ branch,497+ cause: Cause.pretty(cause),498+ }).pipe(Effect.as(false)),499+ ),500+ );
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
90ba6d09-4063-4ce8-8038-fabb89f33183You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share