refactor(hermes_time,registration_lifecycle,toolset_distributions): inline single-use helpers, flatten predecessor walk, pack distribution table; -27 LOC

This commit is contained in:
Teknium
2026-09-02 22:02:12 -07:00
parent cf378b812f
commit 48c34eb194
3 changed files with 40 additions and 67 deletions

View File

@@ -61,16 +61,6 @@ def _resolve_timezone_name() -> str:
return "" return ""
def _get_zoneinfo(name: str) -> Optional[ZoneInfo]:
if not name:
return None
try:
return ZoneInfo(name)
except Exception as exc:
logger.warning("Invalid timezone '%s': %s. Falling back to server local time.", name, exc)
return None
def get_timezone() -> Optional[ZoneInfo]: def get_timezone() -> Optional[ZoneInfo]:
"""Return the active profile's configured ZoneInfo, or None (server-local).""" """Return the active profile's configured ZoneInfo, or None (server-local)."""
cache_identity = _timezone_cache_identity() cache_identity = _timezone_cache_identity()
@@ -81,7 +71,12 @@ def get_timezone() -> Optional[ZoneInfo]:
# Resolve outside the lock (config file I/O); first writer wins so concurrent resolvers of the # Resolve outside the lock (config file I/O); first writer wins so concurrent resolvers of the
# same identity converge on one ZoneInfo object. # same identity converge on one ZoneInfo object.
name = _resolve_timezone_name() name = _resolve_timezone_name()
tz = _get_zoneinfo(name) tz = None
if name:
try:
tz = ZoneInfo(name)
except Exception as exc:
logger.warning("Invalid timezone '%s': %s. Falling back to server local time.", name, exc)
with _cache_lock: with _cache_lock:
return _tz_cache.setdefault(cache_identity, (name, tz))[1] return _tz_cache.setdefault(cache_identity, (name, tz))[1]

View File

@@ -50,10 +50,8 @@ class ReplacementCoordinator:
with self._lock: with self._lock:
yield yield
def acquire( def acquire(self, slot: Hashable, *, current: Any, previous: Any, restore: Callable[[Any], bool],
self, slot: Hashable, *, current: Any, previous: Any, restore: Callable[[Any], bool], finalize: Callable[[], None] | None = None) -> ReplacementLease:
finalize: Callable[[], None] | None = None,
) -> ReplacementLease:
"""Attach a new live generation to the matching active predecessor.""" """Attach a new live generation to the matching active predecessor."""
with self._lock: with self._lock:
leases = self._active.setdefault(slot, []) leases = self._active.setdefault(slot, [])
@@ -63,7 +61,10 @@ class ReplacementCoordinator:
return lease return lease
def dispose(self, lease: ReplacementLease) -> None: def dispose(self, lease: ReplacementLease) -> None:
"""Remove *lease*, restoring the nearest still-live predecessor.""" """Remove *lease*, restoring the nearest still-live predecessor.
``restore`` -> ``finalize`` -> slot pruning each run even when an earlier step raises.
"""
with self._lock: with self._lock:
if not lease.active: if not lease.active:
return return
@@ -75,25 +76,20 @@ class ReplacementCoordinator:
try: try:
try: try:
if latest is lease: if latest is lease:
replacement = lease.previous # Restore the nearest still-live predecessor, else the last dead generation's previous.
predecessor = lease.predecessor replacement, predecessor = lease.previous, lease.predecessor
while predecessor is not None: while predecessor is not None and not predecessor.active:
if predecessor.active: replacement, predecessor = predecessor.previous, predecessor.predecessor
replacement = predecessor.current lease.restore(predecessor.current if predecessor is not None else replacement)
break
replacement = predecessor.previous
predecessor = predecessor.predecessor
lease.restore(replacement)
finally: finally:
if lease.finalize is not None: if lease.finalize is not None:
lease.finalize() lease.finalize()
finally: finally:
if leases: live = [item for item in leases if item.active]
live = [item for item in leases if item.active] if live:
if live: self._active[lease.slot] = live
self._active[lease.slot] = live elif leases:
else: self._active.pop(lease.slot, None)
self._active.pop(lease.slot, None)
replacement_coordinator = ReplacementCoordinator() replacement_coordinator = ReplacementCoordinator()

