Skip to content

Stop two CI guards failing for the machine they ran on - #11164

Merged
danielhanchen merged 9 commits into
mainfrom
ci/flaky-timing-and-tempdir
Sep 17, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
ci/flaky-timing-and-tempdir

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two CI guards have been going red on branches that touch neither of them. Both are timing or environment reads rather than claims about the code under test, and both are fixed here without loosening what they assert.

The linearity guard re-measures instead of loosening

tests/studio/test_update_release_notes.py::test_stripping_comments_stays_linear_in_the_code_spans reported 4x the input cost 7.2x the time against a bar of 6.0 on #11152, a branch that changes none of this code.

tests/_shared/growth.py already forms paired ratios and takes a median, which divides most contention out. Not all of it: the big leg runs four times longer than the small one, so a scheduler stall is four times more likely to land in the big half, which biases a pair upward and never downward. Three pairs is few enough that two unlucky ones move the median.

assert_linear now re-measures on a larger sample when the first reading is over the bar, and fails only if that second reading is over it too. This is not a second chance. A genuinely quadratic path measures about factor ** 2 in every pair, so its second median comes back over the bar as surely as its first, while a contention spike does not survive being asked again on a bigger sample. The extra pairs are paid only on the reading that would otherwise have failed, so a green run still costs three pairs.

assert_linear decides the verdict of four test files and had no rows of its own, so it gets them in tests/test_growth_ratio_guard.py:

  • a linear path passes (the negative control, without which every other row could pass by the guard never accepting)
  • a quadratic path fails
  • a linear path with a stalled first sample passes, and the test asserts it really did reach the retry rather than passing on the first reading
  • a quadratic path with the same stalls still fails, which is the half that must not be weakened
  • the failure message names both samples
  • growth reports the best big time, not the worst, so the 60s backstop does not fire on a runner that stalled once

Cost is simulated rather than spent: run sleeps for a duration computed from the input size, so a quadratic shape here is quadratic exactly and the whole file runs in a few seconds.

A temp-file check that was reading the whole box

studio/backend/tests/test_data_recipe_dataset_download.py::test_build_dataset_download_leaves_no_temp_file_when_export_fails snapshotted tempfile.gettempdir() before and after, to check that a failed export unlinked what it created.

Its siblings in the same file each leave a NamedTemporaryFile(delete = False, suffix = ".jsonl") in flight until their own finally runs, and the backend job shards this suite with -n 4 --dist loadgroup with no xdist_group on the file. A sibling on another worker can therefore put a .jsonl in that directory between the two reads and fail this test for something it does not measure. It failed that way on #11159.

Pointing tempfile.tempdir at a directory only this test can reach makes the reading exact, and lets the assertion be "nothing at all" rather than "the same set as a moment ago".

Verification

  • tests/test_growth_ratio_guard.py and tests/studio/test_update_release_notes.py: 195 passed
  • studio/backend/tests/test_data_recipe_dataset_download.py and studio/backend/tests/test_tool_xml_strip.py: 122 passed
  • The temp-dir test still catches the regression it exists for: with the jsonl_path.unlink(missing_ok = True) arm in core/data_recipe/export.py removed, it fails.

Both went red on branches that touch neither of them.

tests/studio/test_update_release_notes.py's linearity guard reported 7.2x
against a bar of 6.0 on #11152. Pairing divides most contention out but not
all of it: the big leg runs four times longer than the small one, so a
scheduler stall is four times more likely to land in the big half, which biases
a pair upward and never downward, and three pairs is few enough that two
unlucky ones move the median. Re-measure on a larger sample instead of
loosening the bar. That is not a second chance, because a genuinely quadratic
path measures about sixteen in every pair and comes back over the bar as surely
the second time, while a stall does not survive being asked again. The cost is
paid only on the reading that would otherwise have failed.

assert_linear decides four test files and had no rows of its own, so it gets
them: linear passes, quadratic fails, a stalled linear path passes and is shown
to have really taken the retry, a stalled quadratic path still fails, and the
backstop reads the best big time rather than the worst. Cost is simulated
rather than spent, so the shapes are exact and the file runs in seconds.

studio/backend/tests/test_data_recipe_dataset_download.py snapshotted the
shared system temp dir to check a failed export unlinked what it created,
which made it a claim about the whole box. Its siblings each leave a
NamedTemporaryFile with the same .jsonl suffix in flight until their own
finally runs, and CI shards this suite with -n 4 --dist loadgroup, so one of
them on another worker could fail this test for something it does not measure.
Point tempfile.tempdir at a directory only this test can reach; the assertion
becomes nothing at all rather than the same set as a moment ago. Confirmed it
still fails when the cleanup arm is removed.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-17T03:16:21.221468Z 2c859d1 Manual request
🔒 Security Review ✅ Completed 2026-09-17T00:29:39.955827Z 67ec340 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 67ec340580

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

