Skip to content

Commit 30f2e83

Browse files
Clearfallcodex
andcommitted
LR-IMP-DUPES: exclude Local browse records from library duplicate matching
Prefer real library entries when importing browsed copies and track promoted hashes across scan and background-import batches. Add synthetic regressions for permanent deletion, restart, trash restoration, and duplicate promotion. Draft validation on the previous base: 59 import tests passed; all 8 cargo xtask ci steps passed with PR storytold#1052 in a temporary validation worktree. Rebased onto main 68c9240, which includes storytold#1052; validation of this base is recorded in the PR. Co-authored-by: Codex <noreply@openai.com>
1 parent 68c9240 commit 30f2e83

3 files changed

Lines changed: 178 additions & 16 deletions

File tree

‎crates/engine/src/import.rs‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ pub struct ScanInput {
549549
probe: Option<crate::media::FileProbe>,
550550
skip: Option<PathBuf>,
551551
/// path → (photo, its summary) for files already in the library.
552-
by_path: HashMap<PathBuf, (PhotoId, ImportCandidate)>,
552+
by_path: HashMap<PathBuf, (PhotoId, ImportCandidate, Option<String>)>,
553553
by_hash: HashMap<String, PhotoId>,
554554
cache: HashMap<String, ProbeInfo>,
555555
}
@@ -585,13 +585,16 @@ impl ScanInput {
585585
height: p.height,
586586
file_size: p.file_size,
587587
captured: p.captured.clone(),
588-
duplicate: Some("path".into()),
588+
duplicate: (!p.local).then(|| "path".into()),
589589
existing: Some(p.id.0),
590590
..Default::default()
591591
};
592-
by_path.insert(PathBuf::from(path), (p.id, c));
592+
by_path.insert(PathBuf::from(path), (p.id, c, p.content_hash.clone()));
593593
}
594-
if let Some(h) = &p.content_hash {
594+
// Browsing another copy does not add its content to My Photos.
595+
if !p.local
596+
&& let Some(h) = &p.content_hash
597+
{
595598
by_hash.insert(h.clone(), p.id);
596599
}
597600
}
@@ -637,9 +640,22 @@ pub fn scan_with(mut input: ScanInput, paths: &[String], progress: &ScanProgress
637640
let mut out = Vec::with_capacity(files.len());
638641
let mut kept = HashMap::new();
639642
for f in files {
640-
if let Some((_, c)) = input.by_path.get(Path::new(&f)) {
643+
if let Some((id, c, hash)) = input.by_path.get(Path::new(&f)) {
641644
let mut c = c.clone();
642645
c.name = Path::new(&f).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| f.clone());
646+
if c.duplicate.is_none()
647+
&& let Some(h) = hash
648+
{
649+
match seen_hash.get(h) {
650+
Some(existing) => {
651+
c.duplicate = Some("content".into());
652+
c.existing = *existing;
653+
}
654+
None => {
655+
seen_hash.insert(h.clone(), Some(id.0));
656+
}
657+
}
658+
}
643659
out.push(c);
644660
continue;
645661
}
@@ -701,8 +717,8 @@ pub struct ImportJob {
701717
mode: ImportMode,
702718
lib_dir: Option<PathBuf>,
703719
copy_root: Option<PathBuf>,
704-
/// path → (photo, a Local browse record?)
705-
by_path: HashMap<String, (PhotoId, bool)>,
720+
/// path → (photo, a Local browse record?, content hash)
721+
by_path: HashMap<String, (PhotoId, bool, Option<String>)>,
706722
/// content hash → the photo that has it (`None`: a file earlier in this import)
707723
by_hash: HashMap<String, Option<PhotoId>>,
708724
probe: Option<crate::media::FileProbe>,
@@ -850,9 +866,11 @@ impl ImportJob {
850866
continue;
851867
}
852868
if let Source::File { path } = &p.source {
853-
by_path.insert(path.clone(), (p.id, p.local));
869+
by_path.insert(path.clone(), (p.id, p.local, p.content_hash.clone()));
854870
}
855-
if let Some(h) = &p.content_hash {
871+
if !p.local
872+
&& let Some(h) = &p.content_hash
873+
{
856874
by_hash.insert(h.clone(), Some(p.id));
857875
}
858876
}
@@ -906,14 +924,26 @@ impl ImportJob {
906924
let mut out = Prepared { scanned: files.len(), items: Vec::new() };
907925
let mut todo = Vec::new();
908926
for f in files {
909-
match self.by_path.get(f.as_str()).copied() {
910-
Some((id, true)) if !opts.local => {
927+
match self.by_path.get(f.as_str()).cloned() {
928+
Some((id, true, hash)) if !opts.local => {
929+
// A library match takes precedence over promoting a browsed copy. Newly
930+
// promoted content counts immediately, including across worker batches.
931+
if let Some(h) = &hash
932+
&& let Some(existing) = self.by_hash.get(h)
933+
{
934+
out.items.push(PreparedItem::Duplicate { path: f, existing: *existing, reason: "content", hash });
935+
self.done.fetch_add(1, Relaxed);
936+
continue;
937+
}
938+
if let Some(h) = &hash {
939+
self.by_hash.insert(h.clone(), Some(id));
940+
}
911941
// a file that was only browsed (Local) joins the library when it is imported for real
912-
self.by_path.insert(f, (id, false));
942+
self.by_path.insert(f, (id, false, hash));
913943
out.items.push(PreparedItem::Promote(id));
914944
self.done.fetch_add(1, Relaxed);
915945
}
916-
Some((id, _)) => {
946+
Some((id, _, _)) => {
917947
out.items.push(PreparedItem::Duplicate { path: f, existing: Some(id), reason: "path", hash: None });
918948
self.done.fetch_add(1, Relaxed);
919949
}
@@ -1289,7 +1319,10 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare
12891319
PreparedItem::Duplicate { path, existing, reason, hash } => {
12901320
let existing = existing.or_else(|| {
12911321
let h = hash?;
1292-
new_hash.get(&h).copied().or_else(|| s.catalog.photos().find(|p| p.content_hash.as_deref() == Some(h.as_str())).map(|p| p.id))
1322+
new_hash
1323+
.get(&h)
1324+
.copied()
1325+
.or_else(|| s.catalog.photos().find(|p| !p.local && p.content_hash.as_deref() == Some(h.as_str())).map(|p| p.id))
12931326
});
12941327
let trashed = existing.filter(|id| s.catalog.photo(*id).is_some_and(|p| p.deleted));
12951328
match trashed {

‎crates/engine/src/tests_import.rs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,134 @@ fn ids(v: &Value, key: &str) -> usize {
3030
v[key].as_array().map(Vec::len).unwrap_or(0)
3131
}
3232

33+
#[test]
34+
fn local_twin_does_not_block_reimport_after_permanent_delete_and_restart() {
35+
use lightcraft_catalog::PhotoId;
36+
for mode in ["add", "copy"] {
37+
let src = temp_dir(&format!("local-twin-src-{mode}"));
38+
let lib = temp_dir(&format!("local-twin-lib-{mode}"));
39+
let original = src.join("original.png");
40+
let twin = src.join("browsed.png");
41+
write_png(&original, 71);
42+
std::fs::copy(&original, &twin).unwrap();
43+
let bytes = std::fs::read(&original).unwrap();
44+
let mut s = Session::new().with_fs();
45+
s.open_library(&lib, false).unwrap();
46+
let r = s.execute("library.import", &json!({"paths": [original], "mode": mode})).unwrap();
47+
let old = r["imported"][0].as_u64().unwrap();
48+
let r = s.execute("library.import", &json!({"paths": [twin], "local": true})).unwrap();
49+
let local = PhotoId(r["imported"][0].as_u64().unwrap());
50+
let before = s.catalog.photo(local).unwrap().clone();
51+
s.execute("photo.delete", &json!({"ids": [old]})).unwrap();
52+
s.execute("library.source", &json!({"kind": "recentlyDeleted"})).unwrap();
53+
s.execute("library.select", &json!({"ids": [old]})).unwrap();
54+
s.execute("photo.deletePermanently", &json!({"ids": [old]})).unwrap();
55+
s.close_library().unwrap();
56+
s.open_library(&lib, false).unwrap();
57+
let scan = s.execute("library.importPreview", &json!({"paths": [original]})).unwrap();
58+
assert_eq!(scan["duplicates"], 0, "a browsed copy is not a library duplicate: {scan}");
59+
let r = s.execute("library.import", &json!({"paths": [original], "mode": mode})).unwrap();
60+
assert_eq!(ids(&r, "imported"), 1, "{r}");
61+
assert_eq!(ids(&r, "restored"), 0);
62+
assert_eq!(ids(&r, "duplicates"), 0);
63+
assert_ne!(r["imported"][0], old);
64+
assert_eq!(s.catalog.photo(local).unwrap(), &before, "leave the other path's browse record alone");
65+
assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1);
66+
let scan = s.execute("library.importPreview", &json!({"paths": [original]})).unwrap();
67+
assert_eq!(scan["duplicates"], 1, "the actual library photo still counts");
68+
assert_eq!(std::fs::read(&original).unwrap(), bytes);
69+
assert_eq!(std::fs::read(&twin).unwrap(), bytes);
70+
s.close_library().unwrap();
71+
let _ = std::fs::remove_dir_all(src);
72+
let _ = std::fs::remove_dir_all(lib);
73+
}
74+
}
75+
76+
#[test]
77+
fn local_twin_import_matches_the_library_photo_before_promoting() {
78+
use lightcraft_catalog::PhotoId;
79+
let src = temp_dir("local-twin-priority");
80+
let original = src.join("original.png");
81+
let twin = src.join("browsed.png");
82+
write_png(&original, 72);
83+
std::fs::copy(&original, &twin).unwrap();
84+
let mut s = Session::new().with_fs();
85+
let r = s.execute("library.import", &json!({"paths": [original]})).unwrap();
86+
let id = r["imported"][0].as_u64().unwrap();
87+
s.execute("photo.rate", &json!({"ids": [id], "rating": 5})).unwrap();
88+
let r = s.execute("library.import", &json!({"paths": [twin], "local": true})).unwrap();
89+
let local = PhotoId(r["imported"][0].as_u64().unwrap());
90+
let scan = s.execute("library.importPreview", &json!({"paths": [twin]})).unwrap();
91+
assert_eq!(scan["duplicates"], 1, "{scan}");
92+
assert_eq!(scan["candidates"][0]["existing"], id);
93+
let r = s.execute("library.import", &json!({"paths": [twin]})).unwrap();
94+
assert_eq!(ids(&r, "imported"), 0);
95+
assert_eq!(r["duplicates"][0]["existing"], id);
96+
s.execute("photo.delete", &json!({"ids": [id]})).unwrap();
97+
let r = s.execute("library.import", &json!({"paths": [twin], "onDeleted": "skip"})).unwrap();
98+
assert_eq!(ids(&r, "imported"), 0, "skip must not promote the local twin: {r}");
99+
assert_eq!(r["duplicates"][0]["existingDeleted"], true);
100+
let r = s.execute("library.import", &json!({"paths": [twin]})).unwrap();
101+
assert_eq!(r["restored"], json!([id]));
102+
assert!(s.catalog.photo(local).unwrap().local);
103+
assert_eq!(s.catalog.photo(PhotoId(id)).unwrap().rating, 5);
104+
let _ = std::fs::remove_dir_all(src);
105+
}
106+
107+
#[test]
108+
fn local_twins_are_deduplicated_when_promoted_in_one_import() {
109+
let src = temp_dir("local-twin-batch");
110+
let a = src.join("a.png");
111+
let b = src.join("b.png");
112+
let c = src.join("c.png");
113+
write_png(&a, 73);
114+
std::fs::copy(&a, &b).unwrap();
115+
std::fs::copy(&a, &c).unwrap();
116+
let mut s = Session::new().with_fs();
117+
let r = s.execute("library.import", &json!({"paths": [a, b], "local": true})).unwrap();
118+
assert_eq!(ids(&r, "imported"), 2, "browsing shows both paths: {r}");
119+
let scan = s.execute("library.importPreview", &json!({"paths": [a, b, c]})).unwrap();
120+
assert_eq!(scan["duplicates"], 2, "only one candidate per content: {scan}");
121+
let r = s.execute("library.import", &json!({"paths": [a, b, c]})).unwrap();
122+
assert_eq!(ids(&r, "imported"), 1, "{r}");
123+
assert_eq!(ids(&r, "duplicates"), 2);
124+
assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1);
125+
assert_eq!(s.catalog.photos().filter(|p| p.local).count(), 1);
126+
s.execute("edit.undo", &json!({})).unwrap();
127+
assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 0);
128+
assert_eq!(s.catalog.photos().filter(|p| p.local).count(), 2);
129+
let _ = std::fs::remove_dir_all(src);
130+
}
131+
132+
#[test]
133+
fn local_twin_promotion_counts_across_background_import_batches() {
134+
use crate::import::{ImportJob, ImportOptions, commit_prepared};
135+
let src = temp_dir("local-twin-worker");
136+
let a = src.join("a.png");
137+
let b = src.join("b.png");
138+
let c = src.join("c.png");
139+
write_png(&a, 74);
140+
std::fs::copy(&a, &b).unwrap();
141+
std::fs::copy(&a, &c).unwrap();
142+
let mut s = Session::new().with_fs();
143+
let r = s.execute("library.import", &json!({"paths": [a, b], "local": true})).unwrap();
144+
let id = r["imported"][0].as_u64().unwrap();
145+
s.execute("photo.rate", &json!({"ids": [id], "rating": 4})).unwrap();
146+
let mut job = ImportJob::new(&mut s, ImportOptions::default()).unwrap();
147+
let cancel = std::sync::atomic::AtomicBool::new(false);
148+
let first = job.prepare(&[a.to_string_lossy().to_string()], &cancel);
149+
let report = commit_prepared(&mut s, &job.opts, job.now(), first).unwrap();
150+
assert_eq!(report.imported, [id], "promote the existing record with its edits");
151+
assert_eq!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().rating, 4);
152+
let next = job.prepare(&[b.to_string_lossy().to_string(), c.to_string_lossy().to_string()], &cancel);
153+
let report = commit_prepared(&mut s, &job.opts, job.now(), next).unwrap();
154+
assert!(report.imported.is_empty());
155+
assert_eq!(report.duplicates.len(), 2);
156+
assert!(report.duplicates.iter().all(|d| d.existing == Some(id)));
157+
assert_eq!(s.catalog.photos().filter(|p| p.in_library()).count(), 1);
158+
let _ = std::fs::remove_dir_all(src);
159+
}
160+
33161
#[test]
34162
fn recursive_import_with_duplicates() {
35163
let src = temp_dir("src");

‎docs/parity-checklist.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lightroom parity checklist
22

3-
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (LR-MERGE-HDR: merges of raws without colour matrices keep their colour (#965); rapid slider entry; LR-PROF-CAMERACOLOR: tone curve from black-and-white camera JPEGs; LR-VIEW-ZOOM, LR-VIEW-COMPARE: Compare and Reference zoom windows; LR-IMP-FORMATS, LR-IMP-CAMERA-COVERAGE: EOS R5 Mark II CR3 decodes; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md; filesystem history retry/Undo/Redo evidence; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
3+
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (Local browse records no longer block import; LR-MERGE-HDR: merges of raws without colour matrices keep their colour (#965); rapid slider entry; LR-PROF-CAMERACOLOR: tone curve from black-and-white camera JPEGs; LR-VIEW-ZOOM, LR-VIEW-COMPARE: Compare and Reference zoom windows; LR-IMP-FORMATS, LR-IMP-CAMERA-COVERAGE: EOS R5 Mark II CR3 decodes; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md; filesystem history retry/Undo/Redo evidence; renamed from `parity.md`; Top gaps moved to gaps.md, shortcut notes to ui-parity.md) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
44
55
The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we
66
have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good
@@ -71,7 +71,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts).
7171
|---|---|---|---|---|---|
7272
| LR-IMP-ADD-DIALOG | Add photos/folders | P0 | ✅ | `cmd:file.addPhotos`, `cmd:library.importPreview`, `cmd:library.import` (`mode`, `album`, `albumName`, `preset`, `keywords`), `crates/ui-egui/src/import.rs` | review dialog (Import Photos): the scanned source (a scanned folder is not added to Local), candidate grid with thumbnails and checkboxes (duplicates by path/content marked and unchecked; with several file types, a toggle per type checks or unchecks all of its files, e.g. the raws without the JPEGs beside them, issue #344) filling a resizable window (issue #337), transfer mode (add in place / copy / move, each explained), album (existing/new), preset, keywords; the folder scan runs on a background thread, shown in the activity stack with ✕ to cancel (a NAS folder no longer freezes the window); the import itself (probing, sidecars, copying / moving files) runs on a worker thread and its batches join the library between frames, shown in the activity stack with ✕ to cancel; one undo step. Drag-and-drop goes through the same background import. File dialogs list the importable extensions (`lightcraft_engine::import::EXTENSIONS`) in any letter case on Linux, where GTK and the desktop portal match globs case-sensitively (`apps/lightcraft/src/dialog_filter.rs`, issue #342; 0.4.0 hid `.JPG` / `.NEF`, issue #705). The folder scan, Browse Folder, drag-and-drop and the sidecar lookup match extensions in any case (`crates/engine/src/tests_import.rs`) |
7373
| LR-IMP-DRAGDROP | Drop files/folders to import | P0 | ✅ | `crates/ui-egui/src/lib.rs` (dropped files → `cmd:library.import`) | dropping onto a specific album not supported |
74-
| LR-IMP-DUPES | Skip duplicates by content | P1 | ✅ | `crates/engine/src/import.rs`, `crates/engine/src/tests_import.rs` | |
74+
| LR-IMP-DUPES | Skip duplicates by content | P1 | ✅ | `crates/engine/src/import.rs`, `crates/engine/src/tests_import.rs` | Only library records (including Recently Deleted) count as existing content; a copy merely browsed in Local does not block importing another path. A browsed file at the same path is offered for import and promoted with its edits, unless a library match or an earlier promotion already has its content. The review and background import batches deduplicate promoted files too. |
7575
| LR-IMP-DEVICE | Import from camera/card | P1 | ✅ | `cmd:library.devices`, `cmd:file.addFromDevice`, `crates/engine/src/devices.rs` | mounted volumes with a DCIM folder (macOS /Volumes, Linux /media and /run/media, Windows drive letters); File → Import from Device → the import review, copying into the library by default; menus serve the last scan and rescan in the background (never blocking a frame; hot-plugs repaint); copies are verified (a new file, synced, checked against the content hash the scan computed — byte compare with the source when there is none, issue #134; a bad copy is removed and reported as failed, never counted as imported; taken names get -1, -2…, never replaced — issue #96, `crates/engine/src/import_move.rs`); no PTP/MTP (cameras that don't mount as a disk) |
7676
| LR-IMP-AUTO | Watched-folder auto import | P2 | ✅ | `cmd:library.autoImport`, `cmd:library.autoImportScan`, `crates/ui-egui/src/panels/settings.rs` | Settings ▸ Import ▸ Auto Import: a watched folder whose new photos are added (in place or copied) once complete, into an optional album; scanned every 3 s |
7777
| LR-IMP-PRESET | Preset on import | P2 | ✅ | `cmd:library.import` (`preset`) | chosen in the import review; one History entry |
@@ -788,6 +788,7 @@ Latin stays upright. Advanced Japanese composition remains open. Regression cove
788788

789789
| Date | Change | Summary |
790790
|---|---|---|
791+
| 2026-10-12 | patch | LR-IMP-DUPES: distinguish Local browsing from library membership in import review and content matching; regression tests cover permanent deletion/restart, restoration, promotion, worker batches and undo. Status and summary counts unchanged. |
791792
| 2026-10-11 | minor | LR-MERGE-HDR: HDR and panorama merges of raws without colour matrices (CR2, NEF, ARW…) write the colour they open with as DNG tags (issue #965); status unchanged |
792793
| 2026-10-12 | minor | LR-META-XMP: Read Metadata from File no longer auto-writes the sidecar it has just read, which added `lc:settings` and hid another app's later `crs:` edits (#934); status unchanged (✅) |
793794
| 2026-10-12 | patch | LR-IMP-CAMERA-COVERAGE: CR2 white level from the active area, not the saturated masked rows above it (issue #1272); status unchanged (🟡) |

0 commit comments

Comments
 (0)