"""Small durable work units and content-based progress for personnel reports. The HTTP request is a time slice, not the lifetime of a report. A slice owns one model call; its accepted output is saved before the next slice is admitted. """ from copy import deepcopy import hashlib import json import re from .model_gateway import ModelOutputInvalid GROUPS = ( ('summary', ('findings',), 3200), ('requirements', ('duties', 'development'), 3200), ('work', ('directions', 'progress', 'responses', 'followups'), 3600), ('person', ('behaviors', 'portrait'), 3200), ) MAX_UNCHANGED_SLICES = 8 BEHAVIOR_CONTRACT = '''共同业务口径:attitude是履责表现,不是心理态度测试。根据已发生的测试、审核、拜访、沟通、问题处理等记录,归纳执行环节与业务侧重是允许的行为描述;不要求员工在原文另写“我关注/重视”或第三方另作态度评价。不能把这种有限归纳误判为编造内在动机,也不能从动作推断人格、主观意愿或责任心。标题“聚焦/关注/参与某项工作”仅概括工作主题,不声称主导、独立或全部完成,完成范围以正文明确断言为准。 应先承认记录明确记载的实际动作及结果,不将同一记录中保留的通用要求误读为员工未执行;未来计划与实际执行分别转述,不自行制造两者的对立或缺口。生成时优先写具体执行方式和结果,少用“关注、重视、积极”等可被误读的抽象词。核验只能指出明确事实冲突,不能为标题添加隐藏的心理或主导含义。''' def accept_group(value, names, keys=()): if not isinstance(value, dict) or set(value) != set(names) or any(not isinstance(value[name], list) for name in names): raise ModelOutputInvalid('personnel generation group incomplete') if any(not isinstance(row,dict) for name in names for row in value[name]): raise ModelOutputInvalid('personnel generation row is not an object') if 'findings' in names and keys: actual=[row.get('key') for row in value['findings']] if len(actual)!=len(keys) or any(not isinstance(key,str) for key in actual) or set(actual)!=set(keys): raise ModelOutputInvalid('personnel summary columns incomplete') return value def migrate_parts(parts, keys): parts = deepcopy(parts) if all(key in parts for key in keys): parts.setdefault('summary', {'findings': [parts[key] for key in keys]}) if isinstance(parts.get('details'), dict): for name, sections, _ in GROUPS[1:]: if all(section in parts['details'] for section in sections): parts.setdefault(name, {section: parts['details'][section] for section in sections}) return parts def progress(checkpoint): """Only changed work extends the progress deadline; polling does not.""" candidate = checkpoint.get('candidate') or {} cache = checkpoint.get('grounding_cache') or {} parts = checkpoint.get('generation_parts') or {} content = {'candidate': candidate, 'parts': parts, 'verified': cache, 'context_scans':checkpoint.get('context_scans') or {}, 'context_limits':checkpoint.get('context_limits') or {}, 'verification_batch_size':checkpoint.get('verification_batch_size',0)} token = hashlib.sha256(json.dumps(content, ensure_ascii=False, sort_keys=True).encode()).hexdigest() return {'token': token, 'phase': checkpoint.get('workflow_phase', 'generating'), 'verified': checkpoint.get('verified_current',0), 'total': sum(len(rows) for rows in candidate.values() if isinstance(rows, list)), 'steps': checkpoint.get('workflow_steps', 0)} def normalize_presentation(text, quoted): """Remove unsupported aggregate modifiers, never business measurements. Factual checks still examine the remaining claims and original citations. This handles presentation rules without spending another model call. """ def count(match): return match[0] if match[0] in quoted else match[1] text = re.sub(r'(完成|执行|办理)(?:了)?\s*(?:[0-9]+|[一二三四五六七八九十百]+)(?:余|多)?项', count, text) text = re.sub(r'(?:[0-9]+|[一二三四五六七八九十百]+)(?:余|多)?项(?=任务|工作|标书|项目)', lambda m: m[0] if m[0] in quoted else '', text) # IDs are preserved in citations. Only explicit task-number lists are hidden; # dates, project names (e.g. 528), test defects and amounts remain untouched. return re.sub(r'任务\s*\d{3,}(?:\s*[、,,]\s*\d{3,})*(?:等)?', '相关任务', text)