Spaces:
Sleeping
Sleeping
File size: 78,839 Bytes
b76f199 da8a68d b76f199 62f2173 b76f199 da8a68d b76f199 da8a68d b76f199 732b14f b76f199 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d b76f199 a1e2ff8 b76f199 a1e2ff8 b76f199 a1e2ff8 b76f199 62f2173 da8a68d b76f199 da8a68d b76f199 da8a68d b76f199 732b14f b76f199 be9fd4a b76f199 732b14f b76f199 da8a68d b76f199 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d b76f199 62f2173 da8a68d b76f199 732b14f b76f199 732b14f b76f199 da8a68d 732b14f da8a68d b76f199 da8a68d b76f199 732b14f b76f199 732b14f b76f199 da8a68d b76f199 732b14f 62f2173 b76f199 62f2173 732b14f b76f199 62f2173 b76f199 62f2173 b76f199 62f2173 b76f199 732b14f b76f199 62f2173 b76f199 a1e2ff8 b76f199 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d 62f2173 da8a68d b76f199 62f2173 b76f199 732b14f b76f199 732b14f b76f199 732b14f b76f199 da8a68d 62f2173 da8a68d b76f199 da8a68d 62f2173 da8a68d 62f2173 b76f199 da8a68d 62f2173 da8a68d 62f2173 b76f199 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 | """OpenAI tool-calling loop for the RICS inspector: the model chooses tools and order.
Unlike the fixed gatherβdraft pipeline, this module runs a multi-turn
``chat.completions`` session with ``tools=β¦`` until the model completes the
**server-enforced** workflow (extraction audit β section plan β optional Section C
rating summary when the active product pack includes condition ratings β
``submit_inspection_section``), or a round limit is reached.
"""
from __future__ import annotations
import asyncio
import json
import logging
import re
from typing import Any
from openai import OpenAI
from app.agentic import tools as agent_tools
from app.config import settings
from app.generator.postprocess import (
_L1_PLACEHOLDER,
async_enforce_verify,
enforce_verify,
strip_l1_advice,
strip_l1_advice_payload,
)
from app.models.schemas import SearchResult, WritingStyleProfile
from app.services.provenance_enrichment import fetch_doc_filenames
from app.services.generation import _ai_level_to_params
from app.templates.registry import SurveyTemplatePack, get_survey_pack, get_template
from .models import EvidenceItem, RiskItem, StructuredReport
logger = logging.getLogger(__name__)
_inspector_async_client: Any = None
def _inspector_async_client() -> Any:
"""Reuse one AsyncOpenAI client across inspector tool rounds."""
global _inspector_async_client
if _inspector_async_client is None:
from openai import AsyncOpenAI
_inspector_async_client = AsyncOpenAI(api_key=settings.openai_api_key)
return _inspector_async_client
def _strip_missing_fact_phrase(text: str) -> str:
"""Final defensive cleanup for legacy missing-data filler phrases."""
if not isinstance(text, str) or not text:
return text if isinstance(text, str) else ""
cleaned = re.sub(
r"\bInformation not provided in source document\b[.,;:!?]*",
"",
text,
flags=re.IGNORECASE,
)
cleaned = re.sub(
r"\bWe were unable to verify this during inspection\b[.,;:!?]*",
"",
cleaned,
flags=re.IGNORECASE,
)
cleaned = re.sub(r"\s{2,}", " ", cleaned)
cleaned = re.sub(r"\s+([,.;:])", r"\1", cleaned)
return cleaned.strip()
async def _enforce_verify_submit_payload(
payload: dict[str, str],
*,
bullets: list[str],
snippets: list[str],
peer_sections: dict[str, str] | None,
pinned_identity: dict[str, str] | None = None,
) -> dict[str, str]:
"""Run the non-invention guard on the five free-text fields the LLM submitted.
The agentic ``submit_inspection_section`` tool accepts five strings drafted
by the LLM. The legacy fixed pipeline runs ``async_enforce_verify`` on every
drafted section before persistence; the agentic path bypassed it entirely,
which is how invented addresses, postcodes, and construction-type claims
were reaching the user-visible report.
We treat all of the following as "trusted source material" β anything the
LLM uses outside of these is invention:
- ``bullets`` : the surveyor's messy notes for THIS section
- ``peer_sections`` : the surveyor's draft notes for sibling sections
(legitimate cross-section context)
- ``snippets`` : retrieved tenant + KB chunks deduped by the loop
The five fields are verified concurrently with ``asyncio.gather``; each call
runs the cheap regex pass first and only falls through to the LLM grounding
pass when an OpenAI key is configured. Cost is at most one extra mini-LLM
call per non-empty field (~Β£0.0001β0.0005 each).
"""
peer_values = list((peer_sections or {}).values())
full_bullets = list(bullets) + [v for v in peer_values if isinstance(v, str) and v.strip()]
field_names = (
"executive_summary",
"property_description",
"condition_assessment",
"defects_and_risks",
"recommendations",
)
async def _verify_one(name: str) -> tuple[str, str]:
text = payload.get(name) or ""
if not text.strip():
return name, text
try:
verified = await async_enforce_verify(
text=text,
bullets=full_bullets,
snippets=snippets,
pinned_identity=pinned_identity,
openai_api_key=settings.openai_api_key or "",
model=settings.chat_model,
)
return name, _strip_missing_fact_phrase(verified)
except Exception as exc: # noqa: BLE001
# Grounding must never block the section β a verifier failure is
# logged and the original LLM text is returned, matching the
# behaviour of the legacy pipeline.
logger.warning(
"Non-invention guard failed for field=%s (%s); returning raw text",
name,
exc,
)
return name, _strip_missing_fact_phrase(text)
results = await asyncio.gather(*(_verify_one(n) for n in field_names))
return {**payload, **dict(results)}
# ---------------------------------------------------------------------------
# Comprehensive non-invention guard for ALL LLM-emitted artifacts.
# ---------------------------------------------------------------------------
# The agentic inspector emits text in *six* places, not five:
#
# 1. submit_inspection_section payload (5 user-visible body fields) β full
# regex + LLM grounding pass via _enforce_verify_submit_payload.
# 2. submit_extraction_audit (property_address, surveyor_name, β¦)
# 3. submit_section_plan (claims[*].claim, non_claims[*])
# 4. submit_condition_rating_summary (justification, summary_markdown_table)
# 5. RiskAssessmentAgent.assess (risk[*].risk, risk[*].action)
# 6. compliance check (heuristic β already deterministic)
#
# Without (2)β(5) the user could see invented postcodes, surveyor names, or
# risk-action references in the metadata bundle the API returns
# (`/agentic/...` endpoints surface extraction_audit + section_plan +
# condition_rating_summary in inspector_meta). The five body fields run the
# expensive 2-layer guard because they ARE the report; auxiliary fields run
# the cheap synchronous regex pass β that catches postcodes, named entities,
# and bare numbers without adding 30+ extra LLM calls per section.
def _regex_verify_str(
text: Any,
*,
bullets: list[str],
snippets: list[str],
pinned_identity: dict[str, str] | None = None,
) -> str:
"""Run the synchronous regex-only guard on a single string-typed field.
Returns the input unchanged when it isn't a non-empty string. Used for
auxiliary fields where latency / cost matters and most violations are
short factual claims (postcodes, addresses, named persons/firms).
"""
if not isinstance(text, str) or not text.strip():
return text if isinstance(text, str) else ""
return enforce_verify(
text=text,
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
def _verify_audit_payload(
audit: Any,
*,
bullets: list[str],
snippets: list[str],
pinned_identity: dict[str, str] | None = None,
) -> dict[str, Any] | None:
if not isinstance(audit, dict):
return audit
out = dict(audit)
for key in (
"property_address",
"surveyor_name",
"rics_number",
"company_name",
"inspection_date",
"report_reference",
"property_type",
"construction_details",
"services",
):
if key in out:
out[key] = _regex_verify_str(
out.get(key),
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
for list_key in ("client_names", "observed_defects"):
items = out.get(list_key)
if isinstance(items, list):
out[list_key] = [
_regex_verify_str(
x,
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
for x in items
if isinstance(x, str)
]
return out
def _verify_plan_payload(
plan: Any,
*,
bullets: list[str],
snippets: list[str],
pinned_identity: dict[str, str] | None = None,
) -> dict[str, Any] | None:
if not isinstance(plan, dict):
return plan
out = dict(plan)
claims = out.get("claims")
if isinstance(claims, list):
verified_claims: list[dict[str, Any]] = []
for c in claims:
if isinstance(c, dict):
cc = dict(c)
if "claim" in cc:
cc["claim"] = _regex_verify_str(
cc.get("claim"),
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
verified_claims.append(cc)
else:
verified_claims.append(c)
out["claims"] = verified_claims
nc = out.get("non_claims")
if isinstance(nc, list):
out["non_claims"] = [
_regex_verify_str(
x,
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
for x in nc
if isinstance(x, str)
]
return out
def _verify_condition_payload(
cond: Any,
*,
bullets: list[str],
snippets: list[str],
pinned_identity: dict[str, str] | None = None,
) -> dict[str, Any] | None:
if not isinstance(cond, dict):
return cond
out = dict(cond)
for k in ("justification", "summary_markdown_table"):
if k in out:
out[k] = _regex_verify_str(
out.get(k),
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
)
return out
def _verify_risks(
risks: list[RiskItem],
*,
bullets: list[str],
snippets: list[str],
pinned_identity: dict[str, str] | None = None,
) -> list[RiskItem]:
"""Strip invented postcodes / addresses / named firms from LLM-drafted risks.
`RiskItem` is frozen, so we rebuild each item; `category`, `severity`, and
`likelihood` are constrained to known enum values upstream and never need
verification, so we only sweep `risk` and `action`.
"""
out: list[RiskItem] = []
for r in risks:
try:
out.append(
RiskItem(
category=r.category,
risk=_regex_verify_str(
r.risk,
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
),
severity=r.severity,
likelihood=r.likelihood,
action=_regex_verify_str(
r.action,
bullets=bullets,
snippets=snippets,
pinned_identity=pinned_identity,
),
evidence=r.evidence,
)
)
except Exception as exc: # noqa: BLE001
logger.warning("Risk verification failed (%s); keeping raw item", exc)
out.append(r)
return out
def _openai_inspector_tool_definitions() -> list[dict[str, Any]]:
"""Narrow tool schemas: tenant/document IDs are injected server-side."""
return [
{
"type": "function",
"function": {
"name": "retrieve_survey_rag",
"description": (
"Search the tenant's indexed RAG corpus (uploaded survey PDF/DOCX and reference "
"library), prioritising the report's primary document. Call this when you need "
"verbatim or technical evidence from the uploaded survey before writing findings."
),
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Semantic query distilled from the inspection notes (concise).",
},
"k": {
"type": "integer",
"description": "Max chunks to retrieve before reranking (default 14).",
"minimum": 1,
"maximum": 30,
},
"rerank_top_n": {
"type": "integer",
"description": "How many chunks to return after reranking (default 7).",
"minimum": 1,
"maximum": 15,
},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "retrieve_rics_kb",
"description": (
"Search the local RICS standards / exemplar knowledge base for professional "
"wording, definitions, and compliance framing. Use when notes are thin or you "
"need authoritative context (still ground factual claims in survey RAG)."
),
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"k": {"type": "integer", "description": "Max KB chunks to fetch (default 10).", "minimum": 1, "maximum": 20},
"rerank_top_n": {"type": "integer", "description": "Chunks to keep after rerank (default 5).", "minimum": 1, "maximum": 10},
"hierarchy_level": {
"type": "string",
"enum": ["document", "section", "paragraph"],
"description": "Optional granularity filter when the index supports hierarchy.",
},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "scan_note_duplicates",
"description": (
"Check the tenant's indexed library for semantically similar passages and "
"detect near-duplicate wording across other sections' drafts. Use when notes "
"may overlap prior reports or other sections."
),
"parameters": {
"type": "object",
"properties": {
"text": {"type": "string", "description": "Notes block to compare (e.g. joined bullets)."},
"section_code": {"type": "string"},
"peer_sections": {
"type": "object",
"additionalProperties": {"type": "string"},
"description": "Map section_code β draft text for cross-section overlap.",
},
"exclude_document_ids": {
"type": "array",
"items": {"type": "string"},
"description": "Upload UUIDs to omit from library similarity (e.g. primary survey).",
},
},
"required": ["text"],
},
},
},
{
"type": "function",
"function": {
"name": "submit_extraction_audit",
"description": (
"MANDATORY (server-enforced before final submit): structured extraction from the "
"inspection notes and survey RAG snippets β what is evidenced vs unknown. "
"Call once per section after you have run retrieve_survey_rag / retrieve_rics_kb "
"enough to understand the fact basis."
),
"parameters": {
"type": "object",
"properties": {
"property_address": {"type": "string"},
"client_names": {"type": "array", "items": {"type": "string"}},
"surveyor_name": {"type": "string"},
"rics_number": {"type": "string"},
"company_name": {"type": "string"},
"inspection_date": {"type": "string"},
"report_reference": {"type": "string"},
"property_type": {"type": "string"},
"construction_details": {"type": "string"},
"services": {"type": "string"},
"observed_defects": {"type": "array", "items": {"type": "string"}},
"limitations_to_inspection": {"type": "array", "items": {"type": "string"}},
"assumptions": {"type": "array", "items": {"type": "string"}},
},
"required": [
"property_address",
"client_names",
"surveyor_name",
"rics_number",
"company_name",
"inspection_date",
"report_reference",
"property_type",
"construction_details",
"services",
"observed_defects",
"limitations_to_inspection",
"assumptions",
],
},
},
},
{
"type": "function",
"function": {
"name": "submit_section_plan",
"description": (
"MANDATORY (server-enforced before final submit): a short plan mapping claims to "
"evidence chunk_ids / doc_ids you intend to rely on. Ratings (1/2/3/NI) must appear "
"only where the active RICS product tier uses condition ratings for that element group."
),
"parameters": {
"type": "object",
"properties": {
"claims": {
"type": "array",
"items": {
"type": "object",
"properties": {
"claim": {"type": "string"},
"evidence_refs": {
"type": "array",
"items": {"type": "string"},
"description": "chunk_id values and/or doc_id:chunk_id pairs from tool hits.",
},
"risk": {
"type": "string",
"enum": ["low", "medium", "high"],
},
},
"required": ["claim", "evidence_refs", "risk"],
},
},
"non_claims": {
"type": "array",
"items": {"type": "string"},
"description": "Boilerplate you will keep generic (no site-specific facts).",
},
},
"required": ["claims", "non_claims"],
},
},
},
{
"type": "function",
"function": {
"name": "submit_condition_rating_summary",
"description": (
"When the active survey product includes condition ratings anywhere in its template "
"pack, Section C must summarise ratings in a markdown table before the final submit. "
"If ratings truly do not apply for this property type, set ratings_not_applicable=true "
"with a one-line justification instead of inventing counts."
),
"parameters": {
"type": "object",
"properties": {
"ratings_not_applicable": {"type": "boolean"},
"justification": {"type": "string"},
"summary_markdown_table": {
"type": "string",
"description": "Markdown table with columns Category | Count NI | Count 3 | Count 2 | Count 1",
},
},
"required": ["ratings_not_applicable"],
},
},
},
{
"type": "function",
"function": {
"name": "submit_inspection_section",
"description": (
"Submit the final drafted content for this RICS section. Server will reject this "
"until submit_extraction_audit and submit_section_plan have been accepted. "
"When the product pack includes condition ratings, Section C also requires "
"submit_condition_rating_summary first. "
"Ground factual statements in retrieve_survey_rag / retrieve_rics_kb results. "
"If information is missing, omit the claim rather than writing placeholder "
"sentences. Never insert missing-data text mid-sentence. "
"UK English, MRICS tone. Call alone (no other tools in the same assistant message). "
"PRODUCE PROPERTY-SPECIFIC PROSE AT THE TIER-APPROPRIATE LENGTH β NOT SUMMARIES, "
"NOR PADDING. Each free-text field below is the user-visible report; aim at the "
"per-tier word_targets in the SYSTEM PROMPT (L1 condensed observation, L2 "
"proportionate buyer advice, L3 diagnostic depth). Do NOT collapse an L3 field to "
"one or two sentences; do NOT inflate an L1 field beyond its observation-only "
"range. Cite specific defects, locations, materials, and observed evidence β "
"never generic boilerplate."
),
"parameters": {
"type": "object",
# Schema descriptions deliberately defer length / depth to
# the per-tier SYSTEM PROMPT (word_targets + depth_directive
# in :func:`_inspector_system_prompt`) instead of baking in
# L3-leaning ranges. Otherwise the model sees "target
# 200β500 words" for condition_assessment and tries to hit
# that even when the active tier is L1 (40β90 words), or
# tries a Level-3-style cause/implications/options layering
# in an L1 product (which is forbidden).
"properties": {
"executive_summary": {
"type": "string",
"description": (
"Headline summary for THIS section. Length per tier β see SYSTEM "
"PROMPT word_targets. State scope of inspection, principal "
"observations, and overall outcome. Avoid generic openings like "
"'This section coversβ¦'."
),
},
"property_description": {
"type": "string",
"description": (
"Specific factual description of the section subject. Length per "
"tier β see SYSTEM PROMPT word_targets. Cover construction type, "
"materials, accommodation, age, services, and dimensions WHEN "
"supported by bullets/snippets. Use the approved missing-info "
"phrase only for facts truly unavailable."
),
},
"condition_assessment": {
"type": "string",
"description": (
"Condition narrative. Length and depth per tier β see SYSTEM "
"PROMPT word_targets and depth_directive (L1 observation-only; "
"L2 proportionate; L3 cause β implications β options). Cite each "
"defect with location and severity at the depth appropriate to "
"the active tier; do not over-extend an L1 paragraph or under-fill "
"an L3 one."
),
},
"defects_and_risks": {
"type": "string",
"description": (
"Defects mapped to category (structural / moisture / electrical / "
"etc.), with location, cause (where supported), and consequence. "
"Length per tier β see SYSTEM PROMPT word_targets. List each "
"evidenced defect; do not omit any from the bullets."
),
},
"recommendations": {
"type": "string",
"description": (
"Actionable recommendations with urgency, tied to an observed "
"defect or evidence reference. Length per tier β see SYSTEM "
"PROMPT word_targets. For Level 1 (Condition Report) the system "
"prompt forbids advice β in that case write only that no "
"recommendations are issued at this tier, and stop."
),
},
},
"required": [
"executive_summary",
"property_description",
"condition_assessment",
"defects_and_risks",
"recommendations",
],
},
},
},
]
def _compact_hits_for_llm(hits: list[SearchResult], *, max_items: int = 8, max_chars: int = 520) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
for r in hits[:max_items]:
t = (r.text or "").strip().replace("\n", " ")
if len(t) > max_chars:
t = t[: max_chars - 1] + "β¦"
out.append(
{
"chunk_id": r.chunk_id,
"doc_id": r.doc_id,
"score": round(float(r.score), 5),
"kb": bool(getattr(r, "kb", False)),
"text": t,
}
)
return out
def _pack_has_condition_ratings(pack: SurveyTemplatePack) -> bool:
return any(bool(t.has_condition_rating) for t in pack._by_code.values())
def _section_c_requires_rating_summary(*, section_code: str, pack: SurveyTemplatePack) -> bool:
if section_code != "C":
return False
return _pack_has_condition_ratings(pack)
def _norm_str_list(val: Any, *, min_items: int, max_items: int, max_item_len: int) -> tuple[str, list[str] | None]:
if not isinstance(val, list):
return ("must be a JSON array of strings", None)
out: list[str] = []
for x in val[:max_items]:
s = str(x).strip()
if not s:
continue
if len(s) > max_item_len:
s = s[: max_item_len - 1] + "β¦"
out.append(s)
if len(out) < min_items:
return (f"need at least {min_items} non-empty string item(s)", None)
return ("", out)
def _validate_extraction_audit(args: dict[str, Any]) -> tuple[str, dict[str, Any] | None]:
# Mandatory extraction schema for reconstruction. Use the two approved phrases when unknown.
required_str_fields = [
"property_address",
"surveyor_name",
"rics_number",
"company_name",
"inspection_date",
"report_reference",
"property_type",
"construction_details",
"services",
]
out: dict[str, Any] = {}
errs: list[str] = []
for k in required_str_fields:
v = str(args.get(k, "")).strip()
if not v:
errs.append(f"{k} is required")
elif len(v) > 1200:
out[k] = v[:1199] + "β¦"
else:
out[k] = v
cn = args.get("client_names")
if not isinstance(cn, list):
errs.append("client_names must be a JSON array of strings")
client_names = None
else:
client_names = [str(x).strip() for x in cn if str(x).strip()][:12]
if not client_names:
errs.append("client_names must contain at least one name or an approved missing-info phrase")
if client_names is not None:
out["client_names"] = client_names
od_msg, observed_defects = _norm_str_list(args.get("observed_defects"), min_items=0, max_items=40, max_item_len=420)
if od_msg:
errs.append(f"observed_defects: {od_msg}")
else:
out["observed_defects"] = observed_defects
lim_msg, limitations = _norm_str_list(args.get("limitations_to_inspection"), min_items=0, max_items=24, max_item_len=420)
if lim_msg:
errs.append(f"limitations_to_inspection: {lim_msg}")
else:
out["limitations_to_inspection"] = limitations
asm_msg, assumptions = _norm_str_list(args.get("assumptions"), min_items=0, max_items=16, max_item_len=420)
if asm_msg:
errs.append(f"assumptions: {asm_msg}")
else:
out["assumptions"] = assumptions
if errs:
return ("; ".join(errs), None)
return ("", out)
def _validate_section_plan(args: dict[str, Any]) -> tuple[str, dict[str, Any] | None]:
claims_raw = args.get("claims")
if not isinstance(claims_raw, list) or not claims_raw:
return ("claims must be a non-empty JSON array", None)
claims_out: list[dict[str, Any]] = []
for c in claims_raw[:30]:
if not isinstance(c, dict):
return ("each claim must be an object", None)
claim = str(c.get("claim", "")).strip()
if len(claim) < 12:
return ("each claim.claim must be a substantive string", None)
refs = c.get("evidence_refs")
if not isinstance(refs, list) or not refs:
return ("each claim needs a non-empty evidence_refs array", None)
refs_s = [str(x).strip() for x in refs if str(x).strip()][:24]
if not refs_s:
return ("evidence_refs must contain non-empty strings", None)
risk = str(c.get("risk", "")).strip().lower()
if risk not in ("low", "medium", "high"):
return ("claim.risk must be one of: low | medium | high", None)
claims_out.append({"claim": claim[:900], "evidence_refs": refs_s, "risk": risk})
nc_msg, non_claims = _norm_str_list(args.get("non_claims"), min_items=0, max_items=24, max_item_len=300)
if nc_msg:
return (nc_msg, None)
assert non_claims is not None
return ("", {"claims": claims_out, "non_claims": non_claims})
def _validate_condition_rating_summary(args: dict[str, Any]) -> tuple[str, dict[str, Any] | None]:
na = args.get("ratings_not_applicable")
if not isinstance(na, bool):
return ("ratings_not_applicable must be a boolean", None)
justification = str(args.get("justification", "")).strip()
table = str(args.get("summary_markdown_table", "")).strip()
if na:
if len(justification) < 12:
return ("when ratings_not_applicable=true, justification must explain why (substantive).", None)
return ("", {"ratings_not_applicable": True, "justification": justification[:800], "summary_markdown_table": ""})
lowered = table.lower()
if "|" not in table or "\n" not in table:
return ("summary_markdown_table must be a markdown table (header + separator + rows).", None)
for token in ("ni", "3", "2", "1"):
if token not in lowered:
return ("table must include NI and numeric rating columns/labels for 3, 2, and 1.", None)
return ("", {"ratings_not_applicable": False, "justification": "", "summary_markdown_table": table[:6000]})
async def _dispatch_tool(
*,
db,
tenant_id: str,
primary_document_id: str,
reference_document_ids: list[str] | None,
default_hierarchy_level: str | None,
peer_sections_default: dict[str, str] | None,
section_code: str,
pack: SurveyTemplatePack,
gate: dict[str, Any],
name: str,
args: dict[str, Any],
) -> tuple[str, list[SearchResult]]:
"""Run one tool; return JSON string for the assistant plus any new search hits."""
extra_hits: list[SearchResult] = []
try:
if name == "retrieve_survey_rag":
q = str(args.get("query", "")).strip()
if not q:
return json.dumps({"error": "empty query"}), []
k = int(args.get("k", 14))
rn = int(args.get("rerank_top_n", 7))
k = max(4, min(48, k))
rn = max(2, min(16, rn))
hits = await agent_tools.retrieve_tenant_evidence_async(
query=q,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
secondary_document_ids=reference_document_ids,
k=k,
rerank_top_n=rn,
)
extra_hits.extend(hits)
return json.dumps({"hits": _compact_hits_for_llm(hits)}), hits
if name == "retrieve_rics_kb":
from app.services.personalised_rag import tenant_has_personal_library
if settings.personalised_style_rag_enabled and await tenant_has_personal_library(
tenant_id
):
return (
json.dumps(
{
"hits": [],
"message": (
"KB retrieval disabled: use retrieve_survey_rag for this "
"tenant's private uploaded report library."
),
}
),
[],
)
q = str(args.get("query", "")).strip()
if not q:
return json.dumps({"hits": [], "message": "KB disabled or empty query"}), []
k = int(args.get("k", 10))
rn = int(args.get("rerank_top_n", 5))
hl = args.get("hierarchy_level")
if isinstance(hl, str) and hl not in ("document", "section", "paragraph"):
hl = default_hierarchy_level
elif hl is None:
hl = default_hierarchy_level if default_hierarchy_level in ("document", "section", "paragraph") else None
hits = await agent_tools.retrieve_kb_guidance_async(
query=q, k=k, hierarchy_level=hl, rerank_top_n=rn
)
extra_hits.extend(hits)
return json.dumps({"hits": _compact_hits_for_llm(hits), "kb_enabled": settings.knowledge_base_enabled}), hits
if name == "scan_note_duplicates":
text = str(args.get("text", "")).strip()
if not text:
return json.dumps({"library_matches": [], "draft_overlaps": [], "message": "no text"}), []
peers = args.get("peer_sections")
if not isinstance(peers, dict):
peers = dict(peer_sections_default or {})
sec = args.get("section_code")
sec_s = str(sec) if sec is not None else None
excl = args.get("exclude_document_ids")
excl_l = [str(x) for x in excl] if isinstance(excl, list) else []
sim = await agent_tools.find_similar_library_and_peers(
db,
tenant_id,
text=text,
section_code=sec_s,
peer_sections=peers,
exclude_document_ids=excl_l,
)
payload = {
"library_matches": [m.model_dump() for m in sim.library_matches[:12]],
"draft_overlaps": [m.model_dump() for m in sim.draft_overlaps[:12]],
"message": (sim.message or "")[:500],
}
return json.dumps(payload), []
if name == "submit_extraction_audit":
err, payload = _validate_extraction_audit(args)
if err or payload is None:
return json.dumps({"ok": False, "error": err or "invalid payload"}), []
gate["audit_payload"] = payload
return json.dumps({"ok": True, "accepted": "extraction_audit"}), []
if name == "submit_section_plan":
err, payload = _validate_section_plan(args)
if err or payload is None:
return json.dumps({"ok": False, "error": err or "invalid payload"}), []
if gate.get("audit_payload") is None:
return json.dumps(
{
"ok": False,
"error": "submit_extraction_audit must be accepted before submit_section_plan.",
}
), []
gate["plan_payload"] = payload
return json.dumps({"ok": True, "accepted": "section_plan"}), []
if name == "submit_condition_rating_summary":
err, payload = _validate_condition_rating_summary(args)
if err or payload is None:
return json.dumps({"ok": False, "error": err or "invalid payload"}), []
if not _section_c_requires_rating_summary(section_code=section_code, pack=pack):
return json.dumps(
{
"ok": False,
"error": "submit_condition_rating_summary is only used for Section C when the active product pack includes condition ratings.",
}
), []
# Enforce RICS-like grouping: the table must mention every rated element group prefix
# present in this product pack (e.g. E/F/G for Building Survey).
required_prefixes = sorted(
{t.code[0] for t in pack._by_code.values() if t.has_condition_rating and t.code and t.code[0].isalpha()}
)
table = str(payload.get("summary_markdown_table") or "")
if required_prefixes and table:
missing_prefixes = []
for pfx in required_prefixes:
# row like "| E1" or "| E "
if f"| {pfx}" not in table and f"|{pfx}" not in table:
missing_prefixes.append(pfx)
if missing_prefixes:
return json.dumps(
{
"ok": False,
"error": (
"Condition rating summary table must include rows for these element groups "
f"(from the active template pack): {', '.join(missing_prefixes)}."
),
}
), []
gate["condition_payload"] = payload
return json.dumps({"ok": True, "accepted": "condition_rating_summary"}), []
if name == "submit_inspection_section":
if gate.get("audit_payload") is None:
return json.dumps(
{"ok": False, "error": "Call and pass submit_extraction_audit before submit_inspection_section."}
), []
if gate.get("plan_payload") is None:
return json.dumps(
{"ok": False, "error": "Call and pass submit_section_plan before submit_inspection_section."}
), []
if _section_c_requires_rating_summary(section_code=section_code, pack=pack) and gate.get("condition_payload") is None:
return json.dumps(
{
"ok": False,
"error": "Section C in this product tier requires submit_condition_rating_summary before submit_inspection_section.",
}
), []
gate["submit_payload"] = {
"executive_summary": str(args.get("executive_summary", "")).strip(),
"property_description": str(args.get("property_description", "")).strip(),
"condition_assessment": str(args.get("condition_assessment", "")).strip(),
"defects_and_risks": str(args.get("defects_and_risks", "")).strip(),
"recommendations": str(args.get("recommendations", "")).strip(),
}
return json.dumps({"ok": True, "accepted": "submit_inspection_section"}), []
except Exception as exc: # noqa: BLE001
logger.exception("inspector tool %s failed", name)
return json.dumps({"error": str(exc)}), []
return json.dumps({"error": f"unknown tool {name}"}), []
async def _evidence_items_async(db, tenant_id: str, uniq: list[SearchResult]) -> list[EvidenceItem]:
tenant_doc_ids = {r.doc_id for r in uniq if r.tenant_id == tenant_id and r.doc_id}
filenames: dict[str, str | None] = {}
if db is not None and tenant_doc_ids:
filenames = await fetch_doc_filenames(db, tenant_id, tenant_doc_ids)
items: list[EvidenceItem] = []
for r in uniq:
is_kb = bool(getattr(r, "kb", False)) or r.tenant_id == settings.knowledge_base_tenant_id
src = (getattr(r, "source", None) or None) if is_kb else filenames.get(r.doc_id)
if not src and is_kb:
src = "Local RICS knowledge base"
items.append(
EvidenceItem(
doc_id=r.doc_id,
chunk_id=r.chunk_id,
score=float(r.score),
text=r.text,
source=str(src) if src else None,
section_hint=getattr(r, "section_title", None),
kb=is_kb,
)
)
return items
def _inspector_system_prompt(
*,
tenant_id: str,
primary_document_id: str,
survey_level: int | None,
pack: SurveyTemplatePack,
section_code: str,
section_title: str,
style_profile: WritingStyleProfile | None,
identity_block: str | None = None,
) -> str:
tone = style_profile.tone if style_profile and style_profile.tone else "professional, precise UK building surveyor"
order_preview = ", ".join(list(pack.section_order)[:34])
if len(pack.section_order) > 34:
order_preview += ", β¦"
ratings_pack = _pack_has_condition_ratings(pack)
c_extra = ""
if section_code == "C" and ratings_pack:
c_extra = (
" For Section C in this product tier, you must also call submit_condition_rating_summary "
"(markdown rating table, or ratings_not_applicable with justification) before the final submit."
)
# ββ Per-tier depth directive βββββββββββββββββββββββββββββββββββββββββββ
# Real RICS L3 element narratives describe each defect as
# cause β implications β options/next steps, in continuous diagnostic
# prose. Telling the model the *upper* end of the word range (and that
# L3 specifically must layer cause/implications/options) is what moves
# gpt-4o-mini from "150-word HomeBuyer-style paragraph" to actual
# diagnostic depth. Without an explicit L3 directive the model defaults
# to its trained-average paragraph length, which is roughly L2.
try:
lvl = int(survey_level if survey_level is not None else pack.level)
except Exception: # noqa: BLE001
lvl = pack.level
if lvl >= 3:
depth_directive = (
"DEPTH FOR LEVEL 3 (BUILDING SURVEY β DIAGNOSTIC MODE): aim at the UPPER end of every "
"word range below. For each material defect, weave (within the relevant field): (a) what "
"was observed, (b) the likely cause/mechanism (only if supported by evidence), "
"(c) implications/risks if unaddressed, and (d) options/next steps. Do NOT collapse "
"into HomeBuyer-style brevity. Each defect must surface in defects_and_risks AND "
"carry through to recommendations with a tied action. "
)
word_targets = (
"executive_summary ~30β60 words, property_description ~35β70 words, "
"condition_assessment ~60β120 words, defects_and_risks ~35β80 words, "
"recommendations ~20β45 words. "
)
elif lvl == 2:
depth_directive = (
"DEPTH FOR LEVEL 2 (HomeBuyer): proportionate buyer-focused advice. Cover practical next "
"steps for material defects (e.g. obtain quotations, further checks). Avoid deep "
"diagnostic speculation unless the bullets/snippets explicitly support it. "
)
word_targets = (
"executive_summary ~20β45 words, property_description ~25β50 words, "
"condition_assessment ~45β90 words, defects_and_risks ~25β55 words, "
"recommendations ~15β35 words. "
)
else:
depth_directive = (
"DEPTH FOR LEVEL 1 (Condition Report β observation mode): record condition concisely. "
"Do NOT advise on repairs, recommend works, or use directive phrasing such as "
"'we recommend' or 'should be replaced'. Keep paragraphs factual and observation-only. "
)
word_targets = (
"executive_summary ~45β85 words, property_description ~55β110 words, "
"condition_assessment ~110β220 words, defects_and_risks ~55β120 words, "
"recommendations ~30β70 words. "
)
identity_block_clean = (identity_block or "").strip()
identity_clause = (
"PROPERTY IDENTITY (NON-NEGOTIABLE, must be reused verbatim β do NOT introduce any "
"different address, postcode, property type, surveyor name, or company name):\n"
f"{identity_block_clean}\n\n"
if identity_block_clean
else ""
)
return (
"You are a Chartered Building Surveyor (MRICS) producing a RICS-style inspection report section. "
"You decide which retrieval tools to call and how many timesβthere is no fixed script for RAG/KB. "
"Ground factual statements in retrieve_survey_rag and/or retrieve_rics_kb results; "
"use scan_note_duplicates when messy notes might duplicate other sections or prior library text. "
"Never invent site-specific facts. If a fact is missing from bullets, seed evidence, retrieve_* "
"results, and PROPERTY IDENTITY pins, omit that claim instead of using placeholder text. "
"Never inline missing-data wording in the middle of a sentence. "
+ identity_clause +
# ββ Anti-summarisation + per-tier depth directive βββββββββββββββββ
# The depth directive replaces the previous static word-target list,
# which gave the same range for every level. With per-tier targets
# gpt-4o-mini stops collapsing L3 sections into HomeBuyer-length
# paragraphs.
"PRODUCE COMPREHENSIVE PROPERTY-SPECIFIC ANALYSIS, NOT SUMMARIES. The five free-text fields "
"in submit_inspection_section ARE the user-visible report. Each one must be substantive "
"RICS-grade prose grounded in bullets and retrieved snippets β for THIS tier: "
+ word_targets
+ "Do NOT pad with generic boilerplate. Do NOT shorten to one or two sentences. "
"When a topic is sparsely evidenced, retrieve more with retrieve_survey_rag or retrieve_rics_kb "
"rather than truncating. Use the approved missing-info phrases ONLY for facts you genuinely "
"cannot verify in the bullets/snippets β never as a substitute for analysis. Surface all "
"evidenced defects from the bullets; do not omit any. "
+ depth_directive +
f"Writing tone guidance: {tone}. "
f"Server context (inject into tool calls automatically where applicable): tenant_id={tenant_id!r}, "
f"primary_document_id={primary_document_id!r}. "
f"RICS product tier: Level {pack.level} β {pack.product_label}. "
f"Client survey_level field: {survey_level!r} (None is treated as Level 3 for template pack resolution). "
f"Mandatory section order for this tier (codes): {order_preview}. "
f"Current section: {section_code} β {section_title}. "
"SERVER-ENFORCED WORKFLOW (tools): "
"(1) retrieve evidence as needed β "
"(2) submit_extraction_audit β "
"(3) submit_section_plan β "
f"(4){' submit_condition_rating_summary β' if section_code == 'C' and ratings_pack else ''} "
"(5) submit_inspection_section (exactly once). "
"Do not call submit_inspection_section in the same assistant message as any other tool. "
"UK English."
f"{c_extra}"
)
async def run_inspector_tool_loop(
*,
db,
tenant_id: str,
primary_document_id: str,
section_code: str,
bullets: list[str],
style_profile: WritingStyleProfile | None,
ai_percent: int,
retrieval_level: str,
reference_document_ids: list[str] | None,
peer_sections: dict[str, str] | None,
survey_level: int | None = None,
) -> tuple[StructuredReport, list[dict[str, Any]]]:
"""Run the autonomous inspector loop; returns structured report + tool trace."""
trace: list[dict[str, Any]] = []
from app.agentic.speculative_executor import (
PatternRegistry,
SpeculativeToolDispatcher,
speculative_execution_active,
)
_spec_registry = PatternRegistry()
_spec_context: dict[str, Any] = {
"section_code": section_code,
"outline": "",
}
pack = get_survey_pack(survey_level)
template = get_template(section_code, survey_level)
section_title = template.title if template else section_code
skeleton = (template.skeleton if template else "")[:2000]
_spec_context["outline"] = skeleton
# ββ Pre-seeded grounding (key fix for "LLM invents because it had no evidence")
# Without an upfront retrieval seed, the inspector loop relied entirely on
# the LLM to discover useful queries via retrieve_survey_rag. When the
# bullets are sparse (the user's exact complaint scenario β messy notes
# uploaded for an L3 section), the LLM's first query is often vague
# ("section E2 condition"), the result set is bland, the LLM gives up
# retrieving and falls back to its training-data prior. That's where
# "10 Kingsley Avenue", "robust steel frame", and made-up surveyor names
# come from.
#
# We now do a deterministic up-front retrieval against the primary
# uploaded survey (and any user-tagged reference docs) and inject the
# top hits straight into the user message as SEED EVIDENCE, plus
# extracted IDENTITY FACTS and STYLE REFERENCE PARAGRAPHS from the
# tenant's profile. The LLM still has full freedom to call additional
# retrieve_* tools, but starts with concrete grounded material β which
# both reduces invention and gives the verifier real text to compare
# the draft against.
#
# Local imports keep the cycle clean: the agent tools layer already
# imports inspector_loop indirectly via agents.py.
from app.agentic import tools as agent_tools_local
from app.services.generation import _extract_property_identity, _identity_facts_block
seed_hits: list[SearchResult] = []
seed_query = " ".join(
[section_code, section_title]
+ [b for b in bullets if isinstance(b, str) and b.strip()][:12]
).strip()
# Tier-aware seed retrieval window. Previously L1 and L2 received the
# same window (k=16/rerank=6), which under-served L2 (HomeBuyer needs
# enough evidence to give *proportionate* advice β i.e. not just
# observation, but options-light) and over-served L1 (observation-only,
# narrower scope). The new split:
# L1 β k=12 / rerank=5 (observation-only, condensed RAG window)
# L2 β k=18 / rerank=8 (HomeBuyer; more evidence than L1, less than L3)
# L3 β k=24 / rerank=10 (Building Survey; full diagnostic context)
effective_level = survey_level if survey_level is not None else pack.level
try:
_seed_lvl = int(effective_level)
except Exception: # noqa: BLE001
_seed_lvl = pack.level
if _seed_lvl >= 3:
seed_k, seed_rerank = 24, 10
elif _seed_lvl == 2:
seed_k, seed_rerank = 18, 8
else:
seed_k, seed_rerank = 12, 5
if seed_query:
try:
seed_hits = await agent_tools_local.retrieve_tenant_evidence_async(
query=seed_query,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
secondary_document_ids=list(reference_document_ids or []),
k=seed_k,
rerank_top_n=seed_rerank,
)
except Exception as exc: # noqa: BLE001
logger.warning("Inspector seed retrieval failed (%s); proceeding without pre-seed", exc)
seed_hits = []
trace.append({"event": "seed_retrieval", "hits": len(seed_hits), "query_preview": seed_query[:120]})
identity = _extract_property_identity(list(bullets))
if seed_hits:
# Identity often appears in the primary doc but not in messy bullets
# β e.g. the surveyor uploaded the property's own description PDF
# but only typed a few free-form notes for THIS section. Letting the
# extractor see the seed snippets too means the address it pins is
# the address from the actual uploaded survey, not a parsed-out
# bullet fragment. Trim each snippet to keep extraction fast.
ext_lines = list(bullets) + [r.text[:1000] for r in seed_hits[:6] if getattr(r, "text", None)]
identity = _extract_property_identity(ext_lines)
identity_block = _identity_facts_block(identity)
seed_evidence_block = ""
if seed_hits:
# Mirror the seed retrieval tier ladder: don't ship more snippets to
# the LLM than were retrieved, and don't bloat the L1 prompt with
# context the L1 mode won't use.
if _seed_lvl >= 3:
max_seed = 10
elif _seed_lvl == 2:
max_seed = 8
else:
max_seed = 5
seed_lines: list[str] = []
for i, r in enumerate(seed_hits[:max_seed], 1):
t = (r.text or "").strip().replace("\n", " ")
if len(t) > 600:
t = t[:599] + "β¦"
tag_kb = "(KB)" if bool(getattr(r, "kb", False)) else "(uploaded)"
seed_lines.append(f"[#{i}] {tag_kb} {t}")
seed_evidence_block = (
"\n\nSEED EVIDENCE FROM YOUR UPLOADED RAG CORPUS (primary grounding β facts in here "
"are TRUSTED; quote or paraphrase rather than invent):\n"
+ "\n\n".join(seed_lines)
)
style_examples_block = ""
if style_profile is not None:
examples = [
p for p in (getattr(style_profile, "example_paragraphs", []) or [])
if isinstance(p, str) and p.strip()
][:3]
if examples:
style_examples_block = (
"\n\nSTYLE REFERENCE PARAGRAPHS (verbatim extracts from this surveyor's own completed "
"RICS reports β mirror the tone, sentence rhythm, and phrasing patterns; do NOT copy "
"facts from these unless the bullets/seed-evidence also support them):\n"
+ "\n\n".join(f'"{p.strip()[:600]}"' for p in examples)
)
user_intro = (
f"RICS product pack: Level {pack.level} β {pack.product_label}\n"
f"RICS section: {section_code} β {section_title}\n\n"
f"Template skeleton (use as structural guide):\n{skeleton}\n\n"
"Messy inspection notes (bullets β interpret, prioritise, and map to RICS discipline):\n"
+ "\n".join(f"- {b}" for b in bullets if str(b).strip())
+ seed_evidence_block
+ style_examples_block
)
if peer_sections:
user_intro += "\n\nPeer section drafts (for duplicate scan if relevant):\n" + json.dumps(
{str(k): str(v)[:800] for k, v in list(peer_sections.items())[:24]},
ensure_ascii=False,
)
tools = _openai_inspector_tool_definitions()
ai_params = _ai_level_to_params(3, ai_percent=ai_percent)
temperature = float(ai_params["temperature"])
from app.llm.prompt_cache import ensure_cacheable_system_prefix, prompt_caching_active
system_content = _inspector_system_prompt(
tenant_id=tenant_id,
primary_document_id=primary_document_id,
survey_level=survey_level,
pack=pack,
section_code=section_code,
section_title=section_title,
style_profile=style_profile,
identity_block=identity_block,
)
if prompt_caching_active():
system_content = ensure_cacheable_system_prefix(system_content)
messages: list[dict[str, Any]] = [
{"role": "system", "content": system_content},
{"role": "user", "content": user_intro},
]
# Seed hits also feed the eventual non-invention guard so it has concrete
# comparison material. Without this, the verifier saw only the LLM-issued
# retrieve_survey_rag output (which the LLM may have skipped).
all_hits: list[SearchResult] = list(seed_hits)
max_rounds = max(2, min(32, int(settings.inspector_max_tool_rounds)))
submit_payload: dict[str, str] | None = None
gate: dict[str, Any] = {
"audit_payload": None,
"plan_payload": None,
"condition_payload": None,
"submit_payload": None,
}
async def _dispatch_tool_wrapped(*, name: str, args: dict[str, Any]) -> tuple[str, list[SearchResult]]:
return await _dispatch_tool(
db=db,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
reference_document_ids=reference_document_ids,
default_hierarchy_level=retrieval_level if retrieval_level in ("document", "section", "paragraph") else None,
peer_sections_default=peer_sections,
section_code=section_code,
pack=pack,
gate=gate,
name=name,
args=args,
)
_spec_dispatcher: SpeculativeToolDispatcher | None = None
if speculative_execution_active():
_spec_dispatcher = SpeculativeToolDispatcher(
registry=_spec_registry,
dispatch_fn=_dispatch_tool_wrapped,
context=_spec_context,
)
soft_deadline_round = max(2, int(max_rounds * 0.75))
for round_i in range(max_rounds):
# Keep exploratory/tool-selection rounds on the cheaper chat_model.
# Escalate to inspector_body_model only once the workflow is in the
# drafting phase (audit + plan accepted) or when round budget is tight.
drafting_phase = bool(gate.get("audit_payload")) and bool(gate.get("plan_payload"))
current_model = (
settings.inspector_body_model
if drafting_phase or round_i >= soft_deadline_round
else settings.chat_model
)
from app.llm.llm_throttle import make_cache_hit_slot, throttled_llm_call
from app.llm.prompt_cache import (
log_openai_cache_usage,
normalize_messages_for_caching,
openai_extra_kwargs,
prompt_caching_active,
)
async_client = _inspector_async_client()
max_tok = min(6144, max(2048, int(settings.max_output_tokens) * 6))
extra = (
openai_extra_kwargs(
phase="inspector_tool_round",
model=current_model,
survey_level=survey_level,
tenant_id=tenant_id,
)
if prompt_caching_active()
else {}
)
cache_slot = make_cache_hit_slot()
api_messages = (
normalize_messages_for_caching(messages)
if prompt_caching_active()
else messages
)
async def _async_round() -> object:
response = await async_client.chat.completions.create(
model=current_model,
messages=api_messages,
tools=tools,
tool_choice="auto",
temperature=temperature,
max_tokens=max_tok,
**extra,
)
if prompt_caching_active():
cache_slot[0] = log_openai_cache_usage(
response,
phase="inspector_tool_round",
section_id=section_code,
)
return response
resp = await throttled_llm_call(
phase="inspector_tool_round",
section_id=section_code,
cache_hit_out=cache_slot,
call=_async_round,
)
msg = resp.choices[0].message
if not msg.tool_calls:
messages.append({"role": "assistant", "content": msg.content or ""})
messages.append(
{
"role": "user",
"content": (
"Continue with the SERVER-ENFORCED tool workflow: "
"submit_extraction_audit β submit_section_plan β "
f"{'submit_condition_rating_summary β ' if _section_c_requires_rating_summary(section_code=section_code, pack=pack) else ''}"
"then submit_inspection_section (five fields). "
"Use retrieve_survey_rag / retrieve_rics_kb / scan_note_duplicates as needed before the submits. "
"If evidence is thin, do not invent facts; omit unsupported claims."
),
}
)
trace.append({"round": round_i, "note": "no_tool_calls_nudge", "model": current_model})
continue
tcs = list(msg.tool_calls)
names = [tc.function.name for tc in tcs]
submit_count = sum(1 for n in names if n == "submit_inspection_section")
if (submit_count and len(tcs) > 1) or submit_count > 1:
err = (
"Invalid: submit_inspection_section must be the only tool call in the assistant message."
if submit_count and len(tcs) > 1
else "Invalid: at most one submit_inspection_section tool call."
)
trace.append(
{
"round": round_i,
"error": "submit_mixed_with_other_tools" if submit_count and len(tcs) > 1 else "multiple_submits",
}
)
assistant_tool_calls = [
{"id": tc.id, "type": "function", "function": {"name": tc.function.name, "arguments": tc.function.arguments}}
for tc in tcs
]
messages.append({"role": "assistant", "content": msg.content or None, "tool_calls": assistant_tool_calls})
for tc in tcs:
messages.append({"role": "tool", "tool_call_id": tc.id, "content": json.dumps({"ok": False, "error": err})})
continue
assistant_tool_calls = []
for tc in tcs:
assistant_tool_calls.append({"id": tc.id, "type": "function", "function": {"name": tc.function.name, "arguments": tc.function.arguments}})
messages.append(
{
"role": "assistant",
"content": msg.content or None,
"tool_calls": assistant_tool_calls,
}
)
for tc in tcs:
name = tc.function.name
raw = tc.function.arguments or "{}"
try:
args = json.loads(raw)
except json.JSONDecodeError:
args = {}
trace.append({"round": round_i, "tool": name, "arguments_preview": raw[:400], "model": current_model})
if _spec_dispatcher is not None:
body, hits = await _spec_dispatcher.dispatch(name=name, args=args)
else:
body, hits = await _dispatch_tool(
db=db,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
reference_document_ids=reference_document_ids,
default_hierarchy_level=retrieval_level if retrieval_level in ("document", "section", "paragraph") else None,
peer_sections_default=peer_sections,
section_code=section_code,
pack=pack,
gate=gate,
name=name,
args=args,
)
all_hits.extend(hits)
messages.append({"role": "tool", "tool_call_id": tc.id, "content": body[:24_000]})
if name == "submit_inspection_section":
try:
parsed = json.loads(body)
except json.JSONDecodeError:
parsed = {}
if isinstance(parsed, dict) and parsed.get("ok") is True and gate.get("submit_payload"):
submit_payload = gate["submit_payload"]
break
if submit_payload is not None:
break
if round_i >= soft_deadline_round and submit_payload is None:
retrieve_calls = sum(
1
for t in trace
if isinstance(t, dict) and str(t.get("tool", "")).startswith("retrieve_")
)
if retrieve_calls >= 6:
messages.append(
{
"role": "user",
"content": (
"You have retrieved enough evidence. STOP retrieving. "
"Call submit_extraction_audit, then submit_section_plan, then "
"submit_inspection_section immediately using current evidence. "
"Partial evidence is acceptable; no-submit fallback is worse."
),
}
)
trace.append({"round": round_i, "note": "force_submit_deadline_nudge"})
# Risk + compliance heuristics (same as scripted agent) for stable API shapes
from app.agentic import agents as agents_mod
risk_agent = agents_mod.RiskAssessmentAgent()
comp_agent = agents_mod.StandardsComplianceAgent()
# `RiskAssessmentAgent.assess` is async (it can call OpenAI for contextual risk
# reasoning, falling back to keyword heuristics offline). Without the await,
# `risks` would be a coroutine and the downstream `tuple(risks)` would raise
# TypeError, crashing risk/compliance finalization for every section.
risks = await risk_agent.assess(section_code=section_code, bullets=bullets)
kb_glimpses = [r.text for r in all_hits if bool(getattr(r, "kb", False)) or r.tenant_id == settings.knowledge_base_tenant_id][:3]
comp = comp_agent.check(
section_code=section_code,
bullets=bullets,
kb_guidance=kb_glimpses or None,
survey_level=survey_level,
)
uniq_hits = agent_tools.dedupe_search_results(all_hits)
evidence_items = await _evidence_items_async(db, tenant_id, uniq_hits)
if submit_payload:
# Non-invention enforcement on EVERY LLM-emitted artifact.
#
# The five body fields (the user-visible report) get the full 2-layer
# guard (regex + contextual LLM grounding). The auxiliary fields the
# tool gates produced (extraction_audit, section_plan,
# condition_rating_summary) and LLM-drafted risks all go through the
# cheap synchronous regex pass. Without this, GPT-4o-mini routinely
# ships invented postcodes / surveyor names / firm references in the
# metadata bundle the API surfaces (`/agentic/...` returns the gate
# payloads in `inspector_meta`).
verify_snippets = [r.text for r in uniq_hits if getattr(r, "text", None)]
peer_values = [
v for v in (peer_sections or {}).values() if isinstance(v, str) and v.strip()
]
full_bullets = list(bullets) + peer_values
submit_payload = await _enforce_verify_submit_payload(
submit_payload,
bullets=bullets,
snippets=verify_snippets,
peer_sections=peer_sections,
pinned_identity=identity,
)
gate["audit_payload"] = _verify_audit_payload(
gate.get("audit_payload"),
bullets=full_bullets,
snippets=verify_snippets,
pinned_identity=identity,
)
gate["plan_payload"] = _verify_plan_payload(
gate.get("plan_payload"),
bullets=full_bullets,
snippets=verify_snippets,
pinned_identity=identity,
)
gate["condition_payload"] = _verify_condition_payload(
gate.get("condition_payload"),
bullets=full_bullets,
snippets=verify_snippets,
pinned_identity=identity,
)
risks = _verify_risks(
risks,
bullets=full_bullets,
snippets=verify_snippets,
pinned_identity=identity,
)
# ββ Level-1 behavioural enforcement (parity with the legacy LCEL path)
# The legacy `_run_generate` calls `_tier_validation_issues` and
# retries once when an L1 draft contains advice phrasing. The
# agentic inspector loop has no such retry β by the time we reach
# this point the LLM has already submitted, and a second tool-loop
# round-trip per section would double the cost. We instead apply a
# deterministic regex sanitiser that strips advice sentences from
# the five body fields and from L1 risk actions. This is the same
# sanitiser the legacy path can fall back to when the retry still
# returns advice, so behaviour matches across both pipelines.
if _seed_lvl <= 1:
submit_payload = strip_l1_advice_payload(submit_payload)
risks = [
RiskItem(
category=r.category,
risk=r.risk,
severity=r.severity,
likelihood=r.likelihood,
action=strip_l1_advice(r.action) if r.action else r.action,
evidence=r.evidence,
)
for r in risks
]
rep = StructuredReport(
executive_summary=_strip_missing_fact_phrase(submit_payload["executive_summary"] or ""),
property_description=_strip_missing_fact_phrase(submit_payload["property_description"] or ""),
condition_assessment=_strip_missing_fact_phrase(submit_payload["condition_assessment"] or ""),
defects_and_risks=_strip_missing_fact_phrase(submit_payload["defects_and_risks"] or ""),
recommendations=_strip_missing_fact_phrase(submit_payload["recommendations"] or ""),
risks=tuple(risks),
compliance=tuple(comp),
evidence_items=tuple(evidence_items),
tool_trace=tuple(trace),
extraction_audit=gate.get("audit_payload") if isinstance(gate.get("audit_payload"), dict) else None,
section_plan=gate.get("plan_payload") if isinstance(gate.get("plan_payload"), dict) else None,
condition_rating_summary=gate.get("condition_payload") if isinstance(gate.get("condition_payload"), dict) else None,
)
return rep, trace
# Fallback: single LCEL draft from accumulated evidence (no submit)
from app.llm import generation_facade as gen_llm
snippets = [r.text for r in uniq_hits[:12]]
base = await gen_llm.generate_section(
skeleton=template.skeleton if template else f"[{section_code}]: [content].",
bullets=bullets,
snippets=[],
style_profile=style_profile if ai_percent > 5 else None,
temperature=temperature,
creativity_hint=str(ai_params["creativity_hint"])
+ "\n\nAGENTIC FALLBACK: Tool loop did not call submit_inspection_section in time; draft from evidence.",
document_context=snippets[:4],
hierarchy_section_snippets=None,
paragraph_snippets=snippets[4:],
tenant_id=tenant_id,
style_anchor=None,
survey_level=survey_level,
)
# Same non-invention enforcement as the submit path. Fallback drafts skip
# the structured five-field tool, so they are arguably MORE prone to
# hallucination β guarding here is critical, not optional.
try:
base = await async_enforce_verify(
text=base,
bullets=bullets,
snippets=snippets,
pinned_identity=identity,
openai_api_key=settings.openai_api_key or "",
model=settings.chat_model,
)
except Exception as exc: # noqa: BLE001
logger.warning("Non-invention guard failed on fallback draft (%s); returning raw text", exc)
trace.append({"error": "submit_inspection_section not called", "fallback": "generate_section"})
# Apply the same regex guard to fallback risks for parity with the success
# path. (`base` itself was already passed through async_enforce_verify
# above; this catches inventions inside risk[*].risk / risk[*].action.)
risks = _verify_risks(
risks,
bullets=bullets,
snippets=snippets,
pinned_identity=identity,
)
# Level-1 behavioural enforcement on the fallback draft and on the
# risk[*].action strings β mirrors the success path's L1 sweep above.
if _seed_lvl <= 1:
base = strip_l1_advice(base)
risks = [
RiskItem(
category=r.category,
risk=r.risk,
severity=r.severity,
likelihood=r.likelihood,
action=strip_l1_advice(r.action) if r.action else r.action,
evidence=r.evidence,
)
for r in risks
]
# Tier-aware defects/recommendations construction. Building both fields
# by concatenating risk[*].risk + risk[*].action is the right shape for
# L2/L3 (HomeBuyer / Building Survey both produce explicit
# recommendations), but writes "Investigate source" / "Seek structural
# engineer review" into an L1 product β exactly the leakage the L1
# sanitiser above is meant to prevent. For L1 we surface defects as
# observation only and emit the L1 placeholder for recommendations.
if _seed_lvl <= 1:
defects_text = " ".join(
f"{r.category}: {r.risk} ({r.severity}/{r.likelihood})."
for r in risks
).strip()[:2400]
recommendations_text = _L1_PLACEHOLDER
else:
defects_text = " ".join(
f"{r.category}: {r.risk} ({r.severity}/{r.likelihood}). {r.action}"
for r in risks
).strip()[:2400]
recommendations_text = " ".join(r.action for r in risks).strip()[:1600]
# Previous behaviour duplicated `base[:900]` across both
# `property_description` and `condition_assessment`, and used `base[:400]`
# as a third copy in `executive_summary` β three views of the same
# truncated text, which is exactly why fallback output read like a
# one-paragraph summary repeated under three headings. We instead place
# the full LLM draft once, in `condition_assessment` (the spine of the
# L3 element renderer and the most substantive subhead for non-L3 codes
# in `render_report_text`), and leave `property_description` blank so
# the renderer skips it rather than printing a duplicate.
# Fallback path: the autonomous tool loop did not converge on a clean
# structured submit within max_rounds. We still have a usable LLM
# narrative draft (`base`) collected from retrieve_survey_rag snippets,
# so we hand THAT to the user under condition_assessment. The previous
# version of this branch leaked dev-quality language ("the autonomous
# tool loop did not complete a structured submit before the round
# limit") into executive_summary β surfacing internal pipeline state in
# the user-facing report. RAGAS evaluation flagged this as the largest
# qualitative defect on Section D in the 37 Elms Crescent run. The
# replacement copy below is short, professional, and indistinguishable
# from a clean submit at the user level; the underlying telemetry is
# still visible to operators via tool_trace and the pipeline metadata.
rep = StructuredReport(
executive_summary=(
f"{section_title}: this section summarises the surveyor's notes and the "
f"evidence drawn from the supporting documents. Key observations and any "
f"matters identified during the inspection are set out below."
).strip(),
property_description="",
condition_assessment=_strip_missing_fact_phrase(base.strip()),
defects_and_risks=_strip_missing_fact_phrase(defects_text),
recommendations=_strip_missing_fact_phrase(recommendations_text),
risks=tuple(risks),
compliance=tuple(comp),
evidence_items=tuple(evidence_items),
tool_trace=tuple(trace),
extraction_audit=None,
section_plan=None,
condition_rating_summary=None,
)
return rep, trace
|