( worktreeSession: PersistedWorktreeSession | null | undefined, )
| 330 | * it back to disk. |
| 331 | */ |
| 332 | export function restoreWorktreeForResume( |
| 333 | worktreeSession: PersistedWorktreeSession | null | undefined, |
| 334 | ): void { |
| 335 | const fresh = getCurrentWorktreeSession() |
| 336 | if (fresh) { |
| 337 | saveWorktreeState(fresh) |
| 338 | return |
| 339 | } |
| 340 | if (!worktreeSession) return |
| 341 | |
| 342 | try { |
| 343 | process.chdir(worktreeSession.worktreePath) |
| 344 | } catch { |
| 345 | // Directory is gone. Override the stale cache so the next |
| 346 | // reAppendSessionMetadata records "exited" instead of re-persisting |
| 347 | // a path that no longer exists. |
| 348 | saveWorktreeState(null) |
| 349 | return |
| 350 | } |
| 351 | |
| 352 | setCwd(worktreeSession.worktreePath) |
| 353 | setOriginalCwd(getCwd()) |
| 354 | // projectRoot is intentionally NOT set here. The transcript doesn't record |
| 355 | // whether the worktree was entered via --worktree (which sets projectRoot) |
| 356 | // or EnterWorktreeTool (which doesn't). Leaving projectRoot stable matches |
| 357 | // EnterWorktreeTool's behavior — skills/history stay anchored to the |
| 358 | // original project. |
| 359 | restoreWorktreeSession(worktreeSession) |
| 360 | // The /resume slash command calls this mid-session after caches have been |
| 361 | // populated against the old cwd. Cheap no-ops for the CLI-flag path |
| 362 | // (caches aren't populated yet there). |
| 363 | clearMemoryFileCaches() |
| 364 | clearSystemPromptSections() |
| 365 | getPlansDirectory.cache.clear?.() |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Undo restoreWorktreeForResume before a mid-session /resume switches to |
no test coverage detected