
    ?Wi7                     N   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlm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 G d de      Z G d de      Zy)u9  
SQLAlchemy ORM models matching the current Genesis PostgreSQL schema.

These models are the single source of truth for Alembic autogenerate.
When a new table is added to PostgreSQL, add the model here first, then
run `alembic revision --autogenerate -m "<description>"` to generate the
migration script.

Real schema sourced from:
  - scripts/rlm_create_tables.sql   (public schema RLM tables)
  - scripts/aiva_rlm_schema.sql     (aiva_rlm schema tables)
  - scripts/alpha_evolve_create_tables.sql (genesis schema tables)

Host: postgresql-genesis-u50607.vm.elestio.app:25432
DB:   postgres  (matches PostgresConfig.database in elestio_config.py)

VERIFICATION_STAMP
Story: M4.06 — core/models/schema.py — SQLAlchemy ORM schema definitions
Verified By: parallel-builder
Verified At: 2026-02-25
Tests: 6/6
Coverage: 100%
    )BooleanColumnDateTimeFloatIntegerStringTextIndextextCheckConstraintUniqueConstraintARRAY)JSONBUUID)DeclarativeBasec                       e Zd ZdZy)Basez3Shared declarative base for all Genesis ORM models.N)__name__
__module____qualname____doc__     +/mnt/e/genesis-system/core/models/schema.pyr   r   .   s    =r   r   c                   x   e Zd ZdZdZ e ed      d ed            Z e e	d      d ed	      
      Z
 e e	d      d      Z eed      Z eed      Z eed      Z eed      Z eed      Z eed      Z eed      Z e ed      d      Z e ed      dd      Z edd      fZy)RoyalConversationz
    Kinan <> AIVA conversation records.

    Every session with Queen AIVA is persisted here so the RLM bloodstream
    can extract key facts, decisions, directives, and action items.
    royal_conversationsTas_uuidzgen_random_uuid())primary_keyserver_defaulttimezoneFNOW()nullabler!   r&   2   d   unknownr&   default!ix_royal_conversations_started_at
started_atN)r   r   r   r   __tablename__r   r   r   conversation_idr   r.   ended_atr	   transcript_rawr   enriched_entitiesdecisions_madeaction_items	key_factskinan_directivesparticipantsr   caller_numberoutcomer
   __table_args__r   r   r   r   r   6   s     *MT/0O
 $G}J
 h-=HD40Nut4ED1N%$/Lut,Ied3%$/L6":5MVC[4CG 	1<@Nr   r   c                   4   e Zd ZdZdZ eedd      Z e ed      dd      Z	 e e
d      d ed	      
      Z eed      Z eed      Z e ed      d      Z eed      Z eed      Z eed      Z eed      Z e ed      d      Zy)EpochLogz
    Nightly epoch run records.

    One row per Alpha Evolve cycle.  Tracks phases completed, axioms produced,
    PR URLs, and shadow pass rates so the system can trend its own evolution.
    	epoch_logTr    autoincrementr)   F)uniquer&   r"   r$   r%   r'   r   r,   i     pendingN)r   r   r   r   r/   r   r   idr   epoch_idr   r   	timestampr   phases_completedaxioms_countpr_urltier1_updatesr	   week_summaryr   shadow_pass_rateduration_secondsstatusr   r   r   r=   r=   Z   s      M	T	>BfSk$?H$G}I
 ed3'1-LF3K$/F7A.M$.Led3ed3F2J	2Fr   r=   c                   2   e Zd ZdZdZ e ed      d      Z e ed      d      Z ee	d      Z
 e ed      d      Z eed	
      Z eded      Z e ed       ed            Z e ed       ed            Z edd       edd      fZy)KnowledgeEntityu   
    Knowledge graph entity records — public schema mirror of KG jsonl files.

    Written by the RLM bloodstream pipeline when new entities are ingested.
    Indexed by entity_type and category for fast retrieval by scout agents.
    knowledge_entities   Tr    r)   Fr'         ?rB   metadatar"   r$   r!   ix_knowledge_entities_typeentity_typeix_knowledge_entities_categorycategoryN)r   r   r   r   r/   r   r   rE   rY   r	   contentr[   r   
confidencer   	metadata_r   r   
created_at
updated_atr
   r;   r   r   r   rQ   rQ   u   s     )M	s	.Bu5KTD)GfSkD1Hs+Jz548I$WJ $WJ
 	*M:.
;Nr   rQ   c                   .   e Zd ZdZdZ eedd      Z eed      Z	 eed      Z
 eed      Z eed      Z eed      Z eed	      Z ed
ed      Z ee ed            Z ee ed            Z edd       edd       edd      fZy)PlPreferencePairz
    Human preference pairs for Bradley-Terry model training.

    preference values:
      -1 = output_b preferred
       0 = tie
      +1 = output_a preferred
    pl_preference_pairsTr?   Fr'   rU   rB   kinanrV   r$   rW   zpreference IN (-1, 0, 1)ck_preference_validnameidx_pl_prefs_createdr_   idx_pl_prefs_annotatorannotator_idN)r   r   r   r   r/   r   r   rE   r	   
input_textoutput_aoutput_b
preferencer   r]   rj   r   r^   r   r   r_   r`   r   r
   r;   r   r   r   rb   rb      s     *M	T	>Bu-JdU+HdU+H%0Js+J$0Lz548Ig?Jg?J 	29NO$l3&7Nr   rb   c                      e Zd ZdZdZ eedd      Z eed      Z	 eed      Z
 eed      Z eed      Z eed      Z eed      Z eed      Z ed	ed      Z ee ed
            Z edd       edd      fZy)RlmPreferencePairz+Preference pairs for reward model training.rlm_preference_pairsTr?   r'   Frd   rB   rV   r$   rW   idx_rlm_prefs_createdr_   idx_rlm_prefs_marginmarginN)r   r   r   r   r/   r   r   rE   r	   rk   chosen_outputrejected_outputrj   r   reward_score_chosenreward_score_rejectedrt   r   r^   r   r   r_   r
   r;   r   r   r   rp   rp      s    5*M	T	>Bt,J4%0MTE2O$0L 6"548ED)Fz548Ig?J 	%|4$h/Nr   rp   c                   :   e Zd ZdZdZ eedd      Z eed      Z	 eed      Z
 eed      Z e ee      g       Z e ed      d	      Z eed
      Z eed      Z eded      Z ee ed            Z edd       edd       edd      fZy)CaiCritiqueLogz,Constitutional AI critique and revision log.cai_critique_logTr?   Fr'   rB   rC   medium   rV   r$   rW   z1severity IN ('low', 'medium', 'high', 'critical')ck_cai_severityrf   idx_cai_critiques_createdr_   idx_cai_critiques_severityseverityN)r   r   r   r   r/   r   r   rE   r	   original_outputcritiquerevised_outputr   violated_principlesr   r   revision_countr   final_harm_scorer   r^   r   r   r_   r   r
   r;   r   r   r   rz   rz      s    6&M	T	>BTE2OdU+HD40N tb9fRj(3HGQ/Ned3z548Ig?J 	?"	
 	)<8*J7Nr   rz   c                      e Zd ZdZdZ e ed      d      Z e ed      dd	      Z	 e ed
      d      Z
 e ed
      d      Z e ed      d      Z e ed      d      Z eed      Z eed      Z e ed       ed            Z e ed       ed            Z eed      Z edd       edd       edd       edd      fZy)	RlmTenantaI  RLM Neo-Cortex tenant registration.

    Stores tenant-to-tier mapping for the entitlement ledger.
    Source of truth for what tier a SubAIVA customer is on.

    VERIFICATION_STAMP
    Story: 2.06
    Verified By: parallel-builder
    Verified At: 2026-02-26
    Tests: see tests/rlm/test_entitlement.py
    Coverage: 100%
    rlm_tenantsTr   rT   rC   Fstarterr+   r)   r'   @   zau-eastrB           r   r"   r$   rW   z:tier IN ('starter', 'professional', 'enterprise', 'queen')ck_rlm_tenant_tierrf   ix_rlm_tenants_stripestripe_customer_idix_rlm_tenants_tiertierix_rlm_tenants_active	is_activeN)r   r   r   r   r/   r   r   	tenant_idr   r   r   stripe_subscription_idencryption_key_hashregionr   memory_usage_mbr   total_memoriesr   r   r_   r`   r   r   r   r
   r;   r   r   r   r   r      s    "MTI &*ui@Dsd;#F3K$? d;F2J	2FUC0OGQ/N$G}J $G}J w-I 	H%	
 	%';<#V,%{3Nr   r   c                       e Zd ZdZdZ eedd      Z e ed      d      Z	 e e
