Docker: lasciare la pagina del browser copia la sessione (#36, S4b, risposta 1)
verifica / controlli (push) Successful in 24s
verifica / prove (push) Failing after 41m21s
verifica / installatore (push) Skipped
verifica / immagine (push) Skipped

In Docker lasciare la pagina «Browser» o la vista remota di Collegamento mentre
Alberto guida (un'altra pagina, la scheda chiusa, il collegamento perso) valeva
«Annulla» e la sessione non si copiava. Ora è come chiudere la finestra del
browser su Windows: `cancel` con `pageLeft` va a LeavePageAsync, la vista
remota chiusa dalla sua pagina lo dice alla connessione, e se per 5 secondi
non si apre un'altra vista né arriva un `begin` (una pagina ricaricata) il
controllo del permesso copia la sessione con CopyOnLetGoAsync, solo da una
pagina del sito che non sia di accesso o di verifica, poi rilascia il permesso
e il motore rimette il browser a riposo. Il pulsante «Annulla» continua a non
copiare; Windows non cambia.

Prove nuove per la copia lasciando la pagina, la ricarica che non è lasciare,
la pagina di accesso, la vista sostituita o chiusa da Febe; guida, protocollo
e una riga in decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-25 10:52:51 +01:00
1 parent 537517b855
commit 2fc4b40c41
15 files changed
+312 -44

No files matched your search

+85 -9
View File
@@ -43,8 +43,10 @@ namespace Febe.App;
/// </para>
/// <para>
/// With a saved session (issue #36, S4b) the session is copied at «Ho finito», and also when Alberto lets go of the
/// browser without it: the window «Browser di Febe» closed on Windows (<see cref="CopyOnWindowClosedAsync"/>) and the
/// lease of «Accedi» / «Usa il browser» ending by itself. Always before the page goes back to the engine, with the
/// browser without it: the window «Browser di Febe» closed on Windows (<see cref="CopyOnWindowClosedAsync"/>), the page
/// of the browser left in Docker (<see cref="LeavePageAsync"/>, <see cref="RemoteViewClosed"/>: after
/// <see cref="PageLeaveGrace"/> without a view open again, answer of 25 November 2026) and the lease of «Accedi» /
/// «Usa il browser» ending by itself. «Annulla» never copies. Always before the page goes back to the engine, with the
/// same guarantees as «Ho finito» (the driving gate held, the page's turn), and only from a page of the site that is
/// not a login or verification page. «Collegato» then also needs a readable saved session: without one the state is
/// <see cref="ConnectionState.SessionCopyMissing"/> («Serve «Ho finito»») and the engine waits.
@@ -64,6 +66,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
/// <summary>How often the cookie is looked at in the background (outside sessions it is the only way to notice).</summary>
public static readonly TimeSpan BackgroundCheck = TimeSpan.FromSeconds(30);
/// <summary>How the log names leaving the page of the browser in Docker (answer of 25 November 2026).</summary>
private const string PageLeftText = "Pagina del browser lasciata";
private readonly IFebeBrowser _browser;
private readonly IFebeStorage _storage;
private readonly BrowserUseTracker _tracker;
@@ -85,6 +90,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
private DateTimeOffset _checkedAt = DateTimeOffset.MinValue;
private DateTimeOffset _lastCopyAt = DateTimeOffset.MinValue;
private SessionCopyOutcome? _letGoCopy;
private DateTimeOffset? _leftAt;
/// <param name="browser">The browser of Febe.</param>
/// <param name="storage">The database (first connection, «Scollega»).</param>
@@ -169,6 +175,14 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
/// </summary>
public TimeSpan DrivingGrace { get; set; } = TimeSpan.FromMinutes(2);
/// <summary>
/// How long after Alberto left the page of the browser in Docker (<see cref="LeavePageAsync"/>, or its remote view
/// closed by the page: <see cref="RemoteViewClosed"/>) no view may open again before Febe takes it as letting go of
/// the browser (answer of 25 November 2026, proposal of 5 seconds): a reload of the page opens its view again well
/// before. Read at every look; tests shorten it.
/// </summary>
public TimeSpan PageLeaveGrace { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>
/// How long Alberto's page waits for its turn while a step of the profile search holds it (real time). Read at every
/// wait; tests shorten it.
@@ -273,6 +287,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
}
_lastDrivingSign = _clock.Now;
// Alberto is back (a reload of the page left a moment ago): not a letting go.
_leftAt = null;
}
_tracker.MarkUsed(_clock.Now);
@@ -397,6 +414,52 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
}
}
/// <summary>
/// Docker (answer of 25 November 2026): the page of the browser was left without «Ho finito» or «Annulla». Nothing
/// happens at once — a reload opens the page, «Usa il browser» and the view again within a moment. The watch of the
/// lease lets go of the browser once, for <see cref="PageLeaveGrace"/> after this and after the last view closed, no
/// view is open and no «Accedi» / «Usa il browser» came: the copy of the session as when the window is closed on
/// Windows, then the lease ends and the page goes back to the engine (which puts it to rest).
/// </summary>
public Task LeavePageAsync(CancellationToken ct)
{
if (NoteLeft())
{
_log.Info("collegamento", $"Pagina del browser lasciata senza «Ho finito» né «Annulla»: se entro {ItalianText.Duration(PageLeaveGrace)} non torni al browser, Febe copia la sessione e il motore può riprendere.");
}
return Task.CompletedTask;
}
/// <summary>
/// Docker: a remote view was closed by its page (the page left, the tab closed, the link lost; not another view taking
/// its place, nor Febe stopping). While the lease of «Accedi» / «Usa il browser» holds it counts as leaving the page
/// (<see cref="LeavePageAsync"/>): the last view closed, the moment starts again from now.
/// </summary>
public void RemoteViewClosed()
{
// The browser gone: its view closed with it, Alberto did not leave (the page opens it again once it is back).
if (_browser.IsAvailable)
{
NoteLeft();
}
}
/// <summary>Notes that Alberto may have left the browser; false without the lease of «Accedi» / «Usa il browser».</summary>
private bool NoteLeft()
{
lock (_gate)
{
if (_lease is null)
{
return false;
}
_leftAt = _clock.Now;
return true;
}
}
public async Task<FinishResult> FinishAsync(CancellationToken ct)
{
await _commands.WaitAsync(ct).ConfigureAwait(false);
@@ -718,9 +781,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
while (true)
{
var grace = DrivingGrace;
var poll = TimeSpan.FromTicks(Math.Clamp(grace.Ticks / 8, TimeSpan.FromMilliseconds(20).Ticks, TimeSpan.FromSeconds(5).Ticks));
var leaveGrace = PageLeaveGrace;
var shortest = leaveGrace < grace ? leaveGrace : grace;
var poll = TimeSpan.FromTicks(Math.Clamp(shortest.Ticks / 8, TimeSpan.FromMilliseconds(20).Ticks, TimeSpan.FromSeconds(5).Ticks));
await _clock.Delay(poll, ct).ConfigureAwait(false);
DateTimeOffset lastSign;
DateTimeOffset? leftAt;
lock (_gate)
{
if (!ReferenceEquals(_lease, lease))
@@ -735,15 +801,22 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
}
lastSign = _lastDrivingSign;
leftAt = _leftAt;
}
if (_clock.Now - lastSign < grace)
// Docker: the page of the browser left, or its last view closed by the page, and for a moment nothing came
// back (answer of 25 November 2026). Otherwise the long wait of a lease nobody uses.
var now = _clock.Now;
var left = leftAt is { } at && now - (at > lastSign ? at : lastSign) >= leaveGrace;
if (!left && now - lastSign < grace)
{
continue;
}
// Before the page goes back to the engine: the copy of the session (S4b, answer 5).
if (!await CopyBeforeLeaseEndsAsync(lease, lastSign, poll, ct).ConfigureAwait(false))
// Before the page goes back to the engine: the copy of the session (S4b, answer 5; leaving the page in
// Docker, answer of 25 November 2026).
var what = left ? PageLeftText : "Fine della guida del browser";
if (!await CopyBeforeLeaseEndsAsync(lease, lastSign, poll, what, ct).ConfigureAwait(false))
{
continue;
}
@@ -753,7 +826,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
// the lease, so that nothing slips in between.
if (ReleaseIdleLease(lease, lastSign))
{
_log.Info("collegamento", $"Nessuna vista del browser aperta da {ItalianText.Duration(grace)} senza «Ho finito» né «Annulla»: il motore può riprendere.");
_log.Info("collegamento", left
? $"{PageLeftText}: nessuna vista del browser riaperta, il motore può riprendere."
: $"Nessuna vista del browser aperta da {ItalianText.Duration(grace)} senza «Ho finito» né «Annulla»: il motore può riprendere.");
return;
}
}
@@ -777,7 +852,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
/// (<paramref name="poll"/>) of the copy is that hold, not Alberto. False when the lease must not end now: a sign of
/// driving came while the copy waited for its turn.
/// </summary>
private async Task<bool> CopyBeforeLeaseEndsAsync(IDisposable lease, DateTimeOffset lastSign, TimeSpan poll, CancellationToken ct)
private async Task<bool> CopyBeforeLeaseEndsAsync(IDisposable lease, DateTimeOffset lastSign, TimeSpan poll, string what, CancellationToken ct)
{
if (_copier is null)
{
@@ -828,7 +903,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
return false;
}
await CopyOnLetGoAsync("Fine della guida del browser", StillAlbertos, ct).ConfigureAwait(false);
await CopyOnLetGoAsync(what, StillAlbertos, ct).ConfigureAwait(false);
return true;
}
finally
@@ -1345,6 +1420,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
var taken = (_lease, _leaseWatch);
_lease = null;
_leaseWatch = null;
_leftAt = null;
return taken;
}
+20 -5
View File
@@ -26,6 +26,7 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
private readonly IClock _browserClock;
private readonly FebeLog _log;
private readonly Func<CancellationToken, Task>? _openStartPage;
private readonly Action? _viewClosed;
private readonly Lock _gate = new();
private readonly SemaphoreSlim _opening = new(1, 1);
private BrowserRemoteViewConnection? _current;
@@ -43,7 +44,12 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
/// What «Pagina iniziale» does (issue #52): the connection opens the start page once the engine's session ended and
/// with the page's turn; null: the button does nothing.
/// </param>
public BrowserRemoteViewHost(IFebeBrowser browser, BrowserUseTracker tracker, IClock clock, FebeLog log, IClock? browserClock = null, Func<CancellationToken, Task>? openStartPage = null)
/// <param name="viewClosed">
/// Told when a view ends from the page's side (the page left, the tab closed, the link lost), not when another view
/// takes its place or Febe stops: the connection then knows that Alberto may have left the browser (Docker, answer of
/// 25 November 2026, spec §6.0 «Quando si copia»); null: nobody is told.
/// </param>
public BrowserRemoteViewHost(IFebeBrowser browser, BrowserUseTracker tracker, IClock clock, FebeLog log, IClock? browserClock = null, Func<CancellationToken, Task>? openStartPage = null, Action? viewClosed = null)
{
_browser = browser ?? throw new ArgumentNullException(nameof(browser));
_tracker = tracker ?? throw new ArgumentNullException(nameof(tracker));
@@ -51,6 +57,7 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
_log = log ?? throw new ArgumentNullException(nameof(log));
_browserClock = browserClock ?? new SystemClock(clock.LocalZone);
_openStartPage = openStartPage;
_viewClosed = viewClosed;
}
/// <summary>
@@ -104,8 +111,8 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
catch (Exception ex)
{
// Whatever went wrong (the page gone away, an unexpected frame tree), nothing of the view stays behind:
// no lease, no screencast, no subscription.
await connection.DisposeAsync().ConfigureAwait(false);
// no lease, no screencast, no subscription. Ended by the server: the page did not leave the browser.
await connection.EndAsync(RemoteViewEnd.BrowserStopped).ConfigureAwait(false);
if (ex is CdpDisconnectedException or BrowserUnavailableException or CdpCommandException or TimeoutException)
{
throw new RemoteViewUnavailableException("Il browser di Febe non risponde: riprova fra qualche secondo.", ex);
@@ -158,7 +165,9 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
}
}
internal void Closed(BrowserRemoteViewConnection connection)
/// <param name="connection">The view that ended.</param>
/// <param name="byPage">True when the page ended it (left, tab closed, link lost), false when the server did.</param>
internal void Closed(BrowserRemoteViewConnection connection, bool byPage)
{
lock (_gate)
{
@@ -169,6 +178,10 @@ internal sealed class BrowserRemoteViewHost : IRemoteViewHost
}
_log.Info("vista", "Vista remota chiusa.");
if (byPage)
{
_viewClosed?.Invoke();
}
}
}
@@ -445,6 +458,8 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
return;
}
// No reason given by the server (another view, Febe stopping, the browser gone): the page closed it.
var byPage = Ended is null;
Ended ??= RemoteViewEnd.BrowserStopped;
await _ended.CancelAsync().ConfigureAwait(false);
_session.Screencast.FrameReceived -= OnFrame;
@@ -462,7 +477,7 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
// The browser is gone: the lease is released anyway.
}
_owner.Closed(this);
_owner.Closed(this, byPage);
}
private void MarkUsed()
+9 -1
View File
@@ -87,7 +87,15 @@ internal sealed class DockerPlatform(ServerOptions options) : IFebePlatform
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(app);
RemoteView = new BrowserRemoteViewHost(app.Browser!, app.Tracker, app.Clock, app.Log, openStartPage: ct => app.Connection?.OpenStartPageAsync(ct) ?? Task.CompletedTask);
// A view closed by its page may be Alberto leaving the browser (answer of 25 November 2026): the connection copies
// the session and gives the page back if no other view opens within a moment (spec §6.0 «Quando si copia»).
RemoteView = new BrowserRemoteViewHost(
app.Browser!,
app.Tracker,
app.Clock,
app.Log,
openStartPage: ct => app.Connection?.OpenStartPageAsync(ct) ?? Task.CompletedTask,
viewClosed: () => app.Connection?.RemoteViewClosed());
services.AddSingleton<IRemoteViewHost>(RemoteView);
}
+7 -1
View File
@@ -38,7 +38,9 @@ public static class ConnectionApi
});
// «Annulla», or a page left without either button ({ pageLeft: true }). On Windows the browser is a window of its
// own: leaving a page of Febe leaves it as it is, on screen until Alberto closes it (issue #52).
// own: leaving a page of Febe leaves it as it is, on screen until Alberto closes it (issue #52). In Docker leaving
// the page lets go of the browser like closing that window: the session is copied, not cancelled (answer of
// 25 November 2026). Only the button «Annulla» cancels without copying.
endpoints.MapPost("/api/connection/cancel", async (HttpContext context, IConnectionService connection, IBrowserWindow window, CancellationToken ct) =>
{
var body = await ApiHttp.OptionalBodyAsync(context, FebeJsonContext.Default.CancelDrivingRequest, new CancelDrivingRequest(false)).ConfigureAwait(false);
@@ -46,6 +48,10 @@ public static class ConnectionApi
{
await connection.ReleaseAsync(ct).ConfigureAwait(false);
}
else if (body.PageLeft)
{
await connection.LeavePageAsync(ct).ConfigureAwait(false);
}
else
{
await connection.CancelAsync(ct).ConfigureAwait(false);
@@ -114,6 +114,15 @@ public interface IConnectionService
/// </summary>
Task ReleaseAsync(CancellationToken ct);
/// <summary>
/// The Febe page that took the browser was left (another page, a reload, the tab closed) while the browser is the
/// remote view (Docker, answer of Alberto of 25 November 2026, spec §6.0 «Quando si copia»): like closing the window
/// «Browser di Febe» on Windows, not like «Annulla». If within a moment no view opens again and no «Accedi» / «Usa il
/// browser» comes (a reload is not leaving), the session is copied while the page is still his, and then the page goes
/// back to the engine. By default, for a service without a saved session to copy, it is «Annulla».
/// </summary>
Task LeavePageAsync(CancellationToken ct) => CancelAsync(ct);
/// <summary>«Scollega» (after confirmation in the UI): stops the engine and deletes the browser profile.</summary>
Task DisconnectAsync(CancellationToken ct);
}
+9 -5
View File
@@ -33,14 +33,18 @@ nessuna operazione configurata); altrimenti `{ state: "present"|"none"|"unreadab
valuesPresent, valuesExpected, missingValues: [nomi], docIdsPresent, docIdsExpected, missingDocIds: [operazioni],
neededMissing }`: solo conteggi e nomi, mai valori. Con il cookie di sessione nel browser ma senza una sessione salvata
leggibile lo stato è `copyMissing` («Serve «Ho finito»»): il motore aspetta, e `/api/engine` dice `copyMissing` nel
collegamento. La sessione si copia a «Ho finito», alla chiusura della finestra del browser su Windows e quando il
permesso di guida finisce da solo, sempre prima che la pagina torni al motore.
collegamento. La sessione si copia a «Ho finito», alla chiusura della finestra del browser su Windows, lasciando la
pagina del browser in Docker (sotto) e quando il permesso di guida finisce da solo, sempre prima che la pagina torni al
motore. «Annulla» non copia mai.
L'interfaccia manda `cancel` con `{ "pageLeft": true }` anche quando Alberto lascia la pagina Browser, o la pagina
Collegamento mentre accede, senza «Ho finito» né «Annulla»: un'altra pagina, la scheda chiusa o ricaricata (`fetch`
con `keepalive`, che porta l'`Origin` dell'interfaccia). In Docker vale come «Annulla» (il browser torna a riposo) e
non copia la sessione: in Docker la copia viene da «Ho finito», e dalla fine del permesso solo quando la pagina sparisce
senza riuscire a mandare `cancel` (rete caduta, sospensione).
con `keepalive`, che porta l'`Origin` dell'interfaccia). In Docker non è «Annulla» (`IConnectionService.LeavePageAsync`,
risposta di Alberto del 25 novembre 2026): è come chiudere la finestra del browser su Windows. Febe lo nota e aspetta
5 secondi; anche la vista remota chiusa dalla sua pagina (scheda chiusa, collegamento perso, non sostituita da un'altra
scheda) fa ricominciare l'attesa. Se in quel tempo non si apre un'altra vista e non arriva un `begin` (una pagina
ricaricata fa tutti e due), copia la sessione come alla chiusura della finestra (solo da una pagina del sito che non sia
di accesso o di verifica), poi il permesso finisce e il motore rimette il browser a riposo.
Su Windows, dove il browser è una finestra a parte, finisce solo il permesso tenuto dalla pagina: la finestra resta
com'è, sullo schermo e con la sua pagina, finché Alberto non la chiude (e finché è sullo schermo il motore resta
fermo). Dopo un «Ho finito» riuscito la pagina chiude la vista remota e non manda un nuovo `begin`.
+2 -2
View File
@@ -1,8 +1,8 @@
// Browser (spec §10.3 «Apri il browser»; mockup 10 without the steps column): in Docker the remote view of the
// server's browser and «Ho finito»; on Windows the browser is its own window. While Alberto uses the browser the
// engine is still; «Ho finito» lets «Riprendi» become active after a verification. Leaving the page without «Ho finito»
// (another page, a reload, closing the tab) gives the browser back, like «Annulla» (on Windows the window stays as it
// is until Alberto closes it). A browser at rest opens its start page; «?accesso=1» (the red strip of a disconnection)
// (another page, a reload, closing the tab) gives the browser back, like closing the window on Windows: the server copies
// the session if no view opens again within a moment (on Windows the window stays as it is until Alberto closes it). A browser at rest opens its start page; «?accesso=1» (the red strip of a disconnection)
// opens the login page, once: the parameter leaves the address straight away.
import { api } from '../core/api.js';
+4 -3
View File
@@ -42,9 +42,10 @@ export async function beginDriving(openLogin) {
/**
* The browser as a page uses it (spec §4.1, §10.3: the engine is still only while Alberto uses the browser). begin()
* takes it; «Ho finito», «Annulla» and «Scollega» give it back on the server, and the page calls settled(). Leaving the
* page in any other way — another page, a reload, closing the tab — gives it back too: in Docker like «Annulla» (the
* browser goes back to the empty page and the engine may work again); on Windows the window «Browser di Febe» stays as
* it is, on screen until Alberto closes it (issue #52), and only the page's hold ends.
* page in any other way — another page, a reload, closing the tab — gives it back too: in Docker like closing the
* window on Windows, not like «Annulla» (answer of 25 November 2026: if no view opens again within a moment, the
* server copies the session and the engine may work again; a reload opens the view again); on Windows the window
* «Browser di Febe» stays as it is, on screen until Alberto closes it (issue #52), and only the page's hold ends.
*/
export function drivingLease(page) {
let held = false;