Skip to content

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.

fix: Abort cancelled interactive prompts instead of accepting defaults (#259)

Rudy Mizrahi Celeklidaf66e6505b8
diff --git a/ifixai/cli/ui.py b/ifixai/cli/ui.py
index a8fa451..b4c069a 100644
--- a/ifixai/cli/ui.py
+++ b/ifixai/cli/ui.py
@@ -12,6 +12,8 @@ import sys
from collections.abc import Sequence
from functools import lru_cache
+import click
+
from ifixai.cli._branding import supports_color
_ACCENT = "rgb(232,99,42)"
@@ -117,15 +119,17 @@ def select(
if descriptions:
kwargs["show_description"] = True
answer = questionary.select(message, **kwargs).ask()
- return answer if answer is not None else (default or choices[0])
+ if answer is None:
+ raise click.Abort()
+ return answer
def select_or_abort(message: str, choices: Sequence[str], default: str) -> str:
"""Arrow-key single choice where Ctrl-C aborts instead of picking the default.
- ``select`` answers a cancelled prompt with its default, which is wrong when
- the default starts work the user just tried to stop. Raises
- ``KeyboardInterrupt`` on Ctrl-C. Callers must check ``is_interactive()`` first.
+ Like ``select``, this prompt does not choose a default after cancellation.
+ It propagates ``KeyboardInterrupt`` on Ctrl-C; ``select`` raises
+ ``click.Abort`` instead. Callers must check ``is_interactive()`` first.
"""
import questionary
@@ -160,7 +164,9 @@ def multiselect(
if descriptions:
kwargs["show_description"] = True
answer = questionary.checkbox(message, **kwargs).ask()
- return answer or []
+ if answer is None:
+ raise click.Abort()
+ return answer
def confirm(message: str, default: bool = True) -> bool:
@@ -170,7 +176,9 @@ def confirm(message: str, default: bool = True) -> bool:
import questionary
answer = questionary.confirm(message, default=default, style=_qstyle()).ask()
- return default if answer is None else bool(answer)
+ if answer is None:
+ raise click.Abort()
+ return bool(answer)
def text(message: str, default: str = "") -> str:
@@ -180,7 +188,9 @@ def text(message: str, default: str = "") -> str:
import questionary
answer = questionary.text(message, default=default, style=_qstyle()).ask()
- return default if answer is None else answer
+ if answer is None:
+ raise click.Abort()
+ return answer
def table(title: str, columns: Sequence[str], rows: Sequence[Sequence[str]]) -> bool:
diff --git a/ifixai/tests/test_cli_arrow_menu.py b/ifixai/tests/test_cli_arrow_menu.py
index 78d8c55..25b6f18 100644
--- a/ifixai/tests/test_cli_arrow_menu.py
+++ b/ifixai/tests/test_cli_arrow_menu.py
@@ -4,6 +4,7 @@ from collections.abc import Iterator
from pathlib import Path
from typing import Any
+import click
import pytest
from ifixai.cli import pro_offer, ui
@@ -68,13 +69,14 @@ def test_ctrl_c_raises_instead_of_answering_with_the_default(keys: Any) -> None:
ui.select_or_abort("Pick one", ["first", "second"], "second")
-def test_the_existing_select_still_answers_a_cancel_with_its_default(
+def test_select_aborts_cancel_instead_of_answering_with_its_default(
keys: Any, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(ui, "is_interactive", report_terminal_attached)
keys.send_text(CTRL_C)
- assert ui.select("Pick one", ["first", "second"], default="second") == "second"
+ with pytest.raises(click.Abort):
+ ui.select("Pick one", ["first", "second"], default="second")
@pytest.mark.parametrize(
diff --git a/ifixai/tests/test_ui_cancelled_prompts.py b/ifixai/tests/test_ui_cancelled_prompts.py
new file mode 100644
index 0000000..d6dccc5
--- /dev/null
+++ b/ifixai/tests/test_ui_cancelled_prompts.py
@@ -0,0 +1,28 @@
+from types import SimpleNamespace
+
+import click
+import pytest
+import questionary
+
+from ifixai.cli import ui
+
+
[email protected]('kind', ['select', 'checkbox', 'confirm', 'text'])
+def test_cancelled_interactive_prompt_aborts_instead_of_accepting_default(monkeypatch, kind):
+ monkeypatch.setattr(ui, 'is_interactive', lambda: True)
+ monkeypatch.setattr(questionary, kind, lambda *args, **kwargs: SimpleNamespace(ask=lambda: None))
+ with pytest.raises(click.Abort):
+ if kind == 'select':
+ ui.select('Choose provider', ['one', 'two'], default='one')
+ elif kind == 'checkbox':
+ ui.multiselect('Choose judges', ['one', 'two'])
+ elif kind == 'confirm':
+ ui.confirm('Run the paid audit now?', default=True)
+ else:
+ ui.text('Endpoint', default='http://localhost')
+
+
+def test_explicit_negative_confirmation_is_retained(monkeypatch):
+ monkeypatch.setattr(ui, 'is_interactive', lambda: True)
+ monkeypatch.setattr(questionary, 'confirm', lambda *args, **kwargs: SimpleNamespace(ask=lambda: False))
+ assert ui.confirm('Run now?', default=True) is False