
    jJ                        d Z ddlmZ ddlZddlmc mZ ddl	Z	ddl
mZ ddlZddlmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZmZ  ee      j:                  dz  d	z  Zdd
ZddZ ddZ!ddZ"ddZ#ddZ$ddZ%y)u   tests.test_dispatch_gate_integration_2645 — task-2645 §17.13/§17.17.

dispatch.py 가 cron_response.status="ok" 만 보고 final success 보고하지 못하게
한다. 직접 cron workaround 와 dispatch.py 결과를 동일 spawn gate 로 검증.
    )annotationsN)Path)SpawnSignalsdispatched_dict_is_unverifiedevaluate_signalsis_silent_drop_confirmed)DISPATCH_SILENT_DROP_HOLDDISPATCH_SUBMITTED_UNVERIFIEDDISPATCH_VERIFIED_SPAWNassert_not_premature_successis_final_success)PROMPT_HARD_BLOCK_THRESHOLDclassify_prompt_bytes
is_allowedfixturesdispatch_gatec                    t        |       s| j                  d      ddS | j                  di       j                  d      xs d}t        ||      }|j                  }|t        k(  rt        |j                         dS ||j                         dS )u   dispatch.py 반환 dict + spawn signals 를 최종 status 로 settle.

    회장 verbatim 게이트 — cron_response.status=ok 단독으로 final success 보지 않음.
    statusN)final_statusverificationcron_responseid )r   getr   next_statusr
   to_json)dispatched_dictsignalscron_idrfinals        Z/home/jay/workspace/.worktrees/task-2645-dev2/tests/test_dispatch_gate_integration_2645.py_settle_to_finalr#   %   s    
 )9 / 3 3H =tTT!!/26::4@FBG'*AMME-- =qyy{[[!199;??    c            	        t        j                  t        dz  j                               } | d   }|d   }t	        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}x}x}}|d   }d}||u }	|	slt        j                  d|	fd||f      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            d x}x}	}t        |d   t        di |d         }|d   }|t        k(  }	|	st        j                  d|	fd|t        f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              nddz  }dd|iz  }t        t        j                  |            d x}}	|d   }|d   }||k(  }	|	slt        j                  d|	fd||f      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            d x}x}	}|d   }t!        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}x}x}}|d   }d}||u }	|	slt        j                  d|	fd||f      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            d x}x}	}y )Ndispatch_false_ok_replay.jsondispatch_py_pathr   Tisz0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} is %(py7)sr   py0py2py4py7assert %(py9)spy9expected_unverified_before_gate)z%(py1)s is %(py4)spy1r.   assert %(py6)spy6spawn_signalsr   ==z%(py1)s == %(py3)sr	   r4   py3assert %(py5)spy5 expected_final_status_after_gatez%(py1)s == %(py4)sFr   expected_is_final_success )jsonloadsFIXTURE_DIR	read_textr   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationr#   r   r	   r   )fixcase@py_assert1@py_assert3@py_assert6@py_assert5@py_format8@py_format10@py_assert0@py_assert2@py_format5@py_format7settled@py_format4@py_format6s                  r"   7test_false_ok_replay_dispatch_py_settles_to_silent_dropr^   7   s   
**k$CCNNP
QC!"D *..?)@I()@AITIATIIIIATIIIIII(III(III)@IIIAIIITIIIIIII12:d:2d::::2d:::2:::d::::::: t$568]tOG\8]^G>"?"&?????"&????"??????&????&????????>"Nd+M&NN"&NNNNN"&NNNN"NNN&NNNNNNNN $N3=34==4====4============3===4==========+,55,5555,555,5555555555r$   c            	        t        j                  t        dz  j                               } | d   }|d   }t	        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}x}x}}t        |d   t        di |d         }|d   }	|	t        k(  }
|
st        j                  d|
fd|	t        f      t        j                  |	      dt        j                         v st        j                  t              rt        j                  t              nddz  }dd|iz  }t        t        j                  |            d x}	}
|d   }	|d   }|	|k(  }
|
slt        j                  d|
fd|	|f      t        j                  |	      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}	x}
}|d   }t!        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}x}x}}y )Nr&   direct_cron_pathr   Tr(   r*   r   r+   r0   r1   r7   r   r8   r:   r   r;   r=   r>   r?   r@   r3   r5   r6   r   rB   )rC   rD   rE   rF   r   rG   rH   rI   rJ   rK   rL   rM   rN   r#   r   r   r   )rO   rP   rQ   rR   rS   rT   rU   rV   r[   rW   rX   r\   r]   rY   rZ   s                  r"   :test_false_ok_replay_direct_cron_settles_to_verified_spawnra   I   s    
**k$CCNNP
QC!"D)-.?)@I()@AITIATIIIIATIIIIII(III(III)@IIIAIIITIIIIIIIt$568]tOG\8]^G>"="&====="&===="======&====&========>"Nd+M&NN"&NNNNN"&NNNN"NNN&NNNNNNNN#N3<34<<4<<<<4<<<<<<<<<<<<3<<<4<<<<<<<<<<r$   c                    t        j                  t        dz  j                               } t	        di | d   d   }t	        di | d   d   }| d   d   }| d   d   }t        ||      }t        ||      }|d   }|d   }||k(  }	|t        k(  }
|	r|
st        j                  d|	|
fd||t        f      t        j                  |      t        j                  |      d	t        j                         v st        j                  t              rt        j                  t              nd	d
z  }dd|iz  }t        t        j                  |            dx}x}	x}
}t        ||      }t        ||      }|d   }|d   }||k(  }	|t        k(  }
|	r|
st        j                  d|	|
fd||t        f      t        j                  |      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndd
z  }dd|iz  }t        t        j                  |            dx}x}	x}
}y)uS   동일한 spawn signals 면 dispatch.py 든 직접 cron 이든 동일 final status.r&   r'   r7   r`   r   r   )r9   r9   )z%(py1)s == %(py5)sz%(py5)s == %(py6)sr	   )r4   r>   r6   assert %(py8)spy8Nr   rB   )rC   rD   rE   rF   r   r#   r	   rG   rH   rL   rI   rJ   rK   rM   rN   r   )rO   
sig_silent	sig_alivedispatch_py_dictdirect_dicts1s2rW   @py_assert4rX   rR   rZ   @py_format9s3s4s                  r"   6test_dispatch_py_and_direct_cron_share_same_gate_logicro   T   s   
**k$CCNNP
QCI$6 7 HIJHs#56GHI-./@A()*;<K 
*J	7B	+z	2BnPN!3P!3PP!37PPPPPP!37PPPPPPP!3PPPPPP7PPPP7PPPPPPPP 
*I	6B	+y	1BnNN!3N!3NN!37NNNNNN!37NNNNNNN!3NNNNNN7NNNN7NNNNNNNNNr$   c                    t         } t        j                  t              5  t	        |        ddd       d}t        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t
              rt        j                  t
              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}}d}t        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t
              rt        j                  t
              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}}y# 1 sw Y   -xY w)uR   ANCHOR-1: status=DISPATCH_SUBMITTED_UNVERIFIED 단독 final success 보고 금지.NokFr(   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} is %(py6)sr   fake_successr,   r4   r<   r6   rc   rd   
