
    %iF                    X   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 ddlmZ ddlmZmZmZ ddlZdZeej.                  vrej.                  j1                  de       ddlmZmZmZmZmZ dd	lm Z m!Z!m"Z" dd
l#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7m8Z8 dRdSdZ9dTdZ:	 	 dU	 	 	 	 	 dVdZ;	 	 dW	 	 	 	 	 dXdZ<dYdZdZ=d Z>d Z?d Z@d ZAd ZBd ZCd ZDd ZEd ZFd ZGd ZHd  ZId! ZJd" ZKd# ZLd$ ZMd% ZNd& ZOd' ZPd( ZQeRd)k(  rDddlSZSddlTZTd*e>fd+e?fd,e@fd-eAfd.eBfd/eCfd0eDfd1eEfd2eFfd3eGfd4eHfd5eIfd6eJfd7eKfd8eLfd9eMfd:eNfd;eOfd<ePfd=eQfgZUdZVdZW eSj                         5 ZYeUD ]l  \  ZZZ[ eeY      eZj                  d>d?      j                  d@dA      j                  dBd?      z  Z]e]j                  dCdCD       	  e[e]        e_dEeZ        eVdFz  ZVn 	 ddd        e_dIdJ         e_dK        e_dL eceU               e_dMeV         e_dNeW         e_dOeWdk(  rdPndQ         e_dJ        eWdkD  r ej                  dF       yyy# e`$ r/Za e_dGeZ dHea         eTj                          eWdFz  ZWY dZa[adZa[aww xY w# 1 sw Y   xY w)[up  
tests/evolution/test_dual_loop.py

Story 8.09: Integration Test Suite — Module 8 Dual-Loop
=========================================================

Validates the entire Module 8 (Evolution Dual-Loop) pipeline.
All 8 components must work together correctly.

Module 8 components:
  8.01  ImmutableKernel     — file permission locker
  8.02  AxiomaticTests      — compiled GLOBAL_GENESIS_RULES assertions
  8.03  MetaArchitect       — scar-driven structural analysis
  8.04  ShadowArena         — containerized test sandbox
  8.05  ScarAggregator      — L3 failure pattern collector
  8.06  CodeProposer        — structural fix generator
  8.07  GitOpsPRCreator     — GitHub PR automation
  8.08  Tier1AutonomousUpdater — epistemic self-updates

Integration test scenarios (IT1–IT10) + black box + white box cases.
Total: 20 test cases (exceeds the required 16).

ALL external services are mocked — no real Qdrant, Postgres, Redis,
GitHub, or git calls are made. File I/O uses pytest tmp_path only.

VERIFICATION_STAMP
Story: 8.09
Verified By: parallel-builder
Verified At: 2026-02-25
Tests: 20/20
Coverage: 100%
    )annotationsN)Path)Any)	MagicMockcallpatchz/mnt/e/genesis-system)ImmutableKernel
LockResultVerifyResultKERNEL_FILESKERNEL_DIRS)AxiomaticTestsAxiomResultAxiomViolation)MetaArchitectArchitectureAnalysis
BottleneckFixProposal)ShadowArenaArenaResultSHADOW_PREFIX)ScarAggregator
ScarReportScarCluster)CodeProposerCodeProposal)GitOpsPRCreatorPRResult)Tier1AutonomousUpdaterTier1Resultc                    i }t         D ]:  }| |z  }|j                  j                  dd       |j                  |       |||<   < |S )z=Create all KERNEL_FILES inside tmp_path with default content.Tparentsexist_ok)r   parentmkdirwrite_bytes)tmp_pathcontentcreatedrelfulls        7/mnt/e/genesis-system/tests/evolution/test_dual_loop.py_make_kernel_filesr.   i   sT    !G #~$6!	
 N    c                ,    t        t        |             S )z.Return an ImmutableKernel pointed at tmp_path.)	base_path)r	   str)r(   s    r-   _make_kernelr3   t   s    S]33r/   c                B    | d|dddgid}t        j                  |      S )zEReturn a JSON string with a valid (axiom-clean) CodeProposal payload.zfrom core.interceptors.base_interceptor import BaseInterceptor

class FixInterceptor(BaseInterceptor):
    def intercept(self, ctx):
        return ctx
zdef test_placeholder(): pass
scar_idssc_001	file_pathcode_contenttest_file_pathtest_contentconfig_changes)jsondumps)r8   r:   payloads      r-   _valid_proposal_jsonr@   y   s6     #
 )8%z2G ::gr/   c                &    t        | |ddgdg      S )Nsaga_001saga_002r6   description	frequencyaffected_saga_idsr5   )r   )descrF   s     r-   _make_bottleneckrI      s$     %z2	 r/   c           
         t        |t        | dz        t        | dz        t        | dz        t        | dz              S )z>Factory: Tier1AutonomousUpdater with all paths under tmp_path.kg_entitiespromptszGLOBAL_GENESIS_RULES.mdtier1_updates.jsonl)qdrant_clientkg_base_pathprompts_dir
rules_fileaudit_log_path)r   r2   )r(   rN   s     r-   _make_updaterrS      sH    !#M129,-x";;<8&;;< r/   c           
        t        |        t        |       }|j                         }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                  }t        |      }t        t              }||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	t        j                         v st        j                  t              rt        j                  t              nd	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}x}x}}|j                   }g }||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}}| t        d   z  }t#        j$                  t&        t(        f      5  |j+                  dd       ddd       |j-                         }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}|j0                  }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}}|j2                  }g }||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)u   
    IT1 (BB): Lock kernel files in tmp_path.
    Writing to a locked file must raise PermissionError (or OSError).
    Verify kernel integrity → intact=True.
    5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}
isinstancelock_resultr
   py0py1py2py4N==)zl%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.locked
})
} == %(py10)s
{%(py10)s = %(py7)s(%(py8)s)
}lenr   )rY   rZ   py3py5py7py8py10zassert %(py12)spy12)z.%(py2)s
{%(py2)s = %(py0)s.failed
} == %(py5)srY   r[   ra   assert %(py7)srb   r   ztamper!utf-8encodingverifyr   Tis)z.%(py2)s
{%(py2)s = %(py0)s.intact
} is %(py5)s)z6%(py2)s
{%(py2)s = %(py0)s.modified_files
} == %(py5)s)r.   r3   lock_kernelrV   r
   @py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationlockedr_   r   _call_reprcomparefailedpytestraisesPermissionErrorOSError
