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.
fix(settings): lift an existing 70k context budget to the new default
Durganshu Mishra2c6a352ea9d5
diff --git a/app/src/main/java/com/gotcha/data/SettingsRepository.kt b/app/src/main/java/com/gotcha/data/SettingsRepository.ktindex 401642c..8157c54 100644--- a/app/src/main/java/com/gotcha/data/SettingsRepository.kt+++ b/app/src/main/java/com/gotcha/data/SettingsRepository.kt@@ -31,6 +31,36 @@ enum class WakeWordListeningMode {}}+/**+ * Chat context budget, in tokens, for a fresh install.+ */+const val DEFAULT_MAX_CONTEXT_TOKENS = 256_000++/**+ * The budget before [DEFAULT_MAX_CONTEXT_TOKENS] was raised. A stored copy of+ * exactly this value is lifted once by [SettingsRepository.resolvedMaxContextTokens];+ * see the note there for why raising the default alone was not enough.+ */+private const val LEGACY_MAX_CONTEXT_TOKENS = 70_000++/** What the one-shot context-budget lift decided: the value to use, and whether to store it. */+internal data class MaxContextTokensLift(val value: Int, val writeBack: Boolean)++/**+ * Decides whether a stored context budget should be lifted to the current default.+ *+ * Only a stored value equal to the old default is lifted, and only while+ * [alreadyLifted] is false, so a user who deliberately picks 70k afterwards+ * keeps it. Kept pure and separate from the prefs read because+ * `EncryptedSharedPreferences` cannot be built under Robolectric, which would+ * otherwise leave a silent one-shot migration with no test at all.+ */+internal fun liftMaxContextTokens(stored: Int, alreadyLifted: Boolean): MaxContextTokensLift = when {+ alreadyLifted -> MaxContextTokensLift(stored, writeBack = false)+ stored == LEGACY_MAX_CONTEXT_TOKENS -> MaxContextTokensLift(DEFAULT_MAX_CONTEXT_TOKENS, writeBack = true)+ else -> MaxContextTokensLift(stored, writeBack = false)+}+data class Settings(// Which LLM backend is active. Defaults to the Samosa AI flow.val provider: LlmProvider = LlmProvider.SAMOSA_AI,@@ -55,7 +85,7 @@ data class Settings(* each call carries a freshly rephrased task string.*/val maxConsecutiveDelegations: Int = 3,- val maxContextTokens: Int = 256000,+ val maxContextTokens: Int = DEFAULT_MAX_CONTEXT_TOKENS,val apiTimeoutSeconds: Long = 0L,// TTS / STT settingsval ttsProvider: AudioProvider = AudioProvider.ANDROID,@@ -380,6 +410,33 @@ class SettingsRepository(context: Context) : SettingsStore {return migrated}+ /**+ * Reads the context budget, lifting a stored 70k to the current default once.+ *+ * Raising the default in [Settings] only reaches installs that never wrote+ * the key, and an APK update -- from Android Studio or a GitHub release --+ * keeps the app's data directory, so every existing user kept 70k. Anyone+ * who has opened AI Config and pressed Save has the old default written out+ * explicitly, which is indistinguishable from having chosen it.+ *+ * So this runs exactly once, tracked by its own flag rather than by the+ * value: after it has run, a user is free to set 70k back and keep it.+ */+ private fun resolvedMaxContextTokens(): Int {+ val alreadyLifted = prefs.getBoolean(KEY_MAX_CONTEXT_TOKENS_RAISED, false)+ val lift = liftMaxContextTokens(+ stored = prefs.getInt(KEY_MAX_CONTEXT_TOKENS, DEFAULT_MAX_CONTEXT_TOKENS),+ alreadyLifted = alreadyLifted+ )+ if (!alreadyLifted) {+ prefs.edit().apply {+ putBoolean(KEY_MAX_CONTEXT_TOKENS_RAISED, true)+ if (lift.writeBack) putInt(KEY_MAX_CONTEXT_TOKENS, lift.value)+ }.apply()+ }+ return lift.value+ }+override fun load(): Settings = Settings(provider = LlmProvider.fromName(prefs.getString(KEY_PROVIDER, null)),apiKey = string(KEY_API_KEY),@@ -393,7 +450,7 @@ class SettingsRepository(context: Context) : SettingsStore {maxRepeatedToolCalls = prefs.getInt(KEY_MAX_REPEATED_TOOL_CALLS, 20),maxNavigationToolCalls = prefs.getInt(KEY_MAX_NAVIGATION_TOOL_CALLS, 30),maxConsecutiveDelegations = prefs.getInt(KEY_MAX_CONSECUTIVE_DELEGATIONS, 3),- maxContextTokens = prefs.getInt(KEY_MAX_CONTEXT_TOKENS, 256000),+ maxContextTokens = resolvedMaxContextTokens(),apiTimeoutSeconds = prefs.getLong(KEY_API_TIMEOUT, 0L),ttsProvider = runCatching {AudioProvider.valueOf(string(KEY_TTS_PROVIDER, "ANDROID"))@@ -577,6 +634,9 @@ class SettingsRepository(context: Context) : SettingsStore {const val KEY_MAX_NAVIGATION_TOOL_CALLS = "max_navigation_tool_calls"const val KEY_MAX_CONSECUTIVE_DELEGATIONS = "max_consecutive_delegations"const val KEY_MAX_CONTEXT_TOKENS = "max_context_tokens"++ /** Whether the one-shot 70k -> 256k lift has already run. */+ const val KEY_MAX_CONTEXT_TOKENS_RAISED = "max_context_tokens_raised"const val KEY_API_TIMEOUT = "api_timeout"const val KEY_TTS_PROVIDER = "tts_provider"const val KEY_TTS_API_URL = "tts_api_url"diff --git a/app/src/main/java/com/gotcha/ui/AiConfigScreen.kt b/app/src/main/java/com/gotcha/ui/AiConfigScreen.ktindex da1f726..57b0f94 100644--- a/app/src/main/java/com/gotcha/ui/AiConfigScreen.kt+++ b/app/src/main/java/com/gotcha/ui/AiConfigScreen.kt@@ -24,6 +24,7 @@ import androidx.compose.ui.platform.testTagimport androidx.compose.ui.text.input.KeyboardTypeimport androidx.compose.ui.text.input.PasswordVisualTransformationimport androidx.compose.ui.text.input.VisualTransformation+import com.gotcha.data.DEFAULT_MAX_CONTEXT_TOKENSimport com.gotcha.data.LlmProviderimport com.gotcha.data.Settingsimport com.gotcha.ui.theme.SkinExposedDropdownMenu@@ -129,7 +130,7 @@ fun AiConfigScreen(maxRepeatedToolCalls = maxRepeatedToolCalls.toIntOrNull()?.takeIf { it > 0 } ?: 20,maxNavigationToolCalls = maxNavigationToolCalls.toIntOrNull()?.takeIf { it > 0 } ?: 30,maxConsecutiveDelegations = maxConsecutiveDelegations.toIntOrNull()?.takeIf { it > 0 } ?: 3,- maxContextTokens = maxContextTokens.toIntOrNull()?.takeIf { it > 0 } ?: 256000,+ maxContextTokens = maxContextTokens.toIntOrNull()?.takeIf { it > 0 } ?: DEFAULT_MAX_CONTEXT_TOKENS,apiTimeoutSeconds = apiTimeoutSeconds.toLongOrNull()?.takeIf { it >= 0 } ?: 0L)diff --git a/app/src/test/java/com/gotcha/data/MaxContextTokensLiftTest.kt b/app/src/test/java/com/gotcha/data/MaxContextTokensLiftTest.ktnew file mode 100644index 0000000..33a27f0--- /dev/null+++ b/app/src/test/java/com/gotcha/data/MaxContextTokensLiftTest.kt@@ -0,0 +1,68 @@+package com.gotcha.data++import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++/**+ * Raising the default context budget reaches nobody on its own: an APK update+ * keeps the app's data directory, so every existing install reads back the 70k+ * it already stored. This lift is what actually moves them — silently, once,+ * writing its result back. Same reasoning as [SkinMigrationTest]: a mistake+ * here is one nobody reports.+ */+class MaxContextTokensLiftTest {++ @Test+ fun `an install still on the old default is raised and the new value stored`() {+ val lift = liftMaxContextTokens(stored = 70_000, alreadyLifted = false)+ assertEquals(DEFAULT_MAX_CONTEXT_TOKENS, lift.value)+ assertTrue("the raised value has to be persisted, not just returned", lift.writeBack)+ }++ /**+ * A fresh install has never written the key, so the read already returns the+ * current default. Nothing to lift, and nothing worth a write.+ */+ @Test+ fun `a fresh install is left alone`() {+ val lift = liftMaxContextTokens(+ stored = DEFAULT_MAX_CONTEXT_TOKENS,+ alreadyLifted = false+ )+ assertEquals(DEFAULT_MAX_CONTEXT_TOKENS, lift.value)+ assertFalse(lift.writeBack)+ }++ /**+ * The whole point of the one-shot flag. Someone who wants a small context+ * window gets to keep it — the lift must not chase them back up on the next+ * load.+ */+ @Test+ fun `70k chosen deliberately after the lift has run is kept`() {+ val lift = liftMaxContextTokens(stored = 70_000, alreadyLifted = true)+ assertEquals(70_000, lift.value)+ assertFalse(lift.writeBack)+ }++ /**+ * Only the exact old default is treated as "never chose". A tuned value is+ * a decision, and survives even on the run where the lift fires.+ */+ @Test+ fun `a hand-tuned budget is never rewritten`() {+ val lift = liftMaxContextTokens(stored = 32_000, alreadyLifted = false)+ assertEquals(32_000, lift.value)+ assertFalse(lift.writeBack)+ }++ /** A budget above the new default is a decision too, and must not be lowered. */+ @Test+ fun `a budget larger than the new default is left where it is`() {+ val lift = liftMaxContextTokens(stored = 400_000, alreadyLifted = false)+ assertEquals(400_000, lift.value)+ assertFalse(lift.writeBack)+ }+}