Skip to content

refactor: extract item validators from _preflight_codex_input_items#23952

Closed
kshitijk4poor wants to merge 2 commits into
NousResearch:mainfrom
kshitijk4poor:refactor/codex-preflight-input
Closed

refactor: extract item validators from _preflight_codex_input_items#23952
kshitijk4poor wants to merge 2 commits into
NousResearch:mainfrom
kshitijk4poor:refactor/codex-preflight-input

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Split the 210-line _preflight_codex_input_items (C901 complexity 51) into five focused validators:

Function Item type Complexity
_preflight_codex_input_items Type dispatcher 51 → <10
_validate_function_call_item function_call <10
_validate_function_call_output_item function_call_output 12
_validate_reasoning_item reasoning <10
_validate_message_item message 11
_validate_role_content_item user/assistant content 14

Zero logic changes — pure extraction. Each validator handles one item type's validation + normalization + append.

Part 2 of 4 for codex_responses_adapter.py.

Depends on: #23951 (refactor/codex-chat-to-responses) — same file.

…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.
Split the 210-line validator into five focused functions, one per item type:

- _validate_function_call_item()      — call_id, name, arguments
- _validate_function_call_output_item() — output (string or multimodal array)
- _validate_reasoning_item()          — encrypted content with dedup
- _validate_message_item()            — role=assistant with content parts
- _validate_role_content_item()       — bare role+content items

Main dispatcher complexity: 51 → <10 (C901 cleared).
Zero logic changes — pure extraction.

Depends on: refactor/codex-chat-to-responses (same file)
@alt-glitch alt-glitch added type/refactor Code restructuring, no behavior change comp/agent Core agent loop, run_agent.py, prompt builder P3 Low — cosmetic, nice to have labels May 11, 2026
@kshitijk4poor

Copy link
Copy Markdown
Collaborator Author

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 tests/run_agent/ and tests/agent/test_codex_responses_adapter.py. The bisect-during-debug argument is real but doesn't justify +139 LOC in already-covered code.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P3 Low — cosmetic, nice to have type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants