Merge remote-tracking branch 'origin/main' into funzione/video-fermi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
19119064f0
29 files changed
+922
-519
No files matched your search
@@ -189,6 +189,17 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// </summary>
|
||||
public TimeSpan PageTurnWait { get; set; } = TimeSpan.FromSeconds(15);
|
||||
|
||||
/// <summary>
|
||||
/// How long Alberto's first navigation waits, at most, for the browser to say which page it shows when it does not
|
||||
/// answer (real time): right after «Scollega» it has just restarted with an empty profile and may not answer yet.
|
||||
/// After it the page counts as the rest page, and the start page opens (issue #67, decision of 29 November 2026):
|
||||
/// never a white window (issue #52). Read at every wait; tests shorten it.
|
||||
/// </summary>
|
||||
public TimeSpan AddressWait { get; set; } = TimeSpan.FromSeconds(10);
|
||||
|
||||
/// <summary>How often the wait of <see cref="AddressWait"/> asks the browser again.</summary>
|
||||
private static readonly TimeSpan AddressPoll = TimeSpan.FromMilliseconds(200);
|
||||
|
||||
/// <summary>The engine, once it exists (it is built after the browser).</summary>
|
||||
public void AttachEngine(EngineHost engine) => _engine = engine ?? throw new ArgumentNullException(nameof(engine));
|
||||
|
||||
@@ -1083,14 +1094,23 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// <summary>
|
||||
/// Alberto's own first navigation (rule 7: not an engine action, no Governor), under his lease: the login page when
|
||||
/// asked for, and the start page when the browser rests on the empty page (issue #52). Any other page stays as it is
|
||||
/// — the one he was using, or the verification page of a stop.
|
||||
/// — the one he was using, or the verification page of a stop. A browser that does not say its page is waited for,
|
||||
/// up to <see cref="AddressWait"/>; still silent, its page counts as the rest page (issue #67).
|
||||
/// </summary>
|
||||
private async Task OpenForAlbertoAsync(bool openLogin, CancellationToken ct)
|
||||
{
|
||||
if (!openLogin && !IsRest(await FullUrlAsync(ct).ConfigureAwait(false)))
|
||||
// A known address that is not the rest page is a page Alberto uses: never touched. An unknown one (the browser
|
||||
// still silent after the wait) is the rest page.
|
||||
var answered = true;
|
||||
if (!openLogin)
|
||||
{
|
||||
_log.Info("collegamento", "Alberto usa il browser.");
|
||||
return;
|
||||
var first = await AwaitFullUrlAsync(ct).ConfigureAwait(false);
|
||||
answered = first is not null;
|
||||
if (answered && !IsRest(first))
|
||||
{
|
||||
_log.Info("collegamento", "Alberto usa il browser.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The engine ends its session when the lease starts; wait for it before taking the page. A search step in
|
||||
@@ -1104,7 +1124,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
target = _loginUrl;
|
||||
what = "pagina di accesso";
|
||||
}
|
||||
else if (IsRest(await FullUrlAsync(ct).ConfigureAwait(false)))
|
||||
else if (await IsAtRestAsync(wait: answered, ct).ConfigureAwait(false))
|
||||
{
|
||||
target = StartPageUrl;
|
||||
what = target == _loginUrl ? "era a riposo, pagina di accesso" : "era a riposo, pagina iniziale del sito";
|
||||
@@ -1116,7 +1136,19 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await _browser.Cdp.NavigateAsync(target, ct).ConfigureAwait(false);
|
||||
// A browser still silent (restarting after «Scollega», issue #67) usually refuses the navigation too: the window
|
||||
// comes to the front all the same, as when its page was unknown before, never an internal error.
|
||||
NavigationResult result;
|
||||
try
|
||||
{
|
||||
result = await _browser.Cdp.NavigateAsync(target, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is CdpDisconnectedException or BrowserUnavailableException or CdpCommandException or TimeoutException)
|
||||
{
|
||||
_log.Info("collegamento", $"Alberto usa il browser: {what} non aperta (il browser non risponde).");
|
||||
return;
|
||||
}
|
||||
|
||||
_log.Info("collegamento", result.Succeeded
|
||||
? $"Alberto usa il browser: {what} aperta."
|
||||
: $"Alberto usa il browser: {what} non aperta (errore di rete del browser).");
|
||||
@@ -1267,6 +1299,44 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The address of the main frame as <see cref="FullUrlAsync"/> gives it, but a browser that does not answer is asked
|
||||
/// again every <see cref="AddressPoll"/> until it does, for at most <see cref="AddressWait"/> (real time, issue #67);
|
||||
/// null when it stayed silent. Cancelled with <paramref name="ct"/>.
|
||||
/// </summary>
|
||||
private async Task<string?> AwaitFullUrlAsync(CancellationToken ct)
|
||||
{
|
||||
using var limit = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
limit.CancelAfter(AddressWait);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (await FullUrlAsync(limit.Token).ConfigureAwait(false) is { } url)
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
await _clock.Delay(AddressPoll, limit.Token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
|
||||
{
|
||||
_log.Info("collegamento", "Il browser non ha indicato la pagina in tempo: la pagina conta come quella di riposo.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// After the engine's session ended: whether the page is the rest page (or still unknown). A browser that already let
|
||||
/// <see cref="AddressWait"/> pass is asked once more (<paramref name="wait"/> false), not waited for again.
|
||||
/// </summary>
|
||||
private async Task<bool> IsAtRestAsync(bool wait, CancellationToken ct)
|
||||
{
|
||||
var url = wait ? await AwaitFullUrlAsync(ct).ConfigureAwait(false) : await FullUrlAsync(ct).ConfigureAwait(false);
|
||||
return url is null || IsRest(url);
|
||||
}
|
||||
|
||||
private async Task WaitForSessionEndAsync(CancellationToken ct)
|
||||
{
|
||||
if (_engine is not { } engine)
|
||||
|
||||
@@ -74,7 +74,10 @@ public sealed class EngineHost : IAsyncDisposable
|
||||
|
||||
internal IReadOnlyList<RecentAction> RecentActions(int limit) => _recent.Snapshot(limit);
|
||||
|
||||
/// <summary>Starts the background loop.</summary>
|
||||
/// <summary>
|
||||
/// Starts the background loop. It counts as running from here (<see cref="EngineRuntime.IsRunning"/>), not only once
|
||||
/// the thread pool gets to it: whoever started it (Febe's start, «Scollega») says so right away.
|
||||
/// </summary>
|
||||
public Task StartAsync(CancellationToken ct = default)
|
||||
{
|
||||
lock (_gate)
|
||||
@@ -86,6 +89,7 @@ public sealed class EngineHost : IAsyncDisposable
|
||||
|
||||
_loopCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
var token = _loopCts.Token;
|
||||
_s.Runtime.IsRunning = true;
|
||||
_loop = Task.Run(() => RunAsync(token), CancellationToken.None);
|
||||
}
|
||||
|
||||
@@ -739,8 +743,11 @@ public sealed class EngineHost : IAsyncDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
await _runner.RestAsync(ct).ConfigureAwait(false);
|
||||
s.PageTurns.NoteRested();
|
||||
if (!KeepsPageAfterWaitingForTurn())
|
||||
{
|
||||
await _runner.RestAsync(ct).ConfigureAwait(false);
|
||||
s.PageTurns.NoteRested();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
@@ -797,6 +804,23 @@ public sealed class EngineHost : IAsyncDisposable
|
||||
s.Events.Publish(EngineNotificationKind.StateChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looked at again with the page turn in hand, right before a rest of the engine: the turn may have come only after
|
||||
/// Alberto's own navigation, which takes it too (he took the browser again right after letting go of it), or after a
|
||||
/// verification. Then the page is his, or must stay as it is (spec §6.1, §7.6): no rest, and the rest is owed for
|
||||
/// when he lets go. The profile search looks again in the same way before its own rest.
|
||||
/// </summary>
|
||||
private bool KeepsPageAfterWaitingForTurn()
|
||||
{
|
||||
if (_s.Runtime.UserIsDriving)
|
||||
{
|
||||
Volatile.Write(ref _restAfterDriving, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
return _s.Breaker.State.Status is BreakerStatus.Challenge or BreakerStatus.Disconnected;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Alberto let go of the browser and no session runs: the page goes back to rest (spec §6.1), so that nothing he or a
|
||||
/// cancelled session left open stays on the site — a story viewer moves on by itself to other accounts (§6.3).
|
||||
@@ -821,6 +845,11 @@ public sealed class EngineHost : IAsyncDisposable
|
||||
|
||||
await using (turn.ConfigureAwait(false))
|
||||
{
|
||||
if (KeepsPageAfterWaitingForTurn())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _runner.RestAsync(ct).ConfigureAwait(false);
|
||||
_s.PageTurns.NoteRested();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user