优化:未分类连续维护与柏拉图预汇总,同步已发布修复

This commit is contained in:
2026-11-01 12:40:50 +08:00
parent 572873c6b8
commit 38290f5fec
135 changed files with 17613 additions and 663 deletions
@@ -763,7 +763,7 @@ public class AnalysisChangesService {
}
private boolean validRate(BigDecimal value) {
return value != null && value.compareTo(BigDecimal.ZERO) >= 0 && value.compareTo(BigDecimal.ONE) <= 0;
return value != null && value.signum() >= 0;
}
private boolean positive(BigDecimal value) { return value != null && value.signum() > 0; }
@@ -9,7 +9,7 @@ import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.release.ReleaseService;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
@@ -18,8 +18,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@@ -63,6 +64,7 @@ public class AnalysisFineDefectsService {
private final GovernanceAccessService governance;
private final JsonMapper json;
private SourceColumnQueries sourceColumns;
private FineDefectRollups rollups;
public AnalysisFineDefectsService(JdbcTemplate jdbc, ReleaseService releases,
GovernanceAccessService governance, JsonMapper json) {
@@ -75,6 +77,9 @@ public class AnalysisFineDefectsService {
@org.springframework.beans.factory.annotation.Autowired(required = false)
public void setSourceColumns(SourceColumnQueries sourceColumns) { this.sourceColumns = sourceColumns; }
@org.springframework.beans.factory.annotation.Autowired(required = false)
public void setRollups(FineDefectRollups rollups) { this.rollups = rollups; }
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
public Map<String, Object> query(Map<String, Object> input, AppUserPrincipal actor) {
Set<String> allowed = governance.allowedFields(actor);
@@ -117,6 +122,11 @@ public class AnalysisFineDefectsService {
&& isAllowed(category.componentCode(), allowed)) componentCodes.add(category.componentCode());
}
Aggregator aggregate = new Aggregator(request, visibleFields, categories, componentCodes, json);
if (rollups != null && request.dimension().equals("defect") && request.extraFilters().isEmpty()
&& (!(input.get("columnFilters") instanceof List<?> filters) || filters.isEmpty())
&& rollups.read(aggregate, request, allowed, predicate.toString(), args)) {
return withScope(aggregate.response(request, release), scopeContext);
}
String sql = """
SELECT %s AS raw_fields, %s AS formal_components, f.group_name AS business_group%s
FROM release_member rm
@@ -127,14 +137,19 @@ public class AnalysisFineDefectsService {
""".formatted(jsonObject("f.raw_values", java.util.stream.Stream.concat(visibleFields.stream().map(DefectField::key),categories.stream().map(c->ExpectedSourceDetailPolicy.totalField(c.componentCode())).filter(java.util.Objects::nonNull).filter(k->isAllowed(k,allowed))).distinct().toList()),
jsonObject("f.components", new ArrayList<>(componentCodes)),
request.dimension().equals("product") ? ", f.product AS product_key" : "", factJoin, predicate);
jdbc.query(sql, (RowCallbackHandler) rs -> {
try {
aggregate.add(readObject(rs.getString("raw_fields")), readObject(rs.getString("formal_components")),
request.dimension().equals("product") ? rs.getString("product_key") : null,rs.getString("business_group"));
} catch (SQLException error) {
throw new IllegalStateException(error);
jdbc.query(connection -> {
var statement=connection.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
// Connector/J's negative fetch size enables row streaming instead of buffering the full year.
statement.setFetchSize(Integer.MIN_VALUE);
new ArgumentPreparedStatementSetter(args.toArray()).setValues(statement);
return statement;
}, (ResultSetExtractor<Void>) resultSet -> {
while(resultSet.next()) {
aggregate.add(readObject(resultSet.getString("raw_fields")), readObject(resultSet.getString("formal_components")),
request.dimension().equals("product") ? resultSet.getString("product_key") : null,resultSet.getString("business_group"));
}
}, args.toArray());
return null;
});
return withScope(aggregate.response(request, release), scopeContext);
}
@@ -311,14 +326,18 @@ public class AnalysisFineDefectsService {
}
}
private static String jsonObject(String column, List<String> keys) {
static String jsonObject(String column, List<String> keys) {
if (keys.isEmpty()) return "JSON_OBJECT()";
List<String> pairs = new ArrayList<>();
for (String key : keys) pairs.add("'" + key.replace("'", "''") + "', JSON_EXTRACT(" + column + ", '$.\"" + key + "\"')");
return "JSON_OBJECT(" + String.join(",", pairs) + ")";
}
private Map<String, Object> readObject(String value) {
Map<String, Object> readObject(String value) {
return readObject(json, value);
}
static Map<String, Object> readObject(JsonMapper json, String value) {
if (value == null || value.isBlank()) return Map.of();
JsonNode node = json.readTree(value);
if (node == null || !node.isObject()) return Map.of();
@@ -416,18 +435,31 @@ public class AnalysisFineDefectsService {
private long sourceRows;
private final Map<String,Long> expectedRows=new LinkedHashMap<>();
private final Map<String,BigDecimal> expectedWeights=new LinkedHashMap<>();
private final Map<String,List<String>> categoryKeys = new LinkedHashMap<>();
Aggregator(Request request, List<DefectField> fields, List<Category> categories, Set<String> componentCodes, JsonMapper json) {
this.request = request; this.fields = fields; this.categories = categories; this.componentCodes = componentCodes; this.json = json;
fields.forEach(field -> fieldStats.put(field.key(), new FieldStats()));
componentCodes.forEach(code -> componentStats.put(code, new FieldStats()));
for (Category category : categories) {
List<String> selected = fields.stream().filter(f -> f.category().equals(category.name())).map(DefectField::key).toList();
if (selected.size() == fields(request.stage()).stream().filter(f -> f.category().equals(category.name())).count())
categoryKeys.put(category.name(), selected);
}
}
void add(Map<String, Object> raw, Map<String, Object> components, String product) { add(raw,components,product,null); }
void add(Map<String, Object> raw, Map<String, Object> components, String product,String group) {
sourceRows++;
for(Category category:categories){List<String> selected=fields.stream().filter(f->f.category().equals(category.name())).map(DefectField::key).toList();long full=fields(request.stage()).stream().filter(f->f.category().equals(category.name())).count();String total=ExpectedSourceDetailPolicy.totalField(category.componentCode());
if(total!=null&&selected.size()==full&&ExpectedSourceDetailPolicy.applies(request.stage(),group,selected,raw,components.get(category.componentCode()),raw.get(total))){expectedRows.merge(category.name(),1L,Long::sum);expectedWeights.merge(category.name(),AnalysisInvestigationService.decimal(components.get(category.componentCode())),BigDecimal::add);}}
for (Category category : categories) {
String total = ExpectedSourceDetailPolicy.totalField(category.componentCode());
List<String> selected = categoryKeys.get(category.name());
if (total != null && selected != null && ExpectedSourceDetailPolicy.applies(request.stage(), group, selected, raw,
components.get(category.componentCode()), raw.get(total))) {
expectedRows.merge(category.name(), 1L, Long::sum);
expectedWeights.merge(category.name(), AnalysisInvestigationService.decimal(components.get(category.componentCode())), BigDecimal::add);
}
}
for (DefectField field : fields) {
FieldStats stats = fieldStats.get(field.key());
@@ -442,6 +474,34 @@ public class AnalysisFineDefectsService {
for (String code : componentCodes) componentStats.get(code).add(components.get(code));
}
record RollupStat(String kind, String key, BigDecimal sum, long present, long missing, long invalid, long negative, long rows) {}
List<RollupStat> rollupStats() {
List<RollupStat> result = new ArrayList<>();
fieldStats.forEach((key, s) -> result.add(new RollupStat("source", key, s.sum, s.presentRows, s.missingRows, s.invalidRows, s.negativeRows, sourceRows)));
componentStats.forEach((key, s) -> result.add(new RollupStat("formal", key, s.sum, s.presentRows, s.missingRows, s.invalidRows, s.negativeRows, sourceRows)));
for (Category category : categories) if (ExpectedSourceDetailPolicy.totalField(category.componentCode()) != null)
result.add(new RollupStat("expected", category.name(), expectedWeights.getOrDefault(category.name(), BigDecimal.ZERO),
expectedRows.getOrDefault(category.name(), 0L), 0, 0, 0, sourceRows));
return result;
}
void mergeRollup(RollupStat stat, Set<String> allowed) {
sourceRows = Math.max(sourceRows, stat.rows());
FieldStats stats = stat.kind().equals("source") ? fieldStats.get(stat.key()) : stat.kind().equals("formal") ? componentStats.get(stat.key()) : null;
if (stats != null) {
stats.sum = stats.sum.add(stat.sum()); stats.presentRows += stat.present(); stats.missingRows += stat.missing();
stats.invalidRows += stat.invalid(); stats.negativeRows += stat.negative();
}
if (stat.kind().equals("expected")) {
Category category = categories.stream().filter(c -> c.name().equals(stat.key())).findFirst().orElseThrow();
String total = ExpectedSourceDetailPolicy.totalField(category.componentCode());
if (categoryKeys.containsKey(category.name()) && componentCodes.contains(category.componentCode()) && isAllowed(total, allowed)) {
expectedRows.merge(category.name(), stat.present(), Long::sum); expectedWeights.merge(category.name(), stat.sum(), BigDecimal::add);
}
}
}
Map<String, Object> response(Request request, Map<String, Object> release) {
List<Map<String, Object>> rows = new ArrayList<>(request.dimension().equals("defect") ? defectRows() : productRows());
rows.sort(Comparator.<Map<String, Object>, BigDecimal>comparing(row -> (BigDecimal) row.get("weight"),
@@ -76,8 +76,11 @@ final class AnalysisProtocolChanges {
List<String> stages=selected instanceof String name?List.of(name):List.of("black","bright");int sourceStages=0;
for(String stage:stages) {
Map<String,Object> block=map(values.get(stage)),support=map(values.get("rateSupport"));
boolean sourced=selected!=null||Boolean.TRUE.equals(support.get(stage+"Measured"))
||List.of("rate","qualifiedWeight","scrapWeight","totalWeight").stream().anyMatch(field->decimal(block.get(field))!=null);
String measuredKey=stage+"Measured";
boolean hasMeasuredCoverage=support.containsKey(measuredKey);
boolean sourced=hasMeasuredCoverage?Boolean.TRUE.equals(support.get(measuredKey))
:selected!=null||List.of("rate","qualifiedWeight","scrapWeight","totalWeight").stream()
.anyMatch(field->decimal(block.get(field))!=null);
if(!sourced)continue;sourceStages++;
for(String field:List.of("totalWeight","scrapWeight"))if(fields.contains(field)) {
BigDecimal value=decimal(block.get(field));if(value==null||value.signum()<0)gaps.add("变化行"+side+"的"+stage+field+"缺失、不可见或非法");
@@ -110,7 +113,7 @@ final class AnalysisProtocolChanges {
}
private static boolean sortFields(List<?> fields,String sort){return Set.of("rate_drop","current_rate").contains(sort)?fields.contains("rate")
:Set.of("scrap_delta","current_scrap").contains(sort)&&fields.contains("scrapWeight");}
private static boolean validRate(Object value){BigDecimal rate=decimal(value);return rate!=null&&rate.signum()>=0&&rate.compareTo(BigDecimal.ONE)<=0;}
private static boolean validRate(Object value){BigDecimal rate=decimal(value);return rate!=null&&rate.signum()>=0;}
static BigDecimal decimal(Object value){if(!(value instanceof Number))return null;try{return new BigDecimal(value.toString());}catch(NumberFormatException invalid){return null;}}
private static boolean same(Object left,Object right){return Objects.equals(AnalysisProtocolService.canonical(left),AnalysisProtocolService.canonical(right));}
static boolean sameFilters(Map<String,Object> left,Map<String,Object> right){return FILTERS.containsAll(left.keySet())&&FILTERS.containsAll(right.keySet())
@@ -18,8 +18,11 @@ final class AnalysisProtocolExploration {
var rootStages=AnalysisProtocolService.list(root.get("stages"));var stages=AnalysisProtocolService.list(node.get("stages"));
boolean bothStages=new HashSet<>(rootStages).equals(Set.of("black","bright"));
boolean combinedRate=stages.isEmpty()&&bothStages&&Set.of("metrics","trend").contains(purpose)&&AnalysisProtocolService.list(node.get("fields")).equals(List.of("rate"));
boolean combinedRanking=stages.isEmpty()&&bothStages&&"ranking".equals(purpose)
&&AnalysisProtocolService.list(node.get("fields")).contains("scrapWeight")
&&Set.of("rate","scrapWeight").containsAll(AnalysisProtocolService.list(node.get("fields")));
boolean combinedChanges=stages.isEmpty()&&bothStages&&"changes".equals(purpose);
if(!rootStages.isEmpty()&&(!rootStages.containsAll(stages)||stages.isEmpty()&&!combinedRate&&!combinedChanges))throw new IllegalArgumentException("自主步骤不能扩大根任务阶段");
if(!rootStages.isEmpty()&&(!rootStages.containsAll(stages)||stages.isEmpty()&&!combinedRate&&!combinedRanking&&!combinedChanges))throw new IllegalArgumentException("自主步骤不能扩大根任务阶段");
// A combined rate read returns both stage baselines. Keep their coverage obligations explicit;
// the actual metrics request has no stage parameter. Combined changes retain stage=null.
var next=new LinkedHashMap<>(node);if(combinedRate)next.put("stages",List.of("black","bright"));
@@ -55,8 +58,11 @@ final class AnalysisProtocolExploration {
var base=scopes.stream().filter(s->sameFilters(root,AnalysisProtocolService.map(s.get("task")))).toList();
var stages=AnalysisProtocolService.list(root.get("stages"));List<?> requiredStages=stages.isEmpty()?List.of("overall"):stages;
for(Object field:AnalysisProtocolService.list(root.get("fields")))for(Object stage:requiredStages) {
var candidates=base.stream().filter(s->Set.of("metrics","trend").contains(s.get("purpose")))
.filter(s->{var child=AnalysisProtocolService.map(s.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));return AnalysisProtocolService.list(child.get("fields")).contains(field)&&("overall".equals(stage)?own.isEmpty():own.contains(stage));}).toList();
var candidates=base.stream().filter(s->Set.of("metrics","trend").contains(s.get("purpose"))||combinedRanking(root,s))
.filter(s->{var child=AnalysisProtocolService.map(s.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));
boolean combined=combinedRanking(root,s);
return AnalysisProtocolService.list(child.get("fields")).contains(field)
&&("overall".equals(stage)?own.isEmpty():own.contains(stage)||combined);}).toList();
if(candidates.isEmpty())gaps.add("根任务指标阶段尚未核实:"+field+"/"+stage);
if((Boolean.TRUE.equals(root.get("comparisonRequired"))||root.get("comparisonPeriod") instanceof Map<?,?>)&&candidates.stream().noneMatch(s->Boolean.TRUE.equals(AnalysisProtocolService.map(s.get("task")).get("comparisonRequired"))))gaps.add("根任务期间比较尚未核实:"+field+"/"+stage);
if(Boolean.TRUE.equals(root.get("trendRequired"))&&candidates.stream().noneMatch(s->"trend".equals(s.get("purpose"))))gaps.add("根任务趋势尚未核实:"+field+"/"+stage);
@@ -65,10 +71,20 @@ final class AnalysisProtocolExploration {
var matching=scopes.stream().filter(s->AnalysisProtocolService.list(AnalysisProtocolService.map(s.get("task")).get("dimensions")).contains(dimension)).toList();
var explicit=AnalysisProtocolService.list(AnalysisProtocolService.map(root.get("dimensionStages")).get(dimension));
if(explicit.isEmpty()){if(matching.isEmpty())gaps.add("根任务分组目标尚未核实:"+dimension);}
else for(Object stage:explicit)if(matching.stream().noneMatch(scope->AnalysisProtocolService.list(AnalysisProtocolService.map(scope.get("task")).get("stages")).contains(stage)))gaps.add("根任务分组阶段目标尚未核实:"+dimension+"/"+stage);
else for(Object stage:explicit)if(matching.stream().noneMatch(scope->AnalysisProtocolService.list(AnalysisProtocolService.map(scope.get("task")).get("stages")).contains(stage)||combinedRanking(root,scope)))gaps.add("根任务分组阶段目标尚未核实:"+dimension+"/"+stage);
}
for(String purpose:List.of("source","records"))if(Boolean.TRUE.equals(root.get(purpose.equals("source")?"verifySource":"fullDetails"))&&base.stream().noneMatch(s->purpose.equals(s.get("purpose"))))gaps.add("根任务原件目标尚未核实:"+purpose);
return gaps;
}
private static boolean combinedRanking(Map<String,Object> root,Map<String,Object> scope) {
if(!"ranking".equals(scope.get("purpose")))return false;
var child=AnalysisProtocolService.map(scope.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));
var rootStages=AnalysisProtocolService.list(root.get("stages"));var fields=AnalysisProtocolService.list(child.get("fields"));
boolean compatibleRoot=rootStages.isEmpty()||new HashSet<>(rootStages).equals(Set.of("black","bright"));
var rootDimensions=AnalysisProtocolService.list(root.get("dimensions"));var childDimensions=AnalysisProtocolService.list(child.get("dimensions"));
boolean compatibleDimension=rootDimensions.isEmpty()||childDimensions.stream().anyMatch(rootDimensions::contains);
return compatibleRoot&&compatibleDimension&&own.isEmpty()&&fields.contains("scrapWeight")
&&Set.of("rate","scrapWeight").containsAll(fields);
}
private static boolean sameFilters(Map<String,Object> root,Map<String,Object> child){return AnalysisProtocolService.canonical(AnalysisProtocolQueryPlan.identityFilters(AnalysisProtocolService.map(root.get("filters")))).equals(AnalysisProtocolService.canonical(AnalysisProtocolQueryPlan.identityFilters(AnalysisProtocolService.map(child.get("filters")))));}
}
@@ -12,6 +12,7 @@ import tools.jackson.databind.json.JsonMapper;
/** Server-owned import of frozen, same-session business responses into a distinct report execution. */
final class AnalysisProtocolHistory {
private static final String VERIFIED_RESULTS_ONLY = "verified_results_only";
@FunctionalInterface interface QueryValidator {
Map<String,Object> validate(Map<String,Object> source,Map<String,Object> query,Map<String,Object> response,AppUserPrincipal actor);
}
@@ -33,9 +34,11 @@ final class AnalysisProtocolHistory {
throw bad("历史报告只整理已核验的原范围;重新查询或改变范围需要新的分析任务");
boolean first="first".equals(proposed.get("historyMode"));
List<String> ids=jdbc.query("SELECT id,plan_json,status,answer_json FROM analysis_turn WHERE session_id=? AND user_id=? AND id<>? ORDER BY created_at,id",
(rs,n)->Map.of("id",rs.getString(1),"plan",read(rs.getString(2)),"status",Objects.toString(rs.getString(3),""),"answer",first?map(read(rs.getString(4))):Map.of()),a.sessionId(),a.userId(),a.turnId()).stream()
(rs,n)->Map.of("id",rs.getString(1),"plan",read(rs.getString(2)),"status",Objects.toString(rs.getString(3),""),"answer",map(read(rs.getString(4)))),a.sessionId(),a.userId(),a.turnId()).stream()
.filter(row->factTurn(map(row.get("plan"))))
.filter(row->!first||Set.of("COMPLETED","PARTIAL").contains(row.get("status"))&&!list(map(row.get("answer")).get("protocolQueryIds")).isEmpty())
.filter(row->{String status=Objects.toString(row.get("status"),"");List<?> accepted=list(map(row.get("answer")).get("protocolQueryIds"));
return first?Set.of("COMPLETED","PARTIAL").contains(status)&&!accepted.isEmpty()
:"COMPLETED".equals(status)||"PARTIAL".equals(status)&&!accepted.isEmpty();})
.map(row->Objects.toString(row.get("id"))).toList();
if(first&&!ids.isEmpty())ids=ids.subList(0,1);
if("last".equals(proposed.get("historyMode"))&&!ids.isEmpty())ids=ids.subList(ids.size()-1,ids.size());
@@ -49,6 +52,9 @@ final class AnalysisProtocolHistory {
for(String key:List.of("comparisonRequired","trendRequired","verifySource","fullDetails"))result.put(key,false);
result.put("historySourceTurnIds",List.copyOf(ids));
result.put("historySelectionHash",hash(selection(a,ids)));
// This mode is issued only by the server after it has resolved the canonical
// same-session source set. A client-supplied value is always overwritten.
result.put("historyResultMode",VERIFIED_RESULTS_ONLY);
return result;
}
@@ -89,26 +95,150 @@ final class AnalysisProtocolHistory {
String title=Objects.toString(details.get("reportTitle"),"");
boolean safeWorkerTitle=autonomous&&"verified_history_summary".equals(details.get("titleSource"))&&"历史质量分析汇总报告".equals(title)&&validTitle(title,payload,task);
boolean titled=(Boolean.TRUE.equals(details.get("titleGeneratedByModel"))||safeWorkerTitle)&&validTitle(title,payload,task);
List<String> gaps=new ArrayList<>(strings(map(payload.get("historyCoverage")).get("missingEvidence")));
if(!titled)gaps.add("报告标题尚未完成有效概括,保留已核实数据");
if(!facts.isEmpty()&&selected.isEmpty())gaps.add("报告重点尚未完成选择,保留完整数据明细");
var sourceCoverage=map(payload.get("historyCoverage"));boolean sourceIncomplete=!Boolean.TRUE.equals(sourceCoverage.get("complete"));
boolean selectionMissing=!facts.isEmpty()&&selected.isEmpty();
var bodyCompletion=map(details.get("evidenceCompletion"));
Object bodySufficient=bodyCompletion.containsKey("bodyEvidenceSufficient")
?bodyCompletion.get("bodyEvidenceSufficient"):bodyCompletion.get("modelDeclaredEvidenceSufficient");
boolean explanationIncomplete=autonomous&&Boolean.FALSE.equals(bodySufficient);
boolean serverFallback=autonomous&&(selectionMissing||explanationIncomplete);
boolean bodyVerified=!serverFallback;
var gaps=new ArrayList<String>();if(sourceIncomplete)gaps.add("部分已选历史来源未能完整核验。");
if(!titled)gaps.add("报告标题尚未生成或未通过内容核验。");
if(selectionMissing)gaps.add("报告重点未通过模型选择,已使用已核验事实生成简短提示。");
if(explanationIncomplete)gaps.add("正文解释未完成事实核验,已按已确认数据生成简短报告。");
var displayIds=new ArrayList<String>();for(Object id:selected)displayIds.add(id.toString());
if(autonomous&&selectionMissing)displayIds.addAll(fallbackHeadlineIds(facts));
List<Map<String,Object>> projected=list(payload.get("sourceResults")).stream().map(AnalysisProtocolHistory::map).toList();
var rendered=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,strings(selected),gaps));
if(!facts.isEmpty()&&selected.isEmpty())rendered.put("answer","已核验的历史数据已保留,重点结论尚未整理完成,可查看报告中的完整表格。");
var rendered=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,gaps));
if(!autonomous&&selectionMissing)
rendered.put("answer","已核验的历史数据已保留;重点结论尚未整理完成,请查看本报告表格。");
var reportVisuals=new ArrayList<Map<String,Object>>();
for(Object raw:list(payload.get("visuals"))) {var chart=new LinkedHashMap<>(map(raw));chart.put("originTurnId",a.turnId());reportVisuals.add(chart);}
rendered.put("releaseId",a.releaseId());rendered.put("visualSchemaVersion",payload.get("visualSchemaVersion"));
rendered.put("visualState",payload.get("visualState"));rendered.put("visuals",List.copyOf(reportVisuals));
rendered.put("historyVisualSources",payload.get("historyVisualSources"));
if(autonomous) {
var knownIds=new LinkedHashSet<String>();for(var fact:facts)if(fact.get("id") instanceof String id)knownIds.add(id);
var presented=AnalysisProtocolHistorySummary.normalize(details,knownIds,Objects.toString(rendered.get("answer"),""));
gaps.addAll(presented.gaps());rendered.put("answer",presented.answer());rendered.put("readingSummary",presented.summary());rendered.put("queryEvidenceRefs",presented.refs());
rendered.put("titleSource",safeWorkerTitle?"verified_history_summary":"model");rendered.put("titleGeneratedByModel",Boolean.TRUE.equals(details.get("titleGeneratedByModel")));
StringBuilder anchors=new StringBuilder("<details class=\"history-facts\"><summary>引用依据</summary><ul>");
for(var fact:facts) {String id=Objects.toString(fact.get("id"),"");anchors.append("<li id=\"history-fact:").append(escape(id)).append("\" data-history-fact-id=\"").append(escape(id)).append("\">").append(escape(Objects.toString(fact.get("text"),""))).append("</li>");}
anchors.append("</ul></details>");String report=Objects.toString(rendered.get("reportHtml"),"");int end=report.lastIndexOf("</article>");
if(end>=0)rendered.put("reportHtml",report.substring(0,end)+anchors+report.substring(end));
var finalGaps=new ArrayList<String>();
if(sourceIncomplete)finalGaps.add("部分已选历史来源未能完整核验");
if(!titled)finalGaps.add("报告标题尚未生成或未通过内容核验");
if(selectionMissing)finalGaps.add("重点结论未通过模型选择,已由已核验事实补充");
if(explanationIncomplete)finalGaps.add("正文解释未完成事实核验,已按已确认数据生成简短报告");
AnalysisProtocolHistorySummary.Result presented;
Map<String,Object> finalRender;
if(serverFallback) {
finalRender=AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,gaps);
String safeAnswer=Objects.toString(finalRender.get("reportMarkdown"),"");
String safeLead=Objects.toString(finalRender.get("answer"),"已确认历史事实已按原范围整理。");
var safeSummary=new LinkedHashMap<String,Object>();safeSummary.put("version",1);safeSummary.put("lead",safeLead);
safeSummary.put("evidence",List.of());safeSummary.put("notices",List.copyOf(finalGaps));
// Facts are anchored by the server-side render. Keep source fact IDs out of
// user-visible text and summary metadata; saved chart query refs are added below.
safeSummary.put("evidenceRefs",List.of());
presented=new AnalysisProtocolHistorySummary.Result(safeAnswer,safeSummary,List.of(),List.of());
} else {
var presentedModel=AnalysisProtocolHistorySummary.normalize(details,knownIds,anchoredFallback(facts,displayIds));
if(!presentedModel.gaps().isEmpty()){bodyVerified=false;finalGaps.add("部分解释内容未通过事实或格式核验,已过滤");}
finalRender=AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,finalGaps);
var safeSummary=new LinkedHashMap<String,Object>(presentedModel.summary());
var notices=new LinkedHashSet<String>(strings(safeSummary.get("notices")));notices.addAll(finalGaps);
safeSummary.put("notices",List.copyOf(notices));
presented=new AnalysisProtocolHistorySummary.Result(presentedModel.answer(),safeSummary,
presentedModel.refs(),presentedModel.gaps());
}
if(!finalGaps.isEmpty())gaps= new ArrayList<>(List.of(String.join(";",finalGaps)+",报告保持部分完成。"));
else gaps.clear();
rendered.put("answer",presented.answer());rendered.put("reportMarkdown",finalRender.get("reportMarkdown"));
rendered.put("reportHtml",finalRender.get("reportHtml"));rendered.put("historySections",finalRender.get("historySections"));
var summary=historyReadingSummary(presented.summary(),presented.refs(),reportVisuals,facts);
appendSourceDeliveryWarnings(summary,strings(payload.get("sourceDeliveryWarnings")));
rendered.put("readingSummary",summary);
rendered.put("queryEvidenceRefs",historyEvidenceRefs(presented.refs(),reportVisuals));
rendered.put("titleSource",titled?(safeWorkerTitle?"verified_history_summary":"model_verified_content"):"safe_partial_fallback");
rendered.put("titleGeneratedByModel",titled&&Boolean.TRUE.equals(details.get("titleGeneratedByModel")));
} else {
var summary=historyReadingSummary(Map.of(),List.of(),reportVisuals,facts);
appendSourceDeliveryWarnings(summary,strings(payload.get("sourceDeliveryWarnings")));
rendered.put("readingSummary",summary);
rendered.put("queryEvidenceRefs",historyEvidenceRefs(List.of(),reportVisuals));
}
rendered.put("historySources",payload.get("historySources"));rendered.put("historyCoverage",Map.of("complete",gaps.isEmpty()&&Boolean.TRUE.equals(map(payload.get("historyCoverage")).get("complete")),
"expectedSources",map(payload.get("historyCoverage")).get("expectedSources"),"importedSources",map(payload.get("historyCoverage")).get("importedSources"),"missingEvidence",gaps.stream().distinct().toList()));
rendered.put("historySources",payload.get("historySources"));var finalCoverage=new LinkedHashMap<String,Object>();
finalCoverage.put("complete",gaps.isEmpty()&&Boolean.TRUE.equals(sourceCoverage.get("complete")));
finalCoverage.put("expectedSources",sourceCoverage.get("expectedSources"));finalCoverage.put("importedSources",sourceCoverage.get("importedSources"));
finalCoverage.put("missingEvidence",gaps.stream().distinct().toList());
finalCoverage.put("sourceDeliveryWarnings",strings(payload.get("sourceDeliveryWarnings")));
rendered.put("historyCoverage",finalCoverage);
rendered.put("historyEvidence",identity(a,stored));rendered.put("completionMethod","confirmed_history_report");
rendered.put("reportTitle",titled?title:"历史质量分析汇总");return rendered;
rendered.put("reportTitle",titled?title:"历史质量分析汇总");
boolean titleRequired=!details.containsKey("titleRequired")||Boolean.TRUE.equals(details.get("titleRequired"));
boolean modelTitleValid=titleRequired&&titled&&Boolean.TRUE.equals(details.get("titleValid"))
&&Boolean.TRUE.equals(details.get("titleGeneratedByModel"));
rendered.put("titleRequired",titleRequired);rendered.put("titleValid",modelTitleValid);
if(autonomous) {
var completion=new LinkedHashMap<String,Object>(bodyCompletion);
completion.put("serverCoverageSufficient",Boolean.TRUE.equals(sourceCoverage.get("complete")));
completion.put("modelDeclaredEvidenceSufficient",bodyVerified);completion.put("bodyEvidenceSufficient",bodyVerified);
completion.put("bodyMissingEvidence",bodyVerified?List.of():strings(bodyCompletion.get("bodyMissingEvidence")).isEmpty()
?List.of(explanationIncomplete?"正文解释未完成事实核验。":"正文摘要未通过引用或格式核验。")
:strings(bodyCompletion.get("bodyMissingEvidence")));
completion.put("titleRequired",titleRequired);completion.put("titleValid",modelTitleValid);
completion.put("titleMissingEvidence",modelTitleValid?List.of():List.of("报告标题尚未生成或未通过内容核验。"));
rendered.put("evidenceCompletion",completion);
}
return rendered;
}
private List<String> fallbackHeadlineIds(List<Map<String,Object>> facts) {
Set<String> seenScopes=new HashSet<>();List<String> selected=new ArrayList<>();
Set<String> metrics=Set.of("rate","compositeRate","scrapRate","totalWeight","scrapWeight");
for(var fact:facts) {
if(!metrics.contains(Objects.toString(fact.get("metricCode"),""))||!(fact.get("id") instanceof String id))continue;
var source=map(fact.get("scope"));var scope=new LinkedHashMap<String,Object>();
for(String key:List.of("period","filters","dimension","stage"))if(source.containsKey(key))scope.put(key,source.get(key));
String key=write(canonical(scope));if(!seenScopes.add(key))continue;
selected.add(id);if(selected.size()==5)break;
}
return List.copyOf(selected);
}
private String anchoredFallback(List<Map<String,Object>> facts,List<String> ids) {
String sentence="已核验重点表格按各自期间与筛选保留,未合并不同范围。";
var anchors=new ArrayList<String>();
for(String id:ids)if(facts.stream().anyMatch(fact->Objects.equals(id,fact.get("id"))))anchors.add("["+id+"]");
return sentence+String.join("",anchors);
}
private Map<String,Object> historyReadingSummary(Map<String,Object> original,List<String> selectedRefs,
List<Map<String,Object>> visuals,List<Map<String,Object>> facts) {
var summary=new LinkedHashMap<String,Object>(original);var selected=new ArrayList<Map<String,Object>>();
Set<String> selectedIds=new HashSet<>(selectedRefs);
for(var fact:facts)if(selectedIds.contains(Objects.toString(fact.get("id"))))selected.add(fact);
if(!summary.containsKey("lead")) {
String lead=selected.isEmpty()?"已核验的历史质量结果已整理完成。":Objects.toString(selected.getFirst().getOrDefault("displayText",selected.getFirst().get("text")),"");
summary.put("version",1);summary.put("lead",lead);
summary.put("evidence",selected.stream().skip(1).map(f->Objects.toString(f.getOrDefault("displayText",f.get("text")),"")).filter(s->!s.isBlank()).limit(4).toList());
summary.put("notices",List.of());
}
Set<String> refs=new LinkedHashSet<>(strings(summary.get("evidenceRefs")));refs.addAll(selectedRefs);
for(var chart:visuals)for(Object ref:list(chart.get("evidenceRefs")))if(ref instanceof String value)refs.add(value);
summary.put("evidenceRefs",List.copyOf(refs));
var visualIds=visuals.stream().map(chart->Objects.toString(chart.get("id"),"")).filter(s->!s.isBlank()).limit(3).toList();
if(!visualIds.isEmpty())summary.put("visualIds",visualIds);
return summary;
}
private List<String> historyEvidenceRefs(List<String> factRefs,List<Map<String,Object>> visuals) {
var refs=new LinkedHashSet<String>(factRefs);
for(var chart:visuals)for(Object ref:list(chart.get("evidenceRefs")))if(ref instanceof String value)refs.add(value);
return List.copyOf(refs);
}
void appendSourceDeliveryWarnings(Map<String,Object> summary,List<String> warnings) {
if(warnings.stream().noneMatch(s->s!=null&&!s.isBlank()))return;
var notices=new LinkedHashSet<>(strings(summary.get("notices")));
notices.add("本报告沿用已核验的正式统计结果,未核验细项不纳入判断。");
summary.put("notices",List.copyOf(notices));
}
void authorizeSaved(String turnId,AppUserPrincipal actor,QueryValidator validator) {
@@ -123,7 +253,8 @@ final class AnalysisProtocolHistory {
private Map<String,Object> build(AnalysisProtocolService.Attempt a,Map<String,Object> task,AppUserPrincipal actor,QueryValidator validator) {
List<Map<String,Object>> sources=new ArrayList<>(),results=new ArrayList<>(),sections=new ArrayList<>(),facts=new ArrayList<>();
List<String> gaps=new ArrayList<>();long budget=0;
List<String> gaps=new ArrayList<>(),sourceDeliveryWarnings=new ArrayList<>();List<Map<String,Object>> visuals=new ArrayList<>(),visualSources=new ArrayList<>();Set<String> visualIds=new HashSet<>();
boolean visualFailed=false,visualUnavailable=false;long budget=0;
for(String id:sourceIds(task)) {
Map<String,Object> source=source(a,id,actor);List<Map<String,Object>> queries=new ArrayList<>();
Map<String,Object> meta=metadata(source);sources.add(meta);
@@ -132,6 +263,11 @@ final class AnalysisProtocolHistory {
List<?> accepted=list(map(source.get("result")).get("protocolQueryIds"));
if(accepted.isEmpty()||new HashSet<>(accepted).size()!=accepted.size())throw denied("该轮缺少最终接受的正式查询证明");
List<Map<String,Object>> all=queryRows(id);source.put("queryRecords",all);
Map<String,Object> answer=map(source.get("answer"));
boolean verifiedPartialResults="PARTIAL".equals(source.get("status"))
&&VERIFIED_RESULTS_ONLY.equals(task.get("historyResultMode"))
&&serverVerifiedPartialResults(source,accepted,all);
List<String> deliveryWarnings=verifiedPartialResults?partialDeliveryWarnings(source):List.of();
for(Object queryId:accepted) {
var query=all.stream().filter(q->Objects.equals(q.get("queryId"),queryId)).findFirst().orElseThrow(()->denied("历史查询记录已不存在"));
if(!Objects.equals(source.get("attemptId"),query.get("attemptId"))||!Objects.equals(source.get("executionId"),query.get("executionId"))
@@ -143,9 +279,38 @@ final class AnalysisProtocolHistory {
queries.add(validator.validate(source,query,response,actor));
}
source.put("queries",queries);
if("PARTIAL".equals(source.get("status"))&&AnalysisProtocolExploration.nativeAgent(map(source.get("task"))))source.put("gaps",strings(map(map(source.get("result")).get("executionCoverage")).get("missingEvidence")));
String priorVisualState=Objects.toString(answer.get("visualState"),"not_requested");
boolean sourceVisualRequested=Set.of("ready","failed").contains(priorVisualState);
if("unavailable".equals(priorVisualState))visualUnavailable=true;
var checkedVisuals=checkedSourceVisuals(answer,id,sourceVisualScope(source),actor);
if("ready".equals(checkedVisuals.state()))for(var chart:checkedVisuals.visuals()) {
var copy=new LinkedHashMap<>(chart);String chartId=Objects.toString(copy.get("id"),"");
if(!visualIds.add(chartId)) {
String suffix=AnalysisProtocolService.hash(id).substring(0,8);
copy.put("id",chartId.substring(0,Math.min(chartId.length(),110))+"-"+suffix);
visualIds.add(Objects.toString(copy.get("id")));
}
if(visuals.size()<12) {
visuals.add(copy);var lineage=new LinkedHashMap<String,Object>();
lineage.put("visualId",copy.get("id"));lineage.put("sourceTurnId",id);lineage.put("scope",copy.get("scope"));
lineage.put("period",copy.get("period"));lineage.put("evidenceRefs",copy.get("evidenceRefs"));
for(String key:List.of("comparisonPeriod","dimension","stage","metric"))lineage.put(key,copy.get(key));
visualSources.add(lineage);
}
}
else if(sourceVisualRequested)visualFailed=true;
if("PARTIAL".equals(source.get("status"))&&AnalysisProtocolExploration.nativeAgent(map(source.get("task")))) {
source.put("gaps",verifiedPartialResults?List.of():strings(map(map(source.get("result")).get("executionCoverage")).get("missingEvidence")));
if(verifiedPartialResults) {source.put("verifiedResultsMode",true);source.put("sourceDeliveryWarnings",deliveryWarnings);}
}
var result=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.buildSource(source,json));
result.put("sourceTurnId",id);result.put("task",source.get("task"));result.put("metadata",meta);
result.put("verifiedResultsMode",verifiedPartialResults);
if(!deliveryWarnings.isEmpty()) {
result.put("sourceDeliveryWarnings",deliveryWarnings);meta.put("sourceDeliveryWarnings",deliveryWarnings);
sourceDeliveryWarnings.addAll(deliveryWarnings);
}
result.put("sourceVisuals",checkedVisuals.visuals());
for(String key:List.of("status","revision","executionId"))result.put(key,source.get(key));
int sectionOffset=sections.size();
var displayFacts=new ArrayList<Map<String,Object>>();
@@ -155,7 +320,7 @@ final class AnalysisProtocolHistory {
results.add(result);sections.addAll(list(result.get("sections")).stream().map(AnalysisProtocolHistory::map).toList());
facts.addAll(displayFacts);
meta.put("available",true);meta.put("queryIds",List.copyOf(accepted));
if(!Boolean.TRUE.equals(result.get("complete"))||!"COMPLETED".equals(source.get("status")))gaps.add("历史轮次 "+id+" 尚未完整覆盖其原任务");
if(!Boolean.TRUE.equals(result.get("complete"))||!"COMPLETED".equals(source.get("status"))&&!verifiedPartialResults)gaps.add("历史轮次 "+id+" 尚未完整覆盖其原任务");
gaps.addAll(strings(result.get("gaps")));
} catch(RuntimeException invalid) {
meta.put("available",false);meta.put("reason",safeReason(invalid));gaps.add("历史轮次 "+id+":"+safeReason(invalid));
@@ -163,10 +328,83 @@ final class AnalysisProtocolHistory {
}
if(sources.isEmpty())gaps.add("当前会话没有可复用的历史正式分析");
var payload=new LinkedHashMap<String,Object>();payload.put("historySources",sources);payload.put("sourceResults",results);payload.put("sections",sections);payload.put("facts",facts);
payload.put("visualSchemaVersion",AnalysisVisualSnapshotAccess.SCHEMA_VERSION);
payload.put("visualState",visuals.isEmpty()?(visualFailed?"failed":visualUnavailable?"unavailable":"not_requested"):"ready");
payload.put("visuals",List.copyOf(visuals));payload.put("historyVisualSources",List.copyOf(visualSources));
payload.put("sourceDeliveryWarnings",sourceDeliveryWarnings.stream().filter(s->s!=null&&!s.isBlank()).distinct().toList());
payload.put("historyCoverage",Map.of("complete",gaps.isEmpty(),"expectedSources",sources.size(),"importedSources",results.size(),"missingEvidence",gaps.stream().distinct().toList()));
return payload;
}
/**
* A PARTIAL delivery may be reused only when the server's saved native
* coverage proves the accepted facts complete. This never substitutes for
* the per-query hash/scope/period/stage/ranking validation in build().
*/
private boolean serverVerifiedPartialResults(Map<String,Object> source,List<?> accepted,List<Map<String,Object>> queries) {
if(!AnalysisProtocolExploration.nativeAgent(map(source.get("task"))))return false;
Map<String,Object> coverage=map(map(source.get("result")).get("executionCoverage"));
if(!Boolean.TRUE.equals(coverage.get("factsComplete")))return false;
List<?> coveredIds=list(coverage.get("acceptedProtocolQueryIds"));
if(coveredIds.isEmpty()||!coveredIds.equals(accepted)||new HashSet<>(coveredIds).size()!=coveredIds.size())return false;
Map<String,Map<String,Object>> scopes=new HashMap<>();
for(Object raw:list(coverage.get("scopeCoverage"))) {
Map<String,Object> row=map(raw);String node=Objects.toString(row.get("nodeId"),""),scope=Objects.toString(row.get("scopeId"),"");
if(node.isBlank()||scope.isBlank())return false;
if(scopes.putIfAbsent(node+"\u0000"+scope,row)!=null)return false;
}
Map<Object,Map<String,Object>> queryById=new HashMap<>();
for(Map<String,Object> query:queries)if(queryById.putIfAbsent(query.get("queryId"),query)!=null)return false;
for(Object id:accepted) {
if(!(id instanceof String)||id.toString().isBlank())return false;
Map<String,Object> query=queryById.get(id);if(query==null)return false;
Map<String,Object> scope=scopes.get(Objects.toString(query.get("nodeId"),"")+"\u0000"+Objects.toString(query.get("scopeId"),""));
if(scope==null||!Boolean.TRUE.equals(scope.get("complete"))||!list(scope.get("queryIds")).contains(id))return false;
}
return true;
}
private List<String> partialDeliveryWarnings(Map<String,Object> source) {
Map<String,Object> coverage=map(map(source.get("result")).get("executionCoverage"));
var warnings=new LinkedHashSet<String>();
for(Object raw:list(coverage.get("explorationWarnings")))if(raw instanceof String text&&!text.isBlank())warnings.add(text.trim());
// Keep any non-fatal server coverage note visible as a limitation. It is
// not added to the facts/query gap list and cannot establish completeness.
for(Object raw:list(coverage.get("missingEvidence")))if(raw instanceof String text&&!text.isBlank())warnings.add(text.trim());
warnings.add("原分析轮次交付状态为 PARTIAL;本报告仅整理已核验的正式结果。");
warnings.add("未列入已接受正式查询的探索内容不作为本报告正式结论依据。");
return List.copyOf(warnings);
}
private Map<String,Object> sourceVisualScope(Map<String,Object> source) {
Map<String,Object> scope=new LinkedHashMap<>();scope.put("releaseId",Objects.toString(source.get("releaseId"),""));
Map<String,Object> task=map(source.get("task"));scope.putAll(map(task.get("filters")));
scope.putAll(map(task.get("period")));
if(!map(task.get("sourceFieldLabels")).isEmpty())scope.put("columnLabels",task.get("sourceFieldLabels"));
List<String> stages=strings(task.get("stages"));if(stages.size()==1)scope.put("stage",stages.getFirst());
List<String> dimensions=strings(task.get("dimensions"));if(dimensions.size()==1)scope.put("view",dimensions.getFirst());
return scope;
}
private AnalysisVisualSnapshotAccess.Snapshot checkedSourceVisuals(Map<String,Object> answer,String sourceTurnId,
Map<String,Object> expectedScope,AppUserPrincipal actor) {
Set<String> fields=governance.allowedFields(actor);
return checkSavedVisuals(answer,fields,AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc,fields),sourceTurnId,expectedScope);
}
static AnalysisVisualSnapshotAccess.Snapshot checkSavedVisuals(Map<String,Object> answer,Set<String> allowedFields,
Set<String> visibleFilterKeys,String sourceTurnId,Map<String,Object> expectedScope) {
var checked=AnalysisVisualSnapshotAccess.fromAnswer(answer,allowedFields,visibleFilterKeys,sourceTurnId,expectedScope);
if(!"ready".equals(checked.state()))return checked;
Set<String> signedRefs=new HashSet<>();
for(Object raw:list(answer.get("queryEvidenceRefs")))if(raw instanceof String ref&&ref.matches("Q[A-F0-9]{8}"))signedRefs.add(ref);
var visuals=checked.visuals().stream().filter(chart->{
List<?> refs=list(chart.get("evidenceRefs"));return !refs.isEmpty()&&signedRefs.containsAll(refs);
}).toList();
return visuals.isEmpty()?AnalysisVisualSnapshotAccess.empty("failed")
:new AnalysisVisualSnapshotAccess.Snapshot(AnalysisVisualSnapshotAccess.SCHEMA_VERSION,"ready",visuals);
}
/** A presentation recovery reads the original accepted responses, never a model/evidence file. */
Map<String,Object> recoverySource(AnalysisProtocolService.Attempt a,AppUserPrincipal actor,QueryValidator validator) {
requireAccess(a.releaseId(),actor);
@@ -204,6 +442,9 @@ final class AnalysisProtocolHistory {
var before=map(original.get("historyCoverage"));var after=map(current.get("historyCoverage"));
for(String key:List.of("complete","expectedSources","importedSources"))if(!Objects.equals(before.get(key),after.get(key)))
throw denied("历史来源覆盖状态已变化,请重新整理报告");
for(String key:List.of("visualSchemaVersion","visualState","visuals"))
if(!write(canonical(original.getOrDefault(key,"visualSchemaVersion".equals(key)?AnalysisVisualSnapshotAccess.SCHEMA_VERSION:"visuals".equals(key)?List.of():"not_requested")))
.equals(write(canonical(current.get(key)))))throw denied("历史图表来源或当前授权已变化,请重新整理报告");
return original;
}
@@ -226,7 +467,13 @@ final class AnalysisProtocolHistory {
||!Objects.equals(a.scopeHash(),AuthorizationFingerprint.of(actor,governance)))throw denied("历史来源发布版本或授权范围已变化");
if(!map(source.get("task")).isEmpty()&&!Objects.equals(source.get("taskHash"),hash(source.get("task"))))throw denied("源任务已变化");
if(!map(source.get("task")).isEmpty()&&!Objects.equals(canonical(map(source.get("plan")).get("task")),canonical(source.get("task"))))throw denied("源轮次与已确认任务不一致");
if(!Objects.equals(canonical(map(source.get("answer")).get("protocolQueryIds")),canonical(map(source.get("result")).get("protocolQueryIds"))))throw denied("源轮次的最终查询清单已变化");
// Failed/clarification turns are deliberately imported only as unavailable source metadata.
// Their turn answer may contain a failure explanation while the execution has no validated
// result payload. Business-source turns remain bound byte-for-byte to their accepted result.
if(Set.of("COMPLETED","PARTIAL").contains(source.get("status"))) {
if(!Objects.equals(canonical(source.get("answer")),canonical(source.get("result"))))throw denied("源轮次答复与已确认执行结果不一致");
if(!Objects.equals(canonical(map(source.get("answer")).get("protocolQueryIds")),canonical(map(source.get("result")).get("protocolQueryIds"))))throw denied("源轮次的最终查询清单已变化");
}
return source;
}
@@ -258,11 +505,195 @@ final class AnalysisProtocolHistory {
||!Objects.equals(task.get("historySelectionHash"),row.get("selectionHash")))throw denied("历史导入快照与当前报告任务不一致");
}
private Map<String,Object> decodeImport(Map<String,Object> row) {return exact.readValue(AnalysisProtocolEvidenceSnapshot.decode((byte[])row.get("payload"),((Number)row.get("bytes")).intValue(),Objects.toString(row.get("encoding"),null),Objects.toString(row.get("hash"),null)),Map.class);}
private Map<String,Object> visibleImport(AnalysisProtocolService.Attempt a,Map<String,Object> stored,Map<String,Object> payload) {var result=new LinkedHashMap<>(payload);result.remove("sourceResults");result.put("kind","protocol_history_snapshot");result.put("version",1);result.put("releaseId",a.releaseId());result.put("historyEvidence",identity(a,stored));return result;}
private Map<String,Object> visibleImport(AnalysisProtocolService.Attempt a,Map<String,Object> stored,Map<String,Object> payload) {
var result=modelVisiblePayload(payload);
result.put("kind","protocol_history_snapshot");result.put("version",1);
result.put("releaseId",a.releaseId());result.put("historyEvidence",identity(a,stored));return result;
}
static Map<String,Object> modelVisiblePayload(Map<String,Object> payload) {
var result=deepCopyMap(payload);
projectLegacyRankingDimensions(result);
projectSourceTaskContexts(result);
result.remove("sourceResults");
return result;
}
/** Keep only the selected source's signed business task, never its saved
* answer, execution material or complete result, in the model-visible copy. */
static int projectSourceTaskContexts(Map<String,Object> visiblePayload) {
Map<String,Map<String,Object>> sources=new HashMap<>();Set<String> duplicates=new HashSet<>();
for(Object raw:list(visiblePayload.get("sourceResults"))) {
Map<String,Object> source=mutableMap(raw);
String turn=Objects.toString(source==null?null:source.get("sourceTurnId"),"");
if(turn.isBlank())continue;
if(sources.putIfAbsent(turn,source)!=null)duplicates.add(turn);
}
duplicates.forEach(sources::remove);
Map<String,Integer> selectedCounts=new HashMap<>();
for(Object raw:list(visiblePayload.get("historySources"))) {
String turn=Objects.toString(map(raw).get("turnId"),"");
selectedCounts.merge(turn,1,Integer::sum);
}
int projected=0;
for(Object raw:list(visiblePayload.get("historySources"))) {
Map<String,Object> selected=mutableMap(raw);if(selected==null)continue;
selected.remove("taskContext");
String turn=Objects.toString(selected.get("turnId"),"");
Map<String,Object> source=sources.get(turn);
if(!Boolean.TRUE.equals(selected.get("available"))||selectedCounts.getOrDefault(turn,0)!=1||source==null
||!Set.of("COMPLETED","PARTIAL").contains(Objects.toString(selected.get("status"),""))
||!Objects.equals(selected.get("status"),source.get("status"))
||exactIndex(selected.get("revision"))<1
||exactIndex(selected.get("revision"))!=exactIndex(source.get("revision"))
||!(selected.get("executionId") instanceof String execution)||execution.isBlank()
||!execution.equals(source.get("executionId")))continue;
Map<String,Object> task=map(source.get("task"));
if(task.isEmpty())continue;
var context=new LinkedHashMap<String,Object>();
for(String key:List.of("period","filters","dimensions","fields","stages","comparisonPeriod",
"goal","rootGoal","question","sourceFieldLabels")) {
if(task.containsKey(key))context.put(key,deepCopyValue(task.get(key)));
}
if(task.get("queryPlan") instanceof List<?> nodes) {
var plan=new ArrayList<Map<String,Object>>();
for(Object rawNode:nodes) {
Map<String,Object> node=map(rawNode);var projectedNode=new LinkedHashMap<String,Object>();
for(String key:List.of("purpose","dimension","fields","stages","limit")) {
if(node.containsKey(key))projectedNode.put(key,deepCopyValue(node.get(key)));
}
if(!projectedNode.isEmpty())plan.add(projectedNode);
}
context.put("queryPlan",plan);
}
if(!context.isEmpty()) {selected.put("taskContext",context);projected++;}
}
return projected;
}
/**
* Old signed imports predate the structured ranking dimension. Restore it
* only in the model-visible copy, using the signed source task and the
* source's exact section/row fact identity. The stored payload and hash
* remain untouched.
*/
static int projectLegacyRankingDimensions(Map<String,Object> visiblePayload) {
List<?> rawSources=list(visiblePayload.get("sourceResults"));
Map<String,Map<String,Object>> sourcesByTurn=new HashMap<>();Set<String> duplicateTurns=new HashSet<>();
for(Object raw:rawSources) {
Map<String,Object> source=mutableMap(raw);String turn=Objects.toString(source==null?null:source.get("sourceTurnId"),"");
if(turn.isBlank())continue;
if(sourcesByTurn.putIfAbsent(turn,source)!=null)duplicateTurns.add(turn);
}
duplicateTurns.forEach(sourcesByTurn::remove);
List<?> sections=list(visiblePayload.get("sections")),facts=list(visiblePayload.get("facts"));
int projected=0;
for(int sectionIndex=0;sectionIndex<sections.size();sectionIndex++) {
Map<String,Object> section=mutableMap(sections.get(sectionIndex));if(section==null)continue;
String turn=Objects.toString(section.get("sourceTurnId"),"");Map<String,Object> source=sourcesByTurn.get(turn);
if(source==null)continue;
List<String> dimensions=strings(map(source.get("task")).get("dimensions"));
if(dimensions.size()!=1||!Set.of("product","department","group").contains(dimensions.getFirst()))continue;
String dimension=dimensions.getFirst();Object existingSectionDimension=section.get("dimension");
if(existingSectionDimension!=null&&!dimension.equals(existingSectionDimension))continue;
List<?> columns=list(section.get("columns"));Set<String> columnKeys=new HashSet<>();
for(Object rawColumn:columns)columnKeys.add(Objects.toString(map(rawColumn).get("key"),""));
List<?> rows=list(section.get("rows"));
if(!columnKeys.containsAll(Set.of("rank","label","weight"))||rows.isEmpty())continue;
List<?> sourceSections=list(source.get("sections"));int matchingSourceSections=0;
for(int i=0;i<sourceSections.size();i++)if(sameRankingSection(section,mutableMap(sourceSections.get(i)))) {
matchingSourceSections++;
}
if(matchingSourceSections!=1)continue;
List<?> sourceFacts=list(source.get("facts"));List<Map<String,Object>> rowFacts=new ArrayList<>();boolean valid=true;
for(int rowIndex=0;rowIndex<rows.size();rowIndex++) {
Map<String,Object> row=mutableMap(rows.get(rowIndex));if(row==null||exactIndex(row.get("rank"))<1
||!(row.get("label") instanceof String label)||label.isBlank()) {valid=false;break;}
List<Map<String,Object>> topMatches=new ArrayList<>();
for(Object rawFact:facts) {
Map<String,Object> fact=mutableMap(rawFact);
if(isBoundRankingFact(fact,turn,sectionIndex,rowIndex,section,row,dimension))topMatches.add(fact);
}
if(topMatches.size()!=1) {valid=false;break;}
Map<String,Object> topFact=topMatches.getFirst();Object factId=topFact.get("id");
List<Map<String,Object>> sourceMatches=new ArrayList<>();
for(Object rawFact:sourceFacts) {
Map<String,Object> fact=mutableMap(rawFact);
if(fact!=null&&Objects.equals(fact.get("id"),factId)
&&isBoundRankingFact(fact,turn,sectionIndex,rowIndex,section,row,dimension))sourceMatches.add(fact);
}
if(sourceMatches.size()!=1) {valid=false;break;}
rowFacts.add(topFact);
}
if(!valid||rowFacts.size()!=rows.size())continue;
section.put("dimension",dimension);
for(Map<String,Object> fact:rowFacts) {
Map<String,Object> scope=mutableMap(fact.get("scope"));
if(scope==null)continue;
if(scope.get("dimension")==null)scope.put("dimension",dimension);
fact.put("scope",scope);
}
projected++;
}
return projected;
}
private static boolean isBoundRankingFact(Map<String,Object> fact,String turn,int sectionIndex,int rowIndex,
Map<String,Object> section,Map<String,Object> row,String dimension) {
Object rawLabel=row==null?null:row.get("label");
if(fact==null||!Objects.equals(turn,fact.get("sourceTurnId"))||!Objects.equals("scrapWeight",fact.get("metricCode"))
||exactIndex(fact.get("sectionIndex"))!=sectionIndex||exactIndex(fact.get("rowIndex"))!=rowIndex
||!(rawLabel instanceof String label)||label.isBlank()||!Objects.toString(fact.get("text"),"").contains(label)
||!sameNumber(fact.get("rawValue"),row.get("weight")))return false;
Map<String,Object> scope=map(fact.get("scope"));
if(!Objects.equals(scope.get("period"),section.get("period"))
||!Objects.equals(scope.get("filters"),section.get("filters"))
||!Objects.equals(Objects.toString(scope.get("stage"),"overall"),Objects.toString(section.get("stage"),"overall")))return false;
Object factDimension=scope.get("dimension");return factDimension==null||dimension.equals(factDimension);
}
private static boolean sameRankingSection(Map<String,Object> first,Map<String,Object> second) {
if(first==null||second==null)return false;
for(String key:List.of("title","sourceTurnId","period","filters","stage","dimension","columns","rows"))
if(!Objects.equals(first.get(key),second.get(key)))return false;
return true;
}
private static boolean sameNumber(Object first,Object second) {
if(!(first instanceof Number)&&!(first instanceof String)||!(second instanceof Number)&&!(second instanceof String))return false;
try{return new java.math.BigDecimal(first.toString()).compareTo(new java.math.BigDecimal(second.toString()))==0;}
catch(NumberFormatException invalid){return false;}
}
private static int exactIndex(Object value) {
if(!(value instanceof Number))return -1;
try{return new java.math.BigDecimal(value.toString()).intValueExact();}
catch(NumberFormatException|ArithmeticException invalid){return -1;}
}
@SuppressWarnings("unchecked") private static Map<String,Object> mutableMap(Object raw) {
return raw instanceof Map<?,?>?(Map<String,Object>)raw:null;
}
private static Map<String,Object> deepCopyMap(Map<String,Object> raw) {
Object copy=deepCopyValue(raw);return mutableMap(copy);
}
private static Object deepCopyValue(Object value) {
if(value instanceof Map<?,?> source) {
Map<String,Object> copy=new LinkedHashMap<>();source.forEach((key,item)->{if(key instanceof String text)copy.put(text,deepCopyValue(item));});return copy;
}
if(value instanceof List<?> source) {List<Object> copy=new ArrayList<>();for(Object item:source)copy.add(deepCopyValue(item));return copy;}
return value;
}
private Map<String,Object> identity(AnalysisProtocolService.Attempt a,Map<String,Object> stored) {return Map.of("importId",stored.get("id"),"attemptId",a.attemptId(),"executionId",a.executionId(),"revision",a.revision(),"sessionId",a.sessionId(),"turnId",a.turnId(),"snapshotHash",stored.get("hash"));}
private Map<String,Object> metadata(Map<String,Object> source) {var m=new LinkedHashMap<String,Object>();for(String key:List.of("turnId","revision","executionId","status"))m.put(key,source.get(key));m.put("available",false);return m;}
private void requireAccess(String release,AppUserPrincipal actor) {governance.requireDetail(actor);governance.requireBoundAction("QUERY","query.detail");governance.requireBoundAction("QUERY","query.aggregate");options.options(release,actor);}
private boolean factTurn(Map<String,Object> plan) {var task=map(plan.get("task"));return !Boolean.TRUE.equals(task.get("needsClarification"))&&!task.containsKey("directResponse")&&!Set.of("report","explain","restate").contains(Objects.toString(task.get("kind"),""));}
private boolean factTurn(Map<String,Object> plan) {
var task=map(plan.get("task"));
String kind=Objects.toString(task.get("kind"),"");
return Set.of("analysis","metrics","members","source_verify","details",AnalysisProtocolInspection.KIND).contains(kind)
&&!Boolean.TRUE.equals(task.get("needsClarification"))&&!task.containsKey("directResponse");
}
boolean validTitle(String title,Map<String,Object> payload,Map<String,Object> task) {if(title.isBlank()||title.length()>100||title.equals(task.get("question"))||title.chars().anyMatch(c->c<32)||title.matches(".*[<>].*")||mentionsPartialScope(title,payload))return false;var numeric=java.util.regex.Pattern.compile("[0-9]+(?:\\.[0-9]+)?");Set<String> allowed=new HashSet<>();var input=numeric.matcher(write(payload));while(input.find())allowed.add(input.group());var proposed=numeric.matcher(title);while(proposed.find())if(!allowed.contains(proposed.group()))return false;return true;}
private boolean mentionsPartialScope(String title,Map<String,Object> payload) {
List<Set<String>> frames=new ArrayList<>();Set<String> excluded=new HashSet<>();
@@ -12,6 +12,8 @@ final class AnalysisProtocolHistoryFacts {
private static final Map<String,String> FIELD_LABELS = Map.of(
"rate", "合格率", "scrapRate", "报废率", "totalWeight", "投入重量", "scrapWeight", "报废重量",
"compositeRate", "综合合格率");
private static final java.util.regex.Pattern HISTORY_INTERNAL_METADATA = java.util.regex.Pattern.compile(
"(?is)(?:history-fact:|\\b(?:queryId|protocolQueryId|factId|evidenceRef|sourceTurnId|attemptId|executionId|snapshotHash|scopeHash)\\s*[:=]|\\bfilters\\s*=|\\{\\s*\")");
private static final JsonMapper HTML_JSON = JsonMapper.builder().build();
@@ -24,9 +26,15 @@ final class AnalysisProtocolHistoryFacts {
List<Map<String,Object>> facts = new ArrayList<>();
List<Map<String,Object>> metricContexts = new ArrayList<>();
List<String> gaps = new ArrayList<>();
List<String> sourceDeliveryWarnings = new ArrayList<>();
addStrings(gaps, source.get("gaps"));
String sourceStatus = text(source.get("status"), "");
if (Set.of("PARTIAL", "FAILED", "CANCELLED", "NEEDS_CLARIFICATION").contains(sourceStatus.toUpperCase(Locale.ROOT)))
boolean verifiedPartialResults = "PARTIAL".equalsIgnoreCase(sourceStatus)
&& Boolean.TRUE.equals(source.get("verifiedResultsMode"));
if (verifiedPartialResults) {
addStrings(sourceDeliveryWarnings, source.get("sourceDeliveryWarnings"));
sourceDeliveryWarnings.add("原分析轮次交付状态为 PARTIAL;本报告仅整理已核验的正式结果。");
} else if (Set.of("PARTIAL", "FAILED", "CANCELLED", "NEEDS_CLARIFICATION").contains(sourceStatus.toUpperCase(Locale.ROOT)))
gaps.add("该历史轮次状态为 " + sourceStatus + "。");
int[] sequence = {0};
@@ -81,6 +89,9 @@ final class AnalysisProtocolHistoryFacts {
.filter(s -> !s.isEmpty()).distinct().toList();
var result=mapOf("sections", List.copyOf(sections), "facts", List.copyOf(facts),
"complete", uniqueGaps.isEmpty(), "gaps", uniqueGaps);
List<String> uniqueWarnings=sourceDeliveryWarnings.stream().filter(Objects::nonNull).map(String::trim)
.filter(s->!s.isEmpty()).distinct().toList();
if(!uniqueWarnings.isEmpty())result.put("sourceDeliveryWarnings",uniqueWarnings);
if(!metricContexts.isEmpty())result.put("metricContexts",List.copyOf(metricContexts));
return result;
}
@@ -124,7 +135,7 @@ final class AnalysisProtocolHistoryFacts {
for (String field : fields) {
Object value = stages.isEmpty() ? summary.get("compositeRate") : map(summary.get(stage)).get(field);
String stageLabel = stages.isEmpty() ? "综合" : stageLabel(stage);
Map<String,Object> row = mapOf("stage", stageLabel, "field", fieldLabel(field), "value", value);
Map<String,Object> row = mapOf("stage", stageLabel, "field", fieldLabel(field), "metricCode",field,"value", value);
summaryRows.add(row);
addFact(facts, turnId, sequence, scopedText(period, filters, stageLabel + fieldLabel(field)
+ " = " + valueText(value)));
@@ -145,7 +156,7 @@ final class AnalysisProtocolHistoryFacts {
for (String stage : stages.isEmpty() ? List.of("") : stages) for (String field : fields) {
Object value = stages.isEmpty() ? raw.get("compositeRate") : map(raw.get(stage)).get(field);
projected.add(mapOf("key", raw.get("key"), "label", raw.get("label"),
"stage", stages.isEmpty() ? "综合" : stageLabel(stage), "field", fieldLabel(field), "value", value));
"stage", stages.isEmpty() ? "综合" : stageLabel(stage), "field", fieldLabel(field), "metricCode",field,"value", value));
addFact(facts, turnId, sequence, scopedText(period, filters,
text(raw.get("label"), text(raw.get("key"), "未命名分组")) + " · "
+ (stages.isEmpty() ? "综合" : stageLabel(stage)) + fieldLabel(field) + " = " + valueText(value)));
@@ -167,7 +178,7 @@ final class AnalysisProtocolHistoryFacts {
Object value = stages.isEmpty() ? raw.get("compositeRate") : map(raw.get(stage)).get(field);
String month = text(raw.get("month"), text(raw.get("period"), ""));
Map<String,Object> row = mapOf("month", month, "stage", stages.isEmpty() ? "综合" : stageLabel(stage),
"field", fieldLabel(field), "value", value);
"field", fieldLabel(field), "metricCode",field,"value", value);
projected.add(row);
addFact(facts, turnId, sequence, scopedText(period, filters, month + " · " + row.get("stage")
+ row.get("field") + " = " + valueText(value)));
@@ -194,7 +205,7 @@ final class AnalysisProtocolHistoryFacts {
Object value=map(summary.get(stage)).get(field);if(!(value instanceof Number))continue;
var componentTask=new LinkedHashMap<>(task);componentTask.put("fields",List.of(field));componentTask.put("stages",List.of(stage));componentTask.put("trendRequired",false);componentTask.put("periodCoverage","available");
if(!Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(response,componentTask,period).get("metricsComplete")))continue;
rows.add(mapOf("stage",stageLabel(stage),"field",fieldLabel(field),"value",value));
rows.add(mapOf("stage",stageLabel(stage),"field",fieldLabel(field),"metricCode",field,"value",value));
addFact(facts,turnId,sequence,scopedText(period,filters,stageLabel(stage)+fieldLabel(field)+" = "+valueText(value)));
displayFact(facts,sections.size(),rows.size()-1,period,filters,stage,field,value,null);facts.getLast().put("sourceKind","native_saved_aggregate");
}
@@ -210,41 +221,108 @@ final class AnalysisProtocolHistoryFacts {
Map<String,Object> period = period(scope, task, parameters);
if (!validPeriod(period)) gaps.add("正式分组排名缺少有效的已验证期间。");
Map<String,Object> filters = filters(scope, task);
String dimension=text(artifact.get("dimension"),"");
boolean validDimension=Set.of("product","department","group").contains(dimension);
List<String> allowedStages = strings(task.get("stages")), allowedFields = strings(task.get("fields"));
List<Map<String,Object>> rankings = maps(artifact.get("rankings"));
if (rankings.isEmpty()) { gaps.add("正式分组排名缺少已保存的 Top 快照。"); return; }
for (Map<String,Object> rank : rankings) {
String stage = text(rank.get("stage"), ""), metric = text(rank.get("metric"), "");
if (!allowedStages.contains(stage) || !allowedFields.contains(metric)
boolean overallCombinedScrap="overall".equals(stage)&&allowedStages.isEmpty()
&&"scrapWeight".equals(metric)&&allowedFields.contains("scrapWeight");
boolean stageScrap="scrapWeight".equals(metric)&&allowedStages.contains(stage)&&allowedFields.contains("scrapWeight");
if ((!overallCombinedScrap&&!allowedStages.contains(stage)) || !allowedFields.contains(metric)
|| !Boolean.TRUE.equals(rank.get("completeRanking"))) {
gaps.add("正式分组排名快照超出本次任务授权范围或未声明完整。");
continue;
}
if (rank.get("denominator") == null) gaps.add("正式分组排名缺少同范围正式分母。");
if((overallCombinedScrap||stageScrap)&&validEmptyScrapRanking(rank)) {
Object rate=rank.get("compositeRate");
String stageText=overallCombinedScrap?"综合" : stageLabel(stage);
String fact="所选范围"+stageText+"正式报废总重量为 0,没有正报废重量分组可排名。";
if(overallCombinedScrap&&rate instanceof Number)fact+=" 综合合格率沿用正式值 = "+valueText(rate)+"。";
else if(overallCombinedScrap&&allowedFields.contains("rate"))gaps.add("合计排名没有可引用的正式综合合格率;未按缺失阶段补值。");
addFact(facts,turnId,sequence,scopedText(period,filters,fact));
displayFact(facts,sections.size(),-1,period,filters,stage,"scrapWeight",rank.get("denominator"),null);
if(validDimension)addRankingDimension(facts.getLast(),dimension);
var emptyRow=new LinkedHashMap<String,Object>();emptyRow.put("message","没有正报废重量分组可排名");
List<Map<String,Object>> emptyColumns=new ArrayList<>(columns("message","结果"));
if(overallCombinedScrap&&rate instanceof Number) {emptyRow.put("compositeRate",rate);emptyColumns.addAll(columns("compositeRate","综合合格率"));}
String note="正式分母为 0;原始完整分组覆盖已验证,空榜不代表缺失来源数据。";
if(overallCombinedScrap&&rate instanceof Number)note+="综合合格率沿用同一正式响应的综合值,没有推算阶段率。";
var emptySection=section((validDimension?dimension:"分组")+"报废重量排名",turnId,period,filters,overallCombinedScrap?null:stage,emptyColumns,List.of(emptyRow),note);
if(validDimension)emptySection.put("dimension",dimension);
sections.add(emptySection);
continue;
}
int limit = integer(artifact.get("displayLimit"), 10);
List<Map<String,Object>> top = maps(rank.get("rows"));
if (limit > 0 && top.size() > limit) top = top.subList(0, limit);
if(top.isEmpty()) {gaps.add("正式分组排名没有可展示的正重量分组。");continue;}
List<Map<String,Object>> projected = new ArrayList<>();
int shown = top.size();
for (Map<String,Object> raw : top) {
Map<String,Object> row = mapOf("rank", raw.get("rank"), "key", raw.get("key"),
"label", raw.get("label"), "weight", raw.get("weight"));
if(overallCombinedScrap)for(String key:List.of("compositeRate","blackScrapWeight","brightScrapWeight",
"blackTotalWeight","brightTotalWeight","share","cumulativeShare"))
if(raw.containsKey(key))row.put(key,raw.get(key));
projected.add(row);
addFact(facts, turnId, sequence, scopedText(period, filters,
"Top " + rankBoundary(limit, shown) + " " + text(raw.get("label"), text(raw.get("key"), "未命名分组"))
+ " · " + stageLabel(stage) + fieldLabel(metric) + " = " + valueText(raw.get("weight"))
+ ";同范围正式分母 = " + valueText(rank.get("denominator"))));
StringBuilder factText=new StringBuilder("Top ").append(rankBoundary(limit,shown)).append(' ')
.append(text(raw.get("label"),text(raw.get("key"),"未命名分组"))).append(" · ")
.append(stageLabel(stage)).append(fieldLabel(metric)).append(" = ").append(valueText(raw.get("weight")));
if(overallCombinedScrap) {
appendArtifactFact(factText,"综合合格率",raw.get("compositeRate"));
appendArtifactFact(factText,"黑片报废重量",raw.get("blackScrapWeight"));
appendArtifactFact(factText,"亮片报废重量",raw.get("brightScrapWeight"));
appendArtifactFact(factText,"黑片投入重量",raw.get("blackTotalWeight"));
appendArtifactFact(factText,"亮片投入重量",raw.get("brightTotalWeight"));
appendArtifactFact(factText,"占同范围全部报废",raw.get("share"));
appendArtifactFact(factText,"累计占比",raw.get("cumulativeShare"));
}
factText.append(";同范围正式分母 = ").append(valueText(rank.get("denominator")));
addFact(facts, turnId, sequence, scopedText(period, filters, factText.toString()));
displayFact(facts,sections.size(),projected.size()-1,period,filters,stage,metric,raw.get("weight"),null);
if(validDimension)addRankingDimension(facts.getLast(),dimension);
}
String dimension = text(artifact.get("dimension"), "分组");
sections.add(section("Top " + rankBoundary(limit, shown) + " " + dimension + "排名", turnId, period, filters,
stage, columns("rank", "排名", "key", "分组键", "label", "分组", "weight", "重量"), projected,
"正式分母(" + stageLabel(stage) + fieldLabel(metric) + ")为 " + valueText(rank.get("denominator"))
+ "。此表只表示已保存的 Top " + rankBoundary(limit, shown) + "(本次展示 " + shown
+ " 条),不代表全量分组;重量沿用来源单位。"));
List<Map<String,Object>> rankingColumns=new ArrayList<>(columns("rank","排名","key","分组键","label","分组","weight",overallCombinedScrap?"报废总重(原单位)":"重量"));
if(overallCombinedScrap)rankingColumns.addAll(columns("compositeRate","综合合格率","blackScrapWeight","黑片报废重量(原单位)",
"brightScrapWeight","亮片报废重量(原单位)","blackTotalWeight","黑片投入重量(原单位)",
"brightTotalWeight","亮片投入重量(原单位)","share","占同范围全部报废","cumulativeShare","累计占比"));
String note="正式分母(" + stageLabel(stage) + fieldLabel(metric) + ")为 " + valueText(rank.get("denominator"))
+ "。此表只表示已保存的 Top " + rankBoundary(limit, shown) + "(本次展示 " + shown
+ " 条),不代表全量分组;重量沿用来源单位。";
if(overallCombinedScrap)note+="综合合格率沿用同一正式响应返回值;阶段重量、投入重量及占比仅展示排名快照保存值。";
var rankingSection=section("Top " + rankBoundary(limit, shown) + " " + (validDimension?dimension:"分组") + "排名", turnId, period, filters,
overallCombinedScrap?null:stage, rankingColumns, projected, note);
if(validDimension)rankingSection.put("dimension",dimension);
sections.add(rankingSection);
}
}
private static void addRankingDimension(Map<String,Object> fact,String dimension) {
Map<String,Object> scope=new LinkedHashMap<>(map(fact.get("scope")));scope.put("dimension",dimension);fact.put("scope",scope);
}
private static boolean validEmptyScrapRanking(Map<String,Object> rank) {
Object denominator=rank.get("denominator"), rows=rank.get("rows");
if(!(denominator instanceof Number number))return false;
try {
return new BigDecimal(number.toString()).signum()==0
&&"scrapWeight".equals(rank.get("metric"))
&&"no_positive_scrap".equals(rank.get("emptyReason"))
&&integer(rank.get("totalRows"),-1)==0&&integer(rank.get("returnedRows"),-1)==0
&&Boolean.TRUE.equals(rank.get("completeRanking"))
&&"weight_desc_key_codepoint_asc".equals(rank.get("orderPolicy"))
&&Boolean.FALSE.equals(rank.get("truncated"))&&rows instanceof List<?> list&&list.isEmpty();
} catch(NumberFormatException invalid) {return false;}
}
private static void appendArtifactFact(StringBuilder text,String label,Object value) {
if(value!=null)text.append(";").append(label).append(" = ").append(valueText(value));
}
private static void buildChanges(String turnId, Map<String,Object> task, Map<String,Object> scope,
Map<String,Object> parameters, Map<String,Object> response, Map<String,Object> coverage,
List<Map<String,Object>> sections, List<Map<String,Object>> facts, List<String> gaps, int[] sequence, JsonMapper json) {
@@ -507,15 +585,24 @@ final class AnalysisProtocolHistoryFacts {
allSections.add(copy);
}
allFacts.addAll(maps(source.get("facts")));
addStrings(allGaps, source.get("gaps"));
if (Boolean.FALSE.equals(source.get("complete"))) allGaps.add("历史来源 " + turn + " 存在未完成范围。");
if(historical) {
if(Boolean.FALSE.equals(source.get("complete")))allGaps.add("部分已选历史来源存在未完成范围。");
} else {
addStrings(allGaps, source.get("gaps"));
if (Boolean.FALSE.equals(source.get("complete"))) allGaps.add("历史来源 " + turn + " 存在未完成范围。");
}
}
List<Map<String,Object>> renderedSections=historical?uniqueSections(allSections):allSections;
Map<String,Map<String,Object>> factsById = new LinkedHashMap<>();
for (Map<String,Object> fact : allFacts) factsById.put(text(fact.get("id"), ""), fact);
List<Map<String,Object>> headlines = new ArrayList<>();
Set<String> headlineTexts=new HashSet<>();
if (headlineFactIds != null) for (String id : headlineFactIds) {
Map<String,Object> fact = factsById.get(id);
if (fact != null && !text(fact.get("text"), "").isBlank() && !headlines.contains(fact)) headlines.add(fact);
String headline=fact==null?"":headlineText(fact,allSections,sources,historical);
if (fact != null && !headline.isBlank() && headlineTexts.add(headline)) {
var presented=new LinkedHashMap<>(fact);presented.put("_displayText",headline);headlines.add(presented);
}
}
String answer;
if (headlines.isEmpty()) {
@@ -523,43 +610,91 @@ final class AnalysisProtocolHistoryFacts {
} else {
List<String> lines = new ArrayList<>();
for (Map<String,Object> fact : headlines) {
String text = text(fact.get("text"), "");
if (!text.isBlank()) lines.add("- " + md(text(fact.get("sourceTurnId"), "历史来源")) + ":" + md(text));
String text = text(fact.get("_displayText"), text(fact.get("displayText"), Objects.toString(fact.get("text"), "")));
if (!text.isBlank()) lines.add("- " + (historical?"":md(text(fact.get("sourceTurnId"), "历史来源"))+":") + md(text));
}
answer = lines.isEmpty() ? "没有可引用的已校验历史数值事实。" : String.join("\n", lines);
}
String markdown = renderMarkdown(safeTitle, headlines, allSections, allGaps, sources, historical);
String html = renderHtml(safeTitle, headlines, allSections, allGaps, sources, historical);
String markdown = renderMarkdown(safeTitle, headlines, renderedSections, allGaps, sources, historical);
String html = renderHtml(safeTitle, headlines, renderedSections, allGaps, sources, historical);
return mapOf("answer", answer, "reportMarkdown", markdown, "reportHtml", html,
"historySections", List.copyOf(allSections));
"historySections", List.copyOf(renderedSections));
}
private static String headlineText(Map<String,Object> fact,List<Map<String,Object>> sections,
List<Map<String,Object>> sources,boolean historical) {
if(!historical)return text(fact.get("text"),"");
Object rawIndex=fact.get("sectionIndex"),rawRow=fact.get("rowIndex");
if(rawIndex instanceof Number sectionNumber&&rawRow instanceof Number rowNumber) {
int sectionIndex=sectionNumber.intValue(),rowIndex=rowNumber.intValue();
if(sectionIndex>=0&&sectionIndex<sections.size()) {
var section=sections.get(sectionIndex);var rows=maps(section.get("rows"));
if(rowIndex>=0&&rowIndex<rows.size()) {
var row=rows.get(rowIndex);String metric=text(fact.get("metricCode"),"");
String scope=historyScopeText(section,sources);
if("scrapWeight".equals(metric)&&row.containsKey("weight")) {
String unit=verifiedWeightUnit(section,sources);var parts=new ArrayList<String>();
String label=text(row.get("label"),"已确认对象");
parts.add(scope+" · "+label+"报废重量 "+renderedValue(section,Map.of("key","weight"),row,unit));
if(row.containsKey("compositeRate"))parts.add("综合合格率 "+renderedValue(section,Map.of("key","compositeRate"),row,unit));
if(row.containsKey("share"))parts.add("占同范围全部报废 "+renderedValue(section,Map.of("key","share"),row,unit));
if(row.containsKey("cumulativeShare"))parts.add("累计占比 "+renderedValue(section,Map.of("key","cumulativeShare"),row,unit));
return String.join(";",parts);
}
if(row.containsKey("value")) {
String value=renderedValue(section,Map.of("key","value"),row,verifiedWeightUnit(section,sources));
return scope+" · "+fieldLabel(text(row.get("field"),"指标"))+" = "+value;
}
}
}
}
String display=text(fact.get("displayText"),text(fact.get("text"),""));
if(hasHistoryInternalMetadata(display))return "";
return display;
}
private static List<Map<String,Object>> uniqueSections(List<Map<String,Object>> sections) {
Set<String> signatures=new HashSet<>();List<Map<String,Object>> unique=new ArrayList<>();
for(Map<String,Object> section:sections) {
Map<String,Object> content=new LinkedHashMap<>();
for(String key:List.of("title","period","filters","stage","columns","rows","note"))
if(section.containsKey(key))content.put(key,section.get(key));
if(signatures.add(HTML_JSON.writeValueAsString(content)))unique.add(section);
}
return unique;
}
private static String renderMarkdown(String title, List<Map<String,Object>> headlines, List<Map<String,Object>> sections, List<String> gaps,
List<Map<String,Object>> sources, boolean historical) {
StringBuilder out = new StringBuilder("# ").append(md(title)).append("\n\n");
out.append(historical?"本报告按每个历史来源的原查询范围分别列示;不同期间与重叠总体没有合并或求均值。\n\n":"本报告依据本轮受控正式类别查询;范围为当前发布版本全部已发布期间,不含未分类。\n\n");
for (Map<String,Object> source : sources) out.append("- 来源轮次:").append(md(sourceReference(source)))
.append(";状态:").append(md(text(source.get("status"), "未知"))).append("\n");
if (!sources.isEmpty()) out.append('\n');
if(!historical) {
for (Map<String,Object> source : sources) out.append("- 来源轮次:").append(md(sourceReference(source)))
.append(";状态:").append(md(text(source.get("status"), "未知"))).append("\n");
if (!sources.isEmpty()) out.append('\n');
}
if (!headlines.isEmpty()) {
out.append("## 重点结论\n\n");
for (Map<String,Object> fact : headlines) out.append("- ").append(md(text(fact.get("text"), ""))).append('\n');
for (Map<String,Object> fact : headlines) out.append("- ").append(md(text(fact.get("_displayText"),text(fact.get("text"), "")))).append('\n');
out.append('\n');
}
for (Map<String,Object> section : sections) {
out.append("## ").append(md(text(section.get("title"), "历史数据"))).append("\n\n");
out.append("范围:").append(md(scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
.append("\n\n来源轮次:").append(md(text(section.get("sourceTurnId"), "未知"))).append("\n\n");
List<Map<String,Object>> columns = maps(section.get("columns"));
if(historical)columns=historyColumns(section,columns);
out.append("## ").append(md(historical?historySectionTitle(section,columns):text(section.get("title"), "历史数据"))).append("\n\n");
out.append("范围:").append(md(historical?historyScopeText(section,sources):scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
.append(historical?"\n\n":"\n\n来源轮次:"+md(text(section.get("sourceTurnId"), "未知"))+"\n\n");
if (!columns.isEmpty()) {
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(text(column.get("label"), text(column.get("key"), "")))).append(" |");
String unit=historical?verifiedWeightUnit(section,sources):null;
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(historical?historyColumnLabel(section,column,unit):displayColumnLabel(column,unit))).append(" |");
out.append("\n|"); for (int i=0;i<columns.size();i++) out.append(" --- |"); out.append('\n');
for (Map<String,Object> row : maps(section.get("rows"))) {
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(valueText(row.get(text(column.get("key"), ""))))).append(" |"); out.append('\n');
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(renderedValue(section,column,row,unit))).append(" |"); out.append('\n');
}
out.append('\n');
}
if (section.get("note") != null) out.append(md(text(section.get("note"), ""))).append("\n\n");
String note=renderedNote(section,historical?verifiedWeightUnit(section,sources):null,historical);
if (!note.isBlank()) out.append(md(note)).append("\n\n");
}
if (!gaps.isEmpty()) {
out.append("## 尚未覆盖的范围\n\n");
@@ -583,25 +718,28 @@ final class AnalysisProtocolHistoryFacts {
.append("details{margin:8px 0}summary{cursor:pointer;color:#48627b}.empty{color:#77889a;font-style:italic}")
.append("</style></head><body><main><article class=\"paper\"><h1>").append(html(title)).append("</h1>")
.append(historical?"<p>按每个历史来源的原查询范围分别列示;不同期间与重叠总体没有合并或求均值。</p>":"<p>依据本轮受控正式类别查询;范围为当前发布版本全部已发布期间,不含未分类。</p>");
for (Map<String,Object> source : sources) {
if(!historical)for (Map<String,Object> source : sources) {
out.append("<p class=\"source\">来源轮次:").append(html(sourceReference(source))).append(";状态:")
.append(html(text(source.get("status"), "未知"))).append("</p>");
}
if (!headlines.isEmpty()) {
out.append("<section class=\"highlights\"><h2>重点结论</h2><ul>");
for (Map<String,Object> fact : headlines) out.append("<li>").append(html(text(fact.get("text"), ""))).append("</li>");
for (Map<String,Object> fact : headlines) out.append("<li>").append(html(text(fact.get("_displayText"),text(fact.get("text"), "")))).append("</li>");
out.append("</ul></section>");
}
for (Map<String,Object> section : sections) {
out.append("<section><h2>").append(html(text(section.get("title"), "历史数据"))).append("</h2><p class=\"scope\">范围:")
.append(html(scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
.append("</p><p class=\"source\">来源轮次:").append(html(text(section.get("sourceTurnId"), "未知"))).append("</p>");
List<Map<String,Object>> columns = maps(section.get("columns"));
if(historical)columns=historyColumns(section,columns);
out.append("<section><h2>").append(html(historical?historySectionTitle(section,columns):text(section.get("title"), "历史数据"))).append("</h2><p class=\"scope\">范围:")
.append(html(historical?historyScopeText(section,sources):scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage")))).append("</p>");
if(!historical)out.append("<p class=\"source\">来源轮次:").append(html(text(section.get("sourceTurnId"), "未知"))).append("</p>");
List<Map<String,Object>> rows = maps(section.get("rows"));
if (columns.isEmpty()) out.append("<p class=\"empty\">没有可展示的列。</p>");
else if ("物理原始记录".equals(section.get("title")) && !rows.isEmpty()) appendRecordTables(out, columns, rows);
else if(historical)appendHistoryHtmlTable(out,section,columns,rows,verifiedWeightUnit(section,sources));
else appendHtmlTables(out, columns, rows);
if (section.get("note") != null) out.append("<p class=\"note\">").append(html(text(section.get("note"), ""))).append("</p>");
String note=renderedNote(section,historical?verifiedWeightUnit(section,sources):null,historical);
if (!note.isBlank()) out.append("<p class=\"note\">").append(html(note)).append("</p>");
out.append("</section>");
}
if (!gaps.isEmpty()) {
@@ -650,6 +788,202 @@ final class AnalysisProtocolHistoryFacts {
appendHtmlTables(out, compact, displayed, rows);
}
private static void appendHistoryHtmlTable(StringBuilder out,Map<String,Object> section,
List<Map<String,Object>> columns,List<Map<String,Object>> rows,String unit) {
List<List<Map<String,Object>>> segments=new ArrayList<>();
if(columns.size()<=6)segments.add(columns);
else {
segments.add(columns.subList(0,6));
for(int start=6;start<columns.size();start+=5) {
List<Map<String,Object>> segment=new ArrayList<>();segment.add(columns.getFirst());
segment.addAll(columns.subList(start,Math.min(start+5,columns.size())));segments.add(segment);
}
}
for(List<Map<String,Object>> segment:segments) {
out.append("<table><thead><tr>");
for(Map<String,Object> column:segment)out.append("<th>").append(html(historyColumnLabel(section,column,unit))).append("</th>");
out.append("</tr></thead><tbody>");
if(rows.isEmpty())out.append("<tr><td colspan=\"").append(segment.size()).append("\" class=\"empty\">无返回行</td></tr>");
for(Map<String,Object> row:rows) {
out.append("<tr>");
for(Map<String,Object> column:segment)out.append("<td>").append(html(renderedValue(section,column,row,unit))).append("</td>");
out.append("</tr>");
}
out.append("</tbody></table>");
}
}
private static String displayColumnLabel(Map<String,Object> column,String unit) {
String key=text(column.get("key"),""),label=text(column.get("label"),key);
if(unit==null||!weightField(key))return label;
return label.replace("(原单位)","("+unit+")").replace("(原单位)","("+unit+")");
}
private static List<Map<String,Object>> historyColumns(Map<String,Object> section,List<Map<String,Object>> columns) {
List<Map<String,Object>> visible=new ArrayList<>();
boolean product=isProductSection(section);
for(Map<String,Object> column:columns) {
String key=text(column.get("key"),"");String lower=key.toLowerCase(Locale.ROOT);
String label=text(column.get("label"),key);
if(key.isBlank()||Set.of("key","groupkey","productkey","dimensionkey","factid","historyfactid",
"evidenceref","queryid","sourceid","sourceturnid","attemptid","executionid",
"snapshotid","scopeid","releaseid","factkey","scopekey").contains(lower)
||lower.endsWith("factid")||lower.endsWith("queryid")||lower.endsWith("sourceid")
||lower.endsWith("executionid")||lower.endsWith("attemptid")||lower.endsWith("snapshotid")
||lower.endsWith("scopeid")||lower.endsWith("hash")
||Set.of("分组键","产品键","规格键","来源ID","查询ID").contains(label))continue;
var safe=new LinkedHashMap<>(column);
if("label".equals(key)&&product)safe.put("label","规格");
else if("label".equals(key)&&"group".equals(section.get("dimension")))safe.put("label","业务分组");
visible.add(safe);
}
return List.copyOf(visible);
}
private static boolean isProductSection(Map<String,Object> section) {
String title=text(section.get("title"),"").toLowerCase(Locale.ROOT);
return "product".equals(section.get("dimension"))||title.contains("product")
||title.contains("规格")||title.contains("产品");
}
private static String historySectionTitle(Map<String,Object> section,List<Map<String,Object>> columns) {
String title=text(section.get("title"),"历史数据");
String lower=title.toLowerCase(Locale.ROOT);
boolean ranking=lower.matches("(?s).*\\btop\\s*\\d+\\b.*")||columns.stream().anyMatch(c->"rank".equals(c.get("key")));
if(!ranking)return title.replace("product","产品").replace("Product","产品");
var match=java.util.regex.Pattern.compile("(?i)\\btop\\s*(\\d+)\\b").matcher(title);
int count=maps(section.get("rows")).size();
if(match.find())try{int requested=Integer.parseInt(match.group(1));if(requested>0)count=requested;}catch(NumberFormatException ignored){}
String noun=isProductSection(section)?"规格":"项";
boolean weightRanking=columns.stream().anyMatch(c->Set.of("weight","scrapWeight","blackScrapWeight","brightScrapWeight").contains(c.get("key")));
return "Top "+count+noun+"排名"+(weightRanking?"(按合计报废重量排序)":"");
}
private static String historyColumnLabel(Map<String,Object> section,Map<String,Object> column,String unit) {
String key=text(column.get("key"),"");
Map<String,Object> localized=new LinkedHashMap<>(column);
if("label".equals(key)&&isProductSection(section))localized.put("label","规格");
else if("label".equals(key)&&"group".equals(section.get("dimension")))localized.put("label","业务分组");
return displayColumnLabel(localized,unit);
}
private static String historyScopeText(Map<String,Object> section,List<Map<String,Object>> sources) {
List<String> parts=new ArrayList<>();String date=periodText(map(section.get("period")));
if(!date.isBlank())parts.add(date);
Object stage=section.get("stage");if(stage instanceof String value&&!value.isBlank())parts.add(stageLabel(value));
Map<String,Object> filters=map(section.get("filters"));
addScopePart(parts,"产业部",filters.get("department"));addScopePart(parts,"业务分组",filters.get("group"));
addScopePart(parts,"规格",filters.get("product"));addScopePart(parts,"项目组",filters.get("projectGroup"));
Map<String,Object> extra=map(filters.get("extraFilters"));
for(String key:extra.keySet().stream().sorted().toList()) {
String label=historyFieldLabel(section,sources,key);
addScopePart(parts,label,extra.get(key));
}
Map<String,Object> exclusions=map(filters.get("exclusions"));
for(String key:exclusions.keySet().stream().sorted().toList()) {
String label=switch(key){case "department"->"产业部";case "group"->"业务分组";case "product"->"规格";case "projectGroup"->"项目组";default->"对象";};
List<String> values=scopeLiterals(exclusions.get(key));if(!values.isEmpty())parts.add("排除"+label+" "+String.join("、",values));
}
if(Boolean.TRUE.equals(filters.get("includeUnassigned")))parts.add("包含未分配项");
Map<String,Object> columnLabels=map(filters.get("columnLabels"));
for(Object raw:AnalysisProtocolService.list(filters.get("columnFilters"))) {
Map<String,Object> rule=map(raw);String field=text(rule.get("field"),"");
String label=text(columnLabels.get(field),historyFieldLabel(section,sources,field));
if(label.isBlank())label="授权列";
List<String> prefix=strings(rule.get("stages")).stream().map(AnalysisProtocolHistoryFacts::stageLabel).toList();
String operator=switch(text(rule.get("operator"),"")){case "in"->"包含";case "not_in"->"不含";case "is_empty"->"为空";case "not_empty"->"非空";default->"筛选";};
List<String> values=scopeLiterals(rule.get("values"));
StringBuilder part=new StringBuilder();if(!prefix.isEmpty())part.append(String.join("、",prefix)).append(' ');
part.append(label).append(' ').append(operator);if(!values.isEmpty())part.append(' ').append(String.join("、",values));parts.add(part.toString());
}
return parts.isEmpty()?"全范围(无额外筛选)":String.join(" · ",parts);
}
private static String historyFieldLabel(Map<String,Object> section,List<Map<String,Object>> sources,String field) {
if(field.isBlank())return "授权列";
Map<String,Object> filters=map(section.get("filters"));String label=text(map(filters.get("columnLabels")).get(field),"");
if(!label.isBlank())return label;
String sourceTurn=text(section.get("sourceTurnId"),"");
for(Map<String,Object> source:sources) {
if(!sourceTurn.equals(text(source.get("sourceTurnId"),text(source.get("turnId"),""))))continue;
Map<String,Object> task=map(source.get("task"));label=text(map(task.get("sourceFieldLabels")).get(field),"");
if(!label.isBlank())return label;
}
return switch(field){case "customerCode"->"客户代码";case "workOrder"->"委托单";case "orderNo"->"订单号";default->"授权列";};
}
private static void addScopePart(List<String> parts,String label,Object raw) {
List<String> values=scopeLiterals(raw);if(!values.isEmpty())parts.add(label+" "+String.join("、",values));
}
private static List<String> scopeLiterals(Object raw) {
List<?> values=raw instanceof List<?> list?list:List.of(raw==null?"":raw);
return values.stream().filter(value->value instanceof String||value instanceof Number||value instanceof Boolean)
.map(Objects::toString).map(String::trim).filter(value->!value.isBlank()).distinct().sorted().toList();
}
private static String renderedValue(Map<String,Object> section,Map<String,Object> column,
Map<String,Object> row,String unit) {
String key=text(column.get("key"),"");Object value=row.get(key);
String metric="value".equals(key)?text(row.get("metricCode"),""):key;
if(value instanceof Number number&&ratioField(metric)) {
try{return new BigDecimal(number.toString()).multiply(BigDecimal.valueOf(100))
.setScale(2,RoundingMode.HALF_UP).toPlainString()+"%";}
catch(NumberFormatException ignored) {return valueText(value);}
}
if(value instanceof Number number&&weightField(metric)&&unit!=null) {
try{return new BigDecimal(number.toString()).stripTrailingZeros().toPlainString()+" "+unit;}
catch(NumberFormatException ignored) {return valueText(value);}
}
return valueText(value);
}
private static boolean ratioField(String metric) {
return Set.of("rate","compositeRate","scrapRate","share","cumulativeShare").contains(metric);
}
private static boolean weightField(String metric) {
return Set.of("weight","totalWeight","scrapWeight","blackScrapWeight","brightScrapWeight",
"blackTotalWeight","brightTotalWeight","blackWeight","brightWeight").contains(metric);
}
private static String verifiedWeightUnit(Map<String,Object> section,List<Map<String,Object>> sources) {
Map<String,Object> filters=map(section.get("filters"));
Set<String> supported=Set.of("department","group","product","projectGroup","includeUnassigned");
if(filters.keySet().stream().anyMatch(key->!supported.contains(key)))return null;
Map<String,Object> expected=new LinkedHashMap<>();
for(String key:List.of("department","group","product","projectGroup"))if(filters.containsKey(key))expected.put(key,filters.get(key));
String sourceTurn=text(section.get("sourceTurnId"),"");Set<String> units=new HashSet<>();
for(Map<String,Object> source:sources) {
String turn=text(source.get("sourceTurnId"),text(source.get("turnId"),""));
if(!sourceTurn.equals(turn))continue;
for(Map<String,Object> chart:maps(source.get("sourceVisuals"))) {
Map<String,Object> chartScope=map(chart.get("scope"));
Map<String,Object> chartFilters=new LinkedHashMap<>();
for(String key:List.of("department","group","product","projectGroup"))if(chartScope.containsKey(key))chartFilters.put(key,chartScope.get(key));
String stage=text(section.get("stage"),"overall");Object chartStage=chart.get("stage");
if(!Objects.equals(chart.get("originTurnId"),sourceTurn)||!Objects.equals(chart.get("period"),section.get("period"))
||!Objects.equals(chartFilters,expected)||chartScope.get("includeUnassigned")!=null
&&!Objects.equals(chartScope.get("includeUnassigned"),filters.getOrDefault("includeUnassigned",false))
||!"product".equals(chart.get("dimension"))
||chartStage!=null&&!(Objects.equals(chartStage,stage)||Objects.equals(chartStage,"overall"))
||!Set.of("current_scrap_weight","black_scrap_weight","bright_scrap_weight","scrapWeight","weight").contains(Objects.toString(chart.get("metric"),"")))continue;
String unit=text(chart.get("unit"),"");if(Set.of("kg","g","mg","ton").contains(unit))units.add(unit);
}
}
return units.size()==1?units.iterator().next():null;
}
private static String renderedNote(Map<String,Object> section,String unit,boolean historical) {
String note=text(section.get("note"),"");
if(historical&&hasHistoryInternalMetadata(note))return "";
return unit==null?note:note.replace("重量沿用来源单位","重量单位为 "+unit);
}
private static boolean hasHistoryInternalMetadata(String value) {
return value!=null&&HISTORY_INTERNAL_METADATA.matcher(value).find();
}
private static void appendHtmlTables(StringBuilder out, List<Map<String,Object>> columns, List<Map<String,Object>> rows) {
appendHtmlTables(out, columns, rows, List.of());
}
@@ -789,7 +1123,7 @@ final class AnalysisProtocolHistoryFacts {
return String.join(" · ", parts);
}
private static String fieldLabel(String field) { return FIELD_LABELS.getOrDefault(field, field); }
private static String stageLabel(String stage) { return "black".equals(stage) ? "黑片" : "bright".equals(stage) ? "亮片" : stage; }
private static String stageLabel(String stage) { return "black".equals(stage) ? "黑片" : "bright".equals(stage) ? "亮片" : "overall".equals(stage) ? "综合" : stage; }
private static String counts(Map<String,Object> row) { return valueText(row.get("presentRows")) + "/" + valueText(row.get("missingRows")) + "/" + valueText(row.get("invalidRows")) + "/" + valueText(row.get("negativeRows")); }
private static String rankBoundary(int limit, int shown) { return Integer.toString(limit > 0 ? limit : shown); }
private static void addFact(List<Map<String,Object>> facts, String turnId, int[] sequence, String text) {
@@ -797,7 +1131,7 @@ final class AnalysisProtocolHistoryFacts {
}
private static void displayFact(List<Map<String,Object>> facts,int sectionIndex,int rowIndex,Map<String,Object> period,
Map<String,Object> filters,String stage,String metric,Object raw,String displayText) {
var fact=facts.getLast();fact.putAll(displayMetric(metric,raw));fact.put("sectionIndex",sectionIndex);fact.put("rowIndex",rowIndex);
var fact=facts.getLast();fact.putAll(displayMetric(metric,raw));fact.put("sectionIndex",sectionIndex);if(rowIndex>=0)fact.put("rowIndex",rowIndex);
var scope=mapOf("period",period,"filters",filters,"stage",stage.isBlank()?"overall":stage);fact.put("scope",scope);
String original=text(fact.get("text"),"");String token=valueText(raw);
fact.put("displayText",displayText!=null?displayText:raw==null?original:original.replace(" = "+token," = "+displayValue(metric,raw)));
@@ -7,41 +7,42 @@ import java.util.regex.Pattern;
final class AnalysisProtocolHistorySummary {
private static final Pattern BRACKET=Pattern.compile("\\[([^\\[\\]\\r\\n]{1,240})\\]");
private static final Pattern ACTIVE_HTML=Pattern.compile("(?is)<(script|style|iframe|object|embed)\\b[^>]*>.*?</\\1\\s*>");
private static final Pattern INTERNAL=Pattern.compile("(?i)(?:history-fact:|Q[A-F0-9]{8}|[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|(?:queryId|sourceTurnId|attemptId|executionId|snapshotHash|scopeHash)\\s*[:=]|filters\\s*=|\\{\\s*\\\")");
record Result(String answer,Map<String,Object> summary,List<String> refs,List<String> gaps){}
private record Text(String value,List<String> refs,boolean filtered){}
static Result normalize(Map<String,Object> details,Set<String> known,String fallback) {
var raw=AnalysisProtocolService.map(details.get("readingSummary"));var gaps=new LinkedHashSet<String>();
boolean shape=Objects.equals(raw.get("version"),1)&&raw.get("lead") instanceof String&&raw.get("evidence") instanceof List<?>&&raw.get("notices") instanceof List<?>&&raw.get("evidenceRefs") instanceof List<?>;
if(!shape)gaps.add("历史解释摘要格式未通过核验,保留完整数据附录。");
if(!shape)gaps.add("历史解释摘要格式未通过核验。");
var used=new LinkedHashSet<String>();var summaryRefs=new LinkedHashSet<String>();
var lead=clean(shape?raw.get("lead"):fallback,known,12000);summaryRefs.addAll(lead.refs());
var evidence=new ArrayList<String>();var notices=new LinkedHashSet<String>();
if(lead.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
if(lead.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
if(shape) {
collect(raw.get("evidence"),evidence,summaryRefs,gaps,known,20);
var noticeList=new ArrayList<String>();collect(raw.get("notices"),noticeList,summaryRefs,gaps,known,40);notices.addAll(noticeList);
var offered=AnalysisProtocolService.list(raw.get("evidenceRefs"));
if(offered.size()>100||offered.stream().anyMatch(id->!(id instanceof String)||!known.contains(id)))gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
if(offered.size()>100||offered.stream().anyMatch(id->!(id instanceof String)||!known.contains(id)))gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
}
used.addAll(summaryRefs);
var paragraphs=new ArrayList<String>();Object rawAnswer=details.get("answer");
if(rawAnswer instanceof String text&&text.length()<=64000)for(String paragraph:text.split("\\n\\s*\\n")) {
var cleaned=clean(paragraph,known,12000);if(!cleaned.value().isBlank()){paragraphs.add(cleaned.value());used.addAll(cleaned.refs());}
if(cleaned.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
if(cleaned.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
}
if(paragraphs.isEmpty())gaps.add("历史解释正文尚未形成有效内容,保留完整数据附录。");
if(!known.isEmpty()&&used.isEmpty())gaps.add("历史解释没有绑定已核验事实,保留完整数据附录。");
if(known.isEmpty())gaps.add("历史快照没有可用于解释的事实,保留完整数据附录。");
if(paragraphs.isEmpty())gaps.add("历史解释正文未形成有效内容。");
if(!known.isEmpty()&&used.isEmpty())gaps.add("历史解释未绑定已核验事实。");
if(known.isEmpty())gaps.add("历史快照没有可用于解释的已核验事实。");
notices.addAll(gaps);
var summary=new LinkedHashMap<String,Object>();summary.put("version",1);summary.put("lead",lead.value().isBlank()?fallback:lead.value());summary.put("evidence",List.copyOf(evidence));summary.put("notices",List.copyOf(notices));summary.put("evidenceRefs",List.copyOf(summaryRefs));
return new Result(paragraphs.isEmpty()?fallback:String.join("\n\n",paragraphs),summary,List.copyOf(used),List.copyOf(gaps));
}
private static void collect(Object raw,List<String> target,Set<String> refs,Set<String> gaps,Set<String> known,int max) {
var values=AnalysisProtocolService.list(raw);
if(values.size()>max)gaps.add("历史解释摘要超过展示上限,已保留有限内容与完整附录。");
if(values.size()>max)gaps.add("历史解释摘要超过展示上限,已保留可核验内容。");
for(Object value:values.stream().limit(max).toList()) {
var cleaned=clean(value,known,12000);if(!cleaned.value().isBlank()){target.add(cleaned.value());refs.addAll(cleaned.refs());}
if(cleaned.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
if(cleaned.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
}
}
private static Text clean(Object raw,Set<String> known,int limit) {
@@ -51,6 +52,8 @@ final class AnalysisProtocolHistorySummary {
while(matcher.find())if(known.contains(matcher.group(1)))refs.add(matcher.group(1));else unknown=true;
// Do not retain an unsupported assertion after merely hiding its invented citation.
if(unknown)return new Text("",List.of(),true);
return new Text(safe.strip(),List.copyOf(refs),filtered);
String visible=BRACKET.matcher(safe).replaceAll("");
if(INTERNAL.matcher(visible).find())return new Text("",List.of(),true);
return new Text(visible.strip(),List.copyOf(refs),filtered);
}
}
@@ -47,7 +47,9 @@ final class AnalysisProtocolMetricCoverage {
&& STAGES.containsAll(stages)
&& fields.stream().distinct().count() == fields.size()
&& stages.stream().distinct().count() == stages.size()
&& (stages.isEmpty() ? fields.equals(List.of("rate")) : true);
&& (stages.isEmpty() ? fields.equals(List.of("rate"))
|| !trendRequired && !AnalysisProtocolService.list(task.get("dimensions")).isEmpty()
&& fields.contains("scrapWeight") && Set.of("rate","scrapWeight").containsAll(fields) : true);
if (!taskValid) {
missing.add("临时指标任务字段或期间覆盖模式无效。");
result.put("availableMonths", List.of());
@@ -87,7 +89,10 @@ final class AnalysisProtocolMetricCoverage {
missing.add("综合合格率没有可核验的已发布来源月份。");
continue;
}
if (!validCompositeRate(summary, months, coverage))
if ("scrapWeight".equals(field)) {
if (!validCombinedScrapWeight(summary, months, coverage))
missing.add("合计报废重量正式值或双阶段来源覆盖无效。");
} else if (!validCompositeRate(summary, months, coverage))
missing.add("综合合格率正式值或双阶段来源覆盖无效。");
continue;
}
@@ -111,7 +116,7 @@ final class AnalysisProtocolMetricCoverage {
BigDecimal value = decimal(values.get(field));
if (value == null) return false;
if ("rate".equals(field))
return inRange(value, BigDecimal.ZERO, BigDecimal.ONE)
return nonNegative(value)
&& positive(decimal(values.get("totalWeight")));
if ("scrapRate".equals(field))
return positive(decimal(values.get("totalWeight")))
@@ -124,7 +129,7 @@ final class AnalysisProtocolMetricCoverage {
private static boolean validCompositeRate(Map<String, Object> summary, List<Integer> months,
MonthCoverage coverage) {
BigDecimal value = decimal(summary.get("compositeRate"));
if (!inRange(value, BigDecimal.ZERO, BigDecimal.ONE)) return false;
if (!nonNegative(value)) return false;
boolean blackSource = false, brightSource = false;
for (int month : months) {
Map<String, Object> item = coverage.byMonth().get(month);
@@ -143,6 +148,39 @@ final class AnalysisProtocolMetricCoverage {
|| (brightSource && positive(decimal(bright.get("totalWeight"))));
}
private static boolean validCombinedScrapWeight(Map<String, Object> summary, List<Integer> months,
MonthCoverage coverage) {
if (months.isEmpty()) return false;
Map<String, Object> summaryCoverage = map(summary.get("coverage"));
Map<String, Long> stageSources = new LinkedHashMap<>();
for (String stage : STAGES) stageSources.put(stage, 0L);
for (int month : months) {
Map<String,Object> item = coverage.byMonth().get(month);
Long black = item == null ? null : count(item.get("blackRows"));
Long bright = item == null ? null : count(item.get("brightRows"));
if (black == null || bright == null || black < 0 || bright < 0 || black == 0 && bright == 0) return false;
stageSources.put("black", stageSources.get("black") + black);
stageSources.put("bright", stageSources.get("bright") + bright);
}
for (String stage : STAGES) {
Map<String, Object> stageSummary = map(summary.get(stage));
boolean hasValue = stageSummary.containsKey("scrapWeight");
BigDecimal value = decimal(stageSummary.get("scrapWeight"));
long monthlyRows = stageSources.get(stage);
Long publishedRows = count(summaryCoverage.get(stage + "Rows"));
if (publishedRows != null && publishedRows != monthlyRows) return false;
if (monthlyRows > 0) {
if (!hasValue || !nonNegative(value) || publishedRows != null && publishedRows == 0) return false;
} else {
// A missing metric is acceptable only when both the period-level and
// summary-level coverage explicitly prove that this stage had no rows.
if (hasValue && (!nonNegative(value) || value.signum() != 0)) return false;
if (!hasValue && (publishedRows == null || publishedRows != 0)) return false;
}
}
return true;
}
private static void inspectTrend(Map<String, Object> response, List<String> fields, List<String> stages,
Period period, MonthCoverage coverage, boolean complete,
List<String> missing) {
@@ -425,10 +463,6 @@ final class AnalysisProtocolMetricCoverage {
}
}
private static boolean inRange(BigDecimal value, BigDecimal minimum, BigDecimal maximum) {
return value != null && value.compareTo(minimum) >= 0 && value.compareTo(maximum) <= 0;
}
private static boolean positive(BigDecimal value) { return value != null && value.signum() > 0; }
private static boolean nonNegative(BigDecimal value) { return value != null && value.signum() >= 0; }
@@ -1,5 +1,6 @@
package cn.yunsheng.quality.analysis;
import cn.yunsheng.quality.common.SelectionValues;
import java.util.*;
import java.util.regex.Pattern;
import tools.jackson.databind.json.JsonMapper;
@@ -39,7 +40,8 @@ final class AnalysisProtocolQueryPlan {
if(!rawPurpose&&node.containsKey("sourceFields"))throw invalid("sourceFields 只能用于 source/records 节点");
Map<String,List<String>> selectedSourceFields=rawPurpose&&node.containsKey("sourceFields")
?normalizeSourceFields(node.get("sourceFields"),stages,catalog):Map.of();
if(stages.isEmpty()&&!fields.equals(List.of("rate"))&&!changesPurpose)throw invalid("重量和报废率节点必须明确阶段,不能自行合并黑片亮片");
boolean combinedRanking="ranking".equals(purpose)&&stages.isEmpty()&&fields.contains("scrapWeight")&&Set.of("rate","scrapWeight").containsAll(fields);
if(stages.isEmpty()&&!fields.equals(List.of("rate"))&&!changesPurpose&&!combinedRanking)throw invalid("重量和报废率节点必须明确阶段;合计排名仅支持报废总重及综合合格率");
boolean compare=bool(node.getOrDefault("comparisonRequired",false));
if(compare&&(!Set.of("metrics","source","records","defects","changes").contains(purpose)||!(task.get("comparisonPeriod") instanceof Map<?,?>)))throw invalid("期间比较节点需要两个已确认期间");
if("trend".equals(purpose)&&map(task.get("period")).containsKey("month"))throw invalid("月度趋势需要多个月份范围");
@@ -71,7 +73,8 @@ final class AnalysisProtocolQueryPlan {
String stage=string(selector.get("stage")),metric=string(selector.get("metric"));
count=integer(selector.get("count"),1,10);
if(ranking==null||!"ranking".equals(ranking.get("purpose"))||!"product".equals(ranking.get("dimension"))
||!"base".equals(map(ranking.get("selector")).get("kind"))||!list(ranking.get("stages")).contains(stage)
||!"base".equals(map(ranking.get("selector")).get("kind"))||!(list(ranking.get("stages")).contains(stage)
||list(ranking.get("stages")).isEmpty()&&"overall".equals(stage)&&"scrapWeight".equals(metric))
||!list(ranking.get("fields")).contains(metric)||count>((Number)ranking.get("limit")).intValue())throw invalid("规格下钻必须依赖前面已声明的实际产品排名");
requestedDimensions.add("product");cleanSelector.put("sourceNodeId",source);cleanSelector.put("stage",stage);cleanSelector.put("metric",metric);cleanSelector.put("count",count);
}
@@ -104,7 +107,7 @@ final class AnalysisProtocolQueryPlan {
clean.put("selector",cleanSelector);clean.put("comparisonRequired",compare);clean.put("visualPreference",preference);clean.put("visualType",visual);
if("ranking".equals(purpose)) {
String dimension=string(node.get("dimension"));
if(!DIMENSIONS.contains(dimension)||!WEIGHTS.containsAll(fields)||stages.isEmpty()||compare||!kind.equals("base"))throw invalid("排名节点需要明确阶段、重量和基础分组");
if(!DIMENSIONS.contains(dimension)||!(combinedRanking||WEIGHTS.containsAll(fields)&&!stages.isEmpty())||compare||!kind.equals("base"))throw invalid("排名节点需要正式重量和基础分组;合计口径使用空阶段");
requestedDimensions.add(dimension);clean.put("dimension",dimension);clean.put("limit",integer(node.getOrDefault("limit",10),1,10));
} else if(changesPurpose) {
String dimension=string(node.get("dimension")),sort=string(node.get("sort"));
@@ -121,7 +124,7 @@ final class AnalysisProtocolQueryPlan {
if("product".equals(query.get("dimension")))requestedDimensions.add("product");
} else if(node.containsKey("dimension")||node.containsKey("limit"))throw invalid("指标或趋势节点不能伪装为分组排名");
int periodCount=compare?2:1;
int readsPerScope=changesPurpose?1:"ranking".equals(purpose)?fields.size()*stages.size():rawPurpose||defectPurpose?stages.size()*periodCount:periodCount;
int readsPerScope=changesPurpose||combinedRanking?1:"ranking".equals(purpose)?fields.size()*stages.size():rawPurpose||defectPurpose?stages.size()*periodCount:periodCount;
scopeBound+=count;queryBound+=count*readsPerScope;
if(scopeBound>20||queryBound>40)throw invalid("本轮查询对象或正式读取超过有界上限,请拆分任务");
trend|="trend".equals(purpose);comparison|=compare;
@@ -244,7 +247,7 @@ final class AnalysisProtocolQueryPlan {
private Map<String,Object> childTask(Map<String,Object> parent,Map<String,Object> node,Map<String,Object> filters,Map<String,Object> catalog) {
Map<String,Object> child=new LinkedHashMap<>(parent);child.remove("queryPlan");child.remove("comparisonMode");
String purpose=string(node.get("purpose"));boolean ranking="ranking".equals(purpose),changes="changes".equals(purpose),defects="defects".equals(purpose),source="source".equals(purpose),records="records".equals(purpose),compare=Boolean.TRUE.equals(node.get("comparisonRequired"));
child.put("kind",source?"source_verify":records?"details":ranking||defects||changes?"analysis":"metrics");child.put("filters",filters);child.put("fields",node.get("fields"));child.put("stages",node.get("stages"));
child.put("kind",source?"source_verify":records?"details":singleObjectMetricRanking(parent,node,filters)?"metrics":ranking||defects||changes?"analysis":"metrics");child.put("filters",filters);child.put("fields",node.get("fields"));child.put("stages",node.get("stages"));
child.put("dimensions",ranking?List.of(node.get("dimension")):List.of());child.put("dimensionStages",ranking?Map.of(string(node.get("dimension")),node.get("stages")):Map.of());
child.remove("defectQuery");child.remove("changeQuery");
if(changes){child.put("dimensions",List.of(node.get("dimension")));child.put("dimensionStages",Map.of(string(node.get("dimension")),node.get("stages")));child.put("changeQuery",Map.of("dimension",node.get("dimension"),"sort",node.get("sort"),"limit",node.get("limit")));}
@@ -275,6 +278,35 @@ final class AnalysisProtocolQueryPlan {
}
return child;
}
/**
* A confirmed one-object metrics request may use the ranking endpoint only
* to obtain combined scrap weight and composite rate. Keep that bounded
* scalar intent on the child task so the visual gate does not mistake the
* transport operator for a requested Top-N presentation.
*/
private boolean singleObjectMetricRanking(Map<String,Object> parent,Map<String,Object> node,Map<String,Object> filters) {
if(!"metrics".equals(parent.get("kind"))||!"ranking".equals(node.get("purpose"))
||!"auto".equals(node.get("visualPreference"))||!"auto".equals(parent.get("visualPreference"))
||Boolean.TRUE.equals(parent.get("comparisonRequired"))||Boolean.TRUE.equals(node.get("comparisonRequired"))
||Boolean.TRUE.equals(parent.get("trendRequired"))||parent.get("comparisonPeriod") instanceof Map<?,?>
||!list(parent.get("stages")).isEmpty()||!list(node.get("stages")).isEmpty()
||!new HashSet<>(list(parent.get("fields"))).equals(Set.of("rate","scrapWeight"))
||!new HashSet<>(list(node.get("fields"))).equals(Set.of("rate","scrapWeight"))
||!DIMENSIONS.contains(node.get("dimension"))||!Objects.equals(node.get("limit"),1)
||!"base".equals(map(node.get("selector")).get("kind")))return false;
String dimension=string(node.get("dimension"));
if(!new HashSet<>(list(parent.get("dimensions"))).equals(Set.of(dimension)))return false;
Object parentObject=map(parent.get("filters")).get(dimension),childObject=filters.get(dimension);
List<String> parentValues=singleSelectionValues(parentObject),childValues=singleSelectionValues(childObject);
if(parentValues.size()!=1||childValues.size()!=1||!parentValues.equals(childValues))return false;
List<?> plan=list(parent.get("queryPlan"));
return plan.size()==1&&Objects.equals(map(plan.getFirst()).get("nodeId"),node.get("nodeId"));
}
private List<String> singleSelectionValues(Object wire) {
if(!(wire instanceof String value)||value.isBlank())return List.of();
try{return SelectionValues.parse(value);}catch(RuntimeException invalid){return List.of();}
}
private static Map<String,List<String>> normalizeSourceFields(Object raw,List<String> stages,Map<String,Object> catalog) {
if(!(raw instanceof Map<?,?> selected)||selected.isEmpty()||selected.size()>stages.size())throw invalid("sourceFields 必须按阶段选择授权源字段");
Map<String,Map<String,Object>> available=sourceFieldCatalog(catalog);Map<String,List<String>> result=new LinkedHashMap<>();
@@ -10,9 +10,82 @@ final class AnalysisProtocolRankArtifact {
List<Map<String,Object>> rankings=new ArrayList<>();
Map<String,Object> perDimension=AnalysisProtocolService.map(task.get("dimensionStages"));
List<?> stages=AnalysisProtocolService.list(perDimension.getOrDefault(dimension,task.get("stages")));
if(stages.isEmpty()&&AnalysisProtocolRankingCoverage.complete(response,task,dimension)) {
boolean compositeRateAvailable=AnalysisProtocolService.list(task.get("fields")).contains("rate")
&&Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(response,task,
AnalysisProtocolService.map(task.get("period"))).get("metricsComplete"));
List<Map<String,Object>> positive=new ArrayList<>();
for(Object raw:AnalysisProtocolService.list(response.get("rows"))) {
Map<String,Object> row=AnalysisProtocolService.map(raw),coverage=AnalysisProtocolService.map(row.get("coverage"));BigDecimal sum=BigDecimal.ZERO;
Map<String,Object> measures=new LinkedHashMap<>();
for(String stage:List.of("black","bright")) {
Object value=AnalysisProtocolService.map(row.get(stage)).get("scrapWeight");
boolean observed=number(coverage.get(stage+"Rows"))>0;
if(value instanceof Number number) {
BigDecimal component=new BigDecimal(number.toString());sum=sum.add(component);
measures.put(stage+"ScrapWeight",component);
} else measures.put(stage+"ScrapWeight",null);
Object scale=AnalysisProtocolService.map(row.get(stage)).get("totalWeight");
measures.put(stage+"TotalWeight",observed&&scale instanceof Number number?new BigDecimal(number.toString()):null);
}
Object compositeRate=row.get("compositeRate");
if(sum.signum()>0) {
var ranked=new LinkedHashMap<String,Object>();ranked.put("key",row.get("key"));ranked.put("label",Objects.toString(row.get("label"),row.get("key").toString()));ranked.put("weight",sum);
boolean rateSource=false;
for(String stage:List.of("black","bright")) {
Object stageRows=coverage.get(stage+"Rows"),stageWeight=measures.get(stage+"TotalWeight");
rateSource|=stageRows instanceof Number count&&count.intValue()>0&&stageWeight instanceof Number value&&new BigDecimal(value.toString()).signum()>0;
}
if(compositeRateAvailable&&rateSource) {
BigDecimal officialRate=decimal(compositeRate);
if(officialRate!=null&&officialRate.signum()>=0)ranked.put("compositeRate",officialRate);
}
ranked.putAll(measures);positive.add(ranked);
}
}
positive.sort((a,b)->{int weight=((BigDecimal)b.get("weight")).compareTo((BigDecimal)a.get("weight"));return weight!=0?weight:codePointCompare(a.get("key").toString(),b.get("key").toString());});
var summary=AnalysisProtocolService.map(response.get("summary"));BigDecimal denominator=BigDecimal.ZERO;
for(String stage:List.of("black","bright")) {
Object published=AnalysisProtocolService.map(summary.get(stage)).get("scrapWeight");
if(published==null)continue; // RankingCoverage proved this stage has no source rows.
BigDecimal formal=decimal(published);
if(formal==null||formal.signum()<0)return Map.of();
denominator=denominator.add(formal);
}
if(denominator.signum()==0) {
Map<String,Object> empty=emptyRanking("overall",BigDecimal.ZERO);
if(compositeRateAvailable) {
BigDecimal officialRate=decimal(summary.get("compositeRate"));
if(officialRate!=null&&officialRate.signum()>=0)empty.put("compositeRate",officialRate);
}
rankings.add(empty);
return Map.of("version",1,"releaseId",release,"dimension",dimension,"period",task.get("period"),"filters",task.get("filters"),"rankings",rankings);
}
if(denominator.signum()<0)return Map.of();
List<Map<String,Object>> rows=new ArrayList<>();
BigDecimal cumulative=BigDecimal.ZERO;
for(int i=0;i<Math.min(10,positive.size());i++) {
var row=new LinkedHashMap<>(positive.get(i));cumulative=cumulative.add((BigDecimal)row.get("weight"));
row.put("rank",i+1);row.put("share",((BigDecimal)row.get("weight")).divide(denominator,java.math.MathContext.DECIMAL128));
row.put("cumulativeShare",cumulative.divide(denominator,java.math.MathContext.DECIMAL128));rows.add(row);
}
Map<String,Object> combined=new LinkedHashMap<>();combined.put("stage","overall");combined.put("metric","scrapWeight");combined.put("rows",rows);
if(compositeRateAvailable) {
BigDecimal officialRate=decimal(summary.get("compositeRate"));
if(officialRate!=null&&officialRate.signum()>=0)combined.put("compositeRate",officialRate);
}
combined.put("totalRows",positive.size());combined.put("returnedRows",rows.size());combined.put("truncated",positive.size()>rows.size());
combined.put("denominator",denominator);combined.put("completeRanking",true);combined.put("orderPolicy","weight_desc_key_codepoint_asc");rankings.add(combined);
}
for(Object stage:stages)for(Object field:AnalysisProtocolService.list(task.get("fields"))) {
Map<String,Object> one=new LinkedHashMap<>(task);one.put("stages",List.of(stage));one.put("fields",List.of(field));one.put("dimensionStages",Map.of(dimension,List.of(stage)));
if(!AnalysisProtocolRankingCoverage.complete(response,one,dimension))continue;
Object rawDenominator=AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get(stage)).get(field);
BigDecimal denominator=decimal(rawDenominator);
if("scrapWeight".equals(field)&&denominator!=null&&denominator.signum()==0) {
rankings.add(emptyRanking(stage,denominator));
continue;
}
List<Map<String,Object>> positive=new ArrayList<>();BigDecimal negative=BigDecimal.ZERO;int negativeCount=0;
for(Object raw:AnalysisProtocolService.list(response.get("rows"))) {
Map<String,Object> row=AnalysisProtocolService.map(raw);Object value=AnalysisProtocolService.map(row.get(stage)).get(field);
@@ -36,4 +109,18 @@ final class AnalysisProtocolRankArtifact {
while(i<a.length()&&j<b.length()){int x=a.codePointAt(i),y=b.codePointAt(j);if(x!=y)return Integer.compare(x,y);i+=Character.charCount(x);j+=Character.charCount(y);}
return Boolean.compare(i<a.length(),j<b.length());
}
private static int number(Object raw) {
if(!(raw instanceof Number value))return 0;
try{return new BigDecimal(value.toString()).intValueExact();}catch(RuntimeException invalid){return 0;}
}
private static BigDecimal decimal(Object raw) {
if(!(raw instanceof Number value))return null;
try{return new BigDecimal(value.toString());}catch(RuntimeException invalid){return null;}
}
private static Map<String,Object> emptyRanking(Object stage,BigDecimal denominator) {
Map<String,Object> empty=new LinkedHashMap<>();empty.put("stage",stage);empty.put("metric","scrapWeight");empty.put("rows",List.of());
empty.put("totalRows",0);empty.put("returnedRows",0);empty.put("truncated",false);empty.put("denominator",denominator);
empty.put("completeRanking",true);empty.put("emptyReason","no_positive_scrap");empty.put("orderPolicy","weight_desc_key_codepoint_asc");
return empty;
}
}
@@ -8,27 +8,51 @@ final class AnalysisProtocolRankingCoverage {
private AnalysisProtocolRankingCoverage() {}
static boolean complete(Map<String,Object> response,Map<String,Object> task,String dimension) {
return complete(response,task,dimension,false);
}
private static boolean complete(Map<String,Object> response,Map<String,Object> task,String dimension,boolean allowZeroScrap) {
List<?> rows=AnalysisProtocolService.list(response.get("rows"));
Object raw=AnalysisProtocolService.map(response.get("context")).getOrDefault("rowCount",rows.size());
if(!(raw instanceof Number n)||n.doubleValue()!=n.longValue()||n.longValue()!=rows.size()||rows.size()>100000)return false;
Map<String,Object> perDimension=AnalysisProtocolService.map(task.get("dimensionStages"));
List<?> stages=AnalysisProtocolService.list(perDimension.getOrDefault(dimension,task.get("stages")));
if(stages.isEmpty())return false;
if(stages.isEmpty()) {
List<?> fields=AnalysisProtocolService.list(task.get("fields"));
if(!fields.contains("scrapWeight")||!Set.of("rate","scrapWeight").containsAll(fields))return false;
Map<String,Object> expanded=new LinkedHashMap<>(task);
expanded.put("stages",List.of("black","bright"));expanded.put("fields",List.of("scrapWeight"));
expanded.put("dimensionStages",Map.of(dimension,List.of("black","bright")));
BigDecimal black=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("black")).get("scrapWeight"));
BigDecimal bright=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("bright")).get("scrapWeight"));
boolean blackAbsent=knownNoSourceStage(response,"black");
boolean brightAbsent=knownNoSourceStage(response,"bright");
if(black==null&&blackAbsent)black=BigDecimal.ZERO;
if(bright==null&&brightAbsent)bright=BigDecimal.ZERO;
if(black==null||bright==null||black.signum()<0||bright.signum()<0||blackAbsent&&brightAbsent)return false;
if(black.add(bright).signum()>0)return complete(response,expanded,dimension,true);
return hasObservedStageRows(response)&&complete(response,expanded,dimension,true);
}
Set<Object> keys=new HashSet<>();
Map<String,Object> summaryCoverage=AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("coverage"));
for(Object value:rows) {
Object key=AnalysisProtocolService.map(value).get("key");
if(!(key instanceof String text)||text.isBlank()||!keys.add(key))return false;
}
for(Object stage:stages)for(Object field:AnalysisProtocolService.list(task.get("fields"))) {
if(allowZeroScrap&&"scrapWeight".equals(field)&&knownNoSourceStage(response,Objects.toString(stage)))continue;
BigDecimal total=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get(stage)).get(field));
if(total==null||total.signum()<=0)return false;
Long stageSourceRows=integer(summaryCoverage.get(stage+"Rows"));
if("scrapWeight".equals(field)&&stageSourceRows!=null&&stageSourceRows==0&&total!=null&&total.signum()!=0)return false;
boolean observedZeroScrap="scrapWeight".equals(field)&&total!=null&&total.signum()==0&&hasObservedStageRows(response,Objects.toString(stage));
if(total==null||total.signum()<0||total.signum()==0&&!allowZeroScrap&&!observedZeroScrap)return false;
BigDecimal sum=BigDecimal.ZERO;
for(Object value:rows) {
Map<String,Object> row=AnalysisProtocolService.map(value);
BigDecimal weight=number(AnalysisProtocolService.map(row.get(stage)).get(field));
Long rowSourceRows=integer(AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows"));
if("scrapWeight".equals(field)&&rowSourceRows!=null&&rowSourceRows==0&&weight!=null&&weight.signum()!=0)return false;
if(weight==null) {
Object count=AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows");
if(count instanceof Number c&&c.doubleValue()==0)continue;
if(rowSourceRows!=null&&rowSourceRows==0)continue;
return false;
}
// Black formal total already includes the existing signed utilization correction.
@@ -39,6 +63,65 @@ final class AnalysisProtocolRankingCoverage {
}
return true;
}
private static boolean knownNoSourceStage(Map<String,Object> response,String stage) {
if(!Set.of("black","bright").contains(stage))return false;
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
Map<String,Object> summaryCoverage=AnalysisProtocolService.map(summary.get("coverage"));
if(!Long.valueOf(0L).equals(integer(summaryCoverage.get(stage+"Rows"))))return false;
Object rawMonths=AnalysisProtocolService.map(response.get("coverage")).get("sourceMonths");
if(!(rawMonths instanceof List<?> months)||months.isEmpty())return false;
String other="black".equals(stage)?"bright":"black";
long otherRows=0;
for(Object raw:months) {
Map<String,Object> month=AnalysisProtocolService.map(raw);
Long own=integer(month.get(stage+"Rows")),otherCount=integer(month.get(other+"Rows"));
if(own==null||own!=0||otherCount==null||otherCount<0||own+otherCount==0)return false;
otherRows+=otherCount;
}
Long publishedOther=integer(summaryCoverage.get(other+"Rows"));
if(publishedOther!=null&&publishedOther!=otherRows)return false;
Map<String,Object> stageSummary=AnalysisProtocolService.map(summary.get(stage));
if(stageSummary.containsKey("scrapWeight")) {
BigDecimal weight=number(stageSummary.get("scrapWeight"));
if(weight==null||weight.signum()!=0)return false;
}
List<?> rows=AnalysisProtocolService.list(response.get("rows"));
if(rows.isEmpty())return false;
for(Object raw:rows) {
Map<String,Object> row=AnalysisProtocolService.map(raw);
Long count=integer(AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows"));
if(count==null||count!=0)return false;
Map<String,Object> stageRow=AnalysisProtocolService.map(row.get(stage));
if(stageRow.containsKey("scrapWeight")) {
BigDecimal weight=number(stageRow.get("scrapWeight"));
if(weight==null||weight.signum()!=0)return false;
}
}
return otherRows>0;
}
private static Long integer(Object raw) {
if(!(raw instanceof Number number))return null;
try{return new BigDecimal(number.toString()).longValueExact();}
catch(RuntimeException invalid){return null;}
}
private static boolean hasObservedStageRows(Map<String,Object> response) {
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
Map<String,Object> coverage=AnalysisProtocolService.map(summary.get("coverage"));
Long black=nonNegativeCount(coverage.get("blackRows")),bright=nonNegativeCount(coverage.get("brightRows"));
return black!=null&&bright!=null&&(black>0||bright>0);
}
private static boolean hasObservedStageRows(Map<String,Object> response,String stage) {
if(!Set.of("black","bright").contains(stage))return false;
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
Map<String,Object> coverage=AnalysisProtocolService.map(summary.get("coverage"));
Long rows=nonNegativeCount(coverage.get(stage+"Rows"));
return rows!=null&&rows>0;
}
private static Long nonNegativeCount(Object raw) {
if(!(raw instanceof Number number))return null;
try {long value=new BigDecimal(number.toString()).longValueExact();return value<0?null:value;}
catch(RuntimeException invalid){return null;}
}
static boolean visualsComplete(Map<String,Object> answer,Map<String,Object> task) {
if(!"ready".equals(answer.get("visualState")))return false;
String type=Objects.toString(task.get("visualType"),"bar");
@@ -3,6 +3,7 @@ package cn.yunsheng.quality.analysis;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.options.OptionsController;
import cn.yunsheng.quality.common.SelectionValues;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
@@ -276,7 +277,7 @@ public class AnalysisProtocolService {
var queries=new ArrayList<Map<String,Object>>();
for(Object raw:list(source.get("queries"))) {
var q=map(raw);var one=new LinkedHashMap<String,Object>();
for(String key:List.of("queryId","target","parameters","responseHash","nodeId","scopeId","response","task","scope","coverage"))if(q.containsKey(key))one.put(key,q.get(key));
for(String key:List.of("queryId","target","parameters","responseHash","nodeId","scopeId","response","task","scope","coverage","rankArtifact"))if(q.containsKey(key))one.put(key,q.get(key));
var proof=new LinkedHashMap<String,Object>();for(String key:List.of("turnId","sessionId","attemptId","executionId","revision"))proof.put(key,out.get(key));
proof.put("queryId",q.get("queryId"));proof.put("responseHash",q.get("responseHash"));
for(String key:List.of("nodeId","scopeId"))if(q.get(key)!=null)proof.put(key,q.get(key));one.put("proof",proof);queries.add(one);
@@ -343,6 +344,14 @@ public class AnalysisProtocolService {
if(ids.isEmpty()||new HashSet<>(ids).size()!=ids.size()||!known.containsAll(ids)||!name.equals(ref.get("evidenceRef")))throw denied("恢复图表引用了其他执行的查询");
refs.add(Map.of("evidenceRef",name,"queryIds",ids));references.add(name);
}
var rankBindings=new ArrayList<RankingArtifactBinding>();
for(var binding:refs)if(list(binding.get("queryIds")).size()==1)for(Object id:list(binding.get("queryIds"))) {
var query=list(input.get("queries")).stream().map(AnalysisProtocolService::map)
.filter(item->Objects.equals(item.get("queryId"),id)).findFirst().orElse(Map.of());
Map<String,Object> artifact=map(query.get("rankArtifact"));
if(!artifact.isEmpty())rankBindings.add(new RankingArtifactBinding(Objects.toString(id),Objects.toString(binding.get("evidenceRef")),
Objects.toString(query.get("nodeId"),null),Objects.toString(query.get("scopeId"),null),artifact));
}
var original=AnalysisVisualSnapshotAccess.fromAnswer(answer,fields,AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc,fields),turn,expected);
for(var chart:snapshot.visuals())if(!references.containsAll(list(chart.get("evidenceRefs")))&&original.visuals().stream().noneMatch(old->Objects.equals(canonical(old),canonical(chart))))throw denied("恢复图表缺少原始查询引用");
var merged=new LinkedHashMap<String,Map<String,Object>>();
@@ -355,11 +364,11 @@ public class AnalysisProtocolService {
var candidate=new LinkedHashMap<>(snapshot.asMap());candidate.put("visuals",merged.values().stream().limit(12).toList());
if(!merged.isEmpty())candidate.put("visualState","ready");
if(task.containsKey("queryPlan")||AnalysisProtocolExploration.nativeAgent(task))for(Object raw:list(input.get("scopes"))) {
var scope=map(raw);if(!rawScope(scope)&&!plannedVisualsComplete(candidate,scope,release))chartsComplete=false;
var scope=map(raw);if(!rawScope(scope)&&!plannedVisualsComplete(candidate,scope,release,rankBindings))chartsComplete=false;
}
else {
String purpose=!list(task.get("dimensions")).isEmpty()?"ranking":Boolean.TRUE.equals(task.get("trendRequired"))?"trend":"metrics";
if(!plannedVisualsComplete(candidate,Map.of("task",task,"purpose",purpose),release))chartsComplete=false;
if(!plannedVisualsComplete(candidate,Map.of("task",task,"purpose",purpose),release,rankBindings))chartsComplete=false;
}
chartsComplete&=gaps.isEmpty();if(!chartsComplete&&gaps.isEmpty())gaps.add("所需图表尚未全部恢复,已保留可用图表和原分析结果");
var result=new LinkedHashMap<>(candidate);result.put("chartsComplete",chartsComplete);result.put("queryRefs",refs);result.put("missingEvidence",gaps.stream().distinct().toList());return result;
@@ -616,7 +625,16 @@ public class AnalysisProtocolService {
binding.current(a.sessionId(),a.turnId(),a.userId(),a.revision(),a.executionId());
Map<String,Object> task=read(store.revision(a.sessionId(),a.revision()).taskJson());
if(!Objects.equals(canonical(task),canonical(details.get("agentTask")))) throw denied("最终答复没有绑定当前任务");
if(Set.of("COMPLETED","PARTIAL").contains(status)) {
if(Set.of("COMPLETED","PARTIAL").contains(status)&&!"report".equals(task.get("kind"))
&&!hasProtocolEvidence(details)) {
status="FAILED";
answer.clear();
answer.put("answer",noProofFailureReason(details));
answer.put("results",List.of());
answer.put("releaseId",a.releaseId());
answer.put("engine","NAO");
answer.put("stopReason","formal_query_evidence_missing");
} else if(Set.of("COMPLETED","PARTIAL").contains(status)) {
governance.requireBoundAction("QUERY","query.aggregate");
if(AnalysisProtocolInspection.KIND.equals(task.get("kind"))) {
validateEvidence(a,details);
@@ -624,10 +642,12 @@ public class AnalysisProtocolService {
answer.clear();answer.putAll(inspection.finish(a,task,ids,actor));
if(!Boolean.TRUE.equals(map(answer.get("inspectionCoverage")).get("complete")))status="PARTIAL";
} else if("report".equals(task.get("kind"))) {
for(String key:List.of("historySources","historySections","historyCoverage","historyEvidence","reportMarkdown","reportHtml","reportTitle","visuals","visualSnapshot","executionCoverage","executionScopes","readingSummary","queryEvidenceRefs"))answer.remove(key);
for(String key:List.of("historySources","historySections","historyCoverage","historyEvidence","historyVisualSources","reportMarkdown","reportHtml","reportTitle","visuals","visualSnapshot","executionCoverage","executionScopes","readingSummary","queryEvidenceRefs"))answer.remove(key);
var checked=history.finish(a,task,details,actor,historicalQueryValidator(),autonomousSession(a.sessionId()));
answer.putAll(checked);answer.put("protocolQueryIds",List.of());answer.put("results",List.of());
answer.put("visuals",List.of());answer.put("visualState","not_requested");answer.put("visualSchemaVersion",1);
if(!(checked.get("visuals") instanceof List<?>))answer.put("visuals",List.of());
if(!(checked.get("visualState") instanceof String))answer.put("visualState","not_requested");
if(!checked.containsKey("visualSchemaVersion"))answer.put("visualSchemaVersion",1);
boolean complete=Boolean.TRUE.equals(map(checked.get("historyCoverage")).get("complete"));
if(!complete){status="PARTIAL";if(autonomousSession(a.sessionId()))appendCoverageNotices(answer,list(map(checked.get("historyCoverage")).get("missingEvidence")));}
answer.put("stopReason",complete&&"COMPLETED".equals(status)?"history_report_complete":"history_report_partial");
@@ -641,6 +661,16 @@ public class AnalysisProtocolService {
else factsComplete&=Boolean.TRUE.equals(checked.get("factsComplete"));
Object recordList=checked.remove("recordList");if(recordList!=null)answer.put("recordList",recordList);
answer.put("executionCoverage",checked);answer.put("executionScopes",planResolution(a,task,actor).scopes());
if(answer.get("answerQuality") instanceof Map<?,?>) {
var quality=new LinkedHashMap<>(map(answer.get("answerQuality")));
quality.put("dataComplete",Boolean.TRUE.equals(checked.get("factsComplete")));
if(Boolean.TRUE.equals(checked.get("factsComplete"))&&!Boolean.TRUE.equals(checked.get("complete")))quality.put("chartsComplete",false);
if(!Boolean.TRUE.equals(checked.get("complete"))) {
Set<Object> gaps=new LinkedHashSet<>(list(quality.get("missingEvidence")));gaps.addAll(list(checked.get("missingEvidence")));
quality.put("missingEvidence",List.copyOf(gaps));
}
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"))) {
for(String field:List.of("reportMarkdown","reportHtml","reportTitle"))answer.remove(field);
@@ -655,7 +685,12 @@ public class AnalysisProtocolService {
} else {
validateMetricEvidence(a,task,details);
factsComplete&=metricFactsComplete(a,task,details);
if(!list(task.get("dimensions")).isEmpty()&&(!completeRankingCoverage(a,task,details)||!AnalysisProtocolRankingCoverage.visualsComplete(answer,task)))status="PARTIAL";
if(!list(task.get("dimensions")).isEmpty()) {
var rankingScope=new LinkedHashMap<String,Object>();rankingScope.put("purpose","ranking");rankingScope.put("task",task);
var rankBindings=rankingArtifactBindings(a,details);
boolean visualsRequired=!"none".equals(task.get("visualPreference"));
if(!completeRankingCoverage(a,task,details)||visualsRequired&&!plannedVisualsComplete(answer,rankingScope,a.releaseId(),rankBindings))status="PARTIAL";
}
if(task.get("comparisonPeriod") instanceof Map<?,?>&&!completeComparisonCoverage(a,task,details))status="PARTIAL";
}
var accepted=list(map(answer.get("executionCoverage")).get("acceptedProtocolQueryIds"));
@@ -722,6 +757,38 @@ public class AnalysisProtocolService {
if(count==null||count!=1)throw denied("查询证据属于其他任务或已失效");
}
}
private boolean hasProtocolEvidence(Map<String,Object> details) {
return details.get("protocolEvidence") instanceof List<?> refs&&!refs.isEmpty();
}
private String noProofFailureReason(Map<String,Object> details) {
Map<String,Object> diagnostic=list(details.get("failureDiagnostics")).stream()
.map(AnalysisProtocolService::map).findFirst().orElse(Map.of());
String kind=Objects.toString(diagnostic.get("failureKind"),"");
return switch(kind) {
case "source_coverage" -> "当前已确认范围没有可用的正式来源数据,本轮未提交业务结论。";
case "formal_query_rejected" -> "正式查询响应未通过当前范围或覆盖核验,本轮未提交业务结论。";
case "transient_http" -> {
Long status=safeHttpStatus(diagnostic.get("httpStatus"));
boolean classifiedRetryable=Boolean.TRUE.equals(diagnostic.get("retryable"));
if(classifiedRetryable&&status!=null&&Set.of(429L,502L,503L,504L).contains(status))
yield "正式查询服务暂时不可用(HTTP "+status+"),本轮未提交业务结论,可稍后重试。";
yield "正式查询未取得可核验回执,本轮未提交业务结论。";
}
case "http_error" -> {
Long status=safeHttpStatus(diagnostic.get("httpStatus"));
yield status==null?"正式查询服务未返回可用结果,本轮未提交业务结论。"
:"正式查询服务返回HTTP "+status+",本轮未提交业务结论。";
}
case "transport_unavailable" -> "正式查询连接暂时不可用,本轮未提交业务结论,可稍后重试。";
case "query_rejected" -> "正式查询未通过已确认的范围或参数校验,本轮未提交业务结论。";
default -> "本轮未取得可核验的正式查询回执,未提交业务结论。";
};
}
private Long safeHttpStatus(Object raw) {
if(!(raw instanceof Number number))return null;
try {long value=new java.math.BigDecimal(number.toString()).longValueExact();return value>=400&&value<=599?value:null;}
catch(RuntimeException invalid){return null;}
}
@Transactional
public Map<String,Object> queries(String attemptId,AppUserPrincipal actor) {
lock(sessionForAttempt(attemptId));Attempt a=require(attemptId,actor,true);
@@ -1085,9 +1152,10 @@ public class AnalysisProtocolService {
var state=nativeScopeState(a,task,actor);
// Older native workers supplied all registered scopes. Keep that conservative compatibility path.
if(!details.containsKey("conclusionProtocolQueryIds")) {
var result=new LinkedHashMap<>(plannedCoverage(a,task,details,answer,actor,"required".equals(task.get("visualPreference"))));
var result=new LinkedHashMap<>(plannedCoverage(a,task,details,answer,actor,true));
var gaps=new LinkedHashSet<String>();for(Object gap:list(result.get("missingEvidence")))if(gap instanceof String text)gaps.add(text);gaps.addAll(state.rootGaps());result.put("missingEvidence",List.copyOf(gaps));result.put("complete",gaps.isEmpty());
result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",state.warnings());result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
var warnings=new LinkedHashSet<String>(state.warnings());for(Object gap:list(result.get("visualGaps")))if(gap instanceof String text)warnings.add(text);
result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",List.copyOf(warnings));result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
}
Object raw=details.get("conclusionProtocolQueryIds");var selected=list(raw);
if(!(raw instanceof List<?>)||selected.size()>40||new HashSet<>(selected).size()!=selected.size()||selected.stream().anyMatch(id->!(id instanceof String)))throw denied("最终结论查询引用格式无效");
@@ -1111,14 +1179,18 @@ public class AnalysisProtocolService {
}
var activeIds=active.stream().map(scope->scope.get("scopeId")).collect(java.util.stream.Collectors.toSet());
var activeProofs=state.queries().stream().map(q->map(q.get("proof"))).filter(p->activeIds.contains(p.get("scopeId"))).toList();
var result=new LinkedHashMap<>(plannedCoverage(a,task,Map.of("protocolEvidence",activeProofs),answer,actor,false,new AnalysisProtocolQueryPlan.Resolution(active,List.of(),List.of(),List.of()),false));
var warnings=new LinkedHashSet<String>(state.warnings());
boolean required="required".equals(task.get("visualPreference"));boolean requiredReady=false;
for(var scope:active)if(!rawScope(scope)) {
if(!plannedVisualsComplete(answer,scope,a.releaseId()))warnings.add(Objects.toString(scope.get("label"),"所选范围")+"所需图表尚未完整生成,可独立恢复图表");
if(required){var requiredScope=new LinkedHashMap<>(scope);var child=new LinkedHashMap<>(map(scope.get("task")));child.put("visualPreference","required");requiredScope.put("task",child);requiredReady|=plannedVisualsComplete(answer,requiredScope,a.releaseId());}
boolean required="required".equals(task.get("visualPreference"));
var checkedScopes=new ArrayList<Map<String,Object>>();for(var scope:active) {
if(!required||rawScope(scope)){checkedScopes.add(scope);continue;}
var requiredScope=new LinkedHashMap<>(scope);var child=new LinkedHashMap<>(map(scope.get("task")));child.put("visualPreference","required");requiredScope.put("task",child);checkedScopes.add(requiredScope);
}
if(required&&!requiredReady)fatal.add("用户要求的图表尚未完整生成");
var activeDetails=new LinkedHashMap<String,Object>();activeDetails.put("protocolEvidence",activeProofs);
if(details.containsKey("queryEvidenceBindings"))activeDetails.put("queryEvidenceBindings",details.get("queryEvidenceBindings"));
var result=new LinkedHashMap<>(plannedCoverage(a,task,activeDetails,answer,actor,true,new AnalysisProtocolQueryPlan.Resolution(checkedScopes,List.of(),List.of(),List.of()),false));
var visualGaps=new LinkedHashSet<String>();for(Object gap:list(result.get("visualGaps")))if(gap instanceof String text)visualGaps.add(text);
warnings.addAll(visualGaps);fatal.addAll(visualGaps);
if(required&&!visualGaps.isEmpty())fatal.add("用户要求的图表尚未完整生成");
result.put("factsComplete",selectedEvidenceComplete&&state.rootGaps().isEmpty()&&state.coverage().stream().filter(c->selectedScopes.contains(c.get("scopeId"))).allMatch(c->Boolean.TRUE.equals(c.get("complete")))&&!selected.isEmpty());
result.put("complete",fatal.isEmpty());result.put("missingEvidence",List.copyOf(fatal));result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",List.copyOf(warnings));result.put("conclusionProtocolQueryIds",List.copyOf(selected));result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
}
@@ -1130,7 +1202,37 @@ public class AnalysisProtocolService {
String key=switch(Objects.toString(scope.get("purpose"))){case "ranking"->"weightsComplete";case "defects"->"defectsComplete";case "changes"->"changesComplete";case "source"->"sourceVerificationComplete";case "records"->"recordsPageComplete";default->"metricsComplete";};
return Boolean.TRUE.equals(map(query.get("coverage")).get(key));
}
private record PlannedQuery(String id,String node,String scope,Map<String,String> parameters,Map<String,Object> coverage,String target,Map<String,Object> response){}
private record PlannedQuery(String id,String node,String scope,Map<String,String> parameters,Map<String,Object> coverage,String target,Map<String,Object> response,Map<String,Object> rankArtifact){}
private record RankingArtifactBinding(String id,String evidenceRef,String node,String scope,Map<String,Object> artifact){}
private Map<String,List<String>> queryEvidenceBindings(Object raw) {
Map<String,List<String>> result=new LinkedHashMap<>();
for(Object item:list(raw)) {
Map<String,Object> binding=map(item);Object ref=binding.get("evidenceRef");List<?> rawIds=list(binding.get("queryIds"));
if(!(ref instanceof String reference)||!reference.matches("Q[A-F0-9]{8}")||rawIds.isEmpty()||rawIds.size()>40
||rawIds.stream().anyMatch(id->!(id instanceof String text)||text.isBlank())
||new HashSet<>(rawIds).size()!=rawIds.size()||result.containsKey(reference))continue;
result.put(reference,rawIds.stream().map(Object::toString).toList());
}
return result;
}
private List<RankingArtifactBinding> rankingArtifactBindings(List<PlannedQuery> queries,Object rawBindings) {
var refs=queryEvidenceBindings(rawBindings);var result=new ArrayList<RankingArtifactBinding>();
for(var query:queries)if(Boolean.TRUE.equals(query.coverage().get("weightsComplete"))&&!query.rankArtifact().isEmpty())
for(var entry:refs.entrySet())if(entry.getValue().size()==1&&Objects.equals(entry.getValue().getFirst(),query.id()))
result.add(new RankingArtifactBinding(query.id(),entry.getKey(),query.node(),query.scope(),query.rankArtifact()));
return List.copyOf(result);
}
private List<RankingArtifactBinding> rankingArtifactBindings(Attempt a,Map<String,Object> details) {
var queries=new ArrayList<PlannedQuery>();
for(Object raw:list(details.get("protocolEvidence"))) {
Map<String,Object> proof=map(raw);Object queryId=proof.get("queryId");
var rows=jdbc.query("SELECT protocol_node_id,protocol_scope_id,coverage_json,rank_artifact_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",
(rs,n)->new PlannedQuery(Objects.toString(queryId),rs.getString(1),rs.getString(2),Map.of(),read(rs.getString(3)),"GET /api/metrics",Map.of(),read(rs.getString(4))),
queryId,a.attemptId(),a.executionId());
if(rows.size()==1)queries.add(rows.getFirst());
}
return rankingArtifactBindings(queries,details.get("queryEvidenceBindings"));
}
private Map<String,Object> plannedCoverage(Attempt a,Map<String,Object> task,Map<String,Object> details,Map<String,Object> answer,AppUserPrincipal actor) {
return plannedCoverage(a,task,details,answer,actor,true);
}
@@ -1145,9 +1247,9 @@ public class AnalysisProtocolService {
List<PlannedQuery> verified=new ArrayList<>();
for(Object item:list(details.get("protocolEvidence"))) {
Map<String,Object> ref=map(item);
var queries=jdbc.query("SELECT protocol_node_id,protocol_scope_id,parameters_json,coverage_json,query_target,response_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",(rs,n)->{
var queries=jdbc.query("SELECT protocol_node_id,protocol_scope_id,parameters_json,coverage_json,query_target,response_json,rank_artifact_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",(rs,n)->{
Map<String,String> parameters=new LinkedHashMap<>();read(rs.getString(3)).forEach((k,v)->parameters.put(k,Objects.toString(v)));
return new PlannedQuery(Objects.toString(ref.get("queryId")),rs.getString(1),rs.getString(2),canonicalQuery(parameters),read(rs.getString(4)),rs.getString(5),read(rs.getString(6)));
return new PlannedQuery(Objects.toString(ref.get("queryId")),rs.getString(1),rs.getString(2),canonicalQuery(parameters),read(rs.getString(4)),rs.getString(5),read(rs.getString(6)),read(rs.getString(7)));
},ref.get("queryId"),a.attemptId(),a.executionId());
if(queries.size()!=1)throw denied("分步任务没有对应的正式节点查询证据");
PlannedQuery query=queries.getFirst();
@@ -1157,6 +1259,7 @@ public class AnalysisProtocolService {
if(!matches)throw denied("节点证据不属于当前合法的查询范围或期间");verified.add(query);
}
List<AnalysisRecordSnapshot.Input> recordInputs=new ArrayList<>();List<Map<String,Object>> sourceChecks=new ArrayList<>();
List<RankingArtifactBinding> rankBindings=rankingArtifactBindings(verified,details.get("queryEvidenceBindings"));
for(Map<String,Object> scope:resolution.scopes()) {
String label=Objects.toString(scope.get("label"),"所选范围");boolean ranking="ranking".equals(scope.get("purpose"));
if("changes".equals(scope.get("purpose"))) {
@@ -1167,7 +1270,7 @@ public class AnalysisProtocolService {
missing.add(label+"变化排名尚未取得完整同范围正式依据");
for(var query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
}
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需变化图表尚未完整生成");
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需变化图表尚未完整生成");
continue;
}
if("defects".equals(scope.get("purpose"))) {
@@ -1179,7 +1282,7 @@ public class AnalysisProtocolService {
for(var query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
}
}
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需缺陷图表尚未完整生成");
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需缺陷图表尚未完整生成");
continue;
}
if(rawScope(scope)) {
@@ -1211,7 +1314,7 @@ public class AnalysisProtocolService {
for(PlannedQuery query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
}
}
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需图表尚未完整生成");
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需图表尚未完整生成");
}
if(resolution.scopes().isEmpty())missing.add("没有可核验的实际查询范围");
Map<String,Object> result=new LinkedHashMap<>();
@@ -1224,6 +1327,7 @@ public class AnalysisProtocolService {
}
result.put("factsComplete",missing.isEmpty());missing.addAll(visualMissing);
result.put("complete",missing.isEmpty());result.put("missingEvidence",missing.stream().distinct().toList());result.put("scopeCount",resolution.scopes().size());
result.put("visualGaps",visualMissing.stream().distinct().toList());
if(!sourceChecks.isEmpty())result.put("sourceChecks",sourceChecks);
result.put("skipped",resolution.skipped());
result.put("verifiedQueryCount",verified.stream().map(PlannedQuery::id).distinct().count());return result;
@@ -1249,8 +1353,19 @@ public class AnalysisProtocolService {
return String.join("\n\n",lines);
}
private boolean plannedVisualsComplete(Map<String,Object> answer,Map<String,Object> scope,String release) {
return plannedVisualsComplete(answer,scope,release,List.of());
}
private boolean plannedVisualsComplete(Map<String,Object> answer,Map<String,Object> scope,String release,List<RankingArtifactBinding> rankBindings) {
Map<String,Object> child=map(scope.get("task"));String purpose=Objects.toString(scope.get("purpose"));
boolean required=!"none".equals(child.get("visualPreference"))&&("ranking".equals(purpose)||"trend".equals(purpose)||"required".equals(child.get("visualPreference"))||child.get("visualType")!=null);
boolean metricScope="metrics".equals(purpose)||"metrics".equals(child.get("kind"));
boolean singleObjectMetricRanking=singleObjectMetricRanking(scope,child,purpose);
boolean optionalAutoScalarMetric=metricScope&&"auto".equals(child.get("visualPreference"))
&&!Boolean.TRUE.equals(child.get("comparisonRequired"))&&!Boolean.TRUE.equals(child.get("trendRequired"))
&&list(child.get("fields")).size()==1&&list(child.get("stages")).size()<=1;
boolean required=!"none".equals(child.get("visualPreference"))&&("ranking".equals(purpose)&&!singleObjectMetricRanking||"trend".equals(purpose)
||"required".equals(child.get("visualPreference"))
||metricScope&&Boolean.TRUE.equals(child.get("comparisonRequired"))
||child.get("visualType")!=null&&!optionalAutoScalarMetric&&!singleObjectMetricRanking);
if("changes".equals(purpose)&&!"none".equals(child.get("visualPreference"))) {
var query=map(child.get("changeQuery"));
boolean related=list(answer.get("visuals")).stream().map(AnalysisProtocolService::map).anyMatch(chart->Objects.equals(query.get("dimension"),chart.get("dimension"))
@@ -1260,12 +1375,15 @@ public class AnalysisProtocolService {
required="required".equals(child.get("visualPreference"))||related;
}
if(!required)return true;
if("ranking".equals(purpose)&&!rankingHasVisualDataOrUnknown(scope,release,rankBindings))return true;
if(!"ready".equals(answer.get("visualState")))return false;
List<Map<String,Object>> charts=list(answer.get("visuals")).stream().map(AnalysisProtocolService::map).filter(chart->{
Map<String,Object> actual=map(chart.get("scope"));Map<String,Object> business=new LinkedHashMap<>();
for(String key:FILTERS)if(actual.containsKey(key))business.put(key,actual.get(key));
boolean sameScope="changes".equals(purpose)?AnalysisProtocolChanges.sameFilters(business,map(child.get("filters")))
:Objects.equals(canonical(AnalysisProtocolQueryPlan.identityFilters(business)),canonical(AnalysisProtocolQueryPlan.identityFilters(map(child.get("filters")))));
// Snapshot normalization omits cleared empty rules. Compare their
// business meaning without changing saved scope IDs or accepting a
// missing non-empty restriction.
boolean sameScope=AnalysisProtocolChanges.sameFilters(business,map(child.get("filters")));
if(!release.equals(actual.get("releaseId"))||!sameScope)return false;
if(!chartPeriodMatches(map(chart.get("period")),map(child.get("period")),"trend".equals(purpose)))return false;
return !Boolean.TRUE.equals(child.get("comparisonRequired"))||chartPeriodMatches(map(chart.get("comparisonPeriod")),map(child.get("comparisonPeriod")),false);
@@ -1286,20 +1404,80 @@ public class AnalysisProtocolService {
}
List<?> stages=list(child.get("stages")).isEmpty()?List.of("overall"):list(child.get("stages"));
for(Object field:list(child.get("fields")))for(Object rawStage:stages) {
// Composite rate is a companion column on the same scrap-ranked
// population; it does not require a second independently sorted chart.
if("ranking".equals(purpose)&&list(child.get("stages")).isEmpty()&&"rate".equals(field))continue;
String stage=rawStage.toString();Set<String> metrics=switch(field.toString()) {
case "rate"->Set.of(stage.equals("overall")?"composite_rate":stage+"_rate");
case "scrapRate"->Set.of(stage+"_scrap_rate");case "totalWeight"->Set.of(stage+"_total_weight");
case "scrapWeight"->Set.of(stage+"_scrap_weight","current_scrap_weight");default->Set.of();
};
if("ranking".equals(purpose)&&"scrapWeight".equals(field)
&&!rankingTargetHasVisualDataOrUnknown(scope,release,rankBindings,stage))continue;
boolean found=charts.stream().anyMatch(chart->metrics.contains(chart.get("metric"))
&&(stage.equals("overall")?chart.get("stage")==null:stage.equals(chart.get("stage")))
&&("ranking".equals(purpose)?"all_scope_weight".equals(map(chart.get("coverage")).get("shareBasis"))&&list(child.get("dimensions")).contains(chart.get("dimension")):
"trend".equals(purpose)?Set.of("line","area","heatmap").contains(chart.get("type")):true)
&&(child.get("visualType")==null||Objects.equals(child.get("visualType"),chart.get("type"))));
&&(child.get("visualType")==null||Objects.equals(child.get("visualType"),chart.get("type")))
&&(!"ranking".equals(purpose)||rankingChartBound(chart,scope,release,rankBindings)));
if(!found)return false;
}
return true;
}
private boolean singleObjectMetricRanking(Map<String,Object> scope,Map<String,Object> child,String purpose) {
if(!"ranking".equals(purpose)||!"metrics".equals(child.get("kind"))
||!"auto".equals(child.get("visualPreference"))
||Boolean.TRUE.equals(child.get("comparisonRequired"))||Boolean.TRUE.equals(child.get("trendRequired"))
||child.get("comparisonPeriod") instanceof Map<?,?>
||!list(child.get("stages")).isEmpty()
||!new HashSet<>(list(child.get("fields"))).equals(Set.of("rate","scrapWeight"))
||!Objects.equals(scope.get("limit"),1))return false;
List<?> dimensions=list(child.get("dimensions"));
if(dimensions.size()!=1||!(dimensions.getFirst() instanceof String dimension)
||!Set.of("product","department","group").contains(dimension))return false;
Object selected=map(child.get("filters")).get(dimension);
if(!(selected instanceof String wire)||wire.isBlank())return false;
try {if(SelectionValues.parse(wire).size()!=1)return false;}catch(RuntimeException invalid){return false;}
return list(map(child.get("dimensionStages")).get(dimension)).isEmpty();
}
private boolean rankingHasVisualDataOrUnknown(Map<String,Object> scope,String release,List<RankingArtifactBinding> bindings) {
Map<String,Object> child=map(scope.get("task"));List<?> stages=list(child.get("stages")).isEmpty()?List.of("overall"):list(child.get("stages"));
boolean hasVisualTarget=false;
for(Object field:list(child.get("fields")))for(Object stage:stages) {
if(list(child.get("stages")).isEmpty()&&"rate".equals(field))continue;
hasVisualTarget=true;
if(!"scrapWeight".equals(field)||rankingTargetHasVisualDataOrUnknown(scope,release,bindings,Objects.toString(stage)))return true;
}
return !hasVisualTarget;
}
private boolean rankingTargetHasVisualDataOrUnknown(Map<String,Object> scope,String release,
List<RankingArtifactBinding> bindings,String stage) {
Map<String,Object> child=map(scope.get("task"));List<?> dimensions=list(child.get("dimensions"));
if(dimensions.isEmpty())return true;
for(Object dimension:dimensions) {
var candidates=bindings.stream().filter(binding->bindingMatchesScope(binding,scope))
.filter(binding->Objects.equals(dimension,binding.artifact().get("dimension"))).toList();
if(candidates.isEmpty()||candidates.stream().anyMatch(binding->
!AnalysisVisualSnapshotAccess.rankingArtifactProvesEmptyScrap(binding.artifact(),child,release,Objects.toString(dimension),stage)))return true;
}
return false;
}
private boolean bindingMatchesScope(RankingArtifactBinding binding,Map<String,Object> scope) {
if(scope.get("nodeId")!=null||scope.get("scopeId")!=null)
return Objects.equals(scope.get("nodeId"),binding.node())&&Objects.equals(scope.get("scopeId"),binding.scope());
return binding.node()==null&&binding.scope()==null;
}
private boolean rankingChartBound(Map<String,Object> chart,Map<String,Object> scope,String release,List<RankingArtifactBinding> bindings) {
List<?> refs=list(chart.get("evidenceRefs"));if(refs.size()!=1||!(refs.getFirst() instanceof String ref))return false;
Map<String,Object> child=map(scope.get("task"));int limit=scope.get("limit") instanceof Number n?n.intValue():child.get("limit") instanceof Number n?n.intValue():10;
for(var binding:bindings) {
if(!Objects.equals(ref,binding.evidenceRef()))continue;
if(!bindingMatchesScope(binding,scope))continue;
if(AnalysisVisualSnapshotAccess.rankingChartMatchesArtifact(chart,binding.artifact(),child,release,limit,ref))return true;
}
return false;
}
private boolean chartPeriodMatches(Map<String,Object> actual,Map<String,Object> expected,boolean trend) {
if(actual.isEmpty()||expected.isEmpty())return false;
Map<String,Object> plain=new LinkedHashMap<>(actual);plain.remove("label");
@@ -59,6 +59,7 @@ public class AnalysisTurnService {
private static final int PLAN_REVISIONS_MAX = 8;
private static final int DEADLINE_SECONDS = 240;
private static final int NAO_DEADLINE_SECONDS = Math.toIntExact(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT.toSeconds());
private static final int NAO_TOOL_CALLS_MAX = 1000;
private static final int NATIVE_HISTORY_QUERY_LIMIT = 12;
private static final int NATIVE_HISTORY_ENTRY_LIMIT = 8;
private static final int NATIVE_HISTORY_CONTEXT_LIMIT = 16_000;
@@ -345,8 +346,14 @@ public class AnalysisTurnService {
private Map<String, Object> visibleTurnPayload(Turn turn, AppUserPrincipal actor, Map<String, Object> context) {
Map<String, Object> answer = read(turn.answerJson());
if(protocol!=null&&protocol.ownsTurn(turn.id())&&"report".equals(mapValue(mapValue(readNullable(turn.planJson(),Map.of())).get("task")).get("kind"))
&&Set.of("COMPLETED","PARTIAL").contains(turn.status()))protocol.authorizeReportSnapshot(turn.id(),actor);
Map<String,Object> plan=readNullable(turn.planJson(),Map.of());
Map<String,Object> task=mapValue(plan.get("task"));
boolean authorizedHistoryReport=false;
if(protocol!=null&&protocol.ownsTurn(turn.id())&&"report".equals(task.get("kind"))
&&Set.of("COMPLETED","PARTIAL").contains(turn.status())) {
protocol.authorizeReportSnapshot(turn.id(),actor);
authorizedHistoryReport="confirmed_history_report".equals(answer.get("completionMethod"));
}
if(protocol!=null&&protocol.ownsTurn(turn.id())&&AnalysisProtocolInspection.KIND.equals(mapValue(mapValue(readNullable(turn.planJson(),Map.of())).get("task")).get("kind"))
&&Set.of("COMPLETED","PARTIAL").contains(turn.status()))protocol.authorizeInspectionSnapshot(turn.id(),actor);
Map<String, Object> budget = readNullable(turn.budgetJson(), Map.of());
@@ -361,10 +368,10 @@ public class AnalysisTurnService {
try { governance.requireEvidence(actor); result.put("evidence", readAny(turn.evidenceJson(), List.of())); }
catch (ResponseStatusException denied) { result.put("evidence", List.of()); result.put("evidenceAvailable", false); }
result.put("answer", answer.getOrDefault("answer", "")); result.put("results", filterResultList(answer.get("results"), actor));
result.putAll(visibleVisuals(answer, turn, actor, context));
result.putAll(visibleVisuals(answer, turn, actor, context,authorizedHistoryReport));
for (String field : List.of("engine", "explanationEngine", "explanationWarning", "warning",
"businessResultsSentToModel", "releaseId", "task", "analysisChecks", "agentDecisions", "completion",
"reportMarkdown", "reportHtml", "reportTitle", "nativeChatId", "toolCallCount", "protocol", "queryEvidenceRefs", "actualModel", "phaseTimingsMs", "executionMode", "readingSummary", "scopeCoverage", "explorationWarnings", "conclusionProtocolQueryIds", "attemptProtocolQueryIds", "memberList", "recordList", "historySources", "historySections", "historyCoverage", "historyEvidence", "inspectionCoverage", "controlledInspection"))
"reportMarkdown", "reportHtml", "reportTitle", "nativeChatId", "toolCallCount", "protocol", "queryEvidenceRefs", "actualModel", "phaseTimingsMs", "executionMode", "completionMethod", "readingSummary", "answerQuality", "scopeCoverage", "explorationWarnings", "conclusionProtocolQueryIds", "attemptProtocolQueryIds", "memberList", "recordList", "historySources", "historySections", "historyCoverage", "historyEvidence", "historyVisualSources", "inspectionCoverage", "controlledInspection"))
if (answer.containsKey(field)) result.put(field, answer.get(field));
result.put("stopReason", turn.stopReason()); result.put("budget", budget);
result.putAll(jdbc.queryForObject("SELECT UNIX_TIMESTAMP(created_at)*1000,UNIX_TIMESTAMP(started_at)*1000,UNIX_TIMESTAMP(finished_at)*1000 FROM analysis_turn WHERE id=? AND user_id=?",
@@ -935,6 +942,7 @@ public class AnalysisTurnService {
String attemptId=attempt.attemptId();
try {
if(attempt.input().containsKey("controlledInspection")) {asSecurityActor(actor,()->{runProtocolInspection(attempt,actor,session);return null;});return;}
initializeNativeBudget(turn.id(),turn.budgetJson());
NaoAnalysisRunner.Result result=naoRunner.run(turn.id(),session.id(),actor,turn.question(),session.releaseId(),protocol.runtimeContext(attempt,actor),
progress->{
if(progress.isEmpty()||protocol.isStopped(attemptId))return;
@@ -975,13 +983,7 @@ public class AnalysisTurnService {
Map<String, Object> plan = new LinkedHashMap<>();
Map<String, Object> submittedPlan = readNullable(turn.planJson(), Map.of());
Map<String, Object> context = nativeContext(submittedPlan, session.releaseId());
Map<String, Object> nativeBudget = readNullable(turn.budgetJson(), Map.of());
nativeBudget.put("toolCallsMax", 1000);
nativeBudget.put("planRevisionsMax", 0);
nativeBudget.put("maxOutputTokens", AnalysisRuntimePolicy.MAX_OUTPUT_TOKENS);
nativeBudget.put("deadlineAt", Instant.now().plusSeconds(NAO_DEADLINE_SECONDS).toString());
jdbc.update("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'",
write(nativeBudget), turnId);
initializeNativeBudget(turnId,turn.budgetJson());
plan.put("status", "NATIVE_NAO");
plan.put("nativeRuntime", true);
plan.put("engine", "NAO");
@@ -1074,6 +1076,21 @@ public class AnalysisTurnService {
}
}
void initializeNativeBudget(String turnId,String currentBudgetJson) {
Map<String,Object> nativeBudget=readNullable(currentBudgetJson,Map.of());
nativeBudget.put("toolCallsMax",NAO_TOOL_CALLS_MAX);
nativeBudget.put("toolCallsUsed",0);
nativeBudget.put("toolCallsRemaining",NAO_TOOL_CALLS_MAX);
nativeBudget.put("planRevisionsMax",0);
nativeBudget.put("planRevisionsUsed",0);
nativeBudget.put("maxOutputTokens",AnalysisRuntimePolicy.MAX_OUTPUT_TOKENS);
nativeBudget.put("turnTimeoutSeconds",AnalysisRuntimePolicy.TURN_TIMEOUT.toSeconds());
nativeBudget.put("requestTimeoutSeconds",AnalysisRuntimePolicy.REQUEST_TIMEOUT.toSeconds());
nativeBudget.put("deadlineAt",Instant.now().plus(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT).toString());
jdbc.update("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'",
write(nativeBudget),turnId);
}
private Map<String, Object> nativeContext(Map<String, Object> persistedPlan, String releaseId) {
Map<String, Object> frozen = mapValue(persistedPlan.get("frozenContext"));
if (frozen.isEmpty()) frozen = mapValue(persistedPlan.get("context"));
@@ -1603,6 +1620,14 @@ public class AnalysisTurnService {
if (details.get("timeline") instanceof List<?>) answer.put("timeline", NaoAnalysisRunner.safeTimeline(details.get("timeline")));
if(AnalysisProtocolExploration.nativeAgent(protocolTask))answer.put("executionMode","native_agent");
var summary=boundedReadingSummary(details.get("readingSummary"),answer.get("queryEvidenceRefs"));if(!summary.isEmpty())answer.put("readingSummary",summary);
if(details.get("answerQuality") instanceof Map<?,?> quality) {
var safe=new LinkedHashMap<String,Object>();safe.put("version",1);
for(String field:List.of("dataComplete","narrativeComplete","chartsComplete"))
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());
answer.put("answerQuality",safe);
}
Object count = details.get("toolCount");
if (count instanceof Number) answer.put("toolCallCount", count);
if(details.get("actualModel") instanceof String name&&name.matches("[A-Za-z0-9_./:@+\\-]{1,160}"))answer.put("actualModel",name);
@@ -2751,9 +2776,15 @@ public class AnalysisTurnService {
}
private Map<String, Object> visibleVisuals(Map<String, Object> answer, Turn turn, AppUserPrincipal actor,
Map<String, Object> expectedScope) {
return visibleVisuals(answer,turn,actor,expectedScope,false);
}
private Map<String, Object> visibleVisuals(Map<String, Object> answer, Turn turn, AppUserPrincipal actor,
Map<String, Object> expectedScope,boolean authorizedHistoryReport) {
if (!visualsEnabled) return AnalysisVisualSnapshotAccess.empty("not_requested").asMap();
Set<String> allowedFields = governance.allowedFields(actor);
Set<String> visibleFilters = AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc, allowedFields);
if(authorizedHistoryReport)return visibleHistoryVisuals(answer,turn,allowedFields,visibleFilters);
Map<String, Object> scope = expectedScope;
if (isNativePlan(readNullable(turn.planJson(), Map.of()))) {
String releaseId = clean(expectedScope == null ? null : expectedScope.get("releaseId"));
@@ -2762,6 +2793,66 @@ public class AnalysisTurnService {
}
return AnalysisVisualSnapshotAccess.fromAnswer(answer, allowedFields, visibleFilters, turn.id(), cleanContext(scope)).asMap();
}
private Map<String,Object> visibleHistoryVisuals(Map<String,Object> answer,Turn turn,Set<String> allowedFields,
Set<String> visibleFilters) {
if(!"confirmed_history_report".equals(answer.get("completionMethod")))
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
if(!"ready".equals(answer.get("visualState")))
return AnalysisVisualSnapshotAccess.fromAnswer(answer,allowedFields,visibleFilters,turn.id(),Map.of()).asMap();
if(!(answer.get("visuals") instanceof List<?> charts)||charts.isEmpty()
||!(answer.get("historyVisualSources") instanceof List<?> lineages)||lineages.size()!=charts.size())
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
Map<String,Map<String,Object>> sourceByVisualId=new LinkedHashMap<>();
Set<String> signedSourceTurns=new HashSet<>();
for(Object raw:objectList(answer.get("historySources"))) {
Map<String,Object> source=mapValue(raw);String id=clean(source.get("turnId"));
if(Boolean.TRUE.equals(source.get("available"))&&id!=null)signedSourceTurns.add(id);
}
for(Object raw:lineages) {
Map<String,Object> lineage=mapValue(raw);
if(!lineage.keySet().containsAll(Set.of("visualId","sourceTurnId","scope","period","comparisonPeriod","dimension","stage","metric","evidenceRefs")))
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
String visualId=clean(lineage.get("visualId"));
String sourceTurnId=clean(lineage.get("sourceTurnId"));
if(visualId==null||sourceTurnId==null||!signedSourceTurns.contains(sourceTurnId)
||sourceByVisualId.putIfAbsent(visualId,lineage)!=null)
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
}
Set<String> reportRefs=new HashSet<>();
for(Object ref:objectList(answer.get("queryEvidenceRefs")))if(ref instanceof String text)reportRefs.add(text);
List<Map<String,Object>> checkedCharts=new ArrayList<>();Set<String> checkedIds=new HashSet<>();
for(Object raw:charts) {
Map<String,Object> chart=mapValue(raw);String visualId=clean(chart.get("id"));
Map<String,Object> lineage=sourceByVisualId.get(visualId);
if(lineage==null||!checkedIds.add(visualId))return AnalysisVisualSnapshotAccess.empty("failed").asMap();
if(!Objects.equals(turn.id(),chart.get("originTurnId"))
||!sameCanonical(chart.get("scope"),lineage.get("scope"))
||!sameCanonical(chart.get("period"),lineage.get("period"))
||!sameCanonical(chart.get("comparisonPeriod"),lineage.get("comparisonPeriod"))
||!sameCanonical(chart.get("dimension"),lineage.get("dimension"))
||!sameCanonical(chart.get("stage"),lineage.get("stage"))
||!sameCanonical(chart.get("metric"),lineage.get("metric"))
||!sameCanonical(chart.get("evidenceRefs"),lineage.get("evidenceRefs")))
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
List<?> refs=chart.get("evidenceRefs") instanceof List<?> values?values:List.of();
if(refs.isEmpty()||!reportRefs.containsAll(refs))return AnalysisVisualSnapshotAccess.empty("failed").asMap();
Map<String,Object> sourceScope=mapValue(lineage.get("scope"));
if(sourceScope.isEmpty())return AnalysisVisualSnapshotAccess.empty("failed").asMap();
Map<String,Object> rebound=new LinkedHashMap<>(chart);rebound.put("originTurnId",turn.id());
Map<String,Object> oneChart=new LinkedHashMap<>(answer);oneChart.put("visuals",List.of(rebound));
var checked=AnalysisVisualSnapshotAccess.fromAnswer(oneChart,allowedFields,visibleFilters,turn.id(),sourceScope);
if(!"ready".equals(checked.state())||checked.visuals().size()!=1)
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
checkedCharts.add(checked.visuals().getFirst());
}
return new AnalysisVisualSnapshotAccess.Snapshot(AnalysisVisualSnapshotAccess.SCHEMA_VERSION,"ready",List.copyOf(checkedCharts)).asMap();
}
private boolean sameCanonical(Object left,Object right) {
return Objects.equals(AnalysisProtocolService.canonical(left),AnalysisProtocolService.canonical(right));
}
private Map<String,Object> stringMap(Map<?,?> map) { Map<String,Object> out=new LinkedHashMap<>();map.forEach((k,v)->out.put(String.valueOf(k),v));return out; }
private String write(Object value) { return json.writeValueAsString(value); }
private String sha256(String value) { try{return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));}catch(Exception e){throw new IllegalStateException(e);} }
@@ -96,6 +96,149 @@ public final class AnalysisVisualSnapshotAccess {
allowedFields, visibleExtraFilterKeys, turnId, expectedScope);
}
/** Binds a normalized ranking chart to the authoritative rank artifact and its real query reference. */
static boolean rankingChartMatchesArtifact(Map<String,Object> chart, Map<String,Object> artifact,
Map<String,Object> task, String release, int limit,
String expectedEvidenceRef) {
try {
if (chart == null || artifact == null || task == null || expectedEvidenceRef == null
|| !List.of(expectedEvidenceRef).equals(chart.get("evidenceRefs"))
|| !Objects.equals(release, artifact.get("releaseId"))
|| !Objects.equals(chart.get("dimension"), artifact.get("dimension"))
|| !AnalysisProtocolService.list(task.get("dimensions")).contains(artifact.get("dimension"))
|| !AnalysisProtocolChanges.sameFilters(stringMap(artifact.get("filters")), stringMap(task.get("filters")))
|| !rankingPeriodMatches(artifact.get("period"), task.get("period"))
|| !rankingPeriodMatches(chart.get("period"), task.get("period"))) return false;
String visualMetric = chart.get("metric") instanceof String value ? value : "";
String stage;
String field;
if ("current_scrap_weight".equals(visualMetric)) {
stage = "overall"; field = "scrapWeight";
if (chart.get("stage") != null) return false;
} else if (visualMetric.startsWith("black_") || visualMetric.startsWith("bright_")) {
stage = visualMetric.startsWith("black_") ? "black" : "bright";
if (!Objects.equals(stage, chart.get("stage"))) return false;
field = visualMetric.endsWith("_scrap_weight") ? "scrapWeight"
: visualMetric.endsWith("_total_weight") ? "totalWeight" : "";
} else return false;
if (field.isEmpty() || !AnalysisProtocolService.list(task.get("fields")).contains(field)
|| !AnalysisProtocolService.list(task.get("stages")).isEmpty()
&& !AnalysisProtocolService.list(task.get("stages")).contains(stage)) return false;
Map<String,Object> ranking = AnalysisProtocolService.list(artifact.get("rankings")).stream()
.map(AnalysisProtocolService::map)
.filter(item -> Objects.equals(stage,item.get("stage")) && Objects.equals(field,item.get("metric"))
&& Boolean.TRUE.equals(item.get("completeRanking")))
.findFirst().orElse(null);
if (ranking == null || !"weight_desc_key_codepoint_asc".equals(ranking.get("orderPolicy"))) return false;
Long totalRows = longInteger(ranking.get("totalRows"));
BigDecimal denominator = decimal(ranking.get("denominator"));
List<?> rows = ranking.get("rows") instanceof List<?> values ? values : null;
if (totalRows == null || totalRows < 0 || rows == null || rows.size() != Math.min(10L,totalRows)
|| denominator == null || denominator.signum() <= 0 || limit < 1 || limit > 10) return false;
List<?> categories = chart.get("categories") instanceof List<?> values ? values : null;
List<?> series = chart.get("series") instanceof List<?> values ? values : null;
if (categories == null || series == null || series.size() != 1) return false;
Map<String,Object> oneSeries = AnalysisProtocolService.map(series.getFirst());
List<?> values = oneSeries.get("values") instanceof List<?> points ? points : null;
int expectedRows = (int)Math.min(limit,totalRows);
if (categories.size() != expectedRows || values == null || values.size() != expectedRows
|| !Objects.equals(visualMetric,oneSeries.get("key"))) return false;
Map<String,Object> coverage = stringMap(chart.get("coverage"));
if (!Objects.equals("all_scope_weight",coverage.get("shareBasis"))
|| !integerEquals(coverage.get("totalRows"),Math.toIntExact(totalRows))
|| !integerEquals(coverage.get("returnedRows"),expectedRows)
|| !Objects.equals(coverage.get("truncated"),expectedRows < totalRows)
|| !sameDecimal(coverage.get("denominator"),denominator)) return false;
BigDecimal previousWeight = null;
String previousKey = null;
for (int index = 0; index < expectedRows; index++) {
Map<String,Object> row = AnalysisProtocolService.map(rows.get(index));
Map<String,Object> category = AnalysisProtocolService.map(categories.get(index));
BigDecimal weight = decimal(row.get("weight"));
String key = row.get("key") instanceof String text ? text : null;
String label = row.get("label") instanceof String text ? text : null;
if (integerEquals(row.get("rank"),index+1) == false || weight == null || weight.signum() <= 0
|| key == null || key.isBlank() || label == null || label.isBlank()
|| !Objects.equals(key,category.get("key")) || !Objects.equals(label,category.get("label"))
|| !sameDecimal(values.get(index),weight)
|| previousWeight != null && (previousWeight.compareTo(weight) < 0
|| previousWeight.compareTo(weight) == 0 && codePointCompare(previousKey,key) > 0)) return false;
previousWeight = weight; previousKey = key;
}
return true;
} catch (RuntimeException invalid) { return false; }
}
/** Accepts only a server-signed empty scrap ranking as proof that no chartable data exists. */
static boolean rankingArtifactProvesEmptyScrap(Map<String,Object> artifact,Map<String,Object> task,
String release,String dimension,String stage) {
try {
if(artifact==null||task==null||!Objects.equals(release,artifact.get("releaseId"))
||!Objects.equals(dimension,artifact.get("dimension"))
||!AnalysisProtocolService.list(task.get("dimensions")).contains(dimension)
||!AnalysisProtocolService.list(task.get("fields")).contains("scrapWeight"))return false;
List<?> taskStages=AnalysisProtocolService.list(task.get("stages"));
if(taskStages.isEmpty()? !"overall".equals(stage) : !taskStages.contains(stage)||"overall".equals(stage))return false;
if(!AnalysisProtocolChanges.sameFilters(stringMap(artifact.get("filters")),stringMap(task.get("filters")))
||!rankingPeriodMatches(artifact.get("period"),task.get("period")))return false;
Map<String,Object> ranking=AnalysisProtocolService.list(artifact.get("rankings")).stream()
.map(AnalysisProtocolService::map)
.filter(item->Objects.equals(stage,item.get("stage"))&&"scrapWeight".equals(item.get("metric")))
.findFirst().orElse(null);
if(ranking==null||!Boolean.TRUE.equals(ranking.get("completeRanking"))
||!"weight_desc_key_codepoint_asc".equals(ranking.get("orderPolicy"))
||!"no_positive_scrap".equals(ranking.get("emptyReason"))
||!integerEquals(ranking.get("totalRows"),0)
||!integerEquals(ranking.get("returnedRows"),0)
||!Boolean.FALSE.equals(ranking.get("truncated")))return false;
List<?> rows=ranking.get("rows") instanceof List<?> values?values:null;
BigDecimal denominator=decimal(ranking.get("denominator"));
return rows!=null&&rows.isEmpty()&&denominator!=null&&denominator.signum()==0;
} catch(RuntimeException invalid) { return false; }
}
private static boolean rankingPeriodMatches(Object actualRaw,Object expectedRaw) {
Map<String,Object> actual = stringMap(actualRaw), expected = stringMap(expectedRaw);
actual.remove("label"); actual.remove("granularity");
if (actual.containsKey("start") || actual.containsKey("end")) {
if (!"month".equals(stringMap(actualRaw).get("granularity")) || !(actual.get("start") instanceof String start)
|| !(actual.get("end") instanceof String end) || !start.matches("[0-9]{4}-[0-9]{2}")
|| !end.matches("[0-9]{4}-[0-9]{2}")) return false;
Long year = number(expected.get("year")); if (year == null) return false;
int first = expected.containsKey("monthStart") ? Math.toIntExact(number(expected.get("monthStart")))
: expected.containsKey("month") ? Math.toIntExact(number(expected.get("month")))
: expected.containsKey("quarter") ? (Math.toIntExact(number(expected.get("quarter")))-1)*3+1 : 1;
int last = expected.containsKey("monthEnd") ? Math.toIntExact(number(expected.get("monthEnd")))
: expected.containsKey("month") ? first : expected.containsKey("quarter") ? first+2 : 12;
return start.equals(String.format(java.util.Locale.ROOT,"%04d-%02d",year,first))
&& end.equals(String.format(java.util.Locale.ROOT,"%04d-%02d",year,last));
}
return Objects.equals(AnalysisProtocolService.canonical(actual),AnalysisProtocolService.canonical(expected));
}
private static Long number(Object raw) {
if (!(raw instanceof Number value)) return null;
try { return Long.valueOf(value.toString()); }
catch (NumberFormatException invalid) { return null; }
}
private static boolean sameDecimal(Object left,Object right) {
BigDecimal a=decimal(left),b=decimal(right);
return a!=null&&b!=null&&a.compareTo(b)==0;
}
private static int codePointCompare(String left,String right) {
int i=0,j=0;
while(i<left.length()&&j<right.length()) {
int a=left.codePointAt(i),b=right.codePointAt(j);if(a!=b)return Integer.compare(a,b);
i+=Character.charCount(a);j+=Character.charCount(b);
}
return Boolean.compare(i<left.length(),j<right.length());
}
/** Current enabled filter fields whose source columns are both visible to this identity. */
public static Set<String> visibleExtraFilterKeys(JdbcTemplate jdbc, Set<String> allowedFields) {
Set<String> allowed = allowedFields == null ? Set.of() : allowedFields;
@@ -174,7 +317,7 @@ public final class AnalysisVisualSnapshotAccess {
|| !validWaterfallChart(type, metric, unit, semantics, categories, series, coverage, period, comparisonPeriod)) return null;
if ("all_scope_weight".equals(coverage.get("shareBasis"))
&& (!Set.of("product","department","group").contains(dimension)
|| !Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric))) return null;
|| !Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric))) return null;
if (!allowedFields.contains("*") && !allowedFields.containsAll(requiredFields)) return null;
Map<String, Object> scope = normalizeScope(input.get("scope"), visibleExtraFilterKeys, expectedScope);
Object scopedStage = scope == null ? null : scope.get("stage");
@@ -240,7 +383,7 @@ public final class AnalysisVisualSnapshotAccess {
BigDecimal decimal = decimal(point);
if (decimal == null || decimal.precision() > 64 || Math.abs(decimal.scale()) > 64) return null;
if (Set.of("composite_rate", "black_rate", "bright_rate").contains(metric)
&& (decimal.signum() < 0 || decimal.compareTo(BigDecimal.ONE) > 0)) return null;
&& decimal.signum() < 0) return null;
if (Set.of("black_scrap_rate", "bright_scrap_rate").contains(metric) && decimal.signum() < 0) return null;
if (Set.of("black_good_weight", "black_scrap_weight", "black_total_weight",
"black_responsibility_scrap", "black_material_scrap", "black_external_scrap",
@@ -249,8 +392,6 @@ public final class AnalysisVisualSnapshotAccess {
"bright_processing_scrap", "bright_diffusion_scrap", "current_scrap_weight",
"source_defect_weight").contains(metric) && decimal.signum() < 0
&& !("line".equals(type)&&"black_total_weight".equals(metric))) return null;
if (Set.of("rate_drop_pp", "composite_impact_pp").contains(metric)
&& decimal.abs().compareTo(BigDecimal.valueOf(100)) > 0) return null;
numbers.add(decimal);
}
}
@@ -470,8 +611,8 @@ public final class AnalysisVisualSnapshotAccess {
Map<String, Object> comparisonPeriod) {
if (!Set.of("donut", "pareto").contains(type)) return true;
if ("pareto".equals(type) && "all_scope_weight".equals(coverage.get("shareBasis"))) {
if (!Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric)
|| !"kg".equals(unit) || semantics.stage() == null || comparisonPeriod != null
if (!Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric)
|| !"kg".equals(unit) || semantics.stage() == null&&!"current_scrap_weight".equals(metric) || comparisonPeriod != null
|| categories.isEmpty() || categories.size() > 10 || series.size() != 1
|| !integerEquals(coverage.get("returnedRows"),categories.size())) return false;
BigDecimal denominator = decimal(coverage.get("denominator")), sum = BigDecimal.ZERO, previous = null;
@@ -528,7 +669,7 @@ public final class AnalysisVisualSnapshotAccess {
BigDecimal[] points = new BigDecimal[3];
for (int index = 0; index < values.size(); index++) {
points[index] = decimal(values.get(index));
if (points[index] == null || points[index].abs().compareTo(BigDecimal.valueOf(100)) > 0) return false;
if (points[index] == null) return false;
}
BigDecimal tolerance = new BigDecimal("0.000001");
return points[0].add(points[1]).subtract(points[2]).abs().compareTo(tolerance) <= 0;
@@ -552,7 +693,7 @@ public final class AnalysisVisualSnapshotAccess {
MetricSemantics semantics, Map<String, Object> scope,
Map<String, Object> period) {
if (section == null && dimension == null) return true; // schema-1 snapshots created before chapter metadata
if (Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric)
if (Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric)
&& Set.of("bar","column","pareto").contains(type)
&& dimension != null && Set.of("department","group","product").contains(dimension))
return section == null || section.equals(dimension);
@@ -0,0 +1,30 @@
package cn.yunsheng.quality.analysis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
/** Upgrade the current immutable release once; older releases retain the exact source fallback. */
@Component
public class FineDefectRollupBackfill implements ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(FineDefectRollupBackfill.class);
private final JdbcTemplate jdbc;
private final FineDefectRollups rollups;
public FineDefectRollupBackfill(JdbcTemplate jdbc, FineDefectRollups rollups) { this.jdbc = jdbc; this.rollups = rollups; }
@Override public void run(ApplicationArguments args) {
var missing = jdbc.query("""
SELECT DISTINCT b.id FROM current_release c JOIN release_member rm ON rm.release_id=c.release_id
JOIN transform_build b ON b.id=rm.transform_build_id
LEFT JOIN fine_defect_rollup_build rb ON rb.build_id=b.id
WHERE c.dataset_id='quality' AND b.status='COMPLETED' AND rb.build_id IS NULL ORDER BY b.id
""", (rs, row) -> rs.getString(1));
long start = System.nanoTime();
for (String build : missing) rollups.refreshBuild(build);
if (!missing.isEmpty()) LOG.info("Fine defect rollup backfill: builds={}, elapsedMs={}", missing.size(), (System.nanoTime() - start) / 1_000_000L);
}
}
@@ -0,0 +1,180 @@
package cn.yunsheng.quality.analysis;
import cn.yunsheng.quality.metrics.MetricsRollupService.GroupKey;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tools.jackson.databind.json.JsonMapper;
import static cn.yunsheng.quality.analysis.AnalysisFineDefectsService.*;
/** Mergeable source statistics, frozen in the same transaction as each transform build. */
@Service
public class FineDefectRollups {
private final JdbcTemplate jdbc;
private final JsonMapper json;
public FineDefectRollups(JdbcTemplate jdbc, JsonMapper json) { this.jdbc = jdbc; this.json = json; }
@Transactional
public void refreshBuild(String build) {
jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build);
jdbc.update("DELETE FROM fine_defect_rollup WHERE build_id=?", build);
long rows = 0;
for (String stage : List.of("black", "bright")) {
var groups = aggregate(build, stage, "", List.of());
if (!representable(groups)) return; // Unusual precision keeps the exact source-query fallback.
insert(build, groups);
rows += groups.values().stream().mapToLong(a -> a.rollupStats().getFirst().rows()).sum();
}
refreshOrganizations(build, "", List.of());
jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) VALUES(?,?)", build, rows);
}
public void copyBuild(String parent, String child) {
Integer ready = jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup_build WHERE build_id=?", Integer.class, parent);
if (ready == null || ready != 1) { refreshBuild(child); return; }
jdbc.update("""
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
SELECT ?,stage,month_key,department,group_name,project_group,product,grain,
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count
FROM fine_defect_rollup WHERE build_id=?
""", child, parent);
jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) SELECT ?,source_row_count FROM fine_defect_rollup_build WHERE build_id=?", child, parent);
}
public void refreshGroups(String build, Set<GroupKey> keys) {
for (String stage : List.of("black", "bright")) {
var selected = keys.stream().filter(k -> stage.equals(k.stage())).toList();
if (selected.isEmpty()) continue;
String predicate = String.join(" OR ", java.util.Collections.nCopies(selected.size(),
"(f.month_key<=>? AND f.department COLLATE utf8mb4_0900_as_cs<=>? AND f.group_name COLLATE utf8mb4_0900_as_cs<=>? AND f.project_group COLLATE utf8mb4_0900_as_cs<=>? AND f.product COLLATE utf8mb4_0900_as_cs<=>?)"));
List<Object> args = new ArrayList<>();
for (var key : selected) { args.add(key.month()); args.add(key.department()); args.add(key.group()); args.add(key.project()); args.add(key.product()); }
List<Object> deletedArgs = new ArrayList<>(List.of(build, stage)); deletedArgs.addAll(args);
jdbc.update("DELETE f FROM fine_defect_rollup f WHERE f.build_id=? AND f.stage=? AND f.grain='detail' AND (" + predicate + ")", deletedArgs.toArray());
var groups = aggregate(build, stage, " AND (" + predicate + ")", args);
if (!representable(groups)) { jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build); return; }
insert(build, groups);
}
var organizations = keys.stream().map(k -> new GroupKey(k.stage(), k.month(), k.department(), k.group(), null, null)).distinct().toList();
if (!organizations.isEmpty()) {
String predicate = String.join(" OR ", java.util.Collections.nCopies(organizations.size(),
"(f.stage=? AND f.month_key<=>? AND f.department COLLATE utf8mb4_0900_as_cs<=>? AND f.group_name COLLATE utf8mb4_0900_as_cs<=>?)"));
List<Object> args = new ArrayList<>();
for (var key : organizations) { args.add(key.stage()); args.add(key.month()); args.add(key.department()); args.add(key.group()); }
refreshOrganizations(build, " AND (" + predicate + ")", args);
}
}
private void refreshOrganizations(String build, String predicate, List<Object> scopeArgs) {
List<Object> args = new ArrayList<>(List.of(build)); args.addAll(scopeArgs);
jdbc.update("DELETE f FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='organization'" + predicate, args.toArray());
jdbc.update("""
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
SELECT f.build_id,f.stage,f.month_key,f.department,f.group_name,NULL,NULL,'organization',
f.stat_kind,f.field_key,SUM(f.weight_sum),SUM(f.present_rows),SUM(f.missing_rows),
SUM(f.invalid_rows),SUM(f.negative_rows),SUM(f.source_row_count)
FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='detail'
""" + predicate + " GROUP BY f.build_id,f.stage,f.month_key,f.department,f.group_name,f.stat_kind,f.field_key", args.toArray());
}
boolean read(Aggregator aggregate, Request request, Set<String> allowed, String predicate, List<Object> scopeArgs) {
Integer missing = jdbc.queryForObject("""
SELECT COUNT(*) FROM release_member rm LEFT JOIN fine_defect_rollup_build rb ON rb.build_id=rm.transform_build_id
WHERE rm.release_id=? AND rb.build_id IS NULL
""", Integer.class, scopeArgs.getFirst());
if (missing == null || missing > 0) return false;
List<Object> args = new ArrayList<>(scopeArgs);
args.add(request.stage());
// Scope predicates are generated by the shared authorization helper, never supplied as SQL by a caller.
boolean organization = !predicate.contains("f.product") && !predicate.contains("f.project_group");
args.add(organization ? "organization" : "detail");
List<String> selected = new ArrayList<>();
for (var stat : aggregate.rollupStats()) {
selected.add("(f.stat_kind=? AND f.field_key=?)"); args.add(stat.kind()); args.add(stat.key());
}
String sql = """
SELECT f.stat_kind,f.field_key,SUM(f.weight_sum) AS weight_sum,
SUM(f.present_rows) AS present_rows,SUM(f.missing_rows) AS missing_rows,
SUM(f.invalid_rows) AS invalid_rows,SUM(f.negative_rows) AS negative_rows,
SUM(f.source_row_count) AS source_row_count
FROM release_member rm
JOIN transform_build b ON b.id=rm.transform_build_id AND b.source_revision_id=rm.source_revision_id
JOIN source_revision sr ON sr.id=rm.source_revision_id
JOIN fine_defect_rollup f
""" + (organization ? " FORCE INDEX (ix_fine_rollup_build_month) " : " ")
+ " ON f.build_id=rm.transform_build_id " + predicate
+ " AND f.stage=? AND f.grain=? AND (" + String.join(" OR ", selected) + ") GROUP BY f.stat_kind,f.field_key";
jdbc.query(sql, (RowCallbackHandler) rs -> aggregate.mergeRollup(new Aggregator.RollupStat(
rs.getString("stat_kind"), rs.getString("field_key"), rs.getBigDecimal("weight_sum"),
rs.getLong("present_rows"), rs.getLong("missing_rows"), rs.getLong("invalid_rows"),
rs.getLong("negative_rows"), rs.getLong("source_row_count")), allowed), args.toArray());
return true;
}
private Map<GroupKey, Aggregator> aggregate(String build, String stage, String predicate, List<Object> scopeArgs) {
Request request = new Request(build, "top", null, null, null, null, null, null, null, null,
true, stage, "defect", null, null, 100, Map.of());
var fields = fields(stage); var categories = categories(stage);
Set<String> components = new LinkedHashSet<>();
for (var category : categories) if (fields.stream().anyMatch(f -> f.category().equals(category.name()))) components.add(category.componentCode());
List<String> rawKeys = new ArrayList<>(fields.stream().map(DefectField::key).toList());
categories.stream().map(c -> ExpectedSourceDetailPolicy.totalField(c.componentCode())).filter(java.util.Objects::nonNull).forEach(rawKeys::add);
String sql = "SELECT f.month_key,f.department,f.group_name,f.project_group,f.product," + jsonObject("f.raw_values", rawKeys)
+ " AS raw_fields," + jsonObject("f.components", new ArrayList<>(components)) + " AS formal_components FROM fact_" + stage + " f WHERE f.build_id=?" + predicate;
List<Object> args = new ArrayList<>(List.of(build)); args.addAll(scopeArgs);
Map<GroupKey, Aggregator> groups = new LinkedHashMap<>();
jdbc.query(connection -> {
var statement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE); new ArgumentPreparedStatementSetter(args.toArray()).setValues(statement); return statement;
}, (ResultSetExtractor<Void>) rs -> {
while (rs.next()) {
GroupKey key = new GroupKey(stage, rs.getString("month_key"), rs.getString("department"), rs.getString("group_name"), rs.getString("project_group"), rs.getString("product"));
groups.computeIfAbsent(key, ignored -> new Aggregator(request, fields, categories, components, json))
.add(readObject(json, rs.getString("raw_fields")), readObject(json, rs.getString("formal_components")), key.product(), key.group());
}
return null;
});
return groups;
}
private static boolean representable(Map<GroupKey, Aggregator> groups) {
Map<String,BigDecimal> sums = new LinkedHashMap<>();
if (groups.values().stream().flatMap(a -> a.rollupStats().stream()).anyMatch(s -> !representable(s.sum()))) return false;
groups.values().forEach(a -> a.rollupStats().forEach(s -> sums.merge(s.kind() + ":" + s.key(), s.sum(), BigDecimal::add)));
return sums.values().stream().allMatch(FineDefectRollups::representable);
}
private static boolean representable(BigDecimal value) {
BigDecimal sum = value.stripTrailingZeros(); return sum.scale() <= 20 && sum.precision() - sum.scale() <= 45;
}
private void insert(String build, Map<GroupKey, Aggregator> groups) {
List<Object[]> batch = new ArrayList<>();
groups.forEach((key, aggregate) -> aggregate.rollupStats().forEach(s -> batch.add(new Object[] {
build,key.stage(),key.month(),key.department(),key.group(),key.project(),key.product(),
s.kind(),s.key(),s.sum(),s.present(),s.missing(),s.invalid(),s.negative(),s.rows()})));
for (int offset = 0; offset < batch.size(); offset += 500) {
var selected = batch.subList(offset, Math.min(batch.size(), offset + 500));
List<Object> values = new ArrayList<>(); selected.forEach(row -> java.util.Collections.addAll(values, row));
jdbc.update("""
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
VALUES
""" + String.join(",", java.util.Collections.nCopies(selected.size(), "(?,?,?,?,?,?,?,'detail',?,?,?,?,?,?,?,?)")), values.toArray());
}
}
}
@@ -79,8 +79,15 @@ public class ApiExceptionHandler {
ProblemDetail unexpected(Exception exception, HttpServletRequest request) {
// Record only classes and frames: exception messages may contain request
// values, database parameters, or upstream credentials.
LOG.error("Unexpected API failure: {} frames={}", exception.getClass().getName(),
LOG.error("Unexpected API failure: {} {} {} frames={}", request.getMethod(), request.getRequestURI(), exception.getClass().getName(),
java.util.Arrays.stream(exception.getStackTrace()).limit(14).toList());
Throwable cause = exception.getCause();
var seen = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<Throwable, Boolean>());
seen.add(exception);
for (int depth = 0; cause != null && depth < 4 && seen.add(cause); depth++, cause = cause.getCause()) {
LOG.error("API failure cause: {} frames={}", cause.getClass().getName(),
java.util.Arrays.stream(cause.getStackTrace()).limit(8).toList());
}
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "服务处理失败");
detail.setProperty("path", request.getRequestURI());
detail.setProperty("timestamp", Instant.now().toString());
@@ -0,0 +1,60 @@
package cn.yunsheng.quality.governance;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.common.ImmutableQueryCache;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
/** Original rows for the clicked business-group inventory key, never a recalculation. */
@Service
public class ClassificationSourceService {
private final GovernanceSupport g;
private final CandidateAcceptanceService access;
private final GovernanceAccessService fields;
private record Key(String release,String stage,List<ClassificationPreviewService.Condition> conditions) {
Key {conditions=List.copyOf(conditions);}
}
private final ImmutableQueryCache<Key,List<String>> indexes=new ImmutableQueryCache<>(32);
public ClassificationSourceService(GovernanceSupport g,CandidateAcceptanceService access,GovernanceAccessService fields) {
this.g=g;this.access=access;this.fields=fields;
}
public record Request(String stage,List<ClassificationPreviewService.Condition> conditions,Integer page,Integer pageSize,String releaseId) {}
public Map<String,Object> list(Request request,AppUserPrincipal actor) {
if(request==null||!Set.of("black","bright").contains(Objects.toString(request.stage(),"")))throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"来源阶段无效");
ClassificationPreviewService.validate(new ClassificationPreviewService.Request(request.stage(),"原始数据",request.conditions()));
int page=request.page()==null?1:request.page(),size=request.pageSize()==null?15:request.pageSize();
if(page<1||size<1||size>100)throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"分页无效");
access.requireSourceDetail(actor);
var current=g.rows("SELECT release_id FROM current_release WHERE dataset_id='quality'");
if(current.isEmpty())return Map.of("rows",List.of(),"total",0,"page",page,"pageSize",size);
String release=request.releaseId()==null?String.valueOf(current.getFirst().get("releaseId")):request.releaseId();
if(g.rows("SELECT id FROM data_release WHERE id=? AND status IN ('PUBLISHED','ACTIVE')",release).isEmpty())throw GovernanceSupport.error(HttpStatus.NOT_FOUND,"正式数据版本不存在");
access.requireRead(release,actor);
Map<String,String> expressions=Map.of("@department","f.department","@product","f.product","PDT代码","JSON_VALUE(f.components,'$.sourcePdtCode')","销售片区","JSON_VALUE(f.components,'$.sourceSalesRegion')");
List<Object> args=new ArrayList<>();args.add(release);
StringBuilder where=new StringBuilder(" WHERE rm.release_id=? AND (f.group_name IS NULL OR TRIM(f.group_name)='')");
for(var condition:request.conditions()) {
String expression=expressions.get(condition.field());
if(condition.operator().equals("IS_BLANK"))where.append(" AND (").append(expression).append(" IS NULL OR REGEXP_LIKE(").append(expression).append(",'^[[:space:]]*$'))");
else {where.append(" AND CAST(").append(expression).append(" AS BINARY)=CAST(? AS BINARY)");args.add(condition.value());}
}
String from=" FROM release_member rm JOIN fact_"+request.stage()+" f ON f.build_id=rm.transform_build_id JOIN source_revision sr ON sr.id=rm.source_revision_id";
var ids=indexes.get(new Key(release,request.stage(),request.conditions()),()->g.jdbc.queryForList("SELECT f.id"+from+where+" ORDER BY f.month_key,sr.source_file,f.sheet_name,f.physical_row_no,f.id",String.class,args.toArray()));
int start=(int)Math.min(ids.size(),(long)(page-1)*size);var selected=ids.subList(start,Math.min(ids.size(),start+size));
Set<String> allowed=fields.allowedFields(actor);
List<Map<String,Object>> rows=new ArrayList<>();
if(!selected.isEmpty()) {
List<Object> rowArgs=new ArrayList<>(args);rowArgs.addAll(selected);
var fetched=g.rows("SELECT f.id,sr.source_file,f.sheet_name AS sheet,f.physical_row_no AS `row_number`,f.month_key AS month,f.department,f.product,f.raw_values"+from+where+" AND f.id IN ("+String.join(",",Collections.nCopies(selected.size(),"?"))+")",rowArgs.toArray());
Map<String,Map<String,Object>> byId=new HashMap<>();
for(var row:fetched) {
Object decoded=g.decode(Objects.toString(row.remove("rawValues"),"{}"));Map<String,Object> visible=new LinkedHashMap<>();
if(decoded instanceof Map<?,?> raw)raw.forEach((key,value)->{if(allowed.contains("*")||allowed.contains(String.valueOf(key)))visible.put(String.valueOf(key),value);});
row.put("raw",visible);row.put("stage",request.stage());byId.put(String.valueOf(row.get("id")),row);
}
for(String id:selected)if(byId.containsKey(id))rows.add(byId.get(id));
}
return Map.of("rows",rows,"total",ids.size(),"page",page,"pageSize",size,"releaseId",release);
}
}
@@ -10,7 +10,9 @@ public class MappingIssueController {
private final MappingIssueService service;
private final UnclassifiedSourceService unclassified;
private final ClassificationPreviewService preview;
public MappingIssueController(MappingIssueService service, UnclassifiedSourceService unclassified,ClassificationPreviewService preview){this.service=service;this.unclassified=unclassified;this.preview=preview;}
private final ClassificationSourceService sources;
public MappingIssueController(MappingIssueService service, UnclassifiedSourceService unclassified,ClassificationPreviewService preview,ClassificationSourceService sources){this.service=service;this.unclassified=unclassified;this.preview=preview;this.sources=sources;}
@PostMapping("/classification-sources") public Map<String,Object> classificationSources(@RequestBody ClassificationSourceService.Request request,@AuthenticationPrincipal AppUserPrincipal actor){return sources.list(request,actor);}
@PostMapping("/classification-preview") public Map<String,Object> classificationPreview(@RequestBody ClassificationPreviewService.Request request,@AuthenticationPrincipal AppUserPrincipal actor){return preview.preview(request,actor);}
@GetMapping("/classification-metadata") public Map<String,Object> classificationMetadata(){return cn.yunsheng.quality.ingestion.ConfigurableMatchRules.metadata();}
@GetMapping("/unclassified") public Map<String,Object> unclassified(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="50") int pageSize,@AuthenticationPrincipal AppUserPrincipal actor){return unclassified.list(page,pageSize,actor);}
@@ -89,20 +89,30 @@ public final class UnclassifiedMappingRebuild {
changes.add(new Change(stage,text(row.get("build_id")),text(row.get("sheet_name")),((Number)row.get("physical_row_no")).intValue(),text(row.get("month_key")),text(row.get("department")),text(row.get("product")),oldGroup,oldProject,group,project,groupChanged,projectChanged,components));
}
}
var changesByBuild=new LinkedHashMap<String,List<Change>>();
for(var change:changes)changesByBuild.computeIfAbsent(change.parent(),ignored->new ArrayList<>()).add(change);
var changedMonths=changes.stream().map(Change::month).distinct().sorted().toList();
String release=UUID.randomUUID().toString(),run=UUID.randomUUID().toString();
int releaseInserted=jdbc.update("INSERT INTO data_release(id,name,status,parent_release_id,expected_current_release_id,rule_version,mapping_version,target_version_id,complete,validation,created_by) SELECT ?,?,'CANDIDATE',id,id,rule_version,?,target_version_id,TRUE,JSON_SET(COALESCE(validation,JSON_OBJECT()),'$.recalculationScope','UNCLASSIFIED','$.scannedRows',?,'$.classifiedRows',?,'$.excelFilesParsed',0),? FROM data_release WHERE id=? AND rule_version=?",release,"未分类归类 "+operation.substring(0,8),mapping,scanned,changes.size(),actor.getId().toString(),parent,rule);
if(releaseInserted!=1)throw new IllegalStateException("当前数据版本已变化,未分类归类未提交");
jdbc.update("INSERT INTO import_run(id,idempotency_key,status,stage,requested_sources,expected_current_release_id,created_by,processed,total,result_release_id,expected_mapping_version,expected_rule_version,workflow_operation_id,validation,started_at,completed_at) VALUES(?,?,'COMPLETED','CLASSIFICATION_READY',JSON_ARRAY(),?,?,?,?,?,?,?,?,JSON_OBJECT('recalculationScope','UNCLASSIFIED','excelFilesParsed',0),CURRENT_TIMESTAMP(6),CURRENT_TIMESTAMP(6))",run,"UNCLASSIFIED:"+operation,parent,actor.getId().toString(),changes.size(),scanned,release,mapping,rule,operation);
var members=jdbc.queryForList("SELECT m.source_document_id,m.source_revision_id,m.transform_build_id FROM release_member m WHERE m.release_id=?",parent);
jdbc.update("UPDATE data_release SET validation=JSON_SET(validation,'$.classificationSnapshotReuse',1,'$.classificationChangedMonths',CAST(? AS JSON),'$.classificationCopiedSources',?,'$.classificationReusedSources',?) WHERE id=?",encode(changedMonths),changesByBuild.size(),members.size()-changesByBuild.size(),release);
var changedOrigins=new ArrayList<cn.yunsheng.quality.governance.MappingIssueService.SourceRow>();
for(var member:members){
String old=text(member.get("transform_build_id")),build=UUID.randomUUID().toString();
String old=text(member.get("transform_build_id"));
var selectedChanges=changesByBuild.get(old);
if(selectedChanges==null){
jdbc.update("INSERT INTO release_member(release_id,source_document_id,source_revision_id,transform_build_id) VALUES(?,?,?,?)",release,member.get("source_document_id"),member.get("source_revision_id"),old);
continue;
}
String build=UUID.randomUUID().toString();
int inserted=jdbc.update("INSERT INTO transform_build(id,source_revision_id,build_key,parser_version,rule_version,mapping_version,status,row_count,rejected_count,component_summary,error_summary,started_at,completed_at,parser_config,parser_config_hash) SELECT ?,source_revision_id,?,parser_version,rule_version,?,'COMPLETED',row_count,rejected_count,JSON_SET(COALESCE(component_summary,JSON_OBJECT()),'$.classificationParentBuildId',id),error_summary,CURRENT_TIMESTAMP(6),CURRENT_TIMESTAMP(6),parser_config,parser_config_hash FROM transform_build WHERE id=? AND status='COMPLETED' AND rule_version=?",build,hash("UNCLASSIFIED\n"+operation+"\n"+old+"\n"+mapping),mapping,old,rule);if(inserted!=1)throw new IllegalStateException("来源快照已变化");
// 只复制已冻结结果,不解析文件、不重算已分类行的数值或维度。
for(String stage:List.of("black","bright"))jdbc.update("INSERT INTO fact_"+stage+"(build_id,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components) SELECT ?,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components FROM fact_"+stage+" WHERE build_id=?",build,old);
jdbc.update("INSERT INTO defect_component(build_id,stage,sheet_name,physical_row_no,defect_name,weight,source_field) SELECT ?,stage,sheet_name,physical_row_no,defect_name,weight,source_field FROM defect_component WHERE build_id=?",build,old);
rollups.copyBuild(old,build);var keys=new LinkedHashSet<MetricsRollupService.GroupKey>();
for(var change:changes)if(change.parent().equals(old)){
for(var change:selectedChanges){
int updated=jdbc.update("UPDATE fact_"+change.stage()+" SET group_name=?,project_group=?,components=CAST(? AS JSON) WHERE build_id=? AND sheet_name=? AND physical_row_no=? AND (group_name<=>?) AND (project_group<=>?) AND "+(change.groupChanged()?"(group_name IS NULL OR TRIM(group_name)='')":"(project_group IS NULL OR TRIM(project_group)='')"),change.group(),change.project(),encode(change.components()),build,change.sheet(),change.row(),change.oldGroup(),change.oldProject());if(updated!=1)throw new IllegalStateException("未分类行已变化,归类未提交");
changedOrigins.add(new cn.yunsheng.quality.governance.MappingIssueService.SourceRow(change.stage(),build,change.sheet(),change.row()));
keys.add(new MetricsRollupService.GroupKey(change.stage(),change.month(),change.department(),change.oldGroup(),change.oldProject(),change.product()));keys.add(new MetricsRollupService.GroupKey(change.stage(),change.month(),change.department(),change.group(),change.project(),change.product()));
@@ -0,0 +1,71 @@
package cn.yunsheng.quality.ingestion;
import java.util.*;
import org.springframework.jdbc.core.JdbcTemplate;
import tools.jackson.databind.json.JsonMapper;
/** Certifies immutable source reuse only for a bound, unclassified-only workflow. */
public final class UnclassifiedSnapshotReuse {
private UnclassifiedSnapshotReuse() {}
public record Proof(Set<String> buildIds,List<String> changedMonths) {}
public static Proof verify(JdbcTemplate jdbc,JsonMapper json,String releaseId) {
var rows=jdbc.queryForList("""
SELECT c.parent_release_id,c.expected_current_release_id,c.rule_version,c.target_version_id,
p.status parent_status,p.rule_version parent_rule,p.target_version_id parent_target,
CAST(c.validation AS CHAR) validation,CAST(old_map.definition AS CHAR) before_definition,
CAST(new_map.definition AS CHAR) after_definition,
(SELECT COUNT(*) FROM import_run i JOIN data_workflow_operation w ON w.id=i.workflow_operation_id
WHERE i.result_release_id=c.id AND i.expected_current_release_id=p.id
AND i.expected_mapping_version=c.mapping_version AND i.expected_rule_version=c.rule_version
AND i.status='COMPLETED' AND i.stage='CLASSIFICATION_READY'
AND i.idempotency_key=CONCAT('UNCLASSIFIED:',w.id)
AND w.operation_type='APPLY_MAPPING' AND w.expected_current_release_id=p.id
AND w.mapping_version_id=c.mapping_version AND w.created_by=c.created_by
AND i.created_by=c.created_by) bound_receipts
FROM data_release c JOIN data_release p ON p.id=c.parent_release_id
JOIN mapping_version old_map ON old_map.id=p.mapping_version
JOIN mapping_version new_map ON new_map.id=c.mapping_version WHERE c.id=?
""",releaseId);
if(rows.size()!=1)return null;
var row=rows.getFirst();var validation=decode(json,row.get("validation"));
if(!Objects.equals(validation.get("classificationSnapshotReuse"),1)
||!"UNCLASSIFIED".equals(validation.get("recalculationScope"))
||!Set.of("PUBLISHED","ACTIVE").contains(row.get("parent_status"))
||!Objects.equals(row.get("parent_release_id"),row.get("expected_current_release_id"))
||!Objects.equals(row.get("rule_version"),row.get("parent_rule"))
||!Objects.equals(row.get("target_version_id"),row.get("parent_target"))
||!(((Number)row.get("bound_receipts")).intValue()==1))return null;
try {
if(UnclassifiedMappingRebuild.changedRules(decode(json,row.get("before_definition")),decode(json,row.get("after_definition")))==null)return null;
}catch(IllegalArgumentException invalidChange){return null;}
if(!(validation.get("classificationChangedMonths") instanceof List<?> rawMonths))return null;
var months=new ArrayList<String>();
for(Object value:rawMonths){if(!(value instanceof String month)||!month.matches("[0-9]{4}-(0[1-9]|1[0-2])")||months.contains(month))return null;months.add(month);}
String parent=Objects.toString(row.get("parent_release_id"));
// No source can be added, removed, swapped, or rebound to another revision.
Integer invalid=jdbc.queryForObject("""
SELECT
(SELECT COUNT(*) FROM release_member n LEFT JOIN release_member p
ON p.release_id=? AND p.source_document_id=n.source_document_id
LEFT JOIN transform_build b ON b.id=n.transform_build_id
WHERE n.release_id=? AND (p.source_document_id IS NULL OR n.source_revision_id<>p.source_revision_id
OR (n.transform_build_id<>p.transform_build_id AND
COALESCE(JSON_VALUE(b.component_summary,'$.classificationParentBuildId'),'')<>p.transform_build_id)))
+(SELECT COUNT(*) FROM release_member p WHERE p.release_id=? AND NOT EXISTS(
SELECT 1 FROM release_member n WHERE n.release_id=? AND n.source_document_id=p.source_document_id))
""",Integer.class,parent,releaseId,parent,releaseId);
if(invalid==null||invalid!=0)return null;
var reused=new LinkedHashSet<String>(jdbc.query("""
SELECT n.transform_build_id FROM release_member n JOIN release_member p
ON p.release_id=? AND p.source_document_id=n.source_document_id
AND p.source_revision_id=n.source_revision_id AND p.transform_build_id=n.transform_build_id
WHERE n.release_id=?
""",(rs,index)->rs.getString(1),parent,releaseId));
Integer copied=jdbc.queryForObject("SELECT COUNT(*) FROM release_member n JOIN release_member p ON p.release_id=? AND p.source_document_id=n.source_document_id WHERE n.release_id=? AND n.transform_build_id<>p.transform_build_id",Integer.class,parent,releaseId);
if(copied==null||copied==0&&!months.isEmpty()||copied>0&&months.isEmpty())return null;
return new Proof(Set.copyOf(reused),List.copyOf(months));
}
@SuppressWarnings("unchecked")
private static Map<String,Object> decode(JsonMapper json,Object value){return value==null?Map.of():json.readValue(String.valueOf(value),Map.class);}
}
@@ -21,8 +21,12 @@ public class MetricsRollupService {
"'bright_diffusion_scrap'," + sumComponent("bright_diffusion_scrap") + ")";
private final JdbcTemplate jdbc;
private final cn.yunsheng.quality.analysis.FineDefectRollups fineDefects;
public MetricsRollupService(JdbcTemplate jdbc) { this.jdbc = jdbc; }
public MetricsRollupService(JdbcTemplate jdbc) {
this.jdbc = jdbc;
this.fineDefects = new cn.yunsheng.quality.analysis.FineDefectRollups(jdbc, tools.jackson.databind.json.JsonMapper.builder().build());
}
public record GroupKey(String stage,String month,String department,String group,String project,String product) {}
@@ -30,6 +34,7 @@ public class MetricsRollupService {
public void copyBuild(String parent,String child) {
jdbc.update("INSERT INTO metric_rollup(build_id,stage,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,components,source_row_count,excluded_blank_rows,unmapped_rows,project_unconfigured_rows) SELECT ?,stage,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,components,source_row_count,excluded_blank_rows,unmapped_rows,project_unconfigured_rows FROM metric_rollup WHERE build_id=?",child,parent);
if(jdbc.update("INSERT INTO metric_rollup_build(build_id,source_row_count) SELECT ?,source_row_count FROM metric_rollup_build WHERE build_id=?",child,parent)!=1) throw new IllegalStateException("旧版本缺少已冻结的指标汇总");
fineDefects.copyBuild(parent, child);
}
public void refreshGroups(String build,java.util.Set<GroupKey> keys) {
@@ -43,6 +48,7 @@ public class MetricsRollupService {
var insertArgs=new java.util.ArrayList<Object>(List.of(stage,build));insertArgs.addAll(values);
jdbc.update(rollupInsertSql("fact_"+stage).replace("WHERE f.build_id=?","WHERE f.build_id=? AND ("+inserted+")"),insertArgs.toArray());
}
fineDefects.refreshGroups(build, keys);
}
/** Idempotently refreshes a completed build in the caller's transaction. */
@@ -63,6 +69,7 @@ public class MetricsRollupService {
(SELECT COUNT(*) FROM fact_black WHERE build_id=?) +
(SELECT COUNT(*) FROM fact_bright WHERE build_id=?), CURRENT_TIMESTAMP(6)
""", buildId, buildId, buildId);
fineDefects.refreshBuild(buildId);
}
static String rollupInsertSql(String table) {
@@ -182,7 +182,15 @@ public class ReleaseService {
if(coverage.overlapCount(releaseId)>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"来源快照或声明范围相互覆盖,不能重复计数发布");
Integer invalidDefinitions=jdbc.queryForObject("SELECT COUNT(*) FROM data_release r LEFT JOIN rule_version rules ON rules.id=r.rule_version LEFT JOIN mapping_version maps ON maps.id=r.mapping_version LEFT JOIN target_version targets ON targets.id=r.target_version_id WHERE r.id=? AND (rules.id IS NULL OR maps.id IS NULL OR(r.target_version_id IS NOT NULL AND(targets.id IS NULL OR targets.status NOT IN('ACTIVE','SUPERSEDED','VALIDATED'))))",Integer.class,releaseId);
if(invalidDefinitions!=null&&invalidDefinitions>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"规则、映射或目标冻结依赖不可执行");
Integer invalid=jdbc.queryForObject("SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id JOIN data_release r ON r.id=rm.release_id WHERE rm.release_id=? AND (b.status<>'COMPLETED' OR b.rejected_count>0 OR b.row_count=0 OR b.rule_version<>r.rule_version OR b.mapping_version<>r.mapping_version OR b.source_revision_id<>rm.source_revision_id)",Integer.class,releaseId);
var reuse=cn.yunsheng.quality.ingestion.UnclassifiedSnapshotReuse.verify(jdbc,mapper,releaseId);
String buildCheck="SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id JOIN data_release r ON r.id=rm.release_id WHERE rm.release_id=? AND (b.status<>'COMPLETED' OR b.rejected_count>0 OR b.row_count=0 OR b.rule_version<>r.rule_version OR b.mapping_version<>r.mapping_version OR b.source_revision_id<>rm.source_revision_id)";
var buildArgs=new ArrayList<Object>(List.of(releaseId));
if(reuse!=null&&!reuse.buildIds().isEmpty()){
var reused=reuse.buildIds().stream().sorted().toList();
buildCheck=buildCheck.replace("b.mapping_version<>r.mapping_version","(b.mapping_version<>r.mapping_version AND b.id NOT IN ("+String.join(",",java.util.Collections.nCopies(reused.size(),"?"))+"))");
buildArgs.addAll(reused);
}
Integer invalid=jdbc.queryForObject(buildCheck,Integer.class,buildArgs.toArray());
if(invalid!=null&&invalid>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"版本转换产物或冻结依赖不完整");
Integer missing=jdbc.queryForObject("SELECT COUNT(*) FROM release_protocol_dependency dp JOIN source_protocol p ON p.id=dp.protocol_id WHERE dp.release_id=? AND p.applicable=TRUE AND p.expected=TRUE AND NOT EXISTS(SELECT 1 FROM release_member rm JOIN source_document d ON d.id=rm.source_document_id WHERE rm.release_id=? AND "+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("d.source_slot")+"="+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("p.source_slot")+") AND NOT EXISTS(SELECT 1 FROM release_source_exclusion e JOIN source_document d ON d.id=e.source_document_id WHERE e.release_id=? AND "+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("d.source_slot")+"="+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("p.source_slot")+")",Integer.class,releaseId,releaseId,releaseId);
if(missing!=null&&missing>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"协议要求的来源分片尚未到齐");
@@ -551,14 +551,22 @@ public class DataWorkflowService {
/** Execute the candidate's own formulas before changing any active definition or pointer. */
private void validateCandidateMetrics(String releaseId,AppUserPrincipal actor) {
var reuse=cn.yunsheng.quality.ingestion.UnclassifiedSnapshotReuse.verify(jdbc,json,releaseId);
// A zero-hit classification changes no fact or formula. Other classification
// updates need only the affected months and their enclosing quarter/year.
if(reuse!=null&&reuse.changedMonths().isEmpty())return;
Set<CandidatePeriod> periods=new LinkedHashSet<>();
for(Map<String,Object> row:jdbc.queryForList("SELECT DISTINCT d.source_year,d.source_month FROM release_member m JOIN source_document d ON d.id=m.source_document_id WHERE m.release_id=? ORDER BY d.source_year,d.source_month",releaseId)) {
int year=((Number)row.get("source_year")).intValue(),month=((Number)row.get("source_month")).intValue();
if(reuse!=null&&!reuse.changedMonths().contains(String.format(java.util.Locale.ROOT,"%04d-%02d",year,month)))continue;
periods.add(new CandidatePeriod(year,month,null));
periods.add(new CandidatePeriod(year,null,(month-1)/3+1));
periods.add(new CandidatePeriod(year,null,null));
}
if(periods.isEmpty())periods.add(new CandidatePeriod(java.time.LocalDate.now(java.time.ZoneId.of("Asia/Shanghai")).getYear(),null,null));
if(periods.isEmpty()){
if(reuse!=null)throw conflict("未分类更新期间与来源不一致");
periods.add(new CandidatePeriod(java.time.LocalDate.now(java.time.ZoneId.of("Asia/Shanghai")).getYear(),null,null));
}
try {
for(CandidatePeriod period:periods)for(String dimension:List.of("overall","department","group","product","projectGroup"))
metrics.calculateCandidateForGovernance(new MetricsService.MetricsQuery(period.year(),period.month(),period.quarter(),
@@ -0,0 +1,29 @@
CREATE TABLE fine_defect_rollup_build (
build_id CHAR(36) COLLATE utf8mb4_0900_ai_ci NOT NULL PRIMARY KEY,
source_row_count BIGINT NOT NULL,
refreshed_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
CONSTRAINT fk_fine_rollup_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
CREATE TABLE fine_defect_rollup (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
build_id CHAR(36) COLLATE utf8mb4_0900_ai_ci NOT NULL,
stage VARCHAR(16) NOT NULL,
month_key CHAR(7) NOT NULL,
department VARCHAR(190) NULL,
group_name VARCHAR(190) NULL,
project_group VARCHAR(190) NULL,
product VARCHAR(190) NULL,
grain VARCHAR(16) NOT NULL,
stat_kind VARCHAR(16) NOT NULL,
field_key VARCHAR(100) NOT NULL,
weight_sum DECIMAL(65,20) NOT NULL,
present_rows BIGINT NOT NULL,
missing_rows BIGINT NOT NULL,
invalid_rows BIGINT NOT NULL,
negative_rows BIGINT NOT NULL,
source_row_count BIGINT NOT NULL,
KEY ix_fine_rollup_build_month (build_id, stage, grain, month_key),
KEY ix_fine_rollup_dimensions (build_id, stage, department(100), group_name(100), product(100), project_group(100)),
CONSTRAINT fk_fine_rollup_fact_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
@@ -0,0 +1,53 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.nio.file.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import tools.jackson.databind.json.JsonMapper;
/** Local, read-only acceptance against private captures; no database or model calls. */
@EnabledIfEnvironmentVariable(named="QUALITY_REPLAY_FIXTURES_ROOT",matches=".+")
class AnalysisAnswerChainReplayTest {
@Test void capturedAnswersPassTheActualPlanDataRankingSnapshotAndCompletionGates() throws Exception {
var json=JsonMapper.builder().build();
var root=Path.of(System.getenv("QUALITY_REPLAY_FIXTURES_ROOT"));
var compiler=new AnalysisProtocolQueryPlan(json);
var service=mock(AnalysisProtocolService.class,CALLS_REAL_METHODS);
var gate=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class);
gate.setAccessible(true);
for(int index=1;index<=3;index++) {
var folder=root.resolve("replay-"+index);
var answer=json.readValue(Files.readString(folder.resolve("result.json")),Map.class);
var response=json.readValue(Files.readString(folder.resolve("formal-response.json")),Map.class);
var task=new LinkedHashMap<String,Object>(AnalysisProtocolService.map(answer.get("task")));
task.put("queryPlan",compiler.normalize(task,Map.of(),List.of()));
var scopes=compiler.resolve(task,"capture-execution",answer.get("releaseId").toString(),Map.of(),List.of(),List.of()).scopes();
assertEquals(1,scopes.size());
assertEquals("COMPLETED",answer.get("status"),"Narrative for question "+index);
var child=AnalysisProtocolService.map(scopes.getFirst().get("task"));
assertTrue(AnalysisProtocolRankingCoverage.complete(response,child,"product"));
assertTrue(Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(response,child,
AnalysisProtocolService.map(task.get("period"))).get("metricsComplete")));
var artifact=AnalysisProtocolRankArtifact.build(response,child,"product",answer.get("releaseId").toString());
var ranking=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
var expected=AnalysisProtocolService.list(ranking.get("rows"));
var rawCharts=AnalysisProtocolService.list(answer.get("visuals"));
assertEquals(1,rawCharts.size());
var chart=AnalysisProtocolService.map(rawCharts.getFirst());
var actual=AnalysisProtocolService.list(chart.get("categories"));
assertEquals(index==3?3:5,actual.size());
for(int row=0;row<actual.size();row++)assertEquals(
AnalysisProtocolService.map(expected.get(row)).get("key"),
AnalysisProtocolService.map(actual.get(row)).get("key"));
var context=new LinkedHashMap<String,Object>(AnalysisProtocolService.map(task.get("period")));
context.putAll(AnalysisProtocolService.map(task.get("filters")));context.put("releaseId",answer.get("releaseId"));
var snapshot=AnalysisVisualSnapshotAccess.normalize(1,"ready",rawCharts,Set.of("*"),Set.of(),answer.get("id").toString(),context);
assertEquals("ready",snapshot.state(),"Snapshot for question "+index);
var checked=new LinkedHashMap<String,Object>(answer);checked.put("visuals",snapshot.visuals());
assertEquals(true,gate.invoke(service,checked,scopes.getFirst(),answer.get("releaseId").toString()),"Completion for question "+index);
}
}
}
@@ -188,6 +188,28 @@ class AnalysisChangesServiceTest {
assertNull(((Map<?, ?>) current.get("bright")).get("rate"));
}
@Test
void keepsFormalCompositeRateAboveOneAndLeavesUnexplainableStageBreakdownUnavailable() {
authorize(Set.of("*"));
when(responseAccess.visible(any(MetricSnapshot.class), eq(actor))).thenAnswer(call -> call.getArgument(0));
when(metrics.calculateComparisonBatch(any(MetricsQuery.class), any(MetricsQuery.class), anyMap(), eq(actor)))
.thenReturn(new MetricsService.ComparisonSnapshots(
snapshot("1.2", "1.4", "0.8", List.of(row("P", "1.2", "1.4", "0.8")), coverage("2026-09", 4)),
snapshot("1.3", "1.5", "0.9", List.of(row("P", "1.3", "1.5", "0.9")), coverage("2026-08", 4))));
Map<String,Object> result=service.query(request("product","rate_drop",1),actor);
assertEquals(Boolean.TRUE,result.get("completeRanking"));
assertEquals(Boolean.TRUE,((Map<?,?>)result.get("coverage")).get("rateComparable"));
assertEquals(new BigDecimal("1.2"),((Map<?,?>)result.get("current")).get("compositeRate"));
assertEquals(new BigDecimal("1.3"),((Map<?,?>)result.get("comparison")).get("compositeRate"));
Map<?,?> ranked=(Map<?,?>)((List<?>)result.get("rows")).getFirst();
assertEquals(new BigDecimal("1.2"),((Map<?,?>)ranked.get("current")).get("rate"));
assertEquals(new BigDecimal("1.3"),((Map<?,?>)ranked.get("comparison")).get("rate"));
assertEquals(new BigDecimal("0.1"),ranked.get("rateDrop"));
assertEquals("unavailable",((Map<?,?>)result.get("stageDecomposition")).get("status"),
"a mismatch in the optional stage explanation must not invalidate the official composite-rate comparison");
}
@Test
void leavesCompositeRateNullWhenNeitherStageHasMeasuredData() {
authorize(Set.of("*"));
@@ -20,6 +20,8 @@ import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.release.ReleaseService;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -27,9 +29,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.server.ResponseStatusException;
@@ -117,6 +122,23 @@ class AnalysisFineDefectsScopeTest {
}
return null;
}).when(fixture.jdbc).query(anyString(), any(RowCallbackHandler.class), any(Object[].class));
Connection streamingConnection=mock(Connection.class);PreparedStatement streamingStatement=mock(PreparedStatement.class);
when(streamingConnection.prepareStatement(anyString(),eq(ResultSet.TYPE_FORWARD_ONLY),eq(ResultSet.CONCUR_READ_ONLY)))
.thenAnswer(invocation->{aggregateSql.set(invocation.getArgument(0));return streamingStatement;});
doAnswer(invocation->{int parameter=invocation.getArgument(0);String value=invocation.getArgument(1);
List<Object> bound=fixture.queryArgs.get();while(bound.size()<parameter)bound.add(null);bound.set(parameter-1,value);return null;
}).when(streamingStatement).setString(any(Integer.class),anyString());
doAnswer(invocation->{
PreparedStatementCreator creator=invocation.getArgument(0);creator.createPreparedStatement(streamingConnection);
ResultSetExtractor<?> extractor=invocation.getArgument(1);
List<Map<String,String>> rows=List.of(
Map.of("raw_fields","{\"\\u8d23\\u5e9f\\u5c3a\\u5bf8\\u91cd\\u91cf\":1.25}","formal_components","{\"black_responsibility_scrap\":1.25}","business_group","A","product_key","P-1"),
Map.of("raw_fields","{\"\\u8d23\\u5e9f\\u5c3a\\u5bf8\\u91cd\\u91cf\":2.25}","formal_components","{\"black_responsibility_scrap\":2.25}","business_group","A","product_key","P-1"));
AtomicInteger rowIndex=new AtomicInteger(-1);ResultSet row=mock(ResultSet.class);
when(row.next()).thenAnswer(next->rowIndex.incrementAndGet()<rows.size());
when(row.getString(anyString())).thenAnswer(read->rows.get(rowIndex.get()).get(read.getArgument(0)));
extractor.extractData(row);return null;
}).when(fixture.jdbc).query(any(PreparedStatementCreator.class),any(ResultSetExtractor.class));
Map<String, Object> response = fixture.service.query(request, ACTOR);
@@ -149,6 +171,39 @@ class AnalysisFineDefectsScopeTest {
assertFalse((Boolean) responsibility.get("isReconciled"), "部分缺陷字段仍以完整类别字段数作为回加分母");
}
@Test
void topStreamsRowsWithForwardOnlyReadOnlyStatementAndKeepsBoundParameters() throws Exception {
JdbcTemplate jdbc=mock(JdbcTemplate.class);ReleaseService releases=mock(ReleaseService.class);
GovernanceAccessService governance=mock(GovernanceAccessService.class);
when(governance.allowedFields(ACTOR)).thenReturn(Set.of("*"));
when(releases.resolvePublishedRelease("release-1")).thenReturn("release-1");
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenReturn(List.of(Map.of(
"id","release-1","name","Published","status","PUBLISHED","ruleVersion","r1","mappingVersion","m1")));
Connection connection=mock(Connection.class);PreparedStatement statement=mock(PreparedStatement.class);
AtomicReference<String> preparedSql=new AtomicReference<>();
AtomicReference<PreparedStatement> created=new AtomicReference<>();
doAnswer(invocation->{preparedSql.set(invocation.getArgument(0));return statement;})
.when(connection).prepareStatement(anyString(),eq(ResultSet.TYPE_FORWARD_ONLY),eq(ResultSet.CONCUR_READ_ONLY));
doAnswer(invocation->{PreparedStatementCreator creator=invocation.getArgument(0);
created.set(creator.createPreparedStatement(connection));return null;
}).when(jdbc).query(any(PreparedStatementCreator.class),any(ResultSetExtractor.class));
var service=new AnalysisFineDefectsService(jdbc,releases,governance,JsonMapper.builder().build());
var request=new LinkedHashMap<String,Object>();request.put("releaseId","release-1");request.put("year",2026);
request.put("month",9);request.put("stage","black");request.put("productSearch","P-1");
service.query(request,ACTOR);
assertTrue(preparedSql.get().contains("rm.release_id = ?"));
assertTrue(preparedSql.get().contains("f.month_key = ?"));
assertTrue(preparedSql.get().contains("LOCATE(LOWER(?), LOWER(COALESCE(f.product,''))) > 0"));
assertEquals(statement,created.get());
verify(connection).prepareStatement(preparedSql.get(),ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
verify(statement).setFetchSize(Integer.MIN_VALUE);
verify(statement).setString(1,"release-1");
verify(statement).setString(2,"2026-09");
verify(statement).setString(3,"P-1");
}
@Test
void catalogEchoesScopeWithoutPretendingToCalculateFilteredStatistics() {
Fixture fixture = new Fixture();
@@ -10,6 +10,20 @@ class AnalysisProtocolChangesTest {
"stages",List.of(),"fields",List.of("rate"),"filters",Map.of("department","汽车"),"changeQuery",Map.of("dimension","group","sort","rate_drop","limit",2)));}
private Map<String,Object> response(){var r=new LinkedHashMap<String,Object>();r.put("kind","quality_changes");r.put("releaseId","r");r.put("dimension","group");r.put("stage",null);r.put("sort","rate_drop");r.put("currentPeriod",Map.of("year",2026,"month",9));r.put("comparisonPeriod",Map.of("year",2026,"month",8));r.put("filters",Map.of("department","汽车"));r.put("totalRows",1);r.put("completeRanking",true);r.put("coverage",Map.of("current",Map.of("sourceMonths",List.of()),"comparison",Map.of("sourceMonths",List.of()),"rateComparable",true));r.put("current",Map.of("compositeRate",new BigDecimal(".8")));r.put("comparison",Map.of("compositeRate",new BigDecimal(".9")));r.put("rows",List.of(Map.of("rank",1,"key","国内","label","国内","filters",Map.of("department","汽车","group","国内"),"current",Map.of("rate",new BigDecimal(".8")),"comparison",Map.of("rate",new BigDecimal(".9")),"rateDrop",new BigDecimal(".1"),"scrapDelta",10)));return r;}
@Test void exactBodyAndCompleteTopProofBindEveryFilterAndPeriod(){var expected=AnalysisProtocolChanges.request("r",task());assertNull(expected.get("stage"));assertTrue(AnalysisProtocolChanges.acceptParameters(expected,new LinkedHashMap<>(expected)));var changed=new LinkedHashMap<>(expected);changed.put("period",Map.of("year",2026,"month",7));assertFalse(AnalysisProtocolChanges.acceptParameters(expected,changed));assertTrue((Boolean)AnalysisProtocolChanges.inspect(response(),expected).get("changesComplete"));var artifact=AnalysisProtocolChanges.artifact(response(),expected);assertEquals("quality_changes_artifact",artifact.get("kind"));assertEquals(expected.get("period"),artifact.get("period"));assertEquals(response().get("rows"),artifact.get("rows"));}
@Test void changeEvidencePreservesFormalRatesAboveOneWhenInputsRemainComparable() {
var expected=AnalysisProtocolChanges.request("r",task());var response=response();
response.put("current",Map.of("compositeRate",new BigDecimal("1.2")));
response.put("comparison",Map.of("compositeRate",new BigDecimal("1.3")));
var row=new LinkedHashMap<>(AnalysisProtocolService.map(((List<?>)response.get("rows")).getFirst()));
row.put("current",Map.of("rate",new BigDecimal("1.2")));row.put("comparison",Map.of("rate",new BigDecimal("1.3")));
row.put("rateDrop",new BigDecimal("0.1"));response.put("rows",List.of(row));
assertTrue((Boolean)AnalysisProtocolChanges.inspect(response,expected).get("changesComplete"));
var artifact=AnalysisProtocolChanges.artifact(response,expected);
assertEquals(new BigDecimal("1.2"),AnalysisProtocolService.map(artifact.get("current")).get("compositeRate"));
assertEquals(new BigDecimal("1.3"),AnalysisProtocolService.map(artifact.get("comparison")).get("compositeRate"));
assertEquals(new BigDecimal("1.2"),AnalysisProtocolService.map(AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rows")).getFirst()).get("current")).get("rate"));
}
@Test void incompleteRowsDuplicateKeysChangedParentAndWrongReleaseFailClosed(){var expected=AnalysisProtocolChanges.request("r",task());for(String mutation:List.of("release","rows","parent","complete","coverage")){var r=response();if(mutation.equals("release"))r.put("releaseId","other");if(mutation.equals("rows"))r.put("totalRows",2);if(mutation.equals("complete"))r.put("completeRanking",false);if(mutation.equals("coverage"))r.put("coverage",Map.of("rateComparable",true));if(mutation.equals("parent")){var row=new LinkedHashMap<>(AnalysisProtocolService.map(((List<?>)r.get("rows")).getFirst()));row.put("filters",Map.of("department","其他","group","国内"));r.put("rows",List.of(row));}assertFalse((Boolean)AnalysisProtocolChanges.inspect(r,expected).get("changesComplete"),mutation);assertTrue(AnalysisProtocolChanges.artifact(r,expected).isEmpty(),mutation);}}
@Test void dominantOnlyUsesReliableAdverseFormalContribution(){var artifact=new LinkedHashMap<String,Object>();artifact.put("stageDecomposition",Map.of("status","available","blackContributionToCompositePP",-1,"brightContributionToCompositePP",-3));assertEquals(List.of("bright"),AnalysisProtocolChanges.selectedStages(artifact,"dominant",List.of("black","bright")));artifact.put("stageDecomposition",Map.of("status","unavailable","blackPP",-9,"brightPP",0));assertEquals(List.of("black","bright"),AnalysisProtocolChanges.selectedStages(artifact,"dominant",List.of("black","bright")));artifact.put("stageDecomposition",Map.of("status","available","blackPP",0,"brightPP",1));assertTrue(AnalysisProtocolChanges.noAdverseStage(artifact));}
private String selections(String... values){return cn.yunsheng.quality.common.SelectionValues.PREFIX+Base64.getUrlEncoder().withoutPadding().encodeToString(tools.jackson.databind.json.JsonMapper.builder().build().writeValueAsString(List.of(values)).getBytes(java.nio.charset.StandardCharsets.UTF_8));}
@@ -39,4 +53,27 @@ class AnalysisProtocolChangesTest {
parent.put("fields",List.of("rate","scrapRate"));assertThrows(IllegalArgumentException.class,()->AnalysisProtocolChanges.request("r",parent));
}
@Test void validCompositeReferenceDoesNotRequireWeightsFromAnUnmeasuredStage(){
var parent=task();parent.put("fields",List.of("rate","totalWeight","scrapWeight"));
var expected=AnalysisProtocolChanges.request("r",parent);var r=response();
var row=new LinkedHashMap<>(AnalysisProtocolService.map(((List<?>)r.get("rows")).getFirst()));
row.put("present",Map.of("current",true,"comparison",true));
row.put("current",Map.of("rate",new BigDecimal("0"),"black",Map.of(),
"bright",Map.of("totalWeight",new BigDecimal("0.31"),"scrapWeight",new BigDecimal("0.31")),
"rateSupport",Map.of("blackMeasured",false,"brightMeasured",true)));
row.put("comparison",Map.of("rate",new BigDecimal("1.4177269517146895"),
"black",Map.of("totalWeight",new BigDecimal("-2.67"),"scrapWeight",new BigDecimal("1.23")),
"bright",Map.of("totalWeight",new BigDecimal("317.56"),"scrapWeight",new BigDecimal("9.337")),
"rateSupport",Map.of("blackMeasured",false,"brightMeasured",true)));
row.put("rateDrop",new BigDecimal("1.4177269517146895"));r.put("rows",List.of(row));
var inspection=AnalysisProtocolChanges.inspect(r,expected);
assertTrue((Boolean)inspection.get("changesComplete"),inspection.get("missingEvidence").toString());
assertFalse(AnalysisProtocolChanges.artifact(r,expected).isEmpty());
assertEquals(new BigDecimal("-2.67"),AnalysisProtocolService.map(AnalysisProtocolService.map(
AnalysisProtocolService.map(AnalysisProtocolService.list(AnalysisProtocolChanges.artifact(r,expected).get("rows")).getFirst())
.get("comparison")).get("black")).get("totalWeight"));
}
}
@@ -10,6 +10,19 @@ class AnalysisProtocolChangesVisualContractTest {
private Map<String,Object> child(String sort,String stage){var c=new LinkedHashMap<String,Object>();c.put("changeQuery",Map.of("dimension","group","sort",sort,"limit",3));c.put("fields",List.of("rate","totalWeight","scrapWeight"));c.put("stages",stage==null?List.of():List.of(stage));c.put("period",Map.of("year",2026,"month",9));c.put("comparisonPeriod",Map.of("year",2026,"month",8));c.put("comparisonRequired",true);c.put("filters",Map.of("department","汽车"));c.put("visualPreference","auto");return c;}
private Map<String,Object> chart(Map<String,Object> child,String metric,String stage){var c=new LinkedHashMap<String,Object>();c.put("metric",metric);c.put("type","bar");c.put("dimension","group");c.put("period",child.get("period"));c.put("comparisonPeriod",child.get("comparisonPeriod"));c.put("scope",Map.of("releaseId","r","department","汽车"));if(stage!=null)c.put("stage",stage);return c;}
private boolean accepts(Map<String,Object> child,List<Map<String,Object>> charts)throws Exception{var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class);method.setAccessible(true);return (boolean)method.invoke(service,Map.of("visualState","ready","visuals",charts),Map.of("purpose","changes","task",child),"r");}
private boolean acceptsScope(String state,String purpose,Map<String,Object> child)throws Exception{
return acceptsScope(state,purpose,child,List.of());
}
private boolean acceptsScope(String state,String purpose,Map<String,Object> child,List<Map<String,Object>> charts)throws Exception{
var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class);
method.setAccessible(true);
return (boolean)method.invoke(service,Map.of("visualState",state,"visuals",charts),Map.of("purpose",purpose,"task",child),"r");
}
private boolean acceptsRankingScope(String state,Map<String,Object> child,int limit)throws Exception{
var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class);
method.setAccessible(true);
return (boolean)method.invoke(service,Map.of("visualState",state,"visuals",List.of()),Map.of("purpose","ranking","limit",limit,"task",child),"r");
}
@Test void actualChangeMetricsPassWithoutRequiringOneChartPerRequestedField()throws Exception{
for(String stage:Arrays.asList(null,"black","bright")){var rate=child("current_rate",stage);assertTrue(accepts(rate,List.of(chart(rate,stage==null?"composite_rate":stage+"_rate",stage))));var drop=child("rate_drop",stage);assertTrue(accepts(drop,List.of(chart(drop,"rate_drop_pp",stage))));var scrap=child("scrap_delta",stage);assertTrue(accepts(scrap,List.of(chart(scrap,"scrap_delta",stage))));}
var current=child("current_scrap","black");assertTrue(accepts(current,List.of(chart(current,"current_scrap_weight","black"))));
@@ -26,4 +39,146 @@ class AnalysisProtocolChangesVisualContractTest {
c.put("visualPreference","required");
assertFalse((boolean)method.invoke(service,Map.of("visualState","unavailable","visuals",List.of()),Map.of("purpose","changes","task",c),"r"));
}
@Test void automaticScalarMetricStyleHintDoesNotRequireAChartButExplicitAndComparisonDo()throws Exception{
var scalar=new LinkedHashMap<String,Object>();scalar.put("kind","metrics");scalar.put("fields",List.of("rate"));
scalar.put("stages",List.of("black"));scalar.put("period",Map.of("year",2026,"month",9));
scalar.put("filters",Map.of());scalar.put("visualPreference","auto");scalar.put("visualType","bar");
scalar.put("comparisonRequired",false);scalar.put("trendRequired",false);
assertTrue(acceptsScope("not_requested","metrics",scalar),
"an auto single-value support metric can be shown in the table without poisoning the report");
scalar.put("visualPreference","required");
assertFalse(acceptsScope("unavailable","metrics",scalar),"an explicitly required chart remains blocking");
var explicitChart=Map.<String,Object>of("metric","black_rate","stage","black","type","bar",
"period",scalar.get("period"),"scope",Map.of("releaseId","r"));
assertTrue(acceptsScope("ready","metrics",scalar,List.of(explicitChart)),"an exact explicit chart satisfies the requirement");
scalar.put("visualPreference","auto");scalar.put("comparisonRequired",true);
scalar.put("comparisonPeriod",Map.of("year",2026,"month",8));
assertFalse(acceptsScope("unavailable","metrics",scalar),"a comparison visualization remains blocking");
var comparisonChart=new LinkedHashMap<>(explicitChart);comparisonChart.put("comparisonPeriod",scalar.get("comparisonPeriod"));
assertTrue(acceptsScope("ready","metrics",scalar,List.of(comparisonChart)),"an exact comparison chart satisfies the requirement");
}
@Test void rankingAndTrendScopesStillRequireTheirCharts()throws Exception{
var ranking=new LinkedHashMap<String,Object>();ranking.put("kind","analysis");ranking.put("fields",List.of("scrapWeight"));
ranking.put("stages",List.of("black"));ranking.put("dimensions",List.of("product"));
ranking.put("period",Map.of("year",2026,"month",9));ranking.put("filters",Map.of());
ranking.put("visualPreference","auto");ranking.put("visualType","bar");
assertFalse(acceptsScope("ready","ranking",ranking),"a chartable ranking without its chart remains blocking");
assertFalse(acceptsRankingScope("ready",ranking,1),"an actual ranking remains chart-gated even at limit 1");
var trend=new LinkedHashMap<String,Object>();trend.put("kind","metrics");trend.put("fields",List.of("rate"));
trend.put("stages",List.of("black"));trend.put("period",Map.of("year",2026,"month",9));
trend.put("filters",Map.of());trend.put("visualPreference","auto");trend.put("visualType","bar");
trend.put("trendRequired",true);
assertFalse(acceptsScope("ready","trend",trend),"a required trend without its chart remains blocking");
var multiMetric=new LinkedHashMap<>(trend);multiMetric.put("trendRequired",false);
multiMetric.put("fields",List.of("rate","scrapWeight"));multiMetric.put("comparisonRequired",false);
assertFalse(acceptsScope("unavailable","metrics",multiMetric),
"the scalar exception does not suppress automatic multi-metric chart requirements");
}
private Map<String,Object> scalarPair(String dimension,Object selected) {
var scalar=new LinkedHashMap<String,Object>();scalar.put("kind","metrics");scalar.put("fields",List.of("rate","scrapWeight"));
scalar.put("stages",List.of());scalar.put("dimensions",List.of(dimension));scalar.put("dimensionStages",Map.of(dimension,List.of()));
scalar.put("period",Map.of("year",2026,"month",9));scalar.put("filters",Map.of(dimension,selected));
scalar.put("visualPreference","auto");scalar.put("visualType","bar");scalar.put("comparisonRequired",false);scalar.put("trendRequired",false);
return scalar;
}
@Test void onlyConfirmedSingleDimensionCombinedMetricRankingMaySkipAutomaticChart()throws Exception{
for(var selected:Map.of("product","A","department","汽车","group","国内").entrySet()) {
assertTrue(acceptsRankingScope("unavailable",scalarPair(selected.getKey(),selected.getValue()),1),
"the exact one-"+selected.getKey()+" combined scalar pair may finish without a ranking chart under auto");
}
assertTrue(acceptsRankingScope("unavailable",scalarPair("product","~qv1~WyJBIl0"),1),
"a valid one-value selection wire is still one exact product");
var scalar=scalarPair("product","A");
scalar.put("visualPreference","required");
assertFalse(acceptsRankingScope("unavailable",scalar,1),"an explicitly required ranking chart remains blocking");
scalar.put("visualPreference","auto");
assertFalse(acceptsRankingScope("unavailable",scalar,2),"a broader Top-N ranking is not a scalar pair");
scalar.put("filters",Map.of("product",List.of("A","B")));
assertFalse(acceptsRankingScope("unavailable",scalar,1),"multiple products cannot use the exception");
scalar.put("filters",Map.of("product","A"));scalar.put("comparisonRequired",true);
assertFalse(acceptsRankingScope("unavailable",scalar,1),"a comparison still requires a chart");
scalar.put("comparisonRequired",false);scalar.put("trendRequired",true);
assertFalse(acceptsRankingScope("unavailable",scalar,1),"a trend still requires its chart");
scalar.put("trendRequired",false);scalar.put("stages",List.of("black"));
assertFalse(acceptsRankingScope("unavailable",scalar,1),"a stage-specific ranking is not the combined scalar pair");
scalar.put("stages",List.of());scalar.put("kind","analysis");
assertFalse(acceptsRankingScope("unavailable",scalar,1),"a true analysis/ranking child remains chart-gated");
var multiObject=scalarPair("group",List.of("国内","海外"));
assertFalse(acceptsRankingScope("unavailable",multiObject,1),"a list of groups is not one exact selected object");
assertFalse(acceptsRankingScope("unavailable",scalarPair("product","~qv1~WyJBIiwiQiJd"),1),
"a two-object encoded selection is not a single product");
assertFalse(acceptsRankingScope("unavailable",scalarPair("product","~qv1~%%%"),1),
"a malformed selection wire cannot use the exception");
var noExactObject=scalarPair("department","汽车");noExactObject.put("filters",Map.of("product","A"));
assertFalse(acceptsRankingScope("unavailable",noExactObject,1),"the selected filter key must match the child dimension");
}
@Test void rankingCannotCompleteFromSchemaOnlyWithoutItsVerifiedArtifact()throws Exception {
var task=new LinkedHashMap<String,Object>();task.put("fields",List.of("scrapWeight","rate"));task.put("stages",List.of());task.put("dimensions",List.of("product"));
task.put("period",Map.of("year",2026,"month",9));task.put("filters",Map.of("includeUnassigned",false,"extraFilters",Map.of("process",List.of("A"))));
task.put("visualPreference","auto");task.put("visualType","bar");task.put("limit",5);
var chart=Map.<String,Object>of("metric","current_scrap_weight","type","bar","dimension","product","period",task.get("period"),
"scope",Map.of("releaseId","release-1","includeUnassigned",false,"extraFilters",Map.of("process",List.of("A"))),
"categories",List.of(Map.of("key","invented-product","label","invented-product")),
"series",List.of(Map.of("key","current_scrap_weight","values",List.of(999999))),
"coverage",Map.of("shareBasis","all_scope_weight","denominator",100));
var scope=Map.<String,Object>of("purpose","ranking","nodeId","rank-node","scopeId","rank-scope","task",task);
var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class);method.setAccessible(true);
assertFalse((boolean)method.invoke(service,Map.of("visualState","ready","visuals",List.of(chart)),scope,"release-1"),
"a shape-valid chart must not complete ranking without same-scope saved rank_artifact_json evidence");
}
@Test void rankingCompletionAcceptsOnlyTheChartBoundToItsSameScopeArtifactAndEvidenceRef()throws Exception {
var filters=Map.<String,Object>of("includeUnassigned",false,"extraFilters",Map.of("process",List.of("A")));
var task=new LinkedHashMap<String,Object>();task.put("fields",List.of("scrapWeight","rate"));task.put("stages",List.of());task.put("dimensions",List.of("product"));
task.put("period",Map.of("year",2026,"month",9));task.put("filters",filters);task.put("visualPreference","auto");task.put("visualType","bar");
var chart=new LinkedHashMap<String,Object>();chart.put("metric","current_scrap_weight");chart.put("type","bar");chart.put("dimension","product");chart.put("period",task.get("period"));
chart.put("scope",Map.of("releaseId","release-1","includeUnassigned",false,"extraFilters",Map.of("process",List.of("A"))));
chart.put("evidenceRefs",List.of("QABCDEF12"));chart.put("categories",List.of(Map.of("key","P1","label","产品一"),Map.of("key","P2","label","产品二"),Map.of("key","P3","label","产品三")));
chart.put("series",List.of(Map.of("key","current_scrap_weight","values",List.of(65,41,20))));
chart.put("coverage",Map.of("totalRows",3,"returnedRows",3,"truncated",false,"shareBasis","all_scope_weight","denominator",200));
var artifact=Map.<String,Object>of("releaseId","release-1","dimension","product","period",task.get("period"),"filters",filters,
"rankings",List.of(Map.of("stage","overall","metric","scrapWeight","completeRanking",true,"orderPolicy","weight_desc_key_codepoint_asc",
"totalRows",3,"denominator",200,"rows",List.of(Map.of("rank",1,"key","P1","label","产品一","weight",65),
Map.of("rank",2,"key","P2","label","产品二","weight",41),Map.of("rank",3,"key","P3","label","产品三","weight",20)))));
Class<?> bindingType=Arrays.stream(AnalysisProtocolService.class.getDeclaredClasses()).filter(c->c.getSimpleName().equals("RankingArtifactBinding")).findFirst().orElseThrow();
var constructor=bindingType.getDeclaredConstructor(String.class,String.class,String.class,String.class,Map.class);constructor.setAccessible(true);
Object binding=constructor.newInstance("query-1","QABCDEF12","rank-node","rank-scope",artifact);
var scope=Map.<String,Object>of("purpose","ranking","nodeId","rank-node","scopeId","rank-scope","limit",5,"task",task);
var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class,List.class);method.setAccessible(true);
assertTrue((boolean)method.invoke(service,Map.of("visualState","ready","visuals",List.of(chart)),scope,"release-1",List.of(binding)));
var crossRef=new LinkedHashMap<>(chart);crossRef.put("evidenceRefs",List.of("Q00000000"));
assertFalse((boolean)method.invoke(service,Map.of("visualState","ready","visuals",List.of(crossRef)),scope,"release-1",List.of(binding)));
}
@Test void emptyVerifiedRankingArtifactAllowsAutomaticNoChartButIncompleteArtifactDoesNot()throws Exception {
var filters=Map.<String,Object>of("includeUnassigned",false,"extraFilters",Map.of("process",List.of("A")));
var task=new LinkedHashMap<String,Object>();task.put("fields",List.of("scrapWeight","rate"));task.put("stages",List.of());task.put("dimensions",List.of("product"));
task.put("period",Map.of("year",2026,"month",9));task.put("filters",filters);task.put("visualPreference","auto");task.put("limit",5);
var emptyRanking=Map.<String,Object>of("stage","overall","metric","scrapWeight","completeRanking",true,
"orderPolicy","weight_desc_key_codepoint_asc","totalRows",0,"returnedRows",0,"truncated",false,
"emptyReason","no_positive_scrap","denominator",0,"rows",List.of());
var artifact=Map.<String,Object>of("releaseId","release-1","dimension","product","period",task.get("period"),"filters",filters,"rankings",List.of(emptyRanking));
Class<?> bindingType=Arrays.stream(AnalysisProtocolService.class.getDeclaredClasses()).filter(c->c.getSimpleName().equals("RankingArtifactBinding")).findFirst().orElseThrow();
var constructor=bindingType.getDeclaredConstructor(String.class,String.class,String.class,String.class,Map.class);constructor.setAccessible(true);
Object binding=constructor.newInstance("query-1","QABCDEF12","rank-node","rank-scope",artifact);
var scope=Map.<String,Object>of("purpose","ranking","nodeId","rank-node","scopeId","rank-scope","limit",5,"task",task);
var method=AnalysisProtocolService.class.getDeclaredMethod("plannedVisualsComplete",Map.class,Map.class,String.class,List.class);method.setAccessible(true);
assertTrue((boolean)method.invoke(service,Map.of("visualState","not_requested","visuals",List.of()),scope,"release-1",List.of(binding)),
"an exact complete artifact with no positive rank rows has no suitable chart to require");
var incomplete=new LinkedHashMap<String,Object>(artifact);incomplete.put("rankings",List.of(Map.of("stage","overall","metric","scrapWeight","completeRanking",false,
"orderPolicy","weight_desc_key_codepoint_asc","totalRows",0,"returnedRows",0,"truncated",false,
"emptyReason","no_positive_scrap","denominator",0,"rows",List.of())));
Object unverified=constructor.newInstance("query-1","QABCDEF12","rank-node","rank-scope",incomplete);
assertFalse((boolean)method.invoke(service,Map.of("visualState","not_requested","visuals",List.of()),scope,"release-1",List.of(unverified)),
"a zero-looking but incomplete artifact cannot bypass the chart gate");
assertFalse((boolean)method.invoke(service,Map.of("visualState","not_requested","visuals",List.of()),scope,"release-1",List.of()),
"absence of an artifact cannot be interpreted as no suitable data");
var stageTask=new LinkedHashMap<>(task);stageTask.put("fields",List.of("scrapWeight"));stageTask.put("stages",List.of("black"));
var stageRanking=new LinkedHashMap<String,Object>(emptyRanking);stageRanking.put("stage","black");
var stageArtifact=new LinkedHashMap<String,Object>(artifact);stageArtifact.put("rankings",List.of(stageRanking));
Object stageBinding=constructor.newInstance("query-2","QABCDEF13","rank-node","rank-scope",stageArtifact);
var stageScope=Map.<String,Object>of("purpose","ranking","nodeId","rank-node","scopeId","rank-scope","limit",5,"task",stageTask);
assertTrue((boolean)method.invoke(service,Map.of("visualState","unavailable","visuals",List.of()),stageScope,"release-1",List.of(stageBinding)),
"a formal empty single-stage scrap ranking also has no chartable rows");
}
}
@@ -21,6 +21,25 @@ class AnalysisProtocolExplorationTest {
@Test void explicitDimensionStagesRequireEachActualStageRatherThanOneDimensionAppearance(){var root=root();root.put("fields",List.of());root.put("dimensions",List.of("defect"));root.put("dimensionStages",Map.of("defect",List.of("black","bright")));var black=Map.<String,Object>of("purpose","defects","task",Map.of("filters",root.get("filters"),"dimensions",List.of("defect"),"stages",List.of("black")));var bright=Map.<String,Object>of("purpose","defects","task",Map.of("filters",root.get("filters"),"dimensions",List.of("defect"),"stages",List.of("bright")));assertTrue(AnalysisProtocolExploration.goalGaps(root,List.of(black)).stream().anyMatch(g->g.contains("defect/bright")));assertTrue(AnalysisProtocolExploration.goalGaps(root,List.of(black,bright)).isEmpty());root.put("dimensionStages",Map.of());assertTrue(AnalysisProtocolExploration.goalGaps(root,List.of(black)).isEmpty());}
@Test void bothStageRootAllowsCombinedChangesWithoutBroadeningSingleStage(){var root=root();root.put("stages",List.of("black","bright"));root.put("comparisonPeriod",Map.of("year",2025));var node=node();node.put("purpose","changes");node.put("dimension","product");node.put("fields",List.of("rate","totalWeight","scrapWeight"));node.put("stages",List.of());node.put("comparisonRequired",true);node.put("sort","rate_drop");node.put("limit",5);var result=AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog());assertEquals(List.of(),AnalysisProtocolService.map(result.scopes().getFirst().get("task")).get("stages"));root.put("stages",List.of("black"));assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog()));}
@Test void combinedRateMetricsAndTrendKeepBothStageCoverageObligations(){for(String purpose:List.of("metrics","trend")){var root=root();root.put("stages",List.of("black","bright"));root.put("trendRequired",purpose.equals("trend"));var node=node();node.put("purpose",purpose);node.put("fields",List.of("rate"));node.put("stages",List.of());node.put("selector",Map.of("kind","base"));var result=AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog());assertEquals(List.of("black","bright"),AnalysisProtocolService.map(result.scopes().getFirst().get("task")).get("stages"));assertTrue(AnalysisProtocolExploration.goalGaps(root,result.scopes()).isEmpty());}}
@Test void combinedRankingIsAllowedAsAChildOfBothStageRoot() {
var root=root();root.put("stages",List.of("black","bright"));root.put("fields",List.of("rate","scrapWeight"));
root.put("dimensions",List.of("product"));root.put("dimensionStages",Map.of("product",List.of("black","bright")));
var node=node();node.put("purpose","ranking");node.put("dimension","product");node.put("fields",List.of("scrapWeight","rate"));
node.put("stages",List.of());node.put("limit",5);node.put("selector",Map.of("kind","base"));
var result=AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog());
var compiled=AnalysisProtocolService.map(result.scopes().getFirst().get("task"));
assertEquals(List.of(),compiled.get("stages"));
assertTrue(AnalysisProtocolExploration.goalGaps(root,result.scopes()).isEmpty());
}
@Test void verifiedCombinedRankingCanSatisfyOverallRootFieldsWithoutDuplicateMetricsRead() {
var root=root();root.put("fields",List.of("scrapWeight","rate"));root.put("dimensions",List.of("product"));
var child=Map.<String,Object>of("fields",List.of("scrapWeight","rate"),"stages",List.of(),"dimensions",List.of("product"),"filters",root.get("filters"));
var scope=Map.<String,Object>of("purpose","ranking","task",child);
assertTrue(AnalysisProtocolExploration.goalGaps(root,List.of(scope)).isEmpty());
var wrongDimension=Map.<String,Object>of("fields",List.of("scrapWeight","rate"),"stages",List.of(),"dimensions",List.of("department"),"filters",root.get("filters"));
assertFalse(AnalysisProtocolExploration.goalGaps(root,List.of(Map.of("purpose","ranking","task",wrongDimension))).isEmpty(),
"a combined ranking for another dimension cannot satisfy the confirmed product goal");
}
@Test void bothStageRootStillRejectsUnstagedWeightsAndRawReading(){var root=root();root.put("stages",List.of("black","bright"));var node=node();node.put("stages",List.of());assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog()));root.put("fullDetails",true);node.put("purpose","records");node.put("fields",List.of());assertThrows(IllegalArgumentException.class,()->AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog()));}
@Test void declaredComparisonPeriodCannotBeIgnoredByOmittingBoolean(){var root=root();root.put("comparisonPeriod",Map.of("year",2025));var node=node();node.put("selector",Map.of("kind","base"));node.put("fields",List.of("rate"));node.put("stages",List.of());var result=AnalysisProtocolExploration.compile(json,root,node,AnalysisProtocolService.map(root.get("filters")),"e","r",catalog());assertTrue(AnalysisProtocolExploration.goalGaps(root,result.scopes()).stream().anyMatch(g->g.contains("期间比较")));}
@Test void narrowerObservationAloneCannotCompleteRootGoal(){var root=root();var resolution=AnalysisProtocolExploration.compile(json,root,node(),AnalysisProtocolService.map(root.get("filters")),"e","r",catalog());assertFalse(AnalysisProtocolExploration.goalGaps(root,resolution.scopes()).isEmpty());assertFalse(AnalysisProtocolExploration.goalGaps(root,List.of()).isEmpty());}
@@ -0,0 +1,74 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolHistoryDimensionProjectionTest {
@Test void legacyDimensionIsProjectedOnlyFromOneSignedTaskDimensionAndExactRowLineage() {
Map<String,Object> stored=legacyPayload(List.of("product"),null);
Map<String,Object> visible=legacyPayload(List.of("product"),null);
assertEquals(1,AnalysisProtocolHistory.projectLegacyRankingDimensions(visible));
assertNull(AnalysisProtocolService.map(stored.get("sections") instanceof List<?> xs?xs.getFirst():null).get("dimension"));
var section=AnalysisProtocolService.map(((List<?>)visible.get("sections")).getFirst());
var fact=AnalysisProtocolService.map(((List<?>)visible.get("facts")).getFirst());
assertEquals("product",section.get("dimension"));
assertEquals("product",AnalysisProtocolService.map(fact.get("scope")).get("dimension"));
}
@Test void multipleDimensionsAreNotInferredFromRankingTitle() {
Map<String,Object> visible=legacyPayload(List.of("product","department"),null);
assertEquals(0,AnalysisProtocolHistory.projectLegacyRankingDimensions(visible));
assertNull(AnalysisProtocolService.map(((List<?>)visible.get("sections")).getFirst()).get("dimension"));
assertNull(AnalysisProtocolService.map(AnalysisProtocolService.map(((List<?>)visible.get("facts")).getFirst()).get("scope")).get("dimension"));
}
@Test void conflictingBoundFactScopeRejectsTheWholeSectionProjection() {
Map<String,Object> visible=legacyPayload(List.of("product"),"department");
assertEquals(0,AnalysisProtocolHistory.projectLegacyRankingDimensions(visible));
assertNull(AnalysisProtocolService.map(((List<?>)visible.get("sections")).getFirst()).get("dimension"));
assertEquals("department",AnalysisProtocolService.map(AnalysisProtocolService.map(((List<?>)visible.get("facts")).getFirst()).get("scope")).get("dimension"));
}
@Test void fractionalOrTextualFactIndicesCannotBeTruncatedIntoAValidRowBinding() {
for(Object invalid:List.of(new BigDecimal("0.5"),"0",new BigDecimal("2147483648"))) {
for(String key:List.of("sectionIndex","rowIndex")) {
var visible=legacyPayload(List.of("product"),null);
@SuppressWarnings("unchecked") var fact=(Map<String,Object>)((List<?>)visible.get("facts")).getFirst();
fact.put(key,invalid);
assertEquals(0,AnalysisProtocolHistory.projectLegacyRankingDimensions(visible));
assertNull(AnalysisProtocolService.map(((List<?>)visible.get("sections")).getFirst()).get("dimension"));
}
}
}
private static Map<String,Object> legacyPayload(List<String> dimensions,String factDimension) {
var period=Map.<String,Object>of("year",2026,"month",9);
var filters=Map.<String,Object>of("group","业务分组A");
var row=Map.<String,Object>of("rank",1,"key","P-1","label","规格A","weight",new BigDecimal("12.50"));
var columns=List.of(Map.<String,Object>of("key","rank"),Map.<String,Object>of("key","key"),
Map.<String,Object>of("key","label"),Map.<String,Object>of("key","weight"));
var section=new LinkedHashMap<String,Object>();section.put("title","Top 1 product排名");
section.put("sourceTurnId","source-turn-A");section.put("period",period);section.put("filters",filters);
section.put("columns",columns);section.put("rows",List.of(row));section.put("note","signed");
var sourceFact=rankingFact(0,dimensionsFactScope(period,filters,null));
var topFact=new LinkedHashMap<>(rankingFact(0,dimensionsFactScope(period,filters,factDimension)));
var sourceSection=new LinkedHashMap<String,Object>(section);
var source=Map.<String,Object>of("sourceTurnId","source-turn-A","task",Map.of("dimensions",dimensions),
"sections",List.of(sourceSection),"facts",List.of(sourceFact));
return new LinkedHashMap<>(Map.of("sourceResults",List.of(source),"sections",List.of(new LinkedHashMap<>(section)),"facts",List.of(topFact)));
}
private static Map<String,Object> dimensionsFactScope(Map<String,Object> period,Map<String,Object> filters,String dimension) {
var scope=new LinkedHashMap<String,Object>();scope.put("period",period);scope.put("filters",filters);scope.put("stage","overall");
if(dimension!=null)scope.put("dimension",dimension);return scope;
}
private static Map<String,Object> rankingFact(int sectionIndex,Map<String,Object> scope) {
return Map.of("id","source-turn-A:fact:00001","sourceTurnId","source-turn-A","sectionIndex",sectionIndex,
"rowIndex",0,"metricCode","scrapWeight","rawValue",new BigDecimal("12.50"),
"text","Top 1 规格A · 综合报废重量 = 12.50","scope",scope);
}
}
@@ -9,6 +9,17 @@ import tools.jackson.databind.json.JsonMapper;
class AnalysisProtocolHistoryFactsTest {
private final JsonMapper json = JsonMapper.builder().build();
@Test void historyDeliveryWarningsAreSummarizedOnlyAsOneBusinessNotice() {
var history=new AnalysisProtocolHistory(null,json,null,null);
var summary=new LinkedHashMap<String,Object>();summary.put("notices",List.of("已有业务提示"));
history.appendSourceDeliveryWarnings(summary,List.of("PARTIAL","解释答复格式未通过有限修复"));
assertEquals(List.of("已有业务提示","本报告沿用已核验的正式统计结果,未核验细项不纳入判断。"),summary.get("notices"));
assertFalse(summary.toString().contains("PARTIAL"));assertFalse(summary.toString().contains("解释答复格式未通过有限修复"));
var empty=new LinkedHashMap<String,Object>();history.appendSourceDeliveryWarnings(empty,List.of(" "));
assertTrue(empty.isEmpty());
}
@Test void metricsKeepEachHistoricalScopeSeparateAndDoNotAverage() {
Map<String,Object> first = metricSource("turn-a", Map.of("year", 2026, "monthStart", 1, "monthEnd", 3),
new BigDecimal("0.812"));
@@ -37,6 +48,37 @@ class AnalysisProtocolHistoryFactsTest {
assertFalse(report.contains("0.8585"));
}
@Test void verifiedPartialSourceKeepsDeliveryWarningSeparateFromFormalDataGaps() {
var source=metricSource("turn-partial-delivery",Map.of("year",2026,"month",9),new BigDecimal("0.91"));
source.put("status","PARTIAL");
source.put("verifiedResultsMode",true);
source.put("sourceDeliveryWarnings",List.of("非必需细项范围未完整覆盖;未用于正式结论。"));
var projected=AnalysisProtocolHistoryFacts.buildSource(source,json);
assertTrue((Boolean)projected.get("complete"),projected.get("gaps").toString());
assertTrue(((List<?>)projected.get("sourceDeliveryWarnings")).contains("非必需细项范围未完整覆盖;未用于正式结论。"));
assertTrue(projected.get("sourceDeliveryWarnings").toString().contains("PARTIAL"));
assertEquals("PARTIAL",source.get("status"));
assertTrue(((List<?>)projected.get("gaps")).isEmpty());
}
@Test void partialSourceStillBlocksWithoutServerVerifiedResultsModeOrWithRealQueryGap() {
var source=metricSource("turn-partial-unverified",Map.of("year",2026,"month",9),new BigDecimal("0.91"));
source.put("status","PARTIAL");
var unverified=AnalysisProtocolHistoryFacts.buildSource(source,json);
assertFalse((Boolean)unverified.get("complete"));
assertTrue(unverified.get("gaps").toString().contains("PARTIAL"));
source.put("verifiedResultsMode",true);
var query=new LinkedHashMap<>(firstMap(source.get("queries")));
query.put("coverage",Map.of("metricsComplete",false,"missingEvidence",List.of("正式期间数据行缺失")));
source.put("queries",List.of(query));
var dataGap=AnalysisProtocolHistoryFacts.buildSource(source,json);
assertFalse((Boolean)dataGap.get("complete"));
assertTrue(dataGap.get("gaps").toString().contains("正式期间数据行缺失"));
}
@Test void ratioDisplayMetadataUsesExactRawNumbersAndKeepsOriginalTextAndFrame() {
var source=metricSource("turn-percent",Map.of("year",2026,"month",9),new BigDecimal("1"));
var query=firstMap(source.get("queries"));query.put("task",task("metrics",Map.of("year",2026,"month",9),Map.of("product","A"),List.of("rate"),List.of()));query.put("scope",Map.of("period",Map.of("year",2026,"month",9),"filters",Map.of("product","A")));
@@ -100,6 +142,69 @@ class AnalysisProtocolHistoryFactsTest {
assertFalse(htmlHeadlines.contains("missing-fact-id"));
}
@Test void historicalReportDeduplicatesSameScopedTableHidesSourceIdsAndUsesVerifiedDisplayUnits() {
var filters=Map.of("department","消费电子产业部","group","消费电子-3C");var period=Map.of("year",2026,"month",9);
var row=mapOf("rank",1,"label","规格甲","weight",new BigDecimal("493.99660000"),
"compositeRate",new BigDecimal("0.9122755444707148916393578115770785"),
"share",new BigDecimal("0.07078164231313173578917252155832738"));
var columns=List.of(mapOf("key","rank","label","排名"),mapOf("key","label","label","规格"),
mapOf("key","weight","label","综合报废重量(原单位)"),mapOf("key","compositeRate","label","综合合格率"),
mapOf("key","share","label","占同范围全部报废"));
var section=mapOf("title","Top 5 产品排名","period",period,"filters",filters,"columns",columns,"rows",List.of(row),
"note","正式分母为 6969.5000;本表只表示已保存Top 5,重量沿用来源单位。");
var chart=mapOf("id","saved-chart","originTurnId","turn-private-a","dimension","product","metric","current_scrap_weight",
"stage","overall","period",period,"unit","kg","scope",Map.of("department",filters.get("department"),
"group",filters.get("group"),"releaseId","rel-1"));
var firstFact=mapOf("id","turn-private-a:fact:00001","text","2026年9月规格甲Top1合格率91.23%","sourceTurnId","turn-private-a");
var secondFact=mapOf("id","turn-private-b:fact:00001","text","2026年9月规格甲Top1合格率91.23%","sourceTurnId","turn-private-b");
var first=mapOf("turnId","turn-private-a","sourceTurnId","turn-private-a","revision",7,"executionId","exec-private-a",
"status","COMPLETED","sections",List.of(section),"facts",List.of(firstFact),"sourceVisuals",List.of(chart));
var secondChart=new LinkedHashMap<>(chart);secondChart.put("originTurnId","turn-private-b");
var second=mapOf("turnId","turn-private-b","sourceTurnId","turn-private-b","revision",8,"executionId","exec-private-b",
"status","COMPLETED","sections",List.of(section),"facts",List.of(secondFact),"sourceVisuals",List.of(secondChart));
var rendered=AnalysisProtocolHistoryFacts.render("2026年9月历史排名",List.of(first,second),
List.of("turn-private-a:fact:00001","turn-private-b:fact:00001"),List.of());
String markdown=rendered.get("reportMarkdown").toString(),html=rendered.get("reportHtml").toString();
assertEquals(1,countOccurrences(markdown,"## Top 5规格排名(按合计报废重量排序)"));
assertEquals(1,countOccurrences(html,"<section><h2>Top 5规格排名(按合计报废重量排序)</h2>"));
for(String internal:List.of("turn-private-a","turn-private-b","exec-private-a","exec-private-b","history-fact:")) {
assertFalse(markdown.contains(internal));assertFalse(html.contains(internal));
}
assertTrue(markdown.contains("493.9966 kg"));assertTrue(html.contains("493.9966 kg"));
assertTrue(markdown.contains("91.23%"));assertTrue(html.contains("91.23%"));
assertTrue(markdown.contains("7.08%"));assertTrue(html.contains("7.08%"));
assertTrue(markdown.contains("正式分母为 6969.5000"));
assertEquals(1,maps(rendered.get("historySections")).size());
assertEquals(new BigDecimal("493.99660000"),firstMap(maps(rendered.get("historySections")).getFirst().get("rows")).get("weight"));
}
@Test void historicalHeadlineIndicesStayBoundToOriginalSectionsAfterDuplicateRemoval() {
List<Map<String,Object>> sources=new ArrayList<>();List<String> selected=new ArrayList<>();
List<String> departments=List.of("A","A","B","C");List<BigDecimal> rates=List.of(
new BigDecimal(".1"),new BigDecimal(".1"),new BigDecimal(".2"),new BigDecimal(".3"));
for(int index=0;index<departments.size();index++) {
String turn="source-"+index,department=departments.get(index),id=turn+":fact:00001";
var period=Map.<String,Object>of("year",2026,"month",9);var filters=Map.<String,Object>of("department",department);
var row=mapOf("field","合格率","metricCode","rate","value",rates.get(index));
var section=mapOf("title","正式指标汇总","sourceTurnId",turn,"period",period,"filters",filters,
"columns",List.of(mapOf("key","field","label","指标"),mapOf("key","value","label","正式值")),
"rows",List.of(row),"note","来自已核验正式响应。");
var fact=mapOf("id",id,"text","正式合格率","sourceTurnId",turn,"sectionIndex",index,
"rowIndex",0,"metricCode","rate","displayText","正式合格率");
sources.add(mapOf("turnId",turn,"sourceTurnId",turn,"status","COMPLETED",
"sections",List.of(section),"facts",List.of(fact)));
if(index>=2)selected.add(id);
}
var rendered=AnalysisProtocolHistoryFacts.render("按范围汇总",sources,selected,List.of());
String markdown=Objects.toString(rendered.get("reportMarkdown"),"");
assertEquals(3,maps(rendered.get("historySections")).size());
assertEquals(3,countOccurrences(markdown,"## 正式指标汇总"));
assertTrue(markdown.contains("产业部 B · 合格率 = 20.00%"),markdown);
assertTrue(markdown.contains("产业部 C · 合格率 = 30.00%"),markdown);
assertFalse(markdown.contains("产业部 B · 合格率 = 30.00%"),markdown);
assertFalse(markdown.contains("产业部 C · 合格率 = 20.00%"),markdown);
}
@Test void metricQueryPeriodOverridesRootAndScopePeriodForComparisonReads() {
Map<String,Object> currentPeriod = Map.of("year", 2026, "month", 9);
Map<String,Object> task = task("metrics", currentPeriod, Map.of(), List.of("totalWeight"), List.of("black"));
@@ -129,6 +234,8 @@ class AnalysisProtocolHistoryFactsTest {
"task", Map.of("releaseId", "rel-1"), "queries", List.of(query)), json);
Map<String,Object> section = maps(projected.get("sections")).getFirst();
assertEquals("black", section.get("stage"));
assertEquals("product",section.get("dimension"));
assertEquals("product",AnalysisProtocolService.map(firstMap(projected.get("facts")).get("scope")).get("dimension"));
assertTrue(section.get("title").toString().contains("Top 5"));
assertTrue(section.get("note").toString().contains("120.500"));
assertTrue(section.get("note").toString().contains("不代表全量分组"));
@@ -139,6 +246,64 @@ class AnalysisProtocolHistoryFactsTest {
assertTrue(((String) rendered.get("reportHtml")).contains("黑片"));
}
@Test void overallCombinedRankingAcceptsEmptyStageAndKeepsEveryArtifactValue() {
var task=task("ranking",Map.of("year",2026,"month",9),Map.of(),List.of("rate","scrapWeight"),List.of());
var rankRows=List.of(
mapOf("rank",1,"key","P1","label","规格一","weight",new BigDecimal("65"),
"compositeRate",new BigDecimal(".81"),"blackScrapWeight",new BigDecimal("5"),"brightScrapWeight",new BigDecimal("60"),
"blackTotalWeight",new BigDecimal("100"),"brightTotalWeight",new BigDecimal("200"),
"share",new BigDecimal(".65"),"cumulativeShare",new BigDecimal(".65")),
mapOf("rank",2,"key","P2","label","规格二","weight",new BigDecimal("35"),
"compositeRate",new BigDecimal(".92"),"blackScrapWeight",new BigDecimal("34"),"brightScrapWeight",new BigDecimal("1"),
"blackTotalWeight",new BigDecimal("300"),"brightTotalWeight",new BigDecimal("400"),
"share",new BigDecimal(".35"),"cumulativeShare",BigDecimal.ONE));
var rank=mapOf("stage","overall","metric","scrapWeight","totalRows",2,"denominator",new BigDecimal("100"),
"completeRanking",true,"rows",rankRows);
var query=mapOf("queryId","q-overall-rank","target","GET /api/metrics","task",task,
"parameters",Map.of("year","2026","month","9","view","product"),
"scope",Map.of("period",task.get("period"),"filters",Map.of()),"coverage",Map.of("weightsComplete",true),
"rankArtifact",mapOf("dimension","product","displayLimit",10,"rankings",List.of(rank)),
"response",Map.of("summary",Map.of("compositeRate",new BigDecimal(".90"))));
var projected=AnalysisProtocolHistoryFacts.buildSource(mapOf("turnId","turn-overall-rank","status","COMPLETED",
"task",Map.of("releaseId","rel-1"),"queries",List.of(query)),json);
assertTrue((Boolean)projected.get("complete"),projected.get("gaps").toString());
assertEquals(2,maps(projected.get("facts")).size());
var section=firstMap(projected.get("sections"));
assertFalse(section.containsKey("stage"));
var rows=maps(section.get("rows"));assertEquals(2,rows.size());
assertEquals(new BigDecimal(".81"),rows.getFirst().get("compositeRate"));
assertEquals(new BigDecimal("5"),rows.getFirst().get("blackScrapWeight"));
assertEquals(new BigDecimal("60"),rows.getFirst().get("brightScrapWeight"));
assertEquals(new BigDecimal("200"),rows.getFirst().get("brightTotalWeight"));
assertEquals(new BigDecimal(".65"),rows.getFirst().get("share"));
assertEquals(BigDecimal.ONE,rows.get(1).get("cumulativeShare"));
assertTrue(firstText(projected.get("facts")).contains("综合合格率"));
assertTrue(firstText(projected.get("facts")).contains("同范围正式分母 = 100"));
}
@Test void verifiedZeroScrapRankingsAreBrieflyProjectedButMissingEmptyContractRemainsAGap() {
var task=task("ranking",Map.of("year",2026,"month",9),Map.of(),List.of("scrapWeight"),List.of("bright"));
var rank=mapOf("stage","bright","metric","scrapWeight","totalRows",0,"returnedRows",0,
"denominator",BigDecimal.ZERO,"completeRanking",true,"truncated",false,
"emptyReason","no_positive_scrap","orderPolicy","weight_desc_key_codepoint_asc","rows",List.of());
var query=mapOf("queryId","q-zero-bright","target","GET /api/metrics","task",task,
"parameters",Map.of("year","2026","month","9","view","product"),
"scope",Map.of("period",task.get("period"),"filters",Map.of()),"coverage",Map.of("weightsComplete",true),
"rankArtifact",mapOf("dimension","product","displayLimit",10,"rankings",List.of(rank)),
"response",Map.of("summary",Map.of("bright",Map.of("scrapWeight",BigDecimal.ZERO))));
var source=mapOf("turnId","turn-zero-bright","status","COMPLETED","task",Map.of("releaseId","rel-1"),"queries",List.of(query));
var projected=AnalysisProtocolHistoryFacts.buildSource(source,json);
assertTrue((Boolean)projected.get("complete"),projected.get("gaps").toString());
assertTrue(firstText(projected.get("facts")).contains("亮片正式报废总重量为 0"));
var section=firstMap(projected.get("sections"));assertEquals("bright",section.get("stage"));
assertTrue(section.get("note").toString().contains("空榜不代表缺失来源数据"));
rank.remove("emptyReason");
var incomplete=AnalysisProtocolHistoryFacts.buildSource(source,json);
assertFalse((Boolean)incomplete.get("complete"));
assertTrue(incomplete.get("gaps").toString().contains("没有可展示的正重量分组"));
}
@Test void defectsKeepMissingWeightNullAndExposeDiffReconciliation() {
Map<String,Object> defectTask = task("defects", Map.of("year", 2026, "month", 9), Map.of("product", "P1"),
List.of("scrapWeight"), List.of("bright"));
@@ -312,6 +477,72 @@ class AnalysisProtocolHistoryFactsTest {
"status","COMPLETED","sections",List.of(section),"facts",List.of())),List.of(),List.of());
}
@Test void historicalRankingFallbackUsesHumanScopeAndHidesInternalKeysAndFactIds() {
String turn="turn-safe-history";
var filters=Map.<String,Object>of("department","消费电子产业部","group","消费电子-3C",
"columnFilters",List.of(Map.of("field","src_work_order","stages",List.of("black"),
"operator","in","values",List.of("WO-7"))),
"columnLabels",Map.of("src_work_order","黑片委托单号"));
var rows=List.of(Map.<String,Object>of("rank",1,"key","private-product-key-1","label","规格甲",
"weight",new BigDecimal("35.055"),"compositeRate",new BigDecimal(".9123")),
Map.<String,Object>of("rank",2,"key","private-product-key-2","label","规格乙",
"weight",new BigDecimal("17.25"),"compositeRate",new BigDecimal(".905")));
var section=mapOf("title","Top 2 product排名","sourceTurnId",turn,"period",Map.of("year",2026,"month",9),
"filters",filters,"dimension","product","columns",List.of(
mapOf("key","rank","label","排名"),mapOf("key","key","label","分组键"),
mapOf("key","label","label","分组"),mapOf("key","weight","label","报废总重(原单位)"),
mapOf("key","compositeRate","label","综合合格率")),"rows",rows,
"note","正式分母(综合报废重量)为 52.305;重量沿用来源单位。");
var fact=mapOf("id",turn+":fact:00001","sourceTurnId",turn,"sectionIndex",0,"rowIndex",0,
"metricCode","scrapWeight","text","2026年9月(筛选 {group: 消费电子-3C, department: 消费电子产业部}):规格甲 报废重量=35.055",
"displayText","2026年9月(筛选 {group: 消费电子-3C, department: 消费电子产业部}):规格甲 报废重量=35.055");
var source=mapOf("turnId",turn,"status","COMPLETED","task",Map.of("sourceFieldLabels",Map.of("src_work_order","黑片委托单号")),
"sections",List.of(section),"facts",List.of(fact));
var rendered=AnalysisProtocolHistoryFacts.render("历史质量报告",List.of(source),List.of(fact.get("id").toString()),
List.of("正文解释未完成事实核验,报告保持部分完成。"));
String answer=rendered.get("answer").toString(),markdown=rendered.get("reportMarkdown").toString(),html=rendered.get("reportHtml").toString();
for(String visible:List.of(answer,markdown,html)) {
assertTrue(visible.contains("产业部 消费电子产业部"),visible);
assertTrue(visible.contains("业务分组 消费电子-3C"),visible);
assertTrue(visible.contains("黑片委托单号 包含 WO-7"),visible);
assertTrue(visible.contains("规格甲"),visible);
assertFalse(visible.contains("筛选 {"),visible);assertFalse(visible.contains("group:"),visible);
assertFalse(visible.contains("department:"),visible);assertFalse(visible.contains("src_work_order"),visible);
assertFalse(visible.contains("分组键"),visible);assertFalse(visible.contains("private-product-key"),visible);
assertFalse(visible.contains(turn+":fact:"),visible);assertFalse(visible.contains("product排名"),visible);
}
assertFalse(answer.contains("Top 2规格排名"));
assertTrue(markdown.contains("Top 2规格排名(按合计报废重量排序)"));
assertTrue(html.contains("Top 2规格排名(按合计报废重量排序)"));
assertTrue(markdown.contains("35.055"));assertTrue(markdown.contains("91.23%"));
assertTrue(markdown.contains("正文解释未完成事实核验"));
}
@Test void historicalMetadataFilterKeepsBusinessCodesUuidShapedValuesAndPreciseNumbers() {
String business="规格编号 QABCDEF12 / 123e4567-e89b-12d3-a456-426614174000,正式重量 123456789.123456789 kg。";
String turn="turn-business-values";
var businessFact=mapOf("id",turn+":fact:00001","sourceTurnId",turn,
"text",business,"displayText",business);
var businessSection=mapOf("title","业务值","sourceTurnId",turn,"period",Map.of("year",2026,"month",9),
"filters",Map.of(),"columns",List.of(mapOf("key","value","label","规格编码")),
"rows",List.of(mapOf("value","QABCDEF12")),"note",business);
var metadataSection=mapOf("title","元数据","sourceTurnId",turn,"period",Map.of("year",2026,"month",9),
"filters",Map.of(),"columns",List.of(mapOf("key","value","label","说明")),
"rows",List.of(mapOf("value","仅用于测试")),
"note","sourceTurnId=internal-source filters={\\\"group\\\":\\\"internal\\\"}");
var source=mapOf("turnId",turn,"status","COMPLETED","sections",List.of(businessSection,metadataSection),
"facts",List.of(businessFact));
var rendered=AnalysisProtocolHistoryFacts.render("历史报告",List.of(source),List.of(turn+":fact:00001"),List.of());
String answer=rendered.get("answer").toString(),markdown=rendered.get("reportMarkdown").toString();
assertTrue(answer.contains("QABCDEF12"));assertTrue(answer.contains("123e4567-e89b-12d3-a456-426614174000"));
assertTrue(answer.contains("123456789.123456789"));
assertTrue(markdown.contains("QABCDEF12"));assertTrue(markdown.contains("123e4567-e89b-12d3-a456-426614174000"));
assertTrue(markdown.contains("123456789.123456789"));
assertFalse(markdown.contains("sourceTurnId=internal-source"));
assertFalse(markdown.contains("filters="));
}
@Test void reportEscapesHtmlAndNeverUsesUnverifiedSourceAnswerText() {
Map<String,Object> emptySource = mapOf("sourceTurnId", "turn-empty", "status", "COMPLETED",
"answer", "旧回答密文不能进入报告 <script>alert(1)</script>", "facts", List.of(),
@@ -7,7 +7,20 @@ import org.junit.jupiter.api.Test;
class AnalysisProtocolHistorySummaryTest {
private final String ref="turn:fact:00001";
private Map<String,Object> details(){return new LinkedHashMap<>(Map.of("answer","自由解释["+ref+"]","readingSummary",Map.of("version",1,"lead","先给结论["+ref+"]","evidence",List.of("依据["+ref+"]"),"notices",List.of(),"evidenceRefs",List.of(ref),"untrustedHtml","<script>bad</script>")));}
@Test void retainsVerifiedFreeExplanationWithOnlySupportedSummaryFields(){var clean=AnalysisProtocolHistorySummary.normalize(details(),Set.of(ref),"fallback");assertEquals("自由解释["+ref+"]",clean.answer());assertEquals(Set.of("version","lead","evidence","notices","evidenceRefs"),clean.summary().keySet());assertEquals(List.of(ref),clean.refs());assertTrue(clean.gaps().isEmpty());}
@Test void retainsVerifiedFreeExplanationWithoutShowingInternalFactReferences(){var clean=AnalysisProtocolHistorySummary.normalize(details(),Set.of(ref),"fallback");assertEquals("自由解释",clean.answer());assertEquals("先给结论",clean.summary().get("lead"));assertEquals(List.of("依据"),clean.summary().get("evidence"));assertEquals(Set.of("version","lead","evidence","notices","evidenceRefs"),clean.summary().keySet());assertEquals(List.of(ref),clean.refs());assertTrue(clean.gaps().isEmpty());assertFalse(clean.answer().contains(ref));}
@Test void retainsFormalWeightsAndLongPrecisionRateThroughCompleteNormalization(){
String answer="9月Top首位综合报废重量493.99660000["+ref+"],8月Top首位综合报废重量1155.48340000["+ref+"],综合合格率0.9122755444707148916393578115770785["+ref+"]。";
var raw=details();raw.put("answer",answer);raw.put("readingSummary",Map.of("version",1,
"lead","Top首位:报废重量493.99660000、1155.48340000;综合合格率0.9122755444707148916393578115770785["+ref+"]",
"evidence",List.of("正式综合合格率0.9122755444707148916393578115770785["+ref+"]"),
"notices",List.of(),"evidenceRefs",List.of(ref)));
var clean=AnalysisProtocolHistorySummary.normalize(raw,Set.of(ref),"fallback");
assertEquals("9月Top首位综合报废重量493.99660000,8月Top首位综合报废重量1155.48340000,综合合格率0.9122755444707148916393578115770785。",clean.answer());
assertEquals("Top首位:报废重量493.99660000、1155.48340000;综合合格率0.9122755444707148916393578115770785",clean.summary().get("lead"));
assertEquals(List.of("正式综合合格率0.9122755444707148916393578115770785"),clean.summary().get("evidence"));
assertEquals(List.of(ref),clean.refs());assertTrue(clean.gaps().isEmpty());
}
@Test void dropsUnknownCitationParagraphAndIllegalHtmlInsteadOfMakingUpQRefs(){var raw=details();raw.put("answer","有效解释["+ref+"]\n\n伪造结论[unknown:fact:99999]");raw.put("readingSummary",Map.of("version",1,"lead","<b>有效解释["+ref+"]</b><script>alert(1)</script>","evidence",List.of("伪造[QDEADBEEF]","已核实["+ref+"]"),"notices",List.of(),"evidenceRefs",List.of(ref,"QDEADBEEF")));var clean=AnalysisProtocolHistorySummary.normalize(raw,Set.of(ref),"fallback");assertFalse(clean.answer().contains("伪造"));assertFalse(clean.summary().toString().contains("script"));assertFalse(clean.summary().toString().contains("QDEADBEEF"));assertEquals(List.of(ref),clean.refs());assertFalse(clean.gaps().isEmpty());}
@Test void claimedMetadataAloneCannotCreateTrustedReferences(){var raw=details();raw.put("answer","没有引用的说法");raw.put("readingSummary",Map.of("version",1,"lead","没有引用的说法","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of(ref)));var clean=AnalysisProtocolHistorySummary.normalize(raw,Set.of(ref),"fallback");assertTrue(clean.refs().isEmpty());assertFalse(clean.gaps().isEmpty());}
@Test void rejectsInternalJsonAndUnknownReferencesWithoutTreatingLongDecimalsAsInternal(){var raw=details();raw.put("answer","正式率91.23%["+ref+"]\n\nfilters={\"product\":\"A\"}\n\n原值0.91234567890123456789["+ref+"]");raw.put("readingSummary",Map.of("version",1,"lead","正式率91.23%["+ref+"]","evidence",List.of("伪造[QDEADBEEF]","已核实["+ref+"]"),"notices",List.of("queryId=QDEADBEEF"),"evidenceRefs",List.of(ref,"QDEADBEEF")));var clean=AnalysisProtocolHistorySummary.normalize(raw,Set.of(ref),"fallback");assertEquals("正式率91.23%\n\n原值0.91234567890123456789",clean.answer());assertEquals("正式率91.23%",clean.summary().get("lead"));assertEquals(List.of("已核实"),clean.summary().get("evidence"));assertFalse(clean.answer().contains("filters"));assertFalse(clean.summary().toString().contains("QDEADBEEF"));assertFalse(clean.gaps().isEmpty());}
}
@@ -0,0 +1,90 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolHistoryTaskContextTest {
@Test void visibleImportKeepsOnlySignedBusinessTaskAndDoesNotMutateStoredPayload() {
var stored=payload();
String original=stored.toString();
var visible=AnalysisProtocolHistory.modelVisiblePayload(stored);
assertFalse(visible.containsKey("sourceResults"));
var task=context(visible);
assertEquals(Set.of("period","filters","dimensions","fields","stages","comparisonPeriod",
"goal","rootGoal","question","sourceFieldLabels","queryPlan"),task.keySet());
assertEquals("按报废总重查看重点规格",task.get("goal"));
var node=AnalysisProtocolService.map(((List<?>)task.get("queryPlan")).getFirst());
assertEquals(Set.of("purpose","dimension","fields","stages","limit"),node.keySet());
assertEquals(5,node.get("limit"));
assertFalse(task.toString().contains("private-connection"));
assertFalse(visible.toString().contains("private-saved-answer"));
assertEquals(original,stored.toString());
@SuppressWarnings("unchecked") var filters=(Map<String,Object>)task.get("filters");
filters.put("product","仅修改展示副本");
assertEquals(original,stored.toString());
}
@Test void unavailableOrDifferentSourceIdentityCannotSupplyTaskContext() {
for(String changed:List.of("available","revision","executionId","status")) {
var stored=payload();
var selected=selected(stored);
selected.put(changed,switch(changed) {
case "available" -> false;
case "revision" -> 3;
case "executionId" -> "other-execution";
default -> "FAILED";
});
assertFalse(selected(AnalysisProtocolHistory.modelVisiblePayload(stored)).containsKey("taskContext"),changed);
}
}
@Test void duplicateSourcesAndDuplicateSelectionDoNotGrantAmbiguousContext() {
var duplicateSources=payload();
Object source=((List<?>)duplicateSources.get("sourceResults")).getFirst();
duplicateSources.put("sourceResults",List.of(source,source));
assertFalse(selected(AnalysisProtocolHistory.modelVisiblePayload(duplicateSources)).containsKey("taskContext"));
var duplicateSelection=payload();
Object selected=((List<?>)duplicateSelection.get("historySources")).getFirst();
duplicateSelection.put("historySources",List.of(selected,selected));
assertFalse(selected(AnalysisProtocolHistory.modelVisiblePayload(duplicateSelection)).containsKey("taskContext"));
}
@Test void revisionMustBeAnExactNumberAndStaleContextIsRemoved() {
for(Object revision:List.of(new BigDecimal("2.5"),"2",new BigDecimal("2147483648"))) {
var stored=payload();
selected(stored).put("revision",revision);
selected(stored).put("taskContext",Map.of("goal","stale"));
assertFalse(selected(AnalysisProtocolHistory.modelVisiblePayload(stored)).containsKey("taskContext"));
}
}
private static Map<String,Object> context(Map<String,Object> payload) {
return AnalysisProtocolService.map(selected(payload).get("taskContext"));
}
@SuppressWarnings("unchecked") private static Map<String,Object> selected(Map<String,Object> payload) {
return (Map<String,Object>)((List<?>)payload.get("historySources")).getFirst();
}
private static Map<String,Object> payload() {
var task=new LinkedHashMap<String,Object>();
task.put("period",Map.of("year",2026,"month",9));task.put("filters",Map.of("department","部门A"));
task.put("dimensions",List.of("product"));task.put("fields",List.of("rate","scrapWeight"));task.put("stages",List.of());
task.put("comparisonPeriod",null);task.put("goal","按报废总重查看重点规格");
task.put("rootGoal","查看重点规格");task.put("question","不良Top5");
task.put("sourceFieldLabels",Map.of("customerCode","客户代码"));
task.put("connection","private-connection");
task.put("queryPlan",List.of(Map.of("purpose","ranking","dimension","product",
"fields",List.of("rate","scrapWeight"),"stages",List.of(),"limit",5,
"selector",Map.of("private","private-connection"),"nodeId","internal-node")));
var source=new LinkedHashMap<String,Object>();source.put("sourceTurnId","source-A");source.put("revision",2L);
source.put("executionId","execution-A");source.put("status","COMPLETED");source.put("task",task);
source.put("answer","private-saved-answer");
var selected=new LinkedHashMap<String,Object>();selected.put("turnId","source-A");selected.put("revision",2);
selected.put("executionId","execution-A");selected.put("status","COMPLETED");selected.put("available",true);
return new LinkedHashMap<>(Map.of("historySources",List.of(selected),"sourceResults",List.of(source),
"sections",List.of(),"facts",List.of()));
}
}
@@ -0,0 +1,37 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolHistoryVisualTest {
private static final String TURN="source-turn-1";
private static final String REF="QABCDEF12";
private static final Set<String> FIELDS=Set.of("black_good_weight","black_total_weight");
private static final Map<String,Object> SCOPE=Map.of("releaseId","release-1","year",2026,"month",9,"view","product","includeUnassigned",false);
@Test void savedVisualsKeepSourceEvidenceAndScopeOnlyAfterCurrentPermissionRecheck() {
var chart=chart();
var answer=Map.<String,Object>of("visualSchemaVersion",1,"visualState","ready","visuals",List.of(chart),"queryEvidenceRefs",List.of(REF));
var checked=AnalysisProtocolHistory.checkSavedVisuals(answer,FIELDS,Set.of(),TURN,SCOPE);
assertEquals("ready",checked.state());assertEquals(1,checked.visuals().size());
var saved=checked.visuals().getFirst();assertEquals(TURN,saved.get("originTurnId"));assertEquals(List.of(REF),saved.get("evidenceRefs"));
assertEquals(SCOPE,saved.get("scope"));assertEquals(Map.of("year",2026,"month",9),saved.get("period"));
var revoked=AnalysisProtocolHistory.checkSavedVisuals(answer,Set.of("black_scrap_weight"),Set.of(),TURN,SCOPE);
assertEquals("failed",revoked.state());assertTrue(revoked.visuals().isEmpty());
var forgedRefAnswer=new LinkedHashMap<>(answer);forgedRefAnswer.put("queryEvidenceRefs",List.of("QDEADBEEF"));
var unbound=AnalysisProtocolHistory.checkSavedVisuals(forgedRefAnswer,FIELDS,Set.of(),TURN,SCOPE);
assertEquals("failed",unbound.state());assertTrue(unbound.visuals().isEmpty());
}
private Map<String,Object> chart() {
var chart=new LinkedHashMap<String,Object>();chart.put("id","rank-chart");chart.put("type","column");chart.put("title","规格合格率");chart.put("unit","ratio");
chart.put("categories",List.of(Map.of("key","A","label","规格A"),Map.of("key","B","label","规格B")));
chart.put("series",List.of(Map.of("key","black_rate","label","黑片合格率","values",List.of(.91,.92))));
chart.put("evidenceRefs",List.of(REF));chart.put("requiredFields",List.of("black_good_weight","black_total_weight"));
chart.put("scope",SCOPE);chart.put("period",Map.of("year",2026,"month",9));chart.put("stage","black");chart.put("metric","black_rate");
chart.put("coverage",Map.of("totalRows",2,"returnedRows",2,"truncated",false));chart.put("originTurnId",TURN);
chart.put("sectionKey","product");chart.put("dimension","product");return chart;
}
}
@@ -1,10 +1,88 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolMetricCoverageTest {
@Test void knownZeroSourceStageMayOmitOnlyItsMissingScrapWeightWhileOfficialCompositeRateRemainsValid() {
var task=task(List.of("rate","scrapWeight"),List.of(),false,"available");
task.put("dimensions",List.of("product"));
var period=Map.<String,Object>of("year",2026,"month",9);
var zeroBrightOnly=Map.<String,Object>of(
"summary",Map.of("black",Map.of("rate",1),"bright",Map.of("scrapWeight",0,"totalWeight",3.17),
"compositeRate",1,"coverage",Map.of("blackRows",0,"brightRows",3)),
"coverage",Map.of("sourceMonths",List.of(month(2026,9,0,3))));
var singleBlack=Map.<String,Object>of(
"summary",Map.of("black",Map.of("rate",0.8646003262642741,"scrapWeight",0.83,"totalWeight",6.13),
"bright",Map.of("rate",1),"compositeRate",0.8646003262642741,
"coverage",Map.of("blackRows",2,"brightRows",0)),
"coverage",Map.of("sourceMonths",List.of(month(2026,9,2,0))));
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(zeroBrightOnly,task,period)),
"the actual 0/3 source snapshot has a known absent black stage and a formal zero bright scrap weight");
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(singleBlack,task,period)),
"the actual 2/0 source snapshot has a formal black weight and an absent bright stage");
assertEquals(1,AnalysisProtocolService.map(zeroBrightOnly.get("summary")).get("compositeRate"));
assertEquals(0.8646003262642741,AnalysisProtocolService.map(singleBlack.get("summary")).get("compositeRate"));
}
@Test void combinedScrapDoesNotTreatUnknownOrContradictoryMissingStageAsZero() {
var task=task(List.of("rate","scrapWeight"),List.of(),false,"available");task.put("dimensions",List.of("product"));
var period=Map.<String,Object>of("year",2026,"month",9);
var unknown=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",2),
"bright",Map.of("scrapWeight",0),"compositeRate",0.9),
"coverage",Map.of("sourceMonths",List.of(month(2026,9,null,3))));
var contradictory=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",2),
"bright",Map.of("scrapWeight",3),"compositeRate",0.9,
"coverage",Map.of("blackRows",2,"brightRows",0)),
"coverage",Map.of("sourceMonths",List.of(month(2026,9,2,0))));
var missingZeroProof=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",2),
"bright",Map.of("rate",1),"compositeRate",0.9),
"coverage",Map.of("sourceMonths",List.of(month(2026,9,2,0))));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(unknown,task,period)));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(contradictory,task,period)));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(missingZeroProof,task,period)));
}
@Test void combinedRankingUsesEitherPublishedStagePerMonthAndHonorsPeriodCoverageMode() {
var task=task(List.of("scrapWeight","rate"),List.of(),false,"available");task.put("dimensions",List.of("product"));
var period=Map.<String,Object>of("year",2026,"monthStart",3,"monthEnd",5);
var response=summary(Map.of("compositeRate",0.913,"black",Map.of("scrapWeight",12,"totalWeight",100),
"bright",Map.of("scrapWeight",8,"totalWeight",80)),List.of(month(2026,3,2,0),
month(2026,4,0,3),month(2026,5,0,0)));
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(response,task,period)),
"available coverage may omit a month with no source when the other months have a valid stage source");
task.put("periodCoverage","complete");
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(response,task,period)),
"complete coverage must reject a queried month with no source in either stage");
}
@Test void combinedRankingRejectsUnknownOrBothMissingStageSourcesWithoutRecomputingFormalRate() {
var task=task(List.of("scrapWeight","rate"),List.of(),false,"available");task.put("dimensions",List.of("product"));
var period=Map.<String,Object>of("year",2026,"monthStart",3,"monthEnd",4);
var unknown=summary(Map.of("compositeRate",0.913,"black",Map.of("scrapWeight",12,"totalWeight",100),
"bright",Map.of("scrapWeight",8,"totalWeight",80)),List.of(month(2026,3,2,null),month(2026,4,0,3)));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(unknown,task,period)));
var absent=summary(Map.of("compositeRate",0.913,"black",Map.of("scrapWeight",12,"totalWeight",100),
"bright",Map.of("scrapWeight",8,"totalWeight",80)),List.of(month(2026,3,2,0),month(2026,4,0,0)));
task.put("periodCoverage","complete");
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(absent,task,period)));
assertEquals(0.913,AnalysisProtocolService.map(absent.get("summary")).get("compositeRate"),
"coverage validation must preserve the official composite rate instead of deriving a replacement");
}
@Test void combinedRankingRequiresBothOfficialScrapSourcesAndPublishedCompositeRate() {
var selected=task(List.of("rate","scrapWeight"),List.of(),false,"available");selected.put("dimensions",List.of("product"));
var period=Map.<String,Object>of("year",2026,"month",9);
var response=summary(Map.of("compositeRate",0.9,"black",Map.of("scrapWeight",0,"totalWeight",100),
"bright",Map.of("scrapWeight",10,"totalWeight",100)),List.of(month(2026,9,2,3)));
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(response,selected,period)));
var missing=summary(Map.of("compositeRate",0.9,"black",Map.of("scrapWeight",0),"bright",Map.of("totalWeight",100)),List.of(month(2026,9,2,3)));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(missing,selected,period)));
}
@Test void summaryUsesQueriedPeriodAndAvailableMonthsWithoutFillingGaps() {
Map<String,Object> task = task(List.of("rate"), List.of("black"), false, "available");
task.put("period", Map.of("year", 2026, "month", 1));
@@ -68,6 +146,24 @@ class AnalysisProtocolMetricCoverageTest {
List.of(unknown)), task, Map.of("year", 2026, "month", 1))));
}
@Test void formalRatesAboveOnePassOnlyWithTheirObservedSourceAndPositiveDenominator() {
BigDecimal independentComposite=new BigDecimal("120").divide(new BigDecimal("100"));
Map<String,Object> composite=summary(Map.of("compositeRate",independentComposite,
"black",Map.of("totalWeight",60),"bright",Map.of("totalWeight",40)),
List.of(month(2026,9,2,2)));
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(composite,task(List.of("rate"),List.of(),false,"available"),
Map.of("year",2026,"month",9))),"the formal aggregate may exceed 100% under the signed correction rule");
assertEquals(independentComposite,AnalysisProtocolService.map(composite.get("summary")).get("compositeRate"),
"the coverage check must preserve the published value rather than clamp or recompute it");
var stage=summary(Map.of("black",Map.of("rate",new BigDecimal("1.5"),"totalWeight",60)),List.of(month(2026,9,2,0)));
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(stage,task(List.of("rate"),List.of("black"),false,"available"),
Map.of("year",2026,"month",9))),"a sourced stage rate follows the same formal correction rule");
var noDenominator=summary(Map.of("black",Map.of("rate",new BigDecimal("1.5"),"totalWeight",0)),List.of(month(2026,9,2,0)));
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(noDenominator,task(List.of("rate"),List.of("black"),false,"available"),
Map.of("year",2026,"month",9))));
}
@Test void stageRateAndScrapRateDistinguishTrueZeroFromMissingOrZeroDenominator() {
Map<String,Object> rateTask = task(List.of("rate"), List.of("bright"), false, "available");
Map<String,Object> validZeroRate = summary(Map.of("bright", Map.of("rate", 0, "totalWeight", 4)),
@@ -184,9 +280,14 @@ class AnalysisProtocolMetricCoverageTest {
trend("2026-03", 0.8, 2, 0), trend("2026-05", 0.82, 2, 0)), sources);
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(missingMiddle, task, period)));
Map<String,Object> invalidApril = trendResponse(List.of(
Map<String,Object> validAboveOne = trendResponse(List.of(
trend("2026-03", 0.8, 2, 0), trend("2026-04", 1.2, 2, 0),
trend("2026-05", 0.82, 2, 0)), sources);
assertTrue(complete(AnalysisProtocolMetricCoverage.inspect(validAboveOne, task, period)));
Map<String,Object> invalidApril = trendResponse(List.of(
trend("2026-03", 0.8, 2, 0), trend("2026-04", Double.NaN, 2, 0),
trend("2026-05", 0.82, 2, 0)), sources);
assertFalse(complete(AnalysisProtocolMetricCoverage.inspect(invalidApril, task, period)));
Map<String,Object> unknownSource = month(2026, 4, null, 0);
@@ -6,6 +6,75 @@ import org.junit.jupiter.api.Test;
import tools.jackson.databind.json.JsonMapper;
class AnalysisProtocolQueryPlanTest {
@Test void combinedScrapAndPublishedCompositeRateUseOneSinglePeriodScope() {
var n=node("combined","ranking",List.of("rate","scrapWeight"),List.of(),Map.of("kind","base"));
n.put("dimension","product");n.put("limit",5);n.put("visualPreference","auto");n.put("visualType","bar");
var task=root(List.of(n),List.of("rate","scrapWeight"),List.of("product"),false);task.put("stages",List.of());
task.put("dimensionStages",Map.of("product",List.of()));
task.put("queryPlan",compiler.normalize(task,catalog,List.of()));
var scopes=compiler.resolve(task,"execution","release",catalog,List.of(),List.of()).scopes();
assertEquals(1,scopes.size());var child=AnalysisProtocolService.map(scopes.getFirst().get("task"));
assertEquals(List.of(),child.get("stages"));assertEquals(List.of("rate","scrapWeight"),child.get("fields"));
assertFalse(child.containsKey("comparisonPeriod"));
}
@Test void oneExactDimensionMetricPairRetainsMetricsKindOnlyForSingleAutoBaseRankingNode() {
var ranking=node("combined","ranking",List.of("rate","scrapWeight"),List.of(),Map.of("kind","base"));
ranking.put("dimension","product");ranking.put("limit",1);ranking.put("visualPreference","auto");ranking.put("visualType","bar");
var task=root(List.of(ranking),List.of("rate","scrapWeight"),List.of("product"),false);
task.put("kind","metrics");task.put("visualPreference","auto");task.put("filters",Map.of("product","A"));
task.put("stages",List.of());task.put("queryPlan",compiler.normalize(task,catalog,List.of()));
var scope=compiler.resolve(task,"execution","release",catalog,List.of(),List.of()).scopes().getFirst();
assertEquals("metrics",AnalysisProtocolService.map(scope.get("task")).get("kind"));
assertEquals(1,scope.get("limit"));
var encodedSingle=node("combined","ranking",List.of("rate","scrapWeight"),List.of(),Map.of("kind","base"));
encodedSingle.put("dimension","product");encodedSingle.put("limit",1);encodedSingle.put("visualPreference","auto");
var encodedTask=root(List.of(encodedSingle),List.of("rate","scrapWeight"),List.of("product"),false);
encodedTask.put("kind","metrics");encodedTask.put("visualPreference","auto");encodedTask.put("filters",Map.of("product","~qv1~WyJBIl0"));
encodedTask.put("stages",List.of());encodedTask.put("queryPlan",compiler.normalize(encodedTask,catalog,List.of()));
var encodedChild=AnalysisProtocolService.map(compiler.resolve(encodedTask,"execution","release",catalog,List.of(),List.of()).scopes().getFirst().get("task"));
assertEquals("metrics",encodedChild.get("kind"),"a valid one-value selection wire denotes one exact object");
for(var selected:Map.of("department","汽车","group","国内").entrySet()) {
var scopedRanking=node("combined","ranking",List.of("rate","scrapWeight"),List.of(),Map.of("kind","base"));
scopedRanking.put("dimension",selected.getKey());scopedRanking.put("limit",1);scopedRanking.put("visualPreference","auto");
var scopedTask=root(List.of(scopedRanking),List.of("rate","scrapWeight"),List.of(selected.getKey()),false);
scopedTask.put("kind","metrics");scopedTask.put("visualPreference","auto");scopedTask.put("filters",Map.of(selected.getKey(),selected.getValue()));
scopedTask.put("stages",List.of());scopedTask.put("queryPlan",compiler.normalize(scopedTask,catalog,List.of()));
var scopedChild=AnalysisProtocolService.map(compiler.resolve(scopedTask,"execution","release",catalog,List.of(),List.of()).scopes().getFirst().get("task"));
assertEquals("metrics",scopedChild.get("kind"),"the exact "+selected.getKey()+" scalar scope is retained");
assertEquals(List.of(selected.getKey()),scopedChild.get("dimensions"));
}
var ordinary=root(List.of(ranking),List.of("rate","scrapWeight"),List.of("product"),false);
ordinary.put("kind","analysis");ordinary.put("visualPreference","auto");ordinary.put("filters",Map.of("product","A"));
ordinary.put("stages",List.of());ordinary.put("queryPlan",compiler.normalize(ordinary,catalog,List.of()));
var ordinaryScope=compiler.resolve(ordinary,"execution","release",catalog,List.of(),List.of()).scopes().getFirst();
assertEquals("analysis",AnalysisProtocolService.map(ordinaryScope.get("task")).get("kind"),
"a true ranking request remains a ranking child even when it is Top1");
var moreThanOneNode=node("metric","metrics",List.of("rate"),List.of(),Map.of("kind","base"));
var combinedAndAnotherRead=root(List.of(ranking,moreThanOneNode),List.of("rate","scrapWeight"),List.of("product"),false);
combinedAndAnotherRead.put("kind","metrics");combinedAndAnotherRead.put("visualPreference","auto");combinedAndAnotherRead.put("filters",Map.of("product","A"));
combinedAndAnotherRead.put("stages",List.of());combinedAndAnotherRead.put("queryPlan",compiler.normalize(combinedAndAnotherRead,catalog,List.of()));
var multiScopes=compiler.resolve(combinedAndAnotherRead,"execution","release",catalog,List.of(),List.of()).scopes();
assertEquals("analysis",AnalysisProtocolService.map(multiScopes.getFirst().get("task")).get("kind"),
"the exception is not propagated when the confirmed plan contains another node");
List<Map<String,Object>> nonExactFiltersList=List.of(
new LinkedHashMap<>(Map.<String,Object>of("product",List.of("A","B"))),new LinkedHashMap<>(),
new LinkedHashMap<>(Map.<String,Object>of("product","~qv1~WyJBIiwiQiJd")),
new LinkedHashMap<>(Map.<String,Object>of("product","~qv1~%%%")));
for(Map<String,Object> nonExactFilters:nonExactFiltersList) {
var unbounded=node("combined","ranking",List.of("rate","scrapWeight"),List.of(),Map.of("kind","base"));
unbounded.put("dimension","product");unbounded.put("limit",1);unbounded.put("visualPreference","auto");
var unboundedTask=root(List.of(unbounded),List.of("rate","scrapWeight"),List.of("product"),false);
unboundedTask.put("kind","metrics");unboundedTask.put("visualPreference","auto");unboundedTask.put("filters",nonExactFilters);
unboundedTask.put("stages",List.of());unboundedTask.put("queryPlan",compiler.normalize(unboundedTask,catalog,List.of()));
var unboundedChild=AnalysisProtocolService.map(compiler.resolve(unboundedTask,"execution","release",catalog,List.of(),List.of()).scopes().getFirst().get("task"));
assertEquals("analysis",unboundedChild.get("kind"),"the exception needs one exact selected object");
}
}
private final AnalysisProtocolQueryPlan compiler=new AnalysisProtocolQueryPlan(JsonMapper.builder().build());
private final Map<String,Object> catalog=Map.of("products",List.of("A","B","C"),"departments",List.of("汽车"),"groups",List.of("国内","海外"),"groupDepartments",Map.of("国内","汽车","海外","汽车"));
private Map<String,Object> node(String id,String purpose,List<String> fields,List<String> stages,Map<String,Object> selector){
@@ -4,6 +4,140 @@ import java.math.BigDecimal;
import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolRankArtifactTest {
@Test void actualSingleStageAndZeroScrapSnapshotsPassCoverageAndBuildWithoutMissingDenominatorNpe() {
var task=Map.<String,Object>of("dimensions",List.of("product"),"stages",List.of(),
"fields",List.of("rate","scrapWeight"),"period",Map.of("year",2026,"month",9),
"periodCoverage","available","trendRequired",false,"filters",Map.of("product","redacted-product"),
"dimensionStages",Map.of("product",List.of()));
var zero=Map.<String,Object>of(
"summary",Map.of("key","overall","label","总体","compositeRate",1,
"coverage",Map.of("blackRows",0,"brightRows",3),
"black",Map.of("rate",1),
"bright",Map.of("qualifiedWeight",3.17,"scrapWeight",0.0,"totalWeight",3.17,"rate",1)),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",0,"brightRows",3))),
"context",Map.of("rowCount",1),
"rows",List.of(Map.of("key","redacted-product","label","redacted-product","compositeRate",1,
"coverage",Map.of("blackRows",0,"brightRows",3),"black",Map.of("rate",1),
"bright",Map.of("qualifiedWeight",3.17,"scrapWeight",0.0,"totalWeight",3.17,"rate",1))));
var singleBlack=Map.<String,Object>of(
"summary",Map.of("key","overall","label","总体","compositeRate",0.8646003262642741,
"coverage",Map.of("blackRows",2,"brightRows",0),
"black",Map.of("qualifiedWeight",5.3,"scrapWeight",0.83,"totalWeight",6.13,"rate",0.8646003262642741),
"bright",Map.of("rate",1)),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",2,"brightRows",0))),
"context",Map.of("rowCount",1),
"rows",List.of(Map.of("key","redacted-product","label","redacted-product","compositeRate",0.8646003262642741,
"coverage",Map.of("blackRows",2,"brightRows",0),
"black",Map.of("qualifiedWeight",5.3,"scrapWeight",0.83,"totalWeight",6.13,"rate",0.8646003262642741),
"bright",Map.of("rate",1))));
for(var snapshot:List.of(zero,singleBlack)) {
assertTrue(Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(snapshot,task,Map.of("year",2026,"month",9)).get("metricsComplete")));
assertTrue(AnalysisProtocolRankingCoverage.complete(snapshot,task,"product"));
}
var emptyArtifact=AnalysisProtocolRankArtifact.build(zero,task,"product","release");
var empty=AnalysisProtocolService.map(AnalysisProtocolService.list(emptyArtifact.get("rankings")).getFirst());
assertEquals(BigDecimal.ZERO,empty.get("denominator"));assertEquals(BigDecimal.ONE,empty.get("compositeRate"));
assertEquals("no_positive_scrap",empty.get("emptyReason"));
var rankArtifact=AnalysisProtocolRankArtifact.build(singleBlack,task,"product","release");
var ranking=AnalysisProtocolService.map(AnalysisProtocolService.list(rankArtifact.get("rankings")).getFirst());
var top=AnalysisProtocolService.map(AnalysisProtocolService.list(ranking.get("rows")).getFirst());
assertEquals(new BigDecimal("0.83"),ranking.get("denominator"));
assertEquals(new BigDecimal("0.83"),top.get("weight"));
assertEquals(new BigDecimal("0.8646003262642741"),top.get("compositeRate"));
}
@Test void combinedRankingRetainsOneAuthoritativeOrderForLaterFollowups() {
var combined=Map.<String,Object>of("stages",List.of(),"fields",List.of("rate","scrapWeight"),"period",Map.of("year",2026),"filters",Map.of(),"dimensionStages",Map.of("product",List.of()));
var source=Map.<String,Object>of("summary",Map.of("coverage",Map.of("blackRows",2,"brightRows",2),"black",Map.of("scrapWeight",45),"bright",Map.of("scrapWeight",61)),"rows",List.of(
Map.of("key","a","black",Map.of("scrapWeight",5),"bright",Map.of("scrapWeight",60)),
Map.of("key","b","black",Map.of("scrapWeight",40),"bright",Map.of("scrapWeight",1))));
var artifact=AnalysisProtocolRankArtifact.build(source,combined,"product","release");
var profile=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
assertEquals("overall",profile.get("stage"));assertEquals(new BigDecimal("106"),profile.get("denominator"));
var rows=AnalysisProtocolService.list(profile.get("rows"));assertEquals("a",AnalysisProtocolService.map(rows.getFirst()).get("key"));
assertEquals(new BigDecimal("65"),AnalysisProtocolService.map(rows.getFirst()).get("weight"));
}
@Test void combinedTopArtifactKeepsOfficialRateStageComponentsScaleAndShares() {
var task=Map.<String,Object>of("dimensions",List.of("product"),"stages",List.of(),"fields",List.of("rate","scrapWeight"),"period",Map.of("year",2026,"month",9),
"periodCoverage","complete","trendRequired",false,"filters",Map.of(),"dimensionStages",Map.of("product",List.of()));
var summary=Map.<String,Object>of("coverage",Map.of("blackRows",3,"brightRows",5),"black",Map.of("scrapWeight",45,"totalWeight",400),"bright",Map.of("scrapWeight",61,"totalWeight",600),"compositeRate",new BigDecimal(".91"));
var rows=List.of(
Map.<String,Object>of("key","a","label","A","compositeRate",new BigDecimal(".81"),"coverage",Map.of("blackRows",2,"brightRows",3),
"black",Map.of("scrapWeight",5,"totalWeight",100),"bright",Map.of("scrapWeight",60,"totalWeight",200)),
Map.<String,Object>of("key","b","label","B","compositeRate",new BigDecimal(".92"),"coverage",Map.of("blackRows",1,"brightRows",2),
"black",Map.of("scrapWeight",40,"totalWeight",300),"bright",Map.of("scrapWeight",1,"totalWeight",400)));
var response=Map.<String,Object>of("summary",summary,"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",3,"brightRows",5))),
"context",Map.of("rowCount",rows.size()),"rows",rows);
var artifact=AnalysisProtocolRankArtifact.build(response,task,"product","release");
var ranking=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
var top=AnalysisProtocolService.map(AnalysisProtocolService.list(ranking.get("rows")).getFirst());
assertEquals(2,ranking.get("totalRows"));assertEquals(2,AnalysisProtocolService.list(ranking.get("rows")).size());
assertEquals(new BigDecimal(".81"),top.get("compositeRate"));
assertEquals(new BigDecimal("5"),top.get("blackScrapWeight"));assertEquals(new BigDecimal("60"),top.get("brightScrapWeight"));
assertEquals(new BigDecimal("100"),top.get("blackTotalWeight"));assertEquals(new BigDecimal("200"),top.get("brightTotalWeight"));
assertEquals(new BigDecimal("65"),top.get("weight"));
assertEquals(0,new BigDecimal("65").divide(new BigDecimal("106"),java.math.MathContext.DECIMAL128).compareTo((BigDecimal)top.get("share")));
assertEquals(top.get("share"),top.get("cumulativeShare"));
}
@Test void combinedRankArtifactPreservesIndependentlyVerifiedFormalRateAboveOne() {
BigDecimal independent=new BigDecimal("70").add(new BigDecimal("50"))
.divide(new BigDecimal("60").add(new BigDecimal("40")));
var task=Map.<String,Object>of("dimensions",List.of("product"),"stages",List.of(),"fields",List.of("rate","scrapWeight"),
"period",Map.of("year",2026,"month",9),"periodCoverage","complete","trendRequired",false,
"filters",Map.of(),"dimensionStages",Map.of("product",List.of()));
var stageSummary=Map.<String,Object>of("black",Map.of("scrapWeight",20,"totalWeight",60,"qualifiedWeight",70),
"bright",Map.of("scrapWeight",10,"totalWeight",40,"qualifiedWeight",50),"compositeRate",independent,
"coverage",Map.of("blackRows",2,"brightRows",2));
var row=Map.<String,Object>of("key","anonymized-product","label","产品","compositeRate",independent,
"coverage",Map.of("blackRows",2,"brightRows",2),
"black",Map.of("scrapWeight",20,"totalWeight",60,"qualifiedWeight",70),
"bright",Map.of("scrapWeight",10,"totalWeight",40,"qualifiedWeight",50));
var response=Map.<String,Object>of("summary",stageSummary,
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",2,"brightRows",2))),
"context",Map.of("rowCount",1),"rows",List.of(row));
var artifact=AnalysisProtocolRankArtifact.build(response,task,"product","release");
var ranking=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
var ranked=AnalysisProtocolService.map(AnalysisProtocolService.list(ranking.get("rows")).getFirst());
assertTrue(independent.compareTo(BigDecimal.ONE)>0);
assertEquals(independent,ranking.get("compositeRate"));
assertEquals(independent,ranked.get("compositeRate"),"the official row value is copied without clipping or recomputation");
}
@Test void zeroCombinedScrapIssuesAnExplicitEmptyArtifactAndKeepsOnlyAValidatedOfficialRate() {
var task=Map.<String,Object>of("dimensions",List.of("product"),"stages",List.of(),"fields",List.of("rate","scrapWeight"),
"period",Map.of("year",2026,"month",9),"periodCoverage","complete","trendRequired",false,
"filters",Map.of(),"dimensionStages",Map.of("product",List.of()));
var response=Map.<String,Object>of("summary",Map.of("coverage",Map.of("blackRows",1,"brightRows",1),"compositeRate",new BigDecimal(".95"),
"black",Map.of("scrapWeight",0,"totalWeight",100),"bright",Map.of("scrapWeight",0,"totalWeight",200)),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",1,"brightRows",1))),
"context",Map.of("rowCount",1),"rows",List.of(Map.of("key","a","compositeRate",new BigDecimal(".89"),
"coverage",Map.of("blackRows",1,"brightRows",1),"black",Map.of("scrapWeight",0,"totalWeight",100),
"bright",Map.of("scrapWeight",0,"totalWeight",200))));
var artifact=AnalysisProtocolRankArtifact.build(response,task,"product","release");
var rank=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
assertEquals("overall",rank.get("stage"));assertEquals("scrapWeight",rank.get("metric"));assertTrue((Boolean)rank.get("completeRanking"));
assertEquals("no_positive_scrap",rank.get("emptyReason"));assertEquals(BigDecimal.ZERO,rank.get("denominator"));
assertEquals(0,rank.get("totalRows"));assertEquals(0,rank.get("returnedRows"));assertEquals(false,rank.get("truncated"));
assertEquals(List.of(),rank.get("rows"));assertEquals(new BigDecimal(".95"),rank.get("compositeRate"));
}
@Test void zeroStageScrapIssuesAnExplicitEmptyArtifactOnlyAfterFullCoverage() {
var task=Map.<String,Object>of("dimensions",List.of("product"),"stages",List.of("bright"),"fields",List.of("scrapWeight"),
"period",Map.of("year",2026,"month",9),"periodCoverage","complete","trendRequired",false,
"filters",Map.of(),"dimensionStages",Map.of("product",List.of("bright")));
var response=Map.<String,Object>of("summary",Map.of("coverage",Map.of("brightRows",1),"bright",Map.of("scrapWeight",0)),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"brightRows",1))),
"context",Map.of("rowCount",1),"rows",List.of(Map.of("key","a","coverage",Map.of("brightRows",1),
"bright",Map.of("scrapWeight",0))));
var artifact=AnalysisProtocolRankArtifact.build(response,task,"product","release");
var rank=AnalysisProtocolService.map(AnalysisProtocolService.list(artifact.get("rankings")).getFirst());
assertEquals("bright",rank.get("stage"));assertEquals("scrapWeight",rank.get("metric"));assertTrue((Boolean)rank.get("completeRanking"));
assertEquals("no_positive_scrap",rank.get("emptyReason"));assertEquals(BigDecimal.ZERO,rank.get("denominator"));
assertEquals(0,rank.get("totalRows"));assertEquals(0,rank.get("returnedRows"));assertEquals(false,rank.get("truncated"));
assertEquals(List.of(),rank.get("rows"));assertEquals("weight_desc_key_codepoint_asc",rank.get("orderPolicy"));
var incomplete=new LinkedHashMap<>(response);incomplete.put("context",Map.of("rowCount",2));
assertTrue(AnalysisProtocolRankArtifact.build(incomplete,task,"product","release").isEmpty());
}
private final Map<String,Object> task=Map.of("stages",List.of("black"),"fields",List.of("totalWeight"),"period",Map.of("year",2026),"filters",Map.of("extraFilters",Map.of("customerCode",List.of("800001"))));
private Map<String,Object> row(String key,Number weight){return Map.of("key",key,"black",Map.of("totalWeight",weight));}
private Map<String,Object> data(Number total,List<?> rows){return Map.of("summary",Map.of("black",Map.of("totalWeight",total)),"context",Map.of("rowCount",rows.size()),"rows",rows);}
@@ -5,6 +5,88 @@ import java.util.*;
import org.junit.jupiter.api.Test;
class AnalysisProtocolRankingCoverageTest {
@Test void combinedRankingAcceptsTheActualMissingZeroSourceStageSnapshots() {
var task=Map.<String,Object>of("stages",List.of(),"fields",List.of("rate","scrapWeight"),
"dimensionStages",Map.of("product",List.of()));
var zeroBrightOnly=Map.<String,Object>of(
"summary",Map.of("black",Map.of("rate",1),"bright",Map.of("scrapWeight",0),
"coverage",Map.of("blackRows",0,"brightRows",3)),
"context",Map.of("rowCount",1),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",0,"brightRows",3))),
"rows",List.of(Map.of("key","product-zero","black",Map.of("rate",1),"bright",Map.of("scrapWeight",0),
"coverage",Map.of("blackRows",0,"brightRows",3))));
var singleBlack=Map.<String,Object>of(
"summary",Map.of("black",Map.of("scrapWeight",0.83),"bright",Map.of("rate",1),
"coverage",Map.of("blackRows",2,"brightRows",0)),
"context",Map.of("rowCount",1),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",2,"brightRows",0))),
"rows",List.of(Map.of("key","product-black","black",Map.of("scrapWeight",0.83),"bright",Map.of("rate",1),
"coverage",Map.of("blackRows",2,"brightRows",0))));
assertTrue(AnalysisProtocolRankingCoverage.complete(zeroBrightOnly,task,"product"));
assertTrue(AnalysisProtocolRankingCoverage.complete(singleBlack,task,"product"));
}
@Test void combinedRankingRejectsMissingStageWeightWithoutConsistentZeroSourceProof() {
var task=Map.<String,Object>of("stages",List.of(),"fields",List.of("rate","scrapWeight"),
"dimensionStages",Map.of("product",List.of()));
var noMonthlyProof=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",2),
"bright",Map.of("rate",1),"coverage",Map.of("blackRows",2,"brightRows",0)),
"context",Map.of("rowCount",1),"coverage",Map.of("sourceMonths",List.of()),
"rows",List.of(Map.of("key","product","black",Map.of("scrapWeight",2),"bright",Map.of("rate",1),
"coverage",Map.of("blackRows",2,"brightRows",0))));
var contradictory=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",2),
"bright",Map.of("scrapWeight",5),"coverage",Map.of("blackRows",2,"brightRows",0)),
"context",Map.of("rowCount",1),
"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",9,"blackRows",2,"brightRows",0))),
"rows",List.of(Map.of("key","product","black",Map.of("scrapWeight",2),"bright",Map.of("scrapWeight",5),
"coverage",Map.of("blackRows",2,"brightRows",0))));
assertFalse(AnalysisProtocolRankingCoverage.complete(noMonthlyProof,task,"product"));
assertFalse(AnalysisProtocolRankingCoverage.complete(contradictory,task,"product"));
}
@Test void combinedScrapMustReconcileBothFullStagesBeforeTopSelection() {
var task=Map.<String,Object>of("stages",List.of(),"fields",List.of("rate","scrapWeight"),"dimensionStages",Map.of("product",List.of()));
var data=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",40),"bright",Map.of("scrapWeight",60)),
"rows",List.of(Map.of("key","a","black",Map.of("scrapWeight",40),"bright",Map.of("scrapWeight",60))));
assertTrue(AnalysisProtocolRankingCoverage.complete(data,task,"product"));
var broken=new LinkedHashMap<>(data);broken.put("summary",Map.of("black",Map.of("scrapWeight",40),"bright",Map.of("scrapWeight",61)));
assertFalse(AnalysisProtocolRankingCoverage.complete(broken,task,"product"));
var zero=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",60)),
"rows",List.of(Map.of("key","a","black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",60))));
assertTrue(AnalysisProtocolRankingCoverage.complete(zero,task,"product"));
}
@Test void combinedZeroScrapNeedsCompleteKnownGroupingAndAtLeastOneRealStageSource() {
var task=Map.<String,Object>of("stages",List.of(),"fields",List.of("rate","scrapWeight"),"dimensionStages",Map.of("product",List.of()));
var data=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0),
"coverage",Map.of("blackRows",2,"brightRows",1)),
"context",Map.of("rowCount",2),"rows",List.of(
Map.of("key","a","black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",2,"brightRows",0)),
Map.of("key","b","black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",0,"brightRows",1))));
assertTrue(AnalysisProtocolRankingCoverage.complete(data,task,"product"));
var noSource=new LinkedHashMap<>(data);noSource.put("summary",Map.of("black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",0,"brightRows",0)));
assertFalse(AnalysisProtocolRankingCoverage.complete(noSource,task,"product"),"empty formal values alone do not prove an observed zero");
var unknown=new LinkedHashMap<>(data);unknown.put("summary",Map.of("black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0)));
assertFalse(AnalysisProtocolRankingCoverage.complete(unknown,task,"product"),"missing source coverage cannot be inferred as zero");
var missingGroupValue=new LinkedHashMap<>(data);missingGroupValue.put("rows",List.of(Map.of("key","a","black",Map.of("scrapWeight",0),"bright",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",2,"brightRows",0)),
Map.of("key","b","black",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",0,"brightRows",1))));
assertFalse(AnalysisProtocolRankingCoverage.complete(missingGroupValue,task,"product"),"each sourced group needs a known formal value");
}
@Test void singleStageZeroScrapNeedsThatStagesOwnObservedRows() {
var task=Map.<String,Object>of("stages",List.of("black"),"fields",List.of("scrapWeight"));
var data=Map.<String,Object>of("summary",Map.of("black",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",2,"brightRows",0)),
"context",Map.of("rowCount",1),"rows",List.of(Map.of("key","a","black",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",2,"brightRows",0))));
assertTrue(AnalysisProtocolRankingCoverage.complete(data,task,"product"));
var noSource=new LinkedHashMap<>(data);noSource.put("summary",Map.of("black",Map.of("scrapWeight",0),"coverage",Map.of("blackRows",0,"brightRows",0)));
assertFalse(AnalysisProtocolRankingCoverage.complete(noSource,task,"product"));
var unknown=new LinkedHashMap<>(data);unknown.put("summary",Map.of("black",Map.of("scrapWeight",0)));
assertFalse(AnalysisProtocolRankingCoverage.complete(unknown,task,"product"));
var negative=new LinkedHashMap<>(data);negative.put("rows",List.of(Map.of("key","a","black",Map.of("scrapWeight",-1),"coverage",Map.of("blackRows",2,"brightRows",0))));
assertFalse(AnalysisProtocolRankingCoverage.complete(negative,task,"product"));
assertFalse(AnalysisProtocolRankingCoverage.complete(response(0,List.of(row("a",0))),Map.of("stages",List.of("black"),"fields",List.of("totalWeight")),"product"),
"the zero exception is limited to scrapWeight and cannot bypass totalWeight validation");
}
private final Map<String,Object> task=Map.of("stages",List.of("black"),"fields",List.of("totalWeight"));
private Map<String,Object> response(Object total,List<?> rows) {
return Map.of("summary",Map.of("black",Map.of("totalWeight",total)),"rows",rows);
@@ -426,22 +426,46 @@ class AnalysisProtocolServiceMySqlTest {
@Test void firstHistoryDoesNotFallForwardWhenTheSelectedProofIsNoLongerValid() {
enableAutonomous();String first=completedHistoryMetric(null,8,"35.055");String second=completedHistoryMetric(null,9,"12.345");var bytes=jdbc.queryForMap("SELECT q.query_id,q.response_snapshot FROM analysis_protocol_query q JOIN analysis_protocol_attempt a ON a.attempt_id=q.attempt_id WHERE a.turn_id=?",first);byte[] damaged=((byte[])bytes.get("response_snapshot")).clone();damaged[damaged.length/2]^=1;jdbc.update("UPDATE analysis_protocol_query SET response_snapshot=? WHERE query_id=?",damaged,bytes.get("query_id"));
String report=newTurn("第一轮的报告");var a=claim(report,false);var task=historyTask(a,report,"first");assertEquals(List.of(first),task.get("historySourceTurnIds"));var snapshot=tx.execute(s->service.history(a.attemptId(),actor));assertEquals(false,AnalysisProtocolService.map(snapshot.get("historyCoverage")).get("complete"));assertEquals(List.of(),snapshot.get("facts"));var sources=AnalysisProtocolService.list(snapshot.get("historySources"));assertEquals(1,sources.size());assertEquals(first,AnalysisProtocolService.map(sources.getFirst()).get("turnId"));assertEquals(false,AnalysisProtocolService.map(sources.getFirst()).get("available"));assertFalse(snapshot.toString().contains(second));assertFalse(snapshot.toString().contains("12.345"));
String allReport=newTurn("整理全部正式历史来源");var b=claim(allReport,false);var allTask=historyTask(b,allReport,"all");assertEquals(List.of(first,second),allTask.get("historySourceTurnIds"));var allSnapshot=tx.execute(s->service.history(b.attemptId(),actor));assertEquals(false,AnalysisProtocolService.map(allSnapshot.get("historyCoverage")).get("complete"));assertEquals(2,AnalysisProtocolService.map(allSnapshot.get("historyCoverage")).get("expectedSources"));assertEquals(first,AnalysisProtocolService.map(AnalysisProtocolService.list(allSnapshot.get("historySources")).getFirst()).get("turnId"));assertEquals(false,AnalysisProtocolService.map(AnalysisProtocolService.list(allSnapshot.get("historySources")).getFirst()).get("available"));assertTrue(AnalysisProtocolService.map(AnalysisProtocolService.list(allSnapshot.get("historySources")).getLast()).get("available").equals(true));
}
@Test void aLegacyStaticSessionCannotRequestFirstHistoryMode() {
completedHistoryMetric(null,9,"35.055");String report=newTurn("旧会话不开放first");var a=claim(report,false);assertThrows(RuntimeException.class,()->tx.execute(s->service.confirm(a.attemptId(),historyProposal(report,"first"),actor)));assertEquals(0,jdbc.queryForObject("SELECT COUNT(*) FROM analysis_protocol_history_import",Integer.class));var task=historyTask(a,report,"all");assertEquals(1,AnalysisProtocolService.list(task.get("historySourceTurnIds")).size());
}
@Test void historyLastDoesNotFallBackFromAnUnavailableLastBusinessTurnAndAllCannotNarrowSources() {
String good=completedHistoryMetric(null,9,"35.055");
String failed=newTurn("另一范围尚未形成结果");var old=claim(failed,false);tx.executeWithoutResult(s->service.fail(old.attemptId(),"测试中断"));
String turn=newTurn("整理最后一轮报告");var a=claim(turn,false);var task=historyTask(a,turn,"last");
assertEquals(List.of(failed),task.get("historySourceTurnIds"));
@Test void historyAllAndLastIgnoreFailedBusinessTurnsButCannotNarrowCompletedSources() {
String first=completedHistoryMetric(null,9,"35.055");
String failed=recordedHistoryMetric(session,null,8,"12.345","FAILED");
String latest=completedHistoryMetric(null,10,"24.680");
String turn=newTurn("整理最后一轮完成报告");var a=claim(turn,false);var task=historyTask(a,turn,"last");
assertEquals(List.of(latest),task.get("historySourceTurnIds"));
var snapshot=tx.execute(s->service.history(a.attemptId(),actor));
assertEquals(false,AnalysisProtocolService.map(snapshot.get("historyCoverage")).get("complete"));
assertEquals(List.of(),snapshot.get("facts"));finishHistory(a,historyDetails(task,snapshot));assertEquals("PARTIAL",turnStatus(turn));
String all=newTurn("整理所有报告");var b=claim(all,false);var proposal=historyProposal(all,"all");proposal.put("historySourceTurnIds",List.of(good));
assertEquals(true,AnalysisProtocolService.map(snapshot.get("historyCoverage")).get("complete"));
assertEquals(latest,AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("historySources")).getFirst()).get("turnId"));
finishHistory(a,historyDetails(task,snapshot));assertEquals("COMPLETED",turnStatus(turn));
String all=newTurn("整理所有已完成报告");var b=claim(all,false);var proposal=historyProposal(all,"all");proposal.put("historySourceTurnIds",List.of(first));
assertThrows(RuntimeException.class,()->tx.execute(s->service.confirm(b.attemptId(),proposal,actor)));
var allTask=historyTask(b,all,"all");assertEquals(List.of(first,latest),allTask.get("historySourceTurnIds"));
assertFalse(AnalysisProtocolService.list(tx.execute(s->service.history(b.attemptId(),actor)).get("historySources")).stream()
.map(AnalysisProtocolService::map).anyMatch(source->failed.equals(source.get("turnId"))));
}
@Test void historyAllSkipsTasklessFailedReportAndFailedBusinessTurnAndRejectsExplicitSubstitution() {
enableAutonomous();String good=completedHistoryMetric(null,9,"35.055");
String failedReport=newTurn("上次历史报告规划失败");var planning=claim(failedReport,false);
tx.executeWithoutResult(s->service.fail(planning.attemptId(),"规划阶段未形成已确认任务"));
String failedBusiness=recordedHistoryMetric(session,null,8,"12.345","FAILED");
String turn=newTurn("整理全部已确认历史来源");var a=claim(turn,false);var task=historyTask(a,turn,"all");
assertEquals(List.of(good),task.get("historySourceTurnIds"));
var snapshot=tx.execute(s->service.history(a.attemptId(),actor));var coverage=AnalysisProtocolService.map(snapshot.get("historyCoverage"));
assertEquals(1,coverage.get("expectedSources"));assertEquals(1,coverage.get("importedSources"));assertEquals(true,coverage.get("complete"));
for(String unavailable:List.of(failedReport,failedBusiness)) {
String forged=newTurn("用失败轮次替换历史来源");var b=claim(forged,false);var proposal=historyProposal(forged,"all");
proposal.put("historySourceTurnIds",List.of(good,unavailable));
assertThrows(RuntimeException.class,()->tx.execute(s->service.confirm(b.attemptId(),proposal,actor)));
}
}
@Test void clarificationIsNotAFactualHistorySourceForAllOrLast() {
@@ -586,7 +610,25 @@ class AnalysisProtocolServiceMySqlTest {
var offset=new LinkedHashMap<>(query);offset.put("rowOffset","1");
assertThrows(RuntimeException.class,()->service.checkTarget(attempt.attemptId(),"GET","/api/metrics",offset,actor));
var proof=tx.execute(s->service.recordQuery(attempt.attemptId(),query,Map.of("context",Map.of("rowCount",2),"summary",Map.of("black",Map.of("totalWeight",30)),"rows",List.of(Map.of("key","a","black",Map.of("totalWeight",20)),Map.of("key","b","black",Map.of("totalWeight",10)))),actor));
finish(attempt,confirmed,proof);assertEquals("COMPLETED",turnStatus(turn));
Map<String,Object> artifact=json.readValue(jdbc.queryForObject("SELECT rank_artifact_json FROM analysis_protocol_query WHERE query_id=?",String.class,proof.get("queryId")),Map.class);
Map<String,Object> profile=AnalysisProtocolService.list(artifact.get("rankings")).stream().map(AnalysisProtocolService::map)
.filter(row->Objects.equals("black",row.get("stage"))&&Objects.equals("totalWeight",row.get("metric"))).findFirst().orElseThrow();
List<Map<String,Object>> rankedRows=AnalysisProtocolService.list(profile.get("rows")).stream().map(AnalysisProtocolService::map).toList();
List<Map<String,Object>> categories=rankedRows.stream().map(row->Map.<String,Object>of("key",row.get("key"),"label",row.get("label"))).toList();
List<Object> values=rankedRows.stream().map(row->row.get("weight")).toList();
Map<String,Object> chart=new LinkedHashMap<>();chart.put("dimension","product");chart.put("stage","black");chart.put("metric","black_total_weight");chart.put("type","bar");
var chartScope=new LinkedHashMap<>(AnalysisProtocolService.map(confirmed.get("filters")));chartScope.put("releaseId",release);chartScope.putAll(AnalysisProtocolService.map(confirmed.get("period")));
chart.put("id","scoped-ranking-chart");chart.put("title","黑片投入重量排名");chart.put("subtitle","来自正式分组结果");chart.put("unit","kg");chart.put("scope",chartScope);
chart.put("period",confirmed.get("period"));chart.put("evidenceRefs",List.of("QABCDEF12"));chart.put("categories",categories);
chart.put("requiredFields",List.of("black_total_weight"));chart.put("originTurnId",attempt.turnId());
chart.put("series",List.of(Map.of("key","black_total_weight","label","黑片投入重量","values",values)));
chart.put("coverage",Map.of("shareBasis","all_scope_weight","totalRows",profile.get("totalRows"),"returnedRows",rankedRows.size(),
"truncated",rankedRows.size()<((Number)profile.get("totalRows")).intValue(),"denominator",profile.get("denominator")));
var result=answer();result.put("visualState","ready");result.put("visuals",List.of(chart));
var details=new LinkedHashMap<String,Object>();details.put("agentTask",confirmed);details.put("protocolEvidence",List.of(proof));
details.put("queryEvidenceBindings",List.of(Map.of("evidenceRef","QABCDEF12","queryIds",List.of(proof.get("queryId")))));
details.put("evidenceCompletion",Map.of("serverCoverageSufficient",true));
tx.executeWithoutResult(s->service.finish(attempt.attemptId(),"COMPLETED",result,details,actor));assertEquals("COMPLETED",turnStatus(turn));
}
@Test void aTruncatedRankingOrAnUnqueriedDimensionCannotBeMarkedComplete() {
@@ -749,6 +791,24 @@ class AnalysisProtocolServiceMySqlTest {
assertEquals(first.get("revision"),second.get("revision"));assertNotEquals(first.get("executionId"),second.get("executionId"));
}
@Test void completionWithoutFormalEvidenceFailsClosedWithSafeClassifiedReason() {
String turn=newTurn("当前范围正式数据是否可用");var attempt=claim(turn,false);
var accepted=tx.execute(s->service.confirm(attempt.attemptId(),task(turn,Map.of(),false),actor));
var confirmed=AnalysisProtocolService.map(accepted.get("task"));
var details=Map.<String,Object>of("agentTask",confirmed,"protocolEvidence",List.of(),
"failureDiagnostics",List.of(Map.of("failureKind","source_coverage","errorType","SourceCoverageUnavailable")));
var invented=Map.<String,Object>of("answer","伪造业务结论 secret=hidden","results",List.of(Map.of("rate",0.99)));
tx.executeWithoutResult(s->service.finish(attempt.attemptId(),"COMPLETED",invented,details,actor));
assertEquals("FAILED",turnStatus(turn));
var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);
assertEquals("当前已确认范围没有可用的正式来源数据,本轮未提交业务结论。",saved.get("answer"));
assertEquals(List.of(),saved.get("results"));
assertFalse(saved.toString().contains("伪造业务结论"));
assertFalse(saved.toString().contains("hidden"));
}
@Test void memberCompletionRequiresAllServerRecordedPagesWithTheConfirmedProjection() {
var source=mock(cn.yunsheng.quality.filters.SourceColumnQueries.class);
when(source.catalog(release,actor)).thenReturn(List.of(Map.of("key","src_orders","label","委托单号","stages",List.of("black","bright"))));
@@ -1123,11 +1183,29 @@ class AnalysisProtocolServiceMySqlTest {
var result=answer();result.put("stopReason","evidence_incomplete");result.put("readingSummary",Map.of("version",1,"lead","当前只解释已核实部分","evidence",List.of(),"notices",List.of("部分解释仍待补充"),"evidenceRefs",List.of()));
tx.executeWithoutResult(s->service.finish(a.attemptId(),explicitRootGap?"COMPLETED":"PARTIAL",result,details,actor));assertEquals("PARTIAL",turnStatus(turn));
var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertEquals(List.of(rootProof.get("queryId")),saved.get("protocolQueryIds"));assertEquals(Set.of(badProof.get("queryId"),rootProof.get("queryId")),new HashSet<>(AnalysisProtocolService.list(saved.get("attemptProtocolQueryIds"))));assertFalse(AnalysisProtocolService.list(saved.get("explorationWarnings")).isEmpty());
String report=newTurn("汇总刚才已核实的部分");var reportAttempt=claim(report,false);var reportTask=historyTask(reportAttempt,report,"last");var snapshot=tx.execute(s->service.history(reportAttempt.attemptId(),actor));
var coverage=AnalysisProtocolService.map(snapshot.get("historyCoverage"));assertEquals(false,coverage.get("complete"));assertEquals(1,coverage.get("importedSources"));assertFalse(AnalysisProtocolService.list(coverage.get("missingEvidence")).isEmpty());if(explicitRootGap)assertTrue(coverage.get("missingEvidence").toString().contains("defect/bright"));
String report=newTurn("汇总刚才已核实的部分");var reportAttempt=claim(report,false);var reportProposal=historyProposal(report,"last");reportProposal.put("historyResultMode","client_unverified");var reportTask=AnalysisProtocolService.map(tx.execute(s->service.confirm(reportAttempt.attemptId(),reportProposal,actor)).get("task"));assertEquals("verified_results_only",reportTask.get("historyResultMode"));var snapshot=tx.execute(s->service.history(reportAttempt.attemptId(),actor));
var coverage=AnalysisProtocolService.map(snapshot.get("historyCoverage"));assertEquals(!explicitRootGap,coverage.get("complete"));assertEquals(1,coverage.get("importedSources"));
if(explicitRootGap)assertTrue(AnalysisProtocolService.list(coverage.get("missingEvidence")).stream().anyMatch(gap->gap.toString().contains("defect/bright")));
else {assertTrue(AnalysisProtocolService.list(coverage.get("missingEvidence")).isEmpty());assertFalse(AnalysisProtocolService.list(snapshot.get("sourceDeliveryWarnings")).isEmpty());}
var source=AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("historySources")).getFirst());assertEquals(true,source.get("available"));assertEquals("PARTIAL",source.get("status"));assertEquals(List.of(rootProof.get("queryId")),source.get("queryIds"));assertFalse(AnalysisProtocolService.list(snapshot.get("facts")).isEmpty());assertFalse(AnalysisProtocolService.list(snapshot.get("sections")).isEmpty());assertFalse(source.get("queryIds").toString().contains(badProof.get("queryId").toString()));
var forged=new LinkedHashMap<>(historyDetails(reportTask,snapshot));forged.put("historyHeadlineFactIds",List.of(badProof.get("queryId")));assertThrows(RuntimeException.class,()->finishHistory(reportAttempt,forged));
finishHistory(reportAttempt,historyDetails(reportTask,snapshot));assertEquals("PARTIAL",turnStatus(report));
var reportDetails=new LinkedHashMap<>(historyDetails(reportTask,snapshot));
if(!explicitRootGap) {
String selectedFact=Objects.toString(AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("facts")).getFirst()).get("id"));
String grounded="已核验的正式指标按其原期间和筛选范围整理。["+selectedFact+"]";
reportDetails.put("answer",grounded);reportDetails.put("readingSummary",Map.of("version",1,"lead",grounded,"evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of(selectedFact)));
reportDetails.put("titleValid",true);reportDetails.put("titleRequired",true);reportDetails.put("titleSource","model_verified_content");
}
var forged=new LinkedHashMap<>(reportDetails);forged.put("historyHeadlineFactIds",List.of(badProof.get("queryId")));assertThrows(RuntimeException.class,()->finishHistory(reportAttempt,forged));
finishHistory(reportAttempt,reportDetails);var savedReport=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,report),Map.class);
assertEquals(explicitRootGap?"PARTIAL":"COMPLETED",turnStatus(report),()->"historyCoverage="+savedReport.get("historyCoverage")+"; evidenceCompletion="+savedReport.get("evidenceCompletion")+"; readingSummary="+savedReport.get("readingSummary"));assertEquals("PARTIAL",turnStatus(turn));
if(!explicitRootGap) {
var reportCoverage=AnalysisProtocolService.map(savedReport.get("historyCoverage"));
assertEquals(snapshot.get("sourceDeliveryWarnings"),reportCoverage.get("sourceDeliveryWarnings"));
var notices=AnalysisProtocolService.list(AnalysisProtocolService.map(savedReport.get("readingSummary")).get("notices"));
assertEquals(1,Collections.frequency(notices,"本报告沿用已核验的正式统计结果,未核验细项不纳入判断。"));
assertFalse(notices.toString().contains("PARTIAL"));assertFalse(notices.toString().contains("解释答复格式未通过有限修复"));
assertEquals("PARTIAL",turnStatus(turn));
}
}
}
@Test void conclusionCannotCiteEarlierIncompleteQueryAfterSameScopeHasSucceeded() {
@@ -1140,12 +1218,14 @@ class AnalysisProtocolServiceMySqlTest {
String turn=newTurn("图表策略"+preference+withSummary);var a=claim(turn,false);var proposal=nativeTask(turn,Map.of());proposal.put("visualPreference",preference);var task=AnalysisProtocolService.map(tx.execute(s->service.confirm(a.attemptId(),proposal,actor)).get("task"));
var root=step(a,"root",Map.of("kind","base"),List.of());var response=Map.<String,Object>of("summary",Map.of("compositeRate",0.9),"coverage",Map.of("sourceMonths",List.of(Map.of("year",2026,"month",1,"blackRows",1,"brightRows",1))),"rows",List.of(),"trend",List.of());var rootProof=tx.execute(s->service.recordQuery(a.attemptId(),"GET","/api/metrics",scopeParams(AnalysisProtocolService.map(root.get("scope"))),response,actor,"root",root.get("scopeId").toString()));
var node=Map.<String,Object>of("nodeId","rank","purpose","ranking","dimension","product","fields",List.of("totalWeight"),"stages",List.of("black"),"visualPreference","auto","selector",Map.of("kind","base"));var rank=tx.execute(s->service.explorationStep(a.attemptId(),Map.of("requestKey","rank","node",node),actor));var rankProof=tx.execute(s->service.recordQuery(a.attemptId(),"GET","/api/metrics",scopeParams(AnalysisProtocolService.map(rank.get("scope"))),rankResponse(),actor,"rank",rank.get("scopeId").toString()));
var result=answer();String fullAnswer="已核实的业务解释。\n\n## 完整附录\n自动图表未生成,详细范围保留在此。";result.put("answer",fullAnswer);result.put("reportMarkdown",fullAnswer);result.put("visualState","failed");result.put("visuals",List.of());if(withSummary)result.put("readingSummary",Map.of("version",1,"lead","业务概括","evidence",List.of("核心业务依据"),"notices",List.of("Python已归并的业务局限"),"evidenceRefs",List.of()));tx.executeWithoutResult(s->service.finish(a.attemptId(),"COMPLETED",result,Map.of("agentTask",task,"protocolEvidence",List.of(rootProof,rankProof),"conclusionProtocolQueryIds",List.of(rootProof.get("queryId"),rankProof.get("queryId")),"evidenceCompletion",Map.of("serverCoverageSufficient",true)),actor));assertEquals(preference.equals("auto")?"COMPLETED":"PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertEquals(true,AnalysisProtocolService.map(saved.get("factCoverage")).get("complete"));assertFalse(AnalysisProtocolService.list(saved.get("explorationWarnings")).isEmpty());assertEquals(fullAnswer,saved.get("reportMarkdown"));
var result=answer();String fullAnswer="已核实的业务解释。\n\n## 完整附录\n自动图表未生成,详细范围保留在此。";result.put("answer",fullAnswer);result.put("reportMarkdown",fullAnswer);result.put("visualState",preference.equals("required")?"ready":"failed");result.put("visuals",List.of());
if(preference.equals("required"))result.put("visuals",List.of(Map.of("metric","composite_rate","type","bar","period",Map.of("year",2026),"scope",Map.of("releaseId",release))));
if(withSummary)result.put("readingSummary",Map.of("version",1,"lead","业务概括","evidence",List.of("核心业务依据"),"notices",List.of("Python已归并的业务局限"),"evidenceRefs",List.of()));tx.executeWithoutResult(s->service.finish(a.attemptId(),"COMPLETED",result,Map.of("agentTask",task,"protocolEvidence",List.of(rootProof,rankProof),"conclusionProtocolQueryIds",List.of(rootProof.get("queryId"),rankProof.get("queryId")),"evidenceCompletion",Map.of("serverCoverageSufficient",true)),actor));assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertEquals(true,AnalysisProtocolService.map(saved.get("factCoverage")).get("complete"));assertFalse(AnalysisProtocolService.list(saved.get("explorationWarnings")).isEmpty());assertEquals(fullAnswer,saved.get("reportMarkdown"));
if(withSummary) {
var notices=AnalysisProtocolService.list(AnalysisProtocolService.map(saved.get("readingSummary")).get("notices"));assertTrue(notices.contains("Python已归并的业务局限"));
if(preference.equals("auto")){assertEquals(List.of("Python已归并的业务局限"),notices);assertEquals(fullAnswer,saved.get("answer"));}
if(preference.equals("auto")){assertTrue(notices.contains("Python已归并的业务局限"));assertTrue(notices.stream().anyMatch(notice->notice.toString().contains("图表尚未完整生成")));assertTrue(saved.get("answer").toString().contains("图表尚未完整生成"));}
else {assertTrue(notices.contains("用户要求的图表尚未完整生成"));assertTrue(saved.get("answer").toString().contains("用户要求的图表尚未完整生成"));assertTrue(notices.stream().noneMatch(n->n.toString().contains("可独立恢复图表")));}
} else {assertFalse(saved.containsKey("readingSummary"));if(preference.equals("auto"))assertEquals(fullAnswer,saved.get("answer"));}
} else {assertFalse(saved.containsKey("readingSummary"));if(preference.equals("auto"))assertTrue(saved.get("answer").toString().contains("图表尚未完整生成"));}
}
}
@Test void nativeRecordsAppendixRetainsLegalPagesAtReadCapWithoutAcceptingIncompleteScope() {
@@ -1204,11 +1284,36 @@ class AnalysisProtocolServiceMySqlTest {
tx.execute(s->service.recordQuery(a.attemptId(),"GET","/api/metrics",p,response,actor,"root",registered.get("scopeId").toString()));
var snapshot=tx.execute(s->service.queries(a.attemptId(),actor));assertEquals(1,AnalysisProtocolService.list(snapshot.get("scopes")).size());assertTrue(AnalysisProtocolService.list(snapshot.get("gaps")).isEmpty());assertEquals(true,snapshot.get("factsComplete"));
}
@Test void autonomousHistoryKeepsVerifiedExplanationSummaryAndServerAppendixWithRealFactAnchors() {
@Test void autonomousHistoryKeepsVerifiedExplanationWithoutRenderingInternalFactAppendix() {
enableAutonomous();completedHistoryMetric(null,9,"35.055");String turn=newTurn("解释历史结果并生成报告");var a=claim(turn,false);var task=historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));
String ref=AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("facts")).getFirst()).get("id").toString();var details=new LinkedHashMap<>(historyDetails(task,snapshot));details.put("reportTitle","历史质量分析汇总报告");details.put("titleSource","verified_history_summary");details.put("titleGeneratedByModel",false);details.put("answer","报废规模需要结合当前范围理解["+ref+"]");details.put("readingSummary",Map.of("version",1,"lead","报废规模观察["+ref+"]","evidence",List.of("对应正式历史值["+ref+"]"),"notices",List.of(),"evidenceRefs",List.of(ref)));
finishHistory(a,details);assertEquals("COMPLETED",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);
assertEquals(details.get("answer"),saved.get("answer"));assertEquals(List.of(ref),saved.get("queryEvidenceRefs"));assertEquals(details.get("readingSummary"),saved.get("readingSummary"));String html=saved.get("reportHtml").toString();assertTrue(html.contains("<table"));assertTrue(html.contains("id=\"history-fact:"+ref+"\""));assertTrue(html.contains("35.055"));assertFalse(AnalysisProtocolService.list(saved.get("historySections")).isEmpty());assertEquals(false,saved.get("titleGeneratedByModel"));service.authorizeReportSnapshot(turn,actor);
assertEquals("报废规模需要结合当前范围理解",saved.get("answer"));assertEquals(List.of(ref),saved.get("queryEvidenceRefs"));var normalized=AnalysisProtocolService.map(saved.get("readingSummary"));assertEquals("报废规模观察",normalized.get("lead"));assertEquals(List.of("对应正式历史值"),normalized.get("evidence"));String html=saved.get("reportHtml").toString();String markdown=saved.get("reportMarkdown").toString();assertTrue(html.contains("<table"));assertTrue(html.contains("35.055"));assertFalse(html.contains("history-facts"));assertFalse(html.contains("data-history-fact-id"));assertFalse(html.contains(ref));assertFalse(markdown.contains(ref));assertFalse(saved.get("answer").toString().contains("turn:"));assertFalse(normalized.toString().contains("turn:"));assertFalse(AnalysisProtocolService.list(saved.get("historySections")).isEmpty());assertEquals(false,saved.get("titleGeneratedByModel"));service.authorizeReportSnapshot(turn,actor);
}
@Test void autonomousHistoryMissingTitleKeepsVerifiedBodyAndMarksOnlyTitleGap() {
enableAutonomous();completedHistoryMetric(null,9,"35.055");String turn=newTurn("报告标题缺失但正文已过审");var a=claim(turn,false);var task=historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));
String ref=AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("facts")).getFirst()).get("id").toString();var details=new LinkedHashMap<>(historyDetails(task,snapshot));
details.put("reportTitle","历史质量分析汇总报告");details.put("titleGeneratedByModel",false);details.put("titleSource","safe_partial_fallback");
details.put("answer","已核验正文保留正式报废重量35.055 kg。["+ref+"]");details.put("readingSummary",Map.of("version",1,"lead","已核验正文保留正式报废重量35.055 kg.["+ref+"]","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of(ref)));
details.put("evidenceCompletion",Map.of("modelDeclaredEvidenceSufficient",true,"bodyEvidenceSufficient",true));
finishHistory(a,details);assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);
String verifiedBody="已核验正文保留正式报废重量35.055 kg。";
String titleGap="尚未解决:报告标题尚未生成或未通过内容核验,报告保持部分完成。";
String answerText=Objects.toString(saved.get("answer"),"");
assertEquals(verifiedBody+"\n\n"+titleGap,answerText);
assertEquals(answerText.indexOf(titleGap),answerText.lastIndexOf(titleGap));
assertTrue(saved.get("reportMarkdown").toString().contains("35.055"));
assertFalse(saved.get("answer").toString().contains(ref));assertFalse(saved.get("answer").toString().contains("正文解释未完成事实核验"));assertFalse(saved.get("reportMarkdown").toString().contains("分组键"));
var summary=AnalysisProtocolService.map(saved.get("readingSummary"));assertTrue(summary.get("lead").toString().contains("已核验正文保留"));assertFalse(summary.toString().contains("正文解释未完成事实核验"));
assertTrue(AnalysisProtocolService.list(summary.get("notices")).stream().anyMatch(value->value.toString().contains("标题尚未生成")));
assertFalse(AnalysisProtocolService.list(summary.get("notices")).stream().anyMatch(value->value.toString().contains("事实核验")));
var completion=AnalysisProtocolService.map(saved.get("evidenceCompletion"));assertEquals(true,completion.get("bodyEvidenceSufficient"));assertEquals(false,completion.get("titleValid"));assertEquals(true,completion.get("titleRequired"));
}
@Test void autonomousHistoryFormatFallbackUsesServerFactsButRemainsPartial() {
enableAutonomous();completedHistoryMetric(null,9,"35.055");String turn=newTurn("解释历史结果并生成报告");var a=claim(turn,false);var task=historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));
String ref=AnalysisProtocolService.map(AnalysisProtocolService.list(snapshot.get("facts")).getFirst()).get("id").toString();var details=new LinkedHashMap<>(historyDetails(task,snapshot));details.put("titleSource","verified_history_summary");details.put("titleGeneratedByModel",false);details.put("answer","history-fact:forged filters={\"department\":\"伪造\"} 0.1234567890123456789");details.put("readingSummary",Map.of("version",1,"lead","伪造正文","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of(ref)));details.put("evidenceCompletion",Map.of("modelDeclaredEvidenceSufficient",false));
finishHistory(a,details);assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);String answerText=Objects.toString(saved.get("answer"),"");
assertTrue(answerText.contains("35.055"));assertFalse(answerText.contains("history-fact:"));assertFalse(answerText.contains("filters="));assertFalse(answerText.contains("筛选 {"));assertFalse(answerText.contains("分组键"));assertFalse(answerText.contains(ref));assertFalse(answerText.contains("0.1234567890123456789"));assertTrue(saved.get("reportMarkdown").toString().contains("|"));assertEquals(List.of(),saved.get("queryEvidenceRefs"));assertFalse(saved.get("readingSummary").toString().contains(ref));assertEquals(false,AnalysisProtocolService.map(saved.get("historyCoverage")).get("complete"));assertTrue(AnalysisProtocolService.list(AnalysisProtocolService.map(saved.get("historyCoverage")).get("missingEvidence")).stream().anyMatch(item->item.toString().contains("正文解释未完成事实核验")));var completion=AnalysisProtocolService.map(saved.get("evidenceCompletion"));assertEquals(false,completion.get("bodyEvidenceSufficient"));assertEquals(false,completion.get("titleValid"));assertEquals(true,completion.get("titleRequired"));
}
@Test void historyDisplayFactsLinkToGlobalSectionsWithMatchingRealSourceFrames() {
enableAutonomous();completedHistoryMetric("18X15X4+T",8,"35.055");completedHistoryMetric("18X15X4+Z",9,"12.345");String turn=newTurn("整理两个历史范围");var a=claim(turn,false);historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));
@@ -1236,7 +1341,7 @@ class AnalysisProtocolServiceMySqlTest {
finishHistory(a,details);assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertFalse(saved.get("answer").toString().contains("错误"));assertFalse(saved.get("readingSummary").toString().contains("script"));assertEquals(List.of(ref),saved.get("queryEvidenceRefs"));assertFalse(AnalysisProtocolService.list(AnalysisProtocolService.map(saved.get("readingSummary")).get("notices")).isEmpty());assertTrue(saved.get("answer").toString().contains("尚未解决"));
}
@Test void staticHistoryCannotClaimAutonomousWorkerTitleOrSummary() {
completedHistoryMetric(null,9,"35.055");String turn=newTurn("旧模式历史报告");var a=claim(turn,false);var task=historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));var details=new LinkedHashMap<>(historyDetails(task,snapshot));details.put("titleGeneratedByModel",false);details.put("titleSource","verified_history_summary");details.put("answer","不得绕过旧模式");details.put("readingSummary",Map.of("version",1,"lead","forged","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of()));finishHistory(a,details);assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertNotEquals(details.get("answer"),saved.get("answer"));assertFalse(saved.containsKey("readingSummary"));
completedHistoryMetric(null,9,"35.055");String turn=newTurn("旧模式历史报告");var a=claim(turn,false);var task=historyTask(a,turn,"all");var snapshot=tx.execute(s->service.history(a.attemptId(),actor));var details=new LinkedHashMap<>(historyDetails(task,snapshot));details.put("titleGeneratedByModel",false);details.put("titleSource","verified_history_summary");details.put("answer","不得绕过旧模式");details.put("readingSummary",Map.of("version",1,"lead","forged","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of()));finishHistory(a,details);assertEquals("PARTIAL",turnStatus(turn));var saved=json.readValue(jdbc.queryForObject("SELECT answer_json FROM analysis_turn WHERE id=?",String.class,turn),Map.class);assertNotEquals(details.get("answer"),saved.get("answer"));var summary=AnalysisProtocolService.map(saved.get("readingSummary"));assertFalse(summary.get("lead").toString().contains("forged"));assertEquals(List.of(),summary.get("evidenceRefs"));
}
private void enableAutonomous(){jdbc.update("UPDATE analysis_protocol_session SET autonomous_enabled=TRUE WHERE session_id=?",session);}
private Map<String,Object> nativeTask(String turn,Map<String,Object> filters){var t=task(turn,filters,false);t.put("kind","analysis");t.put("executionMode","native_agent");t.put("trendRequired",false);t.put("visualPreference","none");return t;}
@@ -0,0 +1,88 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.time.Duration;
import java.time.Instant;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.jdbc.core.JdbcTemplate;
import tools.jackson.databind.json.JsonMapper;
class AnalysisTurnNativeBudgetTest {
@Test void nativeBudgetPublishesSharedRuntimeDeadlineAndInvocationLimits() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);
JsonMapper json=JsonMapper.builder().build();
var service=new AnalysisTurnService(jdbc,json,null,null,null,null,null,null);
Instant started=Instant.now();
service.initializeNativeBudget("turn-id","{\"toolCallsMax\":50,\"planRevisionsMax\":3,\"custom\":\"keep\"}");
ArgumentCaptor<Object[]> args=ArgumentCaptor.forClass(Object[].class);
verify(jdbc).update(eq("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'"),args.capture());
assertEquals("turn-id",args.getValue()[1]);
Map<?,?> budget=json.readValue((String)args.getValue()[0],Map.class);
assertEquals(1000,((Number)budget.get("toolCallsMax")).intValue());
assertEquals(0,((Number)budget.get("toolCallsUsed")).intValue());
assertEquals(1000,((Number)budget.get("toolCallsRemaining")).intValue());
assertEquals(0,((Number)budget.get("planRevisionsMax")).intValue());
assertEquals(0,((Number)budget.get("planRevisionsUsed")).intValue());
assertEquals(AnalysisRuntimePolicy.MAX_OUTPUT_TOKENS,((Number)budget.get("maxOutputTokens")).intValue());
assertEquals(AnalysisRuntimePolicy.TURN_TIMEOUT.toSeconds(),((Number)budget.get("turnTimeoutSeconds")).longValue());
assertEquals(AnalysisRuntimePolicy.REQUEST_TIMEOUT.toSeconds(),((Number)budget.get("requestTimeoutSeconds")).longValue());
Instant deadline=Instant.parse((String)budget.get("deadlineAt"));
Instant expected=started.plus(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT);
assertTrue(Duration.between(expected,deadline).abs().compareTo(Duration.ofSeconds(2))<0,
"deadline metadata uses the shared NAO watchdog policy");
assertEquals("keep",budget.get("custom"));
}
@Test void protocolNativeWorkerReplacesLegacyQueuedDeadlineBeforeStartingRunner() throws Exception {
JdbcTemplate jdbc=mock(JdbcTemplate.class);
JsonMapper json=JsonMapper.builder().build();
AnalysisProtocolService protocol=mock(AnalysisProtocolService.class);
NaoAnalysisRunner runner=mock(NaoAnalysisRunner.class);
String turnId="turn-id",sessionId="session-id",attemptId="attempt-id";
var attempt=new AnalysisProtocolService.Attempt(attemptId,sessionId,turnId,"RUNNING",1L,"execution-id",
Map.of(),"user-id","release-id","scope",false,"RUNNING",attemptId);
when(protocol.claim(turnId,null)).thenReturn(attempt);
when(protocol.runtimeContext(attempt,null)).thenReturn(Map.of());
when(runner.run(eq(turnId),eq(sessionId),isNull(),eq("question"),eq("release-id"),anyMap(),any(),any(),any()))
.thenReturn(new NaoAnalysisRunner.Result("FAILED","",Map.of(),"failed"));
var service=new AnalysisTurnService(jdbc,json,null,null,null,null,null,null,null,runner,null);
service.setProtocol(protocol);
Class<?> turnType=Arrays.stream(AnalysisTurnService.class.getDeclaredClasses())
.filter(type->type.getSimpleName().equals("Turn")).findFirst().orElseThrow();
Constructor<?> turnConstructor=turnType.getDeclaredConstructors()[0];turnConstructor.setAccessible(true);
Object turn=turnConstructor.newInstance(turnId,sessionId,"user-id","question","QUEUED","{}","{}","[]","[]",
"{\"deadlineAt\":\"2000-01-01T00:04:00Z\"}",null,"scope");
Class<?> sessionType=Arrays.stream(AnalysisTurnService.class.getDeclaredClasses())
.filter(type->type.getSimpleName().equals("Session")).findFirst().orElseThrow();
Constructor<?> sessionConstructor=sessionType.getDeclaredConstructors()[0];sessionConstructor.setAccessible(true);
Object session=sessionConstructor.newInstance(sessionId,"user-id","release-id",Map.of(),"scope");
Method run=AnalysisTurnService.class.getDeclaredMethod("runProtocolNative",turnType,AppUserPrincipal.class,sessionType);
run.setAccessible(true);
Instant started=Instant.now();
run.invoke(service,turn,null,session);
ArgumentCaptor<Object[]> args=ArgumentCaptor.forClass(Object[].class);
verify(jdbc).update(eq("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'"),args.capture());
Map<?,?> budget=json.readValue((String)args.getValue()[0],Map.class);
assertEquals(1000,((Number)budget.get("toolCallsMax")).intValue());
assertEquals(AnalysisRuntimePolicy.TURN_TIMEOUT.toSeconds(),((Number)budget.get("turnTimeoutSeconds")).longValue());
assertEquals(AnalysisRuntimePolicy.REQUEST_TIMEOUT.toSeconds(),((Number)budget.get("requestTimeoutSeconds")).longValue());
Instant deadline=Instant.parse((String)budget.get("deadlineAt"));
assertTrue(Duration.between(started.plus(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT),deadline).abs()
.compareTo(Duration.ofSeconds(2))<0);
verify(runner).run(eq(turnId),eq(sessionId),isNull(),eq("question"),eq("release-id"),anyMap(),any(),any(),any());
}
}
@@ -17,6 +17,8 @@ import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.identity.JdbcAppUserDetailsService;
import cn.yunsheng.quality.metrics.MetricsService;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.List;
@@ -98,6 +100,110 @@ class AnalysisTurnSnapshotTest {
details.put("readingSummary",Map.of("version",1,"lead","未知引用[QDEADBEEF]","evidence",List.of(),"notices",List.of(),"evidenceRefs",List.of("QDEADBEEF")));assertEquals(false,harness.service.nativeAnswer(details,"text",Map.of(),"release-1","COMPLETED","stop",TURN_ID,null).containsKey("readingSummary"));
}
@Test void historicalReportLineageKeepsItsServerMethodAfterSnapshotRead() {
Harness harness = new Harness("valid");
var proof = Map.of("importId", "server-import", "snapshotHash", "signed-snapshot");
var lineage = List.of(Map.of("visualId", "saved-chart", "sourceTurnId", "source-turn"));
harness.storedOverride = Map.of("answer", "已核实汇总", "completionMethod", "confirmed_history_report",
"historyEvidence", proof, "historyVisualSources", lineage,
"visualSchemaVersion", 1, "visualState", "not_requested", "visuals", List.of());
var result = harness.service.getSnapshot(TURN_ID, harness.submittedActor);
assertEquals("confirmed_history_report", result.get("completionMethod"));
assertEquals(proof, result.get("historyEvidence"));
assertEquals(lineage, result.get("historyVisualSources"));
verifyNoInteractions(harness.metrics);
}
@Test void publicSnapshotKeepsHistoryChartsUnderTheirSignedSourceScope() {
for (Map<String,Object> sourceFilter : List.<Map<String,Object>>of(
Map.of("exclusions",Map.of("product",List.of("excluded-product"))),
Map.of("columnFilters",List.of(Map.of("field","src_any","stages",List.of("black"),"operator","in","values",List.of("000123")))))) {
Harness harness=new Harness("valid");harness.enableConfirmedHistoryReport();
harness.storedOverride=historyReportAnswer(sourceScope(sourceFilter),true);
var result=harness.service.getSnapshot(TURN_ID,harness.submittedActor);
assertEquals("ready",result.get("visualState"),sourceFilter.toString());
var chart=(Map<?,?>)((List<?>)result.get("visuals")).getFirst();
assertEquals("source-chart",chart.get("id"));
assertEquals(TURN_ID,chart.get("originTurnId"),"the report must rebind the chart origin to the current report turn");
assertEquals("source-turn",((Map<?,?>)((List<?>)result.get("historyVisualSources")).getFirst()).get("sourceTurnId"),
"the source lineage remains bound to its historical turn");
assertEquals(sourceFilter.get("exclusions"),((Map<?,?>)chart.get("scope")).get("exclusions"));
if(sourceFilter.containsKey("columnFilters"))assertEquals(sourceFilter.get("columnFilters"),((Map<?,?>)chart.get("scope")).get("columnFilters"));
verify(harness.protocol).authorizeReportSnapshot(TURN_ID,harness.freshActor);
}
}
@Test void publicSnapshotRejectsChangedOrMissingHistoryVisualLineageAndNonReportExemption() {
Map<String,Object> scope=sourceScope(Map.of("exclusions",Map.of("product",List.of("excluded-product"))));
for(String mutation:List.of("scope","period","comparisonPeriod","dimension","stage","metric","evidenceRefs","visualId","sourceTurnId","foreignOrigin","missing")) {
Harness harness=new Harness("valid");harness.enableConfirmedHistoryReport();
var answer=historyReportAnswer(scope,true);
if("missing".equals(mutation))answer.put("historyVisualSources",List.of());
else if("foreignOrigin".equals(mutation)) {
var chart=new LinkedHashMap<>((Map<String,Object>)((List<?>)answer.get("visuals")).getFirst());
chart.put("originTurnId","foreign-turn");answer.put("visuals",List.of(chart));
}
else {
var lineage=new LinkedHashMap<>((Map<String,Object>)((List<?>)answer.get("historyVisualSources")).getFirst());
switch(mutation) {
case "scope" -> lineage.put("scope",Map.of("releaseId","release-1","year",2026,"month",3,"view","product","stage","black","includeUnassigned",false));
case "period" -> lineage.put("period",Map.of("year",2026,"month",4,"label","2026-04","granularity","month"));
case "comparisonPeriod" -> lineage.put("comparisonPeriod",Map.of("year",2026,"month",1,"label","2026-01","granularity","month"));
case "dimension" -> lineage.put("dimension","group");
case "stage" -> lineage.put("stage","bright");
case "metric" -> lineage.put("metric","bright_total_weight");
case "evidenceRefs" -> lineage.put("evidenceRefs",List.of("QDEADBEEF"));
case "visualId" -> lineage.put("visualId","other-chart");
case "sourceTurnId" -> lineage.put("sourceTurnId","unavailable-source");
default -> throw new IllegalStateException(mutation);
}
answer.put("historyVisualSources",List.of(lineage));
}
harness.storedOverride=answer;
var result=harness.service.getSnapshot(TURN_ID,harness.submittedActor);
assertEquals("failed",result.get("visualState"),mutation);
assertEquals(List.of(),result.get("visuals"),mutation);
}
Harness nonReport=new Harness("valid");nonReport.enableConfirmedHistoryReport();
nonReport.planOverride=Map.of("context",Map.of("releaseId","release-1","year",2026,"month",9,"view","overall"),"task",Map.of("kind","metrics"));
nonReport.storedOverride=historyReportAnswer(scope,true);
var result=nonReport.service.getSnapshot(TURN_ID,nonReport.submittedActor);
assertEquals("failed",result.get("visualState"));
assertEquals(List.of(),result.get("visuals"));
org.mockito.Mockito.verify(nonReport.protocol,org.mockito.Mockito.never()).authorizeReportSnapshot(TURN_ID,nonReport.freshActor);
}
private static Map<String,Object> sourceScope(Map<String,Object> sourceFilter) {
var scope=new LinkedHashMap<String,Object>();scope.put("releaseId","release-1");scope.put("year",2026);scope.put("month",3);
scope.put("view","product");scope.put("stage","black");scope.put("includeUnassigned",false);scope.putAll(sourceFilter);return scope;
}
private static Map<String,Object> historyReportAnswer(Map<String,Object> sourceScope,boolean includeLineage) {
var period=Map.<String,Object>of("year",2026,"month",3,"label","2026-03","granularity","month");
var comparison=Map.<String,Object>of("year",2026,"month",2,"label","2026-02","granularity","month");
var evidenceRefs=List.of("Q1234ABCD");
var chart=new LinkedHashMap<String,Object>();chart.put("id","source-chart");chart.put("type","bar");chart.put("title","正式投入重量");chart.put("unit","kg");
chart.put("categories",List.of(Map.of("key","P1","label","产品一"),Map.of("key","P2","label","产品二")));
chart.put("series",List.of(Map.of("key","black_total_weight","label","黑片投入重量","values",List.of(20,10))));
chart.put("evidenceRefs",evidenceRefs);chart.put("requiredFields",List.of("black_total_weight"));chart.put("scope",sourceScope);
chart.put("period",period);chart.put("comparisonPeriod",comparison);chart.put("stage","black");chart.put("metric","black_total_weight");
chart.put("coverage",Map.of("totalRows",2,"returnedRows",2,"truncated",false));chart.put("dimension","product");chart.put("sectionKey","product");
chart.put("originTurnId",TURN_ID);
Map<String,Object> lineage=Map.of("visualId","source-chart","sourceTurnId","source-turn","scope",sourceScope,"period",period,
"comparisonPeriod",comparison,"dimension","product","stage","black","metric","black_total_weight","evidenceRefs",evidenceRefs);
var answer=new LinkedHashMap<String,Object>();answer.put("answer","历史报告");answer.put("completionMethod","confirmed_history_report");
answer.put("historyEvidence",Map.of("importId","signed-import","snapshotHash","signed-hash"));
answer.put("historySources",List.of(Map.of("turnId","source-turn","available",true)));
answer.put("queryEvidenceRefs",evidenceRefs);
answer.put("visualSchemaVersion",1);answer.put("visualState","ready");answer.put("visuals",List.of(chart));
answer.put("historyVisualSources",includeLineage?List.of(lineage):List.of());return answer;
}
private static final class Harness {
private final JdbcTemplate jdbc = mock(JdbcTemplate.class);
private final MetricsService metrics = mock(MetricsService.class);
@@ -107,7 +213,9 @@ class AnalysisTurnSnapshotTest {
private final AppUserPrincipal freshActor = actor("fresh-user", "SYSTEM_ADMIN");
private final String fingerprint;
private final AnalysisTurnService service;
private final AnalysisProtocolService protocol=mock(AnalysisProtocolService.class);
private Map<String,Object> storedOverride;
private Map<String,Object> planOverride;
private Harness(String storedScope) {
when(governance.policy(freshActor.getId().toString())).thenReturn(Map.of(
@@ -136,7 +244,8 @@ class AnalysisTurnSnapshotTest {
}
if (sql.contains("FROM analysis_turn WHERE id=? AND user_id=?")) {
if (!TURN_ID.equals(requestedTurnId)) return List.of();
ResultSet rs = turnRow(storedOverride==null?answer:json.writeValueAsString(storedOverride), plan, savedHash);
ResultSet rs = turnRow(storedOverride==null?answer:json.writeValueAsString(storedOverride),
planOverride==null?plan:json.writeValueAsString(planOverride), savedHash);
return List.of(mapper.mapRow(rs, 0));
}
if (sql.contains("FROM analysis_session WHERE id=? AND user_id=?")) {
@@ -150,7 +259,11 @@ class AnalysisTurnSnapshotTest {
}
return List.of();
}).when(jdbc).query(anyString(), any(RowMapper.class), any(Object[].class));
doAnswer(invocation -> List.of()).when(jdbc).query(eq(QUERY_FILTERS), any(RowMapper.class));
doAnswer(invocation -> {
RowMapper<?> mapper=invocation.getArgument(1);ResultSet rs=mock(ResultSet.class);
when(rs.getString(1)).thenReturn("src_any");when(rs.getString(2)).thenReturn("src_any_black");when(rs.getString(3)).thenReturn("src_any_bright");
return List.of(mapper.mapRow(rs,0));
}).when(jdbc).query(eq(QUERY_FILTERS), any(RowMapper.class));
doAnswer(invocation -> {
RowMapper<?> mapper = invocation.getArgument(1);
ResultSet rs = mock(ResultSet.class);
@@ -163,6 +276,13 @@ class AnalysisTurnSnapshotTest {
service = new AnalysisTurnService(jdbc, json, metrics, null, null, users, governance, null);
}
private void enableConfirmedHistoryReport() {
planOverride=Map.of("context",Map.of("releaseId","release-1","year",2026,"month",9,"view","overall"),"task",Map.of("kind","report"));
when(protocol.ownsTurn(TURN_ID)).thenReturn(true);
when(protocol.visualRecoveryOverlay(anyString(),any())).thenReturn(Map.of());
service.setProtocol(protocol);
}
private ResultSet turnRow(String answer, String plan, String savedHash) throws Exception {
ResultSet rs = mock(ResultSet.class);
when(rs.getString(1)).thenReturn(TURN_ID);
@@ -23,6 +23,46 @@ import org.springframework.jdbc.core.RowMapper;
import tools.jackson.databind.json.JsonMapper;
class AnalysisVisualSnapshotAccessTest {
@Test void combinedScrapRankingSurvivesSnapshotValidationWithBothStagePermissions() {
var fields=List.of("black_scrap_weight","bright_scrap_weight");
var chart=chart("current_scrap_weight","kg",null,fields,List.of(65,41));
chart.put("sectionKey","product");chart.put("dimension","product");
chart.put("coverage",Map.of("totalRows",20,"returnedRows",2,"truncated",true,"shareBasis","all_scope_weight","denominator",200));
for(String type:List.of("bar","column","pareto")) {
chart.put("type",type);
assertEquals("ready",normalize(List.of(chart),Set.copyOf(fields),SCOPE).state(),type);
assertEquals("failed",normalize(List.of(chart),Set.of("black_scrap_weight"),SCOPE).state(),type);
}
chart.put("series",List.of(Map.of("key","scrap_weight","label","报废总重","values",List.of(65,41))));
assertEquals("failed",normalize(List.of(chart),Set.copyOf(fields),SCOPE).state());
}
@Test void combinedRankingChartMustMatchSavedTopRowsWeightsDenominatorAndQueryReference() {
var fields=List.of("black_scrap_weight","bright_scrap_weight");
var chart=chart("current_scrap_weight","kg",null,fields,List.of(65,41));
chart.put("dimension","product");chart.put("sectionKey","product");
chart.put("categories",List.of(Map.of("key","P1","label","产品一"),Map.of("key","P2","label","产品二")));
chart.put("series",List.of(Map.of("key","current_scrap_weight","label","报废总重","values",List.of(65,41))));
chart.put("coverage",Map.of("totalRows",3,"returnedRows",2,"truncated",true,"shareBasis","all_scope_weight","denominator",200));
chart.put("period",Map.of("year",2026,"month",9));chart.put("evidenceRefs",List.of("QABCDEF12"));
var task=Map.<String,Object>of("fields",List.of("scrapWeight","rate"),"stages",List.of(),"dimensions",List.of("product"),
"filters",Map.of("includeUnassigned",false,"extraFilters",Map.of("process",List.of("A"))),"period",Map.of("year",2026,"month",9));
var artifact=Map.<String,Object>of("releaseId","release-1","dimension","product","period",Map.of("year",2026,"month",9),
"filters",task.get("filters"),"rankings",List.of(Map.of("stage","overall","metric","scrapWeight","completeRanking",true,
"orderPolicy","weight_desc_key_codepoint_asc","totalRows",3,"denominator",200,
"rows",List.of(Map.of("rank",1,"key","P1","label","产品一","weight",65),Map.of("rank",2,"key","P2","label","产品二","weight",41),Map.of("rank",3,"key","P3","label","产品三","weight",20)))));
assertEquals("ready",normalize(List.of(chart),Set.copyOf(fields),SCOPE).state());
assertTrue(AnalysisVisualSnapshotAccess.rankingChartMatchesArtifact(chart,artifact,task,"release-1",2,"QABCDEF12"));
var wrongValue=new LinkedHashMap<>(chart);wrongValue.put("series",List.of(Map.of("key","current_scrap_weight","label","报废总重","values",List.of(999999,41))));
var wrongDenominator=new LinkedHashMap<>(chart);wrongDenominator.put("coverage",Map.of("totalRows",3,"returnedRows",2,"truncated",true,"shareBasis","all_scope_weight","denominator",100));
var wrongTop=new LinkedHashMap<>(chart);wrongTop.put("categories",List.of(Map.of("key","invented-product","label","伪造产品"),Map.of("key","P2","label","产品二")));
var wrongOrder=new LinkedHashMap<>(chart);wrongOrder.put("categories",List.of(Map.of("key","P2","label","产品二"),Map.of("key","P1","label","产品一")));wrongOrder.put("series",List.of(Map.of("key","current_scrap_weight","label","报废总重","values",List.of(41,65))));
var wrongMetric=new LinkedHashMap<>(chart);wrongMetric.put("metric","black_scrap_weight");wrongMetric.put("stage","black");wrongMetric.put("series",List.of(Map.of("key","black_scrap_weight","label","黑片报废重量","values",List.of(65,41))));
var missingTop=new LinkedHashMap<>(chart);missingTop.put("categories",List.of(Map.of("key","P1","label","产品一")));missingTop.put("series",List.of(Map.of("key","current_scrap_weight","label","报废总重","values",List.of(65))));missingTop.put("coverage",Map.of("totalRows",3,"returnedRows",1,"truncated",true,"shareBasis","all_scope_weight","denominator",200));
for(var invalid:List.of(wrongValue,wrongDenominator,wrongTop,wrongOrder,wrongMetric,missingTop))
assertFalse(AnalysisVisualSnapshotAccess.rankingChartMatchesArtifact(invalid,artifact,task,"release-1",2,"QABCDEF12"));
assertFalse(AnalysisVisualSnapshotAccess.rankingChartMatchesArtifact(chart,artifact,task,"release-1",2,"Q00000000"),"unbound or cross-query references fail closed");
}
@Test void scrapRateUsesBothWeightPermissionsAndPreservesCorrectedValuesAboveOne() {
var chart=chart("black_scrap_rate","ratio","black",List.of("black_scrap_weight","black_total_weight"),List.of(0,1.2));
var accepted=normalize(List.of(chart),Set.of("black_scrap_weight","black_total_weight"),SCOPE);
@@ -68,6 +108,18 @@ class AnalysisVisualSnapshotAccessTest {
if(stage!=null){current.put("stage",stage.equals("black")?"bright":"black");assertEquals("failed",normalize(List.of(current),all,SCOPE).state());}
}
}
@Test void officialRateChartsPreserveCorrectedValuesAboveOne() {
var fields=Set.of("black_good_weight","black_total_weight","bright_good_weight","bright_total_weight");
for(String metric:List.of("composite_rate","black_rate","bright_rate")) {
String stage=metric.equals("composite_rate")?null:metric.startsWith("black")?"black":"bright";
var chart=chart(metric,"ratio",stage,List.copyOf(fields),List.of(1.2,1.3));
assertEquals("ready",normalize(List.of(chart),fields,SCOPE).state(),metric);
}
var delta=chart("rate_drop_pp","pp",null,List.copyOf(fields),List.of(120,-130));
delta.put("dimension","product");delta.put("sectionKey","product");
delta.put("comparisonPeriod",Map.of("year",2026,"month",8,"label","2026-08","granularity","month"));
assertEquals("ready",normalize(List.of(delta),fields,SCOPE).state(),"rate differences may exceed 100 percentage points when official rates exceed one");
}
private static final String TURN_ID = "93ab6413-6459-426e-921a-e7494bb2c915";
private static final Map<String, Object> SCOPE = Map.of(
"year", 2026,
@@ -299,6 +351,11 @@ class AnalysisVisualSnapshotAccessTest {
assertEquals("overview", accepted.visuals().getFirst().get("sectionKey"));
assertEquals(allFields, Set.copyOf((List<String>) accepted.visuals().getFirst().get("requiredFields")));
Map<String, Object> largeReconciledImpact = waterfallChart();
largeReconciledImpact.put("series", List.of(Map.of("key", "composite_impact_pp", "label", "数学影响(pp)", "values", List.of(-120, -130, -250))));
assertEquals("ready", normalize(List.of(largeReconciledImpact), allFields, SCOPE).state(),
"a large but reconciled official impact must not be rejected by a 100 pp cap");
Map<String, Object> wrongSum = waterfallChart();
wrongSum.put("series", List.of(Map.of("key", "composite_impact_pp", "label", "影响", "values", List.of(-0.3, -0.14, -0.5))));
Map<String, Object> wrongKeys = waterfallChart();
@@ -0,0 +1,180 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.filters.SourceColumnQueries;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.metrics.MetricsRollupService;
import cn.yunsheng.quality.release.ReleaseService;
import java.math.BigDecimal;
import java.util.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import tools.jackson.databind.json.JsonMapper;
@EnabledIfEnvironmentVariable(named="QUALITY_SOURCE_COLUMN_TEST_CREATE_DATABASE", matches="true")
class FineDefectRollupsMySqlTest {
JdbcTemplate admin, jdbc; String database;
final String build = UUID.randomUUID().toString(), release = UUID.randomUUID().toString();
final JsonMapper json = JsonMapper.builder().build();
final GovernanceAccessService governance = mock(GovernanceAccessService.class);
final ReleaseService releases = mock(ReleaseService.class);
final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "rollup-test", "", "测试", true, false,
List.of("SYSTEM_ADMIN"), List.of("*"), List.of("*"), List.of());
MetricsRollupService metrics;
FineDefectRollups rollups;
AnalysisFineDefectsService source, fast;
@BeforeEach void setup() throws Exception {
String url = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL");
assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:") && url.contains("/?"));
String user = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"), password = System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin = new JdbcTemplate(new DriverManagerDataSource(url, user, password));
database = "quality_fine_rollup_" + UUID.randomUUID().toString().replace("-", "");
admin.execute("CREATE DATABASE " + database + " CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
jdbc = spy(new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?", "/" + database + "?"), user, password)));
jdbc.execute("CREATE TABLE transform_build(id CHAR(36) PRIMARY KEY,source_revision_id CHAR(36))");
jdbc.execute("CREATE TABLE source_revision(id CHAR(36) PRIMARY KEY)");
jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(100),status VARCHAR(32),published_at TIMESTAMP NULL,rule_version VARCHAR(100),mapping_version VARCHAR(100))");
jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36))");
for (String stage : List.of("black", "bright")) jdbc.execute("CREATE TABLE fact_" + stage + "(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),source_revision_id CHAR(36),month_key CHAR(7),department VARCHAR(190),group_name VARCHAR(190),project_group VARCHAR(190),product VARCHAR(190),qualified_weight DECIMAL(38,8),scrap_weight DECIMAL(38,8),total_weight DECIMAL(38,8),raw_values JSON,components JSON,KEY ix_fact_" + stage + "_month_dims(build_id,month_key))");
for (String migration : List.of("V16__metric_rollups.sql", "V36__fine_defect_rollups.sql")) {
try (var stream = getClass().getResourceAsStream("/db/migration/" + migration)) {
for (String sql : new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8).split(";")) if (!sql.isBlank()) jdbc.execute(sql);
}
}
jdbc.update("INSERT INTO source_revision VALUES(?)", build);
jdbc.update("INSERT INTO transform_build VALUES(?,?)", build, build);
jdbc.update("INSERT INTO release_member VALUES(?,?,?)", release, build, build);
jdbc.update("INSERT INTO data_release VALUES(?,'Published','PUBLISHED',NULL,'rules','mapping')", release);
when(releases.resolvePublishedRelease(anyString())).thenAnswer(c -> c.getArgument(0));
when(governance.allowedFields(any())).thenReturn(Set.of("*"));
for (int i = 0; i < 7; i++) {
Object value = new Object[] {"0.100000000000000001", "0.200000000000000002", 0, "", "bad", -2, 4}[i];
seed("black", i == 6 ? "2026-02" : "2026-01", i == 6 ? "B" : "A", i == 5 ? null : "G", "P" + i % 2,
Map.of("责废尺寸重量", value), Map.of("black_responsibility_scrap", 1));
}
seed("black", "2026-01", "A", "g", "P0", Map.of("责废尺寸重量", 30), Map.of("black_responsibility_scrap", 30));
seed("bright", "2026-01", "A", ExpectedSourceDetailPolicy.GROUP, "VCM", Map.of("镀废重量合计", 5), Map.of("bright_plating_scrap", 5));
seed("bright", "2026-02", "B", "G", "P0", Map.of("镀废缺角重量", 2), Map.of("bright_plating_scrap", 2));
rollups = new FineDefectRollups(jdbc, json); metrics = new MetricsRollupService(jdbc); metrics.refreshBuild(build);
source = service(false); fast = service(true);
}
AnalysisFineDefectsService service(boolean enabled) {
var service = new AnalysisFineDefectsService(jdbc, releases, governance, json);
service.setSourceColumns(new SourceColumnQueries(jdbc, governance, json));
if (enabled) service.setRollups(rollups);
return service;
}
void seed(String stage, String month, String department, String group, String product, Map<String,Object> raw, Map<String,Object> components) {
jdbc.update("INSERT INTO fact_" + stage + "(build_id,source_revision_id,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components) VALUES(?,?,?,?,?,NULL,?,10,1,11,CAST(? AS JSON),CAST(? AS JSON))",
build, build, month, department, group, product, json.writeValueAsString(raw), json.writeValueAsString(components));
}
Map<String,Object> request(String stage, Map<String,Object> scope) {
var value = new LinkedHashMap<String,Object>(Map.of("releaseId", release, "year", 2026, "stage", stage, "limit", 100, "includeUnassigned", true));
value.putAll(scope); return value;
}
Object canonical(Object value) {
if (value instanceof Number n) return new BigDecimal(n.toString()).stripTrailingZeros();
if (value instanceof Map<?,?> m) { var out = new LinkedHashMap<>(); m.forEach((k,v) -> out.put(k, canonical(v))); return out; }
if (value instanceof List<?> list) return list.stream().map(this::canonical).toList();
return value;
}
void equivalent(Map<String,Object> request, AppUserPrincipal user) {
var expected = source.query(request, user);
clearInvocations(jdbc);
assertEquals(canonical(expected), canonical(fast.query(request, user)));
verify(jdbc, never()).query(any(PreparedStatementCreator.class), any(ResultSetExtractor.class));
}
@Test void exactWeightsCountsReconciliationAndAllStandardScopesMatchTheSourceWithoutScanningFacts() {
for (String stage : List.of("black", "bright")) {
for (Map<String,Object> scope : List.<Map<String,Object>>of(Map.of(), Map.of("month", 1), Map.of("quarter", 1),
Map.of("department", "A"), Map.of("group", "G"), Map.of("group", "g"), Map.of("productSearch", "p0"),
Map.of("includeUnassigned", false), Map.of("monthStart", 1, "monthEnd", 2),
Map.of("exclusions", Map.of("department", List.of("B"))))) equivalent(request(stage, scope), actor);
}
}
@Test void fieldAndOrganizationPermissionsRemainEffective() {
var viewer = new AppUserPrincipal(UUID.randomUUID(), "viewer", "", "Viewer", true, false,
List.of("QUALITY_VIEWER"), List.of("A"), List.of("G"), List.of());
when(governance.allowedFields(viewer)).thenReturn(Set.of("责废尺寸重量"));
equivalent(request("black", Map.of()), viewer);
when(governance.allowedFields(actor)).thenReturn(Set.of("镀废缺角重量", "bright_plating_scrap"));
equivalent(request("bright", Map.of("group", ExpectedSourceDetailPolicy.GROUP)), actor);
}
@Test void missingOrInexactBuildFallsBackToTheCompleteSourceQuery() {
seed("black", "2026-01", "A", "G", "P0", Map.of("责废尺寸重量", "0.000000000000000000001"), Map.of());
// Their total fits DECIMAL(65,20), but each independently filterable group must also fit exactly.
seed("black", "2026-01", "A", "OTHER", "P0", Map.of("责废尺寸重量", "0.000000000000000000009"), Map.of());
rollups.refreshBuild(build);
assertEquals(0, jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup_build", Integer.class));
var request = request("black", Map.of());
assertEquals(canonical(source.query(request, actor)), canonical(fast.query(request, actor)));
}
@Test void copyAndChangedGroupsRefreshTheDetailsAlongsideTheMainRollup() {
String child = UUID.randomUUID().toString();
jdbc.update("INSERT INTO transform_build VALUES(?,?)", child, build);
for (String stage : List.of("black", "bright")) jdbc.update("INSERT INTO fact_" + stage + "(build_id,source_revision_id,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components) SELECT ?,source_revision_id,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components FROM fact_" + stage + " WHERE build_id=?", child, build);
metrics.copyBuild(build, child);
jdbc.update("UPDATE fact_black SET group_name='NEW' WHERE build_id=? AND department='B'", child);
metrics.refreshGroups(child, Set.of(new MetricsRollupService.GroupKey("black", "2026-02", "B", "G", null, "P0"),
new MetricsRollupService.GroupKey("black", "2026-02", "B", "NEW", null, "P0")));
jdbc.update("UPDATE release_member SET transform_build_id=? WHERE release_id=?", child, release);
equivalent(request("black", Map.of("group", "NEW")), actor);
equivalent(request("black", Map.of("group", "G")), actor);
equivalent(request("bright", Map.of()), actor);
}
@AfterEach void cleanup() {
if (admin != null && database != null && database.matches("quality_fine_rollup_[a-f0-9]{32}")) admin.execute("DROP DATABASE " + database);
}
@Test
@EnabledIfEnvironmentVariable(named="QUALITY_FINE_ROLLUP_SOURCE_DATABASE", matches="[a-zA-Z0-9_]+")
void currentRealDataHasIdenticalResultsAndFasterQueriesInAnIsolatedCopy() throws Exception {
String origin = System.getenv("QUALITY_FINE_ROLLUP_SOURCE_DATABASE");
assertTrue(origin.matches("[a-zA-Z0-9_]+") && !origin.equals(database));
String current = jdbc.queryForObject("SELECT release_id FROM " + origin + ".current_release WHERE dataset_id='quality'", String.class);
for (String table : List.of("fine_defect_rollup", "fine_defect_rollup_build", "metric_rollup", "metric_rollup_build", "fact_black", "fact_bright", "release_member", "transform_build", "source_revision")) jdbc.update("DELETE FROM " + table);
jdbc.update("INSERT INTO transform_build SELECT b.id,b.source_revision_id FROM " + origin + ".transform_build b JOIN " + origin + ".release_member rm ON rm.transform_build_id=b.id WHERE rm.release_id=?", current);
jdbc.update("INSERT INTO source_revision SELECT DISTINCT source_revision_id FROM transform_build");
jdbc.update("INSERT INTO release_member SELECT ?,source_revision_id,transform_build_id FROM " + origin + ".release_member WHERE release_id=?", release, current);
for (String stage : List.of("black", "bright")) jdbc.update("INSERT INTO fact_" + stage + "(build_id,source_revision_id,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components) SELECT f.build_id,f.source_revision_id,f.month_key,f.department,f.group_name,f.project_group,f.product,f.qualified_weight,f.scrap_weight,f.total_weight,f.raw_values,f.components FROM " + origin + ".fact_" + stage + " f JOIN release_member rm ON rm.transform_build_id=f.build_id AND rm.source_revision_id=f.source_revision_id");
var result = new LinkedHashMap<String,Object>(); result.put("sourceRelease", current);
long started = System.nanoTime();
var transaction = new org.springframework.transaction.support.TransactionTemplate(
new org.springframework.jdbc.datasource.DataSourceTransactionManager(jdbc.getDataSource()));
for (String id : jdbc.queryForList("SELECT id FROM transform_build", String.class)) transaction.executeWithoutResult(status -> rollups.refreshBuild(id));
result.put("backfillMs", (System.nanoTime()-started)/1_000_000L);
result.put("rollupRows", jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup", Long.class));
result.put("organizationRollupRows", jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup WHERE grain='organization'", Long.class));
var measurements = new ArrayList<Map<String,Object>>();
for (String stage : List.of("black", "bright")) {
var request = request(stage, Map.of());
started = System.nanoTime(); var expected = source.query(request, actor); long sourceMs = (System.nanoTime()-started)/1_000_000L;
started = System.nanoTime(); var actual = fast.query(request, actor); long rollupMs = (System.nanoTime()-started)/1_000_000L;
assertEquals(canonical(expected), canonical(actual));
measurements.add(Map.of("stage", stage, "sourceRows", actual.get("sourceRows"), "sourceMs", sourceMs, "rollupMs", rollupMs, "resultsIdentical", true));
}
result.put("measurements", measurements);
for (String stage : List.of("black", "bright")) equivalent(request(stage, Map.of("month", 1)), actor);
var path = java.nio.file.Path.of(System.getenv("QUALITY_PROJECT_ROOT"), "output/20261101-pareto-rollup/real-data.json");
java.nio.file.Files.writeString(path, json.writerWithDefaultPrettyPrinter().writeValueAsString(result));
System.out.println("Fine defect real-data measurements: " + json.writeValueAsString(result));
for (var measurement : measurements) assertTrue(((Number)measurement.get("rollupMs")).longValue() < ((Number)measurement.get("sourceMs")).longValue() * .65, measurement.toString());
}
}
@@ -0,0 +1,55 @@
package cn.yunsheng.quality.governance;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import tools.jackson.databind.json.JsonMapper;
@EnabledIfEnvironmentVariable(named="QUALITY_SOURCE_COLUMN_TEST_CREATE_DATABASE",matches="true")
class ClassificationSourceMySqlTest {
private JdbcTemplate admin,jdbc;private String database;
private ClassificationSourceService service;private CandidateAcceptanceService access;private GovernanceAccessService fields;
private final AppUserPrincipal actor=mock(AppUserPrincipal.class);
private final List<ClassificationPreviewService.Condition> conditions=List.of(new ClassificationPreviewService.Condition("@department","EQUALS","A"),new ClassificationPreviewService.Condition("@product","EQUALS","P"),new ClassificationPreviewService.Condition("PDT代码","IS_BLANK",""),new ClassificationPreviewService.Condition("销售片区","EQUALS","X"));
@BeforeEach void create(){
String url=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_SERVER_URL");assertTrue(url.startsWith("jdbc:mysql://127.0.0.1:")&&url.contains("/?"));
String user=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_USER"),password=System.getenv("QUALITY_SOURCE_COLUMN_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(url,user,password));database="quality_classification_source_"+UUID.randomUUID().toString().replace("-","");admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
jdbc=new JdbcTemplate(new DriverManagerDataSource(url.replaceFirst("/\\?","/"+database+"?"),user,password));
jdbc.execute("CREATE TABLE current_release(dataset_id VARCHAR(20),release_id VARCHAR(20))");jdbc.update("INSERT INTO current_release VALUES('quality','r')");
jdbc.execute("CREATE TABLE data_release(id VARCHAR(20),status VARCHAR(20))");jdbc.update("INSERT INTO data_release VALUES('r','PUBLISHED'),('new','PUBLISHED')");
jdbc.execute("CREATE TABLE release_member(release_id VARCHAR(20),transform_build_id VARCHAR(20),source_revision_id VARCHAR(20))");jdbc.update("INSERT INTO release_member VALUES('r','b','s')");
jdbc.execute("CREATE TABLE source_revision(id VARCHAR(20),source_file VARCHAR(60))");jdbc.update("INSERT INTO source_revision VALUES('s','原始数据.xlsx')");
jdbc.execute("CREATE TABLE fact_black(id VARCHAR(30),build_id VARCHAR(20),department VARCHAR(40),product VARCHAR(40),group_name VARCHAR(40),month_key VARCHAR(7),sheet_name VARCHAR(40),physical_row_no INT,components JSON,raw_values JSON)");
for(int i=1;i<=18;i++)jdbc.update("INSERT INTO fact_black VALUES(?,'b','A','P',NULL,'2026-01','报废明细',?,JSON_OBJECT('sourcePdtCode',?,'sourceSalesRegion','X'),JSON_OBJECT('客户','原始客户','隐藏字段','secret'))","row"+i,i,i==18?" ":"");
jdbc.update("INSERT INTO fact_black VALUES('assigned','b','A','P','KEEP','2026-01','报废明细',99,JSON_OBJECT('sourcePdtCode','','sourceSalesRegion','X'),JSON_OBJECT('客户','不应出现'))");
jdbc.update("INSERT INTO fact_black VALUES('case','b','A','P',NULL,'2026-01','报废明细',100,JSON_OBJECT('sourcePdtCode','','sourceSalesRegion','x'),JSON_OBJECT('客户','小写不应出现'))");
access=mock(CandidateAcceptanceService.class);fields=mock(GovernanceAccessService.class);when(fields.allowedFields(actor)).thenReturn(Set.of("客户"));
service=new ClassificationSourceService(new GovernanceSupport(jdbc,JsonMapper.builder().build()),access,fields);
}
@AfterEach void cleanup(){if(database!=null&&database.matches("quality_classification_source_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);}
@Test void exactSourceRowsPaginateFifteenAndStayPinnedWithoutPreviewOrTarget(){
var first=service.list(new ClassificationSourceService.Request("black",conditions,1,15,null),actor);
assertEquals(18,first.get("total"));var rows=(List<?>)first.get("rows");assertEquals(15,rows.size());var row=(Map<?,?>)rows.getFirst();assertEquals("原始数据.xlsx",row.get("sourceFile"));assertEquals(1,row.get("rowNumber"));assertEquals(Map.of("客户","原始客户"),row.get("raw"));
jdbc.update("UPDATE current_release SET release_id='new'");
var second=service.list(new ClassificationSourceService.Request("black",conditions,2,15,"r"),actor);assertEquals(3,((List<?>)second.get("rows")).size());assertEquals(16,((Map<?,?>)((List<?>)second.get("rows")).getFirst()).get("rowNumber"));
assertEquals(0,service.list(new ClassificationSourceService.Request("black",conditions,1,15,null),actor).get("total"));
assertEquals(1,jdbc.queryForObject("SELECT COUNT(*) FROM fact_black WHERE group_name='KEEP'",Integer.class));
}
@Test void cachedIndexesNeverBypassLatestPermissions(){
var request=new ClassificationSourceService.Request("black",conditions,1,15,null);service.list(request,actor);
doThrow(new org.springframework.web.server.ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(access).requireRead("r",actor);
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->service.list(request,actor));
}
@Test void invalidStageConditionsAndPagesAreRejectedBeforeQuerying(){
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->service.list(new ClassificationSourceService.Request("all",conditions,1,15,null),actor));
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->service.list(new ClassificationSourceService.Request("black",List.of(new ClassificationPreviewService.Condition("unsafe","EQUALS","x")),1,15,null),actor));
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->service.list(new ClassificationSourceService.Request("black",conditions,1,1000,null),actor));
verifyNoInteractions(access);
}
}
@@ -29,6 +29,7 @@ class UnclassifiedMappingRebuildMySqlTest {
for(String stage:List.of("black","bright"))jdbc.execute("CREATE TABLE fact_"+stage+"(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),source_revision_id VARCHAR(100),sheet_name VARCHAR(100),physical_row_no INT,month_key CHAR(7),department VARCHAR(190),group_name VARCHAR(190),project_group VARCHAR(190),product VARCHAR(190),qualified_weight DECIMAL(24,8),scrap_weight DECIMAL(24,8),total_weight DECIMAL(24,8),raw_values JSON,components JSON,UNIQUE(build_id,sheet_name,physical_row_no))");
jdbc.execute("CREATE TABLE defect_component(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),stage VARCHAR(16),sheet_name VARCHAR(100),physical_row_no INT,defect_name VARCHAR(100),weight DECIMAL(24,8),source_field VARCHAR(100))");
try(var stream=getClass().getResourceAsStream("/db/migration/V16__metric_rollups.sql")){for(String sql:new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).split(";"))if(!sql.isBlank())jdbc.execute(sql);}
try(var stream=getClass().getResourceAsStream("/db/migration/V36__fine_defect_rollups.sql")){for(String sql:new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).split(";"))if(!sql.isBlank())jdbc.execute(sql);}
jdbc.execute("CREATE TABLE import_run(id CHAR(36) PRIMARY KEY,idempotency_key VARCHAR(190),status VARCHAR(32),stage VARCHAR(80),requested_sources JSON,expected_current_release_id CHAR(36),created_by CHAR(36),processed BIGINT,total BIGINT,result_release_id CHAR(36),expected_mapping_version VARCHAR(100),expected_rule_version VARCHAR(100),workflow_operation_id CHAR(36),validation JSON,started_at TIMESTAMP(6),completed_at TIMESTAMP(6))");
jdbc.execute("CREATE TABLE release_protocol_dependency(release_id CHAR(36),protocol_id VARCHAR(100),PRIMARY KEY(release_id,protocol_id))");jdbc.execute("CREATE TABLE release_source_exclusion(release_id CHAR(36),source_document_id VARCHAR(100),request_id VARCHAR(100))");
jdbc.update("INSERT INTO data_release VALUES(?,'parent','PUBLISHED',NULL,NULL,'rule','old-map',NULL,TRUE,JSON_OBJECT(),?)",parent,UUID.randomUUID().toString());
@@ -90,4 +91,76 @@ class UnclassifiedMappingRebuildMySqlTest {
assertEquals("KEEP",jdbc.queryForObject("SELECT f.project_group FROM fact_black f JOIN release_member m ON m.transform_build_id=f.build_id WHERE m.release_id=? AND f.physical_row_no=1",String.class,candidate.releaseId()));
}
@Test void zeroMatchesReuseEveryFrozenBuildWithoutCopyingFactsOrRollups(){
for(String stage:List.of("black","bright"))jdbc.update("UPDATE fact_"+stage+" SET group_name='KEEP'");
var metrics=spy(rollups);var candidate=run(metrics);
assertEquals(0,candidate.classifiedRows());assertEquals(0,candidate.scannedRows());
assertEquals(2,jdbc.queryForObject("SELECT COUNT(*) FROM transform_build",Integer.class));
assertEquals(2,jdbc.queryForObject("SELECT COUNT(*) FROM release_member n JOIN release_member p ON p.release_id=? AND p.source_document_id=n.source_document_id AND p.transform_build_id=n.transform_build_id WHERE n.release_id=?",Integer.class,parent,candidate.releaseId()));
verify(metrics,never()).copyBuild(anyString(),anyString());verify(metrics,never()).refreshGroups(anyString(),anySet());
assertEquals(3,jdbc.queryForObject("SELECT COUNT(*) FROM fact_black",Integer.class));
assertEquals(1,jdbc.queryForObject("SELECT COUNT(*) FROM fact_bright",Integer.class));
}
@Test void oneStageChangeReusesTheUnaffectedSourceSnapshot(){
var selected=new LinkedHashMap<String,Object>(pending());selected.put("stage","black");
var metrics=spy(rollups);
var candidate=new TransactionTemplate(new DataSourceTransactionManager(dataSource)).execute(status->new UnclassifiedMappingRebuild(jdbc,json,worker,metrics).create(UUID.randomUUID().toString(),parent,"new-map","rule",Map.of("matchRules",List.of(selected)),List.of(selected),actor));
assertEquals(1,candidate.classifiedRows());
assertEquals(bright,jdbc.queryForObject("SELECT transform_build_id FROM release_member WHERE release_id=? AND source_document_id=?",String.class,candidate.releaseId(),bright));
assertNotEquals(black,jdbc.queryForObject("SELECT transform_build_id FROM release_member WHERE release_id=? AND source_document_id=?",String.class,candidate.releaseId(),black));
assertEquals(3,jdbc.queryForObject("SELECT COUNT(*) FROM transform_build",Integer.class));
assertEquals(1,jdbc.queryForObject("SELECT COUNT(*) FROM fact_bright",Integer.class));
verify(metrics,times(1)).copyBuild(eq(black),anyString());verify(metrics,never()).copyBuild(eq(bright),anyString());
}
void bindWorkflow(UnclassifiedMappingRebuild.Candidate candidate,Map<String,Object> definition){
jdbc.execute("CREATE TABLE mapping_version(id VARCHAR(100) PRIMARY KEY,definition JSON)");
jdbc.update("INSERT INTO mapping_version VALUES('old-map',CAST(? AS JSON)),('new-map',CAST(? AS JSON))",json.writeValueAsString(Map.of("matchRules",List.of())),json.writeValueAsString(definition));
jdbc.execute("CREATE TABLE data_workflow_operation(id CHAR(36) PRIMARY KEY,operation_type VARCHAR(32),expected_current_release_id CHAR(36),mapping_version_id VARCHAR(100),created_by CHAR(36))");
jdbc.update("INSERT INTO data_workflow_operation SELECT workflow_operation_id,'APPLY_MAPPING',expected_current_release_id,expected_mapping_version,created_by FROM import_run WHERE id=?",candidate.runId());
}
cn.yunsheng.quality.release.ReleaseService releaseGuard(){
jdbc.execute("CREATE TABLE rule_version(id VARCHAR(100) PRIMARY KEY)");jdbc.update("INSERT INTO rule_version VALUES('rule')");
jdbc.execute("CREATE TABLE target_version(id VARCHAR(100) PRIMARY KEY,status VARCHAR(32))");
jdbc.execute("CREATE TABLE source_protocol(id VARCHAR(100),applicable BOOLEAN,expected BOOLEAN,source_slot VARCHAR(190))");
jdbc.execute("CREATE TABLE source_document(id VARCHAR(100),source_slot VARCHAR(190))");
jdbc.execute("CREATE TABLE source_revision(id VARCHAR(100),archive_path VARCHAR(1000),sha256 CHAR(64),file_size BIGINT)");
when(actor.getRoleCodes()).thenReturn(List.of("SYSTEM_ADMIN"));
return new cn.yunsheng.quality.release.ReleaseService(jdbc,json,mock(org.springframework.context.ApplicationEventPublisher.class),mock(cn.yunsheng.quality.source.SourceScanner.class),mock(cn.yunsheng.quality.governance.SourceCoverageGuard.class),mock(cn.yunsheng.quality.governance.SourceAuthorizationService.class),mock(cn.yunsheng.quality.governance.CandidateAcceptanceService.class));
}
@Test void reuseNeedsTheBoundWorkflowAndUnclassifiedOnlyDefinitionAndDirectParent(){
var selected=new LinkedHashMap<String,Object>(pending());selected.put("stage","black");
Map<String,Object> definition=Map.of("matchRules",List.of(selected));
var candidate=new TransactionTemplate(new DataSourceTransactionManager(dataSource)).execute(status->new UnclassifiedMappingRebuild(jdbc,json,worker,rollups).create(UUID.randomUUID().toString(),parent,"new-map","rule",definition,List.of(selected),actor));
bindWorkflow(candidate,definition);
var proof=UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId());
assertNotNull(proof);assertEquals(Set.of(bright),proof.buildIds());assertEquals(List.of("2026-01"),proof.changedMonths());
var release=releaseGuard();
assertDoesNotThrow(()->org.springframework.test.util.ReflectionTestUtils.invokeMethod(release,"verifyDependencies",candidate.releaseId(),actor));
jdbc.update("UPDATE data_workflow_operation SET mapping_version_id='other-map'");
assertNull(UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId()));
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->org.springframework.test.util.ReflectionTestUtils.invokeMethod(release,"verifyDependencies",candidate.releaseId(),actor));
jdbc.update("UPDATE data_workflow_operation SET mapping_version_id='new-map'");
jdbc.update("UPDATE mapping_version SET definition=JSON_SET(definition,'$.categoryLookup',JSON_OBJECT('other','changed')) WHERE id='new-map'");
assertNull(UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId()));
jdbc.update("UPDATE mapping_version SET definition=CAST(? AS JSON) WHERE id='new-map'",json.writeValueAsString(definition));
jdbc.update("UPDATE release_member SET source_revision_id='other-revision' WHERE release_id=? AND source_document_id=?",candidate.releaseId(),bright);
assertNull(UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId()));
jdbc.update("UPDATE release_member SET source_revision_id=? WHERE release_id=? AND source_document_id=?",bright,candidate.releaseId(),bright);
jdbc.update("UPDATE data_release SET validation=JSON_SET(validation,'$.classificationChangedMonths',JSON_ARRAY()) WHERE id=?",candidate.releaseId());
assertNull(UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId()));
}
@Test void zeroHitProofReusesAllMembersAndCannotHideAFormulaChange(){
for(String stage:List.of("black","bright"))jdbc.update("UPDATE fact_"+stage+" SET group_name='KEEP'");
var candidate=run(rollups);bindWorkflow(candidate,Map.of("matchRules",List.of(pending())));
var proof=UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId());assertNotNull(proof);
assertEquals(Set.of(black,bright),proof.buildIds());assertTrue(proof.changedMonths().isEmpty());
jdbc.update("UPDATE data_release SET rule_version='changed-rule' WHERE id=?",candidate.releaseId());
assertNull(UnclassifiedSnapshotReuse.verify(jdbc,json,candidate.releaseId()));
}
}