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
20 changes: 10 additions & 10 deletions app/src/main/java/com/yugahashimoto/andcode/AndCodeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.yugahashimoto.andcode.core.storage.DeviceStorage
import com.yugahashimoto.andcode.core.storage.DeviceStorageAccess
import com.yugahashimoto.andcode.core.util.debounceFalseEdge
import com.yugahashimoto.andcode.data.connection.SecureSettingsRepository
import com.yugahashimoto.andcode.data.repository.AgentCatalogCache
import com.yugahashimoto.andcode.data.repository.AndroidRuntimeActivityMessages
import com.yugahashimoto.andcode.data.repository.AndroidRuntimeCatalogMessages
import com.yugahashimoto.andcode.data.repository.ProviderCatalogCache
Expand Down Expand Up @@ -532,20 +533,19 @@ class AndCodeApplication : Application() {
runtimeWork = runtimeWork,
messages = claudeMessages,
)
val catalogCacheJson =
Json {
ignoreUnknownKeys = true
isLenient = true
encodeDefaults = true
}
val catalogCacheDir = File(filesDir, "catalog-cache")
catalogRepository =
RuntimeCatalogRepository(
registry = runtimeRegistry,
scope = applicationScope,
providerCache =
ProviderCatalogCache(
directory = File(filesDir, "catalog-cache"),
json =
Json {
ignoreUnknownKeys = true
isLenient = true
encodeDefaults = true
},
),
providerCache = ProviderCatalogCache(catalogCacheDir, catalogCacheJson),
agentCache = AgentCatalogCache(catalogCacheDir, catalogCacheJson),
messages = AndroidRuntimeCatalogMessages(this),
)
// Learns each recent chat's pull requests from its transcript, so the drawer can badge a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.yugahashimoto.andcode.data.repository

import com.yugahashimoto.andcode.core.api.OpenCodeAgent
import com.yugahashimoto.andcode.core.api.selectablePrimaryAgents
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File

