Files
Febe/src/Febe.Core/Collection/Recipes/CaptureCollector.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

352 lines
11 KiB
C#

using System.Text.Json;
using Febe.Core.Abstractions;
using Febe.Core.Collection.Diagnostics;
using Febe.Core.Collection.Readers;
namespace Febe.Core.Collection.Recipes;
/// <summary>One response read by the reader, in arrival order.</summary>
/// <param name="Sequence">Arrival number, from 1.</param>
/// <param name="Response">Sanitized descriptor (no query string).</param>
/// <param name="Result">What the reader recognized, detached from the body.</param>
public sealed record CapturedRead(long Sequence, ResponseDescriptor Response, ReadResult Result);
/// <summary>
/// Where the search answers stood at one moment (issue #37): the last search answer that arrived and the last request
/// the page started. Pass it to <see cref="CaptureCollector.SearchSince"/>.
/// </summary>
public readonly record struct SearchAnswerMark(long Arrival, long Request);
/// <summary>
/// Bridge between the response tap and the recipes (spec §6.2, §6.4): every JSON body the site sends is read by the
/// <see cref="ShapeReader"/> as it arrives, and the recipes wait for what they expect after a gesture. Bodies are
/// never kept: only the reader's detached results are.
/// </summary>
/// <remarks>
/// It also tracks which data responses are still on their way (their body not read yet), so a recipe can wait until
/// the page has settled. Thread-safe.
/// </remarks>
public sealed class CaptureCollector : IResponseConsumer, IResponseObserver
{
private const int MaxKept = 2000;
private const int MaxSearchKept = 50;
private readonly ShapeReader _reader;
private readonly IClock _clock;
private readonly Lock _gate = new();
private readonly List<CapturedRead> _reads = [];
private readonly List<CapturedRead> _searchReads = [];
private readonly Dictionary<string, DateTimeOffset> _pending = new(StringComparer.Ordinal);
private long _sequence;
private long _searchSequence;
private long _lastRequestOrder;
private DateTimeOffset? _lastArrivalAt;
private string? _viewerId;
public CaptureCollector(ShapeReader reader, IClock clock)
{
_reader = reader ?? throw new ArgumentNullException(nameof(reader));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
}
public ShapeReader Reader => _reader;
/// <summary>
/// The recorder of the diagnostic report (issue #39), told what the readers did with every body, also the ones
/// not kept (another host, unrelated); null: nothing is recorded.
/// </summary>
public CollectionDiagnostics? Diagnostics { get; init; }
/// <summary>How long a data response may stay «on its way» before it is forgotten (its body may never come).</summary>
public TimeSpan PendingLifetime { get; init; } = TimeSpan.FromSeconds(15);
/// <summary>
/// Identifier of the connected account in the latest site answer that gave one (<see cref="ReadResult.ViewerId"/>),
/// also an answer that is otherwise <see cref="ReadShape.Unrelated"/> and not kept.
/// </summary>
public string? ViewerId
{
get
{
lock (_gate)
{
return _viewerId;
}
}
}
/// <summary>Sequence number of the last read: pass it to <see cref="Since"/> to get what arrives after now.</summary>
public long Mark()
{
lock (_gate)
{
return _sequence;
}
}
/// <summary>Reads that arrived after <paramref name="mark"/>, oldest first.</summary>
public IReadOnlyList<CapturedRead> Since(long mark)
{
lock (_gate)
{
return [.. _reads.Where(r => r.Sequence > mark)];
}
}
/// <summary>
/// Where the search box answers stand now (issue #37): pass it to <see cref="SearchSince"/>. Search answers have
/// their own buffer, so recipe windows (<see cref="Mark"/>, <see cref="Since"/>) never see them. Taken right before
/// a key, it separates the requests the page started for the texts before that key from the ones after it.
/// </summary>
public SearchAnswerMark SearchMark()
{
lock (_gate)
{
return new SearchAnswerMark(_searchSequence, _lastRequestOrder);
}
}
/// <summary>
/// Search box answers to requests the page started after <paramref name="mark"/>, oldest request first. An answer
/// whose request order is unknown counts by its arrival instead.
/// </summary>
public IReadOnlyList<CapturedRead> SearchSince(SearchAnswerMark mark)
{
lock (_gate)
{
return [.. _searchReads
.Where(r => r.Response.RequestOrder > 0 ? r.Response.RequestOrder > mark.Request : r.Sequence > mark.Arrival)
.OrderBy(r => r.Response.RequestOrder)
.ThenBy(r => r.Sequence)];
}
}
/// <summary>When the body of the last response of the site was read (any kind, unrelated ones included).</summary>
public DateTimeOffset? LastArrivalAt
{
get
{
lock (_gate)
{
return _lastArrivalAt;
}
}
}
/// <summary>Data responses whose body has not been read yet.</summary>
public int Pending
{
get
{
lock (_gate)
{
ExpirePending();
return _pending.Count;
}
}
}
/// <summary>
/// Waits until the reads after <paramref name="mark"/> satisfy <paramref name="until"/>, or <paramref name="timeout"/>
/// passes. Returns the reads after the mark either way.
/// </summary>
public async Task<IReadOnlyList<CapturedRead>> WaitAsync(long mark, Func<IReadOnlyList<CapturedRead>, bool> until, TimeSpan timeout, TimeSpan poll, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(until);
var deadline = _clock.Now + timeout;
while (true)
{
var reads = Since(mark);
if (until(reads) || _clock.Now >= deadline)
{
return reads;
}
await _clock.Delay(poll, ct).ConfigureAwait(false);
}
}
/// <summary>
/// Waits until no data response is on its way and nothing new arrived for <paramref name="quiet"/>, at most
/// <paramref name="timeout"/>.
/// </summary>
public async Task SettleAsync(TimeSpan quiet, TimeSpan timeout, TimeSpan poll, CancellationToken ct)
{
var deadline = _clock.Now + timeout;
var lastSeen = Mark();
var quietSince = _clock.Now;
while (_clock.Now < deadline)
{
await _clock.Delay(poll, ct).ConfigureAwait(false);
var current = Mark();
if (current != lastSeen || Pending > 0)
{
lastSeen = current;
quietSince = _clock.Now;
continue;
}
if (_clock.Now - quietSince >= quiet)
{
return;
}
}
}
/// <summary>Forgets everything (start of a recipe).</summary>
public void Clear()
{
lock (_gate)
{
_reads.Clear();
_pending.Clear();
}
}
ValueTask IResponseConsumer.ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
Accept(response, body.RootElement);
return ValueTask.CompletedTask;
}
/// <summary>
/// Reads one body (also used by tests and the probe samples). Returns null, and keeps nothing, for responses that
/// are not the site's or that are <see cref="ReadShape.Unrelated"/>.
/// </summary>
public CapturedRead? Accept(ResponseDescriptor response, JsonElement body)
{
ArgumentNullException.ThrowIfNull(response);
lock (_gate)
{
_pending.Remove(response.RequestId);
}
if (response.HostKind != HostKind.Site)
{
Observe(response, null, body);
return null;
}
var result = _reader.Read(body);
Observe(response, result, body);
lock (_gate)
{
_lastArrivalAt = _clock.Now;
if (result.ViewerId is { } viewerId)
{
_viewerId = viewerId;
}
}
if (result.Search is not null)
{
// A search box answer goes to its own buffer: it never stops, fills or judges a recipe (issue #37).
lock (_gate)
{
var found = new CapturedRead(++_searchSequence, response, result);
_searchReads.Add(found);
if (_searchReads.Count > MaxSearchKept)
{
_searchReads.RemoveRange(0, _searchReads.Count - MaxSearchKept);
}
return found;
}
}
if (result.Shape == ReadShape.Unrelated)
{
// Not about contents (promotions, logging): neither data nor an empty page, and not «something new
// arrived» for SettleAsync, or a page that keeps logging would never look settled.
return null;
}
lock (_gate)
{
var read = new CapturedRead(++_sequence, response, result);
_reads.Add(read);
if (_reads.Count > MaxKept)
{
_reads.RemoveRange(0, _reads.Count - MaxKept);
}
return read;
}
}
/// <summary>
/// Tells the diagnostics what the readers made of a body; for an unknown shape, its strict key structure, taken
/// now, while the document is alive. Never a reason to fail the reading.
/// </summary>
private void Observe(ResponseDescriptor response, ReadResult? result, JsonElement body)
{
if (Diagnostics is not { } diagnostics)
{
return;
}
try
{
var structure = result?.Shape == ReadShape.Unrecognized ? KeyStructure.DescribeStrict(body) : null;
diagnostics.ReadObserved(response, result, structure);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Diagnostics are optional: the reading goes on as if they were not there.
}
}
void IResponseObserver.OnResponse(ResponseDescriptor response)
{
if (response.HostKind == HostKind.Site
&& DataResponses.IsData(response.MimeType, response.ResourceType)
&& response.Status is >= 200 and < 600 && response.Status != 204)
{
lock (_gate)
{
_pending[response.RequestId] = _clock.Now;
}
}
}
void IResponseObserver.OnRequestStarted(long order)
{
lock (_gate)
{
if (order > _lastRequestOrder)
{
_lastRequestOrder = order;
}
}
}
void IResponseObserver.OnBodyDropped(string requestId)
{
lock (_gate)
{
_pending.Remove(requestId);
}
}
void IResponseObserver.OnNavigation(NavigationDescriptor navigation)
{
if (!navigation.SameDocument)
{
// Bodies of the previous document will not be read any more (the tap forgets them too).
lock (_gate)
{
_pending.Clear();
}
}
}
private void ExpirePending()
{
var limit = _clock.Now - PendingLifetime;
foreach (var key in _pending.Where(p => p.Value < limit).Select(p => p.Key).ToList())
{
_pending.Remove(key);
}
}
}