Desktop v0.5.5 (Windows): permanent WebSocket disconnect loop — gateway.ws_flow_encode_or_budget_failed (ValueError) ~1s after every ws.authenticated

Author: xiaoli-6559Created Sep 24, 2026Updated Sep 24, 2026

Environment

  • OpenSquilla Desktop v0.5.5 (Windows x64 installer, gateway opensquilla-gateway.exe bundled)
  • Windows 11, gateway on default 127.0.0.1:18791
  • Provider: tokenrhythm, single model glm-5.3-flash, squilla_router.enabled = false
  • First observed right after saving onboarding config in the desktop wizard (config persisted → gateway restarted while WebUI stayed connected)

Symptoms

  1. WebUI repeatedly flips to disconnected every ~10s, forever, regardless of user activity ("entering a task session disconnects immediately" is just the most visible symptom).
  2. Onboarding "test model" button always fails with Cannot call onboarding.provider.probe: not connected (state: disconnected) — the RPC never reaches the gateway because the socket is dead by then.
  3. Restarting the app / killing the gateway does not help — the loop resumes within seconds after every restart.
  4. Rolling back config.toml to the pre-change backup does not durably help either (brief calm, then the loop resumes).

Gateway log evidence

Every connection follows the same deterministic pattern (~1s between auth and kill):

[INFO] opensquilla.gateway.websocket: {"event": "ws.connected",    "conn_id": "…"}
[INFO] opensquilla.gateway.websocket: {"event": "ws.authenticated", "conn_id": "…", "role": "operator"}
[WARNING] opensquilla.gateway.websocket: {"event": "gateway.ws_flow_encode_or_budget_failed", "exception_type": "ValueError", "conn_id": "…"}
[INFO] opensquilla.gateway.websocket: {"event": "ws.disconnected", "conn_id": "…"}

78 consecutive occurrences on 2026-09-24 (17:13–17:58 local) before we applied the workaround. Preceding the very first failure: onboarding.config_persisted → gateway.config_persisted → tiktoken_encoding_unavailable_fallback → first flow_encode_or_budget_failed 1s later.

Note: the JSON log formatter drops the exc_info traceback, so only exception_type survives — attaching the full traceback (or logging it separately) would make this class of bug much easier to triage.

Analysis (from reading the source of the flow path)

_prepare_flow_frame() (gateway/websocket.py, called at line ~1905) raises ValueError at 5 sites (wire-limit, snapshot-delivery-not-current ×2, snapshot-over-budget, control-buffer-full, transport-budget-full). For non-session.event.* frames the handler force-closes the connection with code 1013 (transport_resource_limit).

Hypothesis: after the gateway restarts mid-session, the WebUI replays a stale snapshot delivery receipt / flow epoch on reconnect; validation fails deterministically → force close → the UI retries with the same stale state → infinite loop. The stale state lives in the Electron WebUI storage, which is why app restarts cannot clear it.

Workaround that fully resolved it

The documented emergency kill switch completely fixed the loop (verified: 0 disconnects over the following session, all features working):

OPENSQUILLA_GATEWAY_WS_TRANSPORT_FLOW_ENABLED=false

Cost: only loses incremental resume; the UI falls back to full re-sync.

Suggestions

  1. Handle a stale/invalid delivery receipt on sessions.messages.snapshot resume gracefully (full re-sync instead of ValueError → close 1013).
  2. When _prepare_flow_frame fails for the same conn repeatedly, drop the flow state for that connection and degrade to no-flow transport instead of closing.
  3. Log the exc_info traceback for gateway.ws_flow_encode_or_budget_failed even in JSON format (a traceback field would have turned a day of debugging into minutes).