
    4jT                    \   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ZddlmZ ddlmZ ddlZ ee      j+                         j,                  d   Z ee      ej2                  vr"ej2                  j5                  d ee             d0dZ edd	        ed
d       ej8                  j;                  d      Ze eedd      jA                  d      reZ!nejD                  jG                  dedz  dz   eedz        g      Z$g Z%dZ&e$e&uZ'e'Z(e're$jR                  Z*dZ+e*e+uZ,e,Z(e(sB ejZ                  de'fde$e&f      d ej\                         v s ej^                  e$      r ej`                  e$      nd ej`                  e&      dz  Z1dde1iz  Z2e%jg                  e2       e'r ejZ                  de,fde*e+f      d ej\                         v s ej^                  e$      r ej`                  e$      nd ej`                  e*       ej`                  e+      dz  Z4dde4iz  Z5e%jg                  e5        ejl                  e%d      i z  Z7dde7iz  Z8 e9 ejt                  e8            dxZ(xZ%xZ'xZ&xZ*xZ,Z+ejD                  jw                  e$      Z!e!ej8                  d<   e$jR                  jy                  e!       ddl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE ddlFmGZGmHZImJZJmKZKmLZL dd lMmNZNmOZO d!ZPd1d"ZQd1d#ZRd1d$ZSd1d%ZTd1d&ZUd1d'ZVd1d(ZWd1d)ZXd1d*ZYd1d+ZZd1d,Z[d1d-Z\d1d.Z]d1d/Z^y)2u  tests/regression/test_dispatch_init_callback_wiring.py

task-2621 — DISPATCH_INIT_PRODUCTION_PATH_CALLBACK_CONTRACT_WIRING regression.

Validates the ADDITIVE wiring at dispatch/__init__.py production path
(site #1 composite L~2962 · site #2 single-team L~3993) that pins the
callback owner to the independent ANU key (c119085addb0f8b7) and
fail-closes against the executor self-key (1e41a2324a3ccdd0). All wiring
is ADDITIVE — the existing dispatch.dispatch() PASS-path is byte-0.

Required fixtures (회장 §5 verbatim, V1–V6):
  (V1) production path call evidence — the contract symbols are actually
       imported AND bound in dispatch.__init__ at module load time
  (V2) normal callback mandatory — classify_dispatch_contract returns
       DISPATCH_CONTRACT_VIOLATION when normal+fallback are both absent
  (V3) fallback safety-net argv built — build_anu_owned_callback_request
       returns ok=True with argv containing the ANU key (recovery-only)
  (V4) self-key fail-closed — assert_collector_key_is_independent_anu
       raises ExecutorSelfKeyForbidden on the executor self-key
  (V5) DISPATCH_CONTRACT_VIOLATION classification — explicit fixture
  (V6) recovery watcher idempotent 1-shot — spawn exactly once

Spec (sha256 17673fa80a960cea497396a9915c39046e4f339494cab2cd79c55b8a30efb202):
  memory/tasks/task-2621.md (chair-authorized · ADDITIVE only · byte-0
  invariants enumerated in §4/§6/§7)

Import strategy mirrors test_callback_owner_enforcement_2553plus49.py — the
tests/ tree contains a `dispatch/` shadow package (tests/dispatch/__init__.py)
that pytest's rootdir-based discovery would otherwise resolve to. We
hermetically load the real workspace modules via spec_from_file_location.
    )annotationsN)Path)mock   c                   t         j                  j                  |       }|t        |dd      j	                  |      r|S t
        j                  j                  | t        |z        }g }d}||u}|}|r|j                  }d}	||	u}
