修复:完整展示AI原文并穿插图表,统一运行反馈与变化符号
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
# 质量数据分析平台设计规范
|
||||
|
||||
更新:2026-10-30
|
||||
更新:2026-11-02
|
||||
|
||||
## 智能分析正文与运行反馈(2026-11-02)
|
||||
|
||||
- 原生 AI 会话及报告以模型完整原文为正文,保留段落、表格和顺序,正式查询图表穿插在相关章节后;取消二次改写和统计兜底报告替换。历史兜底报告仅在保存的模型原文可恢复时恢复,原文缺失不伪造补齐。
|
||||
- 会话只显示一次最终回答,运行过程保留思考与工具步骤;运行中默认展开,可手动收起。发送立即显示等待状态和计时,收到服务器开始时间后显示运行计时和旋转图标。
|
||||
- 不在会话追加范围说明、进度句、错误或“部分完成”提示;真实失败、证据缺口和完成状态仍保存在后台,恢复及重试入口保留。
|
||||
- 合格率变化统一为本期减比较期:下降为负 pp,上升为正 pp。用于筛选和排序的降幅指标保持原有语义,转换仅作用于展示值;图表、数据表和导出一致。
|
||||
|
||||
## 已确认方向
|
||||
- 保持现有导航宽度、模块顺序、筛选承载、指标横条、表格及趋势位置。(用户确认)
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
比例转百分数乘100;两比例之差乘100才是“百分点”。例如0.96→0.95是下降1个百分点,不能先对原始比例差保留两位再换算。保留原始精度到最后展示一步。空值表示缺失/不适用,不等于零;新增/退出对象允许描述重量变化,但不补造其缺失期合格率。
|
||||
|
||||
合格率变化统一为“本期-比较期”:下降显示 -X.XX pp,上升显示 +X.XX pp,持平为 0.00 pp;可另外标注方向。rateDrop/rate_drop 是比较期减本期的降幅排序量,不能直接当作有符号变化;展示其变化时取反并按正式单位换算,排序顺序不变。优先引用工具已提供的正式变化值,不能从舍入后的两期率重新计算。
|
||||
|
||||
重量统一以正式kg展示。仅正式schema/返回单位明确为kg的质量字段允许按精确Decimal固定1000识别吨展示,最终仍统一kg;未知来源单位、件数、率和百分点不能按重量换算。严格比较阈值保留原始精确边界,不能用舍入值改变“低于/超过”等关系。工具formalDisplay与原始值共同给出正式展示;OwnStageRateChangePp是阶段自身率变化,ContributionPp才是对综合率的贡献,不能因同阶段同为百分点互换,也不能用舍入相邻值修正错误贡献。
|
||||
|
||||
“汽车交通”是“新能源汽车磁钢”产业部的业务别名,统一用 filters.department=新能源汽车磁钢;不能用国内和海外的 group 多选集合替代产业部范围,两者原始记录覆盖未必相同。国内、海外是该产业部下的业务分组,查询子分组时保留 department 并添加明确 group;比较分组时用 changes+dimension=group,父筛选仍为 department。总体指全公司。规格对应 product。期间、范围、基期、发布版本要一致。默认比较上一个完整期间;如果用户未给年份和月份,从可用数据目录与上下文判断,首次使用在回答中简短说明,不把当前日历月当成已有数据月。
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
优先使用工具formalDisplay的规范数值与单位,重量最终统一kg。阶段自身率变化与对综合率贡献按metricRole明确区分;不能把OwnStageRateChangePp当ContributionPp,也不能从舍入后的阶段变化凑总体等式。只有正式原单位明确kg的质量字段可按固定1000识别吨,数量和未知单位不换算,严格阈值不舍入。
|
||||
|
||||
合格率变化统一为“本期-比较期”:下降显示 -X.XX pp,上升显示 +X.XX pp,持平为 0.00 pp;可另外标注方向。rateDrop/rate_drop 是比较期减本期的降幅排序量,不能直接当作有符号变化;展示其变化时取反并按正式单位换算,排序顺序不变。优先引用工具已提供的正式变化值,不能从舍入后的两期率重新计算。
|
||||
|
||||
当前已确认 filters、period、comparisonPeriod 是唯一范围权威。历史问答只帮助理解指代,不能恢复已取消条件。每轮使用同一个内部releaseId但不向用户展示内部ID、版本锁定或执行协议。修改用户范围需要下一轮确认;调查内只允许保持父条件、沿授权目录缩小对象。
|
||||
|
||||
原因分析须先取得整个已确认范围的正式本期/比较期基线,再决定是否缩小范围;不能用某个子分组或单个规格的率代替整个问题范围。首次compare/query保持_agentTask.filters原样,不额外添加子对象。之后的方向和深度由真实返回的线索决定。
|
||||
|
||||
@@ -37,7 +37,7 @@ releaseId、发布版本号、候选发布状态、Story ID/slug/version 仅作
|
||||
整体变化先用简短摘要说清变化方向、主要阶段和优先核查线索,再展开各节。每节先写一到三句解释已确认的现象及实际含义,必要时附一张紧凑数据表,不只连续抄表、不在“重点关注”重复全部数字。阶段数学影响沿用 stageDecomposition 的服务端结果,不能把阶段自身率变化当作对综合率贡献,也不把数学拆解称现场生产根因。亮片报废下降而投入下降更多时,要明确解释合格率仍可能下降。
|
||||
|
||||
常规整体分析的重点规格优先结合报废增量、当前规模、率变化和分阶段有效投入确定核查优先级。单按合格率降幅返回的小批量、0率或阶段缺失规格,另列“极端率变化线索”,不能只因降幅大就称整体主要原因;两期分母未查清时不推断停产、退出或全批报废。用户明确只看降幅排名时保留真实排名与批量背景。不得自设未授权的最低批量阈值或忽略小批量的安全/质量异常。
|
||||
即使这些规格报废绝对重量较小,也不能据此断言其对总体综合率的影响“可忽略”或计算贡献;只能说明报废重量及投入规模,并把总体影响留待正式可加总口径核验。0率原因未核实前,不用“更可能是全批报废/覆盖问题”等概率判断,只写待核对的分母、批次和数据覆盖。没有已确认的批量阈值时,不给规格贴“批量有效/无代表性”的确定性标签,可具体写两期投入重量及其变化。合格率变化一律明确比较期→本期,并写“下降/上升X.XX个百分点”,避免只写带符号的pp。
|
||||
即使这些规格报废绝对重量较小,也不能据此断言其对总体综合率的影响“可忽略”或计算贡献;只能说明报废重量及投入规模,并把总体影响留待正式可加总口径核验。0率原因未核实前,不用“更可能是全批报废/覆盖问题”等概率判断,只写待核对的分母、批次和数据覆盖。没有已确认的批量阈值时,不给规格贴“批量有效/无代表性”的确定性标签,可具体写两期投入重量及其变化。合格率变化统一为“本期-比较期”:下降显示 -X.XX pp,上升显示 +X.XX pp,持平为 0.00 pp;可另外标注方向。rateDrop/rate_drop 是比较期减本期的降幅排序量,不能直接当作有符号变化;展示其变化时取反并按正式单位换算,排序顺序不变。优先引用工具已提供的正式变化值,不能从舍入后的两期率重新计算。
|
||||
|
||||
组织、规格和具体缺陷之间的关系只写实际交叉查询已经核对的联系;没有交叉证据时保持各自范围,不把两个独立排名拼成因果链。最后只列能执行的优先核查问题,分清已确认现象与待验证原因,不编造责任人、完成期限或闭环结果。所有图文继续复用同一轮证据;不要为了报告样式重复查询、重新计算核心指标或自动生成故事。
|
||||
|
||||
|
||||
@@ -415,7 +415,8 @@ public class AnalysisProtocolService {
|
||||
jdbc.update("UPDATE analysis_protocol_execution SET status='FAILED' WHERE execution_id=? AND status='RUNNING'",a.executionId());
|
||||
jdbc.update("UPDATE analysis_protocol_session SET status='FAILED',version=version+1 WHERE session_id=?",a.sessionId());
|
||||
jdbc.update("UPDATE analysis_protocol_attempt SET status='FAILED' WHERE attempt_id=?",id);
|
||||
Map<String,Object> answer=Map.of("answer",reason,"results",List.of(),"releaseId",a.releaseId(),"engine",a.input().containsKey("controlledInspection")?"CONTROLLED_INSPECTION_PROTOCOL":"NAO","completion",Map.of("completed",false,"reason",reason));
|
||||
boolean nativeConversation=autonomousSession(a.sessionId())&&!a.input().containsKey("controlledInspection");
|
||||
Map<String,Object> answer=Map.of("answer",nativeConversation?"":reason,"results",List.of(),"releaseId",a.releaseId(),"engine",a.input().containsKey("controlledInspection")?"CONTROLLED_INSPECTION_PROTOCOL":"NAO","completion",Map.of("completed",false,"reason",reason));
|
||||
jdbc.update("UPDATE analysis_turn SET status='FAILED',answer_json=CAST(? AS JSON),stop_reason=?,finished_at=CURRENT_TIMESTAMP(6) WHERE id=?",write(answer),reason,a.turnId());
|
||||
inspection.updateAssociation(a,"FAILED");
|
||||
event(id,"failed",Map.of("turnId",a.turnId(),"status","FAILED","result",answer,"stopReason",reason));
|
||||
@@ -628,12 +629,18 @@ public class AnalysisProtocolService {
|
||||
if(Set.of("COMPLETED","PARTIAL").contains(status)&&!"report".equals(task.get("kind"))
|
||||
&&!hasProtocolEvidence(details)) {
|
||||
status="FAILED";
|
||||
String original=AnalysisProtocolExploration.nativeAgent(task)?Objects.toString(answer.get("answer"),""):null;
|
||||
answer.clear();
|
||||
answer.put("answer",noProofFailureReason(details));
|
||||
answer.put("answer",original!=null?original:noProofFailureReason(details));
|
||||
answer.put("results",List.of());
|
||||
answer.put("releaseId",a.releaseId());
|
||||
answer.put("engine","NAO");
|
||||
answer.put("stopReason","formal_query_evidence_missing");
|
||||
if(original!=null) {
|
||||
answer.put("executionMode","native_agent");
|
||||
answer.put("answerQuality",Map.of("version",1,"dataComplete",false,"narrativeFactBinding","not_assessed",
|
||||
"missingEvidence",List.of(noProofFailureReason(details))));
|
||||
}
|
||||
} else if(Set.of("COMPLETED","PARTIAL").contains(status)) {
|
||||
governance.requireBoundAction("QUERY","query.aggregate");
|
||||
if(AnalysisProtocolInspection.KIND.equals(task.get("kind"))) {
|
||||
@@ -672,7 +679,7 @@ public class AnalysisProtocolService {
|
||||
answer.put("answerQuality",quality);
|
||||
}
|
||||
if(AnalysisProtocolExploration.nativeAgent(task)){for(String key:List.of("scopeCoverage","explorationWarnings","conclusionProtocolQueryIds","attemptProtocolQueryIds"))if(checked.containsKey(key))answer.put(key,checked.get(key));}
|
||||
if(Boolean.TRUE.equals(task.get("verifySource"))||Boolean.TRUE.equals(task.get("fullDetails"))) {
|
||||
if(!AnalysisProtocolExploration.nativeAgent(task)&&(Boolean.TRUE.equals(task.get("verifySource"))||Boolean.TRUE.equals(task.get("fullDetails")))) {
|
||||
for(String field:List.of("reportMarkdown","reportHtml","reportTitle"))answer.remove(field);
|
||||
String rawStatus=rawStatusText(checked,map(recordList));
|
||||
if(planResolution(a,task,actor).scopes().stream().allMatch(AnalysisProtocolService::rawScope))answer.put("answer",rawStatus);
|
||||
@@ -680,7 +687,7 @@ public class AnalysisProtocolService {
|
||||
}
|
||||
if(!Boolean.TRUE.equals(checked.get("complete"))) {
|
||||
status="PARTIAL";answer.put("stopReason","evidence_incomplete");
|
||||
if(AnalysisProtocolExploration.nativeAgent(task))appendCoverageNotices(answer,list(checked.get("missingEvidence")));
|
||||
if(AnalysisProtocolExploration.nativeAgent(task))appendNativeCoverageDiagnostics(answer,list(checked.get("missingEvidence")));
|
||||
}
|
||||
} else {
|
||||
validateMetricEvidence(a,task,details);
|
||||
@@ -801,6 +808,15 @@ public class AnalysisProtocolService {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/** Native model prose is immutable; coverage gaps belong to the saved execution metadata. */
|
||||
static void appendNativeCoverageDiagnostics(Map<String,Object> answer,List<?> missing) {
|
||||
var gaps=new LinkedHashSet<String>();
|
||||
var quality=new LinkedHashMap<>(map(answer.get("answerQuality")));
|
||||
for(Object raw:list(quality.get("missingEvidence")))if(raw instanceof String gap&&!gap.isBlank())gaps.add(gap);
|
||||
for(Object raw:missing)if(raw instanceof String gap&&!gap.isBlank())gaps.add(gap);
|
||||
if(gaps.isEmpty())return;
|
||||
quality.putIfAbsent("version",1);quality.put("missingEvidence",List.copyOf(gaps));answer.put("answerQuality",quality);
|
||||
}
|
||||
/** A server downgrade must remain visible even when the model omitted its own limitations. */
|
||||
private void appendCoverageNotices(Map<String,Object> answer,List<?> missing) {
|
||||
var gaps=missing.stream().filter(String.class::isInstance).map(String.class::cast).filter(s->!s.isBlank()).distinct().toList();
|
||||
|
||||
@@ -1626,6 +1626,15 @@ public class AnalysisTurnService {
|
||||
if(quality.get(field) instanceof Boolean value)safe.put(field,value);
|
||||
if(quality.get("missingEvidence") instanceof List<?> gaps)safe.put("missingEvidence",gaps.stream()
|
||||
.filter(value->value instanceof String gap&&!gap.isBlank()&&gap.length()<=1000).distinct().limit(20).toList());
|
||||
if("not_assessed".equals(quality.get("narrativeFactBinding")))safe.put("narrativeFactBinding","not_assessed");
|
||||
for(String field:List.of("citedEvidenceRefs","unresolvedEvidenceRefs"))if(quality.get(field) instanceof List<?> refs) {
|
||||
var visible=refs.stream().filter(value->value instanceof String ref&&ref.matches("Q[A-F0-9]{8}")
|
||||
&&(!field.equals("citedEvidenceRefs")||(answer.get("queryEvidenceRefs") instanceof List<?> allowed?allowed:List.of()).contains(ref))).distinct().limit(100).toList();
|
||||
safe.put(field,visible);
|
||||
if(field.equals("unresolvedEvidenceRefs"))safe.put("citationComplete",visible.isEmpty());
|
||||
}
|
||||
if(quality.get("notices") instanceof List<?> notices)safe.put("notices",notices.stream()
|
||||
.filter(value->value instanceof String notice&&!notice.isBlank()&¬ice.length()<=1000).distinct().limit(20).toList());
|
||||
answer.put("answerQuality",safe);
|
||||
}
|
||||
Object count = details.get("toolCount");
|
||||
|
||||
+20
-1
@@ -1134,6 +1134,25 @@ class AnalysisProtocolServiceMySqlTest {
|
||||
assertEquals(40,jdbc.queryForObject("SELECT COUNT(*) FROM analysis_protocol_query",Integer.class));
|
||||
} finally {pool.shutdownNow();}
|
||||
}
|
||||
@Test void nativeWithoutFormalEvidenceKeepsOriginalBodyButCannotPublishMetricsOrSucceed() throws Exception {
|
||||
enableAutonomous();String turn=newTurn("原生回答缺少正式证据");var a=claim(turn,false);
|
||||
var task=AnalysisProtocolService.map(tx.execute(s->service.confirm(a.attemptId(),nativeTask(turn,Map.of()),actor)).get("task"));
|
||||
String original="## 结论\n\n模型完整原文,不用平台错误句替换。";
|
||||
tx.executeWithoutResult(s->service.finish(a.attemptId(),"PARTIAL",Map.of("answer",original,"visuals",List.of(Map.of("id","unproved"))),Map.of("agentTask",task,"protocolEvidence",List.of()),actor));
|
||||
var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);
|
||||
assertEquals("FAILED",turnStatus(turn));assertEquals(original,saved.get("answer"));assertFalse(saved.containsKey("visuals"));
|
||||
assertEquals("native_agent",saved.get("executionMode"));
|
||||
assertEquals(false,AnalysisProtocolService.map(saved.get("answerQuality")).get("dataComplete"));
|
||||
assertEquals("formal_query_evidence_missing",saved.get("stopReason"));
|
||||
}
|
||||
@Test void nativePlanningFailureStoresReasonWithoutReplacingAnswerWithSystemError() throws Exception {
|
||||
enableAutonomous();String turn=newTurn("原生规划失败");var a=claim(turn,false);
|
||||
tx.executeWithoutResult(s->service.fail(a.attemptId(),"本轮分析未完成,请重试"));
|
||||
var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);
|
||||
assertEquals("FAILED",turnStatus(turn));assertEquals("",saved.get("answer"));
|
||||
assertEquals("本轮分析未完成,请重试",jdbc.queryForObject("SELECT stop_reason FROM analysis_turn WHERE id=?",String.class,turn));
|
||||
assertEquals(false,AnalysisProtocolService.map(saved.get("completion")).get("completed"));
|
||||
}
|
||||
@Test void autonomousReadBudgetAndFinishRequireRealRootEvidence() {
|
||||
enableAutonomous();String turn=newTurn("检查根任务证据");var a=claim(turn,false);var confirmed=AnalysisProtocolService.map(tx.execute(s->service.confirm(a.attemptId(),nativeTask(turn,Map.of()),actor)).get("task"));
|
||||
var registered=step(a,"read",Map.of("kind","base"),List.of());var scope=AnalysisProtocolService.map(registered.get("scope"));var p=scopeParams(scope);var proofs=new ArrayList<Map<String,Object>>();
|
||||
@@ -1273,7 +1292,7 @@ class AnalysisProtocolServiceMySqlTest {
|
||||
var result=answer();result.put("readingSummary",Map.of("version",1,"lead","已核实总体率","notices",List.of("已有提示")));
|
||||
tx.executeWithoutResult(s->service.finish(a.attemptId(),"COMPLETED",result,Map.of("agentTask",confirmed,"protocolEvidence",proofs,"factsComplete",true,"evidenceCompletion",Map.of("serverCoverageSufficient",true)),actor));
|
||||
assertEquals("PARTIAL",turnStatus(turn));var saved=AnalysisProtocolService.map(json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class));
|
||||
var notices=AnalysisProtocolService.list(AnalysisProtocolService.map(saved.get("readingSummary")).get("notices"));assertTrue(notices.contains("已有提示"));assertTrue(notices.containsAll(gaps));assertTrue(saved.get("answer").toString().contains("尚未解决:"));assertTrue(saved.get("answer").toString().contains("18X15X4+T"));
|
||||
var notices=AnalysisProtocolService.list(AnalysisProtocolService.map(saved.get("answerQuality")).get("missingEvidence"));assertTrue(notices.containsAll(gaps));assertEquals(result.get("answer"),saved.get("answer"));assertFalse(saved.get("answer").toString().contains("尚未解决:"));
|
||||
}
|
||||
}
|
||||
@Test void rejectedRegistrationDoesNotPolluteLaterSuccessfulScopeCoverage() {
|
||||
|
||||
@@ -37,6 +37,19 @@ class AnalysisTurnSnapshotTest {
|
||||
private static final String SESSION_ID = "66b51f0e-0252-4ef1-9aeb-58bd262293db";
|
||||
private static final String QUERY_FILTERS = "SELECT field_key,black_column,bright_column FROM query_filter_field WHERE enabled=TRUE";
|
||||
|
||||
@Test void originalBodyQualityMetadataSurvivesSavedSnapshotWithoutClaimingFactVerification() {
|
||||
Harness harness=new Harness("valid");
|
||||
var quality=Map.<String,Object>of("version",1,"dataComplete",true,"narrativeComplete",true,"chartsComplete",false,
|
||||
"narrativeFactBinding","not_assessed","citedEvidenceRefs",List.of("Q1234ABCD","Q00000000","bad"),
|
||||
"unresolvedEvidenceRefs",List.of("QDEADBEEF","bad"),"notices",List.of("图表尚未齐全"),"missingEvidence",List.of("未知引用"));
|
||||
var details=Map.<String,Object>of("answerQuality",quality,"queryEvidenceRefs",List.of("Q1234ABCD"));
|
||||
var answer=harness.service.nativeAnswer(details,"完整AI正文",Map.of(),"release-1","PARTIAL","evidence_incomplete",TURN_ID,null);
|
||||
var savedQuality=AnalysisProtocolService.map(answer.get("answerQuality"));
|
||||
assertEquals("not_assessed",savedQuality.get("narrativeFactBinding"));assertEquals(false,savedQuality.get("citationComplete"));
|
||||
assertEquals(List.of("Q1234ABCD"),savedQuality.get("citedEvidenceRefs"));assertEquals(List.of("QDEADBEEF"),savedQuality.get("unresolvedEvidenceRefs"));
|
||||
harness.storedOverride=answer;var snapshot=harness.service.getSnapshot(TURN_ID,harness.submittedActor);
|
||||
assertEquals(savedQuality,snapshot.get("answerQuality"));assertEquals("完整AI正文",snapshot.get("answer"));
|
||||
}
|
||||
@Test
|
||||
void snapshotReadUsesFreshAuthorizationAndDoesNotRequeryBusinessMetrics() {
|
||||
Harness harness = new Harness("valid");
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.yunsheng.quality.analysis;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
class NativeOriginalCoverageTest {
|
||||
@Test void nativeCoverageKeepsModelProseByteForByteAndStoresGapsSeparately() {
|
||||
String original="## 结论\n\n原始模型正文。\n\n| 变化 |\n| --- |\n| -0.30 pp |";
|
||||
var answer=new LinkedHashMap<String,Object>();answer.put("answer",original);
|
||||
answer.put("answerQuality",Map.of("version",1,"dataComplete",true,"missingEvidence",List.of("已有缺口")));
|
||||
AnalysisProtocolService.appendNativeCoverageDiagnostics(answer,List.of("图表未齐","图表未齐", ""));
|
||||
assertEquals(original,answer.get("answer"));
|
||||
var quality=AnalysisProtocolService.map(answer.get("answerQuality"));
|
||||
assertEquals(true,quality.get("dataComplete"));assertEquals(List.of("已有缺口","图表未齐"),quality.get("missingEvidence"));
|
||||
assertFalse(answer.containsKey("readingSummary"));
|
||||
}
|
||||
@Test void noGapsDoNotCreateOrRewriteNarrativeMetadata() {
|
||||
var answer=new LinkedHashMap<String,Object>();answer.put("answer","原文");
|
||||
AnalysisProtocolService.appendNativeCoverageDiagnostics(answer,List.of());
|
||||
assertEquals(Map.of("answer","原文"),answer);
|
||||
}
|
||||
}
|
||||
@@ -963,7 +963,7 @@ def _change_chart(document: dict[str, Any], result: dict[str, Any], turn_id: str
|
||||
note = "黑片与亮片两阶段报废重量之和仅作分组描述,不代表独立投入分母或总体变化贡献。"
|
||||
elif order == "rate_drop":
|
||||
title = "合格率变化排名" if stage is None else "分组合格率变化排名"
|
||||
metric, unit, label, field = "rate_drop_pp", "pp", "合格率下降(pp)", "rateDrop"
|
||||
metric, unit, label, field = "rate_drop_pp", "pp", "合格率变化(pp)", "rateDrop"
|
||||
required_fields = (["black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight"]
|
||||
if stage is None else [f"{stage}_good_weight", f"{stage}_total_weight"])
|
||||
|
||||
@@ -971,11 +971,14 @@ def _change_chart(document: dict[str, Any], result: dict[str, Any], turn_id: str
|
||||
raw, ok = _finite_number(row.get(field))
|
||||
if not ok:
|
||||
return None, False
|
||||
return (raw * 100 if raw is not None else None), True
|
||||
points = -raw * 100 if raw is not None else None
|
||||
if points is not None and not math.isfinite(points):
|
||||
return None, False
|
||||
return points, True
|
||||
|
||||
note = "正值表示合格率下降,负值表示改善;百分点取自正式分组率差。"
|
||||
note = "正值表示本期合格率上升,负值表示下降;变化值为本期减比较期,排序仍沿用正式率降排名。"
|
||||
if stage is None:
|
||||
note = "正值表示综合合格率下降,负值表示改善;按正式综合率比较,不作为生产根因。"
|
||||
note = "正值表示本期综合合格率上升,负值表示下降;变化值为本期减比较期,排序仍沿用正式率降排名,不代表生产根因。"
|
||||
elif order == 'current_rate':
|
||||
title = '分组本期合格率排名'
|
||||
metric = (stage+'_rate') if stage else 'composite_rate'
|
||||
@@ -1023,7 +1026,7 @@ def _change_chart(document: dict[str, Any], result: dict[str, Any], turn_id: str
|
||||
title = title.replace("分组", "产品")
|
||||
if order == "rate_drop":
|
||||
title = title.replace("合格率变化排名", "合格率降幅线索")
|
||||
note = "条形末端显示具体变化:正值为下降,负值为改善,单位是百分点。本图只按率降幅排序,未纳入投入规模;少量生产或投入骤变的规格不能仅凭此图认定为重点,需结合分阶段投入与报废增量核查。"
|
||||
note = "条形末端显示本期减比较期的变化:负值为下降,正值为上升,单位是百分点。本图仍按率降幅排序,未纳入投入规模;少量生产或投入骤变的规格不能仅凭此图认定为重点,需结合分阶段投入与报废增量核查。"
|
||||
elif result["dimension"] == "group":
|
||||
title = title.replace("分组", "业务分组")
|
||||
else:
|
||||
|
||||
+77
-43
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import re
|
||||
import time
|
||||
|
||||
|
||||
@@ -22,12 +23,17 @@ def exploration_notices(scopes, coverage):
|
||||
for kind,labels in grouped.items()]
|
||||
|
||||
|
||||
def _answer_evidence_refs(answer):
|
||||
"""Return only explicit query references present in the upstream answer."""
|
||||
return list(dict.fromkeys(re.findall(r'\bQ[A-F0-9]{8}\b', answer)))
|
||||
|
||||
|
||||
def run_native_exploration(*, request, task, authority, actor_root, delegated, connection,
|
||||
manager, state, cancel_event, emit, prior_evidence_files, phase_timings=None):
|
||||
from nao_quality_runtime import _atomic_json, _authenticate, _sse_request, _read_json
|
||||
from nao_agent_task import bind_task
|
||||
from nao_protocol_execution import _identity, _platform_visual_context, query_evidence_bindings
|
||||
from nao_evidence_finalizer import evidence_summaries, synthesize_native
|
||||
from nao_protocol_execution import _identity, query_evidence_bindings
|
||||
from nao_evidence_finalizer import evidence_summaries
|
||||
from analysis_visual_evidence import protocol_visual_result
|
||||
from nao_runtime_policy import native_timeout
|
||||
from nao_native_exploration import _proofs
|
||||
@@ -112,6 +118,8 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
|
||||
and not missing and not progress.get('halted'))
|
||||
if native_status != 'COMPLETED' and not corrected_tool_errors and not controlled_no_progress:
|
||||
missing.append('自主调查未正常结束(' + str(native_reason or native_status) + '),已保留核实结果。')
|
||||
elif native_status == 'COMPLETED' and not natural_finish:
|
||||
missing.append('自主调查未收到有效的正常结束信号。')
|
||||
if not documents:
|
||||
missing.append('本轮未取得当前执行的可核验查询证据。')
|
||||
context['_protocolScopes'] = {'scopes': scopes, 'pending': snapshot.get('pending', []), 'gaps': missing}
|
||||
@@ -119,24 +127,7 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
|
||||
visuals = protocol_visual_result(documents, scopes, request['question'], request['turnId'],
|
||||
release_id=request['releaseId'], base_context=context)
|
||||
timings['visuals'] = round((time.monotonic() - started) * 1000)
|
||||
context['_platformVisuals'] = _platform_visual_context(visuals)
|
||||
emit({'type': 'progress', 'message': '正在核对结论与图表…', 'executionMode': 'native_agent'})
|
||||
started = time.monotonic()
|
||||
final = synthesize_native(base_url=connection[0], api_key=connection[1], model=connection[2],
|
||||
question=request['question'], evidence=documents, cancel_event=cancel_event,
|
||||
context=context, native_answer=accumulator.answer)
|
||||
timings['synthesis'] = round((time.monotonic() - started) * 1000)
|
||||
_atomic_json(actor_root / 'private' / 'native-composition.json', {
|
||||
'turnId': request['turnId'], 'rawModelResponses': final.get('rawModelResponses', []),
|
||||
'compositionDiagnostics': final.get('compositionDiagnostics', {}),
|
||||
'bindingInput': final.get('_privateBindingInput'),
|
||||
'titleMetadata': {key: final.get(key) for key in
|
||||
('reportTitle', 'titleGeneratedByModel', 'titleValid', 'titleSource')},
|
||||
'readingSummary': final.get('readingSummary')})
|
||||
full_facts = final.get('fullEvidenceFacts', [])
|
||||
if full_facts:
|
||||
_atomic_json(actor_root / 'private' / 'native-full-evidence-facts.json', full_facts)
|
||||
missing.extend(final.get('missingEvidence', []))
|
||||
emit({'type': 'progress', 'message': '正在生成正式图表并整理证据状态…', 'executionMode': 'native_agent'})
|
||||
visual_warnings = list(visuals.get('missingEvidence', []))
|
||||
if task.get('visualPreference') == 'required':
|
||||
missing.extend(visual_warnings)
|
||||
@@ -144,41 +135,84 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
|
||||
if visual_warnings and not summary_warnings:
|
||||
summary_warnings = ['部分重点图表未能完整展示;已保留核实文字和表格。']
|
||||
missing = list(dict.fromkeys(str(item) for item in missing if item))
|
||||
complete = bool(documents and final.get('evidenceSufficient') and not missing)
|
||||
used_refs = set(final.get('usedEvidenceRefs', []))
|
||||
cited_ids = list(dict.fromkeys(p['queryId'] for doc in documents if doc.get('evidenceRef') in used_refs
|
||||
for p in _proofs(doc.get('result', {}))))
|
||||
if final.get('evidenceSufficient') and not cited_ids:
|
||||
missing.append('解释未关联当前执行的实际查询证明。')
|
||||
complete = False
|
||||
answer = accumulator.answer if isinstance(accumulator.answer, str) else ''
|
||||
original_refs = _answer_evidence_refs(answer)
|
||||
complete_receipt_ids = {p.get('queryId') for p in receipts if isinstance(p, dict)
|
||||
and p.get('executionId') == identity['executionId'] and isinstance(p.get('queryId'), str)
|
||||
and p.get('queryId') in accepted_ids
|
||||
and complete_scopes is not None and (p.get('nodeId'), p.get('scopeId')) in complete_scopes}
|
||||
verified_document_ids = {}
|
||||
for doc in documents:
|
||||
proofs = _proofs(doc.get('result', {})) if isinstance(doc, dict) else []
|
||||
proof_ids = list(dict.fromkeys(p.get('queryId') for p in proofs
|
||||
if isinstance(p, dict) and isinstance(p.get('queryId'), str)))
|
||||
if (doc.get('evidenceRef') and proof_ids
|
||||
and all(query_id in complete_receipt_ids for query_id in proof_ids)):
|
||||
verified_document_ids[doc['evidenceRef']] = proof_ids
|
||||
document_refs = set(verified_document_ids)
|
||||
cited_refs = set(original_refs) & document_refs
|
||||
cited_ids = list(dict.fromkeys(query_id for doc_ref in original_refs if doc_ref in cited_refs
|
||||
for query_id in verified_document_ids[doc_ref]))
|
||||
unresolved_refs = sorted(set(original_refs) - cited_refs)
|
||||
if unresolved_refs:
|
||||
missing.extend('原文引用未关联本轮完整scope正式查询证据:' + ref + '。' for ref in unresolved_refs)
|
||||
background_ids = list(dict.fromkeys(query_id for proof_ids in verified_document_ids.values()
|
||||
for query_id in proof_ids)) if not original_refs else []
|
||||
if not original_refs and not background_ids and documents:
|
||||
missing.append('本轮没有可关联的完整scope正式查询证明。')
|
||||
conclusion_ids = cited_ids if original_refs else background_ids
|
||||
if len(conclusion_ids) > 40:
|
||||
missing.append('完整查询证据引用超过平台允许的40项上限。')
|
||||
conclusion_ids = conclusion_ids[:40]
|
||||
if not answer.strip():
|
||||
missing.append('自主分析未生成正文。')
|
||||
missing = list(dict.fromkeys(str(item) for item in missing if item))
|
||||
data_complete = bool(documents and not snapshot.get('rootGoalGaps')
|
||||
and (complete_scopes is not None or not snapshot.get('gaps'))
|
||||
and bool(conclusion_ids) and not unresolved_refs
|
||||
and len(cited_ids if original_refs else background_ids) <= 40)
|
||||
narrative_complete = bool(answer.strip() and natural_finish
|
||||
and (native_status == 'COMPLETED' or corrected_tool_errors))
|
||||
charts_complete = not visual_warnings
|
||||
complete = bool(data_complete and narrative_complete and not missing)
|
||||
if warnings or visual_warnings:
|
||||
_atomic_json(actor_root / 'private' / 'native-warning-diagnostics.json', {
|
||||
'turnId': request['turnId'], 'explorationWarnings': warnings,
|
||||
'visualWarnings': visual_warnings})
|
||||
if summary_warnings:
|
||||
if isinstance(final.get('readingSummary'), dict):
|
||||
final['readingSummary']['notices'] = list(dict.fromkeys([
|
||||
*final['readingSummary'].get('notices', []), *summary_warnings]))
|
||||
else:
|
||||
final['answer'] += '\n\n' + '\n'.join(dict.fromkeys(summary_warnings))
|
||||
if missing:
|
||||
final['answer'] += '\n\n尚未解决:' + ';'.join(missing)
|
||||
if isinstance(final.get('readingSummary'), dict):
|
||||
final['readingSummary']['notices'] = list(dict.fromkeys([
|
||||
*final['readingSummary'].get('notices', []), *missing]))
|
||||
_atomic_json(actor_root / 'private' / 'native-original-body-diagnostics.json', {
|
||||
'turnId': request['turnId'], 'executionId': identity['executionId'],
|
||||
'nativeStatus': native_status, 'nativeReason': native_reason,
|
||||
'finishReasons': list(accumulator.finish_reasons),
|
||||
'originalBodyCharacters': len(answer), 'originalBodyNonempty': bool(answer.strip()),
|
||||
'narrativeComplete': narrative_complete, 'dataComplete': data_complete,
|
||||
'narrativeFactBinding': 'not_assessed', 'citedEvidenceRefs': sorted(cited_refs),
|
||||
'unresolvedEvidenceRefs': unresolved_refs, 'missingEvidence': missing,
|
||||
'visualWarnings': visual_warnings, 'notices': list(dict.fromkeys(summary_warnings))})
|
||||
result = {'type': 'result', 'engine': 'NAO', 'status': 'COMPLETED' if complete else 'PARTIAL',
|
||||
'answer': final['answer'], 'readingSummary': final.get('readingSummary'),
|
||||
'reportTitle': final.get('reportTitle'), 'agentTask': task, 'protocol': identity,
|
||||
'answer': answer, 'readingSummary': None,
|
||||
'reportTitle': None, 'agentTask': task, 'protocol': identity,
|
||||
'executionMode': 'native_agent', 'completionMethod': 'native_autonomous_analysis',
|
||||
'stopReason': 'evidence_finalized' if complete else 'evidence_incomplete', 'finishReason': 'stop',
|
||||
'stopReason': 'evidence_finalized' if complete else 'evidence_incomplete',
|
||||
'finishReason': ('stop' if narrative_complete else
|
||||
str(accumulator.finish_reasons[-1]) if natural_finish and accumulator.finish_reasons else
|
||||
'incomplete' if native_status == 'COMPLETED' else str(native_reason or native_status)),
|
||||
'nativeStopReason': native_reason, 'nativeStopSucceeded': stop_succeeded,
|
||||
'nativeFinishReason': 'stop' if natural_finish else native_reason,
|
||||
'conclusionProtocolQueryIds': cited_ids,
|
||||
'conclusionProtocolQueryIds': conclusion_ids,
|
||||
'answerQuality': {'version': 1, 'dataComplete': data_complete,
|
||||
'narrativeComplete': narrative_complete,
|
||||
'chartsComplete': charts_complete,
|
||||
'narrativeFactBinding': 'not_assessed',
|
||||
'citedEvidenceRefs': sorted(cited_refs),
|
||||
'backgroundEvidenceQueryIds': background_ids,
|
||||
'unresolvedEvidenceRefs': unresolved_refs,
|
||||
'missingEvidence': missing,
|
||||
'notices': list(dict.fromkeys(summary_warnings))},
|
||||
'actualModel': model, 'phaseTimingsMs': timings,
|
||||
'protocolEvidence': receipts, 'queryEvidenceRefs': [doc['evidenceRef'] for doc in documents],
|
||||
'queryEvidenceBindings': query_evidence_bindings(documents, receipts),
|
||||
'toolCalls': accumulator.tool_calls, 'toolCount': len(accumulator.tool_calls),
|
||||
'timeline': accumulator.timeline, 'factsComplete': bool(documents and not missing),
|
||||
'timeline': accumulator.timeline, 'factsComplete': None,
|
||||
'evidenceCompletion': {'serverCoverageSufficient': not snapshot.get('rootGoalGaps'),
|
||||
'missingEvidence': missing},
|
||||
'agentContext': {key: copy.deepcopy(task[key]) for key in
|
||||
|
||||
@@ -316,14 +316,18 @@ class AnalysisVisualsTest(unittest.TestCase):
|
||||
|
||||
def test_rate_drop_is_signed_and_converted_to_percentage_points(self):
|
||||
result = changes_result(sort="rate_drop", rows=[
|
||||
{"rank": 1, "key": "p-1", "label": "规格一", "rateDrop": -0.025},
|
||||
{"rank": 2, "key": "p-2", "label": "规格二", "rateDrop": 0.04},
|
||||
{"rank": 1, "key": "p-1", "label": "规格一", "rateDrop": 0.025},
|
||||
{"rank": 2, "key": "p-2", "label": "规格二", "rateDrop": -0.04},
|
||||
])
|
||||
chart = build_visuals([evidence(result)], "画图比较产品合格率降幅", "turn-03")["visuals"][0]
|
||||
self.assertEqual(chart["metric"], "rate_drop_pp")
|
||||
self.assertEqual(chart["unit"], "pp")
|
||||
self.assertEqual(chart["series"][0]["values"], [-2.5, 4.0])
|
||||
self.assertEqual([row["key"] for row in result["rows"]], ["p-1", "p-2"])
|
||||
self.assertEqual([item["key"] for item in chart["categories"]], ["p-1", "p-2"])
|
||||
self.assertIn("率降幅线索", chart["title"])
|
||||
self.assertIn("本期减比较期", chart["note"])
|
||||
self.assertIn("仍按率降幅排序", chart["note"])
|
||||
self.assertIn("未纳入投入规模", chart["note"])
|
||||
|
||||
def test_broad_product_focus_omits_rate_only_ranking_chart(self):
|
||||
@@ -377,7 +381,9 @@ class AnalysisVisualsTest(unittest.TestCase):
|
||||
self.assertEqual(chart["requiredFields"], [
|
||||
"black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight"
|
||||
])
|
||||
self.assertEqual(chart["series"][0]["values"], [2.0, -1.0])
|
||||
self.assertEqual(chart["series"][0]["values"], [-2.0, 1.0])
|
||||
self.assertIn("负值为下降,正值为上升", chart["note"])
|
||||
self.assertIn("本期减比较期", chart["note"])
|
||||
|
||||
def test_quality_top_uses_ranked_weights_and_never_share(self):
|
||||
result = {
|
||||
|
||||
@@ -0,0 +1,313 @@
|
||||
"""Regression tests for the isolated native-agent original-body runtime copy."""
|
||||
import copy
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
PROJECT = Path(__file__).resolve().parents[1]
|
||||
RUNTIME_COPY = Path(os.environ.get('NAO_ORIGINAL_BODY_PATH',
|
||||
str(PROJECT / 'scripts/nao_native_run.py'))).expanduser().resolve()
|
||||
SPEC = importlib.util.spec_from_file_location('nao_native_run_original_body', RUNTIME_COPY)
|
||||
NATIVE_RUN = importlib.util.module_from_spec(SPEC)
|
||||
sys.modules[SPEC.name] = NATIVE_RUN
|
||||
SPEC.loader.exec_module(NATIVE_RUN)
|
||||
|
||||
|
||||
IDENTITY = {'attemptId': 'attempt-current', 'sessionId': 'session-current',
|
||||
'turnId': 'turn-current', 'revision': 3, 'executionId': 'execution-current'}
|
||||
|
||||
|
||||
def proof(query_id='query-current'):
|
||||
return {**IDENTITY, 'queryId': query_id, 'nodeId': 'root', 'scopeId': 'scope-root',
|
||||
'responseHash': 'a' * 64}
|
||||
|
||||
|
||||
def document(ref='Q00000001', evidence_proof=None):
|
||||
return {'evidenceRef': ref, 'result': {'protocolEvidence': evidence_proof or proof()}}
|
||||
|
||||
|
||||
def task():
|
||||
return {'version': 1, 'goal': '解释下降并根据实际结果调查', 'question': '本期下降原因',
|
||||
'executionMode': 'native_agent', 'kind': 'analysis',
|
||||
'period': {'year': 2026, 'month': 9}, 'comparisonPeriod': {'year': 2026, 'month': 8},
|
||||
'filters': {'department': '当前产业部'}, 'fields': ['rate'], 'stages': [],
|
||||
'dimensions': [], 'dimensionStages': {}, 'visualPreference': 'auto',
|
||||
'comparisonRequired': True, 'trendRequired': False,
|
||||
'verifySource': False, 'fullDetails': False}
|
||||
|
||||
|
||||
class NativeOriginalBodyTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.actor_root = Path(self.temp.name)
|
||||
(self.actor_root / 'private').mkdir()
|
||||
(self.actor_root / 'evidence').mkdir()
|
||||
self.cancel_event = threading.Event()
|
||||
self.task = task()
|
||||
self.request = {'sessionId': IDENTITY['sessionId'], 'turnId': IDENTITY['turnId'],
|
||||
'actorId': 'actor-current', 'releaseId': 'release-current',
|
||||
'question': self.task['question'],
|
||||
'context': {'_serverHistory': [], '_serverQuestions': [], '_protocol': {}}}
|
||||
self.current_proof = proof()
|
||||
self.documents = [document(evidence_proof=self.current_proof)]
|
||||
self.snapshot = {'scopes': [{'nodeId': 'root', 'scopeId': 'scope-root', 'task': self.task}],
|
||||
'queries': [{'proof': self.current_proof}],
|
||||
'scopeCoverage': [{'nodeId': 'root', 'scopeId': 'scope-root', 'complete': True}],
|
||||
'rootGoalGaps': [], 'gaps': [], 'pending': [], 'explorationWarnings': []}
|
||||
self.accumulator = SimpleNamespace(answer='原始回答 [Q00000001]', finish_reasons=['stop'],
|
||||
tool_calls=[{'tool': 'quality_data', 'status': 'completed', 'input': {'mode': 'compare'}}],
|
||||
timeline=[{'kind': 'assistant', 'text': '原始回答'}])
|
||||
self.native_status, self.native_reason, self.stop_succeeded = 'COMPLETED', 'stop', None
|
||||
self.events = []
|
||||
self.manager = Mock()
|
||||
self.manager.ensure_instance.return_value = (None, None, 'fixture-model', 'http://native.invalid')
|
||||
self.delegated = Mock()
|
||||
self.delegated.call.side_effect = lambda *args, **kwargs: copy.deepcopy(self.snapshot)
|
||||
self.synthetic_writer = Mock(side_effect=AssertionError('native original-body path must not synthesize text'))
|
||||
|
||||
quality_runtime = ModuleType('nao_quality_runtime')
|
||||
quality_runtime._atomic_json = self.atomic_json
|
||||
quality_runtime._authenticate = Mock(return_value=object())
|
||||
quality_runtime._sse_request = self.native
|
||||
quality_runtime._read_json = self.read_json
|
||||
agent_task = ModuleType('nao_agent_task')
|
||||
agent_task.bind_task = lambda seed, current_task: {**copy.deepcopy(current_task), **copy.deepcopy(seed)}
|
||||
protocol_execution = ModuleType('nao_protocol_execution')
|
||||
protocol_execution._identity = lambda authority: copy.deepcopy(IDENTITY)
|
||||
protocol_execution._platform_visual_context = lambda visuals: {'visualState': visuals.get('visualState')}
|
||||
protocol_execution.query_evidence_bindings = lambda docs, receipts: [
|
||||
{'evidenceRef': item['evidenceRef'], 'proofs': item['result'].get('protocolEvidence')}
|
||||
for item in docs]
|
||||
evidence_finalizer = ModuleType('nao_evidence_finalizer')
|
||||
evidence_finalizer.evidence_summaries = lambda *args, **kwargs: copy.deepcopy(self.documents)
|
||||
evidence_finalizer.synthesize_native = self.synthetic_writer
|
||||
visual_evidence = ModuleType('analysis_visual_evidence')
|
||||
visual_evidence.protocol_visual_result = self.visual_result
|
||||
runtime_policy = ModuleType('nao_runtime_policy')
|
||||
runtime_policy.native_timeout = lambda: 30
|
||||
native_exploration = ModuleType('nao_native_exploration')
|
||||
native_exploration._proofs = lambda result: ([result['protocolEvidence']]
|
||||
if isinstance(result, dict) and isinstance(result.get('protocolEvidence'), dict) else [])
|
||||
self.modules = {'nao_quality_runtime': quality_runtime, 'nao_agent_task': agent_task,
|
||||
'nao_protocol_execution': protocol_execution,
|
||||
'nao_evidence_finalizer': evidence_finalizer,
|
||||
'analysis_visual_evidence': visual_evidence,
|
||||
'nao_runtime_policy': runtime_policy,
|
||||
'nao_native_exploration': native_exploration}
|
||||
self.module_patch = patch.dict(sys.modules, self.modules)
|
||||
self.module_patch.start()
|
||||
self.addCleanup(self.module_patch.stop)
|
||||
|
||||
def atomic_json(self, path, payload):
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps(payload, ensure_ascii=False), encoding='utf-8')
|
||||
|
||||
@staticmethod
|
||||
def read_json(path):
|
||||
return json.loads(Path(path).read_text(encoding='utf-8'))
|
||||
|
||||
def native(self, **kwargs):
|
||||
return self.accumulator, self.native_status, self.native_reason, self.stop_succeeded
|
||||
|
||||
def visual_result(self, documents, scopes, question, turn_id, **kwargs):
|
||||
return {'visualState': 'ready', 'visuals': [{'id': 'formal-query-chart',
|
||||
'evidenceRefs': [item['evidenceRef'] for item in documents]}], 'missingEvidence': []}
|
||||
|
||||
def invoke(self):
|
||||
return NATIVE_RUN.run_native_exploration(request=copy.deepcopy(self.request),
|
||||
task=copy.deepcopy(self.task), authority=copy.deepcopy(IDENTITY),
|
||||
actor_root=self.actor_root, delegated=self.delegated,
|
||||
connection=('https://model.invalid/v1', 'synthetic-fixture-key', 'fixture-model'),
|
||||
manager=self.manager, state={}, cancel_event=self.cancel_event,
|
||||
emit=self.events.append, prior_evidence_files={'prior-evidence.json'},
|
||||
phase_timings={'planningAndReview': 10, 'taskConfirmation': 20})
|
||||
|
||||
def test_persists_the_complete_original_body_and_keeps_formal_charts(self):
|
||||
body = '原生回答第一行\n' + ('原生正文' * 1800) + '\n[Q00000001]'
|
||||
self.accumulator.answer = body
|
||||
self.accumulator.timeline = [{'kind': 'assistant', 'text': 'x' * 8000}]
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'COMPLETED')
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertIsNone(result['readingSummary'])
|
||||
self.assertEqual(result['visuals'][0]['id'], 'formal-query-chart')
|
||||
self.assertEqual(result['queryEvidenceRefs'], ['Q00000001'])
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-current'])
|
||||
self.assertEqual(result['answerQuality']['narrativeFactBinding'], 'not_assessed')
|
||||
self.assertEqual(result['answerQuality']['backgroundEvidenceQueryIds'], [])
|
||||
self.assertIsNone(result['factsComplete'])
|
||||
self.assertEqual(self.read_json(self.actor_root / 'private/native-draft.json')['answer'], body)
|
||||
self.assertEqual(self.read_json(self.actor_root / 'private/native-completion.json')['answer'], body)
|
||||
self.assertEqual(self.read_json(self.actor_root / 'private/native-original-body-diagnostics.json')
|
||||
['originalBodyCharacters'], len(body))
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_empty_native_body_stays_empty_and_cannot_be_completed(self):
|
||||
self.accumulator.answer = ''
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], '')
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['answerQuality']['narrativeComplete'])
|
||||
self.assertIn('自主分析未生成正文。', result['answerQuality']['missingEvidence'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_missing_formal_evidence_is_metadata_and_does_not_rewrite_the_body(self):
|
||||
self.documents = []
|
||||
body = '原始正文照常保留。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['answerQuality']['dataComplete'])
|
||||
self.assertIn('本轮未取得当前执行的可核验查询证据。', result['answerQuality']['missingEvidence'])
|
||||
self.assertNotIn('尚未解决', result['answer'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_partial_or_interrupted_generation_is_not_rewritten_or_marked_complete(self):
|
||||
self.native_status, self.native_reason = 'PARTIAL', 'length'
|
||||
self.accumulator.finish_reasons = ['length']
|
||||
body = '模型已输出的局限与正文。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['answerQuality']['narrativeComplete'])
|
||||
self.assertEqual(result['finishReason'], 'length')
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_completed_status_without_natural_stop_remains_partial(self):
|
||||
body = '原生回答保留,结束信号不足。'
|
||||
self.accumulator.answer = body
|
||||
cases = (
|
||||
('missing_stop_finish_reason', [], None),
|
||||
('stop_control_failed', ['stop'], False),
|
||||
)
|
||||
for label, finish_reasons, stop_succeeded in cases:
|
||||
with self.subTest(label=label):
|
||||
self.accumulator.finish_reasons = finish_reasons
|
||||
self.stop_succeeded = stop_succeeded
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['answerQuality']['narrativeComplete'])
|
||||
self.assertEqual(result['finishReason'], 'incomplete')
|
||||
self.assertIn('自主调查未收到有效的正常结束信号。',
|
||||
result['answerQuality']['missingEvidence'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_recovered_tool_error_keeps_natural_stop_completion_without_fact_binding(self):
|
||||
self.native_status, self.native_reason = 'PARTIAL', 'mcp_error'
|
||||
body = '原生回答保留自己的局限。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'COMPLETED')
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-current'])
|
||||
self.assertEqual(result['answerQuality']['backgroundEvidenceQueryIds'], ['query-current'])
|
||||
self.assertTrue(result['answerQuality']['narrativeComplete'])
|
||||
self.assertEqual(result['answerQuality']['narrativeFactBinding'], 'not_assessed')
|
||||
self.assertIsNone(result['factsComplete'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_no_explicit_refs_cannot_use_docs_without_verified_complete_scope_receipts(self):
|
||||
self.snapshot['scopeCoverage'] = None
|
||||
body = '正常原生正文不写证据编号。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], [])
|
||||
self.assertEqual(result['answerQuality']['backgroundEvidenceQueryIds'], [])
|
||||
self.assertFalse(result['answerQuality']['dataComplete'])
|
||||
self.assertIn('本轮没有可关联的完整scope正式查询证明。',
|
||||
result['answerQuality']['missingEvidence'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_unbound_original_citation_is_a_gap_without_rewriting_the_body(self):
|
||||
body = '原生回答里保留未绑定的引用。[QABCDEF99]'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertFalse(result['answerQuality']['dataComplete'])
|
||||
self.assertEqual(result['answerQuality']['unresolvedEvidenceRefs'], ['QABCDEF99'])
|
||||
self.assertIn('原文引用未关联本轮完整scope正式查询证据:QABCDEF99。',
|
||||
result['answerQuality']['missingEvidence'])
|
||||
self.assertNotIn('QABCDEF99', result['conclusionProtocolQueryIds'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_explicit_hexadecimal_query_reference_maps_only_its_receipt(self):
|
||||
hex_proof = proof('query-hex')
|
||||
self.snapshot['queries'].append({'proof': hex_proof})
|
||||
self.documents.append(document('QABCDEF12', hex_proof))
|
||||
body = '原文引用正式查询[QABCDEF12]。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'COMPLETED')
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['answerQuality']['citedEvidenceRefs'], ['QABCDEF12'])
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-hex'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_cancelled_turn_remains_cancelled_and_does_not_read_or_synthesize(self):
|
||||
for cancellation in ('event', 'native_status'):
|
||||
with self.subTest(cancellation=cancellation):
|
||||
self.cancel_event.clear()
|
||||
self.native_status, self.native_reason = 'COMPLETED', 'stop'
|
||||
if cancellation == 'event':
|
||||
self.cancel_event.set()
|
||||
else:
|
||||
self.native_status = 'CANCELLED'
|
||||
self.accumulator.answer = '中断前的草稿'
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'CANCELLED')
|
||||
self.assertEqual(result['answer'], '')
|
||||
self.delegated.call.assert_not_called()
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
def test_server_coverage_gap_keeps_body_and_prevents_completed_status(self):
|
||||
self.snapshot['rootGoalGaps'] = ['亮片必答阶段尚无完整两期依据。']
|
||||
body = '原生结论及其局限。'
|
||||
self.accumulator.answer = body
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['answer'], body)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['answerQuality']['dataComplete'])
|
||||
self.assertIn('亮片必答阶段尚无完整两期依据。', result['answerQuality']['missingEvidence'])
|
||||
self.synthetic_writer.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,6 +1,8 @@
|
||||
"""Exercise the native runner with isolated files and mocked upstream/bridge calls."""
|
||||
import copy
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -10,8 +12,15 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts')]
|
||||
from nao_native_run import run_native_exploration
|
||||
PROJECT = Path(__file__).resolve().parents[1]
|
||||
sys.path[:0] = [str(PROJECT / 'scripts')]
|
||||
RUNTIME_PATH = Path(os.environ.get('NAO_ORIGINAL_BODY_PATH',
|
||||
str(PROJECT / 'scripts/nao_native_run.py'))).expanduser().resolve()
|
||||
RUNTIME_SPEC = importlib.util.spec_from_file_location('nao_native_run_integration', RUNTIME_PATH)
|
||||
RUNTIME_MODULE = importlib.util.module_from_spec(RUNTIME_SPEC)
|
||||
sys.modules[RUNTIME_SPEC.name] = RUNTIME_MODULE
|
||||
RUNTIME_SPEC.loader.exec_module(RUNTIME_MODULE)
|
||||
run_native_exploration = RUNTIME_MODULE.run_native_exploration
|
||||
from nao_quality_runtime import _atomic_json
|
||||
|
||||
|
||||
@@ -54,31 +63,25 @@ class NativeRunTests(unittest.TestCase):
|
||||
'missingEvidence': ['节点bright-detail范围基础范围缺少bright来源具体缺陷重量图。']}
|
||||
result = self.invoke()
|
||||
self.assertEqual(result['status'], 'COMPLETED')
|
||||
self.assertEqual(result['readingSummary']['notices'],
|
||||
self.assertEqual(result['answerQuality']['notices'],
|
||||
['亮片细分缺陷尚未完整核实:当前范围;未用于已核实结论。'])
|
||||
self.assertNotIn('调查局限', result['answer'])
|
||||
self.assertNotIn('bright', result['answer'])
|
||||
stored = json.loads((self.actor_root / 'private/native-warning-diagnostics.json').read_text())
|
||||
self.assertIn('bright', stored['visualWarnings'][0])
|
||||
|
||||
def test_full_facts_remain_private_instead_of_being_appended_to_the_business_answer(self):
|
||||
self.final['fullEvidenceFacts'] = [{'text': '后台保留的完整统计事实', 'evidenceRef': 'Q00000001'}]
|
||||
def test_original_answer_is_not_replaced_by_a_generated_report(self):
|
||||
self.accumulator.answer = '原生回答原样保留,局限也照原文保留。'
|
||||
result = self.invoke()
|
||||
self.assertNotIn('完整统计依据', result['answer'])
|
||||
self.assertNotIn('后台保留的完整统计事实', result['answer'])
|
||||
stored = json.loads((self.actor_root / 'private/native-full-evidence-facts.json').read_text())
|
||||
self.assertEqual(stored, self.final['fullEvidenceFacts'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertIsNone(result['readingSummary'])
|
||||
self.synthesis.assert_not_called()
|
||||
|
||||
def test_issued_binding_catalog_remains_private_for_exact_replay(self):
|
||||
issued = {'bindingInventory': {'N17': {'factId': 'F2', 'value': '493.9966', 'unit': 'kg'}},
|
||||
'facts': [{'id': 'F2', 'rawValue': '493.99660000'}]}
|
||||
self.final['_privateBindingInput'] = issued
|
||||
def test_no_second_composition_artifact_is_written(self):
|
||||
result = self.invoke()
|
||||
stored = json.loads((self.actor_root / 'private/native-composition.json').read_text())
|
||||
self.assertEqual(stored['bindingInput'], issued)
|
||||
self.assertNotIn('_privateBindingInput', result)
|
||||
self.assertNotIn('bindingInput', result)
|
||||
self.assertNotIn('bindingInventory', json.dumps(self.events, ensure_ascii=False))
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertFalse((self.actor_root / 'private/native-composition.json').exists())
|
||||
self.synthesis.assert_not_called()
|
||||
|
||||
def setUp(self):
|
||||
self.folder = tempfile.TemporaryDirectory()
|
||||
@@ -105,11 +108,6 @@ class NativeRunTests(unittest.TestCase):
|
||||
'scopeCoverage': [{'nodeId': 'root', 'scopeId': 'scope-root', 'complete': True},
|
||||
{'nodeId': 'group', 'scopeId': 'scope-group', 'complete': True}],
|
||||
'rootGoalGaps': [], 'gaps': [], 'pending': [], 'explorationWarnings': []}
|
||||
self.final = {'answer': '按已核实结果解释[Q00000001]', 'reportTitle': '质量数据分析',
|
||||
'readingSummary': {'version': 1, 'lead': '已核实解释[Q00000001]',
|
||||
'evidence': [], 'notices': [], 'evidenceRefs': ['Q00000001']},
|
||||
'usedEvidenceRefs': ['Q00000001'], 'evidenceSufficient': True,
|
||||
'missingEvidence': [], 'rawModelResponses': [], 'compositionDiagnostics': {}}
|
||||
self.accumulator = SimpleNamespace(answer='原生草稿', finish_reasons=['stop'],
|
||||
tool_calls=[{'tool': 'quality_data', 'status': 'failed', 'input': {'mode': 'compare'}},
|
||||
{'tool': 'quality_data', 'status': 'completed', 'input': {'mode': 'compare'}}],
|
||||
@@ -127,7 +125,7 @@ class NativeRunTests(unittest.TestCase):
|
||||
self.read_evidence = stack.enter_context(patch('nao_evidence_finalizer.evidence_summaries',
|
||||
side_effect=lambda *args, **kwargs: copy.deepcopy(self.documents)))
|
||||
self.synthesis = stack.enter_context(patch('nao_evidence_finalizer.synthesize_native',
|
||||
side_effect=lambda **kwargs: copy.deepcopy(self.final)))
|
||||
side_effect=AssertionError('native-agent original body must not be synthesized')))
|
||||
self.visuals = stack.enter_context(patch('analysis_visual_evidence.protocol_visual_result',
|
||||
return_value={'visualState': 'not_requested', 'visuals': [], 'missingEvidence': []}))
|
||||
stack.enter_context(patch('nao_runtime_policy.native_timeout', return_value=30))
|
||||
@@ -161,8 +159,12 @@ class NativeRunTests(unittest.TestCase):
|
||||
|
||||
self.assertTrue(result['evidenceCompletion']['serverCoverageSufficient'])
|
||||
self.assertEqual(result['timeline'], self.accumulator.timeline)
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-current', 'query-compare'])
|
||||
self.assertIn(self.snapshot['explorationWarnings'][0], result['readingSummary']['notices'])
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'],
|
||||
['query-current', 'query-compare', 'query-group'])
|
||||
self.assertEqual(result['answerQuality']['citedEvidenceRefs'], [])
|
||||
self.assertEqual(result['answerQuality']['narrativeFactBinding'], 'not_assessed')
|
||||
self.assertIn(self.snapshot['explorationWarnings'][0], result['answerQuality']['notices'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertIn('firstUsefulMs', result['phaseTimingsMs'])
|
||||
self.assertGreaterEqual(result['phaseTimingsMs']['firstUsefulMs'], 30)
|
||||
self.delegated.call.assert_called_once_with('/api/analysis/protocol/queries', payload={}, method='POST')
|
||||
@@ -172,35 +174,33 @@ class NativeRunTests(unittest.TestCase):
|
||||
self.assertIsNone(self.sse.call_args.kwargs['chat_id'])
|
||||
stored = json.loads((self.actor_root / 'private/native-completion.json').read_text())
|
||||
self.assertEqual(stored['status'], 'COMPLETED')
|
||||
self.assertEqual(stored['answer'], self.accumulator.answer)
|
||||
self.assertEqual(self.events[-1], result)
|
||||
|
||||
def test_generated_chart_contract_is_available_before_the_single_synthesis(self):
|
||||
def test_generated_chart_contract_uses_formal_query_evidence_without_rewriting_answer(self):
|
||||
self.task['visualPreference']='required'
|
||||
self.visuals.return_value={'visualState':'ready','missingEvidence':[], 'visuals':[{
|
||||
'id':'actual-chart','type':'line','metric':'composite_rate','title':'正式趋势',
|
||||
'scope':{'department':'当前产业部'},'period':{'year':2026,'month':9},
|
||||
'categories':[{'key':'2026-09'}],'series':[{'values':[.95]}],
|
||||
'evidenceRefs':['Q00000001']}]}
|
||||
def synthesize(**kwargs):
|
||||
preview=kwargs['context']['_platformVisuals']
|
||||
self.assertEqual(preview['visualState'],'ready')
|
||||
self.assertEqual(preview['charts'][0]['id'],'actual-chart')
|
||||
self.assertEqual(preview['charts'][0]['evidenceRefs'],['Q00000001'])
|
||||
self.assertEqual(preview['charts'][0]['pointCount'],1)
|
||||
self.assertNotIn('series',preview['charts'][0])
|
||||
return copy.deepcopy(self.final)
|
||||
self.synthesis.side_effect=synthesize
|
||||
result=self.invoke()
|
||||
self.assertEqual(result['status'],'COMPLETED')
|
||||
self.visuals.assert_called_once();self.synthesis.assert_called_once()
|
||||
self.visuals.assert_called_once()
|
||||
self.assertEqual(self.visuals.call_args.args[0], self.documents)
|
||||
self.assertEqual(result['visuals'][0]['id'], 'actual-chart')
|
||||
self.assertEqual(result['visuals'][0]['evidenceRefs'], ['Q00000001'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.synthesis.assert_not_called()
|
||||
|
||||
def test_root_gap_remains_partial_even_with_stop_and_available_evidence(self):
|
||||
self.snapshot['rootGoalGaps'] = ['亮片必答阶段尚无完整两期依据。']
|
||||
result = self.invoke()
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['evidenceCompletion']['serverCoverageSufficient'])
|
||||
self.assertIn(self.snapshot['rootGoalGaps'][0], result['answer'])
|
||||
self.assertIn(self.snapshot['rootGoalGaps'][0], result['readingSummary']['notices'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertIn(self.snapshot['rootGoalGaps'][0], result['evidenceCompletion']['missingEvidence'])
|
||||
self.assertIn(self.snapshot['rootGoalGaps'][0], result['answerQuality']['missingEvidence'])
|
||||
|
||||
def test_three_no_progress_attempts_remain_partial_after_natural_stop(self):
|
||||
_atomic_json(self.actor_root / 'private/native-exploration-state.json',
|
||||
@@ -210,7 +210,8 @@ class NativeRunTests(unittest.TestCase):
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertTrue(any('连续三次' in gap for gap in result['evidenceCompletion']['missingEvidence']))
|
||||
self.assertEqual(result['phaseTimingsMs']['formalQuery'], 1234)
|
||||
self.assertTrue(any('连续三次' in notice for notice in result['readingSummary']['notices']))
|
||||
self.assertTrue(any('连续三次' in gap for gap in result['answerQuality']['missingEvidence']))
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
|
||||
def test_controlled_no_progress_stop_does_not_add_generic_tool_failure(self):
|
||||
_atomic_json(self.actor_root / 'private/native-exploration-state.json',
|
||||
@@ -233,7 +234,7 @@ class NativeRunTests(unittest.TestCase):
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertTrue(any('未正常结束' in gap for gap in result['evidenceCompletion']['missingEvidence']))
|
||||
|
||||
def test_only_complete_current_scope_documents_reach_synthesis(self):
|
||||
def test_only_complete_current_scope_documents_reach_visual_generation_and_result(self):
|
||||
self.snapshot['queries'].extend([{'proof': self.p_failed}, {'proof': self.p_old}])
|
||||
self.snapshot['scopeCoverage'].append(
|
||||
{'nodeId': 'failed', 'scopeId': 'scope-failed', 'complete': False})
|
||||
@@ -245,27 +246,52 @@ class NativeRunTests(unittest.TestCase):
|
||||
{'evidenceRef': 'Q00000007', 'result': {'summary': {'unproved': True}}},
|
||||
])
|
||||
result = self.invoke()
|
||||
admitted = self.synthesis.call_args.kwargs['evidence']
|
||||
admitted = self.visuals.call_args.args[0]
|
||||
self.assertEqual([doc['evidenceRef'] for doc in admitted], ['Q00000001', 'Q00000002'])
|
||||
self.assertEqual(result['queryEvidenceRefs'], ['Q00000001', 'Q00000002'])
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'],
|
||||
['query-current', 'query-compare', 'query-group'])
|
||||
self.assertNotIn('query-failed', result['conclusionProtocolQueryIds'])
|
||||
self.assertNotIn('query-old', result['conclusionProtocolQueryIds'])
|
||||
self.assertNotIn('query-not-in-current-receipts', result['conclusionProtocolQueryIds'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.read_evidence.assert_called_once_with(self.actor_root / 'evidence', RELEASE,
|
||||
exclude_files={'prior-evidence.json'}, allow_empty=True)
|
||||
self.synthesis.assert_not_called()
|
||||
|
||||
def test_used_refs_map_only_to_actual_current_proofs_not_unused_or_foreign_documents(self):
|
||||
def test_original_citations_map_only_to_actual_current_query_proofs(self):
|
||||
self.snapshot['queries'].append({'proof': self.p_old})
|
||||
self.documents.append(document('Q00000003', self.p_old))
|
||||
self.final['usedEvidenceRefs'] = ['Q00000001', 'Q00000003', 'QDEADBEEF', 'Q00000001']
|
||||
self.accumulator.answer = '原文引用当前证据[Q00000001],以及外部证据[Q00000003]。'
|
||||
result = self.invoke()
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-current', 'query-compare'])
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertEqual(result['answerQuality']['unresolvedEvidenceRefs'], ['Q00000003'])
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertNotIn('query-group', result['conclusionProtocolQueryIds'])
|
||||
self.assertNotIn('query-old', result['conclusionProtocolQueryIds'])
|
||||
|
||||
def test_claimed_sufficient_answer_without_current_cited_proof_remains_partial(self):
|
||||
self.final['usedEvidenceRefs'] = ['QDEADBEEF']
|
||||
def test_uppercase_hexadecimal_reference_maps_to_its_actual_receipt(self):
|
||||
hexadecimal = proof('query-hexadecimal')
|
||||
self.snapshot['queries'].append({'proof': hexadecimal})
|
||||
self.documents.append(document('QABCDEF12', hexadecimal))
|
||||
self.accumulator.answer = '原文显式引用[QABCDEF12]。'
|
||||
|
||||
result = self.invoke()
|
||||
|
||||
self.assertEqual(result['status'], 'COMPLETED')
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], ['query-hexadecimal'])
|
||||
self.assertEqual(result['answerQuality']['citedEvidenceRefs'], ['QABCDEF12'])
|
||||
|
||||
def test_unbound_original_citation_remains_a_metadata_gap(self):
|
||||
self.accumulator.answer = '原生回答的引用[QABCDEF99]仍按原文保留。'
|
||||
result = self.invoke()
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertEqual(result['conclusionProtocolQueryIds'], [])
|
||||
self.assertTrue(any('实际查询证明' in gap for gap in result['evidenceCompletion']['missingEvidence']))
|
||||
self.assertEqual(result['answer'], self.accumulator.answer)
|
||||
self.assertEqual(result['answerQuality']['unresolvedEvidenceRefs'], ['QABCDEF99'])
|
||||
self.assertTrue(any('QABCDEF99' in gap for gap in result['evidenceCompletion']['missingEvidence']))
|
||||
|
||||
def test_cancelled_native_turn_never_reads_query_snapshot_or_synthesizes(self):
|
||||
for cancellation in ('event', 'native_status'):
|
||||
@@ -295,12 +321,12 @@ class NativeRunTests(unittest.TestCase):
|
||||
self.assertNotIn('图表尚未完整生成,可独立恢复。', result['answer'])
|
||||
self.assertNotIn('结果为部分完成', result['answer'])
|
||||
self.assertNotIn('共有15张图表', result['answer'])
|
||||
self.assertEqual(result['readingSummary']['notices'],
|
||||
self.assertEqual(result['answerQuality']['notices'],
|
||||
['部分重点图表未能完整展示;已保留核实文字和表格。'])
|
||||
else:
|
||||
self.assertIn('图表尚未完整生成,可独立恢复。', result['answer'])
|
||||
self.assertIn('图表尚未完整生成,可独立恢复。', result['readingSummary']['notices'])
|
||||
self.assertIn('结果为部分完成', result['answer'])
|
||||
self.assertNotIn('图表尚未完整生成,可独立恢复。', result['answer'])
|
||||
self.assertNotIn('结果为部分完成', result['answer'])
|
||||
self.assertIn('图表尚未完整生成,可独立恢复。', result['answerQuality']['missingEvidence'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -21,7 +21,7 @@ export function AnalysisConclusion({ reading, visuals, renderVisuals }: {
|
||||
<div className="analysis-comparison-previous"><span>{comparison.previousLabel}</span><strong>{comparison.previous}</strong></div>
|
||||
<span className="analysis-comparison-arrow" aria-hidden="true">→</span>
|
||||
<div className="analysis-comparison-current"><span>{comparison.currentLabel}</span><strong>{comparison.current}</strong></div>
|
||||
<div className={`analysis-comparison-change ${/下降|降低/.test(comparison.direction) ? 'is-worse' : 'is-better'}`}><strong>{comparison.direction} {comparison.change}</strong><span>{comparison.unit}</span></div>
|
||||
<div className={`analysis-comparison-change ${/下降|降低/.test(comparison.direction) ? 'is-worse' : 'is-better'}`}><strong>{comparison.change}</strong><span>{comparison.unit} · {comparison.direction}</span></div>
|
||||
</div>
|
||||
</div>}
|
||||
{section.markdown && <AiMarkdown className="ai-answer" content={section.markdown} layout="sections" />}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { originalAnalysisBody } from './analysisOriginalBody';
|
||||
import { AnalysisOriginalContent } from './AnalysisOriginalContent';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Button } from 'antd';
|
||||
import { AiMarkdown } from './AiMarkdown';
|
||||
@@ -103,6 +105,7 @@ export function AnalysisNarrative({ answer, visuals, visualState, question, turn
|
||||
reveal(); window.addEventListener('hashchange', reveal);
|
||||
return () => window.removeEventListener('hashchange', reveal);
|
||||
}, [model.evidence, prefix]);
|
||||
const original = originalAnalysisBody(turn);
|
||||
const primaryIds = new Set(primaryVisuals.map(visual => visual.id));
|
||||
const compact = (visual: AnalysisVisual) => !reading.hasAppendix && useCompactRateComparison(visual, question);
|
||||
const comparisons = model.sections.flatMap(section => section.visuals).filter(compact);
|
||||
@@ -130,7 +133,7 @@ export function AnalysisNarrative({ answer, visuals, visualState, question, turn
|
||||
<NarrativeEvidence model={model} prefix={prefix} />
|
||||
</>;
|
||||
return <div className={`analysis-narrative ${comparisons.length ? 'has-comparisons' : ''}`}>
|
||||
{reading.hasAppendix ? <AnalysisConclusion reading={reading} visuals={primaryVisuals} renderVisuals={figures => <AnalysisVisuals visuals={figures} visualState="ready" beforeExport={beforeExport} embedded hideEvidence combineStages={false} />} /> : fullContent}
|
||||
{original ? <AnalysisOriginalContent answer={original.answer} visuals={model.sections.flatMap(section => section.visuals)} renderVisuals={figures => <AnalysisVisuals visuals={figures} visualState="ready" beforeExport={beforeExport} embedded hideEvidence combineStages={false} />} /> : reading.hasAppendix ? <AnalysisConclusion reading={reading} visuals={primaryVisuals} renderVisuals={figures => <AnalysisVisuals visuals={figures} visualState="ready" beforeExport={beforeExport} embedded hideEvidence combineStages={false} />} /> : fullContent}
|
||||
{turn && drilldown && <AnalysisProductDrilldown turn={turn} selection={drilldown} onClose={() => setDrilldown(null)} />}
|
||||
</div>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { AiMarkdown } from './AiMarkdown';
|
||||
import { originalAnalysisSections } from './analysisOriginalBody';
|
||||
import type { AnalysisVisual } from './analysisVisualsModel';
|
||||
export function AnalysisOriginalContent({ answer, visuals, renderVisuals }: {
|
||||
answer: string; visuals: AnalysisVisual[]; renderVisuals: (visuals: AnalysisVisual[]) => ReactNode;
|
||||
}) {
|
||||
return <div className="analysis-original-document">{originalAnalysisSections(answer, visuals).map(section =>
|
||||
<section key={section.id} data-original-section={section.id}>
|
||||
{section.markdown.trim() && <AiMarkdown content={section.markdown} className="ai-answer" />}
|
||||
{!!section.visuals.length && renderVisuals(section.visuals)}
|
||||
</section>)}</div>;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Spin } from 'antd';
|
||||
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
||||
import { CheckCircleOutlined, CloseCircleOutlined, ClockCircleOutlined, RightOutlined } from '@ant-design/icons';
|
||||
import { analysisIsRunning, compactAnalysisTimeline, duplicatesFinalAnalysisAnswer } from './analysisConversationState';
|
||||
import { analysisIsRunning, compactAnalysisTimeline } from './analysisConversationState';
|
||||
import { AiMarkdown } from './AiMarkdown';
|
||||
import { sanitizeAnalysisDisplayText } from './analysisDisplayText';
|
||||
import { AnalysisThinkingBlock } from './ModelThinkingPanel';
|
||||
@@ -45,32 +45,32 @@ export function AnalysisProcess({ turn, metadata }: { turn: Record<string, unkno
|
||||
const running = analysisIsRunning(turn.status || result.status);
|
||||
const raw = turn.timeline || result.timeline || plan.timeline;
|
||||
const timeline = compactAnalysisTimeline(Array.isArray(raw) ? raw.map(record) : []);
|
||||
const answer = String(result.answer || turn.answer || '');
|
||||
const rawSteps = turn.steps || result.steps || plan.steps;
|
||||
const steps = Array.isArray(rawSteps) ? rawSteps.map(record) : [];
|
||||
const fallback = steps.map((step, index) => ({ ...step, kind: 'tool', sequence: index, at: step.startedAt, hasResult: ['completed', 'failed'].includes(String(step.status)) }));
|
||||
const events = groupAdjacentThinking(orderedAnalysisProcess(turn, timeline.length ? timeline : fallback));
|
||||
const turnId = String(turn.id || turn.turnId || '');
|
||||
const [open, setOpen] = useState(false);
|
||||
const [open, setOpen] = useState(running);
|
||||
const previous = useRef({ turnId, running });
|
||||
const visibleOpen = previous.current.turnId === turnId && open;
|
||||
const visibleOpen = previous.current.turnId !== turnId ? running : previous.current.running && !running ? false : open;
|
||||
useEffect(() => {
|
||||
const changedTurn = previous.current.turnId !== turnId;
|
||||
const completed = previous.current.running && !running;
|
||||
if (changedTurn || completed) setOpen(false);
|
||||
if (changedTurn || !previous.current.running && running) setOpen(running);
|
||||
else if (completed) setOpen(false);
|
||||
previous.current = { turnId, running };
|
||||
}, [running, turnId]);
|
||||
const content = events.map(entry => {
|
||||
if (entry.kind === 'thinking') return <AnalysisThinkingBlock key={String(entry.key)} entry={entry as unknown as ModelThinking} running={running} />;
|
||||
const text = sanitizeAnalysisDisplayText(String(entry.text || ''));
|
||||
if (entry.kind === 'assistant') {
|
||||
if (!text.trim() || !running && entry.sequence === timeline.at(-1)?.sequence && timeline.at(-1)?.kind === 'assistant' && duplicatesFinalAnalysisAnswer(text, answer)) return null;
|
||||
if (!running || !text.trim()) return null;
|
||||
return <div className="analysis-answer analysis-event-note" key={String(entry.key)}><AiMarkdown content={text} className="ai-answer" /></div>;
|
||||
}
|
||||
const step = steps.find(item => item.toolCallId && item.toolCallId === entry.toolCallId);
|
||||
return <ToolCall key={String(entry.key)} entry={{ ...entry, ...(step?.recovered ? { recovered: true, text: step.outputSummary || '调整调用后已重试成功' } : {}) }} running={running} />;
|
||||
});
|
||||
if (!events.length) return <>{metadata}{running && <p className="analysis-chat-notice" role="status">{sanitizeAnalysisDisplayText(String(turn.progressMessage || plan.progressMessage || '正在准备分析…'))}</p>}</>;
|
||||
if (!events.length) return <>{metadata}</>;
|
||||
return <details className="analysis-process-history analysis-agent-process" open={visibleOpen} onToggle={event => setOpen(event.currentTarget.open)}>
|
||||
<summary aria-label={running ? '分析运行信息,展开分析过程' : '分析运行信息,展开已保存过程'}>{metadata || <span>{running ? '分析中' : '已完成'}</span>}<RightOutlined aria-hidden="true" /></summary>
|
||||
<div className="analysis-agent-stream" aria-label={running ? '实时分析对话' : '已保存的分析过程'}>{content}</div>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { originalAnalysisBody } from './analysisOriginalBody';
|
||||
import { AnalysisOriginalContent } from './AnalysisOriginalContent';
|
||||
import { useMemo } from 'react';
|
||||
import { AiMarkdown } from './AiMarkdown';
|
||||
import { AnalysisConclusion } from './AnalysisConclusion';
|
||||
@@ -15,6 +17,7 @@ export function AnalysisReportNarrative({ turn, answer, images }: { turn: Row; a
|
||||
const { model, reading, primaryVisuals } = useMemo(() => buildAnalysisReadingModel(answer, visuals, String(turn.question || ''), turn), [answer, signature, turn]);
|
||||
const prefix = `report-${String(turn.id || turn.turnId || 'answer').replace(/[^a-zA-Z0-9_-]/g, '')}-`;
|
||||
const text = (content: string) => <AiMarkdown content={content} evidenceLabels={model.evidenceLabels} evidencePrefix={prefix} />;
|
||||
const original = originalAnalysisBody(turn);
|
||||
const primaryIds = new Set(primaryVisuals.map(visual => visual.id));
|
||||
const complete = <>
|
||||
{model.sections.filter(section => !primaryIds.size || section.narrative || section.tables.length || section.focusProducts?.length || section.visuals.some(visual => !primaryIds.has(visual.id))).map(section => <section key={section.id} id={`${prefix}${section.id}`} className={`analysis-narrative-section is-${section.kind}`} data-section-kind={section.kind}>
|
||||
@@ -30,7 +33,7 @@ export function AnalysisReportNarrative({ turn, answer, images }: { turn: Row; a
|
||||
<NarrativeEvidence model={model} prefix={prefix} />
|
||||
</>;
|
||||
return <div className="analysis-narrative report-narrative">
|
||||
{reading.hasAppendix ? <AnalysisConclusion reading={reading} visuals={primaryVisuals} renderVisuals={figures => figures.map(visual => <div key={visual.id}>
|
||||
{original ? <AnalysisOriginalContent answer={original.answer} visuals={visuals} renderVisuals={figures => figures.map(visual => <div key={visual.id}><ReportVisualFigure visual={visual} image={images.find(image => image.id === visual.id)} hideEvidence /></div>)} /> : reading.hasAppendix ? <AnalysisConclusion reading={reading} visuals={primaryVisuals} renderVisuals={figures => figures.map(visual => <div key={visual.id}>
|
||||
<ReportVisualFigure visual={visual} image={images.find(image => image.id === visual.id)} hideEvidence />
|
||||
</div>)} /> : complete}
|
||||
</div>;
|
||||
|
||||
+44
-30
@@ -38,7 +38,7 @@ import {
|
||||
} from 'antd';
|
||||
import type { ColumnsType } from 'antd/es/table';
|
||||
import { CompactManagementTables, QualityTable as Table } from './QualityTable';
|
||||
import { DeleteOutlined, EditOutlined, MoreOutlined, ReloadOutlined, RobotOutlined } from '@ant-design/icons';
|
||||
import { DeleteOutlined, EditOutlined, MoreOutlined, ReloadOutlined, RobotOutlined, LoadingOutlined } from '@ant-design/icons';
|
||||
import { ApiError, api, collectionItems, decodeQueryValueList } from './api';
|
||||
import type { AuthUser, Collection, OptionsResponse, QueryParams, SourceItem, ViewKind } from './types';
|
||||
import type { DataWorkflowStatus } from './NoCodeDataTypes';
|
||||
@@ -50,11 +50,11 @@ import { AnalysisReportExport, NativeAnalysisReport } from './AnalysisReportExpo
|
||||
import { AiMarkdown } from './AiMarkdown';
|
||||
import { authorizeAnalysisSnapshot } from './analysisSnapshot';
|
||||
import { AnalysisApiSettings } from './AnalysisApiSettings';
|
||||
import { acceptAnalysisEvent, analysisScrollFollows, partialAnalysisNotice, partialAnswerIsOnlyProgress, type AnalysisScrollIntent } from './analysisConversationState';
|
||||
import { acceptAnalysisEvent, analysisScrollFollows, analysisStreamStatus, partialAnswerIsOnlyProgress, type AnalysisScrollIntent } from './analysisConversationState';
|
||||
import { AnalysisProcess } from './AnalysisProcess';
|
||||
import { AnalysisMemberListPanel } from './AnalysisMemberList';
|
||||
import { AnalysisRecordListPanel } from './AnalysisRecordList';
|
||||
import { analysisScopeText, restoredAnalysisDraft, analysisComposerState, analysisFollowRequestReady } from './analysisScope';
|
||||
import { restoredAnalysisDraft, analysisComposerState, analysisFollowRequestReady } from './analysisScope';
|
||||
import { isValidAnalysisPeriodContext } from './analysisPeriodModel';
|
||||
import { beginAnalysisSubmission, requestSubmissionCancellation, submissionOutcomeUnknown, type AnalysisSubmission } from './analysisSubmission';
|
||||
import { activeAnalysisTurn, analysisIsRunning, beginAnalysisSelection, defaultAnalysisSession, rememberAnalysisSession, rememberPendingAnalysisSession, settledAnalysisTurn } from './analysisConversationState';
|
||||
@@ -1330,6 +1330,28 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
let streamedResult: Record<string, unknown> = {};
|
||||
let terminalEventStatus = '';
|
||||
setLastTurn(previous => ({ ...(previous || {}), id, question, status: previous?.status || 'QUEUED' }));
|
||||
let timingReadInFlight = false, timingReadAgain = false, startConfirmed = false;
|
||||
const refreshTiming = async (): Promise<void> => {
|
||||
if (!linkedSessionId || !current() || startConfirmed) return;
|
||||
if (timingReadInFlight) { timingReadAgain = true; return; }
|
||||
timingReadInFlight = true;
|
||||
try {
|
||||
const history = await api.analysisSessionTurns(linkedSessionId);
|
||||
if (!current()) return;
|
||||
const timing = collectionItems(history).find(item => item.id === id);
|
||||
if (!timing) return;
|
||||
startConfirmed = typeof timing.startedAt === 'string' && Number.isFinite(Date.parse(timing.startedAt));
|
||||
setLastTurn(previous => previous?.id !== id ? previous : { ...previous,
|
||||
...(typeof timing.createdAt === 'string' ? { createdAt: timing.createdAt } : {}),
|
||||
...(startConfirmed ? { startedAt: timing.startedAt } : {}),
|
||||
});
|
||||
} catch (value) { if (current()) onUnauthorized(value); }
|
||||
finally {
|
||||
timingReadInFlight = false;
|
||||
if (timingReadAgain && !startConfirmed && current()) { timingReadAgain = false; void refreshTiming(); }
|
||||
}
|
||||
};
|
||||
void refreshTiming();
|
||||
try {
|
||||
let detail: Record<string, unknown> = {};
|
||||
let reconnectAttempt = 0;
|
||||
@@ -1350,15 +1372,13 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
if (eventResult) streamedResult = { ...streamedResult, ...eventResult };
|
||||
const eventName = event.event.toUpperCase().replaceAll('.', '_').replaceAll('-', '_');
|
||||
const eventStatus = String(data.status || '').toUpperCase();
|
||||
if (ANALYSIS_FINAL_STATUSES.has(eventStatus)) terminalEventStatus = eventStatus;
|
||||
else if (/^(?:TURN_)?(COMPLETED|PARTIAL|FAILED|CANCELLED|NEEDS_CLARIFICATION)$/.test(eventName)) {
|
||||
terminalEventStatus = eventName.replace(/^TURN_/, '');
|
||||
}
|
||||
const turnEventStatus = analysisStreamStatus(undefined, event.event, data.status);
|
||||
if (ANALYSIS_FINAL_STATUSES.has(turnEventStatus)) terminalEventStatus = turnEventStatus;
|
||||
setLastTurn(previous => {
|
||||
const prior = previous || {};
|
||||
if (prior.id === id && ANALYSIS_FINAL_STATUSES.has(String(prior.status || '').toUpperCase()) && !ANALYSIS_FINAL_STATUSES.has(eventStatus)) return prior;
|
||||
const priorResult = isObjectRecord(prior.result) ? prior.result : {};
|
||||
const status = data.status || (['progress', 'message', 'heartbeat'].includes(event.event.toLowerCase()) ? prior.status || 'RUNNING' : event.event.toUpperCase());
|
||||
const status = analysisStreamStatus(prior.status, event.event, data.status);
|
||||
return {
|
||||
...prior,
|
||||
id,
|
||||
@@ -1379,6 +1399,7 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
// The SSE request can otherwise remain open after its terminal event.
|
||||
// Closing this subscription does not cancel the analysis itself.
|
||||
if (terminalEventStatus) controller.abort();
|
||||
else if (eventStatus === 'RUNNING' || eventName.startsWith('TOOL_')) void refreshTiming();
|
||||
},
|
||||
});
|
||||
} catch (streamError) {
|
||||
@@ -1530,7 +1551,8 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
followRequest.current = attempt.idempotencyKey;
|
||||
followOutput.current = true;
|
||||
explicitlyFollowing.current = false;
|
||||
setLastTurn({ ...turn, status: 'QUEUED', _uiFollowKey: attempt.idempotencyKey });
|
||||
const submittedAt = new Date().toISOString();
|
||||
setLastTurn({ id, sessionId: targetSessionId, question, status: 'SUBMITTING', _uiSubmittedAt: submittedAt, _uiFollowKey: attempt.idempotencyKey });
|
||||
const current = () => mounted.current && generation === navigationGeneration.current && submissionAttempt.current === attempt;
|
||||
setError(''); setBusy(true); setActiveTurnId(id);
|
||||
try {
|
||||
@@ -1548,7 +1570,7 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
if (!current()) return;
|
||||
const retriedDetail = await api.analysisTurn(retryId);
|
||||
if (!current()) return;
|
||||
setLastTurn({ ...retriedDetail, ...retried, id: retryId, sessionId: retrySessionId, question, _uiFollowKey: attempt.idempotencyKey });
|
||||
setLastTurn({ ...retriedDetail, ...retried, id: retryId, sessionId: retrySessionId, question, _uiSubmittedAt: submittedAt, _uiFollowKey: attempt.idempotencyKey });
|
||||
writeAnalysisLocation(retrySessionId, retryId);
|
||||
void loadSessions();
|
||||
if (!current()) return;
|
||||
@@ -1590,7 +1612,8 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
setError('');
|
||||
setRetryQuestion(question.trim());
|
||||
if (typeof lastTurn?.id === 'string' && lastTurn.id) setTurns(previous => previous.some(item => item.id === lastTurn.id) ? previous : [...previous, lastTurn]);
|
||||
setLastTurn({ question: question.trim(), status: 'QUEUED', _uiFollowKey: attempt.idempotencyKey });
|
||||
const submittedAt = new Date().toISOString();
|
||||
setLastTurn({ question: question.trim(), status: 'SUBMITTING', _uiSubmittedAt: submittedAt, _uiFollowKey: attempt.idempotencyKey });
|
||||
setQuery('');
|
||||
try {
|
||||
let selectedSessionId = sessionId;
|
||||
@@ -1619,7 +1642,7 @@ function AnalysisPage({ onUnauthorized, user }: { onUnauthorized: (error: unknow
|
||||
attempt.uncertain = false;
|
||||
attempt.acceptedTurnId = turnId;
|
||||
setActiveTurnId(turnId);
|
||||
setLastTurn({ ...turn, id: turnId, question: question.trim(), sessionId: selectedSessionId, _uiFollowKey: attempt.idempotencyKey });
|
||||
setLastTurn({ ...turn, status: turn.status || 'QUEUED', id: turnId, question: question.trim(), sessionId: selectedSessionId, _uiSubmittedAt: submittedAt, _uiFollowKey: attempt.idempotencyKey });
|
||||
writeAnalysisLocation(selectedSessionId, turnId);
|
||||
if (attempt.cancelRequested && await cancelAcceptedTurn(turnId, generation, question.trim())) return;
|
||||
void loadSessions();
|
||||
@@ -1744,7 +1767,6 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
|
||||
const question = typeof turn.question === 'string' ? turn.question : '';
|
||||
if (!turn.id && typeof turn.submissionNotice === 'string') return <article className="analysis-turn">
|
||||
{question && <div className="analysis-question"><p>{question}</p></div>}
|
||||
<p className="analysis-chat-notice" role="status">{turn.submissionNotice}</p>
|
||||
</article>;
|
||||
const result = isObjectRecord(turn.result) ? turn.result : turn;
|
||||
const memberList = result.memberList ?? turn.memberList;
|
||||
@@ -1754,24 +1776,15 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
|
||||
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;
|
||||
const resumable = ['QUEUED', 'RUNNING', 'PROCESSING', 'PLANNING', 'TOOL_RUNNING'].includes(status);
|
||||
const errorValue = result.error || turn.error;
|
||||
const failure = typeof errorValue === 'string' ? errorValue : '分析暂时中断,可以重试或继续提问。';
|
||||
const emptyStatus = status === 'CANCELLED' ? '本次分析已取消,未返回回答内容。'
|
||||
: status === 'PARTIAL' ? '本轮已保存查询过程,尚未形成最终答复。'
|
||||
: status === 'COMPLETED' ? '分析任务已完成,但未返回回答内容。'
|
||||
: failure;
|
||||
const scope = analysisScopeText(turn);
|
||||
const resumable = ['SUBMITTING', 'QUEUED', 'RUNNING', 'PROCESSING', 'PLANNING', 'TOOL_RUNNING'].includes(status);
|
||||
return <article className="analysis-turn" data-analysis-turn-id={String(turn.id || '')} data-analysis-status={status}>
|
||||
{question && <div className="analysis-question"><p>{question}</p></div>}
|
||||
{scope && <p className="analysis-turn-scope" aria-label="本轮分析范围">{scope}</p>}
|
||||
<AnalysisProcess turn={turn} metadata={<AnalysisTurnMetadata turn={turn} status={status} compact />} />
|
||||
{(!resumable || !Array.isArray(turn.timeline || (isObjectRecord(turn.plan) ? turn.plan.timeline : undefined))) && (answer || !resumable || !retryDisabled) && <div className="analysis-answer">
|
||||
{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} />}
|
||||
</> : <p className={resumable ? 'analysis-chat-notice' : undefined}>{resumable ? '分析任务仍在运行,尚未返回完整回答。' : emptyStatus}</p>}
|
||||
{status === 'PARTIAL' && answer && <p className="analysis-chat-notice">{partialAnalysisNotice(result.stopReason || turn.stopReason || (isObjectRecord(turn.plan) ? turn.plan.stopReason : ''), result)}</p>}
|
||||
</> : null}
|
||||
<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} />}
|
||||
@@ -1779,7 +1792,6 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
|
||||
{['FAILED', 'CANCELLED', 'PARTIAL'].includes(status) && typeof turn.id === 'string' && onRetry && <Button size="small" type="text" disabled={retryDisabled||recovery.recovering} onClick={() => onRetry(String(turn.id))}>重新分析</Button>}
|
||||
{resumable && !retryDisabled && typeof turn.id === 'string' && onResume && <Button size="small" type="text" onClick={() => onResume(String(turn.id))}>继续分析</Button>}
|
||||
</div>
|
||||
{recovery.error&&<p className="analysis-chat-notice" role="alert">{recovery.error}</p>}
|
||||
</div>}
|
||||
{memberList !== undefined && memberList !== null && <AnalysisMemberListPanel turn={turn} value={memberList} onUnauthorized={onUnauthorized} />}
|
||||
{recordList !== undefined && recordList !== null && <AnalysisRecordListPanel turn={turn} value={recordList} onUnauthorized={onUnauthorized} />}
|
||||
@@ -1788,21 +1800,23 @@ function AnalysisTurn({ turn: sourceTurn, onResume, onRetry, onClarify, onUnauth
|
||||
|
||||
function AnalysisTurnMetadata({ turn, status, compact = false }: { turn: Record<string, unknown>; status: string; compact?: boolean }) {
|
||||
const [now, setNow] = useState(Date.now());
|
||||
const running = analysisIsRunning(status);
|
||||
const running = analysisIsRunning(status) || status === 'SUBMITTING';
|
||||
useEffect(() => { if (!running) return; setNow(Date.now()); const timer = window.setInterval(() => setNow(Date.now()), 1000); return () => window.clearInterval(timer); }, [running]);
|
||||
const plan = isObjectRecord(turn.plan) ? turn.plan : {};
|
||||
const date = (value: unknown) => typeof value === 'string' && Number.isFinite(Date.parse(value)) ? Date.parse(value) : undefined;
|
||||
const start = date(turn.startedAt) ?? date(plan.startedAt);
|
||||
const waitingSince = date(turn._uiSubmittedAt) ?? date(turn.createdAt);
|
||||
const clockStart = start ?? (running ? waitingSince : undefined);
|
||||
const finish = date(turn.finishedAt);
|
||||
const duration = typeof turn.durationMs === 'number' ? turn.durationMs : start !== undefined && (running || finish !== undefined) ? Math.max(0, (running ? now : finish!) - start) : undefined;
|
||||
const duration = !running && typeof turn.durationMs === 'number' ? turn.durationMs : clockStart !== undefined && (running || finish !== undefined) ? Math.max(0, (running ? now : finish!) - clockStart) : undefined;
|
||||
const seconds = duration === undefined ? undefined : Math.floor(duration / 1000);
|
||||
const durationText = seconds === undefined ? '' : seconds >= 3600 ? `${Math.floor(seconds / 3600)}小时${Math.floor(seconds % 3600 / 60)}分${seconds % 60}秒` : seconds >= 60 ? `${Math.floor(seconds / 60)}分${seconds % 60}秒` : `${seconds}秒`;
|
||||
const stamp = (value: number) => new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).format(value);
|
||||
const steps = Array.isArray(turn.steps) ? turn.steps : Array.isArray(plan.steps) ? plan.steps : [];
|
||||
const labels: Record<string, string> = { QUEUED: '排队中', RUNNING: '运行中', COMPLETED: '已完成', PARTIAL: '部分完成', FAILED: '未完成', CANCELLED: '已停止', NEEDS_CLARIFICATION: '待补充信息' };
|
||||
const labels: Record<string, string> = { SUBMITTING: '提交中', QUEUED: '排队中', RUNNING: '运行中', COMPLETED: '已完成', PARTIAL: '', FAILED: '未完成', CANCELLED: '已停止', NEEDS_CLARIFICATION: '待补充信息' };
|
||||
return <span className="analysis-turn-metadata" aria-label="分析运行信息">
|
||||
<span>{labels[status] || (running ? '运行中' : '分析记录')}</span>
|
||||
{durationText && <span>{running ? '已运行' : '运行时长'} {durationText}</span>}
|
||||
{status !== 'PARTIAL' && <span>{labels[status] || (running ? '运行中' : '分析记录')}</span>}
|
||||
{durationText && <span className="analysis-runtime-clock">{running && <LoadingOutlined spin className="analysis-runtime-spinner" aria-hidden="true" />}{running ? start === undefined ? '已等待' : '已运行' : '运行时长'} {durationText}</span>}
|
||||
{!compact && start !== undefined && <span>开始 {stamp(start)}</span>}
|
||||
{!compact && !running && finish !== undefined && <span>{status === 'COMPLETED' ? '完成时间' : '结束时间'} {stamp(finish)}</span>}
|
||||
{!compact && steps.length > 0 && <span>工具调用 {steps.length} 次</span>}
|
||||
|
||||
@@ -141,15 +141,15 @@
|
||||
.analysis-process-history{margin:0;min-width:0;color:var(--muted-ink,#63758b)}
|
||||
.analysis-process-history>summary{display:flex;align-items:center;gap:8px;width:fit-content;min-height:24px;cursor:pointer;list-style:none;font-size:var(--font-control)}
|
||||
.analysis-process-history>summary::-webkit-details-marker{display:none}
|
||||
.analysis-process-history>summary .anticon{font-size:var(--font-note);transition:transform .16s ease}
|
||||
.analysis-process-history[open]>summary .anticon{transform:rotate(90deg)}
|
||||
.analysis-process-history>summary>.anticon{font-size:var(--font-note);transition:transform .16s ease}
|
||||
.analysis-process-history[open]>summary>.anticon{transform:rotate(90deg)}
|
||||
.analysis-process-history>summary:focus-visible{outline:2px solid var(--primary-color,#607ae1);outline-offset:3px}
|
||||
.analysis-process-history-body{padding:8px 0;max-height:360px;overflow:auto;overscroll-behavior:contain}
|
||||
.analysis-queued-followup{display:flex;align-items:center;gap:8px;color:var(--muted-ink,#63758b);font-size:var(--font-control);min-width:0}
|
||||
.analysis-queued-followup>span{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.analysis-queued-followup .ant-btn{flex:none;font-size:var(--font-control)}
|
||||
@media(max-width:700px){.analysis-simple-page .ai-actions{align-items:flex-start;gap:8px;flex-wrap:wrap}.analysis-composer-buttons{margin-left:auto;gap:5px}}
|
||||
@media(prefers-reduced-motion:reduce){.analysis-process-history>summary .anticon{transition:none}}
|
||||
@media(prefers-reduced-motion:reduce){.analysis-process-history>summary>.anticon{transition:none}}
|
||||
|
||||
@keyframes analysis-event-appear{from{opacity:.4;transform:translateY(2px)}to{opacity:1;transform:none}}
|
||||
.analysis-agent-stream>*{animation:analysis-event-appear .14s ease-out}
|
||||
@@ -171,3 +171,7 @@
|
||||
.analysis-clarification-actions{display:flex;justify-content:flex-end;margin-top:10px}
|
||||
.analysis-clarification fieldset:disabled .analysis-clarification-option{cursor:default;opacity:.65}
|
||||
.analysis-clarification-option:focus-within{outline:2px solid var(--primary-color,#607ae1);outline-offset:2px}
|
||||
|
||||
.analysis-runtime-clock{display:inline-flex;align-items:center;gap:5px}
|
||||
.analysis-turn-metadata .analysis-runtime-spinner{font-size:12px}
|
||||
@media(prefers-reduced-motion:reduce){.analysis-runtime-spinner,.analysis-runtime-spinner svg{animation:none!important}}
|
||||
|
||||
@@ -16,7 +16,11 @@ export function checkedConclusionComparison(sentence: string): ConclusionCompari
|
||||
const value = sentence.replace(/\*\*/g, '').trim();
|
||||
const match = value.match(/^([^。;\n]{1,50}?)(综合合格率|黑片合格率|亮片合格率|合格率)(?:为|是|[::])?\s*(\d+(?:\.\d+)?%)\s*[,,]\s*(?:较|相比|对比|比)\s*([^,,。;\n]{1,30}?)的?\s*(\d+(?:\.\d+)?%)\s*(下降|上升|提高|降低)\s*(\d+(?:\.\d+)?)(个百分点|pp)[。.]?$/);
|
||||
if (!match || [match[3], match[5]].some(rate => Number.parseFloat(rate) > 100)) return null;
|
||||
return { metric: match[2], currentLabel: match[1], current: match[3], previousLabel: match[4].replace(/的$/, ''), previous: match[5], direction: match[6], change: match[7], unit: match[8], original: value };
|
||||
const currentRate = Number.parseFloat(match[3]), previousRate = Number.parseFloat(match[5]);
|
||||
const isDecrease = /下降|降低/.test(match[6]);
|
||||
if (currentRate !== previousRate && (currentRate < previousRate) !== isDecrease) return null;
|
||||
const change = `${isDecrease ? '-' : '+'}${match[7]}`;
|
||||
return { metric: match[2], currentLabel: match[1], current: match[3], previousLabel: match[4].replace(/的$/, ''), previous: match[5], direction: match[6], change, unit: match[8], original: value };
|
||||
}
|
||||
|
||||
function topicMatches(title: string, visual: AnalysisVisual): boolean {
|
||||
|
||||
@@ -136,3 +136,16 @@ export function partialAnswerIsOnlyProgress(turn: Record<string, unknown>): bool
|
||||
if (lastAssistant < 0 || String(record(entries[lastAssistant]).text || '').trim() !== answer) return false;
|
||||
return entries.slice(lastAssistant + 1).some(item => ['tool_start', 'tool_result'].includes(String(record(item).kind)));
|
||||
}
|
||||
|
||||
/** Tool event names describe a step, never the lifecycle of the whole turn. */
|
||||
export function analysisStreamStatus(previous: unknown, event: string, reported: unknown): string {
|
||||
const prior = String(previous || '').toUpperCase();
|
||||
const terminal = new Set(['COMPLETED', 'PARTIAL', 'FAILED', 'CANCELLED', 'NEEDS_CLARIFICATION']);
|
||||
if (terminal.has(prior)) return prior;
|
||||
const name = event.toUpperCase().replaceAll('.', '_').replaceAll('-', '_');
|
||||
const status = String(reported || '').toUpperCase();
|
||||
if (/^(?:TURN_)?(COMPLETED|PARTIAL|FAILED|CANCELLED|NEEDS_CLARIFICATION)$/.test(name)) return name.replace(/^TURN_/, '');
|
||||
if (!name.startsWith('TOOL_') && (analysisIsRunning(status) || terminal.has(status))) return status;
|
||||
if (name.startsWith('TOOL_') || name === 'ANSWER') return 'RUNNING';
|
||||
return analysisIsRunning(prior) ? prior : 'RUNNING';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { analysisTextEvidenceRefs } from './analysisEvidenceRefs.ts';
|
||||
import type { AnalysisVisual } from './analysisVisualsModel.ts';
|
||||
type Row = Record<string, unknown>;
|
||||
const object = (value: unknown): Row => value && typeof value === 'object' && !Array.isArray(value) ? value as Row : {};
|
||||
const text = (value: unknown) => typeof value === 'string' ? value : '';
|
||||
export interface OriginalAnalysisBody { answer: string; source: 'saved' | 'timeline' | 'unavailable' }
|
||||
|
||||
/** Older native turns saved a replacement report; their final assistant remains in the authorized timeline. */
|
||||
export function originalAnalysisBody(turn?: Row): OriginalAnalysisBody | null {
|
||||
if (!turn) return null;
|
||||
const result = turn.result && typeof turn.result === 'object' ? object(turn.result) : turn;
|
||||
const plan = object(turn.plan || result.plan);
|
||||
if (![turn.executionMode, result.executionMode, plan.executionMode].includes('native_agent')) return null;
|
||||
const saved = text(result.answer);
|
||||
// Explicitly empty/redacted answers must not be replenished from an older transcript.
|
||||
if (!saved.trim()) return { answer: '', source: 'unavailable' };
|
||||
const lead = text(object(result.readingSummary).lead);
|
||||
const replaced = /已取得本轮统计结果,答案整理尚未完整完成|已取得本轮统计结果,部分解释尚未完成|现有证据不足以支持本轮完整分析,已保留可核验统计结果/.test(lead || saved.slice(0, 160));
|
||||
if (!replaced) return { answer: saved, source: 'saved' };
|
||||
const raw = turn.timeline || result.timeline || plan.timeline;
|
||||
const timeline = Array.isArray(raw) ? raw.map(object) : [];
|
||||
let lastTool = -1;
|
||||
timeline.forEach((entry, index) => { if (/^tool(?:_|$)/.test(text(entry.kind))) lastTool = index; });
|
||||
const parts = timeline.slice(lastTool + 1).filter(entry => entry.kind === 'assistant' && text(entry.text).trim()).map(entry => text(entry.text));
|
||||
// Historical public timeline entries were capped at 8000 characters without a truncation flag.
|
||||
if (parts.some(part => part.length >= 8000)) return { answer: '', source: 'unavailable' };
|
||||
return parts.length ? { answer: parts.join('\n\n'), source: 'timeline' } : { answer: '', source: 'unavailable' };
|
||||
}
|
||||
|
||||
export interface OriginalAnalysisSection { id: string; markdown: string; visuals: AnalysisVisual[] }
|
||||
/** Keep every Markdown line in order; images are additions after the associated chapter. */
|
||||
export function originalAnalysisSections(answer: string, visuals: AnalysisVisual[]): OriginalAnalysisSection[] {
|
||||
const sections: Array<OriginalAnalysisSection & { title: string; lines: string[] }> = [];
|
||||
let current = { id: 'original-1', markdown: '', visuals: [] as AnalysisVisual[], title: '', lines: [] as string[] };
|
||||
sections.push(current);
|
||||
let fenced = false;
|
||||
for (const line of answer.split('\n')) {
|
||||
if (/^\s*```/.test(line)) fenced = !fenced;
|
||||
const heading = !fenced ? line.match(/^#{1,6}\s+(.+?)\s*#*\s*$/) : null;
|
||||
if (heading && current.lines.length) {
|
||||
current = { id: `original-${sections.length + 1}`, markdown: '', visuals: [], title: heading[1], lines: [] };
|
||||
sections.push(current);
|
||||
} else if (heading) current.title = heading[1];
|
||||
current.lines.push(line);
|
||||
}
|
||||
sections.forEach(section => { section.markdown = section.lines.join('\n'); });
|
||||
const relevant = (title: string, visual: AnalysisVisual): number => {
|
||||
const stage = visual.stage || (visual.metric.startsWith('black_') ? 'black' : visual.metric.startsWith('bright_') ? 'bright' : undefined);
|
||||
if (stage === 'black' && /亮片/.test(title) && !/黑片/.test(title)
|
||||
|| stage === 'bright' && /黑片/.test(title) && !/亮片/.test(title)) return 0;
|
||||
|
||||
if (visual.dimension === 'product') return /规格|产品/.test(title) ? 4 : 0;
|
||||
if (visual.dimension === 'department' || visual.dimension === 'group') return /组织|产业部|分组|区域/.test(title) ? 4 : 0;
|
||||
if (visual.dimension === 'defect' || visual.metric.startsWith('source_defect')) return /缺陷|缺角|隐裂|源字段/.test(title) ? 4 : 0;
|
||||
if (/category/.test(visual.sectionKey || '')) return /报废大类|报废类别|正式报废/.test(title) ? 4 : 0;
|
||||
if (visual.type === 'line' || visual.type === 'area') return /趋势|逐月|月度/.test(title) ? 4 : 0;
|
||||
return /总体|阶段/.test(title) || stage === 'black' && /黑片/.test(title) || stage === 'bright' && /亮片/.test(title) ? 4 : /结论/.test(title) ? 2 : 0;
|
||||
};
|
||||
for (const visual of visuals) {
|
||||
const candidates = sections.map(section => {
|
||||
const refs = analysisTextEvidenceRefs(section.markdown);
|
||||
const topic = relevant(section.title, visual);
|
||||
return { section, score: topic ? topic + (visual.evidenceRefs.some(ref => refs.includes(ref)) ? 10 : 0) : 0 };
|
||||
}).filter(item => item.score > 0).sort((a, b) => b.score - a.score);
|
||||
if (candidates.length) candidates[0].section.visuals.push(visual);
|
||||
else sections.push({ id: `original-chart-${sections.length + 1}`, title: '', lines: [], markdown: '', visuals: [visual] });
|
||||
}
|
||||
return sections.filter(section => section.markdown.trim() || section.visuals.length).map(({ id, markdown, visuals }) => ({ id, markdown, visuals }));
|
||||
}
|
||||
@@ -7,6 +7,12 @@ export function analysisSnapshotResult(turn: Row): Row {
|
||||
return turn.result && typeof turn.result === 'object' && !Array.isArray(turn.result) ? object(turn.result) : turn;
|
||||
}
|
||||
|
||||
function originalTranscript(turn: Row): unknown {
|
||||
const result = analysisSnapshotResult(turn), plan = object(turn.plan || result.plan);
|
||||
if (![turn.executionMode, result.executionMode, plan.executionMode].includes('native_agent')) return null;
|
||||
return turn.timeline || result.timeline || plan.timeline || [];
|
||||
}
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`;
|
||||
if (value && typeof value === 'object') {
|
||||
@@ -19,16 +25,17 @@ export function reportSnapshotIdentity(turn: Row): string {
|
||||
const result = analysisSnapshotResult(turn);
|
||||
const value = (key: string, fallback: unknown) => ['readingSummary', 'queryEvidenceRefs'].includes(key)
|
||||
? result[key] ?? fallback : result[key] ?? turn[key] ?? fallback;
|
||||
return canonical({ answer: value('answer', ''), visuals: value('visuals', []),
|
||||
return canonical({ originalTranscript: originalTranscript(turn), answer: value('answer', ''), visuals: value('visuals', []),
|
||||
results: value('results', []), reportMarkdown: value('reportMarkdown', ''),
|
||||
reportHtml: value('reportHtml', ''), memberList: value('memberList', null),
|
||||
recordList: value('recordList', null), readingSummary: value('readingSummary', null), queryEvidenceRefs: value('queryEvidenceRefs', []), question: turn.question || '' });
|
||||
}
|
||||
|
||||
export function reportSnapshotDifferences(left: Row, right: Row): string[] {
|
||||
const fields = ['answer', 'visuals', 'results', 'reportMarkdown', 'reportHtml', 'memberList', 'recordList', 'readingSummary', 'queryEvidenceRefs', 'question'];
|
||||
const fields = ['answer', 'visuals', 'results', 'reportMarkdown', 'reportHtml', 'memberList', 'recordList', 'readingSummary', 'queryEvidenceRefs', 'question', 'originalTranscript'];
|
||||
return fields.filter(key => {
|
||||
const value = (turn: Row) => {
|
||||
if (key === 'originalTranscript') return originalTranscript(turn);
|
||||
const result = analysisSnapshotResult(turn);
|
||||
return key === 'question' ? turn.question || '' : ['readingSummary', 'queryEvidenceRefs'].includes(key)
|
||||
? result[key] ?? (key === 'readingSummary' ? null : []) : result[key] ?? turn[key] ?? (key === 'memberList' || key === 'recordList' || key === 'readingSummary' ? null : key === 'visuals' || key === 'results' || key === 'queryEvidenceRefs' ? [] : '');
|
||||
@@ -47,7 +54,7 @@ export async function authorizeAnalysisSnapshot(turn: Row): Promise<void> {
|
||||
}
|
||||
const differences = reportSnapshotDifferences(current, turn);
|
||||
if (differences.length) {
|
||||
const labels: Record<string, string> = { answer: '回答', visuals: '图表', results: '查询结果', reportMarkdown: '报告正文', reportHtml: '报告 HTML', memberList: '成员清单', recordList: '原始记录', readingSummary: '核心结论', queryEvidenceRefs: '数据依据', question: '问题' };
|
||||
const labels: Record<string, string> = { answer: '回答', visuals: '图表', results: '查询结果', reportMarkdown: '报告正文', reportHtml: '报告 HTML', memberList: '成员清单', recordList: '原始记录', readingSummary: '核心结论', queryEvidenceRefs: '数据依据', question: '问题', originalTranscript: '原始正文' };
|
||||
throw new Error(`分析结果或访问权限已变化(${differences.map(key => labels[key] || key).join('、')}),请重新打开这条分析后再导出。`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,6 +121,15 @@ function hasOnlyKeys(value: Record<string, unknown>, allowed: Set<string>): bool
|
||||
return Object.keys(value).every(key => allowed.has(key));
|
||||
}
|
||||
|
||||
function canonicalLegacyRateDropNote(note: string | undefined): string {
|
||||
const details = (note || '')
|
||||
.replace(/^(?:条形末端显示具体变化:)?[^;。]*?(?:正值|负值)[^;。]*[;。]\s*/, '')
|
||||
.replace(/^[;;,,。.\s]+/, '')
|
||||
.trim();
|
||||
const basis = '正值表示本期合格率上升,负值表示下降;变化值为本期减比较期。';
|
||||
return details ? `${basis} ${details}` : basis;
|
||||
}
|
||||
|
||||
/** 解析前仅复制普通 JSON 数据,阻止 getter、toJSON、原型和函数进入渲染链路。 */
|
||||
function copyJsonData(value: unknown, seen = new Set<object>(), budget = { nodes: 0 }, depth = 0): unknown {
|
||||
if (depth > 32 || ++budget.nodes > 100_000) throw new Error('unsafe JSON data');
|
||||
@@ -272,7 +281,7 @@ function parseVisual(value: unknown, index: number, issues: string[]): AnalysisV
|
||||
const comparisonPeriod = value.comparisonPeriod === undefined ? undefined : safeContext(value.comparisonPeriod);
|
||||
const evidenceRefs = safeTextList(value.evidenceRefs, 100);
|
||||
const requiredFields = safeTextList(value.requiredFields, 100);
|
||||
const note = value.note === undefined ? undefined : boundedText(value.note, 500);
|
||||
const rawNote = value.note === undefined ? undefined : boundedText(value.note, 500);
|
||||
const stage = value.stage === undefined ? undefined : value.stage;
|
||||
const sectionKey = value.sectionKey === undefined ? undefined : value.sectionKey;
|
||||
const dimension = value.dimension === undefined ? undefined : value.dimension;
|
||||
@@ -292,6 +301,12 @@ function parseVisual(value: unknown, index: number, issues: string[]): AnalysisV
|
||||
...(coverage.denominator === undefined ? {} : { denominator: Number(coverage.denominator) }),
|
||||
}
|
||||
: null;
|
||||
// v1 saved rate-drop charts used positive values for declines. New charts
|
||||
// carry "本期减比较期" in the existing bounded note field. This survives the
|
||||
// backend's strict v1 field allowlist; unmarked snapshots are legacy values.
|
||||
const normalizeLegacyRateDrop = metric === 'rate_drop_pp' && unit === 'pp'
|
||||
&& !(rawNote || '').includes('本期减比较期');
|
||||
const note = normalizeLegacyRateDrop ? canonicalLegacyRateDropNote(rawNote || undefined) : rawNote;
|
||||
|
||||
const categories = Array.isArray(value.categories) && value.categories.length > 0 && value.categories.length <= MAX_VISUAL_CATEGORIES
|
||||
? value.categories.map(category => {
|
||||
@@ -325,7 +340,7 @@ function parseVisual(value: unknown, index: number, issues: string[]): AnalysisV
|
||||
return null;
|
||||
}
|
||||
if (unit === 'ratio' && (item < 0 || (!allowsAboveOneRatio(metric || '') && item > 1))) invalidValueCells.push(`${seriesIndex}:${valueIndex}`);
|
||||
return item;
|
||||
return normalizeLegacyRateDrop ? -item : item;
|
||||
});
|
||||
series.push({ key, label, values });
|
||||
}
|
||||
@@ -335,7 +350,7 @@ function parseVisual(value: unknown, index: number, issues: string[]): AnalysisV
|
||||
const valid = !!id && !!type && !!title && subtitle !== null && UNIT_VALUES.has(unit as AnalysisVisualUnit)
|
||||
&& !!metric && !!originTurnId && !!scope && isValidAnalysisPeriodContext(scope)
|
||||
&& !!period && isValidAnalysisPeriodContext(period) && (value.comparisonPeriod === undefined || !!comparisonPeriod && isValidAnalysisPeriodContext(comparisonPeriod))
|
||||
&& !!evidenceRefs && !!requiredFields && (value.note === undefined || note !== null)
|
||||
&& !!evidenceRefs && !!requiredFields && (value.note === undefined || rawNote !== null)
|
||||
&& (stage === undefined || stage === 'black' || stage === 'bright')
|
||||
&& (sectionKey === undefined || SECTION_KEYS.has(sectionKey as AnalysisVisualSectionKey))
|
||||
&& (dimension === undefined || DIMENSIONS.has(dimension as AnalysisVisualDimension))
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>AI原文与运行状态验收</title></head><body><div id="root"></div><script type="module" src="./analysis-original.tsx"></script></body></html>
|
||||
@@ -0,0 +1,41 @@
|
||||
// Local acceptance fixture: all requests are intercepted; no model, API or database writes.
|
||||
import React from 'react';
|
||||
import {createRoot} from 'react-dom/client';
|
||||
import {App,ConfigProvider} from 'antd';
|
||||
import ManagementPages from '../src/ManagementPages';
|
||||
import '../src/styles.css';
|
||||
import '../src/workspace-finish.css';
|
||||
const user={id:'original-fixture',username:'fixture',displayName:'本地验收',roles:['SYSTEM_ADMIN'],departments:[],groups:[]};
|
||||
const modelAnswer='## 结论\n\n本期合格率下降,两个阶段的变化机制不同。完整模型正文必须保留。\n\n### 总体与阶段\n\n黑片合格率从97.58%降至97.27%,变化为 -0.30 pp。\n\n| 阶段 | 变化 |\n| --- | --- |\n| 黑片 | -0.30 pp |\n\n表格之后继续解释投入规模与报废规模,不能因为增加图表就丢掉这段。[Q11111111]\n\n### 下一步\n\n上述是统计线索,仍需现场核实生产原因。';
|
||||
let question='',status='QUEUED',createdAt='',startedAt='',finishedAt='';let timeline:Record<string,unknown>[]=[];
|
||||
const visual={id:'stage-change',type:'column',title:'黑片合格率变化',subtitle:'',unit:'pp',metric:'rate_drop_pp',stage:'black',categories:[{key:'black',label:'黑片'}],series:[{key:'rateDrop',label:'合格率降幅',values:[.30]}],evidenceRefs:['Q11111111'],requiredFields:['black_good_weight','black_total_weight'],scope:{year:2026,month:9,releaseId:'fixture'},period:{year:2026,month:9},comparisonPeriod:{year:2026,month:8},originTurnId:'fixture-turn',coverage:{totalRows:1,returnedRows:1,truncated:false}};
|
||||
const saved=()=>({id:'fixture-turn',sessionId:'fixture-session',question,status,createdAt,startedAt,finishedAt,executionMode:'native_agent',plan:{timeline},answer:'已取得本轮统计结果,答案整理尚未完整完成。',readingSummary:{version:1,lead:'已取得本轮统计结果,答案整理尚未完整完成。',evidence:[],notices:[],evidenceRefs:['Q11111111']},queryEvidenceRefs:['Q11111111'],visualSchemaVersion:1,visualState:'ready',visuals:[visual],stopReason:'evidence_incomplete'});
|
||||
const json=(value:unknown)=>new Response(JSON.stringify(value),{headers:{'Content-Type':'application/json'}});
|
||||
window.fetch=async(input,init)=>{
|
||||
const path=new URL(String(input),location.origin).pathname;
|
||||
if(path==='/api/auth/csrf')return json({token:'fixture'});
|
||||
if(path==='/api/options')return json({years:[2026],departments:[],groups:[],products:[],projectGroups:[]});
|
||||
if(path==='/api/analysis/sessions'&&init?.method==='POST'){await new Promise(r=>setTimeout(r,1500));return json({session:{id:'fixture-session'}});}
|
||||
if(path==='/api/analysis/sessions')return json({items:question?[{id:'fixture-session',title:question,activeTurnStatus:status}]:[]});
|
||||
if(path==='/api/analysis/sessions/fixture-session/turns'&&init?.method==='POST'){
|
||||
question=JSON.parse(String(init.body)).question;createdAt=new Date().toISOString();await new Promise(r=>setTimeout(r,300));return json({turn:{id:'fixture-turn',sessionId:'fixture-session',status,createdAt}});
|
||||
}
|
||||
if(path==='/api/analysis/sessions/fixture-session/turns')return json({items:[{id:'fixture-turn',status,createdAt,startedAt,finishedAt}]});
|
||||
if(path==='/api/analysis/sessions/fixture-session')return json({id:'fixture-session',context:{year:2026},turns:status==='PARTIAL'?[saved()]:[]});
|
||||
if(path==='/api/analysis/turns/fixture-turn'||path.endsWith('/snapshot'))return json(saved());
|
||||
if(path.endsWith('/events')){
|
||||
const encoder=new TextEncoder();let closed=false;const timers:ReturnType<typeof setTimeout>[]=[];
|
||||
const stream=new ReadableStream({start(controller){
|
||||
let eventId=0;const send=(event:string,data:unknown)=>{if(!closed)controller.enqueue(encoder.encode(`id: ${++eventId}\nevent: ${event}\ndata: ${JSON.stringify(data)}\n\n`));};
|
||||
send('progress',{status:'QUEUED'});
|
||||
timers.push(setTimeout(()=>{status='RUNNING';startedAt=new Date().toISOString();send('progress',{status,message:'正在理解本轮目标和连续追问…'});},300));
|
||||
timers.push(setTimeout(()=>{timeline=[{kind:'tool_start',sequence:1,tool:'read',toolCallId:'read-1',label:'核对正式指标',status:'running'}];send('tool_started',{timeline,step:timeline[0]});},800));
|
||||
timers.push(setTimeout(()=>{timeline.push({kind:'tool_result',sequence:2,tool:'read',toolCallId:'read-1',label:'核对正式指标',status:'completed'});send('tool_completed',{timeline,step:timeline[1]});},6000));
|
||||
timers.push(setTimeout(()=>{status='PARTIAL';finishedAt=new Date().toISOString();timeline.push({kind:'assistant',sequence:3,text:modelAnswer});send('partial',{status,result:saved()});if(!closed){closed=true;controller.close();}},30000));
|
||||
init?.signal?.addEventListener('abort',()=>{if(!closed){closed=true;controller.close();}timers.forEach(clearTimeout);},{once:true});
|
||||
},cancel(){closed=true;timers.forEach(clearTimeout);}});
|
||||
return new Response(stream,{headers:{'Content-Type':'text/event-stream'}});
|
||||
}
|
||||
return json({items:[]});
|
||||
};
|
||||
createRoot(document.getElementById('root')!).render(<ConfigProvider><App><main style={{padding:18,maxWidth:1500,margin:'auto'}}><ManagementPages page="analysis" user={user} onUnauthorized={()=>false} onNavigate={()=>{}}/></main></App></ConfigProvider>);
|
||||
@@ -0,0 +1,62 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import ts from 'typescript';
|
||||
import React from 'react';
|
||||
import {renderToStaticMarkup} from 'react-dom/server';
|
||||
import {analysisStreamStatus,analysisIsRunning,acceptAnalysisEvent,settledAnalysisTurn} from '../src/analysisConversationState.ts';
|
||||
const source=fs.readFileSync(new URL('../src/ManagementPages.tsx',import.meta.url),'utf8');
|
||||
const compile=code=>ts.transpileModule(code,{fileName:'status.tsx',compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText;
|
||||
const tick=()=>new Promise(resolve=>setImmediate(resolve));
|
||||
const deferred=()=>{let resolve;const promise=new Promise(r=>resolve=r);return{promise,resolve};};
|
||||
const isObjectRecord=v=>!!v&&typeof v==='object'&&!Array.isArray(v);
|
||||
const finalStatuses=new Set(['COMPLETED','PARTIAL','FAILED','CANCELLED','NEEDS_CLARIFICATION']);
|
||||
function fixture(){
|
||||
let lastTurn={id:'t1',question:'q',status:'QUEUED',createdAt:'2026-11-02T01:00:00Z'},onEvent,reads=0;
|
||||
const stream=deferred(),firstRead=deferred(),secondRead=deferred();
|
||||
const env={mounted:{current:true},streamController:{current:null},subscriptionGeneration:{current:0},sessionId:'s1',eventCursors:{current:{}},ANALYSIS_FINAL_STATUSES:finalStatuses,
|
||||
setLastTurn:next=>lastTurn=typeof next==='function'?next(lastTurn):next,writeAnalysisLocation(){},setActiveTurnId(){},setBusy(){},setRetryQuestion(){},setTurns(){},setError(){},loadSessions:async()=>{},onUnauthorized:()=>false,apiMessage:e=>e.message,
|
||||
isObjectRecord,analysisStreamStatus,analysisIsRunning,acceptAnalysisEvent,settledAnalysisTurn,collectionItems:value=>value.items||[],modelThinkingEntries:()=>[],mergeModelThinking:()=>[]};
|
||||
const api={analysisTurnEvents:async(_id,options)=>{onEvent=options.onEvent;return stream.promise;},analysisSessionTurns:()=>{reads++;return reads===1?firstRead.promise:secondRead.promise;}};
|
||||
const code=source.slice(source.indexOf(' const followTurn = async'),source.indexOf('\n useEffect(() => {\n if (!active || restoreStarted.current)'));
|
||||
const follow=new Function('api',...Object.keys(env),compile(code+'\nreturn followTurn;'))(api,...Object.values(env));
|
||||
const work=follow('t1','q','s1');
|
||||
return{get:()=>lastTurn,reads:()=>reads,switchTurn(){env.subscriptionGeneration.current++;lastTurn={id:'other',status:'RUNNING'};},emit(event,data){onEvent({id:String(Math.random()),event,data});},firstRead,secondRead,async close(){env.streamController.current?.abort();stream.resolve();await work;}};
|
||||
}
|
||||
test('tool step events keep the whole turn running and terminal status does not regress',()=>{
|
||||
assert.equal(analysisStreamStatus('RUNNING','tool_started',undefined),'RUNNING');
|
||||
assert.equal(analysisStreamStatus('RUNNING','tool_completed','completed'),'RUNNING');
|
||||
assert.equal(analysisStreamStatus('RUNNING','completed','COMPLETED'),'COMPLETED');
|
||||
assert.equal(analysisStreamStatus('COMPLETED','progress','RUNNING'),'COMPLETED');
|
||||
});
|
||||
test('actual event subscriber keeps status live and fills the server clock without re-entering',async()=>{
|
||||
const ui=fixture();assert.equal(ui.reads(),1);
|
||||
ui.emit('progress',{status:'RUNNING'});
|
||||
ui.firstRead.resolve({items:[{id:'t1',createdAt:'2026-11-02T01:00:00Z',startedAt:''}]});await tick();
|
||||
assert.equal(ui.reads(),2,'a running event during an initial queued timing read schedules one non-overlapping follow-up');
|
||||
ui.secondRead.resolve({items:[{id:'t1',startedAt:'2026-11-02T01:00:05Z'}]});await tick();
|
||||
ui.emit('tool_started',{step:{status:'running'}});
|
||||
assert.equal(ui.get().status,'RUNNING');assert.equal(ui.get().startedAt,'2026-11-02T01:00:05Z');
|
||||
ui.emit('tool_completed',{step:{status:'completed'}});assert.equal(ui.get().status,'RUNNING');
|
||||
assert.equal(ui.reads(),2);await ui.close();
|
||||
});
|
||||
test('late timing metadata cannot update a different selected turn',async()=>{
|
||||
const ui=fixture();ui.switchTurn();await ui.close();ui.firstRead.resolve({items:[{id:'t1',startedAt:'2026-11-02T01:00:05Z'}]});await tick();
|
||||
assert.equal(ui.get().startedAt,undefined);
|
||||
});
|
||||
test('metadata renders a waiting clock immediately and a spinning running clock after server start',()=>{
|
||||
const code=source.slice(source.indexOf('function AnalysisTurnMetadata('),source.indexOf('\nfunction isObjectRecord('));
|
||||
const Metadata=new Function('React','useState','useEffect','analysisIsRunning','isObjectRecord','LoadingOutlined',compile(code+'\nreturn AnalysisTurnMetadata;'))(React,initial=>[initial,()=>{}],()=>{},analysisIsRunning,isObjectRecord,props=>React.createElement('i',props));
|
||||
const now=Date.now();
|
||||
const waiting=renderToStaticMarkup(React.createElement(Metadata,{turn:{_uiSubmittedAt:new Date(now-30000).toISOString()},status:'SUBMITTING',compact:true}));
|
||||
assert.match(waiting,/提交中/);assert.match(waiting,/已等待 30秒/);
|
||||
const running=renderToStaticMarkup(React.createElement(Metadata,{turn:{startedAt:new Date(now-10000).toISOString()},status:'RUNNING',compact:true}));
|
||||
assert.match(running,/已运行 10秒/);assert.match(running,/analysis-runtime-spinner/);
|
||||
});
|
||||
|
||||
test('partial remains a backend state without an extra partial label in conversation metadata',()=>{
|
||||
const code=source.slice(source.indexOf('function AnalysisTurnMetadata('),source.indexOf('\nfunction isObjectRecord('));
|
||||
const Metadata=new Function('React','useState','useEffect','analysisIsRunning','isObjectRecord','LoadingOutlined',compile(code+'\nreturn AnalysisTurnMetadata;'))(React,initial=>[initial,()=>{}],()=>{},analysisIsRunning,isObjectRecord,()=>null);
|
||||
const html=renderToStaticMarkup(React.createElement(Metadata,{turn:{durationMs:6000},status:'PARTIAL',compact:true}));
|
||||
assert.doesNotMatch(html,/部分完成/);assert.match(html,/运行时长 6秒/);
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { originalAnalysisBody, originalAnalysisSections } from '../src/analysisOriginalBody.ts';
|
||||
const original='## 结论\n\n合格率下降来自两个不同的阶段。\n\n### 总体与阶段\n\n先说明投入规模。\n\n| 阶段 | 变化 |\n| --- | --- |\n| 黑片 | -0.30 pp |\n\n表格之后的完整解释不能挪走。[Q11111111]\n\n### 组织维度\n\n国内与海外结构不同。\n\n### 局限\n\n不能据此认定生产根因。';
|
||||
const legacy=()=>({status:'PARTIAL',result:{executionMode:'native_agent',answer:'已取得本轮统计结果,答案整理尚未完整完成。\n\n正式统计表',readingSummary:{lead:'已取得本轮统计结果,答案整理尚未完整完成。'}},plan:{timeline:[{kind:'assistant',text:'先进行查询。'},{kind:'tool_start',sequence:1},{kind:'tool_result',sequence:2},{kind:'assistant',sequence:3,text:original}]}});
|
||||
test('legacy native fallback restores the entire final model body instead of another summary',()=>{
|
||||
const body=originalAnalysisBody(legacy());assert.equal(body.source,'timeline');assert.equal(body.answer,original);
|
||||
assert.doesNotMatch(body.answer,/已取得本轮统计结果/);
|
||||
});
|
||||
test('new saved original body wins over an 8000-character transcript and remains complete',()=>{
|
||||
const data=legacy();const answer='正文'.repeat(6000);data.result.answer=answer;delete data.result.readingSummary;
|
||||
data.plan.timeline.at(-1).text=answer.slice(0,8000)+'…';assert.equal(originalAnalysisBody(data).answer,answer);
|
||||
});
|
||||
test('a capped historical transcript cannot masquerade as the complete original body',()=>{
|
||||
const data=legacy();data.plan.timeline.at(-1).text='原'.repeat(8000);
|
||||
assert.deepEqual(originalAnalysisBody(data),{answer:'',source:'unavailable'});
|
||||
});
|
||||
test('a failed native result with no formal evidence still renders its saved original body',()=>{
|
||||
assert.deepEqual(originalAnalysisBody({status:'FAILED',result:{executionMode:'native_agent',answer:original,results:[],answerQuality:{dataComplete:false}}}),{answer:original,source:'saved'});
|
||||
});
|
||||
test('earlier assistant progress, redacted answers, and unavailable originals are never promoted',()=>{
|
||||
const data=legacy();data.plan.timeline.pop();assert.deepEqual(originalAnalysisBody(data),{answer:'',source:'unavailable'});
|
||||
data.result.answer='';data.plan.timeline.push({kind:'assistant',text:original});assert.equal(originalAnalysisBody(data).answer,'');
|
||||
delete data.result.executionMode;assert.equal(originalAnalysisBody(data),null);
|
||||
});
|
||||
test('original Markdown lines, table placement and scope caveats stay in their exact order',()=>{
|
||||
const parts=originalAnalysisSections(original,[]);assert.equal(parts.map(p=>p.markdown).join('\n'),original);
|
||||
assert.ok(parts[1].markdown.indexOf('| 黑片')<parts[1].markdown.indexOf('表格之后'));
|
||||
assert.match(parts.at(-1).markdown,/不能据此认定生产根因/);
|
||||
});
|
||||
test('figures are inserted after the associated complete chapter without removing prose or tables',()=>{
|
||||
const visual={id:'stage',title:'阶段合格率',metric:'black_rate',type:'column',evidenceRefs:['Q11111111']};
|
||||
const sections=originalAnalysisSections(original,[visual]);assert.equal(sections[1].visuals[0],visual);
|
||||
assert.equal(sections.flatMap(s=>s.visuals).length,1);assert.equal(sections.map(s=>s.markdown).join('\n'),original);
|
||||
});
|
||||
|
||||
test('alternate historical evidence fallback also recovers native prose',()=>{
|
||||
const data=legacy();data.result.readingSummary.lead='现有证据不足以支持本轮完整分析,已保留可核验统计结果。';assert.equal(originalAnalysisBody(data).answer,original);
|
||||
});
|
||||
test('black and bright figures cannot be attached to the opposite stage chapter',()=>{
|
||||
const body='## 黑片阶段\n\n黑片完整解释。\n\n## 亮片阶段\n\n亮片完整解释。';
|
||||
const figures=[{id:'black',type:'column',stage:'black',metric:'black_rate',evidenceRefs:[]},{id:'bright',type:'column',stage:'bright',metric:'bright_rate',evidenceRefs:[]}];
|
||||
const sections=originalAnalysisSections(body,figures);assert.equal(sections[0].visuals[0].id,'black');assert.equal(sections[1].visuals[0].id,'bright');
|
||||
});
|
||||
@@ -274,17 +274,17 @@ test('history proof and lineage in model-controlled task fields cannot authorize
|
||||
assert.deepEqual(selectedHistoryIds(turn), []);
|
||||
});
|
||||
|
||||
test('running process starts folded, stays open after a user opens it, closes on completion, and ignores late running status', () => {
|
||||
test('running process starts open, honors manual collapse, and closes on completion', () => {
|
||||
const harness = hookHarness();
|
||||
const turn = { id: 'process-turn', status: 'RUNNING', steps: [{ tool: 'read', label: '读取数据', status: 'running' }] };
|
||||
let element = harness.render(turn);
|
||||
assert.equal(element.type, 'details');
|
||||
assert.equal(element.props.open, false);
|
||||
assert.equal(element.props.open, true);
|
||||
|
||||
element.props.onToggle({ currentTarget: { open: true } });
|
||||
element.props.onToggle({ currentTarget: { open: false } });
|
||||
turn.steps = [...turn.steps, { tool: 'mcp_call', label: '查询质量数据', status: 'running' }];
|
||||
element = harness.render(turn);
|
||||
assert.equal(element.props.open, true);
|
||||
assert.equal(element.props.open, false);
|
||||
|
||||
turn.status = 'COMPLETED';
|
||||
turn.steps = [...turn.steps, { tool: 'mcp_call', label: '查询质量数据', status: 'completed' }];
|
||||
@@ -293,5 +293,5 @@ test('running process starts folded, stays open after a user opens it, closes on
|
||||
|
||||
turn.status = 'RUNNING';
|
||||
element = harness.render(turn);
|
||||
assert.equal(element.props.open, false);
|
||||
assert.equal(element.props.open, true);
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { originalAnalysisBody, originalAnalysisSections } from '../src/analysisOriginalBody.ts';
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
@@ -22,7 +23,9 @@ const turn = (readingSummary = summary()) => ({ id: 'turn', question: '9月合
|
||||
releaseId: 'release', visuals: [chart], queryEvidenceRefs: ['Q11111111', 'Q22222222'], readingSummary,
|
||||
} });
|
||||
|
||||
let OriginalContent=()=>null;
|
||||
function component(source, name, bindings) {
|
||||
bindings={originalAnalysisBody,AnalysisOriginalContent:props=>React.createElement(OriginalContent,props),...bindings};
|
||||
const cleaned = source.replace(/^import .*;\n/gm, '').replace(/^export /gm, '');
|
||||
return new Function('React', ...Object.keys(bindings), ts.transpileModule(`${cleaned}\nreturn ${name};`, {
|
||||
fileName: `${name}.tsx`, compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React },
|
||||
@@ -30,6 +33,7 @@ function component(source, name, bindings) {
|
||||
}
|
||||
const markdownSource = readFileSync(new URL('../src/AiMarkdown.tsx', import.meta.url), 'utf8');
|
||||
const AiMarkdown = component(markdownSource, 'AiMarkdown', { sanitizeAnalysisDisplayText, analysisEvidenceParts });
|
||||
OriginalContent=component(readFileSync(new URL('../src/AnalysisOriginalContent.tsx',import.meta.url),'utf8'),'AnalysisOriginalContent',{AiMarkdown,originalAnalysisSections});
|
||||
|
||||
test('conclusion layout separates the lead, findings and table titles without rewriting checked facts', () => {
|
||||
const content = '9月综合合格率**95.13%**,较8月的**95.57%**下降**0.44个百分点**。主要来自**黑片阶段**,正式影响为**-0.30个百分点**;亮片影响**-0.15个百分点**。\n\n'
|
||||
@@ -81,7 +85,7 @@ const visibleSummary = html => html.match(/<div class="ai-markdown[^"]*">([\s\S]
|
||||
test('comparison readout uses the checked change, preserving rounded stage values and explicit scope', () => {
|
||||
const sentence='汽车交通2026年9月黑片合格率**97.27%**,较8月的**97.58%**下降**0.30个百分点**。';
|
||||
const value=checkedConclusionComparison(sentence);
|
||||
assert.equal(value.change,'0.30'); assert.equal(value.currentLabel,'汽车交通2026年9月');
|
||||
assert.equal(value.change,'-0.30'); assert.equal(value.currentLabel,'汽车交通2026年9月');
|
||||
assert.equal(value.previousLabel,'8月'); assert.equal(value.current,'97.27%'); assert.equal(value.previous,'97.58%');
|
||||
for (const invalid of ['9月合格率95.13%。','9月合格率95.13%,较8月95.57%。','9月合格率101.00%,较8月95.57%下降0.44个百分点。']) assert.equal(checkedConclusionComparison(invalid),null);
|
||||
assert.equal(checkedConclusionComparison('9月合格率0.00%,较8月95.57%下降95.57个百分点。').current,'0.00%');
|
||||
@@ -93,7 +97,7 @@ test('conclusion chapters put each authorized figure beside its cited topic, wit
|
||||
'**重点规格**:甲规格报废增加128.45 kg。[Q11111111]']});
|
||||
const figures=[{...chart,id:'department',metric:'rate_drop_pp',dimension:'department',evidenceRefs:['Q22222222']},{...chart,id:'product',metric:'scrap_delta',dimension:'product'}];
|
||||
const document=buildConclusionDocument(buildAnalysisReadingModel('',[],data.question,data).reading,figures);
|
||||
assert.equal(document.comparison.change,'0.44');
|
||||
assert.equal(document.comparison.change,'-0.44');
|
||||
assert.deepEqual(document.sections.find(x=>x.title==='产业部变化').visuals.map(v=>v.id),['department']);
|
||||
assert.deepEqual(document.sections.find(x=>x.title==='重点规格').visuals.map(v=>v.id),['product']);
|
||||
assert.deepEqual(document.sections.flatMap(x=>x.visuals.map(v=>v.id)),['department','product']);
|
||||
@@ -109,7 +113,7 @@ test('reference-style cards and chapters keep chat/report values, figures and or
|
||||
data.result.answer=data.result.readingSummary.lead+'\n\n'+data.result.readingSummary.evidence[0];
|
||||
for(const html of [renderChat(data),renderReport(data)]) {
|
||||
assert.match(html,/analysis-comparison-current[^>]*><span>2026年9月<\/span><strong>95.13%<\/strong>/);
|
||||
assert.match(html,/下降 0.44/); assert.match(html,/95.57%/);
|
||||
assert.match(html,/-0.44/); assert.match(html,/95.57%/);
|
||||
assert.equal((html.match(/<table>/g)||[]).length,1);
|
||||
assert.doesNotMatch(html,/Q11111111|Q22222222|关键依据|数据依据|analysis-full-appendix/);
|
||||
}
|
||||
|
||||
@@ -158,10 +158,10 @@ test('settled process is collapsed, with the original records retained inside',(
|
||||
assert.doesNotMatch(html,/<details class="analysis-process-history analysis-agent-process" open/);
|
||||
}
|
||||
});
|
||||
test('running process stays folded by default while retaining its current records inside',()=>{
|
||||
test('running process is expanded by default while retaining its current records inside',()=>{
|
||||
const html=renderToStaticMarkup(React.createElement(Process,{turn:processTurn('RUNNING')}));
|
||||
assert.match(html,/读取数据/);assert.match(html,/<details class="analysis-process-history analysis-agent-process"/);
|
||||
assert.doesNotMatch(html,/<details class="analysis-process-history analysis-agent-process" open/);
|
||||
assert.match(html,/<details class="analysis-process-history analysis-agent-process" open/);
|
||||
});
|
||||
|
||||
test('a submitted follow-up rearms following only when its own turn is rendered',()=>{
|
||||
@@ -329,7 +329,8 @@ test('agent process interleaves thoughts, tool calls and text with one runtime h
|
||||
]};
|
||||
const html=renderToStaticMarkup(React.createElement(Process,{turn,metadata:React.createElement('span',null,'运行时长 25秒')}));
|
||||
assert.equal((html.match(/运行时长/g)||[]).length,1);
|
||||
const positions=['先核对范围','我来查询两个期间。','读取数据','再整理结果'].map(value=>html.indexOf(value));
|
||||
assert.doesNotMatch(html,/我来查询两个期间。/);
|
||||
const positions=['先核对范围','读取数据','再整理结果'].map(value=>html.indexOf(value));
|
||||
assert.ok(positions.every((p,i)=>p>=0&&(!i||p>positions[i-1])));
|
||||
assert.equal((html.match(/class="analysis-thinking-block"/g)||[]).length,2);
|
||||
for(const hidden of ['model-hidden','任务理解','整理结论','analysis-thinking-heading','展示接口实际返回'])assert.ok(!html.includes(hidden),hidden);
|
||||
|
||||
@@ -182,7 +182,7 @@ test('unknown 503 submission renders recovery notice without claiming a queued o
|
||||
await ui.submit('question with unknown outcome');
|
||||
assert.equal(ui.get().attempt.uncertain,true);
|
||||
const html=renderDraft(ui.get().lastTurn);
|
||||
assert.match(html,/提交结果暂未确认,请重试上一问恢复原任务/);
|
||||
assert.doesNotMatch(html,/提交结果暂未确认/);
|
||||
assert.doesNotMatch(html,/排队中|正在准备分析|仍在运行/);
|
||||
assert.equal(ui.get().lastTurn.status,undefined);
|
||||
});
|
||||
@@ -192,7 +192,23 @@ test('an explicit 400 rejection renders an unsuccessful draft without a running
|
||||
await ui.submit('rejected question');
|
||||
assert.equal(ui.get().attempt,null);
|
||||
const html=renderDraft(ui.get().lastTurn);
|
||||
assert.match(html,/本次提交未成功,请检查错误提示后重试/);
|
||||
assert.doesNotMatch(html,/本次提交未成功/);
|
||||
assert.doesNotMatch(html,/排队中|正在准备分析|仍在运行|恢复原任务/);
|
||||
assert.equal(ui.get().lastTurn.status,undefined);
|
||||
});
|
||||
|
||||
|
||||
test('a fresh submission immediately carries waiting status and a clock before acceptance',async()=>{
|
||||
const pending=deferred();const ui=fixture({startAnalysisTurn:()=>pending.promise});
|
||||
const work=ui.submit('即时状态');assert.equal(ui.get().lastTurn.status,'SUBMITTING');
|
||||
assert.ok(Number.isFinite(Date.parse(ui.get().lastTurn._uiSubmittedAt)));
|
||||
pending.resolve({turn:{id:'accepted',status:'RUNNING'}});await work;
|
||||
assert.ok(Number.isFinite(Date.parse(ui.get().lastTurn._uiSubmittedAt)));
|
||||
});
|
||||
|
||||
test('retry starts a fresh waiting clock without displaying the old body or plan clock',async()=>{
|
||||
const response=deferred();const ui=fixture({retryAnalysisTurn:()=>response.promise,analysisTurn:async()=>({id:'old',status:'QUEUED'})});
|
||||
const work=ui.retryTurn({id:'old',status:'PARTIAL',answer:'old text',createdAt:'2020-01-01T00:00:00Z',startedAt:'2020-01-01T00:00:01Z',plan:{startedAt:'2020-01-01T00:00:01Z'}});
|
||||
assert.equal(ui.get().lastTurn.status,'SUBMITTING');assert.equal(ui.get().lastTurn.answer,undefined);assert.equal(ui.get().lastTurn.plan,undefined);
|
||||
response.resolve({turn:{id:'old',sessionId:'session-1',status:'QUEUED'}});await work;
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
visualUnitLabel,
|
||||
} from '../src/analysisVisualsModel.ts';
|
||||
import { buildAnalysisNarrative } from '../src/analysisReportModel.ts';
|
||||
import { buildConclusionDocument, checkedConclusionComparison } from '../src/analysisConclusionModel.ts';
|
||||
|
||||
function visual(overrides = {}) {
|
||||
return {
|
||||
@@ -305,6 +306,64 @@ test('negative percentage point changes remain signed and keep a zero baseline',
|
||||
assert.equal(formatVisualValue(0.5, 'pp'), '+0.5 pp');
|
||||
});
|
||||
|
||||
test('legacy rate-drop charts normalize once and chat tables and exports use current minus prior', () => {
|
||||
const legacy = visual({
|
||||
id: 'legacy-rate-drop', type: 'bar', title: '业务分组合格率降幅线索', subtitle: '8月 → 9月',
|
||||
unit: 'pp', metric: 'rate_drop_pp', dimension: 'group', sectionKey: 'group',
|
||||
categories: [{ key: 'g1', label: '分组一' }, { key: 'g2', label: '分组二' }],
|
||||
series: [{ key: 'rate_drop_pp', label: '合格率变化(pp)', values: [2.5, -4] }],
|
||||
evidenceRefs: ['evidence-1'], requiredFields: ['black_good_weight', 'black_total_weight'],
|
||||
scope: { department: '工业电机' }, period: { label: '2026-09' }, comparisonPeriod: { label: '2026-08' },
|
||||
coverage: { totalRows: 2, returnedRows: 2, truncated: false },
|
||||
note: '条形末端显示具体变化:正值为下降,负值为改善,单位是百分点。本图只按率降幅排序,未纳入投入规模。',
|
||||
});
|
||||
const model = parseAnalysisVisuals([legacy], 1).visuals[0];
|
||||
assert.deepEqual(model.series[0].values, [-2.5, 4]);
|
||||
assert.match(model.note, /本期减比较期/);
|
||||
assert.match(model.note, /仍按率降幅排序|只按率降幅排序/);
|
||||
assert.deepEqual(parseAnalysisVisuals([model], 1).visuals[0].series[0].values, [-2.5, 4]);
|
||||
|
||||
const legacyNotes = [
|
||||
'正值表示合格率下降,负值表示改善;百分点取自正式分组率差。',
|
||||
'正值表示综合合格率下降,负值表示改善;按正式综合率比较,不作为生产根因。',
|
||||
undefined,
|
||||
];
|
||||
for (const [index, note] of legacyNotes.entries()) {
|
||||
const snapshot = { ...legacy, id: `legacy-rate-drop-${index}`, note };
|
||||
const normalized = parseAnalysisVisuals([snapshot], 1).visuals[0];
|
||||
assert.deepEqual(normalized.series[0].values, [-2.5, 4]);
|
||||
assert.match(normalized.note, /本期减比较期/);
|
||||
assert.deepEqual(parseAnalysisVisuals([normalized], 1).visuals[0].series[0].values, [-2.5, 4]);
|
||||
}
|
||||
|
||||
const chart = buildAnalysisVisualOption(model);
|
||||
assert.deepEqual(chart.series[0].data, [-2.5, 4]);
|
||||
const table = renderVisualTableHtml(legacy);
|
||||
assert.match(table, /-2\.5 pp/);
|
||||
assert.match(table, /\+4 pp/);
|
||||
const exported = buildAnalysisVisualExportOption(model, 1280, 'report');
|
||||
assert.deepEqual(exported.option.series[0].data, [-2.5, 4]);
|
||||
|
||||
const modern = { ...legacy, id: 'current-rate-drop', note: '正值表示本期合格率上升,负值表示下降;变化值为本期减比较期。',
|
||||
series: [{ ...legacy.series[0], values: [-2.5, 4] }] };
|
||||
assert.deepEqual(parseAnalysisVisuals([modern], 1).visuals[0].series[0].values, [-2.5, 4]);
|
||||
|
||||
const reading = { lead: '分组变化', evidence: [], notices: [], evidenceRefs: ['evidence-1'],
|
||||
hasAppendix: false, fullAnswer: '### 业务分组变化\n\n合格率有变化。[evidence-1]', visualCount: 1 };
|
||||
const conclusion = buildConclusionDocument(reading, [model]);
|
||||
assert.deepEqual(conclusion.sections.find(section => section.title === '业务分组变化').visuals[0].series[0].values, [-2.5, 4]);
|
||||
});
|
||||
|
||||
test('checked conclusion comparisons give declines negative pp and increases positive pp', () => {
|
||||
const down = checkedConclusionComparison('9月综合合格率95.13%,较8月的95.57%下降0.44个百分点。');
|
||||
const up = checkedConclusionComparison('9月综合合格率95.57%,较8月的95.13%上升0.44个百分点。');
|
||||
assert.equal(down.change, '-0.44');
|
||||
assert.equal(up.change, '+0.44');
|
||||
assert.equal(down.direction, '下降');
|
||||
assert.equal(up.direction, '上升');
|
||||
assert.equal(checkedConclusionComparison('9月综合合格率95.13%,较8月的95.57%上升0.44个百分点。'), null);
|
||||
});
|
||||
|
||||
test('non-finite and out-of-range ratio values degrade to a table while retaining zero and null', () => {
|
||||
const parsed = parseAnalysisVisuals([visual({ series: [{ key: 'r', label: '率', values: [0, Number.NaN] }] })]);
|
||||
assert.equal(parsed.visuals.length, 1);
|
||||
|
||||
Reference in New Issue
Block a user