
    i                     b   U d Z ddlZddlZddl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
 ddlmZ ddlmZ  ej        e          Z ej        dd          Zej        e         ed	<   d
eddfdZdedej        e         fdZdej        e         ddfdZddedefdZdZdZdZdZde de dZde de dZ dZ!dZ"dddd d!d"d#d$e"d%z   d&fe"d'z   d(fe"d)z   d*fe"d+z   d,fgZ#ej$        ej%        z  Z&d- e#D             Z'd.ede(fd/Z)d0ede*fd1Z+g d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBd#dCdDdEdFdGe dHfdIe dJfdGe  dKe! dLfdIe  dKe! dMfdNdOdPdQdRdSdTdUdVdWdXdYe  dKe! dZfd[d\d]d^d_d`dadbdcZ,dd e,D             Z-deedefdfZ.i Z/e*ee0e         f         edg<   e,D ]x\  Z1Z2 e.e1          Z3e2Z4e/5                    e4 e0                      6                    e4e3h           e/5                    e3 e0                      6                    e3e4h           ydhede0e         fdiZ7d.edefdjZ8d.ede(fdkZ9 ej:                    Z;i Z<e*ee*f         edl<   i Z=e*ee0f         edm<    e0            Z>e0e         edn<    e0            Z?e0edo<    G dp dq          Z@i ZAe*eeBf         edr<   i ZCe*eeDf         eds<   deddfdtZEdeddfduZF	 ddedwedxeGdeHfdyZIdedeGfdzZJded{e*fd|ZKdedhefd}ZLdeddfd~ZMdeddfdZNdeddfdZOdedeGfdZPdeGfdZQdedhedeGfdZRdhefdZSde0fdZTde0fdZUde0fdZV	 	 	 dd.ed0edeHdz  deGdef
dZWdefdZXde*fdZYdefdZZdeHfdZ[defdZ\d.ed0edefdZ]	 dd.edede*fdZ^de*defdZ_	 dd.edede*fdZ` eU             dS )a  Dangerous command approval -- detection, prompting, and per-session state.

This module is the single source of truth for the dangerous command system:
- Pattern detection (DANGEROUS_PATTERNS, detect_dangerous_command)
- Per-session approval state (thread-safe, keyed by session_key)
- Approval prompting (CLI interactive + gateway async)
- Smart approval via auxiliary LLM (auto-approve low-risk commands)
- Permanent allowlist persistence (config.yaml)
    N)Optional)cfg_get)is_truthy_valueapproval_session_key default_approval_session_key	hook_namereturnc                     	 ddl m} n# t          $ r Y dS w xY w	  || fi | dS # t          $ r'}t                              d| |           Y d}~dS d}~ww xY w)a{  Invoke a plugin lifecycle hook for the approval system.

    Lazy-imports the plugin manager to avoid circular imports (approval.py is
    imported very early, long before plugins are discovered). Never raises --
    plugin errors are logged and swallowed.

    Only fires for the two approval-specific hooks in VALID_HOOKS:
    pre_approval_request, post_approval_response.
    r   )invoke_hookNz$Approval hook %s dispatch failed: %s)hermes_cli.pluginsr   	Exceptionloggerdebug)r   kwargsr   excs       3/home/ubuntu/.hermes/hermes-agent/tools/approval.py_fire_approval_hookr   $   s    2222222    	MI((((((( M M M 	;YLLLLLLLLL	Ms    	 
	& 
AAAsession_keyc                 :    t                               | pd          S )z<Bind the active approval session key to the current context.r   )r
   setr   s    r   set_current_session_keyr   >   s     $$[%6B777    tokenc                 :    t                               |            dS )z/Restore the prior approval session key context.N)r
   reset)r   s    r   reset_current_session_keyr    C   s    &&&&&r   r	   c                 `    t                                           }|r|S ddlm}  |d|           S )a  Return the active session key, preferring context-local state.

    Resolution order:
    1. approval-specific contextvars (set by gateway before agent.run)
    2. session_context contextvars (set by _set_session_env)
    3. os.environ fallback (CLI, cron, tests)
    r   )get_session_envHERMES_SESSION_KEY)r
   getgateway.session_contextr"   )r	   r   r"   s      r   get_current_session_keyr&   H   sH     (++--K 777777?/999r   z$(?:~|\$home|\$\{home\})/\.ssh(?:/|$)z\(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)\.env\bz;(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*\.env(?:\.[^/\s"\'`]+)*)z0(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*config\.yaml)z(?:/etc/|/dev/sd||)z(?:z(?:\s*(?:&&|\|\||;).*)?$zp(?:^|[;&|\n`]|\$\()\s*(?:sudo\s+(?:-[^\s]+\s+)*)?(?:env\s+(?:\w+=\S*\s+)*)?(?:(?:exec|nohup|setsid|time)\s+)*\s*)z&\brm\s+(-[^\s]*\s+)*(/|/\*|/ \*)(\s|$)z#recursive delete of root filesystem)z\brm\s+(-[^\s]*\s+)*(/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/\*|/var|/var/\*|/bin|/bin/\*|/sbin|/sbin/\*|/boot|/boot/\*|/lib|/lib/\*)(\s|$)z$recursive delete of system directory)z-\brm\s+(-[^\s]*\s+)*(~|\$HOME)(/?|/\*)?(\s|$)z"recursive delete of home directory)z\bmkfs(\.[a-z0-9]+)?\bzformat filesystem (mkfs))z9\bdd\b[^\n]*\bof=/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*zdd to raw block device)z.>\s*/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*\bzredirect to raw block device)z(:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:z	fork bomb)z\bkill\s+(-[^\s]+\s+)*-1\bkill all processesz!(shutdown|reboot|halt|poweroff)\bzsystem shutdown/rebootzinit\s+[06]\bzinit 0/6 (shutdown/reboot)z*systemctl\s+(poweroff|reboot|halt|kexec)\bzsystemctl poweroff/rebootztelinit\s+[06]\bztelinit 0/6 (shutdown/reboot)c                 L    g | ]!\  }}t          j        |t                    |f"S  recompile	_RE_FLAGS.0patterndescriptions      r   
