Coverage for src/retrospeedlab/backend/recap/text.py: 98%
47 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
1import re
3from ..discord.writer import Check
4from ..narration.line import refuse, validated_line, writing_limit
5from ..promotion.style import emoji_count
6from .learner import BULLETS, EMOJI, PLAIN, STORY, Variant
7from .sections import BEST, CLOSER, HUMANS, LAB, LEVEL, RIVALRY, RUN, Section
9BRAND = "RetroSpeedLab"
10TITLE = f"Today at {BRAND}"
11CALENDAR = "📅"
12BULLET = "•"
13LIST_MARKS = "•-*· "
14EMOJIS = {LEVEL: "🏆", BEST: "⚡", RUN: "⏱️", CLOSER: "📍", LAB: "🧪", HUMANS: "🎮", RIVALRY: "🤖"}
15LINE_LIMIT = 160
16STORY_LIMIT = 400
17BRAND_SPELLINGS = re.compile(r"retro\W*speed\W*lab", re.IGNORECASE)
18SYSTEM = f"""You write the daily recap for the {BRAND} Discord server. {BRAND} is a game studio run by AI agents: \
19an AI game designer made original 8-bit games, a speedrun AI races them live on Twitch, Claude Code improves the \
20speedrun AI in lab runs, and people race the AI on the games site.
21Read the day's facts and write the recap for the community: friendly, short and concrete, leading with the first fact.
22Rules:
23- Use only the facts. Never invent anything; copy every number exactly as the facts write it.
24- Keep the facts' order and leave none out.
25- Spell the studio {BRAND}.
26- Never mention code, files, servers, prompts, keys or secrets.
27- No links, hashtags, quotes or markdown.
28- Reply with the recap only."""
29FORMS = {
30 BULLETS: f"Write one line per fact, each at most {writing_limit(LINE_LIMIT)} characters.",
31 STORY: f"Write the whole recap as one line of short sentences, at most {writing_limit(STORY_LIMIT)} characters.",
32}
33EMOJI_RULES = {EMOJI: "Start each line with one fitting emoji.", PLAIN: "Use no emoji."}
36def title(variant: Variant) -> str:
37 return f"{CALENDAR} {TITLE}" if variant.emoji == EMOJI else TITLE
40def template(sections: list[Section], variant: Variant) -> str:
41 with_emoji = variant.emoji == EMOJI
42 if variant.form == STORY:
43 story = " ".join(f"{section.line}." for section in sections)
44 return f"{EMOJIS[sections[0].kind]} {story}" if with_emoji else story
45 return "\n".join(f"{EMOJIS[section.kind] if with_emoji else BULLET} {section.line}" for section in sections)
48def prompt(sections: list[Section], variant: Variant) -> str:
49 facts = "\n".join(f"- {section.line}" for section in sections)
50 return f"Facts:\n{facts}\n\nForm:\n- {FORMS[variant.form]}\n- {EMOJI_RULES[variant.emoji]}"
53def checked_lines(text: str, facts: str, variant: Variant) -> list[str]:
54 lines = [line.strip().lstrip(LIST_MARKS) for line in text.splitlines() if line.strip()]
55 wanted = 1 if variant.form == STORY else len(variant.sections)
56 if len(lines) != wanted:
57 refuse(f"The recap has {len(lines)} lines, not {wanted}", text)
58 if any(spelled != BRAND for spelled in BRAND_SPELLINGS.findall(text)):
59 refuse(f"The recap misspells {BRAND}", text)
60 if variant.emoji == PLAIN and emoji_count(text):
61 refuse("The plain recap holds emoji", text)
62 limit = STORY_LIMIT if variant.form == STORY else LINE_LIMIT
63 return [validated_line(line, facts, limit) for line in lines]
66def recap_check(facts: str, variant: Variant) -> Check:
67 def checked(text: str) -> str:
68 lines = checked_lines(text, facts, variant)
69 if variant.form == BULLETS and variant.emoji == PLAIN:
70 return "\n".join(f"{BULLET} {line}" for line in lines)
71 return "\n".join(lines)
73 return checked