/**
* Disk cache for a runtime's agent list.
*
* A local OpenCode installs its plugins on the first `GET /agent` after every start, which can take
* over a minute on a phone. Without this the mode chip shows its spinner for that whole window even
* though the list rarely changes between restarts, and the user cannot tell which agent a new chat
* would use. The last list is shown straight away and replaced as soon as the fresh read lands.
*
* Keyed on the runtime only, not on its version: unlike the provider catalogue, an agent list stays
* meaningful across an OpenCode upgrade until the new server says otherwise, and every load refreshes
* it in the background anyway. An empty list is never stored, because that is the shape a server
* gives while it is still starting.
*/
class AgentCatalogCache(
private val directory: File,
private val json: Json,
) {
fun read(runtimeId: String): List<OpenCodeAgent>? =
runCatching { json.decodeFromString<List<OpenCodeAgent>>(file(runtimeId).readText()) }
.getOrNull()
?.takeIf { it.selectablePrimaryAgents().isNotEmpty() }

fun write(
runtimeId: String,
agents: List<OpenCodeAgent>,
) {
if (agents.selectablePrimaryAgents().isEmpty()) return
runCatching {
directory.mkdirs()
file(runtimeId).writeText(json.encodeToString(agents))
}
}

private fun file(runtimeId: String) = File(directory, "agents-${runtimeId.replace(Regex("[^A-Za-z0-9._-]"), "_")}.json")
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RuntimeCatalogRepository(
private val registry: RuntimeRegistry,
private val scope: CoroutineScope,
private val providerCache: ProviderCatalogCache? = null,
private val agentCache: AgentCatalogCache? = null,
private val messages: RuntimeCatalogMessages = RuntimeCatalogMessages,
) {
private val mutableState = MutableStateFlow(RuntimeCatalogState(runtime = registry.selected.value))
Expand All @@ -71,6 +72,14 @@ class RuntimeCatalogRepository(
val state: StateFlow<RuntimeCatalogState> = mutableState.asStateFlow()
private val refreshMutex = Mutex()

/**
* The one load-time agent read in flight and the runtime it is for, so overlapping loads do not
* each start a plugin install. Separate from [refreshMutex] on purpose: a slow `GET /agent` must
* not hold up the rest of the catalogue or the next load.
*/
private var agentRead: Pair<String, Job>? = null
private val agentReadLock = Any()

/**
* The one agent retry in flight and the runtime it is for, so repeated taps on an empty mode
* chip do not stack polls. Guarded by [agentRetryLock]: taps (main) and load (IO) both start it.
Expand Down Expand Up @@ -190,6 +199,7 @@ class RuntimeCatalogRepository(
runtime = target,
isRefreshing = true,
error = null,
agentsError = null,
agentsLoading = !current.agents.hasSelectableAgent(),
agentsInitialReadPending = true,
)
Expand All @@ -202,6 +212,20 @@ class RuntimeCatalogRepository(
mutableState.update { it.copy(runtime = target, providers = cached) }
}

// The agent list has the same wait, and a longer one: a restarted local OpenCode
// installs its plugins on the first `GET /agent`, which can take over a minute. Show the
// last list instead of a spinner for that whole window and let the read below replace
// it. Only fills an empty list, so a refresh never downgrades agents already on screen.
agentCache?.read(target.id)?.let { cached ->
mutableState.update { state ->
if (state.runtime?.id == target.id && !state.agents.hasSelectableAgent()) {
state.copy(agents = cached, agentsLoading = false)
} else {
state
}
}
}

// The local runtime is usually still starting when the app opens, so a single attempt
// fails and nothing ever retried: the catalogue stayed empty until something else asked
// for it, which is what made the model picker look slow.
Expand All @@ -212,49 +236,39 @@ class RuntimeCatalogRepository(
connection = target.connect()
}
if (connection.isFailure) {
mutableState.value =
mutableState.update { current ->
// Keep the agent list this runtime already had - cached, or from before the
// restart - so a stopped server does not empty the mode chip and force the user
// to wait for the next connect before they can pick an agent again.
val keptAgents = current.agentsFor(target)
RuntimeCatalogState(
runtime = target,
agents = keptAgents,
isRefreshing = false,
agentsLoading = false,
error = connection.exceptionOrNull().safeMessage(messages.connectionFailed),
)
}
return
}
mutableState.update {
if (it.runtime?.id == target.id) it.copy(health = connection.getOrNull()) else it
}

// Started before the other reads, and on the repository scope rather than under the
// refresh lock: a local OpenCode installing its plugins can take over a minute on its
// first `GET /agent`, and sessions, providers and workspaces must publish without it.
// [readAgents] publishes the agents the moment they arrive and retries in the background.
startAgentRead(target)

val catalog =
supervisorScope {
val sessions = async { runCatching { target.listSessions() } }
val providers = async { runCatching { target.listProviders() } }
// 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(),
providers = providers.await(),
agents = agents.await(),
workspaces = workspaces.await(),
)
}
Expand Down Expand Up @@ -283,43 +297,25 @@ class RuntimeCatalogRepository(
}
}
}
// Like the providers above: a read that failed or offered no agent to pick must not blank
// a list this runtime already showed, or a flaky `GET /agent` empties a working picker.
val loadedAgents = catalog.agents.getOrNull()
// Read and written in one update: a background agent retry can land between this load's
// fetch and its write, and taking the list from before that would put an empty one back.
// The agent list is not written here: [readAgents] owns it, so a slow or failed
// `GET /agent` can neither delay the rest of the catalogue nor blank a list on screen.
// Its last failure, if any, is folded back into the catalogue error so the existing
// "Agents: ..." line still shows while nothing selectable has arrived.
mutableState.update { current ->
val agentsToUse =
if (loadedAgents.hasSelectableAgent()) {
loadedAgents.orEmpty()
} else {
current.agentsFor(target).ifEmpty { loadedAgents.orEmpty() }
}
val agentsFound = agentsToUse.hasSelectableAgent()
// A list kept on screen - from an earlier load or a retry that already succeeded -
// carries no failure of its own, so neither the chip nor the "Agents: ..." line in
// the catalogue error reports this read's failure.
val readFailure = catalog.agentsFailure(messages)
val agentsFailure = readFailure.takeUnless { agentsFound && !loadedAgents.hasSelectableAgent() }
val errors = catalog.failures(messages).filterNot { agentsFailure == null && it == readFailure }
RuntimeCatalogState(
if (current.runtime?.id != target.id) return@update current
val agentsError = current.agentsError.takeIf { !current.agentsFor(target).hasSelectableAgent() }
val errors = (catalog.failures(messages) + listOfNotNull(agentsError)).distinct()
current.copy(
runtime = target,
health = connection.getOrNull(),
sessions = catalog.sessions.getOrDefault(emptyList()),
providers = providersToUse,
agents = agentsToUse,
workspaces = catalog.workspaces.getOrDefault(emptyList()),
isRefreshing = false,
error = errors.takeIf { it.isNotEmpty() }?.joinToString("\n"),
agentsError = agentsFailure,
// Still loading when nothing selectable came back: the retry below takes over.
agentsLoading = !agentsFound,
)
}
}
// Outside the lock: a local OpenCode installing its plugins can take tens of seconds per
// `GET /agent`, and the rest of the catalogue - and every other refresh - must not wait on it.
if (!mutableState.value.agentsFor(target).hasSelectableAgent()) refreshAgents()
}

/**
Expand All @@ -340,6 +336,70 @@ class RuntimeCatalogRepository(
return result
}

/**
* One line, so [withAgents] can find it again in [RuntimeCatalogState.error]: decoding errors
* carry the JSON input on a second line, which a line-by-line removal would never match.
*/
private fun agentsFailure(result: Result<List<OpenCodeAgent>>): String? =
result.exceptionOrNull()?.let {
messages.agents(it.safeMessage(messages.connectionFailed).replace(Regex("\\s*\\n\\s*"), " "))
}

/**
* Starts the one load-time agent read, replacing any read left over from another runtime.
*
* A local OpenCode installs its plugins on the first `GET /agent` after a start, which can take
* over a minute. That read runs outside the refresh lock so sessions, providers and workspaces
* publish (and the next load can start) while it is still running.
*/
private fun startAgentRead(target: RuntimeTarget) {
synchronized(agentReadLock) {
val running = agentRead
if (running != null && running.second.isActive) {
// One read per runtime at a time: a repeated warmup must not stack plugin installs.
if (running.first == target.id) return
// A read left over from the runtime the user switched away from must not make the
// new runtime wait for it.
running.second.cancel()
}
agentRead = target.id to scope.launch { readAgents(target) }
}
}

/**
* Reads the agents once and publishes them the moment they answer. A read that offers nothing
* hands over to [refreshAgents]; a list already on screen is kept and not blamed for this
* read's failure.
*/
private suspend fun readAgents(target: RuntimeTarget) {
if (registry.selected.value?.id != target.id) return
val result = runCatching { target.listAgents() }
val list = result.getOrNull()
// Keep the last good list for the next cold start, where the same read can take a
// minute again while the plugins install.
if (list.hasSelectableAgent()) agentCache?.write(target.id, list.orEmpty())
if (registry.selected.value?.id != target.id) return
val failure = agentsFailure(result)
mutableState.update { state ->
if (state.runtime?.id != target.id) {
state
} else {
val completed = state.copy(agentsInitialReadPending = false)
when {
list.hasSelectableAgent() -> completed.withAgents(list.orEmpty())
state.agents.hasSelectableAgent() -> completed
else ->
completed.copy(
agentsLoading = true,
agentsError = failure,
error = appendError(completed.error, failure),
)
}
}
}
if (!mutableState.value.agentsFor(target).hasSelectableAgent()) refreshAgents()
}

/**
* Re-reads the agent list in the background until the runtime offers one to pick, for a load
* whose `GET /agent` failed or for a mode chip tapped while empty.
Expand Down Expand Up @@ -377,6 +437,7 @@ class RuntimeCatalogRepository(
target.id to
scope.launch {
val list = loadAgents(target).getOrNull()?.takeIf { it.hasSelectableAgent() }
list?.let { agentCache?.write(target.id, it) }
if (registry.selected.value?.id != target.id) return@launch
mutableState.update {
when {
Expand All @@ -394,23 +455,12 @@ class RuntimeCatalogRepository(
private data class LoadedCatalog(
val sessions: Result<List<OpenCodeSession>>,
val providers: Result<ProviderCatalog>,
val agents: Result<List<OpenCodeAgent>>,
val workspaces: Result<List<WorkspaceRef>>,
) {
/**
* One line, so [withAgents] can find it again in [RuntimeCatalogState.error]: decoding errors
* carry the JSON input on a second line, which a line-by-line removal would never match.
*/
fun agentsFailure(messages: RuntimeCatalogMessages): String? =
agents.exceptionOrNull()?.let {
messages.agents(it.safeMessage(messages.connectionFailed).replace(Regex("\\s*\\n\\s*"), " "))
}

fun failures(messages: RuntimeCatalogMessages): List<String> =
listOfNotNull(
sessions.exceptionOrNull()?.let { messages.sessions(it.safeMessage(messages.connectionFailed)) },
providers.exceptionOrNull()?.let { messages.providers(it.safeMessage(messages.connectionFailed)) },
agentsFailure(messages),
workspaces.exceptionOrNull()?.let { messages.workspaces(it.safeMessage(messages.connectionFailed)) },
)
}
Expand All @@ -425,6 +475,15 @@ class RuntimeCatalogRepository(

private fun Throwable?.safeMessage(fallback: String): String = this?.message?.takeIf { it.isNotBlank() } ?: fallback

/** [error] with [line] appended, skipping either when blank. Used to add one failure to a catalogue error. */
private fun appendError(
error: String?,
line: String?,
): String? =
listOfNotNull(error?.takeIf { it.isNotBlank() }, line?.takeIf { it.isNotBlank() })
.takeIf { it.isNotEmpty() }
?.joinToString("\n")

/**
* The providers on screen, but only while the state still belongs to [target]: another runtime's
* catalogue must never be treated as this one's, so its models cannot show under a runtime that
Expand Down
Loading
Loading