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 <noreply@anthropic.com>
This commit is contained in:
1 parent
50e28fd63d
commit
d940fc7c61
6 files changed
+1247
-158
No files matched your search
@@ -11,20 +11,26 @@ namespace Chronos.Data.Projects;
|
||||
/// <summary>The newest version of a project as its record names it: id and number.</summary>
|
||||
public sealed record ProjectHead(Guid Id, int Number);
|
||||
|
||||
/// <summary>A version's file would be over <see cref="ProjectSidecars.MaxVersionBytes"/>, so it could never be read back: nothing was written.</summary>
|
||||
public sealed class ProjectVersionTooLargeException(string message) : IOException(message);
|
||||
|
||||
/// <summary>
|
||||
/// The record of a project in its folder (<c>.chronos/projects/<id>/project.json</c>): what the index row is rebuilt
|
||||
/// from. <see cref="Deleted"/> is set when the project was deleted, before its folder moves to the projects' trash.
|
||||
/// from. <see cref="Deleted"/> is set when the project was deleted, once its folder is in the projects' trash.
|
||||
/// </summary>
|
||||
/// <param name="Mirrored">Main only: the version whose settings a mirror last wrote into the sequence's <c>project.json</c>
|
||||
/// (any index of the archive). An index that meets Main for the first time compares <c>project.json</c> with it, never
|
||||
/// with a copy of the settings it may have read long ago (<see cref="ProjectService"/>). Older records have none.</param>
|
||||
public sealed record ProjectRecord(Guid Id, Guid SequenceId, string Name, bool IsMain, ProjectHead? Head, DateTimeOffset Created, string? CreatedBy,
|
||||
DateTimeOffset Updated, DateTimeOffset? Deleted = null)
|
||||
DateTimeOffset Updated, DateTimeOffset? Deleted = null, ProjectHead? Mirrored = null)
|
||||
{
|
||||
/// <summary>The record of an index row.</summary>
|
||||
public static ProjectRecord Of(SequenceProject project)
|
||||
/// <summary>The record of an index row, naming <paramref name="mirrored"/> as the version mirrored last (Main).</summary>
|
||||
public static ProjectRecord Of(SequenceProject project, ProjectHead? mirrored = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(project);
|
||||
return new ProjectRecord(project.Id, project.SequenceId, project.Name, project.IsMain,
|
||||
project.HeadVersionId is { } head ? new ProjectHead(head, project.HeadNumber) : null, project.CreatedAt, project.CreatedBy, project.UpdatedAt,
|
||||
project.DeletedAt);
|
||||
project.DeletedAt, project.IsMain ? mirrored : null);
|
||||
}
|
||||
|
||||
/// <summary>The index row of this record (nothing mirrored known yet).</summary>
|
||||
@@ -58,20 +64,33 @@ public static class ProjectSidecars
|
||||
public const string VersionFormat = "chronos-project-version";
|
||||
public const int FormatVersion = 1;
|
||||
|
||||
/// <summary>A version file is a few kilobytes; a larger one is not read.</summary>
|
||||
public const int MaxVersionBytes = 8 << 20;
|
||||
/// <summary>
|
||||
/// The one size limit of a version file, for writing and reading alike: a version that could not be read back is never
|
||||
/// written (<see cref="WriteVersion"/>). A version is a few kilobytes; each photo it parks (an exclusion, the hero, a
|
||||
/// keyframe waiting for its photo) takes about 340 bytes of the file, so this holds the positions of about 190,000
|
||||
/// photos, well past the largest sequences.
|
||||
/// </summary>
|
||||
public const int MaxVersionBytes = 64 << 20;
|
||||
|
||||
private static readonly JsonWriterOptions Indented = new() { Indented = true };
|
||||
|
||||
/// <summary>Whether <paramref name="version"/> can be stored: its file within <see cref="MaxVersionBytes"/>.</summary>
|
||||
public static bool Fits(ProjectVersion version) => Encoding.UTF8.GetByteCount(VersionJson(version)) <= MaxVersionBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Writes a version file. A version is immutable: an existing file is left when it holds the same version (same id
|
||||
/// and content hash) and refused otherwise, unless <paramref name="replace"/> (a workspace's own copy of a version,
|
||||
/// stored at its home since).
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">Another version is at that path.</exception>
|
||||
/// <exception cref="ProjectVersionTooLargeException">The file would be over <see cref="MaxVersionBytes"/>: nothing was written.</exception>
|
||||
public static void WriteVersion(string path, ProjectVersion version, bool replace = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(version);
|
||||
var json = VersionJson(version);
|
||||
var bytes = Encoding.UTF8.GetByteCount(json);
|
||||
if (bytes > MaxVersionBytes)
|
||||
throw new ProjectVersionTooLargeException($"Version {version.Number} of project {version.ProjectId} takes {bytes} bytes, over the {MaxVersionBytes} a version may.");
|
||||
if (!replace && File.Exists(path))
|
||||
{
|
||||
ProjectVersion existing;
|
||||
@@ -92,11 +111,11 @@ public static class ProjectSidecars
|
||||
}
|
||||
}
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
AtomicFile.WriteAllText(path, VersionJson(version));
|
||||
AtomicFile.WriteAllText(path, json);
|
||||
}
|
||||
|
||||
/// <summary>Reads a version file and checks its content hash.</summary>
|
||||
/// <exception cref="InvalidDataException">Not a version file, damaged, newer, or its settings do not match their hash.</exception>
|
||||
/// <exception cref="InvalidDataException">Not a version file, damaged, newer, over <see cref="MaxVersionBytes"/>, or its settings do not match their hash.</exception>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
public static ProjectVersion ReadVersion(string path)
|
||||
{
|
||||
@@ -189,6 +208,13 @@ public static class ProjectSidecars
|
||||
if (record.CreatedBy is { } by) w.WriteString("createdBy", by);
|
||||
w.WriteString("updated", record.Updated);
|
||||
if (record.Deleted is { } deleted) w.WriteString("deleted", deleted);
|
||||
if (record.Mirrored is { } mirrored)
|
||||
{
|
||||
w.WriteStartObject("mirrored");
|
||||
w.WriteString("id", mirrored.Id);
|
||||
w.WriteNumber("number", mirrored.Number);
|
||||
w.WriteEndObject();
|
||||
}
|
||||
w.WriteEndObject();
|
||||
}
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
@@ -203,22 +229,26 @@ public static class ProjectSidecars
|
||||
using var document = Parse(AtomicFile.ReadAllText(path), path);
|
||||
var root = document.RootElement;
|
||||
Check(root, RecordFormat, path);
|
||||
ProjectHead? head = null;
|
||||
if (root.TryGetProperty("head", out var h) && h.ValueKind == JsonValueKind.Object && Id(h, "id") is { } headId && Int(h, "number") is { } number and > 0)
|
||||
head = new ProjectHead(headId, number);
|
||||
var name = Text(root, "name");
|
||||
return new ProjectRecord(
|
||||
Id(root, "id") ?? throw new InvalidDataException($"{path} has no project id."),
|
||||
Id(root, "sequenceId") ?? throw new InvalidDataException($"{path} names no sequence."),
|
||||
string.IsNullOrWhiteSpace(name) ? throw new InvalidDataException($"{path} has no name.") : name,
|
||||
root.TryGetProperty("isMain", out var main) && main.ValueKind == JsonValueKind.True,
|
||||
head,
|
||||
Head(root, "head"),
|
||||
Time(root, "created") ?? default,
|
||||
Text(root, "createdBy"),
|
||||
Time(root, "updated") ?? Time(root, "created") ?? default,
|
||||
Time(root, "deleted"));
|
||||
Time(root, "deleted"),
|
||||
Head(root, "mirrored"));
|
||||
}
|
||||
|
||||
/// <summary>A version a record names (its head, the version mirrored): <c>{ "id", "number" }</c>; null when it names none.</summary>
|
||||
private static ProjectHead? Head(JsonElement root, string name) =>
|
||||
root.TryGetProperty(name, out var h) && h.ValueKind == JsonValueKind.Object && Id(h, "id") is { } id && Int(h, "number") is { } number and > 0
|
||||
? new ProjectHead(id, number)
|
||||
: null;
|
||||
|
||||
/// <summary>The version files of a folder by number and id (<c>000001-<id>.json</c>), in number order; other files are not listed.</summary>
|
||||
public static IReadOnlyList<(int Number, Guid Id, string Path)> ListVersions(string directory)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user