Files
hermes-agent/gateway/status_lock_dir.py
Teknium 861a0299ed refactor(gateway): no re-export shims for the split status/base helpers; the lock split is one-way
The round-1 health offset moved safe_url_for_log and the scoped-lock
release pair into siblings but re-exported them from gateway.status /
gateway.platforms.base, and status_scoped_locks read gateway.status
privates at call time while gateway.status re-exported the sibling's
privates (_unusable_lock_dir, _warned_unusable_lock_dirs). AGENTS.md:
no re-export shims for internal moves.

- gateway/status_lock_dir.py: the unusable-lock-dir report
  (ScopedLockDirUnusable + public unusable_lock_dir / _recovered /
  reset / reported). gateway.status imports it; it imports nothing back.
- gateway/status_scoped_locks.py: only the release pair, reading
  gateway.status at call time; gateway.status no longer imports it.
- Every importer points at the real home: base.py, run_bootstrap.py,
  the Feishu and Slack adapters, and the tests that patch them
  (gateway.status_scoped_locks.release_*). safe_url_for_log comes from
  gateway.platforms.base_log_safety; base.py imports it only for its
  own use.
- classify_changes: status_lock_dir.py rides with gateway/status.py on
  the e2e_upgrade lane.

code_health: 0 blocking (status.py stays under its main value).

Review finding by @unsupportedpastels on #140490 (minor 4) and by
@alt-glitch on #140490 (suggestion 4)
Refs #134889 (W2)
Refs #140337
2026-10-17 08:03:53 -07:00

51 lines
2.2 KiB
Python

"""The unusable scoped-lock dir report (#134889 W2). :mod:`gateway.status` raises it from
``acquire_scoped_lock``; nothing here reads back into that module."""
from __future__ import annotations
import logging
from pathlib import Path
from hermes_dir_acl import unusable_dir_remedy
log = logging.getLogger("gateway.status")
class ScopedLockDirUnusable(PermissionError):
"""This token cannot create, read or replace locks in the scoped-lock dir (#134889 W2).
On Windows an elevated install/update creates ``gateway-locks`` owned by Administrators with
no entry for the user; the same user's non-elevated gateway then gets ``Permission denied`` on
every token lock. The adapter reports it once and names the repair; it stays retryable (the
dir is shared by every token-locked adapter), so a repair is picked up without a restart. A
``PermissionError`` subclass so existing callers keep their handling.
"""
# Lock dirs already reported unusable by this process: WARNING once per dir, DEBUG after, since the
# reconnect watcher and every adapter sharing the dir re-enter the acquire path.
_warned: set[Path] = set()
def unusable_lock_dir(lock_dir: Path, exc: PermissionError) -> ScopedLockDirUnusable:
"""Log *lock_dir* as unusable (WARNING once, then DEBUG) and return the error to raise."""
level = logging.DEBUG if lock_dir in _warned else logging.WARNING
_warned.add(lock_dir)
log.log(level, "Gateway lock directory %s is not usable by this process (%s); it was likely created "
"by an elevated (administrator) Hermes. Platforms that need a token lock stay down until it "
"is repaired. To repair, %s", lock_dir, exc, unusable_dir_remedy(lock_dir))
return ScopedLockDirUnusable(exc.errno, f"gateway lock directory is not usable: {exc}", str(lock_dir))
def unusable_lock_dir_recovered(lock_dir: Path) -> None:
"""A lock was taken in *lock_dir*: warn again if it ever becomes unusable later."""
_warned.discard(lock_dir)
def reset_unusable_lock_dir_reports() -> None:
"""Forget every report (a fresh process's view; tests)."""
_warned.clear()
def unusable_lock_dir_reported(lock_dir: Path) -> bool:
return lock_dir in _warned