{"record":{"id":"a70067bf2226a757","repo":"xai-org/grok-build","slug":"could-not-reserve-a-unique-kind-artifact","errorCode":null,"errorMessage":"could not reserve a unique {kind} artifact","messagePattern":"could not reserve a unique (.+?) artifact","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-config/src/managed_text/transaction.rs","lineNumber":272,"sourceCode":"            use std::os::unix::fs::OpenOptionsExt as _;\n            options.mode(mode);\n        }\n        #[cfg(not(unix))]\n        let _ = mode;\n        match options.open(&candidate) {\n            Ok(file) => return Ok((candidate, file)),\n            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,\n            Err(source) => {\n                return Err(ManagedConfigError::Write {\n                    path: candidate,\n                    source,\n                });\n            }\n        }\n    }\n    Err(ManagedConfigError::Write {\n        path: target.to_path_buf(),\n        source: std::io::Error::new(\n            std::io::ErrorKind::AlreadyExists,\n            format!(\"could not reserve a unique {kind} artifact\"),\n        ),\n    })\n}\n\nfn write_reserved(\n    path: &Path,\n    file: &mut File,\n    bytes: &[u8],\n    mode: Option<u32>,\n) -> Result<(), ManagedConfigError> {\n    file.write_all(bytes)\n        .and_then(|()| apply_exact_mode(file, mode))\n        .and_then(|()| file.sync_all())\n        .map_err(|source| ManagedConfigError::Write {\n            path: path.to_path_buf(),\n            source,","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-config/src/managed_text/transaction.rs#L254-L290","documentation":"ManagedConfigError::Write wrapping an io::Error of kind AlreadyExists, raised when a transaction cannot reserve a unique artifact filename for a {kind} item. The reserve helper retries candidate paths and gives up when every candidate already exists, indicating pathological name-collision or an exhausted namespace. It surfaces as a write failure on the target path.","triggerScenarios":"Calling the managed-config transaction commit/reserve flow when the generated candidate artifact name collides with existing files on every retry iteration (e.g. thousands of same-named artifacts already in the target directory).","commonSituations":"Runaway accumulation of auto-named artifacts in the config directory; clock/counter sources producing identical names; a read-only or mirrored directory where cleanup never happens.","solutions":["Inspect the target directory and prune stale/duplicate {kind} artifacts so a free name exists","Check the unique-name generation (counter/timestamp) for wrap-around or constant values","Retry the transaction; transient collisions resolve once a unique slot frees","If the file exists but should be overwritten, delete it or use a non-reserving write path"],"exampleFix":"// before\nlet artifact = txn.reserve_unique(\"session\")?; // AlreadyExists if all candidates taken\n// after\nstd::fs::remove_file(target.join(\"session-0001.toml\")).ok(); // prune stale artifact\nlet artifact = txn.reserve_unique(\"session\")?;","handlingStrategy":"retry","validationCode":"let target = &txn.target_dir();\nlet collisions: Vec<_> = std::fs::read_dir(target)?\n    .filter_map(|e| e.ok())\n    .filter(|e| e.file_name().to_string_lossy().starts_with(kind))\n    .collect();\nif collisions.len() > 100 { eprintln!(\"artifact dir saturated: {} candidates\", collisions.len()); }","typeGuard":null,"tryCatchPattern":"match txn.reserve_unique(kind) {\n    Err(ManagedConfigError::Write { source, .. })\n        if source.kind() == std::io::ErrorKind::AlreadyExists =>\n        eprintln!(\"name space exhausted for {kind}; prune artifacts\"),\n    other => other?,\n}","preventionTips":["Prune stale artifacts from the managed config directory periodically","Cap how many same-kind artifacts accumulate","Use monotonic, never-repeating unique name sources","Alert when reserve retries exceed a small threshold"],"tags":["io","filesystem","collision","codegen"],"backgroundTag":"file-already-exists","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}