
    'tj                    *   U d Z ddlmZ ddlZddlZddl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mZmZmZmZ ddlmZmZmZ d	Zd
ed<   dZd
ed<    ede      Z ede      Z ej:                  d      Z ej:                  d      Z G d de      Z  G d de!      Z" G d de"      Z# G d de"      Z$ G d de"      Z% G d de%      Z& G d de$      Z' G d  d!e%      Z( G d" d#e%      Z) ed$d$%       G d& d'             Z* ed$d$%       G d( d)             Z+ ed$d$%       G d* d+             Z, ed$d$%       G d, d-             Z- G d. d/e      Z. ed$d$%       G d0 d1             Z/ ed$d$%       G d2 d3             Z0d9d4Z1d:d5Z2d;d6Z3d<d7Z4d=d8Z5y)>z:Typed day-status values for the weekly-operations sidecar.    )annotationsN)	dataclass)datedatetime	timedelta)StrEnum)ClassVarFinalLiteralNewTypeoverride)	BaseModel
ConfigDictFieldznutricoach-weekly-operations-v1r
   WEEKLY_OPERATIONS_SCHEMA@0000000000000000000000000000000000000000000000000000000000000000ZERO_DIGESTCustomerKeyCustomerIdentityDigest^[0-9a-f]{64}$#^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}$c                      e Zd ZdZdZdZdZy)DayStatez4Terminal timeliness states for one KST calendar day.	submittedmissedlate_submittedN)__name__
