Skip to content

Fix Android session never loading for long conversations#214

Merged
PureWeen merged 3 commits intomainfrom
PhoneGrump
Feb 26, 2026
Merged

Fix Android session never loading for long conversations#214
PureWeen merged 3 commits intomainfrom
PhoneGrump

Conversation

@PureWeen
Copy link
Copy Markdown
Owner

Problem

On Android (remote mode), sessions with long conversation histories (like FixUITestFromFreezing) show "Loading conversation…" forever and never display messages.

Root Causes

1. Thread-safety in SendSessionHistoryToClient

session.History is a plain List<ChatMessage> modified concurrently by SDK event handlers on background threads. When SendSessionHistoryToClient iterates the list via .Skip().ToList(), it can throw InvalidOperationException: Collection was modified. The exception is silently caught by the generic handler — so no history is ever sent to the mobile client.

2. Unbounded history payload on turn end

When a turn ends, RequestHistoryAsync(s) was called with limit: null, requesting ALL messages. For long conversations with thousands of messages and tool results, this serializes to a massive JSON payload that can block or fail the WebSocket channel.

Fix

  • Defensive snapshot: SendSessionHistoryToClient now takes an array snapshot of History with retry on InvalidOperationException, preventing concurrent modification crashes
  • Capped turn-end history: Post-turn-end refresh capped to 200 messages (users can still click "Load rest of conversation" for the full history)

Also included

  • Updated android-wifi-deploy skill: adb push + pm install instead of adb install -r (the ~98MB APK consistently drops WiFi ADB connections mid-transfer)

PureWeen and others added 2 commits February 25, 2026 17:31
Two issues caused sessions with long histories to show 'Loading conversation...'
forever on Android (remote mode):

1. Thread-safety: SendSessionHistoryToClient iterated session.History (a plain
   List<ChatMessage>) without synchronization. SDK event handlers modify this
   list from background threads, causing InvalidOperationException during
   enumeration. The exception was silently caught by the generic handler,
   so no history was ever sent to the mobile client. Fixed by taking a
   defensive snapshot via ToArray() with retry on concurrent modification.

2. Unbounded history request on turn end: RequestHistoryAsync was called with
   limit: null after each turn, serializing ALL messages for long conversations
   into a single massive JSON payload. Capped to 200 messages to keep payloads
   manageable while still providing good context.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- adb install -r consistently drops for ~98MB APKs over WiFi ADB
- adb push + pm install is reliable (push streams, install runs on-device)
- Add invariant: never hardcode ports in proxy script, always use CLI args
- Update all references from adb install to push+pm install workflow

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ailure

- ToArray() uses Array.Copy, not enumerator — won't throw
  InvalidOperationException. Remove dead retry/catch block.
- On snapshot failure, log and return without sending — never send an
  empty authoritative payload (TotalCount=0, HasMore=false) that
  permanently breaks the session on the client.
- Catch Exception (not bare catch) to handle ArgumentOutOfRangeException
  from concurrent list resize.
- Extract TurnEndHistoryLimit constant (200).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant