Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(llm): prevent HTTP400 after interrupting an agent (orphaned tool_calls)

Gotcha Contributorb23cbfbda3b2
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index 26b4770..e74a880 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -520,7 +520,13 @@ class AgentEngine(
// make the server 400 the whole chat (issue #13). executeToolCalls
// below still runs the ORIGINAL calls, so the model still sees the
// truthful "Malformed tool arguments" result and can fix itself.
- history += message.withValidToolCallArguments()
+ // Track the assistant index so an interrupt that orphans tool_calls
+ // can be repaired — otherwise the next request is malformed and
+ // the provider 400s every later turn.
+ val sanitizedAssistant = message.withValidToolCallArguments()
+ history += sanitizedAssistant
+ val orphanAssistantIdx = history.size - 1
+ val orphanExpectedCalls = sanitizedAssistant.toolCalls ?: toolCalls
if (message.hasText || !message.reasoningContent.isNullOrBlank()) {
events.onUi(
MessageKind.ASSISTANT,
@@ -617,7 +623,15 @@ class AgentEngine(
}
}
val historySizeBeforeTools = history.size
- executeToolCalls()
+ try {
+ executeToolCalls()
+ } catch (e: CancellationException) {
+ // Interrupt orphaned the assistant's tool_calls. Synthesize
+ // cancelled stubs so history stays valid for the next request
+ // and the provider does not 400 every later turn.
+ sanitizeOrphanedToolCalls(orphanAssistantIdx, orphanExpectedCalls)
+ throw e
+ }
// finish_task: the model's explicit "done" signal. Without it the only
// way out of this loop is a reply that happens to carry no tool calls,
@@ -1323,6 +1337,52 @@ class AgentEngine(
return trimmed
}
+ /**
+ * Repairs an orphaned assistant `tool_calls` left behind by an interrupt.
+ * Some LLM providers 400 the whole chat when `assistant.tool_calls` count
+ * != following `tool` results, bricking the session for every later turn.
+ * We synthesize `Cancelled` stubs for missing calls so the next request is
+ * spec-compliant. Called from `run`'s `CancellationException` path and from
+ * `ChatViewModel`'s interrupt cleanup.
+ */
+ internal fun sanitizeOrphanedToolCalls(assistantIdx: Int, expectedCalls: List<ToolCall>) {
+ if (assistantIdx < 0 || assistantIdx >= history.size) return
+ val assistant = history[assistantIdx]
+ val calls = expectedCalls.ifEmpty { assistant.toolCalls ?: return }
+ if (calls.isEmpty()) return
+ // Count tool results already written after this assistant.
+ var have = 0
+ for (i in assistantIdx + 1 until history.size) {
+ if (history[i].role == "tool") have++
+ // Stop at next user/assistant boundary — tool results are contiguous.
+ if (history[i].role == "user" || history[i].role == "assistant") break
+ }
+ if (have >= calls.size) return
+ // Synthesize missing tool stubs in order, reusing original ids.
+ for (idx in have until calls.size) {
+ val call = calls[idx]
+ history += ChatMessage(
+ role = "tool",
+ content = JsonPrimitive(
+ "Cancelled: agent was interrupted by the user before this tool completed."
+ ),
+ toolCallId = call.id
+ )
+ }
+ }
+
+ /** Convenience: sanitize the last assistant if it carries orphaned tool_calls. */
+ internal fun sanitizeLastOrphanedAssistant() {
+ for (i in history.size - 1 downTo 0) {
+ val msg = history[i]
+ if (msg.role == "assistant" && !msg.toolCalls.isNullOrEmpty()) {
+ sanitizeOrphanedToolCalls(i, msg.toolCalls!!)
+ return
+ }
+ if (msg.role == "user") return
+ }
+ }
+
/**
* Capture a screenshot via the `screencap -p` shell command, compressed as JPEG.
* Returns a [ScreenPerception.CompressedScreenshot] with base64 data, or null on failure.
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 2ee5e79..3546e7d 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -543,10 +543,19 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
agentEngine.run(agent)
} catch (_: CancellationException) {
appendEngineUi(MessageKind.ERROR, "Agent was interrupted by the user.")
+ // The interrupt may have orphaned an assistant with tool_calls but no
+ // matching tool results. Repair it in NonCancellable before the next
+ // turn is built — otherwise the provider 400s every later request.
+ withContext(NonCancellable) {
+ agentEngine.sanitizeLastOrphanedAssistant()
+ }
} finally {
withContext(NonCancellable) {
currentRunIsVoice = false
lastInputWasVoice = false
+ // Belt-and-suspenders: an interrupt that slipped past the engine's
+ // own sanitize still gets repaired here before persisting.
+ agentEngine.sanitizeLastOrphanedAssistant()
agentEngine.saveCurrentSession()
_uiState.update {
it.copy(