fix(projects): never undo a deletion by a record rebuild; keep outside project.json edits

Phase 5a fix round 4, projects engine.

- The reconcile reads the projects' trash before the live folders, so a
  deletion wins over what a sync tool has left of the live folder. A lost
  record is never written again while the trash holds a folder of that
  project; a trashed record that cannot be read holds the project back.
- A lost record is written again from the index only once the folder has
  looked the same, file by file, on two reconciles two minutes apart, so a
  record a sync tool is still bringing (a rename, a newer head) wins.
- IndexAsync's "behind" is the rule the heal uses: the record's head must
  be a version the index holds below its head. A record on another chain
  keeps its name.
- A record written before what the index knows (its Updated time is
  older, e.g. a backup made before a rename) never gives its name, and the
  heal writes the index's name back. A record's Updated time never goes
  back, so a newer rename from a PC whose clock is behind is still taken.
- A mirror never writes project.json over settings written there since
  this index last knew what it held (another PC, an older client): they
  are merged first (theirs win where both changed) or kept as an external
  version, then the result is mirrored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 13:58:13 +01:00
1 parent c5ed31bced
commit 56b8ba3c1d
4 files changed
+449 -45

No files matched your search

+126 -18
View File
@@ -1,4 +1,8 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Model;
@@ -24,8 +28,11 @@ internal sealed record ProjectReconcile(bool Missing, IReadOnlySet<Guid> Seen, I
/// behind on moves forward, a name or deletion recorded since is taken, and a row the index marked deleted is listed again
/// when its live record says it is not. Only a record that says so, live or in the projects' trash, deletes a project:
/// never a folder that is not there (moved for a moment, a backup put back, a sync tool) nor a record that cannot be read,
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>); a record missing from the folder of a
/// live project the index holds is written again from the index when the archive takes writes. A version file written past its
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>). The projects' trash is read before the
/// live folders, so a deletion is never undone by what is left of a live folder. A record missing from the folder of a live
/// project the index holds is written again from the index when the archive takes writes, once the folder has settled and
/// never while the trash holds a folder of that project. A record older than what the index knows (behind its head, or
/// written before it) never gives the index its name. A version file written past its
/// record's head by a push a crash cut short is adopted when it follows the head (its file is complete: versions are
/// written atomically, then the record); any other file past the head, or a second file of a number the record's chain
/// names another for, is moved aside to <c>versions/.stray</c>, never deleted. Runs under the sequence's lease, or without
@@ -38,6 +45,15 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
/// <summary>Versions read at once when a project's sidecars are written again from the index.</summary>
private const int WriteBackPage = 500;
/// <summary>The live folders with no record seen by a reconcile, by project: what they held then, and when (<see cref="Rebuildable"/>).</summary>
private readonly ConcurrentDictionary<Guid, (string Look, long Since)> _unsettled = new();
/// <summary>
/// How long a live folder with no record must look the same before its record is written again from the index (a sync
/// tool bringing the real one has it win meanwhile). Tests stand in a shorter one.
/// </summary>
internal TimeSpan RecordSettle { get; set; } = TimeSpan.FromMinutes(2);
/// <summary>Reconciles the projects of <paramref name="sequenceId"/> in <paramref name="folder"/>; the caller holds the sequence's lease.</summary>
/// <param name="writable">False for a read-only archive: the sidecars are read as they are and only the index is written
/// (a version past its record's head is indexed, not adopted into the record; nothing is moved aside).</param>
@@ -72,18 +88,22 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
Log.Info("projects", $"{directory} holds no project record; it is left as it is");
continue;
}
// A record lost (a sync tool, a disk) of a live project the index holds: written again from the index.
if (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId)
// A record lost (a sync tool, a disk) of a live project the index holds: written again from the index, once the
// folder has settled and no deletion of it is on its way (Rebuildable).
if (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId && Rebuildable(folder, directory, named))
record = await RecordFromIndexAsync(folder, row, recordPath, cancellationToken).ConfigureAwait(false);
}
if (record is null)
{
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);
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main):
// also when the record that cannot be read is in the projects' trash, which may say a project the index has
// live was deleted (the trash is read first, so its live folder is not taken either).
if (seen.Add(named) && (!trashed || await LiveHereAsync(named, sequenceId, cancellationToken).ConfigureAwait(false))) heldBack.Add(named);
continue;
}
}
_unsettled.TryRemove(record.Id, out _);
// A sequence folder copied by hand carries the projects of its source, which stay that sequence's.
if (record.SequenceId != sequenceId || !seen.Add(record.Id)) continue;
if (trashed && record.Deleted is null) record = record with { Deleted = Directory.GetLastWriteTimeUtc(directory) };
@@ -171,7 +191,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
foreach (var loser in claimants.Skip(1))
{
if (await db.Projects.GetAsync(loser.Id, cancellationToken).ConfigureAwait(false) is not { IsMain: false, DeletedAt: null } row) continue;
var record = live[loser.Id] with { IsMain = false, Name = row.Name, Mirrored = null, Updated = DateTimeOffset.UtcNow };
var record = live[loser.Id] with { IsMain = false, Name = row.Name, Mirrored = null, Updated = UpdatedAfter(live[loser.Id].Updated) };
try
{
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(loser.Id), record);
@@ -189,7 +209,8 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
/// The record of a live project the index holds, written again from the index when its folder has none (a sync tool, a
/// disk lost it): the version files the folder lacks first (those there are left as they are), then the record naming
/// the index's head and, for Main, the version this index mirrored. The versions the folder holds past that head are
/// then adopted as after any push a crash cut short. Null when it cannot be written now (tried again next time).
/// then adopted as after any push a crash cut short. Only once <see cref="Rebuildable"/>. Null when it cannot be written
/// now (tried again next time).
/// </summary>
private async Task<ProjectRecord?> RecordFromIndexAsync(SequencePaths folder, SequenceProject row, string path, CancellationToken cancellationToken)
{
@@ -211,6 +232,30 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
}
}
/// <summary>
/// Whether <paramref name="record"/>'s head is one of the versions the index holds below its head (or it names none while
/// the index has one): the record is behind. A record whose head the index does not hold at its number (another chain, a
/// version this Chronos cannot read) is not: it is never written over.
/// </summary>
internal static async Task<bool> BehindAsync(ChronosDatabase db, SequenceProject project, ProjectRecord record, CancellationToken cancellationToken)
{
if (project.HeadVersionId is null) return false;
if (record.Head is not { } head) return true;
return head.Number < project.HeadNumber && await db.Projects.GetVersionAsync(head.Id, cancellationToken).ConfigureAwait(false) is { } version
&& version.ProjectId == project.Id && version.Number == head.Number;
}
/// <summary>
/// The time a record written now from the index says it was updated: now, or just after <paramref name="previous"/> (the
/// time the record it replaces says) when a clock behind the one that wrote it would go back. A record's time never goes
/// back, so the newest write of a project (a rename on another PC) is always seen as newer than the one before.
/// </summary>
internal static DateTimeOffset UpdatedAfter(DateTimeOffset previous)
{
var now = DateTimeOffset.UtcNow;
return now > previous ? now : previous.AddMilliseconds(1);
}
/// <summary>The version files of a project numbered above <paramref name="after"/> written from the index (immutable: one there already is left).</summary>
internal static async Task WriteVersionsAsync(ChronosDatabase db, SequencePaths folder, Guid projectId, int after, CancellationToken cancellationToken)
{
@@ -239,18 +284,77 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
return candidate;
}
/// <summary>The project folders of a sequence: the live ones, then those in the projects' trash (<c>&lt;id&gt;-&lt;time&gt;</c>).</summary>
/// <summary>
/// The project folders of a sequence: those in the projects' trash (<c>&lt;id&gt;-&lt;time&gt;</c>), then the live ones.
/// The trash comes first so a deletion wins over what is left of the project's live folder (a sync tool moving it into
/// the trash file by file, another PC's delete arriving): a deletion is never undone by a reconcile.
/// </summary>
private static IEnumerable<(string Directory, bool Trashed)> ProjectFolders(SequencePaths folder)
{
if (Directory.Exists(folder.ProjectsTrashDirectory))
{
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Order(StringComparer.Ordinal))
{
if (File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))) yield return (directory, true);
}
}
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsDirectory).Order(StringComparer.Ordinal))
{
if (Guid.TryParseExact(Path.GetFileName(directory), "N", out _)) yield return (directory, false);
}
if (!Directory.Exists(folder.ProjectsTrashDirectory)) yield break;
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Order(StringComparer.Ordinal))
}
/// <summary>Whether the index holds project <paramref name="projectId"/> live, of this sequence.</summary>
private async Task<bool> LiveHereAsync(Guid projectId, Guid sequenceId, CancellationToken cancellationToken) =>
await db.Projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false) is { DeletedAt: null } row && row.SequenceId == sequenceId;
/// <summary>
/// Whether the live folder <paramref name="directory"/> of project <paramref name="projectId"/>, which has no record, may
/// have its record written again from the index now. Never while the projects' trash holds a folder of it (a deletion on
/// its way, its record maybe not there yet), and only once the folder has looked the same, file by file, on two
/// reconciles <see cref="RecordSettle"/> apart: a sync tool still bringing the real record (with a rename, a newer head)
/// has it win. Until then the project is held back.
/// </summary>
private bool Rebuildable(SequencePaths folder, string directory, Guid projectId)
{
var prefix = projectId.ToString("N") + "-";
try
{
if (File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))) yield return (directory, true);
if (Directory.Exists(folder.ProjectsTrashDirectory)
&& Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Any(d => Path.GetFileName(d).StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
{
Log.Info("projects", $"Project {projectId} has a folder in the projects' trash; its record is not written again");
return false;
}
var look = Look(directory);
var now = Stopwatch.GetTimestamp();
if (_unsettled.TryGetValue(projectId, out var earlier) && earlier.Look == look)
{
if (Stopwatch.GetElapsedTime(earlier.Since, now) < RecordSettle) return false;
_unsettled.TryRemove(projectId, out _);
return true;
}
_unsettled[projectId] = (look, now);
Log.Info("projects", $"Project {projectId} has no record in its folder; it is written again from the index once the folder settles");
return false;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("projects", $"The folder of project {projectId} cannot be read now", ex);
return false;
}
}
/// <summary>What a project folder holds now, file by file (path, size, time written), as one hash.</summary>
private static string Look(string directory)
{
var text = new StringBuilder();
foreach (var file in new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories).OrderBy(f => f.FullName, StringComparer.Ordinal))
{
text.Append(Path.GetRelativePath(directory, file.FullName)).Append('|').Append(file.Length.ToString(CultureInfo.InvariantCulture)).Append('|')
.Append(file.LastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).Append('\n');
}
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString())));
}
/// <summary>
@@ -296,7 +400,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
SetAside(versions, stray.Path, $"does not follow the head of project {record.Id}");
}
if (!moved) return record;
var rolled = record with { Head = head, Updated = DateTimeOffset.UtcNow };
var rolled = record with { Head = head, Updated = UpdatedAfter(record.Updated) };
if (!writable) return rolled;
try
{
@@ -351,8 +455,10 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
row = project with { HeadVersionId = null, HeadNumber = 0 };
await db.Projects.InsertAsync(row, cancellationToken).ConfigureAwait(false);
}
// A record behind the index (a projects folder put back from an older backup) says what was: its name is not taken.
var behind = row.HeadVersionId is not null && (record.Head?.Number ?? 0) < row.HeadNumber;
// A record behind the index (a projects folder put back from an older backup: its head is a version the index holds
// below its own, or it was written before what the index knows of the project, as a rename made since) says what was:
// its name is not taken, and the heal writes the index's back (ProjectService). A record on another chain is not behind.
var stale = await BehindAsync(db, row, record, cancellationToken).ConfigureAwait(false) || record.Updated < row.UpdatedAt;
await IndexVersionsAsync(record, directory, writable, cancellationToken).ConfigureAwait(false);
var (headId, headNumber) = (row.HeadVersionId, row.HeadNumber);
var reached = true;
@@ -376,7 +482,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
{
updated = updated with { DeletedAt = row.DeletedAt ?? record.Deleted };
}
else if (row.DeletedAt is not null || (row.Name != record.Name && !behind))
else if (row.DeletedAt is not null || (row.Name != record.Name && !stale))
{
var others = (await db.Projects.ListBySequenceAsync(record.SequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
.Where(p => p.Id != record.Id).ToList();
@@ -389,10 +495,12 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
// The record's name (unless it is behind), else (another project has it) the name the index has, else one free.
updated = updated with
{
Name = !behind && FreeName(others, record.Name, record.Id) == record.Name ? record.Name
Name = !stale && FreeName(others, record.Name, record.Id) == record.Name ? record.Name
: FreeName(others, row.Name, record.Id) == row.Name ? row.Name
: FreeName(others, behind ? row.Name : record.Name, record.Id),
: FreeName(others, stale ? row.Name : record.Name, record.Id),
};
// What the index knows of the project is now as new as the record, so an older one put back later is seen as such.
if (!stale && record.Updated > updated.UpdatedAt) updated = updated with { UpdatedAt = record.Updated };
}
if (updated != row) await db.Projects.UpdateAsync(updated, cancellationToken).ConfigureAwait(false);
return reached || record.Deleted is not null || trashed;