Skip to content

Studio: remove the background staged desktop update and return to the classic update - #10604

Merged
danielhanchen merged 17 commits into
mainfrom
studio-revert-staged-update
Sep 9, 2026
Merged

danielhanchen merged 17 commits into
mainfrom
studio-revert-staged-update

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Summary

Removes the background staged update that #9890 added to Unsloth Desktop (release 805 onward) and returns the desktop to the classic update flow: run studio update in place, install the shell bundle, relaunch.

The four independent fixes that shipped inside #9890 stay:

  • the Windows lazy-import gate for Studio mutation commands (_windows_studio_mutation_entry)
  • the POSIX flock runtime gate on .studio-runtime.lock, on both the Rust and the Python side
  • the process-group hardening in update.rs::stop_update
  • the unconditional PYTHONHOME / PYTHONPATH scrub for the update child

Why

Users reported that updating the desktop became much slower from 806 to 807 on Windows and macOS. I bisected releases 800 to 807 on isolated environments and on Windows, macOS and Linux runners, forcing each old version and timing every phase of the update with a byte-counting proxy in front of it. The 806 to 807 wheel itself did not regress: the wheel-only update costs the same on every OS. What users feel is the staged update from #9890:

OS plain 806 to 807 update staged update plus activation
Windows 115 to 154 s 267 to 291 s
macOS 63 s plus 25 to 35 s
Ubuntu 12 s plus 10 to 15 s

On Windows the stage copies the whole venv with shutil.copytree (100 to 134 s for a 2 GB CPU venv, several minutes for CUDA), verifies the copy, and verifies it again at activation. On macOS and Linux the stage fails whenever the Studio path is longer than about 68 characters, because the relocatable shebang rewrite changes script sizes and the RECORD check then rejects the clone. Those users pay a failed stage and then the full blocking update anyway. The design also left a lot of state on disk (.update-stage, .update-prev, .update-failed.json, rollback directories) that the shell had to reconcile at every launch.

What changes

CLI (unsloth_cli)

  • studio update --stage is kept as a hidden option but now refuses: it writes STUDIO_HOME/.update-failed.json with string backend_version and shell_version fields, prints [TAURI:ERROR] background staging is no longer supported; run the standard update, and exits 1. An 805 to 807 shell that still asks for a stage reads the marker, marks the backend as failed and falls back to the classic update on the next check. An orphaned .update-stage is moved aside first so it cannot mask the refusal.
  • _studio_stage.py is trimmed to the consumer side (is_staging, runtime_root, make_relocatable, finalize_for_activation, child_environment, the probes). The relocatable shebang rewrite now pads scripts back to the size the installer recorded, so a launcher never shrinks below its RECORD entry.

Desktop shell (studio/src-tauri)

  • UpdateKind::Staged, the staged environment plumbing, the pending-backend validation in process.rs, the four staged Tauri commands and the macOS rollback relaunch are removed. The runtime gate handoff is now set on every platform, since the shell holds the retained POSIX lock around the whole update child.
  • staged_update.rs becomes a legacy cleanup module. reconcile_legacy_at_launch removes stale trash, the compatibility failure marker and any .update-stage before anything could activate it, quarantines a confirmed previous runtime, and rolls a PENDING journal back only when the runtime is idle. reconcile_before_update runs the same reconciliation inside the runtime guard before a classic update, so a deferred journal cannot outlive it. Every step is a rename or a delete and is safe to repeat. 807-shaped journals still parse.
  • desktop_updater.rs keeps the bundle download; the classic flow downloads in the foreground when the bundle is not already on disk and reports progress. When the shell had already started that download on its own, the update waits on it for at most ten minutes before starting its own; a fresh download has no deadline, the same as before Studio: update in the background and restart when ready #9890.

Frontend (studio/frontend)

  • update-preparation.ts and its test are deleted. use-tauri-update.ts is back to the classic flow (check, policy, start_backend_update with progress events, bundle download, install, relaunch) and keeps the startup, hourly, focus and visibility rechecks from desktop: check for updates periodically #9505.
  • The compact preparing pill is gone from the update banner; the install button keeps its test id. The settings update control loses the preparing and ready branches. Six locale keys are removed from twelve locales and the available-update description is restored.

Compatibility

An 805 to 807 shell runs the installed (old) CLI with --stage. That CLI clones the venv itself, runs the new package's setup.sh / setup.ps1 inside the stage with UNSLOTH_STUDIO_STAGE_ROOT set, and on POSIX imports finalize_for_activation from the new package. The new wheel therefore keeps the passive side of that contract: the STAGE_ROOT handling in both setup scripts, the nested-stage branch of update(), the uv cache marker parking under a stage root, and an importable finalize_for_activation. A staging run driven by a real 806 CLI against this branch's wheel succeeds on macOS and Linux with the live venv untouched.

