(log: LogOption)
| 922 | * Copy file history snapshots for a given log option. |
| 923 | */ |
| 924 | export async function copyFileHistoryForResume(log: LogOption): Promise<void> { |
| 925 | if (!fileHistoryEnabled()) { |
| 926 | return |
| 927 | } |
| 928 | |
| 929 | const fileHistorySnapshots = log.fileHistorySnapshots |
| 930 | if (!fileHistorySnapshots || log.messages.length === 0) { |
| 931 | return |
| 932 | } |
| 933 | const lastMessage = log.messages[log.messages.length - 1] |
| 934 | const previousSessionId = lastMessage?.sessionId |
| 935 | if (!previousSessionId) { |
| 936 | logError( |
| 937 | new Error( |
| 938 | `FileHistory: Failed to copy backups on restore (no previous session id)`, |
| 939 | ), |
| 940 | ) |
| 941 | return |
| 942 | } |
| 943 | |
| 944 | const sessionId = getSessionId() |
| 945 | if (previousSessionId === sessionId) { |
| 946 | logForDebugging( |
| 947 | `FileHistory: No need to copy file history for resuming with same session id: ${sessionId}`, |
| 948 | ) |
| 949 | return |
| 950 | } |
| 951 | |
| 952 | try { |
| 953 | // All backups share the same directory: {configDir}/file-history/{sessionId}/ |
| 954 | // Create it once upfront instead of once per backup file |
| 955 | const newBackupDir = join( |
| 956 | getClaudeConfigHomeDir(), |
| 957 | 'file-history', |
| 958 | sessionId, |
| 959 | ) |
| 960 | await mkdir(newBackupDir, { recursive: true }) |
| 961 | |
| 962 | // Migrate all backup files from the previous session to current session. |
| 963 | // Process all snapshots in parallel; within each snapshot, links also run in parallel. |
| 964 | let failedSnapshots = 0 |
| 965 | await Promise.allSettled( |
| 966 | fileHistorySnapshots.map(async snapshot => { |
| 967 | const backupEntries = Object.values(snapshot.trackedFileBackups).filter( |
| 968 | (backup): backup is typeof backup & { backupFileName: string } => |
| 969 | backup.backupFileName !== null, |
| 970 | ) |
| 971 | |
| 972 | const results = await Promise.allSettled( |
| 973 | backupEntries.map(async ({ backupFileName }) => { |
| 974 | const oldBackupPath = resolveBackupPath( |
| 975 | backupFileName, |
| 976 | previousSessionId, |
| 977 | ) |
| 978 | const newBackupPath = join(newBackupDir, backupFileName) |
| 979 | |
| 980 | try { |
| 981 | await link(oldBackupPath, newBackupPath) |
no test coverage detected