Raccolta (M0/M2): sito di prova completo, lettori per forma, ricette su browser vero, coda degli scaricamenti, sonda con ripulitura e verifica; documenti delle ipotesi e delle prove dal vivo

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

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-13 17:32:35 +00:00
1 parent e2c987873b
commit 4fc5a6c0d4
74 files changed
+12689 -13

No files matched your search

@@ -0,0 +1,199 @@
using System.Text.Json;
using Febe.Core.Abstractions;
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>
/// 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 readonly ShapeReader _reader;
private readonly IClock _clock;
private readonly Lock _gate = new();
private readonly List<CapturedRead> _reads = [];
private readonly Dictionary<string, DateTimeOffset> _pending = new(StringComparer.Ordinal);
private long _sequence;
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>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>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>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).</summary>
public CapturedRead? Accept(ResponseDescriptor response, JsonElement body)
{
ArgumentNullException.ThrowIfNull(response);
lock (_gate)
{
_pending.Remove(response.RequestId);
}
if (response.HostKind != HostKind.Site)
{
return null;
}
var result = _reader.Read(body);
lock (_gate)
{
var read = new CapturedRead(++_sequence, response, result);
_reads.Add(read);
if (_reads.Count > MaxKept)
{
_reads.RemoveRange(0, _reads.Count - MaxKept);
}
return read;
}
}
void IResponseObserver.OnResponse(ResponseDescriptor response)
{
if (response.HostKind == HostKind.Site
&& response.MimeType.Contains("json", StringComparison.OrdinalIgnoreCase)
&& response.Status is >= 200 and < 600 && response.Status != 204)
{
lock (_gate)
{
_pending[response.RequestId] = _clock.Now;
}
}
}
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);
}
}
}