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.

Harden feature-use detection after review

Rishabh Bajpaid5b05257f2f4
diff --git a/app/src/main/java/com/gotcha/MainActivity.kt b/app/src/main/java/com/gotcha/MainActivity.kt
index b918d95..1060f21 100644
--- a/app/src/main/java/com/gotcha/MainActivity.kt
+++ b/app/src/main/java/com/gotcha/MainActivity.kt
@@ -1255,8 +1255,7 @@ class MainActivity : ComponentActivity() {
)
}
Route.SECOND_BRAIN -> {
- // The drawer's door: back returns to chat, while the
- // Settings page keeps its own back-to-list behavior.
+ // Reached from the drawer: back returns to chat.
BackHandler { currentRoute = Route.HOME }
SecondBrainScreen(
onBack = { currentRoute = Route.HOME },
diff --git a/app/src/main/java/com/gotcha/data/FeatureUsage.kt b/app/src/main/java/com/gotcha/data/FeatureUsage.kt
index 2263caa..c03605b 100644
--- a/app/src/main/java/com/gotcha/data/FeatureUsage.kt
+++ b/app/src/main/java/com/gotcha/data/FeatureUsage.kt
@@ -26,7 +26,9 @@ fun loadFeatureUsage(
/** Injectable for tests: the real store lives in encrypted prefs. */
loadTasks: () -> List<ScheduledTask> = { ScheduledTaskStore(context).all() }
): FeatureUsage = FeatureUsage(
- memory = MemoryStore.isInUse(),
+ // A listing can fail on a hostile or unmounted volume; "not used" is the
+ // safe answer, since it only means the chip may be offered.
+ memory = runCatching { MemoryStore.isInUse() }.getOrDefault(false),
schedule = runCatching { scheduleInUse(loadTasks()) }.getOrDefault(false)
)
diff --git a/app/src/main/java/com/gotcha/data/MemoryStore.kt b/app/src/main/java/com/gotcha/data/MemoryStore.kt
index b542b9b..b8f8539 100644
--- a/app/src/main/java/com/gotcha/data/MemoryStore.kt
+++ b/app/src/main/java/com/gotcha/data/MemoryStore.kt
@@ -99,18 +99,46 @@ object MemoryStore {
* viewer's full load.
*/
fun isInUse(): Boolean {
- fun hasMarkdown(dir: java.io.File): Boolean =
- dir.listFiles()?.any { it.isFile && it.extension.equals("md", true) } == true
- if (hasMarkdown(GotchaStorage.memoryNotesDir()) ||
- hasMarkdown(GotchaStorage.memoryPeopleDir()) ||
- GotchaStorage.memoryRawDir().listFiles()?.any { it.isFile } == true
+ fun markdownFiles(dir: java.io.File): List<java.io.File> = dir.listFiles().orEmpty().filter {
+ it.isFile && (it.extension.equals("md", true) || it.extension.equals("markdown", true))
+ }
+ // Empty files for which the viewer shows nothing are not use.
+ if (markdownFiles(GotchaStorage.memoryNotesDir()).any { it.length() > 0 } ||
+ markdownFiles(GotchaStorage.memoryPeopleDir()).any { it.length() > 0 }
+ ) {
+ return true
+ }
+ // Hidden files are the media scanner's, not the user's.
+ if (GotchaStorage.memoryRawDir().listFiles().orEmpty().any {
+ it.isFile && it.length() > 0 && !it.name.startsWith(".")
+ }
) {
return true
}
- val soul = runCatching { GotchaStorage.soulFile().readText() }.getOrNull()
- val index = runCatching { GotchaStorage.memoryIndexFile().readText() }.getOrNull()
- return (soul != null && !isPristineSoul(soul)) ||
- (index != null && !isPristineMemoryIndex(index))
+ val areas = markdownFiles(GotchaStorage.memoryAreasDir())
+ if (areas.size > 4) return true
+ if (areas.any { area -> runCatching { !isPristineArea(area.readText()) }.getOrDefault(false) }) {
+ return true
+ }
+ return editedFromDefault(GotchaStorage.soulFile(), SOUL_CAP_CHARS, ::isPristineSoul) ||
+ editedFromDefault(GotchaStorage.memoryIndexFile(), MEMORY_CAP_CHARS, ::isPristineMemoryIndex)
+ }
+
+ /**
+ * A generated file is untouched when it still matches its template. One
+ * longer than the startup cap cannot be, so it is not read at all; smaller
+ * files are compared with line endings normalised, since an external edit
+ * may have converted them to CRLF without changing a word.
+ */
+ private fun editedFromDefault(
+ file: java.io.File,
+ capChars: Int,
+ isPristine: (String) -> Boolean
+ ): Boolean {
+ if (!file.exists()) return false
+ if (file.length() > capChars) return true
+ val text = runCatching { file.readText() }.getOrNull() ?: return false
+ return !isPristine(text)
}
/**
@@ -118,11 +146,12 @@ object MemoryStore {
* 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()
+ fun isPristineSoul(content: String): Boolean =
+ normalizeNewlines(content) == normalizeNewlines(defaultSoul())
/** See [isPristineSoul]. */
fun isPristineMemoryIndex(content: String): Boolean =
- content.trim() == defaultMemoryIndex().trim()
+ normalizeNewlines(content) == normalizeNewlines(defaultMemoryIndex())
/**
* True while an area hub still holds only its generated heading and the one
@@ -131,10 +160,13 @@ object MemoryStore {
* user or agent added.
*/
fun isPristineArea(content: String): Boolean {
- val body = content.substringAfter("\n---\n", content)
+ val normalized = normalizeNewlines(content)
+ val body = normalized.substringAfter("\n---\n", normalized)
return body.lines().count { it.isNotBlank() } <= 2
}
+ private fun normalizeNewlines(text: String): String = text.replace("\r\n", "\n").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/ui/AppDrawer.kt b/app/src/main/java/com/gotcha/ui/AppDrawer.kt
index c872b2c..f188a2b 100644
--- a/app/src/main/java/com/gotcha/ui/AppDrawer.kt
+++ b/app/src/main/java/com/gotcha/ui/AppDrawer.kt
@@ -272,8 +272,7 @@ fun AppDrawerContent(
HorizontalDivider()
NavigationDrawerItem(
// Sits with Scheduled tasks: both are the parts of Gotcha that
- // keep working when no chat is open. The Settings row stays as
- // the second door, for people who look there first.
+ // keep working when no chat is open. This is the only door.
label = { Text(stringResource(R.string.drawer_second_brain)) },
icon = { Icon(Icons.Default.Psychology, contentDescription = null) },
selected = false,
diff --git a/app/src/main/java/com/gotcha/ui/ChatScreen.kt b/app/src/main/java/com/gotcha/ui/ChatScreen.kt
index d278593..eb1314a 100644
--- a/app/src/main/java/com/gotcha/ui/ChatScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/ChatScreen.kt
@@ -169,9 +169,21 @@ fun ChatScreen(
// per empty chat, so using it during this session retires the chip too.
LaunchedEffect(isHome, state.activeSessionId) {
if (!isHome) return@LaunchedEffect
+ // Blank while the check runs: a chip that was on in the previous empty
+ // chat must not flash if the feature has since been used.
+ showMemoryIntro = false
+ showScheduleIntro = false
val usage = withContext(Dispatchers.IO) { loadFeatureUsage(context) }
- showMemoryIntro = !featureIntro.hasSeen(FeatureIntroStore.MEMORY) && !usage.memory
- showScheduleIntro = !featureIntro.hasSeen(FeatureIntroStore.SCHEDULE) && !usage.schedule
+ // Usage retires a chip permanently, so deleting the note or task later
+ // does not bring "New for you" back for something they already know.
+ if (usage.memory && !featureIntro.hasSeen(FeatureIntroStore.MEMORY)) {
+ featureIntro.markSeen(FeatureIntroStore.MEMORY)
+ }
+ if (usage.schedule && !featureIntro.hasSeen(FeatureIntroStore.SCHEDULE)) {
+ featureIntro.markSeen(FeatureIntroStore.SCHEDULE)
+ }
+ showMemoryIntro = !featureIntro.hasSeen(FeatureIntroStore.MEMORY)
+ showScheduleIntro = !featureIntro.hasSeen(FeatureIntroStore.SCHEDULE)
}
// A draft handed over by the view model (a tapped daily tip): fill, never send.
LaunchedEffect(state.composerDraft) {
diff --git a/app/src/test/java/com/gotcha/data/FeatureUsageTest.kt b/app/src/test/java/com/gotcha/data/FeatureUsageTest.kt
index 01cc5f8..497c1bd 100644
--- a/app/src/test/java/com/gotcha/data/FeatureUsageTest.kt
+++ b/app/src/test/java/com/gotcha/data/FeatureUsageTest.kt
@@ -68,6 +68,80 @@ class FeatureUsageTest {
assertTrue(usage().memory)
}
+ @Test
+ fun `an edited map counts as memory use`() {
+ GotchaStorage.memoryIndexFile().writeText("# MEMORY\n\n- Gym: notes/gym.md")
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `a person counts as memory use`() {
+ File(GotchaStorage.memoryPeopleDir(), "ana.md").writeText("# Ana\n\nSister.")
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `a raw file counts as memory use`() {
+ File(GotchaStorage.memoryRawDir(), "receipt.pdf").writeBytes(byteArrayOf(1, 2, 3))
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `a hidden raw file is the media scanner, not use`() {
+ File(GotchaStorage.memoryRawDir(), ".nomedia").writeText("")
+
+ assertFalse(usage().memory)
+ }
+
+ @Test
+ fun `an empty note is not use`() {
+ File(GotchaStorage.memoryNotesDir(), "empty.md").writeText("")
+
+ assertFalse(usage().memory)
+ }
+
+ @Test
+ fun `a markdown note counts even with the long extension`() {
+ File(GotchaStorage.memoryNotesDir(), "tea.markdown").writeText("# Tea\n\nMasala chai.")
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `an edited area hub counts as memory use`() {
+ val work = File(GotchaStorage.memoryAreasDir(), "work.md")
+ work.writeText(work.readText() + "\n- Alpha ships Friday")
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `a fifth area counts as memory use`() {
+ File(GotchaStorage.memoryAreasDir(), "finance.md").writeText(
+ "---\ntitle: Finance\ntype: area\n---\n\n# Finance\n\nBudget notes."
+ )
+
+ assertTrue(usage().memory)
+ }
+
+ @Test
+ fun `CRLF in a generated file is still the skeleton`() {
+ val soul = GotchaStorage.soulFile()
+ soul.writeText(soul.readText().replace("\n", "\r\n"))
+
+ assertFalse(usage().memory)
+ }
+
+ @Test
+ fun `a failing task read does not fail the check`() {
+ val usage = loadFeatureUsage(context) { throw IllegalStateException("boom") }
+
+ assertFalse(usage.schedule)
+ }
+
@Test
fun `a task the user made counts as scheduling use`() {
tasks.put(task())