Ë
    ÛJêjÁ*  ã                  óš  — d Z ddlmZ ddlZddlmc mZ ddl	Z	ddl
Z
ddlZddlmZmZ ddlmZ ddlmZmZ ddlmZ ej,                  j/                   e
j0                  d«      du d	¬
«      Zej,                  j                  dd„«       Zej,                  j                  dd„«       Zej,                  j                  dd„«       Zej,                  j                  edd„«       «       Zej,                  j                  edd„«       «       Zej,                  j                  edd„«       «       Zej,                  j                  edd„«       «       Z y)z;End-to-end checks for the agent security and approval path.é    )ÚannotationsN)Ú
PtahBridgeÚRoadmapStep)ÚApprovalManager)ÚAuditEventTypeÚ
AuditQuery)ÚPermissionScopeÚbwrapzBubblewrap is unavailable)Úreasonc            
   ƒ  óþ  ‡K  — t        «       Št        ‰d¬«      } | j                  j                  j                  j                  t        j                  j                  «       d%ˆfd„}t        j                   |«       «      }| j                  dt        ddddd	i¬
«      «      ƒ d {  –—† }|ƒ d {  –—†  |j                  }d}||u }|s­t        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}|j,                  }d}||k(  }|s­t        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}| j.                  j1                  t3        t4        j6                  d¬«      «      }	| j.                  j1                  t3        t4        j8                  d¬«      «      }
| j.                  j1                  t3        t4        j:                  d¬«      «      }t=        |	«      }d}||k(  }|söt        j                  d|fd||f«      dt!        j"                  «       v st        j$                  t<        «      rt        j&                  t<        «      nddt!        j"                  «       v st        j$                  |	«      rt        j&                  |	«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}t=        |
«      }d}||k(  }|söt        j                  d|fd||f«      dt!        j"                  «       v st        j$                  t<        «      rt        j&                  t<        «      nddt!        j"                  «       v st        j$                  |
«      rt        j&                  |
«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}t=        |«      }d}||k(  }|söt        j                  d|fd||f«      dt!        j"                  «       v st        j$                  t<        «      rt        j&                  t<        «      nddt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}|	d   }|j>                  }d}||k(  }|s€t        j                  d|fd ||f«      t        j&                  |«      t        j&                  |«      t        j&                  |«      d!œz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}x}}|
d   }|j@                  }d}||k(  }|s€t        j                  d|fd"||f«      t        j&                  |«      t        j&                  |«      t        j&                  |«      d!œz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}x}}|d   }|jB                  }d#}||k(  }|s€t        j                  d|fd$||f«      t        j&                  |«      t        j&                  |«      t        j&                  |«      d!œz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}x}}y 7 �ŒQ7 �ŒJ­w)&Nç      ð?©Úapproval_managerÚapproval_timeout_secondsc               “  óî   •K  — ‰j                  «       s.t        j                  d«      ƒ d {  –—†  ‰j                  «       sŒ.‰j                  «       d   } ‰j                  | j                  dd«       y 7 ŒF­w)Nç{®Gázt?r   Úreviewerzapproved in integration test©Úlist_pendingÚasyncioÚsleepÚapproveÚ
request_id©ÚrequestÚ	approvalss    €ú9/home/oem/CyberBrain26-unified/tests/test_security_e2e.pyÚapprove_when_requestedz]test_ptah_dispatch_requires_approval_and_audits_the_full_task.<locals>.approve_when_requested   sd   øè ø€ Ø×(Ñ(Ô*Ü—-‘- Ó&×&Ð&ð ×(Ñ(Õ*à×(Ñ(Ó*¨1Ñ-ˆØ×Ñ˜'×,Ñ,¨jÐ:XÕYð 'úó   ƒ)A5¬A3­A5Á2A5zroadmap-e2ez
task-e2e-1Úcoderzexercise the guarded agent taskÚactionÚexecute_shell©ÚidÚroleÚdescriptionÚpayloadT©Úis©z/%(py2)s
{%(py2)s = %(py0)s.success
} is %(py5)sÚresult©Úpy0Úpy2Úpy5úassert %(py7)sÚpy7©ú==)z/%(py2)s
{%(py2)s = %(py0)s.step_id
} == %(py5)s©Ú
event_typeÚtask_idé   ©z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sÚlenÚrequired©r-   Úpy1Úpy3Úpy6úassert %(py8)sÚpy8ÚgrantedÚ	completedr   z
ptah-coder)z0%(py3)s
{%(py3)s = %(py1)s.agent_id
} == %(py6)s©r<   r=   r>   )z-%(py3)s
{%(py3)s = %(py1)s.actor
} == %(py6)sÚ	succeeded©z.%(py3)s
{%(py3)s = %(py1)s.result
} == %(py6)s©ÚreturnÚNone)"r   r   r    ÚidentityÚscopesÚaddr	   ÚSHELLÚvaluer   Úcreate_taskÚexecute_stepr   ÚsuccessÚ
@pytest_arÚ_call_reprcompareÚ@py_builtinsÚlocalsÚ_should_repr_global_nameÚ	_safereprÚAssertionErrorÚ_format_explanationÚstep_idÚ	audit_logÚqueryr   r   ÚAPPROVAL_REQUIREDÚAPPROVAL_GRANTEDÚAGENT_FINISHEDr9   Úagent_idÚactorr+   )Úbridger   Úapproval_taskr+   Ú@py_assert1Ú@py_assert4Ú@py_assert3Ú@py_format6Ú@py_format8r:   rA   rB   Ú@py_assert2Ú@py_assert5Ú@py_format7Ú@py_format9Ú@py_assert0r   s                    @r   Ú=test_ptah_dispatch_requires_approval_and_audits_the_full_taskrm      sÙ  øè ø€ äÓ!€IÜØ"Ø!$ô€Fð ‡L�L×Ñ× Ñ ×$Ñ$¤_×%:Ñ%:×%@Ñ%@ÔAõZô ×'Ñ'Ñ(>Ó(@ÓA€MØ×&Ñ&ØÜØØØ9Ø˜Ð/ô		
ó÷ €Fð ×Ðà�>‰>Ð!˜TÐ!ˆ>˜TÐ!×!×!Õ!ˆ>˜T×!×!×!×!×!Ñ!ˆ6×!×!Ó!ˆ6×!×!Ô!ˆ>×!×!Ò!˜T×!×!×!×!×!×!Ó!Ø�>‰>Ð)˜\Ð)ˆ>˜\Ñ)×)×)Õ)ˆ>˜\×)×)×)×)×)Ñ)ˆ6×)×)Ó)ˆ6×)×)Ô)ˆ>×)×)Ò)˜\×)×)×)×)×)×)Ó)Ø×Ñ×%Ñ%Üœn×>Ñ>ÈÔUó€Hð ×Ñ×$Ñ$Üœn×=Ñ=À|ÔTó€Gð × Ñ ×&Ñ&Üœn×;Ñ;À\ÔRó€Iô ˆx‹=Ð˜AÐˆ=˜AÑ××Õˆ=˜A×××××ÑŒ3××ÓŒ3×××××Òˆx××Óˆx××Ôˆ=××Ò˜A××××××ÓÜˆw‹<Ð˜1Ðˆ<˜1Ñ××Õˆ<˜1×××××ÑŒ3××ÓŒ3×××××Òˆw××Óˆw××Ôˆ<××Ò˜1××××××ÓÜˆy‹>Ð˜QÐˆ>˜QÑ××Õˆ>˜Q×××××ÑŒ3××ÓŒ3×××××Òˆy××Óˆy××Ôˆ>××Ò˜Q××××××ÓØ�A‰;Ð/ˆ;×ÑÐ/ <Ð/Ð <Ñ/×/×/Õ/Ð <×/×/Ó/ˆ;×/×/Ò/Ð×/×/Ò/ <×/×/×/×/×/×/Õ/Ø�1‰:Ð+ˆ:×ÑÐ+˜|Ð+Ð˜|Ñ+×+×+Õ+Ð˜|×+×+Ó+ˆ:×+×+Ò+Ð×+×+Ò+˜|×+×+×+×+×+×+Õ+Ø�Q‰<Ð-ˆ<×ÑÐ- +Ð-Ð +Ñ-×-×-Õ-Ð +×-×-Ó-ˆ<×-×-Ò-Ð×-×-Ò- +×-×-×-×-×-×-Ö-ð7ùð ûs%   ƒB#_=Â&_7Â'	_=Â0_:Â1]_=ß:_=c            
   ƒ  ó
  K  — t        d¬«      } | j                  j                  j                  j	                  t
        j                  j                  «       | j                  dt        dddddi¬	«      «      ƒ d {  –—† }|j                  }d
}||u }|s­t        j                  d|fd||f«      dt        j                  «       v st        j                  |«      rt        j                   |«      ndt        j                   |«      t        j                   |«      dœz  }dd|iz  }t#        t        j$                  |«      «      ‚d x}x}}d}g }|j&                  }|}|sd}	|	}|j(                  }
 |
