Skip to content

The agent that turns websites into APIs!

fix(cli): address review feedback on agent --dry-run (PR #67)

Kalil Bouziguese8109fd7ed6c
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 65bc3d9..1bb86e3 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -250,7 +250,9 @@ def _build_dry_run_payload(
else:
checks.append({"name": f"sdk:{sdk}", "status": "ok", "message": f"{sdk_env_var} present"})
- # 5. Node.js for MCP servers (both auto and chrome-mcp use npx)
+ # 5. Node.js + npx for MCP servers (both auto and chrome-mcp shell out to
+ # `npx <package>`; minimal Docker images sometimes ship `node` without
+ # `npx`, so checking only `node` would lull dry-run into a false ok).
node = shutil.which("node")
if node is None:
checks.append({
@@ -267,6 +269,16 @@ def _build_dry_run_payload(
except Exception as e:
checks.append({"name": "node", "status": "warn", "message": f"could not query version: {e}"})
+ npx = shutil.which("npx")
+ if npx is None:
+ checks.append({
+ "name": "npx",
+ "status": "error",
+ "message": "npx not found in PATH; both MCP servers are launched via `npx <package>`",
+ })
+ else:
+ checks.append({"name": "npx", "status": "ok", "message": npx})
+
# 6. Headed chrome-mcp requires the user has a real Chrome with auto-connect
if agent_provider == "chrome-mcp" and not headless:
checks.append({
@@ -275,11 +287,18 @@ def _build_dry_run_payload(
"message": "chrome-mcp without --headless requires Chrome 146+ with auto-connect enabled at chrome://inspect/#remote-debugging — this is not auto-checkable",
})
- # 7. Output dir writability
+ # 7. Output dir writability — probe with a unique filename so we never
+ # clobber a real user file (a fixed name like `.dry_run_write_probe`
+ # could legitimately exist in someone's output dir).
+ import secrets
+
base = Path(output_dir or config_manager.get("output_dir") or "~/.reverse-api/runs").expanduser()
try:
base.mkdir(parents=True, exist_ok=True)
- probe = base / ".dry_run_write_probe"
+ probe = base / f".rae_dry_run_probe_{os.getpid()}_{secrets.token_hex(4)}"
+ # If somehow this exact filename already exists, refuse to touch it.
+ if probe.exists():
+ raise FileExistsError(f"unique probe path collision: {probe}")
probe.write_text("")
probe.unlink()
checks.append({"name": "output_dir", "status": "ok", "message": str(base)})
@@ -300,6 +319,21 @@ def _build_dry_run_payload(
# Misuse for prompt/url; config_invalid otherwise
error_kind = "misuse" if first_err["name"] in ("prompt", "url") else "config_invalid"
+ # `would_run.model` resolution mirrors the live capture path: each SDK
+ # has its own model config key, so picking `claude_code_model` for an
+ # opencode/copilot session would misreport what would actually run.
+ sdk_model_key = {
+ "claude": "claude_code_model",
+ "opencode": "opencode_model",
+ "copilot": "copilot_model",
+ }.get(sdk, "claude_code_model")
+ sdk_default_model = {
+ "claude": "claude-sonnet-4-6",
+ "opencode": "claude-opus-4-6",
+ "copilot": "gpt-5",
+ }.get(sdk, "claude-sonnet-4-6")
+ resolved_model = model or config_manager.get(sdk_model_key, sdk_default_model)
+
return {
"schema_version": AGENT_JSON_SCHEMA_VERSION,
"status": "error" if has_error else "ok",
@@ -315,7 +349,7 @@ def _build_dry_run_payload(
"would_run": {
"agent_provider": agent_provider,
"sdk": sdk,
- "model": model or config_manager.get("claude_code_model", "claude-sonnet-4-6"),
+ "model": resolved_model,
"output_dir": str(base),
"headless": headless,
},
diff --git a/tests/test_cli_followups.py b/tests/test_cli_followups.py
index 46ed07c..a3933e4 100644
--- a/tests/test_cli_followups.py
+++ b/tests/test_cli_followups.py
@@ -427,6 +427,110 @@ class TestAgentDryRun:
# Click reflows whitespace, so "Implies\n--json" or "Implies --json"
assert "Implies" in result.output and "--json" in result.output
+ def test_dry_run_checks_npx_separately_from_node(self, tmp_path):
+ """cubic-dev-ai PR #67 review (P2): MCP servers shell out to `npx`,
+ so dry-run must check npx availability — not just node — otherwise
+ a minimal Docker image with node-but-no-npx passes dry-run and then
+ fails the real run."""
+ from reverse_api.cli import agent as agent_cmd
+
+ runner = CliRunner()
+
+ # Pretend npx is missing while node is present
+ def fake_which(name):
+ if name == "node":
+ return "/usr/bin/node"
+ if name == "npx":
+ return None
+ return None
+
+ with patch("reverse_api.cli.config_manager") as cm, \
+ patch("shutil.which", side_effect=fake_which):
+ cm.get.side_effect = lambda key, default=None: {
+ "agent_provider": "auto",
+ "sdk": "claude",
+ "output_dir": str(tmp_path),
+ }.get(key, default)
+ result = runner.invoke(agent_cmd, ["--dry-run", "-p", "x"])
+
+ assert result.exit_code == 1
+ payload = json.loads(result.stdout.strip())
+ assert payload["error_kind"] == "config_invalid"
+ npx_check = next(c for c in payload["checks"] if c["name"] == "npx")
+ assert npx_check["status"] == "error"
+ assert "npx not found" in npx_check["message"]
+
+ def test_dry_run_probe_does_not_clobber_existing_files(self, tmp_path):
+ """cubic-dev-ai PR #67 review (P2): a fixed probe filename like
+ `.dry_run_write_probe` could legitimately exist in a user's output
+ dir and would be deleted by the probe. We use a unique filename
+ with PID + random hex so collisions are astronomically unlikely,
+ and refuse to touch any path that already exists."""
+ from reverse_api.cli import agent as agent_cmd
+
+ # Pre-populate the output dir with a file that would collide with
+ # the OLD fixed probe name. The dry-run must not delete it.
+ canary = tmp_path / ".dry_run_write_probe"
+ canary.write_text("user data — do not delete")
+
+ runner = CliRunner()
+ with patch("reverse_api.cli.config_manager") as cm:
+ cm.get.side_effect = lambda key, default=None: {
+ "agent_provider": "auto",
+ "sdk": "claude",
+ "output_dir": str(tmp_path),
+ }.get(key, default)
+ result = runner.invoke(agent_cmd, ["--dry-run", "-p", "x"])
+
+ assert result.exit_code == 0, result.output
+ # The user's pre-existing file is untouched
+ assert canary.exists()
+ assert canary.read_text() == "user data — do not delete"
+
+ def test_dry_run_resolves_correct_model_per_sdk(self, tmp_path):
+ """cubic-dev-ai PR #67 review (P2): when sdk=opencode the live agent
+ uses `opencode_model`, not `claude_code_model`. would_run.model must
+ reflect what would actually run, otherwise the manifest lies."""
+ from reverse_api.cli import agent as agent_cmd
+
+ runner = CliRunner()
+
+ # Configure opencode SDK with a custom opencode_model
+ with patch("reverse_api.cli.config_manager") as cm:
+ cm.get.side_effect = lambda key, default=None: {
+ "agent_provider": "auto",
+ "sdk": "opencode",
+ "opencode_model": "claude-opus-4-6-custom",
+ "claude_code_model": "claude-sonnet-4-6-irrelevant",
+ "output_dir": str(tmp_path),
+ }.get(key, default)
+ result = runner.invoke(agent_cmd, ["--dry-run", "-p", "x"])
+
+ assert result.exit_code == 0, result.output
+ payload = json.loads(result.stdout.strip())
+ assert payload["would_run"]["sdk"] == "opencode"
+ assert payload["would_run"]["model"] == "claude-opus-4-6-custom"
+ # And NOT the claude_code_model that the old code would have grabbed
+ assert "irrelevant" not in payload["would_run"]["model"]
+
+ def test_dry_run_copilot_model_resolution(self, tmp_path):
+ from reverse_api.cli import agent as agent_cmd
+
+ runner = CliRunner()
+ with patch("reverse_api.cli.config_manager") as cm:
+ cm.get.side_effect = lambda key, default=None: {
+ "agent_provider": "auto",
+ "sdk": "copilot",
+ "copilot_model": "gpt-5-custom",
+ "output_dir": str(tmp_path),
+ }.get(key, default)
+ result = runner.invoke(agent_cmd, ["--dry-run", "-p", "x"])
+
+ assert result.exit_code == 0, result.output
+ payload = json.loads(result.stdout.strip())
+ assert payload["would_run"]["sdk"] == "copilot"
+ assert payload["would_run"]["model"] == "gpt-5-custom"
+
class TestRootHelpMentionsScripted:
"""Item #6 partial: root --help should advertise scripted features."""