Skip to content

Commit 4eebe72

Browse files
EvangelinkCopilot
andauthored
Fix agentic workflow pins and revision-bound build analysis (#12109)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e10a293 commit 4eebe72

38 files changed

Lines changed: 358 additions & 83 deletions

‎.github/aw/actions-lock.json‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"version": "v6.0.0",
3131
"sha": "a98b56852c35b8e3190ac28c8c2271da59106c68"
3232
},
33+
"actions/setup-node@v7.1.0": {
34+
"repo": "actions/setup-node",
35+
"version": "v7.1.0",
36+
"sha": "949feb2413d6458794dcd2491c4babbbce0c15c1"
37+
},
3338
"actions/setup-python@v7.0.0": {
3439
"repo": "actions/setup-python",
3540
"version": "v7.0.0",
@@ -40,10 +45,10 @@
4045
"version": "v7.0.2",
4146
"sha": "cf430e030ddbb5b0abf93d22962f4752f3646cd9"
4247
},
43-
"github/codeql-action/upload-sarif@v4.38.2": {
48+
"github/codeql-action/upload-sarif@v4.39.0": {
4449
"repo": "github/codeql-action/upload-sarif",
45-
"version": "v4.38.2",
46-
"sha": "2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2"
50+
"version": "v4.39.0",
51+
"sha": "6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee"
4752
},
4853
"github/gh-aw-actions/setup-cli@v0.89.21": {
4954
"repo": "github/gh-aw-actions/setup-cli",
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import pathlib
5+
import shutil
6+
import subprocess
7+
import tempfile
8+
import unittest
9+
10+
import yaml
11+
12+
13+
BASH = "bash"
14+
if os.name == "nt":
15+
git_path = shutil.which("git")
16+
if git_path is None:
17+
raise RuntimeError("Git for Windows is required to exercise the checkout workflow.")
18+
# The WSL bash launcher does not inherit these per-test environment values.
19+
candidates = (
20+
pathlib.Path(git_path).resolve().parents[1] / "bin" / "bash.exe",
21+
pathlib.Path(os.environ["ProgramFiles"]) / "Git" / "bin" / "bash.exe",
22+
)
23+
git_bash = next((candidate for candidate in candidates if candidate.is_file()), None)
24+
if git_bash is None:
25+
raise RuntimeError("Git for Windows Bash is required to exercise the checkout workflow.")
26+
BASH = str(git_bash)
27+
28+
29+
ROOT = pathlib.Path(__file__).resolve().parents[2]
30+
WORKFLOW_PATH = pathlib.Path(
31+
os.environ.get(
32+
"BUILD_ANALYSIS_WORKFLOW",
33+
ROOT / ".github/workflows/build-failure-analysis.md",
34+
)
35+
)
36+
WORKFLOW = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8").split("---", 2)[1])
37+
ATTACH_SCRIPT = next(
38+
(
39+
step["run"]
40+
for step in WORKFLOW["pre-agent-steps"]
41+
if step["name"] == "Attach the verified analysis revision"
42+
),
43+
"",
44+
)
45+
46+
47+
class BuildFailureAnalysisCheckoutTests(unittest.TestCase):
48+
def setUp(self) -> None:
49+
self.temporary = tempfile.TemporaryDirectory()
50+
self.addCleanup(self.temporary.cleanup)
51+
self.root = pathlib.Path(self.temporary.name)
52+
self.origin = self.root / "origin"
53+
self.workspace = self.root / "workspace"
54+
self.environment = {
55+
**os.environ,
56+
"GIT_CONFIG_COUNT": "2",
57+
"GIT_CONFIG_KEY_0": "user.name",
58+
"GIT_CONFIG_VALUE_0": "Checkout Test",
59+
"GIT_CONFIG_KEY_1": "user.email",
60+
"GIT_CONFIG_VALUE_1": "checkout-test@example.invalid",
61+
}
62+
self.git("init", "--quiet", "--initial-branch=main", str(self.origin))
63+
self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "Base")
64+
self.git("-C", str(self.origin), "switch", "--quiet", "--create", "analysis-target")
65+
self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "PR head")
66+
self.head = self.git("-C", str(self.origin), "rev-parse", "HEAD")
67+
self.git("-C", str(self.origin), "update-ref", "refs/pull/42/head", self.head)
68+
self.git("-C", str(self.origin), "switch", "--quiet", "main")
69+
self.git("init", "--quiet", str(self.workspace))
70+
self.git("-C", str(self.workspace), "remote", "add", "origin", self.origin.as_uri())
71+
72+
def git(self, *arguments: str) -> str:
73+
result = subprocess.run(
74+
["git", *arguments],
75+
env=self.environment,
76+
text=True,
77+
capture_output=True,
78+
timeout=30,
79+
check=True,
80+
)
81+
return result.stdout.strip()
82+
83+
def checkout(self, checkout_ref: str = "analysis-target") -> None:
84+
values = {
85+
"${{ needs.fetch-binlog.outputs.pr-head-sha }}": self.head,
86+
"${{ needs.fetch-binlog.outputs.pr-checkout-ref }}": checkout_ref,
87+
}
88+
ref = values[WORKFLOW["checkout"][0]["ref"]]
89+
result = subprocess.run(
90+
["git", "-C", str(self.workspace), "fetch", "--quiet", "--depth=1", "origin", ref],
91+
env=self.environment,
92+
text=True,
93+
capture_output=True,
94+
timeout=30,
95+
)
96+
self.assertEqual(0, result.returncode, result.stderr)
97+
if ref == self.head or ref.startswith("refs/pull/"):
98+
self.git("-C", str(self.workspace), "checkout", "--quiet", "--detach", "FETCH_HEAD")
99+
else:
100+
self.git("-C", str(self.workspace), "checkout", "--quiet", "-b", ref, "FETCH_HEAD")
101+
102+
def attach(self, checkout_ref: str = "analysis-target", expected_sha: str | None = None) -> subprocess.CompletedProcess:
103+
return subprocess.run(
104+
[BASH, "-c", ATTACH_SCRIPT],
105+
cwd=self.workspace,
106+
env={
107+
**self.environment,
108+
"EXPECTED_HEAD_SHA": self.head if expected_sha is None else expected_sha,
109+
"ANALYSIS_CHECKOUT_REF": checkout_ref,
110+
},
111+
text=True,
112+
capture_output=True,
113+
timeout=30,
114+
)
115+
116+
def assert_attached_revision(self) -> None:
117+
result = self.attach()
118+
self.assertEqual(0, result.returncode, result.stdout + result.stderr)
119+
self.assertEqual(self.head, self.git("-C", str(self.workspace), "rev-parse", "HEAD"))
120+
self.assertEqual(
121+
"analysis-target",
122+
self.git("-C", str(self.workspace), "branch", "--show-current"),
123+
)
124+
self.assertEqual(
125+
self.head,
126+
self.git("-C", str(self.workspace), "rev-parse", "refs/remotes/origin/analysis-target"),
127+
)
128+
129+
def test_deleted_branch_checks_out_validated_commit(self) -> None:
130+
self.git("-C", str(self.origin), "branch", "--delete", "--force", "analysis-target")
131+
132+
self.checkout()
133+
134+
self.assert_attached_revision()
135+
branches = self.git("-C", str(self.origin), "for-each-ref", "--format=%(refname)", "refs/heads")
136+
self.assertEqual("refs/heads/main", branches)
137+
138+
def test_moving_branch_does_not_change_analysis_revision(self) -> None:
139+
self.git("-C", str(self.origin), "switch", "--quiet", "analysis-target")
140+
self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "Newer head")
141+
142+
self.checkout()
143+
144+
self.assert_attached_revision()
145+
146+
def test_same_repo_patch_baseline_is_original_head(self) -> None:
147+
self.checkout()
148+
self.assert_attached_revision()
149+
150+
self.git("-C", str(self.workspace), "commit", "--quiet", "--allow-empty", "-m", "Agent fix")
151+
152+
self.assertEqual(
153+
"1",
154+
self.git("-C", str(self.workspace), "rev-list", "--count", "origin/analysis-target..HEAD"),
155+
)
156+
157+
def test_fork_remains_detached_without_same_named_origin_branch(self) -> None:
158+
self.checkout("refs/pull/42/head")
159+
160+
result = self.attach("refs/pull/42/head")
161+
162+
self.assertEqual(0, result.returncode, result.stdout + result.stderr)
163+
self.assertEqual(self.head, self.git("-C", str(self.workspace), "rev-parse", "HEAD"))
164+
self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current"))
165+
self.assertEqual(
166+
"",
167+
self.git("-C", str(self.workspace), "for-each-ref", "--format=%(refname)", "refs/remotes"),
168+
)
169+
170+
def test_mismatched_checkout_is_rejected_before_creating_refs(self) -> None:
171+
self.checkout()
172+
173+
result = self.attach(expected_sha="0" * 40)
174+
175+
self.assertNotEqual(0, result.returncode)
176+
self.assertIn("The checkout does not match the revision", result.stdout)
177+
self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current"))
178+
179+
def test_invalid_branch_is_rejected_before_creating_refs(self) -> None:
180+
self.checkout()
181+
182+
result = self.attach("--orphan")
183+
184+
self.assertNotEqual(0, result.returncode)
185+
self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current"))
186+
self.assertEqual(
187+
"",
188+
self.git("-C", str(self.workspace), "for-each-ref", "--format=%(refname)", "refs/remotes"),
189+
)
190+
191+
192+
if __name__ == "__main__":
193+
unittest.main()

