Skip to content

The agent that turns websites into APIs!

feat: AskUserQuestion free mode, abort handling, and follow-up chat

kalil0321089c4df7da6e
diff --git a/src/reverse_api/base_engineer.py b/src/reverse_api/base_engineer.py
index 62d4880..884018c 100644
--- a/src/reverse_api/base_engineer.py
+++ b/src/reverse_api/base_engineer.py
@@ -15,6 +15,8 @@ from .utils import generate_folder_name, get_docs_dir, get_history_path, get_scr
DEBUG = os.environ.get("DEBUG", "0") == "1"
+OTHER_OPTION = "Other (type your answer)"
+
class BaseEngineer(ABC):
"""Abstract base class for API reverse engineering implementations."""
@@ -172,6 +174,7 @@ class BaseEngineer(ABC):
for opt in options
]
if choices:
+ choices.append(OTHER_OPTION)
selected = await questionary.checkbox(
f" > {question_text}",
choices=choices,
@@ -188,7 +191,20 @@ class BaseEngineer(ABC):
if selected is None:
raise KeyboardInterrupt
- labels = [s.split(" - ")[0] if " - " in s else s for s in selected]
+ has_other = OTHER_OPTION in selected
+ labels = [s.split(" - ")[0] if " - " in s else s for s in selected if s != OTHER_OPTION]
+
+ if has_other:
+ other_text = await questionary.text(
+ " > Your answer: ",
+ qmark="",
+ style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),
+ ).ask_async()
+ if other_text is None:
+ raise KeyboardInterrupt
+ if other_text.strip():
+ labels.append(other_text.strip())
+
answers[question_text] = ", ".join(labels)
else:
answer = await questionary.text(
@@ -207,6 +223,7 @@ class BaseEngineer(ABC):
for opt in options
]
if choices:
+ choices.append(OTHER_OPTION)
answer = await questionary.select(
f" > {question_text}",
choices=choices,
@@ -222,8 +239,18 @@ class BaseEngineer(ABC):
if answer is None:
raise KeyboardInterrupt
- label = answer.split(" - ")[0] if " - " in answer else answer
- answers[question_text] = label
+ if answer == OTHER_OPTION:
+ answer = await questionary.text(
+ " > Your answer: ",
+ qmark="",
+ style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),
+ ).ask_async()
+ if answer is None:
+ raise KeyboardInterrupt
+ answers[question_text] = answer.strip()
+ else:
+ label = answer.split(" - ")[0] if " - " in answer else answer
+ answers[question_text] = label
else:
answer = await questionary.text(
f" > {question_text}",
@@ -243,6 +270,21 @@ class BaseEngineer(ABC):
self.ui.console.print()
return answers
+ async def _prompt_follow_up(self) -> str | None:
+ """Prompt user for a follow-up message. Returns None to finish."""
+ try:
+ self.ui.console.print(f" [{THEME_PRIMARY}]>[/{THEME_PRIMARY}] [dim]type a follow-up or press Enter to finish[/dim]")
+ answer = await questionary.text(
+ " > ",
+ qmark="",
+ style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),
+ ).ask_async()
+ if answer is None or answer.strip() == "":
+ return None
+ return answer.strip()
+ except KeyboardInterrupt:
+ return None
+
@staticmethod
def _get_opt_field(opt: Any, field: str) -> str:
"""Get a field from an option, supporting both dict and object access."""
@@ -593,6 +635,14 @@ Here is the output directory where you should save your generated files:
**IMPORTANT: You have access to the AskUserQuestion tool to ask clarifying questions during your analysis.**
Use this tool when you need to clarify functional requirements, prioritize features, choose between implementation approaches, or gather any other information that would help you generate better {task_description}.
+The AskUserQuestion tool supports:
+- **Multiple questions** in a single call (list of question objects)
+- **Single-select** questions with predefined options (user picks one, or types a custom answer)
+- **Multi-select** questions with predefined options (user picks multiple, or adds a custom answer)
+- **Free-form** questions with no options (user types their answer freely)
+
+Use free-form questions (empty options list) for open-ended input like URLs, credentials, descriptions, or any question where predefined choices don't make sense.
+
Your task is to:
1. **Read and analyze the HAR file** to understand all API calls that were captured. Look for:
diff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.py
index a422d6b..735bff9 100644
--- a/src/reverse_api/engineer.py
+++ b/src/reverse_api/engineer.py
@@ -39,8 +39,94 @@ class ClaudeEngineer(BaseEngineer):
# Auto-approve all other tools
return PermissionResultAllow(updated_input=input_data)
+ async def _process_streaming_response(self, client: ClaudeSDKClient) -> dict[str, Any] | None:
+ """Process a single streaming response from the SDK client.
+
+ Returns a result dict on success, or None on error.
+ """
+ async for message in client.receive_response():
+ if hasattr(message, "usage") and isinstance(message.usage, dict):
+ self.usage_metadata.update(message.usage)
+
+ if isinstance(message, AssistantMessage):
+ last_tool_name = None
+ for block in message.content:
+ if isinstance(block, ToolUseBlock):
+ last_tool_name = block.name
+ self.ui.tool_start(block.name, block.input)
+ self.message_store.save_tool_start(block.name, block.input)
+ elif isinstance(block, ToolResultBlock):
+ is_error = block.is_error if block.is_error else False
+
+ output = None
+ if hasattr(block, "content"):
+ output = block.content
+ elif hasattr(block, "result"):
+ output = block.result
+ elif hasattr(block, "output"):
+ output = block.output
+
+ tool_name = last_tool_name or "Tool"
+ self.ui.tool_result(tool_name, is_error, output)
+ self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)
+ elif isinstance(block, TextBlock):
+ self.ui.thinking(block.text)
+ self.message_store.save_thinking(block.text)
+
+ elif isinstance(message, ResultMessage):
+ if message.is_error:
+ self.ui.error(message.result or "Unknown error")
+ self.message_store.save_error(message.result or "Unknown error")
+ return None
+ else:
+ script_path = str(self.scripts_dir / self._get_client_filename())
+ local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None
+ self.ui.success(script_path, local_path)
+
+ if self.usage_metadata:
+ input_tokens = self.usage_metadata.get("input_tokens", 0)
+ output_tokens = self.usage_metadata.get("output_tokens", 0)
+ cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)
+ cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)
+
+ from .pricing import calculate_cost
+
+ cost = calculate_cost(
+ model_id=self.model,
+ input_tokens=input_tokens,
+ output_tokens=output_tokens,
+ cache_creation_tokens=cache_creation_tokens,
+ cache_read_tokens=cache_read_tokens,
+ )
+ self.usage_metadata["estimated_cost_usd"] = cost
+
+ self.ui.console.print(" [dim]Usage:[/dim]")
+ if input_tokens > 0:
+ self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")
+ if cache_creation_tokens > 0:
+ self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")
+ if cache_read_tokens > 0:
+ self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")
+ if output_tokens > 0:
+ self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")
+ self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")
+
+ result: dict[str, Any] = {
+ "script_path": script_path,
+ "usage": self.usage_metadata,
+ }
+ self.message_store.save_result(result)
+ return result
+
+ return None
+
async def analyze_and_generate(self) -> dict[str, Any] | None:
- """Run the reverse engineering analysis with Claude."""
+ """Run the reverse engineering analysis with Claude.
+
+ Supports follow-up messages: after the initial analysis completes,
+ the user can send follow-ups in the same session for iterative refinement.
+ Press Enter or Ctrl+C to finish and return to the REPL.
+ """
self.ui.header(self.run_id, self.prompt, self.model, self.sdk, mode="engineer")
self.ui.start_analysis()
self.message_store.save_prompt(self._build_analysis_prompt())
@@ -54,83 +140,34 @@ class ClaudeEngineer(BaseEngineer):
stderr=self._handle_cli_stderr,
)
+ last_result: dict[str, Any] | None = None
+
try:
async with ClaudeSDKClient(options=options) as client:
await client.query(self._build_analysis_prompt())
- async for message in client.receive_response():
- if hasattr(message, "usage") and isinstance(message.usage, dict):
- self.usage_metadata.update(message.usage)
-
- if isinstance(message, AssistantMessage):
- last_tool_name = None
- for block in message.content:
- if isinstance(block, ToolUseBlock):
- last_tool_name = block.name
- self.ui.tool_start(block.name, block.input)
- self.message_store.save_tool_start(block.name, block.input)
- elif isinstance(block, ToolResultBlock):
- is_error = block.is_error if block.is_error else False
-
- output = None
- if hasattr(block, "content"):
- output = block.content
- elif hasattr(block, "result"):
- output = block.result
- elif hasattr(block, "output"):
- output = block.output
-
- tool_name = last_tool_name or "Tool"
- self.ui.tool_result(tool_name, is_error, output)
- self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)
- elif isinstance(block, TextBlock):
- self.ui.thinking(block.text)
- self.message_store.save_thinking(block.text)
-
- elif isinstance(message, ResultMessage):
- if message.is_error:
- self.ui.error(message.result or "Unknown error")
- self.message_store.save_error(message.result or "Unknown error")
- return None
- else:
- script_path = str(self.scripts_dir / self._get_client_filename())
- local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None
- self.ui.success(script_path, local_path)
-
- if self.usage_metadata:
- input_tokens = self.usage_metadata.get("input_tokens", 0)
- output_tokens = self.usage_metadata.get("output_tokens", 0)
- cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)
- cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)
-
- from .pricing import calculate_cost
-
- cost = calculate_cost(
- model_id=self.model,
- input_tokens=input_tokens,
- output_tokens=output_tokens,
- cache_creation_tokens=cache_creation_tokens,
- cache_read_tokens=cache_read_tokens,
- )
- self.usage_metadata["estimated_cost_usd"] = cost
-
- self.ui.console.print(" [dim]Usage:[/dim]")
- if input_tokens > 0:
- self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")
- if cache_creation_tokens > 0:
- self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")
- if cache_read_tokens > 0:
- self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")
- if output_tokens > 0:
- self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")
- self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")
-
- result: dict[str, Any] = {
- "script_path": script_path,
- "usage": self.usage_metadata,
- }
- self.message_store.save_result(result)
- return result
+ # Process initial response
+ last_result = await self._process_streaming_response(client)
+ if last_result is None:
+ return None
+
+ # Conversation loop: prompt for follow-ups
+ while True:
+ follow_up = await self._prompt_follow_up()
+ if not follow_up:
+ return last_result
+
+ self.ui.console.print()
+ self.message_store.save_prompt(follow_up)
+ await client.query(follow_up)
+
+ result = await self._process_streaming_response(client)
+ if result is not None:
+ last_result = result
+
+ except KeyboardInterrupt:
+ self.ui.console.print("\n [dim]run aborted[/dim]")
+ return last_result
except Exception as e:
self.ui.error(str(e))
@@ -138,8 +175,6 @@ class ClaudeEngineer(BaseEngineer):
self.ui.console.print("\n[dim]Make sure Claude Code CLI is installed: npm install -g @anthropic-ai/claude-code[/dim]")
return None
- return None
-
# Keep old class name for backwards compatibility
APIReverseEngineer = ClaudeEngineer
@@ -232,6 +267,9 @@ def run_reverse_engineering(
try:
result = asyncio.run(engineer.analyze_and_generate())
+ except KeyboardInterrupt:
+ # Absorb interrupt so REPL continues instead of exiting
+ result = None
finally:
# Always stop sync when done
engineer.stop_sync()
diff --git a/tests/test_engineer.py b/tests/test_engineer.py
index 5157e2c..06b9bbb 100644
--- a/tests/test_engineer.py
+++ b/tests/test_engineer.py
@@ -5,6 +5,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from reverse_api.base_engineer import OTHER_OPTION
from reverse_api.engineer import APIReverseEngineer, ClaudeEngineer, run_reverse_engineering
@@ -100,6 +101,26 @@ class TestRunReverseEngineering:
)
mock_instance.stop_sync.assert_called_once()
+ def test_keyboard_interrupt_returns_none(self, tmp_path):
+ """KeyboardInterrupt is caught and returns None."""
+ har_path = tmp_path / "test.har"
+ har_path.touch()
+
+ with patch("reverse_api.engineer.ClaudeEngineer") as mock_cls:
+ mock_instance = MagicMock()
+ mock_cls.return_value = mock_instance
+ mock_instance.start_sync = MagicMock()
+ mock_instance.stop_sync = MagicMock()
+
+ with patch("reverse_api.engineer.asyncio.run", side_effect=KeyboardInterrupt):
+ result = run_reverse_engineering(
+ run_id="test123",
+ har_path=har_path,
+ prompt="test prompt",
+ )
+ assert result is None
+ mock_instance.stop_sync.assert_called_once()
+
def test_passes_all_params_to_claude(self, tmp_path):
"""All parameters are forwarded to ClaudeEngineer."""
har_path = tmp_path / "test.har"
@@ -133,8 +154,8 @@ class TestRunReverseEngineering:
assert kwargs["is_fresh"] is True
-class TestClaudeEngineerHandleAskUserQuestion:
- """Test _handle_ask_user_question method."""
+class TestAskUserInteractive:
+ """Test _ask_user_interactive method on BaseEngineer (via ClaudeEngineer)."""
def _make_engineer(self, tmp_path):
har_path = tmp_path / "test.har"
@@ -149,68 +170,100 @@ class TestClaudeEngineerHandleAskUserQuestion:
)
@pytest.mark.asyncio
- async def test_non_ask_user_tool_allows(self, tmp_path):
- """Non-AskUserQuestion tools return allow behavior."""
+ async def test_single_select_with_options(self, tmp_path):
+ """Single select question with options includes Other option."""
eng = self._make_engineer(tmp_path)
- result = await eng._handle_ask_user_question("Write", {"file_path": "/test.py"})
- assert result["behavior"] == "allow"
- assert result["updatedInput"] == {"file_path": "/test.py"}
+
+ mock_select = MagicMock()
+ mock_select.ask_async = AsyncMock(return_value="Option A - Description A")
+
+ with patch("reverse_api.base_engineer.questionary.select", return_value=mock_select) as mock_sel:
+ answers = await eng._ask_user_interactive([{
+ "question": "Which option?",
+ "header": "Choice",
+ "multiSelect": False,
+ "options": [
+ {"label": "Option A", "description": "Description A"},
+ {"label": "Option B", "description": "Description B"},
+ ],
+ }])
+ assert answers["Which option?"] == "Option A"
+ # Verify OTHER_OPTION was appended to choices
+ call_kwargs = mock_sel.call_args
+ choices = call_kwargs.kwargs.get("choices") or call_kwargs[1].get("choices")
+ assert OTHER_OPTION in choices
@pytest.mark.asyncio
- async def test_single_select_question(self, tmp_path):
- """Single select question with options."""
+ async def test_single_select_other_option(self, tmp_path):
+ """Selecting 'Other' in single-select prompts for free text."""
eng = self._make_engineer(tmp_path)
mock_select = MagicMock()
- mock_select.ask_async = AsyncMock(return_value="Option A - Description A")
+ mock_select.ask_async = AsyncMock(return_value=OTHER_OPTION)
+
+ mock_text = MagicMock()
+ mock_text.ask_async = AsyncMock(return_value="my custom answer")
- with patch("reverse_api.engineer.questionary.select", return_value=mock_select):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
+ with patch("reverse_api.base_engineer.questionary.select", return_value=mock_select):
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ answers = await eng._ask_user_interactive([{
"question": "Which option?",
- "header": "Choice",
+ "header": "",
"multiSelect": False,
- "options": [
- {"label": "Option A", "description": "Description A"},
- {"label": "Option B", "description": "Description B"},
- ],
- }],
- })
- assert result["behavior"] == "allow"
- assert result["updatedInput"]["answers"]["Which option?"] == "Option A"
+ "options": [{"label": "Option A"}],
+ }])
+ assert answers["Which option?"] == "my custom answer"
@pytest.mark.asyncio
- async def test_multi_select_question(self, tmp_path):
+ async def test_multi_select_with_options(self, tmp_path):
"""Multi select question with options."""
eng = self._make_engineer(tmp_path)
mock_checkbox = MagicMock()
mock_checkbox.ask_async = AsyncMock(return_value=["Option A - Desc", "Option B"])
- with patch("reverse_api.engineer.questionary.checkbox", return_value=mock_checkbox):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
+ with patch("reverse_api.base_engineer.questionary.checkbox", return_value=mock_checkbox):
+ answers = await eng._ask_user_interactive([{
+ "question": "Which features?",
+ "header": "Features",
+ "multiSelect": True,
+ "options": [
+ {"label": "Option A", "description": "Desc"},
+ {"label": "Option B"},
+ ],
+ }])
+ assert "Option A" in answers["Which features?"]
+
+ @pytest.mark.asyncio
+ async def test_multi_select_other_option(self, tmp_path):
+ """Selecting 'Other' in multi-select prompts for free text combined with other selections."""
+ eng = self._make_engineer(tmp_path)
+
+ mock_checkbox = MagicMock()
+ mock_checkbox.ask_async = AsyncMock(return_value=["Option A", OTHER_OPTION])
+
+ mock_text = MagicMock()
+ mock_text.ask_async = AsyncMock(return_value="custom extra")
+
+ with patch("reverse_api.base_engineer.questionary.checkbox", return_value=mock_checkbox):
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ answers = await eng._ask_user_interactive([{
"question": "Which features?",
- "header": "Features",
+ "header": "",
"multiSelect": True,
- "options": [
- {"label": "Option A", "description": "Desc"},
- {"label": "Option B"},
- ],
- }],
- })
- assert result["behavior"] == "allow"
- assert "Option A" in result["updatedInput"]["answers"]["Which features?"]
+ "options": [{"label": "Option A"}, {"label": "Option B"}],
+ }])
+ assert "Option A" in answers["Which features?"]
+ assert "custom extra" in answers["Which features?"]
@pytest.mark.asyncio
async def test_empty_question_skipped(self, tmp_path):
"""Empty question text is skipped."""
eng = self._make_engineer(tmp_path)
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{"question": "", "header": "", "multiSelect": False, "options": []}],
- })
- assert result["behavior"] == "allow"
- assert result["updatedInput"]["answers"] == {}
+ answers = await eng._ask_user_interactive(
+ [{"question": "", "header": "", "multiSelect": False, "options": []}]
+ )
+ assert answers == {}
@pytest.mark.asyncio
async def test_select_cancelled(self, tmp_path):
@@ -220,16 +273,14 @@ class TestClaudeEngineerHandleAskUserQuestion:
mock_select = MagicMock()
mock_select.ask_async = AsyncMock(return_value=None)
- with patch("reverse_api.engineer.questionary.select", return_value=mock_select):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Which option?",
- "header": "",
- "multiSelect": False,
- "options": [{"label": "A"}],
- }],
- })
- assert result["updatedInput"]["answers"]["Which option?"] == ""
+ with patch("reverse_api.base_engineer.questionary.select", return_value=mock_select):
+ answers = await eng._ask_user_interactive([{
+ "question": "Which option?",
+ "header": "",
+ "multiSelect": False,
+ "options": [{"label": "A"}],
+ }])
+ assert answers["Which option?"] == ""
@pytest.mark.asyncio
async def test_checkbox_cancelled(self, tmp_path):
@@ -239,92 +290,117 @@ class TestClaudeEngineerHandleAskUserQuestion:
mock_checkbox = MagicMock()
mock_checkbox.ask_async = AsyncMock(return_value=None)
- with patch("reverse_api.engineer.questionary.checkbox", return_value=mock_checkbox):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Which features?",
- "header": "",
- "multiSelect": True,
- "options": [{"label": "A"}],
- }],
- })
- assert result["updatedInput"]["answers"]["Which features?"] == ""
+ with patch("reverse_api.base_engineer.questionary.checkbox", return_value=mock_checkbox):
+ answers = await eng._ask_user_interactive([{
+ "question": "Which features?",
+ "header": "",
+ "multiSelect": True,
+ "options": [{"label": "A"}],
+ }])
+ assert answers["Which features?"] == ""
@pytest.mark.asyncio
- async def test_text_fallback_single_select(self, tmp_path):
- """Text input fallback when no options for single select."""
+ async def test_text_fallback_no_options(self, tmp_path):
+ """Text input fallback when no options."""
eng = self._make_engineer(tmp_path)
mock_text = MagicMock()
mock_text.ask_async = AsyncMock(return_value="custom answer")
- with patch("reverse_api.engineer.questionary.text", return_value=mock_text):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Enter value?",
- "header": "",
- "multiSelect": False,
- "options": [],
- }],
- })
- assert result["updatedInput"]["answers"]["Enter value?"] == "custom answer"
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ answers = await eng._ask_user_interactive([{
+ "question": "Enter value?",
+ "header": "",
+ "multiSelect": False,
+ "options": [],
+ }])
+ assert answers["Enter value?"] == "custom answer"
@pytest.mark.asyncio
- async def test_text_fallback_multi_select(self, tmp_path):
- """Text input fallback when no options for multi select."""
+ async def test_text_fallback_cancelled(self, tmp_path):
+ """Cancelled text input returns empty answer."""
eng = self._make_engineer(tmp_path)
mock_text = MagicMock()
- mock_text.ask_async = AsyncMock(return_value="custom answer")
+ mock_text.ask_async = AsyncMock(return_value=None)
- with patch("reverse_api.engineer.questionary.text", return_value=mock_text):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Enter value?",
- "header": "",
- "multiSelect": True,
- "options": [],
- }],
- })
- assert result["updatedInput"]["answers"]["Enter value?"] == "custom answer"
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ answers = await eng._ask_user_interactive([{
+ "question": "Enter value?",
+ "header": "",
+ "multiSelect": False,
+ "options": [],
+ }])
+ assert answers["Enter value?"] == ""
@pytest.mark.asyncio
- async def test_text_fallback_cancelled(self, tmp_path):
- """Cancelled text input returns empty answer."""
+ async def test_multiple_questions(self, tmp_path):
+ """Multiple questions in a single call."""
eng = self._make_engineer(tmp_path)
+ mock_select = MagicMock()
+ mock_select.ask_async = AsyncMock(return_value="A")
+
mock_text = MagicMock()
- mock_text.ask_async = AsyncMock(return_value=None)
+ mock_text.ask_async = AsyncMock(return_value="free answer")
+
+ with patch("reverse_api.base_engineer.questionary.select", return_value=mock_select):
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ answers = await eng._ask_user_interactive([
+ {
+ "question": "Pick one?",
+ "header": "Section 1",
+ "multiSelect": False,
+ "options": [{"label": "A"}, {"label": "B"}],
+ },
+ {
+ "question": "Describe it?",
+ "header": "Section 2",
+ "multiSelect": False,
+ "options": [],
+ },
+ ])
+ assert answers["Pick one?"] == "A"
+ assert answers["Describe it?"] == "free answer"
+
+
+class TestHandleToolPermission:
+ """Test _handle_tool_permission method."""
- with patch("reverse_api.engineer.questionary.text", return_value=mock_text):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Enter value?",
- "header": "",
- "multiSelect": False,
- "options": [],
- }],
- })
- assert result["updatedInput"]["answers"]["Enter value?"] == ""
+ def _make_engineer(self, tmp_path):
+ har_path = tmp_path / "test.har"
+ har_path.touch()
+ with patch("reverse_api.base_engineer.get_scripts_dir", return_value=tmp_path / "scripts"):
+ with patch("reverse_api.base_engineer.MessageStore"):
+ return ClaudeEngineer(
+ run_id="test123",
+ har_path=har_path,
+ prompt="test prompt",
+ output_dir=str(tmp_path),
+ )
@pytest.mark.asyncio
- async def test_question_with_header(self, tmp_path):
- """Question with header displays it."""
+ async def test_non_ask_user_tool_auto_approves(self, tmp_path):
+ """Non-AskUserQuestion tools are auto-approved."""
eng = self._make_engineer(tmp_path)
+ result = await eng._handle_tool_permission("Write", {"file_path": "/test.py"}, None)
+ assert result.updated_input == {"file_path": "/test.py"}
- mock_select = MagicMock()
- mock_select.ask_async = AsyncMock(return_value="A")
+ @pytest.mark.asyncio
+ async def test_ask_user_question_delegates(self, tmp_path):
+ """AskUserQuestion delegates to _ask_user_interactive."""
+ eng = self._make_engineer(tmp_path)
- with patch("reverse_api.engineer.questionary.select", return_value=mock_select):
- result = await eng._handle_ask_user_question("AskUserQuestion", {
- "questions": [{
- "question": "Pick?",
- "header": "Section Header",
- "multiSelect": False,
- "options": [{"label": "A"}],
- }],
- })
- assert result["behavior"] == "allow"
+ mock_text = MagicMock()
+ mock_text.ask_async = AsyncMock(return_value="user answer")
+
+ with patch("reverse_api.base_engineer.questionary.text", return_value=mock_text):
+ result = await eng._handle_tool_permission(
+ "AskUserQuestion",
+ {"questions": [{"question": "What?", "header": "", "multiSelect": False, "options": []}]},
+ None,
+ )
+ assert result.updated_input["answers"]["What?"] == "user answer"
class TestClaudeEngineerAnalyzeAndGenerate:
@@ -386,8 +462,8 @@ class TestClaudeEngineerAnalyzeAndGenerate:
return mock
@pytest.mark.asyncio
- async def test_successful_generation(self, tmp_path):
- """Successful analysis returns result dict."""
+ async def test_successful_generation_no_followup(self, tmp_path):
+ """Successful analysis with no follow-up returns result dict."""
eng = self._make_engineer(tmp_path)
mock_result = self._make_result_message(is_error=False)
@@ -404,9 +480,10 @@ class TestClaudeEngineerAnalyzeAndGenerate:
mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
- result = await eng.analyze_and_generate()
- assert result is not None
- assert "script_path" in result
+ with patch.object(eng, "_prompt_follow_up", new_callable=AsyncMock, return_value=None):
+ result = await eng.analyze_and_generate()
+ assert result is not None
+ assert "script_path" in result
@pytest.mark.asyncio
async def test_result_error(self, tmp_path):
@@ -471,9 +548,16 @@ class TestClaudeEngineerAnalyzeAndGenerate:
mock_client = AsyncMock()
mock_client.query = AsyncMock()
+ call_count = 0
+
async def mock_receive():
- yield mock_assistant
- yield mock_result
+ nonlocal call_count
+ call_count += 1
+ if call_count == 1:
+ yield mock_assistant
+ yield mock_result
+ else:
+ yield mock_result
mock_client.receive_response = mock_receive
@@ -481,8 +565,9 @@ class TestClaudeEngineerAnalyzeAndGenerate:
mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
- result = await eng.analyze_and_generate()
- assert result is not None
+ with patch.object(eng, "_prompt_follow_up", new_callable=AsyncMock, return_value=None):
+ result = await eng.analyze_and_generate()
+ assert result is not None
@pytest.mark.asyncio
async def test_exception_handling(self, tmp_path):
@@ -496,6 +581,58 @@ class TestClaudeEngineerAnalyzeAndGenerate:
result = await eng.analyze_and_generate()
assert result is None
+ @pytest.mark.asyncio
+ async def test_keyboard_interrupt_returns_partial(self, tmp_path):
+ """KeyboardInterrupt during streaming returns last result or None."""
+ eng = self._make_engineer(tmp_path)
+
+ mock_client = AsyncMock()
+ mock_client.query = AsyncMock()
+
+ async def mock_receive():
+ raise KeyboardInterrupt
+
+ mock_client.receive_response = mock_receive
+
+ with patch("reverse_api.engineer.ClaudeSDKClient") as mock_sdk:
+ mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
+ mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
+
+ result = await eng.analyze_and_generate()
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_follow_up_sends_new_query(self, tmp_path):
+ """Follow-up message sends a new query and processes response."""
+ eng = self._make_engineer(tmp_path)
+
+ mock_result = self._make_result_message(is_error=False)
+
+ mock_client = AsyncMock()
+ mock_client.query = AsyncMock()
+
+ async def mock_receive():
+ yield mock_result
+
+ mock_client.receive_response = mock_receive
+
+ follow_up_responses = iter(["refine the auth handling", None])
+
+ with patch("reverse_api.engineer.ClaudeSDKClient") as mock_sdk:
+ mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
+ mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
+
+ with patch.object(
+ eng,
+ "_prompt_follow_up",
+ new_callable=AsyncMock,
+ side_effect=lambda: next(follow_up_responses),
+ ):
+ result = await eng.analyze_and_generate()
+ assert result is not None
+ # Initial query + follow-up query
+ assert mock_client.query.call_count == 2
+
@pytest.mark.asyncio
async def test_result_with_usage_metadata(self, tmp_path):
"""Result with usage metadata calculates cost."""
@@ -521,10 +658,11 @@ class TestClaudeEngineerAnalyzeAndGenerate:
mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
- result = await eng.analyze_and_generate()
- assert result is not None
- assert "usage" in result
- assert "estimated_cost_usd" in result["usage"]
+ with patch.object(eng, "_prompt_follow_up", new_callable=AsyncMock, return_value=None):
+ result = await eng.analyze_and_generate()
+ assert result is not None
+ assert "usage" in result
+ assert "estimated_cost_usd" in result["usage"]
@pytest.mark.asyncio
async def test_result_with_local_scripts_dir(self, tmp_path):
@@ -545,5 +683,7 @@ class TestClaudeEngineerAnalyzeAndGenerate:
with patch("reverse_api.engineer.ClaudeSDKClient") as mock_sdk:
mock_sdk.return_value.__aenter__ = AsyncMock(return_value=mock_client)
mock_sdk.return_value.__aexit__ = AsyncMock(return_value=False)
- result = await eng.analyze_and_generate()
- assert result is not None
+
+ with patch.object(eng, "_prompt_follow_up", new_callable=AsyncMock, return_value=None):
+ result = await eng.analyze_and_generate()
+ assert result is not None