diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt index 3e97d4ca67..3488feb7ff 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentHarness.kt @@ -109,7 +109,8 @@ object ClaudeCode : AgentHarness { } AgentEvent(conversation, results) } - "result" -> AgentEvent( + // A resumed session first answers background tasks the last run left behind; that result is not the turn's. + "result" -> if (o.optJSONObject("origin")?.optString("kind") == "task-notification") conversation?.let { AgentEvent(it) } else AgentEvent( conversation, listOf(AgentHost.Entry.Finished(if (o.optBoolean("is_error")) o.optString("result").ifEmpty { o.optString("subtype") } else null)), usage = AgentHost.Usage( diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt index d75e67b7ca..1b42fa0941 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentHost.kt @@ -103,7 +103,8 @@ object AgentHost { /** Counts turns, so a process that starts after its turn was stopped is not adopted by the next one. */ private var generation = 0 private var labProcess: Process? = null - private var retriedResume = false + /** The turn now running resends a message that got no reply, so it is not resent again. */ + private var retrying = false /** The running Lab build was asked for by the current turn, so stopping the turn stops it. */ private var labFromTurn = false private var conversation: String? = null @@ -374,13 +375,15 @@ object AgentHost { unseen = true failed = error != null AgentHostService.stop(app) - // A resumed turn that the CLI ended without a reply is tried once more. + // Claude Code now and then ends a resumed turn at once without reading the message; nothing + // happened, so the message is sent once more. val asked = entries.lastOrNull { it is Entry.Asked } as? Entry.Asked - if (!stopped && error == null && answerless(entries) && asked?.text == app.getString(R.string.agent_resumed) && !retriedResume) { - retriedResume = true - main.postDelayed({ if (state == State.IDLE) send(app, asked.text) }, RETRY_MS) + if (!stopped && error == null && asked != null && answerless(entries) && !retrying) { + retrying = true + main.postDelayed({ if (state == State.IDLE) send(app, asked.text, asked.files) }, RETRY_MS) return } + retrying = false if (!stopped && queue.isNotEmpty()) { val next = queue.removeAt(0) send(app, next.text, next.files) diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt index 371c3576ca..27ceeb241e 100644 --- a/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentHostTest.kt @@ -59,6 +59,11 @@ class AgentHostTest { 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)