test(connect): phase 5 — restart lands on the selected profile, no login (#827)
* test(connect): pin restart-with-scoped-profile token resolution (phase 5) * fix(profiles): scope switch via un-clamped active-profile state The switch persisted the server profile through serverStore.selectedProfileId, whose selfHealed() clamps ids absent from the local connection list — server profiles like "meow" never exist there, so the scope silently reset to default and the re-dialed socket created the fresh session in the wrong profile (live logcat 2026-08-06: session.create carried profile=default right after switching to meow). - AuthManager: prefs-backed activeProfileId state, immune to the clamp, restored on startup; resetAuthStateForTest clears it - ProfileScopeInterceptor + WsProfileParams + ChatViewModel fetchContextUsage: read the active profile instead of the connection selection - ProfileSwitchCoordinator: write activeProfileId; dial the socket on Dispatchers.IO (the ticket mint did blocking I/O on the main thread — NetworkOnMainThreadException — and fell back to a 1s reconnect) - ProfilesViewModel: inject the IO dispatcher; ProfilesViewModelTest drops its static Dispatchers mock (JVM-wide bleed class) - Tests: WsProfileParams/Interceptor tests use the real prefs-backed state instead of mockkObject(AuthManager) (re-mock breaks in suite order after real AuthManager init); AuthManagerTest + Issue647ProfileUrlTest cancel the init() DataStore scope in tearDown (UncaughtExceptionsBeforeTest phantoms); regression test pins the clamp on the old path vs the new state * fix(profiles): resolve getter/property JVM collision; de-poison slash test - AuthManager.activeProfileId (StateFlow) and a getActiveProfileId() function generated the same JVM getter name — calls silently resolved to the property getter at runtime (ReadonlyStateFlow -> String ClassCastException, CI unit red 2026-08-06). Function removed; callers read activeProfileId.value. - ChatViewModel: inject ioDispatcher (38 sites) so SlashCommandDispatchRpcTest can drop its mockkStatic(Dispatchers) bleed without racing real IO. - SlashCommandDispatchRpcTest: drop the static Dispatchers mock (JVM-wide bleed; CI cross-wired COMMAND_DISPATCH captures in suite order), construct the VM with the test dispatcher. --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
dd8dabecb0
commit
d9f437939a
14 files changed
+225
-73
No files matched your search
@@ -41,6 +41,7 @@ import kotlinx.coroutines.runBlocking
|
||||
*/
|
||||
object AuthManager {
|
||||
private const val PREFS_FILE = "hermes_secure_prefs"
|
||||
private const val KEY_ACTIVE_PROFILE_ID = "active_profile_id"
|
||||
|
||||
const val DEFAULT_PROFILE_ID = "default"
|
||||
const val DEFAULT_PROFILE_NAME = "Default"
|
||||
@@ -80,6 +81,21 @@ object AuthManager {
|
||||
private val _selectedProfileFlow = MutableStateFlow<String?>(null)
|
||||
val selectedProfileFlow: StateFlow<String?> = _selectedProfileFlow.asStateFlow()
|
||||
|
||||
/**
|
||||
* Active server-side Hermes profile scope (e.g. "default" / "meow").
|
||||
*
|
||||
* DISTINCT from [selectedProfileId] (which selects a LOCAL connection
|
||||
* profile — a server + its token). The active profile is the scope
|
||||
* injected as REST `?profile=` and WS `params.profile`. It deliberately
|
||||
* does NOT route through [ServerStoreState.selfHealed]: server profile
|
||||
* ids are not guaranteed to exist in the local connection list, and
|
||||
* clamping them silently reset the scope to "default" — the live
|
||||
* profile-switch bug (logcat 2026-08-06: session.create carried
|
||||
* profile=default right after switching to meow).
|
||||
*/
|
||||
private val _activeProfileId = MutableStateFlow<String?>(null)
|
||||
val activeProfileId: StateFlow<String?> = _activeProfileId.asStateFlow()
|
||||
|
||||
private val _baseUrlFlow = MutableStateFlow("")
|
||||
val baseUrlFlow: StateFlow<String> = _baseUrlFlow.asStateFlow()
|
||||
|
||||
@@ -146,6 +162,7 @@ object AuthManager {
|
||||
)
|
||||
migrateLegacyDefaultIfNeeded(p)
|
||||
_tokenFlow.value = getTokenInternal(p)
|
||||
_activeProfileId.value = p.getString(KEY_ACTIVE_PROFILE_ID, null)?.takeIf { it.isNotBlank() }
|
||||
p
|
||||
}
|
||||
}
|
||||
@@ -374,6 +391,28 @@ object AuthManager {
|
||||
appScope?.launch { syncCookieStoreForProfile(id) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the active server-side Hermes profile scope.
|
||||
*
|
||||
* Unlike [setSelectedProfileId], this does NOT touch the server store —
|
||||
* it is prefs-backed and immune to [ServerStoreState.selfHealed], so a
|
||||
* server profile that has no local [ConnectionProfile] (the normal case)
|
||||
* survives the switch instead of being clamped to null/default.
|
||||
*
|
||||
* NOTE: read the value via [activeProfileId].value — a JVM getter named
|
||||
* `getActiveProfileId()` would collide with the property's generated
|
||||
* getter (same name, different return type) and silently resolve to the
|
||||
* property getter at runtime (ClassCastException: ReadonlyStateFlow ->
|
||||
* String, 2026-08-06 CI).
|
||||
*/
|
||||
fun setActiveProfileId(id: String?) {
|
||||
val normalized = id?.takeIf { it.isNotBlank() }
|
||||
_activeProfileId.value = normalized
|
||||
runCatching {
|
||||
requirePrefs().edit().putString(KEY_ACTIVE_PROFILE_ID, normalized).apply()
|
||||
}
|
||||
}
|
||||
|
||||
private fun normalizedProfileId(profileId: String?): String =
|
||||
profileId?.takeIf { it.isNotBlank() } ?: DEFAULT_PROFILE_ID
|
||||
|
||||
@@ -416,6 +455,7 @@ object AuthManager {
|
||||
}
|
||||
appScope = null
|
||||
}
|
||||
_activeProfileId.value = null
|
||||
}
|
||||
|
||||
fun getToken(): String? {
|
||||
|
||||
@@ -60,7 +60,7 @@ object ProfileScopeInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
val profile =
|
||||
AuthManager.getSelectedProfileId()
|
||||
AuthManager.activeProfileId.value
|
||||
?: return chain.proceed(request)
|
||||
|
||||
val url = request.url
|
||||
|
||||
@@ -41,10 +41,16 @@ object ProfileSwitchCoordinator {
|
||||
}
|
||||
if (result !is NetworkResult.Success) return result
|
||||
|
||||
AuthManager.setSelectedProfileId(name)
|
||||
AuthManager.setActiveProfileId(name)
|
||||
_switched.emit(name)
|
||||
HermesWsClient.disconnect()
|
||||
HermesWsClient.connect()
|
||||
// The ticket mint inside connect() does blocking network I/O — it must
|
||||
// run off the main thread or the dial crashes with
|
||||
// NetworkOnMainThreadException and falls back to the 1s reconnect
|
||||
// retry (visible in the 2026-08-06 live logcat).
|
||||
withContext(Dispatchers.IO) {
|
||||
HermesWsClient.disconnect()
|
||||
HermesWsClient.connect()
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import com.m57.hermescontrol.data.local.AuthManager
|
||||
* (usually "default"), even after the user switches profiles in the app.
|
||||
*
|
||||
* Rules (mirror the REST interceptor contract):
|
||||
* - If [AuthManager.getSelectedProfileId] is `null` → pass through unchanged
|
||||
* - If [AuthManager.activeProfileId] is `null` → pass through unchanged
|
||||
* (legacy single-profile behavior).
|
||||
* - If the caller already supplied an explicit `"profile"` key in [params] →
|
||||
* explicit wins; do **not** overwrite.
|
||||
@@ -29,7 +29,7 @@ object WsProfileParams {
|
||||
params: Map<String, Any>,
|
||||
): Map<String, Any> {
|
||||
// No active profile → legacy single-profile behavior.
|
||||
val profile = AuthManager.getSelectedProfileId() ?: return params
|
||||
val profile = AuthManager.activeProfileId.value ?: return params
|
||||
|
||||
// Only scoped methods get the profile key.
|
||||
if (method !in WsMethods.PROFILE_SCOPED_METHODS) return params
|
||||
|
||||
@@ -2263,7 +2263,7 @@ class ChatViewModel(
|
||||
*/
|
||||
fun fetchContextUsage() {
|
||||
val sessionId = _uiState.value.currentSessionId ?: return
|
||||
val profile = AuthManager.getSelectedProfileId()
|
||||
val profile = AuthManager.activeProfileId.value
|
||||
viewModelScope.launch(ioDispatcher) {
|
||||
// Denominator fallback: full context window (cheap, public, rarely
|
||||
// changes). The RPC's context_max below overrides it when present.
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.m57.hermescontrol.data.remote.NetworkResult
|
||||
import com.m57.hermescontrol.data.remote.safeApiCall
|
||||
import com.m57.hermescontrol.data.session.ProfileSwitchCoordinator
|
||||
import com.m57.hermescontrol.ui.common.ToastHost
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
@@ -52,8 +53,9 @@ data class ProfilesUiState(
|
||||
val isSearchingHub: Boolean = false,
|
||||
)
|
||||
|
||||
class ProfilesViewModel :
|
||||
ViewModel(),
|
||||
class ProfilesViewModel(
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
|
||||
) : ViewModel(),
|
||||
ToastHost {
|
||||
private val _uiState = MutableStateFlow(ProfilesUiState())
|
||||
val uiState: StateFlow<ProfilesUiState> = _uiState.asStateFlow()
|
||||
@@ -63,9 +65,9 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
coroutineScope {
|
||||
val profilesDeferred = async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getProfiles() } }
|
||||
val profilesDeferred = async(ioDispatcher) { safeApiCall { ApiClient.hermesApi.getProfiles() } }
|
||||
val activeDeferred =
|
||||
async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getActiveProfile() } }
|
||||
async(ioDispatcher) { safeApiCall { ApiClient.hermesApi.getActiveProfile() } }
|
||||
|
||||
val profilesResult = profilesDeferred.await()
|
||||
val activeResult = activeDeferred.await()
|
||||
@@ -130,7 +132,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isLoadingSoul = true, selectedSoulContent = null) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.getProfileSoul(profileName) }
|
||||
}
|
||||
when (result) {
|
||||
@@ -161,7 +163,7 @@ class ProfilesViewModel :
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.updateProfileSoul(
|
||||
profileName,
|
||||
@@ -198,7 +200,7 @@ class ProfilesViewModel :
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.updateProfileModel(
|
||||
profileName,
|
||||
@@ -238,7 +240,7 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(isLoading = true) }
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.cloneProfile(
|
||||
sourceProfileName,
|
||||
@@ -275,7 +277,7 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(isLoading = true) }
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.updateProfileDescription(
|
||||
profileName,
|
||||
@@ -313,7 +315,7 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(isLoading = true) }
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.renameProfile(
|
||||
oldName,
|
||||
@@ -347,7 +349,7 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(isLoading = true) }
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.deleteProfile(name) }
|
||||
}
|
||||
when (result) {
|
||||
@@ -376,7 +378,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isAutoDescribing = true) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall {
|
||||
ApiClient.hermesApi.describeProfileAuto(
|
||||
name,
|
||||
@@ -425,7 +427,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isLoadingSetupCommand = true, setupCommand = null) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.getProfileSetupCommand(name) }
|
||||
}
|
||||
when (result) {
|
||||
@@ -454,7 +456,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isLoadingBuilderData = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.getModelOptions() }
|
||||
}
|
||||
when (result) {
|
||||
@@ -502,8 +504,8 @@ class ProfilesViewModel :
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
coroutineScope {
|
||||
val modelsDeferred = async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getModelOptions() } }
|
||||
val skillsDeferred = async(Dispatchers.IO) { safeApiCall { ApiClient.hermesApi.getSkills() } }
|
||||
val modelsDeferred = async(ioDispatcher) { safeApiCall { ApiClient.hermesApi.getModelOptions() } }
|
||||
val skillsDeferred = async(ioDispatcher) { safeApiCall { ApiClient.hermesApi.getSkills() } }
|
||||
|
||||
val modelsResult = modelsDeferred.await()
|
||||
val skillsResult = skillsDeferred.await()
|
||||
@@ -545,7 +547,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isSearchingHub = true) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.searchSkillsHub(query) }
|
||||
}
|
||||
when (result) {
|
||||
@@ -577,7 +579,7 @@ class ProfilesViewModel :
|
||||
_uiState.update { it.copy(isLoading = true) }
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
withContext(ioDispatcher) {
|
||||
safeApiCall { ApiClient.hermesApi.createProfile(request) }
|
||||
}
|
||||
when (result) {
|
||||
|
||||
Reference in new issue
Block a user