diff --git a/ai/nao-quality/agent/prompts/system-native.md b/ai/nao-quality/agent/prompts/system-native.md index 128decd..3c6443d 100644 --- a/ai/nao-quality/agent/prompts/system-native.md +++ b/ai/nao-quality/agent/prompts/system-native.md @@ -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。工具支持的正式差值和阶段拆解可以直接引用;缺少的数据不得补零。统计关联不能宣称已证实设备、工艺、供应商或人员根因。 diff --git a/backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisProtocolService.java b/backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisProtocolService.java index d5a3405..cb8ba52 100644 --- a/backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisProtocolService.java +++ b/backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisProtocolService.java @@ -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();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"); } diff --git a/backend/src/main/java/cn/yunsheng/quality/analysis/NaoAnalysisRunner.java b/backend/src/main/java/cn/yunsheng/quality/analysis/NaoAnalysisRunner.java index 8c5971f..bb5d264 100644 --- a/backend/src/main/java/cn/yunsheng/quality/analysis/NaoAnalysisRunner.java +++ b/backend/src/main/java/cn/yunsheng/quality/analysis/NaoAnalysisRunner.java @@ -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(); + 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(); + 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); diff --git a/backend/src/test/java/cn/yunsheng/quality/analysis/AnalysisProtocolServiceMySqlTest.java b/backend/src/test/java/cn/yunsheng/quality/analysis/AnalysisProtocolServiceMySqlTest.java index d9e85ba..a5fb898 100644 --- a/backend/src/test/java/cn/yunsheng/quality/analysis/AnalysisProtocolServiceMySqlTest.java +++ b/backend/src/test/java/cn/yunsheng/quality/analysis/AnalysisProtocolServiceMySqlTest.java @@ -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; } diff --git a/backend/src/test/java/cn/yunsheng/quality/analysis/NaoAnalysisRunnerResultTest.java b/backend/src/test/java/cn/yunsheng/quality/analysis/NaoAnalysisRunnerResultTest.java index 57b65e6..e7a3436 100644 --- a/backend/src/test/java/cn/yunsheng/quality/analysis/NaoAnalysisRunnerResultTest.java +++ b/backend/src/test/java/cn/yunsheng/quality/analysis/NaoAnalysisRunnerResultTest.java @@ -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.of("finishReason","tool-calls"), + Map.of("nativeStopSucceeded",false), + Map.of("toolCalls",java.util.List.of(Map.of("tool","mcp_call","status","started"))), + Map.of("answerQuality",Map.of("narrativeComplete",false)))) { + var message=new java.util.LinkedHashMap();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( diff --git a/scripts/nao_agent_contract.py b/scripts/nao_agent_contract.py index ef9e8f4..74856df 100644 --- a/scripts/nao_agent_contract.py +++ b/scripts/nao_agent_contract.py @@ -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 diff --git a/scripts/nao_model_gate.py b/scripts/nao_model_gate.py index 34ebec3..9f340e5 100644 --- a/scripts/nao_model_gate.py +++ b/scripts/nao_model_gate.py @@ -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('模型授权已关闭') diff --git a/scripts/nao_native_run.py b/scripts/nao_native_run.py index 6b01809..253365b 100644 --- a/scripts/nao_native_run.py +++ b/scripts/nao_native_run.py @@ -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')) diff --git a/scripts/nao_quality_runtime.py b/scripts/nao_quality_runtime.py index e6a79a9..986c69a 100644 --- a/scripts/nao_quality_runtime.py +++ b/scripts/nao_quality_runtime.py @@ -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() diff --git a/verification/test_minimal_query_execution.py b/verification/test_minimal_query_execution.py index 9a88ef1..3e4a192 100644 --- a/verification/test_minimal_query_execution.py +++ b/verification/test_minimal_query_execution.py @@ -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)) diff --git a/verification/test_nao_evidence_finalizer.py b/verification/test_nao_evidence_finalizer.py index 329e896..e93a496 100644 --- a/verification/test_nao_evidence_finalizer.py +++ b/verification/test_nao_evidence_finalizer.py @@ -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,'分析质量变化')) diff --git a/verification/test_nao_model_gate.py b/verification/test_nao_model_gate.py index b5efb36..3d2f5ce 100644 --- a/verification/test_nao_model_gate.py +++ b/verification/test_nao_model_gate.py @@ -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 diff --git a/verification/test_nao_quality_runtime.py b/verification/test_nao_quality_runtime.py index d0b44c1..b22ce73 100644 --- a/verification/test_nao_quality_runtime.py +++ b/verification/test_nao_quality_runtime.py @@ -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): diff --git a/verification/test_native_run.py b/verification/test_native_run.py index 06203cb..208761d 100644 --- a/verification/test_native_run.py +++ b/verification/test_native_run.py @@ -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', diff --git a/web/src/ManagementPages.tsx b/web/src/ManagementPages.tsx index 4a288ab..ffe22d3 100644 --- a/web/src/ManagementPages.tsx +++ b/web/src/ManagementPages.tsx @@ -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 ? : answer ? <> {resumable ? : authorizeAnalysisSnapshot(turn)} turn={turn} />} - : null} + : emptyNotice ?

{emptyNotice}

: null} + {recovery.error &&

{recovery.error}

}
{(reportMarkdown.trim() || reportHtml?.trim()) && } {!reportMarkdown.trim() && !reportHtml?.trim() && answer && ['COMPLETED', 'PARTIAL'].includes(status) && } diff --git a/web/src/analysisConversationState.ts b/web/src/analysisConversationState.ts index 65c780e..c349e9a 100644 --- a/web/src/analysisConversationState.ts +++ b/web/src/analysisConversationState.ts @@ -102,6 +102,20 @@ export function partialAnalysisNotice(reason: unknown, result: Record, displayedAnswer: string): string { + const result = turn.result && typeof turn.result === 'object' && !Array.isArray(turn.result) + ? turn.result as Record : 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 : {}; + return quality.dataComplete === true ? '数据查询已完成,但未生成分析正文。请重新分析。' : '本轮未生成可显示的回答,请重新分析。'; +} + /** Old answers keep their original wording; surface their explicitly recorded scope limitation. */ export function hasUnreconciledEvidence(turn: Record): boolean { const record = (value: unknown): Record => value && typeof value === 'object' && !Array.isArray(value) ? value as Record : {}; diff --git a/web/src/analysisVisualRecovery.ts b/web/src/analysisVisualRecovery.ts index 6870f26..16c2e9e 100644 --- a/web/src/analysisVisualRecovery.ts +++ b/web/src/analysisVisualRecovery.ts @@ -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'); diff --git a/web/verification/analysis_empty_answer.test.mjs b/web/verification/analysis_empty_answer.test.mjs new file mode 100644 index 0000000..555122b --- /dev/null +++ b/web/verification/analysis_empty_answer.test.mjs @@ -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', '

报告

']]) { + assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', result: { [key]: value } }, ''), ''); + assert.equal(emptyAnalysisNotice({ status: 'PARTIAL', [key]: value }, ''), ''); + } +}); diff --git a/web/verification/analysis_member_list.test.mjs b/web/verification/analysis_member_list.test.mjs index fbbecb2..3035fe1 100644 --- a/web/verification/analysis_member_list.test.mjs +++ b/web/verification/analysis_member_list.test.mjs @@ -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, /