Integrazione 0.1.1: unione di correzione/storico-completato-una-volta

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/decisioni.md
This commit is contained in:
Alby96 committed 2026-10-20 18:05:33 +02:00
commit 00c3d8bdf4
5 files changed
+356 -27

No files matched your search

@@ -184,7 +184,11 @@ public sealed partial class RecipeRunner
}
else if (page?.HasMore == false)
{
// The whole profile is on the first page, saved just now. For an account «Storico in corso» its history
// is complete: the engine closes it, with its line (RecipeOutcome.BackfillFinished). An account that
// keeps only the new posts had no history to announce.
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
run.Finished = account.State == AccountState.Backfilling;
}
}
else if (CollectionWriter.Wants(account, EntryKind.Post) && (account.BackfillCursor is not null || account.BackfillCompletedAt is not null))
@@ -394,9 +398,10 @@ public sealed partial class RecipeRunner
run.Finished = true;
if (walk.IsGrid)
{
// «Storico completato» in Attività, and the new state of the account, are the engine's: one line per
// history, with its numbers (SessionComposer, from RecipeOutcome.BackfillFinished).
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
await settings.SetAsync(walk.MarkKey(account.Id), null, run.Ct).ConfigureAwait(false);
await Writer.EventAsync("storico-completato", $"Storico di {account.Username} completato.", EventSeverity.Success, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
}
else
{
+4 -1
View File
@@ -89,7 +89,10 @@ public sealed record RecipeOutcome(
/// <summary>Accounts where the sweep saw new posts (FollowingSweep).</summary>
public IReadOnlyList<long> AccountsWithNewPosts { get; init; } = [];
/// <summary>For Backfill: true when the beginning of the profile has been reached.</summary>
/// <summary>
/// Backfill: true when the beginning of the profile has been reached. ProfileCheck: true when the check found the
/// whole profile of an account «Storico in corso» on its first page (its history is complete as well).
/// </summary>
public bool BackfillFinished { get; init; }
}
+89 -25
View File
@@ -51,6 +51,7 @@ internal sealed class SessionComposer
public async Task<SessionSummary> RunAsync(SessionContext context, EngineTask? outsideWindowTask, CancellationToken ct)
{
var state = new SessionState(context);
await CloseCompletedHistoriesAsync(state, ct).ConfigureAwait(false);
var accounts = await SyncQueueAsync(ct).ConfigureAwait(false);
if (outsideWindowTask is not null)
@@ -145,12 +146,41 @@ internal sealed class SessionComposer
// 5. A few pages of history on one account.
if (!light && !Over(state, ct))
{
await RunBackfillAsync(accounts, state, ct).ConfigureAwait(false);
await RunBackfillAsync(state, ct).ConfigureAwait(false);
}
return state.Summary();
}
/// <summary>
/// Accounts still «Storico in corso» whose history is complete: a run closed it but its outcome was never recorded
/// (Febe stopped in between), or an earlier version left them so. Closed before the session plans anything, with
/// their line (<see cref="FinishBackfillAsync"/>); nothing towards the site.
/// </summary>
private async Task CloseCompletedHistoriesAsync(SessionState state, CancellationToken ct)
{
var complete = (await Store.Accounts.ListAsync(ct).ConfigureAwait(false))
.Where(a => a.State == AccountState.Backfilling && a.BackfillCompletedAt is not null)
.ToList();
if (complete.Count == 0)
{
return;
}
await _queueGate.WaitAsync(ct).ConfigureAwait(false);
try
{
foreach (var account in complete)
{
await FinishBackfillAsync(account, state, Clock.Now, CancellationToken.None).ConfigureAwait(false);
}
}
finally
{
_queueGate.Release();
}
}
/// <summary>
/// Keeps the queue in step with the accounts: a profile check and a story check (as the account wants them) for
/// every contactable account, a look at each highlight collection on its profile, a backfill task for every account
@@ -568,14 +598,16 @@ internal sealed class SessionComposer
/// History advances on one account per session, alternating among the accounts «Storico in corso», each up to its
/// daily maximum; suspended while the account has too many files waiting or the data budget is reached (§6.3).
/// </summary>
private async Task RunBackfillAsync(Dictionary<long, Account> accounts, SessionState state, CancellationToken ct)
private async Task RunBackfillAsync(SessionState state, CancellationToken ct)
{
if (_health.IsSuspended(RecipeKind.Backfill) || state.Exhausted.Contains(ActionKind.BackfillPage))
{
return;
}
var candidates = accounts.Values
// Read again, not taken from the start of the session: a profile check of this session may have completed a
// history already (its line is written), and that account is not walked again.
var candidates = (await Store.Accounts.ListAsync(ct).ConfigureAwait(false))
.Where(a => a.State == AccountState.Backfilling && a.BackfillCompletedAt is null && CheckPolicy.IsContactable(a))
.ToList();
if (candidates.Count == 0)
@@ -771,6 +803,15 @@ internal sealed class SessionComposer
var now = Clock.Now;
var kind = request.Kind;
var writes = new List<Task>();
// The history reached the beginning of the profile: the walk of the Storico, 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),
// or a profile check that found the whole profile on its first page. What follows sees the account as it is now.
if (kind is RecipeKind.Backfill or RecipeKind.ProfileCheck && outcome.BackfillFinished && account is not null)
{
account = await FinishBackfillAsync(account, state, now, ct).ConfigureAwait(false);
}
switch (outcome.Status)
{
case RecipeStatus.Completed:
@@ -798,7 +839,12 @@ internal sealed class SessionComposer
case RecipeStatus.StoppedBySignal:
case RecipeStatus.Cancelled:
if (task is not null)
if (task is not null && kind == RecipeKind.Backfill && outcome.BackfillFinished)
{
// The walk got to the end before the run stopped: nothing to take up again (closed above).
writes.Add(Store.Tasks.CompleteAsync(task.Id, now, ct));
}
else if (task is not null)
{
await RequeueAsync(task, outcome.Status == RecipeStatus.Cancelled ? "Interrotta" : "Fermata da un segnale", now).ConfigureAwait(false);
}
@@ -947,27 +993,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 +1007,43 @@ 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. Called once per history: after it the account is no longer «Storico in corso». Returns the account
/// as it is now.
/// </summary>
private async Task<Account> 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);
// The Coda follows at once, not at the next session: no «Storico» left for the account, and nothing planned
// for an account that stays paused. Commands of Alberto stay; the task in flight is settled by its outcome.
foreach (var task in await Store.Tasks.ListPendingAsync(10_000, ct).ConfigureAwait(false))
{
if (task.AccountId == account.Id && !task.IsManual && (newState == AccountState.Paused || task.Kind == EngineTaskKind.Backfill))
{
await Store.Tasks.CancelAsync(task.Id, now, 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);
return await Store.Accounts.GetAsync(account.Id, ct).ConfigureAwait(false) ?? fresh;
}
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);