( log: LogOption, targetSessionId?: SessionId, )
| 162 | * not the temporary session ID from before resume. |
| 163 | */ |
| 164 | export async function copyPlanForResume( |
| 165 | log: LogOption, |
| 166 | targetSessionId?: SessionId, |
| 167 | ): Promise<boolean> { |
| 168 | const slug = getSlugFromLog(log) |
| 169 | if (!slug) { |
| 170 | return false |
| 171 | } |
| 172 | |
| 173 | // Set the slug for the target session ID (or current if not provided) |
| 174 | const sessionId = targetSessionId ?? getSessionId() |
| 175 | setPlanSlug(sessionId, slug) |
| 176 | |
| 177 | // Attempt to read the plan file directly — recovery triggers on ENOENT. |
| 178 | const planPath = join(getPlansDirectory(), `${slug}.md`) |
| 179 | try { |
| 180 | await getFsImplementation().readFile(planPath, { encoding: 'utf-8' }) |
| 181 | return true |
| 182 | } catch (e: unknown) { |
| 183 | if (!isENOENT(e)) { |
| 184 | // Don't throw — called fire-and-forget (void copyPlanForResume(...)) with no .catch() |
| 185 | logError(e) |
| 186 | return false |
| 187 | } |
| 188 | // Only attempt recovery in remote sessions (CCR) where files don't persist |
| 189 | if (getEnvironmentKind() === null) { |
| 190 | return false |
| 191 | } |
| 192 | |
| 193 | logForDebugging( |
| 194 | `Plan file missing during resume: ${planPath}. Attempting recovery.`, |
| 195 | ) |
| 196 | |
| 197 | // Try file snapshot first (written incrementally during session) |
| 198 | const snapshotPlan = findFileSnapshotEntry(log.messages, 'plan') |
| 199 | let recovered: string | null = null |
| 200 | if (snapshotPlan && snapshotPlan.content.length > 0) { |
| 201 | recovered = snapshotPlan.content |
| 202 | logForDebugging( |
| 203 | `Plan recovered from file snapshot, ${recovered.length} chars`, |
| 204 | { level: 'info' }, |
| 205 | ) |
| 206 | } else { |
| 207 | // Fall back to searching message history |
| 208 | recovered = recoverPlanFromMessages(log) |
| 209 | if (recovered) { |
| 210 | logForDebugging( |
| 211 | `Plan recovered from message history, ${recovered.length} chars`, |
| 212 | { level: 'info' }, |
| 213 | ) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (recovered) { |
| 218 | try { |
| 219 | await writeFile(planPath, recovered, { encoding: 'utf-8' }) |
| 220 | return true |
| 221 | } catch (writeError) { |
no test coverage detected