"""Run an isolated, delegated-auth NAO quality conversation as an NDJSON worker. The worker keeps NAO's native engine and MCP protocol intact. The quality MCP uses DelegatedApi, which can only call the local, actor-scoped runtime-query API. Credentials are read from stdin and kept in a mode-0600 file outside the NAO project; they are never passed to the model or written to worker output. """ from __future__ import annotations import argparse import base64 import contextlib import fcntl import hashlib import http.cookiejar import json import os import re import secrets import shutil import signal import socket import stat import subprocess import sys import threading import time import uuid from dataclasses import dataclass from datetime import datetime, timezone from pathlib import Path from typing import Any, Callable, Iterator from urllib.error import HTTPError, URLError from urllib.parse import parse_qs, quote, urlencode, urlsplit from urllib.request import HTTPCookieProcessor, Request, build_opener ROOT = Path(__file__).resolve().parents[1] USERS_ROOT = ROOT / ".runtime" / "nao-users" TRIAL = ROOT / ".runtime" / "nao-trial" VENV = TRIAL / "venv" PYTHON = VENV / "bin" / "python" NAO_SERVER = VENV / "lib" / "python3.11" / "site-packages" / "nao_core" / "bin" / "nao-chat-server" FASTAPI_MAIN = VENV / "lib" / "python3.11" / "site-packages" / "nao_core" / "bin" / "fastapi" / "main.py" TEMPLATES = ROOT / "ai" / "nao-quality" RUNTIME_QUERY_URL = "http://127.0.0.1:5281/api/analysis/runtime/query" MAX_INSTANCES = 2 TURN_TIMEOUT_SECONDS = 600 SSE_IDLE_TIMEOUT_SECONDS = 60 NATIVE_STOP_TIMEOUT_SECONDS = 4.0 CHAT_ID_WAIT_SECONDS = 0.75 MAX_QUESTION_CHARS = 20_000 MAX_CONTEXT_CHARS = 32_000 MAX_HTML_BYTES = 12 * 1024 * 1024 _UUID_RE = re.compile(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$") _ALLOWED_PATHS = { "GET": { "/api/releases", "/api/options", "/api/metrics", "/api/analysis/tools/catalog", "/api/analytics/top", }, "POST": {"/api/analysis/tools/details", "/api/analysis/defects"}, } class RuntimeErrorSafe(RuntimeError): """An error safe to return without exposing credentials or request bodies.""" def _uuid(value: Any, field: str) -> str: if not isinstance(value, str) or not _UUID_RE.fullmatch(value): raise ValueError(f"{field} 必须是 UUID。") return str(uuid.UUID(value)) def _mode_private(path: Path, mode: int = 0o600) -> None: path.chmod(mode) def _atomic_json(path: Path, value: Any, *, mode: int = 0o600) -> None: path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) temp = path.with_name(f".{path.name}.{os.getpid()}.{secrets.token_hex(5)}.tmp") fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, mode) try: with os.fdopen(fd, "w", encoding="utf-8") as stream: json.dump(value, stream, ensure_ascii=False, separators=(",", ":"), allow_nan=False) stream.write("\n") stream.flush() os.fsync(stream.fileno()) os.replace(temp, path) _mode_private(path, mode) finally: temp.unlink(missing_ok=True) def _read_json(path: Path) -> Any: with path.open("r", encoding="utf-8") as stream: return json.load(stream) def _safe_runtime_root(runtime_root: Path) -> Path: runtime_root = Path(runtime_root).absolute() if runtime_root.is_symlink(): raise ValueError("运行目录无效。") root = runtime_root.resolve() if root != USERS_ROOT.resolve() and not root.name.startswith("test-nao-users-"): raise ValueError("运行目录无效。") return root @dataclass(frozen=True) class AccessSnapshot: actor_id: str token: str release_id: str query_url: str def load_access_file( access_path: str | Path, *, runtime_root: Path = USERS_ROOT, expected_actor_id: str | None = None, ) -> AccessSnapshot: """Read and validate a private access file without following path escapes.""" root = _safe_runtime_root(Path(runtime_root)) candidate = Path(access_path) if candidate.is_symlink(): raise ValueError("访问凭据文件路径无效。") absolute = candidate.absolute() actor_dir = absolute.parent if actor_dir.is_symlink() or actor_dir.parent.resolve() != root: raise ValueError("访问凭据文件路径越界。") actor_id = _uuid(actor_dir.name, "actorId") if expected_actor_id is not None and actor_id != _uuid(expected_actor_id, "actorId"): raise ValueError("访问凭据与当前 actor 不匹配。") if absolute.name != "access.json" or absolute.parent.resolve() != actor_dir.resolve(): raise ValueError("访问凭据文件路径无效。") try: info = absolute.stat(follow_symlinks=False) except OSError: raise ValueError("访问凭据文件不可用。") from None if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077: raise ValueError("访问凭据文件权限无效。") try: raw = _read_json(absolute) except (OSError, json.JSONDecodeError): raise ValueError("访问凭据文件内容无效。") from None if not isinstance(raw, dict): raise ValueError("访问凭据文件内容无效。") token = raw.get("token") release_id = raw.get("releaseId") query_url = raw.get("queryUrl") if not isinstance(token, str) or not token.strip() or len(token) > 4096 or "\n" in token or "\r" in token: raise ValueError("访问凭据不完整。") release_id = _uuid(release_id, "releaseId") parsed = urlsplit(query_url if isinstance(query_url, str) else "") if (parsed.scheme != "http" or parsed.hostname != "127.0.0.1" or parsed.port != 5281 or parsed.path != "/api/analysis/runtime/query" or parsed.username or parsed.password or parsed.query or parsed.fragment): raise ValueError("质量查询地址必须是固定的本机接口。") return AccessSnapshot(actor_id, token, release_id, RUNTIME_QUERY_URL) def _check_release_pin(path: str, payload: Any, release_id: str) -> None: query = parse_qs(urlsplit(path).query, keep_blank_values=True, strict_parsing=False) values = query.get("releaseId", []) if any(value != release_id for value in values): raise ValueError("请求的 releaseId 与本轮固定版本不一致。") def walk(value: Any) -> Iterator[Any]: if isinstance(value, dict): for key, child in value.items(): if key == "releaseId": yield child yield from walk(child) elif isinstance(value, list): for child in value: yield from walk(child) if any(value != release_id for value in walk(payload)): raise ValueError("请求的 releaseId 与本轮固定版本不一致。") def _redact_credentials(value: Any, token: str) -> Any: if isinstance(value, dict): sensitive_keys = {"token", "accesstoken", "authorization", "bearer"} return { key: _redact_credentials(child, token) for key, child in value.items() if not isinstance(key, str) or key.casefold() not in sensitive_keys } if isinstance(value, list): return [_redact_credentials(child, token) for child in value] if isinstance(value, str): return value.replace(token, "[REDACTED]") return value class DelegatedApi: """QualityTools API adapter. Each call reloads actor credentials and delegates.""" def __init__( self, access_path: str | Path, *, runtime_root: Path = USERS_ROOT, expected_actor_id: str | None = None, opener: Any | None = None, timeout: float = 180, ) -> None: self.access_path = Path(access_path) self.runtime_root = Path(runtime_root) self.expected_actor_id = expected_actor_id self.opener = opener or build_opener() self.timeout = timeout def login(self) -> "DelegatedApi": """Validate the delegated file only; never logs into the NAO admin API.""" load_access_file( self.access_path, runtime_root=self.runtime_root, expected_actor_id=self.expected_actor_id, ) return self def call(self, path: str, payload: dict[str, Any] | None = None, method: str | None = None) -> dict[str, Any]: verb = (method or ("POST" if payload is not None else "GET")).upper() if verb not in _ALLOWED_PATHS or not isinstance(path, str): raise ValueError("质量查询接口不在允许范围内。") parsed_path = urlsplit(path) if (not parsed_path.path.startswith("/api/") or parsed_path.scheme or parsed_path.netloc or parsed_path.fragment or parsed_path.path not in _ALLOWED_PATHS[verb]): raise ValueError("质量查询接口不在允许范围内。") snapshot = load_access_file( self.access_path, runtime_root=self.runtime_root, expected_actor_id=self.expected_actor_id, ) _check_release_pin(path, payload, snapshot.release_id) if payload is not None and not isinstance(payload, dict): raise ValueError("质量查询参数格式无效。") wrapper = {"token": snapshot.token, "method": verb, "path": path, "payload": payload} request = Request( snapshot.query_url, data=json.dumps(wrapper, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode("utf-8"), headers={"Accept": "application/json", "Content-Type": "application/json"}, method="POST", ) try: with self.opener.open(request, timeout=self.timeout) as response: raw = response.read(8 * 1024 * 1024 + 1) except HTTPError as error: # Runtime API may return a safe, actor-scoped detail (for example 403/409). try: detail = json.loads(error.read(64 * 1024).decode("utf-8", "replace")) except Exception: detail = None safe_detail = detail.get("detail") if isinstance(detail, dict) else None if isinstance(safe_detail, str) and len(safe_detail) <= 600: raise RuntimeErrorSafe(safe_detail.replace(snapshot.token, "[REDACTED]")) from None raise RuntimeErrorSafe(f"质量中心查询暂不可用(HTTP {error.code})。") from None except (URLError, OSError, TimeoutError): raise RuntimeErrorSafe("质量中心查询暂不可用,请确认本机质量服务后重试。") from None if len(raw) > 8 * 1024 * 1024: raise RuntimeErrorSafe("质量中心查询响应超过允许大小。") try: result = json.loads(raw.decode("utf-8")) if raw else {} except (UnicodeDecodeError, json.JSONDecodeError): raise RuntimeErrorSafe("质量中心查询返回格式无效。") from None if not isinstance(result, dict): raise RuntimeErrorSafe("质量中心查询返回格式无效。") return _redact_credentials(result, snapshot.token) class ConversationAccumulator: """Decode public text/tool events, ignoring reasoning and raw tool payloads.""" def __init__(self, on_answer: Callable[[str], None] | None = None) -> None: self.blocks: dict[str, str] = {} self.block_order: list[str] = [] self.active_text_id = "text" self.finish_reasons: list[str] = [] self.tool_calls: list[dict[str, str]] = [] self.tool_outputs: dict[str, Any] = {} self.tool_error = False self.error_event = False self.on_answer = on_answer self.chat_id: str | None = None self.done_received = False def feed(self, event: dict[str, Any]) -> None: kind = event.get("type") if kind == "data-newChat": value = (event.get("data") or {}).get("id") if isinstance(event.get("data"), dict) else None if isinstance(value, str) and value and len(value) <= 256: self.chat_id = value elif kind == "text-start": block_id = event.get("id") if isinstance(block_id, str) and block_id: self.active_text_id = block_id if block_id not in self.blocks: self.blocks[block_id] = "" self.block_order.append(block_id) elif kind == "text-delta": block_id = event.get("id") if isinstance(event.get("id"), str) else self.active_text_id self.active_text_id = block_id if block_id not in self.blocks: self.blocks[block_id] = "" self.block_order.append(block_id) delta = event.get("delta") if isinstance(delta, str): self.blocks[block_id] += delta if self.on_answer: self.on_answer(self.blocks[block_id]) elif kind == "finish": reason = event.get("finishReason") if isinstance(reason, str): self.finish_reasons.append(reason) elif kind == "tool-input-available": tool_name = event.get("toolName") if isinstance(tool_name, str): call_id = event.get("toolCallId") call = {"tool": tool_name, "status": "started"} if isinstance(call_id, str): call["toolCallId"] = call_id self.tool_calls.append(call) elif kind in ("tool-output-available", "tool-result"): call_id = event.get("toolCallId") output = event.get("output") if isinstance(call_id, str): self.tool_outputs[call_id] = output if _contains_error_flag(output): self.tool_error = True tool_name = event.get("toolName") call_id = event.get("toolCallId") if isinstance(tool_name, str) or isinstance(call_id, str): for item in reversed(self.tool_calls): matches = (item.get("toolCallId") == call_id if isinstance(call_id, str) else item["tool"] == tool_name) if matches and item["status"] == "started": item["status"] = "failed" if _contains_error_flag(output) else "completed" break elif kind in ("tool-output-error", "error"): self.tool_error = True if kind == "tool-output-error": call_id = event.get("toolCallId") if isinstance(call_id, str): for item in reversed(self.tool_calls): if item.get("toolCallId") == call_id and item["status"] == "started": item["status"] = "failed" break if kind == "error": self.error_event = True elif kind == "[DONE]": self.done_received = True @property def answer(self) -> str: for block_id in reversed(self.block_order): value = self.blocks.get(block_id, "").strip() if value: return value return "" def status(self, *, cancel_cause: str | None = None, stop_succeeded: bool | None = None, transport_failed: bool = False) -> tuple[str, str | None]: if cancel_cause == "user": return "CANCELLED", "user_cancel" if cancel_cause == "timeout": return ("PARTIAL" if self.answer else "FAILED", "timeout") if cancel_cause == "transport": if any(reason == "length" for reason in self.finish_reasons): return "PARTIAL", "length" return ("PARTIAL" if self.answer else "FAILED", "transport_or_stream_error") if any(reason == "length" for reason in self.finish_reasons): return "PARTIAL", "length" if transport_failed or self.error_event or not self.finish_reasons: return ("PARTIAL" if self.answer else "FAILED", "transport_or_stream_error") if self.tool_error: return ("PARTIAL" if self.answer else "FAILED", "mcp_error") if self.answer and (self.done_received or "stop" in self.finish_reasons): return "COMPLETED", "stop" return ("PARTIAL" if self.answer else "FAILED", "incomplete") def _contains_error_flag(value: Any) -> bool: if isinstance(value, dict): if value.get("isError") is True or value.get("error"): return True return any(_contains_error_flag(item) for item in value.values()) if isinstance(value, list): return any(_contains_error_flag(item) for item in value) if isinstance(value, str) and len(value) <= 64 * 1024: stripped = value.strip() if stripped.startswith(("{", "[")): try: return _contains_error_flag(json.loads(stripped)) except json.JSONDecodeError: return False return False class NativeStopController: """Stop the native NAO chat before closing its SSE stream on cancel/timeout.""" def __init__(self, stop_call: Callable[[str | None], bool], timeout: float) -> None: self.stop_call = stop_call self.timeout = timeout self.finished = threading.Event() self._trigger = threading.Event() self._condition = threading.Condition() self._chat_id: str | None = None self._response: Any | None = None self.cause: str | None = None self.stop_succeeded: bool | None = None self.thread = threading.Thread(target=self._watch, name="nao-native-stop", daemon=True) def start(self) -> None: self.thread.start() def set_response(self, response: Any) -> None: with self._condition: self._response = response def set_chat_id(self, chat_id: str | None) -> None: if not chat_id: return with self._condition: self._chat_id = chat_id self._condition.notify_all() def request_cancel(self, cause: str = "user") -> None: if cause not in ("user", "timeout", "transport"): return if not self._trigger.is_set(): self.cause = cause self._trigger.set() with self._condition: self._condition.notify_all() def _watch(self) -> None: deadline = time.monotonic() + self.timeout while not self.finished.is_set(): remaining = deadline - time.monotonic() if remaining <= 0: self.request_cancel("timeout") break if self._trigger.wait(min(0.1, remaining)): break if self.finished.is_set(): return self.cause = self.cause or "timeout" deadline = time.monotonic() + CHAT_ID_WAIT_SECONDS chat_id = None with self._condition: while not self.finished.is_set() and not self._chat_id and time.monotonic() < deadline: self._condition.wait(timeout=min(0.25, max(0.01, deadline - time.monotonic()))) chat_id = self._chat_id response = self._response try: self.stop_succeeded = bool(self.stop_call(chat_id)) except Exception: self.stop_succeeded = False if response is not None: with contextlib.suppress(Exception): response.close() def close(self) -> None: if self._trigger.is_set(): if self.thread.is_alive(): self.thread.join(timeout=7) if self.thread.is_alive(): self.finished.set() with contextlib.suppress(Exception): if self._response is not None: self._response.close() self.thread.join(timeout=0.2) else: self.finished.set() with self._condition: self._condition.notify_all() if self.thread.is_alive(): self.thread.join(timeout=0.2) class SessionState: """Per-actor mapping from Java session UUIDs to native chat IDs.""" def __init__(self, actor_root: Path, actor_id: str) -> None: self.path = actor_root / "private" / "sessions.json" self.actor_id = actor_id def get_chat_id(self, session_id: str) -> str | None: try: value = _read_json(self.path) except (OSError, json.JSONDecodeError): return None if not isinstance(value, dict) or value.get("actorId") != self.actor_id: return None sessions = value.get("sessions") result = sessions.get(session_id) if isinstance(sessions, dict) else None return result if isinstance(result, str) and result else None def set_chat_id(self, session_id: str, chat_id: str) -> None: self.path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) try: value = _read_json(self.path) except (OSError, json.JSONDecodeError): value = {} if not isinstance(value, dict) or value.get("actorId") != self.actor_id: value = {"actorId": self.actor_id, "sessions": {}} sessions = value.setdefault("sessions", {}) sessions[session_id] = chat_id _atomic_json(self.path, value) def _model_settings() -> tuple[str, str, str]: try: from nao_model_settings import model_settings base, key, model = model_settings() except Exception: raise RuntimeErrorSafe("所选模型配置不可用。") from None if not all(isinstance(item, str) and item for item in (base, key, model)): raise RuntimeErrorSafe("所选模型配置不完整。") return base, key, model def _config_fingerprint(model_base: str, model_key: str, model_name: str) -> str: paths = [TEMPLATES / "RULES.md", TEMPLATES / "agent" / "prompts" / "system.md", ROOT / "scripts" / "nao_quality_runtime.py", ROOT / "scripts" / "nao_quality_runtime_mcp.py", ROOT / "scripts" / "nao_quality_tools.py", ROOT / "scripts" / "nao_quality_mcp.py", ROOT / "scripts" / "nao_model_settings.py"] digest = hashlib.sha256() for path in paths: digest.update(str(path.relative_to(ROOT)).encode()) digest.update(path.read_bytes()) # The one-way fingerprint detects selected model changes without saving its key. digest.update(model_base.encode()) digest.update(model_name.encode()) digest.update(hashlib.sha256(model_key.encode()).digest()) try: sys.path.insert(0, str(ROOT / "scripts")) from nao_quality_tools import tool_definition digest.update(json.dumps(tool_definition(), ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()) except Exception: raise RuntimeErrorSafe("质量工具定义无法读取。") from None return digest.hexdigest() def _write_private_state(actor_root: Path) -> dict[str, str]: private_path = actor_root / "private" / "credentials.json" private_path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) if private_path.exists(): try: value = _read_json(private_path) if all(isinstance(value.get(key), str) and value[key] for key in ("authSecret", "email", "password")): return value except (OSError, json.JSONDecodeError, AttributeError): pass private = { "authSecret": secrets.token_urlsafe(48), "email": f"nao-{uuid.uuid4().hex}@localhost.invalid", "password": secrets.token_urlsafe(32), } _atomic_json(private_path, private) return private def _write_project(actor_root: Path, session_id: str, model_name: str, *, refresh_templates: bool, refresh_mcp: bool, backup_schema: bool) -> None: project = actor_root / "project" if project.is_symlink(): raise RuntimeErrorSafe("NAO project 路径无效。") project.mkdir(parents=True, exist_ok=True, mode=0o700) (project / "agent" / "prompts").mkdir(parents=True, exist_ok=True, mode=0o700) (project / "agent" / "mcps").mkdir(parents=True, exist_ok=True, mode=0o700) if refresh_templates: old_schema = project / "agent" / "mcps" / "quality" if backup_schema and old_schema.exists(): backup = actor_root / "backups" / datetime.now().strftime("%Y%m%d-%H%M%S-%f") / "generatedspec" backup.parent.mkdir(parents=True, exist_ok=True, mode=0o700) shutil.move(str(old_schema), str(backup)) for relative in (Path("RULES.md"), Path("agent/prompts/system.md")): source = TEMPLATES / relative target = project / relative if target.is_symlink(): raise RuntimeErrorSafe("NAO project 模板路径无效。") shutil.copyfile(source, target) target.chmod(0o600) config = ( "project_name: quality-center\n" "databases: []\n" "llm:\n" f" annotation_model: openaiCompatible:{model_name}\n" " providers:\n" " - provider: openaiCompatible\n" " api_key: \"{{ env('NAO_MODEL_KEY') }}\"\n" " base_url: \"{{ env('NAO_MODEL_BASE') }}\"\n" " models:\n" f" - id: {json.dumps(model_name, ensure_ascii=False)}\n" " default: true\n" " settings:\n" " maxOutputTokens: 8192\n" " temperature: 0.2\n" " reasoningEffort: low\n" ) config_path = project / "nao_config.yaml" config_path.write_text(config, encoding="utf-8") config_path.chmod(0o600) evidence_dir = actor_root / session_id / "evidence" evidence_dir.mkdir(parents=True, exist_ok=True, mode=0o700) mcp = { "mcpServers": { "quality": { "transport": "stdio", "command": str(PYTHON), "args": [str(ROOT / "scripts" / "nao_quality_runtime_mcp.py")], "env": { "QUALITY_AGENT_ACCESS_FILE": str(actor_root / "access.json"), "QUALITY_AGENT_EVIDENCE_DIR": str(evidence_dir), "QUALITY_AGENT_SESSION_STATE_FILE": str(actor_root / "private" / "current-session.json"), "QUALITY_AGENT_ACTOR_ID": _uuid(actor_root.name, "actorId"), "QUALITY_AGENT_SESSION_ID": session_id, "QUALITY_AGENT_USERS_ROOT": str(USERS_ROOT.resolve()), "PYTHONPATH": str(ROOT / "scripts"), }, } } } mcp_path = project / "agent" / "mcps" / "mcp.json" if refresh_mcp or not mcp_path.exists(): _atomic_json(mcp_path, mcp) _atomic_json(actor_root / "private" / "current-session.json", {"actorId": actor_root.name, "sessionId": session_id}) os.chmod(project, 0o700) def _available_port() -> int: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.bind(("127.0.0.1", 0)) return int(sock.getsockname()[1]) def _ps_value(pid: int, field: str) -> str | None: try: result = subprocess.run(["ps", "-p", str(pid), "-o", f"{field}="], capture_output=True, text=True, timeout=3, check=False) except (OSError, subprocess.TimeoutExpired): return None if result.returncode != 0: return None return result.stdout.strip() or None def _process_record(pid: int, kind: str, port: int) -> dict[str, Any]: started = _ps_value(pid, "lstart") if not started: raise RuntimeErrorSafe("NAO 本地进程启动失败。") return {"pid": pid, "pgid": pid, "lstart": started, "kind": kind, "port": port} def _process_matches(record: dict[str, Any], actor_root: Path) -> bool: try: pid = int(record["pid"]) if int(record["pgid"]) != pid or os.getpgid(pid) != pid: return False except (KeyError, TypeError, ValueError, ProcessLookupError, PermissionError): return False start = _ps_value(pid, "lstart") command = _ps_value(pid, "command") or "" if not start or start != record.get("lstart"): return False kind = record.get("kind") if kind == "fastapi": return str(PYTHON) in command and str(FASTAPI_MAIN) in command if kind == "server": return (str(NAO_SERVER) in command and "serve" in command and f"--port {int(record['port'])}" in command and "--host 127.0.0.1" in command) return False def _stop_instance(actor_root: Path, *, force: bool = False, grace_seconds: float | None = None) -> bool: state_path = actor_root / "instance.json" try: state = _read_json(state_path) except (OSError, json.JSONDecodeError): return False processes = state.get("processes", []) if isinstance(state, dict) else [] owned: list[dict[str, Any]] = [] for record in processes: if not isinstance(record, dict): continue pid = record.get("pid") if not isinstance(pid, int) or _ps_value(pid, "command") is None: continue if not _process_matches(record, actor_root): raise RuntimeErrorSafe("NAO 进程身份校验失败,已拒绝停止该进程。") owned.append(record) for record in owned: with contextlib.suppress(ProcessLookupError): os.killpg(record["pgid"], signal.SIGTERM) grace = grace_seconds if grace_seconds is not None else (8 if force else 5) deadline = time.monotonic() + grace while time.monotonic() < deadline and any(_process_matches(r, actor_root) for r in owned): time.sleep(0.1) still_owned = [r for r in owned if _process_matches(r, actor_root)] for record in still_owned: if not _process_matches(record, actor_root): continue with contextlib.suppress(ProcessLookupError): os.killpg(record["pgid"], signal.SIGKILL) state_path.unlink(missing_ok=True) return bool(owned) @contextlib.contextmanager def _file_lock(path: Path, *, blocking: bool) -> Iterator[int]: path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) fd = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) try: try: fcntl.flock(fd, fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB)) except BlockingIOError: yield -1 else: yield fd finally: if fd >= 0: with contextlib.suppress(OSError): fcntl.flock(fd, fcntl.LOCK_UN) os.close(fd) def _actor_dirs(users_root: Path = USERS_ROOT) -> list[Path]: if not users_root.exists() or users_root.is_symlink(): return [] return [path for path in users_root.iterdir() if not path.is_symlink() and path.is_dir() and _UUID_RE.fullmatch(path.name)] class RuntimeManager: def __init__(self, users_root: Path = USERS_ROOT) -> None: users_root = Path(users_root).absolute() if users_root.is_symlink(): raise ValueError("运行目录无效。") self.users_root = users_root.resolve() def _actor_root(self, actor_id: str) -> Path: actor_id = _uuid(actor_id, "actorId") self.users_root.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(self.users_root, 0o700) path = self.users_root / actor_id if path.is_symlink(): raise RuntimeErrorSafe("actor 运行目录无效。") path.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(path, 0o700) return path def acquire_actor(self, actor_id: str) -> tuple[Path, int]: actor_root = self._actor_root(actor_id) lock_path = actor_root / "active.lock" fd = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600) try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: os.close(fd) raise RuntimeErrorSafe("该质量账号正处理另一轮分析,请稍后重试。") from None return actor_root, fd def release_actor(self, fd: int) -> None: with contextlib.suppress(OSError): fcntl.flock(fd, fcntl.LOCK_UN) os.close(fd) @staticmethod def _current_session(actor_root: Path) -> str | None: try: value = _read_json(actor_root / "private" / "current-session.json") if isinstance(value, dict) and value.get("actorId") == actor_root.name: return _uuid(value.get("sessionId"), "sessionId") except (OSError, json.JSONDecodeError, ValueError): return None return None def ensure_instance(self, actor_root: Path, session_id: str) -> tuple[str, str, str, str]: model_base, model_key, model_name = _model_settings() fingerprint = _config_fingerprint(model_base, model_key, model_name) _write_private_state(actor_root) state_path = actor_root / "instance.json" state = None try: state = _read_json(state_path) except (OSError, json.JSONDecodeError): pass current_session = self._current_session(actor_root) project = actor_root / "project" project_ready = all((project / relative).is_file() and not (project / relative).is_symlink() for relative in ("RULES.md", "nao_config.yaml", "agent/prompts/system.md", "agent/mcps/mcp.json")) refresh_templates = not project_ready or not isinstance(state, dict) or state.get("fingerprint") != fingerprint refresh_mcp = not project_ready or current_session != session_id backup_schema = isinstance(state, dict) and state.get("fingerprint") != fingerprint if isinstance(state, dict): procs = state.get("processes", []) alive = all(isinstance(r, dict) and _process_matches(r, actor_root) for r in procs) and len(procs) == 2 if alive and state.get("fingerprint") == fingerprint and not refresh_mcp: if self._wait_server(int(state["serverPort"]), timeout=0.5): return model_base, model_key, model_name, f"http://127.0.0.1:{int(state['serverPort'])}" # Stop only processes that still match the recorded PID, start time and process group. _stop_instance(actor_root) self._with_registry_lock(actor_root, session_id, model_base, model_key, model_name, fingerprint, refresh_templates=refresh_templates, refresh_mcp=refresh_mcp, backup_schema=backup_schema) state = _read_json(state_path) return model_base, model_key, model_name, f"http://127.0.0.1:{int(state['serverPort'])}" def _with_registry_lock(self, actor_root: Path, session_id: str, model_base: str, model_key: str, model_name: str, fingerprint: str, *, refresh_templates: bool, refresh_mcp: bool, backup_schema: bool) -> None: with _file_lock(self.users_root / "registry.lock", blocking=True): active = [] actor_dirs = _actor_dirs(self.users_root) for other in actor_dirs: try: state = _read_json(other / "instance.json") except (OSError, json.JSONDecodeError): continue procs = state.get("processes", []) if isinstance(state, dict) else [] if len(procs) == 2 and all(isinstance(r, dict) and _process_matches(r, other) for r in procs): active.append(other) if actor_root not in active and len(active) >= MAX_INSTANCES: evicted = False for other in active: if other == actor_root: continue with _file_lock(other / "active.lock", blocking=False) as lock_fd: if lock_fd < 0: continue _stop_instance(other) evicted = True break if not evicted: raise RuntimeErrorSafe("当前已有两个质量分析实例正在使用,暂时无法启动新实例。") _write_project(actor_root, session_id, model_name, refresh_templates=refresh_templates, refresh_mcp=refresh_mcp, backup_schema=backup_schema) self._start(actor_root, model_base, model_key, model_name, fingerprint) def _start(self, actor_root: Path, model_base: str, model_key: str, model_name: str, fingerprint: str) -> None: private = _write_private_state(actor_root) project = actor_root / "project" logs = actor_root / "logs" logs.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(logs, 0o700) fastapi_port = _available_port() server_port = _available_port() while server_port == fastapi_port: server_port = _available_port() env = os.environ.copy() env.update({ "NAO_MODEL_BASE": model_base, "NAO_MODEL_KEY": model_key, "NAO_MODEL_NAME": model_name, "NAO_DEFAULT_PROJECT_PATH": str(project), "DB_URI": "sqlite:" + str(actor_root / "private" / "nao.sqlite"), "BETTER_AUTH_URL": f"http://127.0.0.1:{server_port}", "BETTER_AUTH_SECRET": private["authSecret"], "FASTAPI_PORT": str(fastapi_port), "PORT": str(fastapi_port), "SERVER_PORT": str(server_port), "ENABLE_USER_SIGNUP": "false", "MODE": "prod", "NAO_CORE_VERSION": "0.3.26", "POSTHOG_DISABLED": "true", "DO_NOT_TRACK": "1", "OTEL_SDK_DISABLED": "true", "ANONYMIZED_TELEMETRY": "False", }) for name in ("nao.sqlite",): path = actor_root / "private" / name path.touch(mode=0o600, exist_ok=True) path.chmod(0o600) processes: list[dict[str, Any]] = [] log_path = logs / "server.log" fd = os.open(log_path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600) try: with os.fdopen(fd, "ab") as log: fastapi = subprocess.Popen( [str(PYTHON), str(FASTAPI_MAIN)], cwd=project, env=env, stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True, ) processes.append(_process_record(fastapi.pid, "fastapi", fastapi_port)) server = subprocess.Popen( [str(NAO_SERVER), "serve", "--port", str(server_port), "--host", "127.0.0.1"], cwd=NAO_SERVER.parent, env=env, stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True, ) processes.append(_process_record(server.pid, "server", server_port)) except Exception: for record in processes: if _process_matches(record, actor_root): with contextlib.suppress(ProcessLookupError): os.killpg(record["pgid"], signal.SIGTERM) raise RuntimeErrorSafe("NAO 本地运行实例启动失败。") from None state = { "actorId": actor_root.name, "serverPort": server_port, "fastapiPort": fastapi_port, "fingerprint": fingerprint, "processes": processes, "startedAt": datetime.now(timezone.utc).isoformat(), } _atomic_json(actor_root / "instance.json", state) if not self._wait_server(server_port, timeout=30): _stop_instance(actor_root) raise RuntimeErrorSafe("NAO 本地服务未能就绪。") @staticmethod def _wait_server(port: int, timeout: float) -> bool: deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: with socket.create_connection(("127.0.0.1", port), timeout=0.25): return True except OSError: time.sleep(0.1) return False def stop_idle_all(self) -> int: stopped = 0 self.users_root.mkdir(parents=True, exist_ok=True, mode=0o700) with _file_lock(self.users_root / "registry.lock", blocking=True): for actor_root in _actor_dirs(self.users_root): with _file_lock(actor_root / "active.lock", blocking=False) as lock_fd: if lock_fd < 0: continue if _stop_instance(actor_root): stopped += 1 (actor_root / "access.json").unlink(missing_ok=True) return stopped def write_access_file(actor_root: Path, token: str, release_id: str) -> Path: access_path = actor_root / "access.json" _atomic_json(access_path, {"token": token, "releaseId": _uuid(release_id, "releaseId"), "queryUrl": RUNTIME_QUERY_URL}) return access_path def _make_opener(actor_root: Path, base_url: str) -> Any: cookie_path = actor_root / "private" / "cookies.txt" cookie_path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) jar = http.cookiejar.LWPCookieJar(str(cookie_path)) if cookie_path.exists(): try: jar.load(ignore_discard=True, ignore_expires=True) except (OSError, http.cookiejar.LoadError): raise RuntimeErrorSafe("NAO 本地会话凭据不可用。") from None opener = build_opener(HTTPCookieProcessor(jar)) opener._nao_cookie_jar = jar opener._nao_cookie_path = cookie_path opener._nao_base_url = base_url return opener def _save_cookies(client: Any) -> None: jar = getattr(client, "_nao_cookie_jar", None) path = getattr(client, "_nao_cookie_path", None) if jar is not None and path is not None: jar.save(ignore_discard=True, ignore_expires=True) Path(path).chmod(0o600) def _http_json(client: Any, request: Request, timeout: float = 20) -> tuple[int, Any]: try: with client.open(request, timeout=timeout) as response: body = response.read(1024 * 1024) return response.status, json.loads(body.decode("utf-8")) if body else {} except HTTPError as error: try: data = json.loads(error.read(1024 * 1024).decode("utf-8", "replace")) except Exception: data = None return error.code, data except (URLError, OSError, TimeoutError): raise RuntimeErrorSafe("NAO 本地服务暂不可用。") from None def _public_config(client: Any, base_url: str) -> dict[str, Any]: url = base_url + "/api/trpc/system.getPublicConfig?input=" + quote('{"json":null}') request = Request(url, headers={"Accept": "application/json", "Origin": base_url}) status, value = _http_json(client, request) result = value.get("result", {}).get("data", {}).get("json") if isinstance(value, dict) else None if status != 200 or not isinstance(result, dict) or not isinstance(result.get("enableUserSignup"), bool): raise RuntimeErrorSafe("NAO 本地注册状态无法验证。") return result def _authenticate(actor_root: Path, base_url: str) -> Any: private = _write_private_state(actor_root) client = _make_opener(actor_root, base_url) config = _public_config(client, base_url) body = {"email": private["email"], "password": private["password"], "name": "Quality Actor"} if config["enableUserSignup"]: endpoint = "sign-up/email" else: endpoint = "sign-in/email" request = Request(base_url + "/api/auth/" + endpoint, data=json.dumps(body, separators=(",", ":")).encode("utf-8"), headers={"Content-Type": "application/json", "Origin": base_url, "Accept": "application/json"}) status, _ = _http_json(client, request) if status >= 400 and endpoint == "sign-up/email": # A concurrent/bootstrap edge may have created the one permitted account. request = Request(base_url + "/api/auth/sign-in/email", data=json.dumps(body, separators=(",", ":")).encode("utf-8"), headers={"Content-Type": "application/json", "Origin": base_url, "Accept": "application/json"}) status, _ = _http_json(client, request) if status >= 400: raise RuntimeErrorSafe("NAO 本地账号认证失败。") _save_cookies(client) config = _public_config(client, base_url) if config.get("enableUserSignup") is not False: raise RuntimeErrorSafe("NAO 本地注册状态未关闭,已停止本轮分析。") return client def _stop_native(client: Any, base_url: str, chat_id: str) -> bool: body = json.dumps({"json": {"chatId": chat_id}}, separators=(",", ":")).encode("utf-8") request = Request(base_url + "/api/trpc/chat.stop", data=body, headers={"Content-Type": "application/json", "Origin": base_url, "Accept": "application/json"}, method="POST") try: with client.open(request, timeout=NATIVE_STOP_TIMEOUT_SECONDS) as response: response.read(1024 * 1024) return 200 <= response.status < 300 except Exception: return False def _story_files(client: Any, base_url: str, chat_id: str | None, tool_calls: list[dict[str, str]], tool_outputs: dict[str, Any]) -> tuple[str | None, str | None]: if not chat_id: return None, None # Later story calls may replace an earlier report. Follow invocation order, # newest first, and return the matching markdown/HTML pair from one call. for story in reversed(tool_calls): call_id = story.get("toolCallId") if story.get("tool") != "story" or not isinstance(call_id, str): continue output = tool_outputs.get(call_id) if not isinstance(output, dict) or output.get("success") is not True: continue markdown = output.get("code") if isinstance(output.get("code"), str) else None slug = output.get("id") if not isinstance(slug, str) or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", slug): return markdown, None query = {"json": {"chatId": chat_id, "storySlug": slug, "format": "html"}} url = base_url + "/api/trpc/story.download?input=" + quote(json.dumps(query, separators=(",", ":"))) request = Request(url, headers={"Accept": "application/json", "Origin": base_url}) try: with client.open(request, timeout=30) as response: envelope = json.loads(response.read(MAX_HTML_BYTES * 2).decode("utf-8")) export = envelope.get("result", {}).get("data", {}).get("json", {}) encoded = export.get("data") if isinstance(export, dict) else None if not isinstance(encoded, str) or len(encoded) > MAX_HTML_BYTES * 2: return markdown, None html = base64.b64decode(encoded, validate=True) if len(html) > MAX_HTML_BYTES: return markdown, None return markdown, html.decode("utf-8", "replace") except Exception: return markdown, None return None, None def _sse_request( *, client: Any, base_url: str, model: str, question: str, release_id: str, context: Any, chat_id: str | None, session_state: SessionState, session_id: str, actor_root: Path, emit: Callable[[dict[str, Any]], None], cancel_event: threading.Event | None = None, timeout: float = TURN_TIMEOUT_SECONDS, ) -> tuple[ConversationAccumulator, str, str | None, bool | None]: context_text = "" if context not in (None, "", [], {}): context_text = "\n会话上下文(仅用于理解本轮追问,不改变数据权限):\n" + json.dumps( context, ensure_ascii=False, separators=(",", ":"), allow_nan=False, ) prompt = (f"【仅内部查询约束,禁止向用户复述】本轮数据查询使用 releaseId={release_id},后续所有数据查询必须沿用该标识。" "如果当前数据下的证据不足,请如实说明,不要切换数据。用户可见的欢迎语、回答和报告只写业务问题、期间、范围、指标、结果与必要局限;不展示 releaseId、发布版本号、Story ID/version,也不说‘固定版本’。不要展示‘原始明细不发AI’等内部流程提示。" + context_text + "\n\n用户问题:\n" + question) body: dict[str, Any] = { "message": {"text": prompt}, "model": {"provider": "openaiCompatible", "modelId": model}, "timezone": "Asia/Shanghai", } if chat_id: body["chatId"] = chat_id request = Request(base_url + "/api/agent/", data=json.dumps(body, ensure_ascii=False).encode("utf-8"), headers={"Content-Type": "application/json", "Origin": base_url, "Accept": "text/event-stream"}, method="POST") def stop_or_shutdown(value: str | None) -> bool: stopped = bool(value and _stop_native(client, base_url, value)) if stopped: return True try: # A lost SSE transport must not leave a native model task running. return bool(_stop_instance(actor_root, grace_seconds=1.0)) except Exception: return False stopper = NativeStopController(stop_or_shutdown, timeout) bridge: threading.Thread | None = None bridge_done = threading.Event() if cancel_event is not None: def bridge_cancel() -> None: while not bridge_done.wait(0.05): if cancel_event.is_set(): stopper.request_cancel("user") return bridge = threading.Thread(target=bridge_cancel, name="nao-cancel-bridge", daemon=True) bridge.start() current_answer = {"value": "", "last_emit": 0.0} answer_lock = threading.Lock() def publish_answer(value: str) -> None: with answer_lock: current_answer["value"] = value now = time.monotonic() should_emit = now - current_answer["last_emit"] >= 0.35 if should_emit: current_answer["last_emit"] = now if should_emit: emit({"type": "answer", "answer": value}) accumulator = ConversationAccumulator(publish_answer) accumulator.chat_id = chat_id stopper.set_chat_id(chat_id) stopper.start() transport_failed = False try: # urllib's timeout also applies to each blocking SSE read. Keep a generous # idle interval; the independent watchdog enforces the 10-minute turn cap. response = client.open(request, timeout=SSE_IDLE_TIMEOUT_SECONDS) stopper.set_response(response) with response: for raw in response: if stopper.cause: break line = raw.decode("utf-8", "replace").strip() if not line.startswith("data:"): continue data = line[5:].strip() if data == "[DONE]": accumulator.done_received = True break try: event = json.loads(data) except json.JSONDecodeError: continue if not isinstance(event, dict): continue previous_chat = accumulator.chat_id accumulator.feed(event) if accumulator.chat_id and accumulator.chat_id != previous_chat: session_state.set_chat_id(session_id, accumulator.chat_id) stopper.set_chat_id(accumulator.chat_id) kind = event.get("type") if kind == "tool-input-available": count = len(accumulator.tool_calls) emit({"type": "progress", "message": "正在调用质量分析工具…", "toolCount": count}) # A clean native finish or [DONE] closes the task. A plain EOF can leave # the upstream model task alive, so stop it before returning to Java. if not stopper.cause and not accumulator.done_received and "stop" not in accumulator.finish_reasons: stopper.request_cancel("transport") except Exception: transport_failed = True stopper.request_cancel("transport") finally: stopper.close() if bridge and bridge.is_alive(): bridge_done.set() bridge.join(timeout=0.1) cancel_cause = stopper.cause status, stop_reason = accumulator.status(cancel_cause=cancel_cause, stop_succeeded=stopper.stop_succeeded, transport_failed=transport_failed) # status and stop reason are attached by caller; keep return transport-neutral. if status == "COMPLETED": # The Java result mapper uses the native finish reason to recognize a full turn. stop_reason = stop_reason or "stop" elif status == "CANCELLED": stop_reason = "用户停止" if cancel_cause == "user" else "超时停止" return accumulator, status, stop_reason, stopper.stop_succeeded def _normalize_request(raw: Any) -> dict[str, Any]: if not isinstance(raw, dict): raise ValueError("请求必须是 JSON 对象。") turn_id = _uuid(raw.get("turnId"), "turnId") session_id = _uuid(raw.get("sessionId"), "sessionId") actor_id = _uuid(raw.get("actorId"), "actorId") release_id = _uuid(raw.get("releaseId"), "releaseId") token = raw.get("accessToken") question = raw.get("question") if not isinstance(token, str) or not token.strip() or len(token) > 4096 or "\n" in token or "\r" in token: raise ValueError("accessToken 无效。") if not isinstance(question, str) or not question.strip() or len(question) > MAX_QUESTION_CHARS: raise ValueError("question 无效或超过长度限制。") context = raw.get("context") try: context_size = len(json.dumps(context, ensure_ascii=False, separators=(",", ":"))) if context is not None else 0 except (TypeError, ValueError): raise ValueError("context 必须是 JSON 数据。") from None if context_size > MAX_CONTEXT_CHARS: raise ValueError("context 超过允许大小。") return {"turnId": turn_id, "sessionId": session_id, "actorId": actor_id, "releaseId": release_id, "accessToken": token, "question": question.strip(), "context": context} def _emit(stream: Any, lock: threading.Lock, value: dict[str, Any]) -> None: with lock: stream.write(json.dumps(value, ensure_ascii=False, separators=(",", ":"), allow_nan=False) + "\n") stream.flush() def _input_cancel_listener(stream: Any, on_cancel: Callable[[], None]) -> threading.Thread: def listen() -> None: for line in stream: try: value = json.loads(line) except json.JSONDecodeError: continue if isinstance(value, dict) and value.get("type") == "cancel": on_cancel() return thread = threading.Thread(target=listen, name="nao-worker-cancel-input", daemon=True) thread.start() return thread def run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any = sys.stdin) -> dict[str, Any]: output_lock = threading.Lock() emit = lambda value: _emit(stdout, output_lock, value) actor_root: Path | None = None actor_fd: int | None = None access_path: Path | None = None try: request = _normalize_request(payload) emit({"type": "progress", "message": "正在准备独立质量分析环境…"}) manager = RuntimeManager() actor_root, actor_fd = manager.acquire_actor(request["actorId"]) access_path = write_access_file(actor_root, request["accessToken"], request["releaseId"]) del request["accessToken"] model_base, model_key, model_name, base_url = manager.ensure_instance(actor_root, request["sessionId"]) # The model process receives only NAO's configured model credential. The delegated # user token remains in access.json, read only by the MCP shim. del model_base, model_key client = _authenticate(actor_root, base_url) state = SessionState(actor_root, request["actorId"]) native_chat_id = state.get_chat_id(request["sessionId"]) emit({"type": "progress", "message": "正在查询质量中心并整理结论…"}) cancel_event = threading.Event() def request_cancel() -> None: cancel_event.set() _input_cancel_listener(stdin, request_cancel) accumulator, status, stop_reason, stop_succeeded = _sse_request( client=client, base_url=base_url, model=model_name, question=request["question"], release_id=request["releaseId"], context=request["context"], chat_id=native_chat_id, session_state=state, session_id=request["sessionId"], emit=emit, actor_root=actor_root, cancel_event=cancel_event, ) answer = accumulator.answer if answer: emit({"type": "answer", "answer": answer}) if status == "COMPLETED": report_markdown, report_html = _story_files(client, base_url, accumulator.chat_id, accumulator.tool_calls, accumulator.tool_outputs) else: report_markdown, report_html = None, None if status == "COMPLETED" and not answer: status, stop_reason = "FAILED", "empty_answer" result = { "type": "result", "status": status, "engine": "NAO", "answer": answer, "nativeChatId": accumulator.chat_id, "toolCalls": accumulator.tool_calls, "stopReason": stop_reason, "nativeStopSucceeded": stop_succeeded, "finishReason": accumulator.finish_reasons[-1] if accumulator.finish_reasons else None, } if report_markdown is not None: result["reportMarkdown"] = report_markdown if report_html is not None: result["reportHtml"] = report_html emit(result) return result except RuntimeErrorSafe as error: result = {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "", "toolCalls": [], "stopReason": "runtime_error", "message": str(error)} emit(result) return result except (ValueError, OSError, RuntimeError): result = {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "", "toolCalls": [], "stopReason": "runtime_error", "message": "本轮分析无法启动,请检查本机运行状态后重试。"} emit(result) return result finally: if access_path is not None: access_path.unlink(missing_ok=True) if actor_fd is not None: RuntimeManager().release_actor(actor_fd) def _read_initial_request(stdin: Any) -> dict[str, Any]: line = stdin.readline() if not line: raise ValueError("缺少首行请求。") try: value = json.loads(line) except json.JSONDecodeError: raise ValueError("首行请求不是有效 JSON。") from None if not isinstance(value, dict) or value.get("type") == "cancel": raise ValueError("首行必须是请求对象。") return value def main(argv: list[str] | None = None) -> int: os.umask(0o077) parser = argparse.ArgumentParser(add_help=True) parser.add_argument("--stop-all", action="store_true", help="仅停止已验证且空闲的NAO质量实例") args = parser.parse_args(argv) if args.stop_all: count = RuntimeManager().stop_idle_all() sys.stdout.write(json.dumps({"stopped": count}, separators=(",", ":")) + "\n") return 0 try: request = _read_initial_request(sys.stdin) run_turn(request) return 0 except ValueError as error: _emit(sys.stdout, threading.Lock(), {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "", "toolCalls": [], "stopReason": "invalid_request", "message": str(error)}) return 2 except Exception: _emit(sys.stdout, threading.Lock(), {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "", "toolCalls": [], "stopReason": "runtime_error", "message": "本轮分析无法启动,请检查本机运行状态后重试。"}) return 1 if __name__ == "__main__": raise SystemExit(main())