The rows were driven by real sleeps, so on a loaded runner they could overshoot
independently and stop meaning what they say: stretch either nominal 4 ms small
leg in the stalled-but-linear row past about 6.7 ms and its injected ratio
falls under the bar, and if both stretch the retry is never reached and the
call-count assertion fails. That is the same unrelated red this change exists
to remove, reintroduced in the test of the fix.

growth and assert_linear now take an optional clock, defaulting to
time.perf_counter, and the rows drive a fake one that advances by exactly the
cost the shape declares. A quadratic path here is quadratic to the last digit,
the file runs in 0.04s, and fifteen consecutive runs give the same seven
passes. Nothing in the repo passes clock outside these tests.

Costs are scaled so the quadratic rows fail on the ratio rather than on the 60s
backstop, which now has a row of its own for each arm: one stalled leg must not
trip it, and a path genuinely too slow to measure must.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The retry could be accepted from a sample that never finished. If its first
pair blew the 60s backstop the loop stopped there, and if that same pair's
small leg was also slow the one surviving ratio could sit under the tolerance,
so a superlinear path passed on one aborted pair. Taking min(big, big_again)
then hid the abort, because the first sample's best big time was well under
budget.

Both halves are now required of the confirmation itself: its own big time under
the budget, and all of its pairs completed. Verified against the old code,
which accepts exactly this shape and now does not, with a row that sizes the
stalls so the surviving ratio really is under the bar and the failure is the
abort rather than the ratio.

growth reports how many pairs it completed, since a caller that reads its
median as a verdict cannot tell an aborted sample from a finished one.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The 60s backstop bounds a single leg. It does not bound the sample: a
regression that holds each big leg just under it still costs about 7 * 60s
in the confirmation, and the four pytest invocations in
studio-backend-ci.yml pass --timeout=330. The worker is killed part way
through, and the run reports a bare timeout instead of the linearity
failure this guard was written to name, which is the same complaint the
docstring already makes about the absolute budget it replaced.

The retry now takes as many pairs as it can afford of a 120s budget,
estimating the pair from the big leg already measured and the ratio already
formed. Fewer pairs is a weaker vote, not a broken one, since contention is
the same size in every pair. Below three there is nothing worth buying, so
the reading already taken becomes the verdict and says so in its message
rather than spending five minutes to reach the same answer.

Two rows for the two arms: a 40s big leg over the bar fails as a linearity
failure and provably does not start a confirmation (six legs, no more), and
a 20s one still takes five pairs and passes on them.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Sizing the retry from the first sample was an estimate dressed as a bound.
It reads the BEST big leg and the MEDIAN ratio, and those come from
different pairs: a sample of 59s, 59s and 1s big legs against 1s small legs
offers a one-second pair cost alongside a ratio of 59, so it authorises all
seven pairs, and if the confirmation's own legs then arrive at 40s the run
overruns --timeout=330 exactly as before.

growth() takes a budget_s that bounds the whole call, checked on the pair
boundary so a stopped sample still holds a whole number of ratios.
abort_over_s bounds one big leg and budget_s bounds the sample; neither
implies the other, which is the gap this closes. A confirmation cut short
already fails through the pairs_completed assertion, so the verdict arrives
as a sentence naming the unfinished sample rather than as the runner's
timeout, which is the whole point.

The pre-sizing stays, demoted to what it is: it refuses a retry that
obviously cannot fit and keeps a green run cheap. One row for the exact
mixed-estimate shape, pinning that the loop gives up after three of the
seven pairs it was authorised.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A bound tested once a pair is already home is not a bound on that pair.
Three 59-second pairs under a 120-second budget all run, because each one
is inside the budget at the moment its predecessor finishes, and the sample
costs 177s. With a first sample of 39s, 59s and 59s big legs -- which sizes
the retry at three pairs, since the estimate reads the best leg and the
median ratio from different pairs -- the whole test reaches about 334s and
--timeout=330 kills it before the assertion, which is what the budget was
added to prevent.

The loop now reserves room for another pair at the cost of the worst pair
it has seen in this sample, and stops when that no longer fits. Worst
rather than mean because contention only adds. On the shape above it stops
at two pairs, the confirmation spends 118s of its 120, and the test lands
around 275s.

One row for that shape, asserting the pair count, the message, and that the
whole test stays under the runner's timeout. The existing mixed-estimate
row now stops at two pairs rather than three, for the same reason.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The retry got a fresh 120s no matter what the first sample had already
spent. The first sample has no bound of its own past the 60s per big leg,
so a scheduler stall in one of its small legs can burn most of the runner's
patience before the re-measurement is even considered, and a confirmation
counted fresh from there overruns whatever was left.

