Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Text.Json;

namespace UnskipClosedTests.Tool;

internal static partial class ApplyEngine
{
private static List<string> ReadAgentSelection(string path, string expectedManifestDigest)
{
using JsonDocument document = JsonSupport.ReadDocument(path);
JsonElement root = document.RootElement;
List<JsonElement> items = [];
if (root.ValueKind == JsonValueKind.Array)
{
items.AddRange(root.EnumerateArray());
}
else if (root.ValueKind == JsonValueKind.Object)
{
JsonElement collection;
if (root.TryGetProperty("items", out collection) ||
root.TryGetProperty("outputs", out collection) ||
root.TryGetProperty("safe_outputs", out collection))
{
foreach (JsonProperty property in root.EnumerateObject())
{
if (property.Name is not ("items" or "outputs" or "safe_outputs"))
{
throw new ContractException($"Unknown agent output root field '{property.Name}'.");
}
}

if (collection.ValueKind != JsonValueKind.Array)
{
throw new ContractException("Agent output item collection must be an array.");
}

items.AddRange(collection.EnumerateArray());
}
else if (root.TryGetProperty("type", out _))
{
items.Add(root);
}
else
{
throw new ContractException("Agent output must contain an item array.");
}
}
else
{
throw new ContractException("Agent output root must be an object or array.");
}

if (items.Count != 1 || items[0].ValueKind != JsonValueKind.Object)
{
throw new ContractException("Agent output must contain exactly one output item.");
}

JsonElement item = items[0];
HashSet<string> allowed = ["type", "manifest_digest", "candidate_ids_json"];
foreach (JsonProperty property in item.EnumerateObject())
{
if (!allowed.Contains(property.Name))
{
throw new ContractException($"Unknown apply_verified_unskips field '{property.Name}'.");
}
}

string type = RequiredString(item, "type");
if (type != "apply_verified_unskips")
{
throw new ContractException($"Unexpected output item type '{type}'.");
}

string manifestDigest = RequiredString(item, "manifest_digest");
if (!string.Equals(manifestDigest, expectedManifestDigest, StringComparison.Ordinal))
{
throw new ContractException("Agent output manifest_digest does not match the resolved manifest.");
}

if (!item.TryGetProperty("candidate_ids_json", out JsonElement idsElement))
{
throw new ContractException("candidate_ids_json is required.");
}

JsonElement array;
JsonDocument? parsedString = null;
if (idsElement.ValueKind == JsonValueKind.String)
{
try
{
parsedString = JsonDocument.Parse(idsElement.GetString()!);
array = parsedString.RootElement;
}
catch (JsonException ex)
{
throw new ContractException($"candidate_ids_json string is malformed JSON: {ex.Message}");
}
}
else
{
array = idsElement;
}

try
{
if (array.ValueKind != JsonValueKind.Array)
{
throw new ContractException("candidate_ids_json must be a JSON array.");
}

List<string> ids = [];
foreach (JsonElement id in array.EnumerateArray())
{
if (id.ValueKind != JsonValueKind.String || string.IsNullOrWhiteSpace(id.GetString()))
{
throw new ContractException("candidate_ids_json must contain only non-empty strings.");
}

ids.Add(id.GetString()!);
}

if (ids.Distinct(StringComparer.Ordinal).Count() != ids.Count)
{
throw new ContractException("candidate_ids_json contains duplicate candidate IDs.");
}

return ids;
}
finally
{
parsedString?.Dispose();
}
}

private static string RequiredString(JsonElement element, string name)
{
if (!element.TryGetProperty(name, out JsonElement value) || value.ValueKind != JsonValueKind.String)
{
throw new ContractException($"{name} must be a string.");
}

return value.GetString()!;
}
}
113 changes: 113 additions & 0 deletions .github/workflows/unskip-closed-tests-tool/ApplyEngine.Edits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace UnskipClosedTests.Tool;

