Author SHA1 Message Date
lyun 52d92c95d8 修复:智能分析空回答与工具结束条件冲突
统一自主分析工具范围与执行前校验,严格核验正常结束、最终正文和图表恢复状态,并显示空回答及恢复失败原因。

包含两次已上线修复及回归测试,保留已有回答与业务数据。

验证:1101 项 Python、469 项前端、97 项后端测试通过;前后端构建和真实模型、页面验收通过。
2026-11-03 08:56:49 +08:00
lyun 7ffb5cdb9c 优化:趋势图采用轻度平滑曲线并保留柏拉图直线 2026-11-02 16:30:53 +08:00
23 changed files with 392 additions and 39 deletions
+2
View File
@@ -133,3 +133,5 @@
- 更新中的旧范围结果不能钻取、查看明细或导出;区域独立重试。任务使用真实阶段,写入即时忙碌并防重复,不显示虚构百分比或新增常驻说明。
- 返回页面保留筛选、页码、滚动位置及表格高度;隐藏页面和未选管理页签不主动刷新。业务分页、全范围搜索排序和导出不变。
- 实现与验证详见 docs/全系统统一加载实现与验收_20261102.md。
- 2026-11-02:趋势折线使用用户选定的 A 轻度平滑(0.15),沿横轴约束曲线;查询合格率、不良率、智能分析与导出统一。柏拉图累计线保持直线,数据点、缺失断点和计算口径不变。
@@ -2,6 +2,8 @@
先读 /RULES.md 了解正式公式、单位、范围和来源语义;需要时读 /agent/mcps/quality/quality_data.json 一次了解工具。所有业务数据经原生 mcp_call 调用 quality 的 quality_data 取得。不调用数据库、execute_sql、display_chart 或 story;平台负责图表与报告。
本模式开放只读文件工具 read/list/grep/search 和 quality 的 mcp_call/mcp_connect。平台负责查询前的任务澄清、数据绑定和后续追问;不使用原生 clarification、数据库选择、凭据收集、建议追问或写入工具结束任务。证据足够时,在最后一次查询返回后直接输出最终正文,并正常结束;工具返回、图表安排或进度说明不等于最终回答。
本会话使用自主调查模式。遵守RULES中的正式公式、数据语义与证据边界,不设固定全维度覆盖、维度顺序或自动补查配方。以本轮 _agentTask 的明确目标和约束为准:必要基线→观察结果→选择下次查询或验证反例→判断是否已回答。不要为了覆盖目录而查询每个产业部、分组或规格,不固定只追一个产品或一个阶段。若多个线索同样重要,应比较后再判断。
正式数值直接来自工具,不能自行重算KPI或编造缺失值。你可以解释分子分母、规模、结构及阶段的关系,清楚区分已核实事实、统计线索与待验证生产假设;引用真实 evidenceRef。工具支持的正式差值和阶段拆解可以直接引用;缺少的数据不得补零。统计关联不能宣称已证实设备、工艺、供应商或人员根因。
@@ -326,8 +326,16 @@ public class AnalysisProtocolService {
metadata.put("sourceStatus",input.get("sourceStatus"));metadata.put("queryRefs",checked.get("queryRefs"));metadata.put("missingEvidence",checked.get("missingEvidence"));
var overlay=new LinkedHashMap<String,Object>();for(String key:List.of("visualSchemaVersion","visualState","visuals"))overlay.put(key,checked.get(key));overlay.put("visualRecovery",metadata);
boolean originalComplete="COMPLETED".equals(input.get("sourceStatus"));
boolean factsComplete=Boolean.TRUE.equals(map(map(input.get("sourceAnswer")).get("factCoverage")).get("complete"))&&Boolean.TRUE.equals(input.get("factsStillCovered"));
if(!originalComplete&&factsComplete&&Boolean.TRUE.equals(checked.get("chartsComplete"))) {
var sourceAnswer=map(input.get("sourceAnswer"));var quality=new LinkedHashMap<>(map(sourceAnswer.get("answerQuality")));
boolean chartsComplete=Boolean.TRUE.equals(checked.get("chartsComplete"));
if(!quality.isEmpty()){quality.put("chartsComplete",chartsComplete);overlay.put("answerQuality",quality);}
boolean factsComplete=Boolean.TRUE.equals(map(sourceAnswer.get("factCoverage")).get("complete"))&&Boolean.TRUE.equals(input.get("factsStillCovered"));
boolean narrativeComplete=Boolean.TRUE.equals(quality.get("narrativeComplete"))&&Boolean.TRUE.equals(quality.get("dataComplete"))
&&!Boolean.FALSE.equals(sourceAnswer.get("nativeStopSucceeded"))
&&!Boolean.FALSE.equals(quality.get("citationComplete"))&&list(quality.get("unresolvedEvidenceRefs")).isEmpty()
&&!Objects.toString(sourceAnswer.get("answer"),"").isBlank();
if(!originalComplete&&"PARTIAL".equals(input.get("sourceStatus"))&&factsComplete&&narrativeComplete&&chartsComplete) {
metadata.put("priorMissingEvidence",list(quality.get("missingEvidence")));quality.put("missingEvidence",List.of());
overlay.put("status","COMPLETED");overlay.put("originalStatus",input.get("sourceStatus"));
overlay.put("completion",Map.of("completed",true,"reason","visual_recovery_complete"));overlay.put("stopReason","visual_recovery_complete");
}
@@ -13,6 +13,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
@@ -429,6 +430,26 @@ public class NaoAnalysisRunner {
status = "FAILED";
if (reason.isBlank()) reason = "原装 NAO 返回了无效结束状态";
}
boolean conflictingExit=Boolean.FALSE.equals(message.get("nativeStopSucceeded"))
|| message.get("finishReason") instanceof String finish&&!finish.isBlank()&&!"stop".equalsIgnoreCase(finish)
|| message.get("toolCalls") instanceof List<?> calls&&calls.stream().anyMatch(raw->raw instanceof Map<?,?> call&&"started".equals(call.get("status")))
|| message.get("answerQuality") instanceof Map<?,?> quality&&Boolean.FALSE.equals(quality.get("narrativeComplete"));
if(Set.of("COMPLETED","PARTIAL").contains(status)&&conflictingExit) {
if("COMPLETED".equals(status)){status="PARTIAL";reason="incomplete_finish";}
var quality=new LinkedHashMap<String,Object>();
if(message.get("answerQuality") instanceof Map<?,?> prior)prior.forEach((k,v)->quality.put(String.valueOf(k),v));
quality.put("version",1);quality.put("narrativeComplete",false);details.put("answerQuality",quality);
}
boolean visibleBody=!answer.isBlank()
|| message.get("reportMarkdown") instanceof String markdown&&!markdown.isBlank()
|| message.get("reportHtml") instanceof String html&&!html.isBlank()
|| message.get("memberList") instanceof Map<?,?> || message.get("recordList") instanceof Map<?,?>;
if("COMPLETED".equals(status)&&!visibleBody) {
status="PARTIAL";reason="answer_missing";
var quality=new LinkedHashMap<String,Object>();
if(message.get("answerQuality") instanceof Map<?,?> prior)prior.forEach((k,v)->quality.put(String.valueOf(k),v));
quality.put("version",1);quality.put("narrativeComplete",false);details.put("answerQuality",quality);
}
if ("FAILED".equals(status) && safeMessage != null) {
details.put("message", safeMessage);
if (answer.isBlank()) answer = safeMessage;
@@ -437,6 +458,7 @@ public class NaoAnalysisRunner {
details.remove("message");
}
details.put("status", status);
details.put("stopReason", reason);
details.put("answer", answer);
details.put("engine", "NAO");
return new Result(status, answer, details, reason);
@@ -273,6 +273,15 @@ class AnalysisProtocolServiceMySqlTest {
tx.executeWithoutResult(s->service.completeVisualRecovery(claim.get("id").toString(),claim.get("token").toString(),proposed,actor));
assertEquals(1,AnalysisProtocolService.list(tx.execute(s->service.visualRecoveryOverlay(turn,actor)).get("visuals")).size());
}
@Test void recoveredChartsCannotCompleteAnUnfinishedNarrative() {
String turn=recoverableTurn(true,false);
var claim=tx.execute(s->service.beginVisualRecovery(turn,actor));var input=AnalysisProtocolService.map(claim.get("input"));
tx.executeWithoutResult(s->service.completeVisualRecovery(claim.get("id").toString(),claim.get("token").toString(),recoveryChart(input),actor));
var overlay=tx.execute(s->service.visualRecoveryOverlay(turn,actor));
assertFalse(overlay.containsKey("status"),json.writeValueAsString(overlay));
assertEquals(true,AnalysisProtocolService.map(overlay.get("answerQuality")).get("chartsComplete"));
assertEquals(false,AnalysisProtocolService.map(overlay.get("answerQuality")).get("narrativeComplete"));
}
@Test void aChartCannotCompleteMissingFactsOrPublishAfterTheOriginalAttemptIsRetried() {
String turn=recoverableTurn(false);var claim=tx.execute(s->service.beginVisualRecovery(turn,actor));var input=AnalysisProtocolService.map(claim.get("input"));
tx.executeWithoutResult(s->service.completeVisualRecovery(claim.get("id").toString(),claim.get("token").toString(),recoveryChart(input),actor));
@@ -288,6 +297,9 @@ class AnalysisProtocolServiceMySqlTest {
assertTrue(tx.execute(s->service.visualRecoveryOverlay(turn,actor)).isEmpty());
}
private String recoverableTurn(boolean facts) {
return recoverableTurn(facts,true);
}
private String recoverableTurn(boolean facts,boolean narrative) {
String turn=newTurn("2026年8至9月黑片报废重量月度趋势");var a=claim(turn,false);var t=task(turn,Map.of(),false);
t.put("period",Map.of("year",2026,"monthStart",8,"monthEnd",9));t.put("periodCoverage","available");t.put("stages",List.of("black"));t.put("fields",List.of("scrapWeight"));t.put("visualPreference","required");
var confirmed=AnalysisProtocolService.map(tx.execute(s->service.confirm(a.attemptId(),t,actor)).get("task"));
@@ -298,6 +310,8 @@ class AnalysisProtocolServiceMySqlTest {
"coverage",Map.of("rawRows",2,"includedRows",2,"sourceMonths",List.of(Map.of("year",2026,"month",8,"blackRows",1,"brightRows",0),Map.of("year",2026,"month",9,"blackRows",1,"brightRows",0))));
var proof=tx.execute(s->service.recordQuery(a.attemptId(),"GET","/api/metrics",parameters,response,actor));
var result=answer();result.put("visualState","failed");result.put("visuals",List.of());
if(!narrative)result.put("answer","");
result.put("answerQuality",Map.of("version",1,"dataComplete",facts,"narrativeComplete",narrative,"chartsComplete",false));
tx.executeWithoutResult(s->service.finish(a.attemptId(),"PARTIAL",result,Map.of("agentTask",confirmed,"protocolEvidence",List.of(proof),"factsComplete",facts,"evidenceCompletion",Map.of("serverCoverageSufficient",true)),actor));
return turn;
}
@@ -6,6 +6,22 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
class NaoAnalysisRunnerResultTest {
@Test void aStopReasonCannotOverrideConflictingFinishOrPendingTools() {
for(var conflict:java.util.List.of(Map.<String,Object>of("finishReason","tool-calls"),
Map.<String,Object>of("nativeStopSucceeded",false),
Map.<String,Object>of("toolCalls",java.util.List.of(Map.of("tool","mcp_call","status","started"))),
Map.<String,Object>of("answerQuality",Map.of("narrativeComplete",false)))) {
var message=new java.util.LinkedHashMap<String,Object>();message.put("type","result");message.put("status","COMPLETED");message.put("stopReason","stop");message.put("answer","正在查询。");message.putAll(conflict);
assertEquals("PARTIAL",NaoAnalysisRunner.Result.from(message,"",false).status());
}
}
@Test void anEmptyStopResultCannotCompleteUnlessItHasAVisibleStructuredBody() {
var empty=NaoAnalysisRunner.Result.from(Map.of("type","result","status","COMPLETED","answer","", "stopReason","stop"),"",false);
assertEquals("PARTIAL",empty.status());assertEquals("answer_missing",empty.stopReason());
assertEquals(false,AnalysisProtocolService.map(empty.details().get("answerQuality")).get("narrativeComplete"));
var report=NaoAnalysisRunner.Result.from(Map.of("type","result","status","COMPLETED","answer","", "stopReason","stop","reportMarkdown","已生成的报告"),"",false);
assertEquals("COMPLETED",report.status());
}
@Test
void onlyNativeStopFinishReasonCanComplete() {
NaoAnalysisRunner.Result completed = NaoAnalysisRunner.Result.from(Map.of(
+17 -8
View File
@@ -62,20 +62,29 @@ def capability_context(task=None):
}
def apply_platform_tools(payload):
"""Do not advertise a native SQL-result renderer for quality API evidence."""
QUALITY_NATIVE_TOOLS = frozenset({'read', 'list', 'grep', 'search', 'mcp_call', 'mcp_connect'})
def apply_platform_tools(payload, *, native_agent=False):
"""Expose tools compatible with platform evidence and answer completion."""
tools = payload.get('tools')
if not isinstance(tools, list): return payload
kept = [tool for tool in tools if not (isinstance(tool, dict)
and isinstance(tool.get('function'), dict)
and tool['function'].get('name') == 'display_chart')]
if len(kept) == len(tools): return payload
allowed = QUALITY_NATIVE_TOOLS if native_agent else QUALITY_NATIVE_TOOLS | {'story'}
# Clarification/warehouse selection are upstream stop conditions with native
# UI replies that this platform cannot consume. SQL, renderers and writers
# also use a different data/artifact lifecycle. Unknown tools stay disabled.
tools = tools if isinstance(tools, list) else []
kept = [tool for tool in tools if isinstance(tool, dict) and tool.get('type') == 'function'
and isinstance(tool.get('function'), dict) and isinstance(tool['function'].get('name'), str)
and tool['function'].get('name') in allowed]
if kept: payload['tools'] = kept
else: payload.pop('tools', None)
choice = payload.get('tool_choice')
if ((isinstance(choice, dict) and (choice.get('function') or {}).get('name') == 'display_chart')
kept_names = {tool['function']['name'] for tool in kept}
choice_name = choice.get('function', {}).get('name') if isinstance(choice, dict) and isinstance(choice.get('function'), dict) else None
if ((isinstance(choice, dict) and choice_name not in kept_names)
or not kept and choice in ('auto', 'required')):
payload.pop('tool_choice', None)
if not kept: payload.pop('parallel_tool_calls', None)
return payload
+50 -13
View File
@@ -21,6 +21,10 @@ class _NoRedirect(HTTPRedirectHandler):
return None
class ToolPolicyError(ValueError):
pass
class ModelGate:
"""A stable per-session endpoint backed only by this turn's live delegation."""
@@ -41,7 +45,9 @@ class ModelGate:
self._thinking = dict(thinking_options) if thinking_options is not None else None
self._on_thinking = on_thinking
self._native_turn_marker = native_turn_marker
self.native_agent = False
self.rejected_stale_requests = 0
self.rejected_tool_calls = 0
self._request_number = 0
self._request_prefix = secrets.token_hex(4)
self.request_audit = []
@@ -92,9 +98,10 @@ class ModelGate:
for message in messages)):
with gate._audit_lock: gate.rejected_stale_requests += 1
self.failure(403, '模型请求不属于当前轮次'); return
decoded = gate.apply_thinking(decoded)
from nao_agent_contract import apply_platform_tools
decoded = apply_platform_tools(decoded)
decoded = apply_platform_tools(decoded, native_agent=gate.native_agent)
decoded = gate.apply_thinking(decoded)
offered_tools = {tool['function']['name'] for tool in decoded.get('tools', [])}
payload = json.dumps(decoded,ensure_ascii=False).encode()
except (ValueError, OSError):
self.failure(400, '模型请求格式无效'); return
@@ -141,9 +148,10 @@ class ModelGate:
thinking('start')
# SSE termination is a transport concern even when no thinking
# callback is installed (for example, native stream forwarding).
observer = ProviderStream(lambda text:thinking('delta',text=text)) if gate._on_thinking or is_sse else None
observer = ProviderStream(lambda text:thinking('delta',text=text))
raw_json = bytearray()
if not bridge:
pending_wire = bytearray()
if is_sse and not bridge:
self.send_response(response.status)
self.send_header('Content-Type', response.headers.get('Content-Type', 'application/json'))
self.send_header('Cache-Control', 'no-store')
@@ -161,21 +169,29 @@ class ModelGate:
if time.monotonic() - checked_at >= 1:
gate._check()
checked_at = time.monotonic()
if observer:
if is_sse:observer.feed(chunk)
else:raw_json.extend(chunk)
if not bridge:
self.wfile.write(chunk)
self.wfile.flush()
if is_sse:
observer.feed(chunk)
gate.check_returned_tools(observer, offered_tools)
if not bridge:
# A partial SSE line cannot be checked yet. Hold
# it until its complete event has passed policy.
pending_wire.extend(chunk)
end=pending_wire.rfind(b'\n')+1
if end:
self.wfile.write(pending_wire[:end]);self.wfile.flush()
del pending_wire[:end]
else:raw_json.extend(chunk)
# A provider may keep the HTTP connection alive with heartbeats
# after its explicit terminal event. Do not wait for EOF, but
# keep reading past finish_reason so the usage tail is preserved.
if is_sse and observer.done:
break
gate._check()
if observer:
if is_sse:observer.finish()
else:observer.document(json.loads(raw_json))
if is_sse:observer.finish()
else:observer.document(json.loads(raw_json))
gate.check_returned_tools(observer, offered_tools, final=True)
if is_sse and not bridge and pending_wire:
self.wfile.write(pending_wire);self.wfile.flush()
result = observer.completion() if bridge or is_sse else None
stops = [row['finish_reason'] for row in observer.choices.values()] if observer else []
thinking('end',finishReason='length' if 'length' in stops else (stops[-1] if stops else ''),interrupted=bool(observer and not stops))
@@ -188,6 +204,15 @@ class ModelGate:
self.send_response(200);self.send_header('Content-Type','application/json')
self.send_header('Cache-Control','no-store');self.send_header('Content-Length',str(len(encoded)))
self.end_headers();headers_sent=True;self.wfile.write(encoded);self.wfile.flush()
elif not is_sse:
self.send_response(response.status);self.send_header('Content-Type','application/json')
self.send_header('Cache-Control','no-store');self.send_header('Content-Length',str(len(raw_json)))
self.end_headers();headers_sent=True;self.wfile.write(raw_json);self.wfile.flush()
except ToolPolicyError:
if not headers_sent:self.failure(502,'模型返回了当前任务未开放的工具调用')
else:
with contextlib.suppress(OSError):
self.wfile.write(b'data: {"error":{"message":"Tool call is unavailable for this task"}}\n\n');self.wfile.flush()
except HTTPError as error:
if not headers_sent:
self.failure(error.code if 400 <= error.code <= 599 else 502, '模型接口暂不可用')
@@ -230,6 +255,8 @@ class ModelGate:
else:payload['reasoning_effort']=effort
with self._audit_lock:
if len(self.request_audit)<200:self.request_audit.append({'enabled':enabled,'effort':effort,
'tools':[tool['function']['name'] for tool in payload.get('tools',[])],
'nativeAgent':self.native_agent,
'protocol':protocol,'stream':payload.get('stream',False),
'maxCompletionTokens':payload.get('max_completion_tokens',payload.get('max_tokens')),
'thinkingType':(payload.get('thinking') or {}).get('type'),
@@ -237,6 +264,16 @@ class ModelGate:
'reasoningEffort':payload.get('reasoning_effort',(payload.get('chat_template_kwargs') or {}).get('reasoning_effort'))})
return payload
def check_returned_tools(self, observer, offered, *, final=False):
for choice, tools in observer.tools.items():
finished=final or bool(observer.choices.get(choice,{}).get('finish_reason')) or observer.done
for tool in tools.values():
name=tool.get('function',{}).get('name','')
valid=(name in offered) if finished else (not name or any(allowed.startswith(name) for allowed in offered))
if not valid:
with self._audit_lock:self.rejected_tool_calls+=1
raise ToolPolicyError('Unadvertised tool call')
def _check(self) -> None:
if self._closed.is_set():
raise PermissionError('模型授权已关闭')
+3 -1
View File
@@ -112,7 +112,9 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
and type(progress.get('noProgress')) is int and progress['noProgress'] >= 3)
if controlled_no_progress:
missing.append('连续三次没有取得新证据,调查已停止;其余线索尚未验证。')
natural_finish = 'stop' in accumulator.finish_reasons and stop_succeeded is not False
natural_finish = bool(accumulator.finish_reasons and accumulator.finish_reasons[-1] == 'stop'
and stop_succeeded is not False
and getattr(accumulator, 'final_answer_ready', True))
corrected_tool_errors = (native_reason == 'mcp_error' and natural_finish
and complete_scopes is not None and bool(documents)
and not missing and not progress.get('halted'))
+30 -4
View File
@@ -763,6 +763,17 @@ class ConversationAccumulator:
return value
return ""
@property
def final_answer_ready(self) -> bool:
if not self.answer or any(call.get('status') == 'started' for call in self.tool_calls):
return False
if not self.tool_calls:
return True
last_tool = max((i for i, entry in enumerate(self.timeline)
if entry.get('kind') in ('tool_start', 'tool_result')), default=-1)
return any(entry.get('kind') == 'assistant' and str(entry.get('text') or '').strip()
for entry in self.timeline[last_tool + 1:])
def status(self, *, cancel_cause: str | None = None, stop_succeeded: bool | None = None,
transport_failed: bool = False) -> tuple[str, str | None]:
if cancel_cause == "user":
@@ -781,18 +792,27 @@ class ConversationAccumulator:
return ("PARTIAL" if self.answer else "FAILED", "transport_or_stream_error")
if self.tool_error and (self.unattributed_tool_error or any(call["status"] == "failed" and not call.get("recovered") for call in self.tool_calls)):
return ("PARTIAL" if self.answer else "FAILED", "mcp_error")
if self.answer and (self.done_received or "stop" in self.finish_reasons):
if (self.final_answer_ready and self.finish_reasons[-1] == 'stop'
and stop_succeeded is not False):
return "COMPLETED", "stop"
return ("PARTIAL" if self.answer else "FAILED", "incomplete")
def _contains_error_flag(value: Any) -> bool:
if isinstance(value, dict):
if value.get("isError") is True or value.get("error"):
if (any(value.get(flag) is True for flag in ('isError', 'mcpValidationError', 'mcpAuthRequired'))
or value.get('success') is False or value.get('error')):
return True
return any(_contains_error_flag(item) for item in value.values())
# Inspect only known response envelopes. Rows, source cells, notes and
# arbitrary JSON strings are business data, not nested tool outcomes.
content = value.get('content')
if isinstance(content, list) and any(isinstance(part, dict) and part.get('type') == 'text'
and _contains_error_flag(part.get('text')) for part in content):
return True
return _contains_error_flag(value['structuredContent']) if isinstance(value.get('structuredContent'), dict) else False
if isinstance(value, list):
return any(_contains_error_flag(item) for item in value)
return any(isinstance(part, dict) and part.get('type') == 'text'
and _contains_error_flag(part.get('text')) for part in value)
if isinstance(value, str) and len(value) <= 64 * 1024:
stripped = value.strip()
if stripped.startswith(("{", "[")):
@@ -1922,6 +1942,10 @@ def _sse_request(
ready_state: dict[str, Any] = {"identity": None, "since": None}
def completion_ready() -> bool:
# The watchdog can observe a stale halted exploration state between a
# natural model finish and the stream's trailing [DONE] marker.
if accumulator.done_received or (accumulator.finish_reasons and accumulator.finish_reasons[-1] == 'stop'):
return False
if autonomous:
progress_path = actor_root / 'private' / 'native-exploration-state.json'
if not _require_private_regular_file(progress_path):
@@ -2630,6 +2654,7 @@ def _run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any =
from nao_protocol_execution import execute
request['_autonomousAnswer'] = protocol.get('autonomousEnabled') is True
if authority['task'].get('executionMode') == 'native_agent':
model_gate.native_agent = True
from nao_native_run import run_native_exploration
return run_native_exploration(request=request, task=authority['task'], authority=authority,
actor_root=actor_root, delegated=delegated, connection=selected_connection,
@@ -3108,6 +3133,7 @@ def _run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any =
if model_gate is not None:
try:
_atomic_json(actor_root/'private'/'last-model-audit.json',{'model':selected_model,'requests':model_gate.request_audit,
'rejectedToolCalls':model_gate.rejected_tool_calls if isinstance(model_gate.rejected_tool_calls,int) else 0,
'rejectedStaleRequests':model_gate.rejected_stale_requests if isinstance(model_gate.rejected_stale_requests,int) else 0})
except (OSError,TypeError,ValueError):pass
model_gate.close()
+1 -1
View File
@@ -97,7 +97,7 @@ class MinimalQueryTests(unittest.TestCase):
context=bind_task({'releaseId':RELEASE},task))
self.assertEqual(visual['visualState'],'ready',visual)
self.assertEqual(visual['visuals'][0]['metric'],'rate_drop_pp')
self.assertEqual(visual['visuals'][0]['series'][0]['values'],[20.0])
self.assertEqual(visual['visuals'][0]['series'][0]['values'],[-20.0])
result['sort']='current_rate';task['changeQuery']['sort']='current_rate'
visual=build_visuals([doc],task['question'],TURN,
context=bind_task({'releaseId':RELEASE},task))
+12 -2
View File
@@ -149,14 +149,24 @@ class EvidenceFinalizerTest(unittest.TestCase):
with self.assertRaises(finalizer.FinalizationUnavailable):
finalizer.packed_evidence([{'unique': 'a'*finalizer.MAX_INPUT_CHARS}])
def test_real_second_turn_replay_fits_and_preserves_every_summary_value(self):
def test_real_second_turn_replay_preserves_every_value_and_obeys_current_input_budget(self):
root=Path(__file__).resolve().parents[1]/'output/20261023-autonomous-investigation/runtime'
directories=list(root.glob('.runtime/nao-users/*/sessions/57676d5a-5138-48f4-9343-bae9731aae5a/evidence'))
if not directories: self.skipTest('Private captured acceptance evidence not present')
with patch.object(finalizer,'packed_evidence',side_effect=AssertionError('Local archives do not consume model input budget')):
docs=finalizer.evidence_summaries(directories[0],'194b0287-9044-4d80-9138-ecd3c481015c')
self.assertEqual(len(docs),15)
self.assertEqual(unpack(finalizer.packed_evidence(docs)),docs)
# Formal display metadata has grown since this private run was captured.
# Local archives remain lossless; a bounded model handoff may reject an
# oversized summary, but may never silently trim it to fit.
limit=finalizer.MAX_INPUT_CHARS
with patch.object(finalizer,'MAX_INPUT_CHARS',100_000_000):
packed=finalizer.packed_evidence(docs)
self.assertEqual(unpack(packed),docs)
encoded=json.dumps(packed,ensure_ascii=False,separators=(',',':'))
if len(encoded)>limit:
with self.assertRaises(finalizer.FinalizationUnavailable):finalizer.packed_evidence(docs)
else:self.assertEqual(unpack(finalizer.packed_evidence(docs)),docs)
# This captured run predates paired black/bright defect reads for each
# highlighted product, so it is now correctly labelled incomplete.
self.assertFalse(finalizer.coverage_sufficient(docs,'分析质量变化'))
+65
View File
@@ -114,6 +114,71 @@ class ModelGateTests(unittest.TestCase):
self.assertNotIn('fake-sensitive-provider-details', body)
self.assertEqual(len(self.calls), 1)
def test_unadvertised_tool_calls_are_blocked_before_json_or_stream_execution(self):
from unittest.mock import Mock
data_tool={'type':'function','function':{'name':'mcp_call','parameters':{'type':'object'}}}
tool={'id':'call-blocked','type':'function','function':{'name':'clarification','arguments':'{}'}}
reply={'choices':[{'index':0,'message':{'role':'assistant','content':None,'tool_calls':[tool]},'finish_reason':'tool_calls'}]}
upstream=HeldOpenProviderResponse([json.dumps(reply).encode()]);upstream.headers={'Content-Type':'application/json'};upstream.release.set()
self.gate._opener=Mock();self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'tools':[data_tool]})
self.assertEqual(code,502);self.assertNotIn('clarification',body)
for newline in (True,False):
prefix=b'data: {"choices":[{"index":0,"delta":{"content":"Preserved text"}}]}\n\n'
bad=('data: '+json.dumps({'choices':[{'index':0,'delta':{'tool_calls':[tool]},'finish_reason':'tool_calls'}]})+ ('\n\n' if newline else '')).encode()
upstream=HeldOpenProviderResponse([prefix,bad[:19],bad[19:]]);upstream.release.set()
self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'stream':True,'tools':[data_tool]})
self.assertEqual(code,200);self.assertIn('Preserved text',body)
self.assertNotIn('clarification',body);self.assertIn('error',body)
def test_allowed_fragmented_tool_calls_keep_their_exact_wire_data(self):
from unittest.mock import Mock
rows=[{'choices':[{'index':0,'delta':{'tool_calls':[{'index':0,'id':'q','type':'function','function':{'name':'mcp_','arguments':''}}]}}]},
{'choices':[{'index':0,'delta':{'tool_calls':[{'index':0,'function':{'name':'call','arguments':'{}'}}]},'finish_reason':'tool_calls'}]}]
wire=b''.join(('data: '+json.dumps(row)+'\n\n').encode() for row in rows)+b'data: [DONE]\n\n'
upstream=HeldOpenProviderResponse([wire[:23],wire[23:71],wire[71:]]);upstream.release.set()
self.gate._opener=Mock();self.gate._opener.open.return_value=upstream
code,body=self.request(extra={'stream':True,'tools':[{'type':'function','function':{'name':'mcp_call'}}]})
self.assertEqual(code,200);self.assertEqual(body.encode(),wire)
def test_follow_up_tool_cannot_end_analysis_before_the_model_writes_an_answer(self):
data_tool = {'type': 'function', 'function': {'name': 'mcp_call', 'parameters': {'type': 'object'}}}
follow_up = {'type': 'function', 'function': {'name': 'suggest_follow_ups', 'parameters': {'type': 'object'}}}
for choice in ('auto', {'type': 'function', 'function': {'name': 'suggest_follow_ups'}}):
with self.subTest(choice=choice):
self.assertEqual(self.request(extra={'tools': [data_tool, follow_up], 'tool_choice': choice})[0], 200)
self.assertEqual(self.calls[-1]['tools'], [data_tool])
if isinstance(choice, dict):
self.assertNotIn('tool_choice', self.calls[-1])
self.assertEqual(self.request(extra={'tools': [follow_up], 'tool_choice': 'required'})[0], 200)
self.assertNotIn('tools', self.calls[-1])
self.assertNotIn('tool_choice', self.calls[-1])
def test_only_supported_tools_reach_the_native_model_in_each_execution_mode(self):
helpers = ['read', 'list', 'grep', 'search', 'mcp_call', 'mcp_connect']
incompatible = ['clarification', 'request_warehouse_selection', 'request_warehouse_credentials',
'execute_sql', 'execute_semantic_query', 'read_query_result', 'display_chart', 'display_map',
'write', 'str_replace', 'execute_python', 'execute_sandboxed_code', 'task',
'onboarding_command', 'onboarding_progress', 'generate_onboarding_rules',
'suggest_follow_ups', 'future_unknown_tool']
tools = [{'type': 'function', 'function': {'name': n, 'parameters': {'type': 'object'}}}
for n in helpers + ['story'] + incompatible]
for native in (False, True):
self.gate.native_agent = native
self.assertEqual(self.request(extra={'tools': tools})[0], 200)
self.assertEqual([t['function']['name'] for t in self.calls[-1]['tools']],
helpers + ([] if native else ['story']))
def test_forced_unavailable_tool_cannot_survive_even_without_a_tool_definition(self):
for extra in ({'tool_choice': {'type': 'function', 'function': {'name': 'clarification'}}},
{'tool_choice': {'type': 'function', 'function': 'malformed'}},
{'tools': [], 'tool_choice': 'required'},
{'tools': [{'type': 'function', 'function': {'name': 'read'}}],
'tool_choice': {'type': 'function', 'function': {'name': 'execute_sql'}}}):
self.assertEqual(self.request(extra=extra)[0], 200)
self.assertNotIn('tool_choice', self.calls[-1])
def test_previous_turn_background_call_cannot_enter_current_thinking_or_provider(self):
marker='quality-turn:current-request'
self.gate._native_turn_marker=marker
+69
View File
@@ -216,6 +216,53 @@ class RuntimeStreamTests(unittest.TestCase):
truncated.feed({"type": "finish", "finishReason": "length"})
self.assertEqual(truncated.status(), ("PARTIAL", "length"))
def test_tool_errors_are_read_from_envelopes_not_business_data_cells(self):
for flag in ('mcpValidationError','mcpAuthRequired'):
stream=runtime.ConversationAccumulator()
stream.feed({'type':'tool-input-available','toolName':'mcp_call','toolCallId':'q'})
stream.feed({'type':'tool-output-available','toolCallId':'q','output':{flag:True,'server':'quality'}})
stream.feed({'type':'text-delta','id':'final','delta':'尚未查到数据。'})
stream.feed({'type':'finish','finishReason':'stop'})
self.assertEqual(stream.tool_calls[0]['status'],'failed')
self.assertEqual(stream.status(),('PARTIAL','mcp_error'))
payload={'kind':'quality_records','rows':[{'error':'缺角','isError':True}], 'note':'{"error":"原始单元格内容"}'}
stream=runtime.ConversationAccumulator()
stream.feed({'type':'tool-input-available','toolName':'mcp_call','toolCallId':'q'})
stream.feed({'type':'tool-output-available','toolCallId':'q','output':{'content':[{'type':'text','text':json.dumps(payload)}]}})
stream.feed({'type':'text-delta','id':'final','delta':'已查询并保留原始字段。'})
stream.feed({'type':'finish','finishReason':'stop'})
self.assertEqual(stream.tool_calls[0]['status'],'completed')
self.assertEqual(stream.status(),('COMPLETED','stop'))
def test_transport_done_is_not_a_normal_model_finish(self):
stream = runtime.ConversationAccumulator()
stream.feed({'type': 'text-delta', 'id': 'progress', 'delta': '先查一下。'})
stream.feed({'type': 'tool-input-available', 'toolName': 'mcp_call', 'toolCallId': 'q'})
stream.feed({'type': 'tool-output-available', 'toolCallId': 'q', 'output': {}})
stream.feed({'type': 'finish', 'finishReason': 'tool-calls'})
stream.feed({'type': '[DONE]'})
self.assertNotEqual(stream.status()[0], 'COMPLETED')
self.assertEqual(stream.answer, '先查一下。')
def test_progress_before_a_tool_or_unsettled_tool_cannot_count_as_final_answer(self):
for settled in (False, True):
stream = runtime.ConversationAccumulator()
stream.feed({'type': 'text-delta', 'id': 'progress', 'delta': '正在查询。'})
stream.feed({'type': 'tool-input-available', 'toolName': 'mcp_call', 'toolCallId': 'q'})
if settled:stream.feed({'type': 'tool-output-available', 'toolCallId': 'q', 'output': {}})
stream.feed({'type': 'finish', 'finishReason': 'stop'})
self.assertNotEqual(stream.status()[0], 'COMPLETED')
stream.feed({'type': 'text-delta', 'id': 'final', 'delta': '已核实的最终回答。'})
self.assertEqual(stream.status()[0], 'COMPLETED' if settled else 'PARTIAL')
def test_only_the_terminal_finish_and_successful_stop_control_can_complete(self):
for reasons, stopped in ((['stop', 'tool-calls'], None), (['stop'], False)):
stream = runtime.ConversationAccumulator()
stream.feed({'type': 'text-delta', 'id': 'answer', 'delta': '保留原文'})
for reason in reasons:stream.feed({'type': 'finish', 'finishReason': reason})
stream.feed({'type': '[DONE]'})
self.assertNotEqual(stream.status(stop_succeeded=stopped)[0], 'COMPLETED')
def test_native_stop_controller_stops_before_closing_stream_on_cancel(self):
calls = []
response = FakeResponse()
@@ -306,6 +353,28 @@ class RuntimeStreamTests(unittest.TestCase):
self.assertEqual(result[2], "transport_or_stream_error")
native_stop.assert_called_once_with(unittest.mock.ANY, "http://127.0.0.1:10000", "existing-native-id")
def test_an_already_finished_native_reply_cannot_be_cancelled_by_stale_no_progress_state(self):
controls=[];ready=[];controller=runtime.NativeStopController
def capture(*args):
value=controller(*args);controls.append(value);return value
class Response:
def __enter__(self):return self
def __exit__(self,*_):pass
def close(self):pass
def __iter__(self):
yield b'data: {"type":"text-delta","id":"final","delta":"final reply"}\n'
yield b'data: {"type":"finish","finishReason":"stop"}\n'
ready.append(controls[0].completion_ready())
yield b'data: [DONE]\n'
with tempfile.TemporaryDirectory() as temp:
root=Path(temp)/str(uuid.uuid4());root.mkdir();actor=root.name;sid=str(uuid.uuid4())
runtime._atomic_json(root/'private/native-exploration-state.json',{'executionId':'e','halted':True})
client=unittest.mock.Mock();client.open.return_value=Response()
with patch.object(runtime,'NativeStopController',side_effect=capture),patch('nao_agent_task.task_from_context',return_value={'executionMode':'native_agent','freshQuery':True}):
result=runtime._sse_request(client=client,base_url='http://native.invalid',model='fixture',question='q',release_id=str(uuid.uuid4()),
context={'_nativeExecutionId':'e'},chat_id='chat',session_state=runtime.SessionState(root,actor),session_id=sid,actor_root=root,emit=lambda _:None,timeout=5)
self.assertEqual(ready,[False]);self.assertEqual(result[1],'COMPLETED')
def test_story_export_uses_last_successful_call_and_matching_markdown(self):
class StoryClient:
def __init__(self):
+12
View File
@@ -52,6 +52,18 @@ def document(ref, *proofs):
class NativeRunTests(unittest.TestCase):
def test_pretool_progress_cannot_be_promoted_by_a_stop_finish(self):
from nao_quality_runtime import ConversationAccumulator
self.accumulator=ConversationAccumulator()
for event in ({'type':'text-delta','id':'progress','delta':'正在查询。'},
{'type':'tool-input-available','toolName':'mcp_call','toolCallId':'q'},
{'type':'tool-output-available','toolCallId':'q','output':{}},
{'type':'finish','finishReason':'stop'}):self.accumulator.feed(event)
result=self.invoke()
self.assertEqual(result['status'],'PARTIAL')
self.assertFalse(result['answerQuality']['narrativeComplete'])
self.assertEqual(result['answer'],'正在查询。')
def test_optional_scope_notice_is_shown_once_without_internal_visual_diagnostics(self):
child = {**self.task, 'stages': ['bright']}
self.snapshot['scopes'].append({'nodeId': 'bright-detail', 'scopeId': 'bright-detail',
+4 -2
View File
@@ -50,7 +50,7 @@ import { AnalysisReportExport, NativeAnalysisReport } from './AnalysisReportExpo
import { AiMarkdown } from './AiMarkdown';
import { authorizeAnalysisSnapshot } from './analysisSnapshot';
import { AnalysisApiSettings } from './AnalysisApiSettings';
import { acceptAnalysisEvent, analysisScrollFollows, analysisStreamStatus, partialAnswerIsOnlyProgress, type AnalysisScrollIntent } from './analysisConversationState';
import { acceptAnalysisEvent, analysisScrollFollows, analysisStreamStatus, emptyAnalysisNotice, partialAnswerIsOnlyProgress, type AnalysisScrollIntent } from './analysisConversationState';
import { AnalysisProcess } from './AnalysisProcess';
import { AnalysisMemberListPanel } from './AnalysisMemberList';
import { AnalysisRecordListPanel } from './AnalysisRecordList';
@@ -1806,6 +1806,7 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
const recordList = result.recordList ?? turn.recordList;
const answer = partialAnswerIsOnlyProgress(turn) ? '' : typeof result.answer === 'string' ? result.answer : typeof turn.answer === 'string' ? turn.answer : '';
const status = String(turn.status || result.status || '').toUpperCase();
const emptyNotice = emptyAnalysisNotice(turn, answer);
const visuals = result.visualSchemaVersion === 1 ? result.visuals : undefined;
const reportMarkdown = typeof result.reportMarkdown === 'string' ? result.reportMarkdown : typeof turn.reportMarkdown === 'string' ? turn.reportMarkdown : '';
const reportHtml = typeof result.reportHtml === 'string' ? result.reportHtml : typeof turn.reportHtml === 'string' ? turn.reportHtml : undefined;
@@ -1817,7 +1818,8 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
{status === 'NEEDS_CLARIFICATION' && onClarify ? <AnalysisClarification turn={turn} disabled={retryDisabled} onContinue={onClarify} /> : answer ? <>
{resumable ? <AiMarkdown content={answer} className="ai-answer" />
: <AnalysisNarrative answer={answer} visuals={visuals} visualState={result.visualState} question={question} turnId={String(turn.id || '')} beforeExport={() => authorizeAnalysisSnapshot(turn)} turn={turn} />}
</> : null}
</> : emptyNotice ? <p role="status">{emptyNotice}</p> : null}
{recovery.error && <p role="alert">{recovery.error}</p>}
<div className="analysis-message-actions">
{(reportMarkdown.trim() || reportHtml?.trim()) && <NativeAnalysisReport turn={turn} reportMarkdown={reportMarkdown} reportHtml={reportHtml} />}
{!reportMarkdown.trim() && !reportHtml?.trim() && answer && ['COMPLETED', 'PARTIAL'].includes(status) && <AnalysisReportExport turn={turn} />}
+1 -1
View File
@@ -1305,7 +1305,7 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
xAxis: { type: 'category', data: labels, triggerEvent: true, axisLine: { lineStyle: { color: line } }, axisLabel: { color: ink, fontFamily, fontSize, margin: 12 }, axisTick: { show: false } },
};
let option: echarts.EChartsCoreOption;
const pointStyle = { z: 10, symbol: 'circle', symbolSize: 5, showSymbol: true, emphasis: { focus: 'none' as const, scale: 1.35 }, connectNulls: false };
const pointStyle = { smooth: 0.15, smoothMonotone: 'x' as const, z: 10, symbol: 'circle', symbolSize: 5, showSymbol: true, emphasis: { focus: 'none' as const, scale: 1.35 }, connectNulls: false };
if (mode === 'production') {
const rates = [...rows.flatMap(row => [visibleCompositeRate(row, rateBasis), visibleStageRate(row, 'black'), visibleStageRate(row, 'bright', rateBasis)]).filter((value): value is number => value != null).map(value => value * 100), ...(targetRate == null ? [] : [targetRate])];
const minRate = rates.length ? Math.max(0, Math.floor(Math.min(...rates) - 2)) : 0;
+14
View File
@@ -102,6 +102,20 @@ export function partialAnalysisNotice(reason: unknown, result: Record<string, un
return '本轮分析未完整结束,已保存现有结果。可以重试或继续追问。';
}
/** An ended turn without a visible answer still needs an explanation beside retry. */
export function emptyAnalysisNotice(turn: Record<string, unknown>, displayedAnswer: string): string {
const result = turn.result && typeof turn.result === 'object' && !Array.isArray(turn.result)
? turn.result as Record<string, unknown> : turn;
const status = String(turn.status || result.status || '').toUpperCase();
if (displayedAnswer.trim() || !['PARTIAL', 'FAILED', 'CANCELLED', 'COMPLETED'].includes(status)) return '';
if ([result.memberList, turn.memberList, result.recordList, turn.recordList].some(value => value !== undefined && value !== null)
|| [result.reportMarkdown, turn.reportMarkdown, result.reportHtml, turn.reportHtml].some(value => typeof value === 'string' && value.trim())) return '';
if (status === 'CANCELLED') return '分析已停止,尚未生成回答。';
if (status === 'COMPLETED') return '此记录没有可显示的回答,请新建会话重试。';
const quality = result.answerQuality && typeof result.answerQuality === 'object' ? result.answerQuality as Record<string, unknown> : {};
return quality.dataComplete === true ? '数据查询已完成,但未生成分析正文。请重新分析。' : '本轮未生成可显示的回答,请重新分析。';
}
/** Old answers keep their original wording; surface their explicitly recorded scope limitation. */
export function hasUnreconciledEvidence(turn: Record<string, unknown>): boolean {
const record = (value: unknown): Record<string, unknown> => value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
+3 -1
View File
@@ -12,7 +12,9 @@ export function canRecoverAnalysisVisuals(turn: Row): boolean {
const planned=Array.isArray(task.queryPlan)&&task.queryPlan.some(raw=>{
const node=object(raw);return ['ranking','trend','defects'].includes(String(node.purpose))&&node.visualPreference!=='none';
});
const requested=task.trendRequired===true||task.visualPreference==='required'||Boolean(task.visualType)||planned;
const gaps=object(result.executionCoverage).visualGaps;
const nativeGap=task.executionMode==='native_agent'&&(['failed','unavailable'].includes(state)||Array.isArray(gaps)&&gaps.length>0);
const requested=task.trendRequired===true||task.visualPreference==='required'||Boolean(task.visualType)||planned||nativeGap;
return plan.protocolVersion===1&&['metrics','analysis'].includes(String(task.kind))
&&['COMPLETED','PARTIAL'].includes(status)&&task.visualPreference!=='none'&&requested
&&object(result.visualRecovery).complete!==true&&(state!=='ready'||status==='PARTIAL');
+2 -1
View File
@@ -892,7 +892,8 @@ export function buildAnalysisVisualOption(visual: AnalysisVisual, theme: VisualC
}),
itemStyle: { color: colors[seriesIndex % colors.length] },
connectNulls: false,
smooth: false,
smooth: ['line', 'area'].includes(visual.type) ? 0.15 : false,
smoothMonotone: 'x',
...(['bar', 'column'].includes(visual.type) ? {
barMaxWidth: combinedDefects ? 18 : 32,
...(combinedDefects ? { stack: 'combined-defect-stage', barWidth: 18 } : {}),
@@ -0,0 +1,28 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { emptyAnalysisNotice } from '../src/analysisConversationState.ts';
test('the reported empty native answer explains the missing body without claiming analysis success', () => {
const result = { answer: '', answerQuality: { dataComplete: true, narrativeComplete: false } };
assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', result }, ''), '数据查询已完成,但未生成分析正文。请重新分析。');
assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', ...result }, ''), '数据查询已完成,但未生成分析正文。请重新分析。');
});
test('running, successful, clarification and nonempty answers retain their existing display', () => {
for (const status of ['QUEUED', 'RUNNING', 'NEEDS_CLARIFICATION']) assert.equal(emptyAnalysisNotice({ status }, ''), '');
for (const status of ['PARTIAL', 'FAILED', 'CANCELLED']) assert.equal(emptyAnalysisNotice({ status }, '原文'), '');
});
test('a historical empty completed record is visible without pretending its completed turn can be retried',()=>{
assert.equal(emptyAnalysisNotice({status:'COMPLETED'},''),'此记录没有可显示的回答,请新建会话重试。');
assert.equal(emptyAnalysisNotice({status:'COMPLETED'},'已保存原文'),'');
});
test('failure, incomplete data and cancellation receive accurate, bounded notices', () => {
assert.equal(emptyAnalysisNotice({ status: 'FAILED', stopReason: 'private diagnostics' }, ''), '本轮未生成可显示的回答,请重新分析。');
assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', answerQuality: { dataComplete: false } }, ''), '本轮未生成可显示的回答,请重新分析。');
assert.equal(emptyAnalysisNotice({ status: 'CANCELLED' }, ''), '分析已停止,尚未生成回答。');
});
test('structured lists and reports do not need an additional prose answer', () => {
for (const [key, value] of [['memberList', {}], ['recordList', []], ['reportMarkdown', '报告'], ['reportHtml', '<p>报告</p>']]) {
assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', result: { [key]: value } }, ''), '');
assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', [key]: value }, ''), '');
}
});
@@ -4,6 +4,7 @@ import fs from 'node:fs';
import ts from 'typescript';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { emptyAnalysisNotice } from '../src/analysisConversationState.ts';
import {
analysisMemberListFullyRead,
analysisMemberListPage,
@@ -113,12 +114,17 @@ test('member-list display is opt-in, keeping earlier Markdown answers on their e
const source = fs.readFileSync(new URL('../src/ManagementPages.tsx', import.meta.url), 'utf8');
const turn = source.slice(source.indexOf('function AnalysisTurn('), source.indexOf('function AnalysisTurnMetadata('))
.replace('function AnalysisTurn(', 'function AnalysisTurn(');
const component = new Function('React', 'useAnalysisVisualRecovery', 'partialAnswerIsOnlyProgress', 'analysisScopeText', 'isObjectRecord', 'AnalysisProcess', 'AnalysisTurnMetadata', 'AiMarkdown', 'AnalysisNarrative', 'NativeAnalysisReport', 'AnalysisReportExport', 'Button', 'AnalysisMemberListPanel',
const component = new Function('React', 'useAnalysisVisualRecovery', 'partialAnswerIsOnlyProgress', 'analysisScopeText', 'isObjectRecord', 'AnalysisProcess', 'AnalysisTurnMetadata', 'AiMarkdown', 'AnalysisNarrative', 'NativeAnalysisReport', 'AnalysisReportExport', 'Button', 'AnalysisMemberListPanel', 'emptyAnalysisNotice',
ts.transpileModule(`${turn}\nreturn AnalysisTurn;`, { fileName: 'analysis-turn.tsx', compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React } }).outputText
)(React, turn => ({turn,available:false,recovering:false,error:''}), () => false, () => '', value => Boolean(value) && typeof value === 'object' && !Array.isArray(value), () => null, () => null, ({ content }) => React.createElement('p', null, content),
({ answer }) => React.createElement('div', { className: 'legacy-markdown' }, answer), () => null, () => null, ({ children }) => React.createElement('button', null, children), () => null);
)(React, turn => ({turn,available:false,recovering:false,error:turn.recoveryError||''}), () => false, () => '', value => Boolean(value) && typeof value === 'object' && !Array.isArray(value), () => null, () => null, ({ content }) => React.createElement('p', null, content),
({ answer }) => React.createElement('div', { className: 'legacy-markdown' }, answer), () => null, () => null, ({ children }) => React.createElement('button', null, children), () => null, emptyAnalysisNotice);
const html = renderToStaticMarkup(React.createElement(component, { turn: { id: 'old-turn', status: 'COMPLETED', answer: '| old | Markdown table |' }, onUnauthorized: () => false }));
assert.match(html, /legacy-markdown/);
assert.match(html, /\| old \| Markdown table \|/);
assert.doesNotMatch(html, /analysis-member-list/);
const empty = renderToStaticMarkup(React.createElement(component, { turn: { id: 'empty-turn', status: 'PARTIAL', answer: '', answerQuality: { dataComplete: true, narrativeComplete: false } }, onUnauthorized: () => false, onRetry: () => {} }));
assert.match(empty, /role="status">数据查询已完成,但未生成分析正文。请重新分析。/);
assert.match(empty, /<button>重新分析<\/button>/);
const failedRecovery=renderToStaticMarkup(React.createElement(component,{turn:{id:'old-turn',status:'PARTIAL',answer:'原答案',recoveryError:'图表恢复暂不可用'},onUnauthorized:()=>false}));
assert.match(failedRecovery,/role="alert">图表恢复暂不可用/);assert.match(failedRecovery,/原答案/);
});
@@ -22,6 +22,12 @@ function fixture(request){let cursor=0,dirty=false,queue=[],source=saved(),value
const snapshot=new Function('api',snapshotBody+';return {analysisSnapshotResult};')({});
const canRecover=new Function('analysisSnapshotResult',hookBody.slice(0,hookBody.indexOf('/**'))+';return canRecoverAnalysisVisuals;')(snapshot.analysisSnapshotResult);
test('only a saved protocol task requesting a chart offers recovery',()=>{assert.equal(canRecover(saved()),true);for(const source of [{...saved(),status:'RUNNING'},{...saved(),plan:{...saved().plan,protocolVersion:0}},{...saved(),plan:{...saved().plan,task:{...saved().plan.task,visualPreference:'none'}}},{...complete(saved())}])assert.equal(canRecover(source),false);});
test('native auto charts offer recovery for real rendering gaps without requiring a queryPlan',()=>{
const source=saved();source.plan.task={kind:'analysis',executionMode:'native_agent',visualPreference:'auto'};
source.executionCoverage={visualGaps:['总体比较图未生成']};source.visualState='ready';assert.equal(canRecover(source),true);
source.executionCoverage={visualGaps:[]};source.answerQuality={chartsComplete:false};source.status='COMPLETED';assert.equal(canRecover(source),false);
source.visualState='failed';assert.equal(canRecover(source),true);
});
test('a duplicate click recovers only charts and retains facts',async()=>{const response=deferred(),ui=fixture(()=>response.promise);ui.render();const a=ui.value.recover(),b=ui.value.recover();ui.render();assert.equal(ui.calls.api,1);response.resolve(complete(ui.source));await Promise.all([a,b]);await ui.settle();assert.equal(ui.value.turn.status,'COMPLETED');assert.equal(ui.value.turn.answer,'原答案');assert.equal(ui.value.available,false);ui.unmount();});
test('a changed fact, source execution, or returned turn is rejected',async()=>{for(const change of [v=>({...v,answer:'换了答案'}),v=>({...v,id:'turn2'}),v=>({...v,plan:{...v.plan,executionId:'exec2'}})]){let ui;ui=fixture(async()=>change(complete(ui.source)));ui.render();await ui.value.recover();await ui.settle();assert.equal(ui.value.turn.status,'PARTIAL');assert.match(ui.value.error,/原分析已变化/);ui.unmount();}});
test('late recovery cannot overwrite a changed revision or an unmounted view',async()=>{const response=deferred(),ui=fixture(()=>response.promise);ui.render();const before=structuredClone(ui.source),request=ui.value.recover();ui.replace({...before,plan:{...before.plan,revision:3},answer:'新答案'});response.resolve(complete(before));await request;await ui.settle();assert.equal(ui.value.turn.answer,'新答案');assert.equal(ui.value.turn.status,'PARTIAL');ui.unmount();});