Rebuild the state from the brokers after a restore and refuse an older copy

A restore, an import or an adopted database restarts protected: the engine
stays stopped, reads from the broker what the archive does not know (orders,
stops, executions, also from the OKX history up to 89 days), cancels orders
left working, protects what it finds and waits for the user. Strategies with
real money stay exit only until confirmed again. The database of before the
restore, put back by mistake, is refused by its fingerprint.

A failed migration leaves the engine off with one backup and a way to try
again; an archive is checked whole before the database is touched; a changed
secret key no longer crashes anything, and "anubis access reset" gives the
access back to whoever holds the computer. The image runs the db and access
commands without the guard.

refs #69

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 18:52:08 +01:00
1 parent 3da9b00f44
commit 4897594b87
40 files changed
+1871 -90

No files matched your search

+27 -3
View File
@@ -96,6 +96,9 @@ internal sealed partial class AnubisHost(
public int PasswordIterations => overrides?.PasswordIterations ?? Passwords.DefaultIterations;
/// <summary>Why the last migration failed; the error page's "Riprova" clears it.</summary>
internal string? MigrationFailed { get; set; }
/// <summary>Asks the loop to check again now (after the wizard or an error-page choice).</summary>
public void Wake()
{
@@ -286,7 +289,7 @@ internal sealed partial class AnubisHost(
}
catch (SchemaException ex)
{
SetStatus(HostMode.SchemaProblem, ex.Message);
SetStatus(HostMode.SchemaProblem, Redactor.Redact(ex.Message));
return;
}
@@ -396,9 +399,27 @@ internal sealed partial class AnubisHost(
SetStatus(HostMode.Ready, null);
}
private async Task MigrateAsync(Backups backups, CancellationToken cancellationToken)
internal async Task MigrateAsync(Backups backups, CancellationToken cancellationToken)
{
var applied = await Migrations.MigrateAsync(_db!, clock, ct => backups.TakeAsync(_db!, "prima-della-migrazione", ct), cancellationToken).ConfigureAwait(false);
// SPEC 9.4: after a migration that failed the engine stays off. It is not tried again by itself, because every
// attempt would take one more backup; the user tries again from the error page or with a restart.
if (MigrationFailed is { } failed)
{
throw new SchemaException(failed);
}
int applied;
try
{
applied = await Migrations.MigrateAsync(_db!, clock, ct => backups.TakeAsync(_db!, Backups.BeforeMigration, ct), cancellationToken).ConfigureAwait(false);
}
catch (SchemaException ex) when (ex.InnerException is not null)
{
LogMigrationFailed(logger, ex);
MigrationFailed = ex.Message;
throw;
}
if (applied > 0)
{
LogMigrated(logger, applied, Migrations.Latest);
@@ -739,6 +760,9 @@ internal sealed partial class AnubisHost(
[LoggerMessage(Level = LogLevel.Warning, Message = "Demo mode: separate database in {Folder}")]
private static partial void LogDemo(ILogger logger, string folder);
[LoggerMessage(Level = LogLevel.Error, Message = "Migration failed: the engine stays off until the user tries again")]
private static partial void LogMigrationFailed(ILogger logger, Exception exception);
[LoggerMessage(Level = LogLevel.Information, Message = "Applied {Count} migrations, schema version {Version}")]
private static partial void LogMigrated(ILogger logger, int count, int version);