Skip to content

Cover the restore of every variable the install.ps1 handoff block saves - #10566

Merged
danielhanchen merged 15 commits into
mainfrom
fix-main-red-handoff-anchors-and-crash-order
Sep 10, 2026
Merged

danielhanchen merged 15 commits into
mainfrom
fix-main-red-handoff-anchors-and-crash-order

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Follow-on to #10538. Not a fix for anything red: main is green, and this is coverage the runtime tests there do not yet have.

What already landed

This PR originally fixed two failures that were red on a clean main. Both were fixed first, by other work, and those fixes are on main now:

Both reached the same conclusions this PR had: install.ps1 was not at fault, the harness was. Everything this PR carried for those two defects has been dropped. What is left is one file and one idea.

What this adds

The handoff block saves and restores fifteen caller-shell variables. On main:

  • test_the_bail_restores_the_caller_environment asserts two of them, STUDIO_PACKAGE_NAME and SKIP_STUDIO_BASE.
  • test_the_caller_environment_survives_the_setup_call asserts none of them. It checks the ROCm handoff and the public override only.

So twelve of the fifteen have no restore assertion anywhere. Each one leaks into the caller's shell, which irm ... | iex makes the user's own terminal, and that leak is the failure this file exists to catch.

The other half was missing too. Nothing seeds those variables in the child environment, so only the arm of the finally that removes a variable ever runs. The arm that puts a caller's prior value back was unexercised for all fifteen.

Four changes, one file.

The block is sliced from the first save rather than from the ROCm one. Five pairs are saved above that point: SKIP_STUDIO_BASE, UNSLOTH_STUDIO_HOME, UNSLOTH_TAURI_MODE and the two private handoffs. The harness was supplying their $previous* and $hadPrevious* itself, so their restores were measured against harness constants and a save-side bug could not show at all. All fifteen are now shipped code on both halves. This is the change with teeth, see the mutation table below.

Presence of the caller variables is a parameter, crossed with the existing setup_ok / setup_throws split and applied to the bail. The absent arm asserts Test-Path Env:NAME is false, not an empty or null value. PowerShell 7.5+ keeps a variable present when it is assigned "", so a restore writing "" instead of removing would satisfy a value comparison while leaving the caller holding a variable it never had.

The presence patterns cover every ORDERED pair, not every pair. All-present and all-absent run both arms of the finally, but they run the same arm for all fifteen at once, so a restore consulting another variable's $hadPrevious flag still lands on the right branch by luck. Separating each pair with binary masks was the obvious next step and it was not enough. Substituting a flag is a directed edit: if ($hadPreviousSkipStudioBase) becoming if ($hadPreviousTauriMode) still assigns $previousSkipStudioBase, so it is observable only where SKIP_STUDIO_BASE is present and UNSLOTH_TAURI_MODE is absent. The other direction takes the then-arm and assigns the saved $null, which removes the variable exactly as the correct code does. A mask supplies "A present, B absent" only when index(A) is not a submask of index(B), and index 0 is a submask of everything, so SKIP_STUDIO_BASE was absent from every mask and present only in all, where everything else is present too. Adding the complement of each mask reverses the ordering of every pair a mask splits, and the property test now asserts the directional form. Ten patterns, not 2**14.

Sentinels are distinct per variable, so a finally that restored every variable from the wrong save is caught rather than passing.

A new test checks the table against the source, from the restore side as well as the save side, so a sixteenth save becomes a named failure rather than a silent gap even when it is prepended above the slice anchor. UV_CACHE_DIR, TMP and TEMP stay out of scope and the test says why: they are saved and restored hundreds of lines outside this block, so covering them means slicing most of install.ps1 and stubbing the venv build, the torch install and the llama.cpp fetch.

What is kept from #10538

The saved < bail < restored offset assertion stays exactly as it is. It is a cheap companion to the runtime tests rather than a substitute, and its own docstring already makes the right argument: ordering cannot prove the finally runs, which is why the runtime bail test sits beside it. Nothing there is replaced.

The two variables its bail test named individually are now two of the fifteen the shared helper checks, in both directions instead of only for removal. That is a strict superset of what it asserted.