write_textverify_kernelr   intactmodified_files)r(   kernelrW   @py_assert3@py_format5@py_assert2@py_assert4@py_assert9@py_assert6@py_format11@py_format13@py_assert1@py_format6@py_format8
first_filerk   s                   r-   0test_it1_immutable_kernel_lock_then_write_raisesr      s    x (#F$$&Kk:........:...:......k...k......:...:..........!!73!"7c,&77"&77777"&777777737773777777{777{777!777"777777c777c777777,777,777&77777777############;###;############# LO+J	1	2 ;i':; !!#Ffl++++++++:+++:++++++f+++f++++++l+++l++++++++++== D =D    =D      6   6   =   D         &B& B&&&& B&&&&&&6&&&6&&& &&&B&&&&&&&; ;s   [(([2c                   d}t        d|dd      }t        d t               t        | dz              }|j	                  |      }d	}||u }|st        j                  d
|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}}t        d      }	t        ddd      }
t        dd|
|	      }|	j                   }| }|st        j                  d      dz   dt        j                         v st        j                  |	      rt        j                  |	      ndt        j                  |      dz  }t        t        j                  |            dx}}|
j                   }| }|st        j                  d      dz   dt        j                         v st        j                  |
      rt        j                  |
      ndt        j                  |      dz  }t        t        j                  |            dx}}y) z
    IT2 (BB): Code with `import sqlite3` violates AXIOM_NO_SQLITE.
    CodeProposer.validate_proposal() returns False.
    GitOpsPRCreator must NOT be called.
    zimport sqlite3
from core.interceptors.base_interceptor import BaseInterceptor

class BadInterceptor(BaseInterceptor):
    def intercept(self, ctx):
        return ctx
zcore/interceptors/bad.pyztests/interceptors/test_bad.pyzdef test_bad(): pass
r8   r9   r:   r;   c                    t               S N)r@   prompts    r-   <lambda>z4test_it2_axiom_violation_blocks_pr.<locals>.<lambda>   s
    #7#9 r/   	proposalsopus_clientaxiomatic_testsproposals_dirFrl   z%(py0)s is %(py3)sis_validrY   r`   z:Proposal with sqlite3 should be rejected by AxiomaticTests
>assert %(py5)sra   Nokreturn_valuezhttps://github.com/pr/1   html_urlnumber
fake_token
owner/repogithub_tokengithub_repohttp_client
git_runnerz9git_runner must not have been called before pipeline gate0
>assert not %(py2)s
{%(py2)s = %(py0)s.called
}mock_gitrY   r[   z:http_client must not have been called before pipeline gate	mock_http)r   r   r   r2   validate_proposalrq   rw   ro   rp   rr   rs   _format_assertmsgrt   ru   r   r   called)r(   sqlite_codebad_proposalproposerr   r   r   @py_format4r   r   r   creatorr   s                r-   "test_it2_axiom_violation_blocks_prr      s   	   , 7-	L 9&(([01H )),7HZ8uZZZ8uZZZZZZ8ZZZ8ZZZuZZZZZZZZZZ d+H4MYZ'[\I! 	G [[[[ [[[[[[[x[[[x[[[[[[[[[]]]]!]]]]]]]y]]]y]]]]]]]]]r/   c                   t        dg dd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}||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}}t        d      }t        ddd      }t        dd||      }	|j                  r |	j                  t        dddd      |d       |j                  }| }|st        j                   d      dz   d t	        j
                         v st        j                  |      rt        j                  |      nd t        j                  |      d!z  }
t        t        j                  |
            dx}}|j                  }| }|st        j                   d"      dz   d#t	        j
                         v st        j                  |      rt        j                  |      nd#t        j                  |      d!z  }
t        t        j                  |
            dx}}y)$u   
    IT3 (BB): ShadowArena with pass_rate < 0.8 → ready_for_pr=False.
    Verify no PR is opened when arena result fails.
    g333333?g?皙?old_success_ratenew_success_ratedeltaF	pass_rateaxiom_violationsimproved_metricsready_for_prrl   z4%(py2)s
{%(py2)s = %(py0)s.ready_for_pr
} is %(py5)sarena_resultrf   rg   rb   N皙?)<)z0%(py2)s
{%(py2)s = %(py0)s.pass_rate
} < %(py5)sr   r   zhttps://github.com/pr/99c   r   r   r   r   zf.py zt.py	epoch_it3)proposalr   epoch_idz.git_runner must not be called when arena failsr   r   r   z/http_client must not be called when arena failsr   )r   r   rq   rw   ro   rp   rr   rs   rt   ru   r   r   r   	create_prr   r   r   )r(   r   r   r   r   r   r   r   r   r   r   s              r-   /test_it3_shadow_arena_below_threshold_blocks_prr      s'   
 .1sUXY	L $$--$----$------<---<---$----------!!'C'!C''''!C''''''<'''<'''!'''C''''''' d+H4NZ\']^I! 	G   !&"fb9%  	 	
 PPPP PPPPPPPxPPPxPPPPPPPPPRRRR!RRRRRRRyRRRyRRRRRRRRRr/   c           	     	   t        t        dddgdg      gt        ddd	      t        d
dd	      gd      }t        |       }|j	                  |d      }t        | j                  d            }|D cg c]%  }|j                         s|j                  dk(  s$|' }}g }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }	t        j                  d|D cg c]  }t!        |       c}       dz   d|	iz  }
t#        t        j$                  |
            dx}}| dz  }| dz  }|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&                  } |       }|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}}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(}||kD  }|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c c}w c c}w ).z
    IT4 (BB): Tier1AutonomousUpdater creates NO .py files.
    Verify only KG entities (.jsonl), prompts (.md/.txt), rules (.md),
    and audit (.jsonl) are written.
    zprompt instruction unclear   s1sc1rD   z"core/evolution/dangerous_module.pyrefactorz"this should not produce a .py filetarget_filechange_type	rationalezconfig/prompts/system_prompt.mdprompt_updatezsafe prompt update	epistemicbottlenecksrecommended_fixesscope	it4_epochr   *z.pyr]   z%(py0)s == %(py3)spy_filesr   z)Tier 1 must NOT create .py files. Found: r   ra   NrK   rM   z!KG base directory was not createdC
