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