diff --git a/app/src/main/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepository.kt b/app/src/main/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepository.kt index e6a3c9867..72320f312 100644 --- a/app/src/main/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepository.kt +++ b/app/src/main/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepository.kt @@ -47,6 +47,8 @@ data class RuntimeCatalogState( * instead of looking dead. */ val agentsLoading: Boolean = false, + /** Whether the catalog's own agent request can still supply a result. */ + internal val agentsInitialReadPending: Boolean = false, ) class RuntimeCatalogRepository( @@ -182,13 +184,14 @@ class RuntimeCatalogRepository( // as the fetch took — seconds, while a stopped runtime is retried — and OpenCode // models appeared under Claude Code. if (current.runtime?.id != target.id) { - RuntimeCatalogState(runtime = target, isRefreshing = true, agentsLoading = true) + RuntimeCatalogState(runtime = target, isRefreshing = true, agentsLoading = true, agentsInitialReadPending = true) } else { current.copy( runtime = target, isRefreshing = true, error = null, agentsLoading = !current.agents.hasSelectableAgent(), + agentsInitialReadPending = true, ) } } @@ -217,12 +220,36 @@ class RuntimeCatalogRepository( ) return } + mutableState.update { + if (it.runtime?.id == target.id) it.copy(health = connection.getOrNull()) else it + } val catalog = supervisorScope { val sessions = async { runCatching { target.listSessions() } } val providers = async { runCatching { target.listProviders() } } - val agents = async { runCatching { target.listAgents() } } + // The picker can use agents as soon as they arrive, even if a provider or + // workspace request is still waiting on the runtime. + val agents = + async { + runCatching { target.listAgents() }.also { result -> + val list = result.getOrNull() + if (registry.selected.value?.id == target.id) { + mutableState.update { state -> + if (state.runtime?.id != target.id) { + state + } else { + val completed = state.copy(agentsInitialReadPending = false) + if (list.hasSelectableAgent()) completed.withAgents(list.orEmpty()) else completed + } + } + if (!mutableState.value.agentsFor(target).hasSelectableAgent()) { + // Retry immediately; unrelated requests may remain in flight. + refreshAgents() + } + } + } + } val workspaces = async { runCatching { target.listWorkspaces() } } LoadedCatalog( sessions = sessions.await(), @@ -355,9 +382,9 @@ class RuntimeCatalogRepository( when { it.runtime?.id != target.id -> it list != null -> it.withAgents(list) - // A load still in flight for this runtime keeps the chip loading: it reads the - // agents again itself, and a "tap to retry" now would only race it. - else -> it.copy(agentsLoading = it.isRefreshing) + // A separate initial agent read may still succeed; other catalog reads + // alone must not keep the agent spinner running after retries give up. + else -> it.copy(agentsLoading = it.agentsInitialReadPending) } } } diff --git a/app/src/test/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepositoryTest.kt b/app/src/test/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepositoryTest.kt index 4247b1cab..b4848b473 100644 --- a/app/src/test/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepositoryTest.kt +++ b/app/src/test/java/com/yugahashimoto/andcode/data/repository/RuntimeCatalogRepositoryTest.kt @@ -19,12 +19,14 @@ import com.yugahashimoto.andcode.runtime.RuntimeState import com.yugahashimoto.andcode.runtime.RuntimeTarget import com.yugahashimoto.andcode.runtime.RuntimeType import com.yugahashimoto.andcode.runtime.WorkspaceRef +import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json @@ -41,6 +43,85 @@ class RuntimeCatalogRepositoryTest { @get:Rule val temporaryFolder = TemporaryFolder() + @Test + fun `agents become selectable while providers are still loading`() = + runTest { + val dispatcher = StandardTestDispatcher(testScheduler) + val target = FakeTarget("mac") + val providersGate = CompletableDeferred() + target.providersGate = providersGate + val registry = + RuntimeRegistry( + store = FakeStore(selectedRuntimeId = "mac"), + localTarget = FakeTarget("local-android", RuntimeType.LOCAL), + remoteFactory = { target }, + ) + val repository = RuntimeCatalogRepository(registry, TestScope(dispatcher)) + + testScheduler.runCurrent() + + assertEquals(listOf("build"), repository.state.value.agents.map { it.name }) + assertFalse(repository.state.value.agentsLoading) + assertTrue(repository.state.value.isRefreshing) + providersGate.complete(Unit) + advanceUntilIdle() + assertEquals(listOf("build"), repository.state.value.agents.map { it.name }) + assertFalse(repository.state.value.isRefreshing) + } + + @Test + fun `agent retry succeeds while providers are still loading`() = + runTest { + val dispatcher = StandardTestDispatcher(testScheduler) + val target = FakeTarget("mac") + target.agentAnswers.add { error("temporarily unavailable") } + val providersGate = CompletableDeferred() + target.providersGate = providersGate + val registry = + RuntimeRegistry( + store = FakeStore(selectedRuntimeId = "mac"), + localTarget = FakeTarget("local-android", RuntimeType.LOCAL), + remoteFactory = { target }, + ) + val repository = RuntimeCatalogRepository(registry, TestScope(dispatcher)) + + testScheduler.runCurrent() + + assertEquals(listOf("build"), repository.state.value.agents.map { it.name }) + assertFalse(repository.state.value.agentsLoading) + assertTrue(repository.state.value.isRefreshing) + providersGate.complete(Unit) + advanceUntilIdle() + assertNull(repository.state.value.agentsError) + } + + @Test + fun `exhausted agent retries stop loading even while providers are pending`() = + runTest { + val dispatcher = StandardTestDispatcher(testScheduler) + val target = FakeTarget("mac") + repeat(6) { target.agentAnswers.add { error("temporarily unavailable") } } + val providersGate = CompletableDeferred() + target.providersGate = providersGate + val registry = + RuntimeRegistry( + store = FakeStore(selectedRuntimeId = "mac"), + localTarget = FakeTarget("local-android", RuntimeType.LOCAL), + remoteFactory = { target }, + ) + val repository = RuntimeCatalogRepository(registry, TestScope(dispatcher)) + + testScheduler.runCurrent() + advanceTimeBy(13_000) + testScheduler.runCurrent() + + assertTrue(repository.state.value.agents.isEmpty()) + assertFalse(repository.state.value.agentsLoading) + assertTrue(repository.state.value.isRefreshing) + providersGate.complete(Unit) + advanceUntilIdle() + } + @Test fun `loads selected runtime catalog and workspaces`() = runTest { @@ -498,6 +579,7 @@ class RuntimeCatalogRepositoryTest { /** What [listProviders] returns, when a test needs to change the catalogue mid-run. */ var providersOverride: ProviderCatalog? = null + var providersGate: CompletableDeferred? = null override suspend fun connect(): Result = connectError?.let(Result.Companion::failure) @@ -520,6 +602,7 @@ class RuntimeCatalogRepositoryTest { override suspend fun listMessages(sessionId: String): List = emptyList() override suspend fun listProviders(): ProviderCatalog { + providersGate?.await() providersError?.let { throw it } providersOverride?.let { return it } return ProviderCatalog(