
    Ֆj+E             
      X   U d Z ddl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Zddl	Z	ddl
Z
ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZMmNZN ddlOmPZPmQZQmRZRmSZS ddlTmUZUmVZV ddlWmXZXmYZY ddlZm[Z[m\Z\m]Z]m^Z^m_Z_m`ZambZbmcZcmdZdmeZemfZfmgZgmhZh ddddddd dg d!g d"g d#g d$d%d&d'gd(d)	Zid*ejd+<   dd,	 	 	 	 	 	 	 	 	 	 	 dd-Zk G d. d/el      Zm ed0d01       G d2 d3             Zn ed0d01       G d4 d5             Zo ej                  d6      Zqd7Zrd8Zs e#d9      Ztd:Zu e       Zvedd;       Zwdd<Zxdd=Zydd>Zzd?d@	 	 	 	 	 	 	 ddAZ{	 	 	 	 	 	 	 	 	 	 ddBZ|	 	 	 	 	 	 ddCZ}ddDZ~	 	 	 	 	 	 	 	 ddEZd dFZddGeBj                  dH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddIZdddddddJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddKZdddddL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddMZddNZddOZddPZ	 	 	 	 	 	 	 	 ddQZ	 	 	 	 	 	 	 	 ddRZddSZ	 	 	 	 	 	 	 	 ddTZ	 	 	 	 	 	 	 	 ddUZ	 	 	 	 	 	 	 	 d	dVZdWZdXZeeegef   Z edYdZ      Zd[ejd\<   e	 	 	 	 d
d]       Zdd^	 	 	 	 	 	 	 	 	 	 	 	 	 dd_Zdd^	 	 	 	 	 	 	 	 	 	 	 	 	 dd`Z	 	 	 	 	 	 	 	 	 	 ddaZddbZddcZdddZddeZdfZdgZddhZddiZddjZddkZddlZddmZddnZddoZddpZddqZddrZddsZddtZddddddu	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddvZ	 	 	 	 	 	 	 	 ddwZ	 	 	 	 	 	 	 	 ddxZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddyZddzZ	 	 	 	 	 	 	 	 dd{Zd d|Zd d}Z	 	 	 	 	 	 	 	 d!d~Z	 	 	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 	 	 	 d#dZ	 	 d$	 	 	 	 	 	 	 d%dZd&dZdZdZdZdZdZdZ eh d      Z eh d      Z eh d      Zd'dZd(dZÐd)dZĐd*dZ	 	 	 	 	 	 	 	 	 	 d+dZd?d	 	 	 	 	 d,dZǐd-dZȐd.dZ	 	 	 	 d/dZ	 	 	 	 d/dZ	 	 	 	 	 	 	 	 d0dZ	 	 	 	 	 	 d1dZ	 	 	 	 	 	 	 	 d2dZ	 	 	 	 	 	 	 	 d3dZ	 	 	 	 	 	 	 	 	 	 	 	 d4dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d5dZ	 	 	 	 d/dZed6d       Z	 	 	 	 	 	 	 	 	 	 	 	 d7dZ	 	 	 	 	 	 	 	 d8dZՐd9dZ	 	 	 	 	 	 	 	 	 	 d:dZ	 	 	 	 	 	 	 	 d;dZ	 	 	 	 	 	 d<dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=dZ	 	 	 	 	 	 	 	 	 	 	 	 d>dZ	 	 	 	 	 	 	 	 	 	 	 	 d>dZ	 	 	 	 	 	 d?dZ	 	 	 	 	 	 d?dZd0d	 	 	 	 	 	 	 d@dZdddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dAdZdddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dAdZdddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dAdZ	 	 	 	 	 	 	 	 dBdZ	 	 	 	 	 	 	 	 dCdZ	 	 	 	 	 	 dDdZdZdddddddZ	 	 	 	 	 	 dEdZ	 	 	 	 	 	 	 	 dFdZ	 	 	 	 	 	 	 	 dGdZdHdZdIdZ	 	 	 	 	 	 	 	 	 	 dJdZ	 	 	 	 	 	 	 	 	 	 dKdZ	 	 	 	 	 	 	 	 	 	 dKdZdd	 	 	 	 	 	 	 dLdÄZdd	 	 	 	 	 	 	 dLdĄZ	 	 	 	 	 	 	 	 	 	 	 	 dMdńZdNdƄZ	 	 	 	 	 	 	 	 dOdǄZ	 	 	 	 	 	 dPdȄZ	 	 	 	 	 	 	 	 	 	 	 	 dQdɄZddddddu	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRdʄZdSd˄ZdTd̄ZdZdZdZdZ ej                  dѫ      Z eh dң      Z eh dӣ      Z  eh dԣ      Z eh dգ      eEz  ZddքZ ed0d01       G dׄ deeef                ZdUdلZdVdڄZdWdۄZ	dXd܄Z
dYd݄ZdZdބZd[d߄Zd\dZ	 	 	 	 	 	 d]dZd^dZ	 	 	 	 d_dZd`dZdadZddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dbdZdd	 	 	 	 	 dcdZdd	 	 	 	 	 	 	 	 	 	 	 dddZdedZ	 	 	 	 	 	 dfdZ	 	 	 	 	 	 	 	 	 	 	 	 dgdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 dhdZ	 	 	 	 didZdd	 	 	 	 	 	 	 djdZdkdZdldmdZedk(  r e  e             y(n  z;Safe customer registry onboarding with explicit activation.    )annotationsN)contextmanager)
ContextVar)MappingProxyType)	dataclass)datedatetimetime	timedeltatimezone)Path)CallableIteratorMapping)local)ValidationError)ZoneInfo)ActivationChecklistBindingsTokenRotationPolicyErrorvalidate_token_rotation_policy)CONSENT_VERSIONAiProcessingConsentAdaptiveRegistrationInputsCustomerRuntimeCustomerTrainingScheduleEntryCustomerRegistryErrorCustomerSpecRegistryDocumentTelegramAddressCustomerRegistryload_customer_registry)PilotKPIJudgementjudge_pilot_kpis)_claims_by_schedule_current_rows_read_fence_read_jsonl_validate_schedule_rows)CustomerScheduleError)OperatorTaskPaymentKindPaymentMethodRecordResult) NUTRITION_READINESS_REASON_CODESNutritionReadinessError!require_nutrition_start_readiness)%apply_nutrition_onboarding_projection!build_legacy_activation_authority%build_nutrition_activation_receipt_v2$customer_nutrition_projection_digestproject_nutrition_document$validate_legacy_activation_authority)atomic_write_private_jsonread_private_jsonvalidate_private_filevalidate_profile_path)CanonicalEventTransaction
EventStore)StaffMembershipEvidenceError"validate_staff_membership_evidence)AdaptiveEventStore append_approved_policy_extensioncanonical_event_recordscanonical_json(customer_policy_from_onboarding_artifactdigestfeature_config_digestinitialize_adaptive_customer_DUAL_COACH_RISK_POLICY_FILE_DUAL_COACH_RISK_POLICY_ID_DUAL_COACH_RISK_POLICY_SCHEMA'_dual_coach_risk_policy_document_digest&_validate_dual_coach_risk_policy_valuez<=2z>2-4z>4)normalelevatedhighz>=7z5-<7z<5)lowmoderaterN   )nonepresentsevere)possiblelimited
impossible)rQ   partialmaterial   pain_overrideexercise_impossible_overriderisk_evidence_unavailable)	weight_change_percentsleep_hoursfatiguepainexercise_feasibilitymeal_deviationscore_thresholdhard_overridesmissing_evidence_reasondict[str, object]_DUAL_COACH_RISK_POLICY_VALUE)approved_at_kstc               B
   t        |      t        us|j                         st        d      t	        t        |             }t        |      t        |t              st        d      t        |      5  t        |      }t        ||      }t        fd|j                  D        d      }|t        d       |j                  d      |j                  j                  d      k7  rt        d      t!        ||||      \  }	}
t#        ||d      }|j$                  }|j'                         rt        d	      |j)                  d
d
d       |j+                         }t+        j,                  |j.                        rC|j0                  t3        j4                         k7  s"t+        j6                  |j.                        dk7  rt        d	      t9        t:              }t<        t>        ||tA        |      dd
|j                  d      tC        |      |	d   |	d   |	d   |
d}tE        |      |d<   tG        |      dz   jI                  d      }|tJ        z  }|j'                         rt        d      |dtJ         dtM        jN                         jP                   dz  }d}	 t3        jR                  |t2        jT                  t2        jV                  z  t2        jX                  z  t2        jZ                  z  t2        j\                  z  d      }t3        j^                  ||       t3        j`                  |       t3        jb                  |      }t+        jd                  |j.                        rR|j0                  t3        j4                         k7  s1|jf                  dk7  s"t+        j6                  |j.                        dk7  rt        d      t3        jh                  |       d}t3        jj                  ||       t3        jR                  |t2        jl                  t2        jn                  z  t2        j\                  z        }	 t3        j`                  |       t3        jh                  |       	 |t3        jh                  |       	 |js                          |cddd       S # t3        jh                  |       w xY w# tp        $ r}t        d      |d}~ww xY w# tt        $ r Y Mw xY w# |t3        jh                  |       	 |js                          w # tt        $ r Y w w xY wxY w# 1 sw Y   yxY w)zDWrite the one owner-approved risk policy through the authority lock.z)dual-coach risk policy version is invalidz3dual-coach risk policy requires a typed owner actorc              3  B   K   | ]  }|j                   k(  s|  y wNcustomer_key.0itemkeys     k/home/cube/projects/richard/.worktrees/nutricoach-v140-impl/dualcoach/profile/checkin_cli/customer_admin.py	<genexpr>z1approve_dual_coach_risk_policy.<locals>.<genexpr>|   !     M0dD4E4E4LT0   Nunknown customer: jsonmodez1dual-coach risk policy owner actor is not currentz%dual-coach risk policy root is unsafeT  parentsexist_okry    owner_digestregistry_digestactivation_receipt_digest)schema_version	policy_idversionpolicypolicy_digestdocument_digestapprovedapproved_byrh   rm   r   r   r   authority_digestr   
utf-8z%dual-coach risk policy path is unsafe..tmp     z%dual-coach risk policy file is unsafez#dual-coach risk policy write failed);typestrstripCustomerAdminError_resolve_profile_rootr   _require_customer_key
isinstancer   profile_authority_lock_resolve_registry_path_read_profile_registrynext	customers
model_dumpowner"_registration_activation_authority_registered_customernutrition_plans_root
is_symlinkmkdirstatS_ISDIRst_modest_uidosgeteuidS_IMODErK   rg   rI   rH   adaptive_digest_registration_kst_timestamprJ   rB   encoderG   uuiduuid4hexopenO_WRONLYO_CREATO_EXCL
O_NOFOLLOW	O_CLOEXECwritefsyncfstatS_ISREGst_nlinkclosereplaceO_RDONLYO_DIRECTORYOSErrorunlinkFileNotFoundError)profile_rootrm   r   owner_actorrh   rootregistry_pathregistryselected	authorityr   runtime
plans_rootroot_statusr   documentencodedpath	temporary
descriptoropened	directoryexcrq   s                          @rr   approve_dual_coach_risk_policyr   i   s`    G}Cw}} !LMM l!34D

-Ck?3 !VWW		%.t4)->Mh00Mt
 $'9#%?@@!!v!.(..2K2K 3L 3
 
 %C  'I-8'
#	# '}dCF11
  "$%LMM5A oo'[001!!RZZ\1||K//0E9$%LMM78UV<3,V4!&11v1>:?K%n5():;)23N)O 0'
  'Nh&W"#!(+d2::7C88??$%LMM19:!DJJL<L<L;MTRR 	 "&
 	bjj(2994r}}Dr||SJ
 HHZ)HHZ XXj)FLL0==BJJL0??a'<</58()PQQHHZ JJJy$'
BKK"..,H2<<,WXI$## %$  " q 
&	%\ # 	U$%JKQTT	U % 	 %$  "$ m 
&	%s   3H(TFR-5R
R- T8S
TR**R--	S6SSS
	STSTT2TT	T	TT	TTTc                      e Zd Zy)r   N)__name__
__module____qualname__     rr   r   r      s    r   r   T)frozenslotsc                  .   e Zd ZU ded<   ded<   ded<   ded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZd ed!<   dZd ed"<   dZded#<   y)$CustomerDraftr   rm   display_nameuser_idchat_idtopic_idr   	starts_onr
   
daily_timeintweekly_weekdaymonthly_daycalories_kcal	protein_gtuple[str, ...]meals   미정primary_goalr   dietary_restrictions	allergiesfood_preferencessupplementsN
str | Nonedigestion_contextzfloat | Nonesleep_goal_hoursrecovery_goaltraining_context
int | Nonecarbohydrate_gfat_gwater_liters)r   r   r   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   rr   r   r      s    LLMON L# ,./.!#I#(*o*#%K%$(z(%)l) $M:$#'j'!%NJ%E:!%L,%r   r   c                      e Zd ZU dZded<   ded<   ded<   ded<   edd       Zedd	       Zedd
       Zedd       Z	y)ActivationResultz,Receipt for one guarded customer activation.r   customerr   r   	data_root
audit_pathc                .    | j                   j                  S rk   r   rm   selfs    rr   customer_idzActivationResult.customer_id       }})))r   c                .    | j                   j                  S rk   r  r  s    rr   rm   zActivationResult.customer_key   r  r   c                .    | j                   j                  S rk   )r   enabledr  s    rr   r
  zActivationResult.enabled  s    }}$$$r   c                    | j                   S rk   )r   r  s    rr   speczActivationResult.spec  s    }}r   N)returnr   r  bool)r  r   )
r   r   r   __doc__r   propertyr  rm   r
  r  r   r   rr   r   r      sj    6O* * * * % %  r   r   z^[a-z0-9][a-z0-9_-]{2,63}$   P   z
Asia/Seoulz.adaptive-authority.lockc              #    K   t        t        |             }|dz  }|j                         rt        d      |j	                  ddd       |j                  d       |t        z  }|j                         rt        d      t        t        dd      }|i }|t        _	        t        |      }|j                  |      }|4|\  }}||d	z   f||<   	 d |d
k(  r|j                  |d       y||f||<   yd}		 t        j                  |t        j                  t        j                   z  t        j"                  z  t        j$                  z  d      }	t        j&                  |	      }
|j)                         }|
j*                  |
j,                  f}t/        j0                  |
j2                        rm|
j4                  t        j6                         k7  sL|
j8                  d	k7  s=t/        j:                  |
j2                        dk7  s||j*                  |j,                  fk7  rt        d      t=        j>                  |	t<        j@                         ||j)                         j*                  |j)                         j,                  fk7  rt        d      |	d	f||<   	 d |j                  |d       t=        j>                  |	t<        jB                         	 |	t        jF                  |	       yy# |d
k(  r|j                  |d       w ||f||<   w xY w# |j                  |d       t=        j>                  |	t<        jB                         w xY w# tD        $ r}t        d      |d}~ww xY w# |	t        jF                  |	       w w xY ww)z@Serialize all profile authority/config writers across processes.dataz6profile authority lock parent symlinks are not allowedTrz   r{   z/profile authority lock symlinks are not allowedlocksNr   r   r   z profile authority lock is unsafez#profile authority lock was replacedz%profile authority lock is unavailable)$r   r   r   r   r   chmod_PROFILE_AUTHORITY_LOCK_FILEgetattr_PROFILE_AUTHORITY_LOCK_STATEr  r   getpopr   r   O_RDWRr   r   r   r   lstatst_devst_inor   r   r   r   r   r   r   fcntlflockLOCK_EXLOCK_UNr   r   )r   r   lock_parent	lock_pathstaterq   heldhandledepthr   r   namedidentityr   s                 rr   r   r     s     !l!34D-K D
 	
 dT>e::I !RSS17DAE}.3%+
i.C99S>Deai(c
	-z		#t$ 	 %e_c
!J!WWII

"R]]2R\\A


 *%!MM6==1V^^,}}

,!#||FNN+u4ELL%,,77$%GHHJ.	)00)//2C2J2JKK$%JKK !_c
	3IIc4 KK
EMM2 !HHZ  "G z		#t$$e_c
6 IIc4 KK
EMM2 S !HIsRS !HHZ  "sg   CM<K# "M<5FM L 6M 
M<#!LM<8L??M 	MMMM M99M<c                h    t        |       t        ust        j                  |       t	        d      | S )Nzcustomer key is invalid)r   r   _CUSTOMER_KEY	fullmatchr   values    rr   r   r   V  s/    E{#!8!8!?!G !:;;Lr   c                    t        |       t        u r| S t        |       t        urt        | d      	 t        j                  |       S # t
        $ r}t        | d      |d }~ww xY w)Nz must be an ISO date)r   r   r   r   fromisoformat
ValueError)r1  
field_namer   s      rr   _require_dater6  \  sp    E{dE{# J</C!DEEO!!%(( O J</C!DE3NOs   A	 		A&A!!A&c                X   | 't        j                  t              j                         S t	        |       t        u r| S t	        |       t         u rS| j
                  $| j                  t              j                         S | j                  t              j                         S t        d      )Ntzinfoz#kst_date must be a date or datetime)	r	   now_KSTr   r   r9  r   
astimezoner   r0  s    rr   _activation_kst_dater=  g  s    }||D!&&((E{dE{h<<===-2244%**,,
B
CCr   Foptionalc                   |r| y t        |       t        ust        t        |       cxk  r	t        k  sn t        | d      | j                         | k7  st        d | D              rt        | d      | S )Nz2 must be an opaque id between 16 and 80 charactersc              3  X   K   | ]"  }t        |      d k  xs t        |      dk(   $ yw)       N)ordro   	characters     rr   rs   z%_require_opaque_id.<locals>.<genexpr>|  s.      %INII7Y4!77s   (*z contains invalid characters)r   r   _OPAQUE_ID_MINlen_OPAQUE_ID_MAXr   r   any)r1  r5  r?  s      rr   _require_opaque_idrK  s  s     EME{#^s5z%S^%S lLM
 	
 {{} %IN% " !J</K!LMMLr   c                   t        |      t        t        |            }t        |      }t        |       }|j	                         s|j                         |k7  rt        d      	 t        |      }t        fd|j                  D        d       }	|	t        d       |	j                  j                         }
|
j                         r-|
j                         r|
j!                  |      rt#        |
|      rt        d      |9t        |      }|j	                         s|j                         |
k7  rt        d      |	S # t        t        t        f$ r}t        d      |d }~ww xY w)N1registry path does not match the profile registryzcustomer registry failed closedc              3  V   K   | ]   }|j                   j                  k(  s| " y wrk   r  rm   rn   s     rr   rs   z'_registered_customer.<locals>.<genexpr>  %     N,$		0F0F#0M,   ))rv   (registered customer data root is invalidz5data root does not match the registered customer root)r   r   r   r   r   resolver   load_runtime_customer_registryr   r   r4  r   r   r   existsis_diris_relative_to_path_has_symlink)r   r   rm   r   r   canonical_registryrequested_registryr   r   r   registered_rootrequested_rootrq   s               @rr   r   r     s_     
-C l!34D/5d%%'%%'+== !TUUM1$7 N(,,NPTG  #5cU!;<<''//1O""$%%'--d3_d3 !KLLi$$&.*@*@*Bo*U$G  N+ "7J7 M !BCLMs   &D? ?E$EE$c                   	 t        |      }| j                  j
                  }|t         j                  u r|||t        d      z   fS ||t        d      z   |t        d      z   fS # t        t        f$ r}t        d      |d }~ww xY w)Nz'payment kind must be initial or renewal   days   7   )r+   	TypeErrorr4  r   planr   INITIALr   )r  kindpayment_kindr   r   s        rr   _payment_periodrh    s    U"4( 		##I{***Y	I24F(FFFYB%77YTVEW9WWW z" U !JKQTTUs   A& &B5BBc                V    | j                   dvs| j                  t        | d      | S )N>   recorded	duplicatez record was not accepted)outcomeevent_idr   )resultlabels     rr   _accepted_recordrp    s0    ~~66&//:Q E7*B!CDDMr   c                  t        | ||d      }|j                  }|j                  st        d      	 t	        j
                  |      }|j                  }|j                         sn|j                         r^|j                         rN|j                  j                         s4|j                  j                         r|j                  j                         st        d      	 |j                          t        ||j                        S # t        t        f$ r}t        d      |d}~ww xY w# t        t         t"        t        f$ r}t        d      |d}~ww xY w)zNJudge the registered pilot customer using only canonical plan and event paths.Nz*customer must be enabled for KPI judgementz.canonical customer event ledger is unavailable)rd  z.canonical customer KPI judgement failed closed)r   r  r
  r   r;   for_customer_runtimer   r4  events_pathr   rU  is_filesequence_pathread_snapshot_readonlyr#   rd  RuntimeErrorUnicodeDecodeError)r   rm   r   r   r  transactionr   rs  s           rr   judge_customer_pilot_kpisrz    s2    #4|TJG<<D<< !MNN/DDWM
 ))K !!#""$$$//1((//1((002 !QRR**,$))<<! Z   <
	" \#5zB  <
	s/   D &D( D%D  D%(EEEc           
        | |j                   j                         |j                  j                         d|j                  |j                  |j
                  d|j                  |j                  d|j                  t        |j                        dd|j                  it        |j                        d}t        t        j                  |dd	             y )
N)r   ends_on)passedrate_percent)r}  score)r}  weekly_minutesr}  )rm   windowr}  checkinsatisfactionoperator_timerenewalfailure_reasonsFTensure_ascii	sort_keys)r   	isoformatr|  r}  checkin_passcheckin_rate_percentsatisfaction_passsatisfaction_scoreoperator_time_passlistweekly_operator_minutesrenewal_validr  printrw   dumps)rm   	judgementpayloads      rr   _print_kpi_judgementr    s    $",,668 ((224
 "",,%::

  1111

  22"9#D#DE
 i556	 9 9:)G, 
$**W5D
ABr   I )r   
amount_krwmethodc          
     x   t        | |||      }
|
j                  }t        |      t        us|dk7  rt	        d      	 t        |      }t        ||	      \  }}}t        |d      }t        |d      }t        |d      }||k7  s||k7  rt	        |j                   d      ||kD  rt	        d      	 t        j                  |
      j                  ||||||||j                  	      }t#        |d      S # t        t        f$ r}t	        d      |d }~ww xY w# t        t         t        f$ r}t	        d
      |d }~ww xY w)Nr  z)payment amount must be exactly 150000 KRWz$payment method must be bank_transferpaid_onperiod_start_onperiod_end_onz6 payment period must match the canonical customer planz8payment paid_on must not be after the payment period end)r  r  r  r  r  rf  rd  zpayment record was rejectedpayment)r   r  r   r   r   r,   rc  r4  rh  r6  r1  r<   for_registeredrecord_paymentrd  r   rw  rp  )r   rm   r   r   r  r  r  r  r  rf  r   r  payment_methodr   rg  expected_startexpected_endpaidperiod_start
period_endrn  s                        rr   r  r    sq    #4|YOG<<DJs"jF&: !LMMR&v. 2At1L.L.,),D 2CDL}o>J~%|)C !!""XY
 	
 l F
 	
I**73BB!($! C 	
 FI..7 z" R !GHcQR2 \:. I !>?SHIs/   C1 /6D 1D DDD9(D44D9)r   score_1to10collected_onnoter  collection_datec                  t        | |||      }	||n|}
t        |
      t        usd|
cxk  rdk  st        d       t        d      ||||k7  rt        d      ||n|}|t        d      t	        |d      }|*t        |      t
        ust        |      dkD  rt        d      	 t        j                  |	      j                  ||
||	      }t        |d      S # t        t        t        f$ r}t        d
      |d }~ww xY w)Nr   
   z2satisfaction score must be an integer from 1 to 10z%satisfaction score was supplied twicez%satisfaction collected_on is requiredr  i  z=satisfaction note must be text no longer than 2000 characters)r  r  r  z satisfaction record was rejectedr  )r   r   r   r   r6  r   rH  r<   r  record_satisfactionr   rw  r4  rp  )r   rm   r   r   r  r  r  r  r  r   selected_scoreselected_date	collectedrn  r   s                  rr   r  r  .  s6    #4|YOG$/$;[NN3&a>.GR.G !UVV /H !UVV5#49M !HII$0$<L/M !HIIm^<IT$Zs2c$i$6F K
 	
N**73GG&"	 H 
 FN33 \:. N !CD#MNs   ((C D0C<<D)r   	work_datework_onsupersedes_entry_idc       	   	     x   t        | |||      }t        |d      }t        |d      }t        |
dd      }|||k(  rt        d      t        |      t        usd|cxk  rdk  st        d	       t        d	      	 t        |      }||n|	}|t        d      t        |d      }||	||	k7  rt        d      	 t        j                  |      j                  |||||||      }t        |d      S # t        t        f$ r}t        d
      |d }~ww xY w# t        t        t        f$ r}t        d      |d }~ww xY w)Nentry_id
attempt_idr  Tr>  z0operator-time correction must use a new entry_idr   iX  z6operator-time minutes must be an integer from 1 to 600zoperator-time task is invalidz#operator-time work_date is requiredr  z*operator-time work date was supplied twice)r  r  minutestaskr  r  z!operator-time record was rejectedzoperator-time)r   rK  r   r   r   r*   rc  r4  r6  r<   r  record_operator_timer   rw  rp  )r   rm   r   r   r  r  r  r  r  r  r  r   entryattemptreplacementoperator_taskr   r  workedrn  s                       rr   r  r  T  s    #4|YOGx4E \:G$K
 ;%#7 !STTG}CqG':s': D
 	
 (; D
 	
K$T* "+!6IGM !FGG=+6F!4g9M !MNNO**73HH + I 
 FO44) z" K !@AsJK$ \:. O !DE3NOs0   ;C1 :+D 1D DDD9(D44D9c                v    t        t        j                  | j                  | j                  dddd             y )N)receiptstatusT,:)r  
separatorsr  )r  rw   r  rm  rl  )rn  s    rr   _print_record_receiptr    s/    	

6>>B!		
r   c                n    t        t        |             5  t        | |      cd d d        S # 1 sw Y   y xY wrk   )r   _registry_profile_root_register_customer_locked)r   drafts     rr   register_customerr    s&    	 6t <	=(u5 
>	=	=s   +4c                   t        |       }t        fd|j                  D              rt        dj                         t        fdt        dd      D              }j                  j                  dj                  j                  j                  dj                  j                  j                  dj                  j                  j                   j"                  j$                  j&                  j(                  j*                  j,                  d		j.                  d
|dd}t1        j2                  |      }	 |j5                  |       |j9                  d      }|d   }t;        |t<              st        d      |j?                  |j9                  d             tA        | tC        j2                  |             |S # t6        $ r}t        d      |d}~ww xY w)z>Append one disabled, validated customer draft for plan review.c              3  P   K   | ]  }|j                   j                   k(    y wrk   rl   )ro   rp   r  s     rr   rs   z,_register_customer_locked.<locals>.<genexpr>  s%     
R?Qt4 2 22?Qs   #&zcustomer already exists: c           	   3     K   | ]J  }|j                   j                  j                  j                  j                  j
                  d  L yw))weekr   r   meal_structurer   r   r   N)r   r   r   r   r   r   )ro   r  r  s     rr   rs   z,_register_customer_locked.<locals>.<genexpr>  sR       !D "00#kk#22[[!..	
 !s   AAr      Fr   r   r   )r   r   r   )	r   r   r   r   r   r   r   r   r   nutrition_90_training_10)r   focusweeks)rm   r   r
  telegramscheduleprofilerd  )pilot supports only one external customerNrw   rx   r   z!registry customers must be a list)"_readrJ  r   r   rm   tupleranger   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   model_validate validate_pilot_customer_boundaryr   r   r   r  append_writer   )	r   r  r   r  customer_payloadr   r   r  r   s	    `       rr   r  r    s   T{H

Rx?Q?Q
RR #<U=O=O<P!QRR  !RL E ****}}}}
  **#22 ,,
 "..$)$>$> % 6 6 ,,!&!8!8 % 6 6"00 % 6 6

 /
3> **+;<HW11(; !!v!.G$Ii& !DEEX((f(56
4!009:O ! W !LMSVVWs   G 	G*G%%G*c               r    t        t        |             5  t        | ||      cd d d        S # 1 sw Y   y xY w)Nr
  )r   r  _set_customer_enabled_locked)r   rm   r
  s      rr   set_customer_enabledr    s*     
  6t <	=+D,P 
>	=	=s   -6c                  |rt        d      t        |       }d}g }|j                  D ]I  }|j                  |k(  r|j	                  ddi      n|}|j                  |k(  r|}|j                  |       K |t        d|       |j                  d      }|D cg c]  }|j                  d       c}|d	<   t        | t        j                  |             |S c c}w )
zIDisable a customer; enabling is available only through activate_customer.z5direct enabling is not allowed; use activate_customerNr
  Fupdaterv   rw   rx   r   )
r   r  r   rm   
model_copyr  r   r  r   r  )	r   rm   r
  r   r   r   r   updatedr  s	            rr   r  r    s     C
 	
 T{H$(H$&I&& $$4 	5'9: 	
   L0H! '  #5l^!DEE!!v!.GMVWYH//V/<YWGK
4!009:O Xs   Cc                    t        | |d      S )z?Disable a customer without evaluating activation prerequisites.Fr  )r  )r   rm   s     rr   disable_customerr    s    lEBBr   c                p    t        t        |             5  t        | ||      cd d d        S # 1 sw Y   y xY wrk   )r   r  _set_customer_ai_consent_locked)r   rm   consents      rr   set_customer_ai_consentr    s*     
  6t <	=.t\7K 
>	=	=s   ,5c                   t        |       }d }g }|j                  D ]I  }|j                  |k(  r|j                  d|i      n|}|j                  |k(  r|}|j	                  |       K |t        d|       |j                  d      }|D cg c]  }|j                  d       c}|d<   t        | t        j                  |             |S c c}w )Nai_processing_consentr  rv   rw   rx   r   )
r  r   rm   r  r  r   r   r  r   r  )	r   rm   r  r   r   r   r   r  r  s	            rr   r  r    s     T{H$(H$&I&& $$4 (?'IJ 	
   L0H! '  #5l^!DEE!!v!.GMVWYH//V/<YWGK
4!009:O Xs   Cc                  t        t        |             }t        |      |t        d      t	        |      }t        d|t              }t        |      5  t        |      }t        ||       t        ||      }t        fd|j                  D        d      }|t        d       |j                  d|d      }	|	|k(  r|cddd       S |j                  d	
      }
|j                  D cg c]5  }|j                  k(  r|	j                  d	
      n|j                  d	
      7 c}|
d<   t!        j"                  |
      }t%        ||       t        fd|j                  D              cddd       S c c}w # 1 sw Y   yxY w)z?Atomically disable a customer and record AI consent withdrawal.Nzkst_date is requiredFgrantedrecorded_onnotice_versionc              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z$withdraw_customer.<locals>.<genexpr>3  rt   ru   rv   )r
  r  r  rw   rx   r   c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z$withdraw_customer.<locals>.<genexpr>I  s"     X%;Tt?P?PTW?WD%;ru   )r   r   r   r   r=  r   r   r   r   "_recover_activation_journal_lockedr   r   r   r  r   rm   r   r  r  )r   rm   kst_dater   withdrawal_daywithdrawn_consentr   r   r   	withdrawnr  rp   transitionedrq   s                @rr   withdraw_customerr    s    !l!34D

-C !788)(3N+"&
 
 	%.t4*4?)->Mh00M
 $'9#%?@@'' ): ( 
	  # 
&	%$ %%6%2
 !**	 
 +   C'   f -f-. +	 
 (66w?}l+X\%;%;XX7 
&	%& 
' 
&	%s&   A.E; E;.:E6(AE;6E;;F)token_rotatedmissend_test_passedprovider_terms_checkedwithdrawal_deletion_docretention_backup_docmanual_fallback_doc)personalrichardr   )activation_historical_artifact_classifierdefaultz:ContextVar[_HistoricalActivationArtifactClassifier | None]*_activation_historical_artifact_classifierc              #     K   t        |       st        d      t        j                  |       }	 d t        j	                  |       y# t        j	                  |       w xY ww)zHScope a caller-authenticated historical-artifact exception to one audit.z,G1 historical artifact classifier is invalidN)callabler   r  setreset)
classifiertokens     rr   %_activation_historical_artifact_auditr  [  sR     
 J !OPP6:::FE@288?288?s   ,A#A	 A#	A  A#r  c          	         t        t        |             }t        |      5  t        ||||||      cd d d        S # 1 sw Y   y xY w)Nr  )r   r   r   _activate_customer_locked)r   r   r  checklist_evidence_pathstaff_membership_evidence_pathr  r   s          rr   activate_customerr  i  sA     !l!34D		%(#*
 
&	%	%s	   ;Ac                  t        |       }t        |      }t        ||      }t        fd|j                  D        d      }	|	t        d       	 t        t        |      |||	j                  j                        }
t        ||       t        ||      }t        |||       	 t        ||      }t        fd|j                  D        d      }|t        d       |j$                  }|j&                  rt        d       t)        |      }|j*                  j,                  }|t/        d	      z   }||cxk  r|k  s0n t        d
|j1                          d|j1                          d      |j3                  ddi      }	 |j5                  |       	 |j7                          	 |j9                  |       t;        t        |      ||j<                  ||       	 t?        |      }tC        |j<                  |jD                        }tG        |      5  tI        |t        |      |j<                  ||       ddd       tK        |jD                  d   tM        tO        |jD                              tQ        |jS                  d            |jD                  d         }	 tU        |j<                         tW        |j<                  dz  dz        }|jY                  d      dus|jY                  d      durt#        d      t[        |j<                  |j\                  |j*                  j,                  |jD                  d          |jS                  d      }|j                  D cg c]5  }|j`                  k(  r|jS                  d      n|jS                  d      7 c}|d!<   	 tc        jd                  |      }|jg                         }ti        |      }tk        |      }tm        jn                         jp                  }ts        |      }tu        jv                  tx        jz                        j1                         } t}        ||j<                  t        |      ||| ||
#	      }!i d$d%d&d'd(dd)|d*d+t        |j                               d,t        |j<                  j                               d-t        t        |      j                               d.t        |j                               d/|d0ts        |      d1tM        |!      d2t        |      d3|jS                  d      d4| d5| d6||
}"t        |      }#t        |#|"       	 t        |||j<                  t        |      ||| ||
#
       t        ||       	 ts        t        |            }$t        ||tM        |!      7      }%|$|k7  s|%d9k7  rt        d8      t        |#i |"d:dtu        jv                  tx        jz                        j1                         d;       t        fd=|j                  D              }'t        |'||j<                  |      S # t        $ r}t        t        |            |d}~ww xY w# t        t         t"        f$ r}t        d      |d}~ww xY w# t        t"        f$ r}t        d      |d}~ww xY w# t        t"        f$ r}t        d      |d}~ww xY w# t        t"        f$ r}t        d      |d}~ww xY w# t@        $ r}t        t        |            |d}~ww xY w# 1 sw Y   xY w# t         t^        t"        f$ r}t        d       |d}~ww xY wc c}w # t        t"        f$ r}t        d"      |d}~ww xY w# t        $ r}t        d8      |d}~ww xY w# t        $ rH}	 t        |||||#|"       n# t
        $ r}&|&|d}&~&ww xY wt        |t
              r t        d<      |d}~ww xY w)>zCEnable one customer only after the complete G1-G5 activation guard.c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   ro   rp   r  s     rr   rs   z,_activate_customer_locked.<locals>.<genexpr>  s"     `;$t?P?PT_?_;ru   Nrv   )r   r   r  customer_user_idz%registry failed activation validationc              3  V   K   | ]   }|j                   j                  k(  s| " y wrk   rO  r  s     rr   rs   z,_activate_customer_locked.<locals>.<genexpr>  s%     V,$		0F0F+0U,rQ  zcustomer is already enabled: r^  r_  z+activation is outside the KST plan window [z, ]r
  Tr  r  z.G2/G3 customer activation prerequisites failedz&G4 customer identity validation failedr  rw   rx   input_reconciliationrm   readiness_receipt_digestreadiness_bundle_digestcustomer_projection_digestinput_reconciliation_digestnutrition-plansfeature-epoch.json
activationFdeliveryz+adaptive runtime flags must remain disabledadjustment_policy)r   plan_starts_onexpected_digest#adaptive runtime preparation failedr   z%activation registry transition failedtransaction_idregistry_sha256recorded_atnutrition_activation_receiptmembership_bindingsr      r'  preparedrecovery_requiredr0  r  r   r   r  r  r1  previous_registry_sha256audit_record_sha256previous_audit_sha256previous_registry
created_atprepared_atr3  r0  r9  z-activation side effects could not be verifiedrR   	committed)r'  r7  committed_atz-activation transaction could not be committedc              3  @   K   | ]  }|j                   k(  r|  y wrk   rl   r  s     rr   rs   z,_activate_customer_locked.<locals>.<genexpr>f  s'      0D+ 	0   )Kr   r   r   r   r   r   r>   r   r  r   r=   r   _recover_activation_journal%_validate_enabled_activation_receiptsr!   r   r   r4  r  r
  r=  rd  r   r   r  r  r  'validate_local_activation_prerequisitesvalidate_enabled_customer_read_activation_checklistr   r0   r/   -_authenticated_onboarding_artifact_classifierdigestsr  _validate_activation_data_rootr3   _json_digestdictr4   r   rF   r8   r  %_promote_onboarding_adjustment_policyr   rc  rm   r   r  
read_bytes_activation_audit_path_read_optional_bytesr   r   r   _document_fingerprintr	   r:  r   utc_build_activation_audit_recordrS  _bytes_digest_activation_journal_path_write_activation_journal_append_activation_auditr  r  _activation_audit_receipt_state	Exception_rollback_activationr   r   )(r   r   r  r  r  r  r   r   pre_activation_documentpre_activation_specr4  r   r   r   r   r  activation_daywindow_start
window_endenabled_specreadiness_auditr  r3  feature_epochr  rp   activated_documentregistry_beforer  audit_beforer0  r1  r2  audit_recordpendingjournal_pathwritten_registry_sha256written_audit_staterollback_error	activateds(     `                                     rr   r  r  ~  s    !.D*40M4]DI`1;;` " #5k]!CDD	4@/0'#099AA
  m4%mT:H)$xHS)-> V(,,VG  #5k]!CDD<<D|| #@!NOO)(3N99&&L	r 22J>7Z7 &&()J,@,@,B+C1F
 	
 ??9d*;?<LW11,?446T**<8 $%4;D+N ?J 
/z	:&O	
 
; $I !0!8!8!C ,T/2I2I-J K#GOOO($
 %4$;$;<R$S$ Q$W%6%67) 114HH
 l+58  ,E9JKK-..99..+334GH		
 !!v!.G
 &&	 'D + 	V,__&_)	* '	GKS-<<WE $..0O'-J'
3LZZ\%%N+,>?O,,x||,668K1$%%'%A/
L1 	U 	.	
 	{ 	]2245 	S**2245 	"3t,C'D'L'L'N#O 	c*,,./ 	? 	#$9($C 	|L9 	 |!< 	X00f0= 	k  	{!" 	'(D#$ %G( ,D1LlG47 ())+#)E 3	
 	}01
	&;E-<P&Q#"A-$0$># $6"i/$%TUU!$%* (X\\ : D D F		
6  &00 I
 I}g6G6GTTe ( 4 S*34 "7J7 S !HIsRS, ":. W !LMSVVW
 ":.  <
	 ":. T !IJPSST # 4 S*34 
;	:D Y
+ Q !FGSPQ ":. S !HIsRS@  	$?	$  
	*  " 	* c)	*c-. ;
	s.  -X: 'Y  Z Z+ +[ [1 #\!B%\$ &:]'] 4^ +]4 ,A^ :	YYY Z4Z  ZZ(Z##Z(+[:[[[.[))[.1	\:\\\!$]	8]]	]1 ],,]14	^=^		^^ 	_"^,+_,	^=5^88^== __"c               |   | dz  dz  }t        |      }|j                  d      }|j                         D ci c]  \  }}|dk7  s|| }	}}t        |t              r||k7  st        |	      |k7  rt        d      t        |	      }
|
j                  |k7  rt        d      d|d   |d|j                  d	
      |d   |	d}| dz  }|dz  }t        |      }||k(  ry |dddk7  rt        d      dD ]7  }||z  }t        |       |j                         j                  s.t        d       t        ||       y c c}}w )Nnutrition-onboardingzadjustment-policy-v1.jsonrD   z,onboarding adjustment policy digest is stalez+onboarding adjustment policy start is stale1.0policy_versionTrw   rx   effective_at_kst)r   r   rD   r   r   rh   r   r'  policy.jsonF)r   r
  z8adaptive policy promotion conflicts with existing policy)zpolicy-revisions.jsonlzpolicy-audit.jsonlz9adaptive policy promotion conflicts with revision history)r8   r  itemsr   r   rK  r4  rC   r   r   r9   r   st_sizer7   )r   r   r,  r-  source_pathsourcesource_digestrq   r1  policy_valuer   promotedr   policy_pathcurrentfilenamerh  s                    rr   rM  rM  n  sq    447RRK{+FJJx(M17S:33(?CJLS}c*O+%6GHH5lCF>)FGG*+''V'4!"45H ..J},K,G(Uu==STTD!H,l+&&K 	 E k84C Ts   D8D8c                    t        |       }|j                         s |j                         r|j                         st	        d      |j                         }|j                         rt	        d      |S )Nz*profile root must be an existing directoryz%profile root symlinks are not allowed)r   r   rU  rV  r   rS  )r   	candidateresolveds      rr   r   r     se    \"IY%5%5%7y?O?O?Q !MNN  "H !HIIOr   c                    | dz  dz  | dz  f}|D ]]  }|j                         s|j                         rt        d      |j                         }|j	                  |       st        d      |c S  t        d      )Nr   zregistry.json!registry symlinks are not allowedregistry escapes the profilez#profile registry.json was not found)rU  r   r   rS  rW  )r   
candidatesr~  r  s       rr   r   r     s    {"_4&J  	##%()LMM ((*H**<8()GHHO   B
CCr   c                B   t        |       }	 |j                         }|j
                  j                  dk(  r|j
                  j
                  n|j
                  }t        |      }t        |      |k7  rt	        d      |S # t        t        f$ r}t	        d      |d }~ww xY w)Nzregistry path is invalidr   rM  )	r   rS  r   rw  r   parentnamer   r   )r   	requestedr  r   root_candidater   s         rr   r  r    s    T
IF$$&
 ??;. 	__ 
 !0Dd#x/ !TUUK \" F !;<#EFs   A> >BBBc                p    | j                         }|j                  |      st        d      t        |       S )Nr  )rS  rW  r   r  )r   r   r  s      rr   r   r     s1    ||~H""<0 !?@@;r   z customer-activation-journal.jsonzcustomer-activation-audit.jsonlc                    | dz  t         z  S Nr  )_ACTIVATION_JOURNAL_FILEr   s    rr   rU  rU    s    & #;;;r   c                    | dz  t         z  S r  )_ACTIVATION_AUDIT_FILEr  s    rr   rO  rO    s    & #999r   c                    t        j                  | ddd      j                  d      }t        j                  |      j                         S )NFTr  r  r  r  r   )rw   r  r   hashlibsha256	hexdigest)r1  r  s     rr   rK  rK    sD    jj	
 fWo  >>'",,..r   c                N    | y t        j                  |       j                         S rk   )r  r  r  r0  s    rr   rT  rT    s"    }>>% **,,r   c                8    t        | j                  d            S )Nrw   rx   )rK  r   )r   s    rr   rQ  rQ    s    +++899r   c                    | j                   }|j                         rt        d      |j                  ddd       |j	                  d       y )Nz6activation persistence parent symlinks are not allowedTrz   r{   )r  r   r   r   r  )r   r  s     rr   _ensure_private_parentr    sG    [[F D
 	
 LL5L9
LLr   c                    t         j                  t        t         dd      z  }t        j                  | |      }	 t        j                  |       t        j
                  |       y # t        j
                  |       w xY w)Nr   r   )r   r   r  r   r   r   )r   flagsr   s      rr   _fsync_directoryr    sS    KK'"mQ77Eu%J

s   A% %A<c                   | j                         rt        d      t        |        | j                  d| j                   dt        j                         j                   d      }	 |j                  d      5 }|j                  d       |j                  |       |j                          t        j                  |j                                d d d        t        j                  ||        | j                  d       t!        | j"                         y # 1 sw Y   FxY w# t$        $ r" 	 |j'                           # t$        $ r Y  w xY ww xY w)N/activation persistence symlinks are not allowedr   r   wbr   )r   r   r  	with_namer  r   r   r   r   r  r   flushr   r   filenor   r  r  r   r   )r   contentr   r)  s       rr   _atomic_write_bytesr    s    !RSS4 499+Qtzz|/?/?.@EFI^^D!VOOE"LL!LLNHHV]]_%	 "
 	

9d#

5% "!  	 	  		sJ   &D 7ADAD DD 	E	(D98E	9	EE	EE	c                    | j                         rt        d      | j                         sy | j                         st        d      	 | j	                         S # t
        $ r}t        d      |d }~ww xY w)Nr  z2activation persistence path must be a regular filez(activation persistence could not be read)r   r   rU  rt  rN  r   r   r   s     rr   rP  rP    sm     !RSS;;=<<> !UVVV   V !KLRUUVs   	A 	A3"A..A3c                    |Q| j                         rt        d      | j                         r%| j                          t	        | j
                         y t        | |       y )Nr  )r   r   rU  r   r  r  r  )r   previouss     rr   _restore_bytesr  $  sI    ??$%VWW;;=KKMT[[)h'r   c                z   | j                         rt        d      | j                         sy | j                         st        d      	 t	        j
                  | j                  d            }t        |t              st        d      |S # t        t        t        j                  f$ r}t        d      |d }~ww xY w)Nz+activation journal symlinks are not allowedz)activation journal must be a regular filer   encodingzactivation journal is invalidz(activation journal must be a JSON object)r   r   rU  rt  rw   loads	read_textr   rx  JSONDecodeErrorr   rL  )r   r  r   s      rr   _read_activation_journalr  /  s     !NOO;;=<<> !LMMK**T^^W^=> gt$ !KLLN	 ')=)=> K !@AsJKs   	%B B:)B55B:c                &   |j                  d      }|dvrt        d      	 t        j                  |ddd      j	                  d      d	z   }t        | |       y # t        t        t        f$ r"}t        |t              r t        d
      |d }~ww xY w)Nr'  >   r6  	abandonedr?  z#activation journal state is invalidFT   )r  r  indentr      
z)activation journal could not be persisted)
r  r   rw   r  r   r  r   rc  r4  r   )r   r  r'  r  r   s        rr   rV  rV  ?  s    KK E:: !FGGWJJ"	
 fWo 	 	D'*Y
+ Wc-. !LMSVVWs   7A B.BBc                L   t        | t              r| st        d| d      t        |       }|j	                         rt        d| d      	 |j                         }|j                  |      st        d| d      |S # t        t        f$ r}t        d| d      |d }~ww xY w)Nzactivation journal  is invalidz symlinks are not allowedz escapes the profile)	r   r   r   r   r   rS  r   rw  rW  )r1  r   ro  r~  r  r   s         rr   _resolve_journal_pathr  T  s    eS! #6ug[!IJJUI #6ug=V!WXXT$$& ""<0 #6ug=Q!RSSO	 \" T #6ug[!IJPSSTs   A? ?B#BB#r/  c          	     4   d|dt        |       t        |      t        |j                               |xs1 t        j                  t        j
                        j                         d}	|||	d<   |||	d<   |t        |      |	d<   ||	j                  |       |	S )Ncustomer_activationT)eventr  r
  r   r   r  r2  r0  r1  r3  )	r   rS  r	   r:  r   rR  r  rL  r  )
r   r   r  checklist_pathr0  r1  r2  r3  r4  records
             rr   rS  rS  c  s     '"]+^#&~'='='?#@"Lhll8<<&@&J&J&L!F !#1 "$3 !#/156R1S-.&)*Mr   c                v    | |t        |      j                  t        |      xs t        |      j                  dS )N)	component	operation
error_typeerror)r   r   r   )r  r  r  s      rr   _activation_failure_detailr    s7     5k**U3tE{33	 r   c                d   |j                  d      }t        |t              r!|D cg c]  }t        |t              s| c}ng |z   }i |dd|t	        j
                  t        j                        j                         d}	 t        | |       y c c}w # t        $ r}t        d      |d }~ww xY w)Nrecovery_failuresr6  T)r'  r7  r  recovery_recorded_atzHactivation recovery required and failure evidence could not be persisted)r  r   r  rL  r	   r:  r   rR  r  rV  rY  r   )rh  rg  failuresprior_failuresrp   combined_failuresr  r   s           rr   $_record_activation_recovery_requiredr    s    
 [[!45N nd+ )C.$JtT,B.C	

!. (X\\ : D D FG!,8 	D   V
	s"   BBB 	B/B**B/c           	     n   g }d }	 t        | |       	 t        |       }	|	|k7  r)|j                  t	        dd|xs t        d                   d }
	 t        ||       	 t        |      }||k7  r)|j                  t	        dd|
xs t        d                   |rt        |||       t        d      	 t        |i |ddt        j                  t        j                        j                         d	       y # t        $ r}|}Y d }~d }~ww xY w# t        $ r}d }	|xs |}Y d }~d }~ww xY w# t        $ r}|}
Y d }~d }~ww xY w# t        $ r}t               }|
xs |}
Y d }~d }~ww xY w# t        $ rB}t	        d
d|      }	 t        |||g       n# t
        $ r}||d }~ww xY wt        d      |d }~ww xY w)Nr   restorez1registry bytes do not match the prepared snapshotauditz.audit bytes do not match the prepared snapshotzactivation recovery requiredr  F)r'  r7  abandoned_atjournalzrecord abandoned state)r  rY  rP  r  r  r   objectr  rV  r	   r:  r   rR  r  )r   rd  r  re  rh  rg  r  registry_errorr   registry_afteraudit_erroraudit_afterterminalization_failureevidence_errors                 rr   rZ  rZ    s    &(H+/N}o6/-m< (& %G			
 )-Kz<0)*:6 l"& X%&VW		
 ,\7HM !?@@J!$%* (X\\ : D D F		
Y    /'.3/$    )h!(S)6  J"<$#

	*0()
 " 	* c)	* !?@cIJs   C4 D D+ E 0AE) 4	D	=DD		D(D##D(+	E 4D;;E 	E&E!!E&)	F42F/ FF/	F FF  F//F4c                z   | j                         rt        d      | j                         sy | j                         st        d      	 | j	                         j                  d      }g }d}|D ]_  }	 t        j                  |j                  d            }t        |t              r|j                  d      |k(  rd}O|j                  |       a |rt!        | d	j#                  |             y y # t        $ r}t        d      |d }~ww xY w# t        t        j                  f$ r |j                  |       Y w xY w)
N.activation audit path symlinks are not allowed,activation audit path must be a regular fileTkeepends"activation audit could not be readFr   r0  r   )r   r   rU  rt  rN  
splitlinesr   rw   r  decoderx  r  r  r   rL  r  r  join)r   r0  linesr   retainedchangedliner  s           rr   _remove_activation_audit_recordr    s%    !QRR;;=<<> !OPPP!,,d,; HG	ZZG 45F fd#

3C(D(VG  D#((8"45   P !EFCOP #D$8$89 	OOD!	s*   	 C0 3$D0	D
9DD
*D:9D:c                  | j                         rt        d      | j                         sy| j                         st        d      	 | j	                         j                         }d}|D ]  }|j                         s	 t        j                  |j                  d            }t        |t              st        d	      |j                  d
      |k7  ri|dz  }|j                  d      dust!        |      |k7  st        d       |dkD  rt        d      |dk(  rdS dS # t        t        f$ r}t        d      |d}~ww xY w# t        t        j                  f$ r}t        d      |d}~ww xY w)zBReturn present/absent, rejecting any ambiguous audit ledger state.r  absentr  r  Nr   r   zactivation audit is invalidz"activation audit record is invalidr0  r   r
  Tz%activation audit receipt is ambiguousz&activation audit receipt is duplicatedrR   )r   r   rU  rt  rN  r  r   rx  r   rw   r  r  r  r   rL  r  rK  )r  r0  r9  r  r   matchingr  r  s           rr   rX  rX    su     !QRR !OPPP%%'224 Hzz|	MZZG 45F &$'$%IJJ::&'>9AJJy!-F#'::$%LMM! " !| !IJJ A9383- '( P !EFCOP #D$8$89 	M$%BCL	Ms0   	D   $E E /D;;E E-E((E-c                    t        t        |             }t        |      }t        |      j                         |k7  rt	        d      t        |      5  t        ||       d d d        y # 1 sw Y   y xY w)N6activation journal registry does not match the profile)r   r   r   rS  r   r   r  )r   r   r   rY  s       rr   rC  rC  ?  sd     l!34D/5M""$(:: D
 	
 
 	%*41CD 
&	%	%s   A((A1c                	   t        |       t              y j                  d      }j                  d      dvrt        d      |dv ry |dvrt        d      t	        j                  d      | d      }||j                         k7  rt        d	      j                  d
      }t        |t              st        d      	 t        j                  |      }j                  d      }j                  d      }j                  d      }	j                  d      t        |t              r8|r6t        |t              r&|r$t        |	t              r|	rt        t              rst        d      t        fd|j                  D        d       }
|
|
j                   rt        d      t#        |      }j                  d      }|||k7  rt        d      ||k(  rt        d      t	        j                  d      | d      }|t%        |       j                         k7  rt        d      t	        j                  d      | d      }| dz  dz  z  j                         }||k7  rt        d      	 	 	 	 	 	 	 	 d1fd	 t'        |      }t#        |      }	 t+        |||	       }d2fd"}|k(  rMd#k(  rH|j-                         r|j/                         rt1        ||       r dd$t        d%              |d&       y ||k(  rd'k(  r	 |d(       y ||k(  rod'k(  rj	 t3        ||       	 t#        t'        |            }|k7  r dd*t        d+             	 t+        |||	       }|d'k7  r d!d,t        d-              |d(       y ||k(  rBd#k(  r=	 t5        ||       	 t+        |||	       }|d'k7  r d!d*t        d-              |d(       y  d.d/t        d0             y # t        t        t        f$ r}t        d      |d }~ww xY w# t(        $ r} dd|       Y d }~d }~ww xY w# t(        $ r} d!d|       Y d }~d }~ww xY w# t(        $ r} dd)|       Y d }~;d }~ww xY w# t(        $ r} dd*|       Y d }~Gd }~ww xY w# t(        $ r} d!d,|       Y d }~Ad }~ww xY w# t(        $ r} d!d)|       Y d }~*d }~ww xY w# t(        $ r} d!d*|       Y d }~<d }~ww xY w)3Nr'  r      r   r  r5  z%activation journal version is invalid>   r  r?  >   rg  r6  r7  z'activation journal has an unknown stater   r  r;  z+pending activation has no rollback registryz/pending activation rollback registry is invalidr0  r1  r9  r  z%pending activation journal is invalidc              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   r  s     rr   rs   z5_recover_activation_journal_locked.<locals>.<genexpr>z  s!     Q,$0A0A[0P,ru   z/pending activation rollback customer is invalidr8  z;pending activation rollback registry fingerprint is invalidz8pending activation journal has identical registry statesr  z3activation journal audit does not match the profiler   r  r   z8activation journal data root does not match the customerc                R    t        | ||      }t        |g       t        d      |)Nz$pending activation recovery required)r  r  r   )r  r  r  failurer  rh  s       rr   require_recoveryz<_recover_activation_journal_locked.<locals>.require_recovery  s/    
 -Y	5I,\7WIN !GHeSr   r   inspectr>  r  c                    	 t        i d| dd|  dt        j                  t        j                        j                         i       y # t        $ r} dd|  d|       Y d }~y d }~ww xY w)Nr'  r7  F_atr  zrecord z state)rV  r	   r:  r   rR  r  rY  )
state_namer   r  rh  r  s     rr   terminalizez7_recover_activation_journal_locked.<locals>.terminalize  s~    	K%Z'!l#&X\\(B(L(L(N	  	KY'*V(DcJJ	Ks   AA 	A-A((A-rR   zverify committed activationz)activation data root is missing or unsafer?  r  r  r  zverify restorez4registry restore did not reach the prepared snapshotzverify compensationz(activation audit receipt remains presentr)  	reconcilez'registry and audit states are ambiguous)r  r   r  r   r  BaseExceptionr  None)r  r   r  r  )rU  r  r  r   r  rS  r   rL  r   r  r   rc  r4  r   r   r   r
  rQ  rO  r  rY  rX  rU  rV  rX  r  r  )r   r   r'  journal_registryprevious_payloadr  r   r0  r1  r9  previous_customerprevious_sha256recorded_previous_sha256r  r   expected_data_rootr{  current_fingerprintaudit_stater  restoredr  r  rh  r  s                        @@@@rr   r  r  J  s   +L9L&|4GKK E{{9Y. !HII**@@ !JKK,O$
 =0022 D
 	
 {{#67&- !NOO#223CD [[!12Nkk"34O!++&;<++m,K~s+/3/-s3"+s+ !HIIQ(,,Q  $5$=$= !RSS+H5O&{{+EF ,$7 I
 	
 /) F
 	
 'L!<J +L9AACC !VWW%K ,I '/+=KTTV&& F
 	
TTT T 
	T5&3G<25) 3
K o-+2J  "##% L9-"#NO
 	K o-+2IK o-+2I	9=(+	@,U=-ABH & "J	B9-$7K ("%"#MN
 	K o-+2J	6+JG	=9-$7K (" "#MN
 	K DEM "9j9  =
	D  5Y445  2)S112L  	9ZC88	9  	@Z)93??	@   	BW&;SAA	B  	6Wi55	6  	=W&6<<	=s   O# P P, Q ,Q. R R0  S #P7PP	P)
P$$P),	Q
5
QQ
	Q+
Q&&Q+.	R7
RR	R-
R((R-0	S9
S		S	S/
S**S/c               D    	 t        | ||      dk(  S # t        $ r Y yw xY w)Nr>  rR   F)rX  r   )r  r0  r9  s      rr   _audit_contains_receiptr    s;    
+-$7
 	
  s    	c                   t        t        |             j                  d      dk7  rt        d      j                  d      }|dvsj                  d      |j                  k7  rt        d      j                  d      du rt        d      j                  d	      }j                  d
      }j                  d      }t        |t              r"|r t        |t              rt        |t              st        d      |dv rωj                  d      }t        |t              st        d      	 t        | |j                        }	t        |j                  |	j                  d   t        t        |	j                              t        |j                  d            |	j                  d         }
||
k7  rt        d      |dk(  rj                  d      }d}t        |t              rt#        fd|D              rt        d      t%        || d      }	 |j'                         }t)        j*                  |      j-                         }t/        j0                  |      }t        |t              rZ|j                  d      k7  sF|j                  d      j                  d      k7  s#|j                  d      j                  d      k7  rt        d      | dz  dz  dz  d z  }	 t9        ||        t;        |      }t        |t              r_t=        ||j                  t              t        |j                  d            t?        |j@                  j                  d            "      st        d!      	 t%        j                  d#      | d#      }t%        j                  d$      | d$      }t%        j                  d%      | d%      }| dz  d&z  |j                  z  jC                         }||jC                         k7  si||k7  sd|tE        |       jC                         k7  sHtG        |      |k7  s:|jI                         r*|jK                         rtM        ||       stO        |||'      st        d      d(D ]D  }j                  |      }t        |t              st        d      	 tQ        jR                  |       F |S # t        $ r}t        t        |            |d }~wt        t         f$ r}t        d      |d }~ww xY w# t2        t4        t.        j6                  f$ r}t        d      |d }~ww xY w# t2        t         f$ r}t        d!      |d }~ww xY w# t        $ r t        d      w xY w# t         $ r}t        d      |d }~ww xY w))Nr'  r?  0committed activation receipt is missing or staler   r  r  r7  Tr0  r1  r9  >   r  r5  r3  z3nutrition activation v2 receipt is missing or staler  rw   rx   r!  r"  r5  r  ) staff_membership_evidence_sha256staff_chat_inventory_sha256 membership_subscription_epoch_idc              3     K   | ]8  }t        j                  |      t               xs j                  |        : y wrk   )r   r  r   )ro   fieldr  s     rr   rs   z8_require_committed_activation_receipt.<locals>.<genexpr>a  s@      ;7E w{{51377Qw{{5?Q;QQ7s   >Az7committed staff membership evidence is missing or staler  r  subscription_epoch_idr  r  
migrationsznutrition-readiness-v1z legacy-activation-authority.jsonz8legacy activation authority manifest is missing or stale)rm   r   registry_projection_digestr   r   r   r  r   r>  )r<  r@  )*r  rU  r  r   rm   r   r   rL  r0   r3   rI  rK  r4   r   r/   KeyErrorr4  rJ  r  rN  r  r  r  rw   r  r   rx  r  r:   r8   r6   r   r   rS  rO  rQ  rU  rV  rX  r  r	   r3  )r   r   r   r  r   r0  r1  r9  r  	readinessexpected_receiptr   membership_pathrequired_membership_fieldsresolved_membershipmembership_bytesmembership_digestmembership_payloadmanifest_pathmanifestreceipt_registryreceipt_datareceipt_auditexpected_datarq   r1  r  s                             @rr   %_require_committed_activation_receiptr  %  s    ''?'MNG'++g.+= !STTkk)$Gi7;;}#=ARAR#R !STT{{&'4/ !STT[[!12Nkk"34O!++&;<~s+/3/-s3 !STT&++<='4($E 	9d//I  E!..)2):):9)E(4T):K:K5L(M+OOOO0, -6,=,=>T,U  &&$E  a<%kk*JKO*&
 os3s ;7; 8 )M  #8/O##6#A#A#C $+NN3C$D$N$N$P!%)ZZ0@%A" 148$4V(WW%))*GH;;<=>%))*AB;;ABC )M 
  '' 1	1 		!->(7H
 (D)1U**&27&;'KV,( ))B)B)B)OP2
 %J U0KK(

 -KK$

 .KK%
 "F*[84;L;LLUUWMM1133=(2<@HHJJ *o=##%##%]L9&) 3
 !!STT-C %%$%WXX	""5) . k ' 	8$SX.C7*% 	$E	: /1E1EF (M4 $ 	$J	@  U !STTU0  	$B	s{   5A?S AT &T7 8AU (U2	T
ST1S==TT4#T//T47UUUU/2	V;VVc                \    |j                   D ]  }|j                  st        | |||        y rk   )r   r
  r  )r   r   r   r  s       rr   rD  rD    s,    
 ""<<1mXt #r   c                ^   t        |       }|t        |      nt        |      j                         }t	        ||       t        ||      }t        fd|j                  D        d      }|t        d       |j                  r[t        ||||       t        t        |            }|t        d      |j                  d      dk(  r	 t        ||j                         yyt%        |||       t'        d |j                  D              }	|	rSt        t        |            }|t        d      |j                  d      dk(  r|	D ]  }	 t        ||j                          y# t         $ r}t        t#        |            |d}~ww xY w# t         $ r}t        t#        |            |d}~ww xY w)	zKFail closed unless every selected enabled customer has a committed receipt.Nc              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   r  s     rr   rs   z0validate_committed_activation.<locals>.<genexpr>  s!     U0dD4E4E4TT0ru   rv   r  r   r  c              3  :   K   | ]  }|j                   s|  y wrk   r  )ro   r   s     rr   rs   z0validate_committed_activation.<locals>.<genexpr>  s     X1CXxGWGW1C   T)r   r   r   rS  rC  r   r   r   r   r
  r  r  rU  r  r0   rm   r/   r   rD  r  )
r   r   r  r   resolved_registryr   r   r  r   r
  s
     `       rr   validate_committed_activationr"    s    !.D   	t$- ((* 
  &78%&7>HUh00U
 $'9+%GHH1'8 //G/MNG(F  {{9%*@5dH<Q<QR$  	.d4ExPX1C1CXX./G/MNG(F  {{9%* 'HD9$@U@UV !(
 # / @,SX6C?@ 3 D0S:CDs0   E# 
F	#	F,FF		F,F''F,c                    t        |       }t        |      }t        ||       t        ||      }t	        |||       	 t        ||      S # t        t        t        f$ r}t        d      |d}~ww xY w)zOLoad the registry only when enabled entries have committed activation receipts.zruntime registry failed closedN)
r   r   rC  r   rD  r!   r   r   r4  r   )r   r   r   r   r   s        rr   rT  rT  	  sr     .D*40Mm4%mT:H)$xHL%mT::!7J7 L !ABKLs   A	 	A.A))A.ro  z"adaptive-registration-inputs.jsonlz"adaptive-registration-config.jsonlzinput-approvals.jsonlz".adaptive-registration-inputs.lock@0000000000000000000000000000000000000000000000000000000000000000>   rf  r'  r   r   	intent_id
row_digestr   rm   r   input_documentr   append_sequencerh   prepared_digestr   revision_digestr   supersedes_digestactivation_receipt_idr   >   rf  r'  r   r%  r&  r   rm   r   r   r(  rh   r)  r   r*  artifact_digestsr   r+  r,  r   >   rf  r'  r%  r&  r   rm   r   artifact_kindr   r(  rh   artifact_digestr)  r   artifact_versionr   artifact_documentr+  registration_digestr   c                   t        |       }|j                         s=|j                         r-|j                         r|j	                         j
                  dz  rt        d      |j                  dk7  r|dz  }|j                         s=|j                         r-|j                         r|j	                         j
                  dz  rt        d      |S )N?   z)adaptive registration root is unavailabler'  )r   r   rU  rV  r   r   r   r  )r   r   s     rr   _registration_private_rootr5  e  s    	?D{{}{{}99;& !LMMyy%%''{{}{{}99;& !LMMKr   c                F    | t         z  | t        z  | t        z  | t        z  fS rk   )_REGISTRATION_REVISION_FILE_REGISTRATION_CONFIG_FILE_REGISTRATION_APPROVAL_FILE_REGISTRATION_LOCK_FILE)r   s    rr   _registration_pathsr;  z  s0    **((**&&	 r   c                Z   t        | t              r| j                  d      S t        | t              st	        d      t        |       h dk7  rt	        d      	 t        j                  t        |             }|j                  d      S # t        t        f$ r}t	        d      |d }~ww xY w)Nrw   rx   z>adaptive registration approver must be the full owner identity   r   r   r   )adaptive registration approver is invalid)
r   r   r   r   r   r  r  rL  rc  r4  )r1  r   r   s      rr   _registration_ownerr?    s    %)V,,eW% L
 	
 5z77 L
 	
W..tE{; (( z" W !LMSVVWs   B
 
B*B%%B*c                   | t        j                  t              n| }t        |t              r	 t        j
                  |      }nt        |t               r|}nt        d      |j                  |j                         t        d      k7  rt        d      |j                         S # t        $ r}t        d      |d }~ww xY w)N.adaptive registration approval time is invalid	   hours/adaptive registration approval time must be KST)r	   r:  r;  r   r   r3  r4  r   r9  	utcoffsetr   r  )r1  	timestampparsedr   s       rr   r   r     s    &+mT"I)S!	++I6F
 
Ix	( !QRR}} 0 0 2ia6H H !RSS  	$@	s   B' '	C0B<<Cc                   |j                   st        d      t        | |||      }t        t	        |             }|t        d      dD ]0  }|j                  |      }t        |t              r|r't        d       |j                  j                  d      }t        |      }	t        |      }
t        t        |j                  ||d   |d   d      }t        |j                  ||	|
||d	}|t        |      fS )
N9customer must be enabled for adaptive registration inputsz&activation receipt is missing or stale)r1  r9  rw   rx   r1  r9  )r   rm   r,  r1  r9  )r   rm   r   r   r   r,  r   )r
  r   r  r  rU  r  r   r   r   r   r   rQ  _REGISTRATION_SCHEMA_VERSIONrm   )r   r   r   r  r0  r  rq   r1  r   r   r   r   r   s                rr   r   r     s    << G
 	
 ;	N ''?'MNG !IJJ9C %%U$%MNN : NN%%6%2E"5)L+H5O /: --%3&'89#*+@#A	
! 7))$*!/%>I oi000r   recoverc               H   | j                         rt        d      | j                         sg S | j                         st        d      	 | j	                         }g }d}t        |j                  d            D ]  \  }}|t        |      z   t        |      k(  }|j                  d      s|r|r nt        d|       |d d	 }	|	j                  d
      r|	d d	 }	|	st        d      	 t        j                  |	j                  d            }
t        |
t               st        d      |j#                  |
       |t        |      z  } |rM|t        |      k7  r?	 | j%                  d      5 }|j'                  |       d d d        | j)                  d       |S |t        |      k7  rt        d      |S # t
        $ r}t        d      |d }~ww xY w# t        t        j                  f$ r}t        d      |d }~ww xY w# 1 sw Y   xY w# t
        $ r}t        d      |d }~ww xY w)N4adaptive registration journal symlink is not allowedz4adaptive registration journal must be a regular filez,adaptive registration journal is unavailabler   Tr  r  z5adaptive registration journal has a torn tail at row    z2adaptive registration journal contains a blank rowr   z(adaptive registration journal is invalidz,adaptive registration journal row is invalidzr+br   z-adaptive registration journal recovery failedz-adaptive registration journal has a torn tail)r   r   rU  rt  rN  r   	enumerater  rH  endswithrw   r  r  rx  r  r   rL  r  r   truncater  )r   rM  rawr   rowscomplete_endindexr  finalr  rH  r)  s               rr   _registration_jsonlrZ    s*     !WXX;;=	<<> !WXXoo
 %'DL !>?ts4y(CH4}}U#5$GwO  s)E"crlG$D 	ZZw 78F
 &$'$%STTFD	!1 @2 <3s8+	5!V- "JJu K 
S	! !PQQKU   :
	. #D$8$89 	$:	 "!  	$?	s`   
F1 '$GH )G;;H 1	G:GGG8'G33G8;H H 	H!HH!c                v    | j                         D ci c]  \  }}|dk7  s|| }}}t        |      S c c}}w )Nr&  )rs  r   )rowrq   r1  bodys       rr   _registration_row_digestr^  	  s<    ),L:3|8KCJDL4   Ms   55c                    t        | t              r t        |       dk7  st        d | D              rt	        d| d      | S )N@   c              3  $   K   | ]  }|d v 
 yw0123456789abcdefNr   rE  s     rr   rs   z'_registration_digest.<locals>.<genexpr>	  s     JEyy 22E   adaptive registration z digest is invalid)r   r   rH  rJ  r   )r1  ro  s     rr   _registration_digestrf  	  sA    uc"u:JEJJ #9%@R!STTLr   c                n   i }g }t        | d      D ]  \  }}t        |      t        k7  rt        d      |j	                  d      t
        k7  rt        d      |j	                  d      dk7  rt        d      |j	                  d	      |k7  rt        d
      |j	                  d      t        |      k7  rt        d      |j	                  d      }|j	                  d      }|j	                  d      }t        |t              r|st        d      t        |t              r|st        d      |dvrt        d      |dk(  r|j	                  d      t        d      |dk(  r*t        |j	                  d      t              st        d      t        |j	                  d      d      }t        |j	                  d      d      }	t        |j	                  d      d      }
t        |j	                  d      d        t        |j	                  d!      d"       t        |j	                  d#      d$       t        |j	                  d%      t              st        d&      t        |j	                  d      t              st        d'      t        |j	                  d(      t              st        d)      t        |j	                  d*      t              st        d+      	 t        j                  t        |d*               }|j                  |j                         t!        d,-      k7  rt        d.      |d%   }|j	                  d      |k7  s|j	                  d/      |k7  rt        d0      |j	                  d      |	k7  rt        d1      |j	                  d      |
k7  rt        d2      t#        t%        |d               |
k7  rt        d3      |dk(  rt        |j	                  d      d4       |j	                  |      }| |dk7  rt        d5      t%        |      ||<   |j	                  d      dk7  s|dk7  rt        d6      |j	                  d      |j	                  d      k7  rt        d7      t        h d8z
  D ]0  }|j	                  |      |j	                  |      k7  s't        d9       t%        |      ||<   |j'                  t%        |              |j)                         D cg c]  }|j	                  d      dk(  s| }}i }|D ]/  }|j+                  t        |d         g       j'                  |       1 |j-                         D ]B  \  }}t.        }|D ]2  }|j	                  d      |k7  rt        d:|       t        |d         }4 D ||fS # t        $ r}t        d+      |d }~ww xY wc c}w );Nr   startz6adaptive registration revision journal schema mismatchr   z.adaptive registration revision schema mismatchrf  customer_adaptive_registrationz.adaptive registration revision kind is invalidr(  z9adaptive registration revision sequence is not contiguousr&  z2adaptive registration revision row digest mismatchr%  rm   r'  z0adaptive registration revision intent is invalidz2adaptive registration revision customer is invalid>   r6  r?  z/adaptive registration revision state is invalidr6  r)  z=prepared adaptive registration revision has a terminal digestr?  z?committed adaptive registration revision has no prepared digestr*  revisionr+  supersessionr   r   r   r   r   r   r   r)  r'  /adaptive registration input document is invalidz*adaptive registration authority is invalidr   r>  rh   rA  rB  rC  rE  rD   z0adaptive registration revision document mismatchz4adaptive registration revision supersession mismatchz1adaptive registration revision authority mismatchz/adaptive registration authority digest mismatchzprepared rowz6adaptive registration terminal row has no prepared rowz4adaptive registration revision transition is invalidz.adaptive registration prepared digest mismatch>   r'  r&  r(  r)  z/adaptive registration prepared payload mismatchz8adaptive registration supersession chain is invalid for )rR  r  _REGISTRATION_REVISION_FIELDSr   r  rK  r^  r   r   rf  r   r	   r3  r4  r9  rF  r   r   rL  r  values
setdefaultrs  _REGISTRATION_ZERO_DIGEST)rV  statesr?  expected_sequencer\  r%  rm   r'  r*  
supersedesr   	parsed_atr   r   r  rq   rg  by_customercustomer_rowsexpecteds                       rr   $_validate_registration_revision_rowsry  	  s    ,.F)+I"+D":":3s844$H  77#$(DD$%UVV776?>>$%UVV77$%)::$K  77< $<S$AA$D  GGK(	ww~. )S)$%WXX,,L$D  11$%VWWJ377+<#=#I$O  K
377;L3Ms(S$Q  /sww7H/I:V)#''2E*FW
/GG&'
 	SWW%67DSWW^4g>SWW%@A<P#''"23W=$%VWW#''+.8$%QRR#''-0':$%PQQ#''"34c:$%UVV	 ..s37H3I/JKI
 #y':':'<	PQ@R'R$%VWW'(LL(L8||H%8$%WXX<<+,
:$F  <<*+/??$C  4K 0126FF$%VWWK ):!;^L::i(
"(L  !%S	F9<< J.%;2F$F  77$%l)CC$%UVV0 4
 
C wws|x||C00(E 
 !Iyc#O #;P %mmoPos1AZ1OsoGP68Ks3~#67<CCCH '2'8'8':#m, Cww*+x7(N|n]  3012H ! (; g}  	$@	d Qs$   !V&V2 V2	V/V**V/c                ,   i }g }t        | d      D ]E  \  }}t        |      t        k7  rt        d      |j	                  d      t
        k7  rt        d      |j	                  d      dk7  rt        d      |j	                  d	      |k7  rt        d
      |j	                  d      t        |      k7  rt        d      |j	                  d      dvrt        d      |j	                  d      }t        |t              r|st        d      dD ]   \  }}t        |j	                  |      |       " |j	                  d      }t        |t              rt        |      h dk7  rt        d      |j                         D ]  }	t        |	d        t        |j	                  d      t              st        d      t        |j	                  d      t              st        d      |j	                  d      dk(  r|j	                  d      t        d      |j	                  d      dk(  r*t        |j	                  d      t              st        d      |j	                  |      }
|
/|j	                  d      dk7  rt        d       t        |      ||<   |
j	                  d      dk7  s|j	                  d      dk7  rt        d!      |j	                  d      |
j	                  d      k7  rt        d"      t        h d#z
  D ]0  }|j	                  |      |
j	                  |      k7  s't        d$       t        |      ||<   |j                  t        |             H |j                         D cg c]  }|j	                  d      dk(  s| }}||fS c c}w )%Nr   rh  z4adaptive registration config journal schema mismatchr   z,adaptive registration config schema mismatchrf  %customer_adaptive_registration_configz,adaptive registration config kind is invalidr(  z7adaptive registration config sequence is not contiguousr&  z0adaptive registration config row digest mismatchr'  >   r6  r?  z-adaptive registration config state is invalidr%  z.adaptive registration config intent is invalid))r*  rk  r+  rl  r   r   r   r   r   r   r   r)  r-  >   catalogbase_policymeal_constraintsz2adaptive registration config artifacts are invalidartifactr   z0adaptive registration config approver is invalidrh   z5adaptive registration config approval time is invalidr6  r)  z;prepared adaptive registration config has a terminal digestr?  z=committed adaptive registration config has no prepared digestz=adaptive registration config terminal row has no prepared rowz2adaptive registration config transition is invalidz5adaptive registration config prepared digest mismatch>   r'  r&  r(  r)  z6adaptive registration config prepared payload mismatch)rR  r  _REGISTRATION_CONFIG_FIELDSr   r  rK  r^  r   r   rf  r   ro  rL  r  )rV  rr  r?  rs  r\  r%  r  ro  r-  r/  r  rq   rg  s                rr   "_validate_registration_config_rowsr  	  s    ,.F)+I"+D":":3s822$F  77#$(DD$%STT776?EE$%STT77$%)::$I  77< $<S$AA$%WXX777#<<$%TUUGGK(	)S)$%UVV
LE5 !7
 77#56*G4<L8M R
 9

 %D   0668O *=  9#''-0':$%WXX#''"34c:$G  777z)cgg6G.H.T$M  777{*:GG%&4
 %O  ::i(www:-(S  !%S	F9<< J.#'''2Bk2Q$D  77$%l)CC$G  / 2
 
C wws|x||C00(L 
 !Iyc#g #;h %mmoPos1AZ1OsoGPg Qs   -NNc                    |}|dddd|   }t         fd|D        d      }|t        d| d      |j                         s|j                         st        d| d	      	 |j	                         j
                  d
z  rt        d| d      t        j                  |j                  d            }nt        |t        t        f      rt        |      }|j                         s|j                         st        d| d	      	 |j	                         j
                  d
z  rt        d| d      t        j                  |j                  d            }t        |t              st        d| d      t!        |      S # t        t        t        j                  f$ r}t        d| d	      |d}~ww xY w# t        t        t        j                  f$ r}t        d| d	      |d}~ww xY w)zVRead approved artifacts; only a missing canonical catalog may use legacy catalog.json.N)rr  )zmeal-constraints.json)zfood-catalog.jsonzcatalog.jsonr  r  r  c              3  t   K   | ]/  }|z  j                         s|z  j                         r|z   1 y wrk   rU  r   )ro   r  r   s     rr   rs   z5_registration_read_artifact_source.<locals>.<genexpr>
  s<      !D4K'')dTk-E-E-G t!s   58	adaptive z artifact is missingz artifact is invalidr4  z# artifact permissions are too broadr   r  z artifact must be an object)r   r   r   rt  r   r   rw   r  r  r   rx  r  r   r   r   r   rL  )r   r.  suppliedr1  namesr   r   s   `      rr   "_registration_read_artifact_sourcer  	  s    E} , :<
 	
 !
 
 <$y?S%TUU??DLLN$y?S%TUU		yy{""U*(.QR  JJt~~w~?@E
 
EC;	'E{??DLLN$y?S%TUU		yy{""U*(.QR  JJt~~w~?@E
 eW% 9]O;V!WXX;) +T-A-AB 	$M?*>?	 +T-A-AB 	$M?*>?	s2   %AF AF9 F6!F11F69G,G''G,c                    dddd|   }|D ]  }|| v s| |   c S  h d}| j                         D ci c]  \  }}||vs|| }}}|r|S t        d| d      c c}}w )N)r   r  r1  r  )r  constraintsr1  r  )r  food_catalogfoodsr1  r  r  >   rD   r
  r   r   r   rm   r   r   rh   r   r   r,  r  z artifact value is missing)rs  r   )r   r.  aliasesrq   metadatar~  r1  s          rr   _registration_artifact_valuer  .
  s    
 BPH 	G
 (?C=  H .6^^-=-=>3	HATY-= 
  
y7QR
SSs   A Ac                &   | j                  d      durt        d| d      | j                  d      t        |      k7  rt        d| d      | j                  d      }t        |t              r|j                         st        d| d      | j                  d	      }t        |t              r|j                         st        d| d
      t        |       | j                  d      }t        |t              r t        |      dk7  st        d |D              rt        d| d      t        | |      t              |k7  rt        d| d      |dk(  rt        t              st        d      |dk(  r/t        t              rt        fddD              st        d      |dk(  rJt        t              rdv rj                  d      n}t        |t        t        f      r|st        d      t        |       |j                         |fS )Nr   Tr  z artifact is not approvedr   z' artifact approver does not match ownerr   z artifact version is requiredrh   z# artifact approval time is requiredrD   r`  c              3  $   K   | ]  }|d v 
 ywrb  r   rE  s     rr   rs   z;_registration_validate_external_artifact.<locals>.<genexpr>j
  s     MHyy 22Hrd  z artifact digest is invalidz' artifact digest does not match contentr  z4adaptive base_policy artifact must contain an objectr  c              3  &   K   | ]  }|v  
 y wrk   r   ro   rq   r1  s     rr   rs   z;_registration_validate_external_artifact.<locals>.<genexpr>u
  s      5
$SSC5L$S   )
meal_countbudget_tiercooking_accessz0adaptive meal constraints artifact is incompleter  r  z"adaptive catalog artifact is empty)r  r   rL  r   r   r   r   rH  rJ  r  r   r   allr  r  )r   r.  r   r   approved_atdeclaredrV  r1  s          @rr   (_registration_validate_external_artifactr  P
  s   
 ||Jt+ 9]O;T!UVV||M"d5k1 &MN
 	
 ll9%Ggs#7==? &CD
 	
 ,,01Kk3'{/@/@/B &IJ
 	
  ,||H%Hx%x=BMHMM 9]O;V!WXX(=AEu) &MN
 	
 %j.H !WXX**%) 5
$S5
 2
 %%WXX	! %)g.> IIg 	
 $u.d$%IJJ>7==?H44r   c               2   | d   | d   | d   t        | d         t        | d         t        | d         | d   D cg c]  }|d   |d	   |d
   |d   d c}dd}t        |      }t        | d    d|dt        |      ||d}|t	        |d         |fS c c}w )Nr  budget_bandr  preferences
exclusionsr   training_scheduler   r
   weekdayload_category)kst_daytraining_timer  r  T)r  r  r  r  excluded_food_idsr   training_time_by_daystrict_inputsr   z.meal-constraints)r   r   rD   r   r   rh   r  )r  r   rK  rL  r   )r  r   rh   r  r1  r/  r   s          rr   &_registration_derived_meal_constraintsr  
  s     l+}-!"23GM23!',"78'+./ !!45!
 6 !=!&v +!&!7	 6!
 !E$ &e,O6i()):;!E{*!H S),->>+!
