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.
Address review: keep the memory label after reopen, stricter empty check
Rishabh Bajpaifee1a4c27112
diff --git a/app/src/main/java/com/gotcha/data/MemoryStore.kt b/app/src/main/java/com/gotcha/data/MemoryStore.ktindex fd7fc62..d3ff748 100644--- a/app/src/main/java/com/gotcha/data/MemoryStore.kt+++ b/app/src/main/java/com/gotcha/data/MemoryStore.kt@@ -92,6 +92,17 @@ object MemoryStore {}}+ /**+ * True while [content] is still the generated skeleton. Used by the viewer+ * to tell "nothing here yet" from a brain the agent has already shaped,+ * including when the only change is to identity or the map.+ */+ fun isPristineSoul(content: String): Boolean = content.trim() == defaultSoul().trim()++ /** See [isPristineSoul]. */+ fun isPristineMemoryIndex(content: String): Boolean =+ content.trim() == defaultMemoryIndex().trim()+/** Rewrites user.md from Personal Info. Called on startup and on profile updates. */fun syncUserFile(settings: Settings) {runCatching {diff --git a/app/src/main/java/com/gotcha/data/SecondBrainSummary.kt b/app/src/main/java/com/gotcha/data/SecondBrainSummary.ktindex 7eb6bd2..6bb780e 100644--- a/app/src/main/java/com/gotcha/data/SecondBrainSummary.kt+++ b/app/src/main/java/com/gotcha/data/SecondBrainSummary.kt@@ -4,10 +4,10 @@ package com.gotcha.data* What the Second Brain page says above the files, taken from the snapshot it* already loaded.*- * [empty] means the vault holds nothing but the seeded skeleton — `SOUL.md`,- * `MEMORY.md`, `user.md` and the four area hubs. That is the state worth a- * "start it with a conversation" card: showing the same rows with an empty- * notes list teaches nothing.+ * [empty] means the vault holds nothing but the seeded skeleton — the generated+ * `SOUL.md` and `MEMORY.md`, `user.md` and the four area hubs. A brain whose+ * identity or map has already been edited is not empty, even with no notes+ * yet: the start card would be claiming it knows nothing when it does.*/data class SecondBrainSummary(val notes: Int,@@ -21,5 +21,9 @@ fun MemoryStore.SecondBrainSnapshot.summary(): SecondBrainSummary = SecondBrainSnotes = notes.size,people = people.size,areas = areas.size,- empty = notes.isEmpty() && people.isEmpty()+ empty = notes.isEmpty() &&+ people.isEmpty() &&+ raw.isEmpty() &&+ (soul == null || MemoryStore.isPristineSoul(soul.content)) &&+ (memoryIndex == null || MemoryStore.isPristineMemoryIndex(memoryIndex.content)))diff --git a/app/src/main/java/com/gotcha/data/SecondBrainWrite.kt b/app/src/main/java/com/gotcha/data/SecondBrainWrite.ktindex 56411f0..86b2518 100644--- a/app/src/main/java/com/gotcha/data/SecondBrainWrite.kt+++ b/app/src/main/java/com/gotcha/data/SecondBrainWrite.kt@@ -10,25 +10,49 @@ enum class SecondBrainWrite {}/**- * Reads a tool-call line as the transcript shows it — "write_file: Wrote 1.2 KB- * to '…' (resolved: /…/memory/notes/plan.md)." — and says whether it landed in- * [memoryRoot].+ * Reads a tool line and says whether it wrote inside [memoryRoot].*- * Deliberately strict about the tool name and about the resolved path: the- * label "Saved to Second Brain" must never appear for a file written somewhere- * else, and a line that was truncated before its resolved path says nothing.+ * Two shapes reach the transcript. While a run is live, [com.gotcha.agent.AgentEngine]+ * prefixes the tool name — "write_file: Wrote 1.2 KB to '…' (resolved: …)". A+ * reopened chat rebuilds its transcript from history, where the stored tool+ * message is only the result — "Wrote … (resolved: …)" for a write, "Replaced+ * 1 occurrence in '/…/note.md'." for an edit. Both have to be recognised, or+ * the label would disappear the first time the chat is opened again.+ *+ * Deliberately strict: a read, a write elsewhere, or a line truncated before+ * its path makes no claim at all.*/fun secondBrainWriteOf(text: String, memoryRoot: String): SecondBrainWrite? {val write = when {- text.startsWith("write_file:") -> SecondBrainWrite.SAVED- text.startsWith("edit:") -> SecondBrainWrite.UPDATED+ text.startsWith("write_file:") ||+ text.startsWith("Wrote ") ||+ text.startsWith("Appended ") -> SecondBrainWrite.SAVED++ text.startsWith("edit:") || text.startsWith("Replaced ") -> SecondBrainWrite.UPDATED+else -> return null}+ val path = resolvedPathIn(text) ?: editedPathIn(text) ?: return null+ val root = memoryRoot.trimEnd('/')+ if (root.isEmpty()) return null+ return if (path == root || path.startsWith("$root/")) write else null+}++/** The canonical target the file tools print as "(resolved: …)", when present. */+private fun resolvedPathIn(text: String): String? {val marker = "(resolved: "val at = text.lastIndexOf(marker)if (at < 0) return null- val path = text.substring(at + marker.length).substringBefore(')').trim()- val root = memoryRoot.trimEnd('/')- if (root.isEmpty() || path.isEmpty()) return null- return if (path == root || path.startsWith("$root/")) write else null+ return text.substring(at + marker.length).substringBefore(')').trim().ifEmpty { null }+}++/** The target of an edit result: "Replaced 1 occurrence in '/path'." */+private fun editedPathIn(text: String): String? {+ val marker = " in '"+ val at = text.indexOf(marker)+ if (at < 0) return null+ val start = at + marker.length+ val end = text.indexOf('\'', start)+ if (end < 0) return null+ return text.substring(start, end).trim().ifEmpty { null }}diff --git a/app/src/main/java/com/gotcha/scheduled/ScheduledTaskStore.kt b/app/src/main/java/com/gotcha/scheduled/ScheduledTaskStore.ktindex 6c93f62..a461454 100644--- a/app/src/main/java/com/gotcha/scheduled/ScheduledTaskStore.kt+++ b/app/src/main/java/com/gotcha/scheduled/ScheduledTaskStore.kt@@ -30,6 +30,7 @@ import java.util.UUIDinternal const val KEY_BRIEFING_SEEDED = "briefing_preset_seeded"internal const val KEY_BRIEFING_TASK_ID = "briefing_preset_task_id"+@Suppress("TooManyFunctions") // A storage facade: one small verb per operation, no shared state of its own.class ScheduledTaskStore internal constructor(private val prefs: SharedPreferences,private val attachmentsDir: Filediff --git a/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt b/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.ktindex 56d68f2..e8f5b13 100644--- a/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt+++ b/app/src/main/java/com/gotcha/ui/ScheduledTasksScreen.kt@@ -63,10 +63,10 @@ import com.gotcha.data.GotchaStorageimport com.gotcha.i18n.stringLookupimport com.gotcha.scheduled.Repeatimport com.gotcha.scheduled.RepeatKind+import com.gotcha.scheduled.ScheduledPresetsimport com.gotcha.scheduled.ScheduledStartimport com.gotcha.scheduled.ScheduledTaskimport com.gotcha.scheduled.ScheduledTaskForm-import com.gotcha.scheduled.ScheduledPresetsimport com.gotcha.scheduled.ScheduledTaskNotifierimport com.gotcha.scheduled.ScheduledTaskPlannerimport com.gotcha.scheduled.ScheduledTaskRunsdiff --git a/app/src/main/java/com/gotcha/ui/SecondBrainCards.kt b/app/src/main/java/com/gotcha/ui/SecondBrainCards.ktnew file mode 100644index 0000000..c273d0a--- /dev/null+++ b/app/src/main/java/com/gotcha/ui/SecondBrainCards.kt@@ -0,0 +1,108 @@+package com.gotcha.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.material3.Button+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.OutlinedButton+import androidx.compose.material3.Surface+import androidx.compose.material3.Text+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.testTag+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.gotcha.R+import com.gotcha.tools.AgentMode+import com.gotcha.ui.theme.fade++/**+ * What the page says before Gotcha can read its memory: what the thing is,+ * what it buys the user, and the one system toggle that stands in the way.+ * The old copy named the toggle but left the journey to the user.+ */+@Composable+internal fun SecondBrainAccessCard(onGrant: () -> Unit) {+ Surface(+ shape = RoundedCornerShape(16.dp),+ color = MaterialTheme.colorScheme.surfaceVariant.fade(0.4f),+ modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_setup")+ ) {+ Column(+ modifier = Modifier.padding(16.dp),+ verticalArrangement = Arrangement.spacedBy(8.dp)+ ) {+ Text(+ stringResource(R.string.second_brain_setup_title),+ style = MaterialTheme.typography.titleMedium+ )+ Text(+ stringResource(R.string.second_brain_setup_body),+ style = MaterialTheme.typography.bodyMedium+ )+ listOf(+ R.string.second_brain_setup_remembers,+ R.string.second_brain_setup_grows,+ R.string.second_brain_setup_yours+ ).forEach { line ->+ Text(+ "• " + stringResource(line),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant+ )+ }+ Button(+ onClick = onGrant,+ modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_grant")+ ) { Text(stringResource(R.string.second_brain_setup_grant)) }+ Text(+ stringResource(R.string.second_brain_setup_grant_hint),+ style = MaterialTheme.typography.labelSmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant+ )+ }+ }+}++/**+ * A vault holding only the seeded skeleton. Two prompts rather than an empty+ * list: one starts the memory, the other shows what it is for. Both fill the+ * composer and stop, like every other starter in the app.+ */+@Composable+internal fun SecondBrainStartCard(onStartChat: (String, AgentMode) -> Unit) {+ val meetPrompt = stringResource(R.string.second_brain_start_meet_prompt)+ val recallPrompt = stringResource(R.string.second_brain_start_recall_prompt)+ Surface(+ shape = RoundedCornerShape(16.dp),+ color = MaterialTheme.colorScheme.surfaceVariant.fade(0.4f),+ modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start")+ ) {+ Column(+ modifier = Modifier.padding(16.dp),+ verticalArrangement = Arrangement.spacedBy(8.dp)+ ) {+ Text(+ stringResource(R.string.second_brain_start_title),+ style = MaterialTheme.typography.titleMedium+ )+ Text(+ stringResource(R.string.second_brain_start_body),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant+ )+ Button(+ // Saving memory is a write, so this chat opens in Operator.+ onClick = { onStartChat(meetPrompt, AgentMode.OPERATOR) },+ modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start_meet")+ ) { Text(stringResource(R.string.second_brain_start_meet)) }+ OutlinedButton(+ onClick = { onStartChat(recallPrompt, AgentMode.MONITOR) },+ modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start_recall")+ ) { Text(stringResource(R.string.second_brain_start_recall)) }+ }+ }+}diff --git a/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt b/app/src/main/java/com/gotcha/ui/SecondBrainScreen.ktindex 27a65bf..9b1d5ee 100644--- a/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt+++ b/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt@@ -15,14 +15,12 @@ import androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.padding-import androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.text.selection.SelectionContainerimport androidx.compose.material3.Buttonimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.OutlinedButtonimport androidx.compose.material3.OutlinedTextField-import androidx.compose.material3.Surfaceimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.DisposableEffect@@ -277,94 +275,6 @@ fun SecondBrainScreen(}}-/**- * What the page says before Gotcha can read its memory: what the thing is,- * what it buys the user, and the one system toggle that stands in the way.- * The old copy named the toggle but left the journey to the user.- */-@Composable-private fun SecondBrainAccessCard(onGrant: () -> Unit) {- Surface(- shape = RoundedCornerShape(16.dp),- color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f),- modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_setup")- ) {- Column(- modifier = Modifier.padding(16.dp),- verticalArrangement = Arrangement.spacedBy(8.dp)- ) {- Text(- stringResource(R.string.second_brain_setup_title),- style = MaterialTheme.typography.titleMedium- )- Text(- stringResource(R.string.second_brain_setup_body),- style = MaterialTheme.typography.bodyMedium- )- listOf(- R.string.second_brain_setup_remembers,- R.string.second_brain_setup_grows,- R.string.second_brain_setup_yours- ).forEach { line ->- Text(- "• " + stringResource(line),- style = MaterialTheme.typography.bodySmall,- color = MaterialTheme.colorScheme.onSurfaceVariant- )- }- Button(- onClick = onGrant,- modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_grant")- ) { Text(stringResource(R.string.second_brain_setup_grant)) }- Text(- stringResource(R.string.second_brain_setup_grant_hint),- style = MaterialTheme.typography.labelSmall,- color = MaterialTheme.colorScheme.onSurfaceVariant- )- }- }-}--/**- * A vault holding only the seeded skeleton. Two prompts rather than an empty- * list: one starts the memory, the other shows what it is for. Both fill the- * composer and stop, like every other starter in the app.- */-@Composable-private fun SecondBrainStartCard(onStartChat: (String, AgentMode) -> Unit) {- val meetPrompt = stringResource(R.string.second_brain_start_meet_prompt)- val recallPrompt = stringResource(R.string.second_brain_start_recall_prompt)- Surface(- shape = RoundedCornerShape(16.dp),- color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f),- modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start")- ) {- Column(- modifier = Modifier.padding(16.dp),- verticalArrangement = Arrangement.spacedBy(8.dp)- ) {- Text(- stringResource(R.string.second_brain_start_title),- style = MaterialTheme.typography.titleMedium- )- Text(- stringResource(R.string.second_brain_start_body),- style = MaterialTheme.typography.bodySmall,- color = MaterialTheme.colorScheme.onSurfaceVariant- )- Button(- // Saving memory is a write, so this chat opens in Operator.- onClick = { onStartChat(meetPrompt, AgentMode.OPERATOR) },- modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start_meet")- ) { Text(stringResource(R.string.second_brain_start_meet)) }- OutlinedButton(- onClick = { onStartChat(recallPrompt, AgentMode.MONITOR) },- modifier = Modifier.fillMaxWidth().testTag("settings_second_brain_start_recall")- ) { Text(stringResource(R.string.second_brain_start_recall)) }- }- }-}-/*** Neuron-style brain graph: nodes render as shaded spheres at varying depths* across two hemispheres, links as curved synapses. Nodes drift gently so thediff --git a/app/src/test/java/com/gotcha/data/SecondBrainSummaryTest.kt b/app/src/test/java/com/gotcha/data/SecondBrainSummaryTest.ktindex 328d599..0f9b819 100644--- a/app/src/test/java/com/gotcha/data/SecondBrainSummaryTest.kt+++ b/app/src/test/java/com/gotcha/data/SecondBrainSummaryTest.kt@@ -13,11 +13,13 @@ class SecondBrainSummaryTest {private fun snapshot(notes: Int = 0,people: Int = 0,- areas: Int = 4+ areas: Int = 4,+ soul: String? = null,+ memoryIndex: String? = null) = MemoryStore.SecondBrainSnapshot(available = true,- soul = file("SOUL.md"),- memoryIndex = file("MEMORY.md"),+ soul = soul?.let { MemoryStore.BrainFile("SOUL.md", it, false) },+ memoryIndex = memoryIndex?.let { MemoryStore.BrainFile("MEMORY.md", it, false) },userCard = file("user.md"),areas = List(areas) { file("area$it.md") },people = List(people) { file("person$it.md") },@@ -44,6 +46,16 @@ class SecondBrainSummaryTest {assertFalse(snapshot(people = 1).summary().empty)}+ @Test+ fun `an edited identity is not empty even without notes`() {+ assertFalse(snapshot(soul = "# SOUL\n\n- Tone: dry").summary().empty)+ }++ @Test+ fun `an edited map is not empty even without notes`() {+ assertFalse(snapshot(memoryIndex = "# MEMORY\n\n- Gym: notes/gym.md").summary().empty)+ }+@Testfun `counts follow the snapshot`() {val summary = snapshot(notes = 3, people = 2, areas = 4).summary()diff --git a/app/src/test/java/com/gotcha/data/SecondBrainWriteTest.kt b/app/src/test/java/com/gotcha/data/SecondBrainWriteTest.ktindex 05f26e9..ac2dad9 100644--- a/app/src/test/java/com/gotcha/data/SecondBrainWriteTest.kt+++ b/app/src/test/java/com/gotcha/data/SecondBrainWriteTest.kt@@ -37,6 +37,29 @@ class SecondBrainWriteTest {assertNull(secondBrainWriteOf(line, root))}+ @Test+ fun `a reopened chat's stored write still says saved`() {+ // ChatViewModel rebuilds tool rows from history, where the tool name is+ // gone and only the result remains.+ val line = "Wrote 1.2 KB to 'notes/plan.md' (resolved: $root/notes/plan.md)."++ assertEquals(SecondBrainWrite.SAVED, secondBrainWriteOf(line, root))+ }++ @Test+ fun `a reopened chat's stored edit still says updated`() {+ val line = "Replaced 1 occurrence in '$root/MEMORY.md'.\n- old\n+ new"++ assertEquals(SecondBrainWrite.UPDATED, secondBrainWriteOf(line, root))+ }++ @Test+ fun `a stored edit outside the vault is not memory`() {+ val line = "Replaced 1 occurrence in '/sdcard/Download/a.md'.\n- old\n+ new"++ assertNull(secondBrainWriteOf(line, root))+ }+@Testfun `a read of a memory file is not a write`() {val line = "read_file: Read MEMORY.md (resolved: $root/MEMORY.md)."