Skip to content

Studio: skip the startup manifest migration scan when nothing changed since a clean pass - #16270

Merged
danielhanchen merged 7 commits into
mainfrom
perf/r14-startup-maint
Nov 1, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
perf/r14-startup-maint

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Studio: skip the startup manifest downgrade scan when nothing changed since the last clean pass

What changes

Every backend start runs migrate_ordinary_v2_manifests_for_downgrade() inside the lifespan, before the server answers anything. It globs every Hub download manifest, stats each one and reads a 256 byte prefix to find ordinary v2 records to rewrite as v1. On a home with thousands of manifests that is the largest single lifespan step, and it competes for the GIL with the hardware detection thread, so it costs far more in the server than in isolation (about 65 us per file in the server vs 12 us standalone).

The scan now records a clean pass and skips itself on the next start when nothing it depends on has changed:

  • After a complete pass that found no ordinary v2 record, it writes hub-state/manifest_migration.json with the mtime of manifests/ and of each cache-* scope dir, the active hub cache (canonical and raw), the manifest versions and a migration id.
  • On the next start, if the marker matches the current mtimes and settings, the scan is skipped. That costs one stat per scope dir.
  • Every manifest write goes through tmp + os.replace, so any added or replaced manifest moves the mtime of the dir holding it. A new scope dir moves the parent's. So any download, any manifest written by another Studio version, a cache setting change or a bump of _MANIFEST_MIGRATION_ID / _MANIFEST_VERSION re-runs the full scan.
  • No marker is written when the pass migrated something, hit a write or read failure (an unreadable manifest or a cache-* scope that cannot be listed), stopped at the 10,000 file cap, or saw an ordinary v2 record it left alone. It is also skipped when the dirs changed in the last 2 s, so a write landing in the same mtime tick as the scan cannot be missed. In that case the next start scans as before.
  • The marker lives beside manifests/, not inside it, so writing it does not move the mtimes it records.

The migration still runs synchronously in the same place in the lifespan, before any download can start, so no write moves into a window where it could race a download.

Results, base vs head

run.py --api-only on a scratch Studio home with 5000 manifests (10% v2 on first start), 1000 chat threads, 4000 messages and 200 research runs, CPU only, empty scratch HF cache. Interleaved base/head, separate home copy per arm. Medians [range], ms.

5000 manifests base head
migration step, warm (5 runs) 339 [316-481] 3 [2-3]
lifespan, warm 506 [473-652] 147 [143-150]
spawn to ready, warm 2334 [2213-2558] 1973 [1891-2037]
spawn to first /api/health done, warm 3727 [3562-3789] 3197 [3110-3245]
login, warm 52 [42-83] 45 [41-66]
spawn to ready, first start on a fresh copy (3 runs) 2975 [2765-4981] 2806 [2768-2960]

Every warm head run is faster than the fastest warm base run. The first start does the full scan on both sides, as expected.

500 manifests base head
migration step, warm (4 runs) 50 [35-57] 3 [1-44]
lifespan, warm 193 [174-216] 155 [144-175]
spawn to ready, warm 2040 [1981-2246] 1930 [1905-2192]

The 44 ms head run at 500 is the first start after a start that migrated 50 manifests. That pass is not recorded, so the next one scans again and then writes the marker.

Other startup maintenance measured on the same home and left alone: studio.db schema check plus research claim backfill (open_wal_keeper) 11-14 ms warm, orphan reconciliation under 7 ms.

Tests

  • New tests/test_manifest_migration_marker.py, 18 tests (one POSIX-only, skipped on Windows). Head passes 18/18 on Linux and macOS, 17 + 1 skipped on Windows.
    • Manifest bytes after startup match the unmarked scan on a home with v1, ordinary v2 (scoped and unscoped), dataset v2 and a dataset completion record. A fresh home with no manifests dir also works.
    • The second start reads no manifest. This fails on main, where 10 manifests are read again.
    • The scan re-runs for each of these: a new v2 manifest, an overwritten manifest name, a new cache scope, a migration id change, a manifest version change, an active cache change and a corrupt marker.
    • No marker is written after a pass that migrated something, after recent dir changes, after a write failure, or at the file cap.
  • Neighbour suites (35 files touching manifests or hub state), base vs head: 2354 passed on both sides, with the same 13 failing IDs on both. Those are in test_cached_gguf_routes, test_export_offline_window, test_hub_download_http_size_ceiling and test_mtp_drafter_companion, and they predate this change.

#- A marker that cannot be read (including deeply nested JSON that raises RecursionError) or any unexpected error in the marker check falls back to the full scan and writes no marker, so a bad marker can never block the migration.

  • What a skipped migration would cost: no code on main writes an ordinary v2 manifest, so only pre-feat(studio): rework train page setup flow #7633 builds or another Studio version sharing the home can leave one. The migration only matters for a later downgrade to a v1-only version, which would then miss one partial download; the current version reads both. Every Studio writer goes through tmp + os.replace, which moves the folder mtime.

