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.

style(agent): satisfy detekt on the #143 review fixes

Durganshud40cf15a08d0
diff --git a/app/src/main/java/com/gotcha/tools/SubAgentSession.kt b/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
index b222e62..081cdce 100644
--- a/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
+++ b/app/src/main/java/com/gotcha/tools/SubAgentSession.kt
@@ -9,6 +9,7 @@ import com.gotcha.connectors.ConnectorRegistry
import com.gotcha.data.Settings
import com.gotcha.llm.ChatMessage
import com.gotcha.llm.LLMClient
+import com.gotcha.llm.ToolCall
import com.gotcha.llm.withValidToolCallArguments
import com.gotcha.util.GotchaLog
import kotlinx.coroutines.CancellationException
@@ -132,11 +133,7 @@ class SubAgentSession(
for (call in toolCalls) {
val toolName = call.function.name
blockedOn?.let { permission ->
- val skipped = "Not run: an earlier call in this step needs $permission, " +
- "which is not granted."
- history.add(ChatMessage(role = "tool", content = JsonPrimitive(skipped), toolCallId = call.id))
- collectedSteps.add("$toolName → skipped: needs $permission")
- onStep(toolName, "completed", "skipped: needs $permission")
+ skipCall(call, permission, history)
continue
}
onStep(toolName, "running", "")
@@ -181,14 +178,7 @@ class SubAgentSession(
)
)
- // Report reasoning text that sometimes comes alongside tool calls
- if (message.hasText && collectedSteps.none { it.startsWith("reasoning:") }) {
- val thought = message.textContent.take(120)
- if (thought.isNotBlank()) {
- collectedSteps.add("reasoning: $thought")
- onStep("reasoning", "completed", thought)
- }
- }
+ reportReasoning(message)
}
if (activeTokenCount > settings.maxContextTokens * 0.8) {
@@ -200,6 +190,25 @@ class SubAgentSession(
return SubAgentOutput(msg, collectedSteps.toList())
}
+ /** Reports reasoning text that sometimes comes alongside tool calls, once per run. */
+ private fun reportReasoning(message: ChatMessage) {
+ if (!message.hasText || collectedSteps.any { it.startsWith("reasoning:") }) return
+ val thought = message.textContent.take(120)
+ if (thought.isNotBlank()) {
+ collectedSteps.add("reasoning: $thought")
+ onStep("reasoning", "completed", thought)
+ }
+ }
+
+ /** Answers [call] without running it, as one planned after [permission] was refused. */
+ private fun skipCall(call: ToolCall, permission: String, history: MutableList<ChatMessage>) {
+ val toolName = call.function.name
+ val skipped = "Not run: an earlier call in this step needs $permission, which is not granted."
+ history.add(ChatMessage(role = "tool", content = JsonPrimitive(skipped), toolCallId = call.id))
+ collectedSteps.add("$toolName → skipped: needs $permission")
+ onStep(toolName, "completed", "skipped: needs $permission")
+ }
+
/**
* Puts a tool's missing permission in front of the user, the way the main
* agent does, instead of leaving it as prose in this session's private
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
index 06db061..007a049 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
@@ -7,9 +7,9 @@ import com.gotcha.data.Settings
import com.gotcha.data.SettingsRepository
import com.gotcha.testsupport.FakeAndroidKeyStore
import kotlinx.coroutines.async
-import kotlinx.coroutines.launch
import kotlinx.coroutines.job
import kotlinx.coroutines.joinAll
+import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.resetMain
@@ -134,28 +134,29 @@ class ChatViewModelPermissionGateTest {
}
@Test
- fun `a special access raised behind another app opens its Settings screen once Gotcha is back`() = runTest(dispatcher) {
- // App navigation stops on a missing access with another app in front.
- // Settings can't be opened from there, so the ask waits for onStart
- // instead of being dropped or blocked (issue #133).
- val delivered = mutableListOf<String>()
- val collector = launch { viewModel.permissionRequests.collect { delivered += it } }
- runCurrent()
- viewModel.setForeground(false)
-
- viewModel.runner.onPermissionRequest(com.gotcha.tools.ToolResult.ACCESSIBILITY_ACCESS)
- runCurrent()
- assertTrue("Settings was opened from behind another app: $delivered", delivered.isEmpty())
-
- viewModel.setForeground(true)
- runCurrent()
- assertEquals(listOf(com.gotcha.tools.ToolResult.ACCESSIBILITY_ACCESS), delivered)
-
- // Handed over once, not again on every return to the app.
- viewModel.setForeground(false)
- viewModel.setForeground(true)
- runCurrent()
- assertEquals(1, delivered.size)
- collector.cancel()
- }
+ fun `a special access raised behind another app opens its Settings screen once Gotcha is back`() =
+ runTest(dispatcher) {
+ // App navigation stops on a missing access with another app in front.
+ // Settings can't be opened from there, so the ask waits for onStart
+ // instead of being dropped or blocked (issue #133).
+ val delivered = mutableListOf<String>()
+ val collector = launch { viewModel.permissionRequests.collect { delivered += it } }
+ runCurrent()
+ viewModel.setForeground(false)
+
+ viewModel.runner.onPermissionRequest(com.gotcha.tools.ToolResult.ACCESSIBILITY_ACCESS)
+ runCurrent()
+ assertTrue("Settings was opened from behind another app: $delivered", delivered.isEmpty())
+
+ viewModel.setForeground(true)
+ runCurrent()
+ assertEquals(listOf(com.gotcha.tools.ToolResult.ACCESSIBILITY_ACCESS), delivered)
+
+ // Handed over once, not again on every return to the app.
+ viewModel.setForeground(false)
+ viewModel.setForeground(true)
+ runCurrent()
+ assertEquals(1, delivered.size)
+ collector.cancel()
+ }
}