Files
quality_analyse/scripts/build_dsh_application.py
T
lyun bc19475b29 修复:DSH 自主查询、图片直显与报告稳定性
允许 Agent 在循环中查目录、修正查询和提问,保留实时权限检查及模型原始回答。
统一相同查询证据与图表资源;排名补齐阶段指标,修正 OR 重量筛选,并限制模型明细输入。
增加运行恢复、图表自适应坐标轴与两位小数,修复报告主图选择、重复图及边缘标签。

验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。
线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
2026-11-12 16:14:20 +08:00

298 lines
19 KiB
Python

#!/usr/bin/env python3
"""Build the standalone quality application for the pinned DSH runtime.
The shared metric/planning/checking code is reused without shipping the old
engine, its launcher, admin bootstrap client, or compatibility configuration.
"""
from __future__ import annotations
import argparse
import ast
import hashlib
import json
from pathlib import Path
import re
import shutil
import tempfile
ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = ROOT / "scripts"
ASSETS = ROOT / "runtime/dsh/application"
CONTEXT_FUNCTIONS = (
"RuntimeErrorSafe", "_require_private_regular_file", "_uuid", "_mode_private", "_atomic_json",
"_read_json", "_safe_runtime_root", "_runtime_identity", "_reject_symlink_components", "AccessSnapshot",
"load_access_file", "_check_release_pin", "_redact_credentials", "DelegatedApi", "public_tool_call",
"public_progress_text", "public_evidence_checks", "public_tool_failure", "public_tool_result",
"tool_operation_key", "complete_defect_result", "ConversationAccumulator", "_contains_error_flag",
"_file_lock", "write_access_file", "_drop_repeated_followup_context", "_normalize_request",
"_emit", "_input_cancel_listener", "plan_agent_task", "_record_non_query_turn", "_read_initial_request",
)
MODULES = {name: "quality_" + name[4:] for name in (
"nao_agent_contract", "nao_agent_report", "nao_agent_task", "nao_analysis_coverage", "nao_answer_focus",
"nao_changes", "nao_combined_ranking", "nao_conversation", "nao_data_presentation", "nao_defect_report",
"nao_evidence_finalizer", "nao_evidence_report", "nao_history_report", "nao_metric_request",
"nao_model_gate", "nao_model_settings", "nao_native_answer", "nao_native_contract", "nao_native_exploration",
"nao_native_planning", "nao_native_run", "nao_period", "nao_plan_recovery", "nao_product_defect_pairing",
"nao_product_identification", "nao_protocol_execution", "nao_query_contract", "nao_query_plan",
"nao_runtime_policy", "nao_thinking_stream", "nao_tool_stream", "nao_visual_recovery", "nao_weight_ranking",
)}
MODULES.update(nao_quality_runtime="quality_runtime_context", nao_quality_tools="quality_tools",
nao_quality_mcp="quality_mcp_result")
MODULES.update({name: name for name in ("analysis_visual_evidence", "analysis_visuals", "analysis_query_visuals", "dsh_agent_runtime", "dsh_model_input",
"dsh_row_selection", "dsh_startup", "dsh_mcp_bridge", "setup_dsh_runtime")})
OLD_RUNTIME = SCRIPTS / "nao_quality_runtime.py"
STATE = '''ROOT = Path(__file__).resolve().parents[1]
USERS_ROOT = Path(os.environ.get("QUALITY_ANALYSIS_STATE_DIR", str(ROOT / ".runtime/quality-analysis"))).absolute()
_RUNTIME_QUERY_PORT = os.environ.get("QUALITY_ANALYSIS_RUNTIME_QUERY_PORT", "5281")
if _RUNTIME_QUERY_PORT not in {"5281", "5283", "5284", "5285", "5287"}:
raise RuntimeError("质量分析授权查询端口无效。")
RUNTIME_QUERY_URL = f"http://127.0.0.1:{_RUNTIME_QUERY_PORT}/api/analysis/runtime/query"
MAX_QUESTION_CHARS = 20_000
MAX_CONTEXT_CHARS = 32_000
'''
WORKSPACE = '''
class SessionWorkspace:
"""Private attempt workspace, with no subprocess or chat state of its own."""
def __init__(self):
self.users_root = _safe_runtime_root(USERS_ROOT)
def acquire(self, actor_id, session_id):
target = self.users_root / _uuid(actor_id, "actorId") / "sessions" / _uuid(session_id, "sessionId")
_reject_symlink_components(target, self.users_root)
directory = self.users_root
for part in (None, *target.relative_to(self.users_root).parts):
if part is not None:
directory = directory / part
directory.mkdir(mode=0o700, exist_ok=True)
if directory.is_symlink() or not directory.is_dir() or directory.stat().st_mode & 0o077:
raise RuntimeErrorSafe("私有分析运行目录无效。")
for subdirectory in ("evidence", "private"):
path = target / subdirectory
_reject_symlink_components(path, self.users_root)
path.mkdir(mode=0o700, exist_ok=True)
if not path.is_dir() or path.stat().st_mode & 0o077:
raise RuntimeErrorSafe("私有分析运行目录无效。")
lock = _file_lock(target / "active.lock", blocking=False)
if lock.__enter__() < 0:
lock.__exit__(None, None, None)
raise RuntimeErrorSafe("该任务正处理另一轮分析,请稍后重试。")
return target, lock
@staticmethod
def release(lock):
lock.__exit__(None, None, None)
'''
def definitions(source, names):
parsed = ast.parse(source)
nodes = {}
for node in parsed.body:
if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
nodes[node.name] = node
elif isinstance(node, ast.Assign):
nodes.update({target.id: node for target in node.targets if isinstance(target, ast.Name)})
lines = source.splitlines()
output = []
for name in names:
node = nodes[name]
start = min([node.lineno] + [item.lineno for item in getattr(node, "decorator_list", [])])
output.append("\n".join(lines[start - 1:node.end_lineno]))
return "\n\n\n".join(output) + "\n"
def replace(source, old, new):
if source.count(old) != 1:
raise ValueError("Unexpected source contract at " + old.splitlines()[0])
return source.replace(old, new, 1)
def splice(source, first, last, replacement):
start = source.index(first)
end = source.index(last, start)
return source[:start] + replacement + source[end:]
def without_branch(source, start, stop, *, keep):
begin = source.index(start)
end = source.index(stop, begin)
body = source[begin + len(start):end]
if not keep:
return source[:begin] + source[end:]
return source[:begin] + "\n".join(line[4:] if line.startswith(" ") else line
for line in body.split("\n")) + source[end:]
def neutralize(source):
pattern = r"\b(?:" + "|".join(map(re.escape, sorted(MODULES, key=len, reverse=True))) + r")\b"
source = re.sub(pattern, lambda match: MODULES[match[0]], source)
source = source.replace("nao-quality-", "quality-evidence-").replace("nao-quality", "quality-analysis")
source = source.replace("'engine': 'NAO'", "'engine': 'DSH'").replace('"engine": "NAO"', '"engine": "DSH"')
source = source.replace("NAO", "DSH").replace("nao-", "quality-").replace("nao_", "quality_")
return source
def transformed(name, source):
if name == "nao_quality_runtime":
imports = '''"""Authorized query transport, turn workspace and public timeline projection."""
from __future__ import annotations
import contextlib
import fcntl
import hashlib
import json
import math
import os
import re
import secrets
import stat
import threading
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, urlsplit
from urllib.request import ProxyHandler, Request, build_opener
'''
source = imports + "\n" + STATE + "\n" + definitions(source, ("_UUID_RE", "_ALLOWED_PATHS", *CONTEXT_FUNCTIONS)) + WORKSPACE
source = replace(source, 'if root != USERS_ROOT.resolve() and not root.name.startswith("test-nao-users-"):', 'if root != USERS_ROOT.resolve():')
source = replace(source, "self.opener = opener or build_opener()", "self.opener = opener or build_opener(ProxyHandler({}))")
return replace(source, "Validate the delegated file only; never logs into the NAO admin API.", "Validate the delegated actor and attempt without exchanging credentials.")
if name == "nao_quality_mcp":
return "import json\nfrom typing import Any\n\n" + definitions(source, ("_tool_result",))
if name == "nao_quality_tools":
source = replace(source, "from dbgpt_trial_data import encoded_selection # noqa: E402\nfrom local_api import Api # noqa: E402",
"import base64\n\n" + definitions((SCRIPTS / "dbgpt_trial_data.py").read_text(), ("encoded_selection",)))
source = replace(source, "self._api_factory = api_factory or Api", "if api_factory is None:\n raise ValueError('缺少已授权质量查询接口。')\n self._api_factory = api_factory")
return source
if name == "nao_model_settings":
source = splice(source, "def _legacy_settings():", "def model_configuration():", "")
source = splice(source, " if not SETTINGS_FILE.exists():", " enabled=", " value = json.loads(_read_private(SETTINGS_FILE, 32768).decode('utf-8'))\n base,key,model = _validate(value)\n")
return replace(source, "QUALITY_NAO_MODEL_CONFIG_FILE', str(ROOT / '.runtime/nao-model-config.json')",
"QUALITY_ANALYSIS_MODEL_CONFIG_FILE', str(ROOT / '.runtime/quality-model-config.json')")
if name == "nao_agent_contract":
source = splice(source, "QUALITY_NATIVE_TOOLS =", "QUALITY_DSH_TOOLS =", "")
source = replace(source, "def apply_platform_tools(payload, *, native_agent=False, runtime_engine='NAO'):", "def apply_platform_tools(payload):")
return splice(source, " if runtime_engine not in", " tools = tools if", " allowed = QUALITY_DSH_TOOLS\n")
if name == "nao_model_gate":
source = splice(source, "native_turn_marker: str | None = None, runtime_engine:", " parsed = urlsplit", "native_turn_marker: str | None = None) -> None:\n")
source = replace(source, " self.runtime_engine = runtime_engine\n", "")
source = replace(source, " self.base_url = f'http://127.0.0.1:{port}/v1'\n", "")
source = replace(source, "self._server = Server(('127.0.0.1', port), Handler)",
"self._server = Server(('127.0.0.1', port), Handler)\n self.base_url = f'http://127.0.0.1:{self._server.server_port}/v1'")
source = re.sub(r"apply_platform_tools\(decoded, native_agent=gate\.native_agent,\s+runtime_engine=gate\.runtime_engine\)", "apply_platform_tools(decoded)", source)
return replace(source, "if is_sse and gate.runtime_engine == 'DSH' else None", "if is_sse else None")
if name == "nao_native_run":
source = replace(source, "manager, state, cancel_event", "cancel_event")
source = replace(source, "_atomic_json, _authenticate, _sse_request, _read_json", "_atomic_json, _read_json")
source = replace(source, " from nao_runtime_policy import native_timeout\n", "")
source = splice(source, " started = time.monotonic()\n engine =", " emit({'type': 'progress'", " engine = 'DSH'\n model = connection[2]\n")
source = without_branch(source, " if engine == 'DSH':\n", " else:\n accumulator", keep=True)
source = splice(source, " else:\n accumulator", " timings['investigation']", "")
source = splice(source, " draft_path =", " if cancel_event.is_set() or native_status == 'CANCELLED':", "")
source = replace(source, "if engine == 'DSH' and not documents:", "if not documents:")
source = replace(source, "if engine == 'DSH' and documents and answer.strip():", "if documents and answer.strip():")
source = replace(source, "and (engine != 'DSH' or narrative_binding == 'verified_composition')", "and narrative_binding == 'verified_composition'")
source = replace(source, " checked_context = {**context, '_autonomousAnswer': True,", " from quality_report_deliverable import requested\n checked_context = {**context, '_autonomousAnswer': True,\n '_requestedQualityDeliverable': requested(request['question'], task),")
return source
if name == "nao_native_answer":
source = replace(source, " return task.get('kind') == 'report' or (context.get('_reportTitleRequired')", " return (context.get('_requestedQualityDeliverable') is True and bool(facts)) or task.get('kind') == 'report' or (context.get('_reportTitleRequired')")
source = replace(source, " raw = response.read(_MAX_BODY + 1)\n",
" raw = response.read(_MAX_BODY + 1)\n"
" from quality_original_answer import capture_envelope\n"
" capture_envelope(raw, phase=phase)\n")
# Capture a complete response even if cancellation arrives during the HTTP read.
source = replace(source, " if cancel_event.is_set():\n raise FinalizationUnavailable('已取消')\n if len(raw) > _MAX_BODY:",
" if len(raw) > _MAX_BODY:")
return source
if name == "nao_evidence_finalizer":
return replace(source, " raw = response.read(2 * 1024 * 1024 + 1)\n",
" raw = response.read(2 * 1024 * 1024 + 1)\n"
" from quality_original_answer import capture_envelope\n"
" capture_envelope(raw)\n")
if name == "nao_protocol_execution":
source = replace(source, " return {'_autonomousAnswer': True} if request.get('_autonomousAnswer') is True else {}",
" from quality_report_deliverable import requested\n\n"
" context = {'_autonomousAnswer': True} if request.get('_autonomousAnswer') is True else {}\n"
" if requested(request.get('question')):\n"
" context['_requestedQualityDeliverable'] = True\n"
" return context")
marker = " 'toolCalls': calls, 'toolCount': len(calls), 'reportTitle': final.get('reportTitle'),\n"
if source.count(marker) != 2:
raise ValueError("Unexpected bounded result shape")
return source.replace(marker, marker +
" '_verifiedReportNarrative': final.get('finishReason') == 'stop'\n"
" and final.get('evidenceSufficient') is True and bool(final.get('selectedFactIds')),\n")
if name == "dsh_agent_runtime":
source = replace(source, "ROOT = Path(__file__).resolve().parents[1]", "ROOT = Path(__file__).resolve().parents[1]\nfrom quality_draft import DraftPublisher")
source = replace(source, "root = Path(users_root or ROOT / '.runtime/nao-users')", "from quality_runtime_context import USERS_ROOT\n root = Path(users_root or USERS_ROOT)")
source = replace(source, "def _prompt(question, context, task, identity, turn_id):\n", "def _prompt(question, context, task, identity, turn_id):\n from quality_report_deliverable import requested\n\n")
source = replace(source, " if task.get('responseStyle') == 'brief':\n",
" if requested(question, task):\n"
" output_contract += ('本轮交付报告。用能概括实际范围的标题,开篇给出核心发现。'\n"
" '自行选择与问题匹配的段落、标题、对比表和图表;仅在证据支持且能帮助理解时使用。'\n"
" '将证据、时间范围和实际限制放到相关发现附近。没有支持内容的章节直接省略。')\n"
" if task.get('responseStyle') == 'brief':\n")
source = replace(source, "self.accumulator, self.emit = accumulator, emit", "self.accumulator, self.emit = accumulator, emit\n self.draft = DraftPublisher(emit)\n from quality_original_answer import current\n self.original = current()")
source = source.replace(" from nao_quality_runtime import public_progress_text\n self.emit({'type': 'answer', 'answer': public_progress_text(acc.answer)})",
" if self.original is not None:\n self.original.capture_native(text)\n self.draft.update(attempt, text)", 1)
source = replace(source, " from nao_quality_runtime import public_progress_text\n self.emit({'type': 'answer', 'answer': public_progress_text(acc.answer)})", " self.draft.clear()")
source = replace(source, " if frame['type'] == 'start':\n", " if frame['type'] == 'start':\n self.draft.clear()\n")
source = replace(source, " if frame['type'] == 'end':\n", " if frame['type'] == 'end':\n self.draft.clear()\n")
source = replace(source, " elif kind == 'tool/call':\n", " elif kind == 'tool/call':\n self.draft.clear()\n")
source = replace(source, " elif kind == 'turn/end':\n", " elif kind == 'turn/end':\n self.draft.clear()\n")
source = replace(source, " relay.close()\n if worker is not None:\n",
" relay.close()\n projection.draft.clear()\n if worker is not None:\n")
return source
if name == "setup_dsh_runtime":
return replace(source, "RUNTIME = ROOT / 'runtime/dsh'", "RUNTIME = (ROOT / 'runtime/dsh').resolve()")
return source
def build(destination):
destination = destination.absolute()
if destination.exists():
raise ValueError("Target application already exists")
destination.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix=".dsh-application-", dir=destination.parent) as temporary:
target = Path(temporary) / "runtime-app"
scripts = target / "scripts"
scripts.mkdir(parents=True)
(target / ".runtime").mkdir(mode=0o700)
inventory = {}
for original, name in MODULES.items():
path = SCRIPTS / (original + ".py")
source = neutralize(transformed(original, path.read_text(encoding="utf-8")))
compile(source, name + ".py", "exec")
(scripts / (name + ".py")).write_text(source, encoding="utf-8")
inventory[name + ".py"] = hashlib.sha256(source.encode()).hexdigest()
for path in ASSETS.glob("*.py"):
compile(path.read_bytes(), path.name, "exec")
shutil.copyfile(path, scripts / path.name)
inventory[path.name] = hashlib.sha256(path.read_bytes()).hexdigest()
policy = target / "backend/src/main/resources/analysis-runtime-policy.json"
policy.parent.mkdir(parents=True)
shutil.copyfile(ROOT / "backend/src/main/resources/analysis-runtime-policy.json", policy)
rules = target / "ai/quality-analysis/RULES.md"
rules.parent.mkdir(parents=True)
rules.write_text(neutralize((ROOT / "ai/nao-quality/RULES.md").read_text()), encoding="utf-8")
(target / "runtime").mkdir()
# Only pinned DSH distribution assets are shared with the host installation.
(target / "runtime/dsh").symlink_to(ROOT / "runtime/dsh", target_is_directory=True)
(target / "application.json").write_text(json.dumps({"engine": "DSH", "scripts": inventory}, indent=2) + "\n")
target.rename(destination)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("destination", type=Path)
args = parser.parse_args()
build(args.destination)
print("Built DSH quality application at", args.destination)
if __name__ == "__main__":
main()