The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
chore(sessions): meet code-health on the round-3 browse catch-up
Tekniumf9ba374d13eb
diff --git a/hermes_state_reconcile.py b/hermes_state_reconcile.pyindex 4beac40299a..80f90b6e634 100644--- a/hermes_state_reconcile.py+++ b/hermes_state_reconcile.py@@ -176,7 +176,7 @@ def _lockless_gateway_holder(path: Path) -> str | None:try:import psutilargv = psutil.Process(pid).cmdline() or None- except Exception:+ except (ImportError, OSError, psutil.Error): # gone, denied, or no psutil: not provably oursargv = Noneif argv and _looks_like_hermes(argv) and 'gateway' in argv[1:]:return describe_holder_pid(pid)@@ -365,6 +365,32 @@ def _reserve_for_ddl(home: Path, path: Path, owned):yield schema_drift_reason(path) is None+def _verify_clean_before_lock(path: Path, too_slow):+ """The pre-lock phase of the browse catch-up: a full read-only integrity scan OUTSIDE gateway.lock+ (it may be slow on a large store and must never block a gateway boot). Returns the file signature+ the scan vouches for. Raises :class:`CorruptStoreError` for a damaged store (latched, so polls do+ not rescan it) and ``too_slow()`` when the scan cannot finish in its budget (remembered for the+ same signature, so a polling sidebar does not rescan a multi-GB file every few seconds)."""+ from hermes_state_health import STORAGE_CORRUPT, mark_storage_corrupt, storage_corrupt_reason, storage_state+ if storage_state(path) == STORAGE_CORRUPT:+ raise CorruptStoreError(storage_corrupt_reason(path) or 'latched corrupt')+ key = str(path.resolve())+ signature = _store_signature(path)+ if _scan_too_slow.get(key) == signature:+ raise too_slow()+ try:+ problem = _integrity_problem(path, _Budget(BROWSE_INTEGRITY_BUDGET_S))+ except BrowseBudgetExceeded as exc:+ _scan_too_slow[key] = signature+ raise too_slow() from exc+ if problem:+ # A failing column probe hides damage elsewhere in the file: additive DDL is not+ # corruption recovery, so never write into a store that is not verified clean.+ mark_storage_corrupt(path, problem)+ raise CorruptStoreError(problem)+ return signature++def reconcile_drift_for_browse(path: Path) -> bool:"""Catch a lagging store up for a read-only browse; True when DDL ran, False when already current.@@ -373,7 +399,6 @@ def reconcile_drift_for_browse(path: Path) -> bool:when the lagging store is also damaged. Never touches a store missing its history tables:recreating them empty would hide lost sessions."""from gateway.runtime_ownership import OwnershipConflict, live_runtime_owner- from hermes_state_health import STORAGE_CORRUPT, mark_storage_corrupt, storage_corrupt_reason, storage_statereason = schema_drift_reason(path)if reason is None:@@ -419,27 +444,10 @@ def reconcile_drift_for_browse(path: Path) -> bool:raise owned(reason) from excif holder := _lockless_gateway_holder(path):raise owned(reason, holder)- # Already latched corrupt in this process: do not rescan a damaged file on every poll.- if storage_state(path) == STORAGE_CORRUPT:- raise CorruptStoreError(storage_corrupt_reason(path) or 'latched corrupt')- key = str(path.resolve())with _path_scan_lock(path): # one scan per store per process; concurrent polls wait for itif schema_drift_reason(path) is None:return False- signature = _store_signature(path)- if _scan_too_slow.get(key) == signature:- raise too_slow()- # Outside gateway.lock: read-only, may be slow on a large store, never blocks a gateway boot.- try:- problem = _integrity_problem(path, _Budget(BROWSE_INTEGRITY_BUDGET_S))- except BrowseBudgetExceeded as exc:- _scan_too_slow[key] = signature- raise too_slow() from exc- if problem:- # A failing column probe hides damage elsewhere in the file: additive DDL is not- # corruption recovery, so never write into a store that is not verified clean.- mark_storage_corrupt(path, problem)- raise CorruptStoreError(problem)+ signature = _verify_clean_before_lock(path, too_slow)try:with _reserve_for_ddl(home, path, owned) as current:if current:diff --git a/tests/hermes_cli/test_browse_schema_drift.py b/tests/hermes_cli/test_browse_schema_drift.pyindex 6a2c5244a67..36ab943f04a 100644--- a/tests/hermes_cli/test_browse_schema_drift.py+++ b/tests/hermes_cli/test_browse_schema_drift.py@@ -740,7 +740,7 @@ def test_gateway_boot_never_waits_on_a_slow_integrity_scan(tmp_path):browse = subprocess.Popen([sys.executable, "-c", _SLOW_SCAN_BROWSE, str(path), "4"], env=os.environ,stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)assert browse.stdout.readline().strip() == "started"- time.sleep(1.5) # inside the 4 s scan+ time.sleep(1.5) # health: allow HX013 -- the child scan is a fixed 4 s stand-in; boot must land mid-scanbooting = ProfileOwnership()try:started = time.monotonic()@@ -770,7 +770,7 @@ class _SlowProgress:import timedef slow():- time.sleep(0.01)+ time.sleep(0.01) # health: allow HX013 -- the slowed VM IS the behaviour under test (cold disk)return handler() if handler is not None else 0return self._conn.set_progress_handler(slow, 5)