"""Write a prompt's "fixtures" files into its OUTDIR before the agent runs.
python3 evals/write_fixtures.py OUTDIR PROMPT_ID [PROMPTS_JSON] # one prompt
python3 evals/write_fixtures.py ITERATION_DIR --all [PROMPTS_JSON] # every prompt, into ITERATION_DIR/<id>/
A prompt in agent_prompts_24.json may carry {"fixtures": {"cues_zh.txt": "..."}}. Those files are
text the prompt refers to (cue files in the prompt's language); they are written by the harness,
never committed, so the repo stays free of generated media and generated fixtures. Existing files
are overwritten. Prompts without a "fixtures" key write nothing.
"""
import json
import ntpath
import os
import posixpath
import re
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
def absolute_output_dir(text: str, name: str, outdir: Path) -> str:
"""A batch recipe's "output_dir" is resolved against the CALLER's cwd, not against the folder
of inputs, so a relative "out" in a staged fixture wrote somewhere the run never looked and
the agent had to rewrite the recipe (eval 18 bp1/bp2). Stage it absolute, under OUTDIR.
"""
if not name.endswith(".json"):
return text
try:
doc = json.loads(text)
except ValueError:
return text
if not isinstance(doc, dict) or not isinstance(doc.get("output_dir"), str):
return text
stated = os.path.normpath(doc["output_dir"]).replace("\\", "/")
if posixpath.isabs(stated) or ntpath.isabs(doc["output_dir"]):
return text
doc["output_dir"] = str((outdir / doc["output_dir"]).resolve())
return json.dumps(doc, indent=2, ensure_ascii=False) + "\n"
def write(outdir: Path, prompt: dict) -> list:
written = []
for name, text in (prompt.get("fixtures") or {}).items():
if "/" in name or "\\" in name or name.startswith("."):
raise SystemExit(f"fixture name must be a plain file name: {name!r}")
outdir.mkdir(parents=True, exist_ok=True)
target = outdir / name
text = absolute_output_dir(text, name, outdir)
target.write_text(text, encoding="utf-8")
written.append(target)
names = prompt.get("emoji_assets") or []
if names:
import subprocess
assets = outdir / "emoji"
assets.mkdir(parents=True, exist_ok=True)
palette = ("orange", "gold", "tomato", "limegreen", "deepskyblue")
for i, name in enumerate(names):
target = assets / (name + ".png")
subprocess.run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-f", "lavfi",
"-i", "color=c=%s:s=72x72:d=0.04" % palette[i % len(palette)],
"-vf", "format=rgba", "-frames:v", "1", str(target)], check=True)
written.append(target)
for name in prompt.get("media_fixtures") or []:
target = outdir / name
if not target.exists():
outdir.mkdir(parents=True, exist_ok=True)
build_media_fixture(name, target)
written.append(target)
return written
def build_media_fixture(name: str, target: Path) -> None:
"""Synthetic media eval 22's Set B prompts need, ffmpeg-generated on demand (never
committed) so the repo stays free of generated media. Ground truth for each file is
documented next to its build here, and was verified against the tool it exercises
(scenes.py --shots, silence.py --speech-aware, sync.py, multicam.py --switch energy)
when these prompts were staged as scratchpad/eval22."""
import subprocess
def ffmpeg(*args):
subprocess.run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", *[str(a) for a in args]], check=True)
if name == "shots.mp4":
ffmpeg("-f", "lavfi", "-i",
"life=size=80x45:rate=30:mold=32:ratio=0.1:death_color=#101030:life_color=#39ff88:seed=1",
"-t", "4", "-vf", "scale=640:360:flags=neighbor", target.parent / "_shots_a.mp4")
ffmpeg("-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30",
"-t", "4", "-vf", "crop=640:360:'80*t':0", target.parent / "_shots_b.mp4")
ffmpeg("-f", "lavfi", "-i", "color=c=0x101832:s=640x360:rate=30",
"-f", "lavfi", "-i", "color=c=0xffcc00:s=80x80:rate=30",
"-filter_complex", "[0:v][1:v]overlay=x='(640-80)*t/4':y=140", "-t", "4",
target.parent / "_shots_c.mp4")
list_file = target.parent / "_shots_concat.txt"
list_file.write_text("\n".join(f"file '{target.parent / n}'" for n in
("_shots_a.mp4", "_shots_b.mp4", "_shots_c.mp4")) + "\n",
encoding="utf-8")
ffmpeg("-f", "concat", "-safe", "0", "-i", list_file, "-c:v", "libx264",
"-preset", "ultrafast", "-pix_fmt", "yuv420p", target)
for n in ("_shots_a.mp4", "_shots_b.mp4", "_shots_c.mp4"):
(target.parent / n).unlink(missing_ok=True)
list_file.unlink(missing_ok=True)
elif name == "source.mp4":
ffmpeg("-f", "lavfi", "-i", "testsrc2=size=1280x720:rate=30",
"-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000", "-t", "8",
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", target)
elif name == "speech_breaths.m4a":
expr = ("0.5*sin(2*PI*180*t)*(lt(t\\,1.2)+between(t\\,1.5\\,2.6)+between(t\\,2.9\\,4.0)"
"+between(t\\,5.5\\,6.7)+between(t\\,7.0\\,8.2)+gt(t\\,9.7))")
ffmpeg("-f", "lavfi", "-i", f"aevalsrc='{expr}':s=48000", "-t", "11", "-c:a", "aac", target)
elif name in ("camA.mp4", "camB.mp4"):
loud_first = name == "camA.mp4"
hue = "" if loud_first else ",hue=h=90"
vol_expr = ("0.8*sin(2*PI*440*t)*lt(t\\,6)+0.01*sin(2*PI*440*t)*gt(t\\,6)" if loud_first else
"0.01*sin(2*PI*440*t)*lt(t\\,6)+0.8*sin(2*PI*440*t)*gt(t\\,6)")
ffmpeg("-f", "lavfi", "-i", f"testsrc2=size=160x90:rate=30{hue}",
"-f", "lavfi", "-i", f"aevalsrc='{vol_expr}':s=48000",
"-t", "12", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
"-c:a", "aac", target)
elif name == "lav.m4a":
vol_expr = "0.8*sin(2*PI*440*t)*lt(t\\,6)+0.01*sin(2*PI*440*t)*gt(t\\,6)"
ffmpeg("-f", "lavfi", "-i", f"aevalsrc='{vol_expr}':s=48000", "-t", "9.5",
"-af", "adelay=2500|2500", "-c:a", "aac", target)
elif name == "cam3.m4a":
vol_expr = "0.8*sin(2*PI*440*(t+1.18))*lt(t+1.18\\,6)+0.01*sin(2*PI*440*(t+1.18))*gt(t+1.18\\,6)"
ffmpeg("-f", "lavfi", "-i", f"aevalsrc='{vol_expr}':s=48000", "-t", "10.82", "-c:a", "aac", target)
elif re.fullmatch(r"tts_\d+\.wav", name):
idx = int(re.search(r"\d+", name).group())
ffmpeg("-f", "lavfi", "-i", f"sine=frequency={300 + 60 * idx}:sample_rate=24000",
"-t", "1", "-ac", "1", target)
elif re.fullmatch(r"(seg|cam)_?\w*\d+\.mp4|[ab]\.mp4", name):
ffmpeg("-f", "lavfi", "-i", "testsrc2=size=320x180:rate=30",
"-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000", "-t", "3",
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", target)
else:
raise SystemExit(f"no media fixture builder for {name!r}")
def main(argv) -> int:
args = [a for a in argv if a != "--all"]
every = "--all" in argv
if not args:
raise SystemExit(__doc__)
root = Path(args[0])
if every:
prompts_json = Path(args[1]) if len(args) > 1 else HERE / "agent_prompts_24.json"
prompts = json.loads(prompts_json.read_text(encoding="utf-8"))
for p in prompts:
for f in write(root / p["id"], p):
print(f)
return 0
if len(args) < 2:
raise SystemExit(__doc__)
prompts_json = Path(args[2]) if len(args) > 2 else HERE / "agent_prompts_24.json"
prompts = {p["id"]: p for p in json.loads(prompts_json.read_text(encoding="utf-8"))}
if args[1] not in prompts:
raise SystemExit(f"no prompt {args[1]} in {prompts_json}")
for f in write(root, prompts[args[1]]):
print(f)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))