dispatchedfake_success_2)r
   pytestraisesrM   r   r   rG   rH   rI   rJ   rK   rL   rN   )pre_gate_statusrs   rX   rT   rk   rZ   rl   rv   s           r"   0test_false_dispatched_report_must_fail_assertionrz   i   sb    4O	~	& 6$_56 LL)2U2)U2222)U222222222222222L222L222)222U2222222!NN+4u4+u4444+u444444444444444N444N444+444u44444446 6s   II!c                    dt         dz   z  } t        |       }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}}t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }	dd|	iz  }
t        t        j                  |
            d
x}x}}y
)uO   prompt >3900 bytes 면 dispatch 전 차단 — spawn gate 까지 가지 않음.x   Tr(   )z2%(py2)s
{%(py2)s = %(py0)s.hard_block
} is %(py5)sc)r,   r-   r>   zassert %(py7)sr/   NFrr   r   overrt   rc   rd   )r   r   
hard_blockrG   rH   rI   rJ   rK   rL   rM   rN   r   )r   r~   rQ   rk   rR   r]   rU   rX   rT   rZ   rl   s              r"   6test_prompt_hardblock_short_circuits_before_spawn_gater   w   s   -12Dd#A<<4<4<411<4d$u$u$$$$u$$$$$$:$$$:$$$$$$d$$$d$$$$$$u$$$$$$$r$   c                    d} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               nddt        j                         v st        j
                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}}t        dddddd      }t        d|      }|j                  }|t        k(  }	|	st        j                  d|	fd|t        f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j
                  t              rt        j                  t              nddz  }
dd|
iz  }t        t        j                  |            d
x}}	t        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t              rt        j                  t              nddt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}}y
)u8   ≤3200 prompt + 정상 spawn 신호 → VERIFIED_SPAWN.  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxTr(   rr   r   promptrt   rc   rd   NF)workspace_dir_existsworkspace_dir_has_activityexecutor_process_presentschedule_visible_in_cron_list schedule_accepted_marker_presentschedule_history_log_presentOK1234r8   )z3%(py2)s
{%(py2)s = %(py0)s.next_status
} == %(py4)sr    r   )r,   r-   r.   r5   r6   r   )r   rG   rH   rI   rJ   rK   rL   rM   rN   r   r   r   r   r   )r   rX   rT   rk   rZ   rl   sigr    rQ   rR   rY   s              r"   7test_prompt_target_proceeds_and_spawn_gate_then_settlesr      s   Ff%%%%%%%%%%%%:%%%:%%%%%%f%%%f%%%%%%%%%%%%%
!d!%U)-DC
 	3'A==3=33333=333333313331333=333333333333333333#A&/%/&%////&%//////#///#//////A///A///&///%///////r$   )r   dictr   r   returnr   )r   None)&__doc__
__future__r   builtinsrI   _pytest.assertion.rewrite	assertionrewriterG   rC   pathlibr   rw   utils.dispatch_spawn_verifierr   r   r   r   utils.dispatch_status_enumr	   r
   r   r   r   utils.prompt_byte_classifierr   r   r   __file__parentrE   r#   r^   ra   ro   rz   r   r   rB   r$   r"   <module>r      sz   
 #         8n##j0?B@$6$=O*
5%0r$   