{"record":{"id":"01ebca6e281e14de","repo":"xai-org/grok-build","slug":"refusing-to-delete-pre-existing-snapshot-its-m","errorCode":null,"errorMessage":"refusing to delete pre-existing snapshot {}: its metadata targets a different worktree than {}","messagePattern":"refusing to delete pre-existing snapshot (.+?): its metadata targets a different worktree than (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/btrfs/snapshot.rs","lineNumber":161,"sourceCode":"            bail!(\n                \"refusing to delete pre-existing snapshot {}: outside grok-managed \\\n                 btrfs storage\",\n                snapshot_path.display()\n            );\n        } else {\n            match snapshot_meta_state(&snapshot_path, dest) {\n                // Recreate a stale snapshot for this worktree, or reclaim a\n                // crashed-creation orphan whose metadata was never written; the\n                // is_safe check above keeps the delete inside managed storage.\n                SnapshotMetaState::Matches | SnapshotMetaState::Absent => {\n                    tracing::warn!(\n                        snapshot_path = %snapshot_path.display(),\n                        \"snapshot path already exists for this worktree, recreating\"\n                    );\n                    delete_snapshot(&snapshot_path)?;\n                }\n                // Metadata proves a different session owns it — never delete.\n                SnapshotMetaState::Mismatch => bail!(\n                    \"refusing to delete pre-existing snapshot {}: its metadata targets a \\\n                     different worktree than {}\",\n                    snapshot_path.display(),\n                    dest.display()\n                ),\n            }\n        }\n    }\n\n    tracing::info!(\n        source = %snapshot_source.display(),\n        snapshot = %snapshot_path.display(),\n        dest = %dest.display(),\n        \"creating BTRFS snapshot with symlink\"\n    );\n\n    // Create the snapshot inside the btrfs filesystem\n    create_snapshot(snapshot_source, &snapshot_path)?;","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/btrfs/snapshot.rs#L143-L179","documentation":"When a pre-existing snapshot carries metadata (owner worktree/session), `create_snapshot_with_symlink` checks it with `snapshot_meta_state`. A `Mismatch` means the metadata proves the snapshot belongs to a different worktree/session than `dest`, so the library bails instead of deleting another session's snapshot. This is a deliberate ownership guard against cross-session data loss.","triggerScenarios":"Calling `create_snapshot_with_symlink` with a dest whose snapshot path already exists and whose embedded metadata points at a different worktree id than the current dest.","commonSituations":"Reusing the same snapshot path across two concurrent sessions; dest renamed/moved so its id differs from the recorded owner; cloned or restored environments carrying foreign snapshot metadata; leftover snapshots from deleted sessions.","solutions":["Read the error: it names the conflicting snapshot path and the intended dest; confirm which session truly owns the snapshot.","Choose a unique dest/snapshot path per session so paths don't collide across sessions.","If the foreign snapshot is stale and confirmed unowned, delete it manually (`btrfs subvolume delete` or the library's `delete_snapshot` only if you own it) and retry.","Do not try to bypass the check by editing metadata unless you fully control both sessions.","Investigate why two sessions derived the same snapshot path — usually a dest id collision or restored state."],"exampleFix":"// before\n// session B reuses session A's snapshot path\nlet dest = Path::new(\"/srv/wt/shared-id\"); // meta says owned by session A\ncreate_snapshot_with_symlink(&src, dest)?; // refuses\n// after\nlet dest = Path::new(\"/srv/wt/session-b-unique-id\"); // distinct id per session\ncreate_snapshot_with_symlink(&src, dest)?;\n","handlingStrategy":"try-catch","validationCode":"use std::path::Path;\nfn no_foreign_snapshot(dest: &Path, snapshot_path: &Path) -> bool {\n    !snapshot_path.exists()\n        || match crate::btrfs::snapshot::snapshot_meta_state_public(snapshot_path, dest) {\n            Ok(crate::btrfs::snapshot::MetaState::Match | crate::btrfs::snapshot::MetaState::None) => true,\n            _ => false,\n        }\n}","typeGuard":null,"tryCatchPattern":"match create_snapshot_with_symlink(&src, &dest) {\n    Err(e) if e.to_string().contains(\"targets a different worktree\") => {\n        eprintln!(\"snapshot owned by another session — pick a new dest or clean up the old session: {e}\");\n    }\n    r => r?,\n}","preventionTips":["Guarantee one dest id per session — never reuse snapshot paths across sessions","Track session lifecycle and delete snapshots when a session ends","On restore/clone of environments, strip foreign snapshot metadata","Log dest ids so collisions between concurrent sessions are easy to spot"],"tags":["btrfs","ownership","safety-guard","session-conflict"],"backgroundTag":"snapshot-owned-by-other-session","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}