Skip to content

[Bug]: /queue FIFO chain drops MEDIA files — only last item gets media delivery #18539

@vnhwd

Description

@vnhwd

Bug

When multiple /queue items are chained in a single session via the FIFO queue mechanism, only the last queue item gets its MEDIA files delivered. All preceding items have their MEDIA:/path/to/file tags rendered as raw text — the files are generated but never uploaded to the messaging platform.

Root Cause

Two code paths are responsible:

1. Normal path (works correctly)gateway/platforms/base.py line 2724:

response = await self._message_handler(event)
# ...
if response:
    media_files, response = self.extract_media(response)  # ← MEDIA extracted before send
    await self.send(chat_id, cleaned_text)

Every _handle_message return goes through extract_media() before delivery.

2. Queue follow-up path (broken)gateway/run.py lines 13079-13098:

# First queue item complete, second in line → deliver first response
first_response = result.get("final_response", "")
if first_response and not _already_streamed:
    await adapter.send(source.chat_id, first_response, ...)  # ← RAW text, no extract_media!

This directly calls adapter.send() with the raw final_response — including MEDIA:/path tags — bypassing extract_media() entirely. The files exist on disk but never get uploaded.

The queue drain loop (added for FIFO /queue semantics) handles its own "deliver previous response → run next queue item" pipeline, but never calls extract_media() or _deliver_media_from_response() for non-last items.

The streaming path at line 6244 already calls _deliver_media_from_response() correctly — this same treatment is missing from the queue drain path.

Steps to Reproduce

  1. In a single session, send two /queue commands that each produce files:
    /queue Generate a PDF report → deliver with MEDIA:/tmp/report1.pdf
    /queue Generate another PDF → deliver with MEDIA:/tmp/report2.pdf
    
  2. Wait for both to complete
  3. Expected: Both PDFs delivered natively
  4. Actual: Only report2.pdf is delivered (or streamed). report1.pdf shows as raw MEDIA:/tmp/report1.pdf text.

Affected Code

  • gateway/run.py ~13079-13098 — queue follow-up response delivery, missing extract_media() call
  • gateway/run.py ~6244-6262 — streaming path already has _deliver_media_from_response() correctly (reference)
  • gateway/platforms/base.py ~2724 — normal path correctly calls extract_media() (reference)

Suggested Fix

Insert extract_media() call before adapter.send() in the queue follow-up path (~line 13086):

if first_response and not _already_streamed:
    # Extract and deliver MEDIA files before sending the text
    media_files, first_response = adapter.extract_media(first_response)
    await adapter.send(source.chat_id, first_response, ...)

And deliver the extracted media files via the adapter's native media upload path.

Workaround

  • Send queue items one at a time (don't chain)
  • Use cronjob for file-generating tasks instead of /queue
  • Bundle all files into the last queue item

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions