namespace Febe.Core.Abstractions; /// /// Categories of counted actions toward the site (spec §7.4). Each has an hourly and a daily budget, /// except , which has a daily byte budget. /// public enum ActionKind { /// Opening the home page (Giro dei seguiti). Home, /// Opening a profile page. ProfileOpen, /// One story shown in the viewer (stories and highlight items). StoryView, /// One page of history loaded by scrolling (Storico, Taggati, Reel). BackfillPage, /// Opening a single post page. SinglePost, /// One page of the list of profiles Alberto follows. FollowingListPage, /// /// One step of the profile search toward the site: opening Instagram for the search, opening its «Cerca» panel or /// focusing its box, or one burst of typing (issue #37). Outside sessions, with its own wide budget that does not /// consume the collection budgets. /// ProfileSearch, /// Bytes downloaded from the CDN (daily byte budget). DataDownload, } /// Why the Governor refused an action. Shown in the interface. public enum DenialReason { None, BreakerOpen, OutsideActiveWindow, NoSession, HourlyBudgetReached, DailyBudgetReached, MinimumSpacing, AccountLimitReached, UserIsDriving, Paused, /// The account is «Solo archivio», paused or not reachable: the engine does not contact it (spec §4.2, rule 6). AccountNotContactable, } /// /// The Governor's answer to a request: either a permission or a motivated refusal (spec §7.4). /// public sealed record Permit(bool Granted, ActionKind Kind, long? AccountId, DenialReason Reason, string? Message, DateTimeOffset? RetryAfter) { public static Permit Grant(ActionKind kind, long? accountId) => new(true, kind, accountId, DenialReason.None, null, null); public static Permit Deny(ActionKind kind, long? accountId, DenialReason reason, string message, DateTimeOffset? retryAfter = null) => new(false, kind, accountId, reason, message, retryAfter); } /// /// The single point through which every engine action toward the site passes (spec §2 rule 7, §7.4). /// No code path may load an Instagram page or perform a gesture on it without a granted permit. /// public interface IGovernor { Task RequestAsync(ActionKind kind, long? accountId, CancellationToken ct); } /// Severity levels of stop signals (spec §7.6). public enum SignalLevel { /// Rallentamento: timed stop (1h, 6h, 24h), then halved budgets for 48 hours. Throttle, /// Verifica: stops everything until Alberto resumes by hand. Challenge, /// Disconnessione: as Challenge; never tries to log back in. Disconnect, } /// A stop signal reported by recipes or by the response capture. /// Severity. /// Stable identifier of the signal, e.g. "http-429" or "challenge-url" (from segnali.json). /// Address where it was seen, without query string (never secrets). /// When it was seen. public sealed record Signal(SignalLevel Level, string Code, string? Url, DateTimeOffset At); /// State of the circuit breaker (Interruttore). public enum BreakerStatus { /// Normal operation. Closed, /// Timed stop after a throttle signal; reopens by itself at . Throttled, /// Stopped after a challenge: needs Alberto and «Riprendi». Challenge, /// Stopped after a disconnection: needs Alberto and «Riprendi». Disconnected, } public sealed record BreakerState(BreakerStatus Status, DateTimeOffset? Until, Signal? LastSignal, DateTimeOffset? HalvedBudgetsUntil) { public static readonly BreakerState Normal = new(BreakerStatus.Closed, null, null, null); public bool AllowsSiteActions => Status == BreakerStatus.Closed; } /// The circuit breaker (spec §7.6). Every stop is persisted as an incident. public interface ICircuitBreaker { BreakerState State { get; } void Report(Signal signal); }