fix(state): repository iv_rv_history time-stable + input validation

Risponde alla code review di 395191e:
- iv_rv_history accetta as_of (default now UTC) invece di
  affidarsi al clock SQLite, rendendo i test time-stable.
- Valida max_days > 0 e raise se as_of/reference sono naive.
- Aggiunge 3 test sulle nuove guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
root
2026-05-08 23:00:06 +00:00
parent 395191ea13
commit 8221aba10f
2 changed files with 72 additions and 7 deletions
+16 -3
View File
@@ -414,20 +414,31 @@ class Repository:
*,
asset: str,
max_days: int,
as_of: datetime | None = None,
) -> list[Decimal]:
"""Lista IV-RV ordinata ASC sull'intervallo `[now-max_days, now]`.
"""Lista IV-RV ordinata ASC sull'intervallo `[as_of - max_days, as_of]`.
Esclude righe con ``fetch_ok=0`` o ``iv_minus_rv IS NULL``.
Usata dal validator quando il gate adattivo è abilitato.
Args:
as_of: Reference time for the rolling window. Defaults to
``datetime.now(UTC)``. Tests can pin a fixed value.
"""
if max_days <= 0:
raise ValueError(f"max_days must be positive, got {max_days}")
ref = as_of if as_of is not None else datetime.now(UTC)
if ref.tzinfo is None:
raise ValueError("as_of must be timezone-aware")
cutoff = ref - timedelta(days=max_days)
rows = conn.execute(
"SELECT iv_minus_rv FROM market_snapshots "
"WHERE asset = ? "
" AND fetch_ok = 1 "
" AND iv_minus_rv IS NOT NULL "
" AND timestamp >= datetime('now', ?) "
" AND timestamp >= ? "
"ORDER BY timestamp ASC",
(asset, f"-{int(max_days)} days"),
(asset, _enc_dt(cutoff)),
).fetchall()
return [Decimal(str(r["iv_minus_rv"])) for r in rows]
@@ -445,6 +456,8 @@ class Repository:
``dvol IS NOT NULL``) entro la tolerance. Usato dal Vol-of-Vol
guard per stimare DVOL N ore fa.
"""
if reference.tzinfo is None:
raise ValueError("reference must be timezone-aware")
ref_lo = (reference - timedelta(minutes=tolerance_minutes)).astimezone(UTC)
ref_hi = (reference + timedelta(minutes=tolerance_minutes)).astimezone(UTC)
row = conn.execute(