
    `gjJ	                        U d Z ddlmZ ddlmZ  ee      j                         j                  j                  Zda	de
d<   ddZddZdd	Zdd
Zy)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_fingerprintc                 F    	 ddl m}   | t              S # t        $ r Y y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    D/root/.hermes/venv/lib/python3.12/site-packages/gateway/code_skew.py_fingerprintr      s'    B-m<< s    	  c                 &    t         t               a yy)z?Snapshot the checkout revision at gateway startup (idempotent).N)r   r        r   record_boot_fingerprintr   )   s      (N !r   c                l    | j                  dd      d   }|r|dk7  rt        |      dkD  r|dd S |xs | S )z<Render a ``git:<ref>:<sha>`` fingerprint as a compact label.:   
unresolved
   N)rsplitlen)fingerprintshas     r   _shortr   0   sC    


S!
$R
(C
sl"s3x"}3Bx+r   c                 p    t         yt               } | 	| t         k(  ry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   s9      nG'%66#$fWo55r   )returnr   )r!   None)r   strr!   r#   )r!   ztuple[str, str] | None)__doc__
__future__r   pathlibr   __file__resolveparentr   r   __annotations__r   r   r   r    r   r   r   <module>r+      sO   " # X&&(//66 $ : $+6r   