接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
301 lines
14 KiB
Python
301 lines
14 KiB
Python
#!/usr/bin/env python3
|
|
"""Run a DSH quality-analysis turn over the authenticated platform worker protocol."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import copy
|
|
import json
|
|
import os
|
|
import secrets
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
from quality_runtime_context import (
|
|
DelegatedApi, RuntimeErrorSafe, SessionWorkspace, _atomic_json, _emit,
|
|
_input_cancel_listener, _normalize_request, _read_initial_request,
|
|
_drop_repeated_followup_context, plan_agent_task, write_access_file,
|
|
)
|
|
from quality_runtime_policy import turn_scope
|
|
from quality_report_deliverable import attach
|
|
from quality_original_answer import OriginalAnswer, current as original_answer
|
|
from quality_thinking_stream import ThinkingPublisher
|
|
from quality_tools import SourceCoverageUnavailable
|
|
|
|
|
|
def _validate_request(payload):
|
|
if not isinstance(payload, dict):
|
|
raise RuntimeErrorSafe("分析请求格式无效。")
|
|
context = payload.get("context")
|
|
if not isinstance(context, dict):
|
|
raise RuntimeErrorSafe("缺少正式分析协议。")
|
|
for selected in (payload.get("runtimeEngine"), context.get("_runtimeEngine")):
|
|
if selected is not None and selected != "DSH":
|
|
raise RuntimeErrorSafe("此运行环境只支持 DeepSeek Harness。")
|
|
request = _normalize_request(payload)
|
|
protocol = request["context"].get("_protocol")
|
|
if not isinstance(protocol, dict) or protocol.get("attemptId") != request["sessionId"]:
|
|
raise RuntimeErrorSafe("分析任务授权与运行身份不匹配。")
|
|
request["runtimeEngine"] = "DSH"
|
|
return request, protocol
|
|
|
|
|
|
def _plan(request, protocol, delegated, connection, workspace, cancel, emit):
|
|
from quality_agent_task import bind_task
|
|
from quality_product_identification import (
|
|
authorized_extra_fields, identify_products, resolve_clarification_choice,
|
|
)
|
|
|
|
context = request["context"]
|
|
options = delegated.call("/api/options?releaseId=" + request["releaseId"], method="GET")
|
|
catalog = {key: options.get(key, []) for key in (
|
|
"years", "periods", "latestDataPeriod", "departments", "groups", "groupDepartments",
|
|
)}
|
|
catalog.update(identify_products(request["question"], options,
|
|
respect_exclusions=protocol.get("autonomousEnabled") is True))
|
|
try:
|
|
authorized = delegated.call("/api/analysis/tools/catalog", method="GET")
|
|
except (RuntimeErrorSafe, ValueError, OSError):
|
|
authorized = None
|
|
catalog.update(authorized_extra_fields(authorized))
|
|
if isinstance(authorized, dict):
|
|
for key, expected_type in (("sourceFields", list), ("fineDefects", dict)):
|
|
if isinstance(authorized.get(key), expected_type):
|
|
catalog[key] = authorized[key]
|
|
context["_businessCatalog"] = catalog
|
|
request['_analysisDirectory'] = {key: copy.deepcopy(options[key]) for key in
|
|
('departments','groups','groupDepartments','periods','latestDataPeriod') if key in options}
|
|
|
|
previous = protocol.get("previousTask") or None
|
|
resolution = resolve_clarification_choice(request["question"], options, previous) if previous else None
|
|
if resolution is not None:
|
|
catalog.update(resolution)
|
|
planning_context = bind_task(context, previous) if previous is not None else context
|
|
cleaned, repeated = _drop_repeated_followup_context(request["question"], context)
|
|
if repeated:
|
|
request["context"] = cleaned
|
|
request["restartConversation"] = True
|
|
planning_context = cleaned
|
|
if resolution is None:
|
|
previous = None
|
|
if isinstance(catalog.get("groupDepartments"), dict):
|
|
request["_scopeDirectory"] = {"groupDepartments": copy.deepcopy(catalog["groupDepartments"])}
|
|
|
|
emit({"type": "progress", "message": "正在理解本轮目标和连续追问…"})
|
|
retry = protocol.get("retryTask")
|
|
if isinstance(retry, dict) and retry:
|
|
return retry
|
|
audit = []
|
|
|
|
def resolve_source_values(*, release_id, period, filters, field, stages, search, limit, offset):
|
|
query = {"releaseId": release_id, "period": period, "filters": filters,
|
|
"field": field, "stages": stages, "search": search, "limit": limit, "offset": offset}
|
|
started = time.monotonic()
|
|
try:
|
|
response = delegated.call("/api/analysis/source/values", payload=query, method="POST")
|
|
except Exception as error:
|
|
audit.append({"field": field, "stages": stages, "errorType": type(error).__name__})
|
|
_atomic_json(workspace / "private/source-lookup-audit.json", audit[-100:])
|
|
raise
|
|
audit.append({"field": field, "stages": stages, "elapsedMs": round((time.monotonic() - started) * 1000),
|
|
"candidateCount": response.get("totalCount"), "exactFound": search in response.get("values", []),
|
|
"echoMatches": all(response.get(key) == query[key]
|
|
for key in ("releaseId", "period", "filters", "field", "offset", "limit"))
|
|
and response.get("selectedStages") == stages,
|
|
"candidateOnly": response.get("candidateOnly")})
|
|
_atomic_json(workspace / "private/source-lookup-audit.json", audit[-100:])
|
|
return response
|
|
|
|
return plan_agent_task(
|
|
base_url=connection[0], api_key=connection[1], model=connection[2],
|
|
question=request["question"], context=planning_context, previous=previous,
|
|
cancel_event=cancel, diagnostic_path=workspace / "private/task-plan-failure.json",
|
|
diagnostic_turn_id=request["turnId"], recovery_progress=emit,
|
|
semantic_review=request["restartConversation"] or bool(planning_context.get("_serverQuestions")),
|
|
fresh_retry=request["restartConversation"], source_value_resolver=resolve_source_values,
|
|
checks_enabled=False,
|
|
on_proposal=lambda value: _atomic_json(workspace / 'private/model-task-proposal.json', value),
|
|
)
|
|
|
|
|
|
def run_turn(payload, *, stdout=sys.stdout, stdin=sys.stdin):
|
|
"""Run the authorized DSH loop and publish its original answer directly."""
|
|
output_lock = threading.Lock()
|
|
with turn_scope(), OriginalAnswer(lambda event: _emit(stdout, output_lock, event)):
|
|
return _run_turn(payload, stdout=stdout, stdin=stdin, output_lock=output_lock)
|
|
|
|
|
|
def _run_turn(payload, *, stdout, stdin, output_lock):
|
|
from quality_model_gate import ModelGate
|
|
from quality_model_settings import model_configuration
|
|
from setup_dsh_runtime import inspect_runtime
|
|
|
|
lock = output_lock
|
|
thinking = ThinkingPublisher(lambda block: _emit(stdout, lock, {"type": "progress", "thinkingUpdate": block}))
|
|
cancelled = threading.Event()
|
|
gate = None
|
|
workspace = None
|
|
access = None
|
|
request = None
|
|
session_lock = None
|
|
store = None
|
|
|
|
def emit(event):
|
|
# Native drafts travel separately from durable process and answer data.
|
|
if event.get("type") == "result":
|
|
thinking.close()
|
|
event["engine"] = "DSH"
|
|
original_answer().settle(event)
|
|
event["answerValidation"] = {"mode": "disabled", "originalPreserved": True}
|
|
if request is not None:
|
|
attach(event, request["question"])
|
|
event = dict(event)
|
|
if isinstance(event.get("timeline"), list):
|
|
event["timeline"] = [entry for entry in event["timeline"]
|
|
if isinstance(entry, dict) and entry.get("kind") in ("tool_start", "tool_result")]
|
|
_emit(stdout, lock, event)
|
|
|
|
def cancel():
|
|
cancelled.set()
|
|
if gate is not None:
|
|
gate.revoke()
|
|
|
|
def failed(reason, message):
|
|
if cancelled.is_set():
|
|
return {"type": "result", "engine": "DSH", "status": "CANCELLED", "answer": "",
|
|
"toolCalls": [], "stopReason": "cancelled", "message": "已停止分析。"}
|
|
return {"type": "result", "engine": "DSH", "status": "FAILED", "answer": "",
|
|
"toolCalls": [], "stopReason": reason, "message": message}
|
|
|
|
try:
|
|
request, protocol = _validate_request(payload)
|
|
_input_cancel_listener(stdin, cancel)
|
|
if cancelled.is_set():
|
|
result = failed("cancelled", "已停止分析。")
|
|
emit(result)
|
|
return result
|
|
if not inspect_runtime().get("ready"):
|
|
raise RuntimeErrorSafe("DeepSeek Harness 运行环境未就绪。")
|
|
if protocol.get("autonomousEnabled") is not True:
|
|
for key in ("_serverHistory", "_serverQuestions", "lastVisibleAnswer", "_task", "primaryEvidence"):
|
|
request["context"].pop(key, None)
|
|
emit({"type": "progress", "message": "正在准备独立质量分析环境…"})
|
|
store = SessionWorkspace()
|
|
workspace, session_lock = store.acquire(request["actorId"], request["sessionId"])
|
|
access = write_access_file(workspace, request.pop("accessToken"), request["releaseId"],
|
|
expected_actor_id=request["actorId"], expected_session_id=request["sessionId"])
|
|
delegated = DelegatedApi(access, runtime_root=store.users_root, expected_actor_id=request["actorId"],
|
|
expected_session_id=request["sessionId"]).login()
|
|
|
|
def authorize():
|
|
if cancelled.is_set():
|
|
raise RuntimeErrorSafe("已停止分析。")
|
|
delegated.call("/api/analysis/model-authorization", method="GET")
|
|
|
|
authorize()
|
|
try:
|
|
base, key, model, controls = model_configuration()
|
|
except (ValueError, OSError):
|
|
raise RuntimeErrorSafe("所选模型配置不可用。") from None
|
|
gate_key = secrets.token_urlsafe(32)
|
|
gate = ModelGate(upstream_base=base, upstream_key=key, model=model, port=0, client_key=gate_key,
|
|
authorize=authorize, thinking_options=controls, on_thinking=thinking.receive,
|
|
native_turn_marker="quality-turn:" + request["turnId"])
|
|
del key
|
|
connection = (gate.base_url, gate_key, model)
|
|
emit({"type": "progress", "message": "正在查询质量数据分析平台并整理结论…"})
|
|
planning_started = time.monotonic()
|
|
task = _plan(request, protocol, delegated, connection, workspace, cancelled, emit)
|
|
request["context"].pop("_businessCatalog", None)
|
|
if not isinstance(task, dict):
|
|
raise RuntimeErrorSafe("未形成可提交的任务提案。")
|
|
authorize()
|
|
planning_ms = round((time.monotonic() - planning_started) * 1000)
|
|
confirmation_started = time.monotonic()
|
|
_atomic_json(workspace / "private/proposed-agent-task.json", {"turnId": request["turnId"], "task": task})
|
|
authority = delegated.call("/api/analysis/protocol/task", payload=task, method="POST")
|
|
if not isinstance(authority.get("task"), dict) or authority.get("attemptId") != request["sessionId"]:
|
|
raise RuntimeErrorSafe("后台未确认当前任务。")
|
|
request["_autonomousAnswer"] = protocol.get("autonomousEnabled") is True
|
|
options = dict(request=request, task=authority["task"], authority=authority, actor_root=workspace,
|
|
delegated=delegated, connection=connection, cancel_event=cancelled, emit=emit,
|
|
phase_timings={"planningAndReview": planning_ms,
|
|
"taskConfirmation": round((time.monotonic() - confirmation_started) * 1000)})
|
|
if ((authority["task"].get("executionMode") == "native_agent" and not authority["task"].get("needsClarification"))
|
|
or (authority["task"].get("kind") == "report" and not authority["task"].get("needsClarification"))):
|
|
from quality_native_run import run_native_exploration
|
|
|
|
gate.native_agent = True
|
|
prior = frozenset(path.name for path in (workspace / "evidence").glob("quality-evidence-*.json"))
|
|
return run_native_exploration(**options, prior_evidence_files=prior)
|
|
from quality_protocol_execution import execute
|
|
|
|
return execute(**options)
|
|
except SourceCoverageUnavailable as error:
|
|
if workspace is not None and request is not None:
|
|
_atomic_json(workspace / "private/source-coverage-gap.json", {
|
|
"turnId": request["turnId"], "period": error.period, "message": str(error),
|
|
})
|
|
result = failed("missing_source", str(error))
|
|
except RuntimeErrorSafe as error:
|
|
result = failed("runtime_error", str(error))
|
|
except Exception as error:
|
|
if workspace is not None and request is not None:
|
|
try:
|
|
_atomic_json(workspace / "private/last-runtime-error.json", {
|
|
"turnId": request["turnId"], "errorType": type(error).__name__,
|
|
})
|
|
except (OSError, ValueError):
|
|
pass
|
|
result = failed("runtime_error", "分析执行未完成,已保留当前问题和条件,请重试本轮。")
|
|
finally:
|
|
thinking.close()
|
|
if gate is not None:
|
|
try:
|
|
_atomic_json(workspace / "private/last-model-audit.json", {
|
|
"model": model, "requests": gate.request_audit,
|
|
"rejectedToolCalls": gate.rejected_tool_calls,
|
|
"rejectedToolNames": gate.rejected_tool_names,
|
|
"toolStreamFailures": gate.tool_stream_failures,
|
|
"toolStreamDiagnostics": gate.tool_stream_diagnostics,
|
|
"rejectedStaleRequests": gate.rejected_stale_requests,
|
|
})
|
|
except (OSError, TypeError, ValueError):
|
|
pass
|
|
finally:
|
|
gate.close()
|
|
try:
|
|
if access is not None:
|
|
access.unlink(missing_ok=True)
|
|
finally:
|
|
if session_lock is not None:
|
|
store.release(session_lock)
|
|
emit(result)
|
|
return result
|
|
|
|
|
|
def main(argv=None):
|
|
os.umask(0o077)
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--stop-all", action="store_true")
|
|
args = parser.parse_args(argv)
|
|
if args.stop_all:
|
|
from dsh_agent_runtime import stop_all_dsh
|
|
|
|
stopped = stop_all_dsh()
|
|
print(json.dumps(stopped, ensure_ascii=False))
|
|
return 0 if stopped["unconfirmed"] == 0 else 1
|
|
try:
|
|
request = _read_initial_request(sys.stdin)
|
|
except (ValueError, OSError):
|
|
print(json.dumps({"type": "result", "engine": "DSH", "status": "FAILED", "answer": "",
|
|
"toolCalls": [], "stopReason": "invalid_request", "message": "首行分析请求无效。"},
|
|
ensure_ascii=False))
|
|
return 2
|
|
run_turn(request)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|