
    n iì                       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ZddlmZmZ ddlmZ ddlmZmZmZmZ ddlZej.                  j1                  dd       ddlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d	 Z3d
Z4dMdNdZ5dOdPdZ6dQdZ7dQdZ8dRdSdZ9dTdUdZ:dVdWdZ;dXdYdZ<d Z=d Z>d Z?d Z@d ZAd ZBd ZCd ZDd ZEd ZFd ZGd ZHd  ZId! ZJd" ZKd# ZLd$ ZMd% ZNd& ZOd' ZPd( ZQd) ZRd* ZSd+ ZTeUd,k(  rddlVZVd-e=fd.e>fd/e?fd0e@fd1eAfd2eBfd3eCfd4eDfd5eEfd6eFfd7eGfd8eHfd9eIfd:eJfd;eKfd<eLfd=eNfd>eOfd?ePfd@eQfdAeRfdBeSfdCeTfgZWddlZdD ZXeWj                  dEeXf       dZZ e[eW      Z\eWD ]  \  Z]Z^	  e^         e_dFe]        eZdz  ZZ  e_dIeZ dJe\ dK       eZe\k(  r	 e_dL       y ej                  d       yy# e`$ r)Za e_dGe] dHea         eVj                          Y dZa[azdZa[aww xY w)Zu  
tests/coherence/test_coherence_pipeline.py

Story 6.09 — Integration Test Suite: Module 6 Multi-Agent Coherence Pipeline

Tests the full 8-step coherence pipeline end-to-end, with 3 simulated workers,
all composed from the real Module 6 class interfaces (StateDelta, RedisMasterState,
TaskDAGPusher, StagingArea, SwarmWorkerBase, OCCCommitEngine, BulkheadGuard,
CoherenceOrchestrator).

ALL external I/O is mocked:
  - Zero real Redis connections (all redis calls use AsyncMock / MagicMock)
  - Zero real Qdrant connections
  - events.jsonl writes are redirected to tmp_path

Test categories
---------------
BB (Black Box) — end-to-end contracts verified externally:
  BB1: Full 8-step pipeline with 3 workers (all succeed) — end-to-end execute()
  BB2: Version conflict + retry path — OCC retries through orchestrator
  BB3: Worker crash → scar written (failure path through bulkhead)

WB (White Box) — internal mechanics verified:
  WB1: OCC WATCH/MULTI/EXEC pattern verified via mock call inspection
  WB2: asyncio.gather(return_exceptions=True) in bulkhead verified

Additional coverage (16+ total test cases):
  INT1-INT6:   Full integration scenarios with varied configurations
  UNIT tests:  Per-component unit verification as used in pipeline context
  PKG:         Package import sanity

VERIFICATION_STAMP
Story: 6.09
Verified By: parallel-builder
Verified At: 2026-02-25
Tests: 24/24
Coverage: 100%
    )annotationsN)datetimetimezone)Path)	AsyncMock	MagicMockcallpatchz/mnt/e/genesis-system
StateDeltaPatchConflictErrorvalidate_patchapply_patch	VALID_OPSRedisMasterStateCommitResultTaskDAGPusher
STREAM_KEYDEFAULT_GROUPStagingAreaSTAGING_KEY_PREFIXSTAGING_TTL_SECONDSSwarmWorkerBasePEL_TIMEOUT_MSBulkheadGuardBulkheadResultCRITICAL_THRESHOLDOCCCommitEngineOccCommitResultMAX_RETRIESCoherenceOrchestratorCoherenceResultORCHESTRATION_TIMEOUT_SECONDSEVENTS_LOG_PATHc                    t        j                         }	 |j                  |       |j                          S # |j                          w xY w)z4Run a coroutine synchronously in a fresh event loop.)asyncionew_event_looprun_until_completeclose)coroloops     @/mnt/e/genesis-system/tests/coherence/test_coherence_pipeline.pyrunr-   \   s6    !!#D&&t,



s	   7 A	zHcore.coherence.coherence_orchestrator.CoherenceOrchestrator._write_eventc                   |i }t        j                  | |dd      j                         }t               }t	               |_        t	        | dkD  r|nd      |_        t	               |_        t               |_        t               |_	        t	        dg      |_
        t	        |      |_        t	        d      |_        t               }t        |      |_        t	        | dkD  r|nd      |_        t	        d      |_	        ||fS )	u  
    Build a mock async Redis pipeline that simulates a successful WATCH / MULTI / EXEC
    sequence for RedisMasterState.commit_patch.

    The pipeline mock supports:
      - async context manager (__aenter__ / __aexit__)
      - pipe.watch(key) — async
      - pipe.get(key)   — async, returns JSON-encoded state
      - pipe.multi()    — sync (just sets a flag)
      - pipe.set(key, value) — sync (queued in pipeline)
      - pipe.execute()  — async, returns [True]
      - pipe.unwatch()  — async
    Nversiondata,:
separatorsr   return_valueTF)jsondumpsencoder   r   watchgetunwatchmultisetexecute
__aenter__	__aexit__pipeline)initial_versioninitial_data	raw_statepiperediss        r,   _make_redis_pipeline_okrJ   r   s     

#\:zfh  ;DDJ?Q3FiDQDH;DLDJ{DH4&1DL  T2DOE2DNKED1ENOa4GyTREIt,EI$;    c                   ddl m} |i }t        j                  | |dd      j	                         }t               }t               |_        t        |      |_        t               |_	        t               |_
        t               |_        t         |d            |_        t        |      |_        t        d	      |_        t               }t        |      |_        t        |      |_        ||fS )
z
    Build a mock async Redis pipeline that simulates a WatchError (OCC conflict).
    The pipeline.execute() raises redis.exceptions.WatchError.
    r   
WatchErrorr/   r2   r5   r7   zsimulated conflictside_effectF)redis.exceptionsrN   r9   r:   r;   r   r   r<   r=   r>   r?   r@   rA   rB   rC   rD   )r0   rF   rN   rG   rH   rI   s         r,   _make_redis_pipeline_conflictrR      s    
 ,