<listcomp>r4      s=        Z##[1  r   commandc                     t          |                                           }t          D ] \  }}|                    |          rd|fc S !dS )zCheck if a command matches the unconditional hardline blocklist.

    Returns:
        (is_hardline, description) or (False, None)
    T)FN) _normalize_command_for_detectionlowerHARDLINE_PATTERNS_COMPILEDsearch)r5   
normalized
pattern_rer3   s       r   detect_hardline_commandr=      sb     2'::@@BBJ#= ' '
KZ(( 	'+&&&&	'=r   r3   c                     ddd|  ddS )z5Build the standard block result for a hardline match.FTzBLOCKED (hardline): u   . This command is on the unconditional blocklist and cannot be executed via the agent — not even with --yolo, /yolo, approvals.mode=off, or cron approve mode. If you genuinely need to run it, run it yourself in a terminal outside the agent.)approvedhardlinemessager+   )r3   s    r   _hardline_block_resultrB      s/     ;   	  r   )z\brm\s+(-[^\s]*\s+)*/zdelete in root path)z\brm\s+-[^\s]*rzrecursive delete)z\brm\s+--recursive\bzrecursive delete (long flag))z8\bchmod\s+(-[^\s]*\s+)*(777|666|o\+[rwx]*w|a\+[rwx]*w)\bz world/other-writable permissions)z8\bchmod\s+--recursive\b.*(777|666|o\+[rwx]*w|a\+[rwx]*w)z*recursive world/other-writable (long flag))z\bchown\s+(-[^\s]*)?R\s+rootzrecursive chown to root)z\bchown\s+--recursive\b.*rootz#recursive chown to root (long flag))z\bmkfs\bzformat filesystem)z\bdd\s+.*if=z	disk copy)z>\s*/dev/sdzwrite to block device)z\bDROP\s+(TABLE|DATABASE)\bzSQL DROP)z \bDELETE\s+FROM\b(?!.*\bWHERE\b)zSQL DELETE without WHERE)z\bTRUNCATE\s+(TABLE)?\s*\wzSQL TRUNCATE)z	>\s*/etc/zoverwrite system config)z8\bsystemctl\s+(-[^\s]+\s+)*(stop|restart|disable|mask)\bzstop/restart system service)z\bkill\s+-9\s+-1\br)   )z\bpkill\s+-9\bzforce kill processes)z%\b(bash|sh|zsh|ksh)\s+-[^\s]*c(\s+|$)zshell command via -c/-lc flag)z)\b(python[23]?|perl|ruby|node)\s+-[ec]\s+zscript execution via -e/-c flag)z\b(curl|wget)\b.*\|\s*(ba)?sh\bzpipe remote content to shell)z1\b(bash|sh|zsh|ksh)\s+<\s*<?\s*\(\s*(curl|wget)\bz.execute remote script via process substitutionz\btee\b.*["\']?zoverwrite system file via teez>>?\s*["\']?z%overwrite system file via redirectionz["\']?z$overwrite project env/config via teez,overwrite project env/config via redirection)z\bxargs\s+.*\brm\bzxargs with rm)z\bfind\b.*-exec\s+(/\S*/)?rm\bzfind -exec rm)z\bfind\b.*-delete\bzfind -delete)z%\bhermes\s+gateway\s+(stop|restart)\bz2stop/restart hermes gateway (kills running agents))z\bhermes\s+update\bz6hermes update (restarts gateway, kills running agents))z4gateway\s+run\b.*(&\s*$|&\s*;|\bdisown\b|\bsetsid\b)Mstart gateway outside systemd (use 'systemctl --user restart hermes-gateway'))z\bnohup\b.*gateway\s+run\brC   )z1\b(pkill|killall)\b.*\b(hermes|gateway|cli\.py)\bz.kill hermes/gateway process (self-termination))z\bkill\b.*\$\(\s*pgrep\bz3kill process via pgrep expansion (self-termination))z\bkill\b.*`\s*pgrep\bz<kill process via backtick pgrep expansion (self-termination))z\b(cp|mv|install)\b.*\s/etc/zcopy/move file into /etc/z\b(cp|mv|install)\b.*\s["\']?z!overwrite project env/config file)z\bsed\s+-[^\s]*i.*\s/etc/zin-place edit of system config)z\bsed\s+--in-place\b.*\s/etc/z*in-place edit of system config (long flag))z#\b(python[23]?|perl|ruby|node)\s+<<zscript execution via heredoc)z\bgit\s+reset\s+--hard\bz/git reset --hard (destroys uncommitted changes))z\bgit\s+push\b.*--force\bz(git force push (rewrites remote history))z\bgit\s+push\b.*-f\bz3git force push short flag (rewrites remote history))z\bgit\s+clean\s+-[^\s]*fz.git clean with force (deletes untracked files))z\bgit\s+branch\s+-D\bzgit branch force delete)z\bchmod\s+\+x\b.*[;&|]+\s*\./z(chmod +x followed by immediate executionc                 L    g | ]!\  }}t          j        |t                    |f"S r+   r,   r0   s      r   r4   r4     s=        Z##[1  r   r2   c                 T    d| v r|                      d          d         n	| dd         S )zIReproduce the old regex-derived approval key for backwards compatibility.z\b   N   )split)r2   s    r   _legacy_pattern_keyrI   $  s0    &+w&6&67==""GCRCLHr   _PATTERN_KEY_ALIASESpattern_keyc                 :    t                               | | h          S )zReturn all approval keys that should match this pattern.

    New approvals use the human-readable description string, but older
    command_allowlist entries and session approvals may still contain the
    historical regex-derived key.
    )rJ   r$   rK   s    r   _approval_key_aliasesrN   1  s      ##K+???r   c                 ~    ddl m}  ||           } |                     dd          } t          j        d|           } | S )a  Normalize a command string before dangerous-pattern matching.

    Strips ANSI escape sequences (full ECMA-48 via tools.ansi_strip),
    null bytes, and normalizes Unicode fullwidth characters so that
    obfuscation techniques cannot bypass the pattern-based detection.
    r   )
