part #2
This commit is contained in:
8
src/__Libraries/StellaOps.Replay/Engine/IFeedLoader.cs
Normal file
8
src/__Libraries/StellaOps.Replay/Engine/IFeedLoader.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public interface IFeedLoader
|
||||
{
|
||||
Task<FeedSnapshot> LoadByDigestAsync(string digest, CancellationToken ct = default);
|
||||
}
|
||||
8
src/__Libraries/StellaOps.Replay/Engine/IPolicyLoader.cs
Normal file
8
src/__Libraries/StellaOps.Replay/Engine/IPolicyLoader.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public interface IPolicyLoader
|
||||
{
|
||||
Task<PolicySnapshot> LoadByDigestAsync(string digest, CancellationToken ct = default);
|
||||
}
|
||||
10
src/__Libraries/StellaOps.Replay/Engine/IReplayEngine.cs
Normal file
10
src/__Libraries/StellaOps.Replay/Engine/IReplayEngine.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using StellaOps.Replay.Models;
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public interface IReplayEngine
|
||||
{
|
||||
Task<ReplayResult> ReplayAsync(RunManifest manifest, ReplayOptions options, CancellationToken ct = default);
|
||||
DeterminismCheckResult CheckDeterminism(ReplayResult a, ReplayResult b);
|
||||
}
|
||||
10
src/__Libraries/StellaOps.Replay/Engine/IScanner.cs
Normal file
10
src/__Libraries/StellaOps.Replay/Engine/IScanner.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using StellaOps.Replay.Models;
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public interface IScanner
|
||||
{
|
||||
Task<ScanResult> ScanAsync(ImmutableArray<ArtifactDigest> artifacts, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using StellaOps.Replay.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public interface IScannerFactory
|
||||
{
|
||||
IScanner Create(ScannerOptions options);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using StellaOps.Replay.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public sealed partial class ReplayEngine
|
||||
{
|
||||
public DeterminismCheckResult CheckDeterminism(ReplayResult a, ReplayResult b)
|
||||
{
|
||||
if (a.VerdictDigest == b.VerdictDigest)
|
||||
{
|
||||
return new DeterminismCheckResult
|
||||
{
|
||||
IsDeterministic = true,
|
||||
DigestA = a.VerdictDigest,
|
||||
DigestB = b.VerdictDigest,
|
||||
Differences = []
|
||||
};
|
||||
}
|
||||
|
||||
var differences = FindJsonDifferences(a.VerdictJson, b.VerdictJson);
|
||||
return new DeterminismCheckResult
|
||||
{
|
||||
IsDeterministic = false,
|
||||
DigestA = a.VerdictDigest,
|
||||
DigestB = b.VerdictDigest,
|
||||
Differences = differences
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using StellaOps.Canonicalization.Verification;
|
||||
using StellaOps.Replay.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public sealed partial class ReplayEngine
|
||||
{
|
||||
private static IReadOnlyList<JsonDifference> FindJsonDifferences(string? a, string? b)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
return [new JsonDifference("$", "One or both values are null")];
|
||||
|
||||
var verifier = new DeterminismVerifier();
|
||||
var result = verifier.Compare(a, b);
|
||||
return result.Differences.Select(d => new JsonDifference(d, "Value mismatch")).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using StellaOps.Replay.Models;
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public sealed partial class ReplayEngine
|
||||
{
|
||||
private async Task<LoadResult<FeedSnapshot>> LoadFeedSnapshotAsync(
|
||||
FeedSnapshot snapshot,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var feed = await _feedLoader.LoadByDigestAsync(snapshot.Digest, ct).ConfigureAwait(false);
|
||||
if (!string.Equals(feed.Digest, snapshot.Digest, StringComparison.OrdinalIgnoreCase))
|
||||
return LoadResult<FeedSnapshot>.Fail($"Feed digest mismatch: expected {snapshot.Digest}");
|
||||
return LoadResult<FeedSnapshot>.Ok(feed);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return LoadResult<FeedSnapshot>.Fail($"Failed to load feed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<LoadResult<PolicySnapshot>> LoadPolicySnapshotAsync(
|
||||
PolicySnapshot snapshot,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var policy = await _policyLoader.LoadByDigestAsync(snapshot.LatticeRulesDigest, ct)
|
||||
.ConfigureAwait(false);
|
||||
return LoadResult<PolicySnapshot>.Ok(policy);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return LoadResult<PolicySnapshot>.Fail($"Failed to load policy: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using StellaOps.Replay.Models;
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
public sealed partial class ReplayEngine
|
||||
{
|
||||
private static ValidationResult ValidateManifest(RunManifest manifest)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(manifest.RunId))
|
||||
errors.Add("RunId is required");
|
||||
|
||||
if (manifest.ArtifactDigests.Length == 0)
|
||||
errors.Add("At least one artifact digest required");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(manifest.FeedSnapshot.Digest))
|
||||
errors.Add("Feed snapshot digest required");
|
||||
|
||||
return new ValidationResult(errors.Count == 0, errors);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Canonicalization.Json;
|
||||
using StellaOps.Canonicalization.Verification;
|
||||
using StellaOps.Replay.Models;
|
||||
using StellaOps.Testing.Manifests.Models;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace StellaOps.Replay.Engine;
|
||||
|
||||
@@ -12,7 +10,7 @@ namespace StellaOps.Replay.Engine;
|
||||
/// Executes scans deterministically from run manifests.
|
||||
/// Enables time-travel replay for verification and auditing.
|
||||
/// </summary>
|
||||
public sealed class ReplayEngine : IReplayEngine
|
||||
public sealed partial class ReplayEngine : IReplayEngine
|
||||
{
|
||||
private readonly IFeedLoader _feedLoader;
|
||||
private readonly IPolicyLoader _policyLoader;
|
||||
@@ -44,16 +42,32 @@ public sealed class ReplayEngine : IReplayEngine
|
||||
var validationResult = ValidateManifest(manifest);
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return ReplayResult.Failed(manifest.RunId, "Manifest validation failed", validationResult.Errors);
|
||||
return ReplayResult.Failed(
|
||||
manifest.RunId,
|
||||
"Manifest validation failed",
|
||||
validationResult.Errors,
|
||||
_timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
var feedResult = await LoadFeedSnapshotAsync(manifest.FeedSnapshot, ct).ConfigureAwait(false);
|
||||
if (!feedResult.Success)
|
||||
return ReplayResult.Failed(manifest.RunId, "Failed to load feed snapshot", [feedResult.Error ?? "Unknown error"]);
|
||||
{
|
||||
return ReplayResult.Failed(
|
||||
manifest.RunId,
|
||||
"Failed to load feed snapshot",
|
||||
[feedResult.Error ?? "Unknown error"],
|
||||
_timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
var policyResult = await LoadPolicySnapshotAsync(manifest.PolicySnapshot, ct).ConfigureAwait(false);
|
||||
if (!policyResult.Success)
|
||||
return ReplayResult.Failed(manifest.RunId, "Failed to load policy snapshot", [policyResult.Error ?? "Unknown error"]);
|
||||
{
|
||||
return ReplayResult.Failed(
|
||||
manifest.RunId,
|
||||
"Failed to load policy snapshot",
|
||||
[policyResult.Error ?? "Unknown error"],
|
||||
_timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
var scannerOptions = new ScannerOptions
|
||||
{
|
||||
@@ -81,109 +95,4 @@ public sealed class ReplayEngine : IReplayEngine
|
||||
DurationMs = scanResult.DurationMs
|
||||
};
|
||||
}
|
||||
|
||||
public DeterminismCheckResult CheckDeterminism(ReplayResult a, ReplayResult b)
|
||||
{
|
||||
if (a.VerdictDigest == b.VerdictDigest)
|
||||
{
|
||||
return new DeterminismCheckResult
|
||||
{
|
||||
IsDeterministic = true,
|
||||
DigestA = a.VerdictDigest,
|
||||
DigestB = b.VerdictDigest,
|
||||
Differences = []
|
||||
};
|
||||
}
|
||||
|
||||
var differences = FindJsonDifferences(a.VerdictJson, b.VerdictJson);
|
||||
return new DeterminismCheckResult
|
||||
{
|
||||
IsDeterministic = false,
|
||||
DigestA = a.VerdictDigest,
|
||||
DigestB = b.VerdictDigest,
|
||||
Differences = differences
|
||||
};
|
||||
}
|
||||
|
||||
private static ValidationResult ValidateManifest(RunManifest manifest)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(manifest.RunId))
|
||||
errors.Add("RunId is required");
|
||||
|
||||
if (manifest.ArtifactDigests.Length == 0)
|
||||
errors.Add("At least one artifact digest required");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(manifest.FeedSnapshot.Digest))
|
||||
errors.Add("Feed snapshot digest required");
|
||||
|
||||
return new ValidationResult(errors.Count == 0, errors);
|
||||
}
|
||||
|
||||
private async Task<LoadResult<FeedSnapshot>> LoadFeedSnapshotAsync(
|
||||
FeedSnapshot snapshot, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var feed = await _feedLoader.LoadByDigestAsync(snapshot.Digest, ct).ConfigureAwait(false);
|
||||
if (!string.Equals(feed.Digest, snapshot.Digest, StringComparison.OrdinalIgnoreCase))
|
||||
return LoadResult<FeedSnapshot>.Fail($"Feed digest mismatch: expected {snapshot.Digest}");
|
||||
return LoadResult<FeedSnapshot>.Ok(feed);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return LoadResult<FeedSnapshot>.Fail($"Failed to load feed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<LoadResult<PolicySnapshot>> LoadPolicySnapshotAsync(
|
||||
PolicySnapshot snapshot, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var policy = await _policyLoader.LoadByDigestAsync(snapshot.LatticeRulesDigest, ct).ConfigureAwait(false);
|
||||
return LoadResult<PolicySnapshot>.Ok(policy);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return LoadResult<PolicySnapshot>.Fail($"Failed to load policy: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static IReadOnlyList<JsonDifference> FindJsonDifferences(string? a, string? b)
|
||||
{
|
||||
if (a is null || b is null)
|
||||
return [new JsonDifference("$", "One or both values are null")];
|
||||
|
||||
var verifier = new DeterminismVerifier();
|
||||
var result = verifier.Compare(a, b);
|
||||
return result.Differences.Select(d => new JsonDifference(d, "Value mismatch")).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IReplayEngine
|
||||
{
|
||||
Task<ReplayResult> ReplayAsync(RunManifest manifest, ReplayOptions options, CancellationToken ct = default);
|
||||
DeterminismCheckResult CheckDeterminism(ReplayResult a, ReplayResult b);
|
||||
}
|
||||
|
||||
public interface IScannerFactory
|
||||
{
|
||||
IScanner Create(ScannerOptions options);
|
||||
}
|
||||
|
||||
public interface IScanner
|
||||
{
|
||||
Task<ScanResult> ScanAsync(ImmutableArray<ArtifactDigest> artifacts, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public interface IFeedLoader
|
||||
{
|
||||
Task<FeedSnapshot> LoadByDigestAsync(string digest, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public interface IPolicyLoader
|
||||
{
|
||||
Task<PolicySnapshot> LoadByDigestAsync(string digest, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user