Files
Chronos/src/Chronos.Data/Copies/UploadAllRule.cs
T
Alby96andClaude Opus 5.5 6db4c2e80c 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>
2026-11-18 14:01:45 +01:00

85 lines
4.0 KiB
C#

using Chronos.Data.Sync;
namespace Chronos.Data.Copies;
/// <summary>Why Upload all local left a sequence out (owner review 6, Q8).</summary>
public enum UploadSkip
{
/// <summary>In a match nobody answered yet: it may be on the server already.</summary>
SuspectedCopy,
/// <summary>The user kept it apart from a match.</summary>
KeptApart,
/// <summary>It lies in a server's originals archive: it came from a server.</summary>
ServerOriginals,
/// <summary>The server deleted its copy (a 404 for the linked sequence).</summary>
DeletedOnServer,
/// <summary>An Integrate or Merge job holds it, or will.</summary>
Busy,
}
/// <summary>
/// Which local-only sequences Upload all local sends (design §6.4, owner review 6, Q8): suspected copies, kept-apart ones,
/// those the server deleted, those an Integrate or Merge holds and those in any server's originals archive are skipped and
/// flagged, never uploaded as new server sequences. Pure.
/// </summary>
public static class UploadAllRule
{
/// <summary>
/// Splits <paramref name="localOnly"/> into the entries to upload and those skipped with the reason.
/// <paramref name="copies"/> are every pair of the index (any server, and local pairs); <paramref name="serverOriginalsArchives"/>
/// every known server's originals archive (the current one's too); <paramref name="busy"/> the targets of pending or
/// running Integrate and MergeSequences jobs. An entry without a local sequence is neither.
/// </summary>
public static (IReadOnlyList<SyncEntry> Upload, IReadOnlyList<(SyncEntry Entry, UploadSkip Why)> Skipped) Select(
IEnumerable<SyncEntry> localOnly, IReadOnlyCollection<FoundCopy> copies, IReadOnlySet<Guid> serverOriginalsArchives,
IReadOnlySet<Guid> busy)
{
ArgumentNullException.ThrowIfNull(localOnly);
ArgumentNullException.ThrowIfNull(copies);
ArgumentNullException.ThrowIfNull(serverOriginalsArchives);
ArgumentNullException.ThrowIfNull(busy);
var bySequence = new Dictionary<Guid, List<FoundCopy>>();
foreach (var copy in copies)
{
Add(copy.LocalId, copy);
if (copy.OtherId is { } other) Add(other, copy);
}
var upload = new List<SyncEntry>();
var skipped = new List<(SyncEntry, UploadSkip)>();
foreach (var entry in localOnly)
{
if (entry.Local is not { } local) continue;
// A Kept apart answer given before a server sequence was confirmed deleted there (two 404s apart) holds nothing
// back: that sequence is no copy of anything any more, and the local one is this PC's only. A Keep here, answered
// once the deletion was shown, is the user's answer about the deletion itself: it still skips.
var pairs = (bySequence.GetValueOrDefault(local.Id) ?? []).Where(p => !ApartBeforeDeletion(p)).ToList();
UploadSkip? why =
busy.Contains(local.Id) || pairs.Any(p => p.State == CopyState.Integrating) ? UploadSkip.Busy
: pairs.Any(p => p.Kind == CopyKind.LocalServer && p.RemoteGoneHits >= 1) ? UploadSkip.DeletedOnServer
: pairs.Any(p => p.State == CopyState.KeptApart) ? UploadSkip.KeptApart
: pairs.Any(p => p.State == CopyState.Candidate) ? UploadSkip.SuspectedCopy
: serverOriginalsArchives.Contains(local.ArchiveId) ? UploadSkip.ServerOriginals
: null;
if (why is { } reason) skipped.Add((entry, reason));
else upload.Add(entry);
}
return (upload, skipped);
static bool ApartBeforeDeletion(FoundCopy p) =>
p is { Kind: CopyKind.LocalServer, State: CopyState.KeptApart, RemoteGone: true, AnsweredAt: { } answered, RemoteGoneAt: { } gone }
&& answered < gone;
void Add(Guid id, FoundCopy copy)
{
if (!bySequence.TryGetValue(id, out var list)) bySequence[id] = list = [];
list.Add(copy);
}
}
}