strip_ansi r   NFKC)tools.ansi_striprP   replaceunicodedata	normalize)r5   rP   s     r   r7   r7   ?  sQ     ,+++++ j!!Goofb))G#FG44GNr   c                     t          |                                           }t          D ]#\  }}|                    |          r	|}d||fc S $dS )zCheck if a command matches any dangerous patterns.

    Returns:
        (is_dangerous, pattern_key, description) or (False, None, None)
    T)FNN)r7   r8   DANGEROUS_PATTERNS_COMPILEDr:   )r5   command_lowerr<   r3   rK   s        r   detect_dangerous_commandrZ   Q  sl     5W==CCEEM#> 4 4
K]++ 	4%K+{3333	4 r   _pending_session_approved_session_yolo_permanent_approvedc                   "    e Zd ZdZdZdefdZdS )_ApprovalEntryz@One pending dangerous-command approval inside a gateway session.)eventdataresultrb   c                 R    t          j                    | _        || _        d | _        d S N)	threadingEventra   rb   rc   )selfrb   s     r   __init__z_ApprovalEntry.__init__v  s#    _&&
	%)r   N)__name__
__module____qualname____doc__	__slots__dictri   r+   r   r   r`   r`   r  s:        JJ+I*T * * * * * *r   r`   _gateway_queues_gateway_notify_cbsc                 Z    t           5  |t          | <   ddd           dS # 1 swxY w Y   dS )ua  Register a per-session callback for sending approval requests to the user.

    The callback signature is ``cb(approval_data: dict) -> None`` where
    *approval_data* contains ``command``, ``description``, and
    ``pattern_keys``.  The callback bridges sync→async (runs in the agent
    thread, must schedule the actual send on the event loop).
    N)_lockrq   )r   cbs     r   register_gateway_notifyru     sy     
 . .+-K(. . . . . . . . . . . . . . . . . .    $$c                     t           5  t                              | d           t                              | g           }ddd           n# 1 swxY w Y   |D ]}|j                                         dS )zUnregister the per-session gateway approval callback.

    Signals ALL blocked threads for this session so they don't hang forever
    (e.g. when the agent run finishes or is interrupted).
    N)rs   rq   poprp   ra   r   r   entriesentrys      r   unregister_gateway_notifyr|     s     
 7 7T222!%%k2667 7 7 7 7 7 7 7 7 7 7 7 7 7 7    s   7AAAFchoiceresolve_allc                    t           5  t                              |           }|s	 ddd           dS |r$t          |          }|                                 n|                    d          g}|st                              | d           ddd           n# 1 swxY w Y   |D ]"}||_        |j                                         #t          |          S )aT  Called by the gateway's /approve or /deny handler to unblock
    waiting agent thread(s).

    When *resolve_all* is True every pending approval in the session is
    resolved at once (``/approve all``).  Otherwise only the oldest one
    is resolved (FIFO).

    Returns the number of approvals resolved (0 means nothing was pending).
    Nr   )
rs   rp   r$   listclearrx   rc   ra   r   len)r   r}   r~   queuetargetsr{   s         r   resolve_gateway_approvalr     s1    
 
3 
3##K00 	
3 
3 
3 
3 
3 
3 
3 
3  	%5kkGKKMMMMyy||nG 	3T222
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3   w<<s   BABBBc                     t           5  t          t                              |                     cddd           S # 1 swxY w Y   dS )zFCheck if a session has one or more blocking gateway approvals waiting.N)rs   boolrp   r$   r   s    r   has_blocking_approvalr     s    	 6 6O''44556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   '<A A approvalc                 Z    t           5  |t          | <   ddd           dS # 1 swxY w Y   dS )z/Store a pending approval request for a session.N)rs   r[   )r   r   s     r   submit_pendingr     sv    	 ) ) () ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )rv   c                     t           5  t                              | t                                                    |           ddd           dS # 1 swxY w Y   dS )z(Approve a pattern for this session only.N)rs   r\   
setdefaultr   add)r   rK   s     r   approve_sessionr     s    	 J J$$[#%%88<<[IIIJ J J J J J J J J J J J J J J J J Js   ;AAAc                     | sdS t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z,Enable YOLO bypass for a single session key.N)rs   r]   r   r   s    r   enable_session_yolor     s     	 ' '+&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '   488c                     | sdS t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z-Disable YOLO bypass for a single session key.N)rs   r]   discardr   s    r   disable_session_yolor     s     	 + +k***+ + + + + + + + + + + + + + + + + +r   c                 l   | sdS t           5  t                              | d           t                              |            t
                              | d           t                              | g           }ddd           n# 1 swxY w Y   |D ]"}d|_        |j        	                                 #dS )z7Remove all approval and yolo state for a given session.Ndeny)
rs   r\   rx   r]   r   r[   rp   rc   ra   r   ry   s      r   clear_sessionr     s     	 7 7k4000k***[$'''!%%k266	7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
    	 s   A,BBBc                 ^    | sdS t           5  | t          v cddd           S # 1 swxY w Y   dS )z?Return True when YOLO bypass is enabled for a specific session.FN)rs   r]   r   s    r   is_session_yolo_enabledr     s     u	 , ,m+, , , , , , , , , , , , , , , , , ,s   	"&&c                  <    t          t          d                    S )zEReturn True when the active approval session has YOLO bypass enabled.r   r   )r   r&   r+   r   r   is_current_session_yolo_enabledr     s    "#:2#F#F#FGGGr   c                 6   t          |          }t          5  t          d |D                       r	 ddd           dS t                              | t                                t          fd|D                       cddd           S # 1 swxY w Y   dS )zCheck if a pattern is approved (session-scoped or permanent).

    Accept both the current canonical key and the legacy regex-derived key so
    existing command_allowlist entries continue to work after key migrations.
    c              3   (   K   | ]}|t           v V  d S re   )r^   )r1   aliass     r   	<genexpr>zis_approved.<locals>.<genexpr>  s(      AAu++AAAAAAr   NTc              3       K   | ]}|v V  	d S re   r+   )r1   r   session_approvalss     r   r   zis_approved.<locals>.<genexpr>   s)      CC%5--CCCCCCr   )rN   rs   anyr\   r$   r   )r   rK   aliasesr   s      @r   is_approvedr     s    $K00G	 D DAAAAAAA 	D D D D D D D D .11+suuEECCCC7CCCCC	D D D D D D D D D D D D D D D D D Ds   B ABBBc                 z    t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z)Add a pattern to the permanent allowlist.N)rs   r^   r   rM   s    r   approve_permanentr     s    	 - -,,,- - - - - - - - - - - - - - - - - -   044patternsc                 z    t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z2Bulk-load permanent allowlist entries from config.N)rs   r^   update)r   s    r   load_permanentr   	  s    	 - -""8,,,- - - - - - - - - - - - - - - - - -r   c                     	 ddl m}   |             }t          |                    dg           pg           }|rt	          |           |S # t
          $ r3}t                              d|           t                      cY d}~S d}~ww xY w)zLoad permanently allowed command patterns from config.

    Also syncs them into the approval module so is_approved() works for
    patterns added via 'always' in a previous session.
    r   load_configcommand_allowlistz&Failed to load permanent allowlist: %sN)hermes_cli.configr   r   r$   r   r   r   warning)r   configr   es       r   load_permanent_allowlistr     s    	111111vzz"5r::@bAA 	%8$$$   ?CCCuus   AA
 

B(B<BBc                     	 ddl m}m}  |            }t          |           |d<    ||           dS # t          $ r&}t
                              d|           Y d}~dS d}~ww xY w)z4Save permanently allowed command patterns to config.r   )r   save_configr   zCould not save allowlist: %sN)r   r   r   r   r   r   r   )r   r   r   r   r   s        r   save_permanent_allowlistr   &  s    :>>>>>>>>&*8nn"#F : : :5q999999999:s   /3 
A#AA#Ttimeout_secondsallow_permanentc                   
 |t                      }|D	  || |          S # t          $ r(}t                              d|d           Y d}~dS d}~ww xY w	 ddlm}  |            t                              d	| |           dS n# t          $ r Y nw xY wd
t          j        d<   	 	 t                       t          d|            t          d|             t                       rt          d           nt          d           t                       t          j                                         ddi

fd}t          j        |d          }|                                 |                    |           |                                rYt          d           	 dt          j        v rt          j        d= t                       t          j                                         dS 
d         }	|	dv rYt          d           	 dt          j        v rt          j        d= t                       t          j                                         dS |	dv rYt          d           	 dt          j        v rt          j        d= t                       t          j                                         dS |	dv rsYt          d           	 dt          j        v rt          j        d= t                       t          j                                         dS t          d           	 dt          j        v rt          j        d= t                       t          j                                         dS t          d           	 dt          j        v rt          j        d= t                       t          j                                         dS # t$          t&          f$ rZ t          d            Y dt          j        v rt          j        d= t                       t          j                                         dS w xY w# dt          j        v rt          j        d= t                       t          j                                         w xY w)!a  Prompt the user to approve a dangerous command (CLI only).

    Args:
        allow_permanent: When False, hide the [a]lways option (used when
            tirith warnings are present, since broad permanent allowlisting
            is inappropriate for content-level security findings).
        approval_callback: Optional callback registered by the CLI for
            prompt_toolkit integration. Signature:
            (command, description, *, allow_permanent=True) -> str.

    Returns: 'once', 'session', 'always', or 'deny'
    N)r   zApproval callback failed: %sT)exc_infor   r   )get_app_or_nonezDangerous-command approval requested on a thread with no approval callback while prompt_toolkit is active; denying to avoid stdin deadlock. command=%r description=%r1HERMES_SPINNER_PAUSEu     ⚠️  DANGEROUS COMMAND: z      z2      [o]nce  |  [s]ession  |  [a]lways  |  [d]enyz%      [o]nce  |  [s]ession  |  [d]enyr}   r   c                      	 rdnd} t          |                                                                           d<   d S # t          t          f$ r	 dd<   Y d S w xY w)Nz      Choice [o/s/a/D]: z      Choice [o/s/D]: r}   r   )inputstripr8   EOFErrorOSError)promptr   rc   s    r   	get_inputz,prompt_dangerous_approval.<locals>.get_input{  sz    *;Jh77PhF',V}}':':'<'<'B'B'D'DF8$$$ '* * * *')F8$$$$*s   <A AA)targetdaemontimeoutu$   
      ⏱ Timeout - denying command)oonceu         ✓ Allowed oncer   )ssessionu"         ✓ Allowed for this sessionr   )aalwaysu&         ✓ Added to permanent allowlistr   u         ✗ Deniedu   
      ✗ Cancelled)_get_approval_timeoutr   r   error"prompt_toolkit.application.currentr   r   osenvironprintsysstdoutflushrf   Threadstartjoinis_aliver   KeyboardInterrupt)r5   r3   r   r   approval_callbackr   r   r   threadr}   rc   s      `      @r   prompt_dangerous_approvalr   5  s     /11$	$$Wk5DF F F F 	 	 	LL7TLJJJ66666	FFFFFF?(NNE 	   6 )     		 *-BJ%&6,	GGG?+??@@@$7$$%%%GGG ?JKKKK=>>>GGGJ^F* * * * * * %YtDDDFLLNNNKKK000   =>>>. "RZ//
