Skip to content

studio/tests: cover the GGUF load ordering behaviourally and make the structlog stub order-independent - #7442

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
danielhanchen:fix/studio-inherited-extra-args-ordering
Jul 26, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
danielhanchen:fix/studio-inherited-extra-args-ordering

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 25, 2026 •

Copy link
Copy Markdown
Member

Status update

main no longer fails: #7443 (85f6231a2) landed the anchor fix for
test_load_marker_precedes_hub_guard_and_unload, so Backend CI is unblocked already. This branch is
now merged with main and keeps that landed assertion as is. What is left here is the extra
coverage this PR was carrying on top of it.

Original failure

studio/backend/tests/test_gguf_load_cache_reuse.py::TestLoadHubDownloadExclusion::test_load_marker_precedes_hub_guard_and_unload failed on main itself, so every open PR inherited the failure regardless of its diff (seen on #7392 and #7404, neither of which touches the load path).

assert source.index("= _resolve_inherited_extra_args(") < source.index("if config.is_gguf:")
E   assert 186995 < 185014

The assertion anchored on source.index("if config.is_gguf:"), a first-match string search over routes/inference.py. #7239 (a7761e174) reworked the GGUF GPU-pool validation in _load_model_impl into a bare if config.is_gguf: placed earlier in the function than the GGUF load branch, so that new statement became the first match and the assertion silently started comparing against the GPU-pool check. The endpoint ordering was never broken; only the assertion was wrong.

What this PR still adds

  1. test_inherited_extra_args_shape_hub_guard_require_mmproj: a behavioural test for the same invariant. It drives _load_model_impl over a vision GGUF whose previous same-model load stored --no-mmproj, stubs the hub guard to report a conflicting download (so the 409 is the observation point and no llama-server starts), and captures the require_mmproj the guard was called with:

    • inherited --no-mmproj gives require_mmproj = False
    • nothing to inherit gives require_mmproj = True
    • an explicit request list wins over the stored one, both directions

    Moving the _resolve_inherited_extra_args call to after the guard makes the inherited case report True and the test fails, so it detects the reorder without depending on how many if config.is_gguf: statements the endpoint happens to contain.

  2. Module-level structlog stub repair. routes/inference.py pulls in core.inference.external_provider, which binds structlog.get_logger(__name__) at import time. The stub gains a get_logger factory, and an existing bare structlog stub left in sys.modules by an earlier test module (for example test_checkpoints_scan.py) is repaired rather than silently kept by setdefault, so the import is order independent. Without it, pytest tests/test_checkpoints_scan.py tests/test_gguf_load_cache_reuse.py fails with AttributeError: module 'structlog' has no attribute 'get_logger'. The real structlog always has get_logger, so the installed package is never shadowed.

Testing

  • tests/test_gguf_load_cache_reuse.py: 34 passed, standalone
  • tests/test_checkpoints_scan.py tests/test_gguf_load_cache_reuse.py: 40 passed (fails before the stub repair)
  • Full backend run under the CI selection: no failures in this file
  • Mutation check: with the resolution call moved below the guard, the new test fails and the rest of the file still passes
  • ruff check clean

test_load_marker_precedes_hub_guard_and_unload fails on main, so every
open PR against the repo inherits the failure.

Root cause. unslothai#7239 (a7761e1) reworked the GGUF GPU-pool validation in
_load_model_impl from "if config.is_gguf and effective_gpu_ids is not
None:" to a bare "if config.is_gguf:", placed earlier in the function
than the GGUF load branch. The test anchors on
source.index("if config.is_gguf:"), a first-match search, so it silently
re-anchored onto the GPU-pool statement. unslothai#7251 (95f42bc) then restored
the assertion "= _resolve_inherited_extra_args(" before
"if config.is_gguf:" against a tree where that anchor already pointed at
the wrong statement, and main went red. Checking out 95f42bc and
running the suite reproduces the same single failure.

The code is correct. _resolve_inherited_extra_args still runs before the
GGUF load branch and before the hub-download guard that consumes
extra_llama_args for require_mmproj, so the guarantee unslothai#7251 protects is
intact; only the assertion is wrong.

Fix. Assert that guarantee behaviourally instead of by source offsets.
The new test drives _load_model_impl over a vision GGUF with a stored
--no-mmproj from a previous same-model load and captures the
require_mmproj the hub guard is called with: inherited --no-mmproj gives
False, nothing to inherit gives True, and an explicit request list wins
over the stored one both ways. Moving the resolution call after the
guard makes the inherited case report True and the test fails, so it
detects the reorder the old assertion was meant to catch, without
depending on how many "if config.is_gguf:" statements the endpoint has.

The surviving marker-before-guard-before-unload assertion had the same
ambiguous anchor for its slice start, silently widening the slice past
the GPU-pool block. It now slices from the "if config.is_gguf:" nearest
above the in-flight marker, which pins the load branch.

The structlog test stub gains a get_logger factory so routes/inference.py
is importable when structlog is absent.

34 pass in tests/test_gguf_load_cache_reuse.py (was 32 pass, 1 fail);
350 pass across it plus test_llama_cpp_mmproj_fallback.py and
test_llama_cpp_mtp_detection.py. A full backend run before and after is
identical apart from this test going from fail to pass.

@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: 9470bf2204

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

# structlog logger at import time; give the stub a working factory so the route
# module is importable when structlog is not installed.
_structlog_stub.get_logger = lambda *_args, **_kwargs: logging.getLogger("structlog_stub")
sys.modules.setdefault("structlog", _structlog_stub)

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 Repair an existing structlog stub before importing routes

When this test is run in a lightweight environment without structlog after a test such as test_checkpoints_scan.py, that earlier module can leave a bare ModuleType("structlog") in sys.modules. This setdefault preserves the existing stub, so the newly added get_logger is never installed; _load_route_module() then imports core.inference.external_provider, whose module-level structlog.get_logger(...) raises AttributeError before the behavioral test runs. Assign get_logger to sys.modules["structlog"] when it is missing, as the sibling llama.cpp tests do, to keep combined/subset test runs order-independent.

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 and fixed in bc89ab6. Reproduced with pytest tests/test_checkpoints_scan.py tests/test_gguf_load_cache_reuse.py: that module parks a bare ModuleType("structlog"), the setdefault here keeps it, and the route import then dies with AttributeError: module 'structlog' has no attribute 'get_logger' at core/inference/external_provider.py:25. The stub is now repaired when get_logger is missing, so the import is order independent. Real structlog always has get_logger, so the installed package is never shadowed. Standalone and combined runs both pass.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

…extra-args-ordering

# Conflicts:
#	studio/backend/tests/test_gguf_load_cache_reuse.py
@danielhanchen danielhanchen changed the title studio: fix Backend CI red on main from an ambiguous ordering anchor studio/tests: cover the GGUF load ordering behaviourally and make the structlog stub order-independent Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: ee547dc4ef

ℹ️ 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 d7cdc96 into unslothai:main Jul 26, 2026
31 of 34 checks passed
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