fix(transport): blocker heuristic no longer hard-blocks valid imports — local declarations excluded, denylist extended, template-script findings downgraded to warnings

#05-T19. Import name-resolution findings are now split by origin: template-script
(and attribute-default-expression) references become non-blocking advisory
warnings (ConflictKind.Warning + ImportResult.Warnings) since the design-time
deploy gate re-validates authoritatively; ApiMethod-script references stay hard
errors (SemanticValidationException / ConflictKind.Blocker) as they have no
downstream gate. Names declared locally in a body (Roslyn-parsed local functions
/ methods) are excluded so a script's own helper isn't flagged; KnownNonReferenceNames
extended with common BCL/LINQ/SQL surface. DetectBlockersAsync and
RunSemanticValidationAsync Pass 1 apply the identical split so preview and apply
agree. Rollback/hard-fail tests retargeted to ApiMethods (the still-hard-failing
vehicle). CentralUI preview renders the new Warning badge/advisory.

Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
Joseph Doherty
2026-07-10 02:12:26 -04:00
parent 8e92198078
commit c60347c5e7
10 changed files with 426 additions and 121 deletions
@@ -77,6 +77,10 @@ public sealed class SemanticValidatorImportTests : IDisposable
var exporter = scope.ServiceProvider.GetRequiredService<IBundleExporter>();
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var ids = await ctx.Templates.Select(t => t.Id).ToListAsync();
// #05-T19: also carry any seeded ApiMethods so the severity-split
// tests (template-script = warning, ApiMethod = hard error) exercise
// both source kinds through the same export→load→apply path.
var apiIds = await ctx.ApiMethods.Select(m => m.Id).ToListAsync();
var selection = new ExportSelection(
TemplateIds: ids,
SharedScriptIds: Array.Empty<int>(),
@@ -84,7 +88,7 @@ public sealed class SemanticValidatorImportTests : IDisposable
DatabaseConnectionIds: Array.Empty<int>(),
NotificationListIds: Array.Empty<int>(),
SmtpConfigurationIds: Array.Empty<int>(),
ApiMethodIds: Array.Empty<int>(),
ApiMethodIds: apiIds,
IncludeDependencies: false);
var stream = await exporter.ExportAsync(selection,
@@ -103,6 +107,7 @@ public sealed class SemanticValidatorImportTests : IDisposable
ctx.TemplateScripts.RemoveRange(ctx.TemplateScripts);
ctx.TemplateAttributes.RemoveRange(ctx.TemplateAttributes);
ctx.Templates.RemoveRange(ctx.Templates);
ctx.ApiMethods.RemoveRange(ctx.ApiMethods);
await ctx.SaveChangesAsync();
}
@@ -118,15 +123,14 @@ public sealed class SemanticValidatorImportTests : IDisposable
}
[Fact]
public async Task SemanticValidator_catches_invalid_call_target_at_import()
public async Task TemplateScript_with_unresolved_reference_warns_but_does_not_block_import()
{
// Arrange — template whose script body calls UnknownHelper(): a
// #05-T19 — a template whose script body calls UnknownHelper(): a
// PascalCase identifier that doesn't resolve to any SharedScript or
// ExternalSystem in the bundle or the target. This is the operator-
// facing "invalid call target" surface — the full SemanticValidator's
// CallScript/CallShared signature checks live downstream of name
// resolution (you can't check arg count against a function that
// doesn't exist). Pass 1 catches it first and fails fast.
// ExternalSystem in the bundle or the target. Under the severity split,
// template-script name-resolution findings are ADVISORY (the design-time
// deploy gate re-validates authoritatively), so the import SUCCEEDS with
// the finding surfaced on ImportResult.Warnings — it does NOT hard-block.
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
@@ -140,8 +144,129 @@ public sealed class SemanticValidatorImportTests : IDisposable
var sessionId = await ExportWipeAndLoadAsync();
// Act — apply must throw SemanticValidationException carrying the bad
// call target by name.
// Act — apply succeeds (no throw); the finding rides on Warnings.
ImportResult result;
await using (var scope = _provider.CreateAsyncScope())
{
var importer = scope.ServiceProvider.GetRequiredService<IBundleImporter>();
result = await importer.ApplyAsync(sessionId,
new List<ImportResolution>
{
new("Template", "ScriptCallsUnknown", ResolutionAction.Add, null),
},
user: "bob");
}
// Assert — the template landed AND the advisory warning names the target.
Assert.Equal(1, result.Added);
Assert.Contains(result.Warnings,
w => w.Contains("UnknownHelper", StringComparison.Ordinal));
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
Assert.True(await ctx.Templates.AnyAsync(t => t.Name == "ScriptCallsUnknown"));
}
}
[Fact]
public async Task Apply_TemplateScriptWithLocalPascalCaseMethod_DoesNotHardBlock()
{
// #05-T19 — a template script that DECLARES and calls its own local
// PascalCase function. The identifier scan would otherwise flag
// ComputeRate as a missing SharedScript reference; the local-declaration
// exclusion (Roslyn-parsed local functions/methods) removes it, so the
// import produces neither an error nor a warning.
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var t = new Template("SelfContainedCalc");
t.Attributes.Add(new TemplateAttribute("A") { DataType = DataType.Double, Value = "1" });
t.Scripts.Add(new Commons.Entities.Templates.TemplateScript(
"init",
"decimal ComputeRate(decimal x) => x * 2; return ComputeRate(2m);"));
ctx.Templates.Add(t);
await ctx.SaveChangesAsync();
}
var sessionId = await ExportWipeAndLoadAsync();
ImportResult result;
await using (var scope = _provider.CreateAsyncScope())
{
var importer = scope.ServiceProvider.GetRequiredService<IBundleImporter>();
result = await importer.ApplyAsync(sessionId,
new List<ImportResolution>
{
new("Template", "SelfContainedCalc", ResolutionAction.Add, null),
},
user: "bob");
}
Assert.Equal(1, result.Added);
// The locally-declared function must NOT surface as a warning.
Assert.DoesNotContain(result.Warnings,
w => w.Contains("ComputeRate", StringComparison.Ordinal));
}
[Fact]
public async Task Apply_TemplateScriptWithRegexAndStringBuilder_DoesNotHardBlock()
{
// #05-T19 — a template script using stdlib helpers that the extended
// denylist now recognizes (Regex, StringBuilder, …). None resolve to a
// user SharedScript/ExternalSystem, and none should be flagged.
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var t = new Template("UsesStdlib");
t.Scripts.Add(new Commons.Entities.Templates.TemplateScript(
"init",
"""
var ok = Regex.IsMatch("abc", "a.c");
var sb = new StringBuilder();
sb.Append("x");
return sb.ToString();
"""));
ctx.Templates.Add(t);
await ctx.SaveChangesAsync();
}
var sessionId = await ExportWipeAndLoadAsync();
ImportResult result;
await using (var scope = _provider.CreateAsyncScope())
{
var importer = scope.ServiceProvider.GetRequiredService<IBundleImporter>();
result = await importer.ApplyAsync(sessionId,
new List<ImportResolution>
{
new("Template", "UsesStdlib", ResolutionAction.Add, null),
},
user: "bob");
}
Assert.Equal(1, result.Added);
Assert.DoesNotContain(result.Warnings,
w => w.Contains("Regex", StringComparison.Ordinal)
|| w.Contains("StringBuilder", StringComparison.Ordinal));
}
[Fact]
public async Task Apply_ApiMethodWithUnresolvedReference_StillHardBlocks()
{
// #05-T19 — an ApiMethod script referencing a genuinely-missing
// ExternalSystem. ApiMethod findings have no downstream deploy gate, so
// they remain HARD errors: the apply throws SemanticValidationException.
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
ctx.ApiMethods.Add(new Commons.Entities.InboundApi.ApiMethod(
"Ingest",
"var x = ErpMissing(); return x;"));
await ctx.SaveChangesAsync();
}
var sessionId = await ExportWipeAndLoadAsync();
SemanticValidationException ex = default!;
await using (var scope = _provider.CreateAsyncScope())
{
@@ -150,21 +275,20 @@ public sealed class SemanticValidatorImportTests : IDisposable
importer.ApplyAsync(sessionId,
new List<ImportResolution>
{
new("Template", "ScriptCallsUnknown", ResolutionAction.Add, null),
new("ApiMethod", "Ingest", ResolutionAction.Add, null),
},
user: "bob"));
}
// Assert — error message names the bad target.
Assert.NotEmpty(ex.Errors);
Assert.Contains(ex.Errors,
err => err.Contains("UnknownHelper", StringComparison.Ordinal));
err => err.Contains("ErpMissing", StringComparison.Ordinal));
// Rollback — no template row landed.
// Rollback — no ApiMethod row landed.
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
Assert.False(await ctx.Templates.AnyAsync(t => t.Name == "ScriptCallsUnknown"));
Assert.False(await ctx.ApiMethods.AnyAsync(m => m.Name == "Ingest"));
}
}