Skip to content

Studio: repair duplicate package metadata during updates - #8515

Merged
danielhanchen merged 60 commits into
unslothai:mainfrom
oobabooga:fix/studio-duplicate-metadata-integrity
Aug 23, 2026
Merged

danielhanchen merged 60 commits into
unslothai:mainfrom
oobabooga:fix/studio-duplicate-metadata-integrity

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Problem

A successful Studio update can report files as missing when stale and current .dist-info directories remain for the same package. The reported files may simply have been removed in the newer release. Duplicate metadata can also make version checks select a stale release.

Root cause

Studio assumed each normalized package name had one metadata directory. Python's single-version lookup selects the first matching record, while Studio's integrity check read every matching RECORD. A stale record could therefore look like installation damage.

Fix

  • Group installed metadata by canonical package name and treat multiple records as a conflict instead of selecting one arbitrarily.
  • Skip file verification for an ambiguous package while preserving its path ownership for checks on other packages.
  • Make installer manifests and shell and PowerShell fast paths reject duplicate metadata.
  • Repair managed core packages with a targeted dependency-free reinstall, verify that one record remains, and preserve the installed Torch stack.
  • Fail only for conflicts in Studio's core packages, warn when other packages are ambiguous, and avoid unsafe generic reinstall commands.

Testing

  • Studio installer suite (1,771 passed, 26 skipped)
  • Cross-platform dependency and no-Torch suites (158 passed, 17 skipped)
  • Focused metadata repair, manifest, and update verification suites (125 passed)
  • Ruff, bash -n studio/setup.sh, and git diff --check

Live validation

GitHub Actions reconstructed the stale 2026.8.12 and current 2026.8.15 metadata state on native Ubuntu and Windows runners. The base commit reproduced all eight false missing-file reports on both systems. This branch reported the metadata conflict instead, completed the production repair, and finished with one metadata record and a clean file scan.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 54bcc05914

