feat(auditlog): site script-side emitters stamp ParentExecutionId

This commit is contained in:
Joseph Doherty
2026-05-21 17:45:55 -04:00
parent 6af2607a50
commit 150ba5e63f
9 changed files with 343 additions and 60 deletions

View File

@@ -92,22 +92,6 @@ public class ExecutionCorrelationContextTests
parentExecutionId: parentExecutionId);
}
/// <summary>
/// Reads a private <see cref="Guid"/>/<see cref="Nullable{Guid}"/> field off a
/// <see cref="ScriptRuntimeContext"/>. The ParentExecutionId plumbing (Audit
/// Log #23, Task 4) only stores the value on the context — no emitter stamps
/// it onto an audit row yet (that is Task 5) — so the field is inspected
/// directly rather than through an emitted row.
/// </summary>
private static object? ReadPrivateField(ScriptRuntimeContext context, string fieldName)
{
var field = typeof(ScriptRuntimeContext).GetField(
fieldName,
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
Assert.NotNull(field);
return field!.GetValue(context);
}
/// <summary>
/// Spin up a fresh in-memory SQLite database with a tiny single-table
/// schema. The keep-alive root must outlive any auditing wrapper the test
@@ -203,52 +187,99 @@ public class ExecutionCorrelationContextTests
}
[Fact]
public void ParentExecutionIdSupplied_StoredVerbatim_AndOwnExecutionIdIsFreshAndDistinct()
public async Task ParentExecutionIdSupplied_StampedOnEmittedRow_AndDistinctFromOwnExecutionId()
{
// Audit Log #23 (ParentExecutionId, Task 4): an inbound-API-routed call
// Audit Log #23 (ParentExecutionId, Task 5): an inbound-API-routed call
// supplies the spawning execution's ExecutionId as the routed script's
// ParentExecutionId. The context must store that value verbatim AND
// still mint its OWN fresh ExecutionId — the routed script is a new
// execution, it does not inherit the parent's id.
// ParentExecutionId. Every audit row the routed script emits must carry
// that value in AuditEvent.ParentExecutionId — and still carry its OWN
// fresh ExecutionId, distinct from the parent (the routed script is a
// new execution, it does not inherit the parent's id).
var parentExecutionId = Guid.NewGuid();
var client = new Mock<IExternalSystemClient>();
client
.Setup(c => c.CallAsync("ERP", "GetOrder", It.IsAny<IReadOnlyDictionary<string, object?>?>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ExternalCallResult(true, "{}", null));
var writer = new CapturingAuditWriter();
var context = CreateContext(
externalSystemClient: null,
client.Object,
databaseGateway: null,
auditWriter: null,
writer,
// executionId omitted — the ctor's `?? Guid.NewGuid()` fallback runs.
parentExecutionId: parentExecutionId);
await context.ExternalSystem.Call("ERP", "GetOrder");
var storedParent = ReadPrivateField(context, "_parentExecutionId");
var ownExecutionId = ReadPrivateField(context, "_executionId");
// The parent id is carried through untouched.
Assert.Equal(parentExecutionId, storedParent);
var evt = Assert.Single(writer.Events);
// The parent id is stamped on the emitted row untouched.
Assert.Equal(parentExecutionId, evt.ParentExecutionId);
// The routed script's own ExecutionId is freshly generated, non-empty,
// and NOT the parent id — they are separate correlation values.
Assert.NotNull(ownExecutionId);
var ownId = Assert.IsType<Guid>(ownExecutionId);
Assert.NotEqual(Guid.Empty, ownId);
Assert.NotEqual(parentExecutionId, ownId);
Assert.NotNull(evt.ExecutionId);
Assert.NotEqual(Guid.Empty, evt.ExecutionId!.Value);
Assert.NotEqual(parentExecutionId, evt.ExecutionId!.Value);
}
[Fact]
public void NoParentExecutionIdSupplied_NonRoutedRun_ParentStaysNull()
public async Task NoParentExecutionIdSupplied_NonRoutedRun_ParentStaysNullOnEmittedRow()
{
// A normal (tag-change / timer) script run is not inbound-API-routed —
// no ParentExecutionId is supplied, so _parentExecutionId stays null
// while the run still gets its own fresh ExecutionId.
// no ParentExecutionId is supplied, so every emitted audit row carries
// a null ParentExecutionId while the run still gets its own fresh
// ExecutionId.
var client = new Mock<IExternalSystemClient>();
client
.Setup(c => c.CallAsync("ERP", "GetOrder", It.IsAny<IReadOnlyDictionary<string, object?>?>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ExternalCallResult(true, "{}", null));
var writer = new CapturingAuditWriter();
var context = CreateContext(client.Object, databaseGateway: null, writer);
await context.ExternalSystem.Call("ERP", "GetOrder");
var evt = Assert.Single(writer.Events);
Assert.Null(evt.ParentExecutionId);
Assert.NotNull(evt.ExecutionId);
Assert.NotEqual(Guid.Empty, evt.ExecutionId!.Value);
}
[Fact]
public async Task ParentExecutionIdSupplied_StampedOnApiAndDbRows_FromSameContext()
{
// The execution-wide contract extends to ParentExecutionId: an
// ExternalSystem.Call and a sync Database write performed through ONE
// routed context both carry the identical ParentExecutionId.
var parentExecutionId = Guid.NewGuid();
using var keepAlive = new SqliteConnection("Data Source=ecc-parent;Mode=Memory;Cache=Shared");
var innerDb = NewInMemoryDb(out var _);
var client = new Mock<IExternalSystemClient>();
client
.Setup(c => c.CallAsync("ERP", "GetOrder", It.IsAny<IReadOnlyDictionary<string, object?>?>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ExternalCallResult(true, "{}", null));
var gateway = new Mock<IDatabaseGateway>();
gateway
.Setup(g => g.GetConnectionAsync(ConnectionName, It.IsAny<CancellationToken>()))
.ReturnsAsync(innerDb);
var writer = new CapturingAuditWriter();
var context = CreateContext(
externalSystemClient: null,
databaseGateway: null,
auditWriter: null);
client.Object, gateway.Object, writer, parentExecutionId: parentExecutionId);
var storedParent = ReadPrivateField(context, "_parentExecutionId");
var ownExecutionId = ReadPrivateField(context, "_executionId");
await context.ExternalSystem.Call("ERP", "GetOrder");
Assert.Null(storedParent);
var ownId = Assert.IsType<Guid>(ownExecutionId);
Assert.NotEqual(Guid.Empty, ownId);
await using (var conn = await context.Database.Connection(ConnectionName))
await using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO t (id, name) VALUES (1, 'alpha')";
await cmd.ExecuteNonQueryAsync();
}
Assert.Equal(2, writer.Events.Count);
var apiRow = Assert.Single(writer.Events, e => e.Channel == AuditChannel.ApiOutbound);
var dbRow = Assert.Single(writer.Events, e => e.Channel == AuditChannel.DbOutbound);
Assert.Equal(parentExecutionId, apiRow.ParentExecutionId);
Assert.Equal(parentExecutionId, dbRow.ParentExecutionId);
}
}