s   Bc          
     (   t        |t              r|j                  dd      }n]t        |t              rt	        |      }nAt        |dd       }t        |      r
 |dd      ni }t        |t              rt	        |      ni }t        | d|j                  d|j                  d                  }t        | d|j                  d|j                  d                  }	t        |d|      \  }
}}t        |	d|      \  }
}}t        |||	      \  }}}|j                  d
      }|bt        | d
|      }t        |d
|      \  }
}}t        |d
      }t        |d
      }||k7  s||k7  st        |      t        |      k7  rt        d      d|||fd
|||fd|	||ffS )Nrw   Try   exclude_noner   r  r   r  r  r   rh   r  zFadaptive meal constraints must be derived from the registration inputs)r   r   r   r   rL  r  r  r  r  r  r  r  r   r   )r   r  inputsr   rh   rU  r   dumpedpolicy_documentcatalog_document_rp  r   catalog_versioncatalog_digestconstraints_documentconstraints_versionconstraints_digestconstraints_sourcesupplied_constraintssupplied_versionsupplied_digestsupplied_valuederived_values                           rr   _registration_artifact_bundler  
  s    &45V$?	FG	$6lV\48
:B::NJF6TV 	 )9d6lr8swwx01O
 :	 23
 (P($A~}
 *R*&A 	/+	
 B-/A !34%A 

 0X 0
,_
 6 
 5 

  33"44~./-2PP$X  
G 		
 
$o~F	 	r   c                    i dt         ddd| d|d|d|d|d	|d
|d|dt        |      d|	dt        |      d|d|
d   d|
d   d|
d   ||d}i |dt        |      iS )Nr   rf  adaptive_input_approvalr(  r%  r'  rm   r2  r.  r0  r/  r1  r+  r   rh   r   r   r   )r   r)  r&  rK  rL  r^  )sequencer%  r'  rm   r2  r.  r1  r0  r/  r+  r   r   r   rh   r)  r]  s                   rr   _registration_approval_rowr  
  s   $6) 	8 	Y	
 	 	 	2 	 	, 	? 	T"34 	. 	tE{ 	? 	9%67  		.1!" 	$Y/J%K#$ -*'D* BdAL":4"@AAr   c                    i }g }t        | d      D ]  \  }}t        |      t        k7  rt        d      |j	                  d      t
        k7  rt        d      |j	                  d      dk7  rt        d      |j	                  d	      |k7  rt        d
      |j	                  d      t        |      k7  rt        d      |j	                  d      dvrt        d      |j	                  d      }t        |t              r|st        d      |j	                  d      dvrt        d      dD ]   \  }}t        |j	                  |      |       " t        |j	                  d      t              st        d      t        |d   t        |d               }t        |      |j	                  d      k7  rt        d      t        |j	                  d      t              st        d      t        |j	                  d             t        |j	                  d      t              st        d      t        |d          |j	                  d      dk(  r|j	                  d      t        d       |j	                  d      d!v r*t        |j	                  d      t              st        d"      |j	                  |      }	|	/|j	                  d      dk7  rt        d#      t        |      ||<   |	j	                  d      dk7  s|j	                  d      d!vrt        d$      |j	                  d      |	j	                  d      k7  rt        d%      t        h d&z
  D ]0  }
|j	                  |
      |	j	                  |
      k7  s't        d'       t        |      ||<   |j	                  d      d(k(  s|j!                  t        |              |j#                         D cg c]  }|j	                  d      dk(  s| }}||fS c c}w ))Nr   rh  z/adaptive input approval journal schema mismatchr   z'adaptive input approval schema mismatchrf  r  z'adaptive input approval kind is invalidr(  z2adaptive input approval sequence is not contiguousr&  z+adaptive input approval row digest mismatchr'  >   r6  r  r?  z(adaptive input approval state is invalidr%  z)adaptive input approval intent is invalidr.  >   r  r  r  z0adaptive input approval artifact kind is invalid))r2  registration)r/  r  r|  r~  r  r  r}  r1  +adaptive input approval artifact is invalidr/  z0adaptive input approval artifact digest mismatchr   (adaptive input approval owner is invalidrh   z'adaptive input approval time is invalidr6  r)  z6prepared adaptive input approval has a terminal digest>   r  r?  z7terminal adaptive input approval has no prepared digestz8adaptive input approval terminal row has no prepared rowz-adaptive input approval transition is invalidz0adaptive input approval prepared digest mismatch>   r'  r&  r(  r)  z1adaptive input approval prepared payload mismatchr?  )rR  r  _REGISTRATION_APPROVAL_FIELDSr   r  rK  r^  r   r   rf  r   r  r   r?  r   rL  r  ro  )rV  rr  r?  rs  r\  r%  r  ro  artifact_valuer  rq   rg  s               rr   $_validate_registration_approval_rowsr  (  s    ,.F)+I"+D":":3s844$%VWW77#$(DD$%NOO776?77$%NOO77$%)::$D  77< $<S$AA$%RSS777#II$%OPPGGK(	)S)$%PQQ77?# ,
 

 %%WXX
LE5 !7
 #''"56@$%RSS5#$O$%
 >*cgg6G.HH$%WXX#''-0':$%OPPCGGM23#''"34c:$%NOO#C(9$:;777z)cgg6G.H.T$H  77799*GG%&C
 %I  ::i(www:-(N  !%S	F9<< J.#'''2B K
 3
 %%TUU77$%l)CC$%WXX0 4
 
C wws|x||C00(G 
 !Iy777{*T#Y'k #;l %mmoPos1AZ1OsoGPg Qs   O;1O;c              #  Z  K   t        |       \  }}}}d }d}	 	 |j                         rt        d      t        j                  |t        j
                  t        j                  z  t        j                  z  t        j                  z  d      }t        j                  |      }|j                         }|j                  |j                  f}t        j                  |j                        rm|j                   t        j"                         k7  sL|j$                  dk7  s=t        j&                  |j                        dk7  s||j                  |j                  fk7  rt        d      t)        j*                  |t(        j,                         d}||j                         j                  |j                         j                  fk7  rt        d      	 d  |=	 |r$t)        j*                  |t(        j0                         t        j2                  |       y y # t        $ r  t.        $ r}t        d      |d }~ww xY w# t        j2                  |       w xY w# |W	 |r$t)        j*                  |t(        j0                         t        j2                  |       w # t        j2                  |       w xY ww xY ww)	NFz3adaptive registration lock symlinks are not allowedr   r   z$adaptive registration lock is unsafeTz'adaptive registration lock was replacedz)adaptive registration lock is unavailable)r;  r   r   r   r   r  r   r   r   r   r  r  r   r   r   r   r   r   r   r   r!  r"  r#  r   r$  r   )	r   r  r&  r   lockedr   r+  r,  r   s	            rr   _registration_lockr    s    ,T2Aq!Y!JF'%	##%(I  		BJJ&6EJ
 XXj)FOO%Ev}}5HLL0==BJJL0??a'<</58ell;;()OPPKK
EMM2FIOO-44ioo6G6N6NOO()RSS P 	!%KK
EMM:$ " " 	 	$;	 $ !%KK
EMM:$$ "sk   J+F/H I J+&H3 6J+H0H++H00I 3I

J+J(&J7J(J$$J((J+c                   t        |       \  }}}}|j                  d      }t        |t              st	        d      	 t        |       5  t        |d      }t        |      \  }	}
t        |d         t        |d         i }t        |	      D ]A  }|j                  d      k7  r|j                  t        |d         t        |d                C |D ],  \  }}}t        fd	|	D        d       }|H|j                  d      |k7  s(|j                  d
      |k7  s|j                  d      |k7  rt	        d      jt        fd|
D        d       }|j                  t              t        fd|	D        d       }|$|k7  r|j                  d      |k(  rt	        d      ||j                  d      |k7  s|j                  d      k7  rt	        d      |}t        t        |      dz   t        |d         d||||||t        |d         t        |d               }t        ||       |j!                  |       t        |      \  }	}
d d d }t        t        |      dz   |d||||||t        |d         d       }t        ||       t        t        |      dz   |d||||||t        |d         |d         }t        ||       |j#                  ||f       t        |      \  }	}
/ 	 d d d        y # 1 sw Y   y xY w# t        $ r  t$        $ r}t	        d      |d }~ww xY w)Nr   r  TrL  rm   rD   r.  r/  c              3     K   | ]B  }|j                  d       k(  r,|j                  d      k(  r|j                  d      k(  r| D ywrm   r2  r.  Nr  ro   r\  r.  rq   r2  s     rr   rs   z1_append_registration_approvals.<locals>.<genexpr>  sN      #,C77>2c9GG$9:>QQGGO4E	 #,   AAr1  r   z*conflicting adaptive input approval replayc              3     K   | ]B  }|j                  d       k(  r,|j                  d      k(  r|j                  d      k(  r| D ywr  r  r  s     rr   rs   z1_append_registration_approvals.<locals>.<genexpr>  sN      #*C77>2c9GG$9:>QQGGO4E	 #*r  c              3     K   | ]B  }|j                  d       k(  r,|j                  d      k(  r|j                  d      k(  r| D yw)rm   r.  r/  Nr  )ro   r\  r.  rq   rt  s     rr   rs   z1_append_registration_approvals.<locals>.<genexpr>  sM      #,C77>2c9GGO4EGG$56*D	 #,r  r0  z5adaptive input artifact version must change on updater+  z.conflicting incomplete adaptive input approvalr   r%  r?  rh   r&  )r  r%  r'  rm   r2  r.  r1  r0  r/  r+  r   r   r   rh   r)  zadaptive-input:r  r6  r  z)adaptive input approval transition failed)r;  r  r   r   r   r  rZ  r  r   reversedrp  r   rq  r  rH  _append_registration_rowr  extendr   )r   r   bundler   r   r  approval_pathr   rV  r?  rg  current_by_kindr\  r1  r0  r/  r  pending_match
prior_kindr6  commitr%  committed_rowr   r.  rq   r2  rt  s                           @@@@rr   _append_registration_approvalsr    s    16Aq-LL'EeW% !KLLPW%&}dCD!Ed!KIwh~./C"%hx&8"9.0O	*77>*c1**O,-s37H3I/J + ! #, 	 ' %67?J#<<(;<@QQ#<<(:;?OO0H   $#* 	! -00!#<
 "#, 	
 *':5"'9:>NN,O  !,%))*;<O(,,-@AZO0L   -H7!$TQ"%h{&;"<)%(,?&3*;)9(7*4"+)9#(+H5F,G(H(+H\,B(CF" -]FCKK')Md)S&Iw%cU!,?+@-Q  6 Y]'$!$(;"/&7%5$3&0'%5$'1B(C$D$(" )A : Y]'%!$(;"/&7%5$3&0'%5$'1B(C$D$,\$:!" )FX}56%I$%O"	7u # &%%X   W !LMSVVWs7   L J;L	L LL L M0L<<Mc           
     4  	 t        |       \  }}|rt        d      |D cg c],  }|j                  d      k(  r|j                  d      |k(  r|. }}t        |      dk7  s4t        |D ch c]  }t	        |j                  d             c}      dk7  rt        d      |D ci c]  }t	        |d         | }}dD ]F  	t
        }	fd|D        D ]/  }|j                  d	      |k7  rt        d
      t	        |d         }1 H |S c c}w c c}w c c}w )Nz0adaptive input approval transition is incompleterm   r2  r5  r.  z0adaptive input approval artifacts are incompleter  c              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 yw)rm   r.  Nr  )ro   r~  r.  rm   s     rr   rs   z1_registration_latest_approvals.<locals>.<genexpr>j  s;      
&	}}^,<o.-? &   47r+  z5adaptive input approval supersession chain is invalidr/  )r  r   r  rH  r   rq  )
rV  rm   r2  r?  rg  r\  selected_rowsr   rx  r.  s
    `       @rr   _registration_latest_approvalsr  T  sC   
 >dCIw !STT C77>"l2GG)*.AA 	   	MaG#CGGO,-GHAM !STT:GH-3C()3.-HHG,
&
C ww*+x7(K  3012H
 H O3 H Is   1D,!D&Dc           	        | j                         rt        d      	 | j                  j                  ddd       | j                  j	                  d       | j                  dd       | j	                  d       | j                  dd	      5 }|j                  t        j                  t        |      d
dd      dz          |j                          t        j                  |j                                d d d        y # 1 sw Y   y xY w# t        t         t"        f$ r"}t%        |t              r t        d      |d }~ww xY w)NrO  Trz   r{   r   )ry   r}   ar   r  Fr  r  r   z3adaptive registration journal could not be appended)r   r   r  r   r  touchr   r   rw   r  rL  r  r   r   r  r   rc  r4  r   )r   r\  r)  r   s       rr   r  r  x  s    !WXX$EB% 


-

5YYsWY-LL

IETj  LLNHHV]]_% .-- Y
+ c-. A
	s7   A0D
 A(C>5D
 >DD
 D
 
E D;;E c                   | j                  d      durt        d      | j                  d      t        |      k7  rt        d      | j                  d      |k7  rt        d      | j                  d      t        |      k7  rt        d      | j                  d	      |j                  d	      k7  rt        d
      | j                  d      |j                  d      k7  rt        d      | j                  d      |j                  d      k7  rt        d      | j                  d      |j                  d      k7  rt        d      y )Nr   Tz+adaptive registration input is not approvedr   z9adaptive registration input approver does not match ownerr   z(adaptive registration authority is staler   r   z0adaptive registration registry evidence is staler   z-adaptive registration owner evidence is staler   z2adaptive registration activation evidence is staler,  z1adaptive registration activation receipt is stale)r  r   rL  )r   r   r   r   s       rr   _registration_authority_matchesr    s2    ||Jt+ !NOO||M"d5k1 G
 	
 ||&'+;; !KLL||K DO3 !KLL||%&)--8I*JJ !STT||N#y}}^'DD !PQQ||/0IMM#5  !!UVV||+,	>U0VV !TUU Wr   c                  t        |       t        u r| j                  dd      n't        | t              rt        |       nt        d      j                  d      }|||k7  rt        d      j                  dt              t        k7  rt        d      j                  d	|      }t        |t              r|j                         st        d
      |j                         }d,fd}j                  d      }t        |      t        usd|cxk  rdk  st        d       t        d      j                  d      }j                  d      }t        |t              r|j                         st        d      t        |t              r|j                         st        d      j                  d      }	t        |	t        t        f      r|	st        d      g }
t               }|	D ]  }t        |      t        u r|j                  d      }n't        |t              rt        |      }nt        d      t        |      h dk7  rt        d      |j                  d      }|j                  d      }|j                  d      }|j                  d      }t        |t              r t        |t              rt        |t               rt        d      t        |t              r|j                         st        d      t        |t              r|j                         st        d      	 t#        j$                  |      }t'        j$                  |      }|j+                         |k7  rt        d!      ||v rt        d"      |j-                  |       |
j/                  |j1                         ||j1                         |j                         d#        |
j3                  d$ %       t        ||||j                         |j                          |d&       |d'       |d(      |
d)
}j                  d*      }t5        |      }|||k7  rt        d+      |S # t(        $ r}t        d       |d }~ww xY w)-Nrw   Tr  (adaptive registration inputs are invalidrm   z1adaptive registration customer key does not matchr   z/adaptive registration schema version is invalidr   z)adaptive registration version is requiredc                   | vrt        d|  d      |    }t        |t              r|g}t        |t        t        t
        t        f      st        d|  d      g }|D ]s  }t        |t              r,|j                         rt        |j                               dkD  rt        d|  d      |j                         }||vsc|j                  |       u |S )Nre  z is requiredr     )
r   r   r   r  r  r  	frozensetr   rH  r  )r  r1  rn  rp   
normalizedrU  s        rr   text_valuesz0_registration_input_payload.<locals>.text_values  s    s?$'=dV<%PQQD	eS!GE%$sI!>?$'=dV;%OPPDdC(

DJJL@QTW@W(+A${)STTJ'j)  r   r  r      z8adaptive registration meal_count is required and invalidr  r  z-adaptive registration budget_band is requiredz0adaptive registration cooking_access is requiredr  z3adaptive registration training_schedule is requiredrx   z2adaptive registration training_schedule is invalid>   r   r
   r  r  r   r  r
   r  z?adaptive registration training_schedule date/weekday is invalidz7adaptive registration training_schedule time is invalidz@adaptive registration training_schedule load category is invalidz<adaptive registration training_schedule date/time is invalidz:adaptive registration training_schedule weekday is invalidz<adaptive registration training_schedule dates must be unique)r   r  r
   r  c                :    t        | d         t        | d         fS )Nr   r
   r   )rp   s    rr   <lambda>z-_registration_input_payload.<locals>.<lambda>!  s    3tF|+<c$v,>O*Pr   )rq   r  r  r   )
r   rm   r   r  r  r  r  r  r   r  rD   9adaptive registration input digest does not match content)r  r   r  z	list[str])r   r   r   r   r   rL  r   r  rK  r   r   r   r  r  r  r   r  r   r3  r
   r4  r  addr  r  sortr   )r  rm   default_versionsupplied_keyr   r  r  r  r  r  parsed_schedule	seen_daysr  r~  	day_valueweekday_value
time_value
load_value
parsed_dayparsed_timer   r  r  computedrU  s                           @rr   _registration_input_payloadr	    sC    F|11!'!2!2T!2!R	FG	$6l !KLL77>*LLL$@ !TUU ">?'	( !!RSSggi1Ggs#7==? !LMMmmoG" &JJs"!z*>Q*> F
 	
 +? F
 	
 ''-(KWW-.Nk3'{/@/@/B !PQQnc*.2F2F2H !STTww*+Hhu.h !VWW/1O5I;77((f(5Iw'UI$D  y>II$D  MM&)	!i0]]6*
]]?3
9c*mS1-.$Q  *c**2B2B2D$I  *c**2B2B2D$R 	++I6J,,Z8K
 =0$L  "$N  	j!",,.(#--/!+!1!1!3		
c r PQ6$ "((*(..0"=1!,/ -,G ggh'Ow'H"('B G
 	
 NQ  	$N	s   **Q	Q1 Q,,Q1c           
        t        |dd       dk7  ry dD ]0  }| j                  |      t        ||d       k7  s#t        d| d       t        t        |dd            t        t        |dd            t        g t        |d	d      t        |d
d            d}|j	                         D ]I  \  }}| j                  |      }t        |t        t        f      rt        |      |k7  s<t        d| d       y )Nstarting_contextznutrition-onboarding-v1)r  r  r  re  z  differs from onboarding profiler   r   r   r   disliked_foods)r  r   r  )r  r  r   r  rs  r   r  )r  r  r  expected_listsrx  actuals         rr   ._require_onboarding_profile_registration_matchr  7  s    w*D15NN@;;u%!>>$(/OP  A WW.@"EF77K<="8"="2B7
	N *//1xU#&4-0E&MX4M$(/OP  2r   c               (   i t        |       t        |       |dt        |      ||d   |d   |d   |d   |t        |      d}	 t        j                  |      }|j                  dd	      S # t        t
        t        f$ r}t        d      |d }~ww xY w)
NTr,  r   r   r   )rD   r+  r   r   rh   r,  r   r   r   r   r   r  rw   r  )	rL  r   r   r  r   rc  r4  r   r   )	r  r+  r   r   rh   r   rn  	validatedr   s	            rr   _registration_documentr  T  s    
w-!'*.K(*!*+B!C%./J%K$%67!.1,)_FV.==fE	 V$?? "9j9 V !KLRUUVs   A, ,B BBc                    i dt         ddd| d|d|d|d   d|d   d	|d
   dt        |      d|d   d|d   d|d   d|d   d|d   d|d   d|d   d|d   |d   |d}i |dt        |      iS )Nr   rf  rj  r(  r%  r'  rm   r   r*  rD   r'  r+  r,  r   r   r   r   r   r   rh   )rh   r)  r&  r  r  r%  r'  r   r)  r]  s         rr   _registration_revision_rowr  r  sA   60 	8 	Y	
 	 	0 	8I& 	8H- 	$x. 	X&9: 	 *A!B 	$X.I%J 	8$56 	0 	H%78  	Xk*!" 	x.#$ $$56*'D* BdAL":4"@AAr   c                    i dt         ddd| d|d|d|d   d|d   d	|d
   d|d   d|d   d|d   d|d   d|d   d|d   d|j                  d      d|d   d|d   d|i}i |dt        |      iS )Nr   rf  r{  r(  r%  r'  rm   r   r*  rD   r+  r,  r   r   r   r   r-  r   rh   r)  r&  )rK  r  r^  r  s         rr   _registration_config_rowr    sA   67 	8 	Y	
 	 	0 	8I& 	8H- 	X&9: 	 *A!B 	$X.I%J 	8$56 	0 	H%78 	HLL);<  	x.!" 	8$56#$ 	?%D( BdAL":4"@AAr   c           
     x   t        |       \  }}}}t        |d      }t        |d      }t        |      \  }}t        |      \  }	}
t	        |d         t	        |d         t        fd|D        d      }||j                  d      t        |      k7  rt        d      t        fd	|	D        d      }|[d
 d }t        t        |      dz   |d|d      }t        ||       t        t        |      dz   |d||d         }t        ||       y|rt        fd|D        d      }|t        d      |j                  d      t        |      k7  rt        d      |}t        t        |      dz   t	        |d         d|t	        |d               }t        ||       |j                  |       t        |      \  }}n|d d }t        t        |      dz   |d|d      }t        ||       t        t        |      dz   |d||d         }t        ||       |j                  ||f       t        |      \  }}t        |d      }t        |      \  }	}
t        fd|	D        d      }||
rft        fd|
D        d      }|t        d      |}t        t        |      dz   t	        |d         d|t	        |d               }t        ||       yd
 d }t        t        |      dz   |d|d      }t        ||       t        t        |      dz   |d||d         }t        ||       yy)zCAppend a registration revision while the registration lock is held.TrL  rD   rm   c              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 ywrm   r*  Nr  ro   r\  rq   r*  s     rr   rs   z7_registration_append_revision_locked.<locals>.<genexpr>  s<      	
 ww~&#-)*o=  r  Nr'  z(conflicting adaptive registration replayc              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 ywr  r  r  s     rr   rs   z7_registration_append_revision_locked.<locals>.<genexpr>  s<      +C77>*c1GG-./A +r  zregistration-config:r  r   r6  )r  r%  r'  r   r)  r  r?  r&  c              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 ywr  r  r  s     rr   rs   z7_registration_append_revision_locked.<locals>.<genexpr>  s<      "C77>*c1GG-./A "r  z:another adaptive registration transition requires recoveryz3conflicting incomplete adaptive registration replayr%  zregistration:c              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 ywr  r  r  s     rr   rs   z7_registration_append_revision_locked.<locals>.<genexpr>  s<      	
'ww~&#-)*o= 'r  c              3  r   K   | ].  }|j                  d       k(  r|j                  d      k(  r| 0 ywr  r  r  s     rr   rs   z7_registration_append_revision_locked.<locals>.<genexpr>%  s<      -ww~.#5 12oE -r  zAanother adaptive registration config transition requires recovery)r;  rZ  ry  r  r   r   r  rL  r   r  rH  r  r  r  r  )r   r   revisions_pathconfig_pathr  revision_rowsconfig_rowsr?  rg  config_committedconfig_pendingexistingconfig_existingr%  config_preparedconfig_commitr  r6  r  matching_configconfig_intentrq   r*  s                        @@rr   $_registration_append_revision_lockedr,    s    )<D(A%NKA'EM%k4@K=mLIw'I+'V$n(8,-O
h~&
'C	
 	
 	H <<()T(^;$%OPP+ 
 ".se1_4EFI6[)A-# ! $O %[/B4[)A-#!! / =M %[-@" 
 $L  <<()T(^;$E  +'!+(;/0 67
 	!8V$A-P	7#C5/):;	-'!+ 
 	!:+'!+$\2
 	!8h/0A-P	7%k4@K'I+'V$n	
'	
 	O "- O &(W  .O4[)A-ok:;!! #OL$A BM %[-@23%q8IJM6[)A-' ! $O %[/B4[)A-'!! / =M %[-@S r   c                    	 t        |       5  t        | |       d d d        y # 1 sw Y   y xY w# t        $ r  t        $ r}t        d      |d }~ww xY w)N'adaptive registration transition failed)r  r,  r   r   )r   r   r   s      rr   _registration_append_revisionr/  N  sQ    U%0x@ &%%  U !JKQTTUs)   / #/ ,/ / AAAc          
       34567 t        t        |             }t        |      5t        |      }|rt	        ||       t        ||      }t        |||       t        5fd|j                  D        d       }|t        d5       |j                  st        d      t        ||||      \  }}t        5fdt        ||      j                  D        d       }	|	t        d      t        |	j                        }
t        |
      \  }}}}t!        |      }t!        |      }t#        |      \  }}t%        |      \  }}|s|rt        d      |D cg c]  }|j'                  d      5k(  s| }}|D cg c]  }|j'                  d      5k(  s| }}t)        |D ch c]  }t+        |j'                  d             c}      t)        |      k7  s=t)        |D ch c]  }t+        |j'                  d             c}      t)        |      k7  rt        d	      |r|st        d
      |d   6|d   7t!        |      }t-        |5t+        6d               }7j'                  d      6j'                  d      k7  rt        d      6j'                  d      4t/        4t0              st        d      t3        4|||j4                  j7                  d             |j4                  j7                  d      }t9        4j'                  d      d      }|6j'                  d      k7  rt        d      	 t;        45t+        4j'                  dd                  }t=        ||t+        4d               \  }}}tE        |d      }!t/        |!t0              st        d      4j'                  d      }"|"2t/        |"t0              rtG        |"      tG        |!      k7  rt        d      4j'                  d      }#|#|#|k7  rt        d      4j'                  d       |k7  rt        d!      4j'                  d"      |k7  rt        d#      4j'                  d$      }$|$t/        |$t0              rtI        |$      h d%k7  rt        d&      |jK                         D ]i  \  }%}|j'                  d'      }&|$j'                  |%      }'t/        |&t0              r(t/        |'t0              rtG        |'      tG        |&      k7  s`t        d(       4j'                  d)      3t/        3t0              r9tI        3      h d%k7  s)tM        34fd*d+D              s3j'                  d      |k7  rt        d,      d+D ]   }(t9        3j'                  |(      |( d-       " |jK                         D ]  \  }%}|% d.})|j'                  d/      |k7  s|j'                  d0      |j'                  d0      k7  s|j'                  d1      |j'                  d1      k7  s|j'                  d2      |j'                  d2      k7  s]|j'                  d3      |k7  sI|j'                  d4      4j'                  |)      k7  s&|j'                  d5      4j'                  |% d6      k7  rt        d7      |j'                  d'      }&t/        |&t0              st        d8      |%dk(  rtE        |&|%      }*|&j'                  d9      d:usr|&j'                  d3      |k7  s^|&j'                  d      |k7  sJ|&j'                  d      4j'                  d      k7  s'|j'                  d4      |k7  s|*|!k7  stO        |*      |k7  rt        d      |&j'                  d      }+t/        |+t*              st        d;      tQ        |+       tS        |&|%|      \  }},}-|,|j'                  d5      k7  s|-|j'                  d4      k7  rt        d<      tU        |