12
1 H%F&&.///$ "RZ//
12
) +++:;;;  "RZ//
12
# ?**& %>???$ "RZ//
12
 >??? "RZ//
12
 ())) "RZ//
12
 '(   %&&&!RZ//
12
 "RZ//
12
sj   $ 
AAA,B	 	
BB)C;O .O O 0O O (O  P,!P/ +P,,P/ /A	Q8c                     t          | t                    r| du rdndS t          | t                    r*|                                                                 }|pdS dS )a"  Normalize approval mode values loaded from YAML/config.

    YAML 1.1 treats bare words like `off` as booleans, so a config entry like
    `approvals:
  mode: off` is parsed as False unless quoted. Treat that as the
    intended string mode instead of falling back to manual approvals.
    Foffmanual)
isinstancer   strr   r8   )moder;   s     r   _normalize_approval_moder     sc     $ 4uu83$ &ZZ\\''))
%X%8r   c                      	 ddl m}   |             }|                    di           pi S # t          $ r'}t                              d|           i cY d}~S d}~ww xY w)zLRead the approvals config block. Returns a dict with 'mode', 'timeout', etc.r   r   	approvalsz"Failed to load approval config: %sN)r   r   r$   r   r   r   )r   r   r   s      r   _get_approval_configr     s    111111zz+r**0b0   ;Q???						s   '* 
AAAAc                  d    t                                          dd          } t          |           S )zHRead the approval mode from config. Returns 'manual', 'smart', or 'off'.r   r   )r   r$   r   )r   s    r   _get_approval_moder     s+    !!%%fh77D#D)))r   c                      	 t          t                                          dd                    S # t          t          f$ r Y dS w xY w)z>Read the approval timeout from config. Defaults to 60 seconds.r   <   )intr   r$   
ValueError	TypeErrorr+   r   r   r   r     sS    '))--i<<===	"   rrs   .1 AAc                      	 ddl m}   |             }t          t          |ddd                                                                                    }|dv rdS dS # t          $ r Y dS w xY w)	zERead the cron approval mode from config. Returns 'deny' or 'approve'.r   r   r   	cron_moder   r   )approver   allowyesr   )r   r   r   r   r8   r   r   )r   r   r   s      r   _get_cron_approval_moder     s    11111176;VLLLMMSSUU[[]]5559v   vvs   AA 
A,+A,c                 D   	 ddl m} d|  d| d} |dd|dgdd	
          }|j        d         j        j        pd                                                                }d|v rdS d|v rdS dS # t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w)a  Use the auxiliary LLM to assess risk and decide approval.

    Returns 'approve' if the LLM determines the command is safe,
    'deny' if genuinely dangerous, or 'escalate' if uncertain.

    Inspired by OpenAI Codex's Smart Approvals guardian subagent
    (openai/codex#13860).
    r   )call_llmzYou are a security reviewer for an AI coding agent. A terminal command was flagged by pattern matching as potentially dangerous.