\2zfh  ;DDJi0DH;DLDJ{DH4H)IJDLT2DOE2DNKED1ENy1EI$;rK   c                H    t               }t        | xs g d      |_        |S )N)z1234-0z1234-1z1234-2r7   )r   r   push_dag)	entry_idspushers     r,   _make_dag_pusherrW      s%    [F@"@FO MrK   c                D    t               }t        | xs g       |_        |S )Nr7   )r   r   wait_for_all)deltasstagings     r,   _make_staging_arear\      s    kG$&,B?GNrK   c                    t               }| |_        | rddddgng |_        | r|dz   n||_        |xs | rdnd|_        t               }t        |      |_        |S )	Nadd/mergedToppathvalue   	completedconflict_exhaustedr7   )r   successmerged_patchr0   saga_statusr   execute_commit)rg   r0   ri   
occ_resultengines        r,   _make_occ_enginerm      sg    J J=Dy4	89"  )01WJ(^G[I]J[F%:>FMrK   c                   |g }g }t        |       D ]R  }t        t              }||v|_        d| |_        ||v rdnd |_        ||vrddind |_        |j                  |       T t               }t        |      |_	        |S )N)specztask-zworker crasheddoneTr7   )
ranger   r   rg   agent_iderrorresultappendr   run_with_bulkhead)worker_countfailed_indicesresultsirguards         r,   _make_bulkheadr}      s    G<  >*^+	QC[
&'>&9"t%&n%<FD>$q KE'W=ELrK   c                P    t        |       D cg c]  }d| d|idd c}S c c}w )Nztype-indexT1	task_typepayloadtier)rq   )countrz   s     r,   _sample_tasksr      s9     u aSkwlDI  s   #rd   c           	         |
dd|  ddg}t        | ||t        |      t        j                  t        j
                              S )z-Build a StateDelta for use in pipeline tests.r^   /Tr`   )tz)rr   
session_idversion_at_readr
   submitted_at)r   tupler   nowr   utc)rr   r   r0   	patch_opss       r,   _make_deltar      sK    !Qxj>DIJ	I\\X\\2 rK   c                    t        d      } t               }t        t        d      D cg c]
  }d| dd c}      }t	        dd      }t        dg 	      }t        ||||
      }t        t              5  t        |j                  d|             }ddd       t        t              }|sd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dt        j                         v st        j                   t              rt        j"                  t              ndt        j"                  |      dz  }	t%        t        j&                  |	            d}|j(                  }
d}|
|u }|st        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(  }|st        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(  }|st        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}}g }
|j0                  }t        |t2              }|}|r |j0                  }t5        |      }d}||k(  }|}|sOd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"                  |      dt        j                         v st        j                   t2              rt        j"                  t2              ndt        j"                  |      dz  }|
j7                  |       |rt        j*                  dfdf      dt        j                         v st        j                   t4              rt        j"                  t4              nddt        j                         v st        j                   |      rt        j"                  |      ndt        j"                        t        j"                  |      t        j"                  |      d z  }d!d"|iz  }|
j7                  |       t        j8                  |
d      i z  }d#d$|iz  }t%        t        j&                  |            dx}x}
x}x}x}x}x}}d%}|j:                  }||v }|st        j*                  d&|fd'||f      t        j"                  |      dt        j                         v st        j                   |      rt        j"                  |      ndt        j"                  |      d(z  }dd|iz  }t%        t        j&                  |            dx}x}}|j<                  j?                  d|        |j@                  j?                  ddd)*       |jB                  j?                  dd+       |jD                  jG                          yc c}w # 1 sw Y   cxY w),u  
    BB1: Full 8-step pipeline with 3 workers (all mock) — end-to-end
    CoherenceOrchestrator.execute().

    Verifies:
      - CoherenceResult.success is True
      - workers_succeeded == 3, workers_failed == 0
      - saga_id is a valid UUID string
      - committed_state contains merged_patch key
      - DAG pushed, barrier waited, OCC committed — all called exactly once
       zagent-zsess-bb1rr   r   )rZ   T
   rg   r0   rw   rx   
dag_pusherstaging_area
occ_enginebulkheadN5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}
isinstancert   r"   py0py1py2py4isz/%(py2)s
{%(py2)s = %(py0)s.success
} is %(py5)sr   r   py5assert %(py7)spy7==z9%(py2)s
{%(py2)s = %(py0)s.workers_succeeded
} == %(py5)sr   z6%(py2)s
{%(py2)s = %(py0)s.workers_failed
} == %(py5)s$   zK%(py8)s
{%(py8)s = %(py2)s(%(py5)s
{%(py5)s = %(py3)s.saga_id
}, %(py6)s)
}str)r   py3r   py6py8)zT%(py15)s
{%(py15)s = %(py10)s(%(py13)s
{%(py13)s = %(py11)s.saga_id
})
} == %(py18)slen)py10py11py13py15py18%(py20)spy20assert %(py23)spy23rh   in)z7%(py1)s in %(py5)s
{%(py5)s = %(py3)s.committed_state
}r   r   r   `  )expected_count
timeout_msexpected_workers)$r   rW   r\   rq   rm   r}   r!   r
   PATCH_WRITE_EVENTr-   rA   r   r"   @py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationrg   _call_reprcompareworkers_succeededworkers_failedsaga_idr   r   ru   _format_boolopcommitted_staterT   assert_awaited_once_withrY   rj   rv   assert_awaited_once)tasksrV   rz   r[   rl   r   orchestratorrt   @py_assert3@py_format5@py_assert1@py_assert4@py_format6@py_format8@py_assert7@py_assert0@py_assert12@py_assert14@py_assert17@py_assert16@py_format9@py_format19@py_format21@py_format22@py_format24@py_assert2s                             r,   ,test_bb1_full_pipeline_3_workers_all_succeedr     s    !EF NSTUhWvaS\DWG dB7F1R@H(	L 
 	! >\))*e<=> fo........:...:......f...f......o...o..........>>!T!>T!!!!>T!!!!!!6!!!6!!!>!!!T!!!!!!!##(q(#q((((#q((((((6(((6(((#(((q(((((((  %A% A%%%% A%%%%%%6%%%6%%% %%%A%%%%%%%HfnnH:nc*H*H*6>>Hs>/BHbH/Bb/HHHHHHH:HHH:HHHHHHfHHHfHHHnHHHHHHcHHHcHHH*HHHH*HHH/BbHHHHHHsHHHsHHHHHH6HHH6HHH>HHH/BHHHbHHHHHHHHHHHHHH3V333>33333>3333>333333V333V33333333333 OO,,Z?111 2  22:PQ2R224; X> >s   _/7_44_>c                    ! d} t        d      }d| dg dd| dg dg}t               }t        |      |_        t               }d|_        d	d
