Datasets:
Download code/evaluation-code/v0.2-0c2690ae/apps/console/server/registry.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 5.47 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/server/registry.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/apps/console/server/registry.py
-
curl -L -o registry.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/server/registry.py
5.47 kB
| """Startup-only registry; summaries read bounded JSON, never NPZ arrays.""" | |
| from collections import Counter | |
| from dataclasses import dataclass | |
| import json | |
| from pathlib import Path | |
| import re | |
| from .errors import require | |
| from .segment_import import MANIFEST, IMPORT_SCHEMA | |
| MAX_METADATA_BYTES = 16 * 1024 * 1024 | |
| def small_json(path): | |
| if path.stat().st_size > MAX_METADATA_BYTES: | |
| raise ValueError('登记元数据过大:' + str(path)) | |
| return json.loads(path.read_text(encoding='utf-8')) | |
| class SegmentEntry: | |
| id: str | |
| directory: Path | |
| summary: dict | |
| class Registry: | |
| def __init__(self, roots, batch_indexes=None, root_notes=None): | |
| self.entries, self.roots = {}, [] | |
| self.batch_failures, self.index_sources, self.root_notes = {}, {}, dict(root_notes or {}) | |
| aliases = set() | |
| for alias, source in roots: | |
| if not re.fullmatch(r'[A-Za-z0-9_-]+', alias) or alias in aliases: | |
| raise ValueError('根别名必须唯一,且只含字母、数字、下划线或短横线') | |
| aliases.add(alias) | |
| root = Path(source).resolve(strict=True) | |
| if not root.is_dir(): | |
| raise ValueError('根必须是目录') | |
| count = 0 | |
| for manifest in sorted(root.rglob('*-import.json')): | |
| directory=manifest.parent.resolve() | |
| if not directory.is_relative_to(root): | |
| continue | |
| require(manifest.name==MANIFEST,'REVIEW_IMPORT_SCHEMA','非现行导入清单,须先离线转换') | |
| data=small_json(manifest) | |
| require(data.get('schema')==IMPORT_SCHEMA,'REVIEW_IMPORT_SCHEMA','未知导入版本') | |
| segment_id=alias+'/'+str(directory.relative_to(root)) | |
| if count<3: | |
| from .segment_import import read_import | |
| read_import(directory,segment_id) | |
| self.entries[segment_id]=SegmentEntry(segment_id,directory,{**data['summary'],'id':segment_id,'root':alias}) | |
| count+=1 | |
| self.roots.append({'alias': alias, 'segment_count': count}) | |
| if not set(self.root_notes) <= aliases: | |
| raise ValueError('批注根别名未登记') | |
| if any(type(v) is not str or not v.strip() or len(v) > 8000 for v in self.root_notes.values()): | |
| raise ValueError('批次提醒必须为非空文字,至多8000字符') | |
| for alias, path, expected in batch_indexes or []: | |
| if alias not in aliases or alias in self.index_sources: | |
| raise ValueError('批索引根别名未登记或重复') | |
| import hashlib | |
| path = Path(path).resolve(strict=True) | |
| if path.stat().st_size > 64 * 1024 * 1024: | |
| raise ValueError('批索引超过64MiB预算') | |
| raw = path.read_bytes() | |
| if not re.fullmatch('[0-9a-f]{64}', expected) or hashlib.sha256(raw).hexdigest() != expected: | |
| raise ValueError('批索引指纹不符') | |
| index = json.loads(raw) | |
| cases = {r['id']: r for r in index['cases']} | |
| rows = {r['case_id']: r for r in index['rows']} | |
| if len(cases) != index['declared_count'] or len(rows) != len(cases) or set(rows) != set(cases): | |
| raise ValueError('批索引分母/段号重复或缺失') | |
| present = {id.split('/', 1)[1] for id in self.entries if id.startswith(alias + '/')} | |
| failures = [] | |
| for id, row in rows.items(): | |
| if row['status'] == 'generated': | |
| if id not in present: | |
| raise ValueError('索引称生成但剧本未登记:' + id) | |
| continue | |
| if id in present or row['status'] not in ('timeout', 'generation_or_verification_failed'): | |
| raise ValueError('索引失败状态与已登记剧本不一致:' + id) | |
| meta = cases[id] | |
| failures.append({'id': alias + '/' + id, 'world_id': meta['world'], 'duration_tier': meta['tier'], | |
| 'activity_seed': meta['seed'], 'summary_status': 'not_generated', 'selfcheck': 'not_checked', | |
| 'process_status': row['status'], 'process_exit_code': row['exit_code'], | |
| 'process_s': row.get('process_s'), 'reason': row.get('reason'), 'wall_limit_s': row.get('wall_limit_s'), | |
| 'actors': [], 'visibility': None, 'visibility_status': 'not_applicable', 'error': None, | |
| 'observer_generation': {'requested_observers': len(meta['config']['observers']), 'generated_observers': 0, 'omitted': []}}) | |
| if len(present) + len(failures) != len(cases): | |
| raise ValueError('登记段与失败目录未覆盖批索引') | |
| self.batch_failures[alias] = failures | |
| self.index_sources[alias] = {'path': str(path), 'sha256': expected, 'declared_count': index['declared_count']} | |
| def summaries(self): | |
| return [entry.summary for entry in self.entries.values()] | |
| def register_root(self, alias, root): | |
| """Scan a new immutable review-import root before publishing it.""" | |
| if alias in {r['alias'] for r in self.roots}: | |
| raise ValueError('生成批别名重复') | |
| fresh = Registry([(alias, root)]) | |
| self.entries = {**self.entries, **fresh.entries} | |
| self.roots = self.roots + fresh.roots | |