fix(projection): align projection with ADR 0002

Use lifetime written bytes from the current segment's newest published
sample in the headline formula, select the Percentage-Used-implied
baseline after two increments in the segment (otherwise show the
too-coarse fact), and judge horizon agreement across existing horizons.
Make the vacuous projection tests assert unconditionally and add
coverage for implied-baseline gating and single-horizon agreement.
This commit is contained in:
xavierk
2026-10-05 19:45:38 +05:30
parent 117082af8c
commit 96fd1702fa
5 changed files with 168 additions and 45 deletions
+52 -17
View File
@@ -150,16 +150,40 @@ def _get_all_days(conn):
for r in cursor.fetchall()]
def _get_latest_published_sample(conn, segment_id):
"""Newest published sample in the segment: (lifetime bytes written, PU).
W_t is the lifetime counter, not a regime delta. Pending staging lives
in a separate table (ADR 0011), so only published samples are seen here.
bytes_written is DUW x 512000 as stored by the collector; fall back to
that conversion if only the raw counter is present.
"""
if segment_id is not None:
cursor = conn.execute(
"SELECT COALESCE(bytes_written, data_units_written * 512000), percentage_used "
"FROM samples WHERE segment_id = ? ORDER BY id DESC LIMIT 1",
(segment_id,),
)
else:
cursor = conn.execute(
"SELECT COALESCE(bytes_written, data_units_written * 512000), percentage_used "
"FROM samples ORDER BY id DESC LIMIT 1"
)
row = cursor.fetchone()
return (row[0], row[1]) if row else (None, None)
def _get_latest_pu(conn):
cursor = conn.execute("SELECT percentage_used FROM samples ORDER BY id DESC LIMIT 1")
row = cursor.fetchone()
return row[0] if row else None
def _get_pu_increments_in_segment(conn, segment_opened_at):
def _get_pu_increments_in_segment(conn, segment_id):
cursor = conn.execute(
"SELECT COUNT(DISTINCT percentage_used) FROM samples WHERE ts >= ?",
(segment_opened_at,),
"SELECT COUNT(DISTINCT percentage_used) FROM samples "
"WHERE segment_id = ? AND percentage_used IS NOT NULL",
(segment_id,),
)
row = cursor.fetchone()
return max(0, (row[0] if row else 0) - 1)
@@ -186,11 +210,24 @@ def _wall_clock_in_range(conn, start, end):
# Baseline resolution (§6.1, §6.2)
# ---------------------------------------------------------------------------
def _resolve_implied_baseline(conn, current_segment, facts):
"""No override exists: Percentage-Used-implied tier, or unavailable (§10)."""
if current_segment is not None:
_, p = _get_latest_published_sample(conn, current_segment["id"])
increments = _get_pu_increments_in_segment(conn, current_segment["id"])
if (increments >= IMPLIED_MIN_PU_INCREMENTS and p is not None
and IMPLIED_P_MIN <= p <= IMPLIED_P_MAX):
return (BaselineTier.IMPLIED, None,
"implied from vendor wear (%d%% used) — coarse" % p, facts)
facts.append("vendor wear estimate too coarse to imply endurance")
return BaselineTier.NONE, None, "no baseline", facts
def _resolve_baseline(conn, current_segment):
baseline = _get_baseline(conn)
facts: list[str] = []
if baseline is None:
return BaselineTier.NONE, None, "no baseline", facts
return _resolve_implied_baseline(conn, current_segment, facts)
mandatory = [baseline["source_url"], baseline["document_revision"],
baseline["entry_date"], baseline["model_string"],
@@ -386,7 +423,7 @@ def _evaluate_confidence(tier, rate, regime_days, days, current_segment,
supported_facts.append("recent data")
# 5. Horizon agreement
if scenario_range is not None and len(scenario_range.rates) >= 2:
if scenario_range is not None and len(scenario_range.rates) >= 1:
rl = list(scenario_range.rates.values())
if min(rl) > 0 and max(rl) / min(rl) > HORIZON_AGREEMENT_FACTOR:
failing = True
@@ -633,19 +670,17 @@ def compute_projection(conn, clock_now):
all_facts.append(cf)
headline_seconds = None
if state != ConfidenceState.UNSUPPORTED and rate is not None and rate > 0 and baseline is not None:
if tier in (BaselineTier.VERIFIED, BaselineTier.UNVERIFIED):
if state != ConfidenceState.UNSUPPORTED and rate is not None and rate > 0:
W_t, p = _get_latest_published_sample(
conn, current_segment["id"] if current_segment else None)
E_baseline = None
if tier in (BaselineTier.VERIFIED, BaselineTier.UNVERIFIED) and baseline is not None:
E_baseline = baseline["tbw_terabytes"] * TBW_TO_BYTES
elif tier == BaselineTier.IMPLIED:
p = _get_latest_pu(conn)
if p is not None and IMPLIED_P_MIN <= p <= IMPLIED_P_MAX:
E_baseline = 100 * regime_bytes / p
else:
E_baseline = None
else:
E_baseline = None
if E_baseline is not None:
headline_seconds = max(E_baseline - regime_bytes, 0) / rate
elif (tier == BaselineTier.IMPLIED and W_t is not None and p is not None
and IMPLIED_P_MIN <= p <= IMPLIED_P_MAX):
E_baseline = 100 * W_t / p
if E_baseline is not None and W_t is not None:
headline_seconds = max(E_baseline - W_t, 0) / rate
pu_line = _build_pu_context_line(conn, rate, segment_days, clock_now)