__module____qualname____doc__	SUBMITTEDMISSEDLATE_SUBMITTED     /home/cube/projects/richard/traning coach/task-12-independent-verification-r9/build/hermes-full/dualcoach/profile/checkin_cli/weekly_operations.pyr   r      s    >IF%Nr%   r   c                  F     e Zd ZU dZdZded<   d fdZedd       Z xZ	S )	WeeklyOperationsErrorz/Base typed failure for weekly-operations state.reasonztuple[str, ...]	__slots__c                2    t         |   |       || _        y )N)super__init__r*   )selfr*   	__class__s     r&   r.   zWeeklyOperationsError.__init__"   s     !r%   c                     d| j                    S )Nzweekly operations failed: r)   r/   s    r&   __str__zWeeklyOperationsError.__str__&   s    +DKK=99r%   )r*   strreturnNone)r5   r4   )
r   r   r   r    r+   __annotations__r.   r   r3   __classcell__)r0   s   @r&   r(   r(      s)    9!,I," : :r%   r(   c                      e Zd ZdZy)WeeklyOperationsInputErrorz.An append request violates the typed contract.Nr   r   r   r    r$   r%   r&   r:   r:   +   s    8r%   r:   c                      e Zd ZdZy)WeeklyOperationsConflictz-An append conflicts with durable day history.Nr;   r$   r%   r&   r=   r=   /   s    7r%   r=   c                      e Zd ZdZy)WeeklyOperationsCorruptionz6Durable sidecar bytes violate the hash-chain contract.Nr;   r$   r%   r&   r?   r?   3   s    @r%   r?   c                      e Zd ZdZy)"WeeklyOperationsAlreadyInitializedz;Atomic initialization found an already-published authority.Nr;   r$   r%   r&   rA   rA   7   s    Er%   rA   c                      e Zd ZdZy)#CanonicalAuthorityAlreadyRegisteredz?A customer already has one durable canonical authority binding.Nr;   r$   r%   r&   rC   rC   ;   s    Ir%   rC   c                      e Zd ZdZy)#WeeklyOperationsAuthorityCompromisez=A pinned authority namespace no longer names its bound inode.Nr;   r$   r%   r&   rE   rE   ?       Gr%   rE   c                      e Zd ZdZy)$WeeklyOperationsPlatformNotSupportedz=The host cannot provide required descriptor-only publication.Nr;   r$   r%   r&   rH   rH   C   rF   r%   rH   T)frozenslotsc                  *    e Zd ZU ded<   ded<   ddZy)CanonicalPinintsequencer4   digestc                v    | j                   dk  st        j                  | j                        t	        d      y )Nr   zcanonical pin)rN   _DIGEST	fullmatchrO   r:   r2   s    r&   __post_init__zCanonicalPin.__post_init__L   s3    ==1 1 1$++ > F,_== !Gr%   Nr5   r6   r   r   r   r7   rS   r$   r%   r&   rL   rL   G   s    MK>r%   rL   c                  *    e Zd ZU ded<   ded<   ddZy)SourceLineager4   event_idevent_digestc                    t         j                  | j                        t        j                  | j                        t        d      y )Nzsource lineage)_OPAQUErR   rX   rQ   rY   r:   r2   s    r&   rS   zSourceLineage.__post_init__V   s?    T]]+3w7H7HIZIZ7[7c,-=>> 8dr%   NrT   rU   r$   r%   r&   rW   rW   Q   s    M?r%   rW   c                  2    e Zd ZU dZded<   dZded<   ddZy)ReminderIdentityN
str | Nonereservation_idaudit_idc                    | j                   | j                  fD ]%  }|t        j                  |      t	        d       y )Nzreminder identity)r_   r`   r[   rR   r:   )r/   values     r&   rS   zReminderIdentity.__post_init__`   s=    ))4==9E W%6%6u%=%E01DEE :r%   rT   )r   r   r   r_   r7   r`   rS   r$   r%   r&   r]   r]   [   s    !%NJ%HjFr%   r]   c                      e Zd ZU dZ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dZe	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	y)WeeklyOperationInputz<One requested append, parsed before the filesystem boundary.r   customer_identity_digestr   kst_dayr   staterL   	canonicalr   occurred_atNSourceLineage | NonesourceReminderIdentity | Nonereminderc                
   t         j                  | j                        t        d      | j                  j                         t        d      k7  s'| j                  j                         | j                  k7  rt        d      y )Nzcustomer identity digest	   )hoursz$occurred time must match the KST day)	rQ   rR   re   r:   ri   	utcoffsetr   r   rf   r2   s    r&   rS   z"WeeklyOperationInput.__post_init__r   sq    T::;C,-GHH%%'91+==AQAQAVAVAX\`\h\hAh,-STT Bir%   c           	     r    t        j                  d|      t        d       | t        |      ||||||      S )z>Consume a trusted raw key and retain only its opaque identity.z[a-z0-9][a-z0-9_-]{2,63}zcustomer key)rerR   r:   re   )clscustomer_keyrf   rg   rh   ri   rk   rm   s           r&   for_customerz!WeeklyOperationInput.for_customerx   sA     <<3\BJ,^<<+L97E9Vaciksttr%   rT   )NN)ru   r   rf   r   rg   r   rh   rL   ri   r   rk   rj   rm   rl   r5   rd   )
r   r   r   r    r7   rk   rm   rS   classmethodrv   r$   r%   r&   rd   rd   f   s    F44MO#'F '(,H%,U  (,,0u!u u 	u
  u u %u *u 
u ur%   rd   c                  x   e Zd ZU dZ edd      Zded<   ded<    ed	
      Zded<   ded<   ded<    ed      Z	ded<    ed	
      Z
ded<    edd      Zded<    edd	      Zded<    edd      Zded<    edd      Zded<    ed	
      Zded<   d ed!<    ed	
      Zded"<    ed	
      Zded#<   y)$WeeklyOperationRowzAStrict file-boundary representation of one immutable sidecar row.Tforbid)rI   extrazClassVar[ConfigDict]model_configz*Literal['nutricoach-weekly-operations-v1']schema_versionr   )patternr   re   r   rf   r   rg   r   )gerM   canonical_sequencer4   canonical_digestNr   )defaultr~   r^   source_event_idsource_event_digestreminder_reservation_idreminder_audit_idpredecessor_row_digestr   occurred_at_kstlogical_key
row_digest)r   r   r   r    r   r|   r7   r   re   r   r   r   r   r   r   r   r   r   r$   r%   r&   ry   ry      s    K)34x)PL&P>>7<EV7W4WMO#qk)!*;<c<"'>d"eOZe&+DBS&TT*/Fl*mZm$)$@f$gzg"'0A"BCB%67K7$56J6r%   ry   c                  "    e Zd ZU ded<   ded<   y)AppendResultry   rowboolappendedNr   r   r   r7   r$   r%   r&   r   r      s    	Nr%   r   c                  "    e Zd ZU ded<   ded<   y)RepairResultrM   retained_rowsremoved_bytesNr   r$   r%   r&   r   r      s    r%   r   c                    | rt        |       dkD  sd| v rt        d      d|  }t        t        j                  |j                               j                               S )zAIrreversibly derive the durable identity at the trusted boundary.i    zcustomer identityz2nutricoach-weekly-operations-customer-identity-v1 )lenr:   r   hashlibsha256encode	hexdigest)ru   materials     r&   re   re      sT    3|,t3t|7K()<==D\NSH!'..1B"C"M"M"OPPr%   c                n    d|  }t        j                  |j                               j                         S )z.Derive the opaque flat-layout filename digest.z(nutricoach-weekly-operations-storage-v1 )r   r   r   r   )customer_identityr   s     r&   customer_storage_digestr      s/    :;L:MNH>>(//+,6688r%   c                    t          d|  d|j                          d| }t        j                  |j	                               j                         S )z<Return a deterministic privacy-safe logical append identity.r   )r   	isoformatr   r   r   r   )r   rf   identityr   s       r&   operation_logical_keyr      sM    *+2.?-@7CTCTCVBWWYZbYcdH>>(//+,6688r%   c                   |r
t               ndh}| j                  d|      }t        j                  |ddd      j	                         S )z9Encode one row using the durable canonical JSON contract.r   json)modeexcludeFT),:)ensure_ascii	sort_keys
separators)set
model_dumpr   dumpsr   )r   include_digestexcludedrb   s       r&   canonical_weekly_rowr      s?    "0|nHNNN9E::e%4JW^^``r%   c                ^    t        j                  t        | d            j                         S )z=Digest one row body without trusting its supplied row digest.F)r   )r   r   r   r   )r   s    r&   weekly_row_digestr      s"    >>.s5IJTTVVr%   )ru   r   r5   r   )r   r   r5   r4   )r   r   rf   r   r   r4   r5   r4   )r   ry   r   r   r5   bytes)r   ry   r5   r4   )6r    
__future__r   r   r   rs   dataclassesr   r   r   r   enumr   typingr	   r
   r   r   r   pydanticr   r   r   r   r7   r   r4   r   r   compilerQ   r[   r   	Exceptionr(   r:   r=   r?   rA   rC   rE   rH   rL   rW   r]   rd   ry   r   r   re   r   r   r   r   r$   r%   r&   <module>r      s   @ "   	 ! . .  > > 1 1"C % CU mS) !93? 
"**&
'
"**;
<&w &:I :9!6 984 8A!6 AF)C FJ*B JH*D HH+E H $d#> > $> $d#? ? $? $d#F F $F $d#u u $uD7 7( $d#  $
 $d#  $
Q99aWr%   