Relax timeline coverage and add prompt diagnostics

This commit is contained in:
ukr8b3g-cmyk
2026-08-15 13:10:34 +09:00
parent ec197341ef
commit 23b686e582
6 changed files with 161 additions and 30 deletions
+15 -8
View File
@@ -197,22 +197,29 @@ def validate_reference_prompts(prompts: list[str], reference_count: int) -> str:
for prompt in prompts:
found.update(int(value) for value in _PICTURE_TAG.findall(str(prompt)))
invalid = sorted(value for value in found if value < 1 or value > reference_count)
warnings: list[str] = []
if invalid:
unavailable = ", ".join(f"<Picture {value}>" for value in invalid)
return (
f"Warning: prompt references unavailable {unavailable}; only "
warnings.append(
f"H3C-P102 Warning: prompt references unavailable {unavailable}; only "
f"{reference_count} active reference image(s) reached the Sampler. "
"ComfyUI Core permits this and generation will continue, but the "
"unavailable reference may be ignored or hallucinated. Enable the "
"corresponding Reference Image input or remove the unavailable tag."
)
if not found:
return (
"Reference images are connected but the prompt contains no "
"<Picture N> tag; images still condition generation, but explicit "
"identity references are recommended."
missing = sorted(set(range(1, reference_count + 1)) - found)
if missing:
labels = ", ".join(f"<Picture {value}>" for value in missing)
detail = (
"the prompt contains no <Picture N> tag"
if not found
else f"connected reference {labels} is not explicitly mentioned in the prompt"
)
return ""
warnings.append(
f"H3C-P103 Warning: {detail}; images still condition generation, but "
"explicit identity references are recommended."
)
return "\n".join(warnings)
def encode_reference_prompt(
+2 -2
View File
@@ -202,14 +202,14 @@ def validate_reference_audio_prompts(
)
for value in unavailable:
warnings.append(
f"Warning: prompt references unavailable <Audio {value}>; only "
f"H3C-P102 Warning: prompt references unavailable <Audio {value}>; only "
f"{active_count} active reference audio item(s) reached the Sampler. "
"Core-compatible generation will continue; the tag may be ignored "
"or hallucinated."
)
if source is not None and 1 not in found:
warnings.append(
"Reference Audio 1 is connected but the prompt contains no <Audio 1> "
"H3C-P103 Warning: Reference Audio 1 is connected but the prompt contains no <Audio 1> "
"tag; audio still conditions generation, but an explicit tag is recommended."
)
return "\n".join(warnings)
+73 -3
View File
@@ -14,6 +14,7 @@ from ComfyUI_H3_Continuum_Join.v2.prompts import (
detect_prompt_mode,
make_prompt_plan,
parse_sparse_prompt_overrides,
prompt_plan_report,
validate_sparse_prompt_overrides,
validate_prompt_plan,
)
@@ -83,14 +84,83 @@ def test_timeline_preamble_is_applied_to_every_chunk():
assert plan["notes"] == ["Auto detected Timeline", "applied timeline preamble to all chunks"]
def test_timeline_requires_coverage():
with pytest.raises(PromptPlanError):
def test_timeline_missing_coverage_warns_and_repeats_previous_prompt():
plan = make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[0-5s]\none",
chunks=2,
chunk_seconds=5,
)
assert plan["prompts"] == ["one", "one"]
assert any(item["code"] == "H3C-P101" for item in plan["diagnostics"])
assert "fallback" in prompt_plan_report(plan)
def test_timeline_leading_gap_uses_earliest_valid_prompt():
plan = make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[5-10s]\nsecond",
chunks=2,
chunk_seconds=5,
)
assert plan["prompts"] == ["second", "second"]
assert plan["diagnostics"][0]["code"] == "H3C-P101"
def test_timeline_extra_sections_warn_and_are_ignored():
plan = make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[0-5s]\none\n[5-10s]\ntwo\n[10-15s]\nextra",
chunks=2,
chunk_seconds=5,
)
assert plan["prompts"] == ["one", "two"]
assert any(item["code"] == "H3C-P104" for item in plan["diagnostics"])
@pytest.mark.parametrize(
("script", "code"),
[
("[0 to 5s]\none", "H3C-P001"),
("[5-5s]\none", "H3C-P002"),
("[Chunk 0]\none", "H3C-P004"),
("[0-5s]\n", "H3C-P005"),
],
)
def test_invalid_timeline_reports_actionable_diagnostic(script, code):
with pytest.raises(PromptPlanError) as captured:
make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[0-5s]\none",
script=script,
chunks=2,
chunk_seconds=5,
)
message = str(captured.value)
assert code in message
assert "Suggested fix:" in message
def test_duplicate_chunk_section_is_an_error():
with pytest.raises(PromptPlanError, match="H3C-P003"):
make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[Chunk 1]\none\n[Chunk 1]\ntwo",
chunks=2,
chunk_seconds=5,
)
def test_prompt_preflight_reports_explicit_chunk_sources():
plan = make_prompt_plan(
mode=PROMPT_MODE_TIMELINE,
script="[0-5s]\none\n[5-10s]\ntwo",
chunks=2,
chunk_seconds=5,
)
report = prompt_plan_report(plan)
assert "Prompt Preflight:" in report
assert "Chunk 1 source:" in report
assert "Chunk 2 source:" in report
def test_native_h3_chunk_duration_rejects_sub_four_seconds():
+4
View File
@@ -79,6 +79,7 @@ def test_v323_third_reference_adds_only_the_image_3_extension():
def test_unavailable_picture_tags_warn_without_stopping_like_core():
assert validate_reference_prompts(["Use <Picture 1> and <Picture 2>."], 2) == ""
message = validate_reference_prompts(["Use <Picture 2>."], 1)
assert "H3C-P102" in message
assert "only 1 active reference image(s) reached the Sampler" in message
assert "generation will continue" in message
assert "ignored or hallucinated" in message
@@ -89,6 +90,7 @@ def test_unavailable_picture_tags_warn_without_stopping_like_core():
def test_missing_picture_tag_is_a_warning_not_an_error():
warning = validate_reference_prompts(["The same person walks forward."], 1)
assert "H3C-P103" in warning
assert "no <Picture N> tag" in warning
@@ -166,6 +168,7 @@ def test_unavailable_audio_tags_warn_without_stopping_like_core():
["Perform <Audio 1> and <Audio 2>."], None
)
assert "unavailable <Audio 1>" in message
assert "H3C-P102" in message
assert "unavailable <Audio 2>" in message
assert "generation will continue" in message
@@ -176,6 +179,7 @@ def test_connected_audio_keeps_unavailable_extra_tag_as_warning():
)
assert "unavailable <Audio 2>" in message
assert "no <Audio 1> tag" in message
assert "H3C-P103" in message
def _reference_prompt(checkpoint_name: str) -> dict:
+66 -16
View File
@@ -13,12 +13,19 @@ from ..constants import (
from ..version import PROMPT_PLAN_SCHEMA_VERSION
_TIMELINE_HEADER=re.compile(r"^\s*\[\s*(?P<start>\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?\s*[-–—]\s*(?P<end>\d+(?:\.\d+)?)\s*(?:s|sec|seconds)?\s*\]\s*$",re.IGNORECASE)
_CHUNK_HEADER=re.compile(r"^\s*\[\s*(?:chunk|clip)\s*(?P<index>\d+)\s*\]\s*$",re.IGNORECASE)
_TIMELINE_LIKE_HEADER=re.compile(r"^\s*\[\s*(?:(?:chunk|clip)\b|\d+(?:\.\d+)?[^\]]*(?:[-–—]|\bto\b))",re.IGNORECASE)
_LIST_SEPARATOR=re.compile(r"^\s*---+\s*$",re.MULTILINE)
class PromptPlanError(ValueError): pass
def _prompt_error(code,reason,*,line_number=None,source=None,suggested_fix):
location=f" line {line_number}" if line_number is not None else ""
lines=[f"{code}{location}: {reason}"]
if source is not None: lines.append(f"Source: {source}")
if suggested_fix: lines.append(f"Suggested fix: {suggested_fix}")
raise PromptPlanError("\n".join(lines))
def prompt_hash(prompt:str)->str: return hashlib.sha256(prompt.encode("utf-8")).hexdigest()
def _normalize_prompt(value:str,*,label:str)->str:
text=str(value).strip()
if not text: raise PromptPlanError(f"{label} is empty")
if not text: _prompt_error("H3C-P005",f"{label} is empty",suggested_fix="provide non-empty prompt text")
return text
def _parse_json_list(script:str):
stripped=script.lstrip()
@@ -49,22 +56,26 @@ def _parse_timeline_sections(script,*,allow_preamble=True):
if current is None:
if any(line.strip() for line in body) and not allow_preamble: raise PromptPlanError("timeline text before the first [0-5s] or [Chunk 1] header is not allowed")
body=[]; return
if not any(line.strip() for line in body):
_prompt_error("H3C-P005","timeline section body is empty",line_number=current["line_number"],source=current["source"],suggested_fix="add prompt text below this header")
prompt=_normalize_prompt("\n".join(body),label="timeline section")
current["prompt"]=f"{preamble}\n\n{prompt}" if preamble else prompt
sections.append(current); current=None; body=[]
for line in str(script).splitlines():
for line_number,line in enumerate(str(script).splitlines(),start=1):
time_match=_TIMELINE_HEADER.match(line); chunk_match=_CHUNK_HEADER.match(line)
if time_match or chunk_match:
finish()
if time_match:
start=float(time_match.group("start")); end=float(time_match.group("end"))
if not end>start: raise PromptPlanError(f"timeline range must increase: {line.strip()}")
current={"kind":"time","start":start,"end":end}
if not end>start: _prompt_error("H3C-P002","timeline end must be greater than its start",line_number=line_number,source=line.strip(),suggested_fix="use an increasing range such as [0-5s]")
current={"kind":"time","start":start,"end":end,"line_number":line_number,"source":line.strip()}
else:
index=int(chunk_match.group("index"))
if index<1: raise PromptPlanError("chunk numbers are one-based")
current={"kind":"chunk","index":index}
if index<1: _prompt_error("H3C-P004","chunk numbers are one-based",line_number=line_number,source=line.strip(),suggested_fix="use [Chunk 1] or a larger one-based chunk number")
current={"kind":"chunk","index":index,"line_number":line_number,"source":line.strip()}
continue
if _TIMELINE_LIKE_HEADER.match(line):
_prompt_error("H3C-P001","invalid timeline header",line_number=line_number,source=line.strip(),suggested_fix="use [0-5s] or [Chunk 1]")
body.append(line)
finish()
if not sections: raise PromptPlanError("timeline contains no sections")
@@ -88,25 +99,53 @@ def validate_sparse_prompt_overrides(overrides,*,chunks):
if not validated: raise PromptPlanError("Sparse Clip Overrides contains no overrides")
return validated
def _parse_timeline(script,chunks,chunk_seconds):
sections=_parse_timeline_sections(script); prompts=[]; notes=[]
sections=_parse_timeline_sections(script); prompts=[]; notes=[]; diagnostics=[]
if _timeline_preamble(script): notes.append("applied timeline preamble to all chunks")
seen_chunks={}; total_end=chunks*chunk_seconds; usable=[]
for item in sections:
if item["kind"]=="chunk":
index=int(item["index"])
if index in seen_chunks:
_prompt_error("H3C-P003",f"timeline defines Chunk {index} more than once",line_number=item["line_number"],source=item["source"],suggested_fix=f"keep only one [Chunk {index}] section")
seen_chunks[index]=item
if index<=chunks: usable.append(item)
else: diagnostics.append({"level":"warning","code":"H3C-P104","message":f"ignored {item['source']} because the configured sequence has only {chunks} chunk(s)"})
elif min(total_end,item["end"])-max(0.0,item["start"])>0:
usable.append(item)
else:
diagnostics.append({"level":"warning","code":"H3C-P104","message":f"ignored {item['source']} because it is outside 0-{total_end:.3f}s"})
if not usable:
_prompt_error("H3C-P005","timeline contains no usable prompt section for the configured sequence",suggested_fix="add a section such as [0-5s] with non-empty prompt text")
previous_prompt=None; previous_source=None
for chunk_index in range(1,chunks+1):
chunk_sections=[item for item in sections if item["kind"]=="chunk" and item["index"]==chunk_index]
if len(chunk_sections)>1: raise PromptPlanError(f"timeline defines Chunk {chunk_index} more than once")
if chunk_sections: prompts.append(chunk_sections[0]["prompt"]); continue
if chunk_sections:
selected=chunk_sections[0]; prompts.append(selected["prompt"]); previous_prompt=selected["prompt"]; previous_source=f"{selected['source']} at line {selected['line_number']}"
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: {previous_source}"}); continue
start=(chunk_index-1)*chunk_seconds; end=chunk_index*chunk_seconds; scored=[]
for order,item in enumerate(sections):
if item["kind"]!="time": continue
overlap=max(0.0,min(end,item["end"])-max(start,item["start"]))
if overlap>0: scored.append((overlap,-order,item))
if not scored: raise PromptPlanError(f"timeline does not cover chunk {chunk_index} ({start:.3f}-{end:.3f}s)")
scored.sort(reverse=True,key=lambda row:(row[0],row[1])); prompts.append(scored[0][2]["prompt"])
if not scored:
if previous_prompt is None:
selected=usable[0]; fallback_reason=f"used earliest valid prompt from {selected['source']}"
else:
selected=None; fallback_reason=f"reused previous prompt from {previous_source}"
prompt=selected["prompt"] if selected is not None else previous_prompt
source=f"{selected['source']} at line {selected['line_number']}" if selected is not None else previous_source
prompts.append(prompt); previous_prompt=prompt; previous_source=source
diagnostics.append({"level":"warning","code":"H3C-P101","message":f"Timeline does not cover Chunk {chunk_index} ({start:.3f}-{end:.3f}s); {fallback_reason}."})
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: fallback to {source}"}); continue
scored.sort(reverse=True,key=lambda row:(row[0],row[1])); selected=scored[0][2]; prompts.append(selected["prompt"]); previous_prompt=selected["prompt"]; previous_source=f"{selected['source']} at line {selected['line_number']}"
diagnostics.append({"level":"info","code":"H3C-P000","message":f"Chunk {chunk_index} source: {previous_source}"})
if len(scored)>1 and abs(scored[0][0]-scored[1][0])<1e-9: notes.append(f"chunk {chunk_index} had an equal-overlap timeline tie; used the earlier section")
return prompts,notes
return prompts,notes,diagnostics
def detect_prompt_mode(script):
text=str(script)
for line in text.splitlines():
if _TIMELINE_HEADER.match(line) or _CHUNK_HEADER.match(line): return PROMPT_MODE_TIMELINE
if _TIMELINE_LIKE_HEADER.match(line): _prompt_error("H3C-P001","invalid timeline header",source=line.strip(),suggested_fix="use [0-5s] or [Chunk 1]")
if _parse_json_list(text) is not None or _LIST_SEPARATOR.search(text): return PROMPT_MODE_LIST
return PROMPT_MODE_FIXED
def resolve_prompt_mode(mode,script):
@@ -124,9 +163,11 @@ def make_prompt_plan(*,mode,script,chunks,chunk_seconds):
detected={PROMPT_MODE_FIXED:PROMPT_FORMAT_FIXED,PROMPT_MODE_LIST:PROMPT_FORMAT_LIST,PROMPT_MODE_TIMELINE:PROMPT_FORMAT_TIMELINE}[resolved_mode]; notes.append(f"Auto detected {detected}")
if resolved_mode==PROMPT_MODE_FIXED: prompt=_normalize_prompt(script,label="fixed prompt"); prompts=[prompt]*chunks
elif resolved_mode==PROMPT_MODE_LIST: prompts,parse_notes=_parse_list(script,chunks); notes.extend(parse_notes)
elif resolved_mode==PROMPT_MODE_TIMELINE: prompts,parse_notes=_parse_timeline(script,chunks,chunk_seconds); notes.extend(parse_notes)
elif resolved_mode==PROMPT_MODE_TIMELINE: prompts,parse_notes,diagnostics=_parse_timeline(script,chunks,chunk_seconds); notes.extend(parse_notes)
else: raise PromptPlanError(f"unknown prompt mode: {resolved_mode!r}")
return {"magic":PROMPT_PLAN_MAGIC,"schema_version":PROMPT_PLAN_SCHEMA_VERSION,"mode":resolved_mode,"chunks":chunks,"chunk_seconds":chunk_seconds,"prompts":prompts,"hashes":[prompt_hash(p) for p in prompts],"notes":notes}
result={"magic":PROMPT_PLAN_MAGIC,"schema_version":PROMPT_PLAN_SCHEMA_VERSION,"mode":resolved_mode,"chunks":chunks,"chunk_seconds":chunk_seconds,"prompts":prompts,"hashes":[prompt_hash(p) for p in prompts],"notes":notes}
if resolved_mode==PROMPT_MODE_TIMELINE: result["diagnostics"]=diagnostics
return result
def validate_prompt_plan(plan):
if not isinstance(plan,dict) or plan.get("magic")!=PROMPT_PLAN_MAGIC: raise PromptPlanError("invalid H3 Continuum prompt plan")
if int(plan.get("schema_version",-1))!=PROMPT_PLAN_SCHEMA_VERSION: raise PromptPlanError(f"unsupported prompt-plan schema {plan.get('schema_version')}; expected {PROMPT_PLAN_SCHEMA_VERSION}")
@@ -137,6 +178,8 @@ def validate_prompt_plan(plan):
for index,prompt in enumerate(prompts):
_normalize_prompt(prompt,label=f"prompt {index+1}")
if hashes[index]!=prompt_hash(prompt): raise PromptPlanError(f"prompt-plan hash mismatch at chunk {index+1}")
diagnostics=plan.get("diagnostics")
if diagnostics is not None and (not isinstance(diagnostics,list) or not all(isinstance(item,dict) for item in diagnostics)): raise PromptPlanError("prompt-plan diagnostics are invalid")
return plan
def build_sampler_prompt_plan(*,prompt_mode,prompt_script,sequence_prompt,prompt_plan,chunks,chunk_seconds):
chunks=int(chunks); chunk_seconds=float(chunk_seconds)
@@ -153,9 +196,16 @@ def apply_prompt_overrides(plan,overrides):
if value is None: continue
prompts[index]=_normalize_prompt(value,label=f"Clip {index+1} Prompt"); replaced.append(index+1)
if not replaced: return plan
result=dict(plan); result["mode"]=PROMPT_MODE_LIST; result["prompts"]=prompts; result["hashes"]=[prompt_hash(p) for p in prompts]; result["notes"]=list(plan.get("notes") or [])+["external Clip Prompt input(s): "+", ".join(map(str,replaced))]
result=dict(plan); result["mode"]=PROMPT_MODE_LIST; result["prompts"]=prompts; result["hashes"]=[prompt_hash(p) for p in prompts]; result["notes"]=list(plan.get("notes") or [])+["external Clip Prompt input(s): "+", ".join(map(str,replaced))]; result.pop("diagnostics",None)
return validate_prompt_plan(result)
def prompt_plan_report(plan):
plan=validate_prompt_plan(plan); unique=len(set(plan["hashes"])); note="; ".join(plan.get("notes") or [])
result=f"Prompt plan: {plan['mode']}; {plan['chunks']} chunks; {unique} unique prompt(s); {plan['chunk_seconds']:.3f}s per chunk."
return result+(" "+note+"." if note else "")
if note: result+=" "+note+"."
diagnostics=list(plan.get("diagnostics") or [])
if not diagnostics: return result+"\nPrompt Preflight: OK."
lines=[result,"Prompt Preflight:"]
for item in diagnostics:
level=str(item.get("level","info")).upper(); code=str(item.get("code","H3C-P000")); message=str(item.get("message",""))
lines.append(f"- {level} {code}: {message}")
return "\n".join(lines)
+1 -1
View File
@@ -123,7 +123,7 @@ def run_sequence(*,model:Any,clip:Any,video_vae:Any,audio_vae:Any,sampler:Any,si
if reference_warning: reuse_notes.append(reference_warning)
if reference_audio_source is not None:
reuse_notes.insert(0,"Reference Audio 1 conditioning: persistent across all chunks.")
if reference_audio_warning: reuse_notes.append("Warning: "+reference_audio_warning)
if reference_audio_warning: reuse_notes.append(reference_audio_warning)
if session is not None:
old_fingerprint=str(session.get("model_fingerprint",""))
if old_fingerprint and old_fingerprint!=current_model_fingerprint: reuse_notes.append("model/accelerator fingerprint differs from the saved session; accepted chunks were kept")