feat(review): 审查阶段对齐绘图区分辨率 + 放大取证闭环
发现的缺口:maximized_drawing_area 原先只包住取证循环,审查阶段没被包住 → LLM 在受控子循环里 zoom + 截图拿到的是未最大化的 2110x462(正是上一轮刚解决的 低分辨率问题,在最需要放大看细节的审查阶段又出现了);且审查中的 zoom 不复位视口, 跑完把用户的 CAD 留在最后一次放大处。 改动: 1. __main__.run_review:把 maximized_drawing_area 从「只包取证」扩到「包取证 + 审查」 (两者都要截图);report/annotate 留在 with 之外。 2. phases/review.py:新增 _VIEW_TOOLS;review_frame 拆成外壳 + _review_frame, 外壳 try/finally 里仅当确实动过视口才复位到该图框整幅视图 (zoom_to_window(win, margin=0.02))。纯查结构化数据的路径不受影响。 3. checklist.build_system_prompt:把「可调用只读工具」改写成明确的放大取证协议—— 截图看不清时用 zoom_to_window(坐标由图框/实体 bbox 算)或 zoom_to_entity, 再 get_viewport_screenshot 复核;并约束不要为看图而反复放大。 次生问题(同轮修复):_MAX_OBS_IMAGES 原为硬编码 3,而模型一轮内会发 8 次截图 → 拍了 8 张只回灌 3 张。改为配置项 REVIEW_OBS_IMAGES(默认 6),并把预算耗尽的占位 文案改成明确告知模型「不要再请求截图,请给出结论」。 实测(真实图纸 1 图框,新增 verify_review_maximized.py):取证与审查两阶段窗口均为 [-4,-2,3444,1442](最大化态),结束精确还原 [68,0,2316,960],三项全 PASS; 子循环里 LLM 连发 8 组 zoom_to_window + get_viewport_screenshot,提示词生效。 pytest 304 passed。
This commit is contained in:
1 parent
931a430ba4
commit
e8140f2bc5
11 files changed
+320
-40
No files matched your search
+31
-30
@@ -62,11 +62,13 @@ def run_review(ctx, args):
|
||||
|
||||
system_prompt = build_system_prompt(ctx.checklist, allow_tools=True)
|
||||
|
||||
print("[2/4] 取证:逐图框采集(串行) ...")
|
||||
evidences = []
|
||||
# 截屏前把绘图区最大化(窗口铺满 + 干净屏幕),采集完在 finally 里还原;
|
||||
# 旧版插件没有该工具时静默降级。
|
||||
frame_findings = []
|
||||
|
||||
# 截屏前把绘图区最大化(窗口铺满 + 干净屏幕)。**取证与审查都要截图**——审查阶段的
|
||||
# 受控子循环会按需放大复核,故一并包住,结束在 finally 里还原;旧版插件无此工具时静默降级。
|
||||
with evidence_phase.maximized_drawing_area(ctx):
|
||||
print("[2/4] 取证:逐图框采集(串行) ...")
|
||||
for idx, frame in enumerate(frames, 1):
|
||||
print(
|
||||
f" - 图框 {idx}/{len(frames)} kind={frame.get('kind')} "
|
||||
@@ -82,35 +84,34 @@ def run_review(ctx, args):
|
||||
f"截图 {len(evidence_images(ev))} 张"
|
||||
)
|
||||
|
||||
if args.plan_only:
|
||||
path = _dump_evidence(evidences)
|
||||
print(f"[evidence] 已写入 {path}")
|
||||
return 0
|
||||
if args.plan_only:
|
||||
path = _dump_evidence(evidences)
|
||||
print(f"[evidence] 已写入 {path}")
|
||||
return 0
|
||||
|
||||
print("[3/4] 审查:确定性检查前置 + 逐图框 LLM 审查(含受控只读取证子循环) ...")
|
||||
frame_findings = []
|
||||
for ev in evidences:
|
||||
print(f" - 审查图框 {ev['index']} ...")
|
||||
checks = (
|
||||
checks_phase.run_deterministic_checks(ctx, ev)
|
||||
if config.DETERMINISTIC_CHECKS
|
||||
else []
|
||||
)
|
||||
conclusion = review.review_frame(ctx, ev, system_prompt)
|
||||
finding = findings_mod.build_finding(
|
||||
ev, conclusion, checks, validate_handles=config.VALIDATE_HANDLES
|
||||
)
|
||||
frame_findings.append(finding)
|
||||
print("[3/4] 审查:确定性检查前置 + 逐图框 LLM 审查(含受控只读取证子循环) ...")
|
||||
for ev in evidences:
|
||||
print(f" - 审查图框 {ev['index']} ...")
|
||||
checks = (
|
||||
checks_phase.run_deterministic_checks(ctx, ev)
|
||||
if config.DETERMINISTIC_CHECKS
|
||||
else []
|
||||
)
|
||||
conclusion = review.review_frame(ctx, ev, system_prompt)
|
||||
finding = findings_mod.build_finding(
|
||||
ev, conclusion, checks, validate_handles=config.VALIDATE_HANDLES
|
||||
)
|
||||
frame_findings.append(finding)
|
||||
|
||||
counts = finding["counts"]
|
||||
preview = finding["summary"]
|
||||
if len(preview) > 200:
|
||||
preview = preview[:200] + "..."
|
||||
print(
|
||||
f" 结论:{preview}"
|
||||
f"(确定性 {counts['deterministic']} / LLM {counts['llm']}"
|
||||
f" / 未核实 {counts['unverified']})"
|
||||
)
|
||||
counts = finding["counts"]
|
||||
preview = finding["summary"]
|
||||
if len(preview) > 200:
|
||||
preview = preview[:200] + "..."
|
||||
print(
|
||||
f" 结论:{preview}"
|
||||
f"(确定性 {counts['deterministic']} / LLM {counts['llm']}"
|
||||
f" / 未核实 {counts['unverified']})"
|
||||
)
|
||||
|
||||
print("[4/4] 汇总报告 ...")
|
||||
markdown = report.summarize(ctx, frame_findings)
|
||||
|
||||
@@ -53,8 +53,13 @@ def build_system_prompt(checklist, *, allow_tools=True):
|
||||
|
||||
if allow_tools:
|
||||
lines += [
|
||||
"你可以调用**只读**取证工具进一步确认细节(例如放大局部、查实体详情、查重复实体)。",
|
||||
"当结构化数据不足以判断时,先调用工具取证,再下结论。",
|
||||
"你可以调用**只读**取证工具进一步确认细节;**不要凭猜测下结论**:",
|
||||
" - **截图里的文字/标注/细节看不清时**:用 `zoom_to_window` 放大该区域"
|
||||
"(窗口坐标用图框 bbox 或实体 bbox 计算,可适当留边距),"
|
||||
"再 `get_viewport_screenshot` 复核;已知 handle 时也可直接 `zoom_to_entity` 放大该实体。",
|
||||
" - 结构化数据不足以判断时:用 `get_entity_detail` / `find_duplicate_entities` /"
|
||||
" `find_zero_length` / `get_layers` 等查证后再下结论。",
|
||||
" - 放大截图是**复核手段**:确认清楚后即写入结论,不要为看图而反复放大。",
|
||||
"",
|
||||
]
|
||||
|
||||
|
||||
@@ -90,6 +90,10 @@ FRAME_ENTITY_LIMIT = int(os.environ.get("FRAME_ENTITY_LIMIT", "200"))
|
||||
RENDER_WAIT = float(os.environ.get("RENDER_WAIT", "0.6"))
|
||||
# 审查阶段受控工具子循环的最大回合数
|
||||
REVIEW_MAX_ITERS = int(os.environ.get("REVIEW_MAX_ITERS", "4"))
|
||||
# 审查子循环里回灌给 LLM 的截图数上限(控制视觉 token 成本)。
|
||||
# 实测:提示词鼓励「看不清→放大重看」后,模型一轮内会发多次 zoom+截图,
|
||||
# 上限过小会让它白拍(拍了却看不到)。6 可覆盖多数复核需求。
|
||||
REVIEW_OBS_IMAGES = int(os.environ.get("REVIEW_OBS_IMAGES", "6"))
|
||||
# 报告输出目录
|
||||
REPORT_DIR = _resolve_path(os.environ.get("REPORT_DIR", "reports"))
|
||||
# 批注回写使用的图层
|
||||
|
||||
@@ -7,18 +7,24 @@
|
||||
|
||||
结论是**结构化对象** `{"summary": str, "issues": [dict], "raw": str}`,
|
||||
不再依赖第二次 LLM 调用来抽取结构(见 `findings.parse_conclusion`)。
|
||||
|
||||
审查在**绘图区已最大化**的前提下进行(见 `evidence.maximized_drawing_area`),
|
||||
子循环里的放大截图因此是高清的;若 LLM 动过视口,结束时复位到该图框整幅视图。
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
from .. import evidence_model
|
||||
from .. import config, evidence_model
|
||||
from ..cad_tools import READ_ONLY_TOOLS, is_write_tool
|
||||
from ..findings import parse_conclusion
|
||||
from ..llm_client import LlmClient, downsample_image_b64
|
||||
from ..mcp_client import McpError, McpToolError, result_images, result_texts
|
||||
|
||||
# 单次审查回灌给 LLM 的图片数上限(控制视觉 token 成本)
|
||||
_MAX_OBS_IMAGES = 3
|
||||
_MAX_OBS_IMAGES = config.REVIEW_OBS_IMAGES
|
||||
|
||||
# 会改动视口的只读取证工具:用过之后须把视口复位,别把用户的 CAD 留在放大处
|
||||
_VIEW_TOOLS = frozenset({"zoom_to_window", "zoom_to_entity"})
|
||||
|
||||
|
||||
def evidence_payload(evidence):
|
||||
@@ -54,7 +60,12 @@ def _run_readonly_tool(ctx, name, args, image_budget):
|
||||
content.append({"type": "text", "text": text})
|
||||
for b64, mime in result_images(resp):
|
||||
if image_budget[0] <= 0:
|
||||
content.append({"type": "text", "text": "(后续截图已省略以控制成本)"})
|
||||
content.append(
|
||||
{
|
||||
"type": "text",
|
||||
"text": "(截图预算已用完,后续截图已省略;请基于已有信息给出结论,不要再请求截图。)",
|
||||
}
|
||||
)
|
||||
break
|
||||
image_budget[0] -= 1
|
||||
nb, nm = downsample_image_b64(b64, mime)
|
||||
@@ -64,8 +75,32 @@ def _run_readonly_tool(ctx, name, args, image_budget):
|
||||
return content, image_budget
|
||||
|
||||
|
||||
def _reset_viewport(ctx, evidence):
|
||||
"""把视口复位到该图框的整幅视图(审查中的放大不应残留给用户)。"""
|
||||
win = evidence.get("window")
|
||||
if not win:
|
||||
return
|
||||
try:
|
||||
ctx.tools.call("zoom_to_window", dict(win, margin=0.02))
|
||||
except (McpError, McpToolError):
|
||||
pass
|
||||
|
||||
|
||||
def review_frame(ctx, evidence, system_prompt, log=print):
|
||||
"""审查单个图框,返回结构化结论 `{"summary", "issues", "raw"}`。"""
|
||||
"""审查单个图框,返回结构化结论 `{"summary", "issues", "raw"}`。
|
||||
|
||||
审查中的放大取证会改动视口,故在 finally 里复位——**仅当确实动过视口**
|
||||
(LLM 只查结构化数据时不动用户视图)。
|
||||
"""
|
||||
view_changed = [False]
|
||||
try:
|
||||
return _review_frame(ctx, evidence, system_prompt, log, view_changed)
|
||||
finally:
|
||||
if view_changed[0]:
|
||||
_reset_viewport(ctx, evidence)
|
||||
|
||||
|
||||
def _review_frame(ctx, evidence, system_prompt, log, view_changed):
|
||||
payload = evidence_payload(evidence)
|
||||
user_content = [
|
||||
{
|
||||
@@ -105,6 +140,8 @@ def review_frame(ctx, evidence, system_prompt, log=print):
|
||||
except json.JSONDecodeError:
|
||||
args = {}
|
||||
log(f" [取证] {name} {args if args else ''}")
|
||||
if name in _VIEW_TOOLS:
|
||||
view_changed[0] = True
|
||||
content, image_budget = _run_readonly_tool(ctx, name, args, image_budget)
|
||||
messages.append(
|
||||
{"role": "tool", "tool_call_id": tool_call.id, "content": content}
|
||||
|
||||
Reference in new issue
Block a user