完善:质量分析平台查询与分析功能

This commit is contained in:
2026-10-22 21:45:29 +08:00
parent 1e8a655adc
commit 5fbeb52b93
54 changed files with 2331 additions and 564 deletions
+582 -68
View File
@@ -20,6 +20,7 @@ import secrets
import shutil
import signal
import socket
import sqlite3
import stat
import subprocess
import sys
@@ -66,6 +67,22 @@ 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。")
@@ -92,6 +109,23 @@ def _atomic_json(path: Path, value: Any, *, mode: int = 0o600) -> None:
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)
@@ -107,6 +141,32 @@ def _safe_runtime_root(runtime_root: Path) -> Path:
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
@@ -120,33 +180,62 @@ def load_access_file(
*,
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."""
"""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))
candidate = Path(access_path)
if candidate.is_symlink():
raise ValueError("访问凭据文件路径无效。")
absolute = candidate.absolute()
actor_dir = absolute.parent
if actor_dir.is_symlink() or actor_dir.parent.resolve() != root:
raise ValueError("访问凭据文件路径越界。")
actor_id = _uuid(actor_dir.name, "actorId")
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 absolute.parent.resolve() != actor_dir.resolve():
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:
info = absolute.stat(follow_symlinks=False)
except OSError:
raise ValueError("访问凭据文件不可用。") from None
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
raise ValueError("访问凭据文件权限无效。")
try:
raw = _read_json(absolute)
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")
@@ -161,6 +250,19 @@ def load_access_file(
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", [])
@@ -205,12 +307,14 @@ class DelegatedApi:
*,
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
@@ -219,6 +323,7 @@ class DelegatedApi:
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
@@ -233,6 +338,7 @@ class DelegatedApi:
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):
@@ -270,6 +376,45 @@ class DelegatedApi:
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."""
@@ -278,9 +423,11 @@ class ConversationAccumulator:
self.block_order: list[str] = []
self.active_text_id = "text"
self.finish_reasons: list[str] = []
self.tool_calls: list[dict[str, 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
@@ -318,16 +465,21 @@ class ConversationAccumulator:
tool_name = event.get("toolName")
if isinstance(tool_name, str):
call_id = event.get("toolCallId")
call = {"tool": tool_name, "status": "started"}
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
if _contains_error_flag(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")
@@ -336,17 +488,36 @@ class ConversationAccumulator:
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]":
@@ -374,7 +545,7 @@ class ConversationAccumulator:
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:
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"
@@ -482,16 +653,35 @@ class NativeStopController:
class SessionState:
"""Per-actor mapping from Java session UUIDs to native chat IDs."""
"""Per-session mapping to native chat IDs, with read-only legacy fallback."""
def __init__(self, actor_root: Path, actor_id: str) -> None:
self.path = actor_root / "private" / "sessions.json"
self.actor_id = actor_id
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
@@ -500,6 +690,16 @@ class SessionState:
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)
@@ -547,8 +747,12 @@ def _config_fingerprint(model_base: str, model_key: str, model_name: str) -> str
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")):
@@ -565,13 +769,29 @@ def _write_private_state(actor_root: Path) -> dict[str, str]:
def _write_project(actor_root: Path, session_id: str, model_name: str, *,
refresh_templates: bool, refresh_mcp: bool, backup_schema: bool) -> None:
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"
if project.is_symlink():
raise RuntimeErrorSafe("NAO 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():
@@ -581,10 +801,9 @@ def _write_project(actor_root: Path, session_id: str, model_name: str, *,
for relative in (Path("RULES.md"), Path("agent/prompts/system.md")):
source = TEMPLATES / relative
target = project / relative
if target.is_symlink():
raise RuntimeErrorSafe("NAO project 模板路径无效。")
shutil.copyfile(source, target)
target.chmod(0o600)
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"
@@ -603,9 +822,7 @@ def _write_project(actor_root: Path, session_id: str, model_name: str, *,
" reasoningEffort: low\n"
)
config_path = project / "nao_config.yaml"
config_path.write_text(config, encoding="utf-8")
config_path.chmod(0o600)
evidence_dir = actor_root / session_id / "evidence"
_atomic_bytes(config_path, config.encode("utf-8"))
evidence_dir.mkdir(parents=True, exist_ok=True, mode=0o700)
mcp = {
"mcpServers": {
@@ -616,8 +833,8 @@ def _write_project(actor_root: Path, session_id: str, model_name: str, *,
"env": {
"QUALITY_AGENT_ACCESS_FILE": str(actor_root / "access.json"),
"QUALITY_AGENT_EVIDENCE_DIR": str(evidence_dir),
"QUALITY_AGENT_SESSION_STATE_FILE": str(actor_root / "private" / "current-session.json"),
"QUALITY_AGENT_ACTOR_ID": _uuid(actor_root.name, "actorId"),
"QUALITY_AGENT_SESSION_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"),
@@ -628,8 +845,7 @@ def _write_project(actor_root: Path, session_id: str, model_name: str, *,
mcp_path = project / "agent" / "mcps" / "mcp.json"
if refresh_mcp or not mcp_path.exists():
_atomic_json(mcp_path, mcp)
_atomic_json(actor_root / "private" / "current-session.json",
{"actorId": actor_root.name, "sessionId": session_id})
_atomic_json(session_state_path, {"actorId": actor_id, "sessionId": session_id})
os.chmod(project, 0o700)
@@ -657,6 +873,31 @@ def _process_record(pid: int, kind: str, port: int) -> dict[str, Any]:
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"])
@@ -680,10 +921,15 @@ def _process_matches(record: dict[str, Any], actor_root: Path) -> bool:
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:
@@ -715,7 +961,10 @@ def _stop_instance(actor_root: Path, *, force: bool = False, grace_seconds: floa
@contextlib.contextmanager
def _file_lock(path: Path, *, blocking: bool) -> Iterator[int]:
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
fd = os.open(path, os.O_CREAT | os.O_RDWR, 0o600)
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))
@@ -737,6 +986,84 @@ def _actor_dirs(users_root: Path = USERS_ROOT) -> list[Path]:
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()
@@ -755,15 +1082,36 @@ class RuntimeManager:
os.chmod(path, 0o700)
return path
def acquire_actor(self, actor_id: str) -> tuple[Path, int]:
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"
fd = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600)
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
raise RuntimeErrorSafe("该会话正处理另一轮分析,请稍后重试。") from None
return actor_root, fd
def release_actor(self, fd: int) -> None:
@@ -772,16 +1120,34 @@ class RuntimeManager:
os.close(fd)
@staticmethod
def _current_session(actor_root: Path) -> str | None:
def _current_session(actor_root: Path, expected_actor_id: str | None = None,
expected_session_id: str | None = None) -> str | None:
try:
value = _read_json(actor_root / "private" / "current-session.json")
if isinstance(value, dict) and value.get("actorId") == actor_root.name:
return _uuid(value.get("sessionId"), "sessionId")
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) -> tuple[str, str, str, str]:
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)
@@ -791,7 +1157,7 @@ class RuntimeManager:
state = _read_json(state_path)
except (OSError, json.JSONDecodeError):
pass
current_session = self._current_session(actor_root)
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"))
@@ -800,7 +1166,9 @@ class RuntimeManager:
backup_schema = isinstance(state, dict) and state.get("fingerprint") != fingerprint
if isinstance(state, dict):
procs = state.get("processes", [])
alive = all(isinstance(r, dict) and _process_matches(r, actor_root) for r in procs) and len(procs) == 2
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'])}"
@@ -808,23 +1176,123 @@ class RuntimeManager:
_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)
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) -> None:
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 = []
actor_dirs = _actor_dirs(self.users_root)
for other in actor_dirs:
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 []
if len(procs) == 2 and all(isinstance(r, dict) and _process_matches(r, other) for r in procs):
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
@@ -840,13 +1308,33 @@ class RuntimeManager:
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)
refresh_mcp=refresh_mcp, backup_schema=backup_schema,
expected_actor_id=actor_id)
self._start(actor_root, model_base, model_key, model_name, fingerprint)
def _start(self, actor_root: Path, model_base: str, model_key: str, model_name: str, fingerprint: str) -> None:
@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()
@@ -875,11 +1363,13 @@ class RuntimeManager:
})
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 = os.open(log_path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
fd = _open_server_log(log_path, actor_root)
try:
with os.fdopen(fd, "ab") as log:
fastapi = subprocess.Popen(
@@ -900,7 +1390,8 @@ class RuntimeManager:
os.killpg(record["pgid"], signal.SIGTERM)
raise RuntimeErrorSafe("NAO 本地运行实例启动失败。") from None
state = {
"actorId": actor_root.name, "serverPort": server_port, "fastapiPort": fastapi_port,
"actorId": actor_id, "sessionId": session_id,
"serverPort": server_port, "fastapiPort": fastapi_port,
"fingerprint": fingerprint, "processes": processes,
"startedAt": datetime.now(timezone.utc).isoformat(),
}
@@ -924,7 +1415,7 @@ class RuntimeManager:
stopped = 0
self.users_root.mkdir(parents=True, exist_ok=True, mode=0o700)
with _file_lock(self.users_root / "registry.lock", blocking=True):
for actor_root in _actor_dirs(self.users_root):
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
@@ -934,15 +1425,31 @@ class RuntimeManager:
return stopped
def write_access_file(actor_root: Path, token: str, release_id: str) -> Path:
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"
_atomic_json(access_path, {"token": token, "releaseId": _uuid(release_id, "releaseId"), "queryUrl": RUNTIME_QUERY_URL})
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:
@@ -960,6 +1467,8 @@ 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)
@@ -1159,9 +1668,11 @@ def _sse_request(
session_state.set_chat_id(session_id, accumulator.chat_id)
stopper.set_chat_id(accumulator.chat_id)
kind = event.get("type")
if kind == "tool-input-available":
if kind in ("tool-input-available", "tool-output-available", "tool-result", "tool-output-error"):
count = len(accumulator.tool_calls)
emit({"type": "progress", "message": "正在调用质量分析工具…", "toolCount": count})
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:
@@ -1242,15 +1753,18 @@ def run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any =
request = _normalize_request(payload)
emit({"type": "progress", "message": "正在准备独立质量分析环境…"})
manager = RuntimeManager()
actor_root, actor_fd = manager.acquire_actor(request["actorId"])
access_path = write_access_file(actor_root, request["accessToken"], request["releaseId"])
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"])
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"])
state = SessionState(actor_root, request["actorId"], request["sessionId"])
native_chat_id = state.get_chat_id(request["sessionId"])
emit({"type": "progress", "message": "正在查询质量中心并整理结论…"})