Skip to content

fix: read LLAMA_ARG_* toggle pair variables after parsing - #2612

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2609-models-autoload-env
Oct 26, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2609-models-autoload-env

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

LLAMA_ARG_MODELS_AUTOLOAD=<anything> mlxcel serve --no-models-autoload (and the same on mlxcel-server) failed with clap's "cannot be used with '--models-autoload'" error. 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. The audit the issue asks for found the same defect on five more pairs, all fixed here.

Changes

  • --models-autoload (both binaries): the clap env attribute is gone. env_fallback_models_autoload in src/server/cli_input.rs reads LLAMA_ARG_MODELS_AUTOLOAD with parse_env_bool only when neither flag is on the command line and warns on an unparseable value, following env_fallback_cors_credentials. resolve_compat_toggle still resolves the pair.
  • --cache-prompt, --cont-batching (CacheCompatArgs), --context-shift (ContextCompatArgs), --cache-idle-slots, --kv-unified (SlotCompatArgs): their clap env attributes are gone too. Each group has an apply_env_bindings method built on cli::apply_boolish_pair_env, which accepts clap's BoolishValueParser spellings so a variable means what it meant before, and names the variable when its value is invalid. server::apply_bool_pair_env_bindings runs all three groups.
  • Wiring: mlxcel serve applies the fallbacks in run_serve_async, before the memory preflight (which reads --kv-unified) and before the Web UI router override of --models-autoload. mlxcel-server applies them in build_startup_input, next to the CORS fallback. cache_args::from_env applies its group's bindings as well.
  • Manifest: the six entries now record env_binding: runtime with env_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 with ScopedEnv to true/false/1/0. --no-models-autoload resolves to disabled and --models-autoload resolves to enabled. The variable alone still applies. The existing mlxcel_serve_no_models_autoload_reaches_the_resolved_toggle no longer unsets the variable, and the now-unused ScopedEnv::unset is removed.
  • mlxcel-server: models_autoload_env_does_not_conflict_with_either_flag covers both flags, and covers the variable alone reaching build_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 passed
  • cargo test --profile test-fast --lib cli::: 287 passed. --lib server::cli_input: 154 passed
  • cargo clippy --profile test-fast --bin mlxcel --bin mlxcel-server --lib --tests -- -D warnings: clean
  • cargo fmt --all -- --check, make verify-llama-compat, make verify-env-var-docs: pass

Out of scope, noted only: --cors-origins (LLAMA_ARG_CORS_ORIGINS) uses conflicts_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

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 inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority status:done Completed labels Oct 26, 2026
@inureyes
inureyes marked this pull request as ready for review October 26, 2026 05:53
@inureyes
inureyes merged commit 9d20423 into main Oct 26, 2026
1 check passed
@inureyes
inureyes deleted the fix/issue-2609-models-autoload-env branch October 26, 2026 05:53
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
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
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: LLAMA_ARG_MODELS_AUTOLOAD makes --no-models-autoload fail to parse

1 participant