Command: z
Flagged reason: ul  

Assess the ACTUAL risk of this command. Many flagged commands are false positives — for example, `python -c "print('hello')"` is flagged as "script execution via -c flag" but is completely harmless.

Rules:
- APPROVE if the command is clearly safe (benign script execution, safe file operations, development tools, package installs, git operations, etc.)
- DENY if the command could genuinely damage the system (recursive delete of important paths, overwriting system files, fork bombs, wiping disks, dropping databases, etc.)
- ESCALATE if you're uncertain

Respond with exactly one word: APPROVE, DENY, or ESCALATEr   user)rolecontent   )taskmessagestemperature
max_tokensr   APPROVEr   DENYr   escalatez1Smart approvals: LLM call failed (%s), escalatingN)
agent.auxiliary_clientr   choicesrA   r   r   upperr   r   r   )r5   r3   r   r   responseanswerr   s          r   _smart_approver    s   #333333=
= = = = = 8%&99:	
 
 
 "1%-5;BBDDJJLL9v6:   H!LLLzzzzzs   A#A/ 'A/ /
B9BBenv_typec           
         |dv rdddS t          |           \  }}|r3t                              d|| dd                    t          |          S t	          t          j        d                    st                      rdddS t          |           \  }}}|sdddS t                      }t          ||          rdddS t          j        d          }	t          j        d	          }
