"""Human feedback on earlier generations reaches the agents that write the next one.""" import inspect from app.repositories.intelligence import CreativeIntelligenceRepository from app.schemas import GenerationRequest class FakeStore: def __init__(self, tables): self.tables = tables def select(self, table, *, filters=None, limit=100, offset=0, order_by=None, descending=True, columns="*", gte=None, lte=None): return list(self.tables.get(table, []))[:limit] BRIEF = GenerationRequest(prompt="p", niche="auto insurance", target_audience="a", platform="meta", duration_seconds=30) def test_notes_with_a_reason_become_evidence_same_niche_first(): repo = CreativeIntelligenceRepository(FakeStore({ "feedback": [ {"id": "1", "asset_id": "a", "target_kind": "script", "verdict": "bad", "comment": "hook is weak", "quote": "Sorting through my bills", "scene_number": None}, {"id": "2", "asset_id": "b", "target_kind": "render", "verdict": "neutral", "comment": "b-roll not good, script slow", "quote": None, "scene_number": 4}, {"id": "3", "asset_id": "a", "target_kind": "script", "verdict": "good", "comment": None, "quote": None, "scene_number": None}, # nothing said: skipped ], "assets": [{"id": "a", "niche": "weight loss"}, {"id": "b", "niche": "Auto Insurance"}], })) notes = repo._human_notes(BRIEF) assert [n.id for n in notes] == ["2", "1"], "same-niche note first, empty note dropped" assert notes[0].kind == "human_note" and notes[0].relevance == 1.0 assert "NEUTRAL on the render (scene 4)" in notes[0].content assert "b-roll not good" in notes[0].content assert 'the line "Sorting through my bills"' in notes[1].content assert notes[1].relevance < notes[0].relevance def test_the_notes_ride_in_the_evidence_pack_and_a_broken_table_is_not_fatal(): class Broken(FakeStore): def select(self, table, **kw): if table == "feedback": raise RuntimeError("relation does not exist") return [] repo = CreativeIntelligenceRepository(Broken({})) assert repo._human_notes(BRIEF) == [] assert repo.build_evidence_pack(BRIEF).human_notes == [] def test_every_writing_agent_is_handed_the_notes(): from app.agents import broll, hook, script, visual from app.services import revalidate, workflow flow = workflow.CreativeWorkflow for fn in (flow._build_hook, flow._generate_script, flow._direct_visuals, flow._build_broll): assert '"human_notes"' in inspect.getsource(fn), fn.__name__ for fn in (revalidate._redirect_visuals, revalidate._replan_broll): assert '"human_notes"' in inspect.getsource(fn), fn.__name__ for mod in (broll, hook, script, visual): assert "`human_notes`" in inspect.getsource(mod), mod.__name__