Files
Febe/src/Febe.Core/Collection/SignalDetector.cs
T
Claude aff81dfac9 Collegamento e motore: correzioni regole-1, regole-2, regole-6, websec-1, websec-5, websec-6, engine-1, engine-2, sched-1
Pagine aperte da Alberto non sono segnali (regole-1); cookie perso fuori sessione = disconnessione (regole-2); pagina a riposo quando Alberto lascia il browser (regole-6); la lease di «Accedi»/«Usa il browser» finisce da sola dopo 2 minuti senza vista remota né finestra (websec-1); ping con scadenza sulla vista remota (websec-5); aperture della vista remota serializzate (websec-6); orologio virtuale senza fine anticipata (engine-1); la sincronizzazione della coda non duplica il compito in corso (engine-2); raccolte in evidenza programmate (sched-1).

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

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-14 00:21:13 +00:00

260 lines
8.5 KiB
C#

using System.Text.Json;
using System.Text.RegularExpressions;
using Febe.Core.Abstractions;
namespace Febe.Core.Collection;
/// <summary>
/// Turns what the page sees (responses, navigations, JSON bodies, empty pages, the session cookie) into stop
/// signals (spec §7.6) and reports them to the circuit breaker. It never logs or keeps bodies.
/// </summary>
/// <remarks>
/// The same signal code is reported at most once per <see cref="DedupeWindow"/>: a page that receives ten 429
/// responses in a second is one throttle, not ten. Thread-safe.
/// </remarks>
public sealed class SignalDetector : IResponseObserver, IResponseConsumer, IStopSignals
{
private readonly SignalRules _rules;
private readonly ICircuitBreaker _breaker;
private readonly IClock _clock;
private readonly Dictionary<string, DateTimeOffset> _lastReported = new(StringComparer.Ordinal);
private readonly Lock _gate = new();
private int _consecutiveEmpty;
public SignalDetector(SignalRules rules, ICircuitBreaker breaker, IClock clock, TimeSpan? dedupeWindow = null)
{
_rules = rules ?? throw new ArgumentNullException(nameof(rules));
_breaker = breaker ?? throw new ArgumentNullException(nameof(breaker));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
DedupeWindow = dedupeWindow ?? TimeSpan.FromSeconds(60);
}
public TimeSpan DedupeWindow { get; }
/// <summary>
/// True while the page is Alberto's, not the engine's: he holds the «driving» lease and no session runs. The
/// addresses he opens then — the login page of «Accedi a Instagram», a verification he completes by hand — are not
/// engine actions (rule 7 of §2) nor the page «going back to the login» of §7.6, so path rules (navigations and
/// main documents) are not signals. Status and text rules, and the cookie check of the recipes, still are.
/// Null: the page is always the engine's.
/// </summary>
public Func<bool>? UserOwnsPage { get; init; }
/// <summary>Raised after a signal has been reported to the breaker.</summary>
public event EventHandler<Signal>? SignalRaised;
void IResponseObserver.OnResponse(ResponseDescriptor response) => ObserveResponse(response);
void IResponseObserver.OnNavigation(NavigationDescriptor navigation) => ObserveNavigation(navigation);
ValueTask IResponseConsumer.ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
ObserveJsonBody(response, body.RootElement);
return ValueTask.CompletedTask;
}
/// <summary>Status and path rules on a response.</summary>
public Signal? ObserveResponse(ResponseDescriptor response)
{
ArgumentNullException.ThrowIfNull(response);
var scope = response.IsMainFrameDocument ? SignalScope.Document : SignalScope.Subresource;
var paths = !UserIsOnPage();
foreach (var rule in _rules.Rules)
{
if (!rule.HostKinds.Contains(response.HostKind) || (rule.Scope & scope) == 0)
{
continue;
}
if ((rule.Statuses.Count > 0 && rule.Statuses.Contains(response.Status))
|| (paths && rule.Path is not null && SafeIsMatch(rule.Path, response.Path)))
{
return Raise(rule.Level, rule.Code, response.Url);
}
}
return null;
}
/// <summary>Path rules on a main-frame navigation (none while the page is Alberto's: <see cref="UserOwnsPage"/>).</summary>
public Signal? ObserveNavigation(NavigationDescriptor navigation)
{
ArgumentNullException.ThrowIfNull(navigation);
if (UserIsOnPage())
{
return null;
}
foreach (var rule in _rules.Rules)
{
if (rule.Path is null || (rule.Scope & SignalScope.Navigation) == 0 || !rule.HostKinds.Contains(navigation.HostKind))
{
continue;
}
if (SafeIsMatch(rule.Path, navigation.Path))
{
return Raise(rule.Level, rule.Code, navigation.Url);
}
}
return null;
}
/// <summary>Text rules on the string values of a short JSON response.</summary>
public Signal? ObserveJsonBody(ResponseDescriptor response, JsonElement root)
{
ArgumentNullException.ThrowIfNull(response);
foreach (var rule in _rules.Rules)
{
if (rule.Text is null || !rule.HostKinds.Contains(response.HostKind))
{
continue;
}
if (ContainsText(root, rule.Text, rule.MaxTextChars))
{
return Raise(rule.Level, rule.Code, response.Url);
}
}
return null;
}
/// <summary>
/// A recipe loaded a page (or a page of data) that turned out empty. After the configured number of
/// consecutive empty pages a throttle signal is raised. The caller decides what «empty» means.
/// </summary>
public Signal? ObserveEmptyPage(string? url = null)
{
if (_rules.EmptyPages is not { } rule)
{
return null;
}
lock (_gate)
{
_consecutiveEmpty++;
if (_consecutiveEmpty < rule.Consecutive)
{
return null;
}
_consecutiveEmpty = 0;
}
return Raise(rule.Level, rule.Code, url is null ? null : UrlSanitizer.Sanitize(url).Url);
}
/// <summary>A recipe loaded a page with content: resets the empty-page counter.</summary>
public void ObserveNonEmptyPage()
{
lock (_gate)
{
_consecutiveEmpty = 0;
}
}
/// <summary>Result of the session-cookie check: a missing cookie is a disconnection (spec §7.6).</summary>
public Signal? ObserveSessionCookie(bool present) =>
present ? null : Raise(SignalLevel.Disconnect, SignalRules.SessionCookieMissingCode, null);
private bool UserIsOnPage() => UserOwnsPage?.Invoke() == true;
private Signal? Raise(SignalLevel level, string code, string? url)
{
var now = _clock.Now;
lock (_gate)
{
if (_lastReported.TryGetValue(code, out var last) && now - last < DedupeWindow)
{
return null;
}
_lastReported[code] = now;
}
var signal = new Signal(level, code, url, now);
_breaker.Report(signal);
SignalRaised?.Invoke(this, signal);
return signal;
}
private static bool SafeIsMatch(Regex regex, string input)
{
try
{
return regex.IsMatch(input);
}
catch (RegexMatchTimeoutException)
{
return false;
}
}
/// <summary>Looks for <paramref name="regex"/> in the string values, giving up when they exceed <paramref name="maxChars"/>.</summary>
private static bool ContainsText(JsonElement root, Regex regex, int maxChars)
{
var budget = maxChars;
var strings = new List<string>();
if (!Collect(root, strings, ref budget, depth: 0))
{
return false;
}
foreach (var s in strings)
{
if (SafeIsMatch(regex, s))
{
return true;
}
}
return false;
}
private static bool Collect(JsonElement element, List<string> strings, ref int budget, int depth)
{
if (depth > 32)
{
return false;
}
switch (element.ValueKind)
{
case JsonValueKind.String:
var value = element.GetString() ?? string.Empty;
budget -= value.Length;
if (budget < 0)
{
return false;
}
strings.Add(value);
return true;
case JsonValueKind.Object:
foreach (var property in element.EnumerateObject())
{
if (!Collect(property.Value, strings, ref budget, depth + 1))
{
return false;
}
}
return true;
case JsonValueKind.Array:
foreach (var item in element.EnumerateArray())
{
if (!Collect(item, strings, ref budget, depth + 1))
{
return false;
}
}
return true;
default:
return true;
}
}
}