( turnStartTime: TurnStartTime, outputsDir: string, )
| 71 | * @param outputsDir - The directory to scan for modified files |
| 72 | */ |
| 73 | export async function findModifiedFiles( |
| 74 | turnStartTime: TurnStartTime, |
| 75 | outputsDir: string, |
| 76 | ): Promise<string[]> { |
| 77 | // Use recursive flag to get all entries in one call |
| 78 | let entries: Awaited<ReturnType<typeof fs.readdir>> |
| 79 | try { |
| 80 | entries = await fs.readdir(outputsDir, { |
| 81 | withFileTypes: true, |
| 82 | recursive: true, |
| 83 | }) |
| 84 | } catch { |
| 85 | // Directory doesn't exist or is not accessible |
| 86 | return [] |
| 87 | } |
| 88 | |
| 89 | // Filter to regular files only (skip symlinks for security) and build full paths |
| 90 | const filePaths: string[] = [] |
| 91 | for (const entry of entries) { |
| 92 | if (entry.isSymbolicLink()) { |
| 93 | continue |
| 94 | } |
| 95 | if (entry.isFile()) { |
| 96 | // entry.parentPath is available in Node 20+, fallback to entry.path for older versions |
| 97 | const parentPath = getEntryParentPath(entry, outputsDir) |
| 98 | filePaths.push(path.join(parentPath, entry.name)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (filePaths.length === 0) { |
| 103 | logDebug('No files found in outputs directory') |
| 104 | return [] |
| 105 | } |
| 106 | |
| 107 | // Parallelize stat calls for all files |
| 108 | const statResults = await Promise.all( |
| 109 | filePaths.map(async filePath => { |
| 110 | try { |
| 111 | const stat = await fs.lstat(filePath) |
| 112 | // Skip if it became a symlink between readdir and stat (race condition) |
| 113 | if (stat.isSymbolicLink()) { |
| 114 | return null |
| 115 | } |
| 116 | return { filePath, mtimeMs: stat.mtimeMs } |
| 117 | } catch { |
| 118 | // File may have been deleted between readdir and stat |
| 119 | return null |
| 120 | } |
| 121 | }), |
| 122 | ) |
| 123 | |
| 124 | // Filter to files modified since turn start |
| 125 | const modifiedFiles: string[] = [] |
| 126 | for (const result of statResults) { |
| 127 | if (result && result.mtimeMs >= turnStartTime) { |
| 128 | modifiedFiles.push(result.filePath) |
| 129 | } |
| 130 | } |
no test coverage detected