|	s6|
s4t          j        d
          rt                      dk    r	dd| ddS dddS |
st          j        d          r$t          || ||d           d|d| |d| d|  ddS t          | ||          }|dk    rdd| d||dS |dk    rt          ||           n9|dk    r3t          ||           t          |           t!          t"                     dddS )a  Check if a command is dangerous and handle approval.

    This is the main entry point called by terminal_tool before executing
    any command. It orchestrates detection, session checks, and prompting.

    Args:
        command: The shell command to check.
        env_type: Terminal backend type ('local', 'ssh', 'docker', etc.).
        approval_callback: Optional CLI callback for interactive prompts.

    Returns:
        {"approved": True/False, "message": str or None, ...}
    dockersingularitymodaldaytonavercel_sandboxTNr?   rA    Hardline block: %s (command: %s)   HERMES_YOLO_MODEHERMES_INTERACTIVEHERMES_GATEWAY_SESSIONHERMES_CRON_SESSIONr   F'BLOCKED: Command flagged as dangerous () but cron jobs run without a user present to approve it. Find an alternative approach that avoids this command. To allow dangerous commands in cron jobs, set approvals.cron_mode: approve in config.yaml.HERMES_EXEC_ASK)r5   rK   r3   approval_requiredu.   ⚠️ This command is potentially dangerous (z3). Asking the user for approval.

