Skip to content

refactor: simplify large functions & improve code-search clarity (follow-up to #2) #6

Description

@Hrt-Htk

Follow-up to #2 / #5. The module split landed the structural separation (one place for router lifecycle, no cross-file duplication, concern-named files), but two of #2's original goals were deliberately deferred to keep that pass mechanical and behaviour-preserving:

  • functions are still large (no <40-line target met)
  • proxy.py is still ~720 lines (the <100-line entry-point target was not met)

As discussed, the current code separates concerns by file but is still only so-so for code search / readability — the big handlers are monoliths, so grepping for a behaviour lands you in the middle of a 240-line function rather than a focused, well-named unit.

Goals

Improve simplification and code-search clarity without behaviour changes (same mechanical discipline as #2: byte-identical logic, verified by AST diff + live smoke test).

Tasks

  • Split proxy_request (~240 lines in proxy.py) into named, greppable helpers — e.g. dead-worker retry, upstream forward, streaming-response wiring, request/response logging.
  • Split ChatRouterManager.recover_worker (~190 lines in router_manager.py) into focused steps (probe / cycle / reload / verify).
  • Slim the proxy.py entry point toward <100 lines by moving chat handlers (proxy_request, embed_forward, models_handler, props_handler) and preset generation (pick_setup, _model_preset_section, write_preset) into dedicated modules.
  • router_http free-function dedup — extract the repeated auth-header + POST/poll boilerplate in _status / unload / _load_locked into shared helpers (the one Phase-2 item intentionally skipped; care needed to preserve ChatRouterManager's exact _loaded_at timing).
  • Code-search clarity — consistent section markers, function-level docstrings on the new smaller units, and a short module-map (or expand CLAUDE.md's) so "where does X live" is answerable by grep/symbol search.

Acceptance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions