Skip to content

fix(log): sort files before slicing to delete oldest logs first#14792

Open
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/issue-14731-log-cleanup-order
Open

fix(log): sort files before slicing to delete oldest logs first#14792
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/issue-14731-log-cleanup-order

Conversation

@zerone0x
Copy link
Contributor

Issue for this PR

Closes #14731

Type of change

  • Bug fix

What does this PR do?

The cleanup() function in packages/opencode/src/util/log.ts was deleting the newest log files and keeping the oldest ones — the opposite of intended behavior.

Root cause: Glob.scan() uses path-scurry internally, which may return directory entries in reverse chronological order (newest-first). Without an explicit sort, files.slice(0, -10) was selecting the newest files for deletion.

Two bugs fixed:

  1. Added files.sort() before slicing — since filenames are ISO-8601 timestamps, lexicographic sort is chronological, so the oldest files end up at index 0 and get deleted correctly.
  2. Aligned the early-return guard from <= 5 to <= 10 to match the slice(0, -10) that keeps 10 files (previously, 6–10 files would pass the guard but slice would return an empty array).

How did you verify your code works?

Reviewed the logic: after files.sort(), the array is oldest-first. files.slice(0, -10) selects everything except the last 10 entries, i.e., all files older than the 10 most recent — which is the correct set to delete. The guard threshold of <= 10 means we skip cleanup when there are 10 or fewer files, consistent with always keeping exactly 10.

Screenshots / recordings

N/A — logic change only

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code

Glob.scan() / path-scurry may return files in reverse chronological
order (newest-first), causing cleanup() to delete the newest log files
instead of the oldest ones.

Two bugs fixed:
1. Add files.sort() so ISO-8601 filenames are in chronological order
   before slicing, ensuring oldest files are deleted.
2. Align guard threshold from <= 5 to <= 10 to match the slice(0, -10)
   that keeps 10 files.

Fixes anomalyco#14731

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

The search results show PR #14792 (the current PR) and two older related PRs. Let me check if those older PRs are still open and relevant:

However, PR #7245 appears to be an older PR (numbered 7245 vs current 14792), so it's likely already closed/merged. The current PR #14792 is addressing the specific issue from #14731 with a comprehensive fix including both the sort and the guard threshold alignment.

No duplicate PRs found (PR #7245 is likely an older, merged version addressing similar concerns, but not an active duplicate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log cleanup deletes newest log files instead of oldest (path-scurry reverse ordering)

1 participant