refactor(tool_presets): merge Tool + ToolPreset from models.py into tool_presets.py

Per the 4-criteria decision rule: Tool + ToolPreset fail C1 (only used by
tool_presets + tool_bias), fail C2 (no state machine), fail C3 (no
dedicated test file), borderline C4 (~15 lines each). MERGE into the
existing src/tool_presets.py which already has ToolPresetManager.

This commit:
 1. Adds Tool + ToolPreset class definitions to src/tool_presets.py at
    the top (after the stdlib imports). Both classes are used by
    ToolPresetManager and the tests.
 2. Removes Tool + ToolPreset from src/models.py.
 3. Adds lazy re-exports via the existing __getattr__ in src/models.py
    (EAGER import would deadlock because src.tool_presets imports
    BiasProfile from src.models; the lazy __getattr__ breaks the cycle).
 4. Updates src/tool_presets.py import: from
    'from src.models import ToolPreset, BiasProfile' to
    'from src.models import BiasProfile' (ToolPreset is now local).

Verification: VC8 (Tool + ToolPreset)
  from src.tool_presets import Tool, ToolPreset  # OK
  from src.models        import Tool, ToolPreset  # OK (lazy __getattr__)
  Tool is Tool returns True
  ToolPreset is ToolPreset returns True

Tests verified (7/7 PASS):
  tests/test_tool_preset_manager.py (4 tests)
  tests/test_bias_models.py (3 tests)

Consumer check:
  src/ai_client.py: from src.models import FileItem, ToolPreset, BiasProfile, Tool
  src/app_controller.py: (no Tool/ToolPreset import)
  src/tool_bias.py: from src.models import Tool, ToolPreset, BiasProfile
  All resolve via re-export/lazy __getattr__.

The lazy __getattr__ pattern is the same mechanism used for the
Pydantic proxies (GenerateRequest / ConfirmRequest) and for PROVIDERS.
Phase 5 will migrate Tool/ToolPreset to a similar lazy pattern in
the re-export block (or drop them entirely after the consumer
migration).
This commit is contained in:
ed
2026-06-26 10:07:22 -04:00
parent 86f1676721
commit 6adaae2ec3
2 changed files with 67 additions and 66 deletions
+51 -4
View File
@@ -1,11 +1,58 @@
import tomllib
import tomli_w
from pathlib import Path
from typing import Dict, List, Optional, Union, Any
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional, Union, Any
from src import paths
from src.models import ToolPreset, BiasProfile
from src import paths
from src.models import BiasProfile
from src.type_aliases import Metadata
@dataclass
class Tool:
name: str
approval: str = 'auto'
weight: int = 3
parameter_bias: Dict[str, str] = field(default_factory=dict)
def to_dict(self) -> Metadata:
return {
"name": self.name,
"approval": self.approval,
"weight": self.weight,
"parameter_bias": self.parameter_bias,
}
@classmethod
def from_dict(cls, data: Metadata) -> "Tool":
return cls(
name=data["name"],
approval=data.get("approval", "auto"),
weight=data.get("weight", 3),
parameter_bias=data.get("parameter_bias", {}),
)
@dataclass
class ToolPreset:
name: str
categories: Dict[str, List[Union[Tool, Any]]] = field(default_factory=dict)
def to_dict(self) -> Metadata:
serialized_categories = {}
for cat, tools in self.categories.items():
serialized_categories[cat] = [t.to_dict() if isinstance(t, Tool) else t for t in tools]
return {"categories": serialized_categories}
@classmethod
def from_dict(cls, name: str, data: Metadata) -> "ToolPreset":
raw_categories = data.get("categories", {})
parsed_categories = {}
for cat, tools in raw_categories.items():
parsed_categories[cat] = [Tool.from_dict(t) if isinstance(t, dict) else t for t in tools]
return cls(name=name, categories=parsed_categories)
class ToolPresetManager: