Fix what the review of Phase 9 found in accounts, access, capital, health and publication

A position left out together with its stop kept the account prudent for good: the
stop was still asked for shares that no longer exist. The cover required now stops
at what the account holds, the stops are compared again after the choice, and where
one is still missing Align takes the excluded position up again. After the choice
the account waits for the stop with its own reason, so it returns to normal by
itself also past the 15 minutes; a new difference still gets its own 15 minutes.

A right password alone no longer clears the wrong second-factor codes: it takes
back only its own attempt.

Capital and shares are planned and written in one transaction, so overlapping
changes cannot apply twice and an interruption leaves nothing halfway; sending the
same figures again completes what an old one left. The cash that purchases under
way still hold is no longer given away, counted as the risk engine counts it.

/health said healthy when the trading engine had failed to start. It now says so at
once, or after ten minutes of a stalled start, and the host tries the start again
every minute; the guard still sees a start that does not move.

The image of the manual publication says its version and its commit, marked when
the tree is dirty, and publish.sh refuses before any login an image that does not
come from the commit of the tag.

refs #75

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 22:07:46 +01:00
1 parent 24e20584d4
commit 81d6c5f667
20 files changed
+814 -174

No files matched your search

+107 -1
View File
@@ -217,7 +217,7 @@ public sealed class ReconcilerTests
{
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, choice, "test", _none).ConfigureAwait(false));
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.AreEqual((AccountModes.Prudent, DifferenceKinds.Stop), (state.Mode, state.ModeReason));
Assert.AreEqual((AccountModes.Prudent, Reconciler.StopReason), (state.Mode, state.ModeReason));
Assert.IsFalse(state.Excluded.Any(e => e.StartsWith(DifferenceKinds.Stop, StringComparison.Ordinal)), string.Join("; ", state.Excluded));
}
@@ -228,6 +228,112 @@ public sealed class ReconcilerTests
Assert.AreEqual(AccountModes.Normal, report.Mode, string.Join("; ", report.Differences.Select(d => d.Message)));
}
/// <summary>Coin gone from the account and no stop left: prudent, and past the 15 minutes.</summary>
private static async Task GoneWithItsStopAsync(Setup s, decimal missing)
{
s.Broker.Missing["BTC-EUR"] = missing;
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
s.Broker.RefuseStops = true;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
CollectionAssert.AreEquivalent(new[] { DifferenceKinds.Position, DifferenceKinds.Stop }, report.Differences.Select(d => d.Kind).ToArray());
}
[TestMethod]
[TestCategory(Category.Simulation)]
[DataRow(ReconcileChoice.Align)]
[DataRow(ReconcileChoice.Exclude)]
public async Task APositionThatLeftWithItsStopDoesNotKeepTheAccountPrudentAfterTheChoice(ReconcileChoice choice)
{
await using var s = await HoldingAsync().ConfigureAwait(false);
await GoneWithItsStopAsync(s, 0.00998m).ConfigureAwait(false);
// No stop can cover what is not there: whatever the user chooses, none is waited for.
Assert.AreEqual(ReconcileResolution.Normal, await s.Reconciler.ResolveAsync(s.Manager, choice, "test", _none).ConfigureAwait(false));
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.AreEqual(AccountModes.Normal, state.Mode);
Assert.IsEmpty(state.Differences);
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
Assert.AreEqual((ReconcileOutcomes.Clean, AccountModes.Normal), (report.Outcome, report.Mode), string.Join("; ", report.Differences.Select(d => d.Message)));
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AStopMissingOnWhatIsLeftStaysAndAlignTakesUpTheExcludedPosition()
{
await using var s = await HoldingAsync().ConfigureAwait(false);
await GoneWithItsStopAsync(s, 0.004m).ConfigureAwait(false);
// 0.00598 are still on the account without their stop: that is not left out, and only for that quantity.
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Exclude, "test", _none).ConfigureAwait(false));
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.AreEqual((DifferenceKinds.Stop, 0.00598m, 0m), (state.Differences.Single().Kind, state.Differences.Single().Ledger, state.Differences.Single().Broker));
// The stop asked for what the ledger says never goes through: the way out is to take the account's number.
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace));
Assert.IsTrue(state.HasChoices, "The position left out can still be aligned.");
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
Assert.AreEqual(0.00598m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity);
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.IsEmpty(state.Excluded);
Assert.IsFalse(state.HasChoices);
s.Broker.RefuseStops = false;
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
Assert.AreEqual(0.00598m, (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single().Quantity);
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AfterTheChoiceTheAccountReturnsToNormalWheneverTheStopIsBack()
{
await using var s = await HoldingAsync().ConfigureAwait(false);
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
s.Broker.RefuseStops = true;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
// Much later than 15 minutes: the user has already chosen, nothing is left to ask.
s.Rig.Clock.Advance(TimeSpan.FromHours(3));
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
s.Broker.RefuseStops = false;
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
Assert.HasCount(1, await CodesAsync(s.Rig, "account_normal").ConfigureAwait(false));
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task ANewDifferenceWhileAStopIsAwaitedHasItsOwnFifteenMinutes()
{
await using var s = await HoldingAsync().ConfigureAwait(false);
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
s.Broker.RefuseStops = true;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false);
// Coin leaves the account while the stop is awaited: past its 15 minutes that one waits for the user (SPEC 7.4).
s.Rig.Clock.Advance(TimeSpan.FromMinutes(30));
s.Broker.Missing["BTC-EUR"] = 0.004m;
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
Assert.IsFalse(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "It may still explain itself.");
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
s.Broker.Missing.Clear();
s.Broker.RefuseStops = false;
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
Assert.AreEqual(ReconcileResolution.Normal, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
}
[TestMethod]
[TestCategory(Category.Simulation)]
public async Task AKeyThatMayWithdrawStopsPurchasesUntilItIsReplaced()