fix(copies): close the eleventh verification round of owner review 6

- A merge's Verify stage journals a second version, with the hash it is
  filed as, before it files it. A run that stops between filing it and
  leaving it out of Main (a drive dropping out, project.json held, the
  app closing) now leaves it out on resume, instead of reading it as the
  kept sequence's own photo and keeping both versions in Main for good.
- Upload all local ignores a Kept apart pair whose server sequence is
  confirmed deleted only when the answer came before the first 404. A
  Keep here, answered after the deletion was shown, skips the sequence
  again instead of uploading it and erasing the answer.
- An upload's checkpoint records whether the job joined a server
  sequence this PC already knew. A resumed or retried upload no longer
  takes such a sequence for one it made and overwrites its details.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 14:01:45 +01:00
1 parent e002c583d6
commit 6db4c2e80c
10 files changed
+168 -10

No files matched your search

@@ -394,6 +394,62 @@ public sealed class SyncTransferCopyTests(TestContext context)
Assert.Equal("Comet", (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Title);
}
[Test]
public async Task ARetriedUploadThatJoinedAServerSequenceItKnewStillComparesItsDetails()
{
await using var f = Capable(await SyncFixture.CreateAsync(context));
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Comet", [photos[0], photos[1]]);
var engine = await f.StartEngineAsync();
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
await f.Archive.WaitForAsync(job.Id, JobState.Done);
var first = Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)).RemoteId;
// The link and its pair were forgotten; the server sequence is listed again and renamed there.
var observed = Assert.NotNull(await f.Sync.Links.GetAsync(f.Server, first, f.Ct)) with { LocalId = null };
await f.Sync.Links.UpsertAsync(observed, f.Ct);
await using (var session = await f.Db.Database.OpenAsync(f.Ct))
await session.ExecuteAsync("DELETE FROM found_copies WHERE remote_id = $1", [first], f.Ct);
var server = f.Remote.Sequences[first];
server.Title = "Comet NAS";
server.SettingsVersion++;
server.UpdatedAt = DateTimeOffset.UtcNow;
await f.AddLocalAsync(local.Id, photos[2]);
await f.AddLocalAsync(local.Id, photos[3]);
// One original unreadable for the first attempt: it fails after joining the known sequence.
var damaged = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Single(fr => fr.Sha256 == Sha(photos[3]));
var path = f.Archive.Paths.FromRelative(damaged.RelativePath);
var bytes = await File.ReadAllBytesAsync(path, f.Ct);
var broken = bytes.ToArray();
broken[^3] ^= 0xFF;
await File.WriteAllBytesAsync(path, broken, f.Ct);
var row = new Job
{
Id = Guid.CreateVersion7(),
Kind = JobKind.UploadCommit,
TargetIds = [local.Id],
State = JobState.Running,
NodeId = "t#0",
Created = DateTimeOffset.UtcNow,
Started = DateTimeOffset.UtcNow,
};
await f.Db.Jobs.EnqueueAsync(row, f.Ct);
var jobs = new JobEngine(f.Db, [], new JobEngineOptions { WorkerId = "t" });
var attempt = new JobContext(jobs, row, f.Ct);
await Assert.ThrowsAsync<InvalidDataException>(() => new SyncTransfers(f.Sync).UploadAsync(attempt, f.Remote, local.Id));
Assert.Equal(first, Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)).RemoteId);
// Retried from that attempt's checkpoint, the original readable again.
await File.WriteAllBytesAsync(path, bytes, f.Ct);
var retry = new JobContext(jobs, row with { Checkpoint = attempt.Checkpoint }, f.Ct);
await new SyncTransfers(f.Sync).UploadAsync(retry, f.Remote, local.Id);
// Still joined, not made by this upload: the server's title is not overwritten unseen.
Assert.True(server.Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha)));
Assert.Equal("Comet NAS", server.Title);
Assert.Equal("Comet", (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Title);
}
[Test]
public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing()
{