"""R1–R8: mechanically keep source facts out of the transferable production reference. `check` validates the agent's single file (reference_breakdown.json) against the cached measurements and the optional understanding artifacts, computes `derived` in memory, and returns {derived, errors, warnings}. `export` writes production_reference.json only when there are zero errors, then re-scans the serialized result (R8). """ import json import math import re from pathlib import Path from lib import read_json, write_json from reference_measure import EDGE_S, MEASUREMENTS_FILE from reference_profile import ( CUT_MATCH_S, OWNERS, apply_cut_fixes, build_production, derive, is_resolved, resolve) BREAKDOWN_FILE = "reference_breakdown.json" BREAKDOWN_SCHEMA = "video-reference.breakdown.v1" DIMENSIONS = ("narrative_structure", "pacing", "shots_editing", "narration_subtitles", "audio_visual") APPLIES_TO = ("story_plan", "visual_audio_board", "clip_plan", "narration", "style_card") FUNCTIONS = ("hook", "setup", "turn", "escalation", "payoff") NARRATION_JOBS = ("context", "causal_link", "foreshadow", "interpretation", "transition") TARGET_ROOTS = ("shots", "loudness", "derived") # Objects a target may export whole; every other target must be a numeric leaf. No list indexing, # and first_original_at is exported as {fraction} only (its absolute `s` stays local). _TARGET_OBJECTS = re.compile( r"derived\.(?:structure|first_original_at|narration_jobs|by_owner\.[a-z_]+|by_section\.[a-z]+)") GAP_S = 0.5 T_EPSILON_S = 0.05 CJK_RUN_MIN = 8 LATIN_RUN_MIN = 5 _KEYS = { "top": {"schema", "labels", "source_facts", "methods", "skipped_dimensions"}, "labels": {"audio_spans", "sections", "subtitles", "basis", "cut_fixes"}, "cut_fixes": {"add", "remove"}, "span": {"start", "end", "owner", "narration_job"}, "section": {"start", "end", "function"}, "subtitles": {"burned", "max_lines", "marks_original", "evidence_t"}, "fact": {"id", "dimension", "statement", "t", "measure", "entities"}, "method": {"id", "dimension", "rule", "applies_when", "avoid_when", "applies_to", "evidence", "targets"}, "target": {"from"}, } _REQUIRED = { "top": {"schema", "labels"}, "labels": {"audio_spans", "sections"}, "span": {"start", "end", "owner"}, "section": {"start", "end", "function"}, "subtitles": {"burned", "max_lines"}, "fact": {"id", "dimension", "statement", "entities"}, "method": {"id", "dimension", "rule", "applies_to", "evidence"}, "target": {"from"}, } METHOD_TEXT_FIELDS = ("rule", "applies_when", "avoid_when") BANNED_EXPORT_KEYS = {"source_facts", "labels", "entities", "evidence", "statement", "from", "path"} _EXPORT_SCHEMA_KEYS = { "schema", "duration_s", "canvas", "width", "height", "cut_detection", "detector", "hard_score", "soft_score", "isolation_ratio", "isolation_window_s", "scaled", "agent_added", "agent_removed", "profile", "shot_median_s", "cuts_per_min", "integrated_lufs", "mean_short_term_lufs", "narration_share", "narration_chars_per_s", "switch_on_cut_share", "first_original_at", "value", "provenance", "precision", "fraction", "seconds", "share", "blocks", "block_median_s", "structure", "function", "at", "lead_owner", "subtitles", "burned", "max_lines", "marks_original", "methods", "id", "dimension", "rule", "applies_when", "avoid_when", "applies_to", "targets", "skipped_dimensions", *DIMENSIONS, *OWNERS, *FUNCTIONS, *NARRATION_JOBS, } _GENERIC_QUOTED_TERMS = { "反转", "悬念", "钩子", "高潮", "留白", "伏笔", "铺垫", "冲突", "平行剪辑", "双线叙事", "冷开场", "正反打", "蒙太奇", "旁白", "原声", "画外音", "同期声", "转场", "节奏", } _CJK = re.compile(r"[㐀-鿿豈-﫿]+") _WORD = re.compile(r"[A-Za-z0-9']+") # Python's \b treats CJK as word characters, so "在1:23处" has no boundary before "1"; use # digit/ASCII lookarounds instead. Path segments are ASCII so prose like "旁白/原声/音乐" stays legal. # Common aspect ratios (9:16, 16:10, 18:10, 21:10) and explicit ratio labels are excluded. _NUMERAL = "[0-9一二三四五六七八九十百零两]+" _TIMECODE = re.compile( rf"(?= item["end"]: errors.append(f"R2 labels.{name}[{i}]: start 必须小于 end") if item["end"] > duration + GAP_S: errors.append(f"R2 labels.{name}[{i}]: end 超出时长 {duration}") if i: prev = items[i - 1] if item["start"] < prev["end"] - 1e-6: errors.append(f"R2 labels.{name}[{i}]: 与上一段重叠或未按时间排序") elif item["start"] - prev["end"] > GAP_S: errors.append(f"R2 labels.{name}[{i}]: 与上一段间隙 {item['start'] - prev['end']:.2f}s > {GAP_S}s") if items[0]["start"] > GAP_S or items[-1]["end"] < duration - GAP_S: errors.append(f"R2 labels.{name}: 必须覆盖 [0, {duration}](首尾容差 {GAP_S}s)") def _check_cut_fixes(fixes, cuts, duration, errors): """R1/R2: removals name a detected cut, additions a cut the detector did not report.""" if not _closed(fixes, "cut_fixes", "labels.cut_fixes", errors): return for key in ("add", "remove"): values = fixes.get(key, []) if not isinstance(values, list) or not all(_num(v) for v in values): errors.append(f"R1 labels.cut_fixes.{key}: 必须是秒数列表") continue for t in values: near = any(abs(t - c) <= CUT_MATCH_S for c in cuts) if not EDGE_S < t < duration - EDGE_S: errors.append( f"R2 labels.cut_fixes.{key}: {t} 不在 ({EDGE_S}, {round(duration - EDGE_S, 4)}) 内" ) elif key == "remove" and not near: errors.append(f"R2 labels.cut_fixes.remove: {t} 附近 ±{CUT_MATCH_S}s 内没有测得的切点") elif key == "add" and near: errors.append(f"R2 labels.cut_fixes.add: {t} 已是测得的切点(±{CUT_MATCH_S}s)") def _check_labels(labels, duration, errors, cuts=()): if not _closed(labels, "labels", "labels", errors): return [], [] spans = _timed(labels.get("audio_spans"), "span", "owner", OWNERS, errors) sections = _timed(labels.get("sections"), "section", "function", FUNCTIONS, errors) _check_cover(spans, "audio_spans", duration, errors) _check_cover(sections, "sections", duration, errors) if "cut_fixes" in labels: _check_cut_fixes(labels["cut_fixes"], cuts, duration, errors) subtitles = labels.get("subtitles") if subtitles is not None and _closed(subtitles, "subtitles", "labels.subtitles", errors): where = "R1 labels.subtitles" if "burned" in subtitles and not isinstance(subtitles["burned"], bool): errors.append(f"{where}.burned: 必须是 true/false") lines = subtitles.get("max_lines") if "max_lines" in subtitles and (not isinstance(lines, int) or isinstance(lines, bool) or lines < 1): errors.append(f"{where}.max_lines: 必须是 ≥1 的整数") if "marks_original" in subtitles and not isinstance(subtitles["marks_original"], str): errors.append(f"{where}.marks_original: 必须是字符串") evidence_t = subtitles.get("evidence_t", []) if not isinstance(evidence_t, list): errors.append(f"{where}.evidence_t: 必须是数字列表") evidence_t = [] for t in evidence_t: if not _num(t) or not 0 <= t <= duration: errors.append(f"R2 labels.subtitles.evidence_t: {t!r} 不在 [0, {duration}] 内") return spans, sections def _check_ids(items, prefix, where, errors): seen = set() for i, item in enumerate(items): ident = item.get("id") if not isinstance(ident, str) or not re.fullmatch(rf"{prefix}\d+", ident): errors.append(f"R1 {where}[{i}]: id 必须形如 {prefix}1") elif ident in seen: errors.append(f"R1 {where}[{i}]: id {ident} 重复") seen.add(ident) return seen def _anchor_ok(path, measurements, derived): """A `measure` anchor: resolves under shots/loudness/derived and is not a string leaf.""" if not isinstance(path, str) or path.split(".")[0] not in TARGET_ROOTS: return False value = resolve(path, measurements, derived) return is_resolved(value) and not isinstance(value, str) def _check_facts(facts, measurements, derived, duration, errors): """R1 + R3: every fact is anchored to a time range or a resolvable measurement.""" for fact in facts: where = f"source_facts.{fact.get('id')}" if fact.get("dimension") not in DIMENSIONS: errors.append(f"R1 {where}: dimension 必须是 {'|'.join(DIMENSIONS)}") if not isinstance(fact.get("statement"), str) or not fact.get("statement").strip(): errors.append(f"R1 {where}: statement 不能为空") if not isinstance(fact.get("entities"), list) or not all(isinstance(e, str) for e in fact.get("entities", [])): errors.append(f"R3 {where}: 必须显式写 entities 列表(可为空)") if ("t" in fact) == ("measure" in fact): errors.append(f"R3 {where}: t 与 measure 必须二选一") elif "t" in fact: t = fact["t"] if not (isinstance(t, list) and len(t) == 2 and all(_num(x) for x in t) and 0 <= t[0] < t[1] <= duration + T_EPSILON_S): errors.append(f"R3 {where}: t 必须是 [a, b] 且 0 ≤ a < b ≤ {duration}") else: paths = fact["measure"] if not isinstance(paths, list) or not paths: errors.append(f"R3 {where}: measure 必须是非空路径列表") for path in paths if isinstance(paths, list) else []: if not _anchor_ok(path, measurements, derived): errors.append(f"R3 {where}: 测量路径 {path!r} 无法解析到 shots/loudness/derived 下的测量值") def _target_ok(path, value): if not isinstance(path, str) or not is_resolved(value) or value is None: return False parts = path.split(".") if parts[0] not in TARGET_ROOTS or any(p.isdigit() for p in parts) or path == "derived.first_original_at.s": return False return _num(value) or _TARGET_OBJECTS.fullmatch(path) is not None def _check_methods(methods, fact_ids, measurements, derived, errors): """R1 + R4 + R5: closed method shape, real evidence, targets only name measurement paths.""" for method in methods: where = f"methods.{method.get('id')}" if method.get("dimension") not in DIMENSIONS: errors.append(f"R1 {where}: dimension 必须是 {'|'.join(DIMENSIONS)}") if method.get("applies_to") not in APPLIES_TO: errors.append(f"R1 {where}: applies_to 必须是 {'|'.join(APPLIES_TO)}") for field in METHOD_TEXT_FIELDS: if field in method and (not isinstance(method[field], str) or not method[field].strip()): errors.append(f"R1 {where}: {field} 必须是非空字符串") evidence = method.get("evidence") if not isinstance(evidence, list) or not evidence: errors.append(f"R4 {where}: evidence 至少一条") for item in evidence if isinstance(evidence, list) else []: ok = item in fact_ids or ( isinstance(item, str) and item.startswith("measure:") and _anchor_ok(item[len("measure:"):], measurements, derived)) if not ok: errors.append(f"R4 {where}: evidence {item!r} 既不是已有 fact id,也不是可解析的 measure:路径") targets = method.get("targets", {}) if not isinstance(targets, dict): errors.append(f"R1 {where}: targets 必须是对象") continue for name, target in targets.items(): twhere = f"{where}.targets.{name}" if not re.fullmatch(r"[a-z][a-z0-9_]*", str(name)): errors.append(f"R1 {twhere}: 名字只用小写字母、数字和下划线") if not _closed(target, "target", twhere, errors): continue if set(target) & {"value", "provenance"}: errors.append(f"R5 {twhere}: 数值与 provenance 由 export 填写,只写 from") path = target.get("from") if "from" in target and not _target_ok(path, resolve(path, measurements, derived)): errors.append(f"R5 {twhere}: from {path!r} 必须是 shots/loudness/derived 下的数值叶子或允许的派生对象") def _ngrams(text, size): """CJK n-grams per punctuation-free run, plus over all CJK joined (so「你可知道,我是…」still matches).""" runs = _CJK.findall(text) grams = set() for run in [*runs, "".join(runs)]: grams.update(run[i:i + size] for i in range(len(run) - size + 1)) return grams def _word_grams(text, size): words = [w.lower() for w in _WORD.findall(text)] return {tuple(words[i:i + size]) for i in range(len(words) - size + 1)} def _strings(node): if isinstance(node, str): yield node elif isinstance(node, dict): for value in node.values(): yield from _strings(value) elif isinstance(node, list): for value in node: yield from _strings(value) def _string_items(values): """The string entries of a JSON list; a non-list or a non-string entry contributes nothing.""" return [value for value in values if isinstance(value, str)] if isinstance(values, list) else [] def _research_names(research): """Character names and aliases, short cultural-note items, and short quoted terms. background_research.json is agent-written: a name or alias that is not a string (a nested object, a list) is ignored rather than fed to the name set.""" names = set() if not isinstance(research, dict): return names characters = research.get("characters") if isinstance(characters, dict): names.update(characters) elif isinstance(characters, list): names.update(_string_items([c.get("name") for c in characters if isinstance(c, dict)])) details = research.get("character_details") for name, info in details.items() if isinstance(details, dict) else []: names.add(name) names.update(_string_items(info.get("aliases") if isinstance(info, dict) else None)) notes = research.get("cultural_notes") for note in notes if isinstance(notes, list) else []: item = note.get("item") if isinstance(note, dict) else None if isinstance(item, str) and _CJK.fullmatch(item) and len(item) <= 5: names.add(item) for text in _strings(research): names.update( g for match in _QUOTED.findall(text) for g in match if g and g not in _GENERIC_QUOTED_TERMS ) return names def _grams(texts): return (set().union(*(_ngrams(t, CJK_RUN_MIN) for t in texts)) if texts else set(), set().union(*(_word_grams(t, LATIN_RUN_MIN) for t in texts)) if texts else set()) def leak_corpus(facts, asr_segments, asr_evidence, research): """Entity names and source text that must never reach a transferable method. Source text is the ASR (each window and each adjacent pair joined, so a line split by a window boundary still matches) plus the background research prose; the agent's own fact statements are kept apart so an error can say which side a shared phrase came from. """ names = {e for f in facts for e in (f.get("entities") or []) if isinstance(e, str)} research = research if isinstance(research, dict) else {} asr_evidence = asr_evidence if isinstance(asr_evidence, dict) else {} names.update(_research_names(research)) glossary = asr_evidence.get("glossary") names.update(_string_items(glossary.get("names") if isinstance(glossary, dict) else None)) windows = sorted((s for s in asr_segments or [] if isinstance(s, dict)), key=lambda s: float(s.get("start") or 0)) texts = [str(s.get("text") or "") for s in windows] texts += [a + b for a, b in zip(texts, texts[1:])] texts += list(_strings(research)) cjk, latin = _grams(texts) fact_cjk, fact_latin = _grams([str(f.get("statement") or "") for f in facts]) return { "names": sorted({_SPACE.sub("", n) for n in names if isinstance(n, str) and len(_SPACE.sub("", n)) >= 2}, key=len, reverse=True), "cjk": cjk, "latin": latin, "fact_cjk": fact_cjk - cjk, "fact_latin": fact_latin - latin, } def leak_errors(text, where, corpus, rule="R6"): """R6 a–d on one string; each error quotes the offending substring.""" errors = [] lowered = _SPACE.sub("", text).lower() for name in corpus["names"]: if name.isascii(): matched = re.search(rf"(?