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

This commit is contained in:
Kurt Himebauch committed 2026-11-10 00:40:36 -05:00
1 parent 2cbb1e47e0
commit 0995cbd5c7
3 files changed
+15 -6

No files matched your search

@@ -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(
@@ -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)
@@ -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)