Repository navigation
Studio: hash wxc-exec once per Windows MXC launch and build the policy once per stage - #19409
Conversation
…y once per stage Take the runtime digest once under the deny-write guard instead of before and after it, cache the installation identity by file identity, serialize the MXC policy once at build and once at dispatch, skip the read-grant lease when the DACL fallback is off, and check each runtime read root once.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…ests in one attempt, gate the host before opening artifacts
…rdicts, forget the identity on backend refresh
…e its guard opened [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…lls (unslothai#19408) A sandboxed tool call cost far more than the sandbox it runs in. On GitHub runners `bwrap true` takes 3 to 5 ms and Seatbelt about 13 ms, yet a Studio Terminal call took 50 to 130 ms and a `curl` call about 300 ms. Profiling the real `execute_tool` path showed most of that time was Studio repeating work on every call. This PR removes the repeats and keeps every check. ## What changes - **One read connection for app settings and scan folders** (`storage/studio_db.py`). - A sandboxed launch reads about ten settings. Each `get_app_setting` used to open a fresh SQLite connection (connect, three PRAGMAs, close, all behind the process-wide `sqlite_gate`). That cost 1 to 3 ms per read. A cached read now takes about 56 us, against about 484 us for a one-shot connection. - Reads share one long-lived connection per database. Each autocommit SELECT starts a new read transaction, so a write from any connection or process is visible to the next read. - Every read still runs the account-retirement check that `get_connection` runs, so a deleted account is refused on a warm reader too. - The map lock only guards the lookup. Each connection has its own busy lock, and a read that finds it busy uses a one-shot connection. A database waiting on a lock never stalls another account's reads. - A reader is published only if the file did not move while it opened and no close ran meanwhile. It reopens when the file identity changes, is dropped after any SQLite error, and is reset in a forked child. It closes with the WAL keeper (`on_wal_keeper_closed`), on `reset_schema_state_for_tests`, and after every backend and Hub test. - Only the real `get_connection` is cached. A substituted factory (test doubles) is read through. - **One scan memo scope per launch** (`os_sandbox.prepare_tool_launch`). The grant-root checks re-read the HF cache homes and scan folders several times while building one policy. Nothing is kept across launches, so a credential file added between calls is still seen by the next call. - **The egress proxy no longer sleeps on close** (`sandbox_egress.EgressProxy.close`). Closing the listener does not interrupt a blocked `accept()`, so every proxied `curl` or `wget` call waited out the 0.25 s accept timeout. Close now wakes the accept loop once, with a connection that `_track` refuses. A second close is a no-op. Close takes about 0.5 ms instead of up to 250 ms. - **Sandbox shim bytecode** (`tools._ensure_sandbox_site_bytecode`). - The OS sandboxes mount `sandbox_site` read-only, so a sandboxed Python could never cache `sitecustomize.py`. It recompiled about 1,800 lines on every start. - Studio now writes the bytecode itself, once per shim version, before the first launch. It is written to the path a default child reads, ignoring the host's `PYTHONPYCACHEPREFIX` and `-O` tag. - The pyc is checked-hash (PEP 552), so a child validates it against the source bytes. An edit, even a same-size edit within one second, never runs stale bytecode. A child with the checked-hash pyc starts in 21 ms, against 22 ms with a timestamp pyc and 34 ms with none. - A read-only install, or an interpreter with another cache tag, behaves exactly as before. - **Cheaper workdir snapshot** (`tools._snapshot_workdir_views`, `_servable_segment`). The per-character lone-surrogate scan is replaced by `isascii()` plus a regex, and the per-file `os.path.join` is replaced by one join per directory. Output is unchanged. - **Linux: duplicate interpreter paths dropped before resolution** (`sandbox_linux._runtime_read_paths`). The bind list is identical. ## Timing `tool_loop_bench.py` on staging CI. It times the real `execute_tool` in `required` mode and the real tool loops driven by a scripted model. Base is `main` and head is this branch together with unslothai#19409 and unslothai#19410, on the same runner, alternating rounds. On Linux and macOS this PR is the only one of the three that changes these paths. Warm medians are in ms. | Runner | Sandbox | Python | Terminal | Terminal, chat session | `grep` over 2,000 files | `curl` | |---|---|---|---|---|---|---| | ubuntu-24.04 | bubblewrap | 80 -> 52 | 56 -> 29 | 80 -> 47 | 58 -> 29 | 287 -> 50 | | ubuntu-22.04 | bubblewrap | 115 -> 71 | 88 -> 44 | 127 -> 76 | 89 -> 46 | 306 -> 70 | | ubuntu-26.04 | bubblewrap | 81 -> 52 | 56 -> 28 | 80 -> 44 | 57 -> 32 | 286 -> 50 | | ubuntu-latest | bubblewrap | 73 -> 48 | 52 -> 27 | 71 -> 42 | 54 -> 29 | 285 -> 45 | | ubuntu-slim | bubblewrap | 152 -> 88 | 106 -> 55 | 156 -> 88 | 111 -> 55 | 312 -> 84 | | ubuntu-22.04-arm | bubblewrap | 89 -> 57 | 64 -> 31 | 94 -> 55 | 65 -> 32 | 294 -> 55 | | ubuntu-24.04-arm | bubblewrap | 104 -> 71 | 77 -> 43 | 108 -> 68 | 78 -> 44 | 296 -> 69 | | ubuntu-26.04-arm | bubblewrap | 104 -> 67 | 75 -> 40 | 105 -> 65 | 78 -> 43 | 296 -> 65 | | macos-latest | Seatbelt | 116 -> 75 | 69 -> 41 | 100 -> 79 | 72 -> 43 | 261 -> 53 | | macos-15 | Seatbelt | 127 -> 90 | 94 -> 50 | 119 -> 88 | 79 -> 57 | 258 -> 72 | | macos-26 | Seatbelt | 117 -> 83 | 78 -> 47 | 114 -> 76 | 84 -> 56 | 258 -> 61 | | xcode-27 | Seatbelt | 130 -> 73 | 69 -> 35 | 111 -> 61 | 104 -> 44 | 259 -> 52 | Windows shares the settings, scan memo, shim and snapshot paths. The same pass on Windows, with unslothai#19409's MXC changes included: | Runner | Sandbox | Python | Terminal | |---|---|---|---| | windows-11-arm | MXC, native tier | 369 -> 244 | 376 -> 190 | | windows-11-vs2026-arm | MXC, native tier | 350 -> 231 | 346 -> 177 | | windows-2025 | MXC, AppContainer + DACL tier | 237 -> 148 | 252 -> 121 | | windows-2025-vs2026 | MXC, AppContainer + DACL tier | 270 -> 184 | 278 -> 148 | | windows-latest | MXC, AppContainer + DACL tier | 257 -> 174 | 293 -> 138 | | windows-2022 | none (Studio needs build 26100), software safeguards | 41 -> 7 | 107 -> 10 | - The Linux, macOS and native-tier Windows rows ran every case on the real OS sandbox (`bubblewrap`, `macos-seatbelt` or `mxc-processcontainer`). - The DACL-tier runners hit the job time limit after two or three rounds, so those rows are the median over the finished rounds. - The tool loop itself is cheap. On Windows its overhead for three tool rounds went from 14 to 31 ms to 9 to 18 ms, and streaming costs about 17 us per chunk on both sides. ## Behaviour checks - **Settings changed mid-turn.** One GGUF tool-loop turn ran four tool rounds, with changes made between rounds. Base and head produced identical rows on every Linux and macOS runner and on both Windows ARM64 runners: - A model folder outside the scan list is refused at first. - A scan folder added mid-turn is readable on the next call and is still read-only. - A lowered tool output limit applies to the next call. - The approval card's full-access upgrade keeps the OS sandbox. - **Sandbox toggle across turns.** Switching Sandbox High, then Low, then High again gives `os_isolated`, then `software_safeguards`, then `os_isolated`. - **Tests:** - New: `test_studio_db_settings_reader.py` and `test_tool_launch_overhead.py`, plus egress close tests. - With only the functional lines reverted (helpers kept), the shim, reader, scan memo, egress and path dedupe tests fail. The snapshot rule tests pass both ways, as they should. - The sandbox, tool, settings, account, tool-loop and Hub suites show the same failing test IDs on base and head. ## Compared with the bare sandbox Same runners as the timing table, warm medians in ms. - **Host** runs `true` and `python -c pass` with no sandbox. - **Bare sandbox** runs the same two commands under a minimal profile: - Linux: `bwrap --ro-bind / / --dev /dev --proc /proc --unshare-all --die-with-parent` - macOS: `sandbox-exec -p '(version 1)(allow default)'` - **Studio** is a full tool call through `execute_tool` in `required` mode, with no chat session. | Runner | Host shell / Python | Bare sandbox shell / Python | Studio Terminal, base -> head | Studio Python, base -> head | |---|---|---|---|---| | ubuntu-24.04 | 0.4 / 17 | 2.7 / 20 | 56 -> 29 | 80 -> 52 | | ubuntu-22.04 | 0.8 / 27 | 5.3 / 32 | 88 -> 44 | 115 -> 71 | | ubuntu-26.04 | 0.5 / 25 | 3.9 / 30 | 56 -> 28 | 81 -> 52 | | ubuntu-latest | 0.8 / 27 | 5.6 / 33 | 52 -> 27 | 73 -> 48 | | ubuntu-22.04-arm | 0.6 / 22 | 3.6 / 25 | 64 -> 31 | 89 -> 57 | | ubuntu-24.04-arm | 0.6 / 21 | 3.7 / 25 | 77 -> 43 | 104 -> 71 | | ubuntu-26.04-arm | 0.7 / 22 | 4.4 / 26 | 75 -> 40 | 104 -> 67 | | macos-latest | 3.1 / 34 | 15 / 42 | 69 -> 41 | 116 -> 75 | | macos-15 | 4.1 / 34 | 13 / 42 | 94 -> 50 | 127 -> 90 | | macos-26 | 4.7 / 40 | 13 / 56 | 78 -> 47 | 117 -> 83 | | xcode-27 | 3.6 / 33 | 13 / 41 | 69 -> 35 | 130 -> 73 | How to read the table: - The bare sandbox adds 2 to 6 ms with bubblewrap and about 10 ms with Seatbelt. Studio added about 50 to 85 ms on top of that before this PR. After it, the gap above the bare sandbox is about 20 to 50 ms. - That gap is not all removable. Studio's real profile does more than the bare one: - Linux adds seccomp, Landlock, a fresh `/tmp`, runtime, model and identity binds, and per-launch identity files. - macOS adds a generated SBPL profile with literal-only rules. - Both sides also run the safety checks, the workdir snapshot that drives the file cards, PID tracking for cleanup, and output budgeting. - Python additionally loads the sandbox `sitecustomize` shim. - The host and bare-sandbox columns come from an earlier pass on the same runner images. - `ubuntu-slim` is left out because its bare-sandbox Python number matched `true`, so that probe did not run Python.
On Windows a sandboxed tool call took about 210 ms on Server 2025 (DACL tier) and about 370 ms on Windows 11 ARM64 (native tier). On the same runners
wxc-execalone ran Studio's own policy in 45 ms and 102 to 124 ms. The profile shows the 9.5 MBwxc-exec.exewas SHA-256 hashed five times per call. On ARM64 two of those hashes alone took 34 ms. The policy was also serialized four times per call.What changes
mxc_runtime._acquire).wxc-execwill run.mxc_runtime.installation_identity).st_ctimeon older Windows Pythons) and link count. Any change, or a new pin, forces a full rehash, and a failed rehash drops the old verdict.acquire_runtimestill verifies the bytes.verified_installation_identity, which always rehashes. A failed full verification also drops the cached identity.install_mxc_prebuiltdrops the cache after a swap, and so does the backend refresh that runs after setup and on Settings refresh.mxc_policy.policy_hash_of_bytes,mxc_adapter.spawn). Building the request serialized the config for its bytes and again for its hash, and dispatch did both again. Each stage now hashes the bytes it just produced. Dispatch still compares both the bytes and the digest, and sends exactly the verified bytes.mxc_read_grants.hold_before_build,hold_for_request). Each launch and probe took the record lock, created and deleted a lease file, and ran a deferred-revoke check, even on the native tier.allowDaclMutation: false, sowxc-execcannot pick the AppContainer + DACL tier.main.mxc_policy._runtime_read_roots). On Windowssite.getsitepackages()[0]equalssys.prefix, so that root was canonicalized twice. Only an exact repeat is skipped; any other spelling, including a case variant, still gets its own checks.Unchanged:
Measurements
acquire_runtimeon a GitHub x64 runnerEnd to end on staging CI,
tool_loop_bench.pytimes the realexecute_toolinrequiredmode. Base ismain. Head is this branch together with #19408 (settings, scan memo, shim bytecode) and #19410 (macOS only). Warm medians are in ms, with no chat session.grepover 2,000 filesmxc-processcontainer.wxc-execfloor is about 102 to 136 ms on the native tier and 38 to 56 ms on the DACL tier (see below), so Studio's own share of a native-tier Python call went from about 240 ms to about 120 ms.Tests
test_mxc_launch_repeats.py: serialization counts, the lease when DACL is off and on, a switch turned on mid-launch, a failed lease tried once, exact-repeat root dedupe, and a wrong hash still refused.test_mxc_runtime_integrity.pyadditions:test_a_launch_holds_the_read_grants_until_its_last_cleanupasserted the old always-lease behaviour with DACL off. That one case is dropped.MXC versions
All numbers are bare
wxc-exec(no Studio) running the same ProcessContainer profile Studio builds, as warm medians in ms on GitHub runners.cmdrunscmd /d /c echo 1.pythonrunspython -S -c "print(1)"with the Python install granted read-only.0.8.0-alphaconfig. 1.0.1 and 1.1.0 refuse that config and ran the schema1.0.0form. That form only changes the network fields:network.egress.default = "allow"andnetwork.ingress = {"default": "allow", "hostLoopback": "deny"}. With those values it still blocks host loopback and allows internet egress, like 0.8.0 does today.wxc-exec --probeWhat this means:
backend_unavailable. Its probe still exits 0 but reports no tier.1.0.0config.pythonrows show why Studio: grant MXC Tier 3 read access to runtime folders once, not per launch #12121's persistent read grants exist. Without them the DACL tier re-ACLs the whole granted Python tree on every launch. Studio's own DACL calls below grant once and stay around 200 ms.Bare sandbox vs MXC vs Studio
Warm medians in ms.
bwrap --ro-bind / / --dev /dev --proc /proc --unshare-allsandbox-exec -p '(version 1)(allow default)'wxc-exec0.8.0 with Studio's profileexecute_toolcall inrequiredmode onmain, with no chat session on Linux and macOS.main)What this means:
wxc-exec.exe(about 34 ms on ARM64 for two of them) and the repeated policy work.