|%d       }.tS        |.|%|      \  }}/}0|/|j'                  d5      k7  sM|0|j'                  d4      k7  s9|.j'                  d      |&j'                  d      k7  s|.j'                  d3      |k7  st        d<       tM        67fd=d>D              s#7j'                  d)      4j'                  d)      k7  rt        d      tG        4      }1tG        |!      |1d<   ||1d<   |jK                         D %ci c]3  \  }%}t/        |j'                  d'      t0              r|%tG        |d'         5 c}}%|1d$<   	 tW        jX                  |1      }2|2j\                  tO        |2j_                               k7  s9|2j\                  |k7  s*|2j`                  |2jb                  |k7  s|2jd                  |k7  rt        d@      |2S c c}w c c}w c c}w c c}w # t        t>        t@        tB        f$ r} t        d      | d } ~ ww xY wc c}}%w # tZ        t@        tB        f$ r} t        d?      | d } ~ ww xY w)ANc              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z5_load_adaptive_registration_inputs.<locals>.<genexpr>i  !     I,$0A0AS0H,ru   rv   rJ  c              3  T   K   | ]  }|j                   j                  k(  r| ! y wrk   rO  rn   s     rr   rs   z5_load_adaptive_registration_inputs.<locals>.<genexpr>x  +      	
Myy%%, M   %(rR  .adaptive registration transition is incompleterm   r*  z5adaptive registration journal has duplicate revisions1approved adaptive registration inputs are missingrP  %adaptive registration config is staler'  rm  rw   rx   rD   rk  'adaptive registration revision is staler   v1r  rh   r  z+adaptive meal constraints artifact is staler  z-adaptive meal constraints artifact is invalidderived_constraintsz3adaptive registration derived constraints are stalederived_constraints_digestz9adaptive registration derived constraints digest is stalemeal_constraints_digestz6adaptive registration meal constraints digest is stalemeal_constraints_versionz7adaptive registration meal constraints version is staleartifact_documents>   r  r  r  .adaptive registration artifacts are incompleter1  z)adaptive registration artifacts are staler-  c              3  j   K   | ]*  }j                  |      j                  | d       k7   , yw)_digestNr  )ro   rf  r-  r'  s     rr   rs   z5_load_adaptive_registration_inputs.<locals>.<genexpr>  s;      
F   &.*<*<vW=M*NNFs   03r  z0adaptive registration artifact digests are stalez	 artifactrC  r   r   r   r   r   r/  r0  _versionz+adaptive input approval artifacts are staler  r   Tz6adaptive meal constraints artifact metadata is invalidz)adaptive input approval artifact is stalec              3  d   K   | ]'  }j                  |      j                  |      k7   ) y wrk   r  )ro   r  latestlatest_configs     rr   rs   z5_load_adaptive_registration_inputs.<locals>.<genexpr>:  s6      
E 	% FJJu$55
s   -0)rm   r   r*  r+  r,  r   r   r   r   r   rh   1approved adaptive registration inputs are invalidr  )3r   r   r   r   rC  r   rD  r   r   r   r
  r   r!   r5  r   r;  rZ  ry  r  r  rH  r   r  r   r   r  r   r   rf  r	  r  r  rc  r4  r  rL  r  rs  rJ  r   r   r  r  r   r  r   rD   value_payloadr<  r=  r>  )8r   rm   rM  r   r   r   r   r   r   r   adaptive_rootr   r!  r  r  r"  r#  r?  rg  r$  r%  r\  r  selected_configsapproval_rowsapproval_artifactsowner_payloadr*  derived_payloadexpected_meal_documentexpected_meal_versionexpected_meal_digestr   expected_meal_valuesupplied_derivedsupplied_derived_digestinput_artifactsr.  r1  supplied_documentrf  digest_fieldr  r  row_versionr&  current_documentcurrent_versioncurrent_digestauthoritative_documentrn  r-  r'  rq   rF  rG  s8                                                      @@@@@rr   "_load_adaptive_registration_inputsr^  [  sw
    !l!34D

-C*40M#D-8%mT:H)$xHI(,,I4H  #5cU!;<< G
 	
 #E	#I 	
.}dCMM	

 	G  !KLL.w/@/@AM4G4V1NK'7M%k2K=mLIw'I+'V$n. !QRR$-PIS1HC1OSIMP''377>+Bc+I'   }E}C)*+}EF#K 	9IJ9I#c#''+,-9IJ	KsP 
 !C
 	
  0 !TUU2F$R(M'6M7F$%&
 *+vzz:K/LL !HIIZZ 01Nng. !RSS#!!v!.	 NN--6-:M*>+=+=h+GTO&**%677 !JKKY5 2 29d CD
 3# #N3D$E F 	L 57K 7 )73 !PQQ%))*?@#'1 !T*=%>> !VWW,001MN+#';; G
 	
 348LL D
 	
 459NN E
 	
 %(()=>O"/73s?7K P
 8

 %%UVV"4":":"<M3 #(; < / 3 3M B0':!"3W=)*d3D.EE()TUU #= &))*<='1 $RR 
F
 
  237KK !STT>-11$7D69KL ?0668s'0GG&'+;;ww()Y]];L-MMww~&)--*GGww23}}89:ww}%6ww()^-?-?-MMww)*!!]O8"<=> %%RSSGG$78+W5$%RSS..9!N
 "%%j1=$((7=H$((37LL$(():;!%%&78977,-1EE!%88">26JJ()VWW+//0ABKk3/(L  (4%M&
";

 #''"455swwH
 :
 %%PQQ=

 .V.
*?N sww'9::):!;;##$56 $$%678##M2mC$%PQQE 9F  
  
		-	..2D2D3 
 !!HII!.1489L4M01;O78 #5":":"<4"<M3cgg12G< 	tC 3455"<4/0
+::;QR 	)=)=)?@@==O+%%-,,0DD))-AA G
 	
 My Q FJX )Z@ Y !NOUXXYj4 "9j9  ?
	s[   f(f2ff!f!f-Af" 8gg "g;ggg:)g55g:)rh   r+  r  r   r  r  r  c       	            t        t        |             }t        |      5  t        ||||||||||	|
      cd d d        S # 1 sw Y   y xY w)N)	r  r   rh   r+  r  r   r  r  r  )r   r   r   ,_approve_adaptive_registration_inputs_locked)r   rm   r  r   rh   r+  r  r   r  r  r  r   s               rr   $approve_adaptive_registration_inputsra  h  sP     !l!34D		%;#+/#%-
 
&	%	%s   A  A	c       	          + t        t        |             }t        |      +t        |      }t	        ||       t        ||      }t        |||       t        +fd|j                  D        d      }|t        d+       |j                  st        d      t        ||||      \  }}t        |      }||j                  j                  d      k7  rt        d      t        +fdt        ||      j                  D        d      }|t        d	      t!        |j"                        }	 t%        |      5  t'        |      \  }}}}t)        |d
      }t+        |      \  }}|D cg c]  }|j-                  d      +k(  s| }}|rt/        |d   d         nt0        }|rt/        |d   d         nd}dt3        |      dz    }t5        |+|      }t7        ||j8                  j:                         |}|.t=        |t>              r|j-                  d      ntA        |dd      }|dv r|n|}||k7  rt        d      |d   |k(  r|rtC        |      |k7  ri |d|i}tE        |      } tG        ||||| |      }!|}"tI        d ||||	|
fD              rt=        |t>              rtK        |      }#nAtA        |dd      }$tM        |$      r
 |$dd
      ni }%t=        |%t>              rtK        |%      ni }#|||#d<   |
|
|#d<   |||#d<   |||#d<   |	|	|#d<   |#}"tO        |||"||        }&i |!|&d!   d"   |&d!   d#   |&d   d"   |&d   d#   tK        tQ        |&d   d   d            |&d   d#   |&d"   d"   |&d"   d#   |&D '(ci c]
  \  }'}}}(|'|( c}(}}'|&D ')ci c]
  \  }'})}}|'|) c}})}'d$
}!	 tS        jT                  |!      j                  dd
      }!t]        ||!       ddd       ta        |!&||       tS        jT                  |!      S c c}w c c}(}}'w c c}})}'w # tV        tX        tZ        f$ r}*t        d%      |*d}*~*ww xY w# 1 sw Y   hxY w# t        $ r  t^        $ r}*t        d&      |*d}*~*ww xY w)'zAApprove and append one complete customer adaptive-input revision.c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z?_approve_adaptive_registration_inputs_locked.<locals>.<genexpr>  r2  ru   Nrv   rJ  rw   rx   3adaptive registration approver does not match ownerc              3  T   K   | ]  }|j                   j                  k(  r| ! y wrk   rO  rn   s     rr   rs   z?_approve_adaptive_registration_inputs_locked.<locals>.<genexpr>  r4  r5  rR  TrL  rm   rP  r*  r   v0vr   r;  r+  Nr~   z7adaptive registration supersession predecessor is stale)r+  r   r   rh   r   c              3  $   K   | ]  }|d u 
 y wrk   r   ro   r1  s     rr   rs   z?_approve_adaptive_registration_inputs_locked.<locals>.<genexpr>  s!      	E T!rd  r   r  r  r  r   r  r  r  r   r  r5  )
base_policy_versionbase_policy_digestr?  r>  r<  r=  r  r  r-  r@  z2adaptive registration artifact metadata is invalidr.  )1r   r   r   r   rC  r   rD  r   r   r   r
  r   r?  r   r   r!   r5  r   r  r;  rZ  ry  r  r   rq  rH  r	  r  r  r  r   r   r  r   r   r  rJ  rL  r  r  r  r   r  r   rc  r4  r,  r   r  ),r   rm   r  r   rh   r+  r  r   r  r  r  r   r   r   r   r   r   r   r   rJ  r   r  	revisionsr?  rg  r\  selected_committedlatest_digestlatest_versionr  r  raw_supersedesapproval_timeregistration_documentartifact_inputsartifact_payloadr   r  r  rf  r/  r1  r   rq   s,                                              @rr   r`  r`    s     !l!34D

-C*40Mm4%mT:H)$xHI(,,I4H  #5cU!;<< G
 	
 #E	#I  ,E))v)66 !VWW	
.}dCMM	

 	G  !KLL.w/@/@AM}U.&9-&H#NAq!+NDII!Ei!PIw("(CGGN,Cs,Jy  "
 & &r*+<=>.  ;M&r*956RV  !"#&8"9A"=!>?O1_G ;$$ /N% "&'2 JJ23 )<dC  "0:!=>  !M1(M  y!^38J"7+}<!!?G 8HM$:"3#!1 -!%! '-O 	   $	 	 fg.'+F|$!(t!DJ $J/ #TB  )367(CV % *6A$]3#/;K$%78%17$X.+7C$^4&29$Y/"22 -F%'%'-ay|&,Qil,21IaL+1!9Q<'+01?QR( /5Qil#)!9Q<"()A,MS%MS.IdAq/D/)V% RX'QW0M6GAD++QW'%!&(B(Q(Q))*&t*< & 1@UVq /z # &445JKKA"J%' *9jA (Hi /.r   U !JKQTTUs   )Q 40Q $P>PG4Q 6P

Q PQ '&PQ Q Q P=,P88P==Q  Q	Q Q/Q**Q/c       	        .    t        | |||||||||	|
      S )zFCreate a superseding approved revision; no in-place update is allowed.)	r  r   r+  rh   r  r   r  r  r  )ra  )r   rm   r  r   rh   r+  r  r   r  r  r  s              rr   #update_adaptive_registration_inputsrw  B  s2      0+'!) r   c               >  4 t        t        |             }t        |      4t        |      5  t	        |      }t        ||       t        ||      }t        |||       t        4fd|j                  D        d      }|t        d4       |j                  st        d      t        ||||      \  }}t        |      }	|	|j                  j                  d      k7  rt        d      t        4fdt!        ||      j                  D        d      }
|
t        d	      t#        |
j$                        }t'        |      5  t)        |      \  }}}}t+        |d
      }t+        |d
      }t+        |d
      }t-        |      \  }}t/        |      \  }}|s|rt        d      |D cg c]  }|j1                  d      4k(  s| }}|D cg c]  }|j1                  d      4k(  s| }}|r|st        d      |d   }|d   }t3        |d         }|j1                  d      |k7  rt        d      |j1                  d      }t5        |t6              st        d      t9        |4t3        |j1                  dd                  }t;        |      |k7  rt        d      |j1                  d      }|j1                  d      } h d}!t5        |t6              r t=        |      |!k7  st5        | t2              rt;        t?        |            | k7  s|j1                  d      |	k7  s|j1                  d      |	k7  s|j1                  d      |d   k7  s|j1                  d      |j1                  d      k7  si|j1                  d      |j1                  d      k7  sF|j1                  d      |j1                  d      k7  s#|j1                  d       |j1                  d       k7  rt        d!      tA        |4|      }"|j1                  d"      }#t5        |#t6              rt=        |#      h d#k7  rt        d$      |j1                  d%      }$t5        |$t2              st        d&      tC        ||i |	|$'      }%|%D &'()ci c]  \  }&}'}(})|&|'|(|)f }*}(}'}&})|"jE                         D ]
  \  }&}|*|&   \  }+},}-|j1                  d      | k7  s|j1                  d      |j1                  d      k7  s|j1                  d      |j1                  d      k7  s|j1                  d       |j1                  d       k7  su|j1                  d      |	k7  sa|j1                  d(      |#j1                  |&      k7  s>|j1                  d(      |-k7  s*|j1                  d)      |,k7  s|j1                  d*      |+k7  st        d+       | |k(  r(	 tG        jH                  |      cddd       cddd       S tQ        |      d-z   }/|D ch c]  }t3        |d          }0}	 i |dd.|/ i}1t;        |1      |0vrn|/d-z  }/|*d/   d0   }2|*d1   d0   }3ddd       tS        |41|	232      cddd       S c c}w c c}w c c})}(}'}&w # tJ        tL        tN        f$ r}.t        d,      |.d}.~.ww xY wc c}w # 1 sw Y   cxY w# 1 sw Y   yxY w)3zJAppend an authority-only child approval after activation receipt rotation.c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z9reapprove_adaptive_registration_inputs.<locals>.<genexpr>q  rt   ru   Nrv   rJ  rw   rx   rd  c              3  T   K   | ]  }|j                   j                  k(  r| ! y wrk   rO  rn   s     rr   rs   z9reapprove_adaptive_registration_inputs.<locals>.<genexpr>  s+      QD99))S0 Qr5  rR  TrL  r6  rm   r7  rP  r*  r8  r'  rm  r   r:  r;  r9  r   r   >   r   rm   r   r   r   r,  r   r   r   r   r   r,  r   z-adaptive registration owner authority changedr-  >   r  r  r  rA  rh   rA  r  r/  r0  r1  z/adaptive registration approval artifact changedrH  r   rg  r  r   r  )r  r   r+  r  r  )*r   r   r   r   r   r  r   rD  r   r   r   r
  r   r?  r   r   r!   r5  r   r  r;  rZ  ry  r  r  r   r   r   r	  r   r  rL  r  r  rs  r   r  r   rc  r4  rH  r`  )5r   rm   r   r   r   r   r   r   r   r   r   rJ  r   r!  r  r  r"  r#  rL  r?  rg  r$  r%  r\  r  rK  rF  rG  ro  r'  r  old_authorityold_authority_digestexpected_authority_fieldsrM  r-  rh   r  rf  r1  r   r/  bundle_by_kindexpected_documentexpected_versionr-  r   r~  seen_digestsrebound_payloadr  r  rq   s5                                                       @rr   &reapprove_adaptive_registration_inputsr  a  s]    !l!34D

-C		%.t4*4?)->-dM8LMh00M
 $'9#%?@@$K  'I	'
#	# $K0HNN--6-::$E  2=$GQQ
 
 ?$%OPP273D3DE.<O=9NK 0MM-k4HK/tLM!Em!TIw/Q0,n .(D  -6XIS9PTW9WSIMX/ /377>3Jc3Q/    !(8(G  #2&F,R0M'8 9:M  !23}D()PQQ#ZZ(89Nng6(E  2 #N$6$6y$$G HG
 w'=8()RSS*..{;M#1#5#56H#I )% }g6}%)BB!"6<"4#67;OO!%%m4= $$W-6 $$^4	.8QQ!%%n5 $$^45!%%&78 $$%678!%%&=> $$%<=>!%%&AB $$%@AB )C  "@"
  .112DE.8C@P<Q V =
 )D  -001BCOos3(D  3 /F JPIOED+Wo ('?CCIO   0557	cGUHD!#3_ GG./3GGww01$(():;<ww~.-2C2CN2SSww:;$(()DEFww}-6ww015E5I5I$5OOww01_Dww126FFww237HH,I # 8( $'775DD^Tw /.K 
&	%J M*Q.ICPQ=CC$5 67=LQ##9+# #?3<GQ	  )7:K))4Q7LW /X <"+#%
c 
&	%h Y b6 .y*E ,K
 RC /.K 
&	%s   DZ?A'Z&Y YZ
Y$Y(JZ*Y<DZZ%Y9	ZZZ32Z%ZZY?.Y::Y??ZZ	ZZc                    t        t        |             }t        |      t        |xs d      j	                         t              dk7  st        d D              rt        d      t        |      }t        fdt        |      j                  D        d      }|t        d      t        |j                        }t        |      5  t        |      }t!        ||      }t        fd|j                  D        d      }	|	|	j"                  st        d	      t%        ||||	      \  }
}|j'                  d
d      }t)        ||
||j*                  j'                  d
             t-        |      5  t/        |      \  }}}}t1        t3        |            \  }}t5        t3        |            \  }}|s|rt        d      |D cg c]  }|j7                  d      k(  s| }}|D cg c]  }|j7                  d      k(  s| }}|r|st        d      |d   }|d   }|j7                  d      |j8                  k7  s-|j7                  d      |j8                  k7  s|j:                  k7  rt        d      t        fd|D        d      }t=        |t>              r|j7                  d      nd}t=        |t>              st        d      tA        |t        |j7                  dd                  }tC        |      k7  rt        d      |jE                         }|jG                  dd       |jG                  dd       ||k7  s_|j7                  d      |j7                  d      k7  s<|j7                  d      |jH                  k7  s|j7                  d      |jJ                  k7  rt        d      tM        t3        |      |j8                         ddd       ddd       yc c}w c c}w # 1 sw Y   xY w# 1 sw Y   yxY w)zCProve the current registration is an authority-only child revision.r~   r`  c              3  $   K   | ]  }|d v 
 ywrb  r   rE  s     rr   rs   z<validate_adaptive_registration_reapproval.<locals>.<genexpr>1  s      %=H		++[rd  z3adaptive registration predecessor digest is invalidc              3  T   K   | ]  }|j                   j                  k(  r| ! y wrk   rO  rn   s     rr   rs   z<validate_adaptive_registration_reapproval.<locals>.<genexpr>7  s+      	
Fyy%%, Fr5  NrR  c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z<validate_adaptive_registration_reapproval.<locals>.<genexpr>E  rt   ru   rJ  rw   Tr  rx   r6  rm   r7  rP  r*  z1adaptive registration reapproval lineage is stalec              3  J   K   | ]  }|j                  d       k(  r|  yw)r*  Nr  )ro   r\  predecessors     rr   rs   z<validate_adaptive_registration_reapproval.<locals>.<genexpr>z  s+      ,ww01[@ ,s    #r'  z;adaptive registration reapproval predecessor is unavailabler   r:  r;  z5adaptive registration reapproval predecessor is staler   r   r-  z9adaptive registration reapproval changed approved meaning)'r   r   r   r   r   rH  rJ  r   *load_approved_adaptive_registration_inputsr   rT  r   r5  r   r   r   r   r
  r   r   r  r   r  r;  ry  rZ  r  r  rD   r+  r   r   r	  r   rI  r  r   r-  r  ) r   rm   predecessor_digestr   r{  r   rJ  r   r   r   r   r   rZ  r   r!  r  r  r?  rg  r$  r%  r\  r  rK  rF  rG  r  previous_documentr  current_payloadrq   r  s                                  @@rr   )validate_adaptive_registration_reapprovalr  '  s=    !l!34D

-C(.B/557K
;2 %=H% " !!VWW8sCG	
6t<FF	

 	G  !KLL.w/@/@AM		%.t4)->Mh00M
 8#3#3$K  'I	'
#	# #--6-M'NN%%6%2		
  .<O=9NK "F#N3"Iw 0R#K00,n .(D  -6XIS9PTW9WSIMX/ /377>3Jc3Q/    !(8(G  #2&F,R0M

,-? $$%677>>I,,;(G  ,
 H h0 -. 
 /9(Q   ;! #$5$9$9)T$J K 
 /0K?(K  &335O  D1	40 O3$((7#''67$((8G<P<PP$(();<@X@XX(O  +#M2O /1 
&H } Y  /.1 
&H sQ   B'O42AO(OO"O((O#O#GO(O4
O((O1	-O44O=c                    t        | |      S )a  Load the latest approved inputs and their registration-bound artifacts.

    The returned value exposes ``registration_digest``,
    ``derived_constraints``, and ``derived_constraints_digest`` together;
    policy and catalog remain separately owner-approved artifacts.
    )r^  )r   rm   s     rr   r  r    s     .lLIIr   analytics_shadowoperator_candidatesr)  r*  morning	nutritionworkoutsafety_auditlegacy_combinedhistory_import)morning_checkinnutrition_checkinworkout_recordr  check_in_validatedhistory_importedc                6   t        | t              r| }nt        | t              rr|j                  | j                  j                  d      | j                  D cg c]  }|j                  j                  d        c}d}t        j                  |      }n\t        | dd       }t        |      r	 |d      n| }t        |t              st        d      t        j                  t        |            }t        |      t        |      k7  rt        d      |S c c}w )Nrw   rx   )r   r   r   r   zregistry evidence is invalidzregistry evidence is stale)r   r   r    r   r   r   r   r  r  r  r  r   r   rL  rQ  )r   liver~  r   r  r   rU  s          rr   !_reconciliation_registry_documentr    s     (,-		H.	/||^^..F.;DLDVDVDV''V'4DV
 %33G<	X|T:
)1*)=jf%8#w'$%CDD$33DI>	Y'+@+FF !=>>s   #Dc               b   t        |t              st        d      |j                  j	                         s+|j                  j                         |j                         k7  rt        d      d }| }t        | t        t        f      rt        |       }d }nEt        | t              rt        t        | d|            }d }nt        | dd       }|t        |      }d }|<|j	                         s!|j                         |j                         k7  rt        d      	 t        d |j                         j                  D              }||}n)t        |t               rt#        |      f}n	 t        |      }	 t%        |      }	t%        |      }
t        d |	D              t        d |
D              k7  rt        d      t        d	 |
D              S # t        t        t        f$ r}t        d      |d }~ww xY w# t        $ r}t        d      |d }~ww xY w# t        t        f$ r}t        d      |d }~ww xY w)
Nz)canonical transaction evidence is invalidz1canonical EventStore path does not match customer_eventsc              3  B   K   | ]  }|j                  d d        yw)rw   Tr  N)r   )ro   r  s     rr   rs   z1_reconciliation_source_records.<locals>.<genexpr>   s(      
; &t<;   #canonical event evidence is invalidc              3  2   K   | ]  }t        |        y wrk   rB   ro   rp   s     rr   rs   z1_reconciliation_source_records.<locals>.<genexpr>  s     ?.>d^D!.>   c              3  2   K   | ]  }t        |        y wrk   r  r  s     rr   rs   z1_reconciliation_source_records.<locals>.<genexpr>  s      I):t):r  z!canonical event evidence is stalec              3  2   K   | ]  }t        |        y wrk   )rL  r  s     rr   rs   z1_reconciliation_source_records.<locals>.<genexpr>  s     :(9d(9r  )r   r;   r   rs  r   rS  r   r   r<   r  r  read_snapshoteventsr   rc  r4  r   rL  rA   )canonical_eventsexpected_pathry  ru  source_valuescandidate_path	persistedr   r  supplied_recordspersisted_recordss              rr   _reconciliation_source_recordsr    s?    k#<= !LMM**,""**,0E0E0GG !TUU#K,M"S$K0+,	$j	17#3YNO !19dC%~.K M!!#{':':'<@U@U@W'W$C Q 
$224;;
 
	 	M7	+')	U]+HQ28<3I> ?.>??5 I):I D  !!DEE:(9:::) Y
+ Q !FGSPQ  	U$%JKQTT	U
 z" Q !FGSPQsH   *G	 %G1 1H 	G.G))G.1	H:HHH.H))H.c           	        | j                  d      }| j                  d      }| j                  d      }t        |t              r&|r$t        |t              r|rt        |t              r|st        d      | j                  d      }d }t        |t              r|j                  d      }|t        |t              st        d      t        |t              r|j                         nd}|j                  d	      }	t        |	      d
k\  r|	d   dv r|	d   |k7  rt        d      | j                  d      d |fvrt        d      dD ]H  }
| j                  |
      }t        |t              s%|j                  d      }|9||k7  s?t        d       	 t        j                  |      }|j                  |j                  t              }|j                  t              j                         }|xs t        | j                  d      xs |      }|j                         st        d      ||t"        j                  ||      |||t%        |       dS # t        t         f$ r}t        d      |d }~ww xY w)Nrm  
event_typeoccurred_at_kstr  
provenance
source_refz%canonical event provenance is invalidr~   r  r  r   >   pilotr   r   z)canonical event customer scope is invalidrm   )check_inr  r  r  r  r8  z$canonical event timestamp is invalid
dedupe_keyz+canonical event session identity is invalidroot_event_idrm   mapped_flowobservation_kst_day
session_idwriter_epochroot_preimage_digest)r  r   r   r   r   r   splitrH  r	   r3  r9  r   r;  r<  r   rc  r4  _RECON_EVENT_FLOWSr   )r  rm   epochrm  r  occurredr  r  source_ref_valuesource_partspayload_namer  payload_customerrG  observation_dayr   r  s                    rr   _reconciliation_event_planr    sR   
 zz*%HL)Jzz+,Hx%*c*(C( !FGGL)JJ*g&^^L1
jS&A !HII-7
C-Hz'')b#))#.LLQO44O|+ !LMMzz.!$)== !LMM **\*gw'&{{>:+0@L0P()TUUR**84	#!)))6I#..t499; "NSL)A)MX%NJ !NOO!$)--j*E.  / 7  z" R !GHcQRs   AI I-I((I-c                   | j                         s | j                         r| j                         st        d      	 t	        j
                  | j                  d            }t        |t              st        d      t        D ci c]  }||j                  |       }}t        |      dddht        k7  s|j                  d      dk7  st        |j                  d            t         usm|d   d	k  set#        d
 |j%                         D              sEt        |j                  d      t&              r&t)        |d         dk7  st#        d |d   D              rt        d      t+        |d   |      }|d   |k7  rt        d      |d   |d   |fS # t        t        t        j                  f$ r}t        d      |d }~ww xY wc c}w )Nzfeature config is unavailabler   r  zfeature config is invalidr   r  config_digestro  r   c              3  >   K   | ]  }t        |      t        u  y wrk   r   r  ro   flags     rr   rs   z1_reconciliation_feature_config.<locals>.<genexpr>m  s     A.$tDz%.   r`  c              3  $   K   | ]  }|d v 
 ywrb  r   rE  s     rr   rs   z1_reconciliation_feature_config.<locals>.<genexpr>p  s      
AWII//AWrd  zfeature config digest mismatch)r   rU  rt  r   rw   r  r  r   rx  r  r   r   _RECON_FEATURE_FLAGSr  r  r   r   rJ  ro  r   rH  rE   )r   r1  r   r  r  rx  s         rr   _reconciliation_feature_configr  ]  s   T\\^ !@AAG