View File

@@ -15,29 +15,20 @@ def _dist(description: str, **toolsets: int) -> Dict[str, object]:
DISTRIBUTIONS = { DISTRIBUTIONS = {
"default": _dist("All available tools, all the time", "default": _dist("All available tools, all the time", web=100, vision=100, image_gen=100, terminal=100, file=100, browser=100),
web=100, vision=100, image_gen=100, terminal=100, file=100, browser=100), "image_gen": _dist("Heavy focus on image generation with vision and web support", image_gen=90, vision=90, web=55, terminal=45),
"image_gen": _dist("Heavy focus on image generation with vision and web support", "research": _dist("Web research with vision analysis and reasoning", web=90, browser=70, vision=50, terminal=10),
image_gen=90, vision=90, web=55, terminal=45),
"research": _dist("Web research with vision analysis and reasoning",
web=90, browser=70, vision=50, terminal=10),
"science": _dist("Scientific research with web, terminal, file, and browser capabilities", "science": _dist("Scientific research with web, terminal, file, and browser capabilities",
web=94, terminal=94, file=94, vision=65, browser=50, image_gen=15), web=94, terminal=94, file=94, vision=65, browser=50, image_gen=15),
"development": _dist("Terminal, file tools, and reasoning with occasional web lookup", "development": _dist("Terminal, file tools, and reasoning with occasional web lookup", terminal=80, file=80, web=30, vision=10),
terminal=80, file=80, web=30, vision=10), "safe": _dist("All tools except terminal for safety", web=80, browser=70, vision=60, image_gen=60),
"safe": _dist("All tools except terminal for safety", "balanced": _dist("Equal probability of all toolsets", web=50, vision=50, image_gen=50, terminal=50, file=50, browser=50),
web=80, browser=70, vision=60, image_gen=60),
"balanced": _dist("Equal probability of all toolsets",
web=50, vision=50, image_gen=50, terminal=50, file=50, browser=50),
"minimal": _dist("Only web tools for basic research", web=100), "minimal": _dist("Only web tools for basic research", web=100),
"terminal_only": _dist("Terminal and file tools for code execution tasks", terminal=100, file=100), "terminal_only": _dist("Terminal and file tools for code execution tasks", terminal=100, file=100),
"terminal_web": _dist("Terminal and file tools with web search for documentation lookup", "terminal_web": _dist("Terminal and file tools with web search for documentation lookup", terminal=100, file=100, web=100),
terminal=100, file=100, web=100),
"creative": _dist("Image generation and vision analysis focus", image_gen=90, vision=90, web=30), "creative": _dist("Image generation and vision analysis focus", image_gen=90, vision=90, web=30),
"reasoning": _dist("Heavy research/reasoning distribution with minimal other tools", "reasoning": _dist("Heavy research/reasoning distribution with minimal other tools", web=90, file=60, terminal=20),
web=90, file=60, terminal=20), "browser_use": _dist("Full browser-based web interaction with search, vision, and page control", browser=100, web=80, vision=70),
"browser_use": _dist("Full browser-based web interaction with search, vision, and page control",
browser=100, web=80, vision=70),
"browser_only": _dist("Only browser automation tools for pure web interaction tasks", browser=100), "browser_only": _dist("Only browser automation tools for pure web interaction tasks", browser=100),
# browser-use-tasks.jsonl: the browser toolset includes web_search since Google blocks direct browser searches # browser-use-tasks.jsonl: the browser toolset includes web_search since Google blocks direct browser searches
"browser_tasks": _dist( "browser_tasks": _dist(
@@ -45,15 +36,11 @@ DISTRIBUTIONS = {
browser=97, vision=12, terminal=15, browser=97, vision=12, terminal=15,
), ),
# nous-terminal-tasks.jsonl # nous-terminal-tasks.jsonl
"terminal_tasks": _dist( "terminal_tasks": _dist("Terminal-focused distribution with high terminal/file availability, occasional other tools",
"Terminal-focused distribution with high terminal/file availability, occasional other tools", terminal=97, file=97, web=97, browser=75, vision=50, image_gen=10),
terminal=97, file=97, web=97, browser=75, vision=50, image_gen=10,
),
# mixed-browser-terminal-tasks.jsonl # mixed-browser-terminal-tasks.jsonl
"mixed_tasks": _dist( "mixed_tasks": _dist("Mixed distribution with high browser, terminal, and file availability for complex tasks",
"Mixed distribution with high browser, terminal, and file availability for complex tasks", browser=92, terminal=92, file=92, web=35, vision=15, image_gen=15),
browser=92, terminal=92, file=92, web=35, vision=15, image_gen=15,
),
} }
@@ -66,6 +53,10 @@ def list_distributions() -> Dict[str, Dict]:
return DISTRIBUTIONS.copy() return DISTRIBUTIONS.copy()
def validate_distribution(distribution_name: str) -> bool:
return distribution_name in DISTRIBUTIONS
def sample_toolsets_from_distribution(distribution_name: str) -> List[str]: def sample_toolsets_from_distribution(distribution_name: str) -> List[str]:
"""Sample toolset names, each included independently with its % probability. """Sample toolset names, each included independently with its % probability.
@@ -75,34 +66,25 @@ def sample_toolsets_from_distribution(distribution_name: str) -> List[str]:
dist = get_distribution(distribution_name) dist = get_distribution(distribution_name)
if not dist: if not dist:
raise ValueError(f"Unknown distribution: {distribution_name}") raise ValueError(f"Unknown distribution: {distribution_name}")
selected_toolsets = [] selected_toolsets = []
for toolset_name, probability in dist["toolsets"].items(): for toolset_name, probability in dist["toolsets"].items():
if not validate_toolset(toolset_name): if not validate_toolset(toolset_name):
print(f"⚠️ Warning: Toolset '{toolset_name}' in distribution '{distribution_name}' is not valid") print(f"⚠️ Warning: Toolset '{toolset_name}' in distribution '{distribution_name}' is not valid")
continue elif random.random() * 100 < probability:
if random.random() * 100 < probability:
selected_toolsets.append(toolset_name) selected_toolsets.append(toolset_name)
if not selected_toolsets and dist["toolsets"]: if not selected_toolsets and dist["toolsets"]:
highest_prob_toolset = max(dist["toolsets"].items(), key=lambda x: x[1])[0] highest_prob_toolset = max(dist["toolsets"].items(), key=lambda x: x[1])[0]
if validate_toolset(highest_prob_toolset): if validate_toolset(highest_prob_toolset):
selected_toolsets.append(highest_prob_toolset) selected_toolsets.append(highest_prob_toolset)
return selected_toolsets return selected_toolsets
def validate_distribution(distribution_name: str) -> bool:
return distribution_name in DISTRIBUTIONS
def print_distribution_info(distribution_name: str) -> None: def print_distribution_info(distribution_name: str) -> None:
"""Print a distribution's description and toolset probabilities (highest first).""" """Print a distribution's description and toolset probabilities (highest first)."""
dist = get_distribution(distribution_name) dist = get_distribution(distribution_name)
if not dist: if not dist:
print(f"❌ Unknown distribution: {distribution_name}") print(f"❌ Unknown distribution: {distribution_name}")
return return
print(f"\n📊 Distribution: {distribution_name}") print(f"\n📊 Distribution: {distribution_name}")
print(f" Description: {dist['description']}") print(f" Description: {dist['description']}")
print(" Toolsets:") print(" Toolsets:")