fix(galaxy): #859 final review — probe replay per platform session, teardown session bump

BLOCKER: the reconnect replay chose work from the registry entries' sessions,
but ONE registry entry holds every probe handle and any probe publish on the
new session (a seed in the reopen→replay gap, or the straddle retry) re-stamps
it as current — the replay then skipped the probe watcher and the old
platforms stayed bound to handle 0 (host status frozen); a publish after the
snapshot made it re-subscribe everything (double advise, leaked handle).
ReplayAsync now excludes the probe entry from its snapshot and calls the new
PerPlatformProbeWatcher.ResubscribeStaleAsync on every reconnect, which
re-subscribes exactly the platforms whose ProbeHandle.Session predates the
current session. The replay-gap path keeps ResubscribeAsync (all platforms).

Also: ClearRegistry (teardown) starts a new registry session, so a late
PurgeParked from a teardown-orphaned call cannot reach the next runtime's
parked values; the read path's unused pump local is gone (call kept).

Tests go through the driver's reopen/replay: the probe old-session-number test
is rewritten that way, plus the seed-in-the-gap scenario (only P and Q are
re-subscribed, R keeps its new-session handle, all three resolve). Both checked
red with the probe replay removed / re-subscribing all platforms.

Refs #859, #861.

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
Joseph Doherty
2026-11-11 11:07:59 -05:00
parent 133bc07da5
commit a8d3790b09
3 changed files with 136 additions and 20 deletions
@@ -949,24 +949,86 @@ public sealed class EarlyEventWindowTests
[Fact]
public async Task Probe_watcher_never_publishes_or_unsubscribes_old_session_numbers()
{
var registry = new SubscriptionRegistry();
var subscriber = new ScriptedSubscriber();
using var driver = await InitializedDriverAsync(subscriber);
var watcher = driver.ProbeWatcherForTests.ShouldNotBeNull();
var registry = driver.SubscriptionsForTests;
subscriber.Script.Enqueue((refs, _) => Task.FromResult<IReadOnlyList<SubscribeResult>>(
[.. refs.Select((r, i) => Ok(r, i + 1))])); // P → 1, Q → 2
using var watcher = new PerPlatformProbeWatcher(subscriber, new HostStatusAggregator(), registry);
await watcher.SyncPlatformsAsync(["P", "Q"], TestContext.Current.CancellationToken);
registry.StartNewSession();
await driver.InvokeReopenForTestAsync(TestContext.Current.CancellationToken); // gateway session swapped
// The new session reuses number 1 for a user tag.
registry.Register(10, [new TagBinding("User.Tag", 1)]);
subscriber.Script.Enqueue((refs, _) => Task.FromResult<IReadOnlyList<SubscribeResult>>([Ok(refs[0], 1)]));
await driver.SubscribeAsync(["User.Tag"], TimeSpan.Zero, TestContext.Current.CancellationToken);
await watcher.SyncPlatformsAsync(["P"], TestContext.Current.CancellationToken); // drops Q
await watcher.SyncPlatformsAsync(["P"], TestContext.Current.CancellationToken); // a seed in the gap drops Q
subscriber.Unsubscribed.ShouldBeEmpty("Q's handle 2 is an old-session number");
registry.ResolveSubscribers(1).ShouldBe([(10L, "User.Tag")], "P's old-session number must not be re-published");
registry.ResolveSubscribers(1).Single().FullReference.ShouldBe("User.Tag", "P's old-session number must not be re-published");
(await watcher.ResubscribeAsync(TestContext.Current.CancellationToken)).ShouldBe(1); // the replay
var fresh = watcher.CurrentBindings.Single().ItemHandle;
registry.ResolveSubscribers(fresh).Single().FullReference.ShouldBe("P.ScanState");
var replayed = new ConcurrentQueue<string>();
subscriber.Script.Enqueue((refs, _) =>
{
foreach (var r in refs) replayed.Enqueue(r);
return Task.FromResult<IReadOnlyList<SubscribeResult>>([Ok(refs[0], 7)]);
});
await driver.InvokeReplayForTestAsync(TestContext.Current.CancellationToken); // the driver's replay
replayed.ToArray().ShouldBe(["P.ScanState"]);
watcher.CurrentBindings.Single().ItemHandle.ShouldBe(7);
registry.ResolveSubscribers(7).Single().FullReference.ShouldBe("P.ScanState");
}
/// <summary>
/// Final review blocker: a probe seed in the gap between the session swap and the replay
/// re-stamps the shared probe registry entry as current. The replay must still re-subscribe the
/// platforms whose handles predate the swap (they would otherwise freeze at handle 0) — and only
/// those: the platform the seed just advised on the new session is not advised twice.
/// </summary>
[Fact]
public async Task Replay_after_a_seed_in_the_gap_resubscribes_exactly_the_stale_platforms()
{
var subscriber = new ScriptedSubscriber();
using var driver = await InitializedDriverAsync(subscriber);
var watcher = driver.ProbeWatcherForTests.ShouldNotBeNull();
var registry = driver.SubscriptionsForTests;
subscriber.Script.Enqueue((refs, _) => Task.FromResult<IReadOnlyList<SubscribeResult>>(
[.. refs.Select((r, i) => Ok(r, i + 1))])); // P → 1, Q → 2
await watcher.SyncPlatformsAsync(["P", "Q"], TestContext.Current.CancellationToken);
await driver.InvokeReopenForTestAsync(TestContext.Current.CancellationToken);
subscriber.Script.Enqueue((refs, _) => Task.FromResult<IReadOnlyList<SubscribeResult>>([Ok(refs[0], 20)]));
await watcher.SyncPlatformsAsync(["P", "Q", "R"], TestContext.Current.CancellationToken); // seed adds R on the new session
registry.ResolveSubscribers(20).Single().FullReference.ShouldBe("R.ScanState");
var replayed = new ConcurrentQueue<string>();
subscriber.Script.Enqueue((refs, _) =>
{
foreach (var r in refs) replayed.Enqueue(r);
return Task.FromResult<IReadOnlyList<SubscribeResult>>([.. refs.Select((r, i) => Ok(r, 30 + i))]);
});
await driver.InvokeReplayForTestAsync(TestContext.Current.CancellationToken);
replayed.OrderBy(r => r).ToArray().ShouldBe(["P.ScanState", "Q.ScanState"], "R was advised on the new session already");
var bindings = watcher.CurrentBindings.ToDictionary(b => b.FullReference, b => b.ItemHandle);
bindings["R.ScanState"].ShouldBe(20);
bindings["P.ScanState"].ShouldBeGreaterThanOrEqualTo(30);
bindings["Q.ScanState"].ShouldBeGreaterThanOrEqualTo(30);
foreach (var (reference, handle) in bindings)
{
registry.ResolveSubscribers(handle).Single().FullReference.ShouldBe(reference);
}
}
private static async Task<GalaxyDriver> InitializedDriverAsync(ScriptedSubscriber subscriber)
{
var driver = new GalaxyDriver(
"g", Opts(), hierarchySource: null, dataReader: null, dataWriter: null, subscriber: subscriber)
{
RecreateOverrideForTests = _ => Task.CompletedTask,
};
await driver.InitializeAsync("{}", TestContext.Current.CancellationToken);
return driver;
}
}