Skip to content

Pin /perf PR head to the trigger-time commit and gate the sticky comment - #899

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 2 commits into
mainfrom
azchohfi-fix-perf-toctou
Jul 17, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 2 commits into
mainfrom
azchohfi-fix-perf-toctou

Conversation

@azchohfi

Copy link
Copy Markdown
Collaborator

What

Two adjustments to .github/workflows/perf-compare.yml so a /perf run always benchmarks — and comments on — exactly the commit that triggered it.

1. Pin the PR head at trigger time

  • New early Pin PR head SHA step resolves the PR head via the API and records it, running before the .NET setup + PerfLib steps rather than resolving the head live several minutes later.
  • Resolve PR + create PR worktree now consumes that pinned SHA, fetches pull/N/head, and re-verifies the fetched head still equals the pinned SHA (-cne) before creating the build worktree. If the branch moved on after the trigger, the step stops with an actionable message instead of building a different commit.
  • For comment triggers, a head whose commit post-dates the /perf comment is rejected (best-effort on the commit-date lookup; the pinned-SHA re-verification is the hard check either way). workflow_dispatch keeps working unchanged.

2. Only publish a comment the benchmark actually produced

  • Post / update sticky comment now publishes perf-out/comment.md only when the benchmark step''s outcome == success (the trusted script writes that file as the final action of a clean run). On any non-success it synthesizes the neutral fallback, overwriting whatever is on disk, so a leftover or incomplete file is never posted.

Docs

  • tests/stress_perf/ci/README.md updated to describe the pinned-head resolution and the success-gated comment.

Notes

  • A degraded/partial run (previously posted a low-confidence table) now shows the neutral fallback pointing at the run log + perf-logs artifact.

Validation

  • Workflow YAML parses; the three changed PowerShell step bodies parse cleanly.
  • Simulated the pin/equality/gate decision points (timezone-aware date comparison, exact SHA match, publish-vs-fallback matrix incl. the skipped-step case).

Resolve and pin the PR head SHA in an early step (before the .NET setup and
PerfLib steps), then re-verify the fetched `pull/N/head` still equals that
pinned SHA before creating the build worktree, so the benchmarked commit is
the one that existed when the run was triggered. For comment triggers, reject
a head whose commit post-dates the /perf comment.

Publish perf-out/comment.md only when the benchmark step succeeded; on any
non-success, synthesize the neutral fallback (overwriting whatever is on
disk) so a leftover/partial file is never posted.

Update tests/stress_perf/ci/README.md to match.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the on-demand perf comparison workflow so /perf runs benchmark (and comment on) the exact PR head commit resolved at trigger time, and so the sticky PR comment is only published when a benchmark run completed successfully.

Changes:

  • Add an early “Pin PR head SHA” step to resolve and record the PR head SHA immediately, with a best-effort guard for comment triggers where the head appears newer than the /perf comment.
  • Update PR worktree creation to fetch refs/pull/N/head and hard-verify the fetched FETCH_HEAD still matches the pinned SHA before building.
  • Gate sticky comment publishing on the benchmark step’s outcome == success, otherwise overwrite with a neutral workflow-generated fallback.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
.github/workflows/perf-compare.yml Pins PR head SHA at trigger time, verifies it before building, and only publishes perf-out/comment.md on successful benchmark completion.
tests/stress_perf/ci/README.md Documents the pinned-head behavior and the success-gated sticky comment behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…t gating in skill doc

- perf-compare.yml: source the sticky-comment step's PR_NUMBER from the triggering event rather than steps.resolve.outputs, so the always() step can still post the fallback comment when 'Resolve PR' throws (e.g. the PR head advanced after /perf and the run is rejected before resolve emits its outputs).

- perf-compare/SKILL.md: note that /perf publishes the rendered comparison only on a successful run and posts a neutral fallback otherwise.

Co-authored-by: Copilot App <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

📦 Build metrics

Artifact sizes for c6d536c vs the base branch (49c9209).

Packages (compressed .nupkg)

Artifact base PR Δ
Microsoft.UI.Reactor.nupkg 1.95 MB 1.95 MB +28 B (0.00%) ≈
Microsoft.UI.Reactor.Advanced.nupkg 27.1 KB 27.2 KB +11 B (+0.04%) ≈
Microsoft.UI.Reactor.Devtools.nupkg 278.1 KB 278.1 KB +12 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor

Artifact base PR Δ
Reactor.Analyzers.dll 301.0 KB 301.0 KB +0 B (0.00%) ≈
Reactor.dll 3.24 MB 3.24 MB +0 B (0.00%) ≈
Reactor.Localization.Generator.dll 16.0 KB 16.0 KB +0 B (0.00%) ≈
Reactor.Wrappers.Abstractions.dll 10.5 KB 10.5 KB +0 B (0.00%) ≈
Reactor.Wrappers.Generator.dll 99.5 KB 99.5 KB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Advanced

Artifact base PR Δ
Reactor.Advanced.dll 33.0 KB 33.0 KB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Devtools

Artifact base PR Δ
Microsoft.UI.Reactor.Devtools.dll 777.5 KB 777.5 KB +0 B (0.00%) ≈

No size change beyond the noise floor. ✅

✅ smaller / ⚠️ larger / ≈ within noise. Sizes come from a Release dotnet pack on the CI runner: packages are the compressed .nupkg download size, assemblies the uncompressed DLL inside it.
workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Merged coverage

Coverage for c6d536c vs the base branch (49c9209) — unit + selftest merged.

Metric base PR Δ
Line 85.50% 85.48% -0.02 pp ≈
Branch 77.29% (895/1158) 77.20% (894/1158) -0.09 pp ≈

No coverage change beyond the noise floor. ✅

✅ higher / ⚠️ lower / ≈ within noise. Δ is in percentage points; coverage is unit + selftest merged (Debug x64) on the CI runner. Cobertura reports attached to the workflow run as artifacts.

@Hemantxk Hemant Kumar (Hemantxk) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the CI.yml also captures SHA, but it captures it through HEAD_SHA: ${{ github.event.pull_request.head.sha }}, which seems to fetch right hash. Just curious would it be easier to follow the similar approach here?

@azchohfi

Copy link
Copy Markdown
Collaborator Author

Hemant Kumar (@Hemantxk) thanks! That works in ci.yml because it's triggered by pull_request, so the event payload carries the PR object and github.event.pull_request.head.sha is populated.

perf-compare.yml runs on issue_comment (the /perf comment) and workflow_dispatch, and neither payload includes github.event.pull_request — issue_comment only has github.event.issue/github.event.comment, and workflow_dispatch only has inputs.pr_number — so github.event.pull_request.head.sha would be empty here. The gh api repos/${{ github.repository }}/pulls/$pr --jq .head.sha call in the "Pin PR head SHA" step does the equivalent trigger-time capture for a comment/dispatch trigger. We keep it on issue_comment on purpose so the perf scripts always run from the trusted default branch.

@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit 5e92221 into main Jul 17, 2026
21 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-fix-perf-toctou branch July 17, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants