Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix(cli): validate saved defaults through option types (#173)
Rudy Mizrahi Celekli24c0aca885ca
diff --git a/ifixai/cli/run.py b/ifixai/cli/run.pyindex aae06e2..ea74b7f 100644--- a/ifixai/cli/run.py+++ b/ifixai/cli/run.py@@ -210,13 +210,14 @@ def _resolve_concurrency(flag_value: int | None, no_parallel: bool) -> int:def _cfg_value(ctx: click.Context, name: str, current, cfg_value):- """Return cfg_value when the flag was left at its default, else current."""+ """Use a saved default through its CLI type, preserving explicit flags."""from click.core import ParameterSourceif cfg_value is None:return currentif ctx.get_parameter_source(name) == ParameterSource.DEFAULT:- return cfg_value+ parameter = next(param for param in ctx.command.params if param.name == name)+ return parameter.type_cast_value(ctx, cfg_value)return current@@ -843,7 +844,10 @@ def run(if config_obj.judges and (ctx.get_parameter_source("judge_provider") == ParameterSource.DEFAULT):- judge_provider = tuple(j.provider for j in config_obj.judges)+ judge_provider = _cfg_value(+ ctx, "judge_provider", judge_provider,+ tuple(j.provider for j in config_obj.judges),+ )if (ctx.get_parameter_source("judge_model") == ParameterSource.DEFAULTand any(j.model for j in config_obj.judges)@@ -851,11 +855,11 @@ def run(judge_model = tuple((j.model or "") for j in config_obj.judges)if ctx.get_parameter_source("judge_api_key") == ParameterSource.DEFAULT:resolved_judge_keys: list[str] = []- for j in config_obj.judges:- if j.provider == provider and api_key:+ for judge_name in judge_provider:+ if judge_name == provider and api_key:resolved_judge_keys.append(api_key)else:- resolved_judge_keys.append(_lookup_env_api_key(j.provider) or "")+ resolved_judge_keys.append(_lookup_env_api_key(judge_name) or "")judge_api_key = tuple(resolved_judge_keys)if not quiet:click.echo(diff --git a/ifixai/tests/test_cli_config_choice_validation.py b/ifixai/tests/test_cli_config_choice_validation.pynew file mode 100644index 0000000..bba5534--- /dev/null+++ b/ifixai/tests/test_cli_config_choice_validation.py@@ -0,0 +1,100 @@+"""Saved CLI choices must obey the same contract as explicit flags."""++from pathlib import Path++import pytest+from click.testing import CliRunner++from ifixai.cli.main import ifixai_cli+++def _invoke(tmp_path, monkeypatch, config, extra=()):+ monkeypatch.chdir(tmp_path)+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("IFIXAI_TELEMETRY", "0")+ monkeypatch.setenv("DO_NOT_TRACK", "1")+ (tmp_path / "ifixai.yaml").write_text(config, encoding="utf-8")+ fixture = Path(__file__).parents[1] / "fixtures/examples/customer_support.yaml"+ return CliRunner().invoke(+ ifixai_cli,+ [+ "run",+ "--provider",+ "mock",+ "--api-key",+ "owned-unused-key",+ "--fixture",+ str(fixture),+ "--test",+ "B01",+ "--eval-mode",+ "self",+ "--output",+ str(tmp_path / "reports"),+ "--reliability-out",+ str(tmp_path / "runs"),+ "--min-score",+ "0",+ "--no-telemetry",+ "--no-parallel",+ *extra,+ ],+ )+++def test_config_format_is_normalized_and_report_files_are_written(+ tmp_path, monkeypatch+):+ result = _invoke(tmp_path, monkeypatch, "format: BOTH\n")+ assert result.exit_code == 2, result.output+ assert "Total execution time:" in result.output+ assert len(list((tmp_path / "reports").glob("*.json"))) == 1+ assert len(list((tmp_path / "reports").glob("*.md"))) == 2+++ "config,option",+ [+ ("format: csv\n", "--format"),+ ("mode: typo\n", "--mode"),+ ("auth_method: typo\n", "--auth-method"),+ ("grounding: typo\n", "--grounding"),+ ("judges: [{provider: typo}]\n", "--judge-provider"),+ ],+)+def test_invalid_saved_choice_fails_before_a_run(tmp_path, monkeypatch, config, option):+ result = _invoke(tmp_path, monkeypatch, config)+ assert result.exit_code == 2, result.output+ assert option in result.output+ assert "Invalid value" in result.output+ assert not (tmp_path / "runs").exists()+++def test_explicit_flag_overrides_invalid_saved_choice(tmp_path, monkeypatch):+ result = _invoke(tmp_path, monkeypatch, "format: csv\n", ["--format", "json"])+ assert result.exit_code == 2, result.output+ assert "Total execution time:" in result.output+ assert len(list((tmp_path / "reports").glob("*.json"))) == 1+++ "name,value,expected",+ [+ ("provider", "MOCK", "mock"),+ ("auth_method", "NONE", "none"),+ ("grounding", "FIXTURE", "fixture"),+ ("run_mode", "FULL", "full"),+ ("eval_mode", "SELF", "self"),+ ("report_format", "BOTH", "both"),+ ("judge_provider", ("MOCK",), ("mock",)),+ ],+)+def test_saved_choices_use_cli_case_insensitive_contract(name, value, expected):+ import click+ from click.core import ParameterSource++ from ifixai.cli.run import _cfg_value, run++ ctx = click.Context(run)+ ctx.set_parameter_source(name, ParameterSource.DEFAULT)+ assert _cfg_value(ctx, name, None, value) == expected