internal static partial class ApplyEngine
{
private static SourceEdit CreateEdit(GitRepository repository, ToolConfig config, Candidate candidate)
{
if (PathRules.MatchesAnyGlob(candidate.Path, config.ExcludedGlobs.Concat(config.GeneratedGlobs)))
{
throw new ContractException($"Candidate path '{candidate.Path}' is excluded or generated.");
}

string fullPath = PathRules.ResolveInsideRoot(repository.Root, candidate.Path, "candidate path");
PathRules.RejectReparsePoints(repository.Root, fullPath);
byte[] bytes = ReadBytes(fullPath);
if (!string.Equals(JsonSupport.Sha256(bytes), candidate.SourceSha256, StringComparison.Ordinal))
{
throw new ContractException($"Candidate path '{candidate.Path}' source hash is stale.");
}

string text = DecodeUtf8(bytes, out _);
SyntaxTree tree = CSharpSyntaxTree.ParseText(text, path: candidate.Path);
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
List<AttributeSyntax> matches = root.DescendantNodes().OfType<AttributeSyntax>()
.Where(attribute => attribute.SpanStart == candidate.AttributeSpan.Start &&
attribute.Span.Length == candidate.AttributeSpan.Length)
.ToList();
if (matches.Count != 1 ||
!InventoryEngine.AttributeMatches(matches[0], config.IgnoreAttributeNames) ||
!string.Equals(
JsonSupport.Sha256(text.Substring(matches[0].SpanStart, matches[0].Span.Length)),
candidate.AttributeTextSha256,
StringComparison.Ordinal))
{
throw new ContractException($"Candidate '{candidate.CandidateId}' attribute anchor is stale or fabricated.");
}

AttributeSyntax attribute = matches[0];
AttributeListSyntax list = attribute.Parent as AttributeListSyntax
?? throw new ContractException($"Candidate '{candidate.CandidateId}' attribute is not in an attribute list.");
int index = list.Attributes.IndexOf(attribute);
TextSpan removal;
if (list.Attributes.Count == 1)
{
removal = list.Span;
}
else if (index < list.Attributes.Count - 1)
{
removal = TextSpan.FromBounds(attribute.SpanStart, list.Attributes[index + 1].SpanStart);
}
else
{
removal = TextSpan.FromBounds(list.Attributes[index - 1].Span.End, attribute.Span.End);
}

return new SourceEdit(candidate.Path, removal.Start, removal.Length, candidate);
}

private static void RejectOverlappingEdits(List<SourceEdit> edits)
{
foreach (IGrouping<string, SourceEdit> group in edits.GroupBy(static edit => edit.Path, StringComparer.Ordinal))
{
SourceEdit? previous = null;
foreach (SourceEdit edit in group.OrderBy(static item => item.Start))
{
if (previous is not null && edit.Start < previous.Start + previous.Length)
{
throw new ContractException(
$"Candidate edits '{previous.Candidate.CandidateId}' and '{edit.Candidate.CandidateId}' overlap.");
}

previous = edit;
}
}
}

private static byte[] ApplyTextEdit(byte[] sourceBytes, int start, int length)
{
string text = DecodeUtf8(sourceBytes, out bool bom);
if (start < 0 || length <= 0 || start + length > text.Length)
{
throw new ContractException("Candidate edit span is outside the current source.");
}

string edited = text.Remove(start, length);
byte[] content = new UTF8Encoding(false, true).GetBytes(edited);
if (!bom)
{
return content;
}

return [0xEF, 0xBB, 0xBF, .. content];
}

private static string DecodeUtf8(byte[] bytes, out bool bom)
{
bom = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF;
ReadOnlySpan<byte> content = bom ? bytes.AsSpan(3) : bytes;
try
{
return new UTF8Encoding(false, true).GetString(content);
}
catch (DecoderFallbackException ex)
{
throw new ContractException($"Candidate source is not valid UTF-8: {ex.Message}");
}
}
}
87 changes: 87 additions & 0 deletions .github/workflows/unskip-closed-tests-tool/ApplyEngine.FileIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
namespace UnskipClosedTests.Tool;

internal static partial class ApplyEngine
{
private static string? RestoreUnexpectedSourceMutations(
GitRepository repository,
IReadOnlyDictionary<string, byte[]> expectedBytes)
{
string? firstMutation = null;
foreach ((string path, byte[] expected) in expectedBytes)
{
string fullPath = PathRules.ResolveInsideRoot(repository.Root, path, "candidate path");
byte[] actual = ReadBytes(fullPath);
if (!actual.AsSpan().SequenceEqual(expected))
{
firstMutation ??= path;
WriteBytes(fullPath, expected);
}
}

return firstMutation;
}

private static byte[] ReadBytes(string path)
{
try
{
return File.ReadAllBytes(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
throw new InfrastructureException($"Could not read '{path}'.", ex);
}
}

private static void WriteBytes(string path, byte[] bytes)
{
try
{
File.WriteAllBytes(path, bytes);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
throw new InfrastructureException($"Could not write '{path}'.", ex);
}
}

private static void RestoreFiles(GitRepository repository, IReadOnlyDictionary<string, byte[]> files)
{
foreach ((string path, byte[] bytes) in files)
{
WriteBytes(PathRules.ResolveInsideRoot(repository.Root, path, "candidate path"), bytes);
}
}

private static void ResetDirectory(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}

Directory.CreateDirectory(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
throw new InfrastructureException($"Could not prepare verification directory '{path}'.", ex);
}
}

private static void TryDeleteDirectory(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Console.Error.WriteLine($"warning: could not remove verification directory '{path}': {ex.Message}");
}
}
}
Loading
Loading