fix(drivers): mid-connect ApplyDelta adopts config in Calculation, refuses it in Galaxy (#664)

AdoptConfigDuringInit re-runs InitializeAsync(newConfig) and acks success, but
Calculation and Galaxy ignored the JSON there: Calculation kept its old tag
set, and Galaxy connected on its old endpoint. Calculation now rebuilds its tag
table from a config with a body (sharing AdoptConfig with ReinitializeAsync and
stopping any running timer scheduler first). Galaxy, which cannot hot-swap its
options, refuses a non-equivalent one the way ReinitializeAsync does. The actor
now sends DeltaApplied once the adopted init succeeds, so a dependency
consumer's mux interest is re-registered. Unreachable from deploys since #516.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-09 11:05:58 -04:00
parent 1355049f50
commit 97fcf624ae
8 changed files with 234 additions and 52 deletions
@@ -413,6 +413,34 @@ public sealed class CalculationDriverTests
report.SkippedCount.ShouldBe(1, "the reinit payload's tag has no scriptId, so it is no longer a calc tag");
}
/// <summary>
/// Gitea #664 — <c>InitializeAsync</c> re-runs on a LIVE driver with a new config when an ApplyDelta lands
/// mid-connect (<c>DriverInstanceActor.AdoptConfigDuringInit</c>). It used to ignore the JSON, so the
/// driver kept its constructor tag set while the deploy reported the change applied. A config with a body
/// now rebuilds the tag table (and the dependency-ref set the host re-registers from).
/// </summary>
[Fact]
public async Task InitializeAsync_on_a_live_driver_adopts_a_config_with_a_body()
{
var (driver, published, _) = Build(CalcTag("calc/e/a", "return (double)ctx.GetTag(\"src/a\").Value * 2.0;"));
await driver.InitializeAsync("{}", TestContext.Current.CancellationToken);
driver.DependencyRefs.ShouldBe(new[] { "src/a" });
var adopted = System.Text.Json.JsonSerializer.Serialize(new
{
rawTags = new[] { CalcTag("calc/e/b", "return (double)ctx.GetTag(\"src/b\").Value + 1.0;") },
});
await driver.InitializeAsync(adopted, TestContext.Current.CancellationToken);
driver.DependencyRefs.ShouldBe(new[] { "src/b" });
driver.OnDependencyValue("src/b", 4.0, StatusGood, DateTime.UtcNow);
await Settle(driver);
published.ShouldContain(e => e.FullReference == "calc/e/b" && Equals(e.Snapshot.Value, 5.0));
published.ShouldNotContain(e => e.FullReference == "calc/e/a");
await driver.ShutdownAsync(TestContext.Current.CancellationToken);
}
/// <summary>Minimal capturing <see cref="ILogger{T}"/> so a test can assert on what the driver's own
/// logger received (mirrors the equivalent helper in CalculationEvaluatorTests).</summary>
private sealed class CapturingLogger : ILogger<CalculationDriver>
@@ -168,7 +168,9 @@ public sealed class GalaxyDriverFactoryTests
dataWriter: null, subscriber: new NoopSubscriber());
driver.GetHealth().State.ShouldBe(DriverState.Unknown);
await driver.InitializeAsync(MinimalConfig, CancellationToken.None);
// #664: InitializeAsync now refuses a config that differs from the live options, so this uses the
// "{}" lifecycle shape (MinimalConfig's ApiKeySecretRef differs from BuildOptions()).
await driver.InitializeAsync("{}", CancellationToken.None);
driver.GetHealth().State.ShouldBe(DriverState.Healthy);
driver.GetHealth().LastSuccessfulRead.ShouldNotBeNull();
@@ -206,6 +208,38 @@ public sealed class GalaxyDriverFactoryTests
driver.GetHealth().LastSuccessfulRead!.Value.ShouldBeGreaterThan(firstStamp);
}
/// <summary>
/// Gitea #664 — <c>InitializeAsync</c> re-runs on a live driver with a NEW config when an ApplyDelta lands
/// mid-connect. Galaxy cannot hot-swap its options, and it used to ignore the JSON — connecting on the OLD
/// endpoint while the deploy reported the change applied. A non-equivalent config is now refused, exactly
/// as <c>ReinitializeAsync</c> refuses it; an equivalent one still initialises.
/// </summary>
[Fact]
public async Task InitializeAsync_refuses_a_non_equivalent_config_and_accepts_an_equivalent_one()
{
const string moved = """
{
"Gateway": { "Endpoint": "https://elsewhere.test:5001", "ApiKeySecretRef": "key" },
"MxAccess": { "ClientName": "OtOpcUa-A" }
}
""";
const string same = """
{
"Gateway": { "Endpoint": "https://mxgw.test:5001", "ApiKeySecretRef": "key" },
"MxAccess": { "ClientName": "OtOpcUa-A" }
}
""";
using var driver = new GalaxyDriver(
"galaxy-x", BuildOptions(), hierarchySource: null, dataReader: null,
dataWriter: null, subscriber: new NoopSubscriber());
await Should.ThrowAsync<NotSupportedException>(() => driver.InitializeAsync(moved, CancellationToken.None));
driver.GetHealth().State.ShouldBe(DriverState.Unknown, "a refused init must not report Healthy");
await driver.InitializeAsync(same, CancellationToken.None);
driver.GetHealth().State.ShouldBe(DriverState.Healthy);
}
private static GalaxyDriverOptions BuildOptions() => new(
new GalaxyGatewayOptions("https://mxgw.test:5001", "key"),
new GalaxyMxAccessOptions("OtOpcUa-A"),
@@ -283,7 +317,8 @@ public sealed class GalaxyDriverFactoryTests
using var driver = new GalaxyDriver(
"g", BuildOptions(), hierarchySource: null, dataReader: null,
dataWriter: null, subscriber: new NoopSubscriber());
await driver.InitializeAsync(MinimalConfig, CancellationToken.None);
// #664: "{}" — MinimalConfig is not equivalent to BuildOptions(), and InitializeAsync now refuses that.
await driver.InitializeAsync("{}", CancellationToken.None);
driver.GetHostStatuses().ShouldBeEmpty();
}