{"record":{"id":"da49ab6ffd697ac2","repo":"zed-industries/zed","slug":"edit-index-i-has-been-already-used-perhaps-your","errorCode":null,"errorMessage":"Edit index {i} has been already used. Perhaps your spec contains duplicates","messagePattern":"Edit index (.+?) has been already used\\. Perhaps your spec contains duplicates","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/edit_prediction_cli/src/reorder_patch.rs","lineNumber":87,"sourceCode":"    let stats = patch.stats();\n    let total_edits = stats.added + stats.removed;\n    let mut indexes_map = BTreeMap::from_iter((0..total_edits).map(|i| (i, Some(i))));\n\n    for patch_edits_order in edits_order {\n        // Skip duplicated indexes that were already processed\n        let patch_edits_order = patch_edits_order\n            .into_iter()\n            .filter(|&i| indexes_map[&i].is_some()) // skip duplicated indexes\n            .collect::<BTreeSet<_>>();\n\n        if patch_edits_order.is_empty() {\n            continue;\n        }\n\n        let order = patch_edits_order\n            .iter()\n            .map(|&i| {\n                indexes_map[&i].unwrap_or_else(|| panic!(\"Edit index {i} has been already used. Perhaps your spec contains duplicates\"))\n            })\n            .collect::<BTreeSet<_>>();\n\n        let extracted;\n        (extracted, remainder) = extract_edits(&remainder, &order);\n\n        result.hunks.extend(extracted.hunks);\n\n        // Update indexes_map to reflect applied edits. For example:\n        //\n        // Original_index | Removed?  | Mapped_value\n        //       0        | false     | 0\n        //       1        | true      | None\n        //       2        | true      | None\n        //       3        | false     | 1\n\n        for index in patch_edits_order {\n            indexes_map.insert(index, None);","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/edit_prediction_cli/src/reorder_patch.rs#L69-L105","documentation":"reorder_edits remaps original edit indexes as edits are extracted, marking consumed indexes as None in indexes_map. Each round filters out already-used indexes before collecting, so the unwrap guard fires only when an edit index is referenced again after being consumed — the message attributes this to duplicate edit indexes in the spec (edits_order / the example's expected-patch ordering referencing the same edit twice). In practice the preceding filter makes this nearly unreachable; hitting it means the ordering spec hit an edge the filter did not cover or internal state is inconsistent.","triggerScenarios":"An example spec whose edit ordering references the same edit index multiple times across rounds, or indexes outside 0..total_edits corrupting indexes_map state; typically hand-edited or synthesized specs with duplicated edit indexes.","commonSituations":"Hand-written example markdown/json where the same edit appears in two events; a generator bug emitting duplicate indexes in edits_order.","solutions":["Dedupe edit indexes in the spec's ordering so each edit is referenced exactly once","Verify all indexes are within 0..(added+removed) of the patch being reordered","If a minimal spec still triggers it, the guard is firing on an internal invariant bug in reorder_patch — report it with the patch and order spec"],"exampleFix":"// before\nlet order = vec![BTreeSet::from([0, 1]), BTreeSet::from([1, 2])]; // index 1 twice\n\n// after\nlet order = vec![BTreeSet::from([0, 1]), BTreeSet::from([2])];","handlingStrategy":"validation","validationCode":"// ensure each edit index is referenced exactly once across rounds\nlet mut seen = std::collections::BTreeSet::new();\nfor round in &edits_order {\n    for &i in round {\n        assert!(seen.insert(i), \"duplicate edit index {i} in spec\");\n    }\n}","typeGuard":"fn has_unique_edit_indexes(order: &[BTreeSet<usize>]) -> bool {\n    let mut seen = std::collections::BTreeSet::new();\n    order.iter().all(|r| r.iter().all(|i| seen.insert(*i)))\n}","tryCatchPattern":null,"preventionTips":["Validate example specs (unique edit indexes, indexes within patch bounds) before running the pipeline","When generating specs programmatically, assert uniqueness at generation time","Keep the reorder_patch doc example in mind: each index appears in exactly one round"],"tags":["edit-prediction-cli","reorder-patch","duplicate-index","panic"],"backgroundTag":"duplicate-edit-index","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}