Skip to content

fix(live): resume after upstream drops, never freeze, log session lifecycle - #32

Merged
Alorse merged 5 commits into
mainfrom
fix/resume-after-drop
Sep 29, 2026
Merged

Alorse merged 5 commits into
mainfrom
fix/resume-after-drop

Conversation

@Alorse

@Alorse Alorse commented Sep 29, 2026

Copy link
Copy Markdown
Owner

What this changes

Conversations survive a dropped line (#16). Gemini Live sometimes closes the connection mid-conversation (we've seen close 1011 after about 8 minutes). Before, Parley opened a brand-new session, greeted the learner again and had forgotten everything. It could also only do this once. Now every session asks Gemini for resumption handles, and after a drop the server reconnects with the latest handle, so the conversation carries on where it was. While it reconnects:

  • the mic is held; nothing is sent upstream until the new setup completes.
  • the client gets reconnecting followed by interrupted, so audio still queued from the lost turn is dropped rather than played over what comes next.
  • the UI shows "Reconnecting…".

Each drop gets 3 attempts with exponential backoff (starting at 0.5 s, capped at 4 s). A session allows 8 reconnects in total, set by PARLEY_MAX_RECONNECTS.

Nothing waits forever any more (#17).

  • Setup that never completes: it fails after 15 s (PARLEY_SETUP_TIMEOUT_MS), and the next live model is tried.
  • A reply whose turnComplete never arrives: the server completes the turn itself 10 s after the reply's audio would have finished playing (PARLEY_TURN_WATCHDOG_MS), so the mic reopens. The watchdog allows for the playback time, so it doesn't cut short gemini-3.8-live's burst-sent replies.
  • Tutor unreachable for good: this covers reconnects running out or no model completing setup. The server sends a plain error, frees the MAX_SESSIONS slot straight away and closes the socket. The browser then turns the mic off, stops playback, returns to idle and says the conversation ended.
  • Client leaves mid-setup: the upstream session that was still being set up is stopped instead of being left running.

Lifecycle records in the journal (#21). Each /live connection writes one-line live-session {json} records, stamped with a short session id: open, busy, start, upstream-ready, upstream-close (code, reason, uptime), reconnecting, reconnected, gave-up, setup-timeout, turn-watchdog and end (duration, learner turns, reconnects). A test checks they never contain speech, audio, the API key or the resumption handle. DEVELOPING.md explains how to grep them.

Hardening. A second start on the same socket is now rejected with {code:"already-started"}. Before, it opened a second upstream and two voices played into one client.

Browser side. The cache version is bumped from v8 to v9. The late close of a stale socket no longer ends a newer session. Teardown is now shared between the End button and a close from the other side.

Closes #16
Closes #17
Closes #21

Test evidence

npm run typecheck passes. npm test and /usr/bin/node (v20.20.2) --test test/*.test.js both pass: 136 tests, 130 pass, 0 fail, 6 todo. Before this change it was 124 tests, 110 pass, 14 todo. Eight harness todo tests are now regression tests, and there are new tests for the resumption setup, repeated drops, the reconnect limit and backoff, the burst-safe watchdog, config parsing and the session logger.

scripts/repro-browser.mjs (headless Chrome, fake mic, fake upstream), before on main and after on this branch:

scenario main this branch
upstream-drop-twice: persona prompts sent per upstream [1, 1] (greeted again) [1, 0, 0]
upstream-drop-twice: resumption handle used no yes
upstream-drop-twice after the 2nd drop "Listening…", error shown, 0 mic frames reached Gemini in 10 s "Listening…", no error, 232 frames reached Gemini (conversation continues)
upstream-lost (new scenario: upstream can't be reached again) "Listening…", mic on, 27 frames/s sent into nothing, slot held (activeSessions 1) "Ready when you are", mic off, 0 live tracks, 0 frames/s, clear message, activeSessions 0
server-restart: live mic tracks / frames per second after the learner taps again 2 / 54.5 1 / 30–32
end-restart 1 live socket, 0 interleaving unchanged

Real API check. The resumption flow ran once against gemini-3.8-live with the real GeminiLiveSession: greeting, then a turn ("my favourite fruit is a pomegranate"), then a forced drop. The resumed setup was accepted in 491 ms with no second greeting. Asked afterwards, the tutor answered "Your favorite fruit is a pomegranate."

Not in this PR

  • The 8-reconnect lifetime cap means a single conversation that has already dropped about 8 times (at roughly 8 minutes between drops) will end on its next drop. It's configurable. A cap that only counts reconnects in quick succession is a possible follow-up.
  • The remaining 6 harness todos are other issues: the thinking state, Parley misunderstands the user's language and asks them to repeat phrases they never said #14 hearing/persona, and ACCESS_TOKENS. So is the double-tap browser scenario, which still gives 2 mic tracks.

🤖 Generated with Claude Code

Alorse and others added 5 commits September 29, 2026 06:48
Every /live connection now writes one-line 'live-session {json}' records to
the journal with a short session id: open, busy, start, upstream-ready,
upstream-error, upstream-close (code, reason, uptime), reconnecting,
going-away, gave-up, start-failed and end (duration, learner turns,
reconnects, client close code). Metadata only: no speech, audio, API key or
resumption handle.

Refs #21

Co-authored-by: Claude Code <noreply@anthropic.com>
Every Live setup now asks for session resumption and the server keeps the
latest sessionResumptionUpdate handle. When the upstream closes mid
conversation, the server holds the mic gate, tells the client
'reconnecting' then 'interrupted' (so the lost turn's queued audio is
flushed), and reconnects with the handle: the same conversation carries on
with no persona resend and no second greeting. Nothing is sent upstream
until the new setup completes. Drops are retried with exponential backoff
(3 attempts per drop, PARLEY_MAX_RECONNECTS total, default 8). goAway no
longer warns the learner when the close can be resumed. The client shows a
'Reconnecting…' state.

Refs #16

Co-authored-by: Claude Code <noreply@anthropic.com>
Every wait now has a limit. An upstream setup that does not complete within
PARLEY_SETUP_TIMEOUT_MS (default 15 s) fails that attempt. A reply whose
turnComplete never arrives is completed by the server PARLEY_TURN_WATCHDOG_MS
(default 10 s) after its audio would have finished playing, so the mic
reopens without cutting a burst-sent reply short.

When the tutor can't be reached any more (reconnects exhausted, or no live
model completes setup) the server sends an error, frees the MAX_SESSIONS
slot at once and closes the socket. A client that leaves mid-setup no longer
leaves the pending upstream running. In the browser, a remotely ended
conversation now stops the microphone, flushes playback, returns to idle and
says it ended; a stale socket's late close no longer ends a newer session.

Adds an 'upstream-lost' browser repro scenario and a rejectConnections
option to the fake upstream.

Refs #17

Co-authored-by: Claude Code <noreply@anthropic.com>
A repeated start used to open a second upstream session and interleave two
voices into one client. It is now refused with {code:'already-started'} and
logged as start-rejected.

Co-authored-by: Claude Code <noreply@anthropic.com>
- public/app.js: one teardownSession() for the End button and remote
  closes; endSession() uses setUiState('idle').
- server/live.ts: a settle() helper for the four setup outcomes; sendAudio
  relies on _sendUpstream's readiness check.
- server/config.ts: live tuning defaults come from live.ts, defined once.
- server/index.ts: drop the ambiguous 'started' field from the end record.

Co-authored-by: Claude Code <noreply@anthropic.com>
@Alorse
Alorse merged commit 960c5a0 into main Sep 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant