Skip to content

The agent that turns websites into APIs!

fix(cli): preserve results for direct SDK interrupts

kalil0321d25a89ad725c
diff --git a/src/reverse_api/auto_engineer.py b/src/reverse_api/auto_engineer.py
index 0149704..8eac7e6 100644
--- a/src/reverse_api/auto_engineer.py
+++ b/src/reverse_api/auto_engineer.py
@@ -283,8 +283,9 @@ class ClaudeAutoEngineer(ClaudeEngineer):
# the partial result for the CLI, including SIGINT via Runner.
exc.partial_result = last_result # type: ignore[attr-defined]
raise
- except KeyboardInterrupt:
+ except KeyboardInterrupt as exc:
if not self.interactive:
+ exc.partial_result = last_result # type: ignore[attr-defined]
raise
self.ui.console.print("\n [dim]run aborted[/dim]")
return {**(last_result or {}), "error": "interrupted"}
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 113137e..4c8d3ad 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -2192,7 +2192,11 @@ def run_auto_capture(
result = asyncio.run(engineer.analyze_and_generate())
interrupted = isinstance(result, dict) and result.get("error") == "interrupted"
except KeyboardInterrupt as exc:
- result = getattr(exc.__context__, "partial_result", None)
+ result = getattr(exc, "partial_result", None)
+ # Python 3.11+ Runner translates SIGINT cancellation to
+ # KeyboardInterrupt, with the tagged CancelledError as context.
+ if result is None and isinstance(exc.__context__, asyncio.CancelledError):
+ result = getattr(exc.__context__, "partial_result", None)
interrupted = True
console.print("\n [dim]run aborted[/dim]")
except asyncio.CancelledError as exc:
diff --git a/src/reverse_api/cursor_engineer.py b/src/reverse_api/cursor_engineer.py
index ad8811b..7818e2d 100644
--- a/src/reverse_api/cursor_engineer.py
+++ b/src/reverse_api/cursor_engineer.py
@@ -611,8 +611,9 @@ class CursorAutoEngineer(CursorEngineer):
# the partial result for the CLI, including SIGINT via Runner.
exc.partial_result = last_result # type: ignore[attr-defined]
raise
- except KeyboardInterrupt:
+ except KeyboardInterrupt as exc:
if not self.interactive:
+ exc.partial_result = last_result # type: ignore[attr-defined]
raise
self.ui.console.print("\n [dim]run aborted[/dim]")
return {**(last_result or {}), "error": "interrupted"}
diff --git a/tests/test_cli_agent_json.py b/tests/test_cli_agent_json.py
index 4feea07..885a462 100644
--- a/tests/test_cli_agent_json.py
+++ b/tests/test_cli_agent_json.py
@@ -650,3 +650,33 @@ def test_empty_provider_exception_is_failure(tmp_path, error_type, output_flag):
assert payload["error"] == error_type.__name__
else:
assert f"error: {error_type.__name__}" in result.stderr
+
+
[email protected]("output_flag", ["--json", "--json-stream", "--no-interactive"])
+def test_direct_interrupt_after_claude_result_keeps_partial_run(tmp_path, output_flag):
+ from unittest.mock import AsyncMock
+
+ from reverse_api.config import ConfigManager
+
+ config = ConfigManager(tmp_path / "config.json")
+ config.config.update({"sdk": "claude", "agent_provider": "chrome-mcp", "real_time_sync": False})
+ with (
+ patch("reverse_api.cli.config_manager", config),
+ patch("reverse_api.cli.session_manager", SessionManager(tmp_path / "history.json")),
+ patch("reverse_api.auto_engineer.ClaudeSDKClient") as client,
+ patch("reverse_api.auto_engineer.ClaudeAutoEngineer._process_streaming_response",
+ new=AsyncMock(return_value={"script_path": "completed.py", "usage": {"input_tokens": 7}})),
+ ):
+ client.return_value.__aenter__ = AsyncMock(return_value=client.return_value)
+ client.return_value.query = AsyncMock()
+ client.return_value.__aexit__ = AsyncMock(side_effect=KeyboardInterrupt)
+ result = CliRunner().invoke(main, ["agent", "-p", "direct interruption", "--headless", "-o", str(tmp_path), output_flag])
+ assert result.exit_code == 1, result.output
+ assert result.output.count("run aborted") == 1
+ history = json.loads((tmp_path / "history.json").read_text())
+ assert history[0]["paths"]["script_path"] == "completed.py"
+ assert history[0]["usage"]["input_tokens"] == 7
+ if output_flag in ("--json", "--json-stream"):
+ payload = json.loads(result.stdout.strip().splitlines()[-1])
+ assert payload["error"] == "interrupted"
+ assert payload["script_path"] == "completed.py"