From 107d677d67eda8f5e4f0437e74deeb31e6e646a4 Mon Sep 17 00:00:00 2001 From: Grimkeh <218528911+Grimkeh@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:00:23 +0100 Subject: [PATCH] ab_control_flow: stage outcomes.tsv so a write failure keeps the previous report Publish the TSV report through a temp file + replace so a mid-write OSError leaves any prior complete outcomes file intact. Co-authored-by: Cursor --- tests/test_ab_control_flow.py | 12 ++++++++++ tools/ab_control_flow.py | 42 +++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/tests/test_ab_control_flow.py b/tests/test_ab_control_flow.py index 33438b6ccb..91db0663a1 100644 --- a/tests/test_ab_control_flow.py +++ b/tests/test_ab_control_flow.py @@ -605,6 +605,18 @@ def test_outcomes_tsv_contents(self): with open(path, encoding="utf-8") as f: self.assertEqual(f.read(), "shader_a\tidentical\nshader_b\tDIFFERENT\n") + def test_outcomes_write_failure_preserves_previous_report(self): + with tempfile.TemporaryDirectory() as tmp: + previous = "keep\tidentical\n" + path = os.path.join(tmp, "outcomes.tsv") + with open(path, "w", encoding="utf-8", newline="\n") as f: + f.write(previous) + with patch.object(AB, "replace_retry", side_effect=OSError(13, "Permission denied")): + with self.assertRaises(OSError): + AB.write_text_report(path, "new\tDIFFERENT\n") + with open(path, encoding="utf-8") as f: + self.assertEqual(f.read(), previous) + def test_vertex_shader_generation_assembles_and_validates_all_locations(self): with tempfile.TemporaryDirectory() as tmp: calls = [] diff --git a/tools/ab_control_flow.py b/tools/ab_control_flow.py index 10f170551a..19af4b785c 100644 --- a/tools/ab_control_flow.py +++ b/tools/ab_control_flow.py @@ -24,9 +24,15 @@ import collections import os import re +import stat import struct import subprocess import sys +import tempfile + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from file_mode import published_mode # noqa: E402 +from replace_retry import replace as replace_retry # noqa: E402 # Left out: anything that writes memory or images, LDS, and the rare instructions the harness has no setup for. # Loads are fine: with empty user data they read zeros from the start of the harness's heap, the same for both. @@ -118,12 +124,44 @@ def classify_result(translation_valid, dispatch_runs, structured_runs): return "identical" if dispatch == structured else "DIFFERENT" +def write_text_report(path, text): + """Publish a complete regular file, or write directly to a non-regular destination.""" + destination = os.path.realpath(path) + parent = os.path.dirname(destination) or "." + try: + mode = os.stat(destination).st_mode + except FileNotFoundError: + mode = None + if mode is not None and not stat.S_ISREG(mode): + with open(path, "w", encoding="utf-8", newline="\n") as f: + f.write(text) + return + if mode is not None: + # Retain the direct writer's permission check without truncating the report. + os.close(os.open(destination, os.O_WRONLY)) + + staged = tempfile.NamedTemporaryFile( + mode="w", encoding="utf-8", newline="\n", delete=False, + dir=parent, prefix=".ab_control_flow-", suffix=".tmp", + ) + try: + with staged: + staged.write(text) + if os.name != "nt": + os.chmod(staged.name, published_mode(destination)) + replace_retry(staged.name, destination) + finally: + try: + os.unlink(staged.name) + except FileNotFoundError: + pass + + def write_outcomes(out_dir, outcomes): """Write (shader name, outcome) pairs to the harness's tab-separated report.""" os.makedirs(out_dir, exist_ok=True) path = os.path.join(out_dir, "outcomes.tsv") - with open(path, "w", encoding="utf-8", newline="\n") as f: - f.write("".join(f"{name}\t{outcome}\n" for name, outcome in outcomes)) + write_text_report(path, "".join(f"{name}\t{outcome}\n" for name, outcome in outcomes)) return path