kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Offer nearby Kolibri servers at first run and keep switching easy

Open rtibbles opened this issue 5 months ago • 1 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Android ≤ 12 — the majority of Kolibri sessions — cannot resolve kolibri.local in a browser, so the app is the discovery path on Android. Today the app only starts a local server; it never offers servers already on the network. Make discovered servers a first-class choice, at first run and after.

Complexity: High Target branch: develop

Context

  • The app boots a local Kolibri server and loads http://127.0.0.1.
  • WebViewActivity.shouldOverrideUrlLoading sends any non-loopback host to an external browser, so a remote server cannot load in-app today.
  • Kolibri core broadcasts each running server over mDNS (see References), but the peers are only consumable by the Python server — nothing in the app surfaces them.
  • The Kolibri WebView JS bridge backs only printing and blob/data-URL downloads.
  • File upload uses the standard onShowFileChooser, not the bridge.
  • The one device-local secret is the OS-user auth token, injected as a query param on the loopback URL only.

The Change

  • Discover nearby servers natively via NsdManager, not through the Python server, so discovery runs before the local server boots.
  • Identify Kolibri among _http._tcp services by the presence of the contract's TXT keys.
  • Do not rely on the _sub subtype for filtering — Android subtype discovery is unreliable.
  • Exclude the device's own instance from the list.
  • Exclude any Subset-of-Users Device (subset_of_users_device TXT flag) — SoUDs broadcast but aren't meant to serve others.
  • Treat an absent flag (older DEVICE_INFO v1) as a full server.
  • Offer discovered servers in a native picker at first run.
  • Provide a native switcher thereafter: a persistent "server chip" over the WebView.
  • The chip opens a bottom-sheet listing "My Kolibri (this device)" alongside discovered servers.
  • Store the selection by instance_id, or a LOCAL sentinel.
  • Re-resolve the selection via NSD on launch.
  • Load a selected remote server's base_url directly in the WebView.
  • When the selection is remote, keep the local Python server stopped.
  • Boot the local server on demand only for "Start my own" or a switch to local.

Out of Scope

  • Kolibri-core changes — the broadcast contract already exists; this task is app-side only.
  • Proxying remote content through the local server — the remote server loads directly.
  • Cross-instance auth/SSO — a remote server applies its own guest/login (including "learners must sign in").

Acceptance Criteria

"Eligible server" = a discovered Kolibri server that isn't a SoUD or this device.

General — first run & discovery

  • [ ] On a network with 2+ eligible servers, first run shows a picker listing each by device_name, plus "Start my own Kolibri".
  • [ ] On a network with exactly one eligible server, first run skips the picker and loads that server directly.
  • [ ] On a network with no eligible servers, first run proceeds to local setup.

Selection & persistence

  • [ ] Selecting a server opens its base_url in the app's WebView, not an external browser.
  • [ ] The remembered selection is restored after an app restart.
  • [ ] The remembered selection is restored after the server's IP changes.
  • [ ] A signed-in session on a remote server survives an app restart.
  • [ ] A signed-in session on a remote server survives switching away and back.
  • [ ] A remembered remote server unreachable at launch offers retry, pick another, or use local.

Switching

  • [ ] The server chip is available whenever a server is loaded.
  • [ ] When no servers were found, the chip still offers "connect to a nearby Kolibri".
  • [ ] After local setup, the switcher lists discovered servers alongside "My Kolibri (this device)".
  • [ ] Switching between a discovered server and the local instance works in both directions.
  • [ ] While a remote server is selected, no local Kolibri server is running.

Security

  • [ ] Printing works while viewing a remote server.
  • [ ] File upload works while viewing a remote server.
  • [ ] Downloads work while viewing a remote server.
  • [ ] The OS-user auth token is never sent to a remote origin.
  • [ ] Only loopback and the currently-selected remote host load in the WebView.
  • [ ] Any other host opens in an external browser.

Testing

  • [ ] Unit tests cover TXT parsing, base_url construction, the eligibility filters, selection-store read/write, and launch routing (0 / 1 / 2+ / LOCAL / unreachable-remote).

Testing

  • LAN multicast does not traverse the emulator's user-mode NAT, so real NSD discovery cannot be exercised on the emulator. Test discovery on a real LAN, or with an in-process fake broadcast.

References

  • Broadcast contract (already emitted by Kolibri): service Kolibri._sub._http._tcp.local., TXT instance_id, device_name, port, prefix — kolibri/core/discovery/utils/network/broadcast.py.
  • subset_of_users_device flag in the broadcast device_info — kolibri/core/device/utils.py (get_device_info).

AI usage

Drafted with Claude Code in an interactive design session; the author made every design decision in dialogue and verified the code references.

rtibbles avatar Jul 04 '26 04:07 rtibbles

Plan: The Android app discovers Kolibri servers on the LAN natively, offers them at first run and through a persistent switcher, and loads a chosen remote server in its own WebView with the local server stopped (#15012).

  • [ ] Parse a Kolibri TXT record into an eligible server
  • [ ] One-shot mDNS discovery
  • [ ] Selection store and launch routing
  • [ ] Load a remote server in-app with the local server stopped
  • [ ] Server chip and switcher sheet
  • [ ] First-run picker and launch routing
  • [ ] Self-review the produced code

Assumptions — the issue did not settle these; say so if one is wrong.

  • Discovery is a hand-rolled one-shot mDNS query for Kolibri._sub._http._tcp.local., not NsdManager; if NsdManager is required, core must also register _http._tcp and Task 2 is replaced.
  • Kolibri is recognised by TXT instance_id + device_name; if application == "kolibri" must match too, Task 1's check tightens.
  • Launch discovery waits 3 s before routing; too short misses slow servers at first run, too long delays local setup.
  • No stored selection plus an existing db.sqlite3 means LOCAL; if upgrading users should see the picker, drop the adoption step.
  • The first-run outcome (0 → LOCAL, 1 → that server) is persisted; if not, every launch re-routes by count.
  • The selection also stores device_name for the unreachable dialog; if only the id may be stored, the dialog drops the name.
  • Add com.google.android.material for BottomSheetDialog and Chip on a .Bridge theme; if not, Task 5 builds the sheet on AppCompatDialog.
  • The chip floats bottom-start over the WebView; if it covers Kolibri UI, only its gravity and margins change.
  • "Connect to a nearby Kolibri" is a re-scan action in the sheet, not the chip's label; if it should label the chip, Task 5's chip text changes.
  • A remote server reopens at its base_url, not its last page; if it should restore, WebViewLocation needs per-origin paths.
  • Env init and the task worker still run while remote; only the HTTP server stops; if no Python may run, App.onCreate needs gating.
  • A remote that fails mid-session gets no dialog, only the chip; if it should, Task 6 hooks onReceivedError.
  • platforms/android/** joins tox.yml's python filter so JVM tests run; if a separate filter is wanted, the job's if: changes instead.
  • Deployed servers run zeroconf-py2compat 0.19.x; any that ignore legacy-unicast queries go undiscovered.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Oct 21 '26 23:10 rtibblesbot