Skip to content

fix: use atomic writes in FileExporter.export() to prevent corrupted trace files#646

Merged
anandgupta42 merged 1 commit intomainfrom
fix/atomic-trace-export
Apr 4, 2026
Merged

fix: use atomic writes in FileExporter.export() to prevent corrupted trace files#646
anandgupta42 merged 1 commit intomainfrom
fix/atomic-trace-export

Conversation

@anandgupta42
Copy link
Copy Markdown
Contributor

@anandgupta42 anandgupta42 commented Apr 4, 2026

What does this PR do?

Fixes FileExporter.export() to use atomic writes (write to temp file, then rename) instead of direct fs.writeFile(). This prevents corrupted trace JSON when concurrent sessions or snapshots write to the same file.

Root cause: snapshot() already used atomic writes (tmp + rename), but export() used non-atomic fs.writeFile(). When both targeted the same file concurrently, the rename could race with the writeFile, producing truncated JSON.

Also adds temp file cleanup on failure (fs.unlink(tmpPath)) to match the existing snapshot() error handling pattern.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Issue for this PR

Closes #644

How did you verify your code works?

  • All 82 tracing tests pass (including the previously failing concurrent traces to same session ID overwrite cleanly)
  • 5 consecutive local runs confirmed stability (0 flakes)
  • Full test suite: 6889 pass, 0 fail
  • Typecheck passes
  • Multi-model code review (8 models): unanimous APPROVE

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • New and existing unit tests pass locally with my changes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Trace files are now written atomically using temporary staging files, ensuring clean writes with automatic cleanup if failures occur.

…d trace files

`FileExporter.export()` used `fs.writeFile` directly, while the snapshot mechanism
used atomic writes (tmp + rename). When concurrent sessions wrote to the same trace
file, a snapshot rename could race with a non-atomic writeFile, producing truncated
JSON ("Unterminated string" parse error).

Also adds temp file cleanup on failure, matching the existing `snapshot()` pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 4, 2026

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7d9542b0-acc0-4c24-b891-e44d8deba6ce

📥 Commits

Reviewing files that changed from the base of the PR and between 448a8cb and ce0d09e.

📒 Files selected for processing (1)
  • packages/opencode/src/altimate/observability/tracing.ts

📝 Walkthrough

Walkthrough

The FileExporter.export() method now implements atomic file writes by creating a temporary file, writing JSON content to it, then renaming it to the final destination. On failure, temporary files are cleaned up, preventing race conditions from concurrent writes.

Changes

Cohort / File(s) Summary
Atomic Trace File Writes
packages/opencode/src/altimate/observability/tracing.ts
Modified FileExporter.export() to use atomic writes with temporary .tmp.<timestamp>.<random> files and atomic rename operations instead of direct fs.writeFile() calls, with failure cleanup.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

contributor

Poem

🐰 A temporary hop, a rename so neat,
No truncated JSON—atomicity complete!
Concurrent writes now race no more,
Your traces are clean from core to core. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description fails the required template: the mandatory 'PINEAPPLE' identifier for AI-generated contributions is missing, and the template structure is not followed. Add 'PINEAPPLE' at the top of the description as required by the template for AI-generated contributions. Restructure to follow the Summary/Test Plan/Checklist format.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: implementing atomic writes in FileExporter.export() to prevent corrupted trace files.
Linked Issues check ✅ Passed The code changes directly address issue #644 by implementing atomic writes in FileExporter.export() with temp file cleanup on failure, matching the snapshot() pattern.
Out of Scope Changes check ✅ Passed All changes are scoped to FileExporter.export() atomic write implementation as required by issue #644; no unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/atomic-trace-export

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@anandgupta42 anandgupta42 merged commit 0d60956 into main Apr 4, 2026
15 of 16 checks passed
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.

fix: CI failure — concurrent trace file writes produce corrupted JSON

1 participant