From 7f9968b0b2332406adafcfaa787f802bae373229 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Tue, 1 Sep 2026 23:43:01 -0500 Subject: [PATCH] =?UTF-8?q?feat(affect):=20two-axis=20affect=20from=20the?= =?UTF-8?q?=20error=20stream=20(spec=20=C2=A75)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ailed/affect.py | 56 ++++++++++++++++++++++++++++++++++++++ tests/test_affect.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/ailed/affect.py create mode 100644 tests/test_affect.py diff --git a/src/ailed/affect.py b/src/ailed/affect.py new file mode 100644 index 0000000..1df2e44 --- /dev/null +++ b/src/ailed/affect.py @@ -0,0 +1,56 @@ +"""Two-axis affect from the organism's own error stream. Spec §5. + +Pure functions: state in, state out. No torch, no gradient by construction. +""" +from __future__ import annotations + +import math +from dataclasses import dataclass, replace + + +@dataclass(frozen=True) +class AffectConfig: + rho_fast: float = 0.2 # fast error baseline rate + rho_slow: float = 0.02 # slow error baseline rate, must be < rho_fast + rho_a: float = 0.5 # arousal reactivity + rho_v: float = 0.1 # valence reactivity + reward_scale: float = 1.0 # s_r + decay: float = 0.2 # between-episode decay toward neutral + eps: float = 1e-6 + + +@dataclass(frozen=True) +class Affect: + valence: float = 0.0 # v in [-1, 1] + arousal: float = 0.0 # a in [0, 1] + fast: float = 0.0 # L̄_fast + slow: float = 0.0 # L̄_slow + steps: int = 0 + + @property + def axes(self) -> tuple[float, float]: + """(valence, arousal). Consumers read by name; this is the only positional export.""" + return (self.valence, self.arousal) + + +def update(state: Affect, loss: float, cfg: AffectConfig, reward: float = 0.0) -> Affect: + if state.steps == 0: + fast = slow = loss # baselines initialise to the first loss: step one is not a surprise + else: + fast = state.fast + cfg.rho_fast * (loss - state.fast) + slow = state.slow + cfg.rho_slow * (loss - state.slow) + denom = slow + cfg.eps + a_target = math.tanh(max(0.0, loss - slow) / denom) + v_target = math.tanh((slow - fast) / denom + reward / cfg.reward_scale) + return Affect( + valence=state.valence + cfg.rho_v * (v_target - state.valence), + arousal=state.arousal + cfg.rho_a * (a_target - state.arousal), + fast=fast, + slow=slow, + steps=state.steps + 1, + ) + + +def decay(state: Affect, cfg: AffectConfig) -> Affect: + k = 1.0 - cfg.decay + return replace(state, valence=state.valence * k, arousal=state.arousal * k) diff --git a/tests/test_affect.py b/tests/test_affect.py new file mode 100644 index 0000000..bcc2c27 --- /dev/null +++ b/tests/test_affect.py @@ -0,0 +1,65 @@ +from ailed.affect import Affect, AffectConfig, update, decay + +CFG = AffectConfig() + + +def run(losses, rewards=None, cfg=CFG): + s = Affect() + rewards = rewards or [0.0] * len(losses) + out = [] + for l, r in zip(losses, rewards): + s = update(s, l, cfg, reward=r) + out.append(s) + return out + + +def test_first_step_is_not_a_surprise(): + s = update(Affect(), 5.0, CFG) + assert s.arousal == 0.0 + assert s.fast == 5.0 and s.slow == 5.0 + + +def test_constant_loss_settles_at_zero(): + s = run([1.0] * 200)[-1] + assert abs(s.arousal) < 1e-6 + assert abs(s.valence) < 1e-6 + + +def test_spike_raises_arousal_then_decays(): + hist = run([1.0] * 100 + [10.0] + [1.0] * 60) + assert hist[100].arousal > 0.4 + assert hist[-1].arousal < 0.05 + + +def test_decreasing_loss_gives_positive_valence(): + losses = [1.0 - 0.004 * i for i in range(200)] + assert run(losses)[-1].valence > 0.05 + + +def test_increasing_loss_gives_negative_valence(): + losses = [0.2 + 0.004 * i for i in range(200)] + assert run(losses)[-1].valence < -0.05 + + +def test_reward_moves_valence_with_flat_loss(): + s = run([1.0] * 100, rewards=[0.0] * 50 + [1.0] * 50)[-1] + assert s.valence > 0.3 + + +def test_decay_moves_toward_neutral(): + s = Affect(valence=0.8, arousal=0.6, fast=1.0, slow=1.0, steps=10) + d = decay(s, CFG) + assert d.valence == 0.8 * (1 - CFG.decay) + assert d.arousal == 0.6 * (1 - CFG.decay) + assert d.fast == 1.0 and d.slow == 1.0 + + +def test_update_is_pure(): + s = Affect(valence=0.1, arousal=0.2, fast=1.0, slow=1.0, steps=5) + update(s, 3.0, CFG) + assert s == Affect(valence=0.1, arousal=0.2, fast=1.0, slow=1.0, steps=5) + + +def test_axes_by_name(): + s = Affect(valence=0.3, arousal=0.7) + assert s.axes == (0.3, 0.7)