fix(tracing): sweep stale .tmp.* trace files when maxFiles is 0

DE
11d ago
+44-3

Issue for this PR

Closes #874

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

FileExporter.export() only called pruneOldTraces() when maxFiles > 0. The stale .json.tmp.<ts>.<rand> sweep lives inside pruneOldTraces(), so with maxFiles: 0 (unlimited retention) temp files left behind by a crash between writeFile and rename were never cleaned up.

export() now always calls pruneOldTraces(), and pruneOldTraces() returns right after the tmp sweep when maxFiles <= 0, so unlimited retention still never deletes any .json trace. Behaviour for maxFiles > 0 is unchanged.

How did you verify your code works?

  • New test in packages/opencode/test/altimate/tracing-adversarial.test.ts: with maxFiles: 0, a tmp file older than 1h is removed, a fresh in-flight tmp file and the exported trace are kept. It fails on main (stale file is still there after 5s) and passes with the fix.
  • bun test test/altimate/tracing*.test.ts test/skill/release-v0.5.19-adversarial.test.ts and the other tracing suites: 189 pass, 0 fail.
  • packages/opencode typecheck (tsgo --noEmit): no errors.

Cost side effects

None. The sweep is one readdir of the local traces directory per export, which already happens for maxFiles > 0.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR
  • I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"

Devin Review


Summary by cubic

Fixes stale .json.tmp.* trace files never being swept when maxFiles is 0 (unlimited retention). Previously export() only ran the sweep when maxFiles > 0, so temp files left behind by a crash between write and rename could accumulate forever. Now pruneOldTraces() always runs but returns after the tmp sweep when maxFiles <= 0, so unlimited retention still never deletes completed .json traces; behaviour for maxFiles > 0 is unchanged. Closes #874.