Resolve all 46 low-severity architecture review findings

This commit is contained in:
Joseph Doherty
2026-09-09 22:56:36 -04:00
parent fef3e7675f
commit 9ee402bbcd
139 changed files with 2621 additions and 1082 deletions
@@ -271,6 +271,15 @@ public class SqliteAuditWriter : IAuditWriter, ISiteAuditQueue, IAsyncDisposable
-- to the window. Additive and idempotent.
CREATE INDEX IF NOT EXISTS IX_audit_event_occurred
ON audit_event (OccurredAtUtc);
-- Fresh execution IDs permit clock-independent trace reads. Partial indexes
-- exclude malformed legacy details before evaluating either JSON expression.
CREATE INDEX IF NOT EXISTS IX_audit_event_execution
ON audit_event (json_extract(DetailsJson, '$.executionId'), OccurredAtUtc, EventId)
WHERE json_valid(DetailsJson);
CREATE INDEX IF NOT EXISTS IX_audit_event_parent_execution
ON audit_event (json_extract(DetailsJson, '$.parentExecutionId'), OccurredAtUtc, EventId)
WHERE json_valid(DetailsJson);
""";
cmd.ExecuteNonQuery();
}
@@ -859,7 +868,7 @@ public class SqliteAuditWriter : IAuditWriter, ISiteAuditQueue, IAsyncDisposable
/// <inheritdoc />
public Task<IReadOnlyList<AuditEvent>> ReadByExecutionIdsAsync(
IReadOnlyCollection<Guid> executionIds, DateTime sinceUtc, int limit, CancellationToken ct = default)
IReadOnlyCollection<Guid> executionIds, DateTime sinceUtc, int limit, CancellationToken ct = default, string? afterEventId = null)
{
ArgumentNullException.ThrowIfNull(executionIds);
if (limit <= 0)
@@ -895,18 +904,10 @@ public class SqliteAuditWriter : IAuditWriter, ISiteAuditQueue, IAsyncDisposable
}
var inList = string.Join(", ", names);
cmd.CommandText = $"""
SELECT EventId, OccurredAtUtc, Actor, Action, Outcome,
Category, Target, SourceNode, CorrelationId, DetailsJson
FROM audit_event
WHERE OccurredAtUtc >= $since
AND (json_extract(DetailsJson, '$.executionId') IN ({inList})
OR json_extract(DetailsJson, '$.parentExecutionId') IN ({inList}))
ORDER BY OccurredAtUtc ASC, EventId ASC
LIMIT $limit;
""";
cmd.CommandText = BuildTraceReadSql(inList);
cmd.Parameters.AddWithValue("$since", EnsureUtc(sinceUtc).ToString(
"o", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("$after", (object?)afterEventId ?? DBNull.Value);
cmd.Parameters.AddWithValue("$limit", limit);
var rows = new List<AuditEvent>(Math.Min(limit, 256));
@@ -919,6 +920,33 @@ public class SqliteAuditWriter : IAuditWriter, ISiteAuditQueue, IAsyncDisposable
}
}
/// <summary>
/// Reads matching keys through both expression indexes before loading the bounded payload page.
/// UNION deduplicates rows matching both IDs. Keeping payloads outside the union bounds sorting
/// to IDs/timestamps and avoids the optimizer preferring a whole-store timestamp scan.
/// </summary>
internal static string BuildTraceReadSql(string inList) => $"""
SELECT e.EventId, e.OccurredAtUtc, e.Actor, e.Action, e.Outcome,
e.Category, e.Target, e.SourceNode, e.CorrelationId, e.DetailsJson
FROM audit_event AS e
JOIN (
SELECT EventId, OccurredAtUtc
FROM audit_event INDEXED BY IX_audit_event_execution
WHERE json_valid(DetailsJson)
AND json_extract(DetailsJson, '$.executionId') IN ({inList})
AND (OccurredAtUtc > $since OR (OccurredAtUtc = $since AND ($after IS NULL OR EventId > $after)))
UNION
SELECT EventId, OccurredAtUtc
FROM audit_event INDEXED BY IX_audit_event_parent_execution
WHERE json_valid(DetailsJson)
AND json_extract(DetailsJson, '$.parentExecutionId') IN ({inList})
AND (OccurredAtUtc > $since OR (OccurredAtUtc = $since AND ($after IS NULL OR EventId > $after)))
ORDER BY OccurredAtUtc, EventId
LIMIT $limit
) AS matched ON matched.EventId = e.EventId
ORDER BY matched.OccurredAtUtc, matched.EventId;
""";
/// <summary>
/// Highest <c>audit_forward_state.rowid</c> this instance has ever SERVED from
/// <see cref="ReadPendingSinceAsync"/> — i.e. the insertion-order high-water mark of