[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6KKBZRX9QEP8P59YDSXFB89":3,"skill-same:01M6KKBZRX9QEP8P59YDSXFB89":64},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":22,"publishedAt":32,"linkOnly":33,"markdownContent":34,"markdownBody":35,"frontMatter":36,"refName":37,"commitSha":38,"skillDirectory":39,"forksCount":13,"repositoryHtmlUrl":40,"repositoryHomepageUrl":6,"ownerHtmlUrl":41,"ownerBlogUrl":6,"ownerDescription":42,"updatedAt":43,"install":44,"resources":57},"01M6KKBZRX9QEP8P59YDSXFB89","idempotent-file-replace",null,"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.","AG064\u002FOpenSpace","AG064","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F184930299?v=4","01M6KHVFG7TB73NHX3RDYSTMH2","01M6KHVFMM7XA2PZA9Z83PHS3V",0,"MIT","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fidempotent-file-replace\u002FSKILL.md",[17],{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},"01JBSEEDCAT000000000000036","内容创作者","PROFESSION","适用职业",[23,24,25,26,27,28,29,30,31],"file","idempotent file replace","idempotent","writing","content","read","write","diff","effects","2026-11-10 10:12:30",false,"---\nname: idempotent-file-replace\ndescription: 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.\n---\n\n# Idempotent File Replace\n\nWhen tasked with **completely replacing** a file with specific content, always\nperform a read-and-compare step before writing. Only write when the file\nactually differs from the desired content.\n\n## Why This Matters\n\n| Risk of blind writes | Benefit of idempotent check |\n|---|---|\n| Triggers filesystem watchers \u002F hot-reload loops | No spurious rebuild when content is unchanged |\n| Wastes a tool call on a no-op | One extra read saves a write + all downstream costs |\n| Pollutes VCS history with empty diffs | Clean commit history |\n| Breaks CI caching layers | Stable mtimes keep caches valid |\n\n---\n\n## Procedure\n\n### Step 1 — Read the current file\n\nAlways read the file first, even when you are confident about the replacement.\n\n```\nread_file(path=\"\u003Ctarget_file>\")\n```\n\nIf the file does **not yet exist**, treat its current content as an empty\nstring and proceed to Step 3.\n\n---\n\n### Step 2 — Compare current content to desired content\n\nPerform an exact string comparison (whitespace-sensitive).\n\n```python\ncurrent  = \u003Ccontent returned by read_file>\ndesired  = \u003Cfull replacement content>\n\nneeds_write = (current.strip() != desired.strip())\n# Use .strip() to tolerate a single trailing newline difference,\n# or compare verbatim if byte-exact output is required.\n```\n\nAlternatively, a unified diff gives a human-readable explanation of what\nwould change and is useful for logging:\n\n```python\nimport difflib, sys\n\ndiff = list(difflib.unified_diff(\n    current.splitlines(keepends=True),\n    desired.splitlines(keepends=True),\n    fromfile=\"current\",\n    tofile=\"desired\",\n))\n\nif not diff:\n    print(\"Files are identical — skipping write.\")\n    needs_write = False\nelse:\n    print(\"Diff detected:\\n\" + \"\".join(diff))\n    needs_write = True\n```\n\n---\n\n### Step 3 — Conditional write\n\n```python\nif needs_write:\n    write_file(path=\"\u003Ctarget_file>\", content=desired)\n    print(\"File written.\")\nelse:\n    # Report completion immediately — no write needed.\n    print(\"File already matches desired content. No action taken.\")\n```\n\n---\n\n## Decision Flowchart\n\n```\nTask: \"Replace \u003Cfile> with \u003Ccontent>\"\n        │\n        ▼\n   read_file(\u003Cfile>)\n        │\n        ▼\n  content == desired?\n   ┌────┴────┐\n  YES       NO\n   │         │\n   ▼         ▼\n SKIP     write_file(\u003Cfile>, desired)\n write        │\n   │          ▼\n   └──► Report COMPLETE\n```\n\n---\n\n## Complete Example\n\n```python\nTARGET = \"src\u002Fcomponents\u002FPanel.ts\"\nDESIRED_CONTENT = \"\"\"\u002F\u002F Auto-generated — do not edit\nexport const Panel = () => { ... };\n\"\"\"\n\n# Step 1: read\ncurrent = read_file(path=TARGET)          # tool call\n\n# Step 2: compare\nif current.strip() == DESIRED_CONTENT.strip():\n    print(f\"{TARGET} already matches. Skipping write.\")\n    # → COMPLETE\nelse:\n    # Step 3: write\n    write_file(path=TARGET, content=DESIRED_CONTENT)   # tool call\n    print(f\"{TARGET} updated.\")\n    # → COMPLETE\n```\n\n---\n\n## Edge Cases\n\n| Situation | Handling |\n|---|---|\n| File does not exist | `read_file` raises \u002F returns empty → treat as `\"\"` → always write |\n| Encoding differences | Normalize to UTF-8 before comparison |\n| Line-ending differences (CRLF vs LF) | Normalize with `.replace(\"\\r\\n\", \"\\n\")` before comparing |\n| Byte-exact requirement | Skip `.strip()` and compare verbatim |\n| Large files | Compute SHA-256 hash of both strings for efficiency before full diff |\n\n---\n\n## Key Principle\n\n> **Never assume a file needs to be written. One cheap read prevents an\n> expensive (and potentially harmful) write.**\n\nThis pattern applies to any \"replace entire file\" task regardless of file\ntype: source code, configuration, templates, lock files, generated assets,\netc.","# Idempotent File Replace\n\nWhen tasked with **completely replacing** a file with specific content, always\nperform a read-and-compare step before writing. Only write when the file\nactually differs from the desired content.\n\n## Why This Matters\n\n| Risk of blind writes | Benefit of idempotent check |\n|---|---|\n| Triggers filesystem watchers \u002F hot-reload loops | No spurious rebuild when content is unchanged |\n| Wastes a tool call on a no-op | One extra read saves a write + all downstream costs |\n| Pollutes VCS history with empty diffs | Clean commit history |\n| Breaks CI caching layers | Stable mtimes keep caches valid |\n\n---\n\n## Procedure\n\n### Step 1 — Read the current file\n\nAlways read the file first, even when you are confident about the replacement.\n\n```\nread_file(path=\"\u003Ctarget_file>\")\n```\n\nIf the file does **not yet exist**, treat its current content as an empty\nstring and proceed to Step 3.\n\n---\n\n### Step 2 — Compare current content to desired content\n\nPerform an exact string comparison (whitespace-sensitive).\n\n```python\ncurrent  = \u003Ccontent returned by read_file>\ndesired  = \u003Cfull replacement content>\n\nneeds_write = (current.strip() != desired.strip())\n# Use .strip() to tolerate a single trailing newline difference,\n# or compare verbatim if byte-exact output is required.\n```\n\nAlternatively, a unified diff gives a human-readable explanation of what\nwould change and is useful for logging:\n\n```python\nimport difflib, sys\n\ndiff = list(difflib.unified_diff(\n    current.splitlines(keepends=True),\n    desired.splitlines(keepends=True),\n    fromfile=\"current\",\n    tofile=\"desired\",\n))\n\nif not diff:\n    print(\"Files are identical — skipping write.\")\n    needs_write = False\nelse:\n    print(\"Diff detected:\\n\" + \"\".join(diff))\n    needs_write = True\n```\n\n---\n\n### Step 3 — Conditional write\n\n```python\nif needs_write:\n    write_file(path=\"\u003Ctarget_file>\", content=desired)\n    print(\"File written.\")\nelse:\n    # Report completion immediately — no write needed.\n    print(\"File already matches desired content. No action taken.\")\n```\n\n---\n\n## Decision Flowchart\n\n```\nTask: \"Replace \u003Cfile> with \u003Ccontent>\"\n        │\n        ▼\n   read_file(\u003Cfile>)\n        │\n        ▼\n  content == desired?\n   ┌────┴────┐\n  YES       NO\n   │         │\n   ▼         ▼\n SKIP     write_file(\u003Cfile>, desired)\n write        │\n   │          ▼\n   └──► Report COMPLETE\n```\n\n---\n\n## Complete Example\n\n```python\nTARGET = \"src\u002Fcomponents\u002FPanel.ts\"\nDESIRED_CONTENT = \"\"\"\u002F\u002F Auto-generated — do not edit\nexport const Panel = () => { ... };\n\"\"\"\n\n# Step 1: read\ncurrent = read_file(path=TARGET)          # tool call\n\n# Step 2: compare\nif current.strip() == DESIRED_CONTENT.strip():\n    print(f\"{TARGET} already matches. Skipping write.\")\n    # → COMPLETE\nelse:\n    # Step 3: write\n    write_file(path=TARGET, content=DESIRED_CONTENT)   # tool call\n    print(f\"{TARGET} updated.\")\n    # → COMPLETE\n```\n\n---\n\n## Edge Cases\n\n| Situation | Handling |\n|---|---|\n| File does not exist | `read_file` raises \u002F returns empty → treat as `\"\"` → always write |\n| Encoding differences | Normalize to UTF-8 before comparison |\n| Line-ending differences (CRLF vs LF) | Normalize with `.replace(\"\\r\\n\", \"\\n\")` before comparing |\n| Byte-exact requirement | Skip `.strip()` and compare verbatim |\n| Large files | Compute SHA-256 hash of both strings for efficiency before full diff |\n\n---\n\n## Key Principle\n\n> **Never assume a file needs to be written. One cheap read prevents an\n> expensive (and potentially harmful) write.**\n\nThis pattern applies to any \"replace entire file\" task regardless of file\ntype: source code, configuration, templates, lock files, generated assets,\netc.",{"name":5,"description":7},"main","ff671ef61ed6587c49bd54c81cdc751c0b99e363","showcase\u002Fskills\u002Fidempotent-file-replace","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace","https:\u002F\u002Fgithub.com\u002FAG064","AG064, E.Mi.\r\n\r\nA person who thinks all the time, yet all they have to think about is nothing. I want to improve the world and work on open source projects","2026-10-08 06:53:48",{"source":8,"skillName":5,"commands":45,"prompt":55,"zipAvailable":56},[46,49,52],{"tool":47,"command":48},"npx","npx skills add AG064\u002FOpenSpace --skill idempotent-file-replace",{"tool":50,"command":51},"bunx","bunx skills add AG064\u002FOpenSpace --skill idempotent-file-replace",{"tool":53,"command":54},"pnpm","pnpm dlx skills add AG064\u002FOpenSpace --skill idempotent-file-replace","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fidempotent-file-replace\u002FSKILL.md",true,[58],{"path":59,"resourceType":60,"resourceTypeDesc":61,"sizeBytes":62,"sourceHtmlUrl":63},".skill_id","OTHER","其他",37,"https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fidempotent-file-replace\u002F.skill_id",[65,87,103,112,140,156],{"skillNo":66,"name":67,"nameZh":6,"description":68,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":69,"categories":70,"keywords":71,"publishedAt":32},"01M6KKMPWA0B5SCCH766F8M5EA","write-special-chars-ts-check","Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fwrite-special-chars-ts-check\u002FSKILL.md",[],[72,73,29,74,75,76,77,78,79,80,81,82,83,84,23,85,86],"write special chars ts check","check","filters","Existing","type-checking","for","errors","script","output","type","files","only","template","python","typescript",{"skillNo":88,"name":89,"nameZh":6,"description":90,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":93,"publishedAt":32},"01M6KKMJXVBNVEY3TRK055F4A7","write-special-chars-readback","Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of \u002Ftmp helper scripts.","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fwrite-special-chars-readback\u002FSKILL.md",[],[94,95,29,84,96,28,23,85,97,98,99,100,101,77,79,102],"readback","write special chars readback","cleanup","scripts","verification","via","post","helper","with",{"skillNo":104,"name":105,"nameZh":6,"description":106,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":107,"categories":108,"keywords":109,"publishedAt":32},"01M6KKMEGX9M9NC105J0W3Q5B9","write-special-chars-heredoc","Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) by using a Python heredoc script executed via shell.","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fwrite-special-chars-heredoc\u002FSKILL.md",[],[29,110,85,77,99,84,111,23,79],"write special chars heredoc","shell",{"skillNo":113,"name":114,"nameZh":6,"description":115,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":116,"categories":117,"keywords":121,"publishedAt":32},"01M6KKM8EJ2AATVHZBNB916WT0","worldmonitor-reference","Curated architecture index of the WorldMonitor open-source dashboard. Reference for extracting patterns — vanilla TypeScript, Panel class hierarchy, service layer, CSS grid, API edge functions, refresh scheduler, and 60+ data source integrations.","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fworldmonitor-reference\u002FSKILL.md",[118],{"categoryNo":119,"categoryName":120,"taxonomyType":20,"taxonomyTypeDesc":21},"01JBSEEDCAT000000000000033","数据分析师",[122,123,124,125,86,126,127,128,129,130,77,131,132,133,134,135,136,137,138,139],"reference","worldmonitor","worldmonitor reference","dashboard","css","architecture","extracting","hierarchy","source","layer","panel","patterns","edge functions","integrations","open","api","data","scheduler",{"skillNo":141,"name":142,"nameZh":6,"description":143,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":144,"categories":145,"keywords":146,"publishedAt":32},"01M6KKM39PHYK627F19GE2B9XN","virtual-list","Implement efficient virtual scrolling for rendering large lists with DOM recycling, chunk-based rendering, and performance optimizations","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fvirtual-list\u002FSKILL.md",[],[147,148,149,150,77,151,102,152,153,154,155],"virtual list","list","virtual","chunk","dom","rendering","recycling","implement","performance",{"skillNo":157,"name":158,"nameZh":6,"description":159,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":160,"categories":161,"keywords":162,"publishedAt":32},"01M6KKKY7T2P61Y0JH54CDEJKQ","verify-directory-structure","Diagnose and resolve file access issues by first verifying the directory structure.","https:\u002F\u002Fgithub.com\u002FAG064\u002FOpenSpace\u002Fblob\u002Fmain\u002Fshowcase\u002Fskills\u002Fverify-directory-structure\u002FSKILL.md",[],[163,164,165,166,167,168,23,169,170,171],"directory","verify","structure","verify directory structure","access","first","issues","diagnose","file access"]