
    UAj                        S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  SSKJr  \" S5      rSS	 jrSS
 jr S           SS jjrg)a  Discovery upload via the session flow (Phase 2).

Drives open-session -> per-category items -> complete/failed against the
Central API. Upload is best-effort: any transport failure is logged and the
local last.json snapshot remains the fallback. A category whose upload fails is
reported in failed_categories so the backend records the session as partial
(never silently dropped, never reconciled away).
    )annotations)Any)	ApiClientApiError)Config)load_agent_token)
get_loggerzdiscovery.uploadc                    [        U [        5      (       a+  U  Vs/ sH  n[        U[        5      (       a  UOSU0PM      sn$ [        U [        5      (       a  U /$ SU 0/$ s  snf )zENormalize a category value into a list of dict items for raw storage.value)
isinstancelistdict)r   items     :D:\Programs\laragon\www\wolf\agent\src\discovery\upload.py_coerce_itemsr      sc    %PUVPU
4..WdOCPUVV%we Ws   $Ac                j    U R                  5        VVs0 sH  u  pU[        U5      _M     snn$ s  snnf )zAMap a full-discovery inventory dict to category -> list-of-items.)itemsr   )	inventorycategoryr   s      r   categories_from_inventoryr      s/    BK//BSTBSxHmE**BSTTTs   /Nc                   U=(       d    [        U 5      n[        UR                  5       5      n UR                  X%S.U5      nUS   n/ n	/ n
UR                  5        H,  u  p UR                  X{US.U5        U	R                  U5        M.      U	(       d  UR                  USS.U5      nOUR                  UU	U
S	.U5      n[        R                  S
UUUR                  S5      5        U$ ! [        [
        [        4 a   n[        R                  SU5         SnAgSnAff = f! [         a2  n[        R                  SX5        U
R                  U5         SnAM  SnAff = f! [         a   n[        R                  SU5         SnAgSnAff = f)zRun the full session flow for one scan. Returns the finalize response.

Never raises: returns None if the session could not even be opened.
)	scan_typeexpected_categories
session_idz!discovery session open failed: %sN)r   r   r   z(discovery items upload failed for %s: %szall category uploads failed)r   error)r   succeeded_categoriesfailed_categoriesz1discovery %s scan finalized: session=%s status=%sstatuszdiscovery finalize failed: %s)r   sortedkeysopen_discovery_sessionr   KeyError	TypeErrorlogwarningr   upload_discovery_itemsappendfail_discoverycomplete_discoveryinfoget)config
api_clientr   
categoriestokenexpectedopenedr   exc	succeededfailedr   r   results                 r   upload_inventoryr6   $   s    -%f-Ejoo'(H22#Eu
 L)

 IF%++-	$--)%P X& ...)4QRF
  22",,5)/
 F 	?JJx 		
 O h	* 7=  	$KKBHRMM(##	$6  3S9sH   C) "&D!AE  )D>DD!
E+'EE 
F
*FF
)r   r   returnzlist[dict[str, Any]])r   zdict[str, Any]r7   dict[str, list[dict[str, Any]]])N)r,   r   r-   r   r   strr.   r8   r/   z
str | Noner7   zdict[str, Any] | None)__doc__
__future__r   typingr   r-   r   r   r,   r   identityr   loggerr	   r$   r   r   r6        r   <module>rA      sv    #  *  % #$U === = 0	=
 = =r@   