Verification

Unit and integration suites, all green on the branch:

  • cargo test --no-fail-fast in studio/src-tauri, cargo check --all-targets --release (same two pre-existing warnings as main)
  • npm test, npm run typecheck, npm run build, npm run i18n:check:strict in studio/frontend
  • pytest unsloth_cli/tests tests/python/test_studio_runtime_gate.py tests/python/test_windows_installer_concurrency_guard.py tests/python/test_windows_studio_update_launcher.py tests/python/test_whisper_source_build_root.py tests/studio/test_desktop_reliability_frontend_contract.py tests/studio/test_update_release_notes.py tests/studio/install -q
  • tests/sh/test_setup_staged_root.sh, tests/sh/test_studio_home_node_dir.sh
  • python -I -c "from unsloth_cli._studio_stage import finalize_for_activation" exits 0

End to end on Windows, macOS and Linux runners, with a wheel built from this branch served above the release pins so the old installed release's own scripts perform the update, exactly as the desktop does:

check Ubuntu macOS Windows
fresh 805 install, classic update to this branch, healthy launch 10 to 17 s, launch 5 to 6 s 70 s, launch 6 s 144 to 152 s, launch 8 to 9 s
fresh 806 install, classic update to this branch, healthy launch same same same
807 shell asks for --stage against this branch refused, marker written, no stage dir same same
real 806 CLI stages this branch's wheel stage succeeds, live venv untouched same not applicable
legacy leftovers seeded, then cleanup twice cleaned, second run is a no-op same same

No stage directory is created on any OS and the update times match the plain update column of the bisection.

Follow-ups

Two separate PRs build on this one: an idempotent update procedure (sidecars rebuilt only when their own evidence fails, per-step skips in the dependency pass, llama.cpp and node pre-checks, an offline rule) and a lightweight background prefetch of the next release into the uv cache so the swap at restart takes seconds without cloning anything.

Removes the staged-update path #9890 added to the Tauri shell: the staged
UpdateKind and its stage-progress/stage-complete/stage-failed events, the four
staged Tauri commands, the pending-version branches in port validation, the
staged rollback restart and the macOS relaunch helper. The update child now
always runs under the runtime launch guard and always inherits the gate through
STUDIO_RUNTIME_GATE_HANDOFF, which POSIX needs because the shell holds the flock
around the whole child.

staged_update.rs stays as a legacy cleanup module: reconcile_legacy_at_launch
deletes stale rollback trash, the compatibility failure marker and any stage
directory, then restores a runtime an unconfirmed 805-807 activation replaced.
Every step is a rename or a delete, so a second launch is a no-op.

Keeps the independent fixes that shipped in #9890: the POSIX runtime gate and
its ACQUIRE protocol, the stop_update process-group hardening, the unconditional
PYTHONHOME/PYTHONPATH scrub, and the desktop update bundle download with its
three commands.
An 805-807 desktop shell still spawns `unsloth studio update --stage`. The
hidden flag stays, but it now fails fast: no clone, no gate, no idle scan, no
launcher transaction. It writes the .update-failed.json those shells read, so a
later recheck of the same version returns skip rather than offering to prepare
again, prints the [TAURI:ERROR] line update.rs promotes off stdout, and exits 1,
which drops the shell onto the classic update.

_studio_stage.py keeps only the half an old CLI still calls into: the stage root
helpers, the shebang relocation and finalize_for_activation, stdlib only and
importable under python -I. The cloning, ready marker and uv marker promotion go
with the feature.
The frontend no longer prepares an update in the background: the preparing and
ready statuses, the compact pill, the preparation state and the five staged
wrappers all go, together with lib/update-preparation.ts. Update now runs the
backend update, downloads the shell bundle if an earlier attempt did not leave
one, installs it and relaunches, which is what the classic path always did.

Kept: the periodic check from #9505 with its status ref and focus/visibility
wake-ups, the retained bundle download and install commands, the release notes
panel, and the banner card shapes the Python contract tests pin. The locale keys
the background flow added are removed and desktopAvailableDescription goes back
to describing a restart on completion.
download_desktop_update refuses a second concurrent download, so a webview
reload during the shell download left the next Update now failing with
"Desktop update download is already running" instead of finishing. Poll the
bundle status until the running one settles, then reuse it or download.
Both leftovers this reconciler removes are whole runtime trees: .update-stage is
a clone of the managed venv and every native helper, and a confirmed
.update-prev is the runtime it replaced. Unlinking either one inside the Tauri
setup hook holds the runtime gate before the window exists, which on Windows is
minutes for a torch venv, and a complete stage is the common case for exactly
the installs this cleanup targets.

