Skip to content

Linux/macOS: tell an unreadable llama.cpp install apart from someone else's - #7771

Merged
danielhanchen merged 3 commits into
mainfrom
studio-setup-sh-denied-swap
Aug 3, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-setup-sh-denied-swap

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

setup.ps1 learned to handle an unreadable llama.cpp install in #7735 and #7757. setup.sh never did, and it fails differently rather than better: POSIX probes return false instead of throwing, so the failure is a wrong answer rather than a crash.

An install that is ours reads as somebody else's

_assert_studio_owned_or_absent decides ownership by probing for .unsloth-studio-owned inside the tree. A directory that cannot be searched reports every child absent, so a tree that IS ours, with our marker sitting in it, produces:

ERROR: <dir> already exists and is not marked as an Unsloth-owned llama.cpp install.
       Move it aside or choose an empty UNSLOTH_STUDIO_HOME before re-running.

Wrong cause and wrong remedy: nothing needs moving aside, the permissions need fixing. Driving the real function against the same directory before and after a chmod 000 shows it flip from accepted to rejected with no change to its contents. This is the same misdiagnosis Get-StudioAdoptableState had on Windows until #7757.

Search (+x) is the permission those probes need, not read (+r), and the two are independent:

mode [ -f dir/marker ] ( cd dir ) ls -A dir
000 false fail fail
111 true ok fail
444 false fail ok
755 true ok ok

A read probe would be wrong in both directions, so the new helper tests search.

Both destructive replaces ran blind

rm -rf "$LLAMA_CPP_DIR"
mv "$_BUILD_TMP" "$LLAMA_CPP_DIR"

and the --with-llama-cpp-dir equivalent with ln -sfn. Neither checked that the remove worked. Under set -euo pipefail a failing rm -rf aborts on a raw

/bin/rm: cannot remove '<dir>/...': Permission denied

with no [TAURI:ERROR], so the desktop app shows a bare exit code with no reason, and a llama.cpp build that just succeeded is left in $_BUILD_TMP with nothing saying where. That is the same user-visible symptom #7735 removed on Windows.

Both sites now tolerate the failed remove, check the directory is actually gone, and report either the permission problem or where the new build was left. A build that succeeded but could not be installed exits 3, matching the Windows side; setup.sh already uses that code.

Neither new message tells the user to delete a folder Unsloth cannot prove is its own, which is the rule the Windows guard follows.

Tests

tests/sh/test_setup_sh_denied_install_tree.sh, 16 checks: static anchors for both guards, plus behaviour driven against a real chmod 000 tree using the functions extracted from setup.sh itself. It asserts a readable tree of ours is still accepted, an unowned readable tree still stops on ownership exactly as before, and an unreadable tree reports permissions without blaming ownership or advising deletion. It carries a negative control that skips rather than passing vacuously if the host cannot make a directory unsearchable.

Each guard was mutation-tested and is load-bearing:

mutation result
drop the readability check from the ownership guard red
probe read (ls) instead of search (cd) red
drop the swap postcondition red
advise deleting an unverified tree red

Windows is untouched. bash -n clean, and the shell suite runs 31 files with only the pre-existing test_install_host_defaults.sh failure, which is unrelated and already quarantined in tests/run_all.sh.

…else's

setup.ps1 learned this in #7735 and #7757; setup.sh never did.

A tree that is ours, carrying our own marker, reads as somebody else's the
moment it stops being searchable, because every probe inside it reports the
marker absent. The ownership guard then said the folder is not an
Unsloth-owned install and told the user to move it aside or pick an empty
UNSLOTH_STUDIO_HOME, when the actual fix is a permission change. Search (+x)
is what those probes need, not read (+r): a directory can be readable and
unsearchable (mode 444) or searchable and unreadable (mode 111), so the new
probe tests search.

The two destructive replaces also ran blind. Under errexit a failing rm -rf
aborts on a raw permission error with no [TAURI:ERROR], so the desktop app
shows a bare exit code and the freshly built llama.cpp is left in the temp
directory with nothing saying where. Both sites now tolerate the failed
remove, check that the directory is actually gone, and report either the
permission problem or where the new build was left, exiting 3 as the Windows
side does for a build that succeeded but could not be installed.

Neither message tells the user to delete a folder Unsloth cannot prove is its
own, matching the rule the Windows guard follows.

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

ℹ️ 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/setup.sh
# Without this the raw rm error aborts under errexit and the desktop
# app shows a bare exit code, with the fresh build silently stranded.
rm -rf "$LLAMA_CPP_DIR" 2>/dev/null || true
if [ -e "$LLAMA_CPP_DIR" ]; then

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 Treat surviving dangling symlinks as existing

When LLAMA_CPP_DIR is a dangling symlink from an earlier --with-llama-cpp-dir run and rm -rf cannot unlink it—for example, because the parent became non-writable during the build—[ -e "$LLAMA_CPP_DIR" ] is false even though the directory entry remains. The code consequently skips the new failure reporter and reaches mv, which aborts with the same raw error this change is intended to replace and does not tell the user where the completed build was left. Include a symlink check in this postcondition.

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 -e/-L semantics are right, and I reproduced the shape: a dangling link with a non-writable parent gives -e: no -L: yes, skips the reporter, and mv aborts raw.

Rejecting on reachability. _BUILD_TMP="${LLAMA_CPP_DIR}.build.$$" is a sibling of the install dir and the source build clones into it, so the parent has to be writable when the build starts. Getting here needs it to become non-writable between the clone and the swap, which is a mid-build chmod or remount rather than input. In that state mv cannot write to the same parent either, so the run fails regardless and only the wording differs.

The link site cannot reach it at all: [ -L "$LLAMA_CPP_DIR" ] && rm -f runs first and aborts under errexit before the postcondition.

Comment thread studio/setup.sh
Comment on lines +2192 to +2193
if _studio_dir_unsearchable "$LLAMA_CPP_DIR"; then
_path_access_denied "$LLAMA_CPP_DIR" "llama.cpp install"

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 Preserve the stranded-build location in permission failures

When a source build succeeds but removal leaves an unsearchable install directory—for example, on an ACL- or mount-restricted default install—this call exits immediately through _path_access_denied with status 1. It therefore never reaches the status-3 failure below that reports $_BUILD_TMP, leaving the freshly completed build stranded without telling the user where it is despite that being the purpose of the new swap guard. Pass the build location through this permission failure or report it before exiting.

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.

Confirmed the build does survive unnamed on that path, but the message is incomplete rather than wrong, and this is deliberate parity with the Windows side being ported. setup.ps1 does the same: the Denied branch calls Exit-PathAccessDenied and exits 1 without the build path, and only the non-denied branch reports the location. Diverging here would leave the two installers giving different answers to the same state.

Nothing regressed either: before this PR the same state aborted on a raw rm error with no location and no [TAURI:ERROR] at all.

The related point about losing the rm stderr was right, and that one is fixed.

Comment thread studio/setup.sh
Comment on lines +1487 to +1488
if _studio_dir_unsearchable "$LLAMA_CPP_DIR"; then
_path_access_denied "$LLAMA_CPP_DIR" "llama.cpp install"

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 Retain permission details for inaccessible descendants

When deletion fails because a populated child directory is unsearchable while the install root itself remains searchable—for example, llama.cpp/build has mode 000—this probe returns false because cd "$LLAMA_CPP_DIR" still succeeds. Since the preceding rm now discards stderr, setup suppresses the only Permission denied detail and reports only a generic link-replacement failure, so the user is no longer told that permissions are the cause or which subtree needs repair. Capture and inspect the removal error or probe the surviving descendants before selecting the generic failure.

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.

Agreed, and it is a regression this PR introduced: before it, the raw rm error was the only thing the user saw, and it names the exact subpath. My generic message can only name the install root.

Dropping the stderr redirect and keeping || true gets both:

/bin/rm: cannot remove '.../llama.cpp/build': Permission denied
   STEP|llama.cpp|built, but the existing install could not be replaced
   FAIL exit=3 | ... could not be replaced. The new build is at .../llama.cpp.build.999.

Fixed at both replace sites, with an assertion that the redirect does not come back.

The redirect this PR added swallowed the one line that names which subtree
refused to go. rm reports the deepest failing path, llama.cpp/build say, while
the message below it can only name the install root, so a user whose build
directory alone is locked lost the part that told them where to look.

|| true is what keeps errexit from aborting; the redirect was never doing that
job, so stderr stays.
@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. You're on a roll.

Reviewed commit: de8e8a6b7a

ℹ️ 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 merged commit ef08ee9 into main Aug 3, 2026
74 of 77 checks passed
@danielhanchen
danielhanchen deleted the studio-setup-sh-denied-swap branch August 3, 2026 07: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.

1 participant