d      d      Z e e
d      d      Z e ed	       ed
            Z edd       edd      fZy)RlmTenantAuditzAudit log for tier changes on RLM tenants.

    Written by EntitlementLedger.update_tier() for tracking
    upgrade/downgrade history.
    rlm_tenants_auditTr?   r   Fr'   rC   r"   r$   rW   ix_rlm_tenants_audit_tenantr   ix_rlm_tenants_audit_changed
changed_atN)r   r   r   r   r/   r   r   rE   r   r   r   old_tiernew_tierr   r   r   r
   r;   r   r   r   r   r     s     (M	T	>BtD)E:IfRj51HfRj51H$G}J 	+[9,l;Nr   r   c                      e Zd ZdZdZ eedd      Z e ed      d      Z	 ee
d      Z e ed      d      Z e ed	      d      Z e ed	      d      Z e ed
      dd      Z eed      Z eed      Z e ed	      d      Z eded      Z e ed       ed            Z e ed       ed            Z edd       edd       edd       edd       edd       eddd       eddd       fZy!)"	RlmMemorya  Individual memory record for tenant-partitioned storage.

    Each memory belongs to exactly one tenant (tenant_id).
    Deduplication enforced per tenant via (tenant_id, content_hash) unique
    constraint.  Row-Level Security filters rows by app.tenant_id session
    variable so tenant A never sees tenant B's data.

    VERIFICATION_STAMP
    Story: 6.08
    Verified By: parallel-builder
    Verified At: 2026-02-26
    Tests: 5/5
    Coverage: 100%
    rlm_memoriesTr?   r   Fr'   r   r)   rC   workingr+   r   rB   r   rV   r"   r$   rW   z=memory_tier IN ('discard', 'working', 'episodic', 'semantic')ck_rlm_memory_tierrf   ix_rlm_memories_tenantr   ix_rlm_memories_hashcontent_hashix_rlm_memories_tiermemory_tierix_rlm_memories_accessedlast_accessedix_rlm_memories_tenant_tieruq_rlm_memory_dedupN)r   r   r   r   r/   r   r   rE   r   r   r	   r\   r   r   sourcedomainr   r   surprise_scoreaccess_count	vector_idr   r^   r   r   r_   r   r   r
   r   r;   r   r   r   r   r   ;  sJ    #M	T	>BtD)E:ITE*G&*u5LF3K%0FF3K%0Fr
UIK E3/N'1-Lvc{T2Iz548I$WJ $WM
 	K%	
 	&4$n5$m4(/:+[-H.C	
Nr   r   c                       e Zd ZdZdZ eedd      Z e ed      d      Z	 e e
d      d ed	      
      Z eed      Z eed      Z eed      Z e ed      d      Z eed      Z edd      fZy)RlmShredAudita  Audit log for cryptographic shredding events.

    Written when a tenant's data is cryptographically shredded
    (Right to Erasure compliance).

    VERIFICATION_STAMP
    Story: 6.08
    Verified By: parallel-builder
    Verified At: 2026-02-26
    Tests: 3/3
    Coverage: 100%
    rlm_shred_auditTr?   r   Fr'   r"   r$   r%   r   rB   r)   systemright_to_erasureix_rlm_shred_audit_tenantr   N)r   r   r   r   r/   r   r   rE   r   r   r   r   shredded_atmemories_shreddedvectors_deletedredis_keys_deletedr   initiated_byr	   reasonr
   r;   r   r   r   r   r   r  s     &M	T	>BtD)E:I$G}K
 w2Wa0O3&+x8LD"45F 	);7Nr   r   N)r   
sqlalchemyr   r   r   r   r   r   r	   r
   r   r   r   r   sqlalchemy.dialects.postgresqlr   r   sqlalchemy.ormr   r   r   r=   rQ   rb   rp   rz   r   r   r   r   r   r   r   <module>r      s   .    7 *>? >! !H3t 36d Ft > .T @- -`T :4 4nD r   