Skip to content

OPE-245: Stable session connection and sync engine #525

Closed

Description

@ivanfenenko

Session Chat and Home connect fast, reconnect fast, show truthful statuses, switch servers cleanly, and resume work quickly on bad networks and rapid app switching, on both v1 and v2 servers.

Replaces the per-projection recovery patch stack (OPE-165, 177, 197, 201, 233, 234) with one transport FSM and one sync coordinator modeled on the official opencode client (packages/client/src/solid/{connection,data}.ts on origin/beta). Both apps ship each phase together. One commit per phase; the shared shared/sse-recovery/ fault oracle must stay green at every commit.

Audit findings this fixes

  • SSE failures are swallowed; 401/403/404 look like EOF and retry every 1s forever with "Connecting" chrome.
  • Protocol guessed as v1 until probe; a failed implicit probe latches v1 until manual Test Connection.
  • Linear backoff to 10s, no jitter, no immediate retry on path-ready or foreground.
  • iOS opens a new URLSession per SSE attempt and double-parses every data: line; REST on URLSession.shared (60s hangs pin authority slots).
  • iOS .inactive ignored.
  • Reconnect thundering herd: every Live Store × directory × open MessageSync owns a GapAuthority and refetches on three triggers.
  • v2 status is REST polling in disguise: synthetic session.active.snapshot on every step/shell/compaction.ended forces GET /api/session/active.
  • Status is "absent from map = idle" plus a pendingBusy overlay; one missed execution.succeeded shows Working until the next reconnect.
  • canMutate true while Connecting and during the 5s background grace.
  • Profile switch fans out to three independent reactors with yield() ordering; identity ignores password; disconnect() outside mutex; lastDirectoriesByIdentity unbounded.

Phases (one commit each)

Phase 1 — Honest transport

  • Typed SSE attempt outcome: Opened, Ended, Http(status), Transport, Cancelled. 401/403 terminal → AuthFailed chrome, no retry until profile edit. 404 fast-retry only while protocol unresolved.
  • Require server.connected as first frame within a 3s connect window; otherwise fail the attempt.
  • Backoff 1,2,4,8s ×jitter, cap 15s; reset and reconnect immediately on PathReady and Foreground.
  • Resolve protocol before opening SSE on cold start (cached protocol skips probe); clear probeFailed on PathReady.
  • iOS: one long-lived streaming URLSession per profile; drop JSON pre-parse in SSEDataLineParser; dedicated REST URLSession with 15s request timeout; .inactive > 2s starts background grace.

Phase 2 — One sync coordinator

  • Single SyncCoordinator owning the reconnect protocol: disconnect → mark projections stale (no network); server.connected → one bootstrap batch (session/active v2 or session/status v1, session lists for visible directories, focused session last page), merged with execution.* that race the fetch; observed-and-stale projections resync lazily; foreground after ≥20s quiet → same bootstrap.
  • Remove per-projection GapAuthority and LiveOverallFlags reconnect-barrier flags; keep SseEventIngress, reducers, and the oracle; rewrite oracle driver expectations to the new triggers.

Phase 3 — Status as a projection

  • SessionActivity = idle | running | retrying(attempt, next) per session, fed by execution.*/retry.scheduled (v2) and session.status (v1), seeded from bootstrap. Drop absent-means-idle and pendingBusy; optimistic running is a local mutation with 10s expiry, overridden by any server event.
  • Stop emitting session.active.snapshot from step boundaries; one 30s reconcile while a session is running and foregrounded.
  • canMutate = Live only; while Connecting, queue the send until Live or fail after a bounded wait with retry.

Phase 4 — Profile switch as one transaction

  • Single switchProfile(id): stop stream → cancel in-flight REST → clear projections → rebind → rebuild clients → start stream → one ProfileSwitched after the last step. Include password in connection identity. Remove duplicate stop/start from ProcessWorldSignals/SceneWorldSignals. Fix disconnect() mutex and persistLocked capture race; bound lastDirectoriesByIdentity.

Phase 5 — Resume speed

  • Persist last known SessionActivity and session list per profile; Home paints statuses from disk with stale tint.
  • Session Chat: render cache, request limit=20, order=desc (v2) / tail (v1), compare last message id/updated with cache; identical → confirmed without full merge. Replace fingerprint machinery.

Acceptance

  • Wrong password: AuthFailed chrome within 3s, zero retries until profile edit.
  • v2 server cold start: no /global/event or /event requests when protocol is cached; at most one probe when not.
  • Reconnect with 8 projects: one bootstrap batch, no per-directory REST storm; oracle trace added.
  • Missed execution.succeeded: status corrects within 30s without reconnect.
  • Profile A→B→A: no events, REST results, or cache from the wrong profile; one ProfileSwitched per switch.
  • Return to a Session Chat after backgrounding: cached transcript visible immediately, confirmed or patched by one REST call.
  • All 21 existing oracle traces plus new traces per phase pass on both platforms.

Remaining:

  • Phase 1 — Honest transport (328ba01d)
  • Phase 2 — One sync coordinator (37842f4b)
  • Phase 3 — Status as a projection (0e1a5c14)
  • Phase 4 — Profile switch as one transaction (aa7beab5)
  • Phase 5 — Resume speed (2f6deaa4)
  • Manual smoke on a live v1 server and a v2 (origin/beta) server, both apps: wrong password → AuthFailed then fix → Live; airplane-mode toggles; background > 5s then foreground; profile A→B→A; cold start on cached Home; missed execution.succeeded corrects within 30s
  • Add the per-phase oracle traces named in Acceptance (reconnect with 8 projects = one bootstrap batch; auth-failed terminal; profile password change restarts leases). The existing 21 traces pass unchanged on both platforms; no new traces were added
  • Android ForegroundEventStream.stop() joins lease jobs unbounded; iOS bounds the join at 1.5s. Add a bounded join on Android
  • lastDirectoriesByIdentity is still unbounded (keyed by profile identity × protocol; small in practice)
  • Todos projection key is per-session on Android and per-directory on iOS; align if it ever becomes user-visible

Imported from Linear OPE-245. Linear status: Done. Priority: High. Created 2026-09-13. Completed 2026-09-13.
Related: OPE-233, OPE-177, OPE-234, OPE-165.

Activity

  1. self-assigned this
    on Oct 11, 2026
  2. added
    ios
    android
    shared
    improvement
    priority: high
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

androidimprovementiospriority: highshared

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions