Skip to content

Commit 980e926

Browse files
committed
feat(mobile): show a delegated subagent's work in the conversation transcript
A host keeps a spawned child agent in its own hidden session. The parent transcript only carries the tool row that launched it, and the `subagentSessionId` the host stamps on that row was read by SessionRecordReplica and then dropped, so the child's work was unreachable from the phone and a delegation card could only ever show the spawn call's own outcome. Keep the id on the mapped tool (optional, so an older host is unaffected) and follow the child from a new core-feature coordinator: SubagentBranchCoordinator opens one on-demand child stream per owner, attaches the child's own items as the branch of the item that launched it, re-reads the branch from the host's newest page on a stream gap, caps concurrent child streams at eight with oldest-first eviction, and exposes EnsureSubagentAttached so opening a card asks again for a stream that the cap or a failure released. Child steps are never stored on the device: toPersisted() strips them (withoutChildSteps) and the child is re-read on demand. A child stream failure is reported per card (Ready.subagentFailures, keyed by owner tool call id) rather than replacing a readable conversation with a session-level error: the session stays Ready and the launching item keeps its own status. The card's liveness/status presentation replaces the old boolean subagentFailed(), so a worker that is queued, finishing or calling a tool is no longer printed as done, and a status this client cannot read is drawn as unknown instead of guessed as completed. Compatibility: an older desktop sends no subagent_session_id, so openBranch() returns before any stream is opened, decorate() hands the transcript back unchanged and the launch item keeps the plain tool row it has always had -- the transcript and the number of streams are unchanged, though a running task row without a branch is still an expandable card, now expanding to nothing. iOS shares this behaviour rather than being unaffected: core-feature and RemoteSessionStore are shared, so it opens child streams and draws the worker's steps and lifecycle too. It does not get the two Android affordances, because EnsureSubagentAttached is dispatched only from ConversationView.kt and subagentFailures is read only there, so on iOS a cap-released branch keeps its prefix and a child failure is recorded but not shown. That is an accepted limitation with a named follow-up: wire the same intent and failure map into the Swift client. Compile safety holds everywhere else -- iOS only reads RemoteSessionUiState.Ready and never constructs it, a secondary constructor preserves the previous positional initializer, RemoteToolStatusResponse's new field is optional, and HarmonyOS models the relay payload with its own TypeScript interface. Testing level: AI-assisted implementation, unit and compile only. The shared JVM suites (:core-domain:jvmTest, :core-feature:jvmTest) pass - 125 and 556 tests, including 26 in SubagentBranchCoordinatorTest, which also covers both ways a card stops reporting its worker (a real re-read, and steps arriving). :app:testDebugUnitTest still shows exactly the one failure that official main already has (ProcessPresentationTest.completedWorkSummarizesWithoutHidingLiveOrFailedTools, expected:<3> but was:<1>), which this change neither causes nor fixes. The obsolete ProcessPresentationTest.statusPreservesFailuresInsteadOfCallingThemDone is removed with subagentFailed(), and its coverage is replaced by SubagentLivenessTest. ChatMessageBubbleTest's fixtures now ask for reduced motion, because a running worker's state mark pulses forever and three of those pre-existing fixtures draw one. The three instrumented files compile but were not executed (no device or emulator available), and nothing here was run against a live desktop host. Co-authored-by: Driss <braindeadz@users.noreply.github.com>
1 parent ca7a55d commit 980e926

22 files changed

Lines changed: 3391 additions & 45 deletions

File tree

‎src/apps/mobile/AGENTS.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ isolation, run from `shared/` with the Android guide's Java runtime:
119119
`./gradlew :core-feature:jvmTest --tests '*promptSuggestion*' --tests '*SessionRecordReplicaTest*'`.
120120
These are simulated host streams, not evidence of a live remote connection.
121121

122+
For a delegation tool's spawned child — child session id retained on the launch
123+
item, one stream per owner, steps attached to that owner's card, child gap
124+
re-read, child failure before content, a child failure that keeps the session
125+
readable instead of failing it, a late child error from a closed session,
126+
unsupported host, cap and re-attach, session switch — run from `shared/` with the
127+
Android guide's Java runtime:
128+
`./gradlew :core-feature:jvmTest --tests '*SubagentBranchCoordinatorTest*'`.
129+
A child transcript is read on demand and never stored on this device.
130+
122131
Native mobile surfaces are controllers. Desktop and CLI own execution, files, terminals and SSH credentials. Mobile workspace selection may use only connections already saved on the controlled runtime; it must not create an SSH connection or instantiate another runtime. Carry the selected connection identity through every operation, and report missing identity instead of falling back to local files.
123132

124133
## Offline Mini Apps

‎src/apps/mobile/android/app/src/androidTest/kotlin/com/openbitfun/mobile/app/ChatMessageBubbleTest.kt‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.ui.test.onNodeWithText
1414
import androidx.compose.ui.test.performClick
1515
import androidx.test.platform.app.InstrumentationRegistry
1616
import com.openbitfun.mobile.app.ui.chat.ChatMessageBubble
17+
import com.openbitfun.mobile.app.ui.chat.message.LocalReduceMotionOverride
1718
import com.openbitfun.mobile.app.ui.chat.message.SUBAGENT_GROUP_TEST_TAG
1819
import com.openbitfun.mobile.app.ui.chat.message.TYPING_DOTS_TEST_TAG
1920
import com.openbitfun.mobile.core.feature.session.ConversationRow
@@ -255,22 +256,35 @@ class ChatMessageBubbleTest {
255256
composeRule.onNodeWithText("Working on it").assertIsDisplayed()
256257
}
257258

259+
/**
260+
* One turn, drawn with animations removed.
261+
*
262+
* Three fixtures here render a running delegation card, and a running worker's
263+
* state mark pulses forever: a compose test cannot idle on a clock that never
264+
* stops, so without this the pre-existing assertions in this file would wait for
265+
* an idle state that never arrives. The override changes only the movement — the
266+
* reading printed on the card is the same — and
267+
* `SubagentLivenessCardTest.aRunningWorkerMovesAndOnlyItsMarkMoves` is the test
268+
* that measures the pulse, on a fixture that asks for it explicitly.
269+
*/
258270
@androidx.compose.runtime.Composable
259271
private fun Bubble(row: ConversationRow) {
260-
ChatMessageBubble(
261-
row = row,
262-
enabled = true,
263-
onApproveTool = { _, _ -> },
264-
onRejectTool = { _, _ -> },
265-
onCancelTool = { _, _ -> },
266-
onAnswerTool = { _, _ -> },
267-
onAnswerToolStructured = { _, _ -> },
268-
onRetry = {},
269-
onOpenLink = { _, _ -> },
270-
previewingRemotePath = "",
271-
previewLoading = false,
272-
modifier = Modifier,
273-
)
272+
androidx.compose.runtime.CompositionLocalProvider(LocalReduceMotionOverride provides true) {
273+
ChatMessageBubble(
274+
row = row,
275+
enabled = true,
276+
onApproveTool = { _, _ -> },
277+
onRejectTool = { _, _ -> },
278+
onCancelTool = { _, _ -> },
279+
onAnswerTool = { _, _ -> },
280+
onAnswerToolStructured = { _, _ -> },
281+
onRetry = {},
282+
onOpenLink = { _, _ -> },
283+
previewingRemotePath = "",
284+
previewLoading = false,
285+
modifier = Modifier,
286+
)
287+
}
274288
}
275289

276290
private fun row(
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package com.openbitfun.mobile.app
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Surface
8+
import androidx.compose.runtime.CompositionLocalProvider
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.test.assertCountEquals
11+
import androidx.compose.ui.test.assertIsDisplayed
12+
import androidx.compose.ui.test.hasAnyAncestor
13+
import androidx.compose.ui.test.hasTestTag
14+
import androidx.compose.ui.test.hasText
15+
import androidx.compose.ui.test.junit4.v2.createComposeRule
16+
import androidx.compose.ui.test.onNodeWithTag
17+
import androidx.compose.ui.test.performClick
18+
import androidx.compose.ui.unit.dp
19+
import androidx.test.platform.app.InstrumentationRegistry
20+
import com.openbitfun.mobile.app.ui.chat.message.LocalReduceMotionOverride
21+
import com.openbitfun.mobile.app.ui.chat.message.LocalSubagentAttach
22+
import com.openbitfun.mobile.app.ui.chat.message.LocalSubagentFailures
23+
import com.openbitfun.mobile.app.ui.chat.message.MessageBlockCallbacks
24+
import com.openbitfun.mobile.app.ui.chat.message.MessageBlockList
25+
import com.openbitfun.mobile.app.ui.chat.message.SUBAGENT_GROUP_TEST_TAG
26+
import com.openbitfun.mobile.app.ui.theme.OpenBitFunTheme
27+
import com.openbitfun.mobile.core.feature.session.MessageBlock
28+
import com.openbitfun.mobile.core.feature.session.RemoteSessionFailureReason
29+
import com.openbitfun.mobile.core.feature.workspace.RemoteFileDownloadUiState
30+
import org.junit.Assert.assertEquals
31+
import org.junit.Rule
32+
import org.junit.Test
33+
34+
private const val OWNER = "tool-owner"
35+
private const val TITLE = "Audit the parser"
36+
private const val STEP_ONE = "Read the parser module"
37+
private const val STEP_TWO = "Run the parser tests"
38+
39+
/**
40+
* The delegation card as it is painted on a device.
41+
*
42+
* What this proves is the drawing path: a branch block — the shape the shared
43+
* presentation produces for a launch item the coordinator decorated — goes through
44+
* the real `MessageBlockList` -> `SubagentGroup` chain, so the reader really does see
45+
* the worker's steps behind their own expansion, the worker's own lifecycle reading,
46+
* the two "no steps" readings with their own copy, and the header's request to follow
47+
* the worker again reaching the surface that asked for it.
48+
*
49+
* What it does not prove is where those steps came from. The child stream, the cap,
50+
* the gap reset and the unsupported-host discovery are covered by the JVM tests in
51+
* `core-feature` (`SubagentBranchCoordinatorTest`), because none of them is a drawing
52+
* fact and all of them need a host to read from. No transport, no relay, no account
53+
* and no paired session is involved here — the fixture is the coordinator's own
54+
* output shape.
55+
*
56+
* Why the block list and not the whole conversation view: `ConversationView` takes a
57+
* `RemoteSessionUiState.Ready`, and building one from an invented transcript would
58+
* mean this test naming `core-domain` and `core-protocol` types, which the app module
59+
* is not allowed to reach (`scripts/check-mobile-architecture.mjs`, rule 3). The two
60+
* composition locals the card reads are therefore supplied here directly, exactly as
61+
* `ConversationView` supplies them from the state it is handed; that wiring is
62+
* compile-checked, and the store-side half of the same path is asserted in
63+
* `SubagentBranchCoordinatorTest`.
64+
*
65+
* The launch item's status in each fixture is the reading the card is handed, not the
66+
* spawn call's own answer: overriding the spawn's `completed` with the worker's
67+
* `running` is the coordinator's job and is asserted there. What is asserted here is
68+
* that the header prints the status it is given, that the steps are behind the
69+
* reader's own expansion, and that a card with no readable worker says so in the
70+
* reader's language instead of showing an invented step.
71+
*/
72+
class SubagentBranchCardTest {
73+
@get:Rule
74+
val composeRule = createComposeRule()
75+
76+
@Test
77+
fun aDelegationCardShowsTheWorkersStepsWhileTheChildIsStillRunning() {
78+
val attached = mutableListOf<String>()
79+
setCard(
80+
block = runningCard(children = listOf(step(STEP_ONE), step(STEP_TWO))),
81+
onAttach = { attached += it },
82+
// The only fixture here whose worker is in flight, so the only one that
83+
// pulses: pinned still so the card can be driven to idle.
84+
reduceMotion = true,
85+
)
86+
87+
composeRule.onNodeWithTag(SUBAGENT_GROUP_TEST_TAG).assertIsDisplayed()
88+
assertCardShows(R.string.tool_phase_running)
89+
assertCardHides(R.string.tool_phase_completed)
90+
91+
// Collapsed by default: the worker's steps are one tap away, not on screen.
92+
assertCardHidesText(STEP_ONE)
93+
94+
openCard()
95+
96+
assertCardShowsText(STEP_ONE)
97+
assertCardShowsText(STEP_TWO)
98+
// Opening the card is the gesture that asks the store to follow the worker
99+
// again, and it names the launch item's own tool call — the key the store
100+
// follows that child by.
101+
assertEquals(listOf(OWNER), attached)
102+
}
103+
104+
@Test
105+
fun aSettledChildKeepsItsStepsAndShowsTheSettledReading() {
106+
setCard(block = runningCard(status = "completed", children = listOf(step(STEP_ONE), step(STEP_TWO))))
107+
108+
composeRule.onNodeWithTag(SUBAGENT_GROUP_TEST_TAG).assertIsDisplayed()
109+
assertCardShows(R.string.tool_phase_completed)
110+
assertCardHides(R.string.tool_phase_running)
111+
112+
openCard()
113+
114+
// A worker that reported is not a card that empties: its steps stay.
115+
assertCardShowsText(STEP_ONE)
116+
assertCardShowsText(STEP_TWO)
117+
}
118+
119+
@Test
120+
fun aDesktopWithoutOnDemandStreamsSaysSoAndInventsNoSteps() {
121+
setCard(
122+
block = runningCard(status = "completed", children = emptyList()),
123+
failures = mapOf(OWNER to RemoteSessionFailureReason.HOST_STREAM_UNSUPPORTED),
124+
)
125+
126+
composeRule.onNodeWithTag(SUBAGENT_GROUP_TEST_TAG).assertIsDisplayed()
127+
// Nothing was read about the worker, so the launch item's own status stands.
128+
assertCardShows(R.string.tool_phase_completed)
129+
130+
openCard()
131+
132+
assertCardShows(R.string.subagent_steps_unsupported)
133+
// The two "no steps" readings are not the same news, and this one is not a
134+
// stream that merely failed.
135+
assertCardHides(R.string.subagent_steps_unavailable)
136+
// No step was made up to fill the card.
137+
assertCardHides(R.string.chat_subagent_output)
138+
assertCardHidesText(STEP_ONE)
139+
}
140+
141+
@Test
142+
fun aChildStreamThatFailedBeforeAnyContentKeepsTheLaunchStatusAndSaysStepsAreUnavailable() {
143+
setCard(
144+
block = runningCard(status = "completed", children = emptyList()),
145+
failures = mapOf(OWNER to RemoteSessionFailureReason.TRANSPORT),
146+
)
147+
148+
composeRule.onNodeWithTag(SUBAGENT_GROUP_TEST_TAG).assertIsDisplayed()
149+
assertCardShows(R.string.tool_phase_completed)
150+
assertCardHides(R.string.tool_phase_running)
151+
152+
openCard()
153+
154+
assertCardShows(R.string.subagent_steps_unavailable)
155+
assertCardHides(R.string.subagent_steps_unsupported)
156+
assertCardHides(R.string.chat_subagent_output)
157+
assertCardHidesText(STEP_ONE)
158+
}
159+
160+
// --- assertions -----------------------------------------------------------
161+
162+
/**
163+
* Everything below is read *inside* the card.
164+
*
165+
* Several status words are shared with other surfaces ("Done", "Running"), so a
166+
* whole-screen search would be asking a different question than the one these
167+
* tests answer — and would break the day a "Done" button lands somewhere else.
168+
*/
169+
private fun inCard(text: String) =
170+
hasText(text) and hasAnyAncestor(hasTestTag(SUBAGENT_GROUP_TEST_TAG))
171+
172+
private fun assertCardShows(resource: Int) {
173+
composeRule.onNode(inCard(string(resource))).assertIsDisplayed()
174+
}
175+
176+
private fun assertCardHides(resource: Int) {
177+
composeRule.onAllNodes(inCard(string(resource))).assertCountEquals(0)
178+
}
179+
180+
private fun assertCardShowsText(text: String) {
181+
composeRule.onNode(inCard(text)).assertIsDisplayed()
182+
}
183+
184+
private fun assertCardHidesText(text: String) {
185+
composeRule.onAllNodes(inCard(text)).assertCountEquals(0)
186+
}
187+
188+
/** The reader's own gesture: the card header is the only thing that opens it. */
189+
private fun openCard() {
190+
composeRule.onNode(inCard(TITLE)).performClick()
191+
composeRule.waitForIdle()
192+
}
193+
194+
// --- fixtures -------------------------------------------------------------
195+
196+
private fun setCard(
197+
block: MessageBlock.Subagent,
198+
failures: Map<String, RemoteSessionFailureReason> = emptyMap(),
199+
onAttach: (String) -> Unit = {},
200+
/**
201+
* The card's own reading is unchanged by this; only its pulse is.
202+
*
203+
* A running worker pulses forever, which is what a reader sees and what
204+
* [SubagentLivenessCardTest] measures in pixels. Here the subject is the
205+
* drawing path, not the movement, so the one fixture whose worker is still
206+
* in flight pins the reading still: a compose test cannot idle on a clock
207+
* that never stops.
208+
*/
209+
reduceMotion: Boolean = false,
210+
) {
211+
composeRule.setContent {
212+
OpenBitFunTheme(dark = false) {
213+
CompositionLocalProvider(
214+
LocalReduceMotionOverride provides reduceMotion,
215+
LocalSubagentAttach provides onAttach,
216+
LocalSubagentFailures provides failures,
217+
) {
218+
Surface(
219+
modifier = Modifier.fillMaxSize(),
220+
color = MaterialTheme.colorScheme.background,
221+
contentColor = MaterialTheme.colorScheme.onBackground,
222+
) {
223+
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
224+
MessageBlockList(
225+
blocks = listOf(block),
226+
callbacks = callbacks(),
227+
modifier = Modifier,
228+
)
229+
}
230+
}
231+
}
232+
}
233+
}
234+
}
235+
236+
/**
237+
* The card a decorated launch item draws.
238+
*
239+
* [status] is what the header prints; the owner tool call is the key the store
240+
* follows that child by, and it is what opening the card names. [children] is
241+
* what the decoration attaches: the worker's own process, in the worker's own
242+
* order.
243+
*/
244+
private fun runningCard(
245+
status: String = "running",
246+
children: List<MessageBlock>,
247+
) = MessageBlock.Subagent(
248+
id = "message-owner-subagent-0",
249+
title = TITLE,
250+
running = status in setOf("running", "tool_calling"),
251+
text = "",
252+
children = children,
253+
status = status,
254+
ownerToolCallId = OWNER,
255+
)
256+
257+
private fun step(content: String) = MessageBlock.Text("$OWNER-$content", content, false)
258+
259+
private fun callbacks() = MessageBlockCallbacks(
260+
enabled = true,
261+
onApproveTool = { _, _ -> },
262+
onRejectTool = { _, _ -> },
263+
onCancelTool = { _, _ -> },
264+
onAnswerTool = { _, _ -> },
265+
onAnswerToolStructured = { _, _ -> },
266+
onOpenLink = { _, _ -> },
267+
previewingRemotePath = "",
268+
previewLoading = false,
269+
download = RemoteFileDownloadUiState.None,
270+
onDownloadFile = { _, _ -> },
271+
downloadEnabled = true,
272+
)
273+
274+
private fun string(resource: Int): String =
275+
InstrumentationRegistry.getInstrumentation().targetContext.getString(resource)
276+
}

0 commit comments

Comments
 (0)