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
222 changes: 222 additions & 0 deletions tests/Reactor.Tests/Tooling/ReviewerManifestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Xunit;

namespace Microsoft.UI.Reactor.Tests.Tooling;

/// <summary>
/// Gate on <c>tools/reviewer/manifest.json</c>: every path it lists must resolve to a real file.
/// </summary>
/// <remarks>
/// <para>
/// The reviewer never opens these files itself — <c>run-review.ps1</c> interpolates the list into
/// an agent prompt and asks the agent to read them — so a path that resolves to nothing is
/// silently unreviewed code rather than a crash. Nothing fails, and the batch still reports the
/// file as reviewed.
/// </para>
/// <para>
/// It rots continuously, from ordinary file movement between the rare occasions anyone edits the
/// manifest, not from any single bad commit. When this gate was added 147 of 633 entries pointed
/// at nothing and the reporting counted all 633 as reviewed; that had gone unnoticed for months.
/// A one-time path fix would not have prevented the next drift, which is the whole reason this
/// test exists.
/// </para>
/// <para>
/// <c>run-review.ps1</c> deliberately does not fail fast on a stale path — aborting a 91-batch LLM
/// run over one bad entry would trade a reporting defect for an availability one. Blocking is
/// cheap here and expensive there, so the strict check lives in CI and the run reports a shortfall.
/// </para>
/// </remarks>
public class ReviewerManifestTests
{
const string ManifestRelativePath = "tools/reviewer/manifest.json";

readonly record struct Entry(string BatchId, string Path);

static string ManifestPath() => global::System.IO.Path.Join(GallerySources.RepoRoot(), "tools", "reviewer", "manifest.json");

static IReadOnlyList<(string Id, string[] Files)> Batches()
{
var path = ManifestPath();
Assert.True(File.Exists(path), $"reviewer manifest not found at {path}");

using var doc = JsonDocument.Parse(File.ReadAllText(path));
Assert.True(
doc.RootElement.TryGetProperty("batches", out var batches),
$"{ManifestRelativePath} has no 'batches' property — the manifest shape changed and this gate needs updating.");
// Checked explicitly so a shape change reports this message rather than an
// InvalidOperationException from EnumerateArray().
Assert.True(
batches.ValueKind == JsonValueKind.Array,
$"{ManifestRelativePath} has a 'batches' property of kind {batches.ValueKind}, expected an array — the manifest shape changed and this gate needs updating.");

return batches.EnumerateArray().Select(ReadBatch).ToList();
}

/// <summary>
/// Reads one batch, validating its shape so a manifest change reports what is wrong rather
/// than surfacing a bare <c>KeyNotFoundException</c> from <c>GetProperty</c> or an
/// <c>InvalidOperationException</c> from <c>EnumerateArray</c>. This gate's whole value is
/// telling the next person what to fix; an unexplained exception in CI does not.
/// </summary>
static (string Id, string[] Files) ReadBatch(JsonElement batch, int index)
{
Assert.True(
batch.ValueKind == JsonValueKind.Object,
$"{ManifestRelativePath} batch at index {index} is a {batch.ValueKind}, expected an object — the manifest shape changed and this gate needs updating.");

Assert.True(
batch.TryGetProperty("id", out var id) && id.ValueKind == JsonValueKind.String,
$"{ManifestRelativePath} batch at index {index} has no string 'id' — the manifest shape changed and this gate needs updating.");

var batchId = id.GetString()!;

Assert.True(
batch.TryGetProperty("files", out var files) && files.ValueKind == JsonValueKind.Array,
$"{ManifestRelativePath} batch '{batchId}' has no 'files' array — the manifest shape changed and this gate needs updating.");

var paths = new string[files.GetArrayLength()];
var i = 0;
foreach (var file in files.EnumerateArray())
{
Assert.True(
file.ValueKind == JsonValueKind.String,
$"{ManifestRelativePath} batch '{batchId}' has a non-string entry at index {i} (kind {file.ValueKind}) — every file entry must be a path string.");
paths[i++] = file.GetString()!;
}

return (batchId, paths);
}

static IReadOnlyList<Entry> Entries() =>
Batches().SelectMany(b => b.Files.Select(f => new Entry(b.Id, f))).ToList();

/// <summary>
/// Guards the other tests in this class against passing vacuously. Each one asserts "no
/// offenders in this collection", which an empty collection satisfies for free — so if the
/// manifest ever fails to load, or its shape changes such that no entries are read, the
/// coverage checks would go green while checking nothing at all.
/// </summary>
[Fact]
public void ManifestLoadsAndIsNonTrivial()
{
var batches = Batches();
var entryCount = batches.Sum(b => b.Files.Length);

Assert.True(batches.Count >= 50, $"expected the reviewer manifest to define many batches, found {batches.Count}");
Assert.True(entryCount >= 400, $"expected the reviewer manifest to list many entries, found {entryCount}");
}

[Fact]
public void EveryManifestPathResolvesFromRepoRoot()
{
var root = GallerySources.RepoRoot();

var missing = Entries()
.Where(e => !File.Exists(global::System.IO.Path.Join(root, e.Path)))
.ToList();

Assert.True(missing.Count == 0, BuildMissingMessage(missing));
}

static string BuildMissingMessage(IReadOnlyList<Entry> missing)
{
var lines = missing.Select(m => $" {m.BatchId}: {m.Path}");
return $"""
{missing.Count} path(s) in {ManifestRelativePath} do not resolve to a file in the repo.

The reviewer hands these to an agent that cannot open them, and still counts them as
reviewed — so this is unreviewed code, not just a bad path. Retarget each one to where
the file lives now, or drop it if the file is genuinely gone.

Paths are relative to the repo root (note: NOT to src/).

{string.Join("\n", lines)}
""";
}

[Fact]
public void NoBatchIsEmpty()
{
var empty = Batches().Where(b => b.Files.Length == 0).Select(b => b.Id).ToList();

Assert.True(
empty.Count == 0,
$"""
{empty.Count} batch(es) in {ManifestRelativePath} list no files: {string.Join(", ", empty)}

An empty batch still spawns an agent and still produces a report, so it burns a run and
reports zero findings as though the code were clean. Give the batch files or remove it.
""");
}

[Fact]
public void NoBatchListsTheSamePathTwice()
{
var dupes = Batches()
.SelectMany(b => b.Files
.GroupBy(f => f)
.Where(g => g.Count() > 1)
.Select(g => $" {b.Id}: {g.Key} (x{g.Count()})"))
.ToList();

Assert.True(
dupes.Count == 0,
$"""
{ManifestRelativePath} lists the same path more than once within a batch:

{string.Join("\n", dupes)}

A duplicate inflates the reviewed count and asks the agent to read the same file twice.
(The same path appearing in *different* batches is intentional — several agents review
the same file from different angles.)
""");
}

/// <summary>
/// Keeps the single-convention invariant enforceable. The manifest previously mixed
/// repo-root-relative and <c>src/</c>-relative paths, which made any single-base audit report
/// hundreds of false misses, and would force this gate into a two-base resolver that silently
/// accepts a wrong path whenever it happens to resolve under the other base.
/// </summary>
/// <remarks>
/// Rooted paths are rejected here for the sake of the diagnosis, not because they can escape
/// the repo root. This gate composes with <c>Path.Join</c>, which concatenates rather than
/// letting a rooted second argument win, so <c>C:/x.cs</c> already fails <c>File.Exists</c>
/// and <see cref="EveryManifestPathResolvesFromRepoRoot"/> catches it. <c>Path.Combine</c>
/// would let it win — this gate deliberately does not use it, so don't "harden" it by
/// switching. Rejecting rooted paths here only means the failure names the real defect, a
/// malformed path, instead of reporting a missing file.
/// </remarks>
[Fact]
public void PathsUseRepoRootRelativeForwardSlashForm()
{
var malformed = Entries()
.Where(e => e.Path.Contains('\\')
|| global::System.IO.Path.IsPathRooted(e.Path)
|| e.Path.StartsWith("./")
|| HasTraversalSegment(e.Path))
.Select(e => $" {e.BatchId}: {e.Path}")
.ToList();

Assert.True(
malformed.Count == 0,
$"""
{ManifestRelativePath} contains path(s) that are not plain repo-root-relative,
forward-slash form:

{string.Join("\n", malformed)}
""");
}

/// <summary>
/// True when <c>..</c> appears as a whole path segment, i.e. actual parent-directory
/// traversal. Deliberately not a substring test: a file legitimately named
/// <c>foo..bar.cs</c> is not traversal, and flagging it would be a false positive
/// against a rule whose entire purpose is to keep paths anchored to the repo root.
/// </summary>
static bool HasTraversalSegment(string path) =>
path.Split('/').Any(segment => segment == "..");
}
41 changes: 32 additions & 9 deletions tools/reviewer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ Each agent:
4. Applies the Team Lead Test: only emit findings a senior lead would keep
5. Cites pattern IDs from skill catalogs for every finding