Verification

Pinned origin/main at 6b26c24fd8290a1574fc8492d8e5b12c2191fbcb and compared against a control worktree at that same SHA. No earlier result is carried forward; the baseline moved.

tests/test_windows_amd_gpu_scan_fallback.py + tests/test_deliberate_crashes_suppress_cores.py
  control: 156 passed
  branch:  249 passed

Collected node ids, repo root (tests unsloth unsloth_cli) unioned with studio/backend (tests hub/tests): 57,977 on the pinned control, 57,983 on this branch. Every id the control has and the branch does not is equally absent from this branch's own parent commit, so none of them is a deletion by this PR; they are files main gained after this branch's base.

Mutation-checked against that base. Every count below is the whole changed test file run against one mutated install.ps1.

Mutation Result
each of the 15 saves deleted 15 of 15 rejected
each of the 15 restores deleted 15 of 15 rejected
each of the 15 remove arms writes "" instead of removing 15 of 15 rejected, on PowerShell 7.6.0
every directed substitution of one $hadPrevious flag for another, across all 15 210 of 210 rejected, including the 28 that involve the ROCm handoff
UNSLOTH_TAURI_MODE save always reads $null 51 of 176 fail here, and 83 passed on main with the same mutation applied
a new save added to the block the coverage test names UNSLOTH_BRAND_NEW_KNOB

The fourth row is what the ordered-pair change buys. With unordered separation only, 139 of the 182 directed substitutions between the fourteen masked variables were rejected and 43 survived, all of them into the restore of a variable no pattern made present while the substituted flag's variable was absent, and thirteen of them in SKIP_STUDIO_BASE's own restore. The whole file passed 136 tests against an install.ps1 whose SKIP_STUDIO_BASE restore read the tauri flag. It now fails 10 of 176 on that same mutant.

The fifth row is the one that answers "is this worth having". A genuinely broken save is invisible to the tests on main and caught here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 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-10T12:01:08.120898Z 00dd7b0 New commits
🔒 Security Review ✅ Completed 2026-09-10T06:58:14.874274Z 849cccb New commits
ℹ️ 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

#10538 caught the handoff harness up with the block and added a runtime bail test.
That test asserts two variables, STUDIO_PACKAGE_NAME and SKIP_STUDIO_BASE, and
the setup-call cases assert none: they check the ROCm handoff and the public
override only. The block saves and restores fifteen. Twelve had no restore
assertion anywhere, and each is a variable that leaks into the caller's shell,
which `irm ... | iex` makes the user's own terminal.

The other half was missing too. Nothing seeded those variables in the child env,
so only the arm of the finally that REMOVES a variable ever ran. The arm that puts
a caller's prior value back was unexercised for all fifteen.

Three changes.

The block is sliced from the first save rather than from the ROCm one. Five pairs
are saved above that point, and the harness was supplying their $previous* and
$hadPrevious* itself, so their restores were measured against harness constants
and a save-side bug could not show at all. Now all fifteen are shipped code on
both halves.

Presence of the caller variables is a parameter, crossed with the existing
setup_ok / setup_throws split and applied to the bail. The absent arm asserts
Test-Path Env:NAME is false, not an empty or null value: PowerShell 7.5+ keeps a
variable present when it is assigned "", so a restore writing "" instead of
removing would satisfy a value comparison while leaving the caller holding a
variable it never had.

Sentinels are distinct per variable, so a finally that restored every variable
from the wrong save is caught rather than passing.

The table is checked against the source, so a sixteenth save becomes a named
failure instead of a silent gap. UV_CACHE_DIR, TMP and TEMP stay out of scope and
the test says why: they are saved and restored hundreds of lines outside this
block, so reaching them means slicing most of install.ps1.

Kept as they are: the save < bail < restore offset assertion from #10538, which is
a cheap companion to the runtime tests rather than a substitute, and its own
docstring already says ordering cannot prove the finally runs. The two variables
its bail test named individually are now two of the fifteen, checked in both
directions instead of only for removal.

Mutation-checked against this base. Writing "" instead of removing
UNSLOTH_SETUP_PYTHON fails nine absent cases and no present case. Dropping the
UNSLOTH_NO_TORCH restore fails both arms. Restoring UNSLOTH_NO_TORCH from the
STUDIO_PACKAGE_NAME save fails nine present cases, the cross-wiring only distinct
sentinels can see. Breaking the UNSLOTH_TAURI_MODE save fails nine present cases
here and is invisible on main, where all 83 pass with it broken.
@danielhanchen
danielhanchen force-pushed the fix-main-red-handoff-anchors-and-crash-order branch from 5f1a342 to 2f6f32f Compare September 9, 2026 01:58
@danielhanchen danielhanchen changed the title Fix the two test files that are red on a clean main Cover the restore of every variable the install.ps1 handoff block saves Sep 9, 2026
…ccess path

Two gaps in the coverage this branch added.

Presence was one flag for all fifteen variables, so the suite only ever ran
all-present or all-absent. Distinct sentinels catch a restore reading the wrong
variable's VALUE, but not one reading the wrong variable's $hadPrevious FLAG:
with every flag the same, that bug picks the right branch by luck. Presence is now
a pattern, and two complementary mixed ones run alongside all and none, so no
variable is always on the same side as any other. Not exhaustive on purpose, 2**15
would be a worse test rather than a better one.

Mutation-checked. Pointing the UNSLOTH_NO_TORCH restore at
$hadPreviousStudioPackageName while leaving its value correct fails ten mixed
cases and passes every all-present and all-absent case, which is the shape of bug
the mixed patterns exist for. It also passes the complementary mixed pattern,
where the wrong flag is true and the value restored is $null, and assigning $null
to an env var removes it, so the accident lands on the right answer.

--with-llama-cpp-dir was only ever given a path that does not exist. The success
path resolves it into UNSLOTH_LOCAL_LLAMA_CPP_DIR, and that is the only assignment
to that variable in the block, so nothing reached it: the bail returns above it and
every other case passes $null. Its restore was being satisfied by never being
dirtied. A new case passes a directory that is there, asserts the block runs on to
the setup call rather than bailing, and asserts the restore.

Mutation-checked too. Deleting the UNSLOTH_LOCAL_LLAMA_CPP_DIR restore leaves all
thirty-two setup-call cases and all four bail cases green and fails only the four
new ones.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Three follow-ups.

The presence patterns were even/odd complements, which do not do the job they
claim. Two variables of the same parity are present together in one pattern and
absent together in the other, so a restore consulting the wrong variable's
$hadPrevious flag still picks the right branch for them in every case. Cross-wiring
between indices 0 and 2 was invisible.

Replaced with a binary encoding: pattern k keeps the names whose index has bit k
set. Any two distinct indices differ in some bit, so every PAIR is split by at
least one mask, which is the condition under which a wrong flag produces a wrong
branch. Fifteen names need four masks. Six patterns in total, and the exhaustive
claim is over pairs, which is the failure mode, rather than over subsets. A new
test asserts the separation property directly, so adding a sixteenth name reports
the shortfall rather than quietly thinning the coverage.

Demonstrated rather than argued. Cross-wiring UNSLOTH_TAURI_MODE (index 2) to
SKIP_STUDIO_BASE (index 0), same parity, value left correct: the old scheme passes
all 32 cases, the masks fail 8, all of them mask1, which is the mask that separates
those two indices.

The coverage guard had a hole of its own. It read the save side of a slice that
starts at whichever save is first today, so a save PREPENDED above the anchor fell
outside the slice and was invisible to the very guard meant to catch that drift.
The guard now also reads the restore side, which cannot escape because the finally
is inside the slice by construction: a prepended save appears as a variable the
block restores but never seems to save. Anchoring on the enclosing function was the
alternative, and it is no more stable than the current anchor while being harder to
read, so the check was made anchor-independent instead of the anchor made clever.

The llama.cpp success case asserted only that the block ran past the bail, not that
the child received anything. It now asserts the resolved path the child would
inherit, read at the setup call rather than after the finally.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2bd9d040cd

ℹ️ 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".

