diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index 33d14cb766..36ed8d8e1d 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -48,7 +48,7 @@ from .private_images import private_message_caption, private_message_images from .private_delivery_discovery import (PrivateDeliveryDiscovery, allowed_answer_image_urls, answer_image_source_matches, answer_media_dispatch, historical_image_reply_matches, prepared_steward_binding_waits, resumable_answer_image_session) -from .private_ingress import PrivateSourceUnavailable, private_source_text, steward_source_preflight +from .private_ingress import PrivateSourceUnavailable, private_source_text, recovery_source_matches, steward_source_preflight from .private_ingress import project_private_reply_history as project_private_reply_history from . import private_steward_triage as triage from .return_files import uploaded_chat_images, verify_chat_images @@ -682,6 +682,17 @@ def sources() -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]: if row.get("binding_id") == record["binding_id"]), None) if current != selected["binding"]: return None + # Every recovered answer uses the frozen original input, + # including a native result delivered as the first reply + # or as a follow-up to an existing failure notice. + review = (record.get("context_recovery") or {}).get("answer_review") is True + result_turn = (native.get("native_result_recovery") or {}).get("turn_id") + if review or (result_turn is not None and result_turn == record.get("turn_id")): + current_request = self.core.read_request(record["request_ref"]) + if (any(current_request.get(key) != record.get(key) + for key in ("request_ref", "binding_id", "source", "session_id", "turn_id")) + or not recovery_source_matches(record, message, current_request)): + return None if sender_identity is not None: if observation is None: return None @@ -1264,7 +1275,9 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, if historical_source_check is not None and phase != "answer_images.historical": raise ValueError("historical source verification requires a media repair") phase = presentation_phase(record, phase) - expected_text = record["event"]["content"] if kind == "answer_images" else None + source_authority = partial(self._source_authority, record) + if kind == "answer_images": + source_authority = partial(source_authority, expected_text=record["event"]["content"]) phase_state = record["deliveries"].get(phase, {}) if phase_state.get("verified"): return True @@ -1298,8 +1311,8 @@ def provider_images_current() -> bool: def verified_source() -> bool: if historical_source_check is not None: return historical_source_check() - name, verified = self._source_authority(record, record["profile"], - record["event"]["chat_id"], record["event"]["message_id"], expected_text=expected_text) + name, verified = source_authority(record["profile"], + record["event"]["chat_id"], record["event"]["message_id"]) return bool(name) and verified def image_authorized() -> bool: @@ -1328,7 +1341,7 @@ def image_authorized() -> bool: **({"delivery_id": delivery_id} if delivery_id is not None else {}), finalize_reactions=finalize_source and not edit_only and kind not in {"admission", "progress", "periodic_report"} and not (kind == "terminal" and record.get("commission_resources")), source_authority_verifier=lambda profile, chat, message: ( - self._source_authority(record, profile, chat, message, expected_text=expected_text) + source_authority(profile, chat, message) if authorization_check is None or authorization_check() else (None, False))) progress = record["deliveries"].get(presentation_phase(record, "progress")) or {} stream = presentation_stream(record) @@ -2195,38 +2208,10 @@ def reconcile(self) -> int: def _recovery_source_observation(self, record: dict[str, Any], selected: dict[str, Any]) -> dict[str, Any] | None: source = self._source_message(record, selected=selected) - native = self.core.read_request(record["request_ref"]) - expected = record.get("reply_context_source_text", native.get("message")) - # Resume the admitted text, never an edited/replaced source. Keep the - # original reply-context text when admission had added verified ancestry. - # An unavailable rendering is unknown, not permission to guess a body. if source is None: return None - message_type = record["event"].get("message_type") - if source.get("msg_type", source.get("message_type")) != message_type: - return None - if message_type == "post": - # Posts froze their provider rendering at intake. Text-only input - # must also equal its admitted text; image input retains its - # original model text and frozen attachment payload below. - frozen = record.get("source_content") - images = record.get("attachments") - frozen_images = (isinstance(images, list) and bool(images) - and images == native.get("attachments") and record.get("message") == native.get("message")) - if (record.get("attachment_notice") or not isinstance(frozen, str) - or (not frozen_images and (images != [] or native.get("attachments") - or frozen.strip() != expected))): - return None - # The original post's image keys/caption must still render exactly - # as frozen at intake. Core separately verifies the saved image - # bytes against the original pre-dispatch Turn; never redownload or - # silently execute a text-only replacement of an image request. - expected = frozen - elif message_type != "text": - return None - if not isinstance(source.get("content"), str) or source["content"] != expected: - return None - return dict(source) + native = self.core.read_request(record["request_ref"]) + return dict(source) if recovery_source_matches(record, source, native) else None def _received_during_recovery(self, path: Path) -> None: """Acknowledge durable ordinary admission independently of recovery. diff --git a/loopx/extensions/lark/private_ingress.py b/loopx/extensions/lark/private_ingress.py index 3450e34bd9..ae087c3891 100644 --- a/loopx/extensions/lark/private_ingress.py +++ b/loopx/extensions/lark/private_ingress.py @@ -47,6 +47,36 @@ def _private_source_input(message: Mapping[str, Any]) -> tuple[Any, ...]: return kind, content, message.get("create_time"), ancestry +def recovery_source_matches(record: Mapping[str, Any], source: Mapping[str, Any], + native: Mapping[str, Any]) -> bool: + """Match a fresh provider observation against the originally admitted input.""" + if source.get("deleted") is True: + return False + # Keep the source text without verified ancestry added for Core. A missing + # provider rendering is unknown and cannot stand in for the frozen body. + expected = record.get("reply_context_source_text", native.get("message")) + message_type = record["event"].get("message_type") + if source.get("msg_type", source.get("message_type")) != message_type: + return False + if message_type == "post": + # Posts froze their rendering at intake. Text-only input must also equal + # the admitted text. For image input Core separately verifies the saved + # attachment bytes against the original Turn. + frozen = record.get("source_content") + images = record.get("attachments") + frozen_images = (isinstance(images, list) and bool(images) + and images == native.get("attachments") and record.get("message") == native.get("message")) + if (record.get("attachment_notice") or not isinstance(frozen, str) + or (not frozen_images and (images != [] or native.get("attachments") + or frozen.strip() != expected))): + return False + expected = frozen + elif message_type != "text": + return False + observed = source.get("content") + return isinstance(observed, str) and observed == expected + + def steward_source_preflight( binding: Mapping[str, Any], original: Mapping[str, Any], read_source: Callable[[dict[str, Any]], Mapping[str, Any] | None], diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index 1c9d0c59bf..78a3e6bb56 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -1690,7 +1690,34 @@ def test_private_delivery_keeps_retired_steward_cards_out_of_image_recovery( runtime.close() -@pytest.mark.parametrize("case", ["normal", "invalid-metadata", "metadata-only", "unfinished", "source-changed", "closed", "lost-ack", "message-crash", +def assert_recovered_reply_waits_for_original_source(transport, provider, path, source_id, fault, monkeypatch): + original = provider.messages[source_id] + change = ({"deleted": True} if fault == "source-recalled-at-send" else {"content": "Edited during preview"}) + changed = {**original, **change} + reply = transport._reply_runner + + def preview_then_change(args): + result = reply(args) + if "--dry-run" in args and ("+messages-send" in args or "+messages-reply" in args): + provider.messages[source_id] = changed + return result + + writes = list(provider.writes) + with monkeypatch.context() as send: + send.setattr(transport, "_reply_runner", preview_then_change) + delivered = transport.reconcile_request(path) + assert provider.messages[source_id] is changed + assert delivered == 0 and provider.writes == writes + pending = transport.core.read_request(path.stem)["native_result_recovery"] + assert pending["delivery_verified"] is False + assert transport.reconcile_request(path) == 0 and provider.writes == writes + provider.messages[source_id] = original + assert transport.reconcile_request(path) == 1 + assert len(provider.writes) == len(writes) + 1 + + +@pytest.mark.parametrize("case", ["normal", "invalid-metadata", "metadata-only", "unfinished", + "source-changed", "source-edited-at-send", "source-recalled-at-send", "closed", "lost-ack", "message-crash", "historical", "historical-source-changed", "historical-stop"]) def test_original_worker_recovers_native_result_without_replay(tmp_path, language_server, monkeypatch, case): # noqa: F811 from test_native_steward_private import steward @@ -1777,7 +1804,11 @@ def crash(*args, **kwargs): pass restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, runner=provider, cli_bin="lark-cli") - restarted.reconcile() + if case in {"source-edited-at-send", "source-recalled-at-send"}: + assert_recovered_reply_waits_for_original_source( + restarted, provider, path, event["message_id"], case, monkeypatch) + else: + restarted.reconcile() native = restarted.core.read_request(ref) recovered = native.get("native_result_recovery") if fault in {"source-changed", "closed", "stop"}: diff --git a/tests/test_steward_interrupted_answer.py b/tests/test_steward_interrupted_answer.py index d11f737ab0..0476df0309 100644 --- a/tests/test_steward_interrupted_answer.py +++ b/tests/test_steward_interrupted_answer.py @@ -50,12 +50,14 @@ def test_missing_final_message_keeps_the_existing_metadata_contract(tmp_path, la assert adapter.start_turn("Preserve the existing typed metadata") == expected -@pytest.mark.parametrize("fault", [None, "source-changed", "closed", "stop", "native-unavailable", "action", "lost-ack"]) +@pytest.mark.parametrize("fault", [None, "source-changed", "source-edited-at-send", "source-recalled-at-send", + "closed", "stop", "native-unavailable", "action", "lost-ack"]) def test_original_queue_returns_missing_final_message_from_retained_native_result_without_replay( steward, language_server, monkeypatch, fault, ): from loopx.chat import parse_agent_response from loopx.extensions.lark.private_conversations import LarkPrivateConversations + from test_lark_private_conversations import assert_recovered_reply_waits_for_original_source monkeypatch.setenv("LOOPX_MANAGER_MODEL", "gemini-4-argon-high") monkeypatch.setenv("LOOPX_MANAGER_REASONING_EFFORT", "high") @@ -96,30 +98,36 @@ def legacy_parser(text, **kwargs): writes = len(provider.writes) restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, runner=provider, cli_bin="lark-cli") + path = restarted.root / f"{ref}.json" if fault == "stop": monkeypatch.setattr(restarted.core, "_source_stop_observed", lambda *_, **__: True) - restarted.reconcile() + late_source_change = fault in {"source-edited-at-send", "source-recalled-at-send"} + if late_source_change: + assert_recovered_reply_waits_for_original_source( + restarted, provider, path, event["message_id"], fault, monkeypatch) + else: + restarted.reconcile() recovered = restarted.core.read_request(ref).get("native_result_recovery") if fault == "lost-ack": assert recovered and recovered["delivery_verified"] is False - journal = json.loads((restarted.root / f"{ref}.json").read_text()) + journal = json.loads(path.read_text()) assert journal["deliveries"]["terminal"]["started"] assert not journal["deliveries"]["terminal"].get("attempt") after = list(provider.writes) restarted.reconcile() assert provider.writes == after and len(provider.writes) == writes + 1 - journal = json.loads((restarted.root / f"{ref}.json").read_text()) + journal = json.loads(path.read_text()) assert journal["deliveries"]["terminal"]["blocker"] == "provider_delivery_receipt_unknown" elif fault == "action": assert recovered is None assert all("待办尚未写入" in text for _, text in provider.writes[writes:]) assert restarted.core.read_request(ref).get("native_result_recovery") is None - elif fault: + elif fault and not late_source_change: assert recovered is None and len(provider.writes) == writes else: assert recovered["delivery_verified"] is True assert restarted.core.read_request(ref)["delivery_verified"] is True - journal = json.loads((restarted.root / f"{ref}.json").read_text()) + journal = json.loads(path.read_text()) assert journal["status"] == "delivered" and journal["deliveries"]["terminal"]["verified"] terminal = journal["deliveries"]["terminal"] assert "Original verified result." in terminal["text"] @@ -1190,7 +1198,10 @@ def read(_url, _token, method, _body, **_): assert not set(calls) - {"GetCascadeTrajectory", "GetCascadeTrajectorySteps"} -@pytest.mark.parametrize("fault", [None, "stop", "source-changed", "no-capacity", "review-action", "review-gate", "queue-change", "already-codex", "post-enqueue-crash", "lost-ack", "new-question-during-answer", "restricted-review", "primary-quota-unreadable", "native-tail", "authenticated-host", "file-read", "file-running", "cleared-answer"]) +@pytest.mark.parametrize("fault", [None, "stop", "source-changed", "source-edited-at-send", "source-recalled-at-send", + "no-capacity", "review-action", "review-gate", "queue-change", "already-codex", "post-enqueue-crash", + "lost-ack", "new-question-during-answer", "restricted-review", "primary-quota-unreadable", "native-tail", + "authenticated-host", "file-read", "file-running", "cleared-answer"]) def test_original_worker_answers_interrupted_steward_question_once(tmp_path, language_server, monkeypatch, fault): # noqa: F811 import loopx.chat_manager as manager_module import loopx.chat_runtime as runtime_module @@ -1384,7 +1395,39 @@ def crash(**kwargs): assert recovered["context_recovery"]["answer_review"] is True done = runtime.wait_for_turn(session_id=recovered["session_id"], turn_id=recovered["turn_id"], timeout_sec=15) assert done["status"] == ("failed" if fault == "review-action" else "completed"), done - transport.reconcile_request(path) + if fault in {"source-edited-at-send", "source-recalled-at-send"}: + # Change the original provider source after the first check, + # while the final reply preview is in flight. + original_message = provider.messages[event["message_id"]] + change = ({"content": "Edited after review"} if fault == "source-edited-at-send" + else {"deleted": True}) + changed = {**original_message, **change} + reply = transport._reply_runner + previewed = False + + def preview_then_change(args): + nonlocal previewed + result = reply(args) + if "--dry-run" in args and ("+messages-reply" in args or "+messages-send" in args): + previewed = True + provider.messages[event["message_id"]] = changed + return result + + writes = list(provider.writes) + with monkeypatch.context() as send: + send.setattr(transport, "_reply_runner", preview_then_change) + delivery_count = transport.reconcile_request(path) + assert previewed + assert delivery_count == 0 and provider.writes == writes + assert not transport.core.read_request(ref).get("delivery_verified") + assert transport.reconcile_request(path) == 0 and provider.writes == writes + # Once the same frozen source is available again, the original + # completed review can be delivered without another Turn. + provider.messages[event["message_id"]] = original_message + assert transport.reconcile_request(path) == 1 + assert len(provider.writes) == len(writes) + 1 + else: + transport.reconcile_request(path) current = transport.core.read_request(ref) assert current["delivery_verified"] for later in new_question: