Private
Public Access
0
0
Files
manual_slop/src/tool_bias.py
T
ed ecd8e82f2f refactor(tool_bias): merge BiasProfile from models.py into tool_bias.py
Per the 4-criteria decision rule: BiasProfile fails C1 (only used by
tool_presets + tool_bias), fails C2 (no state machine), fails C3 (no
dedicated test file), borderline C4. MERGE into the existing
src/tool_bias.py which already has ToolBiasEngine.

This commit:
 1. Adds BiasProfile class definition to src/tool_bias.py at the top
    (after the dataclass + typing imports).
 2. Removes BiasProfile from src/models.py.
 3. Adds lazy re-export via the existing __getattr__ in src/models.py
    (EAGER would deadlock: tool_presets needs BiasProfile + tool_bias
    needs Tool/ToolPreset, and both want models re-exports).
 4. Updates src/tool_presets.py to use the local-import pattern for
    BiasProfile (in load_all_bias_profiles) + adds
    'from __future__ import annotations' so the 'BiasProfile' type
    annotation is a string. This breaks the cycle.
 5. Updates src/tool_bias.py to import Tool + ToolPreset from
    src.tool_presets directly (no longer through models) + adds
    'from __future__ import annotations'.

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

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

Cycle resolution:
  models -> tool_presets (lazy via __getattr__)
  tool_presets -> tool_bias (local import in function body, only at call time)
  tool_bias -> tool_presets (eager; OK because tool_presets is fully
                              loaded by the time tool_bias's class
                              definitions need Tool/ToolPreset)
  The eager load of tool_bias from tool_presets is what made the
  'from __future__ import annotations' necessary in both files (for
  Tool/ToolPreset string annotations in tool_bias method signatures).
2026-06-26 10:10:28 -04:00

94 lines
3.0 KiB
Python

from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from src.tool_presets import Tool, ToolPreset
from src.type_aliases import Metadata
@dataclass
class BiasProfile:
name: str
tool_weights: Dict[str, int] = field(default_factory=dict)
category_multipliers: Dict[str, float] = field(default_factory=dict)
def to_dict(self) -> Metadata:
return {
"name": self.name,
"tool_weights": self.tool_weights,
"category_multipliers": self.category_multipliers,
}
@classmethod
def from_dict(cls, data: Metadata) -> "BiasProfile":
return cls(
name = data["name"],
tool_weights = data.get("tool_weights", {}),
category_multipliers = data.get("category_multipliers", {}),
)
class ToolBiasEngine:
def apply_semantic_nudges(self, tool_definitions: List[Dict[str, Any]], preset: ToolPreset) -> List[Dict[str, Any]]:
"""
[C: tests/test_tool_bias.py:test_apply_semantic_nudges, tests/test_tool_bias.py:test_parameter_bias_nudging]
"""
weight_map = {
5: "[HIGH PRIORITY] ",
4: "[PREFERRED] ",
2: "[NOT RECOMMENDED] ",
1: "[LOW PRIORITY] "
}
preset_tools: Dict[str, Tool] = {}
for cat_tools in preset.categories.values():
for t in cat_tools:
if isinstance(t, Tool):
preset_tools[t.name] = t
for defn in tool_definitions:
name = defn.get("name")
if name in preset_tools:
tool = preset_tools[name]
prefix = weight_map.get(tool.weight, "")
if prefix:
defn["description"] = prefix + defn.get("description", "")
if tool.parameter_bias:
params = defn.get("parameters") or defn.get("input_schema")
if params and "properties" in params:
props = params["properties"]
for p_name, bias in tool.parameter_bias.items():
if p_name in props:
p_desc = props[p_name].get("description", "")
props[p_name]["description"] = f"[{bias}] {p_desc}".strip()
return tool_definitions
def generate_tooling_strategy(self, preset: ToolPreset, global_bias: BiasProfile) -> str:
"""
[C: tests/test_tool_bias.py:test_generate_tooling_strategy]
"""
lines = ["### Tooling Strategy"]
preferred = []
low_priority = []
for cat_tools in preset.categories.values():
for t in cat_tools:
if not isinstance(t, Tool): continue
if t.weight >= 5: preferred.append(f"{t.name} [HIGH PRIORITY]")
elif t.weight == 4: preferred.append(f"{t.name} [PREFERRED]")
elif t.weight == 2: low_priority.append(f"{t.name} [NOT RECOMMENDED]")
elif t.weight <= 1: low_priority.append(f"{t.name} [LOW PRIORITY]")
if preferred: lines.append(f"Preferred tools: {', '.join(preferred)}.")
if low_priority: lines.append(f"Low-priority tools: {', '.join(low_priority)}.")
if global_bias.category_multipliers:
lines.append("Category focus multipliers:")
for cat, mult in global_bias.category_multipliers.items():
lines.append(f"- {cat}: {mult}x")
return "\n\n".join(lines)