( teamName?: string, maxAgeMs = 3600000, )
| 450 | * @param maxAgeMs - Maximum age in milliseconds (default: 1 hour) |
| 451 | */ |
| 452 | export async function cleanupOldResolutions( |
| 453 | teamName?: string, |
| 454 | maxAgeMs = 3600000, |
| 455 | ): Promise<number> { |
| 456 | const team = teamName || getTeamName() |
| 457 | if (!team) { |
| 458 | return 0 |
| 459 | } |
| 460 | |
| 461 | const resolvedDir = getResolvedDir(team) |
| 462 | |
| 463 | let files: string[] |
| 464 | try { |
| 465 | files = await readdir(resolvedDir) |
| 466 | } catch (e: unknown) { |
| 467 | const code = getErrnoCode(e) |
| 468 | if (code === 'ENOENT') { |
| 469 | return 0 |
| 470 | } |
| 471 | logForDebugging(`[PermissionSync] Failed to cleanup resolutions: ${e}`) |
| 472 | logError(e) |
| 473 | return 0 |
| 474 | } |
| 475 | |
| 476 | const now = Date.now() |
| 477 | const jsonFiles = files.filter(f => f.endsWith('.json')) |
| 478 | |
| 479 | const cleanupResults = await Promise.all( |
| 480 | jsonFiles.map(async file => { |
| 481 | const filePath = join(resolvedDir, file) |
| 482 | try { |
| 483 | const content = await readFile(filePath, 'utf-8') |
| 484 | const request = jsonParse(content) as SwarmPermissionRequest |
| 485 | |
| 486 | // Check if the resolution is old enough to clean up |
| 487 | // Use >= to handle edge case where maxAgeMs is 0 (clean up everything) |
| 488 | const resolvedAt = request.resolvedAt || request.createdAt |
| 489 | if (now - resolvedAt >= maxAgeMs) { |
| 490 | await unlink(filePath) |
| 491 | logForDebugging(`[PermissionSync] Cleaned up old resolution: ${file}`) |
| 492 | return 1 |
| 493 | } |
| 494 | return 0 |
| 495 | } catch { |
| 496 | // If we can't parse it, clean it up anyway |
| 497 | try { |
| 498 | await unlink(filePath) |
| 499 | return 1 |
| 500 | } catch { |
| 501 | // Ignore deletion errors |
| 502 | return 0 |
| 503 | } |
| 504 | } |
| 505 | }), |
| 506 | ) |
| 507 | |
| 508 | const cleanedCount = cleanupResults.reduce<number>((sum, n) => sum + n, 0) |
| 509 |
nothing calls this directly
no test coverage detected