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.

feat: parameterize chat history directory; re-assert working dir per run

Durganshu Mishrac657e2069429
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index 3f90273..3a85208 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -270,6 +270,13 @@ class AgentEngine(
suspend fun run(agent: AgentMode) {
val llm = clientProvider() ?: return
val sessionId = this.sessionId ?: "unknown"
+ // KNOWN LIMITATION: WORKING_DIR_BASE is a process-wide global. We
+ // re-assert it at run start and after every tool round so sequential
+ // runs (in-app chat, voice call) always see their own directory, but
+ // two agents running tool rounds concurrently race last-writer-wins.
+ // The full fix is threading a cwd through ToolExecutor into the file
+ // tools (FileResolver already accepts one); deferred for now.
+ FileResolver.WORKING_DIR_BASE = workingDir().absolutePath
checkAndCompactHistory(llm)
repeat(settings.maxToolRounds) { iteration ->
if (iteration > 0) delay(INTER_CALL_DELAY_MS)
@@ -390,6 +397,8 @@ class AgentEngine(
}
executeToolCalls()
saveCurrentSession()
+ // Re-assert after each round; a concurrent engine may have moved it.
+ FileResolver.WORKING_DIR_BASE = workingDir().absolutePath
}
events.onUi(
MessageKind.ERROR,
diff --git a/app/src/main/java/com/gotcha/data/ChatHistoryRepository.kt b/app/src/main/java/com/gotcha/data/ChatHistoryRepository.kt
index 1c9024b..fafe5cf 100644
--- a/app/src/main/java/com/gotcha/data/ChatHistoryRepository.kt
+++ b/app/src/main/java/com/gotcha/data/ChatHistoryRepository.kt
@@ -18,11 +18,18 @@ data class ChatSession(
)
/**
- * Persists multiple chat sessions as JSON files in a dedicated 'chats' directory.
+ * Persists multiple chat sessions as JSON files in a dedicated directory
+ * (default 'chats'; voice calls use a separate 'calls' directory so they
+ * never show up in the main chat list).
*/
-class ChatHistoryRepository(context: Context) {
+class ChatHistoryRepository internal constructor(private val chatsDir: File) {
- private val chatsDir = File(context.filesDir, "chats").apply { mkdirs() }
+ constructor(context: Context, dirName: String = "chats") :
+ this(File(context.filesDir, dirName))
+
+ init {
+ chatsDir.mkdirs()
+ }
private val json = Json { ignoreUnknownKeys = true }
private val serializer = ChatSession.serializer()
diff --git a/app/src/test/java/com/gotcha/data/ChatHistoryRepositoryTest.kt b/app/src/test/java/com/gotcha/data/ChatHistoryRepositoryTest.kt
new file mode 100644
index 0000000..f7984c3
--- /dev/null
+++ b/app/src/test/java/com/gotcha/data/ChatHistoryRepositoryTest.kt
@@ -0,0 +1,65 @@
+package com.gotcha.data
+
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+import java.io.File
+
+/**
+ * Uses the internal File-based constructor so no Android Context is needed.
+ * Verifies the directory isolation that keeps voice-call sessions ("calls")
+ * out of the main chat list ("chats").
+ */
+class ChatHistoryRepositoryTest {
+
+ @get:Rule
+ val tmp = TemporaryFolder()
+
+ private fun session(id: String) = ChatSession(
+ id = id,
+ title = "Test $id",
+ lastModified = 0L,
+ messages = emptyList()
+ )
+
+ @Test
+ fun `save then load round-trips a session`() = runBlocking {
+ val repo = ChatHistoryRepository(tmp.newFolder("chats"))
+ repo.saveSession(session("abc"))
+ val loaded = repo.loadSession("abc")
+ assertEquals("abc", loaded!!.id)
+ assertEquals("Test abc", loaded.title)
+ }
+
+ @Test
+ fun `sessions saved in the calls directory are invisible to the chats repository`() = runBlocking {
+ val root = tmp.newFolder("filesDir")
+ val chatsRepo = ChatHistoryRepository(File(root, "chats"))
+ val callsRepo = ChatHistoryRepository(File(root, "calls"))
+
+ callsRepo.saveSession(session("call-1"))
+ chatsRepo.saveSession(session("chat-1"))
+
+ assertEquals(listOf("chat-1"), chatsRepo.listSessions().map { it.id })
+ assertEquals(listOf("call-1"), callsRepo.listSessions().map { it.id })
+ assertNull(chatsRepo.loadSession("call-1"))
+ }
+
+ @Test
+ fun `deleteSession removes the backing file`() = runBlocking {
+ val dir = tmp.newFolder("calls")
+ val repo = ChatHistoryRepository(dir)
+ repo.saveSession(session("gone"))
+ assertTrue(File(dir, "gone.json").exists())
+
+ repo.deleteSession("gone")
+
+ assertFalse(File(dir, "gone.json").exists())
+ assertTrue(repo.listSessions().isEmpty())
+ }
+}