Recover missed work after an absence by the rules of SPEC 5.6 (refs #49)

At the start the host measures how long the bot was away from the last
job that finished, records the absence and writes it in the diary; one
longer than a day is kept for the Panoramica until the user has seen it.

A job can now say how a run missed while the bot was off is treated: the
nightly backup, the exchange rates, the market calendar and the cleanup
run once at the restart, queued, however late; a job with a deadline is
skipped past it and the diary says so. Corporate actions are read before
the reconciliation puts any stop back. A decision the engine makes late
because of an absence is written in the diary as recovered, as are the
orders it releases late; entries that moved too far or expired are
dropped with their reason.

Tests switch the bot off for 3 hours, 20 hours and 3 days with a manual
clock and check each of those rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 18:38:50 +01:00
1 parent 41e067e7d1
commit 680cab4bb6
9 files changed
+454 -18

No files matched your search

+35 -7
View File
@@ -312,7 +312,28 @@ internal sealed partial class AnubisHost(
// The laboratory computes on its own thread and reads only local data; the demo shows appendix F and runs none.
_lab = options.Demo ? null : new Laboratory(_db, clock, Config.Data.Research, logger);
_services = new AppServices(_db, settings, secrets, access, backups, scheduler, _market, _trading, _lab);
StartScheduler(scheduler, backups);
// Before the scheduler writes its first run: how long the bot was away, and which runs it missed (SPEC 5.6).
var lastDues = new Dictionary<string, DateTimeOffset>(StringComparer.Ordinal);
try
{
if (!options.Demo)
{
await new AbsenceBook(_db, clock) { Zone = Config.TimeZone }.RecordAsync(cancellationToken).ConfigureAwait(false);
}
foreach (var (name, due) in await _db.ReadAsync(s => s.QueryAsync("SELECT name, last_due_at FROM jobs WHERE last_due_at IS NOT NULL",
r => (r.Text("name"), r.Moment("last_due_at"))), cancellationToken).ConfigureAwait(false))
{
lastDues[name] = due;
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
LogStepFailed(logger, ex);
}
StartScheduler(scheduler, backups, lastDues);
SetStatus(HostMode.Ready, null);
}
@@ -431,14 +452,15 @@ internal sealed partial class AnubisHost(
!options.Demo && (overrides is null ? true : overrides.Trading == true) &&
environment.GetValueOrDefault("ANUBIS_TRADING") is not ("0" or "off" or "false");
private void StartScheduler(Scheduler scheduler, Backups backups)
private void StartScheduler(Scheduler scheduler, Backups backups, Dictionary<string, DateTimeOffset> lastDues)
{
scheduler.Add(new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Config.TimeZone),
// A backup missed while the bot was off is taken once at the restart, whenever it comes (SPEC 5.6).
var backup = new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Config.TimeZone),
async ct => await backups.TakeAsync(_db!, "notturno", ct).ConfigureAwait(false))
{ MaxLateness = TimeSpan.FromHours(2) });
foreach (var job in (_market?.Jobs() ?? []).Concat(_trading?.Jobs() ?? []))
{ MaxLateness = TimeSpan.FromHours(2), CatchUpWithin = TimeSpan.FromDays(3650) };
foreach (var job in new[] { backup }.Concat(_market?.Jobs() ?? []).Concat(_trading?.Jobs() ?? []))
{
scheduler.Add(job);
scheduler.Add(job, lastDues.TryGetValue(job.Name, out var due) ? due : null);
}
_schedulerStop = new CancellationTokenSource();
@@ -525,7 +547,13 @@ internal sealed partial class AnubisHost(
{
await _db!.WriteAsync(async s =>
{
var description = _services?.Scheduler.Upcoming(100).FirstOrDefault(u => u.Name == run.Name)?.Description ?? run.Name;
var description = run.Description ?? _services?.Scheduler.Upcoming(100).FirstOrDefault(u => u.Name == run.Name)?.Description ?? run.Name;
if (AbsenceBook.MissedLine(run with { Description = description }, Config.TimeZone) is { } line)
{
await Diary.WriteAsync(s, clock, DiaryKinds.System, run.Outcome == JobOutcome.Completed ? "job_recovered" : "job_skipped", line,
new { job = run.Name, due = run.DueUtc }, cancellationToken).ConfigureAwait(false);
}
var updated = await s.ExecuteAsync(
"UPDATE jobs SET last_due_at = @d, last_started_at = @s, last_finished_at = @f, last_outcome = @o, last_error = @e, updated_at = @f WHERE name = @n",
("d", run.DueUtc), ("s", run.StartedUtc), ("f", run.FinishedUtc), ("o", run.Outcome.ToString().ToLowerInvariant()), ("e", run.Error), ("n", run.Name)).ConfigureAwait(false);
+94 -4
View File
@@ -121,6 +121,12 @@ public sealed record ScheduledJob(string Name, string Description, Schedule Sche
/// <summary>A move of the bot the user reads in "Prossime mosse" (SPEC 13.6), not a housekeeping job.</summary>
public bool IsMove { get; init; }
/// <summary>
/// A run missed while the bot was off is made once at the restart, queued, when the restart comes within this
/// time of the missed run; later it is skipped and reported (SPEC 5.6). Null: a missed run is simply not made.
/// </summary>
public TimeSpan? CatchUpWithin { get; init; }
}
public enum JobOutcome
@@ -131,7 +137,13 @@ public enum JobOutcome
}
/// <summary>Published on the bus after every run, skip or failure.</summary>
public sealed record JobRun(string Name, DateTimeOffset DueUtc, DateTimeOffset StartedUtc, DateTimeOffset FinishedUtc, JobOutcome Outcome, string? Error);
public sealed record JobRun(string Name, DateTimeOffset DueUtc, DateTimeOffset StartedUtc, DateTimeOffset FinishedUtc, JobOutcome Outcome, string? Error)
{
/// <summary>The run was missed while the bot was off and made, or skipped, at the restart (SPEC 5.6).</summary>
public bool Missed { get; init; }
public string? Description { get; init; }
}
/// <summary>A run coming up, for "Prossime mosse" and the jobs list.</summary>
public sealed record UpcomingRun(string Name, string Description, DateTimeOffset DueUtc)
@@ -148,21 +160,60 @@ public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger<Schedu
private readonly ILogger _logger = logger ?? NullLogger<Scheduler>.Instance;
private readonly Lock _gate = new();
private readonly Dictionary<string, (ScheduledJob Job, DateTimeOffset? Due)> _jobs = new(StringComparer.Ordinal);
private readonly Dictionary<string, DateTimeOffset> _recovering = new(StringComparer.Ordinal);
private readonly List<JobRun> _missed = [];
private TaskCompletionSource _changed = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>Moment of the last loop iteration; the health check uses it to see the loop is alive.</summary>
public DateTimeOffset? LastBeatUtc { get; private set; }
public void Add(ScheduledJob job)
public void Add(ScheduledJob job) => Add(job, null);
/// <summary>
/// Adds a job whose last run, from an earlier life of the process, was due at <paramref name="lastDue"/>. A job with
/// <see cref="ScheduledJob.CatchUpWithin"/> that missed a run since then makes it once at the start, or reports it
/// skipped when the restart is too late (SPEC 5.6).
/// </summary>
public void Add(ScheduledJob job, DateTimeOffset? lastDue)
{
ArgumentNullException.ThrowIfNull(job);
var now = clock.UtcNow;
var missed = job.CatchUpWithin is not null && lastDue is { } last ? LastMissed(job.Schedule, last, now) : null;
lock (_gate)
{
_jobs[job.Name] = (job, job.Schedule.NextAfter(clock.UtcNow));
if (missed is { } due && now - due <= job.CatchUpWithin)
{
_jobs[job.Name] = (job, now);
_recovering[job.Name] = due;
}
else
{
_jobs[job.Name] = (job, job.Schedule.NextAfter(now));
if (missed is { } skipped)
{
_missed.Add(new JobRun(job.Name, skipped, now, now, JobOutcome.Skipped, "Saltato al riavvio: oltre l'orario utile.")
{ Missed = true, Description = job.Description });
}
}
_changed.TrySetResult();
}
}
/// <summary>The last due time after <paramref name="last"/> and not after <paramref name="now"/>: the run the absence took.</summary>
private static DateTimeOffset? LastMissed(Schedule schedule, DateTimeOffset last, DateTimeOffset now)
{
DateTimeOffset? missed = null;
var at = schedule.NextAfter(last);
for (var i = 0; i < 100_000 && at is { } due && due <= now; i++)
{
missed = due;
at = schedule.NextAfter(due);
}
return missed;
}
public IReadOnlyList<UpcomingRun> Upcoming(int count = 10)
{
lock (_gate)
@@ -224,8 +275,13 @@ public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger<Schedu
{
var now = clock.UtcNow;
List<(ScheduledJob Job, DateTimeOffset Due)> due;
List<JobRun> missed;
lock (_gate)
{
// The runs an absence took and the restart came too late for: reported once, at the first round.
missed = [.. _missed];
_missed.Clear();
due = _jobs.Values.Where(j => j.Due is { } d && d <= now).Select(j => (j.Job, j.Due!.Value)).OrderBy(j => j.Value).ToList();
foreach (var (job, _) in due)
{
@@ -233,9 +289,28 @@ public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger<Schedu
}
}
foreach (var (job, dueAt) in due)
foreach (var run in missed)
{
bus.Publish(run);
}
foreach (var (job, queuedAt) in due)
{
var started = clock.UtcNow;
bool recovering;
DateTimeOffset dueAt;
lock (_gate)
{
recovering = _recovering.Remove(job.Name, out var missedDue);
dueAt = recovering ? missedDue : queuedAt;
}
if (recovering)
{
await RecoverAsync(job, dueAt, started, cancellationToken).ConfigureAwait(false);
continue;
}
if (started - dueAt > job.MaxLateness)
{
LogSkipped(_logger, job.Name, dueAt);
@@ -256,6 +331,21 @@ public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger<Schedu
}
}
/// <summary>A run missed while the bot was off, made once now: never skipped for lateness, reported as recovered.</summary>
private async Task RecoverAsync(ScheduledJob job, DateTimeOffset dueAt, DateTimeOffset started, CancellationToken cancellationToken)
{
try
{
await job.Run(cancellationToken).ConfigureAwait(false);
bus.Publish(new JobRun(job.Name, dueAt, started, clock.UtcNow, JobOutcome.Completed, null) { Missed = true, Description = job.Description });
}
catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
{
LogFailed(_logger, ex, job.Name);
bus.Publish(new JobRun(job.Name, dueAt, started, clock.UtcNow, JobOutcome.Failed, ex.Message) { Missed = true, Description = job.Description });
}
}
[LoggerMessage(Level = LogLevel.Warning, Message = "Job {Job} skipped: it was due at {Due} and is too late to run")]
private static partial void LogSkipped(ILogger logger, string job, DateTimeOffset due);
+7 -5
View File
@@ -105,21 +105,22 @@ public sealed partial class MarketDataService : IDisposable
{ MaxLateness = TimeSpan.FromHours(6) },
new("dati-azioni-chiusura", "Barre giornaliere delle azioni dopo la chiusura", Schedule.Market(MarketAnchor.Close, TimeSpan.FromMinutes(30), UsCalendar),
_ => WakeWorker()) { MaxLateness = TimeSpan.FromHours(6) },
// SPEC 5.6: after an absence the last rate available is downloaded at once.
new("dati-cambi-mattina", "Cambi di riferimento della BCE", Schedule.Daily(new TimeOnly(8, 0), Zones.Rome), References.RefreshFxAsync)
{ MaxLateness = TimeSpan.FromHours(4) },
{ MaxLateness = TimeSpan.FromHours(4), CatchUpWithin = TimeSpan.FromDays(3650) },
new("dati-cambi-pomeriggio", "Cambi di riferimento della BCE", Schedule.Daily(new TimeOnly(16, 30), Zones.Rome), References.RefreshFxAsync)
{ MaxLateness = TimeSpan.FromHours(4) },
{ MaxLateness = TimeSpan.FromHours(4), CatchUpWithin = TimeSpan.FromDays(3650) },
new("dati-calendario", "Calendario economico, riunioni e trimestrali", Schedule.Every(TimeSpan.FromHours(1)), RefreshCalendarAsync)
{ MaxLateness = TimeSpan.FromMinutes(50) },
new("dati-umore", "Umore del mercato", Schedule.Every(TimeSpan.FromHours(1)), References.RefreshMoodAsync) { MaxLateness = TimeSpan.FromMinutes(50) },
new("dati-spread", "Campione orario degli spread delle crypto", Schedule.Every(TimeSpan.FromHours(1)), SampleSpreadsAsync)
{ MaxLateness = TimeSpan.FromMinutes(50) },
new("dati-calendario-borsa", "Calendario di borsa di Alpaca", Schedule.Daily(new TimeOnly(4, 0), Zones.Rome), RefreshUsCalendarAsync)
{ MaxLateness = TimeSpan.FromHours(12) },
{ MaxLateness = TimeSpan.FromHours(12), CatchUpWithin = TimeSpan.FromDays(3650) },
new("dati-azioni-societarie", "Frazionamenti e dividendi prima dell'apertura", Schedule.Market(MarketAnchor.Open, TimeSpan.FromHours(-1), UsCalendar),
RefreshCorporateActionsAsync) { MaxLateness = TimeSpan.FromMinutes(50) },
new("dati-pulizia", "Pulizia delle barre al minuto e degli spread vecchi", Schedule.Daily(new TimeOnly(3, 45), Zones.Rome), PruneAsync)
{ MaxLateness = TimeSpan.FromHours(12) },
{ MaxLateness = TimeSpan.FromHours(12), CatchUpWithin = TimeSpan.FromDays(3650) },
];
/// <summary>
@@ -420,7 +421,8 @@ public sealed partial class MarketDataService : IDisposable
await UsCalendar.SaveAsync(sessions, today.AddDays(-30), today.AddDays(90), cancellationToken).ConfigureAwait(false);
}
private async Task RefreshCorporateActionsAsync(CancellationToken cancellationToken)
/// <summary>Splits and dividends of the stock universe from Alpaca; read first at a restart, before any stop is touched (SPEC 5.6, 6.10).</summary>
public async Task RefreshCorporateActionsAsync(CancellationToken cancellationToken)
{
if (await AlpacaAsync(cancellationToken).ConfigureAwait(false) is not { } alpaca)
{
+92
View File
@@ -0,0 +1,92 @@
using Anubis.Core;
using Anubis.Data;
using Anubis.Db;
namespace Anubis.Trading;
/// <summary>A time the bot was off: from its last sign of life to the restart (SPEC 5.6).</summary>
public sealed record Absence(Guid Id, DateTimeOffset StartedAt, DateTimeOffset EndedAt, DateTimeOffset? AcknowledgedAt)
{
public TimeSpan Length => EndedAt - StartedAt;
}
/// <summary>
/// The absences of the bot (SPEC 5.6): measured at the restart from the last job that ran, written in the diary, and
/// declared at the top of the Panoramica when longer than a day, until the user has seen it.
/// </summary>
public sealed class AbsenceBook(Database db, IClock clock)
{
/// <summary>Shorter than this is a restart, not an absence: the minute pulse runs every minute.</summary>
public static readonly TimeSpan Threshold = TimeSpan.FromMinutes(5);
/// <summary>An absence longer than this is declared on the Panoramica.</summary>
public static readonly TimeSpan Declared = TimeSpan.FromHours(24);
public TimeZoneInfo Zone { get; init; } = Zones.Rome;
/// <summary>
/// At the start, before the scheduler: the time since the last job finished. An absence is recorded and written in
/// the diary; null when the bot was not away (or never ran before).
/// </summary>
public async Task<Absence?> RecordAsync(CancellationToken cancellationToken)
{
var now = clock.UtcNow;
return await db.WriteAsync(async s =>
{
var last = await s.FirstOrDefaultAsync("SELECT MAX(last_finished_at) AS last FROM jobs", r => r.IsNull("last") ? (DateTimeOffset?)null : r.Moment("last"))
.ConfigureAwait(false);
if (last is not { } since || now - since < Threshold)
{
return null;
}
var absence = new Absence(Ids.NewId(clock), since, now, null);
await s.ExecuteAsync("INSERT INTO absences (id, started_at, ended_at, acknowledged_at, created_at) VALUES (@id, @s, @e, NULL, @now)", cancellationToken,
("id", absence.Id), ("s", since), ("e", now), ("now", now)).ConfigureAwait(false);
await Diary.WriteAsync(s, clock, DiaryKinds.System, "absence", DiaryText.Absence(absence.Length), new { started = since, ended = now }, cancellationToken)
.ConfigureAwait(false);
return absence;
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>The absence the Panoramica declares: the last one longer than a day the user has not acknowledged.</summary>
public async Task<Absence?> DeclaredAsync(CancellationToken cancellationToken)
{
var rows = await db.ReadAsync(s => s.QueryAsync(
"SELECT id, started_at, ended_at, acknowledged_at FROM absences WHERE acknowledged_at IS NULL ORDER BY ended_at DESC",
r => new Absence(r.Id("id"), r.Moment("started_at"), r.Moment("ended_at"), r.MomentOrNull("acknowledged_at"))), cancellationToken).ConfigureAwait(false);
return rows.FirstOrDefault(a => a.Length > Declared);
}
/// <summary>The user has seen the notice: every absence up to this one stops being declared.</summary>
public Task AcknowledgeAsync(Guid id, CancellationToken cancellationToken) =>
db.WriteAsync(async s =>
{
var ended = await s.FirstOrDefaultAsync("SELECT ended_at FROM absences WHERE id = @id", r => (DateTimeOffset?)r.Moment("ended_at"), ("id", id)).ConfigureAwait(false);
if (ended is { } until)
{
await s.ExecuteAsync("UPDATE absences SET acknowledged_at = @now WHERE acknowledged_at IS NULL AND ended_at <= @e", cancellationToken, ("now", clock.UtcNow), ("e", until))
.ConfigureAwait(false);
}
}, cancellationToken);
/// <summary>The diary line of a run missed while the bot was off: made at the restart, or skipped with its reason.</summary>
public static string? MissedLine(JobRun run, TimeZoneInfo zone)
{
ArgumentNullException.ThrowIfNull(run);
if (!run.Missed)
{
return null;
}
var what = Lower(run.Description ?? run.Name);
return run.Outcome switch
{
JobOutcome.Completed => DiaryText.Recovered(what, run.DueUtc, zone),
JobOutcome.Skipped => DiaryText.Skipped($"{what} (previsto alle {TimeZoneInfo.ConvertTime(run.DueUtc, zone):HH:mm})", "Il riavvio è arrivato oltre l'orario utile."),
_ => null,
};
}
private static string Lower(string text) => text.Length == 0 ? text : char.ToLowerInvariant(text[0]) + text[1..];
}
+8
View File
@@ -749,6 +749,14 @@ public sealed partial class LiveEngine : IDisposable
summary += " " + ReasonCatalog.Text(LiveReasons.LateExitsOnly);
}
if (m.Now - m.Due > Options.RecoveredAfter)
{
// Made late because the bot was off (SPEC 5.6): said before what was decided.
var what = m.Strategy == StrategyNames.CryptoTrend ? "la chiusura giornaliera crypto" : "il calcolo dei segnali sulle azioni";
await Diary.WriteAsync(s, _clock, DiaryKinds.Decisions, LiveReasons.Recovered, DiaryText.Recovered(what, m.Due, Zone), new { decision = decisionId },
cancellationToken).ConfigureAwait(false);
}
await Diary.WriteAsync(s, _clock, DiaryKinds.Decisions, "decision", summary, new { decision = decisionId }, cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
}
+8
View File
@@ -263,6 +263,14 @@ public sealed partial class TradingService : IDisposable
}
await GuardAsync("simulatore", () => FundSimulatorsAsync(cancellationToken), cancellationToken).ConfigureAwait(false);
// SPEC 5.6: corporate actions come first, so the stops the reconciliation puts back already know about a split.
if (_market is not null)
{
await GuardAsync("azioni societarie", () => _market.RefreshCorporateActionsAsync(cancellationToken), cancellationToken).ConfigureAwait(false);
}
await GuardAsync("azioni societarie nei registri", () => CorporateActionsAsync(cancellationToken), cancellationToken).ConfigureAwait(false);
foreach (var manager in _managers.Values)
{
await GuardAsync("avvio " + manager.Account.Name, async () =>