refactor(tools): drop redundant lazy-import guards; _install_running helper; walrus collapses
This commit is contained in:
@@ -143,10 +143,7 @@ def _rel_to_skills_dir(skill_dir: Path) -> Optional[Path]:
|
||||
|
||||
def _skill_rel_path(skill_name: str) -> Optional[PurePosixPath]:
|
||||
"""The skill's path relative to ~/.hermes/skills/ (posix), or None."""
|
||||
try:
|
||||
from tools.skill_usage import _find_skill_dir
|
||||
except Exception:
|
||||
return None
|
||||
from tools.skill_usage import _find_skill_dir
|
||||
skill_dir = _find_skill_dir(skill_name)
|
||||
rel = _rel_to_skills_dir(skill_dir) if skill_dir is not None else None
|
||||
return PurePosixPath(rel.as_posix()) if rel is not None else None
|
||||
@@ -155,11 +152,8 @@ def _skill_rel_path(skill_name: str) -> Optional[PurePosixPath]:
|
||||
def is_sync_eligible(skill_name: str) -> bool:
|
||||
"""Sync candidate (before opt-in): local, NOT bundled/hub-installed/external, NOT under ``_org/``
|
||||
(enterprise content never rides a personal push). Mirrors the curator's exclusions."""
|
||||
try:
|
||||
from tools.skill_usage import is_bundled, is_hub_installed, _find_skill_dir
|
||||
from agent.skill_utils import is_external_skill_path
|
||||
except Exception:
|
||||
return False
|
||||
from tools.skill_usage import is_bundled, is_hub_installed, _find_skill_dir
|
||||
from agent.skill_utils import is_external_skill_path
|
||||
skill_dir = None if is_bundled(skill_name) or is_hub_installed(skill_name) else _find_skill_dir(skill_name)
|
||||
if skill_dir is None or is_external_skill_path(skill_dir):
|
||||
return False
|
||||
@@ -170,12 +164,8 @@ def is_sync_eligible(skill_name: str) -> bool:
|
||||
def list_synced_skill_names() -> List[str]:
|
||||
"""Sorted skill names that should sync: opt-in -> eligible skills with ``sync: true``;
|
||||
opt-out (``sync_default_opt_in()``) -> every eligible skill unless ``sync: false``."""
|
||||
try:
|
||||
from tools.skill_usage import load_usage
|
||||
except Exception:
|
||||
return []
|
||||
usage = load_usage() or {}
|
||||
flags = {n: rec.get("sync") for n, rec in usage.items() if isinstance(rec, dict)}
|
||||
from tools.skill_usage import load_usage
|
||||
flags = {n: rec.get("sync") for n, rec in (load_usage() or {}).items() if isinstance(rec, dict)}
|
||||
if sync_default_opt_in():
|
||||
names = [n for n in _all_local_skill_names() if flags.get(n) is not False and is_sync_eligible(n)]
|
||||
else:
|
||||
@@ -212,11 +202,9 @@ def _adopt_manifest_opt_ins(remote_manifest: Optional[Dict[str, bool]]) -> List[
|
||||
"""Enable local sync intent for skills the plane manifest enabled that are locally
|
||||
curation-eligible. Enables only -- a pull never silently disables. Returns adopted names."""
|
||||
adopted: List[str] = []
|
||||
if not remote_manifest:
|
||||
return adopted
|
||||
try:
|
||||
from tools.skill_usage import set_sync, is_curation_eligible, is_sync_enabled
|
||||
for sname, enabled in remote_manifest.items():
|
||||
for sname, enabled in (remote_manifest or {}).items():
|
||||
if enabled and is_curation_eligible(sname) and not is_sync_enabled(sname):
|
||||
set_sync(sname, True)
|
||||
adopted.append(sname)
|
||||
|
||||
+13
-11
@@ -360,8 +360,7 @@ def _resolve_locally(configured_path: str, *, warn_missing: bool) -> tuple[str |
|
||||
expanded = os.path.expanduser(configured_path)
|
||||
# An explicit (non-"tirith") path is authoritative: never auto-download a replacement.
|
||||
if configured_path != "tirith":
|
||||
found = expanded if _is_executable(expanded) else shutil.which(expanded)
|
||||
if found:
|
||||
if found := (expanded if _is_executable(expanded) else shutil.which(expanded)):
|
||||
_resolved_path = found
|
||||
return found, False
|
||||
if warn_missing:
|
||||
@@ -370,8 +369,7 @@ def _resolve_locally(configured_path: str, *, warn_missing: bool) -> tuple[str |
|
||||
return None, False
|
||||
# Always re-run the cheap local checks so a manual install is picked up even after a
|
||||
# previous network failure (a long-lived gateway recovers without restart).
|
||||
found = _find_local_tirith()
|
||||
if found:
|
||||
if found := _find_local_tirith():
|
||||
_set_resolved(found)
|
||||
_clear_install_failed()
|
||||
return found, False
|
||||
@@ -385,13 +383,15 @@ def _resolve_locally(configured_path: str, *, warn_missing: bool) -> tuple[str |
|
||||
return None, True
|
||||
|
||||
|
||||
def _record_install_result(installed: str | None, reason: str) -> None:
|
||||
def _record_install_result(installed: str | None, reason: str) -> str | None:
|
||||
"""Cache an install outcome in module state + disk marker; returns *installed*."""
|
||||
if installed:
|
||||
_set_resolved(installed)
|
||||
_clear_install_failed()
|
||||
else:
|
||||
_set_failed(reason)
|
||||
_mark_install_failed(reason)
|
||||
return installed
|
||||
|
||||
|
||||
def _resolve_tirith_path(configured_path: str) -> str:
|
||||
@@ -410,20 +410,22 @@ def _resolve_tirith_path(configured_path: str) -> str:
|
||||
if found or not may_install:
|
||||
return found or expanded
|
||||
# A background install is running: don't start a parallel one; fail-open until it finishes.
|
||||
if (_install_thread is not None and _install_thread.is_alive()) or _disk_marker_blocks_install():
|
||||
if _install_running() or _disk_marker_blocks_install():
|
||||
return expanded
|
||||
installed, reason = _install_tirith()
|
||||
_record_install_result(installed, reason)
|
||||
installed = _record_install_result(*_install_tirith())
|
||||
return installed or expanded
|
||||
|
||||
|
||||
def _install_running() -> bool:
|
||||
return _install_thread is not None and _install_thread.is_alive()
|
||||
|
||||
|
||||
def _background_install(*, log_failures: bool = True):
|
||||
"""Background thread target: download and install tirith."""
|
||||
with _install_lock:
|
||||
if _resolved_path is not None: # another thread resolved meanwhile
|
||||
return
|
||||
found = _find_local_tirith() # may have been installed by another process
|
||||
if found:
|
||||
if found := _find_local_tirith(): # may have been installed by another process
|
||||
_set_resolved(found)
|
||||
return
|
||||
_record_install_result(*_install_tirith(log_failures=log_failures))
|
||||
@@ -446,7 +448,7 @@ def ensure_installed(*, log_failures: bool = True):
|
||||
found, may_install = _resolve_locally(cfg["tirith_path"], warn_missing=False)
|
||||
if found or not may_install or _disk_marker_blocks_install():
|
||||
return found
|
||||
if _install_thread is None or not _install_thread.is_alive():
|
||||
if not _install_running():
|
||||
_install_thread = threading.Thread(target=_background_install, daemon=True,
|
||||
kwargs={"log_failures": log_failures})
|
||||
_install_thread.start()
|
||||
|
||||
Reference in New Issue
Block a user