feat(commons): FlowGraphBuilder folds outbound lifecycles into one node; async/spawn edges; Response from the inbound execution

Fold rule: outbound rows sharing (kind, CorrelationId) become one FlowStep with
a Lifecycle list — first row's identity/time/input, latest row's outcome/
subtitle/output/error, DurationMs = sync row's own or submit→terminal wall time
(null while pending). Chain skips async steps (Notify, or first row
CachedSubmit) and links them with Async edges from the preceding chain step.
Spawner edges from a non-RouteCall are Spawn: true. The Response links from the
last non-async step of the inbound execution (the RouteCall), not the globally
last cascaded step (D9). FlowGraphView places a Response at max column + 1 so
the D9 origin does not pull it left (pulled forward from Task 7 to keep the
layout test green).

Claude-Session: https://claude.ai/code/session_01NcMG6xXdsJbmD2wYAgkK29
This commit is contained in:
Joseph Doherty
2026-09-06 01:42:48 -04:00
parent e26616bebc
commit 29129ef17a
5 changed files with 332 additions and 23 deletions
@@ -77,19 +77,26 @@ public class FlowGraphBuilderTests
Assert.Equal(FlowLane.Site, graph.Steps[6].Lane);
Assert.Equal(FlowLane.Central, graph.Steps[7].Lane);
// Exactly two boundary crossings: route → site run, and last site step → response.
var crossing = graph.Edges.Where(e => e.CrossesBoundary).ToList();
Assert.Equal(2, crossing.Count);
Assert.Contains(crossing, e => e.FromEventId == graph.Steps[1].EventId && e.ToEventId == graph.Steps[2].EventId);
Assert.Contains(crossing, e => e.FromEventId == graph.Steps[6].EventId && e.ToEventId == graph.Steps[7].EventId);
// Exactly one boundary crossing: route → site run. The Response links from the RouteCall (D9), Central → Central.
var crossing = Assert.Single(graph.Edges, e => e.CrossesBoundary);
Assert.Equal(graph.Steps[1].EventId, crossing.FromEventId);
Assert.Equal(graph.Steps[2].EventId, crossing.ToEventId);
Assert.Contains(graph.Edges, e => e.FromEventId == graph.Steps[1].EventId && e.ToEventId == graph.Steps[7].EventId && !e.CrossesBoundary);
Assert.DoesNotContain(graph.Edges, e => e.FromEventId == graph.Steps[6].EventId && e.ToEventId == graph.Steps[7].EventId);
// One inferred echo edge: first write → satisfied wait.
var external = Assert.Single(graph.Edges, e => e.External);
Assert.Equal(graph.Steps[3].EventId, external.FromEventId);
Assert.Equal(graph.Steps[4].EventId, external.ToEventId);
// The cascade run hangs off the write that spawned it (same site, no boundary).
Assert.Contains(graph.Edges, e => e.FromEventId == graph.Steps[5].EventId && e.ToEventId == graph.Steps[6].EventId && !e.CrossesBoundary);
// The cascade run hangs off the write that spawned it (same site, no boundary) — a Spawn edge (D9);
// the route → run edge and every in-chain edge are not.
var spawn = Assert.Single(graph.Edges, e => e.Spawn);
Assert.Equal(graph.Steps[5].EventId, spawn.FromEventId);
Assert.Equal(graph.Steps[6].EventId, spawn.ToEventId);
Assert.False(spawn.CrossesBoundary);
Assert.Contains(graph.Edges, e => e.FromEventId == graph.Steps[1].EventId && e.ToEventId == graph.Steps[2].EventId && !e.Spawn);
Assert.DoesNotContain(graph.Edges, e => e.Async);
// Inbound → route call chained inside the root execution.
Assert.Contains(graph.Edges, e => e.FromEventId == graph.Steps[0].EventId && e.ToEventId == graph.Steps[1].EventId);
@@ -179,4 +186,181 @@ public class FlowGraphBuilderTests
Assert.Null(ScriptTraceExtra.TryParse(null));
Assert.Null(ScriptTraceExtra.TryParse("nope"));
}
// ---- Outbound calls (2026-09-06 plan, Task 2) --------------------------------------------
private static AuditEvent SiteRunRow(int ms, Guid executionId = default) => ScadaBridgeAuditEventFactory.Create(
AuditChannel.ScriptTrace, AuditKind.ScriptRun, AuditStatus.Delivered,
occurredAtUtc: T0.AddMilliseconds(ms), actor: "S", target: "S", sourceNode: "node-a",
executionId: executionId == default ? SiteRun : executionId, sourceSiteId: "site-a", sourceInstanceId: "M1", durationMs: 800,
extra: new ScriptTraceExtra { TriggerKind = "call" }.ToJson());
private static AuditEvent WriteRow(int ms, string attr) => ScadaBridgeAuditEventFactory.Create(
AuditChannel.ScriptTrace, AuditKind.AttributeWrite, AuditStatus.Delivered,
occurredAtUtc: T0.AddMilliseconds(ms), actor: "S", target: attr, sourceNode: "node-a",
executionId: SiteRun, sourceSiteId: "site-a", sourceInstanceId: "M1", durationMs: 2,
extra: new ScriptTraceExtra { Writes = [new AttributeWriteDetail(attr, "1", true, null)] }.ToJson());
private static AuditEvent Outbound(AuditChannel channel, AuditKind kind, AuditStatus status, int ms, Guid? correlation, string target,
string node = "node-a", string? error = null, int? durationMs = null, string? request = null, string? response = null)
=> ScadaBridgeAuditEventFactory.Create(channel, kind, status,
occurredAtUtc: T0.AddMilliseconds(ms), actor: "S", target: target, sourceNode: node, correlationId: correlation,
executionId: SiteRun, sourceSiteId: "site-a", sourceInstanceId: "M1", errorMessage: error, durationMs: durationMs,
requestSummary: request, responseSummary: response);
[Fact]
public void CachedApiCall_FourRows_FoldIntoOneAsyncNode()
{
var corr = Guid.NewGuid();
var events = new List<AuditEvent>
{
SiteRunRow(20),
WriteRow(30, "A"),
Outbound(AuditChannel.ApiOutbound, AuditKind.CachedSubmit, AuditStatus.Submitted, 35, corr, "Delmia.AckRecipe", request: "{\"id\":1}"),
WriteRow(40, "B"),
Outbound(AuditChannel.ApiOutbound, AuditKind.ApiCallCached, AuditStatus.Forwarded, 2000, corr, "Delmia.AckRecipe"),
Outbound(AuditChannel.ApiOutbound, AuditKind.ApiCallCached, AuditStatus.Attempted, 3000, corr, "Delmia.AckRecipe", error: "503", durationMs: 40),
Outbound(AuditChannel.ApiOutbound, AuditKind.CachedResolve, AuditStatus.Delivered, 4135, corr, "Delmia.AckRecipe", response: "{\"ok\":true}"),
};
var graph = FlowGraphBuilder.Build(events);
Assert.Equal(4, graph.Steps.Count);
var api = Assert.Single(graph.Steps, s => s.Kind == FlowStepKind.ApiCall);
Assert.Equal(FlowLane.External, api.Lane);
Assert.Equal(events[2].EventId, api.EventId);
Assert.Equal(T0.AddMilliseconds(35), api.OccurredAtUtc.UtcDateTime);
Assert.Equal(4, api.Lifecycle!.Count);
Assert.Equal(new[] { "CachedSubmit", "ApiCallCached", "ApiCallCached", "CachedResolve" }, api.Lifecycle.Select(r => r.Kind));
Assert.Equal("503", api.Lifecycle[2].ErrorMessage);
Assert.Equal(FlowStepOutcome.Ok, api.Outcome);
Assert.Equal(4100, api.DurationMs);
Assert.Equal("{\"id\":1}", api.InputJson);
Assert.Equal("{\"ok\":true}", api.OutputJson);
Assert.Equal("Delivered · cached", api.Subtitle);
Assert.Null(api.ErrorMessage);
Assert.Equal(SiteRun, api.ExecutionId);
var run = graph.Steps[0]; var w1 = graph.Steps[1]; var w2 = graph.Steps[3];
Assert.Equal(new[] { FlowStepKind.ScriptRun, FlowStepKind.AttributeWrite, FlowStepKind.ApiCall, FlowStepKind.AttributeWrite },
graph.Steps.Select(s => s.Kind));
var async = Assert.Single(graph.Edges, e => e.Async);
Assert.Equal(w1.EventId, async.FromEventId);
Assert.Equal(api.EventId, async.ToEventId);
Assert.Contains(graph.Edges, e => e.FromEventId == run.EventId && e.ToEventId == w1.EventId && !e.Async);
Assert.Contains(graph.Edges, e => e.FromEventId == w1.EventId && e.ToEventId == w2.EventId && !e.Async);
Assert.DoesNotContain(graph.Edges, e => e.FromEventId == api.EventId);
Assert.Equal(3, graph.Edges.Count);
}
[Fact]
public void SyncApiCall_SitsInChain_NoAsyncEdge()
{
var events = new List<AuditEvent>
{
SiteRunRow(20),
WriteRow(30, "A"),
Outbound(AuditChannel.ApiOutbound, AuditKind.ApiCall, AuditStatus.Delivered, 35, null, "Delmia.AckRecipe", durationMs: 212),
WriteRow(40, "B"),
};
var graph = FlowGraphBuilder.Build(events);
var api = Assert.Single(graph.Steps, s => s.Kind == FlowStepKind.ApiCall);
Assert.Equal(212, api.DurationMs);
Assert.Single(api.Lifecycle!);
Assert.Equal("Delivered · sync", api.Subtitle);
Assert.DoesNotContain(graph.Edges, e => e.Async);
Assert.Contains(graph.Edges, e => e.FromEventId == graph.Steps[1].EventId && e.ToEventId == api.EventId);
Assert.Contains(graph.Edges, e => e.FromEventId == api.EventId && e.ToEventId == graph.Steps[3].EventId);
Assert.DoesNotContain(graph.Edges, e => e.FromEventId == graph.Steps[1].EventId && e.ToEventId == graph.Steps[3].EventId);
}
[Fact]
public void TwoSyncCalls_NullCorrelation_AreNotFoldedTogether()
{
var events = new List<AuditEvent>
{
SiteRunRow(20),
Outbound(AuditChannel.DbOutbound, AuditKind.DbWrite, AuditStatus.Delivered, 30, null, "MesDb"),
Outbound(AuditChannel.DbOutbound, AuditKind.DbWrite, AuditStatus.Failed, 40, null, "MesDb", error: "deadlock"),
};
var graph = FlowGraphBuilder.Build(events);
Assert.Equal(2, graph.Steps.Count(s => s.Kind == FlowStepKind.DbCall));
Assert.Equal(FlowStepOutcome.Failed, graph.Steps[2].Outcome);
Assert.Equal(2, graph.Edges.Count);
}
[Fact]
public void NotifySend_PlusCentralDeliverParked_FoldsIntoOneParkedNotifyNode()
{
var notificationId = Guid.NewGuid();
var events = new List<AuditEvent>
{
SiteRunRow(20),
WriteRow(30, "A"),
Outbound(AuditChannel.Notification, AuditKind.NotifySend, AuditStatus.Submitted, 35, notificationId, "Planners", request: "{\"subject\":\"hi\"}"),
Outbound(AuditChannel.Notification, AuditKind.NotifyDeliver, AuditStatus.Attempted, 5000, notificationId, "Planners", node: "central-a", error: "smtp down", durationMs: 30),
Outbound(AuditChannel.Notification, AuditKind.NotifyDeliver, AuditStatus.Parked, 9000, notificationId, "Planners", node: "central-a", error: "retries exhausted"),
};
var graph = FlowGraphBuilder.Build(events);
var notify = Assert.Single(graph.Steps, s => s.Kind == FlowStepKind.Notify);
Assert.Equal(FlowStepOutcome.Parked, notify.Outcome);
Assert.Equal(3, notify.Lifecycle!.Count);
Assert.Equal("central-a", notify.Lifecycle[2].Node);
Assert.Equal("retries exhausted", notify.ErrorMessage);
Assert.Equal(8965, notify.DurationMs);
Assert.Equal("Parked", notify.Subtitle);
Assert.Equal("node-a", notify.Node);
var async = Assert.Single(graph.Edges, e => e.Async);
Assert.Equal(graph.Steps[1].EventId, async.FromEventId);
Assert.Equal(notify.EventId, async.ToEventId);
}
[Fact]
public void CachedSubmitOnly_IsPending_WithNoDuration()
{
var events = new List<AuditEvent>
{
SiteRunRow(20),
Outbound(AuditChannel.DbOutbound, AuditKind.CachedSubmit, AuditStatus.Submitted, 35, Guid.NewGuid(), "MesDb"),
};
var graph = FlowGraphBuilder.Build(events);
var db = Assert.Single(graph.Steps, s => s.Kind == FlowStepKind.DbCall);
Assert.Equal(FlowStepOutcome.Pending, db.Outcome);
Assert.Null(db.DurationMs);
Assert.Single(db.Lifecycle!);
var async = Assert.Single(graph.Edges);
Assert.True(async.Async);
Assert.Equal(graph.Steps[0].EventId, async.FromEventId);
}
[Fact]
public void ResponseLinksFromTheInboundExecutionsLastStep_NotTheLastCascadedStep()
{
var events = DelmiaShaped();
var graph = FlowGraphBuilder.Build(events);
var response = Assert.Single(graph.Steps, s => s.Kind == FlowStepKind.Response);
var into = Assert.Single(graph.Edges, e => e.ToEventId == response.EventId);
Assert.Equal(graph.Steps[1].EventId, into.FromEventId);
Assert.Equal(FlowStepKind.RouteCall, graph.Steps[1].Kind);
}
[Fact]
public void ResponseFallsBackToTheInboundRow_WhenNothingElseSharesItsExecution()
{
var inbound = ScadaBridgeAuditEventFactory.Create(AuditChannel.ApiInbound, AuditKind.InboundRequest, AuditStatus.Delivered,
occurredAtUtc: T0, actor: "k", target: "Ping", sourceNode: "central-a", executionId: Root, durationMs: 5, responseSummary: "{}");
var graph = FlowGraphBuilder.Build([inbound]);
Assert.Equal(2, graph.Steps.Count);
var edge = Assert.Single(graph.Edges);
Assert.Equal(graph.Steps[0].EventId, edge.FromEventId);
Assert.Equal(graph.Steps[1].EventId, edge.ToEventId);
}
}