|
| 1 | +using System.Globalization; |
| 2 | + |
| 3 | +namespace OpenVisionLab.ThreeD.Core; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Stable reason codes for the source-quality gate used by product routing. |
| 7 | +/// The gate is deliberately narrower than source identity readiness: tools that |
| 8 | +/// own their own selection or typed-artifact contract remain routable when they |
| 9 | +/// do not require a complete source grid. |
| 10 | +/// </summary> |
| 11 | +public enum SourceQualityToolGateReason |
| 12 | +{ |
| 13 | + Allowed, |
| 14 | + NotApplicable, |
| 15 | + ReportUnavailable, |
| 16 | + ReportError, |
| 17 | + MissingDiagnostics, |
| 18 | + GridDiagnosticsError, |
| 19 | + NoValidSamples, |
| 20 | + SourceIdentityMismatch |
| 21 | +} |
| 22 | + |
| 23 | +public sealed record SourceQualityToolGateResult( |
| 24 | + bool IsAllowed, |
| 25 | + SourceQualityToolGateReason Reason, |
| 26 | + string Detail) |
| 27 | +{ |
| 28 | + public bool IsBlocked => !IsAllowed; |
| 29 | +} |
| 30 | + |
| 31 | +public static class SourceQualityToolGate |
| 32 | +{ |
| 33 | + private static readonly HashSet<string> GridQualityTools = new( |
| 34 | + [ |
| 35 | + "filter", |
| 36 | + "level-surface", |
| 37 | + "remove-outlier-pixels", |
| 38 | + "roi-crop", |
| 39 | + "completeness-grid" |
| 40 | + ], |
| 41 | + StringComparer.OrdinalIgnoreCase); |
| 42 | + |
| 43 | + public static bool RequiresSourceQuality(string? toolId) => |
| 44 | + toolId is not null && GridQualityTools.Contains(toolId); |
| 45 | + |
| 46 | + public static SourceQualityToolGateResult Evaluate( |
| 47 | + string? toolId, |
| 48 | + string? inputContract, |
| 49 | + SourceQualityReport? report, |
| 50 | + string? reportError = null, |
| 51 | + string? expectedSourceEntityId = null, |
| 52 | + string? expectedSourceContentSha256 = null) |
| 53 | + { |
| 54 | + if (!RequiresSourceQuality(toolId) |
| 55 | + || !IsRawSourceContract(inputContract)) |
| 56 | + { |
| 57 | + return new( |
| 58 | + true, |
| 59 | + SourceQualityToolGateReason.NotApplicable, |
| 60 | + "Source Quality gate is not required for this typed input route."); |
| 61 | + } |
| 62 | + |
| 63 | + if (report is null) |
| 64 | + { |
| 65 | + return string.IsNullOrWhiteSpace(reportError) |
| 66 | + ? new( |
| 67 | + false, |
| 68 | + SourceQualityToolGateReason.ReportUnavailable, |
| 69 | + "Source Quality is unavailable; this source route is blocked until the current report is ready.") |
| 70 | + : new( |
| 71 | + false, |
| 72 | + SourceQualityToolGateReason.ReportError, |
| 73 | + $"Source Quality failed closed: {reportError.Trim()}"); |
| 74 | + } |
| 75 | + |
| 76 | + if (!string.IsNullOrWhiteSpace(expectedSourceEntityId) |
| 77 | + && !string.Equals( |
| 78 | + expectedSourceEntityId, |
| 79 | + report.Source.EntityId, |
| 80 | + StringComparison.OrdinalIgnoreCase)) |
| 81 | + { |
| 82 | + return new( |
| 83 | + false, |
| 84 | + SourceQualityToolGateReason.SourceIdentityMismatch, |
| 85 | + $"Source Quality belongs to source '{report.Source.EntityId}', not the current source '{expectedSourceEntityId}'. Reload Source Quality before routing."); |
| 86 | + } |
| 87 | + |
| 88 | + if (!string.IsNullOrWhiteSpace(expectedSourceContentSha256) |
| 89 | + && !string.Equals( |
| 90 | + expectedSourceContentSha256, |
| 91 | + report.Source.ContentSha256, |
| 92 | + StringComparison.OrdinalIgnoreCase)) |
| 93 | + { |
| 94 | + return new( |
| 95 | + false, |
| 96 | + SourceQualityToolGateReason.SourceIdentityMismatch, |
| 97 | + "Source Quality content identity does not match the current source binding. Reload Source Quality before routing."); |
| 98 | + } |
| 99 | + |
| 100 | + if (report.GridDiagnostics is null) |
| 101 | + { |
| 102 | + return new( |
| 103 | + false, |
| 104 | + SourceQualityToolGateReason.MissingDiagnostics, |
| 105 | + "Source Quality has no current grid diagnostics; this source route is blocked until diagnostics are available."); |
| 106 | + } |
| 107 | + |
| 108 | + var failedCheck = report.GridDiagnostics.Checks.FirstOrDefault(check => |
| 109 | + check.State == SourceQualityGridDiagnosticState.Error); |
| 110 | + if (report.GridDiagnostics.State == SourceQualityGridDiagnosticState.Error |
| 111 | + || failedCheck is not null) |
| 112 | + { |
| 113 | + var detail = failedCheck is null |
| 114 | + ? "Source Quality grid diagnostics failed." |
| 115 | + : $"Source Quality grid diagnostics failed at {failedCheck.Code}: {failedCheck.Message}"; |
| 116 | + return new( |
| 117 | + false, |
| 118 | + SourceQualityToolGateReason.GridDiagnosticsError, |
| 119 | + detail); |
| 120 | + } |
| 121 | + |
| 122 | + if (report.Coverage.ValidSampleCount <= 0) |
| 123 | + { |
| 124 | + return new( |
| 125 | + false, |
| 126 | + SourceQualityToolGateReason.NoValidSamples, |
| 127 | + "Source Quality reports no valid height samples; this source route is blocked."); |
| 128 | + } |
| 129 | + |
| 130 | + return new( |
| 131 | + true, |
| 132 | + SourceQualityToolGateReason.Allowed, |
| 133 | + report.Coverage.MissingSampleCount > 0 |
| 134 | + ? "Source Quality is valid; the tool's declared missing-sample policy remains responsible for the missing mask." |
| 135 | + : "Source Quality is valid for this source route."); |
| 136 | + } |
| 137 | + |
| 138 | + private static bool IsRawSourceContract(string? inputContract) => |
| 139 | + string.Equals(inputContract, "SourceC3D / RawHeightField", StringComparison.Ordinal) |
| 140 | + || string.Equals(inputContract, "RawHeightField", StringComparison.Ordinal); |
| 141 | +} |
| 142 | + |
| 143 | +/// <summary> |
| 144 | +/// Numeric before/after evidence for one derived height-field preparation |
| 145 | +/// artifact. Outlier evidence is nullable because most preparation tools do not |
| 146 | +/// classify outliers; null is reported as not evaluated rather than fabricated as |
| 147 | +/// zero. |
| 148 | +/// </summary> |
| 149 | +public sealed record SourceQualityDelta( |
| 150 | + string SourceEntityId, |
| 151 | + string SourceContentSha256, |
| 152 | + string DerivedEntityId, |
| 153 | + string DerivedContentSha256, |
| 154 | + string SourceRootSourceSha256, |
| 155 | + string DerivedRootSourceSha256, |
| 156 | + long BeforeValidSampleCount, |
| 157 | + long AfterValidSampleCount, |
| 158 | + long BeforeMissingSampleCount, |
| 159 | + long AfterMissingSampleCount, |
| 160 | + long? DetectedOutlierCount, |
| 161 | + string OutlierEvidence) |
| 162 | +{ |
| 163 | + public long ValidSampleDelta => AfterValidSampleCount - BeforeValidSampleCount; |
| 164 | + public long MissingSampleDelta => AfterMissingSampleCount - BeforeMissingSampleCount; |
| 165 | + |
| 166 | + public bool SourceIdentityRetained => |
| 167 | + string.Equals(SourceRootSourceSha256, DerivedRootSourceSha256, StringComparison.OrdinalIgnoreCase); |
| 168 | + |
| 169 | + public string Summary => string.Create( |
| 170 | + CultureInfo.InvariantCulture, |
| 171 | + $"quality delta | valid {FormatSigned(ValidSampleDelta)} | missing {FormatSigned(MissingSampleDelta)} | outliers {FormatOutliers()}"); |
| 172 | + |
| 173 | + private string FormatOutliers() => DetectedOutlierCount is { } count |
| 174 | + ? count.ToString("N0", CultureInfo.InvariantCulture) |
| 175 | + : OutlierEvidence; |
| 176 | + |
| 177 | + private static string FormatSigned(long value) => value switch |
| 178 | + { |
| 179 | + > 0 => $"+{value.ToString("N0", CultureInfo.InvariantCulture)}", |
| 180 | + < 0 => value.ToString("N0", CultureInfo.InvariantCulture), |
| 181 | + _ => "0" |
| 182 | + }; |
| 183 | +} |
0 commit comments