feat(inbound-api): surface method compile errors to operators — api-method validate/recompile + compile state on get (ScadaBridge#75)
The data plane answers a broken inbound method with the generic "Script compilation failed for this method"; the Roslyn diagnostic reached only the central log, and the known-bad cache could not be observed or reset per method. - InboundScriptExecutor keeps the diagnostics with each known-bad record and exposes GetCompileState(name, currentScript): IsCompiled / IsKnownBad / LastCompileError, judged against the current script text (a record for older text is reported as not compiled yet). - Management: ValidateApiMethodCommand (compile the stored script, return ApiMethodCompileResult, register nothing, cache untouched) and RecompileApiMethodCommand (CompileAndRegister without re-sending the entity, drops the node's ApiMethodCache row, audited "Recompile"). Both Designer, like UpdateApiMethod; dispatched through the shared ApiMethodCompileOperations. GetApiMethod keeps the entity fields top-level and adds isCompiled, isKnownBad, lastCompileError and compileStateNode. Compile state is per central node and every result names the node. - CLI: api-method validate --id, api-method recompile --id. - Central UI: the API-method edit page shows the saved script's compile state and last error, with Validate and Recompile actions (same operations, same Designer gate, fresh DI scope). - RequiredRoleMatrixTests entries; ManagementSurfaceParityTests covers the new CLI leaves; ApiMethodCompileDiagnosticsTests + an executor CompileState test. - Docs: CLI README, Component-CLI/ManagementService/InboundAPI, components/*. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -57,7 +57,12 @@ public class InboundScriptExecutor
|
||||
// dropped — the cache is just a fast-fail optimisation; the per-request DB
|
||||
// lookup remains the correctness path.
|
||||
private const int KnownBadMethodsCap = 1000;
|
||||
private readonly ConcurrentDictionary<string, string> _knownBadMethods = new();
|
||||
private readonly ConcurrentDictionary<string, KnownBadMethod> _knownBadMethods = new();
|
||||
|
||||
// A known-bad record: the exact script text that failed and the diagnostics it failed
|
||||
// with, so an operator can read them through the management surface (ScadaBridge#75)
|
||||
// instead of the central log.
|
||||
private sealed record KnownBadMethod(string Script, IReadOnlyList<string> Errors);
|
||||
|
||||
/// <summary>Diagnostic helper — whether a compiled handler is cached for the method.</summary>
|
||||
internal bool HasHandler(string methodName) => _scriptHandlers.ContainsKey(methodName);
|
||||
@@ -69,6 +74,31 @@ public class InboundScriptExecutor
|
||||
/// </summary>
|
||||
internal int KnownBadMethodCount => _knownBadMethods.Count;
|
||||
|
||||
/// <summary>
|
||||
/// This node's compile state for <paramref name="methodName"/>, judged against the
|
||||
/// method's CURRENT script text (ScadaBridge#75). A handler or failure record left over
|
||||
/// from different script text describes an older revision, so it is reported as not yet
|
||||
/// compiled — the next request (or a recompile) compiles the current text. The state is
|
||||
/// per central node: each node compiles and caches independently.
|
||||
/// </summary>
|
||||
/// <param name="methodName">The method name.</param>
|
||||
/// <param name="currentScript">The method's current (persisted) script text.</param>
|
||||
/// <returns>The compile state of the method on this node.</returns>
|
||||
public ApiMethodCompileState GetCompileState(string methodName, string? currentScript)
|
||||
{
|
||||
var script = currentScript ?? string.Empty;
|
||||
if (_knownBadMethods.TryGetValue(methodName, out var bad)
|
||||
&& string.Equals(bad.Script, script, StringComparison.Ordinal))
|
||||
{
|
||||
return new ApiMethodCompileState(IsCompiled: false, IsKnownBad: true, string.Join("; ", bad.Errors));
|
||||
}
|
||||
|
||||
// A null Script is the RegisterHandler test seam, which serves regardless of text.
|
||||
var compiled = _scriptHandlers.TryGetValue(methodName, out var cached)
|
||||
&& (cached.Script is null || string.Equals(cached.Script, script, StringComparison.Ordinal));
|
||||
return new ApiMethodCompileState(IsCompiled: compiled, IsKnownBad: false, LastCompileError: null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records <paramref name="methodName"/> → <paramref name="script"/> in the
|
||||
/// known-bad-methods cache so the CURRENT failing script text short-circuits the
|
||||
@@ -79,18 +109,19 @@ public class InboundScriptExecutor
|
||||
/// (paying the cheap recompile next time rather than leaking memory under a
|
||||
/// unique-name flood). Records are cleared on a successful (re)compile.
|
||||
/// </summary>
|
||||
private void TryRecordBadMethod(string methodName, string script)
|
||||
private void TryRecordBadMethod(string methodName, string script, IReadOnlyList<string> errors)
|
||||
{
|
||||
var record = new KnownBadMethod(script, errors);
|
||||
// Overwrite an existing record (newer bad script wins) without counting
|
||||
// against the cap; only a genuinely new method name is gated by the cap.
|
||||
if (_knownBadMethods.ContainsKey(methodName))
|
||||
{
|
||||
_knownBadMethods[methodName] = script;
|
||||
_knownBadMethods[methodName] = record;
|
||||
return;
|
||||
}
|
||||
if (_knownBadMethods.Count >= KnownBadMethodsCap)
|
||||
return;
|
||||
_knownBadMethods.TryAdd(methodName, script);
|
||||
_knownBadMethods.TryAdd(methodName, record);
|
||||
}
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
@@ -227,7 +258,7 @@ public class InboundScriptExecutor
|
||||
// through the capped TryRecordBadMethod helper so the cache
|
||||
// cannot grow without bound under a flood of unique method names.
|
||||
errors = compileErrors;
|
||||
publish = () => TryRecordBadMethod(name, script ?? string.Empty);
|
||||
publish = () => TryRecordBadMethod(name, script ?? string.Empty, compileErrors);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -562,20 +593,20 @@ public class InboundScriptExecutor
|
||||
if (cached == null)
|
||||
{
|
||||
// Fast-fail only when the CURRENT script text is the one already known bad.
|
||||
if (_knownBadMethods.TryGetValue(method.Name, out var badScript)
|
||||
&& string.Equals(badScript, method.Script, StringComparison.Ordinal))
|
||||
if (_knownBadMethods.TryGetValue(method.Name, out var knownBad)
|
||||
&& string.Equals(knownBad.Script, method.Script, StringComparison.Ordinal))
|
||||
return new InboundScriptResult(false, null, "Script compilation failed for this method", "SCRIPT_COMPILE_FAILED");
|
||||
|
||||
// Lazy compile (handles methods created after startup, and stale/known-bad
|
||||
// recompiles). Compile outside the cache so a failed compile is not stored,
|
||||
// then add so concurrent first-callers share a single handler instance.
|
||||
var (compiled, _) = CompileSingleFlight(method);
|
||||
var (compiled, compileErrors) = CompileSingleFlight(method);
|
||||
if (compiled == null)
|
||||
{
|
||||
// Cache the failure so the next request short-circuits above.
|
||||
// Routed through TryRecordBadMethod so the
|
||||
// cache is bounded under a flood of unique method names.
|
||||
TryRecordBadMethod(method.Name, method.Script ?? string.Empty);
|
||||
TryRecordBadMethod(method.Name, method.Script ?? string.Empty, compileErrors);
|
||||
return new InboundScriptResult(false, null, "Script compilation failed for this method", "SCRIPT_COMPILE_FAILED");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user