Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
}
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Unit>()
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<Unit>()
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<Unit>()
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 {
Expand Down Expand Up @@ -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<Unit>? = null

override suspend fun connect(): Result<OpenCodeHealth> =
connectError?.let(Result.Companion::failure)
Expand All @@ -520,6 +602,7 @@ class RuntimeCatalogRepositoryTest {
override suspend fun listMessages(sessionId: String): List<OpenCodeMessage> = emptyList()

override suspend fun listProviders(): ProviderCatalog {
providersGate?.await()
providersError?.let { throw it }
providersOverride?.let { return it }
return ProviderCatalog(
Expand Down
Loading