fix(mqtt): prune LastValueCache to the authored set on Register (R06:M-2)

Both ingest paths now drop cache entries whose RawPath is no longer authored when a
deploy re-registers, so a removed tag's value and every non-authored subscribe
reference's BadNodeIdUnknown marker no longer accumulate for the driver's lifetime.
Still-authored tags keep their observed values.

Refs #744
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 15:28:33 -04:00
parent 43f45bb978
commit 57ffbf79e5
5 changed files with 83 additions and 0 deletions
@@ -664,6 +664,33 @@ public sealed class MqttSubscriptionManagerTests
.ShouldBe(["f/oven/press", OvenTopic]);
}
/// <summary>
/// <b>R06:M-2.</b> Register prunes the cache to the authored set — an unauthored reference's
/// BadNodeIdUnknown marker and a dropped tag's value go. Control: a still-authored tag keeps its
/// value across the re-register (a redeploy must not blank observed values).
/// </summary>
[Fact]
public async Task Register_PrunesCacheEntriesForReferencesNoLongerAuthored()
{
var mgr = NewManager(new FakeTransport());
mgr.Register(
[
Tag(OvenTempPath, $$"""{"topic":"{{OvenTopic}}","payloadFormat":"Scalar","dataType":"Float64"}"""),
Tag(OvenPressPath, """{"topic":"f/oven/press","payloadFormat":"Scalar","dataType":"Float64"}"""),
]);
await mgr.SubscribeAsync([OvenTempPath, OvenPressPath, "Plant/Mqtt/broker1/Nope"], TimeSpan.Zero, TestContext.Current.CancellationToken);
mgr.HandleMessage(OvenTopic, "1"u8.ToArray(), retained: false);
mgr.HandleMessage("f/oven/press", "2"u8.ToArray(), retained: false);
mgr.Values.Read("Plant/Mqtt/broker1/Nope").StatusCode.ShouldBe(BadNodeIdUnknown);
mgr.Register([Tag(OvenTempPath, $$"""{"topic":"{{OvenTopic}}","payloadFormat":"Scalar","dataType":"Float64"}""")]);
mgr.Values.Read(OvenTempPath).Value.ShouldBe(1.0);
mgr.Values.Read(OvenPressPath).StatusCode.ShouldBe(0x80320000u);
mgr.Values.Read("Plant/Mqtt/broker1/Nope").StatusCode.ShouldBe(0x80320000u);
mgr.Values.Count.ShouldBe(1);
}
/// <summary>
/// <b>R06:M-11.</b> A subscribe issued before any transport is attached reaches no broker, so it must
/// not be recorded as established. It used to be — "subscribed, receives nothing" — and the first
@@ -852,6 +852,28 @@ public sealed class SparkplugIngestorTests
ing.TryResolve(TempPath, out _).ShouldBeFalse();
}
/// <summary>
/// <b>R06:M-2.</b> Register prunes the shared cache to the authored set: an unauthored reference's
/// BadNodeIdUnknown marker and a dropped tag's value go, so the cache cannot grow with every RawPath
/// and stray reference ever seen. Control: a still-authored tag's value survives the re-register.
/// </summary>
[Fact]
public async Task Register_PrunesCacheEntriesForReferencesNoLongerAuthored()
{
var ing = NewIngestor();
await ing.SubscribeAsync(["Plant/Mqtt/spb/NotAThing"], TimeSpan.Zero, TestContext.Current.CancellationToken);
var kept = new DataValueSnapshot(1.0f, 0u, null, DateTime.UtcNow);
ing.Values.Update(TempPath, kept);
ing.Values.Update(PressPath, kept);
ing.Register([Tag(TempPath, Blob(Group, Node, Device, "Temperature", "Float32"))]);
ing.Values.Read(TempPath).ShouldBe(kept);
ing.Values.Read(PressPath).StatusCode.ShouldBe(0x80320000u); // dropped tag: no stale value
ing.Values.Read("Plant/Mqtt/spb/NotAThing").StatusCode.ShouldBe(0x80320000u);
ing.Values.Count.ShouldBe(1);
}
[Fact]
public async Task SubscribeAsync_UnauthoredReference_ReportsBadNodeIdUnknown()
{