021a076880
Two DATA-LOSS bugs in ShellFileOperations found in a core-tools audit, each reproduced live against current main: 1. Non-UTF-8 file content silently corrupted on read->write. The terminal env decodes stdout with errors='replace', so a latin-1/8859 file's bytes arrive as U+FFFD before _is_likely_binary inspects them. U+FFFD is 'printable', so the >30%-non-printable check never flagged it, and the agent would read the mojibake and write it back, permanently replacing the original bytes. Fix: treat a sample containing U+FFFD as binary (read-only). 2. Writing through a symlink destroyed the link and orphaned the target. The atomic temp-file + 'mv -f' swap replaced the symlink itself with a plain file; the real target was never updated. Fix: resolve the link with readlink -f/realpath first and recompute the temp dir from the resolved target so the mv stays same-filesystem atomic. Broken links fall back to the original path (no regression). Both verified with sabotage-checked regression tests (fail without the fix). Proper UTF-8 text (incl. non-ASCII) and plain-file writes are unaffected.