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: update token estimation logic during history compaction and prevent orphaned tool messages by skipping leading tool entries

Rishabh Bajpaid061f34acd96
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index f7b88ca..971181e 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -265,7 +265,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
if (preserveLast != null) {
llmHistory.add(preserveLast)
}
- activeSessionTokenCount = response.usage?.totalTokens ?: 0
+ // The new token count is roughly the size of the summary + preserved message
+ val newTokensApprox = (summary.length / 4) + ((preserveLast?.content?.length ?: 0) / 4)
+ activeSessionTokenCount = newTokensApprox
updateContextUsage()
// Show the compacted message in the chat UI as an assistant message
appendUi(MessageKind.ASSISTANT, "[System: History Compacted]\n$summary")
@@ -435,7 +437,8 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
if (start < 0) start = 0
- while (start < llmHistory.size && llmHistory[start].role != "user") start++
+ // Skip leading tool messages so we don't have an orphaned tool result
+ while (start < llmHistory.size && llmHistory[start].role == "tool") start++
return llmHistory.subList(start, llmHistory.size)
}