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(scheduled): keep a same-chat run from changing the chat's mode

Durganshu6fcf24c0a0bf
diff --git a/app/src/main/java/com/gotcha/agent/ChatRunner.kt b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
index 03b8414..c311a7c 100644
--- a/app/src/main/java/com/gotcha/agent/ChatRunner.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
@@ -479,7 +479,10 @@ class ChatRunner(private val app: Application) : AgentEvents {
// nothing has said yet that Gotcha is out of sight.
if ((app as? GotchaApp)?.hasStartedActivity == false) appInForeground = false
if (saved != null) {
- bindSaved(saved, task.mode, saved.displayMessages)
+ // The run uses the task's mode, but the chat keeps its own: a task
+ // must not leave the user's chat switched to another mode.
+ val chatMode = saved.agentMode?.let { runCatching { AgentMode.valueOf(it) }.getOrNull() } ?: AgentMode.MONITOR
+ bindSaved(saved, chatMode, saved.displayMessages)
} else {
bindFresh(java.util.UUID.randomUUID().toString(), task.mode)
engine.sessionPersonaId = task.personaId
@@ -493,7 +496,7 @@ class ChatRunner(private val app: Application) : AgentEvents {
appendTranscript(MessageKind.USER, userDisplayText(task.prompt, msg, attachments), attachments = attachments)
// Main.immediate: executeRun marks the run busy and starts the keep-alive
// service before this returns, while the alarm's grace period still holds.
- agentJob = scope.launch { executeRun(engineAgent, runningId) }
+ agentJob = scope.launch { executeRun(task.mode, runningId) }
return ScheduledStart.Started(runningId)
}
diff --git a/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt b/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt
index a28407a..66c9cb3 100644
--- a/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt
@@ -35,6 +35,7 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -68,6 +69,7 @@ import com.gotcha.scheduled.ScheduledTaskScheduler
import com.gotcha.scheduled.ScheduledTaskStore
import com.gotcha.scheduled.TaskRun
import com.gotcha.scheduled.TaskRunStatus
+import com.gotcha.service.ChatRunService
import com.gotcha.tools.AgentMode
import com.gotcha.ui.theme.SkinAlertDialog
import kotlinx.coroutines.Dispatchers
@@ -108,6 +110,11 @@ fun ScheduledTasksScreen(onBack: () -> Unit, onOpenChat: (String) -> Unit) {
val tasks = remember(tick) { store.all() }
val canRunUnattended = remember(tick) { ScheduledTaskScheduler.canRunUnattended(context) }
+ // A run ending records its result just before the keep-alive service stops,
+ // so the list is read again each time a run starts or ends.
+ val running by ChatRunService.running.collectAsState()
+ LaunchedEffect(running?.sessionId) { tick++ }
+
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle) {
val observer = LifecycleEventObserver { _, event -> if (event == Lifecycle.Event.ON_RESUME) tick++ }
diff --git a/app/src/test/java/com/gotcha/agent/ScheduledRunTest.kt b/app/src/test/java/com/gotcha/agent/ScheduledRunTest.kt
index d946b77..c23495b 100644
--- a/app/src/test/java/com/gotcha/agent/ScheduledRunTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ScheduledRunTest.kt
@@ -174,6 +174,31 @@ class ScheduledRunTest {
assertEquals(listOf("Yesterday", "Summarise the news"), shown.take(2))
}
+ @Test
+ fun `a same-chat run keeps the chat's own mode`() {
+ runBlocking {
+ historyRepository.saveSession(
+ ChatSession(
+ id = "operator-chat",
+ title = "Kept",
+ lastModified = 0L,
+ messages = listOf(ChatMessage(role = "user", content = JsonPrimitive("Earlier"))),
+ agentMode = AgentMode.OPERATOR.name
+ )
+ )
+ }
+ // The task runs in Monitor; the chat stays an Operator chat.
+ task(sameChat = true, chat = "operator-chat")
+
+ runNow("news")
+ waitForRunToFinish()
+
+ assertEquals(
+ AgentMode.OPERATOR.name,
+ runBlocking { historyRepository.loadSession("operator-chat") }!!.agentMode
+ )
+ }
+
@Test
fun `a same-chat task whose chat is gone starts one and keeps it`() {
task(sameChat = true, chat = "deleted")