From f96a3344e7dc4979b55e25f99e3e836e35d425a6 Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Thu, 27 Aug 2026 08:08:53 +0000 Subject: [PATCH] Place ref context after anchor in H3 prompts --- dumas_h3_longvideos.py | 28 ++++++++++++++++++++++------ tests/test_dumas_h3_longvideos.py | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/dumas_h3_longvideos.py b/dumas_h3_longvideos.py index 8f39252..998e70a 100644 --- a/dumas_h3_longvideos.py +++ b/dumas_h3_longvideos.py @@ -4375,6 +4375,27 @@ def _reference_context_for_text(text, ref_slots): return " ".join(parts).strip() +def _inject_reference_context(block, context): + if not context: + return block + text = str(block or "").strip() + if not text: + return context + match = re.match(r"^(\[Generation \d+\]\s*)(.*)$", text, re.S) + if not match: + return f"{context} {text}".strip() + prefix, body = match.groups() + body = body.strip() + split_at = body.find(". ") + if split_at == -1: + return f"{prefix}{body} {context}".strip() + anchor = body[: split_at + 1] + rest = body[split_at + 2 :].strip() + if not rest: + return f"{prefix}{anchor} {context}".strip() + return f"{prefix}{anchor} {context} {rest}".strip() + + def _reference_character_memory(ref_slots): lines = [] seen = set() @@ -6410,12 +6431,7 @@ class H3LongVideos: for block in gens: context = _reference_context_for_text(block, ref_slots) if context: - block = re.sub( - r"^(\[Generation \d+\]\s*)", - lambda m: m.group(1) + context + " ", - block, - count=1, - ) + block = _inject_reference_context(block, context) enriched_gens.append(block) gens = enriched_gens diff --git a/tests/test_dumas_h3_longvideos.py b/tests/test_dumas_h3_longvideos.py index 1e18c8c..fddb3a3 100644 --- a/tests/test_dumas_h3_longvideos.py +++ b/tests/test_dumas_h3_longvideos.py @@ -340,6 +340,22 @@ class DumasH3LongVideosHelperTests(unittest.TestCase): self.assertIn("Persistent appearance for Mara: silver hair.", context) self.assertIn("Persistent wardrobe/style for Mara: red jacket.", context) + def test_reference_context_injects_after_anchor_sentence(self): + block = ( + "[Generation 1] Classic sitcom lighting and staging. " + "Duke walks into the room." + ) + context = "Character facts for Duke: female, 25 years old." + + result = self.module._inject_reference_context(block, context) + + self.assertEqual( + result, + "[Generation 1] Classic sitcom lighting and staging. " + "Character facts for Duke: female, 25 years old. " + "Duke walks into the room.", + ) + def test_reference_character_memory_uses_character_wardrobe_only(self): refs = [ {"kind": "character", "image": "img1", "name": "Mara", "wardrobe": "red jacket, black boots"},