«       }||v }|�s3ddt        j                  «       v st        j                  |«      rt        j                   |«      ndt        j                   |«      dœz  }|j+                  |«       |s+ddt        j                   	«      iz  }|j+                  |«       t        j,                  |d«      i z  }t        j                  d|fd||f«      t        j                   |«      |t        j                   |
«      t        j                   |«      dœz  }dd|iz  }t#        t        j$                  |«      «      ‚d x}x}x}x}x}x}	x}
}| j                  }|j.                  } |«       }g }||k(  }|sÕt        j                  d|fd||f«      dt        j                  «       v st        j                  | «      rt        j                   | «      ndt        j                   |«      t        j                   |«      t        j                   |«      t        j                   |«      d œz  }d!d"|iz  }t#        t        j$                  |«      «      ‚d x}x}x}x}}| j0                  j3                  t5        t6        j8                  d¬#«      «      }t;        |«      }d}||k(  }|söt        j                  d|fd$||f«      d%t        j                  «       v st        j                  t:        «      rt        j                   t:        «      nd%d&t        j                  «       v st        j                  |«      rt        j                   |«      nd&t        j                   |«      t        j                   |«      d'œz  }d(d)|iz  }t#        t        j$                  |«      «      ‚d x}x}}y 7 �Œ�­w)*Ngš™™™™™©?)r   zroadmap-denyztask-denied-1r    z!must not execute without approvalr!   r"   r#   Fr(   r*   r+   r,   r0   r1   zapproval unavailableÚ ú"%(py7)s
{%(py7)s = %(py5)s.error
}©r/   r1   ú%(py10)sÚpy10r7   ©Úin)zI%(py1)s in %(py17)s
{%(py17)s = %(py15)s
{%(py15)s = %(py13)s.lower
}()
})r<   Úpy13Úpy15Úpy17zassert %(py19)sÚpy19r2   ©zi%(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.coder
}.get_implemented
}()
} == %(py9)sra   ©r-   r.   Úpy4r>   Úpy9úassert %(py11)sÚpy11r4   r8   r9   Údeniedr;   r?   r@   )r   r    rI   rJ   rK   r	   rL   rM   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   ÚerrorÚlowerÚappendÚ_format_boolopÚget_implementedrZ   r[   r   r   ÚAPPROVAL_DENIEDr9   )ra   r+   rc   rd   re   rf   rg   rl   Ú@py_assert6Ú@py_assert9Ú@py_assert14Ú@py_assert16rh   Ú@py_format11Ú@py_format12Ú@py_format18Ú@py_format20ri   Ú@py_assert8Ú@py_assert7Ú@py_format10r€   rj   rk   s                           r   Ú:test_ptah_task_is_denied_when_required_approval_is_missingr’   B   s	  è ø€ ä°Ô6€FØ
‡L�L×Ñ× Ñ ×$Ñ$¤_×%:Ñ%:×%@Ñ%@ÔAà×&Ñ&ØÜØØØ;Ø˜Ð/ô		
ó÷ €Fð �>‰>Ð"˜UÐ"ˆ>˜UÐ"×"×"Õ"ˆ>˜U×"×"×"×"×"Ñ"ˆ6×"×"Ó"ˆ6×"×"Ô"ˆ>×"×"Ò"˜U×"×"×"×"×"×"Ó"Ø!ÒA f§l¡lÐA lÐA¡l°bÐA°bÑA×%?Ñ%?ÐAÑ%?Ó%AÐAÐ!Ð%AÐA×A×A×A×A×AÑA f×A×AÓA f×A×AÔA l×A×A×AÑA¡l×A×AÐA°b×A×A×A×A×A×A×A×AÔAÐ!Ð%A×A×AÓAÐ!×A×AÓAÐ%?×A×AÒAÐ%A×A×A×A×A×A×A×AÕAØ�<‰<Ð/ˆ<×'Ñ'Ð/Ñ'Ó)Ð/¨RÐ/Ð)¨RÑ/×/×/Õ/Ð)¨R×/×/×/×/×/Ñ/ˆ6×/×/Ó/ˆ6×/×/Ô/ˆ<×/×/Ò/Ð'×/×/Ò/Ð)×/×/Ò/¨R×/×/×/×/×/×/×/Ø×Ñ×#Ñ#Üœn×<Ñ<ÀoÔVó€Fô ˆv‹;Ð˜!Ðˆ;˜!Ñ××Õˆ;˜!×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜!××××××Ôð!ûs   ‚A8T
Á:TÁ;RT
c            
   ƒ  ó   ‡K  — t        «       Št        ‰d¬«      } | j                  j                  j                  j                  t        j                  j                  «       d ˆfd„}t        j                   |«       «      }| j                  dt        ddddd	i¬
«      «      ƒ d {  –—† }|ƒ d {  –—†  |j                  }d}||u }|s­t        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}| j                  }|j,                  } |«       }	g }
|	|
k(  }|sÕt        j                  d|fd|	|
f«      dt!        j"                  «       v st        j$                  | «      rt        j&                  | «      ndt        j&                  |«      t        j&                  |«      t        j&                  |	«      t        j&                  |
«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}x}	x}}
| j.                  j1                  t3        t4        j6                  d¬«      «      }t9        |«      }d}	||	k(  }|söt        j                  d|fd||	f«      dt!        j"                  «       v st        j$                  t8        «      rt        j&                  t8        «      nddt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |	«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}	y 7 �Œ"7 �Œ­w)!Nr   r   c               “  óî   •K  — ‰j                  «       s.t        j                  d«      ƒ d {  –—†  ‰j                  «       sŒ.‰j                  «       d   } ‰j                  | j                  dd«       y 7 ŒF­w)Nr   r   r   znot approved)r   r   r   Údenyr   r   s    €r   Údeny_when_requestedzNtest_ptah_task_stays_blocked_when_reviewer_denies.<locals>.deny_when_requested`   sa   øè ø€ Ø×(Ñ(Ô*Ü—-‘- Ó&×&Ð&ð ×(Ñ(Õ*à×(Ñ(Ó*¨1Ñ-ˆØ�‰�w×)Ñ)¨:°~ÕFð 'úr   zroadmap-deniedztask-denied-2r    zmust not execute after denialr!   r"   r#   Fr(   r*   r+   r,   r0   r1   r2   rz   ra   r{   r~   r   r4   r7   r8   r9   r€   r;   r?   r@   rF   )r   r   r    rI   rJ   rK   r	   rL   rM   r   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   r…   rZ   r[   r   r   r†   r9   )ra   r–   Údenial_taskr+   rc   rd   re   rf   rg   ri   r�   r�   r‘   rŒ   r€   rh   rj   rk   r   s                     @r   Ú1test_ptah_task_stays_blocked_when_reviewer_deniesr˜   Z   sd  øè ø€ äÓ!€IÜ¨ÈSÔQ€FØ
‡L�L×Ñ× Ñ ×$Ñ$¤_×%:Ñ%:×%@Ñ%@ÔAõGô ×%Ñ%Ñ&9Ó&;Ó<€KØ×&Ñ&ØÜØØØ7Ø˜Ð/ô		
ó÷ €Fð ×Ðà�>‰>Ð"˜UÐ"ˆ>˜UÐ"×"×"Õ"ˆ>˜U×"×"×"×"×"Ñ"ˆ6×"×"Ó"ˆ6×"×"Ô"ˆ>×"×"Ò"˜U×"×"×"×"×"×"Ó"Ø�<‰<Ð/ˆ<×'Ñ'Ð/Ñ'Ó)Ð/¨RÐ/Ð)¨RÑ/×/×/Õ/Ð)¨R×/×/×/×/×/Ñ/ˆ6×/×/Ó/ˆ6×/×/Ô/ˆ<×/×/Ò/Ð'×/×/Ò/Ð)×/×/Ò/¨R×/×/×/×/×/×/×/Ø×Ñ×#Ñ#Üœn×<Ñ<ÀoÔVó€Fô ˆv‹;Ð˜!Ðˆ;˜!Ñ××Õˆ;˜!×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜!××××××Ôð!ùð ûs%   ƒB#OÂ&OÂ'	OÂ0OÂ1LOÏOc               ƒ  óú
  ‡K  — t        «       Št        ‰d¬«      } | j                  j                  j                  j                  t        j                  j                  «       d)ˆfd„}t        j                   |«       «      }| j                  dt        ddddd	d
dœ¬«      «      ƒ d {  –—† }|ƒ d {  –—†  |j                  }d}||u }|s­t        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}x}}|j,                  d   d   }	|	j.                  }
 |
«       }d}||k(  }|s”t        j                  d|fd||f«      t        j&                  |	«      t        j&                  |
«      t        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t)        t        j*                  |«      «      ‚d x}	x}
x}x}}| j0                  j3                  t5        t6        j8                  d¬«      «      }| j0                  j3                  t5        t6        j:                  d¬«      «      }t=        |«      }
d}|
|k(  }|söt        j                  d|fd|
|f«      dt!        j"                  «       v st        j$                  t<        «      rt        j&                  t<        «      ndd t!        j"                  «       v st        j$                  |«      rt        j&                  |«      nd t        j&                  |
«      t        j&                  |«      d!œz  }d"d#|iz  }t)        t        j*                  |«      «      ‚d x}
x}}t=        |«      }
d}|
|k(  }|söt        j                  d|fd|
|f«      dt!        j"                  «       v st        j$                  t<        «      rt        j&                  t<        «      ndd$t!        j"                  «       v st        j$                  |«      rt        j&                  |«      nd$t        j&                  |
«      t        j&                  |«      d!œz  }d"d#|iz  }t)        t        j*                  |«      «      ‚d x}
x}}|d%   }	|	j>                  }
d&}|
|k(  }|s€t        j                  d|fd'|
|f«      t        j&                  |	«      t        j&                  |
«      t        j&                  |«      d(œz  }d"d#|iz  }t)        t        j*                  |«      «      ‚d x}	x}
x}}y 7 �ŒÍ7 �ŒÆ­w)*Nr   r   c               “  óî   •K  — ‰j                  «       s.t        j                  d«      ƒ d {  –—†  ‰j                  «       sŒ.‰j                  «       d   } ‰j                  | j                  dd«       y 7 ŒF­w)Nr   r   r   zsandbox run approvedr   r   s    €r   r   z[test_coder_executes_code_only_after_permission_and_approval.<locals>.approve_when_requested�   sd   øè ø€ Ø×(Ñ(Ô*Ü—-‘- Ó&×&Ð&ð ×(Ñ(Õ*à×(Ñ(Ó*¨1Ñ-ˆØ×Ñ˜'×,Ñ,¨jÐ:PÕQð 'úr   zroadmap-codeztask-code-1r    z run a small isolated Python taskzprint('from-coder')ÚpythonÚcode©rœ   Úlanguager!   r#   Tr(   r*   r+   r,   r0   r1   Ú	executionÚstdoutz
from-coderr2   ©zD%(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.strip
}()
} == %(py8)s©r<   r=   r/   r@   úassert %(py10)srs   r4   r7   r8   r9   Úapprovals_auditedr;   r?   r@   Úresults_auditedr   rD   rE   rC   rF   ) r   r   r    rI   rJ   rK   r	   ÚPYTHONrM   r   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   ÚdataÚstriprZ   r[   r   r   r]   ÚTOOL_RESULTr9   r+   )ra   r   rb   r+   rc   rd   re   rf   rg   rl   rh   r�   r‡   rk   r‹   r¤   r¥   ri   rj   r   s                      @r   Ú;test_coder_executes_code_only_after_permission_and_approvalrª   z   sƒ  øè ø€ ô  Ó!€IÜ¨ÈSÔQ€FØ
‡L�L×Ñ× Ñ ×$Ñ$¤_×%;Ñ%;×%AÑ%AÔBõRô ×'Ñ'Ñ(>Ó(@ÓA€MØ×&Ñ&ØÜØØØ:à-Ø$Ø ñô			
ó÷ €Fð ×Ðà�>‰>Ð!˜TÐ!ˆ>˜TÐ!×!×!Õ!ˆ>˜T×!×!×!×!×!Ñ!ˆ6×!×!Ó!ˆ6×!×!Ô!ˆ>×!×!Ò!˜T×!×!×!×!×!×!Ó!Ø�;‰;�{Ñ# HÑ-ÐEÐ-×3Ñ3ÐEÑ3Ó5ÐE¸ÐEÐ5¸ÑE×E×EÕEÐ5¸×E×EÓEÐ-×E×EÒEÐ3×E×EÒEÐ5×E×EÒE¸×E×E×E×E×E×E×EØ×(Ñ(×.Ñ.Üœn×=Ñ=À}ÔUóÐð ×&Ñ&×,Ñ,Üœn×8Ñ8À-ÔPó€Oô Ð Ó!Ð& QÐ&Ð! QÑ&×&×&Õ&Ð! Q×&×&×&×&×&Ñ&Œ3×&×&Ó&Œ3×&×&×&×&×&Ò&Ð ×&×&Ó&Ð ×&×&Ô&Ð!×&×&Ò& Q×&×&×&×&×&×&Ó&ÜˆÓÐ$ 1Ð$Ð 1Ñ$×$×$Õ$Ð 1×$×$×$×$×$Ñ$Œ3×$×$Ó$Œ3×$×$×$×$×$Ò$ˆ×$×$Ó$ˆ×$×$Ô$Ð×$×$Ò$ 1×$×$×$×$×$×$Ó$Ø˜1ÑÐ3Ð×$Ñ$Ð3¨Ð3Ð$¨Ñ3×3×3Õ3Ð$¨×3×3Ó3Ð×3×3Ò3Ð$×3×3Ò3¨×3×3×3×3×3×3Ö3ð3ùð ûs%   ƒB%U;Â(U5Â)	U;Â2U8Â3SU;Õ8U;c               ƒ  óP
  K  — t        «       } | j                  dt        dddddddœ¬	«      «      ƒ d {  –—† }|j                  }d
}||u }|s­t	        j
                  d|fd||f«      dt        j                  «       v st	        j                  |«      rt	        j                  |«      ndt	        j                  |«      t	        j                  |«      dœz  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}}d}g }|j                  }|}|sd}	|	}||v }
|
�sddt        j                  «       v st	        j                  |«      rt	        j                  |«      ndt	        j                  |«      dœz  }|j                  |«       |s+ddt	        j                  	«      iz  }|j                  |«       t	        j                  |d«      i z  }t	        j
                  d|
fd||f«      t	        j                  |«      |dœz  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}
x}x}x}}	| j                  }|j                   } |«       }g }||k(  }|sÕt	        j
                  d|fd||f«      dt        j                  «       v st	        j                  | «      rt	        j                  | «      ndt	        j                  |«      t	        j                  |«      t	        j                  |«      t	        j                  |«      d œz  }d!d"|iz  }t        t	        j                  |«      «      ‚d x}x}x}x}}| j"                  j%                  t'        t(        j*                  d¬#«      «      }t-        |«      }
d}|
|k(  }|söt	        j
                  d|fd$|
|f«      d%t        j                  «       v st	        j                  t,        «      rt	        j                  t,        «      nd%d&t        j                  «       v st	        j                  |«      rt	        j                  |«      nd&t	        j                  |
«      t	        j                  |«      d'œz  }d(d)|iz  }t        t	        j                  |«      «      ‚d x}
x}}|d*   }|j.                  }
d&}|
|k(  }|s€t	        j
                  d|fd+|
|f«      t	        j                  |«      t	        j                  |
«      t	        j                  |«      d,œz  }d(d)|iz  }t        t	        j                  |«      «      ‚d x}x}
x}}y 7 �Œð­w)-Nzroadmap-no-scopeztask-code-deniedr    z&must be denied before entering sandboxzprint('must-not-run')r›   rœ   r�   r#   Fr(   r*   r+   r,   r0   r1   zlacks required scopero   rp   rq   rr   rs   r7   rt   )z%(py1)s in (%(py13)s))r<   rv   zassert %(py15)srw   r2   rz   ra   r{   r~   r   r4   r8   r9   r€   r;   r?   r@   r   rE   rC   )r   rO   r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   r�   rƒ   r„   r    r…   rZ   r[   r   r   ÚTOOL_CALLEDr9   r+   )ra   r+   rc   rd   re   rf   rg   rl   r‡   rˆ   rh   r‹   rŒ   Ú@py_format14Ú@py_format16ri   r�   r�   r‘   r€   rj   rk   s                         r   Ú4test_coder_does_not_run_code_without_execution_scoper¯   ¤   s  è ø€ ô ‹\€FØ×&Ñ&ØÜØ!ØØ@à/Ø$Ø ñô			
ó÷ €Fð �>‰>Ð"˜UÐ"ˆ>˜UÐ"×"×"Õ"ˆ>˜U×"×"×"×"×"Ñ"ˆ6×"×"Ó"ˆ6×"×"Ô"ˆ>×"×"Ò"˜U×"×"×"×"×"×"Ó"Ø!Ò9 f§l¡lÐ9 lÐ9¡l°bÐ9°bÐ9Ð!Ð9Ð9×9×9×9×9×9Ñ9 f×9×9Ó9 f×9×9Ô9 l×9×9×9Ñ9¡l×9×9Ð9°b×9×9×9×9×9×9×9×9Ô9Ð!×9×9Ô9Ð!×9×9×9×9×9×9×9Ò9Ø�<‰<Ð/ˆ<×'Ñ'Ð/Ñ'Ó)Ð/¨RÐ/Ð)¨RÑ/×/×/Õ/Ð)¨R×/×/×/×/×/Ñ/ˆ6×/×/Ó/ˆ6×/×/Ô/ˆ<×/×/Ò/Ð'×/×/Ò/Ð)×/×/Ò/¨R×/×/×/×/×/×/×/Ø×Ñ×#Ñ#Üœn×8Ñ8ÐBTÔUó€Fô ˆv‹;Ð˜!Ðˆ;˜!Ñ××Õˆ;˜!×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜!××××××ÓØ�!‰9Ð'ˆ9×ÑÐ'˜xÐ'Ð˜xÑ'×'×'Õ'Ð˜x×'×'Ó'ˆ9×'×'Ò'Ð×'×'Ò'˜x×'×'×'×'×'×'Ö'ð+ûs   ‚1T&³T#´S0T&c               ƒ  óÊ  ‡K  — t        «       Št        ‰d¬«      } | j                  j                  j                  j                  t        j                  j                  «       d3ˆfd„}t        j                   |«       «      }| j                  dt        ddddd	d
dœ¬«      «      ƒ d{  –—† }|ƒ d{  –—†  |j                  }d}||u }|sÍt        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }t        j(                  |j*                  «      dz   d|iz  }t-        t        j.                  |«      «      ‚dx}x}}|j0                  d   d   }	|	j2                  }
 |
«       }d}||k(  }|s”t        j                  d|fd||f«      t        j&                  |	«      t        j&                  |
«      t        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t-        t        j.                  |«      «      ‚dx}	x}
x}x}}‰j4                  }
 |
«       }t7        |«      }d}||k(  }|�s4t        j                  d|fd||f«      dt!        j"                  «       v st        j$                  t6        «      rt        j&                  t6        «      ndd t!        j"                  «       v st        j$                  ‰«      rt        j&                  ‰«      nd t        j&                  |
«      t        j&                  |«      t        j&                  |«      t        j&                  |«      d!œz  }t        j(                  d"«      d#z   d$|iz  }t-        t        j.                  |«      «      ‚dx}
x}x}x}}| j8                  j;                  t=        t>        j@                  d¬%«      «      }| j8                  j;                  t=        t>        jB                  d¬%«      «      }|D �cg c]  }|jD                  ‘Œ }	}d&g}|	|k(  }
|
slt        j                  d|
fd'|	|f«      t        j&                  |	«      t        j&                  |«      d(œz  }d)d*|iz  }t-        t        j.                  |«      «      ‚dx}	x}
}d+„ |D «       }tG        |«      }d,d-g}||k(  }|sÉt        j                  d|fd.||f«      d/t!        j"                  «       v st        j$                  tF        «      rt        j&                  tF        «      nd/t        j&                  |«      t        j&                  |«      t        j&                  |«      d0œz  }d1d2|iz  }t-        t        j.                  |«      «      ‚dx}x}x}}|D �ch c]  }|jH                  ’Œ }	}dh}|	|k(  }
|
slt        j                  d|
fd'|	|f«      t        j&                  |	«      t        j&                  |«      d(œz  }d)d*|iz  }t-        t        j.                  |«      «      ‚dx}	x}
}y7 �Œ«7 �Œ¤c c}w c c}w ­w)4aÖ  A code step needs exactly one human decision, and both gates still run.

    The operator approves the first request and nothing else. The step declares
    the action it really performs (``execute_python``), so the dispatcher's
    approval covers the execution gateway's re-check and the gateway records a
    reuse instead of raising a second request. Before this was fixed the run
    hung here until the approval timeout: the second request was never answered.
    ç      @r   c               “  óê   •K  — ‰ j                  «       s.t        j                  d«      ƒ d {  –—†  ‰ j                  «       sŒ.‰ j                  ‰ j                  «       d   j                  dd«       y 7 ŒD­w)Nr   r   r   zapproved oncer   ©r   s   €r   Úapprove_the_first_requestzatest_coder_step_raises_one_approval_that_the_inner_gate_reuses.<locals>.approve_the_first_requestÒ   s`   øè ø€ Ø×(Ñ(Ô*Ü—-‘- Ó&×&Ð&ð ×(Ñ(Õ*ð 	×Ñ˜)×0Ñ0Ó2°1Ñ5×@Ñ@À*ÈoÕ^ð 'úó   ƒ)A3¬A1­A3Á0A3zroadmap-single-approvalztask-single-approvalr    z%declare the action it really performsÚexecute_pythonzprint('single approval')r›   ©r!   rœ   rž   r#   NTr(   r*   r+   r,   ú
>assert %(py7)sr1   rŸ   r    zsingle approvalr2   r¡   r¢   r£   rs   r7   )zj%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.list_records
}()
})
} == %(py10)sr9   r   )r-   r<   r=   r/   r1   rs   z$one step must not need two decisionsz
>assert %(py12)sÚpy12r4   Úpending©z%(py1)s == %(py4)s©r<   r|   úassert %(py6)sr>   c              3  ó4   K  — | ]  }|j                   –— Œ y ­w)N)r+   )Ú.0Úevents     r   ú	<genexpr>zQtest_coder_step_raises_one_approval_that_the_inner_gate_reuses.<locals>.<genexpr>õ   s   è ø€ Ò4 5�%—,•,Ñ4ùs   ‚ÚapprovedÚreused)z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sÚsorted)r-   r.   r|   r1   zassert %(py9)sr}   rF   )%r   r   r    rI   rJ   rK   r	   r¦   rM   r   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   Ú_format_assertmsgr�   rW   rX   r§   r¨   Úlist_recordsr9   rZ   r[   r   r   r\   r]   r+   rÄ   r!   )ra   r´   rb   r+   rc   rd   re   rf   rg   rl   rh   r�   r‡   rk   r‹   rˆ   r�   Ú@py_format13r:   rA   rÀ   Ú@py_format5rj   ri   r‘   r   s                            @r   Ú>test_coder_step_raises_one_approval_that_the_inner_gate_reusesrÉ   Ã   s4  øè ø€ ô  Ó!€IÜ¨ÈSÔQ€FØ
‡L�L×Ñ× Ñ ×$Ñ$¤_×%;Ñ%;×%AÑ%AÔBõ_ô ×'Ñ'Ñ(AÓ(CÓD€MØ×&Ñ&Ø!ÜØ%ØØ?à*Ø2Ø$ñô			
ó÷ €Fð ×Ðà�>‰>Ð/˜TÐ/ˆ>˜TÐ!×/×/Õ/ˆ>˜T×/×/×/×/×/Ñ/ˆ6×/×/Ó/ˆ6×/×/Ô/ˆ>×/×/Ò/˜T×/×/×/ 6§<¡<×/×/×/×/×/×/Ð/Ø�;‰;�{Ñ# HÑ-ÐJÐ-×3Ñ3ÐJÑ3Ó5ÐJÐ9JÐJÐ5Ð9JÑJ×J×JÕJÐ5Ð9J×J×JÓJÐ-×J×JÒJÐ3×J×JÒJÐ5×J×JÒJÐ9J×J×J×J×J×J×J×JØ×%Ñ%ÐUÑ%Ó'ÐUŒ3Ð'Ó(ÐU¨AÐUÐ(¨AÑ-×U×UÖUÐ(¨A×U×U×U×U×UÑUŒ3×U×UÓUŒ3×U×U×U×U×UÒUˆy×U×UÓUˆy×U×UÔUÐ%×U×UÒUÐ'×U×UÒUÐ(×U×UÒU¨A×U×U×UÐ/U×U×U×U×U×U×UÔUð ×Ñ×%Ñ%Üœn×>Ñ>ÐH^Ô_ó€Hð ×Ñ×$Ñ$Üœn×=Ñ=ÐG]Ô^ó€Gð '/Ö/˜UˆE�L‹LÐ/Ð>Ð/°I°;Ð>Ð/°;Ñ>×>×>Õ>Ð/°;×>×>Ó>Ð/×>×>Ò>°;×>×>×>×>×>×>Ó>Ù4¨GÔ4ÐNŒ6Ð4Ó4ÐN¸ÀXÐ8NÐNÐ4Ð8NÑN×N×NÕNÐ4Ð8N×N×N×N×N×NÑNŒ6×N×NÓNŒ6×N×NÔNÐ4×N×NÒNÐ4×N×NÒNÐ8N×N×N×N×N×N×NÕNØ&.Ö/˜UˆE�L‹LÐ/ÐEÐ/Ð4DÐ3EÐEÐ/Ð3EÑE×E×EÕEÐ/Ð3E×E×EÓEÐ/×E×EÒEÐ3E×E×E×E×E×E×EÔEð;ùð ûò 0ùâ/ùsE   ƒB%Y#Â(YÂ)	Y#Â2YÂ3N	Y#Ð<YÑE2Y#×Y×B Y#ÙY#Ù
Y#c               ƒ  ó>  ‡K  — t        «       Št        ‰d¬«      } | j                  j                  j                  j                  t        j                  j                  «       dˆfd„}t        j                   |«       «      }| j                  dt        ddddd	d
dœ¬«      «      ƒ d{  –—† }|ƒ d{  –—†  |j                  }d}||u }|sÍt        j                  d|fd||f«      dt!        j"                  «       v st        j$                  |«      rt        j&                  |«      ndt        j&                  |«      t        j&                  |«      dœz  }t        j(                  |j*                  «      dz   d|iz  }t-        t        j.                  |«      «      ‚dx}x}}‰j1                  «       }	|	D �
cg c]  }
|
j2                  ‘Œ }}
dg}||k(  }|slt        j                  d|fd||f«      t        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t-        t        j.                  |«      «      ‚dx}x}}| j4                  j7                  t9        t:        j<                  d¬«      «      }|D �cg c]  }|j2                  ‘Œ }}dg}||k(  }|slt        j                  d|fd||f«      t        j&                  |«      t        j&                  |«      dœz  }dd|iz  }t-        t        j.                  |«      «      ‚dx}x}}y7 �Œe7 �Œ^c c}
w c c}w ­w)a—  A non-mutating label is not an approval for the code behind it.

    ``code`` is a known safe label, so the outer chain allows the step without a
    human. Because the action the gateway is about to perform is a different one
    (``execute_python``), the grant cannot apply and the gateway asks for the
    real action on its own - which is what stops a label from authorizing
    executable content.
    r±   r   c               “  óê   •K  — ‰ j                  «       s.t        j                  d«      ƒ d {  –—†  ‰ j                  «       sŒ.‰ j                  ‰ j                  «       d   j                  dd«       y 7 ŒD­w)Nr   r   r   rÂ   r   r³   s   €r   r   z\test_declared_label_still_asks_for_the_real_execution_action.<locals>.approve_when_requested  s^   øè ø€ Ø×(Ñ(Ô*Ü—-‘- Ó&×&Ð&ð ×(Ñ(Õ*à×Ñ˜)×0Ñ0Ó2°1Ñ5×@Ñ@À*ÈjÕYð 'úrµ   zroadmap-labelz
task-labelr    z"declared as the non-mutating labelrœ   zprint('labelled')r›   r·   r#   NTr(   r*   r+   r,   r¸   r1   r¶   r2   r»   r¼   r½   r>   r4   rF   )r   r   r    rI   rJ   rK   r	   r¦   rM   r   rN   rO   r   rP   rQ   rR   rS   rT   rU   rV   rÅ   r�   rW   rX   rÆ   r!   rZ   r[   r   r   r\   )ra   r   rb   r+   rc   rd   re   rf   rg   ÚrecordsÚrecordrl   rh   rÈ   rj   r:   rÀ   r   s                    @r   Ú<test_declared_label_still_asks_for_the_real_execution_actionrÎ   ù   s8  øè ø€ ô  Ó!€IÜ¨ÈSÔQ€FØ
‡L�L×Ñ× Ñ ×$Ñ$¤_×%;Ñ%;×%AÑ%AÔBõZô
 ×'Ñ'Ñ(>Ó(@ÓA€MØ×&Ñ&ØÜØØØ<à Ø+Ø$ñô			
ó÷ €Fð ×Ðà�>‰>Ð/˜TÐ/ˆ>˜TÐ!×/×/Õ/ˆ>˜T×/×/×/×/×/Ñ/ˆ6×/×/Ó/ˆ6×/×/Ô/ˆ>×/×/Ò/˜T×/×/×/ 6§<¡<×/×/×/×/×/×/Ð/Ø×$Ñ$Ó&€GØ(/Ö0˜fˆF�M‹MÐ0ÐFÐ0Ð5EÐ4FÐFÐ0Ð4FÑF×F×FÕFÐ0Ð4F×F×FÓFÐ0×F×FÒFÐ4F×F×F×F×F×F×FÓFØ×Ñ×%Ñ%Üœn×>Ñ>ÈÔUó€Hð '/Ö/˜UˆE�L‹LÐ/ÐEÐ/Ð4DÐ3EÐEÐ/Ð3EÑE×E×EÕEÐ/Ð3E×E×EÓEÐ/×E×EÒEÐ3E×E×E×E×E×E×EÔEð+ùð ûò 1ùò 0ùsE   ƒB%LÂ(LÂ)	LÂ2LÂ3C?LÆ2LÇB6LÉ;LÊB LÌLÌ
LrF   )!Ú__doc__Ú
__future__r   ÚbuiltinsrS   Ú_pytest.assertion.rewriteÚ	assertionÚrewriterQ   r   ÚshutilÚpytestÚ&cyberbrain26.orchestration.ptah_bridger   r   Ú$cyberbrain26.security_plane.approvalr   Ú!cyberbrain26.security_plane.auditr   r   Ú$cyberbrain26.security_plane.identityr	   ÚmarkÚskipifÚwhichÚrequires_bubblewraprm   r’   r˜   rª   r¯   rÉ   rÎ   © ó    r   ú<module>rá      sM  ðÙ Aå "ç  „ ƒÛ ã ç JÝ @ß HÝ @à—k‘k×(Ñ(Ø€F‡L�L�Ó˜TÐ!Ð*Eð )ó Ð ð
 ‡�×Ñò*.ó ð*.ðZ ‡�×Ñòó ðð. ‡�×Ñòó ðð> ‡�×ÑØò%4ó ó ð%4ðP ‡�×ÑØò(ó ó ð(ð: ‡�×ÑØò1Fó ó ð1Fðh ‡�×ÑØò(Fó ó ñ(Frà   