The agent that turns websites into APIs!
Fix "Prompt is too long" session deadlock (#93) (#111)
kalil03219da742ea1ae8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e4ef133..8b7ec1e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- **Ollama setup**: OpenCode mode can discover tool-capable Ollama models, start an installed daemon, and inject provider configuration without modifying the user's `opencode.json`.### Fixed+- **"Prompt is too long" session deadlock** (#93): Claude SDK sessions now run with proactive auto-compaction configured per the Claude Code docs — `CLAUDE_CODE_AUTO_COMPACT_WINDOW` (capped by the CLI to the model's real context window) plus `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=85` — leaving enough headroom that a max-size HAR read landing near the threshold no longer jumps straight past the hard context limit. Both variables are respected if the user sets them explicitly. When the window is exhausted anyway, the error is now explained (progress is saved on disk; start a new run for the same target to continue) and the follow-up prompt is no longer offered on the dead session, where every further message would fail with the same error.- **OpenCode permissions**: Permission V2 events now reply through OpenCode's current, non-deprecated permission endpoint while retaining compatibility with older servers.- **OpenCode errors**: Known authentication, model configuration, and temporary provider-availability failures now produce one actionable message without the unexpected-error issue prompt.- **OpenCode TUI prompt echo**: Streamed text is now restricted to assistant message IDs, preventing RAE's internal browser and reverse-engineering instructions from appearing as model output.diff --git a/src/reverse_api/auto_engineer.py b/src/reverse_api/auto_engineer.pyindex 5bc2ae7..563b829 100644--- a/src/reverse_api/auto_engineer.py+++ b/src/reverse_api/auto_engineer.py@@ -24,7 +24,7 @@ from .agent_browser import ()from .engineer import ClaudeEngineerfrom .opencode_engineer import OpenCodeEngineer, debug_log, format_error-from .utils import get_har_dir+from .utils import build_sdk_env, get_har_dir# Suppress claude_agent_sdk logslogging.getLogger("claude_agent_sdk").setLevel(logging.WARNING)@@ -192,6 +192,10 @@ class ClaudeAutoEngineer(ClaudeEngineer):self.ui.header(self.run_id, self.prompt, self.model, mode="agent")self.ui.start_analysis()+ # Fresh SDK session, fresh context window: clear any overflow state+ # left over from a previous run on a reused engineer instance.+ self._context_overflowed = False+if self.agent_provider == "agent-browser":ab_setup = ensure_agent_browser_runtime()print_agent_browser_setup_notices(self.ui.console, ab_setup)@@ -212,7 +216,7 @@ class ClaudeAutoEngineer(ClaudeEngineer):can_use_tool=self._handle_tool_permission,cwd=str(self.scripts_dir.parent.parent),model=self.model,- env={"CLAUDECODE": ""},+ env=build_sdk_env(),stderr=self._handle_cli_stderr,)else:@@ -224,7 +228,7 @@ class ClaudeAutoEngineer(ClaudeEngineer):can_use_tool=self._handle_tool_permission,cwd=str(self.scripts_dir.parent.parent),model=self.model,- env={"CLAUDECODE": ""},+ env=build_sdk_env(),stderr=self._handle_cli_stderr,)@@ -252,6 +256,11 @@ class ClaudeAutoEngineer(ClaudeEngineer):result = await self._process_streaming_response(client)if result is not None:last_result = result+ elif self._context_overflowed:+ # The conversation exceeds the context window; every+ # further query would fail the same way, so end the+ # follow-up loop instead of offering another turn.+ return last_resultexcept KeyboardInterrupt:self.ui.console.print("\n [dim]run aborted[/dim]")diff --git a/src/reverse_api/collector.py b/src/reverse_api/collector.pyindex 8e9372a..dba22e8 100644--- a/src/reverse_api/collector.py+++ b/src/reverse_api/collector.py@@ -18,7 +18,7 @@ from claude_agent_sdk import (from .collector_ui import CollectorUIfrom .messages import MessageStorefrom .pricing import calculate_cost-from .utils import generate_folder_name, get_collected_dir+from .utils import build_sdk_env, generate_folder_name, get_collected_dir# Suppress claude_agent_sdk logslogging.getLogger("claude_agent_sdk").setLevel(logging.WARNING)@@ -112,6 +112,7 @@ class Collector:],cwd=str(self._collected_dir),model=self.model,+ env=build_sdk_env(),)try:diff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.pyindex 83390ad..b5515a0 100644--- a/src/reverse_api/engineer.py+++ b/src/reverse_api/engineer.py@@ -18,6 +18,7 @@ from claude_agent_sdk import ()from .base_engineer import BaseEngineer+from .utils import build_sdk_env, is_context_overflow_error# Suppress claude_agent_sdk logslogging.getLogger("claude_agent_sdk").setLevel(logging.WARNING)@@ -27,6 +28,22 @@ logging.getLogger("claude_agent_sdk._internal.transport.subprocess_cli").setLeveclass ClaudeEngineer(BaseEngineer):"""Uses Claude Agent SDK to analyze HAR files and generate Python API scripts."""+ # Set when the session dies with a context-window overflow ("Prompt is+ # too long"). The conversation itself exceeds the model's limit at that+ # point, so no further query on the same client can succeed and the+ # follow-up loop must not offer another turn.+ _context_overflowed = False++ def _print_context_overflow_help(self) -> None:+ """Explain a context-window overflow and how to continue."""+ self.ui.console.print()+ self.ui.console.print(" [yellow]The session ran out of context window (too much captured traffic for one conversation).[/yellow]")+ self.ui.console.print(" [dim]This session can't continue, but progress is saved: generated files are in[/dim]")+ self.ui.console.print(f" [dim]{self.scripts_dir}[/dim]")+ self.ui.console.print(f" [dim]Start a new run for this target (run id: {self.run_id}) to keep iterating —[/dim]")+ self.ui.console.print(" [dim]it picks up the existing client and HAR from disk. A shorter browsing[/dim]")+ self.ui.console.print(" [dim]session (fewer pages before closing the browser) also keeps the HAR smaller.[/dim]")+async def _handle_tool_permission(self, tool_name: str, input_data: dict[str, Any], context: ToolPermissionContext) -> PermissionResultAllow:"""Handle tool permission requests, with interactive UI for AskUserQuestion."""if tool_name == "AskUserQuestion":@@ -90,8 +107,12 @@ class ClaudeEngineer(BaseEngineer):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")+ error_text = message.result or "Unknown error"+ self.ui.error(error_text)+ self.message_store.save_error(error_text)+ if is_context_overflow_error(error_text):+ self._context_overflowed = True+ self._print_context_overflow_help()return Noneelse:script_path = str(self.scripts_dir / self._get_client_filename())@@ -145,6 +166,10 @@ class ClaudeEngineer(BaseEngineer):self.ui.header(self.run_id, self.prompt, self.model, self.sdk, mode="engineer")self.ui.start_analysis()+ # Fresh SDK session, fresh context window: clear any overflow state+ # left over from a previous run on a reused engineer instance.+ self._context_overflowed = False+system_prompt, user_message = self._build_prompts()self.message_store.save_prompt(user_message)@@ -154,7 +179,7 @@ class ClaudeEngineer(BaseEngineer):can_use_tool=self._handle_tool_permission,cwd=str(self.scripts_dir.parent.parent),model=self.model,- env={"CLAUDECODE": ""},+ env=build_sdk_env(),stderr=self._handle_cli_stderr,)@@ -182,6 +207,11 @@ class ClaudeEngineer(BaseEngineer):result = await self._process_streaming_response(client)if result is not None:last_result = result+ elif self._context_overflowed:+ # The conversation exceeds the context window; every+ # further query would fail the same way, so end the+ # follow-up loop instead of offering another turn.+ return last_resultexcept KeyboardInterrupt:self.ui.console.print("\n [dim]run aborted[/dim]")diff --git a/src/reverse_api/utils.py b/src/reverse_api/utils.pyindex d2e506e..ea25e90 100644--- a/src/reverse_api/utils.py+++ b/src/reverse_api/utils.py@@ -29,6 +29,58 @@ OUTPUT_LANGUAGE_EXTENSIONS = {SCRIPT_EXTENSIONS = frozenset(OUTPUT_LANGUAGE_EXTENSIONS.values())+# Claude Code's default auto-compaction leaves less headroom than a single+# max-size tool result (Read caps at ~25k tokens), so a large HAR read landing+# between the compact check and the next API request can jump straight past+# the hard context limit — after which every request, including the compaction+# request itself, fails with "Prompt is too long"+# (github.com/kalil0321/reverse-api-engineer issues/93).+#+# Per https://code.claude.com/docs/en/env-vars, CLAUDE_AUTOCOMPACT_PCT_OVERRIDE+# lowers the compaction threshold, but only when Claude Code compacts+# *proactively* — which for local sessions on newer models requires+# CLAUDE_CODE_AUTO_COMPACT_WINDOW to be set (otherwise compaction waits for+# the model's context limit: exactly the deadlock-prone behavior). The window+# value is capped at the model's actual context window, so a large value+# means "the model's full window" on every model. 85% keeps at least ~30k+# tokens of headroom on a 200k window.+AUTOCOMPACT_PCT = "85"+AUTOCOMPACT_WINDOW = "1000000"++# Substrings (lowercase) identifying the API's context-window-exhausted+# errors as surfaced through the CLI's result message.+_CONTEXT_OVERFLOW_MARKERS = (+ "prompt is too long",+ "conversation too long",+ "exceed context limit",+)+++def build_sdk_env() -> dict[str, str]:+ """Environment overrides for Claude Agent SDK subprocesses.++ The SDK merges these on top of os.environ, so compaction variables the+ user set explicitly win over our defaults. CLAUDECODE is cleared so the+ CLI doesn't think it's nested inside another Claude Code session.+ """+ env = {"CLAUDECODE": ""}+ if "CLAUDE_CODE_AUTO_COMPACT_WINDOW" not in os.environ:+ env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] = AUTOCOMPACT_WINDOW+ if "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE" not in os.environ:+ env["CLAUDE_AUTOCOMPACT_PCT_OVERRIDE"] = AUTOCOMPACT_PCT+ return env+++def is_context_overflow_error(message: str) -> bool:+ """Return True if an SDK error message means the context window overflowed.++ Once this happens the session is unrecoverable: the conversation itself+ exceeds the model's context limit, so every subsequent request (including+ auto-compaction) fails with the same error.+ """+ lowered = message.lower()+ return any(marker in lowered for marker in _CONTEXT_OVERFLOW_MARKERS)+def check_for_updates() -> str | None:"""Check PyPI for newer version.diff --git a/tests/test_engineer.py b/tests/test_engineer.pyindex 1f4cb27..afec783 100644--- a/tests/test_engineer.py+++ b/tests/test_engineer.py@@ -732,3 +732,137 @@ class TestClaudeEngineerAnalyzeAndGenerate:with patch.object(eng, "_prompt_follow_up", new_callable=AsyncMock, return_value=None):result = await eng.analyze_and_generate()assert result is not None+++class TestContextOverflowHandling:+ """Test context-window overflow ("Prompt is too long") handling."""++ 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),+ )++ def _make_client(self, result_message):+ client = MagicMock()++ async def receive():+ yield result_message++ client.receive_response = receive+ return client++ @pytest.mark.asyncio+ async def test_overflow_error_sets_flag_and_prints_help(self, tmp_path):+ """A 'Prompt is too long' result flags the session as unrecoverable."""+ from claude_agent_sdk import ResultMessage++ eng = self._make_engineer(tmp_path)+ msg = MagicMock(spec=ResultMessage)+ msg.is_error = True+ msg.result = "API Error: 400 Prompt is too long"++ with patch.object(eng, "_print_context_overflow_help") as mock_help:+ result = await eng._process_streaming_response(self._make_client(msg))++ assert result is None+ assert eng._context_overflowed is True+ mock_help.assert_called_once()++ @pytest.mark.asyncio+ async def test_other_error_does_not_set_flag(self, tmp_path):+ """A non-overflow error leaves the follow-up loop available."""+ from claude_agent_sdk import ResultMessage++ eng = self._make_engineer(tmp_path)+ msg = MagicMock(spec=ResultMessage)+ msg.is_error = True+ msg.result = "Some other failure"++ with patch.object(eng, "_print_context_overflow_help") as mock_help:+ result = await eng._process_streaming_response(self._make_client(msg))++ assert result is None+ assert eng._context_overflowed is False+ mock_help.assert_not_called()++ @pytest.mark.asyncio+ async def test_follow_up_loop_exits_after_overflow(self, tmp_path):+ """After an overflow mid-follow-up, the loop returns the last result+ instead of prompting for another turn on a dead session."""+ eng = self._make_engineer(tmp_path)++ first_result = {"script_path": "x", "usage": {}}+ responses = iter([first_result, None])++ async def fake_process(client):+ value = next(responses)+ if value is None:+ eng._context_overflowed = True+ return value++ follow_ups = AsyncMock(side_effect=["make it faster", "should not be asked"])++ mock_client = AsyncMock()+ with patch.object(eng, "_process_streaming_response", side_effect=fake_process):+ with patch.object(eng, "_prompt_follow_up", follow_ups):+ 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 == first_result+ # Only one follow-up should have been requested before the loop ended.+ assert follow_ups.await_count == 1+++class TestSdkEnvWiring:+ """Test that SDK sessions get the auto-compact env override."""++ 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_options_env_includes_autocompact_override(self, tmp_path, monkeypatch):+ """ClaudeAgentOptions receives the lowered auto-compact threshold."""+ from reverse_api.utils import AUTOCOMPACT_PCT, AUTOCOMPACT_WINDOW++ monkeypatch.delenv("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE", raising=False)+ monkeypatch.delenv("CLAUDE_CODE_AUTO_COMPACT_WINDOW", raising=False)+ eng = self._make_engineer(tmp_path)++ mock_client = AsyncMock()++ async def empty_receive():+ return+ yield++ mock_client.receive_response = empty_receive++ with patch("reverse_api.engineer.ClaudeAgentOptions") as mock_options:+ 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)++ await eng.analyze_and_generate()++ env = mock_options.call_args.kwargs["env"]+ assert env["CLAUDE_AUTOCOMPACT_PCT_OVERRIDE"] == AUTOCOMPACT_PCT+ assert env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] == AUTOCOMPACT_WINDOW+ assert env["CLAUDECODE"] == ""diff --git a/tests/test_utils.py b/tests/test_utils.pyindex 814fba0..d0a4102 100644--- a/tests/test_utils.py+++ b/tests/test_utils.py@@ -798,3 +798,75 @@ class TestPathValidationExceptions:with patch.object(Path, "resolve", side_effect=RuntimeError("symlink loop")):with pytest.raises(ValueError, match="Invalid path"):get_har_dir("valid_id")+++class TestBuildSdkEnv:+ """Test SDK subprocess environment construction."""++ def test_default_sets_autocompact_override(self, monkeypatch):+ """A proactive compaction window and lowered threshold apply by default."""+ from reverse_api.utils import AUTOCOMPACT_PCT, AUTOCOMPACT_WINDOW, build_sdk_env++ monkeypatch.delenv("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE", raising=False)+ monkeypatch.delenv("CLAUDE_CODE_AUTO_COMPACT_WINDOW", raising=False)+ env = build_sdk_env()+ assert env["CLAUDE_AUTOCOMPACT_PCT_OVERRIDE"] == AUTOCOMPACT_PCT+ assert env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] == AUTOCOMPACT_WINDOW+ assert env["CLAUDECODE"] == ""++ def test_user_window_wins(self, monkeypatch):+ """An explicit user CLAUDE_CODE_AUTO_COMPACT_WINDOW is not clobbered."""+ from reverse_api.utils import build_sdk_env++ monkeypatch.setenv("CLAUDE_CODE_AUTO_COMPACT_WINDOW", "500000")+ env = build_sdk_env()+ assert "CLAUDE_CODE_AUTO_COMPACT_WINDOW" not in env++ def test_user_override_wins(self, monkeypatch):+ """An explicit user CLAUDE_AUTOCOMPACT_PCT_OVERRIDE is not clobbered."""+ from reverse_api.utils import build_sdk_env++ monkeypatch.setenv("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE", "60")+ env = build_sdk_env()+ # The SDK merges our dict on top of os.environ, so omitting the key+ # leaves the user's value in effect.+ assert "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE" not in env+ assert env["CLAUDECODE"] == ""++ def test_empty_user_override_wins(self, monkeypatch):+ """An explicit empty override (opting back into the CLI default)+ is also respected, not replaced with our threshold."""+ from reverse_api.utils import build_sdk_env++ monkeypatch.setenv("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE", "")+ env = build_sdk_env()+ assert "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE" not in env+++class TestIsContextOverflowError:+ """Test context-window overflow error detection."""++ def test_prompt_too_long(self):+ from reverse_api.utils import is_context_overflow_error++ assert is_context_overflow_error("API Error: 400 Prompt is too long")++ def test_conversation_too_long(self):+ from reverse_api.utils import is_context_overflow_error++ assert is_context_overflow_error(+ "Error during compaction: Error: Conversation too long."+ )++ def test_input_and_max_tokens_variant(self):+ from reverse_api.utils import is_context_overflow_error++ assert is_context_overflow_error(+ "input length and max_tokens exceed context limit"+ )++ def test_unrelated_error(self):+ from reverse_api.utils import is_context_overflow_error++ assert not is_context_overflow_error("Unknown error")+ assert not is_context_overflow_error("rate limit exceeded")