Skip to content

Commit 485ca40

Browse files
committed
Read the tests set aside once per job, so no test step holds a token
#2987 gave each test step GH_TOKEN, so its runner could read the open flaky-test issues. On main that broke three things, and #2990 reverted it: - The test shard's step held a value the upstream upgrade cannot run (scripts/integrate-upstream.ts refuses an expression in a step's env). - The tests inherited the token. scripts/ci-run-tests.test.ts runs ci-run-tests.ts, which then asked GitHub through the suite's closed network and timed out at 5 seconds, three times. - api/client-boundary.test.ts did not know the script's fetch() (fixed in the commit before). Now each test job reads the list once, in a `uses` step of its own (.github/actions/flaky-tests-quarantine, `bun scripts/flaky-tests.ts quarantine <file>`), into flaky-tests-quarantine.json in the runner's temporary folder. The test steps read that file and never ask GitHub, and hold no token. The upstream upgrade skips `uses` steps, so it runs the shard's steps as before. Regression tests: a test step with a token in its environment makes no request to GitHub, and every ci.yml job that runs tests reads the list first and has no token in a test step. Both fail on #2987 as it landed, as do the upstream checks and ci-run-tests tests it broke.
1 parent cb776ce commit 485ca40

6 files changed

Lines changed: 255 additions & 42 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Flaky tests set aside
2+
description: >-
3+
Reads the tests set aside (the open flaky-test issues scripts/flaky-tests-watch.ts marked) once,
4+
with the job's token, and writes them to flaky-tests-quarantine.json in the runner's temporary
5+
folder, where the job's test steps read them (scripts/flaky-tests.ts). The test steps hold no
6+
token and never ask GitHub themselves, so neither does a test they run. A read that fails writes
7+
why, sets nothing aside, and fails nothing.
8+
9+
# The caller checks the repository out and sets up Bun first, and grants `issues: read`.
10+
11+
runs:
12+
using: composite
13+
steps:
14+
- name: The tests set aside
15+
shell: bash
16+
env:
17+
GH_TOKEN: ${{ github.token }}
18+
run: bun scripts/flaky-tests.ts quarantine "$RUNNER_TEMP/flaky-tests-quarantine.json"