### Coverage reporting

The orchestrator does not open the batch files itself — it lists them in the
agent's prompt. So before each batch it resolves every manifest path against the
repo root, prompts with only the files that exist, and writes

```
**Files reviewed**: 7 of 8
```

naming any path that did not resolve. A stale path is reported as a shortfall
rather than aborting the run, because killing 91 batches over one bad entry
would trade a reporting problem for an availability one. `ReviewerManifestTests`
is the strict check; it fails the build instead.

### Phase 2: General Review (after specialists)

The general agent runs after specialists complete. It:
Expand Down Expand Up @@ -122,7 +137,7 @@ Decisions are saved directly into `fix-list.md`. The file can also be edited man

```markdown
## F001
- **File**: Reactor/Core/Reconciler.cs:145-167
- **File**: src/Reactor/Core/Reconciler.cs:145-167
- **Severity**: high
- **Priority**: P1
- **Domain**: concurrency
Expand Down Expand Up @@ -159,23 +174,31 @@ The implementation agent:

## Scope

**Included** (485 files across 91 batches):
- `Reactor/` — Core framework (reconciler, elements, hosting, flex, yoga, animation, markdown, property grid)
- `Reactor.Cli/` — CLI tool
- `Reactor.Localization.Generator/` — Source generator
- `ReactorCharting/` — D3 visualization library
- `vscode-reactor/` — VS Code extension
**Included** (477 files across 91 batches, 630 entries — a file may appear in
several batches so different agents can review it from different angles):
- `src/Reactor/` — Core framework (reconciler, elements, hosting, flex, yoga, animation, property grid)
- `src/Reactor.Advanced/` — Charting/D3 and Markdown
- `src/Reactor.Cli/` — CLI tool
- `src/Reactor.Devtools/` — Devtools and preview server
- `src/Reactor.Localization.Generator/` — Source generator
- `src/vscode-reactor/src/` — VS Code extension sources
- `tests/` — All test projects (reviewed for quality, not just correctness)
- Build config (`Directory.Build.props`, `*.csproj`, `*.sln`)
- Build config (`Directory.Build.props`, `*.csproj`, `Reactor.slnx`)

**Excluded**:
- `samples/` — Sample apps
- `docs/` — Documentation markdown
- `selfhost/` — Runtime config files
- `tests/Reactor.Tests/YogaGenerated/` — Auto-generated upstream test fixtures
- `tests/Reactor.Tests/Md4cGenerated/` — Auto-generated CommonMark spec tests
- Binary/image/config files

The manifest is hand-maintained: there is no generator, because the batch and
agent assignments encode a judgement about which files are worth reviewing
*together*, which a directory glob cannot reproduce. It is gated by
`ReviewerManifestTests` in `tests/Reactor.Tests/Tooling/`, which fails the build
if any listed path stops resolving — the manifest previously drifted to 147 dead
paths out of 633 without anything noticing.

## Advanced Usage

```powershell
Expand Down
Loading