fix(workspace): a cancelled copy clears only media no other copy writes; a deletion keeps late versions

Owner review 2, phase 5a, fix round 5.

- A cancelled workspace copy clears its partial media only under the project's
  lease, after checking again that the job is still cancelled, that no other
  copy of the same root and project is queued, running or paused, and that no
  copy in this process is writing. The engine now waits for a paused job's run
  to end before calling the cancel handler, so the clean-up is never skipped.
- The clean-up never recreates a project folder that was removed meanwhile
  (the lease no longer creates the folder). The next list removes a project
  folder that holds only lease files nobody holds, older than the lease.
- A Main this Chronos cannot read (newer or damaged) refuses the open at once
  only when nothing else can be offered. Otherwise Open in workspace lists it
  disabled, with the reason as its tooltip. The refusal's action tooltip is
  now Remove copy.
- The removal plan counts a conflict the same way the sync status does: a
  conflict that a settling replay took past its commit point is not work
  left.
- A healed record is stamped after both the record's time and the index's, so
  the next rename made elsewhere is taken.
- A trash folder holds back the rebuild of a lost record only when it is this
  sequence's deletion: its record names this sequence or cannot be read, or it
  has no record, is within a one-day grace and holds no version of another
  project. What was seen of folders that are gone or have a record again is
  forgotten.
- When a deletion wins, version files pushed into the live folder before the
  deletion arrived are moved beside the deleted record in the trash, never
  deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 15:10:44 +01:00
1 parent 93afe71639
commit 7692ab402e
16 files changed
+750 -61

No files matched your search

