Files
quality_analyse/scripts/verify_dsh_formal.py
T
lyun f1a2fbd494 统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
2026-11-12 11:57:41 +08:00

625 lines
36 KiB
Python

#!/usr/bin/env python3
"""Start a private, synthetic release for formal DSH acceptance.
Neither mode calls a model. ``--check-fixture`` verifies the seeded data using
the same authenticated HTTP APIs used by the product; ``--serve`` leaves the
isolated backend running for a later browser-driven question.
"""
from __future__ import annotations
import argparse
from dataclasses import dataclass
import hashlib
import json
import os
from pathlib import Path
import secrets
import sys
import time
import traceback
from datetime import datetime, timedelta, timezone
import xml.etree.ElementTree as ET
from zipfile import ZIP_DEFLATED, ZipFile
from urllib.parse import urlencode
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
sys.path.insert(0, str(ROOT / 'verification'))
import project
from build_dsh_application import build as build_dsh_application
from isolated_runtime import LocalApi, isolated_api, sql
PERIODS = ('2026-08', '2026-09', '2026-10')
PRODUCTS = tuple(f'合成产品{i:02d}' for i in range(1, 7))
DEPARTMENTS = ('合成组织甲', '合成组织乙')
GROUPS = ('合成分组甲', '合成分组乙')
PROJECT_GROUPS = ('合成项目组甲', '合成项目组乙')
BLACK_SCRAP = (8, 12, 5, 14, 7, 11)
BRIGHT_SCRAP = (10, 6, 12, 9, 4, 13)
@dataclass(frozen=True)
class FixtureRow:
stage: str
month: str
department: str
group: str
project_group: str
product: str
qualified: int
scrap: int
components: dict[str, int]
raw: dict[str, object]
def fixture_rows() -> list[FixtureRow]:
rows: list[FixtureRow] = []
for month_index, month in enumerate(PERIODS):
for stage in ('black', 'bright'):
baseline = BLACK_SCRAP if stage == 'black' else BRIGHT_SCRAP
for product_index, product in enumerate(PRODUCTS):
department_index = 0 if product_index < 3 else 1
scrap = baseline[product_index] + month_index * (product_index % 3 + 1)
qualified = 1000 + product_index * 40 + month_index * 25 + (10 if stage == 'bright' else 0)
raw: dict[str, object] = {
'合成来源标记': f'SYNTH-{month}-{stage}-{product_index + 1:02d}',
'来源备注': f'仅供本地验收的合成记录 {month} {stage} {product}',
}
if stage == 'black':
responsibility = scrap // 2
material = scrap // 4
external = scrap - responsibility - material
components = {
'black_responsibility_scrap': responsibility,
'black_material_scrap': material,
'black_external_scrap': external,
}
raw.update({
'责废缺角重量': 0,
'责废尺寸重量': None,
'责废其他重量': 0,
'料废尺寸重量': material,
'黑片外废合计重量': external,
})
if (month, product) != ('2026-09', '合成产品06'):
raw['责废尺寸重量'] = responsibility
else:
plating = scrap * 2 // 5
material = scrap // 5
processing = scrap * 3 // 10
diffusion = scrap - plating - material - processing
components = {
'bright_plating_scrap': plating,
'bright_material_scrap': material,
'bright_processing_scrap': processing,
'bright_diffusion_scrap': diffusion,
}
raw.update({
'镀废缺角重量': plating,
'材料缺角重量': material,
'加工缺角重量': processing,
'晶界扩散外观异常重量': diffusion,
'镀废重量合计': plating,
'材料废重量合计': material,
'加工废重量合计': processing,
'晶界扩散报废合计重量': diffusion,
})
rows.append(FixtureRow(stage, month, DEPARTMENTS[department_index], GROUPS[department_index],
PROJECT_GROUPS[department_index], product, qualified, scrap, components, raw))
return rows
def fixture_totals(rows: list[FixtureRow], stage: str, month: str) -> dict[str, int]:
selected = [row for row in rows if row.stage == stage and row.month == month]
return {'qualified': sum(row.qualified for row in selected), 'scrap': sum(row.scrap for row in selected),
'total': sum(row.qualified + row.scrap for row in selected), 'rows': len(selected)}
def source_physical_rows(rows: list[FixtureRow]):
positions: dict[tuple[str, str], int] = {}
for row in rows:
key = (row.stage, row.month)
positions[key] = positions.get(key, 1) + 1
yield row, positions[key]
def _literal(value: str) -> str:
return 'CONVERT(0x' + value.encode('utf-8').hex() + ' USING utf8mb4)'
def _json_sql(value: object) -> str:
encoded = json.dumps(value, ensure_ascii=False, separators=(',', ':'))
return 'CAST(' + _literal(encoded) + ' AS JSON)'
def _column_name(index: int) -> str:
name = ''
while index:
index, remainder = divmod(index - 1, 26)
name = chr(65 + remainder) + name
return name
def _write_workbook(path: Path, rows: list[FixtureRow]) -> tuple[str, int]:
headers = ['序号', *sorted({key for row in rows for key in row.raw})]
namespace = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
rel_namespace = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
package_namespace = 'http://schemas.openxmlformats.org/package/2006/relationships'
content_namespace = 'http://schemas.openxmlformats.org/package/2006/content-types'
workbook = ET.Element(f'{{{namespace}}}workbook')
sheets = ET.SubElement(workbook, f'{{{namespace}}}sheets')
ET.SubElement(sheets, f'{{{namespace}}}sheet', {'name': '合成质量数据', 'sheetId': '1', f'{{{rel_namespace}}}id': 'rId1'})
rels = ET.Element(f'{{{package_namespace}}}Relationships')
ET.SubElement(rels, f'{{{package_namespace}}}Relationship', {
'Id': 'rId1', 'Type': rel_namespace + '/worksheet', 'Target': 'worksheets/sheet1.xml'})
types = ET.Element(f'{{{content_namespace}}}Types')
ET.SubElement(types, f'{{{content_namespace}}}Default', {'Extension': 'rels', 'ContentType': 'application/vnd.openxmlformats-package.relationships+xml'})
ET.SubElement(types, f'{{{content_namespace}}}Default', {'Extension': 'xml', 'ContentType': 'application/xml'})
ET.SubElement(types, f'{{{content_namespace}}}Override', {'PartName': '/xl/workbook.xml', 'ContentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'})
ET.SubElement(types, f'{{{content_namespace}}}Override', {'PartName': '/xl/worksheets/sheet1.xml', 'ContentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'})
worksheet = ET.Element(f'{{{namespace}}}worksheet')
sheet_data = ET.SubElement(worksheet, f'{{{namespace}}}sheetData')
values = [headers]
for ordinal, row in enumerate(rows, start=1):
values.append([ordinal, *(row.raw.get(header) for header in headers[1:])])
for row_index, values_row in enumerate(values, start=1):
row_element = ET.SubElement(sheet_data, f'{{{namespace}}}row', {'r': str(row_index)})
for column_index, value in enumerate(values_row, start=1):
if value is None:
continue
reference = f'{_column_name(column_index)}{row_index}'
cell = ET.SubElement(row_element, f'{{{namespace}}}c', {'r': reference})
if isinstance(value, (int, float)) and not isinstance(value, bool):
ET.SubElement(cell, f'{{{namespace}}}v').text = str(value)
else:
cell.set('t', 'inlineStr')
inline = ET.SubElement(cell, f'{{{namespace}}}is')
ET.SubElement(inline, f'{{{namespace}}}t').text = str(value)
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
with ZipFile(path, 'w', ZIP_DEFLATED) as archive:
archive.writestr('[Content_Types].xml', ET.tostring(types, encoding='utf-8', xml_declaration=True))
archive.writestr('_rels/.rels', ET.tostring(_package_root(
package_namespace, 'rId1', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 'xl/workbook.xml'),
encoding='utf-8', xml_declaration=True))
archive.writestr('xl/workbook.xml', ET.tostring(workbook, encoding='utf-8', xml_declaration=True))
archive.writestr('xl/_rels/workbook.xml.rels', ET.tostring(rels, encoding='utf-8', xml_declaration=True))
archive.writestr('xl/worksheets/sheet1.xml', ET.tostring(worksheet, encoding='utf-8', xml_declaration=True))
os.chmod(path, 0o600)
return hashlib.sha256(path.read_bytes()).hexdigest(), path.stat().st_size
def _package_root(namespace: str, relation_id: str, relation_type: str, target: str) -> ET.Element:
root = ET.Element(f'{{{namespace}}}Relationships')
ET.SubElement(root, f'{{{namespace}}}Relationship', {
'Id': relation_id, 'Type': relation_type, 'Target': target})
return root
def seed_fixture(database: str, archive_root: Path) -> dict[str, object]:
rows = fixture_rows()
release_id = str(uuid.uuid4())
build_by_source: dict[tuple[str, str], tuple[str, str, str]] = {}
for stage in ('black', 'bright'):
for month in PERIODS:
document_id, revision_id, build_id = (str(uuid.uuid4()) for _ in range(3))
source_name = f'SYNTH-DSH-{stage}-{month}'
source_rows = [row for row in rows if row.stage == stage and row.month == month]
archive_file = archive_root / f'{source_name}.xlsx'
digest, file_size = _write_workbook(archive_file, source_rows)
source_file = archive_file.name
sql(f"INSERT INTO source_document(id,source_slot,stage,source_year,source_month,scope_code,source_file,source_mode) "
f"VALUES({_literal(document_id)},{_literal('dsh-formal:' + stage + ':' + month)},{_literal(stage)},2026,{int(month[-2:])},'SYNTHETIC',"
f"{_literal(source_file)},'MONTHLY_SNAPSHOT');", database)
sql(f"INSERT INTO source_revision(id,source_document_id,sha256,source_file,archive_path,file_size,content_type,parser_version,status,sheet_count) "
f"VALUES({_literal(revision_id)},{_literal(document_id)},{_literal(digest)},{_literal(source_file)},"
f"{_literal(str(archive_file.resolve()))},{file_size},'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','dsh-formal-synthetic-v1','ACCEPTED',1);", database)
sql(f"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) "
f"VALUES({_literal(build_id)},{_literal(revision_id)},{_literal(hashlib.sha256((source_name + '-build').encode()).hexdigest())},"
f"'dsh-formal-synthetic-v1','pq-dax-v1','observed-2026-v1','COMPLETED',6,0,CAST('{{}}' AS JSON),CAST('[]' AS JSON),CURRENT_TIMESTAMP(6),CURRENT_TIMESTAMP(6));", database)
build_by_source[(stage, month)] = (document_id, revision_id, build_id)
sql(f"INSERT INTO data_release(id,name,status,rule_version,mapping_version,complete,validation,published_at) "
f"VALUES({_literal(release_id)},'DSH纯合成验收版本','PUBLISHED','pq-dax-v1','observed-2026-v1',TRUE,"
f"CAST('{{\"basis\":\"synthetic-only\",\"sourceRows\":36}}' AS JSON),CURRENT_TIMESTAMP(6));", database)
for (stage, month), (document_id, revision_id, build_id) in build_by_source.items():
sql(f"INSERT INTO release_member(release_id,source_document_id,source_revision_id,transform_build_id) "
f"VALUES({_literal(release_id)},{_literal(document_id)},{_literal(revision_id)},{_literal(build_id)});", database)
for row, row_no in source_physical_rows(rows):
_, revision_id, build_id = build_by_source[(row.stage, row.month)]
all_components = {name: None for name in (
'black_responsibility_scrap', 'black_material_scrap', 'black_external_scrap',
'bright_headquarters_scrap', 'bright_plating_scrap', 'bright_material_scrap',
'bright_processing_scrap', 'bright_diffusion_scrap')}
all_components.update(row.components)
all_components.update({'categoryMappingStatus': 'mapped', 'projectGroupStatus': 'mapped'})
stage_table = 'fact_' + row.stage
sql(f"INSERT INTO {stage_table}(build_id,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,project_group,product,"
f"qualified_weight,scrap_weight,total_weight,raw_values,components) VALUES({_literal(build_id)},{_literal(revision_id)},"
f"'合成质量数据',{row_no},{_literal(row.month)},{_literal(row.department)},{_literal(row.group)},{_literal(row.project_group)},"
f"{_literal(row.product)},{row.qualified},{row.scrap},{row.qualified + row.scrap},{_json_sql(row.raw)},{_json_sql(all_components)});", database)
for code, amount in row.components.items():
source_field = next((key for key, value in row.raw.items() if isinstance(value, int) and value == amount), '合成报废分量')
sql(f"INSERT INTO defect_component(build_id,stage,sheet_name,physical_row_no,defect_name,weight,source_field) "
f"VALUES({_literal(build_id)},{_literal(row.stage)},'合成质量数据',{row_no},{_literal(code)},{amount},{_literal(source_field)});", database)
for stage in ('black', 'bright'):
table = 'fact_' + stage
for month in PERIODS:
build_id = build_by_source[(stage, month)][2]
component_names = ('black_responsibility_scrap', 'black_material_scrap', 'black_external_scrap',
'bright_headquarters_scrap', 'bright_plating_scrap', 'bright_material_scrap',
'bright_processing_scrap', 'bright_diffusion_scrap')
component_expr = ','.join(
"'" + name + "',SUM(CASE WHEN JSON_EXTRACT(f.components,'$." + name + "') IS NULL "
"OR JSON_TYPE(JSON_EXTRACT(f.components,'$." + name + "'))='NULL' THEN NULL "
"ELSE CAST(JSON_UNQUOTE(JSON_EXTRACT(f.components,'$." + name + "')) AS DECIMAL(38,8)) END)"
for name in component_names)
sql(f"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) "
f"SELECT f.build_id,{_literal(stage)},f.month_key,f.department,f.group_name,f.project_group,f.product,SUM(f.qualified_weight),SUM(f.scrap_weight),SUM(f.total_weight),"
f"JSON_OBJECT({component_expr}),COUNT(*),SUM(CASE WHEN f.group_name IS NULL OR f.group_name='' THEN 1 ELSE 0 END),"
f"SUM(CASE WHEN JSON_UNQUOTE(JSON_EXTRACT(f.components,'$.categoryMappingStatus'))='unmapped' THEN 1 ELSE 0 END),"
f"SUM(CASE WHEN JSON_UNQUOTE(JSON_EXTRACT(f.components,'$.projectGroupStatus'))='unmapped' THEN 1 ELSE 0 END) "
f"FROM {table} f WHERE f.build_id={_literal(build_id)} GROUP BY f.build_id,f.month_key,f.department,f.group_name,f.project_group,f.product;", database)
sql(f"INSERT INTO metric_rollup_build(build_id,source_row_count) SELECT {_literal(build_id)},COUNT(*) FROM {table} WHERE build_id={_literal(build_id)};", database)
sql(f"UPDATE current_release SET release_id={_literal(release_id)},pointer_version=pointer_version+1 WHERE dataset_id='quality';", database)
return {'releaseId': release_id, 'rows': rows, 'buildCount': len(build_by_source),
'sourceCount': len(build_by_source), 'productCount': len(PRODUCTS), 'monthCount': len(PERIODS)}
class FixtureApiFailure(RuntimeError):
def __init__(self, endpoint: str, status: int, error_code: str | None = None):
super().__init__('Synthetic acceptance API check failed.')
self.endpoint = endpoint
self.status = status
self.error_code = error_code
class FixtureCheckFailure(RuntimeError):
def __init__(self, message: str, details: dict[str, object]):
super().__init__(message)
self.details = details
def _request(api: LocalApi, path: str, payload: dict | None = None, method: str | None = None) -> dict:
status, result, _ = api.call(path, payload, method)
if status != 200 or not isinstance(result, dict):
code = result.get('code') if isinstance(result, dict) else None
raise FixtureApiFailure(path.split('?', 1)[0], status, code if isinstance(code, str) else None)
return result
def check_fixture(api: LocalApi, fixture: dict[str, object], port: int) -> dict[str, int]:
release_id = str(fixture['releaseId'])
totals = fixture_totals(fixture['rows'], 'black', '2026-10')
settings = _request(api, '/api/analysis/settings')
if settings.get('runtimeEngine') != 'DSH':
raise RuntimeError('The isolated analysis engine setting is not DSH.')
metric = _request(api, '/api/metrics?' + urlencode({'releaseId': release_id, 'year': 2026, 'month': 10,
'stage': 'black', 'view': 'overall'}))
black = metric.get('summary', {}).get('black') or {}
if int(black.get('qualifiedWeight') or 0) != totals['qualified'] or int(black.get('scrapWeight') or 0) != totals['scrap']:
raise RuntimeError('The formal monthly rate source values did not match the fixture.')
trend = _request(api, '/api/metrics/trend?' + urlencode({'releaseId': release_id, 'year': 2026,
'monthStart': 8, 'monthEnd': 10, 'stage': 'black', 'view': 'overall', 'trendGranularity': 'month'}))
points = trend.get('trend') or []
if len(points) != 3:
raise RuntimeError('The monthly trend did not return all three synthetic periods.')
comparison = _request(api, '/api/analysis/changes', {
'releaseId': release_id, 'period': {'year': 2026, 'month': 10},
'comparisonPeriod': {'year': 2026, 'month': 9}, 'filters': {'includeUnassigned': True},
'dimension': 'product', 'stage': 'black', 'sort': 'scrap_delta', 'limit': 6,
'fields': ['rate', 'scrapWeight'],
})
if comparison.get('totalRows') != 6 or not comparison.get('rows'):
raise RuntimeError('The two-period product comparison did not return its formal ranking.')
ranking = _request(api, '/api/metrics?' + urlencode({'releaseId': release_id, 'year': 2026,
'month': 10, 'view': 'product', 'includeUnassigned': 'true'}))
ranking_rows = ranking.get('rows') or []
expected_scrap = {product: sum(row.scrap for row in fixture['rows']
if row.product == product and row.month == '2026-10') for product in PRODUCTS}
actual_scrap = {}
for item in ranking_rows:
black_stage = item.get('black') or {}
bright_stage = item.get('bright') or {}
actual_scrap[item.get('product')] = (float(black_stage.get('scrapWeight') or 0)
+ float(bright_stage.get('scrapWeight') or 0))
actual_order = sorted(actual_scrap, key=lambda product: (-actual_scrap[product], product or ''))
expected_order = sorted(expected_scrap, key=lambda product: (-expected_scrap[product], product))
if len(ranking_rows) != 6 or actual_order != expected_order or actual_scrap != expected_scrap:
raise RuntimeError('The combined-stage product scrap values did not match the synthetic ranking.')
details = _request(api, '/api/analysis/tools/details', {
'releaseId': release_id, 'year': 2026, 'month': 10, 'stage': 'black', 'limit': 5,
'department': DEPARTMENTS[0], 'group': GROUPS[0], 'product': PRODUCTS[0],
})
if not details.get('rows'):
raise RuntimeError('The bounded source detail query is empty.')
defects = _request(api, '/api/analysis/defects', {
'releaseId': release_id, 'mode': 'top', 'year': 2026, 'month': 9, 'stage': 'black',
'dimension': 'defect', 'category': '责废', 'limit': 20,
})
if not defects.get('rows') or defects.get('sourceRows') != 6:
raise RuntimeError('The synthetic fine-defect or missing-coverage query is empty.')
records = _request(api, '/api/records?' + urlencode({
'releaseId': release_id, 'stage': 'black', 'year': 2026, 'month': 10,
'product': PRODUCTS[0], 'page': 1, 'pageSize': 5,
}))
if not records.get('rows'):
raise RuntimeError('The synthetic original-record query is empty.')
source_audit = _request(api, '/api/analysis/investigate', {
'releaseId': release_id, 'year': 2026, 'month': 9, 'stage': 'black', 'category': '责废',
'product': '合成产品06', 'includeUnassigned': True, 'action': 'source',
})
proof = source_audit.get('sourceVerification') or {}
if not (proof.get('complete') and proof.get('allSelectedValuesMatchStored')
and proof.get('expectedRows') == 1 and proof.get('verifiedRows') == 1):
raise FixtureCheckFailure('The synthetic original workbook did not match its source-row audit.', {
key: proof.get(key) for key in ('complete', 'allSelectedValuesMatchStored', 'expectedRows',
'verifiedRows', 'missingRows', 'mismatchRows')
} | {'files': [{key: file.get(key) for key in ('complete', 'archiveFingerprintMatches',
'expectedRows', 'verifiedRows', 'missingRows', 'mismatchRows')}
for file in proof.get('files', [])]})
coverage = next((field.get('sourceCoverage') for source_file in proof.get('files', [])
for field in source_file.get('fields', []) if field.get('field') == '责废尺寸重量'), {})
if not coverage or coverage.get('nullOrBlankRows') != 1:
raise RuntimeError('The original workbook audit did not preserve the explicit missing-detail coverage.')
member_page = _request(api, '/api/analysis/source/members', {
'releaseId': release_id, 'period': {'year': 2026, 'month': 10},
'filters': {'department': DEPARTMENTS[0], 'group': GROUPS[0], 'product': PRODUCTS[0]},
'memberFields': ['product', 'department', 'group', 'projectGroup'],
'stages': ['black', 'bright'], 'limit': 10, 'offset': 0,
})
if not member_page.get('rows'):
raise RuntimeError('The synthetic source-member page is empty.')
source_catalog = _request(api, '/api/analysis/source/catalog?' + urlencode({'releaseId': release_id}))
source_field = next((item.get('key') for item in source_catalog.get('sourceFields', [])
if (item.get('columns') or {}).get('black') == '来源备注'), None)
if not source_field:
raise RuntimeError('The synthetic original source field is absent from the published field catalog.')
source_values = _request(api, '/api/analysis/source/values', {
'releaseId': release_id, 'period': {'year': 2026, 'month': 10},
'filters': {'department': DEPARTMENTS[0], 'group': GROUPS[0], 'product': PRODUCTS[0]},
'field': source_field, 'stages': ['black'], 'limit': 10, 'offset': 0,
})
if PRODUCTS[0] not in json.dumps(source_values, ensure_ascii=False):
raise RuntimeError('The original synthetic source-field lookup did not return its matching value.')
no_data = _request(api, '/api/metrics?' + urlencode({'releaseId': release_id, 'year': 2026, 'month': 10,
'product': '不存在的合成产品', 'stage': 'black', 'view': 'product'}))
if no_data.get('rows'):
raise RuntimeError('The no-data query unexpectedly returned fixture rows.')
session_result = _request(api, '/api/analysis/sessions', {
'title': 'DSH合成验收会话', 'releaseId': release_id,
'context': {'year': 2026, 'month': 10, 'view': 'overall', 'stage': 'black'},
})
session = session_result.get('session') or {}
session_id = session.get('id')
if not session_id or session.get('releaseId') != release_id:
raise RuntimeError('Formal analysis session creation did not pin the synthetic release.')
history = _request(api, '/api/analysis/sessions')
if not any(item.get('id') == session_id for item in history.get('items', [])):
raise RuntimeError('The formal synthetic analysis session was not available in history.')
reports = _request(api, '/api/analysis/reports')
plans = _request(api, '/api/analysis/report-plans')
unauth = LocalApi(port)
status, _, _ = unauth.call('/api/analysis/reports')
if status not in (401, 403):
raise RuntimeError('The periodic report authorization boundary was not enforced.')
plan = _request(api, '/api/analysis/report-plans', {
'periodType': 'MONTH', 'runDay': 1, 'runHour': 9, 'enabled': False,
})
if not (plan.get('plan') or {}).get('id'):
raise RuntimeError('A disabled synthetic report plan could not be created.')
if not isinstance(reports.get('items'), list) or not isinstance(plans.get('items'), list):
raise RuntimeError('The report history or periodic schedule list is unavailable.')
return {'monthlyMetrics': 1, 'trendPoints': len(points), 'comparisonRows': comparison['totalRows'],
'rankingRows': len(ranking_rows), 'detailRows': len(details['rows']),
'defectRows': len(defects['rows']), 'recordRows': len(records['rows']),
'sourceAuditRows': proof['verifiedRows'], 'sourceMembers': len(member_page['rows']),
'sourceValues': len(source_values.get('values', [])),
'sessions': len(history.get('items', [])), 'reportChecks': 3}
def _authorize_synthetic_evidence(api: LocalApi, user_id: str, release_id: str) -> int:
audit_request = {
'releaseId': release_id, 'year': 2026, 'month': 9, 'stage': 'black', 'category': '责废',
'product': '合成产品06', 'includeUnassigned': True, 'action': 'source',
}
denied_status, _, _ = api.call('/api/analysis/investigate', audit_request)
if denied_status != 403:
raise RuntimeError('The unprivileged synthetic identity did not receive the expected evidence denial.')
policy_result = _request(api, f'/api/governance/users/{user_id}/access-policy')
policy = policy_result.get('policy') or {}
fields = policy.get('allowedFields')
if not isinstance(fields, list) or not fields:
fields = ['*']
updated = _request(api, f'/api/governance/users/{user_id}/access-policy', {
'detailAllowed': True, 'evidenceAllowed': True, 'originalDownloadAllowed': False,
'allowedFields': fields, 'expectedRowVersion': int(policy.get('rowVersion') or 0),
'reason': '仅用于随机租约内的纯合成原件验收',
'validUntil': (datetime.now(timezone.utc) + timedelta(hours=2)).isoformat(),
'inheritDepartmentChildren': False,
}, method='PUT')
if not (updated.get('policy') or {}).get('evidenceAllowed'):
raise RuntimeError('The synthetic-only evidence access policy was not applied.')
return denied_status
def _write_private(path: Path, value: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
path.parent.chmod(0o700)
payload = json.dumps(value, ensure_ascii=False, indent=2) + '\n'
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, 'w', encoding='utf-8') as stream:
stream.write(payload)
path.chmod(0o600)
def _private_model_copy(directory: Path) -> Path:
selected = os.environ.get('QUALITY_ANALYSIS_MODEL_CONFIG_FILE')
source = Path(selected) if selected else ROOT / '.runtime/quality-model-config.json'
if not selected and not source.exists():
# Read the local comparison-era settings once to populate an isolated
# DSH fixture. The staged worker only receives the new private copy.
source = ROOT / '.runtime/nao-model-config.json'
if source.is_symlink() or not source.is_file() or source.stat().st_size > 32768:
raise RuntimeError('A private model configuration is not available for isolated DSH selection.')
try:
configuration = json.loads(source.read_text(encoding='utf-8'))
except (OSError, UnicodeError, json.JSONDecodeError):
raise RuntimeError('The private model configuration could not be copied safely.') from None
if not isinstance(configuration, dict):
raise RuntimeError('The private model configuration is invalid.')
configuration.pop('runtimeEngine', None)
target = directory / 'quality-model-config.json'
_write_private(target, configuration)
return target
def _new_run_directory() -> Path:
base = ROOT / 'output/dsh-formal-20261110/private'
base.mkdir(parents=True, exist_ok=True, mode=0o700)
base.chmod(0o700)
directory = base / ('run-' + uuid.uuid4().hex[:12])
directory.mkdir(mode=0o700)
return directory
def _wait_for_browser() -> None:
print('Isolated DSH fixture is serving; private access details are saved in the run directory.', flush=True)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
def run(*, serve: bool, check: bool, port: int) -> int:
if port not in (5281, 5283, 5284, 5285, 5287):
raise ValueError('Choose an isolated project loopback port.')
directory = _new_run_directory()
selected = os.environ.get('QUALITY_ANALYSIS_RUNTIME_SCRIPT')
if selected:
script = Path(selected).absolute()
else:
application = directory / 'runtime-app'
build_dsh_application(application)
script = application / 'scripts/quality_analysis_runtime.py'
model_config = _private_model_copy(directory)
username = 'dshqa_' + secrets.token_hex(5)
password = secrets.token_urlsafe(24)
auth_credentials = (username, password)
overrides = {
'QUALITY_ANALYSIS_MODEL_CONFIG_FILE': str(model_config),
'QUALITY_ANALYSIS_RUNTIME_SCRIPT': str(script),
'QUALITY_ANALYSIS_STATE_DIR': str(directory / 'quality-analysis'),
'QUALITY_ANALYSIS_PROTOCOL_V1_ENABLED': 'true',
'QUALITY_ANALYSIS_AUTONOMOUS_ENABLED': 'true',
'QUALITY_ARCHIVE_ROOT': str((directory / 'archive').resolve()),
'SERVER_SERVLET_SESSION_COOKIE_NAME': 'QUALITY_DSH_ACCEPTANCE',
}
private_access = directory / 'access.json'
public_summary = ROOT / 'output/dsh-formal-20261110/summary.json'
public_summary.parent.mkdir(parents=True, exist_ok=True)
private_summary = {'status': 'RUNNING', 'syntheticOnly': True, 'checks': {}}
try:
with isolated_api(port=port, fresh=True, evidence_dir=directory, env_overrides=overrides,
auth_credentials=auth_credentials) as (api, database, evidence):
status, user_state, _ = api.call('/api/auth/me')
if status != 200 or not isinstance(user_state, dict):
raise FixtureApiFailure('/api/auth/me', status)
current_password = password
if (user_state.get('user') or {}).get('forcePasswordChange'):
current_password = secrets.token_urlsafe(24)
status, _, _ = api.call('/api/auth/change-password', {
'currentPassword': password, 'newPassword': current_password,
})
if status != 200:
raise FixtureApiFailure('/api/auth/change-password', status)
archive_root = directory / 'archive'
archive_root.mkdir(mode=0o700)
fixture = seed_fixture(database, archive_root)
user_id = str((user_state.get('user') or {}).get('id') or '')
denied_evidence_status = _authorize_synthetic_evidence(api, user_id, fixture['releaseId'])
access = {
'url': api.base, 'username': username, 'password': current_password,
'database': database, 'releaseId': fixture['releaseId'], 'runtimeEngine': 'DSH',
'modelConfigPath': str(model_config), 'browserFrontendApiTarget': api.base,
'note': 'Synthetic isolated acceptance login. No source login token is stored here.',
}
_write_private(private_access, access)
counts = check_fixture(api, fixture, port) if check else {}
counts['unauthorizedSourceDenied'] = 1
private_summary.update({'status': 'PASS' if check else 'READY', 'checks': counts,
'syntheticRows': len(fixture['rows']), 'sourceDocuments': fixture['sourceCount'],
'metricRollupBuilds': fixture['buildCount'], 'monthCount': fixture['monthCount'],
'productCount': fixture['productCount'], 'unauthorizedEvidenceStatus': denied_evidence_status,
'served': serve})
if serve:
_wait_for_browser()
private_summary['cleanup'] = evidence.get('cleanup', {})
if private_summary.get('cleanup') and not all(private_summary['cleanup'].values()):
private_summary['status'] = 'CLEANUP_INCOMPLETE'
_write_private(directory / 'result.json', private_summary)
public_summary.write_text(json.dumps({'status': private_summary['status'], 'syntheticOnly': True,
'checks': counts, 'syntheticRows': private_summary.get('syntheticRows', 0)},
ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
public_summary.chmod(0o600)
print(('PASS' if check else 'READY') + ': synthetic DSH fixture ' +
('checks passed; ' if check else 'backend served; ') +
'private access details are under output/dsh-formal-20261110/private.', flush=True)
return 0
except KeyboardInterrupt:
print('DSH synthetic fixture stopped; isolated resources are being cleaned.', flush=True)
return 130
except Exception as failure:
safe = {'status': 'ERROR', 'errorType': type(failure).__name__, 'syntheticOnly': True}
frames = traceback.extract_tb(failure.__traceback__)
if frames:
safe['errorLocation'] = {'file': Path(frames[-1].filename).name,
'function': frames[-1].name, 'line': frames[-1].lineno}
if isinstance(failure, FixtureApiFailure):
safe.update({'failedEndpoint': failure.endpoint, 'httpStatus': failure.status})
if failure.error_code:
safe['errorCode'] = failure.error_code
if isinstance(failure, FixtureCheckFailure):
safe['checkDetails'] = failure.details
_write_private(directory / 'result.json', safe)
public_summary.write_text(json.dumps(safe, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
public_summary.chmod(0o600)
print('ERROR: the synthetic DSH fixture could not complete (' + type(failure).__name__ + ').', file=sys.stderr)
return 1
def main(argv=None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
modes = parser.add_mutually_exclusive_group(required=True)
modes.add_argument('--check-fixture', action='store_true', help='seed a fresh isolated schema and verify formal API values without calling a model')
modes.add_argument('--serve', action='store_true', help='keep a fresh synthetic backend available for later browser-driven questions')
parser.add_argument('--port', type=int, default=5285)
args = parser.parse_args(argv)
if args.check_fixture and args.serve:
parser.error('Choose only one mode.')
return run(serve=args.serve, check=args.check_fixture, port=args.port)
if __name__ == '__main__':
raise SystemExit(main())