nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Checkpoint snapshots are UTF-8-only at every layer, irrecoverably corrupting binary files on save and restore

Open addyCooks opened this issue 3 months ago • 3 comments

Description

Every layer of the checkpoint pipeline reads and writes file contents as UTF-8 text. Binary files, images, icons, .vsix bundles, sqlite databases, fonts, any compiled artifact are not valid UTF-8, so each invalid byte is replaced with U+FFFD (EF BF BD). The file is destroyed, no error is raised, and nothing in the UI indicates anything went wrong.

There are four chokepoints, and the encoding is hardcoded at all of them:

# Location Operation
1 source/services/file-snapshot.ts:29 captureFiles reads the working file as 'utf-8'
2 source/services/checkpoint-manager.ts:174 writes the snapshot into files/ as 'utf-8'
3 source/services/checkpoint-manager.ts:228 loadCheckpoint reads it back as 'utf-8'
4 source/services/file-snapshot.ts:58 restoreFiles writes over the working file as 'utf-8'

The most serious consequence is at steps 1–2, not step 4. The bytes are already lost when the checkpoint is created. The copy persisted under .nanocoder/checkpoints/<name>/files/ is corrupt at rest, so the original content cannot be recovered from the checkpoint either. A repair tool written after the fact would have nothing to recover this is unrecoverable data loss at save time, and the overwrite at step 4 is merely where the user finds out.

Nothing filters binary files out of the path. getModifiedFiles (source/services/file-snapshot.ts:73) returns everything from git diff --name-only HEAD plus git ls-files --others --exclude-standard, filtered only by .gitignore and capped at MAX_CHECKPOINT_FILES. There is no text/binary check anywhere between git and fs.writeFile.

Affected trigger paths all three call the same service:

  • /checkpoint create (source/commands/checkpoint.tsx:82) manual
  • Every daemon-triggered skill run (source/skills/dispatcher.ts:121) automatic, unless in plan mode
  • The safety backup taken immediately before every restore (source/hooks/useAppHandlers.tsx:446) automatic

That last one compounds the damage: the safety net is itself built from lossy snapshots, so restoring twice overwrites the last clean copy on disk with a doubly-corrupted one.

Environment

  • OS: Windows 11 platform-independent; the defect is in the encoding, not the filesystem
  • Node version: v22.22.3
  • Nanocoder version: 1.29.0
  • Provider: N/A
  • Model: N/A reproducible with no LLM involved; the checkpoint service is provider-agnostic

Steps to Reproduce

  1. In a git repo, commit a binary file (e.g. a PNG at assets/logo.png)
  2. Modify it so git diff --name-only HEAD reports it
  3. Run /checkpoint create test
  4. Inspect .nanocoder/checkpoints/test/files/assets/logo.png it is already corrupt, before any restore has happened
  5. Modify the working file again
  6. Run /checkpoint restore test
  7. The working file is now corrupt too, and the original is unrecoverable

Expected Behavior

A checkpoint round-trip is byte-preserving. Any file that can be captured can be restored byte-identically, regardless of its contents. If a file genuinely cannot be snapshotted, that is surfaced to the user rather than silently degraded.

Actual Behavior

Bytes are destroyed at capture, and again at restore. Output on 1.29.0:

original bytes : 67  89504e470d0a1a0a
restored bytes : 77  efbfbd504e470d0a
identical      : false

The PNG magic byte 0x89 becomes EF BF BD (U+FFFD), and the file grows 67 → 77 bytes as every non-UTF-8 byte is replaced. The image no longer opens in any viewer, and neither the working file nor the stored snapshot contains the original data.

Logs

Nanocoder emits no logs for this no error occurs at any layer, so there is nothing to capture. The evidence is a byte comparison instead.

Driving the real CheckpointManager end to end (save → inspect stored copy → restore):

  file                          bytes   first 12 bytes
  ------------------------------------------------------------------
  original on disk              67      89 50 4e 47 0d 0a 1a 0a 00 00 00 0d
  stored inside the checkpoint  77      ef bf bd 50 4e 47 0d 0a 1a 0a 00 00
  restored to the workspace     77      ef bf bd 50 4e 47 0d 0a 1a 0a 00 00

  checkpoint copy matches original : false
  restored file matches original   : false
  valid PNG signature preserved    : false

The PNG signature byte 0x89 is replaced by EF BF BD (U+FFFD) and the file grows 67 → 77 bytes. Note the second row: the copy stored inside the checkpoint is already corrupt, before any restore has been attempted. The original bytes are gone at save time, so no recovery from the checkpoint is possible.

Additional Context

Proposed spec

Per CONTRIBUTING ("No spec? Write one"):

Fix: stop specifying an encoding. Change fileSnapshots from Map<string, string> to Map<string, Buffer> and drop the 'utf-8' argument at all four sites. Node writes a Buffer verbatim, so this needs no binary detection, no encoding marker, and no metadata changeand text files still round-trip byte-identically.

Blast radius: contained. Nothing reads snapshot content as a string types/checkpoint.ts:29 (the type), file-snapshot.ts, checkpoint-manager.ts, and two call sites that only touch .size.

Compatibility: old checkpoints still load. Their binary entries stay corrupt, that data is already gone and text ones are unaffected.

Alternative rejected: base64-encoding only detected binaries needs a metadata schema change, a heuristic that can be wrong at the margins, and a migration path, to land where the Buffer approach lands with none of them.

Tests: the repro above, plus a CheckpointManager round-trip asserting the stored copy under files/ is byte-identical that second one pins chokepoints 2 and 3, which the service-level test doesn't reach.

Two adjacent gaps in the same path

Both are smaller, and I would be glad to split them into their own issue if you would prefer this one stay single-purpose:

  • captureFiles skips unreadable files with only a warning (file-snapshot.ts:32), so a restore can silently be partial likely on Windows, where editors and antivirus hold file locks
  • getModifiedFiles truncates at MAX_CHECKPOINT_FILES = 50 and warns at capture time, but nothing surfaces at restore time, so the user is never told their restore was incomplete

Note

This repository ships assets/nanocoder-vscode.vsix, regenerated by pnpm run build:vscode and modified in open PRs #887 and #826 a tracked binary changed in exactly the workflow where checkpoints run.

Happy to open a PR for this if the approach looks right.

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

addyCooks avatar Aug 26 '26 08:08 addyCooks

Hi @akramcodez, Could you assign it to me?

addyCooks avatar Aug 26 '26 08:08 addyCooks

@addyCooks great finding man, go for it :)

akramcodez avatar Aug 26 '26 09:08 akramcodez

Thanks, @akramcodez! I'll get started on it

addyCooks avatar Aug 26 '26 10:08 addyCooks