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