修复:DSH 自主查询、图片直显与报告稳定性

允许 Agent 在循环中查目录、修正查询和提问,保留实时权限检查及模型原始回答。
统一相同查询证据与图表资源;排名补齐阶段指标,修正 OR 重量筛选,并限制模型明细输入。
增加运行恢复、图表自适应坐标轴与两位小数,修复报告主图选择、重复图及边缘标签。

验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。
线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
This commit is contained in:
2026-11-12 16:14:20 +08:00
parent f1a2fbd494
commit bc19475b29
35 changed files with 983 additions and 114 deletions
@@ -1,11 +1,44 @@
package cn.yunsheng.quality.analysis;
import java.util.*;
import cn.yunsheng.quality.common.SelectionValues;
import tools.jackson.databind.json.JsonMapper;
/** Compiles one observed next step without changing the confirmed business task. */
final class AnalysisProtocolExploration {
static boolean nativeAgent(Map<String,Object> task){return "native_agent".equals(task.get("executionMode"));}
/** 启动仅绑定执行身份;业务名称和参数由每次工具调用确认。 */
static Map<String,Object> start(Map<String,Object> proposed) {
var task=new LinkedHashMap<>(proposed);
task.put("kind","analysis");task.put("filters",Map.of());task.put("needsClarification",false);
task.remove("queryPlan");task.remove("clarificationRequest");task.remove("directResponse");
return task;
}
/** 可变业务查询范围不能改变执行身份、发布版本或真实账号权限。 */
static Map<String,Object> withFrame(Map<String,Object> root,Map<String,Object> frame,Map<String,Object> catalog) {
if(!Boolean.TRUE.equals(root.get("agentDriven")))throw new IllegalArgumentException("当前执行未启用自主参数修正");
if(!Set.of("period","comparisonPeriod","filters").containsAll(frame.keySet())
||!(frame.get("period") instanceof Map<?,?>)||!(frame.get("filters") instanceof Map<?,?>))
throw new IllegalArgumentException("查询范围需要period对象和filters对象;请修正后重新调用工具");
var filters=AnalysisProtocolService.map(frame.get("filters"));
if(!Set.of("department","group","product","projectGroup","extraFilters","exclusions","includeUnassigned","columnFilters").containsAll(filters.keySet()))
throw new IllegalArgumentException("查询包含未支持的筛选字段,请先读取catalog");
for(var item:Map.of("department","departments","group","groups","product","products","projectGroup","projectGroups").entrySet()) {
Object value=filters.get(item.getKey());
if(value!=null) {
List<String> names;
try {names=SelectionValues.parse(Objects.toString(value));}
catch(RuntimeException invalid){throw new IllegalArgumentException("筛选值格式不正确,请读取catalog并使用返回的完整name");}
if(names.isEmpty()||!AnalysisProtocolService.list(catalog.get(item.getValue())).containsAll(names))
throw new IllegalArgumentException("筛选名称未匹配当前目录,请用catalog检索候选后修正或向用户提问:"+item.getKey());
}
}
var result=new LinkedHashMap<>(root);result.put("period",frame.get("period"));result.put("filters",filters);
result.put("stages",List.of());result.remove("comparisonPeriod");
if(frame.get("comparisonPeriod")!=null)result.put("comparisonPeriod",frame.get("comparisonPeriod"));
return result;
}
static AnalysisProtocolQueryPlan.Resolution compile(JsonMapper json,Map<String,Object> root,Map<String,Object> node,
Map<String,Object> inherited,String execution,String release,Map<String,Object> catalog) {
String purpose=Objects.toString(node.get("purpose"),"");
@@ -450,7 +450,7 @@ public class AnalysisProtocolService {
boolean clarification=Boolean.TRUE.equals(task.get("needsClarification"));
var state=store.state(a.sessionId());
Map<String,Object> retry=map(a.input().get("retryTask"));
if(!retry.isEmpty()) {
if(!retry.isEmpty()&&!Boolean.TRUE.equals(task.get("agentDriven"))) {
if(!Objects.equals(canonical(retry),canonical(task)))throw conflict("重试不能更改已确认任务,请新建分析轮次");
} else {
state=store.appendRevision(a.sessionId(),state.version(),clarification?AnalysisExecutionProtocolStore.Disposition.CLARIFICATION:AnalysisExecutionProtocolStore.Disposition.CONFIRMED,write(task));
@@ -989,7 +989,11 @@ public class AnalysisProtocolService {
lock(sessionForAttempt(attemptId));var a=require(attemptId,actor,true);
binding.current(a.sessionId(),a.turnId(),a.userId(),a.revision(),a.executionId());var root=task(a);
if(!autonomousSession(a.sessionId())||!AnalysisProtocolExploration.nativeAgent(root))throw denied("当前任务没有授权自主探索");
if(request==null||!Set.of("requestKey","node","parentQueryIds").containsAll(request.keySet())||!(request.get("requestKey") instanceof String key)||key.isBlank()||key.length()>128||!(request.get("node") instanceof Map<?,?>))throw conflict("自主步骤格式无效");
if(request==null||!Set.of("requestKey","node","parentQueryIds","frame").containsAll(request.keySet())||!(request.get("requestKey") instanceof String key)||key.isBlank()||key.length()>128||!(request.get("node") instanceof Map<?,?>))throw conflict("自主步骤格式无效");
if(request.containsKey("frame")) {
try {root=AnalysisProtocolExploration.withFrame(root,map(request.get("frame")),planCatalog(a,root,actor));}
catch(IllegalArgumentException invalid){throw conflict(invalid.getMessage());}
}
var parents=list(request.getOrDefault("parentQueryIds",List.of()));
if(!(request.getOrDefault("parentQueryIds",List.of()) instanceof List<?>)||parents.size()>8||new HashSet<>(parents).size()!=parents.size()||parents.stream().anyMatch(p->!(p instanceof String)))throw conflict("父查询证明格式无效");
String digest=hash(write(canonical(request)));
@@ -1777,6 +1781,11 @@ public class AnalysisProtocolService {
Map<String,Object> t=new LinkedHashMap<>(proposed==null?Map.of():proposed);
if(!Objects.equals(t.get("version"),1) || !(t.get("goal") instanceof String goal) || goal.isBlank() || goal.length()>1600
|| !(t.get("needsClarification") instanceof Boolean) || !Objects.equals(t.get("question"),question(a.turnId())))throw conflict("任务提案格式或所属轮次不正确");
if(Boolean.TRUE.equals(t.get("agentDriven"))) {
if(!AnalysisProtocolExploration.nativeAgent(t)||!autonomousSession(a.sessionId()))throw denied("当前会话没有自主执行权限");
options.options(a.releaseId(),actor);
return AnalysisProtocolExploration.start(t);
}
if("report".equals(t.get("kind")))return history.freeze(a,t,actor,autonomousSession(a.sessionId()));
Map<String,Object> available=options.options(a.releaseId(),actor);
if(!(t.get("filters") instanceof Map<?,?>))throw conflict("任务必须完整声明筛选对象");
@@ -6,6 +6,29 @@ import org.junit.jupiter.api.Test;
import tools.jackson.databind.json.JsonMapper;
class AnalysisProtocolExplorationTest {
@Test void unresolvedProductIsNotAnExecutionStartGate() {
var proposed=root();proposed.put("agentDriven",true);proposed.put("needsClarification",true);
proposed.put("filters",Map.of("product","18X15X4"));
var started=AnalysisProtocolExploration.start(proposed);
assertEquals(Map.of(),started.get("filters"));assertEquals(false,started.get("needsClarification"));
assertEquals(Map.of("product","18X15X4"),proposed.get("filters"));
}
@Test void agentCanCorrectQueryFrameButCannotSupplyExecutionIdentity() {
var task=root();task.put("agentDriven",true);
var frame=Map.<String,Object>of("period",Map.of("year",2025,"month",10),"filters",Map.of("product","P"));
var scoped=AnalysisProtocolExploration.withFrame(task,frame,catalog());
assertEquals(frame.get("period"),scoped.get("period"));assertEquals(frame.get("filters"),scoped.get("filters"));
assertEquals(Map.of("year",2026),task.get("period"));
var injected=new LinkedHashMap<>(frame);injected.put("releaseId","other");
assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.withFrame(task,injected,catalog()));
assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.withFrame(root(),frame,catalog()));
}
@Test void unknownQueryNameIsRecoverableAndCannotBeSignedAsAuthorizedData() {
var task=root();task.put("agentDriven",true);
var error=assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.withFrame(task,
Map.of("period",Map.of("year",2026),"filters",Map.of("product","18X15X4")),catalog()));
assertTrue(error.getMessage().contains("catalog"));
}
@Test void departmentAndGroupBreakdownCompileToOneImmutableMetricScope() {
for(String dimension:List.of("department","group")) {
var root=root();root.put("dimensions",List.of(dimension));