Contract at server dev 8dd3201: a subagent's conversation by its id
CI / check (push) Canceled after 0s

The server's /api/v2 has the tree routes (8dd3201): GET /chats/{c}/tree
{tree, room, live} and GET /chats/{c}/subagents/{id}/transcript {id,
state, turns}. The activity sheet reads the tree when it opens (an older
server's 404 is ignored), so the list is there without a reply streaming;
a subagent's row opens its conversation from the transcript route, by id,
followed every 3 s while it has not ended -- the provisional member-by-name
route is gone. The conversation dialog is one for both kinds now
(ConversationDialog); a 409 (an agent chat's device away) is said in the
server's words. Findings and stopping are not on Android (the reduced
display, subagents.md §12).

ContractTest reads tree.json and subagent_transcript.json.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-10 10:08:37 +00:00
1 parent 415dc653c3
commit e8d8236530
12 files changed
+237 -38

No files matched your search

@@ -104,19 +104,4 @@ object ActivityLogic {
/** A percentage as the server writes it: one decimal, none when it is whole. */
fun percent(value: Double): String = trim(String.format(Locale.ROOT, "%.1f", value))
/**
* A tree subagent as the agent route knows it: a team member, by its name (`GET
* /chats/{chat}/agents/member/{name}`), which is how the server reads a member's conversation.
*/
fun ofTreeAgent(agent: TreeAgent): ActivityAgent = ActivityAgent(
kind = "member",
id = agent.name,
name = agent.name,
role = agent.position,
brief = agent.task,
state = agent.state,
model = agent.model,
transcript = true,
)
}
@@ -247,6 +247,12 @@ class ApiClient(val base: String, private val token: String, val http: OkHttpCli
/** One background agent and its whole conversation, read-only. `kind` is `member` or `job`. */
suspend fun agent(chatId: String, kind: String, id: String): AgentDetail = get("/chats/$chatId/agents/${segment(kind)}/${segment(id)}")
/** The chat's subagent tree and room (spec 3.4.0); a 404 from a server without it. */
suspend fun tree(chatId: String): TreeView = get("/chats/$chatId/tree")
/** A subagent's conversation by its id (`s1`), read-only; 409 while an agent chat's device is away. */
suspend fun subagentTranscript(chatId: String, id: String): SubagentTranscript = get("/chats/$chatId/subagents/${segment(id)}/transcript")
/** Stop a background agent (a member leaves the team with those under it). */
suspend fun stopAgent(chatId: String, kind: String, id: String): List<String> =
send<Stopped>("POST", "/chats/$chatId/agents/${segment(kind)}/${segment(id)}/stop", null).stopped
@@ -47,6 +47,14 @@ data class TreeAgent(
val contextPercent: Int? get() = context?.takeIf { it.window > 0 }?.let { (it.used.toLong() * 100 / it.window).toInt() }
}
/** `GET /chats/{chat}/tree`: the tree (null: none), the room, and whether it is live (false: kept while the device is away). */
@Serializable
data class TreeView(val tree: SubagentTree? = null, val room: List<RoomMessage> = emptyList(), val live: Boolean = true)
/** `GET /chats/{chat}/subagents/{id}/transcript`: its conversation, read-only, live or after it ended. */
@Serializable
data class SubagentTranscript(val id: String = "", val state: String = "", val turns: List<AgentTurn> = emptyList())
@Serializable
data class TreeContext(val used: Int = 0, val window: Int = 0)
@@ -77,11 +77,13 @@ import eu.llembas.android.api.ActivityAgent
import eu.llembas.android.api.ActivityLogic
import eu.llembas.android.api.ActivitySummary
import eu.llembas.android.api.ActivityTeam
import eu.llembas.android.api.AgentDetail
import eu.llembas.android.api.AgentTurn
import eu.llembas.android.api.ApiClient
import eu.llembas.android.api.ContextUse
import eu.llembas.android.api.Output
import eu.llembas.android.api.RoomMessage
import eu.llembas.android.api.SubagentTree
import eu.llembas.android.api.TreeAgent
import eu.llembas.android.api.TreeLogic
import eu.llembas.android.lembas
import eu.llembas.android.ui.MarkdownText
@@ -195,6 +197,7 @@ private fun categoryName(key: String): String = when (key) {
interface ActivityActions {
fun stop(agent: ActivityAgent) {}
fun read(agent: ActivityAgent) {}
fun readSubagent(agent: TreeAgent) {}
fun openFile(path: String) {}
fun compact() {}
}
@@ -212,6 +215,7 @@ fun ActivityPanel(chatId: String?, state: ChatState, model: ChatViewModel, canOp
var now by remember { mutableLongStateOf(SystemClock.elapsedRealtime()) }
var stopping by remember { mutableStateOf<ActivityAgent?>(null) }
var reading by remember { mutableStateOf<ActivityAgent?>(null) }
var readingSubagent by remember { mutableStateOf<TreeAgent?>(null) }
var viewing by remember { mutableStateOf<String?>(null) }
LaunchedEffect(chatId) {
@@ -240,6 +244,10 @@ fun ActivityPanel(chatId: String?, state: ChatState, model: ChatViewModel, canOp
reading = agent
}
override fun readSubagent(agent: TreeAgent) {
readingSubagent = agent
}
override fun openFile(path: String) {
if (canOpenFiles) viewing = path
}
@@ -283,6 +291,7 @@ fun ActivityPanel(chatId: String?, state: ChatState, model: ChatViewModel, canOp
)
}
if (chatId != null) reading?.let { agent -> AgentConversation(chatId, agent, onClose = { reading = null }) }
if (chatId != null) readingSubagent?.let { agent -> SubagentConversation(chatId, agent, onClose = { readingSubagent = null }) }
viewing?.let { path -> TreeFileViewer(path, onClose = { viewing = null }) }
}
@@ -339,7 +348,7 @@ fun ActivityContent(
if (shownTree != null) {
item {
Section(stringResource(R.string.team_agents), count = shownTree.agents.count { !it.isMain }.takeIf { it > 0 }) {
SubagentList(shownTree) { agent -> actions.read(ActivityLogic.ofTreeAgent(agent)) }
SubagentList(shownTree, actions::readSubagent)
}
}
}
@@ -775,22 +784,57 @@ private fun RoomEntry(entry: RoomMessage) {
/** What a background agent was told and said: `GET /chats/{chat}/agents/{kind}/{id}`, re-read while it works. */
@Composable
private fun AgentConversation(chatId: String, agent: ActivityAgent, onClose: () -> Unit) {
val state = stateWord(agent.state)
ConversationDialog(
name = agent.name,
facts = listOf(state, agent.model),
key = agent.kind + "/" + agent.id,
onClose = onClose,
) { api ->
val detail = api.agent(chatId, agent.kind, agent.id)
Conversation(detail.transcript, detail.agent.isActive)
}
}
/** A tree subagent's conversation (spec 3.4.0 §11), read-only, followed while it runs. */
@Composable
private fun SubagentConversation(chatId: String, agent: TreeAgent, onClose: () -> Unit) {
ConversationDialog(
name = "${agent.name} (${agent.id})",
facts = listOf(treeStateWord(agent.state), agent.position, agent.model),
key = "tree/" + agent.id,
onClose = onClose,
) { api ->
val read = api.subagentTranscript(chatId, agent.id)
Conversation(read.turns, read.state !in TreeLogic.ENDED)
}
}
/** What a conversation dialog draws: the turns, and whether to read them again (it still runs). */
private class Conversation(val turns: List<AgentTurn>, val running: Boolean)
/**
* An agent's conversation, full screen and read-only: read with [load], again every 3 s while
* it runs. Nobody types into it.
*/
@Composable
private fun ConversationDialog(name: String, facts: List<String>, key: String, onClose: () -> Unit, load: suspend (ApiClient) -> Conversation) {
val context = LocalContext.current
val palette = LocalPalette.current
var detail by remember { mutableStateOf<AgentDetail?>(null) }
var detail by remember { mutableStateOf<Conversation?>(null) }
var error by remember { mutableStateOf<String?>(null) }
LaunchedEffect(agent.kind, agent.id) {
LaunchedEffect(key) {
val api = context.lembas.instances.client() ?: return@LaunchedEffect
while (true) {
try {
detail = api.agent(chatId, agent.kind, agent.id)
detail = load(api)
error = null
} catch (e: kotlinx.coroutines.CancellationException) {
throw e
} catch (e: Exception) {
error = context.explain(e)
}
if (detail?.agent?.isActive != true) break
if (detail?.running != true) break
delay(3_000)
}
}
@@ -801,20 +845,20 @@ private fun AgentConversation(chatId: String, agent: ActivityAgent, onClose: ()
navigationIcon = { IconButton(onClick = onClose) { Icon(Icons.Filled.Close, contentDescription = stringResource(R.string.close)) } },
title = {
Column {
Text(agent.name, maxLines = 1, overflow = TextOverflow.Ellipsis)
val now = detail?.agent ?: agent
Text(name, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(
listOf(stateWord(now.state), now.model).filter { it.isNotBlank() }.joinToString(" · "),
facts.filter { it.isNotBlank() }.joinToString(" · "),
style = MaterialTheme.typography.labelSmall,
color = palette.inkFaint,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
},
)
},
) { padding ->
val turns = detail?.transcript
val turns = detail?.turns
when {
turns == null && error != null -> Box(Modifier.padding(padding).fillMaxSize().padding(32.dp), contentAlignment = Alignment.Center) {
Text(error.orEmpty(), color = palette.inkMuted, textAlign = TextAlign.Center)
@@ -458,6 +458,9 @@ class ChatViewModel(app: Application) :
teamOffered = read.subagentsOffered ?: read.teamOffered,
)
}
// The tree (spec 3.4.0), for a sheet opened when no reply streams it; an older
// server answers 404 and the Tasks section stands for it.
runCatching { api().tree(chat.id) }.getOrNull()?.tree?.let { tree -> _state.update { it.copy(tree = tree) } }
} catch (e: Exception) {
fail(e)
}
@@ -118,7 +118,7 @@ private fun TreeRowItem(row: TreeRow, onOpen: (TreeAgent) -> Unit) {
/** The state's word (`loop.json` `team.read.states`), the main agent's and every subagent's. */
@Composable
private fun treeStateWord(state: String): String = when (state) {
internal fun treeStateWord(state: String): String = when (state) {
"working" -> stringResource(R.string.tree_state_working)
"waiting" -> stringResource(R.string.tree_state_waiting)
"finished" -> stringResource(R.string.tree_state_finished)