Skip to content

refactor(gateway): extract media/event helpers from run.py (slice 7 of #54962) - #77702

Open
andrexibiza wants to merge 1 commit into
NousResearch:mainfrom
andrexibiza:fix/gateway-media-helpers
Open

refactor(gateway): extract media/event helpers from run.py (slice 7 of #54962)#77702
andrexibiza wants to merge 1 commit into
NousResearch:mainfrom
andrexibiza:fix/gateway-media-helpers

Conversation

@andrexibiza

@andrexibiza andrexibiza commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Related #54962 #55138

What

Extracts the media/event-handling cluster (9 pure helpers) out of the 26,823-line gateway/run.py god file into a new self-contained gateway/media_helpers.py module.

Moved functions (verbatim — zero behavior change, docstrings/comments preserved):

  • _event_media_type_at
  • _event_media_is_image
  • _event_media_is_audio
  • _event_media_is_stt_input
  • _event_media_is_video
  • _build_media_placeholder
  • _build_document_context_note
  • _format_duration
  • _probe_audio_duration (async)

The cluster is CLEAN: it only depends on MessageType (from gateway.platforms.base), asyncio, os, and typing.Optional — no module-level state, no move-with constants. gateway/run.py now re-exports the names via a module-attribute import, so gateway.run.<name> references (call sites in GatewayRunner methods and any external lazy importers) keep resolving unchanged.

Why

Part of the god-file shrink campaign (#54962): gateway/run.py is 26,823 lines, and this is one of several pure-cluster extractions into focused gateway/*_helpers.py modules. Pure moves only — no behavior change, no refactor of the moved bodies.

How to test

# Import smoke test — must print "import OK"
python -c "import gateway.run; import gateway.media_helpers as mh; print('import OK', gateway.run._event_media_type_at is mh._event_media_type_at)"

# Targeted suites — 131 passed, 1 failed
python -m pytest tests/gateway/test_document_context_note.py \
  tests/gateway/test_mixed_attachment_routing.py \
  tests/gateway/test_video_context_note.py \
  tests/gateway/test_voice_command.py \
  tests/agent/test_insights.py \
  tests/gateway/test_media_extraction.py \
  tests/gateway/test_73771_media_resend_dedup.py \
  -q --no-header -p no:cacheprovider

Expected: import OK and all targeted tests green. The single failure (test_73771_media_resend_dedup.py::test_streamed_explicit_media_resend_is_delivered) is a pre-existing Windows path/URL-encoding environment issue — stash-proven to fail identically on pristine main (it asserts a raw C:\... path against a file://C%3A... URL-encoded form). Not related to this move.

Platforms tested

  • Windows native: parse + import gateway.run + import gateway.media_helpers + targeted test suites above
  • git diff --check clean
  • scripts/check-windows-footguns.py gateway/run.py gateway/media_helpers.py — ✓ No Windows footguns

Shrink

gateway/run.py: 26,823 → 26,681 lines (net −142; 153 lines moved out, 11-line re-export import added). New module: gateway/media_helpers.py (169 lines).

Part of #54962

Part of #55138

Part of #78647

…NousResearch#54962)

Signed-off-by: andrexibiza <84248988+andrexibiza@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants