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.
Keep the user-first guard working after compaction
Durganshue7533488d66b
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.ktindex b08a746..ea7c75d 100644--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt@@ -1912,14 +1912,17 @@ class AgentEngine(while (start < history.size && history[start].role == "tool") start++val trimmed = history.subList(start, history.size)- // Many LLM APIs strictly require history to start with a user message.- // If our truncation or compaction leaves an assistant message first, prepend a dummy user message.- if (trimmed.isNotEmpty() && trimmed.first().role == "assistant") {+ // Many LLM APIs strictly require the first non-system message to be the+ // user's. If truncation or compaction leaves an assistant message first+ // (compaction puts the memory startup system message ahead of its+ // summary), insert a dummy user message after the leading system ones.+ val firstTurn = trimmed.indexOfFirst { it.role != "system" }+ if (firstTurn >= 0 && trimmed[firstTurn].role == "assistant") {val note = ChatMessage(role = "user",content = JsonPrimitive("[System Note: Conversation context restored from previous turn]"))- return listOf(note) + trimmed+ return trimmed.subList(0, firstTurn) + note + trimmed.subList(firstTurn, trimmed.size)}return trimmed}diff --git a/app/src/test/java/com/gotcha/agent/AgentEngineOverheadTest.kt b/app/src/test/java/com/gotcha/agent/AgentEngineOverheadTest.ktindex d1e8dca..d2cb4e5 100644--- a/app/src/test/java/com/gotcha/agent/AgentEngineOverheadTest.kt+++ b/app/src/test/java/com/gotcha/agent/AgentEngineOverheadTest.kt@@ -279,6 +279,19 @@ class AgentEngineOverheadTest {1,startupHits)+ // The startup system message now sits ahead of the assistant+ // summary, so the user-first guard must look past system messages:+ // providers reject a request whose first non-system turn is the+ // assistant's (PR #174 review).+ val roles = kotlinx.serialization.json.Json.parseToJsonElement(mainRequest)+ .let { it as kotlinx.serialization.json.JsonObject }["messages"]+ .let { it as kotlinx.serialization.json.JsonArray }+ .map { (it as kotlinx.serialization.json.JsonObject)["role"].toString().trim('"') }+ assertEquals(+ "first non-system message after compaction must be the user's; roles=$roles",+ "user",+ roles.first { it != "system" }+ )} finally {if (previousRoot != null) {com.gotcha.data.GotchaStorage.rootPath = previousRoot