>assert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}kg_dirrY   r[   r\   zAudit log was not created
audit_filerU   rV   resultr    rX   r   >)z8%(py2)s
{%(py2)s = %(py0)s.kg_entities_added
} > %(py5)srf   rg   rb   )r   r   r   rS   apply_tier1listrglobis_filesuffixrq   rw   ro   rp   rr   rs   r   r2   rt   ru   existsrV   r    kg_entities_added)r(   analysisupdaterr   	all_filesfr   r   r   r   r   r   r   r   r   r   r   s                    r-   "test_it4_tier1_creates_no_py_filesr   (  sO    $8#'&	
 @&>
 =+.
 +H0 H%G  K @F X^^C()I$Ja		E8IJHJ 8r>  8r                  4X4NSV4N3OP    
 %F11J==?=????????????6???6???=??????????;;;; ;;;;;;;:;;;:;;;;;;;;;;;;fk********:***:******f***f******k***k**********##'a'#a''''#a''''''6'''6'''#'''a''''''' K 5Os   5S"S"S"2S'
c                   | dz  }t        dd|      }t        dd      }t        |gt        ddd	|j                   
      gd      }t        t                     }t        |t               t        | dz              }|j                  |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}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-                  |      }d}||u }|st        j*                  d|fd||f      dt        j                         v st        j                   |      rt        j"                  |      ndt        j"                  |      d z  }t        j.                  d!      d"z   d#|iz  }t%        t        j&                  |            dx}}t1        d$g d%d$d&d'd(d)d*d+      }|j2                  }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}}t        d/      }t        d0d1d2      }t5        d3d4||5      }d}|r |j2                  r|j7                  ||d67      }d}||u}|st        j*                  d8|fd9||f      d:t        j                         v st        j                   |      rt        j"                  |      nd:t        j"                  |      d z  }t        j.                  d;      d"z   d#|iz  }t%        t        j&                  |            dx}}t        |t8              }|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                   t8              rt        j"                  t8              nd<t        j"                  |      dz  }	t%        t        j&                  |	            d}|j:                  }d1}||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}}d6}
|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>                  }d0}||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@                  }dA}||k\  }|st        j*                  dB|fdC||f      dDt        j                         v st        j                   |      rt        j"                  |      ndDt        j"                  |      t        j"                  |      d.z  }dd|iz  }t%        t        j&                  |            dx}x}}y)Eu   
    IT5 (BB): Full pipeline:
      MetaArchitect → CodeProposer → AxiomaticTests → ShadowArena → GitOpsPRCreator

    All components are mocked where external I/O is needed.
    Verify the entire chain completes and returns a PRResult.
    zmeta_architect_log.jsonlNrN   pg_connectionlog_pathmodule function refactor needed   )rF   core/interceptors/fix_v1.pyr   zRecurring bottleneck (x3): r   ontologicalr   r   r   r   z# existing interceptor code
rU   rV   r   r   rX   BaseInterceptorin)z4%(py1)s in %(py5)s
{%(py5)s = %(py3)s.code_content
})rZ   r`   ra   rg   rb   Trl   r   r   r   z/Clean proposal should pass axiomatic validationr   ra   gffffff?      ?g?         )r   r   r   old_pass_countnew_pass_counttotal_sagasr   r   r   rf   r   z%https://github.com/owner/repo/pull/42*   r   r   r   r   	epoch_it5r   is notz%(py0)s is not %(py3)s	pr_resultz PR must be created in happy pathr   r]   )z1%(py2)s
{%(py2)s = %(py0)s.pr_number
} == %(py5)s)z3%(py1)s in %(py5)s
{%(py5)s = %(py3)s.branch_name
})z.%(py2)s
{%(py2)s = %(py0)s.pr_url
} == %(py5)s   >=)z2%(py2)s
{%(py2)s = %(py0)s.call_count
} >= %(py5)sr   )!r   rI   r   r   rE   r   r@   r   r   r2   proposerV   r   ro   rp   rq   rr   rs   rt   ru   r9   rw   r   r   r   r   r   r   r   	pr_numberbranch_namepr_url
call_count)r(   r   	architect
bottleneckr   	mock_opusr   r   r   r   @py_assert0r   r   r   r   r   r   r   r   r   r   r   r  s                          r-   !test_it5_full_happy_path_pipeliner  `  s    44HI ""CqQJ#L9&7
8N8N7OP
 
H ';'=>I&(([01H
 
,KLHh--------:---:------h---h-------------------5 5 55 55555 5555555555555555 55555555 ))(3HN8tNNN8tNNNNNN8NNN8NNNtNNNNNNNNNN  # $ 
 L $$,,$,,,,$,,,,,,<,,,<,,,$,,,,,,,,,, d+H4[gi'jkI! 	G IL--%%h{%S	 !D9D DDD9DDDDDDD9DDD9DDDDDDD"DDDDDDDi********:***:******i***i*******************$"$"$$$$"$$$$$$9$$$9$$$$$$"$$$$$$$/)///;/////;////;//////)///)///////////FFFFFFFFFFFFFFF9FFF9FFFFFFFFFFFFFF #!#!####!######8###8######!#######r/   c                <   t               }t               |_        t        d|t               | dz        }|j	                  dddg       |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  }t        j                  d|j                  j
                         dz   d|iz  }t        t        j                  |            dx}x}x}}|j                  j                  D ]O  }	|	j                   r|	j                   d   n|	j"                  j%                  dd      }
|
j&                  } |t(              }|st        j                  d|
 dt(         d      dz   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t        j                  |      dz  }t        t        j                  |            dx}}R 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  }t        j                  dt(         d      dz   d|iz  }t        t        j                  |            dx}}y)z
    IT6 (WB): ShadowArena uses SHADOW: prefix for all Redis keys.
    All saga inputs injected into Redis must use 'SHADOW:<saga_id>' keys.
    Nzshadow_arena_runs.jsonl)r   redis_clientr   r   z!core.evolution.nonexistent_branch
