|
8 | 8 |
|
9 | 9 | from tests.control_plane.codex_checkpoint_fixture import NativeContextReader, last_context_page |
10 | 10 | from loopx.cli_commands.quota_capture import read_context_capture |
| 11 | +from loopx.control_plane.effect_runtime import EffectRuntimeRemoteError, EffectRuntimeStartupError |
11 | 12 | from loopx.control_plane.turn_driver.decision_capture import capture_page_range |
12 | 13 | from test_quota_settlement_cli import _run_cli |
13 | 14 |
|
@@ -276,3 +277,92 @@ def test_cli_missing_batch_pointer_identifies_original_argument_without_source_l |
276 | 277 | assert f"--select argument 3, token {token}" in result["reason"] |
277 | 278 | assert pointer not in result["reason"] and "stop if unavailable" not in json.dumps(result) |
278 | 279 | assert path.read_bytes() == raw and not registry.exists() and not runtime.exists() |
| 280 | + |
| 281 | + |
| 282 | +def test_cli_repairs_missing_selection_using_typed_error_and_same_capture(tmp_path): |
| 283 | + path, raw, original = fixture_capture(tmp_path) |
| 284 | + registry, runtime = tmp_path / "absent.json", tmp_path / "absent-runtime" |
| 285 | + command = ["quota", "context-read", "--capture-file", str(path), |
| 286 | + "--capture-sha256", hashlib.sha256(raw).hexdigest()] |
| 287 | + code, failure = _run_cli(registry, runtime, *command, |
| 288 | + "--select", "/large", "--select", "/rows/0/missing", cwd=tmp_path) |
| 289 | + assert code != 0 and failure["ok"] is False and "content" not in failure |
| 290 | + assert failure["error_kind"] == "request_rejected" |
| 291 | + assert failure["diagnostic_code"] == "context_selection_missing_pointer" |
| 292 | + assert "--list" in failure["recovery"] and "Restore" not in failure["recovery"] |
| 293 | + # Branch on the typed cause, not wording in reason; discover and retry |
| 294 | + # against the original capture without another guard or admission. |
| 295 | + code, index = _run_cli(registry, runtime, *command, "--select", "/rows/0", "--list", cwd=tmp_path) |
| 296 | + assert code == 0 and index["ok"] and index["eof"], index |
| 297 | + fields = json.loads(index["content"])["selections"][0]["fields"] |
| 298 | + pointer = next(field["pointer"] for field in fields if field["pointer"] == "/rows/0/tail") |
| 299 | + code, selected = _run_cli(registry, runtime, *command, "--select", pointer, cwd=tmp_path) |
| 300 | + assert code == 0 and selected["ok"] and selected["eof"], selected |
| 301 | + assert json.loads(selected["content"])["selections"] == [ |
| 302 | + {"pointer": pointer, "value": original["rows"][0]["tail"]}] |
| 303 | + assert capture_page_range(selected, raw=raw, digest=selected["sha256"]) is None |
| 304 | + assert path.read_bytes() == raw and not registry.exists() and not runtime.exists() |
| 305 | + |
| 306 | + |
| 307 | +@pytest.mark.parametrize("kind,code", [ |
| 308 | + ("request_rejected", "request_too_large"), |
| 309 | + ("io_transient", "io_busy"), |
| 310 | + ("io_permanent", "io_permission_denied"), |
| 311 | + ("internal_failure", "unexpected_handler_error"), |
| 312 | +]) |
| 313 | +def test_reader_preserves_runtime_diagnostics_without_parsing_reason(tmp_path, monkeypatch, kind, code): |
| 314 | + from loopx.control_plane.quota import turn_envelope |
| 315 | + |
| 316 | + path, raw, _ = fixture_capture(tmp_path) |
| 317 | + |
| 318 | + def fail(*_args, **_kwargs): |
| 319 | + raise EffectRuntimeRemoteError("same opaque reason", error_kind=kind, diagnostic_code=code) |
| 320 | + |
| 321 | + monkeypatch.setattr(turn_envelope, "_turn_envelope_result", fail) |
| 322 | + result = read_context_capture(args(path, raw)) |
| 323 | + assert result["ok"] is False and "content" not in result and "next_command" not in result |
| 324 | + assert result["error_kind"] == kind and result["diagnostic_code"] == code |
| 325 | + assert result["reason"] == "same opaque reason" |
| 326 | + assert "Restore" not in result["recovery"] |
| 327 | + assert ("--list" in result["recovery"]) is (kind == "request_rejected") |
| 328 | + assert path.read_bytes() == raw |
| 329 | + |
| 330 | + |
| 331 | +def test_runtime_startup_failure_does_not_request_capture_restoration(tmp_path, monkeypatch): |
| 332 | + from loopx.control_plane.quota import turn_envelope |
| 333 | + |
| 334 | + path, raw, _ = fixture_capture(tmp_path) |
| 335 | + |
| 336 | + def fail(*_args, **_kwargs): |
| 337 | + raise EffectRuntimeStartupError("runtime unavailable", diagnostic_code="runtime_startup_timeout") |
| 338 | + |
| 339 | + monkeypatch.setattr(turn_envelope, "_turn_envelope_result", fail) |
| 340 | + result = read_context_capture(args(path, raw)) |
| 341 | + assert result["ok"] is False and "content" not in result |
| 342 | + assert result["diagnostic_code"] == "runtime_startup_timeout" |
| 343 | + assert "runtime" in result["recovery"] and "Restore" not in result["recovery"] |
| 344 | + assert path.read_bytes() == raw |
| 345 | + |
| 346 | + |
| 347 | +@pytest.mark.parametrize("damage", ["missing", "digest", "json"]) |
| 348 | +def test_capture_failure_cannot_be_repaired_as_a_selector_error(tmp_path, monkeypatch, damage): |
| 349 | + from loopx.control_plane.quota import turn_envelope |
| 350 | + |
| 351 | + path, raw, _ = fixture_capture(tmp_path) |
| 352 | + request = args(path, raw) |
| 353 | + if damage == "missing": |
| 354 | + path.unlink() |
| 355 | + elif damage == "digest": |
| 356 | + path.write_bytes(b"{}") |
| 357 | + else: |
| 358 | + path.write_bytes(b"{broken") |
| 359 | + request.capture_sha256 = hashlib.sha256(path.read_bytes()).hexdigest() |
| 360 | + |
| 361 | + def unexpected(*_args, **_kwargs): |
| 362 | + pytest.fail("invalid captures must not reach selection") |
| 363 | + |
| 364 | + monkeypatch.setattr(turn_envelope, "_turn_envelope_result", unexpected) |
| 365 | + result = read_context_capture(request) |
| 366 | + assert result["ok"] is False and "content" not in result and "next_command" not in result |
| 367 | + assert "diagnostic_code" not in result and "error_kind" not in result |
| 368 | + assert "Restore the original digest-bound capture" in result["recovery"] |
0 commit comments