From 7d21d545f280ed0e205b059d2a67cdbe4c56e2d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C4=B0rgin?= Date: Sat, 14 Nov 2026 22:48:26 +0300 Subject: [PATCH 1/2] ci: the merge-queue reuse decision is built and run from the base commit Addresses #826. Build and run the reuse judge in a separate base-commit worktree outside the workspace, using the base's global.json. Rule 5 now guards .github/ only. --- .github/workflows/ci.yml | 14 +++---- docs/engineering/quality-gates.md | 12 +++--- docs/engineering/workflow.md | 4 +- .../Commands/MergeGroupReuseCommandTests.cs | 36 ++++++++++------ .../RepositoryGates/CiBenchmarkGuardTests.cs | 42 +++++++++---------- .../RepositoryGates/CiMergeGroupReuseTests.cs | 35 ++++++++++++++++ .../RepositoryGates/Support/CiWorkflow.cs | 14 +++++++ .../Commands/MergeGroupReuseDecision.cs | 4 +- 8 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs create mode 100644 tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/Support/CiWorkflow.cs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e577997..2af750e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -120,7 +120,7 @@ jobs: uses: actions/checkout@v7 with: fetch-depth: 0 - - name: Guard against self-vouching before executing repository tooling + - name: Gather the group's facts id: guard env: QUEUE_REF: ${{ github.ref }} @@ -140,12 +140,9 @@ jobs: reason="unavailable guard facts" else gather base-tree git rev-parse "$QUEUE_BASE^{tree}" || true - shopt -s nocasematch - while IFS= read -r -d '' path; do - case "$path" in - .github/* | tools/* | Directory.Build.props | */Directory.Build.props | Directory.Build.targets | */Directory.Build.targets | Directory.Packages.props | */Directory.Packages.props | Directory.Build.rsp | */Directory.Build.rsp | global.json | */global.json | nuget.config | */nuget.config) reason="rule 5: guarded path" ;; - esac - done < "$facts/changed-paths" + if ! git worktree add --detach "$RUNNER_TEMP/judge" "$QUEUE_BASE"; then + reason="unavailable judge" + fi fi if [ -n "$reason" ]; then printf 'reuse=false\nreason=%s\nblocked=true\n' "$reason" >> "$GITHUB_OUTPUT" @@ -155,7 +152,7 @@ jobs: if: steps.guard.outputs.blocked != 'true' uses: actions/setup-dotnet@v6 with: - global-json-file: global.json + global-json-file: ${{ runner.temp }}/judge/global.json dotnet-version: 10.0.x - name: Gather GitHub facts id: facts @@ -183,6 +180,7 @@ jobs: - name: Decide reuse id: decide if: steps.guard.outputs.blocked != 'true' + working-directory: ${{ runner.temp }}/judge env: REUSED_RUN: ${{ steps.facts.outputs.link }} run: | diff --git a/docs/engineering/quality-gates.md b/docs/engineering/quality-gates.md index f9870aa5..c1b78ac9 100644 --- a/docs/engineering/quality-gates.md +++ b/docs/engineering/quality-gates.md @@ -79,12 +79,12 @@ attempt's successful `CI result` only with recorded proof of the exact same tree two parents, its second parent is still the queued PR's head, and that PR targets `master`. The attempt's unique, unexpired `tested-tree-` artifact must record the group's tree and the same base tree its diff uses, and have been created within that attempt's successful -`Changes` job window. The PR's changes -from its merge base must touch neither `.github/` nor `tools/`, nor any `Directory.Build.props`, -`Directory.Build.targets`, `Directory.Packages.props`, `Directory.Build.rsp`, `global.json` or -`nuget.config` at any depth, matched case-insensitively. The workflow checks this guard before executing repository tooling. -In practice, a PR whose base has not moved since its green run and which does not touch CI or the -tool avoids the second full run. Any missing, malformed or uncertain fact, or a failed reuse job, +`Changes` job window. The decision is built and run from the merge group's base commit, in a +separate worktree outside the workspace, with the base's own `global.json`, so the PR cannot change +its judge. The PR's changes from its merge base must not touch `.github/`, matched +case-insensitively, because the workflow itself always comes from the group's tree. +In practice, a PR whose base has not moved since its green run and which does not touch `.github/` +avoids the second full run. Any missing, malformed or uncertain fact, or a failed reuse job, means the normal full CI runs. `Changes` must still succeed; the reuse branch of `CI result` accepts only a merge group with all four heavy jobs skipped. diff --git a/docs/engineering/workflow.md b/docs/engineering/workflow.md index 7ddade24..6ba0c055 100644 --- a/docs/engineering/workflow.md +++ b/docs/engineering/workflow.md @@ -1,6 +1,6 @@ # Repository Workflow -Date: 2026-11-09 +Date: 2026-11-14 Canonical working agreements for research, implementation, temporary work, verification, and Git history. @@ -89,7 +89,7 @@ single-owner rules. `master` requires the queue and the `CI result` check, for administrators too. - A pull request joins the queue once the `CI result` check of its own head is green. The queue tests it again on top of current `master` and the entries ahead of it, and merges only on that - green, except when the recorded tested-tree proof and self-vouching guard in `quality-gates.md` + green, except when the recorded tested-tree proof and the base-commit judge in `quality-gates.md` permit reuse of its green PR result. Otherwise the repository guards count exactly over every test file, so a green result from an older base is not enough. After the merge, the work is done when the CI the merge commit triggers on diff --git a/tests/OpenCode.Sdk.Tools.Tests/Commands/MergeGroupReuseCommandTests.cs b/tests/OpenCode.Sdk.Tools.Tests/Commands/MergeGroupReuseCommandTests.cs index b67be7f1..b1f99802 100644 --- a/tests/OpenCode.Sdk.Tools.Tests/Commands/MergeGroupReuseCommandTests.cs +++ b/tests/OpenCode.Sdk.Tools.Tests/Commands/MergeGroupReuseCommandTests.cs @@ -32,18 +32,21 @@ public sealed class MergeGroupReuseCommandTests [Arguments("expired-artifact", "rule 4: unproven tree")] [Arguments("failed-changes", "rule 4: unproven tree")] [Arguments(".github/workflows/ci.yml", "rule 5: guarded path")] - [Arguments("tools/example.cs", "rule 5: guarded path")] - [Arguments("tools/Directory.Build.props", "rule 5: guarded path")] - [Arguments("global.json", "rule 5: guarded path")] - [Arguments("nested/Directory.Build.targets", "rule 5: guarded path")] - [Arguments("nested/Directory.Packages.props", "rule 5: guarded path")] - [Arguments("nested/Directory.Build.rsp", "rule 5: guarded path")] - [Arguments("nested/nuget.config", "rule 5: guarded path")] - [Arguments("nested/NuGet.Config", "rule 5: guarded path")] - [Arguments("nested/Global.json", "rule 5: guarded path")] - [Arguments("nested/directory.build.props", "rule 5: guarded path")] + [Arguments("tools/example.cs", "all rules hold")] + [Arguments("tools/Directory.Build.props", "all rules hold")] + [Arguments("global.json", "all rules hold")] + [Arguments("Directory.Packages.props", "all rules hold")] + [Arguments("nested/Directory.Build.targets", "all rules hold")] + [Arguments("nested/Directory.Packages.props", "all rules hold")] + [Arguments("nested/Directory.Build.rsp", "all rules hold")] + [Arguments("nested/nuget.config", "all rules hold")] + [Arguments("nested/NuGet.Config", "all rules hold")] + [Arguments("nested/Global.json", "all rules hold")] + [Arguments("nested/directory.build.props", "all rules hold")] [Arguments(".GITHUB/workflows/ci.yml", "rule 5: guarded path")] - [Arguments("TOOLS/example.cs", "rule 5: guarded path")] + [Arguments("TOOLS/example.cs", "all rules hold")] + [Arguments("tools/example.cs+different-tree", "rule 4: unproven tree")] + [Arguments("Directory.Packages.props+different-base-tree", "rule 4: unproven tree")] [Arguments("malformed", "malformed facts")] [Arguments("missing", "unreadable facts")] public async Task Execute_Should_Write_The_Decision_And_Reason(string row, string reason, CancellationToken cancellationToken) @@ -67,11 +70,18 @@ public async Task Execute_Should_Write_The_Decision_And_Reason(string row, strin await Assert.That(exitCode).IsEqualTo(0); await Assert.That(await fileSystem.File.ReadAllTextAsync("output", cancellationToken)).IsEqualTo( - $"reuse={(row == "all" ? "true" : "false")}\nreason={reason}\n"); + $"reuse={(reason == "all rules hold" ? "true" : "false")}\nreason={reason}\n"); } private static void ApplyRow(Dictionary facts, string row) { + var parts = row.Split('+'); + if (parts.Length == 2) + { + facts["changed-paths"] = parts[0] + '\0'; + row = parts[1]; + } + // These documents are built below with non-null arrays and members. var runs = JsonNode.Parse(facts["runs.json"])!["workflow_runs"]!.AsArray(); var jobs = JsonNode.Parse(facts["jobs.json"])!["jobs"]!.AsArray(); @@ -80,7 +90,7 @@ private static void ApplyRow(Dictionary facts, string row) facts["runs.json"] = new JsonObject { ["workflow_runs"] = runs.DeepClone() }.ToJsonString(); facts["jobs.json"] = new JsonObject { ["jobs"] = jobs.DeepClone() }.ToJsonString(); facts["artifacts.json"] = new JsonObject { ["artifacts"] = artifacts.DeepClone() }.ToJsonString(); - if (row.Contains('/', StringComparison.Ordinal) || row == "global.json") + if (row.Contains('/', StringComparison.Ordinal) || row is "global.json" or "Directory.Packages.props") { facts["changed-paths"] = row + '\0'; } diff --git a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiBenchmarkGuardTests.cs b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiBenchmarkGuardTests.cs index c5305082..5ca26429 100644 --- a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiBenchmarkGuardTests.cs +++ b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiBenchmarkGuardTests.cs @@ -1,4 +1,4 @@ -using System.Text.RegularExpressions; +using OpenCode.Sdk.Tools.Tests.RepositoryGates.Support; using OpenCode.Sdk.Tools.Tests.Support; using Testably.Abstractions; using TUnit.Assertions.Enums; @@ -13,11 +13,12 @@ public sealed class CiBenchmarkGuardTests [Test] public async Task Ci_Should_Keep_The_Ordered_Sdk_Dry_Commands() { - var workflow = await WorkflowAsync(); - await Assert.That(Job(workflow, "build-and-test")).DoesNotContain("--job Dry"); - await Assert.That(Job(workflow, "publish-nightly")).Contains("needs: [lint, build-and-test, benchmark-sdk]"); - await Assert.That(Job(workflow, "publish-nightly")).Contains("if: ${{ !cancelled() && github.ref == 'refs/heads/master' && github.event_name == 'push' && needs.lint.result == 'success' && needs.build-and-test.result == 'success' && needs.benchmark-sdk.result == 'success' }}"); - var matrix = Job(workflow, "benchmark-sdk"); + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + await Assert.That(CiWorkflow.Job(workflow, "build-and-test")).DoesNotContain("--job Dry"); + await Assert.That(CiWorkflow.Job(workflow, "publish-nightly")).Contains("needs: [lint, build-and-test, benchmark-sdk]"); + await Assert.That(CiWorkflow.Job(workflow, "publish-nightly")).Contains("if: ${{ !cancelled() && github.ref == 'refs/heads/master' && github.event_name == 'push' && needs.lint.result == 'success' && needs.build-and-test.result == 'success' && needs.benchmark-sdk.result == 'success' }}"); + var matrix = CiWorkflow.Job(workflow, "benchmark-sdk"); await Assert.That(matrix).Contains("needs: [changes, reuse]"); await Assert.That(matrix).Contains("if: ${{ !cancelled() && needs.changes.result == 'success' && needs.changes.outputs.benchmarks == 'true' && !(needs.reuse.result == 'success' && needs.reuse.outputs.reuse == 'true') }}"); await Assert.That(matrix).Contains("fail-fast: false"); @@ -42,7 +43,9 @@ await Assert.That(matrix.Split('\n').Select(static line => line.Trim()).Where(st [Test] public async Task Ci_Should_Run_Cold_Start_Once_On_The_Net10_Leg() { - var matrix = Job(await WorkflowAsync(), "benchmark-sdk"); + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + var matrix = CiWorkflow.Job(workflow, "benchmark-sdk"); await Assert.That(matrix).Contains(" - name: SDK cold start (net10)\n"); var start = matrix.IndexOf(" - name: SDK cold start (net10)\n", StringComparison.Ordinal); var end = matrix.IndexOf("\n - name: ", start + 1, StringComparison.Ordinal); @@ -57,7 +60,9 @@ public async Task Ci_Should_Run_Cold_Start_Once_On_The_Net10_Leg() [Test] public async Task Ci_Should_Run_Generator_Stages_In_Their_Own_Job_Without_Runtimes() { - var stage = Job(await WorkflowAsync(), "benchmark-generator-stage"); + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + var stage = CiWorkflow.Job(workflow, "benchmark-generator-stage"); await Assert.That(stage).Contains("needs: [changes, reuse]"); await Assert.That(stage).Contains("if: ${{ !cancelled() && needs.changes.result == 'success' && needs.changes.outputs.benchmarks-generator == 'true' && !(needs.reuse.result == 'success' && needs.reuse.outputs.reuse == 'true') }}"); await Assert.That(stage).Contains("- name: Generator stages\n"); @@ -79,7 +84,9 @@ await Assert.That(Commands(stage)).IsEquivalentTo([ [Test] public async Task Ci_Should_Bound_Every_Dry_Step() { - var timeouts = DrySteps(await WorkflowAsync()).Select(static step => + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + var timeouts = DrySteps(workflow).Select(static step => step.Split('\n').SingleOrDefault(static line => line.TrimStart().StartsWith("timeout-minutes:", StringComparison.Ordinal))?.Trim() ?? ""); await Assert.That(timeouts).IsEquivalentTo([ "timeout-minutes: 20", "timeout-minutes: 20", @@ -89,8 +96,9 @@ await Assert.That(timeouts).IsEquivalentTo([ [Test] public async Task Ci_Should_Require_The_Stage_Result_Only_When_Selected() { - var workflow = await WorkflowAsync(); - var changes = Job(workflow, "changes"); + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + var changes = CiWorkflow.Job(workflow, "changes"); await Assert.That(changes).Contains("benchmarks: ${{ steps.diff.outputs.benchmarks }}"); await Assert.That(changes).Contains("echo \"benchmarks=true\" >> \"$GITHUB_OUTPUT\""); await Assert.That(changes).Contains("src/* | tools/OpenCode.Sdk.Tools/* | tools/curation.json | tools/generation-profile.txt | spec/* | tests/OpenCode.Sdk.Performance.Tests/* | tests/Shared/BenchmarkExitPolicy.cs | tests/Shared/GeneratorPeakMemoryPolicy.cs | tests/Directory.Build.props | Directory.Build.props | Directory.Build.targets | Directory.Packages.props | global.json | nuget.config | .github/workflows/ci.yml) benchmarks=true ;;"); @@ -100,7 +108,7 @@ public async Task Ci_Should_Require_The_Stage_Result_Only_When_Selected() await Assert.That(changes).Contains("benchmarks_generator=false"); await Assert.That(changes).Contains(" tools/* | tests/OpenCode.Sdk.Tools.Tests/* | tests/OpenCode.Sdk.Performance.Tests/* | spec/* | tests/Shared/BenchmarkExitPolicy.cs | tests/Shared/GeneratorPeakMemoryPolicy.cs | tests/Directory.Build.props | Directory.Build.props | Directory.Build.targets | Directory.Packages.props | global.json | nuget.config | .github/workflows/ci.yml) benchmarks_generator=true ;;"); await Assert.That(changes).Contains("echo \"benchmarks-generator=$benchmarks_generator\" >> \"$GITHUB_OUTPUT\""); - var aggregate = Job(workflow, "ci-result"); + var aggregate = CiWorkflow.Job(workflow, "ci-result"); await Assert.That(aggregate).Contains("needs: [changes, reuse, lint, build-and-test, benchmark-sdk, benchmark-generator-stage]"); await Assert.That(aggregate).Contains("SDK_BENCHMARKS: ${{ needs.changes.outputs.benchmarks }}"); await Assert.That(aggregate).Contains("SDK: ${{ needs.benchmark-sdk.result }}"); @@ -124,16 +132,6 @@ await Assert.That(aggregate).Contains(""" """); } - private static async Task WorkflowAsync() - { - var fileSystem = new RealFileSystem(); - return (await fileSystem.File.ReadAllTextAsync(fileSystem.Path.Combine(new RepositoryRoot(fileSystem).Locate(), ".github", "workflows", "ci.yml"))) - .ReplaceLineEndings("\n"); - } - - private static string Job(string workflow, string name) => - Regex.Match(workflow, $@"(?ms)^ {Regex.Escape(name)}:\n.*?(?=^ \S|\z)", RegexOptions.None, Regex.InfiniteMatchTimeout).Value; - private static IEnumerable DrySteps(string workflow) => workflow.Split(" - name: ", StringSplitOptions.None).Where(static step => step.Contains("--job Dry", StringComparison.Ordinal)); diff --git a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs new file mode 100644 index 00000000..f5252998 --- /dev/null +++ b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs @@ -0,0 +1,35 @@ +using OpenCode.Sdk.Tools.Tests.RepositoryGates.Support; +using OpenCode.Sdk.Tools.Tests.Support; +using Testably.Abstractions; +using TUnit.Assertions.Enums; + +namespace OpenCode.Sdk.Tools.Tests.RepositoryGates; + +public sealed class CiMergeGroupReuseTests +{ + [Test] + public async Task Ci_Should_Build_And_Run_The_Reuse_Judge_From_The_Base_Worktree() + { + var fileSystem = new RealFileSystem(); + var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); + var reuse = CiWorkflow.Job(workflow, "reuse"); + await Assert.That(reuse).Contains("git worktree add --detach \"$RUNNER_TEMP/judge\" \"$QUEUE_BASE\""); + var steps = reuse.Split(" - name: ", StringSplitOptions.None); + var setup = steps.Single(static step => step.StartsWith("Setup .NET SDK\n", StringComparison.Ordinal)); + await Assert.That(setup).Contains("global-json-file: ${{ runner.temp }}/judge/global.json"); + var decide = steps.Single(static step => step.StartsWith("Decide reuse\n", StringComparison.Ordinal)); + await Assert.That(decide).Contains("working-directory: ${{ runner.temp }}/judge"); + await Assert.That(decide).Contains("dotnet run --file tools/opencode-tool.cs -- merge-group-reuse"); + await Assert.That(reuse).DoesNotContain("GITHUB_WORKSPACE"); + foreach (var checkout in steps.Where(static step => step.Contains("uses: actions/checkout@", StringComparison.Ordinal))) + { + await Assert.That(checkout.Split('\n')).DoesNotContain(static line => line.TrimStart().StartsWith("path:", StringComparison.Ordinal)); + } + + var permissions = reuse.Split('\n').SkipWhile(static line => line != " permissions:").Skip(1) + .TakeWhile(static line => line.StartsWith(" ", StringComparison.Ordinal)).Select(static line => line.Trim()); + await Assert.That(permissions).IsEquivalentTo([ + "contents: read", "actions: read", "pull-requests: read", + ], CollectionOrdering.Matching); + } +} diff --git a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/Support/CiWorkflow.cs b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/Support/CiWorkflow.cs new file mode 100644 index 00000000..be9295dd --- /dev/null +++ b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/Support/CiWorkflow.cs @@ -0,0 +1,14 @@ +using System.IO.Abstractions; +using System.Text.RegularExpressions; + +namespace OpenCode.Sdk.Tools.Tests.RepositoryGates.Support; + +internal sealed class CiWorkflow(IFileSystem fileSystem) +{ + public async Task ReadAsync(string root) => + (await fileSystem.File.ReadAllTextAsync(fileSystem.Path.Combine(root, ".github", "workflows", "ci.yml"))) + .ReplaceLineEndings("\n"); + + public static string Job(string workflow, string name) => + Regex.Match(workflow, $@"(?ms)^ {Regex.Escape(name)}:\n.*?(?=^ \S|\z)", RegexOptions.None, Regex.InfiniteMatchTimeout).Value; +} diff --git a/tools/OpenCode.Sdk.Tools/Commands/MergeGroupReuseDecision.cs b/tools/OpenCode.Sdk.Tools/Commands/MergeGroupReuseDecision.cs index f97cb7d7..a268a190 100644 --- a/tools/OpenCode.Sdk.Tools/Commands/MergeGroupReuseDecision.cs +++ b/tools/OpenCode.Sdk.Tools/Commands/MergeGroupReuseDecision.cs @@ -83,9 +83,7 @@ private static bool ProvenanceHolds(IReadOnlyDictionary facts, J } private static bool IsGuarded(string path) => - path.StartsWith(".github/", StringComparison.OrdinalIgnoreCase) || path.StartsWith("tools/", StringComparison.OrdinalIgnoreCase) || - path.Split('/')[^1].ToUpperInvariant() is "DIRECTORY.BUILD.PROPS" or "DIRECTORY.BUILD.TARGETS" or "DIRECTORY.PACKAGES.PROPS" or - "DIRECTORY.BUILD.RSP" or "GLOBAL.JSON" or "NUGET.CONFIG"; + path.StartsWith(".github/", StringComparison.OrdinalIgnoreCase); private static bool IsHash(string value) => value.Trim().Length == 40 && value.Trim().All(char.IsAsciiHexDigit); private static string? Text(JsonElement element, string name) => element.GetProperty(name).GetString(); From f5aef9bb5009a71a5b5fa50b9f7369060cc80369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C4=B0rgin?= Date: Sat, 14 Nov 2026 23:23:24 +0300 Subject: [PATCH 2/2] test(ci): the reuse gate pins the judge's input, its fail-closed add and its one executed command Review fix-ups for #826. The gate now asserts that QUEUE_BASE comes from merge_group.base_sha, that the judge worktree is added in the fail-closed "if ! git worktree add ...; then" form, that only the "Decide reuse" step runs a dotnet command, that this command is exactly the base's tool run (a commented copy or a relative path out of the judge fails), and that the job never names github.workspace. The redundant checkout path check is gone: the working-directory assertion already catches a judge placed anywhere else. quality-gates.md says a PR is judged by the base's tool and rules, and that the workflow calling the judge comes from the group's tree, so a workflow change is covered by review. --- docs/engineering/quality-gates.md | 8 +++++--- .../RepositoryGates/CiMergeGroupReuseTests.cs | 15 +++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/docs/engineering/quality-gates.md b/docs/engineering/quality-gates.md index c1b78ac9..46606c12 100644 --- a/docs/engineering/quality-gates.md +++ b/docs/engineering/quality-gates.md @@ -80,9 +80,11 @@ two parents, its second parent is still the queued PR's head, and that PR target The attempt's unique, unexpired `tested-tree-` artifact must record the group's tree and the same base tree its diff uses, and have been created within that attempt's successful `Changes` job window. The decision is built and run from the merge group's base commit, in a -separate worktree outside the workspace, with the base's own `global.json`, so the PR cannot change -its judge. The PR's changes from its merge base must not touch `.github/`, matched -case-insensitively, because the workflow itself always comes from the group's tree. +separate worktree outside the workspace, with the base's own `global.json`, so a PR is judged by +the base's tool and the base's rules, never its own. The PR's changes from its merge base must not +touch `.github/`, matched case-insensitively, because the workflow that gathers the facts and calls +the judge always comes from the group's tree; review covers a workflow change, as for any required +check. In practice, a PR whose base has not moved since its green run and which does not touch `.github/` avoids the second full run. Any missing, malformed or uncertain fact, or a failed reuse job, means the normal full CI runs. `Changes` must still succeed; the reuse branch of `CI result` diff --git a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs index f5252998..6d6dd54b 100644 --- a/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs +++ b/tests/OpenCode.Sdk.Tools.Tests/RepositoryGates/CiMergeGroupReuseTests.cs @@ -13,18 +13,21 @@ public async Task Ci_Should_Build_And_Run_The_Reuse_Judge_From_The_Base_Worktree var fileSystem = new RealFileSystem(); var workflow = await new CiWorkflow(fileSystem).ReadAsync(new RepositoryRoot(fileSystem).Locate()); var reuse = CiWorkflow.Job(workflow, "reuse"); - await Assert.That(reuse).Contains("git worktree add --detach \"$RUNNER_TEMP/judge\" \"$QUEUE_BASE\""); + await Assert.That(reuse).Contains("QUEUE_BASE: ${{ github.event.merge_group.base_sha }}"); + await Assert.That(reuse).Contains("if ! git worktree add --detach \"$RUNNER_TEMP/judge\" \"$QUEUE_BASE\"; then"); var steps = reuse.Split(" - name: ", StringSplitOptions.None); + var dotnetSteps = steps.Where(static step => step.Split('\n').Select(static line => line.TrimStart()) + .Any(static line => line.StartsWith("dotnet ", StringComparison.Ordinal) || line.StartsWith("run: dotnet ", StringComparison.Ordinal))) + .Select(static step => step[..step.IndexOf('\n', StringComparison.Ordinal)]); + await Assert.That(dotnetSteps).IsEquivalentTo(["Decide reuse"], CollectionOrdering.Matching); var setup = steps.Single(static step => step.StartsWith("Setup .NET SDK\n", StringComparison.Ordinal)); await Assert.That(setup).Contains("global-json-file: ${{ runner.temp }}/judge/global.json"); var decide = steps.Single(static step => step.StartsWith("Decide reuse\n", StringComparison.Ordinal)); await Assert.That(decide).Contains("working-directory: ${{ runner.temp }}/judge"); - await Assert.That(decide).Contains("dotnet run --file tools/opencode-tool.cs -- merge-group-reuse"); + await Assert.That(decide.Split('\n').Select(static line => line.Trim()).Where(static line => line.StartsWith("dotnet ", StringComparison.Ordinal))) + .IsEquivalentTo(["dotnet run --file tools/opencode-tool.cs -- merge-group-reuse --facts \"$RUNNER_TEMP/reuse\" --output \"$GITHUB_OUTPUT\""], CollectionOrdering.Matching); await Assert.That(reuse).DoesNotContain("GITHUB_WORKSPACE"); - foreach (var checkout in steps.Where(static step => step.Contains("uses: actions/checkout@", StringComparison.Ordinal))) - { - await Assert.That(checkout.Split('\n')).DoesNotContain(static line => line.TrimStart().StartsWith("path:", StringComparison.Ordinal)); - } + await Assert.That(reuse).DoesNotContain("github.workspace"); var permissions = reuse.Split('\n').SkipWhile(static line => line != " permissions:").Skip(1) .TakeWhile(static line => line.StartsWith(" ", StringComparison.Ordinal)).Select(static line => line.Trim());