{"id":"filesystem-read-write-organize","version":"1.0.0","primitive":"code_execution","description":"Read, write, move, and organize files on the local filesystem","registry_refs":["none (stdlib only)"],"tags":["filesystem","pathlib","atomic-write","file-io","stdlib","move","rename"],"solves":["partial writes on crash","retry-safe mkdir","broken os.path string concat","silent data loss"],"auth_required":false,"verified":true,"last_verified":"2026-04-13","next_check":"2026-07-13","eval_result":"null","eval_env":"null","mast":[],"ref":"https://arxiv.org/abs/2503.13657","executable":"# ============================================\n# checklist:     filesystem-read-write-organize\n# version:       1.0.0\n# primitive:     code_execution\n# description:   Read, write, move, and organize files on the local filesystem\n# registry_refs: none (stdlib only)\n# auth_required: false\n# verified:      true\n# last_verified: 2026-04-13\n# next_check:    2026-07-13\n# eval_result:   null\n# eval_env:      null\n#\n# MAST FAILURE MODES ADDRESSED:\n# FM-1.1 Disobey Task Specification    — atomic write pattern prevents partial writes\n# FM-1.3 Step Repetition               — idempotent mkdir (exist_ok=True) safe on retry\n# FM-2.6 Reasoning-Action Mismatch     — pathlib over os.path enforced, no string concat\n# FM-3.2 No or Incomplete Verification — every operation verified immediately after\n# FM-3.3 Incorrect Verification        — content round-trip check, not just existence check\n#\n# tags:   filesystem, pathlib, atomic-write, file-io, stdlib, move, rename\n# solves: partial writes on crash, retry-safe mkdir, broken os.path string concat, silent data loss\n# ref: https://arxiv.org/abs/2503.13657\n#\n# INPUTS:\n#   WRITE_CONTENT       — string, content written atomically (default: \"hello world\")\n#   SECOND_FILE_CONTENT — string, second file content (default: \"agent notes\")\n#\n# OUTPUTS:\n#   atomic_write_verified        — bool, file written via temp+rename pattern\n#   content_round_trip_verified  — bool, content intact after move and rename\n#   move_verified                — bool, source gone and destination exists\n#   rename_verified              — bool, new name exists, old name gone\n#   directory_structure_verified — bool, final layout matches expected\n# ============================================\n\nimport sys\nimport shutil\nimport tempfile\nfrom pathlib import Path\n\n# ----------------------------------------\n# PRE_EXECUTION\n# no registry fetch needed — stdlib only\n# set up isolated temp workspace\n# clean up on any failure\n# ----------------------------------------\n\nworkspace = Path(tempfile.mkdtemp(prefix=\"checklist_fs_\"))\nprint(f\"PRE_EXECUTION: workspace created at {workspace} ✓\")\n\ntry:\n\n    # ----------------------------------------\n    # EXECUTION\n    # FM-2.6: use pathlib — not os.path string concat\n    # FM-1.3: exist_ok=True — safe on retry\n    # FM-1.1: atomic write via temp file + rename\n    # ----------------------------------------\n\n    # 1. create directory structure\n    docs_dir = workspace / \"documents\"\n    archive_dir = workspace / \"archive\"\n    docs_dir.mkdir(exist_ok=True)    # FM-1.3: idempotent\n    archive_dir.mkdir(exist_ok=True)\n\n    print(\"EXECUTION: directories created ✓\")\n\n    # 2. write file — atomic pattern (write to temp, rename)\n    # FM-1.1: prevents partial writes if process dies mid-write\n    target = docs_dir / \"hello.txt\"\n    tmp = target.with_suffix(\".tmp\")\n    tmp.write_text(\"hello world\", encoding=\"utf-8\")\n    tmp.rename(target)\n\n    print(\"EXECUTION: file written atomically ✓\")\n\n    # 3. read file back\n    content = target.read_text(encoding=\"utf-8\")\n\n    # FM-3.3: content check, not just existence\n    assert content == \"hello world\", \\\n        f\"FAIL: expected 'hello world', got '{content}'\"\n\n    print(\"EXECUTION: file read verified ✓\")\n\n    # 4. write a second file\n    second = docs_dir / \"notes.txt\"\n    second.write_text(\"agent notes\", encoding=\"utf-8\")\n\n    # 5. list directory — verify both files present\n    files = list(docs_dir.iterdir())\n    assert len(files) == 2, \\\n        f\"FAIL: expected 2 files in docs_dir, got {len(files)}\"\n\n    print(f\"EXECUTION: directory contains {len(files)} files ✓\")\n\n    # 6. move file to archive\n    archived = archive_dir / target.name\n    shutil.move(str(target), str(archived))\n\n    # FM-3.2: verify move — check source gone AND dest exists\n    assert not target.exists(), \\\n        \"FAIL: source file still exists after move\"\n    assert archived.exists(), \\\n        \"FAIL: destination file does not exist after move\"\n\n    print(\"EXECUTION: file moved to archive ✓\")\n\n    # 7. rename file\n    renamed = archive_dir / \"hello_renamed.txt\"\n    archived.rename(renamed)\n\n    assert renamed.exists(), \\\n        \"FAIL: renamed file does not exist\"\n    assert not archived.exists(), \\\n        \"FAIL: original name still exists after rename\"\n\n    print(\"EXECUTION: file renamed ✓\")\n\n    # ----------------------------------------\n    # POST_EXECUTION\n    # FM-3.2: verify final state completely\n    # FM-3.3: content round-trip on renamed file\n    # ----------------------------------------\n\n    # verify final workspace state\n    assert (workspace / \"documents\").exists(), \\\n        \"FAIL: documents dir missing\"\n    assert (workspace / \"archive\").exists(), \\\n        \"FAIL: archive dir missing\"\n\n    # one file in docs (notes.txt), one in archive (hello_renamed.txt)\n    doc_files = list(docs_dir.iterdir())\n    arc_files = list(archive_dir.iterdir())\n\n    assert len(doc_files) == 1, \\\n        f\"FAIL: expected 1 file in documents, got {len(doc_files)}\"\n    assert len(arc_files) == 1, \\\n        f\"FAIL: expected 1 file in archive, got {len(arc_files)}\"\n\n    # FM-3.3: content round-trip check\n    final_content = renamed.read_text(encoding=\"utf-8\")\n    assert final_content == \"hello world\", \\\n        f\"FAIL: content corrupted after move/rename, got '{final_content}'\"\n\n    print()\n    print(\"POST_EXECUTION: directory structure verified ✓\")\n    print(\"POST_EXECUTION: content round-trip verified ✓\")\n\n    result = {\n        \"status\": \"pass\",\n        \"atomic_write_verified\": True,\n        \"content_round_trip_verified\": True,\n        \"move_verified\": True,\n        \"rename_verified\": True,\n        \"directory_structure_verified\": True,\n    }\n    print(result)\n    print(\"PASS\")\n\nfinally:\n    # always clean up workspace\n    shutil.rmtree(workspace, ignore_errors=True)\n    print(f\"POST_EXECUTION: workspace cleaned up ✓\")\n"}