Skip to content
Back to skills

2158 2026 02 04 Main Agent Refactoring C3de294f

ASecurity

Refactored `src/tunacode/core/agents/main.py` from 641 lines to 389 lines (39% reduction) by extracting focused modules and removing dead code.

  • 9 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added October 11, 2026
code-qualitynoderefactoringdocumentation

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 2158-2026-02-04-main-agent-refactoring_c3de294f --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 2158 2026 02 04 Main Agent Refactoring C3de294f?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 2158 2026 02 04 Main Agent Refactoring C3de294f
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-2158-2026-02-04-main-agent-refactoring-c3de294f/badge)](https://www.skillsdirectory.com/skills/tools-only-2158-2026-02-04-main-agent-refactoring-c3de294f)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
---
date: 2026-02-04
type: refactoring
scope: core/agents
impact: medium
---

# Main Agent Module Refactoring

## Summary

Refactored `src/tunacode/core/agents/main.py` from 641 lines to 389 lines (39% reduction) by extracting focused modules and removing dead code.

## Changes

### New Files Created

1. **`history_preparer.py`** - `HistoryPreparer` class
   - Encapsulates message history preparation logic
   - Handles pruning, cleanup, and sanitization
   - Single `prepare()` method returns ready-to-use history

2. **`request_logger.py`** - Logging utilities
   - `log_history_state()` - Logs message history for diagnostics
   - `log_sanitized_history_state()` - Logs sanitized history in debug mode
   - `log_run_handle_context_messages()` - Logs pydantic-ai context
   - `log_node_details()` - Logs node type information
   - `message_has_tool_calls()` - Helper for detecting tool calls

### Removed Dead Code

- `DotDict` class - Unused utility class
- `colors` variable - Never referenced
- `IterationManager` class - Trivially simple, inlined into `_handle_iteration_node()`

### Cleaned Up

- `EmptyResponseHandler.prompt_action()` - Replaced inline `StateProxy` class with `@dataclass StateView`
- Simplified counter tracking logic in `EmptyResponseHandler.track()`

## Rationale

The original `main.py` had accumulated multiple responsibilities:
- Request initialization
- Message history preparation (7+ methods)
- Logging (7+ methods)
- Agent iteration loop
- Node handling
- Abort cleanup

This violated single responsibility principle and made the file difficult to navigate.

## Architecture Compliance

All new modules follow layer dependency rules:
- `core` modules only import from `tools`, `utils`, `types`, and `infrastructure`
- No imports from `ui` layer

Verified by passing `tests/architecture/test_layer_dependencies.py`.

## Updated Documentation

- `docs/codebase-map/modules/core-agents.md` - Updated exports and component list
- `docs/architecture/agent-loop.md` - Updated line references and file list

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.