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

This commit is contained in:
Kurt Himebauch committed 2026-11-10 00:40:35 -05:00
1 parent 7836c32aac
commit 53cdf00b43
3 files changed
+27 -1

No files matched your search

@@ -10,6 +10,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.droiddeck.launcher.R
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.session.SessionPrefs
import org.json.JSONObject
@@ -66,9 +67,26 @@ object AgentHost {
if (!loaded) {
loaded = true
entries.addAll(readTranscript(transcript(context)))
resumeAfterRestart(context.applicationContext)
}
}
/**
* A Lab build restarts the app, which ends the agent's process mid-turn. dd-lab-build leaves
* [RESUME] when the app's own agent ran it, so the same conversation picks up on the new build.
*/
private fun resumeAfterRestart(app: Context) {
val marker = File(app.filesDir, RESUME)
val claimed = File(app.filesDir, "$RESUME.claimed")
if (!marker.renameTo(claimed)) return
claimed.delete()
if (!installed || !interrupted(entries)) return
send(app, app.getString(R.string.agent_resumed))
}
@VisibleForTesting
internal fun interrupted(entries: List<Entry>) = entries.isNotEmpty() && entries.last() !is Entry.Finished
/** Switches the agent CLI; a conversation belongs to one CLI, so a new one starts. */
fun choose(context: Context, choice: AgentHarness) {
if (choice == harness || state == State.WORKING) return
@@ -139,7 +157,7 @@ object AgentHost {
val env = listOf(
"/usr/bin/env", "HOME=/root", "PATH=/root/.local/bin:/usr/local/bin:/usr/bin:/bin", "LANG=C.UTF-8",
"ADB_SERIAL=${adb.serial}", "ADB_VENDOR_KEYS=${adb.vendorKeys}",
"BROWSER=/root/.local/bin/android-open", "DD_BROWSER_DISPLAY=${AgentAdb.browserDisplay}",
"DD_AGENT=1", "BROWSER=/root/.local/bin/android-open", "DD_BROWSER_DISPLAY=${AgentAdb.browserDisplay}",
)
val shell = listOf(
"/bin/sh", "-c",
@@ -188,6 +206,7 @@ object AgentHost {
private const val CONVERSATION_KEY = "session_id"
private const val HARNESS_KEY = "harness"
private const val RESUME = "agent/resume"
internal fun prefs(context: Context) = context.getSharedPreferences("agent", Context.MODE_PRIVATE)
+1
View File
@@ -1149,6 +1149,7 @@
<string name="agent_send">Send</string>
<string name="agent_new">New conversation</string>
<string name="agent_stop">Stop</string>
<string name="agent_resumed">DroidDeck restarted on your Lab build. Continue where you left off.</string>
<string name="agent_empty">Ask it why a game won\'t start, to explain a log, or to change DroidDeck and try the change on this device.</string>
<string name="agent_failed">Stopped: %1$s</string>
<string name="agent_setup_title">Set up the agent</string>
@@ -67,6 +67,12 @@ class AgentHostTest {
assertEquals(ClaudeCode, AgentHarness.byId("gone"))
}
@Test fun aTurnWithoutAnEndWasInterrupted() {
assertTrue(AgentHost.interrupted(listOf(Entry.Asked("hi"), Entry.Used("Bash", "dd-lab-build"))))
assertEquals(false, AgentHost.interrupted(listOf(Entry.Asked("hi"), Entry.Finished(null))))
assertEquals(false, AgentHost.interrupted(emptyList()))
}
@Test fun theTranscriptSurvivesARestart() {
val entries = listOf(Entry.Asked("hi"), Entry.Said("hello"), Entry.Used("Read", "/root/x"), Entry.Finished(null), Entry.Finished("stopped"))
val file = Files.createTempFile("transcript", ".jsonl").toFile()