**Command:**
```

```r?   rK   statusr5   r3   rA   )r   zBBLOCKED: User denied this potentially dangerous command (matched 'zL' pattern). Do NOT retry this command - the user has explicitly rejected it.r?   rA   rK   r3   r   r   )r=   r   r   rB   r   r   getenvr   rZ   r&   r   r   r   r   r   r   r   r^   )r5   r  r   is_hardlinehardline_descis_dangerousrK   r3   r   is_cli
is_gatewayr}   s               r   check_dangerous_commandr)  	  s    RRR T222 "9!A!AK 59='RVSVRV-XXX%m444 ry!34455 39X9Z9Z 3 T222-Eg-N-N*L+{ 3 T222)++K;,, 3 T222Y+,,F344J 3* 39*++ 	&((F22 %G+ G G G	 	 	 !T222 
RY011 
{&&%
 %
 	 	 	 &)&V V VGNV V V

 

 
	
 'w9JL L LF  v\g  v  v  v&&	
 
 	
 [1111	8		[111+&&& !4555...r   tirith_resultc           	         |                      d          pg }|s|                      d          pd}d| S g }|D ]}|                     dd          }|                     dd          }|                     dd          }|r*|r(|                    |rd	| d
| d| n| d|            p|r|                    |rd	| d
| n|           |s|                      d          pd}d| S dd                    |          z   S )zBuild a human-readable description from tirith findings.

    Includes severity, title, and description for each finding so users
    can make an informed approval decision.
    findingssummaryzsecurity issue detectedzSecurity scan: severityr   titler3   [z] z: u   Security scan — ; )r$   appendr   )r*  r,  r-  partsfr.  r/  descs           r   _format_tirith_descriptionr6  p  so      ,,2H +##I..K2K****E I I55R((gr""uu]B'' 	IT 	ILLH\8X8888$888UJ\J\VZJ\J\]]]] 	ILLHG0X00000%HHH +##I..K2K****$))E"2"222r   c           
      L   |dv rdddS t          |           \  }}|r3t                              d|| dd                    t          |          S t	                      }t          t          j        d                    st                      s|dk    rdddS t          j        d	          }t          j        d
          }t          j        d          }|sM|sK|sIt          j        d          r0t                      dk    rt          |           \  }	}
}|	r	dd| ddS dddS dg dd}	 ddlm}  ||           }n# t          $ r Y nw xY wt          |           \  }	}}g }t                      }|d         dv rs|                    d          pg }|r|d                             dd          nd}d| }t!          |          }t#          ||          s|                    ||df           |	r(t#          ||          s|                    ||df           |sdddS |dk    rd                    d |D                       }t)          | |          }|dk    rD|D ]\  }}}t+          ||           t                              d | dd!         |           ddd|d"S |dk    r)d                    d# |D                       }dd$| d%dd&S d                    d' |D                       }|d         d         }d( |D             }t/          d) |D                       }|s|rd}t0          5  t2                              |          }ddd           n# 1 swxY w Y   |P| |||d*}t5          |          }t0          5  t6                              |g                               |           ddd           n# 1 swxY w Y   t;          d+| ||t=          |          |d,-           	  ||           n# t>          $ r} t                              d.|            t0          5  t6                              |g           }!||!v r|!                     |           |!st6          !                    |d           ddd           n# 1 swxY w Y   dd/||d0cY d} ~ S d} ~ ww xY wtE                                          d1d2          }"	 tG          |"          }"n# tH          tJ          f$ r d2}"Y nw xY w	 dd3l&m'}# n# t>          $ r d}#Y nw xY wtQ          j)                    }$|$tU          |"d          z   }%|$|$d4}&d}'	 |%tQ          j)                    z
  }(|(dk    rn;|j+        ,                    t[          d5|(          6          rd}'n|# |#|&d7           Xt0          5  t6                              |g           }!||!v r|!                     |           |!st6          !                    |d           ddd           n# 1 swxY w Y   |j.        })|'sd8n|)r|)nd8}*t;          d9| ||t=          |          |d,|*:           |'r|)|)dk    r|'sd;nd<}+dd=|+ d>||d0S |D ]^\  }}},|)d?k    s|)d@k    r|,rt+          ||           %|)d@k    r3t+          ||           t_          |           ta          tb                     _ddd|dAS te          || |||d*           d|dB| |dC| dD|  dEdFS t;          d+| ||t=          |          |dG-           tg          | || |H          })t;          d9| ||t=          |          |dG|):           |)dk    rddI||d0S |D ]^\  }}},|)d?k    s|)d@k    r|,rt+          ||           %|)d@k    r3t+          ||           t_          |           ta          tb                     _ddd|dAS )JaC  Run all pre-exec security checks and return a single approval decision.

    Gathers findings from tirith and dangerous-command detection, then
    presents them as a single combined approval request. This prevents
    a gateway force=True replay from bypassing one check when only the
    other was shown to the user.
    r  TNr  r  r  r  r   r  r  r  r  r   Fr  r  r   r   )actionr,  r-  r   )check_command_securityr8  )blockwarnr,  rule_idunknownztirith:smartr1  c              3   "   K   | ]
\  }}}|V  d S re   r+   r1   _r5  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      )J)J:1dA$)J)J)J)J)J)Jr   r   z'Smart approval: auto-approved '%s' (%s)r   )r?   rA   smart_approvedr3   c              3   "   K   | ]
\  }}}|V  d S re   r+   r@  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      -N-Nzq$d-N-N-N-N-N-Nr   zBLOCKED by smart approval: z@. The command was assessed as genuinely dangerous. Do NOT retry.)r?   rA   smart_deniedc              3   "   K   | ]
\  }}}|V  d S re   r+   r@  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      >>zq$d>>>>>>r   c                     g | ]\  }}}|	S r+   r+   )r1   keyrA  s      r   r4   z,check_all_command_guards.<locals>.<listcomp>  s    ...	Q...r   c              3   "   K   | ]
\  }}}|V  d S re   r+   )r1   rA  is_ts      r   r   z+check_all_command_guards.<locals>.<genexpr>	  s(      55jaDT555555r   )r5   rK   pattern_keysr3   pre_approval_requestgateway)r5   r3   rK   rJ  r   surfacez"Gateway approval notify failed: %sz?BLOCKED: Failed to send approval request to user. Do NOT retry.r"  gateway_timeouti,  )touch_activity_if_due)
last_touchr   g      ?r   zwaiting for user approvalr   post_approval_response)r5   r3   rK   rJ  r   rM  r}   z	timed outzdenied by userzBLOCKED: Command z. Do NOT retry this command.r   r   )r?   rA   user_approvedr3   r  u   ⚠️ z2. Asking the user for approval.