|
}|slt        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  }|j!                  |       |rt        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  }|j!                  |       t        j"                  |d      i z  }dd|iz  }t%        t        j&                  |            dx}x}x}x}x}x}
}	t
        j                  j)                  |      }|t         j                  | <   |j                  j+                  |       |S )u  Hermetic file-path import for the dispatch package + submodules ONLY.

    The tests/ tree shadows the real `dispatch/` with `tests/dispatch/` —
    so under pytest collection, `import dispatch` would otherwise resolve to
    the (empty) shadow package and `from dispatch.callback_owner_enforcer
    import ...` would ModuleNotFoundError. We side-load the real workspace
    dispatch submodules via spec_from_file_location, pre-seed sys.modules
    with the canonical dotted names, and only THEN regular imports resolve.

    Skipped if the canonical name already maps to the real module (so this
    module remains idempotent under repeated test collection).
    N__file__ is notz%(py2)s is not %(py5)sspecpy2py5%(py7)spy7z5%(py11)s
{%(py11)s = %(py9)s.loader
} is not %(py14)spy9py11py14%(py16)spy16r   assert %(py19)spy19)sysmodulesgetgetattrendswith	importlibutilspec_from_file_location_ROOTloader
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprappend_format_boolopAssertionError_format_explanationmodule_from_specexec_module)modnamerelpathexistingr   @py_assert1@py_assert4@py_assert3@py_assert0@py_assert10@py_assert13@py_assert12@py_format6@py_format8@py_format15@py_format17@py_format18@py_format20mods                     J/home/jay/workspace/tests/regression/test_dispatch_init_callback_wiring.py
_load_realrD   1   sc    {{w'H*b A J J7 S>>11'57?KD7t74t77474 777774t77777747774777t77777774777777777777777477777777777777
..
)
)$
/CCKKKKC J    z dispatch.callback_owner_enforcerz#dispatch/callback_owner_enforcer.pyz(dispatch.normal_fallback_callback_helperz+dispatch/normal_fallback_callback_helper.pydispatchr   r	   zdispatch/__init__.py__init__.py)submodule_search_locationsr
   r   _dispatch_init_specr   r   r   r   r   r   r   r   r   )CONTRACT_OKDISPATCH_CONTRACT_VIOLATIONEXECUTOR_SELF_KEY_FORBIDDENINDEPENDENT_ANU_KEYExecutorSelfKeyForbiddenRecoveryWatcher'assert_collector_key_is_independent_anuclassify_dispatch_contract)COLLECTOR_ROLE_ANUPASSPATH_DISPATCH_PYSELF_COLLECTOR_FORBIDDENenforce_callback_owner)CALLBACK_KIND_FALLBACK build_anu_owned_callback_request1e41a2324a3ccdd0c            	        d} d}t        t        | |      }d}||u }|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                  t              rt        j                  t              ndt        j                  |       t        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx} x}x}x}}t        j                  }|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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        j                  }|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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        j                  } t        |       }|sd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  }t        t        j                  |            dx} }t        j                   } t        |       }|sd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  }t        t        j                  |            dx} }t        j"                  }|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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        j&                  }|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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        j*                  } t        |       }|sd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  }t        t        j                  |            dx} }t        j,                  }|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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)zdispatch.__init__ must import and bind all three contract authorities
    so the wiring at site #1 (L~2962) and site #2 (L~3993) can call them.
    _T2621_CONTRACT_AVAILABLEFTis)zC%(py7)s
{%(py7)s = %(py0)s(%(py1)s, %(py3)s, %(py5)s)
} is %(py10)sr   DISPATCH_INIT)py0py1py3r   r   py10zassert %(py12)spy12N==)z6%(py2)s
{%(py2)s = %(py0)s._T2621_ANU_KEY
} == %(py4)srM   r_   r   py4assert %(py6)spy6)zA%(py2)s
{%(py2)s = %(py0)s._T2621_SELF_KEY_FORBIDDEN
} == %(py4)srL   z_assert %(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s._t2621_assert_independent_anu
})
}callable)r_   r`   ra   r   z_assert %(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s._t2621_enforce_callback_owner
})
})zA%(py2)s
{%(py2)s = %(py0)s._T2621_COLLECTOR_ROLE_ANU
} == %(py4)srR   )z?%(py2)s
{%(py2)s = %(py0)s._T2621_PATH_DISPATCH_PY
} == %(py4)srT   ziassert %(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s._t2621_build_anu_owned_callback_request
})
})z<%(py2)s
{%(py2)s = %(py0)s._T2621_KIND_FALLBACK
} == %(py4)srW   )r   r^   r&   r'   r(   r)   r*   r+   r.   r/   _T2621_ANU_KEYrM   _T2621_SELF_KEY_FORBIDDENrL   _t2621_assert_independent_anurj   _t2621_enforce_callback_owner_T2621_COLLECTOR_ROLE_ANUrR   _T2621_PATH_DISPATCH_PYrT   '_t2621_build_anu_owned_callback_request_T2621_KIND_FALLBACKrW   )@py_assert2r6   @py_assert6@py_assert9@py_assert8@py_format11@py_format13r5   r7   @py_format5@py_format7r<   s               rC   /test_v1a_production_path_contract_symbols_boundr{      s    #>MuM7="=uEMMEMMMMEMMMMMM7MMM7MMMMMM=MMM=MMM"=MMMuMMMEMMMMMMMMMM''>'+>>>>>'+>>>>>>>=>>>=>>>'>>>>>>+>>>>+>>>>>>>>22Q26QQQQQ26QQQQQQQ=QQQ=QQQ2QQQQQQ6QQQQ6QQQQQQQQ!??@8?@@@@@@@@8@@@8@@@@@@M@@@M@@@?@@@@@@@@@@!??@8?@@@@@@@@8@@@8@@@@@@M@@@M@@@?@@@@@@@@@@22H26HHHHH26HHHHHHH=HHH=HHH2HHHHHH6HHHH6HHHHHHHH00D04DDDDD04DDDDDDD=DDD=DDD0DDDDDD4DDDD4DDDDDDDD!IIJ8IJJJJJJJJ8JJJ8JJJJJJMJJJMJJJIJJJJJJJJJJ--G-1GGGGG-1GGGGGGG=GGG=GGG-GGGGGG1GGGG1GGGGGGGGrE   c                    t         j                  t         j                         t         j                  dt        t         j                  t         j                  t         j
                  dddddt         j                        } | j                  }|t        k(  }|st        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  }t        j                  | j                         dz   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)u   Calling the wired contract functions with production-path arguments
    (collector_key=ANU_KEY, role=ANU) MUST succeed — this is the PASS-path
    that the production wiring exercises every dispatch.
    ztask-2621-v1bzt2621-norm-v1bzt2621-fb-v1bzt2621-disp-v1b
6937032012Ttask_idexecutor_keycollector_keycollector_owner_keycollector_rolenormal_collector_cron_idfallback_callback_cron_iddispatch_cron_idchat_idprompt_claims_anu_collector
entry_pathrd   )z/%(py2)s
{%(py2)s = %(py0)s.verdict
} == %(py4)senfENFORCER_PASSrf   z
>assert %(py6)sri   Nr\   )z@%(py2)s
{%(py2)s = %(py0)s.owner_is_independent_anu
} is %(py5)sr_   r   r   assert %(py7)sr   )r^   rm   rk   rn   EXECUTOR_SELF_KEY_DEV6ro   rp   verdictr   r&   r'   r(   r)   r*   r+   _format_assertmsgreasonsr.   r/   owner_is_independent_anu)r   r5   r7   ry   rz   r6   r<   r=   s           rC   ;test_v1b_production_path_contract_calls_succeed_for_anu_keyr      se   
 //0L0LM

5
5+#22)88$>>!1"0)$( 88 6 C ;;4;-'444;-44444434443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//////3///3///'///4///////rE   c                 B   t        dddd      } | 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                  }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                  }|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}}y)u   The §7b classifier MUST classify
    (normal_missing + fallback_missing + result_present) as a
    DISPATCH_CONTRACT_VIOLATION with recovery_required=True.
    ztask-2621-v2FTr   normal_callback_presentfallback_presentresult_presentrd   z6%(py2)s
{%(py2)s = %(py0)s.classification
} == %(py4)srecrK   rf   rh   ri   Nr\   z9%(py2)s
{%(py2)s = %(py0)s.recovery_required
} is %(py5)sr   r   r   )zJ%(py2)s
{%(py2)s = %(py0)s.recovery_is_fixed_time_or_dead_man
} is %(py5)s)z5%(py2)s
{%(py2)s = %(py0)s.collector_key
} == %(py4)srM   )rQ   classificationrK   r&   r'   r(   r)   r*   r+   r.   r/   recovery_required"recovery_is_fixed_time_or_dead_manr   rM   r   r5   r7   ry   rz   r6   r<   r=   s           rC   =test_v2_normal_callback_mandatory_violation_when_both_missingr      s
   
 % %	C <!<<<<<!<<<<<<<3<<<3<<<<<<<<<!<<<<!<<<<<<<<  (D( D(((( D((((((3(((3((( (((D(((((((11:U:1U::::1U::::::3:::3:::1:::U:::::::3 33333 333333333333333333333 3333 33333333rE   c                    t        dddd      } | 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                  }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{Counter-evidence: when normal callback IS present, classifier returns
    CONTRACT_OK with fallback cancel-on-success.
    ztask-2621-v2bTr   rd   r   r   rJ   rf   rh   ri   Nr\   )zB%(py2)s
{%(py2)s = %(py0)s.fallback_cancel_on_success
} is %(py5)sr   r   r   )rQ   r   rJ   r&   r'   r(   r)   r*   r+   r.   r/   fallback_cancel_on_successr   s           rC   /test_v2b_normal_callback_present_is_contract_okr      s    % $	C ,,,,,,,,,,,3,,,3,,,,,,,,,,,,,,,,,,,))1T1)T1111)T11111131113111)111T1111111rE   c                    t        t        dt        t        ddddddddt        	      } | j
                  }|st        j                  | j                        d
z   dt        j                         v st        j                  |       rt        j                  |       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                  }t        |v }|st        j                   d|fdt        |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                  |      dz  }dd|iz  }t        t        j                  |            d x}}| j                  }t        |v}|st        j                   d|fdt        |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                  |      dz  }dd|iz  }t        t        j                  |            d 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}}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}
}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}
}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}
}d"}	| j                  d#   }|	|v }
|
slt        j                   d|
fd$|	|f      t        j                  |	      t        j                  |      d%z  }dd|iz  }t        t        j                  |            d x}	x}
}d&}	| j                  d#   }|	|v }
|
slt        j                   d|
fd$|	|f      t        j                  |	      t        j                  |      d%z  }dd|iz  }t        t        j                  |            d x}	x}
}y )'Nztask-2621-v3r}   u   [task-2621 fallback safety-net · recovery-only · non-blocking] task_id=task-2621-v3 — 미수신 안전망 · cancel-on-success.1hzt2621-fb-v3zt2621-disp-v3zt2621-norm-v3Tkindr   r   	owner_keyr   promptatcron_idr   r   r   r   r   z(
>assert %(py2)s
{%(py2)s = %(py0)s.ok
}reqr_   r   r
   )z0%(py2)s
{%(py2)s = %(py0)s.argv
} is not %(py5)sr   r   r   in)z,%(py0)s in %(py4)s
{%(py4)s = %(py2)s.argv
}rM   rf   rh   ri   not in)z0%(py0)s not in %(py4)s
{%(py4)s = %(py2)s.argv
}r   rd   )z1%(py2)s
{%(py2)s = %(py0)s.owner_key
} == %(py4)scokacdir)z,%(py1)s in %(py5)s
{%(py5)s = %(py3)s.argv
})r`   ra   r   --cron--oncezrecovery-onlyr   )z%(py1)s in %(py4)sr`   rg   znon-blocking)rX   rW   r   rM   rT   okr&   r   r   r(   r)   r*   r+   r.   r/   argvr'   r   )r   r5   @py_format3r6   r7   r<   r=   ry   rz   r8   rs   s              rC   3test_v3_fallback_safety_net_argv_built_with_anu_keyr      sf   
*#+%Q (!0"/$(#!C$ 6663;;33688484843384"%((*(****(***************#***#***(*******),1!1111!111111!111!1111111111111111111==/=/////=///////3///3///=//////////////////!!:!!!!:!!!:!!!!!!!!!!!!!!!!!!!sxx8x8x8ssxsxx8x8x8ssx)chhqk)?k))))?k)))?)))k)))))))(SXXa[(>[((((>[(((>((([(((((((rE   c                    dg fd} t        t        j                        }d|d<   t        j                  j                  t        j                  d|       5  t        j                  j                  t        j                  |d      5  t        j                  t        j                  d	t        t        j                  d
dddddddt        j                        }g }|j                  }|}|r|j                  }d}||u}|}|sLddt        j                          v st#        j$                  |      rt#        j&                  |      ndt#        j&                  |      dz  }	|j)                  |	       |rt#        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  }|j)                  |       t#        j,                  |d      i z  }dd|iz  }t/        t#        j0                  |            dx}x}x}x}x}}t        j                  j3                  d      dk(  r#t        j4                  |j                  ddd       ddd       ddd       s{t#        j6                  d       d!z   d"d#t        j                          v st#        j$                        rt#        j&                        nd#iz  }t/        t#        j0                  |            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#        j0                  |            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#        j0                  |            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#        j0                  |            dx}}t8        |v }|st#        j*                  d&|fd.t8        |f      d/t        j                          v st#        j$                  t8              rt#        j&                  t8              nd/d(t        j                          v st#        j$                  |      rt#        j&                  |      nd(d0z  }d1d2|iz  }	t/        t#        j0                  |	            d}t        |v}|st#        j*                  d3|fd4t        |f      d5t        j                          v st#        j$                  t              rt#        j&                  t              nd5d(t        j                          v st#        j$                  |      rt#        j&                  |      nd(d0z  }d1d2|iz  }	t/        t#        j0                  |	            d}y# 1 sw Y   @xY w# 1 sw Y   ExY w)6uS  Production POST-cron wiring at sites #1/#2 actually invokes
    build_anu_owned_callback_request and (when the flag is on) executes
    subprocess.run with the produced argv. We exercise the wiring path's
    helpers directly and verify subprocess.run is called with the ANU-key
    argv — proving auto-registration runs end-to-end.
    z&{"status": "ok", "id": "fake-cron-id"}c                h    j                  t        |              t        j                  dd      S )Nr   r	   )
returncodestdoutstderr)r,   listr   Mock)cmdargskwargscallsfake_responses      rC   	_fake_runz[test_v3b_fallback_safety_net_post_cron_wiring_executes_when_flag_enabled.<locals>._fake_run   s&    T#YyyAmBGGrE   1%COKACDIR_T2621_FALLBACK_AUTO_REGISTERrun)side_effectF)clearztask-2621-v3br}   uI   [task-2621 fallback safety-net · recovery-only · non-blocking] task=v3br   zt2621-fb-v3bzt2621-disp-v3bzt2621-norm-v3bTr   Nz%(py4)s
{%(py4)s = %(py2)s.ok
}r   )r   rg   r
   )z1%(py8)s
{%(py8)s = %(py6)s.argv
} is not %(py11)s)ri   py8r   z%(py13)spy13r   zassert %(py16)sr      )capture_outputtexttimeoutz2fallback safety-net subprocess.run was not invokedz
>assert %(py0)sr_   r   r   r   z%(py1)s in %(py3)sfb_cmdr`   ra   assert %(py5)sr   r   r   )z%(py0)s in %(py2)srM   r   zassert %(py4)srg   r   )z%(py0)s not in %(py2)sr   )dictosenvironr   patchobjectr^   
subprocessrq   rr   r   rk   rp   r   r   r(   r)   r&   r*   r+   r,   r'   r-   r.   r/   r   r   r   rM   )r   envr   r5   r7   r8   @py_assert7r9   ru   ry   @py_format12@py_format14r>   r?   @py_format1r   rs   @py_format4r<   r   r   r   s                       @@rC   Htest_v3b_fallback_safety_net_post_cron_wiring_executes_when_flag_enabledr      s
    =MEH rzz
C36C/0			=33U			R Q	S	6QCC33#/#22 ^"-%5&4(,$<< D 
 	/svv.v.v#((.$.($.......s...s...v....v...($......#...#...(...$..............::>>ABcINN388DtRP'Q Q( FFFFFFFFF5FFF5FFFFF2YF:::8v8v8vv8v8v8vv&((((&(((((((((((((((&(((&(((((((!////!//////!///!////////////////5Q Q Q Qs%   1[.H[!#[.![+	&[..[8c                 ~    t        j                  t              5  t        t               d d d        y # 1 sw Y   y xY wN)pytestraisesrN   rP   r    rE   rC   ?test_v4_assert_independent_anu_fail_closed_on_executor_self_keyr   !  s1    	/	0 H/0FGH H Hs   3<c                 Z   t        dt        t        t        t        dddddt              } | j                  }|t
        k7  }|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                  }t        |v }|st        j                  d|fdt        |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                  |      dz  }dd|iz  }t        t        j                  |            dx}}y)zWhen the callback OWNER key resolves to the executor self-key,
    enforce_callback_owner MUST return FAIL with SELF_COLLECTOR_FORBIDDEN.
    ztask-2621-v4bzt2621-norm-v4bzt2621-fb-v4bzt2621-disp-v4br}   Tr~   )!=)z/%(py2)s
{%(py2)s = %(py0)s.verdict
} != %(py4)sr   r   rf   rh   ri   Nr   )z7%(py0)s in %(py4)s
{%(py4)s = %(py2)s.classifications
}rU   )rV   r   rR   rT   r   r   r&   r'   r(   r)   r*   r+   r.   r/   classificationsrU   )r   r5   r7   ry   rz   s        rC   Btest_v4b_enforce_callback_owner_fail_closed_on_executor_self_ownerr   &  s2    !+,2)!1"0)$(#C ;;';-'''';-''''''3'''3''';''''''-'''-''''''''*':'::#':::::#':::::::#:::#::::::s:::s:::'::::::::rE   c            
     ^   t        t        dt        t        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}||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)zIbuild_anu_owned_callback_request also fail-closes on self-key owner.
    ztask-2621-v4cr}   u   bad — owner is self-keyr   zt2621-fb-v4c)r   r   r   r   r   r   r   r   Fr\   )z*%(py2)s
{%(py2)s = %(py0)s.ok
} is %(py5)sr   r   r   r   N)z,%(py2)s
{%(py2)s = %(py0)s.argv
} is %(py5)s)rX   rW   r   r   r&   r'   r(   r)   r*   r+   r.   r/   r   )r   r5   r6   r7   r<   r=   s         rC   9test_v4c_build_fallback_request_fail_closed_on_self_ownerr   ;  s    +#+(*	C 66U6U?6U336U88t8t8t338trE   c                 `   t        dddd      } | 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                  }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ddd      }|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                  }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)u   Both normal and fallback absent → DISPATCH_CONTRACT_VIOLATION
    (regardless of result_present). result_present only toggles whether the
    recovery watcher is required.
    ztask-2621-v5-aFr   rd   r   rec_no_resultrK   rf   rh   ri   Nr\   r   r   r   r   ztask-2621-v5-bTrec_with_result)rQ   r   rK   r&   r'   r(   r)   r*   r+   r.   r/   r   )	r   r5   r7   ry   rz   r6   r<   r=   r   s	            rC   Ftest_v5_dispatch_contract_violation_when_callback_and_fallback_missingr   O  s"   
 /  %	M ''F'+FFFFF'+FFFFFFF=FFF=FFF'FFFFFF+FFFF+FFFFFFFF**3e3*e3333*e333333=333=333*333e33333330  %	O ))H)-HHHHH)-HHHHHHH?HHH?HHH)HHHHHH-HHHH-HHHHHHHH,,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4rE   c                 |
   g t        fd      } ddddd}| j                  |      }| j                  |      }| j                  |      }|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}}g }
|d   }d}||u }|}|r|d   }d}||u }|}|st        j                  d|fd||f      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}}g }
|d   }d}||u }|}|r|d   }d}||u }|}|st        j                  d|fd||f      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}}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t        f}||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}||u }|slt        j                  d|fd	||f      t        j                  |      t        j                  |      d
z  }dd|iz  }	t        t        j                  |	            dx}x}}y)#u  RecoveryWatcher MUST spawn the independent ANU collector EXACTLY
    once on duplicate observations (idempotency). NOT a fixed-time /
    dead-man trigger — only fires on the DISPATCH_CONTRACT_VIOLATION
    condition (result + normal-missing + fallback-missing).
    c                2    j                  | |f      xs dS )Nspawnedr,   r   keyspawnss     rC   <lambda>zAtest_v6_recovery_watcher_idempotent_single_shot.<locals>.<lambda>r  s    fmmWcN&C&Py rE   spawn_fnztask-2621-v6FTr   r   r\   z%(py1)s is %(py4)sr   rh   ri   Nduplicate_suppressed)z%(py3)s is %(py6)s)ra   ri   z%(py8)sr   )z%(py11)s is %(py14)s)r   r   r   r   r   r   r      rd   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)slenr   r_   r`   ra   ri   assert %(py8)s)z%(py1)s == %(py4)sfixed_time_or_dead_man)rO   maybe_spawnr&   r'   r+   r.   r/   r,   r-   r  r(   r)   r*   rM   )watcherobsr1r2r3r8   r7   rs   ry   rz   r5   @py_assert5r6   r9   r:   r;   @py_format9r>   r?   r@   rA   r   s                        @rC   /test_v6_recovery_watcher_idempotent_single_shotr  j  sD    FPG "#(!	C 
		S	!B			S	!B			S	!Bi= D =D    =D   =   D       H2i=HEH=E!Hb)?&@HDH&@D&HHHH=EHHH=HHHEHHHHHHH&@DHHH&@HHHDHHHHHHHHHHHHHHH2i=HEH=E!Hb)?&@HDH&@D&HHHH=EHHH=HHHEHHHHHHH&@DHHH&@HHHDHHHHHHHHHHHHHHv;!;!;!33vv;!!9=)<==9=====9====9===========&'050'50000'5000'00050000000rE   c                 r  
 g 
t        
fd      } | j                  dddd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}}y)u   When normal callback IS present (CONTRACT_OK), recovery watcher
    MUST be a no-op — proves it's not a progress/fixed-time trigger.
    c                *    j                  | |f      S r   r   r   s     rC   r   zEtest_v6b_recovery_watcher_noop_when_condition_unmet.<locals>.<lambda>  s    fmmWcN&C rE   r   ztask-2621-v6bTFr   r   r\   r   r   rh   ri   Nr   rd   r  r  r   r  r  r   )rO   r  r&   r'   r+   r.   r/   r  r(   r)   r*   )r  resr8   r7   rs   ry   rz   r  r6   r  r   s             @rC   3test_v6b_recovery_watcher_noop_when_condition_unmetr    s    FCG 

&'+ %"		
C y>"U">U"""">U""">"""U"""""""v;!;!;!33vv;!rE   c                     t         dz  dz  t         dz  dz  t         dz  dz  t         dz  dz  t         dz  dz  g} i }| D ]   }|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        j                  |j                               j                         ||j                  <    |j                  } |       }t!        |      }t#        |      }t#        |      }	||	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                  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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
z  }dd|iz  }t        t        j                  |            dx}x}x}x}x}
}	y)u   The wiring is IMPORTS-ONLY — the contract module file digests are
    deterministic across reads. This guards spec §6 byte-0 requirement.
    anu_v3zdispatch_callback_contract.pyrF   zcallback_owner_enforcer.pyz"normal_fallback_callback_helper.pyzcron_dispatch_guard.pyzexecutor_completion_contract.pyzcontract module missing: zC
>assert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}prf   Nrd   )z%(py10)s
{%(py10)s = %(py0)s(%(py8)s
{%(py8)s = %(py1)s(%(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.values
}()
})
})
} == %(py15)s
{%(py15)s = %(py12)s(%(py13)s)
}r  setdigests)
r_   r`   r   rg   ri   r   rb   rc   r   py15zassert %(py17)spy17)r$   existsr&   r   r(   r)   r*   r+   r.   r/   hashlibsha256
read_bytes	hexdigestnamevaluesr  r  r'   )targetsr  r  r5   r7   ry   r  r   ru   @py_assert14@py_assert11@py_format16r@   s                rC   6test_byte_zero_contract_modules_not_modified_by_wiringr%    s.   
 	::
