feat: stable server id and per-home folder layout (owner review 5 P3.1)

The desktop keyed every server by the address it was reached at, so one
NAS reached by name and by IP, or after an address change, looked like two
servers, and all servers shared one workspace and one cache. Each server
now announces a stable id, and the desktop's folders are sorted into one
home per server, so that every server can have its own archive of
downloaded originals (or5 answers Q1).

- The server makes a Guid v7 once, keeps it as the app setting server.id
  (so it survives restarts and restores, on SQLite and Postgres) and
  announces it as ServerInfo.ServerId. Older clients ignore the field, and
  older servers read as null.
- ServerIdentity keys a server as id:<N> when it announces an id, and by
  its address otherwise. Address keys are kept as aliases.
- HomeFolders names the homes: local, servers/<id> or
  servers/addr-<hash16>. The hash equals the 1.0 proxy cache folder name.
  It also names the per-server originals archive root.
- WorkspaceHomes splits the workspace into one format-1 store per home
  under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace
  stays one store until it is sorted.
- HomeLayoutMigration plans as a dry run and applies through a journal:
  - only same-folder renames, each checked by inventory;
  - a clash keeps the copy saved last and sends the other to the trash;
  - it resumes after a crash at any step;
  - it skips leased projects;
  - it defers an offline workspace or one being moved, while the cache
    part still runs.
- RekeyAsync moves an alias home to the id home: whole folders, or
  project-by-project merges. It also rewrites working states under their
  lease, and moves sync_links (the newer row wins a clash).
- ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner
  understand the split layout. The new layout is opt-in through
  SyncOptions.ServersDirectory, so the app sees no change until it is
  wired.

Refs #66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-15 13:15:11 +01:00
1 parent 58ed737310
commit 4d697a4255
27 files changed
+3082 -38

No files matched your search

+36
View File
@@ -58,6 +58,7 @@ public static class ServerHost
{
TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate),
Database = ServerDatabase.DescribeAsync(database, CancellationToken.None).GetAwaiter().GetResult(),
ServerId = ServerIdAsync(database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(),
};
builder.WebHost.ConfigureKestrel(k =>
@@ -117,6 +118,41 @@ public static class ServerHost
}
}
/// <summary>The app setting that holds the server's stable id (<see cref="ServerInfo.ServerId"/>).</summary>
public const string ServerIdKey = "server.id";
/// <summary>
/// The server's stable id (owner review 5, #66): read from its database's app settings (<see cref="ServerIdKey"/>), or made
/// once (a version 7 UUID) and stored there at the first start. It lives in the database, so a restore keeps it and the
/// server keeps its id whatever address, port or name it is reached by. A value that does not read as an id is kept aside
/// in the log and replaced.
/// </summary>
public static async Task<Guid> ServerIdAsync(ChronosDatabase database, ILogger? logger, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(database);
var json = await database.Settings.GetJsonAsync(Data.Repositories.SettingScope.App, null, ServerIdKey, cancellationToken).ConfigureAwait(false);
if (json is not null)
{
try
{
using var stored = System.Text.Json.JsonDocument.Parse(json);
if (stored.RootElement.ValueKind == System.Text.Json.JsonValueKind.String && Guid.TryParse(stored.RootElement.GetString(), out var known)
&& known != Guid.Empty)
return known;
}
catch (System.Text.Json.JsonException)
{
// Replaced below; the old value is logged.
}
logger?.LogWarning("The server id stored in the database ({Value}) is not an id; a new one is made", json);
}
var id = Guid.CreateVersion7();
await database.Settings.SetJsonAsync(Data.Repositories.SettingScope.App, null, ServerIdKey, "\"" + id.ToString("D") + "\"", cancellationToken)
.ConfigureAwait(false);
logger?.LogInformation("Server id {Id} made", id);
return id;
}
/// <summary>The single composition root of the server's services.</summary>
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive)
{