Let a late decision wait for its bars; read the soak run's continuity from jobs and absences (refs #49 #53)

Starting the 48-hour run showed two defects.

After an absence, a decision made past 12:00 UTC was written off as
"no data" at the first pulse, because the data step had not yet brought
the bars of the close: the recovered decision was lost. A late decision
now asks for its bars and waits up to 30 minutes for them. The 20-hour
absence test pulses before the bar arrives and fails without the fix.

The soak report measured coverage and silences from reconciliation
rows, but a clean round writes none, so a healthy run would have been
reported as never covered. It now reads when the scheduled jobs first
ran and last finished, the absences written at each restart, the guard
interventions, and each account's status (reconciled within the last
15 minutes and normal). The test no longer fakes a row every 5 minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 21:35:03 +01:00
1 parent 2185a0b36b
commit be6d5fb91f
5 files changed
+109 -63

No files matched your search

+6 -1
View File
@@ -71,8 +71,13 @@ public sealed class AbsenceTests
AbsenceBook.MissedLine(jobs.Skipped[0], Zones.Rome));
Assert.IsNull(await new AbsenceBook(live.Rig.Db, live.Rig.Clock).DeclaredAsync(_none).ConfigureAwait(false));
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
// The engine pulses before the data step has brought yesterday's bar: the late decision waits for it and
// asks for it, instead of being written off as "no data" at the first pulse.
live.Quotes(LiveFrom, b => b.Close);
await live.MinutesAsync(3).ConfigureAwait(false);
Assert.IsEmpty(await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false), "A late decision waits for its bars.");
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT mode FROM engine_runs", r => r.Text("mode")).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { DecisionModes.ExitsOnly }, runs.ToList());
+36 -30
View File
@@ -29,58 +29,64 @@ public sealed class SoakReportTests
{
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 })
// A quiet run: the scheduler at work from the first minute to the end, the account reconciled and normal at the
// end (a clean round writes no reconciliation row), a crypto decision at each daily close.
await rig.Db.WriteAsync(async s =>
{
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);
}
await s.ExecuteAsync("INSERT INTO jobs (id, name, description, last_due_at, last_started_at, last_finished_at, last_outcome, last_error, created_at, updated_at) " +
"VALUES (@id, 'riconciliazione', 'Riconciliazione dei conti', @to, @to, @to, 'ok', NULL, @from, @to)", CancellationToken.None,
("id", Guid.NewGuid()), ("from", From.AddMinutes(1)), ("to", To)).ConfigureAwait(false);
await s.ExecuteAsync("INSERT INTO account_status (id, account_id, mode, mode_since, mode_reason, differences, last_reconciled_at, last_outcome, created_at, updated_at) " +
"VALUES (@id, @a, 'normal', NULL, NULL, @d, @to, 'clean', @from, @to)", CancellationToken.None,
("id", Guid.NewGuid()), ("a", okx.AccountId), ("d", new Anubis.Db.DbJson("{\"Current\":[],\"Excluded\":[]}")), ("from", From), ("to", To)).ConfigureAwait(false);
foreach (var day in new[] { 12, 13 })
{
var at = new DateTimeOffset(2026, 11, day, 0, 5, 0, TimeSpan.Zero);
await 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);
}
}).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.IsTrue(report.Passed, text);
Assert.Contains("**Esito: regolare.**", text);
Assert.Contains("Durata coperta dal lavoro del bot: 49,0 ore", text);
Assert.Contains("Al lavoro dall'inizio alla fine della finestra.", text);
Assert.Contains("Ogni conto riconciliato fino alla fine e in regola: ", 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.
// Off for 35 minutes, a guard intervention, an unexplained difference at the end: 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);
}
var off = From.AddHours(8).AddMinutes(16);
await s.ExecuteAsync("INSERT INTO absences (id, started_at, ended_at, acknowledged_at, created_at) VALUES (@id, @s, @e, NULL, @e)", CancellationToken.None,
("id", Guid.NewGuid()), ("s", off), ("e", off.AddMinutes(35))).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);
await s.ExecuteAsync("INSERT INTO reconciliations (id, account_id, outcome, details, occurred_at, created_at) VALUES (@id, @a, @o, @d, @at, @at)",
CancellationToken.None, ("id", Guid.NewGuid()), ("a", okx.AccountId), ("o", ReconcileOutcomes.Different), ("d", new Anubis.Db.DbJson("{}")), ("at", To)).ConfigureAwait(false);
await s.ExecuteAsync("UPDATE account_status SET mode = 'prudent' WHERE account_id = @a", CancellationToken.None, ("a", okx.AccountId)).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("Spento: dal 12/11/2026 03:16 per 35 minuti.", failed);
Assert.Contains("Interventi della guardia: 1.", failed);
Assert.IsTrue(failed.Any(f => f.Contains("con differenze", StringComparison.Ordinal)));
Assert.Contains("Riconciliazioni con differenze inspiegate: 1.", failed);
Assert.IsTrue(failed.Any(f => f.Contains("in modalità prudente", StringComparison.Ordinal)), string.Join(" | ", failed));
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;
// A bot that fell silent before the end of the window: its last job and the account's last round say when.
var later = await SoakReporter.BuildAsync(rig.Db, From.AddHours(2), To.AddHours(2), TimeSpan.FromHours(48), Zones.Rome).ConfigureAwait(false);
Assert.IsTrue(later.Checks.Any(c => !c.Ok && c.Text.EndsWith("da allora il bot tace.", StringComparison.Ordinal)), later.Markdown(Zones.Rome));
Assert.IsTrue(later.Checks.Any(c => !c.Ok && c.Text.StartsWith("Durata coperta dal lavoro del bot: 46,4 ore", StringComparison.Ordinal)), later.Markdown(Zones.Rome));
}
}