Fix the defects found by the Phase 6 review (refs #47 #48 #49 #51 #52 #54)

An independent review of the phase found nine defects. The ones that
could lose money or orders:

- Exits at the close of equity_meanrev are now planned orders written in
  the same transaction that claims the decision, so a restart can neither
  lose them nor send them twice. A sell with a deadline starts aggressive.
- Resume also finds a planned order not yet linked to its execution
  (restart between sending and linking) and closes it before re-planning.
- An account is "ready" only after adopting the guard's stops, checking
  the key, reconciling, resuming and aligning stops. Until then nothing is
  decided on it or sent to it, and the loop retries its startup each
  minute. The guard's note is deleted only when every stop it placed is in
  the ledger.
- Each account's turn is cut after 45 s (60 s for a retried startup), so a
  slow broker no longer silences the heartbeat until the guard kills a
  healthy engine; the beat is also withheld when the scheduler has been
  silent for 30 minutes.

Smaller ones: a stock decision made after the open is marked late; the
Telegram loops survive unexpected errors; a long absence is shown on the
Panoramica until "Ho visto" (SPEC 5.6); plain-http heartbeats are only
accepted to home-network addresses; the soak report gives a daily close
30 minutes to be decided before expecting it.

ADR 0012 point 12 records these choices; #51 and the two screen criteria
are ticked in the roadmap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 21:10:28 +01:00
1 parent 3d174931e9
commit 2185a0b36b
16 files changed
+397 -90

No files matched your search

+22
View File
@@ -71,6 +71,8 @@ public sealed class LiveEngineTests
var last = LiveFrom.AddDays(13);
await using var live = await LiveRig.CreateAsync(LiveFrom, last).ConfigureAwait(false);
var restarted = false;
var waited = false;
(int Runs, int Sent) before = default;
for (var day = LiveFrom; day <= last; day = day.AddDays(1))
{
var index = day.DayNumber - LiveFrom.DayNumber;
@@ -90,6 +92,25 @@ public sealed class LiveEngineTests
}
,
// The account has not finished its startup (the broker did not answer it): for three minutes nothing is
// decided or sent on it, then the decision is made late.
5 => async minute =>
{
var runs = (await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false)).Count;
if (minute == 0)
{
before = (runs, live.Sent.Count);
}
else if (minute == 3)
{
Assert.AreEqual(before, (runs, live.Sent.Count), "Nothing is decided or sent on an account not started.");
waited = true;
}
live.AccountStarted = minute >= 3;
}
,
// The process dies with a purchase working at the broker and starts again.
11 => async minute =>
{
@@ -118,6 +139,7 @@ public sealed class LiveEngineTests
}
Assert.IsTrue(restarted, "The run must restart halfway through an order.");
Assert.IsTrue(waited, "The run must hold an account not started.");
Assert.AreEqual(1, live.Restarts);
Assert.IsGreaterThan(0, live.Failures, "The faults must have hit the work.");
+7 -1
View File
@@ -36,6 +36,9 @@ internal sealed class LiveRig : IAsyncDisposable
public LiveEngine Engine { get; private set; } = null!;
/// <summary>Whether the account finished its startup (SPEC 9.4); while false the engine leaves it alone.</summary>
public bool AccountStarted { get; set; } = true;
public EquityBook Equity { get; private set; } = null!;
public Reconciler Reconciler { get; private set; } = null!;
@@ -103,7 +106,10 @@ internal sealed class LiveRig : IAsyncDisposable
{
Broker = new FaultyBroker(Paper);
Manager = Rig.Manager(Broker, Okx);
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, Stages, id => id == Okx.AccountId ? Manager : null, Rig.QuoteAsync);
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, Stages, id => id == Okx.AccountId ? Manager : null, Rig.QuoteAsync)
{
AccountReady = _ => AccountStarted,
};
}
/// <summary>
+13
View File
@@ -130,6 +130,19 @@ public sealed class OverviewScreenTests
StringAssert.Contains(diary, "Come sta il sistema");
StringAssert.Contains(diary, "non configurato");
StringAssert.Contains(diary, "Il computer che lo ospita");
// SPEC 5.6: an absence longer than a day stays at the top of the Panoramica until "Ho visto".
var now = server.Host.Clock.UtcNow;
var id = Guid.NewGuid();
await server.Host.Services!.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO absences (id, started_at, ended_at, acknowledged_at, created_at) VALUES (@id, @s, @e, NULL, @e)",
CancellationToken.None, ("id", id), ("s", now.AddDays(-3).AddHours(-2)), ("e", now)));
StringAssert.Contains(await server.TextAsync("/"), "Sono stato spento per 3 giorni e 2 ore, fino a oggi alle");
using (var seen = await server.PostFormAsync("/", $"/azioni/assenza/{id}/vista"))
{
Assert.AreEqual("/", ServerHarness.Location(seen));
}
Assert.DoesNotContain("Sono stato spento", await server.TextAsync("/"));
}
[TestMethod]
+18 -14
View File
@@ -200,23 +200,27 @@ public sealed class ServerTests
ServerHarness.UsePostgres(env, empty);
}
await using var server = await ServerHarness.StartAsync("docker", env, dataDir: data, waitReady: false);
await server.WaitForAsync(HostMode.IdentityProblem);
Assert.AreEqual(IdentityVerdict.EmptyDatabase, server.Host.Status.Identity!.Verdict);
using (var restore = await server.PostFormAsync("/", "/azioni/servizio/ripristina", ("password", ServerHarness.AdminPassword), ("salvataggio", backup.Name)))
// The server stops before its data folder is removed: a running one may still hold the database file.
await using (var server = await ServerHarness.StartAsync("docker", env, dataDir: data, waitReady: false))
{
Assert.AreEqual("/", ServerHarness.Location(restore));
await server.WaitForAsync(HostMode.IdentityProblem);
Assert.AreEqual(IdentityVerdict.EmptyDatabase, server.Host.Status.Identity!.Verdict);
using (var restore = await server.PostFormAsync("/", "/azioni/servizio/ripristina", ("password", ServerHarness.AdminPassword), ("salvataggio", backup.Name)))
{
Assert.AreEqual("/", ServerHarness.Location(restore));
}
await server.WaitForAsync(HostMode.Ready);
// SPEC 15.1: back with "Ferma tutto" on, a new fingerprint, and nobody signed in.
var state = await server.Host.Services!.Settings.GetAsync(SettingsStore.BotState.Key);
Assert.AreEqual("stopped", state.Value);
var row = await Installation.ReadRowAsync(server.Host.Services.Db);
Assert.IsTrue(row!.ProtectedRestart);
Assert.AreEqual(row.Fingerprint, Installation.ReadFile(new DataDirectory(data))!.Fingerprint);
Assert.AreEqual(0, await server.Host.Services.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM sessions")));
}
await server.WaitForAsync(HostMode.Ready);
// SPEC 15.1: back with "Ferma tutto" on, a new fingerprint, and nobody signed in.
var state = await server.Host.Services!.Settings.GetAsync(SettingsStore.BotState.Key);
Assert.AreEqual("stopped", state.Value);
var row = await Installation.ReadRowAsync(server.Host.Services.Db);
Assert.IsTrue(row!.ProtectedRestart);
Assert.AreEqual(row.Fingerprint, Installation.ReadFile(new DataDirectory(data))!.Fingerprint);
Assert.AreEqual(0, await server.Host.Services.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM sessions")));
TempFolder.Delete(data);
}
+2 -1
View File
@@ -17,7 +17,8 @@ public sealed class SoakReportTests
{
Assert.AreEqual(2, SoakReporter.DailyCloses(From, To)); // 12 and 13 November at 00:05 UTC
var close = new DateTimeOffset(2026, 11, 11, 0, 5, 0, TimeSpan.Zero);
Assert.AreEqual(1, SoakReporter.DailyCloses(close, close));
Assert.AreEqual(1, SoakReporter.DailyCloses(close, close + SoakReporter.DecisionTime));
Assert.AreEqual(0, SoakReporter.DailyCloses(close, close.AddMinutes(10)), "A decision just started is not expected yet.");
Assert.AreEqual(3, SoakReporter.DailyCloses(close, To));
Assert.AreEqual(0, SoakReporter.DailyCloses(From, From.AddHours(5)));
}
+3
View File
@@ -168,6 +168,9 @@ public sealed class TelegramTests
Assert.IsNull(Heartbeat.Check("https://hc-ping.com/uuid"));
Assert.IsNull(Heartbeat.Check("http://192.168.30.23:8000/ping/x"));
Assert.IsNotNull(Heartbeat.Check("http://hc-ping.com/uuid"));
Assert.IsNotNull(Heartbeat.Check("http://8.8.8.8/ping/x"), "A public address in plain http travels in clear.");
Assert.IsNull(Heartbeat.Check("http://10.0.0.5/ping/x"));
Assert.IsNull(Heartbeat.Check("http://[fd00::5]/ping/x"));
Assert.IsNotNull(Heartbeat.Check("ftp://example.com"));
Assert.IsNotNull(Heartbeat.Check("non è un indirizzo"));
}