Ë
    ãß²jæ  ã                   óˆ   — d Z ddlZddlmZmZ ddlmZ dZde	dee	   fd„Z
d	e	dee	   fd
„Zdd	e	dee   de	fd„Zdedefd„Zy)u  Snapshot post-processing for the browser tools: truncate-and-store of oversized
accessibility trees, model-boundary secret redaction, screenshot-path recovery.

Facade-owned state is read through ``_bt`` (``tools.browser_tool``, resolved per call) â€” no import cycle.
é    N)ÚAnyÚOptional)Úorigin)z6Screenshot saved to ['\"](?P<path>/[^'\"]+?\.png)['\"]z0Screenshot saved to (?P<path>/\S+?\.png)(?:\s|$)z(?P<path>/\S+?\.png)(?:\s|$)ÚtextÚreturnc                 óº   — | syt         D ]O  }t        j                  || «      }|sŒ|j                  d«      j	                  «       j	                  d«      }|sŒM|c S  y)zHExtract a screenshot file path from agent-browser human-readable output.NÚpathz'")Ú_SCREENSHOT_PATH_PATTERNSÚreÚsearchÚgroupÚstrip)r   ÚpatternÚmatchr	   s       ú@/home/ubuntu/.hermes/hermes-agent/tools/browser_tool_snapshot.pyÚ"_extract_screenshot_path_from_textr      sY   € áØä,ò ˆÜ—	‘	˜' 4Ó(ˆÚØ—;‘;˜vÓ&×,Ñ,Ó.×4Ñ4°UÓ;ˆDÚØ’ðð ó    Úsnapshot_textc                 ó   — 	 ddl }ddlm} ddlm}  || d¬«      }t        |«      t        j                  kD  r6|dt        j                   dt        j                  d›d	t        |«      d›d
�z   }ddlm	}m
}  |dd«      } ||d¬«       |j                  |j                  d«      «      j                  «       dd }|d|› d�z  }	 ||	|dd¬«       t        |	«      S # t        $ r*}
t        j                   j#                  d|
«       Y d}
~
yd}
~
ww xY w)uš  Write a full snapshot to cache/web and return its path (None on failure â€” best-effort).

    Mirrors ``web_tools._store_full_text``: cache/web is mounted read-only into
    remote backends, so read_file can page through the complete tree on any
    backend. The stored copy is force-redacted (page-rendered secrets must not
    hit disk unmasked) and named by content hash so identical snapshots dedupe.
    r   N)Úget_hermes_dir©Úredact_sensitive_textT©Úforcez 

[... stored copy truncated at ú,z
 chars of z ...])Úensure_spill_dirÚwrite_text_exclusivez	cache/webÚ	web_cacheF)Úprivatezutf-8é
   zbrowser-snapshot-z.txt)r   Ú	overwritez)Failed to store full browser snapshot: %s)ÚhashlibÚhermes_constantsr   Úagent.redactr   ÚlenÚ_btÚMAX_STORED_SNAPSHOT_CHARSÚtools.spill_safetyr   r   Úsha256ÚencodeÚ	hexdigestÚstrÚ	ExceptionÚloggerÚdebug)r   r"   r   r   Úcontentr   r   Ú	cache_dirÚdigestr	   Úexcs              r   Ú_store_full_snapshotr4   !   s  € ðÛÝ3Ý6á'¨¸TÔBˆÜˆw‹<œ#×7Ñ7Ò7àÐ6œ×6Ñ6Ð7Ø6´s×7TÑ7TÐUVÐ6Wð XÜ˜'“l 1Ð% Uð,ñ,ð ÷
 	Ná" ;°Ó<ˆ	Ù˜¨EÕ2Ø—‘ §¡¨wÓ 7Ó8×BÑBÓDÀSÀbÐIˆØÐ.¨v¨h°dÐ;Ñ;ˆñ 	˜T 7°EÀTÕJÜ�4‹yÐøÜò Ü�
‰
×ÑÐDÀcÔJÜûðús   ‚CC Ã	DÃ# DÄDÚ	max_charsc           	      óL  — |€t        j                  «       }t        | «      |k  r| S t        | «      }|rddlm}  ||«      }| j                  d«      }g }d}t        dt        |xs d«      z   |dz  «      }|D ]=  }|t        |«      z   dz   ||z
  kD  r n$|j                  |«       |t        |«      dz   z  }Œ? t        |«      t        |«      z
  }	|	dkD  rA|r*t        |«      dz   }
|j                  d|	› d	|› d
|
› d�«       n|j                  d|	› d�«       dj                  |«      S )u:  Truncate a snapshot at line boundaries (never mid-element) to ``max_chars``.

    Defaults to ``browser.snapshot_threshold``. The full snapshot is stored to
    cache/web and the appended note tells the agent how to page through it
    with read_file â€” element refs beyond the cut are in the file, not lost.
    r   )Úto_agent_visible_cache_pathÚ
én   Ú é   é   z
[... u9    more lines truncated â€” full snapshot: read_file path="z	" offset=z limit=200]z= more lines truncated, use browser_snapshot for full content])
r&   Úget_browser_snapshot_thresholdr%   r4   Útools.credential_filesr7   ÚsplitÚminÚappendÚjoin)r   r5   Ústored_pathr7   ÚlinesÚresultÚcharsÚreserveÚlineÚ	remainingÚ	next_lines              r   Ú_truncate_snapshotrK   E   sH  € ð ÐÜ×6Ñ6Ó8ˆ	Ü
ˆ=Ó˜YÒ&ØÐä& }Ó5€KÙåFÙ1°+Ó>ˆà×Ñ Ó%€EØ€FØ€Eô �#œ˜KÒ-¨2Ó.Ñ.°	¸Q±Ó?€GØò ˆØ”3�t“9Ñ˜qÑ  9¨wÑ#6Ò6ÙØ�‰�dÔØ”�T“˜Q‘Ñ‰ð	ô
 �E“
œS ›[Ñ(€IØ�1‚}ÙÜ˜F› a™ˆIØ�M‰MØ˜)˜ð %#Ø#. -¨y¸¸À;ðPõð
 �M‰M˜G I ;Ð.kÐlÔmØ�9‰9�VÓÐr   Úvaluec                 ó~  — ddl m} t        | t        «      r
 || d¬«      S t        | t        «      r| D �cg c]  }t        |«      ‘Œ c}S t        | t        «      rt        d„ | D «       «      S t        | t        «      r6| j                  «       D ��ci c]  \  }}t        |«      t        |«      “Œ c}}S | S c c}w c c}}w )zäForce-redact secrets in browser-originated data (snapshots, console, eval
    results can carry page-rendered keys/cookies/tokens). Tool output is a model
    boundary, so this applies even when global log redaction is disabled.r   r   Tr   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N)Ú_redact_browser_output)Ú.0Úitems     r   ú	<genexpr>z)_redact_browser_output.<locals>.<genexpr>z   s   è ø€ ÒD°dÔ+¨D×1ÑDùs   ‚)	r$   r   Ú
isinstancer,   ÚlistrP   ÚtupleÚdictÚitems)rL   r   rR   Úkeys       r   rP   rP   o   s¥   € õ 3ä�%œÔÙ$ U°$Ô7Ð7Ü�%œÔØ9>Ö?°Ô& tÕ,Ò?Ð?Ü�%œÔÜÑD¸eÔDÓDÐDÜ�%œÔØ[`×[fÑ[fÓ[h×iÉiÈcÐSWÔ& sÓ+Ô-CÀDÓ-IÑIÓiÐiØ€Lùò @ùó js   µB4ÂB9rO   )Ú__doc__r   Útypingr   r   Útools.browser_tool_originr   r&   r
   r,   r   r4   ÚintrK   rP   © r   r   ú<module>r_      s~   ðñó 
ß  Ý 3ðÐ ð¨Sð °X¸c±]ó ð!¨ð !°¸±ó !ñH' cð '°h¸s±mð 'Èsó 'ðT #ð ¨#ô r   