ℹ️ 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[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

Reviewed this on Linux against merge base 4acc36b1071166645e48973dd7cdd37f947fd9a2 and head dd4aa9213b693674ebd84057ad65dd1d926e5457. The fix does what it says: I reproduced the false missing-file report on the base commit and confirmed it is gone on this branch, with the metadata conflict reported in its place.

Reproduction

Throwaway venv, a package whose current 2.0 release dropped a file the stale 1.0 record still names, with both .dist-info directories present. Same venv, same interpreter, only the tree that supplies the check changes:

BEFORE (4acc36b)  damaged: ['unsloth: unsloth/old.py is missing']
AFTER  (dd4aa92)  damaged: []
                  conflicts: ['unsloth: multiple metadata records (1.0 at unsloth-1.0.dist-info, 2.0 at unsloth-2.0.dist-info)']

Through the post-update guard, which is what the user actually sees:

######## BEFORE (merge base) ########

Update finished, but some installed files are damaged:
  unsloth: unsloth/old.py is missing

An update cannot repair these. pip sees intact package metadata and
reinstalls nothing, so Studio will keep failing to start. Reinstall
over the top:
  curl -fsSL https://unsloth.ai/install.sh | sh
...
[exit code 1]

######## AFTER (this branch) ########

Update finished, but Studio package metadata is inconsistent:
  unsloth: multiple metadata records (1.0 at unsloth-1.0.dist-info, 2.0 at unsloth-2.0.dist-info)

The file check cannot safely choose between these records.
The installer could not repair its managed package metadata.
Recreate the managed environment before running the Studio installer again.

To update anyway without this check: unsloth studio update --no-verify
[exit code 1]

The old advice was the actively harmful part: "reinstall over the top" cannot fix a stale record, so the user loops.

Other things that check out:

  • Fast path. With a duplicate planted, the base probe prints 2.0 and exits 0, so setup.sh declares the package up to date and skips the dependency pass that would repair it. The new probe exits 2 and forces the repair.
  • _venv_site_packages. A venv carrying a leftover lib/python3.11/site-packages from an in-place upgrade: the base merges both trees, this branch returns only the active interpreter's directory, so an ordinary package is no longer seen as duplicated. With the interpreter removed from bin/, it still lands on the single-candidate fallback rather than failing.
  • Metadata variants. Same-version duplicates, a METADATA with no Name, and a METADATA with invalid UTF-8 all come back as a conflict from install_manifest.installed_versions, with the malformed record named by invalid_metadata_paths. A single clean record stays clean, so the check does not fire on a healthy venv.
  • Repair convergence. _repair_duplicate_core_metadata on a real planted duplicate goes from two records to one and returns True.
  • Tests. The touched suites pass at head (146 passed, 11 skipped, plus 55 in unsloth_cli/tests). Copied onto the merge base they produce 43 failures, so they pin the new behaviour rather than restating the old. The full CPU run is 6126 passed, 137 skipped, with 5 failures and 8 errors in test_model_picker_contracts, test_remote_connection_models_contract, test_chat_thread_title_cas and test_torchao_nf4tensor_move; those reproduce identically on the merge base here, so they are this box, not the branch.

One thing worth changing

_repair_duplicate_core_metadata uninstalls every record before it installs anything, and the reinstall goes through pip_install, which routes failure into run() and sys.exits. So a failure between the uninstall loop and the reinstall leaves the venv with the package gone, and the function's own return False (and the return 1 at the call site) never runs. With the index unreachable:

before: installed_versions('demo') = ['1.0', '2.0']
  deps           duplicate metadata for demo detected; reinstalling it
  error          Repairing duplicate metadata for demo failed (exit code 1)
ERROR: No matching distribution found for demo

_repair_duplicate_core_metadata -> SystemExit(1)
after:  installed_versions('demo') = []

For unsloth itself that also takes the unsloth entry point with it, so the CLI the user would reach for next is gone, and nothing in the output says the package was removed. The starting state was ambiguous but working; a flaky moment turns it into an empty venv.

Two ways out, either is fine:

  • Use the existing non-fatal pip_install_try for this one call and return False on failure, so the caller's return 1 runs and the message can say the package now needs a reinstall.
  • Or download or resolve the replacement before the uninstall loop, so the destructive step only happens once the replacement is in hand.

Smaller note

_studio_deps.installed_metadata_conflicts groups on dist.metadata.get("Name") and drops records that have no name, so a nameless or invalid-UTF-8 .dist-info is invisible to it, while install_manifest.installed_versions and _distributions_in both treat the same record as a conflict:

variant=nameless   local installed_metadata_conflicts: []
                   foreign _distributions_in conflicts: ['unsloth']
                   install_manifest conflict: True ['unsloth-1.0.dist-info']

Low impact, since damaged_installed_files ignores those records too and the repair removes them earlier in the same update, but the three detectors disagreeing on the same directory will be confusing later. The filename fallback already in install_manifest._installed_metadata_records would close it.

Codex threads

All 14 look addressed by the later commits. I verified the ones that are checkable on Linux: every stale record removed before reinstall, the active-directory-only foreign scan, malformed and nameless records isolated per distribution and treated as conflicts, malformed metadata removed directly rather than handed to pip, the editable foreign requirements probe, and the CI and local overlay provenance being restored after repair. The Windows launcher one I could not exercise here; the _WindowsLauncherUpdateTransaction now wrapping setup() is the right shape for it.

Nothing blocking apart from the uninstall-before-reinstall gap. Nice fix.

purelib hardcodes lib while platlib follows sys.platlibdir, and venv creates
lib64 as a symlink to lib. On Fedora and SuSE the two schemes are therefore two
names for one directory, and deduplicating the scan roots by string kept both.

installed_versions() then returned every record twice, so metadata_conflict()
was true for every installed package in a perfectly healthy environment: update
would exit 1 telling the user to recreate the venv, and the repair could never
converge because the duplication was an illusion.

Deduplicate on the real path instead.
danielhanchen and others added 2 commits August 23, 2026 02:18
_rewrite_minimal_metadata already refuses a record with no RECORD, because
nothing then knows which files that release owned. The repair only honoured
that when no other record survived. With one surviving, the count stayed
nonzero, pip uninstalled just the readable record, and the quarantine was
discarded on success, so whatever the older release owned alone stayed
importable and the directory that was the evidence was deleted for good.

Measured before the change: repair returned True, the stale module was still
importable, and the stale dist-info was gone. Now the repair stops with the
directory untouched, so a later run can still see the conflict.

Four tests built their malformed record without a RECORD and asserted the
repair continued, which is the behaviour above. They use an unversioned
directory name instead, so the rewrite still fails and the quarantine path
they exist to cover is unchanged. Reported by review.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The previous commit only covered records with no RECORD. Any unrewritable
record leaks the same way: quarantining it hides it, pip removes just the
readable records, the quarantine is discarded once the reinstall succeeds,
and whatever that release owned alone stays importable while the repair
reports success and deletes the directory that was the evidence.

The reachable route is a METADATA that exists but cannot be read, as an
elevated install leaves root-owned. Measured in a real venv with the file
unreadable: repair returned True, the stale module was still importable and
its dist-info was gone. It now stops with the tree as found. An unreadable
METADATA that can be rewritten still repairs, as does an ordinary duplicate.

Quarantine-and-proceed had no remaining purpose once nothing runs after an
unusable record, so it is gone and its three tests collapse into one that
pins the invariant. Quarantine itself is still used for pip's ~ leftover,
which is what the restore-on-staging-failure test now covers.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 23, 2026
danielhanchen and others added 2 commits August 23, 2026 02:47
When the rewritten record has already been uninstalled and a later uninstall
fails, _restore_from_staged reinstalls the package before the finally block
runs quarantine.restore(). If the staged wheel recreates the same dist-info
path, restore then either overwrote the wheel's valid METADATA with the
original corrupt bytes or, where the original was absent, deleted a record
pip had just written, leaving the core package malformed after a recovery
whose whole purpose was to make it whole.

Reproduced as a unit: the repair returned False and reading the resulting
METADATA raised UnicodeDecodeError.

A successful staged reinstall now drops the backed-up copies, since the
wheel's metadata is authoritative. The moved entries are kept, because a
record pip cannot consume still has to go back exactly as found. Reported by
review.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 23, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 0db7227abe

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

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3543c35daa

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants