
    @=jJ	                        U d Z ddlmZ ddlmZ  ee                                          j        j        Zda	de
d<   ddZdd
ZddZddZdS )up  Detect when the gateway is running stale code after a hot ``git pull``.

The gateway is a single long-lived process; its ``sys.modules`` is frozen at
boot. If the checkout is updated underneath it (a manual ``git pull``, or the
window before ``hermes update``'s graceful restart fires), a first-time lazy
import on a new code path can resolve a freshly-pulled consumer module against a
stale cached dependency -> ImportError (see
``tests/test_stale_utils_module_import.py`` for the exact failure).

We snapshot the checkout revision at gateway startup and compare on demand, so
risky callers (e.g. ``/model`` switching) can refuse with a clear "restart the
gateway" message instead of crashing on a cryptic import error.

If the revision can't be read (non-git install, IO error), the boot snapshot
stays ``None`` and skew detection no-ops — it never produces a false positive.
    )annotations)PathN
str | None_boot_fingerprintreturnc                 R    	 ddl m}   | t                    S # t          $ r Y dS w xY w)zCurrent checkout fingerprint, reusing the CLI's git-rev reader.

    ``hermes_cli.main`` is always already imported in a gateway process (it's
    the entry point), so this import is free and avoids duplicating the
    worktree-aware ref resolution.
    r   _read_git_revision_fingerprintN)hermes_cli.mainr
   _PROJECT_ROOT	Exceptionr	   s    7/home/rurouni/.hermes/hermes-agent/gateway/code_skew.py_fingerprintr      sL    BBBBBB--m<<<   tts    
&&Nonec                 4    t           t                      a dS dS )z?Snapshot the checkout revision at gateway startup (idempotent).N)r   r        r   record_boot_fingerprintr   )   s"      (NN ! r   fingerprintstrc                    |                      dd          d         }|r#|dk    rt          |          dk    r
|dd         S |p| S )z<Render a ``git:<ref>:<sha>`` fingerprint as a compact label.:   
unresolved
   N)rsplitlen)r   shas     r   _shortr    0   sT    


S!
$
$R
(C
 sl""s3xx"}}3B3x+r   tuple[str, str] | Nonec                     t           dS t                      } | | t           k    rdS t          t                     t          |           fS )zcReturn ``(boot_rev, disk_rev)`` short labels if the checkout drifted
    since boot, else ``None``.N)r   r   r    )currents    r   detect_code_skewr$   8   sG      tnnG'%666t#$$fWoo55r   )r   r   )r   r   )r   r   r   r   )r   r!   )__doc__
__future__r   pathlibr   __file__resolveparentr   r   __annotations__r   r   r    r$   r   r   r   <module>r,      s     " # " " " " "      X&&((/6 $  $ $ $ $   + + + +   6 6 6 6 6 6r   