[BUG] Agent write operation truncated 31,699-line file to 16 lines — all uncommitted session work lost

Resolved 💬 5 comments Opened Mar 30, 2026 by hs902 Closed May 2, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Description:

During a Claude Code session on 2026-03-29/30, a worktree-isolated agent assigned to modify dashboard.html (31,699 lines, ~2.8MB) truncated the file to 16 lines (588 bytes) during a failed write operation.
The file contained 74 features worth of uncommitted inline code changes accumulated over a ~5 hour session.

Environment:

  • Claude Code CLI on Windows 11 Pro 10.0.26200
  • Model: Claude Opus 4.6 (1M context)
  • Platform: win32, bash shell
  • File: dashboard.html — single-page application, 31,699 lines, ~2.8MB

What happened:

  1. Over a ~5 hour session, ~74 features were built incrementally into dashboard.html through a combination of direct edits and worktree-isolated agents
  2. A worktree agent ("Add data freshness timestamps") was launched to modify dashboard.html
  3. The agent reported that the file was "accidentally truncated to 0 bytes during an edit operation (likely a linter or file watcher interfered between my read and write)"
  4. When I checked the main working directory, dashboard.html was 16 lines / 588 bytes — containing only a fragment of JS code
  5. Running git checkout HEAD -- dashboard.html restored the pre-session version (12,196 lines), but all 74 features (~19,500 lines of additions) that were accumulated during the session were lost
  6. The worktree agent's own copy was also based on git HEAD, so no worktree contained the accumulated work

What was lost:

  • 74 features including: multi-portfolio system, skill-level modes, reverse-DCF calculator, options payoff diagram, SMA/RSI chart indicators, volatility surface, bond screener, credit batch mode, consensus

estimates, company profile panel, corporate events timeline, anomaly flags, and many more

  • ~19,500 lines of inline JavaScript, HTML structure, and CSS additions
  • Estimated 5+ hours of accumulated incremental work

What survived:

  • All server-side Python files (server.py, oxford_ledge_db.py, etc.) — these were committed or in separate files
  • Extracted JS modules (formatting.js, charts.js, modals.js, views.js) — separate files
  • pulse-facts.json (10,498 entries) — the agent successfully extracted this before truncating
  • All database files, tests, MCP server, and other non-dashboard.html files

Root cause hypothesis:
The agent used the Edit tool to write to dashboard.html while another process (possibly a linter, file watcher, or another agent) was also modifying the file. The write-after-read safety check may have
failed, or the file was replaced with a partial write buffer.

Suggested mitigations:

  1. Atomic writes for large files — Write to a temp file first, then os.replace() (this pattern is already used in the Python codebase for JSON files, but not by the Edit tool)
  2. Pre-write backup — Before any Edit/Write tool operation on files >1000 lines, create a .bak copy
  3. Worktree agent file access guard — Worktree agents should ONLY modify files within their worktree, never the main working directory
  4. Session auto-commit — Periodically auto-commit uncommitted changes to a branch (e.g., every 30 minutes or every 10 successful edits)
  5. File size sanity check — After a write, verify the new file size is within 50% of the original. If it shrinks by >50%, abort and restore from the pre-write backup

Impact: ~5 hours of accumulated work lost. Rebuild estimated at 6-8 additional hours. The session had produced 74 features, 9 research plans, 17 unit tests, 10,498 educational facts, and a full company
database crawler — all of which were intact except for the dashboard.html inline changes.

Workaround applied: Manually launched rebuild agents to reconstruct the lost features from memory/documentation. Future sessions will commit dashboard.html to git before every agent batch.

What Should Happen?

If possible, I'd appreciate a credit towards extra usage time to make up for the lost time in creating and recreating the work. Thank you!

Error Messages/Logs

Steps to Reproduce

please see above

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.87 (Claude Code)

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

Windows Terminal

Additional Information

_No response_

View original on GitHub ↗

This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