infra: the guard's baseline is robust to a step in its window (median, MAD)

A real recording (a flysim whose box load stepped 1.0 -> 0.66 inside the
10-minute window) widened the old mean/sd margin to 0.25 and hid a
genuine shadow-induced drop to 0.44-0.60. The baseline RTF is now the
median of the window, its spread 1.4826 x the median absolute deviation,
and the lag rate is taken over the most recent half. The recording now
trips at check 7; over 800 synthetic 3-hour runs shaped on the release
CT (flat 0.66, 0.77-0.99 and 0.6-1.0 noisy, per second and per sample)
there is no false trip, and all 400 degradations (to 0.55-0.75, and
0.66 -> 0.58) trip.
This commit is contained in:
acamilo 2026-09-30 03:16:36 +00:00
parent 504e4bb116
commit 6634b33e80

View file

@ -29,10 +29,12 @@
# The guard (SHADOW-01 review N2, recalibrated by round 2, G1). The live fly does not always run # The guard (SHADOW-01 review N2, recalibrated by round 2, G1). The live fly does not always run
# at real time (the release CT has run at a realtime factor of 0.66, and 0.77-0.99 after its cpuset # at real time (the release CT has run at a realtime factor of 0.66, and 0.77-0.99 after its cpuset
# rebalance), so the guard judges the live fly against ITSELF, before the shadow existed: # rebalance), so the guard judges the live fly against ITSELF, before the shadow existed:
# - `start` first measures a BASELINE over FLY_SHADOW_BASELINE_SECONDS (600): sixty 10-s means # - `start` first samples the live fly over FLY_SHADOW_BASELINE_SECONDS (600): sixty 10-s means
# of fly_realtime_factor (their mean and spread) and the lag growth rate of fly_lag_seconds # of fly_realtime_factor and fly_lag_seconds. The BASELINE is robust: the median RTF, its
# (seconds of lag per wall second: about 1 - RTF when the loop is behind real time). It # spread (1.4826 x the median absolute deviation) and the lag growth rate over the most recent
# refuses to start on fewer than half the samples running. # half (seconds of lag per wall second: about 1 - RTF when the loop is behind real time), so a
# step within the window neither sets the baseline nor widens the margin. It refuses to start
# on fewer than half the samples running.
# - Every 60 s the guard takes one 10-s sample. Over the last 5 samples of the same flysim # - Every 60 s the guard takes one 10-s sample. Over the last 5 samples of the same flysim
# process (a restart starts the window again) it compares the mean RTF and the lag growth # process (a restart starts the window again) it compares the mean RTF and the lag growth
# rate with the baseline. The margin is max(0.05, 4 x the baseline spread / sqrt(5)), so it # rate with the baseline. The margin is max(0.05, 4 x the baseline spread / sqrt(5)), so it
@ -128,14 +130,22 @@ def rate(samples):
return max(0.0, (best[-1]["lag"] - best[0]["lag"]) / (best[-1]["t"] - best[0]["t"])) return max(0.0, (best[-1]["lag"] - best[0]["lag"]) / (best[-1]["t"] - best[0]["t"]))
def baseline(samples): def baseline(samples):
"""Robust statistics of the running samples: a step in the live fly within the window (its own
warm-up, a load coming or going) must neither set the baseline nor widen the margin, as long
as most of the window is on one side of it. The RTF is the median; its spread is 1.4826 x the
median absolute deviation, which estimates the standard deviation without the step; the lag
growth rate is taken over the most recent half, after any early step."""
ok = [s for s in samples if running(s)] ok = [s for s in samples if running(s)]
if len(ok) < max(3, len(samples) // 2): if len(ok) < max(3, len(samples) // 2):
raise SystemExit("baseline: only %d of %d samples running" % (len(ok), len(samples))) raise SystemExit("baseline: only %d of %d samples running" % (len(ok), len(samples)))
rtf = [s["rtMean"] for s in ok] recent = ok[len(ok) // 2:]
mean = sum(rtf) / len(rtf) rtf = sorted(s["rtMean"] for s in ok)
sd = math.sqrt(sum((x - mean) ** 2 for x in rtf) / max(1, len(rtf) - 1)) median = rtf[len(rtf) // 2] if len(rtf) % 2 else (rtf[len(rtf) // 2 - 1] + rtf[len(rtf) // 2]) / 2
lag_rate = rate(ok) dev = sorted(abs(x - median) for x in rtf)
return {"rtfMean": mean, "rtfSd": sd, "lagRate": lag_rate if lag_rate is not None else 0.0, mad = dev[len(dev) // 2] if len(dev) % 2 else (dev[len(dev) // 2 - 1] + dev[len(dev) // 2]) / 2
sd = 1.4826 * mad
lag_rate = rate(recent)
return {"rtfMean": median, "rtfSd": sd, "lagRate": lag_rate if lag_rate is not None else 0.0,
"samples": len(ok), "margin": max(MIN_MARGIN, 4 * sd / math.sqrt(WINDOW))} "samples": len(ok), "margin": max(MIN_MARGIN, 4 * sd / math.sqrt(WINDOW))}
def decide(base, state, s): def decide(base, state, s):