idempotent-file-replace
What it does
This skill tells the AI agent to read a file and compare it before replacing it. It is for people whose builds or version history would be disturbed by unneeded writes.
When to use it
- βWhen you ask the agent to completely replace a file with specific content.
- βWhen file changes would trigger rebuilds or hot reloads for no reason.
- βWhen you want clean version history without empty diffs.
Skill link
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/idempotent-file-replace/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βReplace the config file with this content, but only write it if the file is actually different.β
5 new skills a week. 1 minute to read.
How it works
The agent reads the current file first, then compares it with the desired content. It writes only when the two differ, and it can show a diff to explain what would change.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Idempotent File Replace
When tasked with completely replacing a file with specific content, always perform a read-and-compare step before writing. Only write when the file actually differs from the desired content.
Why This Matters
| Risk of blind writes | Benefit of idempotent check |
|---|---|
| Triggers filesystem watchers / hot-reload loops | No spurious rebuild when content is unchanged |
| Wastes a tool call on a no-op | One extra read saves a write + all downstream costs |
| Pollutes VCS history with empty diffs | Clean commit history |
| Breaks CI caching layers | Stable mtimes keep caches valid |
Procedure
Step 1 β Read the current file
Always read the file first, even when you are confident about the replacement.
read_file(path="<target_file>")
If the file does not yet exist, treat its current content as an empty string and proceed to Step 3.
Step 2 β Compare current content to desired content
Perform an exact string comparison (whitespace-sensitive).
current = <content returned by read_file>
desired = <full replacement content>
needs_write = (current.strip() != desired.strip())
# Use .strip() to tolerate a single trailing newline difference,
# or compare verbatim if byte-exact output is required.
Alternatively, a unified diff gives a human-readable explanation of what would change and is useful for logging:
import difflib, sys
diff = list(difflib.unified_diff(
current.splitlines(keepends=True),
desired.splitlines(keepends=True),
fromfile="current",
tofile="desired",
))
if not diff:
print("Files are identical β skipping write.")
needs_write = False
else:
print("Diff detected:\n" + "".join(diff))
needs_write = True
Step 3 β Conditional write
if needs_write:
write_file(path="<target_file>", content=desired)
print("File written.")
else:
# Report completion immediately β no write needed.
print("File already matches desired content. No action taken.")
Decision Flowchart
Task: "Replace <file> with <content>"
β
βΌ
read_file(<file>)
β
βΌ
content == desired?
ββββββ΄βββββ
YES NO
β β
βΌ βΌ
SKIP write_file(<file>, desired)
write β
β βΌ
ββββΊ Report COMPLETE
Complete Example
TARGET = "src/components/Panel.ts"
DESIRED_CONTENT = """// Auto-generated β do not edit
export const Panel = () => { ... };
"""
# Step 1: read
current = read_file(path=TARGET) # tool call
# Step 2: compare
if current.strip() == DESIRED_CONTENT.strip():
print(f"{TARGET} already matches. Skipping write.")
# β COMPLETE
else:
# Step 3: write
write_file(path=TARGET, content=DESIRED_CONTENT) # tool call
print(f"{TARGET} updated.")
# β COMPLETE
Edge Cases
| Situation | Handling |
|---|---|
| File does not exist | read_file raises / returns empty β treat as "" β always write |
| Encoding differences | Normalize to UTF-8 before comparison |
| Line-ending differences (CRLF vs LF) | Normalize with .replace("\r\n", "\n") before comparing |
| Byte-exact requirement | Skip .strip() and compare verbatim |
| Large files | Compute SHA-256 hash of both strings for efficiency before full diff |
Key Principle
Never assume a file needs to be written. One cheap read prevents an expensive (and potentially harmful) write.
This pattern applies to any "replace entire file" task regardless of file type: source code, configuration, templates, lock files, generated assets, etc.
FAQ
What if the file does not exist yet?
The skill treats the current content as empty and writes the new file.
Does whitespace matter?
The skill compares content strictly, with an option to ignore a single trailing newline difference.
Why does this help builds?
Skipping unchanged writes keeps file times stable, so watchers and caches are not triggered.
Details
Original description by the author
Read-then-diff before writing a file to ensure the write is skipped when the content already matches, preventing unnecessary modifications and downstream side effects.
repo: HKUDS/AgentPilot
Popularity
- GitHub stars
- 7.8k
- Ranking position
- #13056
- License
- MIT
- Updated
- 11/1/2026
Comments
- β¦
More skills from this repository
agentpilot
7.8kHelps your agent improve a project step by step by preparing a task and running the AgentPilot loop that proposes, tests and scores changes.
openspace
7.8kHelps your agent set up OpenSpace, a tool that stores and fetches agent skills, and connect it to your AI app through MCP.
rsi-arena
7.8kHelps your agent set up and run standard AI benchmark tests, such as AIME and Terminal-Bench, to compare different improvement methods.
experiment-guide
7.8kHelps your agent plan small experiments, read their results and record findings while it investigates how to improve a program.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.