diff --git a/goodwe_controller/test_ev69.py b/goodwe_controller/test_ev69.py index 4d64abd..7dca210 100644 --- a/goodwe_controller/test_ev69.py +++ b/goodwe_controller/test_ev69.py @@ -516,6 +516,56 @@ check(f"T9: Manual, foreign 16 A at E+5: mismatch at E+5 ({r.first_ms(E)}), " E + 36) and n_some() == 1 and not r.calls_from(E + 1, 10 ** 9, True)) +# Manual version of T10 (formal Dev note 3: Manual's adopt ends the carry) +r0 = mw(300) +E = r0.ended +r = mw(E + 200, hook2=lambda t, w, d, s: setattr( + d, "cur", 8.0 if t == E + 3 else 12.0 if t == E + 20 else d.cur)) +check(f"T10 (Manual): the charger takes the target 8 A at E+3 (Manual's adopt " + f"ends the carry: {r.carry[E + 4]}), then shows the old close 12 A at " + f"E+20: mismatch at E+20 ({r.first_ms(E)}), hold at E+50 " + f"({r.first_hold(E)}), 1 INFO line ({n_some()})", + r.carry[E + 4] == [] and near(r.first_ms(E), E + 20, E + 21) + and near(r.first_hold(E), E + 50, E + 51) and n_some() == 1) + + +# T17 (i) in the cap-end shape (M14): the 0 A after a return is blocked by +# the used-up cut tries, so only the return itself can end the carry. +def ce_gone(cls, lo, hi, show_at, show_a): + w, drv = mk_ce(cls) + st = {"cap": False} + + def h(t, w_, d, s): + nz = [1 for tt, a in d.calls if a > 0] + if nz and not s["cap"]: + s["cap"] = True + set_cap(w_, 6.0) + if t == show_at: + d.cur = show_a + return Run(w, drv, st=st).go(130, 600, hook=h, + gone=lambda t: lo <= t <= hi) + + +E = ce_gone(None, -1, -1, 10 ** 6, 0.0).ended +rn = ce_gone(None, E + 3, E + 8, E + 9, 10.0) +rbs = ce_gone(Base69, E + 3, E + 8, E + 9, 10.0) +same = all((rn.ms[t], rn.hold[t], rn.ncalls[t]) == + (rbs.ms[t], rbs.hold[t], rbs.ncalls[t]) for t in range(E + 9, 601)) +check(f"T17 (i, cap end): the charger gone E+3..E+8 and back at E+9 showing " + f"10 A, the cut tries used up: the carry stands while it is gone " + f"({rn.carry[E + 8]}) and ends with the return ({rn.carry[E + 9]}); no " + f"mismatch ({rn.first_ms(E + 9)}) and every cycle equals de733f4", + E is not None and rn.carry[E + 8] != [] and rn.carry[E + 9] == [] + and rn.clean(E + 9, 600) and same) + +# R-MB: the standing 0 A is no take (the cur > TOL_A guard in `_watch`) +r = s1(400, press_at=None, show_at=None) +check(f"T4 (0 A guard): S1-np-nt, the charger stands at the carried 0 A: " + f"gave_up stays True 142-400 and nothing non-zero is written " + f"({r.calls_from(143, 400, True)})", + all(r.gu[t] for t in range(142, 401)) + and not r.calls_from(143, 400, True)) + # =========================================================================== # print("Rules the code keeps (Dev notes)") # =========================================================================== # diff --git a/goodwe_controller/test_ev69_fuzz.py b/goodwe_controller/test_ev69_fuzz.py index 8c10e0e..1d42e87 100644 --- a/goodwe_controller/test_ev69_fuzz.py +++ b/goodwe_controller/test_ev69_fuzz.py @@ -8,14 +8,20 @@ and the select are taken), phase changes begun by the plan (Solar) or the user (Manual), late takes of ANY value the writer tried, foreign values (some equal to a tried value), presses, unknown and stale reads, the charger gone and back. Order A applies a cycle's events before its step, order B after it (the -event is read one cycle later). +event is read one cycle later). The phase gap is shortened (as test_ev22_fuzz +does) so a run holds many changes. Every cycle checks, by an oracle that does not read the carry: - N1: after a phase change ends without its value taken (MAX_TRIES, or Solar's cap end), while nothing has ended the bound (a write, a press, a hold or count, a return, a take of what the writer sent or wants), the charger showing a value the change tried gives no mismatch and no hold. -- N2: no non-zero write while a count runs or a hold stands. +- N1b: once a bound has ended (an own take, a new own write, a user change, a + hold, Solar's return) a late take of an old value is another writer's: the + fuzz shows it for WRITER_S + 3 s with nothing else happening and asserts + the hold (skipped when no own value is left to judge against: the EV-22 + start-state blind spot). +- N2: no non-zero write while a count runs (Solar) or a hold stands. - N4: no carry under a hold, after a press, or after a write that is not itself followed by an end. - N5: no write above the clamp or the cap, none in 0-6 A. @@ -24,8 +30,8 @@ Every cycle checks, by an oracle that does not read the carry: - N8: a select only at 0 A. - N9: at most one CHARGER_OTHER (Solar) / one INFO line (Manual) per hold. N3 (the cut never waits on a false hold) follows from N1 and the rows T1, T5, -T19. The branches are counted and a floor is asserted, so a fuzz that never -reaches them fails. +T19. The plan's floors are asserted per writer / per bound end, so a fuzz that +never reaches a branch fails. Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ @@ -40,9 +46,11 @@ sys.path.insert(0, os.path.join(HERE, "tools")) import ev69_kit as K # noqa: E402 from app import carmode as CM # noqa: E402 +CM.PHASE_GAP_S = 40.0 # many changes in 1200 s (test_ev22_fuzz does too) SEEDS = int(os.environ.get("EV69_FUZZ_SEEDS", "10")) STEPS = int(os.environ.get("EV69_FUZZ_STEPS", "1200")) COUNT = {} +CAUSES = ("take", "write", "user", "hold", "return") def bump(k, n=1): @@ -59,7 +67,7 @@ def fuzz(kind, seed): w.cut_key = 0 else: w = CM.ManualWriter() - mode = {"refuse": 0.97, "raise": 0.0} + mode = {"refuse": 1.0 if kind == "solar" else 0.97, "raise": 0.0} def take(a, d): if a == 0.0: @@ -72,24 +80,54 @@ def fuzz(kind, seed): bad = [] s = dict(armed=False, vals=[], seq_own=[], hold_rises=0, press_t=-1, prev_hold=False, was_gone=False, gone=False, tried=set(), - nsel=0) + kind=None, cap_counted=False, seq_n0=0, pend=None, inj=None, end_t=-9999) clamp = {"v": 16.0} def ev(t): - if rnd.random() < (0.25 if w.seq is not None else 0.01): - clamp["v"] = rnd.choice([16.0, 12.0, 10.0, 8.0, 8.0]) + if s["inj"] is not None: # a directed old-value take + drv.cur = s["inj"]["v"] + return + closed = w.seq is not None and (kind == "manual" or [ + 1 for tt, a in drv.calls[s["seq_n0"]:] if a > 0]) + if rnd.random() < (0.4 if closed else 0.01): + clamp["v"] = rnd.choice([16.0, 14.0, 12.0, 10.0, 8.0, 7.0, 6.0]) st["clamp"] = clamp["v"] if kind == "solar" and rnd.random() < 0.01: K.set_cap(w, rnd.choice([None, 6.0, 6.0, 7.0])) + if kind == "solar" and w.seq is not None and w.cut_a is None \ + and [1 for tt, a in drv.calls[s["seq_n0"]:] if a > 0] \ + and rnd.random() < 0.4: + # the EV-60 cap end shape: a close went, then the cap comes, the + # earlier cut tries of this key being used up + K.set_cap(w, 6.0) + w.cut_fails, w.cut_fail_at = max(w.cut_fails, 2), -100.0 + if w.hold and rnd.random() < 0.03: + w.user_changed() # a press lifts a hold + if kind == "manual": + st["rev"] += 1 + s["press_t"] = t + bump("press") + if kind == "solar" and not s["armed"] and w.seq is None \ + and (w.gave_up or w.fail_at is not None) \ + and rnd.random() < 0.05: + w.user_changed() # a press lifts the give-up + s["press_t"] = t + bump("press") if rnd.random() < 0.004: - mode["refuse"] = rnd.choice([0.0, 0.5, 0.97, 1.0, 1.0, 1.0]) + mode["refuse"] = rnd.choice([0.0, 0.5, 0.97, 1.0, 1.0, 1.0] + if kind == "manual" else + [0.97, 1.0, 1.0, 1.0, 1.0, 1.0]) mode["raise"] = rnd.choice([0.0, 0.0, 0.3]) # a phase change: Solar's plan decides it, Manual's user asks for it - if w.seq is None and rnd.random() < 0.03: + if w.seq is None and rnd.random() < (0.1 if kind == "solar" else 0.03): n_to = 3 if drv.ph == 1 else 1 if kind == "solar": if not w.plan.switch: - w.plan._set(rnd.choice([8.0, 10.0, 12.0]), float(t), n_to) + w.plan.n = drv.ph # the plan believes the phases read + w.plan._set(rnd.choice([10.0, 12.0, 14.0]), float(t), n_to) + clamp["v"] = st["clamp"] = 16.0 # it falls in the change + K.set_cap(w, None) # a cap comes later + w.cut_fails = 0 else: w.user_changed() st["rev"] += 1 @@ -98,7 +136,8 @@ def fuzz(kind, seed): s["press_t"] = t # a late take of any value the writer tried (the ended change's # values first) - if rnd.random() < 0.08: + if rnd.random() < (0.0 if (kind == "solar" and w.seq is not None) + else 0.08): pool = [v for v in s["vals"] if v > 0] if ( s["armed"] and rnd.random() < 0.6) else sorted( v for v in s["tried"] if v > 0) @@ -109,8 +148,9 @@ def fuzz(kind, seed): if s["armed"] and v in s["vals"]: bump("late_take_ended_val") if v != s["vals"][-1]: - bump("late_take_nonlast") - if rnd.random() < 0.005: + bump(f"late_nonlast_{kind}") + calm = kind == "solar" and w.seq is not None # let a change run + if rnd.random() < (0.0 if calm else 0.005): drv.cur = rnd.choice([5.0, 7.0, 9.0, 11.0, 13.0, 15.0] + sorted(s["tried"])) bump("foreign") @@ -120,15 +160,20 @@ def fuzz(kind, seed): st["rev"] += 1 s["press_t"] = t bump("press") - if rnd.random() < 0.01: + if rnd.random() < (0.0 if calm else 0.01): drv.cur = 0.0 - s["gone"] = rnd.random() < (0.8 if s["gone"] else 0.003) + s["gone"] = s["gone"] and rnd.random() < 0.8 or ( + not s["gone"] and rnd.random() < 0.003) + if kind == "solar" and s["armed"] and rnd.random() < 0.08: + s["gone"] = True # a charger return inside the bound for t in range(STEPS): if not order_b: ev(t) - gone, unk, stale = s["gone"], rnd.random() < 0.02, rnd.random() < 0.02 - hold0, ms0 = w.hold, w.mismatch_since + inj = s["inj"] + gone = False if inj else s["gone"] + unk = False if inj else rnd.random() < 0.02 + stale = False if inj else rnd.random() < 0.02 n0, sel0 = len(drv.calls), len(drv.sel) before_seq = w.seq is not None if before_seq: @@ -181,6 +226,8 @@ def fuzz(kind, seed): if w.hold and not s["prev_hold"]: s["hold_rises"] += 1 bump("hold") + if t - s["end_t"] <= 300: + bump("foreign_hold_after_end") s["prev_hold"] = w.hold lines = len([1 for _t, lv, m in K.LOG if (kind == "solar" and K.OTHER in m) @@ -189,12 +236,35 @@ def fuzz(kind, seed): bad.append((kind, seed, t, "N9 more lines than holds", lines, s["hold_rises"])) ended_now = before_seq and w.seq is None + if not before_seq and w.seq is not None: + bump(f"seq_start_{kind}") + s["seq_n0"] = len(drv.calls) + if ended_now: + tk = (kind == "solar" and w.taken) or ( + kind == "manual" and w.last_a is not None) + bump(f"seq_end_{kind}_{'taken' if tk else 'refused'}") if w.hold and w._carry: bad.append((kind, seed, t, "N4 carry under a hold", w._carry)) if press_now and w._carry and not ended_now: bad.append((kind, seed, t, "N4 carry after a press", w._carry)) if wrote and w._carry and not ended_now: bad.append((kind, seed, t, "N4 carry after a write", w._carry)) + # the directed old-value take (N1b) + if inj is not None: + inj["n"] += 1 + if wrote or press_now or w.seq is not None \ + or drv.cur != inj["v"] or inj["n"] > 60: + s["inj"] = None # something else happened: no verdict + bump("inj_aborted") + elif inj["n"] >= CM.WRITER_S + 3: + s["inj"] = None + if w.hold: + bump(f"bound_{inj['cause']}_held") + bump("bound_held") + else: + bad.append((kind, seed, t, "N1b an old value after the " + f"bound ended ({inj['cause']}) is not held", + inj["v"], w.mismatch_since)) # the oracle's bound: armed by an end whose last value is not taken, # disarmed by anything that ends it (a superset of the code's ends) took = (kind == "solar" and w.taken) or ( @@ -203,22 +273,66 @@ def fuzz(kind, seed): s["vals"] = [0.0] + [a for a in s["seq_own"] if a > 0] + [ a for tt, a in new if a > 0] s["armed"] = True + s["end_t"] = t + s["cap_counted"] = False + s["pend"] = None + s["kind"] = "cap" if (kind == "solar" and not wrote + and w.cut_fails >= CM.MAX_TRIES) else "max" bump("end") - if kind == "solar" and w.cut_fails >= CM.MAX_TRIES: + bump(f"end_{kind}") + if len(set(a for a in s["vals"] if a > 0)) > 1: + bump(f"end_distinct_{kind}") + if s["kind"] == "cap": bump("end_cap") elif s["armed"]: - if wrote or press_now or w.hold or gone or s["was_gone"] \ - or took: + cause = ("hold" if w.hold else "user" if press_now + else "write" if wrote + else "return" if (gone or s["was_gone"]) + else "take" if took else None) + if cause is not None: s["armed"] = False + if not (kind == "manual" and cause == "return"): + s["pend"] = (cause, list(s["vals"])) elif w.mismatch_since is not None: if seen is not None and seen > CM.TOL_A and seen in s["vals"]: bad.append((kind, seed, t, "N1 a late take of a tried " "value is held as another writer's", seen, s["vals"])) s["armed"] = False # a foreign value: judged as ever + s["pend"] = ("hold", list(s["vals"])) bump("armed_foreign") elif seen is not None and seen > CM.TOL_A and seen in s["vals"]: bump("armed_take_seen") + if s["kind"] == "cap" and not s["cap_counted"]: + s["cap_counted"] = True + bump("cap_end_take") + # start a directed old-value take once the bound has ended and the + # writer is calm: the value must not be own by any other route + if s["pend"] is not None and s["pend"][0] == "hold" \ + and not w.hold and w.mismatch_since is None: + s["pend"] = None # the foreign value went: no hold + if s["pend"] is not None and s["inj"] is None and not gone \ + and not unk and not stale and w.seq is None and not wrote \ + and not press_now and rnd.random() < 0.5 \ + and (s["pend"][0] != "hold" or w.hold): + cause, vals = s["pend"] + own = w._own_values() + pool = [v for v in vals if v > CM.TOL_A + and all(abs(v - o) > CM.TOL_A for o in own)] + if not pool: # any other value the writer tried + pool = [v for v in sorted(s["tried"]) if v > CM.TOL_A + and all(abs(v - o) > CM.TOL_A for o in own)] + s["pend"] = None + if not pool: + bump(f"inj_no_value_{cause}") + elif not own and not w.hold: + bump("inj_blind") # nothing own to judge against + elif cause != "hold" and w.hold: + bump("inj_other_hold") + else: + s["inj"] = dict(v=rnd.choice(pool), cause=cause, n=0) + drv.cur = s["inj"]["v"] + bump("inj_started") s["was_gone"] = gone if order_b: ev(t) @@ -229,17 +343,19 @@ allbad = [] for kind in ("solar", "manual"): for seed in range(SEEDS): allbad += fuzz(kind, seed) -for tag in ("N1", "N2", "N4", "N5", "N6", "N7", "N8", "N9"): +for tag in ("N1", "N1b", "N2", "N4", "N5", "N6", "N7", "N8", "N9"): K.check(f"T21 {tag}: holds in every cycle of {SEEDS} seeds x {STEPS} s, " f"Solar and Manual, both orders " - f"({[b for b in allbad if b[3].startswith(tag)][:2]})", - not [b for b in allbad if b[3].startswith(tag)]) + f"({[b for b in allbad if b[3].startswith(tag + ' ')][:2]})", + not [b for b in allbad if b[3].startswith(tag + ' ')]) print(" counts", dict(sorted(COUNT.items()))) -floor = {"end": 20, "end_cap": 3, "late_take_ended_val": 10, - "late_take_nonlast": 5, "armed_take_seen": 20, "foreign": 20, - "hold": 5} +floor = {"late_nonlast_solar": 10, "late_nonlast_manual": 5, + "cap_end_take": 5, "bound_take_held": 5, "bound_write_held": 5, + "bound_user_held": 5, "bound_hold_held": 5, "bound_return_held": 5, + "foreign_hold_after_end": 5, "end": 20, "end_cap": 3, + "armed_take_seen": 20, "foreign": 20, "hold": 5} if STEPS >= 1200 and SEEDS >= 10: - K.check(f"T21: the fuzz reaches its branches " + K.check(f"T21: the fuzz reaches the plan's floors " f"({ {k: COUNT.get(k, 0) for k in floor} }, floor {floor})", all(COUNT.get(k, 0) >= v for k, v in floor.items())) K.done()