Spaces:
Runtime error
Runtime error
| from __future__ import annotations | |
| from backend.models.schema import RatingSystem, RatingValue, TemplateSchema | |
| from backend.core.reference_mapper import compose_mapping_system_prompt | |
| from backend.prompts.mapping_prompt import ( | |
| MAPPING_SYSTEM_BASE, | |
| MAPPING_SYSTEM_BASE_MAXIMUM, | |
| MAPPING_SYSTEM_BASE_MEDIUM, | |
| MAPPING_SYSTEM_BASE_MINIMUM, | |
| build_mapping_messages, | |
| build_mapping_system_prompt, | |
| ) | |
| def _schema(*, rating: bool) -> TemplateSchema: | |
| rs = RatingSystem( | |
| detected=rating, | |
| name="Condition Rating", | |
| values=[RatingValue(value="1"), RatingValue(value="2"), RatingValue(value="3")], | |
| ) | |
| return TemplateSchema(rating_system=rs) | |
| def test_maximum_system_base_expert_synthesis(): | |
| assert "expert UK Chartered Building Surveyor" in MAPPING_SYSTEM_BASE_MAXIMUM | |
| assert "UK_PROPERTY_SURVEYING_DOMAIN_KNOWLEDGE" in MAPPING_SYSTEM_BASE_MAXIMUM | |
| assert "INTEGRATED NARRATIVE DRAFTING" in MAPPING_SYSTEM_BASE_MAXIMUM | |
| assert "IN-PLACE MUTATION ONLY" not in MAPPING_SYSTEM_BASE_MAXIMUM | |
| # The hallucination license must be gone: no domain-concept invention, no | |
| # "expand a brief mention into a complete assessment" latitude. | |
| assert "domain concepts that resemble" not in MAPPING_SYSTEM_BASE_MAXIMUM | |
| assert "complete structural assessment" not in MAPPING_SYSTEM_BASE_MAXIMUM | |
| def test_every_tier_carries_fact_grounding(): | |
| for level in ("minimum", "medium", "maximum"): | |
| prompt = compose_mapping_system_prompt(level) | |
| assert "FACT GROUNDING" in prompt | |
| assert "from a DIFFERENT property" in prompt | |
| assert "MUST originate from the CURRENT INSPECTION NOTES" in prompt | |
| def test_medium_system_base_grounded_proofreading(): | |
| assert "professional technical copyeditor" in MAPPING_SYSTEM_BASE_MEDIUM | |
| assert "ABSOLUTE HISTORICAL GROUNDING" in MAPPING_SYSTEM_BASE_MEDIUM | |
| assert "PROOFREAD & ENHANCE TECHNICAL DEPTH" in MAPPING_SYSTEM_BASE_MEDIUM | |
| assert "IN-PLACE MUTATION ONLY" not in MAPPING_SYSTEM_BASE_MEDIUM | |
| def test_minimum_system_base_strict_containment(): | |
| assert "zero-inference data mapping engine" in MAPPING_SYSTEM_BASE_MINIMUM | |
| assert "DO NOT ADD NEW SENTENCES" in MAPPING_SYSTEM_BASE_MINIMUM | |
| assert "UPLOADED USER PAST REPORTS" in MAPPING_SYSTEM_BASE_MINIMUM | |
| assert "IN-PLACE MUTATION ONLY" not in MAPPING_SYSTEM_BASE_MINIMUM | |
| def test_system_prompt_includes_rating_when_detected(): | |
| prompt = build_mapping_system_prompt(_schema(rating=True)) | |
| assert "<CONDITION_RATING_SYSTEM>" in prompt | |
| assert "Permissible target values" in prompt | |
| assert "STALE RATING SAFETY VALVE" in prompt | |
| assert "1" in prompt | |
| def test_system_prompt_bans_rating_when_not_detected(): | |
| prompt = build_mapping_system_prompt(_schema(rating=False)) | |
| assert "does NOT utilize a condition rating system" in prompt | |
| assert "COMPREHENSIVE ERADICATION" in prompt | |
| assert "<CONDITION_RATING_SYSTEM>" in prompt | |
| def test_build_messages_structure(): | |
| msgs = build_mapping_messages( | |
| _schema(rating=False), | |
| "E1", | |
| "Chimney Stacks", | |
| ["stack cracked"], | |
| ["The chimney was inspected."], | |
| None, | |
| ) | |
| assert msgs[0]["role"] == "system" | |
| assert "<ROLE>" in msgs[0]["content"] | |
| assert "IN-PLACE MUTATION ONLY" in msgs[0]["content"] | |
| assert MAPPING_SYSTEM_BASE.split()[0] in msgs[0]["content"] | |
| assert "INPUT 1: PAST-REPORT SCAFFOLD" in msgs[1]["content"] | |
| assert "INPUT 2: CURRENT INSPECTION NOTES" in msgs[1]["content"] | |
| assert "* stack cracked" in msgs[1]["content"] | |
| assert "E1" in msgs[1]["content"] | |
| assert "Chimney Stacks" in msgs[1]["content"] | |
| def test_system_prompt_forbids_unmatched_and_contamination(): | |
| prompt = build_mapping_system_prompt(_schema(rating=False)) | |
| assert "UNMATCHED_OBSERVATION" in prompt | |
| assert "[REDACTED" in prompt | |
| assert "ZERO METADATA LEAKAGE" in prompt | |
| assert "IN-PLACE MUTATION ONLY" in prompt | |
| def test_format_template_in_system_prompt_when_present(): | |
| schema = _schema(rating=True) | |
| schema.rating_system.format_template = "Condition Rating [VALUE]" | |
| schema.rating_system.inline_example = "Condition Rating 2" | |
| schema.placeholders.primary_format = "[...]" | |
| prompt = build_mapping_system_prompt(schema) | |
| assert "Condition Rating [VALUE]" in prompt | |
| assert "Condition Rating 2" in prompt | |
| assert "<PLACEHOLDER_SYNTAX>" in prompt | |
| assert "ZERO BRACKET LEAKAGE" in prompt | |