Skip to content

Speed up Studio startup path - #6899

Merged
wasimysaid merged 5 commits into
unslothai:mainfrom
wasimysaid:wasim/studio-startup-speedup
Jul 8, 2026
Merged

wasimysaid merged 5 commits into
unslothai:mainfrom
wasimysaid:wasim/studio-startup-speedup

Conversation

@wasimysaid

Copy link
Copy Markdown
Collaborator

Summary

  • skip the managed Studio CLI help probe when the desktop capability cache already matches the managed binary
  • make Chat model defaults return immediately while Hugging Face top-model discovery finishes in the background
  • defer Chat local model and LoRA inventory scans until after first load or model selector open
  • avoid blocking initial React render on the platform health fetch
  • lazy load PDF, DOCX, RAG sources, and sidebar export helpers from non-critical paths

Tests

  • ~/.unsloth/studio/unsloth_studio/bin/python -m pytest tests/test_inference_default_models_non_blocking.py tests/test_startup_llama_probe_non_blocking.py -q --tb=short
  • cargo test preflight::tests::managed_cli_capability -- --nocapture
  • npm run typecheck
  • npm run build
  • npx biome check src/main.tsx
  • git diff --check

Notes

  • Non-Tauri users still go through the existing web app path. Tauri-only auth and platform refresh remain gated on isTauri and desktop auth readiness.
  • Chat-only route gating still awaits fetchDeviceType() in the root route guard.

@wasimysaid
wasimysaid requested a review from danielhanchen as a code owner July 6, 2026 10:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes startup and initial load performance across the application. On the backend, it prevents the orchestrator from blocking on remote Hugging Face discovery during startup. On the frontend, it introduces lazy loading for heavy components and libraries (such as ProjectSourcesPanel, unpdf, and mammoth), defers model inventory refreshing, and renders the app without waiting for fetchDeviceType(). In the desktop preflight, it skips the CLI help probe on a capability cache hit. A critical issue was identified in the new preflight test, where the lack of setting the UNSLOTH_TEST_DESKTOP_CAPABILITY_CACHE_HOME environment variable could lead to the test modifying or deleting the developer's actual home directory cache.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread studio/src-tauri/src/preflight.rs Resolved
@wasimysaid
wasimysaid force-pushed the wasim/studio-startup-speedup branch from c90a7e1 to 47b4107 Compare July 6, 2026 10:34
@wasimysaid

Copy link
Copy Markdown
Collaborator Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@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: 47b41072a2

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

