ó
    |¤j¡  ã                  óº   • S r SSKJr  SSKrSSKJrJr  SSKJr  \R                  " \
5      rSS jr\ " S S5      5       rSS	 jr\" 5       qSS
 jrSS jrSS jrg)a—  Per-tool and per-domain permission policy.

The interactive permission protocol between ``Executor`` and the TUI/CLI
confirm paths uses raw strings (``"y"``/``"n"``/``"u"``/``"a"``/``"s"``).

``PermissionPolicy`` enables per-tool and per-domain rules so administrators
can express policies like "always allow git status" or "always deny curl to
external hosts" without relying on the per-turn trust cache.

Rules are loaded from ``~/.acli/permissions.toml`` (global) and
``.acli/permissions.toml`` (workspace, wins on conflicts)::

    [tools]
    write_file = "confirm"
    delete_file = "deny"

    [commands]
    "git status" = "allow"
    "git push" = "deny"
é    )ÚannotationsN)Ú	dataclassÚfield)ÚPathc                ó@   • SR                  U R                  5       5      $ )zBCollapse whitespace runs so rule prefixes match ``git\tpush`` too.Ú )ÚjoinÚsplit)Úcommands    Ú§C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\dashscope/acli/permission.pyÚ_normalize_commandr       s   € à�8‰8�G—M‘M“OÓ$Ð$ó    c                  ón   • \ rS rSr% Sr\" \S9rS\S'   \" \S9r	S\S'   SS jr
SS jrSS	 jrSS
 jrSrg)ÚPermissionPolicyé%   a  Per-tool and per-domain permission rules.

Rules are checked before the interactive prompt. For command rules the
most specific (longest) matching prefix wins, so a narrow ``deny``
always overrides a broad ``allow``. If no rule matches, the normal
``PermissionLevel`` flow applies.
)Údefault_factoryzdict[str, str]Ú
tool_rulesÚcommand_rulesc                ó8   • U R                   R                  U5      $ )z?Return the policy decision for a tool, or None to fall through.)r   Úget)ÚselfÚ	tool_names     r   Ú
check_toolÚPermissionPolicy.check_tool4   s   € à�‰×"Ñ" 9Ó-Ð-r   c                óö   • [        U5      nSnU R                  R                  5        HA  u  pEUR                  U5      (       d  M  Ub  [	        U5      US   :”  d  M4  [	        U5      U4nMC     U(       a  US   $ S$ )z8Return the policy decision for a shell command, or None.Nr   é   )r   r   ÚitemsÚ
startswithÚlen)r   r   Ú
normalizedÚbestÚprefixÚdecisions         r   Úcheck_commandÚPermissionPolicy.check_command8   ss   € ä'¨Ó0ˆ
Ø'+ˆØ $× 2Ñ 2× 8Ñ 8Ö :ÑˆFØ×$Ñ$ V×,Ó,à‘<¤3 v£;°°a±Õ#8Ü ›K¨Ð2’Dñ	 !;ö
 ˆt�A‰wÐ( DÐ(r   c                óH   • US;  a  [        SU 35      eX R                  U'   g )N©ÚallowÚdenyÚconfirmú)decision must be allow/deny/confirm, got )Ú
ValueErrorr   )r   r   r#   s      r   Úadd_tool_ruleÚPermissionPolicy.add_tool_ruleC   s0   € ØÐ7Ó7ÜØ;¸H¸:ÐFóð ð &.�‰˜	Ò"r   c                ó‚   • US;  a  [        SU 35      e[        U5      nU(       d  [        S5      eX R                  U'   g )Nr'   r+   z%command rule prefix must not be empty)r,   r   r   )r   r"   r#   r    s       r   Úadd_command_ruleÚ!PermissionPolicy.add_command_ruleJ   sK   € ØÐ7Ó7ÜØ;¸H¸:ÐFóð ô (¨Ó/ˆ
ÞÜÐDÓEÐEØ)1×Ñ˜:Ò&r   © N)r   ÚstrÚreturnú
str | None)r   r3   r4   r5   )r   r3   r#   r3   r4   ÚNone)r"   r3   r#   r3   r4   r6   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Údictr   Ú__annotations__r   r   r$   r-   r0   Ú__static_attributes__r2   r   r   r   r   %   s;   ‡ ññ "'°tÑ!<€J�Ó<á$)¸$Ñ$?€M�>Ó?ô.ô	)ô.÷2r   r   c                óÎ  • SSK Jn  / nU" U5      nUc  U$ SU R                  4SU R                  44 Hz  u  pVUR	                  U0 5      n[        U[        5      (       d  UR                  U SU S35        ME  UR                  5        H!  u  p‰ U" [        U5      [        U	5      5        M#     M|     U$ ! [         a(  n
UR                  U SU SU< S	U
 35         Sn
A
MX  Sn
A
ff = f)
z>Merge a permissions.toml into ``policy``; returns load errors.r   )Ú	load_tomlNÚtoolsÚcommandsz: [z] must be a tablez] z: )Údashscope.acli.utilsr@   r-   r0   r   Ú
isinstancer<   Úappendr   r3   r,   )ÚpolicyÚpathr@   ÚerrorsÚdataÚsectionÚaddÚentriesÚkeyr#   Úes              r   Úload_permissions_filerO   U   sî   € å.à€FÙ�T‹?€DØ�|Øˆà	�&×&Ñ&Ð'Ø	�V×,Ñ,Ð-ó‰ˆð —(‘(˜7 BÓ'ˆÜ˜'¤4×(Ñ(Ø�M‰M˜T˜F # g YÐ.?Ð@ÔAÙØ$Ÿ]™]ž_‰MˆCðCÙ”C˜“Hœc (›mÖ,ó -ñð €Møô ó CØ—‘   c¨'¨°"°S±G¸2¸a¸SÐA×BÒBûðCús   ÂB2Â2
C$Â<CÃC$c                 ó   • [         $ ©N©Ú_policyr2   r   r   Úget_permission_policyrT   r   s   € Ü€Nr   c                ó   • U q g rQ   rR   )rF   s    r   Úset_permission_policyrV   v   s   € à�Gr   c                 ó²   • SSK Jn Jn  [        5       nU S-  US-  4 H+  n[	        X#5       H  n[
        R                  SU5        M     M-     [        U5        U$ )uZ  Load permissions.toml (global + workspace) into the global policy.

Called once at startup alongside ``configure_audit_logger``. Workspace
rules are applied last and win on identical keys. Invalid entries are
skipped with a warning â€” a malformed rule must never widen permissions
silently, so bad decisions are dropped, not defaulted to allow.
r   )Ú
CONFIG_DIRÚWORKSPACE_DIRzpermissions.tomlz%s)Údashscope.acli.configrX   rY   r   rO   ÚlogÚwarningrV   )rX   rY   rF   rG   Úerrs        r   Úconfigure_permission_policyr^   {   s\   € ÷ @äÓ€FàÐ'Ñ'ØÐ*Ñ*óˆô )¨Ö6ˆCÜ�K‰K˜˜cÖ"ó 7ñ	ô ˜&Ô!Ø€Mr   )r   r3   r4   r3   )rF   r   rG   r   r4   z	list[str])r4   r   )rF   r   r4   r6   )r;   Ú
__future__r   ÚloggingÚdataclassesr   r   Úpathlibr   Ú	getLoggerr7   r[   r   r   rO   rS   rT   rV   r^   r2   r   r   Ú<module>rd      se   ðñõ* #ã ß (Ý à×Ò˜Ó!€ô%ð
 ÷,2ð ,2ó ð,2ô^ñ4 Ó
€ôôõ
r   