Files
quality_analyse/verification/test_nao_history_report.py
T
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

653 lines
40 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import copy
import json
import sys
import tempfile
import threading
import unittest
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from unittest.mock import patch
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts'), str(Path(__file__).parent)]
from nao_history_report import (HistorySnapshotError, execute_history_report,
select_history_headline, validate_history_snapshot,
_validate_model_selection)
from nao_model_gate import ModelGate
from nao_protocol_execution import execute
RELEASE = 'release-2026-09'
ATTEMPT = '11111111-1111-4111-8111-111111111111'
SESSION = '22222222-2222-4222-8222-222222222222'
TURN = '33333333-3333-4333-8333-333333333333'
SOURCE_A = '44444444-4444-4444-8444-444444444444'
SOURCE_B = '55555555-5555-4555-8555-555555555555'
def report_task(source_ids=(SOURCE_A, SOURCE_B)):
return {
'version': 1, 'kind': 'report', 'historyMode': 'all', 'freshQuery': False,
'goal': '整合已确认历史质量结果', 'dimensions': [], 'stages': [], 'fields': [],
'comparisonRequired': False, 'trendRequired': False, 'verifySource': False,
'fullDetails': False, 'filters': {}, 'period': None, 'comparisonPeriod': None,
'comparisonMode': None, 'dimensionStages': {},
'historySourceTurnIds': list(source_ids), 'needsClarification': False,
}
def authority(task=None):
return {'attemptId': ATTEMPT, 'sessionId': SESSION, 'turnId': TURN,
'revision': 3, 'executionId': '66666666-6666-4666-8666-666666666666',
'task': copy.deepcopy(task if task is not None else report_task())}
def snapshot(*, unavailable_second=False, facts=None):
sources = [
{'turnId': SOURCE_A, 'revision': 2, 'executionId': 'execution-a',
'status': 'COMPLETED', 'available': True, 'queryIds': ['query-a']},
{'turnId': SOURCE_B, 'revision': 1, 'executionId': 'execution-b',
'status': 'PARTIAL' if unavailable_second else 'COMPLETED',
'available': not unavailable_second,
**({'revision': None, 'executionId': None} if unavailable_second else {'queryIds': ['query-b']}),
**({'reason': '2025年9月海外组没有可用历史来源。'} if unavailable_second else {})},
]
sections = [{
'title': '2026年9月黑片正式报废重量', 'sourceTurnId': SOURCE_A,
'period': {'year': 2026, 'month': 9},
'filters': {'extraFilters': {'customerCode': ['800001']},
'columnFilters': [{'field': 'orderNo', 'stages': ['black'],
'operator': 'in', 'values': ['WO-1']}]},
'stage': 'black',
'columns': [{'key': 'label', 'label': '对象'},
{'key': 'scrapWeight', 'label': '报废重量(kg)'}],
'rows': [{'label': '整体', 'scrapWeight': 1163.6817}],
'note': '历史正式查询结果,本轮未重新计算',
}]
if not unavailable_second:
sections.append({
'title': '2025年9月亮片正式报废重量', 'sourceTurnId': SOURCE_B,
'period': {'year': 2025, 'month': 9}, 'filters': {'group': '汽车交通-海外'},
'stage': 'bright',
'columns': [{'key': 'label', 'label': '对象'},
{'key': 'scrapWeight', 'label': '报废重量(kg)'}],
'rows': [{'label': '整体', 'scrapWeight': 17.25}],
})
return {
'kind': 'protocol_history_snapshot', 'version': 1, 'releaseId': RELEASE,
'historySources': sources, 'sections': sections,
'facts': facts if facts is not None else [
{'id': 'history-fact:F1', 'text': '2026年9月,客户800001,黑片正式报废重量1163.6817 kg。',
'sourceTurnId': SOURCE_A},
*([] if unavailable_second else [{
'id': 'history-fact:F2',
'text': '2025年9月,汽车交通-海外亮片正式报废重量17.25 kg。',
'sourceTurnId': SOURCE_B,
}]),
],
'historyCoverage': {
'complete': not unavailable_second, 'expectedSources': 2,
'importedSources': 1 if unavailable_second else 2,
'missingEvidence': ['2025年9月一条确认历史来源暂不可用。'] if unavailable_second else [],
},
'historyEvidence': {
'importId': 'import-7', 'attemptId': ATTEMPT, 'sessionId': SESSION,
'turnId': TURN, 'revision': 3,
'executionId': '66666666-6666-4666-8666-666666666666',
'snapshotHash': 'sha256:' + 'a' * 64,
},
}
class FakeDelegated:
def __init__(self, response):
self.response = response
self.calls = []
def call(self, path, payload=None, method=None):
self.calls.append((path, copy.deepcopy(payload), method))
if isinstance(self.response, Exception):
raise self.response
return copy.deepcopy(self.response)
class HistorySnapshotValidationTest(unittest.TestCase):
def test_first_mode_keeps_server_confirmed_sources_and_identity_checks(self):
task = report_task((SOURCE_A,)); task['historyMode'] = 'first'
candidate = snapshot()
candidate['historySources'] = candidate['historySources'][:1]
candidate['sections'] = candidate['sections'][:1]
candidate['facts'] = candidate['facts'][:1]
candidate['historyCoverage'].update(expectedSources=1, importedSources=1)
self.assertEqual(validate_history_snapshot(candidate, task=task, authority=authority(task),
release_id=RELEASE, request_turn_id=TURN)['historySources'][0]['turnId'], SOURCE_A)
wrong = copy.deepcopy(candidate); wrong['historySources'][0]['turnId'] = SOURCE_B
with self.assertRaises(HistorySnapshotError):
validate_history_snapshot(wrong, task=task, authority=authority(task), release_id=RELEASE, request_turn_id=TURN)
with self.assertRaises(HistorySnapshotError):
validate_history_snapshot(candidate, task=task, authority=authority(), release_id=RELEASE, request_turn_id=TURN)
def test_autonomous_helper_rejects_unvalidated_identity_before_model_request(self):
from nao_native_answer import synthesize_history_snapshot
candidate = snapshot()
candidate['historyEvidence']['turnId'] = SOURCE_A
with patch('nao_evidence_finalizer._open_model') as model, self.assertRaises(HistorySnapshotError):
synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='汇总', snapshot=candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event())
model.assert_not_called()
def test_all_published_source_period_is_exact_and_not_a_calendar_override(self):
candidate = snapshot()
candidate['sections'][0]['period'] = {'allPublished': True}
validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
for period in ({'allPublished': False}, {'allPublished': 1},
{'allPublished': True, 'year': 2026}):
candidate['sections'][0]['period'] = period
with self.subTest(period=period), self.assertRaises(HistorySnapshotError):
validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
def test_rejects_wrong_source_release_and_identity(self):
task = report_task()
auth = authority(task)
base = snapshot()
for mutate in (
lambda value: value.update(releaseId='other-release'),
lambda value: value['historySources'][0].update(turnId='unconfirmed'),
lambda value: value['historyEvidence'].update(executionId='other-execution')):
candidate = copy.deepcopy(base)
mutate(candidate)
with self.subTest(candidate=candidate['releaseId']):
with self.assertRaises(HistorySnapshotError):
validate_history_snapshot(candidate, task=task, authority=auth,
release_id=RELEASE, request_turn_id=TURN)
def test_rejects_old_protocol_evidence_even_when_nested_in_section(self):
candidate = snapshot()
candidate['sections'][0]['protocolEvidence'] = {'queryId': 'old-query'}
with self.assertRaisesRegex(HistorySnapshotError, '普通查询证明'):
validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
def test_rejects_unavailable_source_rows_and_inconsistent_complete_coverage(self):
candidate = snapshot(unavailable_second=True)
candidate['sections'].append(copy.deepcopy(snapshot()['sections'][1]))
with self.assertRaises(HistorySnapshotError):
validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
candidate = snapshot()
candidate['historyCoverage']['importedSources'] = 1
with self.assertRaisesRegex(HistorySnapshotError, '可用来源不一致'):
validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
def test_preserves_long_record_cells_but_enforces_global_snapshot_capacity(self):
long_cell = '原始长文本' * 600
candidate = snapshot()
candidate['sections'][0]['columns'].append({'key': 'sourceText', 'label': '原始文本'})
candidate['sections'][0]['rows'][0]['sourceText'] = long_cell
validated = validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
self.assertEqual(validated['sections'][0]['rows'][0]['sourceText'], long_cell)
oversized = snapshot()
large_value = 'x' * 3_500_000
columns = [{'key': f'cell{index}', 'label': f'文本{index}'} for index in range(5)]
oversized['sections'][0]['columns'] = columns
oversized['sections'][0]['rows'] = [{column['key']: large_value for column in columns}]
with self.assertRaisesRegex(HistorySnapshotError, '快照超出允许大小'):
validate_history_snapshot(oversized, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
class HistoryReportExecutionTest(unittest.TestCase):
def test_scoped_formula_context_reaches_synthesis_and_the_same_review(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
for product in (True, False):
candidate = snapshot()
task = report_task((SOURCE_A,))
candidate['historySources'] = candidate['historySources'][:1]
candidate['sections'] = candidate['sections'][:1]
candidate['facts'] = candidate['facts'][:1]
candidate['historyCoverage'].update(expectedSources=1, importedSources=1)
rules = {'supportsStageContribution': product, 'metricRules': {'compositeRate': {
'formula': 'blackRate*brightRate' if product else '(blackRate+brightRate)/2'}}}
context = {'ruleVersionId': 'formal-version', 'calculationRules': rules,
'metricFormulaDescriptions': {'compositeRate': '正式规则说明'}}
candidate['sections'][0]['metricContext'] = context
candidate['historySources'][0]['metricContexts'] = [context]
candidate['facts'][0].update(sectionIndex=0, text='2026年9月综合合格率 = 0.9531',
displayText='2026年9月综合合格率95.31%。')
phases = []
def provider(request, timeout):
body = json.loads(request.data); payload = json.loads(body['messages'][1]['content'])
semantics = payload['metricSemantics']
self.assertIs(semantics['productConfirmed'], product)
supplied = semantics['scopedMetricContexts'][0]
self.assertEqual(supplied['calculationRules'], rules)
self.assertEqual(payload['scopes'][0]['metricContextId'], supplied['id'])
if body['messages'][0]['content'].startswith('逐句核验证据'):
phases.append('review')
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}], 'missingGoalDescriptions': []})
phases.append('synthesis')
return response({'blocks': [{'role': 'core', 'text': '本期综合合格率95.31%,沿用本轮正式组合规则解释。', 'factIds': ['F1']}]})
with self.subTest(product=product):
result = synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='汇总历史结果', snapshot=candidate, task=task, authority=authority(task),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event(), opener=provider)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertEqual(phases, ['synthesis', 'review'])
def test_display_percent_and_short_month_use_verified_history_metadata(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = snapshot()
candidate['facts'][0].update(text='2026年9月综合合格率 = 0.9530615',
displayText='2026年9月综合合格率95.31%。', sectionIndex=0)
for text, expected in (('9月综合合格率95.31%。', True), ('10月综合合格率95.31%。', False),
('9月综合合格率95.31 kg。', False), ('9月综合合格率99.31%。', False)):
def provider(request, timeout):
if json.loads(request.data)['messages'][0]['content'].startswith('逐句核验证据'):
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': text, 'factIds': ['F1']}]})
with self.subTest(text=text):
result = synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='历史指标', snapshot=candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event(), opener=provider)
self.assertEqual(result['evidenceSufficient'], expected, result['compositionDiagnostics'])
if expected:
self.assertIn('95.31%', result['answer'])
self.assertFalse(result['compositionRepairAttempted'])
def test_many_history_facts_share_compact_frames_and_keep_later_product(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = snapshot()
source = candidate['historySources'][0]['turnId']
product = '16.33X15.5X3.75+T'
candidate['sections'].append({**copy.deepcopy(candidate['sections'][0]),
'title': '重点规格指标', 'filters': {'product': product},
'columns': [{'key': 'product', 'label': '产品'}, {'key': 'weight', 'label': '报废重量'}],
'rows': [{'product': product, 'weight': 100}]})
candidate['facts'] = [{'id': f'history:{index}', 'sourceTurnId': source, 'sectionIndex': 0,
'text': f'2026年9月黑片报废重量{index}.00 kg。'} for index in range(120)]
candidate['facts'].append({'id': 'history:late-product', 'sourceTurnId': source, 'sectionIndex': 2,
'text': '2026年9月'+product+'黑片报废重量100.00 kg。'})
def provider(request, timeout):
body = json.loads(request.data)
if body['messages'][0]['content'].startswith('逐句核验证据'):
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
payload = json.loads(body['messages'][1]['content'])
self.assertEqual(len(payload['scopes']), 2)
self.assertGreaterEqual(len(payload['facts']), 60)
self.assertFalse(any('rows' in scope or 'historyFactId' in scope for scope in payload['scopes']))
late = next(fact for fact in payload['facts'] if fact['evidenceRef'] == 'history:late-product')
return response({'blocks': [{'text': product+'黑片报废重量100.00 kg,是应核查的规格线索。', 'factIds': [late['id']]}]})
result = synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='汇总重点规格', snapshot=candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event(), opener=provider)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertIn(product, result['readingSummary']['lead'])
self.assertLess(result['compositionDiagnostics']['factPool']['modelEvidenceChars'], 30000)
def test_verified_row_values_remain_visible_to_synthesis_and_same_review(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = snapshot()
candidate['sections'][0].update(title='已保存阶段聚合', stage='black',
columns=[{'key': 'metric', 'label': '指标'}, {'key': 'value', 'label': '正式值'}],
rows=[{'metric': 'rate', 'value': .9531}, {'metric': 'totalWeight', 'value': 100.00}])
candidate['facts'][0].update(text='2026年9月黑片合格率 = 0.9531', displayText='2026年9月黑片合格率95.31%。', sectionIndex=0,
metricCode='rate', sourceKind='native_saved_aggregate', scope={'period': {'year': 2026, 'month': 9}, 'filters': candidate['sections'][0]['filters'], 'stage': 'black'},
values=[{'metricCode': 'currentBlackInput', 'unit': 'source_weight', 'displayValue': '100.00'}])
def provider(request, timeout):
body = json.loads(request.data); payload = json.loads(body['messages'][1]['content'])
current = next(fact for fact in payload['facts'] if fact['id'] == 'F1')
self.assertEqual(current['kind'], 'stage')
self.assertEqual(current['values'][0]['metricCode'], 'currentBlackInput')
if body['messages'][0]['content'].startswith('逐句核验证据'):
self.assertIn('来源重量100.00', payload['blocks'][0]['text'])
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
return response({'blocks': [{'text': '本期黑片合格率95.31%,黑片投入来源重量100.00,应结合阶段变化核查。', 'factIds': ['F1']}]})
result = synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='汇总历史', snapshot=candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event(), opener=provider)
self.assertTrue(result['evidenceSufficient'], result['compositionDiagnostics'])
self.assertNotIn('kg', result['answer'])
def test_global_product_history_keeps_server_row_identifier_and_rejects_dimension_typo(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response, fixture_payload
product = '16.33X15.5X3.75+T'
candidate = snapshot()
candidate['sections'][0].update(title='Top 2 product排名', filters={},
columns=[{'key': key, 'label': key} for key in ('rank', 'key', 'label', 'weight')],
rows=[{'rank': 1, 'key': product, 'label': product, 'weight': 100.00},
{'rank': 2, 'key': '18X15X4+T', 'label': '18X15X4+T', 'weight': 90.00}])
candidate['facts'][0]['text'] = '2026年9月;全局产品排名:'+product+',黑片报废重量100.00 kg。'
for typo in (False, True):
with self.subTest(typo=typo):
calls = []
def provider(request, timeout):
body = json.loads(request.data); calls.append(body)
if body['messages'][0]['content'].startswith('逐句核验证据'):
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
payload = fixture_payload(request)
current = next(fact for fact in payload['facts'] if fact['id'] == 'F1')
scope = next(scope for scope in payload['scopes'] if scope['scopeId'] == current['scopeId'])
self.assertTrue(any(item['value'] == product and item['factId'] == 'F1' for item in payload['identifierTokens'].values()))
value_id = next(key for key, token in payload['numericTokens'].items()
if token['factId'] == 'F1' and token['value'] == '100.00')
name = product.replace('3.75', '3.76') if typo else product
return response({'blocks': [{'text': '全局产品排名中,'+name+'的黑片报废重量为{{'+value_id+'}} kg,应结合投入规模核查。',
'factIds': ['F1'], 'evidenceRefs': ['history-fact:F1'], 'claimType': 'inference'}]})
result = synthesize_history_snapshot(base_url='https://offline.invalid/v1', api_key='fixture', model='fixture',
question='解读全局产品历史排名', snapshot=candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN, cancel_event=threading.Event(), opener=provider)
self.assertEqual(result['evidenceSufficient'], not typo, result.get('compositionDiagnostics'))
if not typo:
self.assertIn(product, result['answer'])
self.assertEqual(result['readingSummary']['evidenceRefs'], ['history-fact:F1'])
self.assertTrue(result['compositionDiagnostics']['identifierBindings'])
else:
self.assertTrue(result['compositionRepairAttempted'])
self.assertNotIn('3.76', result['answer'])
def test_autonomous_report_uses_shared_free_synthesis_and_actual_history_references(self):
from test_native_answer import response
calls = []
def provider(request, timeout):
body = json.loads(request.data); calls.append(body)
if body['messages'][0]['content'].startswith('逐句核验证据'):
return response({'reviews': [{'index': 0, 'supported': True}], 'missingGoalDescriptions': []})
payload = json.loads(body['messages'][1]['content'])
self.assertEqual([fact['evidenceRef'] for fact in payload['facts']], ['history-fact:F1', 'history-fact:F2'])
scopes = {scope['scopeId']: scope for scope in payload['scopes']}
self.assertTrue(all('period' in scopes[fact['scopeId']] for fact in payload['facts']))
self.assertTrue(all('historyFactId' not in scope for scope in scopes.values()))
return response({'blocks': [{'text': '历史来源各自适用不同期间和筛选,应分别解读各自的报废重量。',
'factIds': ['F1', 'F2'], 'evidenceRefs': ['history-fact:F1', 'history-fact:F2'], 'claimType': 'inference'}]})
with tempfile.TemporaryDirectory() as folder, patch('nao_evidence_finalizer._open_model', side_effect=provider), patch(
'nao_history_report.select_history_headline', side_effect=AssertionError('must use shared synthesis')):
result = execute_history_report(request={'releaseId': RELEASE, 'turnId': TURN, 'question': '汇总历史', '_autonomousAnswer': True},
task=report_task(), authority=authority(), actor_root=Path(folder), delegated=FakeDelegated(snapshot()),
connection=('https://offline.invalid/v1', 'fixture', 'fixture'), cancel_event=threading.Event(), emit=lambda _: None, timings={})
private = json.loads((Path(folder) / 'private/native-composition.json').read_text())
self.assertEqual(result['status'], 'COMPLETED')
self.assertIn('各自适用不同期间和筛选', result['answer'])
self.assertEqual(result['readingSummary']['evidenceRefs'], ['history-fact:F1', 'history-fact:F2'])
self.assertEqual(result['historyHeadlineFactIds'], ['history-fact:F1', 'history-fact:F2'])
self.assertEqual(result['historyCoverage'], snapshot()['historyCoverage'])
self.assertEqual(len(calls), 2)
self.assertEqual(len(private['rawModelResponses']), 2)
self.assertNotIn('rawModelResponses', result)
def test_autonomous_raw_or_member_only_snapshot_has_explicit_limitation(self):
with tempfile.TemporaryDirectory() as folder, patch('nao_evidence_finalizer._open_model') as model:
result = execute_history_report(request={'releaseId': RELEASE, 'turnId': TURN, 'question': '汇总历史', '_autonomousAnswer': True},
task=report_task(), authority=authority(), actor_root=Path(folder), delegated=FakeDelegated(snapshot(facts=[])),
connection=('https://offline.invalid/v1', 'fixture', 'fixture'), cancel_event=threading.Event(), emit=lambda _: None, timings={})
self.assertEqual(result['status'], 'PARTIAL')
self.assertIn('平台附录展示', result['answer'])
self.assertTrue(result['readingSummary']['notices'])
self.assertEqual(result['historyCoverage'], snapshot()['historyCoverage'])
model.assert_not_called()
def run_report(self, response, model_result=None, *, model_results=None, cancel_after_model=False):
delegated = FakeDelegated(response)
emitted, model_payloads = [], []
cancel_event = threading.Event()
queued_results = list(model_results) if model_results is not None else []
def fake_model_json(**kwargs):
model_payloads.append(kwargs['payload'])
if cancel_after_model:
cancel_event.set()
result = queued_results.pop(0) if queued_results else model_result
if isinstance(result, Exception):
raise result
return result
with tempfile.TemporaryDirectory() as temp, patch('nao_agent_task._model_json', side_effect=fake_model_json):
result = execute(
request={'releaseId': RELEASE, 'turnId': TURN,
'question': '请汇总已确认历史质量报告'},
task=report_task(), authority=authority(), actor_root=Path(temp),
delegated=delegated, connection=('https://offline.invalid/v1', 'secret', 'model-x'),
cancel_event=cancel_event, emit=emitted.append, phase_timings={})
return result, delegated, emitted, model_payloads
def test_imports_server_snapshot_and_models_only_title_and_existing_fact_ids(self):
result, delegated, emitted, payloads = self.run_report(
snapshot(), {'reportTitle': '2025至2026年质量表现',
'headlineFactIds': ['history-fact:F1', 'history-fact:F2']})
self.assertEqual(delegated.calls, [('/api/analysis/protocol/history', {}, 'POST')])
self.assertEqual(result['status'], 'COMPLETED')
self.assertEqual(result['completionMethod'], 'confirmed_history_report')
self.assertEqual(result['stopReason'], 'history_report_complete')
self.assertTrue(result['titleGeneratedByModel'])
self.assertEqual(result['protocolEvidence'], [])
self.assertEqual(result['historyHeadlineFactIds'], ['history-fact:F1', 'history-fact:F2'])
self.assertEqual(result['historyCoverage'], snapshot()['historyCoverage'])
self.assertEqual(result['agentTask'], authority()['task'])
self.assertNotIn('agentContext', result)
self.assertEqual(result['visualState'], 'not_requested')
self.assertEqual(result['visuals'], [])
self.assertEqual(len(emitted), 2)
self.assertEqual(emitted[-1]['type'], 'result')
self.assertEqual(len(payloads), 1)
serialized = json.dumps(payloads[0], ensure_ascii=False)
self.assertNotIn('rows', payloads[0])
self.assertNotIn('整体', serialized)
self.assertIn('history-fact:F1', serialized)
self.assertEqual(len(payloads[0]['historySources']), 2)
self.assertIn('columnFilters', serialized)
def test_incomplete_history_remains_partial_and_includes_unavailable_source_reason(self):
result, _, _, _ = self.run_report(
snapshot(unavailable_second=True),
{'reportTitle': '2026年9月客户质量结果', 'headlineFactIds': ['history-fact:F1']})
self.assertEqual(result['status'], 'PARTIAL')
self.assertEqual(result['stopReason'], 'history_report_partial')
self.assertIn('2025年9月海外组没有可用历史来源。',
result['evidenceCompletion']['missingEvidence'])
def test_available_partial_source_is_preserved_but_not_marked_complete(self):
partial = snapshot()
partial['historySources'][1]['status'] = 'PARTIAL'
partial['historyCoverage'] = {
'complete': False, 'expectedSources': 2, 'importedSources': 2,
'missingEvidence': ['历史范围中有一项来源覆盖不完整。'],
}
result, _, _, _ = self.run_report(
partial, {'reportTitle': '2025至2026年质量表现',
'headlineFactIds': ['history-fact:F1']})
self.assertEqual(result['status'], 'PARTIAL')
self.assertEqual(result['evidenceCompletion']['serverCoverageSufficient'], False)
def test_unknown_fact_invalid_title_and_model_failure_fall_back_to_partial(self):
invalid_results = [
{'reportTitle': '2026年9月客户质量结果', 'headlineFactIds': ['not-a-fact']},
{'reportTitle': '<b>质量报告</b>', 'headlineFactIds': ['history-fact:F1']},
RuntimeError('model unavailable'),
]
for invalid in invalid_results:
with self.subTest(invalid=repr(invalid)):
result, _, _, _ = self.run_report(snapshot(), invalid)
self.assertEqual(result['status'], 'PARTIAL')
self.assertFalse(result['titleGeneratedByModel'])
self.assertEqual(result['reportTitle'], '历史质量分析报告')
self.assertEqual(result['historyHeadlineFactIds'], [])
def test_mixed_customer_and_unfiltered_source_repairs_title_to_multi_scope(self):
candidate = snapshot()
candidate['sections'][1]['filters'] = {'group': '汽车交通-海外'}
bad = {'reportTitle': '2026年9月客户800001黑片亮片报废重量',
'headlineFactIds': ['history-fact:F1']}
repaired = {'reportTitle': '多范围汇总质量分析',
'headlineFactIds': ['history-fact:F1', 'history-fact:F2']}
result, delegated, _, payloads = self.run_report(candidate, model_results=[bad, repaired])
self.assertEqual(result['status'], 'COMPLETED')
self.assertEqual(result['reportTitle'], '多范围汇总质量分析')
self.assertEqual(len(payloads), 2)
self.assertEqual(payloads[0]['facts'], payloads[1]['facts'])
self.assertEqual(payloads[0]['historySources'], payloads[1]['historySources'])
self.assertIn('repairInstructions', payloads[1])
self.assertEqual(delegated.calls, [('/api/analysis/protocol/history', {}, 'POST')])
self.assertNotIn('rows', payloads[1])
def test_mixed_product_scope_is_not_named_but_common_product_can_be_named(self):
mixed = snapshot()
mixed['sections'][0]['filters']['product'] = '磁钢规格-X'
bad = {'reportTitle': '2026年磁钢规格-X质量汇总',
'headlineFactIds': ['history-fact:F1']}
repaired = {'reportTitle': '多范围汇总质量分析',
'headlineFactIds': ['history-fact:F1']}
result, _, _, payloads = self.run_report(mixed, model_results=[bad, repaired])
self.assertEqual(result['status'], 'COMPLETED')
self.assertEqual(result['reportTitle'], '多范围汇总质量分析')
self.assertEqual(len(payloads), 2)
common = snapshot()
common['sections'][0]['filters']['product'] = '磁钢规格-X'
common['sections'][1]['filters']['product'] = '磁钢规格-X'
common_title = {'reportTitle': '2026年磁钢规格-X质量汇总',
'headlineFactIds': ['history-fact:F1']}
result, _, _, payloads = self.run_report(common, common_title)
self.assertEqual(result['status'], 'COMPLETED')
self.assertEqual(result['reportTitle'], common_title['reportTitle'])
self.assertEqual(len(payloads), 1)
def test_noncommon_column_filter_values_are_blocked_but_field_ids_are_not(self):
candidate = snapshot()
facts = candidate['facts']
self.assertIsNone(_validate_model_selection(
{'reportTitle': '2026年WO-1订单质量汇总', 'headlineFactIds': ['history-fact:F1']},
candidate, '整理历史报告', facts))
self.assertIsNotNone(_validate_model_selection(
{'reportTitle': 'orderNo质量汇总', 'headlineFactIds': ['history-fact:F1']},
candidate, '整理历史报告', facts))
def test_negative_filter_literals_are_never_named_even_when_common(self):
for negative_filter in (
{'columnFilters': [{'field': 'customerCode', 'operator': 'not_in',
'values': ['800001']}]},
{'exclusions': {'product': ['磁钢规格-X']}}):
candidate = snapshot()
candidate['sections'][0]['filters'] = copy.deepcopy(negative_filter)
candidate['sections'][1]['filters'] = copy.deepcopy(negative_filter)
title = ('2026年客户800001质量汇总' if 'columnFilters' in negative_filter
else '2026年磁钢规格-X质量汇总')
with self.subTest(negative_filter=negative_filter):
self.assertIsNone(_validate_model_selection(
{'reportTitle': title, 'headlineFactIds': ['history-fact:F1']},
candidate, '整理历史报告', candidate['facts']))
def test_common_positive_in_filter_literal_may_be_named(self):
candidate = snapshot()
common_filter = {'columnFilters': [{'field': 'customerCode', 'operator': 'in',
'values': ['800001']}]}
candidate['sections'][0]['filters'] = copy.deepcopy(common_filter)
candidate['sections'][1]['filters'] = copy.deepcopy(common_filter)
self.assertIsNotNone(_validate_model_selection(
{'reportTitle': '2026年客户800001质量汇总',
'headlineFactIds': ['history-fact:F1']},
candidate, '整理历史报告', candidate['facts']))
def test_title_repair_is_limited_to_one_retry(self):
candidate = snapshot()
candidate['sections'][1]['filters'] = {}
bad = {'reportTitle': '2026年9月客户800001质量汇总',
'headlineFactIds': ['history-fact:F1']}
result, _, _, payloads = self.run_report(candidate, model_results=[bad, bad])
self.assertEqual(result['status'], 'PARTIAL')
self.assertFalse(result['titleGeneratedByModel'])
self.assertEqual(len(payloads), 2)
def test_no_facts_skips_model_and_is_partial(self):
empty = snapshot(facts=[])
result, _, _, payloads = self.run_report(empty, None)
self.assertEqual(result['status'], 'PARTIAL')
self.assertEqual(payloads, [])
self.assertIn('没有可引用事实', result['evidenceCompletion']['missingEvidence'][0])
def test_cancellation_after_snapshot_keeps_partial_and_does_not_accept_late_title(self):
result, _, _, payloads = self.run_report(
snapshot(), {'reportTitle': '2025至2026年质量表现',
'headlineFactIds': ['history-fact:F1']}, cancel_after_model=True)
self.assertEqual(len(payloads), 1)
self.assertEqual(result['status'], 'PARTIAL')
self.assertFalse(result['titleGeneratedByModel'])
def test_api_failure_is_not_replaced_by_a_local_history_scan(self):
delegated = FakeDelegated(RuntimeError('history service unavailable'))
with tempfile.TemporaryDirectory() as temp:
with self.assertRaisesRegex(RuntimeError, 'history service unavailable'):
execute_history_report(
request={'releaseId': RELEASE, 'turnId': TURN, 'question': '报告'},
task=report_task(), authority=authority(), actor_root=Path(temp),
delegated=delegated, connection=('u', 'k', 'm'),
cancel_event=threading.Event(), emit=lambda _: None, timings={})
self.assertEqual(delegated.calls, [('/api/analysis/protocol/history', {}, 'POST')])
def test_headline_uses_authorized_finalizing_phase_through_real_model_gate(self):
class ProviderHandler(BaseHTTPRequestHandler):
def log_message(self, *_):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
owner.provider_calls.append((self.headers.get('Authorization'), body))
selection = ({'reportTitle': '2026年9月客户800001质量汇总',
'headlineFactIds': ['history-fact:F1']}
if len(owner.provider_calls) == 1 else
{'reportTitle': '多范围汇总质量表现',
'headlineFactIds': ['history-fact:F1']})
content = json.dumps(selection, ensure_ascii=False)
response = json.dumps({'model': 'history-model', 'choices': [{
'finish_reason': 'stop', 'message': {'content': content},
}]}, ensure_ascii=False).encode()
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(response)))
self.end_headers()
self.wfile.write(response)
owner = self
self.provider_calls = []
provider = ThreadingHTTPServer(('127.0.0.1', 0), ProviderHandler)
provider.daemon_threads = True
provider_thread = threading.Thread(target=provider.serve_forever, daemon=True)
provider_thread.start()
authorize_calls = []
gate = ModelGate(
upstream_base=f'http://127.0.0.1:{provider.server_port}/v1',
upstream_key='fake-provider-key', model='history-model', port=0,
client_key='fake-worker-key', authorize=lambda: authorize_calls.append(True),
native_turn_marker='quality-turn:current-history-turn')
try:
selection = select_history_headline(
snapshot=snapshot(), question='整理已确认历史报告',
connection=(f'http://127.0.0.1:{gate._server.server_port}/v1',
gate.client_key, 'history-model'),
cancel_event=threading.Event())
self.assertEqual(selection, ('多范围汇总质量表现', ['history-fact:F1']))
self.assertGreaterEqual(len(authorize_calls), 2)
self.assertEqual(gate.rejected_stale_requests, 0)
self.assertEqual(len(self.provider_calls), 2)
for authorization, body in self.provider_calls:
self.assertEqual(authorization, 'Bearer fake-provider-key')
self.assertEqual(body['model'], 'history-model')
self.assertEqual(len(body['messages']), 2)
self.assertNotIn('sections', body['messages'][1]['content'])
self.assertIn('repairInstructions', self.provider_calls[1][1]['messages'][1]['content'])
finally:
gate.close()
provider.shutdown()
provider.server_close()
provider_thread.join(timeout=1)
if __name__ == '__main__':
unittest.main()