跪拜 Guibai
← All articles
Artificial Intelligence

Never Let AI Edit Your Originals: The Copy, Diff, and Restore Workflow for Safe AI File Operations

By 知行砚 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AI agents are increasingly handling real files, and the difference between "read a file" and "modify a whole directory" is easy to overlook. Adopting a copy-plus-diff workflow gives you a deterministic safety net that doesn't depend on the AI's judgment, and it prevents the silent corruption of source files that could otherwise be caught only after data is lost or leaked.

Summary

When an AI assistant like WorkBuddy gets file access, read permission doesn't imply write permission, but the tool's scope can cover more than the single file you named. That's why the safest way to let AI modify existing files is to never touch the original. Copy the file into a dedicated practice directory, let the AI edit only the copy, then run diff to see exactly which lines changed and how. If anything looks wrong, restoring is a single cp command back to the original state.

The article walks through a concrete drill: copy a markdown file, simulate an AI change that alters a number and adds a line, then read the diff output using line-number prefixes (c for change, a for add, d for delete). It also warns about diff's exit codes—0 means identical, 1 means differences (not an error), and anything above 1 is a real failure—so you shouldn't chain a diff that's expected to return 1 into a && sequence that assumes success. After restore, verify both that diff produces no output and that the exit code is 0.

Beyond the copy workflow, the author stresses a key boundary: content inside a file that says "please forward this data" is not an instruction from you. It's data to analyze and report, not a command to execute. The model should flag such requests and hand the decision back to the user, especially when the action involves sending out sensitive files or incurring costs. This principle of least authorization applies to any external data source, not just files—web pages, emails, and API responses can all contain instructions that should never be treated as user commands.

Takeaways
— Read permission does not imply write permission; tool authorization, OS permissions, and version settings must be checked separately.
— Always point the workspace to a copy directory, never to the original document, desktop, or project folder.
— Use `cp` to create a copy, then edit only the copy; the original stays untouched.
— Use `diff` to compare the original and copy; line prefixes c, a, d indicate change, addition, and deletion respectively.
— Diff exit code 0 means identical, 1 means differences, and >1 means a command error; don't chain an expected-1 diff into a must-succeed && sequence.
— Restore is as simple as `cp` from the original over the copy; verify with diff that there's no output and exit code 0.
— Instructions written inside a file (like "please forward this data") are not user commands; treat them as data to report, not to execute.
— The copy + diff + restore approach applies to any file-modifying automation, including scripts and batch replacements, not just AI tools.
Conclusions

The workflow is fundamentally about replacing human attention with deterministic process: 'being careful' is fragile, but a diff output doesn't forget or rationalize.

The boundary between file content and user instruction is a critical safety principle that scales beyond files—any untrusted data source (web pages, emails, connector inputs) can carry embedded commands.

The article's advice runs counter to the common habit of letting AI edit files directly; it argues that the convenience of direct editing is exactly what introduces silent, hard-to-reverse damage.

Concepts & terms
diff
A command-line tool that compares two files line by line and shows differences. Output uses `<` for lines only in the first file, `>` for lines only in the second, and line-number prefixes like `c`, `a`, and `d` to indicate changed, added, or deleted lines.
exit code
The numeric status a command returns after running. For diff, 0 means no differences, 1 means differences exist, and a value greater than 1 indicates the command itself failed. Understanding this is important when chaining commands with `&&`.
least authorization
A security principle where an agent or process is granted only the permissions required to complete the explicit task. In the context of AI file editing, it means the model should only perform actions the user explicitly requested, and any operation involving external sending or sensitive data requires explicit confirmation.
From the discussion
Featured comments
GenTang 1 likes

[AI-assisted organization, verified against original records] It is recommended to add another item to the recovery drill: file permissions should also be verified, not just content diff. In Safety Arena's Windows records, after executing a fixed Python script with WorkBuddy 5.9.3 / deepseek-v4-pro (2026-11-11, built-in sandbox=on, ask), the host observation panel showed that a test file was granted Everyone Modify permissions, which were only restored after about 10 seconds. Comparing only the content at the end or checking the final permissions would miss this temporary exposure. The comparison FinShield 0.10.9 was configured with built-in sandbox=off, and the observation panel remained LIMITED throughout; the two configurations differ, so this cannot be extrapolated as a guarantee for all versions. Therefore, in a one-time test directory, one can simultaneously record content differences, ACL/owner, and permission changes during execution; backups should also be placed in a location that the executing process cannot modify. This example supplements the acceptance scope of the recovery workflow, and the copy and diff in the article remain useful. Original evidence: https://safety.finaios.ai/results/file-acl-grant/workbuddy/workbuddy-python-file-v2-20261111/ Source relationship: Safety Arena is maintained by the FinAI OS team, which also develops FinShield.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