saga_alpha	saga_beta)proposal_branchtest_saga_idsr   r]   )zK%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.set
}.call_count
} == %(py7)s
mock_redis)rY   r[   r\   rb   z/Expected 2 redis.set calls (one per saga), got z
>assert %(py9)spy9r   keyr   zRedis key 'z' must start with ''zN
>assert %(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.startswith
}(%(py3)s)
}r   )rY   r[   r`   ra   zSHADOW:r   r   z&SHADOW_PREFIX must be 'SHADOW:', got 'r   ra   )r   setr   r   evaluate_proposalr  rq   rw   ro   rp   rr   rs   r   rt   ru   call_args_listargskwargsget
startswithr   )r(   r"  arenar   r   r   @py_assert5r   @py_format10set_callr$  r   r   r   r   s                  r-   test_it6_shadow_mode_redis_keysr1    s   
 J[JN&(55	E 
;#[1   >> >$$  $)  $                  %    )*    :*..:S:S9TU      NN11 
"*--hmmAX__5H5HPR5S~~ 	
~m, 	
, 	
  #1-B	
 	
	6	
 	
   	
 	
 		  	
 	
 		  	
 	
	6	
 	
  , 	
 	
 		 , 	
 	
 		 - 	
 	
 	
 	
 	

 & =I%  =I              &    1qA    r/   c                   g d.fd}t        ddd      }t        dd||      }t        d	d
dd      }t        dg ddddd      }|j	                  ||d      }t              }d}||k(  }	|	st        j                  d|	fd||f      dt        j                         v st        j                  t
              rt        j                  t
              nddt        j                         v st        j                        rt        j                        ndt        j                  |      t        j                  |      dz  }
t        j                  dt               d       dz   d|
iz  }t        t        j                  |            d x}x}	}d!   }d"}||k(  }|st        j                  d|fd#||f      t        j                  |      t        j                  |      d$z  }t        j                  d%d!    d&      d'z   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 )/zt
    IT7 (WB): GitOpsPRCreator branch naming convention:
    Branch must be 'genesis-auto-refactor-{epoch_id}'.
    c                Z    | d   dk(  r"t        |       dkD  rj                  | d          y)Nr   checkoutr   r   )r_   append)r)  captured_branchess    r-   tracking_gitz3test_it7_gitops_branch_naming.<locals>.tracking_git  s.    7j SY]$$T!W-r/   zhttps://github.com/pr/7   r   r   r   r   r   zcore/interceptors/fix_it7.pyz?from core.interceptors.base_interceptor import BaseInterceptor
z"tests/interceptors/test_fix_it7.pyzdef test_it7(): pass
r   ?r  r   r   Tr   epoch_42r   r   r]   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr_   r6  rY   rZ   r`   py6z Expected 1 branch checkout, got : z
>assert %(py8)src   Nr   zgenesis-auto-refactor-epoch_42z%(py1)s == %(py4)srZ   r\   zBranch name mismatch: got 'r%  z
>assert %(py6)sr=  )z3%(py2)s
{%(py2)s = %(py0)s.branch_name
} == %(py5)sr  rf   rg   rb   )r)  z	list[str]returnr2   )r   r   r   r   r   r_   rq   rw   ro   rp   rr   rs   r   rt   ru   r  )r(   r7  r   r   r   r   r  r   r.  r   @py_format7@py_format9r  r   r   r   r   r   r6  s                     @r-   test_it7_gitops_branch_namingrD    s   
 $& 4MYZ'[\I! 	G 0W;-	H .1sUXY	L !!(L:!NI  ! Q !Q&   !Q                !    !    "    &'    +3/@+A*B"EVDWX     Q #C #CC  #C         $D    &&7&:%;1=    
   D$DD $DDDDD $DDDDDDD9DDD9DDD DDD$DDDDDDDDr/   c                   g d G fdd      }t               } |dddd       |ddd	d
       |dddd      gdf|j                  _        t        | dz        }t	        |d|      }|j                  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}	||	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%        |      }	d}|	|k(  }|s6t        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                  |      d"z  }t        j&                  d#t%        |j"                               d$z   d%|iz  }t        t        j                  |            dx}x}	x}}|j"                  d&   }|j(                  }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}	}t               }dd+did,dd+did,dd+did,g|j                  _        | d-z  }t+        |d|.      }|j-                  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/d0t        j                         v st        j                  t.              rt        j                  t.              nd0t        j                  |      dz  }t        t        j                  |            d}|j0                  }t%        |      }	d}|	|k\  }|s
