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.

Second Brain viewer with 3D neuron graph (#156)

Rishabh Bajpai46b4cfe4f667
diff --git a/app/src/main/assets/app/about-gotcha.md b/app/src/main/assets/app/about-gotcha.md
index 7ef77a5..74aeee8 100644
--- a/app/src/main/assets/app/about-gotcha.md
+++ b/app/src/main/assets/app/about-gotcha.md
@@ -72,6 +72,7 @@ Everything below is reached from **Settings** in the navigation drawer.
| Setting | What it controls |
| --- | --- |
| Personal Info | Name, occupation, background, reply style, location, preferred currency and language |
+| Second Brain | Gotcha's persistent memory: identity, user card, areas, people and notes (read-only viewer) |
| Language | App display language, AI reply language, voice language, STT language |
| AI | Hub for the two pages below |
| AI > AI Configuration | LLM provider, API key, Base URL, main and navigator models, API timeout (default 180 seconds; 0 never times out), max context tokens, max tool rounds, max repeated tool calls, max navigation tool calls, max consecutive delegations, cache and debug-screenshot clearing |
diff --git a/app/src/main/assets/skills/memory.json b/app/src/main/assets/skills/memory.json
index 9d6b76f..c19ff6b 100644
--- a/app/src/main/assets/skills/memory.json
+++ b/app/src/main/assets/skills/memory.json
@@ -5,5 +5,5 @@
"description": "How to use Gotcha persistent memory under Gotcha/memory.",
"requiresTools": ["read_file"],
"keywords": ["memory", "soul", "remember", "persona", "user card"],
- "instructions": "Gotcha persistent memory lives under Gotcha/memory next to chats. SOUL.md is identity and behavior. MEMORY.md is the master map with navigational addresses, never facts. user.md is synced from Settings Personal Info. Detail lives in people/, areas/, notes/ with frontmatter title, area, type, tags, updated.\n\nSession start: SOUL.md and MEMORY.md are already injected as [memory_startup] with absolute paths. Follow them for the whole chat. Load detail files only when the task needs them, with read_file or grep using ABSOLUTE paths. Never resolve memory paths relative to the chat working directory, which is a different folder. Never bulk read the folder.\n\nWrites: OPERATOR only with write_file or edit using absolute paths. In MONITOR, read only, never write. One fact in one place, link by path, never copy-paste content. Keep SOUL.md under 1500 chars and MEMORY.md under 1500 chars so session start stays cheap. Never store passwords, OTPs, API tokens, or private keys in memory files.\n\nAreas start as People, Work, Personal, Health. You may rename, add, or archive an area by editing MEMORY.md plus its hub file in areas/. Update MEMORY.md counts and links when you add a note that starts or advances a thread."
+ "instructions": "Gotcha persistent memory lives under Gotcha/memory next to chats. SOUL.md is identity and behavior. MEMORY.md is the master map with navigational addresses, never facts. user.md is synced from Settings Personal Info. Detail lives in people/, areas/, notes/ with frontmatter title, area, type, tags, updated. Raw drops (uploads, audio, images, documents in any format) live in raw/.\n\nSession start: SOUL.md and MEMORY.md are already injected as [memory_startup] with absolute paths. Follow them for the whole chat. Load detail files only when the task needs them, with read_file or grep using ABSOLUTE paths. Never resolve memory paths relative to the chat working directory, which is a different folder. Never bulk read the folder.\n\nLink nodes whenever possible: every note, person, area and raw file you create or update must link to the related nodes with Obsidian-style [[links]] (for example [[people/asha-sharma.md]] or [[notes/trip-plan.md]]), and update MEMORY.md so the graph stays connected. One fact in one place: link, never copy-paste content. The viewer draws these links as a node graph, so an unlinked file is an invisible island.\n\nRaw files: when the user shares or uploads a file worth keeping (a voice memo, photo, PDF, receipt, reference doc), save a copy into Gotcha/memory/raw/ with write_file when the content is small enough, and record its absolute path plus a one-line description in the related note and in MEMORY.md. For large files already on disk, do not duplicate them: record the absolute path in place instead. Never store passwords, OTPs, API tokens, or private keys anywhere in memory.\n\nWrites: OPERATOR only with write_file or edit using absolute paths. In MONITOR, read only, never write. Keep SOUL.md under 1500 chars and MEMORY.md under 1500 chars so session start stays cheap.\n\nAreas start as People, Work, Personal, Health. You may rename, add, or archive an area by editing MEMORY.md plus its hub file in areas/. Update MEMORY.md counts and links when you add a note that starts or advances a thread."
}
diff --git a/app/src/main/java/com/gotcha/data/GotchaStorage.kt b/app/src/main/java/com/gotcha/data/GotchaStorage.kt
index f4198ae..386b431 100644
--- a/app/src/main/java/com/gotcha/data/GotchaStorage.kt
+++ b/app/src/main/java/com/gotcha/data/GotchaStorage.kt
@@ -50,6 +50,7 @@ object GotchaStorage {
fun memoryAreasDir(): File = File(memoryRoot(), "areas")
fun memoryPeopleDir(): File = File(memoryRoot(), "people")
fun memoryNotesDir(): File = File(memoryRoot(), "notes")
+ fun memoryRawDir(): File = File(memoryRoot(), "raw")
fun callsRoot(): File = File(root(), "calls")
fun archiveRoot(): File = File(root(), "old_chats")
fun screenshotsRoot(): File = File(root(), "Screenshots")
diff --git a/app/src/main/java/com/gotcha/data/MemoryStore.kt b/app/src/main/java/com/gotcha/data/MemoryStore.kt
index fb48248..366257e 100644
--- a/app/src/main/java/com/gotcha/data/MemoryStore.kt
+++ b/app/src/main/java/com/gotcha/data/MemoryStore.kt
@@ -1,5 +1,6 @@
package com.gotcha.data
+import androidx.compose.ui.geometry.Offset
import com.gotcha.llm.ChatMessage
import kotlinx.serialization.json.JsonPrimitive
import java.io.File
@@ -16,6 +17,7 @@ import java.io.File
* with read_file/grep. The stable <memory> pointer in the system prompt
* costs about 60 tokens per turn.
*/
+@Suppress("TooManyFunctions")
object MemoryStore {
const val SOUL_CAP_CHARS = 1500
@@ -64,6 +66,7 @@ object MemoryStore {
GotchaStorage.memoryAreasDir().mkdirs()
GotchaStorage.memoryPeopleDir().mkdirs()
GotchaStorage.memoryNotesDir().mkdirs()
+ GotchaStorage.memoryRawDir().mkdirs()
val soul = GotchaStorage.soulFile()
if (!soul.exists()) {
@@ -137,6 +140,297 @@ object MemoryStore {
fun isStartupMessage(msg: ChatMessage): Boolean =
msg.role == "system" && msg.textContent.contains(STARTUP_MARKER)
+ /** One file listed by the Second Brain viewer. Content is capped for display. */
+ data class BrainFile(
+ val name: String,
+ val content: String,
+ val truncated: Boolean,
+ val sizeBytes: Long = content.length.toLong(),
+ val absolutePath: String = ""
+ ) {
+ val extension: String get() = name.substringAfterLast('.', "").lowercase()
+ val isMarkdown: Boolean get() = extension == "md" || extension == "markdown" || extension.isEmpty()
+ }
+
+ /** Everything the Second Brain viewer shows, loaded best-effort. */
+ data class SecondBrainSnapshot(
+ val available: Boolean,
+ val soul: BrainFile?,
+ val memoryIndex: BrainFile?,
+ val userCard: BrainFile?,
+ val areas: List<BrainFile>,
+ val people: List<BrainFile>,
+ val notes: List<BrainFile>,
+ val raw: List<BrainFile> = emptyList()
+ ) {
+ /** Every markdown node shown on the graph, keyed by file name. */
+ fun allMarkdown(): List<BrainFile> = listOfNotNull(soul, memoryIndex, userCard) + areas + people + notes
+ }
+
+ /** Max chars shown per file in the viewer. */
+ const val VIEWER_FILE_CAP_CHARS = 8000
+
+ /** Matches Obsidian-style links: [[path]] or [[path|label]]. */
+ val wikiLinkRegex = Regex("""\[\[([^\]|]+)(?:\|[^\]]*)?]]""")
+
+ /** Extracts link targets from markdown, in order, deduplicated. */
+ fun parseWikiLinks(markdown: String): List<String> =
+ wikiLinkRegex.findAll(markdown).map { it.groupValues[1].trim() }.filter { it.isNotEmpty() }.distinct().toList()
+
+ /** One node on the Second Brain graph. */
+ data class BrainNode(val id: String, val label: String, val kind: String)
+
+ /** One directed link between two node ids. */
+ data class BrainEdge(val fromId: String, val toId: String)
+
+ /** Nodes plus resolved edges for the graph view. */
+ data class BrainGraph(val nodes: List<BrainNode>, val edges: List<BrainEdge>)
+
+ /**
+ * Builds the node-link graph: one node per markdown file plus one per raw
+ * file, edges from [[links]] resolved against relative paths first
+ * (areas/work.md) then bare names (work.md), deduplicated.
+ *
+ * Node ids are memory-relative paths (areas/work.md, raw/memo.mp3), so two
+ * sections can hold same-named files without colliding. Link targets are
+ * parsed from the full file on disk, not the capped preview, up to
+ * [LINK_SCAN_CAP_CHARS].
+ */
+ fun buildGraph(snapshot: SecondBrainSnapshot): BrainGraph {
+ val nodes = mutableListOf<BrainNode>()
+ val byFull = mutableMapOf<String, String>()
+ val byBase = mutableMapOf<String, String>()
+ fun norm(path: String): String = path.lowercase().removeSuffix(".md").removeSuffix(".markdown")
+ fun addNode(id: String, label: String, kind: String) {
+ nodes.add(BrainNode(id, label, kind))
+ byFull.putIfAbsent(norm(id), id)
+ byBase.putIfAbsent(norm(id.substringAfterLast('/')), id)
+ }
+ fun relId(file: BrainFile, fallback: String): String {
+ val abs = file.absolutePath
+ if (abs.isNotBlank()) {
+ val root = runCatching { GotchaStorage.memoryRoot().absolutePath }.getOrNull()
+ if (root != null && abs.startsWith(root)) {
+ return abs.removePrefix(root).trimStart('/')
+ }
+ }
+ return fallback
+ }
+ data class Source(val id: String, val file: BrainFile)
+ val sources = mutableListOf<Source>()
+ fun addSource(file: BrainFile?, fallbackId: String, label: String, kind: String) {
+ if (file == null) return
+ val id = relId(file, fallbackId)
+ addNode(id, label, kind)
+ sources.add(Source(id, file))
+ }
+ addSource(snapshot.soul, "SOUL.md", "SOUL", "soul")
+ addSource(snapshot.memoryIndex, "MEMORY.md", "MEMORY", "map")
+ addSource(snapshot.userCard, "user.md", "User", "user")
+ snapshot.areas.forEach { addSource(it, "areas/${it.name}", it.name.removeSuffix(".md"), "area") }
+ snapshot.people.forEach { addSource(it, "people/${it.name}", it.name.removeSuffix(".md"), "person") }
+ snapshot.notes.forEach { addSource(it, "notes/${it.name}", it.name.removeSuffix(".md"), "note") }
+ snapshot.raw.forEach { addSource(it, "raw/${it.name}", it.name, "raw") }
+ fun resolve(target: String): String? {
+ val normTarget = norm(target.trim())
+ if (normTarget.isEmpty()) return null
+ return byFull[normTarget] ?: byBase[normTarget] ?: byBase[normTarget.substringAfterLast('/')]
+ }
+ val edges = mutableSetOf<BrainEdge>()
+ sources.forEach { source ->
+ if (!source.file.isMarkdown) return@forEach
+ parseWikiLinks(fullTextForLinks(source.file)).forEach { target ->
+ val toId = resolve(target)
+ if (toId != null && toId != source.id) edges.add(BrainEdge(source.id, toId))
+ }
+ }
+ return BrainGraph(nodes, edges.toList())
+ }
+
+ /** Max chars scanned per file for [[link]] parsing (preview cap stays smaller). */
+ const val LINK_SCAN_CAP_CHARS = 200_000
+
+ /** Brain shell radii in graph units. Layout content is confined inside these. */
+ const val BRAIN_RX = 520f
+ const val BRAIN_RY = 360f
+ const val BRAIN_RZ = 280f
+
+ /**
+ * Pulls every point inside the brain ellipsoid (with margin), so no neuron
+ * can sit outside the shell no matter how many files are added later.
+ * Returns the adjusted positions and depths.
+ */
+ fun confineToBrain(points: List<Offset>, depths: List<Float>): Pair<List<Offset>, List<Float>> {
+ if (points.size != depths.size) return points to depths
+ return points.zip(depths).map { (p, z) ->
+ val e = (p.x / BRAIN_RX) * (p.x / BRAIN_RX) +
+ (p.y / BRAIN_RY) * (p.y / BRAIN_RY) +
+ (z / BRAIN_RZ) * (z / BRAIN_RZ)
+ if (e <= 0.81f) {
+ p to z
+ } else {
+ val s = (0.9f / kotlin.math.sqrt(e))
+ Offset(p.x * s, p.y * s) to (z * s)
+ }
+ }.unzip()
+ }
+
+ /**
+ * Force-directed layout (Fruchterman-Reingold, as used by Obsidian graph
+ * plugins): repulsion pushes all nodes apart, springs pull linked nodes
+ * together, gravity keeps the cloud centered. Fixed seed, so the same
+ * vault always draws the same brain. Coordinates are centered at the
+ * origin in a 1000x700 box; the viewer scales them to the canvas.
+ */
+ fun forceLayout(
+ nodeCount: Int,
+ edgeIndexPairs: List<Pair<Int, Int>>,
+ iterations: Int = 160
+ ): List<Offset> {
+ if (nodeCount == 0) return emptyList()
+ if (nodeCount == 1) return listOf(Offset.Zero)
+ val width = 1000f
+ val height = 700f
+ val random = kotlin.random.Random(42)
+ val pos = Array(nodeCount) {
+ Offset(random.nextFloat() * width - width / 2f, random.nextFloat() * height - height / 2f)
+ }
+ val area = width * height
+ val k = kotlin.math.sqrt(area / nodeCount)
+ var temperature = minOf(width, height) / 4f
+ repeat(iterations) {
+ val disp = Array(nodeCount) { Offset.Zero }
+ for (i in 0 until nodeCount) {
+ for (j in i + 1 until nodeCount) {
+ val delta = pos[i] - pos[j]
+ val dist = delta.getDistance().coerceAtLeast(0.01f)
+ val force = k * k / dist
+ val push = delta / dist * force
+ disp[i] += push
+ disp[j] -= push
+ }
+ }
+ edgeIndexPairs.forEach { (a, b) ->
+ if (a in 0 until nodeCount && b in 0 until nodeCount && a != b) {
+ val delta = pos[a] - pos[b]
+ val dist = delta.getDistance().coerceAtLeast(0.01f)
+ val force = dist * dist / k
+ val pull = delta / dist * force
+ disp[a] -= pull
+ disp[b] += pull
+ }
+ }
+ for (i in 0 until nodeCount) {
+ disp[i] -= pos[i] * 0.02f
+ val len = disp[i].getDistance().coerceAtLeast(0.01f)
+ val capped = disp[i] / len * minOf(len, temperature)
+ pos[i] = Offset(
+ (pos[i].x + capped.x).coerceIn(-width / 2f, width / 2f),
+ (pos[i].y + capped.y).coerceIn(-height / 2f, height / 2f)
+ )
+ }
+ temperature *= 0.95f
+ }
+ return pos.toList()
+ }
+
+ /** Full text for link parsing, read straight from disk to see past the preview cap. */
+ private fun fullTextForLinks(file: BrainFile): String {
+ val abs = file.absolutePath
+ if (abs.isNotBlank()) {
+ return readUpTo(File(abs), LINK_SCAN_CAP_CHARS) ?: file.content
+ }
+ return file.content
+ }
+
+ /** Reads at most [maxChars] chars; null on any failure. Never loads the whole file. */
+ private fun readUpTo(file: File, maxChars: Int): String? {
+ return try {
+ if (!file.exists() || !file.isFile) return null
+ file.bufferedReader().use { reader ->
+ val out = StringBuilder()
+ val buf = CharArray(8192)
+ var remaining = maxChars + 1
+ while (remaining > 0) {
+ val n = reader.read(buf, 0, minOf(buf.size, remaining))
+ if (n < 0) break
+ out.append(buf, 0, n)
+ remaining -= n
+ }
+ out.toString()
+ }
+ } catch (_: Exception) {
+ null
+ }
+ }
+
+ /** Loads all viewer content. Never throws; missing files become null/empty lists. */
+ fun loadSnapshot(): SecondBrainSnapshot {
+ val soul = loadBrainFile(GotchaStorage.soulFile())
+ val index = loadBrainFile(GotchaStorage.memoryIndexFile())
+ val user = loadBrainFile(GotchaStorage.memoryUserFile())
+ return SecondBrainSnapshot(
+ available = soul != null || index != null,
+ soul = soul,
+ memoryIndex = index,
+ userCard = user,
+ areas = listBrainFiles(GotchaStorage.memoryAreasDir()),
+ people = listBrainFiles(GotchaStorage.memoryPeopleDir()),
+ notes = listBrainFiles(GotchaStorage.memoryNotesDir()),
+ raw = listRawFiles(GotchaStorage.memoryRawDir())
+ )
+ }
+
+ /** Extensions previewed as text in the viewer; anything else shows file info plus Open. */
+ private val textExtensions = setOf("md", "markdown", "txt", "yaml", "yml", "json", "csv", "log")
+
+ private fun loadBrainFile(file: File): BrainFile? {
+ return try {
+ if (!file.exists() || !file.isFile) return null
+ val ext = file.name.substringAfterLast('.', "").lowercase()
+ val size = file.length()
+ if (ext.isNotEmpty() && ext !in textExtensions) {
+ return BrainFile(file.name, "", false, size, file.absolutePath)
+ }
+ val text = readUpTo(file, VIEWER_FILE_CAP_CHARS) ?: return null
+ if (text.isBlank()) return null
+ if (text.length <= VIEWER_FILE_CAP_CHARS) {
+ BrainFile(file.name, text, false, size, file.absolutePath)
+ } else {
+ BrainFile(file.name, text.take(VIEWER_FILE_CAP_CHARS), true, size, file.absolutePath)
+ }
+ } catch (_: Exception) {
+ null
+ }
+ }
+
+ private fun listBrainFiles(dir: File): List<BrainFile> {
+ return try {
+ if (!dir.exists() || !dir.isDirectory) return emptyList()
+ dir.listFiles()
+ ?.filter { it.isFile && it.name.endsWith(".md") }
+ ?.sortedBy { it.name.lowercase() }
+ ?.mapNotNull { loadBrainFile(it) }
+ .orEmpty()
+ } catch (_: Exception) {
+ emptyList()
+ }
+ }
+
+ /** Raw drops in any format: audio, images, documents. Content loads only for text types. */
+ private fun listRawFiles(dir: File): List<BrainFile> {
+ return try {
+ if (!dir.exists() || !dir.isDirectory) return emptyList()
+ dir.listFiles()
+ ?.filter { it.isFile }
+ ?.sortedBy { it.name.lowercase() }
+ ?.mapNotNull { loadBrainFile(it) }
+ .orEmpty()
+ } catch (_: Exception) {
+ emptyList()
+ }
+ }
+
internal data class CappedText(val text: String, val truncated: Int)
internal fun readCapped(file: File, cap: Int): CappedText? {
diff --git a/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt b/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt
new file mode 100644
index 0000000..b319185
--- /dev/null
+++ b/app/src/main/java/com/gotcha/ui/SecondBrainScreen.kt
@@ -0,0 +1,965 @@
+package com.gotcha.ui
+
+import android.content.Intent
+import android.webkit.MimeTypeMap
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.gestures.awaitEachGesture
+import androidx.compose.foundation.gestures.awaitFirstDown
+import androidx.compose.foundation.gestures.detectTapGestures
+import androidx.compose.foundation.gestures.detectTransformGestures
+import androidx.compose.foundation.gestures.waitForUpOrCancellation
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.text.selection.SelectionContainer
+import androidx.compose.material3.Button
+import androidx.compose.material3.HorizontalDivider
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.OutlinedButton
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.runtime.withFrameNanos
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.Path
+import androidx.compose.ui.graphics.StrokeCap
+import androidx.compose.ui.graphics.drawscope.Stroke
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.layout.onSizeChanged
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.platform.testTag
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.drawText
+import androidx.compose.ui.text.font.FontFamily
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.text.rememberTextMeasurer
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.core.content.FileProvider
+import com.gotcha.R
+import com.gotcha.data.MemoryStore
+import com.gotcha.tools.FileResolver
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import java.io.File
+import kotlin.math.cos
+import kotlin.math.min
+import kotlin.math.sin
+
+/**
+ * The Second Brain viewer: read-only view of Gotcha's persistent memory
+ * (issue #156) so the user never needs a file explorer to see it.
+ *
+ * Two views: a list of sections with in-place Markdown previews, and a node
+ * graph drawn from the [[links]] between files. Non-text files (audio,
+ * images, documents) show file info with an Open button instead of a preview.
+ * Editing stays in the agent and the file system for now.
+ */
+@Composable
+fun SecondBrainScreen(onBack: () -> Unit) {
+ val overlay = rememberSettingsOverlayState()
+ var snapshot by remember { mutableStateOf<MemoryStore.SecondBrainSnapshot?>(null) }
+ var showGraph by remember { mutableStateOf(false) }
+
+ LaunchedEffect(Unit) {
+ snapshot = withContext(Dispatchers.IO) { MemoryStore.loadSnapshot() }
+ }
+
+ SettingsScaffold(
+ title = stringResource(SettingsPage.SECOND_BRAIN.title),
+ onBack = onBack,
+ overlay = overlay
+ ) {
+ Text(
+ stringResource(R.string.second_brain_intro),
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ modifier = Modifier.testTag("settings_second_brain_intro")
+ )
+
+ val current = snapshot
+ if (current == null) {
+ Text(
+ stringResource(R.string.second_brain_loading),
+ style = MaterialTheme.typography.bodyMedium,
+ modifier = Modifier.testTag("settings_second_brain_loading")
+ )
+ return@SettingsScaffold
+ }
+ if (!current.available) {
+ Text(
+ stringResource(R.string.second_brain_unavailable),
+ style = MaterialTheme.typography.bodyMedium,
+ modifier = Modifier.testTag("settings_second_brain_unavailable")
+ )
+ return@SettingsScaffold
+ }
+
+ Row(
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
+ modifier = Modifier.fillMaxWidth()
+ ) {
+ if (!showGraph) {
+ Button(
+ onClick = {},
+ enabled = false,
+ modifier = Modifier.testTag("settings_second_brain_tab_list")
+ ) { Text(stringResource(R.string.second_brain_tab_list)) }
+ OutlinedButton(
+ onClick = { showGraph = true },
+ modifier = Modifier.testTag("settings_second_brain_tab_graph")
+ ) { Text(stringResource(R.string.second_brain_tab_graph)) }
+ } else {
+ OutlinedButton(
+ onClick = { showGraph = false },
+ modifier = Modifier.testTag("settings_second_brain_tab_list")
+ ) { Text(stringResource(R.string.second_brain_tab_list)) }
+ Button(
+ onClick = {},
+ enabled = false,
+ modifier = Modifier.testTag("settings_second_brain_tab_graph")
+ ) { Text(stringResource(R.string.second_brain_tab_graph)) }
+ }
+ }
+
+ if (showGraph) {
+ SecondBrainGraphView(snapshot = current)
+ } else {
+ current.soul?.let {
+ BrainSection(
+ heading = stringResource(R.string.second_brain_identity),
+ file = it,
+ tag = "identity"
+ )
+ }
+ current.memoryIndex?.let {
+ BrainSection(
+ heading = stringResource(R.string.second_brain_map),
+ file = it,
+ tag = "map"
+ )
+ }
+ current.userCard?.let {
+ BrainSection(
+ heading = stringResource(R.string.second_brain_user_card),
+ file = it,
+ tag = "user"
+ )
+ }
+ BrainFileList(
+ heading = stringResource(R.string.second_brain_areas),
+ files = current.areas,
+ tag = "areas"
+ )
+ BrainFileList(
+ heading = stringResource(R.string.second_brain_people),
+ files = current.people,
+ tag = "people"
+ )
+ BrainFileList(
+ heading = stringResource(R.string.second_brain_notes),
+ files = current.notes,
+ tag = "notes"
+ )
+ BrainFileList(
+ heading = stringResource(R.string.second_brain_raw),
+ files = current.raw,
+ tag = "raw"
+ )
+ }
+ }
+}
+
+/**
+ * Neuron-style brain graph: nodes render as shaded spheres at varying depths
+ * across two hemispheres, links as curved synapses. Nodes drift gently so the
+ * structure feels alive. Tap a neuron to preview its file below.
+ */
+@Composable
+private fun SecondBrainGraphView(snapshot: MemoryStore.SecondBrainSnapshot) {
+ val graph = remember(snapshot) { MemoryStore.buildGraph(snapshot) }
+ var selectedId by remember(graph) { mutableStateOf<String?>(null) }
+ var zoom by remember(graph) { mutableStateOf(1f) }
+ // Starting view is a three-quarter turn: cerebellum and brainstem sit at
+ // the right end, length reading horizontally like a side-view diagram.
+ var yawDeg by remember(graph) { mutableStateOf(-55f) }
+ var pitchDeg by remember(graph) { mutableStateOf(20f) }
+ var pan by remember(graph) { mutableStateOf(Offset.Zero) }
+ var rotationDeg by remember(graph) { mutableStateOf(0f) }
+ // True while a double-tap-drag is spinning the globe; the transform
+ // detector ignores slide input meanwhile so one motion never does both.
+ val yawLock = remember(graph) { mutableStateOf(false) }
+ val placed = remember(graph) {
+ val indexById = graph.nodes.mapIndexed { index, node -> node.id to index }.toMap()
+ val pairs = graph.edges.mapNotNull { edge ->
+ val a = indexById[edge.fromId]
+ val b = indexById[edge.toId]
+ if (a != null && b != null) a to b else null
+ }
+ val points = MemoryStore.forceLayout(graph.nodes.size, pairs)
+ val depths = List(graph.nodes.size) { nodeZ(it) }
+ MemoryStore.confineToBrain(points, depths)
+ }
+ val layout: List<Offset> = placed.first
+ val layoutZ: List<Float> = placed.second
+ val degree = remember(graph) {
+ graph.nodes.associate { node ->
+ node.id to graph.edges.count { it.fromId == node.id || it.toId == node.id }
+ }
+ }
+ // Last moment (ms) the user touched the graph. The brain auto-rotates
+ // only after 4 idle seconds; any gesture marks interaction and pauses it.
+ var lastTouchMs by remember(graph) { mutableStateOf(0L) }
+ fun markTouched() {
+ lastTouchMs = System.currentTimeMillis()
+ }
+ LaunchedEffect(graph) {
+ var last = withFrameNanos { it }
+ while (true) {
+ val now = withFrameNanos { it }
+ val dtSec = (now - last) / 1_000_000_000f
+ last = now
+ if (System.currentTimeMillis() - lastTouchMs > 4000L) {
+ yawDeg = (yawDeg + dtSec * 9f) % 360f
+ }
+ }
+ }
+ // 1-hop neighborhood of the focused neuron, or everything when unfocused.
+ val visible = remember(graph, selectedId) {
+ if (selectedId == null) {
+ graph.nodes.map { it.id }.toSet()
+ } else {
+ buildSet {
+ add(selectedId!!)
+ graph.edges.forEach { edge ->
+ if (edge.fromId == selectedId) add(edge.toId)
+ if (edge.toId == selectedId) add(edge.fromId)
+ }
+ }
+ }
+ }
+ val textMeasurer = rememberTextMeasurer()
+ val scheme = MaterialTheme.colorScheme
+ val nodeFill = remember(scheme) {
+ mapOf(
+ "soul" to scheme.primary,
+ "map" to scheme.secondary,
+ "user" to scheme.tertiary,
+ "area" to scheme.primaryContainer,
+ "person" to scheme.secondaryContainer,
+ "note" to scheme.surfaceVariant,
+ "raw" to scheme.errorContainer
+ )
+ }
+
+ // Viewport math shared by drawing and tap detection. Single-finger drags
+ // slide the brain, two fingers pinch-zoom and rotate it on screen, and a
+ // double-tap-drag spins it around the vertical axis like a globe.
+ fun mapper(width: Float, height: Float): GraphMapper {
+ val fit = (min(width, height) / 1000f).coerceAtLeast(0.1f)
+ return GraphMapper(
+ center = Offset(width / 2f, height / 2f) + pan,
+ scale = fit * zoom,
+ yawRad = Math.toRadians(yawDeg.toDouble()).toFloat(),
+ pitchRad = Math.toRadians(pitchDeg.toDouble()).toFloat(),
+ inPlaneRad = Math.toRadians(rotationDeg.toDouble()).toFloat()
+ )
+ }
+
+ if (graph.nodes.isEmpty()) {
+ Text(
+ stringResource(R.string.second_brain_empty),
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant
+ )
+ return
+ }
+
+ val labelStyle = TextStyle(fontSize = 10.sp, color = scheme.onSurface)
+ var canvasPx by remember { mutableStateOf(androidx.compose.ui.unit.IntSize.Zero) }
+ Canvas(
+ modifier = Modifier
+ .fillMaxWidth()
+ .height(360.dp)
+ .testTag("settings_second_brain_graph")
+ .onSizeChanged { canvasPx = it }
+ .pointerInput(graph) {
+ detectTapGestures(
+ onTap = { tap ->
+ val width = canvasPx.width.toFloat()
+ val height = canvasPx.height.toFloat()
+ if (width <= 0f || height <= 0f) return@detectTapGestures
+ val map = mapper(width, height)
+ var best: String? = null
+ var bestDist = Float.MAX_VALUE
+ val tapRadius = neuronScreenRadius(width, height) * 2.6f
+ graph.nodes.forEachIndexed { index, node ->
+ val dist = (map.project(layout[index], layoutZ[index]).screen - tap).getDistance()
+ if (dist < bestDist) {
+ bestDist = dist
+ best = node.id
+ }
+ }
+ if (best != selectedId) markTouched()
+ selectedId = if (bestDist <= tapRadius && best != selectedId) best else null
+ },
+ onDoubleTap = {
+ markTouched()
+ zoom = 1f
+ pan = Offset.Zero
+ rotationDeg = 0f
+ yawDeg = -55f
+ pitchDeg = 20f
+ selectedId = null
+ }
+ )
+ }
+ .pointerInput(graph) {
+ // Double-tap-drag spins the globe horizontally. Only true taps
+ // are recorded, so plain drags keep sliding via the detector below.
+ val timeout = viewConfiguration.doubleTapTimeoutMillis
+ val slop = viewConfiguration.touchSlop * 2
+ var lastTapTime = 0L
+ var lastTapPos = Offset.Zero
+ awaitEachGesture {
+ val down = awaitFirstDown(requireUnconsumed = false)
+ val now = System.currentTimeMillis()
+ if (now - lastTapTime < timeout && (down.position - lastTapPos).getDistance() < slop) {
+ yawLock.value = true
+ markTouched()
+ try {
+ var last = down.position
+ val id = down.id
+ while (true) {
+ val event = awaitPointerEvent()
+ val change = event.changes.find { it.id == id }
+ if (change == null || !change.pressed) break
+ yawDeg = (yawDeg + (change.position.x - last.x) * 0.35f) % 360f
+ last = change.position
+ markTouched()
+ if (event.changes.all { !it.pressed }) break
+ }
+ } finally {
+ yawLock.value = false
+ }
+ lastTapTime = 0L
+ } else {
+ val up = waitForUpOrCancellation()
+ if (up != null) {
+ lastTapTime = System.currentTimeMillis()
+ lastTapPos = up.position
+ } else {
+ lastTapTime = 0L
+ }
+ }
+ }
+ }
+ .pointerInput(graph) {
+ detectTransformGestures { _, panChange, zoomChange, rotationChange ->
+ if (!yawLock.value) {
+ markTouched()
+ pan += panChange
+ }
+ zoom = (zoom * zoomChange).coerceIn(0.4f, 4f)
+ rotationDeg = (rotationDeg + rotationChange) % 360f
+ }
+ }
+ ) {
+ val map = mapper(size.width, size.height)
+ val byId = graph.nodes.mapIndexed { index, node -> node.id to index }.toMap()
+ val projected = graph.nodes.indices.map { map.project(layout[it], layoutZ[it]) }
+ drawBrainShell(map)
+ graph.edges.forEach { edge ->
+ val fromIdx = byId[edge.fromId]
+ val toIdx = byId[edge.toId]
+ if (fromIdx != null && toIdx != null) {
+ val bothVisible = edge.fromId in visible && edge.toId in visible
+ val from = projected[fromIdx].screen
+ val to = projected[toIdx].screen
+ val fromDepth = projected[fromIdx].depth01
+ val toDepth = projected[toIdx].depth01
+ // 3D synapse: segments fade from source depth to target depth.
+ val mid = (from + to) / 2f
+ val normal = Offset(-(to.y - from.y), to.x - from.x)
+ val normalLen = normal.getDistance().coerceAtLeast(1f)
+ val control = mid + normal / normalLen * (from.getDistance() * 0.12f + 18f)
+ val segments = 10
+ val baseAlpha = if (bothVisible) 0.65f else 0.08f
+ for (s in 0 until segments) {
+ val t0 = s / segments.toFloat()
+ val t1 = (s + 1) / segments.toFloat()
+ val depth = fromDepth + (toDepth - fromDepth) * ((t0 + t1) / 2f)
+ drawLine(
+ color = scheme.outline.copy(alpha = baseAlpha * (1f - 0.5f * depth)),
+ start = quadPoint(from, control, to, t0),
+ end = quadPoint(from, control, to, t1),
+ strokeWidth = 3f,
+ cap = StrokeCap.Round
+ )
+ }
+ }
+ }
+ // Back-to-front by projected depth.
+ val order = graph.nodes.indices.sortedByDescending { projected[it].depth01 }
+ order.forEach { index ->
+ val node = graph.nodes[index]
+ val focused = node.id in visible
+ val depth = projected[index].depth01
+ val alpha = (if (focused) 1f else 0.12f) * (1f - 0.4f * depth)
+ val center = projected[index].screen
+ // Graph-unit radius: grows and shrinks with zoom, always fits the brain.
+ val radius = NEURON_GRAPH_RADIUS * map.scale * projected[index].persp *
+ (1f + 0.15f * (degree[node.id] ?: 0)) * (if (node.id == selectedId) 1.25f else 1f)
+ val base = nodeFill[node.kind] ?: scheme.surfaceVariant
+ drawCircle(
+ brush = Brush.radialGradient(
+ colors = listOf(
+ Color.White.copy(alpha = 0.85f * alpha),
+ base.copy(alpha = alpha),
+ base.copy(alpha = 0.55f * alpha)
+ ),
+ center = center + Offset(-radius * 0.3f, -radius * 0.3f),
+ radius = radius * 1.6f
+ ),
+ radius = radius,
+ center = center
+ )
+ if (node.id == selectedId) {
+ drawCircle(
+ color = scheme.onSurface,
+ radius = radius + 5f,
+ center = center,
+ style = androidx.compose.ui.graphics.drawscope.Stroke(width = 3f)
+ )
+ }
+ val showLabel = zoom >= 0.8f || node.id == selectedId || (selectedId != null && node.id in visible)
+ if (showLabel) {
+ val short = node.label.take(10)
+ val measured = textMeasurer.measure(
+ short,
+ labelStyle.copy(color = labelStyle.color.copy(alpha = alpha))
+ )
+ drawText(
+ textLayoutResult = measured,
+ topLeft = Offset(center.x - measured.size.width / 2f, center.y + radius + 4f)
+ )
+ }
+ }
+ }
+
+ Row(
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
+ modifier = Modifier.fillMaxWidth().padding(top = 4.dp)
+ ) {
+ OutlinedButton(
+ onClick = {
+ zoom = 1f
+ pan = Offset.Zero
+ rotationDeg = 0f
+ yawDeg = -55f
+ pitchDeg = 20f
+ selectedId = null
+ },
+ modifier = Modifier.testTag("settings_second_brain_graph_reset")
+ ) { Text(stringResource(R.string.second_brain_graph_reset)) }
+ }
+
+ val selected = selectedId?.let { id -> graph.nodes.find { it.id == id } }
+ if (selected != null) {
+ val file = findBrainFile(snapshot, selected.id)
+ val neighborCount = (visible.size - 1).coerceAtLeast(0)
+ Text(
+ text = selected.label + stringResource(R.string.second_brain_graph_neighbors, neighborCount),
+ style = MaterialTheme.typography.titleSmall,
+ fontWeight = FontWeight.Bold,
+ modifier = Modifier.padding(top = 4.dp)
+ )
+ if (file != null) {
+ BrainFileBody(file = file, tag = "graph_selected")
+ }
+ } else {
+ Text(
+ text = stringResource(R.string.second_brain_graph_hint),
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant
+ )
+ }
+}
+
+/** Viewport math shared by drawing and tap detection: yaw spins around the
+ * vertical axis (globe-style, perpendicular to the screen), pitch tilts,
+ * in-plane rotation turns the finished picture. */
+private data class GraphMapper(
+ val center: Offset,
+ val scale: Float,
+ val yawRad: Float,
+ val pitchRad: Float,
+ val inPlaneRad: Float
+) {
+ /** Projects a force-layout point plus depth offset into screen space. */
+ fun project(p: Offset, z: Float): Projected {
+ val s = project3(p.x, p.y, z)
+ return Projected(screen = s.screen, depth01 = s.depth01, persp = s.persp)
+ }
+
+ /** Projects raw 3D coordinates (used by the brain shell). */
+ fun project3(x: Float, y: Float, z: Float): Projected {
+ val cosY = cos(yawRad)
+ val sinY = sin(yawRad)
+ val x1 = x * cosY + z * sinY
+ val z1 = -x * sinY + z * cosY
+ val cosP = cos(pitchRad)
+ val sinP = sin(pitchRad)
+ val y2 = y * cosP - z1 * sinP
+ val z2 = y * sinP + z1 * cosP
+ val focal = 1600f
+ val persp = focal / (focal + z2)
+ val flat = Offset(x1, y2) * scale * persp
+ val cosI = cos(inPlaneRad)
+ val sinI = sin(inPlaneRad)
+ return Projected(
+ screen = center + Offset(
+ flat.x * cosI - flat.y * sinI,
+ flat.x * sinI + flat.y * cosI
+ ),
+ depth01 = (((z2 / 600f) + 1f) / 2f).coerceIn(0f, 1f),
+ persp = persp
+ )
+ }
+}
+
+private data class Projected(val screen: Offset, val depth01: Float, val persp: Float)
+
+/** Deterministic depth spread per node, inside the shell's z-radius. */
+private fun nodeZ(index: Int): Float = ((index * 53 % 100) / 100f - 0.5f) * 520f
+
+/** Neuron radius in graph units: scales with zoom through the viewport. */
+private const val NEURON_GRAPH_RADIUS = 30f
+
+/** Approximate screen radius for tap hit-testing at the given canvas size. */
+private fun neuronScreenRadius(width: Float, height: Float): Float {
+ val fit = (min(width, height) / 1000f).coerceAtLeast(0.1f)
+ return 30f * fit * 1.6f
+}
+
+/** Point on a quadratic bezier at t, for segmented gradient edges. */
+private fun quadPoint(p0: Offset, c: Offset, p1: Offset, t: Float): Offset {
+ val u = 1f - t
+ return Offset(
+ u * u * p0.x + 2f * u * t * c.x + t * t * p1.x,
+ u * u * p0.y + 2f * u * t * c.y + t * t * p1.y
+ )
+}
+
+/**
+ * The translucent 3D brain shell holding every neuron: a bumpy two-lobed
+ * ellipsoid sampled as real 3D points and projected through the current
+ * yaw/pitch, so the shell itself turns in 3D with the memories inside it.
+ * Sulci ride on the surface (dimmer on the far side); the longitudinal
+ * fissure splits the hemispheres.
+ */
+private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawBrainShell(map: GraphMapper) {
+ val shellColor = Color(0xFF7FB3D5)
+
+ // Parametric bumpy cerebrum. Frontal taper narrows the front so the
+ // silhouette reads as a brain, not a rugby ball. Confined layout content
+ // (±520/±360/±280) fits inside the 600/420/340 radii.
+ fun surface(lon: Float, lat: Float): OffsetTriple {
+ val bump = 1f + 0.05f * sin(3f * lon + 0.8f) + 0.035f * sin(2f * lat + 1.7f) + 0.02f * sin(5f * lon - lat)
+ // Slight center groove between the hemispheres.
+ val groove = 1f - 0.10f * (cos(lon) * cos(lon)) * cos(lat) * cos(lat)
+ // Frontal taper: narrower toward the front (+z). Long in x, lower in y,
+ // like a side-view diagram rather than a ball.
+ val frontness = (sin(lon) * 0.5f + 0.5f)
+ val taper = 1f - 0.10f * frontness
+ return OffsetTriple(
+ 630f * bump * groove * taper * cos(lat) * cos(lon),
+ 390f * bump * taper * sin(lat),
+ 340f * bump * cos(lat) * sin(lon)
+ )
+ }
+ fun sampleSphere(
+ center: OffsetTriple,
+ radius: Float,
+ lonSteps: Int = 16,
+ latSteps: Int = 5
+ ): List<OffsetTriple> {
+ val out = mutableListOf<OffsetTriple>()
+ for (li in 0 until lonSteps) {
+ val lon = 2f * Math.PI.toFloat() * li / lonSteps
+ for (si in 0..latSteps) {
+ val lat = -1.2f + 2.4f * si / latSteps
+ out.add(
+ OffsetTriple(
+ center.x + radius * cos(lat) * cos(lon),
+ center.y + radius * sin(lat),
+ center.z + radius * cos(lat) * sin(lon)
+ )
+ )
+ }
+ }
+ return out
+ }
+ // Sample the surface, project, silhouette via convex hull.
+ val samples = mutableListOf<Offset>()
+ val lonSteps = 28
+ val latSteps = 7
+ for (li in 0 until lonSteps) {
+ val lon = 2f * Math.PI.toFloat() * li / lonSteps
+ for (si in 0..latSteps) {
+ val lat = -1.25f + 2.5f * si / latSteps
+ val s = surface(lon, lat)
+ samples.add(map.project3(s.x, s.y, s.z).screen)
+ }
+ }
+ val hull = convexHull(samples)
+ if (hull.size >= 3) {
+ val path = Path().apply {
+ moveTo(hull[0].x, hull[0].y)
+ hull.drop(1).forEach { lineTo(it.x, it.y) }
+ close()
+ }
+ drawPath(path = path, color = shellColor.copy(alpha = 0.10f))
+ drawPath(path = path, color = shellColor.copy(alpha = 0.45f), style = Stroke(width = 3f))
+ }
+ // Cerebellum and brainstem below; see drawHindbrain.
+ drawHindbrain(map)
+ // Longitudinal fissure: surface line over the top center.
+ var prev: Offset? = null
+ for (si in 0..24) {
+ val lat = -1.2f + 2.4f * si / 24f
+ val s = surface(Math.PI.toFloat() / 2f, lat)
+ val p = map.project3(s.x, s.y, s.z)
+ prev?.let {
+ drawLine(
+ color = shellColor.copy(alpha = 0.5f * (1f - 0.6f * p.depth01)),
+ start = it,
+ end = p.screen,
+ strokeWidth = 3f
+ )
+ }
+ prev = p.screen
+ }
+ // A few sulci per hemisphere, fading on the far side.
+ val pi = Math.PI.toFloat()
+ val sulci = listOf(
+ Triple(0.5f to -0.5f, 0.9f to -0.1f, 1.2f to -0.5f),
+ Triple(0.4f to 0.2f, 0.9f to 0.4f, 1.3f to 0.1f),
+ Triple(0.6f to 0.7f, 1.0f to 0.9f, 1.4f to 0.6f),
+ Triple((pi - 0.5f) to -0.5f, (pi - 0.9f) to -0.1f, (pi - 1.2f) to -0.5f),
+ Triple((pi - 0.4f) to 0.2f, (pi - 0.9f) to 0.4f, (pi - 1.3f) to 0.1f),
+ Triple((pi - 0.6f) to 0.7f, (pi - 1.0f) to 0.9f, (pi - 1.4f) to 0.6f)
+ )
+ sulci.forEach { (p0, c, p1) ->
+ val a = surface(p0.first, p0.second)
+ val b = surface(c.first, c.second)
+ val d = surface(p1.first, p1.second)
+ var last: Offset? = null
+ var lastDepth = 0f
+ for (s in 0..10) {
+ val t = s / 10f
+ val gx = quad1(a.x, b.x, d.x, t)
+ val gy = quad1(a.y, b.y, d.y, t)
+ val gz = quad1(a.z, b.z, d.z, t)
+ val proj = map.project3(gx, gy, gz)
+ last?.let {
+ drawLine(
+ color = shellColor.copy(alpha = 0.35f * (1f - 0.7f * (lastDepth + proj.depth01) / 2f)),
+ start = it,
+ end = proj.screen,
+ strokeWidth = 2f,
+ cap = StrokeCap.Round
+ )
+ }
+ last = proj.screen
+ lastDepth = proj.depth01
+ }
+ }
+}
+
+private data class OffsetTriple(val x: Float, val y: Float, val z: Float)
+
+/** Cerebellum lobes plus tapering brainstem, turning with the same viewport. */
+private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawHindbrain(map: GraphMapper) {
+ val shellColor = Color(0xFF7FB3D5)
+
+ fun blobPath(points: List<OffsetTriple>): Path? {
+ val hull = convexHull(points.map { map.project3(it.x, it.y, it.z).screen })
+ if (hull.size < 3) return null
+ return Path().apply {
+ moveTo(hull[0].x, hull[0].y)
+ hull.drop(1).forEach { lineTo(it.x, it.y) }
+ close()
+ }
+ }
+ fun sampleSphere(
+ center: OffsetTriple,
+ radius: Float,
+ lonSteps: Int = 16,
+ latSteps: Int = 5
+ ): List<OffsetTriple> {
+ val out = mutableListOf<OffsetTriple>()
+ for (li in 0 until lonSteps) {
+ val lon = 2f * Math.PI.toFloat() * li / lonSteps
+ for (si in 0..latSteps) {
+ val lat = -1.2f + 2.4f * si / latSteps
+ out.add(
+ OffsetTriple(
+ center.x + radius * cos(lat) * cos(lon),
+ center.y + radius * sin(lat),
+ center.z + radius * cos(lat) * sin(lon)
+ )
+ )
+ }
+ }
+ return out
+ }
+ // Cerebellum: two small lobes tucked under the back-bottom, with folia stripes.
+ listOf(OffsetTriple(-150f, 330f, -140f), OffsetTriple(150f, 330f, -140f)).forEach { lobe ->
+ blobPath(sampleSphere(lobe, 120f))?.let { path ->
+ drawPath(path = path, color = shellColor.copy(alpha = 0.12f))
+ drawPath(path = path, color = shellColor.copy(alpha = 0.4f), style = Stroke(width = 2f))
+ }
+ for (stripe in -1..1) {
+ var striped: Offset? = null
+ for (i in 0..16) {
+ val a = 2f * Math.PI.toFloat() * i / 16f
+ val p = map.project3(
+ lobe.x + 115f * cos(a),
+ lobe.y + stripe * 38f,
+ lobe.z + 115f * sin(a)
+ ).screen
+ striped?.let {
+ drawLine(color = shellColor.copy(alpha = 0.25f), start = it, end = p, strokeWidth = 1.5f)
+ }
+ striped = p
+ }
+ }
+ }
+ // Brainstem: tapering stalk down the back-center.
+ listOf(
+ OffsetTriple(0f, 400f, -110f) to 62f,
+ OffsetTriple(0f, 460f, -90f) to 44f
+ ).forEach { (center, radius) ->
+ blobPath(sampleSphere(center, radius, 12, 4))?.let { path ->
+ drawPath(path = path, color = shellColor.copy(alpha = 0.12f))
+ drawPath(path = path, color = shellColor.copy(alpha = 0.4f), style = Stroke(width = 2f))
+ }
+ }
+}
+
+private fun quad1(p0: Float, c: Float, p1: Float, t: Float): Float {
+ val u = 1f - t
+ return u * u * p0 + 2f * u * t * c + t * t * p1
+}
+
+/** Monotone-chain convex hull for the shell silhouette. */
+private fun convexHull(points: List<Offset>): List<Offset> {
+ val sorted = points.sortedWith(compareBy({ it.x }, { it.y })).distinct()
+ if (sorted.size < 3) return sorted
+ fun cross(o: Offset, a: Offset, b: Offset): Float =
+ (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
+ val lower = mutableListOf<Offset>()
+ sorted.forEach { p ->
+ while (lower.size >= 2 && cross(lower[lower.size - 2], lower.last(), p) <= 0f) lower.removeLast()
+ lower.add(p)
+ }
+ val upper = mutableListOf<Offset>()
+ sorted.asReversed().forEach { p ->
+ while (upper.size >= 2 && cross(upper[upper.size - 2], upper.last(), p) <= 0f) upper.removeLast()
+ upper.add(p)
+ }
+ lower.removeLast()
+ upper.removeLast()
+ return lower + upper
+}
+
+private fun findBrainFile(snapshot: MemoryStore.SecondBrainSnapshot, id: String): MemoryStore.BrainFile? {
+ val all = snapshot.allMarkdown() + snapshot.raw
+ return all.find { file ->
+ val abs = file.absolutePath
+ if (abs.isNotBlank()) {
+ val root = runCatching { com.gotcha.data.GotchaStorage.memoryRoot().absolutePath }.getOrNull()
+ if (root != null && abs.startsWith(root)) {
+ return@find abs.removePrefix(root).trimStart('/') == id
+ }
+ }
+ file.name == id
+ }
+}
+
+@Composable
+private fun BrainSection(heading: String, file: MemoryStore.BrainFile, tag: String) {
+ var expanded by remember { mutableStateOf(false) }
+ Column(
+ modifier = Modifier
+ .fillMaxWidth()
+ .testTag("settings_second_brain_section_$tag")
+ ) {
+ Text(
+ text = (if (expanded) "▼ " else "▶ ") + heading,
+ style = MaterialTheme.typography.titleMedium,
+ fontWeight = FontWeight.Bold,
+ modifier = Modifier
+ .fillMaxWidth()
+ .clickable { expanded = !expanded }
+ .padding(vertical = 4.dp)
+ )
+ if (expanded) {
+ BrainFileBody(file = file, tag = tag)
+ }
+ }
+ HorizontalDivider(thickness = 1.dp)
+}
+
+@Composable
+private fun BrainFileList(heading: String, files: List<MemoryStore.BrainFile>, tag: String) {
+ var expanded by remember { mutableStateOf(false) }
+ Column(
+ modifier = Modifier
+ .fillMaxWidth()
+ .testTag("settings_second_brain_list_$tag")
+ ) {
+ Text(
+ text = (if (expanded) "▼ " else "▶ ") + "$heading (${files.size})",
+ style = MaterialTheme.typography.titleMedium,
+ fontWeight = FontWeight.Bold,
+ modifier = Modifier
+ .fillMaxWidth()
+ .clickable { expanded = !expanded }
+ .padding(vertical = 4.dp)
+ )
+ if (expanded) {
+ if (files.isEmpty()) {
+ Text(
+ text = stringResource(R.string.second_brain_empty),
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant
+ )
+ } else {
+ files.forEachIndexed { index, file ->
+ var fileExpanded by remember { mutableStateOf(false) }
+ Text(
+ text = (if (fileExpanded) "▼ " else "▶ ") + file.name,
+ style = MaterialTheme.typography.bodyMedium,
+ fontWeight = FontWeight.Medium,
+ modifier = Modifier
+ .fillMaxWidth()
+ .clickable { fileExpanded = !fileExpanded }
+ .padding(vertical = 2.dp)
+ .testTag("settings_second_brain_${tag}_$index")
+ )
+ if (fileExpanded) {
+ BrainFileBody(file = file, tag = "${tag}_$index")
+ }
+ }
+ }
+ }
+ }
+ HorizontalDivider(thickness = 1.dp)
+}
+
+/**
+ * Per-file preview: Markdown renders as formatted text, text files as plain
+ * text, and media/binary files (audio, images, video, documents) show file
+ * info with an Open button that hands them to a system viewer.
+ */
+@Composable
+private fun BrainFileBody(file: MemoryStore.BrainFile, tag: String) {
+ Column(modifier = Modifier.fillMaxWidth()) {
+ if (file.isMarkdown && file.content.isNotBlank()) {
+ SelectionContainer {
+ RichMarkdown(
+ markdown = file.content,
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(start = 8.dp, top = 4.dp, bottom = 4.dp)
+ .testTag("settings_second_brain_body_$tag")
+ )
+ }
+ } else if (file.content.isNotBlank()) {
+ SelectionContainer {
+ Text(
+ text = file.content,
+ style = MaterialTheme.typography.bodySmall.copy(fontFamily = FontFamily.Monospace),
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(start = 8.dp, top = 4.dp, bottom = 4.dp)
+ .testTag("settings_second_brain_body_$tag")
+ )
+ }
+ } else {
+ BrainBinaryRow(file = file, tag = tag)
+ }
+ if (file.truncated) {
+ Text(
+ text = stringResource(R.string.second_brain_truncated),
+ style = MaterialTheme.typography.labelSmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant
+ )
+ }
+ }
+}
+
+@Composable
+private fun BrainBinaryRow(file: MemoryStore.BrainFile, tag: String) {
+ val context = LocalContext.current
+ Row(
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(start = 8.dp, top = 4.dp, bottom = 4.dp)
+ .testTag("settings_second_brain_binary_$tag"),
+ horizontalArrangement = Arrangement.SpaceBetween
+ ) {
+ Column(modifier = Modifier.weight(1f)) {
+ Text(text = file.name, style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Medium)
+ Text(
+ text = "${file.extension.uppercase()} · ${FileResolver.formatSizeStatic(file.sizeBytes)}",
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant
+ )
+ }
+ androidx.compose.material3.TextButton(
+ onClick = {
+ val opened = runCatching {
+ val target = File(file.absolutePath)
+ val uri = FileProvider.getUriForFile(
+ context,
+ "${context.packageName}.fileprovider",
+ target
+ )
+ val mime = MimeTypeMap.getSingleton()
+ .getMimeTypeFromExtension(file.extension) ?: "*/*"
+ val intent = Intent(Intent.ACTION_VIEW).apply {
+ setDataAndType(uri, mime)
+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ }
+ context.startActivity(intent)
+ true
+ }.getOrDefault(false)
+ if (!opened) {
+ android.widget.Toast.makeText(
+ context,
+ context.getString(R.string.second_brain_open_failed),
+ android.widget.Toast.LENGTH_SHORT
+ ).show()
+ }
+ }
+ ) {
+ Text(stringResource(R.string.second_brain_open))
+ }
+ }
+}
diff --git a/app/src/main/java/com/gotcha/ui/SettingsCommon.kt b/app/src/main/java/com/gotcha/ui/SettingsCommon.kt
index 05a4f44..d5c8729 100644
--- a/app/src/main/java/com/gotcha/ui/SettingsCommon.kt
+++ b/app/src/main/java/com/gotcha/ui/SettingsCommon.kt
@@ -127,6 +127,11 @@ enum class SettingsPage(
R.string.settings_page_personal_info_summary,
"settings_personal_info_row"
),
+ SECOND_BRAIN(
+ R.string.settings_page_second_brain,
+ R.string.settings_page_second_brain_summary,
+ "settings_second_brain_row"
+ ),
LANGUAGE(
R.string.settings_page_language,
R.string.settings_page_language_summary,
diff --git a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt b/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
index 45fa2bc..40174ef 100644
--- a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
@@ -204,6 +204,9 @@ fun SettingsScreen(
onBack = backToHome,
onOpenLanguage = { onPageChange(SettingsPage.LANGUAGE) }
)
+ SettingsPage.SECOND_BRAIN -> SecondBrainScreen(
+ onBack = backToHome
+ )
SettingsPage.LANGUAGE -> LanguageScreen(
load = load,
onSave = onSave,
diff --git a/app/src/main/java/com/gotcha/ui/SettingsSearchEntry.kt b/app/src/main/java/com/gotcha/ui/SettingsSearchEntry.kt
index c6abebf..cc25f76 100644
--- a/app/src/main/java/com/gotcha/ui/SettingsSearchEntry.kt
+++ b/app/src/main/java/com/gotcha/ui/SettingsSearchEntry.kt
@@ -134,6 +134,10 @@ private fun entryFor(page: SettingsPage): SettingsSearchEntry = when (page) {
field("settings_user_currency", R.string.settings_field_user_currency)
)
)
+ SettingsPage.SECOND_BRAIN -> SettingsSearchEntry(
+ page,
+ words("memory, second brain, soul, remember, persona, notes, user card")
+ )
SettingsPage.LANGUAGE -> SettingsSearchEntry(
page,
words("translate, locale"),
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 17e39f2..ad9d6b4 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -68,6 +68,27 @@
<!-- Settings pages: title and the summary under it on the home list -->
<string name="settings_page_personal_info">Personal Info</string>
<string name="settings_page_personal_info_summary">Who you are, currency, reply style</string>
+ <string name="settings_page_second_brain">Second Brain</string>
+ <string name="settings_page_second_brain_summary">Gotcha\'s memory: identity, user card, notes</string>
+ <string name="second_brain_intro">What Gotcha remembers about you. Identity first, then the map, then detail. Editing happens through the agent for now.</string>
+ <string name="second_brain_loading">Loading memory…</string>
+ <string name="second_brain_unavailable">Memory is not available yet. Allow All files access under Settings → Permissions, then open a chat and send one message.</string>
+ <string name="second_brain_identity">Identity (SOUL.md)</string>
+ <string name="second_brain_map">Map (MEMORY.md)</string>
+ <string name="second_brain_user_card">User card</string>
+ <string name="second_brain_areas">Areas</string>
+ <string name="second_brain_people">People</string>
+ <string name="second_brain_notes">Notes</string>
+ <string name="second_brain_raw">Raw files</string>
+ <string name="second_brain_empty">Nothing here yet.</string>
+ <string name="second_brain_truncated">Long file — showing the start only.</string>
+ <string name="second_brain_open">Open</string>
+ <string name="second_brain_open_failed">No app on this phone can open that file type.</string>
+ <string name="second_brain_tab_list">List</string>
+ <string name="second_brain_tab_graph">Graph</string>
+ <string name="second_brain_graph_hint">Drag to slide, two fingers to zoom and rotate, double-tap and drag to spin the brain. Tap a neuron to focus its connections, double-tap to reset.</string>
+ <string name="second_brain_graph_reset">Reset view</string>
+ <string name="second_brain_graph_neighbors"> · %1$d connected</string>
<string name="settings_page_language">Language</string>
<string name="settings_page_language_summary">App display, voice, transcription and AI reply language</string>
<string name="settings_page_ai">AI</string>
diff --git a/app/src/test/java/com/gotcha/data/MemoryStoreTest.kt b/app/src/test/java/com/gotcha/data/MemoryStoreTest.kt
index 4bfdbaa..07f9a4c 100644
--- a/app/src/test/java/com/gotcha/data/MemoryStoreTest.kt
+++ b/app/src/test/java/com/gotcha/data/MemoryStoreTest.kt
@@ -3,6 +3,7 @@ package com.gotcha.data
import com.gotcha.llm.ChatMessage
import kotlinx.serialization.json.JsonPrimitive
import org.junit.After
+import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
@@ -109,4 +110,148 @@ class MemoryStoreTest {
assertTrue(pointer.contains("if that message is absent"))
assertTrue(pointer.contains("ABSOLUTE paths"))
}
+
+ @Test
+ fun `snapshot loads seeded files`() {
+ MemoryStore.ensureDefaults(Settings(userName = "Ada"))
+ val snap = MemoryStore.loadSnapshot()
+ assertTrue(snap.available)
+ assertNotNull(snap.soul)
+ assertNotNull(snap.memoryIndex)
+ assertTrue(snap.userCard!!.content.contains("Ada"))
+ assertTrue(snap.areas.map { it.name }.contains("work.md"))
+ }
+
+ @Test
+ fun `snapshot unavailable when files missing`() {
+ val snap = MemoryStore.loadSnapshot()
+ assertFalse(snap.available)
+ assertNull(snap.soul)
+ assertTrue(snap.areas.isEmpty())
+ }
+
+ @Test
+ fun `wiki links parse with labels and dedupe`() {
+ val links = MemoryStore.parseWikiLinks(
+ "See [[notes/trip.md]] and [[people/asha.md| Asha ]] and [[notes/trip.md]]."
+ )
+ assertEquals(listOf("notes/trip.md", "people/asha.md"), links)
+ }
+
+ @Test
+ fun `graph resolves links and skips dangling ones`() {
+ MemoryStore.ensureDefaults(Settings())
+ GotchaStorage.memoryNotesDir().mkdirs()
+ File(GotchaStorage.memoryNotesDir(), "trip.md").writeText("Trip. See [[work]] and [[nowhere]].")
+ val snap = MemoryStore.loadSnapshot()
+ val graph = MemoryStore.buildGraph(snap)
+ assertTrue(graph.nodes.map { it.id }.contains("notes/trip.md"))
+ val edge = graph.edges.singleOrNull { it.fromId == "notes/trip.md" }
+ assertNotNull(edge)
+ assertEquals("areas/work.md", edge!!.toId)
+ }
+
+ @Test
+ fun `graph keeps same-named files apart`() {
+ MemoryStore.ensureDefaults(Settings())
+ GotchaStorage.memoryNotesDir().mkdirs()
+ GotchaStorage.memoryPeopleDir().mkdirs()
+ File(GotchaStorage.memoryNotesDir(), "plan.md").writeText("Plan. See [[people/plan]].")
+ File(GotchaStorage.memoryPeopleDir(), "plan.md").writeText("Person.")
+ val snap = MemoryStore.loadSnapshot()
+ val graph = MemoryStore.buildGraph(snap)
+ assertTrue(graph.nodes.map { it.id }.contains("notes/plan.md"))
+ assertTrue(graph.nodes.map { it.id }.contains("people/plan.md"))
+ val edge = graph.edges.singleOrNull { it.fromId == "notes/plan.md" }
+ assertNotNull(edge)
+ assertEquals("people/plan.md", edge!!.toId)
+ }
+
+ @Test
+ fun `graph sees links past the preview cap`() {
+ MemoryStore.ensureDefaults(Settings())
+ GotchaStorage.memoryNotesDir().mkdirs()
+ val filler = "x".repeat(MemoryStore.VIEWER_FILE_CAP_CHARS + 100)
+ File(GotchaStorage.memoryNotesDir(), "long.md").writeText("$filler [[work]]")
+ val snap = MemoryStore.loadSnapshot()
+ assertTrue(snap.notes.single { it.name == "long.md" }.truncated)
+ val graph = MemoryStore.buildGraph(snap)
+ assertTrue(graph.edges.any { it.fromId == "notes/long.md" && it.toId == "areas/work.md" })
+ }
+
+ @Test
+ fun `markdown extension renders as markdown and links`() {
+ MemoryStore.ensureDefaults(Settings())
+ val rawDir = GotchaStorage.memoryRawDir()
+ rawDir.mkdirs()
+ File(rawDir, "ref.markdown").writeText("Ref. See [[work]].")
+ val snap = MemoryStore.loadSnapshot()
+ val ref = snap.raw.single { it.name == "ref.markdown" }
+ assertTrue(ref.isMarkdown)
+ val graph = MemoryStore.buildGraph(snap)
+ assertTrue(graph.edges.any { it.fromId == "raw/ref.markdown" && it.toId == "areas/work.md" })
+ }
+
+ @Test
+ fun `raw files list any extension without content`() {
+ MemoryStore.ensureDefaults(Settings())
+ val rawDir = GotchaStorage.memoryRawDir()
+ rawDir.mkdirs()
+ File(rawDir, "memo.mp3").writeBytes(byteArrayOf(1, 2, 3, 4))
+ File(rawDir, "note.txt").writeText("hello raw")
+ val snap = MemoryStore.loadSnapshot()
+ val mp3 = snap.raw.single { it.name == "memo.mp3" }
+ assertEquals("", mp3.content)
+ assertEquals(4L, mp3.sizeBytes)
+ assertTrue(snap.raw.single { it.name == "note.txt" }.content.contains("hello"))
+ }
+
+ @Test
+ fun `force layout is deterministic and bounded`() {
+ val edges = listOf(0 to 1, 1 to 2, 2 to 0, 2 to 3)
+ val first = MemoryStore.forceLayout(4, edges)
+ val second = MemoryStore.forceLayout(4, edges)
+ assertEquals(4, first.size)
+ assertEquals(first, second)
+ first.forEach { p ->
+ assertTrue(p.x in -500f..500f)
+ assertTrue(p.y in -350f..350f)
+ }
+ }
+
+ @Test
+ fun `force layout pulls linked nodes closer than unlinked ones`() {
+ val chain = MemoryStore.forceLayout(5, listOf(0 to 1, 1 to 2, 2 to 3, 3 to 4), iterations = 300)
+ fun dist(a: Int, b: Int): Float = (chain[a] - chain[b]).getDistance()
+ val linked = listOf(dist(0, 1), dist(1, 2), dist(2, 3), dist(3, 4)).average()
+ val unlinked = listOf(dist(0, 2), dist(0, 3), dist(0, 4), dist(1, 3), dist(1, 4), dist(2, 4)).average()
+ assertTrue("linked=$linked unlinked=$unlinked", linked < unlinked)
+ }
+
+ @Test
+ fun `force layout handles empty and single node`() {
+ assertTrue(MemoryStore.forceLayout(0, emptyList()).isEmpty())
+ assertEquals(1, MemoryStore.forceLayout(1, emptyList()).size)
+ }
+
+ @Test
+ fun `confinement keeps every point inside the brain`() {
+ val points = listOf(
+ androidx.compose.ui.geometry.Offset(900f, 700f),
+ androidx.compose.ui.geometry.Offset(-50f, 20f),
+ androidx.compose.ui.geometry.Offset(0f, 0f)
+ )
+ val depths = listOf(500f, 10f, 0f)
+ val (keptPoints, keptDepths) = MemoryStore.confineToBrain(points, depths)
+ assertEquals(3, keptPoints.size)
+ keptPoints.zip(keptDepths).forEach { (p, z) ->
+ val e = (p.x / MemoryStore.BRAIN_RX).let { it * it } +
+ (p.y / MemoryStore.BRAIN_RY).let { it * it } +
+ (z / MemoryStore.BRAIN_RZ).let { it * it }
+ assertTrue("e=$e", e <= 0.81f + 1e-3f)
+ }
+ // Inside points pass through untouched.
+ assertEquals(points[1], keptPoints[1])
+ assertEquals(points[2], keptPoints[2])
+ }
}