Il motore chiude lo storico anche se la corsa finisce con un segnale

Quando il cammino dello Storico arriva in fondo, la ricetta segna lo storico
come completato e lo dice al motore (BackfillFinished). Il motore però
chiudeva lo storico (stato «Aggiornato» o «In pausa» e riga «Storico
completato») solo se la corsa finiva «Completata»: un segnale o
un'interruzione arrivati subito dopo l'ultima pagina lasciavano l'account
«Storico in corso» per sempre, senza che nessuno lo riprendesse.

Ora la chiusura sta in un solo punto (FinishBackfillAsync) e scatta ogni
volta che il cammino è arrivato in fondo, comunque sia finita la corsa.
Prova nuova per il segnale e per l'interruzione.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-20 15:23:55 +02:00
1 parent edade61b72
commit f86c794fa4
2 files changed
+93 -21

No files matched your search

+34 -21
View File
@@ -771,6 +771,14 @@ internal sealed class SessionComposer
var now = Clock.Now;
var kind = request.Kind;
var writes = new List<Task>();
// The walk reached the beginning of the profile: the history is complete however the run ended (a signal or a
// cancellation can come right after the last page was stored, and that account would not be walked again).
if (kind == RecipeKind.Backfill && outcome.BackfillFinished && account is not null)
{
await FinishBackfillAsync(account, state, now, ct).ConfigureAwait(false);
}
switch (outcome.Status)
{
case RecipeStatus.Completed:
@@ -947,27 +955,8 @@ internal sealed class SessionComposer
break;
}
case RecipeKind.Backfill when outcome.BackfillFinished:
{
var fresh = await Store.Accounts.GetAsync(account.Id, ct).ConfigureAwait(false) ?? account;
await Store.Accounts.SetBackfillAsync(account.Id, fresh.BackfillCursor, now, ct).ConfigureAwait(false);
var newState = fresh.KeepUpdated ? AccountState.UpToDate : AccountState.Paused;
await Store.Accounts.SetStateAsync(account.Id, newState, now, null, ct).ConfigureAwait(false);
var total = await Store.Entries.CountDiscoveredAsync(account.Id, EntryKind.Post, DateTimeOffset.UnixEpoch, now.AddSeconds(1), ct).ConfigureAwait(false);
var tail = fresh.KeepUpdated
? " Da ora lo tengo aggiornato."
: " «Tieni aggiornato» è spento: l'account resta in pausa.";
await _s.Events.WriteAsync(
EventSeverity.Success,
EngineEventCodes.BackfillCompleted,
$"Storico di {ItalianText.Handle(account.Username)} completato: {ItalianText.Count(total, "post", "post")} in libreria.{tail}",
account.Id,
state.Context.SessionId,
ct).ConfigureAwait(false);
break;
}
case RecipeKind.Backfill:
// A history completed: FinishBackfillAsync, from ApplyOutcomeAsync.
case RecipeKind.Backfill when !outcome.BackfillFinished:
if (contactable)
{
// Behind the other accounts «Storico in corso»: they alternate.
@@ -980,6 +969,30 @@ internal sealed class SessionComposer
}
}
/// <summary>
/// The history of the account is complete: «Aggiornato» (or «In pausa» with «Tieni aggiornato» off) and the one
/// «storico completato» line of Attività, which is also the alert (spec §13.3). The recipes write no line of their
/// own for it.
/// </summary>
private async Task FinishBackfillAsync(Account account, SessionState state, DateTimeOffset now, CancellationToken ct)
{
var fresh = await Store.Accounts.GetAsync(account.Id, ct).ConfigureAwait(false) ?? account;
await Store.Accounts.SetBackfillAsync(account.Id, fresh.BackfillCursor, now, ct).ConfigureAwait(false);
var newState = fresh.KeepUpdated ? AccountState.UpToDate : AccountState.Paused;
await Store.Accounts.SetStateAsync(account.Id, newState, now, null, ct).ConfigureAwait(false);
var total = await Store.Entries.CountDiscoveredAsync(account.Id, EntryKind.Post, DateTimeOffset.UnixEpoch, now.AddSeconds(1), ct).ConfigureAwait(false);
var tail = fresh.KeepUpdated
? " Da ora lo tengo aggiornato."
: " «Tieni aggiornato» è spento: l'account resta in pausa.";
await _s.Events.WriteAsync(
EventSeverity.Success,
EngineEventCodes.BackfillCompleted,
$"Storico di {ItalianText.Handle(account.Username)} completato: {ItalianText.Count(total, "post", "post")} in libreria.{tail}",
account.Id,
state.Context.SessionId,
ct).ConfigureAwait(false);
}
private async Task PauseAfterSingleDownloadAsync(Account account, DateTimeOffset now, SessionState state, CancellationToken ct)
{
await Store.Accounts.SetStateAsync(account.Id, AccountState.Paused, now, null, ct).ConfigureAwait(false);
@@ -0,0 +1,59 @@
using Febe.Core.Abstractions;
using Febe.Core.Alerts;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Storage;
using Febe.Tests.Alerts.Support;
using Febe.Tests.Engine.Support;
namespace Febe.Tests.Engine;
/// <summary>
/// «Storico completato» is one line of Attività and one alert per history, written by the engine with its numbers,
/// also for the runs that end otherwise right after the last page.
/// </summary>
[TestClass]
public sealed class BackfillCompletedTests
{
private static readonly DateTimeOffset Morning = EngineTestBed.Local(2026, 10, 14, 6, 0);
/// <summary>
/// The walk reached the beginning of the profile and stored it, then a signal or a cancellation changed how the
/// run ended: the history is complete all the same and keeps its line (it would never be walked again).
/// </summary>
[TestMethod]
[DataRow(RecipeStatus.StoppedBySignal)]
[DataRow(RecipeStatus.Cancelled)]
public async Task AHistoryCompletedRightBeforeARunEndsOtherwiseKeepsItsLine(RecipeStatus status)
{
await using var bed = await AlertBed.CreateAsync(start: Morning);
var engine = bed.Engine;
var account = await engine.AddAccountAsync("luca.prova", followed: true, state: AccountState.Backfilling, postCount: 30);
await engine.Db.Accounts.UpdateAsync((await engine.Db.Accounts.GetAsync(account.Id))! with { KeepUpdated = false });
var runner = new ScriptedRunner(engine.Governor, engine.Clock);
var backfills = 0;
runner.Before = async r =>
{
if (r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && ++backfills == 1)
{
// What the recipe stores when the walk meets the end of the list.
await engine.Db.Accounts.SetBackfillAsync(account.Id, null, engine.Clock.Now);
}
};
runner.Script = r => r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && backfills == 1
? new RecipeOutcome(status, 1, 6) { BackfillFinished = true }
: null;
engine.Start(runner);
await engine.RunUntilAsync(Morning.AddDays(2));
await engine.StopAsync();
await bed.Alerts.FlushAsync();
Assert.AreEqual(1, backfills, "Never walked again.");
var finished = await engine.Db.Accounts.GetAsync(account.Id);
Assert.AreEqual(AccountState.Paused, finished!.State, "«Tieni aggiornato» off: one download, then paused.");
var lines = await engine.Db.Events.ListAsync(new EventQuery { Codes = [EngineEventCodes.BackfillCompleted] });
Assert.HasCount(1, lines);
Assert.AreEqual("Storico di @luca.prova completato: 0 post in libreria. «Tieni aggiornato» è spento: l'account resta in pausa.", lines[0].Message);
Assert.HasCount(1, bed.Shell.Notifications.Where(n => n.Title == AlertCatalog.Title(AlertKind.BackfillDone)));
}
}