"""told2_logic.py: the arithmetic of the replica-free told-gear runner (card 03 rerun; review/CLAUDE-QUESTION-TOLD-V2-2026-10-13.md, design accepted in principle by review/ASTRA-TOLD-V2-2026-10-13.md). No torch and no MuJoCo, so the Mac can test it. The UNMODIFIED upstream planner plans in executed command units u in [-1, 1] (gear 25 is the real motor). Only the model boundary is reparametrised with a diagonal scale S (knee entry `half`, every other entry 1): - model.next, model.reward, model.Q receive m = S u (non-mutating: a new array/tensor; the caller's u is never touched); - model.pi's proposal m_pi (sampled action and info['mean']) is returned as u = clip(S^-1 m_pi, -1, 1). With half = 1 every conversion multiplies by exactly 1.0 (the identity arm, still measured). No actuator doubling: the planner's u is already the executed command. The helpers work on numpy arrays and torch tensors alike. """ import math import numpy as np import told_logic as TL HALF = TL.HALF_BOX # 0.5: model-space knee bound, what gear 25 delivers in gear-50 units ARMS = ("identity", "K-plan2") RUN_EPISODES = TL.RUN_EPISODES # 10, bank 1000 episodes 0 to 9 (unchanged from card 03) def scale(action_dim, knee_index, half): """S as a vector: the knee entry `half`, every other entry 1.""" if not (0 <= knee_index < action_dim): raise ValueError(f"knee index {knee_index} outside an action of {action_dim}") if not (isinstance(half, (int, float)) and math.isfinite(half) and 0 < half <= 1): raise ValueError(f"half must lie in (0, 1], got {half!r}") s = np.ones(action_dim) s[knee_index] = half return s def to_model(u, s): """m = S u, a NEW array or tensor (the input is never modified).""" return u * s def to_command(m, s, knee_index, half): """u = S^-1 m, a NEW array or tensor; the KNEE entry alone is clipped to [-1, 1], and only when half < 1. At factor one nothing is clipped (the released planner does not clamp policy samples; Cursor, TOLD-V2).""" u = m / s if half < 1: u[..., knee_index] = u[..., knee_index].clip(-1.0, 1.0) return u CAP_MINUTES = 60 # the named-list allowance, fixed EPISODE_MINUTES = 1.5 # card 03 measured about 1.3; margin WRITE_MINUTES = 2.0 # result files def told_arm_fits(elapsed_minutes, episodes=RUN_EPISODES, cap=CAP_MINUTES): """Admission before the told arm (Astra, TOLD-V2): actual elapsed time plus a conservative remaining estimate must fit the fixed cap; otherwise the job keeps the identity record and writes INCOMPLETE. Never a kill or an extension.""" if not (isinstance(elapsed_minutes, (int, float)) and math.isfinite(elapsed_minutes) and elapsed_minutes >= 0): raise ValueError(f"elapsed must be finite and non-negative, got {elapsed_minutes!r}") return elapsed_minutes + EPISODE_MINUTES * episodes + WRITE_MINUTES <= cap def action_repeat_consistent(commands, repeat=2): """Every decision's control steps carry the same command (the wrapper passes one array to both steps).""" if len(commands) % repeat: return False return all(list(commands[i]) == list(commands[i + k]) for i in range(0, len(commands), repeat) for k in range(1, repeat)) def outcome(valid, arm_mean, df_bar, n, t): """Card 03's repaired classes, unchanged (told_logic.outcome).""" return TL.outcome(valid, arm_mean, df_bar, n, t) def progress_total(identity_passed=True): """1 torque line plus 20 evaluated episodes (10 identity, 10 told); 11 when the job stops after identity.""" return 1 + RUN_EPISODES + (RUN_EPISODES if identity_passed else 0)