The stage is renamed into the rollback trash namespace, which a later launch
sweeps anyway, and unlinked on a background thread; a confirmed previous runtime
is dropped on one too, keeping its marker-last ordering so an interrupted delete
just repeats.
A 805-807 shell reports `partial` for any `.update-stage` directory before it
reads `.update-failed.json`, and its `stagingDecision` maps `partial` straight
back to `stage`. An orphaned stage from an earlier attempt therefore had that
shell re-spawning `studio update --stage` at every periodic recheck, whatever the
refusal marker said. Take the directory out of the way first: renamed into the
`.update-rollback-` namespace the desktop already sweeps at launch, then unlinked
off that path, so the name the shell reads is gone the instant the rename lands
and a delete that fails leaves only trash somebody else collects.

Also spell out two things the marker relies on: `shell_version` is an
`Option<String>` on the other side, so null parses and a placeholder would fail
the version comparison exactly as null does, and the marker lands under the
Python STUDIO_HOME, which matches the shell's studio directory except for a
desktop-configured custom home.
`make_relocatable` replaces a console script's `#!<venv>/bin/python` line with
the 82-byte relocatable shebang, so every venv path past about 68 characters
leaves the file SHORTER than the size RECORD holds for it. `install_manifest`
reads anything smaller than its recorded size as damage, which makes `setup.sh`
force the full dependency pass at every update and keeps a deep verification from
ever passing, so the whole cost of that check landed on exactly the installs
whose Studio path is long. This is the rewrite an old shell reaches through
`finalize_for_activation`, and `bin/unsloth` is the one launcher it rewrites, so
the deficit is paid back here: a comment line between the shebang and the body,
which `/bin/sh` never reads past the exec on line 2 and Python treats as a
comment, leaving the script still ending in whatever the installer wrote.

Tests cover a rewrite never shrinking a script, a stage finalised under a
100-character path clearing the same size comparison `install_manifest` runs, and
the shell version recorded in the refusal marker for a missing, blank or real
value. `managed_helper_root` goes with its test: the clone step was its only
caller and that step is gone.
Five things the cleanup module got wrong once a launch was not the only caller.

A rollback the launch deferred outlives the update that follows it. When a
backend still holds the tree, `reconcile_legacy_at_launch` leaves the PENDING
journal for the next launch, which is right at launch and wrong afterwards: a
classic update then installs into a runtime a journal still names as something to
undo, and the next idle launch restores the pre-update trees over all of it.
`reconcile_before_update` settles it inside the runtime guard, after the idle
scan and before the child spawns, and refuses with the one instruction that fixes
it when a backend is holding the tree.

The confirmed backup is quarantined rather than emptied. Deleting in place took
entries out from under the marker that vouches for the live runtime, so an
interrupted delete left a backup with no confirmation, which the next launch
reads as an activation nobody confirmed and rolls back to. The rename is the
whole decision and it is atomic.

The stage discard puts its fallback on a thread too. On Windows the rename fails
exactly when a file inside is still open, which is the multi-gigabyte tree the
background delete exists for, so the in-place unlink held the setup hook for as
long as the clone took.

`roll_back_unconfirmed` stops probing the process table at every launch: an
install with no `.update-prev` has nothing to decide. And a trash name carries a
counter as well as nanos and a pid, so two names in one launch cannot collide.

Tests: a READY stage discarded ahead of a mid-swap PENDING journal, a stage whose
rename fails still swept, distinct trash names, a confirmed backup gone from the
directory on return, and a deferred journal settled or the update refused.
A webview reload leaves the native bundle download running with nothing listening
to it, and `download_desktop_update` refuses a second one, so `installUpdate`
waits that download out. It did so silently and forever: the progress bar sat at
zero for the whole of it, and a download that stalled without ever clearing the
flag held the update there with nothing on screen to say why.

`listenDesktopUpdateDownload` comes out of `downloadDesktopUpdate` so the wait can
attach to the same `desktop-update-download` events, once a download really is in
flight and released whichever way the wait ends. The wait is bounded at ten
minutes, after which the attempt falls through to `downloadDesktopUpdate`, which
either takes the download over or reports why it cannot.

The schedule tests grow an event bus and a scripted bundle status so both shapes
are covered, and the failed-install case now pins a rejected
`invoke("start_backend_update")` rather than leaning on a `listen` stub that
happened to throw somewhere earlier in the same path.
The revert took `data-testid="tauri-update-install"` off with the compact pill it
also belonged to. The card button is the one an end-to-end test drives, and the
background prefetch built on this branch selects it by that id.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 9, 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-09T12:32:50.278558Z 2167e5c 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.

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

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