4>>7>;< eW% !<==/CD/CtT599T?"/CEDE
gN9MN	O99%&%/		'"#3.>AA%,,.AA%))O4c:u_%&", 
AFAW
 
 !!<==$U7^U;H_) !ABB>51588- ')=)=> G !<=3FG Es   %F GF?.F::F?c                *    t        t        |             S rk   )r   r  )rV  s    rr   _reconciliation_journal_digestr  {  s    5;''r   c                  d|fd|ffD ]C  \  }}t        |t              r!t        |      dk7  st        d |D              s7t	        | d       | j
                  j                  d      }t        |      h dk7  s t        d	 |j                         D              rt	        d
      d|t        |      t        |       ||d}t        |      t        |      fS )Nzactivation receiptr  r`  c              3  $   K   | ]  }|d v 
 ywrb  r   rE  s     rr   rs   z2_reconciliation_owner_authority.<locals>.<genexpr>  s     N99$66rd  z evidence is invalidrw   rx   r=  c              3  d   K   | ](  }t        |      t        uxs |j                           * y wrk   )r   r   r   rj  s     rr   rs   z2_reconciliation_owner_authority.<locals>.<genexpr>  s.      ?AOU33ekkm"33   .0z&owner authority evidence is incompletero  )r   rm   r   r   r   consent_digest)r   r   rH  rJ  r   r   r   r  ro  rL  rQ  r   )r   rm   r   r  ro  r1  r   r   s           rr   _reconciliation_owner_authorityr    s     
89	N#u
 5#&5zRNNN$w.B%CDD NN%%6%2E
5z773 ?AF? < !!IJJ$e0:%>(I ;	222r   c                   t        t        |             }t        |      5  t        ||||      cd d d        S # 1 sw Y   y xY w)N)r  r   )r   r   r   -_reconcile_adaptive_nutrition_journals_locked)r   rm   r  r   r   s        rr   %reconcile_adaptive_nutrition_journalsr    s;     !l!34D		%<-	
 
&	%	%s	   9Ac               >  &456 t        t        |             }t        |      t        |      }t	        ||       t        ||      }t        |||       t        ||      }t        fd|j                  D        d      }t        t        d |j                  D                    }	||j                  r|	vrt        d      	 t        ||      }
t        fd|
j                  D        d      }||j$                  j'                         rt        d      |j$                  j)                         }|j+                         r-|j-                         r|j/                  |      rt1        ||      rt        d      |dz  d	z  }t3        j4                  |      }t7        ||||      }t9        d
|i      }t9        |j:                  j=                  d            }t?        |||      }tA        |||      \  }}t        fd|D              }|dz  }|j'                         s |j+                         r|j-                         st        d      	 tC        |       |dz  }|j+                         }|rtE        |       tG        jH                  |      }|j+                         r:	 |jK                          |jM                          dD ]  }|jO                  |        	 tS        |       tE        |      \  5}}t        5fd|D              }|	D ci c]  }|d c}|	D ci c]  }|d c}d}|jO                  d      D ]  &&jU                  d      d5 k7  r&jU                  d      } | |v s1&jU                  d      5k7  s^&jU                  d      |k7  sJt        &jU                  d d!            |	k7  s,&jU                  d"      ||    k7  s&jU                  d#      |k7  st        d$       	 |jW                          |jY                         jZ                  }!|jM                  d%&      }"|jO                  d'd%&      }#|jO                  d(d%&      }$|jO                  dd%&      }%|"D &ci c]  }&t]        |&d*         |& }'}&|D ]  }(|'jU                  t]        |(d*               })t_        |)t`              r9tc        |)jU                  d+            td        u r|)d+   d,k\  r|)jU                  d+      n|(d+   }*|jg                  t]        |(d*         t]        |(d-         t]        |(d.         |(d/   t]        |(d0         |*t]        |(d1         2      }+|+|'t]        |(d*         <   d3|+d4    },d5D ci c]  }||+|   
 c}6|#D &cg c]  }&|&jU                  d      |,k(  s|& }-}&|-D ]6  &&jU                  d      d6vrti        6&fd76D              s-t        d8       ti        d9 |-D              s& |jj                  |,fi 6  |jl                  |,fi 6 |jO                  d'      }# |jY                         jZ                  }!d: d;| }.d<d= |to        jp                  |jr                  jt                  tv        jx                  tz        >      j}                         |!r|!d?   d@   nd,|t        |      ||dA
4|$D &cg c]  }&|&jU                  d      |.k(  s|& }/}&|/D ]7  &&jU                  d      dk7  rti        4&fdB4D              s.t        dC       ti        dD |/D              s|j                  |.t]        4dE         t]        4dF         |t]        4dG         te        4dH         dI|t]        4dJ         t]        4dK         t]        4dL         M       |j                  d(4|.dN       d5 }0|jO                  d      }%|%D &cg c]  }&|&jU                  d      |0k(  s|& }1}&|1D ]  &&jU                  d      dk7  rt        &jU                  d d!            }2&jU                  d"      }3&jU                  d      5k7  sB&jU                  d      |k7  s.|2|	k7  s)|3|	D ci c]  }|d c}k7  s&jU                  d#      |k7  st        d$       ti        dO |1D              sL|j                  5||	dI|	D ci c]  }|d c}|P       |j                  5||	d|	D ci c]  }|d c}|P       |jO                  d      }%|jM                         }"|jO                  d(      }$|jY                         jZ                  }!|jO                  d'      }#dQto        j                  tz              j}                         5|t        |!      t        |!      dRt        |"      t        |"      dRt        |#      t        |#      dRt        |$      t        |$      dRt        |%      t        |%      dRdS
S # t        t         t"        f$ r}t        d      |d}~ww xY w# t        $ r}t        d      |d}~ww xY w# t         tP        t"        f$ r}t        d      |d}~ww xY wc c}w c c}w # t         tP        t"        f$ r}t        d)      |d}~ww xY wc c}&w c c}w c c}&w c c}&w c c}&w c c}w c c}w c c}w )TzIReconcile adaptive projections from one live canonical customer boundary.c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  r2  ru   Nc              3  N   K   | ]  }|j                   s|j                    y wrk   )r
  rm   r  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  s     P-?T4<<t  -?s   %%z4customer must be enabled for adaptive reconciliationz'registry failed adaptive reconciliationc              3  V   K   | ]   }|j                   j                  k(  s| " y wrk   rO  rn   s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  s%     S1$TYY5K5Ks5R1rQ  rR  wizardevents.jsonlr,  rw   rx   )ry  )r   r  c              3  8   K   | ]  }t        |d         ywr   N)r  )ro   r  rq   s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  s      AHv"632   r'  zadaptive runtime is invalidzBadaptive reconciliation requires approved complete customer inputsr(  )
source_dayr   config_epochzadaptive journals failed closedc              3  .   K   | ]  }i |d i  yw)r  Nr   )ro   rd  r  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  s     PKD7477Ks   rg  r?  )r6  r?  r  r%  zepoch:r'  r  r  customer_keysr   customer_stater   z,config epoch does not match enabled registryTrL  r  r   z adaptive journal recovery failedr  r  r   rm   r  r  r  r  r  zsource-day:
mapping_id)r  r  rm   r  r  r  r  r  >   r  r?  c              3  L   K   | ]  }j                  |      |   k7    y wrk   r  )ro   r  intent_payloadr\  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>W  s$     Wu3775>^E%::   !$z(source-day intent does not match mappingc              3  B   K   | ]  }|j                  d       dv   ywr'  >   r  r?  Nr  ro   r\  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>Y  s$      
FVsCGGG ::FVr  z
authority:r  r   zowner-authority:r8  rP  r  )
authority_kindcanonical_fact_idcanonical_fact_digest
valid_fromadaptive_sequencerm   r   r   r   r  c              3  V   K   | ]   }|d k7  rj                  |      |   k7   " yw)r  Nr  )ro   r  authority_payloadr\  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>x  s5      
*++ GGEN/66*s   &)z.authority mirror does not match owner evidencec              3  B   K   | ]  }|j                  d       dv   ywr  r  r  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>~  s$      BS366BSr  r  r  r  r  r6  r   r   r  )r%  r  r  r  r  r  r'  rm   r   r   r   r  )r%  r'  c              3  D   K   | ]  }|j                  d       dk(    yw)r'  r?  Nr  r  s     rr   rs   z@_reconcile_adaptive_nutrition_journals_locked.<locals>.<genexpr>  s!     K:J3swww;.:Js    )r'  customer_statesr   ro  )countrD   )
r   rm   reconciled_at_kstr  r  canonical_sequencesource_day_mappingssource_day_intentsauthority_mirrorr  )Fr   r   r   r   rC  r   rD  r  r   r   r  sortedr
  r   r!   r   r   r4  r   r   rS  rU  rV  rW  rX  r;   rr  r  r   r  r   r  r  r^  r  r?   r  readsource_day_rowsjournal_rowsrx  rF   r  rM  r  sequence_rowsr   r   r   r   r   append_source_day_mappingrJ  prepare_source_daycommit_source_dayr	   combinerd  r   r
   minr;  r  rQ  append_authority_mirrorappend_journalappend_config_epochr:  rH  r  )7r   rm   r  r   r   r   live_documentevidencer   enabled_keyslive_registryr   r   customer_rootcanonical_pathcanonical_transactionr,  r   r  recordsr   authority_fact_digestevent_plansrJ  feature_pathfeature_existsstorerf  r  r  rq   expected_config_statesexpected_stater  mapping_rowssource_intent_rowsauthority_rowsr#  r\  mappings_by_eventrd  r&  mapping_epochmappingr%  existing_intentsauthority_intent_idauthority_intentsconfig_intent_idconfig_for_epochobserved_keysobserved_statesr  r  r  s7                                 `       `             @@@rr   r  r    s    !l!34D

-C*40Mm4*=$?M)$}M0=IHI(,,IH PX-?-?PPL x//3l3J !WXXU.}dC S-11SG '++668 !KLL%%--/M  "##%++D1]D1 !KLL"X->N5JJ7SA	 !0#%:	
!
 %&&11v1>N -)G
 $C";%	$ E   AH K "$55M!}';';'= !>??*45
 !#77L!((*N&|4--g6E	QJJL!!#C""4( D !/<\JE=!PKPPK/;<|S)^|<2>?,3c;&,? !!.177;VE7#33)33GGG%ww'=8SWW_b12lBww'(,B>,RRww}%.$%STT 2N%%'-;;=KK,,T,:"//d/K++K+F(((F DPP<CS12C7<P$((T/-B)CD (G,X\\.12c9(A- LL( n% 	 11d?34T.12D/0 $%: ;4-.&!$T*@%A!B 2 
 9@#d?345!',"7!89		
	 	
 .
-C1E1RC- 	 
 $Cwww'AAWWW()STT	 $
  
FV
 
 %E$$YA.A#E##I@@"//=] ` *779GGM&se1-B,CD!/u5!6&&MM##HH
 )+?LmB/
;RS0:%>(" &%)=AT)T~   !777{* 
*
 

 %%UVV !  BS  	%%)01ABC!"34G"HI"7,\:;!"34G"HI 12C DE&)!"=>' 01ABC 	& 	
  	)	 	 	
  w'$$^4K""cggk&:>N&N{    777{*cggor:;''"23GGG%ww'=8,|"L|3#3|"LLww}%.$%STT   K:JKK!!7CD|S)^|D 	" 	
 	!!9EF#S+-F 	" 	
 $$^4K((*L''4N)779GGM++L9%\\$/99;&'4]C

 &4\B 

 +,45GH

 (4^D

 %4[A
- i "7J7 U !JKQTTUj   P
	 +Z8 	Q$%FGSP	Q =?* '4 N !CD#MN Q*

BP #M E Gs   g 	h  8h 
i
i
(A5i !i7i<2jj,jj:jj
j'
j
jg=,g88g= 	h	hhi1h==ii4#i//i4extension_throughc                   t        t        |             }t        |      5  t        |||      cd d d        S # 1 sw Y   y xY w)Nr2  )r   r   r   *_prepare_adaptive_nutrition_runtime_locked)r   rm   r3  r   s       rr   "prepare_adaptive_nutrition_runtimer6    s8     !l!34D		%9/
 
&	%	%s	   8Ac          
        t        t        |             }t        |      t        |      }t	        ||       t        ||      }t        |||       t        |      }t        fd|j                  D        d      }||j                  j                  durt        d      |t        |      t        urt        d      |j                  j                  j                   }|t#        d      z   }	|t#        d      z   }
|	|cxk  r|
k  s*n ||t#        d	      z   k(  rt        d
      t        d      d}d}d}|t%        ||||j                        }|j&                  j(                  |j&                  j*                  |j&                  j,                  d}d|j/                         ||d}t1        j2                  t4              j/                         }	 t7        |j8                        }||S |J |J |J t;        |j8                  ||t=        |      ||j&                  j(                  |      }i ||S # t        $ r  t>        t@        tB        f$ r}t        d      |d}~ww xY w)zPPrepare private adaptive files and append an approved customer policy extension.c              3  V   K   | ]   }|j                   j                  k(  s| " y wrk   rO  rn   s     rr   rs   z=_prepare_adaptive_nutrition_runtime_locked.<locals>.<genexpr>  rP  rQ  NTz9customer must be enabled for adaptive runtime preparationz"adaptive extension date is invalidra  r_  S   T   zBadaptive extension D+85 is not allowed; valid window is D+29..D+84z0adaptive extension must remain within D+29..D+84r  ro  )r   rm   r3  r,  actor)rm   r3  r,  r   r   r   rh   r.  )"r   r   r   r   rC  r   rD  rT  r   r   r  r
  r   r   r   rd  r   r   r  r   r   r   r   r  r	   r:  r;  rF   r   r@   r   r   rc  r4  )r   rm   r3  r   r   r   r   r   r   minimummaximumr,  r   rh   r;  rn  extension_resultr   rq   s                     @rr   r5  r5    s    !l!34D

-C*40Mm4%mT:H)$xH-d3HN(,,NG ',,..d: G
 	
 $!"$.$%IJJLL%%//	iR00iR00+6w6 I	r0B$BB(X  %%WXX(,*.I"&O$ ELL	!
  ~~--~~-- //
 $!2!<!<!>%:
	 #,,t,668Q-g.?.?@$M$000$$$***;/"7,Y7 ..+	
 .&-,-- Y
+ Q !FGSPQs   H6 *AH6 6I$II$c                D   |j                         rt        d      |j                         }|j                         }|j                         r&|j	                         r|j                  |       r||k7  rt        d      t        ||       rt        d      t        |||       y )Nz%G1 data root symlinks are not allowedz8G1 data root does not match the registered customer root)r   r   rS  rU  rV  rW  rX  _audit_data_root)r   r\  r[  r   r  r  rx  s          rr   rJ  rJ  =  s       " !HII'')J&&(H  "((6! F
 	
 \2 !HIIZ40r   c                    |j                         s| j                  |      sy|}| j                  |      j                  D ]  }||z  }|j                         s y y)NTF)r   rW  relative_toparts)r   r   r{  parts       rr   rX  rX  V  s[     (;(;L(IG  .444 5 r   c                   t               t               d	 	 	 	 	 dfd}t        |j                  j                        } ||j                  |       |j
                  D ]~  }|j                  |j                  k(  rj                  |j                         t        |j                        dk\  rj                  |j                          ||j                  |        t        j                         }| j                  d      D ]  }|j                         rt        d      |j                         r/|j!                  |       j#                         j%                         t'        fdt(        D              }	 |j+                         t'        fdD              }
|s|
s	 |d uxr  ||      d	u }|r|rt        d      t        d       y # t,        $ r}	t        d      |	d }	~	ww xY w# t.        $ r d
}Y Gw xY w)N)allowed_componentsc                   | j                   D ]'  }t        |      dk\  s||vsj                  |       ) j                  dj                  | j                                y )NrY   |)rq   rH  r  r  )addressrF  r1  	forbiddens      rr   add_identityz&_audit_data_root.<locals>.add_identityh  sM    
 [[E5zQ50B#Be$ ! 	chhw{{+,r   rY   *zG1 data root contains a symlinkc              3  &   K   | ]  }|v  
 y wrk   r   )ro   markerrelatives     rr   rs   z#_audit_data_root.<locals>.<genexpr>  s      '
-C6Fh-Cr  z!G1 data root could not be auditedc              3  D   K   | ]  }|j                  d       v   yw)r   N)r   )ro   
identifierr  s     rr   rs   z#_audit_data_root.<locals>.<genexpr>  s%      '
DMjJg&'1I    TFz0G1 data root contains a personal-flow identifierz6G1 data root contains a foreign or personal identifier)rI  r   rF  zfrozenset[str]r  r  )r  r  r  rq   r   r   rm   r  rH  r   r  r  rglobr   r   rV  rB  as_posixlowerrJ  _PERSONAL_PATH_MARKERSrN  r   rY  )r   r   r  rK  current_role_componentsr   historical_classifierr   has_personal_path_markerr   has_forbidden_identifierauthenticated_historyr  rJ  rO  s               @@@rr   r@  r@  a  s   
 %I
 .7[- - +- 
	- ((9(9:4KL&&  D$5$55h++,x$$%*MM(//06	
 ' GJJL$??$%FGG;;=##I.779??A#& '
-C'
 $
 	Soo'G $' '
DM'
 $
  (0H	*%T1 A)$8D@ " !#$%WXX D
 	
? %  	S$%HIsR	S  	* %*!	*s*   >G(G3	G0G++G03H Hc                   | dz  }|dz  }|dz  }	 |j                         }t        j                  |      }t        |t              st        d      |j                  d      }|d S 	 |j                         }	t        j                  |	      }
t        |
t              r|j                  d      |j                  d	      k7  sq|
j                  d
      dk7  s]|
j                  d      |j                  d	      k7  s:|
j                  d      |j                  d      k7  s| |
j                  d      |gk7  rt        d      |j                         ||j                         |	idfd}|S # t        t        t        j
                  f$ r}t        d      |d }~ww xY w# t        t        t        j
                  f$ r}t        d      |d }~ww xY w)Nrn  zbaseline-v1.jsonz
ready.jsonz2authenticated onboarding artifacts are unavailablez,authenticated onboarding artifacts are staleowner_risk_acceptance_receiptc                     yNFr   )_path_contents     rr   r  z?_authenticated_onboarding_artifact_classifier.<locals>.<lambda>  s    ur   rD   baseliner'  readybaseline_digestreadiness_pointer_digestpointerowner_risk_acceptance_receiptsc                    	 j                  | j                               }|d uxr ||k(  S # t        t        f$ r Y yw xY wr_  )r  rS  r   rw  )r   r  rx  alloweds      rr   classifyz?_authenticated_onboarding_artifact_classifier.<locals>.classify  sL    	{{4<<>2H t#;8(;; & 		s   - ??)r   r   r  bytesr  r  )rN  rw   r  r   rx  r  r   r   rL  r  rS  )r   readiness_digestsonboarding_rootbaseline_path
ready_pathbaseline_bytesrb  r   r  ready_bytesrc  rj  ri  s               @rr   rH  rH    s     "88O#&88M </J&113::n-
 h% :
 	
 ll:;G,, ++-

;' ud#X"3"7"7
"CC99W(99&'  ,-99/0  +, 		:;y !:
 	
 	kG
< Oa ')=)=>  @
	 ')=)=>  @
	s/   %E' )%F 'FFFG7GGc                   | j                         s | j                         r| j                         st        d      	 t	        j
                  | j                  d            }t        |t              st        d      |j                  d|j                  d|            }t        |t              st        d      d	|j                  fd
|j                  fd|fd|fd|ffD ]t  \  }}	||vr||   }
|dv rM	 t        t        |
            j!                         t        |	      j!                         k7  rt        d| d      a|
|	k7  sgt        d| d       t$        D ]q  }|j                  |      }|dk(  r&	 t'        ||t)        |j                  |||             ?|dk(  rt        |t              st        d      `|duset        d|        |d   t-        fddD              st        d      t/        fddD              }|rt-        d |D              rt        d      d D ]  }||v s||   t0        k7  st        d!       |S # t        t        t        j                  f$ r}t        d      |d }~ww xY w# t        t"        f$ r}t        d| d      |d }~ww xY w# t*        $ r}t        d|       |d }~ww xY w)"Nz,G5 checklist evidence must be a regular filer   r  z'G5 checklist evidence is not valid JSONz+G5 checklist evidence must be a JSON object	checklistr  z)G5 checklist evidence object is malformedr  rm   r   r   r   >   r   r   r   zG5 checklist z does not match activation rootr  z does not match customerr  )rm   r   r   r   zG5 r  z6G3 provider terms check uses the wrong consent versionTzG5 checklist item is not true: c              3  D   K   | ]  }j                  |      d u   ywTNr  ro   r  termss     rr   rs   z-_read_activation_checklist.<locals>.<genexpr>#  s&      GD 			$4GrR  )checkedr}  validr1  rj  z'G3 provider terms check is not recordedc              3  0   K   | ]  }|v r|     y wrk   r   rv  s     rr   rs   z-_read_activation_checklist.<locals>.<genexpr>(  s$      DD5= 	dDs   )r   r  consent_versionc              3  .   K   | ]  }|t         k7    y wrk   )r   )ro   r   s     rr   rs   z-_read_activation_checklist.<locals>.<genexpr>-  s     NX'7o5Xs   )r{  r  z+G3 checklist consent version is not current)r   rU  rt  r   rw   r  r  r   rx  r  r   rL  r  rm   r   r   rS  rw  _REQUIRED_CHECKLIST_ITEMSr   r   r   rJ  r  r   )r   r   r   r   r  r  r   r  rq   rx  r  r1  versionsrw  s                @rr   rG  rG    s    T\\^ !OPPU**T^^W^=> gt$ !NOO{{;J0PQHh% !LMM 
))*	**+	&	i 	-(X g@@TF$,,.$x.2H2H2JJ,'u,KL  K x$}SE9Q%RSS'* )S!/!?./%)%6%6%1"+&3		 **eT*(L  $'Fse%LMM1 )2 -.E G  !!JKK D H
 sNXNN D
 	
 5'>gclo=$%RSS 5 NQ ')=)=> U !JKQTTU0 \* T(=[)IJPSST& , ?(3se53>?sH   %I 5AI?7$J&I<+I77I<?J#JJ#&	K/J>>Kc                  t        |       }
|
j                         rt        d      |
j                  j	                  ddd       |
j                  j                  d       	 |
j                         r|
j                         nd}t        |||||||||		      }|t        j                  |dd      d	z   j                  d
      z   }t        |
|       |
S # t        t        f$ r}t        d      |d }~ww xY w)Nr  Trz   r{   r   r/  Fr  r   r   z%activation audit could not be written)rO  r   r   r  r   r  rU  rN  rS  rw   r  r   r  r   )r   r   r   r  r  r0  r1  r2  r3  r4  r   r  r  r  r   s                  rr   rW  rW  7  s     ",/D !QRRKKdT>KKeS(,4??$3/)+#)E 3

 JJvETBTI
&/ 	D'* K '( S !HIsRSs   !A/C C2!C--C2c                    | j                         rt        d      t        j                  | j	                  d            S )Nr  r   r  )r   r   r   model_validate_jsonr  )r   s    rr   r  r  _  s4     !DEE//0PQQr   c                d    |j                  d      dz   j                  d      }t        | |       y )Nr  )r  r   r   )model_dump_jsonr   r  )r   r   r  s      rr   r  r  e  s/    ''q'1D8@@IGg&r   gate_d_preflight_v1i@B l    r`  z^[0-9a-f]{64}$>   separate_botprivate_modesschedule_fencereview_operatorschedule_ledgerdelivery_disabledenabled_isolationprofile_containedactivation_receiptapproved_artifactscurrent_kst_windowschedule_tombstonesschedule_transitionsapproved_registrationfeature_flags_disabledidentity_roles_distinctcanonical_reconciliation>
   schedule_rowsr'  r  config_epoch_rowsenabled_customersr  r  canonical_sequencesr  r  >   configr   r  rb  r'  feature_configr  artifact_catalogr  canonical_prefixr  r  r  r  r  r  artifact_base_policyr  artifact_meal_constraints>%   config_invalidconfig_missingdelivery_enabledschedule_missingartifacts_missingplan_window_staleartifacts_unapprovedcustomer_not_enabledregistration_missingreview_space_invalidseparate_bot_invalidseparate_bot_missingfeature_flags_enabledprivate_modes_invalidreview_space_collisionschedule_fence_invalidschedule_fence_missingreview_operator_invalidreview_operator_missingschedule_ledger_invalidmain_profile_containmentschedule_fence_not_readyactivation_receipt_missingschedule_tombstone_invalidschedule_tombstone_missingidentity_roles_not_distinctschedule_legacy_claim_staleschedule_tombstone_unpairedcanonical_reconciliation_stalereview_space_collision_genericreview_space_collision_customer canonical_reconciliation_missing"schedule_tombstone_digest_mismatch$schedule_transition_evidence_invalid$schedule_transition_evidence_missing&review_space_collision_owner_scheduled'schedule_tombstone_reservation_mismatchc                   t        |       t        ust        |       dkD  rt        d      	 t	        j
                  |       }|j                  |j                         t        d      k7  rt        d      |j                  t              j                  d      }| |k7  rt        d	      |S # t        $ r}t        d      |d }~ww xY w)
NrB  z*checked_at_kst must be a bounded timestampz'checked_at_kst must be an ISO timestamprB  rC  z&checked_at_kst must use the KST offsetsecondstimespecz checked_at_kst must be canonical)r   r   rH  r4  r	   r3  r9  rF  r   r<  r;  r  )r1  rH  r   	canonicals       rr   _canonical_preflight_timestampr    s    E{#UbEFFM''. }} 0 0 2ia6H HABB!!$'1191EI	;<<  MBCLMs   B, ,	C5CCc                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   dZded<   ddZddZddZddZ	ddZ
edd       Zed d       Zd!dZy)"GateDPreflightReceipta/  Bounded, read-only Gate-D readiness evidence.

    The receipt deliberately contains no Telegram addresses, credentials, raw
    customer values, or filesystem paths. A false receipt is a normal
    preflight result; corrupt profile input and unsafe paths raise before a
    receipt can be trusted.
    r   r   r  rc  Mapping[str, bool]checksMapping[str, int]countsMapping[str, str]rI  r   r  checked_at_kstr   r   reason_codesc           	     ,   | j                   t        k7  rt        d      t        | j                        t
        urt        d      t        | j                  t              st        d      t        | j                        }t        |      t        k7  s t        d |j                         D              rt        d      t        | j                  t              st        d      t        | j                        }t        |      t        k7  s t        d |j                         D              rt        d      t        | j                   t              st        d	      t        | j                         }t        |      t"        z
  s t        d
 |j                         D              rt        d      | j$                  Ht        | j$                        t&        us"| j$                  dk  s| j$                  t(        kD  rt        d      t+        | j,                        }t/        |      t0        kD  s2t/        t        |            t/        |      k7  st        d |D              rt        d      t2        j5                  | dt7        |             t2        j5                  | dt7        |             t2        j5                  | dt7        |             t2        j5                  | dt+        t9        |                   t2        j5                  | dt;        | j<                               y )Nz#unsupported Gate-D preflight schemazready must be a booleanzchecks must be a mappingc              3  >   K   | ]  }t        |      t        u  y wrk   r  rj  s     rr   rs   z6GateDPreflightReceipt.__post_init__.<locals>.<genexpr>  s      <
1@DKt#r  zchecks are not boundedzcounts must be a mappingc              3  b   K   | ]'  }t        |      t        uxs |d k  xs	 |t        kD   ) ywr  )r   r   _GATE_PREFLIGHT_MAX_COUNTrj  s     rr   rs   z6GateDPreflightReceipt.__post_init__.<locals>.<genexpr>  s8      <
( Ks"TeaiT5;T3TT(s   -/zcounts are not boundedzdigests must be a mappingc              3  p   K   | ].  }t        |      t        uxs t        j                  |      d u  0 y wrk   )r   r   _GATE_PREFLIGHT_DIGESTr/  rj  s     rr   rs   z6GateDPreflightReceipt.__post_init__.<locals>.<genexpr>  s;      =
) Ks"U&<&F&Fu&MQU&UU)s   46zdigests are not canonicalr   zepoch is not boundedc              3  R   K   | ]  }t        |      t        uxs |t        v ! y wrk   )r   r   _GATE_PREFLIGHT_REASON_CODES)ro   reasons     rr   rs   z6GateDPreflightReceipt.__post_init__.<locals>.<genexpr>  s/      %F VC'U69U+UU%s   %'zreason codes are not boundedr  r  rI  r  r  )r   _GATE_PREFLIGHT_SCHEMAr4  r   rc  r  r   r  r   rL  r  _GATE_PREFLIGHT_CHECK_KEYSrJ  ro  r  _GATE_PREFLIGHT_COUNT_KEYSrI  _GATE_PREFLIGHT_DIGEST_KEYSr  r   _GATE_PREFLIGHT_MAX_EPOCHr  r  rH  _GATE_PREFLIGHT_MAX_REASONSr  __setattr__r   r	  r  r  )r  r  r  rI  reasonss        rr   __post_init__z#GateDPreflightReceipt.__post_init__  sk   "88BCC

