Files
quality_analyse/verification/test_nao_history_report.py

2344 lines
149 KiB
Python
Raw Permalink 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 re
import sys
import tempfile
import threading
import unittest
from decimal import Decimal
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,
_history_fallback_title, _validate_model_selection,
_history_model_fact_projection, _history_model_source_goals)
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 set_source_task_context(candidate, source_turn_id, task):
matches = [source for source in candidate.get('historySources') or []
if source.get('turnId') == source_turn_id]
if len(matches) != 1:
raise AssertionError(f'expected one history source for {source_turn_id}, got {len(matches)}')
matches[0]['taskContext'] = copy.deepcopy(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,
},
}
def saved_visual():
return {
'id': 'saved-product-scrap-top', 'type': 'bar', 'title': '产品报废总重排名',
'subtitle': '历史来源图表', 'unit': 'kg',
'categories': [{'key': 'P1', 'label': '规格一'}],
'series': [{'key': 'current_scrap_weight', 'label': '报废总重', 'values': [11]}],
'evidenceRefs': ['QABCDEF12'],
'requiredFields': ['black_scrap_weight', 'bright_scrap_weight'],
'scope': {'year': 2026, 'month': 9, 'releaseId': RELEASE, 'view': 'product',
'includeUnassigned': False},
'period': {'year': 2026, 'month': 9}, 'metric': 'current_scrap_weight',
'coverage': {'totalRows': 1, 'returnedRows': 1, 'truncated': False,
'shareBasis': 'all_scope_weight', 'denominator': 20},
'originTurnId': SOURCE_A, 'sectionKey': 'product', 'dimension': 'product',
}
def saved_visual_lineage():
chart = saved_visual()
return {'visualId': chart['id'], 'sourceTurnId': chart['originTurnId'],
'scope': copy.deepcopy(chart['scope']), 'period': copy.deepcopy(chart['period']),
'comparisonPeriod': chart.get('comparisonPeriod'),
'dimension': chart['dimension'], 'metric': chart['metric'],
'stage': chart.get('stage'),
'evidenceRefs': copy.deepcopy(chart['evidenceRefs'])}
def ranking_history_snapshot():
"""Two identical saved September rankings and one separate August ranking."""
candidate = snapshot()
shared_filters = {'department': '消费电子产业部', 'group': '消费电子-3C'}
columns = [
{'key': 'rank', 'label': '排名'}, {'key': 'key', 'label': '产品键'},
{'key': 'label', 'label': '规格'}, {'key': 'weight', 'label': '综合报废重量'},
{'key': 'compositeRate', 'label': '综合合格率'},
{'key': 'blackScrapWeight', 'label': '黑片报废重量'},
{'key': 'brightScrapWeight', 'label': '亮片报废重量'},
{'key': 'share', 'label': '占比'}, {'key': 'cumulativeShare', 'label': '累计占比'},
]
def rows(prefix, count):
return [{
'rank': index + 1, 'key': f'{prefix}-{index + 1}',
'label': f'{prefix}规格{index + 1}', 'weight': 10.25 - index,
'compositeRate': 0.9123 - index / 100, 'blackScrapWeight': 0.25 + index,
'brightScrapWeight': 0.75 + index, 'share': 0.20 - index / 50,
'cumulativeShare': sum(0.20 - offset / 50 for offset in range(index + 1)),
} for index in range(count)]
september = {
'title': '2026年9月3C Top5产品综合报废重量排名',
'period': {'year': 2026, 'month': 9}, 'filters': shared_filters,
'dimension': 'product', 'metric': 'scrapWeight', 'stage': None,
'columns': columns, 'rows': rows('九月', 5),
}
august = {
'title': '2026年8月全公司Top3产品综合报废重量排名',
'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimension': 'product', 'metric': 'scrapWeight', 'stage': None,
'columns': columns, 'rows': rows('八月', 3),
}
candidate['sections'] = [
{**copy.deepcopy(september), 'sourceTurnId': SOURCE_A},
{**copy.deepcopy(september), 'sourceTurnId': SOURCE_B},
{**copy.deepcopy(august), 'sourceTurnId': SOURCE_B},
]
candidate['facts'] = [
{'id': 'history-fact:SEP-A', 'text': '2026年9月3C Top5综合报废重量排名已确认。',
'displayText': '2026年9月3C Top5综合报废重量排名已确认。',
'sourceTurnId': SOURCE_A, 'sectionIndex': 0,
'scope': {'period': {'year': 2026, 'month': 9}, 'filters': copy.deepcopy(shared_filters),
'dimension': 'product', 'metric': 'scrapWeight'}},
{'id': 'history-fact:SEP-B', 'text': '2026年9月3C Top5综合报废重量排名已确认。',
'displayText': '2026年9月3C Top5综合报废重量排名已确认。',
'sourceTurnId': SOURCE_B, 'sectionIndex': 1,
'scope': {'period': {'year': 2026, 'month': 9}, 'filters': copy.deepcopy(shared_filters),
'dimension': 'product', 'metric': 'scrapWeight'}},
{'id': 'history-fact:AUG-B', 'text': '2026年8月全公司Top3综合报废重量排名已确认。',
'displayText': '2026年8月全公司Top3综合报废重量排名已确认。',
'sourceTurnId': SOURCE_B, 'sectionIndex': 2,
'scope': {'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimension': 'product', 'metric': 'scrapWeight'}},
]
charts, lineages = [], []
for index, (source, period, filters, ref) in enumerate((
(SOURCE_A, {'year': 2026, 'month': 9}, shared_filters, 'Q00000001'),
(SOURCE_B, {'year': 2026, 'month': 9}, shared_filters, 'Q00000002'),
(SOURCE_B, {'year': 2026, 'month': 8}, {}, 'Q00000003'))):
section = next(section for section in candidate['sections']
if section['sourceTurnId'] == source and section['period'] == period
and section['filters'] == filters)
chart = saved_visual()
chart.update(id=f'history-ranking-{index + 1}', originTurnId=source,
period=copy.deepcopy(period), evidenceRefs=[ref],
scope={'year': period['year'], 'month': period['month'], 'releaseId': RELEASE,
'view': 'product', 'includeUnassigned': False, **copy.deepcopy(filters)})
chart['categories'] = [{'key': row.get('key') or row.get('label'),
'label': row.get('label') or row.get('key')}
for row in section['rows']]
chart['series'] = [{'key': chart['metric'], 'label': '报废总重',
'values': [row['weight'] for row in section['rows']]}]
chart['coverage'] = {'totalRows': len(section['rows']), 'returnedRows': len(section['rows']),
'truncated': False, 'shareBasis': 'all_scope_weight', 'denominator': 20}
charts.append(chart)
lineage = saved_visual_lineage()
lineage.update(visualId=chart['id'], sourceTurnId=source,
period=copy.deepcopy(chart['period']), scope=copy.deepcopy(chart['scope']),
evidenceRefs=copy.deepcopy(chart['evidenceRefs']))
lineages.append(lineage)
candidate.update(visualSchemaVersion=1, visualState='ready', visuals=charts,
historyVisualSources=lineages)
return candidate
def facts_for_ranking_sections(candidate):
facts = []
field_labels = {
'compositeRate': '综合合格率', 'blackScrapWeight': '黑片报废重量',
'brightScrapWeight': '亮片报废重量', 'blackTotalWeight': '黑片投入重量',
'brightTotalWeight': '亮片投入重量', 'share': '占同范围全部报废',
'cumulativeShare': '累计占比',
}
for section_index, section in enumerate(candidate['sections']):
period = section['period']
prefix = f"{period['year']}年{period['month']}月:"
for row_index, row in enumerate(section['rows']):
parts = [f"Top {len(section['rows'])} {row['label']} · 综合报废重量 = {row['weight']}"]
parts.extend(f"{field_labels[key]} = {row[key]}" for key in field_labels if key in row)
text_value = prefix + ';'.join(parts) + ';同范围正式分母 = 100。'
facts.append({
'id': f"history-fact:rank-{section_index}-{row_index}",
'text': text_value, 'displayText': text_value,
'sourceTurnId': section['sourceTurnId'], 'sectionIndex': section_index,
'rowIndex': row_index, 'metricCode': 'scrapWeight',
'rawValue': row['weight'], 'rawUnit': 'source_weight',
'unit': 'source_weight', 'displayValue': str(row['weight']),
'scope': {'period': copy.deepcopy(period),
'filters': copy.deepcopy(section['filters']),
'dimension': section.get('dimension') or 'product', 'stage': 'overall'},
})
return facts
def add_formal_mass_units(candidate, unit='kg'):
"""Fixture helper: the signed row values themselves declare physical units."""
metrics = {
'weight': 'scrapWeight', 'blackScrapWeight': 'blackScrapWeight',
'brightScrapWeight': 'brightScrapWeight', 'blackTotalWeight': 'blackTotalWeight',
'brightTotalWeight': 'brightTotalWeight',
}
for fact in candidate.get('facts') or []:
section_index, row_index = fact.get('sectionIndex'), fact.get('rowIndex')
if (type(section_index) is not int or type(row_index) is not int
or section_index >= len(candidate.get('sections') or [])):
continue
rows = candidate['sections'][section_index].get('rows') or []
if not 0 <= row_index < len(rows):
continue
row = rows[row_index]
fact['values'] = [{'metricCode': metric, 'rawValue': str(row[field]),
'rawUnit': 'source_weight', 'unit': unit,
'displayValue': str(row[field])}
for field, metric in metrics.items() if field in row]
return candidate
def actual_precision_ranking_snapshot():
"""Mirror HistoryFacts text precision against JSON-decoded section numbers."""
candidate = ranking_history_snapshot()
for section in candidate['sections']:
section['columns'].extend([
{'key': 'blackTotalWeight', 'label': '黑片投入重量'},
{'key': 'brightTotalWeight', 'label': '亮片投入重量'},
])
for row in section['rows']:
row['blackTotalWeight'] = 1.0
row['brightTotalWeight'] = 1.0
section = candidate['sections'][0]
exact = {
'weight': '493.99660000',
'compositeRate': '0.9122755444707148916393578115770785',
'blackScrapWeight': '164.19900000',
'brightScrapWeight': '329.79760000',
'blackTotalWeight': '7115.68610000',
'brightTotalWeight': '4983.65360000',
'share': '0.07078164231313173578917252155832738',
'cumulativeShare': '0.07078164231313173578917252155832738',
}
labels = {
'weight': '综合报废重量', 'compositeRate': '综合合格率',
'blackScrapWeight': '黑片报废重量', 'brightScrapWeight': '亮片报废重量',
'blackTotalWeight': '黑片投入重量', 'brightTotalWeight': '亮片投入重量',
'share': '占同范围全部报废', 'cumulativeShare': '累计占比',
}
for field, raw in exact.items():
section['rows'][0][field] = float(raw)
candidate['facts'] = facts_for_ranking_sections(candidate)
add_formal_mass_units(candidate)
first = candidate['facts'][0]
for field, raw in exact.items():
label = labels[field]
current = str(section['rows'][0][field])
first['text'] = first['text'].replace(f'{label} = {current}', f'{label} = {raw}')
first['displayText'] = first['displayText'].replace(f'{label} = {current}', f'{label} = {raw}')
return candidate
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_validates_source_bound_visual_metadata_and_rejects_unknown_sources(self):
candidate = snapshot()
candidate.update(visualSchemaVersion=1, visualState='ready', visuals=[saved_visual()],
historyVisualSources=[saved_visual_lineage()])
checked = validate_history_snapshot(candidate, task=report_task(), authority=authority(),
release_id=RELEASE, request_turn_id=TURN)
self.assertEqual(checked['visuals'], [saved_visual()])
self.assertEqual(checked['historyVisualSources'], [saved_visual_lineage()])
candidate['visuals'][0]['originTurnId'] = TURN
with self.assertRaises(HistorySnapshotError):
validate_history_snapshot(candidate, task=report_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_history_report_drops_only_optional_detail_without_fact_ids(self):
from nao_native_answer import _remove_uncited_optional_details
parsed = {'blocks': [
{'role': 'core', 'text': '已核验结论', 'factIds': ['F1']},
{'role': 'detail', 'text': '单期静态排名说明', 'evidenceRefs': ['untrusted-ref']},
{'role': 'detail', 'text': '已绑定补充', 'factIds': ['F2']},
]}
diagnostics = {}
result = _remove_uncited_optional_details(parsed, diagnostics)
self.assertEqual([block['text'] for block in result['blocks']], ['已核验结论', '已绑定补充'])
self.assertEqual(result['blocks'][0]['factIds'], ['F1'])
self.assertEqual(result['blocks'][1]['factIds'], ['F2'])
self.assertEqual(diagnostics['omittedUncitedOptionalDetails'], [1])
self.assertEqual(diagnostics['omittedUncitedOptionalDetailReasons'],
[{'index': 1, 'reason': 'missing_factIds'}])
def test_history_model_view_uses_display_scope_labels_without_changing_signed_scope(self):
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
first_section = candidate['sections'][0]
first_section['filters']['columnFilters'] = [{
'field': 'src_work_order', 'stages': ['black'], 'operator': 'in', 'values': ['WO-7'],
}]
set_source_task_context(candidate, SOURCE_A,
{'sourceFieldLabels': {'src_work_order': '黑片委托单号'}})
set_source_task_context(candidate, SOURCE_B,
{'sourceFieldLabels': {'src_work_order': '亮片委托单号'}})
fact = candidate['facts'][0]
section = candidate['sections'][fact['sectionIndex']]
fact['scope']['filters'] = copy.deepcopy(section['filters'])
raw_filters = copy.deepcopy(fact['scope']['filters'])
raw_text = ('2026年9月(筛选 {group: 消费电子-3C, department: 消费电子产业部, '
'columnFilters: [{field: src_work_order, values: [WO-7]}]}):'
'九月规格1 综合报废重量 = 10.25 综合合格率 = 0.9123')
fact['text'] = raw_text
fact['displayText'] = raw_text
source_text = fact['text']
source_scope = copy.deepcopy(fact['scope'])
projected = _history_model_fact_projection(candidate, [fact])[0]
self.assertIn('产业部 消费电子产业部', projected['modelScope']['displayScope'])
self.assertIn('业务分组 消费电子-3C', projected['modelScope']['displayScope'])
self.assertIn('黑片委托单号 包含 WO-7', projected['modelScope']['displayScope'])
self.assertIn('消费电子产业部', projected['modelText'])
self.assertIn('消费电子-3C', projected['modelText'])
self.assertIn('黑片委托单号', projected['modelText'])
self.assertIn('WO-7', projected['modelText'])
for machine_label in ('筛选 {', 'group:', 'department:', 'columnFilters:', 'src_work_order'):
self.assertNotIn(machine_label, projected['modelText'])
self.assertNotIn(machine_label, json.dumps(projected['modelScope'], ensure_ascii=False))
self.assertEqual(projected['text'], source_text)
self.assertEqual(projected['scope'], source_scope)
self.assertEqual(projected['scope']['filters'], raw_filters)
def test_model_source_goals_keep_exact_scope_label_without_machine_filters(self):
candidate = ranking_history_snapshot()
set_source_task_context(candidate, SOURCE_A,
{'sourceFieldLabels': {'src_customer_code': '客户代码'}})
goals = [{'goal': '客户质量分析', '_sourceTurnId': SOURCE_A,
'period': {'year': 2026, 'month': 9},
'dimensions': ['product'], 'fields': ['rate'],
'filters': {'department': '消费电子产业部', 'group': '消费电子-3C',
'extraFilters': {'src_customer_code': ['800001']}}}]
view = _history_model_source_goals(candidate, goals)
self.assertEqual(len(view), 1)
self.assertNotIn('filters', view[0])
self.assertNotIn('_sourceTurnId', view[0])
self.assertIn('产业部 消费电子产业部', view[0]['displayScopeLabel'])
self.assertIn('业务分组 消费电子-3C', view[0]['displayScopeLabel'])
self.assertIn('客户代码 800001', view[0]['displayScopeLabel'])
self.assertNotIn('src_customer_code', json.dumps(view, ensure_ascii=False))
def test_actual_history_scope_projection_reaches_synthesis_and_review_without_machine_filters(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
source_a_section = candidate['sections'][0]
source_a_section['filters']['columnFilters'] = [{
'field': 'src_work_order', 'stages': ['black'], 'operator': 'in', 'values': ['WO-7'],
}]
for fact in candidate['facts']:
section = candidate['sections'][fact['sectionIndex']]
fact['scope']['filters'] = copy.deepcopy(section['filters'])
first_fact = candidate['facts'][0]
first_fact['text'] = (
'2026年9月(筛选 {group: 消费电子-3C, department: 消费电子产业部, '
'columnFilters: [{field: src_work_order, values: [WO-7]}]}):'
'九月规格1 综合报废重量 = 10.25 综合合格率 = 0.9123'
)
first_fact['displayText'] = first_fact['text']
set_source_task_context(candidate, SOURCE_A, {
'goal': '保留九月3C重点排名', 'question': '九月3C重点排名',
'period': {'year': 2026, 'month': 9},
'filters': copy.deepcopy(source_a_section['filters']),
'dimensions': ['product'], 'stages': [], 'fields': ['scrapWeight'],
'sourceFieldLabels': {'src_work_order': '黑片委托单号'},
'queryPlan': [{'purpose': 'ranking', 'dimension': 'product',
'fields': ['scrapWeight'], 'stages': [], 'limit': 5}],
})
set_source_task_context(candidate, SOURCE_B, {
'goal': '保留八月全公司排名', 'question': '八月全公司排名',
'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimensions': ['product'], 'stages': [], 'fields': ['scrapWeight'],
'queryPlan': [{'purpose': 'ranking', 'dimension': 'product',
'fields': ['scrapWeight'], 'stages': [], 'limit': 3}],
})
signed_facts = copy.deepcopy(candidate['facts'])
signed_sections = copy.deepcopy(candidate['sections'])
captured = {}
def provider(request, timeout):
body = json.loads(request.data)
payload = json.loads(body['messages'][1]['content'])
if body['messages'][0]['content'].startswith('逐句核验证据'):
captured['review'] = payload
return response({'reviews': [{'supported': True, 'statisticalClaimsSupported': True}
for _ in payload['blocks']],
'missingGoalDescriptions': []})
captured['synthesis'] = payload
return response({'reportTitle': '历史多范围报废排名', 'blocks': [{
'role': 'core', 'text': '已按九月和八月各自的确认范围保留重点排名。',
'factIds': [payload['facts'][0]['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.assertEqual(result['evidenceSufficient'], True, result['compositionDiagnostics'])
self.assertTrue(result['titleRequired'])
self.assertEqual(candidate['facts'], signed_facts)
self.assertEqual(candidate['sections'], signed_sections)
self.assertEqual(candidate['facts'][0]['scope']['filters']['columnFilters'][0]['field'], 'src_work_order')
for phase in ('synthesis', 'review'):
payload = captured[phase]
encoded = json.dumps(payload, ensure_ascii=False)
self.assertNotIn('筛选 {', encoded)
self.assertNotIn('group:', encoded)
self.assertNotIn('department:', encoded)
self.assertNotIn('columnFilters', encoded)
self.assertNotIn('src_work_order', encoded)
goals = payload['historySourceGoals']
self.assertTrue(any('产业部 消费电子产业部' in goal['displayScopeLabel']
and '业务分组 消费电子-3C' in goal['displayScopeLabel']
and '黑片委托单号 包含 WO-7' in goal['displayScopeLabel']
for goal in goals))
self.assertTrue(all('filters' not in goal and '_sourceTurnId' not in goal for goal in goals))
self.assertTrue(all('filters' not in scope and 'sourceTurnId' not in scope
for scope in payload['scopes']))
projected = next(fact for fact in payload['facts']
if fact.get('evidenceRef') == 'history-fact:rank-0-0')
self.assertIn('消费电子产业部', projected['text'])
self.assertIn('消费电子-3C', projected['text'])
self.assertIn('黑片委托单号', projected['text'])
self.assertIn('WO-7', projected['text'])
self.assertEqual(projected['scopeId'], payload['scopes'][0]['scopeId'])
def test_combined_history_rank_binds_official_rate_and_share_display_aliases_to_same_row(self):
from nao_native_answer import _binding_inventory, synthesize_history_snapshot
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
add_formal_mass_units(candidate)
original_facts = copy.deepcopy(candidate['facts'])
original_sections = copy.deepcopy(candidate['sections'])
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'titleValid': True, 'titleGeneratedByModel': True,
'titleSource': 'model_verified_content', 'reportTitle': '2026年历史质量排名'}
with patch('nao_native_answer._synthesize_verified_report',side_effect=capture_finalizer):
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())
report_facts = captured['report']['facts']
self.assertEqual(len(report_facts),13)
self.assertFalse(any('附录' in note for note in captured['report']['presentationNotes']))
first = report_facts[0]
tokens = _binding_inventory(report_facts)
first_values = [token for token in tokens.values() if token.get('factId') == first['id']]
self.assertTrue(any(token.get('value') == '91.23%' and token.get('unit') == '%'
and token.get('metricField') == 'rate' for token in first_values),first_values)
self.assertTrue(any(token.get('value') == '10.25' and token.get('unit') == 'kg'
and token.get('metricField') == 'scrapWeight' for token in first_values),first_values)
self.assertTrue(any(token.get('value') == '0.25' and token.get('unit') == 'kg'
and token.get('metricField') == 'scrapWeight' for token in first_values),first_values)
self.assertTrue(any(token.get('value') == '20.00%' and token.get('unit') == '%'
for token in first_values),first_values)
self.assertTrue(any(item.get('metricCode') == 'compositeRate'
and item.get('rawUnit') == 'ratio' and item.get('rawValue') == .9123
and item.get('unit') == 'percent' and item.get('displayValue') == '91.23%'
for item in first.get('verifiedDisplayValues', [])))
self.assertTrue(any(item.get('metricCode') == 'share' and item.get('rawValue') == .2
and item.get('rawUnit') == 'ratio' and item.get('displayValue') == '20.00%'
for item in first.get('verifiedDisplayValues', [])))
self.assertEqual(first['scope']['filters'], candidate['sections'][0]['filters'])
self.assertEqual(candidate['facts'], original_facts)
self.assertEqual(candidate['sections'], original_sections)
self.assertEqual(result['titleValid'],True)
def test_combined_history_row_values_pass_synthesis_and_review_bindings(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
add_formal_mass_units(candidate)
phases = []
def provider(request, timeout):
body = json.loads(request.data)
payload = json.loads(body['messages'][1]['content'])
if body['messages'][0]['content'].startswith('逐句核验证据'):
phases.append('review')
self.assertIn('91.23%',payload['blocks'][0]['text'])
return response({'reviews': [{'index': 0, 'supported': True,
'statisticalClaimsSupported': True}],
'missingGoalDescriptions': []})
phases.append('synthesis')
first = next(fact for fact in payload['facts'] if fact['label'] == '九月规格1')
values = {value['metricCode']: value for value in first.get('values', [])}
self.assertEqual(values['compositeRate']['unit'],'percent')
self.assertEqual(values['scrapWeight']['unit'],'kg')
self.assertEqual(values['share']['displayValue'],'20.00%')
return response({'reportTitle':'2026年8月至9月历史排名表现','blocks':[{
'text':'九月规格1综合合格率91.23%,综合报废重量10.25 kg,黑片报废重量0.25 kg,'
'亮片报废重量0.75 kg,占同范围全部报废20.00%,累计占比20.00%。',
'factIds':[first['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('91.23%',result['answer'])
self.assertIn('10.25 kg',result['answer'])
self.assertIn('20.00%',result['answer'])
self.assertEqual(phases,['synthesis','review'])
def test_history_rank_does_not_borrow_unit_from_different_chart_scope(self):
from nao_native_answer import _binding_inventory, synthesize_history_snapshot
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
candidate['visuals'][0]['scope']['group'] = '另一业务分组'
candidate['historyVisualSources'][0]['scope'] = copy.deepcopy(candidate['visuals'][0]['scope'])
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
return {'answer':'已核验','readingSummary':{'evidenceRefs':[]},
'titleValid':True,'titleGeneratedByModel':True,
'titleSource':'model_verified_content','reportTitle':'历史报废排名'}
with patch('nao_native_answer._synthesize_verified_report',side_effect=capture_finalizer):
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())
first = captured['report']['facts'][0]
tokens = [token for token in _binding_inventory(captured['report']['facts']).values()
if token.get('factId') == first['id'] and token.get('value') == '10.25']
self.assertTrue(any(token.get('unit') == 'source_weight' for token in tokens),tokens)
self.assertFalse(any(token.get('unit') == 'kg' for token in tokens),tokens)
def test_history_rank_chart_unit_requires_same_signed_categories_and_series_values(self):
from nao_native_answer import _binding_inventory, synthesize_history_snapshot
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
# The source facts deliberately have only the storage placeholder
# source_weight. A same-scope chart with one mismatched data point
# cannot establish kg for this row set.
candidate['visuals'][0]['series'][0]['values'][0] += 1
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'titleValid': True, 'titleGeneratedByModel': True,
'titleSource': 'model_verified_content', 'reportTitle': '历史报废排名'}
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
first = captured['report']['facts'][0]
self.assertEqual(first['historyCollection']['unit'], None)
tokens = [token for token in _binding_inventory(captured['report']['facts']).values()
if token.get('factId') == first['id'] and token.get('metricField') == 'scrapWeight']
self.assertTrue(any(token.get('unit') == 'source_weight' for token in tokens), tokens)
self.assertFalse(any(token.get('unit') == 'kg' for token in tokens), tokens)
def test_actual_history_float_projection_reaches_prompt_and_review_as_percent_and_kg(self):
from nao_native_answer import synthesize_history_snapshot
from test_native_answer import response
candidate = actual_precision_ranking_snapshot()
phases = []
captured = {}
def provider(request, timeout):
body = json.loads(request.data)
payload = json.loads(body['messages'][1]['content'])
if body['messages'][0]['content'].startswith('逐句核验证据'):
phases.append('review')
text_value = payload['blocks'][0]['text']
captured['reviewText'] = text_value
return response({'reviews': [{'index': 0, 'supported': True,
'statisticalClaimsSupported': True}],
'missingGoalDescriptions': []})
phases.append('synthesis')
first = payload['facts'][0]
values = {value['metricCode']: value for value in first.get('values', [])}
captured['modelText'] = first['text']
captured['modelValues'] = values
return response({'reportTitle': '多范围历史报废排名', 'blocks': [{
'role': 'core',
'text': '九月规格1综合合格率91.23%,综合报废重量493.9966 kg,累计占同范围报废7.08%。',
'factIds': [first['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.assertEqual(phases, ['synthesis', 'review'])
self.assertIn('综合合格率 = 91.23%', captured['modelText'])
self.assertEqual(captured['modelValues']['compositeRate'],
{'metricCode': 'compositeRate', 'unit': 'percent', 'displayValue': '91.23%'})
self.assertEqual(captured['modelValues']['scrapWeight']['unit'], 'kg')
self.assertEqual(captured['modelValues']['share']['displayValue'], '7.08%')
bound = next(fact for fact in result['fullEvidenceFacts']
if fact.get('evidenceRef') == candidate['facts'][0]['id'])
typed = {item['metricCode']: item for item in bound.get('verifiedDisplayValues', [])}
self.assertEqual(typed['compositeRate'], {'metricCode': 'compositeRate',
'rawValue': '0.9122755444707148916393578115770785', 'rawUnit': 'ratio',
'unit': 'percent', 'displayValue': '91.23%'})
self.assertEqual(typed['scrapWeight']['rawValue'], '493.99660000')
self.assertEqual(typed['share']['rawValue'], '0.07078164231313173578917252155832738')
self.assertIn('91.23%', captured['reviewText'])
self.assertIn('493.9966 kg', captured['reviewText'])
self.assertIn('7.08%', captured['reviewText'])
def test_history_synthesis_carries_noncommon_scope_guard_and_row_bound_rank_groups(self):
from nao_history_report import _noncommon_scope_literals
from nao_native_answer import (synthesize_history_snapshot, _content_title_guide,
_narrative_bindings, _mask_structural_counts)
candidate = ranking_history_snapshot()
candidate['sections'][0].pop('dimension', None)
candidate['sections'][1]['dimension'] = 'department'
candidate['sections'][2]['dimension'] = 'group'
candidate['facts'] = facts_for_ranking_sections(candidate)
add_formal_mass_units(candidate)
fields = ['scrapWeight', 'rate']
for source_turn_id, section_index in ((SOURCE_A, 0), (SOURCE_B, 1)):
section = candidate['sections'][section_index]
dimension = section.get('dimension') or 'product'
limit = len(section['rows'])
set_source_task_context(candidate, source_turn_id, {
'period': copy.deepcopy(section['period']), 'filters': copy.deepcopy(section['filters']),
'dimensions': [dimension], 'fields': fields, 'stages': [],
'queryPlan': [{'purpose': 'ranking', 'dimension': dimension, 'limit': limit,
'fields': fields, 'stages': []}]})
for chart in candidate['visuals']:
chart_section = next(section for section in candidate['sections']
if section['sourceTurnId'] == chart['originTurnId']
and section['period'] == chart['period']
and section['filters'] == {key: value for key, value in chart['scope'].items()
if key in ('department', 'group', 'product', 'projectGroup')})
count = len(chart_section['rows'])
chart['dimension'] = chart_section.get('dimension') or 'product'
chart['coverage'] = {'totalRows': count, 'returnedRows': count, 'truncated': False,
'shareBasis': 'all_scope_weight', 'denominator': 100}
for lineage in candidate['historyVisualSources']:
lineage['dimension'] = next(chart['dimension'] for chart in candidate['visuals']
if chart['id'] == lineage['visualId'])
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
captured['context'] = copy.deepcopy(kwargs['context'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'reportTitle': '多范围历史报废排名', 'titleValid': True,
'titleGeneratedByModel': True, 'titleSource': 'model_verified_content'}
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
report, context = captured['report'], captured['context']
expected_exclusions = sorted(_noncommon_scope_literals(candidate))
self.assertEqual(context['_historyTitleExcludedScopeLiterals'], expected_exclusions)
guide = _content_title_guide(report, context)
self.assertEqual(guide['excludedScopeLabels'], expected_exclusions)
self.assertIn('消费电子-3C', guide['excludedScopeLabels'])
ranked = [fact for fact in report['facts'] if 'rankGroupKey' in fact]
self.assertEqual(len(ranked), 13)
self.assertTrue(all(type(fact.get('rank')) is int for fact in ranked))
self.assertEqual({fact['kind'] for fact in ranked}, {'product', 'department', 'group'})
section_groups = {}
for fact in ranked:
identity = (fact['scope']['sourceTurnId'], fact['scope']['sectionIndex'])
section_groups.setdefault(identity, set()).add(fact['rankGroupKey'])
self.assertEqual(len(section_groups), 3)
self.assertTrue(all(len(keys) == 1 for keys in section_groups.values()))
self.assertEqual(len({next(iter(keys)) for keys in section_groups.values()}), 3)
for section_index, expected_count in ((0, 5), (1, 5), (2, 3)):
section_facts = [fact for fact in ranked if fact['scope']['sectionIndex'] == section_index]
self.assertEqual(sorted(fact['rank'] for fact in section_facts), list(range(1, expected_count + 1)))
bindings = _narrative_bindings(ranked)
for section_index, expected_count in ((0, 5), (1, 5)):
section_facts = [fact for fact in ranked if fact['scope']['sectionIndex'] == section_index]
masked = _mask_structural_counts(f'Top{expected_count}已保存排名', section_facts, bindings)
self.assertNotIn(f'Top{expected_count}', masked)
self.assertIn('Top999', _mask_structural_counts('Top999已保存排名', section_facts, bindings))
august_facts = [fact for fact in ranked if fact['scope']['sectionIndex'] == 2]
self.assertIn('Top3', _mask_structural_counts('Top3已保存排名', august_facts, bindings))
def test_history_rank_alias_requires_structured_dimension_and_never_guesses_from_title(self):
from nao_native_answer import synthesize_history_snapshot
candidate = ranking_history_snapshot()
for section in candidate['sections']:
section.pop('dimension', None)
candidate['facts'] = facts_for_ranking_sections(candidate)
for fact in candidate['facts']:
fact['scope'].pop('dimension', None)
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
captured['context'] = copy.deepcopy(kwargs['context'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'reportTitle': '历史质量排名', 'titleValid': True,
'titleGeneratedByModel': True, 'titleSource': 'model_verified_content'}
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
ranked = next(fact for fact in captured['report']['facts'] if '九月规格1' in fact.get('text', ''))
self.assertNotIn('rankGroupKey', ranked)
self.assertFalse(any(value.get('metricCode') == 'compositeRate'
for value in ranked.get('verifiedDisplayValues', [])))
self.assertIn('综合合格率 = 0.9123', ranked['text'])
self.assertEqual(candidate['sections'][0]['title'], '2026年9月3C Top5产品综合报废重量排名')
def test_history_local_cohort_uses_signed_collection_metadata_and_exact_scope(self):
from nao_native_answer import (_binding_inventory, _mask_structural_counts,
_history_local_cohort, _narrative_bindings,
synthesize_history_snapshot)
candidate = ranking_history_snapshot()
section = copy.deepcopy(candidate['sections'][2])
section['sourceTurnId'] = SOURCE_B
candidate['sections'] = [section]
candidate['facts'] = facts_for_ranking_sections(candidate)
add_formal_mass_units(candidate)
chart = next(chart for chart in candidate['visuals']
if chart['originTurnId'] == SOURCE_B and chart['period']['month'] == 8)
chart['coverage'] = {'totalRows': 2249, 'returnedRows': 3, 'truncated': True,
'shareBasis': 'all_scope_weight', 'denominator': 62726.7476}
candidate['visuals'] = [chart]
candidate['historyVisualSources'] = [next(lineage for lineage in candidate['historyVisualSources']
if lineage['visualId'] == chart['id'])]
query = {'limit': 3, 'fields': ['rate', 'scrapWeight'], 'nodeId': 'rank',
'stages': [], 'purpose': 'ranking', 'selector': {'kind': 'base'},
'dimension': 'product'}
set_source_task_context(candidate, SOURCE_B, {'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimensions': ['product'], 'fields': ['rate', 'scrapWeight'], 'stages': [],
'queryPlan': [{key: copy.deepcopy(value) for key, value in query.items()
if key in ('purpose', 'dimension', 'fields', 'stages', 'limit')}]})
captured = {}
def capture_finalizer(**kwargs):
captured['report'] = copy.deepcopy(kwargs['report'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'reportTitle': '历史规格排名', 'titleValid': True,
'titleGeneratedByModel': True, 'titleSource': 'model_verified_content'}
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
facts = captured['report']['facts']
self.assertEqual(len(facts), 3)
self.assertTrue(all(fact['historyCollection']['complete'] is True for fact in facts))
self.assertTrue(all(fact['historyCollection']['requestedLimit'] == 3 for fact in facts))
self.assertTrue(all(fact['historyCollection']['returnedCount'] == 3 for fact in facts))
self.assertTrue(all(fact['historyCollection']['totalRows'] == 2249 for fact in facts))
self.assertTrue(all(fact['historyCollection']['denominator'] == '62726.7476' for fact in facts))
self.assertTrue(all(fact['historyCollection']['unit'] == 'kg' for fact in facts))
self.assertEqual(facts[0]['historyCollection']['memberFactIds'], [fact['id'] for fact in facts])
tokens = _binding_inventory(facts)
facts_by_id = {fact['id']: fact for fact in facts}
ids = [fact['id'] for fact in facts]
location = {'kind': 'prose', 'prefix': '2026年8月Top3 ', 'periodText': '2026年8月',
'containerLabel': '', 'ancestors': []}
cohort, reason = _history_local_cohort(location, {'factIds': ids}, tokens, facts_by_id,
metric='scrapWeight', stage='overall', unit='kg')
self.assertIsNone(reason)
self.assertEqual(cohort['memberFactIds'], ids)
self.assertEqual((cohort['requestedLimit'], cohort['returnedCount'], cohort['totalRows']), (3, 3, 2249))
self.assertEqual(cohort['denominator'], '62726.7476')
wrong_period, _ = _history_local_cohort(
{**location, 'prefix': '2026年9月Top3 ', 'periodText': '2026年9月'},
{'factIds': ids}, tokens, facts_by_id, metric='scrapWeight', stage='overall', unit='kg')
self.assertIsNone(wrong_period)
wrong_stage, _ = _history_local_cohort(location, {'factIds': ids}, tokens, facts_by_id,
metric='scrapWeight', stage='black', unit='kg')
self.assertIsNone(wrong_stage)
wrong_metric, _ = _history_local_cohort(location, {'factIds': ids}, tokens, facts_by_id,
metric='totalWeight', stage='overall', unit='kg')
self.assertIsNone(wrong_metric)
partial, _ = _history_local_cohort(location, {'factIds': ids[:-1]}, tokens, facts_by_id,
metric='scrapWeight', stage='overall', unit='kg')
self.assertIsNotNone(partial)
self.assertEqual(partial['memberFactIds'], ids)
absent, _ = _history_local_cohort(location, {'factIds': ['other-fact']}, tokens, facts_by_id,
metric='scrapWeight', stage='overall', unit='kg')
self.assertIsNone(absent)
source_b = next(source for source in candidate['historySources'] if source['turnId'] == SOURCE_B)
source_b['taskContext']['queryPlan'] = []
captured.clear()
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
facts_without_limit = captured['report']['facts']
self.assertTrue(all(fact['historyCollection']['requestedLimit'] is None for fact in facts_without_limit))
self.assertTrue(all(fact['historyCollection']['returnedCount'] == 3 for fact in facts_without_limit))
tokens_without_limit = _binding_inventory(facts_without_limit)
masked = _mask_structural_counts('2026年8月Top3已保存排名', facts_without_limit,
_narrative_bindings(facts_without_limit), all_facts=facts_without_limit, tokens=tokens_without_limit)
self.assertIn('Top3', masked)
def test_history_denominator_literals_bind_to_their_signed_collection_owners(self):
from nao_native_answer import (NativeCompositionError, _bind_literal_measures, _validate_blocks)
def make_facts(august_denominator='62726.7476'):
facts = []
cohorts = (
('september', 'source-september', 0, 9,
['F1', 'F3', 'F5', 'F7', 'F8'], '6979.16273',
{'department': '消费电子产业部', 'group': '消费电子-3C'}),
('august', 'source-august', 1, 8,
['F2', 'F4', 'F6'], august_denominator, {}),
)
for name, source_turn, section_index, month, member_ids, denominator, filters in cohorts:
group_key = 'history-rank:' + name
period = {'year': 2026, 'month': month}
scope = {'period': period, 'filters': filters,
'dimension': 'product', 'stage': 'overall'}
collection = {
'verified': True, 'rankGroupKey': group_key,
'sourceTurnId': source_turn, 'sectionIndex': section_index,
'scope': copy.deepcopy(scope), 'period': copy.deepcopy(period),
'dimension': 'product', 'metric': 'scrapWeight', 'stage': 'overall',
'metadataField': 'weight', 'unit': 'kg', 'complete': True,
'memberFactIds': list(member_ids), 'requestedLimit': len(member_ids),
'returnedCount': len(member_ids), 'totalRows': 200,
'truncated': True, 'denominator': denominator,
'shareBasis': 'all_scope_weight',
}
for rank, fact_id in enumerate(member_ids, 1):
facts.append({
'id': fact_id, 'kind': 'product', 'label': f'规格{fact_id}',
'text': f'2026年{month}月规格{fact_id}报废重量正式排名第{rank}',
'rank': rank, 'rankGroupKey': group_key,
'sourceTurnId': source_turn, 'sectionIndex': section_index,
'evidenceRef': 'history:' + fact_id, 'scope': copy.deepcopy(scope),
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'historyCollection': copy.deepcopy(collection),
})
return facts
facts = make_facts()
fact_ids = [fact['id'] for fact in facts]
issued_tokens = {
'P1': {'value': '2026年9月', 'factId': 'F1', 'evidenceRef': 'history:F1',
'calendarPeriod': True},
'P2': {'value': '2026年8月', 'factId': 'F2', 'evidenceRef': 'history:F2',
'calendarPeriod': True},
}
text = ('{{P1}}与{{P2}}两个期间的筛选范围和正式分母不同'
'(6979.16273000 与 62726.74760000),排名不可直接横向对比。')
parsed = {'blocks': [{'text': text, 'factIds': fact_ids, 'role': 'core'}]}
diagnostics = {}
tokens = copy.deepcopy(issued_tokens)
bound = _bind_literal_measures(parsed, tokens, diagnostics, facts=facts)
bound_text = bound['blocks'][0]['text']
self.assertRegex(bound_text, r'\{\{N[1-9][0-9]*\}\} kg 与 \{\{N[1-9][0-9]*\}\} kg')
bindings = diagnostics['historyCollectionBindings']
self.assertEqual([(item['period']['month'], item['rankGroupKey']) for item in bindings],
[(9, 'history-rank:september'), (8, 'history-rank:august')])
rendered, _ = _validate_blocks(bound, facts, tokens)
self.assertIn('6979.16273 kg 与 62726.7476 kg', rendered[0]['text'])
# An explicit single-period anchor cannot borrow the other period's
# denominator, even when that other cohort is cited in the same block.
wrong_period = {'blocks': [{'text': '{{P1}} 单期正式分母为 62726.74760000 kg。',
'factIds': fact_ids, 'role': 'core'}]}
wrong_period_diagnostics = {}
wrong_period = _bind_literal_measures(wrong_period, copy.deepcopy(issued_tokens),
wrong_period_diagnostics, facts=facts)
self.assertNotIn('{{N', wrong_period['blocks'][0]['text'])
self.assertFalse(wrong_period_diagnostics.get('historyCollectionBindings'))
# Equal denominators in two cited collections are ambiguous by value;
# no row order or last-mentioned period may break the tie.
duplicate_facts = make_facts(august_denominator='6979.16273')
duplicate_tokens = copy.deepcopy(issued_tokens)
duplicate = {'blocks': [{'text': '{{P1}}与{{P2}}两个期间正式分母不同(6979.16273)。',
'factIds': [fact['id'] for fact in duplicate_facts], 'role': 'core'}]}
duplicate_diagnostics = {}
duplicate = _bind_literal_measures(duplicate, duplicate_tokens, duplicate_diagnostics,
facts=duplicate_facts)
self.assertNotIn('{{N', duplicate['blocks'][0]['text'])
self.assertFalse(duplicate_diagnostics.get('historyCollectionBindings'))
# A matching denominator is insufficient when one signed member no
# longer matches its collection scope or the collection field/metric
# pairing is inconsistent.
for mutation in ('member_scope', 'metadata_field'):
with self.subTest(mutation=mutation):
invalid_facts = make_facts()
august_ids = {'F2', 'F4', 'F6'}
if mutation == 'member_scope':
next(fact for fact in invalid_facts if fact['id'] == 'F2')['scope']['filters'] = {
'department': 'different department'}
else:
for fact in invalid_facts:
if fact['id'] in august_ids:
fact['historyCollection']['metadataField'] = 'totalWeight'
invalid_tokens = copy.deepcopy(issued_tokens)
invalid = {'blocks': [{'text': text, 'factIds': [fact['id'] for fact in invalid_facts],
'role': 'core'}]}
invalid_diagnostics = {}
invalid = _bind_literal_measures(invalid, invalid_tokens, invalid_diagnostics,
facts=invalid_facts)
invalid_text = invalid['blocks'][0]['text']
self.assertRegex(invalid_text, r'\{\{N[1-9][0-9]*\}\} kg 与 62726\.74760000')
self.assertIn('62726.74760000', invalid_text)
self.assertEqual([item['rankGroupKey'] for item in
invalid_diagnostics.get('historyCollectionBindings', [])],
['history-rank:september'])
# A synthetic denominator token created for one block cannot become a
# reusable value token in a later, differently scoped sentence.
leaked = {'blocks': [
{'text': text, 'factIds': fact_ids, 'role': 'core'},
{'text': '{{P1}} 单期正式分母为 62726.74760000 kg。',
'factIds': fact_ids, 'role': 'core'},
]}
leaked_tokens = copy.deepcopy(issued_tokens)
leaked = _bind_literal_measures(leaked, leaked_tokens, {}, facts=facts)
self.assertNotIn('{{N', leaked['blocks'][1]['text'])
with self.assertRaises(NativeCompositionError):
_validate_blocks(leaked, facts, leaked_tokens)
def test_history_top_count_uses_one_explicit_scope_cohort_and_checks_listed_members(self):
from nao_native_answer import (_history_local_cohort, _mask_structural_counts,
_narrative_bindings)
def ranked_fact(fact_id, rank, group_key, month):
label = f'规格{fact_id}'
return {'id': fact_id, 'kind': 'product', 'label': label, 'rank': rank,
'rankGroupKey': group_key, 'evidenceRef': 'history:' + fact_id,
'text': f'2026年{month}月{label}报废重量正式排名第{rank}',
'scope': {'period': {'year': 2026, 'month': month}, 'filters': {},
'dimension': 'product', 'stage': 'overall', 'sectionIndex': 0 if month == 9 else 1},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'}}
september_key, august_key = 'history-rank:september', 'history-rank:august'
full = [ranked_fact(f'S{rank}', rank, september_key, 9) for rank in range(1, 6)]
full.extend(ranked_fact(f'A{rank}', rank, august_key, 8) for rank in range(1, 4))
for group_key, rows, month, limit in ((september_key, full[:5], 9, 5),
(august_key, full[5:], 8, 3)):
first_scope = {key: copy.deepcopy(value) for key, value in rows[0]['scope'].items()
if key not in ('sourceTurnId', 'rowIndex', 'scopeId')}
collection = {'verified': True, 'rankGroupKey': group_key,
'sourceTurnId': 'source-' + str(month), 'sectionIndex': 0 if month == 9 else 1,
'scope': first_scope, 'period': {'year': 2026, 'month': month},
'dimension': 'product', 'metric': 'scrapWeight', 'stage': 'overall',
'metadataField': 'weight', 'unit': 'kg', 'complete': True,
'memberFactIds': [fact['id'] for fact in rows], 'requestedLimit': limit,
'returnedCount': len(rows), 'totalRows': 100 if month == 9 else 200,
'truncated': True, 'denominator': '5000', 'shareBasis': 'all_scope_weight'}
for fact in rows:
fact['historyCollection'] = copy.deepcopy(collection)
by_id = {fact['id']: fact for fact in full}
tokens = {
'P9': {'factId': 'S1', 'value': '2026年9月', 'calendarPeriod': True},
'P8': {'factId': 'A1', 'value': '2026年8月', 'calendarPeriod': True},
'I1': {'factId': 'S1', 'value': '规格S1', 'productIdentifier': True},
'I2': {'factId': 'S3', 'value': '规格S3', 'productIdentifier': True},
'I3': {'factId': 'A1', 'value': '规格A1', 'productIdentifier': True},
'I4': {'factId': 'A2', 'value': '规格A2', 'productIdentifier': True},
'I5': {'factId': 'A3', 'value': '规格A3', 'productIdentifier': True},
}
bindings = _narrative_bindings(full)
# The signed September cohort has ranks 1..5, although this sentence
# cites only two September rows as examples. Its explicit period selects
# that single cohort without moving August into the Top 5.
selected = [by_id['S1'], by_id['S3']]
positive = _mask_structural_counts('{{P9}} Top 5中{{I1}}、{{I2}}仅作例子', selected,
bindings, all_facts=full, tokens=tokens)
self.assertNotIn('Top 5', positive)
# Two September plus three August members cannot be combined into one
# unscoped Top 5, and an explicit September anchor cannot include August
# members either.
mixed_ids = ['S1', 'S3', 'A1', 'A2', 'A3']
mixed = [by_id[key] for key in mixed_ids]
old_negative = _mask_structural_counts(
'Top 5中{{I1}}、{{I2}}、{{I3}}、{{I4}}、{{I5}}', mixed,
bindings, all_facts=full, tokens=tokens)
self.assertIn('Top 5', old_negative)
explicit_mixed = _mask_structural_counts(
'{{P9}} Top 5包括{{I1}}、{{I2}}、{{I3}}、{{I4}}、{{I5}}', mixed,
bindings, all_facts=full, tokens=tokens)
self.assertIn('Top 5', explicit_mixed)
self.assertIn('Top999', _mask_structural_counts('{{P9}} Top999', selected,
bindings, all_facts=full, tokens=tokens))
# Stage-value predicates can inherit the signed period from their
# nearest governing ranking predicate. Without that local predecessor
# the same facts contain two valid cohorts and must remain ambiguous.
anchored, anchored_reason = _history_local_cohort(
{'kind': 'prose', 'prefix': '黑片 ', 'periodText': None,
'containerLabel': '', 'ancestors': [],
'precedingPredicates': ['{{P9}} Top 5规格的报废重量']},
{'factIds': [fact['id'] for fact in full]}, tokens, by_id)
self.assertIsNone(anchored_reason)
self.assertEqual(anchored['rankGroupKey'], september_key)
latest_period, latest_reason = _history_local_cohort(
{'kind': 'prose', 'prefix': '{{P9}} Top 5 规格与 {{P8}} Top 3 ',
'periodText': None, 'containerLabel': '', 'ancestors': []},
{'factIds': [fact['id'] for fact in full]}, tokens, by_id)
self.assertIsNone(latest_reason)
self.assertEqual(latest_period['rankGroupKey'], august_key)
paired_counts = _mask_structural_counts('{{P9}} {{I1}} Top 5 与 {{P8}} Top 3 已分别核对',
full, bindings, all_facts=full, tokens=tokens)
self.assertNotIn('Top 5', paired_counts)
self.assertNotIn('Top 3', paired_counts)
unanchored, unanchored_reason = _history_local_cohort(
{'kind': 'prose', 'prefix': '黑片 ', 'periodText': None,
'containerLabel': '', 'ancestors': []},
{'factIds': [fact['id'] for fact in full]}, tokens, by_id)
self.assertIsNone(unanchored)
self.assertEqual(unanchored_reason, 'ambiguous_or_missing_local_history_cohort')
def test_history_list_item_uses_ast_owner_stage_without_neighbor_stage_conflict(self):
from nao_native_answer import _history_mass_context
location = {'kind': 'list_item', 'fieldText': '报废重量 {{P10}} {{I11}} 黑片 {{N82}} kg 高于亮片',
'metricFamily': 'scrap', 'metricFamilySource': 'parent', 'ownerStage': 'bright'}
context = _history_mass_context(location)
self.assertTrue(context['valid'])
self.assertEqual(context['metric'], 'scrapWeight')
self.assertEqual(context['stage'], '亮片')
self.assertEqual(context['unit'], 'kg')
def test_history_rank_table_mass_owner_comes_from_same_ast_row_only(self):
from nao_native_answer import _history_table_mass_candidates
def product_fact(fact_id, label, month):
return {'id': fact_id, 'kind': 'product', 'label': label,
'text': f'2026年{month}月{label}综合报废重量 = 493.99660000 kg。',
'evidenceRef': 'history:' + fact_id,
'scope': {'period': {'year': 2026, 'month': month}, 'filters': {},
'dimension': 'product', 'stage': 'overall'},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': label}],
'verifiedDisplayValues': [{'metricCode': 'scrapWeight', 'rawValue': '493.99660000',
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '493.9966'}]}
facts = {'F1': product_fact('F1', '规格A', 9), 'F2': product_fact('F2', '规格B', 9),
'F3': product_fact('F3', '规格C', 8)}
tokens = {
'I1': {'value': '规格A', 'factId': 'F1', 'productIdentifier': True},
'I2': {'value': '规格B', 'factId': 'F2', 'productIdentifier': True},
'I3': {'value': '规格C', 'factId': 'F3', 'productIdentifier': True},
'N1': {'value': '493.9966', 'rawValue': '493.9966', 'unit': 'kg',
'metricField': 'scrapWeight', 'stageHint': '综合', 'factId': 'F1'},
'N2': {'value': '493.9966', 'rawValue': '493.9966', 'unit': 'kg',
'metricField': 'scrapWeight', 'stageHint': '综合', 'factId': 'F2'},
'N3': {'value': '493.9966', 'rawValue': '493.9966', 'unit': 'kg',
'metricField': 'scrapWeight', 'stageHint': '综合', 'factId': 'F3'},
}
location = {'kind': 'table', 'header': '综合报废重量(kg)', 'rowLabel': '1',
'rowCells': [{'text': '1', 'start': 0, 'end': 1},
{'text': '{{I1}}', 'start': 2, 'end': 8},
{'text': '{{N1}} kg', 'start': 9, 'end': 17}],
'columnIndex': 2, 'periodText': '2026年9月', 'ancestors': ()}
block = {'factIds': ['F1', 'F2', 'F3']}
positive = _history_table_mass_candidates(location, block, tokens, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertEqual([(key, unit) for key, _, unit in positive], [('N1', 'kg')])
# Another row's subject or a second subject in this same row cannot
# authorize a matching value, even when the formal number is identical.
wrong_row = {**location, 'rowCells': [location['rowCells'][0],
{'text': '{{I2}}', 'start': 2, 'end': 8}, location['rowCells'][2]]}
wrong_subject = _history_table_mass_candidates(wrong_row, block, tokens, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertFalse(any(key == 'N1' for key, _, _ in wrong_subject))
conflicting_row = {**location, 'rowCells': [location['rowCells'][0],
{'text': '{{I1}} {{I2}}', 'start': 2, 'end': 14}, location['rowCells'][2]]}
conflict = _history_table_mass_candidates(conflicting_row, block, tokens, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertEqual(conflict, [])
wrong_stage = _history_table_mass_candidates(location, block,
{**tokens, 'N1': {**tokens['N1'], 'stageHint': '亮片'}}, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertEqual(wrong_stage, [])
header_stage_conflict = _history_table_mass_candidates(
{**location, 'header': '黑片亮片综合报废重量(kg)'}, block, tokens, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertEqual(header_stage_conflict, [])
wrong_period = {**location, 'rowCells': [location['rowCells'][0],
{'text': '{{I3}}', 'start': 2, 'end': 8},
{'text': '{{N3}} kg', 'start': 9, 'end': 17}]}
wrong_period_value = _history_table_mass_candidates(wrong_period, block, tokens, facts,
Decimal('493.99660000'), explicit_unit='kg')
self.assertEqual(wrong_period_value, [])
def test_history_stage_mass_uses_exact_lineage_chart_unit_without_changing_raw_unit(self):
from nao_native_answer import (_binding_inventory, _history_table_mass_candidates,
synthesize_history_snapshot)
candidate = ranking_history_snapshot()
section = copy.deepcopy(candidate['sections'][0])
candidate['sections'] = [section]
candidate['facts'] = facts_for_ranking_sections(candidate)
chart = next(chart for chart in candidate['visuals']
if chart['originTurnId'] == SOURCE_A and chart['period']['month'] == 9)
candidate['visuals'] = [chart]
candidate['historyVisualSources'] = [next(lineage for lineage in candidate['historyVisualSources']
if lineage['visualId'] == chart['id'])]
query = {'limit': 5, 'fields': ['rate', 'scrapWeight'], 'stages': [],
'purpose': 'ranking', 'dimension': 'product'}
set_source_task_context(candidate, SOURCE_A, {
'period': {'year': 2026, 'month': 9}, 'filters': copy.deepcopy(section['filters']),
'dimensions': ['product'], 'fields': ['rate', 'scrapWeight'], 'stages': [],
'queryPlan': [{key: copy.deepcopy(value) for key, value in query.items()
if key in ('purpose', 'dimension', 'fields', 'stages', 'limit')}],
})
captured = {}
def capture_finalizer(**kwargs):
captured['facts'] = copy.deepcopy(kwargs['report']['facts'])
return {'answer': '已核验', 'readingSummary': {'evidenceRefs': []},
'reportTitle': '历史规格排名', 'titleValid': True,
'titleGeneratedByModel': True, 'titleSource': 'model_verified_content'}
with patch('nao_native_answer._synthesize_verified_report', side_effect=capture_finalizer):
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())
facts = captured['facts']
fact = next(item for item in facts if item.get('label') == section['rows'][0]['label'])
stage_alias = next(item for item in fact['verifiedDisplayValues']
if item.get('metricCode') == 'blackScrapWeight')
self.assertEqual((stage_alias['rawUnit'], stage_alias['unit']), ('source_weight', 'kg'))
self.assertEqual(Decimal(str(stage_alias['rawValue'])),
Decimal(str(section['rows'][0]['blackScrapWeight'])))
tokens = _binding_inventory(facts)
identifier = next(key for key, token in tokens.items()
if key.startswith('I') and token.get('factId') == fact['id']
and token.get('value') == fact['label'])
location = {'kind': 'prose', 'fieldText': '黑片报废重量为 ',
'subjectText': '2026年9月 {{' + identifier + '}} ',
'prefix': '黑片报废重量为 ', 'metricFamily': 'scrap',
'metricFamilySource': 'parent', 'ownerStage': 'black',
'predicateContext': '黑片报废重量为 '}
matches = _history_table_mass_candidates(location, {'factIds': [fact['id']]},
tokens, {item['id']: item for item in facts},
Decimal(str(section['rows'][0]['blackScrapWeight'])), explicit_unit='kg')
self.assertEqual([(token['factId'], unit) for _, token, unit in matches], [(fact['id'], 'kg')])
# If the value clause omits its field label, only the unique signed
# local ranking collection can provide a default metric. Its overall
# selection stage does not overwrite this value's AST-owned black stage.
period = next(key for key, token in tokens.items()
if key.startswith('P') and token.get('factId') == fact['id']
and token.get('calendarPeriod'))
implicit_metric = {**location, 'fieldText': '黑片 ',
'predicateContext': '', 'metricFamily': None,
'precedingPredicates': ['{{' + period + '}} Top 5规格报废重量']}
defaulted = _history_table_mass_candidates(implicit_metric,
{'factIds': [fact['id']]}, tokens, {item['id']: item for item in facts},
Decimal(str(section['rows'][0]['blackScrapWeight'])), explicit_unit='kg')
self.assertEqual([(token['factId'], unit) for _, token, unit in defaulted], [(fact['id'], 'kg')])
wrong_field = {**implicit_metric, 'metricFamily': 'input'}
rejected = _history_table_mass_candidates(wrong_field,
{'factIds': [fact['id']]}, tokens, {item['id']: item for item in facts},
Decimal(str(section['rows'][0]['blackScrapWeight'])), explicit_unit='kg')
self.assertEqual(rejected, [])
def test_history_no_draft_table_binds_bare_mass_only_from_same_row_typed_header_and_period(self):
from nao_native_answer import _bind_literal_measures, _binding_inventory
def history_fact(fact_id, period, label='规格A', text_value=None, values=None):
if text_value is None:
text_value = (f"{period['year']}年{period['month']}月:{label}综合报废重量 = 493.9966 kg;"
'黑片报废重量 = 164.199 kg;亮片报废重量 = 329.7976 kg。')
return {'id': fact_id, 'kind': 'product', 'label': label, 'text': text_value,
'evidenceRef': 'history:' + fact_id,
'scope': {'period': copy.deepcopy(period), 'filters': {'group': '业务组A'}, 'dimension': 'product'},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': label}], 'verifiedDisplayValues': values or [
{'metricCode': 'scrapWeight', 'rawValue': '493.99660000', 'rawUnit': 'source_weight',
'unit': 'kg', 'displayValue': '493.9966'},
{'metricCode': 'blackScrapWeight', 'rawValue': '164.19900000', 'rawUnit': 'source_weight',
'unit': 'kg', 'displayValue': '164.199'},
{'metricCode': 'brightScrapWeight', 'rawValue': '329.79760000', 'rawUnit': 'source_weight',
'unit': 'kg', 'displayValue': '329.7976'},
]}
september = history_fact('F1', {'year': 2026, 'month': 9}, values=[
{'metricCode': 'scrapWeight', 'rawValue': '493.9966', 'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '493.9966'},
{'metricCode': 'blackScrapWeight', 'rawValue': '164.199', 'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '164.199'},
{'metricCode': 'brightScrapWeight', 'rawValue': '329.7976', 'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '329.7976'},
])
facts = [september]
tokens = _binding_inventory(facts)
table = ('### 2026年9月\n| 规格 | 综合报废重量(kg) | 黑片报废重量(kg) | 亮片报废重量(kg) | 数量(件) |\n'
'|---|---:|---:|---:|---:|\n| 规格A | 493.99660000 | 164.199 | 329.7976 | 1 |')
diagnostics = {}
parsed = _bind_literal_measures({'blocks': [{'text': table, 'factIds': ['F1']}]},
tokens, diagnostics, facts=facts)
self.assertEqual(len(re.findall(r'\{\{N[1-9][0-9]*\}\}', parsed['blocks'][0]['text'])), 3)
self.assertRegex(parsed['blocks'][0]['text'], r'\| 规格A \| \{\{N[1-9][0-9]*\}\}')
self.assertIn('| 1 |', parsed['blocks'][0]['text'])
self.assertEqual(len(diagnostics['literalBindings']), 3)
no_unit_header = table.replace('(kg)', '')
inherited = _bind_literal_measures({'blocks': [{'text': no_unit_header, 'factIds': ['F1']}]},
tokens, {}, facts=facts)
self.assertEqual(len(re.findall(r'\{\{N[1-9][0-9]*\}\} kg', inherited['blocks'][0]['text'])), 3)
conflicting_unit = table.replace('(kg)', '(g)')
conflict_unit = _bind_literal_measures({'blocks': [{'text': conflicting_unit, 'factIds': ['F1']}]},
tokens, {}, facts=facts)
self.assertEqual(conflict_unit['blocks'][0]['text'], conflicting_unit)
source_weight_only = copy.deepcopy(facts)
for item in source_weight_only[0]['verifiedDisplayValues']:
item['unit'] = 'source_weight'
untyped = _bind_literal_measures({'blocks': [{'text': no_unit_header, 'factIds': ['F1']}]},
_binding_inventory(source_weight_only), {}, facts=source_weight_only)
self.assertEqual(untyped['blocks'][0]['text'], no_unit_header)
august = history_fact('F2', {'year': 2026, 'month': 8}, text_value=september['text'],
values=copy.deepcopy(september['verifiedDisplayValues']))
same_name_facts = [september, august]
for fact in same_name_facts:
fact['text'] = '规格A综合报废重量 = 12.5 kg。'
fact['verifiedDisplayValues'] = [{'metricCode': 'scrapWeight', 'rawValue': '12.5',
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '12.5'}]
same_name_tokens = {
'I1': {'value': '规格A', 'factId': 'F1', 'evidenceRef': 'history:F1',
'role': 'subject_identifier', 'productIdentifier': True},
'I2': {'value': '规格A', 'factId': 'F2', 'evidenceRef': 'history:F2',
'role': 'subject_identifier', 'productIdentifier': True},
'N1': {'value': '12.5', 'rawValue': '12.5', 'unit': 'kg', 'metricField': 'scrapWeight',
'stageHint': '综合', 'factId': 'F1', 'evidenceRef': 'history:F1'},
'N2': {'value': '12.5', 'rawValue': '12.5', 'unit': 'kg', 'metricField': 'scrapWeight',
'stageHint': '综合', 'factId': 'F2', 'evidenceRef': 'history:F2'},
}
shared_table = ('### 2026年9月\n| 规格 | 综合报废重量(kg) |\n|---|---:|\n| 规格A | 12.5 |')
shared_tokens = copy.deepcopy(same_name_tokens)
selected = _bind_literal_measures({'blocks': [{'text': shared_table, 'factIds': ['F1', 'F2']}]},
shared_tokens, {}, facts=same_name_facts)
self.assertEqual(sum(selected['blocks'][0]['text'].count('{{' + key + '}}') for key in shared_tokens if key.startswith('N')), 1)
self.assertIn('{{N1}}', selected['blocks'][0]['text'])
self.assertNotIn('{{N2}}', selected['blocks'][0]['text'])
august_table = shared_table.replace('2026年9月', '2026年8月')
august_selected = _bind_literal_measures(
{'blocks': [{'text': august_table, 'factIds': ['F1', 'F2']}]},
copy.deepcopy(same_name_tokens), {}, facts=same_name_facts)
self.assertIn('{{N2}}', august_selected['blocks'][0]['text'])
self.assertNotIn('{{N1}}', august_selected['blocks'][0]['text'])
conflict_table = shared_table.replace('### 2026年9月', '### 2026年8月').replace('综合报废重量(kg)', '9月综合报废重量(kg)')
conflict = _bind_literal_measures({'blocks': [{'text': conflict_table, 'factIds': ['F1', 'F2']}]},
shared_tokens, {}, facts=same_name_facts)
self.assertIn('| 规格A | 12.5 |', conflict['blocks'][0]['text'])
wrong_row_owner = _bind_literal_measures(
{'blocks': [{'text': shared_table, 'factIds': ['F2']}]},
copy.deepcopy(same_name_tokens), {}, facts=same_name_facts)
self.assertIn('| 规格A | 12.5 |', wrong_row_owner['blocks'][0]['text'])
base_tokens = {
'I1': {'value': '规格A', 'factId': 'F1', 'evidenceRef': 'history:F1',
'role': 'subject_identifier', 'productIdentifier': True},
'N1': {'value': '493.9966', 'rawValue': '493.9966', 'unit': 'kg',
'metricField': 'scrapWeight', 'stageHint': '综合', 'factId': 'F1',
'evidenceRef': 'history:F1'},
}
cases = (
('| 规格 | 亮片报废重量(kg) |\n|---|---:|\n| 规格A | 164.199 |',
{'metricField': 'scrapWeight', 'stageHint': '黑片', 'unit': 'kg', 'rawValue': '164.199'}),
('| 规格 | 综合报废重量 |\n|---|---:|\n| 规格A | 493.9966 |',
{'metricField': 'scrapWeight', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966'}),
('| 规格 | 缺陷数量(kg) |\n|---|---:|\n| 规格A | 493.9966 |',
{'metricField': 'metadata_count', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966'}),
('| 规格 | 综合报废重量(kg) |\n|---|---:|\n| 规格B | 493.9966 |',
{'metricField': 'scrapWeight', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966'}),
('| 规格 | 综合报废重量(kg) |\n|---|---:|\n| 规格A-1 | 493.9966 |',
{'metricField': 'scrapWeight', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966'}),
('| 规格 | 综合报废重量(kg) |\n|---|---:|\n| 规格A | 493.99660001 |',
{'metricField': 'scrapWeight', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966'}),
('| 规格 | 综合报废重量(kg) |\n|---|---:|\n| 规格A | 493.9966 |',
{'metricField': 'scrapWeight', 'stageHint': '综合', 'unit': 'kg', 'rawValue': '493.9966', 'nativeAtomicOnly': True}),
)
for text_value, typed in cases:
with self.subTest(header=text_value.splitlines()[0], row=text_value.splitlines()[-1]):
test_tokens = copy.deepcopy(base_tokens)
test_tokens['N1'].update(typed)
if '规格A-1' in text_value:
test_tokens['I1']['value'] = '规格A'
rejected = _bind_literal_measures({'blocks': [{'text': text_value, 'factIds': ['F1']}]},
test_tokens, {}, facts=facts)
self.assertIn(text_value.splitlines()[-1], rejected['blocks'][0]['text'])
def test_history_explicit_numeric_table_token_inherits_only_its_verified_unit(self):
from nao_native_answer import _binding_inventory, _validate_blocks, NativeCompositionError
fact = {
'id': 'F1', 'kind': 'product', 'label': '规格A',
'text': '2026年9月:规格A综合报废重量 = 493.99660000 kg。',
'evidenceRef': 'history:F1', 'scope': {'period': {'year': 2026, 'month': 9},
'filters': {}, 'dimension': 'product', 'stage': 'overall'},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': '规格A'}],
'verifiedDisplayValues': [{'metricCode': 'scrapWeight', 'rawValue': '493.99660000',
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '493.9966'}],
}
tokens = _binding_inventory([fact])
number_key = next(key for key, token in tokens.items() if key.startswith('N')
and token.get('metricField') == 'scrapWeight' and token.get('unit') == 'kg'
and token.get('stageHint') == '综合')
identifier_key = next(key for key, token in tokens.items() if key.startswith('I')
and token.get('factId') == 'F1' and token.get('value') == '规格A')
bare_header = '| 规格 | 综合报废重量 |\n|---|---:|\n| {{' + identifier_key + '}} | {{' + number_key + '}} |'
validated, _ = _validate_blocks({'blocks': [{'text': bare_header, 'factIds': ['F1'], 'role': 'core'}]},
[fact], tokens)
self.assertIn('{{' + number_key + '}}', bare_header)
self.assertIn('493.99660000 kg', validated[0]['text'])
explicit_header = bare_header.replace('综合报废重量 |', '综合报废重量(kg) |')
validated_explicit, _ = _validate_blocks(
{'blocks': [{'text': explicit_header, 'factIds': ['F1'], 'role': 'core'}]}, [fact], tokens)
self.assertIn('493.99660000 kg', validated_explicit[0]['text'])
conflicting_header = bare_header.replace('综合报废重量 |', '综合报废重量(g) |')
with self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': conflicting_header, 'factIds': ['F1'], 'role': 'core'}]},
[fact], tokens)
source_weight_only = copy.deepcopy(fact)
source_weight_only['verifiedDisplayValues'][0]['unit'] = 'source_weight'
source_weight_tokens = _binding_inventory([source_weight_only])
source_weight_number = next(key for key, token in source_weight_tokens.items() if key.startswith('N')
and token.get('metricField') == 'scrapWeight')
source_weight_identifier = next(key for key, token in source_weight_tokens.items() if key.startswith('I')
and token.get('value') == '规格A')
unsupported = bare_header.replace(number_key, source_weight_number).replace(identifier_key, source_weight_identifier)
with self.assertRaises(NativeCompositionError):
_validate_blocks({'blocks': [{'text': unsupported, 'factIds': ['F1'], 'role': 'core'}]},
[source_weight_only], source_weight_tokens)
def test_history_prose_mass_uses_same_typed_resolver_and_requires_named_period_scope(self):
from nao_native_answer import (NativeCompositionError, _bind_exact_identifiers, _bind_exact_periods,
_bind_exact_year_literals, _bind_literal_measures, _binding_inventory, _validate_blocks)
def fact(fact_id, label, year, month, raw):
text = f'{year}年{month}月:{label}综合报废重量 = {raw} kg。'
return {'id': fact_id, 'kind': 'product', 'label': label, 'text': text,
'evidenceRef': 'history:' + fact_id,
'scope': {'period': {'year': year, 'month': month}, 'filters': {},
'dimension': 'product', 'stage': 'overall'},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': label}],
'verifiedDisplayValues': [{'metricCode': 'scrapWeight', 'rawValue': raw,
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': raw}]}
facts = [fact('F1', '规格A', 2026, 9, '12.5'),
fact('F2', '规格B', 2026, 8, '12.5'),
fact('F3', '规格C', 2026, 8, '8.0')]
tokens = _binding_inventory(facts)
def bind(text, fact_ids):
parsed = {'blocks': [{'text': text, 'factIds': fact_ids, 'role': 'core'}]}
diagnostics = {}
for binder in (_bind_exact_identifiers, _bind_exact_periods, _bind_exact_year_literals):
parsed = binder(parsed, tokens, diagnostics)
parsed = _bind_literal_measures(parsed, tokens, diagnostics, facts=facts)
return parsed, diagnostics
correct, correct_diagnostics = bind('2026年9月规格A综合报废重量为12.5000。', ['F1', 'F2'])
self.assertRegex(correct['blocks'][0]['text'], r'\{\{N[1-9][0-9]*\}\} kg')
validated, _ = _validate_blocks(correct, facts, tokens)
self.assertIn('12.5 kg', validated[0]['text'])
self.assertEqual(len(correct_diagnostics['literalBindings']), 1)
# When the clause omits a calendar label, its one named signed fact
# supplies the period. The same object repeated across periods does
# not become unambiguous just because the inventory listed one token
# first.
no_period, no_period_diagnostics = bind('规格A综合报废重量为12.5。', ['F1', 'F2'])
self.assertRegex(no_period['blocks'][0]['text'], r'\{\{N[1-9][0-9]*\}\} kg')
self.assertEqual(len(no_period_diagnostics['literalBindings']), 1)
repeated = copy.deepcopy(facts[0])
repeated.update(id='F4', text='2026年8月:规格A综合报废重量 = 12.5 kg。',
scope={'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimension': 'product', 'stage': 'overall'})
duplicate_facts = [*facts, repeated]
duplicate_tokens = _binding_inventory(duplicate_facts)
duplicate = {'blocks': [{'text': '规格A综合报废重量为12.5。',
'factIds': ['F1', 'F4'], 'role': 'core'}]}
duplicate = _bind_exact_identifiers(duplicate, duplicate_tokens, {})
duplicate = _bind_literal_measures(duplicate, duplicate_tokens, {}, facts=duplicate_facts)
self.assertNotIn('{{N', duplicate['blocks'][0]['text'])
# A value from another cited product cannot escape an explicit object
# mismatch through a unique numeric match.
wrong_object = {'blocks': [{'text': '2026年8月规格B综合报废重量为12.5 kg。',
'factIds': ['F1', 'F2'], 'role': 'core'}]}
for binder in (_bind_exact_identifiers, _bind_exact_periods, _bind_exact_year_literals):
wrong_object = binder(wrong_object, tokens, {})
wrong_object = _bind_literal_measures(wrong_object, tokens, {}, facts=facts)
with self.assertRaises(NativeCompositionError):
_validate_blocks(wrong_object, facts, tokens)
wrong_field = bind('2026年9月规格A综合投入重量为12.5。', ['F1', 'F2'])[0]
self.assertNotIn('{{N', wrong_field['blocks'][0]['text'])
wrong_period, wrong_diagnostics = bind('2026年9月规格B综合报废重量为12.5。', ['F1', 'F2'])
self.assertIn('12.5', wrong_period['blocks'][0]['text'])
self.assertNotIn('{{N', wrong_period['blocks'][0]['text'])
self.assertEqual(wrong_diagnostics['historyMeasureBindingGaps'][0]['reason'],
'object_and_period_scope_conflict')
ambiguous, ambiguous_diagnostics = bind(
'2026年8月三款Top规格的报废重量接近(12.5、12.5、8.0)。', ['F2', 'F3'])
self.assertNotIn('{{N', ambiguous['blocks'][0]['text'])
self.assertEqual(len(ambiguous_diagnostics['historyMeasureBindingGaps']), 3)
self.assertTrue(all(item['reason'] == 'no_complete_object_identifier'
for item in ambiguous_diagnostics['historyMeasureBindingGaps']))
def ranked_fact(fact_id, label, rank, raw):
return {'id': fact_id, 'kind': 'product', 'label': label,
'text': f'2026年8月:{label}综合报废重量 = {raw} kg。',
'evidenceRef': 'history:' + fact_id,
'scope': {'period': {'year': 2026, 'month': 8}, 'filters': {},
'dimension': 'product', 'stage': 'overall'},
'rank': rank, 'rankGroupKey': 'history-rank:authorized-group',
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': label}],
'verifiedDisplayValues': [{'metricCode': 'scrapWeight', 'rawValue': raw,
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': raw}]}
ranked = [ranked_fact('R1', '规格一', 1, '1155.48340000'),
ranked_fact('R2', '规格二', 2, '1106.25'),
ranked_fact('R3', '规格三', 3, '1060.75')]
ranked_tokens = _binding_inventory(ranked)
collective_text = '2026年8月三款Top规格的报废重量接近(1155.48340000、1106.25、1060.75)。'
collective = {'blocks': [{'text': collective_text,
'factIds': ['R1', 'R2', 'R3'], 'role': 'core'}]}
for binder in (_bind_exact_periods, _bind_exact_identifiers, _bind_exact_year_literals):
collective = binder(collective, ranked_tokens, {})
collective = _bind_literal_measures(collective, ranked_tokens, {}, facts=ranked)
self.assertEqual(collective['blocks'][0]['text'].count(' kg'), 3)
checked, _ = _validate_blocks(collective, ranked, ranked_tokens)
for display in ('1155.48340000 kg', '1106.25 kg', '1060.75 kg'):
self.assertIn(display, checked[0]['text'])
duplicated_ranked = [*ranked, ranked_fact('R4', '规格四', 4, '1155.48340000')]
duplicated_tokens = _binding_inventory(duplicated_ranked)
duplicated_claim = {'blocks': [{'text': collective_text,
'factIds': ['R1', 'R2', 'R3', 'R4'], 'role': 'core'}]}
for binder in (_bind_exact_periods, _bind_exact_identifiers, _bind_exact_year_literals):
duplicated_claim = binder(duplicated_claim, duplicated_tokens, {})
duplicated_claim = _bind_literal_measures(duplicated_claim, duplicated_tokens, {},
facts=duplicated_ranked)
duplicate_text = duplicated_claim['blocks'][0]['text']
self.assertRegex(duplicate_text, r'1155\.48340000、\{\{N[1-9][0-9]*\}\} kg、\{\{N[1-9][0-9]*\}\} kg')
wrong_collective = {'blocks': [{'text': collective_text.replace('2026年8月', '2026年9月'),
'factIds': ['R1', 'R2', 'R3'], 'role': 'core'}]}
for binder in (_bind_exact_periods, _bind_exact_identifiers, _bind_exact_year_literals):
wrong_collective = binder(wrong_collective, ranked_tokens, {})
wrong_collective = _bind_literal_measures(wrong_collective, ranked_tokens, {}, facts=ranked)
self.assertIn('1155.48340000、1106.25、1060.75', wrong_collective['blocks'][0]['text'])
def test_history_parent_predicate_mass_inheritance_uses_family_stage_and_exact_fact(self):
from nao_native_answer import (NativeCompositionError, _bind_exact_identifiers,
_bind_exact_periods, _bind_exact_year_literals, _binding_inventory, _validate_blocks)
fact = {'id': 'F1', 'kind': 'product', 'label': '规格A',
'text': ('2026年9月规格A综合报废重量=1.00 kg;黑片报废重量=0.25 kg;亮片报废重量=0.75 kg;'
'黑片投入重量=2.00 kg;亮片投入重量=3.00 kg。'),
'evidenceRef': 'history:mass-parent',
'scope': {'period': {'year': 2026, 'month': 9}, 'filters': {},
'dimension': 'product', 'stage': 'overall'},
'validity': {'verified': True, 'basis': 'protocol_history_snapshot'},
'verifiedIdentifiers': [{'value': '规格A'}],
'verifiedDisplayValues': [
{'metricCode': 'scrapWeight', 'rawValue': '1.00', 'rawUnit': 'source_weight', 'unit': 'kg'},
{'metricCode': 'blackScrapWeight', 'rawValue': '0.25', 'rawUnit': 'source_weight', 'unit': 'kg'},
{'metricCode': 'brightScrapWeight', 'rawValue': '0.75', 'rawUnit': 'source_weight', 'unit': 'kg'},
{'metricCode': 'blackTotalWeight', 'rawValue': '2.00', 'rawUnit': 'source_weight', 'unit': 'kg'},
{'metricCode': 'brightTotalWeight', 'rawValue': '3.00', 'rawUnit': 'source_weight', 'unit': 'kg'},
]}
tokens = _binding_inventory([fact])
def token(metric, stage):
return next(key for key, value in tokens.items() if key.startswith('N')
and value.get('metricField') == metric and value.get('stageHint') == stage)
black_scrap, bright_scrap = token('scrapWeight', '黑片'), token('scrapWeight', '亮片')
black_input, bright_input = token('totalWeight', '黑片'), token('totalWeight', '亮片')
text = ('2026年9月规格A的报废以黑片为主(黑片{{'+black_scrap+'}}、亮片{{'+bright_scrap+'}}),'
'投入规模为黑片{{'+black_input+'}}、亮片{{'+bright_input+'}}。')
def validate(value):
draft = {'blocks': [{'text': value, 'factIds': ['F1'], 'role': 'core'}]}
for binder in (_bind_exact_identifiers, _bind_exact_periods, _bind_exact_year_literals):
draft = binder(draft, tokens, {})
return _validate_blocks(draft, [fact], tokens)
parsed, _ = validate(text)
for display in ('0.25 kg', '0.75 kg', '2.00 kg', '3.00 kg'):
self.assertIn(display, parsed[0]['text'])
wrong_family = text.replace('投入规模为黑片{{'+black_input+'}}',
'报废重量为黑片{{'+black_input+'}}')
with self.assertRaises(NativeCompositionError):
validate(wrong_family)
wrong_stage = text.replace('{{'+bright_input+'}}', '{{'+black_input+'}}')
with self.assertRaises(NativeCompositionError):
validate(wrong_stage)
def test_partial_history_fallback_keeps_exact_rank_values_and_saved_denominator(self):
from nao_history_report import _history_fallback_fact_ids, _history_partial_fallback
candidate = actual_precision_ranking_snapshot()
candidate['sections'][1] = copy.deepcopy(candidate['sections'][0])
candidate['sections'][1]['sourceTurnId'] = SOURCE_B
candidate['facts'] = facts_for_ranking_sections(candidate)
for section in candidate['sections']:
section['note'] = '正式分母为 6969.5000;本表仅表示已保存Top排名,重量沿用来源单位。'
answer, summary, sections = _history_partial_fallback(
candidate, missing=['正文解释未通过事实绑定校验,报告保持部分完成。'],
headline_ids=_history_fallback_fact_ids(candidate))
self.assertEqual(len(sections), 2)
self.assertIn('493.9966 kg', answer)
self.assertNotIn('494.00 kg', answer)
self.assertIn('91.23%', answer)
self.assertIn('7.08%', answer)
self.assertIn('正式分母为 6969.5000', answer)
self.assertNotIn('0.9122755444707148916393578115770785', answer)
self.assertNotIn('history-fact:', answer)
self.assertNotIn('附录', answer)
self.assertEqual(answer.count('| 九月规格'), 5)
self.assertEqual(answer.count('| 八月规格'), 3)
self.assertEqual(summary['notices'], ['正文解释未通过事实绑定校验,报告保持部分完成。'])
self.assertEqual(summary['evidenceRefs'][:2],_history_fallback_fact_ids(candidate))
self.assertEqual(len(summary['visualIds']), 3)
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({'reportTitle':'多范围历史质量指标分析',
'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({'reportTitle':'2026年9月历史指标',
'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},
'dimension': 'product', 'stage': 'black',
'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'] = [{**fact, 'factKind': 'product',
'scope': {'period': {'year': 2026, 'month': 9}, 'filters': {},
'dimension': 'product', 'stage': 'black'}} for fact in candidate['facts']]
candidate['facts'].append({'id': 'history:late-product', 'sourceTurnId': source, 'sectionIndex': 2,
'text': '2026年9月'+product+'黑片报废重量100.00 kg。', 'factKind': 'product',
'label': product, 'scope': {'period': {'year': 2026, 'month': 9},
'filters': {'product': product}, 'dimension': 'product', 'stage': 'black'},
'verifiedDisplayValues': [{'metricCode': 'scrapWeight', 'rawValue': '100.00',
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '100.00'}]})
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({'reportTitle': '重点规格报废重量核查',
'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={},
dimension='product', stage='black',
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].update(text='2026年9月;全局产品排名:'+product+',黑片报废重量100.00 kg。',
factKind='product', label=product, sectionIndex=0, rowIndex=0, metricCode='scrapWeight',
rawValue='100.00', rawUnit='source_weight', unit='kg', displayValue='100.00',
scope={'period': {'year': 2026, 'month': 9}, 'filters': {},
'dimension': 'product', 'stage': 'black'},
verifiedDisplayValues=[{'metricCode': 'scrapWeight', 'rawValue': '100.00',
'rawUnit': 'source_weight', 'unit': 'kg', 'displayValue': '100.00'}])
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({'reportTitle': '全局产品报废重量核查',
'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({'reportTitle': '2025至2026年历史质量表现',
'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)
self.assertIn('bindingInput', private)
self.assertNotIn('bindingInput', result)
self.assertNotIn('_privateBindingInput', result)
def test_history_private_capture_preserves_issued_token_sequence_only_in_private_file(self):
issued = {
'facts': [{'id': 'F1', 'text': '已签发事实'}],
'bindingInventory': {
'N17': {'value': '17.25', 'unit': 'kg', 'metricField': 'scrapWeight', 'factId': 'F1'},
'N32': {'value': '32.5%', 'unit': '%', 'metricField': 'rate', 'factId': 'F1'},
},
'requiredFactIds': ['F1'], 'priorityFactIds': ['F1'], 'modelScopes': [{'scopeId': 'scope-1'}],
}
native_final = {
'answer': '已核验历史报废结果。', 'bodyEvidenceSufficient': True,
'evidenceSufficient': True, 'reportTitle': '历史报废结果分析',
'titleValid': True, 'titleGeneratedByModel': True,
'titleSource': 'model_verified_content', 'historyHeadlineFactIds': ['history-fact:F1'],
'readingSummary': {'version': 1, 'lead': '已核验历史报废结果。', 'evidence': [],
'notices': [], 'evidenceRefs': ['history-fact:F1'], 'visualIds': []},
'rawModelResponses': ['private raw'], 'compositionDiagnostics': {},
'_privateBindingInput': issued,
}
with tempfile.TemporaryDirectory() as folder, patch(
'nao_native_answer.synthesize_history_snapshot', return_value=native_final):
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(private['bindingInput']['bindingInventory']['N17']['value'], '17.25')
self.assertEqual(private['bindingInput']['bindingInventory']['N32']['metricField'], 'rate')
self.assertEqual(private['bindingInput'], issued)
self.assertNotIn('bindingInput', result)
self.assertNotIn('_privateBindingInput', result)
def test_autonomous_failed_composition_hides_raw_fallback_but_keeps_history_charts_and_partial(self):
candidate = ranking_history_snapshot()
# The certified short title needs the source task's attested alias,
# independently of the intentionally rejected body below.
candidate['historySources'][0]['taskContext'] = {'question':'9月3C不良Top5',
'period':copy.deepcopy(candidate['sections'][0]['period']),
'filters':copy.deepcopy(candidate['sections'][0]['filters'])}
charts = candidate['visuals']
lineages = candidate['historyVisualSources']
raw_fallback = ('本报告正文含history-fact:F1及filters={"department":"部门甲"};'
'完整历史表格和图表保留在附录。长小数0.123456789012345678901234。')
failed_composition = {
'answer': raw_fallback, 'evidenceSufficient': False,
'reportTitle': '2026年9月3C与8月全公司报废排名分析',
'titleValid': True, 'titleGeneratedByModel': True,
'titleSource': 'model_verified_content',
'historyHeadlineFactIds': ['history-fact:SEP-A', 'history-fact:AUG-B'],
'readingSummary': {'version':1,'lead':raw_fallback,'evidence':[raw_fallback],
'notices':['完整历史表格和图表保留在附录。'],
'evidenceRefs':['history-fact:SEP-A','history-fact:AUG-B'],
'visualIds':[chart['id'] for chart in charts]},
'missingEvidence':['解释未通过事实绑定校验,本轮解释层尚未完成。'],
'compositionDiagnostics': {'formatRepairAttempted':True,'issues':['unbound literal']},
'rawModelResponses':['raw private response'],
}
emitted = []
synthesis_call = {}
def failed_synthesis(**kwargs):
synthesis_call.update(kwargs)
return copy.deepcopy(failed_composition)
with tempfile.TemporaryDirectory() as folder, patch(
'nao_native_answer.synthesize_history_snapshot',side_effect=failed_synthesis), patch(
'nao_history_report.select_history_headline',side_effect=AssertionError('must not request another title')):
result = execute_history_report(
request={'releaseId':RELEASE,'turnId':TURN,'question':'整合会话历史报告',
'_autonomousAnswer':True},
task=report_task(),authority=authority(),actor_root=Path(folder),
delegated=FakeDelegated(candidate),
connection=('https://offline.invalid/v1','fixture','fixture'),
cancel_event=threading.Event(),emit=emitted.append,timings={})
visible_text = '\n'.join([result.get('answer',''), result.get('readingSummary',{}).get('lead',''),
*result.get('readingSummary',{}).get('evidence',[]),
*result.get('readingSummary',{}).get('notices',[])])
self.assertEqual(result['status'],'PARTIAL')
self.assertEqual(result['stopReason'],'history_report_partial')
self.assertEqual(result['reportTitle'],failed_composition['reportTitle'])
self.assertTrue(result['titleGeneratedByModel'])
self.assertEqual(result['historyHeadlineFactIds'],failed_composition['historyHeadlineFactIds'])
self.assertEqual(result['evidenceCompletion']['modelDeclaredEvidenceSufficient'],False)
self.assertNotIn('history-fact:',visible_text)
self.assertNotIn('filters=',visible_text)
self.assertNotIn('附录',visible_text)
self.assertNotIn('0.123456789012345678901234',visible_text)
self.assertIn('正文解释未通过事实绑定校验',visible_text)
self.assertIn('2026年9月 / 产业部 消费电子产业部 / 业务分组 消费电子-3C',result['answer'])
self.assertIn('2026年8月 · Top 3',result['answer'])
self.assertEqual(result['answer'].count('###'),2)
self.assertEqual(result['answer'].count('| 九月规格'),5)
self.assertEqual(result['answer'].count('| 八月规格'),3)
self.assertIn('91.23%',result['answer'])
self.assertIn('10.25 kg',result['answer'])
self.assertIn('20.00%',result['answer'])
self.assertIn('38.00%',result['answer'])
self.assertIn('完全相同范围与内容的排名表只呈现一次',result['answer'])
self.assertEqual(result['historyCoverage'],candidate['historyCoverage'])
self.assertEqual(result['visualState'],'ready')
self.assertEqual(result['visuals'],charts)
self.assertEqual(result['historyVisualSources'],lineages)
self.assertEqual(result['readingSummary']['visualIds'],[chart['id'] for chart in charts])
self.assertEqual(result['queryEvidenceRefs'],list(dict.fromkeys(
ref for chart in charts for ref in chart['evidenceRefs'])))
self.assertEqual(len(synthesis_call['snapshot']['facts']),2)
self.assertEqual(len(synthesis_call['snapshot']['sections']),3)
self.assertEqual(len(candidate['facts']),3)
self.assertEqual(len(candidate['sections']),3)
self.assertEqual(len(result['visuals']),3)
def test_missing_title_does_not_discard_verified_history_body_or_charts(self):
candidate = ranking_history_snapshot()
candidate['facts'] = facts_for_ranking_sections(candidate)
selected = candidate['facts'][0]['id']
native = {
'answer': '已按九月3C与八月全公司各自的已确认范围整理重点结果。',
'evidenceSufficient': True, 'bodyEvidenceSufficient': True,
'reportTitle': '历史质量分析汇总报告', 'titleValid': False,
'titleGeneratedByModel': False, 'titleSource': 'safe_partial_fallback',
'historyHeadlineFactIds': [selected],
'readingSummary': {'version':1,'lead':'九月与八月重点结果已按原范围保留。',
'evidence':['九月Top排名与八月全公司排名分别呈现。'],'notices':[],
'evidenceRefs':[selected],'visualIds':[chart['id'] for chart in candidate['visuals']]},
'missingEvidence': [], 'titleMissingEvidence':['报告标题尚未生成或未通过内容核验。'],
'compositionDiagnostics': {'evidenceReviewAttempted':True,'issues':[]},
}
with tempfile.TemporaryDirectory() as folder, patch(
'nao_native_answer.synthesize_history_snapshot',return_value=native), patch(
'nao_history_report.select_history_headline',side_effect=AssertionError('must not request another title')):
result = execute_history_report(
request={'releaseId':RELEASE,'turnId':TURN,
'question':'基于本会话已完成的结果生成综合报告,按各轮实际期间和范围组织,保留重点表格和图表,不查询新数据。',
'_autonomousAnswer':True},
task=report_task(),authority=authority(),actor_root=Path(folder),
delegated=FakeDelegated(candidate),
connection=('https://offline.invalid/v1','fixture','fixture'),
cancel_event=threading.Event(),emit=lambda _:None,timings={})
self.assertEqual(result['status'],'PARTIAL')
self.assertEqual(result['answer'],native['answer'])
self.assertEqual(result['readingSummary']['lead'],native['readingSummary']['lead'])
self.assertEqual(result['visuals'],candidate['visuals'])
self.assertEqual(result['readingSummary']['visualIds'],[chart['id'] for chart in candidate['visuals']])
self.assertFalse(result['titleGeneratedByModel'])
self.assertTrue(result['titleRequired'])
self.assertFalse(result['titleValid'])
self.assertEqual(result['titleSource'],'safe_partial_fallback')
self.assertEqual(result['evidenceCompletion']['modelDeclaredEvidenceSufficient'],True)
self.assertEqual(result['evidenceCompletion']['bodyEvidenceSufficient'],True)
self.assertEqual(result['evidenceCompletion']['titleValid'],False)
self.assertTrue(result['evidenceCompletion']['titleMissingEvidence'])
self.assertEqual(len(result['evidenceCompletion']['missingEvidence']),1)
self.assertIn('标题',result['evidenceCompletion']['missingEvidence'][0])
self.assertNotIn('事实绑定',result['evidenceCompletion']['missingEvidence'][0])
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.assertNotIn('附录',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):
candidate = snapshot()
candidate.update(visualSchemaVersion=1, visualState='ready', visuals=[saved_visual()],
historyVisualSources=[saved_visual_lineage()])
result, delegated, emitted, payloads = self.run_report(
candidate, {'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['visualSchemaVersion'], 1)
self.assertEqual(result['visualState'], 'ready')
self.assertEqual(result['visuals'], [saved_visual()])
self.assertEqual(result['historyVisualSources'], [saved_visual_lineage()])
forged = {'reportTitle': '历史质量表现', 'headlineFactIds': ['history-fact:F1'],
'visuals': [{'id': 'model-forged-chart'}]}
self.assertIsNone(_validate_model_selection(forged, candidate, '汇总历史', candidate['facts']))
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'], _history_fallback_title(snapshot()))
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()