‎.github/workflows/README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ This update retains gh-aw v0.89.21 because [v0.89.22](https://github.com/github/
201201

202202
The v0.89.21 Node.js setup helper `GenerateNodeJsSetupStep()` reads only the embedded action-pin catalog, bypassing both `aw.json` redirects and the repository cache. For a catalog-only correction, build the exact pinned gh-aw source after resolving the required action version in its cache and running upstream's [`sync-action-pins` target](https://github.com/github/gh-aw/blob/v0.89.21/Makefile). Keep the compiler code unchanged, preserve the pinned setup action, and record the source commit, verified action SHA, and reproduction commands in the PR. On Windows, clone that source with `core.autocrlf=false` so embedded prompt assets retain their committed LF endings. Regenerate the lock rather than editing it, and verify that no unrelated executable configuration changed.
203203

204+
The October 27 repair aligns the embedded catalog and repository cache with
205+
`actions/setup-node@v7.1.0` and `github/codeql-action/upload-sarif@v4.39.0`.
206+
Both actions were already used by executable workflow steps, but 33 generated
207+
manifests and the SARIF cache entry still described older versions. The existing
208+
auditor reported 36 problems. Regeneration must update the manifests as well as
209+
the executable steps; changing only `uses:` lines leaves the repository broken.
210+
The compiler remains v0.89.21 at source commit
211+
`c35393777e5604a63721d09512263b1383301d4f`, with catalog data synchronized from
212+
the repository's verified pins before building. Do not use a newer local
213+
extension to repair these locks: even `--no-emit` can update the action cache
214+
to that extension's version.
215+
216+
For that source build, set both release linker variables:
217+
`-ldflags "-s -w -X main.version=v0.89.21 -X main.isRelease=true"`.
218+
Setting only `main.version` makes `version` print the expected value but leaves
219+
compilation in development mode, emitting local action paths and a different
220+
job graph. Reject that output rather than approving its extra actions.
221+
204222
Because the `compiler_version` header asserts an identity claim rather than the emitted bytes, always re-read the diff of a local compile — a change to a `uses:` pin that you did not intend is the tell. The repository also enforces this automatically:
205223

206224
```bash
@@ -216,6 +234,34 @@ python .github/scripts/check_action_pins.py --list
216234

217235
[`check-action-pins.yml`](./check-action-pins.yml) runs the same script on every pull request that touches `.github/workflows/**`, `.github/actions/**`, `.github/aw/actions-lock.json`, or the script itself. It fails when a generated workflow carries an unpinned `uses:`, when a pin disagrees with `.github/aw/actions-lock.json`, when one action resolves to more than one SHA across the repository, when a `docker://` container action is not pinned to an image digest, or when a `uses` reachable in the parsed YAML is hidden from the line scan (a block scalar, flow mapping or alias). Comparisons are case-insensitive on owner/repository names, since GitHub resolves them that way and `Actions/Checkout` would otherwise escape the lock-file check.
218236

237+
### Build-analysis checkout identity
238+
239+
[Run 40359801044](https://github.com/microsoft/testfx/actions/runs/40359801044)
240+
resolved an open PR, but it merged and its branch was deleted during activation.
241+
The later agent checkout failed before analysis. The automatic build analyst now
242+
checks out the head SHA already validated against the Azure Pipelines artifacts,
243+
asserts that exact identity, and attaches only a local same-repository branch.
244+
Its `origin/<branch>` baseline is also pinned locally to that SHA so incremental
245+
patch collection neither refetches a deleted branch nor includes newer commits.
246+
Fork checkouts remain detached; the safe-output target, closed/deleted-branch
247+
checks, allowed files, loop guard, and credentials are unchanged.
248+
249+
`python .github/scripts/test_build_failure_analysis_checkout.py` exercises the
250+
authored checkout configuration and pre-agent script using isolated local Git
251+
repositories, including deleted/moving branches, fork isolation, patch baseline,
252+
and rejected input. The pin-check workflow runs these regressions as well.
253+
This does not prove hosted artifact access or safe-output publication.
254+
255+
Failures before any jobs exist are not necessarily malformed YAML. For example,
256+
[run 40366844700](https://github.com/microsoft/testfx/actions/runs/40366844700)
257+
expired awaiting approval. Read the run annotation before changing triggers,
258+
permissions, or approval policy. Likewise, daily-credit HTTP 502 failures leave
259+
usage unknown; they do not justify removing the credit guardrail.
260+
261+
Full strict compilation also checks the link checker's command-file writes.
262+
Keep every `GITHUB_OUTPUT` redirection quoted, including its empty-link path;
263+
an output-file path containing spaces must not split into multiple shell words.
264+
219265
## Secrets & authentication
220266

221267
Agentic workflows authenticate through repository secrets:

0 commit comments

Comments
 (0)