4'677$++w/788dkk"v;44 <
17<
 9
 566$++w/788dkk"v;44 <
<
 9
 566$,,0899t||$w<55 =
 )=
 :
 899::!C'zzA~zz55344))*L663w< CL0 % 
 ;<<4+;F+CD4+;F+CD4,<W,EF4vg1GH*4+>+>?	
r   c                (    | j                         |   S rk   )to_dict)r  rq   s     rr   __getitem__z!GateDPreflightReceipt.__getitem__/  s    ||~c""r   c                    t        d      S Nr   rc  r  r  rI  r  r  r  )iterr  s    rr   __iter__zGateDPreflightReceipt.__iter__2  s    	
 	
r   c                     y)Nr  r   r  s    rr   __len__zGateDPreflightReceipt.__len__@  s    r   c                    | j                   S rk   rc  r  s    rr   __bool__zGateDPreflightReceipt.__bool__C  s    zzr   c                    | j                   S rk   r  r  s    rr   r}  zGateDPreflightReceipt.passedF  s    zzr   c                    | j                   S rk   )r  r  s    rr   r  z%GateDPreflightReceipt.failure_reasonsJ  s       r   c           	         | j                   | j                  t        | j                        t        | j                        t        | j
                        | j                  t        | j                        | j                  dS r  )
r   rc  rL  r  r  rI  r  r  r  r  r  s    rr   r  zGateDPreflightReceipt.to_dictN  s^    "11ZZ4;;'4;;'DLL)ZZ !2!23"11	
 		
r   N)r  r  )rq   r   r  r  )r  zIterator[str])r  r   r  )r  r   )r  rf   )r   r   r   r  r   r  r  r  r  r  r  r  r}  r  r  r   r   rr   r  r    s     K$&L/&4
l#
   ! !

r   r  c                    	 t        j                  | j                               j                         S # t        t
        f$ r}t        d      |d }~ww xY w)Nz(Gate-D preflight evidence is unavailable)r  r  rN  r  r   rx  r   r  s     rr   _preflight_digest_bytesr  [  sN    V~~doo/0::<<'( V !KLRUUVs   03 AAAc                   | j                         s| j                         sy|r| j                         sy|s| j                         sy	 | j	                         j
                  dz  dk(  S # t        $ r Y yw xY w)NFr4  r   )r   rU  rV  rt  r   r   r   )r   r   s     rr   _preflight_privater  b  sf    T\\^yy{""U*a// s   	A) )	A54A5c                
   	 t        | j                  d            }|D ]T  }|j                         r y|j	                         rt        |d      r3 y|j                         sFt        |d      rT y y# t        $ r Y yw xY w)z@Check every descendant for symlinks and group/other permissions.rL  FTr   )r  rS  r   r   rV  r  rt  )r   entriesr  s      rr   _preflight_tree_privater  o  sy    

3( <<>%et<]]_%7%O    s   A6 6	BBc                H   | j                         s | j                         r| j                         st        d      	 | j	                         }g }d}t        |j                  d      d      D ]  \  }}|j                  d      st        d|       |d d	 }|j                  d
      r|d d	 }|st        d      	 t        j                  |j                  d            }t        |t              st        d      |j!                  |       |t#        |      z  } |t#        |      k7  rt        d      |t%        j&                  |      j)                         fS # t
        $ r}t        d      |d }~ww xY w# t        t        j                  f$ r}t        d      |d }~ww xY w)Nz5Gate-D canonical reconciliation ledger is unavailabler   Tr  r   rh  r  z>Gate-D canonical reconciliation ledger has a torn tail at row rP  rQ  z;Gate-D canonical reconciliation ledger contains a blank rowr   z1Gate-D canonical reconciliation ledger is corruptz.Gate-D canonical reconciliation row is invalidz6Gate-D canonical reconciliation ledger has a torn tail)r   rU  rt  r   rN  r   rR  r  rS  rw   r  r  rx  r  r   rL  r  rH  r  r  r  )	r   rU  r   rV  offsetrX  r  r1  rH  s	            rr   _preflight_jsonlr     s   T\\^ C
 	
oo
 %'DF !>aHt}}U#$PQVPWX  Sb	>>% #2JE$M 	ZZW 56F
 &$'$%UVVF#d)+ I, S D
 	
 $..000A   C
	( #D$8$89 	$C	s/   E <$E7	E4#E//E47F!FF!c                   | j                         s | j                         r| j                         st        d      	 | j	                         }t        j                  |j                  d            }t        |t              st        d      t        |      t        j                  |      j!                         fS # t        t        t
        j                  f$ r}t        d      |d }~ww xY w)N#Gate-D configuration is unavailabler   Gate-D configuration is invalid)r   rU  rt  r   rN  rw   r  r  r   rx  r  r   r   rL  r  r  r  )r   rU  r1  r   s       rr   _preflight_json_documentr    s    T\\^ !FGGMoo

3::g./ eW% !BCC;s+55777	 ')=)=> M !BCLMs   4B: :C)C$$C)c                   dd l }| j                         }|si S |dv ry |j                         dv r|j                         dk(  S 	 |j                  d      r	 t	        j
                  |      S t        |      S # t        j                  $ r |j                  |      cY S w xY w# t        t        f$ r |j                  d      cY S w xY w)Nr   >   ~NULLNullnull>   truefalser
  ){['"z"')astr   rU  
startswithrw   r  r  literal_evalr   r4  SyntaxError)r1  r  texts      rr   _preflight_scalarr    s    ;;=D	,,zz|((zz|v%%!??/0.zz$'' 4y '' .''--. $ !zz%  !s6   B  A6 +
B  6$BB  BB    CCc           	        g }| j                         }d}|t        |      k  r||   }|dz  }|j                         r|j                         j	                  d      rIt        |      t        |j                  d            z
  }|j                         }d|v r#|j                  dd      d   j                         }d|v rM|j                  dd      \  }}|j                         }	|	dv r#g }
|t        |      k  rf||   }t        |      t        |j                  d            z
  }|j                         r||k  rn%|
j                  |       |dz  }|t        |      k  rf|
D cg c]8  }|j                         r&t        |      t        |j                  d            z
  : }}|rt        |      n|dz   dj                  fd	|
D              }|	j                  d
      s|dz  }|j                  ||j                          d|f       |j                  ||f       |t        |      k  rt        |dd       D ]  \  }\  }}||dz      \  }}|j                  d      s&||k(  s,|j	                  d      s>|dz   }|t        |      k  sR||   \  }}||k  r`||k(  r|j	                  d      sw||k(  r
|dz   |f||<   |dz  }|t        |      k  rH i }d|fg}t        |      D ]a  \  }\  }}|r)||d   d   k  r|j                          |r||d   d   k  r|st        d      |d   d   }|j	                  d      r9t        |t              st        d      |j                  t!        |dd              d|vst        |t"              st        d      |j                  dd      \  }}|j                         }|st        d      |j                         }|rt!        |      ||<   |dz   t        |      k  xr* ||dz      d   |kD  xr ||dz      d   j	                  d      }|rg ni }|||<   |j                  ||f       d |S c c}w )zKParse the small mapping subset used by profile config without a dependency.r   r   # z #r  >   >+>-|+|->rH  r   c              3  L   K   | ]  }|j                         r|d  nd  y wrh  )r   )ro   rp   block_indents     rr   rs   z)_preflight_simple_yaml.<locals>.<genexpr>  s)      "LWD4::<D'R?Kr  -z: NrP  z- z+Gate-D configuration indentation is invalidz$Gate-D configuration list is invalidr  z'Gate-D configuration mapping is invalidz#Gate-D configuration key is invalid)r  rH  r   lstripr  r  rstripr  r  r  rS  rR  r  r   r   r  r  rL  )r  r  	raw_linesrX  rU  indentationr  rq   	raw_valueblock_styleblock_linesr~  candidate_indentationrp   content_indentsr1  
line_indexnext_indentationnext_contentcursoritem_indentationitem_contentr   stackr  next_is_listchildr  s                              @rr   _preflight_simple_yamlr3    s   #%E!IE
#i.
 
yy{cjjl55c:#hSZZ_!55))+7?mmD!,Q/668G'>$]]32NC#//+K@@)+c)n, )% 0I,/	NSAQAQRUAV=W,W) (-Bk-Q&&y1QJE c)n, !,# +zz| IDKK$4 55 +   # -<C(q  		 "LW"  #++C0TMEkciik]"UI+FGHk7+,I #i.
 J /8cr
.C*
*[').zA~)>&,S! K/''-!^F3u:%16v. ,#k1#{2<;R;R< #{2%01_l$CE&M! 3u:% /D& !D(*DzlE)25)9%%WuRy|3IIK uRy|3$%RSSr1d#fd+()OPPMM+GABK89gZ%=$%NOO sA.Yiik$%JKKOO%	+I6F3KAIE
" 5eai #k15eai #..t4 	
 +sk5)*9 *:: KE#s   %=Qc                D   | dz  | dz  | dz  f}t        d |D        d       }|t        d      |j                         s|j                         st	        d      	 |j                         }t        j                  |      j                         }|j                  dk(  r&	 t        j                  |j                  d	            }n&	 dd l}	 |j#                  |j                  d	            }t+        |t,              st	        d
      t/        |      ||fS # t        $ r}t	        d      |d }~ww xY w# t        t        j                  f$ r}t	        d
      |d }~ww xY w# t$        $ r}t	        d
      |d }~ww xY w# t&        $ r t)        |j                  d	            }Y w xY w)Nzconfig.jsonconfig.yamlz
config.ymlc              3  `   K   | ]&  }|j                         s|j                         r| ( y wrk   r  )ro   r~  s     rr   rs   z$_preflight_config.<locals>.<genexpr>.  s/      	
'	!Y%9%9%; 's   ,.zGate-D configuration is missingz-Gate-D configuration symlinks are not allowedr  z.jsonr   r  r   )r   r   r   rt  r   rN  r   r  r  r  suffixrw   r  r  rx  r  yaml	safe_loadrY  ImportErrorr3  r   r   rL  )r   r  r   rU  r   digest_valuer  r8  s           rr   _preflight_configr<  +  s   &}(<d\>QRJ	
'	

 	D | ABB !PQQQoo >>#&002L{{g	QjjG!45G	UU..G)<= gw' !BCC=,,,)  Q !FGSPQ #D$8$89 	Q$%FGSP	Q  U()JKQTTU  	B,SZZ-@AG	BsZ   D $D/ E9 	 E 	D,D''D,/EEE	E6%E11E69#FFc                    |D ]B  }| }|D ]%  }t        |t              s |j                  |      }' t        |t              s@|c S  y rk   )r   r   r  )r  pathsr   r1  rq   s        rr   _preflight_mapping_atr?  R  sM     CeW-IIcNE 
 %)  r   c                     t         t              r j                  S t         t              sy t	        d  fddD        D              }t        d |D              ry |S )Nc              3  v   K   | ]1  }t        |t              rt        |t              st        |      n| 3 y wrk   )r   r   r  r   r  s     rr   rs   z%_preflight_address.<locals>.<genexpr>g  s3      RD  c*:dD3ID	tSRs   79c              3  @   K   | ]  }j                  |        y wrk   r  r  s     rr   rs   z%_preflight_address.<locals>.<genexpr>i  s     R/QUYYs^/QrB  r  c              3  d   K   | ](  }t        |t               xs |j                           * y wrk   )r   r   r   r  s     rr   rs   z%_preflight_address.<locals>.<genexpr>k  s,     
LVTz$$$8DJJL(88Vr  )r   r   rq   r   r  rJ  )r1  ro  s   ` rr   _preflight_addressrD  b  sY    %)yyeW% R/QR F 
LV
LLMr   c                @   t        | t              rt        |       h dk7  ry| d   }| d   }| d   }| d   }t        |t              rV|j	                         rFt        |t              r6|j	                         r&t        |t
              st        |      t        us|dk  ryt        |      t        u rt        |t
              s|}n-t        |t              r|j                         rt        |      }ny|dk7  ry|j	                         |j	                         t        |      f|fS )	zHValidate the exact AdaptiveReviewOperator runtime shape without aliases.   r   r   r   r   Nr   r   r   r   r   ;   )	r   r   r  r   r   r  r   r   isdigit)r1  r   r   topic_valuer   r   s         rr   _preflight_review_operatorrJ  p  s     eW%U 8 * IGIG
#KIGw$}}'3'}}gt$=#Q;KC
;(E	K	%+*=*=*?{#2~MMOW]]_c(m<gEEr   c                   t        | t              r#| j                  d| j                  d|             }nt        | dd      }|t        | d|       }t        |t              r@d|vsd|vrd|vry|j                  d      }|j                  d|j                  d            }nAt        |t        t
        f      r*t        |      dk(  r|\  }}}nt        |      dk(  r|\  }}nyyt        |t              st        |t              ryt        |t        t        f      rt        |      j                         nd	}t        |t        t        f      rt        |      j                         nd	}|r|r||fS dS )
z4Normalize one reserved route to its chat/topic pair.	space_keyrq   Nr   r   	thread_idr5  r  r~   )r   r   r  r  r  r  rH  r  r   r   r   )r1  rU  
chat_valuerI  r  chattopics          rr   _space_pairrQ    s@   %!iiUYYue%<=e[$/;%.C#wCJc$9kQT>TWWY'
ggj#''+*>?	C%	's8q=),&Az;X]&)#J*d#z+t'D&0c3Z&H3z?  "bD(2;c
(KC""$QSE UD%=44r   c              #     K    yt         t              rd v rd vrd vr|d v rx j                  d      }t        |t        t        f      st        d      |D ]@  }t        |t              st        d      t        |      } j                  d      |d<   | B yt         fddD              r  y j                         D ]  }|d 
t        |      E d{     yt         t              r\ j                         j                  d	      }t        |      d
k(  r$|d   j                         dk(  r|d   |d   d yt        d      t         t        t         f      rt        d      t         t        t        t"        t$        f      r2t'                 y D ]  }|d 
t        |      E d{     y  y7 7 w)zEYield route-shaped values while rejecting malformed category entries.Nr   r   rM  topicsz1reserved route category contains an invalid routec              3  &   K   | ]  }|v  
 y wrk   r   ro   r  r1  s     rr   rs   z._iter_reserved_space_values.<locals>.<genexpr>        
Q UNQr  r   r   rM  rL  rq   r  r5  r   r  r   r  r   r   )r   r   r  r  r  r   rL  rJ  ro  _iter_reserved_space_valuesr   r   r  rH  rU  rk  	bytearrayr  r  rQ  )r1  rS  rP  r~  nestedrC  s   `     rr   rY  rY    s    }%!%'5(E!YYx(Ffudm4(G   !%1,K  !K	',yy';	)$    
Q
 
 KllnF~
6v>>>	 %
 	%##C(u:?uQx~~/:=#AhE!H== !TUU%%+, !TUU%%sI67u)KF~
6v>>>	 
 	
K) ?$ ?s%   CG G!CG?G GGr   )customer_routesowner_scheduled_routesgeneric_reserved_routesowner_routesscheduled_routesgeneric_routesc               n   t        | d|       t        t              rt        fddD              t        t        t        f      rt              dk7  rt        d      t        d D              rt        d      t        d         j                         t        d         j                         f}d	|fd
|||nd||ndffd|||ndfff}|D ]P  \  }	}
	 t        t        |
            }|D ]1  }t        |      }|t        |	 d      ||k(  s$t        d|	 d       R y# t        $ r  w xY w)zCReject a review chat/topic shared by any reserved ingress category.rq   c              3  @   K   | ]  }j                  |        y wrk   r  )ro   r  
review_raws     rr   rs   z1validate_review_space_disjoint.<locals>.<genexpr>  s      
/QeJNN5!/QrB  r  r5  z review operator route is invalidc              3     K   | ]I  }t        |t              xs3 t        |t        t        f       xs t        |      j	                           K y wrk   )r   r  r   r   r   r  s     rr   rs   z1validate_review_space_disjoint.<locals>.<genexpr>  sT       D 	4 	!$c
++	!4y  	! s   AAr   r  r   owner_scheduledr   genericz reserved route is invalidzreview space collides with z reserved routeT)r  r   r   r  r  rH  r   rJ  r   r   rY  rQ  )r  r\  r]  r^  r_  r`  ra  review_space
categoriescategoryroutesr  r~  pairrd  s                 @rr   validate_review_space_disjointrm    s    %AJ*g& 
/Q
 

 j5$-0C
Oq4H !CDD
  	  !!CDD
1&,,.JqM0B0H0H0JKL	_%& , 8b$4$@ b	
 '"0"<"	
2J$ '&	:6BCJ $Iy)D|(H:5O)PQQ|#(1(?K  $ '  " 		s   D))D4default_topicc             #     K    yt         t              rd v rd vrd vrd v r j                  d      }t        |t        t        f      st        d      |D ]Q  }t        |t              st        d      t        |      } j                  d      |d<   t        ||      E d{    S yt         fdd	D              r#t               }|d|v rd|vr	d|vr||d<   | y j                         D ]  }t        ||      E d{     yt         t        t        t        t        f      r-t                 y D ]  }t        ||      E d{     yt         t              r\ j                         j                  d
      }t!        |      dk(  r$|d   j#                         dk(  r|d   |d   d yt        d        y7 (7 7 }w)z@Collect config route entries without repairing malformed values.Nr   r   rM  rS  z Gate-D reserved route is invalidrn  c              3  &   K   | ]  }|v  
 y wrk   r   rU  s     rr   rs   z*_preflight_route_values.<locals>.<genexpr>C  rV  r  rW  r  r5  r   r  r   r  rX  )r   r   r  r  r  r   rL  _preflight_route_valuesrJ  ro  r  r  rQ  r   r   r  rH  rU  )r1  ro  rS  rP  r~  r[  rC  s   `      rr   rr  rr  '  s     }%!%'5(E!YYx(Ffudm4()KLL!%1,-OPP K	',yy';	)$2"/      
Q
 
 UI)*i/y0(5	*%OllnF.+   %
 	%%sI67u)KF.+   
 	%##C(u:?uQx~~/:=#AhE!H== !CDD
KS(s9   B%G(G)A!G
GAGGA9GGGc          	         g }| ||fD ]9  }|D ]2  }||v s|j                  t        |j                  |      |             4 ; t        |      S )Nrn  )r  rr  r  r  )r  r  telegram_extrar  ro  ro  rv  r  s           rr   _preflight_config_routesru  j  sZ     F8^4Dv~+

4(&3  5 =r   c                    t        | t              sy | j                  d      }t        |t              r|} dD ]8  }| j                  |      }t        |t              s%|j	                         s6|c S  y )Nr  )bot_idbot_usernameusernamer  	config_id)r   r   r  r   r   )r1  r[  r  r,  s       rr   _preflight_bot_identityr{    sa    eW%YYx F&'"K99T?h$)9O L r   c                   t        | j                  d            }g }|j                  d      |j                  d      |j                  d      |j                  d      |j                  d      | j                  d      | j                  d      | j                  d      | j                  d      f	D ]$  }t        |t              s|j	                  |       & |D ]k  }|j                  d      }t        |t              s|}|j                  d      du xs |j                  d	      du }t        |      }|r||||k(  r y
 y|k y
 y)Nbotr  test_botseparate_bot_config
bot_configr  separateT	dedicated)Fr  ru  )Fr  )r{  r  r   r   r  )	r  adaptivemain_identityr  r1  r~  r  r  r,  s	            rr   _preflight_bot_checkr    s7    ,FJJu,=>M-/J^$Z U*+\"

>"

:

5

< 
 eW%e$
  	]]8,
*g."JNN:&$.U*..2MQU2U 	 +95-(X-F40   )r   c                
   | dz  }|dz  }|dz  }|dz  }|j                         s |j                         r|j                         sd|d<   |j                  d       yt	        |d	
      sd|d<   |j                  d       d}		 t        |      }	|	|j                  d       n&|	j                  dk7  r|j                  d       nd	|d<   |j                         r#t	        |d
      sd|d<   |j                  d       	 t        |      }
d	|d<   |j                         r2t	        |d
      sd|d<   |j                  d       t        |      |d<   n&t        j                  d      j                         |d<   t        |
      |d<   d	}	 t        |
       |rd	|d<   |
D ]  }|j                  d      }|dv r5t!        |j                  d      t"              sd|d<   |j                  d       |dv sRt!        |j                  d      t"              rrd|d<   |j                  d        	 t%        |      }d	|d<   |j                         rDt	        |d	
      sd|d<   |j                  d       t'        |      sd|d<   |j                  d       t        |      |d<   dj)                  d t+        |j-                               D              }t        j                  |      j                         |d<   |syt/        |
      }|j1                         D ]  }t#        |d          }|j                  |      }||j                  d!       6|d"   }t        j                  |      j                         t#        |d#         k7  r|j                  d$       |d%   }|^|j3                  d&      r|j                  d       |j                  d      d'k7  s|j                  d      d(k7  s|j                  d)       ||d*   k7  s|j                  d+        |j-                         D ]Z  \  }}||v r|d"   }|d%   |j                  d,       '|j3                  d&      r|j                  d       J|j                  d)       \ y# t        $ r |j                  d       Y w xY w# t        $ r g }
|j                  d       Y w xY w# t        $ r d}d|d<   |j                  d       Y Ow xY w# t        $ r i }d|d<   |j                  d       Y w xY w)-zKValidate schedule persistence without locks, recovery writes, or migration.r  zscheduled-deliveries.jsonlzscheduled-deliveries-fence.jsonzcustomer-schedule-claimsFr  r  NTr  r  r  r  r  rc  r  r  r  r   r  r  r  r'  >   	deliveredsent_auditedprovider_receiptr  >   unknownr  r  r  r  c              3     K   | ]B  \  }}|j                  d       dz   t        j                  |d         j                         z    D yw)r       r  N)r   r  r  rD   )ro   r  rl  s      rr   rs   z,_preflight_schedule_state.<locals>.<genexpr>	  sE      4NHd 	 5(7>>$q'+B+I+I+KK4s   AA
schedule_keyr  r  legacy_claim_digestr  r5  s    scheduled-delivery-tombstone-v1
r  legacy_claim_unknownr  reservation_idr  r  )r   rU  rV  r  r  r&   r)   r'  r'   r  r  r  r  rH  r(   r  r   r   r$   r  r  r	  rs  r%   ro  r  )r   r  r  rI  r  r   ledger_path
fence_pathclaims_rootfencerV  
rows_validr\  r'  claimsclaim_materialr{  r  rl  rU  	tombstones                        rr   _preflight_schedule_stater    s    vI::K>>J88KY%5%5%7y?O?O?Q#( &'i48"'+,E.J' },-		./#' #5jE#R"'+,);'
 %) !!+?&+F?#KK/0%<[%I!"%,^^C%8%B%B%D!"!$iF?J<%
 )-%&CGGG$E55j*+S? 27-.BC00!3: 27-.BC -$[1 )-$%!+>&+F?#KK/0&{3&+F?#KK/0$'KF !XX $V\\^4 N &-^^N%C%M%M%OG!"D!G~~s>*+zz(#<KK451g>>#((*c#6K2L.MMKK<=G	~~BC89 I-778$(>>9:#.//KKAB)  * !,,.$w1g7KK56^^@AKK45KK56 )A ! .,-. ! /-./" ! <
).%&:;<* ! 2(-$%012sH   Q6 ;R R: #S" 6RRR76R7:!SS"!TTc           
         t        t        |            }t        |      t        z
  rt	        d      t        t        | xr t        | j                               t        |       t        |      t        |      |||      S )Nz2Gate-D preflight produced an unbounded reason coder  )
r  r	  r  r  r   r  r  r  ro  rL  )r  r  rI  r  r  r  normalized_reasonss          rr   _preflight_receiptr  3  sr     vg/
!== !UVV -$$=V]]_)=F|F|W'%	 	r   c                   | t        j                  t              n| }t        |t               r8|j                  |j                  t              n|j                  t              }nTt        |t              r9t        j                  |t        j                        j                  t              }nt        d      |j                         |j                  d      fS )Nr8  z"kst_now must be a date or datetimer  r  )r	   r:  r;  r   r9  r<  r   r   r  r
   r  r   r  )r1  current_valuerx  s      rr   _preflight_checked_atr  K  s     +0-HLL&UM-* ##/ $$T*&&d&3 	
 
M4	(""=$((;CC4CP !EFF<<>7,,i,@@@r   )kst_nowc                 GHI t        t        |             }t        |      It        |      \  }}t	        |      }t               }i ddddddddddddd	dd
ddddddddddddddddddd}ddddddddddd
}	i }
d}t        |d      sd|d<   |j                  d       |j                         s|j                         j                  |      st        d      t        |d      sd|d<   |j                  d       	 t        ||      }t!        |      |
d<   t+        d  |j,                  D              }t/        |      |	d!<   t1        Ifd"|j,                  D        d      }|t        d#I       |j2                  s|j                  d$       t/        |      d%k7  s|d   j4                  Ik7  r|j                  d&       nd|d<   t7        |j8                        }t7        |j:                        }t+        d' ||fD              }|K|It/        |      d(k7  s;t/        t        |            d(k7  s$t/        |D ch c]  }|d%   |d(   f c}      d(k7  r|j                  d       nd|d<   |d)z  d*z  Iz  }t=        |      sd|d<   |j                  d       |j                         sY|j?                         rI|jA                         r9|j                         j                  |      rtC        |j                         |      rt        d+      t        |d      sd|d<   |j                  d       |d,z  }|j?                         r7|j                         |j                         k(  rd|d<   |j                  d&       	 tE        |      \  }}}|r(||
d1<   t        |d      sd|d<   |j                  d       d}d2D ]]  }|jI                  |      }tK        |tL              s%|jO                         s6t        |      jQ                         j                         } n |,||k(  s|j                  |      rd|d<   |j                  d&       tS        |d3d4d5      GGi G|j                  d0       GjI                  d6GjI                  d7            }tS        |d8d9      xs i }tK        |jI                  d:      tT              r|jI                  d:      ni }	 |ftW        |||d;d<=      z   }tW        |||d>d<=      }|du r|j                  d6       n|du rd|d<   n|j                  d0       GjI                  dA      dur|j                  d0       t        GfdBdCD              r|j                  dD       GjI                  d      }tK        |tT              s|j                  dE       nt        |      h dFk7  r"h dFt        |      z
  r|j                  dE       ntY        |      } | |j                  dG       nu| \  }!}"d}#dHGv r"tM        GdH         jO                         |!d%   k(  }#dIGv r	 t[        GdI         }$|#xr |$dJk(  }#|#r||j                  dG       n	 t_        |!|f||K       d|d<   ta        |G      \  }'}(|'rd|d<   n|(|j                  |(       	 tc        ||||      })d|d
<   te        dP|)i      |
d
<   |jf                  jh                  }*|*tk        dRS      z   }+|*|cxk  r|+k  r	n nd|d	<   n|j                  dT       |dUz  },|,j                         s,|,j?                         r|,jA                         rtC        |,|      rt        dV      t        |,d      sd|d<   |j                  d       t=        |,      sd|d<   |j                  d       |,dWz  }-	 tm        |-      \  }.}/|/|
dX<   	 to        |-      \  }0}1}2|0}|1|
dY<   tq        dZ |2js                         D              rd|d<   n|j                  dD       	 t        |-d      sd|d<   |j                  d       	 |d
   r	 tu        |Id]      }3|3jv                  |3jx                  s|j                  d^       nd|d<   tM        |3jv                        |
d_<   |3jz                  xs i }4t        |4      h d`k(  rZd|d<   da|	d<   |
j}                  |4j                         D 5ci c]$  \  }5}tK        |tL              rdb|5 tM        |      & c}}5       n|j                  dc       	 |dez  dfz  }6|,dgz  |,dhz  |,diz  |,djz  dk}7	 t        |6      \  }.}8t        |6d      sd|d<   |j                  d       t        ||      }9t1        Ifdl|9j,                  D        d      }:|:t        dm      t        j                  |:      };|;j                  j                         s4|;j                  j?                         r|;j                  j                         st        dn      |;j                         }<|<j                  D =cg c]  }=|=j                  dodp       }>}=t/        |>      |	dq<   |8|
dq<   |<j                  D ?cg c]  }?t        |?       }@}?dr|@i}At/        |@      |	dr<   t!        |;j                        |
dr<   t        |;j                  d      sd|d<   |j                  d       |7j                         D ]O  \  }5}Bt        |B      \  }C}D|CA|5<   t/        |C      |	|5<   |D|
|5<   t        |Bd      r:d|d<   |j                  d       Q |>D ])  }?|?jI                  ds      dIfvs|j                  d[       + dkD ]3  }5A|5   D ])  }?|?jI                  ds      dIfvs|j                  d[       + 5 Adt   D ?cg c]  }?|?jI                  du      dvk(  r|? }E}?Adw   D ?cg c]  }?|?jI                  du      dvk(  r|? }F}?ErFs|j                  d\       n*|(Edx   jI                  dy      |k7  r|j                  d[       |j                  d\d[h      sd|d<   te        t+        Ajs                                     |
d<   t        |||	|
|       t#        ||	|
|||      S # t        t        f$ rZHt        HfddD              r5|j                  d       t!        |      |
d<   t#        ||	|
|||      cY dHHS t        d      HdHHwt$        t&        t(        f$ rHt        d      HdHHww xY wc c}w # tF        $ r i d-|d.z  }}}|j                  d/       Y 	t        $ r i d-|d.z  }}}|j                  d0       Y 	w xY w# t        $ r |f}d?}|j                  d@       Y }w xY w# t\        t(        f$ r d}#Y Nw xY w# t        $ r_HtM        H      }%|j                  dG       |j                  dL|%v rdMnd@       dND ]  }&|&|%v s|j                  dO|&         n Y dHHdHHww xY w# t        $ r |j                  dQ       Y `w xY w# t        $ r |j                  d[       Y ?w xY w# t        $ r |j                  d\       Y <w xY wc c}}5w # t        $ r |j                  dd       Y |w xY wc c}=w c c}?w c c}?w c c}?w # t        $ r |j                  d\       Y gt$        t        t&        t(        f$ r |j                  d[       Y w xY w)zzHAudit a disposable Gate-D profile without creating or enabling anything.r  Tr  r  Fr  r  r  r  r  r  r  r  r  r  r  r  r  r  r   )
r  r  r  r  r  r'  r  r  r  r  Nr  r  z(Gate-D registry path escapes the profilec              3  8   K   | ]  }|t              v   y wrk   r  )ro   r  r   s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  s&      
 SXr  )zowner and customerr  r   )r  r  rI  r  r  r  zGate-D registry is corruptc              3  :   K   | ]  }|j                   s|  y wrk   r  r  s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  s     H%7T4<<D%7r   r  c              3  B   K   | ]  }|j                   k(  s|  y wrk   rl   rn   s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  r2  ru   rv   r  r   r  c              3  &   K   | ]	  }||  y wrk   r   rj  s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  s      ,%0A,s   r  r  r   zGate-D customer root is unsafezmain-profiler~   r5  r  r  r  )main_profile_rootpersonal_profile_root)	platformsr  extraadaptive_nutrition)r  r  )r  r  r*  )r  r  )r  r  )r]  r`  owner_schedulescheduled_delivery_routescron_routesowner_deliverieshome_channel1)r  ro  )	dm_topicsphysique_checkinr^  ra  reserved_routesreserved_spacesoperator_routesplatform_reserved_routesr   r  r
  c              3  D   K   | ]  }j                  |      d u   ywru  r  )ro   r  r  s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>5  s+      
D 	Td"
rR  r  r  r  rF  r  operator_chat_idoperator_topic_idrG  )r\  r]  r^  collidesr  )r   rf  rg  review_space_collision_r,  r  r^  r_  r  r'  zGate-D adaptive root is unsafer(  rb  r  c              3  $   K   | ]  }|d u  
 yw)FNr   r  s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  s     <^T45=^rd  r  r  rL  r  r  >   r  r  r  r5  	artifact_r  r  r  r  zsource-days.jsonlzsource-day-intents.jsonlzauthority-mirror-intents.jsonlzconfig-epoch-journal.jsonl)r  r  r'  r  c              3  T   K   | ]  }|j                   j                  k(  r| ! y wrk   rO  rn   s     rr   rs   z)audit_gate_d_preflight.<locals>.<genexpr>  s+      6D99))S0 6r5  z'Gate-D canonical runtime is unavailablez/Gate-D canonical sequence ledger is unavailablerw   r  r  r  rm   r  r'  r?  r'  rP  r  )Nr   r   r   r  r   r  r  r  r   rS  rW  r   r   r   r   rJ  r  r  r   rx  r4  r  r   rH  r   r
  rm   rD  r   r  r  rU  rV  rX  r<  r   r  r   r   r   
expanduserr?  r   ru  rJ  r   rc  rm  r  r  r   rd  r   r   r  r  r  ro  r^  rD   r   r-  r  rs  r   r!   r;   rr  ru  rt  rv  r  r   r  rL  intersectionrw  r  )Jr   rm   r  r   current_dayr  r   r  r  r  rI  r  r   r
  r   r   r   
identitiesr1  r  	main_rootr  r  r!  configured_mainkey_namedelivery_valuer  rt  r]  r^  reviewreview_resultreview_address_review_versionlegacy_pair_validlegacy_topicdetailrj  bot_ok
bot_reasonactivation_idstartsendsrJ  r   r  feature_digestrb  feature_config_digest_valuer  r   	artifactsr  r  projection_pathsevents_digestruntime_registryr   ry  snapshotr  r  r\  r  	projectedr   rV  r&  committed_configscommitted_authorityr  r   rq   sJ                                                                          @@@rr   audit_gate_d_preflightr  \  sq    !l!34D

-C"7"@K*40MGT 	U 	"5	
 	5 	 	e 	e 	  	e 	#E 	!% 	U 	% 	5  	u!" 	#F(    F !GEdd3"'+,!)>)>)@)O)OPT)U !KLLmu="'+,H)->* 2-@GJHX%7%7HHG"%g,FI(,,I4H  #5cU!;<<*+
7|qGAJ33s:./&*"#x~~.E!("3"34H !8, J 	z?as:1$*=*q58$*=>!C12,0()6MK/#5M"=1"'+,  "##%##%$$&55d;]224d; !ABBmt<"'+,~%Im3359J9J9LL&+"#./&->t-D*{ )!+?&+F?#KK/0OB

8$eS!ekkm"5k446>>@O	 C
 "44#6#6#G&+"#./$@*	H $%\\"4hll:6NON%	
 	
   ",HLL,A7!KWQS #,"',D -
 "
 #;	 #
( &'	5	 &*"#$%||Id*$%
 
  	+,\\+,Ffg&-.	VE	E5FC-.26: KK12.;+NO $!X-!345;;=PQARR " #h.Q#&x0C'D#EL ):(Plb>P%$(85652&)1/E0G	. 15F,--fh?FJ!%~		J
=	
 (,#$(7$m4)
$% ]]$$FI2&&D$$'+#$'(!$55M  "##%##%]D1 !ABBmt<"'+,"=1"'+, #77L14\B> $2 	5.|< >M6 "E(CG$%<U\\^<<37/034!,%@&+F?#KK/0"#	59$USH &h.?.?2326./*-hoo*>'$55;	y>%SS37F/034F/0NN 09/@/@e)%5 (v.E
:/@ KK 34"X->N,/BB+.HH'*JJ*-II	V6+N;=!.EB&+F?#KK/01-F,66
 
 ?$%NOO/DDWM%%002,,335,,446$%VWW557 "
( &t<( 	 
 &)[!"&3"#.6.D.DE.Dsc.DE!=9
	 ),M(:$%)@%%*
%& "+";";uM&+F?#KK/0*002JD$/5D*"IdOt9F4L&GDM%de<*/'34 3 Cww~&tSk9<= 
D !77>*4+=KK @A '
 !!45
5www;. 5 	 
 !!12
2www;. 2 	 

 !(;KK:;#4R#8#<#<W#E#NKK89##20
 26F-..=eIDTDTDV>W.X*+ dFFGWE% } "?3 H 

 
 KK56"9-"HGJ%-  !!=>CG'4 H !=>CGH6 >8  &-/TM5I{$% &-/TM5I{$%&R  ,"'"$*+,X ":. .(-%. * " XFKK 9:KK%/ 13
%
 $v-#KK*A((LM!%">  2012R " 	:KK89	:  8678J " 	0KK./	0j
 F>


"  867\#5zB 6456sB  q5 tt	 $u -u> v x y 2x" *y* )y$
'C5z z6#z z+B"z 2z 5z 7z z0	z 9zA?z 5tAstst0s<<t	#u/"uuu;:u;>vv	w><w9w99w>xx"y ?y y! y!*zzz {)<){)({)c                 
   t        j                  d      } | j                  dt        d       | j	                  dd      }|j                  d      }|j                  d	d
       |j                  dd
       |j                  dd
       |j                  dd
       |j                  dd
       |j                  dt        j                  d       |j                  dt        j                  t        dd             |j                  dt        d       |j                  dt        d       |j                  dt        d       |j                  dt        d       |j                  ddd       |j                  dd       |j                  d d!g "       |j                  d#d!g "       |j                  d$d!g "       |j                  d%d!g "       |j                  d&       |j                  d't        (       |j                  d)       |j                  d*       |j                  d+t        (       |j                  d,t        (       |j                  d-t        (       |j                  d.      }|j                  d/       |j                  d0      }|j                  d1       |j                  d2t        d       |j                  d3t        d       |j                  d4t        d       |j                  d5t        d       d\d6}|j                  d7      } ||       |j                  d8t        d9 t        D              d:       |j                  d;t        j                  d       |j                  d<t        j                  d       |j                  d=t        j                  d       |j                  d>t        d?       |j                  d@t        j                  j                  ft        j                  j                  A       |j                  dB      } ||       |j                  dCdDdEt        dF       |j                  dGdHdIt        j                  dF       |j                  dJ       |j                  dK      } ||       |j                  dLd
       |j                  dMd
       |j                  dNt        d       |j                  dOt        dP t         D              d:       |j                  dQdRdSt        j                  dF       |j                  dT       |j                  dU      }	|	j                  d/       |	j                  d2t        d       |j                  dV       |j                  dW       |j                  dX      }
|
j                  d/       |
j                  dYt        j                  d       |
j                  dZd
       |j                  d[      }|j                  d/       | S )]Nz$python -m checkin_cli.customer_admin)progz
--registryTr   requiredcommand)destr  r  z--key)r  z--namez	--user-idz	--chat-idz
--topic-idz--starts-onz--daily-timer  r   )r   r  z--weekly-weekdayz--monthly-dayr   z
--caloriesz	--proteinz--meals+)u   아침u   점심u   저녁)nargsr  z--goalr   r  z--restrictionr  )actionr  z	--allergyz--preferencez--supplementz--digestion-contextz--sleep-goalr   z--recovery-goalz--training-contextz--carbsz--fatz--waterdisablerm   activater  --profile-root--data-rootz--checklist-evidencez--staff-membership-evidencec                    | j                  d       | j                  dt        d       | j                  dt               y )Nrm   r  Tr  r  r  )add_argumentr   )r  s    rr   add_record_scopez!_parser.<locals>.add_record_scopeb  s9    ^,-D4H]6r   record-paymentz--kindc              3  4   K   | ]  }|j                     y wrk   r0  )ro   rf  s     rr   rs   z_parser.<locals>.<genexpr>j  s     C{t

{   )choicesr  z	--paid-onz--period-start-onz--period-end-onz--amount-krwr  z--method)r  r  record-satisfactionz--scorez--score-1to10r  )r  r   r  z--collected-onz--collection-dater  z--noterecord-operator-timez
--entry-idz--attempt-idz	--minutesz--taskc              3  4   K   | ]  }|j                     y wrk   r0  )ro   r  s     rr   rs   z_parser.<locals>.<genexpr>  s     D|t

|r  z--work-datez	--work-onr  z--supersedes-entry-id	judge-kpir  validater  z--recorded-onz--notice-versionrevoke-consent)r  argparse.ArgumentParserr  r  )argparseArgumentParserr  r   add_subparsers
add_parserr   r3  r
   r   floatr  r+   r,   BANK_TRANSFERr1  r*   )parsercommandsr  r  r  r  r  r  r  kpir  revokes               rr   _parserr  <  s   $$*PQF
4$?$$)d$CH


e
$CWt,X-[40[40\D1]););dK^$*<*<d1ajQ'c1=_3:\d;[sT:Yc3QRXx0_XrB[2>^HbA^HbA*+^%0&')*YS)W3'YU+!!),G("":.H-(*E-dTB0tdK7dTR7
 !!"23GW%C{CCd   4+=+=M,43E3EPTU*1C1CdSc6B,,224++11   &&'<=L\"?S4      h'''(>?M]#|d;~={tD%D|DDt      67


k
*C^$%D4@
#!!),G(t/A/ADQ+d;  !12F
'Mr   c                
   t               j                  |       }|j                  }|j                  dk(  rt	        |t        d6i d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d|j                  d	|j                  d
|j                  d|j                  d|j                   dt#        |j$                        d|j&                  dt#        |j(                        dt#        |j*                        dt#        |j,                        dt#        |j.                        d|j0                  d|j2                  d|j4                  d|j6                  d|j8                  d|j:                  d|j<                        }t?        d|j@                          y|j                  dk(  r1tC        ||j@                  d      }t?        |j@                   d       y|j                  d k(  rZtE        |jF                  |jH                  |jJ                  |jL                  |jN                        }t?        |jJ                   d!       y|j                  d"k(  rytQ        tS        ||j@                  |jF                  |jH                  |jT                  |jV                  |jX                  |jZ                  |j\                  |j^                  #
             y|j                  d$k(  rXtQ        ta        ||j@                  |jF                  |jH                  |jb                  |jd                  |jf                  %             y|j                  d&k(  rytQ        ti        ||j@                  |jF                  |jH                  |jj                  |jl                  |jn                  |jp                  |jr                  |jt                  '
             y|j                  d(k(  r9tw        ||j@                  |jF                  )      }ty        |j@                  |       y|j                  d*k(  rRt{        d+|j|                  |j~                  ,      }t        ||j@                  |      }t?        |j@                   d-       y|j                  d.k(  r8t        ||j@                  t{                     }t?        |j@                   d/       y|j                  d0k(  r.t        |      }t?        d1t        |j                         d2       yt        |      j                  D ]7  }t?        |j@                   d3|j                   d3|j                  rd4nd5        9 y)7Nr  rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   zcreated disabled draft: r   r  Fr  z
: disabledr  z	: enabledr  )r   r   r  r  r  r  r  rf  r  )r   r   r  r  r  r  )r   r   r  r  r  r  r  r  r  r  r  Tr  z : AI processing consent recordedr  z: AI processing consent revokedr  zregistry valid: z# customers; every plan has 12 weeks	r
  disabledr   )Fr  
parse_argsr   r  r  r   rq   r  r   r   r   r   r   r   r   caloriesproteinr  r   goalrestrictionallergy
preference
supplementr   
sleep_goalr   r   carbsfatwaterr  rm   r  r  r   r   r  checklist_evidencestaff_membership_evidencer  r  r  r  r  r  r  rf  r  r  r  r  r  r  r  r  r  r  r  rz  r  r   r  r  r  r  rH  r   r   r
  )argvargsr   r   rn  r  r  r   s           rr   mainr    s   9%DD||u$ !XX!YY  	
  ..  ??  $22 !,, #mm ,, DJJ' "YY &+4+;+;%<  -  "'t!7!" "$//2#$ #'"8"8%& "&'( #00)* "&!6!6+,  $zz-. hh/0 "ZZ1
: 	()>)>(?@A||y 'd.?.?O&&'z23||z!"NN##**
 	##$I./||''!!!....?? $ 4 4"00{{YY	
 ||,,!!!.... ,,!..YY
	
 ||-- !!!....??YY..$($<$<	
 ||{"-**
	
 	T..	:||y %((..

 +41B1BGL&&''GHI||''*$##%8%:
 	&&''FGH||z!;s8#5#5677Z[	
 $K))$$%R(=(='>bhN^N^dn@op	
 * r   __main__)r   
Path | strrm   r   r   r   r   r   rh   zstr | datetime | Noner  rf   )r   r  r  Iterator[None])r1  r   r  r   )r1  
date | strr5  r   r  r   )r1  date | datetime | Noner  r   )r1  r   r5  r   r?  r  r  r   )
r   r   r   r   rm   r   r   Path | Noner  r   )r  r   rf  PaymentKind | strr  ztuple[PaymentKind, date, date])rn  r-   ro  r   r  r-   )r   r   rm   r   r   r   r  r"   )rm   r   r  r"   r  r  )r   r   rm   r   r   r   r   r  r  r   r  r  r  r  r  r  r  zPaymentMethod | strrf  r  r  r-   )r   r   rm   r   r   r   r   r  r  r   r  date | str | Noner  r   r  r   r  r   r  r-   )r   r   rm   r   r   r   r   r  r  r   r  r   r  r   r  zOperatorTask | strr  r   r  r   r  r   r  r-   )rn  r-   r  r  )r   r   r  r   r  r   )r   r   rm   r   r
  r  r  r   )r   r   rm   r   r  r   )r   r   rm   r   r  r   r  r   )r   r  rm   r   r  zdate | datetimer  r   )r  '_HistoricalActivationArtifactClassifierr  r  )r   r   r   r   r  r   r  r   r  r   r  r  r  r   )
r   r   r   r   r,  r   r-  r   r  r  )r   r   r  r   )r   r   r  r   )r   r   r   r   r  r   )r1  r  r  r   )r1  bytes | Noner  r   )r   r   r  r   )r   r   r  r  )r   r   r  rk  r  r  )r   r   r  r"  )r   r   r  r"  r  r  )r   r   r  zdict[str, object] | None)r   r   r  rf   r  r  )r1  r  r   r   ro  r   r  r   )r   r   r   r   r  r   r  r   r0  r   r1  r   r2  r   r3  Mapping[str, str] | Noner4  Mapping[str, object] | Noner  rf   )r  r   r  r   r  r  r  dict[str, str])rh  r   rg  rf   r  zlist[dict[str, str]]r  r  )r   r   rd  rk  r  r   re  r"  rh  r   rg  rf   r  r  )r   r   r0  r   r  r  )r  r   r0  r   r9  r   r  r   )r   r   r   r   r  r  )r  r   r0  r   r9  r   r  r  )
r   r   r   r   r   r   r  r   r  r   )r   r   r   r   r   r   r  r  )NN)r   r   r   r  r  r   r  r  )r   r   r  r    )r   r   r  r   )r   r   r  ztuple[Path, Path, Path, Path])r1  r  r  r%  )r1  r   r  r   )
r   r   r   r   r   r   r  r   r  ztuple[dict[str, object], str])r   r   rM  r  r  list[dict[str, object]])r\  Mapping[str, object]r  r   )r1  r  ro  r   r  r   )rV  r&  r  z7tuple[list[dict[str, object]], list[dict[str, object]]])r   r   r.  r   r  r  r  r'  )r   r'  r.  r   r  r  )r   r'  r.  r   r   r  r  "tuple[dict[str, object], str, str])r  r'  r   r  rh   r   r  r(  )r   r   r  r'  r  r  r   r  rh   r   r  3tuple[tuple[str, dict[str, object], str, str], ...]) r  r   r%  r   r'  r   rm   r   r2  r   r.  r   r1  r'  r0  r   r/  r   r+  r   r   r'  r   r   r   r  rh   r   r)  r   r  rf   )r   r   r  r  )r   r   r   r'  r  r)  r   r'  r   r   r  r  )rV  r&  rm   r   r2  r   r  zdict[str, dict[str, object]])r   r   r\  r'  r  r  )
r   r'  r   r'  r   r   r   r  r  r  )r  r  rm   r   r  r   r  rf   )r  r'  r  r  r  r  )r  r'  r+  r   r   r'  r   r   rh   r   r   r  r  rf   )r  r   r%  r   r'  r   r   r'  r)  r   r  rf   )r   r   r   r'  r  r  )r   r   rm   r   rM  r  r  r   )r   r   rm   r   r  r  r   r  rh   r   r+  r   r  object | Noner   r*  r  r*  r  r*  r  r*  r  r   )r   r   rm   r   r   r  r  r   )r   r   rm   r   r  r   r  r  )r   r   rm   r   r  r   )r   r  r  r   r  r   )r  r  r  r   ry  r;   r  ztuple[dict[str, object], ...])r  r'  rm   r   r  r   r  rf   )r   r   r  z tuple[int, str, dict[str, bool]])rV  r  r  r   )
r   r   rm   r   r   r   r  r   r  ztuple[dict[str, str], str])
r   r   rm   r   r  r  r   r  r  r'  )r   r   rm   r   r3  zdate | Noner  r  )r   r   r\  r   r[  r   r   r   r  r   r  r  )r   r   r   r   r  r  )r   r   r   r   r  r   r  r  )r   r   rl  r  r  r!  )r   r   r   r   r   r   r   r   r  r   r  rf   )r   r   r   r   r   r   r  r   r  r   r0  r   r1  r   r2  r   r3  r#  r4  r$  r  r   )r   r   r  r   )r   r   r   r   r  r  )r   r   r  r   )r   r   r   r  r  r  )r   r   r  r  )r   r   r  z#tuple[list[dict[str, object]], str])r   r   r  z tuple[Mapping[str, object], str])r1  r   r  r  )r  r   r  r'  )r   r   r  z&tuple[Mapping[str, object], str, Path])r  r'  r>  r   r  r$  )r1  r  r  ztuple[str, str, str] | None)r1  r  r  z'tuple[tuple[str, str, str], int] | None)r1  r  r  ztuple[str, str] | None)r1  r  r  Iterator[object])r  r  r\  r  r]  r  r^  r  r_  r*  r`  r*  ra  r*  r  r  )r1  r  ro  r   r  r+  )r  r'  r  r'  rt  r'  r  r   ro  r   r  ztuple[object, ...])r1  r  r  r   )r  r'  r  r'  r  ztuple[bool, str | None])r   r   r  zdict[str, bool]r  zdict[str, int]rI  r%  r  set[str]r  r  )r  r  r  r  rI  r  r  r   r  r   r  r,  r  r  )r1  r  r  ztuple[date, str])r   r   rm   r   r  r  r  r  )r  r  rk   )r  zlist[str] | Noner  r   (!  r  
__future__r   r  rw   r   r  rer   r!  r   
contextlibr   contextvarsr   typesr   dataclassesr   r	   r   r
   r   r   pathlibr   typingr   r   r   	threadingr   pydanticr   zoneinfor   ,checkin_cli.activation_token_rotation_policyr   r   r   checkin_cli.customer_coachingr   r   r   r   r   r   r   r   r   r    r!   checkin_cli.customer_reportingr"   r#   checkin_cli.customer_scheduler$   r%   r&   r'   r(   5checkin_cli.weekly_operations_schedule_host_models_r4r)   checkin_cli.modelsr*   r+   r,   r-   checkin_cli.nutrition_readinessr.   r/   r0   +checkin_cli.nutrition_onboarding_projectionr1   r2   r3   r4   r5   r6   #checkin_cli.nutrition_onboarding_fsr7   r8   r9   r:   checkin_cli.storer;   r<   %checkin_cli.staff_membership_evidencer=   r>   checkin_cli.adaptive_nutritionr?   r@   rA   rB   rC   rD   r   rE   rF   rG   rH   rI   rJ   rK   rg   r   r   r4  r   r   r   compiler.  rG  rI  r;  r  r  r   r   r6  r=  rK  r   rh  rp  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r}  rV  rk  r  r!  r  r  r  r  rM  r   r   r  r   r  r  rU  rO  rK  rT  rQ  r  r  r  rP  r  r  rV  r  rS  r  r  rZ  r  rX  rC  r  r  r  rD  r"  rT  rK  r7  r8  r9  r:  rq  r  rn  r  r  r5  r;  r?  r   r   rZ  r^  rf  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r,  r/  r^  ra  r`  rw  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r5  rJ  rX  r@  rH  rG  rW  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r   r  r  r3  r<  r?  rD  rJ  rQ  rY  rm  rr  ru  r{  r  r  r  r  r  r  r  r   
SystemExitr   r   rr   <module>rF     s   A "   	  	    % " " ! > >  . .  $  
    O  X U U 
   D   " ).64P#F*)A5&(FG:
4 0 
& .2ggg 	g
 !g +g gT	 	 $d#& & $&6 $d#  $2 

899  %  =! =!@O	D 6;.2 &
&& & 	&
 &R
X

X/
X#
X!
!! 	!
 !HC> " #0"="=./
././ 	./
 ./ ./ ./  ./ ./  ./ ./ ./l ""&*)-#4
#4#4 	#4
 #4 #4 $#4 #4 #4 '#4 #4V "
 $(!%&*25
2525 	25
 25 25 25 25 25 !25 25 $25 25j6
;|Q
Q!Q/3QQ
!/38C
L
L!L,?LL
!,?.,Y,Y,Y 	,Y
 ,Y^  : *2D%=$3F*G ' :DI + - J
 
@7
@
@ 
@( (,


 
 "	

 %)
 %
 
8 (,mUmUmU mU "	mU
 %)mU %mU mU`+5+5 +5 	+5
 +5 
+5\D " > : <:/-:,
V( W** "&"&"=A7;  	     #; 5 @


 
 	
 # 
	4OJOJOJ OJ 	OJ
 OJ OJ 
OJd66%4%4 %4 	%4
 	%4PEEP  	
 
&``` ` 	`
 	`F			 	 
		 "&"222 2 
	2j
L  % B @ 5 > $  )! 0 ( . !*! 4*)$$,1,1,1 ,1 	,1
 #,1` $)5
5 55p!
y
!y<yxZ
!Z<Zz5
55 5 	5pT"TT TD15"1515 15 (	15h"?!"? "? 	"?
 ("?JR
R!R R
 R R 9Rj'B'B 'B 	'B
 'B 'B 'B ,'B 'B 'B 'B $'B 'B 'B 'B   !'B" #'BT\
!\<\~ +% +%\[W
[W"[W @[W $	[W
 [W 
[W|!
!!! ! "	!H2V"V#V V 	V
 
V8GGG 	G
 GT! 
:@!@ @ $	@
 @ @ #@ @<BB B 	B
 #B  B B@BB B 	B
 #B  B B>ZA
ZA"ZA 
ZAz
U

U"
U 

U" 	JJJ 	J
  Jf #'$(!% "&!&*


 	

 
  
 "
 
 
  
 
 $
  
J #'$(!% "&!&*xLxLxL 	xL
 xL  xL "xL xL xL  xL xL $xL  xLB #'$(!% "&!&* 	
    "      $  >C
C
C
 	C

  C
L~~~ ~ 
	~BJJJ  J  !$"+( 
 48;8;8; +	8;
 #8;v? ?? ? 	?D9<(333  #	3
 3  3B


 	

 
 
"iii 	i
 i i`	 &*	


 #	

 
& &*	NQNQNQ #	NQ
 NQb111 1 	1
 1 
12@
@
@
 @
 
	@
F::(: -:zS
SS S 	S
 S Sz "&"&"=A7;%%% % 	%
 % %  % % #;% 5% 
%PR'
 / % %   #$56 & * '  ( 0 &	
(R 'S)' \ $d#q
GCK0 q
 $q
hV
"'1T
8!*\~$-N! ! !F!F,!FH5:7z !%'&("&&*$(99 9 #	9
 $9  9 $9 "9 
9~ !%@@ @ 	@R !% " )
   *
!) !)"!) !)H@7
@7@7 @7 	@7
 @7 
@7F  	
    0A!AA* '+	]]] $	]
 ]@cLD z
TV
 r   