infra: a held reset does not climb the ladder and the last-archive marker expires, so a restart-proof trap is reset in bounded time (row 70 review)
This commit is contained in:
parent
d7f5ef5ff7
commit
a84e617fc9
3 changed files with 130 additions and 6 deletions
|
|
@ -32,6 +32,7 @@ QUIET = 6 * 3600 # this long without a suspected report starts the ladder ov
|
||||||
HOLD = 3 * 3600 # restart-only spacing while the reset budget is spent
|
HOLD = 3 * 3600 # restart-only spacing while the reset budget is spent
|
||||||
DAY = 24 * 3600
|
DAY = 24 * 3600
|
||||||
MAX_RESETS = 2 # milestone resets per DAY
|
MAX_RESETS = 2 # milestone resets per DAY
|
||||||
|
AGAIN = 12 * 3600 # after this long the last reset's archive may be restored again (row 70)
|
||||||
PROTECT = 6 * 3600 # a species owned this recently is not reset away (row 70)
|
PROTECT = 6 * 3600 # a species owned this recently is not reset away (row 70)
|
||||||
VETO_LIMIT = 3 # model vetoes of one step before it goes ahead anyway
|
VETO_LIMIT = 3 # model vetoes of one step before it goes ahead anyway
|
||||||
MODEL_BUDGET = 90 # seconds across all models in one decision
|
MODEL_BUDGET = 90 # seconds across all models in one decision
|
||||||
|
|
@ -235,6 +236,8 @@ def run(now=None, sleep=time.sleep, clock=None):
|
||||||
state.update(bestRank=rank, level=0)
|
state.update(bestRank=rank, level=0)
|
||||||
state.pop("lastReset", None)
|
state.pop("lastReset", None)
|
||||||
level = 0
|
level = 0
|
||||||
|
if "lastReset" in state and now - state.get("lastResetAt", 0) >= AGAIN:
|
||||||
|
state.pop("lastReset", None)
|
||||||
last = state.get("lastSuspectedAt")
|
last = state.get("lastSuspectedAt")
|
||||||
if level and last and now - last > QUIET:
|
if level and last and now - last > QUIET:
|
||||||
log({"at": now, "event": "ladder-reset", "why": "quiet", "rank": rank})
|
log({"at": now, "event": "ladder-reset", "why": "quiet", "rank": rank})
|
||||||
|
|
@ -288,6 +291,11 @@ def run(now=None, sleep=time.sleep, clock=None):
|
||||||
resets_left = MAX_RESETS - len(state["resets"])
|
resets_left = MAX_RESETS - len(state["resets"])
|
||||||
action, target = plan(level, rank, state.get("bestRank", 0), resets_left, state.get("lastReset"))
|
action, target = plan(level, rank, state.get("bestRank", 0), resets_left, state.get("lastReset"))
|
||||||
protected = action == "reset" and now - state.get("speciesAt", -PROTECT) < PROTECT
|
protected = action == "reset" and now - state.get("speciesAt", -PROTECT) < PROTECT
|
||||||
|
# A reset turned into a restart (protected catch, or the archive just restored) does not climb:
|
||||||
|
# the level keeps its slot for when the window ends, so it never drops a rung deeper (row 70).
|
||||||
|
held_reset = protected or (level and resets_left and action == "restart" and rank is not None
|
||||||
|
and (rank if level == 1 else max(state.get("bestRank", 0), rank) - 1)
|
||||||
|
== state.get("lastReset"))
|
||||||
if protected:
|
if protected:
|
||||||
action, target = "restart", None
|
action, target = "restart", None
|
||||||
if action == "restart" and level and now - acted < HOLD:
|
if action == "restart" and level and now - acted < HOLD:
|
||||||
|
|
@ -318,12 +326,12 @@ def run(now=None, sleep=time.sleep, clock=None):
|
||||||
notice(base, "countdown", now)
|
notice(base, "countdown", now)
|
||||||
sleep(COUNTDOWN)
|
sleep(COUNTDOWN)
|
||||||
started = int(clock())
|
started = int(clock())
|
||||||
# Recorded before acting, so a helper killed mid-step has still climbed and spent the reset.
|
# Recorded before acting, so a helper killed mid-step has still climbed (unless held) and spent the reset.
|
||||||
state.update(level=level + 1, actedAt=started, lastAction=action, vetoes=0)
|
state.update(level=level if held_reset else level + 1, actedAt=started, lastAction=action, vetoes=0)
|
||||||
state.pop("observedAt", None)
|
state.pop("observedAt", None)
|
||||||
if action == "reset":
|
if action == "reset":
|
||||||
state["resets"].append(started)
|
state["resets"].append(started)
|
||||||
state["lastReset"] = target
|
state.update(lastReset=target, lastResetAt=started)
|
||||||
write_json(STATE, state)
|
write_json(STATE, state)
|
||||||
notice(base, "acting", started)
|
notice(base, "acting", started)
|
||||||
ok = act(action, target)
|
ok = act(action, target)
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,12 @@ a ladder one step per trap that outlives the previous step:
|
||||||
follows real progress is a new trap, and it starts again from a restart.
|
follows real progress is a new trap, and it starts again from a restart.
|
||||||
- **Protected catches:** a milestone reset never erases a species owned in the last six hours
|
- **Protected catches:** a milestone reset never erases a species owned in the last six hours
|
||||||
(`PROTECT`, from `window.progress.species`). Within that window the step is a restart, spaced by
|
(`PROTECT`, from `window.progress.species`). Within that window the step is a restart, spaced by
|
||||||
the hold like a spent budget, and `history.jsonl` records a `protect` event.
|
the hold like a spent budget, and `history.jsonl` records a `protect` event. A reset turned into a
|
||||||
|
restart this way does not climb the ladder: the level keeps its slot and the reset comes, at the
|
||||||
|
same rung, once the window ends. Evolution also counts as a species owned.
|
||||||
- **No archive twice in a row:** a reset never restores the same archive as the last reset,
|
- **No archive twice in a row:** a reset never restores the same archive as the last reset,
|
||||||
until a new best rung is reached. A restore is deterministic, so the same archive replays the
|
until a new best rung is reached or twelve hours have passed (`AGAIN`; a trap no restart clears
|
||||||
|
is reset again in bounded time, no later than before this rule). Such a step does not climb. A restore is deterministic, so the same archive replays the
|
||||||
same run into the same trap. Row 70's second reset to rung 11 matched the first one event for
|
same run into the same trap. Row 70's second reset to rung 11 matched the first one event for
|
||||||
event: the same Zubat caught, the same trap an hour later, and the catch erased. That step is
|
event: the same Zubat caught, the same trap an hour later, and the catch erased. That step is
|
||||||
a restart instead.
|
a restart instead.
|
||||||
|
|
|
||||||
|
|
@ -155,10 +155,30 @@ class RecoveryTests(unittest.TestCase):
|
||||||
def test_the_last_resets_archive_is_not_restored_again(self):
|
def test_the_last_resets_archive_is_not_restored_again(self):
|
||||||
acted = T0 - recover.HOLD - 100
|
acted = T0 - recover.HOLD - 100
|
||||||
recover.write_json(recover.STATE, {"level": 3, "bestRank": 12, "resets": [acted],
|
recover.write_json(recover.STATE, {"level": 3, "bestRank": 12, "resets": [acted],
|
||||||
"actedAt": acted, "lastReset": 11})
|
"actedAt": acted, "lastReset": 11, "lastResetAt": acted})
|
||||||
self.confirm(T0)
|
self.confirm(T0)
|
||||||
self.assertEqual(self.acts, [("restart", None)])
|
self.assertEqual(self.acts, [("restart", None)])
|
||||||
|
|
||||||
|
def test_a_held_reset_does_not_climb_and_the_marker_expires(self):
|
||||||
|
# protected: the restart keeps the level's slot, so the reset comes at the same rung later
|
||||||
|
owned = T0 - 600
|
||||||
|
recover.write_json(recover.STATE, {"level": 1, "bestRank": 12, "resets": [], "actedAt": 0,
|
||||||
|
"speciesAt": owned})
|
||||||
|
self.confirm(T0)
|
||||||
|
self.assertEqual(self.acts, [("restart", None)])
|
||||||
|
self.assertEqual(recover.load(recover.STATE, {}).get("level"), 1)
|
||||||
|
# the archive just restored: same, and the marker is forgotten after AGAIN
|
||||||
|
self.acts.clear()
|
||||||
|
reset_at = T0 - recover.HOLD - 100
|
||||||
|
recover.write_json(recover.STATE, {"level": 2, "bestRank": 12, "resets": [reset_at], "actedAt": reset_at,
|
||||||
|
"lastReset": 11, "lastResetAt": reset_at})
|
||||||
|
self.confirm(T0 + 1000)
|
||||||
|
self.assertEqual(self.acts, [("restart", None)])
|
||||||
|
self.assertEqual(recover.load(recover.STATE, {}).get("level"), 2)
|
||||||
|
self.acts.clear()
|
||||||
|
self.tick(reset_at + recover.AGAIN + 60)
|
||||||
|
self.assertNotIn("lastReset", recover.load(recover.STATE, {}))
|
||||||
|
|
||||||
def test_a_reset_records_its_archive_and_a_new_best_rung_forgets_it(self):
|
def test_a_reset_records_its_archive_and_a_new_best_rung_forgets_it(self):
|
||||||
recover.write_json(recover.STATE, {"level": 2, "bestRank": 12, "resets": [], "actedAt": 0})
|
recover.write_json(recover.STATE, {"level": 2, "bestRank": 12, "resets": [], "actedAt": 0})
|
||||||
self.confirm(T0)
|
self.confirm(T0)
|
||||||
|
|
@ -317,6 +337,99 @@ BUILD = "lif-1ms-f64-v2/pokered-unique8-v7/abc/fly-kc-mbon-rstdp-v2"
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(recover.os.geteuid() == 0, "the wrapper ignores test overrides as root")
|
@unittest.skipIf(recover.os.geteuid() == 0, "the wrapper ignores test overrides as root")
|
||||||
|
class ClosedLoopTests(unittest.TestCase):
|
||||||
|
"""Row 70 review L1: a trap no restart clears, every reset replaying it exactly (closed loop)."""
|
||||||
|
M = 60
|
||||||
|
R11 = dict(events=[(20, "pokedex"), (21, "trainer"), (35, "trainer"), (44, "area"), (46, "milestone"),
|
||||||
|
(52, "pokedex")], trap=58, rank_after=[(46, 12)], start_rank=11)
|
||||||
|
R12 = dict(events=[(7, "pokedex")], trap=12, rank_after=[], start_rank=12)
|
||||||
|
R10 = dict(events=[(30, "area"), (60, "milestone"), (90, "milestone"), (100, "pokedex")], trap=110,
|
||||||
|
rank_after=[(60, 11), (90, 12)], start_rank=10)
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.temp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(self.temp.cleanup)
|
||||||
|
root = Path(self.temp.name)
|
||||||
|
for name, value in (("REPORT", "loop.json"), ("STATE", "s.json"), ("HISTORY", "h.jsonl"),
|
||||||
|
("NOTICE", "n.json")):
|
||||||
|
patcher = patch.object(recover, name, root / value)
|
||||||
|
patcher.start()
|
||||||
|
self.addCleanup(patcher.stop)
|
||||||
|
for name, value in (("restorable_rungs", lambda: {9, 10, 11, 12}), ("verdict", lambda report: (None, None))):
|
||||||
|
patcher = patch.object(recover, name, value)
|
||||||
|
patcher.start()
|
||||||
|
self.addCleanup(patcher.stop)
|
||||||
|
self.env = patch.dict("os.environ", {}, clear=False)
|
||||||
|
self.env.start()
|
||||||
|
self.addCleanup(self.env.stop)
|
||||||
|
for key in ("FLY_LOOP_ROUTER_URL", "FLY_LOOP_MODELS", "FLY_LOOP_MODEL", "FLY_LOOP_ROUTER_KEY"):
|
||||||
|
recover.os.environ.pop(key, None)
|
||||||
|
|
||||||
|
def simulate(self, archives, hours, species_before=None):
|
||||||
|
"""The fly is trapped from T0; returns [(hours, action, target)]. A reset replays its archive."""
|
||||||
|
M = self.M
|
||||||
|
sim = dict(events=[(T0 - species_before * M, "pokedex")] if species_before else [],
|
||||||
|
rank=12, rank_changes=[], trap_from=T0, now=T0, acts=[])
|
||||||
|
|
||||||
|
def act(action, target):
|
||||||
|
sim["acts"].append((sim["now"], action, target))
|
||||||
|
if action == "reset":
|
||||||
|
a = archives[target]
|
||||||
|
sim["rank"] = a["start_rank"]
|
||||||
|
sim["events"] = [(sim["now"] + o * M, k) for o, k in a["events"]]
|
||||||
|
sim["rank_changes"] = [(sim["now"] + o * M, r) for o, r in a["rank_after"]]
|
||||||
|
sim["trap_from"] = None if a["trap"] is None else sim["now"] + a["trap"] * M
|
||||||
|
return True
|
||||||
|
|
||||||
|
recover.write_json(recover.STATE, {"bestRank": 12, "rank": 12, "level": 0})
|
||||||
|
with patch.object(recover, "act", side_effect=act):
|
||||||
|
t = T0
|
||||||
|
while t < T0 + hours * 3600:
|
||||||
|
for change in list(sim["rank_changes"]):
|
||||||
|
if change[0] <= t:
|
||||||
|
sim["rank"] = change[1]
|
||||||
|
sim["rank_changes"].remove(change)
|
||||||
|
win = [k for at, k in sim["events"] if t - 600 <= at <= t]
|
||||||
|
trapped = sim["trap_from"] is not None and t - sim["trap_from"] >= 600
|
||||||
|
recover.REPORT.write_text(json.dumps({
|
||||||
|
"at": t, "suspected": 1 if trapped else 0, "action": "none", "reason": "dominant",
|
||||||
|
"sequence": ["NEXT"], "milestone": {"rank": sim["rank"], "label": "X"}, "map": 59,
|
||||||
|
"window": {"progress": {
|
||||||
|
"lasting": sum(k in ("pokedex", "area", "trainer", "badge", "milestone") for k in win),
|
||||||
|
"species": sum(k == "pokedex" for k in win)}}}))
|
||||||
|
sim["now"] = t + 5
|
||||||
|
recover.run(now=t + 5, sleep=lambda s: None)
|
||||||
|
t += 300
|
||||||
|
return [((at - T0) / 3600, action, target) for at, action, target in sim["acts"]]
|
||||||
|
|
||||||
|
def test_a_restart_proof_trap_is_reset_in_bounded_time_and_again(self):
|
||||||
|
acts = self.simulate({11: self.R11, 12: self.R12, 10: self.R10, 9: self.R10}, 72, species_before=6)
|
||||||
|
resets = [a for a in acts if a[1] == "reset"]
|
||||||
|
# v0.7.0 resets again at 24.75 h; the ladder is no worse, and keeps resetting
|
||||||
|
self.assertTrue(resets and resets[0][0] <= 25, acts)
|
||||||
|
self.assertTrue(len([a for a in resets if a[0] <= 25]) >= 1, acts)
|
||||||
|
self.assertTrue(len(resets) >= 3, acts)
|
||||||
|
gaps = [b[0] - a[0] for a, b in zip(resets, resets[1:])]
|
||||||
|
self.assertTrue(all(g <= 25 for g in gaps), acts)
|
||||||
|
# the first step is a restart; the catch stays protected for 6 h
|
||||||
|
self.assertEqual(acts[0][1], "restart")
|
||||||
|
self.assertTrue(all(a[0] >= 6 - 0.2 for a in resets[:1]), acts)
|
||||||
|
|
||||||
|
def test_the_held_reset_is_the_rung_12_one_not_a_deeper_rung(self):
|
||||||
|
acts = self.simulate({11: self.R11, 12: self.R12, 10: self.R10, 9: self.R10}, 24, species_before=6)
|
||||||
|
self.assertEqual([a[2] for a in acts if a[1] == "reset"][:1], [12], acts)
|
||||||
|
|
||||||
|
def test_a_trap_the_rung_12_archive_escapes_ends_there(self):
|
||||||
|
clean = dict(self.R12, trap=None)
|
||||||
|
acts = self.simulate({11: self.R11, 12: clean, 10: self.R10, 9: self.R10}, 24, species_before=6)
|
||||||
|
self.assertEqual([a[1:] for a in acts if a[1] == "reset"], [("reset", 12)], acts)
|
||||||
|
self.assertEqual(len(acts), 3, acts)
|
||||||
|
|
||||||
|
def test_without_a_catch_the_ladder_resets_early(self):
|
||||||
|
acts = self.simulate({11: self.R11, 12: self.R12, 10: self.R10, 9: self.R10}, 24)
|
||||||
|
self.assertLessEqual(next(a[0] for a in acts if a[1] == "reset"), 1, acts)
|
||||||
|
|
||||||
|
|
||||||
class WrapperTests(unittest.TestCase):
|
class WrapperTests(unittest.TestCase):
|
||||||
"""infra/bin/fly-loop-reset against a fake flysim, systemctl, reset tool and archives."""
|
"""infra/bin/fly-loop-reset against a fake flysim, systemctl, reset tool and archives."""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue