Skip to content

fix(common): coerce null/empty/whitespace observation Result to UNAVAILABLE#193

Draft
ottobolyos wants to merge 12 commits into
TrakHound:masterfrom
ottobolyos:fix/agent-empty-result-unavailable
Draft

fix(common): coerce null/empty/whitespace observation Result to UNAVAILABLE#193
ottobolyos wants to merge 12 commits into
TrakHound:masterfrom
ottobolyos:fix/agent-empty-result-unavailable

Conversation

@ottobolyos

@ottobolyos ottobolyos commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

MTConnectAgent.AddObservation accepted observations whose Result value was null, the empty string "", or whitespace-only and forwarded that value verbatim onto every wire transport (HTTP /current, /sample, MQTT JSON-cppagent, SHDR). MTConnect Part 1 §Observation Information Model — Representation — Observation Values mandates "UNAVAILABLE" as the sole valid representation of a missing or undetermined observation value:

"If an Agent cannot determine a Valid Data Value for a DataItem, the value returned for the Result for the Data Entity MUST be reported as UNAVAILABLE."

The empty string is, by definition, not a Valid Data Value (it parses as no value at all under every typed DataItem schema). This PR adds an unconditional coerce step in the canonical AddObservation(string deviceKey, IObservationInput, …) overload — null / empty / whitespace Result is rewritten to Observation.Unavailable before validation, so:

  • Ignore / Warning / Remove: publishes UNAVAILABLE rather than the empty value, satisfying the spec mandate on the wire.
  • Strict: coerces to UNAVAILABLE, the observation lands rather than being silently dropped.

The coerce skips CONDITION observations (their Level enum cannot carry the empty pathology). DATA_SET / TABLE / TIME_SERIES Representation pre-fill of Count / SampleCount runs after the coerce — semantics for those representations are unchanged.

No opt-out flag is exposed: the spec mandate is MUST, so a way to opt out of compliance would itself be non-conformant.

Behaviour change

Input Pre-fix wire payload Post-fix wire payload
Result = null "" (empty) "UNAVAILABLE"
Result = "" "" "UNAVAILABLE"
Result = " " (whitespace) " " "UNAVAILABLE"
Result = "AVAILABLE" (concrete) "AVAILABLE" "AVAILABLE" (unchanged)
Result = "" under Strict observation silently dropped lands with "UNAVAILABLE"

Tests

Unit (NUnit, tests/MTConnect.NET-Common-Tests/Agents/AddObservationEmptyResultCoerceTests.cs, 10 cases):

  • empty-string Result under every non-Strict InputValidationLevel
  • the null / "" / whitespace family under Warning
  • empty Result under Strict (lands, not silently dropped)
  • concrete-value preservation (negative — the coerce does not substitute for a Valid Data Value)

Wire-level E2E (xUnit, [Trait("Category", "E2E")]):

tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableWorkflowTests.cs — HTTP /current:

  • null / empty / spaces / tab / newline / CRLF Result → AVAILABILITY element body is UNAVAILABLE (6-case [Theory])
  • concrete value → AVAILABILITY element body is verbatim
  • concrete → empty sequence → AVAILABILITY element reflects the latest (UNAVAILABLE)
  • empty Result under Strict validation → AVAILABILITY element body is UNAVAILABLE (lands, not silently dropped; spins its own agent + server harness)

tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableSampleStreamWorkflowTests.cs — HTTP /sample stream:

  • null / empty / spaces / tab / newline / CRLF Result → latest AVAILABILITY sample is UNAVAILABLE (6-case [Theory])
  • concrete value → latest AVAILABILITY sample is verbatim
  • concrete → empty sequence → two distinct samples in stream order (concrete first, then UNAVAILABLE); proves the coerce does not silently drop the second observation

All wire surfaces fed by the agent's buffer read the same coerced payload — every transport (HTTP, MQTT relay, SHDR adapter output) inherits the fix because the coerce sits above the buffer.