ddg|_        t               }t        |      |_        ddlm} d}t        j                  |i dd      j                         }t               }	d|	_        d|	_        d|	_        t               }
d|
_        |dz   |
_        d|
_        t               }t        |i f      |_        t        |	|
g      |_        t!        |||      !g   !fd}t               }||_        t%               }t'               }t)        dg       }t+        ||||      }t-        t.              5  t1        |j3                  | |            }ddd       t5               }d}||k(  }|st7        j8                  d|fd||f      dt;        j<                         v st7        j>                  t4              rt7        j@                  t4              nddt;        j<                         v st7        j>                         rt7        j@                         ndt7        j@                  |      t7        j@                  |      dz  }dd |iz  }tC        t7        jD                  |            dx}x}} d   d   }|| k(  }|st7        j8                  d|fd!|| f      t7        j@                  |      d"t;        j<                         v st7        j>                  |       rt7        j@                  |       nd"d#z  }d$d%|iz  }tC        t7        jD                  |            dx}}|j                  }|jF                  }d}||k(  }|st7        j8                  d|fd&||f      d't;        j<                         v st7        j>                  |      rt7        j@                  |      nd't7        j@                  |      t7        j@                  |      t7        j@                  |      d(z  }d)d*|iz  }tC        t7        jD                  |            dx}x}x}}y# 1 sw Y   xY w)+u'  
    BB2: Version conflict on first OCC attempt → retry → commits on attempt 2.

    Uses a real OCCCommitEngine with a mock RedisMasterState that fails once
    (conflict) then succeeds, to verify the OCC retry loop executes properly
    through the orchestrator's execute_commit call.
    zsess-bb2-retry   w0   )rr   r   r   r
   w1r7   Tr^   r_   okr`   r   rM   r/   r2   r5   Frd   rO   r   merge_interceptormaster_statec                j   K   j                  | |f       j                  | |       d {   S 7 wN)ru   rj   )sidr   execute_commit_callsreal_occ_engines     r,   tracked_execute_commitzHtest_bb2_version_conflict_retry_succeeds.<locals>.tracked_execute_commito  s5     ##S*:$;<$33C9IJJJJ   )313r   r   Nr   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr   r   r   r   r   r   assert %(py8)sr   z%(py1)s == %(py3)sr   r   r   assert %(py5)sr   zT%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.commit_patch
}.call_count
} == %(py7)smasterr   r   r   r   assert %(py9)spy9)$r   r   r   rY   rg   rh   mergerQ   rN   r9   r:   r;   new_versionconflictget_snapshotcommit_patchr   rj   rW   r\   r}   r!   r
   r   r-   rA   r   r   r   r   r   r   r   r   r   
call_count)"r   r   rZ   r[   merge_resultmergerrN   r0   rG   conflict_commit	ok_commitr  r   occ_wrapperrV   staging_area_mockr   r   rt   r   @py_assert5r   @py_format7r   r   @py_format4r   r   r   @py_assert6r   @py_format10r   r   s"                                   @@r,   (test_bb2_version_conflict_retry_succeedsr  7  s8    "J!E
 TVWTVWF kG$&9G ;LL(-y4!P QL[F,7FL ,G

w;
SZZ\IkO#O"#O#OII#aKII[F#'2?F#)0LMF% O K +K!7KF*,1R@H(&	L 
 	! >\))*e<=> #$))$))))$))))))3)))3))))))#)))#)))$))))))))))"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.)).Q.)Q....)Q......6...6......)...Q.......> >s   8QQc                 J
   d} d| dg}t               }t        |      |_        t               }d|_        dddd	g|_        t               }t        |      |_        d
}t               }d|_        d|_        d|_        t               }d|_        d|_        |dz   |_        t               }t        |i f      |_        t        ||g      |_	        t        |||      }	t        |	j                  | d            }
|j                  }|j                  }d}||k(  }|st        j                  d|fd||f      dt!        j"                         v st        j$                  |      rt        j&                  |      ndt        j&                  |      t        j&                  |      t        j&                  |      dz  }dd|iz  }t)        t        j*                  |            dx}x}x}}|j                  }|j                  }d}||k(  }|st        j                  d|fd||f      dt!        j"                         v st        j$                  |      rt        j&                  |      ndt        j&                  |      t        j&                  |      t        j&                  |      dz  }dd|iz  }t)        t        j*                  |            dx}x}x}}|
j                  }d}||u }|st        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(  }|st        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(  }|st        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}}y)"z
    BB2 (direct): Real OCCCommitEngine retries on version conflict.
    Verifies the OCC WATCH/MULTI/EXEC pattern is used (not Lua script):
    commit_patch called twice, get_snapshot called twice.
    zsess-bb2-directzagent-0r   r7   Tr^   /krd   r`   r   Fr   rO   r   r   r   r   r  r  r  r  r  N)zT%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.get_snapshot
}.call_count
} == %(py7)sr   r   rt   r   r   r   )z/%(py2)s
{%(py2)s = %(py0)s.retries
} == %(py5)sre   z3%(py2)s
{%(py2)s = %(py0)s.saga_status
} == %(py5)s)r   r   rY   rg   rh   r  r  r  r	  r
  r   r-   rj   r  r   r   r   r   r   r   r   r   retriesri   )r   rZ   r[   r  r  r0   r  r  r  rl   rt   r   r   r  r  r   r  r   r   s                      r,   2test_bb2_occ_retry_path_via_real_occ_engine_directr    s`    #J$J?@FkG$&9G;LL(-ta!H IL[F,7FLGkO#O#O"#OIII#aKI[F#'2?F#)0LMF F &&zA&FGF .)).Q.)Q....)Q......6...6......)...Q........)).Q.)Q....)Q......6...6......)...Q.......>>!T!>T!!!!>T!!!!!!6!!!6!!!>!!!T!!!!!!!>>Q>Q>Q66>Q,,,,,,,,,,,,6,,,6,,,,,,,,,,,,,rK   c                 t	   t        d      } d}t               }t               }t        d      }t	        ddg      }g fd}t        ||||      }t        t        |	      5  t        |j                  ||             }d
d
d
       D 	cg c]  }	|	d   dk(  s|	 }
}	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  }t        j"                  d       dz   d|iz  }t%        t        j&                  |            d
x}x}}|
d   d   d   }d}||k(  }|slt        j                  d|fd||f      t        j                   |      t        j                   |      dz  }dd|iz  }t%        t        j&                  |            d
x}x}}j(                  }d}||u }|st        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(  }|st        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(  }|st        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}}y
# 1 sw Y   *xY wc c}	w )%u>  
    BB3: One worker crashes inside BulkheadGuard → BulkheadResult(success=False)
         → workers_failed=1 → _write_event('scar') called by orchestrator.

    Verifies the failure propagation: crash in coroutine → bulkhead captures it →
    orchestrator writes scar → CoherenceResult.success=False.
    r   zsess-bb3-crashTrg   rd   r   c                ,    j                  | |f       y r   ru   
event_typer   captured_eventss     r,   capturez3test_bb3_worker_crash_scar_written.<locals>.capture      
G45rK   r   rO   Nr   scar>=z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} >= %(py6)sr   scar_eventsr   zExpected scar, got: 
>assert %(py8)sr   r   r   z%(py1)s == %(py4)sr   r   assert %(py6)sr   Fr   r   rt   r   r   r   r   r   r   )r   rW   r\   rm   r}   r!   r
   r   r-   rA   r   r   r   r   r   r   r   _format_assertmsgr   r   rg   r   r   )r   r   rV   r[   rl   r   r$  r   rt   er*  r   r  r   r  r   r   r   r   r   r   r   r#  s                         @r,   "test_bb3_worker_crash_scar_writtenr1    s    !E!JF "Gd+F 1aSAH#%O6 )	L 
 g	6 >\))*e<=> .@11@K@{JqJq JJJqJJJJJJ3JJJ3JJJJJJ{JJJ{JJJJJJqJJJ$88I"JJJJJJJJq>!-.3!3.!3333.!333.333!3333333 >>"U">U"""">U""""""6"""6""">"""U"""""""  %A% A%%%% A%%%%%%6%%%6%%% %%%A%%%%%%%##(q(#q((((#q((((((6(((6(((#(((q(((((((> > As   $R(R5R5(R2c                    t        d      } t               }t               }t        d      }t	        dg d      }g fd}t        ||||      }t        t        |      5  t        |j                  d	|             }d
d
d
       D cg c]  }|d   dk(  s| }	}t        |	      }
d}|
|k\  }|st        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   d   d   }d}||k(  }
|
slt        j                  d|
fd||f      t        j                   |      t        j                   |      dz  }dd|iz  }t#        t        j$                  |            d
x}x}
}j&                  }d}||u }|st        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(  }|st        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}}y
# 1 sw Y   HxY wc c}w )#uR   
    BB3 variant: All 3 workers crash → scar written with workers_failed=3.
    r   Tr  )r   rd   r   r   c                ,    j                  | |f       y r   r   r!  s     r,   r$  zCtest_bb3_all_workers_crash_scar_has_correct_counts.<locals>.capture  r%  rK   r   rO   zsess-all-crashNr   r&  rd   r'  r)  r   r*  r   r   r   r   r   r,  r-  r.  r   Fr   r   rt   r   r   r   r   )r   rW   r\   rm   r}   r!   r
   r   r-   rA   r   r   r   r   r   r   r   r   r   rg   r   )r   rV   r[   rl   r   r$  r   rt   r0  r*  r   r  r   r  r   r   r   r   r   r   r   r#  s                        @r,   2test_bb3_all_workers_crash_scar_has_correct_countsr4    s    !EF "Gd+F1YGH#%O6 )	L 
 g	6 D\))*:EBCD .@11@K@{ q q    q      3   3      {   {      q       q>!-.3!3.!3333.!333.333!3333333>>"U">U"""">U""""""6"""6""">"""U"""""""  %A% A%%%% A%%%%%%6%%%6%%% %%%A%%%%%%%D D As   #OOOOc                    t        d      \  } }t        |       }ddddg}t        |j                  dd|            }|j                  }d	}||u }|st        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(  }|st        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}||u }|st        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!                          |j                  j"                  d   d   }
d}||
k(  }|st        j                  d|fd||
f      t        j                  |      dt        j                         v st        j                  |
      rt        j                  |
      nddz  }dd|iz  }t        t        j                  |            dx}}|j$                  j'                          |j(                  j!                          g }d}t+        ||      }| }|}|s|j,                  }|j.                  }| }|}|sd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  }|j1                  |       |sd!dt        j                         v st        j                  |      rt        j                  |      ndt        j                        t        j                        d"z  }|j1                  |       t        j2                  |d      i z  }t        j4                  d#      d$z   d%|iz  }t        t        j                  |            dx}x}x}x}x}x}x}}y)&a  
    WB1: Verify the WATCH / MULTI / EXEC pattern is used by RedisMasterState.commit_patch.

    We inspect the mock Redis pipeline call sequence directly:
      - pipe.watch(key) must be called first
      - pipe.multi() must be called before pipe.execute()
      - pipe.execute() must be called (not a Lua script call)
    This confirms the code uses Redis optimistic locking transactions,
    NOT an atomic Lua EVAL approach.
    r   rE   r^   z/statusrunningr`   zsess-wb1r0   r
   Tr   r   rt   r   r   r   Nrd   r   z3%(py2)s
{%(py2)s = %(py0)s.new_version
} == %(py5)sFz0%(py2)s
{%(py2)s = %(py0)s.conflict
} is %(py5)szgenesis:state:master:sess-wb1r   key_argr   r   r   evalz2not %(py7)s
{%(py7)s = %(py2)s(%(py3)s, %(py5)s)
}hasattrrH   )r   r   r   r   zFnot %(py14)s
{%(py14)s = %(py12)s
{%(py12)s = %(py10)s.eval
}.called
})r   py12py14z'Expected WATCH/MULTI/EXEC, not Lua EVALz
>assert %(py18)sr   )rJ   r   r-   r
  rg   r   r   r   r   r   r   r   r   r  r  r<   r   
