From e31ffc6d637c4335455693d6bb90459cb3e7e9d4 Mon Sep 17 00:00:00 2001 From: legacy-code-archaeologist <359844974+legacy-code-archaeologist@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:02:18 +0100 Subject: [PATCH] shader: invalidate watch manifests on older-mtime sidecar changes Extract read_pixel_input_description() so watch/request paths share exact .psin/.ps57in parsing and conflict detection, compare canonical pixel-input descriptions against .psdep, remove stale .psdep in batch compile_job when pixel inputs are empty, and verify stale preserved-mtime base/RTV sidecars and 0-byte .psdep retranslation in native_compile_dxil_watch_check.py. Refs #3133 AI-assisted. --- src/shader_compute_request.cpp | 33 +++++--- src/shader_compute_request.h | 2 + tests/native_compile_dxil_watch_check.py | 103 +++++++++++++++++++++++ tools/startup/compile_dxil.cpp | 48 +++++++---- 4 files changed, 158 insertions(+), 28 deletions(-) diff --git a/src/shader_compute_request.cpp b/src/shader_compute_request.cpp index 558d8fc467..dda15d074f 100644 --- a/src/shader_compute_request.cpp +++ b/src/shader_compute_request.cpp @@ -181,8 +181,30 @@ x2w::GraphicsRequest read_rtv_variant(const fs::path &marker) { request.rtv_formats = std::move(formats); return request; } +void read_pixel_inputs(const fs::path &marker, uint64_t expected, Bytes &entry, Bytes &description) { + if (fs::exists(fs::path(marker).replace_extension(".psin"))) + parse_pixel_inputs(marker, expected, entry, description); + if (fs::exists(fs::path(marker).replace_extension(".ps57in"))) { + if (!entry.empty()) + throw std::runtime_error("conflicting stream and slot-57 pixel entry records"); + entry = part(marker, ".ps57in", 12); + if (entry.size() != 12 || entry.compare(0, 4, "PSI1")) + throw std::runtime_error("invalid slot-57 pixel entry record"); + description = entry; + } +} } // namespace +std::string x2w::read_pixel_input_description(const fs::path &base_marker) { + const auto stem = base_marker.stem().string(); + if (base_marker.extension() != ".a" || stem.size() != 20 || stem.compare(0, 4, "req_")) + throw std::runtime_error("expected req_<16 hex digits>.a"); + const uint64_t expected = parse_request_stem(stem, "invalid graphics request key"); + Bytes entry, description; + read_pixel_inputs(base_marker, expected, entry, description); + return description; +} + x2w::GraphicsRequest x2w::read_graphics_request(const fs::path &marker) { if (marker.extension() == ".rtv") return read_rtv_variant(marker); auto stem = marker.stem().string(); @@ -193,16 +215,7 @@ x2w::GraphicsRequest x2w::read_graphics_request(const fs::path &marker) { part(marker, ".root", 16 << 20), {}, {}, {}, expected}; if (fs::exists(fs::path(marker).replace_extension(".ia"))) request.input_layout = part(marker, ".ia", 1 << 20); - if (fs::exists(fs::path(marker).replace_extension(".psin"))) - parse_pixel_inputs(marker, expected, request.pixel_entry, request.pixel_input_description); - if (fs::exists(fs::path(marker).replace_extension(".ps57in"))) { - if (!request.pixel_entry.empty()) - throw std::runtime_error("conflicting stream and slot-57 pixel entry records"); - request.pixel_entry = part(marker, ".ps57in", 12); - if (request.pixel_entry.size() != 12 || request.pixel_entry.compare(0, 4, "PSI1")) - throw std::runtime_error("invalid slot-57 pixel entry record"); - request.pixel_input_description = request.pixel_entry; - } + read_pixel_inputs(marker, expected, request.pixel_entry, request.pixel_input_description); const auto request_key = [&](std::string_view revision) { uint64_t key = 1469598103934665603ull; hash_bytes(key, revision.data(), revision.size()); diff --git a/src/shader_compute_request.h b/src/shader_compute_request.h index cdb8c28385..1de45e0e74 100644 --- a/src/shader_compute_request.h +++ b/src/shader_compute_request.h @@ -22,6 +22,8 @@ struct GraphicsRequest { }; // marker: req_.a, or req_.rtv for an RTV variant of a base request in the same directory. GraphicsRequest read_graphics_request(const std::filesystem::path &marker); +// Canonical pixel-input sidecar (.psin or .ps57in) content for req_.a; empty when absent, throws if invalid/conflicting. +std::string read_pixel_input_description(const std::filesystem::path &base_marker); std::string read_shader_container_bytes(const std::filesystem::path &file); ComputeContainer read_compute_container(const std::filesystem::path &file); // Verify the content key over all six immutable capture parts before exposing any bytes. diff --git a/tests/native_compile_dxil_watch_check.py b/tests/native_compile_dxil_watch_check.py index c7c9672bb4..44b4b2d43d 100644 --- a/tests/native_compile_dxil_watch_check.py +++ b/tests/native_compile_dxil_watch_check.py @@ -16,6 +16,7 @@ sys.path.insert(0, str(HERE.parent / "tools" / "startup")) import gfx2hlsl as G # noqa: E402 +from native_direct_container_check import patched_pixel, publish as publish_raw, vertex as container_vertex, words as raw_words # noqa: E402 from native_rtv_variant_check import FLOAT32, UINT16, UNORM8, put_variant # noqa: E402 from native_startup_request_cache_check import ( # noqa: E402 COMPUTE_GROUP, @@ -136,6 +137,19 @@ def main(argv: list[str]) -> int: assert not (d_lost / f"{lost_key:016x}.txt").exists() assert (d_lost / f"{lost_key:016x}.fail").is_file() + # A 0-byte .psdep next to a published manifest + valid .psin triggers retranslation and restores .psdep. + zero_psdep = d_lost / "zero_psdep" + zero_psdep.mkdir() + z_key = put_graphics_request(zero_psdep, VS_WORDS, ps_words(4), g_root, TARGET_MASKS) + assert run_once(cli, sdk, frontend, zero_psdep, jobs=1).returncode == 0 + z_psdep = zero_psdep / f"{z_key:016x}.psdep" + orig_dep = z_psdep.read_bytes() + assert orig_dep + z_psdep.write_bytes(b"") + r_zero = run_once(cli, sdk, frontend, zero_psdep, jobs=1) + assert r_zero.returncode == 0 and f"{z_key:016x}: compiled graphics" in r_zero.stdout, (r_zero.stdout, r_zero.stderr) + assert z_psdep.read_bytes() == orig_dep + # A copied slot-57 sidecar can be older than the published stream manifest. conflict_dir = work / "copied_pixel_sidecar" conflict_dir.mkdir() @@ -194,6 +208,95 @@ def main(argv: list[str]) -> int: assert not (d_rtv / f"{key:016x}.txt").exists() assert (d_rtv / f"{key:016x}.fail").is_file() + # Issue #3133: sidecar content changes or contradictory .ps57in with preserved/older mtimes must invalidate. + # Contradictory .ps57in appearing with older mtime vs newer mtime must withdraw stale .txt identically. + outcomes_ps57 = [] + for label, offset in (("older", -60.0), ("newer", 10.0)): + d_cmp = work / f"ps57_contradict_{label}" + d_cmp.mkdir() + s_key = put_graphics_request(d_cmp, VS_WORDS, ps_words(4), g_root, TARGET_MASKS) + r_pub = run_once(cli, sdk, frontend, d_cmp, jobs=1) + assert r_pub.returncode == 0 and f"{s_key:016x}: compiled graphics" in r_pub.stdout, r_pub.stdout + txt_path = d_cmp / f"{s_key:016x}.txt" + fail_path = d_cmp / f"{s_key:016x}.fail" + assert txt_path.is_file() and (d_cmp / f"{s_key:016x}.psdep").is_file() + # Unchanged-input cache-hit control before adding contradictory .ps57in. + txt_ns = txt_path.stat().st_mtime_ns + r_hit = run_once(cli, sdk, frontend, d_cmp, jobs=1) + assert r_hit.returncode == 0 and f"{s_key:016x}:" not in r_hit.stdout and txt_path.stat().st_mtime_ns == txt_ns + touch( + d_cmp / f"req_{s_key:016x}.ps57in", + b"PSI1" + struct.pack("(size) > max_bytes) + return false; + out.assign(static_cast(size), '\0'); + file.seekg(0); + return !size || static_cast(file.read(out.data(), size)); +} + static bool is_watch_request_pending(const fs::path& request_dir, const WatchRequest& req, bool skip_failed) { std::error_code ec; const auto marker_mtime = fs::last_write_time(req.marker, ec); @@ -704,37 +716,35 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq const fs::path psdep_path = fs::path(stem) += L".psdep"; std::optional psin_mtime; - bool dependency_changed = false; + bool sidecar_changed = false; if (!req.compute) { if (const auto psin_path = graphics_psin_path(req.marker)) { const auto packed_path = fs::path(*psin_path).replace_extension(L".ps57in"); const bool stream = fs::is_regular_file(*psin_path, ec); const bool packed = fs::is_regular_file(packed_path, ec); - std::ifstream dependency(psdep_path, std::ios::binary); - char previous[13]{}; - dependency.read(previous, sizeof(previous)); - const auto previous_size = dependency.gcount(); - const bool was_packed = previous_size >= 4 && std::memcmp(previous, "PSI1", 4) == 0; - if (packed) { - // A copied sidecar may carry a timestamp older than the published manifest. - std::ifstream input(packed_path, std::ios::binary); - char current[13]{}; - input.read(current, sizeof(current)); - dependency_changed = stream || !was_packed || previous_size != 12 || input.gcount() != 12 || - std::memcmp(current, previous, 12) != 0; - } else { - dependency_changed = was_packed || (stream != (previous_size != 0)); - } if (stream || packed) { const auto time = fs::last_write_time(stream ? *psin_path : packed_path, ec); if (!ec) psin_mtime = time; } + const bool has_psdep = fs::is_regular_file(psdep_path, ec); + std::string published_dep; + if (has_psdep && (!read_small_file(psdep_path, 256, published_dep) || published_dep.empty())) { + sidecar_changed = true; + } else { + try { + const std::string current_dep = + x2w::read_pixel_input_description(fs::path(*psin_path).replace_extension(L".a")); + sidecar_changed = current_dep != published_dep; + } catch (const std::exception&) { + sidecar_changed = true; + } + } } } const bool has_manifest = fs::is_regular_file(manifest_path, ec); - if (has_manifest && !dependency_changed) { + if (has_manifest && !sidecar_changed) { const auto manifest_mtime = fs::last_write_time(manifest_path, ec); if (!ec && !(psin_mtime.has_value() && *psin_mtime > manifest_mtime)) { if (req.compute) { @@ -748,7 +758,7 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq } } - const bool recheck = dependency_changed && has_manifest; + const bool recheck = sidecar_changed && has_manifest; if (skip_failed && !recheck && fs::is_regular_file(fail_path, ec)) { const auto fail_mtime = fs::last_write_time(fail_path, ec); const auto latest_input = @@ -1160,6 +1170,8 @@ int wmain(int argc, wchar_t** argv) { if (manifest) { if (!input.pixel_input_description.empty()) publish_atomic(psdep_path, input.pixel_input_description.data(), input.pixel_input_description.size()); + else + fs::remove(psdep_path, ignored); publish_atomic(manifest_path, manifest->data(), manifest->size()); } fs::remove(fs::path(stems[i]) += L".fail", ignored);