统一:DSH 智能分析、Top100 与图表静默恢复

接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
2026-11-12 11:57:41 +08:00
parent cc8832c8e5
commit f1a2fbd494
166 changed files with 26281 additions and 2248 deletions
@@ -0,0 +1,300 @@
#!/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())
+47
View File
@@ -0,0 +1,47 @@
"""Bounded replacement frames for DSH's provisional assistant text."""
import hashlib
import re
import time
_PRIVATE_OUTPUT = re.compile(
r"(?i)https?://|bearer\s|api[_-]?key\b|authorization\b|clientKey\b|accessToken\b|\bsk-[a-z0-9_-]{10,}|"
r"quality-turn:|(?:scope|execution|attempt|release|actor|session)[_-]?id\b|"
r"(?:^|\n)\s*(?:\{|```)|<\s*/?\s*[a-z][a-z0-9:-]*(?:\s|/?>)|"
r"系统提示词|内部请求归属标记|本轮已确认任务、身份和上下文|本运行时使用DSH|"
r"【平台能力与职责】|【默认业务口径】|【内容组织】"
)
_UUID = re.compile(r"(?i)\b[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\b")
_NONPRINTING = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]")
def display_text(value):
if not isinstance(value, str) or _PRIVATE_OUTPUT.search(value):
return ""
return _NONPRINTING.sub("", _UUID.sub("[标识已隐藏]", value)).strip()[:4000]
class DraftPublisher:
def __init__(self, emit, *, clock=time.monotonic):
self.emit = emit
self.clock = clock
self.last_time = float("-inf")
self.current = ""
def update(self, attempt, text):
candidate = display_text(text)
if len(candidate) < 16:
self.clear()
return
now = self.clock()
if candidate == self.current or now - self.last_time < 0.25:
return
self.current = candidate
self.last_time = now
key = hashlib.sha256(attempt.encode("utf-8")).hexdigest()[:24]
self.emit({"type": "progress", "dshDraft": candidate, "dshAttemptId": key})
def clear(self):
if not self.current:
return
self.current = ""
self.emit({"type": "progress", "dshDraft": ""})
@@ -0,0 +1,210 @@
"""Publish DSH's original answer without content review or recomposition."""
from __future__ import annotations
import copy
import json
import time
import re
def initial_read_arguments(task, question):
"""Read the confirmed business frame before asking a model for prose."""
if task.get('kind') == 'report' or task.get('freshQuery') is not True or not task.get('fields'):
return None
dimensions = task.get('dimensions') or []
if task.get('aggregateSelection') and not task.get('comparisonPeriod') and not task.get('comparisonRequired'):
return {'mode': 'query', 'view': task['aggregateSelection']['dimension']}
stages = task.get('stages') or []
count = re.search(r'(?:top|前)\s*(\d+)', question, re.I)
weights = [field for field in task.get('fields', []) if field in ('totalWeight', 'scrapWeight')]
if (count and not task.get('comparisonPeriod') and not task.get('comparisonRequired')
and len(dimensions) == 1 and dimensions[0] in ('department', 'group', 'product')
and len(stages) == 1 and len(weights) == 1):
return {'mode': 'rank', 'dimension': dimensions[0], 'stage': stages[0],
'metric': weights[0], 'limit': min(100, max(1, int(count[1])))}
return {'mode': 'compare' if task.get('comparisonPeriod') else 'query'}
def _documents(directory, excluded):
"""Read this attempt's saved tool results for optional chart rendering."""
documents = []
for path in directory.glob('quality-evidence-*.json'):
if path.name in excluded or path.is_symlink():
continue
try:
document = json.loads(path.read_text())
if isinstance(document, dict) and isinstance(document.get('result'), dict):
documents.append(document)
except (OSError, ValueError):
continue
return documents
def run_native_exploration(*, request, task, authority, actor_root, delegated, connection,
cancel_event, emit, prior_evidence_files, phase_timings=None):
from dsh_agent_runtime import run_dsh_agent
from quality_agent_task import bind_task
from quality_native_exploration import NativeExplorationTools, native_model_view, _proofs
from quality_protocol_execution import _identity
from quality_runtime_context import _atomic_json, _read_json, ConversationAccumulator
from quality_runtime_policy import turn_deadline
from quality_tools import tool_definition, QualityToolError
timings = dict(phase_timings or {})
identity = _identity(authority)
context = bind_task({key: copy.deepcopy(value) for key, value in request['context'].items()
if key in ('_serverHistory', '_serverQuestions', '_protocol')}, task)
context.update(releaseId=request['releaseId'], _nativeExecutionId=identity['executionId'])
history = task.get('kind') == 'report'
history_snapshot = {}
if history:
started = time.monotonic()
history_snapshot = delegated.call('/api/analysis/protocol/history', payload={}, method='POST')
timings['historySnapshot'] = round((time.monotonic() - started) * 1000)
context['historySnapshot'] = history_snapshot
context['readPolicy'] = '本轮仅使用服务器提供的历史快照生成报告,不重新查询业务数据。'
request['context'] = context
context['analysisDirectory'] = copy.deepcopy(request.get('_analysisDirectory',{}))
_atomic_json(actor_root / 'private/last-agent-task.json', {
'sessionId': request['sessionId'], 'releaseId': request['releaseId'],
'task': task, 'protocol': identity})
tools = None if history else NativeExplorationTools(
delegated=delegated, evidence_dir=actor_root / 'evidence', task=task,
identity={**identity, 'releaseId': request['releaseId']},
state_path=actor_root / 'private/native-exploration-state.json', directory=context['analysisDirectory'])
definition = tool_definition(native_protocol=True, task=task, release_id=request['releaseId'])
allowed_modes = definition['inputSchema']['properties']['mode']['enum']
context['toolPolicy'] = {'allowedModes': allowed_modes,
'guidance': '总体query一次已含全部阶段率、逐月趋势、投入、报废和正式大类。query不传stage,period及comparisonPeriod可省略,由运行时带入;若传必须用对象,不用JSON字符串。需要产业部、业务分组时分别query(view=department/group)一次批量汇总。排名用rank且metric必填:投入重量totalWeight,报废重量scrapWeight。产业部排名用dimension=department,不要先选某个产业部。rank返回行含投入、报废、合格率等辅助字段,不为同一对象再查另一个指标排名或扩大Top数量。按原问题取得足够数据即回答,不为未要求的组织或产品扩展调查。analysisDirectory.periods是全部已发布月份,不等于本轮窗口。相邻月份比较使用query的trend逐月行,窗口合计不是焦点月份值,不另行调用compare。重复查询返回reusedEvidence时直接使用已有结果,不重复工具循环。'}
if task.get('aggregateSelection'):
context['aggregateSelection'] = copy.deepcopy(task['aggregateSelection'])
context['toolPolicy']['guidance'] += ' 本轮按aggregateSelection查询完整分组,initialObservation.rows已先按汇总重量筛选、再按正式合格率排序并取所需Top N(最多100)。保持返回名次和条件,直接撰写回答;不再用重量rank代替合格率排名,不再扩大读取或把summary当作Top N合计。原始需求见aggregateSelection.requestQuestion,当前消息可能只是补充口径。'
from dsh_model_input import adjacent_month_period
adjacent = adjacent_month_period(request['question'], context)
if adjacent:
context['focusPeriod'] = adjacent['focusPeriod']
context['toolPolicy']['guidance'] += ' 本题以focusPeriod月份为主,用query返回的focusMonth获取当月指标和正式报废大类,用trend作前后对比;不把summary窗口合计当作当月。未明确要求具体来源缺陷时,当月正式大类已能说明报废线索,无需再查窗口defects或组织/产品榜单。总体低点不代表各阶段的合格率或报废率也都最低;每个指标独立按trendSummary判断,不推断两阶段一定同向。'
from quality_report_deliverable import requested
context['responsePolicy'] = ('直接回答用户本轮问题,保留必要的口径说明;普通问答不套报告标题、导读、章节或重复结论,不主动生成报告文件。'
if not requested(request['question'], task) else
'报告按问题自主组织:排名重在名次与集中度,诊断重在焦点月份与相邻月差异,结构专题重在类别占比与缺陷。导读控制在三句以内,正文不重复整段导读;一份明细表不在多个章节重复。只呈现题目需要的内容,不为凑章节扩大查询。')
def authorize():
if cancel_event.is_set():
raise RuntimeError('cancelled')
delegated.call('/api/analysis/model-authorization', method='GET')
def query(arguments):
if history:
raise QualityToolError('本轮仅整理已有历史快照,不允许重新查询业务数据。')
if not isinstance(arguments, dict) or arguments.get('mode') not in allowed_modes:
raise QualityToolError('本轮可用mode为 '+', '.join(allowed_modes)+';请选择可用模式。排名用rank并传metric=totalWeight或scrapWeight。')
return native_model_view(tools.quality_data(arguments), compact=True, task=task)
started = time.monotonic()
initial = None
arguments = None if history else initial_read_arguments(task, request['question'])
if arguments:
initial = ConversationAccumulator()
call_id = 'initial-' + request['turnId']
initial.feed({'type': 'tool-input-available', 'toolName': 'mcp_call', 'toolCallId': call_id,
'input': {'tool': 'quality_data', 'arguments': arguments}})
emit({'type': 'progress', 'message': '正在读取本题正式数据…',
'toolCalls': initial.tool_calls, 'toolCount': len(initial.tool_calls), 'timeline': initial.timeline})
try:
observation = query(arguments)
except Exception as error:
observation = {'error': {'code': getattr(error, 'code', 'unavailable'),
'message': str(error) if isinstance(error, QualityToolError) else '本题正式数据读取未完成。'}}
initial.feed({'type': 'tool-output-available', 'toolName': 'mcp_call', 'toolCallId': call_id,
'output': observation})
context['initialObservation'] = observation
context['initialObservationCall'] = arguments
emit({'type': 'progress', 'message': '正在根据查询结果生成回答…',
'toolCalls': initial.tool_calls, 'toolCount': len(initial.tool_calls), 'timeline': initial.timeline})
timings['initialRead'] = round((time.monotonic() - started) * 1000)
if initial.tool_calls[0].get('status') == 'completed':
timings['firstUsefulMs'] = sum(timings.get(k, 0) for k in
('planningAndReview', 'taskConfirmation', 'historySnapshot', 'initialRead'))
def progress(event):
if ('firstUsefulMs' not in timings and any(
call.get('status') == 'completed' and (call.get('input') or {}).get('mode') not in (None, 'catalog')
for call in event.get('toolCalls', []))):
timings['firstUsefulMs'] = (sum(timings.get(k, 0) for k in
('planningAndReview', 'taskConfirmation', 'historySnapshot'))
+ round((time.monotonic() - started) * 1000))
emit(event)
emit({'type': 'progress', 'message': '正在生成回答…', 'executionMode': 'native_agent'})
accumulator, status, reason, stopped = run_dsh_agent(
actor_root=actor_root, connection=connection, question=request['question'],
context=context, task=task, identity=identity, turn_id=request['turnId'],
cancel_event=cancel_event, emit=progress, tool_handler=query, authorize=authorize,
tool_definition=definition, deadline=turn_deadline(), initial_accumulator=initial)
timings['investigation'] = round((time.monotonic() - started) * 1000)
answer = accumulator.answer if isinstance(accumulator.answer, str) else ''
if cancel_event.is_set():
status, reason = 'CANCELLED', 'user_cancel'
elif status not in ('COMPLETED', 'PARTIAL', 'FAILED', 'CANCELLED'):
status, reason = 'FAILED', reason or 'runtime_error'
business_calls = [call for call in accumulator.tool_calls
if (call.get('input') or {}).get('mode') not in (None, 'catalog')]
if status == 'COMPLETED' and business_calls and not any(call.get('status') == 'completed' for call in business_calls):
status, reason = 'FAILED', 'tool_queries_failed'
# Charts are optional presentation. A chart/provenance failure cannot
# suppress the model body or change the actual agent execution status.
documents, receipts = [], []
visuals = {'visualSchemaVersion': 1, 'visualState': 'not_requested', 'visuals': []}
if history:
visuals.update({key: copy.deepcopy(history_snapshot[key]) for key in
('visualSchemaVersion', 'visualState', 'visuals') if key in history_snapshot})
elif status != 'CANCELLED':
started = time.monotonic()
try:
from analysis_visual_evidence import protocol_visual_result
documents = _documents(actor_root / 'evidence', prior_evidence_files)
receipts = [proof for document in documents for proof in _proofs(document['result'])]
snapshot = delegated.call('/api/analysis/protocol/queries', payload={}, method='POST')
visuals = protocol_visual_result(documents, snapshot.get('scopes', []),
request['question'], request['turnId'], release_id=request['releaseId'], base_context=context)
if visuals.get('missingEvidence') or visuals.get('visualState') in ('failed', 'unavailable'):
_atomic_json(actor_root / 'private/chart-render-diagnostics.json', {
'state': visuals.get('visualState'), 'missing': visuals.get('missingEvidence', [])})
except Exception as error:
_atomic_json(actor_root / 'private/chart-render-error.json', {'errorType': type(error).__name__})
timings['visuals'] = round((time.monotonic() - started) * 1000)
state_path = actor_root / 'private/native-exploration-state.json'
try:
timings['formalQuery'] = _read_json(state_path).get('queryElapsedMs', 0) if state_path.is_file() else 0
except (OSError, ValueError):
timings['formalQuery'] = 0
timings['synthesis'] = 0
result = {
'type': 'result', 'engine': 'DSH', 'status': status, 'answer': answer,
'agentTask': task, 'protocol': identity, 'executionMode': 'native_agent',
'completionMethod': 'dsh_direct_answer',
'stopReason': 'stop' if status == 'COMPLETED' else str(reason or status).lower(),
'finishReason': (accumulator.finish_reasons[-1] if accumulator.finish_reasons else
'stop' if status == 'COMPLETED' else str(reason or status).lower()),
'nativeStopReason': reason, 'nativeStopSucceeded': stopped,
'actualModel': connection[2], 'phaseTimingsMs': timings,
'toolCalls': accumulator.tool_calls, 'toolCount': len(accumulator.tool_calls),
'timeline': accumulator.timeline, 'protocolEvidence': receipts,
'queryEvidenceRefs': [doc['evidenceRef'] for doc in documents if isinstance(doc.get('evidenceRef'), str)],
'queryEvidenceBindings': [{'evidenceRef': doc['evidenceRef'],
'queryIds': list(dict.fromkeys(p['queryId'] for p in _proofs(doc['result'])))}
for doc in documents if isinstance(doc.get('evidenceRef'), str)],
'agentContext': {**{key: copy.deepcopy(task[key]) for key in
('filters', 'period', 'comparisonPeriod') if task.get(key) is not None},
'releaseId': request['releaseId']},
'visualSchemaVersion': visuals.get('visualSchemaVersion', 1),
'visualState': visuals.get('visualState', 'not_requested'), 'visuals': visuals.get('visuals', []),
}
if history:
for key in ('historySources', 'historySections', 'historyCoverage', 'historyEvidence', 'historyVisualSources'):
if key in history_snapshot:
result[key] = copy.deepcopy(history_snapshot[key])
emit(result)
return result
@@ -0,0 +1,122 @@
"""Preserve assistant prose independently of evidence and presentation validation."""
from contextvars import ContextVar
import json
import re
import threading
import time
_CURRENT = ContextVar('quality_original_answer', default=None)
def current():
return _CURRENT.get()
def capture(content, *, phase='native_synthesis'):
store = current()
if store is not None and phase == 'native_synthesis':
store.capture_composition(content)
def capture_envelope(raw, *, phase='native_synthesis'):
"""Capture only provider assistant content, including an interrupted finish."""
try:
document = json.loads(raw)
content = document['choices'][0]['message']['content']
except (ValueError, TypeError, KeyError, IndexError):
return
capture(content, phase=phase)
def business_text(content):
"""Unwrap business text fields without rewriting the model's strings."""
if not isinstance(content, str):
return '', None
try:
document = json.loads(content)
except (ValueError, TypeError):
return content, None
if not isinstance(document, dict):
return content, None
title = document.get('reportTitle')
for field in ('blocks', 'answerBlocks'):
blocks = document.get(field)
if isinstance(blocks, list):
texts = [block['text'] for block in blocks
if isinstance(block, dict) and isinstance(block.get('text'), str)]
if texts:
return '\n\n'.join(texts), title if isinstance(title, str) else None
for field in ('answer', 'markdown', 'content', 'text'):
if isinstance(document.get(field), str):
return document[field], title if isinstance(title, str) else None
return content, title if isinstance(title, str) else None
class OriginalAnswer:
def __init__(self, emit):
self.emit = emit
self.text = ''
self.title = None
self.source = None
self.lock = threading.Lock()
self.last_time = float('-inf')
self.token = None
def __enter__(self):
self.token = _CURRENT.set(self)
return self
def __exit__(self, *_args):
_CURRENT.reset(self.token)
def capture_native(self, text):
heading = re.search(r'^#{1,6} +([^\n]+)', text, re.M) if isinstance(text, str) else None
self._capture(text, 'dsh_assistant', title=heading[1] if heading else None, force=False)
def capture_composition(self, content):
with self.lock:
# Review or retry output cannot replace the first business answer.
if self.source is not None:
return
text, title = business_text(content)
self._capture(text, 'model_business_text', title=title, force=True)
def _capture(self, text, source, *, title=None, force):
if not isinstance(text, str) or not text:
return
with self.lock:
self.text, self.source = text, source
if title is not None or source == 'dsh_assistant':
self.title = title
now = time.monotonic()
publish = force or now - self.last_time >= 0.25
if publish:
self.last_time = now
if publish:
self.emit({'type': 'progress', 'modelAnswer': text})
def settle(self, event):
"""Publish raw prose for every terminal status, with validation metadata."""
if self.source is None and event.get('completionMethod') == 'direct_conversation':
self.capture_composition(event.get('answer'))
if self.source is None and event.get('status') == 'NEEDS_CLARIFICATION':
task = event.get('agentTask') or {}
if isinstance(task.get('clarification'), str):
self.capture_composition(task['clarification'])
event['modelAnswer'] = self.text
event['answer'] = self.text
event['answerOrigin'] = self.source or 'no_model_text'
event.pop('readingSummary', None)
event.pop('reportHtml', None)
event.pop('_verifiedReportNarrative', None)
if self.title is not None:
event['reportTitle'] = self.title
elif self.source == 'dsh_assistant':
event.pop('reportTitle', None)
event['answerValidation'] = {
'status': event.get('status'),
'stopReason': event.get('stopReason'),
'answerQuality': event.get('answerQuality'),
'evidenceCompletion': event.get('evidenceCompletion'),
'originalPreserved': True,
}
@@ -0,0 +1,32 @@
"""Deliver the original model body when the user requests a report."""
import re
_REQUEST = re.compile(
r"(?:生成|输出|编写|撰写|写|制作|形成|起草|给我|提供|整理(?:为|成)|出)"
r"[^。!?;\n]{0,38}(?:报告|简报|排查纪要)|(?:分析|对比|质量|复盘|管理|诊断)(?:报告|简报)\s*[。!!??]*$"
)
_SKIP = re.compile(r"(?:不用|不必|不要|无需|不需要|别|避免|不(?=(?:再)?(?:生成|输出|编写|撰写|写|制作|形成|起草|提供|出)))\s*(?:另行|另外|单独|再)?\s*[^。!?;\n]{0,18}(?:报告|简报|排查纪要)")
def requested(question, task=None):
if not isinstance(question, str) or _SKIP.search(question):
return False
selection = (task or {}).get("aggregateSelection") or {}
return bool(_REQUEST.search(question) or _REQUEST.search(selection.get("requestQuestion", "")))
def attach(event, question):
"""Use the original assistant body for an explicitly requested deliverable."""
event.pop("_verifiedReportNarrative", None)
if event.get("type") != "result":
return
event.pop("reportMarkdown", None)
if not requested(question, event.get("agentTask")):
return
answer = event.get("modelAnswer")
if not isinstance(answer, str) or not answer:
return
event["reportMarkdown"] = answer
if not event.get("reportTitle"):
heading = re.search(r"^#{1,6} +([^\n]+)", answer, re.M)
event["reportTitle"] = heading[1] if heading else "质量分析报告"