Files
Febe/src/Febe.Core/Collection/Recipes/RecipeRunner.cs
T
Alby96andClaude Opus 5.5 9941a2fa1f
verifica / controlli (push) Successful in 20s
verifica / prove (push) Failing after 19m40s
verifica / installatore (push) Skipped
verifica / immagine (push) Skipped
Ricerca dei profili con suggerimenti e foto (#37)
«Aggiungi account» ha una sola casella: mentre Alberto scrive compaiono
subito i suggerimenti locali, poi i profili di Instagram con la foto, come
chiesto in #37. Il browser di Febe scrive nella casella «Cerca» di Instagram
con tasti veri da una mappa chiusa (mai Invio, Tab o Esc), controlla prima
il fuoco, legge la risposta della pagina e non clicca mai un risultato.

La ricerca ha una categoria propria nel Governatore, con tetti larghi che
non consumano quelli della raccolta, e si dà il turno con il motore
sull'unica pagina; a fine sessione la pagina torna comunque al riposo. Le
foto passano dal CDN con un permesso ciascuna e restano solo in memoria.

Le risposte di Alberto del 15 novembre (controllo del fuoco, turni, evento
requestWillBeSent, niente foto dei seguiti per ora) sono in §6.1 e in
docs/decisioni.md. Su Instagram vero la ricerca parte quando i bersagli
search-open e search-box saranno raccolti dal vivo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:30:03 +01:00

387 lines
18 KiB
C#

using Febe.Core.Abstractions;
using Febe.Core.Collection.Diagnostics;
using Febe.Core.Collection.Readers;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Storage;
namespace Febe.Core.Collection.Recipes;
/// <summary>Everything the recipes work with.</summary>
public sealed record RecipeEnvironment
{
/// <summary>The page, through the browser (or a fake in tests and simulation).</summary>
public required IPageDriver Driver { get; init; }
/// <summary>What the page receives, read by the site's readers.</summary>
public required CaptureCollector Capture { get; init; }
/// <summary>The single point every counted action passes through (rule 7).</summary>
public required IGovernor Governor { get; init; }
/// <summary>Stop signals: raised by the capture, reported by the recipes (empty pages, session cookie).</summary>
public required IStopSignals Signals { get; init; }
public required IFebeStorage Storage { get; init; }
/// <summary>Pages and targets of the site (<c>bersagli.*.json</c>).</summary>
public required SiteMap SiteMap { get; init; }
public required IClock Clock { get; init; }
public required IRandomSource Random { get; init; }
/// <summary>Remaining budgets (stories need at least ten). Null: the check is left to the Governor alone.</summary>
public IBudgetView? Budget { get; init; }
/// <summary>Daily data budget: the backfill stops when it is used up (spec §6.3). Null: not checked here.</summary>
public IDataBudget? DataBudget { get; init; }
/// <summary>The circuit breaker, to stop when it opens. Null: only the signals of the run count.</summary>
public ICircuitBreaker? Breaker { get; init; }
public RecipeOptions Options { get; init; } = new();
/// <summary>The recorder of the diagnostic report (issue #39). Null: nothing is recorded.</summary>
public CollectionDiagnostics? Diagnostics { get; init; }
/// <summary>
/// The turns of the page shared with the profile search (issue #37): recipes give the page to the search before an
/// action that opens a page, when the search waits. Null: no search, no turn points.
/// </summary>
public PageTurns? PageTurns { get; init; }
}
/// <summary>
/// The recipes of spec §6.3 over a page driver: Giro dei seguiti, Storie di un account, Controllo del profilo, Storico,
/// Raccolta in evidenza, Taggati, Reel, Post singolo, Elenco dei seguiti.
/// </summary>
/// <remarks>
/// Common rules: the Governor is asked before every counted action and a refusal stops the recipe in order; a stop
/// signal stops it where it is and leaves the page as it is; a missing target stops it with a report, never with
/// coordinates; an unknown response shape stops it with <see cref="RecipeStatus.UnrecognizedFormat"/> and an event
/// that carries only the key structure. A recipe refuses to run, as <see cref="RecipeStatus.NotConfigured"/>, while the
/// site's map lacks a page or target it needs or the reader vocabulary is not usable (Instagram's targets, H11).
/// </remarks>
public sealed partial class RecipeRunner : IRecipeRunner
{
private readonly Lock _refusalsGate = new();
private readonly HashSet<RecipeKind> _refusalsReported = [];
private long? _sessionId;
private RecipeKind? _lastKind;
public RecipeRunner(RecipeEnvironment environment)
{
Env = environment ?? throw new ArgumentNullException(nameof(environment));
Writer = new CollectionWriter(environment.Storage, environment.Clock) { Diagnostics = environment.Diagnostics };
}
public RecipeEnvironment Env { get; }
public CollectionWriter Writer { get; }
/// <summary>Italian name of a recipe, for messages and events.</summary>
public static string Name(RecipeKind kind) => kind switch
{
RecipeKind.FollowingSweep => "Giro dei seguiti",
RecipeKind.AccountStories => "Storie di un account",
RecipeKind.ProfileCheck => "Controllo del profilo",
RecipeKind.Backfill => "Storico",
RecipeKind.Highlight => "Raccolta in evidenza",
RecipeKind.Tagged => "Taggati",
RecipeKind.Reels => "Reel",
RecipeKind.SinglePost => "Post singolo",
RecipeKind.FollowingList => "Elenco dei seguiti",
_ => kind.ToString(),
};
/// <summary>
/// Pages and targets a recipe cannot work without. «profile-not-found» is not among them: it is looked up only when
/// the map sets it, and without it a profile that does not exist gives no data, judged like any page that arrives
/// empty.
/// </summary>
public static (IReadOnlyList<string> Pages, IReadOnlyList<string> Targets) Requirements(RecipeKind kind) => kind switch
{
RecipeKind.FollowingSweep => ([SitePages.Home], []),
RecipeKind.AccountStories => ([SitePages.Profile, SitePages.StoryViewer], [SiteTargets.ProfileStoryRing, SiteTargets.ViewerNext, SiteTargets.ViewerClose]),
RecipeKind.ProfileCheck or RecipeKind.Backfill => ([SitePages.Profile], []),
RecipeKind.Highlight => ([SitePages.Profile, SitePages.HighlightViewer], [SiteTargets.ProfileHighlight, SiteTargets.ViewerNext, SiteTargets.ViewerClose]),
RecipeKind.Tagged => ([SitePages.Profile], [SiteTargets.ProfileTabTagged]),
RecipeKind.Reels => ([SitePages.Profile], [SiteTargets.ProfileTabReels]),
RecipeKind.SinglePost => ([SitePages.Post], []),
RecipeKind.FollowingList => ([SitePages.Following], []),
_ => ([], []),
};
/// <summary>Italian description of a stop signal.</summary>
public static string Describe(Signal signal) => signal.Level switch
{
SignalLevel.Throttle => $"Segnale di rallentamento ({signal.Code}): la ricetta si ferma dov'è.",
SignalLevel.Challenge => $"Il sito chiede una verifica ({signal.Code}): Febe si ferma e lascia la pagina com'è.",
SignalLevel.Disconnect => $"La sessione è terminata ({signal.Code}): Febe si ferma e non tenta di rientrare.",
_ => $"Segnale {signal.Code}.",
};
public async Task<RecipeOutcome> RunAsync(RecipeRequest request, ISessionContext session, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(session);
var name = Name(request.Kind);
// A map or a vocabulary that lacks what the recipe needs is a state of the configuration, not a failure of the
// recipe: nothing is done, nothing counts towards suspension, and it is said once per recipe and process (the
// engine stops planning it, NotConfiguredReason).
if (Refusal(request.Kind) is { } refusal)
{
bool first;
lock (_refusalsGate)
{
first = _refusalsReported.Add(request.Kind);
}
if (first)
{
await Writer.EventAsync(refusal.Code, refusal.Message + SessionComposer.NotConfiguredTail, EventSeverity.Warning, null, session.SessionId, ct).ConfigureAwait(false);
}
Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, RecipeStatus.NotConfigured, null, 0, 0);
return new RecipeOutcome(RecipeStatus.NotConfigured, 0, 0, refusal.Message);
}
if (Env.Breaker is { } breaker && !breaker.State.AllowsSiteActions)
{
Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, RecipeStatus.Denied, breaker.State.LastSignal?.Code, 0, 0);
return new RecipeOutcome(RecipeStatus.Denied, 0, 0, "L'interruttore è aperto: nessuna azione verso il sito.");
}
if (_sessionId != session.SessionId)
{
Env.Driver.BeginSession(session.Mood);
_sessionId = session.SessionId;
_lastKind = null;
}
using var run = new RecipeRun(this, request, session, ct);
RecipeStatus status;
string? text = null;
string? errorType = null;
try
{
await CheckSessionCookieAsync(run).ConfigureAwait(false);
status = request.Kind switch
{
RecipeKind.FollowingSweep => await FollowingSweepAsync(run).ConfigureAwait(false),
RecipeKind.AccountStories => await AccountStoriesAsync(run).ConfigureAwait(false),
RecipeKind.ProfileCheck => await ProfileCheckAsync(run).ConfigureAwait(false),
RecipeKind.Backfill => await WalkAsync(run, ProfileWalk.Posts).ConfigureAwait(false),
RecipeKind.Tagged => await WalkAsync(run, ProfileWalk.Tagged).ConfigureAwait(false),
RecipeKind.Reels => await WalkAsync(run, ProfileWalk.Reels).ConfigureAwait(false),
RecipeKind.Highlight => await HighlightAsync(run).ConfigureAwait(false),
RecipeKind.SinglePost => await SinglePostAsync(run).ConfigureAwait(false),
RecipeKind.FollowingList => await FollowingListAsync(run).ConfigureAwait(false),
_ => throw RecipeRun.Stop(RecipeStatus.Failed, $"Ricetta sconosciuta: {request.Kind}."),
};
}
catch (RecipeStopException stop)
{
status = stop.Status;
text = stop.Message;
errorType = stop.ErrorType;
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
status = RecipeStatus.Cancelled;
text = "Ricetta interrotta.";
}
catch (OperationCanceledException ex)
{
// E.g. Alberto took the browser: the gesture was not performed.
status = RecipeStatus.Cancelled;
text = ex.Message;
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
status = RecipeStatus.Failed;
text = $"Errore imprevisto in «{name}» ({ex.GetType().Name}): {ex.Message}";
errorType = ex.GetType().Name;
}
if (run.Signal is { } signal && status is not (RecipeStatus.StoppedBySignal or RecipeStatus.Cancelled))
{
status = RecipeStatus.StoppedBySignal;
text = Describe(signal);
}
try
{
await ReportAsync(run, status, text, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException and not OutOfMemoryException)
{
// The timeline is a convenience: failing to write it never changes how the recipe ended.
}
// The report takes the status and the code of the signal, never the message (it may name an account). A recipe
// cancelled because the breaker stopped (a signal reported straight to it, like the session cookie lost) gets
// the breaker's code too.
var breakerState = Env.Breaker?.State;
var signalCode = run.Signal?.Code ?? status switch
{
RecipeStatus.StoppedBySignal => breakerState?.LastSignal?.Code,
RecipeStatus.Cancelled when breakerState is { AllowsSiteActions: false } => breakerState.LastSignal?.Code,
_ => null,
};
Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, status, signalCode, run.Actions, run.NewEntries, status == RecipeStatus.Failed ? errorType : null);
_lastKind = request.Kind;
return new RecipeOutcome(status, run.Actions, run.NewEntries, text)
{
AccountsWithStories = [.. run.AccountsWithStories],
AccountsWithNewPosts = [.. run.AccountsWithNewPosts],
BackfillFinished = run.Finished,
};
}
/// <inheritdoc/>
public string? NotConfiguredReason(RecipeKind kind) => Refusal(kind)?.Message;
/// <summary>
/// Why the site's configuration does not let <paramref name="kind"/> run, with the code of its timeline line: pages
/// or targets missing from the map, or a reader vocabulary that cannot be relied on. Null when it can run.
/// </summary>
private (string Code, string Message)? Refusal(RecipeKind kind)
{
var name = Name(kind);
var (pages, targets) = Requirements(kind);
var missing = Env.SiteMap.Missing(pages, targets);
if (missing.Count > 0)
{
return ("mappa-incompleta", $"«{name}» non può girare su {Env.SiteMap.Site}: la mappa dei bersagli non è ancora compilata (mancano {string.Join(", ", missing)}).");
}
return Env.Capture.Reader.Vocabulary.UnusableReason is { } unusable
? ("lettori-incompleti", $"«{name}» non può girare: {unusable}")
: null;
}
/// <summary>
/// The rest page (spec §6.1). After a verification request or a disconnection the page is left as it is, so that
/// Alberto sees what happened (spec §7.6): resting is skipped while the breaker says so.
/// </summary>
public Task RestAsync(CancellationToken ct)
{
if (Env.Breaker?.State.Status is BreakerStatus.Challenge or BreakerStatus.Disconnected)
{
return Task.CompletedTask;
}
return Env.Driver.RestAsync(ct);
}
private async Task CheckSessionCookieAsync(RecipeRun run)
{
if (await Env.Driver.HasSessionCookieAsync(run.Ct).ConfigureAwait(false) is false)
{
Env.Signals.ObserveSessionCookie(false);
run.ThrowIfSignal();
}
}
private async Task ReportAsync(RecipeRun run, RecipeStatus status, string? text, CancellationToken ct)
{
var name = Name(run.Request.Kind);
var accountId = run.Request.AccountId;
switch (status)
{
case RecipeStatus.UnrecognizedFormat:
var shapes = run.UnknownShapes.Count == 0 ? "(nessuna struttura)" : string.Join(" · ", run.UnknownShapes.Take(3));
await Writer.EventAsync("formato-non-riconosciuto", $"Formato non riconosciuto in «{name}». Struttura delle chiavi: {shapes}", EventSeverity.Warning, accountId, run.Session.SessionId, ct).ConfigureAwait(false);
if (accountId is { } id)
{
await Env.Storage.Accounts.AddNoticesAsync(id, AccountNotices.UnrecognizedFormat, Env.Clock.Now, ct).ConfigureAwait(false);
}
break;
case RecipeStatus.TargetNotFound:
await Writer.EventAsync("bersaglio-non-trovato", $"«{name}» si è fermata: {text}", EventSeverity.Error, accountId, run.Session.SessionId, ct).ConfigureAwait(false);
break;
case RecipeStatus.Failed:
await Writer.EventAsync("ricetta-fallita", text ?? $"«{name}» non è riuscita.", EventSeverity.Error, accountId, run.Session.SessionId, ct).ConfigureAwait(false);
break;
}
}
private async Task<Account> LoadAccountAsync(RecipeRun run)
{
if (run.Request.AccountId is not { } id)
{
throw RecipeRun.Stop(RecipeStatus.Failed, $"«{Name(run.Request.Kind)}» ha bisogno di un account.");
}
var account = await Env.Storage.Accounts.GetAsync(id, run.Ct).ConfigureAwait(false)
?? throw RecipeRun.Stop(RecipeStatus.Failed, $"Account {id} non trovato.");
if (account.State == AccountState.ArchiveOnly)
{
throw RecipeRun.Stop(RecipeStatus.Denied, $"{account.Username} è «Solo archivio»: non si contatta.");
}
return account;
}
/// <summary>The profile of <paramref name="account"/> in the reads: same identifier, or same name when the identifier is not known yet.</summary>
private static ProfileInfo? FindProfile(IEnumerable<CapturedRead> reads, Account account) =>
reads.SelectMany(r => r.Result.Profiles)
.Where(p => account.InstagramId is { } id ? p.Id == id || string.Equals(p.Username, account.Username, StringComparison.OrdinalIgnoreCase) : string.Equals(p.Username, account.Username, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(p => (p.PostCount is null ? 0 : 1) + (p.IsPrivate is null ? 0 : 1))
.FirstOrDefault();
/// <summary>Content items of the reads, in arrival order, without repetitions.</summary>
private static List<ContentItem> Contents(IEnumerable<CapturedRead> reads, Func<ContentItem, bool>? filter = null)
{
var seen = new HashSet<string>(StringComparer.Ordinal);
return [.. reads.SelectMany(r => r.Result.Contents).Where(c => (filter is null || filter(c)) && seen.Add(c.Id))];
}
private static bool IsOwnedBy(ContentItem item, Account account) =>
item.Owner is { } owner && (account.InstagramId is { } id ? owner.Id == id : string.Equals(owner.Username, account.Username, StringComparison.OrdinalIgnoreCase));
/// <summary>
/// A post, not a story (it expires) nor an item found inside a highlight collection. The reader recognizes all three
/// by the same shape; stories and collection items are saved only by the viewer recipes, and only when shown
/// (spec §6.3: «anche quando la pagina riceve in anticipo l'elenco completo»).
/// </summary>
private static bool IsPostLike(ContentItem item) => item.ExpiresAt is null && item.HighlightId is null;
/// <summary>
/// Pinned on top of a profile (spec §6.3: not «already known» for the stop rule, nor a limit of history). When the
/// vocabulary has no key for it, a post followed on its page by a newer one is out of the order of the grid,
/// newest first: that is what a pinned post looks like.
/// </summary>
private bool LooksPinned(ContentItem item, IReadOnlyList<ContentItem> page)
{
if (item.Pinned)
{
return true;
}
if (Env.Capture.Reader.Vocabulary.Content.Pinned.IsSet)
{
return false;
}
var index = -1;
for (var i = 0; i < page.Count; i++)
{
if (string.Equals(page[i].Id, item.Id, StringComparison.Ordinal))
{
index = i;
break;
}
}
return index >= 0 && page.Skip(index + 1).Any(later => later.TakenAt > item.TakenAt);
}
private static IReadOnlyDictionary<string, string> Values(params (string Key, string Value)[] pairs) =>
pairs.ToDictionary(p => p.Key, p => p.Value, StringComparer.Ordinal);
}