
    `gjs(                        d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZ ddddZdd	dd
ZddZddZdddd	 	 	 	 	 	 	 	 	 ddZddZ	 dddddd	 	 	 	 	 	 	 	 	 	 	 ddZy)uA  
`hermes computer-use doctor` — thin client for cua-driver's `health_report` MCP tool.

cua-driver owns the health model (#1908 / be761fac on `main`). This module
just drives the stdio JSON-RPC handshake, calls `health_report`, and
renders the structured response. When the driver gets new checks, they
flow through here without code changes on the Hermes side — the only
contract is the stable `schema_version="1"` payload shape.

Exit code conventions:
- 0: overall == "ok"
- 1: overall in ("degraded", "failed")
- 2: driver binary missing / unreachable / protocol error
    )annotationsN)AnyDictListOptionalSequenceu   ✅u   ❌u   ⏭️passfailskipu   ⚠️)okdegradedfailedc                 n    	 ddl m}   |        S # t        $ r t        t        j
                        cY S w xY w)a+  cua-driver child env with the Hermes telemetry policy applied.

    Delegates to ``cua_backend.cua_driver_child_env`` (telemetry disabled by
    default unless the user opts in). Falls back to the current environment
    if that import fails, so doctor never breaks on a telemetry-helper error.
    r   cua_driver_child_env)tools.computer_use.cua_backendr   	Exceptiondictosenvironr   s    L/root/.hermes/venv/lib/python3.12/site-packages/tools/computer_use/doctor.py_cua_child_envr   (   s1     G#%%  BJJ s    "44c                 V    t               } 	 ddlm}  ||       S # t        $ r | cY S w xY w)uG  Telemetry-policy env with Hermes provider secrets stripped.

    cua-driver is a third-party binary — it must never inherit provider
    API keys (#53503/#55709/#58889 lineage). Falls back to the unsanitized
    telemetry env if the sanitizer can't be imported, so doctor keeps
    working in stripped-down environments.
    r   )_sanitize_subprocess_env)r   tools.environments.localr   r   )envr   s     r   _sanitized_cua_envr   7   s3     
CE',, 
s    (( g      (@)includer   timeoutc                  i }|rt        |      |d<   |rt        |      |d<   t        j                  | dgt        j                  t        j                  t        j                  ddddt	               	      }	 |j
                  j                  t        j                  d	dd
i d      dz          |j
                  j                          |j                  j                         }|sO|j                  j                         xs dj                         j                         dd }t!        d|xs d       |j
                  j                  t        j                  d	ddd|dd      dz          |j
                  j                          |j                  j                         }|st!        d      	 	 |j
                  j#                          	 |j'                  |       	 t        j,                  |      }	d|	v rt!        d|	d          |	j3                  d      xs i }|j3                  d      }t5        |t6              r|S |j3                  dg       D ]W  }|j3                  d       d!k(  s|j3                  d!d      }	 t        j,                  |      }t5        |t6              rd"|v r|c S Y t!        d#t        |j9                                      # t$        $ r Y w xY w# t        j(                  $ r$ |j+                          |j'                          Y :w xY w# 	 |j
                  j#                          n# t$        $ r Y nw xY w	 |j'                  |       w # t        j(                  $ r# |j+                          |j'                          Y w w xY wxY w# t.        t0        f$ r}
t!        d|
 d|dd        d}
~
ww xY w# t.        t0        f$ r Y w xY w)$u  Spawn `<binary> mcp`, perform the JSON-RPC handshake, call
    `health_report`, and return the parsed `structuredContent` dict.

    Raises `RuntimeError` on a protocol-level failure (binary crash,
    malformed response, JSON-RPC error). Never raises on a `health_report`
    that has failing checks — the tool's contract is to always return a
    well-formed report with `overall` set, never to set `isError`.
    r    r   mcpTutf-8replace   )stdinstdoutstderrtextencodingerrorsbufsizer   z2.0
initialize)jsonrpcidmethodparams
 Nz=cua-driver mcp produced no initialize response. stderr tail: z(empty)   z
tools/callhealth_report)name	argumentszAcua-driver mcp closed stdout without responding to health_report.)r!   z+health_report response was not valid JSON: z
raw:    errorzhealth_report JSON-RPC error: resultstructuredContentcontenttyper*   schema_versionzghealth_report response carried neither structuredContent nor a parseable JSON text block. Result keys: )list
subprocessPopenPIPEr   r'   writejsondumpsflushr(   readliner)   readstrip
splitlinesRuntimeErrorcloser   waitTimeoutExpiredkillloads
ValueError	TypeErrorget
isinstancer   keys)binaryr    r   r!   argsproc	init_linestderr_tail	call_lineresper<   scitemr*   parseds                   r   _drive_health_reportrc   H   s    Dw-YDzV 	oo 
D"

A"b%
   	 	

KK((*	;;++-3::<GGI"#NK  + 8y9;  	

A".TB%
  	 	
 	

KK((*	bcc 	JJ	IIgI&
fzz)$ $;DM?KLLXXh%2F
 
'	(B"d	

9b) 	88Fv%88FB'DD)fd+0@F0J!M	 	))-fkkm)<(=	? I  		 (( 	IIKIIK		JJ 			IIgI&(( 	IIKIIK	 	" fH7S\]a^aSbRcdeef. 	* s   6D5L -K K N :*N?	KK3LLNL54N5	M>N MNMN3NNNNN<#N77N<?OOc                   | j                  dd      }| j                  dd      }| j                  dd      }| j                  dd      }t        j                  |d      }|r)|t        v r!d}d}d	}	d
}
d}|||	dj                  |d      }ndx}x}x}	x}
}d}t        | d| d| d| | |
 	       | j                  dg       D ];  }|j                  dd      }|j                  dd      }t        j                  |d      }|j                  d      xs d}|r/|	||dj                  |d      }t        d| d| | |
 d|        nt        d| d| d|        |j                  d      }|rt        d| | |
        |j                  d      }t	        |t
              s|s|j                         D ]G  \  }}t	        |t
        t        f      s|nt        j                  |      }t        d| | d| |
        I > |}y)zyRender the report in the same style as `cua-driver call health_report`
    would (one line per check + a summary footer).r@   ?platformdriver_versionoverallu   •z[31mz[33mz[32mz[0mz[2m)r   r   r   r4   z cua-driver z on u    — checksr8   statusmessager	   z   z: hintu
         → dataz      =N)
rU   _OVERALL_GLYPHprint_STATUS_GLYPHrV   r   itemsrA   rF   rG   )reportcolorschemarf   driver_vrh   header_glyphcol_red
col_yellow	col_green	col_resetcol_dimcol_forcheckr8   rj   glyphrk   
status_colrm   rn   keyvaluerendered_s                            r   _print_text_reportr      sF    ZZ(#.Fzz*c*Hzz*C0HjjC(G!%%gu5LN* 
		$*INRRSZ\^_ACCC*CyC9w	.XJd8*E)G9YK	)
 Hb) Eyy%8S)!!&%0))I&,"!7Gc&"o  BugQzl4&2gYGHBugQtfBwi01yy Jwivi[9: yy dD!d"jjl E
U(254,(G5TZZX]M^wiuAhZ	{CDE)E. 	A    F)r    r   json_outputru   c               <   t         j                  t         j                  fD ]  }	 |j                  dd        | 	 ddlm}  |       } t        j                  |       }|st        d	| d
       t        d       y	 t        |||      }|rGt#        j$                  |t         j                  dd       t         j                  j'                  d       n6|t         j                  j)                         }t+        |t-        |             |j                  d      }
|
dv ryy# t        t
        f$ r Y w xY w# t        $ r' t        j                  j                  d      xs d} Y w xY w# t         $ r(}	t        d|	 t         j                         Y d}	~	yd}	~	ww xY w)a&  Resolve the cua-driver binary, call `health_report`, render the result.

    Honors `HERMES_CUA_DRIVER_CMD` via the same `_cua_driver_cmd()` resolver
    that `install_cua_driver` + the runtime backend use, so the doctor
    diagnoses what your `computer_use` toolset will actually invoke.
    r$   r%   )r+   r,   Nr   )_cua_driver_cmdHERMES_CUA_DRIVER_CMDz
cua-driverz&cua-driver: not installed (looked for z).z"  Run: hermes computer-use installr6   )r    r   z!cua-driver health_report failed: )fileT)indent	sort_keysr3   )ru   rh   )r   r   r&   )sysr(   r)   reconfigureAttributeErrorOSErrorhermes_cli.tools_configr   r   r   r   rU   shutilwhichrq   rc   rM   rF   dumprE   isattyr   bool)
driver_cmdr    r   r   ru   streamr   rX   rt   r_   rh   s              r   
run_doctorr      sr   ( ::szz* 		B
 	Q?(*J \\*%F6znBGH23%fgDI
 		&#**Q$?

=JJ%%'E6e5jj#G((A ( 		  	Q(?@PLJ	Q  1!5CJJGs:   D!D7 =E* !D43D47,E'&E'*	F3FF)returnzDict[str, str])
rX   strr    Sequence[str]r   r   r!   floatr   Dict[str, Any])rt   r   ru   r   r   None)N)r   zOptional[str]r    r   r   r   r   r   ru   zOptional[bool]r   int)__doc__
__future__r   rF   r   r   rB   r   typingr   r   r   r   r   rr   rp   r   r   rc   r   r   r   r   r   <module>r      s    #  	   
 6 6   (  hh h 	h
 h hV3n !%7   77 7 	7
 7 7 	7r   