refactor: extract helpers from _chat_messages_to_responses_input#23951
refactor: extract helpers from _chat_messages_to_responses_input#23951kshitijk4poor wants to merge 1 commit into
Conversation
…nput Split the 192-line dispatcher into three focused functions: - _convert_assistant_msg_to_responses_items() — reasoning replay, message item replay, content emission, and tool call conversion - _convert_tool_msg_to_responses_item() — tool result formatting - _chat_messages_to_responses_input() — thin loop dispatcher Complexity: 45 → <10 on the main dispatcher (C901 cleared). Zero logic changes — pure extraction.
|
Closing — re-evaluated against the original tracker goal in #23972 ("ruff complexity reduction"). The actual LOC accounting here is net positive (+26 / +66 / +85 / +139 across the stack), not negative. The C901 number drops (45/51/39/37 → all <22) come at a real cost: each extracted helper carries ~3-6 lines of decomposition tax (signature + docstring + return-plumbing for shared locals that the original monolithic function held in scope). The helpers themselves are real single-responsibility units, but Python's function-scope semantics don't give us a free way to split without that tax. For complexity reductions that trade LOC for testability, the bar is "is the function so untestable that the tax is worth it?" — and for these four codex functions, they're already exercised by the agent-loop integration tests in What's still moving forward from this tracker:
If a future refactor of these codex functions ends up net-negative on LOC (e.g. by collapsing repeated patterns or eliminating dead-code branches alongside the extraction), I'll reopen. |
Summary
Split the 192-line
_chat_messages_to_responses_input(C901 complexity 45) into three focused functions:_chat_messages_to_responses_input_convert_assistant_msg_to_responses_items_convert_tool_msg_to_responses_itemZero logic changes — pure extraction.
Part 1 of 4 for
codex_responses_adapter.pystructural refactoring.Test plan
tests/run_agent/test_run_agent.py(directly imports the refactored function)