fix(galaxy): an ack does not supersede a re-read entry; a stream reopen discards the re-read (#925 review r1)

- Only full-state live messages (snapshot entry, Raise, Retrigger, Clear) supersede a
  QueryActiveAlarms entry; an Acknowledge merges (Active => ActiveAcked) instead of
  skipping it and then signalling a cold condition known.
- Stream epoch bumped before StreamOpened; a changed epoch at replay drops the replay
  and the signal (an alarm cleared in the gap is absent, never touched); no re-read
  before the current stream's sentinel.
- Lows: dispose race returns false; teardown token on the RPC and the retry wait;
  an unexpected failure keeps a queued follow-up.
This commit is contained in:
Joseph Doherty
2026-11-28 09:34:24 -05:00
parent b4c27bd5b8
commit 14ca1b7168
6 changed files with 181 additions and 30 deletions
@@ -89,6 +89,71 @@ public sealed class GalaxyAlarmSnapshotRefreshTests
]);
}
/// <summary>
/// Review r1 #1: an Acknowledge carries only the ack dimension, so it does not supersede the re-read's view of
/// the alarm's ACTIVE state. Tank2 is Active+unacked when the query reads it; an operator acks it before the
/// replay ⇒ the replay must project Active+acked (not skip it and then signal a cold, inactive condition known).
/// </summary>
[Fact]
public async Task An_acknowledge_during_the_reread_merges_into_its_entry_instead_of_superseding_it()
{
await using var rig = new Rig();
using var driver = NewDriver(rig.Feed);
var trace = Record(driver);
var h1 = await driver.SubscribeAlarmsAsync([Tank1], Ct);
var s = await rig.NextStreamAsync();
await s.WriteAsync(new AlarmFeedMessage { SnapshotComplete = true }, Ct);
await TestWait.UntilAsync(() => Count(trace) == 1, "the first sentinel's signal", () => Dump(trace));
await driver.UnsubscribeAlarmsAsync(h1, Ct);
await driver.SubscribeAlarmsAsync([Tank1, Tank2], Ct);
var q = await rig.NextQueryAsync();
await s.WriteAsync(Transition(Tank2, AlarmTransitionKind.Acknowledge), Ct);
await TestWait.UntilAsync(() => Count(trace) == 2, "the live acknowledge", () => Dump(trace));
q.Complete(Entry(Tank2, AlarmConditionState.Active));
await TestWait.UntilAsync(() => Count(trace) == 4, "the merged entry then the signal", () => Dump(trace));
Snapshot(trace).ShouldBe([
"signal:" + Tank1,
"event:" + Tank2 + ":Acknowledge:live",
"event:" + Tank2 + ":ActiveAcknowledged:restatement",
$"signal:{Tank1},{Tank2}",
]);
}
/// <summary>
/// Review r1 #2: the stream reopens while the query is in flight. An alarm that cleared in the gap is simply
/// absent from the reopened stream's snapshot (never "touched"), so the query's stale Active entry must NOT be
/// replayed, and the re-read must not signal — the reopened stream's own sentinel covers the subscription.
/// </summary>
[Fact]
public async Task A_stream_reopen_during_the_reread_drops_its_replay_and_its_signal()
{
await using var rig = new Rig();
using var driver = NewDriver(rig.Feed);
var trace = Record(driver);
var h1 = await driver.SubscribeAlarmsAsync([Tank1], Ct);
var s = await rig.NextStreamAsync();
await s.WriteAsync(new AlarmFeedMessage { SnapshotComplete = true }, Ct);
await TestWait.UntilAsync(() => Count(trace) == 1, "the first sentinel's signal", () => Dump(trace));
await driver.UnsubscribeAlarmsAsync(h1, Ct);
await driver.SubscribeAlarmsAsync([Tank1, Tank2], Ct);
var q = await rig.NextQueryAsync();
s.TryComplete(new InvalidOperationException("gateway stream fault"));
var s2 = await rig.NextStreamAsync();
await s2.WriteAsync(new AlarmFeedMessage { SnapshotComplete = true }, Ct); // Tank2 cleared in the gap: absent
await TestWait.UntilAsync(() => Count(trace) == 2, "the reopened stream's signal", () => Dump(trace));
q.Complete(Entry(Tank2, AlarmConditionState.Active)); // stale: read before the clear
await Task.Delay(200, Ct);
Snapshot(trace).ShouldBe(["signal:" + Tank1, $"signal:{Tank1},{Tank2}"],
"no phantom Active from the stale re-read, and no second signal");
rig.QueryCount.ShouldBe(1, "positive control: the re-read really ran");
}
/// <summary>F4: a subscription changed during the snapshot phase is re-read at the sentinel instead of waiting for a reopen.</summary>
[Fact]
public async Task A_sentinel_under_an_earlier_subscription_triggers_a_reread_that_signals()