[agent_loop.py] 2158-line god file must be split per AGENTS.md ~400 line limit #664

Closed
opened 2026-06-02 23:37:56 +02:00 by sleepy · 0 comments
Owner

File: src/agent_loop.py (2158 lines)

AGENTS.md rule: "Split files that exceed ~400 lines or contain multiple concerns."

This file contains at least 6 distinct concerns:

  1. System prompt assembly (_AGENT_PREAMBLE, _AGENT_RULES, _API_AGENT_RULES, TOOL_SECTIONS, _assemble_prompt, _build_base_prompt, _build_system_prompt) — ~500 lines of prompt templates + assembly logic
  2. Tool selection / RAG retrieval (_detect_admin_intent, _recent_context_for_retrieval, _extract_last_user_message, tool-index integration) — mixed into stream_agent_loop
  3. Streaming agent loop (stream_agent_loop) — ~900 lines of the main loop
  4. Completion verifier subagent (_run_verifier_subagent, _build_actions_snapshot, _VERIFIER_*) — ~80 lines
  5. Metrics (_compute_final_metrics) — ~50 lines
  6. Document streaming (doc_stream_open/delta logic embedded in the loop) — ~100 lines scattered through the loop

Suggested split

  • src/agent/prompts.py — system prompt templates + assembly
  • src/agent/tool_selection.py — admin detection, RAG retrieval, keyword fallback
  • src/agent/streaming.py — the core stream_agent_loop (consumes the above)
  • src/agent/verifier.py — completion verifier
  • src/agent/metrics.py — metrics computation
  • src/agent/doc_streaming.py — document content streaming helpers

Each sub-module should be imported through a narrow interface. The existing agent_tools.py facade pattern is a good model.

## File: `src/agent_loop.py` (2158 lines) AGENTS.md rule: _"Split files that exceed ~400 lines or contain multiple concerns."_ This file contains at least **6 distinct concerns**: 1. **System prompt assembly** (`_AGENT_PREAMBLE`, `_AGENT_RULES`, `_API_AGENT_RULES`, `TOOL_SECTIONS`, `_assemble_prompt`, `_build_base_prompt`, `_build_system_prompt`) — ~500 lines of prompt templates + assembly logic 2. **Tool selection / RAG retrieval** (`_detect_admin_intent`, `_recent_context_for_retrieval`, `_extract_last_user_message`, tool-index integration) — mixed into `stream_agent_loop` 3. **Streaming agent loop** (`stream_agent_loop`) — ~900 lines of the main loop 4. **Completion verifier subagent** (`_run_verifier_subagent`, `_build_actions_snapshot`, `_VERIFIER_*`) — ~80 lines 5. **Metrics** (`_compute_final_metrics`) — ~50 lines 6. **Document streaming** (doc_stream_open/delta logic embedded in the loop) — ~100 lines scattered through the loop ### Suggested split - `src/agent/prompts.py` — system prompt templates + assembly - `src/agent/tool_selection.py` — admin detection, RAG retrieval, keyword fallback - `src/agent/streaming.py` — the core `stream_agent_loop` (consumes the above) - `src/agent/verifier.py` — completion verifier - `src/agent/metrics.py` — metrics computation - `src/agent/doc_streaming.py` — document content streaming helpers Each sub-module should be imported through a narrow interface. The existing `agent_tools.py` facade pattern is a good model.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
sleepy/odysseus#664
No description provided.