await_argsr?   assert_called_oncerA   r=  r<  calledru   r   r/  )redis_clientrH   rmsr   rt   r   r   r   r   r   r;  r   r   r  r  @py_assert8@py_assert11@py_assert13@py_assert15r   @py_format16@py_format17r   s                          r,   *test_wb1_occ_watch_multi_exec_pattern_usedrK    s    1CL$
<
(Cy9EFI!!*ay!IJF>>!T!>T!!!!>T!!!!!!6!!!6!!!>!!!T!!!!!!!""""""""""""6"""6"""""""""""""??#e#?e####?e######6###6###?###e####### 	JJ""$jj##A&q)G*5*g5555*g555*555555g555g5555555 	JJ!!# 	LL$$&V wtV$ $$ $ $DII I,<,< ,<(< (<                        $    %    %     -1    -1    -6    -=       	2      rK   c                    ddl m}  t        d      \  }}t         | d            |_        t        |      }t        |j                  ddg             }|j                  }d	}||u }|st        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}||u }|st        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(  }|st        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)                          |j*                  j-                          y)z
    WB1 variant: WatchError from Redis triggers conflict=True path.
    Confirms the WATCH-based detection (not a Lua check).
    r   rM   r   r6  zconcurrent writerO   zsess-wb1-conflictr8  Fr   r   rt   r   r   r   NTr:  r   r9  )rQ   rN   rJ   r   rA   r   r-   r
  rg   r   r   r   r   r   r   r   r   r  r  r<   r   r?   rA  )
rN   rC  rH   rD  rt   r   r   r   r   r   s
             r,   1test_wb1_occ_watch_conflict_returns_conflict_truerM  8  s   
 ,0CL$4F)GHDL
<
(C!!"5q!KLF>>"U">U"""">U""""""6"""6""">"""U"""""""??"d"?d""""?d""""""6"""6"""?"""d"""""""""""""""""""6"""6""""""""""""" 	JJ""$JJ!!#rK   c                    t               } g t        j                  ddfd
}d }d }d |       fd |       fg}t        d|	      5  t	        | j                  |            }d
d
d
       t              }d}||k(  }|st        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   d   }d}||u }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d
x}x}}t              }d}||k(  }|st        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 |D              }t        d |D              }|j                   }d}||u }|st        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%i}||k(  }|st        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}||u }|st        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(}|j$                  }||v }|st        j                  d)|fd*||f      t        j                  |      d't        j                         v st        j                  |      rt        j                  |      nd't        j                  |      d+z  }d"d#|iz  }t        t        j                  |            d
x}x}}y
# 1 sw Y   xY w),u,  
    WB2: Verify asyncio.gather(return_exceptions=True) is used by BulkheadGuard.

    We patch asyncio.gather and confirm it is called with return_exceptions=True.
    One coroutine raises, one succeeds — both should produce BulkheadResult
    without the exception propagating to the caller.
    F)return_exceptionsc                j   K   j                  t        |      | d        |d| i d {   S 7 w)N)
coro_countrO  rO  )ru   r   )rO  corosgather_callsoriginal_gathers     r,   mock_gatherzDtest_wb2_bulkhead_uses_gather_return_exceptions.<locals>.mock_gather`  s6     3u:L]^_$eQ?PQQQQr   c                    K   ddiS w)Nstatusr    rX  rK   r,   
succeedingzCtest_wb2_bulkhead_uses_gather_return_exceptions.<locals>.succeedinge  s     $s   c                     K   t        d      w)Nsimulated crashRuntimeErrorrX  rK   r,   crashingzAtest_wb2_bulkhead_uses_gather_return_exceptions.<locals>.crashingh       ,--   agent-okagent-crashz&core.coherence.bulkhead.asyncio.gatherrO   Nrd   r   r   r   rS  r   r   r   r   rO  Tr   )z%(py1)s is %(py4)sr-  r.  r   r   ry   c              3  @   K   | ]  }|j                   d k(  s|  yw)ra  Nrr   .0r{   s     r,   	<genexpr>zBtest_wb2_bulkhead_uses_gather_return_exceptions.<locals>.<genexpr>z  s     D11::+CQD   c              3  @   K   | ]  }|j                   d k(  s|  yw)rb  Nrd  re  s     r,   rg  zBtest_wb2_bulkhead_uses_gather_return_exceptions.<locals>.<genexpr>{  s     JaajjM.IJrh  r   	ok_resultr   r   r   rW  r   )z.%(py2)s
{%(py2)s = %(py0)s.result
} == %(py5)scrash_resultr[  r   )z-%(py1)s in %(py5)s
{%(py5)s = %(py3)s.error
}r   )r   r&   gatherr
   r-   rv   r   r   r   r   r   r   r   r   r   nextrg   rt   rs   )r|   rU  rY  r^  r   ry   r   r  r   r  r   r   r   r   rj  rk  r   r   r   rS  rT  s                      @@r,   /test_wb2_bulkhead_uses_gather_return_exceptionsrn  S  s    OE!LnnO49 R
 . 
Z\"	
#E
 
7[	Q 6e--e456 |!!!!!!!!!!!!3!!!3!!!!!!|!!!|!!!!!!!!!!!!!?./747/47777/4777/77747777777 w<1<1<133ww<1DDDIJ7JJL$$$$$$$$$$$$9$$$9$$$$$$$$$$$$$/$//////////////9///9//////////////(5(5((((5((((((<(((<((((((5(((((((2 2 22 22222 2222222222222222 22222222#6 6s   YY c                    t               } d }d }d |d      fd |       fd |d      fg}t        | j                  |            }t        |      }d}||k(  }|st	        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}}g }
|d   }|j                  }d}||u }|}|r|d   j                  d   }d}||k(  }|}|st	        j
                  d|fd||f      t	        j                  |      t	        j                  |      t	        j                  |      dz  }	dd|	iz  }|
j                  |       |r_t	        j
                  d	fdf      t	        j                  |      t	        j                  |      dz  }dd|iz  }|
j                  |       t	        j                  |
d      i z  }dd|iz  }t        t	        j                  |            dx}x}
x}x}x}x}x}x}}g }
|d   }|j                  }d}||u }|}|rd }|d   }|j                   }||v }|}|s&t	        j
                  d|fd||f      t	        j                  |      t	        j                  |      t	        j                  |      dz  }	dd|	iz  }|
j                  |       |rst	        j
                  d!|fd"|f      t	        j                  |      t	        j                  |      t	        j                  |      d#z  }d$d%|iz  }|
j                  |       t	        j                  |
d      i z  }d&d'|iz  }t        t	        j                  |            dx}x}
x}x}x}x}x}x}x}}g }
|d   }|j                  }d}||u }|}|r|d   j                  d   }d}||k(  }|}|st	        j
                  d|fd||f      t	        j                  |      t	        j                  |      t	        j                  |      dz  }	dd|	iz  }|
j                  |       |r_t	        j
                  d	|fd||f      t	        j                  |      t	        j                  |      dz  }dd|iz  }|
j                  |       t	        j                  |
d      i z  }dd|iz  }t        t	        j                  |            dx}x}
x}x}x}x}x}x}}y)(u   
    WB2 extended: 3 workers, 1 crashes — other 2 succeed and are unaffected.
    Confirms isolation at asyncio.gather level.
    c                   K   | ddS w)Nrp   )workerrW  rX  )idxs    r,   	ok_workerzItest_wb2_bulkhead_isolates_single_crash_from_3_workers.<locals>.ok_worker  s     00   c                     K   t        d      w)Nzworker exploded)
ValueErrorrX  rK   r,   
bad_workerzJtest_wb2_bulkhead_isolates_single_crash_from_3_workers.<locals>.bad_worker  s     *++r`  zworker-0r   worker-1zworker-2r   r   r   r   r   ry   r   r   r   NTrq  r   )z/%(py5)s
{%(py5)s = %(py3)s.success
} is %(py8)s)r   r   r   z%(py10)sr   )z%(py13)s == %(py16)s)r   py16z%(py18)sr   zassert %(py21)spy21rd   Fexplodedr   )z1%(py13)s in %(py18)s
{%(py18)s = %(py16)s.error
})r   ry  r   r   r   r   r   )r   r-   rv   r   r   r   r   r   r   r   r   r   rg   rt   ru   r   rs   )r|   rs  rw  r   ry   r   r  r   r  r   r   r   r  r   r   rH  r   @py_format11rJ  r   @py_format20r   r   r   r   s                            r,   6test_wb2_bulkhead_isolates_single_crash_from_3_workersr~    sy   
 OE1, 