confirm_budget_s becomes total_budget_s and covers the whole call, first
sample included. The retry is sized and bounded by what remains, and the
message says how much that was. 240s against --timeout=330, leaving margin
for collection and the rest of the test body.

Two rows retuned rather than rewritten, since deducting the first sample
changes what each affords. The partial-affordability row now uses 30s big
legs, which put 90s into the first sample and leave four pairs of the
remaining 150. The before-the-pair row uses 13s / 20s / 20s big legs, which
cost 56s and authorise all seven, and its confirmation's 59s legs stop at
three for 177s of the 184 left; asked after the pair instead, the fourth
would have run to 236 and blown a bound it was never tested against while
inside it.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

total_budget_s documented a whole-call guarantee and was only ever handed
to the confirmation. Contention landing in the SMALL legs is what that
misses: 50s small against 55s big reads as a ratio of 1.1, comfortably
under the tolerance, so nothing reaches the retry branch where the budget
was enforced, and three of those pairs run to 315s past --timeout=330 while
returning a cheerful pass.

The first growth call now takes the budget too, and a sample that stopped
short is not accepted as a verdict: two pairs is not the reading the caller
asked for, so it fails saying so rather than reporting a median of whatever
it managed. The per-leg backstop is still checked first, so a path with one
enormous leg keeps its own clearer message.

One row for that shape, asserting the pair count, the message, and that the
call stays under the runner's timeout.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 17, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 2c859d17fd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 9c5b54d into main Sep 17, 2026
33 of 34 checks passed
@danielhanchen
danielhanchen deleted the ci/flaky-timing-and-tempdir branch September 17, 2026 03:20
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
* Stop two CI guards failing for the machine they ran on

Both went red on branches that touch neither of them.

tests/studio/test_update_release_notes.py's linearity guard reported 7.2x
against a bar of 6.0 on unslothai#11152. Pairing divides most contention out but not
all of it: the big leg runs four times longer than the small one, so a
scheduler stall is four times more likely to land in the big half, which biases
a pair upward and never downward, and three pairs is few enough that two
unlucky ones move the median. Re-measure on a larger sample instead of
loosening the bar. That is not a second chance, because a genuinely quadratic
path measures about sixteen in every pair and comes back over the bar as surely
the second time, while a stall does not survive being asked again. The cost is
paid only on the reading that would otherwise have failed.

assert_linear decides four test files and had no rows of its own, so it gets
them: linear passes, quadratic fails, a stalled linear path passes and is shown
to have really taken the retry, a stalled quadratic path still fails, and the
backstop reads the best big time rather than the worst. Cost is simulated
rather than spent, so the shapes are exact and the file runs in seconds.

studio/backend/tests/test_data_recipe_dataset_download.py snapshotted the
shared system temp dir to check a failed export unlinked what it created,
which made it a claim about the whole box. Its siblings each leave a
NamedTemporaryFile with the same .jsonl suffix in flight until their own
finally runs, and CI shards this suite with -n 4 --dist loadgroup, so one of
them on another worker could fail this test for something it does not measure.
Point tempfile.tempdir at a directory only this test can reach; the assertion
becomes nothing at all rather than the same set as a moment ago. Confirmed it
still fails when the cleanup arm is removed.

* Declare the cost in the guard's own tests instead of sleeping for it

The rows were driven by real sleeps, so on a loaded runner they could overshoot
independently and stop meaning what they say: stretch either nominal 4 ms small
leg in the stalled-but-linear row past about 6.7 ms and its injected ratio
falls under the bar, and if both stretch the retry is never reached and the
call-count assertion fails. That is the same unrelated red this change exists
to remove, reintroduced in the test of the fix.

growth and assert_linear now take an optional clock, defaulting to
time.perf_counter, and the rows drive a fake one that advances by exactly the
cost the shape declares. A quadratic path here is quadratic to the last digit,
the file runs in 0.04s, and fifteen consecutive runs give the same seven
passes. Nothing in the repo passes clock outside these tests.

Costs are scaled so the quadratic rows fail on the ratio rather than on the 60s
backstop, which now has a row of its own for each arm: one stalled leg must not
trip it, and a path genuinely too slow to measure must.

* Make the confirmation stand on its own reading

The retry could be accepted from a sample that never finished. If its first
pair blew the 60s backstop the loop stopped there, and if that same pair's
small leg was also slow the one surviving ratio could sit under the tolerance,
so a superlinear path passed on one aborted pair. Taking min(big, big_again)
then hid the abort, because the first sample's best big time was well under
budget.

Both halves are now required of the confirmation itself: its own big time under
the budget, and all of its pairs completed. Verified against the old code,
which accepts exactly this shape and now does not, with a row that sizes the
stalls so the surviving ratio really is under the bar and the failure is the
abort rather than the ratio.

growth reports how many pairs it completed, since a caller that reads its
median as a verdict cannot tell an aborted sample from a finished one.

