refactor(gateway): extract GatewayTurnExecMixin from run.py (slice 29 of #54962) - #77752
Open
andrexibiza wants to merge 3 commits into
Open
refactor(gateway): extract GatewayTurnExecMixin from run.py (slice 29 of #54962)#77752andrexibiza wants to merge 3 commits into
andrexibiza wants to merge 3 commits into
Conversation
… of NousResearch#54962) Signed-off-by: andrexibiza <84248988+andrexibiza@users.noreply.github.com>
…rn_exec_mixin.py Signed-off-by: andrexibiza <84248988+andrexibiza@users.noreply.github.com>
andrexibiza
force-pushed
the
refactor/gateway-mixin-turn-exec
branch
from
August 3, 2026 14:30
8420eba to
1d8b456
Compare
# Conflicts: # gateway/run.py
This was referenced Aug 4, 2026
Open
andrexibiza
added a commit
to andrexibiza/hermes-agent
that referenced
this pull request
Aug 5, 2026
…_for_turn (precedent NousResearch#77752)
This was referenced Aug 5, 2026
Open
Open
Open
Open
This was referenced Aug 5, 2026
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related #54962 #55138
What
Third wave of the GatewayRunner mixin decomposition (gate 4 of #54962): extract the turn-execution engine — 21 methods / ~2,935 lines — out of
GatewayRunneringateway/run.pyinto a newgateway/turn_exec_mixin.py(GatewayTurnExecMixin), following the codebase's existing mixin pattern (gateway/authz_mixin.py,gateway/kanban_watchers.py,gateway/slash_commands.py).The mixin holds: the agent run entry points (
_run_agent,_run_agent_inner), the remote-proxy path (_get_proxy_url,_build_stream_consumer_config,_run_agent_via_proxy), background-task execution (_run_background_task[_inner]), the cached-agent lifecycle (_init_cached_agent_for_turn,_refresh_agent_cache_message_count,_commit_memory_before_soft_evict,_commit_then_release_soft,_release_evicted_agent_soft,_enforce_agent_cache_cap,_sweep_idle_cached_agents), agent resource cleanup (_should_emit_long_running_notification,_defer_agent_cleanup_until_future_done,_cleanup_agent_resources_off_loop,_cleanup_agent_resources), and the gateway-owned executor (_run_in_executor_with_context,_get_executor,_shutdown_executor).GatewayRunnernow readsclass GatewayRunner(GatewayTurnExecMixin, GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, GatewaySlashCommandsMixin)— the new mixin is first in the bases; everyself.*call site resolves unchanged via the MRO.Why
gateway/run.pyis a ~26.8k-line god file; the campaign (#54962) decomposes it into focused mixins/modules. This slice is behavior-neutral: every method is lifted verbatim. run.py shrinks from 26,824 to 23,855 lines (−2,969).How this stays behavior-neutral
run.py(verified by an automated compare againstHEAD~2). 14 lines that carried pre-existing trailing whitespace from run.py were whitespace-normalized in the new file (follow-up style commit) so the added file passesgit diff --check; run.py's own pre-existing trailing whitespace is untouched.git diff --checkover the whole PR diff is clean.self._xdispatch is unchanged._load_gateway_config,_hermes_home,_resolve_gateway_model,_profile_runtime_scope,_platform_config_key, the_AGENT_*/_GATEWAY_*constants,TurnRunner, ...). Each such method gets a deferredfrom gateway.run import ...at the top of its body (the established pattern ingateway/slash_commands.py) — it resolves at call time whengateway.runis fully loaded, preserving monkeypatch semantics (gateway.run._hermes_homepatches in 20+ test files keep working).timeis lazy-imported the same way because tests replacegateway.run.timewholesale.logger = logging.getLogger("gateway.run")(same as the kanban/slash mixins) keeps log-record names unchanged.from gateway.run import— no import cycle; the mixin imports only stdlib + direct deps at module top.How to test
Import smoke:
import gateway.run; assert hasattr(gateway.run.GatewayRunner, '_run_agent_inner')and all 21 moved methods report__module__ == 'gateway.turn_exec_mixin'withGatewayTurnExecMixinfirst inGatewayRunner.__mro__.Platforms tested
scripts/check-windows-footguns.pyon the new module: clean.Scope note
GatewayRunnerstays. Two class-level section comments that described the moved proxy cluster travel with it; the_CLEANUP_TIMEOUT_Sclass attribute stays onGatewayRunner(still referenced viaselfby the moved cleanup methods through the MRO).from run_agent import AIAgent); no run.py imports were pruned (other methods may still use them).tests/gateway/test_fallback_chain_reload.py) pins the location of the twofallback_model=self._refresh_fallback_model()agent-construction sites; both sites moved into the mixin, so the test now counts acrossgateway/run.py+gateway/turn_exec_mixin.py(same invariant, same pattern as the TurnRunner extraction commit 1a3a9de).Part of #54962
Part of #55138
Part of #78207
Part of #78647
Part of #78791