Sessione salvata: correzioni della revisione di S4b (#36)
Un errore imprevisto durante la copia senza «Ho finito» (database, disco) non trattiene più per sempre il permesso di guida: la copia scrive solo il tipo d'errore e la guida finisce lo stesso, così il motore non resta in «Stai usando il browser». Un «Ho finito» tardivo dopo una copia fatta lasciando il browser dice che la sessione era già stata copiata invece di chiedere un nuovo accesso. Il registro distingue la finestra chiusa da quella ridotta a icona troppo a lungo. /api/engine e gli eventi prendono «Serve «Ho finito»» dall'istantanea del motore (EngineSnapshot.SessionCopyMissing) senza chiedere al browser a ogni evento. La prova di WebView2 aspetta la riga del registro, non solo la sessione in memoria. Documenti: nove operazioni GraphQL con __spin_*, H23 descritta in prove-dal-vivo (parte E), guida aggiornata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
883ea4683c
commit
c69755db23
20 files changed
+310
-54
No files matched your search
@@ -84,6 +84,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
private bool? _disconnectedByUser;
|
||||
private DateTimeOffset _checkedAt = DateTimeOffset.MinValue;
|
||||
private DateTimeOffset _lastCopyAt = DateTimeOffset.MinValue;
|
||||
private SessionCopyOutcome? _letGoCopy;
|
||||
|
||||
/// <param name="browser">The browser of Febe.</param>
|
||||
/// <param name="storage">The database (first connection, «Scollega»).</param>
|
||||
@@ -205,14 +206,14 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The window «Browser di Febe» was closed by Alberto without «Ho finito» (Windows, issue #36, S4b, answer 5): the
|
||||
/// session is copied while the page is still his. The caller (the window) holds the driving gate until this returns,
|
||||
/// so the page goes back to the engine only afterwards. Gives up after <see cref="LetGoWait"/>. Never throws for the
|
||||
/// browser; logs counts only.
|
||||
/// The window «Browser di Febe» was closed by Alberto without «Ho finito», or left minimised too long (Windows, issue
|
||||
/// #36, S4b, answer 5): the session is copied while the page is still his. The caller (the window) holds the driving
|
||||
/// gate until this returns, so the page goes back to the engine only afterwards. Gives up after
|
||||
/// <see cref="LetGoWait"/>. Never throws for the browser; logs counts only, and says which of the two happened.
|
||||
/// </summary>
|
||||
public async Task CopyOnWindowClosedAsync(CancellationToken ct)
|
||||
public async Task CopyOnWindowClosedAsync(BrowserWindowLetGo how, CancellationToken ct)
|
||||
{
|
||||
const string What = "Finestra del browser chiusa";
|
||||
var what = how == BrowserWindowLetGo.MinimizedTooLong ? "Finestra del browser ridotta a icona troppo a lungo" : "Finestra del browser chiusa";
|
||||
if (_copier is null || !_browser.IsAvailable)
|
||||
{
|
||||
return;
|
||||
@@ -226,7 +227,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
catch (OperationCanceledException) when (limit.IsCancellationRequested)
|
||||
{
|
||||
_log.Info("collegamento", $"{What}: sessione non copiata, un altro comando del collegamento era ancora in corso.");
|
||||
_log.Info("collegamento", $"{what}: sessione non copiata, un altro comando del collegamento era ancora in corso.");
|
||||
return;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
@@ -236,7 +237,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
|
||||
try
|
||||
{
|
||||
await CopyOnLetGoAsync(What, () => _browser.UserIsDriving, limit.Token).ConfigureAwait(false);
|
||||
await CopyOnLetGoAsync(what, () => _browser.UserIsDriving, limit.Token).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -254,6 +255,8 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
{
|
||||
if (_lease is null)
|
||||
{
|
||||
// A new use of the browser: what an earlier one copied when it let go is no longer this one's.
|
||||
_letGoCopy = null;
|
||||
_lease = _browser.UserDriving.Begin();
|
||||
var watch = new CancellationTokenSource();
|
||||
_leaseWatch = watch;
|
||||
@@ -403,7 +406,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
var first = connected && await MarkConnectedAsync(ct).ConfigureAwait(false);
|
||||
|
||||
// The copy for the site client (S4): while the page still holds the session and is still Alberto's.
|
||||
var copy = connected && _copier is not null ? await CopySessionAsync(ct).ConfigureAwait(false) : null;
|
||||
var copy = connected && _copier is not null ? await CopySessionAsync(ct).ConfigureAwait(false) : default;
|
||||
await RestIfAlbertosAsync().ConfigureAwait(false);
|
||||
await HideQuietlyAsync().ConfigureAwait(false);
|
||||
ReleaseLease();
|
||||
@@ -427,9 +430,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
message = "Collegato a Instagram. Febe riprende da solo alla prossima sessione.";
|
||||
}
|
||||
|
||||
if (copy is not null)
|
||||
if (copy.Outcome is { } outcome)
|
||||
{
|
||||
message += " " + CopyMessage(copy);
|
||||
message += " " + CopyMessage(outcome, copy.Earlier);
|
||||
}
|
||||
|
||||
_log.Info("collegamento", connected ? "«Ho finito»: sessione presente nel browser." : "«Ho finito»: sessione assente nel browser.");
|
||||
@@ -748,6 +751,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
{
|
||||
// The lease ended with a command of Alberto.
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// Whatever failed, the lease must not hold the engine still for ever (rule 10: the kind of error only).
|
||||
_log.Warn("collegamento", $"Controllo della fine della guida del browser interrotto da un errore ({ex.GetType().Name}): il motore può riprendere.");
|
||||
ReleaseLease(lease);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -773,7 +782,15 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
|
||||
// Cancelled when «Ho finito», «Annulla» or «Scollega» end the lease meanwhile: then they decide.
|
||||
await _commands.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await _commands.WaitAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
return true; // Febe is stopping
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
bool StillAlbertos()
|
||||
@@ -844,6 +861,11 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
lock (_gate)
|
||||
{
|
||||
_lastCopyAt = _clock.Now;
|
||||
if (outcome.Status is SessionCopyStatus.Saved or SessionCopyStatus.SavedInMemoryOnly or SessionCopyStatus.KeptComplete)
|
||||
{
|
||||
// A late «Ho finito» of this use says that the session was already copied (S4b).
|
||||
_letGoCopy = outcome;
|
||||
}
|
||||
}
|
||||
|
||||
_log.Info("collegamento", $"{what}: {LetGoSummary(outcome)}.");
|
||||
@@ -857,6 +879,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
{
|
||||
_log.Info("collegamento", $"{what}: sessione non copiata, il browser non ha risposto in tempo.");
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// The database, the disk, Febe stopping: never the lease held for ever because of it. The kind of error only
|
||||
// (rule 10): the page and the saved session hold what the session holds.
|
||||
_log.Warn("collegamento", $"{what}: copia della sessione non riuscita per un errore ({ex.GetType().Name}).");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>What a copy without «Ho finito» did, for the log (counts only).</summary>
|
||||
@@ -977,20 +1005,30 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// The copy at «Ho finito» (issue #36, S4, §6.0): only while Alberto's own lease («Accedi», «Usa il browser») still
|
||||
/// holds — after it ended the page is the engine's, and nothing is read from it (rule 7). A hold of the driving gate
|
||||
/// keeps the lease from ending by itself meanwhile, and the page turn keeps a step of the profile search off the
|
||||
/// page; a lease that ended all the same before the copy is saved throws the copy away.
|
||||
/// page; a lease that ended all the same before the copy is saved throws the copy away. A late «Ho finito», after
|
||||
/// this use of the browser already copied when it let go (S4b), reads nothing and reports that earlier copy
|
||||
/// (<c>Earlier</c>), as long as the saved session is still there.
|
||||
/// </summary>
|
||||
private async Task<SessionCopyOutcome> CopySessionAsync(CancellationToken ct)
|
||||
private async Task<(SessionCopyOutcome? Outcome, bool Earlier)> CopySessionAsync(CancellationToken ct)
|
||||
{
|
||||
IDisposable? lease;
|
||||
SessionCopyOutcome? earlier;
|
||||
lock (_gate)
|
||||
{
|
||||
lease = _lease;
|
||||
earlier = _letGoCopy;
|
||||
}
|
||||
|
||||
if (lease is null || !_browser.UserIsDriving)
|
||||
{
|
||||
if (earlier is not null && _copier!.SavedState == SavedSessionState.Present)
|
||||
{
|
||||
_log.Info("collegamento", "«Ho finito»: sessione non copiata di nuovo, era già stata copiata quando il browser è tornato al motore.");
|
||||
return (earlier, true);
|
||||
}
|
||||
|
||||
_log.Info("collegamento", "«Ho finito»: sessione non copiata, il browser era già tornato al motore.");
|
||||
return new SessionCopyOutcome(SessionCopyStatus.Discarded, null, 0, 0, 0);
|
||||
return (new SessionCopyOutcome(SessionCopyStatus.Discarded, null, 0, 0, 0), false);
|
||||
}
|
||||
|
||||
var hold = _browser.UserDriving.Begin();
|
||||
@@ -1000,7 +1038,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
if (_engine is not null && turn is null)
|
||||
{
|
||||
_log.Info("collegamento", "«Ho finito»: sessione non copiata, la pagina era occupata dalla ricerca dei profili.");
|
||||
return new SessionCopyOutcome(SessionCopyStatus.Refused, CaptureRefusal.BrowserError, 0, 0, 0);
|
||||
return (new SessionCopyOutcome(SessionCopyStatus.Refused, CaptureRefusal.BrowserError, 0, 0, 0), false);
|
||||
}
|
||||
|
||||
bool StillAlbertos()
|
||||
@@ -1011,7 +1049,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
}
|
||||
|
||||
return await _copier!.CopyAsync(StillAlbertos, ct).ConfigureAwait(false);
|
||||
return (await _copier!.CopyAsync(StillAlbertos, ct).ConfigureAwait(false), false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -1023,11 +1061,14 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// The clause of the «Ho finito» message about the copy, in Italian (counts only). It says what the code does: a
|
||||
/// value a configured operation needs and the page did not give stops that request as a disconnection (§6.0), never
|
||||
/// a way back to the browser; and after a refusal a new «Ho finito» needs the browser opened again, because
|
||||
/// «Ho finito» always ends the lease.
|
||||
/// «Ho finito» always ends the lease. With <paramref name="earlier"/> the copy is the one made a moment before, when
|
||||
/// the browser went back to the engine without «Ho finito» (S4b): the message says so, and never asks for a new login
|
||||
/// when that copy is complete.
|
||||
/// </summary>
|
||||
internal static string CopyMessage(SessionCopyOutcome copy)
|
||||
internal static string CopyMessage(SessionCopyOutcome copy, bool earlier = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(copy);
|
||||
const string Before = "La sessione per le richieste era già stata copiata quando il browser è tornato al motore";
|
||||
const string Again = "Riapri il browser dalla pagina Collegamento («Usa il browser» o «Pagina iniziale») e premi di nuovo «Ho finito» sulla tua pagina iniziale.";
|
||||
var missing = copy.MissingValues + copy.MissingDocIds;
|
||||
var missingClause = missing == 0
|
||||
@@ -1037,6 +1078,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
$" Alla pagina però mancavano {missing} valori che servono alle richieste: la copia ha comunque sostituito quella salvata prima, e le richieste che li usano non partono. Alla prima Febe si ferma come disconnessione e chiede un nuovo accesso. {Again}");
|
||||
return copy.Status switch
|
||||
{
|
||||
SessionCopyStatus.Saved when earlier => Before + "." + (missing == 0 ? " Non serve rifare l'accesso." : missingClause),
|
||||
SessionCopyStatus.SavedInMemoryOnly when earlier =>
|
||||
Before + ", ma non ancora scritta su disco: Febe riprova alla prossima scrittura." + (missing == 0 ? " Non serve rifare l'accesso." : missingClause),
|
||||
SessionCopyStatus.KeptComplete when earlier => string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"Quando il browser è tornato al motore alla pagina mancavano {missing} valori che servono alle richieste: come hai scelto nella pagina Collegamento («Tieni la copia completa»), Febe tiene la copia completa salvata prima. Se nel frattempo hai rifatto l'accesso o cambiato account, scegli «Sostituisci comunque». {Again}"),
|
||||
SessionCopyStatus.Saved => "Sessione copiata per le richieste di Febe." + missingClause,
|
||||
SessionCopyStatus.SavedInMemoryOnly =>
|
||||
"Sessione copiata per le richieste di Febe, ma non ancora scritta su disco: Febe riprova alla prossima scrittura." + missingClause,
|
||||
|
||||
Reference in new issue
Block a user