Restore the strict refmod-isolation validator: token-echo check catches scattered re-descriptions the n-gram check missed

This commit is contained in:
Z User 2026-10-30 21:54:01 +00:00
parent fb271a8399
commit 7df79bcf09
2 changed files with 1 additions and 339 deletions

View File

@ -59,57 +59,6 @@ H3_REFERENCE_SECTIONS = ["subject_definitions", "summary", "retention_analysis",
"detailed_description", "overall_soundscape",
"non_diegetic_music"]
# Ref2VA (full-reference) mode: the six native H3 reference sections in canonical
# order, mirroring web/h3_prompt_schema_core.mjs H3_REFERENCE_SECTIONS. The skill
# emits this format; t2va (3-line) stays as an auto-detected legacy path.
REF2VA_SECTIONS = ["subject_definitions", "summary", "retention_analysis",
"detailed_description", "overall_soundscape", "non_diegetic_music"]
REF2VA_TASK_DIRECTIVE = "[reference generation]"
REF2VA_RETENTION_MARKERS = {"fully_preserved", "partially_preserved",
"attribute_transfer", "weak_reference"}
# Rule 7 - a subject_definitions line comes in exactly one of three forms:
# 1. no reference image -> "<Subject N> is a <age> ... with <hair>, <eyes> and
# <build>" - physical traits only: never clothing, never a nude/bare skin
# state
# 2. reference image -> "<Subject N> binds to <Image N> : <what the image
# actually shows>" - faithful to the image; the writer is multimodal
# 3. user refmod -> "<Subject N> identity: <verbatim>; voice: <verbatim>"
# - the user's own tested wording, carried through untouched (exempt from
# the no-image content bans: clothing and skin state)
# No form carries a length cap - describe fully what the model needs.
# No-image and binds subjects get their full look woven into the [Shot 1] body;
# a refmod subject is referenced by its <Subject N> label alone - the refmod
# text never leaves subject_definitions.
REF2VA_REFMOD_RE = re.compile(r"^identity\s*:", re.I)
REF2VA_BINDS_RE = re.compile(r"^binds to <Image\s+\d+>\s*:\s*", re.I)
REF2VA_DEF_SKINSTATE_RE = re.compile(
r"\b(?:nude|naked|unclothed|undressed|bare|barefoot|topless)\b", re.I)
REF2VA_DEF_CLOTHING_RE = re.compile(
r"\b(?:wearing|wears|dressed|dresses|dress|outfit|garment|clothes|clothing|"
r"uniform|scrubs?|jacket|coat|shirt|blouse|trousers|pants|jeans|shorts|skirt|"
r"gown|hoodie|sweater|shoes|sneakers|boots|sandals|socks|stockings|gloves|"
r"helmet|lanyard)\b", re.I)
def _refmod_echo(anchor, text, n=5):
"""First run of >= n consecutive refmod words echoed in text, else None.
A refmod subject's definition is the single source of its identity and look:
the [Shot 1] body must reference the label, never restate the refmod text.
"""
aw = re.sub(r"[^a-z0-9]+", " ", anchor.lower()).split()
tw = re.sub(r"[^a-z0-9]+", " ", text.lower()).split()
if len(aw) < n:
s = " ".join(aw)
return s if s and s in " ".join(tw) else None
grams = {" ".join(aw[i:i + n]) for i in range(len(aw) - n + 1)}
tjoin = " ".join(tw)
for g in sorted(grams):
if g in tjoin:
return g
return None
FENCE_RE = re.compile(r"^\s*```[a-zA-Z0-9_-]*\s*\n(.*?)\n?\s*```\s*$", re.S)
DIALOGUE_SPAN_RE = re.compile(r"<d>(.*?)</d>", re.S)
MARKER_RE = re.compile(r"^\[[^\]]+\]\s*")
@ -224,192 +173,6 @@ def _h3_frame_length(seconds):
return length
def detect_prompt_mode(prompt_text):
"""t2va (3-line) vs ref2va (six-section) - mirrors detectH3Mode()."""
if re.search(r"^(?:subject_definitions|summary|retention_analysis|"
r"detailed_description):", prompt_text, re.M):
return "ref2va"
return "t2va"
def _split_ref2va_sections(lines, where):
"""Group ref2va prompt lines into [(section, [content lines])] in file order.
A line whose stripped form starts with '<section>:' opens that section (its
content is the rest of that line plus following lines until the next header) -
the same rule parseH3Sections() applies."""
records = []
for line in lines:
stripped = line.strip()
hit = None
for sec in REF2VA_SECTIONS:
if stripped.startswith(sec + ":"):
hit = sec
break
if hit is not None:
rest = stripped[len(hit) + 1:].strip()
records.append((hit, [rest] if rest else []))
elif records:
records[-1][1].append(stripped)
else:
raise ValueError(f"{where}: ref2va prompt must open with "
f"'subject_definitions:', got {stripped[:60]!r}")
return records
def check_ref2va_prompt(lines, dur, where, prev_handoff=None):
"""Structural + craft checks for one ref2va prompt array.
Returns (shot_body, soundscape_text, dialogue_spans, defined_labels, wc) so
the fragment chain (handoff mirror, outline match) can reuse them; wc is the
detailed_description word count the thin-scene floor was applied to.
"""
if (not isinstance(lines, list) or len(lines) < 6
or not all(isinstance(line, str) and line.strip() for line in lines)):
raise ValueError(f"{where}: ref2va prompt must be an array of non-empty "
f"strings (headers on their own line, content on the "
f"following lines)")
records = _split_ref2va_sections(lines, where)
names = [name for name, _ in records]
if sorted(names) != sorted(REF2VA_SECTIONS):
missing = [s for s in REF2VA_SECTIONS if s not in names]
raise ValueError(f"{where}: ref2va sections missing {missing} - all six of "
f"{REF2VA_SECTIONS} are required, each exactly once")
if names != REF2VA_SECTIONS:
raise ValueError(f"{where}: ref2va sections must be in canonical order "
f"{REF2VA_SECTIONS}, got {names}")
by_name = dict(records)
summary = " ".join(by_name["summary"]).strip()
if summary != REF2VA_TASK_DIRECTIVE:
raise ValueError(f"{where}: summary must be exactly {REF2VA_TASK_DIRECTIVE!r} "
f"(every scene is built from the wired subject references), "
f"got {summary[:60]!r}")
defs = by_name["subject_definitions"]
if not defs:
raise ValueError(f"{where}: subject_definitions is empty")
defined = []
refmod_anchors = {}
for line in defs:
m = re.match(r"^(<(?:Subject|Picture|Video|Audio)\s+\d+>)\s+\S", line)
if not m:
raise ValueError(f"{where}: subject_definitions lines must start with a "
f"<Subject N> label followed by one of the three "
f"definition forms (is-a physical traits / binds to "
f"<Image N> / verbatim refmod): {line[:60]!r}")
if m.group(1) in defined:
raise ValueError(f"{where}: {m.group(1)} is defined more than once")
anchor = line[m.end(1):].strip()
if anchor.startswith("&"):
raise ValueError(f"{where}: {m.group(1)} keeps the user's &...& wrapper - "
f"strip the & marks and carry the refmod text verbatim "
f"('<Subject N> identity: ...; voice: ...'): {line[:60]!r}")
if REF2VA_REFMOD_RE.match(anchor):
defined.append(m.group(1)) # verbatim user refmod text - never edited
refmod_anchors[m.group(1)] = anchor
continue
mb = REF2VA_BINDS_RE.match(anchor)
if mb:
desc = anchor[mb.end():].strip()
if not desc:
raise ValueError(f"{where}: {m.group(1)} binds line needs a description "
f"after the colon - look at <Image N> and write what "
f"you see: {line[:60]!r}")
else:
if not re.match(r"^is\b", anchor, re.I):
raise ValueError(f"{where}: {m.group(1)} definition is none of the three "
f"forms - '<Subject N> is a ...' (physical traits only: "
f"age, hair, eyes, build), '<Subject N> binds to <Image "
f"N> : ...' (what the image shows), or the user's "
f"verbatim refmod '<Subject N> identity: ...; voice: "
f"...': {line[:60]!r}")
if REF2VA_DEF_SKINSTATE_RE.search(anchor):
raise ValueError(f"{where}: {m.group(1)} has no reference image, so its "
f"definition is physical traits only and carries a "
f"nude/bare skin state - weave the skin state into the "
f"[Shot 1] body where the scene shows it: {line[:60]!r}")
if REF2VA_DEF_CLOTHING_RE.search(anchor):
raise ValueError(f"{where}: {m.group(1)} describes clothing in a no-image "
f"definition - physical traits only (age, hair, eyes, "
f"build); weave every garment into the [Shot 1] body "
f"where the action touches it: {line[:60]!r}")
defined.append(m.group(1))
if "<Subject 1>" not in defined:
raise ValueError(f"{where}: <Subject 1> must be defined in subject_definitions")
retained = {}
for line in by_name["retention_analysis"]:
m = re.match(r"^(<(?:Subject|Picture|Video|Audio)\s+\d+>)\s*:\s*(\S+)\s*$", line)
if not m or m.group(2) not in REF2VA_RETENTION_MARKERS:
raise ValueError(f"{where}: retention_analysis lines must be "
f"'<Subject N>: fully_preserved' (or partially_preserved / "
f"attribute_transfer / weak_reference): {line[:60]!r}")
retained[m.group(1)] = m.group(2)
for label in defined:
if label not in retained:
raise ValueError(f"{where}: {label} has no retention_analysis entry")
dd = "\n".join(by_name["detailed_description"]).strip()
if "[Shot 1]" not in dd:
raise ValueError(f"{where}: detailed_description must contain '[Shot 1]'")
style_part, _, body = dd.partition("[Shot 1]")
body = body.strip()
if not style_part.strip():
raise ValueError(f"{where}: detailed_description needs 1-2 style sentences "
f"before [Shot 1] (medium, lighting quality, palette)")
wc = len((style_part + " " + body).split())
floor = THIN_FLOOR_LONG if dur >= 10 else THIN_FLOOR_SHORT
if wc < floor:
raise ValueError(f"{where}: detailed_description is only {wc} words - too thin "
f"to carry the elements of a scene composition (subject, "
f"wardrobe, style, continuity, blocking, camera, performance, "
f"texture, sound, exit state)")
if "<Subject 1>" not in body:
raise ValueError(f"{where}: the [Shot 1] body must reference <Subject 1> and "
f"carry the head-to-toe look woven into the action (for a "
f"refmod subject the label reference is enough - its "
f"definition carries the look)")
for rlabel, ranchor in refmod_anchors.items():
echo = _refmod_echo(ranchor, dd)
if echo:
raise ValueError(f"{where}: {rlabel} is defined by a verbatim refmod - the "
f"refmod text lives only in subject_definitions; reference "
f"{rlabel} by its label alone in the [Shot 1] body and "
f"never restate its identity, voice, or look (echoed "
f"{echo!r})")
if " is now " in dd.lower():
raise ValueError(f"{where}: change stated as bare outcome instead of shown progression")
if re.search(r"\bbare\b", dd, re.I):
print(f" note: {where} description says 'bare' - call uncovered body parts "
f"'nude' (e.g. 'nude arms')")
ssc_text = "\n".join(by_name["overall_soundscape"]).strip()
if not ssc_text:
raise ValueError(f"{where}: overall_soundscape is empty")
ndm_text = "\n".join(by_name["non_diegetic_music"]).strip()
if ndm_text.upper() != "N/A":
print(f" note: {where} non_diegetic_music is {ndm_text[:40]!r} - N/A unless "
f"the user asked for a score")
spans = [MARKER_RE.sub("", span, count=1).strip()
for span in DIALOGUE_SPAN_RE.findall(dd)]
if spans and not re.search(r"silently with mouth", dd):
raise ValueError(f"{where}: dialogue span lacks a 'silently with mouth' cue")
if prev_handoff is not None:
prev_ssc = ""
if isinstance(prev_handoff, dict):
prev_ssc = str(prev_handoff.get("soundscape", ""))
if prev_ssc and not CONTINUITY_RE.search(ssc_text):
raise ValueError(f"{where}: soundscape does not continue the previous audio "
f"bed (say 'the same ... continues')")
if prev_ssc and not CONTINUITY_RE.search(body):
raise ValueError(f"{where}: description does not open inside the previous "
f"end_state (use a continuity cue: 'Continue the same ...', "
f"'without resetting the motion')")
return body, ssc_text, spans, defined, wc
def validate_plan(raw, default_duration=15.0, default_steps=20):
if isinstance(raw, list):
raw = {"shots": raw}
@ -734,8 +497,6 @@ def check_ref2va_sections(plan):
Ref2VA prompt in canonical order with legal definitions and isolation."""
assert plan["prompt_prefix"] == [""], \
"prompt_prefix must be exactly [''] (empirical: better H3 results, no prose preamble)"
counts = []
modes = []
for idx, shot in enumerate(plan["shots"], 1):
text = _prompt_text(shot.get("prompt", ""), f"shot {idx}")
dur = float(shot.get("duration_seconds",
@ -878,7 +639,7 @@ def validate_fragment(obj, index=1, total=None, prev_handoff=None, expect=None):
raise ValueError(f"{where}: _handoff.dialogue_used {used} does not mirror the "
f"<d> spans {spans}")
if mode == "t2va" and prev_handoff is not None:
if prev_handoff is not None:
prev_line = ""
if isinstance(prev_handoff, dict):
prev_line = str(prev_handoff.get("soundscape", ""))
@ -1049,111 +810,12 @@ NURSE_BINDS = {
}
# Ref2VA fragment: the six native H3 reference sections, one line per element,
# headers on their own line. handoff.soundscape copies the soundscape CONTENT
# line (the element after the overall_soundscape: header), not the header.
REF2VA_NURSE = {
"id": "corridor_walk",
"duration_seconds": 8,
"prompt": [
"subject_definitions:",
"<Subject 1> binds to <Image 1> : a woman in her thirties with dark brown "
"hair in a low bun, calm grey eyes, slim build, pale green scrubs, white "
"sneakers.",
"summary:",
"[reference generation]",
"retention_analysis:",
"<Subject 1>: fully_preserved",
"detailed_description:",
"Photorealistic cool fluorescent lighting, soft clinical palette, stable "
"over-the-shoulder tracking shots.",
"[Shot 1] Continue the same walk along the corridor without resetting the "
"motion. <Subject 1>, the same night nurse from her reference image with a "
"slim build and dark brown hair pulled back in a low bun, pushes the steel "
"trolley forward with both hands, its wheels squeaking softly on the linoleum, "
"a paper cup and a clipped chart riding on top. Her pale green scrub top and "
"matching scrub trousers hang smooth as she leans into the push, her nude "
"hands below the scrub cuffs wrapped around the cold handle, and her white "
"sneakers land heel first and roll to the toe, her nude lower legs showing "
"above the sneaker collars with each stride. The camera keeps a steady "
"over-the-shoulder framing at her shoulder height, gliding at her pace, "
"fluorescent panels passing overhead in even rhythm, the corridor walls "
"sliding past in soft focus. The cup's surface trembles with each push, and "
"the hem of her scrub top sways in counterpoint to the trolley's roll. Her "
"jaw stays set, her calm grey eyes forward, one loose strand of hair lifting "
"with each step; the trolley's shadow stretches and compresses under each "
"ceiling panel. Reaching the blue door at the corridor's end, she slows the "
"trolley to a stop and her right hand leaves the handle, closing on the "
"door's cold push bar.",
"overall_soundscape:",
"the same low fluorescent hum and distant trolley squeak continue, a soft "
"draught breathing under the far door.",
"non_diegetic_music:",
"N/A",
],
"_handoff": {
"end_state": "She is stopped at the blue door at the corridor's end, right "
"hand closing on the cold push bar, trolley at her hip, camera "
"still at her shoulder.",
"soundscape": "the same low fluorescent hum and distant trolley squeak "
"continue, a soft draught breathing under the far door.",
"dialogue_used": [],
},
}
# DOOR exercises the third definition form: a user refmod carried through
# verbatim (identity + voice), exempt from the no-image content bans.
REF2VA_DOOR = {
"id": "door_stop",
"duration_seconds": 8,
"prompt": [
"subject_definitions:",
"<Subject 1> identity: The night nurse is a woman in her thirties with "
"dark brown hair in a low bun and calm grey eyes; voice: The night nurse "
"speaks in a low, unhurried, even voice.",
"summary:",
"[reference generation]",
"retention_analysis:",
"<Subject 1>: fully_preserved",
"detailed_description:",
REF2VA_NURSE["prompt"][7],
"[Shot 1] Continue the same corridor walk without resetting the motion, the "
"trolley still rolling at her pace. <Subject 1> eases the steel trolley to "
"a stop beside the blue door, both hands relaxing on the handle one after "
"the other, its last wheel squeak dying on the linoleum. The camera holds "
"its over-the-shoulder framing while her shoulders settle and the paper "
"cup's surface stills on the trolley top; the fluorescent panels keep "
"their even rhythm overhead and the corridor walls stay in soft focus. "
"Her right hand leaves the handle, opens, and closes on the door's cold "
"push bar while the clipped chart keeps swaying on its clip.",
"overall_soundscape:",
"the same low fluorescent hum continues, the trolley wheels fall silent, a "
"soft draught breathing under the far door.",
"non_diegetic_music:",
"N/A",
],
"_handoff": {
"end_state": "Her right hand is closing on the cold push bar, trolley at her "
"hip, cup stilled, camera at her shoulder.",
"soundscape": "the same low fluorescent hum continues, the trolley wheels "
"fall silent, a soft draught breathing under the far door.",
"dialogue_used": [],
},
}
def _short_fragment(**over):
base = json.loads(json.dumps(CANDY_BROWSE)) # deep copy
base.update(over)
return base
def _ref2va_short(**over):
base = json.loads(json.dumps(REF2VA_NURSE)) # deep copy
base.update(over)
return base
def run_fragment_suite():
print("Fragment edge cases that must PASS:")
handoff = validate_fragment(dict(CANDY_BROWSE), index=1, total=3)