nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Editing a PDF or DOCX overwrites the document with its markdown transcript

Open addyCooks opened this issue 3 months ago • 1 comments

Description

getCachedFileContent is the single read path for the edit tools. For .pdf and .docx it does not return the file's bytes it returns a markdown transcript produced by convertToMarkdown. The write path has no matching branch. So string_replace, diff_edit, and write_file take that transcript, apply the edit to it, and write it back over the original document as UTF-8.

The document is destroyed. A "fix the typo in the spec" request replaces a PDF with a few hundred bytes of plain text, and the tool reports success.

Environment

  • OS: Windows 11 (path is platform-independent)
  • Node version: v22.22.3
  • Nanocoder version: 1.30.0
  • Provider: any
  • Model: any

Steps to Reproduce

  1. read_file a .pdf it returns markdown and marks the path seen.
  2. string_replace on the same path, using any word from that markdown as old_str.
  3. Inspect the file.

Expected Behavior

The edit tools refuse a path whose cached content is a derived representation rather than the file itself, with an error explaining why.

Actual Behavior

BEFORE: bytes= 733  magic= "%PDF-"
read_file returned markdown, first 60: "---\nwordCount: 2\nreadingTime: 1\n---\n\nHello World\n"
replacing token: "wordCount"
string_replace RESULT: Successfully replaced content at line 2 (now line 2).
AFTER : bytes= 50  magic= "---\nT"
STILL A PDF: false

Logs/Screenshots

  • source/utils/file-cache.ts:150 if (ext === '.pdf' || ext === '.docx'), returns result.markdown as content
  • source/tools/file-ops/string-replace.tsx:92 writeFile(absPath, newContent, 'utf-8')
  • source/tools/file-ops/write-file.tsx:40 same, reachable once markFileSeen has been set by the read
  • source/tools/file-ops/diff-edit.tsx same read path, same write

CachedFile carries content, lines, mtime, cachedAt nothing that tells a caller the content is derived, so no caller can check.

Additional Context

Both undo systems are blind to this. TimelineManager explicitly skips binaries, and FileSnapshotService stores text, so it would have mangled the snapshot anyway. There is no restore path the original bytes are gone the moment the write lands.

Suggested fix have getCachedFileContent return a provenance flag (derived: true for converted formats) and have string_replace, diff_edit, and write_file refuse any path whose content is derived, naming the reason so the model stops rather than retries. An extension deny-list on the write side is a cheap defence-in-depth backstop for formats added later.

Related: #962 covers the same class of loss in checkpoints (UTF-8 round-tripping destroys binaries on save). This is the edit-tool path, which #962 does not touch, and the two fixes are independent.

  • [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 29 '26 20:08 addyCooks

Hey @akramcodez ! Happy to take this one if you'd like to assign it to me

addyCooks avatar Aug 29 '26 20:08 addyCooks