From d80c1d1f690503665b837340158d3f8a13284c66 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:00:27 +0800 Subject: [PATCH] fix(lark): recover captured images from legacy private replies Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/file_discovery.py | 3 +- .../extensions/lark/private_conversations.py | 63 +++++++---- tests/test_lark_private_conversations.py | 106 +++++++++++++++++- 3 files changed, 151 insertions(+), 21 deletions(-) diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py index be2480b4df..0cda0b2c29 100644 --- a/loopx/capabilities/native_chat/file_discovery.py +++ b/loopx/capabilities/native_chat/file_discovery.py @@ -149,6 +149,7 @@ def rows(self, directory: Path, paths: Iterable[Path], *, ".gif": "image/gif", ".webp": "image/webp", } _REPLY_IMAGE_SIGNATURES = {"image/png": b"\x89PNG\r\n\x1a\n", "image/jpeg": b"\xff\xd8\xff", "image/gif": b"GIF8"} +REPLY_IMAGE_NAME_PATTERN = re.compile(r"[a-zA-Z0-9][a-zA-Z0-9_.-]{0,119}") def reply_image_directory(turn_id: str) -> Path: @@ -189,7 +190,7 @@ def reply_image_attachment(data: bytes, mime: str, index: int) -> dict[str, Any] def read_reply_image(workspace: Path, turn_id: str, name: str) -> tuple[str, bytes]: from ...control_plane.collaboration.result_files import _workspace_file - if not re.fullmatch(r"[a-zA-Z0-9][a-zA-Z0-9_.-]{0,119}", name): + if not REPLY_IMAGE_NAME_PATTERN.fullmatch(name): raise ValueError("invalid reply image name") mime = _REPLY_IMAGE_TYPES.get(Path(name).suffix.lower()) if mime is None: diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index bb48916cdc..1c8d57975b 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -27,7 +27,9 @@ STEWARD_BINDING_ADOPTION_PENDING, ) from ...capabilities.native_chat.conversation_bindings import ChatConversationBindings -from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint, reply_image_workspace +from ...capabilities.native_chat.file_discovery import ( + REPLY_IMAGE_NAME_PATTERN, FileChatDiscovery, file_change_fingerprint, reply_image_workspace, +) from ...chat_attachments import CHAT_IMAGE_MAX_COUNT, normalize_chat_image_attachments from ...chat_store import CHAT_MESSAGE_SCHEMA_VERSION, ChatSessionStore, TERMINAL_TURN_STATES, _atomic_write_json, _read_json from ...control_plane.collaboration import conversation_reply_context @@ -1567,35 +1569,45 @@ def _historical_image_source(self, record: Mapping[str, Any], plan: Mapping[str, for key in ("request_ref", "binding_id", "source", "session_id", "turn_id"))): raise ValueError("original request no longer matches") turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"]) or {} - if (not isinstance(answer := (turn.get("response") or {}).get("message"), str) + response = turn.get("response") or {} + if (not isinstance(answer := response.get("message"), str) or hashlib.sha256(answer.encode()).hexdigest() != plan["answer_sha256"] or terminal.get("text") != _presentation_text(answer) - or not private_answer_images(answer)[1] or (session := resumable_answer_image_session(self.core, native, turn)) is None): raise ValueError("original completed answer or its authority changed") + image_targets = private_answer_images(answer)[1] + if "captured_images" in plan: + if image_targets or response.get("answer_image_plan") or reply_image_workspace(session, turn) is None: + raise ValueError("original reply is not a qualified legacy project answer") + elif not image_targets: + raise ValueError("original remote image reply unavailable") return session def _historical_image_plan(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict: from ..public_source_reader import public_url - if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", "images"}: + captured = "captured_images" in plan + image_field, reference_field = ("captured_images", "name") if captured else ("images", "url") + if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", image_field}: raise ValueError("invalid original image repair fields") if (not isinstance(plan["message_id"], str) or not LARK_MESSAGE_ID_PATTERN.fullmatch(plan["message_id"]) or not isinstance(plan["answer_sha256"], str) or not BARE_SHA256_PATTERN.fullmatch(plan["answer_sha256"]) - or not isinstance(plan["images"], list) or not 1 <= len(plan["images"]) <= CHAT_IMAGE_MAX_COUNT): + or not isinstance(plan[image_field], list) or not 1 <= len(plan[image_field]) <= CHAT_IMAGE_MAX_COUNT): raise ValueError("invalid original image repair target") images = [] - for image in plan["images"]: - if (not isinstance(image, dict) or set(image) != {"url", "sha256"} - or not isinstance(image["sha256"], str) or not BARE_SHA256_PATTERN.fullmatch(image["sha256"]) - or not isinstance(image["url"], str)): - raise ValueError("each original image requires a URL and its SHA-256") - images.append({"url": public_url(image["url"]), "sha256": image["sha256"]}) - if len({image["url"] for image in images}) != len(images): - raise ValueError("duplicate original image URL") + for image in plan[image_field]: + if (not isinstance(image, dict) or set(image) != {reference_field, "sha256"} + or not isinstance(reference := image[reference_field], str) + or not isinstance(image["sha256"], str) or not BARE_SHA256_PATTERN.fullmatch(image["sha256"])): + raise ValueError("each original image requires a reference and its SHA-256") + if captured and not REPLY_IMAGE_NAME_PATTERN.fullmatch(reference): + raise ValueError("invalid original image snapshot name") + images.append({reference_field: reference if captured else public_url(reference), "sha256": image["sha256"]}) + if len({image[reference_field] for image in images}) != len(images): + raise ValueError("duplicate original image reference") self._historical_image_source(record, plan) - return {**plan, "images": images} + return {**plan, image_field: images} def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dict | None: if record.get("historical_answer_images") is not None: @@ -1652,17 +1664,30 @@ def original_pair_matches() -> bool: if not original_pair_matches(): return 0 phase = "answer_images.historical" + snapshots = plan.get("captured_images") + if snapshots is None: + images = plan["images"] + targets = [item["url"] for item in images] + local_scope = None + verification_key = "source_urls_verified" + else: + # The operator stages the exact Turn's snapshots in this private host + # directory. Paths from the project and model text are never opened here. + images = snapshots + targets = snapshots + local_scope = (self.root / "historical-answer-images", plan["turn_id"]) + verification_key = "captured_images_verified" attachments = state.get("attachments") - recheck_sources = attachments is None or state.get("source_urls_verified") is not True + recheck_sources = attachments is None or state.get(verification_key) is not True if recheck_sources: - attachments = read_answer_images(event["content"], [item["url"] for item in plan["images"]], - attachments, record["deliveries"].get(phase, {})) + attachments = read_answer_images(event["content"], targets, attachments, + record["deliveries"].get(phase, {}), local_scope=local_scope) digests = [hashlib.sha256(base64.b64decode( picture["data_url"].split(",", 1)[1], validate=True)).hexdigest() for picture in attachments] - if digests != [item["sha256"] for item in plan["images"]]: + if digests != [item["sha256"] for item in images]: return 0 if recheck_sources: - state.update(attachments=attachments, source_urls_verified=True) + state.update(attachments=attachments, **{verification_key: True}) _atomic_write_json(path, record) def authorized() -> bool: try: diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index 2577943713..8ce73590fb 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -1560,7 +1560,7 @@ def restart(): # Produce an actual legacy reply and provider receipt before installing # the media-only repair. The stored answer and Core are real. with monkeypatch.context() as legacy: - legacy.setattr(transport, "_deliver_completed_answer", lambda p, row, text, *, inbox: + legacy.setattr(transport, "_deliver_completed_answer", lambda p, row, text, *, inbox, answer_image_plan=None: transport._deliver(p, row, "terminal", text, inbox=inbox)) assert transport.reconcile_request(path) == 1 saved = json.loads(path.read_text()) @@ -1753,6 +1753,110 @@ def edit_source_before_lock(path, **kwargs): runtime.close() +@pytest.mark.parametrize("fault", [ + "none", "bad_digest", "different_turn", "symlink", "foreign_name", + "edited_reply", "stopped", "revoked", "unknown_upload", "unknown_send", +]) +def test_legacy_local_reply_images_are_recovered_only_by_the_original_worker(ordinary, source_https, fault): # noqa: F811 + from hashlib import sha256 + + from loopx.capabilities.native_chat.file_discovery import reply_image_directory + from loopx.chat_store import _atomic_write_json + from test_chat_image_attachments import PNG_BYTES + from test_lark_app_image_projection import ImageIO + + _, _, contexts, _, capture, fake, _ = ordinary + contexts.workspace_grant = "workspace_write" + store, runtime, provider, transport = connect(ordinary) + io = ImageIO(provider) + transport.runner = io + fetched, _ = source_https(lambda _: (None, "image/png", PNG_BYTES)) + answer = "Original explanation.\n\nFirst figure\n\nSecond figure" + marker = '"message": "Runtime response.",' + fake.write_text(fake.read_text().replace(marker, f'"message": {answer!r},', 1)) + try: + event = provider.event("notes-app", "historical-captured-images", "Read the requested figures.") + assert transport.admit("notes-app", event)["status"] == "durably_accepted" + request = transport.core.pending()[0] + sid, tid, ref = (request[key] for key in ("session_id", "turn_id", "request_ref")) + completed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10) + assert completed["status"] == "completed" and not completed["response"].get("answer_image_plan") + path = transport.root / f"{ref}.json" + assert transport.reconcile_request(path) == 1 + original = json.loads(path.read_text()) + terminal = original["deliveries"]["terminal"] + message_id = terminal["attempt"]["message_ref"] + core_before = transport.core.read_request(ref), completed, store.messages(sid) + assert terminal["verified"] and not terminal.get("image_keys") + + private = transport.root / "historical-answer-images" + staged = private / reply_image_directory("f" * 32 if fault == "different_turn" else tid) + staged.mkdir(parents=True) + raw_images = [PNG_BYTES + b"first", PNG_BYTES + b"second"] + names = ["figure-first.png", "figure-second.png"] + for name, data in zip(names, raw_images, strict=True): + output = staged / name + if fault == "symlink": + other = staged.parent / name + other.write_bytes(data) + output.symlink_to(other) + else: + output.write_bytes(data) + manifest = {"binding_id": original["binding_id"], "turn_id": tid, "message_id": message_id, + "answer_sha256": sha256(answer.encode()).hexdigest(), + "captured_images": [{"name": name, "sha256": sha256(data).hexdigest()} + for name, data in zip(names, raw_images, strict=True)]} + if fault == "bad_digest": + (staged / names[1]).write_bytes(PNG_BYTES + b"changed") + if fault == "foreign_name": + manifest["captured_images"][0]["name"] = str(staged / names[0]) + _atomic_write_json(private / path.name, manifest) + + if fault == "edited_reply": + provider.messages[message_id]["body"]["content"] = '{"zh_cn":{"content":[[{"tag":"text","text":"Changed"}]]}}' + elif fault == "stopped": + transport.admit("notes-app", provider.event("notes-app", "stop-legacy-captured-images", "/stop")) + elif fault == "revoked": + contexts.workspace_grant = "workspace_read" + elif fault == "unknown_upload": + io.unknown_upload = True + elif fault == "unknown_send": + provider.lose_next_reply = True + + writes = len(provider.writes) + def restart(): + return LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, + runner=io, cli_bin="lark-cli") + + worker = restart() + assert path not in worker.pending_body_paths() + assert path in worker.pending_delivery_paths() + assert worker.reconcile_request(path) == int(fault == "none") + saved = json.loads(path.read_text()) + assert saved["deliveries"]["terminal"] == terminal + if fault == "none": + media = saved["deliveries"]["answer_images.historical"] + assert media["images_verified"] and io.downloads == 2 + post = provider.messages[media["attempt"]["message_ref"]] + assert post["chat_id"] == event["chat_id"] + assert post["sender"]["id"] == provider.profile_apps["notes-app"] + assert [io.resources[key] for key in media["image_keys"]] == raw_images + assert saved["historical_answer_images"]["status"] == "delivered" + elif fault in {"unknown_upload", "unknown_send"}: + saved["historical_answer_images"]["next_check_at"] = 0 + _atomic_write_json(path, saved) + io.unknown_upload = False + assert restart().reconcile_request(path) == 0 + assert len(provider.writes) == writes + int(fault in {"none", "unknown_send"}) + assert io.uploads == {"none": 2, "unknown_upload": 1, "unknown_send": 2}.get(fault, 0) + assert fetched == [] + assert sum(json.loads(line).get("method") == "turn/start" for line in capture.read_text().splitlines()) == 1 + if fault != "stopped": + assert core_before == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid)) + finally: + runtime.close() + + def test_private_delivery_keeps_retired_steward_cards_out_of_image_recovery( ordinary, # noqa: F811 ):