1863 lines
88 KiB
Python
1863 lines
88 KiB
Python
"""Run an isolated, delegated-auth NAO quality conversation as an NDJSON worker.
|
|
|
|
The worker keeps NAO's native engine and MCP protocol intact. The quality MCP
|
|
uses DelegatedApi, which can only call the local, actor-scoped runtime-query API.
|
|
Credentials are read from stdin and kept in a mode-0600 file outside the NAO
|
|
project; they are never passed to the model or written to worker output.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import contextlib
|
|
import fcntl
|
|
import hashlib
|
|
import http.cookiejar
|
|
import json
|
|
import os
|
|
import re
|
|
import secrets
|
|
import shutil
|
|
import signal
|
|
import socket
|
|
import 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_URL = "http://127.0.0.1:5281/api/analysis/runtime/query"
|
|
MAX_INSTANCES = 2
|
|
TURN_TIMEOUT_SECONDS = 600
|
|
SSE_IDLE_TIMEOUT_SECONDS = 60
|
|
NATIVE_STOP_TIMEOUT_SECONDS = 4.0
|
|
CHAT_ID_WAIT_SECONDS = 0.75
|
|
MAX_QUESTION_CHARS = 20_000
|
|
MAX_CONTEXT_CHARS = 32_000
|
|
MAX_HTML_BYTES = 12 * 1024 * 1024
|
|
|
|
_UUID_RE = re.compile(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$")
|
|
_ALLOWED_PATHS = {
|
|
"GET": {
|
|
"/api/releases", "/api/options", "/api/metrics",
|
|
"/api/analysis/tools/catalog", "/api/analytics/top",
|
|
},
|
|
"POST": {"/api/analysis/tools/details", "/api/analysis/defects"},
|
|
}
|
|
|
|
|
|
class RuntimeErrorSafe(RuntimeError):
|
|
"""An error safe to return without exposing credentials or request bodies."""
|
|
|
|
|
|
def _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 != 5281
|
|
or parsed.path != "/api/analysis/runtime/query" or parsed.username or parsed.password
|
|
or parsed.query or parsed.fragment):
|
|
raise ValueError("质量查询地址必须是固定的本机接口。")
|
|
return AccessSnapshot(actor_id, token, release_id, RUNTIME_QUERY_URL)
|
|
|
|
|
|
def 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
|
|
|
|
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) -> 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("质量查询参数格式无效。")
|
|
wrapper = {"token": snapshot.token, "method": verb, "path": path, "payload": payload}
|
|
request = Request(
|
|
snapshot.query_url,
|
|
data=json.dumps(wrapper, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode("utf-8"),
|
|
headers={"Accept": "application/json", "Content-Type": "application/json"},
|
|
method="POST",
|
|
)
|
|
try:
|
|
with self.opener.open(request, timeout=self.timeout) as response:
|
|
raw = response.read(8 * 1024 * 1024 + 1)
|
|
except HTTPError as error:
|
|
# Runtime API may return a safe, actor-scoped detail (for example 403/409).
|
|
try:
|
|
detail = json.loads(error.read(64 * 1024).decode("utf-8", "replace"))
|
|
except Exception:
|
|
detail = None
|
|
safe_detail = detail.get("detail") if isinstance(detail, dict) else None
|
|
if isinstance(safe_detail, str) and len(safe_detail) <= 600:
|
|
raise RuntimeErrorSafe(safe_detail.replace(snapshot.token, "[REDACTED]")) from None
|
|
raise RuntimeErrorSafe(f"质量中心查询暂不可用(HTTP {error.code})。") from None
|
|
except (URLError, OSError, TimeoutError):
|
|
raise RuntimeErrorSafe("质量中心查询暂不可用,请确认本机质量服务后重试。") from None
|
|
if len(raw) > 8 * 1024 * 1024:
|
|
raise RuntimeErrorSafe("质量中心查询响应超过允许大小。")
|
|
try:
|
|
result = json.loads(raw.decode("utf-8")) if raw else {}
|
|
except (UnicodeDecodeError, json.JSONDecodeError):
|
|
raise RuntimeErrorSafe("质量中心查询返回格式无效。") from None
|
|
if not isinstance(result, dict):
|
|
raise RuntimeErrorSafe("质量中心查询返回格式无效。")
|
|
return _redact_credentials(result, snapshot.token)
|
|
|
|
|
|
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": "核查来源明细"}
|
|
allowed = {"mode", "scope", "period", "comparisonPeriod", "filters", "view", "stage", "dimension",
|
|
"defect", "category", "limit", "sort"}
|
|
nested = {"year", "month", "quarter", "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 tool_operation_key(tool: str, raw_input: Any) -> str:
|
|
value = json.loads(json.dumps(raw_input)) if isinstance(raw_input, dict) else {}
|
|
if tool == "mcp_call" and value.get("tool") == "quality_data":
|
|
args = value.get("arguments")
|
|
if isinstance(args, dict) and args.get("mode") == "compare" and args.get("view") == "overall":
|
|
# `compare` already operates on the selected overall scope; an explicit
|
|
# overall view is the same investigation when the model corrects it.
|
|
args.pop("view")
|
|
encoded = json.dumps([tool, value], sort_keys=True, ensure_ascii=False)
|
|
return hashlib.sha256(encoded.encode()).hexdigest()
|
|
|
|
|
|
class ConversationAccumulator:
|
|
"""Decode public text/tool events, ignoring reasoning and raw tool payloads."""
|
|
|
|
def __init__(self, on_answer: Callable[[str], None] | None = None) -> None:
|
|
self.blocks: dict[str, str] = {}
|
|
self.block_order: list[str] = []
|
|
self.active_text_id = "text"
|
|
self.finish_reasons: list[str] = []
|
|
self.tool_calls: list[dict[str, Any]] = []
|
|
self.tool_outputs: dict[str, Any] = {}
|
|
self.tool_error = False
|
|
self.unattributed_tool_error = False
|
|
self.tool_operation_keys: dict[int, str] = {}
|
|
self.error_event = False
|
|
self.on_answer = on_answer
|
|
self.chat_id: str | None = None
|
|
self.done_received = False
|
|
|
|
def feed(self, event: dict[str, Any]) -> None:
|
|
kind = event.get("type")
|
|
if kind == "data-newChat":
|
|
value = (event.get("data") or {}).get("id") if isinstance(event.get("data"), dict) else None
|
|
if isinstance(value, str) and value and len(value) <= 256:
|
|
self.chat_id = value
|
|
elif kind == "text-start":
|
|
block_id = event.get("id")
|
|
if isinstance(block_id, str) and block_id:
|
|
self.active_text_id = block_id
|
|
if block_id not in self.blocks:
|
|
self.blocks[block_id] = ""
|
|
self.block_order.append(block_id)
|
|
elif kind == "text-delta":
|
|
block_id = event.get("id") if isinstance(event.get("id"), str) else self.active_text_id
|
|
self.active_text_id = block_id
|
|
if block_id not in self.blocks:
|
|
self.blocks[block_id] = ""
|
|
self.block_order.append(block_id)
|
|
delta = event.get("delta")
|
|
if isinstance(delta, str):
|
|
self.blocks[block_id] += delta
|
|
if self.on_answer:
|
|
self.on_answer(self.blocks[block_id])
|
|
elif kind == "finish":
|
|
reason = event.get("finishReason")
|
|
if isinstance(reason, str):
|
|
self.finish_reasons.append(reason)
|
|
elif kind == "tool-input-available":
|
|
tool_name = event.get("toolName")
|
|
if isinstance(tool_name, str):
|
|
call_id = event.get("toolCallId")
|
|
call = {"tool": tool_name, "status": "started", "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)
|
|
if isinstance(event.get("input"), dict):
|
|
self.tool_operation_keys[id(call)] = tool_operation_key(tool_name, event["input"])
|
|
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"] = "工具返回错误,需要调整后继续" if item["status"] == "failed" else "已返回结果"
|
|
if not output_failed:
|
|
key = self.tool_operation_keys.get(id(item))
|
|
for previous in self.tool_calls:
|
|
if previous is item:
|
|
break
|
|
if key is not None and previous["status"] == "failed" and self.tool_operation_keys.get(id(previous)) == key:
|
|
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"] == "started":
|
|
matched = True
|
|
item["status"] = "failed"
|
|
item["finishedAt"] = datetime.now(timezone.utc).isoformat()
|
|
item["outputSummary"] = "本次调用未成功"
|
|
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 answer(self) -> str:
|
|
for block_id in reversed(self.block_order):
|
|
value = self.blocks.get(block_id, "").strip()
|
|
if value:
|
|
return value
|
|
return ""
|
|
|
|
def status(self, *, cancel_cause: str | None = None, stop_succeeded: bool | None = None,
|
|
transport_failed: bool = False) -> tuple[str, str | None]:
|
|
if cancel_cause == "user":
|
|
return "CANCELLED", "user_cancel"
|
|
if cancel_cause == "timeout":
|
|
return ("PARTIAL" if self.answer else "FAILED", "timeout")
|
|
if cancel_cause == "transport":
|
|
if any(reason == "length" for reason in self.finish_reasons):
|
|
return "PARTIAL", "length"
|
|
return ("PARTIAL" if self.answer else "FAILED", "transport_or_stream_error")
|
|
if any(reason == "length" for reason in self.finish_reasons):
|
|
return "PARTIAL", "length"
|
|
if transport_failed or self.error_event or not self.finish_reasons:
|
|
return ("PARTIAL" if self.answer else "FAILED", "transport_or_stream_error")
|
|
if self.tool_error 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.answer and (self.done_received or "stop" in self.finish_reasons):
|
|
return "COMPLETED", "stop"
|
|
return ("PARTIAL" if self.answer else "FAILED", "incomplete")
|
|
|
|
|
|
def _contains_error_flag(value: Any) -> bool:
|
|
if isinstance(value, dict):
|
|
if value.get("isError") is True or value.get("error"):
|
|
return True
|
|
return any(_contains_error_flag(item) for item in value.values())
|
|
if isinstance(value, list):
|
|
return any(_contains_error_flag(item) for item in value)
|
|
if isinstance(value, str) and len(value) <= 64 * 1024:
|
|
stripped = value.strip()
|
|
if stripped.startswith(("{", "[")):
|
|
try:
|
|
return _contains_error_flag(json.loads(stripped))
|
|
except json.JSONDecodeError:
|
|
return False
|
|
return False
|
|
|
|
|
|
class NativeStopController:
|
|
"""Stop the native NAO chat before closing its SSE stream on cancel/timeout."""
|
|
|
|
def __init__(self, stop_call: Callable[[str | None], bool], timeout: float) -> None:
|
|
self.stop_call = stop_call
|
|
self.timeout = timeout
|
|
self.finished = threading.Event()
|
|
self._trigger = threading.Event()
|
|
self._condition = threading.Condition()
|
|
self._chat_id: str | None = None
|
|
self._response: Any | None = None
|
|
self.cause: str | None = None
|
|
self.stop_succeeded: bool | None = None
|
|
self.thread = threading.Thread(target=self._watch, name="nao-native-stop", daemon=True)
|
|
|
|
def start(self) -> None:
|
|
self.thread.start()
|
|
|
|
def set_response(self, response: Any) -> None:
|
|
with self._condition:
|
|
self._response = response
|
|
|
|
def set_chat_id(self, chat_id: str | None) -> None:
|
|
if not chat_id:
|
|
return
|
|
with self._condition:
|
|
self._chat_id = chat_id
|
|
self._condition.notify_all()
|
|
|
|
def request_cancel(self, cause: str = "user") -> None:
|
|
if cause not in ("user", "timeout", "transport"):
|
|
return
|
|
if not self._trigger.is_set():
|
|
self.cause = cause
|
|
self._trigger.set()
|
|
with self._condition:
|
|
self._condition.notify_all()
|
|
|
|
def _watch(self) -> None:
|
|
deadline = time.monotonic() + self.timeout
|
|
while not self.finished.is_set():
|
|
remaining = deadline - time.monotonic()
|
|
if remaining <= 0:
|
|
self.request_cancel("timeout")
|
|
break
|
|
if self._trigger.wait(min(0.1, remaining)):
|
|
break
|
|
if self.finished.is_set():
|
|
return
|
|
self.cause = self.cause or "timeout"
|
|
deadline = time.monotonic() + CHAT_ID_WAIT_SECONDS
|
|
chat_id = None
|
|
with self._condition:
|
|
while not self.finished.is_set() and not self._chat_id and time.monotonic() < deadline:
|
|
self._condition.wait(timeout=min(0.25, max(0.01, deadline - time.monotonic())))
|
|
chat_id = self._chat_id
|
|
response = self._response
|
|
try:
|
|
self.stop_succeeded = bool(self.stop_call(chat_id))
|
|
except Exception:
|
|
self.stop_succeeded = False
|
|
if response is not None:
|
|
with contextlib.suppress(Exception):
|
|
response.close()
|
|
|
|
def close(self) -> None:
|
|
if self._trigger.is_set():
|
|
if self.thread.is_alive():
|
|
self.thread.join(timeout=7)
|
|
if self.thread.is_alive():
|
|
self.finished.set()
|
|
with contextlib.suppress(Exception):
|
|
if self._response is not None:
|
|
self._response.close()
|
|
self.thread.join(timeout=0.2)
|
|
else:
|
|
self.finished.set()
|
|
with self._condition:
|
|
self._condition.notify_all()
|
|
if self.thread.is_alive():
|
|
self.thread.join(timeout=0.2)
|
|
|
|
|
|
class SessionState:
|
|
"""Per-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() -> tuple[str, str, str]:
|
|
try:
|
|
from nao_model_settings import model_settings
|
|
base, key, model = model_settings()
|
|
except Exception:
|
|
raise RuntimeErrorSafe("所选模型配置不可用。") from None
|
|
if not all(isinstance(item, str) and item for item in (base, key, model)):
|
|
raise RuntimeErrorSafe("所选模型配置不完整。")
|
|
return base, key, model
|
|
|
|
|
|
def _config_fingerprint(model_base: str, model_key: str, model_name: str) -> str:
|
|
paths = [TEMPLATES / "RULES.md", TEMPLATES / "agent" / "prompts" / "system.md",
|
|
ROOT / "scripts" / "nao_quality_runtime.py", ROOT / "scripts" / "nao_quality_runtime_mcp.py",
|
|
ROOT / "scripts" / "nao_quality_tools.py", ROOT / "scripts" / "nao_quality_mcp.py",
|
|
ROOT / "scripts" / "nao_model_settings.py"]
|
|
digest = hashlib.sha256()
|
|
for path in paths:
|
|
digest.update(str(path.relative_to(ROOT)).encode())
|
|
digest.update(path.read_bytes())
|
|
# The one-way fingerprint detects selected model changes without saving its key.
|
|
digest.update(model_base.encode())
|
|
digest.update(model_name.encode())
|
|
digest.update(hashlib.sha256(model_key.encode()).digest())
|
|
try:
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
from nao_quality_tools import tool_definition
|
|
digest.update(json.dumps(tool_definition(), ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode())
|
|
except Exception:
|
|
raise RuntimeErrorSafe("质量工具定义无法读取。") from None
|
|
return digest.hexdigest()
|
|
|
|
|
|
def _write_private_state(actor_root: Path) -> dict[str, str]:
|
|
private_path = actor_root / "private" / "credentials.json"
|
|
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 _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
|
|
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"
|
|
" maxOutputTokens: 8192\n"
|
|
" temperature: 0.2\n"
|
|
" reasoningEffort: low\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 _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) -> 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_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,
|
|
) -> tuple[ConversationAccumulator, str, str | None, bool | None]:
|
|
context_text = ""
|
|
if context not in (None, "", [], {}):
|
|
context_text = "\n会话上下文(仅用于理解本轮追问,不改变数据权限):\n" + json.dumps(
|
|
context, ensure_ascii=False, separators=(",", ":"), allow_nan=False,
|
|
)
|
|
prompt = (f"【仅内部查询约束,禁止向用户复述】本轮数据查询使用 releaseId={release_id},后续所有数据查询必须沿用该标识。"
|
|
"如果当前数据下的证据不足,请如实说明,不要切换数据。用户可见的欢迎语、回答和报告只写业务问题、期间、范围、指标、结果与必要局限;不展示 releaseId、发布版本号、Story ID/version,也不说‘固定版本’。不要展示‘原始明细不发AI’等内部流程提示。"
|
|
+ context_text + "\n\n用户问题:\n" + question)
|
|
body: dict[str, Any] = {
|
|
"message": {"text": prompt},
|
|
"model": {"provider": "openaiCompatible", "modelId": model},
|
|
"timezone": "Asia/Shanghai",
|
|
}
|
|
if chat_id:
|
|
body["chatId"] = chat_id
|
|
request = Request(base_url + "/api/agent/", data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
|
headers={"Content-Type": "application/json", "Origin": base_url,
|
|
"Accept": "text/event-stream"}, method="POST")
|
|
def stop_or_shutdown(value: str | None) -> bool:
|
|
stopped = bool(value and _stop_native(client, base_url, value))
|
|
if stopped:
|
|
return True
|
|
try:
|
|
# A lost SSE transport must not leave a native model task running.
|
|
return bool(_stop_instance(actor_root, grace_seconds=1.0))
|
|
except Exception:
|
|
return False
|
|
|
|
stopper = NativeStopController(stop_or_shutdown, timeout)
|
|
bridge: threading.Thread | None = None
|
|
bridge_done = threading.Event()
|
|
if cancel_event is not None:
|
|
def bridge_cancel() -> None:
|
|
while not bridge_done.wait(0.05):
|
|
if cancel_event.is_set():
|
|
stopper.request_cancel("user")
|
|
return
|
|
bridge = threading.Thread(target=bridge_cancel, name="nao-cancel-bridge", daemon=True)
|
|
bridge.start()
|
|
current_answer = {"value": "", "last_emit": 0.0}
|
|
answer_lock = threading.Lock()
|
|
|
|
def publish_answer(value: str) -> None:
|
|
with answer_lock:
|
|
current_answer["value"] = value
|
|
now = time.monotonic()
|
|
should_emit = now - current_answer["last_emit"] >= 0.35
|
|
if should_emit:
|
|
current_answer["last_emit"] = now
|
|
if should_emit:
|
|
emit({"type": "answer", "answer": value})
|
|
|
|
accumulator = ConversationAccumulator(publish_answer)
|
|
accumulator.chat_id = chat_id
|
|
stopper.set_chat_id(chat_id)
|
|
stopper.start()
|
|
transport_failed = False
|
|
try:
|
|
# urllib's timeout also applies to each blocking SSE read. Keep a generous
|
|
# idle interval; the independent watchdog enforces the 10-minute turn cap.
|
|
response = client.open(request, timeout=SSE_IDLE_TIMEOUT_SECONDS)
|
|
stopper.set_response(response)
|
|
with response:
|
|
for raw in response:
|
|
if stopper.cause:
|
|
break
|
|
line = raw.decode("utf-8", "replace").strip()
|
|
if not line.startswith("data:"):
|
|
continue
|
|
data = line[5:].strip()
|
|
if data == "[DONE]":
|
|
accumulator.done_received = True
|
|
break
|
|
try:
|
|
event = json.loads(data)
|
|
except json.JSONDecodeError:
|
|
continue
|
|
if not isinstance(event, dict):
|
|
continue
|
|
previous_chat = accumulator.chat_id
|
|
accumulator.feed(event)
|
|
if accumulator.chat_id and accumulator.chat_id != previous_chat:
|
|
session_state.set_chat_id(session_id, accumulator.chat_id)
|
|
stopper.set_chat_id(accumulator.chat_id)
|
|
kind = event.get("type")
|
|
if kind in ("tool-input-available", "tool-output-available", "tool-result", "tool-output-error"):
|
|
count = len(accumulator.tool_calls)
|
|
latest = accumulator.tool_calls[-1] if count else {}
|
|
emit({"type": "progress", "message": latest.get("label", "正在分析质量数据…"),
|
|
"toolCount": count, "toolCalls": accumulator.tool_calls})
|
|
# A clean native finish or [DONE] closes the task. A plain EOF can leave
|
|
# the upstream model task alive, so stop it before returning to Java.
|
|
if not stopper.cause and not accumulator.done_received and "stop" not in accumulator.finish_reasons:
|
|
stopper.request_cancel("transport")
|
|
except Exception:
|
|
transport_failed = True
|
|
stopper.request_cancel("transport")
|
|
finally:
|
|
stopper.close()
|
|
if bridge and bridge.is_alive():
|
|
bridge_done.set()
|
|
bridge.join(timeout=0.1)
|
|
cancel_cause = stopper.cause
|
|
status, stop_reason = accumulator.status(cancel_cause=cancel_cause,
|
|
stop_succeeded=stopper.stop_succeeded,
|
|
transport_failed=transport_failed)
|
|
# status and stop reason are attached by caller; keep return transport-neutral.
|
|
if status == "COMPLETED":
|
|
# The Java result mapper uses the native finish reason to recognize a full turn.
|
|
stop_reason = stop_reason or "stop"
|
|
elif status == "CANCELLED":
|
|
stop_reason = "用户停止" if cancel_cause == "user" else "超时停止"
|
|
return accumulator, status, stop_reason, stopper.stop_succeeded
|
|
|
|
|
|
def _normalize_request(raw: Any) -> dict[str, Any]:
|
|
if not isinstance(raw, dict):
|
|
raise ValueError("请求必须是 JSON 对象。")
|
|
turn_id = _uuid(raw.get("turnId"), "turnId")
|
|
session_id = _uuid(raw.get("sessionId"), "sessionId")
|
|
actor_id = _uuid(raw.get("actorId"), "actorId")
|
|
release_id = _uuid(raw.get("releaseId"), "releaseId")
|
|
token = raw.get("accessToken")
|
|
question = raw.get("question")
|
|
if not isinstance(token, str) or not token.strip() or len(token) > 4096 or "\n" in token or "\r" in token:
|
|
raise ValueError("accessToken 无效。")
|
|
if not isinstance(question, str) or not question.strip() or len(question) > MAX_QUESTION_CHARS:
|
|
raise ValueError("question 无效或超过长度限制。")
|
|
context = raw.get("context")
|
|
try:
|
|
context_size = len(json.dumps(context, ensure_ascii=False, separators=(",", ":"))) if context is not None else 0
|
|
except (TypeError, ValueError):
|
|
raise ValueError("context 必须是 JSON 数据。") from None
|
|
if context_size > MAX_CONTEXT_CHARS:
|
|
raise ValueError("context 超过允许大小。")
|
|
return {"turnId": turn_id, "sessionId": session_id, "actorId": actor_id,
|
|
"releaseId": release_id, "accessToken": token, "question": question.strip(), "context": context}
|
|
|
|
|
|
def _emit(stream: Any, lock: threading.Lock, value: dict[str, Any]) -> None:
|
|
with lock:
|
|
stream.write(json.dumps(value, ensure_ascii=False, separators=(",", ":"), allow_nan=False) + "\n")
|
|
stream.flush()
|
|
|
|
|
|
def _input_cancel_listener(stream: Any, on_cancel: Callable[[], None]) -> threading.Thread:
|
|
def listen() -> None:
|
|
for line in stream:
|
|
try:
|
|
value = json.loads(line)
|
|
except json.JSONDecodeError:
|
|
continue
|
|
if isinstance(value, dict) and value.get("type") == "cancel":
|
|
on_cancel()
|
|
return
|
|
thread = threading.Thread(target=listen, name="nao-worker-cancel-input", daemon=True)
|
|
thread.start()
|
|
return thread
|
|
|
|
|
|
def run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any = sys.stdin) -> dict[str, Any]:
|
|
output_lock = threading.Lock()
|
|
emit = lambda value: _emit(stdout, output_lock, value)
|
|
actor_root: Path | None = None
|
|
actor_fd: int | None = None
|
|
access_path: Path | None = None
|
|
try:
|
|
request = _normalize_request(payload)
|
|
emit({"type": "progress", "message": "正在准备独立质量分析环境…"})
|
|
manager = RuntimeManager()
|
|
actor_root, actor_fd = manager.acquire_actor(request["actorId"], 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"]
|
|
model_base, model_key, model_name, base_url = manager.ensure_instance(
|
|
actor_root, request["sessionId"], expected_actor_id=request["actorId"])
|
|
# The model process receives only NAO's configured model credential. The delegated
|
|
# user token remains in access.json, read only by the MCP shim.
|
|
del model_base, model_key
|
|
client = _authenticate(actor_root, base_url)
|
|
state = SessionState(actor_root, request["actorId"], request["sessionId"])
|
|
native_chat_id = state.get_chat_id(request["sessionId"])
|
|
emit({"type": "progress", "message": "正在查询质量中心并整理结论…"})
|
|
|
|
cancel_event = threading.Event()
|
|
def request_cancel() -> None:
|
|
cancel_event.set()
|
|
_input_cancel_listener(stdin, request_cancel)
|
|
|
|
accumulator, status, stop_reason, stop_succeeded = _sse_request(
|
|
client=client, base_url=base_url, model=model_name, question=request["question"],
|
|
release_id=request["releaseId"], context=request["context"], chat_id=native_chat_id,
|
|
session_state=state, session_id=request["sessionId"], emit=emit,
|
|
actor_root=actor_root, cancel_event=cancel_event,
|
|
)
|
|
answer = accumulator.answer
|
|
if answer:
|
|
emit({"type": "answer", "answer": answer})
|
|
if status == "COMPLETED":
|
|
report_markdown, report_html = _story_files(client, base_url, accumulator.chat_id,
|
|
accumulator.tool_calls, accumulator.tool_outputs)
|
|
else:
|
|
report_markdown, report_html = None, None
|
|
if status == "COMPLETED" and not answer:
|
|
status, stop_reason = "FAILED", "empty_answer"
|
|
result = {
|
|
"type": "result", "status": status, "engine": "NAO", "answer": answer,
|
|
"nativeChatId": accumulator.chat_id,
|
|
"toolCalls": accumulator.tool_calls,
|
|
"stopReason": stop_reason,
|
|
"nativeStopSucceeded": stop_succeeded,
|
|
"finishReason": accumulator.finish_reasons[-1] if accumulator.finish_reasons else None,
|
|
}
|
|
if report_markdown is not None:
|
|
result["reportMarkdown"] = report_markdown
|
|
if report_html is not None:
|
|
result["reportHtml"] = report_html
|
|
emit(result)
|
|
return result
|
|
except RuntimeErrorSafe as error:
|
|
result = {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "",
|
|
"toolCalls": [], "stopReason": "runtime_error", "message": str(error)}
|
|
emit(result)
|
|
return result
|
|
except (ValueError, OSError, RuntimeError):
|
|
result = {"type": "result", "status": "FAILED", "engine": "NAO", "answer": "",
|
|
"toolCalls": [], "stopReason": "runtime_error", "message": "本轮分析无法启动,请检查本机运行状态后重试。"}
|
|
emit(result)
|
|
return result
|
|
finally:
|
|
if access_path is not None:
|
|
access_path.unlink(missing_ok=True)
|
|
if actor_fd is not None:
|
|
RuntimeManager().release_actor(actor_fd)
|
|
|
|
|
|
def _read_initial_request(stdin: Any) -> dict[str, Any]:
|
|
line = stdin.readline()
|
|
if not line:
|
|
raise ValueError("缺少首行请求。")
|
|
try:
|
|
value = json.loads(line)
|
|
except json.JSONDecodeError:
|
|
raise ValueError("首行请求不是有效 JSON。") from None
|
|
if not isinstance(value, dict) or value.get("type") == "cancel":
|
|
raise ValueError("首行必须是请求对象。")
|
|
return value
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
os.umask(0o077)
|
|
parser = argparse.ArgumentParser(add_help=True)
|
|
parser.add_argument("--stop-all", action="store_true", help="仅停止已验证且空闲的NAO质量实例")
|
|
args = parser.parse_args(argv)
|
|
if args.stop_all:
|
|
count = RuntimeManager().stop_idle_all()
|
|
sys.stdout.write(json.dumps({"stopped": count}, separators=(",", ":")) + "\n")
|
|
return 0
|
|
try:
|
|
request = _read_initial_request(sys.stdin)
|
|
run_turn(request)
|
|
return 0
|
|
except ValueError as error:
|
|
_emit(sys.stdout, threading.Lock(), {"type": "result", "status": "FAILED", "engine": "NAO",
|
|
"answer": "", "toolCalls": [], "stopReason": "invalid_request",
|
|
"message": str(error)})
|
|
return 2
|
|
except Exception:
|
|
_emit(sys.stdout, threading.Lock(), {"type": "result", "status": "FAILED", "engine": "NAO",
|
|
"answer": "", "toolCalls": [], "stopReason": "runtime_error",
|
|
"message": "本轮分析无法启动,请检查本机运行状态后重试。"})
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|