fix(archive): settle conversions by content and keep their aliases through trash, undo, sync and ingest
Owner review 2, phase 4b, fix round 1 (trash, undo, recovery, sync and ingest). - Replace journal: records a phase (moving, written, undoing) and the name each swapped frame is filed under. Recovery decides "sidecar written" by what the sidecar lists, not by the frames version any writer moves; a failed rollback stays undoing and its failure now stops the writer (as a photo deletion's recovery does). The ingest settles restores and replacements first, so an upload can no longer complete a swap that never happened. - Emptying a Replaced entry re-verifies each replacement where it is now: in the sequence through later conversions (a second conversion no longer strands the first originals), or, when the conversion was deleted, its copy in the trash. The dry run marks such entries "conversion deleted" (fingerprinted, shown on the desktop and the panel), and the confirmed emptying may then erase them instead of keeping them forever. - Undo of a conversion takes exactly the entries its job made (the job id is recorded on the entry), never entries found by time window. - Project positions translated by photo use the sidecar's photo of every frame still there, so positions on a converted frame stay put when another frame went meanwhile. - Sync aliases are equivalence classes: an original converted on both sides is one photo; link-by-content, proxy counting and proxy upload follow aliases; a download into a frame missing here swaps the other encoding into that frame instead of adding a frame. - The ingest answers a converted photo's original (a card or folder imported again, a download) as a duplicate of its conversion and keeps the alias tombstone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 3341a2f845753fc4c5bcba51a3b645f126c95eaf)
This commit is contained in:
1 parent
67a6d51cbe
commit
d0bbd77c92
31 files changed
+1326
-211
No files matched your search
@@ -86,6 +86,28 @@ internal sealed class SyncFixture : IAsyncDisposable
|
||||
null, null), Ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the file of the local frame at <paramref name="index"/> with <paramref name="converted"/>, as a conversion's
|
||||
/// Replace does on this PC (owner review 2, R7): the same frame, a tombstone naming the new content.
|
||||
/// </summary>
|
||||
public async Task ReplaceLocalAsync(Guid sequenceId, int index, byte[] converted)
|
||||
{
|
||||
var sequence = (await Db.Sequences.GetAsync(sequenceId, Ct))!;
|
||||
var frame = Chronos.Core.Archiving.ManifestStore.ReadSequence(Archive.Paths.Sequence(sequence.Folder).ManifestPath).Frames[index];
|
||||
var staging = Path.Combine(Archive.Paths.StagingDirectory, "convert-test");
|
||||
Directory.CreateDirectory(staging);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.jpeg");
|
||||
await File.WriteAllBytesAsync(path, converted, Ct);
|
||||
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(converted));
|
||||
var swap = new FrameSwap(frame.Id, Archive.Paths.ToRelative(path), [], (current, _) => Path.ChangeExtension(current.File, ".jpeg"), current => current with
|
||||
{
|
||||
Sha256 = sha,
|
||||
Size = converted.Length,
|
||||
Source = new Chronos.Core.Archiving.FrameSource { Sha256 = current.Sha256, Format = current.Format, File = current.File, Size = current.Size },
|
||||
});
|
||||
Assert.NotNull(await Archive.Service.FrameTrash.ReplaceAsync(Archive.Archive, sequenceId, null, [swap], null, null, null, null, Ct));
|
||||
}
|
||||
|
||||
/// <summary>An edit on this PC, as the editor makes it (project file and index, settings version + 1).</summary>
|
||||
public async Task<Sequence> EditLocalSettingsAsync(Guid id, string settingsJson)
|
||||
{
|
||||
|
||||
@@ -84,7 +84,8 @@ public sealed class SyncRulesTests
|
||||
|
||||
// A photo replaced by a converted file on either side is the same photo as its replacement, and so is a chain of them.
|
||||
var replaced = new SyncTombstones([], [new RemovedPhotoDto("aa", "a2", "replaced", at), new RemovedPhotoDto("a2", "a3", "replaced", at)]);
|
||||
Assert.Equal("a3", replaced.Canonical("AA"));
|
||||
Assert.Equal(replaced.Canonical("a3"), replaced.Canonical("AA"));
|
||||
Assert.Equal(replaced.Canonical("a3"), replaced.Canonical("a2"));
|
||||
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo("aa"), Photo("bb")], [Photo("a3"), Photo("bb")], replaced));
|
||||
Assert.False(replaced.DeletedRemotely("aa"), "a replaced photo is there as its replacement");
|
||||
var replacedHere = new SyncTombstones([new FrameRemoval(localId, "bb", "b2", RemovalReason.Replaced, at)], null);
|
||||
@@ -97,6 +98,43 @@ public sealed class SyncRulesTests
|
||||
new FrameRemoval(localId, "bb", "b2", RemovalReason.Replaced, at)]).Select(r => r.Reason));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APhotoEachSideConvertedOnItsOwnIsStillOnePhoto()
|
||||
{
|
||||
static FramePhoto Photo(string sha) => new(sha, 100, null);
|
||||
var at = new DateTimeOffset(2026, 10, 27, 10, 0, 0, TimeSpan.Zero);
|
||||
var localId = Guid.CreateVersion7();
|
||||
// This PC converted "oo" to "aa", the server converted its copy to "bb" (another decoder, other bytes).
|
||||
var both = new SyncTombstones([new FrameRemoval(localId, "OO", "aa", RemovalReason.Replaced, at)], [new RemovedPhotoDto("oo", "BB", "replaced", at)]);
|
||||
Assert.Equal(both.Canonical("oo"), both.Canonical("aa"));
|
||||
Assert.Equal(both.Canonical("oo"), both.Canonical("bb"));
|
||||
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo("aa"), Photo("cc")], [Photo("bb"), Photo("cc")], both));
|
||||
// The same whichever side's links come first, and with an undo (a link back) in the chain.
|
||||
var reversed = new SyncTombstones([new FrameRemoval(localId, "bb", "oo", RemovalReason.Replaced, at)], [new RemovedPhotoDto("aa", "oo", "replaced", at)]);
|
||||
Assert.Equal(both.Canonical("aa"), reversed.Canonical("bb"));
|
||||
Assert.Equal("cc", both.Canonical("CC"));
|
||||
// A deletion covers every encoding of the photo: the server deleted its conversion, so this PC's is not sent.
|
||||
var deleted = new SyncTombstones([new FrameRemoval(localId, "oo", "aa", RemovalReason.Replaced, at)],
|
||||
[new RemovedPhotoDto("oo", "bb", "replaced", at), new RemovedPhotoDto("bb", null, "deleted", at)]);
|
||||
Assert.True(deleted.DeletedRemotely("aa"));
|
||||
Assert.False(deleted.DeletedLocally("bb"));
|
||||
Assert.False(both.DeletedRemotely("aa"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AProxyIsMadeFromThePhotoThisPcConverted()
|
||||
{
|
||||
var heif = new ImageDecoder([new NativeHeif()]);
|
||||
var at = new DateTimeOffset(2026, 10, 27, 10, 0, 0, TimeSpan.Zero);
|
||||
// The server holds the HEIC original "oo" and cannot decode it; this PC converted its copy to a JPEG ("dd").
|
||||
List<Frame> local = [Original("dd", 100, ImageFileFormat.Jpeg)];
|
||||
var manifest = new SequenceManifest(Guid.CreateVersion7(), 1, 1, null, [], [Wanted("oo", 500, "IMG_0001.HEIC")], [], new ProxySpec(1920, 90));
|
||||
var tombstones = new SyncTombstones([new FrameRemoval(Local.Id, "oo", "dd", RemovalReason.Replaced, at)], null);
|
||||
|
||||
Assert.Equal(1, SyncRules.ProxiesToSend(manifest, local, heif, null, tombstones));
|
||||
Assert.Equal(0, SyncRules.ProxiesToSend(manifest, local, heif));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ATombstoneBesideAPhotoItsSideStillHoldsHidesNothing()
|
||||
{
|
||||
|
||||
@@ -793,6 +793,147 @@ public sealed class SyncTransferTests(TestContext context)
|
||||
Assert.Empty(f.Remote.Sequences);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APhotoEachSideConvertedIsNeitherSentNorFetchedAgain()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(2);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
// Each side converted its own copy of the first photo, to other bytes (another decoder, another quality).
|
||||
var (mine, theirs) = (f.NewPhotos(1)[0], f.NewPhotos(1)[0]);
|
||||
await f.ReplaceLocalAsync(local.Id, 0, mine);
|
||||
var remote = f.Remote.Add("Comet", [theirs, photos[1]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[0]), Sha(theirs), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var entry = await f.EntryAsync("Comet");
|
||||
Assert.Equal((SyncStatus.InSync, 0, 0), (entry.Status, entry.Link!.MissingRemote, entry.Link.MissingLocal));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
await f.Archive.WaitForAsync((await f.Sync.EnqueueUploadAsync(engine, f.Server, local)).Id, JobState.Done);
|
||||
await f.Archive.WaitForAsync((await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Comet", SyncDownloadMode.Originals, null)).Id,
|
||||
JobState.Done);
|
||||
Assert.SequenceEqual([Sha(theirs), Sha(photos[1])], remote.Frames.Select(r => r.Sha256));
|
||||
Assert.SequenceEqual([Sha(mine), Sha(photos[1])], (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Select(r => r.Sha256!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CopiesConvertedOnOneSideBeforeTheyWereLinkedAreLinkedByContent()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
// The Uploader sent the card to the server, which converted it before this PC compared the two.
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var converted = f.NewPhotos(3);
|
||||
var remote = f.Remote.Add("2026-09-28 Canon", converted);
|
||||
foreach (var (original, replacement) in photos.Zip(converted))
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(original), Sha(replacement), "replaced", DateTimeOffset.UtcNow));
|
||||
remote.FirstCapture = local.FirstCapture;
|
||||
remote.CameraModel = local.CameraModel;
|
||||
// And the other way round: this PC converted its copy of another card the server holds as it came.
|
||||
var cards = f.NewPhotos(2);
|
||||
var mine = await f.LocalAsync("Harbour", cards);
|
||||
foreach (var (index, replacement) in f.NewPhotos(2).Index()) await f.ReplaceLocalAsync(mine.Id, index, replacement);
|
||||
mine = (await f.Db.Sequences.GetAsync(mine.Id, f.Ct))!;
|
||||
var theirs = f.Remote.Add("2026-09-28 Canon (2)", cards);
|
||||
theirs.FirstCapture = mine.FirstCapture;
|
||||
theirs.CameraModel = mine.CameraModel;
|
||||
|
||||
await f.RefreshAsync();
|
||||
|
||||
var comet = await f.EntryAsync("Comet");
|
||||
Assert.Equal(remote.Id, comet.Link?.RemoteId);
|
||||
Assert.Equal((0, 0), (comet.Link!.MissingRemote, comet.Link.MissingLocal));
|
||||
var harbour = await f.EntryAsync("Harbour");
|
||||
Assert.Equal(theirs.Id, harbour.Link?.RemoteId);
|
||||
Assert.Equal((0, 0), (harbour.Link!.MissingRemote, harbour.Link.MissingLocal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFrameMissingHereTakesBackItsPhotoAsTheServerConvertedIt()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
// The server converted that photo.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
var remote = f.Remote.Add("Harbour", [photos[0], converted, photos[2]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[1]), Sha(converted), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var behind = await f.EntryAsync("Harbour");
|
||||
Assert.Equal((SyncStatus.ServerAhead, 1), (behind.Status, behind.Link!.MissingLocal));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
// The same frame, holding the server's conversion, and no frame beside it.
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.Equal(3, frames.Count);
|
||||
Assert.Equal((lost.Id, Sha(converted)), (frames[1].Id, frames[1].Sha256));
|
||||
Assert.True(frames.All(fr => (fr.Flags & FrameFlags.Missing) == 0), "the frame is no longer missing");
|
||||
Assert.Equal(Sha(converted), Sha(await File.ReadAllBytesAsync(f.Archive.Paths.FromRelative(frames[1].RelativePath), f.Ct)));
|
||||
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[1]), f.Ct), "the original is an alias of its conversion here too");
|
||||
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionMissingHereTakesBackTheOriginalTheServerHolds()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var remote = f.Remote.Add("Harbour", photos);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
// This PC converted the second photo, and that file is gone since.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
await f.ReplaceLocalAsync(local.Id, 1, converted);
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
await f.RefreshAsync();
|
||||
Assert.Equal(1, (await f.EntryAsync("Harbour")).Link!.MissingLocal);
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.Equal(3, frames.Count);
|
||||
Assert.Equal((lost.Id, Sha(photos[1])), (frames[1].Id, frames[1].Sha256));
|
||||
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(converted), f.Ct), "the conversion is an alias of the original");
|
||||
Assert.False(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[1]), f.Ct));
|
||||
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUploadSendsTheProxyOfAPhotoThisPcConverted()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Holy grail", photos);
|
||||
// The server holds the originals and cannot decode them (HEIF, BRIEF §2.3); this PC converted its copy of the first.
|
||||
var remote = f.Remote.Add("Holy grail", photos);
|
||||
remote.Undecodable = true;
|
||||
await f.ReplaceLocalAsync(local.Id, 0, f.NewPhotos(1)[0]);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var ahead = await f.EntryAsync("Holy grail");
|
||||
Assert.Equal((SyncStatus.LocalAhead, 3), (ahead.Status, ahead.Link!.MissingRemote));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
|
||||
var done = await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
Assert.True(f.Remote.SentProxies.Keys.ToHashSet().SetEquals(remote.Frames.Select(r => r.Id)), "a proxy of every frame");
|
||||
Assert.Equal(3, remote.Frames.Count);
|
||||
Assert.Contains(Loc.L("Archive.Log.ProxiesSent", 3, 0), done.LogTail);
|
||||
}
|
||||
|
||||
private static async Task<Render> AddRenderAsync(SyncFixture f, Sequence local, string name, long bytes)
|
||||
{
|
||||
var render = new Render
|
||||
|
||||
Reference in new issue
Block a user