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