From bc87a578309fd7512951f0343d2c6a6f4bbf781e Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 3 Nov 2026 12:56:51 +0100 Subject: [PATCH] fix(projects): one atomic catch-up merge, Main settled from the records, lost records rebuilt The catch-up merge of a push whose mirror a crash cut short with settings written meanwhile was two appends (External, then Merge), resumed by a purely structural test that also fired on ordinary histories (two outside edits in a row, a second index with an older mirror) and reverted the latest edit. It is now one Merge version on the head in which the settings written meanwhile (the settings in force, which older clients wrote) win where both changed; the head's values stay in its version. A merge that adds nothing to the head makes no version, so a cut before its mirror is finished next time by the same index or a rebuilt one without any marker. The structural resume is gone. An index meeting Main with a lagging record merges project.json the same way, so a newer outside edit is no longer reverted by an older push. Versions the catch-up records as mirrored are named in Main's record too when project.json holds them, so records no longer lag behind externals. Duplicate Main claimants (an index that met the sequence before Main's record arrived) are settled from the records: the oldest claimant with a version is Main in every index, the others are ordinary projects, and their records say so when the archive takes writes. A live project folder whose record is missing while the index holds the project gets its record (and missing version files) written again from the index instead of being held back forever. A record behind the index no longer gives the index its old name. MirrorAsync reads the version leniently and skips a newer one, as the other branches do. Co-Authored-By: Claude Opus 5.5 --- src/Chronos.Data/Projects/ProjectIndexer.cs | 153 +++++++++++++----- src/Chronos.Data/Projects/ProjectService.cs | 140 ++++++++-------- src/Chronos.Data/Projects/ProjectSidecars.cs | 7 +- .../Projects/ProjectMergeTests.cs | 122 +++++++++++--- .../Projects/ProjectRecoveryTests.cs | 106 +++++++++++- 5 files changed, 398 insertions(+), 130 deletions(-) diff --git a/src/Chronos.Data/Projects/ProjectIndexer.cs b/src/Chronos.Data/Projects/ProjectIndexer.cs index a32ce0a9..7f0b4277 100644 --- a/src/Chronos.Data/Projects/ProjectIndexer.cs +++ b/src/Chronos.Data/Projects/ProjectIndexer.cs @@ -24,7 +24,8 @@ internal sealed record ProjectReconcile(bool Missing, IReadOnlySet 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 (). A version file written past its +/// whose project stays as the index has it (); 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 /// 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 versions/.stray, never deleted. Runs under the sequence's lease, or without @@ -34,6 +35,9 @@ internal sealed class ProjectIndexer(ChronosDatabase db) { private const string StrayFolder = ".stray"; + /// Versions read at once when a project's sidecars are written again from the index. + private const int WriteBackPage = 500; + /// Reconciles the projects of in ; the caller holds the sequence's lease. /// 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). @@ -46,29 +50,39 @@ internal sealed class ProjectIndexer(ChronosDatabase db) foreach (var (directory, trashed) in ProjectFolders(folder)) { cancellationToken.ThrowIfCancellationRequested(); - ProjectRecord record; + ProjectRecord? record = null; + var recordPath = Path.Combine(directory, ArchiveLayout.ProjectFile); try { - record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile)); + record = ProjectSidecars.ReadRecord(recordPath); } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { // Its folder names it (, or -