Add the guard that watches the engine and protects positions (refs #48)

The guard (SPEC 9.3, 9.4) is now the container's main process and the parent
of the engine. It listens on a named pipe for the engine's heartbeat and
startup steps, and steps in after two minutes of silence, ten minutes of a
startup that does not move, or an exit without notice. It stops the engine,
cancels our purchases and sales left open on demo accounts, puts back the
stops the positions need from the state file the engine writes every ten
seconds, writes a note, warns the user and restarts the engine without any
injected fault. The third forced restart within an hour writes the STOP
file. The container's stop is passed on to the engine, which closes in order.

At restart the engine adopts the guard's stops before reconciling, cancels
the orders it left open and plans again the exits already decided, so the
rules of SPEC 5.6 apply to them.

The acceptance test blocks the main loop right after a sale leaves: in
process on a demo-like broker, and with real processes in the drill, which
also runs inside the Docker image (tasks ci and the CI workflow). The guard
uses the ASP.NET shared runtime so both programs share /app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 19:36:32 +01:00
1 parent 680cab4bb6
commit d00dfbd32f
26 files changed
+2607 -19

No files matched your search

+49 -3
View File
@@ -1,3 +1,49 @@
// Placeholder: the guard process (SPEC 9.3) arrives in phase 6 (ADR 0003).
await Console.Error.WriteLineAsync("Anubis.Guard non è ancora disponibile: arriva nella Fase 6.").ConfigureAwait(false);
return 2;
using System.Collections;
using System.Runtime.InteropServices;
using Anubis.Core;
using Anubis.Platform;
using Anubis.Supervision;
// The guard (SPEC 9.3, ADR 0012): the main process of the container and the parent of the engine. Its arguments go to
// the engine as they are. "drill" runs the guard drill in a temporary folder instead.
if (args is ["drill", ..])
{
return await GuardDrill.RunAsync(GuardLaunch.Server(), Console.Out, CancellationToken.None).ConfigureAwait(false);
}
var environment = Environment.GetEnvironmentVariables().Cast<DictionaryEntry>()
.ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal);
var paths = new GuardPaths(DataRoot(args, environment));
var logger = new LineLogger(Console.Out.WriteLine);
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(20) };
var clock = SystemClock.Instance;
var guardian = new Guardian(paths, clock, new GuardOptions(),
start => ChildEngine.Start(GuardLaunch.Dotnet(), [GuardLaunch.Server(), .. args], environment, start),
(account, keys) => GuardBrokers.Create(http, clock, account, keys),
(text, keys, ct) => GuardBrokers.AlertAsync(http, logger, keys, text, ct),
logger);
// The stop of the container (SIGTERM) or Ctrl+C: passed on to the engine, which closes in order and is not restarted.
using var shutdown = new CancellationTokenSource();
using var terminate = PosixSignalRegistration.Create(PosixSignal.SIGTERM, Stop);
using var interrupt = PosixSignalRegistration.Create(PosixSignal.SIGINT, Stop);
return await guardian.RunAsync(shutdown.Token).ConfigureAwait(false);
void Stop(PosixSignalContext context)
{
context.Cancel = true;
shutdown.Cancel();
}
// The engine's data folder: --data-dir when given, /data in Docker, the user's folder on desktop (SPEC 15.2, 15.6).
static string DataRoot(string[] args, Dictionary<string, string?> environment)
{
var index = Array.IndexOf(args, "--data-dir");
if (index >= 0 && index + 1 < args.Length)
{
return Path.GetFullPath(args[index + 1]);
}
var platform = Array.IndexOf(args, "--platform") is var p and >= 0 && p + 1 < args.Length ? args[p + 1] : environment.GetValueOrDefault("ANUBIS_PLATFORM");
return platform == "docker" ? "/data" : DataDirectory.ForCurrentUser().Root;
}