[PR #7245] fix(log): await cleanup and sort files before deletion #12318

Open
opened 2026-02-16 18:17:14 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7245

State: open
Merged: No


Summary

  • Await cleanup() in init() to prevent race condition where cleanup could run concurrently with file creation
  • Sort files before slicing to ensure oldest files are deleted, not arbitrary files based on filesystem order

Problem

Log files were being deleted immediately after creation due to two issues:

  1. cleanup() was called fire-and-forget (not awaited), creating a race condition
  2. Bun.Glob.scan() returns files in arbitrary filesystem order, so slice(0, -10) was deleting random files instead of the oldest ones

Fix

  • Added await before cleanup() call
  • Added .sort() before .slice(0, -10) to ensure lexicographic order (which matches chronological order for the YYYY-MM-DDTHHMMSS.log filename format)

Tests

Added 8 tests covering:

  • File creation (timestamp name, dev.log)
  • Cleanup threshold behavior (5, 10, 11 files)
  • Oldest file deletion
  • Newest 10 files preserved
  • dev.log excluded from cleanup (doesn't match glob)
  • New log file created after cleanup runs
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7245 **State:** open **Merged:** No --- ## Summary - Await `cleanup()` in `init()` to prevent race condition where cleanup could run concurrently with file creation - Sort files before slicing to ensure oldest files are deleted, not arbitrary files based on filesystem order ## Problem Log files were being deleted immediately after creation due to two issues: 1. `cleanup()` was called fire-and-forget (not awaited), creating a race condition 2. `Bun.Glob.scan()` returns files in arbitrary filesystem order, so `slice(0, -10)` was deleting random files instead of the oldest ones ## Fix - Added `await` before `cleanup()` call - Added `.sort()` before `.slice(0, -10)` to ensure lexicographic order (which matches chronological order for the `YYYY-MM-DDTHHMMSS.log` filename format) ## Tests Added 8 tests covering: - File creation (timestamp name, dev.log) - Cleanup threshold behavior (5, 10, 11 files) - Oldest file deletion - Newest 10 files preserved - dev.log excluded from cleanup (doesn't match glob) - New log file created after cleanup runs
yindo added the pull-request label 2026-02-16 18:17:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12318