Files touched

  • libraries/MTConnect.NET-Common/Agents/MTConnectAgent.cs — invoke the coerce in AddObservation; add IsEmptyResult + CoerceEmptyResultToUnavailable helpers.
  • tests/MTConnect.NET-Common-Tests/Agents/AddObservationEmptyResultCoerceTests.cs — new unit fixture.
  • tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableWorkflowTests.cs — new wire-level E2E fixture against HTTP /current.
  • tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableSampleStreamWorkflowTests.cs — new wire-level E2E fixture against HTTP /sample stream.
  • .github/workflows/dotnet.yml — drop matrix expressions from the two matrix jobs' name: fields so skipped-state status checks render cleanly (build-and-test / route-check-e2e instead of build-and-test-${{ matrix.os }}).
  • docs/reference/configuration.md — regenerated to surface the new deviceValidationLevel config key on the docs site (IAgentConfiguration + AgentConfiguration sections). Same regenerator (docs/scripts/generate-reference.sh) that drives the docs-site drift gate.

Additional scope (from cascade rebase)

Includes maintainer commit a61a3647 Added separate DeviceValidationLevel propery and enum to handle MTConnectDevices validation:

  • New enum MTConnect.Agents.DeviceValidationLevel (Ignore / Warning / Remove / Strict) — controls how the agent reacts when MTConnectDevices shape data fails validation against the standard.
  • New AgentConfiguration.DeviceValidationLevel / IAgentConfiguration.DeviceValidationLevel property (default Warning). Serialised key: deviceValidationLevel.
  • MTConnectAgent.NormalizeDevice — the three invalid-Component / -Composition / -DataItem branches now consult DeviceValidationLevel instead of InputValidationLevel, splitting device-shape validation from observation validation. Enables InputValidationLevel = Strict (spec-conformant observations) with DeviceValidationLevel = Warning (accept non-standard device shapes).

Orthogonal to the empty-Result coerce contract above; the coerce runs unconditionally in AddObservation regardless of either level, per the Part 1 MUST.

@ottobolyos ottobolyos force-pushed the fix/agent-empty-result-unavailable branch 3 times, most recently from 66db93a to 0a58cfb Compare June 11, 2026 16:10
@ottobolyos ottobolyos marked this pull request as ready for review June 11, 2026 17:35
@ottobolyos ottobolyos marked this pull request as draft June 11, 2026 17:52
@ottobolyos ottobolyos force-pushed the fix/agent-empty-result-unavailable branch from a2c41aa to a90e23b Compare June 11, 2026 17:57
@ottobolyos ottobolyos marked this pull request as ready for review June 11, 2026 18:03
@ottobolyos ottobolyos marked this pull request as draft June 11, 2026 18:10
@ottobolyos ottobolyos force-pushed the fix/agent-empty-result-unavailable branch from a90e23b to 9d1e172 Compare June 11, 2026 18:17
@ottobolyos ottobolyos marked this pull request as ready for review June 11, 2026 18:21
@ottobolyos

Copy link
Copy Markdown
Contributor Author

@PatrickRitchie, this PR fixes a Part 1 spec violation that touches every wire output: MTConnectAgent.AddObservation was forwarding null / empty / whitespace Result values verbatim, where Part 1 §Observation Information Model — Representation — Observation Values mandates UNAVAILABLE as the sole valid representation of a missing value. The fix is small and self-contained—4 files across 4 commits, with 10 unit + 17 wire-level E2E cases pinning the contract—but the bug class is broad enough (every HTTP envelope, MQTT relay, and SHDR adapter output benefits from the coerce at the buffer entry) that a review at your convenience would be appreciated.

@ottobolyos

Copy link
Copy Markdown
Contributor Author

@PatrickRitchie — heads-up: while validating PR #189's dime-connector build I caught an unrelated multi-TFM Release-pack regression introduced by an earlier warnings sweep. The minimal fix lands in PR #194 (just opened as draft); the broader warnings cleanup plus a Release-pack CI gate ships in PR #195.

@PatrickRitchie

Copy link
Copy Markdown
Contributor

Updates

I updated the PR to allow this feature to be toggled on/off based on the 'InputValidationLevel = strict' configuration parameter. This should allow any existing implementation to still work while allowing a strict adherence to the standard when required.

I also added a separate DeviceValidationLevel configuration parameter in order to allow a 'non-standard' DataItem or Component to work with the 'strict' InputValidationLevel. This is common for end users to use a DataItem with a custom Type or SubType.

Tests

I'm not sure extactly how this affects the tests but I saw where InputValidationLevel is used a few times so that may be why it was failing.