Not covered

  • Windows and macOS were not run. The skip relies on a directory's mtime moving when an entry is renamed into it. That holds on ext4, APFS and NTFS. On a filesystem that does not maintain directory mtimes (FAT/exFAT under Windows), a v2 manifest written by another Studio version could go unmigrated until the next download changes the dir.
  • No GPU host. The cost under contention depends on what the hardware detection thread is doing at that moment.
  • The one-time cost of actually migrating many v2 manifests (about 3 ms each, from the fsync per rewrite) is unchanged.

Follow-ups in this PR

  • A manifest that cannot be read during the pass (for example a Windows sharing violation) now keeps the pass unclean instead of being treated as not v2.
  • Migration candidates are listed with os.scandir per directory instead of Path.glob, which silently returns nothing for a directory it cannot read. A cache-* scope that cannot be listed blocks the marker while the other scopes still migrate. Matching stays case-insensitive on Windows, as glob was.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 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 ⚠️ Failed 2026-11-01T11:42:04.739102Z
We couldn’t complete this review. Comment @codex review to try again.
43d713d Draft marked ready
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 334d8bf73f

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

Comment on lines +743 to +744
# Any ordinary v2 record makes the pass unclean, migrated or not.
clean = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid recording a clean pass after manifest read failures

If an ordinary v2 manifest temporarily cannot be opened, _read_migration_payload() catches the OSError and returns None, so the scan continues without setting clean = False. Once the directories are old enough, this records a clean marker despite leaving that manifest unmigrated. Restoring read access (for example, with chmod) does not change the containing directory's mtime, so subsequent startups skip migration indefinitely until another manifest write invalidates the marker. Distinguish read failures from non-v2 payloads and prevent marker creation after a failed read.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9338e57: OSError now propagates from the payload reader, so the per-file handler marks the pass unclean and no marker is written; regression test added.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9338e576a1

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

Comment on lines +803 to +805
if clean:
try:
_record_clean_migration(parent, before, started_ns)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent clean markers after directory enumeration failures

If an existing cache-* scope temporarily lacks directory-listing permission, _migration_manifest_paths() silently yields no files from it because Path.glob() suppresses the permission error. The fingerprint can still stat that directory, so an otherwise settled pass writes a clean marker despite leaving its v2 manifests unmigrated. Restoring permissions does not change the directory mtime, and subsequent startups therefore skip migration indefinitely. The file-read fix does not cover this path: the directory enumeration fails before _read_migration_payload() is called. Use enumeration that propagates failures, or explicitly verify each scope was successfully enumerated before recording the pass.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 16bf40b: enumeration now uses os.scandir per directory, a cache scope that cannot be listed is recorded and blocks the marker while the other scopes still migrate; regression tests added (monkeypatched and real chmod).

@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: 16bf40b488

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

pre-commit.ci run

# Conflicts:
#	studio/backend/hub/utils/download_manifest.py
@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. Nice work!

Reviewed commit: 95703a34e0

ℹ️ 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 marked this pull request as ready for review November 1, 2026 11:41
@danielhanchen
danielhanchen merged commit f6f550d into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the perf/r14-startup-maint branch November 1, 2026 11:42
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 5, 2026
…scan reads (unslothai#16556)

## What changes

Follow-up to unslothai#16270. The downgrade migration scan
(`_migration_manifest_paths`) matches `cache-*` scope folders on the
case-folded name on Windows, as pathlib's glob does. The fingerprint
that decides whether a later start can skip that scan
(`_migration_fingerprint`) matched `cache-` case-sensitively. A scope
folder whose on-disk name is spelled differently (for example
`Cache-...`) was therefore scanned but never fingerprinted: a v2
manifest written into it after a clean pass moved no recorded mtime, and
later starts skipped it.

The fingerprint now uses the same name match as the scan (one `_folded`
helper shared by `_listed` and the fingerprint), and the `os.scandir` it
uses is closed with a `with` block. On Linux and macOS nothing changes
(no folding there, same as the scan). Studio itself only creates
lowercase `cache-<digest>` folders, so this only matters for a folder
created or renamed outside Studio.

## Tests

- New `test_scope_matched_only_when_folded_is_fingerprinted` (folding
forced on so it runs on every OS): after a clean pass with a
`Cache-Hand` scope, the marker records `Cache-Hand`, and a manifest
written into it later is read by the next start. It fails on main
(`'Cache-Hand' not in` the recorded dirs) and passes here.
- `tests/test_manifest_migration_marker.py`: 21 passed.
- Every backend test file importing `download_manifest`: 1929 passed, 1
skipped, and the same single failure as main
(`test_cached_gguf_routes.py::test_pipeline_class_guard_passes_every_shipped_family`,
unrelated).

## Not covered

- Not run on a real Windows host; folding is simulated by patching
`_folded`. The scan side already folds on Windows on main.
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