Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 9.8 KiB · executableRawPermalink
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# Copyright (c) 2026 UtopiaIA
4"""Builds the README's banner: the logo's strands, the tagline, and a
5terminal with the landings of the agents recording, so that the banner and
6docs/assets/demo-agents.gif show the same run, ids and all.
7
8usage: hero.py <site/index.html> <agents.cast | transcript.txt> <out dir>
9
10From a recording (asciicast v2, as record.py writes it) it takes the lines
11from the note "# landing on main ..." to the end; a .txt file gives those
12lines as they are. The lines about the working copy and what to run next
13are left out to keep the banner short: the GIF has them all. A recording
14whose story changed stops it, rather than giving a banner that no longer
15matches.
16
17It writes hero-dark.svg and hero-light.svg, 1280 wide, each a final frame
18that CSS animates in once where the reader allows motion. There is no
19narrow variant: for a reader who has chosen a theme, GitHub rewrites the
20colour-scheme media queries of a README's <picture>, and a source that
21also tests the screen's width then shows on wide screens too.
22
23The terminal stays dark in both themes, as on the site. No scripts, no
24external fonts or images: GitHub shows these files through <img>.
25"""
26import html
27import json
28import os
29import re
30import sys
31
32SITE, SOURCE, OUT = sys.argv[1:4]
33
34SERIF = "'Iowan Old Style', Georgia, 'Palatino Linotype', Palatino, serif"
35MONO = "ui-monospace, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace"
36
37# site.css's tokens, and og/og.svg's glows: (colour, opacity, cx, cy, r).
38THEMES = {
39 "dark": {
40 "ground": "#090a11",
41 "glows": [("#54dcc6", .2, .12, .02, .62), ("#8272ff", .24, .9, .02, .66),
42 ("#c8a06a", .12, .3, 1.05, .7)],
43 "edge": "rgba(150,146,184,.22)",
44 "tagline": "#eae8f2",
45 "url": "#7f7ca0",
46 "strands": ("#c8a06a", "#54dcc6", "#8272ff"),
47 "term_edge": "rgba(150,146,184,.28)",
48 },
49 "light": {
50 "ground": "#f6f4ee",
51 "glows": [("#13917f", .1, .12, .02, .62), ("#5b4ae6", .09, .9, .02, .66),
52 ("#a77a3a", .08, .3, 1.05, .7)],
53 "edge": "rgba(21,20,31,.12)",
54 "tagline": "#15141f",
55 "url": "#5f5b7d",
56 "strands": ("#a77a3a", "#13917f", "#5b4ae6"),
57 "term_edge": "rgba(21,20,31,.22)",
58 },
59}
60# The terminal's palette, the same in both themes: the recordings' own.
61TERM = {"bg": "#0e0f18", "text": "#eae8f2", "dim": "#b3afce", "muted": "#7f7ca0",
62 "exit": "#e779cf", "agent-a": "#c8a06a", "agent-b": "#54dcc6", "agent-c": "#8272ff"}
63
64# The canvas and its side margin, the logo's scale and top, the font size
65# and baseline of the URL and of the tagline, and the terminal's top, font
66# size and line height.
67WIDTH, MARGIN = 1280, 48
68LOGO_SCALE, LOGO_TOP = .75, 24
69URL_SIZE, URL_BASE = 22, 70
70TAG_SIZE, TAG_BASE = 50, 158
71TERM_TOP, SIZE, STEP = 194, 24, 35
72TAGLINE = "Many agents. One main. No retry loop."
73
74
75def transcript(path):
76 text = open(path, encoding="utf-8").read()
77 if path.endswith(".cast"):
78 events = [json.loads(line) for line in text.splitlines()[1:] if line.strip()]
79 text = "".join(event[2] for event in events if event[1] == "o")
80 text = re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", text)
81 text = text.replace("\r\n", "\n").replace("\r", "")
82 lines = [line.rstrip() for line in text.splitlines()]
83 starts = [i for i, line in enumerate(lines) if line.startswith("# landing on main")]
84 if len(starts) != 1:
85 sys.exit(f"{path}: no single '# landing on main' note")
86 lines = lines[starts[0]:]
87 while lines and not lines[-1]:
88 lines.pop()
89 lines = [line for line in lines if line and not line.startswith(("Updated ", "Run `"))]
90 # The story the banner tells, in order; anything else means the
91 # recording changed, and the banner and the README with it.
92 story = [r"# landing on main", r"\[agent-a\] \$ braid land$",
93 r"Landed \w{12} on main \(fast-forward\)$", r"\[agent-b\] \$ braid land$",
94 r"Landed on main as \w{12}, rebased onto \w{12}$", r"\[agent-c\] \$ braid land$",
95 r"\w{12} conflicts with main at \w{12}; main did not move:$",
96 r" CONTENT +shipping\.toml +\(1 region\)$", r"exit status 2$"]
97 if len(lines) != len(story) or not all(re.match(s, l) for s, l in zip(story, lines)):
98 sys.exit(f"{path}: the landings are not the story the banner tells:\n" + "\n".join(lines))
99 return lines
100
101
102def colours(line):
103 """The coloured runs of one line: a prompt, a note, the exit status or
104 braid's output."""
105 if line.startswith("# "):
106 return [(line, "muted")]
107 prompt = re.match(r"\[(agent-[abc])\] \$ (.*)$", line)
108 if prompt:
109 return [(f"[{prompt[1]}]", prompt[1]), (" $ ", "muted"), (prompt[2], "text")]
110 if re.match(r"exit status \d+$", line):
111 return [(line, "exit")]
112 return [(line, "dim")]
113
114
115def logo_parts():
116 page = open(SITE, encoding="utf-8").read()
117 svg = re.search(r'<div class="hero-logo">(<svg.*?</svg>)</div>', page, re.S).group(1)
118 defs = re.search(r"<defs>.*?</defs>", svg, re.S).group(0).replace("hl-cut", "hero-cut")
119 strands = re.findall(r'<path d="([^"]+)"[^>]*class="(s[abc])"[^>]*mask="url\(#hl-cut(\d)\)"', svg)
120 assert len(strands) == 3, strands
121 paths = "\n".join(
122 f'<path class="s {cls}" d="{d}" mask="url(#hero-cut{n})" pathLength="1"/>'
123 for d, cls, n in strands)
124 return defs, paths
125
126
127def build(theme, lines, defs, paths):
128 t = THEMES[theme]
129 first = TERM_TOP + 44
130 term_h = 44 + STEP * (len(lines) - 1) + 24
131 height = TERM_TOP + term_h + 40
132 term_x, term_w = MARGIN, WIDTH - 2 * MARGIN
133 text_x = term_x + 34
134
135 out = []
136 w = out.append
137 w("<!-- SPDX-License-Identifier: Apache-2.0\n Copyright (c) 2026 UtopiaIA\n"
138 " Made by scripts/demos/hero.py from the agents recording: edit that, not this. -->")
139 w(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {WIDTH} {height}" '
140 f'width="{WIDTH}" height="{height}" role="img" aria-labelledby="hero-title hero-desc">')
141 w("<title id=\"hero-title\">Elffuss Braid: Many agents. One main. No retry loop.</title>")
142 w("<desc id=\"hero-desc\">Three agents land on main in turn: agent-a as a fast-forward, "
143 "agent-b rebased onto it, and agent-c with a conflict, so main did not move and braid "
144 "exits with status 2.</desc>")
145 sa, sb, sc = t["strands"]
146 w("<style>")
147 w(f".s {{ fill: none; stroke-width: 9.5; stroke-linecap: round; stroke-linejoin: round; "
148 f"stroke-dasharray: 1 2; stroke-dashoffset: 0; }}")
149 w(f".sa {{ stroke: {sa}; }} .sb {{ stroke: {sb}; }} .sc {{ stroke: {sc}; }}")
150 w(f".tag {{ font-family: {SERIF}; font-size: {TAG_SIZE}px; font-weight: 600; "
151 f"letter-spacing: -0.3px; fill: {t['tagline']}; }}")
152 w(f".url {{ font-family: {MONO}; font-size: {URL_SIZE}px; letter-spacing: 1px; fill: {t['url']}; }}")
153 w(f".ln {{ font-family: {MONO}; font-size: {SIZE}px; white-space: pre; }}")
154 w("/* plays once, then holds: the last frame is the file as written */")
155 w("@media (prefers-reduced-motion: no-preference) {")
156 w(" .s { animation: draw 1.5s cubic-bezier(.22, .61, .36, 1) both; }")
157 w(" .sb { animation-delay: .15s; } .sc { animation-delay: .3s; }")
158 w(" .tag { animation: show .8s ease-out .45s both; }")
159 w(" .url { animation: show .8s ease-out .8s both; }")
160 w(" .term { animation: show .5s ease-out 1s both; }")
161 w(" .ln { animation: show .3s ease-out both; }")
162 w("}")
163 w("@keyframes draw { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }")
164 w("@keyframes show { from { opacity: 0; } to { opacity: 1; } }")
165 w("</style>")
166
167 w("<defs>")
168 w(f'<clipPath id="hero-card"><rect width="{WIDTH}" height="{height}" rx="24"/></clipPath>')
169 for i, (colour, opacity, cx, cy, r) in enumerate(t["glows"]):
170 w(f'<radialGradient id="hero-glow{i}" cx="{cx}" cy="{cy}" r="{r}">'
171 f'<stop offset="0" stop-color="{colour}" stop-opacity="{opacity}"/>'
172 f'<stop offset="1" stop-color="{colour}" stop-opacity="0"/></radialGradient>')
173 w("</defs>")
174 w('<g clip-path="url(#hero-card)">')
175 w(f'<rect width="{WIDTH}" height="{height}" fill="{t["ground"]}"/>')
176 for i in range(len(t["glows"])):
177 w(f'<rect width="{WIDTH}" height="{height}" fill="url(#hero-glow{i})"/>')
178 w("</g>")
179 w(f'<rect x=".5" y=".5" width="{WIDTH - 1}" height="{height - 1}" rx="23.5" fill="none" '
180 f'stroke="{t["edge"]}"/>')
181
182 # The logo's strands start 17 units in, counting their round caps.
183 w(f'<g transform="translate({MARGIN - 17 * LOGO_SCALE:.2f} {LOGO_TOP}) scale({LOGO_SCALE})">')
184 w(defs)
185 w(paths)
186 w("</g>")
187 w(f'<text class="url" x="{WIDTH - MARGIN}" y="{URL_BASE}" text-anchor="end">braid.elffuss.com</text>')
188 w(f'<text class="tag" x="{MARGIN}" y="{TAG_BASE}">{html.escape(TAGLINE)}</text>')
189
190 w('<g class="term">')
191 w(f'<rect x="{term_x}" y="{TERM_TOP}" width="{term_w}" height="{term_h}" rx="16" '
192 f'fill="{TERM["bg"]}" stroke="{t["term_edge"]}"/>')
193 for i, colour in enumerate((TERM["agent-a"], TERM["agent-b"], TERM["agent-c"])):
194 w(f'<rect x="{term_x + 1 + 3 * i}" y="{TERM_TOP + 22}" width="3" height="{term_h - 44}" '
195 f'fill="{colour}"/>')
196 w("</g>")
197 for i, line in enumerate(lines):
198 runs = "".join(f'<tspan fill="{TERM[c]}">{html.escape(s, quote=False)}</tspan>'
199 for s, c in colours(line))
200 w(f'<text class="ln" x="{text_x}" y="{first + i * STEP}" xml:space="preserve" '
201 f'style="animation-delay: {1.1 + .22 * i:.2f}s">{runs}</text>')
202 w("</svg>")
203 return "\n".join(out) + "\n"
204
205
206def main():
207 lines = transcript(SOURCE)
208 defs, paths = logo_parts()
209 for theme in THEMES:
210 name = os.path.join(OUT, f"hero-{theme}.svg")
211 svg = build(theme, lines, defs, paths)
212 with open(name, "w", encoding="utf-8") as f:
213 f.write(svg)
214 print(name, len(svg.encode()), "bytes")
215
216
217main()