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>
87 lines
4.9 KiB
C#
87 lines
4.9 KiB
C#
using Anubis.Core;
|
|
using Anubis.Data;
|
|
using Anubis.Supervision;
|
|
using Anubis.Trading;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>The report of the endurance run (SPEC 17, phase 6): regular when the bot kept working, and it says what was not.</summary>
|
|
[TestClass]
|
|
public sealed class SoakReportTests
|
|
{
|
|
private static readonly DateTimeOffset From = new(2026, 11, 11, 18, 0, 0, TimeSpan.Zero);
|
|
private static readonly DateTimeOffset To = From.AddHours(49);
|
|
|
|
[TestMethod]
|
|
public void TheDailyCryptoClosesAreCountedInTheWindow()
|
|
{
|
|
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 + 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)));
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Integration)]
|
|
public async Task AQuietRunIsRegularAndTroublesAreNamed()
|
|
{
|
|
await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false);
|
|
var okx = await rig.AccountAsync("okx", "demo").ConfigureAwait(false);
|
|
var reconciliations = new List<Guid>();
|
|
for (var at = From.AddMinutes(1); at <= To; at += TimeSpan.FromMinutes(5))
|
|
{
|
|
reconciliations.Add(await ReconciledAsync(rig, okx.AccountId, at, ReconcileOutcomes.Clean).ConfigureAwait(false));
|
|
}
|
|
|
|
foreach (var day in new[] { 12, 13 })
|
|
{
|
|
var at = new DateTimeOffset(2026, 11, day, 0, 5, 0, TimeSpan.Zero);
|
|
await rig.Db.WriteAsync(s => s.ExecuteAsync(
|
|
"INSERT INTO engine_runs (id, strategy, run_key, decision_day, decision_id, mode, state, proposed, created_at) VALUES (@id, @s, @k, @d, @dec, @m, NULL, NULL, @at)",
|
|
CancellationToken.None, ("id", Guid.NewGuid()), ("s", StrategyNames.CryptoTrend), ("k", $"{StrategyNames.CryptoTrend}:2026-11-{day - 1}"),
|
|
("d", $"2026-11-{day - 1}"), ("dec", Guid.NewGuid()), ("m", DecisionModes.Observation), ("at", at))).ConfigureAwait(false);
|
|
}
|
|
|
|
var report = await SoakReporter.BuildAsync(rig.Db, From, To, TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false);
|
|
Assert.IsTrue(report.Passed, report.Markdown(Zones.Rome));
|
|
var text = report.Markdown(Zones.Rome);
|
|
Assert.Contains("**Esito: regolare.**", text);
|
|
Assert.Contains("Decisioni di crypto_trend: 2 su 2 chiusure giornaliere.", text);
|
|
Assert.Contains("crypto_trend 2 (in osservazione)", text);
|
|
Assert.Contains("Ordini: nessuno.", text);
|
|
|
|
// Half an hour of silence, an unexplained difference at the end and a guard intervention: each one is named.
|
|
await rig.Db.WriteAsync(async s =>
|
|
{
|
|
foreach (var id in reconciliations.Skip(100).Take(6))
|
|
{
|
|
await s.ExecuteAsync("DELETE FROM reconciliations WHERE id = @id", CancellationToken.None, ("id", id)).ConfigureAwait(false);
|
|
}
|
|
|
|
rig.Clock.Set(From.AddHours(20));
|
|
await Diary.WriteAsync(s, rig.Clock, DiaryKinds.System, "guard_intervention", "La guardia è intervenuta.", null, CancellationToken.None).ConfigureAwait(false);
|
|
}).ConfigureAwait(false);
|
|
await ReconciledAsync(rig, okx.AccountId, To, ReconcileOutcomes.Different).ConfigureAwait(false);
|
|
|
|
report = await SoakReporter.BuildAsync(rig.Db, From, To, TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false);
|
|
Assert.IsFalse(report.Passed);
|
|
var failed = report.Checks.Where(c => !c.Ok).Select(c => c.Text).ToList();
|
|
Assert.HasCount(4, failed, string.Join(" | ", failed));
|
|
Assert.IsTrue(failed.Any(f => f.StartsWith("Silenzi più lunghi di 15 minuti: dal 12/11/2026 03:16 per 35 minuti", StringComparison.Ordinal)), string.Join(" | ", failed));
|
|
Assert.Contains("Riconciliazioni con differenze inspiegate: 1.", failed);
|
|
Assert.Contains("Interventi della guardia: 1.", failed);
|
|
Assert.IsTrue(failed.Any(f => f.Contains("con differenze", StringComparison.Ordinal)));
|
|
Assert.Contains("**Esito: da guardare.**", report.Markdown(Zones.Rome));
|
|
}
|
|
|
|
private static async Task<Guid> ReconciledAsync(TradingRig rig, Guid account, DateTimeOffset at, string outcome)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
await rig.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO reconciliations (id, account_id, outcome, details, occurred_at, created_at) VALUES (@id, @a, @o, @d, @at, @at)",
|
|
CancellationToken.None, ("id", id), ("a", account), ("o", outcome), ("d", new Anubis.Db.DbJson("{}")), ("at", at))).ConfigureAwait(false);
|
|
return id;
|
|
}
|
|
}
|