"""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 copy import base64 import contextlib import fcntl import hashlib import http.cookiejar import json import math import os import re import secrets import shutil import signal import socket import sqlite3 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_PORT = os.environ.get("QUALITY_ANALYSIS_RUNTIME_QUERY_PORT", "5281") if _RUNTIME_QUERY_PORT not in {"5281", "5283", "5284", "5285"}: raise RuntimeError("质量分析授权查询端口无效。") RUNTIME_QUERY_URL = f"http://127.0.0.1:{_RUNTIME_QUERY_PORT}/api/analysis/runtime/query" MAX_INSTANCES = 2 from nao_runtime_policy import MAX_OUTPUT_TOKENS, TURN_TIMEOUT_SECONDS, turn_scope, native_timeout SSE_IDLE_TIMEOUT_SECONDS = 60 NATIVE_STOP_TIMEOUT_SECONDS = 4.0 CHAT_ID_WAIT_SECONDS = 0.75 SUPPLEMENT_QUERY_TIMEOUT_SECONDS = 30 SOURCE_VERIFICATION_TIMEOUT_SECONDS = 180 SOURCE_FINALIZATION_RESERVE_SECONDS = 90 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", "/api/analysis/model-authorization", }, "POST": {"/api/analysis/tools/details", "/api/analysis/defects", "/api/analysis/changes", "/api/analysis/investigate", "/api/analysis/protocol/task", "/api/analysis/protocol/queries", "/api/analysis/protocol/history", "/api/analysis/protocol/exploration/steps", "/api/analysis/source/members", "/api/analysis/source/values"}, } class RuntimeErrorSafe(RuntimeError): """An error safe to return without exposing credentials or request bodies.""" def __init__(self, message, *, retryable=False, http_status=None): super().__init__(message) self.retryable = retryable is True self.http_status = http_status def _require_private_regular_file(path: Path, *, missing_ok: bool = True) -> bool: if path.is_symlink(): raise RuntimeErrorSafe("私有运行文件路径无效。") try: info = path.stat(follow_symlinks=False) except FileNotFoundError: if missing_ok: return False raise RuntimeErrorSafe("私有运行文件不可用。") from None except OSError: raise RuntimeErrorSafe("私有运行文件不可用。") from None if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077: raise RuntimeErrorSafe("私有运行文件权限无效。") return True 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 _atomic_bytes(path: Path, value: bytes, *, mode: int = 0o600) -> None: """Atomically replace a private file without ever following its final component.""" path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) if path.is_symlink(): raise RuntimeErrorSafe("NAO project 文件路径无效。") 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 | getattr(os, "O_NOFOLLOW", 0), mode) try: with os.fdopen(fd, "wb") as stream: stream.write(value) stream.flush() os.fsync(stream.fileno()) os.replace(temp, path) 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 def _runtime_identity(runtime_root: Path) -> tuple[str, str | None]: """Return (actor_id, session_id) for either a new session root or a legacy actor root.""" root = Path(runtime_root).absolute() if root.parent.name == "sessions": actor_id = _uuid(root.parent.parent.name, "actorId") return actor_id, _uuid(root.name, "sessionId") return _uuid(root.name, "actorId"), None def _reject_symlink_components(path: Path, stop_at: Path) -> None: """Reject symlinks in a runtime path between path and its configured root.""" absolute = Path(os.path.abspath(path)) stop = Path(os.path.abspath(stop_at)) try: relative = absolute.relative_to(stop) except ValueError: raise ValueError("运行路径越界。") from None current = stop if current.is_symlink(): raise ValueError("运行路径无效。") for part in relative.parts: current = current / part if current.is_symlink(): raise ValueError("运行路径无效。") @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, expected_session_id: str | None = None, ) -> AccessSnapshot: """Read and validate a private access file without following path escapes. Session-bound callers must provide both identity fields. The actor-root form remains readable only for old installations and existing offline callers. """ root = _safe_runtime_root(Path(runtime_root)) absolute = Path(os.path.abspath(Path(access_path))) _reject_symlink_components(absolute, root) if expected_session_id is not None: if expected_actor_id is None: raise ValueError("会话访问凭据必须绑定 actorId。") actor_id = _uuid(expected_actor_id, "actorId") session_id = _uuid(expected_session_id, "sessionId") session_root = root / actor_id / "sessions" / session_id if absolute != session_root / "access.json": raise ValueError("访问凭据文件路径与当前会话不匹配。") else: actor_dir = absolute.parent if actor_dir.parent != root: raise ValueError("访问凭据文件路径越界。") actor_id = _uuid(actor_dir.name, "actorId") session_id = None if expected_actor_id is not None and actor_id != _uuid(expected_actor_id, "actorId"): raise ValueError("访问凭据与当前 actor 不匹配。") if absolute.name != "access.json" or not absolute.parent.is_dir(): raise ValueError("访问凭据文件路径无效。") private_dirs = [root, root / actor_id] if session_id is not None: private_dirs.extend((root / actor_id / "sessions", root / actor_id / "sessions" / session_id)) for directory in private_dirs: try: directory_info = directory.stat(follow_symlinks=False) except OSError: raise ValueError("访问凭据目录不可用。") from None if not stat.S_ISDIR(directory_info.st_mode) or directory_info.st_mode & 0o077: raise ValueError("访问凭据目录权限无效。") try: fd = os.open(absolute, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) with os.fdopen(fd, "r", encoding="utf-8") as stream: info = os.fstat(stream.fileno()) if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077: raise ValueError("访问凭据文件权限无效。") raw = json.load(stream) except ValueError: raise except (OSError, json.JSONDecodeError): raise ValueError("访问凭据文件内容无效。") from None if not isinstance(raw, dict): raise ValueError("访问凭据文件内容无效。") if session_id is not None: if raw.get("actorId") != actor_id or raw.get("sessionId") != session_id: raise ValueError("访问凭据与当前会话不匹配。") elif raw.get("actorId") not in (None, actor_id): raise ValueError("访问凭据与当前 actor 不匹配。") 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 != int(_RUNTIME_QUERY_PORT) 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 load_access_snapshot( access_path: str | Path, *, runtime_root: Path = USERS_ROOT, expected_actor_id: str, expected_session_id: str, ) -> AccessSnapshot: """Strict loader for credentials owned by one actor/session pair.""" return load_access_file(access_path, runtime_root=runtime_root, expected_actor_id=expected_actor_id, expected_session_id=expected_session_id) 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, expected_session_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.expected_session_id = expected_session_id self.opener = opener or build_opener() self.timeout = timeout self.deadline_reserve_seconds: float | None = None 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, expected_session_id=self.expected_session_id, ) return self def call(self, path: str, payload: dict[str, Any] | None = None, method: str | None = None, *, node_id: str | None = None, scope_id: 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, expected_session_id=self.expected_session_id, ) _check_release_pin(path, payload, snapshot.release_id) if payload is not None and not isinstance(payload, dict): raise ValueError("质量查询参数格式无效。") if (node_id is None) != (scope_id is None): raise ValueError("协议节点与范围标识必须同时提供。") if node_id is not None: if (parsed_path.path == "/api/analysis/protocol/queries" or not isinstance(node_id, str) or not re.fullmatch(r"[a-z][a-z0-9_-]{0,31}", node_id) or not isinstance(scope_id, str) or not scope_id or len(scope_id) > 180): raise ValueError("协议节点或范围标识无效。") wrapper = {"token": snapshot.token, "method": verb, "path": path, "payload": payload} if node_id is not None: wrapper["nodeId"] = node_id wrapper["scopeId"] = scope_id 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", ) timeout = self.timeout if parsed_path.path == '/api/analysis/model-authorization': timeout = min(timeout, 5) if self.deadline_reserve_seconds is not None: from nao_runtime_policy import remaining_seconds available = remaining_seconds() - self.deadline_reserve_seconds if available <= 0: raise RuntimeErrorSafe('原表核验时间不足,已保留未覆盖内容。') timeout = min(timeout, available) try: with self.opener.open(request, timeout=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]"), retryable=error.code in (429, 502, 503, 504), http_status=error.code) from None raise RuntimeErrorSafe(f"质量数据分析平台查询暂不可用(HTTP {error.code})。", retryable=error.code in (429, 502, 503, 504), http_status=error.code) from None except (URLError, OSError, TimeoutError): raise RuntimeErrorSafe("质量数据分析平台查询暂不可用,请确认本机质量服务后重试。", retryable=True) 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) def public_tool_call(tool: str, raw_input: Any) -> dict[str, Any]: """Operation trace only; never publish model reasoning, file contents or credentials.""" value = raw_input if isinstance(raw_input, dict) else {} if tool == "mcp_call" and value.get("tool") == "quality_data": args = value.get("arguments") if isinstance(value.get("arguments"), dict) else {} labels = {"catalog": "确认可用数据", "query": "查询质量指标", "compare": "比较期间变化", "top": "查询重点对象排名", "changes": "追查变化较大对象", "defects": "分析具体缺陷", "details": "核查来源明细", "investigate": "核对全量明细与原表"} allowed = {"mode", "scope", "period", "comparisonPeriod", "filters", "view", "stage", "dimension", "defect", "category", "limit", "sort", "action", "offset", "recordFilter", "sourceRevisionId"} nested = {"year", "month", "quarter", "monthStart", "monthEnd", "department", "group", "product", "projectGroup", "includeUnassigned"} public_input = {} for key, item in args.items(): if key not in allowed: continue if isinstance(item, dict): public_input[key] = {k: v for k, v in item.items() if k in nested and isinstance(v, (str, int, float, bool))} elif isinstance(item, (str, int, float, bool)): public_input[key] = item[:500] if isinstance(item, str) else item return {"label": labels.get(args.get("mode"), "查询质量数据"), "input": public_input} if tool in ("read", "list", "grep", "glob", "search"): return {"label": "读取分析资料" if tool == "read" else "查找分析资料"} if "story" in tool: return {"label": "生成或更新分析报告"} return {"label": {"python": "执行数据分析", "sql": "查询分析数据"}.get(tool, "执行分析工具")} def public_progress_text(value: str) -> str: value = re.sub(r'quality-turn:[0-9a-f-]{36}', '', value, flags=re.I) value = re.sub(r"(?i)(?:bearer\s+)[A-Za-z0-9._~+/-]+", "Bearer [REDACTED]", value) return re.sub(r"(?i)(api[_-]?key|password|token|secret|authorization)(\s*[=::]\s*)[^\s,,;;]+", r"\1\2[REDACTED]", value)[:8000] def public_evidence_checks(output: Any) -> dict[str, str]: """Only verified tool outcomes update coverage warnings; never assistant text.""" if isinstance(output,str): try: output=json.loads(output) except ValueError:return {} if not isinstance(output,dict):return {} if isinstance(output.get("content"),list): checks={} for part in output["content"]: if isinstance(part,dict) and isinstance(part.get("text"),str):checks.update(public_evidence_checks(part["text"])) return checks checks={} def add(rows,period,proof=None): for row in rows if isinstance(rows,list) else []: if not isinstance(row,dict):continue key=hashlib.sha256(json.dumps([output.get("releaseId"),output.get("stage"),period,output.get("filters"),row.get("category")],ensure_ascii=False,sort_keys=True).encode()).hexdigest()[:16] if output.get("kind")=="quality_investigation": expected=(row.get("expectedTotalsOnly") or {}).get("rows",0)>0 and row.get("unexpectedDifferenceRows")==0 and row.get("requiresFurtherInvestigation") is False status="unresolved" if proof is not None and (proof.get("complete") is not True or proof.get("allSelectedValuesMatchStored") is not True) else "expected" if expected else "unresolved" if row.get("requiresFurtherInvestigation") else "checked" else: status="expected" if row.get("reconciliationStatus")=="EXPECTED_TOTALS_ONLY" else "unresolved" if row.get("reconciliationStatus")!="RECONCILED" else "checked" checks[key]=status if output.get("kind")=="quality_investigation": for name,period in (("current",output.get("period")),("comparison",output.get("comparisonPeriod"))): side=output.get(name) if isinstance(side,dict):add(side.get("categories"),period,side.get("sourceVerification")) elif output.get("kind")=="quality_fine_defects": add(output.get("categoryReconciliation"),output.get("period"));add(output.get("comparisonCategoryReconciliation"),output.get("comparisonPeriod")) return checks def public_tool_failure(output: Any) -> str: if isinstance(output,str): try:output=json.loads(output) except ValueError:return "工具调用未成功,未提供可展示的错误原因" if isinstance(output,dict): error=output.get("error") if isinstance(error,dict) and error.get("code")=="invalid_or_unavailable" and isinstance(error.get("message"),str): return "查询未成功:"+public_progress_text(error["message"])[:450] if isinstance(output.get("structuredContent"),dict):return public_tool_failure(output["structuredContent"]) for part in output.get("content",[]) if isinstance(output.get("content"),list) else []: if isinstance(part,dict) and isinstance(part.get("text"),str):return public_tool_failure(part["text"]) return "工具调用未成功,未提供可展示的错误原因" def public_tool_result(output: Any) -> str: """Summarize only verified response structure; never forward raw tool payloads.""" if isinstance(output, str): try: output = json.loads(output) except ValueError: return "工具已返回;未提供可展示的结构化摘要" if not isinstance(output, dict): return "工具已返回结果" content = output.get("content") if isinstance(content, list): for part in content: if isinstance(part, dict) and isinstance(part.get("text"), str): try: parsed = json.loads(part["text"]) if isinstance(parsed, dict): return public_tool_result(parsed) except ValueError: pass parts = [] if output.get("kind") == "quality_investigation": current = output.get("current", {}) parts.append(f"全量统计 {current.get('totalRows', 0)} 行") proof = current.get("sourceVerification") or {} if proof: parts.append(f"原表核验 {proof.get('verifiedRows', 0)}/{proof.get('expectedRows', 0)} 行,差异 {proof.get('mismatchRows', 0)} 行") for key in ("evidenceRef", "evidenceId"): ref = output.get(key) if isinstance(ref, str) and re.fullmatch(r"[A-Za-z0-9_.:/#-]{1,160}", ref): parts.append("证据:" + ref); break if isinstance(output.get("rows"), list): parts.append(f"返回 {len(output['rows'])} 条结果") if isinstance(output.get("sourceRows"), int): parts.append(f"来源 {output['sourceRows']} 行") coverage = output.get("coverage") if isinstance(coverage, dict) and coverage.get("available") is False: parts.append("当前来源覆盖不足") reconciliation = [row for key in ("categoryReconciliation", "comparisonCategoryReconciliation") for row in (output[key] if isinstance(output.get(key), list) else [])] if reconciliation and any(isinstance(row, dict) and row.get("reconciliationStatus") not in ("RECONCILED", "EXPECTED_TOTALS_ONLY") for row in reconciliation): parts.append("细分与正式大类未对账:仅作为变化线索,不能据此判定正式报废主因") if output.get('kind') == 'quality_changes' and output.get('completeRanking') is True: parts.append('按完整授权范围排序,展示前' + str(len(output.get('rows', []))) + '项') elif output.get("rowsTruncated") is True or output.get("truncated") is True or output.get("pairedTruncated") is True: parts.append("仅覆盖返回集合,不代表全量排名") def number(value: Any) -> float | None: if isinstance(value, (int, float)) and not isinstance(value, bool): return float(value) if math.isfinite(value) else None return None def metric_summary(snapshot: Any, prefix: str = "") -> None: if not isinstance(snapshot, dict): return rate = number(snapshot.get("compositeRate")) if rate is not None: parts.append(f"{prefix}综合合格率 {rate * 100:.2f}%") for stage, label in (("black", "黑片"), ("bright", "亮片")): metric = snapshot.get(stage) if not isinstance(metric, dict): continue weight = number(metric.get("scrapWeight")) if weight is not None: parts.append(f"{prefix}{label}报废 {weight:,.1f} kg") metric_summary(output.get("summary")) metric_summary(output.get("current"), "本期") metric_summary(output.get("comparison"), "对照期") rows = output.get("rows") if isinstance(rows, list) and rows and isinstance(rows[0], dict): if output.get('kind') == 'quality_changes' and output.get('completeRanking') is True: first = rows[0] rate = number((first.get('current') or {}).get('rate')) if isinstance(first.get('label'),str) and rate is not None: prefix = '本期率最低首项' if output.get('sort') == 'current_rate' else '排序首项' parts.append(prefix + ':' + public_progress_text(first['label'])[:160] + f',本期率 {rate*100:.2f}%') label, weight = rows[0].get("label"), number(rows[0].get("weight")) if isinstance(label, str) and weight is not None: prefix = "来源细分" if output.get("grain") == "source_defect_weight" else "返回项" parts.append(f"首条{prefix}:{public_progress_text(label)[:160]} {weight:,.1f} kg") return ";".join(parts) or "工具已返回结果;可继续核查" def tool_operation_key(tool: str, raw_input: Any, *, complete_defects: bool = False) -> str: value = json.loads(json.dumps(raw_input)) if isinstance(raw_input, dict) else {} if tool == "read" and set(value) == {"path"} and value["path"] in ("/RULES.md", "/agent/mcps/quality/quality_data.json"): # The native provider sometimes uses `path` before correcting it to read's # real `file_path` parameter. Only the same known static file can recover it. value = {"file_path": value["path"]} if tool == "mcp_call" and value.get("tool") == "quality_data" and isinstance(value.get("arguments"), dict): tool, value = "quality_data", value["arguments"] if tool == "quality_data": value = {key: item for key, item in value.items() if item is not None} args = value if args.get("mode") == "defects" and "dimension" not in args and "defect" not in args: args["dimension"] = "defect" if args.get("scope") == "overall" and isinstance(args.get("filters"), dict) and args["filters"].get("group"): args.pop("scope") if args.get("mode") == "compare" and args.get("view") == "overall": args.pop("view") if args.get("mode") == "changes" and args.get("view") in ("department", "group", "product", "projectGroup") and "dimension" not in args: args["dimension"] = args.pop("view") if args.get('mode') == 'details' and 'offset' in args: # A pagination request rejected by the sample-only tool can recover # only through the same full-row operation, scope, stage and page. args['mode'] = 'investigate' args['action'] = 'rows' args.setdefault('recordFilter', 'all') if args.get('mode') == 'investigate' and args.get('action') == 'source': if args.get('recordFilter') == 'all': args.pop('recordFilter') if args.get('offset') == 0: args.pop('offset') if args.get("mode") == "defects" and args.get("dimension") == "defect": if args.get("defect") == "all": args.pop("defect") if complete_defects: args.pop("limit", None) encoded = json.dumps([tool, value], sort_keys=True, ensure_ascii=False) return hashlib.sha256(encoded.encode()).hexdigest() def complete_defect_result(output: Any) -> bool: if isinstance(output,str): try:output=json.loads(output) except ValueError:return False if not isinstance(output,dict):return False if isinstance(output.get("content"),list): return any(complete_defect_result(part.get("text")) for part in output["content"] if isinstance(part,dict)) return (output.get("kind")=="quality_fine_defects" and output.get("rankingComplete") is True and output.get("truncated") is False and output.get("pairedTruncated",False) is False) class ConversationAccumulator: """Keep provider-emitted reasoning separate from answers and 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, Any]] = [] self.tool_outputs: dict[str, Any] = {} self.tool_error = False self.unattributed_tool_error = False self.tool_operation_keys: dict[int, str] = {} self.complete_defect_keys: dict[int, str] = {} self.error_event = False self.on_answer = on_answer self.chat_id: str | None = None self.done_received = False self.reasoning_blocks: dict[str, str] = {} self.reasoning_truncated = False self.timeline: list[dict[str, Any]] = [] self.timeline_text: dict[str, dict[str, Any]] = {} self.timeline_segments: dict[int, str] = {} def record_event(self, kind: str, **fields: Any) -> dict[str, Any]: entry = {"sequence": (self.timeline[-1]["sequence"] if self.timeline else 0) + 1, "kind": kind, "at": datetime.now(timezone.utc).isoformat(), **fields} if len(self.timeline) < 3000: self.timeline.append(entry) return entry 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 == "reasoning-delta": block_id = event.get("id") if isinstance(event.get("id"), str) else "reasoning" delta = event.get("delta") if isinstance(delta, str): remaining = max(0, 200_000 - sum(len(value) for value in self.reasoning_blocks.values())) self.reasoning_blocks[block_id] = self.reasoning_blocks.get(block_id, "") + delta[:remaining] self.reasoning_truncated = self.reasoning_truncated or len(delta) > remaining 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 segment = self.timeline_text.get(block_id) if segment is None or not self.timeline or self.timeline[-1] is not segment: segment = self.record_event("assistant", text="") self.timeline_text[block_id] = segment sequence = segment["sequence"] self.timeline_segments[sequence] = self.timeline_segments.get(sequence, "") + delta segment["text"] = public_progress_text(self.timeline_segments[sequence]) 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 in ("tool-input-available", "tool-input-error"): tool_name = event.get("toolName") if isinstance(tool_name, str): call_id = event.get("toolCallId") if isinstance(call_id, str) and any(c.get("toolCallId") == call_id for c in self.tool_calls): return call = {"tool": tool_name, "status": "started", "startedAt": datetime.now(timezone.utc).isoformat(), **public_tool_call(tool_name, event.get("input"))} if isinstance(call_id, str): call["toolCallId"] = call_id self.tool_calls.append(call) self.record_event("tool_start", tool=tool_name, toolCallId=call.get("toolCallId", ""), label=call["label"], input=call.get("input", {}), status="started") if isinstance(event.get("input"), dict): self.tool_operation_keys[id(call)] = tool_operation_key(tool_name, event["input"]) self.complete_defect_keys[id(call)] = tool_operation_key(tool_name,event["input"],complete_defects=True) if kind == "tool-input-error": self.tool_error = True call.update(status="failed",finishedAt=datetime.now(timezone.utc).isoformat(),outputSummary="工具参数未通过校验") self.record_event("tool_result",tool=tool_name,toolCallId=call.get("toolCallId", ""),label=call["label"],status="failed",text=call["outputSummary"]) 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 output_failed = _contains_error_flag(output) matched = False if output_failed: 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": matched = True item["status"] = "failed" if _contains_error_flag(output) else "completed" item["finishedAt"] = datetime.now(timezone.utc).isoformat() item["outputSummary"] = public_tool_failure(output) if item["status"] == "failed" else public_tool_result(output) self.record_event("tool_result", tool=item["tool"], toolCallId=item.get("toolCallId", ""), label=item["label"], status=item["status"], text=item["outputSummary"], evidenceChecks=public_evidence_checks(output) if not output_failed else {}) if not output_failed: key = self.tool_operation_keys.get(id(item)) complete_key=self.complete_defect_keys.get(id(item)) if complete_defect_result(output) else None for previous in self.tool_calls: if previous is item: break same=(key is not None and self.tool_operation_keys.get(id(previous))==key) complete_same=(complete_key is not None and self.complete_defect_keys.get(id(previous))==complete_key) if previous["status"] == "failed" and (same or complete_same): previous["recovered"] = True previous["outputSummary"] = "调整调用后已重试成功" break if output_failed and not matched: self.unattributed_tool_error = True elif kind in ("tool-output-error", "error"): self.tool_error = True matched = False 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"] in ("started", "failed"): matched = True if item["status"] == "failed": break item["status"] = "failed" item["finishedAt"] = datetime.now(timezone.utc).isoformat() item["outputSummary"] = "本次调用未成功" self.record_event("tool_result", tool=item["tool"], toolCallId=call_id, label=item["label"], status="failed", text="本次调用未成功") break if not matched: self.unattributed_tool_error = True if kind == "error": self.error_event = True elif kind == "[DONE]": self.done_received = True @property def model_reasoning(self) -> str: return "\n\n".join(self.reasoning_blocks.values()) @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 "" @property def final_answer_ready(self) -> bool: if not self.answer or any(call.get('status') == 'started' for call in self.tool_calls): return False if not self.tool_calls: return True last_tool = max((i for i, entry in enumerate(self.timeline) if entry.get('kind') in ('tool_start', 'tool_result')), default=-1) return any(entry.get('kind') == 'assistant' and str(entry.get('text') or '').strip() for entry in self.timeline[last_tool + 1:]) 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 == "coverage_ready": return ("PARTIAL" if self.answer else "FAILED", "coverage_ready") 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 and (self.unattributed_tool_error or any(call["status"] == "failed" and not call.get("recovered") for call in self.tool_calls)): return ("PARTIAL" if self.answer else "FAILED", "mcp_error") if (self.final_answer_ready and self.finish_reasons[-1] == 'stop' and stop_succeeded is not False): return "COMPLETED", "stop" return ("PARTIAL" if self.answer else "FAILED", "incomplete") def _contains_error_flag(value: Any) -> bool: if isinstance(value, dict): if (any(value.get(flag) is True for flag in ('isError', 'mcpValidationError', 'mcpAuthRequired')) or value.get('success') is False or value.get('error')): return True # Inspect only known response envelopes. Rows, source cells, notes and # arbitrary JSON strings are business data, not nested tool outcomes. content = value.get('content') if isinstance(content, list) and any(isinstance(part, dict) and part.get('type') == 'text' and _contains_error_flag(part.get('text')) for part in content): return True return _contains_error_flag(value['structuredContent']) if isinstance(value.get('structuredContent'), dict) else False if isinstance(value, list): return any(isinstance(part, dict) and part.get('type') == 'text' and _contains_error_flag(part.get('text')) for part 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, completion_ready: Callable[[], bool] | None = None) -> None: self.stop_call = stop_call self.timeout = timeout self.completion_ready = completion_ready 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", "coverage_ready"): 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 next_coverage_check = time.monotonic() + 1 while not self.finished.is_set(): now = time.monotonic() if self.completion_ready is not None and now >= next_coverage_check: next_coverage_check = now + 2 try: if self.completion_ready(): self.request_cancel("coverage_ready") break except (OSError, ValueError, RuntimeError, KeyError, TypeError): pass 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-session mapping to native chat IDs, with read-only legacy fallback.""" def __init__(self, runtime_root: Path, actor_id: str, session_id: str | None = None) -> None: self.actor_id = _uuid(actor_id, "actorId") actual_actor, path_session = _runtime_identity(runtime_root) if actual_actor != self.actor_id: raise ValueError("会话状态与当前 actor 不匹配。") self.session_id = _uuid(session_id, "sessionId") if session_id is not None else path_session if path_session is not None and self.session_id != path_session: raise ValueError("会话状态与当前 session 不匹配。") self.runtime_root = Path(runtime_root) self.path = (self.runtime_root / "private" / "native-chat.json" if self.session_id else self.runtime_root / "private" / "sessions.json") def get_chat_id(self, session_id: str) -> str | None: if self.session_id and session_id != self.session_id: return None if self.path.is_symlink(): return None try: value = _read_json(self.path) except (OSError, json.JSONDecodeError): value = None if self.session_id: if (isinstance(value, dict) and value.get("actorId") == self.actor_id and value.get("sessionId") == self.session_id): result = value.get("nativeChatId") if isinstance(result, str) and result: return result 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: session_id = _uuid(session_id, "sessionId") if self.session_id: if session_id != self.session_id or not isinstance(chat_id, str) or not chat_id: raise ValueError("会话状态与当前 session 不匹配。") self.path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) if self.path.is_symlink(): raise RuntimeErrorSafe("会话状态路径无效。") _atomic_json(self.path, {"actorId": self.actor_id, "sessionId": session_id, "nativeChatId": chat_id}) return 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(*, include_options=False): try: from nao_model_settings import model_configuration base, key, model, options = model_configuration() 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,options) if include_options else (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", ROOT / "scripts" / "nao_model_gate.py", ROOT / "scripts" / "nao_evidence_finalizer.py", ROOT / "scripts" / "nao_evidence_report.py", ROOT / "scripts" / "nao_weight_ranking.py", ROOT / "scripts" / "nao_metric_request.py", ROOT / "scripts" / "nao_analysis_coverage.py", ROOT / "scripts" / "nao_combined_ranking.py", ROOT / "scripts" / "nao_query_contract.py", ROOT / "scripts" / "nao_answer_focus.py", ROOT / "scripts" / "nao_changes.py", ROOT / "scripts" / "nao_query_plan.py", ROOT / "scripts" / "nao_agent_contract.py", ROOT / "scripts" / "nao_plan_recovery.py", ROOT / "scripts" / "nao_protocol_execution.py", ROOT / "scripts" / "nao_agent_task.py", TEMPLATES / 'agent/prompts/system-native.md', ROOT / 'scripts/nao_native_run.py', ROOT / 'scripts/nao_native_exploration.py', ROOT / 'scripts/nao_native_planning.py', ROOT / 'scripts/nao_native_answer.py', ROOT / "scripts" / "nao_runtime_policy.py", ROOT / "backend/src/main/resources/analysis-runtime-policy.json"] 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(str(native_output_budget()).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" if (actor_root.is_symlink() or private_path.parent.is_symlink()): raise RuntimeErrorSafe("私有运行目录路径无效。") private_path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(private_path.parent, 0o700) if private_path.exists(): _require_private_regular_file(private_path, missing_ok=False) 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 native_output_budget() -> int: return MAX_OUTPUT_TOKENS def _write_project(actor_root: Path, session_id: str, model_name: str, *, refresh_templates: bool, refresh_mcp: bool, backup_schema: bool, expected_actor_id: str | None = None) -> None: session_id = _uuid(session_id, "sessionId") path_actor_id, path_session_id = _runtime_identity(actor_root) actor_id = _uuid(expected_actor_id, "actorId") if expected_actor_id is not None else path_actor_id if actor_id != path_actor_id or (path_session_id is not None and path_session_id != session_id): raise RuntimeErrorSafe("NAO project 与当前 actor/session 不匹配。") project = actor_root / "project" evidence_dir = actor_root / "evidence" if path_session_id else actor_root / session_id / "evidence" session_state_path = actor_root / "private" / "current-session.json" for candidate in (project, project / "agent", project / "agent" / "prompts", project / "RULES.md", project / "nao_config.yaml", project / "agent" / "prompts" / "system.md", project / "agent" / "mcps", project / "agent" / "mcps" / "mcp.json", actor_root / "private", session_state_path, evidence_dir): _reject_symlink_components(candidate, actor_root) 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) for candidate in (project / "RULES.md", project / "nao_config.yaml", project / "agent" / "prompts" / "system.md", project / "agent" / "mcps" / "mcp.json"): _reject_symlink_components(candidate, actor_root) 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 task_record = actor_root / 'private' / 'last-agent-task.json' if (relative == Path('agent/prompts/system.md') and _require_private_regular_file(task_record) and (_read_json(task_record).get('task') or {}).get('executionMode') == 'native_agent'): source = TEMPLATES / 'agent/prompts/system-native.md' target = project / relative if source.is_symlink() or not source.is_file(): raise RuntimeErrorSafe("NAO project 模板不可用。") _atomic_bytes(target, source.read_bytes()) 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" f" maxOutputTokens: {native_output_budget()}\n" " temperature: 0.2\n" ) config_path = project / "nao_config.yaml" _atomic_bytes(config_path, config.encode("utf-8")) 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(session_state_path), "QUALITY_AGENT_ACTOR_ID": actor_id, "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(session_state_path, {"actorId": actor_id, "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 _model_gate_settings(actor_root: Path) -> tuple[int, str]: """Keep the native endpoint stable across turns without storing provider credentials.""" actor_id, session_id = _runtime_identity(actor_root) path = actor_root / "private" / "model-gate.json" _reject_symlink_components(path, actor_root) if _require_private_regular_file(path): value = _read_json(path) if (not isinstance(value, dict) or value.get("actorId") != actor_id or value.get("sessionId") != session_id or not isinstance(value.get("port"), int) or not 1024 <= value["port"] <= 65535 or not isinstance(value.get("clientKey"), str) or not 40 <= len(value["clientKey"]) <= 80): raise RuntimeErrorSafe("本地模型授权代理配置无效。") return value["port"], value["clientKey"] value = {"actorId": actor_id, "sessionId": session_id, "port": _available_port(), "clientKey": secrets.token_urlsafe(32)} _atomic_json(path, value) return value["port"], value["clientKey"] 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 _open_server_log(path: Path, runtime_root: Path) -> int: fd: int | None = None try: _reject_symlink_components(path, runtime_root) fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0), 0o600) info = os.fstat(fd) if not stat.S_ISREG(info.st_mode): os.close(fd) fd = None raise RuntimeErrorSafe("NAO 日志文件路径无效。") os.fchmod(fd, 0o600) result = fd fd = None return result except RuntimeErrorSafe: raise except (OSError, ValueError): raise RuntimeErrorSafe("NAO 日志文件路径无效。") from None finally: if fd is not None: with contextlib.suppress(OSError): os.close(fd) 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" if state_path.is_symlink(): raise RuntimeErrorSafe("NAO 实例状态路径无效,已拒绝停止该进程。") try: state = _read_json(state_path) except (OSError, json.JSONDecodeError): return False actor_id, session_id = _runtime_identity(actor_root) if not RuntimeManager._instance_identity_matches(state, actor_root, actor_id, session_id): raise RuntimeErrorSafe("NAO 实例身份校验失败,已拒绝停止该进程。") 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) if path.is_symlink(): raise RuntimeErrorSafe("运行锁路径无效。") fd = os.open(path, os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0), 0o600) os.fchmod(fd, 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)] def _runtime_roots(users_root: Path = USERS_ROOT) -> list[Path]: """Enumerate legacy actor roots and isolated actor/session runtime roots.""" roots: list[Path] = [] for actor_root in _actor_dirs(users_root): # Keep old state readable and stoppable; never migrate or delete its history. legacy_instance = actor_root / "instance.json" if not legacy_instance.is_symlink() and legacy_instance.is_file(): roots.append(actor_root) sessions_root = actor_root / "sessions" if sessions_root.is_symlink() or not sessions_root.is_dir(): continue for session_root in sessions_root.iterdir(): if (not session_root.is_symlink() and session_root.is_dir() and _UUID_RE.fullmatch(session_root.name) and not (session_root / "instance.json").is_symlink()): roots.append(session_root) return roots def _rebind_legacy_project_path(database: sqlite3.Connection, chat_id: str, legacy_project: Path, session_project: Path) -> None: """Rebind the copied NAO project row while preserving its id and chat links.""" tables = {row[0] for row in database.execute( "SELECT name FROM sqlite_master WHERE type = 'table'")} if "project" not in tables: return project_columns = {row[1] for row in database.execute('PRAGMA table_info("project")')} if not {"id", "path"}.issubset(project_columns): raise RuntimeErrorSafe("旧质量会话 project schema 无法安全迁移,历史数据已保留。") old_path = str(legacy_project.absolute()) new_path = str(session_project.absolute()) chat_project_id: str | None = None if "chat" in tables: chat_columns = {row[1] for row in database.execute('PRAGMA table_info("chat")')} if {"id", "project_id"}.issubset(chat_columns): chat_row = database.execute( 'SELECT "project_id" FROM "chat" WHERE "id" = ?', (chat_id,) ).fetchone() if chat_row is not None: chat_project_id = chat_row[0] if chat_project_id is not None: project_row = database.execute( 'SELECT "path" FROM "project" WHERE "id" = ?', (chat_project_id,) ).fetchone() if project_row is None: raise RuntimeErrorSafe("旧质量会话 chat/project 关系无法验证,历史数据已保留。") current_path = project_row[0] if current_path == new_path: return if current_path not in (None, "", old_path): raise RuntimeErrorSafe("旧质量会话 project 路径与 actor 不匹配,历史数据已保留。") updated = database.execute( 'UPDATE "project" SET "path" = ? WHERE "id" = ? AND "path" IS ?', (new_path, chat_project_id, current_path), ) if updated.rowcount != 1: raise RuntimeErrorSafe("旧质量会话 project 路径无法安全迁移,历史数据已保留。") return # Older NAO snapshots can predate chat.project_id. In that case only move # the unique project that points at this actor's legacy project directory. rows = database.execute( 'SELECT "id" FROM "project" WHERE "path" = ?', (old_path,) ).fetchall() if not rows: return if len(rows) != 1: raise RuntimeErrorSafe("旧质量会话 project 路径不唯一,历史数据已保留。") updated = database.execute( 'UPDATE "project" SET "path" = ? WHERE "id" = ? AND "path" = ?', (new_path, rows[0][0], old_path), ) if updated.rowcount != 1: raise RuntimeErrorSafe("旧质量会话 project 路径无法安全迁移,历史数据已保留。") 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 _session_root(self, actor_id: str, session_id: str) -> Path: actor_id = _uuid(actor_id, "actorId") session_id = _uuid(session_id, "sessionId") actor_root = self._actor_root(actor_id) sessions_root = actor_root / "sessions" if sessions_root.is_symlink(): raise RuntimeErrorSafe("session 运行目录无效。") sessions_root.mkdir(mode=0o700, exist_ok=True) os.chmod(sessions_root, 0o700) session_root = sessions_root / session_id if session_root.is_symlink(): raise RuntimeErrorSafe("session 运行目录无效。") session_root.mkdir(mode=0o700, exist_ok=True) os.chmod(session_root, 0o700) return session_root def acquire_actor(self, actor_id: str, session_id: str | None = None) -> tuple[Path, int]: # The no-session form is retained for legacy callers. Actual analysis # turns always pass Java's session UUID and receive their own lock. actor_root = self._actor_root(actor_id) if session_id is None else self._session_root(actor_id, session_id) lock_path = actor_root / "active.lock" if lock_path.is_symlink(): raise RuntimeErrorSafe("运行锁路径无效。") fd = os.open(lock_path, os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0), 0o600) os.fchmod(fd, 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, expected_actor_id: str | None = None, expected_session_id: str | None = None) -> str | None: try: path = actor_root / "private" / "current-session.json" if path.is_symlink(): return None value = _read_json(path) actor_id, root_session_id = _runtime_identity(actor_root) expected_actor = _uuid(expected_actor_id, "actorId") if expected_actor_id else actor_id session_id = _uuid(value.get("sessionId"), "sessionId") if isinstance(value, dict) else None if (isinstance(value, dict) and value.get("actorId") == expected_actor == actor_id and (root_session_id is None or session_id == root_session_id) and (expected_session_id is None or session_id == _uuid(expected_session_id, "sessionId"))): return session_id except (OSError, json.JSONDecodeError, ValueError): return None return None def ensure_instance(self, actor_root: Path, session_id: str, expected_actor_id: str | None = None, model_connection: tuple[str, str, str] | None = None) -> tuple[str, str, str, str]: session_id = _uuid(session_id, "sessionId") actor_id, root_session_id = _runtime_identity(actor_root) if expected_actor_id is not None and _uuid(expected_actor_id, "actorId") != actor_id: raise RuntimeErrorSafe("NAO runtime 与当前 actor 不匹配。") if root_session_id is not None and root_session_id != session_id: raise RuntimeErrorSafe("NAO runtime 与当前 session 不匹配。") if root_session_id is not None: self._migrate_legacy_session(actor_id, session_id, actor_root) model_base, model_key, model_name = model_connection if model_connection is not None else _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, actor_id, session_id) 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 = (self._instance_identity_matches(state, actor_root, actor_id, root_session_id) and 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, expected_actor_id=actor_id) state = _read_json(state_path) return model_base, model_key, model_name, f"http://127.0.0.1:{int(state['serverPort'])}" def _migrate_legacy_session(self, actor_id: str, session_id: str, session_root: Path) -> bool: """Import one old actor-scoped chat/database snapshot into its session root.""" legacy_root = self.users_root / _uuid(actor_id, "actorId") legacy_private = legacy_root / "private" mapping_path = legacy_private / "sessions.json" if legacy_root.is_symlink() or legacy_private.is_symlink() or mapping_path.is_symlink(): raise RuntimeErrorSafe("旧质量会话记录路径无效,历史数据已保留。") if not mapping_path.is_file(): return False try: mapping = _read_json(mapping_path) except (OSError, json.JSONDecodeError): raise RuntimeErrorSafe("旧质量会话记录无法读取,历史数据已保留。") from None if not isinstance(mapping, dict) or mapping.get("actorId") != actor_id: raise RuntimeErrorSafe("旧质量会话 actor 校验失败,历史数据已保留。") sessions = mapping.get("sessions") chat_id = sessions.get(session_id) if isinstance(sessions, dict) else None if not isinstance(chat_id, str) or not chat_id: return False private_root = session_root / "private" private_root.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(private_root, 0o700) target_db = private_root / "nao.sqlite" target_chat = private_root / "native-chat.json" _reject_symlink_components(target_db, self.users_root) _reject_symlink_components(target_chat, self.users_root) if target_db.exists() and target_chat.exists(): _require_private_regular_file(target_db, missing_ok=False) _require_private_regular_file(target_chat, missing_ok=False) try: current_chat = _read_json(target_chat) except (OSError, json.JSONDecodeError): raise RuntimeErrorSafe("当前质量会话记录无法验证,历史数据已保留。") from None if (not isinstance(current_chat, dict) or current_chat.get("actorId") != actor_id or current_chat.get("sessionId") != session_id): raise RuntimeErrorSafe("当前质量会话 actor/session 校验失败。") return False if target_db.exists() or target_chat.exists(): raise RuntimeErrorSafe("旧质量会话导入状态不完整,历史数据已保留。") source_db = legacy_private / "nao.sqlite" source_credentials = legacy_private / "credentials.json" if (source_db.is_symlink() or not source_db.is_file() or source_credentials.is_symlink() or not source_credentials.is_file()): raise RuntimeErrorSafe("旧质量会话数据库或本地账号凭据不可用,历史数据已保留。") try: credentials = _read_json(source_credentials) except (OSError, json.JSONDecodeError): raise RuntimeErrorSafe("旧质量会话本地账号凭据不可用,历史数据已保留。") from None if not isinstance(credentials, dict) or not all( isinstance(credentials.get(key), str) and credentials[key] for key in ("authSecret", "email", "password")): raise RuntimeErrorSafe("旧质量会话本地账号凭据不完整,历史数据已保留。") _require_private_regular_file(source_db, missing_ok=False) _require_private_regular_file(source_credentials, missing_ok=False) temporary_db = private_root / f".nao.sqlite.{secrets.token_hex(6)}.tmp" try: try: _reject_symlink_components(legacy_root / "project", self.users_root) _reject_symlink_components(session_root / "project", self.users_root) except ValueError: raise RuntimeErrorSafe("旧质量会话 project 路径无效,历史数据已保留。") from None source_uri = "file:" + quote(str(source_db.absolute()), safe="/") + "?mode=ro" source = sqlite3.connect(source_uri, uri=True, timeout=5) target = sqlite3.connect(temporary_db, timeout=5) try: source.backup(target) _rebind_legacy_project_path(target, chat_id, legacy_root / "project", session_root / "project") target.commit() finally: target.close() source.close() os.chmod(temporary_db, 0o600) os.replace(temporary_db, target_db) _atomic_json(private_root / "credentials.json", credentials) _atomic_json(target_chat, {"actorId": actor_id, "sessionId": session_id, "nativeChatId": chat_id}) _atomic_json(private_root / "legacy-import.json", { "actorId": actor_id, "sessionId": session_id, "importedAt": datetime.now(timezone.utc).isoformat(), }) return True except RuntimeErrorSafe: temporary_db.unlink(missing_ok=True) target_db.unlink(missing_ok=True) raise except (OSError, sqlite3.Error): temporary_db.unlink(missing_ok=True) target_db.unlink(missing_ok=True) raise RuntimeErrorSafe("旧质量会话数据库无法安全导入,历史数据已保留。") from None 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, expected_actor_id: str | None = None) -> None: actor_id, session_root_id = _runtime_identity(actor_root) actor_id = _uuid(expected_actor_id, "actorId") if expected_actor_id is not None else actor_id if actor_id != _runtime_identity(actor_root)[0] or (session_root_id is not None and session_root_id != session_id): raise RuntimeErrorSafe("NAO runtime 与当前 actor/session 不匹配。") with _file_lock(self.users_root / "registry.lock", blocking=True): active = [] for other in _runtime_roots(self.users_root): try: state = _read_json(other / "instance.json") except (OSError, json.JSONDecodeError): continue procs = state.get("processes", []) if isinstance(state, dict) else [] other_actor_id, other_session_id = _runtime_identity(other) if (self._instance_identity_matches(state, other, other_actor_id, other_session_id) and 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, expected_actor_id=actor_id) self._start(actor_root, model_base, model_key, model_name, fingerprint) @staticmethod def _instance_identity_matches(state: Any, runtime_root: Path, actor_id: str, session_id: str | None) -> bool: if not isinstance(state, dict) or state.get("actorId") != actor_id: return False if session_id is None: return state.get("sessionId") in (None, "") return state.get("sessionId") == session_id def _start(self, actor_root: Path, model_base: str, model_key: str, model_name: str, fingerprint: str, *, expected_actor_id: str | None = None, session_id: str | None = None) -> None: actor_id, path_session_id = _runtime_identity(actor_root) actor_id = _uuid(expected_actor_id, "actorId") if expected_actor_id is not None else actor_id session_id = path_session_id or (_uuid(session_id, "sessionId") if session_id else None) if actor_id != _runtime_identity(actor_root)[0] or path_session_id not in (None, session_id): raise RuntimeErrorSafe("NAO runtime 与当前 actor/session 不匹配。") private = _write_private_state(actor_root) project = actor_root / "project" logs = actor_root / "logs" try: _reject_symlink_components(logs, actor_root) except ValueError: raise RuntimeErrorSafe("NAO 日志路径无效。") 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 if path.is_symlink(): raise RuntimeErrorSafe("NAO 数据库路径无效。") path.touch(mode=0o600, exist_ok=True) path.chmod(0o600) processes: list[dict[str, Any]] = [] log_path = logs / "server.log" fd = _open_server_log(log_path, actor_root) 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_id, "sessionId": session_id, "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 _runtime_roots(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, *, expected_actor_id: str | None = None, expected_session_id: str | None = None) -> Path: path_actor_id, path_session_id = _runtime_identity(actor_root) actor_id = _uuid(expected_actor_id, "actorId") if expected_actor_id is not None else path_actor_id session_id = (_uuid(expected_session_id, "sessionId") if expected_session_id is not None else path_session_id) if actor_id != path_actor_id or (path_session_id is not None and session_id != path_session_id): raise RuntimeErrorSafe("访问凭据与当前 actor/session 不匹配。") access_path = actor_root / "access.json" value = {"token": token, "releaseId": _uuid(release_id, "releaseId"), "queryUrl": RUNTIME_QUERY_URL, "actorId": actor_id} if session_id is not None: value["sessionId"] = session_id _atomic_json(access_path, value) return access_path def _make_opener(actor_root: Path, base_url: str) -> Any: cookie_path = actor_root / "private" / "cookies.txt" if actor_root.is_symlink() or cookie_path.parent.is_symlink(): raise RuntimeErrorSafe("NAO 本地会话凭据路径无效。") cookie_path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) if cookie_path.exists(): _require_private_regular_file(cookie_path, missing_ok=False) 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: if Path(path).is_symlink(): raise RuntimeErrorSafe("NAO 本地会话凭据路径无效。") 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, prior_evidence_files: frozenset[str] = frozenset(), turn_id: str | None = None, ) -> 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, ) from nao_agent_contract import CAPABILITY_INSTRUCTIONS, capability_context prompt = (f"【仅内部查询约束,禁止向用户复述】本轮数据查询使用 releaseId={release_id},后续所有数据查询必须沿用该标识。" "如果当前数据下的证据不足,请如实说明,不要切换数据。用户可见的欢迎语、回答和报告只写业务问题、期间、范围、指标、结果与必要局限;不展示 releaseId、发布版本号、Story ID/version,也不说‘固定版本’。不要展示‘原始明细不发AI’等内部流程提示。" + "\n" + CAPABILITY_INSTRUCTIONS + context_text + "\n\n用户问题:\n" + question) if turn_id is not None: prompt = f'【内部请求归属标记,禁止向用户展示】quality-turn:{turn_id}\n' + prompt from nao_agent_task import task_from_context semantic_task = task_from_context(context, question) autonomous = bool(semantic_task and semantic_task.get('executionMode') == 'native_agent') if autonomous: prompt += ('\n【自主调查】已确认任务只定义目标与边界,不是一份预设查询路线。' '先取得必要基线,观察返回结果后自己决定下一次quality_data查询、比较、反例和下钻。' '不固定只查一个产品或一个阶段;按线索是否重要、是否矛盾和用户目标判断。' '当前_agentTask.filters和period是唯一范围权威;历史回答仅用于理解指代,不能恢复已取消条件。' 'changes支持current_rate最低率、rate_drop降幅、scrap_delta报废增量和current_scrap本期报废,均走批量接口。' '多层筛选先取得父范围证据,再逐层下钻。具体缺陷用defects,不用原生SQL或display_chart。' '相同查询复用本轮证据;连续三次无新增证据会停止。足以回答时直接结束,不为覆盖目录而遍历全部对象。' '自由解释分子分母、规模与阶段变化,引用真实证据,区分已核实事实、统计线索及待验证生产假设。' '如果来源缺失、到达预算或查询失败,明确未解决的具体问题,不能宣称全都完成。') if semantic_task is not None: prompt += ("\n【本轮结构化任务】上下文_agentTask是已理解的本轮任务。沿用goal及筛选和期间,按dimensions/stages取得足够证据。" "freshQuery=true时必须取得本轮新证据;verifySource要求原件核验;fullDetails要求分页至完成。" "dimensionStages指定各维度自身的阶段,不把总体阶段套到局部排名。缺项先调整计划补查,来源确实不提供就说明限制,不反复调查普通小额差异。" "表格/图表呈现由结构化任务决定,无需用户说固定词语。") if isinstance(context.get('primaryEvidence'), dict): prompt += ('\n【本轮已执行的正式查询】primaryEvidence是平台刚按本轮AI任务执行得到的新证据,' '可直接引用其真实evidenceRef和指标,无需重复相同查询。仅为缺少的维度或交叉核查继续调用quality_data。') from nao_metric_request import metric_request, requires_fresh_query if ((semantic_task['freshQuery'] if semantic_task is not None else requires_fresh_query(question)) or metric_request(question, context)) and not (isinstance(context, dict) and context.get('primaryEvidence')): prompt += "\n【本轮查询约束】这是本轮正式指标查询,必须新调用 quality_data 获取与本轮产品、期间和筛选一致的证据;历史数值不能替代本轮查询。" 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 ready_state: dict[str, Any] = {"identity": None, "since": None} def completion_ready() -> bool: # The watchdog can observe a stale halted exploration state between a # natural model finish and the stream's trailing [DONE] marker. if accumulator.done_received or (accumulator.finish_reasons and accumulator.finish_reasons[-1] == 'stop'): return False if autonomous: progress_path = actor_root / 'private' / 'native-exploration-state.json' if not _require_private_regular_file(progress_path): return False progress = _read_json(progress_path) return progress.get('executionId') == (context or {}).get('_nativeExecutionId') and progress.get('halted') is True from nao_analysis_coverage import is_coverage_question, plan_missing_queries, formal_metric_coverage from nao_evidence_finalizer import coverage_sufficient, evidence_summaries, FinalizationUnavailable pure_metrics = metric_request(question, context) is not None # Only the model profile with a bounded finalizer can be interrupted. if (cancel_event is not None and cancel_event.is_set() or not (pure_metrics or is_coverage_question(question, context)) or accumulator.done_received or not accumulator.tool_calls or any(call.get("status") not in ("completed", "failed") and not call.get("recovered") for call in accumulator.tool_calls)): ready_state.update(identity=None, since=None) return False try: documents = evidence_summaries(actor_root / "evidence", release_id, exclude_files=prior_evidence_files) except FinalizationUnavailable: ready_state.update(identity=None, since=None) return False contract = formal_metric_coverage(documents, question, context) if pure_metrics else None complete = coverage_sufficient(documents, question, context) if pure_metrics and (not contract or contract['missingEvidence'] or not contract['metrics']): ready_state.update(identity=None, since=None) return False # The server can finish a bounded set of omitted read-only dimensions # after stopping the native model. Keep the formal comparison and a # product ranking as anchors before taking that path. repairable = (semantic_task is None and not pure_metrics and not complete and any(doc['result'].get('kind') == 'quality_comparison' for doc in documents) and any(doc['result'].get('kind') == 'quality_changes' and doc['result'].get('dimension') == 'product' for doc in documents) and bool(plan_missing_queries(documents, question, context))) if not complete and not repairable: ready_state.update(identity=None, since=None) return False identity = (tuple(doc["evidenceRef"] for doc in documents), len(accumulator.tool_calls)) now = time.monotonic() if identity != ready_state["identity"]: ready_state.update(identity=identity, since=now) return False return now - ready_state["since"] >= (2 if pure_metrics else 20) stopper = NativeStopController(stop_or_shutdown, timeout, completion_ready) 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 and not autonomous: emit({"type": "answer", "answer": value}) accumulator = ConversationAccumulator(publish_answer) last_reasoning_emit = 0.0 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 in ("text-delta", "text-end", "tool-input-available", "tool-output-available", "tool-result", "tool-output-error"): now = time.monotonic() if kind != "text-delta" or now - last_reasoning_emit >= 0.35: last_reasoning_emit = now latest = accumulator.tool_calls[-1] if accumulator.tool_calls else {} emit({"type": "progress", "message": latest.get("label", "正在整理分析说明…"), "toolCount": len(accumulator.tool_calls), "toolCalls": accumulator.tool_calls, "timeline": accumulator.timeline}) elif kind == "reasoning-delta" and time.monotonic() - last_reasoning_emit >= 15: last_reasoning_emit = time.monotonic() emit({"type":"progress","message":"模型正在整理已返回的查询证据…","toolCount":len(accumulator.tool_calls),"toolCalls":accumulator.tool_calls,"timeline":accumulator.timeline}) # 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 _drop_repeated_followup_context(question: str, context: Any) -> tuple[dict[str, Any], bool]: """Do not anchor a repeated follow-up to its own possibly wrong answer.""" cleaned = dict(context) if isinstance(context,dict) else {} questions = cleaned.get('_serverQuestions') if not isinstance(questions,list) or not questions or not isinstance(question,str): return cleaned,False def same_question(entry): if not isinstance(entry,dict) or not isinstance(entry.get('question'),str): return False prior, current = entry['question'].strip(), question.strip() if prior == current: return True # Historical question summaries can be truncated by the server. A # matching prefix conservatively forces fresh analysis, never reuse. return prior.endswith('…') and len(prior) > 1 and current.startswith(prior[:-1]) index = len(questions) while index > 0: entry = questions[index-1] if not same_question(entry): break index -= 1 if index == len(questions): return cleaned,False repeated_ids = {entry.get('turnId') for entry in questions[index:] if isinstance(entry,dict)} cleaned['_serverQuestions'] = questions[:index] if isinstance(cleaned.get('_serverHistory'),list): cleaned['_serverHistory'] = [entry for entry in cleaned['_serverHistory'] if not isinstance(entry,dict) or (entry.get('turnId') not in repeated_ids and not same_question(entry))] cleaned.pop('lastVisibleAnswer',None) return cleaned,True 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") restart_conversation = False if isinstance(context, dict): context = dict(context) if '_protocol' in context: protocol = context['_protocol'] if (not isinstance(protocol, dict) or type(protocol.get('version')) is not int or protocol['version'] != 1): # An explicit protocol request cannot fall back to actor-local # task/chat files when its envelope is malformed or unsupported. raise RuntimeErrorSafe('分析请求格式或版本无效,请重新发起分析。') # This key is generated from local parsing, never accepted from callers. context.pop('_resolvedMetricRequest', None) context.pop('_agentTask', None) context.pop('agentContext', None) context.pop('_businessCatalog', None) context.pop('primaryEvidence', None) restart_conversation = context.pop("_restartConversation", False) if not isinstance(restart_conversation, bool): raise ValueError("重新分析标记无效。") 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, "restartConversation": restart_conversation} 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 plan_agent_task(**kwargs): from nao_agent_task import plan_task, TaskUnavailable diagnostic_path = kwargs.pop('diagnostic_path', None) diagnostic_turn_id = kwargs.pop('diagnostic_turn_id', None) recovery_progress = kwargs.pop('recovery_progress', None) turn_path = None attempts = [] if diagnostic_path is not None: if diagnostic_turn_id is not None: turn_id = _uuid(diagnostic_turn_id, 'turnId') turn_path = diagnostic_path.parent / 'task-plan-turns' / (turn_id + '.json') _reject_symlink_components(turn_path, diagnostic_path.parent) validation_path = diagnostic_path.with_name('task-plan-validation.json') def record_turn(status, final_error=None): if turn_path is None: return _reject_symlink_components(turn_path, diagnostic_path.parent) value = {'turnId':turn_id, 'phase':'task_planning', 'status':status, 'attempts':attempts} if final_error is not None: value['failure'] = safe_diagnostic(final_error) _atomic_json(turn_path, value) def safe_diagnostic(error): # Avoid retaining prompts, model answers, source values or secrets. # Only bounded protocol metadata is useful across turns. result = {'phase':'task_validation'} if not isinstance(error, dict): return result field = error.get('field') allowed_fields = {'goal', 'kind', 'clarification', 'comparisonRequired', 'trendRequired', 'defectScope/defectProductCount', 'productMatch', 'productScope.review', 'customerCodes', 'customerCodes.review', 'review.protocol', 'requiresComparison', 'requiresComparison.review', 'requiresEvidence', 'requiresTrend', 'requiresTrend.review', 'requiresSourceVerification', 'requiresAllSourceRows', 'targetEntities', 'queries', 'queries.dimension', 'filterUpdates', 'directResponse', 'directResponse.business', 'directResponse.answer', 'directResponse.kind', 'directResponse.schema', 'directResponse.purpose', 'directResponse.requiresMemory', 'freshQuery', 'fullDetails', 'verifySource', 'needsClarification', 'dimensions', 'stages', 'fields', 'queryPlan', 'queryPlan.dimensions', 'planRecovery'} if isinstance(field, str) and (field in allowed_fields or re.fullmatch( r'(?:filterUpdates\.(?:department|group|product|projectGroup|extraFilters|exclusions|includeUnassigned)|targetEntities\.(?:group|department))', field)): result['field'] = field if isinstance(error.get('receivedType'), str) and error['receivedType'] in {'null','str','dict','list','bool','int','float','NoneType'}: result['receivedType'] = error['receivedType'] if isinstance(error.get('causeType'), str) and error['causeType'] in {'HTTPError','URLError','TimeoutError','ValueError','OSError','RuntimeError','JSONDecodeError'}: result['causeType'] = error['causeType'] if type(error.get('attempt')) is int and 0 < error['attempt'] <= 100: result['attempt'] = error['attempt'] if type(error.get('providerStatus')) is int and 100 <= error['providerStatus'] <= 599: result['providerStatus'] = error['providerStatus'] return result def record_validation(error): # Legacy file remains available to existing support workflows. legacy_attempts.append(error) attempts.append(safe_diagnostic(error)) _atomic_json(validation_path, {'attempts':legacy_attempts}) record_turn('planning') if recovery_progress is not None: recovery_progress({'type': 'progress', 'message': '分析计划校验未通过,正在自动修复并重新核对…'}) legacy_attempts = [] _atomic_json(validation_path, {'attempts':[]}) record_turn('planning') kwargs['on_validation_error'] = record_validation try: planned = plan_task(**kwargs) if diagnostic_path is not None: record_turn('planned') if recovery_progress is not None and planned.get('planRecovery'): recovery_progress({'type': 'progress', 'message': '分析计划已自动修复并通过校验,继续执行原目标。'}) return planned except TaskUnavailable as error: if diagnostic_path is not None: _atomic_json(diagnostic_path, {'error': str(error)[:600]}) record_turn('failed', error.details()) raise RuntimeErrorSafe('分析计划自动修复未通过,尚未查询数据;已有问题和条件已保留,可重试本轮。') from None except Exception: if diagnostic_path is not None: record_turn('failed') raise def _record_non_query_turn(actor_root, request): path = actor_root / 'private' / 'non-query-turns' / (_uuid(request['turnId'], 'turnId') + '.json') _reject_symlink_components(path, actor_root) _atomic_json(path, {key:request[key] for key in ('turnId','sessionId','actorId','releaseId')}) def _known_non_query_turn(actor_root, request, entry): if entry.get('evidenceRefs'): return False try: turn_id = _uuid(entry.get('turnId'), 'turnId') path = actor_root / 'private' / 'non-query-turns' / (turn_id + '.json') _reject_symlink_components(path, actor_root) if not _require_private_regular_file(path): return False record = _read_json(path) return (isinstance(record, dict) and record.get('turnId') == turn_id and all(record.get(key) == request[key] for key in ('sessionId','actorId','releaseId'))) except (ValueError, OSError, RuntimeErrorSafe): return False def historical_report(*, request, task, actor_root, connection, cancel_event, emit): """Only a report may reopen explicitly referenced, same-session old evidence.""" from nao_evidence_finalizer import evidence_summaries, synthesize, coverage_sufficient, FinalizationUnavailable from analysis_visual_evidence import visual_result from nao_agent_report import build_agent_report history = request['context'].get('_serverHistory', []) if any(not isinstance(entry, dict) for entry in history): raise RuntimeErrorSafe('历史轮次信息无效,未生成完整报告。') for entry in history: row_refs = entry.get('evidenceRefs') or [] if not isinstance(row_refs, list) or any(not isinstance(ref, str) or not re.fullmatch(r'Q[A-F0-9]{8}', ref) for ref in row_refs): raise RuntimeErrorSafe('历史查询依据编号无效,未生成完整报告。') history = [entry for entry in history if not _known_non_query_turn(actor_root, request, entry)] if task['historyMode'] == 'last': history = history[-1:] unknown_history = [entry for entry in history if not entry.get('evidenceRefs')] refs = {ref for entry in history if isinstance(entry, dict) for ref in entry.get('evidenceRefs', []) if isinstance(ref, str) and re.fullmatch(r'Q[A-F0-9]{8}', ref)} if not refs: raise RuntimeErrorSafe('前述会话尚无可核对的统计依据,不能声称报告已整合完成。') files = list((actor_root / 'evidence').glob('nao-quality-*.json')) selected = {p.name for p in files if re.fullmatch(r'nao-quality-[a-f0-9]{32}\.json', p.name) and 'Q' + p.name[len('nao-quality-'):len('nao-quality-') + 8].upper() in refs} excluded = frozenset(p.name for p in files if p.name not in selected) evidence = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=excluded) # Do not treat an unbound model recollection as an archived query. if not refs <= {doc['evidenceRef'] for doc in evidence}: raise RuntimeErrorSafe('前述会话的部分查询依据不可用,未生成完整报告。') report_task = {**task, 'dimensions': [], 'stages': [], 'fields': [], 'comparisonRequired': False} context = {'releaseId': request['releaseId'], 'filters': {}, '_agentTask': report_task} if task['historyMode'] == 'all': report_task['filters'] = {} if task['historyMode'] == 'last': context.update({key: task[key] for key in ('filters', 'period', 'comparisonPeriod') if task.get(key)}) frames = {} for document in evidence: data = document['result'] if data.get('kind') == 'quality_comparison': frame = {'filters': data.get('filterBinding', {}), 'period': data['current']['period'], 'comparisonPeriod': data['comparison']['period']} elif data.get('kind') == 'quality_metrics': frame = {'filters': data.get('filters', {}), 'period': data.get('period')} else: continue frames[json.dumps(frame, sort_keys=True)] = frame if len(frames) == 1: frame = next(iter(frames.values())) context.update(frame) report_task.update(frame) emit({'type': 'progress', 'message': '正在整合前述已完成分析及对应数据依据…'}) base, key, model = connection final = synthesize(base_url=base, api_key=key, model=model, question=request['question'], evidence=evidence, context=context, cancel_event=cancel_event) complete = final['evidenceSufficient'] and coverage_sufficient(evidence, request['question'], context) and not unknown_history if unknown_history: gap = '部分历史轮次没有可核对的查询依据,未纳入本次报告统计;已保留其他有效分析结果。' final['answer'] += '\n\n' + gap final['missingEvidence'] = list(dict.fromkeys([*final.get('missingEvidence', []), gap])) report = build_agent_report(evidence, context, request['question']) if not final.get('titleGeneratedByModel'): # A long answer may need the verified text fallback. Generate its short # title independently rather than replacing it with the user's prompt. from nao_agent_task import _model_json, TaskUnavailable from nao_evidence_finalizer import _report_title_guide, _valid_report_title guide = _report_title_guide(report, context) emit({'type': 'progress', 'message': '正在整理报告标题…'}) try: titled = _model_json(base_url=base, api_key=key, model=model, system='根据已核实质量数据拟定中文报告标题,概括实际期间、范围与主题,不使用任务指令或问句,不超过40字。只返回JSON {"reportTitle":"标题"}。只能使用提供的真实期间、范围、事实,不编造数字或原因。', payload={'titleGuide': guide, 'facts': report['facts'][:12]}, cancel_event=cancel_event) candidate = titled.get('reportTitle') if isinstance(titled, dict) else None if _valid_report_title(candidate, report, request['question'], guide): final['reportTitle'] = candidate.strip() final['titleGeneratedByModel'] = True except TaskUnavailable: pass # The verified fallback already contains every formal section and table. # Only the model's concise composition needs the detailed appendix added. markdown = final['answer'] + '\n\n## 数据明细\n\n' + report['headline'] + '\n\n' + '\n\n'.join(report['sections']) if cancel_event.is_set(): raise RuntimeErrorSafe('已停止报告整理。') result = {'type': 'result', 'engine': 'NAO', 'status': 'COMPLETED' if complete else 'PARTIAL', 'answer': final['answer'], 'reportMarkdown': markdown, 'completionMethod': 'verified_conversation_report', 'stopReason': 'stop' if complete else 'evidence_incomplete', 'finishReason': 'stop', 'reportTitle': final.get('reportTitle', '质量分析报告'), 'agentTask': report_task, 'agentContext': {key: value for key, value in context.items() if key != '_agentTask'}, 'toolCalls': [{'tool': 'model.finalize', 'label': '整合前述分析与真实依据', 'status': 'completed', 'outputSummary': f'已核对前述 {len(history)-len(unknown_history)} 轮分析的保存依据。'}], 'historyTurnIds': [entry['turnId'] for entry in history if entry.get('evidenceRefs') and entry.get('turnId')], 'unverifiedHistoryTurnIds': [entry.get('turnId') for entry in unknown_history]} result['queryEvidenceRefs'] = sorted(refs) result.update(visual_result(actor_root / 'evidence', request['releaseId'], request['question'], request['turnId'], exclude_files=excluded, cancel_event=cancel_event, context=context)) _atomic_json(actor_root / 'private' / 'last-report-check.json', { 'turnId': request['turnId'], 'status': result['status'], 'reportTitle': result['reportTitle'], 'titleGeneratedByModel': final.get('titleGeneratedByModel', False), 'historyTurnIds': result['historyTurnIds'], 'evidenceRefs': sorted(refs), 'unverifiedHistoryTurnIds': result['unverifiedHistoryTurnIds'], 'visualState': result['visualState'], 'visualCount': len(result['visuals']), 'missingEvidence': final.get('missingEvidence', []), 'serverCoverageSufficient': coverage_sufficient(evidence, request['question'], context)}) emit(result) return result def read_primary_evidence(*, arguments, request, actor_root, access_path, manager): from nao_quality_tools import QualityTools api = DelegatedApi(access_path, runtime_root=manager.users_root, expected_actor_id=request['actorId'], expected_session_id=request['sessionId'], timeout=SUPPLEMENT_QUERY_TIMEOUT_SECONDS).login() task = request.get('context', {}).get('_agentTask') contract = {'task':task,'releaseId':request['releaseId']} if isinstance(task,dict) else None return QualityTools(api_factory=lambda: api, evidence_dir=actor_root / 'evidence', query_contract=contract).quality_data(arguments) def complete_agent_evidence(*, task, request, actor_root, access_path, manager, connection, accumulator, prior_files, cancel_event, emit): """Bounded observe/replan/read/verify loop; only authenticated reads run here.""" from nao_agent_task import plan_repairs, TaskUnavailable from nao_analysis_coverage import coverage_gaps, plan_missing_queries, plan_missing_source_queries from nao_evidence_finalizer import evidence_summaries, coverage_sufficient, FinalizationUnavailable from nao_quality_tools import QualityTools attempted = set() for revision in range(2): if cancel_event.is_set(): return try: docs = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_files, allow_empty=True) gaps = coverage_gaps(docs, request['question'], request['context']) failures = [{'arguments': c.get('input', {}), 'error': c.get('outputSummary', '')} for c in accumulator.tool_calls if c.get('status') == 'failed' and not c.get('recovered')] if task['freshQuery'] and not docs: gaps.append('本轮尚无新的正式数据查询证据。') if (task['verifySource'] or task['fullDetails']) and not coverage_sufficient(docs, request['question'], request['context']): gaps.append('未完成用户要求的原件核验或全量记录分页。') if not gaps and not failures: return emit({'type': 'progress', 'message': '正在根据证据缺口调整查询计划…'}) base, key, model = connection # The model has already resolved the required dimensions. Execute # exact missing reads in that frame before asking it to replan. planned_queries = (plan_missing_source_queries(docs, task, request['context']) + plan_missing_queries(docs, request['question'], request['context'])) queries = [query for query in planned_queries if tool_operation_key('quality_data', query) not in attempted][:5] if not queries: queries = plan_repairs(base_url=base, api_key=key, model=model, task=task, evidence=docs, gaps=gaps, failures=failures, release_id=request['releaseId'], cancel_event=cancel_event) if not queries: return api = DelegatedApi(access_path, runtime_root=manager.users_root, expected_actor_id=request['actorId'], expected_session_id=request['sessionId'], timeout=SUPPLEMENT_QUERY_TIMEOUT_SECONDS).login() tools = QualityTools(api_factory=lambda: api, evidence_dir=actor_root / 'evidence', query_contract={'task':task,'releaseId':request['releaseId']}) for arguments in queries: if cancel_event.is_set(): return identity = tool_operation_key('quality_data', arguments) if identity in attempted: continue source = arguments.get('mode') == 'investigate' and arguments.get('action') == 'source' if source: from nao_runtime_policy import remaining_seconds if remaining_seconds() <= SOURCE_FINALIZATION_RESERVE_SECONDS: emit({'type':'progress','message':'原表核验时间不足,已保留未覆盖内容。'}) return api.timeout = SOURCE_VERIFICATION_TIMEOUT_SECONDS if source else SUPPLEMENT_QUERY_TIMEOUT_SECONDS api.deadline_reserve_seconds = SOURCE_FINALIZATION_RESERVE_SECONDS if source else None attempted.add(identity) call = {'tool': 'quality_data', 'toolCallId': f'agent-repair-{revision}-{len(attempted)}', **public_tool_call('mcp_call', {'tool': 'quality_data', 'arguments': arguments}), 'status': 'started', 'startedAt': datetime.now(timezone.utc).isoformat()} accumulator.tool_calls.append(call) accumulator.record_event('tool_start', tool=call['tool'], toolCallId=call['toolCallId'], label=call['label'], input=call.get('input', {}), status='started') try: result = tools.quality_data(arguments) call.update(status='completed', outputSummary=public_tool_result(result)) accumulator.tool_outputs[call['toolCallId']] = result # A successful read only recovers a failed identical operation. for previous in accumulator.tool_calls[:-1]: if previous.get('status') == 'failed' and accumulator.tool_operation_keys.get(id(previous)) == identity: previous['recovered'] = True except (ValueError, OSError, RuntimeError) as error: from nao_quality_tools import QualityToolError detail = str(error)[:400] if isinstance(error, QualityToolError) else '补查未成功,已保留证据缺口。' call.update(status='failed', outputSummary=detail) accumulator.tool_operation_keys[id(call)] = identity call['finishedAt'] = datetime.now(timezone.utc).isoformat() accumulator.record_event('tool_result', tool=call['tool'], toolCallId=call['toolCallId'], label=call['label'], status=call['status'], text=call['outputSummary']) except (TaskUnavailable, FinalizationUnavailable, ValueError, OSError, RuntimeError): emit({'type': 'progress', 'message': '补查计划未完成,将按实际证据说明缺口。'}) return def run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any = sys.stdin) -> dict[str, Any]: with turn_scope(): return _run_turn(payload, stdout=stdout, stdin=stdin) def _run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any = sys.stdin) -> dict[str, Any]: output_lock = threading.Lock() from nao_quality_tools import SourceCoverageUnavailable from nao_thinking_stream import ThinkingPublisher thinking = ThinkingPublisher(lambda block: _emit(stdout, output_lock, {"type":"progress","thinkingUpdate":block})) def emit(value): if value.get('type') == 'result': thinking.close() _emit(stdout, output_lock, value) actor_root: Path | None = None actor_fd: int | None = None access_path: Path | None = None model_gate: Any | None = None try: request = _normalize_request(payload) protocol_request = isinstance((request.get('context') or {}).get('_protocol'),dict) if protocol_request and request['context']['_protocol'].get('autonomousEnabled') is not True: for legacy in ('_serverHistory','_serverQuestions','lastVisibleAnswer','_task','primaryEvidence'): request['context'].pop(legacy,None) emit({"type": "progress", "message": "正在准备独立质量分析环境…"}) manager = RuntimeManager() actor_root, actor_fd = manager.acquire_actor(request["actorId"], request["sessionId"]) access_path = write_access_file(actor_root, request["accessToken"], request["releaseId"], expected_actor_id=request["actorId"], expected_session_id=request["sessionId"]) del request["accessToken"] # All native and final-synthesis provider requests pass the same live turn authorization. # The native server sees only a stable local endpoint and a private local credential. from nao_model_gate import ModelGate upstream_base, upstream_key, selected_model, model_controls = _model_settings(include_options=True) port, gate_key = _model_gate_settings(actor_root) delegated = DelegatedApi(access_path, runtime_root=manager.users_root, expected_actor_id=request["actorId"], expected_session_id=request["sessionId"], timeout=180 if protocol_request else 5).login() def authorize_model() -> None: delegated.call("/api/analysis/model-authorization", method="GET") authorize_model() model_gate = ModelGate(upstream_base=upstream_base, upstream_key=upstream_key, model=selected_model, port=port, client_key=gate_key, authorize=authorize_model,thinking_options=model_controls, on_thinking=thinking.receive,native_turn_marker='quality-turn:'+request['turnId']) del upstream_key model_name = selected_model # The model process receives only NAO's configured model credential. The delegated # user token remains in access.json, read only by the MCP shim. selected_connection = (model_gate.base_url, gate_key, selected_model) state = SessionState(actor_root, request["actorId"], request["sessionId"]) native_chat_id = None if protocol_request or request["restartConversation"] else state.get_chat_id(request["sessionId"]) controls_path=actor_root/'private'/'last-model-controls.json' effective_controls={'model':selected_model,**model_controls} if native_chat_id and (not _require_private_regular_file(controls_path) or _read_json(controls_path)!=effective_controls): native_chat_id=None emit({"type": "progress", "message": "正在查询质量数据分析平台并整理结论…"}) cancel_event = threading.Event() def request_cancel() -> None: cancel_event.set() _input_cancel_listener(stdin, request_cancel) prior_evidence_files = frozenset(path.name for path in (actor_root / "evidence").glob("nao-quality-*.json")) visible_answer_path = actor_root / "private" / "last-visible-answer.json" previous_visible = None if native_chat_id and _require_private_regular_file(visible_answer_path): candidate_visible = _read_json(visible_answer_path) if (isinstance(candidate_visible, dict) and candidate_visible.get("releaseId") == request["releaseId"] and candidate_visible.get("sessionId") == request["sessionId"] and isinstance(candidate_visible.get("answer"), str)): previous_visible = candidate_visible context = {**(request["context"] if isinstance(request["context"], dict) else {}), "lastVisibleAnswer": previous_visible["answer"]} if len(json.dumps(context, ensure_ascii=False)) <= MAX_CONTEXT_CHARS: request["context"] = context from nao_metric_request import prepare_metric_context, frozen_metric_frame, requires_fresh_query, is_history_restatement base_metric_frame = frozen_metric_frame(request['context']) options = delegated.call('/api/options?releaseId=' + request['releaseId'], method='GET') request['context'] = {**(request['context'] if isinstance(request['context'], dict) else {}), '_businessCatalog': {k: options.get(k, []) for k in ('years', 'periods', 'latestDataPeriod', 'departments', 'groups', 'groupDepartments')}} from nao_product_identification import identify_products, resolve_clarification_choice, authorized_extra_fields # The options endpoint is already restricted to the current user and # release. Product shorthand is only resolved within that catalogue; # ambiguous names are passed to the planner, never merged here. request['context']['_businessCatalog'].update(identify_products(request['question'], options, respect_exclusions=((request['context'].get('_protocol') or {}).get('autonomousEnabled') is True))) try: actor_catalog = delegated.call('/api/analysis/tools/catalog', method='GET') except (RuntimeErrorSafe, ValueError, OSError): actor_catalog = None # Only expose fields actually returned by the actor-scoped catalog; # an unavailable catalog is distinct from an authorized empty list. request['context']['_businessCatalog'].update(authorized_extra_fields(actor_catalog)) if isinstance(actor_catalog,dict) and isinstance(actor_catalog.get('sourceFields'),list): request['context']['_businessCatalog']['sourceFields'] = actor_catalog['sourceFields'] if isinstance(actor_catalog,dict) and isinstance(actor_catalog.get('fineDefects'),dict): request['context']['_businessCatalog']['fineDefects'] = actor_catalog['fineDefects'] from nao_agent_task import bind_task last_task_path = actor_root / 'private' / 'last-agent-task.json' previous_task = None resolution = None planning_context = request['context'] protocol = request['context'].get('_protocol') if isinstance(protocol,dict) and protocol.get('version') == 1: if protocol.get('attemptId') != request['sessionId']: raise RuntimeErrorSafe('统一协议运行身份不匹配。') previous_task = protocol.get('previousTask') or None if previous_task is not None: resolution = resolve_clarification_choice(request['question'],options,previous_task) if resolution is not None: request['context']['_businessCatalog'].update(resolution) planning_context = bind_task(request['context'],previous_task) elif _require_private_regular_file(last_task_path): record = _read_json(last_task_path) if (isinstance(record, dict) and record.get('releaseId') == request['releaseId'] and record.get('sessionId') == request['sessionId'] and isinstance(record.get('task'), dict)): saved_task = record['task'] resolution = resolve_clarification_choice(request['question'], options, saved_task) if not request['restartConversation'] or resolution is not None: previous_task = saved_task if resolution is not None: request['context']['_businessCatalog'].update(resolution) if not request['restartConversation'] and record.get('baseMetricFrame') == base_metric_frame: planning_context = bind_task(planning_context, previous_task) cleaned_context, repeated = _drop_repeated_followup_context(request['question'],request['context']) if repeated: # Keep earlier intent, but replan without the repeated turn's # potentially incorrect answer, task or native chat state. request['context'] = cleaned_context planning_context = cleaned_context # Preserve only the already-authorized clarification selection and # its original user intent. Old answers, native chat and evidence # remain excluded from the fresh repeated analysis. if resolution is None: previous_task = None previous_visible = None native_chat_id = None request['restartConversation'] = True emit({'type': 'progress', 'message': '正在理解本轮目标和连续追问…'}) protocol_planning_started=time.monotonic() retry_task = protocol.get('retryTask') if isinstance(protocol,dict) else None planning_options = {} planning_options['recovery_progress'] = emit if isinstance(protocol,dict): source_lookup_audit = [] def source_value_resolver(*, release_id, period, filters, field, stages, search, limit, offset): query = { 'releaseId':release_id,'period':period,'filters':filters,'field':field, 'stages':stages,'search':search,'limit':limit,'offset':offset} started=time.monotonic() try: response=delegated.call('/api/analysis/source/values',payload=query,method='POST') except Exception as error: source_lookup_audit.append({'field':field,'stages':stages,'errorType':type(error).__name__}) _atomic_json(actor_root/'private'/'source-lookup-audit.json',source_lookup_audit[-100:]) raise source_lookup_audit.append({'field':field,'stages':stages,'elapsedMs':round((time.monotonic()-started)*1000), 'candidateCount':response.get('totalCount'),'exactFound':search in response.get('values',[]), 'echoMatches':all(response.get(key)==query[key] for key in ('releaseId','period','filters','field','offset','limit')) and response.get('selectedStages')==stages,'candidateOnly':response.get('candidateOnly')}) _atomic_json(actor_root/'private'/'source-lookup-audit.json',source_lookup_audit[-100:]) return response planning_options['source_value_resolver'] = source_value_resolver planned = retry_task if isinstance(retry_task,dict) and retry_task else plan_agent_task(base_url=selected_connection[0], api_key=selected_connection[1], model=selected_connection[2], question=request['question'], context=planning_context, previous=previous_task, cancel_event=cancel_event, diagnostic_path=actor_root / 'private' / 'task-plan-failure.json', diagnostic_turn_id=request['turnId'], semantic_review=request['restartConversation'] or bool(planning_context.get('_serverQuestions')), fresh_retry=request['restartConversation'], **planning_options) catalog = planning_context.get('_businessCatalog') if isinstance(planning_context, dict) else None if isinstance(catalog, dict) and isinstance(catalog.get('groupDepartments'), dict): request['_scopeDirectory'] = {'groupDepartments': copy.deepcopy(catalog['groupDepartments'])} request['context'].pop('_businessCatalog', None) if isinstance(protocol,dict) and protocol.get('version') == 1: if planned is None: raise RuntimeErrorSafe('未形成可提交的任务提案。') planning_ms=round((time.monotonic()-protocol_planning_started)*1000) confirmation_started=time.monotonic() _atomic_json(actor_root / 'private' / 'proposed-agent-task.json', { 'turnId': request['turnId'], 'task': planned}) authority = delegated.call('/api/analysis/protocol/task',payload=planned,method='POST') if not isinstance(authority.get('task'),dict) or authority.get('attemptId') != request['sessionId']: raise RuntimeErrorSafe('后台未确认当前任务。') from nao_protocol_execution import execute request['_autonomousAnswer'] = protocol.get('autonomousEnabled') is True if authority['task'].get('executionMode') == 'native_agent': model_gate.native_agent = True from nao_native_run import run_native_exploration return run_native_exploration(request=request, task=authority['task'], authority=authority, actor_root=actor_root, delegated=delegated, connection=selected_connection, manager=manager, state=state, cancel_event=cancel_event, emit=emit, prior_evidence_files=prior_evidence_files, phase_timings={'planningAndReview':planning_ms, 'taskConfirmation':round((time.monotonic()-confirmation_started)*1000)}) return execute(request=request,task=authority['task'],authority=authority,actor_root=actor_root, delegated=delegated,connection=selected_connection,cancel_event=cancel_event,emit=emit, phase_timings={'planningAndReview':planning_ms,'taskConfirmation':round((time.monotonic()-confirmation_started)*1000)}) if planned is not None: if request['restartConversation'] and planned['kind'] in ('analysis','metrics'): planned['freshQuery'] = True request['context'] = bind_task(request['context'], planned) if isinstance(planned.get('directResponse'), dict) and not (planned.get('verifySource') or planned.get('fullDetails')): answer = planned['directResponse']['answer'] cancelled = cancel_event.is_set() conversation = ConversationAccumulator() if not cancelled: conversation.record_event('assistant', text=public_progress_text(answer)) _atomic_json(actor_root / 'private' / 'last-conversation-answer.json', { 'turnId':request['turnId'],'goal':planned['goal'], 'purpose':planned['directResponse']['purpose'],'answer':answer}) _record_non_query_turn(actor_root, request) emit({'type':'answer','answer':answer}) result = {'type':'result','status':'CANCELLED' if cancelled else 'COMPLETED', 'engine':'NAO','answer':'' if cancelled else answer,'toolCalls':[], 'timeline':conversation.timeline,'queryEvidenceRefs':[], 'stopReason':'用户停止' if cancelled else 'stop','agentTask':planned, 'completionMethod':'direct_conversation','visualSchemaVersion':1, 'visualState':'not_requested','visuals':[], 'agentContext':{key:value for key,value in request['context'].items() if key in ('releaseId','period','comparisonPeriod','filters')}} emit(result) return result _atomic_json(last_task_path, {'releaseId': request['releaseId'], 'sessionId': request['sessionId'], 'turnId':request['turnId'], 'baseMetricFrame': base_metric_frame, 'task': planned}) if planned['needsClarification']: result = {'type': 'result', 'status': 'NEEDS_CLARIFICATION', 'engine': 'NAO', 'answer': planned['clarification'], 'stopReason': 'needs_clarification', 'agentTask': planned, 'visualState': 'not_requested', 'visuals': [], 'toolCalls': []} emit(result) return result if (planned['kind'] == 'report' and planned.get('historyMode') in ('all', 'last') and not planned['freshQuery'] and not (planned.get('verifySource') or planned.get('fullDetails'))): return historical_report(request=request, task=planned, actor_root=actor_root, connection=selected_connection, cancel_event=cancel_event, emit=emit) # Start/authenticate the tool-bearing native agent only after the model # selects work that needs it. Plain replies do not create a native chat, # automatic title or memory extraction, and do not replace business state. model_base, model_key, model_name, base_url = manager.ensure_instance( actor_root, request['sessionId'], expected_actor_id=request['actorId'], model_connection=selected_connection) del model_base, model_key client = _authenticate(actor_root, base_url) _atomic_json(controls_path,effective_controls) # A restarted run retains its explicit goal, but cannot inherit an old # request or evidence. Normal follow-ups inherit only verified state. if planned is not None: from nao_metric_request import metric_request current_metric_request = metric_request(request['question'], request['context']) else: request['context'], current_metric_request = prepare_metric_context( request['question'], request['context'], previous_visible if native_chat_id else None) fresh_query = request['restartConversation'] or (planned['freshQuery'] if planned else requires_fresh_query(request['question'])) or current_metric_request is not None primary_call = None from nao_agent_contract import primary_query_required if (planned is not None and planned['kind'] in ('analysis', 'metrics') and planned.get('period') and primary_query_required(planned)): # Execute the model's resolved primary frame before investigating # children. Local drilldowns can then never replace the parent KPI. from nao_evidence_finalizer import _compact arguments = {'mode': 'compare' if planned.get('comparisonPeriod') else 'query', 'releaseId': request['releaseId'], 'period': planned['period'], 'filters': planned['filters']} if planned.get('comparisonPeriod'): arguments['comparisonPeriod'] = planned['comparisonPeriod'] emit({'type': 'progress', 'message': '正在按本轮目标核对正式指标…'}) started = datetime.now(timezone.utc).isoformat() primary = read_primary_evidence(arguments=arguments, request=request, actor_root=actor_root, access_path=access_path, manager=manager) if primary is not None: request['context']['primaryEvidence'] = _compact(primary) if len(json.dumps(request['context'], ensure_ascii=False)) > MAX_CONTEXT_CHARS: request['context'].pop('lastVisibleAnswer', None) primary_call = {'tool': 'quality_data', 'toolCallId': 'primary-' + primary['evidenceRef'], **public_tool_call('mcp_call', {'tool': 'quality_data', 'arguments': arguments}), 'status': 'completed', 'startedAt': started, 'finishedAt': datetime.now(timezone.utc).isoformat(), 'outputSummary': public_tool_result(primary)} 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, timeout=native_timeout(), prior_evidence_files=prior_evidence_files, turn_id=request['turnId'], ) if primary_call is not None: accumulator.tool_calls.insert(0, primary_call) accumulator.record_event('tool_result', tool='quality_data', toolCallId=primary_call['toolCallId'], label=primary_call['label'], status='completed', text=primary_call['outputSummary']) fresh_missing = False if fresh_query and status == "COMPLETED": from analysis_visual_evidence import load_visual_evidence current_documents = load_visual_evidence(actor_root / "evidence", request["releaseId"], exclude_files=prior_evidence_files, budget_seconds=2.0) if not any(doc.get("result", {}).get("kind") in { "quality_query", "quality_metrics", "quality_weight_ranking", "quality_comparison", "quality_changes", "quality_fine_defects", "quality_details", "quality_investigation"} for doc in current_documents): status, stop_reason = "PARTIAL", "evidence_incomplete" fresh_missing = True accumulator.blocks["retry-without-evidence"] = "本次重新分析未取得新的查询证据,不能将旧结论作为重新分析成功的结果。请重试。" accumulator.block_order.append("retry-without-evidence") # The model may investigate one stage of a priority product and forget # the other. Complete the same release/period/filter pair before the # evidence-based answer and charts are assembled. Never borrow a result # from another turn or infer a missing stage from the first one. pairable_stop = status in ("COMPLETED", "PARTIAL") or ( status == "FAILED" and stop_reason in ("length", "timeout", "超时停止", "coverage_ready") and stop_succeeded is True) if planned is not None and pairable_stop and not cancel_event.is_set(): complete_agent_evidence(task=planned, request=request, actor_root=actor_root, access_path=access_path, manager=manager, connection=selected_connection, accumulator=accumulator, prior_files=prior_evidence_files, cancel_event=cancel_event, emit=emit) if fresh_missing: from nao_evidence_finalizer import evidence_summaries, coverage_sufficient, FinalizationUnavailable try: repaired = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_evidence_files) if coverage_sufficient(repaired, request['question'], request['context']): status, stop_reason = 'COMPLETED', 'stop' accumulator.blocks.pop('retry-without-evidence', None) accumulator.block_order = [key for key in accumulator.block_order if key != 'retry-without-evidence'] except (FinalizationUnavailable, ValueError, OSError, KeyError, TypeError): pass if planned is None and pairable_stop and not cancel_event.is_set(): try: from analysis_visual_evidence import load_visual_evidence from nao_product_defect_pairing import plan_missing_product_defect_stages from nao_quality_tools import QualityTools documents = load_visual_evidence(actor_root / "evidence", request["releaseId"], exclude_files=prior_evidence_files, budget_seconds=2.0) missing_stages = plan_missing_product_defect_stages( documents, request["question"], request["context"]) if missing_stages: pairing_api = DelegatedApi(access_path, runtime_root=manager.users_root, expected_actor_id=request["actorId"], expected_session_id=request["sessionId"], timeout=SUPPLEMENT_QUERY_TIMEOUT_SECONDS).login() quality_tools = QualityTools(api_factory=lambda: pairing_api, evidence_dir=actor_root / "evidence", query_contract={'task':planned,'releaseId':request['releaseId']} if planned else None) for arguments in missing_stages: if cancel_event.is_set(): break stage_name = "黑片" if arguments["stage"] == "black" else "亮片" emit({"type": "progress", "message": f"正在补查同规格的{stage_name}具体缺陷…"}) try: paired = quality_tools.quality_data(arguments) except (ValueError, OSError, RuntimeError): emit({"type": "progress", "message": f"{stage_name}具体缺陷补查未成功,结论将保留该阶段的数据限制。"}) continue call = {"tool": "quality_data", "toolCallId": "pair-" + paired["evidenceRef"], "label": f"补查{stage_name}具体缺陷", "status": "completed", "startedAt": datetime.now(timezone.utc).isoformat(), "finishedAt": datetime.now(timezone.utc).isoformat(), "outputSummary": f"同规格、同期间补查完成;依据 {paired['evidenceRef']}"} accumulator.tool_calls.append(call) accumulator.record_event("tool_result", tool=call["tool"], toolCallId=call["toolCallId"], label=call["label"], status="completed", text=call["outputSummary"]) except (ValueError, OSError, RuntimeError): emit({"type": "progress", "message": "阶段配对检查未完成,已有查询结果仍按原范围呈现。"}) # A broad question has a fixed minimum set of business dimensions. # Complete omitted read-only queries in the same frozen frame. The # final coverage gate still reports a partial answer if any read fails. if planned is None and pairable_stop and not cancel_event.is_set(): try: from analysis_visual_evidence import load_visual_evidence from nao_analysis_coverage import plan_missing_queries from nao_quality_tools import QualityTools documents = load_visual_evidence(actor_root / "evidence", request["releaseId"], exclude_files=prior_evidence_files, budget_seconds=2.0) missing_queries = plan_missing_queries(documents, request["question"], request["context"]) if missing_queries: completion_api = DelegatedApi(access_path, runtime_root=manager.users_root, expected_actor_id=request["actorId"], expected_session_id=request["sessionId"], timeout=SUPPLEMENT_QUERY_TIMEOUT_SECONDS).login() quality_tools = QualityTools(api_factory=lambda: completion_api, evidence_dir=actor_root / "evidence", query_contract={'task':planned,'releaseId':request['releaseId']} if planned else None) labels = {"department": "产业部变化", "group": "业务分组变化", "product": "产品变化"} for arguments in missing_queries[:5]: if cancel_event.is_set(): break label = labels.get(arguments.get("dimension"), ("黑片" if arguments.get("stage") == "black" else "亮片") + "具体缺陷") emit({"type": "progress", "message": f"正在补查{label}…"}) try: completed = quality_tools.quality_data(arguments) except (ValueError, OSError, RuntimeError): emit({"type": "progress", "message": f"{label}未查询成功;最终答复将标明缺口。"}) continue call = {"tool": "quality_data", "toolCallId": "coverage-" + completed["evidenceRef"], "label": f"补查{label}", "status": "completed", "startedAt": datetime.now(timezone.utc).isoformat(), "finishedAt": datetime.now(timezone.utc).isoformat(), "outputSummary": f"同期间同范围补查完成;依据 {completed['evidenceRef']}"} accumulator.tool_calls.append(call) accumulator.record_event("tool_result", tool=call["tool"], toolCallId=call["toolCallId"], label=call["label"], status="completed", text=call["outputSummary"]) except (ValueError, OSError, RuntimeError): emit({"type": "progress", "message": "覆盖检查未完成;已有证据仍会按实际范围呈现。"}) from nao_evidence_finalizer import eligible, evidence_summaries, synthesize, coverage_sufficient, FinalizationUnavailable finalized = False evidence_completion = None generated_title = None verified_facts = False if planned is not None and not cancel_event.is_set(): try: from nao_agent_report import build_agent_report checked = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_evidence_files) checked_report = build_agent_report(checked, request['context'], request['question']) verified_facts = bool(checked_report.get('facts')) except (FinalizationUnavailable, OSError, ValueError, KeyError, TypeError): pass # A failed supplementary read must not discard independently verified # facts. Final coverage still decides whether the result is complete. if eligible(model_name, status, stop_reason, accumulator.tool_calls, cancel_event.is_set(), stop_succeeded, verified_facts): final_call = None evidence = None try: native_answer = accumulator.answer native_completed = status == "COMPLETED" evidence = evidence_summaries(actor_root / "evidence", request["releaseId"], exclude_files=prior_evidence_files) if planned is None and native_completed and not any(d["result"].get("kind")=="quality_comparison" for d in evidence): raise FinalizationUnavailable("本轮原生答复已完成,无期间比较需要整理") # An administrator may switch profiles while this turn runs. Its # one synthesis uses the original connection, just like native tools. selected_base, selected_key, selected_name = selected_connection if cancel_event.is_set(): raise FinalizationUnavailable("已取消") final_call = {"tool":"model.finalize", "toolCallId":"finalize-"+request["turnId"], "label":"整理已查询证据", "status":"started", "startedAt":datetime.now(timezone.utc).isoformat()} accumulator.tool_calls.append(final_call) accumulator.record_event("tool_start",tool=final_call["tool"],toolCallId=final_call["toolCallId"],label=final_call["label"],status="started") emit({"type":"progress","message":"查询证据已返回,正在进行一次有限的最终答复整理…","toolCalls":accumulator.tool_calls,"toolCount":len(accumulator.tool_calls),"timeline":accumulator.timeline}) final = synthesize(base_url=selected_base,api_key=selected_key,model=selected_name,question=request["question"],evidence=evidence,cancel_event=cancel_event,context=request["context"]) generated_title = final.get('reportTitle') if planned is not None and planned['fullDetails']: from nao_agent_report import build_agent_report detail_report = build_agent_report(evidence, request['context'], request['question']) for section in detail_report['sections']: if section not in final['answer']: final['answer'] += '\n\n' + section del selected_key if cancel_event.is_set(): raise FinalizationUnavailable("已取消") final_call.update(status="completed",finishedAt=datetime.now(timezone.utc).isoformat(),outputSummary="已按本轮真实查询证据生成最终答复") accumulator.record_event("tool_result",tool=final_call["tool"],toolCallId=final_call["toolCallId"],label=final_call["label"],status="completed",text=final_call["outputSummary"]) accumulator.blocks["evidence-final"] = final["answer"] accumulator.block_order.append("evidence-final") # Keep the final answer once. The preceding native draft is not a # second business answer after the verified report replaces it. accumulator.timeline = [entry for entry in accumulator.timeline if not (entry.get("kind") == "assistant" and native_answer and entry.get("text") == public_progress_text(native_answer))] accumulator.record_event("assistant",text=public_progress_text(final["answer"])) server_coverage = coverage_sufficient(evidence,request["question"],request["context"]) status = "COMPLETED" if final["evidenceSufficient"] and server_coverage else "PARTIAL" completion = {"modelDeclaredEvidenceSufficient":final.get("modelDeclaredEvidenceSufficient", final["evidenceSufficient"]), "serverCoverageSufficient":server_coverage,"missingEvidence":final["missingEvidence"], "modelMissingEvidence":final.get("modelMissingEvidence",[]), "resolvedModelMissingEvidence":final.get("resolvedModelMissingEvidence",[]), "answerFocusSatisfied":final.get('answerFocusSatisfied'), "answerTarget":(final.get('answerFocus') or {}).get('dimension'), "completionAdjusted":final.get("completionAdjusted",False)} _atomic_json(actor_root / "finalization.json", {"turnId":request["turnId"],"status":status,"nativeStopReason":stop_reason,"model":final["model"],"requestParameters":{"max_completion_tokens":MAX_OUTPUT_TOKENS,"stream":False},"reportTitle":generated_title,"titleGeneratedByModel":final.get('titleGeneratedByModel',False),"evidenceRefs":[d["evidenceRef"] for d in evidence],"usage":final["usage"],"finishReason":final["finishReason"],"compositionRepairAttempted":final.get("compositionRepairAttempted",False),"selectionFallback":final.get("selectionFallback",False),"selectionFallbackReason":final.get("selectionFallbackReason"),"modelAttempts":final.get("modelAttempts",1),"attemptUsages":final.get("attemptUsages",[final.get("usage",{})]),**completion}) evidence_completion = completion stop_reason, finalized = "evidence_finalized" if status=="COMPLETED" else "evidence_incomplete", True accumulator.finish_reasons.append("stop") except (FinalizationUnavailable, OSError, ValueError) as error: # A completed native draft is not a completed analysis when # this turn's comparison evidence fails the server coverage gate. native_stop_reason = stop_reason if not cancel_event.is_set() and status == 'COMPLETED' and planned is not None: # Coverage alone does not prove that the draft actually # answered the requested object when synthesis failed. status, stop_reason = 'PARTIAL', 'answer_unverified' accumulator.blocks['answer-unverified'] = '本轮答复尚未完成核验,已保留查询依据,请重新分析。' accumulator.block_order.append('answer-unverified') if not cancel_event.is_set() and status == "COMPLETED": has_comparison = bool(evidence and any( d.get("result", {}).get("kind") == "quality_comparison" for d in evidence)) if has_comparison: try: if not coverage_sufficient(evidence, request["question"], request["context"]): status, stop_reason = "PARTIAL", "evidence_incomplete" except (KeyError, ValueError, TypeError): status, stop_reason = "PARTIAL", "evidence_incomplete" _atomic_json(actor_root / "finalization.json", {"turnId":request["turnId"],"status":"CANCELLED" if cancel_event.is_set() else status,"nativeStopReason":native_stop_reason,"reason":str(error) if isinstance(error,FinalizationUnavailable) else "证据整理不可用"}) if final_call is not None: final_call.update(status="failed",finishedAt=datetime.now(timezone.utc).isoformat(),outputSummary="有限收尾未完成;查询证据和过程已保留") accumulator.record_event("tool_result",tool=final_call["tool"],toolCallId=final_call["toolCallId"],label=final_call["label"],status="failed",text=final_call["outputSummary"]) if cancel_event.is_set(): status,stop_reason="CANCELLED","用户停止" if (planned is not None and planned['kind'] in ('analysis','metrics') and planned['freshQuery'] and status == 'COMPLETED' and not finalized and not cancel_event.is_set()): # A completed native stream or covered query alone cannot verify # that the answer matches the current question and source values. status, stop_reason = 'PARTIAL', 'answer_unverified' accumulator.blocks['answer-unverified'] = '本轮答复尚未完成核验,已保留查询依据,请重新分析。' accumulator.block_order.append('answer-unverified') # Native completion is insufficient for a parsed metric request unless # this turn itself proves its whole requested frame and metric set. if current_metric_request is not None and status == 'COMPLETED' and not cancel_event.is_set(): from nao_analysis_coverage import formal_metric_coverage try: checked = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_evidence_files) contract = formal_metric_coverage(checked, request['question'], request['context']) metric_gaps = contract['missingEvidence'] if contract else ['本轮正式指标需求未取得可核对的证据。'] except (FinalizationUnavailable, OSError, ValueError, KeyError, TypeError): metric_gaps = ['本轮正式指标需求未取得新的、可核对的查询证据。'] if metric_gaps: status, stop_reason = 'PARTIAL', 'evidence_incomplete' accumulator.blocks['metric-coverage-gap'] = accumulator.answer + '\n\n' + '\n'.join(metric_gaps) accumulator.block_order.append('metric-coverage-gap') if cancel_event.is_set(): status, stop_reason = 'CANCELLED', '用户停止' if (planned is not None and (planned['freshQuery'] or planned.get('verifySource') or planned.get('fullDetails')) and status in ('COMPLETED','PARTIAL') and not cancel_event.is_set()): try: checked = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_evidence_files) sufficient = coverage_sufficient(checked, request['question'], request['context']) except (FinalizationUnavailable, OSError, ValueError, KeyError, TypeError): sufficient = False if not sufficient: status, stop_reason = 'PARTIAL', 'evidence_incomplete' if (planned.get('verifySource') or planned.get('fullDetails')) and not finalized: # Do not display an unsupported native claim that all rows # were verified; the actual query evidence stays available. accumulator.blocks = {'required-read-gap':'本轮原表核验或原始记录读取尚未覆盖完整范围,已保留查询依据。'} accumulator.block_order = ['required-read-gap'] accumulator.timeline = [entry for entry in accumulator.timeline if entry.get('kind') != 'assistant'] if (planned is not None and planned['kind'] in ('explain','restate') and not planned['freshQuery'] and (planned.get('historyMode') in ('last','all') or planned.get('requiresVerifiedEvidence') or planned.get('comparisonRequired')) and status == 'COMPLETED' and (planned.get('requiresVerifiedEvidence') or planned.get('comparisonRequired') or planned.get('fields') or planned.get('dimensions')) and not cancel_event.is_set()): # A previous question, or even a completed but differently scoped # answer, is not evidence for this turn's requested frame. from nao_metric_request import frozen_metric_frame expected = frozen_metric_frame(request['context']) matching_refs = set() for entry in request['context'].get('_serverHistory', []): if not isinstance(entry,dict) or not isinstance(entry.get('context'),dict): continue frame = frozen_metric_frame(entry['context']) if all(frame.get(key) == expected.get(key) for key in ('releaseId','filters','period','comparisonPeriod','stage')): matching_refs.update(ref for ref in entry.get('evidenceRefs', []) if isinstance(ref,str)) try: historical = evidence_summaries(actor_root/'evidence',request['releaseId'],allow_empty=True) historical = [doc for doc in historical if doc.get('evidenceRef') in matching_refs] current = evidence_summaries(actor_root/'evidence',request['releaseId'],exclude_files=prior_evidence_files,allow_empty=True) supported = coverage_sufficient(historical,request['question'],request['context']) if historical else False supported = supported or (bool(current) and coverage_sufficient(current,request['question'],request['context'])) except (FinalizationUnavailable, OSError, ValueError, KeyError, TypeError): supported = False if not supported: status, stop_reason = 'PARTIAL', 'evidence_incomplete' accumulator.blocks['history-unverified'] = '未找到同范围、同期间的已核实历史依据,无法安全复述;请重新分析。' accumulator.block_order.append('history-unverified') else: from nao_agent_task import verify_history_answer, TaskUnavailable try: aligned = verify_history_answer(base_url=selected_connection[0],api_key=selected_connection[1], model=selected_connection[2],question=request['question'],task=planned, history=request['context'].get('_serverHistory', []),answer=accumulator.answer,cancel_event=cancel_event) except (TaskUnavailable, OSError, ValueError, TypeError): aligned = False if not aligned: status, stop_reason = 'PARTIAL', 'answer_unverified' accumulator.blocks['history-answer-unverified'] = '本轮答复尚未完成核验,已保留查询依据,请重新分析。' accumulator.block_order.append('history-answer-unverified') prepared_visuals = None from nao_weight_ranking import ranking_task,missing_ranking_visuals if ranking_task(planned) and status == 'COMPLETED' and not cancel_event.is_set(): from analysis_visual_evidence import visual_result prepared_visuals = visual_result(actor_root / 'evidence',request['releaseId'],request['question'],request['turnId'], exclude_files=prior_evidence_files,cancel_event=cancel_event,context=request['context']) missing_visuals = missing_ranking_visuals(planned,prepared_visuals.get('visuals',[])) if missing_visuals: status,stop_reason='PARTIAL','visual_incomplete' accumulator.blocks['required-visual-gap'] = accumulator.answer+'\n\n尚未完成:'+'、'.join(missing_visuals)+'。已保留有效查询依据。' accumulator.block_order.append('required-visual-gap') 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" if status == "COMPLETED" and answer: remembered_request = current_metric_request if (remembered_request is None and is_history_restatement(request['question']) and isinstance(previous_visible, dict) and previous_visible.get('baseMetricFrame') == base_metric_frame): remembered_request = previous_visible.get('metricRequest') _atomic_json(visible_answer_path, {"releaseId":request["releaseId"],"sessionId":request["sessionId"], "turnId":request["turnId"],"answer":answer, "baseMetricFrame":base_metric_frame,"metricRequest":remembered_request}) result = { "type": "result", "status": status, "engine": "NAO", "answer": answer, "nativeChatId": accumulator.chat_id, "completionMethod": "bounded_evidence_finalization" if finalized else "native", "timeline": accumulator.timeline, "toolCalls": accumulator.tool_calls, "stopReason": stop_reason, "nativeStopSucceeded": stop_succeeded, "agentTask": planned, "reportTitle": generated_title, "agentContext": ({k: v for k, v in request['context'].items() if k in {'releaseId', 'period', 'comparisonPeriod', 'stage', 'filters'}} if planned is not None else None), "finishReason": accumulator.finish_reasons[-1] if accumulator.finish_reasons else None, } if finalized: result["evidenceCompletion"] = evidence_completion if answer and status in ("COMPLETED", "PARTIAL") and not cancel_event.is_set(): from analysis_visual_evidence import visual_result result.update(prepared_visuals if prepared_visuals is not None else visual_result(actor_root / "evidence", request["releaseId"], request["question"], request["turnId"], exclude_files=prior_evidence_files, cancel_event=cancel_event,context=request["context"])) if report_markdown is not None: result["reportMarkdown"] = report_markdown if report_html is not None: result["reportHtml"] = report_html try: current_evidence = evidence_summaries(actor_root / 'evidence', request['releaseId'], exclude_files=prior_evidence_files, allow_empty=True) result['queryEvidenceRefs'] = sorted({doc['evidenceRef'] for doc in current_evidence if re.fullmatch(r'Q[A-F0-9]{8}', str(doc.get('evidenceRef', '')))}) except (FinalizationUnavailable, OSError, ValueError, TypeError): result['queryEvidenceRefs'] = [] if (status == 'COMPLETED' and planned is not None and planned['kind'] in ('explain','restate') and not planned['freshQuery'] and not result['queryEvidenceRefs'] and not any(call.get('tool') in ('quality_data','mcp_call') for call in accumulator.tool_calls)): _record_non_query_turn(actor_root, request) emit(result) return result except SourceCoverageUnavailable as error: if actor_root is not None: _atomic_json(actor_root / 'private' / 'source-coverage-gap.json', { 'turnId': request.get('turnId'), 'period':error.period, 'message':str(error)}) result = {'type':'result','status':'FAILED','engine':'NAO','answer':'', 'toolCalls':[], 'stopReason':'missing_source','message':str(error)} 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 Exception as error: if actor_root is not None: try: import traceback from nao_thinking_stream import public_thinking_text _atomic_json(actor_root / 'private' / 'last-runtime-error.json', { 'turnId': request.get('turnId'), 'errorType': type(error).__name__, 'trace': public_thinking_text(traceback.format_exc())[-16000:]}) except (OSError, ValueError, TypeError): pass result = {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "", "toolCalls": [], "stopReason": "runtime_error", "message": "分析执行未完成,已保留当前问题和条件,请重试本轮。"} emit(result) return result finally: thinking.close() if model_gate is not None: try: _atomic_json(actor_root/'private'/'last-model-audit.json',{'model':selected_model,'requests':model_gate.request_audit, 'rejectedToolCalls':model_gate.rejected_tool_calls if isinstance(model_gate.rejected_tool_calls,int) else 0, 'rejectedStaleRequests':model_gate.rejected_stale_requests if isinstance(model_gate.rejected_stale_requests,int) else 0}) except (OSError,TypeError,ValueError):pass model_gate.close() 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())