This commit is contained in:
zyp
2026-11-25 20:46:42 +08:00
parent 066a5023b7
commit 1ae94dfb1f
17 changed files with 633 additions and 55 deletions
+49 -24
View File
@@ -1,9 +1,11 @@
"""Task-specific explanations, grounded in the authorized review snapshot."""
import logging
import time
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, model_validator
from .confidential_delivery import CONFIDENTIAL_DELIVERY_POLICY
from .review_failure import review_failure
logger = logging.getLogger(__name__)
@@ -145,6 +147,7 @@ def citation_schema(sources):
def review_commentary(*, subject, goal, tasks, sources, review_kind, workflow,
alignment, findings, schedule, requirements, limitations,
invoke, timeout_seconds, child_issues=None):
deadline = time.monotonic() + timeout_seconds
try:
if timeout_seconds < 1:
raise TimeoutError()
@@ -182,30 +185,52 @@ def review_commentary(*, subject, goal, tasks, sources, review_kind, workflow,
if alignment and alignment['status'] == 'unrelated':
schema['properties']['verdict']['enum'] = ['adjust']
instruction += '\n本次已根据原文核实:当前任务不属于所挂上级目标。即使任务自身描述清楚,也必须说明这项归属冲突并建议核对挂靠,不能给sound;不要把技术任务改写成另一种业务任务。'
result = Commentary.model_validate(invoke(instruction, data, schema,
timeout_seconds=min(18, timeout_seconds), max_tokens=3000))
available = {s['id']: s['text'] for s in data['sources']}
description = result.description_check
description_details = [description.work_scope, description.expected_result, description.completion_condition]
for citation in [*result.evidence, *(c for a in result.actions for c in a.evidence),
*(d.evidence for d in description_details if d)]:
source = available.get(citation.source_id)
if source is None or not citation.quote.strip() or citation.quote not in source:
raise ValueError("评价引用了未读取的依据")
if not any(c.source_id == f"task:{subject.id}" for c in result.evidence):
raise ValueError("缺少当前工作原文依据")
subject_actions = [a for a in result.actions if a.scope == "subject"]
if description.status == 'needs_detail' and (result.verdict == 'sound' or not subject_actions):
raise ValueError('任务要求不充分不能显示暂无调整建议')
if review_kind == 'task' and any(a.scope != 'subject' for a in result.actions):
raise ValueError("当前任务评价不得混入整体分工建议")
if result.verdict != "sound" and not (subject_actions if review_kind == 'task' else result.actions):
raise ValueError("当前工作评价不能由其他任务的缺口替代")
if result.verdict == "sound" and subject_actions:
raise ValueError("当前工作判断与修改意见不一致")
if alignment and alignment["status"] == "unrelated" and result.verdict != "adjust":
raise ValueError("评价与已核实的任务归属不一致")
def evaluate():
remaining = deadline - time.monotonic()
if remaining < 1:
raise TimeoutError()
raw = invoke(instruction, data, schema, timeout_seconds=min(30, remaining), max_tokens=3000)
return validate_commentary(raw, subject.id, available, review_kind, alignment)
try:
result = evaluate()
except ValueError:
if deadline - time.monotonic() < 8:
raise
instruction += (
'\n上一轮未通过核对。按结构完整重写,引用直接使用对应来源的候选原文,'
'至少一条引用来自当前任务,结论、描述充分性和整改意见必须一致。'
)
result = evaluate()
return dict(status="complete", **result.model_dump())
except Exception as exc:
logger.warning("review_commentary_incomplete error_type=%s", type(exc).__name__)
return dict(status="incomplete", message="本次具体评价未完成,请重新分析。已核实的依据仍可查看。")
failure = review_failure(exc)
logger.warning("review_commentary_incomplete reason=%s error_type=%s", failure["code"], type(exc).__name__)
return dict(status="incomplete", message=failure["message"], analysis_error=failure)
def validate_commentary(raw, subject_id, available, review_kind, alignment):
result = Commentary.model_validate(raw)
description = result.description_check
description_details = [description.work_scope, description.expected_result, description.completion_condition]
for citation in [*result.evidence, *(c for a in result.actions for c in a.evidence),
*(d.evidence for d in description_details if d)]:
source = available.get(citation.source_id)
if source is None or not citation.quote.strip() or citation.quote not in source:
raise ValueError("评价引用了未读取的依据")
if not any(c.source_id == f"task:{subject_id}" for c in result.evidence):
raise ValueError("缺少当前工作原文依据")
subject_actions = [a for a in result.actions if a.scope == "subject"]
if description.status == 'needs_detail' and (result.verdict == 'sound' or not subject_actions):
raise ValueError('任务要求不充分不能显示暂无调整建议')
if review_kind == 'task' and any(a.scope != 'subject' for a in result.actions):
raise ValueError("当前任务评价不得混入整体分工建议")
if result.verdict != "sound" and not (subject_actions if review_kind == 'task' else result.actions):
raise ValueError("当前工作评价不能由其他任务的缺口替代")
if result.verdict == "sound" and subject_actions:
raise ValueError("当前工作判断与修改意见不一致")
if alignment and alignment["status"] == "unrelated" and result.verdict != "adjust":
raise ValueError("评价与已核实的任务归属不一致")
return result