feat(agent): an Agent tab, a session drawer page, and a bubble over the game (#2376)

* feat(agent): an Agent tab in the launcher that talks to Claude Code outside any session

* feat(agent): the agent in the session drawer and as a bubble over the game

* fix(agent): connect loopback adb, show unread replies, and fit the drawer page to the screen

* feat(agent): first-run setup in the Agent tab, a harness choice with Codex, and no permission prompts

* fix(agent): install helpers off the main thread and offer sign-in only after the install

* fix(agent): connect adb only for commands that use it and run setup checks one at a time

* fix(agent): show why a setup check failed

* fix(agent): each harness has its own setup state and conversation

* fix(agent): start the adb server before connecting and retry the connection check

* fix(agent): switching harness clears the previous harness's setup results

* feat(agent): a header, a composer with send and stop, and Markdown replies in the agent chat

* fix(agent): leave room under the chat composer

* feat(agent): continue the conversation after a Lab build restarts the app

* feat(agent): show and remove what the agent stores in the Linux userland

* fix(agent): the build tools take about 5 GB

* feat(agent): DroidDeck opens on the Agent tab after restarting on a Lab build

* feat(agent): conversation history, model and plan mode, live replies, full tool input and output, processes, attachments, queued messages and a Stop that ends the turn

* fix(agent): check the Lab build less often while nothing is happening

* fix(agent): an old Lab build's progress is not shown as a failure, and a restart counts as success

* feat(agent): rebuild a kept Lab build from Updates, and tidy the chat's tool rows, costs, processes and times

* feat(agent): a setup step that prepares fast Lab builds for the installed version, kept alive while it runs

* fix(agent): the notification names a prepare and shows its percent without repeating the title

* fix(agent): run the agent's Lab builds in the app's own process and continue the conversation that asked after the restart

* fix(agent): setup's prepare runs the build itself instead of handing it to the app

* feat(agent): a turn that ends without a reply says so and offers Retry, the resume is retried once, and each turn's raw output is kept

* fix(agent): keep the previous turn's raw output too

* fix(agent): a resumed Claude Code turn no longer ends at the result for a background task the last run left behind

* fix(agent): install Codex's code-mode host with it, without which Codex runs no commands

* feat(agent): setup and storage collapse into a summary card like Setup's, and Codex tool rows show the command without its shell wrapper

* feat(agent): drop the agent bubble over sessions for now; the drawer keeps the agent

* fix(agent): the Agent tab shows once the Linux runtime is downloaded, even while its proot pack is still being fetched

* fix(agent): before the install brings adb, the connection step checks the app's own pairing instead of failing with adb not found

* fix(agent): proot's binding warnings are not shown as a setup step's reason

* fix(agent): the session drawer's Agent page fills the height under the drawer's header instead of a guess from the screen height

* fix(agent): write the browser and GitHub helpers before every setup step, not only the install

* fix(agent): find Wireless debugging's current port when the saved one no longer answers, so sign-ins open the browser after it restarts

* fix(agent): only take a Wireless debugging port that answers, since mDNS can still advertise an old one

* fix(agent): find a loopback Wireless debugging port by scanning when mDNS advertises none

* fix(agent): keep a Codex turn going through reconnect notices and read gh login's status reliably

* fix(agent): time out hung setup checks and keep their results and actions with their harness

* fix(agent): keep the draft across settings and show the Agent tab once the runtime installs

* fix(agent): keep the backslash when quoting apostrophes in a URL the browser helper opens

* fix(agent): refresh apt's package lists before installing, since a fresh Linux runtime ships without them

* fix(agent): sign-in links open through the app itself, so they work without Wireless debugging

* fix(agent): the connection step passes only when adb really reaches the device, not on adb's own error

* fix(agent): say Wireless debugging isn't reachable instead of adb's error

* fix(agent): keep prompts, tool ids, attachments and retries with their own turn and chat

* fix(agent): the service stops only once foreground, and Rebuild waits for a ready agent

* fix(agent): the Codex install says what it is downloading, so a long download doesn't look stuck

* fix(agent): pass the prompt to both CLIs the way they were tested on devices

* fix(agent): the browser helper uses the running app's files directory, falls back to adb when the app cannot open a link, and the link is not logged
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-11 14:10:02 -05:00
1 parent 61031e3c4b
commit db933e4232
33 files changed
+3210 -13

No files matched your search

@@ -0,0 +1,190 @@
package com.droiddeck.launcher.agent
import com.droiddeck.launcher.agent.AgentHost.Entry
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.io.File
import java.nio.file.Files
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class AgentHostTest {
@Test fun claudeInitCarriesOnlyTheConversation() {
val event = ClaudeCode.parse("""{"type":"system","subtype":"init","session_id":"abc","tools":[]}""")!!
assertEquals("abc", event.conversation)
assertEquals(emptyList<Entry>(), event.entries)
}
@Test fun claudeTextThinkingAndToolUseBecomeEntries() {
val event = ClaudeCode.parse(
"""{"type":"assistant","session_id":"abc","message":{"content":[
{"type":"text","text":" Looking at the log. "},
{"type":"tool_use","id":"t1","name":"Bash","input":{"command":"droiddeckctl state\nmore"}},
{"type":"thinking","thinking":"why"}],"usage":{"input_tokens":10,"cache_read_input_tokens":90,"output_tokens":5}}}""",
)!!
assertEquals(listOf(Entry.Said("Looking at the log."), Entry.Used("t1", "Bash", "droiddeckctl state", "droiddeckctl state\nmore"), Entry.Thought("why")), event.entries)
assertEquals(105L, event.context)
}
@Test fun claudeStreamsTextAsItIsWritten() {
val event = ClaudeCode.parse("""{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hel"}}}""")!!
assertEquals("Hel", event.text)
assertEquals("hm", ClaudeCode.parse("""{"type":"stream_event","event":{"type":"content_block_delta","delta":{"type":"thinking_delta","thinking":"hm"}}}""")!!.thinking)
assertNull(ClaudeCode.parse("""{"type":"stream_event","event":{"type":"message_start"}}"""))
}
@Test fun claudeToolResultsAnswerTheirCall() {
val event = ClaudeCode.parse(
"""{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"t1","content":[{"type":"text","text":"ok"}],"is_error":true}]}}""",
)!!
assertEquals(listOf(Entry.Result("t1", "ok", true)), event.entries)
val entries = mutableListOf<Entry>(Entry.Used("t1", "Bash", "ls", "ls"))
assertTrue(AgentChats.merge(entries, event.entries.single()))
assertEquals(Entry.Used("t1", "Bash", "ls", "ls", "ok", true), entries.single())
assertFalse(AgentChats.merge(entries, Entry.Result("unknown", "x", false)))
}
@Test fun aClaudeResultEndsTheTurnWithItsUsage() {
val event = ClaudeCode.parse("""{"type":"result","subtype":"success","is_error":false,"result":"done","duration_ms":4200,"total_cost_usd":0.05,"usage":{"input_tokens":3,"output_tokens":7}}""")!!
assertEquals(listOf(Entry.Finished(null)), event.entries)
assertEquals(AgentHost.Usage(4200, 0.05, 3, 7), event.usage)
assertEquals(listOf(Entry.Finished("Invalid API key")),
ClaudeCode.parse("""{"type":"result","subtype":"success","is_error":true,"result":"Invalid API key"}""")!!.entries)
assertNull(ClaudeCode.parse("not json"))
}
@Test fun aResultForALeftoverBackgroundTaskDoesNotEndTheTurn() {
val event = ClaudeCode.parse("""{"type":"result","is_error":false,"num_turns":0,"session_id":"s","total_cost_usd":0,"usage":{"input_tokens":0,"output_tokens":0},"origin":{"kind":"task-notification"}}""")
assertTrue(event == null || event.entries.none { it is Entry.Finished })
}
@Test fun claudeTurnsBypassPermissionsOrPlanAndTakeAModel() {
val first = ClaudeCode.turn("hi", null, AgentHarness.Options())
assertTrue("bypassPermissions" in first && "--resume" !in first && "--model" !in first && "--include-partial-messages" in first)
val next = ClaudeCode.turn("hi", "abc", AgentHarness.Options(model = "opus", plan = true))
assertEquals("abc", next[next.indexOf("--resume") + 1])
assertEquals("opus", next[next.indexOf("--model") + 1])
assertEquals("plan", next[next.indexOf("--permission-mode") + 1])
}
@Test fun claudeShowsWholeEditsAndTodos() {
assertEquals("a.kt\n\n- old\n\n+ new", ClaudeCode.fullInput("Edit", org.json.JSONObject("""{"file_path":"a.kt","old_string":"old","new_string":"new"}""")))
assertEquals("[x] read\n[~] fix\n[ ] test", ClaudeCode.fullInput("TodoWrite", org.json.JSONObject(
"""{"todos":[{"content":"read","status":"completed"},{"content":"fix","status":"in_progress"},{"content":"test","status":"pending"}]}""")))
}
@Test fun codexEventsBecomeEntries() {
assertEquals("t1", Codex.parse("""{"type":"thread.started","thread_id":"t1"}""")!!.conversation)
assertEquals(listOf(Entry.Used("i1", "Shell", "uname -m", "uname -m")),
Codex.parse("""{"type":"item.started","item":{"id":"i1","type":"command_execution","command":"uname -m","status":"in_progress"}}""")!!.entries)
assertEquals(listOf(Entry.Result("i1", "aarch64\n", false)),
Codex.parse("""{"type":"item.completed","item":{"id":"i1","type":"command_execution","command":"uname -m","aggregated_output":"aarch64\n","exit_code":0}}""")!!.entries)
assertEquals(listOf(Entry.Said("It is aarch64.")),
Codex.parse("""{"type":"item.completed","item":{"id":"i2","type":"agent_message","text":"It is aarch64."}}""")!!.entries)
assertEquals(Entry.Used("i3", "Edit", "a.kt, b.kt", "a.kt\nb.kt"),
Codex.parse("""{"type":"item.completed","item":{"id":"i3","type":"file_change","status":"completed","changes":[{"path":"a.kt"},{"path":"b.kt"}]}}""")!!.entries.first())
assertEquals(listOf(Entry.Finished(null)), Codex.parse("""{"type":"turn.completed","usage":{}}""")!!.entries)
assertEquals(listOf(Entry.Finished("quota")), Codex.parse("""{"type":"turn.failed","error":{"message":"quota"}}""")!!.entries)
assertNull(Codex.parse("""{"type":"error","message":"Reconnecting... 1/5 (stream disconnected before completion)"}"""))
assertEquals(listOf(Entry.Finished("unauthorized")), Codex.parse("""{"type":"error","message":"unauthorized"}""")!!.entries)
assertNull(Codex.parse("""{"type":"item.started","item":{"type":"reasoning"}}"""))
}
@Test fun codexTurnsSkipItsSandboxResumeAndTakeAModel() {
assertEquals(listOf("/root/.local/bin/codex", "exec", "--json", "--dangerously-bypass-approvals-and-sandbox", "--skip-git-repo-check", "hi"),
Codex.turn("hi", null, AgentHarness.Options()))
assertEquals(listOf("/root/.local/bin/codex", "exec", "resume", "t1", "--json", "--dangerously-bypass-approvals-and-sandbox", "--skip-git-repo-check", "-m", "gpt", "hi"),
Codex.turn("hi", "t1", AgentHarness.Options(model = "gpt")))
}
@Test fun aToolIdUsedAgainInALaterTurnIsANewCall() {
val entries = mutableListOf<Entry>(Entry.Asked("one"), Entry.Used("item_0", "Shell", "ls", "ls", "a"), Entry.Finished(null), Entry.Asked("two"))
assertFalse(AgentChats.merge(entries, Entry.Result("item_0", "late", false)))
assertTrue(AgentChats.merge(entries, Entry.Used("item_0", "Shell", "pwd", "pwd")))
assertTrue(AgentChats.merge(entries, Entry.Result("item_0", "/root", false)))
assertEquals(Entry.Used("item_0", "Shell", "ls", "ls", "a"), entries[1])
assertEquals(Entry.Used("item_0", "Shell", "pwd", "pwd", "/root"), entries.last())
}
@Test fun filesAttachedBeforeTheFirstMessageMoveIntoTheChat() {
val context = org.robolectric.RuntimeEnvironment.getApplication()
val dir = AgentAttachments.hostDir(context)
File(dir, "new").mkdirs()
File(dir, "new/a.png").writeText("x")
val moved = AgentAttachments.adopt(context, "42", listOf("/root/.cache/droiddeck/attachments/new/a.png", "/root/.cache/droiddeck/attachments/7/b.txt"))
assertEquals(listOf("/root/.cache/droiddeck/attachments/42/a.png", "/root/.cache/droiddeck/attachments/7/b.txt"), moved)
assertTrue(File(dir, "42/a.png").isFile)
assertFalse(File(dir, "new/a.png").exists())
AgentChats.delete(context, "42")
assertFalse(File(dir, "42").exists())
}
@Test fun codexShellWrappersAreUnwrapped() {
assertEquals("uname -m", shellCommand("/bin/bash -lc 'uname -m'"))
assertEquals("echo 'hi'", shellCommand("/bin/bash -lc 'echo '\''hi'\'''"))
assertEquals("ls", shellCommand("ls"))
}
@Test fun aTurnWithoutAnEndWasInterrupted() {
assertTrue(AgentHost.interrupted(listOf(Entry.Asked("hi"), Entry.Used("1", "Bash", "dd-lab-build"))))
assertEquals(false, AgentHost.interrupted(listOf(Entry.Asked("hi"), Entry.Finished(null))))
}
@Test fun aChatRoundTripsThroughItsFile() {
val file = Files.createTempFile("chat", ".jsonl").toFile()
AgentChats.start(file, "claude")
AgentChats.appendConversation(file, "abc")
val written = listOf(
Entry.Asked("hi", listOf("/root/x.png")), Entry.Said("hello"), Entry.Thought("hm"), Entry.Used("t1", "Read", "/root/x", "{}"),
Entry.Result("t1", "contents", false), Entry.Finished(null, usage = AgentHost.Usage(1, 0.5, 2, 3)), Entry.Finished("boom", stopped = true),
)
written.forEach { AgentChats.append(file, it) }
file.appendText("broken line\n")
val loaded = AgentChats.read(file)
assertEquals("claude", loaded.harness)
assertEquals("abc", loaded.conversation)
assertEquals(listOf(
Entry.Asked("hi", listOf("/root/x.png")), Entry.Said("hello"), Entry.Thought("hm"), Entry.Used("t1", "Read", "/root/x", "{}", "contents", false),
Entry.Finished(null, usage = AgentHost.Usage(1, 0.5, 2, 3)), Entry.Finished("boom", stopped = true),
), loaded.entries)
assertEquals(emptyList<Entry>(), AgentChats.read(File(file.path + ".missing")).entries)
}
@Test fun aTurnThatEndsWithNothingIsAnswerless() {
assertTrue(AgentHost.answerless(listOf(Entry.Said("x"), Entry.Asked("go on"), Entry.Finished(null))))
assertFalse(AgentHost.answerless(listOf(Entry.Asked("go on"), Entry.Said("ok"), Entry.Finished(null))))
assertFalse(AgentHost.answerless(listOf(Entry.Asked("go on"), Entry.Finished("boom"))))
assertFalse(AgentHost.answerless(listOf(Entry.Asked("go on"), Entry.Finished(null, stopped = true))))
}
@Test fun longOutputKeepsItsEnd() {
val clipped = AgentChats.clip("a".repeat(30_000) + "END")
assertTrue(clipped.endsWith("END") && clipped.length < 30_000)
}
@Test fun attachmentsAreListedForTheAgent() {
assertEquals("look", AgentAttachments.prompt("look", emptyList()))
assertEquals("look\n\nAttached files (read them with your tools):\n- /root/a.png", AgentAttachments.prompt("look", listOf("/root/a.png")))
}
@Test fun procStatFieldsCountFromTheEndOfTheName() {
val stat = AgentProcesses.parseStat("1234 (java (x) y) S 1200 1234 1234 0 -1 4194304 10 0 0 0 150 50 0 0 20 0 30 0 98765 1000000 2048 18446744073709551615")
assertEquals(1200, stat.ppid)
assertEquals(200L, stat.ticks)
assertEquals(98765L, stat.startTicks)
assertEquals(2048L, stat.rssPages)
}
@Test fun labProgressIsRead() {
val p = LabProgress.parse("""{"stage":"building","mode":"build","permille":420,"task":":app:compileReleaseKotlin","done":120,"total":300,"eta":95,"elapsed":30,"pid":77}""")!!
assertEquals(420, p.permille)
assertTrue(p.running && !p.prepare)
assertEquals(95, p.etaSeconds)
}
}
@@ -0,0 +1,29 @@
package com.droiddeck.launcher.agent
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.nio.file.Files
class AgentStorageTest {
@Test fun removingAFolderKeepsWhatItsLinksPointAt() {
val base = Files.createTempDirectory("agent-storage")
val outside = Files.createDirectories(base.resolve("sdcard"))
Files.write(outside.resolve("save.dat"), ByteArray(100))
val item = Files.createDirectories(base.resolve("rootfs/android-sdk"))
Files.write(item.resolve("a.jar"), ByteArray(10))
val linked = runCatching { Files.createSymbolicLink(item.resolve("saves"), outside) }.isSuccess
assumeTrue("symbolic links need privileges on this OS", linked)
assertTrue(AgentStorage.size(item) < 100)
AgentStorage.delete(item)
assertFalse(Files.exists(item))
assertTrue(Files.exists(outside.resolve("save.dat")))
}
@Test fun aMissingPathHasNoSize() {
assertEquals(0L, AgentStorage.size(Files.createTempDirectory("agent-storage").resolve("gone")))
}
}
@@ -0,0 +1,27 @@
package com.droiddeck.launcher.ui
import androidx.compose.ui.graphics.Color
import org.junit.Assert.assertEquals
import org.junit.Test
class AgentMarkdownTest {
@Test fun blocksSplitHeadingsBulletsAndCode() {
val blocks = markdownBlocks("## Done\n\n- one\n* two\n```kotlin\nval a = 1\n\nval b = 2\n```\nplain")
assertEquals(
listOf(
MdBlock.Line("Done", heading = true),
MdBlock.Line("• one", bullet = true),
MdBlock.Line("• two", bullet = true),
MdBlock.Code("val a = 1\n\nval b = 2"),
MdBlock.Line("plain"),
),
blocks,
)
}
@Test fun inlineMarkersAreDroppedAndUnclosedOnesKept() {
val text = inlineMarkdown("a **b** `c` d ** e ` f", Color.Red, Color.Black)
assertEquals("a b c d ** e ` f", text.text)
assertEquals(2, text.spanStyles.size)
}
}
@@ -26,4 +26,11 @@ class SessionDrawerPageTest {
assertEquals(SessionDrawerPage.SESSION, pages.step(SessionDrawerPage.GAMES, 1))
}
}
@Test fun theAgentPageComesBeforeSessionOnlyWhenTheAgentIsInstalled() {
val with = sessionDrawerPages(false, hasAgent = true)
assertEquals(SessionDrawerPage.AGENT, with.step(SessionDrawerPage.GAMES, 1))
assertEquals(SessionDrawerPage.SESSION, with.step(SessionDrawerPage.AGENT, 1))
assertFalse(SessionDrawerPage.AGENT in sessionDrawerPages(false, hasAgent = false))
}
}