+113 -14
View File
@@ -45,8 +45,17 @@ 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>
/// The live folders with no record seen by a reconcile, by project: of which sequence, what they held then, and when
/// (<see cref="Rebuildable"/>). A reconcile of the sequence that no longer meets the folder so forgets it.
/// </summary>
private readonly ConcurrentDictionary<Guid, (Guid Sequence, string Look, long Since)> _unsettled = new();
/// <summary>
/// How long a folder of the projects' trash with no record (a deletion on its way, the record not there yet) holds back the
/// rebuild of a lost record, counted from the newest thing written in it: past that it is a leftover, not a deletion.
/// </summary>
internal static readonly TimeSpan TrashRecordGrace = TimeSpan.FromDays(1);
/// <summary>
/// How long a live folder with no record must look the same before its record is written again from the index (a sync
@@ -62,10 +71,21 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
var seen = new HashSet<Guid>();
var heldBack = new HashSet<Guid>();
var live = new Dictionary<Guid, ProjectRecord>();
// The projects of this sequence whose record in the projects' trash was read, with the newest such folder.
var deletions = new Dictionary<Guid, string>();
var unrecorded = new HashSet<Guid>();
if (!Directory.Exists(folder.ProjectsDirectory)) return new ProjectReconcile(true, seen, heldBack, live);
foreach (var (directory, trashed) in ProjectFolders(folder))
{
cancellationToken.ThrowIfCancellationRequested();
// What is left of the live folder of a project whose deletion was read (the trash is read first): skipped, the
// versions it holds beyond the trash's moved there, beside the deleted record (a push made before the deletion
// reached this PC).
if (!trashed && Guid.TryParseExact(Path.GetFileName(directory), "N", out var left) && deletions.TryGetValue(left, out var deletion))
{
if (writable) KeepWithDeletion(directory, deletion, left);
continue;
}
ProjectRecord? record = null;
var recordPath = Path.Combine(directory, ArchiveLayout.ProjectFile);
try
@@ -90,8 +110,11 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
}
// 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 (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId)
{
unrecorded.Add(named);
if (Rebuildable(folder, directory, named, sequenceId)) record = await RecordFromIndexAsync(folder, row, recordPath, cancellationToken).ConfigureAwait(false);
}
}
if (record is null)
{
@@ -103,9 +126,11 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
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;
// A sequence folder copied by hand carries the projects of its source, which stay that sequence's. (A live folder
// with a record now is no longer unsettled: forgotten after the loop.)
if (record.SequenceId != sequenceId) continue;
if (trashed) deletions[record.Id] = directory;
if (!seen.Add(record.Id)) continue;
if (trashed && record.Deleted is null) record = record with { Deleted = Directory.GetLastWriteTimeUtc(directory) };
if (record.Deleted is null)
{
@@ -123,6 +148,11 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
if (record.Deleted is null) heldBack.Add(record.Id);
}
}
// A folder with no record this sequence no longer has (it got one, or went): what was seen of it is forgotten.
foreach (var (id, unsettled) in _unsettled)
{
if (unsettled.Sequence == sequenceId && !unrecorded.Contains(id)) _unsettled.TryRemove(id, out _);
}
try
{
await SettleMainAsync(folder, sequenceId, live, heldBack, writable, cancellationToken).ConfigureAwait(false);
@@ -310,18 +340,19 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
/// <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.
/// have its record written again from the index now. Never while the projects' trash holds a folder of it that may be a
/// deletion on its way (<see cref="DeletionOnItsWay"/>), 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)
private bool Rebuildable(SequencePaths folder, string directory, Guid projectId, Guid sequenceId)
{
var prefix = projectId.ToString("N") + "-";
try
{
if (Directory.Exists(folder.ProjectsTrashDirectory)
&& Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Any(d => Path.GetFileName(d).StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
&& Directory.EnumerateDirectories(folder.ProjectsTrashDirectory)
.Any(d => Path.GetFileName(d).StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && DeletionOnItsWay(d, projectId, sequenceId, DateTime.UtcNow)))
{
Log.Info("projects", $"Project {projectId} has a folder in the projects' trash; its record is not written again");
return false;
@@ -334,7 +365,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
_unsettled.TryRemove(projectId, out _);
return true;
}
_unsettled[projectId] = (look, now);
_unsettled[projectId] = (sequenceId, 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;
}
@@ -345,6 +376,74 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
}
}
/// <summary>
/// Whether the folder <paramref name="directory"/> of the projects' trash, named for <paramref name="projectId"/>, may be
/// a deletion of this sequence's project on its way. Its record names the sequence (one of another, a sequence folder
/// copied by hand, is not), or cannot be read (nothing is guessed). Without a record (versions name no sequence) it is
/// one until <see cref="TrashRecordGrace"/> has passed since anything was last written in it, unless a version in it is
/// another project's: a leftover then, which no longer holds a live project back.
/// </summary>
internal static bool DeletionOnItsWay(string directory, Guid projectId, Guid sequenceId, DateTime nowUtc)
{
var recordPath = Path.Combine(directory, ArchiveLayout.ProjectFile);
if (File.Exists(recordPath))
{
try
{
return ProjectSidecars.ReadRecord(recordPath).SequenceId == sequenceId;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
return true;
}
}
var info = new DirectoryInfo(directory);
var newest = info.LastWriteTimeUtc;
foreach (var entry in info.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
{
if (entry.LastWriteTimeUtc > newest) newest = entry.LastWriteTimeUtc;
}
if (nowUtc - newest >= TrashRecordGrace) return false;
foreach (var (_, _, path) in ProjectSidecars.ListVersions(Path.Combine(directory, ArchiveLayout.ProjectVersionsFolder)))
{
try
{
if (ProjectSidecars.ReadVersion(path).ProjectId != projectId) return false;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
// Not read: proves nothing either way.
}
}
return true;
}
/// <summary>
/// The version files the live folder <paramref name="directory"/> of a deleted project holds that its folder in the
/// projects' trash (<paramref name="deletion"/>) lacks, moved there beside the deleted record: a version pushed into the
/// live folder before the deletion reached this PC is kept with the project, never deleted nor left where nothing reads
/// it. A file the trash has under the same name is left as it is.
/// </summary>
private static void KeepWithDeletion(string directory, string deletion, Guid projectId)
{
var target = Path.Combine(deletion, ArchiveLayout.ProjectVersionsFolder);
foreach (var (number, _, path) in ProjectSidecars.ListVersions(Path.Combine(directory, ArchiveLayout.ProjectVersionsFolder)))
{
var to = Path.Combine(target, Path.GetFileName(path));
try
{
if (File.Exists(to) || ProjectSidecars.ReadVersion(path).ProjectId != projectId) continue;
Directory.CreateDirectory(target);
File.Move(path, to);
Log.Warning("projects", $"Version {number} of deleted project {projectId} was in its live folder; it is kept beside its deleted record in {deletion}");
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Warning("projects", $"Version file {path} of deleted project {projectId} could not be moved beside its deleted record; tried again next time", ex);
}
}
}
/// <summary>What a project folder holds now, file by file (path, size, time written), as one hash.</summary>
private static string Look(string directory)
{