Yq\"	Z\"	Yq\"E %))%01Gw<1<1<133ww<1J71:J:JJ%J'!**;*;H*EJJ*E*JJJJJJJJ:JJJJJJJJJJJJJ*EJJJ*EJJJJJJJJJJJJJJJJJJI71:I:II&I:II9I9II:9I+IIIIIIII:IIIIIIIIIIIII:9IIII:IIIIII9IIIIIIIIIIIIIIIIJ71:J:JJ%J'!**;*;H*EJJ*E*JJJJJJJJ:JJJJJJJJJJJJJ*EJJJ*EJJJJJJJJJJJJJJJJJJrK   c                    d} t        d| d      }i fd}fd}t               }||_        t               |_        ||_        t               |_        t        |      }t        |j                  |             t        |j                  |             }t        |      }d}||k(  }	|	st        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   d   }d}||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   }|| k(  }|st        j                  d|fd|| f      t        j"                  |      dt        j                         v st        j                   |       rt        j"                  |       nddz  }dd|iz  }t%        t        j&                  |            dx}}|d   d   }d}||k(  }|slt        j                  d|fd||f      t        j"                  |      t        j"                  |      dz  }dd|iz  }
t%        t        j&                  |
            dx}x}}y)u   
    INT1: StateDelta.apply_to() + StagingArea.submit_delta() + collect_all()
    form the PROPOSE → BARRIER chain.

    Verifies the data fidelity from StateDelta construction through staging.
    z	sess-int1agent-Ar   )r0   c                   K   ||<   y wr   rX  )keyfieldrc   
hset_callss      r,   	mock_hsetzKtest_int1_state_delta_submit_and_collect_through_staging.<locals>.mock_hset  s     !
5s   
c                `   K   j                         D ci c]  \  }}||
 c}}S c c}}w wr   )items)r  r  valr  s      r,   mock_hgetallzNtest_int1_state_delta_submit_and_collect_through_staging.<locals>.mock_hgetall  s,     -7-=-=-?@zucs
@@@s   .(
.rd   r   r   r   	collectedr   r   r   Nr   rr   r,  r-  r.  r   r   r   r   r   r   r   )r   r   hsetr   expirehgetalldeleter   r-   submit_deltacollect_allr   r   r   r   r   r   r   r   r   )r   deltar  r  rI   r[   r  r   r  r   r  r   r   r   r   r  r   r  s                    @r,   8test_int1_state_delta_submit_and_collect_through_stagingr    s%    J	:q9E J"A KEEJ;EL EM;EL% GU#$ G''
34Iy>Q>Q>Q33yy>QQ<
#0y0#y0000#y000#000y0000000Q<%3%3333%333%3333333333333333Q<)*/a/*a////*a///*///a///////rK   c            
     b   dg d} t        ddddddd	d
ddd	g      }|j                  |       }|d   }d}||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}||k(  }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}|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}||k(  }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}y)zt
    INT1b: StateDelta.apply_to() correctly transforms state dict.
    Verifies the PROPOSE step output format.
    r   )r   r  agent-Bzsess-xrd   replacez/countr   r`   r^   z/new_keyhello)r0   r   r   r   r,  r-  r.  r   Nnew_keyr  )r   apply_tor   r   r   r   r   )stater  	new_stater   r   r   r   r  s           r,   6test_int1b_state_delta_apply_to_produces_correct_stater    s   
 "%E8Qh;*w?
E u%IW"""""""""""""""""""Y*7*7****7******7*******W###################>Q>Q>Q>QrK   c                    t        d      \  } }t        |       }ddddg}t        |j                  dd|            }t	        |t
              }|sd	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dt        j                         v st        j                  t
              rt        j                  t
              ndt        j                  |      dz  }t        t        j                  |            d}|j                  }d}||u }|st        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(  }|st        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}||u }|st        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$                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}}|j&                  }|j(                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}}|j*                  }|j$                  }d}||k(  }|st        j                  d|fd ||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}}y)!u   
    INT2: RedisMasterState.commit_patch succeeds on a fresh session (version=0).
    Verifies WATCH → GET → MULTI → SET → EXEC call sequence.
    r   r6  r^   z/taskr7  r`   z	sess-int2r8  r   r   rt   r   r   NTr   r   r   r   r   rd   r   r9  Fr:  )zN%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.watch
}.await_count
} == %(py7)srH   r  r  r  )zM%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.multi
}.call_count
} == %(py7)s)zP%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.execute
}.await_count
} == %(py7)s)rJ   r   r-   r
  r   r   r   r   r   r   r   r   r   rg   r   r  r  r<   await_countr?   r  rA   )rC  rH   rD  r   rt   r   r   r   r   r   r   r  r  r  s                 r,   +test_int2_redis_master_state_commit_successr    s   
 1CL$
<
(CwCDI!!+q	!JKFfl++++++++:+++:++++++f+++f++++++l+++l++++++++++>>!T!>T!!!!>T!!!!!!6!!!6!!!>!!!T!!!!!!!""""""""""""6"""6"""""""""""""??#e#?e####?e######6###6###?###e####### ::&:!!&Q&!Q&&&&!Q&&&&&&4&&&4&&&:&&&!&&&Q&&&&&&&::%:  %A% A%%%% A%%%%%%4%%%4%%%:%%% %%%A%%%%%%%<<(<##(q(#q((((#q((((((4(((4(((<(((#(((q(((((((rK   c                 "   t               } t        d      | _        t        |       }t	        |j                  d            \  }}d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}}i }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}}y)zS
    INT2b: RedisMasterState.get_snapshot returns (0, {}) for a fresh session.
    Nr7   z
sess-freshr   r   z%(py0)s == %(py3)sr0   r   r   r   r   r1   )r   r   r=   r   r-   r	  r   r   r   r   r   r   r   r   )rC  rD  r0   r1   r   r   r  r   s           r,   0test_int2b_redis_master_state_get_snapshot_emptyr    s     ;L d3L
<
(C((67MGT7a<7a77a42:42442rK   c                 h   ddg} t               }t        |       |_        t        |      }dddiddd	d
i ddg}t	        |j                  d|            }|| k(  }|st        j                  d|fd|| f      dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  |       rt        j                  |       nddz  }dd|iz  }t        t        j                  |            d}|j                  }|j                  }d}	||	k(  }
|
st        j                  d|
fd||	f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |	      dz  }dd|iz  }t        t        j                  |            dx}x}x}
}	|j                  j                  d   }|d   \  }}|t         k(  }|st        j                  d|fd|t         f      dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  t               rt        j                  t               nddz  }dd|iz  }t        t        j                  |            d}|d   }d}||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}||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}||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}||k(  }|slt        j                  d|fd ||f      t        j                  |      t        j                  |      d!z  }d"d#|iz  }t        t        j                  |            dx}x}}d'}||v }|st        j                  d(|fd)||f      t        j                  |      d*t        j                         v st        j                  |      rt        j                  |      nd*d+z  }d,d-|iz  }t        t        j                  |            dx}}|d.   }d/}||k(  }|slt        j                  d|fd ||f      t        j                  |      t        j                  |      d!z  }d"d#|iz  }t        t        j                  |            dx}x}}y)0z
    INT3: TaskDAGPusher.push_dag calls redis.xadd with all required stream fields.
    Verifies the MAP step output format.
    z1000-0z1000-1rO   researchqtestT2high)r   r   r   priority
synthesizer   r   z	sess-int3r   )z%(py0)s == %(py2)s
result_idsentry_ids_returned)r   r   zassert %(py4)sr   Nr   )zM%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.xadd
}.await_count
} == %(py7)srI   r  r  r  r   stream_key_argr   r   r,  r-  r.  r   r   r   r  task_idr   z%(py1)s in %(py3)s
fields_argr   r   r   r   z{"q": "test"})r   r   xaddr   r-   rT   r   r   r   r   r   r   r   r   r  await_args_listr   )r  rI   rV   r   r  r   @py_format3r   r   r  r  r   r  
first_callr  r  r   r   r  r  r   s                        r,   1test_int3_task_dag_pusher_produces_correct_fieldsr    s   
 #H-KE'9:EJ5!F c6]DV\]"r4@E
 V__[%89J+++++:+++++++:+++:++++++++++++++++++::&:!!&Q&!Q&&&&!Q&&&&&&5&&&5&&&:&&&!&&&Q&&&&&&& ++A.J!+ANJZ''''>Z''''''>'''>''''''Z'''Z'''''''l#2{2#{2222#{222#222{2222222k"0j0"j0000"j000"000j0000000f%%%%%%%%%%%%%%%%%%%j!+V+!V++++!V+++!+++V+++++++"9
""""9
"""9""""""
"""
"""""""i 3O3 O3333 O333 333O3333333rK   c            	        t               } d }d }d }d |       fd |       fd |       fg}t        | j                  |            }t        |      }d}||k(  }|st	        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 ci c]  }|j                  | }}|d   }|j                  }d}||u }|st	        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dd}||k(  }|st	        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}||u }|st	        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}|d   }|j                  }||v }|st	        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}||u }|st	        j
                  d|fd||f      t	        j                  |      t	        j                  |      t	        j                  |      dz  }	dd|	iz  }
t        t	        j                  |
            dx}x}x}}| j!                  |      }d}d}||z  }||z
  }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                  |      t	        j                  |      t	        j                  |      d#z  }d$d%|iz  }t        t	        j                  |            dx}x}x}x}x}x}}yc c}w )&u   
    INT4: Real BulkheadGuard with actual coroutines — one raises, two succeed.
    Verifies the full isolation contract at runtime.
    c                    K   dddS w)NAr   agentrt   rX  rX  rK   r,   worker_az>test_int4_bulkhead_real_coroutines_one_fails.<locals>.worker_a9       --rt  c                     K   t        d      w)Nagent B crashedr\  rX  rK   r,   worker_bz>test_int4_bulkhead_real_coroutines_one_fails.<locals>.worker_b<  r_  r`  c                    K   dddS w)NCr   r  rX  rX  rK   r,   worker_cz>test_int4_bulkhead_real_coroutines_one_fails.<locals>.worker_c?  r  rt  r  r  zagent-Cr   r   r   r   ry   r   r   r   NTr   )z/%(py3)s
{%(py3)s = %(py1)s.success
} is %(py6)s)r   r   r   r  r   r  )z.%(py3)s
{%(py3)s = %(py1)s.result
} == %(py6)sFr  r   )z-%(py1)s in %(py6)s
{%(py6)s = %(py4)s.error
})r   r   r   r   g&.><)zH%(py9)s
{%(py9)s = %(py0)s((%(py1)s - (%(py3)s / %(py5)s)))
} < %(py12)sabsrate)r   r   r   r   r  r>  zassert %(py14)sr?  )r   r-   rv   r   r   r   r   r   r   r   r   r   rr   rg   rt   rs   get_success_rater  )r|   r  r  r  r   ry   r   r  r   r  r   r{   by_idr   r   r  r  r   rE  rF  @py_assert10@py_format13@py_format15s                          r,   ,test_int4_bulkhead_real_coroutines_one_failsr  2  s   
 OE... 
HJ	HJ	HJE %))%01Gw<1<1<133ww<1$+,qQZZ],E,+##+t+#t++++#t++++++#+++t+++++++D""Dt&DD"&DDDDD"&DDDDDDD"DDD&DDDDDDDD,##,u,#u,,,,#u,,,,,,#,,,u,,,,,,,6i 06 0 6 66 66666 6666666 0666 66666666+##+t+#t++++#t++++++#+++t+++++++ !!'*D#!#a!e#te|#3|#t#t####t######3###3######t###t###a###!######t######## -s   W<c                   
  G d dt               } d}ddd}|j                         |fd