// Before the rollback, not after: a READY stage must never be activated, and
// a rollback that still saw one would leave the entries it did not restore
// live beside the restored runtime.
discard_stage(home);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not discard stages produced by a pre-upgrade shell

When an 805–807 shell prepares this release, the already-running old CLI owns --stage, clones the live environment, and successfully writes a READY stage after running the new wheel inside it; the new _refuse_staged_update code is therefore never invoked. After that shell installs the new desktop bundle, this launch path deletes the only updated backend instead of activating it, leaving the old live backend behind. Release builds stamp the new PyPI version as the expected backend, so startup must perform the same blocking update again—and can leave the app unusable until repair if the network is no longer available. Preserve/activate a compatible READY stage from the immediately preceding shell, or make the nested staged setup fail so the old shell takes its classic-update fallback.

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.

The mechanism is right, the consequence is not, so I am keeping the discard.

What I checked at head and on the 805-807 code this replaces:

  • 805-807 do not activate their own stage before restarting. installUpdate takes the fast plan, calls stop_server, installs the bundle and relaunches; activation is staged_update::reconcile_at_launch -> activate_ready, and activate_ready only runs when the running shell version equals the stage's recorded shell_version, which is the version being offered. So the stage is indeed meant to be activated by the shell this branch ships, and this branch discards it instead. That part of the report is correct.
  • What follows is not an unusable app. The live backend is untouched, so the managed probe reports desktop_backend_version_outdated -> ManagedStale, and use-tauri-backend handles managed_stale by calling startRepair() when can_auto_repair is set. The first launch runs the classic update by itself, with no user action and no repair prompt, and that classic update is what this PR restores.

On the two suggested remedies:

  • Preserving or activating a compatible READY stage means keeping activate_ready, swap_entries, the PENDING journal and the rollback path alive. That machinery is the regression this PR removes, and the stage in question was cloned by the old CLI, which is the copy that costs 100 to 134 s on Windows and fails above roughly 68 characters of path on macOS and Linux. Reintroducing it to salvage one launch is the wrong trade.
  • Making the nested staged setup fail moves the same single update from just after the relaunch to just before it. The old CLI clones the venv before it ever reaches the new package, so the clone is paid either way, and both orders end with one classic update and a fully updated install.

The refusal path is not dead either: it covers a shell on 805-807 running an already-updated CLI, where _refuse_staged_update writes .update-failed.json so stagingDecision returns skip and the shell drops to the classic update on the next check.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: aa83fc5f29

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 9, 2026

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

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

updateStatus("error");
return;
}
invoke("start_backend_update").catch((e) => resolve(String(e)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Require the offered backend version before installing

When the update manifest's pypi_version is newer than the backend pinned by the currently running shell, this invocation does not pass that offered version to the native update. update.rs therefore sets UNSLOTH_DESKTOP_BACKEND_VERSION to the old shell's expected_backend_version(), and install_python_stack.py accepts any package at that older floor. With a lagging mirror or cached index, the backend update can exit successfully without reaching the version required by the downloaded shell, yet this flow proceeds to install and relaunch that shell; its preflight then rejects the backend and can leave the app stuck in repair. Forward info.pypiVersion as the update floor or verify the installed version before installing the bundle.

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.

This is the classic flow's long-standing behaviour, not something this PR changes, and it self-heals.

configure_tauri_update_environment is byte for byte what it is on main: UpdateKind::Backend has always set UNSLOTH_DESKTOP_BACKEND_VERSION from preflight::expected_backend_version(), and start_backend_update has never taken an offered version. The only thing that checked a manifest version against the installed package was staged_backend_meets, and it only ever guarded the staged path. Every release before 805 shipped exactly this classic invocation, so nothing here is newly reachable.

The end state is also not a stuck app. If the update lands a backend below what the new shell wants, the new shell's managed probe returns desktop_backend_version_outdated -> ManagedStale, release_auto_repair() is true in a release build and the reason is not a context reason, so can_auto_repair is set and use-tauri-backend calls startRepair() on its own. That repair runs the update again, this time under the new shell's higher expected_backend_version(), which is the floor the report wants enforced. It arrives one launch later without user action.

Passing info.pypiVersion down as an update floor is a reasonable improvement to the classic flow, but it belongs in its own change against that flow, not in a revert whose job is to put the classic flow back unchanged.

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed the staged path in studio/src-tauri/src/staged_update.rs is what users are feeling on Windows and macOS, and the legacy cleanup plus the --stage refusal marker cover the 805 to 807 shells. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 2167e5c219

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

1 participant