
    UAj*	                        % S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  \	" S5      r
SSS	S	S
.rS\S'   SS jrSS jrSS jrSS jrg)uE  Runtime state persistence, kept separate from static config.

Static configuration lives in ``/etc/wolfpanel/agent.conf`` which is ALSO
consumed by systemd as an ``EnvironmentFile``, so it must stay clean, stable
KEY=VALUE data. Mutable runtime values — status, last error, the
registration-pending and revoked flags — live here in
``/var/lib/wolfpanel/state.json`` instead, so they never pollute the
EnvironmentFile and never break systemd parsing.

All persisted string values are sanitized to a single line so a multi-line
error message can never corrupt the on-disk state.
    )annotationsN)Path)Any)
get_loggerstate F)status
last_errorregistration_pendingrevokeddict[str, Any]_DEFAULT_STATEc                    [        U [        5      (       a?  SR                  U R                  SS5      R                  SS5      R	                  5       5      $ U $ )z6Collapse runtime strings to a safe, single-line value. 
)
isinstancestrjoinreplacesplit)values    /D:\Programs\laragon\www\wolf\agent\src\state.py	_sanitizer       sD    %xxdC088sCIIKLLL    c                    [        [        5      n [        R                  " U R	                  SS95      n[        U[         5      (       a  UR                  U5        U$ ! [        [        4 a     U$ f = f)z:Load runtime state, falling back to defaults on any error.utf-8encoding)	dictr   jsonloads	read_textr   updateOSError
ValueError)pathr   datas      r   
load_stater)   '   sh     Ezz$..'.:;dD!!LL L Z  Ls   A	A A0/A0c                   UR                  5        VVs0 sH  u  p#U[        U5      _M     nnn U R                  R                  SSS9  U R	                  U R
                  S-   5      nUR                  [        R                  " USS9SS9  UR                  U 5        g	s  snnf ! [         a   n[        R                  SX5         S	nAg	S	nAff = f)
z/Persist runtime state atomically. Never raises.T)parentsexist_okz.tmp   )indentr   r   z!failed to persist state to %s: %sN)itemsr   parentmkdir	with_namename
write_textr!   dumpsr   r%   logwarning)r'   r   keyr   cleantmpexcs          r   
save_stater<   3   s    5:[[]C]zsS)E""]ECD$6nnTYY/0tzz%2WED D  D7CCDs   BA,B" "
C,CCc                ~    [        U 5      nUR                  5        H  u  p4[        U5      X#'   M     [        X5        U$ )z=Merge ``changes`` into the persisted state and write it back.)r)   r/   r   r<   )r'   changesr   r8   r   s        r   update_stater?   ?   s7    tEmmo
u%
 &tLr   )r   r   returnr   )r'   r   r@   r   )r'   r   r   r   r@   None)r'   r   r>   r   r@   r   )__doc__
__future__r   r!   pathlibr   typingr   loggerr   r6   r   __annotations__r   r)   r<   r?    r   r   <module>rI      sV    #     !	" 		Dr   