99
AA
55
>>G G Exx:xz:z::6qc:::::::q:::q:::x:::z::::::!..8BBDE >>5>#5s#$53$%5W5%5555%55555535553555555s555s55555575557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5W555W55555555555rE   c                 B	   t         dz  dz  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}}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 )NrF   rG   zutf-8)encodingz:ADDITIVE WIRING site #1 composite (PRE-cron contract gate)r   r   srcr   r   r   z6ADDITIVE WIRING site #1 composite (POST-cron fallback)z<ADDITIVE WIRING site #2 single-team (PRE-cron contract gate)z8ADDITIVE WIRING site #2 single-team (POST-cron fallback)rm   rn   rq   )
r$   	read_textr&   r'   r+   r(   r)   r*   r.   r/   )r(  r8   rs   r   r<   s        rC   'test_wiring_presence_at_both_call_sitesr*    s   :-
8
8'
8
JCGNG3NNNNG3NNNGNNNNNN3NNN3NNNNNNNCJCsJJJJCsJJJCJJJJJJsJJJsJJJJJJJIPISPPPPISPPPIPPPPPPSPPPSPPPPPPPELELLLLELLLELLLLLLLLLLLLLLLL*1*c1111*c111*111111c111c1111111*1*c1111*c111*111111c111c11111114;4;;;;4;;;4;;;;;;;;;;;;;;;;rE   )r2   strr3   r+  )returnNone)___doc__
__future__r   builtinsr(   _pytest.assertion.rewrite	assertionrewriter&   r  importlib.utilr!   r   r   r   pathlibr   unittestr   r   r   resolveparentsr$   r+  pathinsertrD   r   r   _dispatch_existingr   r    r^   r"   r#   rI   r5   r6   r7   r8   r%   r9   r:   r;   r'   r)   r*   r+   r<   r=   r,   r>   r?   r-   r@   rA   r.   r/   r0   r1   !anu_v3.dispatch_callback_contractrJ   rK   rL   rM   rN   rO   rP   rQ    dispatch.callback_owner_enforcerrR   rS   r   rT   rU   rV   (dispatch.normal_fallback_callback_helperrW   rX   r   r{   r   r   r   r   r   r   r   r   r   r  r  r%  r*  r   rE   rC   <module>r?     s  > #     	  
   X ((+u:SXXHHOOAs5z"8 &) .1
 [[__Z0 !g
B'
(!"'# 'M#..@@
]*$'
(:$;#< A 
 VdUd*U/B/I/IUQUU/IQU/UUUUdUUUUUUUUUUUdUUUUUUU/IQUUUUUU/BUUU/BUUU/IUUQUUUUUUUUUUUUUUNN334GHM+CKK
**=9	 	 	 
 , H$044"2")>*0`H
;*(5616.6,<rE   