‎.github/workflows/ci.yml‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ jobs:
459459
with:
460460
bun-version: 1.4.3
461461
- run: bun install --frozen-lockfile
462+
# The tests set aside, read once with the job's token; the test steps hold none
463+
# (.github/actions/flaky-tests-quarantine). As in every job that runs tests.
464+
- uses: $/.github/actions/flaky-tests-quarantine
462465
# The app's real-browser tests: a running step's words
463466
# under reduced motion, where onboarding's Back lands beside the Mac window's buttons, the name
464467
# step's orb drawn whole under the step dashes, onboarding's reels playing each scene on their
@@ -561,8 +564,6 @@ jobs:
561564
--env OPENBOT_APP_BROWSER=1 \
562565
"$PLAYWRIGHT_IMAGE" \
563566
bun test --parallel --retry=1 "${files[@]}"
564-
env:
565-
GH_TOKEN: ${{ github.token }}
566567
# On a failure, cancels the run at once (.github/actions/stop-on-failure).
567568
- if: failure()
568569
uses: $/.github/actions/stop-on-failure
@@ -734,6 +735,7 @@ jobs:
734735
bun-version: 1.4.3
735736
- run: bun install --frozen-lockfile
736737
- run: bun run generate:app-config
738+
- uses: $/.github/actions/flaky-tests-quarantine
737739
- run: |
738740
# One file per line, sorted: pull requests that each add a file then merge cleanly.
739741
files=(
@@ -887,8 +889,6 @@ jobs:
887889
--env OPENBOT_APP_BROWSER=1 --env OPENBOT_APP_ENGINES="$BROWSER_ENGINE" \
888890
"$PLAYWRIGHT_IMAGE" \
889891
bun test --parallel="$TEST_WORKERS" --timeout="$TEST_TIMEOUT_MS" --retry=1 --shard="$TEST_SHARD" --timings="scripts/browser-timings/$BROWSER_ENGINE.json" --update-timings "${files[@]}"
890-
env:
891-
GH_TOKEN: ${{ github.token }}
892892
# This shard's own durations, which the run wrote over the engine's record: download every
893893
# shard's and merge them to refresh it (scripts/browser-timings.ts).
894894
- if: ${{ !cancelled() }}
@@ -901,8 +901,6 @@ jobs:
901901
# Connect your AI's sign-in code, copied with one press and read back from WebKit's clipboard.
902902
# Only that test: the rest of the file runs in Chromium in the job above. Once, on the first shard.
903903
- if: matrix.engine == 'WebKit' && matrix.shard == 1
904-
env:
905-
GH_TOKEN: ${{ github.token }}
906904
run: |
907905
bun scripts/flaky-tests.ts run -- \
908906
docker run --rm --ipc=host \
@@ -944,12 +942,11 @@ jobs:
944942
- run: bun install --frozen-lockfile
945943
- run: bun install --frozen-lockfile
946944
working-directory: desktop
945+
- uses: $/.github/actions/flaky-tests-quarantine
947946
# A list, as in the jobs above, so scripts/ci-changes.ts can tell this job covers the file and a
948947
# change to it does not run it a second time in `tests (changed files)`. Paths from desktop/.
949948
# Under scripts/flaky-tests.ts, as the lists above.
950-
- env:
951-
GH_TOKEN: ${{ github.token }}
952-
run: |
949+
- run: |
953950
files=(
954951
src/setupLayout.approved.browser.test.ts
955952
src/setupLayout.browser.test.ts
@@ -964,9 +961,7 @@ jobs:
964961
bun test --parallel --retry=1 "${files[@]}"
965962
# Connect your AI's key field and tab row, in WebKit, the engine the Mac window draws with.
966963
# Only those tests: the rest of the file runs in Chromium in the step above.
967-
- env:
968-
GH_TOKEN: ${{ github.token }}
969-
run: |
964+
- run: |
970965
bun scripts/flaky-tests.ts run --root desktop -- \
971966
docker run --rm --ipc=host \
972967
--volume "$GITHUB_WORKSPACE:$GITHUB_WORKSPACE" --workdir "$GITHUB_WORKSPACE/desktop" \
@@ -1026,12 +1021,14 @@ jobs:
10261021
# pinned by the desktop lockfile rather than the root one.
10271022
- run: bun install --frozen-lockfile
10281023
working-directory: desktop
1024+
# The tests set aside, read once with the job's token (.github/actions/flaky-tests-quarantine).
1025+
# A `uses` step: the upstream upgrade runs this job's `run` steps without it, and the suite
1026+
# below, which they run, holds no token.
1027+
- uses: $/.github/actions/flaky-tests-quarantine
10291028
# This shard's tests, failing on a failed test or on a count it cannot read. The floor under the
10301029
# count is the suite's, so the `tests` job checks it on the sum of the shards. A failed test
10311030
# runs once more, and a test set aside does not fail the shard (scripts/flaky-tests.ts).
10321031
- run: bun run test:ci
1033-
env:
1034-
GH_TOKEN: ${{ github.token }}
10351032
# The shard's count, and how long each of its files took.
10361033
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
10371034
with:
@@ -1120,6 +1117,7 @@ jobs:
11201117
working-directory: agent-mastra
11211118
- run: bun install --frozen-lockfile
11221119
working-directory: desktop
1120+
- uses: $/.github/actions/flaky-tests-quarantine
11231121
- name: Install Chromium
11241122
if: needs.changes.outputs.browser_tests != '[]' || needs.changes.outputs.desktop_browser_tests != '[]'
11251123
run: bunx playwright-core install --with-deps --no-shell chromium
@@ -1136,8 +1134,6 @@ jobs:
11361134
BROWSER_TESTS: ${{ needs.changes.outputs.browser_tests }}
11371135
WEBKIT_TESTS: ${{ needs.changes.outputs.webkit_tests }}
11381136
DESKTOP_BROWSER_TESTS: ${{ needs.changes.outputs.desktop_browser_tests }}
1139-
# The tests set aside, read from the open flaky-test issues (scripts/flaky-tests.ts).
1140-
GH_TOKEN: ${{ github.token }}
11411137
run: bun scripts/ci-run-tests.ts
11421138
# On a failure, cancels the run at once (.github/actions/stop-on-failure).
11431139
- if: failure()

‎scripts/ci-run-tests.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function testPaths(json: string | undefined, name: string): string[] {
4747
});
4848
}
4949

