From 79d836842043bb6a9c24b2f7b77bce156e35870c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 14:51:19 +0000 Subject: [PATCH] Contratti fra motore e ricette (RecipeRequest, IRecipeRunner, IDataBudget) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA --- src/Febe.Core/Engine/Contracts.cs | 125 ++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/Febe.Core/Engine/Contracts.cs diff --git a/src/Febe.Core/Engine/Contracts.cs b/src/Febe.Core/Engine/Contracts.cs new file mode 100644 index 0000000..7efcf6b --- /dev/null +++ b/src/Febe.Core/Engine/Contracts.cs @@ -0,0 +1,125 @@ +using Febe.Core.Abstractions; + +namespace Febe.Core.Engine; + +/// The fixed set of recipes (spec §6.3). +public enum RecipeKind +{ + /// Giro dei seguiti: home, stories bar, recent posts of followed profiles. + FollowingSweep, + + /// Storie di un account. + AccountStories, + + /// Controllo del profilo. + ProfileCheck, + + /// Storico: one or more history pages, resuming from the stored point. + Backfill, + + /// Raccolta in evidenza. + Highlight, + + /// Taggati. + Tagged, + + /// Reel. + Reels, + + /// Post singolo: complete data or refresh an expired address. + SinglePost, + + /// Elenco dei seguiti: only on request. + FollowingList, +} + +/// What the scheduler asks a recipe to do. +/// Recipe. +/// Febe account id (database id, not the Instagram id); null for FollowingSweep and FollowingList. +/// For SinglePost: the entry whose data or addresses must be refreshed. +/// For Highlight: the Instagram id of the collection. +/// For Backfill, Tagged, Reels and FollowingList: how many pages at most in this run. +/// True for «Controlla ora» (spec §7.8). +public sealed record RecipeRequest( + RecipeKind Kind, + long? AccountId = null, + long? EntryId = null, + string? HighlightId = null, + int? MaxPages = null, + bool Manual = false); + +/// How a recipe run ended. +public enum RecipeStatus +{ + /// Finished its purpose (reached known content, the end of the list, or MaxPages). + Completed, + + /// The Governor refused a counted action: stopped in order. + Denied, + + /// A stop signal (spec §7.6) was seen: stopped where it was, page left as is. + StoppedBySignal, + + /// A target of the target map was not found: stopped and reported, no random clicks. + TargetNotFound, + + /// Responses had an unrecognized shape (spec §6.4). + UnrecognizedFormat, + + /// The account is not reachable (private not followed, missing, blocked). + AccountUnreachable, + + /// Cancelled (engine paused, Alberto took the browser, shutdown). + Cancelled, + + /// Unexpected error; message says what happened. + Failed, +} + +/// Result of a recipe run, used by the scheduler to plan what comes next. +public sealed record RecipeOutcome( + RecipeStatus Status, + int CountedActions, + int NewEntries, + string? Message = null) +{ + /// Followed accounts the stories bar flagged as having active stories (FollowingSweep). + public IReadOnlyList AccountsWithStories { get; init; } = []; + + /// Accounts where the sweep saw new posts (FollowingSweep). + public IReadOnlyList AccountsWithNewPosts { get; init; } = []; + + /// For Backfill: true when the beginning of the profile has been reached. + public bool BackfillFinished { get; init; } +} + +/// Information about the session in progress, handed to every recipe. +public interface ISessionContext +{ + long SessionId { get; } + + /// Speed factor drawn once per session (log-normal, median 1, σ 0.2, [0.7, 1.5]); multiplies every wait. + double Mood { get; } + + /// Planned end of the session; recipes should finish what they are doing and stop after it. + DateTimeOffset PlannedEnd { get; } +} + +/// Runs one recipe. The real implementation drives the browser; the simulation uses a fake world. +public interface IRecipeRunner +{ + Task RunAsync(RecipeRequest request, ISessionContext session, CancellationToken ct); + + /// Puts the browser on the empty rest page (end of session, pauses, night). + Task RestAsync(CancellationToken ct); +} + +/// Daily byte budget for CDN downloads (category «dati scaricati», spec §7.4). +public interface IDataBudget +{ + /// Bytes still available today (after ramp-up and halving), never negative. + Task RemainingTodayAsync(CancellationToken ct); + + /// Records bytes actually downloaded. + Task RecordAsync(long bytes, long? accountId, CancellationToken ct); +}