Skip to content

Share builders and setup helpers in the studio backend tests, part 2 - #10498

Merged
danielhanchen merged 23 commits into
mainfrom
slim-tests-2
Sep 9, 2026
Merged

danielhanchen merged 23 commits into
mainfrom
slim-tests-2

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

What this does

Removes 4,362 lines from studio/backend/tests without changing what any test checks. Second of three chunks (files test_hf_cache_settings.py through test_partial_resume_verdict.py, 248 files, 183,744 lines).

lines
before 183,744
after 179,382
removed 4,362 (2.37%)

79 files changed, 2,760 insertions, 7,122 deletions.

The figures above are measured against the current merge base, which has moved with pre-commit.ci reformatting and merges of main.

Where the lines went

  1. Repeated multi-line call sites to one compound helper, about 3,000 lines. The dominant cost in this suite is a fixture-shaped setup written out longhand in every test: _wired() in test_openai_auto_switch covers 66 sites of 9 lines each, _run_tool_loop() and _backend_and_payloads() in test_llama_cpp_tool_loop cover 95 and 102 sites.
  2. Function-local imports hoisted to module level, where that does not change what the module imports at collection time. See the section below on the ones that were reverted.
  3. Thin wrappers where call sites share fixed arguments, about 450 lines. A call site is rewritten only if it already passed every wrapped keyword explicitly, so the wrapper cannot change what any caller receives.
  4. Repeated literal payloads to builders returning a fresh copy, about 700 lines. Deliberately functions rather than module constants, so one test cannot mutate a value another test reads.

Example from test_llama_cpp_tool_loop.py:

-    payloads: list[dict] = []
-    backend = _make_backend(monkeypatch, [stream], payloads)
+    backend, payloads = _backend_and_payloads(monkeypatch, [stream])
     _patch_monotonic(monkeypatch, [1.0, 2.0, 3.0, 4.0, 4.0])

Biggest files: test_openai_tool_passthrough.py -956, test_openai_auto_switch.py -927, test_llama_cpp_tool_loop.py -858, test_llama_cpp_mtp_detection.py -251, test_hosted_result_replay.py -196.

How I checked coverage did not move

Whole-area run of the 245 runnable files, before and after:

BEFORE: 99 failed, 12772 passed, 91 skipped
AFTER:  99 failed, 12772 passed, 91 skipped
sorted FAILED and ERROR node ids: byte-identical

Separately, every pytest node id was collected repo-wide in this branch and in a worktree of the merge base, from the repo root and from studio/backend with hub/tests included, and the two sets diffed: 62,026 ids on each side, nothing lost and nothing gained. That is the check that matters here, because a def test_ count in the AST still counts a test that no longer gets collected.

Per file, across all 80 changed files: def test_ 5,170 to 5,170, assert 12,184 to 12,184, pytest.raises 406 to 406, skip and xfail markers 55 to 55, parametrize case counts unchanged.

I also ran a comment census in both directions, which is worth describing because the first version of it was wrong. It originally only checked that no new comment text appeared, which catches a regex rewriting prose but is blind to deletions. Corrected, it reports every removed comment instance as accounted for: one deduplicated from two copies to one, and one moved into a helper docstring. Three # noqa: PLC0415 markers were removed while their imports were at module scope; those imports are back inside their functions and the markers with them.

Four facts were dropped by folds and have been restored:

  • apple_budget_mib = 23_000, # ~22 GB: weights fit, native KV doesn't
  • # 0 = Auto context (the branch that caps); the native 8192 above is the target., folded into the _launch_auto_8k docstring since it applies to all three call sites that pass n_ctx = 0
  • # nothing pinned, so the preflight re-asks the gate; no marker, so it abstains
  • # case-insensitive beside a deliberately lowercase variant

What I threw away

Automated parametrization of near-identical tests was measured at only about 213 lines area-wide once clusters carrying comments were excluded, and merging via ast.unparse would have silently deleted in-body comments and reflowed every string literal. Not applied.

Blanket collapsing of one-argument-per-line call formatting was worth about 3,500 lines but is reformatting for its own sake, so calls were only collapsed as part of an actual helper extraction.

Three passes were reverted for breaking tests: hoisting lifted methods out of nested classes, hoisting two different _resolve bodies collided at module scope, and wrapper defaults referencing a fixture function were evaluated at definition time.

Import hoisting, and why most of it was reverted

Hoisting a function-local import to module scope moves that import from run time to collection time. Where the module then pulls in something it did not pull in before, this is not a refactor, it is a behaviour change, and under a shared invocation it can be fatal.

The first version of this branch hoisted from main import _is_same_origin_request to module scope in the three test_index_bootstrap_* files. main imports loggers.config, and once hub/tests is collected in the same invocation that resolves to something that is not the package:

ERROR collecting studio/backend/tests/test_index_bootstrap_origin.py
tests/test_index_bootstrap_origin.py:15: in <module>
    from main import _is_same_origin_request
main.py:896: in <module>
    from loggers.config import LogConfig
E   ModuleNotFoundError: No module named 'loggers.config'; 'loggers' is not a package

41 tests stopped being collected. Neither pytest tests from studio/backend nor an AST-level count of def test_ can see that, which is why the node id diff above is the check this branch is verified with.

Rather than patch those three files, the invariant was restored across the whole pass: a hoist is kept only where the module's import-time footprint is byte-identical to base. That was measured, not reasoned about, by importing each test module in a fresh interpreter and diffing the resulting sys.modules top-level packages between the two trees. 31 of the 64 files that had hoists pulled new packages at import time, including core, routes, hub, httpx, huggingface_hub, fastapi, sqlite3, ssl, PIL and numpy. Only the main case was fatal today; the rest were latent.

129 imports went back inside the functions that use them, none were kept, and the re-probe flags none of the 31. The three test_index_bootstrap_* files had the hoist as their only change and are reverted to base exactly. This costs 2,162 lines against the earlier figure, which is the right trade: those lines were bought with collection-time coupling.

Local test state

99 pre-existing failures in this chunk on this box, identical before and after, dominated by pytest.mark.asyncio being unregistered on this install. test_nudge_tool_calls_wiring.py does not collect here (AttributeError in torchao while importing unsloth) and aborts the run, so it was excluded from both the baseline and the final measurement, along with test_mcp_server.py and test_rag_ocr_fallback.py for missing optional deps.

I am not claiming a clean local suite. The claim is that the failure set is identical with and without this change.

danielhanchen added 18 commits September 8, 2026 04:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 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-09T04:52:10.805971Z 44d9967 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[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 44d996747d

ℹ️ 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 1f92c95 into main Sep 9, 2026
64 of 68 checks passed
@danielhanchen
danielhanchen deleted the slim-tests-2 branch September 9, 2026 07:06
danielhanchen added a commit to Souravrajvi0/unsloth that referenced this pull request Sep 9, 2026
One conflict, in studio/backend/tests/test_hub_token_caller_identity.py.
Main's unslothai#10498 hoisted the repeated function-local imports in that file; this
branch hoisted the same ones and 58 more in the same pass, so the branch side
is a superset and is taken whole. Confirmed every module unslothai#10498 lifted
(seed_routes, asyncio, fastapi, inspect, model_config_module) is present at
module level here.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant