feat(profiles): phase 2 — atomic switch coordinator (#824)
* feat(profiles): ProfileSwitchCoordinator — atomic re-home on profile switch (phase 2) * fix(ws): deterministic reconnect backoff in tests (testAutoReconnect flake) * test(ws): harden testAutoReconnect against CI latency (stub token refresh, generous latches) * test(profiles): remove static Dispatchers mock from coordinator test — it bled into HermesWsClientTest in full-suite runs --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
398268d105
commit
531219cdb5
8 files changed
+241
-84
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package com.m57.hermescontrol.data.session
|
||||
|
||||
import com.m57.hermescontrol.data.local.AuthManager
|
||||
import com.m57.hermescontrol.data.model.SetActiveProfileRequest
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import com.m57.hermescontrol.data.remote.NetworkResult
|
||||
import com.m57.hermescontrol.data.remote.safeApiCall
|
||||
import com.m57.hermescontrol.data.ws.HermesWsClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* The single flow that performs a profile switch — the mobile equivalent of
|
||||
* desktop's re-home (``requestFreshSession`` + socket swap). Every surface
|
||||
* that switches profiles goes through here, so the switch is atomic instead
|
||||
* of a pile of scattered patches.
|
||||
*
|
||||
* Order matters:
|
||||
* 1. Flip the server's sticky active profile (REST).
|
||||
* 2. Persist the LOCAL selection — the REST interceptor (``?profile=``) and
|
||||
* the WS params injector (``params.profile``) now scope everything to the
|
||||
* new profile. The per-server token fallback (phase 1) keeps auth intact:
|
||||
* no re-login, restart-safe.
|
||||
* 3. Emit [switched] BEFORE the socket re-dial, so chat wipes its stale
|
||||
* conversation first — when the reconnected socket delivers
|
||||
* ``gateway.ready``, ``handleGatewayReady`` sees no open session and
|
||||
* auto-creates a FRESH session in the new profile (desktop parity).
|
||||
* 4. Re-dial the WebSocket so the gateway re-homes chat to the new profile.
|
||||
*/
|
||||
object ProfileSwitchCoordinator {
|
||||
private val _switched = MutableSharedFlow<String>(extraBufferCapacity = 1)
|
||||
val switched: SharedFlow<String> = _switched.asSharedFlow()
|
||||
|
||||
suspend fun switchProfile(name: String): NetworkResult<Unit> {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
safeApiCall { ApiClient.hermesApi.setActiveProfile(SetActiveProfileRequest(name)) }
|
||||
}
|
||||
if (result !is NetworkResult.Success) return result
|
||||
|
||||
AuthManager.setSelectedProfileId(name)
|
||||
_switched.emit(name)
|
||||
HermesWsClient.disconnect()
|
||||
HermesWsClient.connect()
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -65,9 +65,21 @@ object HermesWsClient {
|
||||
|
||||
// ── Backoff settings ─────────────────────────────────────────────────
|
||||
|
||||
private const val INITIAL_BACKOFF_MS = 1_000L
|
||||
private var initialBackoffMs = 1_000L
|
||||
private const val MAX_BACKOFF_MS = 30_000L
|
||||
private const val BACKOFF_MULTIPLIER = 2.0
|
||||
|
||||
/**
|
||||
* Test hook: the initial reconnect backoff, overridable so reconnect
|
||||
* tests are deterministic instead of racing real time (CI runners
|
||||
* routinely exceed fixed latch windows). Production keeps 1s.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
internal fun setReconnectBackoffForTest(initialMillis: Long) {
|
||||
initialBackoffMs = initialMillis
|
||||
currentBackoff = initialMillis
|
||||
}
|
||||
|
||||
private const val MAX_OUTBOUND_MESSAGE_BYTES = 16 * 1024 * 1024
|
||||
private const val OUTBOUND_DRAIN_TIMEOUT_MS = 60_000L
|
||||
|
||||
@@ -89,7 +101,7 @@ object HermesWsClient {
|
||||
private var closingSocket: WebSocket? = null
|
||||
|
||||
@Volatile
|
||||
private var currentBackoff = INITIAL_BACKOFF_MS
|
||||
private var currentBackoff = initialBackoffMs
|
||||
|
||||
private val wsScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
@@ -260,7 +272,7 @@ object HermesWsClient {
|
||||
// therefore allowed to leave the terminal AUTH_EXPIRED state.
|
||||
intentionalClose.set(false)
|
||||
_connectionStatus.value = ConnectionStatus.CONNECTING
|
||||
currentBackoff = INITIAL_BACKOFF_MS
|
||||
currentBackoff = initialBackoffMs
|
||||
true
|
||||
}
|
||||
if (socketToCancel != null) {
|
||||
@@ -282,7 +294,7 @@ object HermesWsClient {
|
||||
return
|
||||
}
|
||||
Log.d(TAG, "Default network changed — reconnecting WebSocket")
|
||||
currentBackoff = INITIAL_BACKOFF_MS
|
||||
currentBackoff = initialBackoffMs
|
||||
reconnectJob?.cancel()
|
||||
reconnectJob = null
|
||||
outboundDrainJob?.cancel()
|
||||
@@ -796,7 +808,7 @@ object HermesWsClient {
|
||||
Log.i(TAG, "WebSocket opened")
|
||||
connected.set(true)
|
||||
_connectionStatus.value = ConnectionStatus.CONNECTED
|
||||
currentBackoff = INITIAL_BACKOFF_MS
|
||||
currentBackoff = initialBackoffMs
|
||||
startHealthTracking()
|
||||
|
||||
while (true) {
|
||||
|
||||
Reference in new issue
Block a user