Separating each pair of caller variables was not enough. Substituting a
flag in the finally is a directed edit: `if ($hadPreviousSkipStudioBase)`
becoming `if ($hadPreviousTauriMode)` still assigns
$previousSkipStudioBase, so it is only observable when SKIP_STUDIO_BASE
is present and UNSLOTH_TAURI_MODE is absent. In the other direction the
then-arm assigns the saved $null, which removes the variable exactly as
the correct code does, and nothing changes.

The binary masks could not supply that direction for every pair. A mask
gives "A present, B absent" only when index(A) is not a submask of
index(B), and index 0 is a submask of everything: SKIP_STUDIO_BASE was
absent from every mask and present only in "all", where every other name
is present too. Mutation testing the 182 directed substitutions between
the fourteen masked variables killed 139 and left 43 alive, thirteen of
them in SKIP_STUDIO_BASE's own restore, and the file passed 136 tests
against an install.ps1 whose SKIP_STUDIO_BASE restore read the tauri
flag.

Add the complement of each mask, which reverses the ordering of every
pair the mask splits, and assert the directional property instead of the
symmetric one. All 182 substitutions are now detected, as are the 28
that involve the ROCm handoff, for 210 of 210 over all fifteen flags.
The properties that already held are unchanged: 91 of 91 unordered pairs
still separated, all 15 save deletions and all 15 restore deletions
still rejected, and all 15 restores that write "" instead of removing
still rejected on PowerShell 7.6.0.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

shimmyshimmer and others added 2 commits September 9, 2026 09:55
Two holes left in the handoff lifecycle coverage, both found by mutating
install.ps1 and watching the suite stay green.

UNSLOTH_STUDIO_HOME and STUDIO_LOCAL_REPO are the only two saved variables
the try does not always assign: the first is set only under redirect mode
'env', the second only under --local, and every other mode removes them
instead. The harness hard-coded redirect mode 'none' and no local install,
so for those two the try left the variable ABSENT and the finally's
Remove-Item arm had nothing to undo. Deleting that arm outright was
invisible, while the same deletion on any of the other thirteen is caught
at once. A new case drives the modes that really do set them, on both the
success and the failure path, and asserts the child saw each value so the
case cannot quietly stop dirtying them and go green again on an
undirtied restore.

Second, the save-order check compared SETS, so moving a save from above
the try down next to its assignment still matched. Nothing at runtime
catches that either: the injected failure is the setup call and the bail
is the missing directory, both below where such a save would land. The
case that bites is a throw above it, leaving $hadPrevious unbound and the
finally removing a value the caller owned. The order check now covers all
fifteen saves and their flags rather than the ROCm handoff alone.

Mutation over the block, 257 mutants, survivors 0. Before: deleting the
else-arm Remove-Item survived for exactly those two variables (13/15
killed), and moving a save inside the try survived for 14 of 15.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Provided git ref 43981a51af2f3bdccf7a120d645c070fe9915b25 does not exist
ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from oobabooga Sep 9, 2026
@unslothai unslothai deleted a comment from oobabooga Sep 9, 2026
@unslothai unslothai deleted a comment from oobabooga Sep 9, 2026
@unslothai unslothai deleted a comment from oobabooga Sep 9, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 43981a51af

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 4db0bbdf96

ℹ️ 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".

Adding caller_env to _run_handoff_lifecycle gave it a default of "all", and
test_only_this_runs_arch_is_handed_to_the_child took that default silently. Before
this branch the helper had no such parameter, so that test ran with no caller
variables set at all. The default quietly replaced the empty-caller case, and the
empty-caller case is the one with teeth.

Shown with a mutant that makes the stale-handoff clear depend on an unrelated
variable's $hadPreviousTauriMode flag:

  base                      1 failed
  this branch before        3 passed     the defect goes through
  this branch after         5 failed

Under caller_env="all" the Tauri variable is present, so the wrong flag reads true
and the clear happens anyway. Only a pattern that leaves it absent exposes it.

Unmutated: 224 passed, up from 197.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 10, 2026
@danielhanchen
danielhanchen merged commit 2bd5dab into main Sep 10, 2026
37 of 38 checks passed
@danielhanchen
danielhanchen deleted the fix-main-red-handoff-anchors-and-crash-order branch September 10, 2026 12:58
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