fd}t               |_        t	               _        t	        d	g g f
      _         |       d_        fd}t         |              j
                  j                          j
                  j                  d   }|d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      dt        j                          v st        j"                  t              rt        j                  t              nddz  }dd|iz  }	t%        t        j&                  |	            dx}}y)zw
    INT5: A SwarmWorkerBase subclass correctly processes a task and calls XACK.
    Verifies the CLAIM step mock.
    c                      e Zd ZddZy)Ctest_int5_swarm_worker_base_process_and_ack.<locals>.ConcreteWorkerc                2   K   d|j                  dd      iS w)N	processedr   unknown)r=   )selftasks     r,   processzKtest_int5_swarm_worker_base_process_and_ack.<locals>.ConcreteWorker.processf  s     +y!ABBs   N)r  dict)__name__
__module____qualname__r  rX  rK   r,   ConcreteWorkerr  e  s    	CrK   r  z1234567890-0s   researchs   {})s	   task_types   payloadr   c                0   K   dz  dk(  rdgfgS g S w)Nrd   s   genesis:swarm:tasksrX  )groupconsumerstreamsr   blockr  messages        r,   mock_xreadgroupzDtest_int5_swarm_worker_base_process_and_ack.<locals>.mock_xreadgroupp  s,     a
?+gY788	s   z0-0r7   Tc            
     J  K   
j                  dd       d {    	j                  ddt        didd       d {   } | r| d   \  }}|D ]  \  }}|j                         D ci c]H  \  }}t	        |t
              r|j                         n|t	        |t
              r|j                         n|J }}}
j                  |       d {   }|	j                  t        dt	        |t
              r|j                         n|       d {     y7 7 c c}}w 7 S7 w)	Ngenesis_workersrx  >rd   i  )r   r  r   rp   )	_reclaim_pending
xreadgroupr   r  r   bytesdecoder  xack)entries_messageseidfkvr  rt   rI   rq  s            r,   run_one_iterationzFtest_int5_swarm_worker_base_process_and_ack.<locals>.run_one_iteration  s)    %%&7DDD((zJ+<AT ) 
 
 !!*KAx" 	uQ
 !"		 1 $.a#7QXXZQ&0E&:
B   &~~d33%**Z1BT^_bdiTjCJJLpsttt	u # 	E
 4tsV   D#D#D#D)D#'AD4D#DD#:D#D!D#D#D#!D#r   r   r   r   r   r   N)r   r;   r   r  r   r  
xautoclaim_runningr-   r   r@  r   r   r   r   r   r   r   r   r   )r  entry_idfieldsr  r  ack_argsr   r   r  r   r  r  rI   rq  s             @@@@r,   +test_int5_swarm_worker_base_process_and_ackr  _  s   C C
 H'U;F &)GJ KE&EEJ ub"o>EE"FFO(  
JJ""$zz$$Q'HA;$;*$$$$;*$$$;$$$$$$*$$$*$$$$$$$rK   c                ^   ddl mc m} | dz  }|j                  }||_        	 t	        d      }t        ddg      }t               }t        dd	
      }t        dg       }t        ||||      }	t        |	j                  d|            }
|
j                  }d}||u }|st        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*                  } |       }|st        j,                  d      dz   dt        j                          v st        j"                  |      rt        j$                  |      ndt        j$                  |      t        j$                  |      dz  }t'        t        j(                  |            dx}}|j/                         j1                         D cg c]#  }|j3                         s|j3                         % }}t5        |      }d}||k\  }|st        j                  d|fd||f      dt        j                          v st        j"                  t4              rt        j$                  t4              nddt        j                          v st        j"                  |      rt        j$                  |      ndt        j$                  |      t        j$                  |      dz  }t        j,                  d      dz   d |iz  }t'        t        j(                  |            dx}x}}|D cg c]  }t7        j8                  |       }}|D cg c]  }|d!   d"k(  s| }}t5        |      }d}||k\  }|st        j                  d|fd||f      dt        j                          v st        j"                  t4              rt        j$                  t4              ndd#t        j                          v st        j"                  |      rt        j$                  |      nd#t        j$                  |      t        j$                  |      dz  }t        j,                  d$|       dz   d |iz  }t'        t        j(                  |            dx}x}}|d   }|d%   }d}||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}||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}||k(  }|slt        j                  d&|fd'||f      t        j$                  |      t        j$                  |      d(z  }d)d*|iz  }t'        t        j(                  |            dx}x}}d-}||v }|st        j                  d.|fd/||f      t        j$                  |      d"t        j                          v st        j"                  |      rt        j$                  |      nd"d0z  }d1d2|iz  }t'        t        j(                  |            dx}}||_        yc c}w c c}w c c}w # ||_        w xY w)3z
    INT6: Run the full pipeline with all 8 steps. Redirect events.jsonl to
    tmp_path. Verify that a 'release' event is written with correct payload.
    r   Nzevents.jsonlr   zid-0zid-1)rU   T   r   r   r   z	sess-int6r   r   rt   r   r   r   zevents.jsonl was not createdzC
>assert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}fake_logr   r   r   rd   r'  r)  r   linesr   z"Expected at least one event in logr+  r   r"  releaserelease_eventszNo release event found: r   r   r,  r-  r.  r   r   r   	timestampr   r  r   r   r   )%core.coherence.coherence_orchestrator	coherencecoherence_orchestratorr$   r   rW   r\   rm   r}   r!   r-   rA   rg   r   r   r   r   r   r   r   r   existsr/  	read_text
splitlinesstripr   r9   loads)tmp_pathmodr  original_pathr   rV   r[   rl   r   r   rt   r   r   r   r   r   r   lr  r   r  r  r   parsedr0  r  r  r   r  s                                r,   *test_int6_full_pipeline_events_log_writtenr    s   
 87.(H''M"C#,a !VV,<=$&!$:!qD, 	
 \))+u=>~~%%~%%%%~%%%%%%v%%%v%%%~%%%%%%%%%% @ @ @@"@@@@@@@x@@@x@@@@@@ @@@@@@$,$6$6$8$C$C$ESqSS5zDQDzQDDDzQDDDDDDsDDDsDDDDDD5DDD5DDDzDDDQDDD DDDDDDDD *//A$**Q-//%+Lq)/K!LL>"LaL"a'LLL"aLLLLLLsLLLsLLLLLL>LLL>LLL"LLLaLLL+CF8)LLLLLLLL #|$33$3333$333$3333333333*+0q0+q0000+q000+000q0000000'(-A-(A----(A---(---A-------%{g%%%%{g%%%{%%%%%%g%%%g%%%%%%% , T 0L ,sD   H
\# -\\D-\# \\# $\2\6M\# \# #	\,c                 v   ddl m} m}m}m}m}m}m}m}m	}m
}	m}
m}m}m}m}m}m}m}m}m}m}m}m}m}m} d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}|	|k(  }|st5        j6                  d|fd|	|f      dt9        j:                         v st5        j<                  |	      rt5        j>                  |	      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}||k(  }|st5        j6                  d|fd||f      dt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd	|iz  }tA        t5        jB                  |            d
x}}d}tE        |      }||v }|st5        j6                  d|fd||f      t5        j>                  |      dt9        j:                         v st5        j<                  tD              rt5        j>                  tD              nddt9        j:                         v st5        j<                  |      rt5        j>                  |      ndt5        j>                  |      dz  }dd |iz  } tA        t5        jB                  |             d
x}x}}tG        |      }|sd!d"t9        j:                         v st5        j<                  tF              rt5        j>                  tF              nd"d#t9        j:                         v st5        j<                  |      rt5        j>                  |      nd#t5        j>                  |      d$z  }tA        t5        jB                  |            d
}tG        |      }|sd!d"t9        j:                         v st5        j<                  tF              rt5        j>                  tF              nd"d%t9        j:                         v st5        j<                  |      rt5        j>                  |      nd%t5        j>                  |      d$z  }tA        t5        jB                  |            d
}tG        |      }|sd!d"t9        j:                         v st5        j<                  tF              rt5        j>                  tF              nd"d&t9        j:                         v st5        j<                  |      rt5        j>                  |      nd&t5        j>                  |      d$z  }tA        t5        jB                  |            d
}tG        |      }|sd!d"t9        j:                         v st5        j<                  tF              rt5        j>                  tF              nd"d't9        j:                         v st5        j<                  |      rt5        j>                  |      nd't5        j>                  |      d$z  }tA        t5        jB                  |            d
}y
)(z
    PKG: All Module 6 exports are importable from core.coherence package.
    This is the regression guard for __init__.py coverage.
    r   r   zgenesis:swarm:tasksr   r  r   r  r   r   Nr  r   zgenesis:staging:r   iX  r   r   r   g      ?r   r   r    x   r#   genesisr   )z0%(py1)s in %(py6)s
{%(py6)s = %(py3)s(%(py4)s)
}r   r$   )r   r   r   r   r   r   z,assert %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}callabler!   )r   r   r   r"   r   r   )$core.coherencer   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   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  r   s!                                    r,   3test_pkg_all_exports_importable_from_core_coherencer    s9   
      : /.:.....:.......:...:...........--=-----=-------=---=-----------!33!33333!3333333333333!33333333"%%#%%%%#%%%%%%%%%%%%#%%%%%%%##>V####>V######>###>###V#######!$$$$$$$$$$$$$$$$$$$$$$$$$;!;!;;!,//(C////(C//////(///(///C///////,O,,9,,,,,9,,,,9,,,,,,,,,,,,,,,O,,,O,,,,,,,,,,, )********8***8******)***)**********O$$$$$$$$8$$$8$$$$$$O$$$O$$$$$$$$$$O$$$$$$$$8$$$8$$$$$$O$$$O$$$$$$$$$$M""""""""8"""8""""""M"""M""""""""""rK   c                 t   h d} t         | k(  }|st        j                  d|fdt         | f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       dz  }dd|iz  }t        t        j                  |            dx}} y)	z2VALID_OPS contains all 6 RFC 6902 operation names.>   r^   copymover  remover  r   r  r   r  r   r   N)	r   r   r   r   r   r   r   r   r   )r   r   r  r   s       r,   test_pkg_valid_ops_setr  
  s^    LL9LLLLL9LLLLLLL9LLL9LLLLLLLLLLLrK   c                 	   d} d| dg}t               }t        |      |_        t               }d|_        g |_        t               }t        |      |_        t               }d|_        d|_        d|_        t               }t        di f      |_        t        |      |_	        t        |||	      }t        |j                  | d
            }|j                  }	d}
