using Febe.Core.Abstractions; using Febe.Core.Domain; using Febe.Core.Pacing; using Febe.Core.Storage; namespace Febe.Core.Engine; /// /// Composes and runs one session (spec §7.2, §7.3): commands of Alberto first, then usually the Giro dei seguiti, /// the flagged and due stories, the expired profile checks and highlight collections, finally a few pages of history on /// one account. About one session in three stops after the sweep and the stories. Accounts are taken in a random order /// weighted by frequency and delay. Planned work lives in the persisted task queue; this class keeps it in step with /// the accounts. /// internal sealed class SessionComposer { private static readonly TimeSpan DefaultWindow = TimeSpan.FromHours(16); private readonly EngineServices _s; private readonly IRecipeRunner _runner; private readonly RecipeHealth _health; private readonly RecentActions _recent; private readonly Action _manualDone; private readonly Lock _noticeGate = new(); private readonly HashSet<(string Code, long? Account, DateOnly Day)> _notices = []; // The interface calls SyncQueueAsync while a session may be running. A recipe's task is Running in the database // between its start and its bookkeeping, and then has its successor: under this gate the queue is looked at only // when every task is pending, in flight (_inFlight) or already replaced by its successor, never in between. private readonly SemaphoreSlim _queueGate = new(1, 1); private EngineTask? _inFlight; private DateTimeOffset? _lastSweepAt; private bool _lastSweepLoaded; public SessionComposer(EngineServices services, IRecipeRunner runner, RecipeHealth health, RecentActions recent, Action manualDone) { _s = services; _runner = runner; _health = health; _recent = recent; _manualDone = manualDone; } private IClock Clock => _s.Clock; private IFebeStorage Store => _s.Storage; private EngineOptions Options => _s.Options; /// Runs the session. is a confirmed «Controlla ora» outside the window: then only that. public async Task 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) { await RunManualAsync(outsideWindowTask, accounts, state, ct).ConfigureAwait(false); return state.Summary(); } // 1. Commands of Alberto («Controlla ora», «Leggi l'elenco»). var due = await Store.Tasks.PeekDueAsync(Clock.Now, 1000, ct).ConfigureAwait(false); await RunManualTasksAsync(due, accounts, state, ct).ConfigureAwait(false); var light = _s.Random.Chance(Options.LightSessionProbability); var anyFollowed = accounts.Values.Any(a => a.IsFollowed && CheckPolicy.IsContactable(a)); // 2. Usually the Giro dei seguiti first. if (anyFollowed && _s.Random.Chance(Options.SweepProbability) && !Over(state, ct)) { var sweepTask = due.FirstOrDefault(t => t.Kind == EngineTaskKind.FollowingSweep && !t.IsManual); await RunSweepAsync(sweepTask, accounts, state, ct).ConfigureAwait(false); } // 3. Stories: flagged by the stories bar first, then the due ones (visits of profiles not followed included). if (!Over(state, ct)) { var now = Clock.Now; var window = await WindowLengthAsync(now, ct).ConfigureAwait(false); due = await Store.Tasks.PeekDueAsync(now, 1000, ct).ConfigureAwait(false); await RunManualTasksAsync(due, accounts, state, ct).ConfigureAwait(false); var storyWork = due.Where(t => !t.IsManual && IsStoryWork(t, accounts, now)).ToList(); var flagged = storyWork.Where(t => t.Priority >= Options.FlaggedStoriesPriority); var rest = storyWork.Where(t => t.Priority < Options.FlaggedStoriesPriority); foreach (var task in Weighted(flagged, accounts, now, window).Concat(Weighted(rest, accounts, now, window))) { if (Over(state, ct)) { break; } await RunScheduledAsync(task, accounts, state, ct).ConfigureAwait(false); } // Followed accounts whose stories no recent sweep has read (e.g. the home budget is exhausted). if (Options.SweepCoversFollowedStories && !Over(state, ct)) { var lastSweep = await LastSweepAsync().ConfigureAwait(false); now = Clock.Now; // Normally the next sweep will cover them: explicit checks only when no sweep can run, or after a long gap. var sweepUnavailable = _health.IsSuspended(RecipeKind.FollowingSweep) || !(await _s.Governor.EvaluateBudgetAsync(ActionKind.Home, null, ct).ConfigureAwait(false)).Granted; var factor = sweepUnavailable ? 1 : 2; var uncovered = accounts.Values .Where(a => a.IsFollowed && CheckPolicy.IsContactable(a) && CheckPolicy.WantsStories(a) && !state.Visited.Contains(a.Id)) .Select(a => (Account: a, Interval: CheckPolicy.StoryInterval(a, window, Options.MinimumStoryChecksPerDay))) .Where(x => Uncovered(x.Account, x.Interval * factor, lastSweep, now)) .Select(x => (x.Account, CheckPolicy.SelectionWeight(x.Account, now - Latest(x.Account.LastStoriesCheckAt, lastSweep) - x.Interval, x.Interval))) .ToList(); foreach (var account in CheckPolicy.WeightedOrder(_s.Random, uncovered)) { if (Over(state, ct)) { break; } await RunStoriesAsync(null, account, state, manual: false, visitNext: null, ct).ConfigureAwait(false); } } } // 4. Expired profile checks, highlight collections and other planned work. if (!light && !Over(state, ct)) { var now = Clock.Now; var window = await WindowLengthAsync(now, ct).ConfigureAwait(false); due = await Store.Tasks.PeekDueAsync(now, 1000, ct).ConfigureAwait(false); await RunManualTasksAsync(due, accounts, state, ct).ConfigureAwait(false); var other = due.Where(t => !t.IsManual && t.Kind is not (EngineTaskKind.FollowingSweep or EngineTaskKind.Backfill or EngineTaskKind.AccountStories) && !(t.AccountId is { } id && state.Visited.Contains(id))); foreach (var task in Weighted(other, accounts, now, window)) { if (Over(state, ct)) { break; } await RunScheduledAsync(task, accounts, state, ct).ConfigureAwait(false); } } // 5. A few pages of history on one account. if (!light && !Over(state, ct)) { await RunBackfillAsync(state, ct).ConfigureAwait(false); } return state.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 (); nothing towards the site. /// 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(); } } /// /// 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 /// «Storico in corso», a Giro dei seguiti when someone is followed; pending planned tasks of accounts that may not be /// contacted are cancelled. Commands of Alberto are left alone, and so is the task of the recipe in progress: its /// own bookkeeping plans the next one. /// public async Task> SyncQueueAsync(CancellationToken ct) { await _queueGate.WaitAsync(ct).ConfigureAwait(false); try { return await SyncQueueLockedAsync(ct).ConfigureAwait(false); } finally { _queueGate.Release(); } } private async Task> SyncQueueLockedAsync(CancellationToken ct) { var now = Clock.Now; var accounts = (await Store.Accounts.ListAsync(ct).ConfigureAwait(false)).ToDictionary(a => a.Id); var pending = await Store.Tasks.ListPendingAsync(10_000, ct).ConfigureAwait(false); var byAccount = pending.Where(t => t.AccountId is not null).ToLookup(t => t.AccountId!.Value); foreach (var account in accounts.Values) { var tasks = byAccount[account.Id].Where(t => !t.IsManual).ToList(); var wanted = WantedKinds(account); foreach (var task in tasks.Where(t => t.Kind != EngineTaskKind.HighlightCheck && !wanted.ContainsKey(t.Kind) && !(t.Kind == EngineTaskKind.AccountStories && t.Priority >= Options.FlaggedStoriesPriority && CheckPolicy.IsContactable(account)))) { await Store.Tasks.CancelAsync(task.Id, now, ct).ConfigureAwait(false); } foreach (var (kind, dueAt) in wanted) { if (tasks.Any(t => t.Kind == kind) || InFlight(account.Id, kind, null)) { continue; } await Store.Tasks.EnqueueAsync( new EngineTask { Kind = kind, AccountId = account.Id, DueAt = dueAt ?? now, CreatedAt = now }, ct).ConfigureAwait(false); } await SyncHighlightsAsync(account, tasks, now, ct).ConfigureAwait(false); } var anyFollowed = accounts.Values.Any(a => a.IsFollowed && CheckPolicy.IsContactable(a)); var sweeps = pending.Where(t => t.Kind == EngineTaskKind.FollowingSweep && !t.IsManual).ToList(); if (anyFollowed && sweeps.Count == 0) { await EnsureSweepTaskAsync(_s.Runtime.NextSessionAt ?? now, ct).ConfigureAwait(false); } else if (!anyFollowed) { foreach (var sweep in sweeps) { await Store.Tasks.CancelAsync(sweep.Id, now, ct).ConfigureAwait(false); } } return accounts; } /// /// «Raccolta in evidenza» (spec §6.3) for an account that wants «Storie in evidenza»: one task per collection the /// profile check found (payload: its Instagram identifier), due at once for a collection never looked at, then /// after . Tasks of collections that left the profile, or of accounts /// that no longer want them or may not be contacted, are cancelled. /// private async Task SyncHighlightsAsync(Account account, List tasks, DateTimeOffset now, CancellationToken ct) { var planned = tasks.Where(t => t.Kind == EngineTaskKind.HighlightCheck).ToList(); var collections = new Dictionary(StringComparer.Ordinal); if (CheckPolicy.IsContactable(account) && CheckPolicy.WantsHighlights(account)) { foreach (var highlight in await Store.Highlights.ListAsync(account.Id, includeRemoved: false, ct).ConfigureAwait(false)) { if (highlight.InstagramId is { Length: > 0 } id) { collections.TryAdd(id, highlight); } } } foreach (var task in planned.Where(t => t.Payload is null || !collections.ContainsKey(t.Payload))) { await Store.Tasks.CancelAsync(task.Id, now, ct).ConfigureAwait(false); } foreach (var (id, highlight) in collections) { if (planned.Any(t => t.Payload == id) || InFlight(account.Id, EngineTaskKind.HighlightCheck, id)) { continue; } var due = highlight.LastCheckedAt is { } last ? last + CheckPolicy.NextCheckDelay(_s.Random, CheckPolicy.HighlightInterval(account)) : now; await Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.HighlightCheck, AccountId = account.Id, Payload = id, DueAt = due, CreatedAt = now }, ct).ConfigureAwait(false); } } /// Whether the recipe in progress runs a task of this kind (and payload) for the account. Under the queue gate. private bool InFlight(long accountId, EngineTaskKind kind, string? payload) => _inFlight is { } task && task.AccountId == accountId && task.Kind == kind && (payload is null || task.Payload == payload); /// The Giro dei seguiti shown in the queue at the next session. public Task EnsureSweepTaskAsync(DateTimeOffset dueAt, CancellationToken ct) => Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.FollowingSweep, DueAt = dueAt, CreatedAt = Clock.Now }, ct); private Dictionary WantedKinds(Account account) { var wanted = new Dictionary(); if (!CheckPolicy.IsContactable(account)) { return wanted; } var profile = CheckPolicy.WantsProfileChecks(account); var stories = CheckPolicy.WantsStories(account); if (profile) { // A profile that is not followed shows its stories only on its page: one visit serves both checks. wanted[EngineTaskKind.ProfileCheck] = !account.IsFollowed && stories ? Earliest(account.NextProfileCheckAt, account.NextStoriesCheckAt) : account.NextProfileCheckAt; } // Followed accounts: under hypothesis H2 every sweep reads their stories bar, so only flagged checks are queued. if (stories && (account.IsFollowed ? !Options.SweepCoversFollowedStories : !profile)) { wanted[EngineTaskKind.AccountStories] = account.NextStoriesCheckAt; } if (account.State == AccountState.Backfilling && account.BackfillCompletedAt is null) { wanted[EngineTaskKind.Backfill] = null; } return wanted; } private static DateTimeOffset? Earliest(DateTimeOffset? a, DateTimeOffset? b) => a is null || b is null ? null : a < b ? a : b; private static bool IsStoryWork(EngineTask task, Dictionary accounts, DateTimeOffset now) { if (task.Kind == EngineTaskKind.AccountStories) { return true; } // The visit of a profile that is not followed is story work when its stories are due. return task.Kind == EngineTaskKind.ProfileCheck && task.AccountId is { } id && accounts.TryGetValue(id, out var account) && !account.IsFollowed && CheckPolicy.WantsStories(account) && (account.NextStoriesCheckAt is null || account.NextStoriesCheckAt <= now); } private IEnumerable Weighted(IEnumerable tasks, Dictionary accounts, DateTimeOffset now, TimeSpan window) { var items = new List<(EngineTask, double)>(); foreach (var task in tasks) { if (task.AccountId is { } id && accounts.TryGetValue(id, out var account)) { var interval = task.Kind == EngineTaskKind.AccountStories ? CheckPolicy.StoryInterval(account, window, Options.MinimumStoryChecksPerDay) : CheckPolicy.VisitInterval(account, window, Options.MinimumStoryChecksPerDay); items.Add((task, CheckPolicy.SelectionWeight(account, now - task.DueAt, interval))); } else { items.Add((task, 2)); } } return CheckPolicy.WeightedOrder(_s.Random, items); } private async Task WindowLengthAsync(DateTimeOffset now, CancellationToken ct) => (await _s.Planner.GetNextWindowAsync(now, ct).ConfigureAwait(false))?.Length ?? DefaultWindow; /// /// The session must not start anything else: stopped, cancelled, time up, or a stop, a pause or Alberto at the /// browser (checked here too, so the session ends at once even before its cancellation arrives). /// private bool Over(SessionState state, CancellationToken ct) => state.Stop || ct.IsCancellationRequested || Clock.Now >= state.Context.PlannedEnd || !_s.Breaker.State.AllowsSiteActions || _s.Runtime.IsPaused || _s.Runtime.UserIsDriving; /// Commands of Alberto that are due and not run yet in this session, first of all. private async Task RunManualTasksAsync(IReadOnlyList due, Dictionary accounts, SessionState state, CancellationToken ct) { foreach (var task in due.Where(t => t.IsManual && state.ManualDone.Add(t.Id)).OrderByDescending(t => t.Priority).ThenBy(t => t.DueAt)) { if (Over(state, ct)) { break; } await RunManualAsync(task, accounts, state, ct).ConfigureAwait(false); } } private async Task RunManualAsync(EngineTask task, Dictionary accounts, SessionState state, CancellationToken ct) { Account? account = null; if (task.AccountId is { } id && !accounts.TryGetValue(id, out account)) { await Store.Tasks.CancelAsync(task.Id, Clock.Now, CancellationToken.None).ConfigureAwait(false); return; } if (account is not null && !CheckPolicy.AllowsManualCheck(account)) { await Store.Tasks.CancelAsync(task.Id, Clock.Now, CancellationToken.None).ConfigureAwait(false); return; } state.Context.ManualAccountId = task.AccountId; try { switch (task.Kind) { case EngineTaskKind.ProfileCheck when account is not null: await RunVisitAsync(task, account, state, manual: true, ct).ConfigureAwait(false); break; case EngineTaskKind.AccountStories when account is not null: await RunStoriesAsync(task, account, state, manual: true, visitNext: null, ct).ConfigureAwait(false); break; default: await RunGenericAsync(task, account, state, manual: true, ct).ConfigureAwait(false); break; } } finally { state.Context.ManualAccountId = null; if (task.AccountId is { } done) { _manualDone(done); } } } private async Task RunScheduledAsync(EngineTask task, Dictionary accounts, SessionState state, CancellationToken ct) { Account? account = null; if (task.AccountId is { } id) { if (!accounts.TryGetValue(id, out account) || !CheckPolicy.IsContactable(account)) { await Store.Tasks.CancelAsync(task.Id, Clock.Now, CancellationToken.None).ConfigureAwait(false); return; } if (state.Visited.Contains(id) && task.Kind is EngineTaskKind.ProfileCheck or EngineTaskKind.AccountStories) { return; } // One highlight collection per account and session, and none on a profile already opened in this session: // the others wait for the next session instead of reopening the same profile again and again. if (task.Kind == EngineTaskKind.HighlightCheck && (state.Visited.Contains(id) || !state.HighlightAccounts.Add(id))) { return; } } switch (task.Kind) { case EngineTaskKind.ProfileCheck when account is not null: await RunVisitAsync(task, account, state, manual: false, ct).ConfigureAwait(false); break; case EngineTaskKind.AccountStories when account is not null: await RunStoriesAsync(task, account, state, manual: false, visitNext: null, ct).ConfigureAwait(false); break; case EngineTaskKind.FollowingSweep or EngineTaskKind.Backfill: break; default: await RunGenericAsync(task, account, state, manual: false, ct).ConfigureAwait(false); break; } } /// A visit of a profile: the profile check, then (if on) the tagged posts and, for profiles not followed or on request, the stories. private async Task RunVisitAsync(EngineTask task, Account account, SessionState state, bool manual, CancellationToken ct) { var outcome = await RunRecipeAsync(task, new RecipeRequest(RecipeKind.ProfileCheck, account.Id, Manual: manual), account, state, ct).ConfigureAwait(false); if (outcome?.Status != RecipeStatus.Completed) { return; } state.Visited.Add(account.Id); var visitNext = state.LastVisitNext; if ((account.ContentTypes & ContentTypes.Tagged) != 0 && !Over(state, ct)) { await RunRecipeAsync(null, new RecipeRequest(RecipeKind.Tagged, account.Id, MaxPages: 1, Manual: manual), account, state, ct).ConfigureAwait(false); } if (CheckPolicy.WantsStories(account) && (!account.IsFollowed || manual) && !Over(state, ct)) { await RunStoriesAsync(null, account, state, manual, account.IsFollowed ? null : visitNext, ct).ConfigureAwait(false); } } private async Task RunStoriesAsync(EngineTask? task, Account account, SessionState state, bool manual, DateTimeOffset? visitNext, CancellationToken ct) { state.PendingStoriesNext = visitNext; try { var outcome = await RunRecipeAsync(task, new RecipeRequest(RecipeKind.AccountStories, account.Id, Manual: manual), account, state, ct).ConfigureAwait(false); if (outcome?.Status == RecipeStatus.Completed) { state.Visited.Add(account.Id); } } finally { state.PendingStoriesNext = null; } } private async Task RunGenericAsync(EngineTask task, Account? account, SessionState state, bool manual, CancellationToken ct) { var kind = CheckPolicy.RecipeOf(task.Kind); var request = kind switch { RecipeKind.SinglePost => new RecipeRequest(kind, task.AccountId, EntryId: ParseLong(task.Payload), Manual: manual), RecipeKind.Highlight => new RecipeRequest(kind, task.AccountId, HighlightId: task.Payload, Manual: manual), RecipeKind.Tagged or RecipeKind.Reels => new RecipeRequest(kind, task.AccountId, MaxPages: 1, Manual: manual), _ => new RecipeRequest(kind, task.AccountId, Manual: manual), }; await RunRecipeAsync(task, request, account, state, ct).ConfigureAwait(false); } private async Task RunSweepAsync(EngineTask? task, Dictionary accounts, SessionState state, CancellationToken ct) { var outcome = await RunRecipeAsync(task, new RecipeRequest(RecipeKind.FollowingSweep), null, state, ct).ConfigureAwait(false); if (outcome?.Status != RecipeStatus.Completed) { return; } var now = Clock.Now; var flagged = new HashSet(outcome.AccountsWithStories); foreach (var id in flagged) { if (accounts.TryGetValue(id, out var account) && CheckPolicy.IsContactable(account) && CheckPolicy.WantsStories(account)) { await Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.AccountStories, AccountId = id, DueAt = now, Priority = Options.FlaggedStoriesPriority, CreatedAt = now, }, CancellationToken.None).ConfigureAwait(false); } } // Hypothesis H2: the stories bar showed every followed profile with active stories; the others count as checked. // One timestamp is enough: story checks of followed accounts that come due are postponed while it is recent. _lastSweepAt = now; await Store.Settings.SetInt64Async(EngineSettingKeys.LastSweep, now.ToUnixTimeMilliseconds(), CancellationToken.None).ConfigureAwait(false); } /// A followed account needs an explicit story check when neither a sweep nor a check covered its interval. private static bool Uncovered(Account account, TimeSpan interval, DateTimeOffset? lastSweep, DateTimeOffset now) => now - Latest(account.LastStoriesCheckAt, lastSweep) >= interval; private static DateTimeOffset Latest(DateTimeOffset? a, DateTimeOffset? b) => (a, b) switch { (null, null) => DateTimeOffset.MinValue, (null, { } y) => y, ({ } x, null) => x, ({ } x, { } y) => x > y ? x : y, }; private async Task LastSweepAsync() { if (_lastSweepLoaded) { return _lastSweepAt; } var stored = await Store.Settings.GetInt64Async(EngineSettingKeys.LastSweep, CancellationToken.None).ConfigureAwait(false); _lastSweepAt ??= stored is { } ms ? DateTimeOffset.FromUnixTimeMilliseconds(ms) : null; _lastSweepLoaded = true; return _lastSweepAt; } /// /// 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). /// private async Task RunBackfillAsync(SessionState state, CancellationToken ct) { if (_health.IsSuspended(RecipeKind.Backfill) || state.Exhausted.Contains(ActionKind.BackfillPage)) { return; } // 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) { return; } var now = Clock.Now; var zone = Clock.LocalZone; var today = LocalCalendar.Today(now, zone); if (await _s.Governor.RemainingTodayAsync(ct).ConfigureAwait(false) <= 0) { await NoticeOnceAsync( EngineEventCodes.BackfillWaiting, null, today, "Storico sospeso: raggiunto il tetto dei dati scaricati di oggi. Riprende domani.", ct).ConfigureAwait(false); return; } var tasks = (await Store.Tasks.ListPendingAsync(10_000, ct).ConfigureAwait(false)) .Where(t => t.Kind == EngineTaskKind.Backfill && !t.IsManual && t.AccountId is not null) .GroupBy(t => t.AccountId!.Value) .ToDictionary(g => g.Key, g => g.First()); var ordered = candidates .OrderBy(a => tasks.TryGetValue(a.Id, out var t) ? t.DueAt : now) .ThenBy(a => a.Id); var (dayStart, dayEnd) = LocalCalendar.DayBounds(now, zone); foreach (var account in ordered) { var pendingFiles = await Store.Media.CountPendingDownloadsAsync(account.Id, ct).ConfigureAwait(false); if (pendingFiles > Options.BackfillMaxPendingDownloads) { await NoticeOnceAsync( EngineEventCodes.BackfillWaiting, account.Id, today, $"Storico di {ItalianText.Handle(account.Username)} in attesa: {ItalianText.Number(pendingFiles)} file da scaricare. Riprende quando la coda scende sotto {ItalianText.Number(Options.BackfillMaxPendingDownloads)}.", ct).ConfigureAwait(false); continue; } var permit = await _s.Governor.EvaluateBudgetAsync(ActionKind.BackfillPage, account.Id, ct).ConfigureAwait(false); if (!permit.Granted) { if (permit.Reason is DenialReason.HourlyBudgetReached or DenialReason.DailyBudgetReached) { state.Exhausted.Add(ActionKind.BackfillPage); return; } continue; } var budgets = _s.Governor.GetEffectiveBudgets(now); var limit = Math.Min(account.Limits.BackfillPostsPerDay, budgets.BackfillPostsPerAccountPerDay); var postsToday = await Store.Entries.CountDiscoveredAsync(account.Id, EntryKind.Post, dayStart, dayEnd, ct).ConfigureAwait(false); var perPage = Math.Max(1, Options.PostsPerBackfillPage); var pagesAllowed = Math.Max(1, (int)Math.Ceiling((limit - postsToday) / (double)perPage)); var pages = Math.Min(_s.Random.CountBetween(Options.BackfillPagesMin, Options.BackfillPagesMax), pagesAllowed); tasks.TryGetValue(account.Id, out var task); await RunRecipeAsync(task, new RecipeRequest(RecipeKind.Backfill, account.Id, MaxPages: pages), account, state, ct).ConfigureAwait(false); return; } } /// Runs one recipe with every check around it, then records its outcome. Null when it did not run. private async Task RunRecipeAsync(EngineTask? task, RecipeRequest request, Account? account, SessionState state, CancellationToken ct) { if (_health.IsSuspended(request.Kind)) { return null; } var category = CheckPolicy.CategoryOf(request.Kind); if (state.Exhausted.Contains(category)) { return null; } var budget = await _s.Governor.EvaluateBudgetAsync(category, request.AccountId, ct).ConfigureAwait(false); if (!budget.Granted) { if (budget.Reason is DenialReason.HourlyBudgetReached or DenialReason.DailyBudgetReached) { state.Exhausted.Add(category); } return null; } var context = state.Context; if (state.Recipes > 0) { var pace = GesturePaceFactors.For(_s.Runtime.Profile.Pace); var wait = _s.Random.Between(Options.BetweenRecipesMin, Options.BetweenRecipesMax) * (context.Mood * pace); await Clock.Delay(wait, ct).ConfigureAwait(false); if (Over(state, ct) && !request.Manual) { return null; } } if (!_s.Breaker.State.AllowsSiteActions || _s.Runtime.IsPaused || _s.Runtime.UserIsDriving) { state.Stop = true; return null; } if (task is not null && !await StartTaskAsync(task, ct).ConfigureAwait(false)) { return null; } state.Recipes++; _s.Runtime.Activity = ActivityText(request.Kind, account); _s.Events.Publish(EngineNotificationKind.ActivityChanged); RecipeOutcome outcome; try { outcome = await _runner.RunAsync(request, context, ct).ConfigureAwait(false); } catch (OperationCanceledException) { if (task is not null) { await _queueGate.WaitAsync(CancellationToken.None).ConfigureAwait(false); try { await RequeueAsync(task, "Interrotta", Clock.Now).ConfigureAwait(false); } finally { _inFlight = null; _queueGate.Release(); } } throw; } catch (Exception ex) when (ex is not OutOfMemoryException) { // The message of an unexpected exception may carry data of the page: only its type is kept. outcome = new RecipeOutcome(RecipeStatus.Failed, 0, 0, $"Errore imprevisto ({ex.GetType().Name})."); } finally { _s.Runtime.Activity = null; } context.AddNewEntries(outcome.NewEntries); await _queueGate.WaitAsync(CancellationToken.None).ConfigureAwait(false); try { await ApplyOutcomeAsync(task, request, account, outcome, state).ConfigureAwait(false); } finally { _inFlight = null; _queueGate.Release(); } _s.Events.Publish(EngineNotificationKind.ActivityChanged); return outcome; } /// Pending → Running, recorded as the task in flight under the queue gate. False when it was not pending. private async Task StartTaskAsync(EngineTask task, CancellationToken ct) { await _queueGate.WaitAsync(ct).ConfigureAwait(false); try { if (!await Store.Tasks.TryStartAsync(task.Id, Clock.Now, ct).ConfigureAwait(false)) { return false; } _inFlight = task; return true; } finally { _queueGate.Release(); } } private async Task ApplyOutcomeAsync(EngineTask? task, RecipeRequest request, Account? account, RecipeOutcome outcome, SessionState state) { // Bookkeeping always completes, even when the session has just been cancelled. Independent writes are queued // together (the single writer keeps their order and commits them in one transaction). var ct = CancellationToken.None; var now = Clock.Now; var kind = request.Kind; var writes = new List(); // 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: await _health.RecordSuccessAsync(kind, ct).ConfigureAwait(false); await OnCheckedAsync(task, request, account, outcome, state, now, writes, ct).ConfigureAwait(false); break; case RecipeStatus.Denied: if (task is not null) { await RequeueAsync(task, "Rimandata: tetto raggiunto", now).ConfigureAwait(false); } var recheck = await _s.Governor.EvaluateBudgetAsync(CheckPolicy.CategoryOf(kind), request.AccountId, ct).ConfigureAwait(false); if (recheck.Reason is DenialReason.HourlyBudgetReached or DenialReason.DailyBudgetReached) { state.Exhausted.Add(CheckPolicy.CategoryOf(kind)); } else if (recheck.Reason is DenialReason.BreakerOpen or DenialReason.OutsideActiveWindow or DenialReason.Paused or DenialReason.UserIsDriving) { state.Stop = true; } break; case RecipeStatus.StoppedBySignal: case RecipeStatus.Cancelled: 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); } state.Stop = true; break; case RecipeStatus.TargetNotFound: if (task is not null) { await RequeueAsync(task, "Bersaglio non trovato", now).ConfigureAwait(false); } await RecordRecipeFailureAsync(kind, account, EngineEventCodes.TargetNotFound, $"Non trovo nella pagina ciò che serve per {RecipeName(kind)}{Of(account)}: mi fermo e lo segnalo, senza cliccare a caso.", now).ConfigureAwait(false); state.Stop = true; break; case RecipeStatus.UnrecognizedFormat: // The check took place: plan the next one as usual instead of insisting. await OnCheckedAsync(task, request, account, outcome, state, now, writes, ct).ConfigureAwait(false); if (account is not null) { await Store.Accounts.AddNoticesAsync(account.Id, AccountNotices.UnrecognizedFormat, now, ct).ConfigureAwait(false); } await RecordRecipeFailureAsync(kind, account, EngineEventCodes.FormatUnrecognized, $"Formato non riconosciuto durante {RecipeName(kind)}{Of(account)}: Instagram potrebbe aver cambiato le sue risposte.", now).ConfigureAwait(false); break; case RecipeStatus.AccountUnreachable: if (task is not null) { await Store.Tasks.CompleteAsync(task.Id, now, ct).ConfigureAwait(false); } if (account is not null) { if (account.State != AccountState.Unreachable) { await Store.Accounts.SetStateAsync(account.Id, AccountState.Unreachable, now, account.UnreachableReason, ct).ConfigureAwait(false); } await Store.Tasks.CancelForAccountAsync(account.Id, now, ct).ConfigureAwait(false); await _s.Events.WriteAsync( EventSeverity.Warning, EngineEventCodes.AccountUnreachable, $"{ItalianText.Handle(account.Username)} non è raggiungibile (privato non seguito, inesistente o bloccato): Febe smette di controllarlo. Lo trovi fra gli account «Da controllare».", account.Id, state.Context.SessionId, ct).ConfigureAwait(false); } break; default: if (task is not null) { await Store.Tasks.FailAsync(task.Id, now, outcome.Message ?? "Non riuscita", now + TimeSpan.FromHours(1), ct).ConfigureAwait(false); } await _s.Events.WriteAsync( EventSeverity.Warning, EngineEventCodes.RecipeFailed, $"{ItalianText.Capitalize(RecipeName(kind))}{Of(account)} non è riuscito: {outcome.Message ?? "errore sconosciuto"}. Riprovo più tardi.", account?.Id, state.Context.SessionId, ct).ConfigureAwait(false); break; } var (title, detail) = Describe(kind, account, outcome); _recent.Add(new RecentAction(now, kind, account?.Id, title, detail, outcome.Status, outcome.NewEntries)); var severity = outcome.Status switch { RecipeStatus.Completed => outcome.NewEntries > 0 ? EventSeverity.Success : EventSeverity.Info, RecipeStatus.Denied or RecipeStatus.Cancelled => EventSeverity.Info, _ => EventSeverity.Warning, }; writes.Add(_s.Events.WriteAsync(severity, EngineEventCodes.RecipePrefix + kind, title + ": " + detail, account?.Id, state.Context.SessionId, ct)); await Task.WhenAll(writes).ConfigureAwait(false); } /// Check times, next task and state changes after a check that took place. private async Task OnCheckedAsync( EngineTask? task, RecipeRequest request, Account? account, RecipeOutcome outcome, SessionState state, DateTimeOffset now, List writes, CancellationToken ct) { if (task is not null) { writes.Add(Store.Tasks.CompleteAsync(task.Id, now, ct)); } if (account is null) { return; } var window = await WindowLengthAsync(now, ct).ConfigureAwait(false); var contactable = CheckPolicy.IsContactable(account); switch (request.Kind) { case RecipeKind.ProfileCheck: { // A profile not followed is visited at the earlier of its post check and its story check. var next = now + CheckPolicy.NextCheckDelay(_s.Random, CheckPolicy.ProfileCheckInterval(account)); DateTimeOffset? storiesNext = !account.IsFollowed && CheckPolicy.WantsStories(account) ? now + CheckPolicy.NextStoryCheckDelay(_s.Random, CheckPolicy.StoryInterval(account, window, Options.MinimumStoryChecksPerDay)) : null; state.LastVisitNext = storiesNext; var due = storiesNext is { } sn && sn < next ? sn : next; writes.Add(Store.Accounts.UpdateChecksAsync(account.Id, new AccountCheckTimes { LastProfileCheckAt = now, NextProfileCheckAt = next }, ct)); if (contactable) { writes.Add(Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.ProfileCheck, AccountId = account.Id, DueAt = due, CreatedAt = now }, ct)); } if (!account.KeepUpdated && account.State == AccountState.UpToDate) { await PauseAfterSingleDownloadAsync(account, now, state, ct).ConfigureAwait(false); } break; } case RecipeKind.AccountStories: { var next = state.PendingStoriesNext ?? now + CheckPolicy.NextStoryCheckDelay(_s.Random, CheckPolicy.StoryInterval(account, window, Options.MinimumStoryChecksPerDay)); writes.Add(Store.Accounts.UpdateChecksAsync(account.Id, new AccountCheckTimes { LastStoriesCheckAt = now, NextStoriesCheckAt = next }, ct)); if (contactable && (account.IsFollowed ? !Options.SweepCoversFollowedStories : !CheckPolicy.WantsProfileChecks(account))) { writes.Add(Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.AccountStories, AccountId = account.Id, DueAt = next, CreatedAt = now }, ct)); } break; } // A history completed: FinishBackfillAsync, from ApplyOutcomeAsync. case RecipeKind.Backfill when !outcome.BackfillFinished: if (contactable) { // Behind the other accounts «Storico in corso»: they alternate. writes.Add(Store.Tasks.EnqueueAsync( new EngineTask { Kind = EngineTaskKind.Backfill, AccountId = account.Id, DueAt = now, CreatedAt = now }, ct)); } break; } } /// /// 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. /// 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); // 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); await Store.Tasks.CancelForAccountAsync(account.Id, now, ct).ConfigureAwait(false); await _s.Events.WriteAsync( EventSeverity.Info, EngineEventCodes.AccountPaused, $"{ItalianText.Handle(account.Username)}: scaricamento unico fatto. «Tieni aggiornato» è spento, quindi l'account resta in pausa.", account.Id, state.Context.SessionId, ct).ConfigureAwait(false); } private async Task RecordRecipeFailureAsync(RecipeKind kind, Account? account, string code, string message, DateTimeOffset now) { var ct = CancellationToken.None; var suspended = await _health.RecordFailureAsync(kind, now, ct).ConfigureAwait(false); var count = _health.Failures(kind); var threshold = Options.ConsecutiveFailuresToSuspend; await _s.Events.WriteAsync( EventSeverity.Warning, code, message + (count < threshold ? $" ({ItalianText.Number(count)} di {ItalianText.Number(threshold)} di fila)" : string.Empty), account?.Id, ct: ct).ConfigureAwait(false); if (suspended) { await _s.Events.WriteAsync( EventSeverity.Error, EngineEventCodes.RecipeSuspended, $"«{ItalianText.Capitalize(RecipeName(kind))}» è sospeso dopo {ItalianText.Number(threshold)} problemi di fila: Instagram potrebbe essere cambiato. Serve un campione nuovo con la modalità sonda; poi si riattiva da Attività.", account?.Id, ct: ct).ConfigureAwait(false); } } private async Task RequeueAsync(EngineTask task, string reason, DateTimeOffset retryAt) => await Store.Tasks.FailAsync(task.Id, Clock.Now, reason, retryAt, CancellationToken.None).ConfigureAwait(false); private async Task NoticeOnceAsync(string code, long? accountId, DateOnly day, string message, CancellationToken ct) { lock (_noticeGate) { if (!_notices.Add((code, accountId, day))) { return; } } await _s.Events.WriteAsync(EventSeverity.Info, code, message, accountId, ct: ct).ConfigureAwait(false); } private static long? ParseLong(string? text) => long.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var value) ? value : null; private static string Of(Account? account) => account is null ? string.Empty : " di " + ItalianText.Handle(account.Username); /// Italian name of a recipe, lower case: «il controllo del profilo». internal static string RecipeName(RecipeKind kind) => kind switch { RecipeKind.FollowingSweep => "il giro dei profili che segui", RecipeKind.AccountStories => "le storie", RecipeKind.ProfileCheck => "il controllo del profilo", RecipeKind.Backfill => "lo storico", RecipeKind.Highlight => "le storie in evidenza", RecipeKind.Tagged => "i post in cui è taggato", RecipeKind.Reels => "i reel", RecipeKind.SinglePost => "il post singolo", RecipeKind.FollowingList => "l'elenco dei profili che segui", _ => kind.ToString(), }; /// What the engine is doing, for the monitor: «Sto guardando le storie di @marta.demo». internal static string ActivityText(RecipeKind kind, Account? account) { var who = ItalianText.Handle(account?.Username); return kind switch { RecipeKind.FollowingSweep => "Sto facendo il giro dei profili che segui", RecipeKind.AccountStories => $"Sto guardando le storie di {who}", RecipeKind.ProfileCheck => $"Sto controllando il profilo di {who}", RecipeKind.Backfill => $"Sto scorrendo lo storico di {who}", RecipeKind.Highlight => $"Sto guardando le storie in evidenza di {who}", RecipeKind.Tagged => $"Sto guardando i post in cui è taggato {who}", RecipeKind.Reels => $"Sto guardando i reel di {who}", RecipeKind.SinglePost => $"Sto aprendo un post di {who}", RecipeKind.FollowingList => "Sto leggendo l'elenco dei profili che segui", _ => "Sto lavorando", }; } internal static (string Title, string Detail) Describe(RecipeKind kind, Account? account, RecipeOutcome outcome) { var title = kind switch { RecipeKind.FollowingSweep => "Giro dei profili che segui", RecipeKind.FollowingList => "Elenco dei profili che segui", _ => ItalianText.Handle(account?.Username), }; var detail = outcome.Status switch { RecipeStatus.Completed => kind switch { RecipeKind.FollowingSweep => SweepDetail(outcome), RecipeKind.AccountStories => outcome.NewEntries > 0 ? ItalianText.Count(outcome.NewEntries, "storia nuova", "storie nuove") : "Nessuna storia nuova", RecipeKind.Backfill => outcome.BackfillFinished ? "Storico completato" : "Storico, " + ItalianText.Count(outcome.NewEntries, "post", "post"), RecipeKind.Highlight => outcome.NewEntries > 0 ? "In evidenza, " + ItalianText.Count(outcome.NewEntries, "contenuto nuovo", "contenuti nuovi") : "In evidenza, nessuna novità", RecipeKind.Tagged => outcome.NewEntries > 0 ? "Taggati, " + ItalianText.Count(outcome.NewEntries, "post nuovo", "post nuovi") : "Taggati, nessuna novità", RecipeKind.FollowingList => "Lettura completata", _ => outcome.NewEntries > 0 ? ItalianText.Count(outcome.NewEntries, "contenuto nuovo", "contenuti nuovi") : "Nessuna novità", }, RecipeStatus.Denied => "Rimandato: tetto raggiunto", RecipeStatus.StoppedBySignal => "Fermato da un segnale di Instagram", RecipeStatus.TargetNotFound => "Bersaglio non trovato nella pagina", RecipeStatus.UnrecognizedFormat => "Formato non riconosciuto", RecipeStatus.AccountUnreachable => "Non raggiungibile", RecipeStatus.Cancelled => "Interrotto", _ => "Non riuscito", }; return (title, detail); } private static string SweepDetail(RecipeOutcome outcome) { var parts = new List(); if (outcome.AccountsWithNewPosts.Count > 0) { parts.Add(ItalianText.Count(outcome.AccountsWithNewPosts.Count, "account con novità", "account con novità")); } else if (outcome.NewEntries > 0) { parts.Add(ItalianText.Count(outcome.NewEntries, "contenuto nuovo", "contenuti nuovi")); } if (outcome.AccountsWithStories.Count > 0) { parts.Add(ItalianText.Count(outcome.AccountsWithStories.Count, "con storie da vedere", "con storie da vedere")); } return parts.Count == 0 ? "Nessuna novità" : string.Join(", ", parts); } private sealed class SessionState(SessionContext context) { public SessionContext Context { get; } = context; public HashSet Exhausted { get; } = []; /// Accounts whose profile or stories were checked in this session. public HashSet Visited { get; } = []; /// Commands of Alberto already taken up in this session. public HashSet ManualDone { get; } = []; /// Accounts whose highlight collection was taken up in this session (one per account and session). public HashSet HighlightAccounts { get; } = []; public bool Stop { get; set; } public int Recipes { get; set; } /// Next story check planned by the last visit of a profile not followed. public DateTimeOffset? LastVisitNext { get; set; } /// Set while the stories of a visit run: their next check is the visit's. public DateTimeOffset? PendingStoriesNext { get; set; } public SessionSummary Summary() => new(Recipes, Stop); } } /// What a session did, for its row and its timeline line. internal sealed record SessionSummary(int Recipes, bool StoppedEarly);