* Bound what the confirmation may spend, not just one leg of it

The 60s backstop bounds a single leg. It does not bound the sample: a
regression that holds each big leg just under it still costs about 7 * 60s
in the confirmation, and the four pytest invocations in
studio-backend-ci.yml pass --timeout=330. The worker is killed part way
through, and the run reports a bare timeout instead of the linearity
failure this guard was written to name, which is the same complaint the
docstring already makes about the absolute budget it replaced.

The retry now takes as many pairs as it can afford of a 120s budget,
estimating the pair from the big leg already measured and the ratio already
formed. Fewer pairs is a weaker vote, not a broken one, since contention is
the same size in every pair. Below three there is nothing worth buying, so
the reading already taken becomes the verdict and says so in its message
rather than spending five minutes to reach the same answer.

Two rows for the two arms: a 40s big leg over the bar fails as a linearity
failure and provably does not start a confirmation (six legs, no more), and
a 20s one still takes five pairs and passes on them.

* Stop the confirmation on a measured deadline, not on a prediction

Sizing the retry from the first sample was an estimate dressed as a bound.
It reads the BEST big leg and the MEDIAN ratio, and those come from
different pairs: a sample of 59s, 59s and 1s big legs against 1s small legs
offers a one-second pair cost alongside a ratio of 59, so it authorises all
seven pairs, and if the confirmation's own legs then arrive at 40s the run
overruns --timeout=330 exactly as before.

growth() takes a budget_s that bounds the whole call, checked on the pair
boundary so a stopped sample still holds a whole number of ratios.
abort_over_s bounds one big leg and budget_s bounds the sample; neither
implies the other, which is the gap this closes. A confirmation cut short
already fails through the pairs_completed assertion, so the verdict arrives
as a sentence naming the unfinished sample rather than as the runner's
timeout, which is the whole point.

The pre-sizing stays, demoted to what it is: it refuses a retry that
obviously cannot fit and keeps a green run cheap. One row for the exact
mixed-estimate shape, pinning that the loop gives up after three of the
seven pairs it was authorised.

* Ask the budget before a pair runs, not after it has

A bound tested once a pair is already home is not a bound on that pair.
Three 59-second pairs under a 120-second budget all run, because each one
is inside the budget at the moment its predecessor finishes, and the sample
costs 177s. With a first sample of 39s, 59s and 59s big legs -- which sizes
the retry at three pairs, since the estimate reads the best leg and the
median ratio from different pairs -- the whole test reaches about 334s and
--timeout=330 kills it before the assertion, which is what the budget was
added to prevent.

The loop now reserves room for another pair at the cost of the worst pair
it has seen in this sample, and stops when that no longer fits. Worst
rather than mean because contention only adds. On the shape above it stops
at two pairs, the confirmation spends 118s of its 120, and the test lands
around 275s.

One row for that shape, asserting the pair count, the message, and that the
whole test stays under the runner's timeout. The existing mixed-estimate
row now stops at two pairs rather than three, for the same reason.

* Count the first sample against the budget it is part of

The retry got a fresh 120s no matter what the first sample had already
spent. The first sample has no bound of its own past the 60s per big leg,
so a scheduler stall in one of its small legs can burn most of the runner's
patience before the re-measurement is even considered, and a confirmation
counted fresh from there overruns whatever was left.

confirm_budget_s becomes total_budget_s and covers the whole call, first
sample included. The retry is sized and bounded by what remains, and the
message says how much that was. 240s against --timeout=330, leaving margin
for collection and the rest of the test body.

Two rows retuned rather than rewritten, since deducting the first sample
changes what each affords. The partial-affordability row now uses 30s big
legs, which put 90s into the first sample and leave four pairs of the
remaining 150. The before-the-pair row uses 13s / 20s / 20s big legs, which
cost 56s and authorise all seven, and its confirmation's 59s legs stop at
three for 177s of the 184 left; asked after the pair instead, the fourth
would have run to 236 and blown a bound it was never tested against while
inside it.

* Put the first sample inside the budget it belongs to

total_budget_s documented a whole-call guarantee and was only ever handed
to the confirmation. Contention landing in the SMALL legs is what that
misses: 50s small against 55s big reads as a ratio of 1.1, comfortably
under the tolerance, so nothing reaches the retry branch where the budget
was enforced, and three of those pairs run to 315s past --timeout=330 while
returning a cheerful pass.

The first growth call now takes the budget too, and a sample that stopped
short is not accepted as a verdict: two pairs is not the reading the caller
asked for, so it fails saying so rather than reporting a median of whatever
it managed. The per-leg backstop is still checked first, so a path with one
enormous leg keeps its own clearer message.

One row for that shape, asserting the pair count, the message, and that the
call stays under the runner's timeout.
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.

1 participant