The agent that turns websites into APIs!
fix: prefer active engineer client over config
kalil0321ab3f51edf369
diff --git a/src/reverse_api/base_engineer.py b/src/reverse_api/base_engineer.pyindex 789e00e..0528feb 100644--- a/src/reverse_api/base_engineer.py+++ b/src/reverse_api/base_engineer.py@@ -7,9 +7,10 @@ from typing import Anyimport questionaryfrom .messages import MessageStore+from .session import SessionManagerfrom .sync import FileSyncWatcher, get_available_directoryfrom .tui import THEME_PRIMARY, THEME_SECONDARY, ClaudeUI-from .utils import generate_folder_name, get_docs_dir, get_scripts_dir+from .utils import generate_folder_name, get_docs_dir, get_history_path, get_scripts_dirclass BaseEngineer(ABC):@@ -244,26 +245,69 @@ class BaseEngineer(ABC):candidates[language] = client_pathreturn candidates- def _resolve_output_language(self, requested_language: str) -> str:- """Keep iterative edits in the same language as the existing client."""+ def _get_recorded_client_path(self, existing_clients: dict[str, Path] | None = None) -> Path | None:+ """Return the last generated client path recorded in session history."""if self.output_mode == "docs" or self.is_fresh:- return requested_language+ return None++ try:+ session_manager = SessionManager(get_history_path())+ run_data = session_manager.get_run(self.run_id)+ except Exception:+ return None++ if not run_data:+ return None++ script_path = run_data.get("paths", {}).get("script_path")+ if not script_path:+ return None++ resolved_path = Path(script_path)+ if not resolved_path.exists():+ return None++ candidates = existing_clients or self._get_existing_client_candidates()+ if resolved_path not in candidates.values():+ return None++ return resolved_path++ def _get_preferred_existing_client(self) -> tuple[str, Path] | None:+ """Return the existing client that iterative edits should continue from."""+ if self.output_mode == "docs" or self.is_fresh:+ return Noneexisting_clients = self._get_existing_client_candidates()if not existing_clients:- return requested_language+ return None- if requested_language in existing_clients:- return requested_language+ recorded_client_path = self._get_recorded_client_path(existing_clients)+ if recorded_client_path:+ for language, client_path in existing_clients.items():+ if client_path == recorded_client_path:+ return language, client_pathreturn max(existing_clients.items(),key=lambda item: item[1].stat().st_mtime_ns,- )[0]+ )++ def _resolve_output_language(self, requested_language: str) -> str:+ """Keep iterative edits in the same language as the existing client."""+ if self.output_mode == "docs" or self.is_fresh:+ return requested_language++ preferred_client = self._get_preferred_existing_client()+ if preferred_client:+ return preferred_client[0]++ return requested_languagedef _get_existing_client_path(self) -> Path | None:"""Return the current client path when iterating on an existing run."""- return self._get_existing_client_candidates().get(self.output_language)+ preferred_client = self._get_preferred_existing_client()+ return preferred_client[1] if preferred_client else Nonedef _get_language_name(self) -> str:"""Return a human-readable language name."""diff --git a/tests/test_base_engineer.py b/tests/test_base_engineer.pyindex e173eb8..7327b3a 100644--- a/tests/test_base_engineer.py+++ b/tests/test_base_engineer.py@@ -69,17 +69,74 @@ class TestBaseEngineerInit:with patch("reverse_api.base_engineer.get_scripts_dir", return_value=scripts_dir):with patch("reverse_api.base_engineer.MessageStore"):- engineer = ConcreteEngineer(- run_id="test123",- har_path=har_path,- prompt="test prompt",- output_language="python",- output_dir=str(tmp_path),- )+ with patch("reverse_api.base_engineer.SessionManager") as mock_session_manager:+ mock_session_manager.return_value.get_run.return_value = None+ engineer = ConcreteEngineer(+ run_id="test123",+ har_path=har_path,+ prompt="test prompt",+ output_language="python",+ output_dir=str(tmp_path),+ )assert engineer.output_language == "typescript"assert engineer.existing_client_path == client_path+ def test_existing_client_language_uses_recorded_script_path(self, tmp_path):+ """Recorded script path takes precedence over config and stale files."""+ har_path = tmp_path / "test.har"+ har_path.touch()+ scripts_dir = tmp_path / "scripts"+ scripts_dir.mkdir()+ python_client = scripts_dir / "api_client.py"+ python_client.write_text("print('python')\n")+ typescript_client = scripts_dir / "api_client.ts"+ typescript_client.write_text("export {};\n")++ with patch("reverse_api.base_engineer.get_scripts_dir", return_value=scripts_dir):+ with patch("reverse_api.base_engineer.MessageStore"):+ with patch("reverse_api.base_engineer.SessionManager") as mock_session_manager:+ mock_session_manager.return_value.get_run.return_value = {+ "paths": {"script_path": str(typescript_client)}+ }+ engineer = ConcreteEngineer(+ run_id="test123",+ har_path=har_path,+ prompt="test prompt",+ output_language="python",+ output_dir=str(tmp_path),+ )++ assert engineer.output_language == "typescript"+ assert engineer.existing_client_path == typescript_client++ def test_existing_client_language_falls_back_to_newest_file(self, tmp_path):+ """Newest existing client wins when no recorded script path is available."""+ har_path = tmp_path / "test.har"+ har_path.touch()+ scripts_dir = tmp_path / "scripts"+ scripts_dir.mkdir()+ python_client = scripts_dir / "api_client.py"+ python_client.write_text("print('python')\n")+ typescript_client = scripts_dir / "api_client.ts"+ typescript_client.write_text("export {};\n")+ typescript_client.touch()++ with patch("reverse_api.base_engineer.get_scripts_dir", return_value=scripts_dir):+ with patch("reverse_api.base_engineer.MessageStore"):+ with patch("reverse_api.base_engineer.SessionManager") as mock_session_manager:+ mock_session_manager.return_value.get_run.return_value = None+ engineer = ConcreteEngineer(+ run_id="test123",+ har_path=har_path,+ prompt="test prompt",+ output_language="python",+ output_dir=str(tmp_path),+ )++ assert engineer.output_language == "typescript"+ assert engineer.existing_client_path == typescript_client+def test_fresh_runs_can_switch_output_language(self, tmp_path):"""Fresh runs ignore existing client language and honor the requested one."""har_path = tmp_path / "test.har"@@ -255,14 +312,16 @@ class TestBaseEngineerBuildPrompt:with patch("reverse_api.base_engineer.get_scripts_dir", return_value=scripts_dir):with patch("reverse_api.base_engineer.get_docs_dir", return_value=tmp_path / "docs"):with patch("reverse_api.base_engineer.MessageStore") as mock_ms:- mock_ms.return_value.messages_path = tmp_path / "messages" / "test.jsonl"- eng = ConcreteEngineer(- run_id="test123",- har_path=har_path,- prompt="test prompt",- output_language="python",- output_dir=str(tmp_path),- )+ with patch("reverse_api.base_engineer.SessionManager") as mock_session_manager:+ mock_ms.return_value.messages_path = tmp_path / "messages" / "test.jsonl"+ mock_session_manager.return_value.get_run.return_value = None+ eng = ConcreteEngineer(+ run_id="test123",+ har_path=har_path,+ prompt="test prompt",+ output_language="python",+ output_dir=str(tmp_path),+ )prompt = eng._build_analysis_prompt()assert str(client_path) in prompt