Skip to content

Chat bridge answer over 2 MiB is cut by readline and the reply is lost #1021

Closed

Description

@czpython

What happened

A chat turn read a PDF from the thread. The reply never arrived. The web log shows the step that collects the reply retried five times and then:

ERROR druks.chat.service Chat delivery failed for conversation <id>
  File "/app/backend/druks/chat/bridge.py", line 41, in request
    response = json.loads(line)
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1465630 (char 1465629)

Why

Bridge.request in backend/druks/chat/bridge.py reads the bridge's one-line JSON answer with reader.readline() on an asyncssh channel. asyncssh's readuntil keeps the line in the channel's receive buffer, and when that buffer reaches the channel's receive window (2 MiB, _DEFAULT_WINDOW) it stops reading and raises IncompleteReadError with the partial line; readline catches that and returns the partial as if the line had ended. No error, no newline.

The turn's event log held two events of 1.4 MB each, the tool result carrying the PDF's text. The events answer was 2.1 MB, so the line was cut mid-string, and every retry got the same cut.

Proof

Replaying the events request against the live bridge of that conversation:

ping             (89 bytes, newline=True, ok=True)
events after=0   (2105344 bytes, newline=False, not json: Unterminated string ... char 1465629)
ping again       (89 bytes, newline=True, ok=True)

2,105,344 bytes is 2 MiB plus 8 KiB: the window, plus the chunk that crossed it. The bridge is healthy after it.

A plain TCP server in the sandbox that writes a 3 MB line through the same port forward:

readline:  got=2110080 newline=False
read loop: got=3000001 end=newline

The forward itself carries 16 MB intact when read in chunks.

Fix

Read the answer in chunks until the newline instead of readline. The protocol is one JSON line per answer, so the newline is the only end that matters. Do not read to end-of-stream: the drukbox gateway's forward does not signal it (filed separately).

Reading the answer this way removes the cap. Whether a tool result of 1.4 MB belongs in a chat event at all is a separate question, filed separately.

Activity

  1. czpython commented on Oct 27, 2026

    @czpython
    OwnerAuthor

    Related: czpython/drukbox#88 (the forward never signals end-of-stream, so the fix must stop at the newline) and #1022 (why the answer was 2 MB).

  2. czpython commented on Nov 11, 2026

    @czpython
    OwnerAuthor

    Fixed by #1119.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions