Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/Compatibility/Aggregates
Joseph Doherty 8a0032bfdf fix(aggregates): live TimeAverage/Total leave the span before an Uncertain first value out (#821)
When the data starts inside an interval (no start bound) and its first value is
Uncertain, the frozen 1.5.378.182 AverageAggregateCalculator integrated the
uncovered span as a ramp from 0 and divided TimeAverage by the whole interval:
GetRegionsInValueSet (ignoreBadData, sloped) overwrites the BadNoData start
region's status with UncertainDataSubNormal in its one branch without a not-Bad
guard. Live AggregateFilter therefore disagreed with HistoryRead and Part 13
§5.4.3.6/§5.4.3.8 ("No Start Bound: Calculate Partial Interval"); QP: Total
188.96 vs 163.96, TimeAverage 9.448 vs 10.931. SDK 2.0.1 stock does the same.
Owner-approved 2026-11-11.

- New app-owned CoveredTimeAverageAggregateCalculator subclasses the baseline
  and recomputes only TimeAverage/Total, restoring the Bad status of a region
  that starts at a Bad point (only the start bound can). Only such an interval's
  value changes; status, timestamps, type and bits are the baseline's. Stepped
  and TreatUncertainAsBad=true never reached the defect. CompatibilityAggregate-
  Factory routes exactly those two IDs; copied SDK files untouched.
- Golden test: 0 fixture vectors change (every data set has a usable value at
  -10 s), asserted. New sweep over late-start variants of every data set (cut
  +5 s / +25 s, first non-Bad sample Uncertain, all directions/stepping/configs/
  incremental): 150 / 142 of 384 change, each pinned to a baseline-derived
  oracle (untouched baseline's value with that sample Good, its status/bits on
  the variant itself) - never our calculator. Plus a hand-computed case.
- Live-vs-HistoryRead parity (TimeWeightedAggregator) forward + reverse, no
  masks, with discrimination against the pre-#821 baseline; QualityMix now
  compares QP's TimeAverage/Total with the live oracle on both SQL backends;
  booted-runtime facts test expects 163.96 / 10.93.
- README recorded decision, ServerAggregateConfiguration remarks, oracle
  remarks, docs/Historian.md and .claude/rules/historian.md updated.

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
2026-11-11 12:30:01 -05:00
..

Preserved aggregate calculations

The SDK 2.0 upgrade keeps the application's existing historian and live AggregateFilter results (owner decision, 2026-11-10, #782). SDK 2.0.1 changes more than the default configuration: uncertain-sample selection, value-based quality, reverse data edges, partial results and interpolation differ. Changing TreatUncertainAsBad alone does not preserve the contract.

The eight upstream algorithm/factory files here originate at OPCFoundation/UA-.NETStandard tag 1.5.378.182. Original MIT license headers remain. baseline-sources.json records each original URL and SHA-256. They are application-private source, not an old SDK runtime or migration shim.

Representation adaptations:

  • Private LegacyDataValue retains mutable intermediate work values and CLR DateTime timestamps; the adapter accepts/emits immutable SDK 2 values, including status and picoseconds.
  • Nullable reference annotations describe the existing optional bounds/slices; no warning policy is disabled.
  • Immutable StatusCode methods replace the old setters. NodeId extraction and QualifiedName construction use supported typed APIs.
  • Removed TypeInfo.Cast/Variant(object, TypeInfo) operations use the SDK 2 typed conversion surface; baseline vectors verify value types, values, timestamps and all status bits.
  • Interval, quality, sample selection, queue trimming, interpolation and partial-result algorithms remain the baseline algorithms. The server creates a fresh SDK AggregateManager and registers these 37 IDs exactly once through the public API. It must not call the base registration first: replacing an existing factory also adds a duplicate diagnostic node in SDK 2.0.1. No extra capability is advertised.

The isolated scripts/validation/opcua-aggregate-baseline process pins only SDK 1.5.378.182 and generates the checked-in golden fixture. It is deliberately outside the solution and does not inherit the candidate package graph. The golden file records the baseline assembly hash, shared inputs and 6,144 outputs/cases across all 37 aggregate IDs: the original 4,144 cases plus 2,000 numeric-width, nonfinite/conversion, timestamp tie/picosecond and configuration-edge cases. Forward/reverse, stepped/sloped interpolation, uncertain-as-bad flags and incremental/partial processing are retained. The candidate tests exercise the same independent expectations both directly and through the booted runtime manager. Unset SDK 2 FILETIME timestamps map explicitly to the original CLR unset sentinel when observing results. No baseline binary is loaded by the candidate tests or production host.

Run the generator with a new output path, then compare it with tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/Fixtures/opcua-1.5.378.182-aggregates.json. It refuses to overwrite evidence. Never regenerate expectations from the compatibility implementation. Existing historian parity tests retain their application-specific documented divergences from the baseline SDK, while new compatibility tests check the complete old-calculator output.

This preserves older behavior deliberately; it does not claim that all older SDK algorithms implement the newest OPC UA interpretation. A future semantic migration needs its own consumer-impact decision and versioned tests.

Recorded semantic decisions

#809 — PercentGood / DurationGood count Good time only (owner-approved 2026-11-11)

The baseline StatusAggregateCalculator.ComputeDurationGoodBad counts Uncertain regions as Good when TreatUncertainAsBad = false (1.5.378.106 and SDK 2.0.1 count Good only). Part 13 §5.4.3.31/§5.4.3.33 (Mantis 0006166) and the HistoryRead evaluators (TimeWeightedAggregator, SqlServer + QuestDB) count Good regions only, so live AggregateFilter results disagreed with HistoryRead whenever Uncertain data was present. ScadaBridge uses neither AggregateFilter nor these aggregates.

  • The copied upstream files above are unchanged (their SHA-256s still match baseline-sources.json).
  • GoodOnlyStatusAggregateCalculator.cs (application code, not a baseline copy) subclasses the baseline StatusAggregateCalculator and replaces only the PercentGood / DurationGood computation: the same slice values, simple bounds, stepped regions, empty-slice BadNoData, timestamps, value type and Calculated bit, with a region counted as Good only when its status is Good — whatever TreatUncertainAsBad says (when true, Uncertain is Bad, so it was never Good). CompatibilityAggregateFactory.Create routes exactly those two IDs to it; the other 35 IDs, including DurationBad / PercentBad / WorstQuality, are the baseline unchanged.
  • Golden test: the 64 vectors this changes (38 PercentGood + 26 DurationGood, all with an effective TreatUncertainAsBad = false over data with Uncertain regions) are exempted exactly — the PercentGood / DurationGood vectors whose baseline output differs from the same case's baseline vector with TreatUncertainAsBad = true, where 1.5.378.182 already counted Good regions only — and each is pinned to that twin's output. The pin is therefore baseline evidence, never regenerated from the application's calculator; any further drift in any of the 6,144 cases fails. Sdk2AggregateCompatibilityTests.Issue809_* also pins a hand-computed case.
  • Live-vs-history parity: SdkAggregateParityTests.Live_AggregateFilter_PercentGood_and_DurationGood_equal_HistoryRead_over_Uncertain_samples compares the registered calculator with TimeWeightedAggregator over Good / Uncertain / Bad mixes with no masks, and checks that the data discriminates against the pre-#809 baseline.

#821 — TimeAverage / Total integrate only the covered part when there is no start bound (owner-approved 2026-11-11)

When the data starts inside an interval (no usable value before it) the baseline's interpolated start bound is BadNoData, so the region before the first value is Bad and left out — Part 13 §5.4.3.6/§5.4.3.8 "No Start Bound: Calculate Partial Interval", and what the HistoryRead evaluator (TimeWeightedAggregator, SqlServer + QuestDB) does. But when that first value is Uncertain, AggregateCalculator.GetRegionsInValueSet (ignoreBadData, sloped) overwrites the region's Bad status with UncertainDataSubNormal (the one branch without the not-Bad guard), so AverageAggregateCalculator integrated the uncovered span as a ramp from 0 and divided TimeAverage by the whole interval (QP: Total 188.96 for 163.96, TimeAverage 9.448 for 10.931). SDK 2.0.1's stock calculator does the same. ScadaBridge uses neither AggregateFilter nor these aggregates.

  • The copied upstream files above are unchanged (their SHA-256s still match baseline-sources.json).
  • CoveredTimeAverageAggregateCalculator.cs (application code) subclasses the baseline AverageAggregateCalculator and recomputes only TimeAverage / Total: the baseline's interpolated-bounds values and regions, with a region that starts at a Bad point (only the start bound can) kept Bad. Values change only for such an interval; status (Uncertain either way, except BadNoData when nothing at all is covered), timestamps, value type and Calculated / Partial bits are the baseline's. Stepped variables and TreatUncertainAsBad = true never reached the defect and are unchanged. CompatibilityAggregateFactory.Create routes exactly those two IDs to it; TimeAverage2 / Total2 and the other 31 IDs are the baseline unchanged.
  • Golden test: no fixture vector changes — every fixture data set has a usable value at −10 s, before every window, so no fixture interval lacks a start bound; all 384 TimeAverage / Total vectors stay held to their own output (Issue821FixtureExemptions = 0). The change is pinned over late-start variants of every data set (cut at +5 s and +25 s, first remaining non-Bad sample made Uncertain, every direction / stepping / configuration / incremental mode): 150 and 142 of the 384 change, each held to a baseline-derived oracle — the untouched baseline's value on the same variant with that sample Good, its status / timestamps / bits on the variant itself. Sdk2AggregateCompatibilityTests.Issue821_* also pins a hand-computed case.
  • Live-vs-history parity: SdkAggregateParityTests.Live_AggregateFilter_TimeAverage_and_Total_equal_HistoryRead_over_an_Uncertain_first_late_start compares the registered calculator with TimeWeightedAggregator, forward and reverse, with no masks, and checks that the data discriminates against the pre-#821 baseline; QualityMixConformance now includes TimeAverage / Total over QP on both SQL backends.