| 1 | """LAW 7 enforcement: an agent-hosted research run without --plan stops. |
| 2 | |
| 3 | SKILL.md LAW 7 says the hosting reasoning model writes the query plan and |
| 4 | passes --plan. When the engine detects an agent host (Claude Code, Codex, or |
| 5 | an explicit LAST30DAYS_HOST_AGENT=1) and no plan was passed, it must stop |
| 6 | before retrieval with a clear message instead of silently spending on its |
| 7 | internal planner, unless the mode is exempt or LAST30DAYS_ALLOW_ENGINE_PLAN=1. |
| 8 | """ |
| 9 | |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import io |
| 13 | import json |
| 14 | import sys |
| 15 | from contextlib import redirect_stderr, redirect_stdout |
| 16 | from unittest import mock |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | import last30days as cli |
| 21 | from lib import env, http, planner |
| 22 | |
| 23 | DUMMY_CONFIG = {"OPENROUTER_API_KEY": "dummy-openrouter-key-000"} |
| 24 | DIAG = { |
| 25 | "available_sources": ["reddit", "hackernews"], |
| 26 | "providers": {"google": False, "openai": False, "xai": False}, |
| 27 | "x_backend": None, |
| 28 | "bird_installed": False, |
| 29 | "bird_authenticated": False, |
| 30 | "bird_username": None, |
| 31 | "native_web_backend": None, |
| 32 | "permission_preflight": {}, |
| 33 | } |
| 34 | TOPIC = ["best", "espresso", "grinders"] |
| 35 | VALID_PLAN = { |
| 36 | "intent": "product", |
| 37 | "freshness_mode": "balanced_recent", |
| 38 | "cluster_mode": "market", |
| 39 | "subqueries": [ |
| 40 | { |
| 41 | "label": "primary", |
| 42 | "search_query": "espresso grinder", |
| 43 | "ranking_query": "Which espresso grinders do people recommend?", |
| 44 | "sources": ["reddit", "hackernews"], |
| 45 | "weight": 1.0, |
| 46 | } |
| 47 | ], |
| 48 | } |
| 49 | |
| 50 | |
| 51 | class _RetrievalReached(Exception): |
| 52 | """pipeline.run was entered: the gate let the run through.""" |
| 53 | |
| 54 | |
| 55 | def _parse(*argv: str): |
| 56 | args, _extra = cli.build_parser().parse_known_args(list(argv)) |
| 57 | return args |
| 58 | |
| 59 | |
| 60 | def _run_main(monkeypatch, argv, *, host_env=None, config=None): |
| 61 | """Drive cli.main() up to retrieval with every side effect stubbed. |
| 62 | |
| 63 | Returns (rc, stderr, mocks). rc is None when pipeline.run was reached. |
| 64 | """ |
| 65 | for name in ("LAST30DAYS_API_KEY", "LAST30DAYS_API_BASE", "LAST30DAYS_MEMORY_DIR"): |
| 66 | monkeypatch.delenv(name, raising=False) |
| 67 | monkeypatch.setenv("LAST30DAYS_SKIP_PREFLIGHT", "1") |
| 68 | for name, value in (host_env or {}).items(): |
| 69 | monkeypatch.setenv(name, value) |
| 70 | monkeypatch.setattr(sys, "argv", ["last30days.py", *argv]) |
| 71 | |
| 72 | run_mock = mock.Mock(side_effect=_RetrievalReached) |
| 73 | diagnose_mock = mock.Mock(return_value=dict(DIAG)) |
| 74 | runtime_mock = mock.Mock(side_effect=AssertionError("provider resolved")) |
| 75 | resolve_mock = mock.Mock(return_value={}) |
| 76 | err = io.StringIO() |
| 77 | with mock.patch.object(cli.env, "get_config", return_value=dict(config or DUMMY_CONFIG)), \ |
| 78 | mock.patch.object(cli.pipeline, "diagnose", diagnose_mock), \ |
| 79 | mock.patch.object(cli.pipeline, "run", run_mock), \ |
| 80 | mock.patch("lib.providers.resolve_runtime", runtime_mock), \ |
| 81 | mock.patch("lib.resolve.auto_resolve", resolve_mock), \ |
| 82 | mock.patch.object(cli.ui, "ProgressDisplay", return_value=mock.Mock()), \ |
| 83 | redirect_stdout(io.StringIO()), redirect_stderr(err): |
| 84 | try: |
| 85 | rc = cli.main() |
| 86 | except _RetrievalReached: |
| 87 | rc = None |
| 88 | except SystemExit as exc: |
| 89 | rc = exc.code |
| 90 | return rc, err.getvalue(), { |
| 91 | "diagnose": diagnose_mock, "run": run_mock, |
| 92 | "runtime": runtime_mock, "resolve": resolve_mock, |
| 93 | } |
| 94 | |
| 95 | |
| 96 | # --------------------------------------------------------------------------- |
| 97 | # Host detection helpers (lib/env.py) |
| 98 | # --------------------------------------------------------------------------- |
| 99 | |
| 100 | |
| 101 | @pytest.mark.parametrize("name", env.AGENT_HOST_ENV_VARS) |
| 102 | def test_each_runtime_marker_signals_an_agent_host(name): |
| 103 | assert env.agent_host_signal({name: "1"}) == name |
| 104 | assert env.agent_hosted_run({name: "anything"}) |
| 105 | |
| 106 | |
| 107 | def test_explicit_host_agent_opt_in_signals_an_agent_host(): |
| 108 | assert env.agent_host_signal({"LAST30DAYS_HOST_AGENT": "1"}) == "LAST30DAYS_HOST_AGENT" |
| 109 | assert not env.agent_hosted_run({"LAST30DAYS_HOST_AGENT": "0"}) |
| 110 | |
| 111 | |
| 112 | def test_host_self_identification_signals_an_agent_host(): |
| 113 | assert env.agent_host_signal({"LAST30DAYS_HOST": "grok-bot"}) == "LAST30DAYS_HOST" |
| 114 | |
| 115 | |
| 116 | def test_no_markers_or_blank_markers_are_not_agent_hosted(): |
| 117 | assert env.agent_host_signal({}) == "" |
| 118 | assert not env.agent_hosted_run({"CLAUDECODE": "", "CODEX_THREAD_ID": " "}) |
| 119 | |
| 120 | |
| 121 | def test_signal_names_the_variable_never_its_value(): |
| 122 | assert env.agent_host_signal({"CODEX_THREAD_ID": "thread-secret-000"}) == "CODEX_THREAD_ID" |
| 123 | |
| 124 | |
| 125 | @pytest.mark.parametrize("value,expected", [("1", True), ("true", True), ("on", True), ("0", False), ("", False)]) |
| 126 | def test_engine_plan_override_is_truthy_only(value, expected): |
| 127 | assert env.engine_plan_allowed({"LAST30DAYS_ALLOW_ENGINE_PLAN": value}) is expected |
| 128 | |
| 129 | |
| 130 | def test_helpers_read_the_process_environment(monkeypatch): |
| 131 | monkeypatch.setenv("CODEX_SANDBOX", "seatbelt") |
| 132 | monkeypatch.setenv("LAST30DAYS_ALLOW_ENGINE_PLAN", "1") |
| 133 | assert env.agent_host_signal() == "CODEX_SANDBOX" |
| 134 | assert env.engine_plan_allowed() |
| 135 | |
| 136 | |
| 137 | # --------------------------------------------------------------------------- |
| 138 | # The gate on the research path |
| 139 | # --------------------------------------------------------------------------- |
| 140 | |
| 141 | |
| 142 | @pytest.mark.parametrize("name", [*env.AGENT_HOST_ENV_VARS, "LAST30DAYS_HOST_AGENT"]) |
| 143 | def test_agent_hosted_run_without_plan_stops_before_retrieval(monkeypatch, name): |
| 144 | rc, stderr, mocks = _run_main(monkeypatch, TOPIC, host_env={name: "1"}) |
| 145 | |
| 146 | assert rc == cli.LAW7_HOST_PLAN_EXIT != 0 |
| 147 | mocks["run"].assert_not_called() |
| 148 | mocks["runtime"].assert_not_called() # internal LLM planner never resolved |
| 149 | mocks["resolve"].assert_not_called() |
| 150 | assert "LAW 7" in stderr |
| 151 | assert f"({name} is set)" in stderr |
| 152 | assert "Step 0.75" in stderr |
| 153 | assert "references/research-runbook.md" in stderr |
| 154 | assert "--plan -" in stderr |
| 155 | assert "<<'PLAN_EOF'" in stderr |
| 156 | assert "LAST30DAYS_ALLOW_ENGINE_PLAN=1" in stderr |
| 157 | assert "dummy-openrouter-key-000" not in stderr |
| 158 | |
| 159 | |
| 160 | def test_entity_topic_stops_before_engine_auto_resolve(monkeypatch): |
| 161 | # Entity-shaped topics switch on auto-resolve (a paid web search) when no |
| 162 | # handle is given; the gate must come first. |
| 163 | rc, _stderr, mocks = _run_main( |
| 164 | monkeypatch, ["Peter", "Steinberger"], host_env={"CLAUDECODE": "1"} |
| 165 | ) |
| 166 | assert rc == cli.LAW7_HOST_PLAN_EXIT |
| 167 | mocks["resolve"].assert_not_called() |
| 168 | mocks["run"].assert_not_called() |
| 169 | |
| 170 | |
| 171 | def test_agent_hosted_run_without_plan_stops_before_live_diagnostics(monkeypatch): |
| 172 | rc, _stderr, mocks = _run_main( |
| 173 | monkeypatch, TOPIC, host_env={"CLAUDECODE": "1"} |
| 174 | ) |
| 175 | assert rc == cli.LAW7_HOST_PLAN_EXIT |
| 176 | mocks["diagnose"].assert_not_called() |
| 177 | |
| 178 | |
| 179 | def test_agent_hosted_run_with_plan_reaches_retrieval(monkeypatch, tmp_path): |
| 180 | plan_file = tmp_path / "plan.json" |
| 181 | plan_file.write_text(json.dumps(VALID_PLAN), encoding="utf-8") |
| 182 | rc, stderr, mocks = _run_main( |
| 183 | monkeypatch, [*TOPIC, "--plan", str(plan_file)], host_env={"CLAUDECODE": "1"} |
| 184 | ) |
| 185 | assert rc is None |
| 186 | assert mocks["run"].call_args.kwargs["external_plan"] == VALID_PLAN |
| 187 | assert "LAW 7" not in stderr |
| 188 | |
| 189 | |
| 190 | def test_agent_hosted_plan_from_stdin_preserves_literal_json(monkeypatch): |
| 191 | plan = dict(VALID_PLAN, context="owner's $HOME `echo literal` \\ path café") |
| 192 | monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(plan, ensure_ascii=False))) |
| 193 | rc, stderr, mocks = _run_main( |
| 194 | monkeypatch, [*TOPIC, "--plan", "-"], host_env={"CODEX_THREAD_ID": "test"} |
| 195 | ) |
| 196 | assert rc is None, stderr |
| 197 | assert mocks["run"].call_args.kwargs["external_plan"] == plan |
| 198 | mocks["runtime"].assert_not_called() |
| 199 | |
| 200 | |
| 201 | @pytest.mark.parametrize("encoding", ["cp1252", "utf-8"]) |
| 202 | def test_stdin_plan_decodes_utf8_bytes_once(monkeypatch, encoding): |
| 203 | plan = json.loads(json.dumps(VALID_PLAN)) |
| 204 | plan["subqueries"][0]["search_query"] = "café grinders" |
| 205 | stdin = io.TextIOWrapper( |
| 206 | io.BytesIO(json.dumps(plan, ensure_ascii=False).encode("utf-8")), |
| 207 | encoding=encoding, |
| 208 | ) |
| 209 | monkeypatch.setattr(sys, "stdin", stdin) |
| 210 | with mock.patch.object(stdin.buffer, "read", wraps=stdin.buffer.read) as read: |
| 211 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--plan", "-"]) |
| 212 | assert rc is None, stderr |
| 213 | assert mocks["run"].call_args.kwargs["external_plan"] == plan |
| 214 | read.assert_called_once_with() |
| 215 | |
| 216 | |
| 217 | def test_invalid_utf8_stdin_plan_stops_before_retrieval(monkeypatch): |
| 218 | stdin = io.TextIOWrapper(io.BytesIO(b'{"context": "\xff"}'), encoding="cp1252") |
| 219 | monkeypatch.setattr(sys, "stdin", stdin) |
| 220 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--plan", "-"]) |
| 221 | assert rc == 2 |
| 222 | assert "Cannot read --plan stdin" in stderr |
| 223 | mocks["run"].assert_not_called() |
| 224 | mocks["runtime"].assert_not_called() |
| 225 | |
| 226 | |
| 227 | @pytest.mark.parametrize( |
| 228 | "raw,message", |
| 229 | [("", "Invalid --plan JSON"), ("{broken", "Invalid --plan JSON"), |
| 230 | ('{"queries": {}}', "Invalid --plan schema")], |
| 231 | ) |
| 232 | def test_invalid_stdin_plan_stops_without_fallback(monkeypatch, raw, message): |
| 233 | monkeypatch.setattr(sys, "stdin", io.StringIO(raw)) |
| 234 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--plan", "-"]) |
| 235 | assert rc == 2 |
| 236 | assert message in stderr |
| 237 | mocks["run"].assert_not_called() |
| 238 | mocks["runtime"].assert_not_called() |
| 239 | |
| 240 | |
| 241 | def test_unreadable_stdin_plan_exits_cleanly(monkeypatch): |
| 242 | monkeypatch.setattr(sys, "stdin", mock.Mock(spec=["read"], read=mock.Mock(side_effect=OSError("unreadable")))) |
| 243 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--plan", "-"]) |
| 244 | assert rc == 2 |
| 245 | assert "Cannot read --plan stdin" in stderr |
| 246 | mocks["run"].assert_not_called() |
| 247 | |
| 248 | |
| 249 | @pytest.mark.parametrize("use_file", [False, True], ids=["inline", "file"]) |
| 250 | def test_existing_plan_inputs_do_not_read_stdin(monkeypatch, tmp_path, use_file): |
| 251 | raw = json.dumps(VALID_PLAN) |
| 252 | if use_file: |
| 253 | plan_file = tmp_path / "plan.json" |
| 254 | plan_file.write_text(raw, encoding="utf-8") |
| 255 | raw = str(plan_file) |
| 256 | stdin = mock.Mock(read=mock.Mock(side_effect=AssertionError("unexpected stdin read"))) |
| 257 | monkeypatch.setattr(sys, "stdin", stdin) |
| 258 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--plan", raw]) |
| 259 | assert rc is None, stderr |
| 260 | assert mocks["run"].call_args.kwargs["external_plan"] == VALID_PLAN |
| 261 | stdin.read.assert_not_called() |
| 262 | |
| 263 | |
| 264 | def test_override_restores_engine_planning_under_an_agent(monkeypatch): |
| 265 | rc, stderr, mocks = _run_main( |
| 266 | monkeypatch, |
| 267 | TOPIC, |
| 268 | host_env={"CLAUDECODE": "1", "LAST30DAYS_ALLOW_ENGINE_PLAN": "1"}, |
| 269 | ) |
| 270 | assert rc is None |
| 271 | assert mocks["run"].call_args.kwargs["external_plan"] is None |
| 272 | assert "LAW 7" not in stderr |
| 273 | |
| 274 | |
| 275 | def test_non_agent_run_is_unchanged(monkeypatch): |
| 276 | rc, stderr, mocks = _run_main(monkeypatch, TOPIC) |
| 277 | assert rc is None |
| 278 | assert mocks["run"].call_args.kwargs["external_plan"] is None |
| 279 | assert "LAW 7" not in stderr |
| 280 | |
| 281 | |
| 282 | @pytest.mark.parametrize( |
| 283 | "extra", |
| 284 | [ |
| 285 | ["--mock"], |
| 286 | ["--hiring-signals"], |
| 287 | ["--auto-resolve"], |
| 288 | ], |
| 289 | ids=["mock", "hiring-signals", "auto-resolve"], |
| 290 | ) |
| 291 | def test_exempt_research_modes_reach_retrieval_under_an_agent(monkeypatch, extra): |
| 292 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, *extra], host_env={"CLAUDECODE": "1"}) |
| 293 | assert rc is None |
| 294 | mocks["run"].assert_called_once() |
| 295 | assert "LAW 7" not in stderr |
| 296 | |
| 297 | |
| 298 | @pytest.mark.parametrize( |
| 299 | "argv", |
| 300 | [ |
| 301 | ["OpenAI", "vs", "Anthropic"], |
| 302 | ["OpenAI", "--competitors"], |
| 303 | ["OpenAI", "--competitors-list", "Anthropic,Google"], |
| 304 | ["OpenAI", "--competitors-plan", '{"Anthropic": {"x_handle": "AnthropicAI"}}'], |
| 305 | ], |
| 306 | ids=["vs-topic", "competitors", "competitors-list", "competitors-plan"], |
| 307 | ) |
| 308 | def test_comparison_runs_are_exempt(monkeypatch, argv): |
| 309 | # Peers never take a query plan (SKILL.md vs-mode invokes without --plan). |
| 310 | monkeypatch.setenv("CLAUDECODE", "1") |
| 311 | args = _parse(*argv) |
| 312 | topic = " ".join(args.topic).strip() |
| 313 | assert cli._law7_host_plan_gate(args, topic) is None |
| 314 | |
| 315 | |
| 316 | def test_gate_function_blocks_a_plain_topic(monkeypatch): |
| 317 | monkeypatch.setenv("CODEX_THREAD_ID", "t") |
| 318 | args = _parse(*TOPIC) |
| 319 | err = io.StringIO() |
| 320 | with redirect_stderr(err): |
| 321 | assert cli._law7_host_plan_gate(args, " ".join(TOPIC)) == cli.LAW7_HOST_PLAN_EXIT |
| 322 | assert "CODEX_THREAD_ID" in err.getvalue() |
| 323 | |
| 324 | |
| 325 | # --------------------------------------------------------------------------- |
| 326 | # Modes that dispatch before the research path never see the gate |
| 327 | # --------------------------------------------------------------------------- |
| 328 | |
| 329 | |
| 330 | @pytest.mark.parametrize( |
| 331 | "argv,target", |
| 332 | [ |
| 333 | (["doctor"], "lib.doctor.run"), |
| 334 | (["setup", "--openclaw"], "lib.setup_wizard.run_openclaw_setup"), |
| 335 | (["library", "search", "espresso"], "last30days._run_library_search"), |
| 336 | (["library", "feed"], "last30days._run_library_feed"), |
| 337 | (["queue", "list"], "last30days._run_queue_list"), |
| 338 | (["--discover"], "last30days._run_discover"), |
| 339 | (["--discover", "ai", "--discover-shallow"], "last30days._run_discover"), |
| 340 | (["--discover", "--nominate-only"], "last30days._run_discover_protocol_leg"), |
| 341 | (["--discover", "--finalize"], "last30days._run_discover_protocol_leg"), |
| 342 | (["--drill", "3"], "last30days._run_drill"), |
| 343 | (["--verify-freshness"], "last30days._run_cached_freshness"), |
| 344 | ], |
| 345 | ids=[ |
| 346 | "doctor", "setup", "library-search", "library-feed", "queue-list", |
| 347 | "discover", "discover-shallow", "nominate-only", "finalize", "drill", |
| 348 | "verify-freshness-cached", |
| 349 | ], |
| 350 | ) |
| 351 | def test_non_research_commands_are_not_gated(monkeypatch, argv, target): |
| 352 | with mock.patch(target, return_value={} if "setup_wizard" in target else 0) as dispatched: |
| 353 | rc, stderr, mocks = _run_main(monkeypatch, argv, host_env={"CLAUDECODE": "1"}) |
| 354 | assert rc == 0 |
| 355 | dispatched.assert_called_once() |
| 356 | mocks["run"].assert_not_called() |
| 357 | assert "LAW 7" not in stderr |
| 358 | |
| 359 | |
| 360 | def test_diagnose_is_not_gated(monkeypatch): |
| 361 | rc, stderr, mocks = _run_main(monkeypatch, [*TOPIC, "--diagnose"], host_env={"CLAUDECODE": "1"}) |
| 362 | assert rc == 0 |
| 363 | mocks["diagnose"].assert_called_once() |
| 364 | assert mocks["diagnose"].call_args.kwargs["safe"] is True |
| 365 | mocks["run"].assert_not_called() |
| 366 | assert "LAW 7" not in stderr |
| 367 | |
| 368 | |
| 369 | def test_preflight_is_not_gated(monkeypatch): |
| 370 | with mock.patch.object(cli.permission_preflight, "render_text", return_value="ok\n"): |
| 371 | rc, stderr, _mocks = _run_main(monkeypatch, ["--preflight"], host_env={"CLAUDECODE": "1"}) |
| 372 | assert rc == 0 |
| 373 | assert "LAW 7" not in stderr |
| 374 | |
| 375 | |
| 376 | def test_hosted_api_path_is_not_gated(monkeypatch): |
| 377 | # The remote API plans server-side and takes no --plan. |
| 378 | monkeypatch.setenv("LAST30DAYS_API_BASE", "https://hosted.example.test") |
| 379 | monkeypatch.setattr(sys, "argv", ["last30days.py", *TOPIC]) |
| 380 | monkeypatch.setenv("CLAUDECODE", "1") |
| 381 | err = io.StringIO() |
| 382 | with mock.patch.object(cli.env, "get_config", return_value={}), \ |
| 383 | mock.patch.object(cli.env, "read_secret_env", return_value="dummy-hosted-key-000"), \ |
| 384 | mock.patch("lib.hosted.run_hosted", return_value=0) as hosted, \ |
| 385 | redirect_stdout(io.StringIO()), redirect_stderr(err): |
| 386 | rc = cli.main() |
| 387 | assert rc == 0 |
| 388 | hosted.assert_called_once() |
| 389 | assert "LAW 7" not in err.getvalue() |
| 390 | |
| 391 | |
| 392 | # --------------------------------------------------------------------------- |
| 393 | # Unattended callers opt back into engine planning |
| 394 | # --------------------------------------------------------------------------- |
| 395 | |
| 396 | |
| 397 | def test_watchlist_subprocess_allows_engine_planning(monkeypatch): |
| 398 | import watchlist |
| 399 | |
| 400 | monkeypatch.setenv("CLAUDECODE", "1") |
| 401 | fake = mock.Mock(returncode=1, stdout="", stderr="boom") |
| 402 | with mock.patch.object(watchlist, "store") as store, \ |
| 403 | mock.patch.object(watchlist.subprocess, "run", return_value=fake) as run: |
| 404 | store.record_run.return_value = 1 |
| 405 | watchlist._run_topic({"id": 1, "name": "test topic", "search_queries": None}) |
| 406 | child_env = run.call_args.kwargs["env"] |
| 407 | assert child_env["LAST30DAYS_ALLOW_ENGINE_PLAN"] == "1" |
| 408 | assert child_env["CLAUDECODE"] == "1" # rest of the environment passes through |
| 409 | |
| 410 | |
| 411 | # --------------------------------------------------------------------------- |
| 412 | # Non-agent path: a failed internal planner still prints the LAW 7 reminder |
| 413 | # --------------------------------------------------------------------------- |
| 414 | |
| 415 | |
| 416 | def _plan_with_failing_provider(*, internal_subrun: bool) -> tuple: |
| 417 | provider = mock.Mock() |
| 418 | provider.generate_json.side_effect = http.HTTPError( |
| 419 | "HTTP 402: insufficient credits", status_code=402 |
| 420 | ) |
| 421 | err = io.StringIO() |
| 422 | with redirect_stderr(err): |
| 423 | plan = planner.plan_query( |
| 424 | topic="best espresso grinders", |
| 425 | available_sources=["reddit", "hackernews"], |
| 426 | requested_sources=None, |
| 427 | depth="default", |
| 428 | provider=provider, |
| 429 | model="dummy-model", |
| 430 | internal_subrun=internal_subrun, |
| 431 | ) |
| 432 | return plan, err.getvalue() |
| 433 | |
| 434 | |
| 435 | def test_provider_failure_prints_law7_reminder(): |
| 436 | plan, stderr = _plan_with_failing_provider(internal_subrun=False) |
| 437 | assert "LLM planning failed" in stderr |
| 438 | assert "No --plan passed" in stderr |
| 439 | assert "YOU ARE the planner" in stderr |
| 440 | assert plan.subqueries |
| 441 | assert any("fallback-plan" in note for note in plan.notes) |
| 442 | |
| 443 | |
| 444 | def test_provider_failure_in_internal_subrun_stays_quiet(): |
| 445 | plan, stderr = _plan_with_failing_provider(internal_subrun=True) |
| 446 | assert "LLM planning failed" in stderr |
| 447 | assert "No --plan passed" not in stderr |
| 448 | assert plan.subqueries |
| 449 | |
| 450 | |
| 451 | def test_successful_provider_plan_prints_no_reminder(): |
| 452 | provider = mock.Mock() |
| 453 | provider.generate_json.return_value = VALID_PLAN |
| 454 | err = io.StringIO() |
| 455 | with redirect_stderr(err): |
| 456 | plan = planner.plan_query( |
| 457 | topic="best espresso grinders", |
| 458 | available_sources=["reddit", "hackernews"], |
| 459 | requested_sources=None, |
| 460 | depth="default", |
| 461 | provider=provider, |
| 462 | model="dummy-model", |
| 463 | ) |
| 464 | assert plan.subqueries |
| 465 | assert "No --plan passed" not in err.getvalue() |
| 466 |