wip: preserve background task viewer

This commit is contained in:
2026-10-18 20:32:36 +01:00
parent 5946f1abc8
commit 6521bf87e9
4 changed files with 105 additions and 32 deletions
@@ -246,12 +246,9 @@ internal fun ChatConversationPane(
)
}
}
// The background processes still running, shown whether or not a turn
// is: a shell can outlive the turn that started it, so this is a strip
// of its own rather than part of the turn indicator, and it clears the
// moment each shell exits.
if (thread.runningShells.isNotEmpty()) {
BackgroundProcessBar(thread.runningShells)
// The viewer keeps the bounded exit tail only when this client saw it.
if (thread.observedShells.isNotEmpty()) {
BackgroundProcessBar(thread.observedShells)
}
if (thread.error != null && thread.loaded) {
Text(
@@ -115,12 +115,11 @@ data class ChatThread(
*/
val lastDeltaAt: Long = 0,
/**
* The agent's shell commands still running for this session, keyed by the
* shell id the server reported and in the order they started. A background
* process can outlive the tool call (and the turn) that started it, so this is
* thread state of its own rather than part of the turn: it keeps showing after
* the turn's indicator has gone, and a shell leaves the map the moment its
* exited frame arrives. Kept on the thread so it survives a chat switch.
* Verified shell commands observed for this session, keyed by their server
* id and in observation order. Running shells come from the authoritative
* snapshot/event stream. A small exited tail is retained only after this
* client observed its exit, so a selected task can settle honestly without
* claiming historical shell discovery.
*/
val shells: Map<String, ChatShell> = emptyMap(),
/** The unsent composer text for this chat. */
@@ -201,11 +200,14 @@ data class ChatThread(
runningToolElapsedMillis(part, toolStartedAt[part.id] ?: 0L, nowMillis)
/**
* The background processes this session is still running, in start order, so
* a conversation shows what is running even when no turn is.
* The background processes currently running for this session, in start
* order, so a conversation shows what is running even when no turn is.
*/
val runningShells: List<ChatShell> get() = runningBackgroundShells(shells)
/** Bounded observed tasks, including a recently observed exit when present. */
val observedShells: List<ChatShell> get() = shells.values.toList()
/**
* What the in-flight turn is doing at [nowMillis] (see [turnPhase]): its
* running tool calls, and how long ago the last delta arrived. Pure, so the
@@ -1049,10 +1051,15 @@ class ChatStore(
val shell = frame.shell ?: return
if (shell.id.isBlank()) return
update(frame.sessionId) { thread ->
val shells = if (shell.status == ChatProtocol.SHELL_EXITED) {
thread.shells - shell.id
val prior = thread.shells[shell.id]
val merged = if (shell.status == ChatProtocol.SHELL_EXITED) {
shell.copy(command = shell.command.ifBlank { prior?.command.orEmpty() })
} else {
thread.shells + (shell.id to shell)
shell.copy(command = shell.command.ifBlank { prior?.command.orEmpty() })
}
val shells = (thread.shells + (shell.id to merged)).let { observed ->
if (observed.size <= OBSERVED_SHELL_LIMIT) observed
else observed.entries.toList().takeLast(OBSERVED_SHELL_LIMIT).associate { it.toPair() }
}
if (shells == thread.shells) thread else thread.copy(shells = shells)
}
@@ -1203,5 +1210,7 @@ class ChatStore(
companion object {
const val LOCAL_MESSAGE_PREFIX = "local-"
const val NOTICE_PREFIX = "notice-"
/** Per-session in-memory observation bound; this is not task history. */
const val OBSERVED_SHELL_LIMIT = 16
}
}
@@ -386,35 +386,48 @@ private fun RunningToolLine(part: ChatPart, elapsedMillis: Long) {
}
}
/**
* The always-visible indicator that the agent has a background process running —
* a dev server, a watching build, a sleep whose tool call has already returned.
* It is deliberately separate from the turn's spinner: a background process can
* outlive the turn, so this is shown whenever a shell is running, whether or not
* a turn is, and each line names the command the way the running-tool line names
* a call. It clears the moment the shell exits, because the exited frame removes
* it from the thread's shells. It survives a chat switch like the other live
* state, since it is read from the thread rather than held here.
*/
/** A read-only, expandable task viewer for verified shells this chat observed. */
@Composable
internal fun BackgroundProcessBar(shells: List<ChatShell>, modifier: Modifier = Modifier) {
var selectedID by rememberSaveable { mutableStateOf<String?>(null) }
val selected = shells.firstOrNull { it.id == selectedID }
Column(
verticalArrangement = Arrangement.spacedBy(SPACING_XS),
modifier = modifier.fillMaxWidth().padding(horizontal = CHAT_GUTTER, vertical = 4.dp),
) {
Text(
text = "Background tasks",
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onSurface,
)
Text(
text = "Running now and recently observed exits; not complete task history.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
shells.forEach { shell ->
key(shell.id) { BackgroundProcessLine(shell) }
key(shell.id) {
BackgroundProcessLine(shell, selectedID == shell.id) {
selectedID = if (selectedID == shell.id) null else shell.id
}
}
}
if (selected != null) {
BackgroundProcessDetail(selected)
}
}
}
/** One background process: a distinct glyph, a `Background process` label and its command. */
/** One verified shell task; selection reveals its read-only detail. */
@Composable
private fun BackgroundProcessLine(shell: ChatShell) {
private fun BackgroundProcessLine(shell: ChatShell, selected: Boolean, onSelect: () -> Unit) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP),
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.minimumInteractiveComponentSize()
.clickable(role = Role.Button, onClick = onSelect),
) {
Icon(
Icons.Default.PlayArrow,
@@ -423,7 +436,7 @@ private fun BackgroundProcessLine(shell: ChatShell) {
modifier = Modifier.size(ICON_STATUS_DP),
)
Text(
text = "Background process",
text = if (shell.status == ChatProtocol.SHELL_EXITED) "Exited" else "Running",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.tertiary,
)
@@ -435,6 +448,44 @@ private fun BackgroundProcessLine(shell: ChatShell) {
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
if (selected) {
Text(
text = "Details",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}
/** Detail intentionally exposes only state known from verified lifecycle frames. */
@Composable
private fun BackgroundProcessDetail(shell: ChatShell) {
Surface(
color = MaterialTheme.colorScheme.surfaceContainer,
shape = RoundedCornerShape(CHAT_GUTTER),
modifier = Modifier.fillMaxWidth(),
) {
SelectionContainer {
Column(
verticalArrangement = Arrangement.spacedBy(SPACING_XS),
modifier = Modifier.padding(CHAT_GUTTER),
) {
Text("Command", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
Text(shell.command.ifBlank { "Unavailable" }, style = MaterialTheme.typography.bodyMedium)
Text("Task identity", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
Text(shell.id, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace)
Text(
text = if (shell.status == ChatProtocol.SHELL_EXITED) {
"Status: exited" + (shell.exit?.let { " (exit $it)" } ?: "; exit status unavailable")
} else {
"Status: running"
},
style = MaterialTheme.typography.bodySmall,
)
Text("Output is unavailable until the server provides verified readback.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
}
}
}