"""Read-only workflow downloads, including full history and authorized artifacts.""" import csv from datetime import datetime, timezone import hashlib import io import json from pathlib import Path import re import tempfile import zipfile from fastapi import HTTPException MAX_EXPORT_BYTES = 256 * 1024 * 1024 LABELS = {'assessing': '整理需求', 'needs_input': '等待补充需求', 'awaiting_selection': '等待审核方案', 'designing': '正在设计与检查', 'exporting': '正在打包成果', 'needs_operator': '服务器自动恢复中', 'draft_ready': '草稿:检查未通过', 'delivery_ready': '检查通过,等待审核', 'completed': '已完成,可下载', 'failed': '本次设计未能完成', 'cancelled': '已取消'} REPORT = re.compile(r'^(?:erc|drc|drc-reopened|mechanical|final-audit|circuit-failure|repair-baseline|semantic|metrics|failure|schema-failure|attempt-\d+-failure|repair-\d+-(?:context|decision)|part-mechanical-preflight|footprint-repair-\d+-\d+|recovery-\d+|footprint-review-\d+|selection-bindings|selection-native-\d+|selection-failure-\d+|native-checks|intake-check|plan-check|plan-review|pcb-(?:dsn-export|layout|placement-check|pad-resolution|routing-completion|connectivity-refresh|mechanical-geometry|mechanical-preflight|mechanical-check|mounting-holes|pipeline|routing|net-mapping|drill-preservation|copper-audit|signal-angle-audit)|schematic-(?:net-plan|pin-plan|logical-netlist|wiring|wiring-check|layout|native-pin-aliases|sheet-history))\.json$') def json_bytes(value): return (json.dumps(value, ensure_ascii=False, indent=2) + '\n').encode('utf-8') def clean(value, root): """Reports need design evidence, never provider secrets or host filesystem paths.""" if isinstance(value, dict): return {k: clean(v, root) for k, v in value.items() if k.lower() not in {'api_key', 'authorization', 'experimental_bearer_token', 'access_token', 'absolute_path', 'native_job_id', 'render_override'}} if isinstance(value, list): return [clean(v, root) for v in value] if isinstance(value, str): if value.startswith(('/home/', '/Users/', '/tmp/')): return '[服务器本地文件]/' + Path(value).name return value.replace(str(root), '[任务运行目录]') return value def timestamp(value): try: return datetime.fromtimestamp(float(value), timezone.utc).isoformat() except (ValueError, TypeError, OverflowError): return '' def collect(settings, store, engine, tid, *, include_checks=True): data = store.task(tid) if not data: raise HTTPException(404, '任务不存在') directory = (settings.core_root / 'tasks' / tid).resolve() if not directory.is_relative_to((settings.core_root / 'tasks').resolve()): raise HTTPException(404, '任务路径不可用') core, notices = {}, [] path = directory / 'task.json' if path.is_file() and not path.is_symlink(): try: core = json.loads(path.read_text()) except (OSError, ValueError): notices.append('原始任务快照暂不可读;已包含网页任务快照。') with store.db() as db: # One query captures all rows through an export watermark, without the UI/SSE limit. events = [{**dict(row), 'data': json.loads(row['data'])} for row in db.execute( 'SELECT seq,kind,data,created FROM events WHERE task_id=? ORDER BY seq', (tid,))] reports, total = [], 0 folders = [directory / 'calls', directory / 'project/work', directory / 'project/output'] if include_checks else [] for folder in folders: if not folder.is_dir(): continue for file in sorted(folder.rglob('*.json')): if not REPORT.fullmatch(file.name) or file.is_symlink() or not file.resolve().is_relative_to(directory): continue try: if file.stat().st_size > MAX_EXPORT_BYTES - total: raise HTTPException(413, '检查记录超过 256MiB,请分项下载工程文件和执行记录。') raw = file.read_bytes() total += len(raw) if total > MAX_EXPORT_BYTES: raise HTTPException(413, '检查记录超过 256MiB,请分项下载工程文件和执行记录。') reports.append({'name': str(file.relative_to(directory)), 'data': clean(json.loads(raw), settings.core_root)}) except (OSError, ValueError): notices.append('导出时暂不可读的记录:' + str(file.relative_to(directory))) artifacts = engine.artifacts(tid) report = {'exported_at': datetime.now(timezone.utc).isoformat(), 'event_count': len(events), 'last_event_seq': events[-1]['seq'] if events else 0, 'snapshot_note': '这是导出时已有资料的快照;任务后续产生的信息需重新下载。未审核放行的制造包不会包含在内。', 'task': engine.public_task(data), 'conversation': core.get('conversation', []), 'attachments': [{'name':item.get('name') or item.get('description') or Path(item.get('path','')).name, 'kind':item.get('kind'), 'sha256':item.get('sha256')} for item in core.get('assets', [])], 'plan': core.get('plan') or core.get('accepted_plan') or core.get('proposal'), 'parts': [{k: item.get(k) for k in ['reference', 'lib_id', 'value', 'footprint', 'role', 'group', 'metadata']} for item in core.get('parts', [])], 'events': events, 'checks': reports, 'artifacts': artifacts, 'notices': notices} for key in ['created_at', 'updated_at', 'design_started_at']: if key in core: report[key] = timestamp(core[key]) return clean(report, settings.core_root), core, directory def csv_bytes(report): stream = io.StringIO(newline='') writer = csv.writer(stream) writer.writerow(['序号', '时间(UTC)', '事件类型', '说明', '详细数据']) def safe(value): text = str(value) return "'" + text if text.lstrip().startswith(('=', '+', '-', '@', '\t', '\r')) else text for event in report['events']: writer.writerow([event['seq'], timestamp(event['created']), safe(event['kind']), safe(event['data'].get('message', '')), safe(json.dumps(event['data'], ensure_ascii=False))]) return stream.getvalue().encode('utf-8-sig') def markdown(report): task, plan = report['task'], report.get('plan') or {} lines = ['# PCB 生成流程资料', '', task.get('title') or task['task_id'], '', f"- 任务:{task['task_id']}", f"- 修订:{task.get('revision', '')}", f"- 状态:{LABELS.get(task['state'], task['state'])}", f"- 导出时间:{report['exported_at']}", f"- 完整执行记录:{report['event_count']} 条", '', report['snapshot_note'], '', '## 用户需求与补充', ''] for index, entry in enumerate(report['conversation'], 1): label = '方案评论' if entry.get('kind') == 'proposal_feedback' else entry.get('role', '') lines += [f"### {index}. {label}", '', entry.get('text', ''), ''] lines += ['## 当前问题', *['- ' + q for q in task.get('questions', [])], '', '## 方案与选择', ''] if plan.get('summary'): lines += [plan['summary'], ''] for option in (task.get('proposal') or {}).get('options', []): lines += [f"### 方案 {option['id']}:{option['summary']}", option['implementation'], '', *['- 优点:' + item for item in option.get('advantages', [])], *['- 限制:' + item for item in option.get('disadvantages', [])], ''] lines += ['已选方案:' + str(task.get('selected_option') or '尚未选择'), '', '## 要求与默认假设', ''] for key in ['requirements', 'interfaces', 'assumptions']: lines += ['- ' + item for item in plan.get(key, [])] if plan.get('board'): lines += ['', '板框:' + json.dumps(plan['board'], ensure_ascii=False)] lines += ['', '## 器件清单', '', '| 器件 | 型号/数值 | 符号 | 封装 | 功能分组 |', '|---|---|---|---|---|'] def cell(value): return str(value or '').replace('|', '\\|').replace('\n', ' ') for part in report['parts']: lines.append('| ' + ' | '.join(cell(part.get(k)) for k in ['reference', 'value', 'lib_id', 'footprint', 'group']) + ' |') lines += ['', '## 诊断与检查结论', '', task.get('error') or '当前未记录任务错误。'] for key in ['manufacturing_blockers', 'findings']: lines += ['- ' + str(item) for item in task.get(key, [])] lines += ['', '## 成果文件清单', ''] for item in report['artifacts']: lines += [f"- {item['kind']}:{item['name']};SHA-256:{item['sha256']}"] lines += ['', '## 完整执行记录', ''] for event in report['events']: lines += [f"### {event['seq']} · {timestamp(event['created'])} · {event['kind']}", '', event['data'].get('message', ''), '', '```json', json.dumps(event['data'], ensure_ascii=False, indent=2), '```', ''] lines += ['## 详细检查记录', ''] for check in report['checks']: lines += ['### ' + check['name'], '', '```json', json.dumps(check['data'], ensure_ascii=False, indent=2), '```', ''] lines += ['## 导出说明', '', *['- ' + notice for notice in report['notices']]] return '\n'.join(lines).encode('utf-8-sig') def bundle(report, core, directory, authorized_file): """Package only existing downloadable artifacts; never relax artifact release gates.""" output = tempfile.SpooledTemporaryFile(max_size=8 * 1024 * 1024, mode='w+b') written = 0 try: with zipfile.ZipFile(output, 'w', compression=zipfile.ZIP_DEFLATED) as archive: def write(name, content): nonlocal written written += len(content) if written > MAX_EXPORT_BYTES: raise HTTPException(413, '全部资料超过 256MiB,请下载流程说明、完整记录,并分项下载工程文件。') archive.writestr(name, content) for index, asset in enumerate(core.get('assets', []), 1): path = Path(asset.get('path', '')).resolve() if not path.is_relative_to(directory / 'project/inputs') or not path.is_file() or path.is_symlink(): report['notices'].append('参考资料暂不可用:' + str(asset.get('name', index))) continue content = path.read_bytes() if hashlib.sha256(content).hexdigest() != asset.get('sha256'): report['notices'].append('参考资料摘要不一致,未打包:' + str(asset.get('name', index))) continue name = re.sub(r'[^\w.() -]', '_', Path(asset.get('name') or path.name).name) write(f'参考资料/{index:02d}-{name}', content) for item in report['artifacts']: try: _, path = authorized_file(report['task']['task_id'], item['artifact_id']) except HTTPException as exc: report['notices'].append(f"成果未包含:{item['name']}(HTTP {exc.status_code}:{exc.detail})") continue if path.stat().st_size > MAX_EXPORT_BYTES - written: raise HTTPException(413, '全部资料超过 256MiB,请分项下载流程资料和工程文件。') content = path.read_bytes() if hashlib.sha256(content).hexdigest() != item['sha256']: report['notices'].append('导出时成果发生变化,未打包:' + item['name']) continue write('工程成果/' + item['artifact_id'] + '/' + path.name, content) write('流程资料.json', json_bytes(report)) write('完整执行记录.csv', csv_bytes(report)) write('流程说明.md', markdown(report)) for check in report['checks']: write('检查记录/' + check['name'], json_bytes(check['data'])) output.seek(0) return output except BaseException: output.close() raise