The best way to control Claude Code, Codex and more from any computer or phone

main
astro 332 lines 10.6 KB
Raw
1 ---
2 /**
3 * `<BackfillFanout />` — how a Domain backfill reaches every instance, and
4 * how the tree grows with the Domain. A relay given more than six items splits
5 * them among six child relays (Cloudflare runs at most six outgoing calls per
6 * invocation), so the tree gets deeper, not wider: about log6 of the instance
7 * count levels. The figure draws one path down the tree for 10 thousand, 100
8 * thousand and a million instances, level by level; at each level the other
9 * five relays do the same work beside it. Counts per level are approximate:
10 * six to the power of the level.
11 */
12
13 const sizes = [
14 { n: 10_000, label: "10k" },
15 { n: 100_000, label: "100k" },
16 { n: 1_000_000, label: "1M" },
17 ];
18 ---
19
20 <figure class="bf not-content" id="bf" aria-label="A backfill tree growing deeper as the Domain grows">
21 <div class="bf-top">
22 <span class="bf-step" id="bf-step"></span>
23 <span class="bf-sizes" role="group" aria-label="Instances">
24 {sizes.map((s, i) => <button type="button" data-size={i}>{s.label}</button>)}
25 </span>
26 <button type="button" id="bf-pause" aria-pressed="false">❚❚ Pause</button>
27 </div>
28 <svg id="bf-svg" viewBox="0 0 820 420" role="img" aria-hidden="true"></svg>
29 <figcaption class="bf-cap">
30 One path down the tree; at every level the other five relays do the same beside it. Each relay calls at most
31 six at once, because a Cloudflare Worker invocation runs at most six outgoing calls at a time.
32 </figcaption>
33 </figure>
34
35 <style is:global>
36 .bf {
37 margin: 2rem 0;
38 padding: 12px 14px 10px;
39 font: 14px/1.5 var(--alc-font-sans);
40 color: var(--alc-fg-1);
41 background: var(--alc-bg-elev-1);
42 border: 1px solid var(--alc-hairline-2);
43 border-radius: 12px;
44 }
45 .bf-top {
46 display: flex;
47 align-items: flex-start;
48 gap: 10px;
49 min-height: 3em; /* two lines, so a longer step doesn't move the tree */
50 margin-bottom: 6px;
51 }
52 .bf-step {
53 flex: 1;
54 font-weight: 700;
55 font-size: 15px;
56 }
57 .bf-sizes {
58 display: inline-flex;
59 gap: 4px;
60 }
61 .bf button {
62 all: unset;
63 flex: none;
64 cursor: pointer;
65 padding: 3px 10px;
66 font-size: 12px;
67 font-weight: 600;
68 color: var(--alc-fg-1);
69 background: var(--alc-bg-elev-1);
70 border: 1px solid var(--alc-hairline-3);
71 border-radius: 7px;
72 }
73 .bf button[aria-current="true"] {
74 color: var(--alc-accent-text);
75 border-color: var(--alc-accent);
76 }
77 .bf button:focus-visible {
78 outline: 2px solid var(--alc-accent);
79 }
80 .bf svg {
81 display: block;
82 width: 100%;
83 height: auto;
84 transition: height 0.4s ease;
85 }
86 .bf-label {
87 font: 600 11px var(--alc-font-mono);
88 fill: var(--alc-fg-3);
89 }
90 .bf-count {
91 font: 600 11px var(--alc-font-mono);
92 fill: var(--alc-fg-3);
93 text-anchor: end;
94 }
95 .bf-edge {
96 fill: none;
97 stroke: var(--alc-accent);
98 stroke-width: 1.2;
99 stroke-opacity: 0.6;
100 stroke-dasharray: 1;
101 stroke-dashoffset: 1;
102 transition: stroke-dashoffset 0.45s ease;
103 }
104 .bf-stub {
105 stroke: var(--alc-accent);
106 stroke-opacity: 0.35;
107 stroke-width: 1;
108 stroke-dasharray: 2 3;
109 opacity: 0;
110 transition: opacity 0.4s ease 0.3s;
111 }
112 .bf-node {
113 opacity: 0;
114 transition: opacity 0.3s ease 0.3s;
115 }
116 .bf-on .bf-edge {
117 stroke-dashoffset: 0;
118 }
119 .bf-on .bf-node,
120 .bf-on .bf-stub,
121 .bf-on .bf-row-text {
122 opacity: 1;
123 }
124 .bf-row-text {
125 opacity: 0;
126 transition: opacity 0.3s ease 0.2s;
127 }
128 .bf-node rect {
129 fill: var(--alc-bg-elev-1);
130 stroke: var(--alc-accent);
131 stroke-width: 1.2;
132 }
133 .bf-node.bf-root rect {
134 fill: color-mix(in srgb, var(--alc-accent) 22%, transparent);
135 }
136 .bf-node text {
137 font: 700 12px var(--alc-font-mono);
138 fill: var(--alc-fg-1);
139 text-anchor: middle;
140 }
141 .bf-relay {
142 fill: var(--alc-bg-elev-1);
143 stroke: var(--alc-accent);
144 stroke-width: 1.2;
145 }
146 .bf-relay.bf-path {
147 fill: color-mix(in srgb, var(--alc-accent) 45%, transparent);
148 }
149 .bf-relay.bf-side {
150 stroke-opacity: 0.5;
151 }
152 .bf-instance {
153 fill: #e3b341; /* aggregate */
154 }
155 .bf-key {
156 fill: #7fb069; /* view */
157 }
158 .bf-cap {
159 margin-top: 6px;
160 font-size: 13px;
161 color: var(--alc-fg-3);
162 text-align: center;
163 }
164 </style>
165
166 <script define:vars={{ sizes }}>
167 const root = document.getElementById("bf");
168 if (root) {
169 const NS = "http://www.w3.org/2000/svg";
170 const FAN = 6;
171 const W = 820;
172 const LEFT = 150;
173 const RIGHT = 760;
174 const ROW = 42;
175 const TOP = 26;
176 const GAP = 34; // between the six children of one relay
177
178 const svg = document.getElementById("bf-svg");
179 const label = document.getElementById("bf-step");
180 const pause = document.getElementById("bf-pause");
181 const buttons = [...root.querySelectorAll("[data-size]")];
182 const reduced = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
183 const fmt = (n) => n.toLocaleString("en-US");
184
185 let paused = false;
186 let pinned = false;
187 let token = 0;
188 const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
189 const wait = async (ms, mine) => {
190 await sleep(ms);
191 while (paused && mine === token) await sleep(120);
192 return mine === token;
193 };
194
195 const el = (name, attrs, parent) => {
196 const node = document.createElementNS(NS, name);
197 for (const [k, v] of Object.entries(attrs)) node.setAttribute(k, String(v));
198 parent.appendChild(node);
199 return node;
200 };
201 const curve = (x1, y1, x2, y2) => {
202 const my = (y1 + y2) / 2;
203 return `M${x1} ${y1}C${x1} ${my} ${x2} ${my} ${x2} ${y2}`;
204 };
205 const clamp = (x) => Math.max(LEFT + GAP * 2.5, Math.min(RIGHT - GAP * 2.5, x));
206
207 /**
208 * Rows: the run, then relay levels until the instances. Levels of relays:
209 * about log6(n), the depth at which six-way splits reach every instance.
210 */
211 const build = (n) => {
212 const depth = Math.ceil(Math.log(n) / Math.log(FAN));
213 const relayLevels = depth - 1;
214 svg.replaceChildren();
215 const rows = [];
216 const rowY = (i) => TOP + i * ROW;
217 const height = rowY(relayLevels + 3) - 10;
218 svg.setAttribute("viewBox", `0 0 ${W} ${height}`);
219
220 // The run.
221 let x = (LEFT + RIGHT) / 2;
222 const runRow = el("g", {}, svg);
223 const runNode = el("g", { class: "bf-node bf-root" }, runRow);
224 el("rect", { x: x - 60, y: rowY(0) - 13, width: 120, height: 26, rx: 7 }, runNode);
225 el("text", { x, y: rowY(0) + 4 }, runNode).textContent = "Backfill run";
226 el("text", { class: "bf-label bf-row-text", x: 8, y: rowY(0) + 4 }, runRow).textContent = "run";
227 rows.push(runRow);
228
229 // Relay levels, then the instances and the keys they feed.
230 let from = { x, y: rowY(0) + 13 };
231 for (let level = 1; level <= relayLevels + 1; level++) {
232 const instances = level === relayLevels + 1;
233 const y = rowY(level);
234 const row = el("g", {}, svg);
235 const center = clamp(x);
236 const kids = Array.from({ length: FAN }, (_, i) => center + (i - (FAN - 1) / 2) * GAP);
237 const next = (level % 2 === 0 ? 3 : 2);
238 for (const [i, kx] of kids.entries()) {
239 el("path", { class: "bf-edge", d: curve(from.x, from.y, kx, y - 7), pathLength: 1 }, row);
240 if (instances) {
241 el("circle", { class: "bf-node bf-instance", cx: kx, cy: y, r: 5 }, row);
242 } else {
243 const path = i === next;
244 el("circle", { class: `bf-node bf-relay ${path ? "bf-path" : "bf-side"}`, cx: kx, cy: y, r: 6 }, row);
245 if (!path) el("line", { class: "bf-stub", x1: kx, y1: y + 7, x2: kx, y2: y + 20 }, row);
246 }
247 }
248 const count = instances ? n : Math.min(FAN ** level, Math.ceil(n / FAN));
249 el("text", { class: "bf-label bf-row-text", x: 8, y: y + 4 }, row).textContent = instances
250 ? "instances"
251 : `relays · level ${level}`;
252 el("text", { class: "bf-count bf-row-text", x: W - 8, y: y + 4 }, row).textContent =
253 `${instances ? "" : "≈ "}${fmt(count)}`;
254 rows.push(row);
255 x = kids[next];
256 from = { x, y: y + 7 };
257 if (instances) {
258 // Each instance's history reaches the keys it feeds.
259 const keys = el("g", {}, svg);
260 const ky = rowY(level + 1);
261 for (const kx of kids) {
262 el("path", { class: "bf-edge", d: `M${kx} ${y + 6}L${kx} ${ky - 6}`, pathLength: 1 }, keys);
263 el("circle", { class: "bf-node bf-key", cx: kx, cy: ky, r: 5 }, keys);
264 }
265 el("text", { class: "bf-label bf-row-text", x: 8, y: ky + 4 }, keys).textContent = "view keys";
266 rows.push(keys);
267 }
268 }
269 return { rows, depth };
270 };
271
272 const show = async (i, mine) => {
273 const { n } = sizes[i];
274 buttons.forEach((b, j) => b.setAttribute("aria-current", String(i === j)));
275 const { rows, depth } = build(n);
276 const text = `${fmt(n)} instances: ${depth} levels of calls, each relay calling at most six at once.`;
277 if (reduced()) {
278 rows.forEach((r) => r.classList.add("bf-on"));
279 label.textContent = text;
280 return true;
281 }
282 label.textContent = `${fmt(n)} instances`;
283 void svg.getBoundingClientRect();
284 for (const r of rows) {
285 r.classList.add("bf-on");
286 if (!(await wait(450, mine))) return false;
287 }
288 label.textContent = text;
289 return true;
290 };
291
292 const play = async (start = 0) => {
293 const mine = ++token;
294 if (reduced()) return void show(sizes.length - 1, mine);
295 for (let i = start; ; i = (i + 1) % sizes.length) {
296 if (!(await show(i, mine))) return;
297 if (!(await wait(3000, mine))) return;
298 if (pinned) return;
299 }
300 };
301
302 buttons.forEach((b, i) =>
303 b.addEventListener("click", () => {
304 pinned = true;
305 paused = false;
306 pause.textContent = "❚❚ Pause";
307 pause.setAttribute("aria-pressed", "false");
308 void play(i);
309 }),
310 );
311 pause.onclick = () => {
312 paused = !paused;
313 pause.textContent = paused ? "▶ Play" : "❚❚ Pause";
314 pause.setAttribute("aria-pressed", String(paused));
315 };
316
317 // Until it scrolls into view, show the largest tree, drawn.
318 build(sizes[sizes.length - 1].n).rows.forEach((r) => r.classList.add("bf-on"));
319 label.textContent = "1,000,000 instances: 8 levels of calls, each relay calling at most six at once.";
320 buttons.forEach((b, j) => b.setAttribute("aria-current", String(j === sizes.length - 1)));
321 const io = new IntersectionObserver(
322 (entries) => {
323 if (entries.some((e) => e.isIntersecting)) {
324 io.disconnect();
325 void play();
326 }
327 },
328 { threshold: 0.3 },
329 );
330 io.observe(root);
331 }
332 </script>