|	|
u }|st        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(  }|st        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,                  }	|	t.        k(  }|st        j                  d|fd|	t.        f      dt        j                          v st        j"                  |      rt        j$                  |      ndt        j$                  |	      dt        j                          v st        j"                  t.              rt        j$                  t.              nddz  }dd|iz  }t'        t        j(                  |            dx}	}|j                  }	|	j0                  }|t.        k(  }|st        j                  d|fd|t.        f      dt        j                          v st        j"                  |      rt        j$                  |      ndt        j$                  |	      t        j$                  |      dt        j                          v st        j"                  t.              rt        j$                  t.              nddz  }dd|iz  }t'        t        j(                  |            dx}	x}}y) u   
    OCC exhaustion: all MAX_RETRIES attempts conflict → conflict_exhausted.
    commit_patch called exactly MAX_RETRIES times.
    zsess-exhaustedr   r   r7   TFr   r   r   rd   r   r   r   rt   r   r   r   Nrf   r   r  )z/%(py2)s
{%(py2)s = %(py0)s.retries
} == %(py4)sr    r  r.  r   )zT%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.commit_patch
}.call_count
} == %(py6)sr  r   r   r   r   r   r   )r   r   rY   rg   rh   r  r  r  r	  r
  r   r-   rj   r   r   r   r   r   r   r   r   ri   r  r    r  )r   rZ   r[   r  r  r  r  rl   rt   r   r   r   r   r   r   r  r  r   s                     r,   #test_unit_occ_all_retries_exhaustedr    s   
 "Jz:;FkG$&9G;LL "L[F,7FLkO#O#O"#O[F#!R9F#AF'VZ`aF&&zA&FGF>>"U">U"""">U""""""6"""6""">"""U"""""""5!55!55555!555555565556555555!55555555>>(>[((((>[((((((6(((6(((>(((((([((([(((((((8))8)[8888)[88888868886888888)888888[888[8888888rK   c                 J   t               } t               | _        t        |       }d }d }d |       fd |       fd |       fg}t	        |j                  |            }| j                  }|j                  }d}||k(  }	|	st        j                  d|	fd	||f      d
t        j                         v st        j                  |       rt        j                  |       nd
t        j                  |      t        j                  |      t        j                  |      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}	}| j                  j                  }|d   d   }d}||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   }|d   }d}||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}||k(  }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}|d   }|t         k  }|st        j                  d|fd|t         f      t        j                  |      dt        j                         v st        j                  t               rt        j                  t               nddz  }dd |iz  }t        t        j                  |            dx}}y)!z
    When success rate < CRITICAL_THRESHOLD (0.5), cold_ledger.write_event
    is called with event_type='swarm_critical_failure'.
    )cold_ledgerc                    K   i S wr   rX  rX  rK   r,   r   zAtest_unit_bulkhead_critical_threshold_triggers_ledger.<locals>.okE  s     	s   c                     K   t        d      w)Ncrashr\  rX  rK   r,   failzCtest_unit_bulkhead_critical_threshold_triggers_ledger.<locals>.failH  s     7##r`  abcrd   r   )zT%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.write_event
}.await_count
} == %(py7)sr  r  r  r  Nr"  swarm_critical_failurer,  r-  r.  r   r   total_tasksr   failed_countr   success_rater  )z%(py1)s < %(py3)sr   r   r   r   )r   r   write_eventr   r-   rv   r  r   r   r   r   r   r   r   r   r@  r   )r  r|   r   r!  r   ry   r   r   r  r  r   r  	call_argsr   r   r   r  r   r  r   s                       r,   5test_unit_bulkhead_critical_threshold_triggers_ledgerr+  ;  sj   
 +K'kKk2E$ 24[3-#tv7E%))%01G""3"..3!3.!3333.!333333;333;333"333.333!3333333''22IQ<%A)AA%)AAAAA%)AAAA%AAA)AAAAAAAAl9%G=!&Q&!Q&&&&!Q&&&!&&&Q&&&&&&&>"'a'"a''''"a'''"'''a'''''''>"7"%77777"%7777"777777%7777%77777777rK   c                    ddddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}dddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}ddddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}ddddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}ddddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}ddddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}dddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}dddg} t        |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |       t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx} x}x}}y)zGvalidate_patch accepts all 6 RFC 6902 ops with correct required fields.r^   r  rd   r`   Tr   )z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} is %(py7)sr   r  r  r  Nr  )ra   rb   r  r   r  z/bz/a)ra   rb   fromr  r  invalidF)ra   rc   )	r   r   r   r   r   r   r   r   r   )r   r   r  r  r   r  s         r,    test_unit_validate_patch_all_opsr/  ]  sf   "'BCL>CDLLDLLLLDLLLLLL>LLL>LLLCLLLDLLLLLLLLLL"*D9:C>:;CtC;tCCCC;tCCCCCC>CCC>CCC:CCC;CCCtCCCCCCC"+TAFGP>GHPDPHDPPPPHDPPPPPP>PPP>PPPGPPPHPPPDPPPPPPP"($EFO>FGO4OG4OOOOG4OOOOOO>OOO>OOOFOOOGOOO4OOOOOOO"($EFO>FGO4OG4OOOOG4OOOOOO>OOO>OOOFOOOGOOO4OOOOOOO"($CDM>DEMMEMMMMEMMMMMM>MMM>MMMDMMMEMMMMMMMMMM #,T:;E>;<EE<EEEE<EEEEEE>EEE>EEE;EEE<EEEEEEEEEE"'!45?>56?%?6%????6%??????>???>???5???6???%???????rK   c            	        t               } t        t              5  t        | j	                  dt        d                  }t        | j	                  dt        d                  }ddd       j                  }j                  }||k7  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}}y# 1 sw Y   xY w)z@Each call to orchestrator.execute() produces a distinct saga_id.zsess-uidrd   N)!=)zL%(py2)s
{%(py2)s = %(py0)s.saga_id
} != %(py6)s
{%(py6)s = %(py4)s.saga_id
}r1r2r  r   r   )r!   r
   r   r-   rA   r   r   r   r   r   r   r   r   r   r   )r   r2  r3  r   r  r   r  r   s           r,   $test_unit_saga_id_unique_per_executer4  q  s    (*L	 	! E%%j-2BCD%%j-2BCDE ::##:####:######2###2###:###################E Es   A	E??F	c                    t        ddg iddd      } | j                  }d}||u }|st        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g i}||k(  }|st        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(  }|st        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(  }|st        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(  }|st        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}}y)z;CoherenceResult has all required fields with correct types.Trh   zabc-123r   r   )rg   r   r   r   r   r   r   r{   r   r   r   Nr   )z7%(py2)s
{%(py2)s = %(py0)s.committed_state
} == %(py5)s)z/%(py2)s
{%(py2)s = %(py0)s.saga_id
} == %(py5)sr   r   )r"   rg   r   r   r   r   r   r   r   r   r   r   r   r   )r{   r   r   r   r   r   s         r,   !test_unit_coherence_result_fieldsr6    sZ   ',	A 99991194 44 44444 444444414441444444 4444444499!	!9	!!!!9	!!!!!!1!!!1!!!9!!!	!!!!!!!#!#!####!######1###1######!####### q q    q      1   1      q       rK   __main__z(BB1: Full pipeline 3 workers all succeedz5BB2: Version conflict retry succeeds via orchestratorz.BB2 direct: OCC retry via real OCCCommitEngineu"   BB3: Worker crash → scar writtenz&BB3 all crash: scar has correct countsz"WB1: WATCH/MULTI/EXEC pattern usedz/WB1 conflict: WatchError triggers conflict=Truez0WB2: asyncio.gather(return_exceptions=True) usedz'WB2: bulkhead isolates 1 crash out of 3z1INT1: StateDelta submit + collect through stagingz1INT1b: StateDelta.apply_to produces correct statez+INT2: RedisMasterState commit_patch successz2INT2b: RedisMasterState get_snapshot empty sessionz2INT3: TaskDAGPusher produces correct stream fieldsz-INT4: BulkheadGuard real coroutines one failsz&INT5: SwarmWorkerBase process and XACKz/PKG: All exports importable from core.coherencezPKG: VALID_OPS set completezUNIT: OCC all retries exhaustedz6UNIT: BulkheadGuard critical threshold triggers ledgerz%UNIT: validate_patch all RFC 6902 opsz UNIT: saga_id unique per executezUNIT: CoherenceResult fieldsc                 ~    t        j                         5 } t        t        |              d d d        y # 1 sw Y   y xY wr   )tempfileTemporaryDirectoryr  r   )tds    r,   run_int6r<    s5    ((* 	Ab6tBx@	A 	A 	As   3<z2INT6: Full pipeline events log written to tmp_pathz	  [PASS] z	  [FAIL] z: 
r   z tests passedu1   ALL TESTS PASSED — Story 6.09 Integration Suite)r   N)rE   intrF   r  )r   N)r0   r>  rF   r  r   )Tr   N)rg   boolr0   r>  ri   r   )r   N)rw   r>  )r   )r   r>  )rd   N)rr   r   r   r   r0   r>  )d__doc__
__future__r   builtinsr   _pytest.assertion.rewrite	assertionrewriter   r&   r9   sysr9  r   r   pathlibr   unittest.mockr   r   r	   r
   pytestrb   insertr  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   rJ   rR   rW   r\   rm   r}   r   r   r   r  r  r1  r4  rK  rM  rn  r~  r  r  r  r  r  r  r  r  r  r  r  r+  r/  r4  r6  r  	tracebacktestsr<  ru   passedr   totalnamefnprint	Exceptionexc	print_excexitrX  rK   r,   <module>rV     s  %N #     
  '  ; ;  * +      B O %PH 
$,5hQ/h.-l))X&J#L$6.3bK@!0H6),&4L%$Z:%D.,l0#fM9N8D@($!( z 
45ab	@Bjk	9;mn	-/QR	13ef	-/YZ	:<mn	;=lm	24jk	<>vw	<>tu	68cd	=?op	=?pq	8:fg	13^_	:<op	&(>?	*,OP	ACxy	02RS	+-QR	')JK/E6 A 
LLFQRFJE "b	"DIdV$%aKF	" 
Bvhawm
,-ABk Z  	"IdV2cU+,I!!	"s   GG>G99G>