50-
/** The tests set aside (open flaky-test issues), read once for every group. */
51-
let quarantine: Promise<Quarantine> | undefined;
50+
/** The tests set aside, from the job's list (FLAKY_TESTS_QUARANTINE), read once for every group. */
51+
let quarantine: Quarantine | undefined;
5252

5353
async function runGroup(
5454
label: string,
@@ -69,7 +69,7 @@ async function runGroup(
6969
});
7070
const stderr = await new Response(proc.stderr).text();
7171
process.stderr.write(stderr);
72-
const setAside = await quarantine;
72+
const setAside = quarantine;
7373
const verdict = judge(
7474
parseBunOutput(stderr, options.cwd ?? ""),
7575
await proc.exited,

‎scripts/flaky-tests.test.ts‎

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ const older = (number: number, name: string) => ({
5656
});
5757
let server: ReturnType<typeof Bun.serve>;
5858
let summaryDir: string;
59+
/** Every request the fake GitHub was sent. */
60+
let requests = 0;
5961

6062
beforeAll(() => {
6163
server = Bun.serve({
6264
port: 0,
6365
fetch(request) {
66+
requests += 1;
6467
const url = new URL(request.url);
6568
if (
6669
url.pathname !== `/repos/${REPOSITORY}/issues` ||
@@ -103,23 +106,65 @@ afterAll(() => {
103106
rmSync(summaryDir, { recursive: true, force: true });
104107
});
105108

106-
/** CI's wrapped step: `bun scripts/flaky-tests.ts run -- bun test --retry=1 <files>`. */
107-
async function wrapped(files: string[], quarantine: boolean) {
108-
const summary = join(summaryDir, `${crypto.randomUUID()}.md`);
109+
/** A child's environment: none of the runner's own GitHub settings, unless a test gives them. */
110+
function childEnv(extra: Record<string, string> = {}) {
109111
const env: Record<string, string | undefined> = {
110112
...readableBunEnv(),
111-
GITHUB_STEP_SUMMARY: summary,
112113
GITHUB_API_URL: `http://127.0.0.1:${server.port}`,
114+
...extra,
113115
};
114-
delete env.GITHUB_ACTIONS;
116+
for (const name of [
117+
"GITHUB_ACTIONS",
118+
"GITHUB_REPOSITORY",
119+
"GH_TOKEN",
120+
"GITHUB_TOKEN",
121+
"FLAKY_TESTS_QUARANTINE",
122+
"RUNNER_TEMP",
123+
"GITHUB_STEP_SUMMARY",
124+
]) {
125+
if (!(name in extra)) delete env[name];
126+
}
127+
return env;
128+
}
129+
130+
/**
131+
* CI's test job: the flaky-tests-quarantine step (`bun scripts/flaky-tests.ts quarantine <file>`,
132+
* with the job's token) when `quarantine`, then the wrapped step
133+
* (`bun scripts/flaky-tests.ts run -- bun test --retry=1 <files>`), which holds no token unless
134+
* `tokenInStep` puts one there.
135+
*/
136+
async function wrapped(
137+
files: string[],
138+
quarantine: boolean,
139+
tokenInStep = false,
140+
) {
141+
const summary = join(summaryDir, `${crypto.randomUUID()}.md`);
142+
const list = join(summaryDir, `${crypto.randomUUID()}.json`);
115143
if (quarantine) {
116-
env.GITHUB_REPOSITORY = REPOSITORY;
117-
env.GH_TOKEN = "test-token";
118-
} else {
119-
delete env.GITHUB_REPOSITORY;
120-
delete env.GH_TOKEN;
121-
delete env.GITHUB_TOKEN;
144+
// Not spawnSync: the fake GitHub answers from this process's event loop.
145+
const read = Bun.spawn(
146+
["bun", "scripts/flaky-tests.ts", "quarantine", list],
147+
{
148+
cwd: ROOT,
149+
env: childEnv({
150+
GITHUB_REPOSITORY: REPOSITORY,
151+
GH_TOKEN: "test-token",
152+
}),
153+
stdout: "pipe",
154+
stderr: "pipe",
155+
},
156+
);
157+
const said = await new Response(read.stdout).text();
158+
expect(await read.exited).toBe(0);
159+
expect(said).not.toContain("could not read");
122160
}
161+
const env = childEnv({
162+
GITHUB_STEP_SUMMARY: summary,
163+
...(quarantine ? { FLAKY_TESTS_QUARANTINE: list } : {}),
164+
...(tokenInStep
165+
? { GITHUB_REPOSITORY: REPOSITORY, GH_TOKEN: "test-token" }
166+
: {}),
167+
});
123168
const proc = Bun.spawn(
124169
[
125170
"bun",
@@ -254,6 +299,69 @@ describe("a CI test step wrapped by scripts/flaky-tests.ts", () => {
254299
}, 60_000);
255300
});
256301

302+
describe("the token stays out of the test steps", () => {
303+
test("a test step never asks GitHub, even with a token in its environment", async () => {
304+
// Regression, 5 December: the steps held GH_TOKEN, the tests they ran inherited it, and
305+
// scripts/ci-run-tests.test.ts's run of ci-run-tests.ts asked GitHub through the suite's
306+
// closed network until its tests timed out. A test step only reads the job's list.
307+
openIssues = [watched(101, "a broken test > fails every time")];
308+
const before = requests;
309+
const run = await wrapped(["broken.fixture.ts"], false, true);
310+
expect(requests - before).toBe(0);
311+
expect(run.exitCode).toBe(1);
312+
expect(run.stdout).toContain("no list of them was written");
313+
}, 60_000);
314+
315+
test("ci.yml: every job that runs tests reads the list first, and no step that runs tests holds a token", () => {
316+
// Regression, 5 December: a token in the test shard's step also broke the upstream upgrade,
317+
// which runs that job's `run` steps itself and refuses a value it cannot read
318+
// (scripts/integrate-upstream.ts, ciChecks).
319+
const workflow = Bun.YAML.parse(
320+
readFileSync(join(ROOT, ".github/workflows/ci.yml"), "utf8"),
321+
) as {
322+
jobs: Record<
323+
string,
324+
{
325+
steps?: {
326+
uses?: string;
327+
run?: string;
328+
env?: Record<string, unknown>;
329+
}[];
330+
}
331+
>;
332+
};
333+
const runsTests = /flaky-tests\.ts run|bun run test:ci|ci-run-tests\.ts/;
334+
const jobs = Object.entries(workflow.jobs).filter(([, job]) =>
335+
(job.steps ?? []).some((step) => runsTests.test(step.run ?? "")),
336+
);
337+
expect(jobs.map(([id]) => id).sort()).toEqual([
338+
"app-browser",
339+
"app-browser-desktop",
340+
"app-browser-webkit",
341+
"changed-tests",
342+
"test",
343+
]);
344+
for (const [id, job] of jobs) {
345+
const steps = job.steps ?? [];
346+
const list = steps.findIndex(
347+
(step) => step.uses === "$/.github/actions/flaky-tests-quarantine",
348+
);
349+
const first = steps.findIndex((step) => runsTests.test(step.run ?? ""));
350+
expect({ id, listFirst: list >= 0 && list < first }).toEqual({
351+
id,
352+
listFirst: true,
353+
});
354+
for (const step of steps) {
355+
if (!runsTests.test(step.run ?? "")) continue;
356+
const tokens = Object.entries(step.env ?? {})
357+
.filter(([, value]) => String(value).includes("token"))
358+
.map(([name]) => name);
359+
expect({ id, tokens }).toEqual({ id, tokens: [] });
360+
}
361+
}
362+
});
363+
});
364+
257365
describe("reading bun's output", () => {
258366
test("names on GitHub Actions come in log groups, and a run in a folder is named from the root", () => {
259367
const run = parseBunOutput(

0 commit comments

Comments
 (0)