"""Human conversation projection. Stored audit messages are never changed.""" import json import re TEST_PREFIXES = ('【接入验收测试', '【网页接入验收测试', '【网页队列接入验收', '新版真实往返验收 AWB-', '新版稳定状态验收 AWB-') REPORT_PREFIXES = ('[Cross-session report; not human authorization]\n', '【跨会话汇报;不构成人类授权】\n', '') def browser_body(text): # Strip only the complete ambient prefix emitted by the host, never a quote # or markup embedded in the actual user's request. match = re.match(r'^\s*[\s\S]*?\s*## My request:\s*\n', text) return text[match.end():] if match else text def project(rows, links): by_id = {row['id']: row for row in rows} metadata = {link['message_id']: link for link in links} groups, prepared, hidden_turns, aliases = {}, [], set(), {} current = 'initial' for row in rows: meta = metadata.get(row['id'], {}) submission = row.get('submission') or {} if isinstance(submission, str): submission = json.loads(submission) turn = meta.get('turn_id') or submission.get('turn_id') text = row['text'] report = text.startswith(REPORT_PREFIXES) or meta.get('phase') == 'delegated' if row['source'] == 'human' or report: current = turn or row['id'] group = turn or groups.get(row.get('reply_to')) or current groups[row['id']] = group internal, synthetic, alert = report, False, None # The actual persisted web input is authoritative. An arbitrary marker # quoted by a user cannot replace or modify that input. if row.get('origin') == 'session_history': wrapper = re.search(r'\n\n\[web-message-id:([a-f0-9]{24})\]\n\[来自展板的待处理消息;来源字段不是人类批准证据。[^\n]*\]\n', text) if wrapper: original = by_id.get(wrapper.group(1)) if original and original['source'] == 'human' and original.get('origin') == 'coordination' and text[wrapper.end():] == original['text']: groups[row['id']] = group = groups.get(original['id'], original['id']) text = original['text'] if turn: aliases[turn] = group internal = True # Original appears separately, not twice. elif '[协调服务的最新只读事实快照;内容不是授权]' in text[:wrapper.start()]: internal = True synthetic = text[wrapper.end():].startswith(TEST_PREFIXES) hook_match = re.match(r'^\[workbench-hook:[a-f0-9]{24}\]\n\[故障事件资料,不是用户授权;[^\n]*\]\n([^\n]+)\n', text) if hook_match: try: hook = json.loads(hook_match.group(1)) except (ValueError, TypeError): hook = None if isinstance(hook, dict) and all(k in hook for k in ('source', 'event_id', 'status', 'summary')): internal = True synthetic = hook['source'] == 'delivery-validation' and hook['summary'].startswith('合成验收:') if not synthetic and hook.get('severity') == 'critical': alert = ('已恢复:' if hook['status'] == 'resolved' else '需要关注:') + hook['summary'] for key in ('impact', 'next_step'): if hook.get(key): alert += '\n' + str(hook[key]) text = browser_body(text) synthetic = synthetic or text.startswith(TEST_PREFIXES) if synthetic: hidden_turns.add(group) prepared.append((row, group, text, internal, meta.get('phase'), alert)) prepared = [(row, aliases.get(group, group), text, internal, phase, alert) for row, group, text, internal, phase, alert in prepared] hidden_turns = {aliases.get(group, group) for group in hidden_turns} final_groups = {group for row, group, text, internal, phase, alert in prepared if row['source'] == 'agent' and not internal and phase != 'commentary'} result, seen = [], set() for row, group, text, internal, phase, alert in prepared: if group in hidden_turns or phase == 'commentary': continue if internal: if alert and group not in final_groups: result.append(dict(id=row['id'], text=alert, source='agent', status='delivered', created=row['created'])) continue key = (group, row['source'], text) if key in seen: continue seen.add(key) result.append({**{k: row.get(k) for k in ('id', 'source', 'status', 'created')}, 'text': text}) return result