Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23e19f53b2 | ||
|
|
46db871152 | ||
|
|
d37ac8c1c7 | ||
|
|
bae608a505 |
No files matched your search
@@ -0,0 +1,715 @@
|
|||||||
|
# Extraction Coverage Guarantee — Implementation Plan
|
||||||
|
|
||||||
|
> **For Hermes:** Use subagent-driven-development skill to implement this plan task-by-task.
|
||||||
|
|
||||||
|
**Goal:** Eliminate dark sheets (missed pages) and vision-misread content by making wave-1 extraction coverage-guaranteed: deterministic coverage measurement, a text-first retry ladder, deterministic fallback extraction, and text-layer sheet identity recovery.
|
||||||
|
|
||||||
|
**Architecture:** For text-bearing sheets the authoritative alphanumeric content already exists in the PyMuPDF text layer (backend/text_layer.py). Today the LLM transcribes from pixels and we merely *detect* failure post-hoc (coverage_gaps logs; nothing retries). This plan flips wave 1 to: run the vision pass (unchanged, always, on every page) → measure text coverage deterministically per page → if below floor, ADD a text-only structuring pass (LLM segments the text layer, no image, no misreads possible) and MERGE its objects into the vision results — vision keeps everything it found, text structuring fills what it missed → if still below floor, emit deterministic stub objects straight from the text layer so NO text-bearing page ever contributes zero objects. Sheet identity is recovered from the text layer when the LLM drops the header. Vision stays the only source for graphical content (symbols, geometry, line work) and the only path for scanned pages.
|
||||||
|
|
||||||
|
**Tech Stack:** Python 3.14, PyMuPDF (already a dep), existing call_json LLM plumbing, pytest.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Root-Cause Diagnosis (why this keeps happening)
|
||||||
|
|
||||||
|
Confirmed against Cypress job 3e01d5baba32 (38-page Verizon set) and code:
|
||||||
|
|
||||||
|
**Missed sheets (pages 8 = S202 wood notes, 10 = S204 lap-splice tables, 18 = A102 REFLECTED CEILING PLAN — zero assertions each):**
|
||||||
|
1. The extractor prompt (backend/prompts.py:277) is biased toward physical "construction objects" (rooms, doors, fixtures). Notes/table-dense sheets have few, so the model returns a bare array with ONE generic summary object (log: `wrapping bare objects array (1 items, no sheet header)`).
|
||||||
|
2. The grounding guard (backend/pipeline/extractor.py:178 `_is_grounded`) drops that summary object as ungrounded → 0 objects.
|
||||||
|
3. `_wrap_bare_list` (backend/agents/extractors.py:33) converts the 1-item bare array into a valid dict, so the compact retry (extractors.py:71) NEVER fires — it only triggers when parsing fully fails. A 1-object page counts as "success".
|
||||||
|
4. `coverage_gaps()` (backend/text_layer.py:211) only LOGS the gap and adds a failed_scope note. No retry, no fallback. The page is silently dark for every downstream wave.
|
||||||
|
5. Sheet identity comes ONLY from the LLM reading the title block in the image. 7/38 Cypress pages ended with `sheet_number=None` (4 of them WITH assertions: pages 22, 30, 31, 37), so they can't join sheet-keyed scopes and corrupt `missing_expected_sheets` downstream.
|
||||||
|
|
||||||
|
**Completely incorrect information:**
|
||||||
|
1. Vision misreads of dense alphanumeric content (the "(2) vs (5) 2x6 STUD PACK" family). The wave-1.5 rescue tier catches invented numbers but is a SET subset test — it cannot catch SWAPPED numbers (documented in docs/superpowers/specs/2026-08-12-text-layer-grounding-design.md).
|
||||||
|
2. Gemini thinking tokens count against max_tokens → `recovered truncated JSON` silently drops tail objects (bottom/right of sheet vanishes). Nothing flags the page as degraded.
|
||||||
|
3. The model paraphrases `source_text`; the guard only checks digit-run/token overlap, so plausible-but-wrong values pass.
|
||||||
|
4. `JSON parse error (giving up)` → classic path returns an empty "extraction failed" sheet (extractor.py:282-291); the page vanishes from analysis while `sheets_analyzed` still counts it.
|
||||||
|
|
||||||
|
**Cornerstone principles for the fix:**
|
||||||
|
1. If a page has a text layer, the truth is already deterministic and free. The LLM's job on such pages is STRUCTURING, not TRANSCRIPTION. Every extracted alphanumeric claim must trace to the text layer; anything that can't is vision-only and gets stamped as such.
|
||||||
|
2. The vision pass is never skipped and never replaced. These are construction documents: symbols, device/fixture locations, geometry, and line work exist only in the image. The text-only rung and the fallback rung are strictly ADDITIVE — they merge into the vision results (deduped by normalized source_text), so a rescue can only add coverage, never subtract graphical content.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1: Coverage metric module (backend/text_coverage.py)
|
||||||
|
|
||||||
|
**Objective:** Deterministic per-page coverage measurement: what fraction of the text layer is actually represented in extracted objects.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `backend/text_coverage.py`
|
||||||
|
- Test: `tests/test_text_coverage.py`
|
||||||
|
|
||||||
|
**Step 1: Write failing test**
|
||||||
|
|
||||||
|
```python
|
||||||
|
# tests/test_text_coverage.py
|
||||||
|
from backend.text_coverage import text_coverage, segment_text_layer, fallback_objects
|
||||||
|
|
||||||
|
def test_coverage_full():
|
||||||
|
text = "NOTE 1\nALL LUMBER NO. 2 SOUTHERN PINE\nNOTE 2\nUSE 5/8\" PLYWOOD"
|
||||||
|
objects = [{"source_text": "ALL LUMBER NO. 2 SOUTHERN PINE"},
|
||||||
|
{"source_text": "USE 5/8\" PLYWOOD"}]
|
||||||
|
cov = text_coverage(text, objects)
|
||||||
|
assert cov["covered_lines"] == 2
|
||||||
|
assert cov["total_lines"] == 2
|
||||||
|
assert cov["ratio"] == 1.0
|
||||||
|
|
||||||
|
def test_coverage_zero_on_empty_objects():
|
||||||
|
cov = text_coverage("LINE A\nLINE B\nLINE C", [])
|
||||||
|
assert cov["ratio"] == 0.0 and cov["total_lines"] == 3
|
||||||
|
|
||||||
|
def test_coverage_ignores_short_and_numeric_noise_lines():
|
||||||
|
text = "15\"\n19\"\nA\nB\nREAL NOTE ABOUT FRAMING HERE"
|
||||||
|
cov = text_coverage(text, [{"source_text": "REAL NOTE ABOUT FRAMING HERE"}])
|
||||||
|
# short/noise lines (< MIN_LINE_CHARS or pure dimension ticks) excluded
|
||||||
|
assert cov["total_lines"] == 1 and cov["ratio"] == 1.0
|
||||||
|
|
||||||
|
def test_segment_notes_and_rows():
|
||||||
|
text = "WOOD CONSTRUCTION\n1. \nALL SAWN LUMBER TO BE SOUTHERN PINE.\n2. \nROOF SHEATHING 5/8\" PLYWOOD."
|
||||||
|
segs = segment_text_layer(text)
|
||||||
|
assert any("ALL SAWN LUMBER" in s for s in segs)
|
||||||
|
assert any("ROOF SHEATHING" in s for s in segs)
|
||||||
|
|
||||||
|
def test_fallback_objects_verbatim_and_stamped():
|
||||||
|
objs = fallback_objects("1. \nALL SAWN LUMBER TO BE SOUTHERN PINE.", page_number=8)
|
||||||
|
assert len(objs) == 1
|
||||||
|
assert objs[0]["source_text"] == "ALL SAWN LUMBER TO BE SOUTHERN PINE."
|
||||||
|
assert objs[0]["grounding"] == "text_layer_fallback"
|
||||||
|
assert objs[0]["confidence"] == "low"
|
||||||
|
|
||||||
|
def test_merge_objects_keeps_vision_and_unions_text():
|
||||||
|
vision = [
|
||||||
|
{"source_text": "2X6 WD STUD @ 16\" O.C.", "object_type": "wall"},
|
||||||
|
{"source_text": None, "graphical_basis": "light fixture symbol, grid C-4",
|
||||||
|
"object_type": "lighting_fixture"}, # graphical: exists only in image
|
||||||
|
]
|
||||||
|
text = [
|
||||||
|
{"source_text": "2X6 WD STUD @ 16\" O.C.", "object_type": "wall"}, # dup
|
||||||
|
{"source_text": "ALL LUMBER NO. 2 SOUTHERN PINE", "object_type": "general_note"},
|
||||||
|
]
|
||||||
|
merged = merge_objects(vision, text)
|
||||||
|
assert len(merged) == 3 # dup dropped, note added
|
||||||
|
assert any(o.get("graphical_basis") for o in merged) # graphical kept
|
||||||
|
assert merged[0]["object_type"] == "wall" # vision order preserved
|
||||||
|
|
||||||
|
def test_merge_objects_dedupes_by_normalized_text():
|
||||||
|
a = [{"source_text": "RTU-1: 5 TON, 1600 CFM"}]
|
||||||
|
b = [{"source_text": "rtu 1 5 ton 1600 cfm"}] # same content, different case/punct
|
||||||
|
assert len(merge_objects(a, b)) == 1
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: Run test to verify failure**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_text_coverage.py -v`
|
||||||
|
Expected: FAIL — ModuleNotFoundError: backend.text_coverage
|
||||||
|
|
||||||
|
**Step 3: Implement `backend/text_coverage.py`**
|
||||||
|
|
||||||
|
```python
|
||||||
|
"""text_coverage.py - deterministic extraction-coverage measurement.
|
||||||
|
|
||||||
|
The coverage guarantee: for any page with a usable text layer, measure how
|
||||||
|
much of that layer ended up represented in extracted objects. Pages below
|
||||||
|
the floor route into the extraction retry ladder (agents/extractors.py and
|
||||||
|
pipeline/extractor.py). fallback_objects() is the last rung: stub objects
|
||||||
|
segmented straight from the text layer so no text-bearing page goes dark.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
# Lines below this many meaningful chars are noise (dimension ticks, grid
|
||||||
|
# bubbles, single letters) and excluded from the coverage denominator.
|
||||||
|
MIN_LINE_CHARS = 12
|
||||||
|
# Pure dimension/elevation ticks like 15" or 8' - 0" carry no prose content.
|
||||||
|
_TICK_RE = re.compile(r"^[\d\s'\"/.,-]+$")
|
||||||
|
_WORD_RE = re.compile(r"[a-z0-9]+")
|
||||||
|
|
||||||
|
|
||||||
|
def _meaningful_lines(text: str) -> List[str]:
|
||||||
|
lines = []
|
||||||
|
for raw in (text or "").splitlines():
|
||||||
|
line = " ".join(raw.split())
|
||||||
|
if len(line) < MIN_LINE_CHARS or _TICK_RE.match(line):
|
||||||
|
continue
|
||||||
|
lines.append(line)
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def _norm(text: str) -> str:
|
||||||
|
return " ".join(_WORD_RE.findall((text or "").lower()))
|
||||||
|
|
||||||
|
|
||||||
|
def text_coverage(page_text: str, objects: List[Dict]) -> Dict:
|
||||||
|
"""Fraction of meaningful text-layer lines whose normalized form appears
|
||||||
|
in the concatenated normalized source_text of extracted objects."""
|
||||||
|
lines = _meaningful_lines(page_text)
|
||||||
|
if not lines:
|
||||||
|
return {"total_lines": 0, "covered_lines": 0, "ratio": 1.0}
|
||||||
|
haystack = " ".join(
|
||||||
|
_norm(str(o.get("source_text") or o.get("object_description")
|
||||||
|
or o.get("value") or ""))
|
||||||
|
for o in objects if isinstance(o, dict)
|
||||||
|
)
|
||||||
|
covered = sum(1 for ln in lines if _norm(ln) and _norm(ln) in haystack)
|
||||||
|
return {
|
||||||
|
"total_lines": len(lines),
|
||||||
|
"covered_lines": covered,
|
||||||
|
"ratio": covered / len(lines) if lines else 1.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def segment_text_layer(text: str) -> List[str]:
|
||||||
|
"""Segment a page text layer into note-sized blocks: numbered notes and
|
||||||
|
contiguous prose runs. PyMuPDF emits each note number on its own line
|
||||||
|
('1. ', '2. ') followed by wrapped text lines; rejoin number->body and
|
||||||
|
merge continuation lines until the next number or blank-line break."""
|
||||||
|
segments: List[str] = []
|
||||||
|
buf: List[str] = []
|
||||||
|
number_re = re.compile(r"^(\d{1,2}[.)]?|[A-Z]\d{0,2}[.)]?)\s*$")
|
||||||
|
|
||||||
|
def flush():
|
||||||
|
joined = " ".join(buf).strip()
|
||||||
|
if len(joined) >= MIN_LINE_CHARS:
|
||||||
|
segments.append(joined)
|
||||||
|
buf.clear()
|
||||||
|
|
||||||
|
for raw in (text or "").splitlines():
|
||||||
|
line = raw.strip()
|
||||||
|
if not line:
|
||||||
|
flush()
|
||||||
|
continue
|
||||||
|
if number_re.match(line):
|
||||||
|
flush()
|
||||||
|
buf.append(line.rstrip(".)"))
|
||||||
|
continue
|
||||||
|
buf.append(line)
|
||||||
|
# wrapped-note heuristic: a line starting a new sentence after a
|
||||||
|
# period ends the segment
|
||||||
|
if line.endswith(".") and len(" ".join(buf)) > 120:
|
||||||
|
flush()
|
||||||
|
flush()
|
||||||
|
return segments
|
||||||
|
|
||||||
|
|
||||||
|
def fallback_objects(page_text: str, page_number: int,
|
||||||
|
max_objects: int = 200) -> List[Dict]:
|
||||||
|
"""Last-rung deterministic extraction: one stub object per text segment,
|
||||||
|
source_text verbatim from the text layer. confidence=low and
|
||||||
|
grounding=text_layer_fallback make their provenance explicit downstream."""
|
||||||
|
objs = []
|
||||||
|
for idx, seg in enumerate(segment_text_layer(page_text)[:max_objects]):
|
||||||
|
objs.append({
|
||||||
|
"object_id": f"p{page_number}-tl{idx}",
|
||||||
|
"object_type": "general_note",
|
||||||
|
"category": "general",
|
||||||
|
"tag": None,
|
||||||
|
"name": seg[:80],
|
||||||
|
"description": seg,
|
||||||
|
"attributes": {},
|
||||||
|
"location_key": {},
|
||||||
|
"source_text": seg,
|
||||||
|
"graphical_basis": None,
|
||||||
|
"review_uses": ["code_review", "constructability_review"],
|
||||||
|
"confidence": "low",
|
||||||
|
"grounding": "text_layer_fallback",
|
||||||
|
})
|
||||||
|
return objs
|
||||||
|
|
||||||
|
|
||||||
|
def merge_objects(vision_objs: List[Dict], text_objs: List[Dict]) -> List[Dict]:
|
||||||
|
"""Union of vision and text-structured objects. Vision results come first
|
||||||
|
and are never dropped (graphical_basis objects exist only in the image).
|
||||||
|
Text objects are appended unless their normalized source_text is already
|
||||||
|
represented. A merge can only add coverage, never subtract it."""
|
||||||
|
merged = list(vision_objs or [])
|
||||||
|
seen = {_norm(str(o.get("source_text") or ""))
|
||||||
|
for o in merged if isinstance(o, dict)}
|
||||||
|
seen.discard("")
|
||||||
|
for obj in text_objs or []:
|
||||||
|
if not isinstance(obj, dict):
|
||||||
|
continue
|
||||||
|
key = _norm(str(obj.get("source_text") or ""))
|
||||||
|
if key and key in seen:
|
||||||
|
continue
|
||||||
|
seen.add(key)
|
||||||
|
merged.append(obj)
|
||||||
|
return merged
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 4: Run test to verify pass**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_text_coverage.py -v`
|
||||||
|
Expected: 7 passed
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/text_coverage.py tests/test_text_coverage.py
|
||||||
|
git commit -m "feat: deterministic text-layer coverage metric + fallback extraction"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2: Sheet identity recovery from the text layer
|
||||||
|
|
||||||
|
**Objective:** When the LLM drops/misreads the sheet header, recover `sheet_number` (and discipline via existing `discipline_from_sheet_number`) deterministically from the text layer instead of leaving None.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/text_coverage.py` (add `recover_sheet_number`)
|
||||||
|
- Test: `tests/test_text_coverage.py` (add tests)
|
||||||
|
|
||||||
|
**Step 1: Write failing test**
|
||||||
|
|
||||||
|
```python
|
||||||
|
def test_recover_sheet_number_from_title_block():
|
||||||
|
text = ("WALL SECTIONS\n...\nSheet Information\nS301\n"
|
||||||
|
"Issue Date 05.29.26\nProject Number 25177")
|
||||||
|
assert recover_sheet_number(text) == "S301"
|
||||||
|
|
||||||
|
def test_recover_sheet_number_none_when_absent():
|
||||||
|
assert recover_sheet_number("just some notes about lumber") is None
|
||||||
|
|
||||||
|
def test_recover_prefers_discipline_pattern_over_dates():
|
||||||
|
# 05.29.26 and 25177 must never match
|
||||||
|
text = "Issue Date 05.29.26\nProject Number 25177\nA102 REFLECTED CEILING PLAN"
|
||||||
|
assert recover_sheet_number(text) == "A102"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: Run to verify failure**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_text_coverage.py::test_recover_sheet_number_from_title_block -v`
|
||||||
|
Expected: FAIL — ImportError
|
||||||
|
|
||||||
|
**Step 3: Implement in `backend/text_coverage.py`**
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Sheet ids: 1-2 uppercase letters + 2-3 digits + optional decimal suffix
|
||||||
|
# (S301, A102, M200, E500, LS101, P100, G000). Deliberately excludes pure
|
||||||
|
# numbers (dates, project numbers) and long alphanumerics (member marks).
|
||||||
|
_SHEET_ID_RE = re.compile(r"\b([A-Z]{1,2}\d{2,3}(?:\.\d+)?)\b")
|
||||||
|
_TITLE_HINT_RE = re.compile(
|
||||||
|
r"(?i)sheet\s*(?:information|no|number)?|"
|
||||||
|
r"(floor plan|ceiling plan|elevations?|sections?|details?|schedule|"
|
||||||
|
r"notes|legend|plan)")
|
||||||
|
|
||||||
|
def recover_sheet_number(page_text: str) -> Optional[str]:
|
||||||
|
"""Deterministic sheet id from the text layer. Strategy: collect every
|
||||||
|
sheet-id-shaped token, prefer ones appearing near title words or in the
|
||||||
|
last ~15%% of the page (title block lives at the drawing edge)."""
|
||||||
|
text = page_text or ""
|
||||||
|
cands = _SHEET_ID_RE.findall(text)
|
||||||
|
if not cands:
|
||||||
|
return None
|
||||||
|
tail = text[int(len(text) * 0.85):]
|
||||||
|
for cand in reversed(_SHEET_ID_RE.findall(tail)):
|
||||||
|
return cand
|
||||||
|
return cands[0]
|
||||||
|
```
|
||||||
|
|
||||||
|
(Add `from typing import Optional` to the imports.)
|
||||||
|
|
||||||
|
**Step 4: Run to verify pass**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_text_coverage.py -v`
|
||||||
|
Expected: all pass (10 tests)
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/text_coverage.py tests/test_text_coverage.py
|
||||||
|
git commit -m "feat: deterministic sheet-number recovery from text layer"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3: Text-only structuring prompt (no image)
|
||||||
|
|
||||||
|
**Objective:** Second rung of the ladder: give the LLM the raw text layer and ask it to segment EVERY note/row/callout into objects with verbatim source_text. No image = no vision misreads for alphanumerics; far cheaper than the vision pass.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/prompts.py` (append after EXTRACTOR_USER_INSTRUCTION, ~line 282)
|
||||||
|
- Test: `tests/agents/test_extraction_ladder.py` (prompt-content assertions only; rendering tested in Task 4)
|
||||||
|
|
||||||
|
**Step 1: Write failing test**
|
||||||
|
|
||||||
|
```python
|
||||||
|
# tests/agents/test_extraction_ladder.py
|
||||||
|
from backend.prompts import TEXT_STRUCTURING_SYSTEM_PROMPT, TEXT_STRUCTURING_USER_INSTRUCTION
|
||||||
|
|
||||||
|
def test_text_structuring_prompt_demands_verbatim_and_completeness():
|
||||||
|
assert "verbatim" in TEXT_STRUCTURING_USER_INSTRUCTION.lower()
|
||||||
|
assert "every" in TEXT_STRUCTURING_USER_INSTRUCTION.lower()
|
||||||
|
assert "{text_layer}" in TEXT_STRUCTURING_USER_INSTRUCTION
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 2: Run to verify failure**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/agents/test_extraction_ladder.py -v`
|
||||||
|
Expected: FAIL — ImportError
|
||||||
|
|
||||||
|
**Step 3: Append to `backend/prompts.py`**
|
||||||
|
|
||||||
|
```python
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Stage 2b - text-only structuring (extraction retry ladder, rung 2)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
TEXT_STRUCTURING_SYSTEM_PROMPT = """You are a construction document structuring engine.
|
||||||
|
You receive the deterministic text layer extracted from one drawing sheet. It is complete and authoritative.
|
||||||
|
Your ONLY job is to segment it into structured objects. You are NOT reading an image. You must NOT invent, complete, or correct any text.
|
||||||
|
Rules:
|
||||||
|
- Every numbered note, schedule row, callout, tag, legend entry, and title-block field becomes its own object.
|
||||||
|
- source_text must be copied VERBATIM from the input, character-for-character. Never paraphrase.
|
||||||
|
- Cover the ENTIRE input. Omitting a note is a failure. When unsure of an object's type, use general_note with confidence low.
|
||||||
|
- Numbers, model numbers, dimensions, and tags must appear in source_text exactly as in the input.
|
||||||
|
Respond only with valid JSON."""
|
||||||
|
|
||||||
|
TEXT_STRUCTURING_USER_INSTRUCTION = """Segment this sheet's text layer into structured construction objects.
|
||||||
|
Respond ONLY with a valid JSON object - no markdown fences:
|
||||||
|
{ "sheet": { "sheet_number": "string or null", "sheet_title": "string or null", "discipline": "string or null", "drawing_type": "string or null", "level": "string or null", "scale": "string or null" }, "objects": [ { "object_id": "string", "object_type": "room | door | window | wall | finish | ceiling | dimension | grid | callout | keynote | general_note | equipment | plumbing_fixture | mechanical_equipment | electrical_device | lighting_fixture | structural_element | schedule_reference | symbol | abbreviation", "category": "architectural | structural | mechanical | electrical | plumbing | code | general", "tag": "string or null", "name": "string or null", "description": "string or null", "attributes": { "attribute_name": "attribute_value" }, "location_key": { "room_number": "string or null", "grid": "string or null", "detail_reference": "string or null" }, "source_text": "VERBATIM text copied from the input", "graphical_basis": null, "review_uses": [ "schedule_comparison", "cross_discipline_coordination", "code_review", "constructability_review" ], "confidence": "high | medium | low" } ], "unresolved_items": [] }
|
||||||
|
Optional sheet hint: {sheet_hint}
|
||||||
|
|
||||||
|
TEXT LAYER (segment ALL of it):
|
||||||
|
{text_layer}"""
|
||||||
|
```
|
||||||
|
|
||||||
|
NOTE the two render sites you will add in Tasks 4-5 substitute `{sheet_hint}` and `{text_layer}` with str.replace directly (NOT via render()/call_stage) — this matches the wave-1.5 pattern and avoids the classic-path literal-placeholder leak documented in the project pitfalls.
|
||||||
|
|
||||||
|
**Step 4: Run to verify pass**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/agents/test_extraction_ladder.py -v`
|
||||||
|
Expected: 1 passed
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/prompts.py tests/agents/test_extraction_ladder.py
|
||||||
|
git commit -m "feat: text-only structuring prompt for extraction retry ladder"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4: Retry ladder in the AGENT path (SheetExtractorAgent)
|
||||||
|
|
||||||
|
**Objective:** Replace the binary parse-fail retry with a coverage-driven ladder: vision pass → coverage check → text-only structuring pass → deterministic fallback. Also recover sheet identity and mark truncation-degraded pages.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/agents/extractors.py:63-86` (SheetExtractorAgent.run)
|
||||||
|
- Modify: `backend/config.py` (new knobs, below)
|
||||||
|
- Test: `tests/agents/test_extraction_ladder.py`
|
||||||
|
|
||||||
|
**New config knobs (backend/config.py, follow existing env pattern):**
|
||||||
|
|
||||||
|
```python
|
||||||
|
EXTRACT_COVERAGE_FLOOR = float(os.getenv("EXTRACT_COVERAGE_FLOOR", "0.6"))
|
||||||
|
EXTRACT_TEXT_RETRY_ENABLED = os.getenv("EXTRACT_TEXT_RETRY_ENABLED", "true").lower() == "true"
|
||||||
|
EXTRACT_FALLBACK_ENABLED = os.getenv("EXTRACT_FALLBACK_ENABLED", "true").lower() == "true"
|
||||||
|
EXTRACT_FALLBACK_MAX_OBJECTS = int(os.getenv("EXTRACT_FALLBACK_MAX_OBJECTS", "200"))
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 1: Write failing test**
|
||||||
|
|
||||||
|
```python
|
||||||
|
from backend.agents.extractors import SheetExtractorAgent
|
||||||
|
from backend.agents.base import AgentScope, AgentUsage
|
||||||
|
|
||||||
|
def _page(n=8, text="1. \nALL SAWN LUMBER IN CONTACT WITH SOIL TO BE SOUTHERN PINE, PRESSURE TREATED.\n2. \nROOF SHEATHING: 5/8\" PLYWOOD, C-D GRADE, STRUCTURAL I."):
|
||||||
|
return {"page_number": n, "base64": "AAAA", "text_layer": text}
|
||||||
|
|
||||||
|
def test_ladder_falls_back_when_vision_returns_nothing(agent_monkeypatch):
|
||||||
|
# vision pass returns 1 summary object that the guard drops;
|
||||||
|
# text-structuring disabled to exercise the deterministic rung
|
||||||
|
agent_monkeypatch.setattr("backend.agents.extractors.call_json",
|
||||||
|
lambda **kw: [{"name": "general notes", "value": "notes"}])
|
||||||
|
agent_monkeypatch.setattr("backend.config.EXTRACT_TEXT_RETRY_ENABLED", False)
|
||||||
|
agent = SheetExtractorAgent(AgentUsage())
|
||||||
|
scope = AgentScope(scope_id="sheet:8", payload={"page": _page(), "sheet_hint": ""})
|
||||||
|
result = agent.run(scope)
|
||||||
|
sheet = result.artifacts[0]
|
||||||
|
assert sheet["assertions"], "dark sheet must be impossible with fallback enabled"
|
||||||
|
assert all(a.get("grounding") == "text_layer_fallback" for a in sheet["assertions"])
|
||||||
|
assert sheet["coverage"]["ratio"] >= 0.6
|
||||||
|
|
||||||
|
def test_ladder_merge_preserves_graphical_objects(agent_monkeypatch):
|
||||||
|
# vision finds a graphical symbol + misreads nothing; text rung adds notes.
|
||||||
|
# The graphical object MUST survive the merge.
|
||||||
|
calls = {"n": 0}
|
||||||
|
def fake_call_json(**kw):
|
||||||
|
calls["n"] += 1
|
||||||
|
if kw.get("images_b64"): # vision pass
|
||||||
|
return {"sheet": {}, "objects": [
|
||||||
|
{"object_id": "g1", "object_type": "lighting_fixture",
|
||||||
|
"name": "pendant at grid C-4", "source_text": None,
|
||||||
|
"graphical_basis": "16in pendant symbol at grid C-4"}]}
|
||||||
|
return {"sheet": {}, "objects": [ # text-structuring pass
|
||||||
|
{"object_id": "t1", "object_type": "general_note",
|
||||||
|
"source_text": "ALL SAWN LUMBER IN CONTACT WITH SOIL TO BE SOUTHERN PINE, PRESSURE TREATED.",
|
||||||
|
"name": "lumber note"}]}
|
||||||
|
agent_monkeypatch.setattr("backend.agents.extractors.call_json", fake_call_json)
|
||||||
|
agent = SheetExtractorAgent(AgentUsage())
|
||||||
|
scope = AgentScope(scope_id="sheet:8", payload={"page": _page(), "sheet_hint": ""})
|
||||||
|
sheet = agent.run(scope).artifacts[0]
|
||||||
|
assert any(a.get("graphical_basis") for a in sheet["assertions"])
|
||||||
|
assert any("SAWN LUMBER" in (a.get("source_text") or "") for a in sheet["assertions"])
|
||||||
|
|
||||||
|
def test_ladder_recovers_sheet_number_from_text_layer(agent_monkeypatch):
|
||||||
|
agent_monkeypatch.setattr(
|
||||||
|
"backend.agents.extractors.call_json",
|
||||||
|
lambda **kw: {"sheet": {}, "objects": [
|
||||||
|
{"object_id": "o1", "name": "RCP note",
|
||||||
|
"source_text": "GYP. BD. CEILING 8'-11 3/8\" A.F.F. TYP. FOR ALL STOREFRONT",
|
||||||
|
"attributes": {"height": "8'-11 3/8\""}}]})
|
||||||
|
agent = SheetExtractorAgent(AgentUsage())
|
||||||
|
scope = AgentScope(scope_id="sheet:18",
|
||||||
|
payload={"page": _page(18, "REFLECTED CEILING PLAN\nA102\nGYP. BD. CEILING 8'-11 3/8\" A.F.F. TYP. FOR ALL STOREFRONT"),
|
||||||
|
"sheet_hint": ""})
|
||||||
|
sheet = agent.run(scope).artifacts[0]
|
||||||
|
assert sheet["sheet_number"] == "A102"
|
||||||
|
```
|
||||||
|
|
||||||
|
(Monkeypatch fixture: plain `unittest.mock.patch` context or pytest `monkeypatch`; follow tests/agents/test_text_layer_flow.py patterns for scope/result construction — check AgentScope/AgentResult signatures in backend/agents/base.py before writing.)
|
||||||
|
|
||||||
|
**Step 2: Run to verify failure**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/agents/test_extraction_ladder.py -v`
|
||||||
|
Expected: FAIL — assertions on coverage/sheet_number fail (ladder not implemented)
|
||||||
|
|
||||||
|
**Step 3: Implement the ladder in `backend/agents/extractors.py`**
|
||||||
|
|
||||||
|
Replace `SheetExtractorAgent.run` (lines 63-86) with:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def _text_structuring_call(self, page, sheet_hint):
|
||||||
|
from backend.prompts import (TEXT_STRUCTURING_SYSTEM_PROMPT,
|
||||||
|
TEXT_STRUCTURING_USER_INSTRUCTION)
|
||||||
|
instruction = (TEXT_STRUCTURING_USER_INSTRUCTION
|
||||||
|
.replace("{sheet_hint}", str(sheet_hint or ""))
|
||||||
|
.replace("{text_layer}",
|
||||||
|
(page.get("text_layer") or "")
|
||||||
|
[:config.TEXT_LAYER_MAX_CHARS]))
|
||||||
|
return call_json(
|
||||||
|
system_prompt=TEXT_STRUCTURING_SYSTEM_PROMPT,
|
||||||
|
user_text=instruction,
|
||||||
|
images_b64=None,
|
||||||
|
max_tokens=config.EXTRACT_MAX_TOKENS,
|
||||||
|
model=config.AGENT_EXTRACT_MODEL,
|
||||||
|
usage_tracker=self.usage,
|
||||||
|
usage_stage="agent.extract_text",
|
||||||
|
reasoning_effort=config.EXTRACT_REASONING_EFFORT or None,
|
||||||
|
reasoning_max_tokens=config.EXTRACT_REASONING_MAX_TOKENS or None,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, scope: AgentScope) -> AgentResult:
|
||||||
|
from backend.text_coverage import (fallback_objects, merge_objects,
|
||||||
|
recover_sheet_number, text_coverage)
|
||||||
|
try:
|
||||||
|
page = scope.payload["page"]
|
||||||
|
hint = scope.payload.get("sheet_hint") or ""
|
||||||
|
page_text = page.get("text_layer")
|
||||||
|
instruction = EXTRACTOR_USER_INSTRUCTION.replace(
|
||||||
|
"{sheet_hint}", str(hint)) + _text_layer_block(page)
|
||||||
|
|
||||||
|
# Rung 1: vision pass (unchanged behaviour, incl. compact retry)
|
||||||
|
parsed = _wrap_bare_list(self._call(instruction, page),
|
||||||
|
page["page_number"])
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
print(f"[Extract] Page {page['page_number']}: full extraction "
|
||||||
|
f"failed, retrying compact")
|
||||||
|
parsed = _wrap_bare_list(
|
||||||
|
self._call(instruction + _COMPACT_RETRY_SUFFIX, page),
|
||||||
|
page["page_number"])
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
parsed = {"sheet": {}, "objects": []}
|
||||||
|
|
||||||
|
sheet = _normalize_sheet(parsed, page["page_number"],
|
||||||
|
page_text=page_text)
|
||||||
|
cov = text_coverage(page_text or "", sheet["assertions"])
|
||||||
|
sheet["coverage"] = cov
|
||||||
|
|
||||||
|
# Rung 2: text-only structuring when coverage is below floor.
|
||||||
|
# MERGE, never replace: vision keeps every object it found
|
||||||
|
# (graphical_basis content exists only in the image); the text
|
||||||
|
# pass fills in the text content the vision pass missed.
|
||||||
|
if (page_text and config.EXTRACT_TEXT_RETRY_ENABLED
|
||||||
|
and cov["ratio"] < config.EXTRACT_COVERAGE_FLOOR):
|
||||||
|
print(f"[Extract] Page {page['page_number']}: coverage "
|
||||||
|
f"{cov['ratio']:.0%} < floor - text-only structuring pass")
|
||||||
|
parsed2 = _wrap_bare_list(
|
||||||
|
self._text_structuring_call(page, hint), page["page_number"])
|
||||||
|
if isinstance(parsed2, dict):
|
||||||
|
sheet2 = _normalize_sheet(parsed2, page["page_number"],
|
||||||
|
page_text=page_text)
|
||||||
|
before = len(sheet["assertions"])
|
||||||
|
sheet["assertions"] = merge_objects(sheet["assertions"],
|
||||||
|
sheet2["assertions"])
|
||||||
|
# Fill header gaps the vision pass left null
|
||||||
|
for key in ("sheet_number", "sheet_title", "discipline",
|
||||||
|
"level", "scale", "drawing_type"):
|
||||||
|
if not sheet.get(key) and sheet2.get(key):
|
||||||
|
sheet[key] = sheet2[key]
|
||||||
|
cov = text_coverage(page_text, sheet["assertions"])
|
||||||
|
sheet["coverage"] = cov
|
||||||
|
print(f"[Extract] Page {page['page_number']}: merged "
|
||||||
|
f"{len(sheet['assertions']) - before} text-structured "
|
||||||
|
f"object(s), coverage now {cov['ratio']:.0%}")
|
||||||
|
|
||||||
|
# Rung 3: deterministic fallback - dark sheets are impossible.
|
||||||
|
# Also merged (deduped) so stub notes never double up with
|
||||||
|
# objects the earlier rungs already captured.
|
||||||
|
if (page_text and config.EXTRACT_FALLBACK_ENABLED
|
||||||
|
and cov["ratio"] < config.EXTRACT_COVERAGE_FLOOR):
|
||||||
|
stubs = fallback_objects(page_text, page["page_number"],
|
||||||
|
config.EXTRACT_FALLBACK_MAX_OBJECTS)
|
||||||
|
stubs = _normalize_sheet({"sheet": {}, "objects": stubs},
|
||||||
|
page["page_number"],
|
||||||
|
page_text=page_text)["assertions"]
|
||||||
|
before = len(sheet["assertions"])
|
||||||
|
sheet["assertions"] = merge_objects(sheet["assertions"], stubs)
|
||||||
|
print(f"[Extract] Page {page['page_number']}: fallback merged "
|
||||||
|
f"{len(sheet['assertions']) - before} text-layer stub(s)")
|
||||||
|
sheet["coverage"] = text_coverage(page_text,
|
||||||
|
sheet["assertions"])
|
||||||
|
|
||||||
|
# Identity recovery: never leave a text-bearing page sheet-less
|
||||||
|
if not sheet.get("sheet_number") and page_text:
|
||||||
|
recovered = recover_sheet_number(page_text)
|
||||||
|
if recovered:
|
||||||
|
sheet["sheet_number"] = recovered
|
||||||
|
sheet["discipline"] = (
|
||||||
|
__import__("backend.pipeline.extractor",
|
||||||
|
fromlist=["discipline_from_sheet_number"])
|
||||||
|
.discipline_from_sheet_number(recovered)
|
||||||
|
or sheet.get("discipline") or "Unknown")
|
||||||
|
print(f"[Extract] Page {page['page_number']}: sheet number "
|
||||||
|
f"recovered from text layer -> {recovered}")
|
||||||
|
|
||||||
|
return AgentResult(scope_id=scope.scope_id, artifacts=[sheet])
|
||||||
|
except Exception as exc:
|
||||||
|
return failure(scope, exc)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Step 4: Run to verify pass**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/agents/test_extraction_ladder.py -v`
|
||||||
|
Expected: all pass
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/agents/extractors.py backend/config.py tests/agents/test_extraction_ladder.py
|
||||||
|
git commit -m "feat: coverage-driven extraction retry ladder (agent path)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5: Same ladder in the CLASSIC path (pipeline/extractor.py)
|
||||||
|
|
||||||
|
**Objective:** The classic pipeline (`_extract_one`, backend/pipeline/extractor.py:273-293) must get the identical ladder — two render paths share everything, per the documented project trap.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/pipeline/extractor.py:273-293`
|
||||||
|
- Test: `tests/test_text_layer_flow.py` or new `tests/test_extraction_ladder_classic.py`
|
||||||
|
|
||||||
|
**Step 1: Write failing test** — mirror Task 4's tests against `_extract_one` directly (monkeypatch `backend.pipeline.extractor.call_json`).
|
||||||
|
|
||||||
|
**Step 2: Run to verify failure**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_extraction_ladder_classic.py -v`
|
||||||
|
Expected: FAIL
|
||||||
|
|
||||||
|
**Step 3: Implement** — same ladder shape as Task 4 but inside `_extract_one`; the text-structuring call here uses default model (no `model=` kwarg, matching existing `_extract_one` call_json usage). Keep the existing "extraction failed" empty-sheet shape for pages with NO text layer (scanned pages stay vision-only and may legitimately return empty).
|
||||||
|
|
||||||
|
**Step 4: Run to verify pass**
|
||||||
|
|
||||||
|
Run: `.venv/bin/python -m pytest tests/test_extraction_ladder_classic.py -v`
|
||||||
|
Expected: all pass
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/pipeline/extractor.py tests/test_extraction_ladder_classic.py
|
||||||
|
git commit -m "feat: coverage-driven extraction retry ladder (classic path)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 6: Verbatim-source stamping upgrade
|
||||||
|
|
||||||
|
**Objective:** When a text layer exists, check each object's source_text against the page text with the existing fuzzy machinery; stamp `grounding="vision_unverified"` when it doesn't match so the wave-5b verifier prioritizes it. Cheap upgrade, reuses text_layer._tokens — no new call sites.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/pipeline/extractor.py` (`_normalize_sheet`, ~line 182 where `grounding` is stamped)
|
||||||
|
- Test: extend `tests/test_extractor_text_grounding.py`
|
||||||
|
|
||||||
|
**Step 1: Failing test** — object whose source_text is NOT a fuzzy substring of the page text keeps the object (guard passes via digits) but gets stamped `vision_unverified`.
|
||||||
|
|
||||||
|
**Step 2:** Run, expect FAIL.
|
||||||
|
|
||||||
|
**Step 3: Implement** — in `_normalize_sheet`, when `page_text` is present and no `grounding` stamp yet: normalized source_text (via `backend.text_coverage._norm`) not substring of normalized page text → `grounding = "vision_unverified"` (counted in the existing log line as a third counter).
|
||||||
|
|
||||||
|
**Step 4:** Run, expect PASS.
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/pipeline/extractor.py tests/test_extractor_text_grounding.py
|
||||||
|
git commit -m "feat: stamp vision-unverified source_text against text layer"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 7: Surface coverage in the report
|
||||||
|
|
||||||
|
**Objective:** `report.summary` gains per-job extraction-quality visibility so "is extraction healthy?" is answerable without log spelunking.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `backend/agents/runner.py` (where summary is assembled) and/or `backend/pipeline/report.py`
|
||||||
|
- Test: extend the runner-level stub test (tests/agents/test_wave5b_suppression.py pattern)
|
||||||
|
|
||||||
|
**Step 1: Failing test** — runner-level: summary contains `extraction_coverage = {"pages_below_floor": [...], "mean_ratio": float, "fallback_pages": [...]}`.
|
||||||
|
|
||||||
|
**Step 2-4:** Implement by aggregating the `coverage` dicts Task 4/5 attach to each sheet; NO new ProjectMemory keys (closed registry trap) — compute at report assembly from the sheets list already in scope.
|
||||||
|
|
||||||
|
**Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add backend/agents/runner.py backend/pipeline/report.py tests/
|
||||||
|
git commit -m "feat: extraction coverage summary in report"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 8: Full suite + Cypress validation run
|
||||||
|
|
||||||
|
**Step 1:** `.venv/bin/python -m pytest tests/ -q` — expected: all pass (128 + new).
|
||||||
|
|
||||||
|
**Step 2:** Push branch, wait for Gitea Actions sha-<short> build, deploy per the skill's deploy runbook (compose pull + up -d --force-recreate).
|
||||||
|
|
||||||
|
**Step 3:** Resubmit the exact Cypress PDF (`docker cp`'d source.pdf preserved at /tmp/cypress-source.pdf on sits-docker):
|
||||||
|
`curl -F file=@source.pdf -F pipeline_mode=agent https://conchecker.scoutitsystems.com/check`
|
||||||
|
|
||||||
|
**Step 4: Acceptance criteria (compare against job 3e01d5baba32):**
|
||||||
|
- Zero text-bearing pages with 0 assertions (was: pages 8, 10, 18).
|
||||||
|
- `sheet_number` present on >= 37/38 pages (was: 31/38).
|
||||||
|
- A102 RCP content (ceiling heights, tape lights, sconces) present in assertions.
|
||||||
|
- Spot-check: no regression in validated-issue quality — suppressed_issues and validated_issues counts within noise of the prior run; cost delta reported (expect +1 cheap text-only call per low-coverage page, ~$0 on healthy pages).
|
||||||
|
- `report.summary.extraction_coverage.pages_below_floor` is empty or every entry is a genuinely scanned page.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Files Touched (summary)
|
||||||
|
|
||||||
|
- Create: `backend/text_coverage.py`
|
||||||
|
- Modify: `backend/prompts.py`, `backend/config.py`, `backend/agents/extractors.py`, `backend/pipeline/extractor.py`, `backend/agents/runner.py`, `backend/pipeline/report.py`
|
||||||
|
- Tests: `tests/test_text_coverage.py`, `tests/agents/test_extraction_ladder.py`, `tests/test_extraction_ladder_classic.py`, extensions to `tests/test_extractor_text_grounding.py` and the runner-level stub test
|
||||||
|
|
||||||
|
## Risks, Tradeoffs, Open Questions
|
||||||
|
|
||||||
|
- **Fallback flood risk:** 200 low-confidence stubs/page could flood downstream scopes. Mitigations: EXTRACT_FALLBACK_MAX_OBJECTS cap, confidence=low (specialists already weight confidence), fallback only fires below the coverage floor (3/38 pages on Cypress). If Brain merge gets noisy, lower the cap or restrict fallback to pages where rungs 1+2 BOTH return 0 objects.
|
||||||
|
- **Cost:** rung 2 adds one text-only call per low-coverage page (~12k input chars, no image) — negligible vs the 65k-token vision pass. Healthy pages skip it entirely.
|
||||||
|
- **Sheet-id regex false positives:** member marks like W12X26 are excluded by the 2-3-digit shape, but "S301" inside a detail reference ("2/S301") will match. Tail-of-page preference mitigates; wrong-but-present sheet_number is still strictly better than None for scope keying (sheet_index wave can correct it).
|
||||||
|
- **Open question:** should rung 2 route to the local model (aimax LM Studio) instead of the cloud extractor model to make retries free? Config knob `AGENT_EXTRACT_TEXT_MODEL` would allow it; not included in this plan (YAGNI until cost data from the validation run says otherwise).
|
||||||
|
- **Explicit non-goal:** graphical-only content (symbol geometry, line work) stays vision-based — text-layer-first cannot see it. Scanned PDFs (no text layer) keep today's behaviour plus the existing failed_scopes gap note.
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
# Plan: Brain-directed clarification pass (bounded hub-and-spoke)
|
||||||
|
|
||||||
|
Date: 2026-08-20
|
||||||
|
Branch: agent-mode
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
Let the Brain actively chase weak/ambiguous findings instead of only judging
|
||||||
|
the finished pile once. Bounded, traceable, reuses the wave-5b verifier as the
|
||||||
|
"answer" channel. NOT a free agentic loop.
|
||||||
|
|
||||||
|
## Shape (agent pipeline)
|
||||||
|
Insert **wave 6.5: Brain-directed clarification** between the wave-6 Brain merge
|
||||||
|
and the review-gate / wave-7 branches, so BOTH paths benefit.
|
||||||
|
|
||||||
|
1. `BrainAgent.plan_clarifications(prioritized)` — one focused LLM call. Brain
|
||||||
|
names findings it is unsure about and emits TYPED requests:
|
||||||
|
`{issue_id, request_type, reason}`. v1 executes only `verify_evidence`;
|
||||||
|
the router accepts other types but logs them as "planned, not executed"
|
||||||
|
(extensible without a rewrite). Capped at `BRAIN_CLARIFY_MAX_REQUESTS`.
|
||||||
|
Brain is told which findings already carry `verification` (from 5b) so it
|
||||||
|
does not re-request them.
|
||||||
|
2. Route `verify_evidence` requests → build verify scopes for exactly those
|
||||||
|
findings (reuse the SAME scope builder as wave 5b: fresh page images +
|
||||||
|
hi-DPI evidence crops + text-layer oracle) → `EvidenceVerifierAgent` →
|
||||||
|
`apply_verdicts(prioritized, ...)`. Refuted findings are annotated,
|
||||||
|
demoted, removed from `prioritized`, and pushed into `memory["suppressed"]`
|
||||||
|
(existing key — no memory-registry crash). Clarify decisions recorded in
|
||||||
|
`memory["decisions"]`.
|
||||||
|
3. No second full Brain merge: Brain ASKED (step 1) and the verifier ANSWERED
|
||||||
|
(step 2); the answer prunes/annotates the list. This keeps issue_ids stable
|
||||||
|
for the review queue and adds at most 1 + N calls. One iteration only.
|
||||||
|
|
||||||
|
## Bounds / knobs (config.py, all env-overridable)
|
||||||
|
- `ENABLE_BRAIN_CLARIFY` (_flag, default true)
|
||||||
|
- `BRAIN_CLARIFY_MAX_REQUESTS` (default 8)
|
||||||
|
- reuse `AGENT_VERIFY_CONCURRENCY`, `VERIFY_MAX_TOKENS`,
|
||||||
|
`AGENT_VERIFY_REASONING_EFFORT`, `AGENT_CONFLICT_MAX_IMAGES`,
|
||||||
|
`VERIFY_HI_DPI_CROPS`.
|
||||||
|
|
||||||
|
## Reuse / refactor
|
||||||
|
- Extract the inline wave-5b verify-scope construction into
|
||||||
|
`_build_verify_scopes(findings, targets, sheet_to_page, page_to_b64,
|
||||||
|
page_to_text, page_words, pdf_path, prefix)` so wave 5b and wave 6.5 share
|
||||||
|
it. Preserve wave-5b behavior exactly (its tests guard this).
|
||||||
|
|
||||||
|
## Classic pipeline
|
||||||
|
Out of scope for v1 — the verifier/crops live only in the agent path. Classic
|
||||||
|
keeps its single dedup_validate. Documented as agent-only.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
- `plan_clarifications` parses/caps/skips-already-verified (stub call_json).
|
||||||
|
- Router executes verify_evidence, ignores unknown types.
|
||||||
|
- Runner smoke: a low-confidence finding Brain flags gets refuted → moves to
|
||||||
|
suppressed_issues; stub verifier.call_json (no live calls).
|
||||||
|
|
||||||
|
## Pitfalls to respect
|
||||||
|
- ProjectMemory keys are a closed registry — only use existing `suppressed` /
|
||||||
|
`decisions`. (Skill defect C1.)
|
||||||
|
- Stub `backend.agents.verifier.call_json` in runner tests or it hits the net.
|
||||||
|
- Extractor stub sheets need >= 2 assertions or no clusters form.
|
||||||
@@ -212,10 +212,54 @@ From the CLI, `--no-review` bypasses the gate for that run (it overrides
|
|||||||
python cli/run_check.py samples/your_set.pdf --mode agent --no-review --out out/agent-run
|
python cli/run_check.py samples/your_set.pdf --mode agent --no-review --out out/agent-run
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Asking the run why: review chat
|
||||||
|
|
||||||
|
Each item on the review screen has an **Ask about this finding** panel, and the
|
||||||
|
screen carries one **Ask about this run** panel for questions that are not about
|
||||||
|
a single finding. The chat answers from the job's own artifacts — the finding's
|
||||||
|
evidence, the cluster it came from, the raw per-sheet extraction, the
|
||||||
|
verification verdict, the Brain's merge decision, the sheet index, the cover-index
|
||||||
|
reconciliation, and matching `job.log` lines.
|
||||||
|
|
||||||
|
```
|
||||||
|
"why does it think the AC unit is mounted on the ground?" -> item scope
|
||||||
|
"why didn't it pick up on the Civil set?" -> run scope
|
||||||
|
```
|
||||||
|
|
||||||
|
The chat is **read-only**. It cannot change a finding, a severity, a decision,
|
||||||
|
or the report, and the prompt forbids it from proposing code or config changes —
|
||||||
|
the radio buttons remain the only thing that alters review state. When the
|
||||||
|
artifacts do not contain the answer, it says so and names what is missing rather
|
||||||
|
than guessing.
|
||||||
|
|
||||||
|
Every turn is logged twice:
|
||||||
|
|
||||||
|
- `outputs/<job_id>/review/chat_log.jsonl` — the auditable record: the issue as
|
||||||
|
it stood when asked about, the question, the answer, the determinations, and
|
||||||
|
the evidence quoted. Readable as a transcript at
|
||||||
|
`GET /jobs/{id}/review-chat/log`.
|
||||||
|
- `REVIEW_FEEDBACK_DIR/chat_turns.jsonl` — the cross-job roll-up, alongside
|
||||||
|
`decisions.jsonl`. When a reviewer corrects a misidentification in
|
||||||
|
conversation ("that is not a floor drain, it is a power floor box"), the
|
||||||
|
correction is captured as `suggested_category_correction` rather than dying in
|
||||||
|
free text. Nothing reads this store yet; writing it is what makes priming a
|
||||||
|
future run on past corrections possible.
|
||||||
|
|
||||||
|
| Key | Default | Effect |
|
||||||
|
|-----|---------|--------|
|
||||||
|
| `ENABLE_REVIEW_CHAT` | `true` | `false` = the chat endpoints refuse and the panels stay empty |
|
||||||
|
| `REVIEW_CHAT_MODEL` | `TEXT_MODEL` | Model for chat answers |
|
||||||
|
| `REVIEW_CHAT_MAX_TOKENS` | `4096` | Answer budget |
|
||||||
|
| `REVIEW_CHAT_HISTORY_TURNS` | `6` | Prior turns replayed into a thread's prompt |
|
||||||
|
| `REVIEW_CHAT_LOG_LINES` | `40` | Max `job.log` lines pulled into the context bundle |
|
||||||
|
| `REVIEW_FEEDBACK_DIR` | `backend/outputs/_feedback` | Cross-job decision + chat feedback store |
|
||||||
|
|
||||||
**Deployment note:** the review endpoints (`/jobs/{id}/review-decisions`,
|
**Deployment note:** the review endpoints (`/jobs/{id}/review-decisions`,
|
||||||
`/jobs/{id}/finalize-review`) are **state-changing and sensitive** — they accept
|
`/jobs/{id}/finalize-review`) are **state-changing and sensitive** — they accept
|
||||||
human decisions that alter the final report. Do **not** expose the UI/API
|
human decisions that alter the final report. `/jobs/{id}/review-chat` does not
|
||||||
publicly without reverse-proxy auth or a shared access token in front of it.
|
change review state, but it does spend model budget and returns drawing
|
||||||
|
evidence. Do **not** expose the UI/API publicly without reverse-proxy auth or a
|
||||||
|
shared access token in front of it.
|
||||||
|
|
||||||
Web UI (upload + view):
|
Web UI (upload + view):
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,23 @@ AGENT_CONFLICT_CONCURRENCY=4
|
|||||||
AGENT_SPECIALIST_CONCURRENCY=4
|
AGENT_SPECIALIST_CONCURRENCY=4
|
||||||
AGENT_RFI_CONCURRENCY=4
|
AGENT_RFI_CONCURRENCY=4
|
||||||
|
|
||||||
|
# -- Review focus toggles -------------------------------------------
|
||||||
|
# ENABLE_CODE_REVIEW: run the code/ADA/jurisdiction review path (both pipelines).
|
||||||
|
# Default OFF - the product focuses on drawing integrity and cross-discipline
|
||||||
|
# coordination, not code/accessibility compliance. Set to 1 to restore it.
|
||||||
|
ENABLE_CODE_REVIEW=false
|
||||||
|
# ENABLE_DRAWING_INTEGRITY: per-sheet Drawing Integrity QA wave (both pipelines).
|
||||||
|
# The drawing-focused pass - dangling references, on-sheet contradictions,
|
||||||
|
# dimension sanity, missing sheet essentials, tag hygiene. Default ON.
|
||||||
|
ENABLE_DRAWING_INTEGRITY=true
|
||||||
|
AGENT_INTEGRITY_MODEL=
|
||||||
|
AGENT_INTEGRITY_CONCURRENCY=4
|
||||||
|
AGENT_INTEGRITY_MAX_IMAGES=1
|
||||||
|
AGENT_INTEGRITY_MAX_ASSERTIONS=80
|
||||||
|
INTEGRITY_MAX_TOKENS=16384
|
||||||
|
# Skip sheets with fewer than this many extracted objects (too sparse to check)
|
||||||
|
INTEGRITY_MIN_ASSERTIONS=3
|
||||||
|
|
||||||
# Agent-mode human-review gate (pipeline stops after Brain until a human reviews)
|
# Agent-mode human-review gate (pipeline stops after Brain until a human reviews)
|
||||||
AGENT_REQUIRE_REVIEW=true
|
AGENT_REQUIRE_REVIEW=true
|
||||||
# Max clean clusters added to the review queue as non-blocking spot-checks
|
# Max clean clusters added to the review queue as non-blocking spot-checks
|
||||||
@@ -34,6 +51,21 @@ AGENT_REVIEW_AUDIT_SAMPLE=5
|
|||||||
# Allow future cross-job review-feedback aggregation to include source_text/images/comments
|
# Allow future cross-job review-feedback aggregation to include source_text/images/comments
|
||||||
REVIEW_AGGREGATE_INCLUDE_TEXT=false
|
REVIEW_AGGREGATE_INCLUDE_TEXT=false
|
||||||
|
|
||||||
|
# Review chat: read-only Q&A about findings and coverage on the review screen.
|
||||||
|
# It explains what the run did from the job's artifacts; it never changes a
|
||||||
|
# finding, a decision, or the report.
|
||||||
|
ENABLE_REVIEW_CHAT=true
|
||||||
|
# Model for chat answers (blank inherits TEXT_MODEL)
|
||||||
|
REVIEW_CHAT_MODEL=
|
||||||
|
REVIEW_CHAT_MAX_TOKENS=4096
|
||||||
|
# Prior turns replayed into a thread's prompt
|
||||||
|
REVIEW_CHAT_HISTORY_TURNS=6
|
||||||
|
# Max job.log lines searched into the chat's context bundle
|
||||||
|
REVIEW_CHAT_LOG_LINES=40
|
||||||
|
REVIEW_CHAT_MAX_QUESTION_CHARS=2000
|
||||||
|
# Cross-job store for review decisions + chat turns (blank = backend/outputs/_feedback)
|
||||||
|
REVIEW_FEEDBACK_DIR=
|
||||||
|
|
||||||
# Pipeline tuning
|
# Pipeline tuning
|
||||||
PDF_DPI=100
|
PDF_DPI=100
|
||||||
MAX_PAGES=60
|
MAX_PAGES=60
|
||||||
@@ -79,6 +111,14 @@ AGENT_VERIFY_SEVERITIES=critical,high
|
|||||||
AGENT_VERIFY_REASONING_EFFORT=low
|
AGENT_VERIFY_REASONING_EFFORT=low
|
||||||
VERIFY_MAX_TOKENS=8192
|
VERIFY_MAX_TOKENS=8192
|
||||||
|
|
||||||
|
# Wave 6.5 Brain-directed clarification (bounded hub-and-spoke). After the Brain
|
||||||
|
# merge, the Brain names findings it is unsure about; verify_evidence requests
|
||||||
|
# route back through the wave-5b verifier. One planning call + at most
|
||||||
|
# BRAIN_CLARIFY_MAX_REQUESTS verifications, single iteration. Default ON.
|
||||||
|
ENABLE_BRAIN_CLARIFY=true
|
||||||
|
BRAIN_CLARIFY_MAX_REQUESTS=8
|
||||||
|
BRAIN_CLARIFY_MAX_TOKENS=4096
|
||||||
|
|
||||||
# Text-layer grounding (deterministic PDF text layer via PyMuPDF)
|
# Text-layer grounding (deterministic PDF text layer via PyMuPDF)
|
||||||
# TEXT_LAYER_ENABLED: master switch for text-layer extraction/grounding
|
# TEXT_LAYER_ENABLED: master switch for text-layer extraction/grounding
|
||||||
# TEXT_LAYER_MIN_CHARS: below this per page the sheet stays vision-only
|
# TEXT_LAYER_MIN_CHARS: below this per page the sheet stays vision-only
|
||||||
|
|||||||
+72
-1
@@ -6,7 +6,12 @@ from typing import Dict, List, Tuple
|
|||||||
|
|
||||||
from backend import config
|
from backend import config
|
||||||
from backend.agents.base import AgentUsage
|
from backend.agents.base import AgentUsage
|
||||||
from backend.agents.prompts import BRAIN_SYSTEM_PROMPT, BRAIN_USER_PROMPT
|
from backend.agents.prompts import (
|
||||||
|
BRAIN_CLARIFY_SYSTEM_PROMPT,
|
||||||
|
BRAIN_CLARIFY_USER_PROMPT,
|
||||||
|
BRAIN_SYSTEM_PROMPT,
|
||||||
|
BRAIN_USER_PROMPT,
|
||||||
|
)
|
||||||
from backend.llm import call_json
|
from backend.llm import call_json
|
||||||
from backend.pipeline._stage import collect_list, validate_issue
|
from backend.pipeline._stage import collect_list, validate_issue
|
||||||
|
|
||||||
@@ -127,3 +132,69 @@ class BrainAgent:
|
|||||||
issues.sort(key=lambda item: -int(item.get("risk_score") or 0))
|
issues.sort(key=lambda item: -int(item.get("risk_score") or 0))
|
||||||
decisions = parsed.get("decisions") or []
|
decisions = parsed.get("decisions") or []
|
||||||
return issues, [item for item in decisions if isinstance(item, dict)]
|
return issues, [item for item in decisions if isinstance(item, dict)]
|
||||||
|
|
||||||
|
def plan_clarifications(self, prioritized: List[Dict]) -> List[Dict]:
|
||||||
|
"""Wave 6.5 planning call: name kept findings the Brain wants to
|
||||||
|
double-check before publishing, as typed clarification requests.
|
||||||
|
|
||||||
|
Returns a capped list of {issue_id, request_type, reason}. Only
|
||||||
|
findings that carry an issue_id and do NOT already have a verification
|
||||||
|
result are offered to the model; anything the model names outside that
|
||||||
|
set, or with an unknown request_type, is dropped by the caller/router.
|
||||||
|
Never raises — a failed/empty plan just yields no requests.
|
||||||
|
"""
|
||||||
|
max_requests = config.BRAIN_CLARIFY_MAX_REQUESTS
|
||||||
|
if not prioritized or max_requests <= 0:
|
||||||
|
return []
|
||||||
|
candidates = [
|
||||||
|
{
|
||||||
|
"issue_id": f.get("issue_id"),
|
||||||
|
"severity": f.get("severity"),
|
||||||
|
"confidence": f.get("confidence"),
|
||||||
|
"source_stage": f.get("source_stage"),
|
||||||
|
"description": (f.get("description") or "")[:400],
|
||||||
|
"evidence": f.get("evidence") or [],
|
||||||
|
"already_verified": bool(f.get("verification")),
|
||||||
|
}
|
||||||
|
for f in prioritized
|
||||||
|
if f.get("issue_id") and not f.get("verification")
|
||||||
|
]
|
||||||
|
if not candidates:
|
||||||
|
return []
|
||||||
|
instruction = (
|
||||||
|
BRAIN_CLARIFY_USER_PROMPT
|
||||||
|
.replace("{max_requests}", str(max_requests))
|
||||||
|
.replace("{findings}", json.dumps(candidates, ensure_ascii=True))
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
parsed = call_json(
|
||||||
|
system_prompt=BRAIN_CLARIFY_SYSTEM_PROMPT,
|
||||||
|
user_text=instruction,
|
||||||
|
max_tokens=config.BRAIN_CLARIFY_MAX_TOKENS,
|
||||||
|
model=config.AGENT_BRAIN_MODEL,
|
||||||
|
usage_tracker=self.usage,
|
||||||
|
usage_stage="agent.brain_clarify",
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
raw = parsed.get("requests") if isinstance(parsed, dict) else parsed
|
||||||
|
if not isinstance(raw, list):
|
||||||
|
return []
|
||||||
|
valid_ids = {c["issue_id"] for c in candidates}
|
||||||
|
requests: List[Dict] = []
|
||||||
|
seen: set = set()
|
||||||
|
for item in raw:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
issue_id = item.get("issue_id")
|
||||||
|
if issue_id not in valid_ids or issue_id in seen:
|
||||||
|
continue
|
||||||
|
requests.append({
|
||||||
|
"issue_id": issue_id,
|
||||||
|
"request_type": (item.get("request_type") or "verify_evidence").strip(),
|
||||||
|
"reason": (item.get("reason") or "").strip(),
|
||||||
|
})
|
||||||
|
seen.add(issue_id)
|
||||||
|
if len(requests) >= max_requests:
|
||||||
|
break
|
||||||
|
return requests
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
"""Per-sheet Drawing Integrity QA agent.
|
||||||
|
|
||||||
|
Reads ONE sheet's own extracted objects + sheet image + deterministic text
|
||||||
|
layer and flags defects internal to that single sheet: dangling detail/
|
||||||
|
callout/keynote references, schedule-vs-plan/legend disagreements on the same
|
||||||
|
sheet, dimension strings that do not sum, missing title-block/scale/north
|
||||||
|
essentials, and duplicate/inconsistent tags. This is the drawing-focused pass
|
||||||
|
that complements the cross-sheet conflict critic; it never does code/ADA or
|
||||||
|
cross-sheet coordination.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
from backend.agents.base import AgentResult, AgentScope, AgentUsage, failure
|
||||||
|
from backend.agents.prompts import (
|
||||||
|
DRAWING_INTEGRITY_SYSTEM_PROMPT,
|
||||||
|
DRAWING_INTEGRITY_USER_PROMPT,
|
||||||
|
)
|
||||||
|
from backend.llm import call_json
|
||||||
|
from backend.pipeline._serialize import dumps
|
||||||
|
from backend.pipeline._stage import collect_list, validate_issue
|
||||||
|
|
||||||
|
|
||||||
|
def _sheet_meta(sheet: Dict) -> Dict:
|
||||||
|
"""Compact title-block-ish descriptor of the sheet (no raw assertions)."""
|
||||||
|
return {
|
||||||
|
"sheet_number": sheet.get("sheet_number"),
|
||||||
|
"sheet_title": sheet.get("sheet_title"),
|
||||||
|
"discipline": sheet.get("discipline"),
|
||||||
|
"drawing_type": sheet.get("drawing_type"),
|
||||||
|
"level": sheet.get("level"),
|
||||||
|
"scale": sheet.get("scale"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def build_integrity_scopes(
|
||||||
|
sheets: List[Dict], page_to_b64: Dict, page_to_text: Dict
|
||||||
|
) -> List[AgentScope]:
|
||||||
|
"""One scope per sheet that carries enough objects to judge internal
|
||||||
|
consistency. Sheets below INTEGRITY_MIN_ASSERTIONS are skipped as too
|
||||||
|
sparse for a meaningful single-sheet back-check."""
|
||||||
|
scopes: List[AgentScope] = []
|
||||||
|
for sheet in sheets:
|
||||||
|
assertions = sheet.get("assertions") or []
|
||||||
|
if len(assertions) < config.INTEGRITY_MIN_ASSERTIONS:
|
||||||
|
continue
|
||||||
|
page_number = sheet.get("page_number")
|
||||||
|
scopes.append(AgentScope(
|
||||||
|
scope_id=f"integrity:{page_number}",
|
||||||
|
payload={
|
||||||
|
"sheet": sheet,
|
||||||
|
"page_number": page_number,
|
||||||
|
"image_b64": page_to_b64.get(page_number),
|
||||||
|
"text_layer": page_to_text.get(page_number) or "",
|
||||||
|
},
|
||||||
|
))
|
||||||
|
return scopes
|
||||||
|
|
||||||
|
|
||||||
|
class DrawingIntegrityAgent:
|
||||||
|
name = "drawing_integrity"
|
||||||
|
|
||||||
|
def __init__(self, usage: AgentUsage) -> None:
|
||||||
|
self.usage = usage
|
||||||
|
|
||||||
|
def run(self, scope: AgentScope) -> AgentResult:
|
||||||
|
try:
|
||||||
|
sheet = dict(scope.payload["sheet"])
|
||||||
|
assertions = (
|
||||||
|
sheet.get("assertions") or []
|
||||||
|
)[:config.AGENT_INTEGRITY_MAX_ASSERTIONS]
|
||||||
|
text_layer = (scope.payload.get("text_layer") or "")[
|
||||||
|
:config.TEXT_LAYER_MAX_CHARS
|
||||||
|
]
|
||||||
|
image_b64 = scope.payload.get("image_b64")
|
||||||
|
images = [image_b64] if image_b64 else []
|
||||||
|
images = images[:config.AGENT_INTEGRITY_MAX_IMAGES]
|
||||||
|
|
||||||
|
instruction = DRAWING_INTEGRITY_USER_PROMPT
|
||||||
|
for key, value in {
|
||||||
|
"sheet_meta": dumps(_sheet_meta(sheet)),
|
||||||
|
"assertions": dumps(assertions),
|
||||||
|
"text_layer": text_layer,
|
||||||
|
}.items():
|
||||||
|
instruction = instruction.replace("{" + key + "}", value)
|
||||||
|
|
||||||
|
parsed = call_json(
|
||||||
|
system_prompt=DRAWING_INTEGRITY_SYSTEM_PROMPT,
|
||||||
|
user_text=instruction,
|
||||||
|
images_b64=images,
|
||||||
|
max_tokens=config.INTEGRITY_MAX_TOKENS,
|
||||||
|
model=config.AGENT_INTEGRITY_MODEL,
|
||||||
|
usage_tracker=self.usage,
|
||||||
|
usage_stage="agent.drawing_integrity",
|
||||||
|
reasoning_effort=config.EXTRACT_REASONING_EFFORT or None,
|
||||||
|
reasoning_max_tokens=config.EXTRACT_REASONING_MAX_TOKENS or None,
|
||||||
|
)
|
||||||
|
findings = collect_list(
|
||||||
|
parsed, "issues",
|
||||||
|
lambda item: validate_issue(item, "drawing_integrity"),
|
||||||
|
)
|
||||||
|
sheet_number = sheet.get("sheet_number")
|
||||||
|
for finding in findings:
|
||||||
|
finding.update(agent=self.name, scope_id=scope.scope_id)
|
||||||
|
# Anchor the finding to this sheet if the model left it blank.
|
||||||
|
if not finding.get("sheets") and sheet_number:
|
||||||
|
finding["sheets"] = [sheet_number]
|
||||||
|
return AgentResult(scope_id=scope.scope_id, artifacts=findings)
|
||||||
|
except Exception as exc:
|
||||||
|
return failure(scope, exc)
|
||||||
@@ -12,6 +12,42 @@ Sheet index: {sheet_index}
|
|||||||
Aggregate sheet summaries: {sheet_summaries}
|
Aggregate sheet summaries: {sheet_summaries}
|
||||||
Cluster summary: {cluster_summary}"""
|
Cluster summary: {cluster_summary}"""
|
||||||
|
|
||||||
|
DRAWING_INTEGRITY_SYSTEM_PROMPT = """You are a Senior Architect performing a single-sheet QAQC back-check of ONE construction drawing before the set is issued for bid, permit, or construction.
|
||||||
|
You are given the extracted construction objects for this one sheet, plus the sheet image and its deterministic PDF text layer.
|
||||||
|
Your job is to find problems INTERNAL TO THIS SHEET - defects a human checker would red-line on this drawing by itself, without needing any other sheet.
|
||||||
|
You are NOT performing code review. You are NOT checking ADA/accessibility. You are NOT doing cross-sheet coordination (a separate reviewer handles conflicts between sheets). You are NOT estimating cost. You are NOT redesigning anything.
|
||||||
|
What IS a drawing-integrity issue on this sheet:
|
||||||
|
- Dangling reference: a detail callout, section marker, elevation marker, keynote, or sheet reference that points to a target that does not exist on this sheet AND is not resolved by an explicit off-sheet reference (e.g. "SIM 5/A501" when this is A501 and it has no detail 5; a keynote number called out in the plan but absent from the keynote legend on the same sheet).
|
||||||
|
- On-sheet contradiction: the plan disagrees with a schedule or legend printed on the SAME sheet; two notes on the sheet contradict each other; a tag in the plan is not in the sheet's own schedule/legend (or vice versa); the title block discipline/level disagrees with the drawing content.
|
||||||
|
- Dimension sanity: a dimension string whose segments do not sum to the stated overall; an overall dimension that contradicts a repeated/typical dimension on the same sheet; obviously impossible or missing critical dimensions on a dimensioned plan.
|
||||||
|
- Missing sheet essentials: no scale, no north arrow on a plan that needs one, missing sheet number/title in the title block, a schedule with header columns but no rows, a legend referenced but not present.
|
||||||
|
- Label/tag hygiene: duplicate tags that should be unique on this sheet (two different doors both tagged 101A), a room shown with no room number/name where the sheet otherwise numbers rooms, inconsistent tag formatting that breaks a reference.
|
||||||
|
What is NOT a drawing-integrity issue:
|
||||||
|
- Anything requiring another sheet to judge (that is cross-sheet coordination, handled elsewhere).
|
||||||
|
- A code, ADA, or accessibility requirement.
|
||||||
|
- A design preference or cost concern.
|
||||||
|
- A value simply not repeated where repetition is optional.
|
||||||
|
- Anything you cannot support with text or a clear visual from THIS sheet.
|
||||||
|
Be conservative and evidence-bound:
|
||||||
|
- Only flag defects you can point to with verbatim source_text from this sheet or a clear description of what the image shows.
|
||||||
|
- Trust the TEXT LAYER for alphanumeric content (numbers, tags, note text, dimensions); use the image for geometry, symbols, linework, and whether a referenced target actually appears.
|
||||||
|
- When a value is marked DISPUTED (possible extraction misread), verify against the image before relying on it.
|
||||||
|
- If the sheet is internally clean, return an empty issues array.
|
||||||
|
Severity (use exactly one of critical, high, medium, low):
|
||||||
|
- high = a defect that would cause rework, a wrong build, or a stop at permit/bid if issued as-is (missing critical dimension, dangling reference to a nonexistent detail that drives construction).
|
||||||
|
- medium = a real drawing defect needing correction before issue.
|
||||||
|
- low = minor cleanup/clarification.
|
||||||
|
Use plain ASCII only. Respond only with valid JSON."""
|
||||||
|
|
||||||
|
DRAWING_INTEGRITY_USER_PROMPT = """Back-check this single sheet for internal drawing-integrity defects.
|
||||||
|
Respond ONLY with a valid JSON object - no markdown fences, no explanation:
|
||||||
|
{"issues":[{"issue_id":"string","source_stage":"drawing_integrity","category":"dangling_reference | on_sheet_contradiction | dimension_error | missing_sheet_essential | tag_or_label_error | other","severity":"critical | high | medium | low","confidence":"high | medium | low","location":"where on the sheet, e.g. 'Room 124 / detail callout 5' or 'door schedule'","disciplines":["string"],"sheets":["this sheet number"],"description":"senior architect explanation of the defect and why it matters","evidence":[{"discipline":"string","sheet":"string","source_text":"verbatim text from this sheet","asserted_value":"string"}],"recommended_resolution":"coordinate drawing | correct dimension | add missing detail | issue RFI | verify with architect | verify with engineer","code_reference":null}]}
|
||||||
|
If the sheet is internally clean, return {"issues":[]}.
|
||||||
|
Sheet: {sheet_meta}
|
||||||
|
Extracted objects on this sheet: {assertions}
|
||||||
|
TEXT LAYER (deterministic page text - authoritative for alphanumeric content):
|
||||||
|
{text_layer}"""
|
||||||
|
|
||||||
BRAIN_SYSTEM_PROMPT = """You are the central decision layer for a construction drawing
|
BRAIN_SYSTEM_PROMPT = """You are the central decision layer for a construction drawing
|
||||||
review. Merge duplicate specialist findings, reject vague or unsupported findings,
|
review. Merge duplicate specialist findings, reject vague or unsupported findings,
|
||||||
preserve verbatim evidence, and prioritize the kept issues. Do not create new issues.
|
preserve verbatim evidence, and prioritize the kept issues. Do not create new issues.
|
||||||
@@ -19,7 +55,23 @@ Conflicts need drawing evidence; completeness findings may instead cite an expli
|
|||||||
missing item from the sheet index. Return only valid JSON."""
|
missing item from the sheet index. Return only valid JSON."""
|
||||||
|
|
||||||
BRAIN_USER_PROMPT = """Judge and consolidate these scoped specialist findings.
|
BRAIN_USER_PROMPT = """Judge and consolidate these scoped specialist findings.
|
||||||
Return {"issues":[{"issue_id":"string","source_stage":"conflict | qaqc | code | constructability","category":"string","severity":"critical | high | medium | low","confidence":"high | medium | low","location":"string","disciplines":["string"],"sheets":["string"],"description":"string","evidence":[{"discipline":"string","sheet":"string","source_text":"string","asserted_value":"string"}],"recommended_resolution":"string","code_reference":"string or null","risk_score":1,"recommended_priority":"immediate | before_bid | before_permit | before_construction | track_only"}],"decisions":[{"finding_refs":["string"],"action":"kept | merged | dropped","reason":"string","kept_issue_id":"string or null"}]}.
|
Return {"issues":[{"issue_id":"string","source_stage":"conflict | drawing_integrity | qaqc | code | constructability","category":"string","severity":"critical | high | medium | low","confidence":"high | medium | low","location":"string","disciplines":["string"],"sheets":["string"],"description":"string","evidence":[{"discipline":"string","sheet":"string","source_text":"string","asserted_value":"string"}],"recommended_resolution":"string","code_reference":"string or null","risk_score":1,"recommended_priority":"immediate | before_bid | before_permit | before_construction | track_only"}],"decisions":[{"finding_refs":["string"],"action":"kept | merged | dropped","reason":"string","kept_issue_id":"string or null"}]}.
|
||||||
Sheet index: {sheet_index}
|
Sheet index: {sheet_index}
|
||||||
Jurisdiction summary: {jurisdiction}
|
Jurisdiction summary: {jurisdiction}
|
||||||
Specialist findings: {findings}"""
|
Specialist findings: {findings}"""
|
||||||
|
|
||||||
|
BRAIN_CLARIFY_SYSTEM_PROMPT = """You are the central decision layer for a construction drawing review, deciding which of your kept findings you are NOT yet confident enough to publish.
|
||||||
|
You have already merged and prioritized the findings. Now, for the borderline ones, you may request ONE targeted clarification each before the report is finalized.
|
||||||
|
Request a clarification only when a finding's evidence is thin, ambiguous, possibly a misread of the drawing, or internally inconsistent - the kind of finding a senior reviewer would double-check against the sheet before signing off. Do NOT request clarification for findings that are already clearly supported by verbatim evidence, and do NOT re-request a finding that already carries a verification result.
|
||||||
|
The only request type available right now is:
|
||||||
|
- verify_evidence: re-check this finding's quoted evidence against the actual sheet images and deterministic text layer (catches wave-1 vision misreads such as "(2)" vs "(5)" and dangling references that do not actually appear on the sheet).
|
||||||
|
Be selective. Requesting everything wastes the budget and slows the review; request only the findings where a second look would actually change your decision.
|
||||||
|
Use plain ASCII only. Respond only with valid JSON."""
|
||||||
|
|
||||||
|
BRAIN_CLARIFY_USER_PROMPT = """Decide which of these kept findings you want to double-check before publishing.
|
||||||
|
You may request at most {max_requests} clarifications. Choose the findings where a second look at the sheet would most likely change your keep/drop/severity decision.
|
||||||
|
Respond ONLY with a valid JSON object - no markdown fences, no explanation:
|
||||||
|
{"requests":[{"issue_id":"the issue_id of the finding to check","request_type":"verify_evidence","reason":"one sentence: why this finding is uncertain"}]}
|
||||||
|
If every finding is already well supported, return {"requests":[]}.
|
||||||
|
Findings (each shows issue_id, severity, confidence, evidence, and whether it already has a verification result):
|
||||||
|
{findings}"""
|
||||||
+197
-38
@@ -18,6 +18,9 @@ from backend.agents.extractors import (
|
|||||||
SheetExtractorAgent,
|
SheetExtractorAgent,
|
||||||
SheetIndexAgent,
|
SheetIndexAgent,
|
||||||
)
|
)
|
||||||
|
from backend.agents.integrity_agent import (
|
||||||
|
DrawingIntegrityAgent, build_integrity_scopes,
|
||||||
|
)
|
||||||
from backend.agents.linker import LinkerAgent, build_link_scopes, build_object_graph
|
from backend.agents.linker import LinkerAgent, build_link_scopes, build_object_graph
|
||||||
from backend.agents.memory import ProjectMemory
|
from backend.agents.memory import ProjectMemory
|
||||||
from backend.agents.orchestrator import Orchestrator
|
from backend.agents.orchestrator import Orchestrator
|
||||||
@@ -31,6 +34,7 @@ from backend.pipeline.report import build_report, to_markdown
|
|||||||
from backend.pipeline.sheet_index import derive_project_meta_from_cover
|
from backend.pipeline.sheet_index import derive_project_meta_from_cover
|
||||||
from backend.review.gate import build_review_queue
|
from backend.review.gate import build_review_queue
|
||||||
from backend.review.store import ReviewStore
|
from backend.review.store import ReviewStore
|
||||||
|
from backend.sheet_reconcile import declared_sheet_list, reconcile_sheets
|
||||||
from backend.text_layer import (
|
from backend.text_layer import (
|
||||||
attach_text_layers, coverage_gaps, find_evidence_bbox, render_crop,
|
attach_text_layers, coverage_gaps, find_evidence_bbox, render_crop,
|
||||||
)
|
)
|
||||||
@@ -84,6 +88,20 @@ def run_agent_pipeline(
|
|||||||
sheets.sort(key=lambda sheet: sheet.get("page_number") or 0)
|
sheets.sort(key=lambda sheet: sheet.get("page_number") or 0)
|
||||||
memory.replace("sheets", sheets)
|
memory.replace("sheets", sheets)
|
||||||
memory.dump("01-extract.json")
|
memory.dump("01-extract.json")
|
||||||
|
|
||||||
|
# Deterministic reconciliation: the cover sheet's own sheet index
|
||||||
|
# declares what the set should contain; compare against what wave 1
|
||||||
|
# identified (catches missed sheets AND phantom/misread sheet numbers).
|
||||||
|
sheet_recon = reconcile_sheets(sheets, declared_sheet_list(page_to_text))
|
||||||
|
if sheet_recon["declared_total"]:
|
||||||
|
print(f"[SheetIndex] cover declares {sheet_recon['declared_total']} "
|
||||||
|
f"sheets; {sheet_recon['found_total']} identified in set")
|
||||||
|
if sheet_recon["declared_not_in_set"]:
|
||||||
|
print(f"[SheetIndex] declared but not in set: "
|
||||||
|
f"{', '.join(sheet_recon['declared_not_in_set'][:20])}")
|
||||||
|
if sheet_recon["in_set_not_declared"]:
|
||||||
|
print(f"[SheetIndex] in set but not declared: "
|
||||||
|
f"{', '.join(sheet_recon['in_set_not_declared'][:20])}")
|
||||||
# Coverage signal: text layer present but extraction failed/empty reuses
|
# Coverage signal: text layer present but extraction failed/empty reuses
|
||||||
# the failed-scopes gap-finding path (finding built below wave 6).
|
# the failed-scopes gap-finding path (finding built below wave 6).
|
||||||
for gap_page in coverage_gaps(pages, sheets):
|
for gap_page in coverage_gaps(pages, sheets):
|
||||||
@@ -159,11 +177,23 @@ def run_agent_pipeline(
|
|||||||
memory.extend("findings", conflict_findings)
|
memory.extend("findings", conflict_findings)
|
||||||
|
|
||||||
orchestrator.stage("Agent wave 5: scoped specialists")
|
orchestrator.stage("Agent wave 5: scoped specialists")
|
||||||
code_results = orchestrator.run_scopes(
|
if config.ENABLE_CODE_REVIEW:
|
||||||
CodeAgent(usage),
|
code_results = orchestrator.run_scopes(
|
||||||
build_code_scopes(sheets, jurisdiction, sheet_index),
|
CodeAgent(usage),
|
||||||
config.AGENT_SPECIALIST_CONCURRENCY,
|
build_code_scopes(sheets, jurisdiction, sheet_index),
|
||||||
)
|
config.AGENT_SPECIALIST_CONCURRENCY,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
orchestrator.stage("[wave 5] code/ADA review disabled (ENABLE_CODE_REVIEW=0)")
|
||||||
|
code_results = []
|
||||||
|
if config.ENABLE_DRAWING_INTEGRITY:
|
||||||
|
integrity_results = orchestrator.run_scopes(
|
||||||
|
DrawingIntegrityAgent(usage),
|
||||||
|
build_integrity_scopes(sheets, page_to_b64, page_to_text),
|
||||||
|
config.AGENT_INTEGRITY_CONCURRENCY,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
integrity_results = []
|
||||||
construct_results = orchestrator.run_scopes(
|
construct_results = orchestrator.run_scopes(
|
||||||
ConstructabilityAgent(usage),
|
ConstructabilityAgent(usage),
|
||||||
build_construct_scopes(clusters, conflict_findings),
|
build_construct_scopes(clusters, conflict_findings),
|
||||||
@@ -182,7 +212,8 @@ def run_agent_pipeline(
|
|||||||
)
|
)
|
||||||
specialist_findings = [
|
specialist_findings = [
|
||||||
artifact
|
artifact
|
||||||
for result in code_results + construct_results + completeness_results
|
for result in (code_results + integrity_results
|
||||||
|
+ construct_results + completeness_results)
|
||||||
for artifact in result.artifacts
|
for artifact in result.artifacts
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -195,38 +226,12 @@ def run_agent_pipeline(
|
|||||||
severities=config.AGENT_VERIFY_SEVERITIES,
|
severities=config.AGENT_VERIFY_SEVERITIES,
|
||||||
)
|
)
|
||||||
target_indexes = {id(f): i for i, f in enumerate(specialist_findings)}
|
target_indexes = {id(f): i for i, f in enumerate(specialist_findings)}
|
||||||
verify_scopes = []
|
verify_scopes = _build_verify_scopes(
|
||||||
for finding in verify_targets:
|
verify_targets,
|
||||||
cited_pages = [
|
index_for=lambda f: target_indexes[id(f)],
|
||||||
sheet_to_page[str(name)]
|
sheet_to_page=sheet_to_page, page_to_b64=page_to_b64,
|
||||||
for name in (finding.get("sheets") or [])
|
page_to_text=page_to_text, page_words=page_words, pdf_path=pdf_path,
|
||||||
if sheet_to_page.get(str(name)) in page_to_b64
|
)
|
||||||
]
|
|
||||||
images = [
|
|
||||||
page_to_b64[p]
|
|
||||||
for p in cited_pages[:config.AGENT_CONFLICT_MAX_IMAGES]
|
|
||||||
]
|
|
||||||
if not images:
|
|
||||||
continue # never judge evidence against images we could not load
|
|
||||||
# Text oracle: concatenated text layer of the cited sheets, capped.
|
|
||||||
excerpt = "\n\n".join(
|
|
||||||
f"--- Page {p} ---\n{page_to_text[p]}"
|
|
||||||
for p in cited_pages
|
|
||||||
if page_to_text.get(p)
|
|
||||||
)[:config.VERIFY_TEXT_MAX_CHARS]
|
|
||||||
if config.VERIFY_HI_DPI_CROPS:
|
|
||||||
images = _evidence_crops(finding, cited_pages, sheet_to_page,
|
|
||||||
page_words, page_to_b64, pdf_path,
|
|
||||||
fallback=images)
|
|
||||||
verify_scopes.append(AgentScope(
|
|
||||||
scope_id=f"verify:{target_indexes[id(finding)]}",
|
|
||||||
payload={
|
|
||||||
"finding_index": target_indexes[id(finding)],
|
|
||||||
"finding": finding,
|
|
||||||
"images_b64": images,
|
|
||||||
"text_layer_excerpt": excerpt,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
verify_results = orchestrator.run_scopes(
|
verify_results = orchestrator.run_scopes(
|
||||||
EvidenceVerifierAgent(usage), verify_scopes, config.AGENT_VERIFY_CONCURRENCY)
|
EvidenceVerifierAgent(usage), verify_scopes, config.AGENT_VERIFY_CONCURRENCY)
|
||||||
suppressed = apply_verdicts(specialist_findings, verify_results)
|
suppressed = apply_verdicts(specialist_findings, verify_results)
|
||||||
@@ -271,6 +276,18 @@ def run_agent_pipeline(
|
|||||||
1 for decision in decisions if decision.get("action") == "merged"
|
1 for decision in decisions if decision.get("action") == "merged"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Wave 6.5 — Brain-directed clarification (bounded hub-and-spoke). The Brain
|
||||||
|
# names kept findings it is unsure about; verify_evidence requests route
|
||||||
|
# back through the wave-5b verifier (fresh images + text-layer oracle).
|
||||||
|
# Refuted findings are demoted, dropped from `prioritized`, and moved to
|
||||||
|
# memory["suppressed"]. One planning call, one bounded verify wave, no loop.
|
||||||
|
if config.ENABLE_BRAIN_CLARIFY and prioritized:
|
||||||
|
prioritized = _brain_clarification_pass(
|
||||||
|
orchestrator, usage, memory, prioritized,
|
||||||
|
sheet_to_page=sheet_to_page, page_to_b64=page_to_b64,
|
||||||
|
page_to_text=page_to_text, page_words=page_words, pdf_path=pdf_path,
|
||||||
|
)
|
||||||
|
|
||||||
if require_review:
|
if require_review:
|
||||||
orchestrator.stage("Agent review gate: build human-review queue")
|
orchestrator.stage("Agent review gate: build human-review queue")
|
||||||
memory_snapshot = memory.snapshot()
|
memory_snapshot = memory.snapshot()
|
||||||
@@ -289,6 +306,7 @@ def run_agent_pipeline(
|
|||||||
"project_input": merged_input,
|
"project_input": merged_input,
|
||||||
"jurisdiction": jurisdiction,
|
"jurisdiction": jurisdiction,
|
||||||
"sheet_index": sheet_index,
|
"sheet_index": sheet_index,
|
||||||
|
"sheet_reconciliation": sheet_recon,
|
||||||
"project_intelligence": object_graph,
|
"project_intelligence": object_graph,
|
||||||
"validated_issues": prioritized,
|
"validated_issues": prioritized,
|
||||||
"rfis": [],
|
"rfis": [],
|
||||||
@@ -315,6 +333,10 @@ def run_agent_pipeline(
|
|||||||
1 for item in specialist_findings
|
1 for item in specialist_findings
|
||||||
if item.get("source_stage") == "code"
|
if item.get("source_stage") == "code"
|
||||||
),
|
),
|
||||||
|
"drawing_integrity": sum(
|
||||||
|
1 for item in specialist_findings
|
||||||
|
if item.get("source_stage") == "drawing_integrity"
|
||||||
|
),
|
||||||
"constructability": sum(
|
"constructability": sum(
|
||||||
1 for item in specialist_findings
|
1 for item in specialist_findings
|
||||||
if item.get("source_stage") == "constructability"
|
if item.get("source_stage") == "constructability"
|
||||||
@@ -365,6 +387,7 @@ def run_agent_pipeline(
|
|||||||
"project_input": merged_input,
|
"project_input": merged_input,
|
||||||
"jurisdiction": jurisdiction,
|
"jurisdiction": jurisdiction,
|
||||||
"sheet_index": sheet_index,
|
"sheet_index": sheet_index,
|
||||||
|
"sheet_reconciliation": sheet_recon,
|
||||||
"project_intelligence": object_graph,
|
"project_intelligence": object_graph,
|
||||||
"validated_issues": prioritized,
|
"validated_issues": prioritized,
|
||||||
"rfis": rfis,
|
"rfis": rfis,
|
||||||
@@ -387,6 +410,10 @@ def run_agent_pipeline(
|
|||||||
1 for item in specialist_findings
|
1 for item in specialist_findings
|
||||||
if item.get("source_stage") == "code"
|
if item.get("source_stage") == "code"
|
||||||
),
|
),
|
||||||
|
"drawing_integrity": sum(
|
||||||
|
1 for item in specialist_findings
|
||||||
|
if item.get("source_stage") == "drawing_integrity"
|
||||||
|
),
|
||||||
"constructability": sum(
|
"constructability": sum(
|
||||||
1 for item in specialist_findings
|
1 for item in specialist_findings
|
||||||
if item.get("source_stage") == "constructability"
|
if item.get("source_stage") == "constructability"
|
||||||
@@ -423,6 +450,138 @@ def _dump(out_dir: str, name: str, value) -> None:
|
|||||||
json.dump(value, f, indent=2)
|
json.dump(value, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def _brain_clarification_pass(
|
||||||
|
orchestrator,
|
||||||
|
usage,
|
||||||
|
memory,
|
||||||
|
prioritized,
|
||||||
|
sheet_to_page,
|
||||||
|
page_to_b64,
|
||||||
|
page_to_text,
|
||||||
|
page_words,
|
||||||
|
pdf_path,
|
||||||
|
):
|
||||||
|
"""Wave 6.5: let the Brain request targeted clarifications, execute the
|
||||||
|
verify_evidence ones through the wave-5b verifier, and prune refuted
|
||||||
|
findings out of `prioritized` into memory["suppressed"].
|
||||||
|
|
||||||
|
Bounded and non-looping: one Brain planning call, at most
|
||||||
|
BRAIN_CLARIFY_MAX_REQUESTS verifications, a single pass. Returns the
|
||||||
|
(possibly shortened) prioritized list. Any request type other than
|
||||||
|
verify_evidence is logged as planned-but-not-executed and left untouched.
|
||||||
|
"""
|
||||||
|
requests = BrainAgent(usage).plan_clarifications(prioritized)
|
||||||
|
if not requests:
|
||||||
|
return prioritized
|
||||||
|
by_id = {f.get("issue_id"): f for f in prioritized}
|
||||||
|
verify_findings = []
|
||||||
|
unsupported = 0
|
||||||
|
for req in requests:
|
||||||
|
if req.get("request_type") != "verify_evidence":
|
||||||
|
unsupported += 1
|
||||||
|
continue
|
||||||
|
finding = by_id.get(req.get("issue_id"))
|
||||||
|
if finding is not None and finding not in verify_findings:
|
||||||
|
verify_findings.append(finding)
|
||||||
|
orchestrator.stage(
|
||||||
|
f"Agent wave 6.5: Brain-directed clarification "
|
||||||
|
f"({len(verify_findings)} verify, {unsupported} other)"
|
||||||
|
)
|
||||||
|
if unsupported:
|
||||||
|
for req in requests:
|
||||||
|
if req.get("request_type") != "verify_evidence":
|
||||||
|
orchestrator.stats.failed_scopes.append(
|
||||||
|
f"brain_clarify:{req.get('issue_id')}: "
|
||||||
|
f"request_type '{req.get('request_type')}' planned, "
|
||||||
|
f"not executed (v1 supports verify_evidence only)"
|
||||||
|
)
|
||||||
|
if not verify_findings:
|
||||||
|
return prioritized
|
||||||
|
index_of = {id(f): i for i, f in enumerate(prioritized)}
|
||||||
|
verify_scopes = _build_verify_scopes(
|
||||||
|
verify_findings,
|
||||||
|
index_for=lambda f: index_of[id(f)],
|
||||||
|
sheet_to_page=sheet_to_page, page_to_b64=page_to_b64,
|
||||||
|
page_to_text=page_to_text, page_words=page_words, pdf_path=pdf_path,
|
||||||
|
)
|
||||||
|
if not verify_scopes:
|
||||||
|
return prioritized
|
||||||
|
verify_results = orchestrator.run_scopes(
|
||||||
|
EvidenceVerifierAgent(usage), verify_scopes,
|
||||||
|
config.AGENT_VERIFY_CONCURRENCY,
|
||||||
|
)
|
||||||
|
suppressed = apply_verdicts(prioritized, verify_results)
|
||||||
|
if suppressed:
|
||||||
|
suppressed_ids = {id(f) for f in suppressed}
|
||||||
|
prioritized = [f for f in prioritized if id(f) not in suppressed_ids]
|
||||||
|
existing = memory.snapshot().get("suppressed") or []
|
||||||
|
memory.replace("suppressed", existing + suppressed)
|
||||||
|
memory.extend("decisions", [
|
||||||
|
{
|
||||||
|
"finding_refs": [f.get("issue_id")],
|
||||||
|
"action": "dropped",
|
||||||
|
"reason": "Brain-directed clarification: evidence refuted on re-check",
|
||||||
|
"kept_issue_id": None,
|
||||||
|
}
|
||||||
|
for f in suppressed
|
||||||
|
])
|
||||||
|
return prioritized
|
||||||
|
|
||||||
|
|
||||||
|
def _build_verify_scopes(
|
||||||
|
targets,
|
||||||
|
index_for,
|
||||||
|
sheet_to_page,
|
||||||
|
page_to_b64,
|
||||||
|
page_to_text,
|
||||||
|
page_words,
|
||||||
|
pdf_path,
|
||||||
|
):
|
||||||
|
"""Build EvidenceVerifierAgent scopes for a set of findings.
|
||||||
|
|
||||||
|
Shared by wave 5b (severity-gated) and wave 6.5 (Brain-directed): each
|
||||||
|
finding's cited sheets are mapped to page images (hi-DPI evidence crops
|
||||||
|
when enabled, else full pages) plus a capped text-layer oracle. Findings
|
||||||
|
whose sheets resolve to NO loadable image are skipped (I2 guard) — never
|
||||||
|
judge evidence against images we could not load. index_for(finding) yields
|
||||||
|
the finding_index the verifier echoes back for apply_verdicts alignment.
|
||||||
|
"""
|
||||||
|
scopes = []
|
||||||
|
for finding in targets:
|
||||||
|
cited_pages = [
|
||||||
|
sheet_to_page[str(name)]
|
||||||
|
for name in (finding.get("sheets") or [])
|
||||||
|
if sheet_to_page.get(str(name)) in page_to_b64
|
||||||
|
]
|
||||||
|
images = [
|
||||||
|
page_to_b64[p]
|
||||||
|
for p in cited_pages[:config.AGENT_CONFLICT_MAX_IMAGES]
|
||||||
|
]
|
||||||
|
if not images:
|
||||||
|
continue # never judge evidence against images we could not load
|
||||||
|
# Text oracle: concatenated text layer of the cited sheets, capped.
|
||||||
|
excerpt = "\n\n".join(
|
||||||
|
f"--- Page {p} ---\n{page_to_text[p]}"
|
||||||
|
for p in cited_pages
|
||||||
|
if page_to_text.get(p)
|
||||||
|
)[:config.VERIFY_TEXT_MAX_CHARS]
|
||||||
|
if config.VERIFY_HI_DPI_CROPS:
|
||||||
|
images = _evidence_crops(finding, cited_pages, sheet_to_page,
|
||||||
|
page_words, page_to_b64, pdf_path,
|
||||||
|
fallback=images)
|
||||||
|
finding_index = index_for(finding)
|
||||||
|
scopes.append(AgentScope(
|
||||||
|
scope_id=f"verify:{finding_index}",
|
||||||
|
payload={
|
||||||
|
"finding_index": finding_index,
|
||||||
|
"finding": finding,
|
||||||
|
"images_b64": images,
|
||||||
|
"text_layer_excerpt": excerpt,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
return scopes
|
||||||
|
|
||||||
|
|
||||||
def _evidence_crops(
|
def _evidence_crops(
|
||||||
finding: Dict,
|
finding: Dict,
|
||||||
cited_pages: list,
|
cited_pages: list,
|
||||||
|
|||||||
@@ -36,6 +36,15 @@ def _valid_verdict(item):
|
|||||||
|
|
||||||
|
|
||||||
def _status(verdicts):
|
def _status(verdicts):
|
||||||
|
"""Roll per-evidence verdicts up to a finding-level status.
|
||||||
|
|
||||||
|
NOTE on "corrected": it is deliberately NON-confirming. The canonical case
|
||||||
|
(job 959e16407573) is evidence quoting "(2) 2x6 STUD PACK" against a sheet
|
||||||
|
that reads "(5)" — the text exists but the VALUE the finding rests on was a
|
||||||
|
wave-1 misread, so the finding's basis is gone. Hence refuted = zero
|
||||||
|
CONFIRMED verdicts, not zero not_found ones. Do not "fix" this to treat
|
||||||
|
corrected as supporting; see tests/agents/test_verifier.py.
|
||||||
|
"""
|
||||||
if not verdicts:
|
if not verdicts:
|
||||||
return "unverified"
|
return "unverified"
|
||||||
confirmed = sum(1 for v in verdicts if v["verdict"] == "confirmed")
|
confirmed = sum(1 for v in verdicts if v["verdict"] == "confirmed")
|
||||||
|
|||||||
+71
-4
@@ -12,6 +12,15 @@ load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env"))
|
|||||||
|
|
||||||
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
_TRUTHY = ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
|
def _flag(name: str, default: str) -> bool:
|
||||||
|
"""Parse a boolean env knob. Accepts 1/true/yes/on (case-insensitive) so a
|
||||||
|
knob set to "1" behaves the same as one set to "true" — mixing bare
|
||||||
|
`== "true"` comparisons with this set silently disabled features."""
|
||||||
|
return os.getenv(name, default).strip().lower() in _TRUTHY
|
||||||
|
|
||||||
# -- AI Backend (OpenRouter) ----------------------------------------
|
# -- AI Backend (OpenRouter) ----------------------------------------
|
||||||
# One multimodal model does both extraction (Stage 1) and conflict
|
# One multimodal model does both extraction (Stage 1) and conflict
|
||||||
# reasoning (Stage 3). Override MODEL per-stage if you ever split them.
|
# reasoning (Stage 3). Override MODEL per-stage if you ever split them.
|
||||||
@@ -47,6 +56,33 @@ AGENT_CONFLICT_CONCURRENCY = int(os.getenv("AGENT_CONFLICT_CONCURRENCY", "4"))
|
|||||||
AGENT_SPECIALIST_CONCURRENCY = int(os.getenv("AGENT_SPECIALIST_CONCURRENCY", "4"))
|
AGENT_SPECIALIST_CONCURRENCY = int(os.getenv("AGENT_SPECIALIST_CONCURRENCY", "4"))
|
||||||
AGENT_RFI_CONCURRENCY = int(os.getenv("AGENT_RFI_CONCURRENCY", "4"))
|
AGENT_RFI_CONCURRENCY = int(os.getenv("AGENT_RFI_CONCURRENCY", "4"))
|
||||||
|
|
||||||
|
# -- Review focus toggles -------------------------------------------
|
||||||
|
# ENABLE_CODE_REVIEW gates the code/ADA/jurisdiction review path in BOTH
|
||||||
|
# pipelines. Default OFF: the product's focus is drawing-integrity and
|
||||||
|
# cross-discipline coordination, not code/accessibility compliance. When
|
||||||
|
# False the CodeAgent wave (agent) and the Code/ADA stage (classic) are
|
||||||
|
# skipped entirely, by_stage.code reports 0, and nothing in the ADA corpus
|
||||||
|
# or jurisdiction meta is deleted so the path can be re-enabled with one env
|
||||||
|
# flag. Set ENABLE_CODE_REVIEW=1 to restore code/ADA findings.
|
||||||
|
ENABLE_CODE_REVIEW = _flag("ENABLE_CODE_REVIEW", "false")
|
||||||
|
|
||||||
|
# Per-sheet Drawing Integrity QA wave (agent + classic). This is the
|
||||||
|
# drawing-focused pass: it reads ONE sheet's own objects + image + text layer
|
||||||
|
# and flags problems internal to that sheet -- dangling detail/callout/keynote
|
||||||
|
# references, schedule-vs-plan or legend disagreements on the same sheet,
|
||||||
|
# dimension strings that do not sum, missing title-block/scale/north-arrow,
|
||||||
|
# and notes that contradict each other. It complements (does not replace) the
|
||||||
|
# cross-sheet conflict critic. Default ON.
|
||||||
|
ENABLE_DRAWING_INTEGRITY = _flag("ENABLE_DRAWING_INTEGRITY", "true")
|
||||||
|
AGENT_INTEGRITY_MODEL = os.getenv("AGENT_INTEGRITY_MODEL", "") or MODEL
|
||||||
|
AGENT_INTEGRITY_CONCURRENCY = int(os.getenv("AGENT_INTEGRITY_CONCURRENCY", "4"))
|
||||||
|
AGENT_INTEGRITY_MAX_IMAGES = int(os.getenv("AGENT_INTEGRITY_MAX_IMAGES", "1"))
|
||||||
|
AGENT_INTEGRITY_MAX_ASSERTIONS = int(os.getenv("AGENT_INTEGRITY_MAX_ASSERTIONS", "80"))
|
||||||
|
INTEGRITY_MAX_TOKENS = int(os.getenv("INTEGRITY_MAX_TOKENS", "16384"))
|
||||||
|
# Skip sheets with fewer than this many extracted objects -- too sparse for a
|
||||||
|
# meaningful internal-consistency pass (avoids burning a call on near-empty pages).
|
||||||
|
INTEGRITY_MIN_ASSERTIONS = int(os.getenv("INTEGRITY_MIN_ASSERTIONS", "3"))
|
||||||
|
|
||||||
# Wave 5b evidence verification (vision fact-check of cited sheet text)
|
# Wave 5b evidence verification (vision fact-check of cited sheet text)
|
||||||
AGENT_VERIFY_MODEL = os.getenv("AGENT_VERIFY_MODEL", "") or MODEL
|
AGENT_VERIFY_MODEL = os.getenv("AGENT_VERIFY_MODEL", "") or MODEL
|
||||||
AGENT_VERIFY_CONCURRENCY = int(os.getenv("AGENT_VERIFY_CONCURRENCY", "4"))
|
AGENT_VERIFY_CONCURRENCY = int(os.getenv("AGENT_VERIFY_CONCURRENCY", "4"))
|
||||||
@@ -59,6 +95,17 @@ AGENT_VERIFY_SEVERITIES = {
|
|||||||
AGENT_VERIFY_REASONING_EFFORT = os.getenv("AGENT_VERIFY_REASONING_EFFORT", "low").strip()
|
AGENT_VERIFY_REASONING_EFFORT = os.getenv("AGENT_VERIFY_REASONING_EFFORT", "low").strip()
|
||||||
VERIFY_MAX_TOKENS = int(os.getenv("VERIFY_MAX_TOKENS", "8192"))
|
VERIFY_MAX_TOKENS = int(os.getenv("VERIFY_MAX_TOKENS", "8192"))
|
||||||
|
|
||||||
|
# Wave 6.5 Brain-directed clarification. After the Brain merge, the Brain may
|
||||||
|
# name findings it is unsure about and emit typed clarification requests; v1
|
||||||
|
# executes verify_evidence requests by routing them back through the wave-5b
|
||||||
|
# EvidenceVerifierAgent (fresh page images + hi-DPI evidence crops + text-layer
|
||||||
|
# oracle). Bounded: one planning call, at most BRAIN_CLARIFY_MAX_REQUESTS
|
||||||
|
# verifications, a single iteration. Reuses AGENT_VERIFY_* / VERIFY_* knobs for
|
||||||
|
# the verification calls. Default ON.
|
||||||
|
ENABLE_BRAIN_CLARIFY = _flag("ENABLE_BRAIN_CLARIFY", "true")
|
||||||
|
BRAIN_CLARIFY_MAX_REQUESTS = int(os.getenv("BRAIN_CLARIFY_MAX_REQUESTS", "8"))
|
||||||
|
BRAIN_CLARIFY_MAX_TOKENS = int(os.getenv("BRAIN_CLARIFY_MAX_TOKENS", "4096"))
|
||||||
|
|
||||||
# -- Text-layer grounding (deterministic PDF text layer via PyMuPDF) ----
|
# -- Text-layer grounding (deterministic PDF text layer via PyMuPDF) ----
|
||||||
# The vector text layer is extracted once per job and grounds the extractor,
|
# The vector text layer is extracted once per job and grounds the extractor,
|
||||||
# rescues misquoted-but-real values in the grounding guard, and serves the
|
# rescues misquoted-but-real values in the grounding guard, and serves the
|
||||||
@@ -82,6 +129,26 @@ AGENT_REVIEW_AUDIT_SAMPLE = int(os.getenv("AGENT_REVIEW_AUDIT_SAMPLE", "5"))
|
|||||||
# NOTE: currently unwired - reserved for future cross-job aggregation tooling.
|
# NOTE: currently unwired - reserved for future cross-job aggregation tooling.
|
||||||
REVIEW_AGGREGATE_INCLUDE_TEXT = os.getenv("REVIEW_AGGREGATE_INCLUDE_TEXT", "false").strip().lower() in ("1", "true", "yes")
|
REVIEW_AGGREGATE_INCLUDE_TEXT = os.getenv("REVIEW_AGGREGATE_INCLUDE_TEXT", "false").strip().lower() in ("1", "true", "yes")
|
||||||
|
|
||||||
|
# -- Review chat (ask-the-run Q&A on the review screen) --------------
|
||||||
|
# A read-only explainer: it answers "why did the run decide X?" from the job's
|
||||||
|
# own artifacts and never mutates findings, decisions, or code. Every turn is
|
||||||
|
# appended to <out_dir>/review/chat_log.jsonl AND to the cross-job feedback
|
||||||
|
# store (REVIEW_FEEDBACK_DIR) so answers are available to future prompt priors.
|
||||||
|
# HISTORY_TURNS caps how much of a thread is replayed into the prompt;
|
||||||
|
# LOG_LINES caps how many job.log lines are searched into the context bundle.
|
||||||
|
ENABLE_REVIEW_CHAT = _flag("ENABLE_REVIEW_CHAT", "true")
|
||||||
|
REVIEW_CHAT_MODEL = os.getenv("REVIEW_CHAT_MODEL", "") or TEXT_MODEL
|
||||||
|
REVIEW_CHAT_MAX_TOKENS = int(os.getenv("REVIEW_CHAT_MAX_TOKENS", "4096"))
|
||||||
|
REVIEW_CHAT_HISTORY_TURNS = int(os.getenv("REVIEW_CHAT_HISTORY_TURNS", "6"))
|
||||||
|
REVIEW_CHAT_LOG_LINES = int(os.getenv("REVIEW_CHAT_LOG_LINES", "40"))
|
||||||
|
REVIEW_CHAT_MAX_QUESTION_CHARS = int(os.getenv("REVIEW_CHAT_MAX_QUESTION_CHARS", "2000"))
|
||||||
|
# Cross-job feedback store: where review decisions and chat turns accumulate so
|
||||||
|
# a future run can be primed with "what humans corrected last time". Job-local
|
||||||
|
# artifacts stay the source of truth; this is the append-only roll-up.
|
||||||
|
# (OUTPUT_DIR is defined further down; keep this in sync with it.)
|
||||||
|
REVIEW_FEEDBACK_DIR = os.getenv("REVIEW_FEEDBACK_DIR", "") or os.path.join(
|
||||||
|
_BASE_DIR, "outputs", "_feedback")
|
||||||
|
|
||||||
# -- Hybrid (local text LLM) ----------------------------------------
|
# -- Hybrid (local text LLM) ----------------------------------------
|
||||||
# Optional OpenAI-compatible local endpoint (e.g. a vLLM box) for the text-only
|
# Optional OpenAI-compatible local endpoint (e.g. a vLLM box) for the text-only
|
||||||
# QAQC stages. Vision stages ALWAYS use OpenRouter. The user picks hybrid per
|
# QAQC stages. Vision stages ALWAYS use OpenRouter. The user picks hybrid per
|
||||||
@@ -118,8 +185,8 @@ EXTRACT_REASONING_MAX_TOKENS = int(os.getenv("EXTRACT_REASONING_MAX_TOKENS", "20
|
|||||||
# structuring pass (rung 2), then deterministic text-layer fallback stubs
|
# structuring pass (rung 2), then deterministic text-layer fallback stubs
|
||||||
# (rung 3) so no text-bearing page goes dark.
|
# (rung 3) so no text-bearing page goes dark.
|
||||||
EXTRACT_COVERAGE_FLOOR = float(os.getenv("EXTRACT_COVERAGE_FLOOR", "0.6"))
|
EXTRACT_COVERAGE_FLOOR = float(os.getenv("EXTRACT_COVERAGE_FLOOR", "0.6"))
|
||||||
EXTRACT_TEXT_RETRY_ENABLED = os.getenv("EXTRACT_TEXT_RETRY_ENABLED", "true").lower() == "true"
|
EXTRACT_TEXT_RETRY_ENABLED = _flag("EXTRACT_TEXT_RETRY_ENABLED", "true")
|
||||||
EXTRACT_FALLBACK_ENABLED = os.getenv("EXTRACT_FALLBACK_ENABLED", "true").lower() == "true"
|
EXTRACT_FALLBACK_ENABLED = _flag("EXTRACT_FALLBACK_ENABLED", "true")
|
||||||
EXTRACT_FALLBACK_MAX_OBJECTS = int(os.getenv("EXTRACT_FALLBACK_MAX_OBJECTS", "200"))
|
EXTRACT_FALLBACK_MAX_OBJECTS = int(os.getenv("EXTRACT_FALLBACK_MAX_OBJECTS", "200"))
|
||||||
REASON_MAX_TOKENS = int(os.getenv("REASON_MAX_TOKENS", "4096"))
|
REASON_MAX_TOKENS = int(os.getenv("REASON_MAX_TOKENS", "4096"))
|
||||||
|
|
||||||
@@ -191,5 +258,5 @@ SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
|
|||||||
SMTP_USER = os.getenv("SMTP_USER", "")
|
SMTP_USER = os.getenv("SMTP_USER", "")
|
||||||
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "")
|
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "")
|
||||||
SMTP_FROM = os.getenv("SMTP_FROM", "")
|
SMTP_FROM = os.getenv("SMTP_FROM", "")
|
||||||
SMTP_USE_TLS = os.getenv("SMTP_USE_TLS", "true").lower() == "true"
|
SMTP_USE_TLS = _flag("SMTP_USE_TLS", "true")
|
||||||
SMTP_USE_SSL = os.getenv("SMTP_USE_SSL", "false").lower() == "true"
|
SMTP_USE_SSL = _flag("SMTP_USE_SSL", "false")
|
||||||
@@ -23,6 +23,7 @@ import backend.jobs
|
|||||||
from backend import config, llm
|
from backend import config, llm
|
||||||
from backend.jobs import PIPELINE_MODES, create_job, get_job, _set
|
from backend.jobs import PIPELINE_MODES, create_job, get_job, _set
|
||||||
from backend.pipeline.pdf_processor import render_page_jpeg
|
from backend.pipeline.pdf_processor import render_page_jpeg
|
||||||
|
from backend.review import chat as review_chat
|
||||||
from backend.review.feedback import decision_to_label, write_label
|
from backend.review.feedback import decision_to_label, write_label
|
||||||
from backend.review.finalizer import finalize_review
|
from backend.review.finalizer import finalize_review
|
||||||
from backend.review.store import ReviewStore
|
from backend.review.store import ReviewStore
|
||||||
@@ -252,6 +253,67 @@ def finalize_review_endpoint(job_id: str):
|
|||||||
return {"status": "finalizing"}
|
return {"status": "finalizing"}
|
||||||
|
|
||||||
|
|
||||||
|
# Statuses in which the review chat may be used. The chat is read-only, so it
|
||||||
|
# stays available after finalization - a reviewer often asks "why did it say
|
||||||
|
# that?" about a report they have already sent.
|
||||||
|
_CHAT_STATES = ("needs_review", "reviewing", "finalizing", "done", "finalization_error")
|
||||||
|
|
||||||
|
|
||||||
|
def _chat_out_dir(job_id: str) -> str:
|
||||||
|
"""Resolve a job's output dir for a chat request, or raise an HTTP error."""
|
||||||
|
job = get_job(job_id)
|
||||||
|
if not job:
|
||||||
|
raise HTTPException(status_code=404, detail="Job not found")
|
||||||
|
if job.get("status") not in _CHAT_STATES:
|
||||||
|
raise HTTPException(status_code=409, detail={
|
||||||
|
"detail": f"review chat is not available for a job in status {job.get('status')}",
|
||||||
|
})
|
||||||
|
return job.get("out_dir") or os.path.join(config.OUTPUT_DIR, job_id)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/jobs/{job_id}/review-chat")
|
||||||
|
def review_chat_ask(job_id: str, payload: dict):
|
||||||
|
"""Ask one question about a finding, or about the run as a whole.
|
||||||
|
|
||||||
|
Read-only: this answers from the job's artifacts and appends to the chat
|
||||||
|
log. It never changes a finding, a decision, or the report.
|
||||||
|
"""
|
||||||
|
out_dir = _chat_out_dir(job_id)
|
||||||
|
store = ReviewStore(out_dir, create=False)
|
||||||
|
try:
|
||||||
|
turn = review_chat.ask(
|
||||||
|
job_id=job_id,
|
||||||
|
out_dir=out_dir,
|
||||||
|
question=payload.get("question"),
|
||||||
|
review_item_id=payload.get("review_item_id") or None,
|
||||||
|
queue=store.read_queue(),
|
||||||
|
decisions=store.read_decisions(),
|
||||||
|
)
|
||||||
|
except review_chat.ChatError as e:
|
||||||
|
raise HTTPException(status_code=422, detail=str(e))
|
||||||
|
except Exception as e:
|
||||||
|
# A failed model call is an upstream problem, not a bad request; the
|
||||||
|
# review screen shows it inline and the reviewer can retry.
|
||||||
|
raise HTTPException(status_code=502, detail=f"review chat failed: {e}")
|
||||||
|
return {"turn": turn}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/jobs/{job_id}/review-chat")
|
||||||
|
def review_chat_history(job_id: str, review_item_id: Optional[str] = None):
|
||||||
|
"""Logged chat turns, oldest first. Without review_item_id, all threads."""
|
||||||
|
out_dir = _chat_out_dir(job_id)
|
||||||
|
turns = review_chat.read_log(out_dir, review_item_id=review_item_id)
|
||||||
|
return {"turns": turns, "enabled": config.ENABLE_REVIEW_CHAT}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/jobs/{job_id}/review-chat/log")
|
||||||
|
def review_chat_log(job_id: str):
|
||||||
|
"""The chat log as a readable transcript: issue, questions, findings."""
|
||||||
|
out_dir = _chat_out_dir(job_id)
|
||||||
|
markdown = review_chat.render_log_markdown(review_chat.read_log(out_dir))
|
||||||
|
return Response(content=markdown, media_type="text/markdown; charset=utf-8")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/jobs/{job_id}/sheet-image/{page}")
|
@app.get("/jobs/{job_id}/sheet-image/{page}")
|
||||||
def sheet_image(job_id: str, page: int):
|
def sheet_image(job_id: str, page: int):
|
||||||
"""Render one page of a completed job's source PDF as JPEG (sheet viewer)."""
|
"""Render one page of a completed job's source PDF as JPEG (sheet viewer)."""
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
"""
|
||||||
|
drawing_integrity.py - Per-sheet Drawing Integrity QA (LLM, classic pipeline).
|
||||||
|
|
||||||
|
The drawing-focused pass: reads ONE sheet's own extracted objects + sheet image
|
||||||
|
+ deterministic text layer and flags defects internal to that single sheet
|
||||||
|
(dangling detail/callout/keynote references, schedule-vs-plan/legend
|
||||||
|
disagreements on the same sheet, dimension strings that do not sum, missing
|
||||||
|
title-block/scale essentials, duplicate/inconsistent tags). It complements the
|
||||||
|
cross-sheet conflict checker; it never does code/ADA or cross-sheet
|
||||||
|
coordination. Emits the canonical issue schema. Returns [] on failure.
|
||||||
|
|
||||||
|
Gated by config.ENABLE_DRAWING_INTEGRITY. Runs sheets concurrently, one call
|
||||||
|
per sheet, skipping sheets below INTEGRITY_MIN_ASSERTIONS.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
from backend.agents.prompts import (
|
||||||
|
DRAWING_INTEGRITY_SYSTEM_PROMPT,
|
||||||
|
DRAWING_INTEGRITY_USER_PROMPT,
|
||||||
|
)
|
||||||
|
from backend.pipeline._serialize import dumps
|
||||||
|
from backend.pipeline._stage import call_stage, collect_list, validate_issue
|
||||||
|
|
||||||
|
|
||||||
|
def _sheet_meta(sheet: Dict) -> Dict:
|
||||||
|
return {
|
||||||
|
"sheet_number": sheet.get("sheet_number"),
|
||||||
|
"sheet_title": sheet.get("sheet_title"),
|
||||||
|
"discipline": sheet.get("discipline"),
|
||||||
|
"drawing_type": sheet.get("drawing_type"),
|
||||||
|
"level": sheet.get("level"),
|
||||||
|
"scale": sheet.get("scale"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _review_sheet(sheet: Dict, page_to_b64: Dict, page_to_text: Dict) -> List[Dict]:
|
||||||
|
page_number = sheet.get("page_number")
|
||||||
|
assertions = (sheet.get("assertions") or [])[
|
||||||
|
:config.AGENT_INTEGRITY_MAX_ASSERTIONS
|
||||||
|
]
|
||||||
|
text_layer = (page_to_text.get(page_number) or "")[
|
||||||
|
:config.TEXT_LAYER_MAX_CHARS
|
||||||
|
]
|
||||||
|
image = page_to_b64.get(page_number)
|
||||||
|
images = [image][:config.AGENT_INTEGRITY_MAX_IMAGES] if image else []
|
||||||
|
parsed = call_stage(
|
||||||
|
DRAWING_INTEGRITY_SYSTEM_PROMPT,
|
||||||
|
DRAWING_INTEGRITY_USER_PROMPT,
|
||||||
|
subs={
|
||||||
|
"sheet_meta": dumps(_sheet_meta(sheet)),
|
||||||
|
"assertions": dumps(assertions),
|
||||||
|
"text_layer": text_layer,
|
||||||
|
},
|
||||||
|
images_b64=images,
|
||||||
|
max_tokens=config.INTEGRITY_MAX_TOKENS,
|
||||||
|
)
|
||||||
|
issues = collect_list(
|
||||||
|
parsed, "issues", lambda c: validate_issue(c, "drawing_integrity")
|
||||||
|
)
|
||||||
|
sheet_number = sheet.get("sheet_number")
|
||||||
|
for issue in issues:
|
||||||
|
if not issue.get("sheets") and sheet_number:
|
||||||
|
issue["sheets"] = [sheet_number]
|
||||||
|
return issues
|
||||||
|
|
||||||
|
|
||||||
|
def drawing_integrity_review(sheets: List[Dict], pages: List[Dict]) -> List[Dict]:
|
||||||
|
"""One LLM call per non-sparse sheet, run concurrently."""
|
||||||
|
if not config.ENABLE_DRAWING_INTEGRITY:
|
||||||
|
return []
|
||||||
|
page_to_b64 = {p["page_number"]: p.get("base64") for p in pages}
|
||||||
|
page_to_text = {p["page_number"]: p.get("text_layer") for p in pages}
|
||||||
|
targets = [
|
||||||
|
s for s in sheets
|
||||||
|
if len(s.get("assertions") or []) >= config.INTEGRITY_MIN_ASSERTIONS
|
||||||
|
]
|
||||||
|
issues: List[Dict] = []
|
||||||
|
if targets:
|
||||||
|
with ThreadPoolExecutor(max_workers=config.AGENT_INTEGRITY_CONCURRENCY) as pool:
|
||||||
|
for res in pool.map(
|
||||||
|
lambda s: _review_sheet(s, page_to_b64, page_to_text), targets
|
||||||
|
):
|
||||||
|
issues.extend(res)
|
||||||
|
print(f"[DrawingIntegrity] {len(issues)} issue(s) across {len(targets)} sheet(s)")
|
||||||
|
return issues
|
||||||
@@ -27,6 +27,7 @@ from typing import Dict, Optional, Callable
|
|||||||
|
|
||||||
from backend.pipeline.pdf_processor import convert_pdf_to_images
|
from backend.pipeline.pdf_processor import convert_pdf_to_images
|
||||||
from backend.pipeline.extractor import extract_assertions
|
from backend.pipeline.extractor import extract_assertions
|
||||||
|
from backend.sheet_reconcile import declared_sheet_list, reconcile_sheets
|
||||||
from backend.text_layer import attach_text_layers, coverage_gaps
|
from backend.text_layer import attach_text_layers, coverage_gaps
|
||||||
from backend.pipeline.sheet_index import classify_sheets, derive_project_meta_from_cover
|
from backend.pipeline.sheet_index import classify_sheets, derive_project_meta_from_cover
|
||||||
from backend.pipeline.jurisdiction import run_jurisdiction
|
from backend.pipeline.jurisdiction import run_jurisdiction
|
||||||
@@ -38,6 +39,7 @@ from backend.agents.disputes import annotate_clusters
|
|||||||
from backend.pipeline.conflict_checker import check_conflicts
|
from backend.pipeline.conflict_checker import check_conflicts
|
||||||
from backend.pipeline.qaqc_review import senior_review
|
from backend.pipeline.qaqc_review import senior_review
|
||||||
from backend.pipeline.code_review import code_review
|
from backend.pipeline.code_review import code_review
|
||||||
|
from backend.pipeline.drawing_integrity import drawing_integrity_review
|
||||||
from backend.pipeline.constructability import constructability_review
|
from backend.pipeline.constructability import constructability_review
|
||||||
from backend.pipeline.validator import dedup_validate
|
from backend.pipeline.validator import dedup_validate
|
||||||
from backend.pipeline.risk import score_and_prioritize
|
from backend.pipeline.risk import score_and_prioritize
|
||||||
@@ -111,6 +113,19 @@ def _run_stages(
|
|||||||
sheets = extract_assertions(pages)
|
sheets = extract_assertions(pages)
|
||||||
coverage_gaps(pages, sheets) # classic: log-only recall signal
|
coverage_gaps(pages, sheets) # classic: log-only recall signal
|
||||||
|
|
||||||
|
# Deterministic reconciliation: cover-sheet index vs identified sheets.
|
||||||
|
page_to_text = {p["page_number"]: p.get("text_layer") for p in pages}
|
||||||
|
sheet_recon = reconcile_sheets(sheets, declared_sheet_list(page_to_text))
|
||||||
|
if sheet_recon["declared_total"]:
|
||||||
|
print(f"[SheetIndex] cover declares {sheet_recon['declared_total']} "
|
||||||
|
f"sheets; {sheet_recon['found_total']} identified in set")
|
||||||
|
if sheet_recon["declared_not_in_set"]:
|
||||||
|
print(f"[SheetIndex] declared but not in set: "
|
||||||
|
f"{', '.join(sheet_recon['declared_not_in_set'][:20])}")
|
||||||
|
if sheet_recon["in_set_not_declared"]:
|
||||||
|
print(f"[SheetIndex] in set but not declared: "
|
||||||
|
f"{', '.join(sheet_recon['in_set_not_declared'][:20])}")
|
||||||
|
|
||||||
stage("Classify sheet index")
|
stage("Classify sheet index")
|
||||||
sheet_index = classify_sheets(sheets)
|
sheet_index = classify_sheets(sheets)
|
||||||
|
|
||||||
@@ -144,15 +159,23 @@ def _run_stages(
|
|||||||
stage("Full-set QAQC review")
|
stage("Full-set QAQC review")
|
||||||
qaqc_issues = senior_review(sheets, clusters, conflicts, sheet_index)
|
qaqc_issues = senior_review(sheets, clusters, conflicts, sheet_index)
|
||||||
|
|
||||||
stage("Code / ADA review")
|
if config.ENABLE_CODE_REVIEW:
|
||||||
code_issues = code_review(jurisdiction, sheets, sheet_index)
|
stage("Code / ADA review")
|
||||||
|
code_issues = code_review(jurisdiction, sheets, sheet_index)
|
||||||
|
else:
|
||||||
|
print("[Code] code/ADA review disabled (ENABLE_CODE_REVIEW=0)")
|
||||||
|
code_issues = []
|
||||||
|
|
||||||
|
stage("Drawing integrity (per-sheet QA)")
|
||||||
|
integrity_issues = drawing_integrity_review(sheets, pages)
|
||||||
|
|
||||||
stage("Constructability review")
|
stage("Constructability review")
|
||||||
construct_issues = constructability_review(sheets, clusters, conflicts)
|
construct_issues = constructability_review(sheets, clusters, conflicts)
|
||||||
|
|
||||||
stage("Validate & deduplicate")
|
stage("Validate & deduplicate")
|
||||||
conflict_issues = [v for v in (validate_issue(c, "conflict") for c in conflicts) if v]
|
conflict_issues = [v for v in (validate_issue(c, "conflict") for c in conflicts) if v]
|
||||||
all_issues = conflict_issues + qaqc_issues + code_issues + construct_issues
|
all_issues = (conflict_issues + integrity_issues + qaqc_issues
|
||||||
|
+ code_issues + construct_issues)
|
||||||
validated = dedup_validate(all_issues)
|
validated = dedup_validate(all_issues)
|
||||||
|
|
||||||
stage("Risk scoring & prioritization")
|
stage("Risk scoring & prioritization")
|
||||||
@@ -167,6 +190,7 @@ def _run_stages(
|
|||||||
report["project_input"] = merged_input
|
report["project_input"] = merged_input
|
||||||
report["jurisdiction"] = jurisdiction
|
report["jurisdiction"] = jurisdiction
|
||||||
report["sheet_index"] = sheet_index
|
report["sheet_index"] = sheet_index
|
||||||
|
report["sheet_reconciliation"] = sheet_recon
|
||||||
report["project_intelligence"] = project_intel
|
report["project_intelligence"] = project_intel
|
||||||
report["validated_issues"] = prioritized
|
report["validated_issues"] = prioritized
|
||||||
report["rfis"] = rfis
|
report["rfis"] = rfis
|
||||||
@@ -174,6 +198,7 @@ def _run_stages(
|
|||||||
"conflicts": len(conflicts),
|
"conflicts": len(conflicts),
|
||||||
"qaqc": len(qaqc_issues),
|
"qaqc": len(qaqc_issues),
|
||||||
"code": len(code_issues),
|
"code": len(code_issues),
|
||||||
|
"drawing_integrity": len(integrity_issues),
|
||||||
"constructability": len(construct_issues),
|
"constructability": len(construct_issues),
|
||||||
"validated": len(validated),
|
"validated": len(validated),
|
||||||
"rfis": len(rfis),
|
"rfis": len(rfis),
|
||||||
@@ -198,6 +223,7 @@ def _run_stages(
|
|||||||
_dump(out_dir, "project_intelligence.json", project_intel)
|
_dump(out_dir, "project_intelligence.json", project_intel)
|
||||||
_dump(out_dir, "qaqc_issues.json", qaqc_issues)
|
_dump(out_dir, "qaqc_issues.json", qaqc_issues)
|
||||||
_dump(out_dir, "code_issues.json", code_issues)
|
_dump(out_dir, "code_issues.json", code_issues)
|
||||||
|
_dump(out_dir, "drawing_integrity.json", integrity_issues)
|
||||||
_dump(out_dir, "constructability.json", construct_issues)
|
_dump(out_dir, "constructability.json", construct_issues)
|
||||||
_dump(out_dir, "validated_issues.json", prioritized)
|
_dump(out_dir, "validated_issues.json", prioritized)
|
||||||
_dump(out_dir, "rfis.json", rfis)
|
_dump(out_dir, "rfis.json", rfis)
|
||||||
|
|||||||
+10
-10
@@ -410,24 +410,24 @@ Normalized assertions: {normalized_assertions}"""
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
CONFLICT_SYSTEM_PROMPT = """You are a Senior Architect and construction-drawing coordination reviewer doing a back-check of a drawing set BEFORE it is issued for bid, permit, or construction.
|
CONFLICT_SYSTEM_PROMPT = """You are a Senior Architect and construction-drawing coordination reviewer doing a back-check of a drawing set BEFORE it is issued for bid, permit, or construction.
|
||||||
You are given clustered facts that multiple disciplines have asserted about the same location or element.
|
You are given clustered facts asserted about the same location or element. Those facts may come from MULTIPLE disciplines, from a SINGLE discipline across several sheets, or from ONE sheet (plan vs schedule vs detail vs keynote on that sheet).
|
||||||
Decide whether these disciplines GENUINELY CONTRADICT each other - the kind of issue a human coordinator would issue as a QAQC comment or RFI before the set goes out.
|
Decide whether these facts GENUINELY CONTRADICT each other - the kind of issue a human coordinator would issue as a QAQC comment or RFI before the set goes out. A contradiction between two facts is a conflict whether or not the two facts come from different disciplines.
|
||||||
You are NOT performing code review in this stage. You are NOT checking ADA in this stage. You are NOT estimating cost or scope. You are NOT rewriting the drawings.
|
You are NOT performing code review in this stage. You are NOT checking ADA in this stage. You are NOT estimating cost or scope. You are NOT rewriting the drawings.
|
||||||
What IS a conflict:
|
What IS a conflict:
|
||||||
- Two disciplines state different values for the same physical quantity at the same place.
|
- Two facts state different values for the same physical quantity at the same place (across disciplines, across sheets of one discipline, or on the same sheet).
|
||||||
- An element is shown in different locations by different disciplines.
|
- An element is shown in different locations by different facts.
|
||||||
- A schedule disagrees with what is drawn on the plan.
|
- A schedule disagrees with what is drawn on the plan (even on the same sheet).
|
||||||
- A detail disagrees with the plan.
|
- A detail disagrees with the plan.
|
||||||
- A keynote disagrees with a schedule, plan, or detail.
|
- A keynote or general note disagrees with a schedule, plan, detail, or legend - including a keynote/legend mismatch on a single sheet.
|
||||||
|
- A callout, detail reference, section marker, or tag references something that does not exist (a dangling reference).
|
||||||
|
- The same room, door, equipment, wall, or utility is labeled or dimensioned inconsistently across sheets or within one sheet.
|
||||||
- An element required by one discipline has no counterpart where another discipline should show it.
|
- An element required by one discipline has no counterpart where another discipline should show it.
|
||||||
- A duct, pipe, conduit, or piece of equipment conflicts with structure, ceiling height, rated wall, or required clearance.
|
- A duct, pipe, conduit, or piece of equipment conflicts with structure, ceiling height, rated wall, or required clearance.
|
||||||
- Equipment shown by one discipline lacks required power, plumbing, ventilation, access, or support in another discipline.
|
- Equipment shown by one discipline lacks required power, plumbing, ventilation, access, or support in another discipline.
|
||||||
- Demolition drawings remove something that new work drawings keep without explanation.
|
- Demolition drawings remove something that new work drawings keep without explanation.
|
||||||
- A callout, keynote, or tag references something that does not exist.
|
|
||||||
- The same room, door, equipment, wall, or utility is labeled inconsistently across sheets.
|
|
||||||
What is NOT a conflict:
|
What is NOT a conflict:
|
||||||
- Two disciplines describing different, compatible aspects of the same place.
|
- Two facts describing different, compatible aspects of the same place.
|
||||||
- A value shown on one discipline and simply not repeated on another, unless that discipline is expected to show it.
|
- A value shown once and simply not repeated elsewhere, unless another sheet or discipline is expected to show it.
|
||||||
- Rounding or representation differences that resolve to the same real value.
|
- Rounding or representation differences that resolve to the same real value.
|
||||||
- A possible code issue.
|
- A possible code issue.
|
||||||
- A design preference.
|
- A design preference.
|
||||||
|
|||||||
@@ -0,0 +1,368 @@
|
|||||||
|
"""Review-screen chat: ask the run why it concluded something.
|
||||||
|
|
||||||
|
Read-only by construction. The chat reads job artifacts, calls one LLM, and
|
||||||
|
appends a log record; it never mutates findings, review decisions, or the
|
||||||
|
report, and the prompt forbids it from emitting code or config changes.
|
||||||
|
|
||||||
|
Every turn is logged twice, on purpose:
|
||||||
|
|
||||||
|
- ``<out_dir>/review/chat_log.jsonl`` - job-local, the auditable record of what
|
||||||
|
was asked about which finding and what came back.
|
||||||
|
- ``REVIEW_FEEDBACK_DIR/chat_turns.jsonl`` - cross-job, append-only, so the
|
||||||
|
corrections a reviewer makes in conversation ("that is not a floor drain, it
|
||||||
|
is a power floor box") accumulate somewhere a future run can be primed from.
|
||||||
|
Nothing reads this yet; writing it is what makes that possible later.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
from backend.llm import call_json
|
||||||
|
from backend.pipeline._serialize import dumps
|
||||||
|
from backend.review.chat_context import build_context
|
||||||
|
from backend.review.chat_prompts import (
|
||||||
|
REVIEW_CHAT_SYSTEM_PROMPT,
|
||||||
|
REVIEW_CHAT_USER_PROMPT,
|
||||||
|
)
|
||||||
|
from backend.review.feedback import append_shared_feedback
|
||||||
|
|
||||||
|
_ANSWERABLE = {"yes", "partial", "no"}
|
||||||
|
_ASSESSMENTS = {"looks_supported", "looks_unsupported", "cannot_tell", "not_applicable"}
|
||||||
|
_CONFIDENCE = {"high", "medium", "low"}
|
||||||
|
_MAX_FINDINGS = 12
|
||||||
|
_MAX_EVIDENCE = 12
|
||||||
|
|
||||||
|
|
||||||
|
class ChatError(Exception):
|
||||||
|
"""Raised for a caller-fixable problem (bad question, chat disabled)."""
|
||||||
|
|
||||||
|
|
||||||
|
def _now() -> str:
|
||||||
|
return datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def _one_of(value: Any, allowed: set, default: str) -> str:
|
||||||
|
text = str(value or "").strip().lower()
|
||||||
|
return text if text in allowed else default
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_question(raw: Any) -> str:
|
||||||
|
question = str(raw or "").strip()
|
||||||
|
if not question:
|
||||||
|
raise ChatError("question is required")
|
||||||
|
if len(question) > config.REVIEW_CHAT_MAX_QUESTION_CHARS:
|
||||||
|
raise ChatError(
|
||||||
|
f"question is too long (max {config.REVIEW_CHAT_MAX_QUESTION_CHARS} characters)")
|
||||||
|
return question
|
||||||
|
|
||||||
|
|
||||||
|
def _log_path(out_dir: str) -> str:
|
||||||
|
return os.path.join(out_dir, "review", "chat_log.jsonl")
|
||||||
|
|
||||||
|
|
||||||
|
def read_log(out_dir: str, review_item_id: Optional[str] = None,
|
||||||
|
scope_only: bool = False) -> List[Dict[str, Any]]:
|
||||||
|
"""Chat turns for this job, oldest first.
|
||||||
|
|
||||||
|
``review_item_id`` filters to one finding's thread; with ``scope_only`` and
|
||||||
|
no id, returns only the run-scope turns. Corrupt lines are skipped rather
|
||||||
|
than failing the read - a truncated log must not hide the rest.
|
||||||
|
"""
|
||||||
|
path = _log_path(out_dir)
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
return []
|
||||||
|
turns: List[Dict[str, Any]] = []
|
||||||
|
try:
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
turn = json.loads(line)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
if not isinstance(turn, dict):
|
||||||
|
continue
|
||||||
|
if review_item_id is not None:
|
||||||
|
if turn.get("review_item_id") != review_item_id:
|
||||||
|
continue
|
||||||
|
elif scope_only and turn.get("review_item_id") is not None:
|
||||||
|
continue
|
||||||
|
turns.append(turn)
|
||||||
|
except OSError:
|
||||||
|
return []
|
||||||
|
return turns
|
||||||
|
|
||||||
|
|
||||||
|
def _append_log(out_dir: str, turn: Dict[str, Any]) -> None:
|
||||||
|
"""Append one turn as a JSON line; never raises on I/O failure."""
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.join(out_dir, "review"), exist_ok=True)
|
||||||
|
with open(_log_path(out_dir), "a", encoding="utf-8") as f:
|
||||||
|
f.write(json.dumps(turn) + "\n")
|
||||||
|
except OSError as e:
|
||||||
|
print(f"[ReviewChat] chat log write failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def _issue_snapshot(item: Optional[Dict]) -> Optional[Dict[str, Any]]:
|
||||||
|
"""The issue as it stood when asked about - the log's 'issue in question'.
|
||||||
|
|
||||||
|
Copied rather than referenced by id so the log stays readable after
|
||||||
|
finalization renumbers or suppresses the finding.
|
||||||
|
"""
|
||||||
|
if not item:
|
||||||
|
return None
|
||||||
|
payload = item.get("payload") or {}
|
||||||
|
if item.get("kind") == "clean_cluster":
|
||||||
|
return {
|
||||||
|
"review_item_id": item.get("review_item_id"),
|
||||||
|
"kind": item.get("kind"),
|
||||||
|
"cluster_key": payload.get("key"),
|
||||||
|
"location": payload.get("location"),
|
||||||
|
"disciplines": payload.get("disciplines"),
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
"review_item_id": item.get("review_item_id"),
|
||||||
|
"kind": item.get("kind"),
|
||||||
|
"issue_id": payload.get("issue_id"),
|
||||||
|
"source_stage": payload.get("source_stage"),
|
||||||
|
"category": payload.get("category"),
|
||||||
|
"severity": payload.get("severity"),
|
||||||
|
"confidence": payload.get("confidence"),
|
||||||
|
"location": payload.get("location"),
|
||||||
|
"disciplines": payload.get("disciplines"),
|
||||||
|
"sheets": payload.get("sheets"),
|
||||||
|
"description": payload.get("description"),
|
||||||
|
"blocking": item.get("blocking"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _history_block(turns: List[Dict[str, Any]]) -> str:
|
||||||
|
if not turns:
|
||||||
|
return ""
|
||||||
|
recent = turns[-config.REVIEW_CHAT_HISTORY_TURNS:]
|
||||||
|
lines = ["Earlier turns in this thread (oldest first):"]
|
||||||
|
for turn in recent:
|
||||||
|
lines.append(f"Reviewer: {turn.get('question') or ''}")
|
||||||
|
lines.append(f"You: {turn.get('answer') or ''}")
|
||||||
|
lines.append("")
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_answer(parsed: Optional[Dict]) -> Optional[Dict[str, Any]]:
|
||||||
|
"""Coerce the model's JSON into the log/API shape, or None if unusable."""
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
return None
|
||||||
|
answer = str(parsed.get("answer") or "").strip()
|
||||||
|
if not answer:
|
||||||
|
return None
|
||||||
|
findings = [
|
||||||
|
str(item).strip()
|
||||||
|
for item in (parsed.get("findings") or [])
|
||||||
|
if isinstance(item, (str, int, float)) and str(item).strip()
|
||||||
|
][:_MAX_FINDINGS]
|
||||||
|
evidence = []
|
||||||
|
for item in (parsed.get("evidence_cited") or [])[:_MAX_EVIDENCE]:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
evidence.append({
|
||||||
|
"artifact": str(item.get("artifact") or "").strip() or None,
|
||||||
|
"sheet": item.get("sheet"),
|
||||||
|
"quote": str(item.get("quote") or "").strip() or None,
|
||||||
|
"why_it_matters": str(item.get("why_it_matters") or "").strip() or None,
|
||||||
|
})
|
||||||
|
correction = parsed.get("suggested_category_correction")
|
||||||
|
correction = str(correction).strip() if correction else ""
|
||||||
|
missing = parsed.get("missing_information")
|
||||||
|
return {
|
||||||
|
"answer": answer,
|
||||||
|
"findings": findings,
|
||||||
|
"evidence_cited": evidence,
|
||||||
|
"answerable": _one_of(parsed.get("answerable"), _ANSWERABLE, "partial"),
|
||||||
|
"missing_information": str(missing).strip() if missing else None,
|
||||||
|
"assessment_of_finding": _one_of(parsed.get("assessment_of_finding"),
|
||||||
|
_ASSESSMENTS, "cannot_tell"),
|
||||||
|
# The feedback signal: a reviewer correcting a misidentification in
|
||||||
|
# conversation ("that is a power floor box") lands here as structured
|
||||||
|
# data instead of dying in free text.
|
||||||
|
"suggested_category_correction": correction or None,
|
||||||
|
"confidence": _one_of(parsed.get("confidence"), _CONFIDENCE, "low"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _feedback_record(turn: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Cross-job roll-up of one turn: metadata + the correction signal.
|
||||||
|
|
||||||
|
Mirrors the privacy stance of the decision labels - no images, no raw sheet
|
||||||
|
dumps. The question and answer ARE carried, because a chat turn without its
|
||||||
|
question is not usable as feedback; keep this store job-internal.
|
||||||
|
"""
|
||||||
|
issue = turn.get("issue") or {}
|
||||||
|
return {
|
||||||
|
"kind": "review_chat_turn",
|
||||||
|
"turn_id": turn.get("turn_id"),
|
||||||
|
"job_id": turn.get("job_id"),
|
||||||
|
"created_at": turn.get("created_at"),
|
||||||
|
"review_item_id": turn.get("review_item_id"),
|
||||||
|
"scope": turn.get("scope"),
|
||||||
|
"issue_id": issue.get("issue_id"),
|
||||||
|
"source_stage": issue.get("source_stage"),
|
||||||
|
"category": issue.get("category"),
|
||||||
|
"severity": issue.get("severity"),
|
||||||
|
"confidence": issue.get("confidence"),
|
||||||
|
"sheets": issue.get("sheets"),
|
||||||
|
"question": turn.get("question"),
|
||||||
|
"answer": turn.get("answer"),
|
||||||
|
"findings": turn.get("findings"),
|
||||||
|
"assessment_of_finding": turn.get("assessment_of_finding"),
|
||||||
|
"suggested_category_correction": turn.get("suggested_category_correction"),
|
||||||
|
"answerable": turn.get("answerable"),
|
||||||
|
"model": turn.get("model"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def ask(job_id: str, out_dir: str, question: str,
|
||||||
|
review_item_id: Optional[str] = None,
|
||||||
|
queue: Optional[List[Dict]] = None,
|
||||||
|
decisions: Optional[Dict[str, Dict]] = None) -> Dict[str, Any]:
|
||||||
|
"""Answer one reviewer question and log the turn.
|
||||||
|
|
||||||
|
Returns the logged turn. Raises ChatError for a bad question or a disabled
|
||||||
|
chat, and RuntimeError when the model call fails outright (the caller maps
|
||||||
|
both to HTTP status codes).
|
||||||
|
"""
|
||||||
|
if not config.ENABLE_REVIEW_CHAT:
|
||||||
|
raise ChatError("review chat is disabled (ENABLE_REVIEW_CHAT=false)")
|
||||||
|
question = _clean_question(question)
|
||||||
|
queue = queue or []
|
||||||
|
|
||||||
|
item = next((candidate for candidate in queue
|
||||||
|
if candidate.get("review_item_id") == review_item_id), None)
|
||||||
|
if review_item_id and item is None:
|
||||||
|
raise ChatError(f"unknown review_item_id {review_item_id!r}")
|
||||||
|
|
||||||
|
context = build_context(out_dir, review_item_id, queue, decisions, question)
|
||||||
|
history = read_log(out_dir, review_item_id=review_item_id) if review_item_id \
|
||||||
|
else read_log(out_dir, scope_only=True)
|
||||||
|
|
||||||
|
scope_line = (
|
||||||
|
f"Scope: this question is about review item {review_item_id}."
|
||||||
|
if item else
|
||||||
|
"Scope: this question is about the run as a whole, not one finding."
|
||||||
|
)
|
||||||
|
user_text = (REVIEW_CHAT_USER_PROMPT
|
||||||
|
.replace("{scope_line}", scope_line)
|
||||||
|
.replace("{question}", question)
|
||||||
|
.replace("{history_block}", _history_block(history))
|
||||||
|
.replace("{context}", dumps(context)))
|
||||||
|
|
||||||
|
parsed = call_json(
|
||||||
|
system_prompt=REVIEW_CHAT_SYSTEM_PROMPT,
|
||||||
|
user_text=user_text,
|
||||||
|
max_tokens=config.REVIEW_CHAT_MAX_TOKENS,
|
||||||
|
model=config.REVIEW_CHAT_MODEL,
|
||||||
|
usage_stage="review.chat",
|
||||||
|
)
|
||||||
|
answer = _normalize_answer(parsed)
|
||||||
|
if answer is None:
|
||||||
|
raise RuntimeError("the model did not return a usable answer")
|
||||||
|
|
||||||
|
turn = {
|
||||||
|
"turn_id": uuid.uuid4().hex[:12],
|
||||||
|
"job_id": job_id,
|
||||||
|
"created_at": _now(),
|
||||||
|
"review_item_id": review_item_id,
|
||||||
|
"scope": context.get("scope"),
|
||||||
|
"issue": _issue_snapshot(item),
|
||||||
|
"question": question,
|
||||||
|
"reviewer_decision_at_time": context.get("reviewer_decision_so_far"),
|
||||||
|
"artifacts_consulted": sorted(
|
||||||
|
name for name, present
|
||||||
|
in (context.get("artifacts_available") or {}).items() if present
|
||||||
|
),
|
||||||
|
"model": config.REVIEW_CHAT_MODEL,
|
||||||
|
**answer,
|
||||||
|
}
|
||||||
|
_append_log(out_dir, turn)
|
||||||
|
append_shared_feedback(_feedback_record(turn))
|
||||||
|
return turn
|
||||||
|
|
||||||
|
|
||||||
|
def render_log_markdown(turns: List[Dict[str, Any]]) -> str:
|
||||||
|
"""Human-readable transcript: the issue, the questions, the findings.
|
||||||
|
|
||||||
|
Grouped by review item so one finding's whole thread reads together, with
|
||||||
|
run-scope questions last under their own heading.
|
||||||
|
"""
|
||||||
|
by_item: Dict[str, List[Dict[str, Any]]] = {}
|
||||||
|
for turn in turns:
|
||||||
|
by_item.setdefault(turn.get("review_item_id") or "", []).append(turn)
|
||||||
|
|
||||||
|
lines = ["# Review chat log", ""]
|
||||||
|
if not turns:
|
||||||
|
lines.append("_No questions have been asked about this run._")
|
||||||
|
return "\n".join(lines) + "\n"
|
||||||
|
lines.append(f"{len(turns)} turn(s) across {len(by_item)} thread(s).")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
for item_id in sorted(by_item, key=lambda key: (key == "", key)):
|
||||||
|
item_turns = by_item[item_id]
|
||||||
|
issue = next((turn.get("issue") for turn in item_turns if turn.get("issue")), None)
|
||||||
|
if not item_id:
|
||||||
|
lines += ["## Run-scope questions", "",
|
||||||
|
"_Not about a single finding._", ""]
|
||||||
|
elif issue:
|
||||||
|
lines.append(f"## {issue.get('issue_id') or item_id}")
|
||||||
|
lines.append("")
|
||||||
|
meta = [
|
||||||
|
("Category", issue.get("category")),
|
||||||
|
("Severity", issue.get("severity")),
|
||||||
|
("Run confidence", issue.get("confidence")),
|
||||||
|
("Location", issue.get("location")),
|
||||||
|
("Sheets", ", ".join(str(s) for s in issue.get("sheets") or []) or None),
|
||||||
|
("Stage", issue.get("source_stage")),
|
||||||
|
]
|
||||||
|
for label, value in meta:
|
||||||
|
if value:
|
||||||
|
lines.append(f"- **{label}:** {value}")
|
||||||
|
if issue.get("description"):
|
||||||
|
lines += ["", f"> {issue['description']}"]
|
||||||
|
lines.append("")
|
||||||
|
else:
|
||||||
|
lines += [f"## {item_id}", ""]
|
||||||
|
|
||||||
|
for turn in item_turns:
|
||||||
|
lines.append(f"### Q ({turn.get('created_at') or ''})")
|
||||||
|
lines += ["", turn.get("question") or "", "", "**Answer**", "",
|
||||||
|
turn.get("answer") or "", ""]
|
||||||
|
if turn.get("findings"):
|
||||||
|
lines.append("**Findings**")
|
||||||
|
lines.append("")
|
||||||
|
lines += [f"- {finding}" for finding in turn["findings"]]
|
||||||
|
lines.append("")
|
||||||
|
if turn.get("evidence_cited"):
|
||||||
|
lines += ["**Evidence cited**", ""]
|
||||||
|
for item in turn["evidence_cited"]:
|
||||||
|
where = item.get("artifact") or "?"
|
||||||
|
sheet = f" ({item['sheet']})" if item.get("sheet") else ""
|
||||||
|
quote = item.get("quote") or ""
|
||||||
|
lines.append(f"- `{where}`{sheet}: \"{quote}\"")
|
||||||
|
if item.get("why_it_matters"):
|
||||||
|
lines.append(f" - {item['why_it_matters']}")
|
||||||
|
lines.append("")
|
||||||
|
tail = [
|
||||||
|
("Answerable", turn.get("answerable")),
|
||||||
|
("Assessment", turn.get("assessment_of_finding")),
|
||||||
|
("Confidence", turn.get("confidence")),
|
||||||
|
("Missing", turn.get("missing_information")),
|
||||||
|
("Suggested correction", turn.get("suggested_category_correction")),
|
||||||
|
("Model", turn.get("model")),
|
||||||
|
]
|
||||||
|
lines.append(" | ".join(f"{label}: {value}" for label, value in tail if value))
|
||||||
|
lines.append("")
|
||||||
|
return "\n".join(lines) + "\n"
|
||||||
@@ -0,0 +1,315 @@
|
|||||||
|
"""Evidence bundles for the review chat.
|
||||||
|
|
||||||
|
The chat is an explainer, not an investigator: it may only answer from what the
|
||||||
|
run actually produced. This module assembles that material from the job's own
|
||||||
|
artifacts and hands the model a bounded, slimmed view.
|
||||||
|
|
||||||
|
Two shapes, matching the two kinds of question a reviewer asks:
|
||||||
|
|
||||||
|
- item scope ("why does it think the AC unit is on the ground?") - the finding,
|
||||||
|
its evidence, the cluster the finding came from, the sheets those assertions
|
||||||
|
were extracted from, any wave-5b verification verdict, the Brain's merge/drop
|
||||||
|
decision, and the reviewer's own saved decision.
|
||||||
|
- run scope ("why didn't it pick up the Civil set?") - the sheet index by
|
||||||
|
discipline, the deterministic cover-index reconciliation, per-stage counts,
|
||||||
|
what got suppressed and why, and matching job.log lines.
|
||||||
|
|
||||||
|
Everything here is read-only and degrades to empty on a missing or corrupt
|
||||||
|
artifact; a chat request must never be the thing that breaks a review screen.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
|
||||||
|
# Assertion/evidence text is quoted back verbatim so the reviewer can check the
|
||||||
|
# answer against the sheet, but a whole cluster of them would swamp the prompt.
|
||||||
|
_MAX_CLUSTER_ASSERTIONS = 40
|
||||||
|
_MAX_SHEET_ASSERTIONS = 25
|
||||||
|
_MAX_SOURCE_TEXT_CHARS = 400
|
||||||
|
_MAX_SHEETS_IN_ROSTER = 400
|
||||||
|
_MAX_SUPPRESSED = 25
|
||||||
|
_MAX_LOG_LINE_CHARS = 400
|
||||||
|
|
||||||
|
|
||||||
|
def _read_json(path: str, default):
|
||||||
|
try:
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
except (OSError, json.JSONDecodeError):
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def _truncate(value: Any, limit: int = _MAX_SOURCE_TEXT_CHARS) -> Any:
|
||||||
|
if not isinstance(value, str) or len(value) <= limit:
|
||||||
|
return value
|
||||||
|
return value[:limit] + "..."
|
||||||
|
|
||||||
|
|
||||||
|
def _slim_assertion(assertion: Dict) -> Dict:
|
||||||
|
"""Drop base64/bookkeeping; keep what explains where a value came from."""
|
||||||
|
out = {
|
||||||
|
"sheet_number": assertion.get("sheet_number"),
|
||||||
|
"discipline": assertion.get("discipline"),
|
||||||
|
"attribute": assertion.get("attribute"),
|
||||||
|
"value": assertion.get("value"),
|
||||||
|
"source_text": _truncate(assertion.get("source_text")),
|
||||||
|
"location_key": assertion.get("location_key"),
|
||||||
|
"normalized_value": assertion.get("normalized_value"),
|
||||||
|
"disputed": assertion.get("disputed"),
|
||||||
|
}
|
||||||
|
return {key: value for key, value in out.items() if value is not None}
|
||||||
|
|
||||||
|
|
||||||
|
def _slim_finding(finding: Dict) -> Dict:
|
||||||
|
"""The finding as the run recorded it, including how it was checked."""
|
||||||
|
out = {
|
||||||
|
"issue_id": finding.get("issue_id"),
|
||||||
|
"source_stage": finding.get("source_stage"),
|
||||||
|
"agent": finding.get("agent"),
|
||||||
|
"category": finding.get("category"),
|
||||||
|
"severity": finding.get("severity"),
|
||||||
|
"confidence": finding.get("confidence"),
|
||||||
|
"location": finding.get("location"),
|
||||||
|
"disciplines": finding.get("disciplines"),
|
||||||
|
"sheets": finding.get("sheets"),
|
||||||
|
"description": _truncate(finding.get("description"), 1200),
|
||||||
|
"recommended_resolution": finding.get("recommended_resolution"),
|
||||||
|
"code_reference": finding.get("code_reference"),
|
||||||
|
"risk_score": finding.get("risk_score"),
|
||||||
|
"recommended_priority": finding.get("recommended_priority"),
|
||||||
|
"scope_id": finding.get("scope_id"),
|
||||||
|
"evidence": [
|
||||||
|
{
|
||||||
|
"discipline": item.get("discipline"),
|
||||||
|
"sheet": item.get("sheet"),
|
||||||
|
"source_text": _truncate(item.get("source_text")),
|
||||||
|
"asserted_value": item.get("asserted_value"),
|
||||||
|
}
|
||||||
|
for item in (finding.get("evidence") or [])
|
||||||
|
if isinstance(item, dict)
|
||||||
|
],
|
||||||
|
# Wave 5b / Brain-clarify re-checked some findings against fresh sheet
|
||||||
|
# images + the text layer. When present this is the single best answer
|
||||||
|
# to "did it actually look again?", so it is never dropped.
|
||||||
|
"verification": finding.get("verification"),
|
||||||
|
"clarification_of": finding.get("clarification_of"),
|
||||||
|
}
|
||||||
|
return {key: value for key, value in out.items() if value is not None}
|
||||||
|
|
||||||
|
|
||||||
|
def _slim_sheet(sheet: Dict, limit: int = _MAX_SHEET_ASSERTIONS) -> Dict:
|
||||||
|
assertions = sheet.get("assertions") or []
|
||||||
|
out = {
|
||||||
|
"sheet_number": sheet.get("sheet_number"),
|
||||||
|
"sheet_title": sheet.get("sheet_title"),
|
||||||
|
"discipline": sheet.get("discipline"),
|
||||||
|
"level": sheet.get("level"),
|
||||||
|
"page_number": sheet.get("page_number"),
|
||||||
|
"assertion_count": len(assertions),
|
||||||
|
"assertions": [_slim_assertion(item) for item in assertions[:limit]],
|
||||||
|
}
|
||||||
|
if len(assertions) > limit:
|
||||||
|
out["assertions_omitted"] = len(assertions) - limit
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _discipline_roster(sheet_index: Dict, sheets: List[Dict]) -> Dict[str, List[str]]:
|
||||||
|
"""Sheet numbers grouped by discipline - the 'is Civil in here?' answer.
|
||||||
|
|
||||||
|
Built from the classified sheet index when there is one, falling back to
|
||||||
|
raw extraction, so an empty/failed index stage does not read as "no sheets".
|
||||||
|
"""
|
||||||
|
entries = (sheet_index or {}).get("sheet_index") or []
|
||||||
|
if not entries:
|
||||||
|
entries = [
|
||||||
|
{"sheet_number": sheet.get("sheet_number"),
|
||||||
|
"discipline": sheet.get("discipline")}
|
||||||
|
for sheet in sheets or []
|
||||||
|
]
|
||||||
|
roster: Dict[str, List[str]] = {}
|
||||||
|
for entry in entries:
|
||||||
|
if not isinstance(entry, dict):
|
||||||
|
continue
|
||||||
|
discipline = str(entry.get("discipline") or "unknown")
|
||||||
|
number = entry.get("sheet_number") or entry.get("sheet_id") or "?"
|
||||||
|
bucket = roster.setdefault(discipline, [])
|
||||||
|
if len(bucket) < _MAX_SHEETS_IN_ROSTER and number not in bucket:
|
||||||
|
bucket.append(str(number))
|
||||||
|
return roster
|
||||||
|
|
||||||
|
|
||||||
|
def _log_excerpt(out_dir: str, terms: List[str], limit: int) -> List[str]:
|
||||||
|
"""job.log lines mentioning any search term, newest last.
|
||||||
|
|
||||||
|
The run log is where stage skips, retries, and coverage decisions are
|
||||||
|
recorded ("[Code] gated off", "[Extract] page 12 empty"), which is often
|
||||||
|
the literal answer to "why didn't it look at X".
|
||||||
|
"""
|
||||||
|
path = os.path.join(out_dir, "job.log")
|
||||||
|
needles = [term.lower() for term in terms if term and len(str(term)) >= 2]
|
||||||
|
if not needles or not os.path.isfile(path):
|
||||||
|
return []
|
||||||
|
hits: List[str] = []
|
||||||
|
try:
|
||||||
|
with open(path, encoding="utf-8", errors="replace") as f:
|
||||||
|
for line in f:
|
||||||
|
lowered = line.lower()
|
||||||
|
if any(needle in lowered for needle in needles):
|
||||||
|
hits.append(_truncate(line.rstrip("\n"), _MAX_LOG_LINE_CHARS))
|
||||||
|
except OSError:
|
||||||
|
return []
|
||||||
|
return hits[-limit:]
|
||||||
|
|
||||||
|
|
||||||
|
def _stage_terms(question: str) -> List[str]:
|
||||||
|
"""Search terms for the log: quoted sheet-ish tokens plus long words.
|
||||||
|
|
||||||
|
Deliberately crude - this only decides which log lines get shown, and an
|
||||||
|
over-broad match is bounded by REVIEW_CHAT_LOG_LINES anyway.
|
||||||
|
"""
|
||||||
|
tokens = re.findall(r"[A-Za-z][A-Za-z0-9.\-]{2,}", question or "")
|
||||||
|
stop = {"the", "why", "did", "not", "and", "for", "was", "were", "does",
|
||||||
|
"this", "that", "with", "from", "what", "how", "you", "its",
|
||||||
|
"it's", "there", "when", "have", "has", "any", "are", "but"}
|
||||||
|
return [token for token in tokens if token.lower() not in stop][:12]
|
||||||
|
|
||||||
|
|
||||||
|
def build_context(out_dir: str, review_item_id: Optional[str],
|
||||||
|
queue: Optional[List[Dict]] = None,
|
||||||
|
decisions: Optional[Dict[str, Dict]] = None,
|
||||||
|
question: str = "") -> Dict[str, Any]:
|
||||||
|
"""Assemble the evidence bundle for one chat turn.
|
||||||
|
|
||||||
|
``review_item_id`` selects item scope; None (or an id not in the queue)
|
||||||
|
gives run scope. Missing artifacts degrade to empty sections rather than
|
||||||
|
raising - the model is told what is missing via ``artifacts_available``.
|
||||||
|
"""
|
||||||
|
report = _read_json(os.path.join(out_dir, "conflicts.json"), {}) or {}
|
||||||
|
snapshot = _read_json(os.path.join(out_dir, "agent", "memory.json"), {}) or {}
|
||||||
|
summary = report.get("summary") or {}
|
||||||
|
sheets = snapshot.get("sheets") or []
|
||||||
|
sheet_index = report.get("sheet_index") or snapshot.get("sheet_index") or {}
|
||||||
|
|
||||||
|
context: Dict[str, Any] = {
|
||||||
|
"scope": "run",
|
||||||
|
"run": {
|
||||||
|
"source": report.get("source"),
|
||||||
|
"pipeline_mode": summary.get("pipeline_mode"),
|
||||||
|
"agent_status": summary.get("agent_status"),
|
||||||
|
"sheets_analyzed": summary.get("sheets_analyzed") or len(sheets),
|
||||||
|
"by_stage": summary.get("by_stage"),
|
||||||
|
"conflicts_found": summary.get("conflicts_found"),
|
||||||
|
"by_severity": summary.get("by_severity"),
|
||||||
|
"models_used": summary.get("models_used"),
|
||||||
|
# Stage gating is the answer to a whole class of "why didn't it
|
||||||
|
# check X" questions, so it is stated rather than left implied.
|
||||||
|
"code_review_enabled": config.ENABLE_CODE_REVIEW,
|
||||||
|
},
|
||||||
|
"sheets_by_discipline": _discipline_roster(sheet_index, sheets),
|
||||||
|
"sheet_reconciliation": report.get("sheet_reconciliation"),
|
||||||
|
"missing_expected_sheets": (sheet_index or {}).get("missing_expected_sheets"),
|
||||||
|
"suppressed_by_the_run": [
|
||||||
|
{
|
||||||
|
"issue_id": item.get("issue_id"),
|
||||||
|
"category": item.get("category"),
|
||||||
|
"description": _truncate(item.get("description"), 300),
|
||||||
|
"verification": item.get("verification"),
|
||||||
|
}
|
||||||
|
for item in (snapshot.get("suppressed") or [])[:_MAX_SUPPRESSED]
|
||||||
|
if isinstance(item, dict)
|
||||||
|
],
|
||||||
|
"artifacts_available": {
|
||||||
|
"conflicts.json": bool(report),
|
||||||
|
"agent/memory.json": bool(snapshot),
|
||||||
|
"job.log": os.path.isfile(os.path.join(out_dir, "job.log")),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
item = None
|
||||||
|
for candidate in queue or []:
|
||||||
|
if candidate.get("review_item_id") == review_item_id:
|
||||||
|
item = candidate
|
||||||
|
break
|
||||||
|
if item is None:
|
||||||
|
context["log_excerpt"] = _log_excerpt(
|
||||||
|
out_dir, _stage_terms(question), config.REVIEW_CHAT_LOG_LINES)
|
||||||
|
return context
|
||||||
|
|
||||||
|
context["scope"] = "item"
|
||||||
|
payload = item.get("payload") or {}
|
||||||
|
context["review_item"] = {
|
||||||
|
"review_item_id": item.get("review_item_id"),
|
||||||
|
"kind": item.get("kind"),
|
||||||
|
"blocking": item.get("blocking"),
|
||||||
|
"review_triggers": item.get("reasons"),
|
||||||
|
}
|
||||||
|
saved = (decisions or {}).get(review_item_id) or {}
|
||||||
|
if saved:
|
||||||
|
context["reviewer_decision_so_far"] = {
|
||||||
|
"decision": saved.get("decision"),
|
||||||
|
"reason_code": saved.get("reason_code"),
|
||||||
|
"comment": _truncate(saved.get("comment")),
|
||||||
|
}
|
||||||
|
|
||||||
|
if item.get("kind") == "clean_cluster":
|
||||||
|
context["cluster"] = {
|
||||||
|
"key": payload.get("key"),
|
||||||
|
"location": payload.get("location"),
|
||||||
|
"disciplines": payload.get("disciplines"),
|
||||||
|
"kind": payload.get("kind"),
|
||||||
|
"assertions": [_slim_assertion(a)
|
||||||
|
for a in (payload.get("assertions") or [])[:_MAX_CLUSTER_ASSERTIONS]],
|
||||||
|
}
|
||||||
|
cited_sheets = [a.get("sheet_number") for a in payload.get("assertions") or []]
|
||||||
|
else:
|
||||||
|
context["finding"] = _slim_finding(payload)
|
||||||
|
cited_sheets = list(payload.get("sheets") or [])
|
||||||
|
cited_sheets += [e.get("sheet") for e in payload.get("evidence") or []
|
||||||
|
if isinstance(e, dict)]
|
||||||
|
scope_id = str(payload.get("scope_id") or "")
|
||||||
|
if scope_id.startswith("conflict:"):
|
||||||
|
cluster_key = scope_id.split(":", 1)[1]
|
||||||
|
cluster = next((c for c in snapshot.get("clusters") or []
|
||||||
|
if c.get("key") == cluster_key), None)
|
||||||
|
if cluster is not None:
|
||||||
|
assertions = cluster.get("assertions") or []
|
||||||
|
context["originating_cluster"] = {
|
||||||
|
"key": cluster.get("key"),
|
||||||
|
"location": cluster.get("location"),
|
||||||
|
"disciplines": cluster.get("disciplines"),
|
||||||
|
"kind": cluster.get("kind"),
|
||||||
|
"disputed_attributes": cluster.get("disputed_attributes"),
|
||||||
|
"assertion_count": len(assertions),
|
||||||
|
"assertions": [_slim_assertion(a)
|
||||||
|
for a in assertions[:_MAX_CLUSTER_ASSERTIONS]],
|
||||||
|
}
|
||||||
|
cited_sheets += [a.get("sheet_number") for a in assertions]
|
||||||
|
issue_id = payload.get("issue_id")
|
||||||
|
brain_decisions = [
|
||||||
|
decision for decision in snapshot.get("decisions") or []
|
||||||
|
if isinstance(decision, dict) and (
|
||||||
|
decision.get("kept_issue_id") == issue_id
|
||||||
|
or issue_id in (decision.get("finding_refs") or []))
|
||||||
|
]
|
||||||
|
if brain_decisions:
|
||||||
|
context["brain_decisions"] = brain_decisions[:10]
|
||||||
|
|
||||||
|
# The sheets the finding actually rests on, with their raw extraction -
|
||||||
|
# this is what lets the model say "it read 'MOUNTED ON GRADE' off M2.1".
|
||||||
|
wanted = {str(number) for number in cited_sheets if number}
|
||||||
|
if wanted:
|
||||||
|
context["source_sheets"] = [
|
||||||
|
_slim_sheet(sheet) for sheet in sheets
|
||||||
|
if str(sheet.get("sheet_number") or "") in wanted
|
||||||
|
]
|
||||||
|
|
||||||
|
context["log_excerpt"] = _log_excerpt(
|
||||||
|
out_dir,
|
||||||
|
_stage_terms(question) + sorted(wanted),
|
||||||
|
config.REVIEW_CHAT_LOG_LINES,
|
||||||
|
)
|
||||||
|
return context
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""Prompts for the review-screen chat (read-only run explainer)."""
|
||||||
|
|
||||||
|
REVIEW_CHAT_SYSTEM_PROMPT = """You are the explainer for a completed automated construction-drawing review run. A human reviewer is working through the review queue and is asking you why the run reached a particular conclusion.
|
||||||
|
|
||||||
|
Your ONLY job is to explain what the run did and why, using the run's own artifacts, which are supplied to you as a JSON context bundle. You are a witness to the run, not a participant in it.
|
||||||
|
|
||||||
|
HARD RULES - never break these:
|
||||||
|
- You do NOT write, propose, suggest, or output code, patches, diffs, file edits, configuration changes, prompt changes, or shell commands. If the reviewer asks for any of those, say that this chat only explains findings, and answer the underlying question in construction-review terms instead.
|
||||||
|
- You do NOT change, re-decide, confirm, reject, or re-score any finding. The reviewer owns that decision; the radio buttons on their screen are the only thing that changes a finding. You may explain what the evidence supports, and you may say plainly that a finding looks wrong, but you never state that a finding "has been" changed.
|
||||||
|
- You answer ONLY from the supplied context bundle. You have no access to the PDF, to sheets that were not extracted, or to anything outside the bundle. Never invent a sheet number, a quotation, a dimension, or a stage that is not in the bundle.
|
||||||
|
- Separate what the run RECORDED from what you INFER. Attribute recorded facts to the artifact they came from ("the extractor recorded ... on M2.1"). Mark reasoning of your own as inference.
|
||||||
|
- When the bundle does not contain the answer, say so directly and name what is missing and which artifact would have held it. "The Civil sheets were never extracted, so there are no Civil assertions to compare" is a good answer. Guessing is not.
|
||||||
|
|
||||||
|
HOW TO ANSWER "why does it think X":
|
||||||
|
Trace the chain backwards through the bundle and quote it: the finding's evidence, the assertions in the originating cluster, the source_text the extractor pulled off each sheet, any verification verdict from the re-check pass, and the Brain's merge or drop decision. If a value is marked disputed, or the verification status is refuted or unverified, say so - that is usually the real answer.
|
||||||
|
|
||||||
|
HOW TO ANSWER "why didn't it pick up X":
|
||||||
|
Work through the bundle's coverage material in this order and report which one explains it: (1) sheets_by_discipline - was the discipline in the set at all? (2) sheet_reconciliation - did the cover sheet's own index declare sheets that were never identified (declared_not_in_set)? (3) run.by_stage and run.code_review_enabled - was the responsible stage gated off or did it produce nothing? (4) suppressed_by_the_run - was something found and then dropped? (5) log_excerpt - did the run log record a skip, a retry, or an empty page? Name the specific reason. If several are possible, say which is best supported and what would confirm it.
|
||||||
|
|
||||||
|
Be direct and concrete. Quote verbatim source_text when it carries the answer. A short, specific, evidence-anchored answer is worth more than a thorough hedge. Use plain ASCII. Respond only with valid JSON."""
|
||||||
|
|
||||||
|
REVIEW_CHAT_USER_PROMPT = """A reviewer is asking about this run. Answer from the context bundle only.
|
||||||
|
|
||||||
|
Respond ONLY with a valid JSON object - no markdown fences, no prose outside the JSON:
|
||||||
|
{"answer":"your direct explanation to the reviewer, plain text, no markdown headings","findings":["one short factual determination per item - what you established about this question, each standing on its own"],"evidence_cited":[{"artifact":"which part of the bundle, e.g. 'finding.evidence' or 'source_sheets[M2.1]' or 'log_excerpt'","sheet":"sheet number or null","quote":"verbatim text from the bundle","why_it_matters":"one sentence"}],"answerable":"yes | partial | no","missing_information":"what the bundle would need to answer fully, or null if fully answered","assessment_of_finding":"looks_supported | looks_unsupported | cannot_tell | not_applicable","suggested_category_correction":"if the reviewer is telling you the run misidentified an object, the object they say it actually is, e.g. 'power floor box'; otherwise null","confidence":"high | medium | low"}
|
||||||
|
|
||||||
|
Set assessment_of_finding to not_applicable for run-scope questions that are not about one finding. Set suggested_category_correction to null unless the reviewer is asserting a correction - do not invent one.
|
||||||
|
|
||||||
|
{scope_line}
|
||||||
|
|
||||||
|
Reviewer's question:
|
||||||
|
{question}
|
||||||
|
|
||||||
|
{history_block}
|
||||||
|
Context bundle (the complete set of artifacts you may reason from):
|
||||||
|
{context}"""
|
||||||
@@ -1,9 +1,19 @@
|
|||||||
"""Feedback labels: one label artifact per human-review decision, for metrics."""
|
"""Feedback labels: one label artifact per human-review decision, for metrics.
|
||||||
|
|
||||||
|
Labels are written twice: job-locally under ``<out_dir>/review/`` (the
|
||||||
|
auditable record for that run) and, via ``append_shared_feedback``, to the
|
||||||
|
cross-job store at ``config.REVIEW_FEEDBACK_DIR``. The shared store is
|
||||||
|
append-only and nothing reads it yet - it exists so that a later pass can prime
|
||||||
|
a run with what reviewers corrected on previous sets without having to walk
|
||||||
|
every job directory.
|
||||||
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
|
||||||
|
|
||||||
def _as_dict(value) -> dict:
|
def _as_dict(value) -> dict:
|
||||||
return value if isinstance(value, dict) else {}
|
return value if isinstance(value, dict) else {}
|
||||||
@@ -21,6 +31,7 @@ def decision_to_label(queue_item: dict, decision: dict, job: dict) -> dict:
|
|||||||
payload = _as_dict(queue_item.get("payload"))
|
payload = _as_dict(queue_item.get("payload"))
|
||||||
summary = _as_dict(_as_dict(job.get("report")).get("summary"))
|
summary = _as_dict(_as_dict(job.get("report")).get("summary"))
|
||||||
return {
|
return {
|
||||||
|
"kind": "review_decision",
|
||||||
"review_item_id": queue_item.get("review_item_id"),
|
"review_item_id": queue_item.get("review_item_id"),
|
||||||
"job_id": job.get("job_id"),
|
"job_id": job.get("job_id"),
|
||||||
"pipeline_mode": job.get("pipeline_mode"),
|
"pipeline_mode": job.get("pipeline_mode"),
|
||||||
@@ -30,6 +41,11 @@ def decision_to_label(queue_item: dict, decision: dict, job: dict) -> dict:
|
|||||||
"confidence": payload.get("confidence"),
|
"confidence": payload.get("confidence"),
|
||||||
"decision": decision.get("decision"),
|
"decision": decision.get("decision"),
|
||||||
"reason_code": decision.get("reason_code"),
|
"reason_code": decision.get("reason_code"),
|
||||||
|
# The reviewer's structured corrections. Carried here (and into the
|
||||||
|
# cross-job store) so "wrong category" survives as data rather than
|
||||||
|
# only as free text on the suppressed issue.
|
||||||
|
"category_correction": decision.get("category_correction"),
|
||||||
|
"severity_correction": decision.get("severity_correction"),
|
||||||
"location": payload.get("location"),
|
"location": payload.get("location"),
|
||||||
"disciplines": payload.get("disciplines"),
|
"disciplines": payload.get("disciplines"),
|
||||||
"sheets": payload.get("sheets"),
|
"sheets": payload.get("sheets"),
|
||||||
@@ -40,7 +56,12 @@ def decision_to_label(queue_item: dict, decision: dict, job: dict) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def write_label(out_dir: str, label: dict) -> None:
|
def write_label(out_dir: str, label: dict) -> None:
|
||||||
"""Append one label as a JSON line; never raises on I/O failure."""
|
"""Append one label job-locally and to the cross-job store.
|
||||||
|
|
||||||
|
Never raises on I/O failure: a lost label must not fail the save that
|
||||||
|
produced it.
|
||||||
|
"""
|
||||||
|
append_shared_feedback(label)
|
||||||
try:
|
try:
|
||||||
review_dir = os.path.join(out_dir, "review")
|
review_dir = os.path.join(out_dir, "review")
|
||||||
os.makedirs(review_dir, exist_ok=True)
|
os.makedirs(review_dir, exist_ok=True)
|
||||||
@@ -49,3 +70,49 @@ def write_label(out_dir: str, label: dict) -> None:
|
|||||||
f.write(json.dumps(label) + "\n")
|
f.write(json.dumps(label) + "\n")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(f"[Review] feedback label write failed: {e}")
|
print(f"[Review] feedback label write failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def append_shared_feedback(record: dict) -> None:
|
||||||
|
"""Append one record to the cross-job feedback store; never raises.
|
||||||
|
|
||||||
|
One JSONL file per record ``kind`` so a reader can pick up decisions and
|
||||||
|
chat turns independently. Failure here is logged and swallowed: the
|
||||||
|
cross-job roll-up is a convenience, and losing a line must never fail the
|
||||||
|
review action that produced it.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
kind = str(record.get("kind") or "misc")
|
||||||
|
os.makedirs(config.REVIEW_FEEDBACK_DIR, exist_ok=True)
|
||||||
|
name = "chat_turns.jsonl" if kind == "review_chat_turn" else "decisions.jsonl"
|
||||||
|
path = os.path.join(config.REVIEW_FEEDBACK_DIR, name)
|
||||||
|
with open(path, "a", encoding="utf-8") as f:
|
||||||
|
f.write(json.dumps(record) + "\n")
|
||||||
|
except OSError as e:
|
||||||
|
print(f"[Review] shared feedback write failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def read_shared_feedback(kind: str = "review_decision") -> list:
|
||||||
|
"""Read the cross-job store for one record kind, oldest first.
|
||||||
|
|
||||||
|
Corrupt lines are skipped so a partial write cannot hide the rest.
|
||||||
|
"""
|
||||||
|
name = "chat_turns.jsonl" if kind == "review_chat_turn" else "decisions.jsonl"
|
||||||
|
path = os.path.join(config.REVIEW_FEEDBACK_DIR, name)
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
return []
|
||||||
|
records = []
|
||||||
|
try:
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
value = json.loads(line)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
if isinstance(value, dict):
|
||||||
|
records.append(value)
|
||||||
|
except OSError:
|
||||||
|
return []
|
||||||
|
return records
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
"""sheet_reconcile.py - deterministic sheet-list reconciliation (no LLM).
|
||||||
|
|
||||||
|
The cover sheet's own sheet index (SHEET LIST / DRAWING INDEX) declares which
|
||||||
|
sheets the set is SUPPOSED to contain. Comparing that declaration against the
|
||||||
|
sheets wave-1 actually identified answers two early questions:
|
||||||
|
|
||||||
|
- declared_not_in_set: sheets the index lists but we didn't identify - dark
|
||||||
|
pages, misidentification, or disciplines genuinely absent from this PDF.
|
||||||
|
- in_set_not_declared: sheet numbers we extracted that the index doesn't
|
||||||
|
list - misread title blocks or unlisted sheets.
|
||||||
|
|
||||||
|
Deterministic complement to the LLM sheet_index stage, which can only infer
|
||||||
|
from what extraction already found.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
# Markers that introduce the drawing set's own sheet index on a cover page.
|
||||||
|
_INDEX_MARKERS = (
|
||||||
|
"SHEET LIST",
|
||||||
|
"DRAWING INDEX",
|
||||||
|
"SHEET INDEX",
|
||||||
|
"DRAWING LIST",
|
||||||
|
"INDEX OF DRAWINGS",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sheet ids: 1-2 letters, optional hyphen, 2-3 digits, optional decimal suffix.
|
||||||
|
# Covers S301, A102, LS101, C-001, C-001.1; excludes dates/project numbers
|
||||||
|
# (pure digits) and member marks (W12X26 - letter after digits).
|
||||||
|
_SHEET_TOKEN_RE = re.compile(r"\b([A-Z]{1,2}-?\d{2,3}(?:\.\d+)?)\b")
|
||||||
|
|
||||||
|
# Only cover-front pages carry the set index.
|
||||||
|
_MAX_INDEX_PAGE = 5
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_id(sheet_id: str) -> str:
|
||||||
|
return (sheet_id or "").upper().replace("-", "").strip()
|
||||||
|
|
||||||
|
|
||||||
|
def declared_sheet_list(page_texts: Dict[int, Optional[str]]) -> List[str]:
|
||||||
|
"""Scrape the declared sheet list off the cover page's text layer.
|
||||||
|
|
||||||
|
page_texts: {page_number: text_layer_or_None}. Returns the ordered,
|
||||||
|
deduped list of declared sheet ids, or [] when no index marker exists.
|
||||||
|
Only the FIRST page containing a marker is parsed (later 'sheet list'
|
||||||
|
echoes in legends/schedules are ignored).
|
||||||
|
"""
|
||||||
|
for page_number in sorted(page_texts):
|
||||||
|
if page_number > _MAX_INDEX_PAGE:
|
||||||
|
break
|
||||||
|
text = page_texts.get(page_number) or ""
|
||||||
|
upper = text.upper()
|
||||||
|
marker_at = -1
|
||||||
|
for marker in _INDEX_MARKERS:
|
||||||
|
marker_at = upper.find(marker)
|
||||||
|
if marker_at >= 0:
|
||||||
|
break
|
||||||
|
if marker_at < 0:
|
||||||
|
continue
|
||||||
|
section = text[marker_at:]
|
||||||
|
declared: List[str] = []
|
||||||
|
for token in _SHEET_TOKEN_RE.findall(section):
|
||||||
|
if token not in declared:
|
||||||
|
declared.append(token)
|
||||||
|
return declared
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile_sheets(sheets: List[Dict], declared: List[str]) -> Dict:
|
||||||
|
"""Compare extracted sheet_numbers against the declared index.
|
||||||
|
|
||||||
|
Comparison is hyphen/case-normalized; output lists keep the declared /
|
||||||
|
extracted originals.
|
||||||
|
"""
|
||||||
|
found: List[str] = [str(s["sheet_number"]) for s in sheets or []
|
||||||
|
if s.get("sheet_number")]
|
||||||
|
found_norm = {_normalize_id(n) for n in found}
|
||||||
|
declared_norm = {_normalize_id(n) for n in declared}
|
||||||
|
|
||||||
|
declared_not_in_set = [n for n in declared if _normalize_id(n) not in found_norm]
|
||||||
|
# Preserve extraction order, dedupe, keep originals.
|
||||||
|
in_set_not_declared: List[str] = []
|
||||||
|
for n in found:
|
||||||
|
if _normalize_id(n) not in declared_norm and n not in in_set_not_declared:
|
||||||
|
in_set_not_declared.append(n)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"declared_total": len(declared),
|
||||||
|
"found_total": len(found),
|
||||||
|
"declared_not_in_set": declared_not_in_set,
|
||||||
|
"in_set_not_declared": in_set_not_declared,
|
||||||
|
}
|
||||||
@@ -119,7 +119,12 @@ def merge_objects(vision_objs: List[Dict], text_objs: List[Dict]) -> List[Dict]:
|
|||||||
return merged
|
return merged
|
||||||
|
|
||||||
|
|
||||||
_SHEET_ID_RE = re.compile(r"\b([A-Z]{1,2}\d{2,3}(?:\.\d+)?)\b")
|
# Sheet ids: 1-2 letters, OPTIONAL HYPHEN, 2-3 digits, optional decimal suffix.
|
||||||
|
# The hyphen matters: civil/landscape sets number sheets C-001 / L-101, and a
|
||||||
|
# regex without it leaves those pages sheet_number=None, which then shows up as
|
||||||
|
# a false "declared but not in set" in sheet_reconcile. Kept in sync with
|
||||||
|
# sheet_reconcile._SHEET_TOKEN_RE.
|
||||||
|
_SHEET_ID_RE = re.compile(r"\b([A-Z]{1,2}-?\d{2,3}(?:\.\d+)?)\b")
|
||||||
|
|
||||||
|
|
||||||
def recover_sheet_number(page_text: str) -> Optional[str]:
|
def recover_sheet_number(page_text: str) -> Optional[str]:
|
||||||
|
|||||||
@@ -141,21 +141,21 @@
|
|||||||
<div class="num">5</div>
|
<div class="num">5</div>
|
||||||
<div class="icon">🕵️</div>
|
<div class="icon">🕵️</div>
|
||||||
<h2>The Detectives</h2>
|
<h2>The Detectives</h2>
|
||||||
<p>One per topic pile. Compares sheets that should agree and hunts for contradictions: "wall shown here, but not on the structural plan."</p>
|
<p>One per topic pile. Hunts for contradictions between sheets that should agree — <i>and</i> mistakes within a single sheet: "wall shown here, but not on the structural plan."</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="arrow">→</div>
|
<div class="arrow">→</div>
|
||||||
<div class="card spec">
|
<div class="card spec">
|
||||||
<div class="num">6</div>
|
<div class="num">6</div>
|
||||||
<div class="icon">👷</div>
|
<div class="icon">📐</div>
|
||||||
<h2>The Specialists</h2>
|
<h2>The Specialists</h2>
|
||||||
<p>Three experts at once: a <b>code inspector</b>, a veteran <b>builder</b> ("can this actually be built?"), and a <b>checklist keeper</b> ("is anything missing?").</p>
|
<p>Working at once: a <b>drawing proofreader</b> (dangling callouts, a schedule vs its own plan, dimensions that don't add up), a veteran <b>builder</b> ("can this be built?"), and a <b>checklist keeper</b> ("is anything missing?").</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="arrow">→</div>
|
<div class="arrow">→</div>
|
||||||
<div class="card brain">
|
<div class="card brain">
|
||||||
<div class="num">7</div>
|
<div class="num">7</div>
|
||||||
<div class="icon">🧠</div>
|
<div class="icon">🧠</div>
|
||||||
<h2>The Brain</h2>
|
<h2>The Brain</h2>
|
||||||
<p>The senior reviewer. Collects every finding, merges duplicates, discards weak ones, and ranks the rest by how much trouble they'd cause.</p>
|
<p>The senior reviewer. Merges duplicates, discards weak findings, ranks the rest — then sends the ones it doubts back for a <b>zoomed-in second look</b> and drops any that don't hold up.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="arrow">→</div>
|
<div class="arrow">→</div>
|
||||||
<div class="card human">
|
<div class="card human">
|
||||||
@@ -174,9 +174,12 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="note">
|
<div class="note">
|
||||||
<b>Good to know:</b> everyone shares one notebook, so each step builds on the last.
|
<b>Good to know:</b> the review is focused on the <b>drawings themselves</b> —
|
||||||
If one page can't be read, the team keeps going and that page is flagged as a gap
|
contradictions, single-sheet mistakes, buildability, and missing pieces
|
||||||
instead of stopping the whole review. Every finding links back to the sheet it came from.
|
(building-code checks are built in but turned off by default). Everyone shares one
|
||||||
|
notebook, so each step builds on the last. If one page can't be read, the team keeps
|
||||||
|
going and that page is flagged as a gap instead of stopping the whole review. Every
|
||||||
|
finding links back to the sheet it came from.
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 184 KiB After Width: | Height: | Size: 492 KiB |
+70
-31
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
**What it does:** You upload a set of construction drawings (a PDF of blueprints).
|
**What it does:** You upload a set of construction drawings (a PDF of blueprints).
|
||||||
A team of AI assistants reads every page, compares everything against everything
|
A team of AI assistants reads every page, compares everything against everything
|
||||||
else, and hands you a list of problems — contradictions, code violations, missing
|
else, and hands you a list of problems — contradictions between sheets, mistakes
|
||||||
information, and things that would be hard to build — before they cost you money
|
within a single sheet, missing information, and things that would be hard to
|
||||||
in the field.
|
build — before they cost you money in the field.
|
||||||
|
|
||||||
Think of it like hiring a room full of specialist consultants to review your
|
Think of it like hiring a room full of specialist consultants to review your
|
||||||
plans overnight. Each one has a specific job, they pass their notes down the
|
plans overnight. Each one has a specific job, they pass their notes down the
|
||||||
@@ -53,18 +53,23 @@ table, and a senior reviewer at the end sorts it all into one clean report.
|
|||||||
| 4. CONFLICT DETECTIVES (one per topic pile) |
|
| 4. CONFLICT DETECTIVES (one per topic pile) |
|
||||||
| Compares sheets that should agree and looks for |
|
| Compares sheets that should agree and looks for |
|
||||||
| contradictions: "Wall shown here on A-201 but not |
|
| contradictions: "Wall shown here on A-201 but not |
|
||||||
| on S-101", "Pipe runs through the duct" |
|
| on S-101", "Pipe runs through the duct". Now also |
|
||||||
|
| catches contradictions WITHIN a single sheet |
|
||||||
+----------------------------------------------------------+
|
+----------------------------------------------------------+
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
+----------------------------------------------------------+
|
+----------------------------------------------------------+
|
||||||
| 5. THREE SPECIALISTS (work side by side) |
|
| 5. THREE SPECIALISTS (work side by side) |
|
||||||
| * Code Inspector — does anything break the local |
|
| * Drawing Checker — problems on a sheet BY ITSELF: |
|
||||||
| building code? |
|
| a callout pointing to a detail that isn't there, |
|
||||||
| * Builder — can this actually be built as |
|
| a schedule that disagrees with its own plan, |
|
||||||
|
| dimensions that don't add up, missing scale |
|
||||||
|
| * Builder — can this actually be built as |
|
||||||
| drawn? (access, clearances, sequencing) |
|
| drawn? (access, clearances, sequencing) |
|
||||||
| * Completeness Checker — is anything MISSING from |
|
| * Completeness Checker — is anything MISSING from |
|
||||||
| the set? (sheets, schedules, required details) |
|
| the set? (sheets, schedules, required details) |
|
||||||
|
| (A Code Inspector also lives here but is turned OFF |
|
||||||
|
| by default — the focus is the drawings themselves) |
|
||||||
+----------------------------------------------------------+
|
+----------------------------------------------------------+
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
@@ -77,9 +82,21 @@ table, and a senior reviewer at the end sorts it all into one clean report.
|
|||||||
|
|
|
|
||||||
v
|
v
|
||||||
+----------------------------------------------------------+
|
+----------------------------------------------------------+
|
||||||
|
| 6.5 THE BRAIN DOUBLE-CHECKS (asks for a second look) |
|
||||||
|
| For the findings it's unsure about, the Brain sends |
|
||||||
|
| them back to a fact-checker that re-reads the actual |
|
||||||
|
| sheet (zoomed-in image + the page's real text) to |
|
||||||
|
| confirm or debunk. Debunked findings are dropped |
|
||||||
|
| before they ever reach you |
|
||||||
|
+----------------------------------------------------------+
|
||||||
|
|
|
||||||
|
v
|
||||||
|
+----------------------------------------------------------+
|
||||||
| 7. HUMAN REVIEW GATE |
|
| 7. HUMAN REVIEW GATE |
|
||||||
| The important/uncertain findings are queued for a |
|
| The important/uncertain findings are queued for a |
|
||||||
| real person to Confirm / Reject / mark Unsure |
|
| real person to Confirm / Reject / mark Unsure. |
|
||||||
|
| You can also ASK the run why it concluded any of |
|
||||||
|
| them, or why it never looked at something |
|
||||||
+----------------------------------------------------------+
|
+----------------------------------------------------------+
|
||||||
|
|
|
|
||||||
v
|
v
|
||||||
@@ -105,12 +122,15 @@ table, and a senior reviewer at the end sorts it all into one clean report.
|
|||||||
| 2 | Sheet Indexer | Librarian | Builds the table of contents of the drawing set |
|
| 2 | Sheet Indexer | Librarian | Builds the table of contents of the drawing set |
|
||||||
| 2 | Jurisdiction Scout | Local guide | Identifies the project's location so the right building codes are used |
|
| 2 | Jurisdiction Scout | Local guide | Identifies the project's location so the right building codes are used |
|
||||||
| 3 | Linker | Connector | Groups related facts from different sheets into topic clusters |
|
| 3 | Linker | Connector | Groups related facts from different sheets into topic clusters |
|
||||||
| 4 | Conflict Critic | Detective | Examines each cluster for contradictions between disciplines |
|
| 4 | Conflict Critic | Detective | Examines each cluster for contradictions — between disciplines, across a discipline's own sheets, or within one sheet |
|
||||||
| 5 | Code Agent | Code inspector | Flags building-code violations, using the jurisdiction from step 2 |
|
| 5 | Drawing Integrity Agent | Proofreader | Checks each sheet on its own: dangling callouts, a schedule vs its own plan, dimensions that don't sum, missing scale/north/title-block |
|
||||||
| 5 | Constructability Agent | Veteran builder | Flags things that are drawn fine but can't be built practically |
|
| 5 | Constructability Agent | Veteran builder | Flags things that are drawn fine but can't be built practically |
|
||||||
| 5 | Completeness Agent | Checklist keeper | Flags missing sheets, missing details, gaps in the set |
|
| 5 | Completeness Agent | Checklist keeper | Flags missing sheets, missing details, gaps in the set |
|
||||||
|
| 5 | Code Agent *(off by default)* | Code inspector | Building-code/ADA checks — kept in the codebase but disabled so the review focuses on the drawings; one flag turns it back on |
|
||||||
| 6 | Brain | Chief estimator | Deduplicates, judges, and prioritizes all findings |
|
| 6 | Brain | Chief estimator | Deduplicates, judges, and prioritizes all findings |
|
||||||
|
| 6.5 | Brain (clarification) | Second opinion | For findings it distrusts, sends them back to the fact-checker to re-read the sheet; debunked findings are dropped |
|
||||||
| 7 | Review Gate | Your desk | Presents the findings a human should approve before anything goes out |
|
| 7 | Review Gate | Your desk | Presents the findings a human should approve before anything goes out |
|
||||||
|
| 7 | Review Chat | The analyst you can question | Answers "why did it decide that?" and "why didn't it check that?" from the run's own records — it explains, it never changes anything |
|
||||||
| 8 | RFI Writer | Secretary | Writes the formal clarification letters for confirmed issues |
|
| 8 | RFI Writer | Secretary | Writes the formal clarification letters for confirmed issues |
|
||||||
|
|
||||||
Everything the assistants learn is kept in a shared notebook (the "project
|
Everything the assistants learn is kept in a shared notebook (the "project
|
||||||
@@ -122,13 +142,15 @@ the whole review.
|
|||||||
|
|
||||||
## Where Improvements Could Be Made
|
## Where Improvements Could Be Made
|
||||||
|
|
||||||
### 1. Coverage — "make sure every page actually got read"
|
*(Several items from earlier versions have since shipped — noted below.)*
|
||||||
- Today, if a Reader fails on a page (the AI's answer gets cut off or comes back
|
|
||||||
garbled), that page quietly disappears from everything downstream. Worse, the
|
### 1. Coverage — "make sure every page actually got read" ✅ *largely shipped*
|
||||||
Completeness Checker can then report the sheet as "missing from the set" when
|
- Failed pages used to quietly disappear, and the Completeness Checker would
|
||||||
really it was there but unread — a false alarm.
|
then report a sheet as "missing" when it was really just unread.
|
||||||
- **Improvement:** retry failed pages with a backup model, and clearly separate
|
- **Done:** a retry ladder now re-reads a page (text-only pass, then a
|
||||||
"sheet doesn't exist" from "sheet couldn't be read" in the report.
|
deterministic text-layer fallback) so no text-bearing page goes dark, and the
|
||||||
|
report separates "sheet doesn't exist" from "sheet couldn't be read."
|
||||||
|
- **Still open:** try a different backup model on the hardest pages.
|
||||||
|
|
||||||
### 2. Speed — "the team waits in line more than it needs to"
|
### 2. Speed — "the team waits in line more than it needs to"
|
||||||
- The steps run strictly one after another, but some could start earlier. The
|
- The steps run strictly one after another, but some could start earlier. The
|
||||||
@@ -145,14 +167,17 @@ the whole review.
|
|||||||
- **Improvement:** use cheaper models for simple pages (schedules, title
|
- **Improvement:** use cheaper models for simple pages (schedules, title
|
||||||
sheets), save the expensive model for dense drawings; keep tuning the
|
sheets), save the expensive model for dense drawings; keep tuning the
|
||||||
thinking budget knobs; reuse cached answers when the same plan set is
|
thinking budget knobs; reuse cached answers when the same plan set is
|
||||||
re-run.
|
re-run. *(The truncation bug itself is now fixed.)*
|
||||||
|
|
||||||
### 4. Smarter grouping — "better piles, better detective work"
|
### 4. Smarter detective work — "catch conflicts that span piles" ✅ *shipped*
|
||||||
- The Connector caps how many topic piles it keeps (a fixed limit), so on big
|
- The Detectives only saw one topic pile at a time, so a contradiction spanning
|
||||||
sets some connections may never be made. The Detectives only see one pile at
|
two piles — or a mistake on a single sheet — could slip through.
|
||||||
a time, so a contradiction spanning two piles can slip through.
|
- **Done:** the Detectives now also flag contradictions *within* a single sheet,
|
||||||
- **Improvement:** revisit the pile limit, and let the Brain (or a second-pass
|
a new Drawing Checker proofreads every sheet on its own, and after the Brain
|
||||||
Detective) look for conflicts that span multiple piles.
|
sorts everything it can send doubtful findings back for a zoomed-in second
|
||||||
|
look (wave 6.5) and drop the ones that don't hold up.
|
||||||
|
- **Still open:** revisit the hard cap on how many topic piles are kept on very
|
||||||
|
large sets.
|
||||||
|
|
||||||
### 5. Human time — "review less, but review what matters"
|
### 5. Human time — "review less, but review what matters"
|
||||||
- Today the review queue is built from rules about severity and confidence.
|
- Today the review queue is built from rules about severity and confidence.
|
||||||
@@ -160,15 +185,29 @@ the whole review.
|
|||||||
queue better — the system already records your feedback, so it can get
|
queue better — the system already records your feedback, so it can get
|
||||||
smarter over time about what actually needs your eyes.
|
smarter over time about what actually needs your eyes.
|
||||||
|
|
||||||
### 6. Trust — "show the receipts"
|
### 5b. Explaining itself — "why did you think that?" ✅ *shipped*
|
||||||
- Findings carry evidence, but a non-technical reader can't easily see *where
|
- **Done:** every finding on the review screen has a chat panel, plus one for
|
||||||
on the drawing* the problem is.
|
the run as a whole. Ask why a unit was read as ground-mounted, or why a whole
|
||||||
- **Improvement:** attach a cropped image snippet of the exact spot on the
|
discipline never got looked at, and it traces the answer back through what it
|
||||||
sheet to each finding, so anyone can verify it in seconds.
|
actually recorded — quoting the note it read off the sheet, or naming the
|
||||||
|
stage that skipped the pages. It cannot change a finding; that stays yours.
|
||||||
|
- **Done:** when you correct it in conversation ("that's not a floor drain,
|
||||||
|
it's a power floor box"), the correction is filed as structured data rather
|
||||||
|
than a free-text comment.
|
||||||
|
- **Still open:** nothing reads those filed corrections back yet. The next step
|
||||||
|
is priming a new run with what reviewers corrected on previous sets, so the
|
||||||
|
same misread does not come back on the next job.
|
||||||
|
|
||||||
|
### 6. Trust — "show the receipts" ✅ *partially shipped*
|
||||||
|
- **Done:** the fact-checker already pulls a zoomed-in crop of the exact spot on
|
||||||
|
the sheet when it re-reads a finding.
|
||||||
|
- **Still open:** attach that crop to the finding in the final report so a
|
||||||
|
non-technical reader can verify it in seconds without opening the PDF.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
*Technical reference for the curious: the pipeline lives in
|
*Technical reference for the curious: the pipeline lives in
|
||||||
`backend/agents/runner.py` (the waves above are the "Agent wave N" stages), the
|
`backend/agents/runner.py` (the waves above are the "Agent wave N" stages), the
|
||||||
team's shared notebook is `backend/agents/memory.py`, and the review queue is
|
team's shared notebook is `backend/agents/memory.py`, the review queue is
|
||||||
`backend/review/gate.py` + `backend/review/finalizer.py`.*
|
`backend/review/gate.py` + `backend/review/finalizer.py`, and the review chat is
|
||||||
|
`backend/review/chat.py` + `backend/review/chat_context.py`.*
|
||||||
+155
-4
@@ -82,6 +82,29 @@
|
|||||||
border:1px solid var(--line); border-radius:6px; padding:6px 8px; font-size:13px; margin-top:6px; }
|
border:1px solid var(--line); border-radius:6px; padding:6px 8px; font-size:13px; margin-top:6px; }
|
||||||
.review-controls input[type=text] { width:100%; }
|
.review-controls input[type=text] { width:100%; }
|
||||||
.review-controls .hidden { display:none; }
|
.review-controls .hidden { display:none; }
|
||||||
|
.chat { margin-top:10px; padding-top:10px; border-top:1px solid var(--line); font-size:13px; }
|
||||||
|
.chat-toggle { background:none; border:0; color:var(--accent); cursor:pointer; padding:0;
|
||||||
|
font-size:13px; text-decoration:underline dotted; }
|
||||||
|
.chat-body { margin-top:10px; }
|
||||||
|
.chat-body.hidden, .chat .hidden { display:none; }
|
||||||
|
.chat-turns { max-height:340px; overflow-y:auto; margin-bottom:8px; }
|
||||||
|
.chat-q, .chat-a { border-radius:8px; padding:8px 10px; margin:6px 0; }
|
||||||
|
.chat-q { background:#161b25; }
|
||||||
|
.chat-q b { color:var(--accent); }
|
||||||
|
.chat-a { background:#11141a; }
|
||||||
|
.chat-a ul { margin:6px 0 0; padding-left:18px; }
|
||||||
|
.chat-a li { margin:2px 0; }
|
||||||
|
.chat-cite { color:var(--muted); font-size:12px; margin-top:6px; }
|
||||||
|
.chat-cite code { color:var(--accent); }
|
||||||
|
.chat-tags { color:var(--muted); font-size:12px; margin-top:6px; }
|
||||||
|
.chat-row { display:flex; gap:8px; align-items:flex-start; }
|
||||||
|
.chat-row textarea { flex:1; background:#0c0e13; color:var(--text); border:1px solid var(--line);
|
||||||
|
border-radius:6px; padding:8px; font-size:13px; font-family:inherit; resize:vertical;
|
||||||
|
min-height:38px; }
|
||||||
|
.chat-row button { white-space:nowrap; }
|
||||||
|
.chat-hint { color:var(--muted); font-size:12px; margin-top:6px; }
|
||||||
|
.chat-err { color:var(--hi); font-size:12px; margin-top:6px; }
|
||||||
|
.btn.sm { padding:8px 14px; font-size:13px; margin-top:0; }
|
||||||
.pill.blocking { background:rgba(255,93,87,.15); color:var(--hi); }
|
.pill.blocking { background:rgba(255,93,87,.15); color:var(--hi); }
|
||||||
.pill.audit { background:rgba(91,140,255,.15); color:var(--accent); }
|
.pill.audit { background:rgba(91,140,255,.15); color:var(--accent); }
|
||||||
.pill.critical { background:rgba(255,93,87,.28); color:#fff; }
|
.pill.critical { background:rgba(255,93,87,.28); color:#fff; }
|
||||||
@@ -111,7 +134,7 @@
|
|||||||
<input type="email" id="email" placeholder="you@firm.com" />
|
<input type="email" id="email" placeholder="you@firm.com" />
|
||||||
</div>
|
</div>
|
||||||
<details class="email-card" id="intake">
|
<details class="email-card" id="intake">
|
||||||
<summary style="cursor:pointer">Project details <span class="opt">(optional — improves code/ADA review)</span></summary>
|
<summary style="cursor:pointer">Project details <span class="opt">(optional)</span></summary>
|
||||||
<input type="text" id="project_name" placeholder="Project name" style="width:100%;margin-top:8px" />
|
<input type="text" id="project_name" placeholder="Project name" style="width:100%;margin-top:8px" />
|
||||||
<input type="text" id="address" placeholder="Project address" style="width:100%;margin-top:8px" />
|
<input type="text" id="address" placeholder="Project address" style="width:100%;margin-top:8px" />
|
||||||
<input type="text" id="occupancy" placeholder="Occupancy (e.g. Business, Assembly)" style="width:100%;margin-top:8px" />
|
<input type="text" id="occupancy" placeholder="Occupancy (e.g. Business, Assembly)" style="width:100%;margin-top:8px" />
|
||||||
@@ -473,7 +496,7 @@ function render(rep){
|
|||||||
const issues=rep.validated_issues||[];
|
const issues=rep.validated_issues||[];
|
||||||
if(issues.length){
|
if(issues.length){
|
||||||
html+='<details open style="margin-top:24px"><summary><b>QAQC issues ('+issues.length+')</b> '+
|
html+='<details open style="margin-top:24px"><summary><b>QAQC issues ('+issues.length+')</b> '+
|
||||||
'<span class="opt">conflicts + full-set + code/ADA + constructability, deduplicated</span></summary>';
|
'<span class="opt">conflicts + drawing integrity + full-set + constructability, deduplicated</span></summary>';
|
||||||
for(const c of issues){
|
for(const c of issues){
|
||||||
const rs=c.review_state;
|
const rs=c.review_state;
|
||||||
html+='<div class="conflict '+esc(c.severity)+'">'+
|
html+='<div class="conflict '+esc(c.severity)+'">'+
|
||||||
@@ -536,6 +559,12 @@ async function renderReview(job){
|
|||||||
esc(prog.completed||0)+' of '+esc(prog.required||0)+' required items decided.'+
|
esc(prog.completed||0)+' of '+esc(prog.required||0)+' required items decided.'+
|
||||||
((prog.remaining||0)>0?' Decide all blocking items, save, then finalize.':
|
((prog.remaining||0)>0?' Decide all blocking items, save, then finalize.':
|
||||||
' All required items decided \u2014 you can finalize.')+'</div>';
|
' All required items decided \u2014 you can finalize.')+'</div>';
|
||||||
|
html+='<div class="conflict" style="border-left-color:var(--accent)">'+
|
||||||
|
'<div class="row"><span class="cat">Ask about this run</span></div>'+
|
||||||
|
'<div class="meta">Questions about coverage or about the run as a whole — '+
|
||||||
|
'e.g. "why didn\'t it pick up the Civil set?". Answers are explanations only; '+
|
||||||
|
'they never change a finding.</div>'+
|
||||||
|
chatPanelHtml('','Ask a question about this run',true)+'</div>';
|
||||||
const blocking=queue.filter(i=>i.blocking), audit=queue.filter(i=>!i.blocking);
|
const blocking=queue.filter(i=>i.blocking), audit=queue.filter(i=>!i.blocking);
|
||||||
blocking.forEach((item,i)=>{ html+=reviewItemHtml(item,'b'+i,prior[item.review_item_id]); });
|
blocking.forEach((item,i)=>{ html+=reviewItemHtml(item,'b'+i,prior[item.review_item_id]); });
|
||||||
if(audit.length){
|
if(audit.length){
|
||||||
@@ -548,7 +577,11 @@ async function renderReview(job){
|
|||||||
'<button class="btn" id="saveReviewBtn">Save decisions</button> '+
|
'<button class="btn" id="saveReviewBtn">Save decisions</button> '+
|
||||||
'<button class="btn" id="finalizeBtn"'+((prog.remaining||0)===0?'':' disabled')+
|
'<button class="btn" id="finalizeBtn"'+((prog.remaining||0)===0?'':' disabled')+
|
||||||
'>Finalize & send report</button></div>'+
|
'>Finalize & send report</button></div>'+
|
||||||
'<div class="status" id="reviewMsg"></div>';
|
'<div class="status" id="reviewMsg"></div>'+
|
||||||
|
'<div class="meta" style="margin-top:10px">Chat transcript: '+
|
||||||
|
'<a href="/jobs/'+esc(jobId)+'/review-chat/log" target="_blank" rel="noopener">'+
|
||||||
|
'/jobs/'+esc(jobId)+'/review-chat/log</a> '+
|
||||||
|
'(also saved as review/chat_log.jsonl on the server)</div>';
|
||||||
results.innerHTML=html;
|
results.innerHTML=html;
|
||||||
results.querySelectorAll('.review-item input[type=radio]').forEach(r=>{
|
results.querySelectorAll('.review-item input[type=radio]').forEach(r=>{
|
||||||
r.addEventListener('change',()=>syncReviewControls(r.closest('.review-item')));
|
r.addEventListener('change',()=>syncReviewControls(r.closest('.review-item')));
|
||||||
@@ -559,6 +592,8 @@ async function renderReview(job){
|
|||||||
});
|
});
|
||||||
document.getElementById('saveReviewBtn').addEventListener('click',saveReviewDecisions);
|
document.getElementById('saveReviewBtn').addEventListener('click',saveReviewDecisions);
|
||||||
document.getElementById('finalizeBtn').addEventListener('click',finalizeReview);
|
document.getElementById('finalizeBtn').addEventListener('click',finalizeReview);
|
||||||
|
wireChatPanels();
|
||||||
|
loadChatHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
function reviewItemHtml(item,uid,prev){
|
function reviewItemHtml(item,uid,prev){
|
||||||
@@ -600,10 +635,126 @@ function reviewItemHtml(item,uid,prev){
|
|||||||
'<input type="text" class="comment" placeholder="Comment (optional)" value="'+escAttr(prev.comment||'')+'">'+
|
'<input type="text" class="comment" placeholder="Comment (optional)" value="'+escAttr(prev.comment||'')+'">'+
|
||||||
'<input type="text" class="clar'+(prev.decision==='needs_clarification'?'':' hidden')+
|
'<input type="text" class="clar'+(prev.decision==='needs_clarification'?'':' hidden')+
|
||||||
'" placeholder="Clarification answer" value="'+escAttr(prev.clarification_answer||'')+'">'+
|
'" placeholder="Clarification answer" value="'+escAttr(prev.clarification_answer||'')+'">'+
|
||||||
'</div></div>';
|
'</div>'+
|
||||||
|
chatPanelHtml(item.review_item_id,'Ask about this finding')+
|
||||||
|
'</div>';
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- review chat (read-only run explainer) ---
|
||||||
|
// One panel per review item plus one run-scope panel. Panels are keyed by
|
||||||
|
// review_item_id ('' = run scope); history for every panel is fetched once.
|
||||||
|
|
||||||
|
function chatPanelHtml(key,label,openByDefault){
|
||||||
|
const open=!!openByDefault;
|
||||||
|
return '<div class="chat" data-chat="'+escAttr(key||'')+'">'+
|
||||||
|
'<button type="button" class="chat-toggle">'+esc(label)+'</button>'+
|
||||||
|
'<div class="chat-body'+(open?'':' hidden')+'">'+
|
||||||
|
'<div class="chat-turns"></div>'+
|
||||||
|
'<div class="chat-row">'+
|
||||||
|
'<textarea rows="2" placeholder="Why did it conclude that?"></textarea>'+
|
||||||
|
'<button type="button" class="btn sm chat-send">Ask</button>'+
|
||||||
|
'</div>'+
|
||||||
|
'<div class="chat-hint">Explains what the run did, from its own artifacts. '+
|
||||||
|
'It cannot change this finding or your decision.</div>'+
|
||||||
|
'<div class="chat-err"></div>'+
|
||||||
|
'</div></div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
function chatTurnHtml(turn){
|
||||||
|
let html='<div class="chat-q"><b>You:</b> '+esc(turn.question||'')+'</div>'+
|
||||||
|
'<div class="chat-a">'+esc(turn.answer||'');
|
||||||
|
if((turn.findings||[]).length){
|
||||||
|
html+='<ul>'+turn.findings.map(f=>'<li>'+esc(f)+'</li>').join('')+'</ul>';
|
||||||
|
}
|
||||||
|
(turn.evidence_cited||[]).forEach(c=>{
|
||||||
|
html+='<div class="chat-cite"><code>'+esc(c.artifact||'?')+'</code>'+
|
||||||
|
(c.sheet?' ('+esc(c.sheet)+')':'')+
|
||||||
|
(c.quote?': "'+esc(c.quote)+'"':'')+
|
||||||
|
(c.why_it_matters?' — '+esc(c.why_it_matters):'')+'</div>';
|
||||||
|
});
|
||||||
|
const tags=[];
|
||||||
|
if(turn.answerable&&turn.answerable!=='yes') tags.push('answerable: '+turn.answerable);
|
||||||
|
if(turn.assessment_of_finding&&turn.assessment_of_finding!=='not_applicable')
|
||||||
|
tags.push(turn.assessment_of_finding.replace(/_/g,' '));
|
||||||
|
if(turn.confidence) tags.push('confidence: '+turn.confidence);
|
||||||
|
if(turn.missing_information) tags.push('missing: '+turn.missing_information);
|
||||||
|
if(turn.suggested_category_correction)
|
||||||
|
tags.push('correction noted: '+turn.suggested_category_correction);
|
||||||
|
if(tags.length) html+='<div class="chat-tags">'+esc(tags.join(' \u00b7 '))+'</div>';
|
||||||
|
return html+'</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderChatTurns(panel,turns){
|
||||||
|
const box=panel.querySelector('.chat-turns');
|
||||||
|
box.innerHTML=turns.length?turns.map(chatTurnHtml).join('')
|
||||||
|
:'<div class="meta">No questions asked yet.</div>';
|
||||||
|
box.scrollTop=box.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadChatHistory(){
|
||||||
|
let data;
|
||||||
|
try{
|
||||||
|
const res=await fetch('/jobs/'+currentJobId+'/review-chat');
|
||||||
|
if(!res.ok) return; // chat unavailable for this job: leave panels empty
|
||||||
|
data=await res.json();
|
||||||
|
}catch(err){ return; }
|
||||||
|
const byKey={};
|
||||||
|
(data.turns||[]).forEach(t=>{
|
||||||
|
const k=t.review_item_id||'';
|
||||||
|
(byKey[k]=byKey[k]||[]).push(t);
|
||||||
|
});
|
||||||
|
results.querySelectorAll('.chat').forEach(panel=>{
|
||||||
|
const key=panel.getAttribute('data-chat')||'';
|
||||||
|
renderChatTurns(panel,byKey[key]||[]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function wireChatPanels(){
|
||||||
|
results.querySelectorAll('.chat').forEach(panel=>{
|
||||||
|
panel.querySelector('.chat-toggle').addEventListener('click',()=>{
|
||||||
|
panel.querySelector('.chat-body').classList.toggle('hidden');
|
||||||
|
});
|
||||||
|
const send=panel.querySelector('.chat-send');
|
||||||
|
const box=panel.querySelector('textarea');
|
||||||
|
send.addEventListener('click',()=>askChat(panel));
|
||||||
|
// Enter sends, Shift+Enter newlines - the questions are usually one line.
|
||||||
|
box.addEventListener('keydown',e=>{
|
||||||
|
if(e.key==='Enter'&&!e.shiftKey){ e.preventDefault(); askChat(panel); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function askChat(panel){
|
||||||
|
const box=panel.querySelector('textarea');
|
||||||
|
const send=panel.querySelector('.chat-send');
|
||||||
|
const err=panel.querySelector('.chat-err');
|
||||||
|
const question=box.value.trim();
|
||||||
|
err.textContent='';
|
||||||
|
if(!question) return;
|
||||||
|
const key=panel.getAttribute('data-chat')||'';
|
||||||
|
const body={question:question};
|
||||||
|
if(key) body.review_item_id=key;
|
||||||
|
send.disabled=true; send.textContent='Asking...';
|
||||||
|
try{
|
||||||
|
const res=await fetch('/jobs/'+currentJobId+'/review-chat',
|
||||||
|
{method:'POST',headers:{'Content-Type':'application/json'},
|
||||||
|
body:JSON.stringify(body)});
|
||||||
|
if(!res.ok){
|
||||||
|
const e=await res.json().catch(()=>({detail:res.statusText}));
|
||||||
|
const detail=typeof e.detail==='string'?e.detail:JSON.stringify(e.detail);
|
||||||
|
throw new Error(detail||'Request failed');
|
||||||
|
}
|
||||||
|
const data=await res.json();
|
||||||
|
const turnsBox=panel.querySelector('.chat-turns');
|
||||||
|
if(turnsBox.querySelector('.meta')) turnsBox.innerHTML='';
|
||||||
|
turnsBox.insertAdjacentHTML('beforeend',chatTurnHtml(data.turn||{}));
|
||||||
|
turnsBox.scrollTop=turnsBox.scrollHeight;
|
||||||
|
box.value='';
|
||||||
|
}catch(e){ err.textContent=e.message; }
|
||||||
|
finally{ send.disabled=false; send.textContent='Ask'; }
|
||||||
|
}
|
||||||
|
|
||||||
function syncReviewControls(el){
|
function syncReviewControls(el){
|
||||||
const sel=el.querySelector('input[type=radio]:checked');
|
const sel=el.querySelector('input[type=radio]:checked');
|
||||||
const v=sel?sel.value:'';
|
const v=sel?sel.value:'';
|
||||||
|
|||||||
@@ -0,0 +1,198 @@
|
|||||||
|
"""Wave 6.5 Brain-directed clarification: planning unit + runner integration."""
|
||||||
|
|
||||||
|
import backend.agents.brain as brain_mod
|
||||||
|
import backend.agents.runner as runner_mod
|
||||||
|
from backend import config
|
||||||
|
from backend.agents.base import AgentResult
|
||||||
|
from backend.agents.base import AgentUsage
|
||||||
|
from backend.agents.brain import BrainAgent
|
||||||
|
from backend.agents.runner import run_agent_pipeline
|
||||||
|
|
||||||
|
|
||||||
|
# --- plan_clarifications unit tests ---------------------------------------
|
||||||
|
|
||||||
|
def _finding(issue_id, **kw):
|
||||||
|
base = {"issue_id": issue_id, "severity": "medium", "confidence": "low",
|
||||||
|
"source_stage": "conflict", "description": "d",
|
||||||
|
"evidence": [{"sheet": "A1", "source_text": "x"}]}
|
||||||
|
base.update(kw)
|
||||||
|
return base
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_caps_and_filters_unknown_ids(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "BRAIN_CLARIFY_MAX_REQUESTS", 2)
|
||||||
|
monkeypatch.setattr(brain_mod, "call_json", lambda **k: {"requests": [
|
||||||
|
{"issue_id": "A", "request_type": "verify_evidence", "reason": "thin"},
|
||||||
|
{"issue_id": "GHOST", "request_type": "verify_evidence", "reason": "x"},
|
||||||
|
{"issue_id": "B", "request_type": "verify_evidence", "reason": "amb"},
|
||||||
|
{"issue_id": "C", "request_type": "verify_evidence", "reason": "over cap"},
|
||||||
|
]})
|
||||||
|
prioritized = [_finding("A"), _finding("B"), _finding("C")]
|
||||||
|
reqs = BrainAgent(AgentUsage()).plan_clarifications(prioritized)
|
||||||
|
ids = [r["issue_id"] for r in reqs]
|
||||||
|
assert ids == ["A", "B"] # GHOST filtered, capped at 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_skips_already_verified(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "BRAIN_CLARIFY_MAX_REQUESTS", 8)
|
||||||
|
captured = {}
|
||||||
|
|
||||||
|
def fake(**kwargs):
|
||||||
|
captured["user_text"] = kwargs["user_text"]
|
||||||
|
return {"requests": [
|
||||||
|
{"issue_id": "A", "request_type": "verify_evidence", "reason": "y"},
|
||||||
|
]}
|
||||||
|
|
||||||
|
monkeypatch.setattr(brain_mod, "call_json", fake)
|
||||||
|
prioritized = [
|
||||||
|
_finding("A"),
|
||||||
|
_finding("V", verification={"status": "confirmed", "verdicts": []}),
|
||||||
|
]
|
||||||
|
reqs = BrainAgent(AgentUsage()).plan_clarifications(prioritized)
|
||||||
|
assert [r["issue_id"] for r in reqs] == ["A"]
|
||||||
|
# The already-verified finding must not even be offered to the model.
|
||||||
|
assert '"V"' not in captured["user_text"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_empty_on_call_failure(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "BRAIN_CLARIFY_MAX_REQUESTS", 8)
|
||||||
|
|
||||||
|
def boom(**k):
|
||||||
|
raise RuntimeError("brain down")
|
||||||
|
monkeypatch.setattr(brain_mod, "call_json", boom)
|
||||||
|
assert BrainAgent(AgentUsage()).plan_clarifications([_finding("A")]) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_no_requests_returns_empty(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "BRAIN_CLARIFY_MAX_REQUESTS", 8)
|
||||||
|
monkeypatch.setattr(brain_mod, "call_json", lambda **k: {"requests": []})
|
||||||
|
assert BrainAgent(AgentUsage()).plan_clarifications([_finding("A")]) == []
|
||||||
|
|
||||||
|
|
||||||
|
# --- runner-level integration ---------------------------------------------
|
||||||
|
|
||||||
|
def _stub_agent(artifacts):
|
||||||
|
return lambda usage: type("S", (), {
|
||||||
|
"name": "stub",
|
||||||
|
"run": lambda self, scope: AgentResult(
|
||||||
|
scope_id=scope.scope_id, artifacts=list(artifacts)),
|
||||||
|
})()
|
||||||
|
|
||||||
|
|
||||||
|
def _patch_pipeline(monkeypatch, brain_finding, plan_requests):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
runner_mod, "convert_pdf_to_images",
|
||||||
|
lambda path: [{"page_number": 1, "base64": "QUJD"}])
|
||||||
|
monkeypatch.setattr(runner_mod, "SheetExtractorAgent", _stub_agent([
|
||||||
|
{"sheet_number": "S401", "page_number": 1, "level": "roof",
|
||||||
|
"discipline": "S", "assertions": [
|
||||||
|
{"text": "(2) 2x6 STUD PACK", "object_type": "framing"},
|
||||||
|
{"text": "HSS16X4 beam", "object_type": "framing"},
|
||||||
|
]},
|
||||||
|
]))
|
||||||
|
monkeypatch.setattr(runner_mod, "SheetIndexAgent", _stub_agent([{}]))
|
||||||
|
monkeypatch.setattr(runner_mod, "JurisdictionAgent", _stub_agent([{}]))
|
||||||
|
monkeypatch.setattr(runner_mod, "LinkerAgent", _stub_agent([
|
||||||
|
{"key": "c1", "location": "roof beam pocket", "assertions": []},
|
||||||
|
]))
|
||||||
|
# A filler conflict finding so memory["findings"] is non-empty and wave 6
|
||||||
|
# actually invokes Brain.run (which our stub replaces with brain_finding).
|
||||||
|
monkeypatch.setattr(runner_mod, "ConflictCriticAgent", _stub_agent([
|
||||||
|
{"issue_id": "FILLER", "severity": "low", "confidence": "low",
|
||||||
|
"source_stage": "conflict", "sheets": [], "description": "filler",
|
||||||
|
"evidence": []},
|
||||||
|
]))
|
||||||
|
monkeypatch.setattr(runner_mod, "CodeAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(runner_mod, "ConstructabilityAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(runner_mod, "CompletenessAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(runner_mod, "DrawingIntegrityAgent", _stub_agent([]))
|
||||||
|
# Brain.run returns our finding; plan_clarifications returns the requests.
|
||||||
|
monkeypatch.setattr(
|
||||||
|
runner_mod, "BrainAgent",
|
||||||
|
lambda usage: type("B", (), {
|
||||||
|
"run": lambda self, findings, si, ju: ([dict(brain_finding)], []),
|
||||||
|
"plan_clarifications": lambda self, prioritized: list(plan_requests),
|
||||||
|
})())
|
||||||
|
|
||||||
|
|
||||||
|
def test_brain_clarify_refutes_and_suppresses(monkeypatch, tmp_path):
|
||||||
|
"""Brain flags a MEDIUM finding wave-5b's severity gate skipped; the
|
||||||
|
clarification verifier refutes it, so it moves to suppressed_issues."""
|
||||||
|
monkeypatch.setattr(config, "ENABLE_BRAIN_CLARIFY", True)
|
||||||
|
finding = {
|
||||||
|
"issue_id": "M1", "severity": "medium", "confidence": "low",
|
||||||
|
"source_stage": "conflict", "sheets": ["S401"],
|
||||||
|
"description": "beam bears on (2) 2x6 stud pack",
|
||||||
|
"evidence": [{"sheet": "S401", "source_text": "(2) 2x6 STUD PACK"}],
|
||||||
|
}
|
||||||
|
_patch_pipeline(monkeypatch, finding, [
|
||||||
|
{"issue_id": "M1", "request_type": "verify_evidence", "reason": "misread?"},
|
||||||
|
])
|
||||||
|
# The clarification verifier returns a 'corrected' verdict -> refuted.
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"backend.agents.verifier.call_json",
|
||||||
|
lambda **kwargs: {"verdicts": [
|
||||||
|
{"sheet": "S401", "source_text": "(2) 2x6 STUD PACK",
|
||||||
|
"verdict": "corrected", "actual_text": "(5) 2x6 STUD PACK",
|
||||||
|
"notes": "reads (5)"},
|
||||||
|
]})
|
||||||
|
pdf = tmp_path / "d.pdf"
|
||||||
|
pdf.write_bytes(b"%PDF-1.4\n")
|
||||||
|
report = run_agent_pipeline(str(pdf), out_dir=str(tmp_path),
|
||||||
|
require_review=False)
|
||||||
|
validated = report.get("validated_issues") or []
|
||||||
|
assert all(f.get("issue_id") != "M1" for f in validated) # dropped
|
||||||
|
assert [f["issue_id"] for f in report["suppressed_issues"]] == ["M1"]
|
||||||
|
assert report["suppressed_issues"][0]["verification"]["status"] == "refuted"
|
||||||
|
|
||||||
|
|
||||||
|
def test_brain_clarify_confirms_keeps_finding(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_BRAIN_CLARIFY", True)
|
||||||
|
finding = {
|
||||||
|
"issue_id": "M2", "severity": "medium", "confidence": "low",
|
||||||
|
"source_stage": "conflict", "sheets": ["S401"],
|
||||||
|
"description": "beam bears on (5) 2x6 stud pack",
|
||||||
|
"evidence": [{"sheet": "S401", "source_text": "(5) 2x6 STUD PACK"}],
|
||||||
|
}
|
||||||
|
_patch_pipeline(monkeypatch, finding, [
|
||||||
|
{"issue_id": "M2", "request_type": "verify_evidence", "reason": "check"},
|
||||||
|
])
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"backend.agents.verifier.call_json",
|
||||||
|
lambda **kwargs: {"verdicts": [
|
||||||
|
{"sheet": "S401", "source_text": "(5) 2x6 STUD PACK",
|
||||||
|
"verdict": "confirmed", "actual_text": None, "notes": None},
|
||||||
|
]})
|
||||||
|
pdf = tmp_path / "d.pdf"
|
||||||
|
pdf.write_bytes(b"%PDF-1.4\n")
|
||||||
|
report = run_agent_pipeline(str(pdf), out_dir=str(tmp_path),
|
||||||
|
require_review=False)
|
||||||
|
validated = report.get("validated_issues") or []
|
||||||
|
kept = [f for f in validated if f.get("issue_id") == "M2"]
|
||||||
|
assert len(kept) == 1
|
||||||
|
assert kept[0]["verification"]["status"] == "confirmed"
|
||||||
|
assert report["suppressed_issues"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_brain_clarify_disabled_is_noop(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_BRAIN_CLARIFY", False)
|
||||||
|
finding = {
|
||||||
|
"issue_id": "M3", "severity": "medium", "confidence": "low",
|
||||||
|
"source_stage": "conflict", "sheets": ["S401"],
|
||||||
|
"description": "d", "evidence": [{"sheet": "S401", "source_text": "t"}],
|
||||||
|
}
|
||||||
|
# plan_clarifications should never be consulted; give it a bomb to prove it.
|
||||||
|
def _bomb(self, prioritized):
|
||||||
|
raise AssertionError("plan_clarifications must not run when disabled")
|
||||||
|
_patch_pipeline(monkeypatch, finding, [])
|
||||||
|
monkeypatch.setattr(
|
||||||
|
runner_mod, "BrainAgent",
|
||||||
|
lambda usage: type("B", (), {
|
||||||
|
"run": lambda self, findings, si, ju: ([dict(finding)], []),
|
||||||
|
"plan_clarifications": _bomb})())
|
||||||
|
pdf = tmp_path / "d.pdf"
|
||||||
|
pdf.write_bytes(b"%PDF-1.4\n")
|
||||||
|
report = run_agent_pipeline(str(pdf), out_dir=str(tmp_path),
|
||||||
|
require_review=False)
|
||||||
|
validated = report.get("validated_issues") or []
|
||||||
|
assert any(f.get("issue_id") == "M3" for f in validated)
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
"""Unit tests for the per-sheet DrawingIntegrityAgent and scope builder."""
|
||||||
|
|
||||||
|
import backend.agents.integrity_agent as integ
|
||||||
|
from backend.agents.base import AgentScope, AgentUsage
|
||||||
|
from backend.agents.integrity_agent import (
|
||||||
|
DrawingIntegrityAgent, build_integrity_scopes,
|
||||||
|
)
|
||||||
|
from backend import config
|
||||||
|
|
||||||
|
|
||||||
|
def _sheet(page, sheet_number, n_assertions):
|
||||||
|
return {
|
||||||
|
"sheet_number": sheet_number,
|
||||||
|
"page_number": page,
|
||||||
|
"sheet_title": f"Sheet {sheet_number}",
|
||||||
|
"discipline": "Architectural",
|
||||||
|
"assertions": [
|
||||||
|
{"object_type": "note", "source_text": f"note {i}"}
|
||||||
|
for i in range(n_assertions)
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_scopes_skips_sparse_sheets(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "INTEGRITY_MIN_ASSERTIONS", 3)
|
||||||
|
sheets = [
|
||||||
|
_sheet(1, "A101", 5), # kept
|
||||||
|
_sheet(2, "A102", 2), # skipped (too sparse)
|
||||||
|
_sheet(3, "A103", 3), # kept (== floor)
|
||||||
|
]
|
||||||
|
page_to_b64 = {1: "IMG1", 2: "IMG2", 3: "IMG3"}
|
||||||
|
page_to_text = {1: "text one", 2: "text two", 3: "text three"}
|
||||||
|
scopes = build_integrity_scopes(sheets, page_to_b64, page_to_text)
|
||||||
|
ids = sorted(s.scope_id for s in scopes)
|
||||||
|
assert ids == ["integrity:1", "integrity:3"]
|
||||||
|
# Each scope carries its own page image + text layer.
|
||||||
|
by_id = {s.scope_id: s for s in scopes}
|
||||||
|
assert by_id["integrity:1"].payload["image_b64"] == "IMG1"
|
||||||
|
assert by_id["integrity:1"].payload["text_layer"] == "text one"
|
||||||
|
|
||||||
|
|
||||||
|
def test_agent_parses_and_anchors_sheet(monkeypatch):
|
||||||
|
"""Findings with blank sheets get anchored to the scope's sheet number."""
|
||||||
|
captured = {}
|
||||||
|
|
||||||
|
def fake_call_json(**kwargs):
|
||||||
|
captured.update(kwargs)
|
||||||
|
return {"issues": [
|
||||||
|
{"issue_id": "DI-1", "severity": "high", "confidence": "high",
|
||||||
|
"category": "dangling_reference", "sheets": [],
|
||||||
|
"description": "Detail callout 5/A101 has no detail 5 on this sheet",
|
||||||
|
"evidence": [{"sheet": "A101", "source_text": "5/A101"}]},
|
||||||
|
]}
|
||||||
|
|
||||||
|
monkeypatch.setattr(integ, "call_json", fake_call_json)
|
||||||
|
sheet = _sheet(1, "A101", 5)
|
||||||
|
scope = AgentScope("integrity:1", {
|
||||||
|
"sheet": sheet, "page_number": 1,
|
||||||
|
"image_b64": "IMG1", "text_layer": "the deterministic text layer",
|
||||||
|
})
|
||||||
|
result = DrawingIntegrityAgent(AgentUsage()).run(scope)
|
||||||
|
assert result.error == ""
|
||||||
|
assert len(result.artifacts) == 1
|
||||||
|
finding = result.artifacts[0]
|
||||||
|
assert finding["source_stage"] == "drawing_integrity"
|
||||||
|
assert finding["sheets"] == ["A101"] # anchored
|
||||||
|
assert finding["agent"] == "drawing_integrity"
|
||||||
|
assert finding["scope_id"] == "integrity:1"
|
||||||
|
# The image + text layer reached the model.
|
||||||
|
assert captured["images_b64"] == ["IMG1"]
|
||||||
|
assert "the deterministic text layer" in captured["user_text"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_agent_empty_issues_is_clean(monkeypatch):
|
||||||
|
monkeypatch.setattr(integ, "call_json", lambda **k: {"issues": []})
|
||||||
|
scope = AgentScope("integrity:1", {
|
||||||
|
"sheet": _sheet(1, "A101", 5), "page_number": 1,
|
||||||
|
"image_b64": "IMG1", "text_layer": "t",
|
||||||
|
})
|
||||||
|
result = DrawingIntegrityAgent(AgentUsage()).run(scope)
|
||||||
|
assert result.error == ""
|
||||||
|
assert result.artifacts == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_agent_survives_call_failure(monkeypatch):
|
||||||
|
def boom(**kwargs):
|
||||||
|
raise RuntimeError("model exploded")
|
||||||
|
monkeypatch.setattr(integ, "call_json", boom)
|
||||||
|
scope = AgentScope("integrity:1", {
|
||||||
|
"sheet": _sheet(1, "A101", 5), "page_number": 1,
|
||||||
|
"image_b64": "IMG1", "text_layer": "t",
|
||||||
|
})
|
||||||
|
result = DrawingIntegrityAgent(AgentUsage()).run(scope)
|
||||||
|
assert "model exploded" in result.error
|
||||||
|
assert result.artifacts == []
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
"""Flag-gating tests: ENABLE_CODE_REVIEW off skips code, drawing_integrity runs.
|
||||||
|
|
||||||
|
Runner-level smoke tests using stubbed agents (same pattern as
|
||||||
|
test_wave5b_suppression). Verifies the code/ADA wave is skipped when
|
||||||
|
ENABLE_CODE_REVIEW is false and the Drawing Integrity wave feeds findings
|
||||||
|
into the report by_stage counters.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import backend.agents.runner as runner_mod
|
||||||
|
from backend import config
|
||||||
|
from backend.agents.base import AgentResult
|
||||||
|
from backend.agents.runner import run_agent_pipeline
|
||||||
|
|
||||||
|
|
||||||
|
def _stub_agent(artifacts):
|
||||||
|
return lambda usage: type("S", (), {
|
||||||
|
"name": "stub",
|
||||||
|
"run": lambda self, scope: AgentResult(
|
||||||
|
scope_id=scope.scope_id, artifacts=list(artifacts)),
|
||||||
|
})()
|
||||||
|
|
||||||
|
|
||||||
|
def _integrity_finding():
|
||||||
|
return {
|
||||||
|
"issue_id": "DI-1", "severity": "high", "confidence": "high",
|
||||||
|
"source_stage": "drawing_integrity", "sheets": ["A101"],
|
||||||
|
"category": "dangling_reference",
|
||||||
|
"description": "Detail callout 5/A101 has no detail 5 on this sheet",
|
||||||
|
"evidence": [{"sheet": "A101", "source_text": "5/A101"}],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _patch(monkeypatch, code_should_raise):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
runner_mod, "convert_pdf_to_images",
|
||||||
|
lambda path: [{"page_number": 1, "base64": "QUJD"}])
|
||||||
|
# Sheet has 3+ assertions so the integrity wave does NOT skip it.
|
||||||
|
monkeypatch.setattr(runner_mod, "SheetExtractorAgent", _stub_agent([
|
||||||
|
{"sheet_number": "A101", "page_number": 1, "level": "1",
|
||||||
|
"discipline": "A", "assertions": [
|
||||||
|
{"text": "5/A101", "object_type": "detail_marker"},
|
||||||
|
{"text": "ROOM 101", "object_type": "room"},
|
||||||
|
{"text": "DOOR 101A", "object_type": "door"},
|
||||||
|
]},
|
||||||
|
]))
|
||||||
|
monkeypatch.setattr(runner_mod, "SheetIndexAgent", _stub_agent([{}]))
|
||||||
|
monkeypatch.setattr(runner_mod, "JurisdictionAgent", _stub_agent([{}]))
|
||||||
|
monkeypatch.setattr(runner_mod, "LinkerAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(runner_mod, "ConflictCriticAgent", _stub_agent([]))
|
||||||
|
|
||||||
|
def code_boom(usage):
|
||||||
|
if code_should_raise:
|
||||||
|
raise AssertionError("CodeAgent must not run when gated off")
|
||||||
|
return _stub_agent([])(usage)
|
||||||
|
monkeypatch.setattr(runner_mod, "CodeAgent", code_boom)
|
||||||
|
|
||||||
|
monkeypatch.setattr(runner_mod, "DrawingIntegrityAgent",
|
||||||
|
_stub_agent([_integrity_finding()]))
|
||||||
|
monkeypatch.setattr(runner_mod, "ConstructabilityAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(runner_mod, "CompletenessAgent", _stub_agent([]))
|
||||||
|
monkeypatch.setattr(
|
||||||
|
runner_mod, "BrainAgent",
|
||||||
|
lambda usage: type("B", (), {
|
||||||
|
"run": lambda self, findings, si, ju: (list(findings), []),
|
||||||
|
"plan_clarifications": lambda self, prioritized: []})())
|
||||||
|
# Stub the wave-5b verifier so the high-severity integrity finding is
|
||||||
|
# confirmed (never a live network call).
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"backend.agents.verifier.call_json",
|
||||||
|
lambda **kwargs: {"verdicts": [
|
||||||
|
{"sheet": "A101", "source_text": "5/A101",
|
||||||
|
"verdict": "confirmed", "actual_text": None, "notes": None},
|
||||||
|
]})
|
||||||
|
|
||||||
|
|
||||||
|
def test_code_gated_off_integrity_on(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_CODE_REVIEW", False)
|
||||||
|
monkeypatch.setattr(config, "ENABLE_DRAWING_INTEGRITY", True)
|
||||||
|
_patch(monkeypatch, code_should_raise=True)
|
||||||
|
pdf = tmp_path / "d.pdf"
|
||||||
|
pdf.write_bytes(b"%PDF-1.4\n")
|
||||||
|
report = run_agent_pipeline(str(pdf), out_dir=str(tmp_path),
|
||||||
|
require_review=False)
|
||||||
|
by_stage = report["summary"]["by_stage"]
|
||||||
|
assert by_stage["code"] == 0
|
||||||
|
assert by_stage["drawing_integrity"] == 1
|
||||||
|
# The integrity finding survived into the validated set.
|
||||||
|
assert any(f.get("issue_id") == "DI-1"
|
||||||
|
for f in report.get("validated_issues") or [])
|
||||||
|
|
||||||
|
|
||||||
|
def test_code_enabled_runs(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_CODE_REVIEW", True)
|
||||||
|
monkeypatch.setattr(config, "ENABLE_DRAWING_INTEGRITY", True)
|
||||||
|
_patch(monkeypatch, code_should_raise=False)
|
||||||
|
# build_code_scopes runs on the real sheet; CodeAgent is stubbed to []
|
||||||
|
pdf = tmp_path / "d.pdf"
|
||||||
|
pdf.write_bytes(b"%PDF-1.4\n")
|
||||||
|
report = run_agent_pipeline(str(pdf), out_dir=str(tmp_path),
|
||||||
|
require_review=False)
|
||||||
|
# No crash; integrity still reported.
|
||||||
|
assert report["summary"]["by_stage"]["drawing_integrity"] == 1
|
||||||
@@ -4,7 +4,7 @@ from backend.agents.runner import run_agent_pipeline
|
|||||||
|
|
||||||
def _patch_brain(monkeypatch):
|
def _patch_brain(monkeypatch):
|
||||||
monkeypatch.setattr("backend.agents.runner.convert_pdf_to_images", lambda path: [{"page_number": 1, "base64": "x"}])
|
monkeypatch.setattr("backend.agents.runner.convert_pdf_to_images", lambda path: [{"page_number": 1, "base64": "x"}])
|
||||||
monkeypatch.setattr("backend.agents.runner.BrainAgent", lambda usage: type("B", (), {"run": lambda self, findings, sheet_index, jurisdiction: ([{"issue_id": "AGENT-0001", "severity": "high", "confidence": "high", "category": "note_or_spec_contradiction", "source_stage": "conflict"}], [])})())
|
monkeypatch.setattr("backend.agents.runner.BrainAgent", lambda usage: type("B", (), {"run": lambda self, findings, sheet_index, jurisdiction: ([{"issue_id": "AGENT-0001", "severity": "high", "confidence": "high", "category": "note_or_spec_contradiction", "source_stage": "conflict"}], []), "plan_clarifications": lambda self, prioritized: []})())
|
||||||
|
|
||||||
|
|
||||||
def test_agent_runner_can_enter_review_mode(monkeypatch, tmp_path):
|
def test_agent_runner_can_enter_review_mode(monkeypatch, tmp_path):
|
||||||
|
|||||||
@@ -63,7 +63,8 @@ def _patch_pipeline(monkeypatch, finding, verify_sink):
|
|||||||
runner_mod, "BrainAgent",
|
runner_mod, "BrainAgent",
|
||||||
lambda usage: type("B", (), {
|
lambda usage: type("B", (), {
|
||||||
"run": lambda self, findings, sheet_index, jurisdiction:
|
"run": lambda self, findings, sheet_index, jurisdiction:
|
||||||
(list(findings), [])})())
|
(list(findings), []),
|
||||||
|
"plan_clarifications": lambda self, prioritized: []})())
|
||||||
|
|
||||||
class _RecordingVerifier:
|
class _RecordingVerifier:
|
||||||
name = "verify"
|
name = "verify"
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ def _patch_pipeline(monkeypatch, finding):
|
|||||||
runner_mod, "BrainAgent",
|
runner_mod, "BrainAgent",
|
||||||
lambda usage: type("B", (), {
|
lambda usage: type("B", (), {
|
||||||
"run": lambda self, findings, sheet_index, jurisdiction:
|
"run": lambda self, findings, sheet_index, jurisdiction:
|
||||||
(list(findings), [])})())
|
(list(findings), []),
|
||||||
|
"plan_clarifications": lambda self, prioritized: []})())
|
||||||
|
|
||||||
|
|
||||||
def test_refuted_finding_is_suppressed_not_crash(monkeypatch, tmp_path):
|
def test_refuted_finding_is_suppressed_not_crash(monkeypatch, tmp_path):
|
||||||
|
|||||||
@@ -0,0 +1,139 @@
|
|||||||
|
"""API tests for the review-chat endpoints."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from backend.main import app
|
||||||
|
from backend.review.store import ReviewStore
|
||||||
|
|
||||||
|
|
||||||
|
def _queue_item() -> dict:
|
||||||
|
return {"review_item_id": "finding:AGENT-0007", "kind": "finding",
|
||||||
|
"blocking": True, "reasons": ["severity_high"],
|
||||||
|
"payload": {"issue_id": "AGENT-0007", "category": "elevation_disagreement",
|
||||||
|
"severity": "high", "sheets": ["M2.1"],
|
||||||
|
"description": "AC-1 at grade vs roof.",
|
||||||
|
"scope_id": "conflict:roof-ac1"}}
|
||||||
|
|
||||||
|
|
||||||
|
def _reply() -> dict:
|
||||||
|
return {"answer": "It read 'AC-1 MOUNTED ON GRADE' off M2.1.",
|
||||||
|
"findings": ["The grade value came from M2.1."],
|
||||||
|
"evidence_cited": [], "answerable": "yes",
|
||||||
|
"assessment_of_finding": "looks_supported", "confidence": "high"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def job(monkeypatch, tmp_path):
|
||||||
|
"""A finished, review-gated job with one queued finding and a stub model."""
|
||||||
|
store = ReviewStore(str(tmp_path))
|
||||||
|
store.write_queue([_queue_item()])
|
||||||
|
monkeypatch.setattr("backend.main.get_job", lambda job_id: {
|
||||||
|
"job_id": job_id, "status": "needs_review", "out_dir": str(tmp_path)})
|
||||||
|
monkeypatch.setattr("backend.review.chat.call_json", lambda **kw: _reply())
|
||||||
|
return str(tmp_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ask_about_a_finding_returns_and_logs_a_turn(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/jobs/job1/review-chat", json={
|
||||||
|
"question": "Why does it think AC-1 is at grade?",
|
||||||
|
"review_item_id": "finding:AGENT-0007"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
turn = response.json()["turn"]
|
||||||
|
assert turn["issue"]["issue_id"] == "AGENT-0007"
|
||||||
|
assert turn["findings"] == ["The grade value came from M2.1."]
|
||||||
|
with open(os.path.join(job, "review", "chat_log.jsonl"), encoding="utf-8") as f:
|
||||||
|
assert len([line for line in f if line.strip()]) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_ask_about_the_run_needs_no_item(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/jobs/job1/review-chat",
|
||||||
|
json={"question": "Why didn't it pick up the Civil set?"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json()["turn"]["scope"] == "run"
|
||||||
|
|
||||||
|
|
||||||
|
def test_history_endpoint_filters_by_item(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
client.post("/jobs/job1/review-chat", json={
|
||||||
|
"question": "Why grade?", "review_item_id": "finding:AGENT-0007"})
|
||||||
|
client.post("/jobs/job1/review-chat", json={"question": "Why no Civil?"})
|
||||||
|
assert len(client.get("/jobs/job1/review-chat").json()["turns"]) == 2
|
||||||
|
filtered = client.get("/jobs/job1/review-chat",
|
||||||
|
params={"review_item_id": "finding:AGENT-0007"}).json()
|
||||||
|
assert len(filtered["turns"]) == 1
|
||||||
|
assert filtered["turns"][0]["question"] == "Why grade?"
|
||||||
|
|
||||||
|
|
||||||
|
def test_transcript_endpoint_renders_markdown(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
client.post("/jobs/job1/review-chat", json={
|
||||||
|
"question": "Why grade?", "review_item_id": "finding:AGENT-0007"})
|
||||||
|
response = client.get("/jobs/job1/review-chat/log")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.headers["content-type"].startswith("text/markdown")
|
||||||
|
assert "## AGENT-0007" in response.text
|
||||||
|
assert "Why grade?" in response.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_blank_question_is_422(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
assert client.post("/jobs/job1/review-chat", json={"question": " "}).status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_item_is_422(job):
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/jobs/job1/review-chat",
|
||||||
|
json={"question": "why?", "review_item_id": "finding:NOPE"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_failure_is_502_not_500(monkeypatch, job):
|
||||||
|
monkeypatch.setattr("backend.review.chat.call_json", lambda **kw: None)
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/jobs/job1/review-chat", json={"question": "why?"})
|
||||||
|
assert response.status_code == 502
|
||||||
|
|
||||||
|
|
||||||
|
def test_chat_stays_available_after_the_job_is_done(monkeypatch, tmp_path):
|
||||||
|
"""The chat is read-only, so a finalized report can still be questioned."""
|
||||||
|
ReviewStore(str(tmp_path)).write_queue([_queue_item()])
|
||||||
|
monkeypatch.setattr("backend.main.get_job", lambda job_id: {
|
||||||
|
"job_id": job_id, "status": "done", "out_dir": str(tmp_path)})
|
||||||
|
monkeypatch.setattr("backend.review.chat.call_json", lambda **kw: _reply())
|
||||||
|
client = TestClient(app)
|
||||||
|
assert client.post("/jobs/job1/review-chat",
|
||||||
|
json={"question": "why?"}).status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_chat_is_409_while_the_job_is_still_running(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr("backend.main.get_job", lambda job_id: {
|
||||||
|
"job_id": job_id, "status": "running", "out_dir": str(tmp_path)})
|
||||||
|
client = TestClient(app)
|
||||||
|
assert client.post("/jobs/job1/review-chat",
|
||||||
|
json={"question": "why?"}).status_code == 409
|
||||||
|
|
||||||
|
|
||||||
|
def test_chat_404s_for_unknown_job(monkeypatch, tmp_path):
|
||||||
|
monkeypatch.setattr("backend.main.get_job", lambda job_id: None)
|
||||||
|
client = TestClient(app)
|
||||||
|
assert client.post("/jobs/nope/review-chat",
|
||||||
|
json={"question": "why?"}).status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_chat_never_mutates_review_decisions(job):
|
||||||
|
"""The whole point: asking questions cannot change the review state."""
|
||||||
|
client = TestClient(app)
|
||||||
|
before = ReviewStore(job, create=False).read_decisions()
|
||||||
|
client.post("/jobs/job1/review-chat", json={
|
||||||
|
"question": "This is wrong, reject it.",
|
||||||
|
"review_item_id": "finding:AGENT-0007"})
|
||||||
|
after = ReviewStore(job, create=False).read_decisions()
|
||||||
|
assert before == after == {}
|
||||||
|
with open(os.path.join(job, "review", "review_queue.json"), encoding="utf-8") as f:
|
||||||
|
assert json.load(f) == [_queue_item()]
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
"""Shared test fixtures."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def isolated_feedback_store(tmp_path, monkeypatch):
|
||||||
|
"""Keep the cross-job feedback store out of the real outputs directory.
|
||||||
|
|
||||||
|
Saving a review decision or asking a chat question appends to
|
||||||
|
config.REVIEW_FEEDBACK_DIR, which is process-wide rather than job-local.
|
||||||
|
Without this, running the suite would accumulate junk in backend/outputs.
|
||||||
|
"""
|
||||||
|
monkeypatch.setattr("backend.config.REVIEW_FEEDBACK_DIR",
|
||||||
|
str(tmp_path / "_feedback"))
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
"""Review chat: answer normalization, logging, and the feedback roll-up."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from backend import config
|
||||||
|
from backend.review import chat
|
||||||
|
|
||||||
|
|
||||||
|
def _queue() -> list:
|
||||||
|
return [{
|
||||||
|
"review_item_id": "finding:AGENT-0007", "kind": "finding", "blocking": True,
|
||||||
|
"reasons": ["severity_high"],
|
||||||
|
"payload": {"issue_id": "AGENT-0007", "source_stage": "conflict",
|
||||||
|
"category": "elevation_disagreement", "severity": "high",
|
||||||
|
"confidence": "medium", "location": "Roof / AC-1",
|
||||||
|
"sheets": ["M2.1"], "description": "AC-1 at grade vs roof.",
|
||||||
|
"evidence": [], "scope_id": "conflict:roof-ac1"},
|
||||||
|
}]
|
||||||
|
|
||||||
|
|
||||||
|
def _model_reply(**overrides) -> dict:
|
||||||
|
reply = {
|
||||||
|
"answer": "The extractor read 'AC-1 MOUNTED ON GRADE' off M2.1.",
|
||||||
|
"findings": ["The grade reading came from M2.1's text layer."],
|
||||||
|
"evidence_cited": [{"artifact": "source_sheets[M2.1]", "sheet": "M2.1",
|
||||||
|
"quote": "AC-1 MOUNTED ON GRADE",
|
||||||
|
"why_it_matters": "It is the sole basis for 'grade'."}],
|
||||||
|
"answerable": "yes",
|
||||||
|
"missing_information": None,
|
||||||
|
"assessment_of_finding": "looks_supported",
|
||||||
|
"suggested_category_correction": None,
|
||||||
|
"confidence": "high",
|
||||||
|
}
|
||||||
|
reply.update(overrides)
|
||||||
|
return reply
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def fake_llm(monkeypatch):
|
||||||
|
"""Stub the model; the chat must never need a network to be tested."""
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def _call(**kwargs):
|
||||||
|
calls.append(kwargs)
|
||||||
|
return calls_reply[0]
|
||||||
|
|
||||||
|
calls_reply = [_model_reply()]
|
||||||
|
monkeypatch.setattr("backend.review.chat.call_json", lambda **kw: _call(**kw))
|
||||||
|
return calls, calls_reply
|
||||||
|
|
||||||
|
|
||||||
|
def test_ask_logs_issue_question_and_findings(tmp_path, fake_llm):
|
||||||
|
turn = chat.ask("job1", str(tmp_path), "Why is AC-1 at grade?",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
assert turn["question"] == "Why is AC-1 at grade?"
|
||||||
|
assert turn["findings"] == ["The grade reading came from M2.1's text layer."]
|
||||||
|
# The log's "issue in question" is a snapshot, not a bare id.
|
||||||
|
assert turn["issue"]["issue_id"] == "AGENT-0007"
|
||||||
|
assert turn["issue"]["severity"] == "high"
|
||||||
|
path = os.path.join(str(tmp_path), "review", "chat_log.jsonl")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
logged = [json.loads(line) for line in f if line.strip()]
|
||||||
|
assert len(logged) == 1
|
||||||
|
assert logged[0]["turn_id"] == turn["turn_id"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_ask_appends_to_the_cross_job_feedback_store(tmp_path, fake_llm):
|
||||||
|
chat.ask("job1", str(tmp_path), "Is this really a floor drain?",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
path = os.path.join(config.REVIEW_FEEDBACK_DIR, "chat_turns.jsonl")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
records = [json.loads(line) for line in f if line.strip()]
|
||||||
|
assert records[0]["kind"] == "review_chat_turn"
|
||||||
|
assert records[0]["issue_id"] == "AGENT-0007"
|
||||||
|
assert records[0]["job_id"] == "job1"
|
||||||
|
|
||||||
|
|
||||||
|
def test_correction_signal_is_captured_as_structured_data(tmp_path, fake_llm):
|
||||||
|
"""A misidentification correction survives as a field, not free text."""
|
||||||
|
_, reply = fake_llm
|
||||||
|
reply[0] = _model_reply(suggested_category_correction="power floor box",
|
||||||
|
assessment_of_finding="looks_unsupported")
|
||||||
|
turn = chat.ask("job1", str(tmp_path), "That is not a floor drain.",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
assert turn["suggested_category_correction"] == "power floor box"
|
||||||
|
path = os.path.join(config.REVIEW_FEEDBACK_DIR, "chat_turns.jsonl")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
record = json.loads(f.readline())
|
||||||
|
assert record["suggested_category_correction"] == "power floor box"
|
||||||
|
assert record["assessment_of_finding"] == "looks_unsupported"
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_scope_question_needs_no_item(tmp_path, fake_llm):
|
||||||
|
turn = chat.ask("job1", str(tmp_path), "Why didn't it pick up the Civil set?")
|
||||||
|
assert turn["review_item_id"] is None
|
||||||
|
assert turn["scope"] == "run"
|
||||||
|
assert turn["issue"] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_history_is_replayed_for_the_same_thread(tmp_path, fake_llm):
|
||||||
|
calls, _ = fake_llm
|
||||||
|
chat.ask("job1", str(tmp_path), "First question?",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
chat.ask("job1", str(tmp_path), "Follow-up?",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
assert "First question?" in calls[1]["user_text"]
|
||||||
|
# A run-scope turn must not inherit an item thread's history.
|
||||||
|
chat.ask("job1", str(tmp_path), "Unrelated run question?")
|
||||||
|
assert "First question?" not in calls[2]["user_text"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_blank_and_oversized_questions_are_rejected(tmp_path, fake_llm):
|
||||||
|
with pytest.raises(chat.ChatError):
|
||||||
|
chat.ask("job1", str(tmp_path), " ")
|
||||||
|
with pytest.raises(chat.ChatError):
|
||||||
|
chat.ask("job1", str(tmp_path),
|
||||||
|
"x" * (config.REVIEW_CHAT_MAX_QUESTION_CHARS + 1))
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_review_item_is_rejected(tmp_path, fake_llm):
|
||||||
|
with pytest.raises(chat.ChatError):
|
||||||
|
chat.ask("job1", str(tmp_path), "why?", review_item_id="finding:NOPE",
|
||||||
|
queue=_queue())
|
||||||
|
|
||||||
|
|
||||||
|
def test_unusable_model_reply_raises_and_logs_nothing(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setattr("backend.review.chat.call_json", lambda **kw: None)
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
chat.ask("job1", str(tmp_path), "why?")
|
||||||
|
assert not os.path.exists(os.path.join(str(tmp_path), "review", "chat_log.jsonl"))
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_enum_values_fall_back_instead_of_failing(tmp_path, fake_llm):
|
||||||
|
_, reply = fake_llm
|
||||||
|
reply[0] = _model_reply(answerable="probably", confidence="",
|
||||||
|
assessment_of_finding="made_up")
|
||||||
|
turn = chat.ask("job1", str(tmp_path), "why?")
|
||||||
|
assert turn["answerable"] == "partial"
|
||||||
|
assert turn["confidence"] == "low"
|
||||||
|
assert turn["assessment_of_finding"] == "cannot_tell"
|
||||||
|
|
||||||
|
|
||||||
|
def test_disabled_chat_refuses(tmp_path, monkeypatch, fake_llm):
|
||||||
|
monkeypatch.setattr("backend.config.ENABLE_REVIEW_CHAT", False)
|
||||||
|
with pytest.raises(chat.ChatError):
|
||||||
|
chat.ask("job1", str(tmp_path), "why?")
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_log_skips_corrupt_lines(tmp_path, fake_llm):
|
||||||
|
chat.ask("job1", str(tmp_path), "why?")
|
||||||
|
path = os.path.join(str(tmp_path), "review", "chat_log.jsonl")
|
||||||
|
with open(path, "a", encoding="utf-8") as f:
|
||||||
|
f.write("{not json\n")
|
||||||
|
assert len(chat.read_log(str(tmp_path))) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_transcript_groups_by_issue(tmp_path, fake_llm):
|
||||||
|
chat.ask("job1", str(tmp_path), "Why is AC-1 at grade?",
|
||||||
|
review_item_id="finding:AGENT-0007", queue=_queue())
|
||||||
|
chat.ask("job1", str(tmp_path), "Why no Civil?")
|
||||||
|
markdown = chat.render_log_markdown(chat.read_log(str(tmp_path)))
|
||||||
|
assert "## AGENT-0007" in markdown
|
||||||
|
assert "## Run-scope questions" in markdown
|
||||||
|
assert "Why is AC-1 at grade?" in markdown
|
||||||
|
assert "**Findings**" in markdown
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_transcript_handles_empty_log():
|
||||||
|
assert "No questions" in chat.render_log_markdown([])
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
"""Context bundles for the review chat: what the model is allowed to see."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
from backend.review.chat_context import build_context
|
||||||
|
|
||||||
|
|
||||||
|
def _write(out_dir: str, name: str, value) -> None:
|
||||||
|
path = os.path.join(out_dir, name)
|
||||||
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||||
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(value, f)
|
||||||
|
|
||||||
|
|
||||||
|
def _finding() -> dict:
|
||||||
|
return {
|
||||||
|
"issue_id": "AGENT-0007",
|
||||||
|
"source_stage": "conflict",
|
||||||
|
"category": "elevation_disagreement",
|
||||||
|
"severity": "high",
|
||||||
|
"confidence": "medium",
|
||||||
|
"location": "Roof / AC-1",
|
||||||
|
"disciplines": ["Mechanical"],
|
||||||
|
"sheets": ["M2.1"],
|
||||||
|
"description": "AC-1 shown at grade on M2.1 but on the roof elsewhere.",
|
||||||
|
"evidence": [{"discipline": "Mechanical", "sheet": "M2.1",
|
||||||
|
"source_text": "AC-1 MOUNTED ON GRADE", "asserted_value": "grade"}],
|
||||||
|
"scope_id": "conflict:roof-ac1",
|
||||||
|
"verification": {"status": "unverified", "verdicts": []},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _queue() -> list:
|
||||||
|
return [{"review_item_id": "finding:AGENT-0007", "kind": "finding",
|
||||||
|
"blocking": True, "reasons": ["severity_high"], "payload": _finding()}]
|
||||||
|
|
||||||
|
|
||||||
|
def _job_dir(tmp_path) -> str:
|
||||||
|
out_dir = str(tmp_path)
|
||||||
|
_write(out_dir, "conflicts.json", {
|
||||||
|
"source": "set.pdf",
|
||||||
|
"summary": {"pipeline_mode": "agent", "agent_status": "needs_review",
|
||||||
|
"by_stage": {"conflicts": 3}, "conflicts_found": 3},
|
||||||
|
"sheet_index": {"sheet_index": [
|
||||||
|
{"sheet_number": "M2.1", "discipline": "Mechanical"},
|
||||||
|
{"sheet_number": "A1.1", "discipline": "Architectural"},
|
||||||
|
]},
|
||||||
|
"sheet_reconciliation": {"declared_total": 4, "found_total": 2,
|
||||||
|
"declared_not_in_set": ["C-001", "C-101"],
|
||||||
|
"in_set_not_declared": []},
|
||||||
|
})
|
||||||
|
_write(out_dir, "agent/memory.json", {
|
||||||
|
"sheets": [{
|
||||||
|
"sheet_number": "M2.1", "discipline": "Mechanical", "page_number": 7,
|
||||||
|
"assertions": [{"attribute": "mounting", "value": "grade",
|
||||||
|
"source_text": "AC-1 MOUNTED ON GRADE",
|
||||||
|
"base64": "SHOULD-NOT-APPEAR"}],
|
||||||
|
}],
|
||||||
|
"clusters": [{"key": "roof-ac1", "location": "Roof / AC-1",
|
||||||
|
"disciplines": ["Mechanical", "Architectural"],
|
||||||
|
"assertions": [{"sheet_number": "M2.1", "attribute": "mounting",
|
||||||
|
"value": "grade", "base64": "SHOULD-NOT-APPEAR"}]}],
|
||||||
|
"decisions": [{"finding_refs": ["AGENT-0007"], "action": "kept",
|
||||||
|
"reason": "supported", "kept_issue_id": "AGENT-0007"}],
|
||||||
|
"suppressed": [],
|
||||||
|
})
|
||||||
|
return out_dir
|
||||||
|
|
||||||
|
|
||||||
|
def test_item_scope_carries_the_reasoning_chain(tmp_path):
|
||||||
|
context = build_context(_job_dir(tmp_path), "finding:AGENT-0007", _queue())
|
||||||
|
assert context["scope"] == "item"
|
||||||
|
assert context["finding"]["issue_id"] == "AGENT-0007"
|
||||||
|
# The chain a "why does it think X" answer has to walk.
|
||||||
|
assert context["originating_cluster"]["key"] == "roof-ac1"
|
||||||
|
assert context["source_sheets"][0]["sheet_number"] == "M2.1"
|
||||||
|
assert context["brain_decisions"][0]["action"] == "kept"
|
||||||
|
assert context["finding"]["verification"]["status"] == "unverified"
|
||||||
|
|
||||||
|
|
||||||
|
def test_context_never_leaks_base64(tmp_path):
|
||||||
|
"""Page images blow up the prompt and are useless as quotable evidence."""
|
||||||
|
context = build_context(_job_dir(tmp_path), "finding:AGENT-0007", _queue())
|
||||||
|
assert "SHOULD-NOT-APPEAR" not in json.dumps(context)
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_scope_carries_coverage_material(tmp_path):
|
||||||
|
"""The 'why didn't it pick up the Civil set' inputs are all present."""
|
||||||
|
context = build_context(_job_dir(tmp_path), None, _queue(),
|
||||||
|
question="why didn't it pick up the Civil set?")
|
||||||
|
assert context["scope"] == "run"
|
||||||
|
assert set(context["sheets_by_discipline"]) == {"Mechanical", "Architectural"}
|
||||||
|
assert context["sheet_reconciliation"]["declared_not_in_set"] == ["C-001", "C-101"]
|
||||||
|
assert "finding" not in context
|
||||||
|
assert context["run"]["code_review_enabled"] in (True, False)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_item_falls_back_to_run_scope(tmp_path):
|
||||||
|
context = build_context(_job_dir(tmp_path), "finding:NOPE", _queue())
|
||||||
|
assert context["scope"] == "run"
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_artifacts_degrade_to_empty(tmp_path):
|
||||||
|
context = build_context(str(tmp_path), None, [])
|
||||||
|
assert context["scope"] == "run"
|
||||||
|
assert context["artifacts_available"] == {
|
||||||
|
"conflicts.json": False, "agent/memory.json": False, "job.log": False}
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_excerpt_matches_question_terms(tmp_path):
|
||||||
|
out_dir = _job_dir(tmp_path)
|
||||||
|
with open(os.path.join(out_dir, "job.log"), "w", encoding="utf-8") as f:
|
||||||
|
f.write("[Extract] page 3 Civil sheet unreadable, skipped\n")
|
||||||
|
f.write("[Brain] merged 2 findings\n")
|
||||||
|
context = build_context(out_dir, None, [], question="why no Civil sheets?")
|
||||||
|
assert any("Civil" in line for line in context["log_excerpt"])
|
||||||
|
assert context["artifacts_available"]["job.log"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_reviewer_decision_so_far_is_included(tmp_path):
|
||||||
|
decisions = {"finding:AGENT-0007": {"decision": "reject",
|
||||||
|
"reason_code": "extraction_misread",
|
||||||
|
"comment": "that is a power floor box"}}
|
||||||
|
context = build_context(_job_dir(tmp_path), "finding:AGENT-0007", _queue(), decisions)
|
||||||
|
assert context["reviewer_decision_so_far"]["reason_code"] == "extraction_misread"
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_cluster_item_uses_cluster_scope(tmp_path):
|
||||||
|
queue = [{"review_item_id": "clean_cluster:roof-ac1", "kind": "clean_cluster",
|
||||||
|
"blocking": False, "reasons": ["audit_sample"],
|
||||||
|
"payload": {"key": "roof-ac1", "location": "Roof / AC-1",
|
||||||
|
"assertions": [{"sheet_number": "M2.1", "value": "grade"}]}}]
|
||||||
|
context = build_context(_job_dir(tmp_path), "clean_cluster:roof-ac1", queue)
|
||||||
|
assert context["scope"] == "item"
|
||||||
|
assert context["cluster"]["key"] == "roof-ac1"
|
||||||
|
assert "finding" not in context
|
||||||
@@ -85,3 +85,34 @@ def test_write_label_appends_json_lines(tmp_path):
|
|||||||
with open(path, encoding="utf-8") as f:
|
with open(path, encoding="utf-8") as f:
|
||||||
lines = [json.loads(line) for line in f if line.strip()]
|
lines = [json.loads(line) for line in f if line.strip()]
|
||||||
assert lines == [label1, label2]
|
assert lines == [label1, label2]
|
||||||
|
|
||||||
|
|
||||||
|
def test_decision_label_carries_reviewer_corrections():
|
||||||
|
"""category/severity corrections reach the label instead of being dropped."""
|
||||||
|
decision = {"decision": "reject", "reason_code": "extraction_misread",
|
||||||
|
"category_correction": "power floor box",
|
||||||
|
"severity_correction": "low"}
|
||||||
|
label = decision_to_label(_queue_item(), decision, {"job_id": "abc123", "pipeline_mode": "agent",
|
||||||
|
"report": {"summary": {}}})
|
||||||
|
assert label["category_correction"] == "power floor box"
|
||||||
|
assert label["severity_correction"] == "low"
|
||||||
|
assert label["kind"] == "review_decision"
|
||||||
|
|
||||||
|
|
||||||
|
def test_write_label_also_lands_in_the_cross_job_store(tmp_path):
|
||||||
|
from backend import config
|
||||||
|
from backend.review.feedback import read_shared_feedback
|
||||||
|
label = decision_to_label(_queue_item(), {"decision": "reject",
|
||||||
|
"reason_code": "extraction_misread"},
|
||||||
|
{"job_id": "abc123", "pipeline_mode": "agent",
|
||||||
|
"report": {"summary": {}}})
|
||||||
|
write_label(str(tmp_path), label)
|
||||||
|
assert os.path.isfile(os.path.join(config.REVIEW_FEEDBACK_DIR, "decisions.jsonl"))
|
||||||
|
records = read_shared_feedback("review_decision")
|
||||||
|
assert len(records) == 1
|
||||||
|
assert records[0]["reason_code"] == "extraction_misread"
|
||||||
|
|
||||||
|
|
||||||
|
def test_shared_feedback_read_is_empty_when_nothing_written():
|
||||||
|
from backend.review.feedback import read_shared_feedback
|
||||||
|
assert read_shared_feedback("review_decision") == []
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
"""Tests for the classic-path drawing_integrity_review stage + gating."""
|
||||||
|
|
||||||
|
import backend.pipeline.drawing_integrity as di
|
||||||
|
from backend import config
|
||||||
|
from backend.pipeline.drawing_integrity import drawing_integrity_review
|
||||||
|
|
||||||
|
|
||||||
|
def _sheet(page, sheet_number, n):
|
||||||
|
return {
|
||||||
|
"sheet_number": sheet_number,
|
||||||
|
"page_number": page,
|
||||||
|
"discipline": "Architectural",
|
||||||
|
"assertions": [{"source_text": f"n{i}"} for i in range(n)],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _pages(pages):
|
||||||
|
return [{"page_number": p, "base64": f"IMG{p}", "text_layer": f"txt{p}"}
|
||||||
|
for p in pages]
|
||||||
|
|
||||||
|
|
||||||
|
def test_disabled_returns_empty(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_DRAWING_INTEGRITY", False)
|
||||||
|
called = []
|
||||||
|
monkeypatch.setattr(di, "call_stage", lambda *a, **k: called.append(1) or {})
|
||||||
|
out = drawing_integrity_review([_sheet(1, "A101", 5)], _pages([1]))
|
||||||
|
assert out == []
|
||||||
|
assert called == [] # no LLM calls when disabled
|
||||||
|
|
||||||
|
|
||||||
|
def test_reviews_only_dense_sheets(monkeypatch):
|
||||||
|
monkeypatch.setattr(config, "ENABLE_DRAWING_INTEGRITY", True)
|
||||||
|
monkeypatch.setattr(config, "INTEGRITY_MIN_ASSERTIONS", 3)
|
||||||
|
seen_sheets = []
|
||||||
|
|
||||||
|
def fake_call_stage(system, user, subs=None, images_b64=None, **k):
|
||||||
|
# Record which sheet_meta reached the model.
|
||||||
|
seen_sheets.append(subs["sheet_meta"])
|
||||||
|
return {"issues": [
|
||||||
|
{"severity": "medium", "confidence": "high",
|
||||||
|
"category": "on_sheet_contradiction", "sheets": [],
|
||||||
|
"description": "plan disagrees with same-sheet schedule"},
|
||||||
|
]}
|
||||||
|
|
||||||
|
monkeypatch.setattr(di, "call_stage", fake_call_stage)
|
||||||
|
sheets = [_sheet(1, "A101", 5), _sheet(2, "A102", 1), _sheet(3, "A103", 4)]
|
||||||
|
out = drawing_integrity_review(sheets, _pages([1, 2, 3]))
|
||||||
|
# Two dense sheets reviewed, one skipped; each produced one anchored finding.
|
||||||
|
assert len(out) == 2
|
||||||
|
assert all(f["source_stage"] == "drawing_integrity" for f in out)
|
||||||
|
assert {tuple(f["sheets"]) for f in out} == {("A101",), ("A103",)}
|
||||||
|
assert len(seen_sheets) == 2
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""Regression tests for defects found in the Aug 2026 agent-mode code review.
|
||||||
|
|
||||||
|
R2 - text_coverage._SHEET_ID_RE could not match hyphenated sheet ids (C-001),
|
||||||
|
leaving civil/landscape pages sheet_number=None and producing false
|
||||||
|
"declared but not in set" reconciliation warnings.
|
||||||
|
R3 - config bool knobs mixed `== "true"` with the 1/true/yes set, so setting
|
||||||
|
EXTRACT_TEXT_RETRY_ENABLED=1 silently DISABLED the retry ladder.
|
||||||
|
|
||||||
|
See also tests/agents/test_verifier.py for the verifier's "corrected"
|
||||||
|
semantics, which are intentional and pinned there.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
import os
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from backend.sheet_reconcile import declared_sheet_list, reconcile_sheets
|
||||||
|
from backend.text_coverage import recover_sheet_number
|
||||||
|
|
||||||
|
|
||||||
|
# --- R2: hyphenated sheet ids ----------------------------------------------
|
||||||
|
|
||||||
|
def test_recover_sheet_number_handles_hyphenated_civil_id():
|
||||||
|
page_text = ("GENERAL NOTES\n" * 40) + "PROJECT NO 2024-118\nSHEET\nC-001\n"
|
||||||
|
assert recover_sheet_number(page_text) == "C-001"
|
||||||
|
|
||||||
|
|
||||||
|
def test_recover_sheet_number_still_handles_plain_ids():
|
||||||
|
page_text = ("NOTES\n" * 40) + "SHEET\nS302\n"
|
||||||
|
assert recover_sheet_number(page_text) == "S302"
|
||||||
|
|
||||||
|
|
||||||
|
def test_recovered_hyphenated_id_reconciles_against_declared_index():
|
||||||
|
"""The whole point: a recovered C-001 must not read as a missing sheet."""
|
||||||
|
declared = declared_sheet_list({1: "SHEET LIST\nC-001 CIVIL\nA102 PLAN\n"})
|
||||||
|
assert declared == ["C-001", "A102"]
|
||||||
|
recovered = recover_sheet_number(("X\n" * 40) + "SHEET\nC-001\n")
|
||||||
|
recon = reconcile_sheets(
|
||||||
|
[{"sheet_number": recovered}, {"sheet_number": "A102"}], declared)
|
||||||
|
assert recon["declared_not_in_set"] == []
|
||||||
|
assert recon["in_set_not_declared"] == []
|
||||||
|
|
||||||
|
|
||||||
|
# --- R3: boolean env knob parsing ------------------------------------------
|
||||||
|
|
||||||
|
def test_numeric_one_enables_ladder_knobs():
|
||||||
|
with mock.patch.dict(os.environ, {
|
||||||
|
"EXTRACT_TEXT_RETRY_ENABLED": "1",
|
||||||
|
"EXTRACT_FALLBACK_ENABLED": "yes",
|
||||||
|
}):
|
||||||
|
cfg = importlib.reload(importlib.import_module("backend.config"))
|
||||||
|
try:
|
||||||
|
assert cfg.EXTRACT_TEXT_RETRY_ENABLED is True
|
||||||
|
assert cfg.EXTRACT_FALLBACK_ENABLED is True
|
||||||
|
finally:
|
||||||
|
importlib.reload(cfg)
|
||||||
|
|
||||||
|
|
||||||
|
def test_false_values_still_disable_ladder_knobs():
|
||||||
|
with mock.patch.dict(os.environ, {
|
||||||
|
"EXTRACT_TEXT_RETRY_ENABLED": "false",
|
||||||
|
"EXTRACT_FALLBACK_ENABLED": "0",
|
||||||
|
}):
|
||||||
|
cfg = importlib.reload(importlib.import_module("backend.config"))
|
||||||
|
try:
|
||||||
|
assert cfg.EXTRACT_TEXT_RETRY_ENABLED is False
|
||||||
|
assert cfg.EXTRACT_FALLBACK_ENABLED is False
|
||||||
|
finally:
|
||||||
|
importlib.reload(cfg)
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
"""Deterministic sheet-list reconciliation: cover index vs extracted sheets."""
|
||||||
|
|
||||||
|
from backend.sheet_reconcile import declared_sheet_list, reconcile_sheets
|
||||||
|
|
||||||
|
COVER_TEXT = """VERIZON CYPRESS
|
||||||
|
SHEET LIST
|
||||||
|
SHEET NUMBER
|
||||||
|
SHEET NAME
|
||||||
|
G000
|
||||||
|
COVER
|
||||||
|
G001
|
||||||
|
GENERAL INFO
|
||||||
|
C-001
|
||||||
|
CIVIL COVER
|
||||||
|
C-001.1
|
||||||
|
ALTA SURVEY
|
||||||
|
L-101
|
||||||
|
LANDSCAPE PLAN
|
||||||
|
S101
|
||||||
|
FOUNDATION PLAN
|
||||||
|
S301
|
||||||
|
WALL SECTIONS
|
||||||
|
S401
|
||||||
|
PERSPECTIVE VIEW
|
||||||
|
A101
|
||||||
|
FLOOR PLAN
|
||||||
|
A102
|
||||||
|
REFLECTED CEILING PLAN
|
||||||
|
E400
|
||||||
|
ELECTRICAL SITE PLAN
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def test_declared_sheet_list_from_cover():
|
||||||
|
declared = declared_sheet_list({1: COVER_TEXT, 2: "symbols legend"})
|
||||||
|
assert declared[0] == "G000"
|
||||||
|
assert "C-001" in declared and "C-001.1" in declared # hyphenated ids kept
|
||||||
|
assert "L-101" in declared
|
||||||
|
assert "A102" in declared
|
||||||
|
assert declared.count("G000") == 1
|
||||||
|
assert len(declared) == 11
|
||||||
|
|
||||||
|
|
||||||
|
def test_declared_sheet_list_uses_first_index_page_only():
|
||||||
|
texts = {1: "no index here", 2: COVER_TEXT, 3: "SHEET LIST\nXX999\nBOGUS"}
|
||||||
|
declared = declared_sheet_list(texts)
|
||||||
|
assert "XX999" not in declared # only the first marker page is parsed
|
||||||
|
|
||||||
|
|
||||||
|
def test_declared_sheet_list_none_when_no_marker():
|
||||||
|
assert declared_sheet_list({1: "just notes", 2: "floor plan stuff"}) == []
|
||||||
|
|
||||||
|
|
||||||
|
def _sheets(*nums):
|
||||||
|
return [{"page_number": i + 1, "sheet_number": n}
|
||||||
|
for i, n in enumerate(nums)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_reconcile_both_directions():
|
||||||
|
declared = declared_sheet_list({1: COVER_TEXT})
|
||||||
|
rec = reconcile_sheets(_sheets("G000", "G001", "S101", "S301", "S302", "A101"),
|
||||||
|
declared)
|
||||||
|
# declared but not extracted (civil/landscape not in this PDF + missing)
|
||||||
|
assert "C-001" in rec["declared_not_in_set"]
|
||||||
|
assert "A102" in rec["declared_not_in_set"]
|
||||||
|
assert "E400" in rec["declared_not_in_set"]
|
||||||
|
# extracted but not on the cover index (misread or unlisted sheet)
|
||||||
|
assert rec["in_set_not_declared"] == ["S302"]
|
||||||
|
assert rec["declared_total"] == 11
|
||||||
|
assert rec["found_total"] == 6
|
||||||
|
|
||||||
|
|
||||||
|
def test_reconcile_normalizes_hyphens():
|
||||||
|
declared = ["C-001", "S301"]
|
||||||
|
rec = reconcile_sheets(_sheets("C001", "S301"), declared)
|
||||||
|
assert rec["declared_not_in_set"] == []
|
||||||
|
assert rec["in_set_not_declared"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_reconcile_ignores_unidentified_sheets():
|
||||||
|
rec = reconcile_sheets(
|
||||||
|
[{"page_number": 8, "sheet_number": None},
|
||||||
|
{"page_number": 9, "sheet_number": "S301"}],
|
||||||
|
["S301", "A102"])
|
||||||
|
assert rec["found_total"] == 1
|
||||||
|
assert rec["declared_not_in_set"] == ["A102"]
|
||||||
Reference in new issue
Block a user