fix: truncation cleanup deletes fresh files — Identifier 48-bit timestamp wrapped on 2026-08-14
Issue for this PR
Closes #1112
Type of change
- Bug fix
What does this PR do?
Fixes the Truncate > cleanup test failing on every CI run since 2026-08-17 (on unchanged code), and the underlying live data-loss bug.
Identifier.create packs timestamp * 4096 + counter into 6 bytes, which wraps every 2^36 ms (~795.4 days). The 26th wrap since epoch landed 2026-08-14T11:19:55Z. After that boundary, newly created IDs decode to tiny timestamps while the 7-day-retention cutoff (computed from a pre-wrap timestamp) decodes as astronomically large — so both truncation cleanups considered every freshly written file "older than 7 days" and deleted truncated tool outputs the moment cleanup ran.
Fix: both cleanups (tool/truncate.ts Effect service and the legacy tool/truncation.ts module used by bootstrap/bash/prompt) now age files by mtime, which does not wrap; stat failures keep the file (deletion fails safe). Tagged upstream_fix — the wrap-prone ID encoding is upstream OpenCode code; if upstream reworks the encoding we can drop the marker.
Why this is correct: the wrap arithmetic is verified (2^36 ms = 795.4 days; boundary 26 × 2^36 ms = 2026-08-14T11:19:55Z, matching the first CI failure on Aug 17 when the test's now - 3 days fixture crossed the boundary), and mtime-based aging removes the dependence on the ID encoding entirely.
How did you verify your code works?
- Reproduced the failing test locally (recent file deleted), then green after the fix; test now sets explicit mtimes (
utimes) instead of ID-embedded timestamps. - Full
test/tooldirectory: 496 pass / 0 fail; typecheck clean; marker check clean (upstream_fixmarkers in place). - Not verified: behavior at the NEXT wrap boundary (Nov 2028) — irrelevant now that aging uses mtime.
Screenshots / recordings
Not a UI change.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
🤖 Generated with Claude Code
[!NOTE]
Medium Risk
Touches scheduled cleanup that deletes persisted tool outputs; behavior change is intentional but wrong mtime handling could retain stale files or, if fail-safe were removed, delete unpredictably.
Overview
Fixes truncated tool output being deleted immediately after the 48-bit Identifier timestamp encoding wrapped (~2026-08-14). Cleanup no longer infers age from tool_* filenames; it uses file mtime against a 7-day cutoff in both the Effect Truncate service and the legacy truncation module.
Fail-safe behavior: stat errors, missing mtime, or unstatable entries (e.g. dangling symlinks) are kept, not deleted. The Effect path stats via injected FSUtil so in-memory/custom FS matches production.
Tests set explicit utimes instead of ID-embedded times, add a dangling-symlink case, and introduce scoped symlinkScoped for cleanup.
Reviewed by Cursor Bugbot for commit 78a9ddc6aa9f21451b35e07973ef801fb8794f10. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Prevents truncation cleanup from deleting fresh tool outputs by aging files by mtime instead of decoding Identifier timestamps. After the 48-bit timestamp wrapped on 2026-08-14, new files decoded as “ancient” and were removed; cleanup now compares file mtime to the retention cutoff and keeps files on stat errors.
- Updates both cleanup paths:
packages/opencode/src/tool/truncate.tsstats via injectedFSUtil.Service(from@opencode-ai/core/fs-util);packages/opencode/src/tool/truncation.tsuses Nodefs. Deletes only whenmtimeMs < cutoffMs; stat failure or absent mtime keeps the file (including dangling symlinks). - Tests set explicit mtimes and add a dangling symlink case verified with
lstat; introducessymlinkScopedthat uses the injectedFileSystemservice for teardown even on failed assertions. No public API or configuration changes; no migration required.
Summary by CodeRabbit
-
Bug Fixes
- Improved cleanup of temporary tool files by using their actual filesystem modification times.
- Prevented files from being deleted when their metadata cannot be read, reducing the risk of accidental removal.
- Preserved existing filtering and error-handling behavior, including safe handling of dangling links and failed deletions.
-
Tests
- Updated cleanup coverage to verify removal based on file age rather than identifier contents.
- Added checks ensuring recent files and files with unavailable metadata are preserved.