Keep the laboratory going and resumed units consistent when judging tournaments
An error while judging a tournament (a plan that no longer reads, the database out of reach) could stop the whole laboratory queue or leave the tournament unjudged until a restart. The judge now reads inside its failure handling, so an unreadable plan fails the tournament with an Italian reason; every call of the judge is guarded; and a finished contender trial that comes back to the queue brings its tournament to the judge. Resumed trials and judges reused finished units without checking they were computed on the same inputs, so a restart after new bars or an image update mixed runs of two data sets or two engines. Every unit now records its fingerprint (the data, the engine version, the build) and only units on the same inputs are reused; the runs on the final tract are checked on the build and, when the tract is read, on its data. The judge also checks that each contender trial is the one the tournament plan wrote: plan hash, grid, data source, capital, slippage and reference share. The settings hint now says the workers are calculations of one trial; trials still run one at a time. refs #122, refs #123 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8aafa5ad32
commit
9ae1e955a4
10 files changed
+471
-57
No files matched your search
@@ -252,6 +252,72 @@ public sealed class TrialUnitTests
|
||||
Assert.AreEqual(4, report.TrialsCounted, "Units never count as trials.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A trial stopped halfway whose bars change before the next start (a bar collected again, a corporate action): the units
|
||||
/// done on the old bars are not reused, every unit runs again on the new ones and the report is that of a run without a stop.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task UnitsComputedOnOtherDataAreRunAgainOnResume()
|
||||
{
|
||||
var clock = new ManualClock(new DateTimeOffset(2026, 11, 11, 9, 0, 0, TimeSpan.Zero));
|
||||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||||
var btc = await LaboratoryTests.SeedBitcoinAsync(test, clock).ConfigureAwait(false);
|
||||
using var lab = new Laboratory(test.Db, clock, Path.GetTempPath());
|
||||
var trial = await lab.PlanAsync(LaboratoryTests.Request(), _none).ConfigureAwait(false);
|
||||
var family = StrategyCatalog.Find(trial.Strategy)!;
|
||||
var before = await Laboratory.LoadAsync(test.Db, clock, Path.GetTempPath(), trial.Plan.Data, _none).ConfigureAwait(false);
|
||||
|
||||
// The process stops while the third variant runs, after the first two are recorded.
|
||||
using var stop = new CancellationTokenSource();
|
||||
var first = new List<string>();
|
||||
lab.EngineFor = (p, f, d) => new RecordingEngine(new DailyTrialEngine(p, f, d), first, n =>
|
||||
{
|
||||
if (n == 3)
|
||||
{
|
||||
for (var i = 0; i < 600 && lab.Units.ListAsync(trial.Id, _none).GetAwaiter().GetResult().Count(r => r.Status == TrialUnitStatuses.Done) < 2; i++)
|
||||
{
|
||||
Thread.Sleep(20);
|
||||
}
|
||||
|
||||
stop.Cancel();
|
||||
}
|
||||
});
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => lab.ProcessAsync(trial.Id, stop.Token)).ConfigureAwait(false);
|
||||
var finished = (await lab.Units.ListAsync(trial.Id, _none).ConfigureAwait(false)).Where(r => r.Status == TrialUnitStatuses.Done).Select(r => r.Key).ToList();
|
||||
Assert.IsGreaterThanOrEqualTo(2, finished.Count);
|
||||
|
||||
// The bars are collected again with other prices before the next start.
|
||||
await new BarStore(test.Db, clock).UpsertDailyAsync(btc.Id, BacktestData.Walk(400, 4, start: 40_000, drift: 0.001, volatility: 0.03, from: new DateOnly(2024, 1, 1)),
|
||||
"test", _none).ConfigureAwait(false);
|
||||
var data = await Laboratory.LoadAsync(test.Db, clock, Path.GetTempPath(), trial.Plan.Data, _none).ConfigureAwait(false);
|
||||
Assert.AreNotEqual(before.Fingerprint(), data.Fingerprint());
|
||||
var whole = TrialRunner.Run(trial.Id, trial.Plan, trial.PlanHash, family, data, PriorTrials.None, clock.UtcNow).Report;
|
||||
|
||||
CollectionAssert.Contains((await lab.Trials.ResumeAsync(_none).ConfigureAwait(false)).ToList(), trial.Id);
|
||||
var second = new List<string>();
|
||||
lab.EngineFor = (p, f, d) => new RecordingEngine(new DailyTrialEngine(p, f, d), second);
|
||||
await lab.ProcessAsync(trial.Id, _none).ConfigureAwait(false);
|
||||
var done = (await lab.Trials.GetAsync(trial.Id, _none).ConfigureAwait(false))!;
|
||||
Assert.AreEqual(TrialStatuses.Done, done.Status, done.Error);
|
||||
|
||||
var total = 4 + 2 + 1 + 1;
|
||||
Assert.HasCount(total, second, "Every unit runs again on the new bars.");
|
||||
foreach (var key in finished)
|
||||
{
|
||||
CollectionAssert.Contains(second, CallOf(key), "A unit done on the old bars runs again: " + key);
|
||||
}
|
||||
|
||||
var report = BacktestReport.FromJson((await lab.Trials.ReportJsonAsync(trial.Id, _none).ConfigureAwait(false))!);
|
||||
Assert.AreEqual((whole with { Seconds = 0 }).ToJson(), (report with { Seconds = 0 }).ToJson());
|
||||
|
||||
// Every unit keeps what it was computed on: the new bars, the engine and this build.
|
||||
var rows = await lab.Units.ListAsync(trial.Id, _none).ConfigureAwait(false);
|
||||
Assert.HasCount(total, rows);
|
||||
Assert.IsTrue(rows.All(r => r.Fingerprint == data.Fingerprint() + TrialUnitStore.Build), string.Join(", ", rows.Select(r => r.Fingerprint)));
|
||||
StringAssert.StartsWith(TrialUnitStore.Build, "|" + BacktestEngine.Version + "|");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void TheWorkersOfTheLaboratoryAreAWholeNumberFromOneToOneLessThanTheProcessors()
|
||||
@@ -279,6 +345,7 @@ public sealed class TrialUnitTests
|
||||
await server.SignInFirstTimeAsync();
|
||||
var page = await server.TextAsync("/impostazioni");
|
||||
StringAssert.Contains(page, "Operai del Laboratorio");
|
||||
StringAssert.Contains(page, "Quanti calcoli di una prova del Laboratorio girano insieme: le prove restano una alla volta.");
|
||||
StringAssert.Contains(page, "il Laboratorio non deve togliere tempo al trading");
|
||||
StringAssert.Contains(page, "Vale dalla prossima prova.");
|
||||
|
||||
|
||||
Reference in new issue
Block a user