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