File size: 2,062 Bytes
7bad702
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Phase 4.3 — character evolution state.

One row per character (lazy-created on first post-match evolution run).
Holds cumulative drift across all post-match applications:

- slider_drift: signed deltas on the four personality sliders
- opening_scores: per-opening EMA of result signal in [-1, +1]
- trap_memory: list of trap patterns with fell_for / avoided counters
- tone_drift: confidence + tilt baselines (fed into MoodState initialisation)

See docs/phase_4_evolution.md for the math.
"""

from __future__ import annotations

from datetime import datetime
from typing import Any

from sqlalchemy import JSON, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


def _now() -> datetime:
    return datetime.utcnow()


class CharacterEvolutionState(Base):
    __tablename__ = "character_evolution_state"

    # Primary key = character_id (one row per character). FK enforces
    # deletion cleanup when a character is removed.
    character_id: Mapped[str] = mapped_column(
        String(36),
        ForeignKey("characters.id", ondelete="CASCADE"),
        primary_key=True,
    )

    slider_drift: Mapped[dict[str, float]] = mapped_column(
        JSON, nullable=False, default=dict
    )
    opening_scores: Mapped[dict[str, float]] = mapped_column(
        JSON, nullable=False, default=dict
    )
    trap_memory: Mapped[list[dict[str, Any]]] = mapped_column(
        JSON, nullable=False, default=list
    )
    tone_drift: Mapped[dict[str, float]] = mapped_column(
        JSON, nullable=False, default=dict
    )

    matches_processed: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, server_default="0"
    )
    # Idempotency guard — we skip re-running evolution on a match we've
    # already processed. Nullable on first-ever application.
    last_match_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    last_updated_at: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, default=_now, onupdate=_now
    )