diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json index daddb4c3f3..c42a77b450 100644 --- a/loopx/canary/module_metric_baseline.json +++ b/loopx/canary/module_metric_baseline.json @@ -84,7 +84,7 @@ "lines": 2760 }, "loopx/extensions/lark/private_conversations.py": { - "lines": 2213 + "lines": 2583 }, "loopx/heartbeat_prompt.py": { "any_count": 17, diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py index f3c547215c..081e83bb6c 100644 --- a/loopx/capabilities/native_chat/external_conversations.py +++ b/loopx/capabilities/native_chat/external_conversations.py @@ -27,7 +27,7 @@ from ...file_lock import exclusive_file_lock, try_exclusive_file_lock from . import result_review_evidence from .codex_context import checked_session_home -from .file_discovery import FileChatDiscovery, later_equivalent_source_input, source_control_fields +from .file_discovery import FileChatDiscovery, external_source_route_path, later_equivalent_source_input, source_control_fields # Request journals are named by their 24-hex request_ref; lock metadata and # other siblings in this directory are not requests. @@ -548,7 +548,7 @@ def source_requests() -> list[dict[str, Any]]: # route already owns the fence, leave this request unchanged for # the existing worker's next fresh visit, rather than admitting # from an observation that waited behind that route. - with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json", operation="route_external_chat_request") as acquired: + with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]), operation="route_external_chat_request") as acquired: if acquired is None: return row current = self.bindings.read() @@ -740,7 +740,7 @@ def _recover_native_result(self, path: Path, row: dict[str, Any], session: dict[ proof = recovered.get("native_observation") selected = self.bindings.resolve(binding_id=row["binding_id"], **row["source"], include_observation=True) source = source_observation(selected) - with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json", + with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]), operation="route_external_chat_request") as acquired: if acquired is None: return row @@ -826,8 +826,7 @@ def admit_history_answer(self, *, binding_id: str, source: dict[str, Any], reque raise ValueError("invalid historical answer source") path = self.root / f"{request_ref}.json" with exclusive_file_lock(path, operation="admit_external_chat_request"): - with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json", - operation="route_external_chat_request"): + with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]), operation="route_external_chat_request"): selected = self.bindings.resolve(binding_id=binding_id, **source, include_observation=True) if source_preflight(deepcopy(selected)) is not None: raise ValueError("source preflight must verify or raise") @@ -1391,7 +1390,8 @@ def admit(self, *, binding_id: str, source: dict[str, Any], request_ref: str, # A duplicate waits on its own request before taking the route fence; # it must not hold up other messages while native cancellation runs. with exclusive_file_lock(path, operation="admit_external_chat_request"): - with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json", operation="route_external_chat_request"): + with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]), + operation="route_external_chat_request"): # Resolve and verify the source inside both fences before # creating or replaying any canonical request. selected = resolve_external_admission_source(self.bindings, binding_id, source, source_preflight) diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py index 7fd1a5a27f..1454dde4b8 100644 --- a/loopx/capabilities/native_chat/file_discovery.py +++ b/loopx/capabilities/native_chat/file_discovery.py @@ -11,6 +11,11 @@ from typing import Any +def external_source_route_path(root: Path, binding_id: str, source_ref: str) -> Path: + """Locate the original source's cross-process admission fence.""" + return root / "source-fences" / f"{binding_id}.{source_ref}.json" + + def _ordinary_request_text_digest(row: dict[str, Any]) -> str | None: """Match ordinary private text without retaining its body in discovery.""" message = row.get("message") diff --git a/loopx/extensions/lark/cli_resolution.py b/loopx/extensions/lark/cli_resolution.py index 01bb1fa8c0..acac8668a6 100644 --- a/loopx/extensions/lark/cli_resolution.py +++ b/loopx/extensions/lark/cli_resolution.py @@ -12,6 +12,7 @@ import shutil import subprocess from collections.abc import Callable, Mapping, Sequence +from contextlib import nullcontext from dataclasses import dataclass from pathlib import Path from typing import Any @@ -261,6 +262,8 @@ def run( args: list[str], cwd: Path | None = None, timeout: float | None = None, + *, + dispatch_guard=None, ) -> dict[str, Any]: if not resolution.available: raise LarkCliUnavailableError(resolution.error_code or "lark_cli_not_installed") @@ -272,14 +275,25 @@ def run( env["PATH"] = os.pathsep.join( [parent, *(part for part in current if part != parent)] ) - completed = subprocess_run( - args, - cwd=str(cwd) if cwd else None, - timeout=timeout, - capture_output=True, - env=env, - text=True, - ) + if dispatch_guard is not None: + with dispatch_guard(): + process = subprocess.Popen( + args, cwd=str(cwd) if cwd else None, env=env, text=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + ) + with process: + try: + stdout, stderr = process.communicate(timeout=timeout) + except BaseException: + process.kill() + process.communicate() + raise + completed = subprocess.CompletedProcess(args, process.returncode, stdout, stderr) + else: + completed = subprocess_run( + args, cwd=str(cwd) if cwd else None, timeout=timeout, + capture_output=True, env=env, text=True, + ) return { "returncode": completed.returncode, "stdout": completed.stdout, @@ -287,4 +301,14 @@ def run( "timed_out": False, } + run.dispatch_guard = True return run + + +def run_lark_command_with_dispatch_guard(runner, args, cwd, timeout, guard): + """Keep the native authority fence through CLI spawn, never response IO.""" + if getattr(runner, "dispatch_guard", False) is True: + return runner(args, cwd, timeout, dispatch_guard=guard) + # Injected in-process runners execute synchronously and own no subprocess. + with (guard() if guard is not None else nullcontext()): + return runner(args, cwd, timeout) diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index 8182ba2a7e..33d14cb766 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -5,7 +5,9 @@ """ from __future__ import annotations +import base64 import hashlib +import http.client import json import re import subprocess @@ -13,7 +15,7 @@ from collections.abc import Callable, Iterator, Mapping, Sequence from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timezone -from functools import cached_property +from functools import cached_property, partial from itertools import tee from pathlib import Path from threading import Lock @@ -26,6 +28,7 @@ ) from ...capabilities.native_chat.conversation_bindings import ChatConversationBindings from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint +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 from ...control_plane.effect_runtime import effect_runtime_request_scope @@ -36,14 +39,15 @@ from .event_inbox import acknowledge_lark_event_inbox, ingest_lark_event_inbox from .event_collector_runtime import enrich_lark_event_reply_context from .goal_channel_transport import call, json_payload, lark_args, lark_provider_mention_identities, record_event_timing -from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN +from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN, LARK_MESSAGE_ID_PATTERN from .inbox_reply import _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply from .manager_context import manager_failure_reply from .inbox_reactions import (_reconcile_outbound_input_reactions, mark_lark_event_inbox_processing, mark_lark_event_inbox_received) -from .outbound import present_lark_conversation_text as _presentation_text +from .outbound import normalize_lark_outbound_text, present_lark_conversation_text as _presentation_text from .private_images import private_message_caption, private_message_images -from .private_delivery_discovery import PrivateDeliveryDiscovery, prepared_steward_binding_waits +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 project_private_reply_history as project_private_reply_history from . import private_steward_triage as triage @@ -55,7 +59,90 @@ # Reserve edits for a final/closing notice; continue long Turns on a new draft. PROGRESS_EDIT_BUDGET = 12 CHAT_CONTROL_COMMANDS = {"/status": "status", "/help": "help", "/new": "new", "/stop": "stop"} +ANSWER_IMAGE_RETRY_SECONDS = 300 +ANSWER_IMAGE_SOURCE_TIMEOUT = 5 _UNSUPPORTED_MEDIA_LABELS = {"audio": "音频", "file": "文件", "media": "视频"} +_MARKDOWN_CODE_SPAN = re.compile(r"((?\n]+)>|([^\s<>()]+))(?:[ \t]+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)""") + + +def _private_answer_images(text: str) -> tuple[str, list[str]]: + """Remove image previews from provider prose, leaving the canonical answer alone.""" + lines, targets = [], [] + fence = None + for line in text.splitlines(keepends=True): + marker = re.match(r"^ {0,3}(\x60{3,}|~{3,})(.*)$", line) + if marker and fence is None: + fence = marker[1] + elif marker and fence and marker[1][0] == fence[0] and len(marker[1]) >= len(fence) and not marker[2].strip(): + fence = None + elif not fence and not line.startswith((" ", "\t")): + def link(match: re.Match[str]) -> str: + url = match[2] or match[3] + if url not in targets: + targets.append(url) + caption = match[1] or f"图片 {targets.index(url) + 1}" + return f"[{caption}](<{url}>)" if url.startswith("https://") and " " in url else ( + f"[{caption}]({url})" if url.startswith("https://") else caption) + + start, chunks = 0, [] + for span in _MARKDOWN_CODE_SPAN.finditer(line): + chunks.extend((_ANSWER_IMAGE.sub(link, line[start:span.start()]), span[0])) + start = span.end() + line = "".join((*chunks, _ANSWER_IMAGE.sub(link, line[start:]))) + lines.append(line) + return "".join(lines), targets + + +def _private_answer_image(url: str, *, allowed_urls: set[str], deadline: float, index: int) -> dict[str, Any]: + # Reuse anonymous HTTPS address pinning, redirect checks and byte limits. + # No local file, signed-in browser or model tool authority is involved. + from urllib.parse import urlsplit + + from ..public_source_reader import fetch + + host = urlsplit(url).hostname + same_host_urls = {candidate for candidate in allowed_urls if urlsplit(candidate).hostname == host} + _, mime, data = fetch(url, deadline=deadline, allowed_urls=same_host_urls) + signatures = {"image/png": b"\x89PNG\r\n\x1a\n", "image/jpeg": b"\xff\xd8\xff", "image/gif": b"GIF8"} + if not ((mime in signatures and data.startswith(signatures[mime])) + or (mime == "image/webp" and data[:4] == b"RIFF" and data[8:12] == b"WEBP")): + raise ValueError("unsupported original image") + return {"id": f"reply-image-{index}", "name": f"original-{index}.{mime.split('/')[1]}", + "mime_type": mime, "size": len(data), "data_url": f"data:{mime};base64," + base64.b64encode(data).decode()} + + +def _read_answer_images(source: str, targets: list[str], cached: list | None, + delivery: Mapping[str, Any]) -> list[dict[str, Any]]: + if (delivery.get("started") and not delivery.get("attempt")) or (delivery.get("attempt") and cached is None): + raise ValueError("image delivery acknowledgement or frozen bytes are unavailable") + deadline = time.monotonic() + 15 + allowed_urls = allowed_answer_image_urls(source, targets, deadline=deadline) + if not allowed_urls: + raise ValueError("image was not referenced by the original source") + images = normalize_chat_image_attachments([ + _private_answer_image(url, allowed_urls=allowed_urls, deadline=deadline, index=index) + for index, url in enumerate(targets, 1)]) + # Older queued bytes were fetched before redirect authorization. Keep + # any recorded provider intent tied to the same bytes. + if cached is not None and cached != images: + raise ValueError("unverified cached image bytes changed") + return images + + +def _is_answer_image_followup(record: Mapping[str, Any]) -> bool: + if record.get("status") != "accepted" or not isinstance(record.get("answer_images"), dict): + return False + terminal = record.get("deliveries", {}).get(presentation_phase(record, "terminal"), {}) + return terminal.get("verified") is True + + +def _answer_image_retry_waits(record: Mapping[str, Any], now: float) -> bool: + if not _is_answer_image_followup(record): + return False + state = record["answer_images"] + retry_at = state.get("next_check_at") + return state.get("status") != "delivered" and type(retry_at) in (int, float) and retry_at > now def _unsupported_media_label(row: Mapping[str, Any]) -> str | None: @@ -234,6 +321,7 @@ def __init__(self, *, controller: Any, runtime_root: Path, runner: Any, cli_bin: self.steward_callback_readiness: dict[tuple[str, str], Callable[[], bool]] = {} self.root = controller.store.root / "lark-private-deliveries" self._context_recovery_probe_at: dict[Path, float] = {} + self._historical_image_probe_at: dict[Path, float] = {} self._delivery_discovery = PrivateDeliveryDiscovery(self.root) self._body_discovery = PrivateDeliveryDiscovery(self.root) self._web_projection_scan_at = 0.0 @@ -314,7 +402,9 @@ def observe_completed() -> None: completed.clear() def health_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None: - if row["status"] != "delivered": + # A delivered body may still be waiting for original image recovery. + if (row["status"] != "delivered" + or (row.get("historical_answer_images") or {}).get("status") == "pending"): return None deliveries = row.get("deliveries") if not isinstance(deliveries, dict) or not all( @@ -335,9 +425,12 @@ def delivered_requests() -> Iterator[dict[str, Any]]: self.root, paths, read=_read_json, project=health_fields, ): health.setdefault(row["binding_id"], {"pending_count": 0, "recovery_count": 0}) - if row["status"] != "delivered" and triage.pending(row): + if row["status"] != "delivered": + if triage.pending(row): + count(row) + elif (row.get("historical_answer_images") or {}).get("status") == "pending": count(row) - elif row["status"] == "delivered" and path.parent == self.root and row.get("session_id") and row.get("turn_id"): + elif path.parent == self.root and row.get("session_id") and row.get("turn_id"): yield row # Pair the original receipts with the current Core correlation and Turn. @@ -366,14 +459,13 @@ def _group_source_message(self, profile: str, message_id: str) -> Mapping[str, A return None return _message(payload, message_id) - def _group_source_pair(self, profile: str, message_id: str, - root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]: - # Callers bound these IDs to an admitted topic or a frozen Core stop. - # This is readonly scheduling, not authority; raw mget retains - # body/ancestry/mentions and the current Core selection validates them. - result = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=profile, + def _original_message_pair(self, profile: str, message_id: str, other_id: str, *, + runner=None) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]: + # Read both admitted IDs at once. Callers verify the returned envelopes + # and the current Core selection before using either as authority. + result = call(runner or self.runner, lark_args(cli_bin=self.cli_bin, profile=profile, tail=["api", "GET", "/open-apis/im/v1/messages/mget", "--params", - json.dumps({"message_ids": [message_id, root_id]}), "--as", "bot"])) + json.dumps({"message_ids": [message_id, other_id]}), "--as", "bot"])) payload = json_payload(result) if result.get("returncode") != 0 or payload.get("code", 0) != 0 or payload.get("ok") is False: return None, None @@ -381,10 +473,17 @@ def _group_source_pair(self, profile: str, message_id: str, items = data.get("items") if isinstance(data, Mapping) else None if (not isinstance(items, list) or len(items) != 2 or any(not isinstance(item, Mapping) or not isinstance(item.get("message_id"), str) for item in items) - or {item.get("message_id") for item in items} != {message_id, root_id}): + or {item.get("message_id") for item in items} != {message_id, other_id}): return None, None messages = {item["message_id"]: item for item in items} - return messages[message_id], messages[root_id] + return messages[message_id], messages[other_id] + + def _read_answer_image_source(self, args: list[str], cwd: Path | None, _timeout: float): + return self.runner(args, cwd, ANSWER_IMAGE_SOURCE_TIMEOUT) + + def _group_source_pair(self, profile: str, message_id: str, + root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]: + return self._original_message_pair(profile, message_id, root_id) def _completed_stop_root(self, record: Mapping[str, Any], binding: Mapping[str, Any]) -> str | None: """Read scheduling only; fresh Core resolution still owns authorization.""" @@ -591,18 +690,23 @@ def sources() -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]: pass return None - def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None) -> bool: - return self._source_message(record, render_content=False, sender_identity=sender_identity) is not None + def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None, + expected_text: str | None = None) -> bool: + message = self._source_message(record, render_content=False, sender_identity=sender_identity) + return message is not None and (expected_text is None or ( + message.get("deleted") is not True and message.get("msg_type", message.get("message_type")) == "text" + and private_source_text(message) == expected_text)) def _source_authority(self, record: dict[str, Any], profile: str, chat_id: str, - message_id: str) -> tuple[str | None, bool]: + message_id: str, *, expected_text: str | None = None) -> tuple[str | None, bool]: """Verify one exact outbound operation; retain no identity or grant.""" if (profile != record["profile"] or chat_id != record["event"]["chat_id"] or message_id != record["event"]["message_id"]): return None, False identity: dict[str, str] = {} record_event_timing(profile, message_id, "outbound_source_check_started") - verified = self._source_verified(record, sender_identity=identity) + verified = self._source_verified(record, sender_identity=identity, + **({"expected_text": expected_text} if expected_text is not None else {})) record_event_timing(profile, message_id, "outbound_source_check_returned") return identity.get("bot_display_name"), verified @@ -1147,7 +1251,8 @@ def _feedback(self, path: Path, record: dict[str, Any], *, inbox: Callable[[], P def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, inbox: Callable[[], Path], edit_only: bool = False, authorization_check: Callable[[], bool] | None = None, reply_placement: str | None = None, delivery_id: str | None = None, - finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None) -> bool: + finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None, + historical_source_check: Callable[[], bool] | None = None) -> bool: if not text: return False if reply_placement is not None and (record["event"].get("chat_type") != "p2p" @@ -1156,7 +1261,10 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, if authorization_check is not None and not authorization_check(): return False kind = phase.split(".", 1)[0] + 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 phase_state = record["deliveries"].get(phase, {}) if phase_state.get("verified"): return True @@ -1167,26 +1275,52 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, text = _presentation_text(text) if phase_state.get("text") not in (None, text): raise ValueError("delivery content changed after an attempt") - if image_attachments and (kind != "web" or reply_placement != "chat_root" or edit_only): - raise ValueError("App image projection requires its original private outbox") + if image_attachments and (authorization_check is None or edit_only + or not ((kind == "web" and reply_placement == "chat_root") + or (kind == "answer_images" and record["event"].get("chat_type") == "p2p" + and record["source"].get("private_human_message") is True))): + raise ValueError("Image projection requires its original private outbox") record_event_timing(record["profile"], record["event"]["message_id"], "delivery_started") config = inbox() record_event_timing(record["profile"], record["event"]["message_id"], "delivery_inbox_ready") - def image_authorized() -> bool: - if authorization_check is None or not authorization_check(): - return False + def provider_images_current() -> bool: + if historical_source_check is not None: + return historical_source_check() + source = record["event"] + ref = source["message_id"] + args = lark_args(cli_bin=self.cli_bin, profile=record["profile"], + tail=["api", "GET", f"/open-apis/im/v1/messages/{ref}", "--as", "bot"]) + reply = call(self._read_answer_image_source, args) + payload = json_payload(reply) + return (reply.get("returncode") == 0 and payload.get("code", 0) == 0 and payload.get("ok") is not False + and answer_image_source_matches(_message(payload, ref), source)) + + 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"]) + record["event"]["chat_id"], record["event"]["message_id"], expected_text=expected_text) return bool(name) and verified + def image_authorized() -> bool: + # A provider read can overlap Stop, Close, or an edit to the old reply. + return authorization_check is not None and (provider_images_current() and authorization_check() + if kind == "answer_images" else authorization_check() and verified_source()) + image_keys = () if image_attachments: + resource_scope = (f"{record['binding_id']}:{record['request_ref']}:{phase}" + if kind == "answer_images" else record["binding_id"]) image_keys = uploaded_chat_images(root=self.runtime_root, profile=record["profile"], - provider_ref=record["binding_id"], attachments=image_attachments, private_transport=self, - before_upload=image_authorized, verify_only=bool(phase_state.get("started") or phase_state.get("attempt"))) + provider_ref=resource_scope, attachments=image_attachments, private_transport=self, + before_upload=image_authorized, dispatch_source_check=provider_images_current, + verify_only=bool(phase_state.get("started") or phase_state.get("attempt")), + dispatch_guard=partial(answer_media_dispatch, self.core, record) if kind == "answer_images" else None) if phase_state.get("image_keys", list(image_keys)) != list(image_keys): raise ValueError("App image delivery resources changed after an attempt") phase_state["image_keys"] = list(image_keys) + record["deliveries"][phase] = phase_state + _atomic_write_json(path, record) kwargs = dict(project=self.runtime_root, config_path=config, message_id=record["event"]["message_id"], text=text, runner=self._reply_runner, **({"image_keys": image_keys} if image_keys else {}), @@ -1194,7 +1328,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) + self._source_authority(record, profile, chat, message, expected_text=expected_text) if authorization_check is None or authorization_check() else (None, False))) progress = record["deliveries"].get(presentation_phase(record, "progress")) or {} stream = presentation_stream(record) @@ -1211,13 +1345,10 @@ def image_authorized() -> bool: raise ValueError("a new private message cannot replace an existing source reply") def before_send(_intent: str) -> dict[str, bool]: - # Provider preview is external IO. Recheck the original source and - # current Core grant afterwards, including every resumed rich part. + # Recheck source and grant after preview, including resumed parts. if authorization_check is not None and not authorization_check(): return {"continue_delivery": False} - name, verified = self._source_authority(record, record["profile"], - record["event"]["chat_id"], record["event"]["message_id"]) - if not name or not verified or (authorization_check is not None and not authorization_check()): + if not verified_source() or (authorization_check is not None and not authorization_check()): return {"continue_delivery": False} phase_state.update(text=text, started=True) record["deliveries"][phase] = phase_state @@ -1246,7 +1377,7 @@ def edit_attempt(value: Any) -> None: phase_state.pop(key, None) record["deliveries"][phase] = phase_state _atomic_write_json(path, record) - return self._deliver(path, record, phase, text, inbox=inbox) + return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source) if not edit_only and result.get("blocker") in {"provider_update_too_large", "provider_update_format_unsupported"}: # Do not truncate long/mention-bearing final answers to fit a # draft. Close it, then use the established full final sender. @@ -1255,7 +1386,7 @@ def edit_attempt(value: Any) -> None: phase_state.pop("edit_from", None) record["deliveries"][phase] = phase_state _atomic_write_json(path, record) - return self._deliver(path, record, phase, text, inbox=inbox) + return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source) elif phase_state.get("attempt"): result = verify_lark_inbox_reply(**kwargs, attempt=phase_state["attempt"], resume_parts=True, before_send=before_send) @@ -1297,8 +1428,224 @@ def attempt(value: Any) -> None: record_event_timing(record["profile"], record["event"]["message_id"], "delivery_returned") return bool(phase_state["verified"]) + def _deliver_completed_answer(self, path: Path, record: dict[str, Any], text: str, *, + inbox: Callable[[], Path]) -> bool: + phase = presentation_phase(record, "terminal") + media_phase = "answer_images." + phase + old = record["deliveries"].get(phase) or {} + if old.get("started") and not record.get("answer_images"): + return self._deliver(path, record, "terminal", text, inbox=inbox) + if record["event"].get("chat_type") != "p2p" or record["source"].get("private_human_message") is not True: + return self._deliver(path, record, "terminal", text, inbox=inbox) + body, targets = _private_answer_images(text) + if not targets: + return self._deliver(path, record, "terminal", text, inbox=inbox) + + def authorized() -> bool: + native = self.core.read_request(record["request_ref"]) + same_request = all(native.get(key) == record[key] for key in ("binding_id", "source", "session_id", "turn_id")) + if native.get("status") != "accepted" or not same_request: + return False + turn = self.core.controller.store.load_turn(native["session_id"], native["turn_id"]) + return resumable_answer_image_session(self.core, native, turn) is not None + + state = record.setdefault("answer_images", {}) + # Hold the source and the exact completed body fixed across retries. + digest = hashlib.sha256(text.encode()).hexdigest() + if state.setdefault("answer_sha256", digest) != digest: + return False + if old.get("verified") is not True: + state.setdefault("status", "pending") + _atomic_write_json(path, record) + self._deliver(path, record, "terminal", body + "\n\n原图会另发;有链接的图名可打开原始来源。", + inbox=inbox, finalize_source=False) + # Release the body worker before the pending image work. + return False + if state.get("status") == "delivered" and record["deliveries"].get(media_phase, {}).get("images_verified") is True: + return True + now = datetime.now(timezone.utc).timestamp() + if _answer_image_retry_waits(record, now): + return False + state.update(status="pending", next_check_at=now + ANSWER_IMAGE_RETRY_SECONDS) + _atomic_write_json(path, record) + try: + event = record["event"] + origin = event.get("content") + if (len(targets) > CHAT_IMAGE_MAX_COUNT or event.get("message_type") != "text" + or not isinstance(origin, str) or not authorized() or not self._source_authority( + record, record["profile"], event["chat_id"], event["message_id"], expected_text=origin)[1]): + raise ValueError("answer image source unavailable or not authorized") + images = state.get("attachments") + if images is None or state.get("source_urls_verified") is not True: + images = state["attachments"] = _read_answer_images(origin, targets, images, + record["deliveries"].get(media_phase, {})) + state["source_urls_verified"] = True + _atomic_write_json(path, record) + if self._deliver(path, record, media_phase, "原图(按正文顺序)", inbox=inbox, + authorization_check=authorized, finalize_source=False, + image_attachments=images): + if self.reaction_feedback and not _reconcile_outbound_input_reactions( + project=self.runtime_root, config_path=inbox(), message_id=record["event"]["message_id"], + processing=False, complete=True, before_write=authorized, runner=self._reply_runner).get("ok"): + return False + state.update(status="delivered") + state.pop("attachments", None) + _atomic_write_json(path, record) + return True + except (KeyError, ValueError, OSError, http.client.HTTPException): + pass + state["status"] = "unverified" + _atomic_write_json(path, record) + self._deliver(path, record, "answer_image_notice." + phase, + "正文已送达,图片尚未核验送达。有链接的图名可打开原始来源。", + inbox=inbox, authorization_check=authorized, finalize_source=False) + return False + + def _historical_image_source(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict: + event = record["event"] + terminal = record["deliveries"]["terminal"] + if (record["status"] != "delivered" or record.get("context_recovery") + or event.get("chat_type") != "p2p" or event.get("message_type") != "text" + or record["source"].get("private_human_message") is not True + or record["binding_id"] != plan["binding_id"] or record["turn_id"] != plan["turn_id"] + or terminal.get("verified") is not True or terminal.get("image_keys") + or terminal.get("attempt", {}).get("message_ref") != plan["message_id"]): + raise ValueError("original text-only reply unavailable") + native = self.core.read_request(record["request_ref"]) + if (native.get("status") != "accepted" or native.get("origin") != "lark" + or native.get("command") is not None or native.get("agent_target") + or native.get("delivery_verified") is not True + or any(native.get(key) != record.get(key) + 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) + 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") + 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"}: + 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 re.fullmatch(r"[a-f0-9]{64}", plan["answer_sha256"]) + or not isinstance(plan["images"], list) or not 1 <= len(plan["images"]) <= 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 re.fullmatch(r"[a-f0-9]{64}", 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") + self._historical_image_source(record, plan) + return {**plan, "images": images} + + def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dict | None: + if record.get("historical_answer_images") is not None: + return record["historical_answer_images"] + if not re.fullmatch(r"[a-f0-9]{24}", path.stem): + raise ValueError("invalid original request reference") + # This is a private local operator input, scoped to the existing receipt. + # It is never loaded from a project, provider message or model response. + spec = self.root / "historical-answer-images" / path.name + try: + with spec.open("rb") as handle: + raw = handle.read(40_001) + except FileNotFoundError: + return None + if len(raw) > 40_000: + raise ValueError("original image repair descriptor exceeds its size limit") + plan = json.loads(raw) + if not isinstance(plan, dict) or path.stem != record.get("request_ref"): + raise ValueError("invalid original image repair descriptor") + plan = self._historical_image_plan(record, plan) + state = record["historical_answer_images"] = {"plan": plan, "status": "pending"} + _atomic_write_json(path, record) + return state + + def _reconcile_historical_answer_images(self, path: Path) -> int: + with exclusive_file_lock(path, operation="deliver_private_chat_request"): + record = _read_json(path) + try: + state = self._load_historical_image_plan(path, record) + except (KeyError, ValueError, OSError): + return 0 + now = datetime.now(timezone.utc).timestamp() + if not state or state.get("status") != "pending" or state.get("next_check_at", 0) > now: + return 0 + state["next_check_at"] = now + ANSWER_IMAGE_RETRY_SECONDS + _atomic_write_json(path, record) + plan = state["plan"] + try: + session = self._historical_image_source(record, plan) + selected = self.bindings.resolve(binding_id=plan["binding_id"], session=session, + session_context=session.get("project_context") or session.get("steward_context"), + include_observation=True, **record["source"]) + event = record["event"] + observation = selected["observation"] + if selected["binding"]["transport_ref"] != record["profile"]: + return 0 + original_text = normalize_lark_outbound_text( + record["deliveries"]["terminal"]["text"], limit=None, preserve_format=True) + def original_pair_matches() -> bool: + source, reply = self._original_message_pair(record["profile"], event["message_id"], plan["message_id"], + runner=self._read_answer_image_source) + return answer_image_source_matches(source, event) and historical_image_reply_matches( + reply, event["chat_id"], observation, original_text) + + if not original_pair_matches(): + return 0 + phase = "answer_images.historical" + attachments = state.get("attachments") + recheck_sources = attachments is None or state.get("source_urls_verified") is not True + if recheck_sources: + attachments = _read_answer_images(event["content"], [item["url"] for item in plan["images"]], + attachments, record["deliveries"].get(phase, {})) + 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"]]: + return 0 + if recheck_sources: + state.update(attachments=attachments, source_urls_verified=True) + _atomic_write_json(path, record) + def authorized() -> bool: + try: + return (selected["binding"] in self.bindings.read()["bindings"] + and bool(self._historical_image_source(record, plan))) + except (KeyError, OSError, ValueError): + return False + + if not self._deliver(path, record, phase, "原图(按原文顺序)", + inbox=lambda: self._inbox(record, observation=observation), + authorization_check=authorized, historical_source_check=original_pair_matches, + finalize_source=False, + reply_placement="chat_root", image_attachments=attachments): + return 0 + media = record["deliveries"][phase] + output = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=record["profile"], tail=[ + "im", "+messages-mget", "--message-ids", media["attempt"]["message_ref"], + "--as", "bot", "--no-reactions", "--format", "json"])) + sent = _message(json_payload(output), media["attempt"]["message_ref"]) if output.get("returncode") == 0 else None + if not historical_image_reply_matches(sent, event["chat_id"], observation, media["text"], tuple(media["image_keys"])): + return 0 + except (KeyError, ValueError, OSError, http.client.HTTPException): + return 0 + state.update(status="delivered") + state.pop("attachments", None) + _atomic_write_json(path, record) + return 1 + def _edit_progress(self, path: Path, record: dict[str, Any], text: str, *, inbox: Callable[[], Path], finalizing: bool = False) -> bool: - text = _presentation_text(text) + text = _presentation_text(_private_answer_images(text)[0] if record["event"].get("chat_type") == "p2p" else text) progress = record["deliveries"].get(presentation_phase(record, "progress")) or {} if not progress.get("verified"): return self._deliver(path, record, "progress", text, inbox=inbox) @@ -1476,7 +1823,7 @@ def inbox() -> Path: def pending_body_paths(self) -> list[Path]: """Find unsent bodies without waiting for historical recovery probes.""" paths = [path for path, row, _ in self._body_discovery.rows() - if row["status"] != "delivered" and triage.pending(row)] + if row["status"] != "delivered" and triage.pending(row) and not _is_answer_image_followup(row)] return [*paths, *self._pending_web_projection_paths()] @effect_runtime_request_scope() @@ -1484,8 +1831,16 @@ def pending_delivery_paths(self) -> list[Path]: """The durable transport store is the queue; no in-memory admission.""" paths = [] recovery_candidate = self.core.context_recovery_observer() + now = datetime.now(timezone.utc).timestamp() + planned_repairs = {item.name for item in (self.root / "historical-answer-images").glob("*.json")} for path, row, repairs in self._delivery_discovery.rows(): - if row["status"] != "delivered" and triage.pending(row): + if row["status"] != "delivered": + if triage.pending(row) and not _answer_image_retry_waits(row, now): + paths.append(path) + elif row.get("historical_images_due_at") is not None and row["historical_images_due_at"] <= now: + paths.append(path) + elif (not row.get("historical_images_admitted") and path.name in planned_repairs + and time.monotonic() >= self._historical_image_probe_at.get(path, 0)): paths.append(path) elif (row.get("session_id") and row.get("turn_id") and time.monotonic() >= self._context_recovery_probe_at.get(path, 0)): @@ -1503,7 +1858,10 @@ def is_recovery_path(self, path: Path) -> bool: A verified old notice may need a native recovery probe, but must not precede an unsent body just because both have zero worker attempts. """ - return path.parent != self.root / "web-projections" and _read_json(path).get("status") == "delivered" + if path.parent == self.root / "web-projections": + return False + row = _read_json(path) + return row.get("status") == "delivered" or _is_answer_image_followup(row) def _pending_web_projection_paths(self) -> list[Path]: paths = [] @@ -1939,6 +2297,15 @@ def reconcile_request(self, path: Path) -> int: row = _read_json(path) if "triage" in row: return triage.reconcile(self, path) + historical_images = row.get("historical_answer_images") + if (row.get("status") == "delivered" and ( + (historical_images or {}).get("status") == "pending" + or (historical_images is None and (self.root / "historical-answer-images" / path.name).is_file()))): + if historical_images is None: + self._historical_image_probe_at[path] = time.monotonic() + 60 + return self._reconcile_historical_answer_images(path) + if _answer_image_retry_waits(row, datetime.now(timezone.utc).timestamp()): + return 0 record_event_timing(row["profile"], row["event"]["message_id"], "reconciliation_started") if row["status"] == "delivered" and row.get("session_id") and row.get("turn_id"): # Discovery may find more candidates than the shared pool can run. @@ -2035,6 +2402,7 @@ def reconcile_request(self, path: Path) -> int: config: Path | None = None selected: dict[str, Any] | None = None control_receipt_first = False + completed_answer = False def inbox() -> Path: nonlocal config @@ -2118,6 +2486,7 @@ def inbox() -> Path: "本次执行超时,原会话已保留;请发送 /status 查看状态后再决定是否重试。" if turn["status"] == "timed_out" else "本次未能完成,原会话已保留。") if turn["status"] == "completed": + completed_answer = True response = self.core.todo_confirmation_reply(record["request_ref"], turn.get("response") or {}) else: if record["status"] != "rejected" and not control_receipt_first: @@ -2126,7 +2495,8 @@ def inbox() -> Path: or _command_text(str(record.get("response_code") or "")) or str(record.get("response") or "")) if record.get("status_snapshot"): response = _status_text(record["status_snapshot"], help_requested=native.get("command") == "help") - if self._deliver(path, record, "terminal", response, inbox=inbox): + if (self._deliver_completed_answer(path, record, response, inbox=inbox) if completed_answer else + self._deliver(path, record, "terminal", response, inbox=inbox)): if control_receipt_first: # The verified result is the useful control receipt. # Preserve the usual idempotent reaction, after it. diff --git a/loopx/extensions/lark/private_delivery_discovery.py b/loopx/extensions/lark/private_delivery_discovery.py index 68f14e5bf9..67d54f423e 100644 --- a/loopx/extensions/lark/private_delivery_discovery.py +++ b/loopx/extensions/lark/private_delivery_discovery.py @@ -1,14 +1,135 @@ -"""Read-only scheduling hints for the existing private-message reply worker.""" +"""Read-only source and scheduling checks for private-message delivery.""" from __future__ import annotations -from collections.abc import Iterator +import json +import re +from collections.abc import Callable, Iterator, Mapping +from contextlib import ExitStack, contextmanager from pathlib import Path -from typing import Any, cast +from typing import TYPE_CHECKING, Any, cast from ...chat_store import RESUMABLE_SESSION_STATES, ChatSessionStore, _read_json -from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint +from ...capabilities.native_chat.file_discovery import FileChatDiscovery, external_source_route_path, file_change_fingerprint +from ...file_lock import try_exclusive_file_lock +from .presentation.markdown_post import lark_markdown_readback_matches +from .private_ingress import private_source_text from .private_progress import legacy_recovery_repairs +if TYPE_CHECKING: + from ...capabilities.native_chat.external_conversations import ChatExternalConversations + + +_SOURCE_URL = re.compile(r"https://[^\s<>()\[\]\",。;!?]+") + + +def allowed_answer_image_urls(source: str, targets: list[str], *, deadline: float) -> set[str]: + """Authorize exact owner URLs and images listed by the original same-host article.""" + from urllib.parse import urlsplit + + from ..public_source_reader import article, public_url + + def candidate(url: str) -> str | None: + try: + return public_url(url) if not urlsplit(url).query else None + except ValueError: + return None + + owner_links = {url for link in _SOURCE_URL.findall(source) if (url := candidate(link))} + wanted = {candidate(target) for target in targets} + if None in wanted or not owner_links: + return set() + if wanted <= owner_links: + return owner_links + if len(owner_links) != 1: + return set() + [original] = owner_links + host = urlsplit(original).hostname + if any(urlsplit(url).hostname != host for url in wanted - owner_links): + return set() + observed = article(original, deadline=deadline, allowed_urls={original}) + if observed.get("ok") is not True: + return set() + listed = {url for item in observed["images"] + if (url := candidate(item["url"])) and urlsplit(url).hostname == host} + allowed_urls = owner_links | listed + return allowed_urls if wanted <= allowed_urls else set() + + +def answer_image_source_matches(message: Mapping | None, event: Mapping) -> bool: + if message is None or message.get("deleted") is True or message.get("chat_id") != event["chat_id"]: + return False + sender = message.get("sender") + return (isinstance(sender, Mapping) and sender.get("sender_type") == "user" + and bool(event["sender_id"]) and sender.get("id") == event["sender_id"] + and message.get("msg_type", message.get("message_type")) == "text" + and private_source_text(message) == event["content"]) + + +def historical_image_reply_matches(message: Mapping | None, chat_id: str, observation: Mapping, + text: str, keys: tuple[str, ...] = ()) -> bool: + if message is None or message.get("deleted") is True or message.get("chat_id") != chat_id: + return False + sender = message.get("sender") + if (not isinstance(sender, Mapping) or sender.get("sender_type") != "app" + or not sender.get("id") or sender["id"] not in (observation["bot_app_id"], observation["bot_open_id"])): + return False + raw_body = message.get("body") + if isinstance(raw_body, Mapping) and isinstance(raw_body.get("content"), str): + try: + body = json.loads(raw_body["content"]) + except ValueError: + return False + # Raw mget keeps authored Markdown in content_v2 and a rendered preview + # in content. Match the authored body through the existing wire verifier. + if isinstance(body, Mapping) and "content_v2" in body: + locale = {"title": body.get("title", ""), "content": body["content_v2"]} + message = {**message, "body": {"content": {"zh_cn": locale}}} + return lark_markdown_readback_matches(text=text, message=message, image_keys=keys) + + +def resumable_answer_image_session( + core: ChatExternalConversations, request: dict[str, Any], turn: dict[str, Any] | None +) -> dict[str, Any] | None: + """Apply source controls before reading the current Session.""" + store = core.controller.store + session_id, turn_id = request["session_id"], request["turn_id"] + if (turn is None or turn.get("status") != "completed" + or core._source_stop_observed(request, turn, store.events_after(session_id, turn_id, None))): + return None + session = store.load_session(session_id) + return session if session is not None and session.get("status") in RESUMABLE_SESSION_STATES else None + + +@contextmanager +def answer_media_dispatch(core: ChatExternalConversations, request: dict[str, Any], + source_is_current: Callable[[], bool]) -> Iterator[None]: + """Check the provider source at dispatch under the original local controls.""" + store = core.controller.store + session_id, turn_id = request["session_id"], request["turn_id"] + route = external_source_route_path(core.root, request["binding_id"], request["source"]["source_ref"]) + # Ingress takes the route before its binding and Session operations. + # Contention leaves the original control writer free to finish first. + with ExitStack() as fences: + for path in (route, core.bindings.path, store._session_path(session_id)): + if fences.enter_context(try_exclusive_file_lock(path, operation="dispatch_external_answer_media")) is None: + raise ValueError("original media authority is being updated") + if not any(binding.get("enabled") is True and binding["binding_id"] == request["binding_id"] + and binding["transport_ref"] == request["profile"] for binding in core.bindings.read()["bindings"]): + raise ValueError("original media binding unavailable") + native = core.read_request(request["request_ref"]) + if (native.get("status") != "accepted" + or any(native.get(key) != request[key] for key in ("binding_id", "source", "session_id", "turn_id")) + or resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None): + raise ValueError("original Session or source no longer authorizes an upload") + # Feishu has no conditional image upload tied to a message revision. + # Do the final provider read here, with these local locks held through + # CLI start. Edits made later are checked again before posting the images. + if not source_is_current(): + raise ValueError("original upload source changed") + if resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None: + raise ValueError("original Session or source changed during upload authorization") + yield + def prepared_steward_binding_waits( request: dict[str, Any], bindings: list[dict[str, Any]], store: ChatSessionStore @@ -85,6 +206,11 @@ def _observe(self, path: Path) -> tuple[dict[str, Any], bool]: ("status", "request_ref", "binding_id", "session_id", "turn_id")} view.update(agent_target=bool(row.get("agent_target")), event={"chat_type": row.get("event", {}).get("chat_type")}) + historic_images = row.get("historical_answer_images") or {} + if historic_images: + view["historical_images_admitted"] = True + if historic_images.get("status") == "pending": + view["historical_images_due_at"] = historic_images.get("next_check_at", 0) if before is not None and file_change_fingerprint(path) == before: self._completed[path] = (before, view, repairs) return view, repairs diff --git a/loopx/extensions/lark/return_files.py b/loopx/extensions/lark/return_files.py index e503900a57..d5f652ace6 100644 --- a/loopx/extensions/lark/return_files.py +++ b/loopx/extensions/lark/return_files.py @@ -2,14 +2,18 @@ from __future__ import annotations -import hashlib import base64 +import errno +import hashlib import json import re import subprocess +from contextlib import ExitStack, contextmanager +from functools import partial from pathlib import Path from tempfile import TemporaryDirectory +from .cli_resolution import run_lark_command_with_dispatch_guard from .inbox_reply import _default_runner, _json_object, _message from .outbound import normalize_lark_outbound_text from .presentation.markdown_post import lark_markdown_readback_attachment_keys, lark_markdown_readback_matches @@ -24,12 +28,31 @@ # shapes are stated once here instead of being rebuilt at each use site. RESULT_FILE_KEY_PATTERN = re.compile(r"file_[A-Za-z0-9_-]{1,240}") CHAT_IMAGE_KEY_PATTERN = re.compile(r"img_[A-Za-z0-9_-]{1,240}") +LARK_APP_SCOPE_NOT_APPLIED = 99991672 + +def _image_upload_scope_rejected(result): + """Recognize the CLI's explicit App rejection before resource creation.""" + if result.get("returncode") != 3 or str(result.get("stdout") or "").strip(): + return False + rejected = _json_object(result.get("stderr")) + error = rejected.get("error") + return ( + rejected.get("ok") is False and rejected.get("identity") == "bot" + and isinstance(error, dict) and error.get("identity") == "bot" + and error.get("type") == "authorization" + and error.get("subtype") == "app_scope_not_applied" + and error.get("code") == LARK_APP_SCOPE_NOT_APPLIED + ) -def _call(args, cwd, *, runner, private_transport): + +def _call(args, cwd, *, runner, private_transport, dispatch_guard=None): try: if private_transport is not None: - return private_transport.runner([private_transport.cli_bin, *args[1:]], cwd, 30) + command = [private_transport.cli_bin, *args[1:]] + if dispatch_guard is not None: + return run_lark_command_with_dispatch_guard(private_transport.runner, command, cwd, 30, dispatch_guard) + return private_transport.runner(command, cwd, 30) return runner(args) if runner is not None else _default_runner(args, cwd=cwd) except (OSError, subprocess.SubprocessError): return {"returncode": 1} @@ -59,20 +82,56 @@ def _chat_images(attachments): def uploaded_chat_images(*, root, profile, provider_ref, attachments, private_transport, - before_upload, verify_only=False): + before_upload, verify_only=False, dispatch_guard=None, dispatch_source_check=None): """Reuse resource IO; never regenerate an unknown upload or sent intent.""" + if dispatch_guard is not None and dispatch_source_check is None: + raise ValueError("guarded image upload requires current source verification") return _uploaded_resources(root=root, profile=profile, provider_ref=provider_ref, attachments=_chat_images(attachments), cache_dir="lark-chat-images", resource="images", key_field="image_key", key_pattern=CHAT_IMAGE_KEY_PATTERN, read_bytes=lambda image: image["raw"], upload_data=lambda _image: {"image_type": "message"}, runner=None, private_transport=private_transport, before_upload=before_upload, verify_only=verify_only, - fence_upload=True) + fence_upload=True, dispatch_guard=dispatch_guard, dispatch_source_check=dispatch_source_check) + + +def _restore_unstarted_upload(cache, saved): + if saved is None: + cache.unlink() + else: + _write(cache, saved) + + +@contextmanager +def _authorize_upload_dispatch(guard, source_check, cache, saved, *, guards_spawn): + unstarted = False + try: + with ExitStack() as authority: + try: + authority.enter_context(guard(source_check)) + except (KeyError, OSError, ValueError) as exc: + unstarted = True + raise ValueError("original upload authority unavailable") from exc + try: + yield + except OSError as exc: + # The native guard surrounds Popen only. EMFILE during pipe + # setup or exec leaves no CLI running. A synchronous runner + # can instead raise the same error after a provider write. + unstarted = guards_spawn and exc.errno in ( + errno.ENOENT, errno.ENOTDIR, errno.EACCES, errno.EPERM, errno.ENOEXEC, errno.EMFILE, + ) + raise + finally: + # The resource lock is still held. Release the authority handles + # first so restoring a previous rejection can open its temp file. + if unstarted: + _restore_unstarted_upload(cache, saved) def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir, resource, key_field, key_pattern, read_bytes, upload_data, runner, private_transport, - before_upload, verify_only, fence_upload=False): + before_upload, verify_only, fence_upload=False, dispatch_guard=None, dispatch_source_check=None): keys = [] for attachment in attachments: identity = {"profile": profile, "provider_ref": provider_ref, @@ -81,10 +140,14 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir, with exclusive_file_lock(cache): saved = _read(cache) if cache.exists() else None if saved is not None: - if saved.get("identity") != identity or not key_pattern.fullmatch(str(saved.get(key_field, ""))): + if saved.get("identity") != identity: + raise ValueError("resource upload identity conflict or acknowledgement unavailable") + if key_pattern.fullmatch(str(saved.get(key_field, ""))): + keys.append(saved[key_field]) + continue + # Prior timeouts and missing acknowledgements remain fenced. + if not (fence_upload and saved == {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED}): raise ValueError("resource upload identity conflict or acknowledgement unavailable") - keys.append(saved[key_field]) - continue if verify_only: raise ValueError("sent result file resource record unavailable") raw = read_bytes(attachment) @@ -103,9 +166,20 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir, raise ValueError("result file return authority revoked") if fence_upload: _write(cache, {"identity": identity, "upload_started": True}) - uploaded = _call(args, cwd, runner=runner, private_transport=private_transport) + guard = None + if dispatch_guard is not None: + guard = partial( + _authorize_upload_dispatch, dispatch_guard, dispatch_source_check, cache, saved, + guards_spawn=getattr(private_transport.runner, "dispatch_guard", False) is True, + ) + uploaded = _call(args, cwd, runner=runner, private_transport=private_transport, + dispatch_guard=guard) + if fence_upload and _image_upload_scope_rejected(uploaded): + _write(cache, {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED}) + raise ValueError("result file upload unavailable") payload = _json_object(uploaded.get("stdout")) - key = (payload.get("data") or {}).get(key_field) + data = payload.get("data") + key = data.get(key_field) if isinstance(data, dict) else None if uploaded.get("returncode") != 0 or payload.get("ok") is False or not key_pattern.fullmatch(str(key or "")): raise ValueError("result file upload unavailable") _write(cache, {"identity": identity, key_field: key}) diff --git a/loopx/extensions/public_source_reader.py b/loopx/extensions/public_source_reader.py index 8ee09b60fb..beb90b44bd 100644 --- a/loopx/extensions/public_source_reader.py +++ b/loopx/extensions/public_source_reader.py @@ -21,6 +21,7 @@ import sys import threading import time +from collections.abc import Collection from html.parser import HTMLParser from urllib.parse import urljoin, urlsplit, urlunsplit @@ -101,11 +102,15 @@ def connect(self) -> None: raise -def fetch(value: str, *, deadline: float) -> tuple[str, str, bytes]: +def fetch(value: str, *, deadline: float, + allowed_urls: Collection[str] | None = None) -> tuple[str, str, bytes]: + """Fetch anonymously, checking any caller's exact public URLs before DNS.""" url = public_url(value) for _ in range(6): if time.monotonic() >= deadline: raise TimeoutError("source timeout") + if allowed_urls is not None and url not in allowed_urls: + raise ValueError("public source URL not authorized") u = urlsplit(url) addresses = public_addresses(u.hostname or "") for index, address in enumerate(addresses): @@ -236,9 +241,10 @@ def _text_page_parameters(offset: int, expected_sha256: str | None) -> None: def article(url: str, *, deadline: float, offset: int = 0, - expected_sha256: str | None = None) -> dict[str, object]: + expected_sha256: str | None = None, + allowed_urls: Collection[str] | None = None) -> dict[str, object]: _text_page_parameters(offset, expected_sha256) - observed, mime, body = fetch(url, deadline=deadline) + observed, mime, body = fetch(url, deadline=deadline, allowed_urls=allowed_urls) digest = hashlib.sha256(body).hexdigest() if expected_sha256 is not None and digest != expected_sha256: return {"ok": False, "error": "public_source_changed"} diff --git a/tests/extensions/test_lark_cli_resolution.py b/tests/extensions/test_lark_cli_resolution.py index 85ef88ef55..0eef3e3b45 100644 --- a/tests/extensions/test_lark_cli_resolution.py +++ b/tests/extensions/test_lark_cli_resolution.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +from contextlib import contextmanager from pathlib import Path from typing import Any @@ -9,6 +10,7 @@ LarkCliResolution, build_lark_command_runner, resolve_lark_cli, + run_lark_command_with_dispatch_guard, ) @@ -192,6 +194,53 @@ def fake_run(args: list[str], **kwargs: Any) -> Any: ] +def test_guarded_cli_spawns_inside_authority_and_waits_outside(tmp_path, monkeypatch): + import subprocess + import sys + + real_popen = subprocess.Popen + states = [] + held = False + + @contextmanager + def guard(): + nonlocal held + held = True + try: + yield + finally: + held = False + + class ObservedProcess: + def __init__(self, *args, **kwargs): + states.append(("spawn", held)) + self.process = real_popen(*args, **kwargs) + + def __enter__(self): + self.process.__enter__() + return self + + def __exit__(self, *args): + return self.process.__exit__(*args) + + @property + def returncode(self): + return self.process.returncode + + def communicate(self, *, timeout): + states.append(("wait", held)) + return self.process.communicate(timeout=timeout) + + monkeypatch.setattr(subprocess, "Popen", ObservedProcess) + resolution = LarkCliResolution(sys.executable, True, "fixture", None, None) + runner = build_lark_command_runner(resolution) + result = run_lark_command_with_dispatch_guard( + runner, [sys.executable, "-c", "print('result')"], tmp_path, 5, guard, + ) + assert result["returncode"] == 0 and result["stdout"] == "result\n" + assert states == [("spawn", True), ("wait", False)] + + def test_resolver_source_preserves_public_boundary_contract() -> None: source = Path(__file__).resolve().parents[2] / "loopx/extensions/lark/cli_resolution.py" diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index 64de5fa518..1c9d0c59bf 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -7,6 +7,7 @@ import threading from concurrent.futures import ThreadPoolExecutor from datetime import datetime +from io import BytesIO from types import SimpleNamespace import pytest @@ -1016,6 +1017,679 @@ def connect(fixture): runner=provider, cli_bin="lark-cli") +@pytest.fixture +def source_https(monkeypatch): + from loopx.extensions import public_source_reader + + requests, resolutions = [], [] + + def resolve(host): + resolutions.append(host) + return ["8.8.8.8"] + + def serve(response_for): + class Socket: + def settimeout(self, value): + pass + + def makefile(self, *args): + return BytesIO(self.wire_response) + + class Connection: + def __init__(self, host, address, timeout): + self.host = host + self.sock = Socket() + + def request(self, method, path, *, headers): + assert method == "GET" + assert set(headers) == {"Accept-Encoding", "User-Agent"} + url = f"https://{self.host}{path}" + requests.append(url) + redirect, mime, body = response_for(url) + if redirect: + self.sock.wire_response = f"HTTP/1.1 302 Found\r\nLocation: {redirect}\r\nContent-Length: 0\r\n\r\n".encode() + else: + headers = f"HTTP/1.1 200 OK\r\nContent-Type: {mime}\r\nContent-Length: {len(body)}\r\n\r\n" + self.sock.wire_response = headers.encode() + body + + def getresponse(self): + response = http.client.HTTPResponse(self.sock) + response.begin() + return response + + def close(self): + pass + + monkeypatch.setattr(public_source_reader, "public_addresses", resolve) + monkeypatch.setattr(public_source_reader, "_PinnedHTTPS", Connection) + return requests, resolutions + + return serve + + +@pytest.mark.parametrize("fault", ["none", "foreign_source", "unlisted_image", "changed_request", + "corrupt_readback", "legacy_cached_readback", "upload_denied", "listed_redirect", "same_host_redirect", + "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect", "stopped", "stopped_after_fetch", + "closed_on_upload_preview", "stopped_on_upload_source_read", "closed_on_upload_source_read", + "closed_on_source_controls_read", "stopped_before_upload_intent", "closed_before_upload_intent", + "edited_after_upload_intent"]) +def test_private_completed_answer_returns_image_bytes_instead_of_a_remote_placeholder(ordinary, source_https, monkeypatch, fault): # noqa: F811 + from test_chat_image_attachments import PNG_BYTES + from test_lark_app_image_projection import ImageIO + + store, runtime, provider, transport = connect(ordinary) + capture, fake = ordinary[4:6] + url = ("https://unrelated.example" if fault == "foreign_source" else "https://source.example") + "/chart.png" + authored = f'Original explanation.\n\n![Source chart](<{url}> "Figure 6")' + script = fake.read_text() + default = '"message": "Runtime response.",' + assert default in script + fake.write_text(script.replace(default, + f'"message": {authored!r} if turn_count == 1 else "Next original reply.",', 1)) + article_url = "https://source.example/article" + stopped = fault in ("stopped", "stopped_after_fetch", "stopped_on_upload_source_read", "stopped_before_upload_intent") + closed = fault in ("closed_on_upload_preview", "closed_on_upload_source_read", "closed_on_source_controls_read", + "closed_before_upload_intent") + redirect_targets = { + "listed_redirect": "/canonical.png", + "same_host_redirect": "/unlisted.png", + "foreign_redirect": "https://unrelated.example/foreign.png", + "foreign_round_trip": "https://unrelated.example/foreign.png", + "query_redirect": "/chart.png?private=fixture", + } + + def respond(target): + if target == article_url and fault == "article_redirect": + return "/other-article", None, b"" + if target in (article_url, "https://source.example/other-article"): + basename = "different-chart.png" if fault == "unlisted_image" else "chart.png" + listings = f'' + if fault == "listed_redirect": + listings += '' + return None, "text/html", listings.encode() + if target == url and fault in redirect_targets: + return redirect_targets[fault], None, b"" + if target == "https://unrelated.example/foreign.png" and fault == "foreign_round_trip": + return "https://source.example/unlisted.png", None, b"" + if target == url and fault == "stopped_after_fetch": + transport.admit("notes-app", provider.event("notes-app", "stop-during-image-fetch", "/stop")) + return None, "image/png", PNG_BYTES + + fetched, resolutions = source_https(respond) + images_delivered = fault in ("none", "listed_redirect") + io = ImageIO(provider) + io.corrupt_download = fault in ("corrupt_readback", "legacy_cached_readback") + upload_previewed = False + control_committed = False + source_edited_after_intent = False + if fault == "edited_after_upload_intent": + from loopx.extensions.lark import return_files + + write_receipt = return_files._write + + def edit_after_upload_intent(path, value): + nonlocal source_edited_after_intent + write_receipt(path, value) + if value.get("upload_started") and not source_edited_after_intent: + assert json.loads(path.read_text())["upload_started"] is True + provider.messages[source["message_id"]]["content"] = "The source changed after upload intent." + source_edited_after_intent = True + + monkeypatch.setattr(return_files, "_write", edit_after_upload_intent) + + if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"): + from loopx.extensions.lark import return_files + write_receipt = return_files._write + + def control_before_upload_intent(path, value): + nonlocal control_committed + if value.get("upload_started") and not control_committed: + control_committed = True + if stopped: + transport.admit("notes-app", provider.event("notes-app", "stop-before-resource-intent", "/stop")) + assert any(row.get("command") == "stop" for row in transport.core.source_controls()) + else: + assert runtime.close_session(sid) + assert store.load_session(sid)["status"] == "closed" + return write_receipt(path, value) + + monkeypatch.setattr(return_files, "_write", control_before_upload_intent) + + if fault == "closed_on_source_controls_read": + read_controls = transport.core.source_controls + + def close_during_control_read(): + nonlocal control_committed + if upload_previewed and not control_committed: + control_committed = True + assert runtime.close_session(sid) + assert store.load_session(sid)["status"] == "closed" + return read_controls() + + monkeypatch.setattr(transport.core, "source_controls", close_during_control_read) + + def media(args, cwd=None, timeout=None): + nonlocal upload_previewed, control_committed + if "images" in args and "create" in args and "--dry-run" in args: + upload_previewed = True + if fault == "closed_on_upload_preview": + runtime.close_session(sid) + if (fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read") + and upload_previewed and not control_committed and ( + ("+messages-mget" in args and source["message_id"] in args) or + f"/open-apis/im/v1/messages/{source['message_id']}" in args)): + control_committed = True + if stopped: + transport.admit("notes-app", provider.event("notes-app", "stop-during-source-read", "/stop")) + assert any(control.get("command") == "stop" for control in transport.core.source_controls()) + else: + runtime.close_session(sid) + assert store.load_session(sid)["status"] == "closed" + if fault == "upload_denied" and "images" in args and "create" in args and "--dry-run" not in args: + return {"returncode": 3, "stderr": '{"ok":false,"error":{"code":99991672}}'} + return io(args, cwd, timeout) + + transport.runner = media + try: + source = provider.event("notes-app", "answer-with-original-image", "Read https://source.example/article") + transport.admit("notes-app", source) + original = transport.core.pending()[0] + sid, tid, ref = (original[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" + if fault == "changed_request": + provider.messages[source["message_id"]]["content"] = "Source URL removed." + path = transport.root / f"{ref}.json" + assert path in transport.pending_body_paths() + assert transport.reconcile_request(path) == 0 + text_only = json.loads(path.read_text()) + assert text_only["deliveries"]["terminal"]["verified"] is True + assert transport.core.read_request(ref).get("delivery_verified", False) is False + assert fetched == [] and io.uploads == 0 + assert path not in transport.pending_body_paths() + assert path in transport.pending_delivery_paths() and transport.is_recovery_path(path) + if fault == "stopped": + transport.admit("notes-app", provider.event("notes-app", "stop-before-image-work", "/stop")) + assert transport.reconcile_request(path) == int(images_delivered) + saved = json.loads(path.read_text()) + assert saved["answer_images"]["status"] == ("delivered" if images_delivered else "unverified") + out = [(mid, row, json.loads(row["body"]["content"])["zh_cn"]["content"]) + for mid, row in provider.messages.items() if mid.startswith("om_out_") and row["msg_type"] == "post"] + assert all(row["chat_id"] == source["chat_id"] for _, row, _ in out) + assert any("[Source chart](" in nodes[0][0]["text"] for _, _, nodes in out) + assert all(not any("![Source chart]" in node.get("text", "") for line in nodes for node in line) for _, _, nodes in out) + if fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read", "closed_on_source_controls_read", + "stopped_before_upload_intent", "closed_before_upload_intent"): + assert control_committed + if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"): + from loopx.extensions.lark.return_files import _root + assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == [] + if fault == "edited_after_upload_intent": + from loopx.extensions.lark.return_files import _root + + assert source_edited_after_intent and io.uploads == 0 + assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == [] + assert "answer_images.terminal" not in saved["deliveries"] + if stopped or closed: + assert io.uploads == 0 and transport.core.read_request(ref).get("delivery_verified", False) is False + assert not any(name.startswith(("answer_images.", "answer_image_notice.")) for name in saved["deliveries"]) + if stopped: + assert any(control.get("command") == "stop" for control in transport.core.source_controls()) + else: + assert store.load_session(sid)["status"] == "closed" + assert transport.core.read_request(ref).get("delivery_verified", False) is images_delivered + native_images = [node["image_key"] for _, _, nodes in out for line in nodes for node in line if node["tag"] == "img"] + assert native_images == list(io.resources) + assert all(io.resources[k] == PNG_BYTES for k in native_images) + expected_requests = { + "foreign_source": [], "changed_request": [], "stopped": [], "unlisted_image": [article_url], + "article_redirect": [article_url], "listed_redirect": [article_url, url, "https://source.example/canonical.png"], + }.get(fault, [article_url, url]) + assert fetched == expected_requests + assert resolutions == ["source.example"] * len(expected_requests) + if fault in {"same_host_redirect", "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect"}: + assert io.uploads == 0 and native_images == [] + assert ("图片尚未核验送达" in str(out)) == (not images_delivered and not stopped and not closed) + if fault in ("corrupt_readback", "legacy_cached_readback"): + io.corrupt_download = False + from loopx.chat_store import _atomic_write_json + restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, + runner=media, cli_bin="lark-cli") + writes = list(provider.writes) + assert path not in restarted.pending_body_paths() + assert path not in restarted.pending_delivery_paths() + assert restarted.reconcile_request(path) == 0 + assert provider.writes == writes and io.uploads == 1 and fetched == [article_url, url] + saved["answer_images"]["next_check_at"] = 0 + if fault == "legacy_cached_readback": + saved["answer_images"].pop("source_urls_verified") + _atomic_write_json(path, saved) + assert path in restarted.pending_delivery_paths() + assert restarted.reconcile_request(path) == 1 + assert transport.core.read_request(ref)["delivery_verified"] is True + assert provider.writes == writes and io.uploads == 1 + assert fetched == [article_url, url] * (2 if fault == "legacy_cached_readback" else 1) + expected_turns = 1 + if fault == "upload_denied": + followup = provider.event("notes-app", "text-after-image-denial", "Answer the next question") + retry = transport.admit("notes-app", followup) + assert retry["status"] == "durably_accepted" + later_ref = identity_ref(transport._binding("notes-app")["provider_ref"], followup["message_id"]) + later = transport.core.read_request(later_ref) + assert later["session_id"] == sid + assert runtime.wait_for_turn(session_id=sid, turn_id=later["turn_id"], timeout_sec=10)["status"] == "completed" + later_path = transport.root / f"{later_ref}.json" + assert path not in transport.pending_body_paths() and path not in transport.pending_delivery_paths() + assert later_path in transport.pending_body_paths() + assert transport.reconcile_request(later_path) == 1 + assert transport.core.read_request(later_ref)["delivery_verified"] is True + assert transport.core.read_request(ref).get("delivery_verified", False) is False + expected_turns = 2 + packets = [json.loads(line) for line in capture.read_text().splitlines()] + assert sum(packet.get("method") == "turn/start" for packet in packets) == expected_turns + assert any(row["text"].endswith(authored) for row in store.messages(sid) if row["role"] == "agent") + finally: + runtime.close() + + +@pytest.mark.parametrize("fault", ["readback", "raw_paired_post", "edited_source", "edited_original_reply", "edit_on_image_get", + "edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_preview", "edit_on_send_source_read", + "edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock", + "foreign_sender", "changed_bytes", "stop", "unknown_send", "upload_denied", "upload_scope_then_granted", + "upload_scope_with_conflicting_stdout", "upload_cli_missing", "upload_cli_fd_limit", "upload_ack_missing", + "closed_on_source_controls_read", + "unlisted_redirect", "unlisted_cached_redirect"]) +def test_original_worker_queues_only_media_for_a_verified_legacy_answer(ordinary, monkeypatch, source_https, fault): # noqa: F811 + from hashlib import sha256 + from loopx.chat_store import _atomic_write_json + from test_chat_image_attachments import PNG_BYTES + from test_lark_app_image_projection import ImageIO + + resource = pytest.importorskip("resource") if fault == "upload_cli_fd_limit" else None + store, runtime, provider, transport = connect(ordinary) + fake = ordinary[5] + origin = "https://source.example/article" + urls = [f"https://source.example/figure-{i}.png" for i in range(1, 5)] + figures = {url: PNG_BYTES + bytes([i]) for i, url in enumerate(urls)} + answer = f"Original explanation.\n\n![One]({urls[0]})\n\n![Two]({urls[1]})" + script = fake.read_text() + marker = '"message": "Runtime response.",' + assert marker in script + fake.write_text(script.replace(marker, f'"message": {answer!r},', 1)) + io = ImageIO(provider) + edited_body = None + late_source_edit = False + committed_resource_path = None + upload_previews = 0 + send_preview_seen = False + denied_uploads = 0 + scope_granted = False + executable_restored = False + + def edit_original_reply(): + nonlocal edited_body + edited_body = {"content": json.dumps({"zh_cn": {"content": [[{"tag": "text", "text": "The original answer was edited."}]]}})} + provider.messages[prior_id]["body"] = edited_body + + def provider_io(args, cwd=None, timeout=None): + nonlocal upload_previews, send_preview_seen, denied_uploads, late_source_edit + image_upload = "images" in args and "create" in args and "--dry-run" not in args + pair_read = "/open-apis/im/v1/messages/mget" in args and "--params" in args + source_read = (("+messages-mget" in args and event["message_id"] in args) + or (pair_read and event["message_id"] in json.loads(args[args.index("--params") + 1])["message_ids"])) + if fault == "upload_denied" and image_upload: + provider.calls.append(list(args)) + return {"returncode": 3, "stderr": '{"ok":false,"error":{"type":"authorization","code":99991672}}'} + typed_scope_fault = fault in ("upload_scope_then_granted", "upload_scope_with_conflicting_stdout") + if image_upload and typed_scope_fault and io.uploads == 1 and not scope_granted: + provider.calls.append(list(args)) + denied_uploads += 1 + return {"returncode": 3, + "stderr": json.dumps({"ok": False, "identity": "bot", "error": { + "type": "authorization", "subtype": "app_scope_not_applied", "code": 99991672, + "identity": "bot"}}), + "stdout": "invalid provider acknowledgement" if fault == "upload_scope_with_conflicting_stdout" else ""} + if (fault == "edit_source_on_final_pair_read" and committed_resource_path is not None + and not late_source_edit + and (("+messages-mget" in args and prior_id in args) + or pair_read)): + assert json.loads(committed_resource_path.read_text())["upload_started"] is True + provider.messages[event["message_id"]]["content"] = "The source changed during the final reply read." + late_source_edit = True + uploads_before_edit = {"edit_on_upload_source_read": 0, "edit_on_next_upload_source_read": 1} + if (fault in uploads_before_edit and upload_previews == uploads_before_edit[fault] + 1 and edited_body is None + and io.uploads == uploads_before_edit[fault] + and source_read): + edit_original_reply() + if (fault == "edit_on_send_source_read" and send_preview_seen and edited_body is None + and source_read): + edit_original_reply() + result = io(args, cwd, timeout) + if pair_read and fault == "raw_paired_post": + body = json.loads(result["stdout"]) + historical = next(row for row in body["data"]["items"] if row["message_id"] == prior_id) + original_rows = json.loads(historical["body"]["content"])["zh_cn"]["content"] + historical["body"] = {"content": json.dumps({ + "title": "", "content_v2": original_rows, + "content": [[{"tag": "text", "text": "A rendered preview only."}]], + })} + result = {**result, "stdout": json.dumps(body)} + if "images" in args and "create" in args and "--dry-run" in args: + upload_previews += 1 + if "+messages-send" in args and "--dry-run" in args and "--content" in args: + if "原图(按原文顺序)" in args[args.index("--content") + 1]: + send_preview_seen = True + if fault == "edit_on_send_preview": + edit_original_reply() + return result + + runner = provider_io + launch_faults = {"upload_cli_missing", "upload_cli_fd_limit"} + upload_faults = launch_faults | {"upload_ack_missing"} + if fault in upload_faults: + from loopx.extensions.lark.cli_resolution import LarkCliResolution, build_lark_command_runner + + native_runner = build_lark_command_runner(LarkCliResolution(sys.executable, True, "fixture", None, None)) + missing_executable = str(fake.with_name("missing-lark-cli")) + if resource is not None: + import errno + import subprocess + + popen = subprocess.Popen + + def spawn(command, *args, **kwargs): + if command != [sys.executable, "-c", "pass"] or executable_restored: + return popen(command, *args, **kwargs) + # Exhaust descriptors only at real Popen pipe creation, + # after the source check and durable upload marker. + limits = resource.getrlimit(resource.RLIMIT_NOFILE) + try: + resource.setrlimit(resource.RLIMIT_NOFILE, (0, limits[1])) + return popen(command, *args, **kwargs) + except OSError as exc: + assert exc.errno == errno.EMFILE + raise + finally: + resource.setrlimit(resource.RLIMIT_NOFILE, limits) + + monkeypatch.setattr(subprocess, "Popen", spawn) + + def guarded_provider(args, cwd=None, timeout=None, *, dispatch_guard=None): + if dispatch_guard is not None: + # Use the real subprocess boundary with a synthetic provider: + # an unavailable executable cannot reach resource creation. + command = ([missing_executable] if fault == "upload_cli_missing" and not executable_restored + else [sys.executable, "-c", "pass"]) + assert native_runner(command, cwd, timeout, dispatch_guard=dispatch_guard)["returncode"] == 0 + return provider_io(args, cwd, timeout) + + guarded_provider.dispatch_guard = True + runner = guarded_provider + + transport.runner = runner + + unlisted = "https://source.example/unlisted.png" + + def respond(url): + if url == origin: + return None, "text/html", "".join(f'' for url in urls).encode() + if fault == "edit_on_image_get": + edit_original_reply() + if fault in ("unlisted_redirect", "unlisted_cached_redirect") and url == urls[-1]: + return "/unlisted.png", None, b"" + return None, "image/png", figures[urls[-1] if url == unlisted else url] + + fetched, resolutions = source_https(respond) + + def restart(): + return LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, + runner=runner, cli_bin="lark-cli") + + try: + event = provider.event("notes-app", "historical-image-answer", f"Read {origin}") + assert transport.admit("notes-app", event)["status"] == "durably_accepted" + original = transport.core.pending()[0] + sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref")) + assert runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)["status"] == "completed" + path = transport.root / f"{ref}.json" + # 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: + transport._deliver(p, row, "terminal", text, inbox=inbox)) + assert transport.reconcile_request(path) == 1 + saved = json.loads(path.read_text()) + terminal = saved["deliveries"]["terminal"] + prior_id = terminal["attempt"]["message_ref"] + prior_body = json.dumps(provider.messages[prior_id]["body"]) + core = (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid)) + writes = len(provider.writes) + worker = restart() + assert path not in worker.pending_delivery_paths() + assert worker.reconcile_request(path) == 0 and io.uploads == 0 + + if fault == "closed_on_source_controls_read": + read_controls = worker.core.source_controls + + def close_during_control_read(): + if upload_previews and store.load_session(sid)["status"] != "closed": + assert runtime.close_session(sid) + return read_controls() + + monkeypatch.setattr(worker.core, "source_controls", close_during_control_read) + + plan = {"binding_id": saved["binding_id"], "turn_id": tid, "message_id": prior_id, + "answer_sha256": sha256(answer.encode()).hexdigest(), + "images": [{"url": url, "sha256": sha256(figures[url]).hexdigest()} for url in urls]} + manifest = worker.root / "historical-answer-images" / path.name + _atomic_write_json(manifest, plan) + if fault == "unlisted_cached_redirect": + import base64 + from loopx.chat_attachments import normalize_chat_image_attachments + + legacy_attachments = normalize_chat_image_attachments([ + {"id": f"reply-image-{index}", "name": f"original-{index}.png", "mime_type": "image/png", + "data_url": "data:image/png;base64," + base64.b64encode(data).decode(), "size": len(data)} + for index, data in enumerate(figures.values(), 1)]) + saved["historical_answer_images"] = {"plan": plan, "status": "pending", "attachments": legacy_attachments} + _atomic_write_json(path, saved) + assert path not in worker.pending_body_paths() + assert path in worker.pending_delivery_paths() and worker.is_recovery_path(path) + if fault == "edited_source": + provider.messages[event["message_id"]]["content"] = "Changed source." + elif fault == "edited_original_reply": + edit_original_reply() + elif fault == "foreign_sender": + provider.messages[prior_id]["sender"]["id"] = provider.profile_apps["steward-app"] + elif fault == "changed_bytes": + figures[urls[-1]] = PNG_BYTES + elif fault == "stop": + # The authoritative Stop predates any possible file recovery. + control = {**core[0], "command": "stop"} + _atomic_write_json(transport.core.root / ("d" * 24 + ".json"), control) + elif fault == "readback": + io.corrupt_download = True + elif fault == "unknown_send": + provider.lose_next_reply = True + elif fault == "upload_ack_missing": + io.unknown_upload = True + elif fault in {"edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}: + from loopx.extensions.lark import return_files + + write_receipt = return_files._write + + def edit_after_upload_intent(path, value): + nonlocal committed_resource_path + write_receipt(path, value) + if value.get("upload_started") and committed_resource_path is None: + committed_resource_path = path + if fault == "edit_reply_after_upload_intent": + assert json.loads(path.read_text())["upload_started"] is True + edit_original_reply() + + monkeypatch.setattr(return_files, "_write", edit_after_upload_intent) + if fault == "edit_source_on_dispatch_lock": + from loopx.extensions.lark import private_delivery_discovery + + enter_lock = private_delivery_discovery.try_exclusive_file_lock + + def edit_source_before_lock(path, **kwargs): + nonlocal late_source_edit + if kwargs.get("operation") == "dispatch_external_answer_media" and not late_source_edit: + assert json.loads(committed_resource_path.read_text())["upload_started"] is True + provider.messages[event["message_id"]]["content"] = "The original changed before the dispatch lock." + late_source_edit = True + return enter_lock(path, **kwargs) + + monkeypatch.setattr(private_delivery_discovery, "try_exclusive_file_lock", edit_source_before_lock) + + if fault == "raw_paired_post": + assert worker.reconcile_request(path) == 1 + assert io.uploads == 4 and len(provider.writes) == writes + 1 + assert json.loads(path.read_text())["deliveries"]["answer_images.historical"]["images_verified"] is True + assert worker.reconcile_request(path) == 0 and io.uploads == 4 + assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid)) + return + assert worker.reconcile_request(path) == 0 + if fault == "closed_on_source_controls_read": + assert upload_previews == 1 and store.load_session(sid)["status"] == "closed" + if fault in upload_faults: + # Only the attempt that could not start is safe to retry. + assert io.uploads == int(fault == "upload_ack_missing") + assert len(provider.writes) == writes + pending = json.loads(path.read_text()) + assert pending["historical_answer_images"]["status"] == "pending" + assert "answer_images.historical" not in pending["deliveries"] + pending["historical_answer_images"]["next_check_at"] = 0 + _atomic_write_json(path, pending) + executable_restored = True + io.unknown_upload = False + worker = restart() + recovered = fault in launch_faults + assert worker.reconcile_request(path) == int(recovered) + post_state = json.loads(path.read_text()) + assert post_state["historical_answer_images"]["status"] == ("delivered" if recovered else "pending") + if recovered: + media = post_state["deliveries"]["answer_images.historical"] + post = provider.messages[media["attempt"]["message_ref"]] + assert post["chat_id"] == event["chat_id"] + assert post["sender"]["id"] == provider.profile_apps["notes-app"] + assert media["images_verified"] and io.downloads == 4 + assert [io.resources[key] for key in media["image_keys"]] == list(figures.values()) + assert worker.reconcile_request(path) == 0 + assert io.uploads == (4 if recovered else 1) + assert len(provider.writes) == writes + int(recovered) + elif fault in {"readback", "unknown_send", "upload_scope_then_granted", "upload_scope_with_conflicting_stdout"}: + pending = json.loads(path.read_text()) + assert pending["historical_answer_images"]["status"] == "pending" + if fault.startswith("upload_scope_"): + assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes + assert "answer_images.historical" not in pending["deliveries"] + scope_granted = True + else: + assert io.uploads == 4 and len(provider.writes) == writes + 1 + worker = restart() + assert path not in worker.pending_delivery_paths() + pending["historical_answer_images"]["next_check_at"] = 0 + _atomic_write_json(path, pending) + io.corrupt_download = False + assert path in worker.pending_delivery_paths() + delivered = fault in ("readback", "upload_scope_then_granted") + assert worker.reconcile_request(path) == int(delivered) + if fault == "upload_scope_with_conflicting_stdout": + assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes + assert json.loads(path.read_text())["historical_answer_images"]["status"] == "pending" + return + assert io.uploads == 4 and len(provider.writes) == writes + 1 + row = json.loads(path.read_text()) + assert row["historical_answer_images"]["status"] == ("delivered" if delivered else "pending") + outputs = [m for m in provider.messages.values() if m["msg_type"] == "post" + and "原图(按原文顺序)" in m["body"]["content"]] + assert len(outputs) == 1 + post = outputs[0] + assert post["chat_id"] == event["chat_id"] and post["sender"]["id"] == provider.profile_apps["notes-app"] + lines = json.loads(post["body"]["content"])["zh_cn"]["content"] + keys = [node["image_key"] for line in lines for node in line if node["tag"] == "img"] + assert len(keys) == 4 and [io.resources[key] for key in keys] == list(figures.values()) + assert worker.reconcile_request(path) == 0 + else: + expected_uploads = {"edit_on_send_preview": 4, "edit_on_send_source_read": 4, + "edit_on_next_upload_source_read": 1} + assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes + if fault == "edit_reply_after_upload_intent": + from loopx.extensions.lark.return_files import _root + + assert edited_body is not None + assert list((_root(worker.runtime_root) / "lark-chat-images").glob("*.json")) == [] + if fault in {"edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}: + assert late_source_edit and not committed_resource_path.exists() + assert "answer_images.historical" not in json.loads(path.read_text())["deliveries"] + if fault in {"edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_source_read", + "edit_reply_after_upload_intent"}: + assert edited_body is not None + pending = json.loads(path.read_text()) + pending["historical_answer_images"]["next_check_at"] = 0 + _atomic_write_json(path, pending) + assert restart().reconcile_request(path) == 0 + assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes + if fault in ("unlisted_redirect", "unlisted_cached_redirect"): + assert fetched == [origin, *urls] and resolutions == ["source.example"] * len(fetched) + assert unlisted not in fetched + if fault == "upload_denied": + pending = json.loads(path.read_text()) + assert pending["historical_answer_images"]["status"] == "pending" + assert "answer_images.historical" not in pending["deliveries"] + assert any("images" in cmd and "create" in cmd and "--dry-run" not in cmd for cmd in provider.calls) + assert provider.messages[prior_id]["body"] == (edited_body if edited_body is not None else json.loads(prior_body)) + assert json.loads(path.read_text())["deliveries"]["terminal"] == terminal + assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid)) + assert sum(json.loads(line).get("method") == "turn/start" for line in ordinary[4].read_text().splitlines()) == 1 + finally: + runtime.close() + + +def test_private_delivery_keeps_retired_steward_cards_out_of_image_recovery( + ordinary, # noqa: F811 +): + from loopx.chat_store import _atomic_write_json + + store, runtime, provider, transport = connect(ordinary) + bindings = transport.bindings + workspace = runtime.project_contexts.available()[0]["project_ref"] + binding = bindings.configure(transport_ref="steward-app", project_ref=workspace, + executor_endpoint_id="codex", context_kind="steward") + try: + event = provider.event("steward-app", "retired-card-before-image-repair", "/triage") + assert transport.admit("steward-app", event)["status"] == "triage_queued" + ref = identity_ref(binding["provider_ref"], event["message_id"]) + card = transport.root / f"{ref}.json" + assert card in transport.pending_body_paths() + assert card in transport.pending_delivery_paths() + assert transport.health()[binding["binding_id"]]["pending_count"] == 1 + + provider.messages[event["message_id"]]["content"] = "/status" + assert transport.reconcile_request(card) == 0 + retired = json.loads(card.read_text()) + assert retired["triage"]["retired"] == "original_source_changed" + + # Even an orphaned descriptor with this filename cannot turn an + # upstream transport-only record into image or model recovery work. + _atomic_write_json(transport.root / "historical-answer-images" / card.name, {}) + worker = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root, + runner=provider, cli_bin="lark-cli") + assert card not in worker.pending_body_paths() + assert card not in worker.pending_delivery_paths() + assert worker.health()[binding["binding_id"]]["pending_count"] == 0 + assert not (worker.core.root / card.name).exists() + + assert worker.reconcile_request(card) == 0 + assert json.loads(card.read_text()) == retired + assert not store.list_sessions() + assert not provider.writes + finally: + runtime.close() + + @pytest.mark.parametrize("case", ["normal", "invalid-metadata", "metadata-only", "unfinished", "source-changed", "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 @@ -2428,6 +3102,13 @@ def observe_delivery(path): completed_path.unlink() assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0} + # A delivered body can still have an outstanding historical image reply. + _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "pending"}}) + assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0} + assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0} + _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "delivered"}}) + assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0} + @pytest.mark.parametrize("scenario", ["concurrent-close", "transient-session-read"]) def test_settings_health_uses_current_session_for_each_batch(ordinary, monkeypatch, scenario): # noqa: F811 diff --git a/tests/test_public_source_reader.py b/tests/test_public_source_reader.py index b461adc648..3bd6fa77c9 100644 --- a/tests/test_public_source_reader.py +++ b/tests/test_public_source_reader.py @@ -70,6 +70,26 @@ def test_github_diff_transport_failure_recovers_canonical_source_in_same_budget( for _, _, _, _, headers in state["attempts"]) +def test_github_diff_recovery_honors_exact_source_scope_before_dns(github_diff_transport): + state = github_diff_transport + original = "https://github.com/example/project/pull/42.diff" + with pytest.raises(ValueError, match="public source URL not authorized"): + reader.fetch(original, deadline=110.0, allowed_urls={original}) + assert state["dns"] == ["github.com"] + assert [attempt[0] for attempt in state["attempts"]] == ["github.com", "github.com"] + assert len(state["closed"]) == 2 + + +def test_public_url_scope_rejects_initial_destination_before_dns(monkeypatch): + def resolve(host): + pytest.fail(f"unapproved destination resolved: {host}") + + monkeypatch.setattr(reader, "public_addresses", resolve) + with pytest.raises(ValueError, match="public source URL not authorized"): + reader.fetch("https://source.example/unlisted.png", deadline=time.monotonic() + 10, + allowed_urls={"https://source.example/listed.png"}) + + @pytest.mark.parametrize("host", ["github.com", "patch-diff.githubusercontent.com"]) def test_github_diff_recovery_rejects_mixed_dns_before_connection(github_diff_transport, host): state = github_diff_transport