§
    éŽ’j†b  ã                  ó¾  — d Z ddlmZ ddlmZmZ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ZddlmZmZm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d	eee         z  f         Zeej        geee         z  f         Zd
ZdZeZ  G d„ dej!        ¦  «        Z" ej#        d¬¦  «         G d„ d¦  «        ¦   «         Z$	 dQddddœdRd!„Z%eddd"œdSd$„¦   «         Z&e	 dQddd"œdTd%„¦   «         Z&	 dQddd"œdUd(„Z&eddd"œdSd)„¦   «         Z'e	 dQddd"œdTd*„¦   «         Z'	 dQddd"œdUd+„Z'edddd,œdVd-„¦   «         Z(e	 dQdddd,œdWd.„¦   «         Z(	 dQdddd,œdXd/„Z(dYd0„Z)dZd2„Z*dYd3„Z+	 dQd[d4„Z,ee-ej.        e-         f         Z/d\d7„Z0d]d<„Z1d]d=„Z2d^d?„Z3 G d@„ dAej4        ¦  «        Z5d_dD„Z6e"j7        ej8        e"j9        ej:        e"j;        ej<        iZ=d`dF„Z>dadH„Z?dbdK„Z@ddLœdcdP„ZAdS )dud  Tool call policy system for the Google Antigravity SDK.

Provides a declarative API for expressing tool call policies (APPROVE, DENY,
ASK_USER) that are enforced via the hooks system. Policies are evaluated using
a priority-based model where specificity and safety determine precedence:

  Specific Deny > Specific Ask > Specific Allow >
  Wildcard Deny > Wildcard Ask > Wildcard Allow

Within each priority group, first match wins, enabling short-circuit evaluation.

Default Behavior:

  ``LocalAgentConfig`` uses ``confirm_run_command()`` as its default policy.
  This denies ``run_command`` (the most dangerous tool) while allowing all
  other tools.  To enable autonomous shell access, explicitly pass
  ``policies=[policy.allow_all()]``.

Policy Denial vs. Disabling Tools:

  Policies operate at the hook layer: a denied tool is still *visible* to the
  model in its tool list. If the model calls a policy-denied tool, the SDK
  rejects the call and returns a denial message. The model may then retry or
  choose another approach, but each attempt costs tokens.

  To remove a tool from the model's context entirely â€” so it never sees the
  tool and never wastes tokens on it â€” use ``CapabilitiesConfig.disabled_tools``
  (or ``enabled_tools``) instead.

  **Use policies** when the restriction is conditional or context-dependent
  (e.g., denying ``run_command`` only for dangerous arguments, or requiring
  user approval for certain operations).

  **Use CapabilitiesConfig** when the tool is simply irrelevant to the agent's
  purpose and should not appear in its context at all.

Usage:
  from google.antigravity.hooks import policy

  policies = [
      policy.deny("*"),                     # Block everything by default
      policy.allow("read_file"),            # Except reading files
      policy.deny("run_command",            # Block dangerous commands
          when=lambda args: "rm" in args.get("CommandLine", "")),
      policy.ask_user("run_command",        # Ask for other commands
          handler=my_approval_fn),
  ]

  hook = policy.enforce(policies)
  # Register hook with HookRunner's pre_tool_call_decide_hooks
é    )Úannotations)Ú	AwaitableÚCallableÚSequenceN)ÚAnyÚUnionÚoverload)Úlocalharness_pb2)Útypes)Úhooks.Ú*Úworkspace_onlyc                  ó   — e Zd ZdZdZdZdZdS )ÚDecisionzOutcome a policy can produce.ÚAPPROVEÚDENYÚASK_USERN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   © ó    úMC:\Projects\DSCons\.venv\Lib\site-packages\google\antigravity\hooks\policy.pyr   r   e   s#   € € € € € Ø%Ð%à€'Ø	€$Ø€(€(€(r   r   T)Úfrozenc                  óR   — e Zd ZU dZded<   ded<   dZded<   dZd	ed
<   dZded<   dS )ÚPolicyaâ  A single tool call policy rule.

  Attributes:
    tool: Tool name this policy targets, or "*" for all tools.
    decision: The outcome when this policy matches.
    when: Optional predicate on the tool call's arguments. If None the policy
      matches any call to the named tool.
    ask_user: Handler invoked when decision is ASK_USER. Must be provided for
      ASK_USER policies (validated at enforce() time).
    name: Human-readable label used in logging and deny reasons.
  ÚstrÚtoolr   ÚdecisionNúPredicate | NoneÚwhenúAskUserHandler | NoneÚask_userÚ Úname)r   r   r   r   Ú__annotations__r"   r$   r&   r   r   r   r   r   m   s`   € € € € € € ð
ð 
ð €)€)�)ØÐÐÑØ€$ÐÐÐÑØ$(€(Ð(Ð(Ð(Ñ(Ø€$€.€.€.�.€.€.r   r   r%   ©r"   r&   Úhandlerr    Ú
mcp_configútypes.BaseMcpServerConfigÚ	mcp_toolsúSequence[str] | Noner"   r!   r&   r   r)   r#   Úreturnúlist[Policy]c          
     ó”  — |j         }t          |t          ¦  «        rt          d|› d�¦  «        ‚|€9|p| j                             ¦   «         › d|› d�}t          |› d�| |||¬¦  «        gS g }|D ]X}	|r|› d|	› �n | j                             ¦   «         › d|› d|	› �}|                     t          |› d|	› �| |||¬¦  «        ¦  «         ŒY|S )	a™  Generates MCP-specific policies.

  Commonly used by allow(), deny(), and ask_user() builders. Translates the
  config and tools into the structured 'server/tool' or 'server/*' target
  formats expected by the runtime hook.

  Args:
    decision: The Decision outcome when the policy matches.
    mcp_config: The BaseMcpServerConfig of the target MCP server.
    mcp_tools: Optional sequence of tool names to allow/deny/ask for. If None,
      applies to all tools on this server.
    when: Optional argument predicate.
    name: Optional human-readable label.
    handler: Optional AskUserHandler for ASK_USER policies.

  Returns:
    A list of Policy objects.
  z1mcp_tools must be a sequence of strings (e.g., ['z']), not a single string.NÚ_Ú_allú/*)r   r    r"   r&   r$   ú/)r&   Ú
isinstancer   Ú
ValueErrorÚvalueÚlowerr   Úappend)
r    r*   r,   r"   r&   r)   ÚserverÚpolicy_nameÚpoliciesÚts
             r   Ú_mcp_policiesr>   ‡   sM  € ð6 Œ?€&å�	�3ÑÔð Ý
ð	¸Ið 	ð 	ð 	ñô ð ð
 Ðð ÐB˜Xœ^×1Ò1Ñ3Ô3ÐBÐB°fÐBÐBÐB€KåØ���ØØØØð	
ñ 	
ô 	
ðð ð €(Øð ð €aàÐKˆ4ˆˆ�!ˆˆˆ h¤n×&:Ò&:Ñ&<Ô&<Ð#KÐ#K¸vÐ#KÐ#KÈÐ#KÐ#Kð ð ‡O‚OÝØ��˜Q��ØØØØð	
ñ 	
ô 	
ñô ð ð ð 
€/r   ©r"   r&   r   c               ó   — d S ©Nr   ©r   r"   r&   s      r   ÚallowrC   É   ó	   € ð €#r   c               ó   — d S rA   r   ©r   r,   r"   r&   s       r   rC   rC   Ó   ó	   € ð €#r   ústr | types.BaseMcpServerConfigr   c               óÄ   — t          | t          ¦  «        r.|�t          d¦  «        ‚t          | t          j        ||¬¦  «        S t          t          j        | |||¬¦  «        S )a,  Creates an APPROVE policy.

  Args:
    tool: Tool name, "*" for all tools, or BaseMcpServerConfig.
    mcp_tools: Optional list of tool names if BaseMcpServerConfig is provided.
    when: Optional argument predicate.
    name: Human-readable label.

  Returns:
    A Policy or a list of Policies.
  Nú4mcp_tools cannot be specified when tool is a string.©r   r    r"   r&   r?   )r5   r   r6   r   r   r   r>   rF   s       r   rC   rC   Þ   sb   € õ$ �•cÑÔð NØÐÝÐMÑNÔNÐNÝ�t¥hÔ&6¸TÈÐMÑMÔMÐMå	•xÔ'¨¨y¸tÈ$Ð	OÑ	OÔ	OÐOr   c               ó   — d S rA   r   rB   s      r   ÚdenyrM   ø   rD   r   c               ó   — d S rA   r   rF   s       r   rM   rM     rG   r   c               óÄ   — t          | t          ¦  «        r.|�t          d¦  «        ‚t          | t          j        ||¬¦  «        S t          t          j        | |||¬¦  «        S )a(  Creates a DENY policy.

  Args:
    tool: Tool name, "*" for all tools, or BaseMcpServerConfig.
    mcp_tools: Optional list of tool names if BaseMcpServerConfig is provided.
    when: Optional argument predicate.
    name: Human-readable label.

  Returns:
    A Policy or a list of Policies.
  NrJ   rK   r?   )r5   r   r6   r   r   r   r>   rF   s       r   rM   rM     s`   € õ$ �•cÑÔð KØÐÝÐMÑNÔNÐNÝ�t¥h¤m¸$ÀTÐJÑJÔJÐJå	•x”} d¨I¸DÀtÐ	LÑ	LÔ	LÐLr   )r)   r"   r&   c               ó   — d S rA   r   )r   r)   r"   r&   s       r   r$   r$   '  rG   r   c               ó   — d S rA   r   ©r   r,   r)   r"   r&   s        r   r$   r$   2  s	   € ð €#r   c               óÈ   — t          | t          ¦  «        r/|�t          d¦  «        ‚t          | t          j        |||¬¦  «        S t          t          j        | ||||¬¦  «        S )aÁ  Creates an ASK_USER policy.

  Args:
    tool: Tool name, "*" for all tools, or BaseMcpServerConfig.
    mcp_tools: Optional list of tool names if BaseMcpServerConfig is provided.
    handler: Optional callable invoked to obtain user approval for the tool
      call. If omitted, confirmation is delegated to the host platform.
    when: Optional argument predicate.
    name: Human-readable label.

  Returns:
    A Policy or a list of Policies.
  NrJ   )r   r    r"   r$   r&   r(   )r5   r   r6   r   r   r   r>   rR   s        r   r$   r$   >  s‚   € õ* �•cÑÔð 	ØÐÝÐMÑNÔNÐNÝØÝÔ"ØØØðñ ô ð õ 
ÝÔØ
ØØØØð
ñ 
ô 
ð r   c                 ó.   — t          t          d¬¦  «        S )a  Creates a policy that approves all tool calls without confirmation.

  Intended for autonomous agents and local development where interactive
  confirmation is not needed. Equivalent to ``allow("*")``.

  Returns:
    A Policy that approves every tool call.
  Ú	allow_all©r&   )rC   Ú	_WILDCARDr   r   r   rU   rU   h  s   € õ 
�y˜{Ð	+Ñ	+Ô	+Ð+r   ÚAskUserHandlerc                óx   — d„ t           j                             ¦   «         D ¦   «         t          d| ¬¦  «        gz   S )zÜCreates a set of safe default policies.

  Allows all read-only tools and asks the user for any other tool calls.

  Args:
    handler: The handler to invoke for ASK_USER decisions.

  Returns:
    A list of Policies.
  c                ó6   — g | ]}t          |j        ¦  «        ‘ŒS r   )rC   r7   ©Ú.0r=   s     r   ú
<listcomp>z!safe_defaults.<locals>.<listcomp>  s    € Ð	AÐ	AÐ	A˜Q�%�”‰.Œ.Ð	AÐ	AÐ	Ar   r   ©r)   )r   ÚBuiltinToolsÚ	read_onlyr$   r^   s    r   Úsafe_defaultsra   t  sE   € ð 
BÐ	A¥%Ô"4×">Ò">Ñ"@Ô"@Ð	AÑ	AÔ	AÝˆs˜GÐ$Ñ$Ô$ðEñ 
ð r   c                 ó.   — t          t          d¬¦  «        S )ac  Creates a policy that denies all tool calls.

  Use as a base rule with specific ``allow()`` overrides for a
  deny-by-default posture. Specific policies always take priority over
  wildcard policies, so ``[deny_all(), allow("view_file")]`` will allow
  only ``view_file`` and deny everything else.

  Returns:
    A Policy that denies every tool call.
  Údeny_allrV   )rM   rW   r   r   r   rc   rc   „  s   € õ 
�i˜jÐ	)Ñ	)Ô	)Ð)r   c                óô   — | �<t          t          j        j        j        | d¬¦  «        t          t          d¬¦  «        gS t          t          j        j        j        d¬¦  «        t          t          d¬¦  «        gS )u\  Safe default: allows all tools, denies or confirms run_command.

  When no handler is given, ``run_command`` is denied outright â€” the agent
  sees the tool but calls are rejected with a clear message explaining
  how to enable it.  When a handler is given, ``run_command`` calls
  trigger an ASK_USER flow instead.

  All other tools (file read/write, subagents, image generation, etc.)
  are allowed.

  This is the default policy for ``LocalAgentConfig``.

  Args:
    handler: Optional handler for ASK_USER on run_command. If None, run_command
      is denied.

  Returns:
    A list of Policies.
  NÚconfirm_run_command)r)   r&   rV   )r$   r   r_   ÚRUN_COMMANDr7   rC   rW   rM   r^   s    r   re   re   ’  s‚   € ð, ÐåÝÔÔ*Ô0ØØ&ð	
ñ 	
ô 	
õ
 	�iÐ3Ð4Ñ4Ô4ðð õ �5ÔÔ)Ô/Ð6KÐLÑLÔLÝ�IÐ1Ð2Ñ2Ô2ð
ð r   Ú
workspacesúSequence[PathOrStr]c                ól   — ~ d„ t           j                             ¦   «         D ¦   «         }d„ |D ¦   «         S )a<  Restricts file tools to the given workspace directories.

  File read/write/create operations targeting paths outside any of the
  configured workspace directories are denied. Other tools are unaffected.

  Args:
    workspaces: Absolute paths of allowed workspace directories.

  Returns:
    A list of Policies.
  c                ó   — g | ]	}|j         ‘Œ
S r   )r7   r[   s     r   r]   z"workspace_only.<locals>.<listcomp>Ç  s   € ÐAÐAÐA˜A�”ÐAÐAÐAr   c                ó:   — g | ]}t          |t          ¬ ¦  «        ‘ŒS )rV   )rM   Ú_WORKSPACE_ONLY_POLICY_NAME)r\   r   s     r   r]   z"workspace_only.<locals>.<listcomp>È  s&   € Ð	NÐ	NÐ	N¸4�$ˆtÕ5Ð
6Ñ
6Ô
6Ð	NÐ	NÐ	Nr   )r   r_   Ú
file_tools)rg   rm   s     r   r   r   º  s=   € ð ØAÐA¥Ô!3×!>Ò!>Ñ!@Ô!@ÐAÑAÔA€*Ø	NÐ	NÀ:Ð	NÑ	NÔ	NÐNr   ÚpolicyÚ	tool_callútypes.ToolCallÚboolc              ƒ  ó4  K  — | j         €dS t          j        | j         ¦  «        }t          |j                             ¦   «         ¦  «        }|�r|d         }	 t          j        | j         ¦  «        }|                     |j	        |j
        ¦  «        }n# t          t          f$ r
 |j
        }Y nw xY wt          |t          ¦  «        rzt          |t           j        ¦  «        r`t          |t$          j        ¦  «        r|                       |¦  «        }n_|                     |j        ¦  «        }|                       |¦  «        }n/|                       |j        ¦  «        }n|                       ¦   «         }t          j        |¦  «        r|ƒ d{V —†n|}	t/          |	¦  «        S )a  Evaluates a policy's predicate.

  If the predicate is None, the policy always matches.
  Exceptions are propagated to the caller.

  Args:
    policy: The policy being evaluated.
    tool_call: The ToolCall instance.

  Returns:
    True if the predicate matches, False otherwise.
  NTr   )r"   ÚinspectÚ	signatureÚlistÚ
parametersÚvaluesÚtypingÚget_type_hintsÚgetr&   Ú
annotationÚ	TypeErrorÚ	NameErrorr5   ÚtypeÚ
issubclassÚpydanticÚ	BaseModelr   ÚToolCallÚmodel_validateÚargsÚisawaitablerq   )
rn   ro   ÚsigÚparamsÚfirst_paramÚhintsr{   Ú
raw_resultÚ
typed_argsÚresults
             r   Ú_evaluate_predicater�   Ð  s‰  è è € ð „[ÐØˆ4åÔ˜&œ+Ñ&Ô&€#Ý�”×%Ò%Ñ'Ô'Ñ(Ô(€&àñ Ø˜”)€Kð*ÝÔ# F¤KÑ0Ô0€eØ—9’9˜[Ô-¨{Ô/EÑFÔF€j€jøÝ•yÐ!ð *ð *ð *ØÔ)€j€j€jð*øøøõ �*�dÑ#Ô#ð 	/­
Ø•HÔ&ñ)ô )ð 	/õ 
�J¥¤Ñ	/Ô	/ð -Ø—[’[ Ñ+Ô+ˆ
ˆ
à×.Ò.¨y¬~Ñ>Ô>ˆ
Ø—[’[ Ñ,Ô,ˆ
ˆ
à—;’;˜yœ~Ñ.Ô.€j€jà—’‘”€Jå&Ô2°:Ñ>Ô>ÐN�ÐÐÐÐÐÐÐÀJ€&Ý	ˆf‰ŒÐs   Á9B ÂB,Â+B,c              ƒ  ó˜   K  — | j         €J ‚|                       |¦  «        }t          j        |¦  «        r|ƒ d{V —†}t          |¦  «        S )z>Invokes the policy's ask_user handler, propagating exceptions.N)r$   rs   r…   rq   )rn   ro   rŒ   s      r   Ú_execute_ask_userr�   ÿ  sU   è è € à	ŒÐ	$Ð	$Ð	$Ø�?Š?˜9Ñ%Ô%€&ÝÔ˜Ñ Ô ð Øˆ\ˆ\ˆ\ˆ\ˆ\ˆ\€FÝ	ˆf‰ŒÐr   Úpolicy_toolc                óº   — | t           k    rdS |j        r=|                      d¦  «        r| dd…         |j        k    S | |j        › d|j        › �k    S | |j        k    S )z<Matches a policy tool definition against a tool call target.Tr3   Néþÿÿÿr4   )rW   Úserver_nameÚendswithr&   )r�   ro   s     r   Ú_matches_targetr•     sv   € à•IÒÐØˆ4ØÔð FØ×Ò˜DÑ!Ô!ð 7Ø˜˜"˜Ô Ô!6Ò6Ð6Ø˜YÔ2ÐEÐE°Y´^ÐEÐEÒEÐEØ	˜	œÒ	&Ð&r   c                  ó"   — e Zd ZdZdd„Zdd„ZdS )Ú_PolicyDecideHookzAPreToolCallDecideHook that evaluates dynamic policies in-process.r<   úSequence[Policy]c                ó.   — t          |¦  «        | _        d S rA   )ru   Ú	_policies)Úselfr<   s     r   Ú__init__z_PolicyDecideHook.__init__  s   € Ý˜(‘^”^€D„N€N€Nr   Úcontextúhooks.HookContextÚdatarp   r.   úhooks.HookResultc              ƒ  ó4  K  — ~|}| j         D �]ö}|j        t          k    rŒt          |j        |¦  «        sŒ*	 t          ||¦  «        ƒ d{V —†sŒB|j        p|j        }|j        t          j        k    r=t           
                    d||j        ¦  «         t          j        dd|› d�¬¦  «        c S |j        t          j        k    r8t           
                    d||j        ¦  «         t          j        d¬	¦  «        c S |j        t          j        k    r{|j        �tt           
                    d
||j        ¦  «         t!          ||¦  «        ƒ d{V —†}|rt          j        d¬	¦  «        c S t          j        dd|j        › d|› d�¬¦  «        c S �Œ�# t"          $ rj}t                               d|j        p|j        d¬¦  «         t          j        dd|j        p|j        › dt'          |¦  «        › �¬¦  «        cY d}~c S d}~ww xY wt          j        d¬	¦  «        S )z>Evaluates dynamic policies sequentially against the tool call.NzPolicy %r denied tool %r.FzDenied by policy 'z'.)rC   ÚmessagezPolicy %r approved tool %r.T)rC   z/Policy %r requesting user approval for tool %r.zUser denied tool 'z' (policy 'z').u9   Exception during policy %r evaluation â€” failing closed.)Úexc_infoz%Policy evaluation failed for policy 'z': )rš   r&   rl   r•   r   r�   r    r   r   Ú_loggerÚinfor   Ú
HookResultr   r   r$   r�   Ú	ExceptionÚerrorÚrepr)r›   r�   rŸ   ro   ÚpÚlabelÚapprovedÚes           r   Úrunz_PolicyDecideHook.run  sµ  è è € ð 	Ø€IØŒ^ð 1
ñ 1
ˆØ	
ŒÕ.Ò	.Ð	.àå˜QœV YÑ/Ô/ð Øð)
Ý(¨¨IÑ6Ô6Ð6Ð6Ð6Ð6Ð6Ð6ð 	Ø
à”Ð ˜!œ&ˆØŒ:�œÒ&Ð&Ý
�,Š,Ð2°E¸9¼>Ñ
JÔ
JÐ
JÝÔ!ØØ4¨5Ð4Ð4Ð4ðñ ô ð ð ð ð Œ:�Ô)Ò)Ð)Ý
�,Š,Ð4°e¸Y¼^Ñ
LÔ
LÐ
LÝÔ!¨Ð-Ñ-Ô-Ð
-Ð
-Ð
-ØŒ:�Ô*Ò*Ð*¨q¬zÐ/EÝ
�,Š,Ø?ØØŒnñô ð õ
 -¨Q°	Ñ:Ô:Ð:Ð:Ð:Ð:Ð:Ð:ˆ(Øð 0ÝÔ#¨$Ð/Ñ/Ô/Ð/Ð/Ð/ÝÔ!ØàL y¤~ÐLÐLÀ%ÐLÐLÐLðñ ô ð ð ð ùøõ ð 
ð 
ð 
Ý�ŠØGØŒFÐ�a”fØð 	ñ 	
ô 	
ð 	
õ
 ÔØð¸¼Ð8HÀ!Ä&ð ð Ý˜‘G”Gðð ð
ñ 
ô 
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
øøøøð
øøøõ Ô $Ð'Ñ'Ô'Ð's9   ¸FÁAFÂ/A
FÃ<A)FÅ(!FÆ
HÆAG=Ç5HÇ=HN)r<   r˜   )r�   rž   rŸ   rp   r.   r    )r   r   r   r   rœ   r®   r   r   r   r—   r—     sB   € € € € € ØIÐIð$ð $ð $ð $ð9(ð 9(ð 9(ð 9(ð 9(ð 9(r   r—   r<   ú#Sequence[Policy | Sequence[Policy]]c                ó¦  — g }| D ]Ë}t          |t          ¦  «        r|                     |¦  «         Œ-t          |t          ¦  «        rkt          |t          t
          f¦  «        sO|D ]6}t          |t          ¦  «        st          dt          |¦  «        › �¦  «        ‚Œ7|                     |¦  «         Œ­t          dt          |¦  «        › �¦  «        ‚|S )aO  Flattens nested sequences of policies into a flat list.

  This allows combining single Policy objects and lists of Policies (e.g.
  returned by overloaded builders) seamlessly in config declarations.

  Args:
    policies: A sequence of Policy objects or nested sequences of Policies.

  Returns:
    A flat list of Policy objects.
  zExpected Policy, got z-Expected Policy or Sequence of Policies, got )	r5   r   r9   r   r   Úbytesr6   r~   Úextend)r<   Úflatrª   Úsub_ps       r   Úflatten_policiesrµ   _  sä   € ð 
€$Øð ð €aÝ�!•VÑÔð 
Ø
‡k‚k�!�n„n€n€nÝ	�A•xÑ	 Ô	 ð ­°A½½U°|Ñ)DÔ)Dð Øð Bð Bˆ%Ý˜%¥Ñ(Ô(ð 	BÝÐ@µ4¸±;´;Ð@Ð@ÑAÔAÐ
Að	Bà
‡k‚k�!�n„n€n€nåØ
C½$¸q¹'¼'Ð
CÐ
Cñô ð ð 
€+r   útuple[str, str]c                óf   — | t           k    rdS d| v r|                      dd¦  «        \  }}||fS | dfS )a  Decomposes 'server/tool' format into (tool_name, server_name) for proto.

  Args:
    tool: The tool target string (e.g., "run_command", "*", "server/tool",
      "server/*").

  Returns:
    A (tool_name, server_name) tuple for the PolicyRule proto fields.
  )r   r%   r4   é   r%   )rW   Úsplit)r   r:   Ú	tool_names      r   Ú_parse_tool_targetr»   Š  sJ   € ð 
�YÒÐØˆ9ØˆD€[€[ØŸ
š
 3¨Ñ*Ô*Ñ€FˆIØ�vÐÐØ
�ˆÐr   ú7tuple[localharness_pb2.PolicyConfig, dict[str, Policy]]c                óÂ  — t          | ¦  «        }i }g }t          |¦  «        D ]¤\  }}t          |j        ¦  «        \  }}|j        t
          k    }|j        dup|j        t          j	        k    o| }	d}
|	r
d|› �}
|||
<   | 
                    t          j        ||t          |j                 |j        p|j        |	|
¬¦  «        ¦  «         Œ¥t          j        |¬¦  «        }||fS )aŽ  Serializes Python Policy objects into a PolicyConfig proto.

  Static rules (no condition function, not ASK_USER) are handled entirely by
  localharness with zero wire roundtrips. Dynamic rules (with dynamic callbacks
  or ASK_USER handlers) are tagged with a rule_id and require the SDK to
  evaluate the condition when localharness sends a PolicyDecisionRequest.

  Args:
    policies: The policies to serialize (can be nested).

  Returns:
    A tuple of (PolicyConfig proto, dynamic_policy_map). The dynamic_policy_map
    maps rule_id -> Policy for dynamic rules, used by the event processor
    to handle incoming PolicyDecisionRequest messages.
  Nr%   Úrule_)r   r“   r    r&   Ú
is_dynamicÚrule_id)Úrules)rµ   Ú	enumerater»   r   r&   rl   r"   r    r   r   r9   r
   Ú
PolicyRuleÚ_DECISION_TO_PROTOÚPolicyConfig)r<   r³   Údynamic_policy_mapÚproto_rulesÚirª   rº   r“   Úis_workspace_onlyr¿   rÀ   Úconfigs               r   Ú_to_policy_config_protorË   œ  s#  € õ$ 
˜(Ñ	#Ô	#€$à*,ÐØ35€+å˜‰oŒoð ð �d€aˆå/°´Ñ7Ô7Ñ€Iˆ{ØœÕ"=Ò=Ðà	Œ�dÐÐ=˜aœj­HÔ,=Ò=ð àÐ
ð ð €GØð &Ø˜��€gØ$%Ð˜Ñ!à×ÒÝÔ#ØØ#Ý'¨¬
Ô3Ø”Ð!˜1œ6Ø!Øð	
ñ 	
ô 	
ñ	ô 	ð 	ð 	õ Ô(Øðñ ô €&ð 
Ð#Ð	#Ð#r   rª   útuple[int, int]c                óè   — | j         t          k    rdn| j                              d¦  «        rdnd}t          j        dt          j        dt          j        di}||                     | j        d¦  «        fS )zOReturns a sort key ordering specific before wildcards, and DENY before APPROVE.é   r3   r¸   r   é   )	r   rW   r”   r   r   r   r   rz   r    )rª   ÚscopeÚdecision_orders      r   Ú_policy_sort_keyrÒ   Ö  sk   € àŒv�Ò"Ð"ˆ!ˆ!¨a¬f¯oªo¸dÑ.CÔ.CÐ)J¨¨È€%å„m�QÝÔ˜ÝÔ˜ð€.ð
 �×#Ò# A¤J°Ñ2Ô2Ð	3Ð3r   )Úmcp_serversrÓ   ú*Sequence[types.BaseMcpServerConfig] | Noneúhooks.PreToolCallDecideHookc               ó>  — t          t          | ¦  «        t          ¬¦  «        }t          d„ |D ¦   «         ¦  «        }|r|€t	          d¦  «        ‚|D ]=}|j        t          j        k    r&|j        €t	          d|j	        p|j
        › d�¦  «        ‚Œ>t          |¦  «        S )a{  Creates a PreToolCallDecideHook that evaluates dynamic policies.

  Args:
    policies: The policies to enforce (can be nested).
    mcp_servers: Optional registered MCP server configurations (unused; kept for
      backward compatibility).

  Returns:
    A PreToolCallDecideHook ready for registration.

  Raises:
    ValueError: If any ASK_USER policy is missing a handler.
  )Úkeyc              3  óH   K  — | ]}d |j         v o|j         t          k    V — ŒdS )r4   N)r   rW   )r\   rª   s     r   ú	<genexpr>zenforce.<locals>.<genexpr>ö  sE   è è € ð ð Ø23€sˆaŒf€}Ð,˜œ¥9Ò,ðð ð ð ð ð r   NzÄMCP policies (containing '/') were detected, but 'mcp_servers' was not provided to enforce(). You must pass the registered MCP servers to enable secure policy matching and prevent silent bypasses.zASK_USER policy 'zU' is missing an ask_user handler. Provide one via policy.ask_user(tool, handler=...).)Úsortedrµ   rÒ   Úanyr6   r    r   r   r$   r&   r   r—   )r<   rÓ   Úflat_policiesÚhas_mcp_policyrª   s        r   ÚenforcerÞ   á  sâ   € õ$ Õ)¨(Ñ3Ô3Õ9IÐJÑJÔJ€-õ ð ð Ø7Dðñ ô ñ ô €.ð ð ˜Ð+Ý
ð	Fñô ð ð ð ð €aØ„z•XÔ&Ò&Ð&¨1¬:Ð+=ÝðJ˜aœfÐ.¨¬ð Jð Jð Jñô ð øõ
 
˜=Ñ	)Ô	)Ð)r   rA   )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,   r-   r"   r!   r&   r   r.   r/   )
r   rH   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"   r!   r&   r   r.   r/   )r   rH   r,   r-   r)   r#   r"   r!   r&   r   r.   r   )r.   r   )r)   rX   r.   r/   )r)   r#   r.   r/   )rg   rh   r.   r/   )rn   r   ro   rp   r.   rq   )r�   r   ro   rp   r.   rq   )r<   r¯   r.   r/   )r   r   r.   r¶   )r<   r¯   r.   r¼   )rª   r   r.   rÌ   )r<   r¯   rÓ   rÔ   r.   rÕ   )Br   Ú
__future__r   Úcollections.abcr   r   r   ÚdataclassesÚenumrs   ÚloggingÚosrx   r   r   r	   r€   Úgoogle.antigravity.protor
   Úgoogle.antigravityr   Úgoogle.antigravity.hooksr   Ú	getLoggerr   r¤   rq   Ú	Predicater‚   rX   rW   ÚWORKSPACE_ONLY_POLICY_NAMErl   ÚEnumr   Ú	dataclassr   r>   rC   rM   r$   rU   ra   rc   re   r   ÚPathLikeÚ	PathOrStrr   r�   r�   r•   ÚPreToolCallDecideHookr—   rµ   r   ÚPOLICY_DECISION_ALLOWr   ÚPOLICY_DECISION_DENYr   ÚPOLICY_DECISION_ASK_USERrÄ   r»   rË   rÒ   rÞ   r   r   r   ú<module>ró      sp  ðð2ð 2ðh #Ð "Ð "Ð "Ð "Ð "à 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø Ð Ð Ð Ø €€€Ø €€€Ø €€€Ø 	€	€	€	Ø €€€Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à €€€à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø $Ð $Ð $Ð $Ð $Ð $Ø *Ð *Ð *Ð *Ð *Ð *ð ˆ'Ô
˜HÑ
%Ô
%€ð
 �S˜$ ¨4¤Ñ0Ð0Ô1€	ð ˜5œ>Ð*¨D°9¸T´?Ñ,BÐBÔC€à€	Ø-Ð Ø8Ð ðð ð ð ð ˆtŒyñ ô ð ð €Ô˜dÐ#Ñ#Ô#ðð ð ð ð ñ ô ñ $Ô#ðð8 '+ð?ð
 "ØØ%)ð?ð ?ð ?ð ?ð ?ð ?ðD 
ð "Øð	ð ð ð ð ñ 
„ðð 
ð '+ðð "Øðð ð ð ð ñ 
„ðð '+ðPð "ØðPð Pð Pð Pð Pð Pð4 
ð "Øð	ð ð ð ð ñ 
„ðð 
ð '+ðð "Øðð ð ð ð ñ 
„ðð '+ðMð "ØðMð Mð Mð Mð Mð Mð4 
ð &*Ø!Øðð ð ð ð ñ 
„ðð 
ð '+ðð &*Ø!Øðð ð ð ð ñ 
„ðð '+ð'ð &*Ø!Øð'ð 'ð 'ð 'ð 'ð 'ðT	,ð 	,ð 	,ð 	,ðð ð ð ð *ð *ð *ð *ð &*ð"ð "ð "ð "ð "ðJ �#�r”{ 3Ô'Ð'Ô(€	ðOð Oð Oð Oð,,ð ,ð ,ð ,ð^ð ð ð ð'ð 'ð 'ð 'ð ?(ð ?(ð ?(ð ?(ð ?(˜Ô3ñ ?(ô ?(ð ?(ðNð ð ð ðF ÔÐ&Ô<Ø„MÐ#Ô8ØÔÐ'Ô@ðÐ ðð ð ð ð$2$ð 2$ð 2$ð 2$ðt4ð 4ð 4ð 4ð ?Cð'*ð '*ð '*ð '*ð '*ð '*ð '*ð '*r   