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.

refactor: optimize KV caching by separating volatile timestamps and non-mutative image culling in prompt construction

Rishabh Bajpai93d63a496ab7
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 67b255b..042daa6 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -608,17 +608,18 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
_uiState.update { it.copy(activity = "Thinking…") }
// Build message array optimized for prompt caching:
- // 1. Base environment block (identical across agent switches
- // except for a single "Active agent: X" line)
- // 2. Full conversation history (unchanged on switch)
- // 3. Agent instructions at the tail (changes on switch, keeps
- // the prefix [base env + history] in the KV cache)
+ // 1. Base environment block (fully static — no volatile fields)
+ // 2. Full conversation history (images culled non-mutatively)
+ // 3. Current timestamp (volatile — placed after cacheable prefix)
+ // 4. Agent instructions at the tail
+ // The [env block + history] prefix stays byte-identical across
+ // iterations, maximising server-side KV-cache hits.
val messages = buildList {
add(baseEnvironmentBlock(agent))
- addAll(trimmedHistory())
+ addAll(cullOldImages(trimmedHistory()))
+ add(currentTimestampMessage())
addAll(agentInstructionMessages(agent))
}
- cullOldImages()
val response = try {
llm.chat(messages, ToolRegistry.toolsForAgent(agent), sessionId = sessionId)
} catch (e: CancellationException) {
@@ -1060,14 +1061,26 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
/**
* Environment block sent as the first system message.
- * Only one line ("Active agent: X") differs between Monitor and Operator,
- * so the server KV cache for this ~300-token prefix is mostly preserved
- * across agent switches.
+ * Fully static within a session — no volatile fields like timestamps.
+ * Only "Active agent: X" differs between Monitor and Operator.
+ * The server KV cache for this prefix is preserved across all calls.
*/
private fun baseEnvironmentBlock(agent: AgentMode): ChatMessage {
return ChatMessage(role = "system", content = JsonPrimitive(buildEnvironmentString(agent)))
}
+ /**
+ * A separate system message carrying only the current date/time.
+ * Placed after the conversation history so the [baseEnvironmentBlock] +
+ * history prefix stays in the KV cache while the timestamp is always fresh.
+ */
+ private fun currentTimestampMessage(): ChatMessage {
+ val dateTime = java.text.SimpleDateFormat(
+ "EEE MMM dd yyyy, HH:mm:ss 'UTC'Z", java.util.Locale.US
+ ).apply { timeZone = java.util.TimeZone.getDefault() }.format(java.util.Date())
+ return ChatMessage(role = "system", content = JsonPrimitive("Current date/time: $dateTime"))
+ }
+
/**
* Agent-specific core prompt + system-reminder, sent at the tail of the
* message array (right before the latest user message). Placing these
@@ -1131,8 +1144,13 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
}
/**
- * Builds the environment info string with device info, date/time, agent mode,
- * and service statuses (PRD §14.1). Only varies by a single "Active agent: X" line.
+ * Builds the environment info string with device info, agent mode,
+ * and service statuses (PRD §14.1).
+ *
+ * The date/time is intentionally excluded — it is provided via
+ * [currentTimestampMessage] placed after the conversation history
+ * so that this block stays fully static and the server-side KV cache
+ * for the [env block + history] prefix is preserved across calls.
*/
private fun buildEnvironmentString(agent: AgentMode): String {
val app = getApplication<Application>()
@@ -1170,17 +1188,12 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
?.hasTransport(android.net.NetworkCapabilities.TRANSPORT_VPN) ?: false
} catch (_: Exception) { false }
- val dateTime = java.text.SimpleDateFormat("EEE MMM dd yyyy, HH:mm:ss 'UTC'Z", java.util.Locale.US)
- .apply { timeZone = java.util.TimeZone.getDefault() }
- .format(java.util.Date())
-
return buildString {
appendLine("You are powered by the model named ${settings.model}.")
appendLine("Here is some useful information about the environment you are running in:")
appendLine("<env>")
appendLine(" Device model: ${android.os.Build.MODEL} (${android.os.Build.MANUFACTURER})")
appendLine(" Android version: ${android.os.Build.VERSION.RELEASE} (API ${android.os.Build.VERSION.SDK_INT})")
- appendLine(" Current date/time: $dateTime")
appendLine(" Active agent: ${agent.name}")
appendLine(" Accessibility service enabled: ${if (accEnabled) "yes" else "no"}")
appendLine(" Notification listener enabled: ${if (notifEnabled) "yes" else "no"}")
@@ -1304,28 +1317,30 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
}
/**
- * Scans [llmHistory] for vision messages containing embedded images.
- * Strips the image data from all but the 2 most recent such messages,
- * keeping their text portions intact. This prevents accumulated
- * screenshots from exhausting the LLM context window.
+ * Returns a copy of [messages] with old vision messages replaced by
+ * text-only versions. Only the 2 most recent vision messages keep their
+ * image data. The original list (and [llmHistory]) is NOT modified,
+ * so the prefix sent in previous iterations stays cache-stable.
*/
- private fun cullOldImages() {
- val imageMsgIndices = llmHistory.indices.filter { i ->
- val content = llmHistory[i].content
+ private fun cullOldImages(messages: List<ChatMessage>): List<ChatMessage> {
+ val imageMsgIndices = messages.indices.filter { i ->
+ val content = messages[i].content
content is JsonArray && content.any { part ->
part.jsonObject["type"]?.jsonPrimitive?.content == "image_url"
}
}
- val toCull = imageMsgIndices.dropLast(2)
- for (idx in toCull) {
- val msg = llmHistory[idx]
- val text = msg.textContent
- llmHistory[idx] = msg.copy(
- content = JsonPrimitive(
- text + "\n\n[Previous screenshot removed to save context. " +
- "Only the 2 most recent screenshots are retained.]"
+ val toCull = imageMsgIndices.dropLast(2).toSet()
+ if (toCull.isEmpty()) return messages
+
+ return messages.mapIndexed { idx, msg ->
+ if (idx in toCull) {
+ msg.copy(
+ content = JsonPrimitive(
+ msg.textContent + "\n\n[Previous screenshot removed to save context. " +
+ "Only the 2 most recent screenshots are retained.]"
+ )
)
- )
+ } else msg
}
}
diff --git a/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt b/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
index b7de5b6..3c949bc 100644
--- a/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
@@ -33,6 +33,9 @@ class QuickAskEngine(
private val ttsEngine: TtsEngine,
private val llmProvider: () -> LLMClient?
) {
+ /** Stable session ID for prefix caching across consecutive quick-ask calls. */
+ private val sessionId: String = "quickask_${java.util.UUID.randomUUID()}"
+
private fun settings() = settingsRepository.load()
/** True when speech input is available (a usable STT provider is configured). */
@@ -156,7 +159,7 @@ class QuickAskEngine(
ChatMessage(role = "user", content = JsonPrimitive(userText))
}
- val response = llm.chat(listOf(system, user))
+ val response = llm.chat(listOf(system, user), sessionId = sessionId)
return response.choices.firstOrNull()?.message?.textContent?.ifBlank { "(no reply)" }
?: "(no reply)"
}
diff --git a/app/src/main/java/com/gotcha/tools/SubAgentSession.kt b/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
index 8c7ea2a..f5719ec 100644
--- a/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
+++ b/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
@@ -168,7 +168,7 @@ class SubAgentSession(
}
if (activeTokenCount > settings.maxContextTokens * 0.8) {
- trimHistory(history)
+ compactHistory(history, subLlm)
}
}
@@ -176,10 +176,67 @@ class SubAgentSession(
return SubAgentOutput(msg, collectedSteps.toList())
}
- private fun trimHistory(history: MutableList<ChatMessage>) {
- val sysPrompt = history.firstOrNull { it.role == "system" }
- history.clear()
- if (sysPrompt != null) history.add(sysPrompt)
- activeTokenCount = history.sumOf { it.textContent.length / 4 }
+ /**
+ * Compacts history using an LLM summarization call (same strategy as
+ * ChatViewModel.checkAndCompactHistory). Preserves the system prompt
+ * and original user task, replacing intermediate exchanges with a
+ * dense summary so no task context is lost.
+ */
+ private suspend fun compactHistory(
+ history: MutableList<ChatMessage>,
+ llm: LLMClient
+ ) {
+ if (history.size <= 3) return
+
+ val sysPrompt = history.first()
+ val userTask = history[1]
+
+ // Serialize the intermediate exchanges for the compaction LLM
+ val exchangeText = history.subList(2, history.size).joinToString("\n\n") { msg ->
+ val role = msg.role.uppercase()
+ val text = msg.textContent.ifEmpty {
+ if (!msg.toolCalls.isNullOrEmpty())
+ "Called tools: " + msg.toolCalls.joinToString(", ") { it.function.name }
+ else ""
+ }
+ "[$role]: $text"
+ }
+
+ val compactionSystem = ChatMessage(
+ role = "system",
+ content = JsonPrimitive(
+ "You are a context compaction agent. Compress the following sub-agent " +
+ "tool-call history into a dense summary preserving: what was attempted, " +
+ "what succeeded, what failed, and key data discovered. Be concise but " +
+ "do not lose technical specifics."
+ )
+ )
+ val compactionUser = ChatMessage(
+ role = "user",
+ content = JsonPrimitive(
+ "Original task: ${userTask.textContent}\n\n" +
+ "Intermediate exchanges:\n$exchangeText"
+ )
+ )
+
+ try {
+ val response = llm.chat(listOf(compactionSystem, compactionUser))
+ val summary = response.choices.firstOrNull()?.message?.textContent
+ if (!summary.isNullOrBlank()) {
+ history.clear()
+ history.add(sysPrompt)
+ history.add(userTask)
+ history.add(ChatMessage(
+ role = "assistant",
+ content = JsonPrimitive("[Context compacted]\n$summary")
+ ))
+ activeTokenCount = history.sumOf { it.textContent.length / 4 }
+ }
+ } catch (e: CancellationException) {
+ throw e
+ } catch (_: Exception) {
+ // Compaction failed — keep full history and continue
+ Log.w(TAG, "Sub-agent context compaction failed, keeping full history")
+ }
}
}