From 1f607a16a337f8f302b0e37657ac479d84b5be43 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:07:29 +0800 Subject: [PATCH] fix(qualification): retain failed receipts for unreadable context references Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../testing/model_tool_behavior.py | 5 +++- .../control_plane/test_model_tool_context.py | 8 +++++- .../test_terminal_settlement_tool_behavior.py | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/loopx/control_plane/testing/model_tool_behavior.py b/loopx/control_plane/testing/model_tool_behavior.py index 3c75a206d4..caa9d80203 100644 --- a/loopx/control_plane/testing/model_tool_behavior.py +++ b/loopx/control_plane/testing/model_tool_behavior.py @@ -637,7 +637,10 @@ def read(self, command: str, *, source_root: Path, project_root: Path) -> str: if path or digest: raise ValueError("context reader cannot mix reference and capture arguments") reader = Path(reference_file).expanduser() - _, reference = read_context_read_reference(str(reader if reader.is_absolute() else project_root / reader)) + try: + _, reference = read_context_read_reference(str(reader if reader.is_absolute() else project_root / reader)) + except OSError as exc: + raise ValueError("context reader reference could not be read") from exc path, digest = reference["path"], reference["sha256"] source = Path(path).expanduser() if path else None source = (source if source.is_absolute() else project_root / source) if source else None diff --git a/tests/control_plane/test_model_tool_context.py b/tests/control_plane/test_model_tool_context.py index 744df20385..6556b706d4 100644 --- a/tests/control_plane/test_model_tool_context.py +++ b/tests/control_plane/test_model_tool_context.py @@ -90,7 +90,8 @@ def test_scripted_consumer_cannot_replace_current_retained_context_with_eof(tmp_ latest_quota_payload({"messages": messages}) -@pytest.mark.parametrize("attack", ["other_capture", "other_digest", "session_lookup", "shell_chain"]) +@pytest.mark.parametrize("attack", ["other_capture", "other_digest", "session_lookup", "shell_chain", + "missing_reference", "missing_reference_parent"]) @NATIVE_CAPTURE_TEST def test_model_read_cannot_escape_the_current_capture(tmp_path, attack): output, _ = _captured_output(tmp_path / "current") @@ -106,6 +107,11 @@ def test_model_read_cannot_escape_the_current_capture(tmp_path, attack): command[8] = "0" * 64 elif attack == "session_lookup": command = command[:5] + ["--codex-context-dir", str(tmp_path)] + elif attack in {"missing_reference", "missing_reference_parent"}: + reader = tmp_path / "missing.reader.json" + if attack == "missing_reference_parent": + reader = tmp_path / "missing" / "current.reader.json" + command = command[:5] + ["--reference-file", str(reader)] shell = shlex.join(command) if attack == "shell_chain": shell += " && loopx quota should-run --goal-id another-goal" diff --git a/tests/control_plane/test_terminal_settlement_tool_behavior.py b/tests/control_plane/test_terminal_settlement_tool_behavior.py index df2505fa1a..e11822de31 100644 --- a/tests/control_plane/test_terminal_settlement_tool_behavior.py +++ b/tests/control_plane/test_terminal_settlement_tool_behavior.py @@ -42,6 +42,32 @@ def _latest_quota_packet(request: Mapping[str, Any]) -> dict[str, Any]: raise AssertionError("scripted model did not receive the real quota packet") +def test_missing_context_reference_returns_a_bounded_failed_receipt(tmp_path, monkeypatch): + monkeypatch.delenv("LOOPX_CONTEXT_DELIVERY", raising=False) + fixture = _build_fixture(tmp_path / "oracle") + missing_reader = tmp_path / "missing" / "current.reader.json" + transport = ScriptedDoubaoExecTransport([ + ScriptedExecToolAction(command=fixture.quota_guard_command.replace( + '"${LOOPX_TURN:?}"', "missing-reader-turn")), + ScriptedExecToolAction(command=shlex.join([ + "loopx", "--format", "json", "quota", "context-read", + "--reference-file", str(missing_reader), + ])), + ]) + + receipt = DoubaoTerminalSettlementToolBehaviorActor( + api_key="test-only-placeholder", transport=transport, + ).qualify(qualification_id="missing-reader", fixture_root=tmp_path / "actor") + + assert receipt["qualification_passed"] is False + assert receipt["failure_code"] == "tool_execution_failed" + assert receipt["observed_tool_sequence"] == ["quota_should_run", "tool_execution_failed"] + assert receipt["settlement_receipt_steps"] == [] + assert len(transport.requests) == 2 + assert str(missing_reader) not in json.dumps(receipt) + assert not receipt["boundary"]["external_writes_executed"] + + def _writeback_action(request: Mapping[str, Any]) -> ScriptedExecToolAction: packet = _latest_quota_packet(request) command = packet["interaction_contract"]["cli_channel"]["next_cli_actions"][0]