**Command:**
```
r  r   cli)r   r   z#BLOCKED: User denied. Do NOT retry.)4r=   r   r   rB   r   r   r   r#  r   r   rZ   tools.tirith_securityr9  ImportErrorr&   r$   r6  r   r2  r   r  r   r   r   rs   rq   r`   rp   r   r   r   r   removerx   r   r   r   r   tools.environments.baserO  time	monotonicmaxra   waitminrc   r   r   r^   r   r   )-r5   r  r   r$  r%  approval_moder'  r(  is_askr&  _pkr3   r*  r9  rK   warningsr   r,  r<  
tirith_keytirith_desccombined_desc_for_llmverdictrG  rA  combined_descprimary_keyall_keys
has_tirith	notify_cbapproval_datar{   r   r   r   rO  _now	_deadline_activity_stateresolved
_remainingr}   _outcomereason	is_tiriths-                                                r   check_all_command_guardsrs    s    RRR T222 "9!A!AK 59='RVSVRV-XXX%m444 '((Mry!34455 39X9Z9Z 3^kot^t^t T222Y+,,F344JY())F  3* 3V 39*++ 	&((F221I'1R1R.c; 
$)Kk K K K	 	 	 !T222  'B2FFM@@@@@@..w77    .Fg-N-N*L+{
 H)++K X"333 $$Z006B;CR(1+//)Y777(w((
0??;
33 	=OOZd;<<< ?;44 	?OO[+u=>>>  3 T222  $		)J)J)J)J)J J J *?@@i% 2 2	QS1111LLB "'<> > > $&*#8: : : $(II-N-NX-N-N-N$N$N!!\9N \ \ \ $	   II>>X>>>>>M1+a.K..X...H55H55555J  [
V [
	 	= 	=+//<<I	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=  
 #* (,	 M #=11E J J**;;;BB5IIIJ J J J J J J J J J J J J J J  &)'!(^^'!   	-((((   CSIII ? ?+//R@@E~~U+++  ?'++K>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? !&`#.#0	       . +,,001BCHHGg,,	*   -IIIIIII - - -(,%%%- >##Ds7A.I-1DAAOH&)9)99
?? ;##CZ,@,@#AA #H(4))')D     ; ;'++K<<E>>LL''' ;#''T:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; \F
 "* 7		 &5ffI   ()'!(^^'!	 	 	 	  v~61A1A,4J:J %W6WWW#.#0	   &. B B!Q	Y&&6X+=+=)+=#K5555x''#K555%c***,-@AAA !%%)-I I I
 	{&$(	%
 %
 	 	 	 &)(m-mm_fmmm	
 	
 		
 !(^^    'w;E~9JL L LF  !(^^	 	 	 	 <&(	
 
 	
 & : :Q	Y6X#5#5)#5K----xK---c"""$%8999!-A A As   +D= =
E
	E
,MMM>/N99N= N=(O4 4
R>"R AQ>2R>R	RR		RRR S S&%S&*S1 1T ?T AW..W25W2)F)NTNre   )arm   contextvarsloggingr   r-   r   rf   rX  rU   typingr   r   r   utilsr   	getLoggerrj   r   
ContextVarr
   r   __annotations__r   Tokenr   r    r&   _SSH_SENSITIVE_PATH_HERMES_ENV_PATH_PROJECT_ENV_PATH_PROJECT_CONFIG_PATH_SENSITIVE_WRITE_TARGET_PROJECT_SENSITIVE_WRITE_TARGET_COMMAND_TAIL_CMDPOSHARDLINE_PATTERNS
IGNORECASEDOTALLr/   r9   tupler=   ro   rB   DANGEROUS_PATTERNSrX   rI   rJ   r   _pattern_description_legacy_key_canonical_keyr   r   rN   r7   rZ   Lockrs   r[   r\   r]   r^   r`   rp   r   rq   objectru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r)  r6  rs  r+   r   r   <module>r     s          				 				 



                % % % % % % ! ! ! ! ! !		8	$	$ 6L[5K6 6 6 {-c2   M3 MT M M M M48 81B31G 8 8 8 8
'[%6s%; ' ' ' ' '
: :S : : : : :  >   S J     
 #U):"T"T=Q"T"T"T +F  W J\;\W>9
 335MN!=><<>YZ""$CD+ < MBI%	  1   
S 
U 
 
 
 
     (B5B,B >B f	B
 pB AB NB 'B #B .B 1B FB 4B .B aB  2!B" 0#B$ ?%B( P)B* V+B, I-B. m/B0 2/113RS1B2 /,..0WX3B4 O7NN}NNPvw5B6 L4KKMKKM{|7B8 -9B: 9;B< -=BD eEBF WGBJ OKBL uMBP mQBZ Y[B\ _]B` CaBb ]&E\\]\\  _B  CcBd EeBf UgBl MmBr UsBt OuBv UwBx TyBz :{BB SCB L  2   I I I I I I
 -/ d3C=) . . .0 ^ ^Hl%%h//K!N##NCCEE::AA>S^B_```##K77>>^?\]]]]@s @s3x @ @ @ @c c    $c e    $ 		$sDy/   $& 4S> & & &#%%s3x   355 S      * * * * * * * * $&c4i % % %)+ T#v+& + + +	. 	.T 	. 	. 	. 	.
3 
4 
 
 
 
 27 # s *.;>   :6s 6t 6 6 6 6) )t ) ) ) )J J3 J J J J'S 'T ' ' ' '+c +d + + + +s t     , , , , , ,H H H H H
DS Ds Dt D D D D-3 - - - --S - - - -#    $:s : : : :  =A6:04m ms m m/2Tzm/3m :=m m m m`c    d    *C * * * *s    
 
 
 
 
,C ,c ,c , , , ,` /3`/ `/S `/C `/7;`/ `/ `/ `/N3d 3s 3 3 3 38 04NA NAc NAS NA8<NA NA NA NAd
       r   