fix(projects): phase 5a fix round 2 of the projects engine
The sidecars stay the truth of a sequence's projects in the cases round 1's checkers found still open, and no version or setting is lost on the way: - An index-only Main meeting a Main another index made is written back as another project, and Main passes to theirs in one transaction through a free temporary name, so ux_projects_name and ux_projects_main never fail (the SqliteException used to escape ListAsync and leave the index and the sidecars disagreeing). A refusal is logged; the next reconcile settles it. - The reconcile takes Main from the records, in step with the index, when another index passed it on: exactly one Main per sequence, names from the records where free. - A project folder with versions and no record, of a project no index row names (a creation cut before its record), no longer holds the sequence back forever: it is left as it is and indexed once its record arrives. - A record behind the index (a projects folder put back from an older backup) is made whole from the index: its missing versions and the record are written again, at every catch-up and before any append, rename or delete. Only a record ahead of the index or naming another version is refused, with a message that says so. - An index meeting Main walks the chain down to the version the record names as mirrored (a bound, since that record write may lag project.json), so a lagging record never makes an External that reverts a newer push. When the record names a mirrored ancestor, settings written since merge with the unmirrored versions, as for an index that knows its mirror. - The catch-up merge's External + Merge pair is resumable: an External head whose parent is a push no mirror wrote is finished into its Merge version next time, by this index or one rebuilt from the sidecars. - A project deleted at the home has no head there, so a workspace copy of it reads Gone, never In sync; Remove local copy without discard says the project was deleted in the library. - DatabaseExceptions are contained per sequence in the archive scan's project reconcile and in ProjectService reads. Tests: SQLite (PostgreSQL-ready) for each scenario, including a cut between the two catch-up versions, a 40,000-exclusion version through save, push and rebuild, and the App's Remove local copy of a project deleted at home. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8256b72913
commit
a368dc8bf0
10 files changed
+635
-70
No files matched your search
@@ -53,11 +53,21 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("projects", $"The project in {directory} cannot be read; it stays as the index has it", ex);
|
||||
// Its folder names it (<id>, or <id>-<time> in the trash): its row stays as it is, never taken for gone nor written
|
||||
// again, and a live one is held back (it may be Main).
|
||||
// Its folder names it (<id>, or <id>-<time> in the trash).
|
||||
var name = Path.GetFileName(directory);
|
||||
if (Guid.TryParseExact(trashed && name.Length > 32 ? name[..32] : name, "N", out var named) && seen.Add(named) && !trashed) heldBack.Add(named);
|
||||
if (!Guid.TryParseExact(trashed && name.Length > 32 ? name[..32] : name, "N", out var named)) continue;
|
||||
if (!trashed && !File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))
|
||||
&& await db.Projects.GetAsync(named, cancellationToken).ConfigureAwait(false) is null)
|
||||
{
|
||||
// Versions with no record, of a project no index row names: a creation a crash or a lost disk cut short before its
|
||||
// record (versions are written first), or one a sync tool is still bringing. No project of the sequence is
|
||||
// hidden in it, so it holds nothing back; it is left as it is, and indexed once its record is there.
|
||||
Log.Info("projects", $"{directory} holds no project record; it is left as it is");
|
||||
continue;
|
||||
}
|
||||
Log.Warning("projects", $"The project in {directory} cannot be read; it stays as the index has it", ex);
|
||||
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main).
|
||||
if (seen.Add(named) && !trashed) heldBack.Add(named);
|
||||
continue;
|
||||
}
|
||||
// A sequence folder copied by hand carries the projects of its source, which stay that sequence's.
|
||||
@@ -79,9 +89,61 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
|
||||
if (record.Deleted is null) heldBack.Add(record.Id);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
await SettleMainAsync(sequenceId, live, heldBack, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (DatabaseException ex) when (ex.IsUniqueViolation || ex.IsForeignKeyViolation)
|
||||
{
|
||||
Log.Warning("projects", $"The Main of sequence {sequenceId} could not be taken from its records; tried again next time", ex);
|
||||
}
|
||||
return new ProjectReconcile(false, seen, heldBack, live);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores <paramref name="demoted"/> and <paramref name="promoted"/>, Main passing from the first to the second, each
|
||||
/// with its new name, in one transaction: the first goes through a name no project has, so no two live projects of the
|
||||
/// sequence ever hold one name, nor both Main.
|
||||
/// </summary>
|
||||
internal static async Task SwapMainAsync(ChronosDatabase db, SequenceProject demoted, SequenceProject promoted, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var tx = await db.BeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
await tx.Projects.UpdateAsync(demoted with { IsMain = false, Name = "~" + demoted.Id.ToString("N") }, cancellationToken).ConfigureAwait(false);
|
||||
await tx.Projects.UpdateAsync(promoted with { IsMain = true }, cancellationToken).ConfigureAwait(false);
|
||||
await tx.Projects.UpdateAsync(demoted with { IsMain = false }, cancellationToken).ConfigureAwait(false);
|
||||
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The index's Main taken from the records, which say which project is Main (another index of the archive passed it on:
|
||||
/// an index-only Main written back beside the one made into the archive meanwhile): when the index's Main is read and its
|
||||
/// record no longer says it is Main, or the index has none, the oldest project whose record says it is becomes Main, and
|
||||
/// both take their records' names where free. Only records in step with the index count (a record behind it, put back
|
||||
/// from an older backup, says what was): never while the index's Main is held back or its record is not in step, and
|
||||
/// never more than one Main.
|
||||
/// </summary>
|
||||
private async Task SettleMainAsync(Guid sequenceId, IReadOnlyDictionary<Guid, ProjectRecord> live, IReadOnlySet<Guid> heldBack, CancellationToken cancellationToken)
|
||||
{
|
||||
var rows = await db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false);
|
||||
ProjectRecord? InStep(SequenceProject row) =>
|
||||
!heldBack.Contains(row.Id) && live.TryGetValue(row.Id, out var record) && record.Head?.Id == row.HeadVersionId ? record : null;
|
||||
var main = rows.FirstOrDefault(p => p.IsMain);
|
||||
var own = main is null ? null : InStep(main);
|
||||
if (main is not null && own is not { IsMain: false }) return;
|
||||
var claimant = rows.Where(p => !p.IsMain && InStep(p) is { IsMain: true }).OrderBy(p => p.CreatedAt).ThenBy(p => p.Id).FirstOrDefault();
|
||||
if (claimant is null) return;
|
||||
var others = rows.Where(p => p.Id != claimant.Id && p.Id != main?.Id).ToList();
|
||||
var promoted = claimant with { IsMain = true, Name = FreeName(others, live[claimant.Id].Name, claimant.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
Log.Warning("projects", $"The records of sequence {sequenceId} name project {claimant.Id} as Main{(main is null ? string.Empty : $", not {main.Id}")}; the index takes it");
|
||||
if (main is null)
|
||||
{
|
||||
await db.Projects.UpdateAsync(promoted, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
var demoted = main with { IsMain = false, Name = FreeName([.. others, promoted], own!.Name, main.Id), MirroredVersionId = null, MirroredSettingsVersion = null };
|
||||
await SwapMainAsync(db, demoted, promoted, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="name"/>, or "<paramref name="name"/> (2)", "(3)"… (within <see cref="ProjectService.MaxNameLength"/>):
|
||||
/// the first no project of <paramref name="live"/> but <paramref name="projectId"/> has.
|
||||
|
||||
Reference in new issue
Block a user