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.

Review fixes: guard unbound mutation, single audit lines

Rishabh Bajpai9bdfb0178698
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index a3278a0..b08a746 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -1093,14 +1093,24 @@ class AgentEngine(
* without raising any prompt. No user content goes into the log line. The
* returned snapshot says which path was taken, so audit lines cannot
* mislabel a decision if the flag moves mid-handler.
+ *
+ * @param recordSkip whether to audit the skip here. Handlers with their
+ * own decision record (settings, scheduled tasks) pass false so one
+ * action leaves one skip line, not two.
*/
- private suspend fun confirmOrSkip(toolNames: List<String>, description: String): ConfirmOutcome {
+ private suspend fun confirmOrSkip(
+ toolNames: List<String>,
+ description: String,
+ recordSkip: Boolean = true
+ ): ConfirmOutcome {
if (!skipProtections) return ConfirmOutcome(events.awaitConfirmation(toolNames, description), false)
- toolExecutor.actionLog.record(
- toolNames.joinToString(","),
- "(pre-approved: protections skipped for this chat)",
- ToolResult.ok("Allowed without asking")
- )
+ if (recordSkip) {
+ toolExecutor.actionLog.record(
+ toolNames.joinToString(","),
+ "(pre-approved: protections skipped for this chat)",
+ ToolResult.ok("Allowed without asking")
+ )
+ }
return ConfirmOutcome(true, true)
}
@@ -1127,7 +1137,7 @@ class AgentEngine(
GotchaLog.d(TAG) { "handleUninstallConfirm: label=$label pkg=$pkg" }
val description = "Request to uninstall \"$label\". After you approve, a system dialog will " +
"open — you must tap \"OK\" in that dialog to complete the removal. This action is irreversible."
- val (approved, skipped) = confirmOrSkip(listOf("uninstall_app"), description)
+ val (approved, _) = confirmOrSkip(listOf("uninstall_app"), description)
if (approved) {
val actualResult = toolExecutor.executeUninstall(pkg)
@@ -1158,7 +1168,7 @@ class AgentEngine(
private suspend fun handleSendEmailConfirm(call: ToolCall, result: ToolResult) {
val payload = result.message.removePrefix("CONFIRM_SEND_EMAIL:")
val description = toolExecutor.describeSendEmail(payload)
- val (approved, skipped) = confirmOrSkip(listOf("send_email"), description)
+ val (approved, _) = confirmOrSkip(listOf("send_email"), description)
if (approved) {
val actualResult = toolExecutor.executeSendEmail(payload)
@@ -1203,7 +1213,8 @@ class AgentEngine(
} else {
val (approved, skipped) = confirmOrSkip(
listOf("update_gotcha_settings"),
- GotchaSettingsUpdate.describe(plan, request.reason)
+ GotchaSettingsUpdate.describe(plan, request.reason),
+ recordSkip = false
)
val decided = if (approved) {
handler(plan)
@@ -1252,7 +1263,8 @@ class AgentEngine(
} else {
val (approved, skipped) = confirmOrSkip(
listOf(call.function.name),
- withContext(Dispatchers.IO) { toolExecutor.scheduledTaskTool.describe(request, sessionId, agent) }
+ withContext(Dispatchers.IO) { toolExecutor.scheduledTaskTool.describe(request, sessionId, agent) },
+ recordSkip = false
)
val decided = if (approved) {
withContext(Dispatchers.IO) { toolExecutor.scheduledTaskTool.apply(request, sessionId, agent) }
@@ -1283,7 +1295,11 @@ class AgentEngine(
val id = parts[0]
val label = parts.getOrElse(1) { kind }
val description = "Request to delete $kind \"$label\" (#$id). This action is irreversible."
- val (approved, skipped) = confirmOrSkip(listOf("delete_$kind"), description)
+ val (approved, skipped) = confirmOrSkip(
+ listOf("delete_$kind"),
+ description,
+ recordSkip = kind != "scheduled_task"
+ )
val actualResult = if (approved) {
when (kind) {
diff --git a/app/src/main/java/com/gotcha/agent/ChatRunner.kt b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
index 1db28cd..6181584 100644
--- a/app/src/main/java/com/gotcha/agent/ChatRunner.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
@@ -372,7 +372,7 @@ class ChatRunner(private val app: Application) : AgentEvents {
// not. Keyed off the previous binding, never the id string alone.
if (protectionsSkippedSessionId != null && _state.value.boundSessionId != sessionId) {
protectionsSkippedSessionId = null
- engine.skipProtections = false
+ engine.revokeSkipProtections()
}
setState {
it.copy(
@@ -393,7 +393,10 @@ class ChatRunner(private val app: Application) : AgentEvents {
* or rebound. In-memory only; default off. Voice calls never call this.
*/
fun setProtectionsSkipped(sessionId: String, on: Boolean) {
- if (on && sessionId == _state.value.boundSessionId) {
+ // Anything for a chat that is not bound is a no-op: without this,
+ // touching an idle chat would silently revoke the running chat's skip.
+ if (sessionId != _state.value.boundSessionId) return
+ if (on) {
protectionsSkippedSessionId = sessionId
engine.skipProtections = true
} else {
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 02b46d0..96622ab 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -774,9 +774,6 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), C
fun confirmPendingActions(approved: Boolean) = runner.confirmPendingActions(approved)
- /** Whether chat [sessionId] has Skip Protections on (issue #155). */
- fun isProtectionsSkipped(sessionId: String): Boolean = runner.isProtectionsSkipped(sessionId)
-
/** Turns Skip Protections for chat [sessionId] on or off (issue #155). */
fun setProtectionsSkipped(sessionId: String, on: Boolean) = runner.setProtectionsSkipped(sessionId, on)
diff --git a/app/src/main/java/com/gotcha/ui/ChatScreen.kt b/app/src/main/java/com/gotcha/ui/ChatScreen.kt
index 1873c87..b8ee810 100644
--- a/app/src/main/java/com/gotcha/ui/ChatScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/ChatScreen.kt
@@ -313,6 +313,7 @@ fun ChatScreen(
onSetKeptOutOfNotifications = onSetChatKeptOutOfNotifications,
protectionsSkipped = protectionsSkipped,
onSetProtectionsSkipped = onSetProtectionsSkipped,
+ skipToggleEnabled = !otherChatRunning,
unreadNotifications = unreadNotifications,
onOpenInbox = onOpenInbox
)
@@ -343,7 +344,12 @@ fun ChatScreen(
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.weight(1f)
)
- TextButton(onClick = { onSetProtectionsSkipped(false) }) {
+ TextButton(
+ onClick = { onSetProtectionsSkipped(false) },
+ colors = ButtonDefaults.textButtonColors(
+ contentColor = MaterialTheme.colorScheme.onErrorContainer
+ )
+ ) {
Text(stringResource(R.string.chat_skip_protections_turn_off))
}
}
@@ -997,12 +1003,13 @@ private fun ChatOptionsMenu(
onSetKeptOutOfNotifications: (Boolean) -> Unit,
protectionsSkipped: Boolean,
onSetProtectionsSkipped: (Boolean) -> Unit,
+ skipToggleEnabled: Boolean,
unreadNotifications: Int,
onOpenInbox: () -> Unit
) {
var expanded by remember { mutableStateOf(false) }
// Danger dialog for turning the skip on; turning it off applies at once.
- var showSkipDanger by remember { mutableStateOf(false) }
+ var showSkipDanger by rememberSaveable { mutableStateOf(false) }
Box {
IconButton(onClick = { expanded = true }, modifier = Modifier.testTag("chat_options")) {
BadgedBox(
@@ -1087,6 +1094,9 @@ private fun ChatOptionsMenu(
DropdownMenuItem(
text = { Text(stringResource(R.string.chat_skip_protections)) },
trailingIcon = { Checkbox(checked = protectionsSkipped, onCheckedChange = null) },
+ // While another chat runs, the engine is bound elsewhere: the
+ // toggle would be a no-op (or read stale), so it is off-limits.
+ enabled = skipToggleEnabled,
onClick = {
if (protectionsSkipped) {
expanded = false
@@ -1115,7 +1125,11 @@ private fun ChatOptionsMenu(
onClick = {
showSkipDanger = false
onSetProtectionsSkipped(true)
- }
+ },
+ colors = ButtonDefaults.buttonColors(
+ containerColor = MaterialTheme.colorScheme.error,
+ contentColor = MaterialTheme.colorScheme.onError
+ )
) { Text(stringResource(R.string.chat_skip_protections_turn_on)) }
},
dismissButton = {
diff --git a/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt b/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt
index 68dadb8..90578b8 100644
--- a/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt
@@ -127,10 +127,15 @@ class ChatRunnerLifecycleTest {
assertTrue(runner.isProtectionsSkipped("chat-b"))
assertTrue(runner.engine.skipProtections)
- // Enabling for a chat that is not bound does nothing.
+ // Enabling for a chat that is not bound is a no-op and must not
+ // disturb the bound chat: it leaves another chat's skip intact.
runner.setProtectionsSkipped("chat-a", true)
assertFalse(runner.isProtectionsSkipped("chat-a"))
- assertFalse(runner.engine.skipProtections)
+ assertTrue(
+ "touching an unbound chat must not revoke the bound one",
+ runner.isProtectionsSkipped("chat-b")
+ )
+ assertTrue(runner.engine.skipProtections)
// Revoke clears the flag.
runner.setProtectionsSkipped("chat-b", true)