diff --git a/goodwe_controller/test_ev69_fuzz.py b/goodwe_controller/test_ev69_fuzz.py index 56ea57f..4b6f303 100644 --- a/goodwe_controller/test_ev69_fuzz.py +++ b/goodwe_controller/test_ev69_fuzz.py @@ -3,34 +3,35 @@ Two passes, each SEEDS (10) seeds x STEPS (1200) s for Solar and for Manual, both cycle orders. Writer level (`tools/ev69_kit.py`: `SolarWriter` / -`ManualWriter` on a stub driver). -- Mixed pass: random clamp steps (also inside a change), a peak cut cap (and, - rarely, the EV-60 cap-end shape: `cut_fails` set to 2 after a close), - refused, taken and raised writes (refuse 0.0 / 0.5 / 0.97 / 1.0, 0 A and the - select 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), a 0 A reading, presses, unknown and stale reads, the charger - gone and back. All of it also while a Solar change runs. -- Directed pass, in addition: Solar changes run calm (no late take, foreign - value or 0 A reading inside a change, refuse 0.97 / 1.0 only, the clamp - falls only after a close, a cap with `cut_fails` 2 after the first close) - so that cap ends and Solar's distinct close values are reached often. -Both passes add: presses that lift a hold or a give-up, the charger gone while -a bound stands, the plan's phases set from the read phases, and N1b's directed -old-value take. Order A applies a cycle's events before its step, order B -after it (read one cycle later). The phase gap is shortened (as -test_ev22_fuzz does) so a run holds many changes. +`ManualWriter` on a stub driver). Order A applies a cycle's events before its +step, order B after it (read one cycle later). +- Mixed pass (`fuzz_mixed`): the round-1 event model of 9b09158, unchanged + (same random stream, same rates, PHASE_GAP_S as shipped). Random clamp steps, + a peak cut cap, refused and raised writes (0 A 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. Its floors are + a fixed list (MIXED_FLOOR), each asserted on the mixed counts alone. +- Directed pass (`fuzz_directed`, 4 x SEEDS seeds), in addition, with its + own floors (DIRECTED_FLOOR) asserted on the directed counts alone: Solar + changes run + calm (no late take, foreign value or 0 A reading inside a change, refuse + 0.97 / 1.0 only, the clamp falls only after a close, a cap with `cut_fails` + 2 after the first close) so that cap ends and Solar's distinct close values + are reached; presses that lift a hold or a give-up, the charger gone while a + bound stands, the plan's phases set from the read phases, PHASE_GAP_S 40 (as + test_ev22_fuzz does), and N1b's directed old-value take. 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. -- 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). +- N1b (directed pass): 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. @@ -40,8 +41,7 @@ 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 plan's floors are asserted per writer / per bound end, so a fuzz that -never reaches a branch fails. +T19. Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ @@ -56,20 +56,20 @@ 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): COUNT[k] = COUNT.get(k, 0) + n -def fuzz(kind, seed, directed=False): - rnd = random.Random(seed * 7 + (0 if kind == "solar" else 1) - + (1000 if directed else 0)) +def fuzz_mixed(kind, seed): + """9b09158's fuzz, event model unchanged. Only counters were added (no + random draw): late_nonlast_, cap_end_take, foreign_hold_after_end. + """ + rnd = random.Random(seed * 7 + (0 if kind == "solar" else 1)) order_b = seed % 2 == 1 st = {"a": rnd.choice([8.0, 10.0, 12.0]), "n": 1, "rev": 1, "clamp": 16.0} @@ -78,8 +78,7 @@ def fuzz(kind, seed, directed=False): w.cut_key = 0 else: w = CM.ManualWriter() - mode = {"refuse": 1.0 if (directed and kind == "solar") else 0.97, - "raise": 0.0} + mode = {"refuse": 0.97, "raise": 0.0} def take(a, d): if a == 0.0: @@ -92,7 +91,196 @@ def fuzz(kind, seed, directed=False): bad = [] s = dict(armed=False, vals=[], seq_own=[], hold_rises=0, press_t=-1, prev_hold=False, was_gone=False, gone=False, tried=set(), - kind=None, cap_counted=False, seq_n0=0, fh=False, fv=None, pend=None, inj=None, end_t=-9999) + nsel=0, cap=False, cap_counted=False, fv=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]) + 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 rnd.random() < 0.004: + mode["refuse"] = rnd.choice([0.0, 0.5, 0.97, 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: + 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) + else: + w.user_changed() + st["rev"] += 1 + st["n"] = n_to + st["a"] = rnd.choice([8.0, 10.0, 12.0, 14.0]) + s["press_t"] = t + # a late take of any value the writer tried (the ended change's + # values first) + if rnd.random() < 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) + if pool: + v = rnd.choice(pool) + drv.cur = v + bump("late_take") + if s["armed"] and v in s["vals"]: + bump("late_take_ended_val") + if v != s["vals"][-1]: + bump("late_take_nonlast") + bump(f"late_nonlast_{kind}") + if rnd.random() < 0.005: + drv.cur = rnd.choice([5.0, 7.0, 9.0, 11.0, 13.0, 15.0] + + sorted(s["tried"])) + s["fv"] = (t, drv.cur) + bump("foreign") + if rnd.random() < 0.005: + w.user_changed() + if kind == "manual": + st["rev"] += 1 + s["press_t"] = t + bump("press") + if rnd.random() < 0.01: + drv.cur = 0.0 + s["gone"] = rnd.random() < (0.8 if s["gone"] else 0.003) + + for t in range(STEPS): + if not order_b: + ev(t) + gone, unk, stale = s["gone"], rnd.random() < 0.02, rnd.random() < 0.02 + n0, sel0 = len(drv.calls), len(drv.sel) + before_seq = w.seq is not None + if before_seq: + s["seq_own"] = list(w.seq.get("own", ())) + drv.now = float(t) + K.CLOCK["t"] = t + if hasattr(w, "cut_quarter"): + w.cut_quarter(int(t // 900)) + seen = None if unk else drv.cur + rd = K.Rd(seen, drv.ph, power=0.0 if drv.cur == 0 else 1840.0) + rd.available = not gone + run.trk.obs_at = (w.last_done if (stale and w.last_done is not None) + else None) + cl = clamp["v"] + if kind == "solar": + K.LOOP.run_until_complete(w.step( + drv, rd, mode=CM.SOLAR, sun=CM.SunInputs(target_w=2000.0), + clamp=cl, now=float(t), tracker=run.trk, writing=True, + status="charging")) + else: + K.LOOP.run_until_complete(w.step( + drv, rd, mode=CM.MANUAL, want_a=st["a"], + want_phases=st["n"], rev=st["rev"], clamp=cl, now=float(t), + tracker=run.trk, writing=True, status="charging")) + new = drv.calls[n0:] + wrote = bool(new) + press_now = s["press_t"] == t + for tt, a in new: + if a > 0: + s["tried"].add(a) + if a > 0 and (w.hold or (kind == "solar" + and w.mismatch_since is not None)): + # judged after the step's own watch (a count a read cleared + # in this step is no count when the write goes out); Manual + # writes through a count and stops at the hold (EV-22) + bad.append((kind, seed, t, "N2 write while a count/hold " + "stood", a)) + if a == 0.0 and w.hold: + bad.append((kind, seed, t, "N7 0 A under a hold")) + cutv = getattr(w, "cut_a", None) + if a > cl + 1e-9 and not (cutv is not None and a <= cutv + 1e-9): + bad.append((kind, seed, t, "N5 above the clamp", a, cl)) + if 0 < a < CM.CH.MIN_A - 1e-9: + bad.append((kind, seed, t, "N5 in 0-6 A", a)) + if getattr(w, "cut_fails", 0) > CM.MAX_TRIES: + bad.append((kind, seed, t, "N6 cut tries over the cap")) + for tt, n_, cur_ in drv.sel[sel0:]: + if cur_ > CM.TOL_A: + bad.append((kind, seed, t, "N8 select over load", cur_)) + if w.hold and not s["prev_hold"]: + s["hold_rises"] += 1 + bump("hold") + fv = s["fv"] + if fv is not None and 0 <= t - fv[0] <= 60 \ + and drv.cur == fv[1] and 0 <= 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) + or (kind == "manual" and K.SOMETHING in m)]) + if lines > s["hold_rises"]: + bad.append((kind, seed, t, "N9 more lines than holds", lines, + s["hold_rises"])) + ended_now = before_seq and w.seq is None + 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 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 ( + kind == "manual" and w.last_a is not None) + if ended_now and not took and not gone: + 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"] = kind == "solar" and w.cut_fails >= CM.MAX_TRIES + s["cap_counted"] = False + bump("end") + if s["cap"]: + bump("end_cap") + elif s["armed"]: + if wrote or press_now or w.hold or gone or s["was_gone"] \ + or took: + s["armed"] = False + 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 + bump("armed_foreign") + elif seen is not None and seen > CM.TOL_A and seen in s["vals"]: + bump("armed_take_seen") + if s["cap"] and not s["cap_counted"]: + s["cap_counted"] = True + bump("cap_end_take") + s["was_gone"] = gone + if order_b: + ev(t) + return bad + + +def fuzz_directed(kind, seed): + """The directed pass (round 3's calm Solar pass), in addition.""" + rnd = random.Random(seed * 7 + (0 if kind == "solar" else 1) + 1000) + order_b = seed % 2 == 1 + st = {"a": rnd.choice([8.0, 10.0, 12.0]), "n": 1, "rev": 1, + "clamp": 16.0} + if kind == "solar": + w = K.swriter(8.0, ph=1) + w.cut_key = 0 + else: + w = CM.ManualWriter() + mode = {"refuse": 1.0 if kind == "solar" else 0.97, "raise": 0.0} + + def take(a, d): + if a == 0.0: + return True + if rnd.random() < mode["raise"]: + return "raise" + return rnd.random() >= mode["refuse"] + drv = K.Drv(8.0 if kind == "solar" else st["a"], take, ph=1) + run = K.Run(w, drv, kind=kind, st=st) + bad = [] + s = dict(armed=False, vals=[], seq_own=[], hold_rises=0, press_t=-1, + prev_hold=False, was_gone=False, gone=False, tried=set(), + kind=None, cap_counted=False, seq_n0=0, fv=None, pend=None, + inj=None, end_t=-9999) clamp = {"v": 16.0} def ev(t): @@ -101,23 +289,16 @@ def fuzz(kind, seed, directed=False): 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 directed: - p_clamp = 0.4 if closed else 0.01 # falls only after a close - else: - p_clamp = 0.25 if w.seq is not None else 0.01 - if rnd.random() < p_clamp: - clamp["v"] = rnd.choice( - [16.0, 14.0, 12.0, 10.0, 8.0, 7.0, 6.0] if directed - else [16.0, 12.0, 10.0, 8.0, 8.0]) + if rnd.random() < (0.4 if closed else 0.01): # falls after a close + 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 if directed else 0.03): + 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 (a minority path in - # the full-interference pass) + # 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: @@ -134,8 +315,7 @@ def fuzz(kind, seed, directed=False): bump("press") if rnd.random() < 0.004: mode["refuse"] = rnd.choice( - [0.97, 1.0, 1.0, 1.0, 1.0, 1.0] - if (directed and kind == "solar") else + [0.97, 1.0, 1.0, 1.0, 1.0, 1.0] if kind == "solar" else [0.0, 0.5, 0.97, 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 @@ -145,10 +325,9 @@ def fuzz(kind, seed, directed=False): if not w.plan.switch: 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) - if directed: - clamp["v"] = st["clamp"] = 16.0 # it falls later - K.set_cap(w, None) # a cap comes later - w.cut_fails = 0 + clamp["v"] = st["clamp"] = 16.0 # it falls later + K.set_cap(w, None) # a cap comes later + w.cut_fails = 0 else: w.user_changed() st["rev"] += 1 @@ -157,7 +336,7 @@ def fuzz(kind, seed, directed=False): s["press_t"] = t # a late take of any value the writer tried (the ended change's # values first) - calm = directed and kind == "solar" and w.seq is not None + calm = kind == "solar" and w.seq is not None if rnd.random() < (0.0 if calm else 0.08): pool = [v for v in s["vals"] if v > 0] if ( s["armed"] and rnd.random() < 0.6) else sorted( @@ -227,9 +406,6 @@ def fuzz(kind, seed, directed=False): s["tried"].add(a) if a > 0 and (w.hold or (kind == "solar" and w.mismatch_since is not None)): - # judged after the step's own watch (a count a read cleared - # in this step is no count when the write goes out); Manual - # writes through a count and stops at the hold (EV-22) bad.append((kind, seed, t, "N2 write while a count/hold " "stood", a)) if a == 0.0 and w.hold: @@ -253,7 +429,6 @@ def fuzz(kind, seed, directed=False): # the hold rose over a value the random foreign branch set # (never one of N1b's directed old-value takes) bump("foreign_hold_after_end") - s["fh"] = False s["prev_hold"] = w.hold lines = len([1 for _t, lv, m in K.LOG if (kind == "solar" and K.OTHER in m) @@ -326,7 +501,6 @@ def fuzz(kind, seed, directed=False): s["vals"])) s["armed"] = False # a foreign value: judged as ever s["pend"] = ("hold", list(s["vals"])) - s["fh"] = True bump("armed_foreign") elif seen is not None and seen > CM.TOL_A and seen in s["vals"]: bump("armed_take_seen") @@ -338,7 +512,6 @@ def fuzz(kind, seed, directed=False): 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 - s["fh"] = False 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 \ @@ -367,40 +540,43 @@ def fuzz(kind, seed, directed=False): return bad -allbad = [] +# Fixed floor lists (no fallback between passes). MIXED_FLOOR is 9b09158's +# list, each floor asserted on the mixed counts alone. DIRECTED_FLOOR holds +# the plan floors the mixed pass does not reach, asserted on the directed +# counts alone. +MIXED_FLOOR = {"end": 20, "end_cap": 3, "late_take_ended_val": 10, + "late_take_nonlast": 5, "armed_take_seen": 20, "foreign": 20, + "hold": 5} +DIRECTED_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} +TAGS = {"mixed": ("N1", "N2", "N4", "N5", "N6", "N7", "N8", "N9"), + "directed": ("N1", "N1b", "N2", "N4", "N5", "N6", "N7", "N8", "N9")} MIXED, DIRECTED = {}, {} -COUNT = MIXED # pass 1: the mixed pass, full interference -for kind in ("solar", "manual"): - for seed in range(SEEDS): - allbad += fuzz(kind, seed) -COUNT = DIRECTED # pass 2: calm Solar changes, in addition -for kind in ("solar", "manual"): - for seed in range(SEEDS): - allbad += fuzz(kind, seed, directed=True) -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, in both passes " - f"({[b for b in allbad if b[3].startswith(tag + ' ')][:2]})", - not [b for b in allbad if b[3].startswith(tag + ' ')]) -print(" mixed counts", dict(sorted(MIXED.items()))) -print(" directed counts", dict(sorted(DIRECTED.items()))) -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, "late_take_ended_val": 10, "foreign": 20, - "hold": 5} -# The mixed pass must reach these on its own (the base of the fuzz). -base = ("end", "armed_take_seen", "late_take_ended_val", "foreign", "hold", - "foreign_hold_after_end", "late_nonlast_manual") -if STEPS >= 1200 and SEEDS >= 10: - for k, v in floor.items(): - m, d = MIXED.get(k, 0), DIRECTED.get(k, 0) - if k in base or m >= v: - K.check(f"T21 floor {k} >= {v} in the mixed pass alone ({m})", - m >= v) - else: - K.check(f"T21 floor {k} >= {v}: mixed {m} + directed {d} (the " - f"calm pass adds what full interference rarely reaches)", - m + d >= v) +GAP = CM.PHASE_GAP_S +# The directed pass runs 4 x SEEDS seeds (foreign_hold_after_end is rare when +# Solar changes run calm); the mixed pass keeps 9b09158's SEEDS. +for name, fn, counts, gap, nseeds, floor in ( + ("mixed", fuzz_mixed, MIXED, GAP, SEEDS, MIXED_FLOOR), + ("directed", fuzz_directed, DIRECTED, 40.0, 4 * SEEDS, + DIRECTED_FLOOR)): + COUNT = counts + CM.PHASE_GAP_S = gap # the directed pass: many changes in 1200 s + allbad = [] + for kind in ("solar", "manual"): + for seed in range(nseeds): + allbad += fn(kind, seed) + CM.PHASE_GAP_S = GAP + for tag in TAGS[name]: + hit = [b for b in allbad if b[3].startswith(tag + " ")] + K.check(f"T21 {tag}: holds in every cycle of {nseeds} seeds x {STEPS} " + f"s, Solar and Manual, both orders, {name} pass ({hit[:2]})", + not hit) + print(f" {name} counts", dict(sorted(counts.items()))) + if STEPS >= 1200 and SEEDS >= 10: + for k, v in floor.items(): + K.check(f"T21 floor {k} >= {v} in the {name} pass alone " + f"({counts.get(k, 0)})", counts.get(k, 0) >= v) K.done()