Streaming reconnect persists the same durable _row_id repeatedly; append-only merge amplifies it until the session file OOM-kills the WebUI

Author: Huy3koCreated Sep 29, 2026Updated Oct 1, 2026
Labelsbugsprint-candidateprioritystreaming

Streaming reconnect persists the same durable _row_id repeatedly; append-only merge amplifies it until the session file OOM-kills the WebUI

Summary

When a reconnect (or re-persist) lands while an assistant row is still streaming — finish_reason: "incomplete", empty visible content, only the provider sidecar (api_content) advancing — the WebUI can persist multiple snapshots of the same durable state.db _row_id. The merge path then stops deduplicating them, and every subsequent persist appends another copy. In a production incident this grew one assistant row to 108,454 copies inside a single session file.

Impact (measured, one affected session)

Metric Broken file After manual dedup
stored entries 109,534 1,091 (real messages)
copies of one _row_id 108,454 1
session JSON size 371 MB 7.2 MB (stable after new turns)
peak RSS to deserialize ~5.4 GB ~66 MB

The failure cascade: /api/session/stream?...&known_count=<N> full-deserializes the session JSON → multi-GB RSS → kernel OOM-kill of the WebUI process → Caddy 502 → the chat becomes unopenable ("error immediately when opening, cannot type"). The session content itself is not corrupted — the file is dominated by duplicates.

Suspected mechanism (from api/models.py)

  1. _session_message_dedup_key() extends the key with the api_content sidecar (_session_message_key_with_sidecar), so two snapshots of the same row with different sidecar bytes get different dedup keys.
  2. _row_id_fast_path_allowed() in _merge_session_messages_append_only_impl() returns False as soon as sidecar_row_id_counts[row] > 1 or state_row_id_counts[row] > 1.
  3. With the fast path disabled, the state row falls through to merged_messages.append(...) — append-only amplification.

The amplification is self-reinforcing: the second copy is the tipping point; from then on every merge/persist cycle adds another, and the guard that was meant to protect ambiguous rows guarantees they can never converge again.

Minimal synthetic reproduction

python
import api.models as models

def partial(api_text, ms):
    return {"role": "assistant", "content": "", "timestamp": 1000.0,
            "finish_reason": "incomplete", "api_content": api_text,
            "_row_id": 24318, "_firstTokenMs": ms, "_db_persisted": True}

user = {"role": "user", "content": "canonical prompt",
        "timestamp": 999.0, "_row_id": 24317, "_db_persisted": True}

merged = [user, partial("Reas:", 10), partial("Reas: partial", 12)]
state = [user, partial("Reas: partial longer", 14)]

for _ in range(5):
    merged = models.merge_session_messages_append_only(merged, state)

rows = [m.get("_row_id") for m in merged]
assert rows.count(24318) == 1  # fails: grows to 2 on the first merge and keeps going

Expected: one entry per durable _row_id; the retained snapshot should be the most advanced streaming state. Actual: duplicates accumulate.

Notes

  • The preserve-both rule for two settled rows with distinct provider payloads (existing_api_content != incoming_api_content at the fast path) seems intentional for truncation-survival; a fix should keep that behavior and only collapse unfinished streaming skeletons (assistant, empty visible content, no tool_calls, non-terminal finish_reason, same _row_id).
  • Reproduced against current master. A PR with pre-merge collapse + regression tests is on the way (from a fork; will link it here).

No production data, paths, or session content included — all numbers above come from a local, anonymized incident and a synthetic fixture.