cli: dedupe tmp-state cleanup and rewind preview warnings · Entire

cli: dedupe tmp-state cleanup and rewind preview warnings

13b0ab1→main· Soph·2w ago·2 files·+24 added/-37 removed

CleanupPrePromptState/CleanupPreTaskState repeated the open-root/remove dance — extract cleanupTmpStateFile. The interactive and non-interactive rewind flows repeated the files-to-delete preview warning — extract printRewindPreviewWarnings. Covered by the existing state and rewind tests.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

Sessions

7c22eaba7124View transcript

Changes

2

224 unmodified lines

225
226
227
228
229
230
231
232
233
234
235
236
237
238
228
229
230
231
255 unmodified lines

487
488
489
500
501
502
503
504
505
506
507
508
509
510
490
491
492
493
221 unmodified lines

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736

224 unmodified lines

return handleLogsOnlyRewindInteractive(ctx, w, errW, start, *selectedPoint, shortID)
    }

// Preview rewind to show warnings about files that will be deleted
    preview, previewErr := start.PreviewRewind(ctx, *selectedPoint)
    if previewErr != nil {
        fmt.Fprintf(errW, "Warning: could not preview rewind effects: %v\n", previewErr)
    } else if preview != nil && len(preview.FilesToDelete) > 0 {
        fmt.Fprintf(errW, "\nWarning: The following untracked files will be DELETED:\n")
        for _, f := range preview.FilesToDelete {
            fmt.Fprintf(errW, "  - %s\n", f)
        }
        fmt.Fprintf(errW, "\n")
    }
    printRewindPreviewWarnings(ctx, errW, start, *selectedPoint)

// Confirm rewind
    var confirm bool
255 unmodified lines

return handleLogsOnlyRewindNonInteractive(ctx, w, errW, start, *selectedPoint)
    }

// Resolve agent once for use throughout
    agent, err := getAgent(selectedPoint.Agent)
221 unmodified lines

return filepath.Join(cleaned, paths.TranscriptFileNameLegacy)
}

// printRewindPreviewWarnings previews the rewind and warns about untracked
// files it would delete. Preview failures are non-fatal — the rewind itself
// still runs, so the warning degrades to a notice.
func printRewindPreviewWarnings(ctx context.Context, errW io.Writer, start *strategy.ManualCommitStrategy, point strategy.RewindPoint) {
    preview, previewErr := start.PreviewRewind(ctx, point)
    if previewErr != nil {
        fmt.Fprintf(errW, "Warning: could not preview rewind effects: %v\n", previewErr)
    } else if preview != nil && len(preview.FilesToDelete) > 0 {
        fmt.Fprintf(errW, "\nWarning: The following untracked files will be DELETED:\n")
        for _, f := range preview.FilesToDelete {
            fmt.Fprintf(errW, "  - %s\n", f)
        }
        fmt.Fprintf(errW, "\n")
    }
}

func restoreSessionTranscript(ctx context.Context, w io.Writer, transcriptFile, sessionID string, agent agentpkg.Agent) error {
sessionFile, err := resolveTranscriptPath(ctx, sessionID, agent)
if err != nil {