# Repair of return #1372: `arrangement-stats.py` diagnostics moved from stdout to stderr, and # the remaining stdout made LF-only on every platform. # # Base (served) : d98c6e88933902ebc53aa29a620ad1282d17d4148273cfeac684224bc28cd16b # Repaired : 3c56ea89007fa383961d47b44a23a7ee555d2051618ccd5251f0137aea4ce5ef (rev2) # rev1 (938cf2de...) moved diagnostics only and is superseded by this diff. # # rev1 (938cf2de..., stdout->stderr only) is superseded by this diff; see the repair note. # Apply from the package root: patch -p1 < arrangement-stats.patch # The row artifact (arrangement-composite.json) is unchanged by this patch: the repaired # script reproduces 3a04416d0add73883cd62d7afaf25af7f144c9bcac7fbbe2153f2c9d22f87ba9. --- a/arrangement-stats.py +++ b/arrangement-stats.py @@ -43,6 +43,17 @@ HERE = os.path.dirname(os.path.abspath(__file__)) VERSION = "1.0.0" + + +def note(*a, **kw): + """Diagnostics to STDERR. + + stdout is the artifact and must reproduce byte for byte elsewhere; per-run progress and timings + never can. This is the whole of the repair: no value, threshold, order or arithmetic changes. + A caller's `flush=True` (left in place by the minimal diff) is accepted and absorbed. + """ + kw.pop("flush", None) + print(*a, file=sys.stderr, flush=True, **kw) PREREG = "prereg-arrangement.md" spec = importlib.util.spec_from_file_location("km", os.path.join(HERE, "killer-marginal.py")) @@ -184,8 +195,8 @@ d["Mp"] = Mp out.append(d) if progress and len(out) % progress == 0: - print(" %d rows %.1fs" % (len(out), time.time() - t0), flush=True) - print(" enumerated %d rows in %.1fs" % (len(out), time.time() - t0), flush=True) + note(" %d rows %.1fs" % (len(out), time.time() - t0)) + note(" enumerated %d rows in %.1fs" % (len(out), time.time() - t0)) return out @@ -257,7 +268,7 @@ if state_path: json.dump({k: (list(v) if isinstance(v, tuple) else v) for k, v in st.items()}, open(state_path, "w", encoding="utf-8")) - print("scanned %d/%d (%.2f%%) best=%s %.1fs" + note("scanned %d/%d (%.2f%%) best=%s %.1fs" % (st["a"], L, 100.0 * st["a"] / L, st["best"], time.time() - t0), flush=True) if st["a"] < L: return None, st @@ -324,7 +335,7 @@ with open(os.path.join(HERE, out_path), "w", encoding="utf-8", newline="\n") as fh: json.dump(obj, fh, indent=1, sort_keys=True) fh.write("\n") - print("wrote " + out_path) + note("wrote " + out_path) def check_prereg(): @@ -336,7 +347,7 @@ break rec = open(p, encoding="utf-8").read().split()[0] ok = h == rec - print("prereg %s sha256 %s recorded %s %s" % (PREREG, h[:16], rec[:16], "MATCH" if ok else "MISMATCH")) + note("prereg %s sha256 %s recorded %s %s" % (PREREG, h[:16], rec[:16], "MATCH" if ok else "MISMATCH")) return h, ok @@ -350,9 +361,9 @@ got, got2 = km.kstar(B, S), km.blocked_kstar(B, S, L, 1 << 25) good = got == want == got2 ok = ok and good - print("K*(%-4d,%-18s) want %2d direct %2d windowed %2d %s" + note("K*(%-4d,%-18s) want %2d direct %2d windowed %2d %s" % (B, "{" + ",".join(map(str, S)) + "}", want, got, got2, "PASS" if good else "FAIL")) - print("gates all pass: %s" % ok) + note("gates all pass: %s" % ok) return 0 if ok else 3 @@ -362,7 +373,7 @@ L = d["modulus"] K2, st = pos_scan(P, tuple(d["U"]), L, 1 << 25) if K2 is None: - print("incomplete scan"); return 4 + note("incomplete scan"); return 4 walked = walk_window(P, K2[1], K2[0], L) w = decompose_slots(P, R, E, walked, K2[0], d["A"], L) same = all(d[k] == w[k] for k in ("window_len", "first_slot", "frag_count", "S", "frag_sum", @@ -397,7 +408,7 @@ and custody["rise_le_marginal_all"] == len(rows)) print(json.dumps(custody, indent=1)) if not custody["all_match"]: - print("REFUSING: recomputation does not reproduce #1365's recorded composite statistics.") + note("REFUSING: recomputation does not reproduce #1365's recorded composite statistics.") return 3 res = {"tool": "job2732/arrangement-stats.py", "version": VERSION, "mode": "composite", "cap": cap, "custody": custody, "rows": rows, "verdicts": verdicts(rows)} @@ -413,7 +424,7 @@ x = direct_row(r["P"], tuple(r["R"]), (r["p"],), want_K=r["K"], want_A=r["A"]) assert x["marginal"] == r["marginal"], (x, r) rows.append(x) - print("P=%-4d R=%-18s q=%-3d A=%-3d K=%-3d marg=%-3d F=%-2d S=%-2d sigma=%-5d frags=%s" + note("P=%-4d R=%-18s q=%-3d A=%-3d K=%-3d marg=%-3d F=%-2d S=%-2d sigma=%-5d frags=%s" % (r["P"], r["R"], r["p"], x["A"], x["K"], x["marginal"], x["frag_count"], x["S"], x["sigma"], x["frags"])) res = {"tool": "job2732/arrangement-stats.py", "version": VERSION, "mode": "class-r5", @@ -433,17 +444,17 @@ fresh = [r for r in src["fresh_rows_detail"] if r["nR"] == 3] A_by = {(tuple(r["R"]), r["q"]): r["A"] for r in fresh} K_rec = 10 # recorded by #1370 for K*(30030,{17,19,23,29}) - print("rows in class: %d A values: %s" % (len(A_by), sorted(A_by.values()))) + note("rows in class: %d A values: %s" % (len(A_by), sorted(A_by.values()))) win, st = pos_scan(P, U, L, window, state_path, maxsecs) if win is None: print(json.dumps({"mode": "class-p30030", "incomplete": True, "scanned": st["a"], "of": L, "state": state_path})) return 4 walked = walk_window(P, win[1], win[0], L) - print("scan complete: K=%d (recorded %d) window first=%d last=%d len=%d" + note("scan complete: K=%d (recorded %d) window first=%d last=%d len=%d" % (win[0], K_rec, win[1], win[2], walked.size)) if win[0] != K_rec: - print("REFUSING: windowed scan disagrees with the recorded K*") + note("REFUSING: windowed scan disagrees with the recorded K*") return 3 rows = [decompose_slots(P, tuple(R), (q,), walked, win[0], A_by[(R, q)], L) for (R, q) in sorted(A_by)] @@ -454,7 +465,7 @@ "rows": rows, "verdicts": verdicts(rows)} write_out(out_path, res) for r in rows: - print("R=%-18s q=%-3d A=%-3d K=%-3d marg=%-3d F=%-2d S=%-2d sigma=%-5d frags=%s" + note("R=%-18s q=%-3d A=%-3d K=%-3d marg=%-3d F=%-2d S=%-2d sigma=%-5d frags=%s" % (r["R"], r["E"][0], r["A"], r["K"], r["marginal"], r["frag_count"], r["S"], r["sigma"], r["frags"])) print(json.dumps(res["verdicts"], indent=1, sort_keys=True)) @@ -470,6 +481,13 @@ ap.add_argument("--maxsecs", type=float, default=1e9) ap.add_argument("--window", type=int, default=1 << 25) a = ap.parse_args() + # stdout is an artifact stream: write it with LF-only newlines so the SAME bytes come out + # of a Windows and a POSIX run. The artifact file below is written the same way, and a + # stream whose byte length depends on the platform cannot be compared byte for byte. + try: + sys.stdout.reconfigure(newline="\n") + except (AttributeError, ValueError): + pass if a.mode == "gates": return mode_gates() _, okp = check_prereg()