@ottobolyos ottobolyos marked this pull request as draft July 15, 2026 07:52
@ottobolyos ottobolyos force-pushed the fix/agent-empty-result-unavailable branch from a61a364 to 6ab2685 Compare July 15, 2026 07:54
MTConnectHttpServer.cs moved in the v7.0-prerelease landings on
2026-06-26 (Patrick's direct pushes after PR TrakHound#201 merged). Every
route handler's line-number reference in docs/reference/http-api.md
shifted by 4-22 lines. The committed page still pointed at pre-move
lines, so `docs/scripts/generate-reference.sh --check` failed on
every CI run since 2026-06-26 - blocking the whole Docs-site +
build-and-test + route-check-e2e job DAG downstream.

Regenerated http-api.md against current source. Line-number metadata
only; no content changes.
ottobolyos and others added 10 commits July 15, 2026 12:17
…rrors

The `Prepare generated docs` job runs `generate-api-ref.sh` after the
drift gate; that script builds `build/MTConnect.NET-SysML-Import`
which transitively references Scriban 7.2.0. NuGet detects three
known vulnerabilities in that version:

- GHSA-6q7j-xr26-3h2c (moderate, NU1902)
- GHSA-7jvp-hj45-2f2m (high, NU1903)
- GHSA-q6rr-fm2g-g5x8 (moderate, NU1902)

`<TreatWarningsAsErrors>` promotes them to errors, so the job fails
on `build/MTConnect.NET-SysML-Import.csproj`. Bumping to 7.2.2 (per
dependabot PR TrakHound#204) resolves all three advisories with no source
changes required.

Piggybacked into this drift-regen PR because the two failures share
the same failing job (`Prepare generated docs`) and land as the same
gate red across every push and every PR against master. Landing them
together turns the whole DAG green in a single merge; landing them
separately leaves the gate red across the intermediate state.

Supersedes dependabot TrakHound#204.
… decision

Commit c2041ce ("Updated Docs Site", 2026-06-26) reverted the nav
logo to a plain-text title on the grounds that "logo might be too
small to read". The `themeConfig.logo` and `themeConfig.siteTitle:
false` lines in `docs/.vitepress/config.ts` were commented out; VitePress
now renders "MTConnect.NET" as text in `.VPNavBarTitle` rather than
an <img> under the previous logo-in-nav layout.

The Landing_Page_Carries_The_House_Style_Surfaces fixture has been
red on every push to master since 2026-06-26 because it asserted the
old logo-in-nav layout that the maintainer removed. Adapting the
fixture to the current maintainer intent: assert the nav-bar title
text is "MTConnect.NET" instead of the img.logo `src`. The hero image
assertion (line 349-352) continues to pin the wordmark rendering in
the landing hero block, which the maintainer explicitly retained
(same commit: "Updated hero logo to use a larger image").
Commit c2041ce ("Updated Docs Site" 2026-06-26) swapped the hero
image from `/logo.png` (32-px favicon) to `/logo-large.png` (larger
asset) with the message "Updated hero logo to use a larger image".
The Landing_Page_Carries_The_House_Style_Surfaces fixture asserted
`Does.EndWith("/logo.png")` against the hero-image src, which was
correct for the old asset but fails for the new larger asset.

Loosen the assertion to `Does.Match(@"/logo[^/]*\.png$")` so any
`/logo*.png` variant satisfies it. Survives future asset swaps in
the same shape (e.g. `/logo-xl.png`, `/logo-dark.png`) without
another test edit. The nav-title, favicon, and social-card
assertions continue to pin the specific `/logo.png` path.
This is often used for messages and program names. SHDR should pass the value as is to the Agent and the Agent should then decide (based on validation level) whether to accept the value or not.
Although, based on the MTConnect Standard, a value should never be 'empty', an 'empty' value should be able to be accepted as many adpaters/agents don't adhere to this restriction and could cause issues with real world implementations.

An InputValidationLevel set to 'strict' should rewrite an 'empty' value as 'UNAVAILABLE' so that it strictly conforms to the standard.
…nectDevices validation. This allows a device to be validated at a different level than observations/assets
@ottobolyos ottobolyos force-pushed the fix/agent-empty-result-unavailable branch from 6ab2685 to b788148 Compare July 15, 2026 20:53
The new `DeviceValidationLevel` property + enum added on this branch adds
one config key row to `IAgentConfiguration` and `AgentConfiguration`; the
drift gate `docs/scripts/generate-reference.sh --check` reports DRIFT
until the generated `docs/reference/configuration.md` catches up. Runs
the generator to produce the current output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewing

Development

Successfully merging this pull request may close these issues.

2 participants