From d940fc7c6187cdb07443133f768eb17528c0948a Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 3 Nov 2026 10:13:40 +0100 Subject: [PATCH] fix(projects): sidecars stay the truth when they cannot be read, are missing or newer A project record that could not be read once, or a missing .chronos/projects, marked projects deleted for good and made a second Main; an index meeting Main with no mirror state took its own stale settings column as an outside change and reverted the pushed head; a newer-format or unreadable head made every project call fail or rewrote the record's head over the real history. Now: - Deletion is never inferred from absence or a read failure: an unreadable record holds its project back (listed as indexed, nothing written for it, no second Main), a row marked deleted is revived when its live record says so, and projects the archive has no folder for are written back from the index. - Appends re-read the record under the lease and refuse when its head is not the index's; Main is only made when the sequence has none anywhere. - An index with no mirror state decides from project.json and the record's new mirrored member (older records: the head's ancestry), never from its own column. - A newer-format Main head is listed and pulled but never mirrored, merged or built on; pushes must parse strictly and fit the one 64 MB version limit (TooLarge otherwise). - Duplicate version numbers and name/Main clashes are contained per project, so a scan or rebuild never stops. - Delete moves the folder first, then writes the deleted record in the trash. - The home's read-back for workspace pruning and Remove local copy reads the version files (IProjectHome.HoldsAsync), not the index. - A read-only archive is read without writing: its projects open from the sidecars and a sequence with no Main gets one in the index only, written back once writable. Co-Authored-By: Claude Opus 5.5 --- src/Chronos.App/Workspace/WorkspaceHost.cs | 12 +- src/Chronos.Data/Projects/IProjectHome.cs | 27 + src/Chronos.Data/Projects/ProjectIndexer.cs | 318 ++++++++--- src/Chronos.Data/Projects/ProjectService.cs | 488 +++++++++++++++-- src/Chronos.Data/Projects/ProjectSidecars.cs | 58 +- .../Projects/ProjectRecoveryTests.cs | 502 ++++++++++++++++++ 6 files changed, 1247 insertions(+), 158 deletions(-) create mode 100644 tests/Chronos.Data.Tests/Projects/ProjectRecoveryTests.cs diff --git a/src/Chronos.App/Workspace/WorkspaceHost.cs b/src/Chronos.App/Workspace/WorkspaceHost.cs index 68ce5fd3..7290f506 100644 --- a/src/Chronos.App/Workspace/WorkspaceHost.cs +++ b/src/Chronos.App/Workspace/WorkspaceHost.cs @@ -491,10 +491,8 @@ public sealed class WorkspaceHost var removed = 0; foreach (var version in stored.Where(v => v.Id != basis?.VersionId && !kept.Contains(v.Id))) { - if (await home.GetVersionAsync(projectId, version.Id, cancellationToken).ConfigureAwait(false) is not { } atHome - || !string.Equals(atHome.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase) - || !ProjectContent.Verifies(atHome.SettingsJson, atHome.ContentHash)) - continue; + // Read back from where the home keeps it, not from an index of it. + if (!await home.HoldsAsync(projectId, version, cancellationToken).ConfigureAwait(false)) continue; File.Delete(project.VersionPath(version)); removed++; } @@ -528,16 +526,20 @@ public sealed class WorkspaceHost var home = HomeOf(workspace.Home) ?? throw new WorkspaceException(Loc.L("Errors.Workspace.Offline")); ProjectVersion head; ProjectVersion? basis; + bool held; try { head = await home.HeadAsync(projectId, cancellationToken).ConfigureAwait(false); basis = workspace.Base is { } b ? await home.GetVersionAsync(projectId, b.VersionId, cancellationToken).ConfigureAwait(false) : head; + // Read back from where the home keeps them, not from an index of them. + held = basis is not null && await home.HoldsAsync(projectId, head, cancellationToken).ConfigureAwait(false) + && (basis.Id == head.Id || await home.HoldsAsync(projectId, basis, cancellationToken).ConfigureAwait(false)); } catch (Exception ex) when (ex is HomeUnavailableException or ProjectException or DatabaseException) { throw new WorkspaceException(Loc.L("Errors.Workspace.Offline"), ex); } - if (!ProjectContent.Verifies(head.SettingsJson, head.ContentHash) || basis is null || !ProjectContent.Verifies(basis.SettingsJson, basis.ContentHash) + if (!held || !ProjectContent.Verifies(head.SettingsJson, head.ContentHash) || basis is null || !ProjectContent.Verifies(basis.SettingsJson, basis.ContentHash) || (workspace.Base is { } known && !string.Equals(known.ContentHash, basis.ContentHash, StringComparison.OrdinalIgnoreCase))) throw new WorkspaceException(Loc.L("Errors.Workspace.HomeCheck")); } diff --git a/src/Chronos.Data/Projects/IProjectHome.cs b/src/Chronos.Data/Projects/IProjectHome.cs index 15a30f85..098ec3b8 100644 --- a/src/Chronos.Data/Projects/IProjectHome.cs +++ b/src/Chronos.Data/Projects/IProjectHome.cs @@ -33,6 +33,18 @@ public interface IProjectHome /// A version of the project by id; null when the home has none. Task GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken); + /// + /// Whether the home holds durably, read back from where it keeps it and matching its content + /// hash: what a workspace asks before it drops its own copy. A home that keeps no other copy answers from + /// . + /// + async Task HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(version); + return await GetVersionAsync(projectId, version.Id, cancellationToken).ConfigureAwait(false) is { } held + && string.Equals(held.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase) && ProjectContent.Verifies(held.SettingsJson, held.ContentHash); + } + /// The versions numbered above , oldest first, at most . Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken); @@ -91,6 +103,16 @@ public sealed class ArchiveProjectHome(ChronosDatabase db, ProjectService projec return version is not null && version.ProjectId == projectId ? version : null; }); + /// Read back from the version's file in the archive, never from the index's row (). + public Task HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => + Reach(async () => + { + ArgumentNullException.ThrowIfNull(version); + var project = await Own(projectId, cancellationToken).ConfigureAwait(false); + return project.DeletedAt is null && ProjectContent.Verifies(version.SettingsJson, version.ContentHash) + && await projects.StoredAsync(projectId, version, cancellationToken).ConfigureAwait(false); + }); + public Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) => Reach(async () => { @@ -151,5 +173,10 @@ public sealed class ArchiveProjectHome(ChronosDatabase db, ProjectService projec // Another index of the archive moved the sequence: this one finds it at its next scan. throw new HomeUnavailableException(ex.Message, ex); } + catch (ProjectsUnreadableException ex) + { + // A record or a version this Chronos cannot read now (damaged, held open, of a newer Chronos): nothing was written. + throw new HomeUnavailableException(ex.Message, ex); + } } } diff --git a/src/Chronos.Data/Projects/ProjectIndexer.cs b/src/Chronos.Data/Projects/ProjectIndexer.cs index aaf75ab6..84401b38 100644 --- a/src/Chronos.Data/Projects/ProjectIndexer.cs +++ b/src/Chronos.Data/Projects/ProjectIndexer.cs @@ -5,50 +5,97 @@ using Chronos.Core.Model; namespace Chronos.Data.Projects; +/// What a reconcile of a sequence's projects met (). +/// The sequence has no projects folder: nothing was read, and nothing taken for gone. +/// The projects the archive has a folder for, live or in the projects' trash, whether or not its record read. +/// The live projects the index could not be brought in line with: a record that cannot be read (damaged, +/// of a newer Chronos, a read failure), a head whose version this Chronos cannot read, or a row the index refused. Each stays +/// as the index has it, and nothing is written for it until it reads again. +/// The records of the live projects read, by id (their head as moved forward). +internal sealed record ProjectReconcile(bool Missing, IReadOnlySet Seen, IReadOnlySet HeldBack, IReadOnlyDictionary Live) +{ + /// Every live project of the folder was read and indexed: a sequence without a Main may be given one. + public bool Complete => HeldBack.Count == 0; +} + /// /// Brings the index's projects of one sequence in line with their sidecars (owner review 2, R8), which are the truth: a /// project or version the index lacks is added (an index rebuilt, a sequence restored from the trash), a head the index is -/// behind on moves forward, a name, Main flag or deletion recorded since is taken, and a live index row whose folder is gone -/// is marked deleted. 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 is moved aside to versions/.stray, never deleted. Runs under the sequence's lease. +/// 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 +/// 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 +/// writing to the archive at all when it is read-only. /// internal sealed class ProjectIndexer(ChronosDatabase db) { private const string StrayFolder = ".stray"; /// Reconciles the projects of in ; the caller holds the sequence's lease. - public async Task ReconcileAsync(SequencePaths folder, Guid sequenceId, CancellationToken cancellationToken) + /// 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). + public async Task ReconcileAsync(SequencePaths folder, Guid sequenceId, CancellationToken cancellationToken, bool writable = true) { var seen = new HashSet(); - if (Directory.Exists(folder.ProjectsDirectory)) + var heldBack = new HashSet(); + var live = new Dictionary(); + if (!Directory.Exists(folder.ProjectsDirectory)) return new ProjectReconcile(true, seen, heldBack, live); + foreach (var (directory, trashed) in ProjectFolders(folder)) { - foreach (var (directory, trashed) in ProjectFolders(folder)) + cancellationToken.ThrowIfCancellationRequested(); + ProjectRecord record; + try { - cancellationToken.ThrowIfCancellationRequested(); - ProjectRecord record; - try - { - record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile)); - } - catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) - { - Log.Warning("projects", $"The project in {directory} cannot be read", ex); - continue; - } - // 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) }; - if (record.Deleted is null) record = RollForward(folder, record, directory); - await IndexAsync(record, directory, cancellationToken).ConfigureAwait(false); + record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile)); + } + 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 (, or -