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(agent): address #146 review on the working-dir test and env doc

Durganshub08f48ed6e70
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index a6e35d0..cfaf6a2 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -1584,9 +1584,11 @@ class AgentEngine(
* and service statuses (PRD §14.1).
*
* The working directory and date/time are intentionally excluded — they are
- * provided via [runContextMessage] 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.
+ * provided via [runContextMessage] placed after the conversation history,
+ * so the per-chat directory and per-call timestamp no longer invalidate the
+ * server-side KV cache for the [env block + history] prefix. The rest of the
+ * block can still change between calls (service, VPN, Termux and connector
+ * state), but only when that state does.
*/
private fun buildEnvironmentString(agent: AgentMode): String {
val app = appContext
diff --git a/app/src/test/java/com/gotcha/agent/AgentPromptTest.kt b/app/src/test/java/com/gotcha/agent/AgentPromptTest.kt
index 42e3afc..6eac4c6 100644
--- a/app/src/test/java/com/gotcha/agent/AgentPromptTest.kt
+++ b/app/src/test/java/com/gotcha/agent/AgentPromptTest.kt
@@ -13,6 +13,7 @@ import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
+import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
@@ -95,6 +96,35 @@ class AgentPromptTest {
return server.takeRequest().body.readUtf8()
}
+ @Test
+ fun `working directory reaches the wire once, after the history, not in the system prefix`() = runTest {
+ // Issue #121: a per-chat path in the first system message broke prefix-cache
+ // reuse across chats, so it must ride in the tail message after the history.
+ server.enqueue(
+ MockResponse().setBody("""{"choices":[{"message":{"role":"assistant","content":"ok"}}]}""")
+ )
+ val engine = buildEngine(AgentMode.OPERATOR)
+ engine.history += com.gotcha.llm.ChatMessage(
+ role = "user",
+ content = kotlinx.serialization.json.JsonPrimitive("hello")
+ )
+ engine.run(AgentMode.OPERATOR)
+ val messages = kotlinx.serialization.json.Json.parseToJsonElement(server.takeRequest().body.readUtf8())
+ .let { it as kotlinx.serialization.json.JsonObject }["messages"]
+ .let { it as kotlinx.serialization.json.JsonArray }
+ .map { it as kotlinx.serialization.json.JsonObject }
+ fun role(i: Int) = (messages[i]["role"] as kotlinx.serialization.json.JsonPrimitive).content
+ fun text(i: Int) = messages[i]["content"].toString()
+
+ val userIndex = messages.indices.single { role(it) == "user" && text(it).contains("hello") }
+ val withPath = messages.indices.filter { text(it).contains("Working directory: ${workDir.absolutePath}") }
+ assertEquals("system", role(0))
+ assertFalse(text(0).contains("Working directory"))
+ assertEquals(listOf(userIndex + 1), withPath)
+ assertEquals("system", role(withPath.single()))
+ assertTrue(text(withPath.single()).contains("Current date/time: "))
+ }
+
@Test
fun `MONITOR prompt contains the language directive and English-tool-calls clause`() = runTest {
val body = requestBodyFor(AgentMode.MONITOR)
diff --git a/app/src/test/java/com/gotcha/agent/PromptCacheAndTokenOptimizationTest.kt b/app/src/test/java/com/gotcha/agent/PromptCacheAndTokenOptimizationTest.kt
index 18effd6..2268b58 100644
--- a/app/src/test/java/com/gotcha/agent/PromptCacheAndTokenOptimizationTest.kt
+++ b/app/src/test/java/com/gotcha/agent/PromptCacheAndTokenOptimizationTest.kt
@@ -380,6 +380,7 @@ class PromptCacheAndTokenOptimizationTest {
val engine = newEngine()
val first = "/storage/emulated/0/Gotcha/chats/First-Chat_aaaa1111"
val second = "/storage/emulated/0/Gotcha/chats/Second-Chat_bbbb2222"
+ val savedWorkingDir = com.gotcha.tools.FileResolver.WORKING_DIR_BASE
try {
com.gotcha.tools.FileResolver.WORKING_DIR_BASE = first
val prefixFirst = engine.systemPromptMessage(com.gotcha.tools.AgentMode.OPERATOR).textContent
@@ -396,8 +397,7 @@ class PromptCacheAndTokenOptimizationTest {
assertTrue(tailSecond.textContent.contains("Working directory: $second"))
assertTrue(tailSecond.textContent.contains("Current date/time: "))
} finally {
- com.gotcha.tools.FileResolver.WORKING_DIR_BASE =
- com.gotcha.data.GotchaStorage.root().absolutePath
+ com.gotcha.tools.FileResolver.WORKING_DIR_BASE = savedWorkingDir
}
}