pub(super) async fn probe_managed_bin(bin: PathBuf) -> ManagedProbe {
let started = Instant::now();
if let Some(fingerprint) = managed_bin_fingerprint(&bin) {
if read_cached_capability(&fingerprint).is_some() {

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 Recheck executable state before trusting cache

When a managed install has a valid capability cache but the binary is no longer runnable, for example after its executable bit is cleared or the launcher is quarantined without changing size/mtime, this early cache hit reports ManagedReady without the previous -h probe. The fingerprint only keys on path, size, mtime, root id, and marker data, not executable mode or launchability, so preflight skips the stale/repair path and the later backend start can fail instead; keep a cheap executability check or include launch-relevant metadata in the cache key.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch, fixed in 6f941b9. The cache fast path now confirms the managed binary is still executable before trusting the fingerprint, since clearing the executable bit does not change size or mtime. When it is not executable the probe falls back to the CLI help check and reports Stale so the install can repair, instead of returning Ready and failing later at backend start. Added a regression test that clears the executable bit after priming the cache and asserts Stale.

</StrictMode>,
);

fetchDeviceType().catch(() => undefined);

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 Ignore stale platform bootstrap responses

Starting this fetchDeviceType() after rendering lets it race with the root route/provider calls that also fetch platform info. If this early request is unauthenticated and returns after a later authenticated call (for example during Tauri auto-auth or login), fetchDeviceType still writes the global platform store using the browser fallback when /api/health omits device_type, resetting fetched and potentially overwriting the correct host platform used by Hub filtering and platform-gated UI.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch, fixed in 6f941b9. Now that first render is no longer gated on the initial device fetch, fetchDeviceType keeps an already authoritative result and no longer lets a late unauthenticated or failed non-forced response overwrite the device type, tunnel URL, or secure flag. Forced refreshes and the first unauthenticated load are unchanged, so logout and cold start still behave as before.

…nd ignore stale unauthenticated platform fetches

Preflight: a matching capability cache fingerprint no longer skips the
runnability check when the managed binary's executable bit was cleared
(size and mtime unchanged, since chmod bumps ctime not mtime). The cache
fast path now confirms the binary is still executable, otherwise it falls
back to the CLI help probe so preflight reports Stale and can repair,
instead of returning Ready and failing later at backend start. Adds a
regression test.

Frontend: now that first render is no longer gated on fetchDeviceType,
the initial unauthenticated health call can resolve after an
authenticated platform fetch. Guard the store so a late unauthenticated
or failed non-forced response cannot overwrite an already authoritative
device type, tunnel URL, or secure flag. Forced refreshes and the first
unauthenticated load are unaffected.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@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: 6f941b96ae

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

fn managed_bin_is_executable(bin: &Path) -> bool {
use std::os::unix::fs::PermissionsExt;
match fs::metadata(bin) {
Ok(metadata) => metadata.permissions().mode() & 0o111 != 0,

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 Verify executable access before trusting cache

On Unix cache hits, this treats any execute bit as launchable, but 0o111 can be set only for another owner/group (or be denied by ACLs), so the current user still gets PermissionDenied. In that cached state probe_managed_bin returns ManagedProbe::Ready without running the -h probe, which skips the stale/repair path and leaves startup to fail later when the backend is actually launched.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good point, fixed in 1bb2c54. Switched the cache executability guard from a mode bitmask to a real access(X_OK) check, which honors the calling user's uid and gid (and ACLs) rather than treating any execute bit as launchable. An ownership or permission change now correctly falls back to the CLI help probe and the Stale repair path instead of returning Ready.

A mode bitmask treats any execute bit as launchable, but the executable
bits can be set only for another owner or group, or be denied by an ACL,
so the current user could still hit PermissionDenied at launch and the
cached fast path would wrongly return Ready. access(X_OK) checks real
executability for the calling user, so an ownership or permission change
correctly falls back to the CLI help probe and the Stale repair path.
@danielhanchen

Copy link
Copy Markdown
Member

@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: 1bb2c5478a

ℹ️ 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 thread studio/frontend/src/config/env.ts Outdated
Comment on lines +96 to +101
if (
data.device_type === undefined &&
shouldKeepAuthoritativePlatform(options?.force)
) {
return usePlatformStore.getState().deviceType;
}

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 stale authenticated platform writes

This guard only preserves the newer platform state when the late response has no device_type; with a saved auth token, the post-render non-forced /api/health request can include device_type but still be older than a later forced refresh that picked up cloudflare_url/server_url/secure. If that earlier request resolves last, the setState below still overwrites the forced result with stale/null tunnel fields, which is the race this change is trying to avoid. Re-check the current authoritative state (or ignore non-forced writes) before applying any non-forced response, not just unauthenticated ones.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch, fixed in dcaac06. The guard now drops any non-forced response once the store holds an authoritative server-reported platform, not just unauthenticated ones. So an earlier authenticated request that resolves after a later forced refresh can no longer reset device type or null the tunnel URL and secure flag. Forced refreshes and the first authoritative write are unaffected.

Comment on lines +438 to +446
if managed_bin_is_executable(&bin) {
if let Some(fingerprint) = managed_bin_fingerprint(&bin) {
if read_cached_capability(&fingerprint).is_some() {
info!(
"Managed preflight: using cached desktop capability for {:?} in {}ms",
bin,
started.elapsed().as_millis()
);
return ManagedProbe::Ready { bin };

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 Keep a real launch probe before cached Ready

For cached managed installs this now returns Ready before running unsloth -h; on Unix access(X_OK) only checks permissions on the launcher path, and on non-Unix it is always true. If the cached launcher and markers are unchanged but the venv/interpreter or another runtime dependency was deleted or broken, preflight will report Ready from cache and skip the repair path even though starting the backend will fail; the previous ordering caught that with the help probe before trusting the cache.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Skipping the help probe on an unchanged fingerprint is the deliberate optimization of this change, and the fingerprint already covers the realistic breakage. marker_candidates_for_bin content hashes pyvenv.cfg, uv.lock, requirements.txt, the python interpreter, and the unsloth_cli studio command module across the venv and scripts dirs, so a deleted or recreated venv, a changed interpreter, or changed dependencies all change the fingerprint and fall back to the help probe and the repair path. The only case that escapes is a runtime dependency corrupted in place while every one of those markers keeps an identical size, mtime, and content, which no cheap metadata check can detect. Re-running the launch probe on every cache hit is exactly the subprocess this PR removes to speed up startup, so restoring it would defeat the purpose. That rare residual degrades to a normal backend start failure and repair rather than a silent wrong result, so keeping the cache fast path with the executable-access guard is the intended tradeoff. Marking this one down.

Extend the platform store guard so a non-forced health response never
overwrites an already authoritative result, not only unauthenticated
ones. With a saved token the post-render non-forced request can be
authenticated but older than a later forced refresh that already picked
up the tunnel URL and secure flag; if that earlier request resolves last
it would null those fields. Now any non-forced response is dropped once
the store holds a server-reported platform. Forced refreshes and the
first authoritative write are unaffected.
@danielhanchen

Copy link
Copy Markdown
Member

@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: dcaac06af4

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

bin,
started.elapsed().as_millis()
);
return ManagedProbe::Ready { bin };

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 Keep probing cached CLIs before reporting them ready

On a cached managed install this returns Ready before run_cli_probe(&bin, &["-h"]) runs; an executable-bit check is not equivalent to launching the CLI (for example, a Unix shim can still have X_OK while its shebang interpreter/venv python was deleted, and on Windows the new guard always returns true). In those cases the fingerprint/cache can still match, so preflight skips the failure path that used to report Stale and the app proceeds to a backend start that cannot spawn.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Holding this one, with the specific cases addressed. The deleted-interpreter case is already caught: marker_candidates_for_bin fingerprints python and python.exe, plus pyvenv.cfg, uv.lock, and requirements.txt, in both the venv and scripts dirs, so if the shebang interpreter or venv python is removed that marker drops out of the set, the fingerprint changes, the cache misses, and run_cli_probe with -h runs and reports Stale. On Windows there is no executable bit and run_cli_probe spawns the exe directly, so the guard is intentionally a no-op there. That matches this PR's behavior on Windows before the guard existed, since the cache fast path already applied on all platforms, so it is not a regression introduced by the executability check. The only case that still escapes both the fingerprint and access(X_OK) is a runtime dependency corrupted in place while every marker keeps an identical size, mtime, and content, which no metadata-only check can detect. Catching that requires spawning the -h launch probe on every cache hit, which is exactly the startup cost this PR removes, so re-adding it would revert the change. That rare residual degrades to a normal backend start failure and repair rather than a silent wrong result, so keeping the cache fast path with the executable-access guard is the intended tradeoff.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reconsidered, and fixed in 47e9bf4. Restored running the managed CLI help probe before trusting the cache, so a cached managed install is only reported Ready after the launch probe succeeds. This covers the broken interpreter or dependency and the Windows cases you raised. The capability cache still skips the heavier desktop-capabilities probe on a hit, so a warm cache runs one probe instead of two rather than zero.

…cache

Restore running the managed CLI help probe before returning Ready from
the desktop capability cache, so a managed install whose venv interpreter
or a runtime dependency is broken (while path, size, mtime, and markers
are unchanged) is reported Stale for repair rather than proceeding to a
backend start that cannot spawn. The capability cache still skips the
heavier desktop-capabilities probe on a hit, so a warm cache runs one
probe instead of two. Removes the executable-access shortcut, which the
help probe now subsumes.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 47e9bf4910

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants