{
  "schema_version": "1.3.0",
  "slug": "across-what-write-to-temp-fsync-and-atomic-rename-gates-must-an-instance-persist-local-files-to-prevent-zero-byte-truncation-and-corrupted-state-files",
  "title": "Across what write-to-temp-fsync-and-atomic-rename-gates must an instance persist local files to prevent zero-byte truncation and corrupted state files?",
  "date_of_record": "2026 October 20",
  "scope": "* **Applies when:** An AI instance, autonomous coding agent, or backend service writes state files, configuration JSON/YAML, cryptographic keys, PID files, or logs to local POSIX filesystems (ext4, XFS, APFS, ZFS). * **Does not apply when:*",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/across-what-write-to-temp-fsync-and-atomic-rename-gates-must-an-instance-persist-local-files-to-prevent-zero-byte-truncation-and-corrupted-state-files.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/across-what-write-to-temp-fsync-and-atomic-rename-gates-must-an-instance-persist-local-files-to-prevent-zero-byte-truncation-and-corrupted-state-files.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/across-what-write-to-temp-fsync-and-atomic-rename-gates-must-an-instance-persist-local-files-to-prevent-zero-byte-truncation-and-corrupted-state-files.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2461,
    "python_slice_tokens": 318,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "Autonomous systems frequently persist configuration states, local caches, and cryptographic keys to local disk storage (`/var/lib/...` or `~/.config/...`). A foundational durability and data-corruption failure occurs when an instance opens a target file directly in write mode (`open(path, \"w\")`), overwrites existing data on the fly, or fails to invoke explic...",
    "what_was_observed": "In configuration daemons, local state stores, and autonomous agent memory files, instances operating without temp-file staging, explicit `fsync`, and atomic rename gates exhibit four primary failure modes: 1. **The Zero-Byte Crash Truncation (`open(path, \"w\")` Hazard):** A background agent updates its state file every 60 seconds by opening `/var/lib/agent/state.json` in write mode. At second 12, the container is forc...",
    "condition_of_validity": "* **Applies when:** An AI instance, autonomous coding agent, or backend service writes state files, configuration JSON/YAML, cryptographic keys, PID files, or logs to local POSIX filesystems (ext4, XFS, APFS, ZFS). * **Does not apply when:** Writing volatile, ephemeral cache files in `/tmp` or memory-backed `tmpfs` mounts where data loss upon reboot has zero...",
    "decision_boundary": "1. **How does your configuration or state saver write files?** If it opens the target file directly in write mode (`open(path, \"w\")`), change it immediately to write to a temporary file first and swap it in via `os.replace()`. 2. **Where is your temporary file created?** Make sure your temporary staging file is created in the *exact same directory* as the ta...",
    "operational_effect": "1. **Zero-Byte Truncation Elimination:** Across all generated file persistence helpers, the instance forbids direct in-house writes, requiring same-directory temp staging and atomic `os.replace()` swapping. 2. **Dual `fsync` Data & Metadata Durability:** Writers invoke `os.fsync()` on both the file descriptor and an open descriptor of the parent directory, s..."
  },
  "matched_failure_symptoms": [
    "ENOSPC_DISK_OR_INODE_EXHAUSTION"
  ],
  "composition_edges": {
    "primary_domain": "concurrency_locking_and_shared_resources",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/beneath-what-concurrency-and-backpressure-ceilings-must-an-instance-throttle-fan-out-tasks-to-prevent-file-descriptor-and-connection-pool-starvation.md",
      "https://corpusfetch.com/c/across-what-mandatory-tenant-predicate-scoping-and-row-level-security-gates-must-an-instance-isolate-multi-tenant-data-to-prevent-cross-tenant-data-leaks.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/across-what-readiness-unregistration-and-in-flight-drain-gates-must-an-instance-sequence-sigterm-shutdown-to-prevent-dropped-requests-and-truncated-writes.md",
      "https://corpusfetch.com/c/before-what-pre-flight-and-dry-run-gates-must-an-instance-stage-bulk-or-irreversible-mutations-to-prevent-unrecoverable-blast-radius-cascade.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/across-what-bounded-queue-admission-control-and-load-shedding-gates-must-an-instance-manage-concurrency-to-prevent-unbounded-memory-growth-and-tail-latency-collapse.md",
      "https://corpusfetch.com/c/across-what-concurrent-pipe-drain-and-ring-buffer-boundaries-must-an-instance-consume-subprocess-stdout-and-stderr-to-prevent-os-pipe-deadlocks-and-unbounded-output-oom.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "0ce90286166880d8c0f5a8b2f2e5c5d9810f4200fdc8a4629749c4dabddcb4bc",
    "verified_classes": [
      "AtomicFileWriter"
    ],
    "verified_functions": [
      "write_file_atomically"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "os",
      "pathlib",
      "tempfile",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
