Repository navigation
fix: read LLAMA_ARG_* toggle pair variables after parsing - #2612
Merged
Merged
Conversation
clap registers an overrides_with partner as a direct conflict and counts an env-sourced value as present, while only a command-line occurrence removes the overridden half. Every --x / --no-x pair whose positive half carried a clap env binding therefore failed to parse when its variable was set and the negative flag was passed, for example LLAMA_ARG_MODELS_AUTOLOAD=true mlxcel serve --no-models-autoload. - Drop the clap env attribute from --models-autoload in both binaries and read LLAMA_ARG_MODELS_AUTOLOAD through env_fallback_models_autoload, following env_fallback_cors_credentials: only when neither flag is on the command line, warning on an unparseable value. - Apply the same fix to the other affected pairs found by the audit: --cache-prompt, --cont-batching, --context-shift, --cache-idle-slots and --kv-unified. Each group gains apply_env_bindings, which reads its variables with clap's boolish vocabulary when neither half was given and reports an invalid value by name; apply_bool_pair_env_bindings runs all three in both binaries. - mlxcel serve applies the bindings before the memory preflight and the Web UI router override, so both see the environment as the clap binding delivered it. cache_args::from_env applies them too. - Mark the six manifest entries as runtime env bindings covered by src/server/cli_input_tests.rs. - Add regression tests for both binaries and for every pair: env plus negative flag parses and resolves to the flag, env plus positive flag resolves to the flag, env alone still applies. Closes #2609
inureyes
marked this pull request as ready for review
October 26, 2026 05:53
This was referenced Oct 26, 2026
inureyes
added a commit
that referenced
this pull request
Oct 26, 2026
A comment that #2612 added in `run_serve_async` (`src/commands/serve.rs`) named the toggle pairs `--x` / `--no-x`. `tests/cli_help_consistency.rs` scans `src/commands` for flag names and requires each to appear in some help output, so `every_flag_named_in_command_source_exists_in_some_help` failed on main. The comment now says "on/off flag pairs". No code change. Found by the post-merge `make verify-rocm-held` gate on main `1c520eb1` (13,881 passed, this one failed). With the change, `cargo test --features rocm -p mlxcel --test cli_help_consistency` passes 27/27 and `cargo fmt --check` is clean. Refs #2609
This was referenced Oct 26, 2026
test(cli): forbid clap env bindings on overrides_with flag pairs, unify invalid-value handling
#2618
Closed
inureyes
added a commit
that referenced
this pull request
Oct 26, 2026
clap 4.6.7 counts an env-sourced value as present and registers every overrides_with partner as a direct conflict, so an env binding on either half of a --x / --no-x pair turns VAR=... plus the other half into a parse error (#2609). #2612 removed env from six pairs; nothing stopped a new arg from reintroducing it. - Add mlxcel::cli::env_bound_override_args, which walks a clap Command and its subcommands and lists every arg that binds an env variable while it overrides, or is overridden by, another arg. clap has no public override getter, so the helper reads the overrides field from the Arg Debug output and panics if the field disappears. A self-test on synthetic env + overrides_with args pins the parse. - Assert an empty result for Cli::command() in src/main_tests.rs and in the mlxcel-server tests module. - Route LLAMA_ARG_MODELS_AUTOLOAD through apply_boolish_pair_env. An invalid value is now a startup error with the same message as the other five pairs (b10621 throws on invalid pair values), and the y/t/n/f spellings the old parser rejected are accepted. Both startup call sites (mlxcel-server and mlxcel serve) return the error. Closes #2618
inureyes
added a commit
that referenced
this pull request
Oct 26, 2026
clap 4.6.7 counts an env-sourced value as present and registers every overrides_with partner as a direct conflict, so an env binding on either half of a --x / --no-x pair turns VAR=... plus the other half into a parse error (#2609). #2612 removed env from six pairs; nothing stopped a new arg from reintroducing it. - Add mlxcel::cli::env_bound_override_args, which walks a clap Command and its subcommands and lists every arg that binds an env variable while it overrides, or is overridden by, another arg. clap has no public override getter, so the helper reads the overrides field from the Arg Debug output and panics if the field disappears. A self-test on synthetic env + overrides_with args pins the parse. - Assert an empty result for Cli::command() in src/main_tests.rs and in the mlxcel-server tests module. - Route LLAMA_ARG_MODELS_AUTOLOAD through apply_boolish_pair_env. An invalid value is now a startup error with the same message as the other five pairs (b10621 throws on invalid pair values), and the y/t/n/f spellings the old parser rejected are accepted. Both startup call sites (mlxcel-server and mlxcel serve) return the error. Closes #2618
inureyes
added a commit
that referenced
this pull request
Oct 26, 2026
## Summary Follow-up to #2609 / #2612, both items of #2618 in one PR. ### 1. Guard against `env` + `overrides_with` clap 4.6.7 counts an env-sourced value as present and treats `overrides` as direct conflicts, so an `env` binding on either half of a `--x` / `--no-x` pair makes `VAR=... --other-half` a parse error. Nothing stopped a new arg from reintroducing the binding #2612 removed. - `mlxcel::cli::env_bound_override_args` (`src/cli/mod.rs`) walks a `Command` and every subcommand and returns each arg whose `get_env()` is `Some` and that overrides, or is overridden by, another arg in the same command. clap has no public override getter, so it reads the `overrides: [...]` field from the `Arg` `Debug` output and panics when that field is missing or unterminated, so a clap format change fails loudly instead of turning the guard into a no-op. - Self-tests in `cli::env_override_guard_tests` build synthetic `env` + `overrides_with` commands and check env on the overriding half, on the overridden half, no false positives (pair without env, env without pair), and subcommand walking. - `no_arg_combines_clap_env_with_overrides_with` in `src/main_tests.rs` (`Cli::command()` of `mlxcel`) and in the `tests` module of `src/bin/mlx_server.rs` assert an empty result. No allowlist. ### 2. Autoload invalid values `env_fallback_models_autoload` now goes through `apply_boolish_pair_env` and returns `anyhow::Result<()>`. `LLAMA_ARG_MODELS_AUTOLOAD=maybe` is a startup error with the same message as the other five pairs (`LLAMA_ARG_MODELS_AUTOLOAD has an invalid boolean value "maybe"`), and the `y`/`t`/`n`/`f` spellings are accepted. Both startup call sites (`build_startup_input` in `mlxcel-server`, `run_serve_async` in `mlxcel serve`) propagate it with `?`. The error formatting is shared with `apply_bool_pair_env_bindings` through one helper. ## Guard demonstration With `env = "LLAMA_ARG_MODELS_AUTOLOAD"` temporarily re-added to `mlxcel serve --models-autoload` (`src/main.rs`) and `env = "LLAMA_ARG_CACHE_PROMPT"` re-added to `--cache-prompt` (`src/cli/cache_args.rs`), both guard tests fail and name the offenders: ``` mlxcel serve models_autoload (env LLAMA_ARG_MODELS_AUTOLOAD) <-> _no_models_autoload mlxcel serve cache_prompt (env LLAMA_ARG_CACHE_PROMPT) <-> no_cache_prompt mlxcel-server cache_prompt (env LLAMA_ARG_CACHE_PROMPT) <-> no_cache_prompt ``` With the bindings removed again (this PR), both pass. ## Test plan - `cargo test --profile test-fast --lib cli::` (291 passed, including the 4 guard self-tests) - `cargo test --profile test-fast --lib server::cli_input` (155 passed; `models_autoload_env_rejects_an_unparseable_value`, `models_autoload_env_accepts_boolish_spellings`) - `cargo test --profile test-fast --bin mlxcel --bin mlxcel-server` (316 + 39 passed) - `cargo test --profile test-fast --test cli_help_consistency` (27 passed) - `cargo clippy --profile test-fast --bin mlxcel --bin mlxcel-server --lib --tests -- -D warnings`, `cargo fmt --all -- --check` - `make verify-llama-compat`, `make verify-env-var-docs` - Manual: `LLAMA_ARG_MODELS_AUTOLOAD=maybe mlxcel-server --models-dir <dir>` and `mlxcel serve --models-dir <dir>` both exit with `Error: LLAMA_ARG_MODELS_AUTOLOAD has an invalid boolean value "maybe"` - Full `make verify` not run in this unit (CPU-only worktree on a shared host); left to the merge gate. Closes #2618
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LLAMA_ARG_MODELS_AUTOLOAD=<anything> mlxcel serve --no-models-autoload(and the same onmlxcel-server) failed with clap's "cannot be used with '--models-autoload'" error. clap registers anoverrides_withpartner as a direct conflict and counts an env-sourced value as present, while only a command-line occurrence removes the overridden half. The audit the issue asks for found the same defect on five more pairs, all fixed here.Changes
--models-autoload(both binaries): the clapenvattribute is gone.env_fallback_models_autoloadinsrc/server/cli_input.rsreadsLLAMA_ARG_MODELS_AUTOLOADwithparse_env_boolonly when neither flag is on the command line and warns on an unparseable value, followingenv_fallback_cors_credentials.resolve_compat_togglestill resolves the pair.--cache-prompt,--cont-batching(CacheCompatArgs),--context-shift(ContextCompatArgs),--cache-idle-slots,--kv-unified(SlotCompatArgs): their clapenvattributes are gone too. Each group has anapply_env_bindingsmethod built oncli::apply_boolish_pair_env, which accepts clap'sBoolishValueParserspellings so a variable means what it meant before, and names the variable when its value is invalid.server::apply_bool_pair_env_bindingsruns all three groups.mlxcel serveapplies the fallbacks inrun_serve_async, before the memory preflight (which reads--kv-unified) and before the Web UI router override of--models-autoload.mlxcel-serverapplies them inbuild_startup_input, next to the CORS fallback.cache_args::from_envapplies its group's bindings as well.env_binding: runtimewithenv_test: src/server/cli_input_tests.rs. The doc comments still name each variable.Precedence: an explicit flag, either half, beats the environment. With no flag, the environment applies as it did before.
Tests
src/main_tests.rs::mlxcel_serve_models_autoload_env_yields_to_either_flag: sets the variable withScopedEnvto true/false/1/0.--no-models-autoloadresolves to disabled and--models-autoloadresolves to enabled. The variable alone still applies. The existingmlxcel_serve_no_models_autoload_reaches_the_resolved_toggleno longer unsets the variable, and the now-unusedScopedEnv::unsetis removed.mlxcel-server:models_autoload_env_does_not_conflict_with_either_flagcovers both flags, and covers the variable alone reachingbuild_startup_input.src/server/cli_input_tests.rs: models-autoload fallback precedence and an unparseable value. For the five other pairs: env plus--no-*parses and resolves to the flag, a falsey env value loses to the positive flag, env alone applies, and an invalid value is reported by name.Verification
cargo test --profile test-fast --bin mlxcel --bin mlxcel-server: 315 + 38 passedcargo test --profile test-fast --lib cli::: 287 passed.--lib server::cli_input: 154 passedcargo clippy --profile test-fast --bin mlxcel --bin mlxcel-server --lib --tests -- -D warnings: cleancargo fmt --all -- --check,make verify-llama-compat,make verify-env-var-docs: passOut of scope, noted only:
--cors-origins(LLAMA_ARG_CORS_ORIGINS) usesconflicts_with = "allowed_origins", so an env value also conflicts with--allowed-origins. That pair is documented as mutually exclusive, not an override pair, so it is left alone.Closes #2609