From 1ea898b4bf00318d73776ba84631c8b69e812dfc Mon Sep 17 00:00:00 2001 From: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:53:00 -0300 Subject: [PATCH 1/3] app: keep inspection results tied to the latest selection (#251) InspectionModel.inspect started a detached read per call and published status, report and busy whenever any of them finished. A slow first inspection that completed after a fast second one replaced the newer report and status, and could clear busy while the newer read was still running. - Each inspect call takes a request number; a completion is applied only while it is still the latest, so stale success and stale failure are discarded. Picker, drag and drop and Finder Open With all go through inspect and share this. - A new selection clears the previous report, and a failed inspection of the latest file is shown in red above its report. - The read is unchanged: size checked before reading, balanced security-scoped access, nothing executed. The reader is injectable only for the smoke check. - --smoke-inspection-order drives the model with an injected slow and fast reader: slow then fast with either outcome, and busy held for the newer request while an older read finishes. scripts/build-app.sh runs it after --smoke. All four checks fail without the change. Ported from XenoraOSS/Xenora#423. Signed-off-by: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> --- CHANGELOG.md | 1 + app/Sources/Xenora/main.swift | 73 ++++++++++++++++++++++++++++++++++- scripts/build-app.sh | 1 + 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78da0a4..aaff069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Fixed +- Fixed the app's executable inspector so a late result from an earlier selection can no longer replace a newer one (#251). Each selection from the file picker, drag and drop or Finder Open With now takes a request number, and only the latest request may publish its report and status or clear the busy state. The previous report is cleared when a new file is chosen, and a failed inspection of the latest file is shown in red above its report. The read itself is unchanged: size checked before reading, balanced security-scoped access, nothing executed. `BetterXen --smoke-inspection-order`, run by `scripts/build-app.sh`, drives the model with an injected slow and fast reader and checks a stale success, a stale failure and a busy state held for the newer request; all four checks fail without the change. Checked locally on Apple Silicon; not yet independently reviewed. - Restored the DBT benchmark report-test fixture after the cache-invalidation counter was added. Its existing percentile, JSON and comparison tests now run in ordinary workspace CI instead of requiring a separate benchmark test-harness command. - DBT cache hits now compare an executable-generation token instead of rereading every instruction byte. Address-space or executable-memory changes trigger byte and execute-permission revalidation before a translation can be reused; changed code is retranslated. Ordinary data writes retain the fast path. Tests cover cloned spaces, loader bypass writes, split writes, remapping and bounded cache exhaustion. Same-dispatch self-modification and discarded-code reclamation remain unsupported. - Gate A2 checks the cross-compiler driver before invoking it, then checks its helpers before fixture compilation. CI also verifies the driver architecture before requesting its version. diff --git a/app/Sources/Xenora/main.swift b/app/Sources/Xenora/main.swift index 9ab6667..ddba3d4 100644 --- a/app/Sources/Xenora/main.swift +++ b/app/Sources/Xenora/main.swift @@ -640,11 +640,31 @@ final class InspectionModel: ObservableObject { @Published var busy = false @Published var importing = false + /// Reads and reports one file off the main thread. Replaceable only for the + /// `--smoke-inspection-order` check. + private let reader: @Sendable (URL) -> (Int32, String) + /// Number of the latest `inspect` call. Only that call may publish a result. + private var latestRequest: UInt64 = 0 + + init(reader: @escaping @Sendable (URL) -> (Int32, String) = InspectionModel.run) { + self.reader = reader + } + + var failed: Bool { !busy && fileName != nil && status != Int32(XENORA_OK) } + func inspect(_ url: URL) { + latestRequest &+= 1 + let request = latestRequest fileName = url.lastPathComponent + report = "" + status = Int32(XENORA_OK) busy = true + let reader = reader Task { - let (newStatus, newReport) = await Task.detached { InspectionModel.run(url) }.value + let (newStatus, newReport) = await Task.detached { reader(url) }.value + // A newer selection owns the published state, including `busy`; + // an older read that finishes late is discarded. + guard request == latestRequest else { return } status = newStatus report = newReport busy = false @@ -696,6 +716,10 @@ struct InspectorView: View { } if let name = model.fileName { LabeledContent("File", value: model.busy ? "\(name) (inspecting…)" : name) + if model.failed { + Text("Inspection of \(name) failed (status \(model.status)).") + .foregroundStyle(.red) + } ScrollView { Text(model.report) .font(.system(.caption, design: .monospaced)) @@ -1009,6 +1033,53 @@ if CommandLine.arguments.contains("--smoke-frame") { exit(frame.status == XENORA_OK ? 0 : 1) } +// `--smoke-inspection-order` checks that only the latest selection publishes an +// inspection result. Files are never read: an injected reader answers after a delay +// named by the file. It needs no GPU. +if CommandLine.arguments.contains("--smoke-inspection-order") { + let reader: @Sendable (URL) -> (Int32, String) = { url in + // "--" + let parts = url.lastPathComponent.split(separator: "-") + Thread.sleep(forTimeInterval: Double(parts[0])! / 1000) + let status = parts[1] == "ok" ? Int32(XENORA_OK) : Int32(XENORA_INVALID_ARGUMENT) + return (status, "report \(url.lastPathComponent)") + } + func file(_ name: String) -> URL { URL(fileURLWithPath: "/nonexistent/\(name)") } + func pause(_ ms: UInt64) async { try? await Task.sleep(nanoseconds: ms * 1_000_000) } + + Task { @MainActor in + var failures: [String] = [] + @MainActor func expect(_ model: InspectionModel, _ name: String, busy: Bool, report: String, ok: Bool, _ label: String) { + let actual = "file=\(model.fileName ?? "-") busy=\(model.busy) status=\(model.status) report=\(model.report)" + if model.fileName != name || model.busy != busy || model.report != report + || (model.status == Int32(XENORA_OK)) != ok { + failures.append("\(label): \(actual)") + } + } + // A slow first read finishing after a fast second one, with either outcome. + for (first, second, ok) in [("400-ok-a", "0-ok-b", true), ("400-fail-a", "0-ok-b", true), + ("400-ok-a", "0-fail-b", false)] { + let model = InspectionModel(reader: reader) + model.inspect(file(first)) + model.inspect(file(second)) + await pause(700) + expect(model, second, busy: false, report: "report \(second)", ok: ok, "\(first) then \(second)") + } + // An older read that finishes first must not clear the newer request's busy state. + let model = InspectionModel(reader: reader) + model.inspect(file("100-ok-a")) + model.inspect(file("500-fail-b")) + await pause(300) + expect(model, "500-fail-b", busy: true, report: "", ok: true, "while the newer read runs") + await pause(500) + expect(model, "500-fail-b", busy: false, report: "report 500-fail-b", ok: false, "after the newer read") + for failure in failures { print("smoke-inspection-order: FAILED \(failure)") } + print("smoke-inspection-order: \(failures.isEmpty ? "ok" : "\(failures.count) failed")") + exit(failures.isEmpty ? 0 : 1) + } + dispatchMain() +} + // `--smoke` prints the probe and exits without opening a window, for scripts and CI. if CommandLine.arguments.contains("--smoke") { let probe = RuntimeProbe.run() diff --git a/scripts/build-app.sh b/scripts/build-app.sh index e5e16a7..03e58e3 100755 --- a/scripts/build-app.sh +++ b/scripts/build-app.sh @@ -72,4 +72,5 @@ for dependency in $dependencies; do esac done "$binary" --smoke +"$binary" --smoke-inspection-order echo "build-app: built $bundle" From ae804f0530cb933845d6ed5cb915bcaf2b78d277 Mon Sep 17 00:00:00 2001 From: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:13:23 -0300 Subject: [PATCH 2/3] app: gate the inspection-order smoke check instead of sleeping (#251) Review follow-up. The check used fixed 300/700 ms pauses, so on a loaded host a late stale read could arrive after an assertion, or the newer read could finish before the busy-state check. The injected reader now blocks until the check releases that file, and the check waits on InspectionModel.finishedReads, which counts every finished read whether it was published or discarded. A five-second deadline bounds the wait without deciding the order. Signed-off-by: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> --- CHANGELOG.md | 2 +- app/Sources/Xenora/main.swift | 88 ++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaff069..3725e7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Fixed -- Fixed the app's executable inspector so a late result from an earlier selection can no longer replace a newer one (#251). Each selection from the file picker, drag and drop or Finder Open With now takes a request number, and only the latest request may publish its report and status or clear the busy state. The previous report is cleared when a new file is chosen, and a failed inspection of the latest file is shown in red above its report. The read itself is unchanged: size checked before reading, balanced security-scoped access, nothing executed. `BetterXen --smoke-inspection-order`, run by `scripts/build-app.sh`, drives the model with an injected slow and fast reader and checks a stale success, a stale failure and a busy state held for the newer request; all four checks fail without the change. Checked locally on Apple Silicon; not yet independently reviewed. +- Fixed the app's executable inspector so a late result from an earlier selection can no longer replace a newer one (#251). Each selection from the file picker, drag and drop or Finder Open With now takes a request number, and only the latest request may publish its report and status or clear the busy state. The previous report is cleared when a new file is chosen, and a failed inspection of the latest file is shown in red above its report. The read itself is unchanged: size checked before reading, balanced security-scoped access, nothing executed. `BetterXen --smoke-inspection-order`, run by `scripts/build-app.sh`, drives the model with an injected reader that blocks until the check releases each file, so the check (not timing) decides which read finishes first; it checks a stale success, a stale failure and a busy state held for the newer request, and all four checks fail without the change. Checked locally on Apple Silicon; not yet independently reviewed. - Restored the DBT benchmark report-test fixture after the cache-invalidation counter was added. Its existing percentile, JSON and comparison tests now run in ordinary workspace CI instead of requiring a separate benchmark test-harness command. - DBT cache hits now compare an executable-generation token instead of rereading every instruction byte. Address-space or executable-memory changes trigger byte and execute-permission revalidation before a translation can be reused; changed code is retranslated. Ordinary data writes retain the fast path. Tests cover cloned spaces, loader bypass writes, split writes, remapping and bounded cache exhaustion. Same-dispatch self-modification and discarded-code reclamation remain unsupported. - Gate A2 checks the cross-compiler driver before invoking it, then checks its helpers before fixture compilation. CI also verifies the driver architecture before requesting its version. diff --git a/app/Sources/Xenora/main.swift b/app/Sources/Xenora/main.swift index ddba3d4..83bc36c 100644 --- a/app/Sources/Xenora/main.swift +++ b/app/Sources/Xenora/main.swift @@ -645,6 +645,9 @@ final class InspectionModel: ObservableObject { private let reader: @Sendable (URL) -> (Int32, String) /// Number of the latest `inspect` call. Only that call may publish a result. private var latestRequest: UInt64 = 0 + /// Reads that have finished, whether their result was published or discarded. + /// `--smoke-inspection-order` waits on it instead of on a fixed delay. + private(set) var finishedReads: UInt64 = 0 init(reader: @escaping @Sendable (URL) -> (Int32, String) = InspectionModel.run) { self.reader = reader @@ -662,6 +665,7 @@ final class InspectionModel: ObservableObject { let reader = reader Task { let (newStatus, newReport) = await Task.detached { reader(url) }.value + finishedReads &+= 1 // A newer selection owns the published state, including `busy`; // an older read that finishes late is discarded. guard request == latestRequest else { return } @@ -1034,21 +1038,49 @@ if CommandLine.arguments.contains("--smoke-frame") { } // `--smoke-inspection-order` checks that only the latest selection publishes an -// inspection result. Files are never read: an injected reader answers after a delay -// named by the file. It needs no GPU. +// inspection result. Files are never read: an injected reader blocks until the check +// releases that file, so the check, not timing, decides which read finishes first. +// It needs no GPU. if CommandLine.arguments.contains("--smoke-inspection-order") { + final class ReadGates: @unchecked Sendable { + private let lock = NSLock() + private var gates: [String: DispatchSemaphore] = [:] + func gate(_ name: String) -> DispatchSemaphore { + lock.lock() + defer { lock.unlock() } + if let gate = gates[name] { return gate } + let gate = DispatchSemaphore(value: 0) + gates[name] = gate + return gate + } + func release(_ name: String) { gate(name).signal() } + } + let gates = ReadGates() + // "--"; a gate that is never released times out + // instead of hanging, and the check then fails on its own deadline. let reader: @Sendable (URL) -> (Int32, String) = { url in - // "--" - let parts = url.lastPathComponent.split(separator: "-") - Thread.sleep(forTimeInterval: Double(parts[0])! / 1000) - let status = parts[1] == "ok" ? Int32(XENORA_OK) : Int32(XENORA_INVALID_ARGUMENT) - return (status, "report \(url.lastPathComponent)") + let name = url.lastPathComponent + _ = gates.gate(name).wait(timeout: .now() + 10) + let status = name.hasSuffix("-ok") ? Int32(XENORA_OK) : Int32(XENORA_INVALID_ARGUMENT) + return (status, "report \(name)") } func file(_ name: String) -> URL { URL(fileURLWithPath: "/nonexistent/\(name)") } - func pause(_ ms: UInt64) async { try? await Task.sleep(nanoseconds: ms * 1_000_000) } Task { @MainActor in var failures: [String] = [] + // Waits for the model to finish `count` reads. The deadline only bounds the + // check; the order of completions is set by releasing gates. + @MainActor func settle(_ model: InspectionModel, _ count: UInt64, _ label: String) async -> Bool { + let deadline = Date().addingTimeInterval(5) + while model.finishedReads < count { + if Date() > deadline { + failures.append("\(label): read \(count) did not finish") + return false + } + try? await Task.sleep(nanoseconds: 2_000_000) + } + return true + } @MainActor func expect(_ model: InspectionModel, _ name: String, busy: Bool, report: String, ok: Bool, _ label: String) { let actual = "file=\(model.fileName ?? "-") busy=\(model.busy) status=\(model.status) report=\(model.report)" if model.fileName != name || model.busy != busy || model.report != report @@ -1056,23 +1088,35 @@ if CommandLine.arguments.contains("--smoke-inspection-order") { failures.append("\(label): \(actual)") } } - // A slow first read finishing after a fast second one, with either outcome. - for (first, second, ok) in [("400-ok-a", "0-ok-b", true), ("400-fail-a", "0-ok-b", true), - ("400-ok-a", "0-fail-b", false)] { + // The newer read finishes first; the older one finishes afterwards and must + // change nothing, whatever its outcome. + for (scenario, first, second, ok) in [("s1", "a-ok", "b-ok", true), ("s2", "a-fail", "b-ok", true), + ("s3", "a-ok", "b-fail", false)] { + let (older, newer) = ("\(scenario)-\(first)", "\(scenario)-\(second)") let model = InspectionModel(reader: reader) - model.inspect(file(first)) - model.inspect(file(second)) - await pause(700) - expect(model, second, busy: false, report: "report \(second)", ok: ok, "\(first) then \(second)") + model.inspect(file(older)) + model.inspect(file(newer)) + gates.release(newer) + if await settle(model, 1, "\(older) then \(newer)") { + expect(model, newer, busy: false, report: "report \(newer)", ok: ok, "\(newer) finished") + } + gates.release(older) + if await settle(model, 2, "\(older) then \(newer)") { + expect(model, newer, busy: false, report: "report \(newer)", ok: ok, "stale \(older) finished") + } } - // An older read that finishes first must not clear the newer request's busy state. + // The older read finishes first and must not clear the newer request's busy state. let model = InspectionModel(reader: reader) - model.inspect(file("100-ok-a")) - model.inspect(file("500-fail-b")) - await pause(300) - expect(model, "500-fail-b", busy: true, report: "", ok: true, "while the newer read runs") - await pause(500) - expect(model, "500-fail-b", busy: false, report: "report 500-fail-b", ok: false, "after the newer read") + model.inspect(file("s4-a-ok")) + model.inspect(file("s4-b-fail")) + gates.release("s4-a-ok") + if await settle(model, 1, "s4") { + expect(model, "s4-b-fail", busy: true, report: "", ok: true, "stale s4-a-ok finished first") + } + gates.release("s4-b-fail") + if await settle(model, 2, "s4") { + expect(model, "s4-b-fail", busy: false, report: "report s4-b-fail", ok: false, "s4-b-fail finished") + } for failure in failures { print("smoke-inspection-order: FAILED \(failure)") } print("smoke-inspection-order: \(failures.isEmpty ? "ok" : "\(failures.count) failed")") exit(failures.isEmpty ? 0 : 1) From 873652d6855fd30f1a24d11df1cc3c2aba15b25e Mon Sep 17 00:00:00 2001 From: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:38:12 -0300 Subject: [PATCH 3/3] app: run inspection reads on a Dispatch queue, not a concurrency worker (#251) Review follow-up. The reader blocks on file I/O, and in the --smoke-inspection-order check on its gate semaphore. Inside Task.detached that blocked a Swift concurrency worker; with a single-worker cooperative pool the released newer read could not start while the older one waited, and the check timed out. The read now runs on DispatchQueue.global(qos: .userInitiated) and is bridged back with withCheckedContinuation. The request-number guard is unchanged. Signed-off-by: maridogolpeador <354308468+maridogolpeador@users.noreply.github.com> --- app/Sources/Xenora/main.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Sources/Xenora/main.swift b/app/Sources/Xenora/main.swift index 83bc36c..e720d71 100644 --- a/app/Sources/Xenora/main.swift +++ b/app/Sources/Xenora/main.swift @@ -664,7 +664,14 @@ final class InspectionModel: ObservableObject { busy = true let reader = reader Task { - let (newStatus, newReport) = await Task.detached { reader(url) }.value + // The read blocks on file I/O (and, in `--smoke-inspection-order`, on + // its gate), so it runs on a Dispatch queue instead of occupying a + // Swift concurrency worker that other tasks may need. + let (newStatus, newReport) = await withCheckedContinuation { continuation in + DispatchQueue.global(qos: .userInitiated).async { + continuation.resume(returning: reader(url)) + } + } finishedReads &+= 1 // A newer selection owns the published state, including `busy`; // an older read that finishes late is discarded.