copyPlanForResume method

Future<bool> copyPlanForResume({
  1. required LogOption log,
  2. required String targetSessionId,
  3. bool isRemoteSession = false,
})

Restore plan slug from a resumed session.

Sets the slug in the session cache so getPlanSlug returns it. If the plan file is missing, attempts to recover it from a file snapshot or from message history. Returns true if a plan file exists (or was recovered) for the slug.

Implementation

Future<bool> copyPlanForResume({
  required LogOption log,
  required String targetSessionId,
  bool isRemoteSession = false,
}) async {
  final slug = _getSlugFromLog(log);
  if (slug == null) return false;

  setPlanSlug(targetSessionId, slug);

  final planPath = '$plansDirectory/$slug.md';
  try {
    await File(planPath).readAsString();
    return true;
  } on FileSystemException {
    if (!isRemoteSession) return false;

    // Try file snapshot first
    final snapshotPlan = _findFileSnapshotEntry(log.messages, 'plan');
    String? recovered;
    if (snapshotPlan != null && snapshotPlan.content.isNotEmpty) {
      recovered = snapshotPlan.content;
    } else {
      recovered = _recoverPlanFromMessages(log);
    }

    if (recovered != null) {
      try {
        await File(planPath).writeAsString(recovered);
        return true;
      } catch (_) {
        return false;
      }
    }
    return false;
  }
}