import json import pytest from dx_productivity.dsh.experience import ExperienceStore def incident(root, identity='a' * 24, *, verification=None, evidence=True, **values): directory = root / 'dsh' / 'incidents' / identity directory.mkdir(parents=True) record = {'incident_id': identity, 'task_id': 'task-1', 'issue_code': 'worker_error', 'message': 'Authorization: Bearer sensitiveExample123', 'status': 'resolved', 'analyses': [{'reply': 'sk-sensitiveCredential123 repaired'}], **values} (directory / 'incident.json').write_text(json.dumps(record), encoding='utf8') if verification is not None: (directory / 'verification.json').write_text(json.dumps(verification), encoding='utf8') if evidence: (directory / 'snapshot.json').write_text('{"task_id":"task-1"}', encoding='utf8') return directory GOOD = {'queue_complete': True, 'failed_items': 0, 'interrupted': False, 'paused': False, 'saved_items': 1, 'completed_items': 1} def test_verified_experience_requires_persisted_verification_and_evidence(tmp_path): incident(tmp_path, verification=GOOD) store = ExperienceStore(tmp_path) rows = store.refresh() assert rows[0]['verified'] is True assert rows[0]['verification'] == GOOD assert any(ref.endswith('/verification.json') for ref in rows[0]['evidence_refs']) assert 'sensitive' not in json.dumps(rows) assert ExperienceStore(tmp_path).search('worker_error') == rows assert store.summary()['verified'] == 1 @pytest.mark.parametrize('changes', [{'failed_items': 1}, {'queue_complete': False}, {'interrupted': True}, {'paused': True}, {'failed_items': False}]) def test_unsuccessful_or_malformed_verification_is_not_verified(tmp_path, changes): incident(tmp_path, verification=GOOD | changes) assert ExperienceStore(tmp_path).refresh()[0]['verified'] is False def test_model_claim_closed_ticket_inline_verification_and_missing_evidence_are_not_proof(tmp_path): incident(tmp_path, 'a' * 24, verification=GOOD, evidence=False) incident(tmp_path, 'b' * 24, verification=None, ticket={'state': 'closed'}, verification_claim=GOOD) assert not any(row['verified'] for row in ExperienceStore(tmp_path).refresh()) def test_search_is_bounded_matches_context_and_skips_bad_local_records(tmp_path): incident(tmp_path, issue_code='save_failed', message='resource lock busy', verification=GOOD) incident(tmp_path, 'b' * 24, issue_code='other', message='different', verification=None) directory = tmp_path / 'dsh' / 'incidents' / ('c' * 24) directory.mkdir(); (directory / 'incident.json').write_text('broken', encoding='utf8') store = ExperienceStore(tmp_path); store.refresh() assert store.search('save_failed', limit=1)[0]['issue_code'] == 'save_failed' assert store.search('resource lock')[0]['issue_code'] == 'save_failed' assert store.search('unrelated') == [] assert store.search(limit=0) == [] assert store.summary()['total'] == 2 def test_index_preserves_portable_evidence_refs_and_rejects_mismatched_identity(tmp_path): incident(tmp_path, verification=GOOD) wrong = incident(tmp_path, 'b' * 24, verification=GOOD) record = json.loads((wrong / 'incident.json').read_text(encoding='utf8')) record['incident_id'] = 'c' * 24 (wrong / 'incident.json').write_text(json.dumps(record), encoding='utf8') store = ExperienceStore(tmp_path) rows = store.refresh() assert len(rows) == 1 assert rows[0]['evidence_refs'][0].startswith('dsh/incidents/') assert (tmp_path / 'dsh' / 'experience' / 'index.json').is_file() def test_oversized_incident_and_verification_are_not_read_as_trusted_evidence(tmp_path): directory = incident(tmp_path, verification=GOOD) (directory / 'verification.json').write_text(json.dumps(GOOD | {'extra': 'x' * 100000}), encoding='utf8') assert ExperienceStore(tmp_path).refresh()[0]['verified'] is False (directory / 'incident.json').write_text('x' * 1000000, encoding='utf8') assert ExperienceStore(tmp_path).refresh() == [] def test_reusable_diagnosis_attempt_and_outcome_are_bounded_scrubbed_data(tmp_path): directory = incident(tmp_path, verification=GOOD, attempts=1, analyses=[{'phase': 'analyzing', 'reply': '重读保存证据,确认锁已释放。' + 'x' * 2000, 'tool_calls': [{'execute': 'must not preserve'}]}, {'phase': 'reflecting', 'reply': '下一步:人工核对封装。 sk-sensitiveCredential123'}, {'phase': 'extra', 'reply': 'omitted'}], retry_error={'type': 'TaskCenterError', 'code': 'native_busy', 'detail': 'Authorization: Bearer sensitiveExample123'}, retry_skipped='待人工核对') (directory / 'actions.json').write_text(json.dumps({'attempts': 1, 'verification': GOOD, 'retry_error': {'type': 'TaskCenterError', 'code': 'native_busy', 'detail': '锁仍被占用'}, 'retry_skipped': '保留现场', 'execute': 'must not preserve'}), encoding='utf8') row = ExperienceStore(tmp_path).refresh()[0] assert row['attempts'] == 1 assert len(row['analyses']) == 2 assert row['analyses'][0]['phase'] == 'analyzing' and len(row['analyses'][0]['reply']) <= 1500 assert '下一步' in row['analyses'][1]['reply'] assert row['retry_error']['code'] == 'native_busy' and row['retry_skipped'] == '待人工核对' assert row['actions']['attempts'] == 1 and row['actions']['retry_skipped'] == '保留现场' assert row['actions']['retry_error']['detail'] == '锁仍被占用' assert row['outcome']['verified'] is True and row['outcome']['saved_items'] == 1 assert 'sensitive' not in json.dumps(row) assert 'must not preserve' not in json.dumps(row) def test_action_claim_does_not_verify_or_grant_execution_and_oversize_actions_are_ignored(tmp_path): directory = incident(tmp_path, verification=None, attempts=True, analyses=[{'phase': 'reflecting', 'reply': '已修复,可执行'}]) (directory / 'actions.json').write_text(json.dumps({'attempts': 99, 'verification': GOOD, 'execute': True, 'authorized': True}), encoding='utf8') row = ExperienceStore(tmp_path).refresh()[0] assert row['verified'] is row['outcome']['verified'] is False assert row['attempts'] == 0 and row['actions']['attempts'] <= 1 assert 'execute' not in row['actions'] and 'authorized' not in row['actions'] (directory / 'actions.json').write_text(json.dumps({'retry_skipped': 'x' * 40000}), encoding='utf8') assert ExperienceStore(tmp_path).refresh()[0]['actions'] == {} def test_search_prioritizes_verified_then_exact_code_then_newest_and_searches_diagnosis(tmp_path): incident(tmp_path, 'a' * 24, verification=None, issue_code='save_failed', created_utc='2026-10-30T12:00:00Z', analyses=[{'phase': 'analyzing', 'reply': '对齐元件封装'}]) incident(tmp_path, 'b' * 24, verification=GOOD, issue_code='other', message='save_failed', created_utc='2026-10-30T13:00:00Z') incident(tmp_path, 'c' * 24, verification=GOOD, issue_code='save_failed', created_utc='2026-10-29T12:00:00Z') incident(tmp_path, 'd' * 24, verification=GOOD, issue_code='save_failed', created_utc='2026-10-30T10:00:00Z') store = ExperienceStore(tmp_path) assert [row['incident_id'] for row in store.search('save_failed')] == [ 'd' * 24, 'c' * 24, 'b' * 24, 'a' * 24] assert store.search('对齐元件封装')[0]['incident_id'] == 'a' * 24