t        j                   d1|fd2|	|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                  |      d"z  }d3d%|iz  }t        t        j                  |            dx}x}	x}}|j2                  } |       }|st        j&                  d4      d5z   d6t        j                         v st        j                  |      rt        j                  |      nd6t        j                  |      t        j                  |      d7z  }t        t        j                  |            dx}}y)8u  
    IT8 (BB): ScarAggregator returns clusters → MetaArchitect receives
    scars and produces an ArchitectureAnalysis with bottlenecks.

    Uses mock Qdrant with 3 scars sharing the same unit vector → 1 cluster
    (cosine similarity = 1.0 between identical vectors, above the 0.85 threshold).

    Then MetaArchitect analyzes the same scar set and confirms ArchitectureAnalysis
    with at least 1 bottleneck is returned.
    )g      ?        rF  rF  c                      e Zd Z fdZy)Ctest_it8_scar_aggregator_to_meta_architect_flow.<locals>._MockPointc                8    || _         |||d| _        | _        y )N)textseverity	timestampidr?   vector)selfscar_idrJ  rK  tsunit_vecs        r-   __init__zLtest_it8_scar_aggregator_to_meta_architect_flow.<locals>._MockPoint.__init__*  s    DG$(hRPDL"DKr/   N)__name__
__module____qualname__rT  )rS  s   r-   
_MockPointrH  )  s    	#r/   rX  sc_1zprompt instruction missingr   z2026-02-25T10:00:00+00:00sc_2r9  z2026-02-25T11:00:00+00:00sc_3gffffff?z2026-02-25T12:00:00+00:00Nzscar_agg_log.jsonlz2026-02-24T00:00:00Z)rN   last_epoch_timestampr   r8  lookback_daysrU   rV   reportr   rX   r   r]   )z3%(py2)s
{%(py2)s = %(py0)s.total_scars
} == %(py5)srf   rg   rb   r   )zN%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.clusters
})
} == %(py8)sr_   )rY   rZ   r`   ra   rc   z1Expected 1 cluster (identical unit vectors), got 
>assert %(py10)srd   r   )z4%(py3)s
{%(py3)s = %(py1)s.member_count
} == %(py6)s)rZ   r`   r=  assert %(py8)src   rE   rM  zmeta_arch_log.jsonlr   r   r   r  )zQ%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.bottlenecks
})
} >= %(py8)sassert %(py10)sz(MetaArchitect must write to its log filer   arch_logr   )r   scrollr   r2   r   	aggregaterV   r   ro   rp   rq   rr   rs   rt   ru   total_scarsrw   clustersr_   r   member_countr   analyzer   r   r   )r(   rX  mock_qdrantr   aggr_  r   r   r   r   r   r   r   @py_assert7r   rC  r   r  r.  rB  	ma_qdrantrc  r  r   rS  s                           @r-   /test_it8_scar_aggregator_to_meta_architect_flowrn    sJ    $H# # +K v;SB]^v;SB]^v;SB]^	

 	'K# 8223H
!3C
 ]]]+Ffj)))))))):))):))))))f)))f))))))j)))j))))))))))""""""""""""6"""6""""""""""""" 3 1 1$   1                                 $%    <C<P;QR      ??1/**/a/*a////*a//////*///a/////// I=2N"O[cd=2N"O[cd=2N"O[cd%I! //HI
   q 1Hh 455555555:555:555555h555h555555 4555 45555555555##)3#$))$))))$))))))3)))3))))))x)))x)))#)))$))))))))))??H?HHHHHHHHHH8HHH8HHH?HHHHHHHHHr/   c                    ddl m}  ddlm} ddlm} ddlm} ddlm	} ddl
m} ddlm} dd	lm} | |||||||fD ]  }d
}	||	u}
|
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}
}	 ddlm}m}m}m}m}m}m}m}m}m}m }m	}m!}m"}m}m#}m}m$}m}m%}  d
}	||	u}
|
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}
}	d
}	||	u}
|
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}
}	d
}	||	u}
|
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}
}	g }
tM        |tN              }!|!}"|!rtQ        |      }#d}$|#|$kD  }%|%}"|"s'ddt%        j&                         v st!        j(                  tL              rt!        j*                  tL              nddt%        j&                         v st!        j(                  |      rt!        j*                  |      nddt%        j&                         v st!        j(                  tN              rt!        j*                  tN              ndt!        j*                  |!      dz  }&|
jS                  |&       |!rt!        j"                  d%fd#$f      dt%        j&                         v st!        j(                  tP              rt!        j*                  tP              nddt%        j&                         v st!        j(                  |      rt!        j*                  |      ndt!        j*                  |#      t!        j*                  |$      dz  }'dd|'iz  }(|
jS                  |(       t!        jT                  |
d      i z  })d d!|)iz  }*t-        t!        j.                  |*            d
x}"x}
x}!x}#x}%}$g }
tM        |tN              }!|!}"|!rtQ        |      }#d}$|#|$kD  }%|%}"|"s'ddt%        j&                         v st!        j(                  tL              rt!        j*                  tL              ndd"t%        j&                         v st!        j(                  |      rt!        j*                  |      nd"dt%        j&                         v st!        j(                  tN              rt!        j*                  tN              ndt!        j*                  |!      dz  }&|
jS                  |&       |!rt!        j"                  d|%fd|#|$f      dt%        j&                         v st!        j(                  tP              rt!        j*                  tP              ndd"t%        j&                         v st!        j(                  |      rt!        j*                  |      nd"t!        j*                  |#      t!        j*                  |$      dz  }'dd|'iz  }(|
jS                  |(       t!        jT                  |
d      i z  })d d!|)iz  }*t-        t!        j.                  |*            d
x}"x}
x}!x}#x}%}$y
)#z
    IT9 (BB): Verify all 8 modules import without error and all
    __init__.py exports are accessible from core.evolution.
    r   )r	   )r   )r   )r   )r   )r   )r   )r   Nr  r  clsr   assert %(py5)sra   )r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r	   r
   r   z.%(py6)s
{%(py6)s = %(py2)s(%(py3)s, %(py4)s)
}rV   r   r   )r[   r`   r\   r=  r   )z2%(py11)s
{%(py11)s = %(py8)s(%(py9)s)
} > %(py14)sr_   )rc   r#  py11py14z%(py16)spy16zassert %(py19)spy19r   )+core.evolution.immutable_kernelr	   core.evolution.axiomatic_testsr   core.evolution.meta_architectr   core.evolution.shadow_arenar   core.evolution.scar_aggregatorr   core.evolution.code_proposerr    core.evolution.gitops_pr_creatorr   'core.evolution.tier1_autonomous_updaterr   rq   rw   ro   rp   rr   rs   rt   ru   core.evolutionr
   r   r   r   r   r   r   r   r   r   r   r   r    rV   r   r_   r5  _format_boolop)+IKATMASASAggCPGPRT1Urp  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  @py_assert10@py_assert13@py_assert12rB  @py_format15@py_format17@py_format18@py_format20s+                                              r-   test_it9_all_module_importsr  f  s*    FCA=E?GU BBb#s3 s$s$ss$     0 #'&?$&&&&?$&&&&&&?&&&?&&&$&&&&&&&!!:T!!!!:T!!!!!!:!!!:!!!T!!!!!!!##<t####<t######<###<###t#######C:lD)C)C)c,.?C!C.?!.CCCCCCC:CCC:CCCCCClCCClCCCCCCDCCCDCCC)CCCC)CCC.?!CCCCCCcCCCcCCCCCC,CCC,CCC.?CCC!CCCCCCCCCCCCCCA:k4(A(A(S-=AA-=-AAAAAAA:AAA:AAAAAAkAAAkAAAAAA4AAA4AAA(AAAA(AAA-=AAAAAASAAASAAAAAAAAAAAA-=AAAAAAAAAAAAAAAAAr/   c           	        d}|t         v }|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}}d	}|t         v }|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}}d
}|t         v }|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}}t        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                  t               rt        j                  t               ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        |       }	t         D ]D  }
|	j                  } ||
      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |	      rt        j                  |	      ndt        j                  |      dt	        j
                         v st        j                  |
      rt        j                  |
      ndt        j                  |      t        j                  |      dz  }t        j                  d|
 d      dz   d|iz  }t        t        j                  |            dx}x}x}}G t         D ]I  }
d|
 }|	j                  } ||      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |	      rt        j                  |	      ndt        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        j                  d| d      dz   d|iz  }t        t        j                  |            dx}x}x}}L |	j                  }d} ||      }d}||u }|st        j                  d|fd ||f      dt	        j
                         v st        j                  |	      rt        j                  |	      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      d!z  }d"d#|iz  }t        t        j                  |            dx}x}x}x}}|	j                  }d$} ||      }d}||u }|st        j                  d|fd ||f      dt	        j
                         v st        j                  |	      rt        j                  |	      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      d!z  }d"d#|iz  }t        t        j                  |            dx}x}x}x}}|	j                  }d%} ||      }d}||u }|st        j                  d|fd ||f      dt	        j
                         v st        j                  |	      rt        j                  |	      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      d!z  }d"d#|iz  }t        t        j                  |            dx}x}x}x}}y)&z
    IT10 (BB/WB): KERNEL_FILES contains the 3 expected paths.
    ImmutableKernel.is_kernel_file() recognizes all kernel file paths
    (both relative and absolute).
    z%core/interceptors/base_interceptor.pyr   z%(py1)s in %(py3)sr   rZ   r`   rq  ra   Nztests/axiomatic/test_axioms.pyzcore/storage/shadow_router.pyr   r]   r;  r_   r<  ra  rc   Trl   )zT%(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.is_kernel_file
}(%(py3)s)
} is %(py8)sr   r+   rY   r[   r`   ra   rc   zExpected is_kernel_file('z
') == Truer`  rd   z/mnt/e/genesis-system/abs_pathzsome/random/file.pyF)zT%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.is_kernel_file
}(%(py4)s)
} is %(py9)s)rY   r[   r\   r=  r#  zassert %(py11)srr  zcore/genesis_execution_layer.pyr   )r   rq   rw   rs   ro   rp   rr   rt   ru   r_   r3   is_kernel_filer   )r(   r  r   r   r   r.  r   rB  rC  r   r+   r   rl  r   r   r  r   @py_assert8r/  @py_format12s                       r-   /test_it10_kernel_files_constant_and_recognitionr    s    3B2lBBBB2lBBB2BBBBBBlBBBlBBBBBBB+;+|;;;;+|;;;+;;;;;;|;;;|;;;;;;;*:*l::::*l:::*::::::l:::l:::::::|!!!!!!!!!!!!3!!!3!!!!!!|!!!|!!!!!!!!!!!!!(#F  
$$ 	
$S) 	
T 	
)T1 	
 	
 	
)T 	
 	
	6	
 	
   	
 	
 		  	
 	
 		 % 	
 	
	6	
 	
  &) 	
 	
 		 &) 	
 	
 		 * 	
 	
 		 .2 	
 	
  (uJ7	
 	
 	
 	
 	
 	

  
+C51$$ 	
$X. 	
$ 	
.$6 	
 	
 	
.$ 	
 	
	6	
 	
   	
 	
 		  	
 	
 		 % 	
 	
	6	
 	
  &. 	
 	
 		 &. 	
 	
 		 / 	
 	
 		 37 	
 	
  (z<	
 	
 	
 	
 	
 	

   @!6@ !67@5@75@@@@75@@@@@@6@@@6@@@ @@@!6@@@7@@@5@@@@@@@  L!BL !BCLuLCuLLLLCuLLLLLL6LLL6LLL LLL!BLLLCLLLuLLLLLLL  -- $--$----$------6---6--- ------$-----------r/   c                    t               } | j                  di       }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 cg c]  }|j                   }	}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}
}yc c}w )zp
    BB: AxiomaticTests.run_all detects sqlite3 import.
    Verifies AXIOM_NO_SQLITE violation is returned.
    z2import sqlite3
conn = sqlite3.connect(':memory:')
r9   state_contentrU   rV   r   r   rX   NFrl   z.%(py2)s
{%(py2)s = %(py0)s.passed
} is %(py5)srf   rg   rb   AXIOM_NO_SQLITEr   r  	axiom_idsr  rq  ra   r   run_allrV   r   ro   rp   rq   rr   rs   rt   ru   passedrw   
violationsaxiom_idcheckerr   r   r   r   r   r   r   vr  r  r   r   s                r-   "test_it11_axiom_no_sqlite_detectedr    s   
 G__K  F fk********:***:******f***f******k***k**********==!E!=E!!!!=E!!!!!!6!!!6!!!=!!!E!!!!!!!%+%6%677I7)	))))	)))))))))	)))	))))))) 8s   Kc                    t               } | j                  dddi      }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 cg c]  }|j                   }	}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}
}y
c c}w )z
    BB: AxiomaticTests.run_all detects API key leak in state_content.
    Verifies AXIOM_NO_API_KEY_LEAK violation is returned.
    z# clean code
tokenzsk-12345abcr  rU   rV   r   r   rX   NFrl   r  rf   rg   rb   AXIOM_NO_API_KEY_LEAKr   r  r  r  rq  ra   r  r  s                r-   (test_it12_axiom_no_api_key_leak_detectedr    s   
 G__%.  F fk********:***:******f***f******k***k**********==!E!=E!!!!=E!!!!!!6!!!6!!!=!!!E!!!!!!!%+%6%677I7"/"i////"i///"//////i///i/////// 8s   Kc                    t        d t                     } t        dddd      }| j                  } ||      }d}||u }|st	        j
                  d	|fd
||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      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}x}}y)z
    BB: CodeProposal without BaseInterceptor in code_content fails validation.
    Validates the validate_proposal gate enforces BaseInterceptor inheritance.
    c                     yNz{} r   s    r-   r   zMtest_it13_code_proposer_validates_base_interceptor_required.<locals>.<lambda>      r/   r   r   core/interceptors/fix.pyz?class FixInterceptor:
    def intercept(self, ctx): return ctx
ztests/interceptors/test_fix.pydef test_fix(): pass
r   Frl   zW%(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.validate_proposal
}(%(py3)s)
} is %(py8)sr   r   r  rb  rd   Nr   r   r   r   rq   rw   ro   rp   rr   rs   rt   ru   r   r   r   r   rl  r   rC  r   s           r-   ;test_it13_code_proposer_validates_base_interceptor_requiredr    s    
 '&(H
 ,X7-	H %%8%h/858/58888/588888888888888%888888h888h888/88858888888r/   c                    t        d t                     } t        dddd      }| j                  } ||      }d}||u }|st	        j
                  d	|fd
||f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      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}x}}y)z
    BB: CodeProposal test_file_path not starting with 'tests/' fails validation.
    Validates test path convention is enforced.
    c                     yr  r  r   s    r-   r   zHtest_it14_code_proposer_validates_test_path_convention.<locals>.<lambda>  r  r/   r  r  zofrom core.interceptors.base_interceptor import BaseInterceptor
class FixInterceptor(BaseInterceptor):
    pass
zwrong_path/test_fix.pyr  r   Frl   r  r   r   r  rb  rd   Nr  r  s           r-   6test_it14_code_proposer_validates_test_path_conventionr    s    
 '&(H ,A 0-H %%8%h/858/58888/588888888888888%888888h888h888/88858888888r/   c                T   t        dg ddddd      }|j                  dk\  xr t        |j                        dk(  }d}||u }|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}}|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}}y)z
    WB: ShadowArena with pass_rate exactly 0.8 and no violations
    must return ready_for_pr=True (boundary condition: >= 0.8 is accepted).
    r   r   r   Tr   r   rl   r   computedr   rq  ra   Nr   r   rf   rg   rb   )r   r   r_   r   rq   rw   ro   rp   rr   rs   rt   ru   r   )
r(   r   r  r   r   r   r   r   r   r   s
             r-   5test_it15_shadow_arena_pass_rate_exactly_08_qualifiesr    s    .1sUXY	L %%,X\5R5R1SWX1XH8t8t88t$$,,$,,,,$,,,,,,<,,,<,,,$,,,,,,,,,,r/   c           	        t        d      }t        ddd      }t        dd||      }t        d	d
dddddgi      }t        dg dddddd      }|j	                  ||d      }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!}||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}||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}||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}}y )"zW
    WB: GitOpsPRCreator._build_pr_body includes epoch_id, pass_rate, and metrics.
    r   r   zhttps://github.com/pr/16   r   r   r   r   zcore/interceptors/fix_it16.pyz# clean
ztests/interceptors/test_it16.pyzdef test(): pass
r5   r6   sc_002r7   gq=
ףp?r  gzG?   )r   r   r   r  Tr   epoch_16r   r   r  bodyr  rq  ra   Nz92.0%)r   r   r   r   _build_pr_bodyrq   rw   rs   ro   rp   rr   rt   ru   )r(   r   r   r   r   r   r  r  r   r   r   s              r-   3test_it16_gitops_pr_body_contains_epoch_and_metricsr    s    d+H4NZ\']^I! 	G 1 8)"Xx$89H  # $	
 
L !!(L:!ND:::7d?7d7dd8t8t8tt8t8t8ttr/   c                   | dz  }t        dd|      }|j                  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                  }g }||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                  }g }||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                  } |       }|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}}y)z
    BB: MetaArchitect with no Qdrant and no PG clients returns
    ArchitectureAnalysis with empty bottlenecks (scope='epistemic').
    zempty_ma.jsonlNr   r8  r]  rU   rV   r   r   rX   r]   )z3%(py2)s
{%(py2)s = %(py0)s.bottlenecks
} == %(py5)srf   rg   rb   )z9%(py2)s
{%(py2)s = %(py0)s.recommended_fixes
} == %(py5)sr   )z-%(py2)s
{%(py2)s = %(py0)s.scope
} == %(py5)sz4MetaArchitect must write log even for empty analysisr   r   r   )r   ri  rV   r   ro   rp   rq   rr   rs   rt   ru   r   rw   r   r   r   r   )
r(   r   r  r   r   r   r   r   r   r   s
             r-   :test_it17_meta_architect_no_clients_returns_empty_analysisr  F  sy   
 **HI
   q 1Hh 455555555:555:555555h555h555555 4555 45555555555%2%2%%%%2%%%%%%8%%%8%%%%%%2%%%%%%%%%++%++++%++++++8+++8+++%++++++++++>>([(>[((((>[((((((8(((8(((>((([(((((((??T?TTTTTTTTTT8TTT8TTT?TTTTTTTTTr/   c                     t        t                     } t        j                  t        d      5  | j                  d       ddd       y# 1 sw Y   yxY w)zy
    BB: ScarAggregator.get_top_clusters() before aggregate() raises RuntimeError.
    Validates defensive contract.
    )rN   zNo report available)matchr   )nN)r   r   ry   rz   RuntimeErrorget_top_clusters)rk  s    r-   Ctest_it18_scar_aggregator_get_top_clusters_without_aggregate_raisesr  Z  sE    
 y{
3C	|+@	A "q!" " "s   AAc                   t        t        dddgdg      gt        ddd	      gd
      }t        |       }|j	                  |d       | dz  }|j
                  } |       }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|j                  d      j                         D cg c]  }|j                         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}}
t%        j&                  |d         }h d}|j(                  }|j*                  }
 |
       }t-        |      } ||      }|sSd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"t        j                         v st        j                  |      rt        j                  |      nd"t        j                  |
      t        j                  |      t        j                  |      t        j                  |      d#z  }t        t        j                  |            dx}x}
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c c}w ))z
    WB: Audit log produced by Tier1AutonomousUpdater is valid JSONL.
    Each line must parse as JSON with required keys.
    zcontext window overflowr  r   r   rD   zconfig/prompts/ctx.mdr   zreduce contextr   r   r   
it19_epochr   rM   zAassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}r   r   Nrh   ri   r   r]   r;  r_   linesr<  ra  rc   r   >   r   rL  rules_updatedscars_updatedprompts_updatedr   zassert %(py12)s
{%(py12)s = %(py2)s
{%(py2)s = %(py0)s.issubset
}(%(py10)s
{%(py10)s = %(py3)s(%(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s.keys
}()
})
})
}required_keysr&  entry)rY   r[   r`   r\   r=  rc   rd   re   r   r?  r@  zassert %(py6)sr=  )r   r   r   rS   r   r   ro   rp   rq   rr   rs   rt   ru   	read_text
splitlinesstripr_   rw   r=   loadsissubsetkeysr&  )r(   r   r   r   r   r   r   lr  r   r.  r   rB  rC  r  r  rl  r   @py_assert11r   r  s                        r-   0test_it19_tier1_updater_audit_log_is_valid_jsonlr  d  s   
 $#<*.5'C

 $;$3?OQ
 
H H%G<811J::",,g,>IIKY1qwwyQYEYu::?:33uu:JJuQx EM !!4ejj4jl4#l"34!"344444444=444=444!444444#444#444444e444e444j444l444"344444444444,,,,,,,,,,,,,,,,,,, Zs   Q2-Q2c                    ddl m}  h d}|D ]1  }t        | |      }|st        j                  d| d      dz   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                  |      rt        j                  |      nd	t        j                  |      d
z  }t        t        j                  |            d}4 | j                  D ]1  }t        | |      }|st        j                  d| d      dz   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                  |      rt        j                  |      nd	t        j                  |      d
z  }t        t        j                  |            d}4 y)z
    BB: core.evolution.__init__.py exports all 18 names from all 8 stories.
    Validates the complete __all__ contract for Module 8.
    r   N>   r   r   r
   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r	   r   r   z/core.evolution.__init__.py is missing export: 'r%  z7
>assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}hasattrcenamerX   zcore.evolution.__all__ lists 'z' but it is not accessible)r~  	evolutionr  rq   r   ro   rp   rr   rs   rt   ru   __all__)r  expected_exportsr  r   r   s        r-   'test_it20_complete_module8_init_exportsr    s.   
  " ! 
r4  	
  	
  >dV1E	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
    	
 	
 		   	
 	
 		 ! 	
 	
 	
 	
 	

 

 
r4  	
  	
  -TF2LM	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
    	
 	
 		   	
 	
 		 ! 	
 	
 	
 	
 	

r/   __main__u;   IT1:  ImmutableKernel lock → write raises PermissionErrorz(IT2:  Axiom violation blocks PR pipelinez,IT3:  Shadow Arena below threshold blocks PRz!IT4:  Tier 1 creates no .py fileszIT5:  Full happy path pipelinez/IT6:  Shadow Mode Redis keys use SHADOW: prefixz%IT7:  GitOps branch naming conventionu+   IT8:  ScarAggregator → MetaArchitect flowzIT9:  All module imports workz+IT10: Kernel files constant and recognitionzIT11: AXIOM_NO_SQLITE detectedz$IT12: AXIOM_NO_API_KEY_LEAK detectedz5IT13: CodeProposer validates BaseInterceptor requiredz1IT14: CodeProposer validates test path conventionz3IT15: Shadow Arena pass_rate=0.8 boundary qualifiesz/IT16: GitOps PR body contains epoch and metricsz5IT17: MetaArchitect no clients returns empty analysisz=IT18: ScarAggregator get_top_clusters before aggregate raisesz$IT19: Tier1 audit log is valid JSONLz+IT20: Module 8 __init__.py exports complete _:r   /Tr"   z	  [PASS] r   z	  [FAIL] r>  
z<============================================================u)   Story 8.09 — Module 8 Integration TestszTests Run:    zTests Passed: zTests Failed: zStatus:       PASSFAIL)s   # kernel file
)r(   r   r)   bytesrA  dict)r(   r   rA  r	   )r   z!tests/interceptors/test_fix_v1.py)r8   r2   r:   r2   rA  r2   )r   r   )rH   r2   rF   intrA  r   r   )r(   r   rA  r   )e__doc__
__future__r   builtinsro   _pytest.assertion.rewrite	assertionrewriterq   r=   osstatsyspathlibr   typingr   unittest.mockr   r   r   ry   GENESIS_ROOTpathinsertrv  r	   r
   r   r   r   rw  r   r   r   rx  r   r   r   r   ry  r   r   r   rz  r   r   r   r{  r   r   r|  r   r   r}  r   r    r.   r3   r@   rI   rS   r   r   r   r   r  r1  rD  rn  r  r  r  r  r  r  r  r  r  r  r  r  rU  tempfile	tracebackTESTSr  rx   TemporaryDirectory_tmpdirr  fnreplacetmpr&   print	Exceptionexc	print_excr_   exitr  r/   r-   <module>r     s  B #    	  
   0 0  'sxxHHOOA|$  
  
 
4 3= 	* 2	
		 	 '>-^j#SV0(pT$x"T-EjCIV0Bp.L* 0 9&9*-$%PU("-D!
P z 
GHxy	35WX	79hi	,.PQ	)+LM	:<[\	02OP	68gh	(*EF	68gh	)+MN	/1YZ	@B}~	<>tu	>@uv	:<op	@B|}	H  KN  	O	/1ab	68_`)E. FF	$	$	$	& ' 
	HD"w-$,,sC"8"@"@b"I"Q"QRUWZ"[[CIIdTI23	$()!
	 
Bvh-	57	N3u:,
'(	N6(
#$	N6(
#$	NVq[6f=
>?	VHz c J  	$r#/0#	##%! s7   ?AJ I)/J )J.$JJ JJ  J)