()
| 154 | } |
| 155 | |
| 156 | export async function cleanupOldSessionFiles(): Promise<CleanupResult> { |
| 157 | const cutoffDate = getCutoffDate() |
| 158 | const result: CleanupResult = { messages: 0, errors: 0 } |
| 159 | const projectsDir = getProjectsDir() |
| 160 | const fsImpl = getFsImplementation() |
| 161 | |
| 162 | let projectDirents |
| 163 | try { |
| 164 | projectDirents = await fsImpl.readdir(projectsDir) |
| 165 | } catch { |
| 166 | return result |
| 167 | } |
| 168 | |
| 169 | for (const projectDirent of projectDirents) { |
| 170 | if (!projectDirent.isDirectory()) continue |
| 171 | const projectDir = join(projectsDir, projectDirent.name) |
| 172 | |
| 173 | // Single readdir per project directory — partition into files and session dirs |
| 174 | let entries |
| 175 | try { |
| 176 | entries = await fsImpl.readdir(projectDir) |
| 177 | } catch { |
| 178 | result.errors++ |
| 179 | continue |
| 180 | } |
| 181 | |
| 182 | for (const entry of entries) { |
| 183 | if (entry.isFile()) { |
| 184 | if (!entry.name.endsWith('.jsonl') && !entry.name.endsWith('.cast')) { |
| 185 | continue |
| 186 | } |
| 187 | try { |
| 188 | if ( |
| 189 | await unlinkIfOld(join(projectDir, entry.name), cutoffDate, fsImpl) |
| 190 | ) { |
| 191 | result.messages++ |
| 192 | } |
| 193 | } catch { |
| 194 | result.errors++ |
| 195 | } |
| 196 | } else if (entry.isDirectory()) { |
| 197 | // Session directory — clean up tool-results/<toolDir>/* beneath it |
| 198 | const sessionDir = join(projectDir, entry.name) |
| 199 | const toolResultsDir = join(sessionDir, TOOL_RESULTS_SUBDIR) |
| 200 | let toolDirs |
| 201 | try { |
| 202 | toolDirs = await fsImpl.readdir(toolResultsDir) |
| 203 | } catch { |
| 204 | // No tool-results dir — still try to remove an empty session dir |
| 205 | await tryRmdir(sessionDir, fsImpl) |
| 206 | continue |
| 207 | } |
| 208 | for (const toolEntry of toolDirs) { |
| 209 | if (toolEntry.isFile()) { |
| 210 | try { |
| 211 | if ( |
| 212 | await unlinkIfOld( |
| 213 | join(toolResultsDir, toolEntry.name), |
no test coverage detected