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: prevent URL overflow in feedback submissions by dynamically sizing chat logs based on encoded character budget
Rishabh Bajpaie78303279d71
diff --git a/app/src/main/java/com/gotcha/data/FeedbackChannel.kt b/app/src/main/java/com/gotcha/data/FeedbackChannel.ktindex 576628b..ec1c579 100644--- a/app/src/main/java/com/gotcha/data/FeedbackChannel.kt+++ b/app/src/main/java/com/gotcha/data/FeedbackChannel.kt@@ -53,6 +53,13 @@ object FeedbackChannel {* Builds the pre-filled form URL. [formUrl] and [entries] default to the* build-time configuration but are parameterized so unit tests can exercise* the URL building without depending on local config.+ *+ * The chat log is the only unbounded field, and Google Forms rejects+ * prefilled URLs past roughly 8.2k total characters with a 400. It is+ * therefore budgeted by *encoded* length (Devanagari/emoji expand 9-12x when+ * percent-encoded) into the remaining room under [MAX_PREFILL_URL_LEN], and+ * dropped entirely when there is no room — the consent flow can never+ * produce a 400.*/fun buildFeedbackUrl(prefill: FeedbackPrefill,@@ -73,11 +80,70 @@ object FeedbackChannel {put(entries.androidVersion, prefill.androidVersion)put(entries.userId, prefill.userId)put(entries.usageStats, prefill.usageStats)- put(entries.chatLog, prefill.chatLog)++ val chatLog = prefill.chatLog+ if (entries.chatLog.isNotBlank() && !chatLog.isNullOrBlank()) {+ val overhead = base.length + "?usp=pp_url&".length ++ params.joinToString("&").length ++ (if (params.isNotEmpty()) "&".length else 0) ++ entries.chatLog.length + "=".length+ val budget = MAX_PREFILL_URL_LEN - overhead+ if (budget > 0) {+ val fitted = truncateToEncodedBudget(chatLog, budget)+ if (fitted.isNotBlank()) {+ params += "${entries.chatLog}=${encodeQueryValue(fitted)}"+ }+ }+ }return if (params.isEmpty()) base else "$base?usp=pp_url&${params.joinToString("&")}"}+ /**+ * The largest head+tail slice of [text] (via [truncateMiddle]) whose+ * percent-encoded form is at most [maxEncodedChars]. Binary-searched because+ * encoding expansion is non-linear across scripts. Returns the original text+ * when it already fits; empty when not even a snippet fits.+ */+ private fun truncateToEncodedBudget(text: String, maxEncodedChars: Int): String {+ if (text.isEmpty() || maxEncodedChars <= 0) return ""+ if (encodedLength(text) <= maxEncodedChars) return text+ var lo = 1+ var hi = text.length+ var best = ""+ while (lo <= hi) {+ val mid = (lo + hi) / 2+ val candidate = truncateMiddle(text, mid)+ if (encodedLength(candidate) <= maxEncodedChars) {+ best = candidate+ lo = mid + 1+ } else {+ hi = mid - 1+ }+ }+ return best+ }++ /**+ * Encoded query-string length of [value], matching [encodeQueryValue]+ * without building the encoded string. Counting instead of allocating keeps+ * the binary search in [truncateToEncodedBudget] allocation-free.+ */+ private fun encodedLength(value: String): Int {+ var len = 0+ for (b in value.toByteArray(Charsets.UTF_8)) {+ len += if (isUnreserved(b.toInt() and 0xFF)) 1 else 3+ }+ return len+ }++ /** True when [c] is an RFC 3986 unreserved byte (kept verbatim when encoded). */+ private fun isUnreserved(c: Int): Boolean =+ c in 'a'.code..'z'.code ||+ c in 'A'.code..'Z'.code ||+ c in '0'.code..'9'.code ||+ c == '-'.code || c == '.'.code || c == '_'.code || c == '~'.code+/*** Percent-encodes a value for a query string, leaving only the RFC 3986* unreserved characters intact (so spaces become %20, newlines %0A, etc.).@@ -88,11 +154,7 @@ object FeedbackChannel {return buildString {for (b in value.toByteArray(Charsets.UTF_8)) {val c = b.toInt() and 0xFF- val unreserved = c in 'a'.code..'z'.code ||- c in 'A'.code..'Z'.code ||- c in '0'.code..'9'.code ||- c == '-'.code || c == '.'.code || c == '_'.code || c == '~'.code- if (unreserved) {+ if (isUnreserved(c)) {append(c.toChar())} else {append('%')@@ -149,18 +211,41 @@ object FeedbackChannel {"$label: $text"}}.trim()- if (full.length <= maxChars) return full+ return truncateMiddle(full, maxChars)+ }++ /** Head + `… truncated …` + tail of [text], totalling at most [maxChars] characters. */+ private fun truncateMiddle(text: String, maxChars: Int): String {+ if (text.length <= maxChars) return textval marker = "\n… truncated …\n"+ if (maxChars <= marker.length) return text.take(maxChars)val head = (maxChars - marker.length) * 3 / 4val tail = maxChars - marker.length - head- return full.take(head) + marker + full.takeLast(tail)+ return text.take(head) + marker + text.takeLast(tail)}+ /** Session start date (last modified), local time, `yyyy-MM-dd HH:mm:ss`. */+ private fun formatSessionDate(epochMillis: Long): String =+ java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.US)+ .format(java.util.Date(epochMillis))++ /**+ * One-line session context — session id, start date and message count —+ * prepended to the chat excerpt so a bug report carries *when* the chat+ * started and how much ground it covered, not just the tail.+ */+ internal fun sessionHeader(session: ChatSession): String =+ "Session: ${session.id}\n" ++ "Started: ${formatSessionDate(session.lastModified)}\n" ++ "Messages: ${session.messages.size}\n\n"+/** Excerpt from the most recently modified chat session, or null if none. */suspend fun recentChatExcerpt(context: Context, maxChars: Int = DEFAULT_EXCERPT_CHARS): String? =withContext(Dispatchers.IO) {val newest = ChatHistoryRepository(context).listSessions().firstOrNull()- newest?.let { chatLogExcerpt(it.messages, maxChars) }+ newest?.let { session ->+ sessionHeader(session) + chatLogExcerpt(session.messages, maxChars)+ }}/** The form's `entry.<id>` query keys, one per pre-filled field. */@@ -173,5 +258,13 @@ object FeedbackChannel {val chatLog: String)- const val DEFAULT_EXCERPT_CHARS = 3500+ /**+ * Hard ceiling for the whole prefilled URL, measured against Google's live+ * edge: values that pushed the total past ~8.2k characters came back 400+ * "Bad Request" before the form even loaded. 8000 leaves a safety margin.+ */+ const val MAX_PREFILL_URL_LEN = 8000++ /** Character budget for the chat excerpt before encoded-length fitting. */+ const val DEFAULT_EXCERPT_CHARS = 8000}diff --git a/app/src/main/java/com/gotcha/ui/FeedbackSheet.kt b/app/src/main/java/com/gotcha/ui/FeedbackSheet.ktindex dfe1849..c616ba5 100644--- a/app/src/main/java/com/gotcha/ui/FeedbackSheet.kt+++ b/app/src/main/java/com/gotcha/ui/FeedbackSheet.kt@@ -46,7 +46,7 @@ fun FeedbackSheet(FeedbackToggle("App info (version, device, Android)", includeAppInfo) { includeAppInfo = it }FeedbackToggle("Usage stats (chats, tool calls, runs)", includeUsageStats) { includeUsageStats = it }FeedbackToggle(- "Chat log excerpt (last ~3.5 KB of recent chat)",+ "Chat log excerpt (recent chat, sized to fit the form)",includeChatLog) { includeChatLog = it }FeedbackToggle("User ID (for cross-checking your feedback)", includeUserId) { includeUserId = it }diff --git a/app/src/test/java/com/gotcha/data/FeedbackChannelTest.kt b/app/src/test/java/com/gotcha/data/FeedbackChannelTest.ktindex 4dbade2..e7439e2 100644--- a/app/src/test/java/com/gotcha/data/FeedbackChannelTest.kt+++ b/app/src/test/java/com/gotcha/data/FeedbackChannelTest.kt@@ -63,6 +63,87 @@ class FeedbackChannelTest {assertTrue(url, url.contains("entry.6=line%20one%0Atwo%20%26%20three"))}+ @Test+ fun `short chat log is included verbatim`() {+ val url = FeedbackChannel.buildFeedbackUrl(+ FeedbackPrefill(chatLog = "hello world"),+ formUrl,+ entries+ )++ assertTrue(url, url.contains("entry.6=hello%20world"))+ }++ @Test+ fun `oversized unicode chat log is fitted so the url stays under the cap`() {+ // Devanagari + emoji expand 9-12x when percent-encoded; a 12k-char log+ // would otherwise blow well past Google's prefilled-URL ceiling.+ val log = buildString {+ repeat(3000) {+ append("यह एक परीक्षण संदेश है नोशन मेरी पेजें नहीं खुल रही हैं 😊🚀\n")+ }+ }+ assertTrue(log.length > FeedbackChannel.DEFAULT_EXCERPT_CHARS)++ val url = FeedbackChannel.buildFeedbackUrl(+ FeedbackPrefill(+ appVersion = "0.1.0",+ deviceModel = "Pixel",+ androidVersion = "Android 16",+ userId = "user-123",+ usageStats = "Runs: 13",+ chatLog = log+ ),+ formUrl,+ entries+ )++ assertTrue(url, url.length <= FeedbackChannel.MAX_PREFILL_URL_LEN)+ assertTrue(url, url.contains("entry.6="))+ // The head of the log must survive the encoded-length fit.+ assertTrue(url, url.contains("%E0%A4%AF%E0%A4%B9"))+ }++ @Test+ fun `chat log is dropped when it cannot fit even a snippet`() {+ // A form URL already at the cap leaves no budget for the chat log.+ val longBase = "https://forms.example.com/" + "x".repeat(8000)+ val url = FeedbackChannel.buildFeedbackUrl(+ FeedbackPrefill(chatLog = "should be dropped"),+ longBase,+ entries+ )++ assertFalse(url, url.contains("entry.6="))+ }++ @Test+ fun `excerpt stays within maxChars even below the marker length`() {+ // maxChars < marker length used to drive head/tail negative; the guard+ // must fall back to a plain prefix instead of emitting a longer string.+ val messages = (1..200).map { ChatMessage(role = "user", content = JsonPrimitive("message $it")) }+ val excerpt = FeedbackChannel.chatLogExcerpt(messages, maxChars = 3)++ assertTrue(excerpt, excerpt.length <= 3)+ assertFalse(excerpt, excerpt.contains("truncated"))+ }++ @Test+ fun `session header includes id start date and message count`() {+ val session = ChatSession(+ id = "abc123",+ title = "Test",+ lastModified = 1_700_000_000_000L,+ messages = listOf(ChatMessage(role = "user", content = JsonPrimitive("hi")))+ )++ val header = FeedbackChannel.sessionHeader(session)++ assertTrue(header, header.contains("Session: abc123"))+ assertTrue(header, header.contains("Started: "))+ assertTrue(header, header.contains("Messages: 1"))+ }+@Testfun `chat excerpt truncates from the middle within the char cap`() {val messages = (1..200).map { ChatMessage(role = "user", content = JsonPrimitive("message $it")) }