feat(analyzers): #1091 OTOPCUA0001 guards the caller-aware write capabilities

- ICallerAwareWritable and the new ICallerAwareWriteOutcomeCapability join the guarded-interface list. Each derives
  from a guarded interface but declares its OWN member, which is not an implementation of the base's, so
  callerAware.WriteAsync(writes, caller, ct) on an interface or concrete receiver passed unflagged.
- ICallerAwareWriteOutcomeCapability (Core.Abstractions.DataConnections): IWriteOutcomeCapability on behalf of a
  WriteCaller, for the gRPC data-connection write path (used by the Galaxy driver, next commit).
- Tests: unwrapped caller-aware WriteAsync trips on interface AND concrete receivers; the caller-aware outcome
  overload trips unwrapped and passes wrapped (positive control). Both failed (0 diagnostics) with the two
  entries removed from the list.
This commit is contained in:
Joseph Doherty
2026-12-04 14:57:53 -05:00
parent ec4eed337c
commit 2f22003061
3 changed files with 97 additions and 1 deletions
@@ -0,0 +1,21 @@
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
/// <summary>
/// #1091 — <see cref="IWriteOutcomeCapability" /> on behalf of a caller: the gRPC data-connection write path's
/// explicit-outcome overload for a driver that also acts on the caller's identity (<see cref="ICallerAwareWritable" />;
/// the Galaxy driver's SecuredWrite attributes). The runtime calls it when the admitted write carries a
/// <see cref="WriteCaller" /> (the connection's key, and only when an export covering the node opts in to secured writes,
/// #1084); a write without one goes to <see cref="IWriteOutcomeCapability.WriteWithOutcomesAsync" />. Either way the
/// driver reports each write's outcome definitively where it can (e.g. a refused secured write is
/// <see cref="DataWriteState.DriverConfirmedFailure" /> + <c>BadUserAccessDenied</c>, not <c>OutcomeUnknown</c>).
/// </summary>
public interface ICallerAwareWriteOutcomeCapability : IWriteOutcomeCapability
{
/// <summary>Write a batch on behalf of <paramref name="caller" />; one outcome per request, in order.</summary>
/// <param name="writes">Pairs of full reference + value to write.</param>
/// <param name="caller">Who issued the write.</param>
/// <param name="cancellationToken">Cancellation token; the driver should abort the batch if cancelled.</param>
/// <returns>One <see cref="DataWriteOutcome" /> per requested write, in the same order.</returns>
Task<IReadOnlyList<DataWriteOutcome>> WriteWithOutcomesAsync(
IReadOnlyList<WriteRequest> writes, WriteCaller caller, CancellationToken cancellationToken);
}
@@ -79,6 +79,11 @@ public sealed class UnwrappedCapabilityCallAnalyzer : DiagnosticAnalyzer
// work and so must run under the invoker's timeout/breaker/telemetry.
"ZB.MOM.WW.OtOpcUa.Core.Abstractions.IAcknowledgedAcquisitionSource",
"ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.IWriteOutcomeCapability",
// #1091: the caller-aware overloads. Each DERIVES from a guarded interface but declares its own member, and a
// derived interface's new member is not an implementation of the base's — so `callerAware.WriteAsync(w, caller, ct)`
// on an ICallerAwareWritable receiver used to pass unflagged. They must be listed themselves.
"ZB.MOM.WW.OtOpcUa.Core.Abstractions.ICallerAwareWritable",
"ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ICallerAwareWriteOutcomeCapability",
];
/// <summary>
@@ -109,7 +114,7 @@ public sealed class UnwrappedCapabilityCallAnalyzer : DiagnosticAnalyzer
category: "OtOpcUa.Resilience",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "Phase 6.1 Stream A requires every IReadable/IWritable/ITagDiscovery/ISubscribable/IHostConnectivityProbe/IAlarmSource/IAcknowledgedAcquisitionSource/IWriteOutcomeCapability call to route through the shared Polly pipeline. Direct calls skip the pipeline + lose per-host isolation, retry semantics, and telemetry. If the caller is Core/Server/Driver dispatch code, wrap the call in CapabilityInvoker.ExecuteAsync. If the caller is a unit test invoking the driver directly to test its wire-level behavior, either suppress with a pragma or move the suppression into a NoWarn for the test project.");
description: "Phase 6.1 Stream A requires every IReadable/IWritable/ITagDiscovery/ISubscribable/IHostConnectivityProbe/IAlarmSource/IAcknowledgedAcquisitionSource/IWriteOutcomeCapability/ICallerAwareWritable/ICallerAwareWriteOutcomeCapability call to route through the shared Polly pipeline. Direct calls skip the pipeline + lose per-host isolation, retry semantics, and telemetry. If the caller is Core/Server/Driver dispatch code, wrap the call in CapabilityInvoker.ExecuteAsync. If the caller is a unit test invoking the driver directly to test its wire-level behavior, either suppress with a pragma or move the suppression into a NoWarn for the test project.");
/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
@@ -32,6 +32,10 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions {
public interface IWritable {
ValueTask WriteAsync(IReadOnlyList<object> ops, CancellationToken ct);
}
public sealed class WriteCaller { }
public interface ICallerAwareWritable : IWritable {
ValueTask WriteAsync(IReadOnlyList<object> ops, WriteCaller caller, CancellationToken ct);
}
public interface ITagDiscovery {
Task DiscoverAsync(CancellationToken ct);
}
@@ -72,6 +76,9 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections {
public interface IWriteOutcomeCapability {
Task<IReadOnlyList<DataWriteOutcome>> WriteWithOutcomesAsync(IReadOnlyList<WriteRequest> writes, CancellationToken ct);
}
public interface ICallerAwareWriteOutcomeCapability : IWriteOutcomeCapability {
Task<IReadOnlyList<DataWriteOutcome>> WriteWithOutcomesAsync(IReadOnlyList<WriteRequest> writes, ZB.MOM.WW.OtOpcUa.Core.Abstractions.WriteCaller caller, CancellationToken ct);
}
}
namespace ZB.MOM.WW.OtOpcUa.Core.Resilience {
using System;
@@ -801,6 +808,69 @@ namespace ZB.MOM.WW.OtOpcUa.Server {
diags.ShouldBeEmpty();
}
/// <summary>#1091: the caller-aware write overload (a NEW member on an interface deriving from IWritable, so not an
/// implementation of IWritable.WriteAsync) trips on an interface receiver and on a concrete driver. Failed before
/// ICallerAwareWritable joined the guarded list (0 diagnostics).</summary>
[Fact]
public async Task Direct_CallerAware_WriteAsync_Call_TripsDiagnostic()
{
const string userSrc = """
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Server {
public sealed class CallerDriver : ICallerAwareWritable {
public ValueTask WriteAsync(IReadOnlyList<object> ops, CancellationToken ct) => default;
public ValueTask WriteAsync(IReadOnlyList<object> ops, WriteCaller caller, CancellationToken ct) => default;
}
public sealed class BadCallerWrite {
public async Task ViaInterface(ICallerAwareWritable driver) {
await driver.WriteAsync(new List<object>(), new WriteCaller(), CancellationToken.None);
}
public async Task ViaConcrete(CallerDriver driver) {
await driver.WriteAsync(new List<object>(), new WriteCaller(), CancellationToken.None);
}
}
}
""";
var diags = await Compile(userSrc);
diags.Length.ShouldBe(2);
diags.ShouldAllBe(d => d.GetMessage().Contains("WriteAsync"));
}
/// <summary>#1091: the caller-aware outcome overload trips too; the same calls wrapped pass (positive control).</summary>
[Fact]
public async Task CallerAware_outcome_and_write_overloads_trip_unwrapped_and_pass_wrapped()
{
const string userSrc = """
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Core.Resilience;
namespace ZB.MOM.WW.OtOpcUa.Server {
public sealed class Mixed {
public async Task Bad(ICallerAwareWriteOutcomeCapability driver) {
var _ = await driver.WriteWithOutcomesAsync(new List<WriteRequest>(), new WriteCaller(), CancellationToken.None);
}
public async Task Good(ICallerAwareWriteOutcomeCapability rich, ICallerAwareWritable legacy, CapabilityInvoker invoker) {
var _ = await invoker.ExecuteWriteAsync("h1", false,
async ct => await rich.WriteWithOutcomesAsync(new List<WriteRequest>(), new WriteCaller(), ct), CancellationToken.None);
await invoker.ExecuteWriteAsync("h1", false,
async ct => await legacy.WriteAsync(new List<object>(), new WriteCaller(), ct), CancellationToken.None);
}
}
}
""";
var diags = await Compile(userSrc);
diags.Length.ShouldBe(1);
diags[0].GetMessage().ShouldContain("WriteWithOutcomesAsync");
}
private static async Task<ImmutableArray<Diagnostic>> CompileWithoutStubs(string userSource)
{
var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(userSource) };