Skip to content

armgit / andee

Public Imported from GitHub
0

Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。

fix(brain): changing model, thinking or effort no longer wipes the conversation

booboosui5bea40b2f33a
diff --git a/CLAUDE.md b/CLAUDE.md
index 81de7c1..d6adb63 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -510,7 +510,7 @@ It also has to sit *here* rather than only in Settings: this is where a user lan
### Configuration
-All settings live in one `SharedPreferences` file (`voice_prefs`), read/written through `VoiceConfig`. Three entry points: `VoiceConfig.load()` for the voice pipeline, `VoiceConfig.hubConfig()` for networking (blank URL means "don't dial out"), and `VoiceConfig.brainConfig()` for the local brain (`brain`, `llm_api_key`, `llm_base_url`, `llm_model`, `llm_thinking`, `llm_reasoning_effort`); none of them throws. `device_id` and `uid` are lazily generated once and persisted. Writes go through `save()`, which silently ignores keys not in `ALLOWED_KEYS` — add new keys there or they vanish. Changing anything in either config in `SettingsUi` restarts the brain on dismiss without bouncing the service.
+All settings live in one `SharedPreferences` file (`voice_prefs`), read/written through `VoiceConfig`. Three entry points: `VoiceConfig.load()` for the voice pipeline, `VoiceConfig.hubConfig()` for networking (blank URL means "don't dial out"), and `VoiceConfig.brainConfig()` for the local brain (`brain`, `llm_api_key`, `llm_base_url`, `llm_model`, `llm_thinking`, `llm_reasoning_effort`); none of them throws. `device_id` and `uid` are lazily generated once and persisted. Writes go through `save()`, which silently ignores keys not in `ALLOWED_KEYS` — add new keys there or they vanish. Changing the hub URL, device name or **who answers** (`brain` mode, `llm_api_key`, `llm_base_url`) in `SettingsUi` restarts the brain on dismiss without bouncing the service. **Changing only how the same backend is asked — `llm_model`, `llm_thinking`, `llm_reasoning_effort` — does not**: `BrainConfig.sameBackend` says which kind of change it was, and the second kind goes through `LocalBrain.retune` → `LlmClient.retune` and takes effect on the next request. The distinction is load-bearing because a restart *is* the loss of the conversation: with the old "any difference restarts" rule, flipping the thinking switch wiped ten minutes of context (measured in `brain_trace.jsonl`: `gen` restarts at 1, first-step prompt back to ~27.7k tokens, `recall` empty), and the model then reported having no memory of a page it had just built. The notebook was no help because nothing in those ten minutes had been `remember`ed.
`llm_api_key` **is** a preference, unlike the two constants below and deliberately so: the DeepSeek key is the user's own and gets rotated, so it has to be replaceable without a rebuild. Note the standing limitation this shares with every other *typed* field: `SettingsUi.saveAndHide()` and `save()` both skip blank values, so a field can be overwritten but never cleared. That is why the brain is an explicit `brain=hub|local` setting rather than "blank `hub_url` means go local" — the latter would have had no way back.
diff --git a/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt b/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt
index eaea2ae..3c1d768 100644
--- a/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt
+++ b/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt
@@ -1835,14 +1835,21 @@ class BodyCoreService : Service() {
val after = net.kuafuai.andee.config.VoiceConfig.hubConfig(this)
val afterBrain = net.kuafuai.andee.config.VoiceConfig.brainConfig(this)
if (before.url != after.url || before.deviceName != after.deviceName ||
- beforeBrain != afterBrain
+ !beforeBrain.sameBackend(afterBrain)
) {
android.util.Log.i("Body", "brain/hub config changed → restart brain")
// Restarting the local brain discards the conversation,
- // which is the right trade: the user just changed the
- // model, the key or the endpoint out from under it.
+ // which is the right trade only when the user changed
+ // *who* answers: the key, the endpoint or the mode.
stopBrain()
startBrain()
+ } else if (beforeBrain != afterBrain) {
+ // Model, thinking or effort: the same backend, asked
+ // differently on the next request. Rebuilding for this
+ // used to wipe a conversation every time the thinking
+ // switch was touched.
+ android.util.Log.i("Body", "brain tuned in place: model=${afterBrain.model} thinking=${afterBrain.thinking}/${afterBrain.reasoningEffort}")
+ localBrain?.retune(afterBrain)
}
}
// The self-check card can be sitting *under* this sheet: its
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt b/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
index 809cb99..0ac6e4b 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
@@ -58,11 +58,30 @@ class LlmHttpException(val code: Int, val body: String) :
class LlmClient(
private val baseUrl: String,
private val apiKey: String,
- private val model: String,
- private val thinking: Boolean,
- private val reasoningEffort: String,
+ model: String,
+ thinking: Boolean,
+ reasoningEffort: String,
) {
+ // Request parameters, read once per [complete]. Volatile because [retune]
+ // lands from the main thread while the loop thread may be mid-turn; the turn
+ // in flight keeps what it already sent and the next request carries the new.
+ @Volatile private var model = model
+ @Volatile private var thinking = thinking
+ @Volatile private var reasoningEffort = reasoningEffort
+
+ /**
+ * Change what the next request asks for, without touching the endpoint or
+ * the key. Those two are the identity of the backend; these three are only
+ * how it is asked, so a settings change to them has no reason to cost the
+ * user their conversation.
+ */
+ fun retune(model: String, thinking: Boolean, reasoningEffort: String) {
+ this.model = model
+ this.thinking = thinking
+ this.reasoningEffort = reasoningEffort
+ }
+
/**
* Read timeout is generous because a thinking model answers in minutes, not
* seconds, and a premature timeout here reads to the user as the ball going
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
index b8add97..99e0fbb 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
@@ -104,6 +104,18 @@ class LocalBrain(
reasoningEffort = cfg.reasoningEffort,
)
+ /**
+ * Apply a settings change that is only about how the backend is asked —
+ * model, thinking, effort — with the conversation intact. The caller has
+ * already established [VoiceConfig.Companion.BrainConfig.sameBackend]; a
+ * different endpoint, key or mode is a different brain and gets rebuilt.
+ * [cfg] itself stays as built: the only things read from it after
+ * construction are the key and the mode, and neither can differ here.
+ */
+ fun retune(next: VoiceConfig.Companion.BrainConfig) {
+ llm.retune(next.model, next.thinking, next.reasoningEffort)
+ }
+
private val loop = Executors.newSingleThreadExecutor { r ->
Thread(r, "LocalBrain-loop").apply { isDaemon = true }
}
diff --git a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
index c51d5c2..c356b56 100644
--- a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
+++ b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
@@ -387,6 +387,17 @@ data class VoiceConfig(
* [BRAIN_HUB] is "run elsewhere, listen on a socket".
*/
val isLocal: Boolean get() = mode == BRAIN_LOCAL || mode == BRAIN_CODEFLYING
+
+ /**
+ * Same backend, possibly asked differently. [mode], [apiKey] and
+ * [baseUrl] say *who* answers; [model], [thinking] and
+ * [reasoningEffort] only say how they are asked, and a running brain
+ * can take those on the next request. A config change that is only
+ * the second kind must not rebuild the brain, because rebuilding it
+ * is what discards the conversation.
+ */
+ fun sameBackend(other: BrainConfig): Boolean =
+ mode == other.mode && apiKey == other.apiKey && baseUrl == other.baseUrl
}
/**
diff --git a/app/src/test/java/net/kuafuai/andee/config/BrainConfigTest.kt b/app/src/test/java/net/kuafuai/andee/config/BrainConfigTest.kt
new file mode 100644
index 0000000..598a7e8
--- /dev/null
+++ b/app/src/test/java/net/kuafuai/andee/config/BrainConfigTest.kt
@@ -0,0 +1,38 @@
+package net.kuafuai.andee.config
+
+import net.kuafuai.andee.config.VoiceConfig.Companion.BrainConfig
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/**
+ * [BrainConfig.sameBackend] decides whether closing the settings card rebuilds
+ * the brain, and a rebuild discards the conversation. Getting it wrong in the
+ * "too eager" direction is silent — the user flips the thinking switch and the
+ * assistant forgets the last ten minutes — which is how it was found.
+ */
+class BrainConfigTest {
+
+ private val base = BrainConfig(
+ mode = VoiceConfig.BRAIN_LOCAL,
+ apiKey = "sk-one",
+ baseUrl = "https://api.example.com/v1",
+ model = "m1",
+ thinking = true,
+ reasoningEffort = "high",
+ )
+
+ @Test
+ fun `how it is asked does not change the backend`() {
+ assertTrue(base.sameBackend(base.copy(thinking = false)))
+ assertTrue(base.sameBackend(base.copy(reasoningEffort = "low")))
+ assertTrue(base.sameBackend(base.copy(model = "m2")))
+ }
+
+ @Test
+ fun `who answers does`() {
+ assertFalse(base.sameBackend(base.copy(apiKey = "sk-two")))
+ assertFalse(base.sameBackend(base.copy(baseUrl = "https://other.example.com/v1")))
+ assertFalse(base.sameBackend(base.copy(mode = VoiceConfig.BRAIN_HUB)))
+ }
+}